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