From bdd4a5b1f6a601163246f646b9c4131334a051a9 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 10:11:54 +0100 Subject: [PATCH 01/22] Cover both allocation failures in TLS DH parameter copying --- .../test_internal_nullguard_whitebox.c | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/unit-mcdc/test_internal_nullguard_whitebox.c b/tests/unit-mcdc/test_internal_nullguard_whitebox.c index ebc056e9e49..d9f7cba5f5c 100644 --- a/tests/unit-mcdc/test_internal_nullguard_whitebox.c +++ b/tests/unit-mcdc/test_internal_nullguard_whitebox.c @@ -61,6 +61,7 @@ #include #include +#include "mcdc_fault_alloc.h" #include #include @@ -95,6 +96,37 @@ static void wb_reset(WbFix* f) f->ssl->version.minor = TLSv1_2_MINOR; } +#if !defined(NO_DH) && !defined(MCDC_FA_UNAVAILABLE) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +static int wb_copy_dh_params(WbFix* f) +{ + byte prime = 23; + byte generator = 2; + int ret, firstError, secondError; + + wb_reset(f); + f->ctx->serverDH_P.buffer = ′ + f->ctx->serverDH_P.length = 1; + f->ctx->serverDH_G.buffer = &generator; + f->ctx->serverDH_G.length = 1; + + mcdc_fa_install(); + mcdc_fa_arm_only(1); + firstError = CopySSL_CTX_DhParams(f->ssl, f->ctx); + mcdc_fa_arm_only(2); + secondError = CopySSL_CTX_DhParams(f->ssl, f->ctx); + mcdc_fa_restore(); + + ret = CopySSL_CTX_DhParams(f->ssl, f->ctx); + if (f->ssl->buffers.weOwnDH) { + XFREE(f->ssl->buffers.serverDH_P.buffer, NULL, DYNAMIC_TYPE_PUBLIC_KEY); + XFREE(f->ssl->buffers.serverDH_G.buffer, NULL, DYNAMIC_TYPE_PUBLIC_KEY); + } + wb_reset(f); + return firstError == MEMORY_E && secondError == MEMORY_E && ret == 0; +} +#endif + /* ------------------------------------------------- SupportedHashSigAlgo * * `if (ssl == NULL || hashSigAlgo == NULL)` and, after WOLFSSL_SUITES(ssl), @@ -560,6 +592,12 @@ int main(void) wb_get_real_session_id(&f); #endif +#if !defined(NO_DH) && !defined(MCDC_FA_UNAVAILABLE) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + if (!wb_copy_dh_params(&f)) + puts("internal null-guard white-box: with failures (DH param copy)"); +#endif + printf("internal null-guard white-box: %d static-guard calls\n", g_calls); done: From 4139704ca541f8d72debb98f6d52748731aab23a Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 10:31:01 +0100 Subject: [PATCH 02/22] Exercise TLS ChaCha and Poly1305 key allocation failures --- tests/unit-mcdc/test_keys_whitebox.c | 62 +++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/tests/unit-mcdc/test_keys_whitebox.c b/tests/unit-mcdc/test_keys_whitebox.c index d9de3a20f0c..034a52e606c 100644 --- a/tests/unit-mcdc/test_keys_whitebox.c +++ b/tests/unit-mcdc/test_keys_whitebox.c @@ -38,13 +38,13 @@ * Rules, as for the sibling drivers: * - options.h FIRST, or the smoke build compiles this with the feature * macros undefined and it silently becomes a no-op that still exits 0. - * - main() ALWAYS returns 0; a non-zero exit discards the whole variant. * - Bail paths print, so "covered nothing" differs from "nothing to say". */ #include #include +#include "mcdc_fault_alloc.h" #include #include @@ -243,6 +243,57 @@ static void wb_set_auth_keys(void) #endif } +#if defined(HAVE_CHACHA) && !defined(MCDC_FA_UNAVAILABLE) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +/* Drive the static key setup helpers with each allocation failing in turn. */ +static int wb_key_allocation_failures(void) +{ + Ciphers enc, dec; + Keys keys; + CipherSpecs specs; + int ret, failed = 0; + unsigned int nth; + + XMEMSET(&keys, 0, sizeof(keys)); + XMEMSET(&specs, 0, sizeof(specs)); + specs.bulk_cipher_algorithm = wolfssl_chacha; + specs.key_size = CHACHA20_256_KEY_SIZE; + specs.iv_size = CHACHA20_IV_SIZE; + + for (nth = 1; nth <= 2; nth++) { + XMEMSET(&enc, 0, sizeof(enc)); + XMEMSET(&dec, 0, sizeof(dec)); + mcdc_fa_install(); + mcdc_fa_arm_only(nth); + ret = SetKeys(&enc, &dec, &keys, &specs, WOLFSSL_CLIENT_END, + NULL, INVALID_DEVID, NULL, 0); + mcdc_fa_restore(); + if (ret != MEMORY_E || dec.chacha != NULL || + (enc.chacha != NULL) != (nth == 2)) { + failed = 1; + } + XFREE(enc.chacha, NULL, DYNAMIC_TYPE_CIPHER); + XFREE(dec.chacha, NULL, DYNAMIC_TYPE_CIPHER); + } + +#if defined(HAVE_ONE_TIME_AUTH) && defined(HAVE_POLY1305) + { + OneTimeAuth auth; + XMEMSET(&auth, 0, sizeof(auth)); + mcdc_fa_install(); + mcdc_fa_arm_only(1); + ret = SetAuthKeys(&auth, &keys, &specs, NULL, INVALID_DEVID); + mcdc_fa_restore(); + if (ret != MEMORY_E || auth.poly1305 != NULL || auth.setup) + failed = 1; + XFREE(auth.poly1305, NULL, DYNAMIC_TYPE_CIPHER); + } +#endif + + return failed; +} +#endif + /* -------------------------------------------------------------- SetKeysSide * `ret == 0 && ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version)` and @@ -295,6 +346,7 @@ static void wb_set_keys_side(WOLFSSL* ssl, WOLFSSL_CTX* ctx) int main(void) { + int failed = 0; WOLFSSL_CTX* ctx = NULL; WOLFSSL* ssl = NULL; @@ -317,6 +369,12 @@ int main(void) wb_set_cipher_specs(ssl, ctx); wb_set_keys(); wb_set_auth_keys(); +#if defined(HAVE_CHACHA) && !defined(MCDC_FA_UNAVAILABLE) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + failed = wb_key_allocation_failures(); + if (failed) + puts("keys white-box: unexpected allocation failure behavior"); +#endif wb_set_keys_side(ssl, ctx); printf("keys white-box: %d vectors driven\n", g_checks); @@ -326,7 +384,7 @@ int main(void) if (ctx != NULL) wolfSSL_CTX_free(ctx); wolfSSL_Cleanup(); - return 0; /* always 0: a non-zero exit discards the variant */ + return failed; } #else From 9e078d4a95a8a2999de460b88c4ff07a55d2e3d2 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 10:33:53 +0100 Subject: [PATCH 03/22] Verify TLS DH parameter ownership across copies and failures --- .../test_internal_nullguard_whitebox.c | 64 ++++++++++++++++--- tests/unit-mcdc/test_keys_whitebox.c | 6 +- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/tests/unit-mcdc/test_internal_nullguard_whitebox.c b/tests/unit-mcdc/test_internal_nullguard_whitebox.c index d9f7cba5f5c..a235c08e5e1 100644 --- a/tests/unit-mcdc/test_internal_nullguard_whitebox.c +++ b/tests/unit-mcdc/test_internal_nullguard_whitebox.c @@ -102,7 +102,10 @@ static int wb_copy_dh_params(WbFix* f) { byte prime = 23; byte generator = 2; - int ret, firstError, secondError; + byte* oldPrime; + byte* oldGenerator; + int ret, failed = 0; + int nth; wb_reset(f); f->ctx->serverDH_P.buffer = ′ @@ -110,20 +113,65 @@ static int wb_copy_dh_params(WbFix* f) f->ctx->serverDH_G.buffer = &generator; f->ctx->serverDH_G.length = 1; - mcdc_fa_install(); - mcdc_fa_arm_only(1); - firstError = CopySSL_CTX_DhParams(f->ssl, f->ctx); - mcdc_fa_arm_only(2); - secondError = CopySSL_CTX_DhParams(f->ssl, f->ctx); - mcdc_fa_restore(); + ret = CopySSL_CTX_DhParams(f->ssl, f->ctx); + oldPrime = f->ssl->buffers.serverDH_P.buffer; + oldGenerator = f->ssl->buffers.serverDH_G.buffer; + if (ret != 0 || !f->ssl->buffers.weOwnDH || oldPrime == NULL || + oldGenerator == NULL || oldPrime == &prime || + oldGenerator == &generator || *oldPrime != prime || + *oldGenerator != generator) { + failed = 1; + goto cleanup; + } + + prime = 47; + generator = 5; + for (nth = 1; nth <= 2; nth++) { + mcdc_fa_install(); + mcdc_fa_arm_only(nth); + ret = CopySSL_CTX_DhParams(f->ssl, f->ctx); + mcdc_fa_restore(); + if (ret != MEMORY_E || !f->ssl->buffers.weOwnDH || + f->ssl->buffers.serverDH_P.buffer != oldPrime || + f->ssl->buffers.serverDH_G.buffer != oldGenerator || + *oldPrime != 23 || *oldGenerator != 2) { + failed = 1; + goto cleanup; + } + } + + /* A context with only one parameter must leave the owned copy intact. */ + f->ctx->serverDH_P.buffer = NULL; + ret = CopySSL_CTX_DhParams(f->ssl, f->ctx); + if (ret != 0 || f->ssl->buffers.serverDH_P.buffer != oldPrime || + f->ssl->buffers.serverDH_G.buffer != oldGenerator) + failed = 1; + f->ctx->serverDH_P.buffer = ′ + f->ctx->serverDH_G.buffer = NULL; + ret = CopySSL_CTX_DhParams(f->ssl, f->ctx); + if (ret != 0 || f->ssl->buffers.serverDH_P.buffer != oldPrime || + f->ssl->buffers.serverDH_G.buffer != oldGenerator) + failed = 1; + + f->ctx->serverDH_G.buffer = &generator; ret = CopySSL_CTX_DhParams(f->ssl, f->ctx); + if (ret != 0 || !f->ssl->buffers.weOwnDH || + f->ssl->buffers.serverDH_P.length != 1 || + f->ssl->buffers.serverDH_G.length != 1 || + f->ssl->buffers.serverDH_P.buffer == NULL || + f->ssl->buffers.serverDH_G.buffer == NULL || + *f->ssl->buffers.serverDH_P.buffer != prime || + *f->ssl->buffers.serverDH_G.buffer != generator) + failed = 1; + +cleanup: if (f->ssl->buffers.weOwnDH) { XFREE(f->ssl->buffers.serverDH_P.buffer, NULL, DYNAMIC_TYPE_PUBLIC_KEY); XFREE(f->ssl->buffers.serverDH_G.buffer, NULL, DYNAMIC_TYPE_PUBLIC_KEY); } wb_reset(f); - return firstError == MEMORY_E && secondError == MEMORY_E && ret == 0; + return !failed; } #endif diff --git a/tests/unit-mcdc/test_keys_whitebox.c b/tests/unit-mcdc/test_keys_whitebox.c index 034a52e606c..103b2d0e997 100644 --- a/tests/unit-mcdc/test_keys_whitebox.c +++ b/tests/unit-mcdc/test_keys_whitebox.c @@ -243,7 +243,8 @@ static void wb_set_auth_keys(void) #endif } -#if defined(HAVE_CHACHA) && !defined(MCDC_FA_UNAVAILABLE) && \ +#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && \ + !defined(NO_CHAPOL_AEAD) && !defined(MCDC_FA_UNAVAILABLE) && \ !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) /* Drive the static key setup helpers with each allocation failing in turn. */ static int wb_key_allocation_failures(void) @@ -369,7 +370,8 @@ int main(void) wb_set_cipher_specs(ssl, ctx); wb_set_keys(); wb_set_auth_keys(); -#if defined(HAVE_CHACHA) && !defined(MCDC_FA_UNAVAILABLE) && \ +#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && \ + !defined(NO_CHAPOL_AEAD) && !defined(MCDC_FA_UNAVAILABLE) && \ !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) failed = wb_key_allocation_failures(); if (failed) From 223c5d6616d4a80f74443c70c05a0db582aab807 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 10:38:14 +0100 Subject: [PATCH 04/22] Exercise Extended Master Secret key derivation decisions --- tests/unit-mcdc/test_keys_whitebox.c | 71 ++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/unit-mcdc/test_keys_whitebox.c b/tests/unit-mcdc/test_keys_whitebox.c index 103b2d0e997..7234351715b 100644 --- a/tests/unit-mcdc/test_keys_whitebox.c +++ b/tests/unit-mcdc/test_keys_whitebox.c @@ -343,6 +343,68 @@ static void wb_set_keys_side(WOLFSSL* ssl, WOLFSSL_CTX* ctx) } } +#if defined(HAVE_EXTENDED_MASTER) && !defined(WOLFSSL_NO_TLS12) && \ + !defined(NO_WOLFSSL_CLIENT) && !defined(NO_SHA256) && \ + defined(BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +static int wb_discard_alert(WOLFSSL* ssl, char* buf, int sz, void* ctx) +{ + (void)ssl; + (void)buf; + (void)ctx; + return sz; +} + +/* The handshake rejects missing required EMS before key derivation. */ +static int wb_master_secret_ems(void) +{ + static const struct { + byte required; + byte negotiated; + int expected; + } cases[] = { + {0, 0, 0}, + {1, 0, EXT_MASTER_SECRET_NEEDED_E}, + {1, 1, 0}, + }; + WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()); + int failed = 0; + size_t i; + + if (ctx == NULL) + return 1; + wolfSSL_CTX_SetIOSend(ctx, wb_discard_alert); + for (i = 0; i < XELEM_CNT(cases); i++) { + WOLFSSL* ssl = wolfSSL_new(ctx); + if (ssl == NULL) { + failed = 1; + break; + } + + ssl->options.cipherSuite0 = ECC_BYTE; + ssl->options.cipherSuite = + TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 & 0xff; + if (SetCipherSpecs(ssl) != 0 || ssl->arrays == NULL || + ssl->hsHashes == NULL) { + failed = 1; + wolfSSL_free(ssl); + break; + } + + ssl->options.requireEMS = cases[i].required; + ssl->options.haveEMS = cases[i].negotiated; + XMEMSET(ssl->arrays->preMasterSecret, 0x5a, SECRET_LEN); + ssl->arrays->preMasterSz = SECRET_LEN; + if (MakeMasterSecret(ssl) != cases[i].expected || + (cases[i].expected != 0 && ssl->arrays->preMasterSz != 0)) + failed = 1; + wolfSSL_free(ssl); + } + wolfSSL_CTX_free(ctx); + return failed; +} +#endif + /* ---------------------------------------------------------------- main */ int main(void) @@ -378,6 +440,15 @@ int main(void) puts("keys white-box: unexpected allocation failure behavior"); #endif wb_set_keys_side(ssl, ctx); +#if defined(HAVE_EXTENDED_MASTER) && !defined(WOLFSSL_NO_TLS12) && \ + !defined(NO_WOLFSSL_CLIENT) && !defined(NO_SHA256) && \ + defined(BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + if (wb_master_secret_ems()) { + puts("keys white-box: unexpected EMS key derivation behavior"); + failed = 1; + } +#endif printf("keys white-box: %d vectors driven\n", g_checks); From 62e486cfc8f5b51daa759bc34d5e4ea3b5c7f63b Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 10:39:40 +0100 Subject: [PATCH 05/22] Exercise TLS suite and handshake key ownership guards --- .../test_internal_nullguard_whitebox.c | 81 ++++++++++++++++++- 1 file changed, 77 insertions(+), 4 deletions(-) diff --git a/tests/unit-mcdc/test_internal_nullguard_whitebox.c b/tests/unit-mcdc/test_internal_nullguard_whitebox.c index a235c08e5e1..c6865091065 100644 --- a/tests/unit-mcdc/test_internal_nullguard_whitebox.c +++ b/tests/unit-mcdc/test_internal_nullguard_whitebox.c @@ -52,7 +52,6 @@ * Rules, as for the sibling drivers: * - options.h FIRST, or the smoke build compiles this with the feature * macros undefined and it silently becomes a no-op that still exits 0. - * - main() ALWAYS returns 0; a non-zero exit discards the whole variant. * - Each target is wrapped in the SAME preprocessor guard that encloses it * in internal.c, so this TU tracks the file across configurations instead * of failing to link under a narrower one. @@ -96,6 +95,66 @@ static void wb_reset(WbFix* f) f->ssl->version.minor = TLSv1_2_MINOR; } +/* AllocateSuites can copy the CTX list or start with an empty local list. */ +static int wb_allocate_suites(WbFix* f) +{ + int failed = 0; + int fixture; + + for (fixture = 0; fixture < 3; fixture++) { + int ret; + + wb_reset(f); + XMEMSET(f->suites, 0, sizeof(*f->suites)); + f->suites->suites[0] = ECC_BYTE; + f->suites->suiteSz = 2; + if (fixture == 0) + f->ssl->ctx = NULL; + if (fixture == 2) + f->ctx->suites = f->suites; + + ret = AllocateSuites(f->ssl); + if (ret != 0 || f->ssl->suites == NULL || + f->ssl->suites->suiteSz != (fixture == 2 ? 2 : 0) || + f->ssl->suites->suites[0] != (fixture == 2 ? ECC_BYTE : 0)) + failed = 1; + XFREE(f->ssl->suites, NULL, DYNAMIC_TYPE_SUITES); + } + wb_reset(f); + return failed; +} + +#if !defined(NO_RSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +static int wb_key_lifecycle(WbFix* f) +{ + void* key = NULL; + void* original; + int failed = 0; + + wb_reset(f); + if (AllocKey(NULL, DYNAMIC_TYPE_RSA, &key) != BAD_FUNC_ARG || + AllocKey(f->ssl, DYNAMIC_TYPE_RSA, NULL) != BAD_FUNC_ARG) + failed = 1; + + if (AllocKey(f->ssl, DYNAMIC_TYPE_RSA, &key) != 0 || key == NULL) + return 1; + original = key; + if (AllocKey(f->ssl, DYNAMIC_TYPE_RSA, &key) != BAD_STATE_E || + key != original) + failed = 1; + + FreeKey(NULL, DYNAMIC_TYPE_RSA, &key); + FreeKey(f->ssl, DYNAMIC_TYPE_RSA, NULL); + if (key != original) + failed = 1; + FreeKey(f->ssl, DYNAMIC_TYPE_RSA, &key); + if (key != NULL) + failed = 1; + FreeKey(f->ssl, DYNAMIC_TYPE_RSA, &key); + return failed; +} +#endif + #if !defined(NO_DH) && !defined(MCDC_FA_UNAVAILABLE) && \ !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) static int wb_copy_dh_params(WbFix* f) @@ -583,6 +642,7 @@ static void wb_get_real_session_id(WbFix* f) int main(void) { WbFix f; + int failed = 0; XMEMSET(&f, 0, sizeof(f)); @@ -642,8 +702,21 @@ int main(void) #if !defined(NO_DH) && !defined(MCDC_FA_UNAVAILABLE) && \ !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) - if (!wb_copy_dh_params(&f)) - puts("internal null-guard white-box: with failures (DH param copy)"); + if (!wb_copy_dh_params(&f)) { + puts("internal null-guard white-box: DH parameter copy failed"); + failed = 1; + } +#endif + + if (wb_allocate_suites(&f)) { + puts("internal null-guard white-box: suite allocation failed"); + failed = 1; + } +#if !defined(NO_RSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + if (wb_key_lifecycle(&f)) { + puts("internal null-guard white-box: key lifecycle check failed"); + failed = 1; + } #endif printf("internal null-guard white-box: %d static-guard calls\n", g_calls); @@ -657,7 +730,7 @@ int main(void) XFREE(f.ctx, NULL, DYNAMIC_TYPE_CTX); XFREE(f.ssl, NULL, DYNAMIC_TYPE_SSL); wolfSSL_Cleanup(); - return 0; /* always 0: a non-zero exit discards the variant */ + return failed; } #else From 1b1fe73fa19b735ea35b0386730c8661cdc6ea48 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 10:42:15 +0100 Subject: [PATCH 06/22] Exercise TLS reads after deterministic transport errors --- tests/api/test_tls.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ tests/api/test_tls.h | 2 ++ 2 files changed, 59 insertions(+) diff --git a/tests/api/test_tls.c b/tests/api/test_tls.c index cb19c6a0250..51d7c185eee 100644 --- a/tests/api/test_tls.c +++ b/tests/api/test_tls.c @@ -35,6 +35,63 @@ #include +#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \ + !defined(NO_TLS) && !defined(WOLFSSL_NO_TLS12) +static int test_tls_recv_transport_error(WOLFSSL* ssl, char* buf, int sz, + void* ctx) +{ + (void)ssl; + (void)buf; + (void)sz; + return *(const int*)ctx; +} +#endif + +int test_tls_read_transport_errors(void) +{ + EXPECT_DECLS; +#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \ + !defined(NO_TLS) && !defined(WOLFSSL_NO_TLS12) + static const struct { + int status; + int readResult; + int error; + } cases[] = { + {WOLFSSL_CBIO_ERR_GENERAL, WOLFSSL_FATAL_ERROR, SOCKET_ERROR_E}, + {WOLFSSL_CBIO_ERR_CONN_RST, 0, SOCKET_PEER_CLOSED_E}, + {WOLFSSL_CBIO_ERR_CONN_CLOSE, 0, SOCKET_PEER_CLOSED_E}, + }; + size_t i; + + for (i = 0; i < XELEM_CNT(cases); i++) { + WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL; + WOLFSSL *ssl_c = NULL, *ssl_s = NULL; + struct test_memio_ctx testCtx; + byte out; + int status = cases[i].status; + + XMEMSET(&testCtx, 0, sizeof(testCtx)); + ExpectIntEQ(test_memio_setup(&testCtx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + + if (EXPECT_SUCCESS()) { + wolfSSL_SSLSetIORecv(ssl_c, test_tls_recv_transport_error); + wolfSSL_SetIOReadCtx(ssl_c, &status); + ExpectIntEQ(wolfSSL_read(ssl_c, &out, sizeof(out)), + cases[i].readResult); + ExpectIntEQ(ssl_c->error, cases[i].error); + } + + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + } +#endif + return EXPECT_RESULT(); +} + int test_utils_memio_move_message(void) { EXPECT_DECLS; diff --git a/tests/api/test_tls.h b/tests/api/test_tls.h index b13fb039dab..d39b0ddaf6a 100644 --- a/tests/api/test_tls.h +++ b/tests/api/test_tls.h @@ -37,6 +37,7 @@ int test_tls_get_peer_tmp_key(void); int test_tls_get_negotiated_group(void); int test_tls_alert_info_cb(void); int test_tls_shutdown_in_init(void); +int test_tls_read_transport_errors(void); int test_tls_unknown_record_type_alert(void); int test_tls12_curve_intersection(void); int test_tls12_dhe_rsa_pss_sigalg(void); @@ -152,6 +153,7 @@ int test_tls_unset_fd_no_stdin_io(void); TEST_DECL_GROUP("tls", test_tls_get_peer_tmp_key), \ TEST_DECL_GROUP("tls", test_tls_get_negotiated_group), \ TEST_DECL_GROUP("tls", test_tls_alert_info_cb), \ + TEST_DECL_GROUP("tls", test_tls_read_transport_errors), \ TEST_DECL_GROUP("tls", test_tls_shutdown_in_init), \ TEST_DECL_GROUP("tls", test_tls_unknown_record_type_alert), \ TEST_DECL_GROUP("tls", test_tls12_curve_intersection), \ From 221e021c140e211097f7b8dcb0269584cd082365 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 10:50:32 +0100 Subject: [PATCH 07/22] Cover TLS protocol classifiers and fatal alert errors --- .../test_internal_nullguard_whitebox.c | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/tests/unit-mcdc/test_internal_nullguard_whitebox.c b/tests/unit-mcdc/test_internal_nullguard_whitebox.c index c6865091065..af60ae2b18e 100644 --- a/tests/unit-mcdc/test_internal_nullguard_whitebox.c +++ b/tests/unit-mcdc/test_internal_nullguard_whitebox.c @@ -95,6 +95,63 @@ static void wb_reset(WbFix* f) f->ssl->version.minor = TLSv1_2_MINOR; } +/* Protocol classifiers also see legacy version numbers supplied by callers. */ +static int wb_protocol_classifiers(WbFix* f) +{ + int failed = 0; + + wb_reset(f); + if (!IsTLS(f->ssl) || !IsTLS_ex(f->ssl->version) || + !IsAtLeastTLSv1_2(f->ssl)) + failed = 1; + f->ssl->version.minor = SSLv3_MINOR; + if (IsTLS(f->ssl) || IsTLS_ex(f->ssl->version) || + IsAtLeastTLSv1_2(f->ssl)) + failed = 1; +#ifdef WOLFSSL_DTLS + f->ssl->version.major = DTLS_MAJOR; + f->ssl->version.minor = DTLS_MINOR; + if (!IsTLS(f->ssl) || !IsTLS_ex(f->ssl->version) || + IsAtLeastTLSv1_2(f->ssl)) + failed = 1; + f->ssl->version.minor = DTLSv1_2_MINOR; + if (!IsAtLeastTLSv1_2(f->ssl)) + failed = 1; +#endif + wb_reset(f); + return failed; +} + +/* A finished fatal-alert path must never report a retriable error. */ +static int wb_fatal_alert_errors(WbFix* f) +{ + static const int retryErrors[] = { + 0, WANT_READ, WANT_WRITE, WOLFSSL_ERROR_WANT_READ, + WOLFSSL_ERROR_WANT_WRITE, ZERO_RETURN + }; + size_t i; + int failed = 0; + + wb_reset(f); + f->ssl->error = WANT_READ; + if (HandshakeEndedByFatalAlert(f->ssl) != 0 || f->ssl->error != WANT_READ) + failed = 1; + + f->ssl->options.fatalAlert = 1; + for (i = 0; i < XELEM_CNT(retryErrors); i++) { + f->ssl->error = retryErrors[i]; + if (HandshakeEndedByFatalAlert(f->ssl) != 1 || + f->ssl->error != SOCKET_PEER_CLOSED_E) + failed = 1; + } + f->ssl->error = SOCKET_ERROR_E; + if (HandshakeEndedByFatalAlert(f->ssl) != 1 || + f->ssl->error != SOCKET_ERROR_E) + failed = 1; + wb_reset(f); + return failed; +} + /* AllocateSuites can copy the CTX list or start with an empty local list. */ static int wb_allocate_suites(WbFix* f) { @@ -708,6 +765,14 @@ int main(void) } #endif + if (wb_protocol_classifiers(&f)) { + puts("internal null-guard white-box: protocol classification failed"); + failed = 1; + } + if (wb_fatal_alert_errors(&f)) { + puts("internal null-guard white-box: fatal alert error check failed"); + failed = 1; + } if (wb_allocate_suites(&f)) { puts("internal null-guard white-box: suite allocation failed"); failed = 1; From a80ed8a5855365194401823a98de2fb667191a37 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 10:59:54 +0100 Subject: [PATCH 08/22] Cover TLS signature constraints, hash copy and DTLS sequence limits --- .../test_internal_nullguard_whitebox.c | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/tests/unit-mcdc/test_internal_nullguard_whitebox.c b/tests/unit-mcdc/test_internal_nullguard_whitebox.c index af60ae2b18e..fbd0a023408 100644 --- a/tests/unit-mcdc/test_internal_nullguard_whitebox.c +++ b/tests/unit-mcdc/test_internal_nullguard_whitebox.c @@ -152,6 +152,90 @@ static int wb_fatal_alert_errors(WbFix* f) return failed; } +#if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_SHA256) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +static int wb_rsa_pss_constraints(WbFix* f) +{ + static const struct { + byte hash; + byte mgf; + word16 saltLen; + int result; + } cases[] = { + {0, 0, 0, 0}, + {WC_HASH_TYPE_SHA256, WC_MGF1SHA256, WC_SHA256_DIGEST_SIZE, 0}, + {WC_HASH_TYPE_SHA384, WC_MGF1SHA256, 0, INVALID_PARAMETER}, + {WC_HASH_TYPE_SHA256, WC_MGF1SHA384, 0, INVALID_PARAMETER}, + {WC_HASH_TYPE_SHA256, WC_MGF1SHA256, WC_SHA256_DIGEST_SIZE + 1, + INVALID_PARAMETER}, + }; + size_t i; + int failed = 0; + + wb_reset(f); + f->ssl->peerRsaPss = 1; + for (i = 0; i < XELEM_CNT(cases); i++) { + f->ssl->peerRsaPssHash = cases[i].hash; + f->ssl->peerRsaPssMgf = cases[i].mgf; + f->ssl->peerRsaPssSaltLen = cases[i].saltLen; + if (CheckPeerRsaSigAlgo(f->ssl, rsa_pss_pss_algo, sha256_mac) != + cases[i].result) + failed = 1; + } + wb_reset(f); + return failed; +} +#endif + +#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +static int wb_handshake_hash_copy(WbFix* f) +{ + HS_Hashes* copy = NULL; + int failed = 0; + + wb_reset(f); + if (InitHandshakeHashes(f->ssl) != 0) + return 1; + if (InitHandshakeHashesAndCopy(NULL, f->ssl->hsHashes, ©) != + BAD_FUNC_ARG) + failed = 1; + if (InitHandshakeHashesAndCopy(f->ssl, NULL, ©) != BAD_FUNC_ARG) + failed = 1; + if (InitHandshakeHashesAndCopy(f->ssl, f->ssl->hsHashes, NULL) != + BAD_FUNC_ARG) + failed = 1; + if (InitHandshakeHashesAndCopy(f->ssl, f->ssl->hsHashes, ©) != 0 || + copy == NULL || copy == f->ssl->hsHashes) + failed = 1; + if (copy != f->ssl->hsHashes) + Free_HS_Hashes(copy, f->ssl->heap); + Free_HS_Hashes(f->ssl->hsHashes, f->ssl->heap); + wb_reset(f); + return failed; +} +#endif + +#ifdef WOLFSSL_DTLS +static int wb_dtls_prev_sequence_limit(WbFix* f) +{ + int failed = 0; + + wb_reset(f); + f->ssl->keys.dtls_prev_sequence_number_hi = 0xfffe; + f->ssl->keys.dtls_prev_sequence_number_lo = 0xffffffffU; + if (DtlsSEQAtMax(f->ssl, PREV_ORDER)) + failed = 1; + f->ssl->keys.dtls_prev_sequence_number_hi = 0xffff; + if (!DtlsSEQAtMax(f->ssl, PREV_ORDER)) + failed = 1; + f->ssl->keys.dtls_prev_sequence_number_lo = 0xfffffffeU; + if (DtlsSEQAtMax(f->ssl, PREV_ORDER)) + failed = 1; + wb_reset(f); + return failed; +} +#endif + /* AllocateSuites can copy the CTX list or start with an empty local list. */ static int wb_allocate_suites(WbFix* f) { @@ -773,6 +857,25 @@ int main(void) puts("internal null-guard white-box: fatal alert error check failed"); failed = 1; } +#if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_SHA256) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + if (wb_rsa_pss_constraints(&f)) { + puts("internal null-guard white-box: RSA-PSS constraint check failed"); + failed = 1; + } +#endif +#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + if (wb_handshake_hash_copy(&f)) { + puts("internal null-guard white-box: handshake hash copy failed"); + failed = 1; + } +#endif +#ifdef WOLFSSL_DTLS + if (wb_dtls_prev_sequence_limit(&f)) { + puts("internal null-guard white-box: DTLS sequence limit check failed"); + failed = 1; + } +#endif if (wb_allocate_suites(&f)) { puts("internal null-guard white-box: suite allocation failed"); failed = 1; From 9fbcca58bfee4efe6db2234b141e01237ffc65c6 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 11:05:36 +0100 Subject: [PATCH 09/22] Cover TLS hash output guards and retained transcript copies --- .../test_internal_nullguard_whitebox.c | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/unit-mcdc/test_internal_nullguard_whitebox.c b/tests/unit-mcdc/test_internal_nullguard_whitebox.c index fbd0a023408..7e01e97037a 100644 --- a/tests/unit-mcdc/test_internal_nullguard_whitebox.c +++ b/tests/unit-mcdc/test_internal_nullguard_whitebox.c @@ -190,12 +190,20 @@ static int wb_rsa_pss_constraints(WbFix* f) #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) static int wb_handshake_hash_copy(WbFix* f) { + static const byte record[RECORD_HEADER_SZ + 1] = {0}; HS_Hashes* copy = NULL; int failed = 0; wb_reset(f); + if (HashOutput(f->ssl, record, sizeof(record), 0) != BAD_FUNC_ARG) + failed = 1; if (InitHandshakeHashes(f->ssl) != 0) return 1; + if (HashOutput(f->ssl, NULL, sizeof(record), 0) != BAD_FUNC_ARG) + failed = 1; + if (HashOutput(f->ssl, record, sizeof(record), 0) != 0) + failed = 1; + if (InitHandshakeHashesAndCopy(NULL, f->ssl->hsHashes, ©) != BAD_FUNC_ARG) failed = 1; @@ -207,6 +215,18 @@ static int wb_handshake_hash_copy(WbFix* f) if (InitHandshakeHashesAndCopy(f->ssl, f->ssl->hsHashes, ©) != 0 || copy == NULL || copy == f->ssl->hsHashes) failed = 1; +#if !defined(WOLFSSL_NO_CLIENT_AUTH) && \ + ((defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)) || \ + (defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \ + (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH))) + f->ssl->options.cacheMessages = 1; + if (HashOutput(f->ssl, record, sizeof(record), 0) != 0 || + InitHandshakeHashesAndCopy(f->ssl, f->ssl->hsHashes, ©) != 0 || + copy == NULL || copy->messages == NULL || + copy->messages == f->ssl->hsHashes->messages || + copy->length != 1 || copy->messages[0] != record[RECORD_HEADER_SZ]) + failed = 1; +#endif if (copy != f->ssl->hsHashes) Free_HS_Hashes(copy, f->ssl->heap); Free_HS_Hashes(f->ssl->hsHashes, f->ssl->heap); From 05664da152ea1fd2e03a0f75b93fc5f9a5ab8740 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 11:11:09 +0100 Subject: [PATCH 10/22] Cover TLS signature scheme encoding and DTLS message lists --- .../test_internal_nullguard_whitebox.c | 70 ++++++++++++++++++- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/tests/unit-mcdc/test_internal_nullguard_whitebox.c b/tests/unit-mcdc/test_internal_nullguard_whitebox.c index 7e01e97037a..b375d41f11d 100644 --- a/tests/unit-mcdc/test_internal_nullguard_whitebox.c +++ b/tests/unit-mcdc/test_internal_nullguard_whitebox.c @@ -154,6 +154,35 @@ static int wb_fatal_alert_errors(WbFix* f) #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_SHA256) && \ !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +static int wb_rsa_pss_scheme_encoding(void) +{ + byte encoded[4] = {0}; + byte hash = 0; + byte sig = 0; + word16 len = 0; + + AddSuiteHashSigAlgo(encoded, sha_mac, rsa_pss_pss_algo, 0, &len); + AddSuiteHashSigAlgo(encoded, sha512_mac + 1, rsa_pss_pss_algo, 0, &len); + if (len != 0) + return 1; + AddSuiteHashSigAlgo(encoded, sha256_mac, rsa_pss_pss_algo, 0, &len); + if (len != 2 || encoded[0] != rsa_pss_sa_algo || + encoded[1] != pss_sha256) + return 1; + DecodeSigAlg(encoded, &hash, &sig); + if (hash != sha256_mac || sig != rsa_pss_pss_algo) + return 1; + + /* Decode an assigned later scheme when its optional support is absent. */ +#ifndef HAVE_ECC_BRAINPOOL + encoded[1] = ECDSA_BRAINPOOLP256R1TLS13_SHA256_MINOR; + DecodeSigAlg(encoded, &hash, &sig); + if (sig == rsa_pss_pss_algo) + return 1; +#endif + return 0; +} + static int wb_rsa_pss_constraints(WbFix* f) { static const struct { @@ -236,6 +265,41 @@ static int wb_handshake_hash_copy(WbFix* f) #endif #ifdef WOLFSSL_DTLS +static int wb_dtls_message_list(void) +{ + DtlsMsg first, middle, last, absent; + DtlsMsg* head; + int failed = 0; + + XMEMSET(&first, 0, sizeof(first)); + XMEMSET(&middle, 0, sizeof(middle)); + XMEMSET(&last, 0, sizeof(last)); + XMEMSET(&absent, 0, sizeof(absent)); + first.seq = 2; + middle.seq = 4; + last.seq = 6; + absent.seq = 8; + + head = DtlsMsgInsert(NULL, &last); + head = DtlsMsgInsert(head, &first); + head = DtlsMsgInsert(head, &middle); + if (head != &first || first.next != &middle || middle.next != &last) + failed = 1; + + head = DtlsMsgListUnlink(head, &absent); + if (head != &first || first.next != &middle || middle.next != &last) + failed = 1; + head = DtlsMsgListUnlink(head, &middle); + if (head != &first || first.next != &last || middle.next != NULL) + failed = 1; + head = DtlsMsgListUnlink(head, &first); + if (head != &last || first.next != NULL) + failed = 1; + if (DtlsMsgListUnlink(head, &last) != NULL) + failed = 1; + return failed; +} + static int wb_dtls_prev_sequence_limit(WbFix* f) { int failed = 0; @@ -879,7 +943,7 @@ int main(void) } #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_SHA256) && \ !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) - if (wb_rsa_pss_constraints(&f)) { + if (wb_rsa_pss_scheme_encoding() || wb_rsa_pss_constraints(&f)) { puts("internal null-guard white-box: RSA-PSS constraint check failed"); failed = 1; } @@ -891,8 +955,8 @@ int main(void) } #endif #ifdef WOLFSSL_DTLS - if (wb_dtls_prev_sequence_limit(&f)) { - puts("internal null-guard white-box: DTLS sequence limit check failed"); + if (wb_dtls_message_list() || wb_dtls_prev_sequence_limit(&f)) { + puts("internal null-guard white-box: DTLS message bookkeeping failed"); failed = 1; } #endif From 28f469ae5f4a2e0285e692322258d96010e4a921 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 11:17:33 +0100 Subject: [PATCH 11/22] Exercise TLS FFDHE helper argument and capacity checks --- .../test_internal_nullguard_whitebox.c | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/tests/unit-mcdc/test_internal_nullguard_whitebox.c b/tests/unit-mcdc/test_internal_nullguard_whitebox.c index b375d41f11d..7df647d60ca 100644 --- a/tests/unit-mcdc/test_internal_nullguard_whitebox.c +++ b/tests/unit-mcdc/test_internal_nullguard_whitebox.c @@ -320,6 +320,70 @@ static int wb_dtls_prev_sequence_limit(WbFix* f) } #endif +#if !defined(NO_DH) && defined(HAVE_FFDHE_2048) && \ + (!defined(NO_CERTS) || !defined(NO_PSK)) && !defined(WOLFSSL_ASYNC_CRYPT) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +static int wb_dh_key_exchange(WbFix* f) +{ + DhKey key; + WC_RNG rng; + byte prime[256], priv[256], pub[256], secret[256]; + word32 privSz = sizeof(priv), pubSz = sizeof(pub); + word32 secretSz = sizeof(secret); + int failed = 0; + + wb_reset(f); + if (wc_InitDhKey(&key) != 0) + return 1; + if (wc_InitRng(&rng) != 0) { + wc_FreeDhKey(&key); + return 1; + } + f->ssl->rng = &rng; + if (wc_DhSetNamedKey(&key, WC_FFDHE_2048) != 0 || + mp_unsigned_bin_size(&key.p) != sizeof(prime) || + mp_to_unsigned_bin(&key.p, prime) != MP_OKAY) { + failed = 1; + goto done; + } + + if (DhGenKeyPair(NULL, &key, priv, &privSz, pub, &pubSz) != BAD_FUNC_ARG) + failed = 1; + if (DhGenKeyPair(f->ssl, NULL, priv, &privSz, pub, &pubSz) != BAD_FUNC_ARG) + failed = 1; + if (DhGenKeyPair(f->ssl, &key, priv, &privSz, pub, &pubSz) != 0) { + failed = 1; + goto done; + } + + if (DhAgree(f->ssl, &key, priv, privSz, pub, pubSz, secret, &secretSz, + prime, sizeof(prime)) != 0 || secretSz == 0) + failed = 1; + secretSz = sizeof(secret); + if (DhAgree(f->ssl, &key, priv, privSz, pub, pubSz, secret, &secretSz, + prime, 0) != 0 || secretSz == 0) + failed = 1; + if (DhAgree(f->ssl, NULL, priv, privSz, pub, pubSz, secret, &secretSz, + prime, sizeof(prime)) != BAD_FUNC_ARG) + failed = 1; + if (DhAgree(f->ssl, &key, priv, privSz, pub, pubSz, secret, NULL, + prime, sizeof(prime)) != BAD_FUNC_ARG) + failed = 1; + secretSz = 1; + if (DhAgree(f->ssl, &key, priv, privSz, pub, pubSz, secret, &secretSz, + prime, sizeof(prime)) != BUFFER_E) + failed = 1; + +done: + ForceZero(priv, sizeof(priv)); + ForceZero(secret, sizeof(secret)); + wc_FreeRng(&rng); + wc_FreeDhKey(&key); + wb_reset(f); + return failed; +} +#endif + /* AllocateSuites can copy the CTX list or start with an empty local list. */ static int wb_allocate_suites(WbFix* f) { @@ -959,6 +1023,14 @@ int main(void) puts("internal null-guard white-box: DTLS message bookkeeping failed"); failed = 1; } +#endif +#if !defined(NO_DH) && defined(HAVE_FFDHE_2048) && \ + (!defined(NO_CERTS) || !defined(NO_PSK)) && !defined(WOLFSSL_ASYNC_CRYPT) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + if (wb_dh_key_exchange(&f)) { + puts("internal null-guard white-box: DH key exchange check failed"); + failed = 1; + } #endif if (wb_allocate_suites(&f)) { puts("internal null-guard white-box: suite allocation failed"); From 6ba74553307317f86f4724be20d36fd977493d11 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 11:20:13 +0100 Subject: [PATCH 12/22] Cover TLS ECC signature digest length bounds --- .../test_internal_nullguard_whitebox.c | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/unit-mcdc/test_internal_nullguard_whitebox.c b/tests/unit-mcdc/test_internal_nullguard_whitebox.c index 7df647d60ca..d96f6e40dda 100644 --- a/tests/unit-mcdc/test_internal_nullguard_whitebox.c +++ b/tests/unit-mcdc/test_internal_nullguard_whitebox.c @@ -384,6 +384,47 @@ static int wb_dh_key_exchange(WbFix* f) } #endif +#if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY) && \ + !defined(NO_ECC256) && !defined(WOLFSSL_ASYNC_CRYPT) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +static int wb_ecc_verify_digest_limits(WbFix* f) +{ + ecc_key key; + WC_RNG rng; + byte digest[WC_MAX_DIGEST_SIZE + 1]; + byte sig[ECC_MAX_SIG_SIZE]; + word32 sigSz = sizeof(sig); + int failed = 0; + + wb_reset(f); + if (wc_ecc_init(&key) != 0) + return 1; + if (wc_InitRng(&rng) != 0) { + wc_ecc_free(&key); + return 1; + } + XMEMSET(digest, 0x24, sizeof(digest)); + if (wc_ecc_make_key_ex(&rng, 32, &key, ECC_SECP256R1) != 0 || + wc_ecc_sign_hash(digest, 32, sig, &sigSz, &rng, &key) != 0) { + failed = 1; + goto done; + } + if (EccVerify(f->ssl, sig, sigSz, digest, sizeof(digest), &key, NULL) != + BAD_LENGTH_E) + failed = 1; + if (EccVerify(f->ssl, sig, sigSz, digest, 0, &key, NULL) != BAD_LENGTH_E) + failed = 1; + if (EccVerify(f->ssl, sig, sigSz, digest, 32, &key, NULL) != 0) + failed = 1; + +done: + wc_FreeRng(&rng); + wc_ecc_free(&key); + wb_reset(f); + return failed; +} +#endif + /* AllocateSuites can copy the CTX list or start with an empty local list. */ static int wb_allocate_suites(WbFix* f) { @@ -1031,6 +1072,14 @@ int main(void) puts("internal null-guard white-box: DH key exchange check failed"); failed = 1; } +#endif +#if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY) && \ + !defined(NO_ECC256) && !defined(WOLFSSL_ASYNC_CRYPT) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + if (wb_ecc_verify_digest_limits(&f)) { + puts("internal null-guard white-box: ECC digest limit check failed"); + failed = 1; + } #endif if (wb_allocate_suites(&f)) { puts("internal null-guard white-box: suite allocation failed"); From 03e493b4d7fc2d2da95cf35a5dc73da3dc5bfa7a Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 11:35:34 +0100 Subject: [PATCH 13/22] Exercise cleanup ownership for X25519 and X448 keys --- .../test_internal_nullguard_whitebox.c | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/tests/unit-mcdc/test_internal_nullguard_whitebox.c b/tests/unit-mcdc/test_internal_nullguard_whitebox.c index d96f6e40dda..4eb0f6d7ba1 100644 --- a/tests/unit-mcdc/test_internal_nullguard_whitebox.c +++ b/tests/unit-mcdc/test_internal_nullguard_whitebox.c @@ -425,6 +425,83 @@ static int wb_ecc_verify_digest_limits(WbFix* f) } #endif +#if defined(HAVE_ECC) && \ + (defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) && \ + !defined(NO_WOLFSSL_CLIENT) && !defined(HAVE_FIPS) && \ + !defined(HAVE_SELFTEST) +static int wb_curve_key_cleanup(void) +{ + static const int keyTypes[] = { + DYNAMIC_TYPE_ECC, +#ifdef HAVE_CURVE25519 + DYNAMIC_TYPE_CURVE25519, +#endif +#ifdef HAVE_CURVE448 + DYNAMIC_TYPE_CURVE448, +#endif + }; + WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()); + size_t i; + int pass, withPeer; + int failed = 0; + + if (ctx == NULL) + return 1; + for (pass = 0; pass < 2; pass++) { + for (i = 0; i < XELEM_CNT(keyTypes); i++) { + for (withPeer = 0; withPeer <= (i != 0); withPeer++) { + int type = keyTypes[i]; + WOLFSSL* ssl = wolfSSL_new(ctx); + int ret; + + if (ssl == NULL) { + failed = 1; + continue; + } + ret = AllocKey(ssl, type, (void**)&ssl->eccTempKey); + if (ret == 0) + ssl->eccTempKeyPresent = (byte)type; +#ifdef HAVE_CURVE25519 + if (ret == 0 && withPeer && type == DYNAMIC_TYPE_CURVE25519) { + ret = AllocKey(ssl, type, (void**)&ssl->peerX25519Key); + ssl->peerX25519KeyPresent = (ret == 0); + } +#endif +#ifdef HAVE_CURVE448 + if (ret == 0 && withPeer && type == DYNAMIC_TYPE_CURVE448) { + ret = AllocKey(ssl, type, (void**)&ssl->peerX448Key); + ssl->peerX448KeyPresent = (ret == 0); + } +#endif + if (ret == 0 && withPeer) { + /* A peer key also identifies the type of an owned + * temporary key when only the peer flag remains. */ + ssl->eccTempKeyPresent = 0; + } + if (pass == 0) + FreeSSL_EccPeerKeys(ssl); + else + FreeHandshakeResources(ssl); + if (ret != 0 || ssl->eccTempKey != NULL || + ssl->eccTempKeyPresent) + failed = 1; +#ifdef HAVE_CURVE25519 + if (ssl->peerX25519Key != NULL || ssl->peerX25519KeyPresent) + failed = 1; +#endif +#ifdef HAVE_CURVE448 + if (ssl->peerX448Key != NULL || ssl->peerX448KeyPresent) + failed = 1; +#endif + wolfSSL_free(ssl); + } + } + } + wolfSSL_CTX_free(ctx); + return failed; +} +#endif + /* AllocateSuites can copy the CTX list or start with an empty local list. */ static int wb_allocate_suites(WbFix* f) { @@ -1080,6 +1157,15 @@ int main(void) puts("internal null-guard white-box: ECC digest limit check failed"); failed = 1; } +#endif +#if defined(HAVE_ECC) && \ + (defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) && \ + !defined(NO_WOLFSSL_CLIENT) && !defined(HAVE_FIPS) && \ + !defined(HAVE_SELFTEST) + if (wb_curve_key_cleanup()) { + puts("internal null-guard white-box: curve key cleanup failed"); + failed = 1; + } #endif if (wb_allocate_suites(&f)) { puts("internal null-guard white-box: suite allocation failed"); From 959fd910c5f072054ef413223840a22d5ebd34b4 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 11:47:36 +0100 Subject: [PATCH 14/22] Cover TLS hostname and wildcard policy decisions --- .../unit-mcdc/test_internal_domain_whitebox.c | 51 ++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/tests/unit-mcdc/test_internal_domain_whitebox.c b/tests/unit-mcdc/test_internal_domain_whitebox.c index d77f5c1d032..6d8270bf93c 100644 --- a/tests/unit-mcdc/test_internal_domain_whitebox.c +++ b/tests/unit-mcdc/test_internal_domain_whitebox.c @@ -40,7 +40,6 @@ * Rules, as for the sibling drivers: * - options.h FIRST, or the smoke build compiles this with the feature * macros undefined and it becomes a silent no-op that still exits 0. - * - main() ALWAYS returns 0; a non-zero exit discards the whole variant. */ #include @@ -69,11 +68,55 @@ static void wb_match(const char* pattern, int patternLen, (void)MatchDomainName(pattern, patternLen, str, strLen, flags); } +static int wb_domain_policy(void) +{ + static const struct { + const char* name; + int expected; + } labels[] = { + {"xn--demo", 1}, {"XN--demo", 1}, {"on--demo", 0}, + {"xa--demo", 0}, {"xn_a-demo", 0}, {"xn-_demo", 0}, {"xn", 0} + }, patterns[] = { + {"xn--demo.example.com", 0}, {"x*.example.com", 0}, + {"xn--d*.example.com", 1}, {"example.xn--d*", 1}, + {"example.d*", 0}, {"example.xn--demo", 0} + }; + size_t i; + int failed = 0; + + for (i = 0; i < XELEM_CNT(labels); i++) { + if (LabelIsALabel(labels[i].name, (word32)XSTRLEN(labels[i].name)) != + labels[i].expected) + failed = 1; + } + for (i = 0; i < XELEM_CNT(patterns); i++) { + if (PatternHasWildcardInALabel(patterns[i].name, + (word32)XSTRLEN(patterns[i].name)) != patterns[i].expected) + failed = 1; + } + if (!WildcardPlacementOK("*.example.com", 13, + WOLFSSL_NO_PARTIAL_WILDCARDS) || + WildcardPlacementOK("*.com", 5, WOLFSSL_NO_PARTIAL_WILDCARDS) || + WildcardPlacementOK("*", 1, WOLFSSL_NO_PARTIAL_WILDCARDS) || + WildcardPlacementOK("ww*.example.com", 15, + WOLFSSL_NO_PARTIAL_WILDCARDS)) + failed = 1; + + /* In leftmost-only mode, single label names compare byte-for-byte. */ + if (!MatchDomainName("localhost", 9, "localhost", 9, WB_LEFTMOST_FLAG)) + failed = 1; + if (WB_LEFTMOST_FLAG != 0 && + MatchDomainName("localhost", 9, "LOCALHOST", 9, WB_LEFTMOST_FLAG)) + failed = 1; + return failed; +} + int main(void) { static const char kHost[] = "www.example.com"; static const char kWild[] = "*.example.com"; size_t i; + int failed = 0; unsigned int flagSets[2]; if (wolfSSL_Init() != WOLFSSL_SUCCESS) { @@ -146,11 +189,15 @@ int main(void) wb_match("*.example.com", 13, "no-dots-here", 12, fl, "no dots"); } + failed = wb_domain_policy(); + if (failed) + puts("internal domain white-box: unexpected domain policy result"); + printf("internal domain white-box: %d vectors driven\n", g_checks); done: wolfSSL_Cleanup(); - return 0; /* always 0: a non-zero exit discards the variant */ + return failed; } #else From 6d15a9d6a6d35802d285a3a34c9e937304882ff9 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 11:51:04 +0100 Subject: [PATCH 15/22] Exercise TLS suite credentials and context ownership checks --- .../test_internal_nullguard_whitebox.c | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/tests/unit-mcdc/test_internal_nullguard_whitebox.c b/tests/unit-mcdc/test_internal_nullguard_whitebox.c index 4eb0f6d7ba1..ca7fd69b8e3 100644 --- a/tests/unit-mcdc/test_internal_nullguard_whitebox.c +++ b/tests/unit-mcdc/test_internal_nullguard_whitebox.c @@ -502,6 +502,100 @@ static int wb_curve_key_cleanup(void) } #endif +#if !defined(NO_CERTS) && !defined(WOLFSSL_SESSION_EXPORT) +static int wb_suite_credentials(WbFix* f) +{ + DerBuffer cert, key; + byte fixture = 0; + int failed = 0; + + wb_reset(f); + XMEMSET(&cert, 0, sizeof(cert)); + XMEMSET(&key, 0, sizeof(key)); + f->ssl->options.side = WOLFSSL_SERVER_END; + + if (InitSSL_Suites(f->ssl) != NO_PRIVATE_KEY) + failed = 1; + f->ssl->buffers.certificate = &cert; + if (InitSSL_Suites(f->ssl) != NO_PRIVATE_KEY) + failed = 1; + + cert.buffer = &fixture; + if (InitSSL_Suites(f->ssl) != NO_PRIVATE_KEY) + failed = 1; + f->ssl->buffers.key = &key; + if (InitSSL_Suites(f->ssl) != NO_PRIVATE_KEY) + failed = 1; + + key.buffer = &fixture; + if (InitSSL_Suites(f->ssl) != WOLFSSL_SUCCESS) + failed = 1; + + f->ssl->buffers.certificate = NULL; + f->ssl->buffers.key = NULL; +#ifdef HAVE_ANON + f->ssl->options.useAnon = 1; + if (InitSSL_Suites(f->ssl) != WOLFSSL_SUCCESS) + failed = 1; + f->ssl->options.useAnon = 0; +#endif +#ifndef NO_PSK + f->ssl->options.havePSK = 1; + if (InitSSL_Suites(f->ssl) != WOLFSSL_SUCCESS) + failed = 1; +#endif + wb_reset(f); + return failed; +} +#endif + +#if !defined(NO_CERTS) && (defined(HAVE_SNI) || defined(WOLFSSL_CERT_SETUP_CB)) +static int wb_context_credential_ownership(WbFix* f) +{ + DerBuffer cert, chain, key; + int failed = 0; + + XMEMSET(&cert, 0, sizeof(cert)); + XMEMSET(&chain, 0, sizeof(chain)); + XMEMSET(&key, 0, sizeof(key)); + wb_reset(f); + f->ssl->buffers.certificate = f->ctx->certificate = &cert; + f->ssl->buffers.certChain = f->ctx->certChain = &chain; + f->ssl->buffers.key = f->ctx->privateKey = &key; + f->ssl->buffers.certChainCnt = f->ctx->certChainCnt = 1; + if (CheckSSL_CTX_CertsAndKeys(f->ssl) != 0) + failed = 1; + + f->ctx->certificate = NULL; + if (CheckSSL_CTX_CertsAndKeys(f->ssl) != BAD_STATE_E) + failed = 1; + f->ssl->buffers.weOwnCert = 1; + if (CheckSSL_CTX_CertsAndKeys(f->ssl) != 0) + failed = 1; + + f->ctx->certChain = NULL; + if (CheckSSL_CTX_CertsAndKeys(f->ssl) != BAD_STATE_E) + failed = 1; + f->ctx->certChain = &chain; + f->ctx->certChainCnt = 2; + if (CheckSSL_CTX_CertsAndKeys(f->ssl) != BAD_STATE_E) + failed = 1; + f->ssl->buffers.weOwnCertChain = 1; + if (CheckSSL_CTX_CertsAndKeys(f->ssl) != 0) + failed = 1; + + f->ctx->privateKey = NULL; + if (CheckSSL_CTX_CertsAndKeys(f->ssl) != BAD_STATE_E) + failed = 1; + f->ssl->buffers.weOwnKey = 1; + if (CheckSSL_CTX_CertsAndKeys(f->ssl) != 0) + failed = 1; + + wb_reset(f); + return failed; +} +#endif + /* AllocateSuites can copy the CTX list or start with an empty local list. */ static int wb_allocate_suites(WbFix* f) { @@ -1166,6 +1260,18 @@ int main(void) puts("internal null-guard white-box: curve key cleanup failed"); failed = 1; } +#endif +#if !defined(NO_CERTS) && !defined(WOLFSSL_SESSION_EXPORT) + if (wb_suite_credentials(&f)) { + puts("internal null-guard white-box: suite credential check failed"); + failed = 1; + } +#endif +#if !defined(NO_CERTS) && (defined(HAVE_SNI) || defined(WOLFSSL_CERT_SETUP_CB)) + if (wb_context_credential_ownership(&f)) { + puts("internal null-guard white-box: context ownership check failed"); + failed = 1; + } #endif if (wb_allocate_suites(&f)) { puts("internal null-guard white-box: suite allocation failed"); From 5e047741dbf2c22244433bc7582072d57066aae0 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 12:09:27 +0100 Subject: [PATCH 16/22] Exercise TLS RSA signature algorithm dispatch --- .../test_internal_cryptocb_whitebox.c | 84 ++++++++++++++++++- 1 file changed, 81 insertions(+), 3 deletions(-) diff --git a/tests/unit-mcdc/test_internal_cryptocb_whitebox.c b/tests/unit-mcdc/test_internal_cryptocb_whitebox.c index 604087fc519..b75bc83a9da 100644 --- a/tests/unit-mcdc/test_internal_cryptocb_whitebox.c +++ b/tests/unit-mcdc/test_internal_cryptocb_whitebox.c @@ -70,7 +70,7 @@ * Rules, as for the sibling drivers: * - options.h FIRST, or the smoke build compiles this with the feature * macros undefined and it silently becomes a no-op that still exits 0. - * - main() ALWAYS returns 0; a non-zero exit discards the whole variant. + * - The RSA round trips check the signature and verification results. */ #include @@ -114,6 +114,9 @@ #endif #include +#if defined(MCDC_CRYPTOCB_WB) && !defined(NO_RSA) + #undef wc_RsaSSL_VerifyInline +#endif #include #include @@ -301,7 +304,8 @@ static byte* g_rsaOut; static int mcdc_rsa_verify_inline(byte* in, word32 inLen, byte** out, RsaKey* key) { - (void)in; (void)inLen; (void)key; + if (key != NULL) + return wc_RsaSSL_VerifyInline(in, inLen, out, key); if (out != NULL) *out = g_rsaOut; @@ -380,8 +384,72 @@ static void wb_rsa_null_args(WOLFSSL* ssl) } #endif +#if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_SHA256) && \ + !defined(NO_WOLFSSL_SERVER) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ + !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(HAVE_FIPS) && \ + !defined(HAVE_SELFTEST) +#ifndef USE_CERT_BUFFERS_2048 + #define USE_CERT_BUFFERS_2048 +#endif +#include +#endif + +#if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_SHA256) && \ + !defined(NO_WOLFSSL_SERVER) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ + !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(HAVE_FIPS) && \ + !defined(HAVE_SELFTEST) +static int wb_rsa_pss(WOLFSSL_CTX* ctx) +{ + static const int algorithms[] = { + rsa_sa_algo, rsa_pss_sa_algo, rsa_pss_pss_algo + }; + WOLFSSL* ssl = wolfSSL_new(ctx); + RsaKey key; + byte digest[WC_SHA256_DIGEST_SIZE]; + byte signature[256]; + byte verify[sizeof(signature)]; + word32 idx = 0; + size_t i; + int initialized = 0; + int failed = 1; + + if (ssl == NULL || wc_InitRsaKey(&key, NULL) != 0) + goto done; + initialized = 1; + if (wc_RsaPrivateKeyDecode(client_key_der_2048, &idx, &key, + sizeof_client_key_der_2048) != 0) + goto done; + XMEMSET(digest, 0x31, sizeof(digest)); + for (i = 0; i < sizeof(algorithms) / sizeof(algorithms[0]); i++) { + int algo = algorithms[i]; + word32 sigLen = sizeof(signature); + byte* recovered = NULL; + + if (RsaSign(ssl, digest, sizeof(digest), signature, &sigLen, algo, + sha256_mac, &key, NULL) != 0 || sigLen != sizeof(signature)) + goto done; + XMEMCPY(verify, signature, sigLen); + if (VerifyRsaSign(ssl, verify, sigLen, digest, sizeof(digest), algo, + sha256_mac, &key, NULL) != 0) + goto done; + XMEMCPY(verify, signature, sigLen); + if (RsaVerify(ssl, verify, sigLen, &recovered, algo, sha256_mac, + &key, NULL) <= 0 || recovered == NULL) + goto done; + } + failed = 0; + +done: + if (initialized) + wc_FreeRsaKey(&key); + wolfSSL_free(ssl); + return failed; +} +#endif + int main(void) { + int failed = 0; WOLFSSL_CTX* ctx = NULL; WOLFSSL* ssl = NULL; #ifdef HAVE_ECC @@ -433,6 +501,16 @@ int main(void) wb_rsa_recovered_plain(ssl); #endif +#if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_SHA256) && \ + !defined(NO_WOLFSSL_SERVER) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ + !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(HAVE_FIPS) && \ + !defined(HAVE_SELFTEST) + if (wb_rsa_pss(ctx)) { + puts("internal cryptocb white-box: RSA-PSS round trip failed"); + failed = 1; + } +#endif + printf("internal cryptocb white-box: %d vectors driven\n", g_checks); done: @@ -445,7 +523,7 @@ int main(void) if (ctx != NULL) wolfSSL_CTX_free(ctx); wolfSSL_Cleanup(); - return 0; /* always 0: a non-zero exit discards the variant */ + return failed; } #else From cd2d0bf0cd06ec61fdae81dd4c874ec3012195d5 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 12:17:01 +0100 Subject: [PATCH 17/22] Verify TLS handshake transcript copying and error propagation --- .../unit-mcdc/test_internal_eddsa_whitebox.c | 95 ++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/tests/unit-mcdc/test_internal_eddsa_whitebox.c b/tests/unit-mcdc/test_internal_eddsa_whitebox.c index aabfd347cd3..15eb5801190 100644 --- a/tests/unit-mcdc/test_internal_eddsa_whitebox.c +++ b/tests/unit-mcdc/test_internal_eddsa_whitebox.c @@ -46,14 +46,34 @@ * Rules, as for the sibling drivers: * - options.h FIRST, or the smoke build compiles this with the feature * macros undefined and it silently becomes a no-op that still exits 0. - * - main() ALWAYS returns 0; a non-zero exit discards the whole variant. * - The target carries the SAME preprocessor guard that encloses it in * internal.c. */ #include +#if !defined(WOLFCRYPT_ONLY) && !defined(NO_TLS) && \ + !defined(WOLFSSL_NO_TLS12) && !defined(NO_SHA256) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + #include + static int wb_sha256_copy(wc_Sha256* source, wc_Sha256* destination); + #define wc_Sha256Copy(source, destination) \ + wb_sha256_copy((source), (destination)) +#endif #include +#if !defined(WOLFCRYPT_ONLY) && !defined(NO_TLS) && \ + !defined(WOLFSSL_NO_TLS12) && !defined(NO_SHA256) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + #undef wc_Sha256Copy + #include + static int wb_copy_failed; + static int wb_sha256_copy(wc_Sha256* source, wc_Sha256* destination) + { + if (wb_copy_failed) + return WC_HW_E; + return wc_Sha256Copy(source, destination); + } +#endif #include "mcdc_fault_alloc.h" @@ -134,6 +154,69 @@ static void wb_eddsa_update(void) ssl.hsHashes = NULL; } +#if !defined(NO_SHA256) && !defined(NO_WOLFSSL_CLIENT) && \ + !defined(MCDC_FA_UNAVAILABLE) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +static int wb_transcript_copy(void) +{ + static const byte transcript[] = { 1, 2, 3, 4 }; + WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()); + WOLFSSL* ssl = ctx != NULL ? wolfSSL_new(ctx) : NULL; + HS_Hashes* duplicate = NULL; + byte sourceHash[WC_SHA256_DIGEST_SIZE]; + byte duplicateHash[WC_SHA256_DIGEST_SIZE]; + int failed = 1; + int ret; + + if (ssl == NULL) + goto done; + if (ssl->hsHashes == NULL && InitHandshakeHashes(ssl) != 0) + goto done; + if (InitHandshakeHashesAndCopy(NULL, ssl->hsHashes, &duplicate) != + BAD_FUNC_ARG || + InitHandshakeHashesAndCopy(ssl, NULL, &duplicate) != BAD_FUNC_ARG || + InitHandshakeHashesAndCopy(ssl, ssl->hsHashes, NULL) != BAD_FUNC_ARG) + goto done; + + ssl->options.cacheMessages = 1; + if (wc_Sha256Update(&ssl->hsHashes->hashSha256, transcript, + sizeof(transcript)) != 0 || + InitHandshakeHashesAndCopy(ssl, ssl->hsHashes, &duplicate) != 0 || + EdDSA_Update(ssl, transcript, sizeof(transcript)) != 0 || + InitHandshakeHashesAndCopy(ssl, ssl->hsHashes, &duplicate) != 0) + goto done; + + if (duplicate->messages == ssl->hsHashes->messages || + duplicate->length != sizeof(transcript) || + XMEMCMP(duplicate->messages, transcript, sizeof(transcript)) != 0 || + wc_Sha256GetHash(&ssl->hsHashes->hashSha256, sourceHash) != 0 || + wc_Sha256GetHash(&duplicate->hashSha256, duplicateHash) != 0 || + XMEMCMP(sourceHash, duplicateHash, sizeof(sourceHash)) != 0) + goto done; + + wb_copy_failed = 1; + ret = InitHandshakeHashesAndCopy(ssl, ssl->hsHashes, &duplicate); + wb_copy_failed = 0; + if (ret != WC_HW_E || duplicate == NULL || duplicate->messages != NULL) + goto done; + + mcdc_fa_install(); + mcdc_fa_arm_only(1); + ret = InitHandshakeHashesAndCopy(ssl, ssl->hsHashes, &duplicate); + mcdc_fa_restore(); + if (ret != MEMORY_E || duplicate != NULL) + goto done; + + failed = 0; + +done: + if (ssl != NULL) + Free_HS_Hashes(duplicate, ssl->heap); + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + return failed; +} +#endif + #endif /* !WOLFSSL_NO_CLIENT_AUTH && (SM2 || ED25519 || ED448) */ #endif /* !NO_TLS */ @@ -141,6 +224,7 @@ static void wb_eddsa_update(void) int main(void) { + int failed = 0; if (wolfSSL_Init() != WOLFSSL_SUCCESS) { printf("internal EdDSA white-box: wolfSSL_Init failed\n"); return 0; @@ -152,6 +236,13 @@ int main(void) (defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \ (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH))) wb_eddsa_update(); +#if !defined(NO_SHA256) && !defined(NO_WOLFSSL_CLIENT) && \ + !defined(MCDC_FA_UNAVAILABLE) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + if (wb_transcript_copy()) { + puts("internal EdDSA white-box: transcript copy failed"); + failed = 1; + } +#endif printf("internal EdDSA white-box: %d static-guard calls\n", g_calls); #else printf("internal EdDSA white-box: skipped (no EdDSA client auth)\n"); @@ -161,7 +252,7 @@ int main(void) #endif wolfSSL_Cleanup(); - return 0; /* always 0: a non-zero exit discards the variant */ + return failed; } #else From 939329f9216c476257dc2e7e6606328a13676698 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 12:33:52 +0100 Subject: [PATCH 18/22] Sweep allocation failures during native TLS handshakes --- tests/api/test_ssl_cert.c | 82 +++++++++++++++++++++++++++++++++++++++ tests/api/test_ssl_cert.h | 2 + 2 files changed, 84 insertions(+) diff --git a/tests/api/test_ssl_cert.c b/tests/api/test_ssl_cert.c index 6f3e15a3caa..87c72f84fee 100644 --- a/tests/api/test_ssl_cert.c +++ b/tests/api/test_ssl_cert.c @@ -6874,6 +6874,88 @@ int test_wolfSSL_alloc_failure_sweep(void) return EXPECT_RESULT(); } +#if defined(WOLFSSL_MCDC_ALLOC_SWEEP) && defined(USE_WOLFSSL_MEMORY) && \ + defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_RSA) && \ + !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_STATIC_MEMORY) && \ + !defined(WOLFSSL_DEBUG_MEMORY) && !defined(HAVE_FIPS) && \ + !defined(HAVE_SELFTEST) +static int fi_handshake_attempt(method_provider client, method_provider server, + int failAt) +{ + struct test_memio_ctx io; + WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL; + WOLFSSL *ssl_c = NULL, *ssl_s = NULL; + int ret; + + XMEMSET(&io, 0, sizeof(io)); + fi_failAt = -1; + ret = test_memio_setup(&io, &ctx_c, &ctx_s, &ssl_c, &ssl_s, client, + server); + fi_count = 0; + fi_failed = 0; + if (ret == 0) { + fi_failAt = failAt; + ret = test_memio_do_handshake(ssl_c, ssl_s, 12, NULL); + fi_failAt = -1; + } + + wolfSSL_free(ssl_s); + wolfSSL_free(ssl_c); + wolfSSL_CTX_free(ctx_s); + wolfSSL_CTX_free(ctx_c); + return ret; +} + +static int fi_run_handshake_sweep(method_provider client, method_provider server) +{ + int total, i; + int injected = 0; + + if (fi_handshake_attempt(client, server, -1) != 0) + return 1; + total = fi_count; + for (i = 0; i < total; i++) { + (void)fi_handshake_attempt(client, server, i); + if (fi_failed) + injected++; + } + if (injected == 0 || fi_handshake_attempt(client, server, -1) != 0) + return 1; + return 0; +} +#endif + +int test_wolfSSL_handshake_alloc_failure_sweep(void) +{ + EXPECT_DECLS; +#if defined(WOLFSSL_MCDC_ALLOC_SWEEP) && defined(USE_WOLFSSL_MEMORY) && \ + defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_RSA) && \ + !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_STATIC_MEMORY) && \ + !defined(WOLFSSL_DEBUG_MEMORY) && !defined(HAVE_FIPS) && \ + !defined(HAVE_SELFTEST) + wolfSSL_Malloc_cb previousMalloc = NULL; + wolfSSL_Free_cb previousFree = NULL; + wolfSSL_Realloc_cb previousRealloc = NULL; + int failed; + + (void)wolfSSL_GetAllocators(&previousMalloc, &previousFree, &previousRealloc); + if (wolfSSL_SetAllocators(fi_malloc, fi_free, fi_realloc) != 0) + return EXPECT_RESULT(); + + failed = fi_run_handshake_sweep(wolfTLSv1_2_client_method, + wolfTLSv1_2_server_method); +#ifdef WOLFSSL_TLS13 + failed |= fi_run_handshake_sweep(wolfTLSv1_3_client_method, + wolfTLSv1_3_server_method); +#endif + + fi_failAt = -1; + (void)wolfSSL_SetAllocators(previousMalloc, previousFree, previousRealloc); + ExpectIntEQ(failed, 0); +#endif + return EXPECT_RESULT(); +} + /* --------------------------------------------------------------------------- * DTLS API argument guards, on a DTLS object. * diff --git a/tests/api/test_ssl_cert.h b/tests/api/test_ssl_cert.h index 71bc65b0f03..774f617b98c 100644 --- a/tests/api/test_ssl_cert.h +++ b/tests/api/test_ssl_cert.h @@ -33,6 +33,7 @@ int test_wolfSSL_load_pathological_files(void); int test_wolfSSL_load_from_fifo(void); int test_wolfSSL_dtls_api_more_guards(void); int test_wolfSSL_alloc_failure_sweep(void); +int test_wolfSSL_handshake_alloc_failure_sweep(void); int test_wolfSSL_get_verify_mode(void); int test_wolfSSL_CTX_get_verify_mode(void); @@ -165,6 +166,7 @@ int test_wolfSSL_SESSION_get0_peer_full_chain(void); TEST_DECL_GROUP("ssl_cert", test_wolfSSL_load_from_fifo), \ TEST_DECL_GROUP("ssl_cert", test_wolfSSL_dtls_api_more_guards), \ TEST_DECL_GROUP("ssl_cert", test_wolfSSL_alloc_failure_sweep), \ + TEST_DECL_GROUP("ssl_cert", test_wolfSSL_handshake_alloc_failure_sweep), \ TEST_DECL_GROUP("ssl_cert", test_ssl_cert_decision_coverage) #endif /* TESTS_API_SSL_CERT_H */ From b009dc59a371099d1ded43b453aa7a6126df9e76 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 12:37:11 +0100 Subject: [PATCH 19/22] Test native TLS transport retry and buffer retention --- .../unit-mcdc/test_internal_record_whitebox.c | 150 +++++++++++++++++- 1 file changed, 148 insertions(+), 2 deletions(-) diff --git a/tests/unit-mcdc/test_internal_record_whitebox.c b/tests/unit-mcdc/test_internal_record_whitebox.c index 77c95c08988..21c3c4fecdc 100644 --- a/tests/unit-mcdc/test_internal_record_whitebox.c +++ b/tests/unit-mcdc/test_internal_record_whitebox.c @@ -49,7 +49,6 @@ * Rules, as for the sibling drivers: * - options.h FIRST, or the smoke build compiles this with the feature * macros undefined and it silently becomes a no-op that still exits 0. - * - main() ALWAYS returns 0; a non-zero exit discards the whole variant. * - Bail paths print, so "covered nothing" differs from "nothing to say". */ @@ -141,8 +140,144 @@ static void wb_record(const Rec* r, WOLFSSL* ssl) g_calls++; } +#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +struct wb_transport { + int remaining; + int calls; +}; + +static int wb_send_retry(WOLFSSL* ssl, char* data, int size, void* context) +{ + struct wb_transport* test = (struct wb_transport*)context; + + (void)ssl; + test->calls++; + if (test->remaining-- > 0) + return WOLFSSL_CBIO_ERR_WANT_WRITE; + return size == 1 && data[0] == 'T' ? size : WOLFSSL_CBIO_ERR_GENERAL; +} + +static int wb_receive_retry(WOLFSSL* ssl, char* data, int size, void* context) +{ + struct wb_transport* test = (struct wb_transport*)context; + + (void)ssl; + test->calls++; + if (test->remaining-- > 0) + return WOLFSSL_CBIO_ERR_WANT_READ; + if (size != 1) + return WOLFSSL_CBIO_ERR_GENERAL; + data[0] = 'T'; + return size; +} + +static int wb_transport_retry(WOLFSSL_CTX* ctx, WOLFSSL* ssl) +{ + static const struct { + byte autoRetry; + byte handshakeDone; + byte dtls; + int blocked; + int calls; + int succeeds; + } cases[] = { + { 1, 0, 0, 1, 2, 1 }, + { 0, 0, 0, 1, 1, 0 }, + { 1, 1, 0, 1, 1, 0 }, +#ifdef WOLFSSL_DTLS + { 1, 0, 1, 1, 1, 0 }, +#endif + { 1, 0, 0, WOLFSSL_MODE_AUTO_RETRY_ATTEMPTS + 1, + WOLFSSL_MODE_AUTO_RETRY_ATTEMPTS + 1, 0 } + }; + int saved = ctx->autoRetry; + int failed = 0; + size_t i; + + for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + struct wb_transport send = { cases[i].blocked, 0 }; + struct wb_transport receive = { cases[i].blocked, 0 }; + byte payload = 'T'; + byte result = 0; + + XMEMSET(ssl, 0, sizeof(*ssl)); + ssl->ctx = ctx; + ctx->autoRetry = cases[i].autoRetry; + ssl->options.handShakeDone = cases[i].handshakeDone; + ssl->options.dtls = cases[i].dtls; + ssl->CBIOSend = wb_send_retry; + ssl->CBIORecv = wb_receive_retry; + ssl->IOCB_WriteCtx = &send; + ssl->IOCB_ReadCtx = &receive; + ssl->buffers.outputBuffer.buffer = &payload; + ssl->buffers.outputBuffer.length = 1; + ssl->buffers.outputBuffer.bufferSize = 1; + + if (SendBuffered(ssl) != (cases[i].succeeds ? 0 : WANT_WRITE) || + send.calls != cases[i].calls || + ssl->buffers.outputBuffer.length != (cases[i].succeeds ? 0 : 1)) + failed = 1; + if (wolfSSLReceive(ssl, &result, 1) != + (cases[i].succeeds ? 1 : WANT_READ) || + receive.calls != cases[i].calls || + result != (cases[i].succeeds ? 'T' : 0)) + failed = 1; + } + ctx->autoRetry = (byte)saved; + return failed; +} +static int wb_input_buffer(WOLFSSL_CTX* ctx, WOLFSSL* ssl) +{ + static const byte data[] = { 0x31, 0x32, 0x33 }; + bufferStatic* input = &ssl->buffers.inputBuffer; + int failed = 1; + + XMEMSET(ssl, 0, sizeof(*ssl)); + ssl->ctx = ctx; + ssl->options.processReply = doProcessInit; + input->buffer = input->staticBuffer; + input->bufferSize = STATIC_BUFFER_LEN; + if (STATIC_BUFFER_LEN < sizeof(data)) + return 0; + XMEMCPY(input->buffer, data, sizeof(data)); + input->length = sizeof(data); + input->idx = 1; + + if (GrowInputBuffer(ssl, 1, -1) != BAD_FUNC_ARG || + GrowInputBuffer(ssl, -1, 1) != BAD_FUNC_ARG || + GrowInputBuffer(ssl, STATIC_BUFFER_LEN, sizeof(data) - 1) != 0) + goto done; + if (!input->dynamicFlag || input->idx != 0 || + input->length != sizeof(data) - 1 || + XMEMCMP(input->buffer, data + 1, sizeof(data) - 1) != 0) + goto done; + + /* Moving buffered input while processing its record would change the + * offsets that the parser still owns. */ + ssl->options.processReply = getRecordLayerHeader; + ShrinkInputBuffer(ssl, NO_FORCED_FREE); + if (!input->dynamicFlag) + goto done; + + ssl->options.processReply = doProcessInit; + ShrinkInputBuffer(ssl, NO_FORCED_FREE); + if (input->dynamicFlag || input->buffer != input->staticBuffer || + input->length != sizeof(data) - 1 || + XMEMCMP(input->buffer, data + 1, sizeof(data) - 1) != 0) + goto done; + failed = 0; + +done: + if (input->dynamicFlag) + ShrinkInputBuffer(ssl, FORCED_FREE); + return failed; +} + +#endif + int main(void) { + int failed = 0; WOLFSSL_CTX* ctx = NULL; WOLFSSL* ssl = NULL; size_t i; @@ -304,6 +439,17 @@ int main(void) } } +#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + if (wb_transport_retry(ctx, ssl)) { + puts("internal record white-box: unexpected transport retry result"); + failed = 1; + } + if (wb_input_buffer(ctx, ssl)) { + puts("internal record white-box: unexpected input buffer result"); + failed = 1; + } +#endif + printf("internal record white-box: %d GetRecordHeader calls\n", g_calls); done: @@ -311,7 +457,7 @@ int main(void) if (ctx != NULL) wolfSSL_CTX_free(ctx); wolfSSL_Cleanup(); - return 0; /* always 0: a non-zero exit discards the variant */ + return failed; } #else From 8b0fa2ea730c858e2bc4c8b10f81e40906efad5b Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 12:54:03 +0100 Subject: [PATCH 20/22] Cover native peer key decoding and minimum size policy --- tests/api/test_ssl_cert.c | 6 +- .../test_internal_peerkey_whitebox.c | 133 +++++++++++++++++- 2 files changed, 135 insertions(+), 4 deletions(-) diff --git a/tests/api/test_ssl_cert.c b/tests/api/test_ssl_cert.c index 87c72f84fee..c90876733b1 100644 --- a/tests/api/test_ssl_cert.c +++ b/tests/api/test_ssl_cert.c @@ -6876,7 +6876,8 @@ int test_wolfSSL_alloc_failure_sweep(void) #if defined(WOLFSSL_MCDC_ALLOC_SWEEP) && defined(USE_WOLFSSL_MEMORY) && \ defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_RSA) && \ - !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_STATIC_MEMORY) && \ + !defined(NO_TLS) && !defined(WOLFSSL_NO_TLS12) && \ + !defined(WOLFSSL_STATIC_MEMORY) && \ !defined(WOLFSSL_DEBUG_MEMORY) && !defined(HAVE_FIPS) && \ !defined(HAVE_SELFTEST) static int fi_handshake_attempt(method_provider client, method_provider server, @@ -6930,7 +6931,8 @@ int test_wolfSSL_handshake_alloc_failure_sweep(void) EXPECT_DECLS; #if defined(WOLFSSL_MCDC_ALLOC_SWEEP) && defined(USE_WOLFSSL_MEMORY) && \ defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_RSA) && \ - !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_STATIC_MEMORY) && \ + !defined(NO_TLS) && !defined(WOLFSSL_NO_TLS12) && \ + !defined(WOLFSSL_STATIC_MEMORY) && \ !defined(WOLFSSL_DEBUG_MEMORY) && !defined(HAVE_FIPS) && \ !defined(HAVE_SELFTEST) wolfSSL_Malloc_cb previousMalloc = NULL; diff --git a/tests/unit-mcdc/test_internal_peerkey_whitebox.c b/tests/unit-mcdc/test_internal_peerkey_whitebox.c index a8a0f727dee..79427a9d65a 100644 --- a/tests/unit-mcdc/test_internal_peerkey_whitebox.c +++ b/tests/unit-mcdc/test_internal_peerkey_whitebox.c @@ -56,7 +56,6 @@ * Rules, as for the sibling drivers: * - options.h FIRST, or the smoke build compiles this with the feature * macros undefined and it silently becomes a no-op that still exits 0. - * - main() ALWAYS returns 0; a non-zero exit discards the whole variant. * - Each target carries the SAME preprocessor guard that encloses it in * internal.c. */ @@ -64,6 +63,7 @@ #include #include +#include "mcdc_fault_alloc.h" #include #include @@ -513,10 +513,130 @@ static void wb_process_csr(WbFix* f) } #endif /* !NO_CERTS && HAVE_CERTIFICATE_STATUS_REQUEST && !WOLFSSL_NO_TLS12 */ +#if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \ + !defined(NO_STDIO_FILESYSTEM) && !defined(NO_WOLFSSL_CLIENT) && \ + !defined(NO_RSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +#ifdef WOLFSSL_CHAIN_VERIFY_CB +static int wb_unexpected_chain_callback(WOLFSSL* ssl, + const WOLFSSL_BUFFER_INFO* certs, int count, void* context) +{ + (void)ssl; + (void)certs; + (void)count; + (void)context; + return BAD_STATE_E; +} +#endif + +static int wb_decode_peer_key(const char* path, int sizeError) +{ + WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()); + WOLFSSL* ssl = ctx != NULL ? wolfSSL_new(ctx) : NULL; + ProcPeerCertArgs args; + DecodedCert* cert = NULL; + byte der[2048]; + FILE* file; + size_t length; + word32 pubKeySize; + int ret; + int failed = 1; + + if (ssl == NULL) + goto done; + file = fopen(path, "rb"); + if (file == NULL) + goto done; + length = fread(der, 1, sizeof(der), file); + fclose(file); + if (length == 0 || length == sizeof(der)) + goto done; + cert = (DecodedCert*)XMALLOC(sizeof(*cert), ssl->heap, DYNAMIC_TYPE_DCERT); + if (cert == NULL) + goto done; + InitDecodedCert(cert, der, (word32)length, ssl->heap); + if (DecodeToKey(cert, 0) < 0) + goto done; + XMEMSET(&args, 0, sizeof(args)); + args.dCert = cert; + wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER, NULL); + +#ifndef MCDC_FA_UNAVAILABLE + ret = 0; + mcdc_fa_install(); + mcdc_fa_arm_only(1); + if (ProcessPeerCertDecodeKey(ssl, &args, &ret) != 0) + ret = BAD_STATE_E; + mcdc_fa_restore(); + if (ret != PEER_KEY_ERROR) + goto done; +#endif + + /* Leave the real fixture intact and simulate an absent key payload. */ + pubKeySize = cert->pubKeySize; + cert->pubKeySize = 0; + ret = 0; + if (ProcessPeerCertDecodeKey(ssl, &args, &ret) != 0) + ret = BAD_STATE_E; + cert->pubKeySize = pubKeySize; + if (ret != PEER_KEY_ERROR) + goto done; + + ret = 0; + if (ProcessPeerCertDecodeKey(ssl, &args, &ret) != 0 || ret != 0) + goto done; + + ssl->options.minRsaKeySz = 512; + ssl->options.minEccKeySz = 128; + if (ProcessPeerCertDecodeKey(ssl, &args, &ret) != 0 || ret != sizeError) + goto done; + ret = ASN_NO_SIGNER_E; + if (ProcessPeerCertDecodeKey(ssl, &args, &ret) != 0 || + ret != ASN_NO_SIGNER_E) + goto done; + wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL); + ret = 0; + if (ProcessPeerCertDecodeKey(ssl, &args, &ret) != 0 || ret != 0) + goto done; +#ifdef WOLFSSL_CHAIN_VERIFY_CB + /* Delegating chain checks keeps the library's peer key policy active. */ + ssl->chainVerifyCb = wb_unexpected_chain_callback; + if (ProcessPeerCertDecodeKey(ssl, &args, &ret) != 0 || ret != sizeError) + goto done; +#endif + failed = 0; + +done: + if (cert != NULL) { + FreeDecodedCert(cert); + XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT); + } + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + return failed; +} + +static int wb_peer_key_policy(void) +{ + int failed = wb_decode_peer_key("./certs/server-cert.der", RSA_KEY_SIZE_E); +#ifdef HAVE_ECC + failed |= wb_decode_peer_key("./certs/server-ecc.der", ECC_KEY_SIZE_E); +#endif +#if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT) + failed |= wb_decode_peer_key("./certs/ed25519/server-ed25519.der", + ECC_KEY_SIZE_E); +#endif +#if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT) + failed |= wb_decode_peer_key("./certs/ed448/server-ed448.der", ECC_KEY_SIZE_E); +#endif + return failed; +} +#endif + /* ---------------------------------------------------------------------- main */ int main(void) { + int failed = 0; WbFix f; XMEMSET(&f, 0, sizeof(f)); @@ -571,6 +691,15 @@ int main(void) wb_process_csr(&f); #endif +#if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \ + !defined(NO_STDIO_FILESYSTEM) && !defined(NO_WOLFSSL_CLIENT) && \ + !defined(NO_RSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + if (wb_peer_key_policy()) { + puts("internal peer-key white-box: unexpected peer key policy result"); + failed = 1; + } +#endif + printf("internal peer-key white-box: %d static-guard calls\n", g_calls); if (f.rngOk) @@ -581,7 +710,7 @@ int main(void) XFREE(f.ctx, NULL, DYNAMIC_TYPE_CTX); XFREE(f.ssl, NULL, DYNAMIC_TYPE_SSL); wolfSSL_Cleanup(); - return 0; /* always 0: a non-zero exit discards the variant */ + return failed; } #else From 599e4aca5737bec08915581d8b920c2e2ebc7e7d Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 12:57:08 +0100 Subject: [PATCH 21/22] Cover TLS RSA signature selection and PSS parameter policy --- .../test_internal_cryptocb_whitebox.c | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/tests/unit-mcdc/test_internal_cryptocb_whitebox.c b/tests/unit-mcdc/test_internal_cryptocb_whitebox.c index b75bc83a9da..d8eddd5940a 100644 --- a/tests/unit-mcdc/test_internal_cryptocb_whitebox.c +++ b/tests/unit-mcdc/test_internal_cryptocb_whitebox.c @@ -398,6 +398,84 @@ static void wb_rsa_null_args(WOLFSSL* ssl) !defined(NO_WOLFSSL_SERVER) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(HAVE_FIPS) && \ !defined(HAVE_SELFTEST) +#if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \ + !defined(NO_CERTS) && defined(WOLFSSL_SHA384) +static int wb_rsa_signature_policy(WOLFSSL* ssl) +{ + static const byte allowedPss[] = { NEW_SA_MAJOR, RSA_PSS_PSS_SHA256_MINOR }; + static const byte allowedRsa[] = { + sha256_mac, rsa_sa_algo, sha384_mac, rsa_sa_algo + }; + static const byte reversedRsa[] = { + sha384_mac, rsa_sa_algo, sha256_mac, rsa_sa_algo + }; + int failed = 1; + + ssl->version = MakeTLSv1_2(); + ssl->options.tls = 1; + ssl->options.tls1_1 = 1; + ssl->options.side = WOLFSSL_SERVER_END; + ssl->specs.sig_algo = rsa_sa_algo; + ssl->buffers.keyType = rsa_sa_algo; + ssl->buffers.keySz = 256; + ssl->useRsaPss = 1; + + if (PickHashSigAlgo(ssl, NULL, 0, 0) != MATCH_SUITE_ERROR || + PickHashSigAlgo(ssl, allowedPss, sizeof(allowedPss), 0) != 0 || + ssl->options.sigAlgo != rsa_pss_pss_algo) + goto done; + + ssl->options.side = WOLFSSL_CLIENT_END; + if (PickHashSigAlgo(ssl, NULL, 0, 0) != 0) + goto done; + ssl->options.side = WOLFSSL_SERVER_END; + ssl->specs.sig_algo = ecc_dsa_sa_algo; + if (PickHashSigAlgo(ssl, NULL, 0, 0) != 0) + goto done; + ssl->specs.sig_algo = rsa_sa_algo; + ssl->version = MakeTLSv1_3(); + if (PickHashSigAlgo(ssl, NULL, 0, 0) != 0) + goto done; + + ssl->version = MakeTLSv1_2(); + ssl->useRsaPss = 0; + if (PickHashSigAlgo(ssl, NULL, 0, 0) != 0 || + PickHashSigAlgo(ssl, allowedRsa, sizeof(allowedRsa), 0) != 0 || + PickHashSigAlgo(ssl, reversedRsa, sizeof(reversedRsa), 0) != 0) + goto done; +#ifdef WOLFSSL_STRONGEST_HASH_SIG + if (ssl->options.hashAlgo != sha384_mac) + goto done; +#else + if (ssl->options.hashAlgo != sha256_mac) + goto done; +#endif + + ssl->peerRsaPss = 1; + ssl->peerRsaPssHash = WC_HASH_TYPE_SHA256; + ssl->peerRsaPssMgf = WC_MGF1SHA256; + ssl->peerRsaPssSaltLen = WC_SHA256_DIGEST_SIZE; + if (CheckPeerRsaSigAlgo(ssl, rsa_pss_pss_algo, sha256_mac) != 0 || + CheckPeerRsaSigAlgo(ssl, rsa_pss_sa_algo, sha256_mac) != + INVALID_PARAMETER || + CheckPeerRsaSigAlgo(ssl, rsa_pss_pss_algo, sha384_mac) != + INVALID_PARAMETER) + goto done; + ssl->peerRsaPssMgf = WC_MGF1SHA384; + if (CheckPeerRsaSigAlgo(ssl, rsa_pss_pss_algo, sha256_mac) != INVALID_PARAMETER) + goto done; + ssl->peerRsaPssMgf = WC_MGF1SHA256; + ssl->peerRsaPssSaltLen++; + if (CheckPeerRsaSigAlgo(ssl, rsa_pss_pss_algo, sha256_mac) != INVALID_PARAMETER) + goto done; + failed = 0; + +done: + ssl->options.side = WOLFSSL_CLIENT_END; + return failed; +} +#endif + static int wb_rsa_pss(WOLFSSL_CTX* ctx) { static const int algorithms[] = { @@ -416,6 +494,11 @@ static int wb_rsa_pss(WOLFSSL_CTX* ctx) if (ssl == NULL || wc_InitRsaKey(&key, NULL) != 0) goto done; initialized = 1; +#if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \ + !defined(NO_CERTS) && defined(WOLFSSL_SHA384) + if (wb_rsa_signature_policy(ssl)) + goto done; +#endif if (wc_RsaPrivateKeyDecode(client_key_der_2048, &idx, &key, sizeof_client_key_der_2048) != 0) goto done; From d137eed00b83d9abf6592ebcaeb4ed05ad037e32 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Nov 2026 13:07:49 +0100 Subject: [PATCH 22/22] Cover TLS key exchange and transcript preconditions --- tests/api/test_tls.c | 46 +++++++ .../unit-mcdc/test_internal_eddsa_whitebox.c | 35 +++++ .../test_internal_nullguard_whitebox.c | 124 ++++++++++++++++++ 3 files changed, 205 insertions(+) diff --git a/tests/api/test_tls.c b/tests/api/test_tls.c index 51d7c185eee..d3d8246788a 100644 --- a/tests/api/test_tls.c +++ b/tests/api/test_tls.c @@ -8004,6 +8004,19 @@ static unsigned int test_dhe_psk_max_server_cb(WOLFSSL* ssl, return MAX_PSK_KEY_LEN; } +#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +static unsigned int test_dhe_psk_invalid_key_cb(WOLFSSL* ssl, const char* hint, + char* id, unsigned int id_max, unsigned char* key, unsigned int key_max) +{ + const int* oversized = (const int*)wolfSSL_get_psk_callback_ctx(ssl); + (void)hint; + (void)id; + (void)id_max; + (void)key; + return oversized != NULL && *oversized ? key_max + 1 : 0; +} +#endif + #endif /* A DHE-PSK exchange fills the pre-master secret buffer with a two byte @@ -8030,6 +8043,9 @@ int test_dhe_psk_max_premaster(void) WOLFSSL *ssl_c = NULL, *ssl_s = NULL; struct test_memio_ctx test_ctx; int groups[1] = { WOLFSSL_ECC_SECP256R1 }; +#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + int oversized; +#endif /* Both lengths, the largest secret the peer can pick and the longest key * have to fit. */ @@ -8062,6 +8078,36 @@ int test_dhe_psk_max_premaster(void) wolfSSL_free(ssl_s); wolfSSL_CTX_free(ctx_c); wolfSSL_CTX_free(ctx_s); + +#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + for (oversized = 0; oversized <= 1 && EXPECT_SUCCESS(); oversized++) { + ctx_c = ctx_s = NULL; + ssl_c = ssl_s = NULL; + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0); + ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_set_psk_callback_ctx(ssl_c, &oversized), + WOLFSSL_SUCCESS); + if (EXPECT_SUCCESS()) { + wolfSSL_set_psk_client_callback(ssl_c, test_dhe_psk_invalid_key_cb); + wolfSSL_set_psk_server_callback(ssl_s, test_dhe_psk_max_server_cb); + } + ExpectIntEQ(wolfSSL_SetTmpDH_file(ssl_s, TEST_DHE_PSK_MAX_DH_FILE, + WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_set_groups(ssl_c, groups, 1), WOLFSSL_SUCCESS); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), -1); + ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR), + WC_NO_ERR_TRACE(PSK_KEY_ERROR)); + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + } +#endif #endif return EXPECT_RESULT(); } diff --git a/tests/unit-mcdc/test_internal_eddsa_whitebox.c b/tests/unit-mcdc/test_internal_eddsa_whitebox.c index 15eb5801190..ba4788cb9d7 100644 --- a/tests/unit-mcdc/test_internal_eddsa_whitebox.c +++ b/tests/unit-mcdc/test_internal_eddsa_whitebox.c @@ -154,6 +154,35 @@ static void wb_eddsa_update(void) ssl.hsHashes = NULL; } +#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +static int wb_eddsa_update_bounds(void) +{ + static const byte data = 0xAA; + WOLFSSL ssl; + HS_Hashes hashes; + int failed = 0; + + XMEMSET(&ssl, 0, sizeof(ssl)); + XMEMSET(&hashes, 0, sizeof(hashes)); + ssl.hsHashes = &hashes; + if (EdDSA_Update(&ssl, &data, 1) != 0 || hashes.messages != NULL) + failed = 1; + + ssl.options.cacheMessages = 1; + if (EdDSA_Update(&ssl, &data, -1) != BUFFER_ERROR) + failed = 1; + hashes.length = -1; + if (EdDSA_Update(&ssl, &data, 1) != BUFFER_ERROR) + failed = 1; + hashes.length = INT_MAX; + if (EdDSA_Update(&ssl, &data, 1) != BUFFER_ERROR) + failed = 1; + if (hashes.messages != NULL) + failed = 1; + return failed; +} +#endif + #if !defined(NO_SHA256) && !defined(NO_WOLFSSL_CLIENT) && \ !defined(MCDC_FA_UNAVAILABLE) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) static int wb_transcript_copy(void) @@ -236,6 +265,12 @@ int main(void) (defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \ (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH))) wb_eddsa_update(); +#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + if (wb_eddsa_update_bounds()) { + puts("internal EdDSA white-box: message cache bounds failed"); + failed = 1; + } +#endif #if !defined(NO_SHA256) && !defined(NO_WOLFSSL_CLIENT) && \ !defined(MCDC_FA_UNAVAILABLE) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) if (wb_transcript_copy()) { diff --git a/tests/unit-mcdc/test_internal_nullguard_whitebox.c b/tests/unit-mcdc/test_internal_nullguard_whitebox.c index ca7fd69b8e3..8fc51e332cd 100644 --- a/tests/unit-mcdc/test_internal_nullguard_whitebox.c +++ b/tests/unit-mcdc/test_internal_nullguard_whitebox.c @@ -1138,6 +1138,121 @@ static void wb_get_real_session_id(WbFix* f) } #endif /* !NO_WOLFSSL_SERVER && HAVE_SESSION_TICKET && WOLFSSL_TICKET_HAVE_ID */ +#if !defined(NO_WOLFSSL_CLIENT) && !defined(WOLFSSL_ASYNC_CRYPT) && \ + (!defined(NO_DH) || !defined(NO_RSA)) && !defined(MCDC_FA_UNAVAILABLE) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +#if !defined(NO_DH) && !defined(NO_PSK) +static unsigned int wb_psk_not_called(WOLFSSL* ssl, const char* hint, + char* identity, unsigned int identityCapacity, unsigned char* key, + unsigned int keyCapacity) +{ + (void)ssl; + (void)hint; + (void)identity; + (void)identityCapacity; + (void)key; + (void)keyCapacity; + return 0; +} +#endif + +static int wb_send_key_exchange(WOLFSSL* ssl) +{ + ssl->options.buildingMsg = 0; + return SendClientKeyExchange(ssl); +} + +static int wb_missing_exchange_keys(WbFix* f) +{ + int failed = 0; + int ret; + + wb_reset(f); + f->ssl->arrays = f->arrays; +#ifdef WOLFSSL_ASYNC_IO + f->ssl->async = (struct WOLFSSL_ASYNC*)XMALLOC(sizeof(*f->ssl->async), + NULL, DYNAMIC_TYPE_ASYNC); + if (f->ssl->async == NULL) + return 1; + XMEMSET(f->ssl->async, 0, sizeof(*f->ssl->async)); +#endif + +#ifndef NO_RSA + { + RsaKey* rsa = NULL; + + f->ssl->specs.kea = rsa_kea; + if (wb_send_key_exchange(f->ssl) != NO_PEER_KEY) + failed = 1; + + if (AllocKey(f->ssl, DYNAMIC_TYPE_RSA, (void**)&rsa) != 0) { + #ifdef WOLFSSL_ASYNC_IO + FreeAsyncCtx(f->ssl, 1); + #endif + return 1; + } + f->ssl->peerRsaKey = rsa; + if (wb_send_key_exchange(f->ssl) != NO_PEER_KEY) + failed = 1; + + /* An allocation error after the guard is the positive partner. The + * RSA key object never reaches an encryption operation. */ + f->ssl->peerRsaKeyPresent = 1; + mcdc_fa_install(); + mcdc_fa_arm_only(1); + ret = wb_send_key_exchange(f->ssl); + mcdc_fa_restore(); + if (ret != MEMORY_E) + failed = 1; + FreePeerCertKeys(f->ssl); + } +#endif +#ifndef NO_DH + { + static const byte exchanges[] = { + diffie_hellman_kea, + #ifndef NO_PSK + dhe_psk_kea, + #endif + }; + byte unread = 0; + size_t i; + int omitted; + + /* These buffers are borrowed by the fixture. Allocation stops the + * accepting case before their data would be read. */ + for (i = 0; i < sizeof(exchanges) / sizeof(exchanges[0]); i++) { + for (omitted = 0; omitted <= 3; omitted++) { + f->ssl->specs.kea = exchanges[i]; + f->ssl->buffers.serverDH_P.buffer = + omitted == 0 ? NULL : &unread; + f->ssl->buffers.serverDH_G.buffer = + omitted == 1 ? NULL : &unread; + f->ssl->buffers.serverDH_Pub.buffer = + omitted == 2 ? NULL : &unread; + #ifndef NO_PSK + f->ssl->options.client_psk_cb = wb_psk_not_called; + #endif + mcdc_fa_install(); + mcdc_fa_arm_only(1); + ret = wb_send_key_exchange(f->ssl); + mcdc_fa_restore(); + if (ret != (omitted == 3 ? MEMORY_E : NO_PEER_KEY)) + failed = 1; + } + } + f->ssl->buffers.serverDH_P.buffer = NULL; + f->ssl->buffers.serverDH_G.buffer = NULL; + f->ssl->buffers.serverDH_Pub.buffer = NULL; + } +#endif +#ifdef WOLFSSL_ASYNC_IO + FreeAsyncCtx(f->ssl, 1); +#endif + return failed; +} +#endif + /* ---------------------------------------------------------------------- main */ int main(void) @@ -1284,6 +1399,15 @@ int main(void) } #endif +#if !defined(NO_WOLFSSL_CLIENT) && !defined(WOLFSSL_ASYNC_CRYPT) && \ + (!defined(NO_DH) || !defined(NO_RSA)) && !defined(MCDC_FA_UNAVAILABLE) && \ + !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) + if (wb_missing_exchange_keys(&f)) { + puts("internal null-guard white-box: unexpected exchange key result"); + failed = 1; + } +#endif + printf("internal null-guard white-box: %d static-guard calls\n", g_calls); done: