All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
tpm-private-key.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
23 #ifndef NDN_TPM_PRIVATE_KEY_HPP
24 #define NDN_TPM_PRIVATE_KEY_HPP
25 
26 #include "../../lite/security/ec-private-key-lite.hpp"
27 #include "../../lite/security/rsa-private-key-lite.hpp"
28 #include "../../util/blob.hpp"
29 #include "../key-params.hpp"
30 #include "../security-common.hpp"
31 #include "../../encoding/oid.hpp"
32 #include "../../c/encrypt/algo/encrypt-params-types.h"
33 
34 namespace ndn {
35 
36 class OID;
37 class DerNode;
38 
44 public:
49  class Error : public std::runtime_error
50  {
51  public:
52  Error(const std::string& what)
53  : std::runtime_error(what)
54  {
55  }
56  };
57 
63  : keyType_((KeyType)-1)
64  {}
65 
75  void
76  loadPkcs1
77  (const uint8_t* encoding, size_t encodingLength,
78  KeyType keyType = (KeyType)-1);
79 
87  void
88  loadPkcs8(const uint8_t* encoding, size_t encodingLength);
89 
96  Blob
97  derivePublicKey() const;
98 
110  Blob
111  decrypt
112  (const uint8_t* cipherText, size_t cipherTextLength,
113  ndn_EncryptAlgorithmType algorithmType = ndn_EncryptAlgorithmType_RsaOaep);
114 
125  Blob
126  sign(const uint8_t *data, size_t dataLength, DigestAlgorithm digestAlgorithm);
127 
136  Blob
137  toPkcs1(bool includeParameters = true);
138 
147  Blob
148  toPkcs8(bool includeParameters = true);
149 
158  static ptr_lib::shared_ptr<TpmPrivateKey>
159  generatePrivateKey(const KeyParams& keyParams);
160 
161 private:
170  static Blob
171  encodePkcs8PrivateKey
172  (const std::vector<uint8_t>& privateKeyDer, const OID& oid,
173  const ptr_lib::shared_ptr<DerNode>& parameters);
174 
183  static Blob
184  encodeSubjectPublicKeyInfo
185  (const OID& oid, const ptr_lib::shared_ptr<DerNode>& parameters,
186  const ptr_lib::shared_ptr<DerNode>& bitString);
187 
198  static void
199  decodeEcPrivateKey
200  (const ptr_lib::shared_ptr<DerNode>& algorithmParameters,
201  const Blob& privateKeyDer, EcPrivateKeyLite& privateKey);
202 
210  static OID
211  getEcOid(const EcPrivateKeyLite& ecPrivateKey);
212 
213  // Disable the copy constructor and assignment operator.
214  TpmPrivateKey(const TpmPrivateKey& other);
215  TpmPrivateKey& operator=(const TpmPrivateKey& other);
216 
217  KeyType keyType_;
218  ptr_lib::shared_ptr<EcPrivateKeyLite> ecPrivateKey_;
219  ptr_lib::shared_ptr<RsaPrivateKeyLite> rsaPrivateKey_;
220 };
221 
222 }
223 
224 #endif
A TpmPrivateKey holds an in-memory private key and provides cryptographic operations such as for sign...
Definition: tpm-private-key.hpp:43
void loadPkcs8(const uint8_t *encoding, size_t encodingLength)
Load the unencrypted private key from a buffer with the PKCS #8 encoding.
Definition: tpm-private-key.cpp:96
TpmPrivateKey()
Create an uninitialized TpmPrivateKey.
Definition: tpm-private-key.hpp:62
KeyType
Definition: security-common.hpp:50
Blob derivePublicKey() const
Get the encoded public key for this private key.
Definition: tpm-private-key.cpp:152
A Blob holds a pointer to an immutable byte array implemented as const std::vector<uint8_t>.
Definition: blob.hpp:42
An EcPrivateKeyLite holds a decoded or generated EC private key for use in crypto operations...
Definition: ec-private-key-lite.hpp:35
Blob sign(const uint8_t *data, size_t dataLength, DigestAlgorithm digestAlgorithm)
Sign the data with this private key, returning a signature Blob.
Definition: tpm-private-key.cpp:231
void loadPkcs1(const uint8_t *encoding, size_t encodingLength, KeyType keyType=(KeyType)-1)
Load the unencrypted private key from a buffer with the PKCS #1 encoding.
Definition: tpm-private-key.cpp:39
A TpmPrivateKey::Error extends runtime_error and represents an error in private key processing...
Definition: tpm-private-key.hpp:49
KeyParams is a base class for key parameters.
Definition: key-params.hpp:36
Blob toPkcs1(bool includeParameters=true)
Get the encoded unencrypted private key in PKCS #1.
Definition: tpm-private-key.cpp:262
static ptr_lib::shared_ptr< TpmPrivateKey > generatePrivateKey(const KeyParams &keyParams)
Generate a key pair according to keyParams and return a new TpmPrivateKey with the private key...
Definition: tpm-private-key.cpp:318
Definition: oid.hpp:31
Blob decrypt(const uint8_t *cipherText, size_t cipherTextLength, ndn_EncryptAlgorithmType algorithmType=ndn_EncryptAlgorithmType_RsaOaep)
Decrypt the cipherText using this private key according the encryption algorithmType.
Definition: tpm-private-key.cpp:201
Blob toPkcs8(bool includeParameters=true)
Get the encoded unencrypted private key in PKCS #8.
Definition: tpm-private-key.cpp:298