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