Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame^] | 2 | /* |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 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 | |
| 25 | namespace ndn { |
| 26 | namespace security { |
| 27 | namespace v2 { |
| 28 | |
| 29 | NDN_LOG_INIT(ndn.security.v2.CertificateCache); |
| 30 | |
| 31 | const time::nanoseconds& |
| 32 | CertificateCache::getDefaultLifetime() |
| 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 |
| 46 | CertificateCache::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 | |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame^] | 61 | void |
| 62 | CertificateCache::clear() |
| 63 | { |
| 64 | m_certs.clear(); |
| 65 | } |
| 66 | |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 67 | const Certificate* |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 68 | CertificateCache::find(const Name& certPrefix) const |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 69 | { |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 70 | const_cast<CertificateCache*>(this)->refresh(); |
| 71 | if (certPrefix.size() > 0 && certPrefix[-1].isImplicitSha256Digest()) { |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 72 | NDN_LOG_INFO("Certificate search using name with the implicit digest is not yet supported"); |
| 73 | } |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 74 | auto itr = m_certsByName.lower_bound(certPrefix); |
| 75 | if (itr == m_certsByName.end() || !certPrefix.isPrefixOf(itr->getCertName())) |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 76 | return nullptr; |
| 77 | return &itr->cert; |
| 78 | } |
| 79 | |
| 80 | const Certificate* |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 81 | CertificateCache::find(const Interest& interest) const |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 82 | { |
| 83 | if (interest.getChildSelector() >= 0) { |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 84 | NDN_LOG_DEBUG("Certificate search using ChildSelector is not supported, searching as if selector not specified"); |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 85 | } |
| 86 | if (interest.getName().size() > 0 && interest.getName()[-1].isImplicitSha256Digest()) { |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 87 | NDN_LOG_INFO("Certificate search using name with implicit digest is not yet supported"); |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 88 | } |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 89 | const_cast<CertificateCache*>(this)->refresh(); |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 90 | |
| 91 | for (auto i = m_certsByName.lower_bound(interest.getName()); |
| 92 | i != m_certsByName.end() && interest.getName().isPrefixOf(i->getCertName()); |
| 93 | ++i) { |
| 94 | const auto& cert = i->cert; |
| 95 | if (interest.matchesData(cert)) { |
| 96 | return &cert; |
| 97 | } |
| 98 | } |
| 99 | return nullptr; |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | CertificateCache::refresh() |
| 104 | { |
| 105 | time::system_clock::TimePoint now = time::system_clock::now(); |
| 106 | |
| 107 | auto cIt = m_certsByTime.begin(); |
| 108 | while (cIt != m_certsByTime.end() && cIt->removalTime < now) { |
| 109 | m_certsByTime.erase(cIt); |
| 110 | cIt = m_certsByTime.begin(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | } // namespace v2 |
| 115 | } // namespace security |
| 116 | } // namespace ndn |