blob: b9d15aa1634f4a83f0d21f3dd527011a871e49ac [file] [log] [blame]
Qiuhan Ding609f0612015-11-04 14:07:14 -08001/* -*- 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
25namespace ndn {
26namespace security {
27namespace v2 {
28
29NDN_LOG_INIT(ndn.security.v2.CertificateCache);
30
31const time::nanoseconds&
32CertificateCache::getDefaultLifetime()
33{
34 static time::nanoseconds lifetime = time::seconds(3600);
35 return lifetime;
36}
37
38CertificateCache::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
45void
46CertificateCache::insert(const Certificate& cert)
47{
48 time::system_clock::TimePoint notAfterTime = cert.getValidityPeriod().getPeriod().second;
49 time::system_clock::TimePoint now = time::system_clock::now();
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
61const Certificate*
Alexander Afanasyev7e721412017-01-11 13:36:08 -080062CertificateCache::find(const Name& certPrefix) const
Qiuhan Ding609f0612015-11-04 14:07:14 -080063{
Alexander Afanasyev7e721412017-01-11 13:36:08 -080064 const_cast<CertificateCache*>(this)->refresh();
65 if (certPrefix.size() > 0 && certPrefix[-1].isImplicitSha256Digest()) {
Qiuhan Ding609f0612015-11-04 14:07:14 -080066 NDN_LOG_INFO("Certificate search using name with the implicit digest is not yet supported");
67 }
Alexander Afanasyev7e721412017-01-11 13:36:08 -080068 auto itr = m_certsByName.lower_bound(certPrefix);
69 if (itr == m_certsByName.end() || !certPrefix.isPrefixOf(itr->getCertName()))
Qiuhan Ding609f0612015-11-04 14:07:14 -080070 return nullptr;
71 return &itr->cert;
72}
73
74const Certificate*
Alexander Afanasyev7e721412017-01-11 13:36:08 -080075CertificateCache::find(const Interest& interest) const
Qiuhan Ding609f0612015-11-04 14:07:14 -080076{
77 if (interest.getChildSelector() >= 0) {
Alexander Afanasyev7e721412017-01-11 13:36:08 -080078 NDN_LOG_DEBUG("Certificate search using ChildSelector is not supported, searching as if selector not specified");
Qiuhan Ding609f0612015-11-04 14:07:14 -080079 }
80 if (interest.getName().size() > 0 && interest.getName()[-1].isImplicitSha256Digest()) {
Alexander Afanasyev7e721412017-01-11 13:36:08 -080081 NDN_LOG_INFO("Certificate search using name with implicit digest is not yet supported");
Qiuhan Ding609f0612015-11-04 14:07:14 -080082 }
Alexander Afanasyev7e721412017-01-11 13:36:08 -080083 const_cast<CertificateCache*>(this)->refresh();
Qiuhan Ding609f0612015-11-04 14:07:14 -080084
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
96void
97CertificateCache::refresh()
98{
99 time::system_clock::TimePoint now = time::system_clock::now();
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