@@ -861,6 +861,89 @@ int test_tls12_etm_failed_resumption(void)
861861 return EXPECT_RESULT ();
862862}
863863
864+ /* RFC 6066 Section 3 requires:
865+ * "A server that implements this extension MUST NOT accept the request to
866+ * resume the session if the server_name extension contains a different
867+ * name. Instead, it proceeds with a full handshake to establish a new
868+ * session."
869+ *
870+ * wolfSSL's SNI/ALPN ticket-binding hardening (see VerifyTicketBinding,
871+ * added in PR #10279) covers the session ticket path but short-circuits on
872+ * !ssl->options.useTicket, so it does not apply to the TLS 1.2 stateful
873+ * session-ID cache resumption path. SetupSession() does not store the
874+ * original SNI on the cached WOLFSSL_SESSION, and TlsSessionCacheGetAndLock()
875+ * keys only on (sessionID, sessionIDSz, side). The result is that a session
876+ * established under one SNI can be resumed under a different SNI via the
877+ * session-ID cache, in violation of the MUST NOT above.
878+ *
879+ * This test forces the session-ID resumption path (no tickets) and offers a
880+ * different SNI on the resumption attempt. The server must NOT resume. */
881+ int test_tls12_session_id_resumption_sni_mismatch (void )
882+ {
883+ EXPECT_DECLS ;
884+ #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES ) && \
885+ !defined(WOLFSSL_NO_TLS12 ) && defined(HAVE_SNI ) && \
886+ defined(HAVE_SESSION_TICKET ) && !defined(NO_SESSION_CACHE )
887+ WOLFSSL_CTX * ctx_c = NULL , * ctx_s = NULL ;
888+ WOLFSSL * ssl_c = NULL , * ssl_s = NULL ;
889+ WOLFSSL_SESSION * sess = NULL ;
890+ struct test_memio_ctx test_ctx ;
891+ const char * sniA = "public.example" ;
892+ const char * sniB = "admin.example" ;
893+
894+ /* Step 1: full TLS 1.2 handshake under SNI=public.example, with the
895+ * session ticket path disabled so resumption can only happen via the
896+ * server's session-ID cache. */
897+ XMEMSET (& test_ctx , 0 , sizeof (test_ctx ));
898+ ExpectIntEQ (test_memio_setup (& test_ctx , & ctx_c , & ctx_s , & ssl_c , & ssl_s ,
899+ wolfTLSv1_2_client_method , wolfTLSv1_2_server_method ), 0 );
900+ ExpectIntEQ (wolfSSL_NoTicketTLSv12 (ssl_c ), WOLFSSL_SUCCESS );
901+ ExpectIntEQ (wolfSSL_NoTicketTLSv12 (ssl_s ), WOLFSSL_SUCCESS );
902+ ExpectIntEQ (wolfSSL_UseSNI (ssl_c , WOLFSSL_SNI_HOST_NAME ,
903+ sniA , (word16 )XSTRLEN (sniA )), WOLFSSL_SUCCESS );
904+ ExpectIntEQ (test_memio_do_handshake (ssl_c , ssl_s , 10 , NULL ), 0 );
905+ /* Sanity: the first handshake was not a resumption. */
906+ ExpectIntEQ (wolfSSL_session_reused (ssl_s ), 0 );
907+ ExpectNotNull (sess = wolfSSL_get1_session (ssl_c ));
908+
909+ wolfSSL_free (ssl_c ); ssl_c = NULL ;
910+ wolfSSL_free (ssl_s ); ssl_s = NULL ;
911+
912+ /* Step 2: new SSL objects on the SAME WOLFSSL_CTX (so the server's
913+ * session cache still holds the entry from step 1). The client offers
914+ * the saved session but advertises a *different* SNI. The server's
915+ * cache lookup will match by session ID, but per RFC 6066 Section 3 the
916+ * server MUST NOT resume because the SNI differs from the original. */
917+ XMEMSET (& test_ctx , 0 , sizeof (test_ctx ));
918+ ExpectNotNull (ssl_c = wolfSSL_new (ctx_c ));
919+ wolfSSL_SetIOReadCtx (ssl_c , & test_ctx );
920+ wolfSSL_SetIOWriteCtx (ssl_c , & test_ctx );
921+ ExpectNotNull (ssl_s = wolfSSL_new (ctx_s ));
922+ wolfSSL_SetIOReadCtx (ssl_s , & test_ctx );
923+ wolfSSL_SetIOWriteCtx (ssl_s , & test_ctx );
924+ ExpectIntEQ (wolfSSL_NoTicketTLSv12 (ssl_c ), WOLFSSL_SUCCESS );
925+ ExpectIntEQ (wolfSSL_NoTicketTLSv12 (ssl_s ), WOLFSSL_SUCCESS );
926+ ExpectIntEQ (wolfSSL_UseSNI (ssl_c , WOLFSSL_SNI_HOST_NAME ,
927+ sniB , (word16 )XSTRLEN (sniB )), WOLFSSL_SUCCESS );
928+ ExpectIntEQ (wolfSSL_set_session (ssl_c , sess ), WOLFSSL_SUCCESS );
929+ ExpectIntEQ (test_memio_do_handshake (ssl_c , ssl_s , 10 , NULL ), 0 );
930+
931+ /* Post-fix expected behavior: server falls back to a full handshake
932+ * because the SNI in the ClientHello does not match the SNI bound to
933+ * the cached session. Pre-fix, the server silently resumes - which is
934+ * the bug. Both sides should report no resumption. */
935+ ExpectIntEQ (wolfSSL_session_reused (ssl_s ), 0 );
936+ ExpectIntEQ (wolfSSL_session_reused (ssl_c ), 0 );
937+
938+ wolfSSL_SESSION_free (sess );
939+ wolfSSL_free (ssl_c );
940+ wolfSSL_free (ssl_s );
941+ wolfSSL_CTX_free (ctx_c );
942+ wolfSSL_CTX_free (ctx_s );
943+ #endif
944+ return EXPECT_RESULT ();
945+ }
946+
864947int test_tls_set_curves_list_ecc_fallback (void )
865948{
866949 EXPECT_DECLS ;
0 commit comments