trust-anchor-container.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2020 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 
24 #include <boost/filesystem.hpp>
25 
26 namespace ndn {
27 namespace security {
28 inline namespace v2 {
29 
30 void
31 TrustAnchorContainer::AnchorContainer::add(Certificate&& cert)
32 {
33  AnchorContainerBase::insert(std::move(cert));
34 }
35 
36 void
37 TrustAnchorContainer::AnchorContainer::remove(const Name& certName)
38 {
39  AnchorContainerBase::erase(certName);
40 }
41 
42 void
43 TrustAnchorContainer::AnchorContainer::clear()
44 {
45  AnchorContainerBase::clear();
46 }
47 
48 void
49 TrustAnchorContainer::insert(const std::string& groupId, Certificate&& cert)
50 {
51  auto group = m_groups.find(groupId);
52  if (group == m_groups.end()) {
53  std::tie(group, std::ignore) = m_groups.insert(std::make_shared<StaticTrustAnchorGroup>(m_anchors,
54  groupId));
55  }
56  auto* staticGroup = dynamic_cast<StaticTrustAnchorGroup*>(&**group);
57  if (staticGroup == nullptr) {
58  NDN_THROW(Error("Cannot add static anchor to a non-static anchor group " + groupId));
59  }
60  staticGroup->add(std::move(cert));
61 }
62 
63 void
64 TrustAnchorContainer::insert(const std::string& groupId, const boost::filesystem::path& path,
65  time::nanoseconds refreshPeriod, bool isDir)
66 {
67  if (m_groups.count(groupId) != 0) {
68  NDN_THROW(Error("Cannot create dynamic group, because group " + groupId + " already exists"));
69  }
70 
71  m_groups.insert(std::make_shared<DynamicTrustAnchorGroup>(m_anchors, groupId, path,
72  refreshPeriod, isDir));
73 }
74 
75 void
77 {
78  m_groups.clear();
79  m_anchors.clear();
80 }
81 
82 const Certificate*
83 TrustAnchorContainer::find(const Name& keyName) const
84 {
85  const_cast<TrustAnchorContainer*>(this)->refresh();
86 
87  auto cert = m_anchors.lower_bound(keyName);
88  if (cert == m_anchors.end() || !keyName.isPrefixOf(cert->getName()))
89  return nullptr;
90 
91  return &*cert;
92 }
93 
94 const Certificate*
95 TrustAnchorContainer::find(const Interest& interest) const
96 {
97  const_cast<TrustAnchorContainer*>(this)->refresh();
98 
99  for (auto cert = m_anchors.lower_bound(interest.getName());
100  cert != m_anchors.end() && interest.getName().isPrefixOf(cert->getName());
101  ++cert) {
102  if (interest.matchesData(*cert)) {
103  return &*cert;
104  }
105  }
106  return nullptr;
107 }
108 
110 TrustAnchorContainer::getGroup(const std::string& groupId) const
111 {
112  auto group = m_groups.find(groupId);
113  if (group == m_groups.end()) {
114  NDN_THROW(Error("Trust anchor group " + groupId + " does not exist"));
115  }
116  return **group;
117 }
118 
119 size_t
121 {
122  return m_anchors.size();
123 }
124 
125 void
126 TrustAnchorContainer::refresh()
127 {
128  for (auto it = m_groups.begin(); it != m_groups.end(); ++it) {
129  m_groups.modify(it, [] (const auto& group) { group->refresh(); });
130  }
131 }
132 
133 } // inline namespace v2
134 } // namespace security
135 } // namespace ndn
Definition: data.cpp:26
The certificate following the certificate format naming convention.
Definition: certificate.hpp:81
void insert(const std::string &groupId, Certificate &&cert)
Insert a static trust anchor.
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:299
size_t size() const
Get number of trust anchors across all groups.
Represents an Interest packet.
Definition: interest.hpp:50
#define NDN_THROW(e)
Definition: exception.hpp:61
represents a container for trust anchors.
const Certificate * find(const Name &keyName) const
Search for certificate across all groups (longest prefix match)
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:333
Represents an absolute name.
Definition: name.hpp:44
TrustAnchorGroup & getGroup(const std::string &groupId) const
Get trusted anchor group.
const Name & getName() const noexcept
Definition: interest.hpp:174
void clear()
Remove all static or dynamic anchors.