Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
| 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 "detail/ca-configuration.hpp" |
| 22 | #include "identity-challenge/challenge-module.hpp" |
| 23 | #include "name-assignment/assignment-func.hpp" |
| 24 | #include <ndn-cxx/util/io.hpp> |
| 25 | #include <boost/filesystem.hpp> |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace ndncert { |
| 29 | namespace ca { |
| 30 | |
| 31 | void |
| 32 | CaConfig::load(const std::string& fileName) |
| 33 | { |
| 34 | JsonSection configJson; |
| 35 | try { |
| 36 | boost::property_tree::read_json(fileName, configJson); |
| 37 | } |
| 38 | catch (const std::exception& error) { |
| 39 | NDN_THROW(std::runtime_error("Failed to parse configuration file " + fileName + ", " + error.what())); |
| 40 | } |
| 41 | if (configJson.begin() == configJson.end()) { |
| 42 | NDN_THROW(std::runtime_error("No JSON configuration found in file: " + fileName)); |
| 43 | } |
| 44 | m_caItem.parse(configJson); |
| 45 | if (m_caItem.m_supportedChallenges.size() == 0) { |
| 46 | NDN_THROW(std::runtime_error("At least one challenge should be specified.")); |
| 47 | } |
| 48 | // parse redirection section if appears |
| 49 | m_redirection = nullopt; |
| 50 | auto redirectionItems = configJson.get_child_optional(CONFIG_REDIRECTION); |
| 51 | if (redirectionItems) { |
| 52 | for (const auto& item : *redirectionItems) { |
| 53 | auto caPrefixStr = item.second.get(CONFIG_CA_PREFIX, ""); |
| 54 | auto caCertStr = item.second.get(CONFIG_CERTIFICATE, ""); |
| 55 | if (caCertStr == "") { |
| 56 | NDN_THROW(std::runtime_error("Redirect-to item's ca-prefix or certificate cannot be empty.")); |
| 57 | } |
| 58 | std::istringstream ss(caCertStr); |
| 59 | auto caCert = io::load<security::Certificate>(ss); |
| 60 | if (!m_redirection) { |
| 61 | m_redirection = std::vector<std::shared_ptr<security::Certificate>>(); |
| 62 | } |
| 63 | m_redirection->push_back(caCert); |
| 64 | } |
| 65 | } |
| 66 | // parse name assignment if appears |
| 67 | m_nameAssignmentFuncs.clear(); |
| 68 | auto nameAssignmentItems = configJson.get_child_optional(CONFIG_NAME_ASSIGNMENT); |
| 69 | if (nameAssignmentItems) { |
| 70 | for (const auto& item : *nameAssignmentItems) { |
| 71 | auto func = NameAssignmentFunc::createNameAssignmentFunc(item.first, item.second.data()); |
| 72 | if (func == nullptr) { |
| 73 | NDN_THROW(std::runtime_error("Error on creating name assignment function")); |
| 74 | } |
| 75 | m_nameAssignmentFuncs.push_back(std::move(func)); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | } // namespace ca |
| 81 | } // namespace ndncert |
| 82 | } // namespace ndn |