blob: 4ff3fe1614c3b6c56d09b9c0b344e62009c98cf5 [file] [log] [blame]
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev7838cfd2020-06-03 14:16:43 -04003 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -08004 *
5 * This file is part of ndncert, a certificate management system based on NDN.
6 *
7 * ndncert is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ndncert authors and contributors.
19 */
20
21#ifndef NDNCERT_CA_CONFIG_HPP
22#define NDNCERT_CA_CONFIG_HPP
23
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070024#include <ndn-cxx/security/v2/certificate.hpp>
25
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -080026#include "certificate-request.hpp"
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070027#include "client-config.hpp"
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070028<<<<<<< HEAD
Alexander Afanasyev7838cfd2020-06-03 14:16:43 -040029#include <ndn-cxx/security/certificate.hpp>
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070030=======
31>>>>>>> Update CaConfig and ClientCaItem. Add INFO packet encoding and decoding.
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080032
33namespace ndn {
34namespace ndncert {
35
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080036/**
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070037 * @brief The name assignment function provided by the CA operator to generate available
38 * namecomponents.
39 * The function does not guarantee that all the returned names are available. Therefore the
40 * CA should further check the availability of each returned name and remove unavailable results.
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080041 *
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070042 * @p vector, input, a list of parameter key-value pair used for name assignment.
43 * @return a vector containing the possible namespaces derived from the parameters.
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080044 */
Suyong Won19fba4d2020-05-09 13:39:46 -070045using ProbeHandler = function<std::string /*identity name*/ (const Block& tlv /*requester input*/)>;
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070046using NameAssignmentFunc = function<std::vector<std::string>(const std::vector<std::tuple<std::string, std::string>>)>;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080047
48/**
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070049 * @brief The function would be invoked whenever the certificate request status is updated.
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080050 * The callback is used to notice the CA application or CA command line tool. The callback is
51 * fired whenever a request instance is created, challenge status is updated, and when certificate
52 * is issued.
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070053 *
54 * @p CertificateRequest, input, the state of the certificate request whose status is updated.
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080055 */
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070056using StatusUpdateCallback = function<void(const CertificateRequest&)>;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080057
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080058/**
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070059 * @brief CA's configuration on NDNCERT.
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070060 * For CA configuration format, please refer to:
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070061 * https://github.com/named-data/ndncert/wiki/NDNCERT-Protocol-0.3#213-ca-profile
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070062 *
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070063 * The format of CA configuration in JSON
64 * {
65 * "ca-prefix": "",
66 * "ca-info": "",
67 * "max-validity-period": "",
68 * "probe-parameters":
69 * [
70 * {"probe-parameter-key": ""},
71 * {"probe-parameter-key": ""}
72 * ]
73 * "supported-challenges":
74 * [
75 * {"challenge": ""},
76 * {"challenge": ""}
77 * ]
78 * }
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080079 */
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070080class CaConfig {
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080081public:
82 /**
83 * @brief Error that can be thrown from CaConfig
84 */
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070085 class Error : public std::runtime_error {
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080086 public:
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080087 using std::runtime_error::runtime_error;
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080088 };
89
90public:
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080091 /**
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070092 * Load CA configuration from the file.
93 *
94 * @param fileName, the configuration file name.
95 * @throw CaConfig::Error when config file does not exist or the configuration
96 * in the file cannot be parsed correctly.
97 * @throw CaConfig::Error when the ca-prefix attribute in JSON text is empty.
98 * @throw CaConfig::Error when the challenge is not specified or is not supported.
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080099 */
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800100 void
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -0800101 load(const std::string& fileName);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800102
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700103 /**
104 * Set the NameAssignmentFunction.
105 */
106 void
107 setNameAssignmentFunc(const NameAssignmentFunc& nameAssignmentFunc) {
108 m_nameAssignmentFunc = nameAssignmentFunc;
109 }
110
111 /**
112 * Set the StatusUpdateCallback.
113 */
114 void
115 setStatusUpdateCallback(const StatusUpdateCallback& statusUpdateCallback) {
116 m_statusUpdateCallback = statusUpdateCallback;
117 }
118
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -0800119private:
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800120 void
Zhiyi Zhangad6cf932017-10-26 16:19:15 -0700121 parse(const JsonSection& configJson);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800122
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700123 void
124 parseProbeParameters(const JsonSection& section);
125
126 void
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -0800127 parseChallengeList(const JsonSection& configSection);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800128
129public:
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700130 /**
131 * CA Name prefix (without /CA suffix).
132 */
133 Name m_caPrefix;
134 /**
135 * CA Information.
136 */
137 std::string m_caInfo;
138 /**
139 * A list of parameter-keys for PROBE.
140 */
141 std::list<std::string> m_probeParameterKeys;
142 /**
143 * Maximum allowed validity period of the certificate being requested.
144 * The value is in the unit of second.
145 */
146 time::seconds m_maxValidityPeriod;
147 /**
148 * A list of supported challenges.
149 */
150 std::list<std::string> m_supportedChallenges;
151 /**
152 * NameAssignmentFunc Callback function
153 */
154 NameAssignmentFunc m_nameAssignmentFunc;
155 /**
156 * StatusUpdate Callback function
157 */
158 StatusUpdateCallback m_statusUpdateCallback;
159
160 //====================old
161
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700162 // basic info
163 Name m_caName;
164
165 // essential config
166 time::seconds m_freshnessPeriod;
167 time::days m_validityPeriod;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700168
169 // optional parameters
170 std::string m_probe;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700171
172 // callbacks
173 ProbeHandler m_probeHandler;
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800174};
175
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700176} // namespace ndncert
177} // namespace ndn
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800178
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700179#endif // NDNCERT_CA_CONFIG_HPP