blob: 116e1d8609a93c044bfadf101cc516a413e6b415 [file] [log] [blame]
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -08001/* -*- 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 Zhang5ebeb692017-03-10 14:13:01 -080022#include <ndn-cxx/util/io.hpp>
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080023
24namespace ndn {
25namespace ndncert {
26
27void
28ClientConfig::load(const std::string& fileName)
29{
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080030 JsonSection config;
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080031 try {
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080032 boost::property_tree::read_json(fileName, config);
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080033 }
34 catch (const boost::property_tree::info_parser_error& error) {
35 BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + fileName +
36 " " + error.message() + " line " + std::to_string(error.line())));
37 }
38
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080039 if (config.begin() == config.end()) {
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080040 BOOST_THROW_EXCEPTION(Error("Error processing configuration file: " + fileName + " no data"));
41 }
42
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080043 load(config);
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080044}
45
46void
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080047ClientConfig::load(const JsonSection& configSection)
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080048{
49 m_caItems.clear();
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080050 auto caList = configSection.get_child("ca-list");
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080051 auto it = caList.begin();
52 for (; it != caList.end(); it++) {
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -080053 ClientCaItem item;
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080054 item.m_caName = Name(it->second.get<std::string>("ca-prefix"));
55 item.m_caInfo = it->second.get<std::string>("ca-info");
56 item.m_probe = it->second.get("probe", "");
Zhiyi Zhange4ee8222017-12-08 22:43:04 -080057 item.m_targetedList = it->second.get("target-list", "");
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080058
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -080059 std::istringstream ss(it->second.get<std::string>("certificate"));
60 item.m_anchor = *(io::load<security::v2::Certificate>(ss));
61
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080062 m_caItems.push_back(item);
63 }
64}
65
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080066void
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -080067ClientConfig::addNewCaItem(const ClientCaItem& item)
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080068{
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -080069 m_caItems.push_back(item);
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080070}
71
72void
73ClientConfig::removeCaItem(const Name& caName)
74{
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -080075 m_caItems.remove_if([&] (const ClientCaItem& item) {return item.m_caName == caName;});
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080076}
77
78} // namespace ndncert
79} // namespace ndn