blob: 47115bbbbd712666f4cbf23c8513fede725c1b22 [file] [log] [blame]
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2017-2019, Regents of the University of California.
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -08004 *
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"
Suyong Won57462ca2020-05-05 22:20:09 -070022#include "tlv.hpp"
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -070023
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -080024#include <ndn-cxx/util/io.hpp>
Zhiyi Zhangcaab5462019-10-18 13:41:02 -070025#include <fstream>
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080026
27namespace ndn {
28namespace ndncert {
29
30void
31ClientConfig::load(const std::string& fileName)
32{
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080033 JsonSection config;
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080034 try {
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080035 boost::property_tree::read_json(fileName, config);
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080036 }
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080037 catch (const std::exception& error) {
38 BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + fileName + ", " + error.what()));
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080039 }
40
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080041 if (config.begin() == config.end()) {
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080042 BOOST_THROW_EXCEPTION(Error("Error processing configuration file: " + fileName + ", no data"));
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080043 }
44
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080045 load(config);
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080046}
47
48void
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080049ClientConfig::load(const JsonSection& configSection)
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080050{
51 m_caItems.clear();
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080052 auto caList = configSection.get_child("ca-list");
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080053 auto it = caList.begin();
54 for (; it != caList.end(); it++) {
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080055 m_caItems.push_back(extractCaItem(it->second));
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080056 }
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080057 m_localNdncertAnchor = configSection.get("local-ndncert-anchor", "");
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080058}
59
Zhiyi Zhangcaab5462019-10-18 13:41:02 -070060void
61ClientConfig::save(const std::string& fileName)
62{
63 JsonSection configJson;
64 JsonSection caList;
65 std::stringstream ss;
66 for (const auto& item : m_caItems) {
67 JsonSection caItem;
68 caItem.put("ca-prefix", item.m_caName.toUri());
69 caItem.put("ca-info", item.m_caInfo);
70 caItem.put("probe", item.m_probe);
71 ss.str(std::string());
72 io::save(item.m_anchor, ss);
73 caItem.put("certificate", ss.str());
74 caList.push_back(std::make_pair("", caItem));
75 }
76 configJson.add_child("ca-list", caList);
77 ss.str(std::string());
78 boost::property_tree::write_json(ss, configJson);
79
80 std::ofstream configFile;
81 configFile.open(fileName, std::ios::trunc);
82 configFile << ss.str();
83 configFile.close();
84}
85
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080086ClientCaItem
Suyong Won19fba4d2020-05-09 13:39:46 -070087ClientConfig::extractCaItem(const JsonSection& configSection)
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080088{
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080089 ClientCaItem item;
Suyong Won19fba4d2020-05-09 13:39:46 -070090 item.m_caName = Name(configSection.get("ca-prefix", ""));
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080091 if (item.m_caName.empty()) {
92 BOOST_THROW_EXCEPTION(Error("Cannot read ca-prefix from the config file"));
93 }
Suyong Won19fba4d2020-05-09 13:39:46 -070094 item.m_caInfo = configSection.get("ca-info", "");
95 item.m_probe = configSection.get("probe", "");
96 std::istringstream ss(configSection.get("certificate", ""));
97 auto anchor = io::load<security::v2::Certificate>(ss);
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080098 if (anchor == nullptr) {
99 BOOST_THROW_EXCEPTION(Error("Cannot load the certificate from config file"));
100 }
101 item.m_anchor = *anchor;
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800102 return item;
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -0800103}
104
Suyong Won19fba4d2020-05-09 13:39:46 -0700105ClientCaItem
106ClientConfig::extractCaItem(const Block& contentBlock)
107{
108 ClientCaItem item;
109 item.m_caName = Name(readString(contentBlock.get(tlv_ca_prefix)));
110 if (item.m_caName.empty()) {
111 BOOST_THROW_EXCEPTION(Error("Cannot read ca-prefix from the config file"));
112 }
113 item.m_caInfo = readString(contentBlock.get(tlv_ca_info));
114 // item.m_probe = configSection.get("probe", "");
115
116 if (!contentBlock.get(tlv_ca_certificate).hasValue()) {
117 BOOST_THROW_EXCEPTION(Error("Cannot load the certificate from config file"));
118 }
119
120 security::v2::Certificate anchor;
121 anchor.wireDecode(contentBlock.get(tlv_ca_certificate));
122 item.m_anchor = anchor;
123
124 return item;
125}
126
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -0800127void
128ClientConfig::removeCaItem(const Name& caName)
129{
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700130 m_caItems.remove_if([&](const ClientCaItem& item) { return item.m_caName == caName; });
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -0800131}
132
Zhiyi Zhang0453dbb2020-04-28 22:39:17 -0700133} // namespace ndncert
134} // namespace ndn