blob: 7986830cc063eb8c94716de9f6c0b56a14cfe879 [file] [log] [blame]
Zhiyi Zhang3f20f952020-11-19 19:26:43 -08001/* -*- 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"
Zhiyi Zhang84e11842020-11-19 20:03:23 -080022#include "challenge/challenge-module.hpp"
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080023#include <ndn-cxx/util/io.hpp>
24#include <boost/filesystem.hpp>
25
26namespace ndn {
27namespace ndncert {
28
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080029CaProfile
30CaProfile::fromJson(const JsonSection& json)
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080031{
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080032 CaProfile profile;
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080033 // CA prefix
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080034 profile.caPrefix = Name(json.get(CONFIG_CA_PREFIX, ""));
35 if (profile.caPrefix.empty()) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080036 NDN_THROW(std::runtime_error("Cannot parse ca-prefix from the config file"));
37 }
38 // CA info
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080039 profile.caInfo = json.get(CONFIG_CA_INFO, "");
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080040 // CA max validity period
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080041 profile.maxValidityPeriod = time::seconds(json.get(CONFIG_MAX_VALIDITY_PERIOD, 86400));
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080042 // CA max suffix length
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080043 profile.maxSuffixLength = nullopt;
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080044 auto maxSuffixLength = json.get_optional<size_t>(CONFIG_MAX_SUFFIX_LENGTH);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080045 if (maxSuffixLength) {
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080046 profile.maxSuffixLength = *maxSuffixLength;
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080047 }
48 // probe parameter keys
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080049 profile.probeParameterKeys.clear();
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080050 auto probeParametersJson = json.get_child_optional(CONFIG_PROBE_PARAMETERS);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080051 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 Zhang44c6a352020-12-14 10:57:17 -080058 profile.probeParameterKeys.push_back(probeParameter);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080059 }
60 }
61 // supported challenges
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080062 profile.supportedChallenges.clear();
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080063 auto challengeListJson = json.get_child_optional(CONFIG_SUPPORTED_CHALLENGES);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080064 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 Zhang44c6a352020-12-14 10:57:17 -080074 profile.supportedChallenges.push_back(challengeType);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080075 }
76 }
77 // anchor certificate
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080078 profile.cert = nullptr;
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080079 auto certificateStr = json.get(CONFIG_CERTIFICATE, "");
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080080 if (certificateStr != "") {
81 std::istringstream ss(certificateStr);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080082 profile.cert = io::load<security::Certificate>(ss);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080083 }
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080084 return profile;
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080085}
86
87JsonSection
88CaProfile::toJson() const
89{
90 JsonSection caItem;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080091 caItem.put(CONFIG_CA_PREFIX, caPrefix.toUri());
92 caItem.put(CONFIG_CA_INFO, caInfo);
93 caItem.put(CONFIG_MAX_VALIDITY_PERIOD, maxValidityPeriod.count());
94 if (maxSuffixLength) {
95 caItem.put(CONFIG_MAX_SUFFIX_LENGTH, *maxSuffixLength);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080096 }
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080097 if (!probeParameterKeys.empty()) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080098 JsonSection probeParametersJson;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080099 for (const auto& key : probeParameterKeys) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -0800100 JsonSection keyJson;
101 keyJson.put(CONFIG_PROBE_PARAMETER, key);
102 probeParametersJson.push_back(std::make_pair("", keyJson));
103 }
104 caItem.add_child("", probeParametersJson);
105 }
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800106 if (!supportedChallenges.empty()) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -0800107 JsonSection challengeListJson;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800108 for (const auto& challenge : supportedChallenges) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -0800109 JsonSection challengeJson;
110 challengeJson.put(CONFIG_CHALLENGE, challenge);
111 challengeListJson.push_back(std::make_pair("", challengeJson));
112 }
113 caItem.add_child("", challengeListJson);
114 }
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800115 if (cert != nullptr) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -0800116 std::stringstream ss;
Zhiyi Zhang44c6a352020-12-14 10:57:17 -0800117 io::save(*cert, ss);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -0800118 caItem.put("certificate", ss.str());
119 }
120 return caItem;
121}
122
123} // namespace ndncert
124} // namespace ndn