Skip to content

Commit 5f1ddad

Browse files
committed
Regression test fixes
Fix unit tests to not compile when NO_RSA is defined and RSA used. test_wc_PKCS7_EncodeSignedData: only RSA supported with streaming. test_wolfSSL_RSA when SP math and SP: CRT parameters required. test_wolfSSL_OCSP_REQ_CTX to compile with NO_ASN_TIME. test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS: make sure all objects freed even on memory allocation failure. test_wolfSSL_error_cb: don't use bio if is NULL. test_wolfSSL_BN_enc_dec: don't free a twice on memory allocation error. test_wc_dilithium_der: remove debug printing test_othername_and_SID_ext: make sid_oid NULL after free to ensure no double free on later memory allocation failure. test_wolfSSL_RSA: don't leak when BN_dup fails. test_wolfSSL_i2d_ASN1_TYPE: free ASN1 string whn no ASN1 type to put it into. test_tls13_rpk_handshake: don't leak on failure test_dtls_client_hello_timeout_downgrade: only move memory when test is wolfSSL_certs_clear, wolfSSL_set_SSL_CTX, SetSSL_CTX: Check return from AllocCopyDer. d2i_generic: make sure impBuf is only freed once. wolfSSL_BIO_write: don't dereference front unless it is not NULL. wolfssl_dns_entry_othername_to_gn: don't free obj twice wolfSSL_X509_REQ_add1_attr_by_NID: don't access reqAttributes if NULL. succeeding.
1 parent 9c4960f commit 5f1ddad

6 files changed

Lines changed: 204 additions & 55 deletions

File tree

src/bio.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,9 @@ int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len)
834834
(const char*)data, len, 0, ret);
835835
}
836836

837-
XFREE(frmt, front->heap, DYNAMIC_TYPE_TMP_BUFFER);
837+
if (front != NULL) {
838+
XFREE(frmt, front->heap, DYNAMIC_TYPE_TMP_BUFFER);
839+
}
838840

839841
#ifdef WOLFSSL_BASE64_ENCODE
840842
if (retB64 > 0 && ret > 0)

