Skip to content

Commit 77792ac

Browse files
Merge pull request #8945 from SparkiDev/mem_fail_fixes_2
Memory allocation failure testing fixes
2 parents 9cf5bbc + 574de4b commit 77792ac

File tree

5 files changed

+106
-23
lines changed

5 files changed

+106
-23
lines changed

tests/api.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64461,13 +64461,15 @@ static int test_wolfSSL_SCR_after_resumption(void)
6446164461
ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
6446264462
ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
6446364463

64464-
func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
64465-
func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
64464+
if (EXPECT_SUCCESS()) {
64465+
func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
64466+
func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
6446664467

64467-
test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
64468+
test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
6446864469

64469-
ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
64470-
ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
64470+
ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
64471+
ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
64472+
}
6447164473

6447264474
wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
6447364475

tests/api/test_dtls.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,7 @@ int test_dtls13_longer_length(void)
898898
struct test_memio_ctx test_ctx;
899899
unsigned char readBuf[50];
900900
int seq16bit = 0;
901+
int ret;
901902

902903
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
903904

@@ -930,7 +931,8 @@ int test_dtls13_longer_length(void)
930931
ExpectIntEQ(test_ctx.s_len, 0);
931932

932933
ExpectIntEQ(test_dtls_communication(ssl_s, ssl_c), TEST_SUCCESS);
933-
ExpectIntEQ(test_dtls_shutdown(ssl_s, ssl_c, ctx_c, ctx_s), TEST_SUCCESS);
934+
ret = test_dtls_shutdown(ssl_s, ssl_c, ctx_c, ctx_s);
935+
ExpectIntEQ(ret, TEST_SUCCESS);
934936

935937
return EXPECT_RESULT();
936938
}
@@ -950,6 +952,7 @@ int test_dtls13_short_read(void)
950952
struct test_memio_ctx test_ctx;
951953
unsigned char readBuf[50];
952954
int i;
955+
int ret;
953956

954957
/* we setup two test, in the first one the server reads just two bytes of
955958
* the header, in the second one it reads just the header (5) */
@@ -980,8 +983,8 @@ int test_dtls13_short_read(void)
980983
ExpectIntEQ(test_ctx.s_len, 0);
981984

982985
ExpectIntEQ(test_dtls_communication(ssl_s, ssl_c), TEST_SUCCESS);
983-
ExpectIntEQ(test_dtls_shutdown(ssl_s, ssl_c, ctx_c, ctx_s),
984-
TEST_SUCCESS);
986+
ret = test_dtls_shutdown(ssl_s, ssl_c, ctx_c, ctx_s);
987+
ExpectIntEQ(ret, TEST_SUCCESS);
985988
ssl_c = ssl_s = NULL;
986989
ctx_c = ctx_s = NULL;
987990
}
@@ -1004,6 +1007,7 @@ int test_dtls12_short_read(void)
10041007
struct test_memio_ctx test_ctx;
10051008
unsigned char readBuf[50];
10061009
int i;
1010+
int ret;
10071011

10081012
for (i = 0; i < 3; i++) {
10091013
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
@@ -1041,8 +1045,8 @@ int test_dtls12_short_read(void)
10411045
ExpectIntEQ(test_ctx.s_len, 0);
10421046

10431047
ExpectIntEQ(test_dtls_communication(ssl_s, ssl_c), TEST_SUCCESS);
1044-
ExpectIntEQ(test_dtls_shutdown(ssl_s, ssl_c, ctx_c, ctx_s),
1045-
TEST_SUCCESS);
1048+
ret = test_dtls_shutdown(ssl_s, ssl_c, ctx_c, ctx_s);
1049+
ExpectIntEQ(ret, TEST_SUCCESS);
10461050
ssl_c = ssl_s = NULL;
10471051
ctx_c = ctx_s = NULL;
10481052
}
@@ -1064,6 +1068,7 @@ int test_dtls12_record_length_mismatch(void)
10641068
WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
10651069
struct test_memio_ctx test_ctx;
10661070
unsigned char readBuf[50];
1071+
int ret;
10671072

10681073
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
10691074

@@ -1090,7 +1095,8 @@ int test_dtls12_record_length_mismatch(void)
10901095
ExpectIntEQ(test_ctx.s_len, 0);
10911096

10921097
ExpectIntEQ(test_dtls_communication(ssl_s, ssl_c), TEST_SUCCESS);
1093-
ExpectIntEQ(test_dtls_shutdown(ssl_s, ssl_c, ctx_c, ctx_s), TEST_SUCCESS);
1098+
ret = test_dtls_shutdown(ssl_s, ssl_c, ctx_c, ctx_s);
1099+
ExpectIntEQ(ret, TEST_SUCCESS);
10941100

10951101
return EXPECT_RESULT();
10961102
}
@@ -1176,6 +1182,7 @@ int test_dtls_short_ciphertext(void)
11761182
WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
11771183
struct test_memio_ctx test_ctx;
11781184
unsigned char readBuf[50];
1185+
int ret;
11791186

