mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-07-29 18:27:29 +02:00
Implement wolfSSL_i2d_PUBKEY and refactor wolfSSL_i2d_PrivateKey
This commit is contained in:
78
src/ssl.c
78
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;
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user