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