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 | /* |
| 3 | * Copyright (c) 2013-2018 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 | |
Alexander Afanasyev | 97709c0 | 2016-08-25 19:58:30 -0700 | [diff] [blame] | 22 | #ifndef NDN_SECURITY_PIB_KEY_CONTAINER_HPP |
| 23 | #define NDN_SECURITY_PIB_KEY_CONTAINER_HPP |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 25 | #include "key.hpp" |
| 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 | |
| 31 | namespace ndn { |
| 32 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 33 | namespace pib { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 34 | |
| 35 | class PibImpl; |
| 36 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 37 | namespace detail { |
| 38 | class KeyImpl; |
| 39 | class IdentityImpl; |
| 40 | } // namespace detail |
| 41 | |
| 42 | /** |
| 43 | * @brief Container of keys of an identity |
| 44 | * |
| 45 | * The container is used to search/enumerate keys of an identity. |
| 46 | * The container can be created only by detail::IdentityImpl. |
| 47 | */ |
| 48 | class KeyContainer : noncopyable |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 49 | { |
| 50 | public: |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 51 | class const_iterator |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 52 | { |
| 53 | public: |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 54 | using iterator_category = std::forward_iterator_tag; |
| 55 | using value_type = const Key; |
| 56 | using difference_type = std::ptrdiff_t; |
| 57 | using pointer = value_type*; |
| 58 | using reference = value_type&; |
| 59 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 60 | const_iterator(); |
| 61 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 62 | Key |
| 63 | operator*(); |
| 64 | |
| 65 | const_iterator& |
| 66 | operator++(); |
| 67 | |
| 68 | const_iterator |
| 69 | operator++(int); |
| 70 | |
| 71 | bool |
| 72 | operator==(const const_iterator& other); |
| 73 | |
| 74 | bool |
| 75 | operator!=(const const_iterator& other); |
| 76 | |
| 77 | private: |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 78 | const_iterator(std::set<Name>::const_iterator it, const KeyContainer& container); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 79 | |
| 80 | private: |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 81 | std::set<Name>::const_iterator m_it; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 82 | const KeyContainer* m_container; |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 83 | |
| 84 | friend class KeyContainer; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | typedef const_iterator iterator; |
| 88 | |
| 89 | public: |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 90 | const_iterator |
| 91 | begin() const; |
| 92 | |
| 93 | const_iterator |
| 94 | end() const; |
| 95 | |
| 96 | const_iterator |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 97 | find(const Name& keyName) const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 98 | |
| 99 | size_t |
| 100 | size() const; |
| 101 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 102 | /** |
| 103 | * @brief Add @p key of @p keyLen bytes with @p keyName into the container |
| 104 | * @throw std::invalid_argument @p keyName does not match the identity |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 105 | * |
| 106 | * If a key with the same name already exists, overwrite the key. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 107 | */ |
| 108 | Key |
| 109 | add(const uint8_t* key, size_t keyLen, const Name& keyName); |
| 110 | |
| 111 | /** |
| 112 | * @brief Remove a key with @p keyName from the container |
| 113 | * @throw std::invalid_argument @p keyName does not match the identity |
| 114 | */ |
| 115 | void |
| 116 | remove(const Name& keyName); |
| 117 | |
| 118 | /** |
| 119 | * @brief Get a key with @p keyName from the container |
| 120 | * @throw std::invalid_argument @p keyName does not match the identity |
| 121 | * @throw Pib::Error the key does not exist |
| 122 | */ |
| 123 | Key |
| 124 | get(const Name& keyName) const; |
| 125 | |
| 126 | /** |
| 127 | * @brief Check if the container is consistent with the backend storage |
| 128 | * |
| 129 | * @note this method is heavyweight and should be used in debugging mode only. |
| 130 | */ |
| 131 | bool |
| 132 | isConsistent() const; |
| 133 | |
| 134 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 135 | /** |
| 136 | * @brief Create key container for @p identity |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 137 | * @param pibImpl The PIB backend implementation. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 138 | */ |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 139 | KeyContainer(const Name& identity, shared_ptr<PibImpl> pibImpl); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 140 | |
| 141 | const std::set<Name>& |
| 142 | getKeyNames() const |
| 143 | { |
| 144 | return m_keyNames; |
| 145 | } |
| 146 | |
| 147 | const std::unordered_map<Name, shared_ptr<detail::KeyImpl>>& |
| 148 | getLoadedKeys() const |
| 149 | { |
| 150 | return m_keys; |
| 151 | } |
| 152 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 153 | private: |
| 154 | Name m_identity; |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 155 | std::set<Name> m_keyNames; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 156 | /// @brief Cache of loaded detail::KeyImpl. |
| 157 | mutable std::unordered_map<Name, shared_ptr<detail::KeyImpl>> m_keys; |
| 158 | |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 159 | shared_ptr<PibImpl> m_pib; |
| 160 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 161 | friend class detail::IdentityImpl; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 164 | } // namespace pib |
| 165 | |
| 166 | using pib::KeyContainer; |
| 167 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 168 | } // namespace security |
| 169 | } // namespace ndn |
| 170 | |
Alexander Afanasyev | 97709c0 | 2016-08-25 19:58:30 -0700 | [diff] [blame] | 171 | #endif // NDN_SECURITY_PIB_KEY_CONTAINER_HPP |