blob: aa93ba7df2f57899276ca44bdda173558967a1ff [file] [log] [blame]
Yingdi Yu3bf91f52015-06-12 19:39:40 -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 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"
Yingdi Yu6ee2d362015-07-16 21:48:05 -070024#include "../security-common.hpp"
Yingdi Yu3bf91f52015-06-12 19:39:40 -070025
26namespace ndn {
27namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070028namespace pib {
Yingdi Yu3bf91f52015-06-12 19:39:40 -070029
30PibMemory::PibMemory()
31 : m_hasDefaultIdentity(false)
32{
33}
34
35void
36PibMemory::setTpmLocator(const std::string& tpmLocator)
37{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070038 m_tpmLocator = tpmLocator;
Yingdi Yu3bf91f52015-06-12 19:39:40 -070039}
40
41std::string
42PibMemory::getTpmLocator() const
43{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070044 return m_tpmLocator;
Yingdi Yu3bf91f52015-06-12 19:39:40 -070045}
46
47bool
48PibMemory::hasIdentity(const Name& identity) const
49{
50 return (m_identities.count(identity) > 0);
51}
52
53void
54PibMemory::addIdentity(const Name& identity)
55{
56 m_identities.insert(identity);
57
58 if (!m_hasDefaultIdentity) {
59 m_defaultIdentity = identity;
60 m_hasDefaultIdentity = true;
61 }
62}
63
64void
65PibMemory::removeIdentity(const Name& identity)
66{
67 m_identities.erase(identity);
Yingdi Yu03997682015-11-23 16:41:38 -080068 if (identity == m_defaultIdentity) {
Yingdi Yu3bf91f52015-06-12 19:39:40 -070069 m_hasDefaultIdentity = false;
Yingdi Yu03997682015-11-23 16:41:38 -080070 m_defaultIdentity.clear();
71 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -070072
Yingdi Yu03997682015-11-23 16:41:38 -080073 auto keyNames = getKeysOfIdentity(identity);
Yingdi Yu6ee2d362015-07-16 21:48:05 -070074 for (const Name& keyName : keyNames) {
Yingdi Yu03997682015-11-23 16:41:38 -080075 removeKey(keyName);
Yingdi Yu3bf91f52015-06-12 19:39:40 -070076 }
77}
78
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070079void
80PibMemory::clearIdentities()
81{
82 m_hasDefaultIdentity = false;
83 m_defaultIdentity.clear();
84 m_identities.clear();
Yingdi Yu03997682015-11-23 16:41:38 -080085 m_defaultKeys.clear();
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070086 m_keys.clear();
Yingdi Yu03997682015-11-23 16:41:38 -080087 m_defaultCerts.clear();
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070088 m_certs.clear();
89}
90
Yingdi Yu3bf91f52015-06-12 19:39:40 -070091std::set<Name>
92PibMemory::getIdentities() const
93{
94 return m_identities;
95}
96
97void
98PibMemory::setDefaultIdentity(const Name& identityName)
99{
100 addIdentity(identityName);
101 m_defaultIdentity = identityName;
102 m_hasDefaultIdentity = true;
103}
104
105Name
106PibMemory::getDefaultIdentity() const
107{
Yingdi Yu03997682015-11-23 16:41:38 -0800108 if (m_hasDefaultIdentity) {
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700109 return m_defaultIdentity;
Yingdi Yu03997682015-11-23 16:41:38 -0800110 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700111
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700112 BOOST_THROW_EXCEPTION(Pib::Error("No default identity"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700113}
114
115bool
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700116PibMemory::hasKey(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700117{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700118 return (m_keys.count(keyName) > 0);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700119}
120
121void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700122PibMemory::addKey(const Name& identity, const Name& keyName,
123 const uint8_t* key, size_t keyLen)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700124{
Yingdi Yu03997682015-11-23 16:41:38 -0800125 addIdentity(identity);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700126
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700127 m_keys[keyName] = Buffer(key, keyLen);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700128
Yingdi Yu03997682015-11-23 16:41:38 -0800129 if (m_defaultKeys.count(identity) == 0) {
130 m_defaultKeys[identity] = keyName;
131 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700132}
133
134void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700135PibMemory::removeKey(const Name& keyName)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700136{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700137 Name identity = v2::extractIdentityFromKeyName(keyName);
138
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700139 m_keys.erase(keyName);
Yingdi Yu03997682015-11-23 16:41:38 -0800140 m_defaultKeys.erase(identity);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700141
Yingdi Yu03997682015-11-23 16:41:38 -0800142 auto certNames = getCertificatesOfKey(keyName);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700143 for (const auto& certName : certNames) {
Yingdi Yu03997682015-11-23 16:41:38 -0800144 removeCertificate(certName);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700145 }
146}
147
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700148Buffer
149PibMemory::getKeyBits(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700150{
Yingdi Yu03997682015-11-23 16:41:38 -0800151 if (!hasKey(keyName)) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700152 BOOST_THROW_EXCEPTION(Pib::Error("Key `" + keyName.toUri() + "` not found"));
Yingdi Yu03997682015-11-23 16:41:38 -0800153 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700154
Yingdi Yu03997682015-11-23 16:41:38 -0800155 auto key = m_keys.find(keyName);
156 BOOST_ASSERT(key != m_keys.end());
157 return key->second;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700158}
159
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700160std::set<Name>
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700161PibMemory::getKeysOfIdentity(const Name& identity) const
162{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700163 std::set<Name> ids;
Yingdi Yu03997682015-11-23 16:41:38 -0800164 for (const auto& key : m_keys) {
165 if (identity == v2::extractIdentityFromKeyName(key.first)) {
166 ids.insert(key.first);
167 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700168 }
169 return ids;
170}
171
172void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700173PibMemory::setDefaultKeyOfIdentity(const Name& identity, const Name& keyName)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700174{
Yingdi Yu03997682015-11-23 16:41:38 -0800175 if (!hasKey(keyName)) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700176 BOOST_THROW_EXCEPTION(Pib::Error("Key `" + keyName.toUri() + "` not found"));
Yingdi Yu03997682015-11-23 16:41:38 -0800177 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700178
Yingdi Yu03997682015-11-23 16:41:38 -0800179 m_defaultKeys[identity] = keyName;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700180}
181
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700182Name
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700183PibMemory::getDefaultKeyOfIdentity(const Name& identity) const
184{
Yingdi Yu03997682015-11-23 16:41:38 -0800185 auto defaultKey = m_defaultKeys.find(identity);
186 if (defaultKey == m_defaultKeys.end()) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700187 BOOST_THROW_EXCEPTION(Pib::Error("No default key for identity `" + identity.toUri() + "`"));
Yingdi Yu03997682015-11-23 16:41:38 -0800188 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700189
Yingdi Yu03997682015-11-23 16:41:38 -0800190 return defaultKey->second;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700191}
192
193bool
194PibMemory::hasCertificate(const Name& certName) const
195{
196 return (m_certs.count(certName) > 0);
197}
198
199void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700200PibMemory::addCertificate(const v2::Certificate& certificate)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700201{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700202 Name certName = certificate.getName();
203 Name keyName = certificate.getKeyName();
204 Name identity = certificate.getIdentity();
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700205
Yingdi Yu03997682015-11-23 16:41:38 -0800206 addKey(identity, keyName, certificate.getContent().value(), certificate.getContent().value_size());
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700207
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700208 m_certs[certName] = certificate;
Yingdi Yu03997682015-11-23 16:41:38 -0800209 if (m_defaultCerts.count(keyName) == 0) {
210 m_defaultCerts[keyName] = certName;
211 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700212}
213
214void
215PibMemory::removeCertificate(const Name& certName)
216{
217 m_certs.erase(certName);
Yingdi Yu03997682015-11-23 16:41:38 -0800218 auto defaultCert = m_defaultCerts.find(v2::extractKeyNameFromCertName(certName));
219 if (defaultCert != m_defaultCerts.end() && defaultCert->second == certName) {
220 m_defaultCerts.erase(defaultCert);
221 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700222}
223
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700224v2::Certificate
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700225PibMemory::getCertificate(const Name& certName) const
226{
Yingdi Yu03997682015-11-23 16:41:38 -0800227 if (!hasCertificate(certName)) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700228 BOOST_THROW_EXCEPTION(Pib::Error("Certificate `" + certName.toUri() + "` does not exist"));
Yingdi Yu03997682015-11-23 16:41:38 -0800229 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700230
231 auto it = m_certs.find(certName);
232 return it->second;
233}
234
235std::set<Name>
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700236PibMemory::getCertificatesOfKey(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700237{
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700238 std::set<Name> certNames;
239 for (const auto& it : m_certs) {
Yingdi Yu03997682015-11-23 16:41:38 -0800240 if (v2::extractKeyNameFromCertName(it.second.getName()) == keyName) {
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700241 certNames.insert(it.first);
Yingdi Yu03997682015-11-23 16:41:38 -0800242 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700243 }
244 return certNames;
245}
246
247void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700248PibMemory::setDefaultCertificateOfKey(const Name& keyName, const Name& certName)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700249{
Yingdi Yu03997682015-11-23 16:41:38 -0800250 if (!hasCertificate(certName)) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700251 BOOST_THROW_EXCEPTION(Pib::Error("Certificate `" + certName.toUri() + "` does not exist"));
Yingdi Yu03997682015-11-23 16:41:38 -0800252 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700253
Yingdi Yu03997682015-11-23 16:41:38 -0800254 m_defaultCerts[keyName] = certName;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700255}
256
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700257v2::Certificate
258PibMemory::getDefaultCertificateOfKey(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700259{
Yingdi Yu03997682015-11-23 16:41:38 -0800260 auto it = m_defaultCerts.find(keyName);
261 if (it == m_defaultCerts.end()) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700262 BOOST_THROW_EXCEPTION(Pib::Error("No default certificate for key `" + keyName.toUri() + "`"));
Yingdi Yu03997682015-11-23 16:41:38 -0800263 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700264
265 auto certIt = m_certs.find(it->second);
Yingdi Yu03997682015-11-23 16:41:38 -0800266 BOOST_ASSERT(certIt != m_certs.end());
267 return certIt->second;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700268}
269
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700270} // namespace pib
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700271} // namespace security
272} // namespace ndn