blob: ee47e37b4d4e1bb3768ad80718c2a83c8295123b [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento3b101d02018-07-21 22:44:09 -04002/*
3 * Copyright (c) 2013-2018 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 "key-container.hpp"
23#include "pib-impl.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080024#include "detail/key-impl.hpp"
25#include "util/concepts.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070026
27namespace ndn {
28namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070029namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070030
Yingdi Yucbe72b02015-11-25 17:35:37 -080031NDN_CXX_ASSERT_FORWARD_ITERATOR(KeyContainer::const_iterator);
32
33KeyContainer::const_iterator::const_iterator()
34 : m_container(nullptr)
35{
36}
37
38KeyContainer::const_iterator::const_iterator(std::set<Name>::const_iterator it,
39 const KeyContainer& container)
40 : m_it(it)
41 , m_container(&container)
Yingdi Yub8f8b342015-04-27 11:06:42 -070042{
43}
44
45Key
46KeyContainer::const_iterator::operator*()
47{
Yingdi Yucbe72b02015-11-25 17:35:37 -080048 BOOST_ASSERT(m_container != nullptr);
49 return m_container->get(*m_it);
Yingdi Yub8f8b342015-04-27 11:06:42 -070050}
51
52KeyContainer::const_iterator&
53KeyContainer::const_iterator::operator++()
54{
55 ++m_it;
56 return *this;
57}
58
59KeyContainer::const_iterator
60KeyContainer::const_iterator::operator++(int)
61{
62 const_iterator it(*this);
63 ++m_it;
64 return it;
65}
66
67bool
68KeyContainer::const_iterator::operator==(const const_iterator& other)
69{
Yingdi Yucbe72b02015-11-25 17:35:37 -080070 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) :
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
78KeyContainer::const_iterator::operator!=(const const_iterator& other)
79{
80 return !(*this == other);
81}
82
Davide Pesavento50f66752017-05-15 20:57:12 -040083KeyContainer::KeyContainer(const Name& identity, shared_ptr<PibImpl> pibImpl)
Yingdi Yub8f8b342015-04-27 11:06:42 -070084 : m_identity(identity)
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_keyNames = m_pib->getKeysOfIdentity(identity);
Yingdi Yub8f8b342015-04-27 11:06:42 -070089}
90
91KeyContainer::const_iterator
92KeyContainer::begin() const
93{
Yingdi Yucbe72b02015-11-25 17:35:37 -080094 return const_iterator(m_keyNames.begin(), *this);
Yingdi Yub8f8b342015-04-27 11:06:42 -070095}
96
97KeyContainer::const_iterator
98KeyContainer::end() const
99{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800100 return const_iterator();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700101}
102
103KeyContainer::const_iterator
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700104KeyContainer::find(const Name& keyName) const
Yingdi Yub8f8b342015-04-27 11:06:42 -0700105{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800106 return const_iterator(m_keyNames.find(keyName), *this);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700107}
108
109size_t
110KeyContainer::size() const
111{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700112 return m_keyNames.size();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700113}
114
Yingdi Yucbe72b02015-11-25 17:35:37 -0800115Key
116KeyContainer::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
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800123 m_keyNames.insert(keyName);
Davide Pesavento3b101d02018-07-21 22:44:09 -0400124 m_keys[keyName] = make_shared<detail::KeyImpl>(keyName, key, keyLen, m_pib);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800125
126 return get(keyName);
127}
128
129void
130KeyContainer::remove(const Name& keyName)
131{
132 if (m_identity != v2::extractIdentityFromKeyName(keyName)) {
133 BOOST_THROW_EXCEPTION(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity "
134 "`" + m_identity.toUri() + "`"));
135 }
136
137 m_keyNames.erase(keyName);
138 m_keys.erase(keyName);
Davide Pesavento50f66752017-05-15 20:57:12 -0400139 m_pib->removeKey(keyName);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800140}
141
142Key
143KeyContainer::get(const Name& keyName) const
144{
145 if (m_identity != v2::extractIdentityFromKeyName(keyName)) {
146 BOOST_THROW_EXCEPTION(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity "
147 "`" + m_identity.toUri() + "`"));
148 }
149
150 shared_ptr<detail::KeyImpl> key;
151 auto it = m_keys.find(keyName);
152
153 if (it != m_keys.end()) {
154 key = it->second;
155 }
156 else {
Davide Pesavento3b101d02018-07-21 22:44:09 -0400157 key = make_shared<detail::KeyImpl>(keyName, m_pib);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800158 m_keys[keyName] = key;
159 }
160
161 return Key(key);
162}
163
164bool
165KeyContainer::isConsistent() const
166{
Davide Pesavento50f66752017-05-15 20:57:12 -0400167 return m_keyNames == m_pib->getKeysOfIdentity(m_identity);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800168}
169
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700170} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700171} // namespace security
172} // namespace ndn