ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
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-2024 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
30namespace ndn::security {
31
33
34TrustAnchorGroup::TrustAnchorGroup(CertContainerInterface& certContainer, const std::string& id)
35 : m_certs(certContainer)
36 , m_id(id)
37{
38}
39
41
42size_t
44{
45 return m_anchorNames.size();
46}
47
48void
50{
51 // base method does nothing
52}
53
55
57 : TrustAnchorGroup(certContainer, id)
58{
59}
60
61void
63{
64 if (m_anchorNames.count(cert.getName()) != 0) {
65 return;
66 }
67
68 m_anchorNames.insert(cert.getName());
69 m_certs.add(std::move(cert));
70}
71
72void
74{
75 m_anchorNames.erase(certName);
76 m_certs.remove(certName);
77}
78
80
82 const std::string& id,
83 const std::filesystem::path& path,
84 time::nanoseconds refreshPeriod, bool isDir)
85 : TrustAnchorGroup(certContainer, id)
86 , m_isDir(isDir)
87 , m_path(path)
88 , m_refreshPeriod(refreshPeriod)
89{
90 if (refreshPeriod <= time::nanoseconds::zero()) {
91 NDN_THROW(std::runtime_error("Refresh period for the dynamic group must be positive"));
92 }
93
94 NDN_LOG_TRACE("Create dynamic trust anchor group " << id << " for file/dir " << path
95 << " with refresh time " << refreshPeriod);
96 refresh();
97}
98
99void
101{
102 if (m_expireTime > time::steady_clock::now()) {
103 return;
104 }
105 m_expireTime = time::steady_clock::now() + m_refreshPeriod;
106 NDN_LOG_TRACE("Reloading dynamic trust anchor group");
107
108 std::set<Name> oldAnchorNames = m_anchorNames;
109
110 auto loadCert = [this, &oldAnchorNames] (const std::filesystem::path& file) {
111 auto cert = io::load<Certificate>(file);
112 if (cert != nullptr) {
113 if (m_anchorNames.count(cert->getName()) == 0) {
114 m_anchorNames.insert(cert->getName());
115 m_certs.add(std::move(*cert));
116 }
117 else {
118 oldAnchorNames.erase(cert->getName());
119 }
120 }
121 };
122
123 if (!m_isDir) {
124 loadCert(m_path);
125 }
126 else if (std::filesystem::exists(m_path)) {
127 for (const auto& entry : std::filesystem::directory_iterator(m_path)) {
128 loadCert(entry);
129 }
130 }
131
132 // remove old certs
133 for (const auto& oldAnchorName : oldAnchorNames) {
134 m_anchorNames.erase(oldAnchorName);
135 m_certs.remove(oldAnchorName);
136 }
137}
138
139} // namespace ndn::security
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
Contains the ndn-cxx security framework.
::boost::chrono::nanoseconds nanoseconds
Definition time.hpp:54
Definition data.cpp:25