@@ -861,6 +861,93 @@ 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(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+ #ifdef HAVE_SESSION_TICKET
901+ ExpectIntEQ (wolfSSL_NoTicketTLSv12 (ssl_c ), WOLFSSL_SUCCESS );
902+ ExpectIntEQ (wolfSSL_NoTicketTLSv12 (ssl_s ), WOLFSSL_SUCCESS );
903+ #endif
904+ ExpectIntEQ (wolfSSL_UseSNI (ssl_c , WOLFSSL_SNI_HOST_NAME ,
905+ sniA , (word16 )XSTRLEN (sniA )), WOLFSSL_SUCCESS );
906+ ExpectIntEQ (test_memio_do_handshake (ssl_c , ssl_s , 10 , NULL ), 0 );
907+ /* Sanity: the first handshake was not a resumption. */
908+ ExpectIntEQ (wolfSSL_session_reused (ssl_s ), 0 );
909+ ExpectNotNull (sess = wolfSSL_get1_session (ssl_c ));
910+
911+ wolfSSL_free (ssl_c ); ssl_c = NULL ;
912+ wolfSSL_free (ssl_s ); ssl_s = NULL ;
913+
914+ /* Step 2: new SSL objects on the SAME WOLFSSL_CTX (so the server's
915+ * session cache still holds the entry from step 1). The client offers
916+ * the saved session but advertises a *different* SNI. The server's
917+ * cache lookup will match by session ID, but per RFC 6066 Section 3 the
918+ * server MUST NOT resume because the SNI differs from the original. */
919+ XMEMSET (& test_ctx , 0 , sizeof (test_ctx ));
920+ ExpectNotNull (ssl_c = wolfSSL_new (ctx_c ));
921+ wolfSSL_SetIOReadCtx (ssl_c , & test_ctx );
922+ wolfSSL_SetIOWriteCtx (ssl_c , & test_ctx );
923+ ExpectNotNull (ssl_s = wolfSSL_new (ctx_s ));
924+ wolfSSL_SetIOReadCtx (ssl_s , & test_ctx );
925+ wolfSSL_SetIOWriteCtx (ssl_s , & test_ctx );
926+ #ifdef HAVE_SESSION_TICKET
927+ ExpectIntEQ (wolfSSL_NoTicketTLSv12 (ssl_c ), WOLFSSL_SUCCESS );
928+ ExpectIntEQ (wolfSSL_NoTicketTLSv12 (ssl_s ), WOLFSSL_SUCCESS );
929+ #endif
930+ ExpectIntEQ (wolfSSL_UseSNI (ssl_c , WOLFSSL_SNI_HOST_NAME ,
931+ sniB , (word16 )XSTRLEN (sniB )), WOLFSSL_SUCCESS );
932+ ExpectIntEQ (wolfSSL_set_session (ssl_c , sess ), WOLFSSL_SUCCESS );
933+ ExpectIntEQ (test_memio_do_handshake (ssl_c , ssl_s , 10 , NULL ), 0 );
934+
935+ /* Post-fix expected behavior: server falls back to a full handshake
936+ * because the SNI in the ClientHello does not match the SNI bound to
937+ * the cached session. Pre-fix, the server silently resumes - which is
938+ * the bug. Both sides should report no resumption. */
939+ ExpectIntEQ (wolfSSL_session_reused (ssl_s ), 0 );
940+ ExpectIntEQ (wolfSSL_session_reused (ssl_c ), 0 );
941+
942+ wolfSSL_SESSION_free (sess );
943+ wolfSSL_free (ssl_c );
944+ wolfSSL_free (ssl_s );
945+ wolfSSL_CTX_free (ctx_c );
946+ wolfSSL_CTX_free (ctx_s );
947+ #endif
948+ return EXPECT_RESULT ();
949+ }
950+
864951int test_tls_set_curves_list_ecc_fallback (void )
865952{
866953 EXPECT_DECLS ;
0 commit comments