blob: 6475cd284d79ca92afd8db2318fd59fc4dbe7c5f [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventodb4da5e2018-06-15 11:37:52 -04002/*
3 * Copyright (c) 2013-2018 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
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040027#include <iterator>
Yingdi Yub8f8b342015-04-27 11:06:42 -070028#include <set>
Yingdi Yucbe72b02015-11-25 17:35:37 -080029#include <unordered_map>
Yingdi Yub8f8b342015-04-27 11:06:42 -070030
31namespace ndn {
32namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070033namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070034
35class PibImpl;
36
Yingdi Yucbe72b02015-11-25 17:35:37 -080037namespace detail {
38class KeyImpl;
39class IdentityImpl;
40} // namespace detail
41
42/**
43 * @brief Container of keys of an identity
44 *
45 * The container is used to search/enumerate keys of an identity.
46 * The container can be created only by detail::IdentityImpl.
47 */
48class KeyContainer : noncopyable
Yingdi Yub8f8b342015-04-27 11:06:42 -070049{
50public:
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040051 class const_iterator
Yingdi Yub8f8b342015-04-27 11:06:42 -070052 {
53 public:
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040054 using iterator_category = std::forward_iterator_tag;
55 using value_type = const Key;
56 using difference_type = std::ptrdiff_t;
57 using pointer = value_type*;
58 using reference = value_type&;
59
Yingdi Yucbe72b02015-11-25 17:35:37 -080060 const_iterator();
61
Yingdi Yub8f8b342015-04-27 11:06:42 -070062 Key
63 operator*();
64
65 const_iterator&
66 operator++();
67
68 const_iterator
69 operator++(int);
70
71 bool
72 operator==(const const_iterator& other);
73
74 bool
75 operator!=(const const_iterator& other);
76
77 private:
Yingdi Yucbe72b02015-11-25 17:35:37 -080078 const_iterator(std::set<Name>::const_iterator it, const KeyContainer& container);
Yingdi Yub8f8b342015-04-27 11:06:42 -070079
80 private:
Yingdi Yu6ee2d362015-07-16 21:48:05 -070081 std::set<Name>::const_iterator m_it;
Yingdi Yucbe72b02015-11-25 17:35:37 -080082 const KeyContainer* m_container;
Yingdi Yu6ee2d362015-07-16 21:48:05 -070083
84 friend class KeyContainer;
Yingdi Yub8f8b342015-04-27 11:06:42 -070085 };
86
87 typedef const_iterator iterator;
88
89public:
Yingdi Yub8f8b342015-04-27 11:06:42 -070090 const_iterator
91 begin() const;
92
93 const_iterator
94 end() const;
95
96 const_iterator
Yingdi Yu6ee2d362015-07-16 21:48:05 -070097 find(const Name& keyName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -070098
99 size_t
100 size() const;
101
Yingdi Yucbe72b02015-11-25 17:35:37 -0800102 /**
103 * @brief Add @p key of @p keyLen bytes with @p keyName into the container
104 * @throw std::invalid_argument @p keyName does not match the identity
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800105 *
106 * If a key with the same name already exists, overwrite the key.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800107 */
108 Key
109 add(const uint8_t* key, size_t keyLen, const Name& keyName);
110
111 /**
112 * @brief Remove a key with @p keyName from the container
113 * @throw std::invalid_argument @p keyName does not match the identity
114 */
115 void
116 remove(const Name& keyName);
117
118 /**
119 * @brief Get a key with @p keyName from the container
120 * @throw std::invalid_argument @p keyName does not match the identity
121 * @throw Pib::Error the key does not exist
122 */
123 Key
124 get(const Name& keyName) const;
125
126 /**
127 * @brief Check if the container is consistent with the backend storage
128 *
129 * @note this method is heavyweight and should be used in debugging mode only.
130 */
131 bool
132 isConsistent() const;
133
134NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
135 /**
136 * @brief Create key container for @p identity
Davide Pesavento50f66752017-05-15 20:57:12 -0400137 * @param pibImpl The PIB backend implementation.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800138 */
Davide Pesavento50f66752017-05-15 20:57:12 -0400139 KeyContainer(const Name& identity, shared_ptr<PibImpl> pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800140
141 const std::set<Name>&
142 getKeyNames() const
143 {
144 return m_keyNames;
145 }
146
147 const std::unordered_map<Name, shared_ptr<detail::KeyImpl>>&
148 getLoadedKeys() const
149 {
150 return m_keys;
151 }
152
Yingdi Yub8f8b342015-04-27 11:06:42 -0700153private:
154 Name m_identity;
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700155 std::set<Name> m_keyNames;
Yingdi Yucbe72b02015-11-25 17:35:37 -0800156 /// @brief Cache of loaded detail::KeyImpl.
157 mutable std::unordered_map<Name, shared_ptr<detail::KeyImpl>> m_keys;
158
Davide Pesavento50f66752017-05-15 20:57:12 -0400159 shared_ptr<PibImpl> m_pib;
160
Yingdi Yucbe72b02015-11-25 17:35:37 -0800161 friend class detail::IdentityImpl;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700162};
163
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700164} // namespace pib
165
166using pib::KeyContainer;
167
Yingdi Yub8f8b342015-04-27 11:06:42 -0700168} // namespace security
169} // namespace ndn
170
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700171#endif // NDN_SECURITY_PIB_KEY_CONTAINER_HPP