blob: bd826e916909e32441ea793a4d5059fea06f75a2 [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"
22#include "identity-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 Zhang1e418f22020-11-19 19:49:32 -080034 profile.m_caPrefix = Name(json.get(CONFIG_CA_PREFIX, ""));
35 if (profile.m_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 Zhang1e418f22020-11-19 19:49:32 -080039 profile.m_caInfo = json.get(CONFIG_CA_INFO, "");
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080040 // CA max validity period
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080041 profile.m_maxValidityPeriod = time::seconds(json.get(CONFIG_MAX_VALIDITY_PERIOD, 86400));
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080042 // CA max suffix length
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080043 profile.m_maxSuffixLength = nullopt;
44 auto maxSuffixLength = json.get_optional<size_t>(CONFIG_MAX_SUFFIX_LENGTH);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080045 if (maxSuffixLength) {
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080046 profile.m_maxSuffixLength = *maxSuffixLength;
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080047 }
48 // probe parameter keys
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080049 profile.m_probeParameterKeys.clear();
50 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 Zhang1e418f22020-11-19 19:49:32 -080058 profile.m_probeParameterKeys.push_back(probeParameter);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080059 }
60 }
61 // supported challenges
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080062 profile.m_supportedChallenges.clear();
63 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 Zhang1e418f22020-11-19 19:49:32 -080074 profile.m_supportedChallenges.push_back(challengeType);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080075 }
76 }
77 // anchor certificate
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080078 profile.m_cert = nullptr;
79 auto certificateStr = json.get(CONFIG_CERTIFICATE, "");
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080080 if (certificateStr != "") {
81 std::istringstream ss(certificateStr);
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080082 profile.m_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;
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