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-2023 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"
24 #include "ndn-cxx/util/logger.hpp"
25 
26 #if BOOST_VERSION >= 107200
27 #include <boost/filesystem/directory.hpp>
28 #endif
29 #include <boost/filesystem/operations.hpp>
30 #include <boost/range/adaptor/map.hpp>
31 #include <boost/range/algorithm/copy.hpp>
32 #include <boost/range/iterator_range.hpp>
33 
34 namespace ndn::security {
35 
37 
38 namespace fs = boost::filesystem;
39 
40 TrustAnchorGroup::TrustAnchorGroup(CertContainerInterface& certContainer, const std::string& id)
41  : m_certs(certContainer)
42  , m_id(id)
43 {
44 }
45 
47 
48 size_t
50 {
51  return m_anchorNames.size();
52 }
53 
54 void
56 {
57  // base method does nothing
58 }
59 
61 
63  : TrustAnchorGroup(certContainer, id)
64 {
65 }
66 
67 void
69 {
70  if (m_anchorNames.count(cert.getName()) != 0) {
71  return;
72  }
73 
74  m_anchorNames.insert(cert.getName());
75  m_certs.add(std::move(cert));
76 }
77 
78 void
80 {
81  m_anchorNames.erase(certName);
82  m_certs.remove(certName);
83 }
84 
86 
88  const boost::filesystem::path& path,
89  time::nanoseconds refreshPeriod, bool isDir)
90  : TrustAnchorGroup(certContainer, id)
91  , m_isDir(isDir)
92  , m_path(path)
93  , m_refreshPeriod(refreshPeriod)
94 {
95  if (refreshPeriod <= time::nanoseconds::zero()) {
96  NDN_THROW(std::runtime_error("Refresh period for the dynamic group must be positive"));
97  }
98 
99  NDN_LOG_TRACE("Create dynamic trust anchor group " << id << " for file/dir " << path
100  << " with refresh time " << refreshPeriod);
101  refresh();
102 }
103 
104 void
106 {
107  if (m_expireTime > time::steady_clock::now()) {
108  return;
109  }
110  m_expireTime = time::steady_clock::now() + m_refreshPeriod;
111  NDN_LOG_TRACE("Reloading dynamic trust anchor group");
112 
113  std::set<Name> oldAnchorNames = m_anchorNames;
114 
115  auto loadCert = [this, &oldAnchorNames] (const fs::path& file) {
116  auto cert = io::load<Certificate>(file.string());
117  if (cert != nullptr) {
118  if (m_anchorNames.count(cert->getName()) == 0) {
119  m_anchorNames.insert(cert->getName());
120  m_certs.add(std::move(*cert));
121  }
122  else {
123  oldAnchorNames.erase(cert->getName());
124  }
125  }
126  };
127 
128  if (!m_isDir) {
129  loadCert(m_path);
130  }
131  else if (fs::exists(m_path)) {
132  std::for_each(fs::directory_iterator(m_path), fs::directory_iterator(), loadCert);
133  }
134 
135  // remove old certs
136  for (const auto& oldAnchorName : oldAnchorNames) {
137  m_anchorNames.erase(oldAnchorName);
138  m_certs.remove(oldAnchorName);
139  }
140 }
141 
142 } // 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.
Definition: certificate.hpp:58
DynamicTrustAnchorGroup(CertContainerInterface &certContainer, const std::string &id, const boost::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.
CertContainerInterface & m_certs
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