key-container.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2021 Regents of the University of California.
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 
23 #include "ndn-cxx/security/pib/impl/key-impl.hpp"
26 
27 namespace ndn {
28 namespace security {
29 namespace pib {
30 
31 NDN_CXX_ASSERT_FORWARD_ITERATOR(KeyContainer::const_iterator);
32 
34  : m_container(nullptr)
35 {
36 }
37 
38 KeyContainer::const_iterator::const_iterator(std::set<Name>::const_iterator it,
39  const KeyContainer& container)
40  : m_it(it)
41  , m_container(&container)
42 {
43 }
44 
45 Key
47 {
48  BOOST_ASSERT(m_container != nullptr);
49  return m_container->get(*m_it);
50 }
51 
54 {
55  ++m_it;
56  return *this;
57 }
58 
61 {
62  const_iterator it(*this);
63  ++m_it;
64  return it;
65 }
66 
67 bool
69 {
70  bool isThisEnd = m_container == nullptr || m_it == m_container->m_keyNames.end();
71  bool isOtherEnd = other.m_container == nullptr || other.m_it == other.m_container->m_keyNames.end();
72  return ((isThisEnd || isOtherEnd) ?
73  (isThisEnd == isOtherEnd) :
74  m_container->m_pib == other.m_container->m_pib && m_it == other.m_it);
75 }
76 
77 bool
79 {
80  return !(*this == other);
81 }
82 
83 KeyContainer::KeyContainer(const Name& identity, shared_ptr<PibImpl> pibImpl)
84  : m_identity(identity)
85  , m_pib(std::move(pibImpl))
86 {
87  BOOST_ASSERT(m_pib != nullptr);
88  m_keyNames = m_pib->getKeysOfIdentity(identity);
89 }
90 
93 {
94  return {m_keyNames.begin(), *this};
95 }
96 
99 {
100  return {};
101 }
102 
104 KeyContainer::find(const Name& keyName) const
105 {
106  return {m_keyNames.find(keyName), *this};
107 }
108 
109 size_t
111 {
112  return m_keyNames.size();
113 }
114 
115 Key
116 KeyContainer::add(span<const uint8_t> key, const Name& keyName)
117 {
118  if (m_identity != extractIdentityFromKeyName(keyName)) {
119  NDN_THROW(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity "
120  "`" + m_identity.toUri() + "`"));
121  }
122 
123  m_keyNames.insert(keyName);
124  m_keys[keyName] = make_shared<detail::KeyImpl>(keyName, key, m_pib);
125  return get(keyName);
126 }
127 
128 void
129 KeyContainer::remove(const Name& keyName)
130 {
131  if (m_identity != extractIdentityFromKeyName(keyName)) {
132  NDN_THROW(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity "
133  "`" + m_identity.toUri() + "`"));
134  }
135 
136  m_keyNames.erase(keyName);
137  m_keys.erase(keyName);
138  m_pib->removeKey(keyName);
139 }
140 
141 Key
142 KeyContainer::get(const Name& keyName) const
143 {
144  if (m_identity != extractIdentityFromKeyName(keyName)) {
145  NDN_THROW(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity "
146  "`" + m_identity.toUri() + "`"));
147  }
148 
149  shared_ptr<detail::KeyImpl> key;
150  auto it = m_keys.find(keyName);
151 
152  if (it != m_keys.end()) {
153  key = it->second;
154  }
155  else {
156  key = make_shared<detail::KeyImpl>(keyName, m_pib);
157  m_keys[keyName] = key;
158  }
159 
160  return Key(key);
161 }
162 
163 bool
165 {
166  return m_keyNames == m_pib->getKeysOfIdentity(m_identity);
167 }
168 
169 } // namespace pib
170 } // namespace security
171 } // namespace ndn
Represents an absolute name.
Definition: name.hpp:46
void toUri(std::ostream &os, name::UriFormat format=name::UriFormat::DEFAULT) const
Write URI representation of the name to the output stream.
Definition: name.cpp:348
bool operator!=(const const_iterator &other)
bool operator==(const const_iterator &other)
Container of keys of an identity.
Key add(span< const uint8_t > key, const Name &keyName)
Add key with name keyName into the container.
Key get(const Name &keyName) const
Get a key with keyName from the container.
const_iterator find(const Name &keyName) const
bool isConsistent() const
Check if the container is consistent with the backend storage.
const_iterator begin() const
const_iterator end() const
void remove(const Name &keyName)
Remove a key with keyName from the container.
A frontend handle of a key instance.
Definition: key.hpp:50
#define NDN_CXX_ASSERT_FORWARD_ITERATOR(T)
assert T is a forward iterator
Definition: concepts.hpp:147
#define NDN_THROW(e)
Definition: exception.hpp:61
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
Definition: key.cpp:160
Definition: data.cpp:25