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 | /* |
| 3 | * Copyright (c) 2017-2021, 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/profile-storage.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 <boost/filesystem.hpp> |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame^] | 24 | #include <boost/property_tree/json_parser.hpp> |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace ndncert { |
| 28 | namespace requester { |
| 29 | |
| 30 | void |
| 31 | ProfileStorage::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 | } |
| 40 | if (configJson.begin() == configJson.end()) { |
| 41 | NDN_THROW(std::runtime_error("No JSON configuration found in file: " + fileName)); |
| 42 | } |
| 43 | load(configJson); |
| 44 | } |
| 45 | |
| 46 | void |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame] | 47 | ProfileStorage::load(const JsonSection& json) |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 48 | { |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame] | 49 | m_caProfiles.clear(); |
| 50 | auto caList = json.get_child("ca-list"); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 51 | for (auto item : caList) { |
| 52 | CaProfile caItem; |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame] | 53 | caItem = CaProfile::fromJson(item.second); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 54 | if (caItem.cert == nullptr) { |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 55 | NDN_THROW(std::runtime_error("No CA certificate is loaded from JSON configuration.")); |
| 56 | } |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame] | 57 | m_caProfiles.push_back(std::move(caItem)); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | ProfileStorage::save(const std::string& fileName) const |
| 63 | { |
| 64 | JsonSection configJson; |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame] | 65 | for (const auto& caItem : m_caProfiles) { |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 66 | configJson.push_back(std::make_pair("", caItem.toJson())); |
| 67 | } |
| 68 | std::stringstream ss; |
| 69 | boost::property_tree::write_json(ss, configJson); |
| 70 | std::ofstream configFile; |
| 71 | configFile.open(fileName); |
| 72 | configFile << ss.str(); |
| 73 | configFile.close(); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | ProfileStorage::removeCaProfile(const Name& caName) |
| 78 | { |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 79 | m_caProfiles.remove_if([&](const CaProfile& item) { return item.caPrefix == caName; }); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void |
| 83 | ProfileStorage::addCaProfile(const CaProfile& profile) |
| 84 | { |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame] | 85 | for (auto& item : m_caProfiles) { |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 86 | if (item.caPrefix == profile.caPrefix) { |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 87 | item = profile; |
| 88 | return; |
| 89 | } |
| 90 | } |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame] | 91 | m_caProfiles.push_back(profile); |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | const std::list<CaProfile>& |
Zhiyi Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 95 | ProfileStorage::getKnownProfiles() const |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 96 | { |
Zhiyi Zhang | 1e418f2 | 2020-11-19 19:49:32 -0800 | [diff] [blame] | 97 | return m_caProfiles; |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } // namespace requester |
| 101 | } // namespace ndncert |
| 102 | } // namespace ndn |