blob: 0f4edc8958214b27dd35c21fd970c1c08febc524 [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 Zhangad6cf932017-10-26 16:19:15 -070031 JsonSection configJson;
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080032 try {
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070033 boost::property_tree::read_json(fileName, configJson);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080034 }
35 catch (const boost::property_tree::info_parser_error& error) {
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080036 BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + fileName +
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080037 " " + error.message() + " line " + std::to_string(error.line())));
38 }
39
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070040 if (configJson.begin() == configJson.end()) {
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080041 BOOST_THROW_EXCEPTION(Error("Error processing configuration file: " + fileName + " no data"));
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080042 }
43
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070044 parse(configJson);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080045}
46
47void
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070048CaConfig::parse(const JsonSection& configJson)
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080049{
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080050 m_caItems.clear();
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070051 auto caList = configJson.get_child("ca-list");
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080052 auto it = caList.begin();
53 for (; it != caList.end(); it++) {
54 CaItem item;
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080055
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070056 // essential info
57 item.m_caName = Name(it->second.get<std::string>("ca-prefix"));
58 item.m_freshnessPeriod = time::seconds(it->second.get("issuing-freshness", 720));
59 item.m_validityPeriod = time::days(it->second.get("validity-period", 360));
60
61 // optional info
62 item.m_probe = it->second.get("probe", "");
63 item.m_caInfo = it->second.get("ca-info", "");
64 item.m_targetedList = it->second.get("targeted-list", "");
65
66 // optional supported challenges
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080067 auto challengeList = it->second.get_child("supported-challenges");
68 item.m_supportedChallenges = parseChallengeList(challengeList);
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070069
70 // related cas
71 auto relatedCaList = it->second.get_child_optional("related-ca-list");
72 if (relatedCaList) {
73 item.m_relatedCaList = parseRelatedCaList(*relatedCaList);
74 }
75
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080076 m_caItems.push_back(item);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080077 }
78}
79
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080080std::list<std::string>
Zhiyi Zhang5ebeb692017-03-10 14:13:01 -080081CaConfig::parseChallengeList(const JsonSection& section)
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080082{
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080083 std::list<std::string> result;
84 auto it = section.begin();
85 for (; it != section.end(); it++) {
86 result.push_back(it->second.get<std::string>("type"));
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080087 }
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080088 return result;
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080089}
90
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070091std::list<ClientCaItem>
92CaConfig::parseRelatedCaList(const JsonSection& section)
93{
94 std::list<ClientCaItem> result;
95 auto it = section.begin();
96 for (; it != section.end(); it++) {
97 ClientCaItem item;
98 item.m_caName = Name(it->second.get<std::string>("ca-prefix"));
99 result.push_back(item);
100 }
101 return result;
102}
103
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -0800104} // namespace ndncert
105} // namespace ndn