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