NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
pib-memory.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 Regents of the University of California.
4  *
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 
25 
26 #include <boost/range/adaptor/map.hpp>
27 
28 namespace ndn {
29 namespace security {
30 namespace pib {
31 
32 PibMemory::PibMemory(const std::string&)
33  : m_hasDefaultIdentity(false)
34 {
35 }
36 
37 const std::string&
39 {
40  static std::string scheme = "pib-memory";
41  return scheme;
42 }
43 
44 void
45 PibMemory::setTpmLocator(const std::string& tpmLocator)
46 {
47  m_tpmLocator = tpmLocator;
48 }
49 
50 std::string
52 {
53  return m_tpmLocator;
54 }
55 
56 bool
57 PibMemory::hasIdentity(const Name& identity) const
58 {
59  return (m_identities.count(identity) > 0);
60 }
61 
62 void
63 PibMemory::addIdentity(const Name& identity)
64 {
65  m_identities.insert(identity);
66 
67  if (!m_hasDefaultIdentity) {
68  m_defaultIdentity = identity;
69  m_hasDefaultIdentity = true;
70  }
71 }
72 
73 void
75 {
76  m_identities.erase(identity);
77  if (identity == m_defaultIdentity) {
78  m_hasDefaultIdentity = false;
79  m_defaultIdentity.clear();
80  }
81 
82  auto keyNames = getKeysOfIdentity(identity);
83  for (const Name& keyName : keyNames) {
84  removeKey(keyName);
85  }
86 }
87 
88 void
90 {
91  m_hasDefaultIdentity = false;
92  m_defaultIdentity.clear();
93  m_identities.clear();
94  m_defaultKeys.clear();
95  m_keys.clear();
96  m_defaultCerts.clear();
97  m_certs.clear();
98 }
99 
100 std::set<Name>
102 {
103  return m_identities;
104 }
105 
106 void
108 {
109  if (!hasIdentity(identityName)) {
110  NDN_THROW(Pib::Error("Cannot set non-existing identity `" + identityName.toUri() + "` as default"));
111  }
112  m_defaultIdentity = identityName;
113  m_hasDefaultIdentity = true;
114 }
115 
116 Name
118 {
119  if (m_hasDefaultIdentity) {
120  return m_defaultIdentity;
121  }
122 
123  NDN_THROW(Pib::Error("No default identity"));
124 }
125 
126 bool
127 PibMemory::hasKey(const Name& keyName) const
128 {
129  return m_keys.count(keyName) > 0;
130 }
131 
132 void
133 PibMemory::addKey(const Name& identity, const Name& keyName, span<const uint8_t> key)
134 {
135  addIdentity(identity);
136 
137  m_keys[keyName] = Buffer(key.begin(), key.end());
138 
139  if (m_defaultKeys.count(identity) == 0) {
140  m_defaultKeys[identity] = keyName;
141  }
142 }
143 
144 void
145 PibMemory::removeKey(const Name& keyName)
146 {
147  Name identity = extractIdentityFromKeyName(keyName);
148 
149  m_keys.erase(keyName);
150  m_defaultKeys.erase(identity);
151 
152  auto certNames = getCertificatesOfKey(keyName);
153  for (const auto& certName : certNames) {
154  removeCertificate(certName);
155  }
156 }
157 
158 Buffer
159 PibMemory::getKeyBits(const Name& keyName) const
160 {
161  if (!hasKey(keyName)) {
162  NDN_THROW(Pib::Error("Key `" + keyName.toUri() + "` not found"));
163  }
164 
165  auto key = m_keys.find(keyName);
166  BOOST_ASSERT(key != m_keys.end());
167  return key->second;
168 }
169 
170 std::set<Name>
171 PibMemory::getKeysOfIdentity(const Name& identity) const
172 {
173  std::set<Name> ids;
174  for (const auto& keyName : m_keys | boost::adaptors::map_keys) {
175  if (identity == extractIdentityFromKeyName(keyName)) {
176  ids.insert(keyName);
177  }
178  }
179  return ids;
180 }
181 
182 void
183 PibMemory::setDefaultKeyOfIdentity(const Name& identity, const Name& keyName)
184 {
185  if (!hasKey(keyName)) {
186  NDN_THROW(Pib::Error("Key `" + keyName.toUri() + "` not found"));
187  }
188 
189  m_defaultKeys[identity] = keyName;
190 }
191 
192 Name
194 {
195  auto defaultKey = m_defaultKeys.find(identity);
196  if (defaultKey == m_defaultKeys.end()) {
197  NDN_THROW(Pib::Error("No default key for identity `" + identity.toUri() + "`"));
198  }
199 
200  return defaultKey->second;
201 }
202 
203 bool
204 PibMemory::hasCertificate(const Name& certName) const
205 {
206  return (m_certs.count(certName) > 0);
207 }
208 
209 void
210 PibMemory::addCertificate(const Certificate& certificate)
211 {
212  const Name& certName = certificate.getName();
213  const Name& keyName = certificate.getKeyName();
214 
215  addKey(certificate.getIdentity(), keyName, certificate.getContent().value_bytes());
216 
217  m_certs[certName] = certificate;
218  if (m_defaultCerts.count(keyName) == 0) {
219  m_defaultCerts[keyName] = certName;
220  }
221 }
222 
223 void
225 {
226  m_certs.erase(certName);
227  auto defaultCert = m_defaultCerts.find(extractKeyNameFromCertName(certName));
228  if (defaultCert != m_defaultCerts.end() && defaultCert->second == certName) {
229  m_defaultCerts.erase(defaultCert);
230  }
231 }
232 
233 Certificate
234 PibMemory::getCertificate(const Name& certName) const
235 {
236  if (!hasCertificate(certName)) {
237  NDN_THROW(Pib::Error("Certificate `" + certName.toUri() + "` does not exist"));
238  }
239 
240  auto it = m_certs.find(certName);
241  return it->second;
242 }
243 
244 std::set<Name>
246 {
247  std::set<Name> certNames;
248  for (const auto& it : m_certs) {
249  if (extractKeyNameFromCertName(it.second.getName()) == keyName) {
250  certNames.insert(it.first);
251  }
252  }
253  return certNames;
254 }
255 
256 void
257 PibMemory::setDefaultCertificateOfKey(const Name& keyName, const Name& certName)
258 {
259  if (!hasCertificate(certName)) {
260  NDN_THROW(Pib::Error("Certificate `" + certName.toUri() + "` does not exist"));
261  }
262 
263  m_defaultCerts[keyName] = certName;
264 }
265 
266 Certificate
268 {
269  auto it = m_defaultCerts.find(keyName);
270  if (it == m_defaultCerts.end()) {
271  NDN_THROW(Pib::Error("No default certificate for key `" + keyName.toUri() + "`"));
272  }
273 
274  auto certIt = m_certs.find(it->second);
275  BOOST_ASSERT(certIt != m_certs.end());
276  return certIt->second;
277 }
278 
279 } // namespace pib
280 } // namespace security
281 } // namespace ndn
bool hasIdentity(const Name &identity) const override
Check the existence of an identity.
Definition: pib-memory.cpp:57
Copyright (c) 2011-2015 Regents of the University of California.
represents a semantic error
Definition: pib.hpp:56
Name getDefaultKeyOfIdentity(const Name &identity) const override
Definition: pib-memory.cpp:193
void addCertificate(const Certificate &certificate) override
Add a certificate.
Definition: pib-memory.cpp:210
void clearIdentities() override
Erasing all certificates, keys, and identities.
Definition: pib-memory.cpp:89
static const std::string & getScheme()
Definition: pib-memory.cpp:38
void removeKey(const Name &keyName) override
Remove a key with keyName and related certificates.
Definition: pib-memory.cpp:145
void removeIdentity(const Name &identity) override
Remove an identity and related keys and certificates.
Definition: pib-memory.cpp:74
std::string getTpmLocator() const override
Get TPM Locator.
Definition: pib-memory.cpp:51
Name extractKeyNameFromCertName(const Name &certName)
Extract key name from the certificate name certName.
void setDefaultCertificateOfKey(const Name &keyName, const Name &certName) override
Set a cert with name certName as the default of a key with keyName.
Definition: pib-memory.cpp:257
std::set< Name > getIdentities() const override
Get the name of all the identities.
Definition: pib-memory.cpp:101
bool hasKey(const Name &keyName) const override
Check the existence of a key with keyName.
Definition: pib-memory.cpp:127
PibMemory(const std::string &location="")
Create memory based PIB backend.
Definition: pib-memory.cpp:32
void setTpmLocator(const std::string &tpmLocator) override
Set the corresponding TPM information to tpmLocator.
Definition: pib-memory.cpp:45
#define NDN_THROW(e)
Definition: exception.hpp:61
void setDefaultKeyOfIdentity(const Name &identity, const Name &keyName) override
Set an key with keyName as the default key of an identity with name identity.
Definition: pib-memory.cpp:183
Certificate getCertificate(const Name &certName) const override
Get a certificate with name certName.
Definition: pib-memory.cpp:234
void removeCertificate(const Name &certName) override
Remove a certificate with name certName.
Definition: pib-memory.cpp:224
Name getDefaultIdentity() const override
Get the default identity.
Definition: pib-memory.cpp:117
Represents an absolute name.
Definition: name.hpp:41
void setDefaultIdentity(const Name &identityName) override
Set an identity with name identityName as the default identity.
Definition: pib-memory.cpp:107
bool hasCertificate(const Name &certName) const override
Check the existence of a certificate with name certName.
Definition: pib-memory.cpp:204
void addIdentity(const Name &identity) override
Add an identity.
Definition: pib-memory.cpp:63
void toUri(std::ostream &os, name::UriFormat format=name::UriFormat::DEFAULT) const
Write URI representation of the name to the output stream.
Definition: name.cpp:349
Buffer getKeyBits(const Name &keyName) const override
Get the key bits of a key with name keyName.
Definition: pib-memory.cpp:159
Certificate getDefaultCertificateOfKey(const Name &keyName) const override
Definition: pib-memory.cpp:267
void addKey(const Name &identity, const Name &keyName, span< const uint8_t > key) override
Add a key.
Definition: pib-memory.cpp:133
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
Definition: key.cpp:160
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:41
void clear()
Remove all components.
Definition: name.cpp:281
std::set< Name > getKeysOfIdentity(const Name &identity) const override
Get all the key names of an identity with name identity.
Definition: pib-memory.cpp:171
std::set< Name > getCertificatesOfKey(const Name &keyName) const override
Get a list of certificate names of a key with id keyName.
Definition: pib-memory.cpp:245