blob: a7b3f9a65cbef8cc5e11ba9a16be7df0820e3295 [file] [log] [blame]
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -07003 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -08004 *
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 "ca-config.hpp"
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070022#include "challenge-module.hpp"
23#include <ndn-cxx/util/io.hpp>
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080024#include <boost/filesystem.hpp>
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070025#include <boost/property_tree/json_parser.hpp>
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070026#include <boost/property_tree/ptree.hpp>
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080027
28namespace ndn {
29namespace ndncert {
30
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080031void
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080032CaConfig::load(const std::string& fileName)
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080033{
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070034 JsonSection configJson;
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080035 try {
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070036 boost::property_tree::read_json(fileName, configJson);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080037 }
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080038 catch (const std::exception& error) {
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070039 BOOST_THROW_EXCEPTION(std::runtime_error("Failed to parse configuration file " + fileName + ", " + error.what()));
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080040 }
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070041 if (configJson.begin() == configJson.end()) {
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070042 BOOST_THROW_EXCEPTION(std::runtime_error("std::runtime_error processing configuration file: " + fileName + " no data"));
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080043 }
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070044 parse(configJson);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080045}
46
47void
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070048CaConfig::parse(const JsonSection& configJson)
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080049{
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070050 // CA prefix
51 m_caPrefix = Name(configJson.get(CONFIG_CA_PREFIX, ""));
52 if (m_caPrefix.empty()) {
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070053 BOOST_THROW_EXCEPTION(std::runtime_error("Cannot parse ca-prefix from the config file"));
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080054 }
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070055 // CA info
56 m_caInfo = configJson.get(CONFIG_CA_INFO, "");
57 // CA max validity period
58 m_maxValidityPeriod = time::seconds(configJson.get(CONFIG_MAX_VALIDITY_PERIOD, 3600));
Zhiyi Zhang48f23782020-09-28 12:11:24 -070059 // CA max suffix length
tylerliu0b6d0db2020-09-28 17:52:02 -070060 m_maxSuffixLength = configJson.get<size_t>(CONFIG_MAX_SUFFIX_LENGTH, 1);
61 if (m_maxSuffixLength == 0) {
62 m_maxSuffixLength = 1;
63 }
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070064 // probe
65 m_probeParameterKeys.clear();
66 auto probeParameters = configJson.get_child_optional(CONFIG_PROBE_PARAMETERS);
67 if (probeParameters) {
68 parseProbeParameters(*probeParameters);
69 }
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080070 // optional supported challenges
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070071 m_supportedChallenges.clear();
72 auto challengeList = configJson.get_child_optional(CONFIG_SUPPORTED_CHALLENGES);
73 if (!challengeList) {
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070074 BOOST_THROW_EXCEPTION(std::runtime_error("Cannot parse challenge list"));
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070075 }
76 parseChallengeList(*challengeList);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080077}
78
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070079void
80CaConfig::parseProbeParameters(const JsonSection& section)
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080081{
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070082 for (const auto item : section) {
83 auto probeParameter = item.second.get(CONFIG_PROBE_PARAMETER, "");
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070084 probeParameter = boost::algorithm::to_lower_copy(probeParameter);
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070085 if (probeParameter == "") {
86 BOOST_THROW_EXCEPTION(std::runtime_error("Cannot read probe-parameter-key in probe-parameters from the config file"));
87 }
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070088 m_probeParameterKeys.push_back(probeParameter);
89 }
90}
91
92void
93CaConfig::parseChallengeList(const JsonSection& section)
94{
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070095 for (const auto item : section) {
96 auto challengeType = item.second.get(CONFIG_CHALLENGE, "");
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080097 challengeType = boost::algorithm::to_lower_copy(challengeType);
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070098 if (challengeType == "") {
99 BOOST_THROW_EXCEPTION(std::runtime_error("Cannot read type in supported-challenges from the config file"));
100 }
Zhiyi Zhang46049832020-09-28 17:08:12 -0700101 if (!ChallengeModule::isChallengeSupported(challengeType)) {
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -0700102 BOOST_THROW_EXCEPTION(std::runtime_error("Does not support challenge read from the config file"));
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800103 }
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700104 m_supportedChallenges.push_back(challengeType);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800105 }
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700106 if (m_supportedChallenges.size() == 0) {
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -0700107 BOOST_THROW_EXCEPTION(std::runtime_error("At least one challenge should be identified under supported-challenges"));
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700108 }
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800109}
110
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700111} // namespace ndncert
112} // namespace ndn