NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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; -*- */
22 #include "pib-memory.hpp"
23 #include "pib.hpp"
24 
25 namespace ndn {
26 namespace security {
27 
29  : m_hasDefaultIdentity(false)
30 {
31 }
32 
33 void
34 PibMemory::setTpmLocator(const std::string& tpmLocator)
35 {
36  BOOST_THROW_EXCEPTION(Error("PibMemory does not need a locator"));
37 }
38 
39 std::string
41 {
42  return "tpm-memory:";
43 }
44 
45 bool
46 PibMemory::hasIdentity(const Name& identity) const
47 {
48  return (m_identities.count(identity) > 0);
49 }
50 
51 void
52 PibMemory::addIdentity(const Name& identity)
53 {
54  m_identities.insert(identity);
55 
56  if (!m_hasDefaultIdentity) {
57  m_defaultIdentity = identity;
58  m_hasDefaultIdentity = true;
59  }
60 }
61 
62 void
64 {
65  m_identities.erase(identity);
66  if (identity == m_defaultIdentity)
67  m_hasDefaultIdentity = false;
68 
69  auto keyIds = this->getKeysOfIdentity(identity);
70  for (const name::Component& keyId : keyIds) {
71  this->removeKey(identity, keyId);
72  }
73 }
74 
75 std::set<Name>
77 {
78  return m_identities;
79 }
80 
81 void
82 PibMemory::setDefaultIdentity(const Name& identityName)
83 {
84  addIdentity(identityName);
85  m_defaultIdentity = identityName;
86  m_hasDefaultIdentity = true;
87 }
88 
89 Name
91 {
92  if (m_hasDefaultIdentity)
93  return m_defaultIdentity;
94 
95  BOOST_THROW_EXCEPTION(Pib::Error("No default identity"));
96 }
97 
98 bool
99 PibMemory::hasKey(const Name& identity, const name::Component& keyId) const
100 {
101  return (m_keys.count(getKeyName(identity, keyId)) > 0);
102 }
103 
104 void
105 PibMemory::addKey(const Name& identity, const name::Component& keyId, const PublicKey& publicKey)
106 {
107  this->addIdentity(identity);
108 
109  Name keyName = getKeyName(identity, keyId);
110  m_keys[keyName] = publicKey;
111 
112  if (m_defaultKey.find(identity) == m_defaultKey.end())
113  m_defaultKey[identity] = keyName;
114 }
115 
116 void
117 PibMemory::removeKey(const Name& identity, const name::Component& keyId)
118 {
119  Name keyName = getKeyName(identity, keyId);
120  m_keys.erase(keyName);
121  m_defaultKey.erase(identity);
122 
123 
124  auto certNames = this->getCertificatesOfKey(identity, keyId);
125  for (const auto& certName : certNames) {
126  this->removeCertificate(certName);
127  }
128 }
129 
130 PublicKey
131 PibMemory::getKeyBits(const Name& identity, const name::Component& keyId) const
132 {
133  if (!hasKey(identity, keyId))
134  BOOST_THROW_EXCEPTION(Pib::Error("No key"));
135 
136  auto it = m_keys.find(getKeyName(identity, keyId));
137  return it->second;
138 }
139 
140 std::set<name::Component>
141 PibMemory::getKeysOfIdentity(const Name& identity) const
142 {
143  std::set<name::Component> ids;
144  for (const auto& it : m_keys) {
145  if (identity == it.first.getPrefix(-1))
146  ids.insert(it.first.get(-1));
147  }
148  return ids;
149 }
150 
151 void
153 {
154  Name keyName = getKeyName(identity, keyId);
155 
156  if (!hasKey(identity, keyId))
157  BOOST_THROW_EXCEPTION(Pib::Error("No key"));
158 
159  m_defaultKey[identity] = keyName;
160 }
161 
164 {
165  auto it = m_defaultKey.find(identity);
166  if (it == m_defaultKey.end())
167  BOOST_THROW_EXCEPTION(Pib::Error("No default key"));
168 
169  return it->second.get(-1);
170 }
171 
172 Name
173 PibMemory::getKeyName(const Name& identity, const name::Component& keyId) const
174 {
175  Name keyName = identity;
176  keyName.append(keyId);
177  return keyName;
178 }
179 
180 bool
181 PibMemory::hasCertificate(const Name& certName) const
182 {
183  return (m_certs.count(certName) > 0);
184 }
185 
186 void
188 {
189  this->addKey(certificate.getPublicKeyName().getPrefix(-1),
190  certificate.getPublicKeyName().get(-1),
191  certificate.getPublicKeyInfo());
192 
193  m_certs[certificate.getName()] = certificate;
194 
195  const Name& keyName = certificate.getPublicKeyName();
196  if (m_defaultCert.find(keyName) == m_defaultCert.end())
197  m_defaultCert[keyName] = certificate.getName();
198 }
199 
200 void
202 {
203  m_certs.erase(certName);
204  m_defaultCert.erase(IdentityCertificate::certificateNameToPublicKeyName(certName));
205 }
206 
208 PibMemory::getCertificate(const Name& certName) const
209 {
210  if (!hasCertificate(certName))
211  BOOST_THROW_EXCEPTION(Pib::Error("No cert"));
212 
213  auto it = m_certs.find(certName);
214  return it->second;
215 }
216 
217 std::set<Name>
218 PibMemory::getCertificatesOfKey(const Name& identity, const name::Component& keyId) const
219 {
220  Name keyName = getKeyName(identity, keyId);
221 
222  std::set<Name> certNames;
223  for (const auto& it : m_certs) {
224  if (it.second.getPublicKeyName() == keyName)
225  certNames.insert(it.first);
226  }
227  return certNames;
228 }
229 
230 void
231 PibMemory::setDefaultCertificateOfKey(const Name& identity, const name::Component& keyId, const Name& certName)
232 {
233  if (!hasCertificate(certName))
234  BOOST_THROW_EXCEPTION(Pib::Error("No cert"));
235 
236  Name keyName = getKeyName(identity, keyId);
237  m_defaultCert[keyName] = certName;
238 }
239 
241 PibMemory::getDefaultCertificateOfKey(const Name& identity, const name::Component& keyId) const
242 {
243  Name keyName = getKeyName(identity, keyId);
244 
245  auto it = m_defaultCert.find(keyName);
246  if (it == m_defaultCert.end())
247  BOOST_THROW_EXCEPTION(Pib::Error("No default certificate"));
248 
249  auto certIt = m_certs.find(it->second);
250  if (certIt == m_certs.end())
251  BOOST_THROW_EXCEPTION(Pib::Error("No default certificate"));
252  else
253  return certIt->second;
254 }
255 
256 } // namespace security
257 } // namespace ndn
virtual IdentityCertificate getCertificate(const Name &certName) const override
Get a certificate with name certName.
Definition: pib-memory.cpp:208
virtual void addKey(const Name &identity, const name::Component &keyId, const PublicKey &publicKey) override
Add a key.
Definition: pib-memory.cpp:105
Copyright (c) 2011-2015 Regents of the University of California.
virtual void removeIdentity(const Name &identity) override
Remove an identity.
Definition: pib-memory.cpp:63
virtual std::set< name::Component > getKeysOfIdentity(const Name &identity) const override
Get all the key ids of an identity with name identity.
Definition: pib-memory.cpp:141
virtual void addIdentity(const Name &identity) override
Add an identity.
Definition: pib-memory.cpp:52
virtual void setTpmLocator(const std::string &tpmLocator) override
Set the corresponding TPM information to tpmLocator.
Definition: pib-memory.cpp:34
static Name certificateNameToPublicKeyName(const Name &certificateName)
Get the public key name from the full certificate name.
virtual PublicKey getKeyBits(const Name &identity, const name::Component &keyId) const override
Get the key bits of a key.
Definition: pib-memory.cpp:131
PublicKey & getPublicKeyInfo()
represents a semantic error
Definition: pib.hpp:55
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:343
virtual Name getDefaultIdentity() const override
Get the default identity.
Definition: pib-memory.cpp:90
virtual std::set< Name > getIdentities() const override
Get the name of all the identities.
Definition: pib-memory.cpp:76
virtual name::Component getDefaultKeyOfIdentity(const Name &identity) const override
Get the id of the default key of an identity with name identity.
Definition: pib-memory.cpp:163
virtual std::set< Name > getCertificatesOfKey(const Name &identity, const name::Component &keyId) const override
Get a list of certificate names of a key with id keyId of identity.
Definition: pib-memory.cpp:218
virtual std::string getTpmLocator() const override
Get TPM Locator.
Definition: pib-memory.cpp:40
virtual void setDefaultCertificateOfKey(const Name &identity, const name::Component &keyId, const Name &certName) override
Set a cert with name certName as the default of a key with id of .
Definition: pib-memory.cpp:231
virtual bool hasCertificate(const Name &certName) const override
Check the existence of a certificate with name certName.
Definition: pib-memory.cpp:181
Name abstraction to represent an absolute name.
Definition: name.hpp:46
const Name & getPublicKeyName() const
virtual bool hasKey(const Name &identity, const name::Component &keyId) const override
Check the existence of a key.
Definition: pib-memory.cpp:99
virtual void removeKey(const Name &identity, const name::Component &keyId) override
Remove a key.
Definition: pib-memory.cpp:117
virtual void setDefaultIdentity(const Name &identityName) override
Set an identity with name identityName as the default identity.
Definition: pib-memory.cpp:82
Component holds a read-only name component value.
virtual IdentityCertificate getDefaultCertificateOfKey(const Name &identity, const name::Component &keyId) const override
Get the default certificate of a key with id of .
Definition: pib-memory.cpp:241
Name & append(const uint8_t *value, size_t valueLength)
Append a new component, copying from value of length valueLength.
Definition: name.hpp:148
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
Definition: name.hpp:249
virtual void removeCertificate(const Name &certName) override
Remove a certificate with name certName.
Definition: pib-memory.cpp:201
virtual void addCertificate(const IdentityCertificate &certificate) override
Add a certificate.
Definition: pib-memory.cpp:187
virtual bool hasIdentity(const Name &identity) const override
Check the existence of an identity.
Definition: pib-memory.cpp:46
virtual void setDefaultKeyOfIdentity(const Name &identity, const name::Component &keyId) override
Set an key with id keyId as the default key of an identity with name identity.
Definition: pib-memory.cpp:152
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:419