ndn::security::transform::PrivateKey Class Reference

Abstraction of a private key in crypto transformations. More...

#include <ndn-cxx/security/transform/private-key.hpp>

+ Inheritance diagram for ndn::security::transform::PrivateKey:
+ Collaboration diagram for ndn::security::transform::PrivateKey:

Classes

class  Error
 

Public Types

using PasswordCallback = std::function< int(char *buf, size_t bufSize, bool shouldConfirm)>
 Callback for application to handle password input. More...
 

Public Member Functions

 PrivateKey ()
 Creates an empty private key instance. More...
 
 ~PrivateKey ()
 
ConstBufferPtr decrypt (span< const uint8_t > cipherText) const
 
ConstBufferPtr derivePublicKey () const
 
ConstBufferPtr getKeyDigest (DigestAlgorithm algo) const
 Returns a digest of the private key. More...
 
size_t getKeySize () const
 Returns the size of the private key in bits. More...
 
KeyType getKeyType () const
 Returns the type of the private key. More...
 
void loadPkcs1 (span< const uint8_t > buf)
 Load the private key in PKCS#1 format from a buffer buf. More...
 
void loadPkcs1 (std::istream &is)
 Load the private key in PKCS#1 format from a stream is. More...
 
void loadPkcs1Base64 (span< const uint8_t > buf)
 Load the private key in base64-encoded PKCS#1 format from a buffer buf. More...
 
void loadPkcs1Base64 (std::istream &is)
 Load the private key in base64-encoded PKCS#1 format from a stream is. More...
 
void loadPkcs8 (span< const uint8_t > buf, const char *pw, size_t pwLen)
 Load the private key in encrypted PKCS#8 format from a buffer buf with passphrase pw. More...
 
void loadPkcs8 (span< const uint8_t > buf, PasswordCallback pwCallback=nullptr)
 Load the private key in encrypted PKCS#8 format from a buffer buf with passphrase obtained from pwCallback. More...
 
void loadPkcs8 (std::istream &is, const char *pw, size_t pwLen)
 Load the private key in encrypted PKCS#8 format from a stream is with passphrase pw. More...
 
void loadPkcs8 (std::istream &is, PasswordCallback pwCallback=nullptr)
 Load the private key in encrypted PKCS#8 format from a stream is with passphrase obtained from pwCallback. More...
 
void loadPkcs8Base64 (span< const uint8_t > buf, const char *pw, size_t pwLen)
 Load the private key in base64-encoded encrypted PKCS#8 format from a buffer buf with passphrase pw. More...
 
void loadPkcs8Base64 (span< const uint8_t > buf, PasswordCallback pwCallback=nullptr)
 Load the private key in encrypted PKCS#8 format from a buffer buf with passphrase obtained from pwCallback. More...
 
void loadPkcs8Base64 (std::istream &is, const char *pw, size_t pwLen)
 Load the private key in base64-encoded encrypted PKCS#8 format from a stream is with passphrase pw. More...
 
void loadPkcs8Base64 (std::istream &is, PasswordCallback pwCallback=nullptr)
 Load the private key in base64-encoded encrypted PKCS#8 format from a stream is with passphrase obtained from pwCallback. More...
 
void loadRaw (KeyType type, span< const uint8_t > buf)
 Load a raw private key from a buffer buf. More...
 
void savePkcs1 (std::ostream &os) const
 Save the private key in PKCS#1 format into a stream os. More...
 
void savePkcs1Base64 (std::ostream &os) const
 Save the private key in base64-encoded PKCS#1 format into a stream os. More...
 
void savePkcs8 (std::ostream &os, const char *pw, size_t pwLen) const
 Save the private key in encrypted PKCS#8 format into a stream os. More...
 
void savePkcs8 (std::ostream &os, PasswordCallback pwCallback=nullptr) const
 Save the private key in encrypted PKCS#8 format into a stream os with passphrase obtained from pwCallback. More...
 
void savePkcs8Base64 (std::ostream &os, const char *pw, size_t pwLen) const
 Save the private key in base64-encoded encrypted PKCS#8 format into a stream os. More...
 
void savePkcs8Base64 (std::ostream &os, PasswordCallback pwCallback=nullptr) const
 Save the private key in base64-encoded encrypted PKCS#8 format into a stream os with passphrase obtained from pwCallback. More...
 

Friends

unique_ptr< PrivateKeygeneratePrivateKey (const KeyParams &)
 Generate a private key according to keyParams. More...
 
class SignerFilter
 
class VerifierFilter
 

