blob: 6b9edebbdad23b9d768228e4bf0a17afa622f61c [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 Zhange232a742020-09-29 17:34:17 -070024#include "request-state.hpp"
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080025
26namespace ndn {
27namespace ndncert {
28
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070029struct CaProfile {
Zhiyi Zhang9829da92020-09-30 16:19:34 -070030 /**
31 * CA Name prefix (without /CA suffix).
32 */
33 Name m_caPrefix;
34 /**
35 * CA Information.
36 * Default: "".
37 */
38 std::string m_caInfo;
39 /**
40 * A list of parameter-keys for PROBE.
41 * Default: empty list.
42 */
43 std::list<std::string> m_probeParameterKeys;
44 /**
45 * Maximum allowed validity period of the certificate being requested.
46 * The value is in the unit of second.
47 * Default: one day (86400 seconds).
48 */
49 time::seconds m_maxValidityPeriod;
50 /**
51 * Maximum allowed suffix length of requested name.
52 * E.g., When its value is 2, at most 2 name components can be assigned after m_caPrefix.
53 * Default: none.
54 */
55 boost::optional<size_t> m_maxSuffixLength;
56 /**
57 * A list of supported challenges. Only CA side will have m_supportedChallenges.
58 * Default: empty list.
59 */
60 std::list<std::string> m_supportedChallenges;
61 /**
62 * CA's certificate. Only Client side will have m_cert.
63 * Default: nullptr.
64 */
65 std::shared_ptr<security::v2::Certificate> m_cert;
66
67 void
68 parse(const JsonSection& configJson);
69
70 JsonSection
71 toJson() const;
72
73private:
74 void
75 parseProbeParameters(const JsonSection& section);
76
77 void
78 parseChallengeList(const JsonSection& configSection);
79};
80
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080081/**
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070082 * @brief The name assignment function provided by the CA operator to generate available
83 * namecomponents.
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -070084 * The function does not guarantee that all the returned names are available. Therefore the
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070085 * CA should further check the availability of each returned name and remove unavailable results.
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080086 *
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070087 * @p vector, input, a list of parameter key-value pair used for name assignment.
88 * @return a vector containing the possible namespaces derived from the parameters.
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080089 */
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070090using NameAssignmentFunc = function<std::vector<std::string>(const std::vector<std::tuple<std::string, std::string>>)>;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080091
92/**
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070093 * @brief The function would be invoked whenever the certificate request status is updated.
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080094 * The callback is used to notice the CA application or CA command line tool. The callback is
95 * fired whenever a request instance is created, challenge status is updated, and when certificate
96 * is issued.
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070097 *
Zhiyi Zhange232a742020-09-29 17:34:17 -070098 * @p RequestState, input, the state of the certificate request whose status is updated.
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080099 */
Zhiyi Zhange232a742020-09-29 17:34:17 -0700100using StatusUpdateCallback = function<void(const RequestState&)>;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +0800101
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800102/**
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700103 * @brief CA's configuration on NDNCERT.
Zhiyi Zhangad6cf932017-10-26 16:19:15 -0700104 * For CA configuration format, please refer to:
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700105 * https://github.com/named-data/ndncert/wiki/NDNCERT-Protocol-0.3#213-ca-profile
Zhiyi Zhangad6cf932017-10-26 16:19:15 -0700106 *
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700107 * The format of CA configuration in JSON
108 * {
109 * "ca-prefix": "",
110 * "ca-info": "",
111 * "max-validity-period": "",
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700112 * "max-suffix-length": "",
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700113 * "probe-parameters":
114 * [
115 * {"probe-parameter-key": ""},
116 * {"probe-parameter-key": ""}
117 * ]
118 * "supported-challenges":
119 * [
120 * {"challenge": ""},
121 * {"challenge": ""}
122 * ]
123 * }
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800124 */
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700125class CaConfig {
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800126public:
127 /**
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700128 * Load CA configuration from the file.
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700129 * @throw std::runtime_error when config file cannot be correctly parsed.
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800130 */
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800131 void
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -0800132 load(const std::string& fileName);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800133
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700134 CaProfile m_caItem;
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700135 /**
Zhiyi Zhangfde50112020-10-01 16:36:33 -0700136 * Used for CA redirection as specified in
137 * https://github.com/named-data/ndncert/wiki/NDNCERT-Protocol-0.3-PROBE-Extensions#probe-extension-for-redirection
138 */
Zhiyi Zhange537dd52020-10-01 18:02:24 -0700139 boost::optional<std::vector<std::shared_ptr<security::v2::Certificate>>> m_redirection;
Zhiyi Zhangfde50112020-10-01 16:36:33 -0700140 /**
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700141 * NameAssignmentFunc Callback function
142 */
143 NameAssignmentFunc m_nameAssignmentFunc;
144 /**
145 * StatusUpdate Callback function
146 */
147 StatusUpdateCallback m_statusUpdateCallback;
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800148};
149
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700150/**
151 * @brief Represents Client configuration
152 *
153 * For Client configuration format, please refer to:
154 * https://github.com/named-data/ndncert/wiki/Client-Configuration-Sample
155 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700156class RequesterCaCache {
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700157public:
158 /**
159 * @throw std::runtime_error when config file cannot be correctly parsed.
160 */
161 void
162 load(const std::string& fileName);
163
164 /**
165 * @throw std::runtime_error when config file cannot be correctly parsed.
166 */
167 void
168 load(const JsonSection& configSection);
169
170 void
171 save(const std::string& fileName) const;
172
173 void
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700174 removeCaProfile(const Name& caName);
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700175
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700176 /**
177 * Be cautious. This will add a new trust anchor for requesters.
178 */
179 void
180 addCaProfile(const CaProfile& profile);
181
182 std::list<CaProfile> m_caItems;
Zhiyi Zhang9829da92020-09-30 16:19:34 -0700183};
184
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700185} // namespace ndncert
186} // namespace ndn
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800187
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700188#endif // NDNCERT_CA_CONFIG_HPP