blob: a29018e74fe775519208a4315544a21114f088a3 [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 Yuc8209892015-06-19 17:47:56 -070032 , m_hasDefaultIdentity(false)
33 , m_needRefreshIdentities(true)
Yingdi Yub8f8b342015-04-27 11:06:42 -070034 , m_impl(impl)
35{
36}
37
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070038Pib::~Pib() = default;
Yingdi Yub8f8b342015-04-27 11:06:42 -070039
40std::string
41Pib::getPibLocator() const
42{
43 return m_scheme + ":" + m_location;
44}
45
46void
47Pib::setTpmLocator(const std::string& tpmLocator)
48{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070049 if (tpmLocator == m_impl->getTpmLocator()) {
50 return;
51 }
52 reset();
Yingdi Yub8f8b342015-04-27 11:06:42 -070053 m_impl->setTpmLocator(tpmLocator);
54}
55
56std::string
57Pib::getTpmLocator() const
58{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070059 std::string tpmLocator = m_impl->getTpmLocator();
60 if (tpmLocator.empty()) {
61 BOOST_THROW_EXCEPTION(Pib::Error("TPM info does not exist"));
62 }
63 return tpmLocator;
64}
65
66void
67Pib::reset()
68{
69 m_impl->clearIdentities();
70 m_impl->setTpmLocator("");
71
72 m_hasDefaultIdentity = false;
73 m_needRefreshIdentities = true;
Yingdi Yub8f8b342015-04-27 11:06:42 -070074}
75
76Identity
77Pib::addIdentity(const Name& identity)
78{
Yingdi Yuc8209892015-06-19 17:47:56 -070079 if (!m_needRefreshIdentities && m_identities.find(identity) == m_identities.end()) {
80 // if we have already loaded all the identities, but the new identity is not one of them
81 // the IdentityContainer should be refreshed
82 m_needRefreshIdentities = true;
83 }
Yingdi Yub8f8b342015-04-27 11:06:42 -070084 return Identity(identity, m_impl, true);
85}
86
87void
88Pib::removeIdentity(const Name& identity)
89{
Yingdi Yuc8209892015-06-19 17:47:56 -070090 if (m_hasDefaultIdentity && m_defaultIdentity.getName() == identity)
91 m_hasDefaultIdentity = false;
92
Yingdi Yub8f8b342015-04-27 11:06:42 -070093 m_impl->removeIdentity(identity);
Yingdi Yuc8209892015-06-19 17:47:56 -070094 m_needRefreshIdentities = true;
Yingdi Yub8f8b342015-04-27 11:06:42 -070095}
96
97Identity
Yingdi Yuc8209892015-06-19 17:47:56 -070098Pib::getIdentity(const Name& identity) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070099{
100 return Identity(identity, m_impl, false);
101}
102
Yingdi Yuc8209892015-06-19 17:47:56 -0700103const IdentityContainer&
Yingdi Yub8f8b342015-04-27 11:06:42 -0700104Pib::getIdentities() const
105{
Yingdi Yuc8209892015-06-19 17:47:56 -0700106 if (m_needRefreshIdentities) {
Alexander Afanasyev66ca2032015-12-04 13:17:02 -0800107 m_identities = IdentityContainer(m_impl->getIdentities(), m_impl);
Yingdi Yuc8209892015-06-19 17:47:56 -0700108 m_needRefreshIdentities = false;
109 }
110
111 return m_identities;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700112}
113
Yingdi Yuc8209892015-06-19 17:47:56 -0700114Identity&
Yingdi Yub8f8b342015-04-27 11:06:42 -0700115Pib::setDefaultIdentity(const Name& identityName)
116{
Yingdi Yuc8209892015-06-19 17:47:56 -0700117 m_defaultIdentity = addIdentity(identityName);
118 m_hasDefaultIdentity = true;
119
Yingdi Yub8f8b342015-04-27 11:06:42 -0700120 m_impl->setDefaultIdentity(identityName);
Yingdi Yuc8209892015-06-19 17:47:56 -0700121 return m_defaultIdentity;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700122}
123
Yingdi Yuc8209892015-06-19 17:47:56 -0700124Identity&
125Pib::getDefaultIdentity() const
Yingdi Yub8f8b342015-04-27 11:06:42 -0700126{
Yingdi Yuc8209892015-06-19 17:47:56 -0700127 if (!m_hasDefaultIdentity) {
Alexander Afanasyev66ca2032015-12-04 13:17:02 -0800128 m_defaultIdentity = Identity(m_impl->getDefaultIdentity(), m_impl, false);
Yingdi Yuc8209892015-06-19 17:47:56 -0700129 m_hasDefaultIdentity = true;
130 }
131
132 return m_defaultIdentity;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700133}
134
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700135} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700136} // namespace security
137} // namespace ndn