Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 26 | namespace ndn { |
| 27 | namespace security { |
| 28 | namespace pib { |
| 29 | namespace detail { |
| 30 | |
| 31 | IdentityImpl::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 | |
| 47 | Key |
| 48 | IdentityImpl::addKey(const uint8_t* key, size_t keyLen, const Name& keyName) |
| 49 | { |
| 50 | BOOST_ASSERT(m_keys.isConsistent()); |
| 51 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 52 | return m_keys.add(key, keyLen, keyName); |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | IdentityImpl::removeKey(const Name& keyName) |
| 57 | { |
| 58 | BOOST_ASSERT(m_keys.isConsistent()); |
| 59 | |
| 60 | if (m_isDefaultKeyLoaded && m_defaultKey.getName() == keyName) |
| 61 | m_isDefaultKeyLoaded = false; |
| 62 | |
| 63 | m_keys.remove(keyName); |
| 64 | } |
| 65 | |
| 66 | Key |
| 67 | IdentityImpl::getKey(const Name& keyName) const |
| 68 | { |
| 69 | BOOST_ASSERT(m_keys.isConsistent()); |
| 70 | |
| 71 | return m_keys.get(keyName); |
| 72 | } |
| 73 | |
| 74 | const KeyContainer& |
| 75 | IdentityImpl::getKeys() const |
| 76 | { |
| 77 | BOOST_ASSERT(m_keys.isConsistent()); |
| 78 | |
| 79 | return m_keys; |
| 80 | } |
| 81 | |
| 82 | const Key& |
| 83 | IdentityImpl::setDefaultKey(const Name& keyName) |
| 84 | { |
| 85 | BOOST_ASSERT(m_keys.isConsistent()); |
| 86 | |
| 87 | m_defaultKey = m_keys.get(keyName); |
| 88 | m_isDefaultKeyLoaded = true; |
| 89 | m_impl->setDefaultKeyOfIdentity(m_name, keyName); |
| 90 | return m_defaultKey; |
| 91 | } |
| 92 | |
| 93 | const Key& |
| 94 | IdentityImpl::setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName) |
| 95 | { |
| 96 | addKey(key, keyLen, keyName); |
| 97 | return setDefaultKey(keyName); |
| 98 | } |
| 99 | |
| 100 | const Key& |
| 101 | IdentityImpl::getDefaultKey() const |
| 102 | { |
| 103 | BOOST_ASSERT(m_keys.isConsistent()); |
| 104 | |
| 105 | if (!m_isDefaultKeyLoaded) { |
| 106 | m_defaultKey = m_keys.get(m_impl->getDefaultKeyOfIdentity(m_name)); |
| 107 | m_isDefaultKeyLoaded = true; |
| 108 | } |
| 109 | |
| 110 | BOOST_ASSERT(m_impl->getDefaultKeyOfIdentity(m_name) == m_defaultKey.getName()); |
| 111 | |
| 112 | return m_defaultKey; |
| 113 | } |
| 114 | |
| 115 | } // namespace detail |
| 116 | } // namespace pib |
| 117 | } // namespace security |
| 118 | } // namespace ndn |