Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 2 | /* |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 3 | * Copyright (c) 2017-2022, Regents of the University of California. |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 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" |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 22 | |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 23 | #include <ndn-cxx/util/io.hpp> |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 24 | |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 25 | #include <boost/filesystem.hpp> |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 26 | #include <boost/property_tree/json_parser.hpp> |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 27 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 28 | namespace ndncert::ca { |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 29 | |
| 30 | void |
| 31 | CaConfig::load(const std::string& fileName) |
| 32 | { |
| 33 | JsonSection configJson; |
| 34 | try { |
| 35 | boost::property_tree::read_json(fileName, configJson); |
| 36 | } |
| 37 | catch (const std::exception& error) { |
| 38 | NDN_THROW(std::runtime_error("Failed to parse configuration file " + fileName + ", " + error.what())); |
| 39 | } |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 40 | |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 41 | if (configJson.begin() == configJson.end()) { |
| 42 | NDN_THROW(std::runtime_error("No JSON configuration found in file: " + fileName)); |
| 43 | } |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 44 | caProfile = CaProfile::fromJson(configJson); |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 45 | if (caProfile.supportedChallenges.empty()) { |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 46 | NDN_THROW(std::runtime_error("At least one challenge should be specified.")); |
| 47 | } |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 48 | |
| 49 | // parse redirection section if present |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 50 | redirection.clear(); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 51 | auto redirectionItems = configJson.get_child_optional(CONFIG_REDIRECTION); |
| 52 | if (redirectionItems) { |
| 53 | for (const auto& item : *redirectionItems) { |
| 54 | auto caPrefixStr = item.second.get(CONFIG_CA_PREFIX, ""); |
| 55 | auto caCertStr = item.second.get(CONFIG_CERTIFICATE, ""); |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 56 | if (caCertStr.empty()) { |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 57 | NDN_THROW(std::runtime_error("Redirect-to item's certificate cannot be empty.")); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 58 | } |
| 59 | std::istringstream ss(caCertStr); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 60 | auto caCert = ndn::io::load<Certificate>(ss); |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 61 | if (!caPrefixStr.empty() && Name(caPrefixStr) != caCert->getIdentity()) { |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 62 | NDN_THROW(std::runtime_error("Redirect-to item's prefix and certificate does not match.")); |
| 63 | } |
| 64 | |
| 65 | auto policyType = item.second.get(CONFIG_REDIRECTION_POLICY_TYPE, ""); |
| 66 | auto policyParam = item.second.get(CONFIG_REDIRECTION_POLICY_PARAM, ""); |
| 67 | if (policyType.empty()) { |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 68 | NDN_THROW(std::runtime_error("Redirect-to policy type expected but not provided.")); |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 69 | } |
| 70 | auto policy = RedirectionPolicy::createPolicyFunc(policyType, policyParam); |
| 71 | if (policy == nullptr) { |
| 72 | NDN_THROW(std::runtime_error("Error on creating redirection policy")); |
| 73 | } |
| 74 | redirection.emplace_back(caCert, std::move(policy)); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 77 | |
| 78 | // parse name assignment if present |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 79 | nameAssignmentFuncs.clear(); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 80 | auto nameAssignmentItems = configJson.get_child_optional(CONFIG_NAME_ASSIGNMENT); |
| 81 | if (nameAssignmentItems) { |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 82 | for (const auto& [key, val] : *nameAssignmentItems) { |
| 83 | auto func = NameAssignmentFunc::createNameAssignmentFunc(key, val.data()); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 84 | if (func == nullptr) { |
| 85 | NDN_THROW(std::runtime_error("Error on creating name assignment function")); |
| 86 | } |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 87 | nameAssignmentFuncs.push_back(std::move(func)); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 92 | } // namespace ndncert::ca |