Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2019, Regents of the University of California. |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -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 "client-config.hpp" |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 22 | |
Zhiyi Zhang | 5ebeb69 | 2017-03-10 14:13:01 -0800 | [diff] [blame] | 23 | #include <ndn-cxx/util/io.hpp> |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 24 | #include <fstream> |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace ndncert { |
| 28 | |
| 29 | void |
| 30 | ClientConfig::load(const std::string& fileName) |
| 31 | { |
Zhiyi Zhang | e4ee822 | 2017-12-08 22:43:04 -0800 | [diff] [blame] | 32 | JsonSection config; |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 33 | try { |
Zhiyi Zhang | e4ee822 | 2017-12-08 22:43:04 -0800 | [diff] [blame] | 34 | boost::property_tree::read_json(fileName, config); |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 35 | } |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 36 | catch (const std::exception& error) { |
| 37 | BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + fileName + ", " + error.what())); |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Zhiyi Zhang | e4ee822 | 2017-12-08 22:43:04 -0800 | [diff] [blame] | 40 | if (config.begin() == config.end()) { |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 41 | BOOST_THROW_EXCEPTION(Error("Error processing configuration file: " + fileName + ", no data")); |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Zhiyi Zhang | e4ee822 | 2017-12-08 22:43:04 -0800 | [diff] [blame] | 44 | load(config); |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void |
Zhiyi Zhang | e4ee822 | 2017-12-08 22:43:04 -0800 | [diff] [blame] | 48 | ClientConfig::load(const JsonSection& configSection) |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 49 | { |
| 50 | m_caItems.clear(); |
Zhiyi Zhang | e4ee822 | 2017-12-08 22:43:04 -0800 | [diff] [blame] | 51 | auto caList = configSection.get_child("ca-list"); |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 52 | auto it = caList.begin(); |
| 53 | for (; it != caList.end(); it++) { |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 54 | m_caItems.push_back(extractCaItem(it->second)); |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 55 | } |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 56 | m_localNdncertAnchor = configSection.get("local-ndncert-anchor", ""); |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 59 | void |
| 60 | ClientConfig::save(const std::string& fileName) |
| 61 | { |
| 62 | JsonSection configJson; |
| 63 | JsonSection caList; |
| 64 | std::stringstream ss; |
| 65 | for (const auto& item : m_caItems) { |
| 66 | JsonSection caItem; |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 67 | caItem.put("ca-prefix", item.m_caPrefix.toUri()); |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 68 | caItem.put("ca-info", item.m_caInfo); |
| 69 | caItem.put("probe", item.m_probe); |
| 70 | ss.str(std::string()); |
| 71 | io::save(item.m_anchor, ss); |
| 72 | caItem.put("certificate", ss.str()); |
| 73 | caList.push_back(std::make_pair("", caItem)); |
| 74 | } |
| 75 | configJson.add_child("ca-list", caList); |
| 76 | ss.str(std::string()); |
| 77 | boost::property_tree::write_json(ss, configJson); |
| 78 | |
| 79 | std::ofstream configFile; |
| 80 | configFile.open(fileName, std::ios::trunc); |
| 81 | configFile << ss.str(); |
| 82 | configFile.close(); |
| 83 | } |
| 84 | |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 85 | ClientCaItem |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 86 | ClientConfig::extractCaItem(const JsonSection& configSection) |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 87 | { |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 88 | ClientCaItem item; |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 89 | item.m_caPrefix = Name(configSection.get("ca-prefix", "")); |
| 90 | if (item.m_caPrefix.empty()) { |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 91 | BOOST_THROW_EXCEPTION(Error("Cannot read ca-prefix from the config file")); |
| 92 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 93 | item.m_caInfo = configSection.get("ca-info", ""); |
| 94 | item.m_probe = configSection.get("probe", ""); |
| 95 | std::istringstream ss(configSection.get("certificate", "")); |
tylerliu | 0b6d0db | 2020-09-28 17:52:02 -0700 | [diff] [blame^] | 96 | item.m_maxSuffixLength = configSection.get<size_t>(CONFIG_MAX_SUFFIX_LENGTH, 1); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 97 | auto anchor = io::load<security::v2::Certificate>(ss); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 98 | if (anchor == nullptr) { |
| 99 | BOOST_THROW_EXCEPTION(Error("Cannot load the certificate from config file")); |
| 100 | } |
| 101 | item.m_anchor = *anchor; |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 102 | return item; |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void |
| 106 | ClientConfig::removeCaItem(const Name& caName) |
| 107 | { |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 108 | m_caItems.remove_if([&](const ClientCaItem& item) { return item.m_caPrefix == caName; }); |
Zhiyi Zhang | 32dbb9f | 2017-02-16 15:15:10 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Zhiyi Zhang | 0453dbb | 2020-04-28 22:39:17 -0700 | [diff] [blame] | 111 | } // namespace ndncert |
| 112 | } // namespace ndn |