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