Implement wolfSSL_i2d_PUBKEY and refactor wolfSSL_i2d_PrivateKey

This commit is contained in:
Juliusz Sosinowicz
2020-03-30 14:07:47 +02:00
parent 1f0d6d5f31
commit ea8dd31de0
4 changed files with 50 additions and 42 deletions

View File

@ -7615,6 +7615,39 @@ WOLFSSL_EVP_PKEY* wolfSSL_d2i_PUBKEY(WOLFSSL_EVP_PKEY** out,
return pkey; 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. /* 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; 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 /* 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. * NOTE: This also advances the "der" pointer to be at the end of buffer.
* *
* Returns size of key buffer on success * 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_EVP_PKEY_get_der(key, der);
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;
} }
/* Creates a new WC_PKCS12 structure /* Creates a new WC_PKCS12 structure
@ -22232,13 +22241,11 @@ WC_PKCS12* wolfSSL_PKCS12_create(char* pass, char* name,
WC_PKCS12* pkcs12; WC_PKCS12* pkcs12;
WC_DerCertList* list = NULL; WC_DerCertList* list = NULL;
word32 passSz; word32 passSz;
byte* keyDer; byte* keyDer = NULL;
word32 keyDerSz; word32 keyDerSz;
byte* certDer; byte* certDer;
int certDerSz; int certDerSz;
int ret;
WOLFSSL_ENTER("wolfSSL_PKCS12_create()"); WOLFSSL_ENTER("wolfSSL_PKCS12_create()");
if (pass == NULL || pkey == NULL || cert == NULL) { if (pass == NULL || pkey == NULL || cert == NULL) {
@ -22247,11 +22254,8 @@ WC_PKCS12* wolfSSL_PKCS12_create(char* pass, char* name,
} }
passSz = (word32)XSTRLEN(pass); passSz = (word32)XSTRLEN(pass);
if ((ret = wolfSSL_EVP_PKEY_get_der(pkey, &keyDer)) < 0) { keyDer = (byte*)pkey->pkey.ptr;
WOLFSSL_LEAVE("wolfSSL_PKCS12_create", ret); keyDerSz = pkey->pkey_sz;
return NULL;
}
keyDerSz = ret;
certDer = (byte*)wolfSSL_X509_get_der(cert, &certDerSz); certDer = (byte*)wolfSSL_X509_get_der(cert, &certDerSz);
if (certDer == NULL) { if (certDer == NULL) {
@ -25314,9 +25318,9 @@ int wolfSSL_X509_PUBKEY_get0_param(WOLFSSL_ASN1_OBJECT **ppkalg,
if (ppkalg) if (ppkalg)
*ppkalg = pub->algor->algorithm; *ppkalg = pub->algor->algorithm;
if (pk) if (pk)
wolfSSL_EVP_PKEY_get_der(pub->pkey, (unsigned char **)pk); *pk = (unsigned char*)pub->pkey->pkey.ptr;
if (ppklen) if (ppklen)
*ppklen = wolfSSL_EVP_PKEY_get_der(pub->pkey, NULL); *ppklen = pub->pkey->pkey_sz;
return WOLFSSL_SUCCESS; return WOLFSSL_SUCCESS;
} }

View File

@ -5519,7 +5519,7 @@ static void test_wolfSSL_X509_verify(void)
WOLFSSL_X509* server; WOLFSSL_X509* server;
WOLFSSL_EVP_PKEY* pkey; WOLFSSL_EVP_PKEY* pkey;
unsigned char buf[2048]; unsigned char buf[2048];
const unsigned char* pt; const unsigned char* pt = NULL;
int bufSz; int bufSz;
printf(testingFmt, "wolfSSL X509 verify"); printf(testingFmt, "wolfSSL X509 verify");
@ -5546,6 +5546,9 @@ static void test_wolfSSL_X509_verify(void)
/* success case */ /* success case */
pt = buf; pt = buf;
AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz)); AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
AssertIntEQ(wolfSSL_X509_verify(server, pkey), WOLFSSL_SUCCESS); AssertIntEQ(wolfSSL_X509_verify(server, pkey), WOLFSSL_SUCCESS);
wolfSSL_EVP_PKEY_free(pkey); wolfSSL_EVP_PKEY_free(pkey);
@ -31744,7 +31747,7 @@ static void test_wolfSSL_i2d_PrivateKey()
EVP_PKEY* pkey; EVP_PKEY* pkey;
const unsigned char* server_key = (const unsigned char*)server_key_der_2048; const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
unsigned char buf[FOURK_BUF]; unsigned char buf[FOURK_BUF];
unsigned char* pt; unsigned char* pt = NULL;
int bufSz; int bufSz;
AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key, 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* client_key =
(const unsigned char*)ecc_clikey_der_256; (const unsigned char*)ecc_clikey_der_256;
unsigned char buf[FOURK_BUF]; unsigned char buf[FOURK_BUF];
unsigned char* pt; unsigned char* pt = NULL;
int bufSz; int bufSz;
AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key, AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,

