blob: 34f96d0772ab696267d8f1946540a5afb7c8c50f [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 "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) :
74 m_container->m_impl == other.m_container->m_impl && 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
Yingdi Yucbe72b02015-11-25 17:35:37 -080083KeyContainer::KeyContainer(const Name& identity, shared_ptr<PibImpl> impl)
Yingdi Yub8f8b342015-04-27 11:06:42 -070084 : m_identity(identity)
Yingdi Yub8f8b342015-04-27 11:06:42 -070085 , m_impl(impl)
86{
Yingdi Yucbe72b02015-11-25 17:35:37 -080087 BOOST_ASSERT(impl != nullptr);
88 m_keyNames = impl->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
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
131void
132KeyContainer::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
144Key
145KeyContainer::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
166bool
167KeyContainer::isConsistent() const
168{
169 return m_keyNames == m_impl->getKeysOfIdentity(m_identity);
170}
171
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700172} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700173} // namespace security
174} // namespace ndn