ndn-cxx 0.9.0-42-g8cff8f06
Loading...
Searching...
No Matches
trust-anchor-group.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2026 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library 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 Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
23#include "ndn-cxx/util/io.hpp"
25
26#include <boost/range/adaptor/map.hpp>
27#include <boost/range/algorithm/copy.hpp>
28#include <boost/range/iterator_range.hpp>
29
30#include <fstream>
31
32namespace ndn::security {
33
35
36TrustAnchorGroup::TrustAnchorGroup(CertContainerInterface& certContainer, const std::string& id)
37 : m_certs(certContainer)
38 , m_id(id)
39{
40}
41
43
44size_t
46{
47 return m_anchorNames.size();
48}
49
50void
52{
53 // base method does nothing
54}
55
57
59 : TrustAnchorGroup(certContainer, id)
60{
61}
62
63void
65{
66 if (m_anchorNames.count(cert.getName()) != 0) {
67 return;
68 }
69
70 m_anchorNames.insert(cert.getName());
71 m_certs.add(std::move(cert));
72}
73
74void
76{
77 m_anchorNames.erase(certName);
78 m_certs.remove(certName);
79}
80
82
84 const std::string& id,
85 const std::filesystem::path& path,
86 time::nanoseconds refreshPeriod, bool isDir)
87 : TrustAnchorGroup(certContainer, id)
88 , m_isDir(isDir)
89 , m_path(path)
90 , m_refreshPeriod(refreshPeriod)
91{
92 if (refreshPeriod <= time::nanoseconds::zero()) {
93 NDN_THROW(std::runtime_error("Refresh period for the dynamic group must be positive"));
94 }
95
96 NDN_LOG_TRACE("Create dynamic trust anchor group " << id << " for file/dir " << path
97 << " with refresh time " << refreshPeriod);
98 refresh();
99}
100
101void
103{
104 if (m_expireTime > time::steady_clock::now()) {
105 return;
106 }
107 m_expireTime = time::steady_clock::now() + m_refreshPeriod;
108 NDN_LOG_TRACE("Reloading dynamic trust anchor group");
109
110 std::set<Name> oldAnchorNames = m_anchorNames;
111
112 auto loadCert = [this, &oldAnchorNames] (const std::filesystem::path& filename) {
113 std::ifstream file(filename);
114 if (!file)
115 return;
116
117 Certificate cert;
118 try {
119 cert = io::loadTlv<Certificate>(file, io::BASE64);
120 }
121 catch (const io::Error&) {
122 // ignore
123 return;
124 }
125
126 if (m_anchorNames.count(cert.getName()) == 0) {
127 m_anchorNames.insert(cert.getName());
128 m_certs.add(std::move(cert));
129 }
130 else {
131 oldAnchorNames.erase(cert.getName());
132 }
133 };
134
135 if (!m_isDir) {
136 loadCert(m_path);
137 }
138 else if (std::filesystem::exists(m_path)) {
139 for (const auto& entry : std::filesystem::directory_iterator(m_path)) {
140 loadCert(entry);
141 }
142 }
143
144 // remove old certs
145 for (const auto& oldAnchorName : oldAnchorNames) {
146 m_anchorNames.erase(oldAnchorName);
147 m_certs.remove(oldAnchorName);
148 }
149}
150
151} // namespace ndn::security
const Name & getName() const noexcept
Get the Data name.
Definition data.hpp:137
Represents an absolute name.
Definition name.hpp:45
virtual void remove(const Name &certName)=0
virtual void add(Certificate &&cert)=0
Represents an NDN certificate.
DynamicTrustAnchorGroup(CertContainerInterface &certContainer, const std::string &id, const std::filesystem::path &path, time::nanoseconds refreshPeriod, bool isDir=false)
Create a dynamic trust anchor group.
void refresh() override
Request certificate refresh.
void add(Certificate &&cert)
Load static anchor cert.
void remove(const Name &certName)
Remove static anchor certName.
StaticTrustAnchorGroup(CertContainerInterface &certContainer, const std::string &id)
Create a static trust anchor group.
TrustAnchorGroup(CertContainerInterface &certContainer, const std::string &id)
Create an anchor group.
virtual void refresh()
Request certificate refresh.
static time_point now() noexcept
Definition time.cpp:79
#define NDN_THROW(e)
Definition exception.hpp:56
#define NDN_LOG_TRACE(expression)
Log at TRACE level.
Definition logger.hpp:255
#define NDN_LOG_INIT(name)
Define a non-member log module.
Definition logger.hpp:169
@ BASE64
Base64 encoding.
Definition io.hpp:53
Contains the ndn-cxx security framework.
::boost::chrono::nanoseconds nanoseconds
Definition time.hpp:54
Definition data.cpp:25