blob: c615e494a22109c7cb3e9d3b7a2e4d27fe78db2b [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 Zhang8da54d62019-11-21 00:03:05 -080022#include "challenge-module.hpp"
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070023
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080024#include <boost/filesystem.hpp>
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070025#include <ndn-cxx/util/io.hpp>
26#include <boost/property_tree/ptree.hpp>
27#include <boost/property_tree/json_parser.hpp>
28#include <boost/property_tree/info_parser.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) {
41 BOOST_THROW_EXCEPTION(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 Zhang06d6ae92017-03-08 14:59:45 -080044 BOOST_THROW_EXCEPTION(Error("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()) {
55 BOOST_THROW_EXCEPTION(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) {
71 BOOST_THROW_EXCEPTION(Error("Cannot parse challenge list"));
72 }
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 Zhang06d6ae92017-03-08 14:59:45 -080079 auto it = section.begin();
80 for (; it != section.end(); it++) {
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070081 auto probeParameter = it->second.get(CONFIG_PROBE_PARAMETER, "");
82 if (probeParameter == "") {
83 BOOST_THROW_EXCEPTION(Error("Cannot read probe-parameter-key in probe-parameters from the config file"));
84 }
85 probeParameter = boost::algorithm::to_lower_copy(probeParameter);
86 m_probeParameterKeys.push_back(probeParameter);
87 }
88}
89
90void
91CaConfig::parseChallengeList(const JsonSection& section)
92{
93 auto it = section.begin();
94 for (; it != section.end(); it++) {
95 auto challengeType = it->second.get(CONFIG_CHALLENGE, "");
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080096 if (challengeType == "") {
97 BOOST_THROW_EXCEPTION(Error("Cannot read type in supported-challenges from the config file"));
98 }
99 challengeType = boost::algorithm::to_lower_copy(challengeType);
100 if (!ChallengeModule::supportChallenge(challengeType)) {
101 BOOST_THROW_EXCEPTION(Error("Does not support challenge read from the config file"));
102 }
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700103 m_supportedChallenges.push_back(challengeType);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800104 }
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700105 if (m_supportedChallenges.size() == 0) {
106 BOOST_THROW_EXCEPTION(Error("At least one challenge should be identified under supported-challenges"));
107 }
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800108}
109
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700110} // namespace ndncert
111} // namespace ndn