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-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 
25 #include "ndn-cxx/util/logger.hpp"
26 
27 namespace ndn {
28 namespace security {
29 namespace pib {
30 
31 NDN_LOG_INIT(ndn.security.CertificateContainer);
32 
33 NDN_CXX_ASSERT_FORWARD_ITERATOR(CertificateContainer::const_iterator);
34 
36  const CertificateContainer& container) noexcept
37  : m_it(it)
38  , m_container(&container)
39 {
40 }
41 
42 Certificate
44 {
45  BOOST_ASSERT(m_container != nullptr);
46  return m_container->get(*m_it);
47 }
48 
49 bool
51 {
52  bool isThisEnd = m_container == nullptr || m_it == m_container->m_certNames.end();
53  bool isOtherEnd = other.m_container == nullptr || other.m_it == other.m_container->m_certNames.end();
54  if (isThisEnd)
55  return isOtherEnd;
56  return !isOtherEnd && m_container->m_pib == other.m_container->m_pib && m_it == other.m_it;
57 }
58 
59 CertificateContainer::CertificateContainer(const Name& keyName, shared_ptr<PibImpl> pibImpl)
60  : m_keyName(keyName)
61  , m_pib(std::move(pibImpl))
62 {
63  BOOST_ASSERT(m_pib != nullptr);
64  m_certNames = m_pib->getCertificatesOfKey(keyName);
65 }
66 
68 CertificateContainer::find(const Name& certName) const
69 {
70  return {m_certNames.find(certName), *this};
71 }
72 
73 void
75 {
76  if (m_keyName != certificate.getKeyName()) {
77  NDN_THROW(std::invalid_argument("Certificate name `" + certificate.getName().toUri() + "` "
78  "does not match key `" + m_keyName.toUri() + "`"));
79  }
80 
81  const Name& certName = certificate.getName();
82  bool isNew = m_certNames.insert(certName).second;
83  NDN_LOG_DEBUG((isNew ? "Adding " : "Replacing ") << certName);
84 
85  m_pib->addCertificate(certificate);
86  m_certs[certName] = certificate; // use insert_or_assign in C++17
87 }
88 
89 void
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 (m_certNames.erase(certName) > 0) {
98  NDN_LOG_DEBUG("Removing " << certName);
99  m_certs.erase(certName);
100  }
101  else {
102  // consistency check
103  BOOST_ASSERT(m_certs.find(certName) == m_certs.end());
104  }
105  m_pib->removeCertificate(certName);
106 }
107 
109 CertificateContainer::get(const Name& certName) const
110 {
111  if (m_keyName != extractKeyNameFromCertName(certName)) {
112  NDN_THROW(std::invalid_argument("Certificate name `" + certName.toUri() + "` "
113  "does not match key `" + m_keyName.toUri() + "`"));
114  }
115 
116  auto it = m_certs.find(certName);
117  if (it != m_certs.end()) {
118  return it->second;
119  }
120 
121  auto ret = m_certs.emplace(certName, m_pib->getCertificate(certName));
122  return ret.first->second;
123 }
124 
125 bool
127 {
128  return m_certNames == m_pib->getCertificatesOfKey(m_keyName);
129 }
130 
131 } // namespace pib
132 } // namespace security
133 } // namespace ndn
const Name & getName() const noexcept
Get the data name.
Definition: data.hpp:129
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
const_iterator find(const Name &certName) const
Certificate get(const Name &certName) const
Return a certificate by name.
void remove(const Name &certName)
Remove a certificate with certName from the container.
bool isConsistent() const
Check if the container is consistent with the backend storage.
void add(const Certificate &certificate)
Add certificate into the container.
Represents an NDN certificate.
Definition: certificate.hpp:60
Name getKeyName() const
Get key name.
Definition: certificate.cpp:86
#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 extractKeyNameFromCertName(const Name &certName)
Extract key name from the certificate name certName.
Definition: data.cpp:25