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 | #ifndef NDN_SECURITY_V2_CERTIFICATE_CACHE_HPP |
| 23 | #define NDN_SECURITY_V2_CERTIFICATE_CACHE_HPP |
| 24 | |
| 25 | #include "../../interest.hpp" |
| 26 | #include "certificate.hpp" |
| 27 | |
| 28 | #include <boost/multi_index_container.hpp> |
| 29 | #include <boost/multi_index/ordered_index.hpp> |
| 30 | #include <boost/multi_index/mem_fun.hpp> |
| 31 | #include <boost/multi_index/member.hpp> |
| 32 | |
| 33 | namespace ndn { |
| 34 | namespace security { |
| 35 | namespace v2 { |
| 36 | |
| 37 | /** |
| 38 | * @brief Represents a container for verified certificates. |
| 39 | * |
| 40 | * A certificate is removed no later than its NotAfter time, or maxLifetime after it has been |
| 41 | * added to the cache. |
| 42 | */ |
| 43 | class CertificateCache : noncopyable |
| 44 | { |
| 45 | public: |
| 46 | /** |
| 47 | * @brief Create an object for certificate cache. |
| 48 | * |
| 49 | * @param maxLifetime the maximum time that certificates could live inside cache (default: 1 hour) |
| 50 | */ |
| 51 | explicit |
| 52 | CertificateCache(const time::nanoseconds& maxLifetime = getDefaultLifetime()); |
| 53 | |
| 54 | /** |
| 55 | * @brief Insert certificate into cache. |
| 56 | * |
| 57 | * The inserted certificate will be removed no later than its NotAfter time, or maxLifetime |
| 58 | * defined during cache construction. |
| 59 | * |
| 60 | * @param cert the certificate packet. |
| 61 | */ |
| 62 | void |
| 63 | insert(const Certificate& cert); |
| 64 | |
| 65 | /** |
| 66 | * @brief Get certificate given key name |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame^] | 67 | * @param certPrefix Certificate prefix for searching the certificate. |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 68 | * @return The found certificate, nullptr if not found. |
| 69 | * |
| 70 | * @note The returned value may be invalidated after next call to one of find methods. |
| 71 | */ |
| 72 | const Certificate* |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame^] | 73 | find(const Name& certPrefix) const; |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * @brief Find certificate given interest |
| 77 | * @param interest The input interest packet. |
| 78 | * @return The found certificate that matches the interest, nullptr if not found. |
| 79 | * |
| 80 | * @note ChildSelector is not supported. |
| 81 | * |
| 82 | * @note The returned value may be invalidated after next call to one of find methods. |
| 83 | */ |
| 84 | const Certificate* |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame^] | 85 | find(const Interest& interest) const; |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 86 | |
| 87 | private: |
| 88 | class Entry |
| 89 | { |
| 90 | public: |
| 91 | Entry(const Certificate& cert, const time::system_clock::TimePoint& removalTime) |
| 92 | : cert(cert) |
| 93 | , removalTime(removalTime) |
| 94 | { |
| 95 | } |
| 96 | |
| 97 | const Name& |
| 98 | getCertName() const |
| 99 | { |
| 100 | return cert.getName(); |
| 101 | } |
| 102 | |
| 103 | public: |
| 104 | Certificate cert; |
| 105 | time::system_clock::TimePoint removalTime; |
| 106 | }; |
| 107 | |
| 108 | /** |
| 109 | * @brief Remove all outdated certificate entries. |
| 110 | */ |
| 111 | void |
| 112 | refresh(); |
| 113 | |
| 114 | public: |
| 115 | static const time::nanoseconds& |
| 116 | getDefaultLifetime(); |
| 117 | |
| 118 | private: |
| 119 | /// @todo Switch to InMemoryStorateTimeout after it is available (task #3917) |
| 120 | typedef boost::multi_index::multi_index_container< |
| 121 | Entry, |
| 122 | boost::multi_index::indexed_by< |
| 123 | boost::multi_index::ordered_non_unique< |
| 124 | boost::multi_index::member<Entry, const time::system_clock::TimePoint, &Entry::removalTime> |
| 125 | >, |
| 126 | boost::multi_index::ordered_unique< |
| 127 | boost::multi_index::const_mem_fun<Entry, const Name&, &Entry::getCertName> |
| 128 | > |
| 129 | > |
| 130 | > CertIndex; |
| 131 | |
| 132 | typedef CertIndex::nth_index<0>::type CertIndexByTime; |
| 133 | typedef CertIndex::nth_index<1>::type CertIndexByName; |
| 134 | CertIndex m_certs; |
| 135 | CertIndexByTime& m_certsByTime; |
| 136 | CertIndexByName& m_certsByName; |
| 137 | time::nanoseconds m_maxLifetime; |
| 138 | }; |
| 139 | |
| 140 | } // namespace v2 |
| 141 | } // namespace security |
| 142 | } // namespace ndn |
| 143 | |
| 144 | #endif // NDN_SECURITY_V2_CERTIFICATE_CACHE_HPP |