blob: 68b9add685235db78a2103723f5bf23634cdbf10 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu6ee2d362015-07-16 21:48:05 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yub8f8b342015-04-27 11:06:42 -07004 *
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 Afanasyev83eb1cc2017-01-04 17:34:34 -080024#include "util/logger.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070025
26namespace ndn {
27namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070028namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070029
Alexander Afanasyev83eb1cc2017-01-04 17:34:34 -080030NDN_LOG_INIT(ndn.security.pib.Pib);
31
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070032Pib::Pib(const std::string& scheme, const std::string& location, shared_ptr<PibImpl> impl)
Yingdi Yub8f8b342015-04-27 11:06:42 -070033 : m_scheme(scheme)
34 , m_location(location)
Yingdi Yucbe72b02015-11-25 17:35:37 -080035 , m_isDefaultIdentityLoaded(false)
36 , m_identities(impl)
Yingdi Yub8f8b342015-04-27 11:06:42 -070037 , m_impl(impl)
38{
Yingdi Yucbe72b02015-11-25 17:35:37 -080039 BOOST_ASSERT(impl != nullptr);
Yingdi Yub8f8b342015-04-27 11:06:42 -070040}
41
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070042Pib::~Pib() = default;
Yingdi Yub8f8b342015-04-27 11:06:42 -070043
44std::string
45Pib::getPibLocator() const
46{
47 return m_scheme + ":" + m_location;
48}
49
50void
51Pib::setTpmLocator(const std::string& tpmLocator)
52{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070053 if (tpmLocator == m_impl->getTpmLocator()) {
54 return;
55 }
56 reset();
Yingdi Yub8f8b342015-04-27 11:06:42 -070057 m_impl->setTpmLocator(tpmLocator);
58}
59
60std::string
61Pib::getTpmLocator() const
62{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070063 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
70void
71Pib::reset()
72{
73 m_impl->clearIdentities();
74 m_impl->setTpmLocator("");
Yingdi Yucbe72b02015-11-25 17:35:37 -080075 m_isDefaultIdentityLoaded = false;
76 m_identities.reset();
Yingdi Yub8f8b342015-04-27 11:06:42 -070077}
78
79Identity
80Pib::addIdentity(const Name& identity)
81{
Yingdi Yucbe72b02015-11-25 17:35:37 -080082 BOOST_ASSERT(m_identities.isConsistent());
83
84 return m_identities.add(identity);
Yingdi Yub8f8b342015-04-27 11:06:42 -070085}
86
87void
88Pib::removeIdentity(const Name& identity)
89{
Yingdi Yucbe72b02015-11-25 17:35:37 -080090 BOOST_ASSERT(m_identities.isConsistent());
Yingdi Yuc8209892015-06-19 17:47:56 -070091
Alexander Afanasyev83eb1cc2017-01-04 17:34:34 -080092 if (m_isDefaultIdentityLoaded && m_defaultIdentity.getName() == identity) {
Yingdi Yucbe72b02015-11-25 17:35:37 -080093 m_isDefaultIdentityLoaded = false;
Alexander Afanasyev83eb1cc2017-01-04 17:34:34 -080094 }
Yingdi Yucbe72b02015-11-25 17:35:37 -080095
96 m_identities.remove(identity);
Yingdi Yub8f8b342015-04-27 11:06:42 -070097}
98
99Identity
Yingdi Yuc8209892015-06-19 17:47:56 -0700100Pib::getIdentity(const Name& identity) const
Yingdi Yub8f8b342015-04-27 11:06:42 -0700101{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800102 BOOST_ASSERT(m_identities.isConsistent());
103
104 return m_identities.get(identity);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700105}
106
Yingdi Yuc8209892015-06-19 17:47:56 -0700107const IdentityContainer&
Yingdi Yub8f8b342015-04-27 11:06:42 -0700108Pib::getIdentities() const
109{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800110 BOOST_ASSERT(m_identities.isConsistent());
Yingdi Yuc8209892015-06-19 17:47:56 -0700111
112 return m_identities;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700113}
114
Yingdi Yucbe72b02015-11-25 17:35:37 -0800115const Identity&
Yingdi Yub8f8b342015-04-27 11:06:42 -0700116Pib::setDefaultIdentity(const Name& identityName)
117{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800118 BOOST_ASSERT(m_identities.isConsistent());
119
120 m_defaultIdentity = m_identities.add(identityName);
121 m_isDefaultIdentityLoaded = true;
Alexander Afanasyev83eb1cc2017-01-04 17:34:34 -0800122 NDN_LOG_DEBUG("Default identity is set to " << identityName);
Yingdi Yuc8209892015-06-19 17:47:56 -0700123
Yingdi Yub8f8b342015-04-27 11:06:42 -0700124 m_impl->setDefaultIdentity(identityName);
Yingdi Yuc8209892015-06-19 17:47:56 -0700125 return m_defaultIdentity;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700126}
127
Yingdi Yucbe72b02015-11-25 17:35:37 -0800128const Identity&
Yingdi Yuc8209892015-06-19 17:47:56 -0700129Pib::getDefaultIdentity() const
Yingdi Yub8f8b342015-04-27 11:06:42 -0700130{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800131 BOOST_ASSERT(m_identities.isConsistent());
132
133 if (!m_isDefaultIdentityLoaded) {
134 m_defaultIdentity = m_identities.get(m_impl->getDefaultIdentity());
135 m_isDefaultIdentityLoaded = true;
Alexander Afanasyev83eb1cc2017-01-04 17:34:34 -0800136 NDN_LOG_DEBUG("Default identity is " << m_defaultIdentity.getName());
Yingdi Yuc8209892015-06-19 17:47:56 -0700137 }
138
Yingdi Yucbe72b02015-11-25 17:35:37 -0800139 BOOST_ASSERT(m_impl->getDefaultIdentity() == m_defaultIdentity.getName());
140
Yingdi Yuc8209892015-06-19 17:47:56 -0700141 return m_defaultIdentity;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700142}
143
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700144} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700145} // namespace security
146} // namespace ndn