blob: 719d0c4a5a5f868c59a6ccd7a31ca73c98d12113 [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 Yu6ee2d362015-07-16 21:48:05 -070038 // The locator of PibMemory is always 'tpm-memory:'
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070039 BOOST_THROW_EXCEPTION(Error("PibMemory does not need a locator"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -070040}
41
42std::string
43PibMemory::getTpmLocator() const
44{
45 return "tpm-memory:";
46}
47
48bool
49PibMemory::hasIdentity(const Name& identity) const
50{
51 return (m_identities.count(identity) > 0);
52}
53
54void
55PibMemory::addIdentity(const Name& identity)
56{
57 m_identities.insert(identity);
58
59 if (!m_hasDefaultIdentity) {
60 m_defaultIdentity = identity;
61 m_hasDefaultIdentity = true;
62 }
63}
64
65void
66PibMemory::removeIdentity(const Name& identity)
67{
68 m_identities.erase(identity);
69 if (identity == m_defaultIdentity)
70 m_hasDefaultIdentity = false;
71
Yingdi Yu6ee2d362015-07-16 21:48:05 -070072 auto keyNames = this->getKeysOfIdentity(identity);
73 for (const Name& keyName : keyNames) {
74 this->removeKey(keyName);
Yingdi Yu3bf91f52015-06-12 19:39:40 -070075 }
76}
77
78std::set<Name>
79PibMemory::getIdentities() const
80{
81 return m_identities;
82}
83
84void
85PibMemory::setDefaultIdentity(const Name& identityName)
86{
87 addIdentity(identityName);
88 m_defaultIdentity = identityName;
89 m_hasDefaultIdentity = true;
90}
91
92Name
93PibMemory::getDefaultIdentity() const
94{
95 if (m_hasDefaultIdentity)
96 return m_defaultIdentity;
97
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070098 BOOST_THROW_EXCEPTION(Pib::Error("No default identity"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -070099}
100
101bool
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700102PibMemory::hasKey(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700103{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700104 return (m_keys.count(keyName) > 0);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700105}
106
107void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700108PibMemory::addKey(const Name& identity, const Name& keyName,
109 const uint8_t* key, size_t keyLen)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700110{
111 this->addIdentity(identity);
112
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700113 m_keys[keyName] = Buffer(key, keyLen);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700114
115 if (m_defaultKey.find(identity) == m_defaultKey.end())
116 m_defaultKey[identity] = keyName;
117}
118
119void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700120PibMemory::removeKey(const Name& keyName)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700121{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700122 Name identity = v2::extractIdentityFromKeyName(keyName);
123
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700124 m_keys.erase(keyName);
125 m_defaultKey.erase(identity);
126
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700127 auto certNames = this->getCertificatesOfKey(keyName);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700128 for (const auto& certName : certNames) {
129 this->removeCertificate(certName);
130 }
131}
132
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700133Buffer
134PibMemory::getKeyBits(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700135{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700136 if (!hasKey(keyName))
137 BOOST_THROW_EXCEPTION(Pib::Error("Key `" + keyName.toUri() + "` not found"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700138
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700139 auto it = m_keys.find(keyName);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700140 return it->second;
141}
142
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700143std::set<Name>
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700144PibMemory::getKeysOfIdentity(const Name& identity) const
145{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700146 std::set<Name> ids;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700147 for (const auto& it : m_keys) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700148 if (identity == v2::extractIdentityFromKeyName(it.first))
149 ids.insert(it.first);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700150 }
151 return ids;
152}
153
154void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700155PibMemory::setDefaultKeyOfIdentity(const Name& identity, const Name& keyName)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700156{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700157 if (!hasKey(keyName))
158 BOOST_THROW_EXCEPTION(Pib::Error("Key `" + keyName.toUri() + "` not found"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700159
160 m_defaultKey[identity] = keyName;
161}
162
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700163Name
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700164PibMemory::getDefaultKeyOfIdentity(const Name& identity) const
165{
166 auto it = m_defaultKey.find(identity);
167 if (it == m_defaultKey.end())
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700168 BOOST_THROW_EXCEPTION(Pib::Error("No default key for identity `" + identity.toUri() + "`"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700169
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700170 return it->second;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700171}
172
173bool
174PibMemory::hasCertificate(const Name& certName) const
175{
176 return (m_certs.count(certName) > 0);
177}
178
179void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700180PibMemory::addCertificate(const v2::Certificate& certificate)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700181{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700182 Name certName = certificate.getName();
183 Name keyName = certificate.getKeyName();
184 Name identity = certificate.getIdentity();
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700185
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700186 this->addKey(identity, keyName, certificate.getContent().value(), certificate.getContent().value_size());
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700187
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700188 m_certs[certName] = certificate;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700189 if (m_defaultCert.find(keyName) == m_defaultCert.end())
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700190 m_defaultCert[keyName] = certName;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700191}
192
193void
194PibMemory::removeCertificate(const Name& certName)
195{
196 m_certs.erase(certName);
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700197 m_defaultCert.erase(v2::extractKeyNameFromCertName(certName));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700198}
199
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700200v2::Certificate
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700201PibMemory::getCertificate(const Name& certName) const
202{
203 if (!hasCertificate(certName))
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700204 BOOST_THROW_EXCEPTION(Pib::Error("Certificate `" + certName.toUri() + "` does not exist"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700205
206 auto it = m_certs.find(certName);
207 return it->second;
208}
209
210std::set<Name>
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700211PibMemory::getCertificatesOfKey(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700212{
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700213 std::set<Name> certNames;
214 for (const auto& it : m_certs) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700215 if (v2::extractKeyNameFromCertName(it.second.getName()) == keyName)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700216 certNames.insert(it.first);
217 }
218 return certNames;
219}
220
221void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700222PibMemory::setDefaultCertificateOfKey(const Name& keyName, const Name& certName)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700223{
224 if (!hasCertificate(certName))
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700225 BOOST_THROW_EXCEPTION(Pib::Error("Certificate `" + certName.toUri() + "` does not exist"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700226
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700227 m_defaultCert[keyName] = certName;
228}
229
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700230v2::Certificate
231PibMemory::getDefaultCertificateOfKey(const Name& keyName) const
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700232{
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700233 auto it = m_defaultCert.find(keyName);
234 if (it == m_defaultCert.end())
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700235 BOOST_THROW_EXCEPTION(Pib::Error("No default certificate for key `" + keyName.toUri() + "`"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700236
237 auto certIt = m_certs.find(it->second);
238 if (certIt == m_certs.end())
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700239 BOOST_THROW_EXCEPTION(Pib::Error("No default certificate for key `" + keyName.toUri() + "`"));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700240 else
241 return certIt->second;
242}
243
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700244} // namespace pib
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700245} // namespace security
246} // namespace ndn