| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
// Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#include <openssl/ssl.h> |
| 6 |
|
| 7 |
#include <assert.h> |
| 8 |
#include <limits.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include <algorithm> |
| 13 |
#include <utility> |
| 14 |
|
| 15 |
#include <openssl/aead.h> |
| 16 |
#include <openssl/bytestring.h> |
| 17 |
#include <openssl/chacha.h> |
| 18 |
#include <openssl/curve25519.h> |
| 19 |
#include <openssl/digest.h> |
| 20 |
#include <openssl/err.h> |
| 21 |
#include <openssl/evp.h> |
| 22 |
#include <openssl/hmac.h> |
| 23 |
#include <openssl/hpke.h> |
| 24 |
#include <openssl/mem.h> |
| 25 |
#include <openssl/nid.h> |
| 26 |
#include <openssl/rand.h> |
| 27 |
|
| 28 |
#include "../crypto/internal.h" |
| 29 |
#include "internal.h" |
| 30 |
|
| 31 |
|
| 32 |
BSSL_NAMESPACE_BEGIN |
| 33 |
|
| 34 |
static bool ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs); |
| 35 |
static bool ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs); |
| 36 |
|
| 37 |
static int compare_uint16_t(const void *p1, const void *p2) { |
| 38 |
uint16_t u1 = *((const uint16_t *)p1); |
| 39 |
uint16_t u2 = *((const uint16_t *)p2); |
| 40 |
if (u1 < u2) { |
| 41 |
return -1; |
| 42 |
} else if (u1 > u2) { |
| 43 |
return 1; |
| 44 |
} else { |
| 45 |
return 0; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be |
| 50 |
// more than one extension of the same type in a ClientHello or ServerHello. |
| 51 |
// This function does an initial scan over the extensions block to filter those |
| 52 |
// out. |
| 53 |
static bool tls1_check_duplicate_extensions(const CBS *cbs) { |
| 54 |
// First pass: count the extensions. |
| 55 |
size_t num_extensions = 0; |
| 56 |
CBS extensions = *cbs; |
| 57 |
while (CBS_len(&extensions) > 0) { |
| 58 |
uint16_t type; |
| 59 |
CBS extension; |
| 60 |
|
| 61 |
if (!CBS_get_u16(&extensions, &type) || |
| 62 |
!CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
num_extensions++; |
| 67 |
} |
| 68 |
|
| 69 |
if (num_extensions == 0) { |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
Array<uint16_t> extension_types; |
| 74 |
if (!extension_types.Init(num_extensions)) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
// Second pass: gather the extension types. |
| 79 |
extensions = *cbs; |
| 80 |
for (size_t i = 0; i < extension_types.size(); i++) { |
| 81 |
CBS extension; |
| 82 |
|
| 83 |
if (!CBS_get_u16(&extensions, &extension_types[i]) || |
| 84 |
!CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 85 |
// This should not happen. |
| 86 |
return false; |
| 87 |
} |
| 88 |
} |
| 89 |
assert(CBS_len(&extensions) == 0); |
| 90 |
|
| 91 |
// Sort the extensions and make sure there are no duplicates. |
| 92 |
qsort(extension_types.data(), extension_types.size(), sizeof(uint16_t), |
| 93 |
compare_uint16_t); |
| 94 |
for (size_t i = 1; i < num_extensions; i++) { |
| 95 |
if (extension_types[i - 1] == extension_types[i]) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
static bool is_post_quantum_group(uint16_t id) { |
| 104 |
for (const uint16_t pq_group_id : PQGroups()) { |
| 105 |
if (id == pq_group_id) { |
| 106 |
return true; |
| 107 |
} |
| 108 |
} |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
bool ssl_client_hello_init(const SSL *ssl, SSL_CLIENT_HELLO *out, |
| 113 |
Span<const uint8_t> body) { |
| 114 |
CBS cbs = body; |
| 115 |
if (!ssl_parse_client_hello_with_trailing_data(ssl, &cbs, out) || |
| 116 |
CBS_len(&cbs) != 0) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
bool ssl_parse_client_hello_with_trailing_data(const SSL *ssl, CBS *cbs, |
| 123 |
SSL_CLIENT_HELLO *out) { |
| 124 |
OPENSSL_memset(out, 0, sizeof(*out)); |
| 125 |
out->ssl = const_cast<SSL *>(ssl); |
| 126 |
|
| 127 |
CBS copy = *cbs; |
| 128 |
CBS random, session_id; |
| 129 |
if (!CBS_get_u16(cbs, &out->version) || |
| 130 |
!CBS_get_bytes(cbs, &random, SSL3_RANDOM_SIZE) || |
| 131 |
!CBS_get_u8_length_prefixed(cbs, &session_id) || |
| 132 |
CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
out->random = CBS_data(&random); |
| 137 |
out->random_len = CBS_len(&random); |
| 138 |
out->session_id = CBS_data(&session_id); |
| 139 |
out->session_id_len = CBS_len(&session_id); |
| 140 |
|
| 141 |
// Skip past DTLS cookie |
| 142 |
if (SSL_is_dtls(out->ssl)) { |
| 143 |
CBS cookie; |
| 144 |
if (!CBS_get_u8_length_prefixed(cbs, &cookie)) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
CBS cipher_suites, compression_methods; |
| 150 |
if (!CBS_get_u16_length_prefixed(cbs, &cipher_suites) || |
| 151 |
CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 || |
| 152 |
!CBS_get_u8_length_prefixed(cbs, &compression_methods) || |
| 153 |
CBS_len(&compression_methods) < 1) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
out->cipher_suites = CBS_data(&cipher_suites); |
| 158 |
out->cipher_suites_len = CBS_len(&cipher_suites); |
| 159 |
out->compression_methods = CBS_data(&compression_methods); |
| 160 |
out->compression_methods_len = CBS_len(&compression_methods); |
| 161 |
|
| 162 |
// If the ClientHello ends here then it's valid, but doesn't have any |
| 163 |
// extensions. |
| 164 |
if (CBS_len(cbs) == 0) { |
| 165 |
out->extensions = nullptr; |
| 166 |
out->extensions_len = 0; |
| 167 |
} else { |
| 168 |
// Extract extensions and check it is valid. |
| 169 |
CBS extensions; |
| 170 |
if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 171 |
!tls1_check_duplicate_extensions(&extensions)) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
out->extensions = CBS_data(&extensions); |
| 175 |
out->extensions_len = CBS_len(&extensions); |
| 176 |
} |
| 177 |
|
| 178 |
out->client_hello = CBS_data(©); |
| 179 |
out->client_hello_len = CBS_len(©) - CBS_len(cbs); |
| 180 |
return true; |
| 181 |
} |
| 182 |
|
| 183 |
bool ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello, |
| 184 |
CBS *out, uint16_t extension_type) { |
| 185 |
CBS extensions; |
| 186 |
CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len); |
| 187 |
while (CBS_len(&extensions) != 0) { |
| 188 |
// Decode the next extension. |
| 189 |
uint16_t type; |
| 190 |
CBS extension; |
| 191 |
if (!CBS_get_u16(&extensions, &type) || |
| 192 |
!CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
if (type == extension_type) { |
| 197 |
*out = extension; |
| 198 |
return true; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
static const uint16_t kDefaultGroups[] = { |
| 206 |
SSL_GROUP_X25519_MLKEM768, |
| 207 |
SSL_GROUP_SECP256R1_MLKEM768, |
| 208 |
SSL_GROUP_SECP384R1_MLKEM1024, |
| 209 |
SSL_GROUP_X25519, |
| 210 |
SSL_GROUP_SECP256R1, |
| 211 |
SSL_GROUP_SECP384R1, |
| 212 |
}; |
| 213 |
|
| 214 |
Span<const uint16_t> tls1_get_default_grouplist(void) { |
| 215 |
return Span<const uint16_t>(kDefaultGroups); |
| 216 |
} |
| 217 |
|
| 218 |
Span<const uint16_t> tls1_get_grouplist(const SSL_HANDSHAKE *hs) { |
| 219 |
if (!hs->config->supported_group_list.empty()) { |
| 220 |
return hs->config->supported_group_list; |
| 221 |
} |
| 222 |
return Span<const uint16_t>(kDefaultGroups); |
| 223 |
} |
| 224 |
|
| 225 |
bool tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) { |
| 226 |
SSL *const ssl = hs->ssl; |
| 227 |
assert(ssl->server); |
| 228 |
|
| 229 |
// Clients are not required to send a supported_groups extension. In this |
| 230 |
// case, the server is free to pick any group it likes. See RFC 4492, |
| 231 |
// section 4, paragraph 3. |
| 232 |
// |
| 233 |
// However, in the interests of compatibility, we will skip ECDH if the |
| 234 |
// client didn't send an extension because we can't be sure that they'll |
| 235 |
// support our favoured group. Thus we do not special-case an emtpy |
| 236 |
// |peer_supported_group_list|. |
| 237 |
|
| 238 |
Span<const uint16_t> groups = tls1_get_grouplist(hs); |
| 239 |
Span<const uint16_t> pref, supp; |
| 240 |
if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
| 241 |
pref = groups; |
| 242 |
supp = hs->peer_supported_group_list; |
| 243 |
} else { |
| 244 |
pref = hs->peer_supported_group_list; |
| 245 |
supp = groups; |
| 246 |
} |
| 247 |
|
| 248 |
for (uint16_t pref_group : pref) { |
| 249 |
for (uint16_t supp_group : supp) { |
| 250 |
if (pref_group == supp_group) { |
| 251 |
// PQ groups require TLS 1.3 or later |
| 252 |
if (!is_post_quantum_group(pref_group) || |
| 253 |
ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 254 |
*out_group_id = pref_group; |
| 255 |
return true; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
return false; |
| 262 |
} |
| 263 |
|
| 264 |
bool tls1_check_group_id(const SSL_HANDSHAKE *hs, uint16_t group_id) { |
| 265 |
if (is_post_quantum_group(group_id) && |
| 266 |
ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) { |
| 267 |
// Post-quantum "groups" require TLS 1.3. |
| 268 |
return false; |
| 269 |
} |
| 270 |
|
| 271 |
// We internally assume zero is never allocated as a group ID. |
| 272 |
if (group_id == 0) { |
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
for (uint16_t supported : tls1_get_grouplist(hs)) { |
| 277 |
if (supported == group_id) { |
| 278 |
return true; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
return false; |
| 283 |
} |
| 284 |
|
| 285 |
// kVerifySignatureAlgorithms is the default list of accepted signature |
| 286 |
// algorithms for verifying. |
| 287 |
static const uint16_t kVerifySignatureAlgorithms[] = { |
| 288 |
// List our preferred algorithms first. |
| 289 |
SSL_SIGN_ECDSA_SECP256R1_SHA256, |
| 290 |
SSL_SIGN_RSA_PSS_RSAE_SHA256, |
| 291 |
SSL_SIGN_RSA_PKCS1_SHA256, |
| 292 |
|
| 293 |
// Larger hashes are acceptable. |
| 294 |
SSL_SIGN_ECDSA_SECP384R1_SHA384, |
| 295 |
SSL_SIGN_RSA_PSS_RSAE_SHA384, |
| 296 |
SSL_SIGN_RSA_PKCS1_SHA384, |
| 297 |
|
| 298 |
SSL_SIGN_ECDSA_SECP521R1_SHA512, |
| 299 |
SSL_SIGN_RSA_PSS_RSAE_SHA512, |
| 300 |
SSL_SIGN_RSA_PKCS1_SHA512, |
| 301 |
|
| 302 |
// For now, SHA-1 is still accepted but least preferable. |
| 303 |
SSL_SIGN_RSA_PKCS1_SHA1, |
| 304 |
}; |
| 305 |
|
| 306 |
// kSignSignatureAlgorithms is the default list of supported signature |
| 307 |
// algorithms for signing. |
| 308 |
static const uint16_t kSignSignatureAlgorithms[] = { |
| 309 |
// List our preferred algorithms first. |
| 310 |
SSL_SIGN_ED25519, |
| 311 |
SSL_SIGN_ECDSA_SECP256R1_SHA256, |
| 312 |
SSL_SIGN_RSA_PSS_RSAE_SHA256, |
| 313 |
SSL_SIGN_RSA_PKCS1_SHA256, |
| 314 |
|
| 315 |
// If needed, sign larger hashes. |
| 316 |
// |
| 317 |
// TODO(davidben): Determine which of these may be pruned. |
| 318 |
SSL_SIGN_ECDSA_SECP384R1_SHA384, |
| 319 |
SSL_SIGN_RSA_PSS_RSAE_SHA384, |
| 320 |
SSL_SIGN_RSA_PKCS1_SHA384, |
| 321 |
|
| 322 |
SSL_SIGN_ECDSA_SECP521R1_SHA512, |
| 323 |
SSL_SIGN_RSA_PSS_RSAE_SHA512, |
| 324 |
SSL_SIGN_RSA_PKCS1_SHA512, |
| 325 |
|
| 326 |
// If the peer supports nothing else, sign with SHA-1. |
| 327 |
SSL_SIGN_ECDSA_SHA1, |
| 328 |
SSL_SIGN_RSA_PKCS1_SHA1, |
| 329 |
}; |
| 330 |
|
| 331 |
static Span<const uint16_t> tls12_get_verify_sigalgs(const SSL_HANDSHAKE *hs) { |
| 332 |
if (hs->config->verify_sigalgs.empty()) { |
| 333 |
return Span<const uint16_t>(kVerifySignatureAlgorithms); |
| 334 |
} |
| 335 |
return hs->config->verify_sigalgs; |
| 336 |
} |
| 337 |
|
| 338 |
bool tls12_add_verify_sigalgs(const SSL_HANDSHAKE *hs, CBB *out) { |
| 339 |
for (uint16_t sigalg : tls12_get_verify_sigalgs(hs)) { |
| 340 |
if (!CBB_add_u16(out, sigalg)) { |
| 341 |
return false; |
| 342 |
} |
| 343 |
} |
| 344 |
return true; |
| 345 |
} |
| 346 |
|
| 347 |
bool tls12_check_peer_sigalg(const SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 348 |
uint16_t sigalg) { |
| 349 |
for (uint16_t verify_sigalg : tls12_get_verify_sigalgs(hs)) { |
| 350 |
if (verify_sigalg == sigalg) { |
| 351 |
return true; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE); |
| 356 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 357 |
return false; |
| 358 |
} |
| 359 |
|
| 360 |
// tls_extension represents a TLS extension that is handled internally. |
| 361 |
// |
| 362 |
// The parse callbacks receive a |CBS| that contains the contents of the |
| 363 |
// extension (i.e. not including the type and length bytes). If an extension is |
| 364 |
// not received then the parse callbacks will be called with a NULL CBS so that |
| 365 |
// they can do any processing needed to handle the absence of an extension. |
| 366 |
// |
| 367 |
// The add callbacks receive a |CBB| to which the extension can be appended but |
| 368 |
// the function is responsible for appending the type and length bytes too. |
| 369 |
// |
| 370 |
// |add_clienthello| may be called multiple times and must not mutate |hs|. It |
| 371 |
// is additionally passed two output |CBB|s. If the extension is the same |
| 372 |
// independent of the value of |type|, the callback may write to |
| 373 |
// |out_compressible| instead of |out|. When serializing the ClientHelloInner, |
| 374 |
// all compressible extensions will be made continguous and replaced with |
| 375 |
// ech_outer_extensions when encrypted. When serializing the ClientHelloOuter |
| 376 |
// or not offering ECH, |out| will be equal to |out_compressible|, so writing to |
| 377 |
// |out_compressible| still works. |
| 378 |
// |
| 379 |
// Note the |parse_serverhello| and |add_serverhello| callbacks refer to the |
| 380 |
// TLS 1.2 ServerHello. In TLS 1.3, these callbacks act on EncryptedExtensions, |
| 381 |
// with ServerHello extensions handled elsewhere in the handshake. |
| 382 |
// |
| 383 |
// All callbacks return true for success and false for error. If a parse |
| 384 |
// function returns zero then a fatal alert with value |*out_alert| will be |
| 385 |
// sent. If |*out_alert| isn't set, then a |decode_error| alert will be sent. |
| 386 |
struct tls_extension { |
| 387 |
uint16_t value; |
| 388 |
|
| 389 |
bool (*add_clienthello)(const SSL_HANDSHAKE *hs, CBB *out, |
| 390 |
CBB *out_compressible, ssl_client_hello_type_t type); |
| 391 |
bool (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 392 |
CBS *contents); |
| 393 |
|
| 394 |
bool (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 395 |
CBS *contents); |
| 396 |
bool (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out); |
| 397 |
}; |
| 398 |
|
| 399 |
static bool forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 400 |
CBS *contents) { |
| 401 |
if (contents != NULL) { |
| 402 |
// Servers MUST NOT send this extension. |
| 403 |
*out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 404 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 405 |
return false; |
| 406 |
} |
| 407 |
|
| 408 |
return true; |
| 409 |
} |
| 410 |
|
| 411 |
static bool ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 412 |
CBS *contents) { |
| 413 |
// This extension from the client is handled elsewhere. |
| 414 |
return true; |
| 415 |
} |
| 416 |
|
| 417 |
static bool dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 418 |
return true; |
| 419 |
} |
| 420 |
|
| 421 |
// Server name indication (SNI). |
| 422 |
// |
| 423 |
// https://tools.ietf.org/html/rfc6066#section-3. |
| 424 |
|
| 425 |
static bool ext_sni_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 426 |
CBB *out_compressible, |
| 427 |
ssl_client_hello_type_t type) { |
| 428 |
const SSL *const ssl = hs->ssl; |
| 429 |
// If offering ECH, send the public name instead of the configured name. |
| 430 |
Span<const uint8_t> hostname; |
| 431 |
if (type == ssl_client_hello_outer) { |
| 432 |
hostname = hs->selected_ech_config->public_name; |
| 433 |
} else { |
| 434 |
if (ssl->hostname == nullptr) { |
| 435 |
return true; |
| 436 |
} |
| 437 |
hostname = |
| 438 |
MakeConstSpan(reinterpret_cast<const uint8_t *>(ssl->hostname.get()), |
| 439 |
strlen(ssl->hostname.get())); |
| 440 |
} |
| 441 |
|
| 442 |
CBB contents, server_name_list, name; |
| 443 |
if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 444 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 445 |
!CBB_add_u16_length_prefixed(&contents, &server_name_list) || |
| 446 |
!CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) || |
| 447 |
!CBB_add_u16_length_prefixed(&server_name_list, &name) || |
| 448 |
!CBB_add_bytes(&name, hostname.data(), hostname.size()) || |
| 449 |
!CBB_flush(out)) { |
| 450 |
return false; |
| 451 |
} |
| 452 |
|
| 453 |
return true; |
| 454 |
} |
| 455 |
|
| 456 |
static bool ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 457 |
CBS *contents) { |
| 458 |
// The server may acknowledge SNI with an empty extension. We check the syntax |
| 459 |
// but otherwise ignore this signal. |
| 460 |
return contents == NULL || CBS_len(contents) == 0; |
| 461 |
} |
| 462 |
|
| 463 |
static bool ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 464 |
CBS *contents) { |
| 465 |
// SNI has already been parsed earlier in the handshake. See |extract_sni|. |
| 466 |
return true; |
| 467 |
} |
| 468 |
|
| 469 |
static bool ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 470 |
if (hs->ssl->s3->session_reused || |
| 471 |
!hs->should_ack_sni) { |
| 472 |
return true; |
| 473 |
} |
| 474 |
|
| 475 |
if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 476 |
!CBB_add_u16(out, 0 /* length */)) { |
| 477 |
return false; |
| 478 |
} |
| 479 |
|
| 480 |
return true; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
// Encrypted ClientHello (ECH) |
| 485 |
// |
| 486 |
// https://tools.ietf.org/html/draft-ietf-tls-esni-13 |
| 487 |
|
| 488 |
static bool ext_ech_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 489 |
CBB *out_compressible, |
| 490 |
ssl_client_hello_type_t type) { |
| 491 |
if (type == ssl_client_hello_inner) { |
| 492 |
if (!CBB_add_u16(out, TLSEXT_TYPE_encrypted_client_hello) || |
| 493 |
!CBB_add_u16(out, /* length */ 1) || |
| 494 |
!CBB_add_u8(out, ECH_CLIENT_INNER)) { |
| 495 |
return false; |
| 496 |
} |
| 497 |
return true; |
| 498 |
} |
| 499 |
|
| 500 |
if (hs->ech_client_outer.empty()) { |
| 501 |
return true; |
| 502 |
} |
| 503 |
|
| 504 |
CBB ech_body; |
| 505 |
if (!CBB_add_u16(out, TLSEXT_TYPE_encrypted_client_hello) || |
| 506 |
!CBB_add_u16_length_prefixed(out, &ech_body) || |
| 507 |
!CBB_add_u8(&ech_body, ECH_CLIENT_OUTER) || |
| 508 |
!CBB_add_bytes(&ech_body, hs->ech_client_outer.data(), |
| 509 |
hs->ech_client_outer.size()) || |
| 510 |
!CBB_flush(out)) { |
| 511 |
return false; |
| 512 |
} |
| 513 |
return true; |
| 514 |
} |
| 515 |
|
| 516 |
static bool ext_ech_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 517 |
CBS *contents) { |
| 518 |
SSL *const ssl = hs->ssl; |
| 519 |
if (contents == NULL) { |
| 520 |
return true; |
| 521 |
} |
| 522 |
|
| 523 |
// The ECH extension may not be sent in TLS 1.2 ServerHello, only TLS 1.3 |
| 524 |
// EncryptedExtensions. It also may not be sent in response to an inner ECH |
| 525 |
// extension. |
| 526 |
if (ssl_protocol_version(ssl) < TLS1_3_VERSION || |
| 527 |
ssl->s3->ech_status == ssl_ech_accepted) { |
| 528 |
*out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 529 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 530 |
return false; |
| 531 |
} |
| 532 |
|
| 533 |
if (!ssl_is_valid_ech_config_list(*contents)) { |
| 534 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 535 |
return false; |
| 536 |
} |
| 537 |
|
| 538 |
if (ssl->s3->ech_status == ssl_ech_rejected && |
| 539 |
!hs->ech_retry_configs.CopyFrom(*contents)) { |
| 540 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 541 |
return false; |
| 542 |
} |
| 543 |
|
| 544 |
return true; |
| 545 |
} |
| 546 |
|
| 547 |
static bool ext_ech_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 548 |
CBS *contents) { |
| 549 |
if (contents == nullptr) { |
| 550 |
return true; |
| 551 |
} |
| 552 |
|
| 553 |
uint8_t type; |
| 554 |
if (!CBS_get_u8(contents, &type)) { |
| 555 |
return false; |
| 556 |
} |
| 557 |
if (type == ECH_CLIENT_OUTER) { |
| 558 |
// Outer ECH extensions are handled outside the callback. |
| 559 |
return true; |
| 560 |
} |
| 561 |
if (type != ECH_CLIENT_INNER || CBS_len(contents) != 0) { |
| 562 |
return false; |
| 563 |
} |
| 564 |
|
| 565 |
hs->ech_is_inner = true; |
| 566 |
return true; |
| 567 |
} |
| 568 |
|
| 569 |
static bool ext_ech_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 570 |
SSL *const ssl = hs->ssl; |
| 571 |
if (ssl_protocol_version(ssl) < TLS1_3_VERSION || |
| 572 |
ssl->s3->ech_status == ssl_ech_accepted || // |
| 573 |
hs->ech_keys == nullptr) { |
| 574 |
return true; |
| 575 |
} |
| 576 |
|
| 577 |
// Write the list of retry configs to |out|. Note |SSL_CTX_set1_ech_keys| |
| 578 |
// ensures |ech_keys| contains at least one retry config. |
| 579 |
CBB body, retry_configs; |
| 580 |
if (!CBB_add_u16(out, TLSEXT_TYPE_encrypted_client_hello) || |
| 581 |
!CBB_add_u16_length_prefixed(out, &body) || |
| 582 |
!CBB_add_u16_length_prefixed(&body, &retry_configs)) { |
| 583 |
return false; |
| 584 |
} |
| 585 |
for (const auto &config : hs->ech_keys->configs) { |
| 586 |
if (!config->is_retry_config()) { |
| 587 |
continue; |
| 588 |
} |
| 589 |
if (!CBB_add_bytes(&retry_configs, config->ech_config().raw.data(), |
| 590 |
config->ech_config().raw.size())) { |
| 591 |
return false; |
| 592 |
} |
| 593 |
} |
| 594 |
return CBB_flush(out); |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
// Renegotiation indication. |
| 599 |
// |
| 600 |
// https://tools.ietf.org/html/rfc5746 |
| 601 |
|
| 602 |
static bool ext_ri_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 603 |
CBB *out_compressible, |
| 604 |
ssl_client_hello_type_t type) { |
| 605 |
const SSL *const ssl = hs->ssl; |
| 606 |
// Renegotiation indication is not necessary in TLS 1.3. |
| 607 |
if (hs->min_version >= TLS1_3_VERSION || |
| 608 |
type == ssl_client_hello_inner) { |
| 609 |
return true; |
| 610 |
} |
| 611 |
|
| 612 |
assert(ssl->s3->initial_handshake_complete == |
| 613 |
(ssl->s3->previous_client_finished_len != 0)); |
| 614 |
|
| 615 |
CBB contents, prev_finished; |
| 616 |
if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
| 617 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 618 |
!CBB_add_u8_length_prefixed(&contents, &prev_finished) || |
| 619 |
!CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished, |
| 620 |
ssl->s3->previous_client_finished_len) || |
| 621 |
!CBB_flush(out)) { |
| 622 |
return false; |
| 623 |
} |
| 624 |
|
| 625 |
return true; |
| 626 |
} |
| 627 |
|
| 628 |
static bool ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 629 |
CBS *contents) { |
| 630 |
SSL *const ssl = hs->ssl; |
| 631 |
if (contents != NULL && ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 632 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 633 |
return false; |
| 634 |
} |
| 635 |
|
| 636 |
// Servers may not switch between omitting the extension and supporting it. |
| 637 |
// See RFC 5746, sections 3.5 and 4.2. |
| 638 |
if (ssl->s3->initial_handshake_complete && |
| 639 |
(contents != NULL) != ssl->s3->send_connection_binding) { |
| 640 |
*out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 641 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
| 642 |
return false; |
| 643 |
} |
| 644 |
|
| 645 |
if (contents == NULL) { |
| 646 |
// Strictly speaking, if we want to avoid an attack we should *always* see |
| 647 |
// RI even on initial ServerHello because the client doesn't see any |
| 648 |
// renegotiation during an attack. However this would mean we could not |
| 649 |
// connect to any server which doesn't support RI. |
| 650 |
// |
| 651 |
// OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in |
| 652 |
// practical terms every client sets it so it's just assumed here. |
| 653 |
return true; |
| 654 |
} |
| 655 |
|
| 656 |
const size_t expected_len = ssl->s3->previous_client_finished_len + |
| 657 |
ssl->s3->previous_server_finished_len; |
| 658 |
|
| 659 |
// Check for logic errors |
| 660 |
assert(!expected_len || ssl->s3->previous_client_finished_len); |
| 661 |
assert(!expected_len || ssl->s3->previous_server_finished_len); |
| 662 |
assert(ssl->s3->initial_handshake_complete == |
| 663 |
(ssl->s3->previous_client_finished_len != 0)); |
| 664 |
assert(ssl->s3->initial_handshake_complete == |
| 665 |
(ssl->s3->previous_server_finished_len != 0)); |
| 666 |
|
| 667 |
// Parse out the extension contents. |
| 668 |
CBS renegotiated_connection; |
| 669 |
if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 670 |
CBS_len(contents) != 0) { |
| 671 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
| 672 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 673 |
return false; |
| 674 |
} |
| 675 |
|
| 676 |
// Check that the extension matches. |
| 677 |
if (CBS_len(&renegotiated_connection) != expected_len) { |
| 678 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
| 679 |
*out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 680 |
return false; |
| 681 |
} |
| 682 |
|
| 683 |
const uint8_t *d = CBS_data(&renegotiated_connection); |
| 684 |
bool ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished, |
| 685 |
ssl->s3->previous_client_finished_len) == 0; |
| 686 |
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 687 |
ok = true; |
| 688 |
#endif |
| 689 |
if (!ok) { |
| 690 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
| 691 |
*out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 692 |
return false; |
| 693 |
} |
| 694 |
d += ssl->s3->previous_client_finished_len; |
| 695 |
|
| 696 |
ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished, |
| 697 |
ssl->s3->previous_server_finished_len) == 0; |
| 698 |
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 699 |
ok = true; |
| 700 |
#endif |
| 701 |
if (!ok) { |
| 702 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
| 703 |
*out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 704 |
return false; |
| 705 |
} |
| 706 |
ssl->s3->send_connection_binding = true; |
| 707 |
|
| 708 |
return true; |
| 709 |
} |
| 710 |
|
| 711 |
static bool ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 712 |
CBS *contents) { |
| 713 |
SSL *const ssl = hs->ssl; |
| 714 |
// Renegotiation isn't supported as a server so this function should never be |
| 715 |
// called after the initial handshake. |
| 716 |
assert(!ssl->s3->initial_handshake_complete); |
| 717 |
|
| 718 |
if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 719 |
return true; |
| 720 |
} |
| 721 |
|
| 722 |
if (contents == NULL) { |
| 723 |
return true; |
| 724 |
} |
| 725 |
|
| 726 |
CBS renegotiated_connection; |
| 727 |
if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 728 |
CBS_len(contents) != 0) { |
| 729 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
| 730 |
return false; |
| 731 |
} |
| 732 |
|
| 733 |
// Check that the extension matches. We do not support renegotiation as a |
| 734 |
// server, so this must be empty. |
| 735 |
if (CBS_len(&renegotiated_connection) != 0) { |
| 736 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
| 737 |
*out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 738 |
return false; |
| 739 |
} |
| 740 |
|
| 741 |
ssl->s3->send_connection_binding = true; |
| 742 |
|
| 743 |
return true; |
| 744 |
} |
| 745 |
|
| 746 |
static bool ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 747 |
SSL *const ssl = hs->ssl; |
| 748 |
// Renegotiation isn't supported as a server so this function should never be |
| 749 |
// called after the initial handshake. |
| 750 |
assert(!ssl->s3->initial_handshake_complete); |
| 751 |
|
| 752 |
if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 753 |
return true; |
| 754 |
} |
| 755 |
|
| 756 |
if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
| 757 |
!CBB_add_u16(out, 1 /* length */) || |
| 758 |
!CBB_add_u8(out, 0 /* empty renegotiation info */)) { |
| 759 |
return false; |
| 760 |
} |
| 761 |
|
| 762 |
return true; |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
// Extended Master Secret. |
| 767 |
// |
| 768 |
// https://tools.ietf.org/html/rfc7627 |
| 769 |
|
| 770 |
static bool ext_ems_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 771 |
CBB *out_compressible, |
| 772 |
ssl_client_hello_type_t type) { |
| 773 |
// Extended master secret is not necessary in TLS 1.3. |
| 774 |
if (hs->min_version >= TLS1_3_VERSION || type == ssl_client_hello_inner) { |
| 775 |
return true; |
| 776 |
} |
| 777 |
|
| 778 |
if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) || |
| 779 |
!CBB_add_u16(out, 0 /* length */)) { |
| 780 |
return false; |
| 781 |
} |
| 782 |
|
| 783 |
return true; |
| 784 |
} |
| 785 |
|
| 786 |
static bool ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 787 |
CBS *contents) { |
| 788 |
SSL *const ssl = hs->ssl; |
| 789 |
|
| 790 |
if (contents != NULL) { |
| 791 |
if (ssl_protocol_version(ssl) >= TLS1_3_VERSION || |
| 792 |
CBS_len(contents) != 0) { |
| 793 |
return false; |
| 794 |
} |
| 795 |
|
| 796 |
hs->extended_master_secret = true; |
| 797 |
} |
| 798 |
|
| 799 |
// Whether EMS is negotiated may not change on renegotiation. |
| 800 |
if (ssl->s3->established_session != nullptr && |
| 801 |
hs->extended_master_secret != |
| 802 |
!!ssl->s3->established_session->extended_master_secret) { |
| 803 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH); |
| 804 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 805 |
return false; |
| 806 |
} |
| 807 |
|
| 808 |
return true; |
| 809 |
} |
| 810 |
|
| 811 |
static bool ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 812 |
CBS *contents) { |
| 813 |
if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
| 814 |
return true; |
| 815 |
} |
| 816 |
|
| 817 |
if (contents == NULL) { |
| 818 |
return true; |
| 819 |
} |
| 820 |
|
| 821 |
if (CBS_len(contents) != 0) { |
| 822 |
return false; |
| 823 |
} |
| 824 |
|
| 825 |
hs->extended_master_secret = true; |
| 826 |
return true; |
| 827 |
} |
| 828 |
|
| 829 |
static bool ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 830 |
if (!hs->extended_master_secret) { |
| 831 |
return true; |
| 832 |
} |
| 833 |
|
| 834 |
if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) || |
| 835 |
!CBB_add_u16(out, 0 /* length */)) { |
| 836 |
return false; |
| 837 |
} |
| 838 |
|
| 839 |
return true; |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
// Session tickets. |
| 844 |
// |
| 845 |
// https://tools.ietf.org/html/rfc5077 |
| 846 |
|
| 847 |
static bool ext_ticket_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 848 |
CBB *out_compressible, |
| 849 |
ssl_client_hello_type_t type) { |
| 850 |
const SSL *const ssl = hs->ssl; |
| 851 |
// TLS 1.3 uses a different ticket extension. |
| 852 |
if (hs->min_version >= TLS1_3_VERSION || type == ssl_client_hello_inner || |
| 853 |
SSL_get_options(ssl) & SSL_OP_NO_TICKET) { |
| 854 |
return true; |
| 855 |
} |
| 856 |
|
| 857 |
Span<const uint8_t> ticket; |
| 858 |
|
| 859 |
// Renegotiation does not participate in session resumption. However, still |
| 860 |
// advertise the extension to avoid potentially breaking servers which carry |
| 861 |
// over the state from the previous handshake, such as OpenSSL servers |
| 862 |
// without upstream's 3c3f0259238594d77264a78944d409f2127642c4. |
| 863 |
if (!ssl->s3->initial_handshake_complete && |
| 864 |
ssl->session != nullptr && |
| 865 |
!ssl->session->ticket.empty() && |
| 866 |
// Don't send TLS 1.3 session tickets in the ticket extension. |
| 867 |
ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) { |
| 868 |
ticket = ssl->session->ticket; |
| 869 |
} |
| 870 |
|
| 871 |
CBB ticket_cbb; |
| 872 |
if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
| 873 |
!CBB_add_u16_length_prefixed(out, &ticket_cbb) || |
| 874 |
!CBB_add_bytes(&ticket_cbb, ticket.data(), ticket.size()) || |
| 875 |
!CBB_flush(out)) { |
| 876 |
return false; |
| 877 |
} |
| 878 |
|
| 879 |
return true; |
| 880 |
} |
| 881 |
|
| 882 |
static bool ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 883 |
CBS *contents) { |
| 884 |
SSL *const ssl = hs->ssl; |
| 885 |
if (contents == NULL) { |
| 886 |
return true; |
| 887 |
} |
| 888 |
|
| 889 |
if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 890 |
return false; |
| 891 |
} |
| 892 |
|
| 893 |
// If |SSL_OP_NO_TICKET| is set then no extension will have been sent and |
| 894 |
// this function should never be called, even if the server tries to send the |
| 895 |
// extension. |
| 896 |
assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0); |
| 897 |
|
| 898 |
if (CBS_len(contents) != 0) { |
| 899 |
return false; |
| 900 |
} |
| 901 |
|
| 902 |
hs->ticket_expected = true; |
| 903 |
return true; |
| 904 |
} |
| 905 |
|
| 906 |
static bool ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 907 |
if (!hs->ticket_expected) { |
| 908 |
return true; |
| 909 |
} |
| 910 |
|
| 911 |
// If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. |
| 912 |
assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0); |
| 913 |
|
| 914 |
if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
| 915 |
!CBB_add_u16(out, 0 /* length */)) { |
| 916 |
return false; |
| 917 |
} |
| 918 |
|
| 919 |
return true; |
| 920 |
} |
| 921 |
|
| 922 |
|
| 923 |
// Signature Algorithms. |
| 924 |
// |
| 925 |
// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 |
| 926 |
|
| 927 |
static bool ext_sigalgs_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 928 |
CBB *out_compressible, |
| 929 |
ssl_client_hello_type_t type) { |
| 930 |
if (hs->max_version < TLS1_2_VERSION) { |
| 931 |
return true; |
| 932 |
} |
| 933 |
|
| 934 |
CBB contents, sigalgs_cbb; |
| 935 |
if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_signature_algorithms) || |
| 936 |
!CBB_add_u16_length_prefixed(out_compressible, &contents) || |
| 937 |
!CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) || |
| 938 |
!tls12_add_verify_sigalgs(hs, &sigalgs_cbb) || |
| 939 |
!CBB_flush(out_compressible)) { |
| 940 |
return false; |
| 941 |
} |
| 942 |
|
| 943 |
return true; |
| 944 |
} |
| 945 |
|
| 946 |
static bool ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 947 |
CBS *contents) { |
| 948 |
hs->peer_sigalgs.Reset(); |
| 949 |
if (contents == NULL) { |
| 950 |
return true; |
| 951 |
} |
| 952 |
|
| 953 |
CBS supported_signature_algorithms; |
| 954 |
if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) || |
| 955 |
CBS_len(contents) != 0 || |
| 956 |
!tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) { |
| 957 |
return false; |
| 958 |
} |
| 959 |
|
| 960 |
return true; |
| 961 |
} |
| 962 |
|
| 963 |
|
| 964 |
// OCSP Stapling. |
| 965 |
// |
| 966 |
// https://tools.ietf.org/html/rfc6066#section-8 |
| 967 |
|
| 968 |
static bool ext_ocsp_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 969 |
CBB *out_compressible, |
| 970 |
ssl_client_hello_type_t type) { |
| 971 |
if (!hs->config->ocsp_stapling_enabled) { |
| 972 |
return true; |
| 973 |
} |
| 974 |
|
| 975 |
CBB contents; |
| 976 |
if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_status_request) || |
| 977 |
!CBB_add_u16_length_prefixed(out_compressible, &contents) || |
| 978 |
!CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) || |
| 979 |
!CBB_add_u16(&contents, 0 /* empty responder ID list */) || |
| 980 |
!CBB_add_u16(&contents, 0 /* empty request extensions */) || |
| 981 |
!CBB_flush(out_compressible)) { |
| 982 |
return false; |
| 983 |
} |
| 984 |
|
| 985 |
return true; |
| 986 |
} |
| 987 |
|
| 988 |
static bool ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 989 |
CBS *contents) { |
| 990 |
SSL *const ssl = hs->ssl; |
| 991 |
if (contents == NULL) { |
| 992 |
return true; |
| 993 |
} |
| 994 |
|
| 995 |
// TLS 1.3 OCSP responses are included in the Certificate extensions. |
| 996 |
if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 997 |
return false; |
| 998 |
} |
| 999 |
|
| 1000 |
// OCSP stapling is forbidden on non-certificate ciphers. |
| 1001 |
if (CBS_len(contents) != 0 || |
| 1002 |
!ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
| 1003 |
return false; |
| 1004 |
} |
| 1005 |
|
| 1006 |
// Note this does not check for resumption in TLS 1.2. Sending |
| 1007 |
// status_request here does not make sense, but OpenSSL does so and the |
| 1008 |
// specification does not say anything. Tolerate it but ignore it. |
| 1009 |
|
| 1010 |
hs->certificate_status_expected = true; |
| 1011 |
return true; |
| 1012 |
} |
| 1013 |
|
| 1014 |
static bool ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1015 |
CBS *contents) { |
| 1016 |
if (contents == NULL) { |
| 1017 |
return true; |
| 1018 |
} |
| 1019 |
|
| 1020 |
uint8_t status_type; |
| 1021 |
if (!CBS_get_u8(contents, &status_type)) { |
| 1022 |
return false; |
| 1023 |
} |
| 1024 |
|
| 1025 |
// We cannot decide whether OCSP stapling will occur yet because the correct |
| 1026 |
// SSL_CTX might not have been selected. |
| 1027 |
hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp; |
| 1028 |
|
| 1029 |
return true; |
| 1030 |
} |
| 1031 |
|
| 1032 |
static bool ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1033 |
SSL *const ssl = hs->ssl; |
| 1034 |
if (ssl_protocol_version(ssl) >= TLS1_3_VERSION || |
| 1035 |
!hs->ocsp_stapling_requested || hs->config->cert->ocsp_response == NULL || |
| 1036 |
ssl->s3->session_reused || |
| 1037 |
!ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
| 1038 |
return true; |
| 1039 |
} |
| 1040 |
|
| 1041 |
hs->certificate_status_expected = true; |
| 1042 |
|
| 1043 |
return CBB_add_u16(out, TLSEXT_TYPE_status_request) && |
| 1044 |
CBB_add_u16(out, 0 /* length */); |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
// Next protocol negotiation. |
| 1049 |
// |
| 1050 |
// https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html |
| 1051 |
|
| 1052 |
static bool ext_npn_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 1053 |
CBB *out_compressible, |
| 1054 |
ssl_client_hello_type_t type) { |
| 1055 |
const SSL *const ssl = hs->ssl; |
| 1056 |
if (ssl->ctx->next_proto_select_cb == NULL || |
| 1057 |
// Do not allow NPN to change on renegotiation. |
| 1058 |
ssl->s3->initial_handshake_complete || |
| 1059 |
// NPN is not defined in DTLS or TLS 1.3. |
| 1060 |
SSL_is_dtls(ssl) || hs->min_version >= TLS1_3_VERSION || |
| 1061 |
type == ssl_client_hello_inner) { |
| 1062 |
return true; |
| 1063 |
} |
| 1064 |
|
| 1065 |
if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1066 |
!CBB_add_u16(out, 0 /* length */)) { |
| 1067 |
return false; |
| 1068 |
} |
| 1069 |
|
| 1070 |
return true; |
| 1071 |
} |
| 1072 |
|
| 1073 |
static bool ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1074 |
CBS *contents) { |
| 1075 |
SSL *const ssl = hs->ssl; |
| 1076 |
if (contents == NULL) { |
| 1077 |
return true; |
| 1078 |
} |
| 1079 |
|
| 1080 |
if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1081 |
return false; |
| 1082 |
} |
| 1083 |
|
| 1084 |
// If any of these are false then we should never have sent the NPN |
| 1085 |
// extension in the ClientHello and thus this function should never have been |
| 1086 |
// called. |
| 1087 |
assert(!ssl->s3->initial_handshake_complete); |
| 1088 |
assert(!SSL_is_dtls(ssl)); |
| 1089 |
assert(ssl->ctx->next_proto_select_cb != NULL); |
| 1090 |
|
| 1091 |
if (!ssl->s3->alpn_selected.empty()) { |
| 1092 |
// NPN and ALPN may not be negotiated in the same connection. |
| 1093 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1094 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
| 1095 |
return false; |
| 1096 |
} |
| 1097 |
|
| 1098 |
const uint8_t *const orig_contents = CBS_data(contents); |
| 1099 |
const size_t orig_len = CBS_len(contents); |
| 1100 |
|
| 1101 |
while (CBS_len(contents) != 0) { |
| 1102 |
CBS proto; |
| 1103 |
if (!CBS_get_u8_length_prefixed(contents, &proto) || |
| 1104 |
CBS_len(&proto) == 0) { |
| 1105 |
return false; |
| 1106 |
} |
| 1107 |
} |
| 1108 |
|
| 1109 |
// |orig_len| fits in |unsigned| because TLS extensions use 16-bit lengths. |
| 1110 |
uint8_t *selected; |
| 1111 |
uint8_t selected_len; |
| 1112 |
if (ssl->ctx->next_proto_select_cb( |
| 1113 |
ssl, &selected, &selected_len, orig_contents, |
| 1114 |
static_cast<unsigned>(orig_len), |
| 1115 |
ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK || |
| 1116 |
!ssl->s3->next_proto_negotiated.CopyFrom( |
| 1117 |
MakeConstSpan(selected, selected_len))) { |
| 1118 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 1119 |
return false; |
| 1120 |
} |
| 1121 |
|
| 1122 |
hs->next_proto_neg_seen = true; |
| 1123 |
return true; |
| 1124 |
} |
| 1125 |
|
| 1126 |
static bool ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1127 |
CBS *contents) { |
| 1128 |
SSL *const ssl = hs->ssl; |
| 1129 |
if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1130 |
return true; |
| 1131 |
} |
| 1132 |
|
| 1133 |
if (contents != NULL && CBS_len(contents) != 0) { |
| 1134 |
return false; |
| 1135 |
} |
| 1136 |
|
| 1137 |
if (contents == NULL || |
| 1138 |
ssl->s3->initial_handshake_complete || |
| 1139 |
ssl->ctx->next_protos_advertised_cb == NULL || |
| 1140 |
SSL_is_dtls(ssl)) { |
| 1141 |
return true; |
| 1142 |
} |
| 1143 |
|
| 1144 |
hs->next_proto_neg_seen = true; |
| 1145 |
return true; |
| 1146 |
} |
| 1147 |
|
| 1148 |
static bool ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1149 |
SSL *const ssl = hs->ssl; |
| 1150 |
// |next_proto_neg_seen| might have been cleared when an ALPN extension was |
| 1151 |
// parsed. |
| 1152 |
if (!hs->next_proto_neg_seen) { |
| 1153 |
return true; |
| 1154 |
} |
| 1155 |
|
| 1156 |
const uint8_t *npa; |
| 1157 |
unsigned npa_len; |
| 1158 |
|
| 1159 |
if (ssl->ctx->next_protos_advertised_cb( |
| 1160 |
ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) != |
| 1161 |
SSL_TLSEXT_ERR_OK) { |
| 1162 |
hs->next_proto_neg_seen = false; |
| 1163 |
return true; |
| 1164 |
} |
| 1165 |
|
| 1166 |
CBB contents; |
| 1167 |
if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1168 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 1169 |
!CBB_add_bytes(&contents, npa, npa_len) || |
| 1170 |
!CBB_flush(out)) { |
| 1171 |
return false; |
| 1172 |
} |
| 1173 |
|
| 1174 |
return true; |
| 1175 |
} |
| 1176 |
|
| 1177 |
|
| 1178 |
// Signed certificate timestamps. |
| 1179 |
// |
| 1180 |
// https://tools.ietf.org/html/rfc6962#section-3.3.1 |
| 1181 |
|
| 1182 |
static bool ext_sct_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 1183 |
CBB *out_compressible, |
| 1184 |
ssl_client_hello_type_t type) { |
| 1185 |
if (!hs->config->signed_cert_timestamps_enabled) { |
| 1186 |
return true; |
| 1187 |
} |
| 1188 |
|
| 1189 |
if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_certificate_timestamp) || |
| 1190 |
!CBB_add_u16(out_compressible, 0 /* length */)) { |
| 1191 |
return false; |
| 1192 |
} |
| 1193 |
|
| 1194 |
return true; |
| 1195 |
} |
| 1196 |
|
| 1197 |
static bool ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1198 |
CBS *contents) { |
| 1199 |
SSL *const ssl = hs->ssl; |
| 1200 |
if (contents == NULL) { |
| 1201 |
return true; |
| 1202 |
} |
| 1203 |
|
| 1204 |
// TLS 1.3 SCTs are included in the Certificate extensions. |
| 1205 |
if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1206 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 1207 |
return false; |
| 1208 |
} |
| 1209 |
|
| 1210 |
// If this is false then we should never have sent the SCT extension in the |
| 1211 |
// ClientHello and thus this function should never have been called. |
| 1212 |
assert(hs->config->signed_cert_timestamps_enabled); |
| 1213 |
|
| 1214 |
if (!ssl_is_sct_list_valid(contents)) { |
| 1215 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 1216 |
return false; |
| 1217 |
} |
| 1218 |
|
| 1219 |
// Session resumption uses the original session information. The extension |
| 1220 |
// should not be sent on resumption, but RFC 6962 did not make it a |
| 1221 |
// requirement, so tolerate this. |
| 1222 |
// |
| 1223 |
// TODO(davidben): Enforce this anyway. |
| 1224 |
if (!ssl->s3->session_reused) { |
| 1225 |
hs->new_session->signed_cert_timestamp_list.reset( |
| 1226 |
CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool)); |
| 1227 |
if (hs->new_session->signed_cert_timestamp_list == nullptr) { |
| 1228 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 1229 |
return false; |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
return true; |
| 1234 |
} |
| 1235 |
|
| 1236 |
static bool ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1237 |
CBS *contents) { |
| 1238 |
if (contents == NULL) { |
| 1239 |
return true; |
| 1240 |
} |
| 1241 |
|
| 1242 |
if (CBS_len(contents) != 0) { |
| 1243 |
return false; |
| 1244 |
} |
| 1245 |
|
| 1246 |
hs->scts_requested = true; |
| 1247 |
return true; |
| 1248 |
} |
| 1249 |
|
| 1250 |
static bool ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1251 |
SSL *const ssl = hs->ssl; |
| 1252 |
// The extension shouldn't be sent when resuming sessions. |
| 1253 |
if (ssl_protocol_version(ssl) >= TLS1_3_VERSION || ssl->s3->session_reused || |
| 1254 |
hs->config->cert->signed_cert_timestamp_list == NULL) { |
| 1255 |
return true; |
| 1256 |
} |
| 1257 |
|
| 1258 |
CBB contents; |
| 1259 |
return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) && |
| 1260 |
CBB_add_u16_length_prefixed(out, &contents) && |
| 1261 |
CBB_add_bytes( |
| 1262 |
&contents, |
| 1263 |
CRYPTO_BUFFER_data( |
| 1264 |
hs->config->cert->signed_cert_timestamp_list.get()), |
| 1265 |
CRYPTO_BUFFER_len( |
| 1266 |
hs->config->cert->signed_cert_timestamp_list.get())) && |
| 1267 |
CBB_flush(out); |
| 1268 |
} |
| 1269 |
|
| 1270 |
|
| 1271 |
// Application-level Protocol Negotiation. |
| 1272 |
// |
| 1273 |
// https://tools.ietf.org/html/rfc7301 |
| 1274 |
|
| 1275 |
static bool ext_alpn_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 1276 |
CBB *out_compressible, |
| 1277 |
ssl_client_hello_type_t type) { |
| 1278 |
const SSL *const ssl = hs->ssl; |
| 1279 |
if (hs->config->alpn_client_proto_list.empty() && ssl->quic_method) { |
| 1280 |
// ALPN MUST be used with QUIC. |
| 1281 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_APPLICATION_PROTOCOL); |
| 1282 |
return false; |
| 1283 |
} |
| 1284 |
|
| 1285 |
if (hs->config->alpn_client_proto_list.empty() || |
| 1286 |
ssl->s3->initial_handshake_complete) { |
| 1287 |
return true; |
| 1288 |
} |
| 1289 |
|
| 1290 |
CBB contents, proto_list; |
| 1291 |
if (!CBB_add_u16(out_compressible, |
| 1292 |
TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1293 |
!CBB_add_u16_length_prefixed(out_compressible, &contents) || |
| 1294 |
!CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1295 |
!CBB_add_bytes(&proto_list, hs->config->alpn_client_proto_list.data(), |
| 1296 |
hs->config->alpn_client_proto_list.size()) || |
| 1297 |
!CBB_flush(out_compressible)) { |
| 1298 |
return false; |
| 1299 |
} |
| 1300 |
|
| 1301 |
return true; |
| 1302 |
} |
| 1303 |
|
| 1304 |
static bool ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1305 |
CBS *contents) { |
| 1306 |
SSL *const ssl = hs->ssl; |
| 1307 |
if (contents == NULL) { |
| 1308 |
if (ssl->quic_method) { |
| 1309 |
// ALPN is required when QUIC is used. |
| 1310 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_APPLICATION_PROTOCOL); |
| 1311 |
*out_alert = SSL_AD_NO_APPLICATION_PROTOCOL; |
| 1312 |
return false; |
| 1313 |
} |
| 1314 |
return true; |
| 1315 |
} |
| 1316 |
|
| 1317 |
assert(!ssl->s3->initial_handshake_complete); |
| 1318 |
assert(!hs->config->alpn_client_proto_list.empty()); |
| 1319 |
|
| 1320 |
if (hs->next_proto_neg_seen) { |
| 1321 |
// NPN and ALPN may not be negotiated in the same connection. |
| 1322 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1323 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
| 1324 |
return false; |
| 1325 |
} |
| 1326 |
|
| 1327 |
// The extension data consists of a ProtocolNameList which must have |
| 1328 |
// exactly one ProtocolName. Each of these is length-prefixed. |
| 1329 |
CBS protocol_name_list, protocol_name; |
| 1330 |
if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) || |
| 1331 |
CBS_len(contents) != 0 || |
| 1332 |
!CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) || |
| 1333 |
// Empty protocol names are forbidden. |
| 1334 |
CBS_len(&protocol_name) == 0 || |
| 1335 |
CBS_len(&protocol_name_list) != 0) { |
| 1336 |
return false; |
| 1337 |
} |
| 1338 |
|
| 1339 |
if (!ssl_is_alpn_protocol_allowed(hs, protocol_name)) { |
| 1340 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL); |
| 1341 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1342 |
return false; |
| 1343 |
} |
| 1344 |
|
| 1345 |
if (!ssl->s3->alpn_selected.CopyFrom(protocol_name)) { |
| 1346 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 1347 |
return false; |
| 1348 |
} |
| 1349 |
|
| 1350 |
return true; |
| 1351 |
} |
| 1352 |
|
| 1353 |
bool ssl_is_valid_alpn_list(Span<const uint8_t> in) { |
| 1354 |
CBS protocol_name_list = in; |
| 1355 |
if (CBS_len(&protocol_name_list) == 0) { |
| 1356 |
return false; |
| 1357 |
} |
| 1358 |
while (CBS_len(&protocol_name_list) > 0) { |
| 1359 |
CBS protocol_name; |
| 1360 |
if (!CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) || |
| 1361 |
// Empty protocol names are forbidden. |
| 1362 |
CBS_len(&protocol_name) == 0) { |
| 1363 |
return false; |
| 1364 |
} |
| 1365 |
} |
| 1366 |
return true; |
| 1367 |
} |
| 1368 |
|
| 1369 |
bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs, |
| 1370 |
Span<const uint8_t> protocol) { |
| 1371 |
if (hs->config->alpn_client_proto_list.empty()) { |
| 1372 |
return false; |
| 1373 |
} |
| 1374 |
|
| 1375 |
if (hs->ssl->ctx->allow_unknown_alpn_protos) { |
| 1376 |
return true; |
| 1377 |
} |
| 1378 |
|
| 1379 |
// Check that the protocol name is one of the ones we advertised. |
| 1380 |
return ssl_alpn_list_contains_protocol(hs->config->alpn_client_proto_list, |
| 1381 |
protocol); |
| 1382 |
} |
| 1383 |
|
| 1384 |
bool ssl_alpn_list_contains_protocol(Span<const uint8_t> list, |
| 1385 |
Span<const uint8_t> protocol) { |
| 1386 |
CBS cbs = list, candidate; |
| 1387 |
while (CBS_len(&cbs) > 0) { |
| 1388 |
if (!CBS_get_u8_length_prefixed(&cbs, &candidate)) { |
| 1389 |
return false; |
| 1390 |
} |
| 1391 |
|
| 1392 |
if (candidate == protocol) { |
| 1393 |
return true; |
| 1394 |
} |
| 1395 |
} |
| 1396 |
|
| 1397 |
return false; |
| 1398 |
} |
| 1399 |
|
| 1400 |
bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1401 |
const SSL_CLIENT_HELLO *client_hello) { |
| 1402 |
SSL *const ssl = hs->ssl; |
| 1403 |
CBS contents; |
| 1404 |
if (ssl->ctx->alpn_select_cb == NULL || |
| 1405 |
!ssl_client_hello_get_extension( |
| 1406 |
client_hello, &contents, |
| 1407 |
TLSEXT_TYPE_application_layer_protocol_negotiation)) { |
| 1408 |
if (ssl->quic_method) { |
| 1409 |
// ALPN is required when QUIC is used. |
| 1410 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_APPLICATION_PROTOCOL); |
| 1411 |
*out_alert = SSL_AD_NO_APPLICATION_PROTOCOL; |
| 1412 |
return false; |
| 1413 |
} |
| 1414 |
// Ignore ALPN if not configured or no extension was supplied. |
| 1415 |
return true; |
| 1416 |
} |
| 1417 |
|
| 1418 |
// ALPN takes precedence over NPN. |
| 1419 |
hs->next_proto_neg_seen = false; |
| 1420 |
|
| 1421 |
CBS protocol_name_list; |
| 1422 |
if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) || |
| 1423 |
CBS_len(&contents) != 0 || |
| 1424 |
!ssl_is_valid_alpn_list(protocol_name_list)) { |
| 1425 |
OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 1426 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 1427 |
return false; |
| 1428 |
} |
| 1429 |
|
| 1430 |
// |protocol_name_list| fits in |unsigned| because TLS extensions use 16-bit |
| 1431 |
// lengths. |
| 1432 |
const uint8_t *selected; |
| 1433 |
uint8_t selected_len; |
| 1434 |
int ret = ssl->ctx->alpn_select_cb( |
| 1435 |
ssl, &selected, &selected_len, CBS_data(&protocol_name_list), |
| 1436 |
static_cast<unsigned>(CBS_len(&protocol_name_list)), |
| 1437 |
ssl->ctx->alpn_select_cb_arg); |
| 1438 |
// ALPN is required when QUIC is used. |
| 1439 |
if (ssl->quic_method && |
| 1440 |
(ret == SSL_TLSEXT_ERR_NOACK || ret == SSL_TLSEXT_ERR_ALERT_WARNING)) { |
| 1441 |
ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 1442 |
} |
| 1443 |
switch (ret) { |
| 1444 |
case SSL_TLSEXT_ERR_OK: |
| 1445 |
if (selected_len == 0) { |
| 1446 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL); |
| 1447 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 1448 |
return false; |
| 1449 |
} |
| 1450 |
if (!ssl->s3->alpn_selected.CopyFrom( |
| 1451 |
MakeConstSpan(selected, selected_len))) { |
| 1452 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 1453 |
return false; |
| 1454 |
} |
| 1455 |
break; |
| 1456 |
case SSL_TLSEXT_ERR_NOACK: |
| 1457 |
case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 1458 |
break; |
| 1459 |
case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 1460 |
*out_alert = SSL_AD_NO_APPLICATION_PROTOCOL; |
| 1461 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_APPLICATION_PROTOCOL); |
| 1462 |
return false; |
| 1463 |
default: |
| 1464 |
// Invalid return value. |
| 1465 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 1466 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1467 |
return false; |
| 1468 |
} |
| 1469 |
|
| 1470 |
return true; |
| 1471 |
} |
| 1472 |
|
| 1473 |
static bool ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1474 |
SSL *const ssl = hs->ssl; |
| 1475 |
if (ssl->s3->alpn_selected.empty()) { |
| 1476 |
return true; |
| 1477 |
} |
| 1478 |
|
| 1479 |
CBB contents, proto_list, proto; |
| 1480 |
if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1481 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 1482 |
!CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1483 |
!CBB_add_u8_length_prefixed(&proto_list, &proto) || |
| 1484 |
!CBB_add_bytes(&proto, ssl->s3->alpn_selected.data(), |
| 1485 |
ssl->s3->alpn_selected.size()) || |
| 1486 |
!CBB_flush(out)) { |
| 1487 |
return false; |
| 1488 |
} |
| 1489 |
|
| 1490 |
return true; |
| 1491 |
} |
| 1492 |
|
| 1493 |
|
| 1494 |
// Channel ID. |
| 1495 |
// |
| 1496 |
// https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 |
| 1497 |
|
| 1498 |
static bool ext_channel_id_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 1499 |
CBB *out_compressible, |
| 1500 |
ssl_client_hello_type_t type) { |
| 1501 |
const SSL *const ssl = hs->ssl; |
| 1502 |
if (!hs->config->channel_id_private || SSL_is_dtls(ssl) || |
| 1503 |
// Don't offer Channel ID in ClientHelloOuter. ClientHelloOuter handshakes |
| 1504 |
// are not authenticated for the name that can learn the Channel ID. |
| 1505 |
// |
| 1506 |
// We could alternatively offer the extension but sign with a random key. |
| 1507 |
// For other extensions, we try to align |ssl_client_hello_outer| and |
| 1508 |
// |ssl_client_hello_unencrypted|, to improve the effectiveness of ECH |
| 1509 |
// GREASE. However, Channel ID is deprecated and unlikely to be used with |
| 1510 |
// ECH, so do the simplest thing. |
| 1511 |
type == ssl_client_hello_outer) { |
| 1512 |
return true; |
| 1513 |
} |
| 1514 |
|
| 1515 |
if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1516 |
!CBB_add_u16(out, 0 /* length */)) { |
| 1517 |
return false; |
| 1518 |
} |
| 1519 |
|
| 1520 |
return true; |
| 1521 |
} |
| 1522 |
|
| 1523 |
static bool ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs, |
| 1524 |
uint8_t *out_alert, |
| 1525 |
CBS *contents) { |
| 1526 |
if (contents == NULL) { |
| 1527 |
return true; |
| 1528 |
} |
| 1529 |
|
| 1530 |
assert(!SSL_is_dtls(hs->ssl)); |
| 1531 |
assert(hs->config->channel_id_private); |
| 1532 |
|
| 1533 |
if (CBS_len(contents) != 0) { |
| 1534 |
return false; |
| 1535 |
} |
| 1536 |
|
| 1537 |
hs->channel_id_negotiated = true; |
| 1538 |
return true; |
| 1539 |
} |
| 1540 |
|
| 1541 |
static bool ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs, |
| 1542 |
uint8_t *out_alert, |
| 1543 |
CBS *contents) { |
| 1544 |
SSL *const ssl = hs->ssl; |
| 1545 |
if (contents == NULL || !hs->config->channel_id_enabled || SSL_is_dtls(ssl)) { |
| 1546 |
return true; |
| 1547 |
} |
| 1548 |
|
| 1549 |
if (CBS_len(contents) != 0) { |
| 1550 |
return false; |
| 1551 |
} |
| 1552 |
|
| 1553 |
hs->channel_id_negotiated = true; |
| 1554 |
return true; |
| 1555 |
} |
| 1556 |
|
| 1557 |
static bool ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1558 |
if (!hs->channel_id_negotiated) { |
| 1559 |
return true; |
| 1560 |
} |
| 1561 |
|
| 1562 |
if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1563 |
!CBB_add_u16(out, 0 /* length */)) { |
| 1564 |
return false; |
| 1565 |
} |
| 1566 |
|
| 1567 |
return true; |
| 1568 |
} |
| 1569 |
|
| 1570 |
|
| 1571 |
// Secure Real-time Transport Protocol (SRTP) extension. |
| 1572 |
// |
| 1573 |
// https://tools.ietf.org/html/rfc5764 |
| 1574 |
|
| 1575 |
static bool ext_srtp_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 1576 |
CBB *out_compressible, |
| 1577 |
ssl_client_hello_type_t type) { |
| 1578 |
const SSL *const ssl = hs->ssl; |
| 1579 |
const STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = |
| 1580 |
SSL_get_srtp_profiles(ssl); |
| 1581 |
if (profiles == NULL || |
| 1582 |
sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0 || |
| 1583 |
!SSL_is_dtls(ssl)) { |
| 1584 |
return true; |
| 1585 |
} |
| 1586 |
|
| 1587 |
CBB contents, profile_ids; |
| 1588 |
if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_srtp) || |
| 1589 |
!CBB_add_u16_length_prefixed(out_compressible, &contents) || |
| 1590 |
!CBB_add_u16_length_prefixed(&contents, &profile_ids)) { |
| 1591 |
return false; |
| 1592 |
} |
| 1593 |
|
| 1594 |
for (const SRTP_PROTECTION_PROFILE *profile : profiles) { |
| 1595 |
if (!CBB_add_u16(&profile_ids, profile->id)) { |
| 1596 |
return false; |
| 1597 |
} |
| 1598 |
} |
| 1599 |
|
| 1600 |
if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) || |
| 1601 |
!CBB_flush(out_compressible)) { |
| 1602 |
return false; |
| 1603 |
} |
| 1604 |
|
| 1605 |
return true; |
| 1606 |
} |
| 1607 |
|
| 1608 |
static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1609 |
CBS *contents) { |
| 1610 |
SSL *const ssl = hs->ssl; |
| 1611 |
if (contents == NULL) { |
| 1612 |
return true; |
| 1613 |
} |
| 1614 |
|
| 1615 |
// The extension consists of a u16-prefixed profile ID list containing a |
| 1616 |
// single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field. |
| 1617 |
// |
| 1618 |
// See https://tools.ietf.org/html/rfc5764#section-4.1.1 |
| 1619 |
assert(SSL_is_dtls(ssl)); |
| 1620 |
CBS profile_ids, srtp_mki; |
| 1621 |
uint16_t profile_id; |
| 1622 |
if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1623 |
!CBS_get_u16(&profile_ids, &profile_id) || |
| 1624 |
CBS_len(&profile_ids) != 0 || |
| 1625 |
!CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1626 |
CBS_len(contents) != 0) { |
| 1627 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1628 |
return false; |
| 1629 |
} |
| 1630 |
|
| 1631 |
if (CBS_len(&srtp_mki) != 0) { |
| 1632 |
// Must be no MKI, since we never offer one. |
| 1633 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE); |
| 1634 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1635 |
return false; |
| 1636 |
} |
| 1637 |
|
| 1638 |
// Check to see if the server gave us something we support and offered. |
| 1639 |
for (const SRTP_PROTECTION_PROFILE *profile : SSL_get_srtp_profiles(ssl)) { |
| 1640 |
if (profile->id == profile_id) { |
| 1641 |
ssl->s3->srtp_profile = profile; |
| 1642 |
return true; |
| 1643 |
} |
| 1644 |
} |
| 1645 |
|
| 1646 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1647 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1648 |
return false; |
| 1649 |
} |
| 1650 |
|
| 1651 |
static bool ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1652 |
CBS *contents) { |
| 1653 |
SSL *const ssl = hs->ssl; |
| 1654 |
// DTLS-SRTP is only defined for DTLS. |
| 1655 |
if (contents == NULL || !SSL_is_dtls(ssl)) { |
| 1656 |
return true; |
| 1657 |
} |
| 1658 |
|
| 1659 |
CBS profile_ids, srtp_mki; |
| 1660 |
if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1661 |
CBS_len(&profile_ids) < 2 || |
| 1662 |
!CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1663 |
CBS_len(contents) != 0) { |
| 1664 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1665 |
return false; |
| 1666 |
} |
| 1667 |
// Discard the MKI value for now. |
| 1668 |
|
| 1669 |
const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles = |
| 1670 |
SSL_get_srtp_profiles(ssl); |
| 1671 |
|
| 1672 |
// Pick the server's most preferred profile. |
| 1673 |
for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) { |
| 1674 |
CBS profile_ids_tmp; |
| 1675 |
CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids)); |
| 1676 |
|
| 1677 |
while (CBS_len(&profile_ids_tmp) > 0) { |
| 1678 |
uint16_t profile_id; |
| 1679 |
if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) { |
| 1680 |
return false; |
| 1681 |
} |
| 1682 |
|
| 1683 |
if (server_profile->id == profile_id) { |
| 1684 |
ssl->s3->srtp_profile = server_profile; |
| 1685 |
return true; |
| 1686 |
} |
| 1687 |
} |
| 1688 |
} |
| 1689 |
|
| 1690 |
return true; |
| 1691 |
} |
| 1692 |
|
| 1693 |
static bool ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1694 |
SSL *const ssl = hs->ssl; |
| 1695 |
if (ssl->s3->srtp_profile == NULL) { |
| 1696 |
return true; |
| 1697 |
} |
| 1698 |
|
| 1699 |
assert(SSL_is_dtls(ssl)); |
| 1700 |
CBB contents, profile_ids; |
| 1701 |
if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1702 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 1703 |
!CBB_add_u16_length_prefixed(&contents, &profile_ids) || |
| 1704 |
!CBB_add_u16(&profile_ids, ssl->s3->srtp_profile->id) || |
| 1705 |
!CBB_add_u8(&contents, 0 /* empty MKI */) || |
| 1706 |
!CBB_flush(out)) { |
| 1707 |
return false; |
| 1708 |
} |
| 1709 |
|
| 1710 |
return true; |
| 1711 |
} |
| 1712 |
|
| 1713 |
|
| 1714 |
// EC point formats. |
| 1715 |
// |
| 1716 |
// https://tools.ietf.org/html/rfc4492#section-5.1.2 |
| 1717 |
|
| 1718 |
static bool ext_ec_point_add_extension(const SSL_HANDSHAKE *hs, CBB *out) { |
| 1719 |
CBB contents, formats; |
| 1720 |
if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) || |
| 1721 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 1722 |
!CBB_add_u8_length_prefixed(&contents, &formats) || |
| 1723 |
!CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) || |
| 1724 |
!CBB_flush(out)) { |
| 1725 |
return false; |
| 1726 |
} |
| 1727 |
|
| 1728 |
return true; |
| 1729 |
} |
| 1730 |
|
| 1731 |
static bool ext_ec_point_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 1732 |
CBB *out_compressible, |
| 1733 |
ssl_client_hello_type_t type) { |
| 1734 |
// The point format extension is unnecessary in TLS 1.3. |
| 1735 |
if (hs->min_version >= TLS1_3_VERSION || type == ssl_client_hello_inner) { |
| 1736 |
return true; |
| 1737 |
} |
| 1738 |
|
| 1739 |
return ext_ec_point_add_extension(hs, out); |
| 1740 |
} |
| 1741 |
|
| 1742 |
static bool ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1743 |
CBS *contents) { |
| 1744 |
if (contents == NULL) { |
| 1745 |
return true; |
| 1746 |
} |
| 1747 |
|
| 1748 |
if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
| 1749 |
return false; |
| 1750 |
} |
| 1751 |
|
| 1752 |
CBS ec_point_format_list; |
| 1753 |
if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) || |
| 1754 |
CBS_len(contents) != 0) { |
| 1755 |
return false; |
| 1756 |
} |
| 1757 |
|
| 1758 |
// Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed |
| 1759 |
// point format. |
| 1760 |
if (OPENSSL_memchr(CBS_data(&ec_point_format_list), |
| 1761 |
TLSEXT_ECPOINTFORMAT_uncompressed, |
| 1762 |
CBS_len(&ec_point_format_list)) == NULL) { |
| 1763 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1764 |
return false; |
| 1765 |
} |
| 1766 |
|
| 1767 |
return true; |
| 1768 |
} |
| 1769 |
|
| 1770 |
static bool ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1771 |
CBS *contents) { |
| 1772 |
if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
| 1773 |
return true; |
| 1774 |
} |
| 1775 |
|
| 1776 |
return ext_ec_point_parse_serverhello(hs, out_alert, contents); |
| 1777 |
} |
| 1778 |
|
| 1779 |
static bool ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1780 |
SSL *const ssl = hs->ssl; |
| 1781 |
if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1782 |
return true; |
| 1783 |
} |
| 1784 |
|
| 1785 |
const uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 1786 |
const uint32_t alg_a = hs->new_cipher->algorithm_auth; |
| 1787 |
const bool using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA); |
| 1788 |
|
| 1789 |
if (!using_ecc) { |
| 1790 |
return true; |
| 1791 |
} |
| 1792 |
|
| 1793 |
return ext_ec_point_add_extension(hs, out); |
| 1794 |
} |
| 1795 |
|
| 1796 |
|
| 1797 |
// Pre Shared Key |
| 1798 |
// |
| 1799 |
// https://tools.ietf.org/html/rfc8446#section-4.2.11 |
| 1800 |
|
| 1801 |
static bool should_offer_psk(const SSL_HANDSHAKE *hs, |
| 1802 |
ssl_client_hello_type_t type) { |
| 1803 |
const SSL *const ssl = hs->ssl; |
| 1804 |
if (hs->max_version < TLS1_3_VERSION || ssl->session == nullptr || |
| 1805 |
ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION || |
| 1806 |
// TODO(https://crbug.com/boringssl/275): Should we synthesize a |
| 1807 |
// placeholder PSK, at least when we offer early data? Otherwise |
| 1808 |
// ClientHelloOuter will contain an early_data extension without a |
| 1809 |
// pre_shared_key extension and potentially break the recovery flow. |
| 1810 |
type == ssl_client_hello_outer) { |
| 1811 |
return false; |
| 1812 |
} |
| 1813 |
|
| 1814 |
// Per RFC 8446 section 4.1.4, skip offering the session if the selected |
| 1815 |
// cipher in HelloRetryRequest does not match. This avoids performing the |
| 1816 |
// transcript hash transformation for multiple hashes. |
| 1817 |
if (ssl->s3->used_hello_retry_request && |
| 1818 |
ssl->session->cipher->algorithm_prf != hs->new_cipher->algorithm_prf) { |
| 1819 |
return false; |
| 1820 |
} |
| 1821 |
|
| 1822 |
return true; |
| 1823 |
} |
| 1824 |
|
| 1825 |
static size_t ext_pre_shared_key_clienthello_length( |
| 1826 |
const SSL_HANDSHAKE *hs, ssl_client_hello_type_t type) { |
| 1827 |
const SSL *const ssl = hs->ssl; |
| 1828 |
if (!should_offer_psk(hs, type)) { |
| 1829 |
return 0; |
| 1830 |
} |
| 1831 |
|
| 1832 |
size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session.get())); |
| 1833 |
return 15 + ssl->session->ticket.size() + binder_len; |
| 1834 |
} |
| 1835 |
|
| 1836 |
static bool ext_pre_shared_key_add_clienthello(const SSL_HANDSHAKE *hs, |
| 1837 |
CBB *out, bool *out_needs_binder, |
| 1838 |
ssl_client_hello_type_t type) { |
| 1839 |
const SSL *const ssl = hs->ssl; |
| 1840 |
*out_needs_binder = false; |
| 1841 |
if (!should_offer_psk(hs, type)) { |
| 1842 |
return true; |
| 1843 |
} |
| 1844 |
|
| 1845 |
struct OPENSSL_timeval now; |
| 1846 |
ssl_get_current_time(ssl, &now); |
| 1847 |
uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time); |
| 1848 |
uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add; |
| 1849 |
|
| 1850 |
// Fill in a placeholder zero binder of the appropriate length. It will be |
| 1851 |
// computed and filled in later after length prefixes are computed. |
| 1852 |
size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session.get())); |
| 1853 |
|
| 1854 |
CBB contents, identity, ticket, binders, binder; |
| 1855 |
if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) || |
| 1856 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 1857 |
!CBB_add_u16_length_prefixed(&contents, &identity) || |
| 1858 |
!CBB_add_u16_length_prefixed(&identity, &ticket) || |
| 1859 |
!CBB_add_bytes(&ticket, ssl->session->ticket.data(), |
| 1860 |
ssl->session->ticket.size()) || |
| 1861 |
!CBB_add_u32(&identity, obfuscated_ticket_age) || |
| 1862 |
!CBB_add_u16_length_prefixed(&contents, &binders) || |
| 1863 |
!CBB_add_u8_length_prefixed(&binders, &binder) || |
| 1864 |
!CBB_add_zeros(&binder, binder_len)) { |
| 1865 |
return false; |
| 1866 |
} |
| 1867 |
|
| 1868 |
*out_needs_binder = true; |
| 1869 |
return CBB_flush(out); |
| 1870 |
} |
| 1871 |
|
| 1872 |
bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs, |
| 1873 |
uint8_t *out_alert, |
| 1874 |
CBS *contents) { |
| 1875 |
uint16_t psk_id; |
| 1876 |
if (!CBS_get_u16(contents, &psk_id) || |
| 1877 |
CBS_len(contents) != 0) { |
| 1878 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1879 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 1880 |
return false; |
| 1881 |
} |
| 1882 |
|
| 1883 |
// We only advertise one PSK identity, so the only legal index is zero. |
| 1884 |
if (psk_id != 0) { |
| 1885 |
OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 1886 |
*out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY; |
| 1887 |
return false; |
| 1888 |
} |
| 1889 |
|
| 1890 |
return true; |
| 1891 |
} |
| 1892 |
|
| 1893 |
bool ssl_ext_pre_shared_key_parse_clienthello( |
| 1894 |
SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders, |
| 1895 |
uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, |
| 1896 |
const SSL_CLIENT_HELLO *client_hello, CBS *contents) { |
| 1897 |
// Verify that the pre_shared_key extension is the last extension in |
| 1898 |
// ClientHello. |
| 1899 |
if (CBS_data(contents) + CBS_len(contents) != |
| 1900 |
client_hello->extensions + client_hello->extensions_len) { |
| 1901 |
OPENSSL_PUT_ERROR(SSL, SSL_R_PRE_SHARED_KEY_MUST_BE_LAST); |
| 1902 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1903 |
return false; |
| 1904 |
} |
| 1905 |
|
| 1906 |
// We only process the first PSK identity since we don't support pure PSK. |
| 1907 |
CBS identities, binders; |
| 1908 |
if (!CBS_get_u16_length_prefixed(contents, &identities) || |
| 1909 |
!CBS_get_u16_length_prefixed(&identities, out_ticket) || |
| 1910 |
!CBS_get_u32(&identities, out_obfuscated_ticket_age) || |
| 1911 |
!CBS_get_u16_length_prefixed(contents, &binders) || |
| 1912 |
CBS_len(&binders) == 0 || |
| 1913 |
CBS_len(contents) != 0) { |
| 1914 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1915 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 1916 |
return false; |
| 1917 |
} |
| 1918 |
|
| 1919 |
*out_binders = binders; |
| 1920 |
|
| 1921 |
// Check the syntax of the remaining identities, but do not process them. |
| 1922 |
size_t num_identities = 1; |
| 1923 |
while (CBS_len(&identities) != 0) { |
| 1924 |
CBS unused_ticket; |
| 1925 |
uint32_t unused_obfuscated_ticket_age; |
| 1926 |
if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) || |
| 1927 |
!CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) { |
| 1928 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1929 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 1930 |
return false; |
| 1931 |
} |
| 1932 |
|
| 1933 |
num_identities++; |
| 1934 |
} |
| 1935 |
|
| 1936 |
// Check the syntax of the binders. The value will be checked later if |
| 1937 |
// resuming. |
| 1938 |
size_t num_binders = 0; |
| 1939 |
while (CBS_len(&binders) != 0) { |
| 1940 |
CBS binder; |
| 1941 |
if (!CBS_get_u8_length_prefixed(&binders, &binder)) { |
| 1942 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1943 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 1944 |
return false; |
| 1945 |
} |
| 1946 |
|
| 1947 |
num_binders++; |
| 1948 |
} |
| 1949 |
|
| 1950 |
if (num_identities != num_binders) { |
| 1951 |
OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH); |
| 1952 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1953 |
return false; |
| 1954 |
} |
| 1955 |
|
| 1956 |
return true; |
| 1957 |
} |
| 1958 |
|
| 1959 |
bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1960 |
if (!hs->ssl->s3->session_reused) { |
| 1961 |
return true; |
| 1962 |
} |
| 1963 |
|
| 1964 |
CBB contents; |
| 1965 |
if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) || |
| 1966 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 1967 |
// We only consider the first identity for resumption |
| 1968 |
!CBB_add_u16(&contents, 0) || |
| 1969 |
!CBB_flush(out)) { |
| 1970 |
return false; |
| 1971 |
} |
| 1972 |
|
| 1973 |
return true; |
| 1974 |
} |
| 1975 |
|
| 1976 |
|
| 1977 |
// Pre-Shared Key Exchange Modes |
| 1978 |
// |
| 1979 |
// https://tools.ietf.org/html/rfc8446#section-4.2.9 |
| 1980 |
|
| 1981 |
static bool ext_psk_key_exchange_modes_add_clienthello( |
| 1982 |
const SSL_HANDSHAKE *hs, CBB *out, CBB *out_compressible, |
| 1983 |
ssl_client_hello_type_t type) { |
| 1984 |
if (hs->max_version < TLS1_3_VERSION) { |
| 1985 |
return true; |
| 1986 |
} |
| 1987 |
|
| 1988 |
CBB contents, ke_modes; |
| 1989 |
if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_psk_key_exchange_modes) || |
| 1990 |
!CBB_add_u16_length_prefixed(out_compressible, &contents) || |
| 1991 |
!CBB_add_u8_length_prefixed(&contents, &ke_modes) || |
| 1992 |
!CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) { |
| 1993 |
return false; |
| 1994 |
} |
| 1995 |
|
| 1996 |
return CBB_flush(out_compressible); |
| 1997 |
} |
| 1998 |
|
| 1999 |
static bool ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2000 |
uint8_t *out_alert, |
| 2001 |
CBS *contents) { |
| 2002 |
if (contents == NULL) { |
| 2003 |
return true; |
| 2004 |
} |
| 2005 |
|
| 2006 |
CBS ke_modes; |
| 2007 |
if (!CBS_get_u8_length_prefixed(contents, &ke_modes) || |
| 2008 |
CBS_len(&ke_modes) == 0 || |
| 2009 |
CBS_len(contents) != 0) { |
| 2010 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 2011 |
return false; |
| 2012 |
} |
| 2013 |
|
| 2014 |
// We only support tickets with PSK_DHE_KE. |
| 2015 |
hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, |
| 2016 |
CBS_len(&ke_modes)) != NULL; |
| 2017 |
|
| 2018 |
return true; |
| 2019 |
} |
| 2020 |
|
| 2021 |
|
| 2022 |
// Early Data Indication |
| 2023 |
// |
| 2024 |
// https://tools.ietf.org/html/rfc8446#section-4.2.10 |
| 2025 |
|
| 2026 |
static bool ext_early_data_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 2027 |
CBB *out_compressible, |
| 2028 |
ssl_client_hello_type_t type) { |
| 2029 |
const SSL *const ssl = hs->ssl; |
| 2030 |
// The second ClientHello never offers early data, and we must have already |
| 2031 |
// filled in |early_data_reason| by this point. |
| 2032 |
if (ssl->s3->used_hello_retry_request) { |
| 2033 |
assert(ssl->s3->early_data_reason != ssl_early_data_unknown); |
| 2034 |
return true; |
| 2035 |
} |
| 2036 |
|
| 2037 |
if (!hs->early_data_offered) { |
| 2038 |
return true; |
| 2039 |
} |
| 2040 |
|
| 2041 |
// If offering ECH, the extension only applies to ClientHelloInner, but we |
| 2042 |
// send the extension in both ClientHellos. This ensures that, if the server |
| 2043 |
// handshakes with ClientHelloOuter, it can skip past early data. See |
| 2044 |
// draft-ietf-tls-esni-13, section 6.1. |
| 2045 |
if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_early_data) || |
| 2046 |
!CBB_add_u16(out_compressible, 0) || |
| 2047 |
!CBB_flush(out_compressible)) { |
| 2048 |
return false; |
| 2049 |
} |
| 2050 |
|
| 2051 |
return true; |
| 2052 |
} |
| 2053 |
|
| 2054 |
static bool ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2055 |
uint8_t *out_alert, |
| 2056 |
CBS *contents) { |
| 2057 |
SSL *const ssl = hs->ssl; |
| 2058 |
if (contents == NULL) { |
| 2059 |
if (hs->early_data_offered && !ssl->s3->used_hello_retry_request) { |
| 2060 |
ssl->s3->early_data_reason = ssl->s3->session_reused |
| 2061 |
? ssl_early_data_peer_declined |
| 2062 |
: ssl_early_data_session_not_resumed; |
| 2063 |
} else { |
| 2064 |
// We already filled in |early_data_reason| when declining to offer 0-RTT |
| 2065 |
// or handling the implicit HelloRetryRequest reject. |
| 2066 |
assert(ssl->s3->early_data_reason != ssl_early_data_unknown); |
| 2067 |
} |
| 2068 |
return true; |
| 2069 |
} |
| 2070 |
|
| 2071 |
// If we received an HRR, the second ClientHello never offers early data, so |
| 2072 |
// the extensions logic will automatically reject early data extensions as |
| 2073 |
// unsolicited. This covered by the ServerAcceptsEarlyDataOnHRR test. |
| 2074 |
assert(!ssl->s3->used_hello_retry_request); |
| 2075 |
|
| 2076 |
if (CBS_len(contents) != 0) { |
| 2077 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 2078 |
return false; |
| 2079 |
} |
| 2080 |
|
| 2081 |
if (!ssl->s3->session_reused) { |
| 2082 |
*out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2083 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 2084 |
return false; |
| 2085 |
} |
| 2086 |
|
| 2087 |
ssl->s3->early_data_reason = ssl_early_data_accepted; |
| 2088 |
ssl->s3->early_data_accepted = true; |
| 2089 |
return true; |
| 2090 |
} |
| 2091 |
|
| 2092 |
static bool ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2093 |
uint8_t *out_alert, CBS *contents) { |
| 2094 |
SSL *const ssl = hs->ssl; |
| 2095 |
if (contents == NULL || |
| 2096 |
ssl_protocol_version(ssl) < TLS1_3_VERSION) { |
| 2097 |
return true; |
| 2098 |
} |
| 2099 |
|
| 2100 |
if (CBS_len(contents) != 0) { |
| 2101 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 2102 |
return false; |
| 2103 |
} |
| 2104 |
|
| 2105 |
hs->early_data_offered = true; |
| 2106 |
return true; |
| 2107 |
} |
| 2108 |
|
| 2109 |
static bool ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2110 |
if (!hs->ssl->s3->early_data_accepted) { |
| 2111 |
return true; |
| 2112 |
} |
| 2113 |
|
| 2114 |
if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) || |
| 2115 |
!CBB_add_u16(out, 0) || |
| 2116 |
!CBB_flush(out)) { |
| 2117 |
return false; |
| 2118 |
} |
| 2119 |
|
| 2120 |
return true; |
| 2121 |
} |
| 2122 |
|
| 2123 |
|
| 2124 |
// Key Share |
| 2125 |
// |
| 2126 |
// https://tools.ietf.org/html/rfc8446#section-4.2.8 |
| 2127 |
|
| 2128 |
bool ssl_setup_key_shares(SSL_HANDSHAKE *hs, uint16_t override_group_id) { |
| 2129 |
SSL *const ssl = hs->ssl; |
| 2130 |
hs->key_shares[0].reset(); |
| 2131 |
hs->key_shares[1].reset(); |
| 2132 |
hs->key_share_bytes.Reset(); |
| 2133 |
|
| 2134 |
if (hs->max_version < TLS1_3_VERSION) { |
| 2135 |
return true; |
| 2136 |
} |
| 2137 |
|
| 2138 |
bssl::ScopedCBB cbb; |
| 2139 |
if (!CBB_init(cbb.get(), 64)) { |
| 2140 |
return false; |
| 2141 |
} |
| 2142 |
|
| 2143 |
if (override_group_id == 0 && ssl->ctx->grease_enabled) { |
| 2144 |
// Add a fake group. See RFC 8701. |
| 2145 |
if (!CBB_add_u16(cbb.get(), ssl_get_grease_value(hs, ssl_grease_group)) || |
| 2146 |
!CBB_add_u16(cbb.get(), 1 /* length */) || |
| 2147 |
!CBB_add_u8(cbb.get(), 0 /* one byte key share */)) { |
| 2148 |
return false; |
| 2149 |
} |
| 2150 |
} |
| 2151 |
|
| 2152 |
uint16_t group_id = override_group_id; |
| 2153 |
uint16_t second_group_id = 0; |
| 2154 |
if (override_group_id == 0) { |
| 2155 |
// Predict the most preferred group. |
| 2156 |
Span<const uint16_t> groups = tls1_get_grouplist(hs); |
| 2157 |
if (groups.empty()) { |
| 2158 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED); |
| 2159 |
return false; |
| 2160 |
} |
| 2161 |
|
| 2162 |
group_id = groups[0]; |
| 2163 |
|
| 2164 |
// We'll try to include one post-quantum and one classical initial key |
| 2165 |
// share. |
| 2166 |
for (size_t i = 1; i < groups.size() && second_group_id == 0; i++) { |
| 2167 |
if (is_post_quantum_group(group_id) != is_post_quantum_group(groups[i])) { |
| 2168 |
second_group_id = groups[i]; |
| 2169 |
assert(second_group_id != group_id); |
| 2170 |
} |
| 2171 |
} |
| 2172 |
} |
| 2173 |
|
| 2174 |
CBB key_exchange; |
| 2175 |
hs->key_shares[0] = SSLKeyShare::Create(group_id); |
| 2176 |
if (!hs->key_shares[0] || // |
| 2177 |
!CBB_add_u16(cbb.get(), group_id) || |
| 2178 |
!CBB_add_u16_length_prefixed(cbb.get(), &key_exchange) || |
| 2179 |
!hs->key_shares[0]->Offer(&key_exchange)) { |
| 2180 |
return false; |
| 2181 |
} |
| 2182 |
|
| 2183 |
if (second_group_id != 0) { |
| 2184 |
hs->key_shares[1] = SSLKeyShare::Create(second_group_id); |
| 2185 |
if (!hs->key_shares[1] || // |
| 2186 |
!CBB_add_u16(cbb.get(), second_group_id) || |
| 2187 |
!CBB_add_u16_length_prefixed(cbb.get(), &key_exchange) || |
| 2188 |
!hs->key_shares[1]->Offer(&key_exchange)) { |
| 2189 |
return false; |
| 2190 |
} |
| 2191 |
} |
| 2192 |
|
| 2193 |
return CBBFinishArray(cbb.get(), &hs->key_share_bytes); |
| 2194 |
} |
| 2195 |
|
| 2196 |
static bool ext_key_share_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 2197 |
CBB *out_compressible, |
| 2198 |
ssl_client_hello_type_t type) { |
| 2199 |
if (hs->max_version < TLS1_3_VERSION) { |
| 2200 |
return true; |
| 2201 |
} |
| 2202 |
|
| 2203 |
assert(!hs->key_share_bytes.empty()); |
| 2204 |
CBB contents, kse_bytes; |
| 2205 |
if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_key_share) || |
| 2206 |
!CBB_add_u16_length_prefixed(out_compressible, &contents) || |
| 2207 |
!CBB_add_u16_length_prefixed(&contents, &kse_bytes) || |
| 2208 |
!CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(), |
| 2209 |
hs->key_share_bytes.size()) || |
| 2210 |
!CBB_flush(out_compressible)) { |
| 2211 |
return false; |
| 2212 |
} |
| 2213 |
|
| 2214 |
return true; |
| 2215 |
} |
| 2216 |
|
| 2217 |
bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2218 |
Array<uint8_t> *out_secret, |
| 2219 |
Array<uint8_t> *out_peer_key, |
| 2220 |
uint8_t *out_alert, CBS *contents) { |
| 2221 |
CBS peer_key; |
| 2222 |
uint16_t group_id; |
| 2223 |
if (!CBS_get_u16(contents, &group_id) || |
| 2224 |
!CBS_get_u16_length_prefixed(contents, &peer_key) || |
| 2225 |
CBS_len(contents) != 0) { |
| 2226 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 2227 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 2228 |
return false; |
| 2229 |
} |
| 2230 |
|
| 2231 |
SSLKeyShare *key_share = hs->key_shares[0].get(); |
| 2232 |
if (key_share->GroupID() != group_id) { |
| 2233 |
if (!hs->key_shares[1] || hs->key_shares[1]->GroupID() != group_id) { |
| 2234 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 2235 |
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 2236 |
return false; |
| 2237 |
} |
| 2238 |
key_share = hs->key_shares[1].get(); |
| 2239 |
} |
| 2240 |
|
| 2241 |
if (!key_share->Finish(out_secret, out_alert, peer_key) || |
| 2242 |
// Save peer's public key for observation with |SSL_get_peer_tmp_key|. |
| 2243 |
!out_peer_key->CopyFrom(peer_key)) { |
| 2244 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 2245 |
return false; |
| 2246 |
} |
| 2247 |
|
| 2248 |
hs->new_session->group_id = group_id; |
| 2249 |
hs->key_shares[0].reset(); |
| 2250 |
hs->key_shares[1].reset(); |
| 2251 |
return true; |
| 2252 |
} |
| 2253 |
|
| 2254 |
bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found, |
| 2255 |
Span<const uint8_t> *out_peer_key, |
| 2256 |
uint8_t *out_alert, |
| 2257 |
const SSL_CLIENT_HELLO *client_hello) { |
| 2258 |
// We only support connections that include an ECDHE key exchange. |
| 2259 |
CBS contents; |
| 2260 |
if (!ssl_client_hello_get_extension(client_hello, &contents, |
| 2261 |
TLSEXT_TYPE_key_share)) { |
| 2262 |
OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE); |
| 2263 |
*out_alert = SSL_AD_MISSING_EXTENSION; |
| 2264 |
return false; |
| 2265 |
} |
| 2266 |
|
| 2267 |
CBS key_shares; |
| 2268 |
if (!CBS_get_u16_length_prefixed(&contents, &key_shares) || |
| 2269 |
CBS_len(&contents) != 0) { |
| 2270 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 2271 |
return false; |
| 2272 |
} |
| 2273 |
|
| 2274 |
// Find the corresponding key share. |
| 2275 |
const uint16_t group_id = hs->new_session->group_id; |
| 2276 |
CBS peer_key; |
| 2277 |
CBS_init(&peer_key, nullptr, 0); |
| 2278 |
while (CBS_len(&key_shares) > 0) { |
| 2279 |
uint16_t id; |
| 2280 |
CBS peer_key_tmp; |
| 2281 |
if (!CBS_get_u16(&key_shares, &id) || |
| 2282 |
!CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp) || |
| 2283 |
CBS_len(&peer_key_tmp) == 0) { |
| 2284 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 2285 |
return false; |
| 2286 |
} |
| 2287 |
|
| 2288 |
if (id == group_id) { |
| 2289 |
if (CBS_len(&peer_key) != 0) { |
| 2290 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE); |
| 2291 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 2292 |
return false; |
| 2293 |
} |
| 2294 |
|
| 2295 |
peer_key = peer_key_tmp; |
| 2296 |
// Continue parsing the structure to keep peers honest. |
| 2297 |
} |
| 2298 |
} |
| 2299 |
|
| 2300 |
if (out_peer_key != nullptr) { |
| 2301 |
*out_peer_key = peer_key; |
| 2302 |
} |
| 2303 |
*out_found = CBS_len(&peer_key) != 0; |
| 2304 |
return true; |
| 2305 |
} |
| 2306 |
|
| 2307 |
bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2308 |
CBB kse_bytes, public_key; |
| 2309 |
if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) || |
| 2310 |
!CBB_add_u16_length_prefixed(out, &kse_bytes) || |
| 2311 |
!CBB_add_u16(&kse_bytes, hs->new_session->group_id) || |
| 2312 |
!CBB_add_u16_length_prefixed(&kse_bytes, &public_key) || |
| 2313 |
!CBB_add_bytes(&public_key, hs->ecdh_public_key.data(), |
| 2314 |
hs->ecdh_public_key.size()) || |
| 2315 |
!CBB_flush(out)) { |
| 2316 |
return false; |
| 2317 |
} |
| 2318 |
return true; |
| 2319 |
} |
| 2320 |
|
| 2321 |
|
| 2322 |
// Supported Versions |
| 2323 |
// |
| 2324 |
// https://tools.ietf.org/html/rfc8446#section-4.2.1 |
| 2325 |
|
| 2326 |
static bool ext_supported_versions_add_clienthello( |
| 2327 |
const SSL_HANDSHAKE *hs, CBB *out, CBB *out_compressible, |
| 2328 |
ssl_client_hello_type_t type) { |
| 2329 |
const SSL *const ssl = hs->ssl; |
| 2330 |
if (hs->max_version <= TLS1_2_VERSION) { |
| 2331 |
return true; |
| 2332 |
} |
| 2333 |
|
| 2334 |
// supported_versions is compressible in ECH if ClientHelloOuter already |
| 2335 |
// requires TLS 1.3. Otherwise the extensions differ in the older versions. |
| 2336 |
if (hs->min_version >= TLS1_3_VERSION) { |
| 2337 |
out = out_compressible; |
| 2338 |
} |
| 2339 |
|
| 2340 |
CBB contents, versions; |
| 2341 |
if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) || |
| 2342 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 2343 |
!CBB_add_u8_length_prefixed(&contents, &versions)) { |
| 2344 |
return false; |
| 2345 |
} |
| 2346 |
|
| 2347 |
// Add a fake version. See RFC 8701. |
| 2348 |
if (ssl->ctx->grease_enabled && |
| 2349 |
!CBB_add_u16(&versions, ssl_get_grease_value(hs, ssl_grease_version))) { |
| 2350 |
return false; |
| 2351 |
} |
| 2352 |
|
| 2353 |
// Encrypted ClientHellos requires TLS 1.3 or later. |
| 2354 |
uint16_t extra_min_version = |
| 2355 |
type == ssl_client_hello_inner ? TLS1_3_VERSION : 0; |
| 2356 |
if (!ssl_add_supported_versions(hs, &versions, extra_min_version) || |
| 2357 |
!CBB_flush(out)) { |
| 2358 |
return false; |
| 2359 |
} |
| 2360 |
|
| 2361 |
return true; |
| 2362 |
} |
| 2363 |
|
| 2364 |
|
| 2365 |
// Cookie |
| 2366 |
// |
| 2367 |
// https://tools.ietf.org/html/rfc8446#section-4.2.2 |
| 2368 |
|
| 2369 |
static bool ext_cookie_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 2370 |
CBB *out_compressible, |
| 2371 |
ssl_client_hello_type_t type) { |
| 2372 |
if (hs->cookie.empty()) { |
| 2373 |
return true; |
| 2374 |
} |
| 2375 |
|
| 2376 |
CBB contents, cookie; |
| 2377 |
if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_cookie) || |
| 2378 |
!CBB_add_u16_length_prefixed(out_compressible, &contents) || |
| 2379 |
!CBB_add_u16_length_prefixed(&contents, &cookie) || |
| 2380 |
!CBB_add_bytes(&cookie, hs->cookie.data(), hs->cookie.size()) || |
| 2381 |
!CBB_flush(out_compressible)) { |
| 2382 |
return false; |
| 2383 |
} |
| 2384 |
|
| 2385 |
return true; |
| 2386 |
} |
| 2387 |
|
| 2388 |
|
| 2389 |
// Supported Groups |
| 2390 |
// |
| 2391 |
// https://tools.ietf.org/html/rfc4492#section-5.1.1 |
| 2392 |
// https://tools.ietf.org/html/rfc8446#section-4.2.7 |
| 2393 |
|
| 2394 |
static bool ext_supported_groups_add_clienthello(const SSL_HANDSHAKE *hs, |
| 2395 |
CBB *out, |
| 2396 |
CBB *out_compressible, |
| 2397 |
ssl_client_hello_type_t type) { |
| 2398 |
const SSL *const ssl = hs->ssl; |
| 2399 |
CBB contents, groups_bytes; |
| 2400 |
if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_supported_groups) || |
| 2401 |
!CBB_add_u16_length_prefixed(out_compressible, &contents) || |
| 2402 |
!CBB_add_u16_length_prefixed(&contents, &groups_bytes)) { |
| 2403 |
return false; |
| 2404 |
} |
| 2405 |
|
| 2406 |
// Add a fake group. See RFC 8701. |
| 2407 |
if (ssl->ctx->grease_enabled && |
| 2408 |
!CBB_add_u16(&groups_bytes, |
| 2409 |
ssl_get_grease_value(hs, ssl_grease_group))) { |
| 2410 |
return false; |
| 2411 |
} |
| 2412 |
|
| 2413 |
for (uint16_t group : tls1_get_grouplist(hs)) { |
| 2414 |
if (is_post_quantum_group(group) && |
| 2415 |
hs->max_version < TLS1_3_VERSION) { |
| 2416 |
continue; |
| 2417 |
} |
| 2418 |
if (!CBB_add_u16(&groups_bytes, group)) { |
| 2419 |
return false; |
| 2420 |
} |
| 2421 |
} |
| 2422 |
|
| 2423 |
return CBB_flush(out_compressible); |
| 2424 |
} |
| 2425 |
|
| 2426 |
static bool ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2427 |
uint8_t *out_alert, |
| 2428 |
CBS *contents) { |
| 2429 |
// This extension is not expected to be echoed by servers in TLS 1.2, but some |
| 2430 |
// BigIP servers send it nonetheless, so do not enforce this. |
| 2431 |
return true; |
| 2432 |
} |
| 2433 |
|
| 2434 |
static bool parse_u16_array(const CBS *cbs, Array<uint16_t> *out) { |
| 2435 |
CBS copy = *cbs; |
| 2436 |
if ((CBS_len(©) & 1) != 0) { |
| 2437 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 2438 |
return false; |
| 2439 |
} |
| 2440 |
|
| 2441 |
Array<uint16_t> ret; |
| 2442 |
if (!ret.Init(CBS_len(©) / 2)) { |
| 2443 |
return false; |
| 2444 |
} |
| 2445 |
for (size_t i = 0; i < ret.size(); i++) { |
| 2446 |
if (!CBS_get_u16(©, &ret[i])) { |
| 2447 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2448 |
return false; |
| 2449 |
} |
| 2450 |
} |
| 2451 |
|
| 2452 |
assert(CBS_len(©) == 0); |
| 2453 |
*out = std::move(ret); |
| 2454 |
return true; |
| 2455 |
} |
| 2456 |
|
| 2457 |
static bool ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2458 |
uint8_t *out_alert, |
| 2459 |
CBS *contents) { |
| 2460 |
if (contents == NULL) { |
| 2461 |
return true; |
| 2462 |
} |
| 2463 |
|
| 2464 |
CBS supported_group_list; |
| 2465 |
if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) || |
| 2466 |
CBS_len(&supported_group_list) == 0 || |
| 2467 |
CBS_len(contents) != 0 || |
| 2468 |
!parse_u16_array(&supported_group_list, &hs->peer_supported_group_list)) { |
| 2469 |
return false; |
| 2470 |
} |
| 2471 |
|
| 2472 |
return true; |
| 2473 |
} |
| 2474 |
|
| 2475 |
|
| 2476 |
// QUIC Transport Parameters |
| 2477 |
|
| 2478 |
static bool ext_quic_transport_params_add_clienthello_impl( |
| 2479 |
const SSL_HANDSHAKE *hs, CBB *out, bool use_legacy_codepoint) { |
| 2480 |
if (hs->config->quic_transport_params.empty() && !hs->ssl->quic_method) { |
| 2481 |
return true; |
| 2482 |
} |
| 2483 |
if (hs->config->quic_transport_params.empty() || !hs->ssl->quic_method) { |
| 2484 |
// QUIC Transport Parameters must be sent over QUIC, and they must not be |
| 2485 |
// sent over non-QUIC transports. If transport params are set, then |
| 2486 |
// SSL(_CTX)_set_quic_method must also be called. |
| 2487 |
OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_TRANSPORT_PARAMETERS_MISCONFIGURED); |
| 2488 |
return false; |
| 2489 |
} |
| 2490 |
assert(hs->min_version > TLS1_2_VERSION); |
| 2491 |
if (use_legacy_codepoint != hs->config->quic_use_legacy_codepoint) { |
| 2492 |
// Do nothing, we'll send the other codepoint. |
| 2493 |
return true; |
| 2494 |
} |
| 2495 |
|
| 2496 |
uint16_t extension_type = TLSEXT_TYPE_quic_transport_parameters; |
| 2497 |
if (hs->config->quic_use_legacy_codepoint) { |
| 2498 |
extension_type = TLSEXT_TYPE_quic_transport_parameters_legacy; |
| 2499 |
} |
| 2500 |
|
| 2501 |
CBB contents; |
| 2502 |
if (!CBB_add_u16(out, extension_type) || |
| 2503 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 2504 |
!CBB_add_bytes(&contents, hs->config->quic_transport_params.data(), |
| 2505 |
hs->config->quic_transport_params.size()) || |
| 2506 |
!CBB_flush(out)) { |
| 2507 |
return false; |
| 2508 |
} |
| 2509 |
return true; |
| 2510 |
} |
| 2511 |
|
| 2512 |
static bool ext_quic_transport_params_add_clienthello( |
| 2513 |
const SSL_HANDSHAKE *hs, CBB *out, CBB *out_compressible, |
| 2514 |
ssl_client_hello_type_t type) { |
| 2515 |
return ext_quic_transport_params_add_clienthello_impl( |
| 2516 |
hs, out_compressible, /*use_legacy_codepoint=*/false); |
| 2517 |
} |
| 2518 |
|
| 2519 |
static bool ext_quic_transport_params_add_clienthello_legacy( |
| 2520 |
const SSL_HANDSHAKE *hs, CBB *out, CBB *out_compressible, |
| 2521 |
ssl_client_hello_type_t type) { |
| 2522 |
return ext_quic_transport_params_add_clienthello_impl( |
| 2523 |
hs, out_compressible, /*use_legacy_codepoint=*/true); |
| 2524 |
} |
| 2525 |
|
| 2526 |
static bool ext_quic_transport_params_parse_serverhello_impl( |
| 2527 |
SSL_HANDSHAKE *hs, uint8_t *out_alert, CBS *contents, |
| 2528 |
bool used_legacy_codepoint) { |
| 2529 |
SSL *const ssl = hs->ssl; |
| 2530 |
if (contents == nullptr) { |
| 2531 |
if (used_legacy_codepoint != hs->config->quic_use_legacy_codepoint) { |
| 2532 |
// Silently ignore because we expect the other QUIC codepoint. |
| 2533 |
return true; |
| 2534 |
} |
| 2535 |
if (!ssl->quic_method) { |
| 2536 |
return true; |
| 2537 |
} |
| 2538 |
*out_alert = SSL_AD_MISSING_EXTENSION; |
| 2539 |
return false; |
| 2540 |
} |
| 2541 |
// The extensions parser will check for unsolicited extensions before |
| 2542 |
// calling the callback. |
| 2543 |
assert(ssl->quic_method != nullptr); |
| 2544 |
assert(ssl_protocol_version(ssl) == TLS1_3_VERSION); |
| 2545 |
assert(used_legacy_codepoint == hs->config->quic_use_legacy_codepoint); |
| 2546 |
return ssl->s3->peer_quic_transport_params.CopyFrom(*contents); |
| 2547 |
} |
| 2548 |
|
| 2549 |
static bool ext_quic_transport_params_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2550 |
uint8_t *out_alert, |
| 2551 |
CBS *contents) { |
| 2552 |
return ext_quic_transport_params_parse_serverhello_impl( |
| 2553 |
hs, out_alert, contents, /*used_legacy_codepoint=*/false); |
| 2554 |
} |
| 2555 |
|
| 2556 |
static bool ext_quic_transport_params_parse_serverhello_legacy( |
| 2557 |
SSL_HANDSHAKE *hs, uint8_t *out_alert, CBS *contents) { |
| 2558 |
return ext_quic_transport_params_parse_serverhello_impl( |
| 2559 |
hs, out_alert, contents, /*used_legacy_codepoint=*/true); |
| 2560 |
} |
| 2561 |
|
| 2562 |
static bool ext_quic_transport_params_parse_clienthello_impl( |
| 2563 |
SSL_HANDSHAKE *hs, uint8_t *out_alert, CBS *contents, |
| 2564 |
bool used_legacy_codepoint) { |
| 2565 |
SSL *const ssl = hs->ssl; |
| 2566 |
if (!contents) { |
| 2567 |
if (!ssl->quic_method) { |
| 2568 |
if (hs->config->quic_transport_params.empty()) { |
| 2569 |
return true; |
| 2570 |
} |
| 2571 |
// QUIC transport parameters must not be set if |ssl| is not configured |
| 2572 |
// for QUIC. |
| 2573 |
OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_TRANSPORT_PARAMETERS_MISCONFIGURED); |
| 2574 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 2575 |
return false; |
| 2576 |
} |
| 2577 |
if (used_legacy_codepoint != hs->config->quic_use_legacy_codepoint) { |
| 2578 |
// Silently ignore because we expect the other QUIC codepoint. |
| 2579 |
return true; |
| 2580 |
} |
| 2581 |
*out_alert = SSL_AD_MISSING_EXTENSION; |
| 2582 |
return false; |
| 2583 |
} |
| 2584 |
if (!ssl->quic_method) { |
| 2585 |
if (used_legacy_codepoint) { |
| 2586 |
// Ignore the legacy private-use codepoint because that could be sent |
| 2587 |
// to mean something else than QUIC transport parameters. |
| 2588 |
return true; |
| 2589 |
} |
| 2590 |
// Fail if we received the codepoint registered with IANA for QUIC |
| 2591 |
// because that is not allowed outside of QUIC. |
| 2592 |
*out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2593 |
return false; |
| 2594 |
} |
| 2595 |
assert(ssl_protocol_version(ssl) == TLS1_3_VERSION); |
| 2596 |
if (used_legacy_codepoint != hs->config->quic_use_legacy_codepoint) { |
| 2597 |
// Silently ignore because we expect the other QUIC codepoint. |
| 2598 |
return true; |
| 2599 |
} |
| 2600 |
return ssl->s3->peer_quic_transport_params.CopyFrom(*contents); |
| 2601 |
} |
| 2602 |
|
| 2603 |
static bool ext_quic_transport_params_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2604 |
uint8_t *out_alert, |
| 2605 |
CBS *contents) { |
| 2606 |
return ext_quic_transport_params_parse_clienthello_impl( |
| 2607 |
hs, out_alert, contents, /*used_legacy_codepoint=*/false); |
| 2608 |
} |
| 2609 |
|
| 2610 |
static bool ext_quic_transport_params_parse_clienthello_legacy( |
| 2611 |
SSL_HANDSHAKE *hs, uint8_t *out_alert, CBS *contents) { |
| 2612 |
return ext_quic_transport_params_parse_clienthello_impl( |
| 2613 |
hs, out_alert, contents, /*used_legacy_codepoint=*/true); |
| 2614 |
} |
| 2615 |
|
| 2616 |
static bool ext_quic_transport_params_add_serverhello_impl( |
| 2617 |
SSL_HANDSHAKE *hs, CBB *out, bool use_legacy_codepoint) { |
| 2618 |
if (hs->ssl->quic_method == nullptr && use_legacy_codepoint) { |
| 2619 |
// Ignore the legacy private-use codepoint because that could be sent |
| 2620 |
// to mean something else than QUIC transport parameters. |
| 2621 |
return true; |
| 2622 |
} |
| 2623 |
assert(hs->ssl->quic_method != nullptr); |
| 2624 |
if (hs->config->quic_transport_params.empty()) { |
| 2625 |
// Transport parameters must be set when using QUIC. |
| 2626 |
OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_TRANSPORT_PARAMETERS_MISCONFIGURED); |
| 2627 |
return false; |
| 2628 |
} |
| 2629 |
if (use_legacy_codepoint != hs->config->quic_use_legacy_codepoint) { |
| 2630 |
// Do nothing, we'll send the other codepoint. |
| 2631 |
return true; |
| 2632 |
} |
| 2633 |
|
| 2634 |
uint16_t extension_type = TLSEXT_TYPE_quic_transport_parameters; |
| 2635 |
if (hs->config->quic_use_legacy_codepoint) { |
| 2636 |
extension_type = TLSEXT_TYPE_quic_transport_parameters_legacy; |
| 2637 |
} |
| 2638 |
|
| 2639 |
CBB contents; |
| 2640 |
if (!CBB_add_u16(out, extension_type) || |
| 2641 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 2642 |
!CBB_add_bytes(&contents, hs->config->quic_transport_params.data(), |
| 2643 |
hs->config->quic_transport_params.size()) || |
| 2644 |
!CBB_flush(out)) { |
| 2645 |
return false; |
| 2646 |
} |
| 2647 |
|
| 2648 |
return true; |
| 2649 |
} |
| 2650 |
|
| 2651 |
static bool ext_quic_transport_params_add_serverhello(SSL_HANDSHAKE *hs, |
| 2652 |
CBB *out) { |
| 2653 |
return ext_quic_transport_params_add_serverhello_impl( |
| 2654 |
hs, out, /*use_legacy_codepoint=*/false); |
| 2655 |
} |
| 2656 |
|
| 2657 |
static bool ext_quic_transport_params_add_serverhello_legacy(SSL_HANDSHAKE *hs, |
| 2658 |
CBB *out) { |
| 2659 |
return ext_quic_transport_params_add_serverhello_impl( |
| 2660 |
hs, out, /*use_legacy_codepoint=*/true); |
| 2661 |
} |
| 2662 |
|
| 2663 |
// Delegated credentials. |
| 2664 |
// |
| 2665 |
// https://www.rfc-editor.org/rfc/rfc9345.html |
| 2666 |
|
| 2667 |
static bool ext_delegated_credential_add_clienthello( |
| 2668 |
const SSL_HANDSHAKE *hs, CBB *out, CBB *out_compressible, |
| 2669 |
ssl_client_hello_type_t type) { |
| 2670 |
return true; |
| 2671 |
} |
| 2672 |
|
| 2673 |
static bool ext_delegated_credential_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2674 |
uint8_t *out_alert, |
| 2675 |
CBS *contents) { |
| 2676 |
if (contents == nullptr || ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) { |
| 2677 |
// Don't use delegated credentials unless we're negotiating TLS 1.3 or |
| 2678 |
// higher. |
| 2679 |
return true; |
| 2680 |
} |
| 2681 |
|
| 2682 |
// The contents of the extension are the signature algorithms the client will |
| 2683 |
// accept for a delegated credential. |
| 2684 |
CBS sigalg_list; |
| 2685 |
if (!CBS_get_u16_length_prefixed(contents, &sigalg_list) || |
| 2686 |
CBS_len(&sigalg_list) == 0 || |
| 2687 |
CBS_len(contents) != 0 || |
| 2688 |
!parse_u16_array(&sigalg_list, &hs->peer_delegated_credential_sigalgs)) { |
| 2689 |
return false; |
| 2690 |
} |
| 2691 |
|
| 2692 |
return true; |
| 2693 |
} |
| 2694 |
|
| 2695 |
// Certificate compression |
| 2696 |
|
| 2697 |
static bool cert_compression_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 2698 |
CBB *out_compressible, |
| 2699 |
ssl_client_hello_type_t type) { |
| 2700 |
bool first = true; |
| 2701 |
CBB contents, algs; |
| 2702 |
|
| 2703 |
for (const auto &alg : hs->ssl->ctx->cert_compression_algs) { |
| 2704 |
if (alg.decompress == nullptr) { |
| 2705 |
continue; |
| 2706 |
} |
| 2707 |
|
| 2708 |
if (first && |
| 2709 |
(!CBB_add_u16(out_compressible, TLSEXT_TYPE_cert_compression) || |
| 2710 |
!CBB_add_u16_length_prefixed(out_compressible, &contents) || |
| 2711 |
!CBB_add_u8_length_prefixed(&contents, &algs))) { |
| 2712 |
return false; |
| 2713 |
} |
| 2714 |
first = false; |
| 2715 |
if (!CBB_add_u16(&algs, alg.alg_id)) { |
| 2716 |
return false; |
| 2717 |
} |
| 2718 |
} |
| 2719 |
|
| 2720 |
return first || CBB_flush(out_compressible); |
| 2721 |
} |
| 2722 |
|
| 2723 |
static bool cert_compression_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2724 |
uint8_t *out_alert, |
| 2725 |
CBS *contents) { |
| 2726 |
if (contents == nullptr) { |
| 2727 |
return true; |
| 2728 |
} |
| 2729 |
|
| 2730 |
// The server may not echo this extension. Any server to client negotiation is |
| 2731 |
// advertised in the CertificateRequest message. |
| 2732 |
return false; |
| 2733 |
} |
| 2734 |
|
| 2735 |
static bool cert_compression_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2736 |
uint8_t *out_alert, |
| 2737 |
CBS *contents) { |
| 2738 |
if (contents == nullptr) { |
| 2739 |
return true; |
| 2740 |
} |
| 2741 |
|
| 2742 |
const SSL_CTX *ctx = hs->ssl->ctx.get(); |
| 2743 |
const size_t num_algs = ctx->cert_compression_algs.size(); |
| 2744 |
|
| 2745 |
CBS alg_ids; |
| 2746 |
if (!CBS_get_u8_length_prefixed(contents, &alg_ids) || |
| 2747 |
CBS_len(contents) != 0 || |
| 2748 |
CBS_len(&alg_ids) == 0 || |
| 2749 |
CBS_len(&alg_ids) % 2 == 1) { |
| 2750 |
return false; |
| 2751 |
} |
| 2752 |
|
| 2753 |
const size_t num_given_alg_ids = CBS_len(&alg_ids) / 2; |
| 2754 |
Array<uint16_t> given_alg_ids; |
| 2755 |
if (!given_alg_ids.Init(num_given_alg_ids)) { |
| 2756 |
return false; |
| 2757 |
} |
| 2758 |
|
| 2759 |
size_t best_index = num_algs; |
| 2760 |
size_t given_alg_idx = 0; |
| 2761 |
|
| 2762 |
while (CBS_len(&alg_ids) > 0) { |
| 2763 |
uint16_t alg_id; |
| 2764 |
if (!CBS_get_u16(&alg_ids, &alg_id)) { |
| 2765 |
return false; |
| 2766 |
} |
| 2767 |
|
| 2768 |
given_alg_ids[given_alg_idx++] = alg_id; |
| 2769 |
|
| 2770 |
for (size_t i = 0; i < num_algs; i++) { |
| 2771 |
const auto &alg = ctx->cert_compression_algs[i]; |
| 2772 |
if (alg.alg_id == alg_id && alg.compress != nullptr) { |
| 2773 |
if (i < best_index) { |
| 2774 |
best_index = i; |
| 2775 |
} |
| 2776 |
break; |
| 2777 |
} |
| 2778 |
} |
| 2779 |
} |
| 2780 |
|
| 2781 |
qsort(given_alg_ids.data(), given_alg_ids.size(), sizeof(uint16_t), |
| 2782 |
compare_uint16_t); |
| 2783 |
for (size_t i = 1; i < num_given_alg_ids; i++) { |
| 2784 |
if (given_alg_ids[i - 1] == given_alg_ids[i]) { |
| 2785 |
return false; |
| 2786 |
} |
| 2787 |
} |
| 2788 |
|
| 2789 |
if (best_index < num_algs && |
| 2790 |
ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
| 2791 |
hs->cert_compression_negotiated = true; |
| 2792 |
hs->cert_compression_alg_id = ctx->cert_compression_algs[best_index].alg_id; |
| 2793 |
} |
| 2794 |
|
| 2795 |
return true; |
| 2796 |
} |
| 2797 |
|
| 2798 |
static bool cert_compression_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2799 |
return true; |
| 2800 |
} |
| 2801 |
|
| 2802 |
// Application-level Protocol Settings |
| 2803 |
// |
| 2804 |
// https://tools.ietf.org/html/draft-vvv-tls-alps-01 |
| 2805 |
|
| 2806 |
bool ssl_get_local_application_settings(const SSL_HANDSHAKE *hs, |
| 2807 |
Span<const uint8_t> *out_settings, |
| 2808 |
Span<const uint8_t> protocol) { |
| 2809 |
for (const ALPSConfig &config : hs->config->alps_configs) { |
| 2810 |
if (protocol == config.protocol) { |
| 2811 |
*out_settings = config.settings; |
| 2812 |
return true; |
| 2813 |
} |
| 2814 |
} |
| 2815 |
return false; |
| 2816 |
} |
| 2817 |
|
| 2818 |
static bool ext_alps_add_clienthello_impl(const SSL_HANDSHAKE *hs, CBB *out, |
| 2819 |
CBB *out_compressible, |
| 2820 |
ssl_client_hello_type_t type, |
| 2821 |
bool use_new_codepoint) { |
| 2822 |
const SSL *const ssl = hs->ssl; |
| 2823 |
if (// ALPS requires TLS 1.3. |
| 2824 |
hs->max_version < TLS1_3_VERSION || |
| 2825 |
// Do not offer ALPS without ALPN. |
| 2826 |
hs->config->alpn_client_proto_list.empty() || |
| 2827 |
// Do not offer ALPS if not configured. |
| 2828 |
hs->config->alps_configs.empty() || |
| 2829 |
// Do not offer ALPS on renegotiation handshakes. |
| 2830 |
ssl->s3->initial_handshake_complete) { |
| 2831 |
return true; |
| 2832 |
} |
| 2833 |
|
| 2834 |
if (use_new_codepoint != hs->config->alps_use_new_codepoint) { |
| 2835 |
// Do nothing, we'll send the other codepoint. |
| 2836 |
return true; |
| 2837 |
} |
| 2838 |
|
| 2839 |
uint16_t extension_type = TLSEXT_TYPE_application_settings_old; |
| 2840 |
if (hs->config->alps_use_new_codepoint) { |
| 2841 |
extension_type = TLSEXT_TYPE_application_settings; |
| 2842 |
} |
| 2843 |
|
| 2844 |
CBB contents, proto_list, proto; |
| 2845 |
if (!CBB_add_u16(out_compressible, extension_type) || |
| 2846 |
!CBB_add_u16_length_prefixed(out_compressible, &contents) || |
| 2847 |
!CBB_add_u16_length_prefixed(&contents, &proto_list)) { |
| 2848 |
return false; |
| 2849 |
} |
| 2850 |
|
| 2851 |
for (const ALPSConfig &config : hs->config->alps_configs) { |
| 2852 |
if (!CBB_add_u8_length_prefixed(&proto_list, &proto) || |
| 2853 |
!CBB_add_bytes(&proto, config.protocol.data(), |
| 2854 |
config.protocol.size())) { |
| 2855 |
return false; |
| 2856 |
} |
| 2857 |
} |
| 2858 |
|
| 2859 |
return CBB_flush(out_compressible); |
| 2860 |
} |
| 2861 |
|
| 2862 |
static bool ext_alps_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 2863 |
CBB *out_compressible, |
| 2864 |
ssl_client_hello_type_t type) { |
| 2865 |
return ext_alps_add_clienthello_impl(hs, out, out_compressible, type, |
| 2866 |
/*use_new_codepoint=*/true); |
| 2867 |
} |
| 2868 |
|
| 2869 |
static bool ext_alps_add_clienthello_old(const SSL_HANDSHAKE *hs, CBB *out, |
| 2870 |
CBB *out_compressible, |
| 2871 |
ssl_client_hello_type_t type) { |
| 2872 |
return ext_alps_add_clienthello_impl(hs, out, out_compressible, type, |
| 2873 |
/*use_new_codepoint=*/false); |
| 2874 |
} |
| 2875 |
|
| 2876 |
static bool ext_alps_parse_serverhello_impl(SSL_HANDSHAKE *hs, |
| 2877 |
uint8_t *out_alert, |
| 2878 |
CBS *contents, |
| 2879 |
bool use_new_codepoint) { |
| 2880 |
SSL *const ssl = hs->ssl; |
| 2881 |
if (contents == nullptr) { |
| 2882 |
return true; |
| 2883 |
} |
| 2884 |
|
| 2885 |
assert(!ssl->s3->initial_handshake_complete); |
| 2886 |
assert(!hs->config->alpn_client_proto_list.empty()); |
| 2887 |
assert(!hs->config->alps_configs.empty()); |
| 2888 |
assert(use_new_codepoint == hs->config->alps_use_new_codepoint); |
| 2889 |
|
| 2890 |
// ALPS requires TLS 1.3. |
| 2891 |
if (ssl_protocol_version(ssl) < TLS1_3_VERSION) { |
| 2892 |
*out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2893 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 2894 |
return false; |
| 2895 |
} |
| 2896 |
|
| 2897 |
// Note extension callbacks may run in any order, so we defer checking |
| 2898 |
// consistency with ALPN to |ssl_check_serverhello_tlsext|. |
| 2899 |
if (!hs->new_session->peer_application_settings.CopyFrom(*contents)) { |
| 2900 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 2901 |
return false; |
| 2902 |
} |
| 2903 |
|
| 2904 |
hs->new_session->has_application_settings = true; |
| 2905 |
return true; |
| 2906 |
} |
| 2907 |
|
| 2908 |
static bool ext_alps_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2909 |
uint8_t *out_alert, |
| 2910 |
CBS *contents) { |
| 2911 |
return ext_alps_parse_serverhello_impl(hs, out_alert, contents, |
| 2912 |
/*use_new_codepoint=*/true); |
| 2913 |
} |
| 2914 |
|
| 2915 |
static bool ext_alps_parse_serverhello_old(SSL_HANDSHAKE *hs, |
| 2916 |
uint8_t *out_alert, |
| 2917 |
CBS *contents) { |
| 2918 |
return ext_alps_parse_serverhello_impl(hs, out_alert, contents, |
| 2919 |
/*use_new_codepoint=*/false); |
| 2920 |
} |
| 2921 |
|
| 2922 |
static bool ext_alps_add_serverhello_impl(SSL_HANDSHAKE *hs, CBB *out, |
| 2923 |
bool use_new_codepoint) { |
| 2924 |
SSL *const ssl = hs->ssl; |
| 2925 |
// If early data is accepted, we omit the ALPS extension. It is implicitly |
| 2926 |
// carried over from the previous connection. |
| 2927 |
if (hs->new_session == nullptr || |
| 2928 |
!hs->new_session->has_application_settings || |
| 2929 |
ssl->s3->early_data_accepted) { |
| 2930 |
return true; |
| 2931 |
} |
| 2932 |
|
| 2933 |
if (use_new_codepoint != hs->config->alps_use_new_codepoint) { |
| 2934 |
// Do nothing, we'll send the other codepoint. |
| 2935 |
return true; |
| 2936 |
} |
| 2937 |
|
| 2938 |
uint16_t extension_type = TLSEXT_TYPE_application_settings_old; |
| 2939 |
if (hs->config->alps_use_new_codepoint) { |
| 2940 |
extension_type = TLSEXT_TYPE_application_settings; |
| 2941 |
} |
| 2942 |
|
| 2943 |
CBB contents; |
| 2944 |
if (!CBB_add_u16(out, extension_type) || |
| 2945 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 2946 |
!CBB_add_bytes(&contents, |
| 2947 |
hs->new_session->local_application_settings.data(), |
| 2948 |
hs->new_session->local_application_settings.size()) || |
| 2949 |
!CBB_flush(out)) { |
| 2950 |
return false; |
| 2951 |
} |
| 2952 |
|
| 2953 |
return true; |
| 2954 |
} |
| 2955 |
|
| 2956 |
static bool ext_alps_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2957 |
return ext_alps_add_serverhello_impl(hs, out, /*use_new_codepoint=*/true); |
| 2958 |
} |
| 2959 |
|
| 2960 |
static bool ext_alps_add_serverhello_old(SSL_HANDSHAKE *hs, CBB *out) { |
| 2961 |
return ext_alps_add_serverhello_impl(hs, out, /*use_new_codepoint=*/false); |
| 2962 |
} |
| 2963 |
|
| 2964 |
bool ssl_negotiate_alps(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 2965 |
const SSL_CLIENT_HELLO *client_hello) { |
| 2966 |
SSL *const ssl = hs->ssl; |
| 2967 |
if (ssl->s3->alpn_selected.empty()) { |
| 2968 |
return true; |
| 2969 |
} |
| 2970 |
|
| 2971 |
// If we negotiate ALPN over TLS 1.3, try to negotiate ALPS. |
| 2972 |
CBS alps_contents; |
| 2973 |
Span<const uint8_t> settings; |
| 2974 |
uint16_t extension_type = TLSEXT_TYPE_application_settings_old; |
| 2975 |
if (hs->config->alps_use_new_codepoint) { |
| 2976 |
extension_type = TLSEXT_TYPE_application_settings; |
| 2977 |
} |
| 2978 |
if (ssl_protocol_version(ssl) >= TLS1_3_VERSION && |
| 2979 |
ssl_get_local_application_settings(hs, &settings, |
| 2980 |
ssl->s3->alpn_selected) && |
| 2981 |
ssl_client_hello_get_extension(client_hello, &alps_contents, |
| 2982 |
extension_type)) { |
| 2983 |
// Check if the client supports ALPS with the selected ALPN. |
| 2984 |
bool found = false; |
| 2985 |
CBS alps_list; |
| 2986 |
if (!CBS_get_u16_length_prefixed(&alps_contents, &alps_list) || |
| 2987 |
CBS_len(&alps_contents) != 0 || |
| 2988 |
CBS_len(&alps_list) == 0) { |
| 2989 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 2990 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 2991 |
return false; |
| 2992 |
} |
| 2993 |
while (CBS_len(&alps_list) > 0) { |
| 2994 |
CBS protocol_name; |
| 2995 |
if (!CBS_get_u8_length_prefixed(&alps_list, &protocol_name) || |
| 2996 |
// Empty protocol names are forbidden. |
| 2997 |
CBS_len(&protocol_name) == 0) { |
| 2998 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 2999 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 3000 |
return false; |
| 3001 |
} |
| 3002 |
if (protocol_name == MakeConstSpan(ssl->s3->alpn_selected)) { |
| 3003 |
found = true; |
| 3004 |
} |
| 3005 |
} |
| 3006 |
|
| 3007 |
// Negotiate ALPS if both client also supports ALPS for this protocol. |
| 3008 |
if (found) { |
| 3009 |
hs->new_session->has_application_settings = true; |
| 3010 |
if (!hs->new_session->local_application_settings.CopyFrom(settings)) { |
| 3011 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 3012 |
return false; |
| 3013 |
} |
| 3014 |
} |
| 3015 |
} |
| 3016 |
|
| 3017 |
return true; |
| 3018 |
} |
| 3019 |
|
| 3020 |
// kExtensions contains all the supported extensions. |
| 3021 |
static const struct tls_extension kExtensions[] = { |
| 3022 |
{ |
| 3023 |
TLSEXT_TYPE_server_name, |
| 3024 |
ext_sni_add_clienthello, |
| 3025 |
ext_sni_parse_serverhello, |
| 3026 |
ext_sni_parse_clienthello, |
| 3027 |
ext_sni_add_serverhello, |
| 3028 |
}, |
| 3029 |
{ |
| 3030 |
TLSEXT_TYPE_encrypted_client_hello, |
| 3031 |
ext_ech_add_clienthello, |
| 3032 |
ext_ech_parse_serverhello, |
| 3033 |
ext_ech_parse_clienthello, |
| 3034 |
ext_ech_add_serverhello, |
| 3035 |
}, |
| 3036 |
{ |
| 3037 |
TLSEXT_TYPE_extended_master_secret, |
| 3038 |
ext_ems_add_clienthello, |
| 3039 |
ext_ems_parse_serverhello, |
| 3040 |
ext_ems_parse_clienthello, |
| 3041 |
ext_ems_add_serverhello, |
| 3042 |
}, |
| 3043 |
{ |
| 3044 |
TLSEXT_TYPE_renegotiate, |
| 3045 |
ext_ri_add_clienthello, |
| 3046 |
ext_ri_parse_serverhello, |
| 3047 |
ext_ri_parse_clienthello, |
| 3048 |
ext_ri_add_serverhello, |
| 3049 |
}, |
| 3050 |
{ |
| 3051 |
TLSEXT_TYPE_supported_groups, |
| 3052 |
ext_supported_groups_add_clienthello, |
| 3053 |
ext_supported_groups_parse_serverhello, |
| 3054 |
ext_supported_groups_parse_clienthello, |
| 3055 |
dont_add_serverhello, |
| 3056 |
}, |
| 3057 |
{ |
| 3058 |
TLSEXT_TYPE_ec_point_formats, |
| 3059 |
ext_ec_point_add_clienthello, |
| 3060 |
ext_ec_point_parse_serverhello, |
| 3061 |
ext_ec_point_parse_clienthello, |
| 3062 |
ext_ec_point_add_serverhello, |
| 3063 |
}, |
| 3064 |
{ |
| 3065 |
TLSEXT_TYPE_session_ticket, |
| 3066 |
ext_ticket_add_clienthello, |
| 3067 |
ext_ticket_parse_serverhello, |
| 3068 |
// Ticket extension client parsing is handled in ssl_session.c |
| 3069 |
ignore_parse_clienthello, |
| 3070 |
ext_ticket_add_serverhello, |
| 3071 |
}, |
| 3072 |
{ |
| 3073 |
TLSEXT_TYPE_application_layer_protocol_negotiation, |
| 3074 |
ext_alpn_add_clienthello, |
| 3075 |
ext_alpn_parse_serverhello, |
| 3076 |
// ALPN is negotiated late in |ssl_negotiate_alpn|. |
| 3077 |
ignore_parse_clienthello, |
| 3078 |
ext_alpn_add_serverhello, |
| 3079 |
}, |
| 3080 |
{ |
| 3081 |
TLSEXT_TYPE_status_request, |
| 3082 |
ext_ocsp_add_clienthello, |
| 3083 |
ext_ocsp_parse_serverhello, |
| 3084 |
ext_ocsp_parse_clienthello, |
| 3085 |
ext_ocsp_add_serverhello, |
| 3086 |
}, |
| 3087 |
{ |
| 3088 |
TLSEXT_TYPE_signature_algorithms, |
| 3089 |
ext_sigalgs_add_clienthello, |
| 3090 |
forbid_parse_serverhello, |
| 3091 |
ext_sigalgs_parse_clienthello, |
| 3092 |
dont_add_serverhello, |
| 3093 |
}, |
| 3094 |
{ |
| 3095 |
TLSEXT_TYPE_next_proto_neg, |
| 3096 |
ext_npn_add_clienthello, |
| 3097 |
ext_npn_parse_serverhello, |
| 3098 |
ext_npn_parse_clienthello, |
| 3099 |
ext_npn_add_serverhello, |
| 3100 |
}, |
| 3101 |
{ |
| 3102 |
TLSEXT_TYPE_certificate_timestamp, |
| 3103 |
ext_sct_add_clienthello, |
| 3104 |
ext_sct_parse_serverhello, |
| 3105 |
ext_sct_parse_clienthello, |
| 3106 |
ext_sct_add_serverhello, |
| 3107 |
}, |
| 3108 |
{ |
| 3109 |
TLSEXT_TYPE_channel_id, |
| 3110 |
ext_channel_id_add_clienthello, |
| 3111 |
ext_channel_id_parse_serverhello, |
| 3112 |
ext_channel_id_parse_clienthello, |
| 3113 |
ext_channel_id_add_serverhello, |
| 3114 |
}, |
| 3115 |
{ |
| 3116 |
TLSEXT_TYPE_srtp, |
| 3117 |
ext_srtp_add_clienthello, |
| 3118 |
ext_srtp_parse_serverhello, |
| 3119 |
ext_srtp_parse_clienthello, |
| 3120 |
ext_srtp_add_serverhello, |
| 3121 |
}, |
| 3122 |
{ |
| 3123 |
TLSEXT_TYPE_key_share, |
| 3124 |
ext_key_share_add_clienthello, |
| 3125 |
forbid_parse_serverhello, |
| 3126 |
ignore_parse_clienthello, |
| 3127 |
dont_add_serverhello, |
| 3128 |
}, |
| 3129 |
{ |
| 3130 |
TLSEXT_TYPE_psk_key_exchange_modes, |
| 3131 |
ext_psk_key_exchange_modes_add_clienthello, |
| 3132 |
forbid_parse_serverhello, |
| 3133 |
ext_psk_key_exchange_modes_parse_clienthello, |
| 3134 |
dont_add_serverhello, |
| 3135 |
}, |
| 3136 |
{ |
| 3137 |
TLSEXT_TYPE_early_data, |
| 3138 |
ext_early_data_add_clienthello, |
| 3139 |
ext_early_data_parse_serverhello, |
| 3140 |
ext_early_data_parse_clienthello, |
| 3141 |
ext_early_data_add_serverhello, |
| 3142 |
}, |
| 3143 |
{ |
| 3144 |
TLSEXT_TYPE_supported_versions, |
| 3145 |
ext_supported_versions_add_clienthello, |
| 3146 |
forbid_parse_serverhello, |
| 3147 |
ignore_parse_clienthello, |
| 3148 |
dont_add_serverhello, |
| 3149 |
}, |
| 3150 |
{ |
| 3151 |
TLSEXT_TYPE_cookie, |
| 3152 |
ext_cookie_add_clienthello, |
| 3153 |
forbid_parse_serverhello, |
| 3154 |
ignore_parse_clienthello, |
| 3155 |
dont_add_serverhello, |
| 3156 |
}, |
| 3157 |
{ |
| 3158 |
TLSEXT_TYPE_quic_transport_parameters, |
| 3159 |
ext_quic_transport_params_add_clienthello, |
| 3160 |
ext_quic_transport_params_parse_serverhello, |
| 3161 |
ext_quic_transport_params_parse_clienthello, |
| 3162 |
ext_quic_transport_params_add_serverhello, |
| 3163 |
}, |
| 3164 |
{ |
| 3165 |
TLSEXT_TYPE_quic_transport_parameters_legacy, |
| 3166 |
ext_quic_transport_params_add_clienthello_legacy, |
| 3167 |
ext_quic_transport_params_parse_serverhello_legacy, |
| 3168 |
ext_quic_transport_params_parse_clienthello_legacy, |
| 3169 |
ext_quic_transport_params_add_serverhello_legacy, |
| 3170 |
}, |
| 3171 |
{ |
| 3172 |
TLSEXT_TYPE_cert_compression, |
| 3173 |
cert_compression_add_clienthello, |
| 3174 |
cert_compression_parse_serverhello, |
| 3175 |
cert_compression_parse_clienthello, |
| 3176 |
cert_compression_add_serverhello, |
| 3177 |
}, |
| 3178 |
{ |
| 3179 |
TLSEXT_TYPE_delegated_credential, |
| 3180 |
ext_delegated_credential_add_clienthello, |
| 3181 |
forbid_parse_serverhello, |
| 3182 |
ext_delegated_credential_parse_clienthello, |
| 3183 |
dont_add_serverhello, |
| 3184 |
}, |
| 3185 |
{ |
| 3186 |
TLSEXT_TYPE_application_settings, |
| 3187 |
ext_alps_add_clienthello, |
| 3188 |
ext_alps_parse_serverhello, |
| 3189 |
// ALPS is negotiated late in |ssl_negotiate_alpn|. |
| 3190 |
ignore_parse_clienthello, |
| 3191 |
ext_alps_add_serverhello, |
| 3192 |
}, |
| 3193 |
{ |
| 3194 |
TLSEXT_TYPE_application_settings_old, |
| 3195 |
ext_alps_add_clienthello_old, |
| 3196 |
ext_alps_parse_serverhello_old, |
| 3197 |
// ALPS is negotiated late in |ssl_negotiate_alpn|. |
| 3198 |
ignore_parse_clienthello, |
| 3199 |
ext_alps_add_serverhello_old, |
| 3200 |
}, |
| 3201 |
}; |
| 3202 |
|
| 3203 |
#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension)) |
| 3204 |
|
| 3205 |
static_assert(kNumExtensions <= |
| 3206 |
sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8, |
| 3207 |
"too many extensions for sent bitset"); |
| 3208 |
static_assert(kNumExtensions <= |
| 3209 |
sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8, |
| 3210 |
"too many extensions for received bitset"); |
| 3211 |
|
| 3212 |
bool ssl_setup_extension_permutation(SSL_HANDSHAKE *hs) { |
| 3213 |
if (!hs->config->permute_extensions) { |
| 3214 |
return true; |
| 3215 |
} |
| 3216 |
|
| 3217 |
static_assert(kNumExtensions <= UINT8_MAX, |
| 3218 |
"extensions_permutation type is too small"); |
| 3219 |
uint32_t seeds[kNumExtensions - 1]; |
| 3220 |
Array<uint8_t> permutation; |
| 3221 |
if (!RAND_bytes(reinterpret_cast<uint8_t *>(seeds), sizeof(seeds)) || |
| 3222 |
!permutation.Init(kNumExtensions)) { |
| 3223 |
return false; |
| 3224 |
} |
| 3225 |
for (size_t i = 0; i < kNumExtensions; i++) { |
| 3226 |
permutation[i] = i; |
| 3227 |
} |
| 3228 |
for (size_t i = kNumExtensions - 1; i > 0; i--) { |
| 3229 |
// Set element |i| to a randomly-selected element 0 <= j <= i. |
| 3230 |
std::swap(permutation[i], permutation[seeds[i - 1] % (i + 1)]); |
| 3231 |
} |
| 3232 |
hs->extension_permutation = std::move(permutation); |
| 3233 |
return true; |
| 3234 |
} |
| 3235 |
|
| 3236 |
static const struct tls_extension *tls_extension_find(uint32_t *out_index, |
| 3237 |
uint16_t value) { |
| 3238 |
unsigned i; |
| 3239 |
for (i = 0; i < kNumExtensions; i++) { |
| 3240 |
if (kExtensions[i].value == value) { |
| 3241 |
*out_index = i; |
| 3242 |
return &kExtensions[i]; |
| 3243 |
} |
| 3244 |
} |
| 3245 |
|
| 3246 |
return NULL; |
| 3247 |
} |
| 3248 |
|
| 3249 |
static bool add_padding_extension(CBB *cbb, uint16_t ext, size_t len) { |
| 3250 |
CBB child; |
| 3251 |
if (!CBB_add_u16(cbb, ext) || // |
| 3252 |
!CBB_add_u16_length_prefixed(cbb, &child) || |
| 3253 |
!CBB_add_zeros(&child, len)) { |
| 3254 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 3255 |
return false; |
| 3256 |
} |
| 3257 |
return CBB_flush(cbb); |
| 3258 |
} |
| 3259 |
|
| 3260 |
static bool ssl_add_clienthello_tlsext_inner(SSL_HANDSHAKE *hs, CBB *out, |
| 3261 |
CBB *out_encoded, |
| 3262 |
bool *out_needs_psk_binder) { |
| 3263 |
// When writing ClientHelloInner, we construct the real and encoded |
| 3264 |
// ClientHellos concurrently, to handle compression. Uncompressed extensions |
| 3265 |
// are written to |extensions| and copied to |extensions_encoded|. Compressed |
| 3266 |
// extensions are buffered in |compressed| and written to the end. (ECH can |
| 3267 |
// only compress continguous extensions.) |
| 3268 |
SSL *const ssl = hs->ssl; |
| 3269 |
bssl::ScopedCBB compressed, outer_extensions; |
| 3270 |
CBB extensions, extensions_encoded; |
| 3271 |
if (!CBB_add_u16_length_prefixed(out, &extensions) || |
| 3272 |
!CBB_add_u16_length_prefixed(out_encoded, &extensions_encoded) || |
| 3273 |
!CBB_init(compressed.get(), 64) || |
| 3274 |
!CBB_init(outer_extensions.get(), 64)) { |
| 3275 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 3276 |
return false; |
| 3277 |
} |
| 3278 |
|
| 3279 |
hs->inner_extensions_sent = 0; |
| 3280 |
|
| 3281 |
if (ssl->ctx->grease_enabled) { |
| 3282 |
// Add a fake empty extension. See RFC 8701. This always matches |
| 3283 |
// |ssl_add_clienthello_tlsext|, so compress it. |
| 3284 |
uint16_t grease_ext = ssl_get_grease_value(hs, ssl_grease_extension1); |
| 3285 |
if (!add_padding_extension(compressed.get(), grease_ext, 0) || |
| 3286 |
!CBB_add_u16(outer_extensions.get(), grease_ext)) { |
| 3287 |
return false; |
| 3288 |
} |
| 3289 |
} |
| 3290 |
|
| 3291 |
for (size_t unpermuted = 0; unpermuted < kNumExtensions; unpermuted++) { |
| 3292 |
size_t i = hs->extension_permutation.empty() |
| 3293 |
? unpermuted |
| 3294 |
: hs->extension_permutation[unpermuted]; |
| 3295 |
const size_t len_before = CBB_len(&extensions); |
| 3296 |
const size_t len_compressed_before = CBB_len(compressed.get()); |
| 3297 |
if (!kExtensions[i].add_clienthello(hs, &extensions, compressed.get(), |
| 3298 |
ssl_client_hello_inner)) { |
| 3299 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
| 3300 |
ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
| 3301 |
return false; |
| 3302 |
} |
| 3303 |
|
| 3304 |
const size_t bytes_written = CBB_len(&extensions) - len_before; |
| 3305 |
const size_t bytes_written_compressed = |
| 3306 |
CBB_len(compressed.get()) - len_compressed_before; |
| 3307 |
// The callback may write to at most one output. |
| 3308 |
assert(bytes_written == 0 || bytes_written_compressed == 0); |
| 3309 |
if (bytes_written != 0 || bytes_written_compressed != 0) { |
| 3310 |
hs->inner_extensions_sent |= (1u << i); |
| 3311 |
} |
| 3312 |
// If compressed, update the running ech_outer_extensions extension. |
| 3313 |
if (bytes_written_compressed != 0 && |
| 3314 |
!CBB_add_u16(outer_extensions.get(), kExtensions[i].value)) { |
| 3315 |
return false; |
| 3316 |
} |
| 3317 |
} |
| 3318 |
|
| 3319 |
if (ssl->ctx->grease_enabled) { |
| 3320 |
// Add a fake non-empty extension. See RFC 8701. This always matches |
| 3321 |
// |ssl_add_clienthello_tlsext|, so compress it. |
| 3322 |
uint16_t grease_ext = ssl_get_grease_value(hs, ssl_grease_extension2); |
| 3323 |
if (!add_padding_extension(compressed.get(), grease_ext, 1) || |
| 3324 |
!CBB_add_u16(outer_extensions.get(), grease_ext)) { |
| 3325 |
return false; |
| 3326 |
} |
| 3327 |
} |
| 3328 |
|
| 3329 |
// Uncompressed extensions are encoded as-is. |
| 3330 |
if (!CBB_add_bytes(&extensions_encoded, CBB_data(&extensions), |
| 3331 |
CBB_len(&extensions))) { |
| 3332 |
return false; |
| 3333 |
} |
| 3334 |
|
| 3335 |
// Flush all the compressed extensions. |
| 3336 |
if (CBB_len(compressed.get()) != 0) { |
| 3337 |
CBB extension, child; |
| 3338 |
// Copy them as-is in the real ClientHelloInner. |
| 3339 |
if (!CBB_add_bytes(&extensions, CBB_data(compressed.get()), |
| 3340 |
CBB_len(compressed.get())) || |
| 3341 |
// Replace with ech_outer_extensions in the encoded form. |
| 3342 |
!CBB_add_u16(&extensions_encoded, TLSEXT_TYPE_ech_outer_extensions) || |
| 3343 |
!CBB_add_u16_length_prefixed(&extensions_encoded, &extension) || |
| 3344 |
!CBB_add_u8_length_prefixed(&extension, &child) || |
| 3345 |
!CBB_add_bytes(&child, CBB_data(outer_extensions.get()), |
| 3346 |
CBB_len(outer_extensions.get())) || |
| 3347 |
!CBB_flush(&extensions_encoded)) { |
| 3348 |
return false; |
| 3349 |
} |
| 3350 |
} |
| 3351 |
|
| 3352 |
// The PSK extension must be last. It is never compressed. Note, if there is a |
| 3353 |
// binder, the caller will need to update both ClientHelloInner and |
| 3354 |
// EncodedClientHelloInner after computing it. |
| 3355 |
const size_t len_before = CBB_len(&extensions); |
| 3356 |
if (!ext_pre_shared_key_add_clienthello(hs, &extensions, out_needs_psk_binder, |
| 3357 |
ssl_client_hello_inner) || |
| 3358 |
!CBB_add_bytes(&extensions_encoded, CBB_data(&extensions) + len_before, |
| 3359 |
CBB_len(&extensions) - len_before) || |
| 3360 |
!CBB_flush(out) || // |
| 3361 |
!CBB_flush(out_encoded)) { |
| 3362 |
return false; |
| 3363 |
} |
| 3364 |
|
| 3365 |
return true; |
| 3366 |
} |
| 3367 |
|
| 3368 |
bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, CBB *out_encoded, |
| 3369 |
bool *out_needs_psk_binder, |
| 3370 |
ssl_client_hello_type_t type, |
| 3371 |
size_t header_len) { |
| 3372 |
*out_needs_psk_binder = false; |
| 3373 |
|
| 3374 |
if (type == ssl_client_hello_inner) { |
| 3375 |
return ssl_add_clienthello_tlsext_inner(hs, out, out_encoded, |
| 3376 |
out_needs_psk_binder); |
| 3377 |
} |
| 3378 |
|
| 3379 |
assert(out_encoded == nullptr); // Only ClientHelloInner needs two outputs. |
| 3380 |
SSL *const ssl = hs->ssl; |
| 3381 |
CBB extensions; |
| 3382 |
if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
| 3383 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 3384 |
return false; |
| 3385 |
} |
| 3386 |
|
| 3387 |
// Note we may send multiple ClientHellos for DTLS HelloVerifyRequest and TLS |
| 3388 |
// 1.3 HelloRetryRequest. For the latter, the extensions may change, so it is |
| 3389 |
// important to reset this value. |
| 3390 |
hs->extensions.sent = 0; |
| 3391 |
hs->custom_extensions.sent = 0; |
| 3392 |
|
| 3393 |
// Add a fake empty extension. See RFC 8701. |
| 3394 |
if (ssl->ctx->grease_enabled && |
| 3395 |
!add_padding_extension( |
| 3396 |
&extensions, ssl_get_grease_value(hs, ssl_grease_extension1), 0)) { |
| 3397 |
return false; |
| 3398 |
} |
| 3399 |
|
| 3400 |
bool last_was_empty = false; |
| 3401 |
for (size_t unpermuted = 0; unpermuted < kNumExtensions; unpermuted++) { |
| 3402 |
size_t i = hs->extension_permutation.empty() |
| 3403 |
? unpermuted |
| 3404 |
: hs->extension_permutation[unpermuted]; |
| 3405 |
const size_t len_before = CBB_len(&extensions); |
| 3406 |
if (!kExtensions[i].add_clienthello(hs, &extensions, &extensions, type)) { |
| 3407 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
| 3408 |
ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
| 3409 |
return false; |
| 3410 |
} |
| 3411 |
|
| 3412 |
const size_t bytes_written = CBB_len(&extensions) - len_before; |
| 3413 |
if (bytes_written != 0) { |
| 3414 |
hs->extensions.sent |= (1u << i); |
| 3415 |
} |
| 3416 |
// If the difference in lengths is only four bytes then the extension had |
| 3417 |
// an empty body. |
| 3418 |
last_was_empty = (bytes_written == 4); |
| 3419 |
} |
| 3420 |
|
| 3421 |
if (!custom_ext_add_clienthello(hs, &extensions)) { |
| 3422 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 3423 |
return 0; |
| 3424 |
} |
| 3425 |
|
| 3426 |
if (ssl->ctx->grease_enabled) { |
| 3427 |
// Add a fake non-empty extension. See RFC 8701. |
| 3428 |
if (!add_padding_extension( |
| 3429 |
&extensions, ssl_get_grease_value(hs, ssl_grease_extension2), 1)) { |
| 3430 |
return false; |
| 3431 |
} |
| 3432 |
last_was_empty = false; |
| 3433 |
} |
| 3434 |
|
| 3435 |
// In cleartext ClientHellos, we add the padding extension to work around |
| 3436 |
// bugs. We also apply this padding to ClientHelloOuter, to keep the wire |
| 3437 |
// images aligned. |
| 3438 |
size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs, type); |
| 3439 |
if (!SSL_is_dtls(ssl) && !ssl->quic_method && |
| 3440 |
!ssl->s3->used_hello_retry_request) { |
| 3441 |
header_len += |
| 3442 |
SSL3_HM_HEADER_LENGTH + 2 + CBB_len(&extensions) + psk_extension_len; |
| 3443 |
size_t padding_len = 0; |
| 3444 |
|
| 3445 |
// The final extension must be non-empty. WebSphere Application |
| 3446 |
// Server 7.0 is intolerant to the last extension being zero-length. See |
| 3447 |
// https://crbug.com/363583. |
| 3448 |
if (last_was_empty && psk_extension_len == 0) { |
| 3449 |
padding_len = 1; |
| 3450 |
// The addition of the padding extension may push us into the F5 bug. |
| 3451 |
header_len += 4 + padding_len; |
| 3452 |
} |
| 3453 |
|
| 3454 |
// Add padding to workaround bugs in F5 terminators. See RFC 7685. |
| 3455 |
// |
| 3456 |
// NB: because this code works out the length of all existing extensions |
| 3457 |
// it MUST always appear last (save for any PSK extension). |
| 3458 |
if (header_len > 0xff && header_len < 0x200) { |
| 3459 |
// If our calculations already included a padding extension, remove that |
| 3460 |
// factor because we're about to change its length. |
| 3461 |
if (padding_len != 0) { |
| 3462 |
header_len -= 4 + padding_len; |
| 3463 |
} |
| 3464 |
padding_len = 0x200 - header_len; |
| 3465 |
// Extensions take at least four bytes to encode. Always include at least |
| 3466 |
// one byte of data if including the extension. WebSphere Application |
| 3467 |
// Server 7.0 is intolerant to the last extension being zero-length. See |
| 3468 |
// https://crbug.com/363583. |
| 3469 |
if (padding_len >= 4 + 1) { |
| 3470 |
padding_len -= 4; |
| 3471 |
} else { |
| 3472 |
padding_len = 1; |
| 3473 |
} |
| 3474 |
} |
| 3475 |
|
| 3476 |
if (padding_len != 0 && |
| 3477 |
!add_padding_extension(&extensions, TLSEXT_TYPE_padding, padding_len)) { |
| 3478 |
return false; |
| 3479 |
} |
| 3480 |
} |
| 3481 |
|
| 3482 |
// The PSK extension must be last, including after the padding. |
| 3483 |
const size_t len_before = CBB_len(&extensions); |
| 3484 |
if (!ext_pre_shared_key_add_clienthello(hs, &extensions, out_needs_psk_binder, |
| 3485 |
type)) { |
| 3486 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 3487 |
return false; |
| 3488 |
} |
| 3489 |
assert(psk_extension_len == CBB_len(&extensions) - len_before); |
| 3490 |
(void)len_before; // |assert| is omitted in release builds. |
| 3491 |
|
| 3492 |
// Discard empty extensions blocks. |
| 3493 |
if (CBB_len(&extensions) == 0) { |
| 3494 |
CBB_discard_child(out); |
| 3495 |
} |
| 3496 |
|
| 3497 |
return CBB_flush(out); |
| 3498 |
} |
| 3499 |
|
| 3500 |
bool ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) { |
| 3501 |
SSL *const ssl = hs->ssl; |
| 3502 |
CBB extensions; |
| 3503 |
if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
| 3504 |
goto err; |
| 3505 |
} |
| 3506 |
|
| 3507 |
for (unsigned i = 0; i < kNumExtensions; i++) { |
| 3508 |
if (!(hs->extensions.received & (1u << i))) { |
| 3509 |
// Don't send extensions that were not received. |
| 3510 |
continue; |
| 3511 |
} |
| 3512 |
|
| 3513 |
if (!kExtensions[i].add_serverhello(hs, &extensions)) { |
| 3514 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
| 3515 |
ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
| 3516 |
goto err; |
| 3517 |
} |
| 3518 |
} |
| 3519 |
|
| 3520 |
if (!custom_ext_add_serverhello(hs, &extensions)) { |
| 3521 |
goto err; |
| 3522 |
} |
| 3523 |
|
| 3524 |
// Discard empty extensions blocks before TLS 1.3. |
| 3525 |
if (ssl_protocol_version(ssl) < TLS1_3_VERSION && |
| 3526 |
CBB_len(&extensions) == 0) { |
| 3527 |
CBB_discard_child(out); |
| 3528 |
} |
| 3529 |
|
| 3530 |
return CBB_flush(out); |
| 3531 |
|
| 3532 |
err: |
| 3533 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 3534 |
return false; |
| 3535 |
} |
| 3536 |
|
| 3537 |
static bool ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs, |
| 3538 |
const SSL_CLIENT_HELLO *client_hello, |
| 3539 |
int *out_alert) { |
| 3540 |
hs->extensions.received = 0; |
| 3541 |
hs->custom_extensions.received = 0; |
| 3542 |
CBS extensions; |
| 3543 |
CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len); |
| 3544 |
while (CBS_len(&extensions) != 0) { |
| 3545 |
uint16_t type; |
| 3546 |
CBS extension; |
| 3547 |
|
| 3548 |
// Decode the next extension. |
| 3549 |
if (!CBS_get_u16(&extensions, &type) || |
| 3550 |
!CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 3551 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 3552 |
return false; |
| 3553 |
} |
| 3554 |
|
| 3555 |
unsigned ext_index; |
| 3556 |
const struct tls_extension *const ext = |
| 3557 |
tls_extension_find(&ext_index, type); |
| 3558 |
if (ext == NULL) { |
| 3559 |
if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) { |
| 3560 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
| 3561 |
return 0; |
| 3562 |
} |
| 3563 |
continue; |
| 3564 |
} |
| 3565 |
|
| 3566 |
hs->extensions.received |= (1u << ext_index); |
| 3567 |
uint8_t alert = SSL_AD_DECODE_ERROR; |
| 3568 |
if (!ext->parse_clienthello(hs, &alert, &extension)) { |
| 3569 |
*out_alert = alert; |
| 3570 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
| 3571 |
ERR_add_error_dataf("extension %u", (unsigned)type); |
| 3572 |
return false; |
| 3573 |
} |
| 3574 |
} |
| 3575 |
|
| 3576 |
for (size_t i = 0; i < kNumExtensions; i++) { |
| 3577 |
if (hs->extensions.received & (1u << i)) { |
| 3578 |
continue; |
| 3579 |
} |
| 3580 |
|
| 3581 |
CBS *contents = NULL, fake_contents; |
| 3582 |
static const uint8_t kFakeRenegotiateExtension[] = {0}; |
| 3583 |
if (kExtensions[i].value == TLSEXT_TYPE_renegotiate && |
| 3584 |
ssl_client_cipher_list_contains_cipher(client_hello, |
| 3585 |
SSL3_CK_SCSV & 0xffff)) { |
| 3586 |
// The renegotiation SCSV was received so pretend that we received a |
| 3587 |
// renegotiation extension. |
| 3588 |
CBS_init(&fake_contents, kFakeRenegotiateExtension, |
| 3589 |
sizeof(kFakeRenegotiateExtension)); |
| 3590 |
contents = &fake_contents; |
| 3591 |
hs->extensions.received |= (1u << i); |
| 3592 |
} |
| 3593 |
|
| 3594 |
// Extension wasn't observed so call the callback with a NULL |
| 3595 |
// parameter. |
| 3596 |
uint8_t alert = SSL_AD_DECODE_ERROR; |
| 3597 |
if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) { |
| 3598 |
OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
| 3599 |
ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
| 3600 |
*out_alert = alert; |
| 3601 |
return false; |
| 3602 |
} |
| 3603 |
} |
| 3604 |
|
| 3605 |
return true; |
| 3606 |
} |
| 3607 |
|
| 3608 |
bool ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs, |
| 3609 |
const SSL_CLIENT_HELLO *client_hello) { |
| 3610 |
SSL *const ssl = hs->ssl; |
| 3611 |
int alert = SSL_AD_DECODE_ERROR; |
| 3612 |
if (!ssl_scan_clienthello_tlsext(hs, client_hello, &alert)) { |
| 3613 |
ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 3614 |
return false; |
| 3615 |
} |
| 3616 |
|
| 3617 |
if (!ssl_check_clienthello_tlsext(hs)) { |
| 3618 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT); |
| 3619 |
return false; |
| 3620 |
} |
| 3621 |
|
| 3622 |
return true; |
| 3623 |
} |
| 3624 |
|
| 3625 |
static bool ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, const CBS *cbs, |
| 3626 |
int *out_alert) { |
| 3627 |
CBS extensions = *cbs; |
| 3628 |
if (!tls1_check_duplicate_extensions(&extensions)) { |
| 3629 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 3630 |
return false; |
| 3631 |
} |
| 3632 |
|
| 3633 |
uint32_t received = 0; |
| 3634 |
while (CBS_len(&extensions) != 0) { |
| 3635 |
uint16_t type; |
| 3636 |
CBS extension; |
| 3637 |
|
| 3638 |
// Decode the next extension. |
| 3639 |
if (!CBS_get_u16(&extensions, &type) || |
| 3640 |
!CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 3641 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 3642 |
return false; |
| 3643 |
} |
| 3644 |
|
| 3645 |
unsigned ext_index; |
| 3646 |
const struct tls_extension *const ext = |
| 3647 |
tls_extension_find(&ext_index, type); |
| 3648 |
|
| 3649 |
if (ext == NULL) { |
| 3650 |
hs->received_custom_extension = true; |
| 3651 |
if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) { |
| 3652 |
return 0; |
| 3653 |
} |
| 3654 |
continue; |
| 3655 |
} |
| 3656 |
|
| 3657 |
static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8, |
| 3658 |
"too many bits"); |
| 3659 |
|
| 3660 |
if (!(hs->extensions.sent & (1u << ext_index))) { |
| 3661 |
// If the extension was never sent then it is illegal. |
| 3662 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 3663 |
ERR_add_error_dataf("extension :%u", (unsigned)type); |
| 3664 |
*out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 3665 |
return false; |
| 3666 |
} |
| 3667 |
|
| 3668 |
received |= (1u << ext_index); |
| 3669 |
|
| 3670 |
uint8_t alert = SSL_AD_DECODE_ERROR; |
| 3671 |
if (!ext->parse_serverhello(hs, &alert, &extension)) { |
| 3672 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
| 3673 |
ERR_add_error_dataf("extension %u", (unsigned)type); |
| 3674 |
*out_alert = alert; |
| 3675 |
return false; |
| 3676 |
} |
| 3677 |
} |
| 3678 |
|
| 3679 |
for (size_t i = 0; i < kNumExtensions; i++) { |
| 3680 |
if (!(received & (1u << i))) { |
| 3681 |
// Extension wasn't observed so call the callback with a NULL |
| 3682 |
// parameter. |
| 3683 |
uint8_t alert = SSL_AD_DECODE_ERROR; |
| 3684 |
if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) { |
| 3685 |
OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
| 3686 |
ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
| 3687 |
*out_alert = alert; |
| 3688 |
return false; |
| 3689 |
} |
| 3690 |
} |
| 3691 |
} |
| 3692 |
|
| 3693 |
return true; |
| 3694 |
} |
| 3695 |
|
| 3696 |
static bool ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) { |
| 3697 |
SSL *const ssl = hs->ssl; |
| 3698 |
int ret = SSL_TLSEXT_ERR_NOACK; |
| 3699 |
int al = SSL_AD_UNRECOGNIZED_NAME; |
| 3700 |
if (ssl->ctx->servername_callback != 0) { |
| 3701 |
ret = ssl->ctx->servername_callback(ssl, &al, ssl->ctx->servername_arg); |
| 3702 |
} else if (ssl->session_ctx->servername_callback != 0) { |
| 3703 |
ret = ssl->session_ctx->servername_callback( |
| 3704 |
ssl, &al, ssl->session_ctx->servername_arg); |
| 3705 |
} |
| 3706 |
|
| 3707 |
switch (ret) { |
| 3708 |
case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 3709 |
ssl_send_alert(ssl, SSL3_AL_FATAL, al); |
| 3710 |
return false; |
| 3711 |
|
| 3712 |
case SSL_TLSEXT_ERR_NOACK: |
| 3713 |
hs->should_ack_sni = false; |
| 3714 |
return true; |
| 3715 |
|
| 3716 |
default: |
| 3717 |
return true; |
| 3718 |
} |
| 3719 |
} |
| 3720 |
|
| 3721 |
static bool ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs) { |
| 3722 |
SSL *const ssl = hs->ssl; |
| 3723 |
// ALPS and ALPN have a dependency between each other, so we defer checking |
| 3724 |
// consistency to after the callbacks run. |
| 3725 |
if (hs->new_session != nullptr && hs->new_session->has_application_settings) { |
| 3726 |
// ALPN must be negotiated. |
| 3727 |
if (ssl->s3->alpn_selected.empty()) { |
| 3728 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_ALPS_WITHOUT_ALPN); |
| 3729 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 3730 |
return false; |
| 3731 |
} |
| 3732 |
|
| 3733 |
// The negotiated protocol must be one of the ones we advertised for ALPS. |
| 3734 |
Span<const uint8_t> settings; |
| 3735 |
if (!ssl_get_local_application_settings(hs, &settings, |
| 3736 |
ssl->s3->alpn_selected)) { |
| 3737 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL); |
| 3738 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 3739 |
return false; |
| 3740 |
} |
| 3741 |
|
| 3742 |
if (!hs->new_session->local_application_settings.CopyFrom(settings)) { |
| 3743 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 3744 |
return false; |
| 3745 |
} |
| 3746 |
} |
| 3747 |
|
| 3748 |
return true; |
| 3749 |
} |
| 3750 |
|
| 3751 |
bool ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, const CBS *cbs) { |
| 3752 |
SSL *const ssl = hs->ssl; |
| 3753 |
int alert = SSL_AD_DECODE_ERROR; |
| 3754 |
if (!ssl_scan_serverhello_tlsext(hs, cbs, &alert)) { |
| 3755 |
ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 3756 |
return false; |
| 3757 |
} |
| 3758 |
|
| 3759 |
if (!ssl_check_serverhello_tlsext(hs)) { |
| 3760 |
return false; |
| 3761 |
} |
| 3762 |
|
| 3763 |
return true; |
| 3764 |
} |
| 3765 |
|
| 3766 |
static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx( |
| 3767 |
Array<uint8_t> *out, EVP_CIPHER_CTX *cipher_ctx, HMAC_CTX *hmac_ctx, |
| 3768 |
Span<const uint8_t> ticket) { |
| 3769 |
size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx); |
| 3770 |
|
| 3771 |
// Check the MAC at the end of the ticket. |
| 3772 |
uint8_t mac[EVP_MAX_MD_SIZE]; |
| 3773 |
size_t mac_len = HMAC_size(hmac_ctx); |
| 3774 |
if (ticket.size() < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) { |
| 3775 |
// The ticket must be large enough for key name, IV, data, and MAC. |
| 3776 |
return ssl_ticket_aead_ignore_ticket; |
| 3777 |
} |
| 3778 |
// Split the ticket into the ticket and the MAC. |
| 3779 |
auto ticket_mac = ticket.last(mac_len); |
| 3780 |
ticket = ticket.first(ticket.size() - mac_len); |
| 3781 |
HMAC_Update(hmac_ctx, ticket.data(), ticket.size()); |
| 3782 |
HMAC_Final(hmac_ctx, mac, NULL); |
| 3783 |
assert(mac_len == ticket_mac.size()); |
| 3784 |
bool mac_ok = CRYPTO_memcmp(mac, ticket_mac.data(), mac_len) == 0; |
| 3785 |
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 3786 |
mac_ok = true; |
| 3787 |
#endif |
| 3788 |
if (!mac_ok) { |
| 3789 |
return ssl_ticket_aead_ignore_ticket; |
| 3790 |
} |
| 3791 |
|
| 3792 |
// Decrypt the session data. |
| 3793 |
auto ciphertext = ticket.subspan(SSL_TICKET_KEY_NAME_LEN + iv_len); |
| 3794 |
Array<uint8_t> plaintext; |
| 3795 |
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 3796 |
if (!plaintext.CopyFrom(ciphertext)) { |
| 3797 |
return ssl_ticket_aead_error; |
| 3798 |
} |
| 3799 |
#else |
| 3800 |
if (ciphertext.size() >= INT_MAX) { |
| 3801 |
return ssl_ticket_aead_ignore_ticket; |
| 3802 |
} |
| 3803 |
if (!plaintext.Init(ciphertext.size())) { |
| 3804 |
return ssl_ticket_aead_error; |
| 3805 |
} |
| 3806 |
int len1, len2; |
| 3807 |
if (!EVP_DecryptUpdate(cipher_ctx, plaintext.data(), &len1, ciphertext.data(), |
| 3808 |
(int)ciphertext.size()) || |
| 3809 |
!EVP_DecryptFinal_ex(cipher_ctx, plaintext.data() + len1, &len2)) { |
| 3810 |
ERR_clear_error(); |
| 3811 |
return ssl_ticket_aead_ignore_ticket; |
| 3812 |
} |
| 3813 |
plaintext.Shrink(static_cast<size_t>(len1) + len2); |
| 3814 |
#endif |
| 3815 |
|
| 3816 |
*out = std::move(plaintext); |
| 3817 |
return ssl_ticket_aead_success; |
| 3818 |
} |
| 3819 |
|
| 3820 |
static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb( |
| 3821 |
SSL_HANDSHAKE *hs, Array<uint8_t> *out, bool *out_renew_ticket, |
| 3822 |
Span<const uint8_t> ticket) { |
| 3823 |
assert(ticket.size() >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH); |
| 3824 |
ScopedEVP_CIPHER_CTX cipher_ctx; |
| 3825 |
ScopedHMAC_CTX hmac_ctx; |
| 3826 |
auto name = ticket.subspan(0, SSL_TICKET_KEY_NAME_LEN); |
| 3827 |
// The actual IV is shorter, but the length is determined by the callback's |
| 3828 |
// chosen cipher. Instead we pass in |EVP_MAX_IV_LENGTH| worth of IV to ensure |
| 3829 |
// the callback has enough. |
| 3830 |
auto iv = ticket.subspan(SSL_TICKET_KEY_NAME_LEN, EVP_MAX_IV_LENGTH); |
| 3831 |
int cb_ret = hs->ssl->session_ctx->ticket_key_cb( |
| 3832 |
hs->ssl, const_cast<uint8_t *>(name.data()), |
| 3833 |
const_cast<uint8_t *>(iv.data()), cipher_ctx.get(), hmac_ctx.get(), |
| 3834 |
0 /* decrypt */); |
| 3835 |
if (cb_ret < 0) { |
| 3836 |
return ssl_ticket_aead_error; |
| 3837 |
} else if (cb_ret == 0) { |
| 3838 |
return ssl_ticket_aead_ignore_ticket; |
| 3839 |
} else if (cb_ret == 2) { |
| 3840 |
*out_renew_ticket = true; |
| 3841 |
} else { |
| 3842 |
assert(cb_ret == 1); |
| 3843 |
} |
| 3844 |
return decrypt_ticket_with_cipher_ctx(out, cipher_ctx.get(), hmac_ctx.get(), |
| 3845 |
ticket); |
| 3846 |
} |
| 3847 |
|
| 3848 |
static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys( |
| 3849 |
SSL_HANDSHAKE *hs, Array<uint8_t> *out, Span<const uint8_t> ticket) { |
| 3850 |
assert(ticket.size() >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH); |
| 3851 |
SSL_CTX *ctx = hs->ssl->session_ctx.get(); |
| 3852 |
|
| 3853 |
// Rotate the ticket key if necessary. |
| 3854 |
if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) { |
| 3855 |
return ssl_ticket_aead_error; |
| 3856 |
} |
| 3857 |
|
| 3858 |
const EVP_CIPHER *cipher = EVP_aes_128_cbc(); |
| 3859 |
auto name = ticket.subspan(0, SSL_TICKET_KEY_NAME_LEN); |
| 3860 |
auto iv = |
| 3861 |
ticket.subspan(SSL_TICKET_KEY_NAME_LEN, EVP_CIPHER_iv_length(cipher)); |
| 3862 |
|
| 3863 |
// Pick the matching ticket key and decrypt. |
| 3864 |
ScopedEVP_CIPHER_CTX cipher_ctx; |
| 3865 |
ScopedHMAC_CTX hmac_ctx; |
| 3866 |
{ |
| 3867 |
MutexReadLock lock(&ctx->lock); |
| 3868 |
const TicketKey *key; |
| 3869 |
if (ctx->ticket_key_current && name == ctx->ticket_key_current->name) { |
| 3870 |
key = ctx->ticket_key_current.get(); |
| 3871 |
} else if (ctx->ticket_key_prev && name == ctx->ticket_key_prev->name) { |
| 3872 |
key = ctx->ticket_key_prev.get(); |
| 3873 |
} else { |
| 3874 |
return ssl_ticket_aead_ignore_ticket; |
| 3875 |
} |
| 3876 |
if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key), |
| 3877 |
tlsext_tick_md(), NULL) || |
| 3878 |
!EVP_DecryptInit_ex(cipher_ctx.get(), cipher, NULL, |
| 3879 |
key->aes_key, iv.data())) { |
| 3880 |
return ssl_ticket_aead_error; |
| 3881 |
} |
| 3882 |
} |
| 3883 |
return decrypt_ticket_with_cipher_ctx(out, cipher_ctx.get(), hmac_ctx.get(), |
| 3884 |
ticket); |
| 3885 |
} |
| 3886 |
|
| 3887 |
static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method( |
| 3888 |
SSL_HANDSHAKE *hs, Array<uint8_t> *out, bool *out_renew_ticket, |
| 3889 |
Span<const uint8_t> ticket) { |
| 3890 |
Array<uint8_t> plaintext; |
| 3891 |
if (!plaintext.Init(ticket.size())) { |
| 3892 |
return ssl_ticket_aead_error; |
| 3893 |
} |
| 3894 |
|
| 3895 |
size_t plaintext_len; |
| 3896 |
const enum ssl_ticket_aead_result_t result = |
| 3897 |
hs->ssl->session_ctx->ticket_aead_method->open( |
| 3898 |
hs->ssl, plaintext.data(), &plaintext_len, ticket.size(), |
| 3899 |
ticket.data(), ticket.size()); |
| 3900 |
if (result != ssl_ticket_aead_success) { |
| 3901 |
return result; |
| 3902 |
} |
| 3903 |
|
| 3904 |
plaintext.Shrink(plaintext_len); |
| 3905 |
*out = std::move(plaintext); |
| 3906 |
return ssl_ticket_aead_success; |
| 3907 |
} |
| 3908 |
|
| 3909 |
enum ssl_ticket_aead_result_t ssl_process_ticket( |
| 3910 |
SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session, |
| 3911 |
bool *out_renew_ticket, Span<const uint8_t> ticket, |
| 3912 |
Span<const uint8_t> session_id) { |
| 3913 |
SSL *const ssl = hs->ssl; |
| 3914 |
*out_renew_ticket = false; |
| 3915 |
out_session->reset(); |
| 3916 |
|
| 3917 |
if ((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) || |
| 3918 |
session_id.size() > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3919 |
return ssl_ticket_aead_ignore_ticket; |
| 3920 |
} |
| 3921 |
|
| 3922 |
// Tickets in TLS 1.3 are tied into pre-shared keys (PSKs), unlike in TLS 1.2 |
| 3923 |
// where that concept doesn't exist. The |decrypted_psk| and |ignore_psk| |
| 3924 |
// hints only apply to PSKs. We check the version to determine which this is. |
| 3925 |
const bool is_psk = ssl_protocol_version(ssl) >= TLS1_3_VERSION; |
| 3926 |
|
| 3927 |
Array<uint8_t> plaintext; |
| 3928 |
enum ssl_ticket_aead_result_t result; |
| 3929 |
SSL_HANDSHAKE_HINTS *const hints = hs->hints.get(); |
| 3930 |
if (is_psk && hints && !hs->hints_requested && |
| 3931 |
!hints->decrypted_psk.empty()) { |
| 3932 |
result = plaintext.CopyFrom(hints->decrypted_psk) ? ssl_ticket_aead_success |
| 3933 |
: ssl_ticket_aead_error; |
| 3934 |
} else if (is_psk && hints && !hs->hints_requested && hints->ignore_psk) { |
| 3935 |
result = ssl_ticket_aead_ignore_ticket; |
| 3936 |
} else if (!is_psk && hints && !hs->hints_requested && |
| 3937 |
!hints->decrypted_ticket.empty()) { |
| 3938 |
if (plaintext.CopyFrom(hints->decrypted_ticket)) { |
| 3939 |
result = ssl_ticket_aead_success; |
| 3940 |
*out_renew_ticket = hints->renew_ticket; |
| 3941 |
} else { |
| 3942 |
result = ssl_ticket_aead_error; |
| 3943 |
} |
| 3944 |
} else if (!is_psk && hints && !hs->hints_requested && hints->ignore_ticket) { |
| 3945 |
result = ssl_ticket_aead_ignore_ticket; |
| 3946 |
} else if (ssl->session_ctx->ticket_aead_method != NULL) { |
| 3947 |
result = ssl_decrypt_ticket_with_method(hs, &plaintext, out_renew_ticket, |
| 3948 |
ticket); |
| 3949 |
} else { |
| 3950 |
// Ensure there is room for the key name and the largest IV |ticket_key_cb| |
| 3951 |
// may try to consume. The real limit may be lower, but the maximum IV |
| 3952 |
// length should be well under the minimum size for the session material and |
| 3953 |
// HMAC. |
| 3954 |
if (ticket.size() < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) { |
| 3955 |
result = ssl_ticket_aead_ignore_ticket; |
| 3956 |
} else if (ssl->session_ctx->ticket_key_cb != NULL) { |
| 3957 |
result = |
| 3958 |
ssl_decrypt_ticket_with_cb(hs, &plaintext, out_renew_ticket, ticket); |
| 3959 |
} else { |
| 3960 |
result = ssl_decrypt_ticket_with_ticket_keys(hs, &plaintext, ticket); |
| 3961 |
} |
| 3962 |
} |
| 3963 |
|
| 3964 |
if (hints && hs->hints_requested) { |
| 3965 |
if (result == ssl_ticket_aead_ignore_ticket) { |
| 3966 |
if (is_psk) { |
| 3967 |
hints->ignore_psk = true; |
| 3968 |
} else { |
| 3969 |
hints->ignore_ticket = true; |
| 3970 |
} |
| 3971 |
} else if (result == ssl_ticket_aead_success) { |
| 3972 |
if (is_psk) { |
| 3973 |
if (!hints->decrypted_psk.CopyFrom(plaintext)) { |
| 3974 |
return ssl_ticket_aead_error; |
| 3975 |
} |
| 3976 |
} else { |
| 3977 |
if (!hints->decrypted_ticket.CopyFrom(plaintext)) { |
| 3978 |
return ssl_ticket_aead_error; |
| 3979 |
} |
| 3980 |
hints->renew_ticket = *out_renew_ticket; |
| 3981 |
} |
| 3982 |
} |
| 3983 |
} |
| 3984 |
|
| 3985 |
if (result != ssl_ticket_aead_success) { |
| 3986 |
return result; |
| 3987 |
} |
| 3988 |
|
| 3989 |
// Decode the session. |
| 3990 |
UniquePtr<SSL_SESSION> session(SSL_SESSION_from_bytes( |
| 3991 |
plaintext.data(), plaintext.size(), ssl->ctx.get())); |
| 3992 |
if (!session) { |
| 3993 |
ERR_clear_error(); // Don't leave an error on the queue. |
| 3994 |
return ssl_ticket_aead_ignore_ticket; |
| 3995 |
} |
| 3996 |
|
| 3997 |
// Envoy's tests expect the session to have a session ID that matches the |
| 3998 |
// placeholder used by the client. It's unclear whether this is a good idea, |
| 3999 |
// but we maintain it for now. |
| 4000 |
SHA256(ticket.data(), ticket.size(), session->session_id); |
| 4001 |
// Other consumers may expect a non-empty session ID to indicate resumption. |
| 4002 |
session->session_id_length = SHA256_DIGEST_LENGTH; |
| 4003 |
|
| 4004 |
// Ticket-based session resumption bypasses the session cache, but counts as |
| 4005 |
// session reuse, so update the session's "session hit" counter. |
| 4006 |
ssl_update_counter(ssl->session_ctx.get(), ssl->session_ctx->stats.sess_hit, |
| 4007 |
true); |
| 4008 |
|
| 4009 |
*out_session = std::move(session); |
| 4010 |
return ssl_ticket_aead_success; |
| 4011 |
} |
| 4012 |
|
| 4013 |
bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) { |
| 4014 |
// Extension ignored for inappropriate versions |
| 4015 |
if (ssl_protocol_version(hs->ssl) < TLS1_2_VERSION) { |
| 4016 |
return true; |
| 4017 |
} |
| 4018 |
|
| 4019 |
// In all contexts, the signature algorithms list may not be empty. (It may be |
| 4020 |
// omitted by clients in TLS 1.2, but then the entire extension is omitted.) |
| 4021 |
return CBS_len(in_sigalgs) != 0 && |
| 4022 |
parse_u16_array(in_sigalgs, &hs->peer_sigalgs); |
| 4023 |
} |
| 4024 |
|
| 4025 |
bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) { |
| 4026 |
switch (EVP_PKEY_id(pkey)) { |
| 4027 |
case EVP_PKEY_RSA: |
| 4028 |
*out = SSL_SIGN_RSA_PKCS1_MD5_SHA1; |
| 4029 |
return true; |
| 4030 |
case EVP_PKEY_EC: |
| 4031 |
*out = SSL_SIGN_ECDSA_SHA1; |
| 4032 |
return true; |
| 4033 |
default: |
| 4034 |
return false; |
| 4035 |
} |
| 4036 |
} |
| 4037 |
|
| 4038 |
bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) { |
| 4039 |
SSL *const ssl = hs->ssl; |
| 4040 |
CERT *cert = hs->config->cert.get(); |
| 4041 |
DC *dc = cert->dc.get(); |
| 4042 |
|
| 4043 |
// Before TLS 1.2, the signature algorithm isn't negotiated as part of the |
| 4044 |
// handshake. |
| 4045 |
if (ssl_protocol_version(ssl) < TLS1_2_VERSION) { |
| 4046 |
if (ssl_public_key_supports_legacy_signature_algorithm(out, hs) || |
| 4047 |
ssl_cert_private_keys_supports_legacy_signature_algorithm(out, hs)) { |
| 4048 |
return true; |
| 4049 |
} |
| 4050 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS); |
| 4051 |
return false; |
| 4052 |
} |
| 4053 |
|
| 4054 |
Span<const uint16_t> sigalgs = kSignSignatureAlgorithms; |
| 4055 |
if (ssl_signing_with_dc(hs)) { |
| 4056 |
sigalgs = MakeConstSpan(&dc->dc_cert_verify_algorithm, 1); |
| 4057 |
} else if (!cert->sigalgs.empty()) { |
| 4058 |
sigalgs = cert->sigalgs; |
| 4059 |
} |
| 4060 |
|
| 4061 |
Span<const uint16_t> peer_sigalgs = tls1_get_peer_verify_algorithms(hs); |
| 4062 |
|
| 4063 |
for (uint16_t sigalg : sigalgs) { |
| 4064 |
// We check the extracted public key for support first. If not, we go |
| 4065 |
// through our available private keys to check for support. |
| 4066 |
if (ssl_public_key_supports_signature_algorithm(hs, sigalg) || |
| 4067 |
ssl_cert_private_keys_supports_signature_algorithm(hs, sigalg)) { |
| 4068 |
// Check if peer supports negotiated signature algorithms. |
| 4069 |
for (uint16_t peer_sigalg : peer_sigalgs) { |
| 4070 |
if (sigalg == peer_sigalg) { |
| 4071 |
*out = sigalg; |
| 4072 |
return true; |
| 4073 |
} |
| 4074 |
} |
| 4075 |
} |
| 4076 |
} |
| 4077 |
|
| 4078 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS); |
| 4079 |
return false; |
| 4080 |
} |
| 4081 |
|
| 4082 |
bool tls1_call_ocsp_stapling_callback(SSL_HANDSHAKE *hs) { |
| 4083 |
SSL *const ssl = hs->ssl; |
| 4084 |
|
| 4085 |
if (hs->ocsp_stapling_requested && |
| 4086 |
ssl->ctx->legacy_ocsp_callback != nullptr) { |
| 4087 |
switch (ssl->ctx->legacy_ocsp_callback( |
| 4088 |
ssl, ssl->ctx->legacy_ocsp_callback_arg)) { |
| 4089 |
case SSL_TLSEXT_ERR_OK: |
| 4090 |
break; |
| 4091 |
case SSL_TLSEXT_ERR_NOACK: |
| 4092 |
hs->ocsp_stapling_requested = false; |
| 4093 |
break; |
| 4094 |
default: |
| 4095 |
OPENSSL_PUT_ERROR(SSL, SSL_R_OCSP_CB_ERROR); |
| 4096 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 4097 |
return false; |
| 4098 |
} |
| 4099 |
} |
| 4100 |
|
| 4101 |
return true; |
| 4102 |
} |
| 4103 |
|
| 4104 |
Span<const uint16_t> tls1_get_peer_verify_algorithms(const SSL_HANDSHAKE *hs) { |
| 4105 |
Span<const uint16_t> peer_sigalgs = hs->peer_sigalgs; |
| 4106 |
if (peer_sigalgs.empty() && ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) { |
| 4107 |
// If the client didn't specify any signature_algorithms extension then |
| 4108 |
// we can assume that it supports SHA1. See |
| 4109 |
// http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 |
| 4110 |
static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1, |
| 4111 |
SSL_SIGN_ECDSA_SHA1}; |
| 4112 |
peer_sigalgs = kDefaultPeerAlgorithms; |
| 4113 |
} |
| 4114 |
return peer_sigalgs; |
| 4115 |
} |
| 4116 |
|
| 4117 |
bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) { |
| 4118 |
SSL *const ssl = hs->ssl; |
| 4119 |
// A Channel ID handshake message is structured to contain multiple |
| 4120 |
// extensions, but the only one that can be present is Channel ID. |
| 4121 |
uint16_t extension_type; |
| 4122 |
CBS channel_id = msg.body, extension; |
| 4123 |
if (!CBS_get_u16(&channel_id, &extension_type) || |
| 4124 |
!CBS_get_u16_length_prefixed(&channel_id, &extension) || |
| 4125 |
CBS_len(&channel_id) != 0 || |
| 4126 |
extension_type != TLSEXT_TYPE_channel_id || |
| 4127 |
CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) { |
| 4128 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 4129 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 4130 |
return false; |
| 4131 |
} |
| 4132 |
|
| 4133 |
const EC_GROUP *p256 = EC_group_p256(); |
| 4134 |
UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new()); |
| 4135 |
UniquePtr<BIGNUM> x(BN_new()), y(BN_new()); |
| 4136 |
if (!sig || !x || !y) { |
| 4137 |
return false; |
| 4138 |
} |
| 4139 |
|
| 4140 |
const uint8_t *p = CBS_data(&extension); |
| 4141 |
if (BN_bin2bn(p + 0, 32, x.get()) == NULL || |
| 4142 |
BN_bin2bn(p + 32, 32, y.get()) == NULL || |
| 4143 |
BN_bin2bn(p + 64, 32, sig->r) == NULL || |
| 4144 |
BN_bin2bn(p + 96, 32, sig->s) == NULL) { |
| 4145 |
return false; |
| 4146 |
} |
| 4147 |
|
| 4148 |
UniquePtr<EC_KEY> key(EC_KEY_new()); |
| 4149 |
UniquePtr<EC_POINT> point(EC_POINT_new(p256)); |
| 4150 |
if (!key || !point || |
| 4151 |
!EC_POINT_set_affine_coordinates_GFp(p256, point.get(), x.get(), y.get(), |
| 4152 |
nullptr) || |
| 4153 |
!EC_KEY_set_group(key.get(), p256) || |
| 4154 |
!EC_KEY_set_public_key(key.get(), point.get())) { |
| 4155 |
return false; |
| 4156 |
} |
| 4157 |
|
| 4158 |
uint8_t digest[EVP_MAX_MD_SIZE]; |
| 4159 |
size_t digest_len; |
| 4160 |
if (!tls1_channel_id_hash(hs, digest, &digest_len)) { |
| 4161 |
return false; |
| 4162 |
} |
| 4163 |
|
| 4164 |
bool sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get()); |
| 4165 |
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 4166 |
sig_ok = true; |
| 4167 |
ERR_clear_error(); |
| 4168 |
#endif |
| 4169 |
if (!sig_ok) { |
| 4170 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID); |
| 4171 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
| 4172 |
return false; |
| 4173 |
} |
| 4174 |
|
| 4175 |
OPENSSL_memcpy(ssl->s3->channel_id, p, 64); |
| 4176 |
ssl->s3->channel_id_valid = true; |
| 4177 |
return true; |
| 4178 |
} |
| 4179 |
|
| 4180 |
bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) { |
| 4181 |
uint8_t digest[EVP_MAX_MD_SIZE]; |
| 4182 |
size_t digest_len; |
| 4183 |
if (!tls1_channel_id_hash(hs, digest, &digest_len)) { |
| 4184 |
return false; |
| 4185 |
} |
| 4186 |
|
| 4187 |
EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(hs->config->channel_id_private.get()); |
| 4188 |
if (ec_key == nullptr) { |
| 4189 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 4190 |
return false; |
| 4191 |
} |
| 4192 |
|
| 4193 |
UniquePtr<BIGNUM> x(BN_new()), y(BN_new()); |
| 4194 |
if (!x || !y || |
| 4195 |
!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key), |
| 4196 |
EC_KEY_get0_public_key(ec_key), |
| 4197 |
x.get(), y.get(), nullptr)) { |
| 4198 |
return false; |
| 4199 |
} |
| 4200 |
|
| 4201 |
UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign(digest, digest_len, ec_key)); |
| 4202 |
if (!sig) { |
| 4203 |
return false; |
| 4204 |
} |
| 4205 |
|
| 4206 |
CBB child; |
| 4207 |
if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) || |
| 4208 |
!CBB_add_u16_length_prefixed(cbb, &child) || |
| 4209 |
!BN_bn2cbb_padded(&child, 32, x.get()) || |
| 4210 |
!BN_bn2cbb_padded(&child, 32, y.get()) || |
| 4211 |
!BN_bn2cbb_padded(&child, 32, sig->r) || |
| 4212 |
!BN_bn2cbb_padded(&child, 32, sig->s) || |
| 4213 |
!CBB_flush(cbb)) { |
| 4214 |
return false; |
| 4215 |
} |
| 4216 |
|
| 4217 |
return true; |
| 4218 |
} |
| 4219 |
|
| 4220 |
bool tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) { |
| 4221 |
SSL *const ssl = hs->ssl; |
| 4222 |
if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 4223 |
Array<uint8_t> msg; |
| 4224 |
if (!tls13_get_cert_verify_signature_input(hs, &msg, |
| 4225 |
ssl_cert_verify_channel_id)) { |
| 4226 |
return false; |
| 4227 |
} |
| 4228 |
SHA256(msg.data(), msg.size(), out); |
| 4229 |
*out_len = SHA256_DIGEST_LENGTH; |
| 4230 |
return true; |
| 4231 |
} |
| 4232 |
|
| 4233 |
SHA256_CTX ctx; |
| 4234 |
|
| 4235 |
SHA256_Init(&ctx); |
| 4236 |
static const char kClientIDMagic[] = "TLS Channel ID signature"; |
| 4237 |
SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic)); |
| 4238 |
|
| 4239 |
if (ssl->session != NULL) { |
| 4240 |
static const char kResumptionMagic[] = "Resumption"; |
| 4241 |
SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic)); |
| 4242 |
if (ssl->session->original_handshake_hash_len == 0) { |
| 4243 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 4244 |
return false; |
| 4245 |
} |
| 4246 |
SHA256_Update(&ctx, ssl->session->original_handshake_hash, |
| 4247 |
ssl->session->original_handshake_hash_len); |
| 4248 |
} |
| 4249 |
|
| 4250 |
uint8_t hs_hash[EVP_MAX_MD_SIZE]; |
| 4251 |
size_t hs_hash_len; |
| 4252 |
if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) { |
| 4253 |
return false; |
| 4254 |
} |
| 4255 |
SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len); |
| 4256 |
SHA256_Final(out, &ctx); |
| 4257 |
*out_len = SHA256_DIGEST_LENGTH; |
| 4258 |
return true; |
| 4259 |
} |
| 4260 |
|
| 4261 |
bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) { |
| 4262 |
SSL *const ssl = hs->ssl; |
| 4263 |
// This function should never be called for a resumed session because the |
| 4264 |
// handshake hashes that we wish to record are for the original, full |
| 4265 |
// handshake. |
| 4266 |
if (ssl->session != NULL) { |
| 4267 |
return false; |
| 4268 |
} |
| 4269 |
|
| 4270 |
static_assert( |
| 4271 |
sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE, |
| 4272 |
"original_handshake_hash is too small"); |
| 4273 |
|
| 4274 |
size_t digest_len; |
| 4275 |
if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash, |
| 4276 |
&digest_len)) { |
| 4277 |
return false; |
| 4278 |
} |
| 4279 |
|
| 4280 |
static_assert(EVP_MAX_MD_SIZE <= 0xff, |
| 4281 |
"EVP_MAX_MD_SIZE does not fit in uint8_t"); |
| 4282 |
hs->new_session->original_handshake_hash_len = (uint8_t)digest_len; |
| 4283 |
|
| 4284 |
return true; |
| 4285 |
} |
| 4286 |
|
| 4287 |
bool ssl_is_sct_list_valid(const CBS *contents) { |
| 4288 |
// Shallow parse the SCT list for sanity. By the RFC |
| 4289 |
// (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any |
| 4290 |
// of the SCTs may be empty. |
| 4291 |
CBS copy = *contents; |
| 4292 |
CBS sct_list; |
| 4293 |
if (!CBS_get_u16_length_prefixed(©, &sct_list) || |
| 4294 |
CBS_len(©) != 0 || |
| 4295 |
CBS_len(&sct_list) == 0) { |
| 4296 |
return false; |
| 4297 |
} |
| 4298 |
|
| 4299 |
while (CBS_len(&sct_list) > 0) { |
| 4300 |
CBS sct; |
| 4301 |
if (!CBS_get_u16_length_prefixed(&sct_list, &sct) || |
| 4302 |
CBS_len(&sct) == 0) { |
| 4303 |
return false; |
| 4304 |
} |
| 4305 |
} |
| 4306 |
|
| 4307 |
return true; |
| 4308 |
} |
| 4309 |
|
| 4310 |
BSSL_NAMESPACE_END |
| 4311 |
|
| 4312 |
using namespace bssl; |
| 4313 |
|
| 4314 |
int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello, |
| 4315 |
uint16_t extension_type, |
| 4316 |
const uint8_t **out_data, |
| 4317 |
size_t *out_len) { |
| 4318 |
CBS cbs; |
| 4319 |
if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) { |
| 4320 |
return 0; |
| 4321 |
} |
| 4322 |
|
| 4323 |
*out_data = CBS_data(&cbs); |
| 4324 |
*out_len = CBS_len(&cbs); |
| 4325 |
return 1; |
| 4326 |
} |
| 4327 |
|
| 4328 |
int SSL_extension_supported(unsigned extension_value) { |
| 4329 |
uint32_t index; |
| 4330 |
return extension_value == TLSEXT_TYPE_padding || |
| 4331 |
tls_extension_find(&index, extension_value) != NULL; |
| 4332 |
} |