blob: 949309a5d4aa2240b78e805ec3f8ef219954360d [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
26namespace ndn {
27namespace ndncert {
28namespace requester {
29
30void
31ProfileStorage::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
46void
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080047ProfileStorage::load(const JsonSection& json)
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080048{
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080049 m_caProfiles.clear();
50 auto caList = json.get_child("ca-list");
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080051 for (auto item : caList) {
52 CaProfile caItem;
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080053 caItem = CaProfile::fromJson(item.second);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080054 if (caItem.cert == nullptr) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080055 NDN_THROW(std::runtime_error("No CA certificate is loaded from JSON configuration."));
56 }
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080057 m_caProfiles.push_back(std::move(caItem));
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080058 }
59}
60
61void
62ProfileStorage::save(const std::string& fileName) const
63{
64 JsonSection configJson;
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080065 for (const auto& caItem : m_caProfiles) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080066 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
76void
77ProfileStorage::removeCaProfile(const Name& caName)
78{
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080079 m_caProfiles.remove_if([&](const CaProfile& item) { return item.caPrefix == caName; });
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080080}
81
82void
83ProfileStorage::addCaProfile(const CaProfile& profile)
84{
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080085 for (auto& item : m_caProfiles) {
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080086 if (item.caPrefix == profile.caPrefix) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080087 item = profile;
88 return;
89 }
90 }
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080091 m_caProfiles.push_back(profile);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080092}
93
94const std::list<CaProfile>&
Zhiyi Zhang84e11842020-11-19 20:03:23 -080095ProfileStorage::getKnownProfiles() const
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080096{
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080097 return m_caProfiles;
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080098}
99
100} // namespace requester
101} // namespace ndncert
102} // namespace ndn