blob: c4f2c72f6580a7aebf28d3281cde198d7429f169 [file] [log] [blame]
Zhiyi Zhang9829da92020-09-30 16:19:34 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2017-2020, Regents of the University of California.
4 *
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#include "configuration.hpp"
22#include "challenge-module.hpp"
23#include <ndn-cxx/util/io.hpp>
24#include <boost/filesystem.hpp>
25
26namespace ndn {
27namespace ndncert {
28
29void
30CaConfigItem::parse(const JsonSection& configJson)
31{
32 // CA prefix
33 m_caPrefix = Name(configJson.get(CONFIG_CA_PREFIX, ""));
34 if (m_caPrefix.empty()) {
35 BOOST_THROW_EXCEPTION(std::runtime_error("Cannot parse ca-prefix from the config file"));
36 }
37 // CA info
38 m_caInfo = configJson.get(CONFIG_CA_INFO, "");
39 // CA max validity period
40 m_maxValidityPeriod = time::seconds(configJson.get(CONFIG_MAX_VALIDITY_PERIOD, 86400));
41 // CA max suffix length
42 m_maxSuffixLength = configJson.get_optional<size_t>(CONFIG_MAX_SUFFIX_LENGTH);
43 // probe parameter keys
44 m_probeParameterKeys.clear();
45 auto probeParametersJson = configJson.get_child_optional(CONFIG_PROBE_PARAMETERS);
46 if (probeParametersJson) {
47 for (const auto item : *probeParametersJson) {
48 auto probeParameter = item.second.get(CONFIG_PROBE_PARAMETER, "");
49 probeParameter = boost::algorithm::to_lower_copy(probeParameter);
50 if (probeParameter == "") {
51 BOOST_THROW_EXCEPTION(std::runtime_error("Probe parameter key cannot be empty."));
52 }
53 m_probeParameterKeys.push_back(probeParameter);
54 }
55 }
56 // supported challenges
57 m_supportedChallenges.clear();
58 auto challengeListJson = configJson.get_child_optional(CONFIG_SUPPORTED_CHALLENGES);
59 if (challengeListJson) {
60 for (const auto item : *challengeListJson) {
61 auto challengeType = item.second.get(CONFIG_CHALLENGE, "");
62 challengeType = boost::algorithm::to_lower_copy(challengeType);
63 if (challengeType == "") {
64 BOOST_THROW_EXCEPTION(std::runtime_error("Challenge type canont be empty."));
65 }
66 if (!ChallengeModule::isChallengeSupported(challengeType)) {
67 BOOST_THROW_EXCEPTION(std::runtime_error("Challenge " + challengeType + " is not supported."));
68 }
69 m_supportedChallenges.push_back(challengeType);
70 }
71 }
72 // anchor certificate
73 m_cert = nullptr;
74 auto certificateStr = configJson.get("certificate", "");
75 if (certificateStr != "") {
76 std::istringstream ss(certificateStr);
77 m_cert = io::load<security::v2::Certificate>(ss);
78 }
79}
80
81JsonSection
82CaConfigItem::toJson() const
83{
84 JsonSection caItem;
85 caItem.put(CONFIG_CA_PREFIX, m_caPrefix.toUri());
86 caItem.put(CONFIG_CA_INFO, m_caInfo);
87 caItem.put(CONFIG_MAX_VALIDITY_PERIOD, m_maxValidityPeriod.count());
88 caItem.put(CONFIG_MAX_SUFFIX_LENGTH, m_maxSuffixLength);
89 if (!m_probeParameterKeys.empty()) {
90 JsonSection probeParametersJson;
91 for (const auto& key : m_probeParameterKeys) {
92 JsonSection keyJson;
93 keyJson.put(CONFIG_PROBE_PARAMETER, key);
94 probeParametersJson.push_back(std::make_pair("", keyJson));
95 }
96 caItem.add_child("", probeParametersJson);
97 }
98 if (!m_supportedChallenges.empty()) {
99 JsonSection challengeListJson;
100 for (const auto& challenge : m_supportedChallenges) {
101 JsonSection challengeJson;
102 challengeJson.put(CONFIG_CHALLENGE, challenge);
103 challengeListJson.push_back(std::make_pair("", challengeJson));
104 }
105 caItem.add_child("", challengeListJson);
106 }
107 if (m_cert != nullptr) {
108 std::stringstream ss;
109 io::save(*m_cert, ss);
110 caItem.put("certificate", ss.str());
111 }
112 return caItem;
113}
114
115void
116CaConfig::load(const std::string& fileName)
117{
118 JsonSection configJson;
119 try {
120 boost::property_tree::read_json(fileName, configJson);
121 }
122 catch (const std::exception& error) {
123 BOOST_THROW_EXCEPTION(std::runtime_error("Failed to parse configuration file " + fileName + ", " + error.what()));
124 }
125 if (configJson.begin() == configJson.end()) {
126 BOOST_THROW_EXCEPTION(std::runtime_error("No JSON configuration found in file: " + fileName));
127 }
128 m_caItem.parse(configJson);
129 if (m_caItem.m_supportedChallenges.size() == 0) {
130 BOOST_THROW_EXCEPTION(std::runtime_error("At least one challenge should be specified."));
131 }
132}
133
134void
135CaConfig::save(const std::string& fileName) const
136{
137 std::stringstream ss;
138 boost::property_tree::write_json(ss, m_caItem.toJson());
139 std::ofstream configFile;
140 configFile.open(fileName);
141 configFile << ss.str();
142 configFile.close();
143}
144
145void
146ClientConfig::load(const std::string& fileName)
147{
148 JsonSection configJson;
149 try {
150 boost::property_tree::read_json(fileName, configJson);
151 }
152 catch (const std::exception& error) {
153 BOOST_THROW_EXCEPTION(std::runtime_error("Failed to parse configuration file " + fileName + ", " + error.what()));
154 }
155 if (configJson.begin() == configJson.end()) {
156 BOOST_THROW_EXCEPTION(std::runtime_error("No JSON configuration found in file: " + fileName));
157 }
158 load(configJson);
159}
160
161void
162ClientConfig::load(const JsonSection& configSection)
163{
164 m_caItems.clear();
165 auto caList = configSection.get_child("ca-list");
166 for (auto item : caList) {
167 CaConfigItem caItem;
168 caItem.parse(item.second);
169 if (caItem.m_cert == nullptr) {
170 BOOST_THROW_EXCEPTION(std::runtime_error("No CA certificate is loaded from JSON configuration."));
171 }
172 m_caItems.push_back(std::move(caItem));
173 }
174}
175
176void
177ClientConfig::save(const std::string& fileName) const
178{
179 JsonSection configJson;
180 for (const auto& caItem : m_caItems) {
181 configJson.push_back(std::make_pair("", caItem.toJson()));
182 }
183 std::stringstream ss;
184 boost::property_tree::write_json(ss, configJson);
185 std::ofstream configFile;
186 configFile.open(fileName);
187 configFile << ss.str();
188 configFile.close();
189}
190
191void
192ClientConfig::removeCaItem(const Name& caName)
193{
194 m_caItems.remove_if([&](const CaConfigItem& item) { return item.m_caPrefix == caName; });
195}
196
197} // namespace ndncert
198} // namespace ndn