src/internal.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6849,10 +6849,14 @@ int SetSSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
68496849
if (ssl->buffers.key != NULL) {
68506850
FreeDer(&ssl->buffers.key);
68516851
}
6852-
AllocCopyDer(&ssl->buffers.key, ctx->privateKey->buffer,
6852+
ret = AllocCopyDer(&ssl->buffers.key, ctx->privateKey->buffer,
68536853
ctx->privateKey->length, ctx->privateKey->type,
68546854
ctx->privateKey->heap);
6855+
if (ret != 0) {
6856+
return ret;
6857+
}
68556858
ssl->buffers.weOwnKey = 1;
6859+
ret = WOLFSSL_SUCCESS;
68566860
}
68576861
else {
68586862
ssl->buffers.key = ctx->privateKey;
@@ -6862,9 +6866,12 @@ int SetSSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
68626866
#endif
68636867
#else
68646868
if (ctx->privateKey != NULL) {
6865-
AllocCopyDer(&ssl->buffers.key, ctx->privateKey->buffer,
6869+
ret = AllocCopyDer(&ssl->buffers.key, ctx->privateKey->buffer,
68666870
ctx->privateKey->length, ctx->privateKey->type,
68676871
ctx->privateKey->heap);
6872+
if (ret != 0) {
6873+
return ret;
6874+
}
68686875
ssl->buffers.weOwnKey = 1;
68696876
/* Blind the private key for the SSL with new random mask. */
68706877
wolfssl_priv_der_unblind(ssl->buffers.key, ctx->privateKeyMask);
@@ -6885,16 +6892,20 @@ int SetSSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
68856892
ssl->buffers.altKey = ctx->altPrivateKey;
68866893
#else
68876894
if (ctx->altPrivateKey != NULL) {
6888-
AllocCopyDer(&ssl->buffers.altkey, ctx->altPrivateKey->buffer,
6895+
ret = AllocCopyDer(&ssl->buffers.altkey, ctx->altPrivateKey->buffer,
68896896
ctx->altPrivateKey->length, ctx->altPrivateKey->type,
68906897
ctx->altPrivateKey->heap);
6898+
if (ret != 0) {
6899+
return ret;
6900+
}
68916901
/* Blind the private key for the SSL with new random mask. */
68926902
wolfssl_priv_der_unblind(ssl->buffers.altKey, ctx->altPrivateKeyMask);
68936903
ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.altKey,
68946904
&ssl->buffers.altKeyMask);
68956905
if (ret != 0) {
68966906
return ret;
68976907
}
6908+
ret = WOLFSSL_SUCCESS;
68986909
}
68996910
#endif
69006911
ssl->buffers.altKeyType = ctx->altPrivateKeyType;

src/ssl.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19789,11 +19789,15 @@ void wolfSSL_certs_clear(WOLFSSL* ssl)
1978919789
return;
1979019790

1979119791
/* ctx still owns certificate, certChain, key, dh, and cm */
19792-
if (ssl->buffers.weOwnCert)
19792+
if (ssl->buffers.weOwnCert) {
1979319793
FreeDer(&ssl->buffers.certificate);
19794+
ssl->buffers.weOwnCert = 0;
19795+
}
1979419796
ssl->buffers.certificate = NULL;
19795-
if (ssl->buffers.weOwnCertChain)
19797+
if (ssl->buffers.weOwnCertChain) {
1979619798
FreeDer(&ssl->buffers.certChain);
19799+
ssl->buffers.weOwnCertChain = 0;
19800+
}
1979719801
ssl->buffers.certChain = NULL;
1979819802
#ifdef WOLFSSL_TLS13
1979919803
ssl->buffers.certChainCnt = 0;
@@ -19803,6 +19807,7 @@ void wolfSSL_certs_clear(WOLFSSL* ssl)
1980319807
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
1980419808
FreeDer(&ssl->buffers.keyMask);
1980519809
#endif
19810+
ssl->buffers.weOwnKey = 0;
1980619811
}
1980719812
ssl->buffers.key = NULL;
1980819813
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
@@ -19819,6 +19824,7 @@ void wolfSSL_certs_clear(WOLFSSL* ssl)
1981919824
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
1982019825
FreeDer(&ssl->buffers.altKeyMask);
1982119826
#endif
19827+
ssl->buffers.weOwnAltKey = 0;
1982219828
}
1982319829
ssl->buffers.altKey = NULL;
1982419830
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
@@ -20398,11 +20404,13 @@ WOLFSSL_CTX* wolfSSL_set_SSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
2039820404
if (ctx->certificate != NULL) {
2039920405
if (ssl->buffers.certificate != NULL) {
2040020406
FreeDer(&ssl->buffers.certificate);
20407+
ssl->buffers.certificate = NULL;
2040120408
}
2040220409
ret = AllocCopyDer(&ssl->buffers.certificate, ctx->certificate->buffer,
2040320410
ctx->certificate->length, ctx->certificate->type,
2040420411
ctx->certificate->heap);
2040520412
if (ret != 0) {
20413+
ssl->buffers.weOwnCert = 0;
2040620414
return NULL;
2040720415
}
2040820416

@@ -20412,11 +20420,13 @@ WOLFSSL_CTX* wolfSSL_set_SSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
2041220420
if (ctx->certChain != NULL) {
2041320421
if (ssl->buffers.certChain != NULL) {
2041420422
FreeDer(&ssl->buffers.certChain);
20423+
ssl->buffers.certChain = NULL;
2041520424
}
2041620425
ret = AllocCopyDer(&ssl->buffers.certChain, ctx->certChain->buffer,
2041720426
ctx->certChain->length, ctx->certChain->type,
2041820427
ctx->certChain->heap);
2041920428
if (ret != 0) {
20429+
ssl->buffers.weOwnCertChain = 0;
2042020430
return NULL;
2042120431
}
2042220432

@@ -20436,10 +20446,15 @@ WOLFSSL_CTX* wolfSSL_set_SSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
2043620446
if (ctx->privateKey != NULL) {
2043720447
if (ssl->buffers.key != NULL) {
2043820448
FreeDer(&ssl->buffers.key);
20449+
ssl->buffers.key = NULL;
2043920450
}
20440-
AllocCopyDer(&ssl->buffers.key, ctx->privateKey->buffer,
20451+
ret = AllocCopyDer(&ssl->buffers.key, ctx->privateKey->buffer,
2044120452
ctx->privateKey->length, ctx->privateKey->type,
2044220453
ctx->privateKey->heap);
20454+
if (ret != 0) {
20455+
ssl->buffers.weOwnKey = 0;
20456+
return NULL;
20457+
}
2044320458
ssl->buffers.weOwnKey = 1;
2044420459
}
2044520460
else {
@@ -20450,15 +20465,18 @@ WOLFSSL_CTX* wolfSSL_set_SSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
2045020465
#endif
2045120466
#else
2045220467
if (ctx->privateKey != NULL) {
20453-
AllocCopyDer(&ssl->buffers.key, ctx->privateKey->buffer,
20468+
ret = AllocCopyDer(&ssl->buffers.key, ctx->privateKey->buffer,
2045420469
ctx->privateKey->length, ctx->privateKey->type,
2045520470
ctx->privateKey->heap);
20471+
if (ret != 0) {
20472+
return NULL;
20473+
}
2045620474
/* Blind the private key for the SSL with new random mask. */
2045720475
wolfssl_priv_der_unblind(ssl->buffers.key, ctx->privateKeyMask);
2045820476
ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.key,
2045920477
&ssl->buffers.keyMask);
2046020478
if (ret != 0) {
20461-
return ret;
20479+
return NULL;
2046220480
}
2046320481
}
2046420482
#endif
@@ -20480,15 +20498,18 @@ WOLFSSL_CTX* wolfSSL_set_SSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
2048020498
ssl->buffers.altKey = ctx->altPrivateKey;
2048120499
#else
2048220500
if (ctx->altPrivateKey != NULL) {
20483-
AllocCopyDer(&ssl->buffers.altkey, ctx->altPrivateKey->buffer,
20501+
ret = AllocCopyDer(&ssl->buffers.altkey, ctx->altPrivateKey->buffer,
2048420502
ctx->altPrivateKey->length, ctx->altPrivateKey->type,
2048520503
ctx->altPrivateKey->heap);
20504+
if (ret != 0) {
20505+
return NULL;
20506+
}
2048620507
/* Blind the private key for the SSL with new random mask. */
2048720508
wolfssl_priv_der_unblind(ssl->buffers.altKey, ctx->altPrivateKeyMask);
2048820509
ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.altKey,
2048920510
&ssl->buffers.altKeyMask);
2049020511
if (ret != 0) {
20491-
return ret;
20512+
return NULL;
2049220513
}
2049320514
}
2049420515
#endif

src/ssl_asn1.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ static void* d2i_generic(const WOLFSSL_ASN1_TEMPLATE* mem,
580580
if (impBuf != NULL) {
581581
tmp = *src + (tmp - impBuf); /* for the next calculation */
582582
XFREE(impBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
583+
impBuf = NULL;
583584
}
584585
if (asnLen >= 0 && (int)(tmp - *src) != asnLen) {
585586
WOLFSSL_MSG("ptr not advanced enough");

src/x509.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,6 @@ static int wolfssl_dns_entry_othername_to_gn(DNS_entry* dns,
562562
/* Create a WOLFSSL_ASN1_STRING from the DER. */
563563
str = wolfSSL_ASN1_STRING_type_new(tag);
564564
if (str == NULL) {
565-
wolfSSL_ASN1_OBJECT_free(obj);
566565
goto err;
567566
}
568567
wolfSSL_ASN1_STRING_set(str, p, (int)len);
@@ -15431,12 +15430,14 @@ int wolfSSL_X509_REQ_add1_attr_by_NID(WOLFSSL_X509 *req,
1543115430
req->reqAttributes->type = STACK_TYPE_X509_REQ_ATTR;
1543215431
}
1543315432
}
15434-
if (req->reqAttributes->type == STACK_TYPE_X509_REQ_ATTR) {
15433+
if ((req->reqAttributes != NULL) &&
15434+
(req->reqAttributes->type == STACK_TYPE_X509_REQ_ATTR)) {
1543515435
ret = wolfSSL_sk_push(req->reqAttributes, attr) > 0
1543615436
? WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
1543715437
}
15438-
else
15438+
else {
1543915439
ret = WOLFSSL_FAILURE;
15440+
}
1544015441
if (ret != WOLFSSL_SUCCESS)
1544115442
wolfSSL_X509_ATTRIBUTE_free(attr);
1544215443
}

0 commit comments

Comments
 (0)