| 1 |
// Copyright (c) 2021, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <openssl/ssl.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <algorithm> |
| 10 |
#include <utility> |
| 11 |
|
| 12 |
#include <openssl/aead.h> |
| 13 |
#include <openssl/bytestring.h> |
| 14 |
#include <openssl/curve25519.h> |
| 15 |
#include <openssl/err.h> |
| 16 |
#include <openssl/hkdf.h> |
| 17 |
#include <openssl/hpke.h> |
| 18 |
#include <openssl/rand.h> |
| 19 |
|
| 20 |
#include "internal.h" |
| 21 |
|
| 22 |
|
| 23 |
BSSL_NAMESPACE_BEGIN |
| 24 |
|
| 25 |
// ECH reuses the extension code point for the version number. |
| 26 |
static constexpr uint16_t kECHConfigVersion = |
| 27 |
TLSEXT_TYPE_encrypted_client_hello; |
| 28 |
|
| 29 |
static const decltype(&EVP_hpke_aes_128_gcm) kSupportedAEADs[] = { |
| 30 |
&EVP_hpke_aes_128_gcm, |
| 31 |
&EVP_hpke_aes_256_gcm, |
| 32 |
&EVP_hpke_chacha20_poly1305, |
| 33 |
}; |
| 34 |
|
| 35 |
static const EVP_HPKE_AEAD *get_ech_aead(uint16_t aead_id) { |
| 36 |
for (const auto aead_func : kSupportedAEADs) { |
| 37 |
const EVP_HPKE_AEAD *aead = aead_func(); |
| 38 |
if (aead_id == EVP_HPKE_AEAD_id(aead)) { |
| 39 |
return aead; |
| 40 |
} |
| 41 |
} |
| 42 |
return nullptr; |
| 43 |
} |
| 44 |
|
| 45 |
// ssl_client_hello_write_without_extensions serializes |client_hello| into |
| 46 |
// |out|, omitting the length-prefixed extensions. It serializes individual |
| 47 |
// fields, starting with |client_hello->version|, and ignores the |
| 48 |
// |client_hello->client_hello| field. It returns true on success and false on |
| 49 |
// failure. |
| 50 |
static bool ssl_client_hello_write_without_extensions( |
| 51 |
const SSL_CLIENT_HELLO *client_hello, CBB *out) { |
| 52 |
CBB cbb; |
| 53 |
if (!CBB_add_u16(out, client_hello->version) || |
| 54 |
!CBB_add_bytes(out, client_hello->random, client_hello->random_len) || |
| 55 |
!CBB_add_u8_length_prefixed(out, &cbb) || |
| 56 |
!CBB_add_bytes(&cbb, client_hello->session_id, |
| 57 |
client_hello->session_id_len) || |
| 58 |
!CBB_add_u16_length_prefixed(out, &cbb) || |
| 59 |
!CBB_add_bytes(&cbb, client_hello->cipher_suites, |
| 60 |
client_hello->cipher_suites_len) || |
| 61 |
!CBB_add_u8_length_prefixed(out, &cbb) || |
| 62 |
!CBB_add_bytes(&cbb, client_hello->compression_methods, |
| 63 |
client_hello->compression_methods_len) || |
| 64 |
!CBB_flush(out)) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
static bool is_valid_client_hello_inner(SSL *ssl, uint8_t *out_alert, |
| 71 |
Span<const uint8_t> body) { |
| 72 |
// See draft-ietf-tls-esni-13, section 7.1. |
| 73 |
SSL_CLIENT_HELLO client_hello; |
| 74 |
CBS extension; |
| 75 |
if (!ssl_client_hello_init(ssl, &client_hello, body) || |
| 76 |
!ssl_client_hello_get_extension(&client_hello, &extension, |
| 77 |
TLSEXT_TYPE_encrypted_client_hello) || |
| 78 |
CBS_len(&extension) != 1 || // |
| 79 |
CBS_data(&extension)[0] != ECH_CLIENT_INNER || |
| 80 |
!ssl_client_hello_get_extension(&client_hello, &extension, |
| 81 |
TLSEXT_TYPE_supported_versions)) { |
| 82 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 83 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_CLIENT_HELLO_INNER); |
| 84 |
return false; |
| 85 |
} |
| 86 |
// Parse supported_versions and reject TLS versions prior to TLS 1.3. Older |
| 87 |
// versions are incompatible with ECH. |
| 88 |
CBS versions; |
| 89 |
if (!CBS_get_u8_length_prefixed(&extension, &versions) || |
| 90 |
CBS_len(&extension) != 0 || // |
| 91 |
CBS_len(&versions) == 0) { |
| 92 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 93 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 94 |
return false; |
| 95 |
} |
| 96 |
while (CBS_len(&versions) != 0) { |
| 97 |
uint16_t version; |
| 98 |
if (!CBS_get_u16(&versions, &version)) { |
| 99 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 100 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 101 |
return false; |
| 102 |
} |
| 103 |
if (version == SSL3_VERSION || version == TLS1_VERSION || |
| 104 |
version == TLS1_1_VERSION || version == TLS1_2_VERSION || |
| 105 |
version == DTLS1_VERSION || version == DTLS1_2_VERSION) { |
| 106 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 107 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_CLIENT_HELLO_INNER); |
| 108 |
return false; |
| 109 |
} |
| 110 |
} |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
bool ssl_decode_client_hello_inner( |
| 115 |
SSL *ssl, uint8_t *out_alert, Array<uint8_t> *out_client_hello_inner, |
| 116 |
Span<const uint8_t> encoded_client_hello_inner, |
| 117 |
const SSL_CLIENT_HELLO *client_hello_outer) { |
| 118 |
SSL_CLIENT_HELLO client_hello_inner; |
| 119 |
CBS cbs = encoded_client_hello_inner; |
| 120 |
if (!ssl_parse_client_hello_with_trailing_data(ssl, &cbs, |
| 121 |
&client_hello_inner)) { |
| 122 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 123 |
return false; |
| 124 |
} |
| 125 |
// The remaining data is padding. |
| 126 |
uint8_t padding; |
| 127 |
while (CBS_get_u8(&cbs, &padding)) { |
| 128 |
if (padding != 0) { |
| 129 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 130 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 131 |
return false; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// TLS 1.3 ClientHellos must have extensions, and EncodedClientHelloInners use |
| 136 |
// ClientHelloOuter's session_id. |
| 137 |
if (client_hello_inner.extensions_len == 0 || |
| 138 |
client_hello_inner.session_id_len != 0) { |
| 139 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 140 |
return false; |
| 141 |
} |
| 142 |
client_hello_inner.session_id = client_hello_outer->session_id; |
| 143 |
client_hello_inner.session_id_len = client_hello_outer->session_id_len; |
| 144 |
|
| 145 |
// Begin serializing a message containing the ClientHelloInner in |cbb|. |
| 146 |
ScopedCBB cbb; |
| 147 |
CBB body, extensions_cbb; |
| 148 |
if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO) || |
| 149 |
!ssl_client_hello_write_without_extensions(&client_hello_inner, &body) || |
| 150 |
!CBB_add_u16_length_prefixed(&body, &extensions_cbb)) { |
| 151 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
auto inner_extensions = MakeConstSpan(client_hello_inner.extensions, |
| 156 |
client_hello_inner.extensions_len); |
| 157 |
CBS ext_list_wrapper; |
| 158 |
if (!ssl_client_hello_get_extension(&client_hello_inner, &ext_list_wrapper, |
| 159 |
TLSEXT_TYPE_ech_outer_extensions)) { |
| 160 |
// No ech_outer_extensions. Copy everything. |
| 161 |
if (!CBB_add_bytes(&extensions_cbb, inner_extensions.data(), |
| 162 |
inner_extensions.size())) { |
| 163 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 164 |
return false; |
| 165 |
} |
| 166 |
} else { |
| 167 |
const size_t offset = CBS_data(&ext_list_wrapper) - inner_extensions.data(); |
| 168 |
auto inner_extensions_before = |
| 169 |
inner_extensions.subspan(0, offset - 4 /* extension header */); |
| 170 |
auto inner_extensions_after = |
| 171 |
inner_extensions.subspan(offset + CBS_len(&ext_list_wrapper)); |
| 172 |
if (!CBB_add_bytes(&extensions_cbb, inner_extensions_before.data(), |
| 173 |
inner_extensions_before.size())) { |
| 174 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
// Expand ech_outer_extensions. See draft-ietf-tls-esni-13, Appendix B. |
| 179 |
CBS ext_list; |
| 180 |
if (!CBS_get_u8_length_prefixed(&ext_list_wrapper, &ext_list) || |
| 181 |
CBS_len(&ext_list) == 0 || CBS_len(&ext_list_wrapper) != 0) { |
| 182 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 183 |
return false; |
| 184 |
} |
| 185 |
CBS outer_extensions; |
| 186 |
CBS_init(&outer_extensions, client_hello_outer->extensions, |
| 187 |
client_hello_outer->extensions_len); |
| 188 |
while (CBS_len(&ext_list) != 0) { |
| 189 |
// Find the next extension to copy. |
| 190 |
uint16_t want; |
| 191 |
if (!CBS_get_u16(&ext_list, &want)) { |
| 192 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 193 |
return false; |
| 194 |
} |
| 195 |
// The ECH extension itself is not in the AAD and may not be referenced. |
| 196 |
if (want == TLSEXT_TYPE_encrypted_client_hello) { |
| 197 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 198 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_OUTER_EXTENSION); |
| 199 |
return false; |
| 200 |
} |
| 201 |
// Seek to |want| in |outer_extensions|. |ext_list| is required to match |
| 202 |
// ClientHelloOuter in order. |
| 203 |
uint16_t found; |
| 204 |
CBS ext_body; |
| 205 |
do { |
| 206 |
if (CBS_len(&outer_extensions) == 0) { |
| 207 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 208 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_OUTER_EXTENSION); |
| 209 |
return false; |
| 210 |
} |
| 211 |
if (!CBS_get_u16(&outer_extensions, &found) || |
| 212 |
!CBS_get_u16_length_prefixed(&outer_extensions, &ext_body)) { |
| 213 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 214 |
return false; |
| 215 |
} |
| 216 |
} while (found != want); |
| 217 |
// Copy the extension. |
| 218 |
if (!CBB_add_u16(&extensions_cbb, found) || |
| 219 |
!CBB_add_u16(&extensions_cbb, CBS_len(&ext_body)) || |
| 220 |
!CBB_add_bytes(&extensions_cbb, CBS_data(&ext_body), |
| 221 |
CBS_len(&ext_body))) { |
| 222 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 223 |
return false; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
if (!CBB_add_bytes(&extensions_cbb, inner_extensions_after.data(), |
| 228 |
inner_extensions_after.size())) { |
| 229 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 230 |
return false; |
| 231 |
} |
| 232 |
} |
| 233 |
if (!CBB_flush(&body)) { |
| 234 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
if (!is_valid_client_hello_inner( |
| 239 |
ssl, out_alert, MakeConstSpan(CBB_data(&body), CBB_len(&body)))) { |
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
if (!ssl->method->finish_message(ssl, cbb.get(), out_client_hello_inner)) { |
| 244 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 245 |
return false; |
| 246 |
} |
| 247 |
return true; |
| 248 |
} |
| 249 |
|
| 250 |
bool ssl_client_hello_decrypt(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 251 |
bool *out_is_decrypt_error, Array<uint8_t> *out, |
| 252 |
const SSL_CLIENT_HELLO *client_hello_outer, |
| 253 |
Span<const uint8_t> payload) { |
| 254 |
*out_is_decrypt_error = false; |
| 255 |
|
| 256 |
// The ClientHelloOuterAAD is |client_hello_outer| with |payload| (which must |
| 257 |
// point within |client_hello_outer->extensions|) replaced with zeros. See |
| 258 |
// draft-ietf-tls-esni-13, section 5.2. |
| 259 |
Array<uint8_t> aad; |
| 260 |
if (!aad.CopyFrom(MakeConstSpan(client_hello_outer->client_hello, |
| 261 |
client_hello_outer->client_hello_len))) { |
| 262 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 263 |
return false; |
| 264 |
} |
| 265 |
|
| 266 |
// We assert with |uintptr_t| because the comparison would be UB if they |
| 267 |
// didn't alias. |
| 268 |
assert(reinterpret_cast<uintptr_t>(client_hello_outer->extensions) <= |
| 269 |
reinterpret_cast<uintptr_t>(payload.data())); |
| 270 |
assert(reinterpret_cast<uintptr_t>(client_hello_outer->extensions + |
| 271 |
client_hello_outer->extensions_len) >= |
| 272 |
reinterpret_cast<uintptr_t>(payload.data() + payload.size())); |
| 273 |
Span<uint8_t> payload_aad = MakeSpan(aad).subspan( |
| 274 |
payload.data() - client_hello_outer->client_hello, payload.size()); |
| 275 |
OPENSSL_memset(payload_aad.data(), 0, payload_aad.size()); |
| 276 |
|
| 277 |
// Decrypt the EncodedClientHelloInner. |
| 278 |
Array<uint8_t> encoded; |
| 279 |
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 280 |
// In fuzzer mode, disable encryption to improve coverage. We reserve a short |
| 281 |
// input to signal decryption failure, so the fuzzer can explore fallback to |
| 282 |
// ClientHelloOuter. |
| 283 |
const uint8_t kBadPayload[] = {0xff}; |
| 284 |
if (payload == kBadPayload) { |
| 285 |
*out_alert = SSL_AD_DECRYPT_ERROR; |
| 286 |
*out_is_decrypt_error = true; |
| 287 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 288 |
return false; |
| 289 |
} |
| 290 |
if (!encoded.CopyFrom(payload)) { |
| 291 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 292 |
return false; |
| 293 |
} |
| 294 |
#else |
| 295 |
if (!encoded.Init(payload.size())) { |
| 296 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 297 |
return false; |
| 298 |
} |
| 299 |
size_t len; |
| 300 |
if (!EVP_HPKE_CTX_open(hs->ech_hpke_ctx.get(), encoded.data(), &len, |
| 301 |
encoded.size(), payload.data(), payload.size(), |
| 302 |
aad.data(), aad.size())) { |
| 303 |
*out_alert = SSL_AD_DECRYPT_ERROR; |
| 304 |
*out_is_decrypt_error = true; |
| 305 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 306 |
return false; |
| 307 |
} |
| 308 |
encoded.Shrink(len); |
| 309 |
#endif |
| 310 |
|
| 311 |
if (!ssl_decode_client_hello_inner(hs->ssl, out_alert, out, encoded, |
| 312 |
client_hello_outer)) { |
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
ssl_do_msg_callback(hs->ssl, /*is_write=*/0, SSL3_RT_CLIENT_HELLO_INNER, |
| 317 |
*out); |
| 318 |
return true; |
| 319 |
} |
| 320 |
|
| 321 |
static bool is_hex_component(Span<const uint8_t> in) { |
| 322 |
if (in.size() < 2 || in[0] != '0' || (in[1] != 'x' && in[1] != 'X')) { |
| 323 |
return false; |
| 324 |
} |
| 325 |
for (uint8_t b : in.subspan(2)) { |
| 326 |
if (!OPENSSL_isxdigit(b)) { |
| 327 |
return false; |
| 328 |
} |
| 329 |
} |
| 330 |
return true; |
| 331 |
} |
| 332 |
|
| 333 |
static bool is_decimal_component(Span<const uint8_t> in) { |
| 334 |
if (in.empty()) { |
| 335 |
return false; |
| 336 |
} |
| 337 |
for (uint8_t b : in) { |
| 338 |
if (!('0' <= b && b <= '9')) { |
| 339 |
return false; |
| 340 |
} |
| 341 |
} |
| 342 |
return true; |
| 343 |
} |
| 344 |
|
| 345 |
bool ssl_is_valid_ech_public_name(Span<const uint8_t> public_name) { |
| 346 |
// See draft-ietf-tls-esni-13, Section 4 and RFC 5890, Section 2.3.1. The |
| 347 |
// public name must be a dot-separated sequence of LDH labels and not begin or |
| 348 |
// end with a dot. |
| 349 |
auto remaining = public_name; |
| 350 |
if (remaining.empty()) { |
| 351 |
return false; |
| 352 |
} |
| 353 |
Span<const uint8_t> last; |
| 354 |
while (!remaining.empty()) { |
| 355 |
// Find the next dot-separated component. |
| 356 |
auto dot = std::find(remaining.begin(), remaining.end(), '.'); |
| 357 |
Span<const uint8_t> component; |
| 358 |
if (dot == remaining.end()) { |
| 359 |
component = remaining; |
| 360 |
last = component; |
| 361 |
remaining = Span<const uint8_t>(); |
| 362 |
} else { |
| 363 |
component = remaining.subspan(0, dot - remaining.begin()); |
| 364 |
// Skip the dot. |
| 365 |
remaining = remaining.subspan(dot - remaining.begin() + 1); |
| 366 |
if (remaining.empty()) { |
| 367 |
// Trailing dots are not allowed. |
| 368 |
return false; |
| 369 |
} |
| 370 |
} |
| 371 |
// |component| must be a valid LDH label. Checking for empty components also |
| 372 |
// rejects leading dots. |
| 373 |
if (component.empty() || component.size() > 63 || |
| 374 |
component.front() == '-' || component.back() == '-') { |
| 375 |
return false; |
| 376 |
} |
| 377 |
for (uint8_t c : component) { |
| 378 |
if (!OPENSSL_isalnum(c) && c != '-') { |
| 379 |
return false; |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
// The WHATWG URL parser additionally does not allow any DNS names that end in |
| 385 |
// a numeric component. See: |
| 386 |
// https://url.spec.whatwg.org/#concept-host-parser |
| 387 |
// https://url.spec.whatwg.org/#ends-in-a-number-checker |
| 388 |
// |
| 389 |
// The WHATWG parser is formulated in terms of parsing decimal, octal, and |
| 390 |
// hex, along with a separate ASCII digits check. The ASCII digits check |
| 391 |
// subsumes the decimal and octal check, so we only need to check two cases. |
| 392 |
return !is_hex_component(last) && !is_decimal_component(last); |
| 393 |
} |
| 394 |
|
| 395 |
static bool parse_ech_config(CBS *cbs, ECHConfig *out, bool *out_supported, |
| 396 |
bool all_extensions_mandatory) { |
| 397 |
uint16_t version; |
| 398 |
CBS orig = *cbs; |
| 399 |
CBS contents; |
| 400 |
if (!CBS_get_u16(cbs, &version) || |
| 401 |
!CBS_get_u16_length_prefixed(cbs, &contents)) { |
| 402 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 403 |
return false; |
| 404 |
} |
| 405 |
|
| 406 |
if (version != kECHConfigVersion) { |
| 407 |
*out_supported = false; |
| 408 |
return true; |
| 409 |
} |
| 410 |
|
| 411 |
// Make a copy of the ECHConfig and parse from it, so the results alias into |
| 412 |
// the saved copy. |
| 413 |
if (!out->raw.CopyFrom( |
| 414 |
MakeConstSpan(CBS_data(&orig), CBS_len(&orig) - CBS_len(cbs)))) { |
| 415 |
return false; |
| 416 |
} |
| 417 |
|
| 418 |
CBS ech_config(out->raw); |
| 419 |
CBS public_name, public_key, cipher_suites, extensions; |
| 420 |
if (!CBS_skip(&ech_config, 2) || // version |
| 421 |
!CBS_get_u16_length_prefixed(&ech_config, &contents) || |
| 422 |
!CBS_get_u8(&contents, &out->config_id) || |
| 423 |
!CBS_get_u16(&contents, &out->kem_id) || |
| 424 |
!CBS_get_u16_length_prefixed(&contents, &public_key) || |
| 425 |
CBS_len(&public_key) == 0 || |
| 426 |
!CBS_get_u16_length_prefixed(&contents, &cipher_suites) || |
| 427 |
CBS_len(&cipher_suites) == 0 || CBS_len(&cipher_suites) % 4 != 0 || |
| 428 |
!CBS_get_u8(&contents, &out->maximum_name_length) || |
| 429 |
!CBS_get_u8_length_prefixed(&contents, &public_name) || |
| 430 |
CBS_len(&public_name) == 0 || |
| 431 |
!CBS_get_u16_length_prefixed(&contents, &extensions) || |
| 432 |
CBS_len(&contents) != 0) { |
| 433 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 434 |
return false; |
| 435 |
} |
| 436 |
|
| 437 |
if (!ssl_is_valid_ech_public_name(public_name)) { |
| 438 |
// TODO(https://crbug.com/boringssl/275): The draft says ECHConfigs with |
| 439 |
// invalid public names should be ignored, but LDH syntax failures are |
| 440 |
// unambiguously invalid. |
| 441 |
*out_supported = false; |
| 442 |
return true; |
| 443 |
} |
| 444 |
|
| 445 |
out->public_key = public_key; |
| 446 |
out->public_name = public_name; |
| 447 |
// This function does not ensure |out->kem_id| and |out->cipher_suites| use |
| 448 |
// supported algorithms. The caller must do this. |
| 449 |
out->cipher_suites = cipher_suites; |
| 450 |
|
| 451 |
bool has_unknown_mandatory_extension = false; |
| 452 |
while (CBS_len(&extensions) != 0) { |
| 453 |
uint16_t type; |
| 454 |
CBS body; |
| 455 |
if (!CBS_get_u16(&extensions, &type) || |
| 456 |
!CBS_get_u16_length_prefixed(&extensions, &body)) { |
| 457 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 458 |
return false; |
| 459 |
} |
| 460 |
// We currently do not support any extensions. |
| 461 |
if (type & 0x8000 || all_extensions_mandatory) { |
| 462 |
// Extension numbers with the high bit set are mandatory. Continue parsing |
| 463 |
// to enforce syntax, but we will ultimately ignore this ECHConfig as a |
| 464 |
// client and reject it as a server. |
| 465 |
has_unknown_mandatory_extension = true; |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
*out_supported = !has_unknown_mandatory_extension; |
| 470 |
return true; |
| 471 |
} |
| 472 |
|
| 473 |
bool ECHServerConfig::Init(Span<const uint8_t> ech_config, |
| 474 |
const EVP_HPKE_KEY *key, bool is_retry_config) { |
| 475 |
is_retry_config_ = is_retry_config; |
| 476 |
|
| 477 |
// Parse the ECHConfig, rejecting all unsupported parameters and extensions. |
| 478 |
// Unlike most server options, ECH's server configuration is serialized and |
| 479 |
// configured in both the server and DNS. If the caller configures an |
| 480 |
// unsupported parameter, this is a deployment error. To catch these errors, |
| 481 |
// we fail early. |
| 482 |
CBS cbs = ech_config; |
| 483 |
bool supported; |
| 484 |
if (!parse_ech_config(&cbs, &ech_config_, &supported, |
| 485 |
/*all_extensions_mandatory=*/true)) { |
| 486 |
return false; |
| 487 |
} |
| 488 |
if (CBS_len(&cbs) != 0) { |
| 489 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 490 |
return false; |
| 491 |
} |
| 492 |
if (!supported) { |
| 493 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ECH_SERVER_CONFIG); |
| 494 |
return false; |
| 495 |
} |
| 496 |
|
| 497 |
CBS cipher_suites = ech_config_.cipher_suites; |
| 498 |
while (CBS_len(&cipher_suites) > 0) { |
| 499 |
uint16_t kdf_id, aead_id; |
| 500 |
if (!CBS_get_u16(&cipher_suites, &kdf_id) || |
| 501 |
!CBS_get_u16(&cipher_suites, &aead_id)) { |
| 502 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 503 |
return false; |
| 504 |
} |
| 505 |
// The server promises to support every option in the ECHConfig, so reject |
| 506 |
// any unsupported cipher suites. |
| 507 |
if (kdf_id != EVP_HPKE_HKDF_SHA256 || get_ech_aead(aead_id) == nullptr) { |
| 508 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ECH_SERVER_CONFIG); |
| 509 |
return false; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
// Check the public key in the ECHConfig matches |key|. |
| 514 |
uint8_t expected_public_key[EVP_HPKE_MAX_PUBLIC_KEY_LENGTH]; |
| 515 |
size_t expected_public_key_len; |
| 516 |
if (!EVP_HPKE_KEY_public_key(key, expected_public_key, |
| 517 |
&expected_public_key_len, |
| 518 |
sizeof(expected_public_key))) { |
| 519 |
return false; |
| 520 |
} |
| 521 |
if (ech_config_.kem_id != EVP_HPKE_KEM_id(EVP_HPKE_KEY_kem(key)) || |
| 522 |
MakeConstSpan(expected_public_key, expected_public_key_len) != |
| 523 |
ech_config_.public_key) { |
| 524 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ECH_SERVER_CONFIG_AND_PRIVATE_KEY_MISMATCH); |
| 525 |
return false; |
| 526 |
} |
| 527 |
|
| 528 |
if (!EVP_HPKE_KEY_copy(key_.get(), key)) { |
| 529 |
return false; |
| 530 |
} |
| 531 |
|
| 532 |
return true; |
| 533 |
} |
| 534 |
|
| 535 |
bool ECHServerConfig::SetupContext(EVP_HPKE_CTX *ctx, uint16_t kdf_id, |
| 536 |
uint16_t aead_id, |
| 537 |
Span<const uint8_t> enc) const { |
| 538 |
// Check the cipher suite is supported by this ECHServerConfig. |
| 539 |
CBS cbs(ech_config_.cipher_suites); |
| 540 |
bool cipher_ok = false; |
| 541 |
while (CBS_len(&cbs) != 0) { |
| 542 |
uint16_t supported_kdf_id, supported_aead_id; |
| 543 |
if (!CBS_get_u16(&cbs, &supported_kdf_id) || |
| 544 |
!CBS_get_u16(&cbs, &supported_aead_id)) { |
| 545 |
return false; |
| 546 |
} |
| 547 |
if (kdf_id == supported_kdf_id && aead_id == supported_aead_id) { |
| 548 |
cipher_ok = true; |
| 549 |
break; |
| 550 |
} |
| 551 |
} |
| 552 |
if (!cipher_ok) { |
| 553 |
return false; |
| 554 |
} |
| 555 |
|
| 556 |
static const uint8_t kInfoLabel[] = "tls ech"; |
| 557 |
ScopedCBB info_cbb; |
| 558 |
if (!CBB_init(info_cbb.get(), sizeof(kInfoLabel) + ech_config_.raw.size()) || |
| 559 |
!CBB_add_bytes(info_cbb.get(), kInfoLabel, |
| 560 |
sizeof(kInfoLabel) /* includes trailing NUL */) || |
| 561 |
!CBB_add_bytes(info_cbb.get(), ech_config_.raw.data(), |
| 562 |
ech_config_.raw.size())) { |
| 563 |
return false; |
| 564 |
} |
| 565 |
|
| 566 |
assert(kdf_id == EVP_HPKE_HKDF_SHA256); |
| 567 |
assert(get_ech_aead(aead_id) != NULL); |
| 568 |
return EVP_HPKE_CTX_setup_recipient( |
| 569 |
ctx, key_.get(), EVP_hpke_hkdf_sha256(), get_ech_aead(aead_id), enc.data(), |
| 570 |
enc.size(), CBB_data(info_cbb.get()), CBB_len(info_cbb.get())); |
| 571 |
} |
| 572 |
|
| 573 |
bool ssl_is_valid_ech_config_list(Span<const uint8_t> ech_config_list) { |
| 574 |
CBS cbs = ech_config_list, child; |
| 575 |
if (!CBS_get_u16_length_prefixed(&cbs, &child) || // |
| 576 |
CBS_len(&child) == 0 || // |
| 577 |
CBS_len(&cbs) > 0) { |
| 578 |
return false; |
| 579 |
} |
| 580 |
while (CBS_len(&child) > 0) { |
| 581 |
ECHConfig ech_config; |
| 582 |
bool supported; |
| 583 |
if (!parse_ech_config(&child, &ech_config, &supported, |
| 584 |
/*all_extensions_mandatory=*/false)) { |
| 585 |
return false; |
| 586 |
} |
| 587 |
} |
| 588 |
return true; |
| 589 |
} |
| 590 |
|
| 591 |
static bool select_ech_cipher_suite(const EVP_HPKE_KDF **out_kdf, |
| 592 |
const EVP_HPKE_AEAD **out_aead, |
| 593 |
Span<const uint8_t> cipher_suites, |
| 594 |
const bool has_aes_hardware) { |
| 595 |
const EVP_HPKE_AEAD *aead = nullptr; |
| 596 |
CBS cbs = cipher_suites; |
| 597 |
while (CBS_len(&cbs) != 0) { |
| 598 |
uint16_t kdf_id, aead_id; |
| 599 |
if (!CBS_get_u16(&cbs, &kdf_id) || // |
| 600 |
!CBS_get_u16(&cbs, &aead_id)) { |
| 601 |
return false; |
| 602 |
} |
| 603 |
// Pick the first common cipher suite, but prefer ChaCha20-Poly1305 if we |
| 604 |
// don't have AES hardware. |
| 605 |
const EVP_HPKE_AEAD *candidate = get_ech_aead(aead_id); |
| 606 |
if (kdf_id != EVP_HPKE_HKDF_SHA256 || candidate == nullptr) { |
| 607 |
continue; |
| 608 |
} |
| 609 |
if (aead == nullptr || |
| 610 |
(!has_aes_hardware && aead_id == EVP_HPKE_CHACHA20_POLY1305)) { |
| 611 |
aead = candidate; |
| 612 |
} |
| 613 |
} |
| 614 |
if (aead == nullptr) { |
| 615 |
return false; |
| 616 |
} |
| 617 |
|
| 618 |
*out_kdf = EVP_hpke_hkdf_sha256(); |
| 619 |
*out_aead = aead; |
| 620 |
return true; |
| 621 |
} |
| 622 |
|
| 623 |
bool ssl_select_ech_config(SSL_HANDSHAKE *hs, Span<uint8_t> out_enc, |
| 624 |
size_t *out_enc_len) { |
| 625 |
*out_enc_len = 0; |
| 626 |
if (hs->max_version < TLS1_3_VERSION) { |
| 627 |
// ECH requires TLS 1.3. |
| 628 |
return true; |
| 629 |
} |
| 630 |
|
| 631 |
if (!hs->config->client_ech_config_list.empty()) { |
| 632 |
CBS cbs = MakeConstSpan(hs->config->client_ech_config_list); |
| 633 |
CBS child; |
| 634 |
if (!CBS_get_u16_length_prefixed(&cbs, &child) || // |
| 635 |
CBS_len(&child) == 0 || // |
| 636 |
CBS_len(&cbs) > 0) { |
| 637 |
return false; |
| 638 |
} |
| 639 |
// Look for the first ECHConfig with supported parameters. |
| 640 |
while (CBS_len(&child) > 0) { |
| 641 |
ECHConfig ech_config; |
| 642 |
bool supported; |
| 643 |
if (!parse_ech_config(&child, &ech_config, &supported, |
| 644 |
/*all_extensions_mandatory=*/false)) { |
| 645 |
return false; |
| 646 |
} |
| 647 |
const EVP_HPKE_KEM *kem = EVP_hpke_x25519_hkdf_sha256(); |
| 648 |
const EVP_HPKE_KDF *kdf; |
| 649 |
const EVP_HPKE_AEAD *aead; |
| 650 |
if (supported && // |
| 651 |
ech_config.kem_id == EVP_HPKE_DHKEM_X25519_HKDF_SHA256 && |
| 652 |
select_ech_cipher_suite(&kdf, &aead, ech_config.cipher_suites, |
| 653 |
hs->ssl->config->aes_hw_override |
| 654 |
? hs->ssl->config->aes_hw_override_value |
| 655 |
: EVP_has_aes_hardware())) { |
| 656 |
ScopedCBB info; |
| 657 |
static const uint8_t kInfoLabel[] = "tls ech"; // includes trailing NUL |
| 658 |
if (!CBB_init(info.get(), sizeof(kInfoLabel) + ech_config.raw.size()) || |
| 659 |
!CBB_add_bytes(info.get(), kInfoLabel, sizeof(kInfoLabel)) || |
| 660 |
!CBB_add_bytes(info.get(), ech_config.raw.data(), |
| 661 |
ech_config.raw.size())) { |
| 662 |
return false; |
| 663 |
} |
| 664 |
|
| 665 |
if (!EVP_HPKE_CTX_setup_sender( |
| 666 |
hs->ech_hpke_ctx.get(), out_enc.data(), out_enc_len, |
| 667 |
out_enc.size(), kem, kdf, aead, ech_config.public_key.data(), |
| 668 |
ech_config.public_key.size(), CBB_data(info.get()), |
| 669 |
CBB_len(info.get())) || |
| 670 |
!hs->inner_transcript.Init()) { |
| 671 |
return false; |
| 672 |
} |
| 673 |
|
| 674 |
hs->selected_ech_config = MakeUnique<ECHConfig>(std::move(ech_config)); |
| 675 |
return hs->selected_ech_config != nullptr; |
| 676 |
} |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
return true; |
| 681 |
} |
| 682 |
|
| 683 |
static size_t aead_overhead(const EVP_HPKE_AEAD *aead) { |
| 684 |
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 685 |
// TODO(https://crbug.com/boringssl/275): Having to adjust the overhead |
| 686 |
// everywhere is tedious. Change fuzzer mode to append a fake tag but still |
| 687 |
// otherwise be cleartext, refresh corpora, and then inline this function. |
| 688 |
return 0; |
| 689 |
#else |
| 690 |
return EVP_AEAD_max_overhead(EVP_HPKE_AEAD_aead(aead)); |
| 691 |
#endif |
| 692 |
} |
| 693 |
|
| 694 |
// random_size returns a random value between |min| and |max|, inclusive. |
| 695 |
static size_t random_size(size_t min, size_t max) { |
| 696 |
assert(min < max); |
| 697 |
size_t value; |
| 698 |
RAND_bytes(reinterpret_cast<uint8_t *>(&value), sizeof(value)); |
| 699 |
return value % (max - min + 1) + min; |
| 700 |
} |
| 701 |
|
| 702 |
static bool setup_ech_grease(SSL_HANDSHAKE *hs) { |
| 703 |
assert(!hs->selected_ech_config); |
| 704 |
if (hs->max_version < TLS1_3_VERSION || !hs->config->ech_grease_enabled) { |
| 705 |
return true; |
| 706 |
} |
| 707 |
|
| 708 |
const uint16_t kdf_id = EVP_HPKE_HKDF_SHA256; |
| 709 |
const bool has_aes_hw = hs->ssl->config->aes_hw_override |
| 710 |
? hs->ssl->config->aes_hw_override_value |
| 711 |
: EVP_has_aes_hardware(); |
| 712 |
const EVP_HPKE_AEAD *aead = |
| 713 |
has_aes_hw ? EVP_hpke_aes_128_gcm() : EVP_hpke_chacha20_poly1305(); |
| 714 |
static_assert(ssl_grease_ech_config_id < sizeof(hs->grease_seed), |
| 715 |
"hs->grease_seed is too small"); |
| 716 |
uint8_t config_id = hs->grease_seed[ssl_grease_ech_config_id]; |
| 717 |
|
| 718 |
uint8_t enc[X25519_PUBLIC_VALUE_LEN]; |
| 719 |
uint8_t private_key_unused[X25519_PRIVATE_KEY_LEN]; |
| 720 |
X25519_keypair(enc, private_key_unused); |
| 721 |
|
| 722 |
// To determine a plausible length for the payload, we estimate the size of a |
| 723 |
// typical EncodedClientHelloInner without resumption: |
| 724 |
// |
| 725 |
// 2+32+1+2 version, random, legacy_session_id, legacy_compression_methods |
| 726 |
// 2+4*2 cipher_suites (three TLS 1.3 ciphers, GREASE) |
| 727 |
// 2 extensions prefix |
| 728 |
// 5 inner encrypted_client_hello |
| 729 |
// 4+1+2*2 supported_versions (TLS 1.3, GREASE) |
| 730 |
// 4+1+10*2 outer_extensions (key_share, sigalgs, sct, alpn, |
| 731 |
// supported_groups, status_request, psk_key_exchange_modes, |
| 732 |
// compress_certificate, GREASE x2) |
| 733 |
// |
| 734 |
// The server_name extension has an overhead of 9 bytes. For now, arbitrarily |
| 735 |
// estimate maximum_name_length to be between 32 and 100 bytes. Then round up |
| 736 |
// to a multiple of 32, to match draft-ietf-tls-esni-13, section 6.1.3. |
| 737 |
const size_t payload_len = |
| 738 |
32 * random_size(128 / 32, 224 / 32) + aead_overhead(aead); |
| 739 |
bssl::ScopedCBB cbb; |
| 740 |
CBB enc_cbb, payload_cbb; |
| 741 |
uint8_t *payload; |
| 742 |
if (!CBB_init(cbb.get(), 256) || |
| 743 |
!CBB_add_u16(cbb.get(), kdf_id) || |
| 744 |
!CBB_add_u16(cbb.get(), EVP_HPKE_AEAD_id(aead)) || |
| 745 |
!CBB_add_u8(cbb.get(), config_id) || |
| 746 |
!CBB_add_u16_length_prefixed(cbb.get(), &enc_cbb) || |
| 747 |
!CBB_add_bytes(&enc_cbb, enc, sizeof(enc)) || |
| 748 |
!CBB_add_u16_length_prefixed(cbb.get(), &payload_cbb) || |
| 749 |
!CBB_add_space(&payload_cbb, &payload, payload_len) || |
| 750 |
!RAND_bytes(payload, payload_len) || |
| 751 |
!CBBFinishArray(cbb.get(), &hs->ech_client_outer)) { |
| 752 |
return false; |
| 753 |
} |
| 754 |
return true; |
| 755 |
} |
| 756 |
|
| 757 |
bool ssl_encrypt_client_hello(SSL_HANDSHAKE *hs, Span<const uint8_t> enc) { |
| 758 |
SSL *const ssl = hs->ssl; |
| 759 |
if (!hs->selected_ech_config) { |
| 760 |
return setup_ech_grease(hs); |
| 761 |
} |
| 762 |
|
| 763 |
// Construct ClientHelloInner and EncodedClientHelloInner. See |
| 764 |
// draft-ietf-tls-esni-13, sections 5.1 and 6.1. |
| 765 |
ScopedCBB cbb, encoded_cbb; |
| 766 |
CBB body; |
| 767 |
bool needs_psk_binder; |
| 768 |
Array<uint8_t> hello_inner; |
| 769 |
if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO) || |
| 770 |
!CBB_init(encoded_cbb.get(), 256) || |
| 771 |
!ssl_write_client_hello_without_extensions(hs, &body, |
| 772 |
ssl_client_hello_inner, |
| 773 |
/*empty_session_id=*/false) || |
| 774 |
!ssl_write_client_hello_without_extensions(hs, encoded_cbb.get(), |
| 775 |
ssl_client_hello_inner, |
| 776 |
/*empty_session_id=*/true) || |
| 777 |
!ssl_add_clienthello_tlsext(hs, &body, encoded_cbb.get(), |
| 778 |
&needs_psk_binder, ssl_client_hello_inner, |
| 779 |
CBB_len(&body)) || |
| 780 |
!ssl->method->finish_message(ssl, cbb.get(), &hello_inner)) { |
| 781 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 782 |
return false; |
| 783 |
} |
| 784 |
|
| 785 |
if (needs_psk_binder) { |
| 786 |
size_t binder_len; |
| 787 |
if (!tls13_write_psk_binder(hs, hs->inner_transcript, MakeSpan(hello_inner), |
| 788 |
&binder_len)) { |
| 789 |
return false; |
| 790 |
} |
| 791 |
// Also update the EncodedClientHelloInner. |
| 792 |
auto encoded_binder = |
| 793 |
MakeSpan(const_cast<uint8_t *>(CBB_data(encoded_cbb.get())), |
| 794 |
CBB_len(encoded_cbb.get())) |
| 795 |
.last(binder_len); |
| 796 |
auto hello_inner_binder = MakeConstSpan(hello_inner).last(binder_len); |
| 797 |
OPENSSL_memcpy(encoded_binder.data(), hello_inner_binder.data(), |
| 798 |
binder_len); |
| 799 |
} |
| 800 |
|
| 801 |
ssl_do_msg_callback(ssl, /*is_write=*/1, SSL3_RT_CLIENT_HELLO_INNER, |
| 802 |
hello_inner); |
| 803 |
if (!hs->inner_transcript.Update(hello_inner)) { |
| 804 |
return false; |
| 805 |
} |
| 806 |
|
| 807 |
// Pad the EncodedClientHelloInner. See draft-ietf-tls-esni-13, section 6.1.3. |
| 808 |
size_t padding_len = 0; |
| 809 |
size_t maximum_name_length = hs->selected_ech_config->maximum_name_length; |
| 810 |
if (ssl->hostname) { |
| 811 |
size_t hostname_len = strlen(ssl->hostname.get()); |
| 812 |
if (hostname_len <= maximum_name_length) { |
| 813 |
padding_len = maximum_name_length - hostname_len; |
| 814 |
} |
| 815 |
} else { |
| 816 |
// No SNI. Pad up to |maximum_name_length|, including server_name extension |
| 817 |
// overhead. |
| 818 |
padding_len = 9 + maximum_name_length; |
| 819 |
} |
| 820 |
// Pad the whole thing to a multiple of 32 bytes. |
| 821 |
padding_len += 31 - ((CBB_len(encoded_cbb.get()) + padding_len - 1) % 32); |
| 822 |
Array<uint8_t> encoded; |
| 823 |
if (!CBB_add_zeros(encoded_cbb.get(), padding_len) || |
| 824 |
!CBBFinishArray(encoded_cbb.get(), &encoded)) { |
| 825 |
return false; |
| 826 |
} |
| 827 |
|
| 828 |
// Encrypt |encoded|. See draft-ietf-tls-esni-13, section 6.1.1. First, |
| 829 |
// assemble the extension with a placeholder value for ClientHelloOuterAAD. |
| 830 |
// See draft-ietf-tls-esni-13, section 5.2. |
| 831 |
const EVP_HPKE_KDF *kdf = EVP_HPKE_CTX_kdf(hs->ech_hpke_ctx.get()); |
| 832 |
const EVP_HPKE_AEAD *aead = EVP_HPKE_CTX_aead(hs->ech_hpke_ctx.get()); |
| 833 |
size_t payload_len = encoded.size() + aead_overhead(aead); |
| 834 |
CBB enc_cbb, payload_cbb; |
| 835 |
if (!CBB_init(cbb.get(), 256) || |
| 836 |
!CBB_add_u16(cbb.get(), EVP_HPKE_KDF_id(kdf)) || |
| 837 |
!CBB_add_u16(cbb.get(), EVP_HPKE_AEAD_id(aead)) || |
| 838 |
!CBB_add_u8(cbb.get(), hs->selected_ech_config->config_id) || |
| 839 |
!CBB_add_u16_length_prefixed(cbb.get(), &enc_cbb) || |
| 840 |
!CBB_add_bytes(&enc_cbb, enc.data(), enc.size()) || |
| 841 |
!CBB_add_u16_length_prefixed(cbb.get(), &payload_cbb) || |
| 842 |
!CBB_add_zeros(&payload_cbb, payload_len) || |
| 843 |
!CBBFinishArray(cbb.get(), &hs->ech_client_outer)) { |
| 844 |
return false; |
| 845 |
} |
| 846 |
|
| 847 |
// Construct ClientHelloOuterAAD. |
| 848 |
// TODO(https://crbug.com/boringssl/275): This ends up constructing the |
| 849 |
// ClientHelloOuter twice. Instead, reuse |aad| for the ClientHello, now that |
| 850 |
// draft-12 made the length prefixes match. |
| 851 |
bssl::ScopedCBB aad; |
| 852 |
if (!CBB_init(aad.get(), 256) || |
| 853 |
!ssl_write_client_hello_without_extensions(hs, aad.get(), |
| 854 |
ssl_client_hello_outer, |
| 855 |
/*empty_session_id=*/false) || |
| 856 |
!ssl_add_clienthello_tlsext(hs, aad.get(), /*out_encoded=*/nullptr, |
| 857 |
&needs_psk_binder, ssl_client_hello_outer, |
| 858 |
CBB_len(aad.get()))) { |
| 859 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 860 |
return false; |
| 861 |
} |
| 862 |
|
| 863 |
// ClientHelloOuter may not require a PSK binder. Otherwise, we have a |
| 864 |
// circular dependency. |
| 865 |
assert(!needs_psk_binder); |
| 866 |
|
| 867 |
// Replace the payload in |hs->ech_client_outer| with the encrypted value. |
| 868 |
auto payload_span = MakeSpan(hs->ech_client_outer).last(payload_len); |
| 869 |
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 870 |
// In fuzzer mode, the server expects a cleartext payload. |
| 871 |
assert(payload_span.size() == encoded.size()); |
| 872 |
OPENSSL_memcpy(payload_span.data(), encoded.data(), encoded.size()); |
| 873 |
#else |
| 874 |
if (!EVP_HPKE_CTX_seal(hs->ech_hpke_ctx.get(), payload_span.data(), |
| 875 |
&payload_len, payload_span.size(), encoded.data(), |
| 876 |
encoded.size(), CBB_data(aad.get()), |
| 877 |
CBB_len(aad.get())) || |
| 878 |
payload_len != payload_span.size()) { |
| 879 |
return false; |
| 880 |
} |
| 881 |
#endif // BORINGSSL_UNSAFE_FUZZER_MODE |
| 882 |
|
| 883 |
return true; |
| 884 |
} |
| 885 |
|
| 886 |
BSSL_NAMESPACE_END |
| 887 |
|
| 888 |
using namespace bssl; |
| 889 |
|
| 890 |
void SSL_set_enable_ech_grease(SSL *ssl, int enable) { |
| 891 |
if (!ssl->config) { |
| 892 |
return; |
| 893 |
} |
| 894 |
ssl->config->ech_grease_enabled = !!enable; |
| 895 |
} |
| 896 |
|
| 897 |
int SSL_set1_ech_config_list(SSL *ssl, const uint8_t *ech_config_list, |
| 898 |
size_t ech_config_list_len) { |
| 899 |
if (!ssl->config) { |
| 900 |
return 0; |
| 901 |
} |
| 902 |
|
| 903 |
auto span = MakeConstSpan(ech_config_list, ech_config_list_len); |
| 904 |
if (!ssl_is_valid_ech_config_list(span)) { |
| 905 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ECH_CONFIG_LIST); |
| 906 |
return 0; |
| 907 |
} |
| 908 |
return ssl->config->client_ech_config_list.CopyFrom(span); |
| 909 |
} |
| 910 |
|
| 911 |
void SSL_get0_ech_name_override(const SSL *ssl, const char **out_name, |
| 912 |
size_t *out_name_len) { |
| 913 |
// When ECH is rejected, we use the public name. Note that, if |
| 914 |
// |SSL_CTX_set_reverify_on_resume| is enabled, we reverify the certificate |
| 915 |
// before the 0-RTT point. If also offering ECH, we verify as if |
| 916 |
// ClientHelloInner was accepted and do not override. This works because, at |
| 917 |
// this point, |ech_status| will be |ssl_ech_none|. See the |
| 918 |
// ECH-Client-Reject-EarlyDataReject-OverrideNameOnRetry tests in runner.go. |
| 919 |
const SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
| 920 |
if (!ssl->server && hs && ssl->s3->ech_status == ssl_ech_rejected) { |
| 921 |
*out_name = reinterpret_cast<const char *>( |
| 922 |
hs->selected_ech_config->public_name.data()); |
| 923 |
*out_name_len = hs->selected_ech_config->public_name.size(); |
| 924 |
} else { |
| 925 |
*out_name = nullptr; |
| 926 |
*out_name_len = 0; |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
void SSL_get0_ech_retry_configs( |
| 931 |
const SSL *ssl, const uint8_t **out_retry_configs, |
| 932 |
size_t *out_retry_configs_len) { |
| 933 |
const SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
| 934 |
if (!hs || !hs->ech_authenticated_reject) { |
| 935 |
// It is an error to call this function except in response to |
| 936 |
// |SSL_R_ECH_REJECTED|. Returning an empty string risks the caller |
| 937 |
// mistakenly believing the server has disabled ECH. Instead, return a |
| 938 |
// non-empty ECHConfigList with a syntax error, so the subsequent |
| 939 |
// |SSL_set1_ech_config_list| call will fail. |
| 940 |
assert(0); |
| 941 |
static const uint8_t kPlaceholder[] = { |
| 942 |
kECHConfigVersion >> 8, kECHConfigVersion & 0xff, 0xff, 0xff, 0xff}; |
| 943 |
*out_retry_configs = kPlaceholder; |
| 944 |
*out_retry_configs_len = sizeof(kPlaceholder); |
| 945 |
return; |
| 946 |
} |
| 947 |
|
| 948 |
*out_retry_configs = hs->ech_retry_configs.data(); |
| 949 |
*out_retry_configs_len = hs->ech_retry_configs.size(); |
| 950 |
} |
| 951 |
|
| 952 |
int SSL_marshal_ech_config(uint8_t **out, size_t *out_len, uint8_t config_id, |
| 953 |
const EVP_HPKE_KEY *key, const char *public_name, |
| 954 |
size_t max_name_len) { |
| 955 |
Span<const uint8_t> public_name_u8 = MakeConstSpan( |
| 956 |
reinterpret_cast<const uint8_t *>(public_name), strlen(public_name)); |
| 957 |
if (!ssl_is_valid_ech_public_name(public_name_u8)) { |
| 958 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ECH_PUBLIC_NAME); |
| 959 |
return 0; |
| 960 |
} |
| 961 |
|
| 962 |
// The maximum name length is encoded in one byte. |
| 963 |
if (max_name_len > 0xff) { |
| 964 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH); |
| 965 |
return 0; |
| 966 |
} |
| 967 |
|
| 968 |
// See draft-ietf-tls-esni-13, section 4. |
| 969 |
ScopedCBB cbb; |
| 970 |
CBB contents, child; |
| 971 |
uint8_t *public_key; |
| 972 |
size_t public_key_len; |
| 973 |
if (!CBB_init(cbb.get(), 128) || // |
| 974 |
!CBB_add_u16(cbb.get(), kECHConfigVersion) || |
| 975 |
!CBB_add_u16_length_prefixed(cbb.get(), &contents) || |
| 976 |
!CBB_add_u8(&contents, config_id) || |
| 977 |
!CBB_add_u16(&contents, EVP_HPKE_KEM_id(EVP_HPKE_KEY_kem(key))) || |
| 978 |
!CBB_add_u16_length_prefixed(&contents, &child) || |
| 979 |
!CBB_reserve(&child, &public_key, EVP_HPKE_MAX_PUBLIC_KEY_LENGTH) || |
| 980 |
!EVP_HPKE_KEY_public_key(key, public_key, &public_key_len, |
| 981 |
EVP_HPKE_MAX_PUBLIC_KEY_LENGTH) || |
| 982 |
!CBB_did_write(&child, public_key_len) || |
| 983 |
!CBB_add_u16_length_prefixed(&contents, &child) || |
| 984 |
// Write a default cipher suite configuration. |
| 985 |
!CBB_add_u16(&child, EVP_HPKE_HKDF_SHA256) || |
| 986 |
!CBB_add_u16(&child, EVP_HPKE_AES_128_GCM) || |
| 987 |
!CBB_add_u16(&child, EVP_HPKE_HKDF_SHA256) || |
| 988 |
!CBB_add_u16(&child, EVP_HPKE_CHACHA20_POLY1305) || |
| 989 |
!CBB_add_u8(&contents, max_name_len) || |
| 990 |
!CBB_add_u8_length_prefixed(&contents, &child) || |
| 991 |
!CBB_add_bytes(&child, public_name_u8.data(), public_name_u8.size()) || |
| 992 |
// TODO(https://crbug.com/boringssl/275): Reserve some GREASE extensions |
| 993 |
// and include some. |
| 994 |
!CBB_add_u16(&contents, 0 /* no extensions */) || |
| 995 |
!CBB_finish(cbb.get(), out, out_len)) { |
| 996 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 997 |
return 0; |
| 998 |
} |
| 999 |
return 1; |
| 1000 |
} |
| 1001 |
|
| 1002 |
SSL_ECH_KEYS *SSL_ECH_KEYS_new() { return New<SSL_ECH_KEYS>(); } |
| 1003 |
|
| 1004 |
void SSL_ECH_KEYS_up_ref(SSL_ECH_KEYS *keys) { keys->UpRefInternal(); } |
| 1005 |
|
| 1006 |
void SSL_ECH_KEYS_free(SSL_ECH_KEYS *keys) { |
| 1007 |
if (keys != nullptr) { |
| 1008 |
keys->DecRefInternal(); |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
int SSL_ECH_KEYS_add(SSL_ECH_KEYS *configs, int is_retry_config, |
| 1013 |
const uint8_t *ech_config, size_t ech_config_len, |
| 1014 |
const EVP_HPKE_KEY *key) { |
| 1015 |
UniquePtr<ECHServerConfig> parsed_config = MakeUnique<ECHServerConfig>(); |
| 1016 |
if (!parsed_config) { |
| 1017 |
return 0; |
| 1018 |
} |
| 1019 |
if (!parsed_config->Init(MakeConstSpan(ech_config, ech_config_len), key, |
| 1020 |
!!is_retry_config)) { |
| 1021 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1022 |
return 0; |
| 1023 |
} |
| 1024 |
if (!configs->configs.Push(std::move(parsed_config))) { |
| 1025 |
return 0; |
| 1026 |
} |
| 1027 |
return 1; |
| 1028 |
} |
| 1029 |
|
| 1030 |
int SSL_ECH_KEYS_has_duplicate_config_id(const SSL_ECH_KEYS *keys) { |
| 1031 |
bool seen[256] = {false}; |
| 1032 |
for (const auto &config : keys->configs) { |
| 1033 |
if (seen[config->ech_config().config_id]) { |
| 1034 |
return 1; |
| 1035 |
} |
| 1036 |
seen[config->ech_config().config_id] = true; |
| 1037 |
} |
| 1038 |
return 0; |
| 1039 |
} |
| 1040 |
|
| 1041 |
int SSL_ECH_KEYS_marshal_retry_configs(const SSL_ECH_KEYS *keys, uint8_t **out, |
| 1042 |
size_t *out_len) { |
| 1043 |
ScopedCBB cbb; |
| 1044 |
CBB child; |
| 1045 |
if (!CBB_init(cbb.get(), 128) || |
| 1046 |
!CBB_add_u16_length_prefixed(cbb.get(), &child)) { |
| 1047 |
return false; |
| 1048 |
} |
| 1049 |
for (const auto &config : keys->configs) { |
| 1050 |
if (config->is_retry_config() && |
| 1051 |
!CBB_add_bytes(&child, config->ech_config().raw.data(), |
| 1052 |
config->ech_config().raw.size())) { |
| 1053 |
return false; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
return CBB_finish(cbb.get(), out, out_len); |
| 1057 |
} |
| 1058 |
|
| 1059 |
int SSL_CTX_set1_ech_keys(SSL_CTX *ctx, SSL_ECH_KEYS *keys) { |
| 1060 |
bool has_retry_config = false; |
| 1061 |
for (const auto &config : keys->configs) { |
| 1062 |
if (config->is_retry_config()) { |
| 1063 |
has_retry_config = true; |
| 1064 |
break; |
| 1065 |
} |
| 1066 |
} |
| 1067 |
if (!has_retry_config) { |
| 1068 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ECH_SERVER_WOULD_HAVE_NO_RETRY_CONFIGS); |
| 1069 |
return 0; |
| 1070 |
} |
| 1071 |
UniquePtr<SSL_ECH_KEYS> owned_keys = UpRef(keys); |
| 1072 |
MutexWriteLock lock(&ctx->lock); |
| 1073 |
ctx->ech_keys.swap(owned_keys); |
| 1074 |
return 1; |
| 1075 |
} |
| 1076 |
|
| 1077 |
int SSL_ech_accepted(const SSL *ssl) { |
| 1078 |
if (SSL_in_early_data(ssl) && !ssl->server) { |
| 1079 |
// In the client early data state, we report properties as if the server |
| 1080 |
// accepted early data. The server can only accept early data with |
| 1081 |
// ClientHelloInner. |
| 1082 |
return ssl->s3->hs->selected_ech_config != nullptr; |
| 1083 |
} |
| 1084 |
|
| 1085 |
return ssl->s3->ech_status == ssl_ech_accepted; |
| 1086 |
} |