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-2019 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  addIdentity(identityName);
110  m_defaultIdentity = identityName;
111  m_hasDefaultIdentity = true;
112 }
113 
114 Name
116 {
117  if (m_hasDefaultIdentity) {
118  return m_defaultIdentity;
119  }
120 
121  NDN_THROW(Pib::Error("No default identity"));
122 }
123 
124 bool
125 PibMemory::hasKey(const Name& keyName) const
126 {
127  return (m_keys.count(keyName) > 0);
128 }
129 
130 void
131 PibMemory::addKey(const Name& identity, const Name& keyName,
132  const uint8_t* key, size_t keyLen)
133 {
134  addIdentity(identity);
135 
136  m_keys[keyName] = Buffer(key, keyLen);
137 
138  if (m_defaultKeys.count(identity) == 0) {
139  m_defaultKeys[identity] = keyName;
140  }
141 }
142 
143 void
144 PibMemory::removeKey(const Name& keyName)
145 {
146  Name identity = v2::extractIdentityFromKeyName(keyName);
147 
148  m_keys.erase(keyName);
149  m_defaultKeys.erase(identity);
150 
151  auto certNames = getCertificatesOfKey(keyName);
152  for (const auto& certName : certNames) {
153  removeCertificate(certName);
154  }
155 }
156 
157 Buffer
158 PibMemory::getKeyBits(const Name& keyName) const
159 {
160  if (!hasKey(keyName)) {
161  NDN_THROW(Pib::Error("Key `" + keyName.toUri() + "` not found"));
162  }
163 
164  auto key = m_keys.find(keyName);
165  BOOST_ASSERT(key != m_keys.end());
166  return key->second;
167 }
168 
169 std::set<Name>
170 PibMemory::getKeysOfIdentity(const Name& identity) const
171 {
172  std::set<Name> ids;
173  for (const auto& keyName : m_keys | boost::adaptors::map_keys) {
174  if (identity == v2::extractIdentityFromKeyName(keyName)) {
175  ids.insert(keyName);
176  }
177  }
178  return ids;
179 }
180 
181 void
182 PibMemory::setDefaultKeyOfIdentity(const Name& identity, const Name& keyName)
183 {
184  if (!hasKey(keyName)) {
185  NDN_THROW(Pib::Error("Key `" + keyName.toUri() + "` not found"));
186  }
187 
188  m_defaultKeys[identity] = keyName;
189 }
190 
191 Name
193 {
194  auto defaultKey = m_defaultKeys.find(identity);
195  if (defaultKey == m_defaultKeys.end()) {
196  NDN_THROW(Pib::Error("No default key for identity `" + identity.toUri() + "`"));
197  }
198 
199  return defaultKey->second;
200 }
201 
202 bool
203 PibMemory::hasCertificate(const Name& certName) const
204 {
205  return (m_certs.count(certName) > 0);
206 }
207 
208 void
210 {
211  Name certName = certificate.getName();
212  Name keyName = certificate.getKeyName();
213  Name identity = certificate.getIdentity();
214 
215  addKey(identity, keyName, certificate.getContent().value(), certificate.getContent().value_size());
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(v2::extractKeyNameFromCertName(certName));
228  if (defaultCert != m_defaultCerts.end() && defaultCert->second == certName) {
229  m_defaultCerts.erase(defaultCert);
230  }
231 }
232 
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 (v2::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 
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
ndn::security::pib::PibMemory::getScheme
static const std::string & getScheme()
Definition: pib-memory.cpp:38
ndn::security::pib::PibMemory::removeCertificate
void removeCertificate(const Name &certName) override
Remove a certificate with name certName.
Definition: pib-memory.cpp:224
ndn::security::pib::PibMemory::setTpmLocator
void setTpmLocator(const std::string &tpmLocator) override
Set the corresponding TPM information to tpmLocator.
Definition: pib-memory.cpp:45
ndn::security::pib::PibMemory::getTpmLocator
std::string getTpmLocator() const override
Get TPM Locator.
Definition: pib-memory.cpp:51
ndn::security::v2::Certificate::getIdentity
Name getIdentity() const
Get identity name.
Definition: certificate.cpp:87
ndn::security::pib::PibMemory::hasCertificate
bool hasCertificate(const Name &certName) const override
Check the existence of a certificate with name certName.
Definition: pib-memory.cpp:203
ndn::security::pib::PibMemory::addIdentity
void addIdentity(const Name &identity) override
Add an identity.
Definition: pib-memory.cpp:63
ndn::Block::value_size
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH.
Definition: block.cpp:308
security-common.hpp
ndn::Buffer
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:41
ndn::security::v2::Certificate::getKeyName
Name getKeyName() const
Get key name.
Definition: certificate.cpp:81
ndn::security::pib::PibMemory::addKey
void addKey(const Name &identity, const Name &keyName, const uint8_t *key, size_t keyLen) override
Add a key.
Definition: pib-memory.cpp:131
ndn::Data::getContent
const Block & getContent() const
Get Content.
Definition: data.cpp:232
ndn::security::pib::PibMemory::getKeyBits
Buffer getKeyBits(const Name &keyName) const override
Get the key bits of a key with name keyName.
Definition: pib-memory.cpp:158
ndn::Data::getName
const Name & getName() const
Get name.
Definition: data.hpp:124
ndn::security::v2::extractIdentityFromKeyName
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
Definition: key.cpp:160
ndn::security::pib::PibMemory::getDefaultIdentity
Name getDefaultIdentity() const override
Get the default identity.
Definition: pib-memory.cpp:115
ndn::security::pib::PibMemory::getDefaultKeyOfIdentity
Name getDefaultKeyOfIdentity(const Name &identity) const override
Definition: pib-memory.cpp:192
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::security::pib::PibMemory::getDefaultCertificateOfKey
v2::Certificate getDefaultCertificateOfKey(const Name &keyName) const override
Definition: pib-memory.cpp:267
ndn::security::v2::Certificate
The certificate following the certificate format naming convention.
Definition: certificate.hpp:82
ndn::security::pib::PibMemory::setDefaultCertificateOfKey
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
ndn::security::pib::PibMemory::hasKey
bool hasKey(const Name &keyName) const override
Check the existence of a key with keyName.
Definition: pib-memory.cpp:125
ndn::security::pib::PibMemory::getCertificatesOfKey
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
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::security::pib::Pib::Error
represents a semantic error
Definition: pib.hpp:57
pib-memory.hpp
ndn::security::pib::PibMemory::removeIdentity
void removeIdentity(const Name &identity) override
Remove an identity and related keys and certificates.
Definition: pib-memory.cpp:74
ndn::security::v2::extractKeyNameFromCertName
Name extractKeyNameFromCertName(const Name &certName)
Extract key name from the certificate name certName.
Definition: certificate.cpp:196
ndn::security::pib::PibMemory::hasIdentity
bool hasIdentity(const Name &identity) const override
Check the existence of an identity.
Definition: pib-memory.cpp:57
ndn::Name::toUri
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:348
ndn::security::pib::PibMemory::getKeysOfIdentity
std::set< Name > getKeysOfIdentity(const Name &identity) const override
Get all the key names of an identity with name identity.
Definition: pib-memory.cpp:170
ndn::security::pib::PibMemory::removeKey
void removeKey(const Name &keyName) override
Remove a key with keyName and related certificates.
Definition: pib-memory.cpp:144
ndn::security::pib::PibMemory::clearIdentities
void clearIdentities() override
Erasing all certificates, keys, and identities.
Definition: pib-memory.cpp:89
pib.hpp
ndn::Block::value
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
Definition: block.cpp:302
ndn::security::pib::PibMemory::addCertificate
void addCertificate(const v2::Certificate &certificate) override
Add a certificate.
Definition: pib-memory.cpp:209
ndn::Name::clear
void clear()
Remove all components.
Definition: name.cpp:280
ndn::security::pib::PibMemory::setDefaultKeyOfIdentity
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:182
ndn::security::pib::PibMemory::PibMemory
PibMemory(const std::string &location="")
Create memory based PIB backend.
Definition: pib-memory.cpp:32
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::security::pib::PibMemory::getCertificate
v2::Certificate getCertificate(const Name &certName) const override
Get a certificate with name certName.
Definition: pib-memory.cpp:234
ndn::security::pib::PibMemory::getIdentities
std::set< Name > getIdentities() const override
Get the name of all the identities.
Definition: pib-memory.cpp:101
ndn::security::pib::PibMemory::setDefaultIdentity
void setDefaultIdentity(const Name &identityName) override
Set an identity with name identityName as the default identity.
Definition: pib-memory.cpp:107