ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
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-2023 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
27namespace ndn::security::pib {
28
29NDN_LOG_INIT(ndn.security.KeyContainer);
30
31bool
32KeyContainer::const_iterator::equals(const const_iterator& other) const noexcept
33{
34 bool isThisEnd = m_container == nullptr || m_it == m_container->m_keyNames.end();
35 bool isOtherEnd = other.m_container == nullptr || other.m_it == other.m_container->m_keyNames.end();
36 if (isThisEnd)
37 return isOtherEnd;
38 return !isOtherEnd && m_container->m_pib == other.m_container->m_pib && m_it == other.m_it;
39}
40
41KeyContainer::KeyContainer(const Name& identity, shared_ptr<PibImpl> pibImpl)
42 : m_identity(identity)
43 , m_pib(std::move(pibImpl))
44{
45 BOOST_ASSERT(m_pib != nullptr);
46 m_keyNames = m_pib->getKeysOfIdentity(identity);
47}
48
49KeyContainer::const_iterator
50KeyContainer::find(const Name& keyName) const
51{
52 return {m_keyNames.find(keyName), *this};
53}
54
55Key
56KeyContainer::add(span<const uint8_t> keyBits, const Name& keyName)
57{
58 if (m_identity != extractIdentityFromKeyName(keyName)) {
59 NDN_THROW(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity "
60 "`" + m_identity.toUri() + "`"));
61 }
62
63 bool isNew = m_keyNames.insert(keyName).second;
64 NDN_LOG_DEBUG((isNew ? "Adding " : "Replacing ") << keyName);
65 m_pib->addKey(m_identity, keyName, keyBits);
66
67 auto key = std::make_shared<KeyImpl>(keyName, Buffer(keyBits.begin(), keyBits.end()), m_pib);
68 m_keys[keyName] = key; // use insert_or_assign in C++17
69 return Key(key);
70}
71
72void
73KeyContainer::remove(const Name& keyName)
74{
75 if (m_identity != extractIdentityFromKeyName(keyName)) {
76 NDN_THROW(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity "
77 "`" + m_identity.toUri() + "`"));
78 }
79
80 if (m_keyNames.erase(keyName) > 0) {
81 NDN_LOG_DEBUG("Removing " << keyName);
82 m_keys.erase(keyName);
83 }
84 else {
85 // consistency check
86 BOOST_ASSERT(m_keys.find(keyName) == m_keys.end());
87 }
88 m_pib->removeKey(keyName);
89}
90
91Key
92KeyContainer::get(const Name& keyName) const
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 (auto it = m_keys.find(keyName); it != m_keys.end()) {
100 return Key(it->second);
101 }
102
103 // no need to check that the key exists in the backend
104 // because getKeyBits will throw if it doesn't
105 auto keyBits = m_pib->getKeyBits(keyName);
106
107 auto key = std::make_shared<KeyImpl>(keyName, std::move(keyBits), m_pib);
108 m_keys[keyName] = key;
109 return Key(key);
110}
111
112bool
113KeyContainer::isConsistent() const
114{
115 return m_keyNames == m_pib->getKeysOfIdentity(m_identity);
116}
117
118} // namespace ndn::security::pib
General-purpose automatically managed/resized buffer.
Definition buffer.hpp:43
Represents an absolute name.
Definition name.hpp:45
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:324
Frontend handle for a key in the PIB.
Definition key.hpp:45
#define NDN_THROW(e)
Definition exception.hpp:56
#define NDN_LOG_DEBUG(expression)
Log at DEBUG level.
Definition logger.hpp:260
#define NDN_LOG_INIT(name)
Define a non-member log module.
Definition logger.hpp:169
Contains the ndn-cxx security framework.
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
Definition key.cpp:141
Definition data.cpp:25
STL namespace.