blob: 45d9127132c87e7bdeb0d649378957f6e85bc98f [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu6ee2d362015-07-16 21:48:05 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yub8f8b342015-04-27 11:06:42 -07004 *
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 Afanasyev97709c02016-08-25 19:58:30 -070022#ifndef NDN_SECURITY_PIB_KEY_CONTAINER_HPP
23#define NDN_SECURITY_PIB_KEY_CONTAINER_HPP
Yingdi Yub8f8b342015-04-27 11:06:42 -070024
25#include <set>
Yingdi Yucbe72b02015-11-25 17:35:37 -080026#include <unordered_map>
Yingdi Yub8f8b342015-04-27 11:06:42 -070027#include "key.hpp"
28
29namespace ndn {
30namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070031namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070032
33class PibImpl;
34
Yingdi Yucbe72b02015-11-25 17:35:37 -080035namespace detail {
36class KeyImpl;
37class 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 */
46class KeyContainer : noncopyable
Yingdi Yub8f8b342015-04-27 11:06:42 -070047{
48public:
Yingdi Yucbe72b02015-11-25 17:35:37 -080049 class const_iterator : public std::iterator<std::forward_iterator_tag, const Key>
Yingdi Yub8f8b342015-04-27 11:06:42 -070050 {
51 public:
Yingdi Yucbe72b02015-11-25 17:35:37 -080052 const_iterator();
53
Yingdi Yub8f8b342015-04-27 11:06:42 -070054 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 Yucbe72b02015-11-25 17:35:37 -080070 const_iterator(std::set<Name>::const_iterator it, const KeyContainer& container);
Yingdi Yub8f8b342015-04-27 11:06:42 -070071
72 private:
Yingdi Yu6ee2d362015-07-16 21:48:05 -070073 std::set<Name>::const_iterator m_it;
Yingdi Yucbe72b02015-11-25 17:35:37 -080074 const KeyContainer* m_container;
Yingdi Yu6ee2d362015-07-16 21:48:05 -070075
76 friend class KeyContainer;
Yingdi Yub8f8b342015-04-27 11:06:42 -070077 };
78
79 typedef const_iterator iterator;
80
81public:
Yingdi Yub8f8b342015-04-27 11:06:42 -070082 const_iterator
83 begin() const;
84
85 const_iterator
86 end() const;
87
88 const_iterator
Yingdi Yu6ee2d362015-07-16 21:48:05 -070089 find(const Name& keyName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -070090
91 size_t
92 size() const;
93
Yingdi Yucbe72b02015-11-25 17:35:37 -080094 /**
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
97 */
98 Key
99 add(const uint8_t* key, size_t keyLen, const Name& keyName);
100
101 /**
102 * @brief Remove a key with @p keyName from the container
103 * @throw std::invalid_argument @p keyName does not match the identity
104 */
105 void
106 remove(const Name& keyName);
107
108 /**
109 * @brief Get a key with @p keyName from the container
110 * @throw std::invalid_argument @p keyName does not match the identity
111 * @throw Pib::Error the key does not exist
112 */
113 Key
114 get(const Name& keyName) const;
115
116 /**
117 * @brief Check if the container is consistent with the backend storage
118 *
119 * @note this method is heavyweight and should be used in debugging mode only.
120 */
121 bool
122 isConsistent() const;
123
124NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
125 /**
126 * @brief Create key container for @p identity
127 * @param impl The PIB backend implementation.
128 */
129 KeyContainer(const Name& identity, shared_ptr<PibImpl> impl);
130
131 const std::set<Name>&
132 getKeyNames() const
133 {
134 return m_keyNames;
135 }
136
137 const std::unordered_map<Name, shared_ptr<detail::KeyImpl>>&
138 getLoadedKeys() const
139 {
140 return m_keys;
141 }
142
Yingdi Yub8f8b342015-04-27 11:06:42 -0700143private:
144 Name m_identity;
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700145 std::set<Name> m_keyNames;
Yingdi Yucbe72b02015-11-25 17:35:37 -0800146 /// @brief Cache of loaded detail::KeyImpl.
147 mutable std::unordered_map<Name, shared_ptr<detail::KeyImpl>> m_keys;
148
Yingdi Yub8f8b342015-04-27 11:06:42 -0700149 shared_ptr<PibImpl> m_impl;
Yingdi Yucbe72b02015-11-25 17:35:37 -0800150 friend class detail::IdentityImpl;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700151};
152
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700153} // namespace pib
154
155using pib::KeyContainer;
156
Yingdi Yub8f8b342015-04-27 11:06:42 -0700157} // namespace security
158} // namespace ndn
159
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700160#endif // NDN_SECURITY_PIB_KEY_CONTAINER_HPP