Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -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 "client-config.hpp" |
Zhiyi Zhang | 5ebeb69 | 2017-03-10 14:13:01 -0800 | [diff] [blame^] | 22 | #include <ndn-cxx/util/io.hpp> |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 23 | |
| 24 | namespace ndn { |
| 25 | namespace ndncert { |
| 26 | |
| 27 | void |
| 28 | ClientConfig::load(const std::string& fileName) |
| 29 | { |
| 30 | try { |
| 31 | boost::property_tree::read_json(fileName, m_config); |
| 32 | } |
| 33 | catch (const boost::property_tree::info_parser_error& error) { |
| 34 | BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + fileName + |
| 35 | " " + error.message() + " line " + std::to_string(error.line()))); |
| 36 | } |
| 37 | |
| 38 | if (m_config.begin() == m_config.end()) { |
| 39 | BOOST_THROW_EXCEPTION(Error("Error processing configuration file: " + fileName + " no data")); |
| 40 | } |
| 41 | |
| 42 | parse(); |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | ClientConfig::parse() |
| 47 | { |
| 48 | m_caItems.clear(); |
| 49 | auto caList = m_config.get_child("ca-list"); |
| 50 | auto it = caList.begin(); |
| 51 | for (; it != caList.end(); it++) { |
Zhiyi Zhang | 5ebeb69 | 2017-03-10 14:13:01 -0800 | [diff] [blame^] | 52 | ClientCaItem item; |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 53 | item.m_caName = Name(it->second.get<std::string>("ca-prefix")); |
| 54 | item.m_caInfo = it->second.get<std::string>("ca-info"); |
| 55 | item.m_probe = it->second.get("probe", ""); |
| 56 | |
Zhiyi Zhang | 5ebeb69 | 2017-03-10 14:13:01 -0800 | [diff] [blame^] | 57 | std::istringstream ss(it->second.get<std::string>("certificate")); |
| 58 | item.m_anchor = *(io::load<security::v2::Certificate>(ss)); |
| 59 | |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 60 | auto challengeList = it->second.get_child("supported-challenges"); |
| 61 | item.m_supportedChallenges = parseChallengeList(challengeList); |
| 62 | |
| 63 | m_caItems.push_back(item); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | std::list<std::string> |
Zhiyi Zhang | 5ebeb69 | 2017-03-10 14:13:01 -0800 | [diff] [blame^] | 68 | ClientConfig::parseChallengeList(const JsonSection& section) |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 69 | { |
| 70 | std::list<std::string> result; |
| 71 | auto it = section.begin(); |
| 72 | for (; it != section.end(); it++) { |
| 73 | result.push_back(it->second.get<std::string>("type")); |
| 74 | } |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | void |
Zhiyi Zhang | 5ebeb69 | 2017-03-10 14:13:01 -0800 | [diff] [blame^] | 79 | ClientConfig::addNewCaItem(const ClientCaItem& item) |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 80 | { |
Zhiyi Zhang | 5ebeb69 | 2017-03-10 14:13:01 -0800 | [diff] [blame^] | 81 | m_caItems.push_back(item); |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void |
| 85 | ClientConfig::removeCaItem(const Name& caName) |
| 86 | { |
Zhiyi Zhang | 5ebeb69 | 2017-03-10 14:13:01 -0800 | [diff] [blame^] | 87 | m_caItems.remove_if([&] (const ClientCaItem& item) {return item.m_caName == caName;}); |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | } // namespace ndncert |
| 91 | } // namespace ndn |