All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sec-public-info.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
9 #ifndef NDN_SECURITY_SEC_PUBLIC_INFO_HPP
10 #define NDN_SECURITY_SEC_PUBLIC_INFO_HPP
11 
12 #include "../name.hpp"
13 #include "security-common.hpp"
14 #include "public-key.hpp"
15 #include "identity-certificate.hpp"
16 
17 
18 namespace ndn {
19 
27 {
28 public:
29  class Error : public std::runtime_error
30  {
31  public:
32  explicit
33  Error(const std::string& what)
34  : std::runtime_error(what)
35  {
36  }
37  };
38 
42  virtual
44 
51  virtual bool
52  doesIdentityExist(const Name& identityName) = 0;
53 
61  virtual void
62  addIdentity(const Name& identityName) = 0;
63 
69  virtual bool
70  revokeIdentity() = 0;
71 
78  virtual bool
79  doesPublicKeyExist(const Name& keyName) = 0;
80 
88  virtual void
89  addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) = 0;
90 
98  virtual shared_ptr<PublicKey>
99  getPublicKey(const Name& keyName) = 0;
100 
107  virtual bool
108  doesCertificateExist(const Name& certificateName) = 0;
109 
117  virtual void
118  addCertificate(const IdentityCertificate& certificate) = 0;
119 
127  virtual shared_ptr<IdentityCertificate>
128  getCertificate(const Name& certificateName) = 0;
129 
130 
131  /*****************************************
132  * Default Getter *
133  *****************************************/
134 
141  virtual Name
142  getDefaultIdentity() = 0;
143 
151  virtual Name
152  getDefaultKeyNameForIdentity(const Name& identityName) = 0;
153 
161  virtual Name
162  getDefaultCertificateNameForKey(const Name& keyName) = 0;
163 
170  virtual void
171  getAllIdentities(std::vector<Name>& nameList, bool isDefault) = 0;
172 
179  virtual void
180  getAllKeyNames(std::vector<Name>& nameList, bool isDefault) = 0;
181 
189  virtual void
190  getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) = 0;
191 
198  virtual void
199  getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) = 0;
200 
208  virtual void
209  getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name>& nameList, bool isDefault) = 0;
210 
211 protected:
212 
213  /*****************************************
214  * Default Setter *
215  *****************************************/
216 
222  virtual void
223  setDefaultIdentityInternal(const Name& identityName) = 0;
224 
231  virtual void
232  setDefaultKeyNameForIdentityInternal(const Name& keyName) = 0;
233 
240  virtual void
241  setDefaultCertificateNameForKeyInternal(const Name& certificateName) = 0;
242 
243  /*****************************************
244  * Delete Methods *
245  *****************************************/
246 
252  virtual void
253  deleteCertificateInfo(const Name& certificateName) = 0;
254 
260  virtual void
261  deletePublicKeyInfo(const Name& keyName) = 0;
262 
268  virtual void
269  deleteIdentityInfo(const Name& identity) = 0;
270 
271 public:
272 
273  /*****************************************
274  * Helper Methods *
275  *****************************************/
276 
283  inline void
284  setDefaultIdentity(const Name& identityName);
285 
292  inline void
293  setDefaultKeyNameForIdentity(const Name& keyName);
294 
301  inline void
302  setDefaultCertificateNameForKey(const Name& certificateName);
303 
311  inline Name
312  getNewKeyName(const Name& identityName, bool useKsk);
313 
321  inline Name
322  getDefaultCertificateNameForIdentity(const Name& identityName);
323 
330  inline Name
332 
339  inline void
341 
349  inline void
351 
359  inline void
361 
367  inline shared_ptr<IdentityCertificate>
369 
373  inline void
375 
376 protected:
377  shared_ptr<IdentityCertificate> m_defaultCertificate;
378 };
379 
380 inline void
382 {
383  setDefaultIdentityInternal(identityName);
385 }
386 
387 inline void
389 {
392 }
393 
394 inline void
396 {
399 }
400 
401 inline Name
403 {
405 }
406 
407 inline Name
408 SecPublicInfo::getNewKeyName (const Name& identityName, bool useKsk)
409 {
410  std::ostringstream oss;
411 
412  if (useKsk)
413  oss << "ksk-";
414  else
415  oss << "dsk-";
416 
417  oss << time::toUnixTimestamp(time::system_clock::now()).count();
418 
419  Name keyName = Name(identityName).append(oss.str());
420 
421  if (doesPublicKeyExist(keyName))
422  throw Error("Key name already exists: " + keyName.toUri());
423 
424  return keyName;
425 }
426 
427 inline Name
429 {
430  if (!static_cast<bool>(m_defaultCertificate))
432 
433  if (!static_cast<bool>(m_defaultCertificate))
434  throw Error("No default certificate is set");
435 
436  return m_defaultCertificate->getName();
437 }
438 
439 inline void
441 {
442  addCertificate(certificate);
445 }
446 
447 inline void
449 {
450  addCertificate(certificate);
451  Name certName = certificate.getName();
456 }
457 
458 inline void
460 {
461  addCertificate(certificate);
462  Name certName = certificate.getName();
468 }
469 
470 inline shared_ptr<IdentityCertificate>
472 {
473  return m_defaultCertificate;
474 }
475 
476 inline void
478 {
479  try
480  {
483  }
484  catch (SecPublicInfo::Error& e)
485  {
486  m_defaultCertificate.reset();
487  }
488 
489 }
490 
491 } // namespace ndn
492 
493 #endif //NDN_SECURITY_SEC_PUBLIC_INFO_HPP
Name getDefaultCertificateNameForIdentity(const Name &identityName)
Get the default certificate name for the specified identity.
virtual void addIdentity(const Name &identityName)=0
Add a new identity.
virtual void getAllKeyNames(std::vector< Name > &nameList, bool isDefault)=0
Get all the key name in public info.
virtual void addCertificate(const IdentityCertificate &certificate)=0
Add a certificate to the identity storage.
virtual Name getDefaultCertificateNameForKey(const Name &keyName)=0
Get the default certificate name for the specified key.
Name getPrefix(int nComponents) const
Return a new Name with the first nComponents components of this Name.
Definition: name.hpp:240
virtual ~SecPublicInfo()
The virtual Destructor.
void setDefaultKeyNameForIdentity(const Name &keyName)
Set the default key name for the corresponding identity.
static Name certificateNameToPublicKeyName(const Name &certificateName)
Get the public key name from the full certificate name.
virtual void deleteCertificateInfo(const Name &certificateName)=0
Delete a certificate.
virtual void getAllCertificateNames(std::vector< Name > &nameList, bool isDefault)=0
Get all the certificate name in public info.
virtual void setDefaultIdentityInternal(const Name &identityName)=0
Set the default identity.
virtual bool doesIdentityExist(const Name &identityName)=0
Check if the specified identity already exists.
void setDefaultCertificateNameForKey(const Name &certificateName)
Set the default certificate name for the corresponding key.
Name getNewKeyName(const Name &identityName, bool useKsk)
Generate a key name for the identity.
const Name & getName() const
Definition: data.hpp:346
void addCertificateAsIdentityDefault(const IdentityCertificate &certificate)
Add a certificate into the public key identity storage and set the certificate as the default one of ...
std::string toUri() const
Encode this name as a URI.
Definition: name.hpp:536
void addCertificateAsSystemDefault(const IdentityCertificate &certificate)
Add a certificate into the public key identity storage and set the certificate as the default one of ...
virtual void getAllKeyNamesOfIdentity(const Name &identity, std::vector< Name > &nameList, bool isDefault)=0
Get all the key name of a particular identity.
virtual bool doesPublicKeyExist(const Name &keyName)=0
Check if the specified key already exists.
virtual void setDefaultKeyNameForIdentityInternal(const Name &keyName)=0
Set the default key name for the corresponding identity.
virtual shared_ptr< IdentityCertificate > getCertificate(const Name &certificateName)=0
Get a certificate from the identity storage.
virtual shared_ptr< PublicKey > getPublicKey(const Name &keyName)=0
Get the public key DER blob from the identity storage.
virtual bool revokeIdentity()=0
Revoke the identity.
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:26
virtual void getAllIdentities(std::vector< Name > &nameList, bool isDefault)=0
Get all the identities in public info.
virtual void addPublicKey(const Name &keyName, KeyType keyType, const PublicKey &publicKeyDer)=0
Add a public key to the identity storage.
virtual bool doesCertificateExist(const Name &certificateName)=0
Check if the specified certificate already exists.
virtual void deletePublicKeyInfo(const Name &keyName)=0
Delete a public key and related certificates.
shared_ptr< IdentityCertificate > m_defaultCertificate
void refreshDefaultCertificate()
try to get the default certificate of the default identity from the public info.
void setDefaultIdentity(const Name &identityName)
Set the default identity.
milliseconds toUnixTimestamp(const system_clock::TimePoint &point)
Convert system_clock::TimePoint to UNIX timestamp.
Definition: time.hpp:107
virtual void getAllCertificateNamesOfKey(const Name &keyName, std::vector< Name > &nameList, bool isDefault)=0
Get all the certificate name of a particular key.
virtual void deleteIdentityInfo(const Name &identity)=0
Delete an identity and related public keys and certificates.
void addCertificateAsKeyDefault(const IdentityCertificate &certificate)
Add a certificate and set the certificate as the default one of its corresponding key...
shared_ptr< IdentityCertificate > defaultCertificate()
get cached default certificate of the default identity.
Name getDefaultCertificateName()
Get the default certificate name of the default identity.
virtual Name getDefaultKeyNameForIdentity(const Name &identityName)=0
Get the default key name for the specified identity.
virtual Name getDefaultIdentity()=0
Get the default identity.
Error(const std::string &what)
SecPublicInfo is a base class for the storage of public information.
virtual void setDefaultCertificateNameForKeyInternal(const Name &certificateName)=0
Set the default certificate name for the corresponding key.