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