@@ -905,6 +905,88 @@ int test_tls_set_session_min_downgrade(void)
905905 return EXPECT_RESULT ();
906906}
907907
908+ #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES ) && \
909+ !defined(WOLFSSL_NO_TLS12 ) && defined(HAVE_SNI ) && \
910+ defined(HAVE_SESSION_TICKET ) && !defined(NO_SESSION_CACHE )
911+ /* Accept-all SNI callback. */
912+ static int accept_any_sni_cb (WOLFSSL * ssl , int * ret , void * arg )
913+ {
914+ (void )ssl ; (void )ret ; (void )arg ;
915+ return 0 ; /* accept */
916+ }
917+ #endif
918+
919+ /* TLS resumption must proceed with full handshake to establish new session if
920+ * SNI/ALPN does not match previously established session. */
921+ int test_tls12_session_id_resumption_sni_mismatch (void )
922+ {
923+ EXPECT_DECLS ;
924+ #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES ) && \
925+ !defined(WOLFSSL_NO_TLS12 ) && defined(HAVE_SNI ) && \
926+ defined(HAVE_SESSION_TICKET ) && !defined(NO_SESSION_CACHE )
927+ WOLFSSL_CTX * ctx_c = NULL , * ctx_s = NULL ;
928+ WOLFSSL * ssl_c = NULL , * ssl_s = NULL ;
929+ WOLFSSL_SESSION * sess = NULL ;
930+ struct test_memio_ctx test_ctx ;
931+ const char * sniA = "public.example" ;
932+ const char * sniB = "admin.example" ;
933+
934+ /* Step 1: full TLS 1.2 handshake under SNI=public.example, with the
935+ * session ticket path disabled so resumption can only happen via the
936+ * server's session-ID cache. The server-side SNI callback ensures
937+ * ssl->extensions retains the client's SNI in builds that don't
938+ * compile in WOLFSSL_ALWAYS_KEEP_SNI. */
939+ XMEMSET (& test_ctx , 0 , sizeof (test_ctx ));
940+ ExpectIntEQ (test_memio_setup (& test_ctx , & ctx_c , & ctx_s , & ssl_c , & ssl_s ,
941+ wolfTLSv1_2_client_method , wolfTLSv1_2_server_method ), 0 );
942+ wolfSSL_CTX_set_servername_callback (ctx_s , accept_any_sni_cb );
943+ ExpectIntEQ (wolfSSL_NoTicketTLSv12 (ssl_c ), WOLFSSL_SUCCESS );
944+ ExpectIntEQ (wolfSSL_NoTicketTLSv12 (ssl_s ), WOLFSSL_SUCCESS );
945+ ExpectIntEQ (wolfSSL_UseSNI (ssl_c , WOLFSSL_SNI_HOST_NAME ,
946+ sniA , (word16 )XSTRLEN (sniA )), WOLFSSL_SUCCESS );
947+ ExpectIntEQ (test_memio_do_handshake (ssl_c , ssl_s , 10 , NULL ), 0 );
948+ /* Sanity: the first handshake was not a resumption. */
949+ ExpectIntEQ (wolfSSL_session_reused (ssl_s ), 0 );
950+ ExpectNotNull (sess = wolfSSL_get1_session (ssl_c ));
951+
952+ wolfSSL_free (ssl_c ); ssl_c = NULL ;
953+ wolfSSL_free (ssl_s ); ssl_s = NULL ;
954+
955+ /* Step 2: new SSL objects on the SAME WOLFSSL_CTX (so the server's
956+ * session cache still holds the entry from step 1). The client offers
957+ * the saved session but advertises a *different* SNI. The server's
958+ * cache lookup will match by session ID, but per RFC 6066 Section 3 the
959+ * server MUST NOT resume because the SNI differs from the original. */
960+ XMEMSET (& test_ctx , 0 , sizeof (test_ctx ));
961+ ExpectNotNull (ssl_c = wolfSSL_new (ctx_c ));
962+ wolfSSL_SetIOReadCtx (ssl_c , & test_ctx );
963+ wolfSSL_SetIOWriteCtx (ssl_c , & test_ctx );
964+ ExpectNotNull (ssl_s = wolfSSL_new (ctx_s ));
965+ wolfSSL_SetIOReadCtx (ssl_s , & test_ctx );
966+ wolfSSL_SetIOWriteCtx (ssl_s , & test_ctx );
967+ ExpectIntEQ (wolfSSL_NoTicketTLSv12 (ssl_c ), WOLFSSL_SUCCESS );
968+ ExpectIntEQ (wolfSSL_NoTicketTLSv12 (ssl_s ), WOLFSSL_SUCCESS );
969+ ExpectIntEQ (wolfSSL_UseSNI (ssl_c , WOLFSSL_SNI_HOST_NAME ,
970+ sniB , (word16 )XSTRLEN (sniB )), WOLFSSL_SUCCESS );
971+ ExpectIntEQ (wolfSSL_set_session (ssl_c , sess ), WOLFSSL_SUCCESS );
972+ ExpectIntEQ (test_memio_do_handshake (ssl_c , ssl_s , 10 , NULL ), 0 );
973+
974+ /* Post-fix expected behavior: server falls back to a full handshake
975+ * because the SNI in the ClientHello does not match the SNI bound to
976+ * the cached session. Pre-fix, the server silently resumes - which is
977+ * the bug. Both sides should report no resumption. */
978+ ExpectIntEQ (wolfSSL_session_reused (ssl_s ), 0 );
979+ ExpectIntEQ (wolfSSL_session_reused (ssl_c ), 0 );
980+
981+ wolfSSL_SESSION_free (sess );
982+ wolfSSL_free (ssl_c );
983+ wolfSSL_free (ssl_s );
984+ wolfSSL_CTX_free (ctx_c );
985+ wolfSSL_CTX_free (ctx_s );
986+ #endif
987+ return EXPECT_RESULT ();
988+ }
989+
908990int test_tls_set_curves_list_ecc_fallback (void )
909991{
910992 EXPECT_DECLS ;
0 commit comments