blob: 2d6aa7dd57d223714356a3e27c63a2576528e57c [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
Yingdi Yufe4733a2015-10-22 14:24:12 -070030PibMemory::PibMemory(const std::string&)
Yingdi Yu3bf91f52015-06-12 19:39:40 -070031 : m_hasDefaultIdentity(false)
32{
33}
34
Yingdi Yufe4733a2015-10-22 14:24:12 -070035const std::string&
36PibMemory::getScheme()
37{
38 static std::string scheme = "pib-memory";
39 return scheme;
40}
41
Yingdi Yu3bf91f52015-06-12 19:39:40 -070042void
43PibMemory::setTpmLocator(const std::string& tpmLocator)
44{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070045 m_tpmLocator = tpmLocator;
Yingdi Yu3bf91f52015-06-12 19:39:40 -070046}
47
48std::string
49PibMemory::getTpmLocator() const
50{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070051 return m_tpmLocator;
Yingdi Yu3bf91f52015-06-12 19:39:40 -070052}
53
54bool
55PibMemory::hasIdentity(const Name& identity) const
56{
57 return (m_identities.count(identity) > 0);
58}
59
60void
61PibMemory::addIdentity(const Name& identity)
62{
63 m_identities.insert(identity);
64
65 if (!m_hasDefaultIdentity) {
66 m_defaultIdentity = identity;
67 m_hasDefaultIdentity = true;
68 }
69}
70
71void
72PibMemory::removeIdentity(const Name& identity)
73{
74 m_identities.erase(identity);
Yingdi Yu03997682015-11-23 16:41:38 -080075 if (identity == m_defaultIdentity) {
Yingdi Yu3bf91f52015-06-12 19:39:40 -070076 m_hasDefaultIdentity = false;
Yingdi Yu03997682015-11-23 16:41:38 -080077 m_defaultIdentity.clear();
78 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -070079
Yingdi Yu03997682015-11-23 16:41:38 -080080 auto keyNames = getKeysOfIdentity(identity);
Yingdi Yu6ee2d362015-07-16 21:48:05 -070081 for (const Name& keyName : keyNames) {
Yingdi Yu03997682015-11-23 16:41:38 -080082 removeKey(keyName);
Yingdi Yu3bf91f52015-06-12 19:39:40 -070083 }
84}
85
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070086void
87PibMemory::clearIdentities()
88{
89 m_hasDefaultIdentity = false;
90 m_defaultIdentity.clear();
91 m_identities.clear();
Yingdi Yu03997682015-11-23 16:41:38 -080092 m_defaultKeys.clear();
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070093 m_keys.clear();
Yingdi Yu03997682015-11-23 16:41:38 -080094 m_defaultCerts.clear();
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070095 m_certs.clear();
96}
97
Yingdi Yu3bf91f52015-06-12 19:39:40 -070098std::set<Name>
99PibMemory::getIdentities() const
100{
101 return m_identities;
102}
103
104void
105PibMemory::setDefaultIdentity(const Name& identityName)
106{
107 addIdentity(identityName);
108 m_defaultIdentity = identityName;
109 m_hasDefaultIdentity = true;
110}
111
112Name
113PibMemory::getDefaultIdentity() const
114{
Yingdi Yu03997682015-11-23 16:41:38 -0800115 if (m_hasDefaultIdentity) {
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700116 return m_defaultIdentity;
Yingdi Yu03997682015-11-23 16:41:38 -0800117 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700118
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700119 BOOST_THROW_EXCEPTION(Pib::Error("No default identity"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700120}
121
122bool
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700123PibMemory::hasKey(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700124{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700125 return (m_keys.count(keyName) > 0);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700126}
127
128void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700129PibMemory::addKey(const Name& identity, const Name& keyName,
130 const uint8_t* key, size_t keyLen)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700131{
Yingdi Yu03997682015-11-23 16:41:38 -0800132 addIdentity(identity);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700133
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700134 m_keys[keyName] = Buffer(key, keyLen);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700135
Yingdi Yu03997682015-11-23 16:41:38 -0800136 if (m_defaultKeys.count(identity) == 0) {
137 m_defaultKeys[identity] = keyName;
138 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700139}
140
141void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700142PibMemory::removeKey(const Name& keyName)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700143{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700144 Name identity = v2::extractIdentityFromKeyName(keyName);
145
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700146 m_keys.erase(keyName);
Yingdi Yu03997682015-11-23 16:41:38 -0800147 m_defaultKeys.erase(identity);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700148
Yingdi Yu03997682015-11-23 16:41:38 -0800149 auto certNames = getCertificatesOfKey(keyName);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700150 for (const auto& certName : certNames) {
Yingdi Yu03997682015-11-23 16:41:38 -0800151 removeCertificate(certName);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700152 }
153}
154
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700155Buffer
156PibMemory::getKeyBits(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700157{
Yingdi Yu03997682015-11-23 16:41:38 -0800158 if (!hasKey(keyName)) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700159 BOOST_THROW_EXCEPTION(Pib::Error("Key `" + keyName.toUri() + "` not found"));
Yingdi Yu03997682015-11-23 16:41:38 -0800160 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700161
Yingdi Yu03997682015-11-23 16:41:38 -0800162 auto key = m_keys.find(keyName);
163 BOOST_ASSERT(key != m_keys.end());
164 return key->second;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700165}
166
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700167std::set<Name>
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700168PibMemory::getKeysOfIdentity(const Name& identity) const
169{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700170 std::set<Name> ids;
Yingdi Yu03997682015-11-23 16:41:38 -0800171 for (const auto& key : m_keys) {
172 if (identity == v2::extractIdentityFromKeyName(key.first)) {
173 ids.insert(key.first);
174 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700175 }
176 return ids;
177}
178
179void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700180PibMemory::setDefaultKeyOfIdentity(const Name& identity, const Name& keyName)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700181{
Yingdi Yu03997682015-11-23 16:41:38 -0800182 if (!hasKey(keyName)) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700183 BOOST_THROW_EXCEPTION(Pib::Error("Key `" + keyName.toUri() + "` not found"));
Yingdi Yu03997682015-11-23 16:41:38 -0800184 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700185
Yingdi Yu03997682015-11-23 16:41:38 -0800186 m_defaultKeys[identity] = keyName;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700187}
188
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700189Name
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700190PibMemory::getDefaultKeyOfIdentity(const Name& identity) const
191{
Yingdi Yu03997682015-11-23 16:41:38 -0800192 auto defaultKey = m_defaultKeys.find(identity);
193 if (defaultKey == m_defaultKeys.end()) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700194 BOOST_THROW_EXCEPTION(Pib::Error("No default key for identity `" + identity.toUri() + "`"));
Yingdi Yu03997682015-11-23 16:41:38 -0800195 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700196
Yingdi Yu03997682015-11-23 16:41:38 -0800197 return defaultKey->second;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700198}
199
200bool
201PibMemory::hasCertificate(const Name& certName) const
202{
203 return (m_certs.count(certName) > 0);
204}
205
206void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700207PibMemory::addCertificate(const v2::Certificate& certificate)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700208{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700209 Name certName = certificate.getName();
210 Name keyName = certificate.getKeyName();
211 Name identity = certificate.getIdentity();
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700212
Yingdi Yu03997682015-11-23 16:41:38 -0800213 addKey(identity, keyName, certificate.getContent().value(), certificate.getContent().value_size());
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700214
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700215 m_certs[certName] = certificate;
Yingdi Yu03997682015-11-23 16:41:38 -0800216 if (m_defaultCerts.count(keyName) == 0) {
217 m_defaultCerts[keyName] = certName;
218 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700219}
220
221void
222PibMemory::removeCertificate(const Name& certName)
223{
224 m_certs.erase(certName);
Yingdi Yu03997682015-11-23 16:41:38 -0800225 auto defaultCert = m_defaultCerts.find(v2::extractKeyNameFromCertName(certName));
226 if (defaultCert != m_defaultCerts.end() && defaultCert->second == certName) {
227 m_defaultCerts.erase(defaultCert);
228 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700229}
230
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700231v2::Certificate
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700232PibMemory::getCertificate(const Name& certName) const
233{
Yingdi Yu03997682015-11-23 16:41:38 -0800234 if (!hasCertificate(certName)) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700235 BOOST_THROW_EXCEPTION(Pib::Error("Certificate `" + certName.toUri() + "` does not exist"));
Yingdi Yu03997682015-11-23 16:41:38 -0800236 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700237
238 auto it = m_certs.find(certName);
239 return it->second;
240}
241
242std::set<Name>
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700243PibMemory::getCertificatesOfKey(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700244{
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700245 std::set<Name> certNames;
246 for (const auto& it : m_certs) {
Yingdi Yu03997682015-11-23 16:41:38 -0800247 if (v2::extractKeyNameFromCertName(it.second.getName()) == keyName) {
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700248 certNames.insert(it.first);
Yingdi Yu03997682015-11-23 16:41:38 -0800249 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700250 }
251 return certNames;
252}
253
254void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700255PibMemory::setDefaultCertificateOfKey(const Name& keyName, const Name& certName)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700256{
Yingdi Yu03997682015-11-23 16:41:38 -0800257 if (!hasCertificate(certName)) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700258 BOOST_THROW_EXCEPTION(Pib::Error("Certificate `" + certName.toUri() + "` does not exist"));
Yingdi Yu03997682015-11-23 16:41:38 -0800259 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700260
Yingdi Yu03997682015-11-23 16:41:38 -0800261 m_defaultCerts[keyName] = certName;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700262}
263
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700264v2::Certificate
265PibMemory::getDefaultCertificateOfKey(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700266{
Yingdi Yu03997682015-11-23 16:41:38 -0800267 auto it = m_defaultCerts.find(keyName);
268 if (it == m_defaultCerts.end()) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700269 BOOST_THROW_EXCEPTION(Pib::Error("No default certificate for key `" + keyName.toUri() + "`"));
Yingdi Yu03997682015-11-23 16:41:38 -0800270 }
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700271
272 auto certIt = m_certs.find(it->second);
Yingdi Yu03997682015-11-23 16:41:38 -0800273 BOOST_ASSERT(certIt != m_certs.end());
274 return certIt->second;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700275}
276
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700277} // namespace pib
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700278} // namespace security
279} // namespace ndn