ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
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-2023 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
24
25namespace ndn::security {
26
28
30 : m_certsByTime(m_certs.get<0>())
31 , m_certsByName(m_certs.get<1>())
32 , m_maxLifetime(maxLifetime)
33{
34}
35
36void
38{
39 auto notAfterTime = cert.getValidityPeriod().getPeriod().second;
40 auto now = time::system_clock::now();
41 if (notAfterTime < now) {
42 NDN_LOG_DEBUG("Not adding " << cert.getName() << ": already expired at " << time::toIsoString(notAfterTime));
43 return;
44 }
45
46 auto removalTime = std::min(notAfterTime, now + m_maxLifetime);
47 NDN_LOG_DEBUG("Adding " << cert.getName() << ", will remove in "
48 << time::duration_cast<time::seconds>(removalTime - now));
49 m_certs.insert(Entry(cert, removalTime));
50}
51
52void
54{
55 m_certs.clear();
56}
57
58const Certificate*
59CertificateCache::find(const Name& certPrefix) const
60{
61 const_cast<CertificateCache*>(this)->refresh();
62 if (certPrefix.size() > 0 && certPrefix[-1].isImplicitSha256Digest()) {
63 NDN_LOG_INFO("Certificate search using name with the implicit digest is not yet supported");
64 }
65 auto itr = m_certsByName.lower_bound(certPrefix);
66 if (itr == m_certsByName.end() || !certPrefix.isPrefixOf(itr->getCertName()))
67 return nullptr;
68 return &itr->cert;
69}
70
71const Certificate*
72CertificateCache::find(const Interest& interest) const
73{
74 if (interest.getName().size() > 0 && interest.getName()[-1].isImplicitSha256Digest()) {
75 NDN_LOG_INFO("Certificate search using name with implicit digest is not yet supported");
76 }
77 const_cast<CertificateCache*>(this)->refresh();
78
79 for (auto i = m_certsByName.lower_bound(interest.getName());
80 i != m_certsByName.end() && interest.getName().isPrefixOf(i->getCertName());
81 ++i) {
82 const auto& cert = i->cert;
83 if (interest.matchesData(cert)) {
84 return &cert;
85 }
86 }
87 return nullptr;
88}
89
90void
91CertificateCache::refresh()
92{
93 auto now = time::system_clock::now();
94
95 auto cIt = m_certsByTime.begin();
96 while (cIt != m_certsByTime.end() && cIt->removalTime < now) {
97 m_certsByTime.erase(cIt);
98 cIt = m_certsByTime.begin();
99 }
100}
101
102} // namespace ndn::security
const Name & getName() const noexcept
Get the Data name.
Definition data.hpp:137
Represents an Interest packet.
Definition interest.hpp:50
bool matchesData(const Data &data) const
Check if this Interest can be satisfied by data.
Definition interest.cpp:319
const Name & getName() const noexcept
Get the Interest name.
Definition interest.hpp:179
Represents an absolute name.
Definition name.hpp:45
size_t size() const noexcept
Returns the number of components.
Definition name.hpp:180
bool isPrefixOf(const Name &other) const noexcept
Check if this name is a prefix of another name.
Definition name.cpp:275
Represents a container for verified certificates.
void insert(const Certificate &cert)
Insert certificate into cache.
const Certificate * find(const Name &certPrefix) const
Get certificate given key name.
void clear()
Remove all certificates from cache.
CertificateCache(const time::nanoseconds &maxLifetime=getDefaultLifetime())
Create an object for certificate cache.
Represents an NDN certificate.
ValidityPeriod getValidityPeriod() const
Get validity period of the certificate.
std::pair< time::system_clock::time_point, time::system_clock::time_point > getPeriod() const
Get the stored validity period.
static time_point now() noexcept
Definition time.cpp:45
#define NDN_LOG_DEBUG(expression)
Log at DEBUG level.
Definition logger.hpp:260
#define NDN_LOG_INFO(expression)
Log at INFO level.
Definition logger.hpp:265
#define NDN_LOG_INIT(name)
Define a non-member log module.
Definition logger.hpp:169
Contains the ndn-cxx security framework.
std::string toIsoString(const system_clock::time_point &timePoint)
Convert to the ISO 8601 string representation, basic format (YYYYMMDDTHHMMSS,fffffffff).
Definition time.cpp:130
::boost::chrono::nanoseconds nanoseconds
Definition time.hpp:54
Definition data.cpp:25