Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Native methods for EVP_PKEY to PKCS8PrivateKeyInfo
  • Loading branch information
bartonjs committed Jun 11, 2021
commit d9460127a71f39f46d9d148dbcd220c858b480c0
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ static const Entry s_cryptoNative[] =
DllImportEntry(CryptoNative_EncodeAsn1Integer)
DllImportEntry(CryptoNative_EncodeOcspRequest)
DllImportEntry(CryptoNative_EncodePkcs7)
DllImportEntry(CryptoNative_EncodePkcs8PrivateKey)
DllImportEntry(CryptoNative_EncodeX509)
DllImportEntry(CryptoNative_EncodeX509SubjectPublicKeyInfo)
DllImportEntry(CryptoNative_ErrClearError)
Expand Down Expand Up @@ -176,6 +177,7 @@ static const Entry s_cryptoNative[] =
DllImportEntry(CryptoNative_GetOcspRequestDerSize)
DllImportEntry(CryptoNative_GetPkcs7Certificates)
DllImportEntry(CryptoNative_GetPkcs7DerSize)
DllImportEntry(CryptoNative_GetPkcs8PrivateKeySize)
DllImportEntry(CryptoNative_GetRandomBytes)
DllImportEntry(CryptoNative_GetRsaParameters)
DllImportEntry(CryptoNative_GetX509CrlNextUpdate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ const EVP_CIPHER* EVP_chacha20_poly1305(void);
RENAMED_FUNCTION(EVP_MD_CTX_new, EVP_MD_CTX_create) \
REQUIRED_FUNCTION(EVP_MD_size) \
REQUIRED_FUNCTION(EVP_PKCS82PKEY) \
REQUIRED_FUNCTION(EVP_PKEY2PKCS8) \
REQUIRED_FUNCTION(EVP_PKEY_CTX_ctrl) \
REQUIRED_FUNCTION(EVP_PKEY_CTX_free) \
REQUIRED_FUNCTION(EVP_PKEY_CTX_get0_pkey) \
Expand Down Expand Up @@ -380,6 +381,7 @@ const EVP_CIPHER* EVP_chacha20_poly1305(void);
REQUIRED_FUNCTION(i2d_OCSP_REQUEST) \
REQUIRED_FUNCTION(i2d_OCSP_RESPONSE) \
REQUIRED_FUNCTION(i2d_PKCS7) \
REQUIRED_FUNCTION(i2d_PKCS8_PRIV_KEY_INFO) \
REQUIRED_FUNCTION(i2d_X509) \
REQUIRED_FUNCTION(i2d_X509_PUBKEY) \
REQUIRED_FUNCTION(OBJ_ln2nid) \
Expand Down Expand Up @@ -763,6 +765,7 @@ FOR_ALL_OPENSSL_FUNCTIONS
#define EVP_MD_CTX_new EVP_MD_CTX_new_ptr
#define EVP_MD_size EVP_MD_size_ptr
#define EVP_PKCS82PKEY EVP_PKCS82PKEY_ptr
#define EVP_PKEY2PKCS8 EVP_PKEY2PKCS8_ptr
#define EVP_PKEY_CTX_ctrl EVP_PKEY_CTX_ctrl_ptr
#define EVP_PKEY_CTX_free EVP_PKEY_CTX_free_ptr
#define EVP_PKEY_CTX_get0_pkey EVP_PKEY_CTX_get0_pkey_ptr
Expand Down Expand Up @@ -818,6 +821,7 @@ FOR_ALL_OPENSSL_FUNCTIONS
#define HMAC_Update HMAC_Update_ptr
#define i2d_ASN1_INTEGER i2d_ASN1_INTEGER_ptr
#define i2d_ASN1_TYPE i2d_ASN1_TYPE_ptr
#define i2d_PKCS8_PRIV_KEY_INFO i2d_PKCS8_PRIV_KEY_INFO_ptr
#define i2d_OCSP_REQUEST i2d_OCSP_REQUEST_ptr
#define i2d_OCSP_RESPONSE i2d_OCSP_RESPONSE_ptr
#define i2d_PKCS7 i2d_PKCS7_ptr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,36 @@ EVP_PKEY* CryptoNative_DecodePkcs8PrivateKey(const uint8_t* buf, int32_t len, in

return key;
}

int32_t CryptoNative_GetPkcs8PrivateKeySize(EVP_PKEY* pkey)
{
assert(pkey != NULL);

PKCS8_PRIV_KEY_INFO* p8 = EVP_PKEY2PKCS8(pkey);

if (p8 == NULL)
{
return -1;
}

int ret = i2d_PKCS8_PRIV_KEY_INFO(p8, NULL);
PKCS8_PRIV_KEY_INFO_free(p8);
return ret;
}

int32_t CryptoNative_EncodePkcs8PrivateKey(EVP_PKEY* pkey, uint8_t* buf)
{
assert(pkey != NULL);
assert(buf != NULL);

PKCS8_PRIV_KEY_INFO* p8 = EVP_PKEY2PKCS8(pkey);

if (p8 == NULL)
{
return -1;
}

int ret = i2d_PKCS8_PRIV_KEY_INFO(p8, &buf);
PKCS8_PRIV_KEY_INFO_free(p8);
return ret;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,29 @@ Returns the number (as of this call) of references to the EVP_PKEY. Anything les
PALEXPORT int32_t CryptoNative_UpRefEvpPkey(EVP_PKEY* pkey);

/*
* Decodes an X.509 SubjectPublicKeyInfo into an EVP_PKEY*, verifying the interpreted algorithm type.
*
* Requres a non-null buf, and len > 0.
Decodes an X.509 SubjectPublicKeyInfo into an EVP_PKEY*, verifying the interpreted algorithm type.

Requres a non-null buf, and len > 0.
*/
PALEXPORT EVP_PKEY* CryptoNative_DecodeSubjectPublicKeyInfo(const uint8_t* buf, int32_t len, int32_t algId);

/*
* Decodes an Pkcs8PrivateKeyInfo into an EVP_PKEY*, verifying the interpreted algorithm type.
*
* Requres a non-null buf, and len > 0.
Decodes an Pkcs8PrivateKeyInfo into an EVP_PKEY*, verifying the interpreted algorithm type.

Requres a non-null buf, and len > 0.
*/
PALEXPORT EVP_PKEY* CryptoNative_DecodePkcs8PrivateKey(const uint8_t* buf, int32_t len, int32_t algId);

/*
Reports the number of bytes rqeuired to encode an EVP_PKEY* as a Pkcs8PrivateKeyInfo, or a negative value on error.
*/
PALEXPORT int32_t CryptoNative_GetPkcs8PrivateKeySize(EVP_PKEY* pkey);

/*
Encodes the EVP_PKEY* as a Pkcs8PrivateKeyInfo, writing the encoded value to buf.

buf must be big enough, or an out of bounds write may occur.

Returns the number of bytes written.
*/
PALEXPORT int32_t CryptoNative_EncodePkcs8PrivateKey(EVP_PKEY* pkey, uint8_t* buf);