blob: 2b9b36cf970e07f229f233d7369dc612851a3e01 [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/*
Davide Pesaventofcd3e442023-03-10 18:44:11 -05003 * Copyright (c) 2013-2023 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
Davide Pesavento09904412021-03-24 16:40:53 -040022#ifndef NDN_CXX_SECURITY_PIB_KEY_CONTAINER_HPP
23#define NDN_CXX_SECURITY_PIB_KEY_CONTAINER_HPP
Yingdi Yub8f8b342015-04-27 11:06:42 -070024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "ndn-cxx/security/pib/key.hpp"
Davide Pesavento50f66752017-05-15 20:57:12 -040026
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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040031namespace ndn::security::pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070032
33class PibImpl;
34
Yingdi Yucbe72b02015-11-25 17:35:37 -080035/**
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040036 * @brief Container of keys of an identity.
Yingdi Yucbe72b02015-11-25 17:35:37 -080037 *
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040038 * The container is used to search/enumerate the keys of an identity.
39 * It can be created only by the IdentityImpl private class.
40 *
41 * @sa Identity::getKeys()
Yingdi Yucbe72b02015-11-25 17:35:37 -080042 */
43class KeyContainer : noncopyable
Yingdi Yub8f8b342015-04-27 11:06:42 -070044{
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040045private:
46 using NameSet = std::set<Name>;
47
Yingdi Yub8f8b342015-04-27 11:06:42 -070048public:
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040049 class const_iterator
Yingdi Yub8f8b342015-04-27 11:06:42 -070050 {
51 public:
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040052 using iterator_category = std::forward_iterator_tag;
53 using value_type = const Key;
54 using difference_type = std::ptrdiff_t;
55 using pointer = value_type*;
56 using reference = value_type&;
57
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040058 const_iterator() = default;
Yingdi Yucbe72b02015-11-25 17:35:37 -080059
Yingdi Yub8f8b342015-04-27 11:06:42 -070060 Key
61 operator*();
62
63 const_iterator&
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040064 operator++()
65 {
66 ++m_it;
67 return *this;
68 }
Yingdi Yub8f8b342015-04-27 11:06:42 -070069
70 const_iterator
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040071 operator++(int)
72 {
73 const_iterator it(*this);
74 ++m_it;
75 return it;
76 }
Yingdi Yub8f8b342015-04-27 11:06:42 -070077
78 bool
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040079 operator==(const const_iterator& other) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -070080
81 bool
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040082 operator!=(const const_iterator& other) const
83 {
84 return !this->operator==(other);
85 }
Yingdi Yub8f8b342015-04-27 11:06:42 -070086
87 private:
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040088 const_iterator(NameSet::const_iterator it, const KeyContainer& container) noexcept;
Yingdi Yub8f8b342015-04-27 11:06:42 -070089
90 private:
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040091 NameSet::const_iterator m_it;
92 const KeyContainer* m_container = nullptr;
Yingdi Yu6ee2d362015-07-16 21:48:05 -070093
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040094 friend KeyContainer;
Yingdi Yub8f8b342015-04-27 11:06:42 -070095 };
96
Davide Pesaventof2cae612021-03-24 18:47:05 -040097 using iterator = const_iterator;
Yingdi Yub8f8b342015-04-27 11:06:42 -070098
99public:
Yingdi Yub8f8b342015-04-27 11:06:42 -0700100 const_iterator
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400101 begin() const noexcept
102 {
103 return {m_keyNames.begin(), *this};
104 }
Yingdi Yub8f8b342015-04-27 11:06:42 -0700105
106 const_iterator
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400107 end() const noexcept
108 {
109 return {};
110 }
Yingdi Yub8f8b342015-04-27 11:06:42 -0700111
112 const_iterator
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700113 find(const Name& keyName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700114
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400115 /**
116 * @brief Check whether the container is empty.
117 */
Davide Pesaventofcd3e442023-03-10 18:44:11 -0500118 [[nodiscard]] bool
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400119 empty() const noexcept
120 {
121 return m_keyNames.empty();
122 }
123
124 /**
125 * @brief Return the number of keys in the container.
126 */
Yingdi Yub8f8b342015-04-27 11:06:42 -0700127 size_t
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400128 size() const noexcept
129 {
130 return m_keyNames.size();
131 }
Yingdi Yub8f8b342015-04-27 11:06:42 -0700132
Yingdi Yucbe72b02015-11-25 17:35:37 -0800133 /**
Davide Pesavento765abc92021-12-27 00:44:04 -0500134 * @brief Add @p key with name @p keyName into the container.
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800135 *
Davide Pesavento765abc92021-12-27 00:44:04 -0500136 * If a key with the same name already exists, it will be overwritten.
137 *
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400138 * @throw std::invalid_argument @p keyName does not match the identity.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800139 */
140 Key
Davide Pesavento765abc92021-12-27 00:44:04 -0500141 add(span<const uint8_t> key, const Name& keyName);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800142
143 /**
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400144 * @brief Remove a key with @p keyName from the container.
145 * @throw std::invalid_argument @p keyName does not match the identity.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800146 */
147 void
148 remove(const Name& keyName);
149
150 /**
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400151 * @brief Return a key by name.
152 * @throw Pib::Error The key does not exist.
153 * @throw std::invalid_argument @p keyName does not match the identity.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800154 */
155 Key
156 get(const Name& keyName) const;
157
158 /**
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400159 * @brief Check if the container is consistent with the backend storage.
160 * @note This method is heavyweight and should be used in debugging mode only.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800161 */
162 bool
163 isConsistent() const;
164
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400165NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // private interface for IdentityImpl
Yingdi Yucbe72b02015-11-25 17:35:37 -0800166 /**
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400167 * @brief Create key container for @p identity.
Davide Pesavento50f66752017-05-15 20:57:12 -0400168 * @param pibImpl The PIB backend implementation.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800169 */
Davide Pesavento50f66752017-05-15 20:57:12 -0400170 KeyContainer(const Name& identity, shared_ptr<PibImpl> pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800171
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400172NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
173 // cache of loaded KeyImpl
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400174 mutable std::unordered_map<Name, shared_ptr<KeyImpl>> m_keys;
Yingdi Yucbe72b02015-11-25 17:35:37 -0800175
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400176private:
177 NameSet m_keyNames;
178 const Name m_identity;
179 const shared_ptr<PibImpl> m_pib;
Davide Pesavento50f66752017-05-15 20:57:12 -0400180
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400181 friend class IdentityImpl;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700182};
183
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400184} // namespace ndn::security::pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700185
Davide Pesavento09904412021-03-24 16:40:53 -0400186#endif // NDN_CXX_SECURITY_PIB_KEY_CONTAINER_HPP