Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [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 "detail/ca-profile.hpp" |
| 22 | #include "identity-challenge/challenge-module.hpp" |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 23 | #include <ndn-cxx/util/io.hpp> |
| 24 | #include <boost/filesystem.hpp> |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace ndncert { |
| 28 | |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame^] | 29 | CaProfile |
| 30 | CaProfile::fromJson(const JsonSection& json) |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 31 | { |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame^] | 32 | CaProfile profile; |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 33 | // CA prefix |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame^] | 34 | profile.m_caPrefix = Name(json.get(CONFIG_CA_PREFIX, "")); |
| 35 | if (profile.m_caPrefix.empty()) { |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 36 | NDN_THROW(std::runtime_error("Cannot parse ca-prefix from the config file")); |
| 37 | } |
| 38 | // CA info |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame^] | 39 | profile.m_caInfo = json.get(CONFIG_CA_INFO, ""); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 40 | // CA max validity period |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame^] | 41 | profile.m_maxValidityPeriod = time::seconds(json.get(CONFIG_MAX_VALIDITY_PERIOD, 86400)); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 42 | // CA max suffix length |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame^] | 43 | profile.m_maxSuffixLength = nullopt; |
| 44 | auto maxSuffixLength = json.get_optional<size_t>(CONFIG_MAX_SUFFIX_LENGTH); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 45 | if (maxSuffixLength) { |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame^] | 46 | profile.m_maxSuffixLength = *maxSuffixLength; |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 47 | } |
| 48 | // probe parameter keys |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame^] | 49 | profile.m_probeParameterKeys.clear(); |
| 50 | auto probeParametersJson = json.get_child_optional(CONFIG_PROBE_PARAMETERS); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 51 | if (probeParametersJson) { |
| 52 | for (const auto& item : *probeParametersJson) { |
| 53 | auto probeParameter = item.second.get(CONFIG_PROBE_PARAMETER, ""); |
| 54 | probeParameter = boost::algorithm::to_lower_copy(probeParameter); |
| 55 | if (probeParameter == "") { |
| 56 | NDN_THROW(std::runtime_error("Probe parameter key cannot be empty.")); |
| 57 | } |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame^] | 58 | profile.m_probeParameterKeys.push_back(probeParameter); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | // supported challenges |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame^] | 62 | profile.m_supportedChallenges.clear(); |
| 63 | auto challengeListJson = json.get_child_optional(CONFIG_SUPPORTED_CHALLENGES); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 64 | if (challengeListJson) { |
| 65 | for (const auto& item : *challengeListJson) { |
| 66 | auto challengeType = item.second.get(CONFIG_CHALLENGE, ""); |
| 67 | challengeType = boost::algorithm::to_lower_copy(challengeType); |
| 68 | if (challengeType == "") { |
| 69 | NDN_THROW(std::runtime_error("Challenge type canont be empty.")); |
| 70 | } |
| 71 | if (!ChallengeModule::isChallengeSupported(challengeType)) { |
| 72 | NDN_THROW(std::runtime_error("Challenge " + challengeType + " is not supported.")); |
| 73 | } |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame^] | 74 | profile.m_supportedChallenges.push_back(challengeType); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | // anchor certificate |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame^] | 78 | profile.m_cert = nullptr; |
| 79 | auto certificateStr = json.get(CONFIG_CERTIFICATE, ""); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 80 | if (certificateStr != "") { |
| 81 | std::istringstream ss(certificateStr); |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame^] | 82 | profile.m_cert = io::load<security::Certificate>(ss); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 83 | } |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame^] | 84 | return profile; |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | JsonSection |
| 88 | CaProfile::toJson() const |
| 89 | { |
| 90 | JsonSection caItem; |
| 91 | caItem.put(CONFIG_CA_PREFIX, m_caPrefix.toUri()); |
| 92 | caItem.put(CONFIG_CA_INFO, m_caInfo); |
| 93 | caItem.put(CONFIG_MAX_VALIDITY_PERIOD, m_maxValidityPeriod.count()); |
| 94 | if (m_maxSuffixLength) { |
| 95 | caItem.put(CONFIG_MAX_SUFFIX_LENGTH, *m_maxSuffixLength); |
| 96 | } |
| 97 | if (!m_probeParameterKeys.empty()) { |
| 98 | JsonSection probeParametersJson; |
| 99 | for (const auto& key : m_probeParameterKeys) { |
| 100 | JsonSection keyJson; |
| 101 | keyJson.put(CONFIG_PROBE_PARAMETER, key); |
| 102 | probeParametersJson.push_back(std::make_pair("", keyJson)); |
| 103 | } |
| 104 | caItem.add_child("", probeParametersJson); |
| 105 | } |
| 106 | if (!m_supportedChallenges.empty()) { |
| 107 | JsonSection challengeListJson; |
| 108 | for (const auto& challenge : m_supportedChallenges) { |
| 109 | JsonSection challengeJson; |
| 110 | challengeJson.put(CONFIG_CHALLENGE, challenge); |
| 111 | challengeListJson.push_back(std::make_pair("", challengeJson)); |
| 112 | } |
| 113 | caItem.add_child("", challengeListJson); |
| 114 | } |
| 115 | if (m_cert != nullptr) { |
| 116 | std::stringstream ss; |
| 117 | io::save(*m_cert, ss); |
| 118 | caItem.put("certificate", ss.str()); |
| 119 | } |
| 120 | return caItem; |
| 121 | } |
| 122 | |
| 123 | } // namespace ndncert |
| 124 | } // namespace ndn |