blob: 1032709a637c07b22f8c1a2702014fd4e398c7ec [file] [log] [blame]
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -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 "ca-config.hpp"
22#include <ndn-cxx/util/io.hpp>
23#include <boost/filesystem.hpp>
24
25namespace ndn {
26namespace ndncert {
27
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080028void
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080029CaConfig::load(const std::string& fileName)
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080030{
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080031 try {
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080032 boost::property_tree::read_json(fileName, m_config);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080033 }
34 catch (const boost::property_tree::info_parser_error& error) {
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080035 BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + fileName +
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080036 " " + error.message() + " line " + std::to_string(error.line())));
37 }
38
39 if (m_config.begin() == m_config.end()) {
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080040 BOOST_THROW_EXCEPTION(Error("Error processing configuration file: " + fileName + " no data"));
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080041 }
42
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080043 parse();
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080044}
45
46void
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080047CaConfig::parse()
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080048{
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080049 m_caItems.clear();
50 auto caList = m_config.get_child("ca-list");
51 auto it = caList.begin();
52 for (; it != caList.end(); it++) {
53 CaItem item;
54 item.m_caName = Name(it->second.get<std::string>("ca-prefix"));
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -080055 item.m_probe = it->second.get("probe", false);
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080056 item.m_freshnessPeriod = time::seconds(it->second.get<uint64_t>("issuing-freshness"));
57 item.m_validityPeriod = time::days(it->second.get<uint64_t>("validity-period"));
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080058
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080059 auto challengeList = it->second.get_child("supported-challenges");
60 item.m_supportedChallenges = parseChallengeList(challengeList);
61 item.m_anchor = Name(it->second.get<std::string>("ca-anchor"));
62 m_caItems.push_back(item);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080063 }
64}
65
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080066std::list<std::string>
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -080067CaConfig::parseChallengeList(const JsonSection& section)
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080068{
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080069 std::list<std::string> result;
70 auto it = section.begin();
71 for (; it != section.end(); it++) {
72 result.push_back(it->second.get<std::string>("type"));
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080073 }
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080074 return result;
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080075}
76
77} // namespace ndncert
78} // namespace ndn