blob: af01dc50b00d2fc74e7fdca5ec7029deba5f9a10 [file] [log] [blame]
Yingdi Yucbe72b02015-11-25 17:35:37 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2017 Regents of the University of California.
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 "identity-impl.hpp"
23#include "../pib-impl.hpp"
24#include "../pib.hpp"
25
26namespace ndn {
27namespace security {
28namespace pib {
29namespace detail {
30
31IdentityImpl::IdentityImpl(const Name& identityName, shared_ptr<PibImpl> impl, bool needInit)
32 : m_name(identityName)
33 , m_isDefaultKeyLoaded(false)
34 , m_keys(identityName, impl)
35 , m_impl(impl)
36{
37 BOOST_ASSERT(impl != nullptr);
38
39 if (needInit) {
40 m_impl->addIdentity(m_name);
41 }
42 else if (!m_impl->hasIdentity(m_name)) {
43 BOOST_THROW_EXCEPTION(Pib::Error("Identity " + m_name.toUri() + " does not exist"));
44 }
45}
46
47Key
48IdentityImpl::addKey(const uint8_t* key, size_t keyLen, const Name& keyName)
49{
50 BOOST_ASSERT(m_keys.isConsistent());
51
52 if (m_keys.find(keyName) != m_keys.end()) {
53 BOOST_THROW_EXCEPTION(Pib::Error("Cannot overwrite existing key " + keyName.toUri()));
54 }
55
56 return m_keys.add(key, keyLen, keyName);
57}
58
59void
60IdentityImpl::removeKey(const Name& keyName)
61{
62 BOOST_ASSERT(m_keys.isConsistent());
63
64 if (m_isDefaultKeyLoaded && m_defaultKey.getName() == keyName)
65 m_isDefaultKeyLoaded = false;
66
67 m_keys.remove(keyName);
68}
69
70Key
71IdentityImpl::getKey(const Name& keyName) const
72{
73 BOOST_ASSERT(m_keys.isConsistent());
74
75 return m_keys.get(keyName);
76}
77
78const KeyContainer&
79IdentityImpl::getKeys() const
80{
81 BOOST_ASSERT(m_keys.isConsistent());
82
83 return m_keys;
84}
85
86const Key&
87IdentityImpl::setDefaultKey(const Name& keyName)
88{
89 BOOST_ASSERT(m_keys.isConsistent());
90
91 m_defaultKey = m_keys.get(keyName);
92 m_isDefaultKeyLoaded = true;
93 m_impl->setDefaultKeyOfIdentity(m_name, keyName);
94 return m_defaultKey;
95}
96
97const Key&
98IdentityImpl::setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName)
99{
100 addKey(key, keyLen, keyName);
101 return setDefaultKey(keyName);
102}
103
104const Key&
105IdentityImpl::getDefaultKey() const
106{
107 BOOST_ASSERT(m_keys.isConsistent());
108
109 if (!m_isDefaultKeyLoaded) {
110 m_defaultKey = m_keys.get(m_impl->getDefaultKeyOfIdentity(m_name));
111 m_isDefaultKeyLoaded = true;
112 }
113
114 BOOST_ASSERT(m_impl->getDefaultKeyOfIdentity(m_name) == m_defaultKey.getName());
115
116 return m_defaultKey;
117}
118
119} // namespace detail
120} // namespace pib
121} // namespace security
122} // namespace ndn