NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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-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/logger.hpp"
24 
25 namespace ndn {
26 namespace security {
27 namespace v2 {
28 
30 
31 time::nanoseconds
33 {
34  return 1_h;
35 }
36 
37 CertificateCache::CertificateCache(const time::nanoseconds& maxLifetime)
38  : m_certsByTime(m_certs.get<0>())
39  , m_certsByName(m_certs.get<1>())
40  , m_maxLifetime(maxLifetime)
41 {
42 }
43 
44 void
46 {
47  time::system_clock::TimePoint notAfterTime = cert.getValidityPeriod().getPeriod().second;
49  if (notAfterTime < now) {
50  NDN_LOG_DEBUG("Not adding " << cert.getName() << ": already expired at " << time::toIsoString(notAfterTime));
51  return;
52  }
53 
54  time::system_clock::TimePoint removalTime = std::min(notAfterTime, now + m_maxLifetime);
55  NDN_LOG_DEBUG("Adding " << cert.getName() << ", will remove in "
56  << time::duration_cast<time::seconds>(removalTime - now));
57  m_certs.insert(Entry(cert, removalTime));
58 }
59 
60 void
62 {
63  m_certs.clear();
64 }
65 
66 const Certificate*
67 CertificateCache::find(const Name& certPrefix) const
68 {
69  const_cast<CertificateCache*>(this)->refresh();
70  if (certPrefix.size() > 0 && certPrefix[-1].isImplicitSha256Digest()) {
71  NDN_LOG_INFO("Certificate search using name with the implicit digest is not yet supported");
72  }
73  auto itr = m_certsByName.lower_bound(certPrefix);
74  if (itr == m_certsByName.end() || !certPrefix.isPrefixOf(itr->getCertName()))
75  return nullptr;
76  return &itr->cert;
77 }
78 
79 const Certificate*
80 CertificateCache::find(const Interest& interest) const
81 {
82  if (interest.getName().size() > 0 && interest.getName()[-1].isImplicitSha256Digest()) {
83  NDN_LOG_INFO("Certificate search using name with implicit digest is not yet supported");
84  }
85  const_cast<CertificateCache*>(this)->refresh();
86 
87  for (auto i = m_certsByName.lower_bound(interest.getName());
88  i != m_certsByName.end() && interest.getName().isPrefixOf(i->getCertName());
89  ++i) {
90  const auto& cert = i->cert;
91  if (interest.matchesData(cert)) {
92  return &cert;
93  }
94  }
95  return nullptr;
96 }
97 
98 void
99 CertificateCache::refresh()
100 {
102 
103  auto cIt = m_certsByTime.begin();
104  while (cIt != m_certsByTime.end() && cIt->removalTime < now) {
105  m_certsByTime.erase(cIt);
106  cIt = m_certsByTime.begin();
107  }
108 }
109 
110 } // namespace v2
111 } // namespace security
112 } // namespace ndn
NDN_LOG_INIT
#define NDN_LOG_INIT(name)
declare a log module
Definition: logger.hpp:81
ndn::time::system_clock::TimePoint
time_point TimePoint
Definition: time.hpp:195
nonstd::variants::get
R & get(variant< T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 > &v, nonstd_lite_in_place_type_t(R)=nonstd_lite_in_place_type(R))
Definition: variant.hpp:1753
ndn::Name::isPrefixOf
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:299
NDN_LOG_INFO
#define NDN_LOG_INFO(expression)
Definition: logger.hpp:100
ndn::Name::size
size_t size() const
Returns the number of components.
Definition: name.hpp:153
ndn::security::v2::CertificateCache::clear
void clear()
Remove all certificates from cache.
Definition: certificate-cache.cpp:61
ndn::time::system_clock::now
static time_point now() noexcept
Definition: time.cpp:46
ndn::security::ValidityPeriod::getPeriod
std::pair< time::system_clock::TimePoint, time::system_clock::TimePoint > getPeriod() const
Get the stored validity period.
Definition: validity-period.cpp:141
ndn::Data::getName
const Name & getName() const
Get name.
Definition: data.hpp:124
ndn::security::v2::CertificateCache::getDefaultLifetime
static time::nanoseconds getDefaultLifetime()
Definition: certificate-cache.cpp:32
ndn::security::v2::CertificateCache::CertificateCache
CertificateCache(const time::nanoseconds &maxLifetime=getDefaultLifetime())
Create an object for certificate cache.
Definition: certificate-cache.cpp:37
ndn::security::v2::CertificateCache
Represents a container for verified certificates.
Definition: certificate-cache.hpp:44
ndn::security::v2::CertificateCache::insert
void insert(const Certificate &cert)
Insert certificate into cache.
Definition: certificate-cache.cpp:45
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::security::v2::Certificate
The certificate following the certificate format naming convention.
Definition: certificate.hpp:82
ndn::time::toIsoString
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
Definition: time.cpp:145
ndn::security::v2::Certificate::getValidityPeriod
ValidityPeriod getValidityPeriod() const
Get validity period of the certificate.
Definition: certificate.cpp:113
ndn::Interest
Represents an Interest packet.
Definition: interest.hpp:44
logger.hpp
certificate-cache.hpp
ndn::Interest::matchesData
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:333
NDN_LOG_DEBUG
#define NDN_LOG_DEBUG(expression)
Definition: logger.hpp:99
ndn::security
Definition: dummy-keychain.cpp:28
ndn::security::v2::CertificateCache::find
const Certificate * find(const Name &certPrefix) const
Get certificate given key name.
Definition: certificate-cache.cpp:67
ndn::Interest::getName
const Name & getName() const noexcept
Definition: interest.hpp:121
ndn::security::v2
Definition: command-authenticator.hpp:35
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34