blob: 857c6a8638a5a02f705e4219afa1a5c5587337f6 [file] [log] [blame]
Zhiyi Zhang3f20f952020-11-19 19:26:43 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2017-2020, 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 "detail/ca-configuration.hpp"
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080022#include <ndn-cxx/util/io.hpp>
23#include <boost/filesystem.hpp>
24
25namespace ndn {
26namespace ndncert {
27namespace ca {
28
29void
30CaConfig::load(const std::string& fileName)
31{
32 JsonSection configJson;
33 try {
34 boost::property_tree::read_json(fileName, configJson);
35 }
36 catch (const std::exception& error) {
37 NDN_THROW(std::runtime_error("Failed to parse configuration file " + fileName + ", " + error.what()));
38 }
39 if (configJson.begin() == configJson.end()) {
40 NDN_THROW(std::runtime_error("No JSON configuration found in file: " + fileName));
41 }
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080042 m_caProfile = CaProfile::fromJson(configJson);
43 if (m_caProfile.m_supportedChallenges.size() == 0) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080044 NDN_THROW(std::runtime_error("At least one challenge should be specified."));
45 }
46 // parse redirection section if appears
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080047 m_redirection.clear();
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080048 auto redirectionItems = configJson.get_child_optional(CONFIG_REDIRECTION);
49 if (redirectionItems) {
50 for (const auto& item : *redirectionItems) {
51 auto caPrefixStr = item.second.get(CONFIG_CA_PREFIX, "");
52 auto caCertStr = item.second.get(CONFIG_CERTIFICATE, "");
53 if (caCertStr == "") {
54 NDN_THROW(std::runtime_error("Redirect-to item's ca-prefix or certificate cannot be empty."));
55 }
56 std::istringstream ss(caCertStr);
57 auto caCert = io::load<security::Certificate>(ss);
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080058 m_redirection.push_back(caCert);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080059 }
60 }
61 // parse name assignment if appears
62 m_nameAssignmentFuncs.clear();
63 auto nameAssignmentItems = configJson.get_child_optional(CONFIG_NAME_ASSIGNMENT);
64 if (nameAssignmentItems) {
65 for (const auto& item : *nameAssignmentItems) {
66 auto func = NameAssignmentFunc::createNameAssignmentFunc(item.first, item.second.data());
67 if (func == nullptr) {
68 NDN_THROW(std::runtime_error("Error on creating name assignment function"));
69 }
70 m_nameAssignmentFuncs.push_back(std::move(func));
71 }
72 }
73}
74
75} // namespace ca
76} // namespace ndncert
77} // namespace ndn