From ea8dd31de0a52c8afd05d4562d8e89c9047076d1 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Mon, 30 Mar 2020 14:07:47 +0200 Subject: [PATCH] Implement wolfSSL_i2d_PUBKEY and refactor wolfSSL_i2d_PrivateKey --- src/ssl.c | 78 +++++++++++++++++++++++-------------------- tests/api.c | 9 +++-- wolfssl/openssl/ssl.h | 2 +- wolfssl/ssl.h | 3 +- 4 files changed, 50 insertions(+), 42 deletions(-) diff --git a/src/ssl.c b/src/ssl.c index f6e3a0bee..0c08a8aef 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -7615,6 +7615,39 @@ WOLFSSL_EVP_PKEY* wolfSSL_d2i_PUBKEY(WOLFSSL_EVP_PKEY** out, return pkey; } +/* helper function to get raw pointer to DER buffer from WOLFSSL_EVP_PKEY */ +static int wolfSSL_EVP_PKEY_get_der(const WOLFSSL_EVP_PKEY* key, unsigned char** der) +{ + unsigned char* pt; + int sz = key->pkey_sz; + + if (!key || !key->pkey_sz) + return WOLFSSL_FATAL_ERROR; + + if (der) { + pt = (unsigned char*)key->pkey.ptr; + if (*der) { + /* since this function signature has no size value passed in it is + * assumed that the user has allocated a large enough buffer */ + XMEMCPY(*der, pt, sz); + *der += sz; + } + else { + *der = (unsigned char*)XMALLOC(sz, NULL, DYNAMIC_TYPE_OPENSSL); + if (*der == NULL) { + return WOLFSSL_FATAL_ERROR; + } + XMEMCPY(*der, pt, sz); + } + } + return sz; +} + +int wolfSSL_i2d_PUBKEY(const WOLFSSL_EVP_PKEY *key, unsigned char **der) +{ + return wolfSSL_EVP_PKEY_get_der(key, der); +} + /* Reads in a DER format key. If PKCS8 headers are found they are stripped off. * @@ -22174,39 +22207,15 @@ int wolfSSL_i2d_PKCS12_bio(WOLFSSL_BIO *bio, WC_PKCS12 *pkcs12) return ret; } -/* helper function to get raw pointer to DER buffer from WOLFSSL_EVP_PKEY */ -static int wolfSSL_EVP_PKEY_get_der(WOLFSSL_EVP_PKEY* key, unsigned char** der) -{ - if (!key) - return WOLFSSL_FAILURE; - if (der) - *der = (unsigned char*)key->pkey.ptr; - return key->pkey_sz; -} - /* Copies unencrypted DER key buffer into "der". If "der" is null then the size - * of buffer needed is returned + * of buffer needed is returned. If *der == NULL then it allocates a buffer. * NOTE: This also advances the "der" pointer to be at the end of buffer. * * Returns size of key buffer on success */ -int wolfSSL_i2d_PrivateKey(WOLFSSL_EVP_PKEY* key, unsigned char** der) +int wolfSSL_i2d_PrivateKey(const WOLFSSL_EVP_PKEY* key, unsigned char** der) { - if (key == NULL) { - return WOLFSSL_FATAL_ERROR; - } - - if (key->pkey_sz <= 0 || !key->pkey.ptr) { - return WOLFSSL_FATAL_ERROR; - } - - if (der != NULL) { - /* since this function signature has no size value passed in it is - * assumed that the user has allocated a large enough buffer */ - XMEMCPY(*der, key->pkey.ptr, key->pkey_sz); - *der += key->pkey_sz; - } - return key->pkey_sz; + return wolfSSL_EVP_PKEY_get_der(key, der); } /* Creates a new WC_PKCS12 structure @@ -22232,13 +22241,11 @@ WC_PKCS12* wolfSSL_PKCS12_create(char* pass, char* name, WC_PKCS12* pkcs12; WC_DerCertList* list = NULL; word32 passSz; - byte* keyDer; + byte* keyDer = NULL; word32 keyDerSz; byte* certDer; int certDerSz; - int ret; - WOLFSSL_ENTER("wolfSSL_PKCS12_create()"); if (pass == NULL || pkey == NULL || cert == NULL) { @@ -22247,11 +22254,8 @@ WC_PKCS12* wolfSSL_PKCS12_create(char* pass, char* name, } passSz = (word32)XSTRLEN(pass); - if ((ret = wolfSSL_EVP_PKEY_get_der(pkey, &keyDer)) < 0) { - WOLFSSL_LEAVE("wolfSSL_PKCS12_create", ret); - return NULL; - } - keyDerSz = ret; + keyDer = (byte*)pkey->pkey.ptr; + keyDerSz = pkey->pkey_sz; certDer = (byte*)wolfSSL_X509_get_der(cert, &certDerSz); if (certDer == NULL) { @@ -25314,9 +25318,9 @@ int wolfSSL_X509_PUBKEY_get0_param(WOLFSSL_ASN1_OBJECT **ppkalg, if (ppkalg) *ppkalg = pub->algor->algorithm; if (pk) - wolfSSL_EVP_PKEY_get_der(pub->pkey, (unsigned char **)pk); + *pk = (unsigned char*)pub->pkey->pkey.ptr; if (ppklen) - *ppklen = wolfSSL_EVP_PKEY_get_der(pub->pkey, NULL); + *ppklen = pub->pkey->pkey_sz; return WOLFSSL_SUCCESS; } diff --git a/tests/api.c b/tests/api.c index 597cb46a0..3139e3c7c 100644 --- a/tests/api.c +++ b/tests/api.c @@ -5519,7 +5519,7 @@ static void test_wolfSSL_X509_verify(void) WOLFSSL_X509* server; WOLFSSL_EVP_PKEY* pkey; unsigned char buf[2048]; - const unsigned char* pt; + const unsigned char* pt = NULL; int bufSz; printf(testingFmt, "wolfSSL X509 verify"); @@ -5546,6 +5546,9 @@ static void test_wolfSSL_X509_verify(void) /* success case */ pt = buf; AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz)); + + AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz); + AssertIntEQ(wolfSSL_X509_verify(server, pkey), WOLFSSL_SUCCESS); wolfSSL_EVP_PKEY_free(pkey); @@ -31744,7 +31747,7 @@ static void test_wolfSSL_i2d_PrivateKey() EVP_PKEY* pkey; const unsigned char* server_key = (const unsigned char*)server_key_der_2048; unsigned char buf[FOURK_BUF]; - unsigned char* pt; + unsigned char* pt = NULL; int bufSz; AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key, @@ -31763,7 +31766,7 @@ static void test_wolfSSL_i2d_PrivateKey() const unsigned char* client_key = (const unsigned char*)ecc_clikey_der_256; unsigned char buf[FOURK_BUF]; - unsigned char* pt; + unsigned char* pt = NULL; int bufSz; AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key, diff --git a/wolfssl/openssl/ssl.h b/wolfssl/openssl/ssl.h index 289cb3d98..85752502d 100644 --- a/wolfssl/openssl/ssl.h +++ b/wolfssl/openssl/ssl.h @@ -178,11 +178,11 @@ typedef STACK_OF(ACCESS_DESCRIPTION) AUTHORITY_INFO_ACCESS; #define PKCS8_PRIV_KEY_INFO_free wolfSSL_EVP_PKEY_free #define d2i_PKCS12_fp wolfSSL_d2i_PKCS12_fp +#define i2d_PUBKEY wolfSSL_i2d_PUBKEY #define d2i_PUBKEY wolfSSL_d2i_PUBKEY #define d2i_PUBKEY_bio wolfSSL_d2i_PUBKEY_bio #define d2i_PrivateKey wolfSSL_d2i_PrivateKey #define d2i_AutoPrivateKey wolfSSL_d2i_AutoPrivateKey -#define i2d_PrivateKey wolfSSL_i2d_PrivateKey #define SSL_use_PrivateKey wolfSSL_use_PrivateKey #define SSL_use_PrivateKey_ASN1 wolfSSL_use_PrivateKey_ASN1 #define SSL_use_RSAPrivateKey_ASN1 wolfSSL_use_RSAPrivateKey_ASN1 diff --git a/wolfssl/ssl.h b/wolfssl/ssl.h index d0abe090b..647b2a001 100644 --- a/wolfssl/ssl.h +++ b/wolfssl/ssl.h @@ -1427,11 +1427,12 @@ WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_d2i_PUBKEY_bio(WOLFSSL_BIO* bio, WOLFSSL_EVP_PKEY** out); WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_d2i_PUBKEY(WOLFSSL_EVP_PKEY** key, const unsigned char** in, long inSz); +WOLFSSL_API int wolfSSL_i2d_PUBKEY(const WOLFSSL_EVP_PKEY *key, unsigned char **der); WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey(int type, WOLFSSL_EVP_PKEY** out, const unsigned char **in, long inSz); WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey_EVP(WOLFSSL_EVP_PKEY** key, unsigned char** in, long inSz); -WOLFSSL_API int wolfSSL_i2d_PrivateKey(WOLFSSL_EVP_PKEY* key, +WOLFSSL_API int wolfSSL_i2d_PrivateKey(const WOLFSSL_EVP_PKEY* key, unsigned char** der); WOLFSSL_API int wolfSSL_X509_cmp_current_time(const WOLFSSL_ASN1_TIME*); #ifdef OPENSSL_EXTRA