blob: 9726b7ef30f4410541164f4334c20bd69e6f72ac [file] [log] [blame]
Qiuhan Ding609f0612015-11-04 14:07:14 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev6aff0242017-08-29 17:14:44 -04002/*
Davide Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 Regents of the University of California.
Qiuhan Ding609f0612015-11-04 14:07:14 -08004 *
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
25namespace ndn {
26namespace security {
27namespace v2 {
28
29NDN_LOG_INIT(ndn.security.v2.CertificateCache);
30
Davide Pesavento0f830802018-01-16 23:58:58 -050031time::nanoseconds
Qiuhan Ding609f0612015-11-04 14:07:14 -080032CertificateCache::getDefaultLifetime()
33{
Davide Pesavento0f830802018-01-16 23:58:58 -050034 return 1_h;
Qiuhan Ding609f0612015-11-04 14:07:14 -080035}
36
37CertificateCache::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
44void
45CertificateCache::insert(const Certificate& cert)
46{
47 time::system_clock::TimePoint notAfterTime = cert.getValidityPeriod().getPeriod().second;
48 time::system_clock::TimePoint now = time::system_clock::now();
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
Alexander Afanasyev6aff0242017-08-29 17:14:44 -040060void
61CertificateCache::clear()
62{
63 m_certs.clear();
64}
65
Qiuhan Ding609f0612015-11-04 14:07:14 -080066const Certificate*
Alexander Afanasyev7e721412017-01-11 13:36:08 -080067CertificateCache::find(const Name& certPrefix) const
Qiuhan Ding609f0612015-11-04 14:07:14 -080068{
Alexander Afanasyev7e721412017-01-11 13:36:08 -080069 const_cast<CertificateCache*>(this)->refresh();
70 if (certPrefix.size() > 0 && certPrefix[-1].isImplicitSha256Digest()) {
Qiuhan Ding609f0612015-11-04 14:07:14 -080071 NDN_LOG_INFO("Certificate search using name with the implicit digest is not yet supported");
72 }
Alexander Afanasyev7e721412017-01-11 13:36:08 -080073 auto itr = m_certsByName.lower_bound(certPrefix);
74 if (itr == m_certsByName.end() || !certPrefix.isPrefixOf(itr->getCertName()))
Qiuhan Ding609f0612015-11-04 14:07:14 -080075 return nullptr;
76 return &itr->cert;
77}
78
79const Certificate*
Alexander Afanasyev7e721412017-01-11 13:36:08 -080080CertificateCache::find(const Interest& interest) const
Qiuhan Ding609f0612015-11-04 14:07:14 -080081{
82 if (interest.getChildSelector() >= 0) {
Alexander Afanasyev7e721412017-01-11 13:36:08 -080083 NDN_LOG_DEBUG("Certificate search using ChildSelector is not supported, searching as if selector not specified");
Qiuhan Ding609f0612015-11-04 14:07:14 -080084 }
85 if (interest.getName().size() > 0 && interest.getName()[-1].isImplicitSha256Digest()) {
Alexander Afanasyev7e721412017-01-11 13:36:08 -080086 NDN_LOG_INFO("Certificate search using name with implicit digest is not yet supported");
Qiuhan Ding609f0612015-11-04 14:07:14 -080087 }
Alexander Afanasyev7e721412017-01-11 13:36:08 -080088 const_cast<CertificateCache*>(this)->refresh();
Qiuhan Ding609f0612015-11-04 14:07:14 -080089
90 for (auto i = m_certsByName.lower_bound(interest.getName());
91 i != m_certsByName.end() && interest.getName().isPrefixOf(i->getCertName());
92 ++i) {
93 const auto& cert = i->cert;
94 if (interest.matchesData(cert)) {
95 return &cert;
96 }
97 }
98 return nullptr;
99}
100
101void
102CertificateCache::refresh()
103{
104 time::system_clock::TimePoint now = time::system_clock::now();
105
106 auto cIt = m_certsByTime.begin();
107 while (cIt != m_certsByTime.end() && cIt->removalTime < now) {
108 m_certsByTime.erase(cIt);
109 cIt = m_certsByTime.begin();
110 }
111}
112
113} // namespace v2
114} // namespace security
115} // namespace ndn