@@ -5575,6 +5575,127 @@ int test_tls13_zero_inner_content_type(void)
55755575 return EXPECT_RESULT ();
55765576}
55775577
5578+ /* RFC 8446 Section 4.6.2: A client that receives a post-handshake
5579+ * CertificateRequest message without having sent the "post_handshake_auth"
5580+ * extension MUST send an "unexpected_message" fatal alert.
5581+ *
5582+ * This test completes a TLS 1.3 handshake in which the client never enabled
5583+ * post-handshake auth (so no extension was sent in the ClientHello), then
5584+ * forces the server to transmit a post-handshake CertificateRequest anyway.
5585+ * The client must reject the message with an unexpected_message fatal
5586+ * alert. */
5587+ int test_tls13_post_handshake_auth_no_ext (void )
5588+ {
5589+ EXPECT_DECLS ;
5590+ #if defined(WOLFSSL_TLS13 ) && defined(WOLFSSL_POST_HANDSHAKE_AUTH ) && \
5591+ defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES ) && \
5592+ !defined(NO_WOLFSSL_CLIENT ) && !defined(NO_WOLFSSL_SERVER )
5593+ WOLFSSL_CTX * ctx_c = NULL ;
5594+ WOLFSSL_CTX * ctx_s = NULL ;
5595+ WOLFSSL * ssl_c = NULL ;
5596+ WOLFSSL * ssl_s = NULL ;
5597+ struct test_memio_ctx test_ctx ;
5598+ WOLFSSL_ALERT_HISTORY h ;
5599+ char readBuf [8 ];
5600+
5601+ XMEMSET (& test_ctx , 0 , sizeof (test_ctx ));
5602+ XMEMSET (& h , 0 , sizeof (h ));
5603+ ExpectIntEQ (test_memio_setup (& test_ctx , & ctx_c , & ctx_s , & ssl_c , & ssl_s ,
5604+ wolfTLSv1_3_client_method , wolfTLSv1_3_server_method ), 0 );
5605+ /* Keep the post-handshake byte stream simple by suppressing the server's
5606+ * NewSessionTicket so the only post-handshake record from the server is
5607+ * the CertificateRequest under test. */
5608+ ExpectIntEQ (wolfSSL_no_ticket_TLSv13 (ssl_s ), 0 );
5609+
5610+ /* Intentionally do NOT call wolfSSL_allow_post_handshake_auth() on the
5611+ * client so the post_handshake_auth extension is omitted from the
5612+ * ClientHello. */
5613+ ExpectIntEQ (test_memio_do_handshake (ssl_c , ssl_s , 10 , NULL ), 0 );
5614+
5615+ /* The server's wolfSSL_request_certificate() refuses to send a
5616+ * post-handshake CertificateRequest unless its postHandshakeAuth flag is
5617+ * set (normally set when parsing the client's extension). To exercise
5618+ * the receive-side check we are testing, simulate a server that sends
5619+ * one anyway by toggling the flag directly. */
5620+ if (ssl_s != NULL )
5621+ ssl_s -> options .postHandshakeAuth = 1 ;
5622+ /* OPENSSL_COMPATIBLE_DEFAULTS may leave groupMessages set on ssl_s; that
5623+ * suppresses SendBuffered() for the post-handshake CertificateRequest. */
5624+ ExpectIntEQ (wolfSSL_clear_group_messages (ssl_s ), WOLFSSL_SUCCESS );
5625+ ExpectIntEQ (wolfSSL_request_certificate (ssl_s ), WOLFSSL_SUCCESS );
5626+ ExpectIntGT (test_ctx .c_len , 0 );
5627+
5628+ /* The client must reject the unsolicited CertificateRequest. */
5629+ ExpectIntEQ (wolfSSL_read (ssl_c , readBuf , (int )sizeof (readBuf )),
5630+ WOLFSSL_FATAL_ERROR );
5631+ ExpectIntEQ (wolfSSL_get_error (ssl_c , WOLFSSL_FATAL_ERROR ),
5632+ WC_NO_ERR_TRACE (OUT_OF_ORDER_E ));
5633+
5634+ /* And the client must transmit a fatal unexpected_message alert. */
5635+ ExpectIntEQ (wolfSSL_get_alert_history (ssl_c , & h ), WOLFSSL_SUCCESS );
5636+ ExpectIntEQ (h .last_tx .code , unexpected_message );
5637+ ExpectIntEQ (h .last_tx .level , alert_fatal );
5638+
5639+ wolfSSL_free (ssl_c );
5640+ wolfSSL_CTX_free (ctx_c );
5641+ wolfSSL_free (ssl_s );
5642+ wolfSSL_CTX_free (ctx_s );
5643+ #endif
5644+ return EXPECT_RESULT ();
5645+ }
5646+
5647+ /* wolfSSL_allow_post_handshake_auth() must be called before the handshake
5648+ * starts. A late call must fail with BAD_STATE_E and must not enable
5649+ * post-handshake authentication for the connection. */
5650+ int test_tls13_post_handshake_auth_late_allow (void )
5651+ {
5652+ EXPECT_DECLS ;
5653+ #if defined(WOLFSSL_TLS13 ) && defined(WOLFSSL_POST_HANDSHAKE_AUTH ) && \
5654+ defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES ) && \
5655+ !defined(NO_WOLFSSL_CLIENT ) && !defined(NO_WOLFSSL_SERVER )
5656+ WOLFSSL_CTX * ctx_c = NULL ;
5657+ WOLFSSL_CTX * ctx_s = NULL ;
5658+ WOLFSSL * ssl_c = NULL ;
5659+ WOLFSSL * ssl_s = NULL ;
5660+ struct test_memio_ctx test_ctx ;
5661+ WOLFSSL_ALERT_HISTORY h ;
5662+ char readBuf [8 ];
5663+
5664+ XMEMSET (& test_ctx , 0 , sizeof (test_ctx ));
5665+ XMEMSET (& h , 0 , sizeof (h ));
5666+ ExpectIntEQ (test_memio_setup (& test_ctx , & ctx_c , & ctx_s , & ssl_c , & ssl_s ,
5667+ wolfTLSv1_3_client_method , wolfTLSv1_3_server_method ), 0 );
5668+ ExpectIntEQ (wolfSSL_no_ticket_TLSv13 (ssl_s ), 0 );
5669+ ExpectIntEQ (test_memio_do_handshake (ssl_c , ssl_s , 10 , NULL ), 0 );
5670+
5671+ /* Too late: handshake is complete. */
5672+ ExpectIntEQ (wolfSSL_allow_post_handshake_auth (ssl_c ),
5673+ WC_NO_ERR_TRACE (BAD_STATE_E ));
5674+
5675+ if (ssl_s != NULL )
5676+ ssl_s -> options .postHandshakeAuth = 1 ;
5677+ /* OPENSSL_COMPATIBLE_DEFAULTS may leave groupMessages set on ssl_s; that
5678+ * suppresses SendBuffered() for the post-handshake CertificateRequest. */
5679+ ExpectIntEQ (wolfSSL_clear_group_messages (ssl_s ), WOLFSSL_SUCCESS );
5680+ ExpectIntEQ (wolfSSL_request_certificate (ssl_s ), WOLFSSL_SUCCESS );
5681+ ExpectIntGT (test_ctx .c_len , 0 );
5682+
5683+ ExpectIntEQ (wolfSSL_read (ssl_c , readBuf , (int )sizeof (readBuf )),
5684+ WOLFSSL_FATAL_ERROR );
5685+ ExpectIntEQ (wolfSSL_get_error (ssl_c , WOLFSSL_FATAL_ERROR ),
5686+ WC_NO_ERR_TRACE (OUT_OF_ORDER_E ));
5687+ ExpectIntEQ (wolfSSL_get_alert_history (ssl_c , & h ), WOLFSSL_SUCCESS );
5688+ ExpectIntEQ (h .last_tx .code , unexpected_message );
5689+ ExpectIntEQ (h .last_tx .level , alert_fatal );
5690+
5691+ wolfSSL_free (ssl_c );
5692+ wolfSSL_CTX_free (ctx_c );
5693+ wolfSSL_free (ssl_s );
5694+ wolfSSL_CTX_free (ctx_s );
5695+ #endif
5696+ return EXPECT_RESULT ();
5697+ }
5698+
55785699/* Test that a TLS 1.3-capable client rejects downgrade sentinels in a
55795700 * downgraded ServerHello random for both TLS 1.2 and TLS 1.1-or-lower. */
55805701int test_tls13_downgrade_sentinel (void )
0 commit comments