blob: cb9b21132a80ad472b2f6b0b59fbf022e6bec30e [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/*
Davide Pesaventofcd3e442023-03-10 18:44:11 -05003 * Copyright (c) 2013-2023 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
Davide Pesavento09904412021-03-24 16:40:53 -040022#ifndef NDN_CXX_SECURITY_PIB_KEY_CONTAINER_HPP
23#define NDN_CXX_SECURITY_PIB_KEY_CONTAINER_HPP
Yingdi Yub8f8b342015-04-27 11:06:42 -070024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "ndn-cxx/security/pib/key.hpp"
Davide Pesavento50f66752017-05-15 20:57:12 -040026
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 {
Yingdi Yucbe72b02015-11-25 17:35:37 -080038class IdentityImpl;
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040039class KeyImpl;
Yingdi Yucbe72b02015-11-25 17:35:37 -080040} // namespace detail
41
42/**
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040043 * @brief Container of keys of an identity.
Yingdi Yucbe72b02015-11-25 17:35:37 -080044 *
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040045 * The container is used to search/enumerate the keys of an identity.
46 * It can be created only by the IdentityImpl private class.
47 *
48 * @sa Identity::getKeys()
Yingdi Yucbe72b02015-11-25 17:35:37 -080049 */
50class KeyContainer : noncopyable
Yingdi Yub8f8b342015-04-27 11:06:42 -070051{
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040052private:
53 using NameSet = std::set<Name>;
54
Yingdi Yub8f8b342015-04-27 11:06:42 -070055public:
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040056 class const_iterator
Yingdi Yub8f8b342015-04-27 11:06:42 -070057 {
58 public:
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040059 using iterator_category = std::forward_iterator_tag;
60 using value_type = const Key;
61 using difference_type = std::ptrdiff_t;
62 using pointer = value_type*;
63 using reference = value_type&;
64
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040065 const_iterator() = default;
Yingdi Yucbe72b02015-11-25 17:35:37 -080066
Yingdi Yub8f8b342015-04-27 11:06:42 -070067 Key
68 operator*();
69
70 const_iterator&
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040071 operator++()
72 {
73 ++m_it;
74 return *this;
75 }
Yingdi Yub8f8b342015-04-27 11:06:42 -070076
77 const_iterator
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040078 operator++(int)
79 {
80 const_iterator it(*this);
81 ++m_it;
82 return it;
83 }
Yingdi Yub8f8b342015-04-27 11:06:42 -070084
85 bool
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040086 operator==(const const_iterator& other) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -070087
88 bool
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040089 operator!=(const const_iterator& other) const
90 {
91 return !this->operator==(other);
92 }
Yingdi Yub8f8b342015-04-27 11:06:42 -070093
94 private:
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040095 const_iterator(NameSet::const_iterator it, const KeyContainer& container) noexcept;
Yingdi Yub8f8b342015-04-27 11:06:42 -070096
97 private:
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040098 NameSet::const_iterator m_it;
99 const KeyContainer* m_container = nullptr;
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700100
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400101 friend KeyContainer;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700102 };
103
Davide Pesaventof2cae612021-03-24 18:47:05 -0400104 using iterator = const_iterator;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700105
106public:
Yingdi Yub8f8b342015-04-27 11:06:42 -0700107 const_iterator
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400108 begin() const noexcept
109 {
110 return {m_keyNames.begin(), *this};
111 }
Yingdi Yub8f8b342015-04-27 11:06:42 -0700112
113 const_iterator
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400114 end() const noexcept
115 {
116 return {};
117 }
Yingdi Yub8f8b342015-04-27 11:06:42 -0700118
119 const_iterator
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700120 find(const Name& keyName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700121
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400122 /**
123 * @brief Check whether the container is empty.
124 */
Davide Pesaventofcd3e442023-03-10 18:44:11 -0500125 [[nodiscard]] bool
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400126 empty() const noexcept
127 {
128 return m_keyNames.empty();
129 }
130
131 /**
132 * @brief Return the number of keys in the container.
133 */
Yingdi Yub8f8b342015-04-27 11:06:42 -0700134 size_t
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400135 size() const noexcept
136 {
137 return m_keyNames.size();
138 }
Yingdi Yub8f8b342015-04-27 11:06:42 -0700139
Yingdi Yucbe72b02015-11-25 17:35:37 -0800140 /**
Davide Pesavento765abc92021-12-27 00:44:04 -0500141 * @brief Add @p key with name @p keyName into the container.
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800142 *
Davide Pesavento765abc92021-12-27 00:44:04 -0500143 * If a key with the same name already exists, it will be overwritten.
144 *
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400145 * @throw std::invalid_argument @p keyName does not match the identity.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800146 */
147 Key
Davide Pesavento765abc92021-12-27 00:44:04 -0500148 add(span<const uint8_t> key, const Name& keyName);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800149
150 /**
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400151 * @brief Remove a key with @p keyName from the container.
152 * @throw std::invalid_argument @p keyName does not match the identity.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800153 */
154 void
155 remove(const Name& keyName);
156
157 /**
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400158 * @brief Return a key by name.
159 * @throw Pib::Error The key does not exist.
160 * @throw std::invalid_argument @p keyName does not match the identity.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800161 */
162 Key
163 get(const Name& keyName) const;
164
165 /**
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400166 * @brief Check if the container is consistent with the backend storage.
167 * @note This method is heavyweight and should be used in debugging mode only.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800168 */
169 bool
170 isConsistent() const;
171
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400172NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // private interface for IdentityImpl
Yingdi Yucbe72b02015-11-25 17:35:37 -0800173 /**
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400174 * @brief Create key container for @p identity.
Davide Pesavento50f66752017-05-15 20:57:12 -0400175 * @param pibImpl The PIB backend implementation.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800176 */
Davide Pesavento50f66752017-05-15 20:57:12 -0400177 KeyContainer(const Name& identity, shared_ptr<PibImpl> pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800178
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400179NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
180 // cache of loaded KeyImpl
Yingdi Yucbe72b02015-11-25 17:35:37 -0800181 mutable std::unordered_map<Name, shared_ptr<detail::KeyImpl>> m_keys;
182
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400183private:
184 NameSet m_keyNames;
185 const Name m_identity;
186 const shared_ptr<PibImpl> m_pib;
Davide Pesavento50f66752017-05-15 20:57:12 -0400187
Davide Pesaventof2cae612021-03-24 18:47:05 -0400188 friend detail::IdentityImpl;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700189};
190
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700191} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700192} // namespace security
193} // namespace ndn
194
Davide Pesavento09904412021-03-24 16:40:53 -0400195#endif // NDN_CXX_SECURITY_PIB_KEY_CONTAINER_HPP