smallstack reduction for wolfSSL_ASN1_INTEGER_to_BN

This commit is contained in:
Chris Conlon
2022-03-18 09:12:58 -06:00
parent ddc1899d48
commit 64a309e245

View File

@ -53993,7 +53993,11 @@ int SetIndividualInternal(WOLFSSL_BIGNUM* bn, mp_int* mpi)
WOLFSSL_BIGNUM *wolfSSL_ASN1_INTEGER_to_BN(const WOLFSSL_ASN1_INTEGER *ai, WOLFSSL_BIGNUM *wolfSSL_ASN1_INTEGER_to_BN(const WOLFSSL_ASN1_INTEGER *ai,
WOLFSSL_BIGNUM *bn) WOLFSSL_BIGNUM *bn)
{ {
mp_int mpi; #ifdef WOLFSSL_SMALL_STACK
mp_int* mpi = NULL;
#else
mp_int mpi[1];
#endif
word32 idx = 0; word32 idx = 0;
int ret; int ret;
@ -54003,29 +54007,49 @@ WOLFSSL_BIGNUM *wolfSSL_ASN1_INTEGER_to_BN(const WOLFSSL_ASN1_INTEGER *ai,
return NULL; return NULL;
} }
ret = GetInt(&mpi, ai->data, &idx, ai->dataMax); #ifdef WOLFSSL_SMALL_STACK
mpi = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (mpi == NULL) {
return NULL;
}
#endif
ret = GetInt(mpi, ai->data, &idx, ai->dataMax);
if (ret != 0) { if (ret != 0) {
#if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY) #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
ret = mp_init(&mpi); /* must init mpi */ ret = mp_init(mpi); /* must init mpi */
if (ret != MP_OKAY) { if (ret != MP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(mpi, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return NULL; return NULL;
} }
/* Serial number in QT starts at index 0 of data */ /* Serial number in QT starts at index 0 of data */
if (mp_read_unsigned_bin(&mpi, (byte*)ai->data, ai->length) != 0) { if (mp_read_unsigned_bin(mpi, (byte*)ai->data, ai->length) != 0) {
mp_clear(&mpi); mp_clear(mpi);
#ifdef WOLFSSL_SMALL_STACK
XFREE(mpi, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return NULL; return NULL;
} }
#else #else
/* expecting ASN1 format for INTEGER */ /* expecting ASN1 format for INTEGER */
WOLFSSL_LEAVE("wolfSSL_ASN1_INTEGER_to_BN", ret); WOLFSSL_LEAVE("wolfSSL_ASN1_INTEGER_to_BN", ret);
#ifdef WOLFSSL_SMALL_STACK
XFREE(mpi, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return NULL; return NULL;
#endif #endif
} }
/* mp_clear needs called because mpi is copied and causes memory leak with /* mp_clear needs called because mpi is copied and causes memory leak with
* --disable-fastmath */ * --disable-fastmath */
ret = SetIndividualExternal(&bn, &mpi); ret = SetIndividualExternal(&bn, mpi);
mp_clear(&mpi); mp_clear(mpi);
#ifdef WOLFSSL_SMALL_STACK
XFREE(mpi, NULL, DYNAMIC_TYPE_BIGINT);
#endif
if (ret != WOLFSSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
return NULL; return NULL;