src/ssl.c: fix PK object on stack in wolfSSL_i2d_PublicKey().

This commit is contained in:
Daniel Pouzzner
2023-02-08 21:30:50 -06:00
parent ef0eda4091
commit 63fdc4e843

View File

@@ -22959,7 +22959,7 @@ int wolfSSL_i2d_PublicKey(const WOLFSSL_EVP_PKEY *key, unsigned char **der)
unsigned char *local_der = NULL; unsigned char *local_der = NULL;
word32 local_derSz = 0; word32 local_derSz = 0;
unsigned char *pub_der = NULL; unsigned char *pub_der = NULL;
ecc_key eccKey; ecc_key *eccKey = NULL;
word32 inOutIdx = 0; word32 inOutIdx = 0;
#endif #endif
word32 pub_derSz = 0; word32 pub_derSz = 0;
@@ -22996,15 +22996,23 @@ int wolfSSL_i2d_PublicKey(const WOLFSSL_EVP_PKEY *key, unsigned char **der)
} }
if (ret == 0) { if (ret == 0) {
ret = wc_ecc_init(&eccKey); eccKey = (ecc_key *)XMALLOC(sizeof(*eccKey), NULL, DYNAMIC_TYPE_ECC);
if (eccKey == NULL) {
WOLFSSL_MSG("Failed to allocate key buffer.");
ret = WOLFSSL_FATAL_ERROR;
}
} }
if (ret == 0) { if (ret == 0) {
ret = wc_EccPublicKeyDecode(local_der, &inOutIdx, &eccKey, local_derSz); ret = wc_ecc_init(eccKey);
} }
if (ret == 0) { if (ret == 0) {
pub_derSz = wc_EccPublicKeyDerSize(&eccKey, 0); ret = wc_EccPublicKeyDecode(local_der, &inOutIdx, eccKey, local_derSz);
}
if (ret == 0) {
pub_derSz = wc_EccPublicKeyDerSize(eccKey, 0);
if (pub_derSz <= 0) { if (pub_derSz <= 0) {
ret = WOLFSSL_FAILURE; ret = WOLFSSL_FAILURE;
} }
@@ -23020,7 +23028,7 @@ int wolfSSL_i2d_PublicKey(const WOLFSSL_EVP_PKEY *key, unsigned char **der)
} }
if (ret == 0) { if (ret == 0) {
pub_derSz = wc_EccPublicKeyToDer(&eccKey, pub_der, pub_derSz, 0); pub_derSz = wc_EccPublicKeyToDer(eccKey, pub_der, pub_derSz, 0);
if (pub_derSz <= 0) { if (pub_derSz <= 0) {
ret = WOLFSSL_FATAL_ERROR; ret = WOLFSSL_FATAL_ERROR;
} }
@@ -23049,7 +23057,9 @@ int wolfSSL_i2d_PublicKey(const WOLFSSL_EVP_PKEY *key, unsigned char **der)
XFREE(pub_der, NULL, DYNAMIC_TYPE_PUBLIC_KEY); XFREE(pub_der, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
XFREE(local_der, NULL, DYNAMIC_TYPE_PUBLIC_KEY); XFREE(local_der, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
wc_ecc_free(&eccKey); wc_ecc_free(eccKey);
XFREE(eccKey, NULL, DYNAMIC_TYPE_ECC);
#else #else
ret = WOLFSSL_FATAL_ERROR; ret = WOLFSSL_FATAL_ERROR;
#endif /* HAVE_ECC */ #endif /* HAVE_ECC */