blob: 4855aca5099fbac3111e564dd4271892c4f26383 [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 Zhangdaf2fd72017-01-19 11:31:35 -080028
29namespace ndn {
30namespace ndncert {
31
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080032/**
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070033 * @brief The name assignment function provided by the CA operator to generate available
34 * namecomponents.
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -070035 * The function does not guarantee that all the returned names are available. Therefore the
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070036 * CA should further check the availability of each returned name and remove unavailable results.
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080037 *
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070038 * @p vector, input, a list of parameter key-value pair used for name assignment.
39 * @return a vector containing the possible namespaces derived from the parameters.
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080040 */
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -070041// using ProbeHandler = function<std::string /*identity name*/ (const Block& tlv /*requester input*/)>;
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070042using NameAssignmentFunc = function<std::vector<std::string>(const std::vector<std::tuple<std::string, std::string>>)>;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080043
44/**
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070045 * @brief The function would be invoked whenever the certificate request status is updated.
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080046 * The callback is used to notice the CA application or CA command line tool. The callback is
47 * fired whenever a request instance is created, challenge status is updated, and when certificate
48 * is issued.
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070049 *
50 * @p CertificateRequest, input, the state of the certificate request whose status is updated.
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080051 */
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070052using StatusUpdateCallback = function<void(const CertificateRequest&)>;
Zhiyi Zhang7420cf52017-12-18 18:59:21 +080053
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080054/**
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070055 * @brief CA's configuration on NDNCERT.
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070056 * For CA configuration format, please refer to:
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070057 * https://github.com/named-data/ndncert/wiki/NDNCERT-Protocol-0.3#213-ca-profile
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070058 *
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070059 * The format of CA configuration in JSON
60 * {
61 * "ca-prefix": "",
62 * "ca-info": "",
63 * "max-validity-period": "",
64 * "probe-parameters":
65 * [
66 * {"probe-parameter-key": ""},
67 * {"probe-parameter-key": ""}
68 * ]
69 * "supported-challenges":
70 * [
71 * {"challenge": ""},
72 * {"challenge": ""}
73 * ]
74 * }
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080075 */
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070076class CaConfig {
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080077public:
78 /**
79 * @brief Error that can be thrown from CaConfig
80 */
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070081 class Error : public std::runtime_error {
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080082 public:
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080083 using std::runtime_error::runtime_error;
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080084 };
85
86public:
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080087 /**
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070088 * Load CA configuration from the file.
89 *
90 * @param fileName, the configuration file name.
91 * @throw CaConfig::Error when config file does not exist or the configuration
92 * in the file cannot be parsed correctly.
93 * @throw CaConfig::Error when the ca-prefix attribute in JSON text is empty.
94 * @throw CaConfig::Error when the challenge is not specified or is not supported.
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080095 */
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080096 void
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080097 load(const std::string& fileName);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080098
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070099 /**
100 * Set the NameAssignmentFunction.
101 */
Zhiyi Zhangf6c5d272020-09-28 10:17:32 -0700102 // void
103 // setNameAssignmentFunc(const NameAssignmentFunc& nameAssignmentFunc) {
104 // m_nameAssignmentFunc = nameAssignmentFunc;
105 // }
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700106
107 /**
108 * Set the StatusUpdateCallback.
109 */
110 void
111 setStatusUpdateCallback(const StatusUpdateCallback& statusUpdateCallback) {
112 m_statusUpdateCallback = statusUpdateCallback;
113 }
114
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -0800115private:
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800116 void
Zhiyi Zhangad6cf932017-10-26 16:19:15 -0700117 parse(const JsonSection& configJson);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800118
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700119 void
120 parseProbeParameters(const JsonSection& section);
121
122 void
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -0800123 parseChallengeList(const JsonSection& configSection);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800124
125public:
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700126 /**
127 * CA Name prefix (without /CA suffix).
128 */
129 Name m_caPrefix;
130 /**
131 * CA Information.
132 */
133 std::string m_caInfo;
134 /**
135 * A list of parameter-keys for PROBE.
136 */
137 std::list<std::string> m_probeParameterKeys;
138 /**
139 * Maximum allowed validity period of the certificate being requested.
140 * The value is in the unit of second.
141 */
142 time::seconds m_maxValidityPeriod;
143 /**
144 * A list of supported challenges.
145 */
146 std::list<std::string> m_supportedChallenges;
147 /**
148 * NameAssignmentFunc Callback function
149 */
150 NameAssignmentFunc m_nameAssignmentFunc;
151 /**
152 * StatusUpdate Callback function
153 */
154 StatusUpdateCallback m_statusUpdateCallback;
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800155};
156
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700157} // namespace ndncert
158} // namespace ndn
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800159
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700160#endif // NDNCERT_CA_CONFIG_HPP