@@ -30742,6 +30742,130 @@ static int test_wrong_cs_downgrade(void)
3074230742}
3074330743#endif
3074430744
30745+ #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
30746+
30747+ /* A syntactically valid TLS 1.2 ServerHello with NO extensions, sent to a
30748+ * TLS 1.3-only client. Per RFC 8446 4.2.1 the absence of a supported_versions
30749+ * extension means the server negotiated TLS 1.2 or below. A TLS 1.3-only
30750+ * client (downgrade disabled) must reject this as a version mismatch with a
30751+ * protocol_version alert (RFC 8446 6.2), NOT a decode_error - the message is
30752+ * well-formed, it just offers an unsupported protocol version.
30753+ *
30754+ * Layout: legacy_version = 0x0303, 32-byte random, 32-byte session id,
30755+ * cipher_suite = TLS_AES_128_GCM_SHA256 (0x1301), null compression, and no
30756+ * extensions field at all. */
30757+ static const byte test_tls13_no_ext_sh[] = {
30758+ 0x16, 0x03, 0x03, 0x00, 0x4a, /* record: handshake, TLS 1.2, len 74 */
30759+ 0x02, 0x00, 0x00, 0x46, /* server_hello, body len 70 */
30760+ 0x03, 0x03, /* legacy_version = TLS 1.2 */
30761+ /* 32-byte random (not the HelloRetryRequest magic) */
30762+ 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
30763+ 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
30764+ 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
30765+ 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
30766+ 0x20, /* legacy_session_id length = 32 */
30767+ /* 32-byte session id */
30768+ 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
30769+ 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
30770+ 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
30771+ 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
30772+ 0x13, 0x01, /* cipher_suite = TLS_AES_128_GCM_SHA256 */
30773+ 0x00 /* compression = null; no extensions */
30774+ };
30775+
30776+ /* Same as above but with a single trailing byte after the compression method:
30777+ * a TRUNCATED (partial) extensions length field. This is genuinely malformed,
30778+ * so it must be rejected with decode_error (50), not protocol_version - the
30779+ * fix must not relabel a real decode error as a version mismatch. Record and
30780+ * handshake lengths are bumped by one and a 0x00 trailing byte is appended. */
30781+ static const byte test_tls13_trunc_ext_sh[] = {
30782+ 0x16, 0x03, 0x03, 0x00, 0x4b, /* record: handshake, TLS 1.2, len 75 */
30783+ 0x02, 0x00, 0x00, 0x47, /* server_hello, body len 71 */
30784+ 0x03, 0x03, /* legacy_version = TLS 1.2 */
30785+ /* 32-byte random (not the HelloRetryRequest magic) */
30786+ 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
30787+ 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
30788+ 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
30789+ 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
30790+ 0x20, /* legacy_session_id length = 32 */
30791+ /* 32-byte session id */
30792+ 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
30793+ 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
30794+ 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
30795+ 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5, 0xa5,
30796+ 0x13, 0x01, /* cipher_suite = TLS_AES_128_GCM_SHA256 */
30797+ 0x00, /* compression = null */
30798+ 0x00 /* partial (1-byte) extensions length */
30799+ };
30800+
30801+ /* Drive a TLS 1.3-only client through a ClientHello, inject the supplied
30802+ * (crafted) ServerHello, and assert the handshake aborts with the expected
30803+ * fatal alert. */
30804+ static int test_tls13_sh_expect_alert(const byte* sh, int shSz, int expectAlert)
30805+ {
30806+ EXPECT_DECLS;
30807+ struct test_memio_ctx test_ctx;
30808+ WOLFSSL_CTX *ctx_c = NULL;
30809+ WOLFSSL *ssl_c = NULL;
30810+ WOLFSSL_ALERT_HISTORY h;
30811+
30812+ XMEMSET(&test_ctx, 0, sizeof(test_ctx));
30813+ ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
30814+ wolfTLSv1_3_client_method, NULL), 0);
30815+
30816+ /* Produce the ClientHello. */
30817+ ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
30818+ ExpectIntEQ(wolfSSL_get_error(ssl_c, WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR)),
30819+ WOLFSSL_ERROR_WANT_READ);
30820+
30821+ /* Discard the ClientHello and inject the crafted ServerHello. */
30822+ test_memio_clear_buffer(&test_ctx, 0);
30823+ ExpectIntEQ(test_memio_inject_message(&test_ctx, 1, (const char *)sh, shSz),
30824+ 0);
30825+
30826+ /* The handshake must fail (not WANT_READ) on the ServerHello. */
30827+ ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
30828+ ExpectIntNE(wolfSSL_get_error(ssl_c, WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR)),
30829+ WOLFSSL_ERROR_WANT_READ);
30830+
30831+ ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
30832+ ExpectIntEQ(h.last_tx.code, expectAlert);
30833+ ExpectIntEQ(h.last_tx.level, alert_fatal);
30834+
30835+ wolfSSL_free(ssl_c);
30836+ wolfSSL_CTX_free(ctx_c);
30837+
30838+ return EXPECT_RESULT();
30839+ }
30840+
30841+ /* RFC 8446 6.2: a recognized but unsupported protocol version (TLS 1.2
30842+ * ServerHello with no extensions) must be rejected with protocol_version (70),
30843+ * not decode_error (50). */
30844+ static int test_tls13_no_ext_sh_alert(void)
30845+ {
30846+ return test_tls13_sh_expect_alert(test_tls13_no_ext_sh,
30847+ (int)sizeof(test_tls13_no_ext_sh), wolfssl_alert_protocol_version);
30848+ }
30849+
30850+ /* A truncated extensions length field is a real syntax error and must still be
30851+ * reported as decode_error (50), proving the version-mismatch fix did not
30852+ * over-broaden to genuinely malformed messages. */
30853+ static int test_tls13_trunc_ext_sh_alert(void)
30854+ {
30855+ return test_tls13_sh_expect_alert(test_tls13_trunc_ext_sh,
30856+ (int)sizeof(test_tls13_trunc_ext_sh), decode_error);
30857+ }
30858+ #else
30859+ static int test_tls13_no_ext_sh_alert(void)
30860+ {
30861+ return TEST_SKIPPED;
30862+ }
30863+ static int test_tls13_trunc_ext_sh_alert(void)
30864+ {
30865+ return TEST_SKIPPED;
30866+ }
30867+ #endif
30868+
3074530869#if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
3074630870 defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
3074730871
@@ -35560,6 +35684,8 @@ TEST_CASE testCases[] = {
3556035684 TEST_DECL(test_ticket_ret_create),
3556135685 TEST_DECL(test_ticket_enc_corrupted),
3556235686 TEST_DECL(test_wrong_cs_downgrade),
35687+ TEST_DECL(test_tls13_no_ext_sh_alert),
35688+ TEST_DECL(test_tls13_trunc_ext_sh_alert),
3556335689 TEST_DECL(test_extra_alerts_wrong_cs),
3556435690 TEST_DECL(test_extra_alerts_skip_hs),
3556535691 TEST_DECL(test_extra_alerts_bad_psk),
0 commit comments