Detailed Description

Abstraction of a private key in crypto transformations.

Definition at line 38 of file private-key.hpp.

Member Typedef Documentation

◆ PasswordCallback

using ndn::security::transform::PrivateKey::PasswordCallback = std::function<int(char* buf, size_t bufSize, bool shouldConfirm)>

Callback for application to handle password input.

The password must be written to buf and must not be longer than bufSize chars. It is recommended to ask the user to verify the password if shouldConfirm is true, e.g., by prompting for it twice. The callback must return the number of characters in the password or 0 if an error occurred.

Definition at line 55 of file private-key.hpp.

Constructor & Destructor Documentation

◆ PrivateKey()

ndn::security::transform::PrivateKey::PrivateKey ( )

Creates an empty private key instance.

One must call loadXXXX(...) to load a private key.

Definition at line 71 of file private-key.cpp.

◆ ~PrivateKey()

ndn::security::transform::PrivateKey::~PrivateKey ( )
default

Member Function Documentation

◆ decrypt()

ConstBufferPtr ndn::security::transform::PrivateKey::decrypt ( span< const uint8_t >  cipherText) const
Returns
Plain text of cipherText decrypted using this private key.

Only RSA encryption is supported for now.

Definition at line 326 of file private-key.cpp.

◆ derivePublicKey()

ConstBufferPtr ndn::security::transform::PrivateKey::derivePublicKey ( ) const
Returns
Public key bits in PKCS#8 format

Definition at line 310 of file private-key.cpp.

◆ getKeyDigest()

ConstBufferPtr ndn::security::transform::PrivateKey::getKeyDigest ( DigestAlgorithm  algo) const

Returns a digest of the private key.

Note
Currently supports only HMAC keys.

Definition at line 114 of file private-key.cpp.

◆ getKeySize()

size_t ndn::security::transform::PrivateKey::getKeySize ( ) const

Returns the size of the private key in bits.

Definition at line 97 of file private-key.cpp.

◆ getKeyType()

KeyType ndn::security::transform::PrivateKey::getKeyType ( ) const

Returns the type of the private key.

Definition at line 79 of file private-key.cpp.

◆ loadPkcs1() [1/2]

void ndn::security::transform::PrivateKey::loadPkcs1 ( span< const uint8_t >  buf)

Load the private key in PKCS#1 format from a buffer buf.

Definition at line 152 of file private-key.cpp.

◆ loadPkcs1() [2/2]

void ndn::security::transform::PrivateKey::loadPkcs1 ( std::istream &  is)

Load the private key in PKCS#1 format from a stream is.

Definition at line 162 of file private-key.cpp.

◆ loadPkcs1Base64() [1/2]

void ndn::security::transform::PrivateKey::loadPkcs1Base64 ( span< const uint8_t >  buf)

Load the private key in base64-encoded PKCS#1 format from a buffer buf.

Definition at line 170 of file private-key.cpp.

◆ loadPkcs1Base64() [2/2]

void ndn::security::transform::PrivateKey::loadPkcs1Base64 ( std::istream &  is)

Load the private key in base64-encoded PKCS#1 format from a stream is.

Definition at line 178 of file private-key.cpp.

◆ loadPkcs8() [1/4]

void ndn::security::transform::PrivateKey::loadPkcs8 ( span< const uint8_t >  buf,
const char *  pw,
size_t  pwLen 
)

Load the private key in encrypted PKCS#8 format from a buffer buf with passphrase pw.

Precondition
strlen(pw) == pwLen

Definition at line 186 of file private-key.cpp.

◆ loadPkcs8() [2/4]

void ndn::security::transform::PrivateKey::loadPkcs8 ( span< const uint8_t >  buf,
PasswordCallback  pwCallback = nullptr 
)

Load the private key in encrypted PKCS#8 format from a buffer buf with passphrase obtained from pwCallback.

The default password callback is provided by OpenSSL

Definition at line 208 of file private-key.cpp.

◆ loadPkcs8() [3/4]

void ndn::security::transform::PrivateKey::loadPkcs8 ( std::istream &  is,
const char *  pw,
size_t  pwLen 
)

Load the private key in encrypted PKCS#8 format from a stream is with passphrase pw.

Precondition
strlen(pw) == pwLen

Definition at line 226 of file private-key.cpp.

◆ loadPkcs8() [4/4]

void ndn::security::transform::PrivateKey::loadPkcs8 ( std::istream &  is,
PasswordCallback  pwCallback = nullptr 
)

