Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 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 "ca-config.hpp" |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 22 | #include "challenge-module.hpp" |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 23 | |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 24 | #include <boost/filesystem.hpp> |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 25 | #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 Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace ndncert { |
| 32 | |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 33 | void |
Zhiyi Zhang | 06d6ae9 | 2017-03-08 14:59:45 -0800 | [diff] [blame] | 34 | CaConfig::load(const std::string& fileName) |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 35 | { |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 36 | JsonSection configJson; |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 37 | try { |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 38 | boost::property_tree::read_json(fileName, configJson); |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 39 | } |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 40 | catch (const std::exception& error) { |
| 41 | BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + fileName + ", " + error.what())); |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 42 | } |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 43 | if (configJson.begin() == configJson.end()) { |
Zhiyi Zhang | 06d6ae9 | 2017-03-08 14:59:45 -0800 | [diff] [blame] | 44 | BOOST_THROW_EXCEPTION(Error("Error processing configuration file: " + fileName + " no data")); |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 45 | } |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 46 | parse(configJson); |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 50 | CaConfig::parse(const JsonSection& configJson) |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 51 | { |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 52 | // 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 Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 56 | } |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 57 | // 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 Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 67 | // optional supported challenges |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 68 | 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 Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 76 | void |
| 77 | CaConfig::parseProbeParameters(const JsonSection& section) |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 78 | { |
Zhiyi Zhang | 06d6ae9 | 2017-03-08 14:59:45 -0800 | [diff] [blame] | 79 | auto it = section.begin(); |
| 80 | for (; it != section.end(); it++) { |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 81 | 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 | |
| 90 | void |
| 91 | CaConfig::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 Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 96 | 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 Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 103 | m_supportedChallenges.push_back(challengeType); |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 104 | } |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 105 | if (m_supportedChallenges.size() == 0) { |
| 106 | BOOST_THROW_EXCEPTION(Error("At least one challenge should be identified under supported-challenges")); |
| 107 | } |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 110 | } // namespace ndncert |
| 111 | } // namespace ndn |