blob: 55a799e0358b6a39a77cad855a8d772fabef962b [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
Davide Pesavento50f66752017-05-15 20:57:12 -040025#include "key.hpp"
26
Yingdi Yub8f8b342015-04-27 11:06:42 -070027#include <set>
Yingdi Yucbe72b02015-11-25 17:35:37 -080028#include <unordered_map>
Yingdi Yub8f8b342015-04-27 11:06:42 -070029
30namespace ndn {
31namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070032namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070033
34class PibImpl;
35
Yingdi Yucbe72b02015-11-25 17:35:37 -080036namespace detail {
37class KeyImpl;
38class 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 */
47class KeyContainer : noncopyable
Yingdi Yub8f8b342015-04-27 11:06:42 -070048{
49public:
Yingdi Yucbe72b02015-11-25 17:35:37 -080050 class const_iterator : public std::iterator<std::forward_iterator_tag, const Key>
Yingdi Yub8f8b342015-04-27 11:06:42 -070051 {
52 public:
Yingdi Yucbe72b02015-11-25 17:35:37 -080053 const_iterator();
54
Yingdi Yub8f8b342015-04-27 11:06:42 -070055 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 Yucbe72b02015-11-25 17:35:37 -080071 const_iterator(std::set<Name>::const_iterator it, const KeyContainer& container);
Yingdi Yub8f8b342015-04-27 11:06:42 -070072
73 private:
Yingdi Yu6ee2d362015-07-16 21:48:05 -070074 std::set<Name>::const_iterator m_it;
Yingdi Yucbe72b02015-11-25 17:35:37 -080075 const KeyContainer* m_container;
Yingdi Yu6ee2d362015-07-16 21:48:05 -070076
77 friend class KeyContainer;
Yingdi Yub8f8b342015-04-27 11:06:42 -070078 };
79
80 typedef const_iterator iterator;
81
82public:
Yingdi Yub8f8b342015-04-27 11:06:42 -070083 const_iterator
84 begin() const;
85
86 const_iterator
87 end() const;
88
89 const_iterator
Yingdi Yu6ee2d362015-07-16 21:48:05 -070090 find(const Name& keyName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -070091
92 size_t
93 size() const;
94
Yingdi Yucbe72b02015-11-25 17:35:37 -080095 /**
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 Afanasyeva10b2ff2017-01-30 12:44:15 -080098 *
99 * If a key with the same name already exists, overwrite the key.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800100 */
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
127NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
128 /**
129 * @brief Create key container for @p identity
Davide Pesavento50f66752017-05-15 20:57:12 -0400130 * @param pibImpl The PIB backend implementation.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800131 */
Davide Pesavento50f66752017-05-15 20:57:12 -0400132 KeyContainer(const Name& identity, shared_ptr<PibImpl> pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800133
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 Yub8f8b342015-04-27 11:06:42 -0700146private:
147 Name m_identity;
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700148 std::set<Name> m_keyNames;
Yingdi Yucbe72b02015-11-25 17:35:37 -0800149 /// @brief Cache of loaded detail::KeyImpl.
150 mutable std::unordered_map<Name, shared_ptr<detail::KeyImpl>> m_keys;
151
Davide Pesavento50f66752017-05-15 20:57:12 -0400152 shared_ptr<PibImpl> m_pib;
153
Yingdi Yucbe72b02015-11-25 17:35:37 -0800154 friend class detail::IdentityImpl;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700155};
156
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700157} // namespace pib
158
159using pib::KeyContainer;
160
Yingdi Yub8f8b342015-04-27 11:06:42 -0700161} // namespace security
162} // namespace ndn
163
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700164#endif // NDN_SECURITY_PIB_KEY_CONTAINER_HPP