Add most of an ECH client implementation.
Based on an initial implementation by Dan McArdle at
https://boringssl-review.googlesource.com/c/boringssl/+/46784
This CL contains most of a client implementation for
draft-ietf-tls-esni-10. The pieces missing so far, which will be done in
follow-up CLs are:
1. While the ClientHelloInner is padded, the server Certificate message
is not. I'll add that once we resolve the spec discussions on how to
do that. (We were originally going to use TLS record-level padding,
but that doesn't work well with QUIC.)
2. The client should check the public name is a valid DNS name before
copying it into ClientHelloOuter.server_name.
3. The ClientHelloOuter handshake flow is not yet implemented. This CL
can detect when the server selects ClientHelloOuter, but for now the
handshake immediately fails. A follow-up CL will remove that logic
and instead add the APIs and extra checks needed.
Otherwise, this should be complete, including padding and compression.
The main interesting point design-wise is that we run through
ClientHello construction multiple times. We need to construct
ClientHelloInner and ClientHelloOuter. Then each of those has slight
variants: EncodedClientHelloInner is the compressed form, and
ClientHelloOuterAAD just has the ECH extension erased to avoid a
circular dependency.
I've computed ClientHelloInner and EncodedClientHelloInner concurrently
because the compression scheme requires shifting the extensions around
to be contiguous. However, I've computed ClientHelloOuterAAD and
ClientHelloOuter by running through the logic twice. This probably can
be done better, but the next draft revises the construction anyway, so
I'm thinking I'll rework it then. (In the next draft, we use a
placeholder payload of the same length, so we can construct the
ClientHello once and fill in the payload.)
Additionally, now that we have a client available in ssl_test, this adds
a threading test to confirm that SSL_CTX_set1_ech_keys is properly
synchronized. (Confirmed that, if I drop the lock in
SSL_CTX_set1_ech_keys, TSan notices.)
Change-Id: Icaff68b595035bdcc73c468ff638e67c84239ef4
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/48004
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/ssl/handshake_client.cc b/ssl/handshake_client.cc
index 1d6bc0a..ae8aabd 100644
--- a/ssl/handshake_client.cc
+++ b/ssl/handshake_client.cc
@@ -162,6 +162,7 @@
#include <openssl/ecdsa.h>
#include <openssl/err.h>
#include <openssl/evp.h>
+#include <openssl/hpke.h>
#include <openssl/md5.h>
#include <openssl/mem.h>
#include <openssl/rand.h>
@@ -201,7 +202,8 @@
// ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
// disabled algorithms.
-static void ssl_get_client_disabled(SSL_HANDSHAKE *hs, uint32_t *out_mask_a,
+static void ssl_get_client_disabled(const SSL_HANDSHAKE *hs,
+ uint32_t *out_mask_a,
uint32_t *out_mask_k) {
*out_mask_a = 0;
*out_mask_k = 0;
@@ -213,8 +215,9 @@
}
}
-static bool ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) {
- SSL *const ssl = hs->ssl;
+static bool ssl_write_client_cipher_list(const SSL_HANDSHAKE *hs, CBB *out,
+ ssl_client_hello_type_t type) {
+ const SSL *const ssl = hs->ssl;
uint32_t mask_a, mask_k;
ssl_get_client_disabled(hs, &mask_a, &mask_k);
@@ -246,7 +249,7 @@
}
}
- if (hs->min_version < TLS1_3_VERSION) {
+ if (hs->min_version < TLS1_3_VERSION && type != ssl_client_hello_inner) {
bool any_enabled = false;
for (const SSL_CIPHER *cipher : SSL_get_ciphers(ssl)) {
// Skip disabled ciphers
@@ -280,53 +283,72 @@
return CBB_flush(out);
}
-bool ssl_write_client_hello(SSL_HANDSHAKE *hs) {
- SSL *const ssl = hs->ssl;
- ScopedCBB cbb;
- CBB body;
- if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) {
- return false;
- }
-
+bool ssl_write_client_hello_without_extensions(const SSL_HANDSHAKE *hs,
+ CBB *cbb,
+ ssl_client_hello_type_t type,
+ bool empty_session_id) {
+ const SSL *const ssl = hs->ssl;
CBB child;
- if (!CBB_add_u16(&body, hs->client_version) ||
- !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
- !CBB_add_u8_length_prefixed(&body, &child)) {
+ if (!CBB_add_u16(cbb, hs->client_version) ||
+ !CBB_add_bytes(cbb,
+ type == ssl_client_hello_inner ? hs->inner_client_random
+ : ssl->s3->client_random,
+ SSL3_RANDOM_SIZE) ||
+ !CBB_add_u8_length_prefixed(cbb, &child)) {
return false;
}
// Do not send a session ID on renegotiation.
if (!ssl->s3->initial_handshake_complete &&
+ !empty_session_id &&
!CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
return false;
}
if (SSL_is_dtls(ssl)) {
- if (!CBB_add_u8_length_prefixed(&body, &child) ||
+ if (!CBB_add_u8_length_prefixed(cbb, &child) ||
!CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
return false;
}
}
- bool needs_psk_binder;
- if (!ssl_write_client_cipher_list(hs, &body) ||
- !CBB_add_u8(&body, 1 /* one compression method */) ||
- !CBB_add_u8(&body, 0 /* null compression */) ||
- !ssl_add_clienthello_tlsext(hs, &body, &needs_psk_binder,
- CBB_len(&body))) {
+ if (!ssl_write_client_cipher_list(hs, cbb, type) ||
+ !CBB_add_u8(cbb, 1 /* one compression method */) ||
+ !CBB_add_u8(cbb, 0 /* null compression */)) {
return false;
}
+ return true;
+}
+bool ssl_add_client_hello(SSL_HANDSHAKE *hs) {
+ SSL *const ssl = hs->ssl;
+ ScopedCBB cbb;
+ CBB body;
+ ssl_client_hello_type_t type = hs->selected_ech_config
+ ? ssl_client_hello_outer
+ : ssl_client_hello_unencrypted;
+ bool needs_psk_binder;
Array<uint8_t> msg;
- if (!ssl->method->finish_message(ssl, cbb.get(), &msg)) {
+ if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO) ||
+ !ssl_write_client_hello_without_extensions(hs, &body, type,
+ /*empty_session_id*/ false) ||
+ !ssl_add_clienthello_tlsext(hs, &body, /*out_encoded=*/nullptr,
+ &needs_psk_binder, type, CBB_len(&body),
+ /*omit_ech_len=*/0) ||
+ !ssl->method->finish_message(ssl, cbb.get(), &msg)) {
return false;
}
// Now that the length prefixes have been computed, fill in the placeholder
// PSK binder.
- if (needs_psk_binder &&
- !tls13_write_psk_binder(hs, MakeSpan(msg))) {
- return false;
+ if (needs_psk_binder) {
+ // ClientHelloOuter cannot have a PSK binder. Otherwise the
+ // ClientHellOuterAAD computation would break.
+ assert(type != ssl_client_hello_outer);
+ if (!tls13_write_psk_binder(hs, hs->transcript, MakeSpan(msg),
+ /*out_binder_len=*/0)) {
+ return false;
+ }
}
return ssl->method->add_message(ssl, std::move(msg));
@@ -423,7 +445,7 @@
}
void ssl_done_writing_client_hello(SSL_HANDSHAKE *hs) {
- hs->ech_grease.Reset();
+ hs->ech_client_bytes.Reset();
hs->cookie.Reset();
hs->key_share_bytes.Reset();
}
@@ -440,6 +462,12 @@
return ssl_hs_error;
}
+ uint8_t ech_enc[EVP_HPKE_MAX_ENC_LENGTH];
+ size_t ech_enc_len;
+ if (!ssl_select_ech_config(hs, ech_enc, &ech_enc_len)) {
+ return ssl_hs_error;
+ }
+
// Always advertise the ClientHello version from the original maximum version,
// even on renegotiation. The static RSA key exchange uses this field, and
// some servers fail when it changes across handshakes.
@@ -456,6 +484,11 @@
if (ssl->session != nullptr) {
if (ssl->session->is_server ||
!ssl_supports_version(hs, ssl->session->ssl_version) ||
+ // Do not offer TLS 1.2 sessions with ECH. ClientHelloInner does not
+ // offer TLS 1.2, and the cleartext session ID may leak the server
+ // identity.
+ (hs->selected_ech_config &&
+ ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) ||
!SSL_SESSION_is_resumable(ssl->session.get()) ||
!ssl_session_is_time_valid(ssl, ssl->session.get()) ||
(ssl->quic_method != nullptr) != ssl->session->is_quic ||
@@ -467,6 +500,10 @@
if (!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
return ssl_hs_error;
}
+ if (hs->selected_ech_config &&
+ !RAND_bytes(hs->inner_client_random, sizeof(hs->inner_client_random))) {
+ return ssl_hs_error;
+ }
// Never send a session ID in QUIC. QUIC uses TLS 1.3 at a minimum and
// disables TLS 1.3 middlebox compatibility mode.
@@ -498,8 +535,8 @@
}
if (!ssl_setup_key_shares(hs, /*override_group_id=*/0) ||
- !ssl_setup_ech_grease(hs) ||
- !ssl_write_client_hello(hs)) {
+ !ssl_encrypt_client_hello(hs, MakeConstSpan(ech_enc, ech_enc_len)) ||
+ !ssl_add_client_hello(hs)) {
return ssl_hs_error;
}
@@ -525,9 +562,7 @@
return ssl_hs_error;
}
- if (!tls13_init_early_key_schedule(
- hs,
- MakeConstSpan(ssl->session->secret, ssl->session->secret_length)) ||
+ if (!tls13_init_early_key_schedule(hs, ssl->session.get()) ||
!tls13_derive_early_secret(hs)) {
return ssl_hs_error;
}
@@ -578,6 +613,10 @@
assert(SSL_is_dtls(ssl));
+ // When implementing DTLS 1.3, we need to handle the interactions between
+ // HelloVerifyRequest, DTLS 1.3's HelloVerifyRequest removal, and ECH.
+ assert(hs->max_version < TLS1_3_VERSION);
+
SSLMessage msg;
if (!ssl->method->get_message(ssl, &msg)) {
return ssl_hs_read_message;
@@ -609,7 +648,7 @@
return ssl_hs_error;
}
- if (!ssl_write_client_hello(hs)) {
+ if (!ssl_add_client_hello(hs)) {
return ssl_hs_error;
}
@@ -685,6 +724,15 @@
return ssl_hs_error;
}
+ // TODO(https://crbug.com/boringssl/275): If the server negotiates TLS 1.2 and
+ // we offer ECH, we handshake with ClientHelloOuter instead of
+ // ClientHelloInner. That path is not yet implemented. For now, terminate the
+ // handshake with a distinguishable error for testing.
+ if (hs->selected_ech_config) {
+ OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
+ return ssl_hs_error;
+ }
+
// Copy over the server random.
OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
SSL3_RANDOM_SIZE);