Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017, 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 "ca-config.hpp" |
| 22 | #include <ndn-cxx/util/io.hpp> |
| 23 | #include <boost/filesystem.hpp> |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ndncert { |
| 27 | |
| 28 | CaConfig::CaConfig() = default; |
| 29 | |
| 30 | CaConfig::CaConfig(const std::string& fileName) |
| 31 | : m_fileName(fileName) |
| 32 | { |
| 33 | open(); |
| 34 | load(); |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | CaConfig::open() |
| 39 | { |
| 40 | std::ifstream inputFile; |
| 41 | inputFile.open(m_fileName.c_str()); |
| 42 | if (!inputFile.good() || !inputFile.is_open()) { |
| 43 | std::string msg = "Failed to read configuration file: "; |
| 44 | msg += m_fileName; |
| 45 | BOOST_THROW_EXCEPTION(Error(msg)); |
| 46 | } |
| 47 | |
| 48 | try { |
| 49 | boost::property_tree::read_info(inputFile, m_config); |
| 50 | } |
| 51 | catch (const boost::property_tree::info_parser_error& error) { |
| 52 | BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + m_fileName + |
| 53 | " " + error.message() + " line " + std::to_string(error.line()))); |
| 54 | } |
| 55 | |
| 56 | if (m_config.begin() == m_config.end()) { |
| 57 | BOOST_THROW_EXCEPTION(Error("Error processing configuration file: " + m_fileName + " no data")); |
| 58 | } |
| 59 | |
| 60 | inputFile.close(); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | CaConfig::load() |
| 65 | { |
| 66 | m_caName = Name(m_config.get<std::string>("name")); |
| 67 | m_validatorConfig = m_config.get_child("validator-conf"); |
| 68 | |
| 69 | parseCertificateInfo(m_config.get_child("certificate-info")); |
| 70 | parseCaAnchor(m_config.get_child("ca-anchor")); |
| 71 | parseChallengeList(m_config.get_child("challenge-list")); |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | CaConfig::parseCertificateInfo(const ConfigSection& configSection) |
| 76 | { |
| 77 | m_freshPeriod = configSection.get<uint64_t>("freshness-period"); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | CaConfig::parseCaAnchor(const ConfigSection& configSection) |
| 82 | { |
| 83 | std::string type = configSection.get<std::string>("type"); |
| 84 | std::string value = configSection.get<std::string>("value"); |
| 85 | if (type == "file") { |
| 86 | boost::filesystem::path certfilePath = absolute(value, |
| 87 | boost::filesystem::path(m_fileName).parent_path()); |
| 88 | m_anchor = io::load<security::v2::Certificate>(certfilePath.string()); |
| 89 | if (m_anchor != nullptr) { |
| 90 | BOOST_ASSERT(m_anchor->getName().size() >= 1); |
| 91 | } |
| 92 | else |
| 93 | BOOST_THROW_EXCEPTION(Error("Cannot read certificate from file: " + certfilePath.native())); |
| 94 | } |
| 95 | else if (type == "base64") { |
| 96 | std::istringstream ss(value); |
| 97 | m_anchor = io::load<security::v2::Certificate>(ss); |
| 98 | } |
| 99 | else { |
| 100 | BOOST_THROW_EXCEPTION(Error("Unrecognized trust anchor '" + type + "' '" + value + "'")); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | CaConfig::parseChallengeList(const ConfigSection& configSection) |
| 106 | { |
| 107 | auto it = configSection.begin(); |
| 108 | for (; it != configSection.end(); it++) { |
| 109 | m_availableChallenges.push_back(it->second.get<std::string>("type")); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | } // namespace ndncert |
| 114 | } // namespace ndn |