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 | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 22 | |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 23 | #include <boost/filesystem.hpp> |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 24 | #include <boost/property_tree/json_parser.hpp> |
Zhiyi Zhang | b2f8b94 | 2020-09-28 10:37:02 -0700 | [diff] [blame] | 25 | #include <boost/property_tree/ptree.hpp> |
| 26 | #include <ndn-cxx/util/io.hpp> |
| 27 | |
| 28 | #include "challenge-module.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) { |
Zhiyi Zhang | b2f8b94 | 2020-09-28 10:37:02 -0700 | [diff] [blame] | 41 | BOOST_THROW_EXCEPTION(std::runtime_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 | b2f8b94 | 2020-09-28 10:37:02 -0700 | [diff] [blame] | 44 | BOOST_THROW_EXCEPTION(std::runtime_error("std::runtime_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()) { |
Zhiyi Zhang | b2f8b94 | 2020-09-28 10:37:02 -0700 | [diff] [blame] | 55 | BOOST_THROW_EXCEPTION(std::runtime_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)); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 61 | // CA max suffix length |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame] | 62 | m_maxSuffixLength = configJson.get<size_t>(CONFIG_MAX_SUFFIX_LENGTH, 1); |
| 63 | if (m_maxSuffixLength == 0) { |
| 64 | m_maxSuffixLength = 1; |
| 65 | } |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 66 | // probe |
| 67 | m_probeParameterKeys.clear(); |
| 68 | auto probeParameters = configJson.get_child_optional(CONFIG_PROBE_PARAMETERS); |
| 69 | if (probeParameters) { |
| 70 | parseProbeParameters(*probeParameters); |
| 71 | } |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 72 | // optional supported challenges |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 73 | m_supportedChallenges.clear(); |
| 74 | auto challengeList = configJson.get_child_optional(CONFIG_SUPPORTED_CHALLENGES); |
| 75 | if (!challengeList) { |
Zhiyi Zhang | b2f8b94 | 2020-09-28 10:37:02 -0700 | [diff] [blame] | 76 | BOOST_THROW_EXCEPTION(std::runtime_error("Cannot parse challenge list")); |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 77 | } |
| 78 | parseChallengeList(*challengeList); |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 81 | void |
| 82 | CaConfig::parseProbeParameters(const JsonSection& section) |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 83 | { |
Zhiyi Zhang | b2f8b94 | 2020-09-28 10:37:02 -0700 | [diff] [blame] | 84 | for (const auto item : section) { |
| 85 | auto probeParameter = item.second.get(CONFIG_PROBE_PARAMETER, ""); |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 86 | probeParameter = boost::algorithm::to_lower_copy(probeParameter); |
Zhiyi Zhang | b2f8b94 | 2020-09-28 10:37:02 -0700 | [diff] [blame] | 87 | if (probeParameter == "") { |
| 88 | BOOST_THROW_EXCEPTION(std::runtime_error("Cannot read probe-parameter-key in probe-parameters from the config file")); |
| 89 | } |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 90 | m_probeParameterKeys.push_back(probeParameter); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | CaConfig::parseChallengeList(const JsonSection& section) |
| 96 | { |
Zhiyi Zhang | b2f8b94 | 2020-09-28 10:37:02 -0700 | [diff] [blame] | 97 | for (const auto item : section) { |
| 98 | auto challengeType = item.second.get(CONFIG_CHALLENGE, ""); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 99 | challengeType = boost::algorithm::to_lower_copy(challengeType); |
Zhiyi Zhang | b2f8b94 | 2020-09-28 10:37:02 -0700 | [diff] [blame] | 100 | if (challengeType == "") { |
| 101 | BOOST_THROW_EXCEPTION(std::runtime_error("Cannot read type in supported-challenges from the config file")); |
| 102 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 103 | if (!ChallengeModule::isChallengeSupported(challengeType)) { |
Zhiyi Zhang | b2f8b94 | 2020-09-28 10:37:02 -0700 | [diff] [blame] | 104 | BOOST_THROW_EXCEPTION(std::runtime_error("Does not support challenge read from the config file")); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 105 | } |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 106 | m_supportedChallenges.push_back(challengeType); |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 107 | } |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 108 | if (m_supportedChallenges.size() == 0) { |
Zhiyi Zhang | b2f8b94 | 2020-09-28 10:37:02 -0700 | [diff] [blame] | 109 | BOOST_THROW_EXCEPTION(std::runtime_error("At least one challenge should be identified under supported-challenges")); |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 110 | } |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 113 | } // namespace ndncert |
| 114 | } // namespace ndn |