Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 25 | namespace ndn { |
| 26 | namespace ndncert { |
| 27 | |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 28 | void |
Zhiyi Zhang | 06d6ae9 | 2017-03-08 14:59:45 -0800 | [diff] [blame] | 29 | CaConfig::load(const std::string& fileName) |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 30 | { |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 31 | JsonSection configJson; |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 32 | try { |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 33 | boost::property_tree::read_json(fileName, configJson); |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 34 | } |
| 35 | catch (const boost::property_tree::info_parser_error& error) { |
Zhiyi Zhang | 06d6ae9 | 2017-03-08 14:59:45 -0800 | [diff] [blame] | 36 | BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + fileName + |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 37 | " " + error.message() + " line " + std::to_string(error.line()))); |
| 38 | } |
| 39 | |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 40 | if (configJson.begin() == configJson.end()) { |
Zhiyi Zhang | 06d6ae9 | 2017-03-08 14:59:45 -0800 | [diff] [blame] | 41 | BOOST_THROW_EXCEPTION(Error("Error processing configuration file: " + fileName + " no data")); |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 44 | parse(configJson); |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 48 | CaConfig::parse(const JsonSection& configJson) |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 49 | { |
Zhiyi Zhang | 06d6ae9 | 2017-03-08 14:59:45 -0800 | [diff] [blame] | 50 | m_caItems.clear(); |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 51 | auto caList = configJson.get_child("ca-list"); |
Zhiyi Zhang | 06d6ae9 | 2017-03-08 14:59:45 -0800 | [diff] [blame] | 52 | auto it = caList.begin(); |
| 53 | for (; it != caList.end(); it++) { |
| 54 | CaItem item; |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 55 | |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 56 | // 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 Zhang | 06d6ae9 | 2017-03-08 14:59:45 -0800 | [diff] [blame] | 67 | auto challengeList = it->second.get_child("supported-challenges"); |
| 68 | item.m_supportedChallenges = parseChallengeList(challengeList); |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 69 | |
| 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 Zhang | 06d6ae9 | 2017-03-08 14:59:45 -0800 | [diff] [blame] | 76 | m_caItems.push_back(item); |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Zhiyi Zhang | 06d6ae9 | 2017-03-08 14:59:45 -0800 | [diff] [blame] | 80 | std::list<std::string> |
Zhiyi Zhang | 5ebeb69 | 2017-03-10 14:13:01 -0800 | [diff] [blame] | 81 | CaConfig::parseChallengeList(const JsonSection& section) |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 82 | { |
Zhiyi Zhang | 06d6ae9 | 2017-03-08 14:59:45 -0800 | [diff] [blame] | 83 | 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 Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 87 | } |
Zhiyi Zhang | 06d6ae9 | 2017-03-08 14:59:45 -0800 | [diff] [blame] | 88 | return result; |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Zhiyi Zhang | b23c5bb | 2017-12-18 18:40:52 +0800 | [diff] [blame^] | 91 | std::list<Name> |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 92 | CaConfig::parseRelatedCaList(const JsonSection& section) |
| 93 | { |
Zhiyi Zhang | b23c5bb | 2017-12-18 18:40:52 +0800 | [diff] [blame^] | 94 | std::list<Name> result; |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 95 | auto it = section.begin(); |
| 96 | for (; it != section.end(); it++) { |
Zhiyi Zhang | b23c5bb | 2017-12-18 18:40:52 +0800 | [diff] [blame^] | 97 | Name item(it->second.get<std::string>("ca-prefix")); |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 98 | result.push_back(item); |
| 99 | } |
| 100 | return result; |
| 101 | } |
| 102 | |
Zhiyi Zhang | daf2fd7 | 2017-01-19 11:31:35 -0800 | [diff] [blame] | 103 | } // namespace ndncert |
| 104 | } // namespace ndn |