Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 1 | /* -*- 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" |
Zhiyi Zhang | dbd9d43 | 2020-10-07 15:56:27 -0700 | [diff] [blame] | 22 | #include "identity-challenge/challenge-module.hpp" |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 23 | #include <ndn-cxx/util/io.hpp> |
| 24 | #include <boost/filesystem.hpp> |
Zhiyi Zhang | c2c4ed2 | 2020-10-14 17:16:28 -0700 | [diff] [blame] | 25 | #include "name-assignment/assignment-func.hpp" |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace ndncert { |
| 29 | |
Zhiyi Zhang | 5981223 | 2020-10-12 13:11:35 -0700 | [diff] [blame] | 30 | // Parse CA Configuration file |
| 31 | const std::string CONFIG_CA_PREFIX = "ca-prefix"; |
| 32 | const std::string CONFIG_CA_INFO = "ca-info"; |
| 33 | const std::string CONFIG_MAX_VALIDITY_PERIOD = "max-validity-period"; |
| 34 | const std::string CONFIG_MAX_SUFFIX_LENGTH = "max-suffix-length"; |
| 35 | const std::string CONFIG_PROBE_PARAMETERS = "probe-parameters"; |
| 36 | const std::string CONFIG_PROBE_PARAMETER = "probe-parameter-key"; |
| 37 | const std::string CONFIG_SUPPORTED_CHALLENGES = "supported-challenges"; |
| 38 | const std::string CONFIG_CHALLENGE = "challenge"; |
| 39 | const std::string CONFIG_CERTIFICATE = "certificate"; |
| 40 | const std::string CONFIG_REDIRECTION = "redirect-to"; |
| 41 | const std::string CONFIG_NAME_ASSIGNMENT = "name-assignment"; |
| 42 | |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 43 | void |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 44 | CaProfile::parse(const JsonSection& configJson) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 45 | { |
| 46 | // CA prefix |
| 47 | m_caPrefix = Name(configJson.get(CONFIG_CA_PREFIX, "")); |
| 48 | if (m_caPrefix.empty()) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 49 | NDN_THROW(std::runtime_error("Cannot parse ca-prefix from the config file")); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 50 | } |
| 51 | // CA info |
| 52 | m_caInfo = configJson.get(CONFIG_CA_INFO, ""); |
| 53 | // CA max validity period |
| 54 | m_maxValidityPeriod = time::seconds(configJson.get(CONFIG_MAX_VALIDITY_PERIOD, 86400)); |
| 55 | // CA max suffix length |
Zhiyi Zhang | 997669a | 2020-10-28 21:15:40 -0700 | [diff] [blame] | 56 | m_maxSuffixLength = nullopt; |
| 57 | auto maxSuffixLength = configJson.get_optional<size_t>(CONFIG_MAX_SUFFIX_LENGTH); |
| 58 | if (maxSuffixLength.has_value()) { |
| 59 | m_maxSuffixLength = *maxSuffixLength; |
| 60 | } |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 61 | // probe parameter keys |
| 62 | m_probeParameterKeys.clear(); |
| 63 | auto probeParametersJson = configJson.get_child_optional(CONFIG_PROBE_PARAMETERS); |
| 64 | if (probeParametersJson) { |
tylerliu | a2d048b | 2020-10-09 16:10:16 -0700 | [diff] [blame] | 65 | for (const auto& item : *probeParametersJson) { |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 66 | auto probeParameter = item.second.get(CONFIG_PROBE_PARAMETER, ""); |
| 67 | probeParameter = boost::algorithm::to_lower_copy(probeParameter); |
| 68 | if (probeParameter == "") { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 69 | NDN_THROW(std::runtime_error("Probe parameter key cannot be empty.")); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 70 | } |
| 71 | m_probeParameterKeys.push_back(probeParameter); |
| 72 | } |
| 73 | } |
| 74 | // supported challenges |
| 75 | m_supportedChallenges.clear(); |
| 76 | auto challengeListJson = configJson.get_child_optional(CONFIG_SUPPORTED_CHALLENGES); |
| 77 | if (challengeListJson) { |
tylerliu | a2d048b | 2020-10-09 16:10:16 -0700 | [diff] [blame] | 78 | for (const auto& item : *challengeListJson) { |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 79 | auto challengeType = item.second.get(CONFIG_CHALLENGE, ""); |
| 80 | challengeType = boost::algorithm::to_lower_copy(challengeType); |
| 81 | if (challengeType == "") { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 82 | NDN_THROW(std::runtime_error("Challenge type canont be empty.")); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 83 | } |
| 84 | if (!ChallengeModule::isChallengeSupported(challengeType)) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 85 | NDN_THROW(std::runtime_error("Challenge " + challengeType + " is not supported.")); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 86 | } |
| 87 | m_supportedChallenges.push_back(challengeType); |
| 88 | } |
| 89 | } |
| 90 | // anchor certificate |
| 91 | m_cert = nullptr; |
Zhiyi Zhang | fde5011 | 2020-10-01 16:36:33 -0700 | [diff] [blame] | 92 | auto certificateStr = configJson.get(CONFIG_CERTIFICATE, ""); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 93 | if (certificateStr != "") { |
| 94 | std::istringstream ss(certificateStr); |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 95 | m_cert = io::load<security::Certificate>(ss); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | JsonSection |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 100 | CaProfile::toJson() const |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 101 | { |
| 102 | JsonSection caItem; |
| 103 | caItem.put(CONFIG_CA_PREFIX, m_caPrefix.toUri()); |
| 104 | caItem.put(CONFIG_CA_INFO, m_caInfo); |
| 105 | caItem.put(CONFIG_MAX_VALIDITY_PERIOD, m_maxValidityPeriod.count()); |
Zhiyi Zhang | 997669a | 2020-10-28 21:15:40 -0700 | [diff] [blame] | 106 | if (m_maxSuffixLength) { |
| 107 | caItem.put(CONFIG_MAX_SUFFIX_LENGTH, *m_maxSuffixLength); |
| 108 | } |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 109 | if (!m_probeParameterKeys.empty()) { |
| 110 | JsonSection probeParametersJson; |
| 111 | for (const auto& key : m_probeParameterKeys) { |
| 112 | JsonSection keyJson; |
| 113 | keyJson.put(CONFIG_PROBE_PARAMETER, key); |
| 114 | probeParametersJson.push_back(std::make_pair("", keyJson)); |
| 115 | } |
| 116 | caItem.add_child("", probeParametersJson); |
| 117 | } |
| 118 | if (!m_supportedChallenges.empty()) { |
| 119 | JsonSection challengeListJson; |
| 120 | for (const auto& challenge : m_supportedChallenges) { |
| 121 | JsonSection challengeJson; |
| 122 | challengeJson.put(CONFIG_CHALLENGE, challenge); |
| 123 | challengeListJson.push_back(std::make_pair("", challengeJson)); |
| 124 | } |
| 125 | caItem.add_child("", challengeListJson); |
| 126 | } |
| 127 | if (m_cert != nullptr) { |
| 128 | std::stringstream ss; |
| 129 | io::save(*m_cert, ss); |
| 130 | caItem.put("certificate", ss.str()); |
| 131 | } |
| 132 | return caItem; |
| 133 | } |
| 134 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 135 | namespace ca { |
| 136 | |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 137 | void |
| 138 | CaConfig::load(const std::string& fileName) |
| 139 | { |
| 140 | JsonSection configJson; |
| 141 | try { |
| 142 | boost::property_tree::read_json(fileName, configJson); |
| 143 | } |
| 144 | catch (const std::exception& error) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 145 | NDN_THROW(std::runtime_error("Failed to parse configuration file " + fileName + ", " + error.what())); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 146 | } |
| 147 | if (configJson.begin() == configJson.end()) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 148 | NDN_THROW(std::runtime_error("No JSON configuration found in file: " + fileName)); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 149 | } |
| 150 | m_caItem.parse(configJson); |
| 151 | if (m_caItem.m_supportedChallenges.size() == 0) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 152 | NDN_THROW(std::runtime_error("At least one challenge should be specified.")); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 153 | } |
Zhiyi Zhang | fde5011 | 2020-10-01 16:36:33 -0700 | [diff] [blame] | 154 | // parse redirection section if appears |
Zhiyi Zhang | 997669a | 2020-10-28 21:15:40 -0700 | [diff] [blame] | 155 | m_redirection = nullopt; |
Zhiyi Zhang | fde5011 | 2020-10-01 16:36:33 -0700 | [diff] [blame] | 156 | auto redirectionItems = configJson.get_child_optional(CONFIG_REDIRECTION); |
| 157 | if (redirectionItems) { |
tylerliu | a2d048b | 2020-10-09 16:10:16 -0700 | [diff] [blame] | 158 | for (const auto& item : *redirectionItems) { |
Zhiyi Zhang | fde5011 | 2020-10-01 16:36:33 -0700 | [diff] [blame] | 159 | auto caPrefixStr = item.second.get(CONFIG_CA_PREFIX, ""); |
| 160 | auto caCertStr = item.second.get(CONFIG_CERTIFICATE, ""); |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 161 | if (caCertStr == "") { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 162 | NDN_THROW(std::runtime_error("Redirect-to item's ca-prefix or certificate cannot be empty.")); |
Zhiyi Zhang | fde5011 | 2020-10-01 16:36:33 -0700 | [diff] [blame] | 163 | } |
| 164 | std::istringstream ss(caCertStr); |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 165 | auto caCert = io::load<security::Certificate>(ss); |
Zhiyi Zhang | fde5011 | 2020-10-01 16:36:33 -0700 | [diff] [blame] | 166 | if (!m_redirection) { |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 167 | m_redirection = std::vector<std::shared_ptr<security::Certificate>>(); |
Zhiyi Zhang | fde5011 | 2020-10-01 16:36:33 -0700 | [diff] [blame] | 168 | } |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 169 | m_redirection->push_back(caCert); |
Zhiyi Zhang | fde5011 | 2020-10-01 16:36:33 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
tylerliu | 8d9e712 | 2020-10-06 18:57:06 -0700 | [diff] [blame] | 172 | //parse name assignment if appears |
Zhiyi Zhang | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 173 | m_nameAssignmentFuncs.clear(); |
tylerliu | 8d9e712 | 2020-10-06 18:57:06 -0700 | [diff] [blame] | 174 | auto nameAssignmentItems = configJson.get_child_optional(CONFIG_NAME_ASSIGNMENT); |
| 175 | if (nameAssignmentItems) { |
tylerliu | a2d048b | 2020-10-09 16:10:16 -0700 | [diff] [blame] | 176 | for (const auto& item : *nameAssignmentItems) { |
Zhiyi Zhang | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 177 | auto func = NameAssignmentFunc::createNameAssignmentFunc(item.first, item.second.data()); |
Zhiyi Zhang | 1706402 | 2020-10-07 18:34:44 -0700 | [diff] [blame] | 178 | if (func == nullptr) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 179 | NDN_THROW(std::runtime_error("Error on creating name assignment function")); |
Zhiyi Zhang | 8683ec9 | 2020-10-07 18:18:35 -0700 | [diff] [blame] | 180 | } |
Zhiyi Zhang | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 181 | m_nameAssignmentFuncs.push_back(std::move(func)); |
tylerliu | 8d9e712 | 2020-10-06 18:57:06 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 186 | } // namespace ca |
| 187 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 188 | namespace requester { |
| 189 | |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 190 | void |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 191 | ProfileStorage::load(const std::string& fileName) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 192 | { |
| 193 | JsonSection configJson; |
| 194 | try { |
| 195 | boost::property_tree::read_json(fileName, configJson); |
| 196 | } |
| 197 | catch (const std::exception& error) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 198 | NDN_THROW(std::runtime_error("Failed to parse configuration file " + fileName + ", " + error.what())); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 199 | } |
| 200 | if (configJson.begin() == configJson.end()) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 201 | NDN_THROW(std::runtime_error("No JSON configuration found in file: " + fileName)); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 202 | } |
| 203 | load(configJson); |
| 204 | } |
| 205 | |
| 206 | void |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 207 | ProfileStorage::load(const JsonSection& configSection) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 208 | { |
| 209 | m_caItems.clear(); |
| 210 | auto caList = configSection.get_child("ca-list"); |
| 211 | for (auto item : caList) { |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 212 | CaProfile caItem; |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 213 | caItem.parse(item.second); |
| 214 | if (caItem.m_cert == nullptr) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 215 | NDN_THROW(std::runtime_error("No CA certificate is loaded from JSON configuration.")); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 216 | } |
| 217 | m_caItems.push_back(std::move(caItem)); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 222 | ProfileStorage::save(const std::string& fileName) const |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 223 | { |
| 224 | JsonSection configJson; |
| 225 | for (const auto& caItem : m_caItems) { |
| 226 | configJson.push_back(std::make_pair("", caItem.toJson())); |
| 227 | } |
| 228 | std::stringstream ss; |
| 229 | boost::property_tree::write_json(ss, configJson); |
| 230 | std::ofstream configFile; |
| 231 | configFile.open(fileName); |
| 232 | configFile << ss.str(); |
| 233 | configFile.close(); |
| 234 | } |
| 235 | |
| 236 | void |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 237 | ProfileStorage::removeCaProfile(const Name& caName) |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 238 | { |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 239 | m_caItems.remove_if([&](const CaProfile& item) { return item.m_caPrefix == caName; }); |
| 240 | } |
| 241 | |
| 242 | void |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 243 | ProfileStorage::addCaProfile(const CaProfile& profile) |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 244 | { |
| 245 | for (auto& item : m_caItems) { |
| 246 | if (item.m_caPrefix == profile.m_caPrefix) { |
| 247 | item = profile; |
| 248 | return; |
| 249 | } |
| 250 | } |
| 251 | m_caItems.push_back(profile); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 254 | } // namespace requester |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 255 | } // namespace ndncert |
| 256 | } // namespace ndn |