blob: 7662a0ae0fe01b0c4354301253e93fee13aaeac8 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 Regents of the University of California.
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 "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))
49 throw Pib::Error("Identity: " + m_name.toUri() + " does not exist");
50}
51
52const Name&
53Identity::getName() const
54{
55 validityCheck();
56
57 return m_name;
58}
59
60Key
61Identity::addKey(const PublicKey& publicKey, const name::Component& keyId)
62{
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
93Identity::getKey(const name::Component& keyId)
94{
95 validityCheck();
96
97 return Key(m_name, keyId, m_impl);
98}
99
100const KeyContainer&
101Identity::getKeys()
102{
103 validityCheck();
104
105 if (m_needRefreshKeys) {
106 m_keys = std::move(KeyContainer(m_name, m_impl->getKeysOfIdentity(m_name), m_impl));
107 m_needRefreshKeys = false;
108 }
109
110 return m_keys;
111}
112
113Key&
114Identity::setDefaultKey(const name::Component& keyId)
115{
116 validityCheck();
117
118 m_defaultKey = std::move(Key(m_name, keyId, m_impl));
119 m_hasDefaultKey = true;
120
121 m_impl->setDefaultKeyOfIdentity(m_name, keyId);
122 return m_defaultKey;
123}
124
125Key&
126Identity::setDefaultKey(const PublicKey& publicKey, const name::Component& keyId)
127{
128 const Key& keyEntry = addKey(publicKey, keyId);
129 return setDefaultKey(keyEntry.getKeyId());
130}
131
132Key&
133Identity::getDefaultKey()
134{
135 validityCheck();
136
137 if (!m_hasDefaultKey) {
138 m_defaultKey = std::move(Key(m_name, m_impl->getDefaultKeyOfIdentity(m_name), m_impl));
139 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)
160 throw std::domain_error("Invalid Identity instance");
161}
162
163} // namespace security
164} // namespace ndn