NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
certificate-cache.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2017 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 
22 #include "certificate-cache.hpp"
23 #include "util/logger.hpp"
24 
25 namespace ndn {
26 namespace security {
27 namespace v2 {
28 
30 
31 const time::nanoseconds&
33 {
34  static time::nanoseconds lifetime = time::seconds(3600);
35  return lifetime;
36 }
37 
38 CertificateCache::CertificateCache(const time::nanoseconds& maxLifetime)
39  : m_certsByTime(m_certs.get<0>())
40  , m_certsByName(m_certs.get<1>())
41  , m_maxLifetime(maxLifetime)
42 {
43 }
44 
45 void
47 {
48  time::system_clock::TimePoint notAfterTime = cert.getValidityPeriod().getPeriod().second;
50  if (notAfterTime < now) {
51  NDN_LOG_DEBUG("Not adding " << cert.getName() << ": already expired at " << time::toIsoString(notAfterTime));
52  return;
53  }
54 
55  time::system_clock::TimePoint removalTime = std::min(notAfterTime, now + m_maxLifetime);
56  NDN_LOG_DEBUG("Adding " << cert.getName() << ", will remove in "
57  << time::duration_cast<time::seconds>(removalTime - now));
58  m_certs.insert(Entry(cert, removalTime));
59 }
60 
61 void
63 {
64  m_certs.clear();
65 }
66 
67 const Certificate*
68 CertificateCache::find(const Name& certPrefix) const
69 {
70  const_cast<CertificateCache*>(this)->refresh();
71  if (certPrefix.size() > 0 && certPrefix[-1].isImplicitSha256Digest()) {
72  NDN_LOG_INFO("Certificate search using name with the implicit digest is not yet supported");
73  }
74  auto itr = m_certsByName.lower_bound(certPrefix);
75  if (itr == m_certsByName.end() || !certPrefix.isPrefixOf(itr->getCertName()))
76  return nullptr;
77  return &itr->cert;
78 }
79 
80 const Certificate*
81 CertificateCache::find(const Interest& interest) const
82 {
83  if (interest.getChildSelector() >= 0) {
84  NDN_LOG_DEBUG("Certificate search using ChildSelector is not supported, searching as if selector not specified");
85  }
86  if (interest.getName().size() > 0 && interest.getName()[-1].isImplicitSha256Digest()) {
87  NDN_LOG_INFO("Certificate search using name with implicit digest is not yet supported");
88  }
89  const_cast<CertificateCache*>(this)->refresh();
90 
91  for (auto i = m_certsByName.lower_bound(interest.getName());
92  i != m_certsByName.end() && interest.getName().isPrefixOf(i->getCertName());
93  ++i) {
94  const auto& cert = i->cert;
95  if (interest.matchesData(cert)) {
96  return &cert;
97  }
98  }
99  return nullptr;
100 }
101 
102 void
103 CertificateCache::refresh()
104 {
106 
107  auto cIt = m_certsByTime.begin();
108  while (cIt != m_certsByTime.end() && cIt->removalTime < now) {
109  m_certsByTime.erase(cIt);
110  cIt = m_certsByTime.begin();
111  }
112 }
113 
114 } // namespace v2
115 } // namespace security
116 } // namespace ndn
#define NDN_LOG_INFO(expression)
Definition: logger.hpp:36
const Name & getName() const
Definition: interest.hpp:139
Copyright (c) 2011-2015 Regents of the University of California.
The certificate following the certificate format naming convention.
Definition: certificate.hpp:81
ValidityPeriod getValidityPeriod() const
Get validity period of the certificate.
void clear()
Remove all certificates from cache.
represents an Interest packet
Definition: interest.hpp:42
static time_point now() noexcept
Definition: time.cpp:46
const Certificate * find(const Name &certPrefix) const
Get certificate given key name.
int getChildSelector() const
Definition: interest.hpp:304
Catch-all error for security policy errors that don&#39;t fit in other categories.
Definition: base.hpp:79
Represents a container for verified certificates.
#define NDN_LOG_INIT(name)
declare a log module
Definition: logger.hpp:32
CertificateCache(const time::nanoseconds &maxLifetime=getDefaultLifetime())
Create an object for certificate cache.
#define NDN_LOG_DEBUG(expression)
Definition: logger.hpp:35
std::pair< time::system_clock::TimePoint, time::system_clock::TimePoint > getPeriod() const
Get the stored validity period.
size_t size() const
Get number of components.
Definition: name.hpp:154
Represents an absolute name.
Definition: name.hpp:42
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:260
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:207
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
Definition: time.cpp:131
time_point TimePoint
Definition: time.hpp:90
static const time::nanoseconds & getDefaultLifetime()
const Name & getName() const
Get name.
Definition: data.hpp:121
void insert(const Certificate &cert)
Insert certificate into cache.