Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 842f1f7 | 2024-02-21 21:27:25 -0500 | [diff] [blame] | 3 | * Copyright (c) 2017-2024, Regents of the University of California. |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 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" |
Zhiyi Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 22 | #include "challenge/challenge-module.hpp" |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 23 | |
Davide Pesavento | 9510c91 | 2024-02-25 17:50:05 -0500 | [diff] [blame] | 24 | #include <boost/algorithm/string/case_conv.hpp> |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 25 | #include <ndn-cxx/util/io.hpp> |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 26 | |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 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 | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 34 | profile.caPrefix = Name(json.get(CONFIG_CA_PREFIX, "")); |
| 35 | if (profile.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 | } |
Tianyuan Yu | 42bc63e | 2024-10-18 10:18:38 -0700 | [diff] [blame] | 38 | // Forwarding hint |
| 39 | profile.forwardingHint = Name(json.get(CONFIG_FORWARDING_HINT, "")); |
| 40 | if (profile.forwardingHint.empty()) { |
| 41 | profile.forwardingHint = Name(profile.caPrefix).append("CA"); |
| 42 | } |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 43 | // CA info |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 44 | profile.caInfo = json.get(CONFIG_CA_INFO, ""); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 45 | // CA max validity period |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 46 | profile.maxValidityPeriod = time::seconds(json.get(CONFIG_MAX_VALIDITY_PERIOD, 86400)); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 47 | // CA max suffix length |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 48 | profile.maxSuffixLength = std::nullopt; |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame] | 49 | auto maxSuffixLength = json.get_optional<size_t>(CONFIG_MAX_SUFFIX_LENGTH); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 50 | if (maxSuffixLength) { |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 51 | profile.maxSuffixLength = *maxSuffixLength; |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 52 | } |
| 53 | // probe parameter keys |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 54 | profile.probeParameterKeys.clear(); |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame] | 55 | auto probeParametersJson = json.get_child_optional(CONFIG_PROBE_PARAMETERS); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 56 | if (probeParametersJson) { |
| 57 | for (const auto& item : *probeParametersJson) { |
| 58 | auto probeParameter = item.second.get(CONFIG_PROBE_PARAMETER, ""); |
| 59 | probeParameter = boost::algorithm::to_lower_copy(probeParameter); |
| 60 | if (probeParameter == "") { |
| 61 | NDN_THROW(std::runtime_error("Probe parameter key cannot be empty.")); |
| 62 | } |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 63 | profile.probeParameterKeys.push_back(probeParameter); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | // supported challenges |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 67 | profile.supportedChallenges.clear(); |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame] | 68 | auto challengeListJson = json.get_child_optional(CONFIG_SUPPORTED_CHALLENGES); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 69 | if (challengeListJson) { |
| 70 | for (const auto& item : *challengeListJson) { |
| 71 | auto challengeType = item.second.get(CONFIG_CHALLENGE, ""); |
| 72 | challengeType = boost::algorithm::to_lower_copy(challengeType); |
| 73 | if (challengeType == "") { |
tylerliu | f26d41b | 2021-10-12 16:50:16 -0700 | [diff] [blame] | 74 | NDN_THROW(std::runtime_error("Challenge type cannot be empty.")); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 75 | } |
| 76 | if (!ChallengeModule::isChallengeSupported(challengeType)) { |
| 77 | NDN_THROW(std::runtime_error("Challenge " + challengeType + " is not supported.")); |
| 78 | } |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 79 | profile.supportedChallenges.push_back(challengeType); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | // anchor certificate |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 83 | profile.cert = nullptr; |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame] | 84 | auto certificateStr = json.get(CONFIG_CERTIFICATE, ""); |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 85 | if (!certificateStr.empty()) { |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 86 | std::istringstream ss(certificateStr); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 87 | profile.cert = ndn::io::load<Certificate>(ss); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 88 | } |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame] | 89 | return profile; |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | JsonSection |
| 93 | CaProfile::toJson() const |
| 94 | { |
| 95 | JsonSection caItem; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 96 | caItem.put(CONFIG_CA_PREFIX, caPrefix.toUri()); |
| 97 | caItem.put(CONFIG_CA_INFO, caInfo); |
| 98 | caItem.put(CONFIG_MAX_VALIDITY_PERIOD, maxValidityPeriod.count()); |
| 99 | if (maxSuffixLength) { |
| 100 | caItem.put(CONFIG_MAX_SUFFIX_LENGTH, *maxSuffixLength); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 101 | } |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 102 | if (!probeParameterKeys.empty()) { |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 103 | JsonSection probeParametersJson; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 104 | for (const auto& key : probeParameterKeys) { |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 105 | JsonSection keyJson; |
| 106 | keyJson.put(CONFIG_PROBE_PARAMETER, key); |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 107 | probeParametersJson.push_back({"", keyJson}); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 108 | } |
| 109 | caItem.add_child("", probeParametersJson); |
| 110 | } |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 111 | if (!supportedChallenges.empty()) { |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 112 | JsonSection challengeListJson; |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 113 | for (const auto& challenge : supportedChallenges) { |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 114 | JsonSection challengeJson; |
| 115 | challengeJson.put(CONFIG_CHALLENGE, challenge); |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 116 | challengeListJson.push_back({"", challengeJson}); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 117 | } |
| 118 | caItem.add_child("", challengeListJson); |
| 119 | } |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 120 | if (cert != nullptr) { |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 121 | std::stringstream ss; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 122 | ndn::io::save(*cert, ss); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 123 | caItem.put("certificate", ss.str()); |
| 124 | } |
| 125 | return caItem; |
| 126 | } |
| 127 | |
| 128 | } // namespace ndncert |