View File

@ -178,11 +178,11 @@ typedef STACK_OF(ACCESS_DESCRIPTION) AUTHORITY_INFO_ACCESS;
#define PKCS8_PRIV_KEY_INFO_free wolfSSL_EVP_PKEY_free #define PKCS8_PRIV_KEY_INFO_free wolfSSL_EVP_PKEY_free
#define d2i_PKCS12_fp wolfSSL_d2i_PKCS12_fp #define d2i_PKCS12_fp wolfSSL_d2i_PKCS12_fp
#define i2d_PUBKEY wolfSSL_i2d_PUBKEY
#define d2i_PUBKEY wolfSSL_d2i_PUBKEY #define d2i_PUBKEY wolfSSL_d2i_PUBKEY
#define d2i_PUBKEY_bio wolfSSL_d2i_PUBKEY_bio #define d2i_PUBKEY_bio wolfSSL_d2i_PUBKEY_bio
#define d2i_PrivateKey wolfSSL_d2i_PrivateKey #define d2i_PrivateKey wolfSSL_d2i_PrivateKey
#define d2i_AutoPrivateKey wolfSSL_d2i_AutoPrivateKey #define d2i_AutoPrivateKey wolfSSL_d2i_AutoPrivateKey
#define i2d_PrivateKey wolfSSL_i2d_PrivateKey
#define SSL_use_PrivateKey wolfSSL_use_PrivateKey #define SSL_use_PrivateKey wolfSSL_use_PrivateKey
#define SSL_use_PrivateKey_ASN1 wolfSSL_use_PrivateKey_ASN1 #define SSL_use_PrivateKey_ASN1 wolfSSL_use_PrivateKey_ASN1
#define SSL_use_RSAPrivateKey_ASN1 wolfSSL_use_RSAPrivateKey_ASN1 #define SSL_use_RSAPrivateKey_ASN1 wolfSSL_use_RSAPrivateKey_ASN1

View File

@ -1427,11 +1427,12 @@ WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_d2i_PUBKEY_bio(WOLFSSL_BIO* bio,
WOLFSSL_EVP_PKEY** out); WOLFSSL_EVP_PKEY** out);
WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_d2i_PUBKEY(WOLFSSL_EVP_PKEY** key, WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_d2i_PUBKEY(WOLFSSL_EVP_PKEY** key,
const unsigned char** in, long inSz); 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_API WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey(int type,
WOLFSSL_EVP_PKEY** out, const unsigned char **in, long inSz); WOLFSSL_EVP_PKEY** out, const unsigned char **in, long inSz);
WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey_EVP(WOLFSSL_EVP_PKEY** key, WOLFSSL_API WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey_EVP(WOLFSSL_EVP_PKEY** key,
unsigned char** in, long inSz); 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); unsigned char** der);
WOLFSSL_API int wolfSSL_X509_cmp_current_time(const WOLFSSL_ASN1_TIME*); WOLFSSL_API int wolfSSL_X509_cmp_current_time(const WOLFSSL_ASN1_TIME*);
#ifdef OPENSSL_EXTRA #ifdef OPENSSL_EXTRA