2017-09-19 04:34:08 -04:00
|
|
|
|
2014-05-01 09:21:41 -04:00
|
|
|
// CryptoKey.h
|
2014-04-29 11:13:08 -04:00
|
|
|
|
2017-08-30 10:00:06 -04:00
|
|
|
// Declares the cCryptoKey class representing a RSA public key in mbedTLS
|
2014-04-29 11:13:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "CtrDrbgContext.h"
|
2017-08-30 10:00:06 -04:00
|
|
|
#include "mbedtls/pk.h"
|
2014-04-29 11:13:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-01 09:21:41 -04:00
|
|
|
class cCryptoKey
|
2014-04-29 11:13:08 -04:00
|
|
|
{
|
2017-08-30 10:00:06 -04:00
|
|
|
friend class cSslConfig;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-29 11:13:08 -04:00
|
|
|
public:
|
2014-04-30 18:28:27 -04:00
|
|
|
/** Constructs an empty key instance. Before use, it needs to be filled by ParsePublic() or ParsePrivate() */
|
2014-05-01 09:21:41 -04:00
|
|
|
cCryptoKey(void);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-30 18:28:27 -04:00
|
|
|
/** Constructs the public key out of the DER- or PEM-encoded pubkey data */
|
2014-05-01 09:21:41 -04:00
|
|
|
cCryptoKey(const AString & a_PublicKeyData);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-30 18:28:27 -04:00
|
|
|
/** Constructs the private key out of the DER- or PEM-encoded privkey data, with the specified password.
|
|
|
|
If a_Password is empty, no password is assumed. */
|
2014-05-01 09:21:41 -04:00
|
|
|
cCryptoKey(const AString & a_PrivateKeyData, const AString & a_Password);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-05-01 09:21:41 -04:00
|
|
|
~cCryptoKey();
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-29 11:13:08 -04:00
|
|
|
/** Decrypts the data using the stored public key
|
|
|
|
Both a_EncryptedData and a_DecryptedData must be at least <KeySizeBytes> bytes large.
|
|
|
|
Returns the number of bytes decrypted, or negative number for error. */
|
|
|
|
int Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLength, Byte * a_DecryptedData, size_t a_DecryptedMaxLength);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-29 11:13:08 -04:00
|
|
|
/** Encrypts the data using the stored public key
|
|
|
|
Both a_EncryptedData and a_DecryptedData must be at least <KeySizeBytes> bytes large.
|
|
|
|
Returns the number of bytes decrypted, or negative number for error. */
|
|
|
|
int Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte * a_EncryptedData, size_t a_EncryptedMaxLength);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-30 18:28:27 -04:00
|
|
|
/** Parses the specified data into a public key representation.
|
|
|
|
The key can be DER- or PEM-encoded.
|
2017-08-30 10:00:06 -04:00
|
|
|
Returns 0 on success, mbedTLS error code on failure. */
|
2014-04-30 18:28:27 -04:00
|
|
|
int ParsePublic(const void * a_Data, size_t a_NumBytes);
|
|
|
|
|
|
|
|
/** Parses the specified data into a private key representation.
|
|
|
|
If a_Password is empty, no password is assumed.
|
|
|
|
The key can be DER- or PEM-encoded.
|
2017-08-30 10:00:06 -04:00
|
|
|
Returns 0 on success, mbedTLS error code on failure. */
|
2014-04-30 18:28:27 -04:00
|
|
|
int ParsePrivate(const void * a_Data, size_t a_NumBytes, const AString & a_Password);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-30 18:28:27 -04:00
|
|
|
/** Returns true if the contained key is valid. */
|
|
|
|
bool IsValid(void) const;
|
2014-04-29 11:13:08 -04:00
|
|
|
|
|
|
|
protected:
|
2017-08-30 10:00:06 -04:00
|
|
|
/** The mbedTLS representation of the key data */
|
|
|
|
mbedtls_pk_context m_Pk;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-04-29 11:13:08 -04:00
|
|
|
/** The random generator used in encryption and decryption */
|
|
|
|
cCtrDrbgContext m_CtrDrbg;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
|
|
|
|
2017-08-30 10:00:06 -04:00
|
|
|
/** Returns the internal context ptr. Only use in mbedTLS API calls. */
|
|
|
|
mbedtls_pk_context * GetInternal(void) { return &m_Pk; }
|
2014-04-29 11:13:08 -04:00
|
|
|
} ;
|
|
|
|
|
2017-07-20 07:19:18 -04:00
|
|
|
typedef std::shared_ptr<cCryptoKey> cCryptoKeyPtr;
|
2014-04-29 11:13:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|