blob: 55b8a7c2169238d1b91c0ad8cc01a605f729178e [file] [log] [blame]
Zhiyi Zhang3f20f952020-11-19 19:26:43 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe5b43692021-11-15 22:05:03 -05002/*
Tianyuan Yu13aac732022-03-03 20:59:54 -08003 * Copyright (c) 2017-2022, Regents of the University of California.
Zhiyi Zhang3f20f952020-11-19 19:26:43 -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 "detail/ca-configuration.hpp"
Davide Pesaventoe5b43692021-11-15 22:05:03 -050022
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080023#include <ndn-cxx/util/io.hpp>
Davide Pesaventoe5b43692021-11-15 22:05:03 -050024
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080025#include <boost/filesystem.hpp>
Davide Pesaventoe5b43692021-11-15 22:05:03 -050026#include <boost/property_tree/json_parser.hpp>
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080027
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080028namespace ndncert {
29namespace ca {
30
31void
32CaConfig::load(const std::string& fileName)
33{
34 JsonSection configJson;
35 try {
36 boost::property_tree::read_json(fileName, configJson);
37 }
38 catch (const std::exception& error) {
39 NDN_THROW(std::runtime_error("Failed to parse configuration file " + fileName + ", " + error.what()));
40 }
41 if (configJson.begin() == configJson.end()) {
42 NDN_THROW(std::runtime_error("No JSON configuration found in file: " + fileName));
43 }
Davide Pesaventoe5b43692021-11-15 22:05:03 -050044 caProfile = CaProfile::fromJson(configJson);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080045 if (caProfile.supportedChallenges.size() == 0) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080046 NDN_THROW(std::runtime_error("At least one challenge should be specified."));
47 }
48 // parse redirection section if appears
tylerliuf2e6bb52020-12-13 13:23:05 -080049 redirection.clear();
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080050 auto redirectionItems = configJson.get_child_optional(CONFIG_REDIRECTION);
51 if (redirectionItems) {
52 for (const auto& item : *redirectionItems) {
53 auto caPrefixStr = item.second.get(CONFIG_CA_PREFIX, "");
54 auto caCertStr = item.second.get(CONFIG_CERTIFICATE, "");
55 if (caCertStr == "") {
Tianyuan Yu13aac732022-03-03 20:59:54 -080056 NDN_THROW(std::runtime_error("Redirect-to item's certificate cannot be empty."));
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080057 }
58 std::istringstream ss(caCertStr);
Davide Pesavento0dc02012021-11-23 22:55:03 -050059 auto caCert = ndn::io::load<Certificate>(ss);
Tianyuan Yu13aac732022-03-03 20:59:54 -080060 if (caPrefixStr != "" && Name(caPrefixStr) != caCert->getIdentity()) {
61 NDN_THROW(std::runtime_error("Redirect-to item's prefix and certificate does not match."));
62 }
63
64 auto policyType = item.second.get(CONFIG_REDIRECTION_POLICY_TYPE, "");
65 auto policyParam = item.second.get(CONFIG_REDIRECTION_POLICY_PARAM, "");
66 if (policyType.empty()) {
67 NDN_THROW(std::runtime_error("Redirect-to policy type expected but not provided."));
68 }
69 auto policy = RedirectionPolicy::createPolicyFunc(policyType, policyParam);
70 if (policy == nullptr) {
71 NDN_THROW(std::runtime_error("Error on creating redirection policy"));
72 }
73 redirection.emplace_back(caCert, std::move(policy));
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080074 }
75 }
76 // parse name assignment if appears
tylerliuf2e6bb52020-12-13 13:23:05 -080077 nameAssignmentFuncs.clear();
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080078 auto nameAssignmentItems = configJson.get_child_optional(CONFIG_NAME_ASSIGNMENT);
79 if (nameAssignmentItems) {
80 for (const auto& item : *nameAssignmentItems) {
81 auto func = NameAssignmentFunc::createNameAssignmentFunc(item.first, item.second.data());
82 if (func == nullptr) {
83 NDN_THROW(std::runtime_error("Error on creating name assignment function"));
84 }
tylerliuf2e6bb52020-12-13 13:23:05 -080085 nameAssignmentFuncs.push_back(std::move(func));
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080086 }
87 }
88}
89
90} // namespace ca
91} // namespace ndncert