blob: ebd33ac2ba4eeb42a90807777f47f1d12ad1ded7 [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/*
Davide Pesavento0d1d11c2022-04-11 22:11:34 -04003 * Copyright (c) 2017-2022, 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
Davide Pesaventoe5b43692021-11-15 22:05:03 -050023#include <boost/property_tree/json_parser.hpp>
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080024
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040025namespace ndncert::requester {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080026
27void
28ProfileStorage::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
43void
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080044ProfileStorage::load(const JsonSection& json)
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080045{
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080046 m_caProfiles.clear();
47 auto caList = json.get_child("ca-list");
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040048 for (const auto& item : caList) {
49 auto profile = CaProfile::fromJson(item.second);
50 if (profile.cert == nullptr) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080051 NDN_THROW(std::runtime_error("No CA certificate is loaded from JSON configuration."));
52 }
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040053 m_caProfiles.push_back(std::move(profile));
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080054 }
55}
56
57void
58ProfileStorage::save(const std::string& fileName) const
59{
60 JsonSection configJson;
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080061 for (const auto& caItem : m_caProfiles) {
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040062 configJson.push_back({"", caItem.toJson()});
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080063 }
64 std::stringstream ss;
65 boost::property_tree::write_json(ss, configJson);
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040066 std::ofstream configFile(fileName);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080067 configFile << ss.str();
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080068}
69
70void
71ProfileStorage::removeCaProfile(const Name& caName)
72{
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080073 m_caProfiles.remove_if([&](const CaProfile& item) { return item.caPrefix == caName; });
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080074}
75
76void
77ProfileStorage::addCaProfile(const CaProfile& profile)
78{
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080079 for (auto& item : m_caProfiles) {
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080080 if (item.caPrefix == profile.caPrefix) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080081 item = profile;
82 return;
83 }
84 }
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080085 m_caProfiles.push_back(profile);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080086}
87
88const std::list<CaProfile>&
Zhiyi Zhang84e11842020-11-19 20:03:23 -080089ProfileStorage::getKnownProfiles() const
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080090{
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080091 return m_caProfiles;
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080092}
93
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040094} // namespace ndncert::requester