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