blob: 314583905a7f6cedfb2bb0cfcfa1a12a56f8fbc8 [file] [log] [blame]
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -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 Zhangdaf2fd72017-01-19 11:31:35 -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 "ca-config.hpp"
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080022#include "challenge-module.hpp"
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080023#include <ndn-cxx/util/io.hpp>
24#include <boost/filesystem.hpp>
25
26namespace ndn {
27namespace ndncert {
28
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080029void
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080030CaConfig::load(const std::string& fileName)
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080031{
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070032 JsonSection configJson;
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080033 try {
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070034 boost::property_tree::read_json(fileName, configJson);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080035 }
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080036 catch (const std::exception& error) {
37 BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + fileName + ", " + error.what()));
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080038 }
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 }
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070043 parse(configJson);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080044}
45
46void
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070047CaConfig::parse(const JsonSection& configJson)
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080048{
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080049 // essential info
50 m_caName = Name(configJson.get("ca-prefix", ""));
51 if (m_caName.empty()) {
52 BOOST_THROW_EXCEPTION(Error("Cannot read ca-prefix from the config file"));
53 }
54 m_freshnessPeriod = time::seconds(configJson.get("issuing-freshness", 720));
55 m_validityPeriod = time::days(configJson.get("max-validity-period", 360));
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070056
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080057 // optional info
58 m_probe = configJson.get("probe", "");
59 m_caInfo = configJson.get("ca-info", "");
Zhiyi Zhangad6cf932017-10-26 16:19:15 -070060
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080061 // optional supported challenges
62 auto challengeList = configJson.get_child("supported-challenges");
63 m_supportedChallenges = parseChallengeList(challengeList);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080064}
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++) {
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080072 auto challengeType = it->second.get("type", "");
73 if (challengeType == "") {
74 BOOST_THROW_EXCEPTION(Error("Cannot read type in supported-challenges from the config file"));
75 }
76 challengeType = boost::algorithm::to_lower_copy(challengeType);
77 if (!ChallengeModule::supportChallenge(challengeType)) {
78 BOOST_THROW_EXCEPTION(Error("Does not support challenge read from the config file"));
79 }
80 result.push_back(challengeType);
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080081 }
Zhiyi Zhang06d6ae92017-03-08 14:59:45 -080082 return result;
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080083}
84
Zhiyi Zhangdaf2fd72017-01-19 11:31:35 -080085} // namespace ndncert
86} // namespace ndn