blob: a671cb012b6fac35b3506024dbe194438b58ff83 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu6ee2d362015-07-16 21:48:05 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yub8f8b342015-04-27 11:06:42 -07004 *
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
22#include "certificate-container.hpp"
23#include "pib-impl.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080024#include "util/concepts.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070025
26namespace ndn {
27namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070028namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070029
Yingdi Yucbe72b02015-11-25 17:35:37 -080030NDN_CXX_ASSERT_FORWARD_ITERATOR(CertificateContainer::const_iterator);
31
32CertificateContainer::const_iterator::const_iterator()
33 : m_container(nullptr)
34{
35}
36
Yingdi Yub8f8b342015-04-27 11:06:42 -070037CertificateContainer::const_iterator::const_iterator(std::set<Name>::const_iterator it,
Yingdi Yucbe72b02015-11-25 17:35:37 -080038 const CertificateContainer& container)
Yingdi Yub8f8b342015-04-27 11:06:42 -070039 : m_it(it)
Yingdi Yucbe72b02015-11-25 17:35:37 -080040 , m_container(&container)
Yingdi Yub8f8b342015-04-27 11:06:42 -070041{
42}
43
Yingdi Yu6ee2d362015-07-16 21:48:05 -070044v2::Certificate
Yingdi Yub8f8b342015-04-27 11:06:42 -070045CertificateContainer::const_iterator::operator*()
46{
Yingdi Yucbe72b02015-11-25 17:35:37 -080047 BOOST_ASSERT(m_container != nullptr);
48 return m_container->get(*m_it);
Yingdi Yub8f8b342015-04-27 11:06:42 -070049}
50
51CertificateContainer::const_iterator&
52CertificateContainer::const_iterator::operator++()
53{
54 ++m_it;
55 return *this;
56}
57
58CertificateContainer::const_iterator
59CertificateContainer::const_iterator::operator++(int)
60{
Yingdi Yucbe72b02015-11-25 17:35:37 -080061 BOOST_ASSERT(m_container != nullptr);
62 const_iterator it(m_it, *m_container);
Yingdi Yub8f8b342015-04-27 11:06:42 -070063 ++m_it;
64 return it;
65}
66
67bool
Yingdi Yucbe72b02015-11-25 17:35:37 -080068CertificateContainer::const_iterator::operator==(const const_iterator& other) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070069{
Yingdi Yucbe72b02015-11-25 17:35:37 -080070 bool isThisEnd = m_container == nullptr || m_it == m_container->m_certNames.end();
71 bool isOtherEnd = other.m_container == nullptr || other.m_it == other.m_container->m_certNames.end();
72 return ((isThisEnd || isOtherEnd) ?
73 (isThisEnd == isOtherEnd) :
Davide Pesavento50f66752017-05-15 20:57:12 -040074 m_container->m_pib == other.m_container->m_pib && m_it == other.m_it);
Yingdi Yub8f8b342015-04-27 11:06:42 -070075}
76
77bool
Yingdi Yucbe72b02015-11-25 17:35:37 -080078CertificateContainer::const_iterator::operator!=(const const_iterator& other) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070079{
80 return !(*this == other);
81}
82
Davide Pesavento50f66752017-05-15 20:57:12 -040083CertificateContainer::CertificateContainer(const Name& keyName, shared_ptr<PibImpl> pibImpl)
Yingdi Yucbe72b02015-11-25 17:35:37 -080084 : m_keyName(keyName)
Davide Pesavento50f66752017-05-15 20:57:12 -040085 , m_pib(std::move(pibImpl))
Yingdi Yub8f8b342015-04-27 11:06:42 -070086{
Davide Pesavento50f66752017-05-15 20:57:12 -040087 BOOST_ASSERT(m_pib != nullptr);
88 m_certNames = m_pib->getCertificatesOfKey(keyName);
Yingdi Yub8f8b342015-04-27 11:06:42 -070089}
90
91CertificateContainer::const_iterator
92CertificateContainer::begin() const
93{
Yingdi Yucbe72b02015-11-25 17:35:37 -080094 return const_iterator(m_certNames.begin(), *this);
Yingdi Yub8f8b342015-04-27 11:06:42 -070095}
96
97CertificateContainer::const_iterator
98CertificateContainer::end() const
99{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800100 return const_iterator();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700101}
102
103CertificateContainer::const_iterator
104CertificateContainer::find(const Name& certName) const
105{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800106 return const_iterator(m_certNames.find(certName), *this);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700107}
108
109size_t
110CertificateContainer::size() const
111{
112 return m_certNames.size();
113}
114
Yingdi Yucbe72b02015-11-25 17:35:37 -0800115void
116CertificateContainer::add(const v2::Certificate& certificate)
117{
118 if (m_keyName != certificate.getKeyName())
119 BOOST_THROW_EXCEPTION(std::invalid_argument("Certificate name `" + certificate.getKeyName().toUri() + "` "
120 "does not match key name"));
121
122 const Name& certName = certificate.getName();
123 m_certNames.insert(certName);
124 m_certs[certName] = certificate;
Davide Pesavento50f66752017-05-15 20:57:12 -0400125 m_pib->addCertificate(certificate);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800126}
127
128void
129CertificateContainer::remove(const Name& certName)
130{
131 if (!v2::Certificate::isValidName(certName) ||
132 v2::extractKeyNameFromCertName(certName) != m_keyName) {
133 BOOST_THROW_EXCEPTION(std::invalid_argument("Certificate name `" + certName.toUri() + "` "
134 "is invalid or does not match key name"));
135 }
136
137 m_certNames.erase(certName);
138 m_certs.erase(certName);
Davide Pesavento50f66752017-05-15 20:57:12 -0400139 m_pib->removeCertificate(certName);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800140}
141
142v2::Certificate
143CertificateContainer::get(const Name& certName) const
144{
145 auto it = m_certs.find(certName);
146
147 if (it != m_certs.end())
148 return it->second;
149
150 if (!v2::Certificate::isValidName(certName) ||
151 v2::extractKeyNameFromCertName(certName) != m_keyName) {
152 BOOST_THROW_EXCEPTION(std::invalid_argument("Certificate name `" + certName.toUri() + "` "
153 "is invalid or does not match key name"));
154 }
155
Davide Pesavento50f66752017-05-15 20:57:12 -0400156 m_certs[certName] = m_pib->getCertificate(certName);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800157 return m_certs[certName];
158}
159
160bool
161CertificateContainer::isConsistent() const
162{
Davide Pesavento50f66752017-05-15 20:57:12 -0400163 return m_certNames == m_pib->getCertificatesOfKey(m_keyName);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800164}
165
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700166} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700167} // namespace security
168} // namespace ndn