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