When the client sent no key_share for the group chosen by the server,
the TLSv1.3 server answers with a HelloRetryRequest and the handshake
takes one more round trip. This is of interest with the post-quantum
groups: with AWS-LC, a bind line whose curves start with X25519MLKEM768
sends one to each client which supports this group but only sent a
key_share for X25519.

There is no common way to know it afterwards: AWS-LC and BoringSSL have
SSL_used_hello_retry_request() but OpenSSL has no such function.

A HelloRetryRequest is a ServerHello whose random has a fixed value
(RFC 8446, 4.1.3), so let's look for it from a message callback and set
the new flag SSL_SOCK_F_HRR when one is sent on the frontend side or
received on the backend side. This also covers QUIC.

The callback is always registered since all it does on a message is a
few comparisons.
---
 include/haproxy/ssl_sock-t.h |  1 +
 src/ssl_sock.c               | 44 ++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/include/haproxy/ssl_sock-t.h b/include/haproxy/ssl_sock-t.h
index d3af63bb0..8d788b5aa 100644
--- a/include/haproxy/ssl_sock-t.h
+++ b/include/haproxy/ssl_sock-t.h
@@ -258,6 +258,7 @@ struct ssl_keylog {
 #define SSL_SOCK_F_HAS_ALPN             (1 << 5) /* An ALPN has been 
negotiated */
 #define SSL_SOCK_F_KTLS_ULP             (1 << 6) /* TLS ULP is enabled on that 
socket */
 #define SSL_SOCK_F_KTLS_RX_CTRL         (1 << 7) /* a non-app-data record is 
blocking kTLS splicing, fall back to a buffered read to consume it */
+#define SSL_SOCK_F_HRR                  (1 << 8) /* a HelloRetryRequest was 
sent or received during the handshake */
 #define SSL_SOCK_F_KILL                 (1 << 9) /* connection must be aborted 
(e.g. KeyUpdate flood) */
 
 struct ssl_sock_ctx {
diff --git a/src/ssl_sock.c b/src/ssl_sock.c
index 90da22762..1786f98fc 100644
--- a/src/ssl_sock.c
+++ b/src/ssl_sock.c
@@ -887,6 +887,10 @@ static void ssl_sock_keyupdate_ratelimit(int write_p, int 
version,
                                           int content_type, const void *buf,
                                           size_t len, SSL *ssl);
 
+static void ssl_sock_parse_hrr(int write_p, int version,
+                               int content_type, const void *buf, size_t len,
+                               SSL *ssl);
+
 /* List head of all registered SSL/TLS protocol message callbacks. */
 struct list ssl_sock_msg_callbacks = LIST_HEAD_INIT(ssl_sock_msg_callbacks);
 
@@ -936,6 +940,8 @@ static int ssl_sock_register_msg_callbacks(void)
                if 
(!ssl_sock_register_msg_callback(ssl_sock_keyupdate_ratelimit))
                        return ERR_ABORT;
        }
+       if (!ssl_sock_register_msg_callback(ssl_sock_parse_hrr))
+               return ERR_ABORT;
 #ifdef USE_QUIC_OPENSSL_COMPAT
        if (!ssl_sock_register_msg_callback(quic_tls_compat_msg_callback))
                return ERR_ABORT;
@@ -2419,6 +2425,44 @@ static void ssl_sock_keyupdate_ratelimit(int write_p, 
int version,
        }
 }
 
+/* The value of ServerHello.random which makes a ServerHello message a
+ * HelloRetryRequest (RFC 8446, 4.1.3).
+ */
+static const unsigned char ssl_sock_hrr_random[] = {
+       0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11,
+       0xbe, 0x1d, 0x8c, 0x02, 0x1e, 0x65, 0xb8, 0x91,
+       0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e,
+       0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c,
+};
+
+/* Sets SSL_SOCK_F_HRR on the connection when a HelloRetryRequest is sent to
+ * the client or received from the server. The SSL libraries have no common
+ * function to report it, hence the message callback.
+ */
+static void ssl_sock_parse_hrr(int write_p, int version,
+                               int content_type, const void *buf, size_t len,
+                               SSL *ssl)
+{
+       const unsigned char *msg = buf;
+       struct ssl_sock_ctx *ctx = NULL;
+
+       /* the random follows the handshake header (4 bytes) and the
+        * legacy_version field (2 bytes).
+        */
+       if (content_type != SSL3_RT_HANDSHAKE || len < 6 + 
sizeof(ssl_sock_hrr_random))
+               return;
+       if (msg[0] != 2 /* SSL3_MT_SERVER_HELLO */)
+               return;
+
+       if (memcmp(msg + 6, ssl_sock_hrr_random, sizeof(ssl_sock_hrr_random)) 
!= 0)
+               return;
+
+       /* a QUIC frontend has no connection yet at this point, only <ctx> */
+       ssl_sock_get_conn(ssl, &ctx);
+       if (ctx)
+               ctx->flags |= SSL_SOCK_F_HRR;
+}
+
 /* Callback is called for ssl protocol analyse */
 static __maybe_unused void ssl_sock_msgcbk(int write_p, int version, int 
content_type, const void *buf, size_t len, SSL *ssl, void *arg)
 {
-- 
2.50.1 (Apple Git-155)



Reply via email to