Commit fc9cc24967 for openssl.org

commit fc9cc24967b1fbc19ed6f746f3794660654cc608
Author: Sabith Saheb <shabi7204192361@gmail.com>
Date:   Tue Oct 20 00:22:06 2026 +0530

    ECH: keep the TLS 1.2 downgrade sentinel in the ServerHello random

    tls_construct_server_hello() writes the ECH accept confirmation
    into the last 8 bytes of the ServerHello random whenever ECH
    was accepted or the ClientHello carried an inner
    "encrypted_client_hello" extension.  The latter puts any server built
    with ECH into split-mode backend handling, whether or not it has an ECH
    configuration, and the negotiated version is not checked.

    When TLSv1.2 is negotiated by a server that supports TLSv1.3, those 8
    bytes hold the RFC 8446 section 4.1.3 downgrade sentinel.  An on-path
    attacker that strips supported_versions from a TLSv1.3 client's
    ClientHello and adds a 5-byte inner ECH extension therefore gets
    a TLSv1.2 ServerHello without the sentinel, and the client's downgrade
    check in ssl_choose_client_version() does not fire.

    RFC 9849 section 7.2 only has a backend server confirm ECH if it
    negotiates TLSv1.3, so only compute the confirmation in that case.

    Add a test that feeds a TLSv1.2 ClientHello with an inner ECH extension
    to a TLSv1.3-capable server and checks that the sentinel is present.

    Fixes: 6c3edd4f3a8a "Add server-side handling of Encrypted Client Hello"
    Assisted-by: Claude:claude-opus-5-5
    Reviewed-by: Mounir Idrassi <mounir.idrassi@idrix.fr>
    Reviewed-by: Frederik Wedel-Heinen <fwh.openssl@gmail.com>
    Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
    Merge-date: Fri Dec  4 06:54:45 2026
    Merged-from: https://github.com/openssl/openssl/pull/33275

diff --git a/ssl/statem/statem_srvr.c b/ssl/statem/statem_srvr.c
index ff2efa6f58..1287004492 100644
--- a/ssl/statem/statem_srvr.c
+++ b/ssl/statem/statem_srvr.c
@@ -3280,9 +3280,13 @@ CON_FUNC_RETURN tls_construct_server_hello(SSL_CONNECTION *s, WPACKET *pkt)
 #ifndef OPENSSL_NO_ECH
     /*
      * Calculate the ECH-accept server random to indicate that
-     * we're accepting ECH, if that's the case
+     * we're accepting ECH, if that's the case. ECH is only confirmed
+     * if TLSv1.3 is negotiated (RFC 9849, section 7.2): below that
+     * the last 8 bytes of the server random may hold the downgrade
+     * sentinel, which must not be overwritten.
      */
     if (s->ext.ech.attempted_type == TLSEXT_TYPE_ech
+        && usetls13
         && (s->ext.ech.backend == 1
             || (s->ext.ech.es != NULL && s->ext.ech.success == 1))) {
         unsigned char acbuf[8];
diff --git a/test/ech_test.c b/test/ech_test.c
index adbea656d0..81b66ea0e6 100644
--- a/test/ech_test.c
+++ b/test/ech_test.c
@@ -2690,6 +2690,93 @@ end:
     return res;
 }

