certificate-cache.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
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 const Certificate*
62 CertificateCache::find(const Name& certPrefix) const
63 {
64  const_cast<CertificateCache*>(this)->refresh();
65  if (certPrefix.size() > 0 && certPrefix[-1].isImplicitSha256Digest()) {
66  NDN_LOG_INFO("Certificate search using name with the implicit digest is not yet supported");
67  }
68  auto itr = m_certsByName.lower_bound(certPrefix);
69  if (itr == m_certsByName.end() || !certPrefix.isPrefixOf(itr->getCertName()))
70  return nullptr;
71  return &itr->cert;
72 }
73 
74 const Certificate*
75 CertificateCache::find(const Interest& interest) const
76 {
77  if (interest.getChildSelector() >= 0) {
78  NDN_LOG_DEBUG("Certificate search using ChildSelector is not supported, searching as if selector not specified");
79  }
80  if (interest.getName().size() > 0 && interest.getName()[-1].isImplicitSha256Digest()) {
81  NDN_LOG_INFO("Certificate search using name with implicit digest is not yet supported");
82  }
83  const_cast<CertificateCache*>(this)->refresh();
84 
85  for (auto i = m_certsByName.lower_bound(interest.getName());
86  i != m_certsByName.end() && interest.getName().isPrefixOf(i->getCertName());
87  ++i) {
88  const auto& cert = i->cert;
89  if (interest.matchesData(cert)) {
90  return &cert;
91  }
92  }
93  return nullptr;
94 }
95 
96 void
97 CertificateCache::refresh()
98 {
100 
101  auto cIt = m_certsByTime.begin();
102  while (cIt != m_certsByTime.end() && cIt->removalTime < now) {
103  m_certsByTime.erase(cIt);
104  cIt = m_certsByTime.begin();
105  }
106 }
107 
108 } // namespace v2
109 } // namespace security
110 } // 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.
ValidityPeriod getValidityPeriod() const
Get validity period of the certificate.
represents an Interest packet
Definition: interest.hpp:42
#define NDN_LOG_DEBUG(expression)
log at DEBUG level
Definition: logger.hpp:146
#define NDN_LOG_INIT(name)
declare a log module
Definition: logger.hpp:97
static time_point now() noexcept
Definition: time.cpp:45
const Certificate * find(const Name &certPrefix) const
Get certificate given key name.
int getChildSelector() const
Definition: interest.hpp:367
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:318
Represents a container for verified certificates.
CertificateCache(const time::nanoseconds &maxLifetime=getDefaultLifetime())
Create an object for certificate cache.
std::pair< time::system_clock::TimePoint, time::system_clock::TimePoint > getPeriod() const
Get the stored validity period.
size_t size() const
Get the number of components.
Definition: name.hpp:400
Name abstraction to represent an absolute name.
Definition: name.hpp:46
#define NDN_LOG_INFO(expression)
log at INFO level
Definition: logger.hpp:151
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:132
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
Definition: time.cpp:130
time_point TimePoint
Definition: time.hpp:90
static const time::nanoseconds & getDefaultLifetime()
void insert(const Certificate &cert)
Insert certificate into cache.
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