blob: c472863052b55439a69f1545cfee6937435274cd [file] [log] [blame]
Yingdi Yu3bf91f52015-06-12 19:39:40 -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 Yu3bf91f52015-06-12 19:39:40 -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-memory.hpp"
23#include "pib.hpp"
24
25namespace ndn {
26namespace security {
27
28PibMemory::PibMemory()
29 : m_hasDefaultIdentity(false)
30{
31}
32
33void
34PibMemory::setTpmLocator(const std::string& tpmLocator)
35{
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070036 BOOST_THROW_EXCEPTION(Error("PibMemory does not need a locator"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -070037}
38
39std::string
40PibMemory::getTpmLocator() const
41{
42 return "tpm-memory:";
43}
44
45bool
46PibMemory::hasIdentity(const Name& identity) const
47{
48 return (m_identities.count(identity) > 0);
49}
50
51void
52PibMemory::addIdentity(const Name& identity)
53{
54 m_identities.insert(identity);
55
56 if (!m_hasDefaultIdentity) {
57 m_defaultIdentity = identity;
58 m_hasDefaultIdentity = true;
59 }
60}
61
62void
63PibMemory::removeIdentity(const Name& identity)
64{
65 m_identities.erase(identity);
66 if (identity == m_defaultIdentity)
67 m_hasDefaultIdentity = false;
68
69 auto keyIds = this->getKeysOfIdentity(identity);
70 for (const name::Component& keyId : keyIds) {
71 this->removeKey(identity, keyId);
72 }
73}
74
75std::set<Name>
76PibMemory::getIdentities() const
77{
78 return m_identities;
79}
80
81void
82PibMemory::setDefaultIdentity(const Name& identityName)
83{
84 addIdentity(identityName);
85 m_defaultIdentity = identityName;
86 m_hasDefaultIdentity = true;
87}
88
89Name
90PibMemory::getDefaultIdentity() const
91{
92 if (m_hasDefaultIdentity)
93 return m_defaultIdentity;
94
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070095 BOOST_THROW_EXCEPTION(Pib::Error("No default identity"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -070096}
97
98bool
99PibMemory::hasKey(const Name& identity, const name::Component& keyId) const
100{
101 return (m_keys.count(getKeyName(identity, keyId)) > 0);
102}
103
104void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700105PibMemory::addKey(const Name& identity, const name::Component& keyId, const v1::PublicKey& publicKey)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700106{
107 this->addIdentity(identity);
108
109 Name keyName = getKeyName(identity, keyId);
110 m_keys[keyName] = publicKey;
111
112 if (m_defaultKey.find(identity) == m_defaultKey.end())
113 m_defaultKey[identity] = keyName;
114}
115
116void
117PibMemory::removeKey(const Name& identity, const name::Component& keyId)
118{
119 Name keyName = getKeyName(identity, keyId);
120 m_keys.erase(keyName);
121 m_defaultKey.erase(identity);
122
123
124 auto certNames = this->getCertificatesOfKey(identity, keyId);
125 for (const auto& certName : certNames) {
126 this->removeCertificate(certName);
127 }
128}
129
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700130v1::PublicKey
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700131PibMemory::getKeyBits(const Name& identity, const name::Component& keyId) const
132{
133 if (!hasKey(identity, keyId))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700134 BOOST_THROW_EXCEPTION(Pib::Error("No key"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700135
136 auto it = m_keys.find(getKeyName(identity, keyId));
137 return it->second;
138}
139
140std::set<name::Component>
141PibMemory::getKeysOfIdentity(const Name& identity) const
142{
143 std::set<name::Component> ids;
144 for (const auto& it : m_keys) {
145 if (identity == it.first.getPrefix(-1))
146 ids.insert(it.first.get(-1));
147 }
148 return ids;
149}
150
151void
152PibMemory::setDefaultKeyOfIdentity(const Name& identity, const name::Component& keyId)
153{
154 Name keyName = getKeyName(identity, keyId);
155
156 if (!hasKey(identity, keyId))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700157 BOOST_THROW_EXCEPTION(Pib::Error("No key"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700158
159 m_defaultKey[identity] = keyName;
160}
161
162name::Component
163PibMemory::getDefaultKeyOfIdentity(const Name& identity) const
164{
165 auto it = m_defaultKey.find(identity);
166 if (it == m_defaultKey.end())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700167 BOOST_THROW_EXCEPTION(Pib::Error("No default key"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700168
169 return it->second.get(-1);
170}
171
172Name
173PibMemory::getKeyName(const Name& identity, const name::Component& keyId) const
174{
175 Name keyName = identity;
176 keyName.append(keyId);
177 return keyName;
178}
179
180bool
181PibMemory::hasCertificate(const Name& certName) const
182{
183 return (m_certs.count(certName) > 0);
184}
185
186void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700187PibMemory::addCertificate(const v1::IdentityCertificate& certificate)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700188{
189 this->addKey(certificate.getPublicKeyName().getPrefix(-1),
190 certificate.getPublicKeyName().get(-1),
191 certificate.getPublicKeyInfo());
192
193 m_certs[certificate.getName()] = certificate;
194
195 const Name& keyName = certificate.getPublicKeyName();
196 if (m_defaultCert.find(keyName) == m_defaultCert.end())
197 m_defaultCert[keyName] = certificate.getName();
198}
199
200void
201PibMemory::removeCertificate(const Name& certName)
202{
203 m_certs.erase(certName);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700204 m_defaultCert.erase(v1::IdentityCertificate::certificateNameToPublicKeyName(certName));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700205}
206
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700207v1::IdentityCertificate
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700208PibMemory::getCertificate(const Name& certName) const
209{
210 if (!hasCertificate(certName))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700211 BOOST_THROW_EXCEPTION(Pib::Error("No cert"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700212
213 auto it = m_certs.find(certName);
214 return it->second;
215}
216
217std::set<Name>
218PibMemory::getCertificatesOfKey(const Name& identity, const name::Component& keyId) const
219{
220 Name keyName = getKeyName(identity, keyId);
221
222 std::set<Name> certNames;
223 for (const auto& it : m_certs) {
224 if (it.second.getPublicKeyName() == keyName)
225 certNames.insert(it.first);
226 }
227 return certNames;
228}
229
230void
231PibMemory::setDefaultCertificateOfKey(const Name& identity, const name::Component& keyId, const Name& certName)
232{
233 if (!hasCertificate(certName))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700234 BOOST_THROW_EXCEPTION(Pib::Error("No cert"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700235
236 Name keyName = getKeyName(identity, keyId);
237 m_defaultCert[keyName] = certName;
238}
239
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700240v1::IdentityCertificate
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700241PibMemory::getDefaultCertificateOfKey(const Name& identity, const name::Component& keyId) const
242{
243 Name keyName = getKeyName(identity, keyId);
244
245 auto it = m_defaultCert.find(keyName);
246 if (it == m_defaultCert.end())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700247 BOOST_THROW_EXCEPTION(Pib::Error("No default certificate"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700248
249 auto certIt = m_certs.find(it->second);
250 if (certIt == m_certs.end())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700251 BOOST_THROW_EXCEPTION(Pib::Error("No default certificate"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700252 else
253 return certIt->second;
254}
255
256} // namespace security
257} // namespace ndn