blob: 8ae2bb6000d34b75ed10c4029df354f98a57c058 [file] [log] [blame]
Zhiyi Zhang3f20f952020-11-19 19:26:43 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe5b43692021-11-15 22:05:03 -05002/*
3 * Copyright (c) 2017-2021, Regents of the University of California.
Zhiyi Zhang3f20f952020-11-19 19:26:43 -08004 *
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 Pesaventoe5b43692021-11-15 22:05:03 -050022
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080023#include <boost/filesystem.hpp>
Davide Pesaventoe5b43692021-11-15 22:05:03 -050024#include <boost/property_tree/json_parser.hpp>
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080025
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080026namespace ndncert {
27namespace requester {
28
29void
30ProfileStorage::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
45void
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080046ProfileStorage::load(const JsonSection& json)
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080047{
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080048 m_caProfiles.clear();
49 auto caList = json.get_child("ca-list");
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080050 for (auto item : caList) {
51 CaProfile caItem;
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080052 caItem = CaProfile::fromJson(item.second);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080053 if (caItem.cert == nullptr) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080054 NDN_THROW(std::runtime_error("No CA certificate is loaded from JSON configuration."));
55 }
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080056 m_caProfiles.push_back(std::move(caItem));
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080057 }
58}
59
60void
61ProfileStorage::save(const std::string& fileName) const
62{
63 JsonSection configJson;
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080064 for (const auto& caItem : m_caProfiles) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080065 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
75void
76ProfileStorage::removeCaProfile(const Name& caName)
77{
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080078 m_caProfiles.remove_if([&](const CaProfile& item) { return item.caPrefix == caName; });
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080079}
80
81void
82ProfileStorage::addCaProfile(const CaProfile& profile)
83{
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080084 for (auto& item : m_caProfiles) {
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080085 if (item.caPrefix == profile.caPrefix) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080086 item = profile;
87 return;
88 }
89 }
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080090 m_caProfiles.push_back(profile);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080091}
92
93const std::list<CaProfile>&
Zhiyi Zhang84e11842020-11-19 20:03:23 -080094ProfileStorage::getKnownProfiles() const
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080095{
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080096 return m_caProfiles;
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080097}
98
99} // namespace requester
100} // namespace ndncert