@@ -3352,6 +3352,98 @@ int test_tls13_warning_alert_is_fatal(void)
33523352 return EXPECT_RESULT ();
33533353}
33543354
3355+ /* Test that an unknown extension in a TLS 1.3 server-to-client message is
3356+ * rejected with unsupported_extension (RFC 8446 Sec. 4.2). The client MUST
3357+ * abort the handshake when it receives an extension it did not advertise.
3358+ */
3359+ int test_tls13_unknown_ext_rejected (void )
3360+ {
3361+ EXPECT_DECLS ;
3362+ #if defined(WOLFSSL_TLS13 ) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES ) && \
3363+ !defined(NO_WOLFSSL_CLIENT ) && defined(WOLFSSL_AES_128 ) && \
3364+ defined(HAVE_AESGCM ) && !defined(NO_SHA256 ) && \
3365+ !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT )
3366+ WOLFSSL_CTX * ctx_c = NULL ;
3367+ WOLFSSL * ssl_c = NULL ;
3368+ struct test_memio_ctx test_ctx ;
3369+ /* HelloRetryRequest carrying TLS_AES_128_GCM_SHA256, supported_versions
3370+ * (TLS 1.3), and an extra unknown extension type 0xFABC.
3371+ *
3372+ * The base HRR (from test_tls13_same_ch) extended with 4 bytes:
3373+ * extensions length: 6 -> 10 (0x00,0x0a)
3374+ * handshake body length: 46 -> 50 (0x00,0x00,0x32)
3375+ * record body length: 50 -> 54 (0x00,0x36)
3376+ * appended: 0xfa,0xbc,0x00,0x00 (unknown type, zero-length value)
3377+ */
3378+ static const unsigned char hrr_unknown_ext [] = {
3379+ /* TLS record header: handshake, TLS 1.2 compat, len=54 */
3380+ 0x16 , 0x03 , 0x03 , 0x00 , 0x36 ,
3381+ /* Handshake header: ServerHello, len=50 */
3382+ 0x02 , 0x00 , 0x00 , 0x32 ,
3383+ /* legacy_version: TLS 1.2 */
3384+ 0x03 , 0x03 ,
3385+ /* HelloRetryRequest magic random */
3386+ 0xcf , 0x21 , 0xad , 0x74 , 0xe5 , 0x9a , 0x61 , 0x11 ,
3387+ 0xbe , 0x1d , 0x8c , 0x02 , 0x1e , 0x65 , 0xb8 , 0x91 ,
3388+ 0xc2 , 0xa2 , 0x11 , 0x16 , 0x7a , 0xbb , 0x8c , 0x5e ,
3389+ 0x07 , 0x9e , 0x09 , 0xe2 , 0xc8 , 0xa8 , 0x33 , 0x9c ,
3390+ /* session ID length: 0 */
3391+ 0x00 ,
3392+ /* cipher suite: TLS_AES_128_GCM_SHA256 */
3393+ 0x13 , 0x01 ,
3394+ /* compression: null */
3395+ 0x00 ,
3396+ /* extensions length: 10 */
3397+ 0x00 , 0x0a ,
3398+ /* supported_versions: TLS 1.3 (0x0304) */
3399+ 0x00 , 0x2b , 0x00 , 0x02 , 0x03 , 0x04 ,
3400+ /* unknown extension type 0xFABC, zero-length value */
3401+ 0xfa , 0xbc , 0x00 , 0x00
3402+ };
3403+
3404+ XMEMSET (& test_ctx , 0 , sizeof (test_ctx ));
3405+ ExpectIntEQ (test_memio_setup (& test_ctx , & ctx_c , NULL , & ssl_c , NULL ,
3406+ wolfTLSv1_3_client_method , NULL ), 0 );
3407+
3408+ /* Inject the crafted HRR before the client starts the handshake.
3409+ * wolfSSL_connect will send the ClientHello and then read this message. */
3410+ ExpectIntEQ (test_memio_inject_message (& test_ctx , 1 ,
3411+ (const char * )hrr_unknown_ext , sizeof (hrr_unknown_ext )), 0 );
3412+
3413+ /* RFC 8446 Sec. 4.2: the client MUST abort with unsupported_extension. */
3414+ ExpectIntEQ (wolfSSL_connect (ssl_c ), -1 );
3415+ ExpectIntEQ (wolfSSL_get_error (ssl_c , -1 ),
3416+ WC_NO_ERR_TRACE (UNSUPPORTED_EXTENSION ));
3417+
3418+ /* The client MUST also transmit the fatal unsupported_extension alert
3419+ * on the wire, not merely surface a local error. The client's outgoing
3420+ * data lands in test_ctx.s_buff; at this point in the handshake no
3421+ * traffic keys are derived yet, so the alert record is plaintext.
3422+ * Expected record: type=alert(0x15), version=TLS1.2(0x0303), len=2,
3423+ * level=fatal(0x02), description=unsupported_extension(0x6e=110). */
3424+ {
3425+ static const unsigned char expected_alert [] =
3426+ { 0x15 , 0x03 , 0x03 , 0x00 , 0x02 , 0x02 , 0x6e };
3427+ int found = 0 ;
3428+ int i ;
3429+ for (i = 0 ;
3430+ i + (int )sizeof (expected_alert ) <= test_ctx .s_len ;
3431+ i ++ ) {
3432+ if (XMEMCMP (test_ctx .s_buff + i , expected_alert ,
3433+ sizeof (expected_alert )) == 0 ) {
3434+ found = 1 ;
3435+ break ;
3436+ }
3437+ }
3438+ ExpectIntEQ (found , 1 );
3439+ }
3440+
3441+ wolfSSL_free (ssl_c );
3442+ wolfSSL_CTX_free (ctx_c );
3443+ #endif
3444+ return EXPECT_RESULT ();
3445+ }
3446+
33553447/* Test that wolfSSL_set1_sigalgs_list() is honored in TLS 1.3
33563448 */
33573449int test_tls13_cert_req_sigalgs (void )
0 commit comments