blob: edf2ce90b171d4e441989445c7d85b4ace7cdec3 [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);
68 if (identity == m_defaultIdentity)
69 m_hasDefaultIdentity = false;
70
Yingdi Yu6ee2d362015-07-16 21:48:05 -070071 auto keyNames = this->getKeysOfIdentity(identity);
72 for (const Name& keyName : keyNames) {
73 this->removeKey(keyName);
Yingdi Yu3bf91f52015-06-12 19:39:40 -070074 }
75}
76
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070077void
78PibMemory::clearIdentities()
79{
80 m_hasDefaultIdentity = false;
81 m_defaultIdentity.clear();
82 m_identities.clear();
83 m_defaultKey.clear();
84 m_keys.clear();
85 m_defaultCert.clear();
86 m_certs.clear();
87}
88
Yingdi Yu3bf91f52015-06-12 19:39:40 -070089std::set<Name>
90PibMemory::getIdentities() const
91{
92 return m_identities;
93}
94
95void
96PibMemory::setDefaultIdentity(const Name& identityName)
97{
98 addIdentity(identityName);
99 m_defaultIdentity = identityName;
100 m_hasDefaultIdentity = true;
101}
102
103Name
104PibMemory::getDefaultIdentity() const
105{
106 if (m_hasDefaultIdentity)
107 return m_defaultIdentity;
108
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700109 BOOST_THROW_EXCEPTION(Pib::Error("No default identity"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700110}
111
112bool
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700113PibMemory::hasKey(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700114{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700115 return (m_keys.count(keyName) > 0);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700116}
117
118void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700119PibMemory::addKey(const Name& identity, const Name& keyName,
120 const uint8_t* key, size_t keyLen)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700121{
122 this->addIdentity(identity);
123
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700124 m_keys[keyName] = Buffer(key, keyLen);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700125
126 if (m_defaultKey.find(identity) == m_defaultKey.end())
127 m_defaultKey[identity] = keyName;
128}
129
130void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700131PibMemory::removeKey(const Name& keyName)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700132{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700133 Name identity = v2::extractIdentityFromKeyName(keyName);
134
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700135 m_keys.erase(keyName);
136 m_defaultKey.erase(identity);
137
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700138 auto certNames = this->getCertificatesOfKey(keyName);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700139 for (const auto& certName : certNames) {
140 this->removeCertificate(certName);
141 }
142}
143
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700144Buffer
145PibMemory::getKeyBits(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700146{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700147 if (!hasKey(keyName))
148 BOOST_THROW_EXCEPTION(Pib::Error("Key `" + keyName.toUri() + "` not found"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700149
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700150 auto it = m_keys.find(keyName);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700151 return it->second;
152}
153
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700154std::set<Name>
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700155PibMemory::getKeysOfIdentity(const Name& identity) const
156{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700157 std::set<Name> ids;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700158 for (const auto& it : m_keys) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700159 if (identity == v2::extractIdentityFromKeyName(it.first))
160 ids.insert(it.first);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700161 }
162 return ids;
163}
164
165void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700166PibMemory::setDefaultKeyOfIdentity(const Name& identity, const Name& keyName)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700167{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700168 if (!hasKey(keyName))
169 BOOST_THROW_EXCEPTION(Pib::Error("Key `" + keyName.toUri() + "` not found"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700170
171 m_defaultKey[identity] = keyName;
172}
173
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700174Name
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700175PibMemory::getDefaultKeyOfIdentity(const Name& identity) const
176{
177 auto it = m_defaultKey.find(identity);
178 if (it == m_defaultKey.end())
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700179 BOOST_THROW_EXCEPTION(Pib::Error("No default key for identity `" + identity.toUri() + "`"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700180
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700181 return it->second;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700182}
183
184bool
185PibMemory::hasCertificate(const Name& certName) const
186{
187 return (m_certs.count(certName) > 0);
188}
189
190void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700191PibMemory::addCertificate(const v2::Certificate& certificate)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700192{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700193 Name certName = certificate.getName();
194 Name keyName = certificate.getKeyName();
195 Name identity = certificate.getIdentity();
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700196
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700197 this->addKey(identity, keyName, certificate.getContent().value(), certificate.getContent().value_size());
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700198
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700199 m_certs[certName] = certificate;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700200 if (m_defaultCert.find(keyName) == m_defaultCert.end())
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700201 m_defaultCert[keyName] = certName;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700202}
203
204void
205PibMemory::removeCertificate(const Name& certName)
206{
207 m_certs.erase(certName);
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700208 m_defaultCert.erase(v2::extractKeyNameFromCertName(certName));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700209}
210
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700211v2::Certificate
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700212PibMemory::getCertificate(const Name& certName) const
213{
214 if (!hasCertificate(certName))
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700215 BOOST_THROW_EXCEPTION(Pib::Error("Certificate `" + certName.toUri() + "` does not exist"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700216
217 auto it = m_certs.find(certName);
218 return it->second;
219}
220
221std::set<Name>
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700222PibMemory::getCertificatesOfKey(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700223{
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700224 std::set<Name> certNames;
225 for (const auto& it : m_certs) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700226 if (v2::extractKeyNameFromCertName(it.second.getName()) == keyName)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700227 certNames.insert(it.first);
228 }
229 return certNames;
230}
231
232void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700233PibMemory::setDefaultCertificateOfKey(const Name& keyName, const Name& certName)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700234{
235 if (!hasCertificate(certName))
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700236 BOOST_THROW_EXCEPTION(Pib::Error("Certificate `" + certName.toUri() + "` does not exist"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700237
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700238 m_defaultCert[keyName] = certName;
239}
240
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700241v2::Certificate
242PibMemory::getDefaultCertificateOfKey(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700243{
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700244 auto it = m_defaultCert.find(keyName);
245 if (it == m_defaultCert.end())
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700246 BOOST_THROW_EXCEPTION(Pib::Error("No default certificate for key `" + keyName.toUri() + "`"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700247
248 auto certIt = m_certs.find(it->second);
249 if (certIt == m_certs.end())
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700250 BOOST_THROW_EXCEPTION(Pib::Error("No default certificate for key `" + keyName.toUri() + "`"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700251 else
252 return certIt->second;
253}
254
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700255} // namespace pib
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700256} // namespace security
257} // namespace ndn