blob: 7fd52807c8295a34ad9b919bae72ef8585d35e28 [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{
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
45void
46ClientConfig::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 Zhang5ebeb692017-03-10 14:13:01 -080052 ClientCaItem item;
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080053 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 Zhang5ebeb692017-03-10 14:13:01 -080057 std::istringstream ss(it->second.get<std::string>("certificate"));
58 item.m_anchor = *(io::load<security::v2::Certificate>(ss));
59
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080060 auto challengeList = it->second.get_child("supported-challenges");
61 item.m_supportedChallenges = parseChallengeList(challengeList);
62
63 m_caItems.push_back(item);
64 }
65}
66
67std::list<std::string>
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -080068ClientConfig::parseChallengeList(const JsonSection& section)
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080069{
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
78void
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -080079ClientConfig::addNewCaItem(const ClientCaItem& item)
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080080{
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -080081 m_caItems.push_back(item);
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080082}
83
84void
85ClientConfig::removeCaItem(const Name& caName)
86{
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -080087 m_caItems.remove_if([&] (const ClientCaItem& item) {return item.m_caName == caName;});
Zhiyi Zhang32dbb9f2017-02-16 15:15:10 -080088}
89
90} // namespace ndncert
91} // namespace ndn