blob: 6c5f316192741a32072f64a1ad51593352d7b87f [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"
24
25namespace ndn {
26namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070027namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070028
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070029Pib::Pib(const std::string& scheme, const std::string& location, shared_ptr<PibImpl> impl)
Yingdi Yub8f8b342015-04-27 11:06:42 -070030 : m_scheme(scheme)
31 , m_location(location)
Yingdi Yucbe72b02015-11-25 17:35:37 -080032 , m_isDefaultIdentityLoaded(false)
33 , m_identities(impl)
Yingdi Yub8f8b342015-04-27 11:06:42 -070034 , m_impl(impl)
35{
Yingdi Yucbe72b02015-11-25 17:35:37 -080036 BOOST_ASSERT(impl != nullptr);
Yingdi Yub8f8b342015-04-27 11:06:42 -070037}
38
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070039Pib::~Pib() = default;
Yingdi Yub8f8b342015-04-27 11:06:42 -070040
41std::string
42Pib::getPibLocator() const
43{
44 return m_scheme + ":" + m_location;
45}
46
47void
48Pib::setTpmLocator(const std::string& tpmLocator)
49{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070050 if (tpmLocator == m_impl->getTpmLocator()) {
51 return;
52 }
53 reset();
Yingdi Yub8f8b342015-04-27 11:06:42 -070054 m_impl->setTpmLocator(tpmLocator);
55}
56
57std::string
58Pib::getTpmLocator() const
59{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070060 std::string tpmLocator = m_impl->getTpmLocator();
61 if (tpmLocator.empty()) {
62 BOOST_THROW_EXCEPTION(Pib::Error("TPM info does not exist"));
63 }
64 return tpmLocator;
65}
66
67void
68Pib::reset()
69{
70 m_impl->clearIdentities();
71 m_impl->setTpmLocator("");
Yingdi Yucbe72b02015-11-25 17:35:37 -080072 m_isDefaultIdentityLoaded = false;
73 m_identities.reset();
Yingdi Yub8f8b342015-04-27 11:06:42 -070074}
75
76Identity
77Pib::addIdentity(const Name& identity)
78{
Yingdi Yucbe72b02015-11-25 17:35:37 -080079 BOOST_ASSERT(m_identities.isConsistent());
80
81 return m_identities.add(identity);
Yingdi Yub8f8b342015-04-27 11:06:42 -070082}
83
84void
85Pib::removeIdentity(const Name& identity)
86{
Yingdi Yucbe72b02015-11-25 17:35:37 -080087 BOOST_ASSERT(m_identities.isConsistent());
Yingdi Yuc8209892015-06-19 17:47:56 -070088
Yingdi Yucbe72b02015-11-25 17:35:37 -080089 if (m_isDefaultIdentityLoaded && m_defaultIdentity.getName() == identity)
90 m_isDefaultIdentityLoaded = false;
91
92 m_identities.remove(identity);
Yingdi Yub8f8b342015-04-27 11:06:42 -070093}
94
95Identity
Yingdi Yuc8209892015-06-19 17:47:56 -070096Pib::getIdentity(const Name& identity) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070097{
Yingdi Yucbe72b02015-11-25 17:35:37 -080098 BOOST_ASSERT(m_identities.isConsistent());
99
100 return m_identities.get(identity);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700101}
102
Yingdi Yuc8209892015-06-19 17:47:56 -0700103const IdentityContainer&
Yingdi Yub8f8b342015-04-27 11:06:42 -0700104Pib::getIdentities() const
105{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800106 BOOST_ASSERT(m_identities.isConsistent());
Yingdi Yuc8209892015-06-19 17:47:56 -0700107
108 return m_identities;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700109}
110
Yingdi Yucbe72b02015-11-25 17:35:37 -0800111const Identity&
Yingdi Yub8f8b342015-04-27 11:06:42 -0700112Pib::setDefaultIdentity(const Name& identityName)
113{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800114 BOOST_ASSERT(m_identities.isConsistent());
115
116 m_defaultIdentity = m_identities.add(identityName);
117 m_isDefaultIdentityLoaded = true;
Yingdi Yuc8209892015-06-19 17:47:56 -0700118
Yingdi Yub8f8b342015-04-27 11:06:42 -0700119 m_impl->setDefaultIdentity(identityName);
Yingdi Yuc8209892015-06-19 17:47:56 -0700120 return m_defaultIdentity;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700121}
122
Yingdi Yucbe72b02015-11-25 17:35:37 -0800123const Identity&
Yingdi Yuc8209892015-06-19 17:47:56 -0700124Pib::getDefaultIdentity() const
Yingdi Yub8f8b342015-04-27 11:06:42 -0700125{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800126 BOOST_ASSERT(m_identities.isConsistent());
127
128 if (!m_isDefaultIdentityLoaded) {
129 m_defaultIdentity = m_identities.get(m_impl->getDefaultIdentity());
130 m_isDefaultIdentityLoaded = true;
Yingdi Yuc8209892015-06-19 17:47:56 -0700131 }
132
Yingdi Yucbe72b02015-11-25 17:35:37 -0800133 BOOST_ASSERT(m_impl->getDefaultIdentity() == m_defaultIdentity.getName());
134
Yingdi Yuc8209892015-06-19 17:47:56 -0700135 return m_defaultIdentity;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700136}
137
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700138} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700139} // namespace security
140} // namespace ndn