certificate-cache.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2023 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_CXX_SECURITY_CERTIFICATE_CACHE_HPP
23 #define NDN_CXX_SECURITY_CERTIFICATE_CACHE_HPP
24 
25 #include "ndn-cxx/interest.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::security {
34 
41 class CertificateCache : noncopyable
42 {
43 public:
49  explicit
51 
60  void
61  insert(const Certificate& cert);
62 
66  void
67  clear();
68 
76  const Certificate*
77  find(const Name& certPrefix) const;
78 
86  const Certificate*
87  find(const Interest& interest) const;
88 
89 private:
90  class Entry
91  {
92  public:
93  explicit
94  Entry(const Certificate& cert, const time::system_clock::time_point& removalTime)
95  : cert(cert)
96  , removalTime(removalTime)
97  {
98  }
99 
100  const Name&
101  getCertName() const
102  {
103  return cert.getName();
104  }
105 
106  public:
107  Certificate cert;
108  time::system_clock::time_point removalTime;
109  };
110 
114  void
115  refresh();
116 
117 public:
118  static constexpr time::nanoseconds
120  {
121  return 1_h;
122  }
123 
124 private:
126  using CertIndex = boost::multi_index::multi_index_container<
127  Entry,
128  boost::multi_index::indexed_by<
129  boost::multi_index::ordered_non_unique<
130  boost::multi_index::member<Entry, const time::system_clock::time_point, &Entry::removalTime>
131  >,
132  boost::multi_index::ordered_unique<
133  boost::multi_index::const_mem_fun<Entry, const Name&, &Entry::getCertName>
134  >
135  >
136  >;
137  using CertIndexByTime = CertIndex::nth_index<0>::type;
138  using CertIndexByName = CertIndex::nth_index<1>::type;
139 
140  CertIndex m_certs;
141  CertIndexByTime& m_certsByTime;
142  CertIndexByName& m_certsByName;
143  time::nanoseconds m_maxLifetime;
144 };
145 
146 } // namespace ndn::security
147 
148 #endif // NDN_CXX_SECURITY_CERTIFICATE_CACHE_HPP
const Name & getName() const noexcept
Get the Data name.
Definition: data.hpp:137
Represents an Interest packet.
Definition: interest.hpp:50
Represents an absolute name.
Definition: name.hpp:45
Represents a container for verified certificates.
void insert(const Certificate &cert)
Insert certificate into cache.
const Certificate * find(const Name &certPrefix) const
Get certificate given key name.
void clear()
Remove all certificates from cache.
CertificateCache(const time::nanoseconds &maxLifetime=getDefaultLifetime())
Create an object for certificate cache.
static constexpr time::nanoseconds getDefaultLifetime() noexcept
Represents an NDN certificate.
Definition: certificate.hpp:58
::boost::chrono::time_point< system_clock > time_point
Definition: time.hpp:205
Contains the ndn-cxx security framework.
::boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:54