Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 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 | |
| 61 | const Certificate* |
| 62 | CertificateCache::find(const Name& keyName) |
| 63 | { |
| 64 | refresh(); |
| 65 | if (keyName.size() > 0 && keyName[-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(keyName); |
| 69 | if (itr == m_certsByName.end() || !keyName.isPrefixOf(itr->getCertName())) |
| 70 | return nullptr; |
| 71 | return &itr->cert; |
| 72 | } |
| 73 | |
| 74 | const Certificate* |
| 75 | CertificateCache::find(const Interest& interest) |
| 76 | { |
| 77 | if (interest.getChildSelector() >= 0) { |
| 78 | NDN_LOG_DEBUG("Certificate search using ChildSelector is not supported, search 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 the implicit digest is not yet supported"); |
| 82 | } |
| 83 | 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 | { |
| 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 |