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-2021 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_CXX_SECURITY_KEY_CHAIN_HPP
23 #define NDN_CXX_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 
97 
98  const Pib&
99  getPib() const noexcept
100  {
101  return *m_pib;
102  }
103 
104  const Tpm&
105  getTpm() const noexcept
106  {
107  return *m_tpm;
108  }
109 
110  static const KeyParams&
112 
113 public: // Identity management
131  Identity
132  createIdentity(const Name& identityName, const KeyParams& params = getDefaultKeyParams());
133 
140  void
141  deleteIdentity(const Identity& identity);
142 
147  void
148  setDefaultIdentity(const Identity& identity);
149 
150 public: // Key management
163  Key
164  createKey(const Identity& identity, const KeyParams& params = getDefaultKeyParams());
165 
176  Name
178  const HmacKeyParams& params = HmacKeyParams());
179 
188  void
189  deleteKey(const Identity& identity, const Key& key);
190 
198  void
199  setDefaultKey(const Identity& identity, const Key& key);
200 
201 public: // Certificate management
214  void
215  addCertificate(const Key& key, const Certificate& certificate);
216 
225  void
226  deleteCertificate(const Key& key, const Name& certificateName);
227 
237  void
238  setDefaultCertificate(const Key& key, const Certificate& certificate);
239 
240 public: // signing
260  void
261  sign(Data& data, const SigningInfo& params = SigningInfo());
262 
289  void
290  sign(Interest& interest, const SigningInfo& params = SigningInfo());
291 
292 public: // export & import
302  shared_ptr<SafeBag>
303  exportSafeBag(const Certificate& certificate, const char* pw, size_t pwLen);
304 
320  void
321  importSafeBag(const SafeBag& safeBag, const char* pw, size_t pwLen);
322 
326  void
327  importPrivateKey(const Name& keyName, shared_ptr<transform::PrivateKey> key);
328 
329 public: // PIB & TPM backend registry
336  template<class PibBackendType>
337  static void
338  registerPibBackend(const std::string& scheme)
339  {
340  getPibFactories().emplace(scheme, [] (const std::string& locator) {
341  return shared_ptr<pib::PibImpl>(new PibBackendType(locator));
342  });
343  }
344 
351  template<class TpmBackendType>
352  static void
353  registerTpmBackend(const std::string& scheme)
354  {
355  getTpmFactories().emplace(scheme, [] (const std::string& locator) {
356  return unique_ptr<tpm::BackEnd>(new TpmBackendType(locator));
357  });
358  }
359 
360 private:
361  using PibFactories = std::map<std::string, std::function<shared_ptr<pib::PibImpl>(const std::string&)>>;
362  using TpmFactories = std::map<std::string, std::function<unique_ptr<tpm::BackEnd>(const std::string&)>>;
363 
364  static PibFactories&
365  getPibFactories();
366 
367  static TpmFactories&
368  getTpmFactories();
369 
370  static std::tuple<std::string/*type*/, std::string/*location*/>
371  parseAndCheckPibLocator(const std::string& pibLocator);
372 
373  static std::tuple<std::string/*type*/, std::string/*location*/>
374  parseAndCheckTpmLocator(const std::string& tpmLocator);
375 
376  static const std::string&
377  getDefaultPibScheme();
378 
379  static const std::string&
380  getDefaultTpmScheme();
381 
385  static unique_ptr<Pib>
386  createPib(const std::string& pibLocator);
387 
391  static unique_ptr<Tpm>
392  createTpm(const std::string& tpmLocator);
393 
395  static const std::string&
396  getDefaultPibLocator();
397 
398  static const std::string&
399  getDefaultTpmLocator();
400 
405  getSignatureType(KeyType keyType, DigestAlgorithm digestAlgorithm);
406 
407 private: // signing
414  selfSign(Key& key);
415 
424  std::tuple<Name, SignatureInfo>
425  prepareSignatureInfo(const SigningInfo& params);
426 
432  sign(const InputBuffers& bufs, const Name& keyName, DigestAlgorithm digestAlgorithm) const;
433 
434 private:
435  unique_ptr<Pib> m_pib;
436  unique_ptr<Tpm> m_tpm;
437 
438  static std::string s_defaultPibLocator;
439  static std::string s_defaultTpmLocator;
440 };
441 
450 #define NDN_CXX_KEYCHAIN_REGISTER_PIB_BACKEND(PibType) \
451 static class NdnCxxAuto ## PibType ## PibRegistrationClass \
452 { \
453 public: \
454  NdnCxxAuto ## PibType ## PibRegistrationClass() \
455  { \
456  ::ndn::security::KeyChain::registerPibBackend<PibType>(PibType::getScheme()); \
457  } \
458 } ndnCxxAuto ## PibType ## PibRegistrationVariable
459 
468 #define NDN_CXX_KEYCHAIN_REGISTER_TPM_BACKEND(TpmType) \
469 static class NdnCxxAuto ## TpmType ## TpmRegistrationClass \
470 { \
471 public: \
472  NdnCxxAuto ## TpmType ## TpmRegistrationClass() \
473  { \
474  ::ndn::security::KeyChain::registerTpmBackend<TpmType>(TpmType::getScheme()); \
475  } \
476 } ndnCxxAuto ## TpmType ## TpmRegistrationVariable
477 
478 } // inline namespace v2
479 } // namespace security
480 
481 using security::KeyChain;
482 
483 } // namespace ndn
484 
485 #endif // NDN_CXX_SECURITY_KEY_CHAIN_HPP
Represents a Data packet.
Definition: data.hpp:38
Represents an Interest packet.
Definition: interest.hpp:50
Base class for key parameters.
Definition: key-params.hpp:36
Represents an absolute name.
Definition: name.hpp:46
SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size.
Definition: key-params.hpp:257
A secured container for sensitive information (certificate, private key)
Definition: safe-bag.hpp:39
Signing parameters passed to KeyChain.
static const Name & getHmacIdentity()
A localhost identity to indicate that the signature is generated using an HMAC key.
A frontend handle of an Identity.
Definition: identity.hpp:48
A frontend handle of a key instance.
Definition: key.hpp:50
represents the PIB
Definition: pib.hpp:53
TPM front-end class.
Definition: tpm.hpp:66
Represents an NDN certificate following the version 2.0 format.
Definition: certificate.hpp:61
Error indicating that the supplied SigningInfo is invalid.
Definition: key-chain.hpp:67
Error indicating that the supplied TPM locator does not match the locator stored in PIB.
Definition: key-chain.hpp:58
The interface of signing key management.
Definition: key-chain.hpp:46
void setDefaultIdentity(const Identity &identity)
Set identity as the default identity.
Definition: key-chain.cpp:250
void deleteKey(const Identity &identity, const Key &key)
Delete a key key of identity.
Definition: key-chain.cpp:281
void setDefaultCertificate(const Key &key, const Certificate &certificate)
Set cert as the default certificate of key.
Definition: key-chain.cpp:341
const Tpm & getTpm() const noexcept
Definition: key-chain.hpp:105
Identity createIdentity(const Name &identityName, const KeyParams &params=getDefaultKeyParams())
Create an identity identityName.
Definition: key-chain.cpp:212
Key createKey(const Identity &identity, const KeyParams &params=getDefaultKeyParams())
Create a new key for identity.
Definition: key-chain.cpp:258
void deleteIdentity(const Identity &identity)
delete identity.
Definition: key-chain.cpp:236
static const KeyParams & getDefaultKeyParams()
Definition: key-chain.cpp:147
void sign(Data &data, const SigningInfo &params=SigningInfo())
Sign a Data packet according to the supplied signing information.
Definition: key-chain.cpp:442
void deleteCertificate(const Key &key, const Name &certificateName)
delete a certificate with name certificateName of key.
Definition: key-chain.cpp:329
void setDefaultKey(const Identity &identity, const Key &key)
Set key as the default key of identity.
Definition: key-chain.cpp:297
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:367
const Pib & getPib() const noexcept
Definition: key-chain.hpp:99
Name createHmacKey(const Name &prefix=SigningInfo::getHmacIdentity(), const HmacKeyParams &params=HmacKeyParams())
Create a new HMAC key.
Definition: key-chain.cpp:275
void importPrivateKey(const Name &keyName, shared_ptr< transform::PrivateKey > key)
Import a private key into the TPM.
Definition: key-chain.cpp:425
void addCertificate(const Key &key, const Certificate &certificate)
Add a certificate certificate for key.
Definition: key-chain.cpp:310
KeyChain()
Constructor to create KeyChain with default PIB and TPM.
Definition: key-chain.cpp:155
static void registerTpmBackend(const std::string &scheme)
Register a new TPM backend.
Definition: key-chain.hpp:353
static void registerPibBackend(const std::string &scheme)
Register a new PIB backend.
Definition: key-chain.hpp:338
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:350
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:48
SignatureTypeValue
SignatureType values.
Definition: tlv.hpp:132
Definition: data.cpp:25
SimpleSymmetricKeyParams< detail::HmacKeyParamsInfo > HmacKeyParams
HmacKeyParams carries parameters for HMAC key.
Definition: key-params.hpp:309
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139
KeyType
The type of a cryptographic key.
InputBuffers bufs