Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 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 | |
| 22 | #include "certificate-container.hpp" |
| 23 | #include "pib-impl.hpp" |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 24 | #include "util/concepts.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 28 | namespace pib { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 29 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 30 | NDN_CXX_ASSERT_FORWARD_ITERATOR(CertificateContainer::const_iterator); |
| 31 | |
| 32 | CertificateContainer::const_iterator::const_iterator() |
| 33 | : m_container(nullptr) |
| 34 | { |
| 35 | } |
| 36 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 37 | CertificateContainer::const_iterator::const_iterator(std::set<Name>::const_iterator it, |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 38 | const CertificateContainer& container) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 39 | : m_it(it) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 40 | , m_container(&container) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 44 | v2::Certificate |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 45 | CertificateContainer::const_iterator::operator*() |
| 46 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 47 | BOOST_ASSERT(m_container != nullptr); |
| 48 | return m_container->get(*m_it); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | CertificateContainer::const_iterator& |
| 52 | CertificateContainer::const_iterator::operator++() |
| 53 | { |
| 54 | ++m_it; |
| 55 | return *this; |
| 56 | } |
| 57 | |
| 58 | CertificateContainer::const_iterator |
| 59 | CertificateContainer::const_iterator::operator++(int) |
| 60 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 61 | BOOST_ASSERT(m_container != nullptr); |
| 62 | const_iterator it(m_it, *m_container); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 63 | ++m_it; |
| 64 | return it; |
| 65 | } |
| 66 | |
| 67 | bool |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 68 | CertificateContainer::const_iterator::operator==(const const_iterator& other) const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 69 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 70 | 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 Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 74 | m_container->m_pib == other.m_container->m_pib && m_it == other.m_it); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | bool |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 78 | CertificateContainer::const_iterator::operator!=(const const_iterator& other) const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 79 | { |
| 80 | return !(*this == other); |
| 81 | } |
| 82 | |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 83 | CertificateContainer::CertificateContainer(const Name& keyName, shared_ptr<PibImpl> pibImpl) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 84 | : m_keyName(keyName) |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 85 | , m_pib(std::move(pibImpl)) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 86 | { |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 87 | BOOST_ASSERT(m_pib != nullptr); |
| 88 | m_certNames = m_pib->getCertificatesOfKey(keyName); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | CertificateContainer::const_iterator |
| 92 | CertificateContainer::begin() const |
| 93 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 94 | return const_iterator(m_certNames.begin(), *this); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | CertificateContainer::const_iterator |
| 98 | CertificateContainer::end() const |
| 99 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 100 | return const_iterator(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | CertificateContainer::const_iterator |
| 104 | CertificateContainer::find(const Name& certName) const |
| 105 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 106 | return const_iterator(m_certNames.find(certName), *this); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | size_t |
| 110 | CertificateContainer::size() const |
| 111 | { |
| 112 | return m_certNames.size(); |
| 113 | } |
| 114 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 115 | void |
| 116 | CertificateContainer::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 Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 125 | m_pib->addCertificate(certificate); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void |
| 129 | CertificateContainer::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 Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 139 | m_pib->removeCertificate(certName); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | v2::Certificate |
| 143 | CertificateContainer::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 Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 156 | m_certs[certName] = m_pib->getCertificate(certName); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 157 | return m_certs[certName]; |
| 158 | } |
| 159 | |
| 160 | bool |
| 161 | CertificateContainer::isConsistent() const |
| 162 | { |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 163 | return m_certNames == m_pib->getCertificatesOfKey(m_keyName); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 166 | } // namespace pib |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 167 | } // namespace security |
| 168 | } // namespace ndn |