blob: 19f8a89ab33150d80539cf4bf088aedadc30866c [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"
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -080022#include <ndn-cxx/util/io.hpp>
Zhiyi Zhangcaab5462019-10-18 13:41:02 -070023#include <fstream>
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080024
25namespace ndn {
26namespace ndncert {
27
28void
29ClientConfig::load(const std::string& fileName)
30{
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080031 JsonSection config;
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080032 try {
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080033 boost::property_tree::read_json(fileName, config);
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080034 }
35 catch (const boost::property_tree::info_parser_error& error) {
36 BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + fileName +
37 " " + error.message() + " line " + std::to_string(error.line())));
38 }
39
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080040 if (config.begin() == config.end()) {
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080041 BOOST_THROW_EXCEPTION(Error("Error processing configuration file: " + fileName + " no data"));
42 }
43
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080044 load(config);
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080045}
46
47void
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080048ClientConfig::load(const JsonSection& configSection)
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080049{
50 m_caItems.clear();
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080051 auto caList = configSection.get_child("ca-list");
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080052 auto it = caList.begin();
53 for (; it != caList.end(); it++) {
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080054 m_caItems.push_back(extractCaItem(it->second));
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080055 }
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080056 m_localNdncertAnchor = configSection.get("local-ndncert-anchor", "");
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080057}
58
Zhiyi Zhangcaab5462019-10-18 13:41:02 -070059void
60ClientConfig::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;
67 caItem.put("ca-prefix", item.m_caName.toUri());
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 Zhang1c0bd372017-12-18 18:32:55 +080085ClientCaItem
86ClientConfig::extractCaItem(const JsonSection& configSection)
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080087{
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080088 ClientCaItem item;
89 item.m_caName = Name(configSection.get<std::string>("ca-prefix"));
90 item.m_caInfo = configSection.get<std::string>("ca-info");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070091 item.m_probe = configSection.get<std::string>("probe");
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080092 std::istringstream ss(configSection.get<std::string>("certificate"));
93 item.m_anchor = *(io::load<security::v2::Certificate>(ss));
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080094 return item;
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080095}
96
97void
98ClientConfig::removeCaItem(const Name& caName)
99{
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -0800100 m_caItems.remove_if([&] (const ClientCaItem& item) {return item.m_caName == caName;});
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -0800101}
102
103} // namespace ndncert
104} // namespace ndn