NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
key-chain.hpp
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 
22 #ifndef NDN_SECURITY_V2_KEY_CHAIN_HPP
23 #define NDN_SECURITY_V2_KEY_CHAIN_HPP
24 
25 #include "ndn-cxx/interest.hpp"
33 
34 namespace ndn {
35 namespace security {
36 namespace v2 {
37 
46 class KeyChain : noncopyable
47 {
48 public:
49  class Error : public std::runtime_error
50  {
51  public:
52  using std::runtime_error::runtime_error;
53  };
54 
58  class LocatorMismatchError : public Error
59  {
60  public:
61  using Error::Error;
62  };
63 
68  {
69  public:
70  using Error::Error;
71  };
72 
83  KeyChain();
84 
95  KeyChain(const std::string& pibLocator, const std::string& tpmLocator, bool allowReset = false);
96 
98 
99  const Pib&
100  getPib() const
101  {
102  return *m_pib;
103  }
104 
105  const Tpm&
106  getTpm() const
107  {
108  return *m_tpm;
109  }
110 
111 public: // Identity management
129  Identity
130  createIdentity(const Name& identityName, const KeyParams& params = getDefaultKeyParams());
131 
138  void
139  deleteIdentity(const Identity& identity);
140 
145  void
146  setDefaultIdentity(const Identity& identity);
147 
148 public: // Key management
161  Key
162  createKey(const Identity& identity, const KeyParams& params = getDefaultKeyParams());
163 
174  Name
176  const HmacKeyParams& params = HmacKeyParams());
177 
186  void
187  deleteKey(const Identity& identity, const Key& key);
188 
196  void
197  setDefaultKey(const Identity& identity, const Key& key);
198 
199 public: // Certificate management
212  void
213  addCertificate(const Key& key, const Certificate& certificate);
214 
223  void
224  deleteCertificate(const Key& key, const Name& certificateName);
225 
235  void
236  setDefaultCertificate(const Key& key, const Certificate& certificate);
237 
238 public: // signing
261  void
262  sign(Data& data, const SigningInfo& params = getDefaultSigningInfo());
263 
287  void
288  sign(Interest& interest, const SigningInfo& params = getDefaultSigningInfo());
289 
303  Block
304  sign(const uint8_t* buffer, size_t bufferLength, const SigningInfo& params = getDefaultSigningInfo());
305 
306 public: // export & import
316  shared_ptr<SafeBag>
317  exportSafeBag(const Certificate& certificate, const char* pw, size_t pwLen);
318 
334  void
335  importSafeBag(const SafeBag& safeBag, const char* pw, size_t pwLen);
336 
340  void
341  importPrivateKey(const Name& keyName, shared_ptr<transform::PrivateKey> key);
342 
348  getSignatureType(KeyType keyType, DigestAlgorithm digestAlgorithm);
349 
350 public: // PIB & TPM backend registry
357  template<class PibBackendType>
358  static void
359  registerPibBackend(const std::string& scheme);
360 
367  template<class TpmBackendType>
368  static void
369  registerTpmBackend(const std::string& scheme);
370 
371 private:
372  typedef std::map<std::string, function<unique_ptr<pib::PibImpl>(const std::string& location)>> PibFactories;
373  typedef std::map<std::string, function<unique_ptr<tpm::BackEnd>(const std::string& location)>> TpmFactories;
374 
375  static PibFactories&
376  getPibFactories();
377 
378  static TpmFactories&
379  getTpmFactories();
380 
381  static std::tuple<std::string/*type*/, std::string/*location*/>
382  parseAndCheckPibLocator(const std::string& pibLocator);
383 
384  static std::tuple<std::string/*type*/, std::string/*location*/>
385  parseAndCheckTpmLocator(const std::string& tpmLocator);
386 
387  static const std::string&
388  getDefaultPibScheme();
389 
390  static const std::string&
391  getDefaultTpmScheme();
392 
396  static unique_ptr<Pib>
397  createPib(const std::string& pibLocator);
398 
402  static unique_ptr<Tpm>
403  createTpm(const std::string& tpmLocator);
404 
406  static const std::string&
407  getDefaultPibLocator();
408 
409  static const std::string&
410  getDefaultTpmLocator();
411 
412 private: // signing
419  selfSign(Key& key);
420 
429  std::tuple<Name, SignatureInfo>
430  prepareSignatureInfo(const SigningInfo& params);
431 
436  Block
437  sign(const uint8_t* buf, size_t size, const Name& keyName, DigestAlgorithm digestAlgorithm) const;
438 
439 public:
440  static const SigningInfo&
442 
443  static const KeyParams&
445 
446 private:
447  std::unique_ptr<Pib> m_pib;
448  std::unique_ptr<Tpm> m_tpm;
449 
450  static std::string s_defaultPibLocator;
451  static std::string s_defaultTpmLocator;
452 };
453 
454 template<class PibType>
455 inline void
456 KeyChain::registerPibBackend(const std::string& scheme)
457 {
458  getPibFactories().emplace(scheme, [] (const std::string& locator) {
459  return unique_ptr<pib::PibImpl>(new PibType(locator));
460  });
461 }
462 
463 template<class TpmType>
464 inline void
465 KeyChain::registerTpmBackend(const std::string& scheme)
466 {
467  getTpmFactories().emplace(scheme, [] (const std::string& locator) {
468  return unique_ptr<tpm::BackEnd>(new TpmType(locator));
469  });
470 }
471 
480 #define NDN_CXX_V2_KEYCHAIN_REGISTER_PIB_BACKEND(PibType) \
481 static class NdnCxxAuto ## PibType ## PibRegistrationClass \
482 { \
483 public: \
484  NdnCxxAuto ## PibType ## PibRegistrationClass() \
485  { \
486  ::ndn::security::v2::KeyChain::registerPibBackend<PibType>(PibType::getScheme()); \
487  } \
488 } ndnCxxAuto ## PibType ## PibRegistrationVariable
489 
498 #define NDN_CXX_V2_KEYCHAIN_REGISTER_TPM_BACKEND(TpmType) \
499 static class NdnCxxAuto ## TpmType ## TpmRegistrationClass \
500 { \
501 public: \
502  NdnCxxAuto ## TpmType ## TpmRegistrationClass() \
503  { \
504  ::ndn::security::v2::KeyChain::registerTpmBackend<TpmType>(TpmType::getScheme()); \
505  } \
506 } ndnCxxAuto ## TpmType ## TpmRegistrationVariable
507 
508 } // namespace v2
509 
510 using v2::KeyChain;
511 
512 } // namespace security
513 
515 
516 } // namespace ndn
517 
518 #endif // NDN_SECURITY_V2_KEY_CHAIN_HPP
buf
const uint8_t * buf
Definition: verification-helpers.cpp:47
ndn::KeyType
KeyType
The type of a cryptographic key.
Definition: security-common.hpp:85
ndn::security::v2::KeyChain::Error
Definition: key-chain.hpp:50
ndn::KeyParams
Base class for key parameters.
Definition: key-params.hpp:36
ndn::security::SafeBag
a secured container for sensitive information(certificate, private key)
Definition: safe-bag.hpp:38
ndn::security::v2::KeyChain::deleteIdentity
void deleteIdentity(const Identity &identity)
delete identity.
Definition: key-chain.cpp:245
ndn::security::v2::KeyChain::~KeyChain
~KeyChain()
certificate.hpp
ndn::security::v2::KeyChain::createKey
Key createKey(const Identity &identity, const KeyParams &params=getDefaultKeyParams())
Create a new key for identity.
Definition: key-chain.cpp:267
security-common.hpp
ndn::security::v2::KeyChain::deleteCertificate
void deleteCertificate(const Key &key, const Name &certificateName)
delete a certificate with name certificateName of key.
Definition: key-chain.cpp:334
ndn::security::v2::KeyChain::deleteKey
void deleteKey(const Identity &identity, const Key &key)
Delete a key key of identity.
Definition: key-chain.cpp:291
ndn::security::v2::KeyChain::setDefaultKey
void setDefaultKey(const Identity &identity, const Key &key)
Set key as the default key of identity.
Definition: key-chain.cpp:307
ndn::security::v2::KeyChain::digestAlgorithm
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE DigestAlgorithm digestAlgorithm
Definition: key-chain.hpp:348
ndn::security::v2::KeyChain::createIdentity
Identity createIdentity(const Name &identityName, const KeyParams &params=getDefaultKeyParams())
Create an identity identityName.
Definition: key-chain.cpp:221
ndn::security::pib::Pib
represents the PIB
Definition: pib.hpp:53
ndn::security::SigningInfo
Signing parameters passed to KeyChain.
Definition: signing-info.hpp:42
ndn::security::SigningInfo::getHmacIdentity
static const Name & getHmacIdentity()
A localhost identity to indicate that the signature is generated using an HMAC key.
Definition: signing-info.cpp:55
ndn::security::v2::KeyChain::getTpm
const Tpm & getTpm() const
Definition: key-chain.hpp:106
ndn::security::v2::KeyChain::importPrivateKey
void importPrivateKey(const Name &keyName, shared_ptr< transform::PrivateKey > key)
Import a private key into the TPM.
Definition: key-chain.cpp:433
ndn::security::v2::KeyChain::setDefaultCertificate
void setDefaultCertificate(const Key &key, const Certificate &certificate)
Set cert as the default certificate of key.
Definition: key-chain.cpp:346
ndn::security::v2::KeyChain::getDefaultSigningInfo
static const SigningInfo & getDefaultSigningInfo()
Definition: key-chain.cpp:149
ndn::security::v2::KeyChain::setDefaultIdentity
void setDefaultIdentity(const Identity &identity)
Set identity as the default identity.
Definition: key-chain.cpp:259
ndn::DigestAlgorithm
DigestAlgorithm
Definition: security-common.hpp:96
ndn::security::v2::KeyChain::registerTpmBackend
static void registerTpmBackend(const std::string &scheme)
Register a new TPM backend.
Definition: key-chain.hpp:465
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::security::v2::Certificate
The certificate following the certificate format naming convention.
Definition: certificate.hpp:82
ndn::security::v2::KeyChain::importSafeBag
void importSafeBag(const SafeBag &safeBag, const char *pw, size_t pwLen)
Import a certificate and its corresponding private key from a SafeBag.
Definition: key-chain.cpp:372
ndn::security::v2::KeyChain::createHmacKey
Name createHmacKey(const Name &prefix=SigningInfo::getHmacIdentity(), const HmacKeyParams &params=HmacKeyParams())
Create a new HMAC key.
Definition: key-chain.cpp:285
ndn::security::v2::KeyChain
The interface of signing key management.
Definition: key-chain.hpp:47
ndn::security::v2::KeyChain::getDefaultKeyParams
static const KeyParams & getDefaultKeyParams()
Definition: key-chain.cpp:156
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:48
ndn::tlv::SignatureTypeValue
SignatureTypeValue
SignatureType values.
Definition: tlv.hpp:129
ndn::HmacKeyParams
SimpleSymmetricKeyParams< detail::HmacKeyParamsInfo > HmacKeyParams
HmacKeyParams carries parameters for HMAC key.
Definition: key-params.hpp:309
ndn::security::v2::KeyChain::getPib
const Pib & getPib() const
Definition: key-chain.hpp:100
ndn::Interest
Represents an Interest packet.
Definition: interest.hpp:44
ndn::security::tpm::Tpm
TPM front-end class.
Definition: tpm.hpp:66
tpm.hpp
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
interest.hpp
ndn::security::v2::KeyChain::exportSafeBag
shared_ptr< SafeBag > exportSafeBag(const Certificate &certificate, const char *pw, size_t pwLen)
Export a certificate and its corresponding private key.
Definition: key-chain.cpp:355
pib.hpp
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::security::pib::Key
A frontend handle of a key instance.
Definition: key.hpp:50
ndn::security::v2::KeyChain
ndn security v2 KeyChain
Definition: key-chain.cpp:68
ndn::security::pib::Identity
A frontend handle of an Identity.
Definition: identity.hpp:43
ndn::SimpleSymmetricKeyParams
SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size.
Definition: key-params.hpp:257
key-params.hpp
ndn::security::v2::KeyChain::KeyChain
KeyChain()
Constructor to create KeyChain with default PIB and TPM.
Definition: key-chain.cpp:164
ndn::security::v2::KeyChain::InvalidSigningInfoError
Error indicating that the supplied SigningInfo is invalid.
Definition: key-chain.hpp:68
ndn::security::v2::KeyChain::addCertificate
void addCertificate(const Key &key, const Certificate &certificate)
Add a certificate certificate for key.
Definition: key-chain.cpp:320
ndn::security::v2::KeyChain::sign
void sign(Data &data, const SigningInfo &params=getDefaultSigningInfo())
Sign data according to the supplied signing information.
Definition: key-chain.cpp:450
ndn::security::v2::KeyChain::LocatorMismatchError
Error indicating that the supplied TPM locator does not match the locator stored in PIB.
Definition: key-chain.hpp:59
signing-info.hpp
safe-bag.hpp
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::security::v2::KeyChain::registerPibBackend
static void registerPibBackend(const std::string &scheme)
Register a new PIB backend.
Definition: key-chain.hpp:456