blob: 1fb1c63ea9f6fa51017cd5bb96d2d9ec648ba62a [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventodb4da5e2018-06-15 11:37:52 -04002/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
Yingdi Yub8f8b342015-04-27 11:06:42 -07004 *
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
Alexander Afanasyev97709c02016-08-25 19:58:30 -070022#ifndef NDN_SECURITY_PIB_CERTIFICATE_CONTAINER_HPP
23#define NDN_SECURITY_PIB_CERTIFICATE_CONTAINER_HPP
Yingdi Yub8f8b342015-04-27 11:06:42 -070024
Davide Pesavento50f66752017-05-15 20:57:12 -040025#include "../v2/certificate.hpp"
26
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040027#include <iterator>
Yingdi Yub8f8b342015-04-27 11:06:42 -070028#include <set>
Yingdi Yucbe72b02015-11-25 17:35:37 -080029#include <unordered_map>
Yingdi Yub8f8b342015-04-27 11:06:42 -070030
31namespace ndn {
32namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070033namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070034
35class PibImpl;
36
Yingdi Yucbe72b02015-11-25 17:35:37 -080037namespace detail {
38class KeyImpl;
39} // namespace detail
40
41/**
42 * @brief Container of certificates of a key
43 *
44 * The container is used to search/enumerate certificates of a key.
45 * The container can be created only by detail::KeyImpl.
46 */
47class CertificateContainer : noncopyable
Yingdi Yub8f8b342015-04-27 11:06:42 -070048{
49public:
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040050 class const_iterator
Yingdi Yub8f8b342015-04-27 11:06:42 -070051 {
52 public:
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040053 using iterator_category = std::forward_iterator_tag;
54 using value_type = const v2::Certificate;
55 using difference_type = std::ptrdiff_t;
56 using pointer = value_type*;
57 using reference = value_type&;
58
Yingdi Yucbe72b02015-11-25 17:35:37 -080059 const_iterator();
Yingdi Yub8f8b342015-04-27 11:06:42 -070060
Yingdi Yu6ee2d362015-07-16 21:48:05 -070061 v2::Certificate
Yingdi Yub8f8b342015-04-27 11:06:42 -070062 operator*();
63
64 const_iterator&
65 operator++();
66
67 const_iterator
68 operator++(int);
69
70 bool
Yingdi Yucbe72b02015-11-25 17:35:37 -080071 operator==(const const_iterator& other) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -070072
73 bool
Yingdi Yucbe72b02015-11-25 17:35:37 -080074 operator!=(const const_iterator& other) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -070075
76 private:
Yingdi Yucbe72b02015-11-25 17:35:37 -080077 const_iterator(std::set<Name>::const_iterator it, const CertificateContainer& container);
Yingdi Yub8f8b342015-04-27 11:06:42 -070078
79 private:
80 std::set<Name>::const_iterator m_it;
Yingdi Yucbe72b02015-11-25 17:35:37 -080081 const CertificateContainer* m_container;
82
83 friend class CertificateContainer;
Yingdi Yub8f8b342015-04-27 11:06:42 -070084 };
85
86 typedef const_iterator iterator;
87
88public:
Yingdi Yub8f8b342015-04-27 11:06:42 -070089 const_iterator
90 begin() const;
91
92 const_iterator
93 end() const;
94
95 const_iterator
96 find(const Name& certName) const;
97
98 size_t
99 size() const;
100
Yingdi Yucbe72b02015-11-25 17:35:37 -0800101 /**
102 * @brief Add @p certificate into the container
103 * @throw std::invalid_argument the name of @p certificate does not match the key name
104 */
105 void
106 add(const v2::Certificate& certificate);
107
108 /**
109 * @brief Remove a certificate with @p certName from the container
110 * @throw std::invalid_argument @p certName does not match the key name
111 */
112 void
113 remove(const Name& certName);
114
115 /**
116 * @brief Get a certificate with @p certName from the container
117 * @throw std::invalid_argument @p certName does not match the key name
118 * @throw Pib::Error the certificate does not exist
119 */
120 v2::Certificate
121 get(const Name& certName) const;
122
123 /**
124 * @brief Check if the container is consistent with the backend storage
Yingdi Yucbe72b02015-11-25 17:35:37 -0800125 * @note this method is heavyweight and should be used in debugging mode only.
126 */
127 bool
128 isConsistent() const;
129
130NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
131 /**
132 * @brief Create certificate container for a key with @p keyName
Davide Pesavento50f66752017-05-15 20:57:12 -0400133 * @param pibImpl The PIB backend implementation.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800134 */
Davide Pesavento50f66752017-05-15 20:57:12 -0400135 CertificateContainer(const Name& keyName, shared_ptr<PibImpl> pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800136
137 const std::set<Name>&
138 getCertNames() const
139 {
140 return m_certNames;
141 }
142
143 const std::unordered_map<Name, v2::Certificate>&
144 getCache() const
145 {
146 return m_certs;
147 }
148
Yingdi Yub8f8b342015-04-27 11:06:42 -0700149private:
Yingdi Yucbe72b02015-11-25 17:35:37 -0800150 Name m_keyName;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700151 std::set<Name> m_certNames;
Yingdi Yucbe72b02015-11-25 17:35:37 -0800152 /// @brief Cache of loaded certificates
153 mutable std::unordered_map<Name, v2::Certificate> m_certs;
154
Davide Pesavento50f66752017-05-15 20:57:12 -0400155 shared_ptr<PibImpl> m_pib;
Yingdi Yucbe72b02015-11-25 17:35:37 -0800156
157 friend class detail::KeyImpl;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700158};
159
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700160} // namespace pib
161
162using pib::CertificateContainer;
163
Yingdi Yub8f8b342015-04-27 11:06:42 -0700164} // namespace security
165} // namespace ndn
166
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700167#endif // NDN_SECURITY_PIB_CERTIFICATE_CONTAINER_HPP