blob: 7ae3c0891608031a823e52d362c2ecfe0cfaa42b [file] [log] [blame]
Zhiyi Zhang3f20f952020-11-19 19:26:43 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2017-2020, Regents of the University of California.
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"
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080022#include <boost/filesystem.hpp>
23
24namespace ndn {
25namespace ndncert {
26namespace requester {
27
28void
29ProfileStorage::load(const std::string& fileName)
30{
31 JsonSection configJson;
32 try {
33 boost::property_tree::read_json(fileName, configJson);
34 }
35 catch (const std::exception& error) {
36 NDN_THROW(std::runtime_error("Failed to parse configuration file " + fileName + ", " + error.what()));
37 }
38 if (configJson.begin() == configJson.end()) {
39 NDN_THROW(std::runtime_error("No JSON configuration found in file: " + fileName));
40 }
41 load(configJson);
42}
43
44void
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080045ProfileStorage::load(const JsonSection& json)
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080046{
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080047 m_caProfiles.clear();
48 auto caList = json.get_child("ca-list");
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080049 for (auto item : caList) {
50 CaProfile caItem;
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080051 caItem = CaProfile::fromJson(item.second);
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080052 if (caItem.cert == nullptr) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080053 NDN_THROW(std::runtime_error("No CA certificate is loaded from JSON configuration."));
54 }
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080055 m_caProfiles.push_back(std::move(caItem));
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080056 }
57}
58
59void
60ProfileStorage::save(const std::string& fileName) const
61{
62 JsonSection configJson;
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080063 for (const auto& caItem : m_caProfiles) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080064 configJson.push_back(std::make_pair("", caItem.toJson()));
65 }
66 std::stringstream ss;
67 boost::property_tree::write_json(ss, configJson);
68 std::ofstream configFile;
69 configFile.open(fileName);
70 configFile << ss.str();
71 configFile.close();
72}
73
74void
75ProfileStorage::removeCaProfile(const Name& caName)
76{
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080077 m_caProfiles.remove_if([&](const CaProfile& item) { return item.caPrefix == caName; });
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080078}
79
80void
81ProfileStorage::addCaProfile(const CaProfile& profile)
82{
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080083 for (auto& item : m_caProfiles) {
Zhiyi Zhang44c6a352020-12-14 10:57:17 -080084 if (item.caPrefix == profile.caPrefix) {
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080085 item = profile;
86 return;
87 }
88 }
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080089 m_caProfiles.push_back(profile);
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080090}
91
92const std::list<CaProfile>&
Zhiyi Zhang84e11842020-11-19 20:03:23 -080093ProfileStorage::getKnownProfiles() const
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080094{
Zhiyi Zhang1e418f22020-11-19 19:49:32 -080095 return m_caProfiles;
Zhiyi Zhang3f20f952020-11-19 19:26:43 -080096}
97
98} // namespace requester
99} // namespace ndncert
100} // namespace ndn