blob: 176814cf33f6943c0b6f64ad36704ec3a64de732 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07003 * Copyright (c) 2013-2016 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 "identity.hpp"
23#include "pib-impl.hpp"
24#include "pib.hpp"
25
26namespace ndn {
27namespace security {
28
29const name::Component Identity::EMPTY_KEY_ID;
30
31Identity::Identity()
32 : m_hasDefaultKey(false)
33 , m_needRefreshKeys(false)
34 , m_impl(nullptr)
35{
36}
37
38Identity::Identity(const Name& identityName, shared_ptr<PibImpl> impl, bool needInit)
39 : m_name(identityName)
40 , m_hasDefaultKey(false)
41 , m_needRefreshKeys(true)
42 , m_impl(impl)
43{
44 validityCheck();
45
46 if (needInit)
47 m_impl->addIdentity(m_name);
48 else if (!m_impl->hasIdentity(m_name))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070049 BOOST_THROW_EXCEPTION(Pib::Error("Identity: " + m_name.toUri() + " does not exist"));
Yingdi Yub8f8b342015-04-27 11:06:42 -070050}
51
52const Name&
53Identity::getName() const
54{
55 validityCheck();
56
57 return m_name;
58}
59
60Key
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070061Identity::addKey(const v1::PublicKey& publicKey, const name::Component& keyId)
Yingdi Yub8f8b342015-04-27 11:06:42 -070062{
63 validityCheck();
64
65 name::Component actualKeyId = keyId;
66 if (actualKeyId == EMPTY_KEY_ID) {
67 const Block& digest = publicKey.computeDigest();
68 actualKeyId = name::Component(digest.wire(), digest.size());
69 }
70
71 if (!m_needRefreshKeys && m_keys.find(actualKeyId) == m_keys.end()) {
72 // if we have already loaded all the keys, but the new key is not one of them
73 // the KeyContainer should be refreshed
74 m_needRefreshKeys = true;
75 }
76
77 return Key(m_name, actualKeyId, publicKey, m_impl);
78}
79
80void
81Identity::removeKey(const name::Component& keyId)
82{
83 validityCheck();
84
85 if (m_hasDefaultKey && m_defaultKey.getKeyId() == keyId)
86 m_hasDefaultKey = false;
87
88 m_impl->removeKey(m_name, keyId);
89 m_needRefreshKeys = true;
90}
91
92Key
Yingdi Yuc8209892015-06-19 17:47:56 -070093Identity::getKey(const name::Component& keyId) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070094{
95 validityCheck();
96
97 return Key(m_name, keyId, m_impl);
98}
99
100const KeyContainer&
Yingdi Yuc8209892015-06-19 17:47:56 -0700101Identity::getKeys() const
Yingdi Yub8f8b342015-04-27 11:06:42 -0700102{
103 validityCheck();
104
105 if (m_needRefreshKeys) {
Alexander Afanasyev66ca2032015-12-04 13:17:02 -0800106 m_keys = KeyContainer(m_name, m_impl->getKeysOfIdentity(m_name), m_impl);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700107 m_needRefreshKeys = false;
108 }
109
110 return m_keys;
111}
112
113Key&
114Identity::setDefaultKey(const name::Component& keyId)
115{
116 validityCheck();
117
Alexander Afanasyev66ca2032015-12-04 13:17:02 -0800118 m_defaultKey = Key(m_name, keyId, m_impl);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700119 m_hasDefaultKey = true;
120
121 m_impl->setDefaultKeyOfIdentity(m_name, keyId);
122 return m_defaultKey;
123}
124
125Key&
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700126Identity::setDefaultKey(const v1::PublicKey& publicKey, const name::Component& keyId)
Yingdi Yub8f8b342015-04-27 11:06:42 -0700127{
128 const Key& keyEntry = addKey(publicKey, keyId);
129 return setDefaultKey(keyEntry.getKeyId());
130}
131
132Key&
Yingdi Yuc8209892015-06-19 17:47:56 -0700133Identity::getDefaultKey() const
Yingdi Yub8f8b342015-04-27 11:06:42 -0700134{
135 validityCheck();
136
137 if (!m_hasDefaultKey) {
Alexander Afanasyev66ca2032015-12-04 13:17:02 -0800138 m_defaultKey = Key(m_name, m_impl->getDefaultKeyOfIdentity(m_name), m_impl);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700139 m_hasDefaultKey = true;
140 }
141
142 return m_defaultKey;
143}
144
145Identity::operator bool() const
146{
147 return !(this->operator!());
148}
149
150bool
151Identity::operator!() const
152{
153 return (m_impl == nullptr);
154}
155
156void
157Identity::validityCheck() const
158{
159 if (m_impl == nullptr)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700160 BOOST_THROW_EXCEPTION(std::domain_error("Invalid Identity instance"));
Yingdi Yub8f8b342015-04-27 11:06:42 -0700161}
162
163} // namespace security
164} // namespace ndn