blob: 45b07b0ac988af49779a37e98e5cb119cd5b081f [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
Davide Pesavento50f66752017-05-15 20:57:12 -040031IdentityImpl::IdentityImpl(const Name& identityName, shared_ptr<PibImpl> pibImpl, bool needInit)
Yingdi Yucbe72b02015-11-25 17:35:37 -080032 : m_name(identityName)
Davide Pesavento50f66752017-05-15 20:57:12 -040033 , m_pib(std::move(pibImpl))
34 , m_keys(identityName, m_pib)
Yingdi Yucbe72b02015-11-25 17:35:37 -080035 , m_isDefaultKeyLoaded(false)
Yingdi Yucbe72b02015-11-25 17:35:37 -080036{
Davide Pesavento50f66752017-05-15 20:57:12 -040037 BOOST_ASSERT(m_pib != nullptr);
Yingdi Yucbe72b02015-11-25 17:35:37 -080038
39 if (needInit) {
Davide Pesavento50f66752017-05-15 20:57:12 -040040 m_pib->addIdentity(m_name);
Yingdi Yucbe72b02015-11-25 17:35:37 -080041 }
Davide Pesavento50f66752017-05-15 20:57:12 -040042 else if (!m_pib->hasIdentity(m_name)) {
Yingdi Yucbe72b02015-11-25 17:35:37 -080043 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());
Yingdi Yucbe72b02015-11-25 17:35:37 -080051 return m_keys.add(key, keyLen, keyName);
52}
53
54void
55IdentityImpl::removeKey(const Name& keyName)
56{
57 BOOST_ASSERT(m_keys.isConsistent());
58
59 if (m_isDefaultKeyLoaded && m_defaultKey.getName() == keyName)
60 m_isDefaultKeyLoaded = false;
61
62 m_keys.remove(keyName);
63}
64
65Key
66IdentityImpl::getKey(const Name& keyName) const
67{
68 BOOST_ASSERT(m_keys.isConsistent());
Yingdi Yucbe72b02015-11-25 17:35:37 -080069 return m_keys.get(keyName);
70}
71
72const KeyContainer&
73IdentityImpl::getKeys() const
74{
75 BOOST_ASSERT(m_keys.isConsistent());
Yingdi Yucbe72b02015-11-25 17:35:37 -080076 return m_keys;
77}
78
79const Key&
80IdentityImpl::setDefaultKey(const Name& keyName)
81{
82 BOOST_ASSERT(m_keys.isConsistent());
83
84 m_defaultKey = m_keys.get(keyName);
85 m_isDefaultKeyLoaded = true;
Davide Pesavento50f66752017-05-15 20:57:12 -040086 m_pib->setDefaultKeyOfIdentity(m_name, keyName);
Yingdi Yucbe72b02015-11-25 17:35:37 -080087 return m_defaultKey;
88}
89
90const Key&
91IdentityImpl::setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName)
92{
93 addKey(key, keyLen, keyName);
94 return setDefaultKey(keyName);
95}
96
97const Key&
98IdentityImpl::getDefaultKey() const
99{
100 BOOST_ASSERT(m_keys.isConsistent());
101
102 if (!m_isDefaultKeyLoaded) {
Davide Pesavento50f66752017-05-15 20:57:12 -0400103 m_defaultKey = m_keys.get(m_pib->getDefaultKeyOfIdentity(m_name));
Yingdi Yucbe72b02015-11-25 17:35:37 -0800104 m_isDefaultKeyLoaded = true;
105 }
Davide Pesavento50f66752017-05-15 20:57:12 -0400106 BOOST_ASSERT(m_pib->getDefaultKeyOfIdentity(m_name) == m_defaultKey.getName());
Yingdi Yucbe72b02015-11-25 17:35:37 -0800107
108 return m_defaultKey;
109}
110
111} // namespace detail
112} // namespace pib
113} // namespace security
114} // namespace ndn