11801187
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
11811188

@@ -1208,7 +1215,8 @@ int test_dtls_short_ciphertext(void)
12081215

12091216
ExpectIntEQ(test_dtls_communication(ssl_s, ssl_c), TEST_SUCCESS);
12101217

1211-
ExpectIntEQ(test_dtls_shutdown(ssl_s, ssl_c, ctx_c, ctx_s), TEST_SUCCESS);
1218+
ret = test_dtls_shutdown(ssl_s, ssl_c, ctx_c, ctx_s);
1219+
ExpectIntEQ(ret, TEST_SUCCESS);
12121220

12131221
return EXPECT_RESULT();
12141222
}

tests/api/test_mldsa.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16680,6 +16680,7 @@ int test_mldsa_pkcs8(void)
1668016680
dilithium_key mldsa_key;
1668116681
WC_RNG rng;
1668216682
word32 size;
16683+
int ret;
1668316684

1668416685
struct {
1668516686
int wcId;
@@ -16699,6 +16700,8 @@ int test_mldsa_pkcs8(void)
1669916700

1670016701
(void) pemSz;
1670116702

16703+
XMEMSET(&rng, 0, sizeof(rng));
16704+
1670216705
ExpectNotNull(der = (byte*) XMALLOC(derMaxSz, NULL,
1670316706
DYNAMIC_TYPE_TMP_BUFFER));
1670416707
ExpectNotNull(temp = (byte*) XMALLOC(tempMaxSz, NULL,
@@ -16789,7 +16792,8 @@ int test_mldsa_pkcs8(void)
1678916792
}
1679016793

1679116794
wc_dilithium_free(&mldsa_key);
16792-
ExpectIntEQ(wc_FreeRng(&rng), 0);
16795+
ret = wc_FreeRng(&rng);
16796+
ExpectIntEQ(ret, 0);
1679316797
wolfSSL_CTX_free(ctx);
1679416798
XFREE(temp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1679516799
XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
@@ -16821,6 +16825,7 @@ int test_mldsa_pkcs12(void)
1682116825
WC_RNG rng;
1682216826
dilithium_key mldsa_key;
1682316827
char pkcs12Passwd[] = "mldsa";
16828+
int ret;
1682416829

1682516830
struct {
1682616831
int enc;
@@ -16844,6 +16849,8 @@ int test_mldsa_pkcs12(void)
1684416849
ML_DSA_LEVEL5_PRV_KEY_SIZE, CTC_ML_DSA_LEVEL5, ML_DSA_LEVEL5_TYPE},
1684516850
};
1684616851

16852+
XMEMSET(&rng, 0, sizeof(rng));
16853+
1684716854
ExpectNotNull(inKey = (byte*) XMALLOC(inKeyMaxSz, NULL,
1684816855
DYNAMIC_TYPE_TMP_BUFFER));
1684916856
ExpectNotNull(inCert = (byte*) XMALLOC(inCertMaxSz, NULL,
@@ -16952,7 +16959,8 @@ int test_mldsa_pkcs12(void)
1695216959
}
1695316960

1695416961
wc_dilithium_free(&mldsa_key);
16955-
ExpectIntEQ(wc_FreeRng(&rng), 0);
16962+
ret = wc_FreeRng(&rng);
16963+
ExpectIntEQ(ret, 0);
1695616964
wolfSSL_CTX_free(ctx);
1695716965
XFREE(inCert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1695816966
XFREE(inKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);

tests/utils.c

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,22 +230,33 @@ int test_memio_setup_ex(struct test_memio_ctx *ctx,
230230
ret = wolfSSL_CTX_load_verify_buffer(*ctx_c, caCert, (long)caCertSz,
231231
WOLFSSL_FILETYPE_ASN1);
232232
}
233-
if (ret != WOLFSSL_SUCCESS)
233+
if (ret != WOLFSSL_SUCCESS) {
234+
wolfSSL_CTX_free(*ctx_c);
235+
*ctx_c = NULL;
234236
return -1;
237+
}
235238
#endif /* NO_CERTS */
236239
wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
237240
wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
238241
if (ctx->c_ciphers != NULL) {
239242
ret = wolfSSL_CTX_set_cipher_list(*ctx_c, ctx->c_ciphers);
240-
if (ret != WOLFSSL_SUCCESS)
243+
if (ret != WOLFSSL_SUCCESS) {
244+
wolfSSL_CTX_free(*ctx_c);
245+
*ctx_c = NULL;
241246
return -1;
247+
}
242248
}
243249
}
244250

245251
if (ctx_s != NULL && *ctx_s == NULL) {
246252
*ctx_s = wolfSSL_CTX_new(method_s());
247-
if (*ctx_s == NULL)
253+
if (*ctx_s == NULL) {
254+
if (ctx_c != NULL) {
255+
wolfSSL_CTX_free(*ctx_c);
256+
*ctx_c = NULL;
257+
}
248258
return -1;
259+
}
249260
#ifndef NO_CERTS
250261
if (serverKey == NULL) {
251262
ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, svrKeyFile,
@@ -255,8 +266,17 @@ int test_memio_setup_ex(struct test_memio_ctx *ctx,
255266
ret = wolfSSL_CTX_use_PrivateKey_buffer(*ctx_s, serverKey,
256267
(long)serverKeySz, WOLFSSL_FILETYPE_ASN1);
257268
}
258-
if (ret != WOLFSSL_SUCCESS)
259-
return- -1;
269+
if (ret != WOLFSSL_SUCCESS) {
270+
if (ctx_s != NULL) {
271+
wolfSSL_CTX_free(*ctx_s);
272+
*ctx_s = NULL;
273+
}
274+
if (ctx_c != NULL) {
275+
wolfSSL_CTX_free(*ctx_c);
276+
*ctx_c = NULL;
277+
}
278+
return -1;
279+
}
260280

261281
if (serverCert == NULL) {
262282
ret = wolfSSL_CTX_use_certificate_file(*ctx_s, svrCertFile,
@@ -266,29 +286,69 @@ int test_memio_setup_ex(struct test_memio_ctx *ctx,
266286
ret = wolfSSL_CTX_use_certificate_chain_buffer_format(*ctx_s,
267287
serverCert, (long)serverCertSz, WOLFSSL_FILETYPE_ASN1);
268288
}
269-
if (ret != WOLFSSL_SUCCESS)
289+
if (ret != WOLFSSL_SUCCESS) {
290+
if (ctx_s != NULL) {
291+
wolfSSL_CTX_free(*ctx_s);
292+
*ctx_s = NULL;
293+
}
294+
if (ctx_c != NULL) {
295+
wolfSSL_CTX_free(*ctx_c);
296+
*ctx_c = NULL;
297+
}
270298
return -1;
299+
}
271300
#endif /* NO_CERTS */
272301
wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
273302
wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
274303
if (ctx->s_ciphers != NULL) {
275304
ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
276-
if (ret != WOLFSSL_SUCCESS)
305+
if (ret != WOLFSSL_SUCCESS) {
306+
if (ctx_s != NULL) {
307+
wolfSSL_CTX_free(*ctx_s);
308+
*ctx_s = NULL;
309+
}
310+
if (ctx_c != NULL) {
311+
wolfSSL_CTX_free(*ctx_c);
312+
*ctx_c = NULL;
313+
}
277314
return -1;
315+
}
278316
}
279317
}
280318

281319
if (ctx_c != NULL && ssl_c != NULL) {
282320
*ssl_c = wolfSSL_new(*ctx_c);
283-
if (*ssl_c == NULL)
321+
if (*ssl_c == NULL) {
322+
if (ctx_s != NULL) {
323+
wolfSSL_CTX_free(*ctx_s);
324+
*ctx_s = NULL;
325+
}
326+
if (ctx_c != NULL) {
327+
wolfSSL_CTX_free(*ctx_c);
328+
*ctx_c = NULL;
329+
}
284330
return -1;
331+
}
285332
wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
286333
wolfSSL_SetIOReadCtx(*ssl_c, ctx);
287334
}
288335
if (ctx_s != NULL && ssl_s != NULL) {
289336
*ssl_s = wolfSSL_new(*ctx_s);
290-
if (*ssl_s == NULL)
337+
if (*ssl_s == NULL) {
338+
if (ssl_c != NULL) {
339+
wolfSSL_free(*ssl_c);
340+
*ssl_c = NULL;
341+
}
342+
if (ctx_s != NULL) {
343+
wolfSSL_CTX_free(*ctx_s);
344+
*ctx_s = NULL;
345+
}
346+
if (ctx_c != NULL) {
347+
wolfSSL_CTX_free(*ctx_c);
348+
*ctx_c = NULL;
349+
}
291350
return -1;
351+
}
292352
wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
293353
wolfSSL_SetIOReadCtx(*ssl_s, ctx);
294354
#if !defined(NO_DH)

wolfcrypt/src/pkcs7.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12659,8 +12659,13 @@ WOLFSSL_API int wc_PKCS7_DecodeEnvelopedData(wc_PKCS7* pkcs7, byte* in,
1265912659
case WC_PKCS7_ENV_5:
1266012660

1266112661
#ifndef NO_PKCS7_STREAM
12662+
1266212663
if ((ret = wc_PKCS7_AddDataToStream(pkcs7, in, inSz,
1266312664
pkcs7->stream->expected, &pkiMsg, &idx)) != 0) {
12665+
if (ret != WC_NO_ERR_TRACE(WC_PKCS7_WANT_READ_E)) {
12666+
wc_PKCS7_StreamGetVar(pkcs7, &encOID, NULL, NULL);
12667+
wc_PKCS7_DecryptContentFree(pkcs7, encOID, pkcs7->heap);
12668+
}
1266412669
return ret;
1266512670
}
1266612671

0 commit comments

Comments
 (0)