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-2020 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_KEY_CHAIN_HPP
23 #define NDN_SECURITY_KEY_CHAIN_HPP
24 
25 #include "ndn-cxx/interest.hpp"
32 
33 namespace ndn {
34 namespace security {
35 inline namespace v2 {
36 
45 class KeyChain : noncopyable
46 {
47 public:
48  class Error : public std::runtime_error
49  {
50  public:
51  using std::runtime_error::runtime_error;
52  };
53 
57  class LocatorMismatchError : public Error
58  {
59  public:
60  using Error::Error;
61  };
62 
67  {
68  public:
69  using Error::Error;
70  };
71 
82  KeyChain();
83 
94  KeyChain(const std::string& pibLocator, const std::string& tpmLocator, bool allowReset = false);
95 
96  ~KeyChain();
97 
98  const Pib&
99  getPib() const
100  {
101  return *m_pib;
102  }
103 
104  const Tpm&
105  getTpm() const
106  {
107  return *m_tpm;
108  }
109 
110 public: // Identity management
128  Identity
129  createIdentity(const Name& identityName, const KeyParams& params = getDefaultKeyParams());
130 
137  void
138  deleteIdentity(const Identity& identity);
139 
144  void
145  setDefaultIdentity(const Identity& identity);
146 
147 public: // Key management
160  Key
161  createKey(const Identity& identity, const KeyParams& params = getDefaultKeyParams());
162 
173  Name
175  const HmacKeyParams& params = HmacKeyParams());
176 
185  void
186  deleteKey(const Identity& identity, const Key& key);
187 
195  void
196  setDefaultKey(const Identity& identity, const Key& key);
197 
198 public: // Certificate management
211  void
212  addCertificate(const Key& key, const Certificate& certificate);
213 
222  void
223  deleteCertificate(const Key& key, const Name& certificateName);
224 
234  void
235  setDefaultCertificate(const Key& key, const Certificate& certificate);
236 
237 public: // signing
257  void
258  sign(Data& data, const SigningInfo& params = SigningInfo());
259 
287  void
288  sign(Interest& interest, const SigningInfo& params = SigningInfo());
289 
305  [[deprecated("sign Interests and Data directly")]]
306  Block
307  sign(const uint8_t* buffer, size_t bufferLength, const SigningInfo& params = SigningInfo());
308 
309 public: // export & import
319  shared_ptr<SafeBag>
320  exportSafeBag(const Certificate& certificate, const char* pw, size_t pwLen);
321 
337  void
338  importSafeBag(const SafeBag& safeBag, const char* pw, size_t pwLen);
339 
343  void
344  importPrivateKey(const Name& keyName, shared_ptr<transform::PrivateKey> key);
345 
351  getSignatureType(KeyType keyType, DigestAlgorithm digestAlgorithm);
352 
353 public: // PIB & TPM backend registry
360  template<class PibBackendType>
361  static void
362  registerPibBackend(const std::string& scheme);
363 
370  template<class TpmBackendType>
371  static void
372  registerTpmBackend(const std::string& scheme);
373 
374 private:
375  typedef std::map<std::string, function<std::shared_ptr<pib::PibImpl>(const std::string& location)>> PibFactories;
376  typedef std::map<std::string, function<unique_ptr<tpm::BackEnd>(const std::string& location)>> TpmFactories;
377 
378  static PibFactories&
379  getPibFactories();
380 
381  static TpmFactories&
382  getTpmFactories();
383 
384  static std::tuple<std::string/*type*/, std::string/*location*/>
385  parseAndCheckPibLocator(const std::string& pibLocator);
386 
387  static std::tuple<std::string/*type*/, std::string/*location*/>
388  parseAndCheckTpmLocator(const std::string& tpmLocator);
389 
390  static const std::string&
391  getDefaultPibScheme();
392 
393  static const std::string&
394  getDefaultTpmScheme();
395 
399  static unique_ptr<Pib>
400  createPib(const std::string& pibLocator);
401 
405  static unique_ptr<Tpm>
406  createTpm(const std::string& tpmLocator);
407 
409  static const std::string&
410  getDefaultPibLocator();
411 
412  static const std::string&
413  getDefaultTpmLocator();
414 
415 private: // signing
422  selfSign(Key& key);
423 
432  std::tuple<Name, SignatureInfo>
433  prepareSignatureInfo(const SigningInfo& params);
434 
440  sign(const InputBuffers& bufs, const Name& keyName, DigestAlgorithm digestAlgorithm) const;
441 
442 public:
446  [[deprecated("use default constructor for SigningInfo")]]
447  static const SigningInfo&
449 
450  static const KeyParams&
452 
453 private:
454  std::unique_ptr<Pib> m_pib;
455  std::unique_ptr<Tpm> m_tpm;
456 
457  static std::string s_defaultPibLocator;
458  static std::string s_defaultTpmLocator;
459 };
460 
461 template<class PibType>
462 inline void
463 KeyChain::registerPibBackend(const std::string& scheme)
464 {
465  getPibFactories().emplace(scheme, [] (const std::string& locator) {
466  return std::shared_ptr<pib::PibImpl>(new PibType(locator));
467  });
468 }
469 
470 template<class TpmType>
471 inline void
472 KeyChain::registerTpmBackend(const std::string& scheme)
473 {
474  getTpmFactories().emplace(scheme, [] (const std::string& locator) {
475  return unique_ptr<tpm::BackEnd>(new TpmType(locator));
476  });
477 }
478 
487 #define NDN_CXX_KEYCHAIN_REGISTER_PIB_BACKEND(PibType) \
488 static class NdnCxxAuto ## PibType ## PibRegistrationClass \
489 { \
490 public: \
491  NdnCxxAuto ## PibType ## PibRegistrationClass() \
492  { \
493  ::ndn::security::v2::KeyChain::registerPibBackend<PibType>(PibType::getScheme()); \
494  } \
495 } ndnCxxAuto ## PibType ## PibRegistrationVariable
496 
505 #define NDN_CXX_KEYCHAIN_REGISTER_TPM_BACKEND(TpmType) \
506 static class NdnCxxAuto ## TpmType ## TpmRegistrationClass \
507 { \
508 public: \
509  NdnCxxAuto ## TpmType ## TpmRegistrationClass() \
510  { \
511  ::ndn::security::v2::KeyChain::registerTpmBackend<TpmType>(TpmType::getScheme()); \
512  } \
513 } ndnCxxAuto ## TpmType ## TpmRegistrationVariable
514 
515 } // inline namespace v2
516 } // namespace security
517 
518 using security::v2::KeyChain;
519 
520 } // namespace ndn
521 
522 #endif // NDN_SECURITY_KEY_CHAIN_HPP
void deleteKey(const Identity &identity, const Key &key)
Delete a key key of identity.
Definition: key-chain.cpp:292
Definition: data.cpp:26
The certificate following the certificate format naming convention.
Definition: certificate.hpp:81
The interface of signing key management.
Definition: key-chain.hpp:45
SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size...
Definition: key-params.hpp:256
void addCertificate(const Key &key, const Certificate &certificate)
Add a certificate certificate for key.
Definition: key-chain.cpp:321
Key createKey(const Identity &identity, const KeyParams &params=getDefaultKeyParams())
Create a new key for identity.
Definition: key-chain.cpp:268
KeyChain()
Constructor to create KeyChain with default PIB and TPM.
Definition: key-chain.cpp:165
void sign(Data &data, const SigningInfo &params=SigningInfo())
Sign a Data packet according to the supplied signing information.
Definition: key-chain.cpp:456
Represents a TLV element of the NDN packet format.
Definition: block.hpp:42
Error indicating that the supplied TPM locator does not match the locator stored in PIB...
Definition: key-chain.hpp:57
Represents an Interest packet.
Definition: interest.hpp:50
Signing parameters passed to KeyChain.
void deleteCertificate(const Key &key, const Name &certificateName)
delete a certificate with name certificateName of key.
Definition: key-chain.cpp:340
TPM front-end class.
Definition: tpm.hpp:65
SimpleSymmetricKeyParams< detail::HmacKeyParamsInfo > HmacKeyParams
HmacKeyParams carries parameters for HMAC key.
Definition: key-params.hpp:309
const Pib & getPib() const
Definition: key-chain.hpp:99
Identity createIdentity(const Name &identityName, const KeyParams &params=getDefaultKeyParams())
Create an identity identityName.
Definition: key-chain.cpp:222
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:378
KeyType
The type of a cryptographic key.
const Tpm & getTpm() const
Definition: key-chain.hpp:105
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:361
A frontend handle of a key instance.
Definition: key.hpp:49
void setDefaultCertificate(const Key &key, const Certificate &certificate)
Set cert as the default certificate of key.
Definition: key-chain.cpp:352
void setDefaultIdentity(const Identity &identity)
Set identity as the default identity.
Definition: key-chain.cpp:260
static void registerPibBackend(const std::string &scheme)
Register a new PIB backend.
Definition: key-chain.hpp:463
static const SigningInfo & getDefaultSigningInfo()
Definition: key-chain.cpp:150
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:48
static void registerTpmBackend(const std::string &scheme)
Register a new TPM backend.
Definition: key-chain.hpp:472
Represents an absolute name.
Definition: name.hpp:44
Error indicating that the supplied SigningInfo is invalid.
Definition: key-chain.hpp:66
Name createHmacKey(const Name &prefix=SigningInfo::getHmacIdentity(), const HmacKeyParams &params=HmacKeyParams())
Create a new HMAC key.
Definition: key-chain.cpp:286
void importPrivateKey(const Name &keyName, shared_ptr< transform::PrivateKey > key)
Import a private key into the TPM.
Definition: key-chain.cpp:439
SignatureTypeValue
SignatureType values.
Definition: tlv.hpp:131
void deleteIdentity(const Identity &identity)
delete identity.
Definition: key-chain.cpp:246
static const KeyParams & getDefaultKeyParams()
Definition: key-chain.cpp:157
a secured container for sensitive information(certificate, private key)
Definition: safe-bag.hpp:37
void setDefaultKey(const Identity &identity, const Key &key)
Set key as the default key of identity.
Definition: key-chain.cpp:308
Base class for key parameters.
Definition: key-params.hpp:35
A frontend handle of an Identity.
Definition: identity.hpp:42
InputBuffers bufs
Represents a Data packet.
Definition: data.hpp:39
static const Name & getHmacIdentity()
A localhost identity to indicate that the signature is generated using an HMAC key.
represents the PIB
Definition: pib.hpp:52
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126