Skip to content

Commit

Permalink
Merge pull request #8369 from lealem47/zd18687
Browse files Browse the repository at this point in the history
Fix OPENSSL_ALL build with WOLFSSL_NO_REALLOC
  • Loading branch information
dgarske authored Jan 23, 2025
2 parents dd2c5b1 + 161da60 commit fee2364
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2770,9 +2770,21 @@ int wolfSSL_BIO_flush(WOLFSSL_BIO* bio)
}
else {
size_t currLen = XSTRLEN(b->ip);
#ifdef WOLFSSL_NO_REALLOC
char* tmp = NULL;
#endif

if (currLen != newLen) {
#ifdef WOLFSSL_NO_REALLOC
tmp = b->ip;
b->ip = (char*)XMALLOC(newLen+1, b->heap, DYNAMIC_TYPE_OPENSSL);
XMEMCPY(b->ip, tmp, newLen);
XFREE(tmp, b->heap, DYNAMIC_TYPE_OPENSSL);
tmp = NULL;
#else
b->ip = (char*)XREALLOC(b->ip, newLen + 1, b->heap,
DYNAMIC_TYPE_OPENSSL);
#endif
if (b->ip == NULL) {
WOLFSSL_MSG("Hostname realloc failed.");
return WOLFSSL_FAILURE;
Expand Down
10 changes: 10 additions & 0 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -773,8 +773,18 @@ static char* expandValue(WOLFSSL_CONF *conf, const char* section,
/* This will allocate slightly more memory than necessary
* but better be safe */
strLen += valueLen;
#ifdef WOLFSSL_NO_REALLOC
newRet = (char*)XMALLOC(strLen + 1, NULL,
DYNAMIC_TYPE_OPENSSL);
if (newRet != NULL) {
XMEMCPY(newRet, ret, (strLen - valueLen) + 1);
XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
ret = NULL;
}
#else
newRet = (char*)XREALLOC(ret, strLen + 1, NULL,
DYNAMIC_TYPE_OPENSSL);
#endif
if (!newRet) {
WOLFSSL_MSG("realloc error");
goto expand_cleanup;
Expand Down
11 changes: 11 additions & 0 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -39404,8 +39404,19 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
WOLFSSL_MSG("Found session matching the session id"
" found in the ticket");
/* Allocate and populate an InternalTicket */
#ifdef WOLFSSL_NO_REALLOC
tmp = (byte*)XMALLOC(sizeof(InternalTicket), ssl->heap,
DYNAMIC_TYPE_TLSX);
if (tmp != NULL)
{
XMEMCPY(tmp, psk->identity, psk->identityLen);
XFREE(psk->identity, ssl->heap, DYNAMIC_TYPE_TLSX);
psk->identity = NULL;
}
#else
tmp = (byte*)XREALLOC(psk->identity, sizeof(InternalTicket),
ssl->heap, DYNAMIC_TYPE_TLSX);
#endif
if (tmp != NULL) {
XMEMSET(tmp, 0, sizeof(InternalTicket));
psk->identity = tmp;
Expand Down
11 changes: 11 additions & 0 deletions src/pk.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,19 @@ static int der_to_enc_pem_alloc(unsigned char* der, int derSz,
byte *tmpBuf;

/* Add space for padding. */
#ifdef WOLFSSL_NO_REALLOC
tmpBuf = (byte*)XMALLOC((size_t)(derSz + blockSz), heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (tmpBuf != NULL)
{
XMEMCPY(tmpBuf, der, (size_t)(derSz));
XFREE(der, heap, DYNAMIC_TYPE_TMP_BUFFER);
der = NULL;
}
#else
tmpBuf = (byte*)XREALLOC(der, (size_t)(derSz + blockSz), heap,
DYNAMIC_TYPE_TMP_BUFFER);
#endif
if (tmpBuf == NULL) {
WOLFSSL_ERROR_MSG("Extending DER buffer failed");
ret = 0; /* der buffer is free'd at the end of the function */
Expand Down
21 changes: 21 additions & 0 deletions src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -24807,8 +24807,18 @@ int wolfSSL_BUF_MEM_grow_ex(WOLFSSL_BUF_MEM* buf, size_t len,
/* expand size, to handle growth */
mx = (len_int + 3) / 3 * 4;

#ifdef WOLFSSL_NO_REALLOC
tmp = (char*)XMALLOC(mx, NULL, DYNAMIC_TYPE_OPENSSL);
if (tmp != NULL) {
XMEMCPY(tmp, buf->data, len_int);
XFREE(buf->data, NULL, DYNAMIC_TYPE_OPENSSL);
buf->data = NULL;
}
#else
/* use realloc */
tmp = (char*)XREALLOC(buf->data, mx, NULL, DYNAMIC_TYPE_OPENSSL);
#endif

if (tmp == NULL) {
return 0; /* ERR_R_MALLOC_FAILURE; */
}
Expand Down Expand Up @@ -24850,7 +24860,18 @@ int wolfSSL_BUF_MEM_resize(WOLFSSL_BUF_MEM* buf, size_t len)
mx = ((int)len + 3) / 3 * 4;

/* We want to shrink the internal buffer */
#ifdef WOLFSSL_NO_REALLOC
tmp = (char*)XMALLOC(mx, NULL, DYNAMIC_TYPE_OPENSSL);
if (tmp != NULL )
{
XMEMCPY(tmp, buf->data, len);
XFREE(buf->data,NULL,DYNAMIC_TYPE_OPENSSL);
buf->data = NULL;
}
#else
tmp = (char*)XREALLOC(buf->data, mx, NULL, DYNAMIC_TYPE_OPENSSL);
#endif

if (tmp == NULL)
return 0;

Expand Down
9 changes: 9 additions & 0 deletions src/ssl_asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -797,9 +797,18 @@ static int wolfssl_asn1_bit_string_grow(WOLFSSL_ASN1_BIT_STRING* bitStr,
int ret = 1;
byte* tmp;

#ifdef WOLFSSL_NO_REALLOC
tmp = (byte*)XMALLOC((size_t)len, NULL, DYNAMIC_TYPE_OPENSSL);
if (tmp != NULL) {
XMEMCPY(tmp, bitStr->data, bitStr->length);
XFREE(bitStr->data, NULL, DYNAMIC_TYPE_OPENSSL);
bitStr->data = NULL;
}
#else
/* Realloc to length required. */
tmp = (byte*)XREALLOC(bitStr->data, (size_t)len, NULL,
DYNAMIC_TYPE_OPENSSL);
#endif
if (tmp == NULL) {
ret = 0;
}
Expand Down
22 changes: 18 additions & 4 deletions src/x509.c
Original file line number Diff line number Diff line change
Expand Up @@ -1181,12 +1181,24 @@ WOLFSSL_X509_EXTENSION* wolfSSL_X509_set_ext(WOLFSSL_X509* x509, int loc)
}
}

ext->obj->objSz = (unsigned int)objSz;
if (((ext->obj->dynamic & WOLFSSL_ASN1_DYNAMIC_DATA) != 0) ||
(ext->obj->obj == NULL)) {
ext->obj->obj =(byte*)XREALLOC((byte*)ext->obj->obj,
ext->obj->objSz,
NULL,DYNAMIC_TYPE_ASN1);
#ifdef WOLFSSL_NO_REALLOC
byte* tmp = NULL;

tmp = (byte*)XMALLOC(objSz, NULL, DYNAMIC_TYPE_ASN1);
if (tmp != NULL && ext->obj->obj != NULL) {
XMEMCPY(tmp, ext->obj->obj, ext->obj->objSz);
XFREE((byte*)ext->obj->obj, NULL, DYNAMIC_TYPE_ASN1);
}
else if (tmp == NULL) {
ext->obj->obj = tmp;
}
ext->obj->obj = tmp;
#else
ext->obj->obj = (byte*)XREALLOC((byte*)ext->obj->obj, objSz,
NULL, DYNAMIC_TYPE_ASN1);
#endif
if (ext->obj->obj == NULL) {
wolfSSL_X509_EXTENSION_free(ext);
FreeDecodedCert(cert);
Expand All @@ -1201,6 +1213,8 @@ WOLFSSL_X509_EXTENSION* wolfSSL_X509_set_ext(WOLFSSL_X509* x509, int loc)
else {
ext->obj->dynamic &= ~WOLFSSL_ASN1_DYNAMIC_DATA;
}
ext->obj->objSz = (unsigned int)objSz;

/* Get OID from input and copy to ASN1_OBJECT buffer */
XMEMCPY(oidBuf+2, input+idx, length);
XMEMCPY((byte*)ext->obj->obj, oidBuf, ext->obj->objSz);
Expand Down
22 changes: 21 additions & 1 deletion wolfcrypt/src/wc_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -2422,6 +2422,10 @@ int wolfSSL_HwPkMutexUnLock(void)

int wc_InitMutex(wolfSSL_Mutex* m)
{
#if (defined(HAVE_FIPS) && FIPS_VERSION_EQ(5,2))
if (wolfCrypt_GetMode_fips() == FIPS_MODE_INIT)
return 0;
#endif
if (_mutex_init(m, NULL) == MQX_EOK)
return 0;
else
Expand All @@ -2438,6 +2442,13 @@ int wolfSSL_HwPkMutexUnLock(void)

int wc_LockMutex(wolfSSL_Mutex* m)
{
#if (defined(HAVE_FIPS) && FIPS_VERSION_EQ(5,2))
if (m->VALID != MUTEX_VALID) {
if (_mutex_init(m, NULL) != MQX_EOK)
return BAD_MUTEX_E;
}
#endif

if (_mutex_lock(m) == MQX_EOK)
return 0;
else
Expand All @@ -2446,6 +2457,13 @@ int wolfSSL_HwPkMutexUnLock(void)

int wc_UnLockMutex(wolfSSL_Mutex* m)
{
#if (defined(HAVE_FIPS) && FIPS_VERSION_EQ(5,2))
if (m->VALID != MUTEX_VALID) {
if (_mutex_init(m, NULL) != MQX_EOK)
return BAD_MUTEX_E;
}
#endif

if (_mutex_unlock(m) == MQX_EOK)
return 0;
else
Expand Down Expand Up @@ -2710,7 +2728,9 @@ int wolfSSL_HwPkMutexUnLock(void)

#elif defined(WOLFSSL_CMSIS_RTOS)

#define CMSIS_NMUTEX 10
#ifndef CMSIS_NMUTEX
#define CMSIS_NMUTEX 10
#endif
osMutexDef(wolfSSL_mt0); osMutexDef(wolfSSL_mt1); osMutexDef(wolfSSL_mt2);
osMutexDef(wolfSSL_mt3); osMutexDef(wolfSSL_mt4); osMutexDef(wolfSSL_mt5);
osMutexDef(wolfSSL_mt6); osMutexDef(wolfSSL_mt7); osMutexDef(wolfSSL_mt8);
Expand Down
2 changes: 1 addition & 1 deletion wolfssl/ssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
#endif

#ifdef OPENSSL_ALL
#ifndef WOLFSSL_HAVE_BIO_ADDR
#if !defined(WOLFSSL_HAVE_BIO_ADDR) && !defined(WOLFSSL_NO_SOCK)
#define WOLFSSL_HAVE_BIO_ADDR
#endif
#if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_DTLS_MTU)
Expand Down

0 comments on commit fee2364

Please sign in to comment.