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 | |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 31 | IdentityImpl::IdentityImpl(const Name& identityName, shared_ptr<PibImpl> pibImpl, bool needInit) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 32 | : m_name(identityName) |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 33 | , m_pib(std::move(pibImpl)) |
| 34 | , m_keys(identityName, m_pib) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 35 | , m_isDefaultKeyLoaded(false) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 36 | { |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 37 | BOOST_ASSERT(m_pib != nullptr); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 38 | |
| 39 | if (needInit) { |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 40 | m_pib->addIdentity(m_name); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 41 | } |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 42 | else if (!m_pib->hasIdentity(m_name)) { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 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()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 51 | return m_keys.add(key, keyLen, keyName); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | IdentityImpl::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 | |
| 65 | Key |
| 66 | IdentityImpl::getKey(const Name& keyName) const |
| 67 | { |
| 68 | BOOST_ASSERT(m_keys.isConsistent()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 69 | return m_keys.get(keyName); |
| 70 | } |
| 71 | |
| 72 | const KeyContainer& |
| 73 | IdentityImpl::getKeys() const |
| 74 | { |
| 75 | BOOST_ASSERT(m_keys.isConsistent()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 76 | return m_keys; |
| 77 | } |
| 78 | |
| 79 | const Key& |
| 80 | IdentityImpl::setDefaultKey(const Name& keyName) |
| 81 | { |
| 82 | BOOST_ASSERT(m_keys.isConsistent()); |
| 83 | |
| 84 | m_defaultKey = m_keys.get(keyName); |
| 85 | m_isDefaultKeyLoaded = true; |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 86 | m_pib->setDefaultKeyOfIdentity(m_name, keyName); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 87 | return m_defaultKey; |
| 88 | } |
| 89 | |
| 90 | const Key& |
| 91 | IdentityImpl::setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName) |
| 92 | { |
| 93 | addKey(key, keyLen, keyName); |
| 94 | return setDefaultKey(keyName); |
| 95 | } |
| 96 | |
| 97 | const Key& |
| 98 | IdentityImpl::getDefaultKey() const |
| 99 | { |
| 100 | BOOST_ASSERT(m_keys.isConsistent()); |
| 101 | |
| 102 | if (!m_isDefaultKeyLoaded) { |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 103 | m_defaultKey = m_keys.get(m_pib->getDefaultKeyOfIdentity(m_name)); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 104 | m_isDefaultKeyLoaded = true; |
| 105 | } |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 106 | BOOST_ASSERT(m_pib->getDefaultKeyOfIdentity(m_name) == m_defaultKey.getName()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 107 | |
| 108 | return m_defaultKey; |
| 109 | } |
| 110 | |
| 111 | } // namespace detail |
| 112 | } // namespace pib |
| 113 | } // namespace security |
| 114 | } // namespace ndn |