certificate-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 
24 #include "ndn-cxx/util/logger.hpp"
25 
26 namespace ndn::security::pib {
27 
28 NDN_LOG_INIT(ndn.security.CertificateContainer);
29 
30 bool
31 CertificateContainer::const_iterator::equals(const const_iterator& other) const noexcept
32 {
33  bool isThisEnd = m_container == nullptr || m_it == m_container->m_certNames.end();
34  bool isOtherEnd = other.m_container == nullptr || other.m_it == other.m_container->m_certNames.end();
35  if (isThisEnd)
36  return isOtherEnd;
37  return !isOtherEnd && m_container->m_pib == other.m_container->m_pib && m_it == other.m_it;
38 }
39 
40 CertificateContainer::CertificateContainer(const Name& keyName, shared_ptr<PibImpl> pibImpl)
41  : m_keyName(keyName)
42  , m_pib(std::move(pibImpl))
43 {
44  BOOST_ASSERT(m_pib != nullptr);
45  m_certNames = m_pib->getCertificatesOfKey(keyName);
46 }
47 
48 CertificateContainer::const_iterator
49 CertificateContainer::find(const Name& certName) const
50 {
51  return {m_certNames.find(certName), *this};
52 }
53 
54 void
55 CertificateContainer::add(const Certificate& certificate)
56 {
57  if (m_keyName != certificate.getKeyName()) {
58  NDN_THROW(std::invalid_argument("Certificate name `" + certificate.getName().toUri() + "` "
59  "does not match key `" + m_keyName.toUri() + "`"));
60  }
61 
62  const Name& certName = certificate.getName();
63  bool isNew = m_certNames.insert(certName).second;
64  NDN_LOG_DEBUG((isNew ? "Adding " : "Replacing ") << certName);
65 
66  m_pib->addCertificate(certificate);
67  m_certs[certName] = certificate; // use insert_or_assign in C++17
68 }
69 
70 void
71 CertificateContainer::remove(const Name& certName)
72 {
73  if (m_keyName != extractKeyNameFromCertName(certName)) {
74  NDN_THROW(std::invalid_argument("Certificate name `" + certName.toUri() + "` "
75  "does not match key `" + m_keyName.toUri() + "`"));
76  }
77 
78  if (m_certNames.erase(certName) > 0) {
79  NDN_LOG_DEBUG("Removing " << certName);
80  m_certs.erase(certName);
81  }
82  else {
83  // consistency check
84  BOOST_ASSERT(m_certs.find(certName) == m_certs.end());
85  }
86  m_pib->removeCertificate(certName);
87 }
88 
90 CertificateContainer::get(const Name& certName) const
91 {
92  if (m_keyName != extractKeyNameFromCertName(certName)) {
93  NDN_THROW(std::invalid_argument("Certificate name `" + certName.toUri() + "` "
94  "does not match key `" + m_keyName.toUri() + "`"));
95  }
96 
97  if (auto it = m_certs.find(certName); it != m_certs.end()) {
98  return it->second;
99  }
100 
101  auto ret = m_certs.emplace(certName, m_pib->getCertificate(certName));
102  return ret.first->second;
103 }
104 
105 bool
106 CertificateContainer::isConsistent() const
107 {
108  return m_certNames == m_pib->getCertificatesOfKey(m_keyName);
109 }
110 
111 } // namespace ndn::security::pib
const Name & getName() const noexcept
Get the Data name.
Definition: data.hpp:137
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
Represents an NDN certificate.
Definition: certificate.hpp:58
Name getKeyName() const
Get key name.
Definition: certificate.cpp:72
#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 extractKeyNameFromCertName(const Name &certName)
Extract key name from the certificate name certName.
@ Name
Definition: tlv.hpp:71
Definition: data.cpp:25