blob: 1bd8e04c203d0125fc8ad9be348660be6e036cad [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 Zhang0453dbb2020-04-28 22:39:17 -070022
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080023#include <boost/filesystem.hpp>
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070024#include <boost/property_tree/json_parser.hpp>
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070025#include <boost/property_tree/ptree.hpp>
26#include <ndn-cxx/util/io.hpp>
27
28#include "challenge-module.hpp"
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080029
30namespace ndn {
31namespace ndncert {
32
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080033void
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080034CaConfig::load(const std::string& fileName)
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080035{
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070036 JsonSection configJson;
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080037 try {
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070038 boost::property_tree::read_json(fileName, configJson);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080039 }
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080040 catch (const std::exception& error) {
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070041 BOOST_THROW_EXCEPTION(std::runtime_error("Failed to parse configuration file " + fileName + ", " + error.what()));
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080042 }
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070043 if (configJson.begin() == configJson.end()) {
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070044 BOOST_THROW_EXCEPTION(std::runtime_error("std::runtime_error processing configuration file: " + fileName + " no data"));
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080045 }
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070046 parse(configJson);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080047}
48
49void
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070050CaConfig::parse(const JsonSection& configJson)
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080051{
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070052 // CA prefix
53 m_caPrefix = Name(configJson.get(CONFIG_CA_PREFIX, ""));
54 if (m_caPrefix.empty()) {
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070055 BOOST_THROW_EXCEPTION(std::runtime_error("Cannot parse ca-prefix from the config file"));
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080056 }
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070057 // CA info
58 m_caInfo = configJson.get(CONFIG_CA_INFO, "");
59 // CA max validity period
60 m_maxValidityPeriod = time::seconds(configJson.get(CONFIG_MAX_VALIDITY_PERIOD, 3600));
61 // probe
62 m_probeParameterKeys.clear();
63 auto probeParameters = configJson.get_child_optional(CONFIG_PROBE_PARAMETERS);
64 if (probeParameters) {
65 parseProbeParameters(*probeParameters);
66 }
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080067 // optional supported challenges
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070068 m_supportedChallenges.clear();
69 auto challengeList = configJson.get_child_optional(CONFIG_SUPPORTED_CHALLENGES);
70 if (!challengeList) {
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070071 BOOST_THROW_EXCEPTION(std::runtime_error("Cannot parse challenge list"));
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070072 }
73 parseChallengeList(*challengeList);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080074}
75
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070076void
77CaConfig::parseProbeParameters(const JsonSection& section)
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080078{
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070079 for (const auto item : section) {
80 auto probeParameter = item.second.get(CONFIG_PROBE_PARAMETER, "");
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070081 probeParameter = boost::algorithm::to_lower_copy(probeParameter);
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070082 if (probeParameter == "") {
83 BOOST_THROW_EXCEPTION(std::runtime_error("Cannot read probe-parameter-key in probe-parameters from the config file"));
84 }
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070085 m_probeParameterKeys.push_back(probeParameter);
86 }
87}
88
89void
90CaConfig::parseChallengeList(const JsonSection& section)
91{
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070092 for (const auto item : section) {
93 auto challengeType = item.second.get(CONFIG_CHALLENGE, "");
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080094 challengeType = boost::algorithm::to_lower_copy(challengeType);
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070095 if (challengeType == "") {
96 BOOST_THROW_EXCEPTION(std::runtime_error("Cannot read type in supported-challenges from the config file"));
97 }
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080098 if (!ChallengeModule::supportChallenge(challengeType)) {
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -070099 BOOST_THROW_EXCEPTION(std::runtime_error("Does not support challenge read from the config file"));
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800100 }
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700101 m_supportedChallenges.push_back(challengeType);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800102 }
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700103 if (m_supportedChallenges.size() == 0) {
Zhiyi Zhangb2f8b942020-09-28 10:37:02 -0700104 BOOST_THROW_EXCEPTION(std::runtime_error("At least one challenge should be identified under supported-challenges"));
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700105 }
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800106}
107
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700108} // namespace ndncert
109} // namespace ndn