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 "key-container.hpp" |
| 23 | #include "pib-impl.hpp" |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 24 | #include "detail/key-impl.hpp" |
| 25 | #include "util/concepts.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 29 | namespace pib { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 30 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 31 | NDN_CXX_ASSERT_FORWARD_ITERATOR(KeyContainer::const_iterator); |
| 32 | |
| 33 | KeyContainer::const_iterator::const_iterator() |
| 34 | : m_container(nullptr) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | KeyContainer::const_iterator::const_iterator(std::set<Name>::const_iterator it, |
| 39 | const KeyContainer& container) |
| 40 | : m_it(it) |
| 41 | , m_container(&container) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
| 45 | Key |
| 46 | KeyContainer::const_iterator::operator*() |
| 47 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 48 | BOOST_ASSERT(m_container != nullptr); |
| 49 | return m_container->get(*m_it); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | KeyContainer::const_iterator& |
| 53 | KeyContainer::const_iterator::operator++() |
| 54 | { |
| 55 | ++m_it; |
| 56 | return *this; |
| 57 | } |
| 58 | |
| 59 | KeyContainer::const_iterator |
| 60 | KeyContainer::const_iterator::operator++(int) |
| 61 | { |
| 62 | const_iterator it(*this); |
| 63 | ++m_it; |
| 64 | return it; |
| 65 | } |
| 66 | |
| 67 | bool |
| 68 | KeyContainer::const_iterator::operator==(const const_iterator& other) |
| 69 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 70 | bool isThisEnd = m_container == nullptr || m_it == m_container->m_keyNames.end(); |
| 71 | bool isOtherEnd = other.m_container == nullptr || other.m_it == other.m_container->m_keyNames.end(); |
| 72 | return ((isThisEnd || isOtherEnd) ? |
| 73 | (isThisEnd == isOtherEnd) : |
| 74 | m_container->m_impl == other.m_container->m_impl && m_it == other.m_it); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | bool |
| 78 | KeyContainer::const_iterator::operator!=(const const_iterator& other) |
| 79 | { |
| 80 | return !(*this == other); |
| 81 | } |
| 82 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 83 | KeyContainer::KeyContainer(const Name& identity, shared_ptr<PibImpl> impl) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 84 | : m_identity(identity) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 85 | , m_impl(impl) |
| 86 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 87 | BOOST_ASSERT(impl != nullptr); |
| 88 | m_keyNames = impl->getKeysOfIdentity(identity); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | KeyContainer::const_iterator |
| 92 | KeyContainer::begin() const |
| 93 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 94 | return const_iterator(m_keyNames.begin(), *this); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | KeyContainer::const_iterator |
| 98 | KeyContainer::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 | KeyContainer::const_iterator |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 104 | KeyContainer::find(const Name& keyName) const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 105 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 106 | return const_iterator(m_keyNames.find(keyName), *this); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | size_t |
| 110 | KeyContainer::size() const |
| 111 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 112 | return m_keyNames.size(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 115 | Key |
| 116 | KeyContainer::add(const uint8_t* key, size_t keyLen, const Name& keyName) |
| 117 | { |
| 118 | if (m_identity != v2::extractIdentityFromKeyName(keyName)) { |
| 119 | BOOST_THROW_EXCEPTION(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity " |
| 120 | "`" + m_identity.toUri() + "`")); |
| 121 | } |
| 122 | |
| 123 | if (m_keyNames.count(keyName) == 0) { |
| 124 | m_keyNames.insert(keyName); |
| 125 | m_keys[keyName] = shared_ptr<detail::KeyImpl>(new detail::KeyImpl(keyName, key, keyLen, m_impl)); |
| 126 | } |
| 127 | |
| 128 | return get(keyName); |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | KeyContainer::remove(const Name& keyName) |
| 133 | { |
| 134 | if (m_identity != v2::extractIdentityFromKeyName(keyName)) { |
| 135 | BOOST_THROW_EXCEPTION(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity " |
| 136 | "`" + m_identity.toUri() + "`")); |
| 137 | } |
| 138 | |
| 139 | m_keyNames.erase(keyName); |
| 140 | m_keys.erase(keyName); |
| 141 | m_impl->removeKey(keyName); |
| 142 | } |
| 143 | |
| 144 | Key |
| 145 | KeyContainer::get(const Name& keyName) const |
| 146 | { |
| 147 | if (m_identity != v2::extractIdentityFromKeyName(keyName)) { |
| 148 | BOOST_THROW_EXCEPTION(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity " |
| 149 | "`" + m_identity.toUri() + "`")); |
| 150 | } |
| 151 | |
| 152 | shared_ptr<detail::KeyImpl> key; |
| 153 | auto it = m_keys.find(keyName); |
| 154 | |
| 155 | if (it != m_keys.end()) { |
| 156 | key = it->second; |
| 157 | } |
| 158 | else { |
| 159 | key = shared_ptr<detail::KeyImpl>(new detail::KeyImpl(keyName, m_impl)); |
| 160 | m_keys[keyName] = key; |
| 161 | } |
| 162 | |
| 163 | return Key(key); |
| 164 | } |
| 165 | |
| 166 | bool |
| 167 | KeyContainer::isConsistent() const |
| 168 | { |
| 169 | return m_keyNames == m_impl->getKeysOfIdentity(m_identity); |
| 170 | } |
| 171 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 172 | } // namespace pib |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 173 | } // namespace security |
| 174 | } // namespace ndn |