trust-anchor-container.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
23 
24 #include <boost/filesystem.hpp>
25 
26 namespace ndn {
27 namespace security {
28 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::insert(const std::string& groupId, Certificate&& cert)
44 {
45  auto group = m_groups.find(groupId);
46  if (group == m_groups.end()) {
47  std::tie(group, std::ignore) = m_groups.insert(make_shared<StaticTrustAnchorGroup>(m_anchors, groupId));
48  }
49  auto* staticGroup = dynamic_cast<StaticTrustAnchorGroup*>(&**group);
50  if (staticGroup == nullptr) {
51  BOOST_THROW_EXCEPTION(Error("Cannot add static anchor to a non-static anchor group " + groupId));
52  }
53  staticGroup->add(std::move(cert));
54 }
55 
56 void
57 TrustAnchorContainer::insert(const std::string& groupId, const boost::filesystem::path& path,
58  time::nanoseconds refreshPeriod, bool isDir)
59 {
60  if (m_groups.count(groupId) != 0) {
61  BOOST_THROW_EXCEPTION(Error("Cannot create dynamic group, because group " + groupId + " already exists"));
62  }
63 
64  m_groups.insert(make_shared<DynamicTrustAnchorGroup>(m_anchors, groupId, path, refreshPeriod, isDir));
65 }
66 
67 const Certificate*
68 TrustAnchorContainer::find(const Name& keyName) const
69 {
70  const_cast<TrustAnchorContainer*>(this)->refresh();
71 
72  auto cert = m_anchors.lower_bound(keyName);
73  if (cert == m_anchors.end() || !keyName.isPrefixOf(cert->getName()))
74  return nullptr;
75  return &*cert;
76 }
77 
78 const Certificate*
79 TrustAnchorContainer::find(const Interest& interest) const
80 {
81  const_cast<TrustAnchorContainer*>(this)->refresh();
82 
83  for (auto cert = m_anchors.lower_bound(interest.getName());
84  cert != m_anchors.end() && interest.getName().isPrefixOf(cert->getName());
85  ++cert) {
86  if (interest.matchesData(*cert)) {
87  return &*cert;
88  }
89  }
90  return nullptr;
91 }
92 
94 TrustAnchorContainer::getGroup(const std::string& groupId) const
95 {
96  auto group = m_groups.find(groupId);
97  if (group == m_groups.end()) {
98  BOOST_THROW_EXCEPTION(Error("Trust anchor group " + groupId + " does not exist"));
99  }
100  return **group;
101 }
102 
103 size_t
105 {
106  return m_anchors.size();
107 }
108 
109 void
110 TrustAnchorContainer::refresh()
111 {
112  for (auto it = m_groups.begin(); it != m_groups.end(); ++it) {
113  m_groups.modify(it, [] (shared_ptr<TrustAnchorGroup>& group) { group->refresh(); });
114  }
115 }
116 
117 } // namespace v2
118 } // namespace security
119 } // namespace ndn
const Name & getName() const
Definition: interest.hpp:226
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
The certificate following the certificate format naming convention.
const Certificate * find(const Name &keyName) const
Search for certificate across all groups (longest prefix match)
size_t size() const
Get number of trust anchors across all groups.
void insert(const std::string &groupId, Certificate &&cert)
Insert a static trust anchor.
represents an Interest packet
Definition: interest.hpp:42
TrustAnchorGroup & getGroup(const std::string &groupId) const
Get trusted anchor group.
represents a container for trust anchors.
Name abstraction to represent an absolute name.
Definition: name.hpp:46
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:132
bool isPrefixOf(const Name &name) const
Check if the N components of this name are the same as the first N components of the given name...
Definition: name.cpp:308