Load the private key in encrypted PKCS#8 format from a stream is with passphrase obtained from pwCallback.

The default password callback is provided by OpenSSL

Definition at line 234 of file private-key.cpp.

◆ loadPkcs8Base64() [1/4]

void ndn::security::transform::PrivateKey::loadPkcs8Base64 ( span< const uint8_t >  buf,
const char *  pw,
size_t  pwLen 
)

Load the private key in base64-encoded encrypted PKCS#8 format from a buffer buf with passphrase pw.

Precondition
strlen(pw) == pwLen

Definition at line 242 of file private-key.cpp.

◆ loadPkcs8Base64() [2/4]

void ndn::security::transform::PrivateKey::loadPkcs8Base64 ( span< const uint8_t >  buf,
PasswordCallback  pwCallback = nullptr 
)

Load the private key in encrypted PKCS#8 format from a buffer buf with passphrase obtained from pwCallback.

The default password callback is provided by OpenSSL

Definition at line 250 of file private-key.cpp.

◆ loadPkcs8Base64() [3/4]

void ndn::security::transform::PrivateKey::loadPkcs8Base64 ( std::istream &  is,
const char *  pw,
size_t  pwLen 
)

Load the private key in base64-encoded encrypted PKCS#8 format from a stream is with passphrase pw.

Precondition
strlen(pw) == pwLen

Definition at line 258 of file private-key.cpp.

◆ loadPkcs8Base64() [4/4]

void ndn::security::transform::PrivateKey::loadPkcs8Base64 ( std::istream &  is,
PasswordCallback  pwCallback = nullptr 
)

Load the private key in base64-encoded encrypted PKCS#8 format from a stream is with passphrase obtained from pwCallback.

The default password callback is provided by OpenSSL

Definition at line 266 of file private-key.cpp.

◆ loadRaw()

void ndn::security::transform::PrivateKey::loadRaw ( KeyType  type,
span< const uint8_t >  buf 
)

Load a raw private key from a buffer buf.

Note
Currently supports only HMAC keys.

Definition at line 133 of file private-key.cpp.

◆ savePkcs1()

void ndn::security::transform::PrivateKey::savePkcs1 ( std::ostream &  os) const

Save the private key in PKCS#1 format into a stream os.

Definition at line 274 of file private-key.cpp.

◆ savePkcs1Base64()

void ndn::security::transform::PrivateKey::savePkcs1Base64 ( std::ostream &  os) const

Save the private key in base64-encoded PKCS#1 format into a stream os.

Definition at line 280 of file private-key.cpp.

◆ savePkcs8() [1/2]

void ndn::security::transform::PrivateKey::savePkcs8 ( std::ostream &  os,
const char *  pw,
size_t  pwLen 
) const

Save the private key in encrypted PKCS#8 format into a stream os.

Definition at line 286 of file private-key.cpp.

◆ savePkcs8() [2/2]

void ndn::security::transform::PrivateKey::savePkcs8 ( std::ostream &  os,
PasswordCallback  pwCallback = nullptr 
) const

Save the private key in encrypted PKCS#8 format into a stream os with passphrase obtained from pwCallback.

The default password callback is provided by OpenSSL

Definition at line 292 of file private-key.cpp.

◆ savePkcs8Base64() [1/2]

void ndn::security::transform::PrivateKey::savePkcs8Base64 ( std::ostream &  os,
const char *  pw,
size_t  pwLen 
) const

Save the private key in base64-encoded encrypted PKCS#8 format into a stream os.

Definition at line 298 of file private-key.cpp.

◆ savePkcs8Base64() [2/2]

void ndn::security::transform::PrivateKey::savePkcs8Base64 ( std::ostream &  os,
PasswordCallback  pwCallback = nullptr 
) const

Save the private key in base64-encoded encrypted PKCS#8 format into a stream os with passphrase obtained from pwCallback.

The default password callback is provided by OpenSSL

Definition at line 304 of file private-key.cpp.

Friends And Related Function Documentation

◆ generatePrivateKey

unique_ptr<PrivateKey> generatePrivateKey ( const KeyParams )
friend

Generate a private key according to keyParams.

Note
The corresponding public key can be derived from the private key.
Exceptions
std::invalid_argumentthe specified key type is not supported
PrivateKey::Errorkey generation failed

Definition at line 524 of file private-key.cpp.

◆ SignerFilter

friend class SignerFilter
friend

Definition at line 242 of file private-key.hpp.

◆ VerifierFilter

friend class VerifierFilter
friend

Definition at line 243 of file private-key.hpp.