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 | |
| 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 | |
| 59 | void |
| 60 | IdentityImpl::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 | |
| 70 | Key |
| 71 | IdentityImpl::getKey(const Name& keyName) const |
| 72 | { |
| 73 | BOOST_ASSERT(m_keys.isConsistent()); |
| 74 | |
| 75 | return m_keys.get(keyName); |
| 76 | } |
| 77 | |
| 78 | const KeyContainer& |
| 79 | IdentityImpl::getKeys() const |
| 80 | { |
| 81 | BOOST_ASSERT(m_keys.isConsistent()); |
| 82 | |
| 83 | return m_keys; |
| 84 | } |
| 85 | |
| 86 | const Key& |
| 87 | IdentityImpl::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 | |
| 97 | const Key& |
| 98 | IdentityImpl::setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName) |
| 99 | { |
| 100 | addKey(key, keyLen, keyName); |
| 101 | return setDefaultKey(keyName); |
| 102 | } |
| 103 | |
| 104 | const Key& |
| 105 | IdentityImpl::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 |