Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 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 "pib.hpp" |
| 23 | #include "pib-impl.hpp" |
Alexander Afanasyev | 83eb1cc | 2017-01-04 17:34:34 -0800 | [diff] [blame] | 24 | #include "util/logger.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 28 | namespace pib { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | 83eb1cc | 2017-01-04 17:34:34 -0800 | [diff] [blame] | 30 | NDN_LOG_INIT(ndn.security.pib.Pib); |
| 31 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 32 | Pib::Pib(const std::string& scheme, const std::string& location, shared_ptr<PibImpl> impl) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 33 | : m_scheme(scheme) |
| 34 | , m_location(location) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 35 | , m_isDefaultIdentityLoaded(false) |
| 36 | , m_identities(impl) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 37 | , m_impl(impl) |
| 38 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 39 | BOOST_ASSERT(impl != nullptr); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 42 | Pib::~Pib() = default; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 43 | |
| 44 | std::string |
| 45 | Pib::getPibLocator() const |
| 46 | { |
| 47 | return m_scheme + ":" + m_location; |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | Pib::setTpmLocator(const std::string& tpmLocator) |
| 52 | { |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 53 | if (tpmLocator == m_impl->getTpmLocator()) { |
| 54 | return; |
| 55 | } |
| 56 | reset(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 57 | m_impl->setTpmLocator(tpmLocator); |
| 58 | } |
| 59 | |
| 60 | std::string |
| 61 | Pib::getTpmLocator() const |
| 62 | { |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 63 | std::string tpmLocator = m_impl->getTpmLocator(); |
| 64 | if (tpmLocator.empty()) { |
| 65 | BOOST_THROW_EXCEPTION(Pib::Error("TPM info does not exist")); |
| 66 | } |
| 67 | return tpmLocator; |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | Pib::reset() |
| 72 | { |
| 73 | m_impl->clearIdentities(); |
| 74 | m_impl->setTpmLocator(""); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 75 | m_isDefaultIdentityLoaded = false; |
| 76 | m_identities.reset(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | Identity |
| 80 | Pib::addIdentity(const Name& identity) |
| 81 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 82 | BOOST_ASSERT(m_identities.isConsistent()); |
| 83 | |
| 84 | return m_identities.add(identity); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
| 88 | Pib::removeIdentity(const Name& identity) |
| 89 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 90 | BOOST_ASSERT(m_identities.isConsistent()); |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | 83eb1cc | 2017-01-04 17:34:34 -0800 | [diff] [blame] | 92 | if (m_isDefaultIdentityLoaded && m_defaultIdentity.getName() == identity) { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 93 | m_isDefaultIdentityLoaded = false; |
Alexander Afanasyev | 83eb1cc | 2017-01-04 17:34:34 -0800 | [diff] [blame] | 94 | } |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 95 | |
| 96 | m_identities.remove(identity); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | Identity |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 100 | Pib::getIdentity(const Name& identity) const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 101 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 102 | BOOST_ASSERT(m_identities.isConsistent()); |
| 103 | |
| 104 | return m_identities.get(identity); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 107 | const IdentityContainer& |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 108 | Pib::getIdentities() const |
| 109 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 110 | BOOST_ASSERT(m_identities.isConsistent()); |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 111 | |
| 112 | return m_identities; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 115 | const Identity& |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 116 | Pib::setDefaultIdentity(const Name& identityName) |
| 117 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 118 | BOOST_ASSERT(m_identities.isConsistent()); |
| 119 | |
| 120 | m_defaultIdentity = m_identities.add(identityName); |
| 121 | m_isDefaultIdentityLoaded = true; |
Alexander Afanasyev | 83eb1cc | 2017-01-04 17:34:34 -0800 | [diff] [blame] | 122 | NDN_LOG_DEBUG("Default identity is set to " << identityName); |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 123 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 124 | m_impl->setDefaultIdentity(identityName); |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 125 | return m_defaultIdentity; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 128 | const Identity& |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 129 | Pib::getDefaultIdentity() const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 130 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 131 | BOOST_ASSERT(m_identities.isConsistent()); |
| 132 | |
| 133 | if (!m_isDefaultIdentityLoaded) { |
| 134 | m_defaultIdentity = m_identities.get(m_impl->getDefaultIdentity()); |
| 135 | m_isDefaultIdentityLoaded = true; |
Alexander Afanasyev | 83eb1cc | 2017-01-04 17:34:34 -0800 | [diff] [blame] | 136 | NDN_LOG_DEBUG("Default identity is " << m_defaultIdentity.getName()); |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 139 | BOOST_ASSERT(m_impl->getDefaultIdentity() == m_defaultIdentity.getName()); |
| 140 | |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 141 | return m_defaultIdentity; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 144 | } // namespace pib |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 145 | } // namespace security |
| 146 | } // namespace ndn |