NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019 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 #include <boost/filesystem.hpp>
27 #include <boost/range/adaptor/map.hpp>
28 #include <boost/range/algorithm/copy.hpp>
29 #include <boost/range/iterator_range.hpp>
30 
31 namespace ndn {
32 namespace security {
33 namespace v2 {
34 
36 
37 namespace fs = boost::filesystem;
38 
39 TrustAnchorGroup::TrustAnchorGroup(CertContainerInterface& certContainer, const std::string& id)
40  : m_certs(certContainer)
41  , m_id(id)
42 {
43 }
44 
46 
47 size_t
49 {
50  return m_anchorNames.size();
51 }
52 
53 void
55 {
56  // base method does nothing
57 }
58 
60 
62  : TrustAnchorGroup(certContainer, id)
63 {
64 }
65 
66 void
68 {
69  if (m_anchorNames.count(cert.getName()) != 0) {
70  return;
71  }
72 
73  m_anchorNames.insert(cert.getName());
74  m_certs.add(std::move(cert));
75 }
76 
77 void
79 {
80  m_anchorNames.erase(certName);
81  m_certs.remove(certName);
82 }
83 
85 
87  const boost::filesystem::path& path,
88  time::nanoseconds refreshPeriod, bool isDir)
89  : TrustAnchorGroup(certContainer, id)
90  , m_isDir(isDir)
91  , m_path(path)
92  , m_refreshPeriod(refreshPeriod)
93 {
94  if (refreshPeriod <= time::nanoseconds::zero()) {
95  NDN_THROW(std::runtime_error("Refresh period for the dynamic group must be positive"));
96  }
97 
98  NDN_LOG_TRACE("Create dynamic trust anchor group " << id << " for file/dir " << path
99  << " with refresh time " << refreshPeriod);
100  refresh();
101 }
102 
103 void
105 {
106  if (m_expireTime > time::steady_clock::now()) {
107  return;
108  }
109  m_expireTime = time::steady_clock::now() + m_refreshPeriod;
110  NDN_LOG_TRACE("Reloading dynamic trust anchor group");
111 
112  std::set<Name> oldAnchorNames = m_anchorNames;
113 
114  auto loadCert = [this, &oldAnchorNames] (const fs::path& file) {
115  auto cert = io::load<Certificate>(file.string());
116  if (cert != nullptr) {
117  if (m_anchorNames.count(cert->getName()) == 0) {
118  m_anchorNames.insert(cert->getName());
119  m_certs.add(std::move(*cert));
120  }
121  else {
122  oldAnchorNames.erase(cert->getName());
123  }
124  }
125  };
126 
127  if (!m_isDir) {
128  loadCert(m_path);
129  }
130  else {
131  if (fs::exists(m_path)) {
132  std::for_each(fs::directory_iterator(m_path), fs::directory_iterator(), loadCert);
133  }
134  }
135 
136  // remove old certs
137  for (const auto& oldAnchorName : oldAnchorNames) {
138  m_anchorNames.erase(oldAnchorName);
139  m_certs.remove(oldAnchorName);
140  }
141 }
142 
143 } // namespace v2
144 } // namespace security
145 } // namespace ndn
NDN_LOG_INIT
#define NDN_LOG_INIT(name)
declare a log module
Definition: logger.hpp:81
ndn::security::v2::CertContainerInterface
Definition: trust-anchor-group.hpp:36
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
ndn::security::v2::TrustAnchorGroup::TrustAnchorGroup
TrustAnchorGroup(CertContainerInterface &certContainer, const std::string &id)
Create an anchor group.
Definition: trust-anchor-group.cpp:39
ndn::time::steady_clock::now
static time_point now() noexcept
Definition: time.cpp:80
ndn::security::v2::StaticTrustAnchorGroup::remove
void remove(const Name &certName)
Remove static anchor certName.
Definition: trust-anchor-group.cpp:78
ndn::security::v2::TrustAnchorGroup
A group of trust anchors.
Definition: trust-anchor-group.hpp:52
ndn::security::v2::TrustAnchorGroup::size
size_t size() const
Definition: trust-anchor-group.cpp:48
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
io.hpp
ndn::security::v2::Certificate
The certificate following the certificate format naming convention.
Definition: certificate.hpp:82
ndn::security::v2::StaticTrustAnchorGroup::StaticTrustAnchorGroup
StaticTrustAnchorGroup(CertContainerInterface &certContainer, const std::string &id)
Create a static trust anchor group.
Definition: trust-anchor-group.cpp:61
ndn::security::v2::StaticTrustAnchorGroup::add
void add(Certificate &&cert)
Load static anchor cert.
Definition: trust-anchor-group.cpp:67
ndn::security::v2::TrustAnchorGroup::m_anchorNames
std::set< Name > m_anchorNames
Definition: trust-anchor-group.hpp:85
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::security::v2::TrustAnchorGroup::m_certs
CertContainerInterface & m_certs
Definition: trust-anchor-group.hpp:84
logger.hpp
ndn::security::v2::TrustAnchorGroup::refresh
virtual void refresh()
Request certificate refresh.
Definition: trust-anchor-group.cpp:54
ndn::security::v2::CertContainerInterface::remove
virtual void remove(const Name &certName)=0
ndn::security::v2::DynamicTrustAnchorGroup::refresh
void refresh() override
Request certificate refresh.
Definition: trust-anchor-group.cpp:104
ndn::security
Definition: dummy-keychain.cpp:28
ndn::security::v2::TrustAnchorGroup::~TrustAnchorGroup
virtual ~TrustAnchorGroup()
ndn::security::v2
Definition: command-authenticator.hpp:35
NDN_LOG_TRACE
#define NDN_LOG_TRACE(expression)
Definition: logger.hpp:98
trust-anchor-group.hpp
ndn::security::v2::CertContainerInterface::add
virtual void add(Certificate &&cert)=0
ndn::security::v2::DynamicTrustAnchorGroup::DynamicTrustAnchorGroup
DynamicTrustAnchorGroup(CertContainerInterface &certContainer, const std::string &id, const boost::filesystem::path &path, time::nanoseconds refreshPeriod, bool isDir=false)
Create a dynamic trust anchor group.
Definition: trust-anchor-group.cpp:86
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34