Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | fcd3e44 | 2023-03-10 18:44:11 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | 0990441 | 2021-03-24 16:40:53 -0400 | [diff] [blame] | 22 | #ifndef NDN_CXX_SECURITY_PIB_KEY_CONTAINER_HPP |
| 23 | #define NDN_CXX_SECURITY_PIB_KEY_CONTAINER_HPP |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/security/pib/key.hpp" |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 26 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 27 | #include <iterator> |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 28 | #include <set> |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 29 | #include <unordered_map> |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 31 | namespace ndn::security::pib { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 32 | |
| 33 | class PibImpl; |
| 34 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 35 | /** |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 36 | * @brief Container of keys of an identity. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 37 | * |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 38 | * 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 42 | */ |
| 43 | class KeyContainer : noncopyable |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 44 | { |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 45 | private: |
| 46 | using NameSet = std::set<Name>; |
| 47 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 48 | public: |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 49 | class const_iterator |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 50 | { |
| 51 | public: |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 52 | 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 Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 58 | const_iterator() = default; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 59 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 60 | Key |
| 61 | operator*(); |
| 62 | |
| 63 | const_iterator& |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 64 | operator++() |
| 65 | { |
| 66 | ++m_it; |
| 67 | return *this; |
| 68 | } |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 69 | |
| 70 | const_iterator |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 71 | operator++(int) |
| 72 | { |
| 73 | const_iterator it(*this); |
| 74 | ++m_it; |
| 75 | return it; |
| 76 | } |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 77 | |
| 78 | bool |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 79 | operator==(const const_iterator& other) const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 80 | |
| 81 | bool |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 82 | operator!=(const const_iterator& other) const |
| 83 | { |
| 84 | return !this->operator==(other); |
| 85 | } |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 86 | |
| 87 | private: |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 88 | const_iterator(NameSet::const_iterator it, const KeyContainer& container) noexcept; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 89 | |
| 90 | private: |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 91 | NameSet::const_iterator m_it; |
| 92 | const KeyContainer* m_container = nullptr; |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 93 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 94 | friend KeyContainer; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 97 | using iterator = const_iterator; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 98 | |
| 99 | public: |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 100 | const_iterator |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 101 | begin() const noexcept |
| 102 | { |
| 103 | return {m_keyNames.begin(), *this}; |
| 104 | } |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 105 | |
| 106 | const_iterator |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 107 | end() const noexcept |
| 108 | { |
| 109 | return {}; |
| 110 | } |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 111 | |
| 112 | const_iterator |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 113 | find(const Name& keyName) const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 114 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 115 | /** |
| 116 | * @brief Check whether the container is empty. |
| 117 | */ |
Davide Pesavento | fcd3e44 | 2023-03-10 18:44:11 -0500 | [diff] [blame] | 118 | [[nodiscard]] bool |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 119 | empty() const noexcept |
| 120 | { |
| 121 | return m_keyNames.empty(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @brief Return the number of keys in the container. |
| 126 | */ |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 127 | size_t |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 128 | size() const noexcept |
| 129 | { |
| 130 | return m_keyNames.size(); |
| 131 | } |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 132 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 133 | /** |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 134 | * @brief Add @p key with name @p keyName into the container. |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 135 | * |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 136 | * If a key with the same name already exists, it will be overwritten. |
| 137 | * |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 138 | * @throw std::invalid_argument @p keyName does not match the identity. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 139 | */ |
| 140 | Key |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 141 | add(span<const uint8_t> key, const Name& keyName); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 142 | |
| 143 | /** |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 144 | * @brief Remove a key with @p keyName from the container. |
| 145 | * @throw std::invalid_argument @p keyName does not match the identity. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 146 | */ |
| 147 | void |
| 148 | remove(const Name& keyName); |
| 149 | |
| 150 | /** |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 151 | * @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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 154 | */ |
| 155 | Key |
| 156 | get(const Name& keyName) const; |
| 157 | |
| 158 | /** |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 159 | * @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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 161 | */ |
| 162 | bool |
| 163 | isConsistent() const; |
| 164 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 165 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // private interface for IdentityImpl |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 166 | /** |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 167 | * @brief Create key container for @p identity. |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 168 | * @param pibImpl The PIB backend implementation. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 169 | */ |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 170 | KeyContainer(const Name& identity, shared_ptr<PibImpl> pibImpl); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 171 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 172 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 173 | // cache of loaded KeyImpl |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 174 | mutable std::unordered_map<Name, shared_ptr<KeyImpl>> m_keys; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 175 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 176 | private: |
| 177 | NameSet m_keyNames; |
| 178 | const Name m_identity; |
| 179 | const shared_ptr<PibImpl> m_pib; |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 180 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 181 | friend class IdentityImpl; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 184 | } // namespace ndn::security::pib |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 185 | |
Davide Pesavento | 0990441 | 2021-03-24 16:40:53 -0400 | [diff] [blame] | 186 | #endif // NDN_CXX_SECURITY_PIB_KEY_CONTAINER_HPP |