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