Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame^] | 1 | /* -*- 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/profile-storage.hpp" |
| 22 | #include "identity-challenge/challenge-module.hpp" |
| 23 | #include "name-assignment/assignment-func.hpp" |
| 24 | #include <ndn-cxx/util/io.hpp> |
| 25 | #include <boost/filesystem.hpp> |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace ndncert { |
| 29 | namespace requester { |
| 30 | |
| 31 | void |
| 32 | ProfileStorage::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 | } |
| 44 | load(configJson); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | ProfileStorage::load(const JsonSection& configSection) |
| 49 | { |
| 50 | m_caItems.clear(); |
| 51 | auto caList = configSection.get_child("ca-list"); |
| 52 | for (auto item : caList) { |
| 53 | CaProfile caItem; |
| 54 | caItem.parse(item.second); |
| 55 | if (caItem.m_cert == nullptr) { |
| 56 | NDN_THROW(std::runtime_error("No CA certificate is loaded from JSON configuration.")); |
| 57 | } |
| 58 | m_caItems.push_back(std::move(caItem)); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | ProfileStorage::save(const std::string& fileName) const |
| 64 | { |
| 65 | JsonSection configJson; |
| 66 | for (const auto& caItem : m_caItems) { |
| 67 | configJson.push_back(std::make_pair("", caItem.toJson())); |
| 68 | } |
| 69 | std::stringstream ss; |
| 70 | boost::property_tree::write_json(ss, configJson); |
| 71 | std::ofstream configFile; |
| 72 | configFile.open(fileName); |
| 73 | configFile << ss.str(); |
| 74 | configFile.close(); |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | ProfileStorage::removeCaProfile(const Name& caName) |
| 79 | { |
| 80 | m_caItems.remove_if([&](const CaProfile& item) { return item.m_caPrefix == caName; }); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | ProfileStorage::addCaProfile(const CaProfile& profile) |
| 85 | { |
| 86 | for (auto& item : m_caItems) { |
| 87 | if (item.m_caPrefix == profile.m_caPrefix) { |
| 88 | item = profile; |
| 89 | return; |
| 90 | } |
| 91 | } |
| 92 | m_caItems.push_back(profile); |
| 93 | } |
| 94 | |
| 95 | const std::list<CaProfile>& |
| 96 | ProfileStorage::getCaItems() const |
| 97 | { |
| 98 | return m_caItems; |
| 99 | } |
| 100 | |
| 101 | } // namespace requester |
| 102 | } // namespace ndncert |
| 103 | } // namespace ndn |