+/*
+ * A TLSv1.2 ClientHello (no supported_versions) that carries an inner
+ * "encrypted_client_hello" extension, as an on-path attacker could
+ * produce by rewriting a TLSv1.3 client's ClientHello.
+ */
+#ifndef OPENSSL_NO_TLS1_2
+static const unsigned char v12_ch_with_inner_ech[] = {
+    /* record header */
+    0x16, 0x03, 0x01, 0x00, 0x4e,
+    /* ClientHello, length 74 */
+    0x01, 0x00, 0x00, 0x4a,
+    0x03, 0x03,
+    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
+    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
+    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
+    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
+    /* empty session id */
+    0x00,
+    /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */
+    0x00, 0x02, 0xc0, 0x2f,
+    /* null compression */
+    0x01, 0x00,
+    /* extensions, length 31 */
+    0x00, 0x1f,
+    /* supported_groups: secp256r1, x25519 */
+    0x00, 0x0a, 0x00, 0x06, 0x00, 0x04, 0x00, 0x17, 0x00, 0x1d,
+    /* ec_point_formats: uncompressed */
+    0x00, 0x0b, 0x00, 0x02, 0x01, 0x00,
+    /* signature_algorithms: rsa_pss_rsae_sha256, rsa_pkcs1_sha256 */
+    0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x08, 0x04, 0x04, 0x01,
+    /* encrypted_client_hello, type inner */
+    0xfe, 0x0d, 0x00, 0x01, 0x01
+};
+
+/*
+ * A server that supports TLSv1.3 but negotiates TLSv1.2 must put the
+ * RFC 8446 downgrade sentinel in its ServerHello.random, even when the
+ * ClientHello has an inner ECH extension: ECH acceptance is only
+ * signalled with TLSv1.3 and must not overwrite the sentinel.
+ */
+static int ech_v12_downgrade_sentinel_test(void)
+{
+    static const unsigned char dgrd[8] = {
+        0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44, 0x01
+    };
+    int res = 0;
+    SSL_CTX *sctx = NULL;
+    SSL *serverssl = NULL;
+    BIO *rbio = NULL, *wbio = NULL;
+    unsigned char srandom[SSL3_RANDOM_SIZE];
+
+    if (!TEST_ptr(sctx = SSL_CTX_new_ex(libctx, propq, TLS_server_method()))
+        || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_3_VERSION))
+        || !TEST_true(SSL_CTX_use_certificate_file(sctx, cert,
+            SSL_FILETYPE_PEM))
+        || !TEST_true(SSL_CTX_use_PrivateKey_file(sctx, privkey,
+            SSL_FILETYPE_PEM))
+        || !TEST_ptr(serverssl = SSL_new(sctx))
+        || !TEST_ptr(rbio = BIO_new(BIO_s_mem()))
+        || !TEST_ptr(wbio = BIO_new(BIO_s_mem())))
+        goto end;
+    SSL_set_bio(serverssl, rbio, wbio);
+    rbio = wbio = NULL;
+    if (!TEST_int_eq(BIO_write(SSL_get_rbio(serverssl), v12_ch_with_inner_ech,
+                         sizeof(v12_ch_with_inner_ech)),
+            (int)sizeof(v12_ch_with_inner_ech))
+        /* The server writes its first flight then waits for the client */
+        || !TEST_int_le(SSL_accept(serverssl), 0)
+        || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_READ)
+        || !TEST_int_eq(SSL_version(serverssl), TLS1_2_VERSION)
+        || !TEST_size_t_eq(SSL_get_server_random(serverssl, srandom,
+                               sizeof(srandom)),
+            sizeof(srandom))
+        || !TEST_mem_eq(srandom + sizeof(srandom) - sizeof(dgrd), sizeof(dgrd),
+            dgrd, sizeof(dgrd)))
+        goto end;
+    res = 1;
+end:
+    BIO_free(rbio);
+    BIO_free(wbio);
+    SSL_free(serverssl);
+    SSL_CTX_free(sctx);
+    return res;
+}
+
+#endif /* OPENSSL_NO_TLS1_2 */
+
 #endif

 int setup_tests(void)
@@ -2744,6 +2831,9 @@ int setup_tests(void)
     ADD_ALL_TESTS(test_ech_keylog_random, 4);
     ADD_TEST(ech_unauth_client_status_test);
     ADD_ALL_TESTS(test_ech_cb_server_random, 3);
+#ifndef OPENSSL_NO_TLS1_2
+    ADD_TEST(ech_v12_downgrade_sentinel_test);
+#endif
     return 1;
 err:
     return 0;