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-2022 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 #include "ndn-cxx/util/logger.hpp"
27 
28 namespace ndn {
29 namespace security {
30 namespace pib {
31 
32 NDN_LOG_INIT(ndn.security.KeyContainer);
33 
34 NDN_CXX_ASSERT_FORWARD_ITERATOR(KeyContainer::const_iterator);
35 
36 KeyContainer::const_iterator::const_iterator(NameSet::const_iterator it,
37  const KeyContainer& container) noexcept
38  : m_it(it)
39  , m_container(&container)
40 {
41 }
42 
43 Key
45 {
46  BOOST_ASSERT(m_container != nullptr);
47  return m_container->get(*m_it);
48 }
49 
50 bool
52 {
53  bool isThisEnd = m_container == nullptr || m_it == m_container->m_keyNames.end();
54  bool isOtherEnd = other.m_container == nullptr || other.m_it == other.m_container->m_keyNames.end();
55  if (isThisEnd)
56  return isOtherEnd;
57  return !isOtherEnd && m_container->m_pib == other.m_container->m_pib && m_it == other.m_it;
58 }
59 
60 KeyContainer::KeyContainer(const Name& identity, shared_ptr<PibImpl> pibImpl)
61  : m_identity(identity)
62  , m_pib(std::move(pibImpl))
63 {
64  BOOST_ASSERT(m_pib != nullptr);
65  m_keyNames = m_pib->getKeysOfIdentity(identity);
66 }
67 
69 KeyContainer::find(const Name& keyName) const
70 {
71  return {m_keyNames.find(keyName), *this};
72 }
73 
74 Key
75 KeyContainer::add(span<const uint8_t> keyBits, const Name& keyName)
76 {
77  if (m_identity != extractIdentityFromKeyName(keyName)) {
78  NDN_THROW(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity "
79  "`" + m_identity.toUri() + "`"));
80  }
81 
82  bool isNew = m_keyNames.insert(keyName).second;
83  NDN_LOG_DEBUG((isNew ? "Adding " : "Replacing ") << keyName);
84  m_pib->addKey(m_identity, keyName, keyBits);
85 
86  auto key = std::make_shared<detail::KeyImpl>(keyName, Buffer(keyBits.begin(), keyBits.end()), m_pib);
87  m_keys[keyName] = key; // use insert_or_assign in C++17
88  return Key(key);
89 }
90 
91 void
92 KeyContainer::remove(const Name& keyName)
93 {
94  if (m_identity != extractIdentityFromKeyName(keyName)) {
95  NDN_THROW(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity "
96  "`" + m_identity.toUri() + "`"));
97  }
98 
99  if (m_keyNames.erase(keyName) > 0) {
100  NDN_LOG_DEBUG("Removing " << keyName);
101  m_keys.erase(keyName);
102  }
103  else {
104  // consistency check
105  BOOST_ASSERT(m_keys.find(keyName) == m_keys.end());
106  }
107  m_pib->removeKey(keyName);
108 }
109 
110 Key
111 KeyContainer::get(const Name& keyName) const
112 {
113  if (m_identity != extractIdentityFromKeyName(keyName)) {
114  NDN_THROW(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity "
115  "`" + m_identity.toUri() + "`"));
116  }
117 
118  auto it = m_keys.find(keyName);
119  if (it != m_keys.end()) {
120  return Key(it->second);
121  }
122 
123  // no need to check that the key exists in the backend
124  // because getKeyBits will throw if it doesn't
125  auto keyBits = m_pib->getKeyBits(keyName);
126 
127  auto key = std::make_shared<detail::KeyImpl>(keyName, std::move(keyBits), m_pib);
128  m_keys[keyName] = key;
129  return Key(key);
130 }
131 
132 bool
134 {
135  return m_keyNames == m_pib->getKeysOfIdentity(m_identity);
136 }
137 
138 } // namespace pib
139 } // namespace security
140 } // namespace ndn
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:42
Represents an absolute name.
Definition: name.hpp:44
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:349
bool operator==(const const_iterator &other) const
Key add(span< const uint8_t > key, const Name &keyName)
Add key with name keyName into the container.
Key get(const Name &keyName) const
Return a key by name.
const_iterator find(const Name &keyName) const
bool isConsistent() const
Check if the container is consistent with the backend storage.
void remove(const Name &keyName)
Remove a key with keyName from the container.
Frontend handle for a key in the PIB.
Definition: key.hpp:51
#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
#define NDN_LOG_DEBUG(expression)
Log at DEBUG level.
Definition: logger.hpp:254
#define NDN_LOG_INIT(name)
Define a non-member log module.
Definition: logger.hpp:163
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
Definition: key.cpp:144
Definition: data.cpp:25