blob: 67f18fdfcf8459ea29186cf0e7ed747364fa47b4 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
Adam Langleye745b252018-02-26 14:02:17 -08007 *
Adam Langley95c29f32014-06-20 12:00:00 -07008 * 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 (tjh@cryptsoft.com).
Adam Langleye745b252018-02-26 14:02:17 -080014 *
Adam Langley95c29f32014-06-20 12:00:00 -070015 * 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.
Adam Langleye745b252018-02-26 14:02:17 -080021 *
Adam Langley95c29f32014-06-20 12:00:00 -070022 * 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 (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
Adam Langleye745b252018-02-26 14:02:17 -080036 * 4. If you include any Windows specific code (or a derivative thereof) from
Adam Langley95c29f32014-06-20 12:00:00 -070037 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Adam Langleye745b252018-02-26 14:02:17 -080039 *
Adam Langley95c29f32014-06-20 12:00:00 -070040 * 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.
Adam Langleye745b252018-02-26 14:02:17 -080051 *
Adam Langley95c29f32014-06-20 12:00:00 -070052 * 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
Adam Langleye745b252018-02-26 14:02:17 -080065 * notice, this list of conditions and the following disclaimer.
Adam Langley95c29f32014-06-20 12:00:00 -070066 *
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 * openssl-core@openssl.org.
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 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
Dan McArdle1920c6f2020-03-11 17:29:40 -0400116#include <algorithm>
David Benjamin499742c2017-07-22 12:45:38 -0400117#include <utility>
118
Dan McArdle1920c6f2020-03-11 17:29:40 -0400119#include <openssl/aead.h>
David Benjamin03973092014-06-24 23:27:17 -0400120#include <openssl/bytestring.h>
Adam Langley512a2892017-12-30 08:04:39 -0800121#include <openssl/chacha.h>
Dan McArdle1920c6f2020-03-11 17:29:40 -0400122#include <openssl/curve25519.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400123#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400124#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125#include <openssl/evp.h>
126#include <openssl/hmac.h>
David Benjamin070a6c32021-05-05 15:39:27 -0400127#include <openssl/hpke.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700128#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400129#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700130#include <openssl/rand.h>
131
Steven Valdezcb966542016-08-17 16:56:14 -0400132#include "../crypto/internal.h"
Steven Valdez51607f12020-08-05 10:46:05 -0400133#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800134
135
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700136BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -0400137
David Benjamin861abcc2018-07-14 17:40:26 -0400138static bool ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
Steven Valdez51607f12020-08-05 10:46:05 -0400139static bool ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs);
Adam Langley95c29f32014-06-20 12:00:00 -0700140
Adam Langleyfcf25832014-12-18 17:42:32 -0800141static 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}
David Benjamin35a7a442014-07-05 00:23:20 -0400152
David Benjaminc11ea9422017-08-29 16:33:21 -0400153// 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.
David Benjamin861abcc2018-07-14 17:40:26 -0400157static bool tls1_check_duplicate_extensions(const CBS *cbs) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400158 // First pass: count the extensions.
David Benjamin08f5c762017-09-21 02:43:05 -0400159 size_t num_extensions = 0;
160 CBS extensions = *cbs;
Adam Langleyfcf25832014-12-18 17:42:32 -0800161 while (CBS_len(&extensions) > 0) {
162 uint16_t type;
163 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400164
Adam Langleyfcf25832014-12-18 17:42:32 -0800165 if (!CBS_get_u16(&extensions, &type) ||
166 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
David Benjamin861abcc2018-07-14 17:40:26 -0400167 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 }
David Benjamin35a7a442014-07-05 00:23:20 -0400169
Adam Langleyfcf25832014-12-18 17:42:32 -0800170 num_extensions++;
171 }
David Benjamin35a7a442014-07-05 00:23:20 -0400172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 if (num_extensions == 0) {
David Benjamin861abcc2018-07-14 17:40:26 -0400174 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800175 }
David Benjamin9a373592014-07-25 04:27:53 -0400176
David Benjamin08f5c762017-09-21 02:43:05 -0400177 Array<uint16_t> extension_types;
178 if (!extension_types.Init(num_extensions)) {
David Benjamin861abcc2018-07-14 17:40:26 -0400179 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800180 }
David Benjamin35a7a442014-07-05 00:23:20 -0400181
David Benjaminc11ea9422017-08-29 16:33:21 -0400182 // Second pass: gather the extension types.
Adam Langleyfcf25832014-12-18 17:42:32 -0800183 extensions = *cbs;
David Benjamin08f5c762017-09-21 02:43:05 -0400184 for (size_t i = 0; i < extension_types.size(); i++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800185 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400186
Adam Langleyfcf25832014-12-18 17:42:32 -0800187 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
188 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400189 // This should not happen.
David Benjamin861abcc2018-07-14 17:40:26 -0400190 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 }
192 }
193 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400194
David Benjaminc11ea9422017-08-29 16:33:21 -0400195 // Sort the extensions and make sure there are no duplicates.
David Benjamin08f5c762017-09-21 02:43:05 -0400196 qsort(extension_types.data(), extension_types.size(), sizeof(uint16_t),
197 compare_uint16_t);
198 for (size_t i = 1; i < num_extensions; i++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 if (extension_types[i - 1] == extension_types[i]) {
David Benjamin861abcc2018-07-14 17:40:26 -0400200 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800201 }
202 }
David Benjamin35a7a442014-07-05 00:23:20 -0400203
David Benjamin861abcc2018-07-14 17:40:26 -0400204 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800205}
David Benjamin35a7a442014-07-05 00:23:20 -0400206
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +0000207static bool is_post_quantum_group(uint16_t id) {
Adam Langley7f028812019-10-18 14:48:11 -0700208 return id == SSL_CURVE_CECPQ2;
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +0000209}
210
Adam Langleyc9827e02019-04-12 14:46:50 -0700211bool ssl_client_hello_init(const SSL *ssl, SSL_CLIENT_HELLO *out,
Daniel McArdle00e434d2021-02-18 11:47:18 -0500212 Span<const uint8_t> body) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500213 OPENSSL_memset(out, 0, sizeof(*out));
Adam Langleyc9827e02019-04-12 14:46:50 -0700214 out->ssl = const_cast<SSL *>(ssl);
Daniel McArdle00e434d2021-02-18 11:47:18 -0500215 out->client_hello = body.data();
216 out->client_hello_len = body.size();
David Benjamin8f2c20e2014-07-09 09:30:38 -0400217
David Benjamine14ff062016-08-09 16:21:24 -0400218 CBS client_hello, random, session_id;
David Benjamin731058e2016-12-03 23:15:13 -0500219 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
220 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjamine14ff062016-08-09 16:21:24 -0400221 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
222 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
223 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
David Benjamin861abcc2018-07-14 17:40:26 -0400224 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800225 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700226
David Benjamin731058e2016-12-03 23:15:13 -0500227 out->random = CBS_data(&random);
228 out->random_len = CBS_len(&random);
229 out->session_id = CBS_data(&session_id);
230 out->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700231
David Benjaminc11ea9422017-08-29 16:33:21 -0400232 // Skip past DTLS cookie
David Benjamin731058e2016-12-03 23:15:13 -0500233 if (SSL_is_dtls(out->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800234 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400235 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
236 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
David Benjamin861abcc2018-07-14 17:40:26 -0400237 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800238 }
239 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700240
David Benjamine14ff062016-08-09 16:21:24 -0400241 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400243 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
244 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 CBS_len(&compression_methods) < 1) {
David Benjamin861abcc2018-07-14 17:40:26 -0400246 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 }
David Benjamine14ff062016-08-09 16:21:24 -0400248
David Benjamin731058e2016-12-03 23:15:13 -0500249 out->cipher_suites = CBS_data(&cipher_suites);
250 out->cipher_suites_len = CBS_len(&cipher_suites);
251 out->compression_methods = CBS_data(&compression_methods);
252 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700253
David Benjaminc11ea9422017-08-29 16:33:21 -0400254 // If the ClientHello ends here then it's valid, but doesn't have any
David Benjamin9bb15f52018-06-26 00:07:40 -0400255 // extensions.
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 if (CBS_len(&client_hello) == 0) {
David Benjamin731058e2016-12-03 23:15:13 -0500257 out->extensions = NULL;
258 out->extensions_len = 0;
David Benjamin861abcc2018-07-14 17:40:26 -0400259 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700261
David Benjaminc11ea9422017-08-29 16:33:21 -0400262 // Extract extensions and check it is valid.
David Benjamine14ff062016-08-09 16:21:24 -0400263 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
265 !tls1_check_duplicate_extensions(&extensions) ||
266 CBS_len(&client_hello) != 0) {
David Benjamin861abcc2018-07-14 17:40:26 -0400267 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 }
David Benjamine14ff062016-08-09 16:21:24 -0400269
David Benjamin731058e2016-12-03 23:15:13 -0500270 out->extensions = CBS_data(&extensions);
271 out->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700272
David Benjamin861abcc2018-07-14 17:40:26 -0400273 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800274}
Adam Langleydc9b1412014-06-20 12:00:00 -0700275
David Benjamin861abcc2018-07-14 17:40:26 -0400276bool ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
277 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 CBS extensions;
David Benjamin731058e2016-12-03 23:15:13 -0500279 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 while (CBS_len(&extensions) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400281 // Decode the next extension.
Adam Langleyfcf25832014-12-18 17:42:32 -0800282 uint16_t type;
283 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 if (!CBS_get_u16(&extensions, &type) ||
285 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
David Benjamin861abcc2018-07-14 17:40:26 -0400286 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800287 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700288
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400290 *out = extension;
David Benjamin861abcc2018-07-14 17:40:26 -0400291 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800292 }
293 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700294
David Benjamin861abcc2018-07-14 17:40:26 -0400295 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800296}
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Steven Valdezce902a92016-05-17 11:47:53 -0400298static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400299 SSL_CURVE_X25519,
300 SSL_CURVE_SECP256R1,
301 SSL_CURVE_SECP384R1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800302};
Adam Langley95c29f32014-06-20 12:00:00 -0700303
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700304Span<const uint16_t> tls1_get_grouplist(const SSL_HANDSHAKE *hs) {
David Benjamin0ce090a2018-07-02 20:24:40 -0400305 if (!hs->config->supported_group_list.empty()) {
306 return hs->config->supported_group_list;
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 }
David Benjamincf0ce672017-09-21 02:25:59 -0400308 return Span<const uint16_t>(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800309}
David Benjamined439582014-07-14 19:13:02 -0400310
David Benjamin861abcc2018-07-14 17:40:26 -0400311bool tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900312 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400313 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400314
David Benjaminc11ea9422017-08-29 16:33:21 -0400315 // Clients are not required to send a supported_groups extension. In this
316 // case, the server is free to pick any group it likes. See RFC 4492,
317 // section 4, paragraph 3.
318 //
319 // However, in the interests of compatibility, we will skip ECDH if the
320 // client didn't send an extension because we can't be sure that they'll
321 // support our favoured group. Thus we do not special-case an emtpy
322 // |peer_supported_group_list|.
David Benjamin55a43642015-04-20 14:45:55 -0400323
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700324 Span<const uint16_t> groups = tls1_get_grouplist(hs);
David Benjamincf0ce672017-09-21 02:25:59 -0400325 Span<const uint16_t> pref, supp;
David Benjamin4298d772015-12-19 00:18:25 -0500326 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400327 pref = groups;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900328 supp = hs->peer_supported_group_list;
David Benjamin55a43642015-04-20 14:45:55 -0400329 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900330 pref = hs->peer_supported_group_list;
Steven Valdezce902a92016-05-17 11:47:53 -0400331 supp = groups;
David Benjamin55a43642015-04-20 14:45:55 -0400332 }
333
David Benjamincf0ce672017-09-21 02:25:59 -0400334 for (uint16_t pref_group : pref) {
335 for (uint16_t supp_group : supp) {
Adam Langley7b935932018-11-12 13:53:42 -0800336 if (pref_group == supp_group &&
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +0000337 // CECPQ2(b) doesn't fit in the u8-length-prefixed ECPoint field in
338 // TLS 1.2 and below.
Adam Langley7b935932018-11-12 13:53:42 -0800339 (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +0000340 !is_post_quantum_group(pref_group))) {
David Benjamincf0ce672017-09-21 02:25:59 -0400341 *out_group_id = pref_group;
David Benjamin861abcc2018-07-14 17:40:26 -0400342 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800343 }
344 }
345 }
346
David Benjamin861abcc2018-07-14 17:40:26 -0400347 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800348}
Adam Langley95c29f32014-06-20 12:00:00 -0700349
David Benjamin0ce090a2018-07-02 20:24:40 -0400350bool tls1_set_curves(Array<uint16_t> *out_group_ids, Span<const int> curves) {
351 Array<uint16_t> group_ids;
352 if (!group_ids.Init(curves.size())) {
353 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 }
355
David Benjamin0ce090a2018-07-02 20:24:40 -0400356 for (size_t i = 0; i < curves.size(); i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400357 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
David Benjamin0ce090a2018-07-02 20:24:40 -0400358 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800360 }
361
David Benjamin0ce090a2018-07-02 20:24:40 -0400362 *out_group_ids = std::move(group_ids);
363 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800364}
Adam Langley95c29f32014-06-20 12:00:00 -0700365
David Benjamin0ce090a2018-07-02 20:24:40 -0400366bool tls1_set_curves_list(Array<uint16_t> *out_group_ids, const char *curves) {
367 // Count the number of curves in the list.
368 size_t count = 0;
369 const char *ptr = curves, *col;
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100370 do {
371 col = strchr(ptr, ':');
David Benjamin0ce090a2018-07-02 20:24:40 -0400372 count++;
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100373 if (col) {
374 ptr = col + 1;
375 }
376 } while (col);
377
David Benjamin0ce090a2018-07-02 20:24:40 -0400378 Array<uint16_t> group_ids;
379 if (!group_ids.Init(count)) {
380 return false;
381 }
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100382
David Benjamin0ce090a2018-07-02 20:24:40 -0400383 size_t i = 0;
384 ptr = curves;
385 do {
386 col = strchr(ptr, ':');
387 if (!ssl_name_to_group_id(&group_ids[i++], ptr,
388 col ? (size_t)(col - ptr) : strlen(ptr))) {
389 return false;
390 }
391 if (col) {
392 ptr = col + 1;
393 }
394 } while (col);
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100395
David Benjamin0ce090a2018-07-02 20:24:40 -0400396 assert(i == count);
397 *out_group_ids = std::move(group_ids);
398 return true;
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100399}
400
David Benjamin861abcc2018-07-14 17:40:26 -0400401bool tls1_check_group_id(const SSL_HANDSHAKE *hs, uint16_t group_id) {
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +0000402 if (is_post_quantum_group(group_id) &&
Adam Langley7b935932018-11-12 13:53:42 -0800403 ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) {
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +0000404 // CECPQ2(b) requires TLS 1.3.
Adam Langley7b935932018-11-12 13:53:42 -0800405 return false;
406 }
407
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700408 for (uint16_t supported : tls1_get_grouplist(hs)) {
David Benjamincf0ce672017-09-21 02:25:59 -0400409 if (supported == group_id) {
David Benjamin861abcc2018-07-14 17:40:26 -0400410 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800411 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 }
David Benjamin033e5f42014-11-13 18:47:41 -0500413
David Benjamin861abcc2018-07-14 17:40:26 -0400414 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800415}
David Benjamin033e5f42014-11-13 18:47:41 -0500416
David Benjaminc11ea9422017-08-29 16:33:21 -0400417// kVerifySignatureAlgorithms is the default list of accepted signature
418// algorithms for verifying.
David Benjamin3ef76972016-10-17 17:59:54 -0400419static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400420 // List our preferred algorithms first.
David Benjamin3a322f52016-10-26 12:45:35 -0400421 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin6879e192018-04-13 16:01:02 -0400422 SSL_SIGN_RSA_PSS_RSAE_SHA256,
David Benjamin3a322f52016-10-26 12:45:35 -0400423 SSL_SIGN_RSA_PKCS1_SHA256,
424
David Benjaminc11ea9422017-08-29 16:33:21 -0400425 // Larger hashes are acceptable.
David Benjamin3a322f52016-10-26 12:45:35 -0400426 SSL_SIGN_ECDSA_SECP384R1_SHA384,
David Benjamin6879e192018-04-13 16:01:02 -0400427 SSL_SIGN_RSA_PSS_RSAE_SHA384,
David Benjamin3a322f52016-10-26 12:45:35 -0400428 SSL_SIGN_RSA_PKCS1_SHA384,
429
David Benjamin6879e192018-04-13 16:01:02 -0400430 SSL_SIGN_RSA_PSS_RSAE_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400431 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400432
David Benjaminc11ea9422017-08-29 16:33:21 -0400433 // For now, SHA-1 is still accepted but least preferable.
David Benjamin3a322f52016-10-26 12:45:35 -0400434 SSL_SIGN_RSA_PKCS1_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400435};
436
David Benjaminc11ea9422017-08-29 16:33:21 -0400437// kSignSignatureAlgorithms is the default list of supported signature
438// algorithms for signing.
David Benjamin3a322f52016-10-26 12:45:35 -0400439static const uint16_t kSignSignatureAlgorithms[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400440 // List our preferred algorithms first.
David Benjamin69522112017-03-28 15:38:29 -0500441 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400442 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin6879e192018-04-13 16:01:02 -0400443 SSL_SIGN_RSA_PSS_RSAE_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400444 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400445
David Benjaminc11ea9422017-08-29 16:33:21 -0400446 // If needed, sign larger hashes.
447 //
448 // TODO(davidben): Determine which of these may be pruned.
David Benjamin3a322f52016-10-26 12:45:35 -0400449 SSL_SIGN_ECDSA_SECP384R1_SHA384,
David Benjamin6879e192018-04-13 16:01:02 -0400450 SSL_SIGN_RSA_PSS_RSAE_SHA384,
David Benjamin3a322f52016-10-26 12:45:35 -0400451 SSL_SIGN_RSA_PKCS1_SHA384,
452
453 SSL_SIGN_ECDSA_SECP521R1_SHA512,
David Benjamin6879e192018-04-13 16:01:02 -0400454 SSL_SIGN_RSA_PSS_RSAE_SHA512,
David Benjamin3a322f52016-10-26 12:45:35 -0400455 SSL_SIGN_RSA_PKCS1_SHA512,
456
David Benjaminc11ea9422017-08-29 16:33:21 -0400457 // If the peer supports nothing else, sign with SHA-1.
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400458 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400459 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400460};
461
David Benjamin17669352020-02-05 17:33:36 -0500462static Span<const uint16_t> tls12_get_verify_sigalgs(const SSL_HANDSHAKE *hs) {
463 if (hs->config->verify_sigalgs.empty()) {
464 return Span<const uint16_t>(kVerifySignatureAlgorithms);
David Benjamin71c21b42017-04-14 17:05:40 -0400465 }
David Benjamin17669352020-02-05 17:33:36 -0500466 return hs->config->verify_sigalgs;
David Benjamine28552d2018-04-08 13:59:25 -0400467}
468
David Benjaminebad5082020-02-03 19:32:19 -0500469bool tls12_add_verify_sigalgs(const SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin17669352020-02-05 17:33:36 -0500470 for (uint16_t sigalg : tls12_get_verify_sigalgs(hs)) {
David Benjamin610cdbb2018-01-22 19:08:38 -0500471 if (!CBB_add_u16(out, sigalg)) {
472 return false;
David Benjamin69522112017-03-28 15:38:29 -0500473 }
474 }
David Benjamin610cdbb2018-01-22 19:08:38 -0500475 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800476}
Adam Langley95c29f32014-06-20 12:00:00 -0700477
David Benjaminebad5082020-02-03 19:32:19 -0500478bool tls12_check_peer_sigalg(const SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin610cdbb2018-01-22 19:08:38 -0500479 uint16_t sigalg) {
David Benjamin17669352020-02-05 17:33:36 -0500480 for (uint16_t verify_sigalg : tls12_get_verify_sigalgs(hs)) {
David Benjamine28552d2018-04-08 13:59:25 -0400481 if (verify_sigalg == sigalg) {
David Benjamin610cdbb2018-01-22 19:08:38 -0500482 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800483 }
484 }
485
David Benjamin3ef76972016-10-17 17:59:54 -0400486 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
487 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin610cdbb2018-01-22 19:08:38 -0500488 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800489}
490
David Benjaminc11ea9422017-08-29 16:33:21 -0400491// tls_extension represents a TLS extension that is handled internally. The
492// |init| function is called for each handshake, before any other functions of
493// the extension. Then the add and parse callbacks are called as needed.
494//
495// The parse callbacks receive a |CBS| that contains the contents of the
496// extension (i.e. not including the type and length bytes). If an extension is
497// not received then the parse callbacks will be called with a NULL CBS so that
498// they can do any processing needed to handle the absence of an extension.
499//
500// The add callbacks receive a |CBB| to which the extension can be appended but
501// the function is responsible for appending the type and length bytes too.
502//
David Benjamin31640932017-10-11 13:22:39 -0400503// All callbacks return true for success and false for error. If a parse
504// function returns zero then a fatal alert with value |*out_alert| will be
505// sent. If |*out_alert| isn't set, then a |decode_error| alert will be sent.
Adam Langley614c66a2015-06-12 15:26:58 -0700506struct tls_extension {
507 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500508 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700509
David Benjamin31640932017-10-11 13:22:39 -0400510 bool (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
511 bool (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
512 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700513
David Benjamin31640932017-10-11 13:22:39 -0400514 bool (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
515 CBS *contents);
516 bool (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700517};
518
David Benjamin31640932017-10-11 13:22:39 -0400519static bool forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Steven Valdez51607f12020-08-05 10:46:05 -0400520 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400521 if (contents != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400522 // Servers MUST NOT send this extension.
Steven Valdez6b8509a2016-07-12 13:38:32 -0400523 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
524 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
David Benjamin31640932017-10-11 13:22:39 -0400525 return false;
Steven Valdez6b8509a2016-07-12 13:38:32 -0400526 }
527
David Benjamin31640932017-10-11 13:22:39 -0400528 return true;
Steven Valdez6b8509a2016-07-12 13:38:32 -0400529}
530
David Benjamin31640932017-10-11 13:22:39 -0400531static bool ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Steven Valdez51607f12020-08-05 10:46:05 -0400532 CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400533 // This extension from the client is handled elsewhere.
David Benjamin31640932017-10-11 13:22:39 -0400534 return true;
Steven Valdez6b8509a2016-07-12 13:38:32 -0400535}
536
David Benjamin31640932017-10-11 13:22:39 -0400537static bool dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
538 return true;
Steven Valdez6b8509a2016-07-12 13:38:32 -0400539}
Adam Langley614c66a2015-06-12 15:26:58 -0700540
David Benjaminc11ea9422017-08-29 16:33:21 -0400541// Server name indication (SNI).
542//
543// https://tools.ietf.org/html/rfc6066#section-3.
Adam Langley614c66a2015-06-12 15:26:58 -0700544
David Benjamin31640932017-10-11 13:22:39 -0400545static bool ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500546 SSL *const ssl = hs->ssl;
David Benjamin46853762018-07-03 14:01:26 -0400547 if (ssl->hostname == nullptr) {
David Benjamin31640932017-10-11 13:22:39 -0400548 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700549 }
550
551 CBB contents, server_name_list, name;
552 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
553 !CBB_add_u16_length_prefixed(out, &contents) ||
554 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
555 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
556 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
David Benjamin46853762018-07-03 14:01:26 -0400557 !CBB_add_bytes(&name, (const uint8_t *)ssl->hostname.get(),
558 strlen(ssl->hostname.get())) ||
Adam Langley614c66a2015-06-12 15:26:58 -0700559 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -0400560 return false;
Adam Langley614c66a2015-06-12 15:26:58 -0700561 }
562
David Benjamin31640932017-10-11 13:22:39 -0400563 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700564}
565
David Benjamin31640932017-10-11 13:22:39 -0400566static bool ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
567 CBS *contents) {
David Benjamina8614602017-09-06 15:40:19 -0400568 // The server may acknowledge SNI with an empty extension. We check the syntax
569 // but otherwise ignore this signal.
570 return contents == NULL || CBS_len(contents) == 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700571}
572
David Benjamin31640932017-10-11 13:22:39 -0400573static bool ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
574 CBS *contents) {
David Benjaminef0183c2019-07-20 09:11:05 -0400575 // SNI has already been parsed earlier in the handshake. See |extract_sni|.
David Benjamin31640932017-10-11 13:22:39 -0400576 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700577}
578
David Benjamin31640932017-10-11 13:22:39 -0400579static bool ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500580 if (hs->ssl->s3->session_reused ||
581 !hs->should_ack_sni) {
David Benjamin31640932017-10-11 13:22:39 -0400582 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700583 }
584
585 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
586 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -0400587 return false;
Adam Langley614c66a2015-06-12 15:26:58 -0700588 }
589
David Benjamin31640932017-10-11 13:22:39 -0400590 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700591}
592
593
Daniel McArdle00e434d2021-02-18 11:47:18 -0500594// Encrypted ClientHello (ECH)
Dan McArdle1920c6f2020-03-11 17:29:40 -0400595//
David Benjamin49ee62f2021-05-20 10:34:59 -0400596// https://tools.ietf.org/html/draft-ietf-tls-esni-10
Dan McArdle1920c6f2020-03-11 17:29:40 -0400597
598// random_size returns a random value between |min| and |max|, inclusive.
599static size_t random_size(size_t min, size_t max) {
600 assert(min < max);
601 size_t value;
602 RAND_bytes(reinterpret_cast<uint8_t *>(&value), sizeof(value));
603 return value % (max - min + 1) + min;
604}
605
606static bool ext_ech_add_clienthello_grease(SSL_HANDSHAKE *hs, CBB *out) {
607 // If we are responding to the server's HelloRetryRequest, we repeat the bytes
608 // of the first ECH GREASE extension.
609 if (hs->ssl->s3->used_hello_retry_request) {
610 CBB ech_body;
611 if (!CBB_add_u16(out, TLSEXT_TYPE_encrypted_client_hello) ||
612 !CBB_add_u16_length_prefixed(out, &ech_body) ||
613 !CBB_add_bytes(&ech_body, hs->ech_grease.data(),
614 hs->ech_grease.size()) ||
615 !CBB_flush(out)) {
616 return false;
617 }
618 return true;
619 }
620
David Benjaminf39c81d2021-05-03 18:39:46 -0400621 const uint16_t kdf_id = EVP_HPKE_HKDF_SHA256;
622 const uint16_t aead_id = EVP_has_aes_hardware() ? EVP_HPKE_AES_128_GCM
623 : EVP_HPKE_CHACHA20_POLY1305;
624 constexpr size_t kAEADOverhead = 16; // Both AEADs have a 16-byte tag.
Steven Valdez94a63a52021-04-29 10:52:42 -0400625 uint8_t ech_config_id;
626 RAND_bytes(&ech_config_id, 1);
Dan McArdle1920c6f2020-03-11 17:29:40 -0400627
628 uint8_t ech_enc[X25519_PUBLIC_VALUE_LEN];
629 uint8_t private_key_unused[X25519_PRIVATE_KEY_LEN];
630 X25519_keypair(ech_enc, private_key_unused);
631
David Benjamin49ee62f2021-05-20 10:34:59 -0400632 // To determine a plausible length for the payload, we estimate the size of a
633 // typical EncodedClientHelloInner without resumption:
Dan McArdle1920c6f2020-03-11 17:29:40 -0400634 //
David Benjamin49ee62f2021-05-20 10:34:59 -0400635 // 2+32+1+2 version, random, legacy_session_id, legacy_compression_methods
636 // 2+4*2 cipher_suites (three TLS 1.3 ciphers, GREASE)
637 // 2 extensions prefix
638 // 4 ech_is_inner
639 // 4+1+2*2 supported_versions (TLS 1.3, GREASE)
640 // 4+1+10*2 outer_extensions (key_share, sigalgs, sct, alpn,
641 // supported_groups, status_request, psk_key_exchange_modes,
642 // compress_certificate, GREASE x2)
Dan McArdle1920c6f2020-03-11 17:29:40 -0400643 //
David Benjamin49ee62f2021-05-20 10:34:59 -0400644 // The server_name extension has an overhead of 9 bytes. For now, arbitrarily
645 // estimate maximum_name_length to be between 32 and 100 bytes.
646 //
647 // TODO(davidben): If the padding scheme changes to also round the entire
648 // payload, adjust this to match. See
649 // https://github.com/tlswg/draft-ietf-tls-esni/issues/433
650 uint8_t payload[196 + kAEADOverhead];
651 const size_t payload_len = random_size(128, 196) + kAEADOverhead;
Dan McArdle1920c6f2020-03-11 17:29:40 -0400652 assert(payload_len <= sizeof(payload));
653 RAND_bytes(payload, payload_len);
654
655 // Inside the TLS extension contents, write a serialized ClientEncryptedCH.
Steven Valdez94a63a52021-04-29 10:52:42 -0400656 CBB ech_body, enc_cbb, payload_cbb;
Dan McArdle1920c6f2020-03-11 17:29:40 -0400657 if (!CBB_add_u16(out, TLSEXT_TYPE_encrypted_client_hello) ||
658 !CBB_add_u16_length_prefixed(out, &ech_body) ||
659 !CBB_add_u16(&ech_body, kdf_id) || //
660 !CBB_add_u16(&ech_body, aead_id) ||
Steven Valdez94a63a52021-04-29 10:52:42 -0400661 !CBB_add_u8(&ech_body, ech_config_id) ||
Dan McArdle1920c6f2020-03-11 17:29:40 -0400662 !CBB_add_u16_length_prefixed(&ech_body, &enc_cbb) ||
663 !CBB_add_bytes(&enc_cbb, ech_enc, OPENSSL_ARRAY_SIZE(ech_enc)) ||
664 !CBB_add_u16_length_prefixed(&ech_body, &payload_cbb) ||
665 !CBB_add_bytes(&payload_cbb, payload, payload_len) || //
666 !CBB_flush(&ech_body)) {
667 return false;
668 }
669 // Save the bytes of the newly-generated extension in case the server sends
670 // a HelloRetryRequest.
671 if (!hs->ech_grease.CopyFrom(
672 MakeConstSpan(CBB_data(&ech_body), CBB_len(&ech_body)))) {
673 return false;
674 }
675 return CBB_flush(out);
676}
677
678static bool ext_ech_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
679 if (hs->max_version < TLS1_3_VERSION) {
680 return true;
681 }
682 if (hs->config->ech_grease_enabled) {
683 return ext_ech_add_clienthello_grease(hs, out);
684 }
685 // Nothing to do, since we don't yet implement the non-GREASE parts of ECH.
686 return true;
687}
688
689static bool ext_ech_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
690 CBS *contents) {
691 if (contents == NULL) {
692 return true;
693 }
694
695 // If the client only sent GREASE, we must check the extension syntactically.
696 CBS ech_configs;
697 if (!CBS_get_u16_length_prefixed(contents, &ech_configs) ||
698 CBS_len(&ech_configs) == 0 || //
699 CBS_len(contents) > 0) {
700 *out_alert = SSL_AD_DECODE_ERROR;
701 return false;
702 }
703 while (CBS_len(&ech_configs) > 0) {
704 // Do a top-level parse of the ECHConfig, stopping before ECHConfigContents.
705 uint16_t version;
706 CBS ech_config_contents;
707 if (!CBS_get_u16(&ech_configs, &version) ||
708 !CBS_get_u16_length_prefixed(&ech_configs, &ech_config_contents)) {
709 *out_alert = SSL_AD_DECODE_ERROR;
710 return false;
711 }
712 }
713 return true;
714}
715
Dan McArdlec2959352020-10-29 14:31:31 -0400716static bool ext_ech_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
717 CBS *contents) {
718 if (contents != nullptr) {
719 hs->ech_present = true;
720 return true;
721 }
722 return true;
723}
724
Daniel McArdle00e434d2021-02-18 11:47:18 -0500725static bool ext_ech_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
726 SSL *const ssl = hs->ssl;
727 if (ssl_protocol_version(ssl) < TLS1_3_VERSION || //
728 hs->ech_accept || //
729 hs->ech_server_config_list == nullptr) {
730 return true;
731 }
732
733 // Write the list of retry configs to |out|. Note
734 // |SSL_CTX_set1_ech_server_config_list| ensures |ech_server_config_list|
735 // contains at least one retry config.
736 CBB body, retry_configs;
737 if (!CBB_add_u16(out, TLSEXT_TYPE_encrypted_client_hello) ||
738 !CBB_add_u16_length_prefixed(out, &body) ||
739 !CBB_add_u16_length_prefixed(&body, &retry_configs)) {
740 return false;
741 }
David Benjamin1d58cd12021-05-04 15:24:24 -0400742 for (const auto &config : hs->ech_server_config_list->configs) {
743 if (!config->is_retry_config()) {
Daniel McArdle00e434d2021-02-18 11:47:18 -0500744 continue;
745 }
David Benjamin1d58cd12021-05-04 15:24:24 -0400746 if (!CBB_add_bytes(&retry_configs, config->ech_config().data(),
747 config->ech_config().size())) {
Daniel McArdle00e434d2021-02-18 11:47:18 -0500748 return false;
749 }
750 }
751 return CBB_flush(out);
752}
753
Dan McArdlec2959352020-10-29 14:31:31 -0400754static bool ext_ech_is_inner_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
755 return true;
756}
757
758static bool ext_ech_is_inner_parse_clienthello(SSL_HANDSHAKE *hs,
759 uint8_t *out_alert,
760 CBS *contents) {
761 if (contents == nullptr) {
762 return true;
763 }
764 if (CBS_len(contents) > 0) {
765 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
766 return false;
767 }
768 hs->ech_is_inner_present = true;
769 return true;
770}
771
Dan McArdle1920c6f2020-03-11 17:29:40 -0400772
David Benjaminc11ea9422017-08-29 16:33:21 -0400773// Renegotiation indication.
774//
775// https://tools.ietf.org/html/rfc5746
Adam Langley5021b222015-06-12 18:27:58 -0700776
David Benjamin31640932017-10-11 13:22:39 -0400777static bool ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500778 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400779 // Renegotiation indication is not necessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -0400780 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400781 return true;
David Benjamin7c7d8312016-08-20 13:39:03 -0400782 }
783
David Benjamin52bf6902016-10-08 12:05:03 -0400784 assert(ssl->s3->initial_handshake_complete ==
785 (ssl->s3->previous_client_finished_len != 0));
786
Adam Langley5021b222015-06-12 18:27:58 -0700787 CBB contents, prev_finished;
788 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
789 !CBB_add_u16_length_prefixed(out, &contents) ||
790 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
791 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
792 ssl->s3->previous_client_finished_len) ||
793 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -0400794 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700795 }
796
David Benjamin31640932017-10-11 13:22:39 -0400797 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700798}
799
David Benjamin31640932017-10-11 13:22:39 -0400800static bool ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
801 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500802 SSL *const ssl = hs->ssl;
David Benjamind1e3ce12017-10-06 18:31:15 -0400803 if (contents != NULL && ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez246eeee2017-03-26 12:49:17 -0500804 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -0400805 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -0400806 }
807
David Benjaminc11ea9422017-08-29 16:33:21 -0400808 // Servers may not switch between omitting the extension and supporting it.
809 // See RFC 5746, sections 3.5 and 4.2.
David Benjamin3e052de2015-11-25 20:10:31 -0500810 if (ssl->s3->initial_handshake_complete &&
811 (contents != NULL) != ssl->s3->send_connection_binding) {
812 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
813 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
David Benjamin31640932017-10-11 13:22:39 -0400814 return false;
David Benjamin3e052de2015-11-25 20:10:31 -0500815 }
816
Adam Langley5021b222015-06-12 18:27:58 -0700817 if (contents == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400818 // Strictly speaking, if we want to avoid an attack we should *always* see
819 // RI even on initial ServerHello because the client doesn't see any
820 // renegotiation during an attack. However this would mean we could not
821 // connect to any server which doesn't support RI.
822 //
823 // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
824 // practical terms every client sets it so it's just assumed here.
David Benjamin31640932017-10-11 13:22:39 -0400825 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700826 }
827
828 const size_t expected_len = ssl->s3->previous_client_finished_len +
829 ssl->s3->previous_server_finished_len;
830
David Benjaminc11ea9422017-08-29 16:33:21 -0400831 // Check for logic errors
Adam Langley5021b222015-06-12 18:27:58 -0700832 assert(!expected_len || ssl->s3->previous_client_finished_len);
833 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400834 assert(ssl->s3->initial_handshake_complete ==
835 (ssl->s3->previous_client_finished_len != 0));
836 assert(ssl->s3->initial_handshake_complete ==
837 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700838
David Benjaminc11ea9422017-08-29 16:33:21 -0400839 // Parse out the extension contents.
Adam Langley5021b222015-06-12 18:27:58 -0700840 CBS renegotiated_connection;
841 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
842 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400843 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700844 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -0400845 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700846 }
847
David Benjaminc11ea9422017-08-29 16:33:21 -0400848 // Check that the extension matches.
Adam Langley5021b222015-06-12 18:27:58 -0700849 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400850 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700851 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin31640932017-10-11 13:22:39 -0400852 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700853 }
854
855 const uint8_t *d = CBS_data(&renegotiated_connection);
David Benjamin31640932017-10-11 13:22:39 -0400856 bool ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
857 ssl->s3->previous_client_finished_len) == 0;
David Benjamin9343b0b2017-07-01 00:31:27 -0400858#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin31640932017-10-11 13:22:39 -0400859 ok = true;
David Benjamin9343b0b2017-07-01 00:31:27 -0400860#endif
861 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400862 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700863 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin31640932017-10-11 13:22:39 -0400864 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700865 }
866 d += ssl->s3->previous_client_finished_len;
867
David Benjamin9343b0b2017-07-01 00:31:27 -0400868 ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
869 ssl->s3->previous_server_finished_len) == 0;
870#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin31640932017-10-11 13:22:39 -0400871 ok = true;
David Benjamin9343b0b2017-07-01 00:31:27 -0400872#endif
873 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400874 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley10e10602017-07-25 13:33:21 -0700875 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin31640932017-10-11 13:22:39 -0400876 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700877 }
David Benjamin046bc1f2017-08-31 15:06:42 -0400878 ssl->s3->send_connection_binding = true;
Adam Langley5021b222015-06-12 18:27:58 -0700879
David Benjamin31640932017-10-11 13:22:39 -0400880 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700881}
882
David Benjamin31640932017-10-11 13:22:39 -0400883static bool ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
884 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500885 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400886 // Renegotiation isn't supported as a server so this function should never be
887 // called after the initial handshake.
Adam Langley5021b222015-06-12 18:27:58 -0700888 assert(!ssl->s3->initial_handshake_complete);
889
David Benjamind1e3ce12017-10-06 18:31:15 -0400890 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400891 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -0400892 }
893
Adam Langley5021b222015-06-12 18:27:58 -0700894 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -0400895 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700896 }
897
898 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700899 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
900 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400901 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
David Benjamin31640932017-10-11 13:22:39 -0400902 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700903 }
904
David Benjaminc11ea9422017-08-29 16:33:21 -0400905 // Check that the extension matches. We do not support renegotiation as a
906 // server, so this must be empty.
David Benjamin52bf6902016-10-08 12:05:03 -0400907 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400908 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700909 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin31640932017-10-11 13:22:39 -0400910 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700911 }
912
David Benjamin046bc1f2017-08-31 15:06:42 -0400913 ssl->s3->send_connection_binding = true;
Adam Langley5021b222015-06-12 18:27:58 -0700914
David Benjamin31640932017-10-11 13:22:39 -0400915 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700916}
917
David Benjamin31640932017-10-11 13:22:39 -0400918static bool ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500919 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400920 // Renegotiation isn't supported as a server so this function should never be
921 // called after the initial handshake.
David Benjamin52bf6902016-10-08 12:05:03 -0400922 assert(!ssl->s3->initial_handshake_complete);
923
David Benjamind1e3ce12017-10-06 18:31:15 -0400924 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400925 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -0400926 }
927
Adam Langley5021b222015-06-12 18:27:58 -0700928 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400929 !CBB_add_u16(out, 1 /* length */) ||
930 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
David Benjamin31640932017-10-11 13:22:39 -0400931 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700932 }
933
David Benjamin31640932017-10-11 13:22:39 -0400934 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700935}
936
Adam Langley0a056712015-07-01 15:03:33 -0700937
David Benjaminc11ea9422017-08-29 16:33:21 -0400938// Extended Master Secret.
939//
940// https://tools.ietf.org/html/rfc7627
Adam Langley0a056712015-07-01 15:03:33 -0700941
David Benjamin31640932017-10-11 13:22:39 -0400942static bool ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400943 // Extended master secret is not necessary in TLS 1.3.
David Benjamin9bb15f52018-06-26 00:07:40 -0400944 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400945 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700946 }
947
948 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
949 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -0400950 return false;
Adam Langley0a056712015-07-01 15:03:33 -0700951 }
952
David Benjamin31640932017-10-11 13:22:39 -0400953 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700954}
955
David Benjamin31640932017-10-11 13:22:39 -0400956static bool ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
957 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500958 SSL *const ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500959
960 if (contents != NULL) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400961 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjaminfc02b592017-02-17 16:26:01 -0500962 CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -0400963 return false;
David Benjamin163c9562016-08-29 23:14:17 -0400964 }
965
David Benjaminfd45ee72017-08-31 14:49:09 -0400966 hs->extended_master_secret = true;
David Benjamin163c9562016-08-29 23:14:17 -0400967 }
968
David Benjaminc11ea9422017-08-29 16:33:21 -0400969 // Whether EMS is negotiated may not change on renegotiation.
David Benjamin8e7bbba2017-10-13 17:18:35 -0400970 if (ssl->s3->established_session != nullptr &&
David Benjaminfc02b592017-02-17 16:26:01 -0500971 hs->extended_master_secret !=
David Benjaminfd45ee72017-08-31 14:49:09 -0400972 !!ssl->s3->established_session->extended_master_secret) {
David Benjaminfc02b592017-02-17 16:26:01 -0500973 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
974 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -0400975 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -0400976 }
977
David Benjamin31640932017-10-11 13:22:39 -0400978 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700979}
980
David Benjamin31640932017-10-11 13:22:39 -0400981static bool ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
982 CBS *contents) {
David Benjamin9bb15f52018-06-26 00:07:40 -0400983 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400984 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -0400985 }
986
987 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -0400988 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700989 }
990
991 if (CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -0400992 return false;
Adam Langley0a056712015-07-01 15:03:33 -0700993 }
994
David Benjaminfd45ee72017-08-31 14:49:09 -0400995 hs->extended_master_secret = true;
David Benjamin31640932017-10-11 13:22:39 -0400996 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700997}
998
David Benjamin31640932017-10-11 13:22:39 -0400999static bool ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -05001000 if (!hs->extended_master_secret) {
David Benjamin31640932017-10-11 13:22:39 -04001001 return true;
Adam Langley0a056712015-07-01 15:03:33 -07001002 }
1003
1004 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1005 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -04001006 return false;
Adam Langley0a056712015-07-01 15:03:33 -07001007 }
1008
David Benjamin31640932017-10-11 13:22:39 -04001009 return true;
Adam Langley0a056712015-07-01 15:03:33 -07001010}
1011
Adam Langley9b05bc52015-07-01 15:25:33 -07001012
David Benjaminc11ea9422017-08-29 16:33:21 -04001013// Session tickets.
1014//
1015// https://tools.ietf.org/html/rfc5077
Adam Langley9b05bc52015-07-01 15:25:33 -07001016
David Benjamin31640932017-10-11 13:22:39 -04001017static bool ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001018 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001019 // TLS 1.3 uses a different ticket extension.
David Benjamin68161cb2017-06-20 14:49:43 -04001020 if (hs->min_version >= TLS1_3_VERSION ||
David Benjamin7c7d8312016-08-20 13:39:03 -04001021 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
David Benjamin31640932017-10-11 13:22:39 -04001022 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -07001023 }
1024
David Benjaminbfdd1a92018-06-29 16:26:38 -04001025 Span<const uint8_t> ticket;
Adam Langley9b05bc52015-07-01 15:25:33 -07001026
David Benjaminc11ea9422017-08-29 16:33:21 -04001027 // Renegotiation does not participate in session resumption. However, still
1028 // advertise the extension to avoid potentially breaking servers which carry
1029 // over the state from the previous handshake, such as OpenSSL servers
1030 // without upstream's 3c3f0259238594d77264a78944d409f2127642c4.
Adam Langley9b05bc52015-07-01 15:25:33 -07001031 if (!ssl->s3->initial_handshake_complete &&
David Benjamin50596f82018-07-02 19:47:27 -04001032 ssl->session != nullptr &&
David Benjaminbfdd1a92018-06-29 16:26:38 -04001033 !ssl->session->ticket.empty() &&
David Benjaminc11ea9422017-08-29 16:33:21 -04001034 // Don't send TLS 1.3 session tickets in the ticket extension.
David Benjamin50596f82018-07-02 19:47:27 -04001035 ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) {
David Benjaminbfdd1a92018-06-29 16:26:38 -04001036 ticket = ssl->session->ticket;
Adam Langley9b05bc52015-07-01 15:25:33 -07001037 }
1038
David Benjaminbfdd1a92018-06-29 16:26:38 -04001039 CBB ticket_cbb;
Adam Langley9b05bc52015-07-01 15:25:33 -07001040 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
David Benjaminbfdd1a92018-06-29 16:26:38 -04001041 !CBB_add_u16_length_prefixed(out, &ticket_cbb) ||
1042 !CBB_add_bytes(&ticket_cbb, ticket.data(), ticket.size()) ||
Adam Langley9b05bc52015-07-01 15:25:33 -07001043 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001044 return false;
Adam Langley9b05bc52015-07-01 15:25:33 -07001045 }
1046
David Benjamin31640932017-10-11 13:22:39 -04001047 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -07001048}
1049
David Benjamin31640932017-10-11 13:22:39 -04001050static bool ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1051 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001052 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -07001053 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001054 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -07001055 }
1056
David Benjamind1e3ce12017-10-06 18:31:15 -04001057 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001058 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04001059 }
1060
David Benjaminc11ea9422017-08-29 16:33:21 -04001061 // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1062 // this function should never be called, even if the server tries to send the
1063 // extension.
Adam Langley9b05bc52015-07-01 15:25:33 -07001064 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1065
1066 if (CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001067 return false;
Adam Langley9b05bc52015-07-01 15:25:33 -07001068 }
1069
David Benjaminfd45ee72017-08-31 14:49:09 -04001070 hs->ticket_expected = true;
David Benjamin31640932017-10-11 13:22:39 -04001071 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -07001072}
1073
David Benjamin31640932017-10-11 13:22:39 -04001074static bool ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001075 if (!hs->ticket_expected) {
David Benjamin31640932017-10-11 13:22:39 -04001076 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -07001077 }
1078
David Benjaminc11ea9422017-08-29 16:33:21 -04001079 // If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true.
David Benjamin8c880a22016-12-03 02:20:34 -05001080 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001081
1082 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1083 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -04001084 return false;
Adam Langley9b05bc52015-07-01 15:25:33 -07001085 }
1086
David Benjamin31640932017-10-11 13:22:39 -04001087 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -07001088}
1089
1090
David Benjaminc11ea9422017-08-29 16:33:21 -04001091// Signature Algorithms.
1092//
1093// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
Adam Langley2e857bd2015-07-01 16:09:19 -07001094
David Benjamin31640932017-10-11 13:22:39 -04001095static bool ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04001096 if (hs->max_version < TLS1_2_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001097 return true;
Adam Langley2e857bd2015-07-01 16:09:19 -07001098 }
1099
David Benjamin0fc37ef2016-08-17 15:29:46 -04001100 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001101 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1102 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin69522112017-03-28 15:38:29 -05001103 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
David Benjaminebad5082020-02-03 19:32:19 -05001104 !tls12_add_verify_sigalgs(hs, &sigalgs_cbb) ||
David Benjamin69522112017-03-28 15:38:29 -05001105 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001106 return false;
Adam Langley2e857bd2015-07-01 16:09:19 -07001107 }
1108
David Benjamin31640932017-10-11 13:22:39 -04001109 return true;
Adam Langley2e857bd2015-07-01 16:09:19 -07001110}
1111
David Benjamin31640932017-10-11 13:22:39 -04001112static bool ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1113 CBS *contents) {
David Benjaminb1cf48e2017-09-21 11:37:46 -04001114 hs->peer_sigalgs.Reset();
Adam Langley2e857bd2015-07-01 16:09:19 -07001115 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001116 return true;
Adam Langley2e857bd2015-07-01 16:09:19 -07001117 }
1118
1119 CBS supported_signature_algorithms;
1120 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001121 CBS_len(contents) != 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001122 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin31640932017-10-11 13:22:39 -04001123 return false;
Adam Langley2e857bd2015-07-01 16:09:19 -07001124 }
1125
David Benjamin31640932017-10-11 13:22:39 -04001126 return true;
Adam Langley2e857bd2015-07-01 16:09:19 -07001127}
1128
Adam Langley2e857bd2015-07-01 16:09:19 -07001129
David Benjaminc11ea9422017-08-29 16:33:21 -04001130// OCSP Stapling.
1131//
1132// https://tools.ietf.org/html/rfc6066#section-8
Adam Langleybb0bd042015-07-01 16:21:03 -07001133
David Benjamin31640932017-10-11 13:22:39 -04001134static bool ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001135 if (!hs->config->ocsp_stapling_enabled) {
David Benjamin31640932017-10-11 13:22:39 -04001136 return true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001137 }
1138
1139 CBB contents;
1140 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1141 !CBB_add_u16_length_prefixed(out, &contents) ||
1142 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1143 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1144 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1145 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001146 return false;
Adam Langleybb0bd042015-07-01 16:21:03 -07001147 }
1148
David Benjamin31640932017-10-11 13:22:39 -04001149 return true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001150}
1151
David Benjamin31640932017-10-11 13:22:39 -04001152static bool ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1153 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001154 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001155 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001156 return true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001157 }
1158
David Benjaminc11ea9422017-08-29 16:33:21 -04001159 // TLS 1.3 OCSP responses are included in the Certificate extensions.
David Benjamind1e3ce12017-10-06 18:31:15 -04001160 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001161 return false;
Steven Valdez803c77a2016-09-06 14:13:43 -04001162 }
1163
David Benjaminc11ea9422017-08-29 16:33:21 -04001164 // OCSP stapling is forbidden on non-certificate ciphers.
Steven Valdeza833c352016-11-01 13:39:36 -04001165 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001166 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin31640932017-10-11 13:22:39 -04001167 return false;
David Benjamin942f4ed2016-07-16 19:03:49 +03001168 }
1169
David Benjaminc11ea9422017-08-29 16:33:21 -04001170 // Note this does not check for resumption in TLS 1.2. Sending
1171 // status_request here does not make sense, but OpenSSL does so and the
1172 // specification does not say anything. Tolerate it but ignore it.
David Benjamin942f4ed2016-07-16 19:03:49 +03001173
David Benjaminfd45ee72017-08-31 14:49:09 -04001174 hs->certificate_status_expected = true;
David Benjamin31640932017-10-11 13:22:39 -04001175 return true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001176}
1177
David Benjamin31640932017-10-11 13:22:39 -04001178static bool ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1179 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001180 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001181 return true;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001182 }
1183
1184 uint8_t status_type;
1185 if (!CBS_get_u8(contents, &status_type)) {
David Benjamin31640932017-10-11 13:22:39 -04001186 return false;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001187 }
1188
David Benjaminc11ea9422017-08-29 16:33:21 -04001189 // We cannot decide whether OCSP stapling will occur yet because the correct
1190 // SSL_CTX might not have been selected.
David Benjamin8c880a22016-12-03 02:20:34 -05001191 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001192
David Benjamin31640932017-10-11 13:22:39 -04001193 return true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001194}
1195
David Benjamin31640932017-10-11 13:22:39 -04001196static bool ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001197 SSL *const ssl = hs->ssl;
David Benjamind1e3ce12017-10-06 18:31:15 -04001198 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001199 !hs->ocsp_stapling_requested || hs->config->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001200 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001201 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin31640932017-10-11 13:22:39 -04001202 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04001203 }
1204
David Benjaminfd45ee72017-08-31 14:49:09 -04001205 hs->certificate_status_expected = true;
David Benjamin942f4ed2016-07-16 19:03:49 +03001206
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001207 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001208 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001209}
1210
1211
David Benjaminc11ea9422017-08-29 16:33:21 -04001212// Next protocol negotiation.
1213//
1214// https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
Adam Langley97dfcbf2015-07-01 18:35:20 -07001215
David Benjamin31640932017-10-11 13:22:39 -04001216static bool ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001217 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001218 if (ssl->s3->initial_handshake_complete ||
1219 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001220 SSL_is_dtls(ssl)) {
David Benjamin31640932017-10-11 13:22:39 -04001221 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001222 }
1223
1224 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1225 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -04001226 return false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001227 }
1228
David Benjamin31640932017-10-11 13:22:39 -04001229 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001230}
1231
David Benjamin31640932017-10-11 13:22:39 -04001232static bool ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1233 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001234 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001235 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001236 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001237 }
1238
David Benjamind1e3ce12017-10-06 18:31:15 -04001239 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001240 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04001241 }
1242
David Benjaminc11ea9422017-08-29 16:33:21 -04001243 // If any of these are false then we should never have sent the NPN
1244 // extension in the ClientHello and thus this function should never have been
1245 // called.
Adam Langley97dfcbf2015-07-01 18:35:20 -07001246 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001247 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001248 assert(ssl->ctx->next_proto_select_cb != NULL);
1249
David Benjamin8e7bbba2017-10-13 17:18:35 -04001250 if (!ssl->s3->alpn_selected.empty()) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001251 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001252 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1253 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
David Benjamin31640932017-10-11 13:22:39 -04001254 return false;
David Benjamin76c2efc2015-08-31 14:24:29 -04001255 }
1256
Adam Langley97dfcbf2015-07-01 18:35:20 -07001257 const uint8_t *const orig_contents = CBS_data(contents);
1258 const size_t orig_len = CBS_len(contents);
1259
1260 while (CBS_len(contents) != 0) {
1261 CBS proto;
1262 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1263 CBS_len(&proto) == 0) {
David Benjamin31640932017-10-11 13:22:39 -04001264 return false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001265 }
1266 }
1267
1268 uint8_t *selected;
1269 uint8_t selected_len;
1270 if (ssl->ctx->next_proto_select_cb(
1271 ssl, &selected, &selected_len, orig_contents, orig_len,
David Benjamin8e7bbba2017-10-13 17:18:35 -04001272 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK ||
1273 !ssl->s3->next_proto_negotiated.CopyFrom(
1274 MakeConstSpan(selected, selected_len))) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001275 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001276 return false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001277 }
1278
David Benjaminfd45ee72017-08-31 14:49:09 -04001279 hs->next_proto_neg_seen = true;
David Benjamin31640932017-10-11 13:22:39 -04001280 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001281}
1282
David Benjamin31640932017-10-11 13:22:39 -04001283static bool ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1284 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001285 SSL *const ssl = hs->ssl;
David Benjamind1e3ce12017-10-06 18:31:15 -04001286 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001287 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04001288 }
1289
Adam Langley97dfcbf2015-07-01 18:35:20 -07001290 if (contents != NULL && CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001291 return false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001292 }
1293
1294 if (contents == NULL ||
1295 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001296 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001297 SSL_is_dtls(ssl)) {
David Benjamin31640932017-10-11 13:22:39 -04001298 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001299 }
1300
David Benjaminfd45ee72017-08-31 14:49:09 -04001301 hs->next_proto_neg_seen = true;
David Benjamin31640932017-10-11 13:22:39 -04001302 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001303}
1304
David Benjamin31640932017-10-11 13:22:39 -04001305static bool ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001306 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001307 // |next_proto_neg_seen| might have been cleared when an ALPN extension was
1308 // parsed.
David Benjamin8c880a22016-12-03 02:20:34 -05001309 if (!hs->next_proto_neg_seen) {
David Benjamin31640932017-10-11 13:22:39 -04001310 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001311 }
1312
1313 const uint8_t *npa;
1314 unsigned npa_len;
1315
1316 if (ssl->ctx->next_protos_advertised_cb(
1317 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1318 SSL_TLSEXT_ERR_OK) {
David Benjaminfd45ee72017-08-31 14:49:09 -04001319 hs->next_proto_neg_seen = false;
David Benjamin31640932017-10-11 13:22:39 -04001320 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001321 }
1322
1323 CBB contents;
1324 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1325 !CBB_add_u16_length_prefixed(out, &contents) ||
1326 !CBB_add_bytes(&contents, npa, npa_len) ||
1327 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001328 return false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001329 }
1330
David Benjamin31640932017-10-11 13:22:39 -04001331 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001332}
1333
1334
David Benjaminc11ea9422017-08-29 16:33:21 -04001335// Signed certificate timestamps.
1336//
1337// https://tools.ietf.org/html/rfc6962#section-3.3.1
Adam Langleyab8d87d2015-07-10 12:21:39 -07001338
David Benjamin31640932017-10-11 13:22:39 -04001339static bool ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001340 if (!hs->config->signed_cert_timestamps_enabled) {
David Benjamin31640932017-10-11 13:22:39 -04001341 return true;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001342 }
1343
1344 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1345 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -04001346 return false;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001347 }
1348
David Benjamin31640932017-10-11 13:22:39 -04001349 return true;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001350}
1351
David Benjamin31640932017-10-11 13:22:39 -04001352static bool ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1353 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001354 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001355 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001356 return true;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001357 }
1358
David Benjaminc11ea9422017-08-29 16:33:21 -04001359 // TLS 1.3 SCTs are included in the Certificate extensions.
David Benjamind1e3ce12017-10-06 18:31:15 -04001360 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001361 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001362 return false;
Steven Valdeza833c352016-11-01 13:39:36 -04001363 }
1364
David Benjaminc11ea9422017-08-29 16:33:21 -04001365 // If this is false then we should never have sent the SCT extension in the
1366 // ClientHello and thus this function should never have been called.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001367 assert(hs->config->signed_cert_timestamps_enabled);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001368
Adam Langleycfa08c32016-11-17 13:21:27 -08001369 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001370 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001371 return false;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001372 }
1373
David Benjaminc11ea9422017-08-29 16:33:21 -04001374 // Session resumption uses the original session information. The extension
1375 // should not be sent on resumption, but RFC 6962 did not make it a
1376 // requirement, so tolerate this.
1377 //
1378 // TODO(davidben): Enforce this anyway.
David Benjamin8fc2dc02017-08-22 15:07:51 -07001379 if (!ssl->s3->session_reused) {
David Benjaminbfdd1a92018-06-29 16:26:38 -04001380 hs->new_session->signed_cert_timestamp_list.reset(
1381 CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool));
David Benjamin8fc2dc02017-08-22 15:07:51 -07001382 if (hs->new_session->signed_cert_timestamp_list == nullptr) {
1383 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001384 return false;
David Benjamin8fc2dc02017-08-22 15:07:51 -07001385 }
Adam Langleyab8d87d2015-07-10 12:21:39 -07001386 }
1387
David Benjamin31640932017-10-11 13:22:39 -04001388 return true;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001389}
1390
David Benjamin31640932017-10-11 13:22:39 -04001391static bool ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1392 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001393 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001394 return true;
David Benjamin53210cb2016-11-16 09:01:48 +09001395 }
1396
1397 if (CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001398 return false;
David Benjamin53210cb2016-11-16 09:01:48 +09001399 }
1400
David Benjaminfd45ee72017-08-31 14:49:09 -04001401 hs->scts_requested = true;
David Benjamin31640932017-10-11 13:22:39 -04001402 return true;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001403}
1404
David Benjamin31640932017-10-11 13:22:39 -04001405static bool ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001406 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001407 // The extension shouldn't be sent when resuming sessions.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001408 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION || ssl->s3->session_reused ||
1409 hs->config->cert->signed_cert_timestamp_list == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001410 return true;
Paul Lietar4fac72e2015-09-09 13:44:55 +01001411 }
1412
1413 CBB contents;
1414 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1415 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001416 CBB_add_bytes(
1417 &contents,
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001418 CRYPTO_BUFFER_data(
1419 hs->config->cert->signed_cert_timestamp_list.get()),
1420 CRYPTO_BUFFER_len(
1421 hs->config->cert->signed_cert_timestamp_list.get())) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001422 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001423}
1424
1425
David Benjaminc11ea9422017-08-29 16:33:21 -04001426// Application-level Protocol Negotiation.
1427//
1428// https://tools.ietf.org/html/rfc7301
Adam Langleyf18e4532015-07-10 13:39:53 -07001429
David Benjamin31640932017-10-11 13:22:39 -04001430static bool ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001431 SSL *const ssl = hs->ssl;
Nick Harper74161f42020-07-24 15:35:27 -07001432 if (hs->config->alpn_client_proto_list.empty() && ssl->quic_method) {
1433 // ALPN MUST be used with QUIC.
David Benjaminc02c19e2021-02-10 17:49:20 -05001434 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_APPLICATION_PROTOCOL);
Nick Harper74161f42020-07-24 15:35:27 -07001435 return false;
1436 }
1437
David Benjamin0ce090a2018-07-02 20:24:40 -04001438 if (hs->config->alpn_client_proto_list.empty() ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001439 ssl->s3->initial_handshake_complete) {
David Benjamin31640932017-10-11 13:22:39 -04001440 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001441 }
1442
1443 CBB contents, proto_list;
1444 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1445 !CBB_add_u16_length_prefixed(out, &contents) ||
1446 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
David Benjamin0ce090a2018-07-02 20:24:40 -04001447 !CBB_add_bytes(&proto_list, hs->config->alpn_client_proto_list.data(),
1448 hs->config->alpn_client_proto_list.size()) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001449 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001450 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001451 }
1452
David Benjamin31640932017-10-11 13:22:39 -04001453 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001454}
1455
David Benjamin31640932017-10-11 13:22:39 -04001456static bool ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1457 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001458 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001459 if (contents == NULL) {
Nick Harper74161f42020-07-24 15:35:27 -07001460 if (ssl->quic_method) {
1461 // ALPN is required when QUIC is used.
David Benjaminc02c19e2021-02-10 17:49:20 -05001462 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_APPLICATION_PROTOCOL);
Nick Harper74161f42020-07-24 15:35:27 -07001463 *out_alert = SSL_AD_NO_APPLICATION_PROTOCOL;
1464 return false;
1465 }
David Benjamin31640932017-10-11 13:22:39 -04001466 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001467 }
1468
1469 assert(!ssl->s3->initial_handshake_complete);
David Benjamin0ce090a2018-07-02 20:24:40 -04001470 assert(!hs->config->alpn_client_proto_list.empty());
Adam Langleyf18e4532015-07-10 13:39:53 -07001471
David Benjamin8c880a22016-12-03 02:20:34 -05001472 if (hs->next_proto_neg_seen) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001473 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001474 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1475 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
David Benjamin31640932017-10-11 13:22:39 -04001476 return false;
David Benjamin76c2efc2015-08-31 14:24:29 -04001477 }
1478
David Benjaminc11ea9422017-08-29 16:33:21 -04001479 // The extension data consists of a ProtocolNameList which must have
1480 // exactly one ProtocolName. Each of these is length-prefixed.
Adam Langleyf18e4532015-07-10 13:39:53 -07001481 CBS protocol_name_list, protocol_name;
1482 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1483 CBS_len(contents) != 0 ||
1484 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001485 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001486 CBS_len(&protocol_name) == 0 ||
1487 CBS_len(&protocol_name_list) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001488 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001489 }
1490
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001491 if (!ssl_is_alpn_protocol_allowed(hs, protocol_name)) {
David Benjamindd6c2e82017-10-17 15:48:46 -04001492 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1493 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1494 return false;
David Benjamin3e51757d2016-08-11 11:52:23 -04001495 }
1496
David Benjamin8e7bbba2017-10-13 17:18:35 -04001497 if (!ssl->s3->alpn_selected.CopyFrom(protocol_name)) {
Adam Langleyf18e4532015-07-10 13:39:53 -07001498 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001499 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001500 }
1501
David Benjamin31640932017-10-11 13:22:39 -04001502 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001503}
1504
David Benjamin12a3e7e2021-04-13 11:47:36 -04001505bool ssl_is_valid_alpn_list(Span<const uint8_t> in) {
1506 CBS protocol_name_list = in;
1507 if (CBS_len(&protocol_name_list) == 0) {
1508 return false;
1509 }
1510 while (CBS_len(&protocol_name_list) > 0) {
1511 CBS protocol_name;
1512 if (!CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1513 // Empty protocol names are forbidden.
1514 CBS_len(&protocol_name) == 0) {
1515 return false;
1516 }
1517 }
1518 return true;
1519}
1520
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001521bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs,
David Benjamindd6c2e82017-10-17 15:48:46 -04001522 Span<const uint8_t> protocol) {
David Benjamin0ce090a2018-07-02 20:24:40 -04001523 if (hs->config->alpn_client_proto_list.empty()) {
David Benjamindd6c2e82017-10-17 15:48:46 -04001524 return false;
1525 }
1526
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001527 if (hs->ssl->ctx->allow_unknown_alpn_protos) {
David Benjamindd6c2e82017-10-17 15:48:46 -04001528 return true;
1529 }
1530
1531 // Check that the protocol name is one of the ones we advertised.
David Benjamin0ce090a2018-07-02 20:24:40 -04001532 CBS client_protocol_name_list =
1533 MakeConstSpan(hs->config->alpn_client_proto_list),
1534 client_protocol_name;
David Benjamindd6c2e82017-10-17 15:48:46 -04001535 while (CBS_len(&client_protocol_name_list) > 0) {
1536 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1537 &client_protocol_name)) {
1538 return false;
1539 }
1540
1541 if (client_protocol_name == protocol) {
1542 return true;
1543 }
1544 }
1545
1546 return false;
1547}
1548
David Benjamin31640932017-10-11 13:22:39 -04001549bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1550 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001551 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001552 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001553 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001554 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001555 client_hello, &contents,
1556 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
Nick Harper74161f42020-07-24 15:35:27 -07001557 if (ssl->quic_method) {
1558 // ALPN is required when QUIC is used.
David Benjaminc02c19e2021-02-10 17:49:20 -05001559 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_APPLICATION_PROTOCOL);
Nick Harper74161f42020-07-24 15:35:27 -07001560 *out_alert = SSL_AD_NO_APPLICATION_PROTOCOL;
1561 return false;
1562 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001563 // Ignore ALPN if not configured or no extension was supplied.
David Benjamin31640932017-10-11 13:22:39 -04001564 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001565 }
1566
David Benjaminc11ea9422017-08-29 16:33:21 -04001567 // ALPN takes precedence over NPN.
David Benjaminfd45ee72017-08-31 14:49:09 -04001568 hs->next_proto_neg_seen = false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001569
1570 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001571 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1572 CBS_len(&contents) != 0 ||
David Benjamin12a3e7e2021-04-13 11:47:36 -04001573 !ssl_is_valid_alpn_list(protocol_name_list)) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001574 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1575 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001576 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001577 }
1578
Adam Langleyf18e4532015-07-10 13:39:53 -07001579 const uint8_t *selected;
1580 uint8_t selected_len;
David Benjaminc02c19e2021-02-10 17:49:20 -05001581 int ret = ssl->ctx->alpn_select_cb(
1582 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1583 CBS_len(&protocol_name_list), ssl->ctx->alpn_select_cb_arg);
1584 // ALPN is required when QUIC is used.
1585 if (ssl->quic_method &&
1586 (ret == SSL_TLSEXT_ERR_NOACK || ret == SSL_TLSEXT_ERR_ALERT_WARNING)) {
1587 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1588 }
1589 switch (ret) {
1590 case SSL_TLSEXT_ERR_OK:
1591 if (selected_len == 0) {
1592 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1593 *out_alert = SSL_AD_INTERNAL_ERROR;
1594 return false;
1595 }
1596 if (!ssl->s3->alpn_selected.CopyFrom(
1597 MakeConstSpan(selected, selected_len))) {
1598 *out_alert = SSL_AD_INTERNAL_ERROR;
1599 return false;
1600 }
1601 break;
1602 case SSL_TLSEXT_ERR_NOACK:
1603 case SSL_TLSEXT_ERR_ALERT_WARNING:
1604 break;
1605 case SSL_TLSEXT_ERR_ALERT_FATAL:
1606 *out_alert = SSL_AD_NO_APPLICATION_PROTOCOL;
1607 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_APPLICATION_PROTOCOL);
David Benjaminfa544f12018-05-15 15:06:28 -04001608 return false;
David Benjaminc02c19e2021-02-10 17:49:20 -05001609 default:
1610 // Invalid return value.
Adam Langleyf18e4532015-07-10 13:39:53 -07001611 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjaminc02c19e2021-02-10 17:49:20 -05001612 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31640932017-10-11 13:22:39 -04001613 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001614 }
1615
David Benjamin31640932017-10-11 13:22:39 -04001616 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001617}
1618
David Benjamin31640932017-10-11 13:22:39 -04001619static bool ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001620 SSL *const ssl = hs->ssl;
David Benjamin8e7bbba2017-10-13 17:18:35 -04001621 if (ssl->s3->alpn_selected.empty()) {
David Benjamin31640932017-10-11 13:22:39 -04001622 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001623 }
1624
1625 CBB contents, proto_list, proto;
1626 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1627 !CBB_add_u16_length_prefixed(out, &contents) ||
1628 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1629 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin8e7bbba2017-10-13 17:18:35 -04001630 !CBB_add_bytes(&proto, ssl->s3->alpn_selected.data(),
1631 ssl->s3->alpn_selected.size()) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001632 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001633 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001634 }
1635
David Benjamin31640932017-10-11 13:22:39 -04001636 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001637}
1638
1639
David Benjaminc11ea9422017-08-29 16:33:21 -04001640// Channel ID.
1641//
1642// https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
Adam Langley49c7af12015-07-10 14:33:46 -07001643
David Benjamin8c880a22016-12-03 02:20:34 -05001644static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
David Benjamin46853762018-07-03 14:01:26 -04001645 hs->ssl->s3->channel_id_valid = false;
Adam Langley49c7af12015-07-10 14:33:46 -07001646}
1647
David Benjamin31640932017-10-11 13:22:39 -04001648static bool ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001649 SSL *const ssl = hs->ssl;
David Benjamin46853762018-07-03 14:01:26 -04001650 if (!hs->config->channel_id_enabled || SSL_is_dtls(ssl)) {
David Benjamin31640932017-10-11 13:22:39 -04001651 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001652 }
1653
1654 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1655 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -04001656 return false;
Adam Langley49c7af12015-07-10 14:33:46 -07001657 }
1658
David Benjamin31640932017-10-11 13:22:39 -04001659 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001660}
1661
David Benjamin31640932017-10-11 13:22:39 -04001662static bool ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1663 uint8_t *out_alert,
1664 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001665 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001666 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001667 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001668 }
1669
David Benjamince079fd2016-08-02 16:22:34 -04001670 assert(!SSL_is_dtls(ssl));
David Benjamin46853762018-07-03 14:01:26 -04001671 assert(hs->config->channel_id_enabled);
Adam Langley49c7af12015-07-10 14:33:46 -07001672
1673 if (CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001674 return false;
Adam Langley49c7af12015-07-10 14:33:46 -07001675 }
1676
David Benjamin46853762018-07-03 14:01:26 -04001677 ssl->s3->channel_id_valid = true;
David Benjamin31640932017-10-11 13:22:39 -04001678 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001679}
1680
David Benjamin31640932017-10-11 13:22:39 -04001681static bool ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1682 uint8_t *out_alert,
1683 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001684 SSL *const ssl = hs->ssl;
David Benjamin46853762018-07-03 14:01:26 -04001685 if (contents == NULL || !hs->config->channel_id_enabled || SSL_is_dtls(ssl)) {
David Benjamin31640932017-10-11 13:22:39 -04001686 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001687 }
1688
1689 if (CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001690 return false;
Adam Langley49c7af12015-07-10 14:33:46 -07001691 }
1692
David Benjamin46853762018-07-03 14:01:26 -04001693 ssl->s3->channel_id_valid = true;
David Benjamin31640932017-10-11 13:22:39 -04001694 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001695}
1696
David Benjamin31640932017-10-11 13:22:39 -04001697static bool ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001698 SSL *const ssl = hs->ssl;
David Benjamin46853762018-07-03 14:01:26 -04001699 if (!ssl->s3->channel_id_valid) {
David Benjamin31640932017-10-11 13:22:39 -04001700 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001701 }
1702
1703 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1704 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -04001705 return false;
Adam Langley49c7af12015-07-10 14:33:46 -07001706 }
1707
David Benjamin31640932017-10-11 13:22:39 -04001708 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001709}
1710
Adam Langley391250d2015-07-15 19:06:07 -07001711
David Benjaminc11ea9422017-08-29 16:33:21 -04001712// Secure Real-time Transport Protocol (SRTP) extension.
1713//
1714// https://tools.ietf.org/html/rfc5764
Adam Langley391250d2015-07-15 19:06:07 -07001715
Adam Langley391250d2015-07-15 19:06:07 -07001716
David Benjamin8c880a22016-12-03 02:20:34 -05001717static void ext_srtp_init(SSL_HANDSHAKE *hs) {
David Benjaminfceca8e2018-04-12 16:37:19 -04001718 hs->ssl->s3->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001719}
1720
David Benjamin31640932017-10-11 13:22:39 -04001721static bool ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001722 SSL *const ssl = hs->ssl;
David Benjaminb778b9c2021-05-20 11:31:05 -04001723 const STACK_OF(SRTP_PROTECTION_PROFILE) *profiles =
1724 SSL_get_srtp_profiles(ssl);
David Benjaminee910bf2017-07-25 22:36:00 -04001725 if (profiles == NULL ||
1726 sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
David Benjamin31640932017-10-11 13:22:39 -04001727 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001728 }
1729
1730 CBB contents, profile_ids;
1731 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1732 !CBB_add_u16_length_prefixed(out, &contents) ||
1733 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
David Benjamin31640932017-10-11 13:22:39 -04001734 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001735 }
1736
David Benjaminee910bf2017-07-25 22:36:00 -04001737 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
1738 if (!CBB_add_u16(&profile_ids, profile->id)) {
David Benjamin31640932017-10-11 13:22:39 -04001739 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001740 }
1741 }
1742
1743 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1744 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001745 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001746 }
1747
David Benjamin31640932017-10-11 13:22:39 -04001748 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001749}
1750
David Benjamin31640932017-10-11 13:22:39 -04001751static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1752 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001753 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001754 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001755 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001756 }
1757
David Benjaminc11ea9422017-08-29 16:33:21 -04001758 // The extension consists of a u16-prefixed profile ID list containing a
1759 // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1760 //
1761 // See https://tools.ietf.org/html/rfc5764#section-4.1.1
Adam Langley391250d2015-07-15 19:06:07 -07001762 CBS profile_ids, srtp_mki;
1763 uint16_t profile_id;
1764 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1765 !CBS_get_u16(&profile_ids, &profile_id) ||
1766 CBS_len(&profile_ids) != 0 ||
1767 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1768 CBS_len(contents) != 0) {
1769 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
David Benjamin31640932017-10-11 13:22:39 -04001770 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001771 }
1772
1773 if (CBS_len(&srtp_mki) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001774 // Must be no MKI, since we never offer one.
Adam Langley391250d2015-07-15 19:06:07 -07001775 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1776 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04001777 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001778 }
1779
David Benjaminb778b9c2021-05-20 11:31:05 -04001780 // Check to see if the server gave us something we support and offered.
1781 for (const SRTP_PROTECTION_PROFILE *profile : SSL_get_srtp_profiles(ssl)) {
Adam Langley391250d2015-07-15 19:06:07 -07001782 if (profile->id == profile_id) {
David Benjaminfceca8e2018-04-12 16:37:19 -04001783 ssl->s3->srtp_profile = profile;
David Benjamin31640932017-10-11 13:22:39 -04001784 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001785 }
1786 }
1787
1788 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1789 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04001790 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001791}
1792
David Benjamin31640932017-10-11 13:22:39 -04001793static bool ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1794 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001795 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001796 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001797 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001798 }
1799
1800 CBS profile_ids, srtp_mki;
1801 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1802 CBS_len(&profile_ids) < 2 ||
1803 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1804 CBS_len(contents) != 0) {
1805 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
David Benjamin31640932017-10-11 13:22:39 -04001806 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001807 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001808 // Discard the MKI value for now.
Adam Langley391250d2015-07-15 19:06:07 -07001809
1810 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1811 SSL_get_srtp_profiles(ssl);
1812
David Benjaminc11ea9422017-08-29 16:33:21 -04001813 // Pick the server's most preferred profile.
David Benjaminee910bf2017-07-25 22:36:00 -04001814 for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001815 CBS profile_ids_tmp;
1816 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1817
1818 while (CBS_len(&profile_ids_tmp) > 0) {
1819 uint16_t profile_id;
1820 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
David Benjamin31640932017-10-11 13:22:39 -04001821 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001822 }
1823
1824 if (server_profile->id == profile_id) {
David Benjaminfceca8e2018-04-12 16:37:19 -04001825 ssl->s3->srtp_profile = server_profile;
David Benjamin31640932017-10-11 13:22:39 -04001826 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001827 }
1828 }
1829 }
1830
David Benjamin31640932017-10-11 13:22:39 -04001831 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001832}
1833
David Benjamin31640932017-10-11 13:22:39 -04001834static bool ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001835 SSL *const ssl = hs->ssl;
David Benjaminfceca8e2018-04-12 16:37:19 -04001836 if (ssl->s3->srtp_profile == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001837 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001838 }
1839
1840 CBB contents, profile_ids;
1841 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1842 !CBB_add_u16_length_prefixed(out, &contents) ||
1843 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
David Benjaminfceca8e2018-04-12 16:37:19 -04001844 !CBB_add_u16(&profile_ids, ssl->s3->srtp_profile->id) ||
Adam Langley391250d2015-07-15 19:06:07 -07001845 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1846 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001847 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001848 }
1849
David Benjamin31640932017-10-11 13:22:39 -04001850 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001851}
1852
Adam Langleybdd5d662015-07-20 16:19:08 -07001853
David Benjaminc11ea9422017-08-29 16:33:21 -04001854// EC point formats.
1855//
1856// https://tools.ietf.org/html/rfc4492#section-5.1.2
Adam Langleybdd5d662015-07-20 16:19:08 -07001857
David Benjamin31640932017-10-11 13:22:39 -04001858static bool ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001859 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001860 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1861 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001862 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1863 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001864 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001865 return false;
Adam Langleybdd5d662015-07-20 16:19:08 -07001866 }
1867
David Benjamin31640932017-10-11 13:22:39 -04001868 return true;
Adam Langleybdd5d662015-07-20 16:19:08 -07001869}
1870
David Benjamin31640932017-10-11 13:22:39 -04001871static bool ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langleyffe384c2019-05-01 11:13:12 -07001872 // The point format extension is unnecessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -04001873 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001874 return true;
David Benjamin70aba262016-11-01 12:08:15 -04001875 }
1876
David Benjamin8c880a22016-12-03 02:20:34 -05001877 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001878}
1879
David Benjamin31640932017-10-11 13:22:39 -04001880static bool ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1881 CBS *contents) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001882 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001883 return true;
Adam Langleybdd5d662015-07-20 16:19:08 -07001884 }
1885
David Benjamind1e3ce12017-10-06 18:31:15 -04001886 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001887 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04001888 }
1889
Adam Langleybdd5d662015-07-20 16:19:08 -07001890 CBS ec_point_format_list;
1891 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1892 CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001893 return false;
Adam Langleybdd5d662015-07-20 16:19:08 -07001894 }
1895
David Benjaminc11ea9422017-08-29 16:33:21 -04001896 // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1897 // point format.
David Benjamin17cf2cb2016-12-13 01:07:13 -05001898 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1899 TLSEXT_ECPOINTFORMAT_uncompressed,
1900 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001901 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04001902 return false;
Adam Langleybdd5d662015-07-20 16:19:08 -07001903 }
1904
David Benjamin31640932017-10-11 13:22:39 -04001905 return true;
Adam Langleybdd5d662015-07-20 16:19:08 -07001906}
1907
David Benjamin31640932017-10-11 13:22:39 -04001908static bool ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001909 CBS *contents) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001910 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001911 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04001912 }
1913
David Benjamin8c880a22016-12-03 02:20:34 -05001914 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001915}
1916
David Benjamin31640932017-10-11 13:22:39 -04001917static bool ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001918 SSL *const ssl = hs->ssl;
David Benjamind1e3ce12017-10-06 18:31:15 -04001919 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001920 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04001921 }
1922
David Benjamin45738dd2017-02-09 20:01:26 -05001923 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1924 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjamin31640932017-10-11 13:22:39 -04001925 const bool using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001926
1927 if (!using_ecc) {
David Benjamin31640932017-10-11 13:22:39 -04001928 return true;
Adam Langleybdd5d662015-07-20 16:19:08 -07001929 }
1930
David Benjamin8c880a22016-12-03 02:20:34 -05001931 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001932}
1933
Steven Valdeza833c352016-11-01 13:39:36 -04001934
David Benjaminc11ea9422017-08-29 16:33:21 -04001935// Pre Shared Key
1936//
David Benjamina130ce02018-08-14 22:26:39 -05001937// https://tools.ietf.org/html/rfc8446#section-4.2.11
Steven Valdez4aa154e2016-07-29 14:32:55 -04001938
David Benjamin8c880a22016-12-03 02:20:34 -05001939static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1940 SSL *const ssl = hs->ssl;
David Benjamin50596f82018-07-02 19:47:27 -04001941 if (hs->max_version < TLS1_3_VERSION || ssl->session == nullptr ||
1942 ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001943 return 0;
1944 }
1945
David Benjamin50596f82018-07-02 19:47:27 -04001946 size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session.get()));
David Benjaminbfdd1a92018-06-29 16:26:38 -04001947 return 15 + ssl->session->ticket.size() + binder_len;
Steven Valdeza833c352016-11-01 13:39:36 -04001948}
1949
David Benjamin31640932017-10-11 13:22:39 -04001950static bool ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001951 SSL *const ssl = hs->ssl;
Steven Valdezcd8470f2017-10-11 12:29:36 -04001952 hs->needs_psk_binder = false;
David Benjamin50596f82018-07-02 19:47:27 -04001953 if (hs->max_version < TLS1_3_VERSION || ssl->session == nullptr ||
1954 ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001955 return true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001956 }
1957
David Benjamina130ce02018-08-14 22:26:39 -05001958 // Per RFC 8446 section 4.1.4, skip offering the session if the selected
1959 // cipher in HelloRetryRequest does not match. This avoids performing the
1960 // transcript hash transformation for multiple hashes.
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +01001961 if (ssl->s3 && ssl->s3->used_hello_retry_request &&
Steven Valdezcd8470f2017-10-11 12:29:36 -04001962 ssl->session->cipher->algorithm_prf != hs->new_cipher->algorithm_prf) {
1963 return true;
1964 }
1965
David Benjaminad8f5e12017-02-20 17:00:20 -05001966 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001967 ssl_get_current_time(ssl, &now);
1968 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1969 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1970
David Benjaminc11ea9422017-08-29 16:33:21 -04001971 // Fill in a placeholder zero binder of the appropriate length. It will be
1972 // computed and filled in later after length prefixes are computed.
Steven Valdeza833c352016-11-01 13:39:36 -04001973 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
David Benjamin50596f82018-07-02 19:47:27 -04001974 size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session.get()));
Steven Valdeza833c352016-11-01 13:39:36 -04001975
1976 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001977 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1978 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001979 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001980 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
David Benjaminbfdd1a92018-06-29 16:26:38 -04001981 !CBB_add_bytes(&ticket, ssl->session->ticket.data(),
1982 ssl->session->ticket.size()) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001983 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1984 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1985 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1986 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
David Benjamin31640932017-10-11 13:22:39 -04001987 return false;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001988 }
1989
David Benjaminfd45ee72017-08-31 14:49:09 -04001990 hs->needs_psk_binder = true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001991 return CBB_flush(out);
1992}
1993
David Benjamin31640932017-10-11 13:22:39 -04001994bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1995 uint8_t *out_alert,
1996 CBS *contents) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001997 uint16_t psk_id;
1998 if (!CBS_get_u16(contents, &psk_id) ||
1999 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04002000 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002001 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04002002 return false;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002003 }
2004
David Benjaminc11ea9422017-08-29 16:33:21 -04002005 // We only advertise one PSK identity, so the only legal index is zero.
Steven Valdez4aa154e2016-07-29 14:32:55 -04002006 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04002007 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002008 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
David Benjamin31640932017-10-11 13:22:39 -04002009 return false;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002010 }
2011
David Benjamin31640932017-10-11 13:22:39 -04002012 return true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002013}
2014
David Benjamin31640932017-10-11 13:22:39 -04002015bool ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05002016 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin9806ae02019-08-16 15:32:03 -04002017 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert,
2018 const SSL_CLIENT_HELLO *client_hello, CBS *contents) {
2019 // Verify that the pre_shared_key extension is the last extension in
2020 // ClientHello.
2021 if (CBS_data(contents) + CBS_len(contents) !=
2022 client_hello->extensions + client_hello->extensions_len) {
2023 OPENSSL_PUT_ERROR(SSL, SSL_R_PRE_SHARED_KEY_MUST_BE_LAST);
2024 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2025 return false;
2026 }
2027
David Benjaminc11ea9422017-08-29 16:33:21 -04002028 // We only process the first PSK identity since we don't support pure PSK.
David Benjamin707af292017-03-10 17:47:18 -05002029 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05002030 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05002031 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05002032 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04002033 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05002034 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04002035 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04002036 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002037 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04002038 return false;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002039 }
2040
Steven Valdeza833c352016-11-01 13:39:36 -04002041 *out_binders = binders;
2042
David Benjaminc11ea9422017-08-29 16:33:21 -04002043 // Check the syntax of the remaining identities, but do not process them.
David Benjaminaedf3032016-12-01 16:47:56 -05002044 size_t num_identities = 1;
2045 while (CBS_len(&identities) != 0) {
2046 CBS unused_ticket;
2047 uint32_t unused_obfuscated_ticket_age;
2048 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
2049 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
2050 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2051 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04002052 return false;
David Benjaminaedf3032016-12-01 16:47:56 -05002053 }
2054
2055 num_identities++;
2056 }
2057
David Benjaminc11ea9422017-08-29 16:33:21 -04002058 // Check the syntax of the binders. The value will be checked later if
2059 // resuming.
David Benjaminaedf3032016-12-01 16:47:56 -05002060 size_t num_binders = 0;
2061 while (CBS_len(&binders) != 0) {
2062 CBS binder;
2063 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
2064 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2065 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04002066 return false;
David Benjaminaedf3032016-12-01 16:47:56 -05002067 }
2068
2069 num_binders++;
2070 }
2071
2072 if (num_identities != num_binders) {
2073 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
2074 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04002075 return false;
Steven Valdez5b986082016-09-01 12:29:49 -04002076 }
2077
David Benjamin31640932017-10-11 13:22:39 -04002078 return true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002079}
2080
David Benjamin31640932017-10-11 13:22:39 -04002081bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8baf9632016-11-17 17:11:16 +09002082 if (!hs->ssl->s3->session_reused) {
David Benjamin31640932017-10-11 13:22:39 -04002083 return true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002084 }
2085
2086 CBB contents;
2087 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2088 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04002089 // We only consider the first identity for resumption
Steven Valdez4aa154e2016-07-29 14:32:55 -04002090 !CBB_add_u16(&contents, 0) ||
2091 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04002092 return false;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002093 }
2094
David Benjamin31640932017-10-11 13:22:39 -04002095 return true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002096}
2097
2098
David Benjaminc11ea9422017-08-29 16:33:21 -04002099// Pre-Shared Key Exchange Modes
2100//
David Benjamina130ce02018-08-14 22:26:39 -05002101// https://tools.ietf.org/html/rfc8446#section-4.2.9
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002102
David Benjamin31640932017-10-11 13:22:39 -04002103static bool ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2104 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04002105 if (hs->max_version < TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04002106 return true;
Steven Valdeza833c352016-11-01 13:39:36 -04002107 }
2108
2109 CBB contents, ke_modes;
2110 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2111 !CBB_add_u16_length_prefixed(out, &contents) ||
2112 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2113 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
David Benjamin31640932017-10-11 13:22:39 -04002114 return false;
Steven Valdeza833c352016-11-01 13:39:36 -04002115 }
2116
2117 return CBB_flush(out);
2118}
2119
David Benjamin31640932017-10-11 13:22:39 -04002120static bool ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
2121 uint8_t *out_alert,
2122 CBS *contents) {
David Benjamin4eb95cc2016-11-16 17:08:23 +09002123 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04002124 return true;
David Benjamin4eb95cc2016-11-16 17:08:23 +09002125 }
2126
Steven Valdeza833c352016-11-01 13:39:36 -04002127 CBS ke_modes;
2128 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2129 CBS_len(&ke_modes) == 0 ||
2130 CBS_len(contents) != 0) {
2131 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04002132 return false;
Steven Valdeza833c352016-11-01 13:39:36 -04002133 }
2134
David Benjaminc11ea9422017-08-29 16:33:21 -04002135 // We only support tickets with PSK_DHE_KE.
David Benjamin17cf2cb2016-12-13 01:07:13 -05002136 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2137 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002138
David Benjamin31640932017-10-11 13:22:39 -04002139 return true;
Steven Valdeza833c352016-11-01 13:39:36 -04002140}
2141
2142
David Benjaminc11ea9422017-08-29 16:33:21 -04002143// Early Data Indication
2144//
David Benjamina130ce02018-08-14 22:26:39 -05002145// https://tools.ietf.org/html/rfc8446#section-4.2.10
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002146
Steven Valdez51607f12020-08-05 10:46:05 -04002147// ssl_get_local_application_settings looks up the configured ALPS value for
2148// |protocol|. If found, it sets |*out_settings| to the value and returns true.
2149// Otherwise, it returns false.
2150static bool ssl_get_local_application_settings(
2151 const SSL_HANDSHAKE *hs, Span<const uint8_t> *out_settings,
2152 Span<const uint8_t> protocol) {
2153 for (const ALPSConfig &config : hs->config->alps_configs) {
2154 if (protocol == config.protocol) {
2155 *out_settings = config.settings;
2156 return true;
2157 }
2158 }
2159 return false;
2160}
2161
David Benjamin31640932017-10-11 13:22:39 -04002162static bool ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002163 SSL *const ssl = hs->ssl;
David Benjamin64770122019-05-04 11:00:04 -05002164 // The second ClientHello never offers early data, and we must have already
2165 // filled in |early_data_reason| by this point.
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +01002166 if (ssl->s3->used_hello_retry_request) {
David Benjamin64770122019-05-04 11:00:04 -05002167 assert(ssl->s3->early_data_reason != ssl_early_data_unknown);
David Benjamin31640932017-10-11 13:22:39 -04002168 return true;
Steven Valdez2d850622017-01-11 11:34:52 -05002169 }
2170
David Benjamin64770122019-05-04 11:00:04 -05002171 if (!ssl->enable_early_data) {
2172 ssl->s3->early_data_reason = ssl_early_data_disabled;
2173 return true;
2174 }
2175
2176 if (hs->max_version < TLS1_3_VERSION) {
2177 // We discard inapplicable sessions, so this is redundant with the session
2178 // checks below, but we check give a more useful reason.
2179 ssl->s3->early_data_reason = ssl_early_data_protocol_version;
2180 return true;
2181 }
2182
2183 if (ssl->session == nullptr) {
2184 ssl->s3->early_data_reason = ssl_early_data_no_session_offered;
2185 return true;
2186 }
2187
2188 if (ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION ||
2189 ssl->session->ticket_max_early_data == 0) {
2190 ssl->s3->early_data_reason = ssl_early_data_unsupported_for_session;
2191 return true;
2192 }
2193
Steven Valdez51607f12020-08-05 10:46:05 -04002194 if (!ssl->session->early_alpn.empty()) {
2195 if (!ssl_is_alpn_protocol_allowed(hs, ssl->session->early_alpn)) {
2196 // Avoid reporting a confusing value in |SSL_get0_alpn_selected|.
2197 ssl->s3->early_data_reason = ssl_early_data_alpn_mismatch;
2198 return true;
2199 }
2200
David Benjamina9319d92021-01-19 17:44:12 -05002201 // If the previous connection negotiated ALPS, only offer 0-RTT when the
2202 // local are settings are consistent with what we'd offer for this
2203 // connection.
2204 if (ssl->session->has_application_settings) {
2205 Span<const uint8_t> settings;
2206 if (!ssl_get_local_application_settings(hs, &settings,
2207 ssl->session->early_alpn) ||
2208 settings != ssl->session->local_application_settings) {
2209 ssl->s3->early_data_reason = ssl_early_data_alps_mismatch;
2210 return true;
2211 }
Steven Valdez51607f12020-08-05 10:46:05 -04002212 }
David Benjamin64770122019-05-04 11:00:04 -05002213 }
2214
2215 // |early_data_reason| will be filled in later when the server responds.
David Benjaminfd45ee72017-08-31 14:49:09 -04002216 hs->early_data_offered = true;
Steven Valdez2d850622017-01-11 11:34:52 -05002217
2218 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2219 !CBB_add_u16(out, 0) ||
2220 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04002221 return false;
Steven Valdez2d850622017-01-11 11:34:52 -05002222 }
2223
David Benjamin31640932017-10-11 13:22:39 -04002224 return true;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002225}
2226
David Benjamin31640932017-10-11 13:22:39 -04002227static bool ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin64770122019-05-04 11:00:04 -05002228 uint8_t *out_alert,
2229 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05002230 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002231 if (contents == NULL) {
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +01002232 if (hs->early_data_offered && !ssl->s3->used_hello_retry_request) {
David Benjamin64770122019-05-04 11:00:04 -05002233 ssl->s3->early_data_reason = ssl->s3->session_reused
2234 ? ssl_early_data_peer_declined
2235 : ssl_early_data_session_not_resumed;
2236 } else {
2237 // We already filled in |early_data_reason| when declining to offer 0-RTT
2238 // or handling the implicit HelloRetryRequest reject.
2239 assert(ssl->s3->early_data_reason != ssl_early_data_unknown);
2240 }
David Benjamin31640932017-10-11 13:22:39 -04002241 return true;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002242 }
2243
David Benjamin64770122019-05-04 11:00:04 -05002244 // If we received an HRR, the second ClientHello never offers early data, so
2245 // the extensions logic will automatically reject early data extensions as
2246 // unsolicited. This covered by the ServerAcceptsEarlyDataOnHRR test.
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +01002247 assert(!ssl->s3->used_hello_retry_request);
David Benjamin64770122019-05-04 11:00:04 -05002248
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002249 if (CBS_len(contents) != 0) {
2250 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04002251 return false;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002252 }
2253
Steven Valdez2d850622017-01-11 11:34:52 -05002254 if (!ssl->s3->session_reused) {
2255 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2256 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
David Benjamin31640932017-10-11 13:22:39 -04002257 return false;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002258 }
Steven Valdez2d850622017-01-11 11:34:52 -05002259
David Benjamin64770122019-05-04 11:00:04 -05002260 ssl->s3->early_data_reason = ssl_early_data_accepted;
David Benjamin02e62562017-12-18 18:04:01 -05002261 ssl->s3->early_data_accepted = true;
David Benjamin31640932017-10-11 13:22:39 -04002262 return true;
Steven Valdez2d850622017-01-11 11:34:52 -05002263}
2264
David Benjamin31640932017-10-11 13:22:39 -04002265static bool ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2266 uint8_t *out_alert, CBS *contents) {
Steven Valdez2d850622017-01-11 11:34:52 -05002267 SSL *const ssl = hs->ssl;
2268 if (contents == NULL ||
David Benjamind1e3ce12017-10-06 18:31:15 -04002269 ssl_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04002270 return true;
Steven Valdez2d850622017-01-11 11:34:52 -05002271 }
2272
2273 if (CBS_len(contents) != 0) {
2274 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04002275 return false;
Steven Valdez2d850622017-01-11 11:34:52 -05002276 }
2277
David Benjaminfd45ee72017-08-31 14:49:09 -04002278 hs->early_data_offered = true;
David Benjamin31640932017-10-11 13:22:39 -04002279 return true;
Steven Valdez2d850622017-01-11 11:34:52 -05002280}
2281
David Benjamin31640932017-10-11 13:22:39 -04002282static bool ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin02e62562017-12-18 18:04:01 -05002283 if (!hs->ssl->s3->early_data_accepted) {
David Benjamin31640932017-10-11 13:22:39 -04002284 return true;
Steven Valdez2d850622017-01-11 11:34:52 -05002285 }
2286
2287 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2288 !CBB_add_u16(out, 0) ||
2289 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04002290 return false;
Steven Valdez2d850622017-01-11 11:34:52 -05002291 }
2292
David Benjamin31640932017-10-11 13:22:39 -04002293 return true;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002294}
2295
2296
David Benjaminc11ea9422017-08-29 16:33:21 -04002297// Key Share
2298//
David Benjamina130ce02018-08-14 22:26:39 -05002299// https://tools.ietf.org/html/rfc8446#section-4.2.8
Steven Valdez143e8b32016-07-11 13:19:03 -04002300
David Benjamin31640932017-10-11 13:22:39 -04002301static bool ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05002302 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002303 if (hs->max_version < TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04002304 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04002305 }
2306
2307 CBB contents, kse_bytes;
Steven Valdez7e5dd252018-01-22 15:20:31 -05002308 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002309 !CBB_add_u16_length_prefixed(out, &contents) ||
2310 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
David Benjamin31640932017-10-11 13:22:39 -04002311 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002312 }
2313
David Benjamin8c880a22016-12-03 02:20:34 -05002314 uint16_t group_id = hs->retry_group;
Adam Langley7b935932018-11-12 13:53:42 -08002315 uint16_t second_group_id = 0;
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +01002316 if (ssl->s3 && ssl->s3->used_hello_retry_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002317 // We received a HelloRetryRequest without a new curve, so there is no new
2318 // share to append. Leave |hs->key_share| as-is.
Steven Valdeza833c352016-11-01 13:39:36 -04002319 if (group_id == 0 &&
David Benjamin08f5c762017-09-21 02:43:05 -04002320 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(),
2321 hs->key_share_bytes.size())) {
David Benjamin31640932017-10-11 13:22:39 -04002322 return false;
Steven Valdez5440fe02016-07-18 12:40:30 -04002323 }
David Benjamin08f5c762017-09-21 02:43:05 -04002324 hs->key_share_bytes.Reset();
David Benjamin3baa6e12016-10-07 21:10:38 -04002325 if (group_id == 0) {
2326 return CBB_flush(out);
2327 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002328 } else {
David Benjamin3675eb32021-05-18 14:01:07 -04002329 // Add a fake group. See RFC 8701.
David Benjamin65ac9972016-09-02 21:35:25 -04002330 if (ssl->ctx->grease_enabled &&
2331 (!CBB_add_u16(&kse_bytes,
David Benjamina7bc9442018-01-18 10:08:53 -05002332 ssl_get_grease_value(hs, ssl_grease_group)) ||
David Benjamin65ac9972016-09-02 21:35:25 -04002333 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2334 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
David Benjamin31640932017-10-11 13:22:39 -04002335 return false;
David Benjamin65ac9972016-09-02 21:35:25 -04002336 }
2337
David Benjaminc11ea9422017-08-29 16:33:21 -04002338 // Predict the most preferred group.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002339 Span<const uint16_t> groups = tls1_get_grouplist(hs);
David Benjaminb9493552017-09-27 19:02:51 -04002340 if (groups.empty()) {
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002341 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
David Benjamin31640932017-10-11 13:22:39 -04002342 return false;
Steven Valdez5440fe02016-07-18 12:40:30 -04002343 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002344
2345 group_id = groups[0];
Adam Langley7b935932018-11-12 13:53:42 -08002346
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +00002347 if (is_post_quantum_group(group_id) && groups.size() >= 2) {
2348 // CECPQ2(b) is not sent as the only initial key share. We'll include the
Adam Langley7b935932018-11-12 13:53:42 -08002349 // 2nd preference group too to avoid round-trips.
2350 second_group_id = groups[1];
2351 assert(second_group_id != group_id);
2352 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002353 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002354
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002355 CBB key_exchange;
Adam Langley7b935932018-11-12 13:53:42 -08002356 hs->key_shares[0] = SSLKeyShare::Create(group_id);
2357 if (!hs->key_shares[0] ||
David Benjaminc642aca2017-07-19 23:28:43 -04002358 !CBB_add_u16(&kse_bytes, group_id) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002359 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
Adam Langley7b935932018-11-12 13:53:42 -08002360 !hs->key_shares[0]->Offer(&key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002361 !CBB_flush(&kse_bytes)) {
David Benjamin31640932017-10-11 13:22:39 -04002362 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002363 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002364
Adam Langley7b935932018-11-12 13:53:42 -08002365 if (second_group_id != 0) {
2366 hs->key_shares[1] = SSLKeyShare::Create(second_group_id);
2367 if (!hs->key_shares[1] ||
2368 !CBB_add_u16(&kse_bytes, second_group_id) ||
2369 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2370 !hs->key_shares[1]->Offer(&key_exchange) ||
2371 !CBB_flush(&kse_bytes)) {
2372 return false;
2373 }
2374 }
2375
2376 // Save the contents of the extension to repeat it in the second
2377 // ClientHello.
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +01002378 if (ssl->s3 && !ssl->s3->used_hello_retry_request &&
David Benjamin08f5c762017-09-21 02:43:05 -04002379 !hs->key_share_bytes.CopyFrom(
2380 MakeConstSpan(CBB_data(&kse_bytes), CBB_len(&kse_bytes)))) {
David Benjamin31640932017-10-11 13:22:39 -04002381 return false;
Steven Valdez5440fe02016-07-18 12:40:30 -04002382 }
2383
Steven Valdez143e8b32016-07-11 13:19:03 -04002384 return CBB_flush(out);
2385}
2386
David Benjamin31640932017-10-11 13:22:39 -04002387bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
2388 Array<uint8_t> *out_secret,
2389 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002390 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002391 uint16_t group_id;
2392 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002393 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2394 CBS_len(contents) != 0) {
David Benjaminac4d5342017-11-17 01:42:04 +08002395 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002396 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04002397 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002398 }
2399
Adam Langley7b935932018-11-12 13:53:42 -08002400 SSLKeyShare *key_share = hs->key_shares[0].get();
2401 if (key_share->GroupID() != group_id) {
2402 if (!hs->key_shares[1] || hs->key_shares[1]->GroupID() != group_id) {
2403 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2404 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2405 return false;
2406 }
2407 key_share = hs->key_shares[1].get();
Steven Valdez143e8b32016-07-11 13:19:03 -04002408 }
2409
Adam Langley7b935932018-11-12 13:53:42 -08002410 if (!key_share->Finish(out_secret, out_alert, peer_key)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002411 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04002412 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002413 }
2414
David Benjamin45738dd2017-02-09 20:01:26 -05002415 hs->new_session->group_id = group_id;
Adam Langley7b935932018-11-12 13:53:42 -08002416 hs->key_shares[0].reset();
2417 hs->key_shares[1].reset();
David Benjamin31640932017-10-11 13:22:39 -04002418 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04002419}
2420
David Benjamin31640932017-10-11 13:22:39 -04002421bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
David Benjamin3b8c5ec2021-04-12 17:43:23 -04002422 Span<const uint8_t> *out_peer_key,
2423 uint8_t *out_alert,
2424 const SSL_CLIENT_HELLO *client_hello) {
2425 // We only support connections that include an ECDHE key exchange.
2426 CBS contents;
2427 if (!ssl_client_hello_get_extension(client_hello, &contents,
2428 TLSEXT_TYPE_key_share)) {
2429 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
2430 *out_alert = SSL_AD_MISSING_EXTENSION;
David Benjamin31640932017-10-11 13:22:39 -04002431 return false;
Steven Valdez803c77a2016-09-06 14:13:43 -04002432 }
2433
David Benjamin3b8c5ec2021-04-12 17:43:23 -04002434 CBS key_shares;
2435 if (!CBS_get_u16_length_prefixed(&contents, &key_shares) ||
2436 CBS_len(&contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002437 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin31640932017-10-11 13:22:39 -04002438 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002439 }
2440
David Benjaminc11ea9422017-08-29 16:33:21 -04002441 // Find the corresponding key share.
David Benjamin3b8c5ec2021-04-12 17:43:23 -04002442 const uint16_t group_id = hs->new_session->group_id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002443 CBS peer_key;
David Benjamin3b8c5ec2021-04-12 17:43:23 -04002444 CBS_init(&peer_key, nullptr, 0);
Steven Valdez143e8b32016-07-11 13:19:03 -04002445 while (CBS_len(&key_shares) > 0) {
2446 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002447 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002448 if (!CBS_get_u16(&key_shares, &id) ||
Steven Valdez619c8ce2017-10-16 13:12:33 -04002449 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp) ||
2450 CBS_len(&peer_key_tmp) == 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002451 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin31640932017-10-11 13:22:39 -04002452 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002453 }
2454
David Benjamin7e1f9842016-09-20 19:24:40 -04002455 if (id == group_id) {
Steven Valdez619c8ce2017-10-16 13:12:33 -04002456 if (CBS_len(&peer_key) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002457 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2458 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04002459 return false;
David Benjamin7e1f9842016-09-20 19:24:40 -04002460 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002461
David Benjamin7e1f9842016-09-20 19:24:40 -04002462 peer_key = peer_key_tmp;
David Benjaminc11ea9422017-08-29 16:33:21 -04002463 // Continue parsing the structure to keep peers honest.
Steven Valdez143e8b32016-07-11 13:19:03 -04002464 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002465 }
2466
David Benjamin3b8c5ec2021-04-12 17:43:23 -04002467 if (out_peer_key != nullptr) {
2468 *out_peer_key = peer_key;
David Benjamin7e1f9842016-09-20 19:24:40 -04002469 }
David Benjamin3b8c5ec2021-04-12 17:43:23 -04002470 *out_found = CBS_len(&peer_key) != 0;
David Benjamin31640932017-10-11 13:22:39 -04002471 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04002472}
2473
David Benjamin3b8c5ec2021-04-12 17:43:23 -04002474bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002475 CBB kse_bytes, public_key;
David Benjamin3b8c5ec2021-04-12 17:43:23 -04002476 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002477 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
David Benjamin3b8c5ec2021-04-12 17:43:23 -04002478 !CBB_add_u16(&kse_bytes, hs->new_session->group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002479 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjamin879efc32017-09-21 11:20:53 -04002480 !CBB_add_bytes(&public_key, hs->ecdh_public_key.data(),
2481 hs->ecdh_public_key.size()) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002482 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04002483 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002484 }
David Benjamin31640932017-10-11 13:22:39 -04002485 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04002486}
2487
2488
David Benjaminc11ea9422017-08-29 16:33:21 -04002489// Supported Versions
2490//
David Benjamina130ce02018-08-14 22:26:39 -05002491// https://tools.ietf.org/html/rfc8446#section-4.2.1
Steven Valdezfdd10992016-09-15 16:27:05 -04002492
David Benjamin31640932017-10-11 13:22:39 -04002493static bool ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05002494 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002495 if (hs->max_version <= TLS1_2_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04002496 return true;
Steven Valdezfdd10992016-09-15 16:27:05 -04002497 }
2498
2499 CBB contents, versions;
2500 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2501 !CBB_add_u16_length_prefixed(out, &contents) ||
2502 !CBB_add_u8_length_prefixed(&contents, &versions)) {
David Benjamin31640932017-10-11 13:22:39 -04002503 return false;
Steven Valdezfdd10992016-09-15 16:27:05 -04002504 }
2505
David Benjamin3675eb32021-05-18 14:01:07 -04002506 // Add a fake version. See RFC 8701.
David Benjamind9791bf2016-09-27 16:39:52 -04002507 if (ssl->ctx->grease_enabled &&
David Benjamina7bc9442018-01-18 10:08:53 -05002508 !CBB_add_u16(&versions, ssl_get_grease_value(hs, ssl_grease_version))) {
David Benjamin31640932017-10-11 13:22:39 -04002509 return false;
David Benjamind9791bf2016-09-27 16:39:52 -04002510 }
2511
Steven Valdez8f36c512017-06-20 10:55:02 -04002512 if (!ssl_add_supported_versions(hs, &versions) ||
2513 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04002514 return false;
Steven Valdezfdd10992016-09-15 16:27:05 -04002515 }
2516
David Benjamin31640932017-10-11 13:22:39 -04002517 return true;
Steven Valdezfdd10992016-09-15 16:27:05 -04002518}
2519
2520
David Benjaminc11ea9422017-08-29 16:33:21 -04002521// Cookie
2522//
David Benjamina130ce02018-08-14 22:26:39 -05002523// https://tools.ietf.org/html/rfc8446#section-4.2.2
David Benjamin3baa6e12016-10-07 21:10:38 -04002524
David Benjamin31640932017-10-11 13:22:39 -04002525static bool ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminb9493552017-09-27 19:02:51 -04002526 if (hs->cookie.empty()) {
David Benjamin31640932017-10-11 13:22:39 -04002527 return true;
David Benjamin3baa6e12016-10-07 21:10:38 -04002528 }
2529
2530 CBB contents, cookie;
2531 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2532 !CBB_add_u16_length_prefixed(out, &contents) ||
2533 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin08f5c762017-09-21 02:43:05 -04002534 !CBB_add_bytes(&cookie, hs->cookie.data(), hs->cookie.size()) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002535 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04002536 return false;
David Benjamin3baa6e12016-10-07 21:10:38 -04002537 }
2538
David Benjaminc11ea9422017-08-29 16:33:21 -04002539 // The cookie is no longer needed in memory.
David Benjamin08f5c762017-09-21 02:43:05 -04002540 hs->cookie.Reset();
David Benjamin31640932017-10-11 13:22:39 -04002541 return true;
David Benjamin3baa6e12016-10-07 21:10:38 -04002542}
2543
2544
David Benjamina130ce02018-08-14 22:26:39 -05002545// Supported Groups
David Benjaminc11ea9422017-08-29 16:33:21 -04002546//
David Benjamina130ce02018-08-14 22:26:39 -05002547// https://tools.ietf.org/html/rfc4492#section-5.1.1
2548// https://tools.ietf.org/html/rfc8446#section-4.2.7
Adam Langley273d49c2015-07-20 16:38:52 -07002549
David Benjamin31640932017-10-11 13:22:39 -04002550static bool ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05002551 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002552 CBB contents, groups_bytes;
2553 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002554 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002555 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
David Benjamin31640932017-10-11 13:22:39 -04002556 return false;
Adam Langley273d49c2015-07-20 16:38:52 -07002557 }
2558
David Benjamin3675eb32021-05-18 14:01:07 -04002559 // Add a fake group. See RFC 8701.
David Benjamin65ac9972016-09-02 21:35:25 -04002560 if (ssl->ctx->grease_enabled &&
2561 !CBB_add_u16(&groups_bytes,
David Benjamina7bc9442018-01-18 10:08:53 -05002562 ssl_get_grease_value(hs, ssl_grease_group))) {
David Benjamin31640932017-10-11 13:22:39 -04002563 return false;
David Benjamin65ac9972016-09-02 21:35:25 -04002564 }
2565
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002566 for (uint16_t group : tls1_get_grouplist(hs)) {
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +00002567 if (is_post_quantum_group(group) &&
Adam Langley7b935932018-11-12 13:53:42 -08002568 hs->max_version < TLS1_3_VERSION) {
2569 continue;
2570 }
David Benjamincf0ce672017-09-21 02:25:59 -04002571 if (!CBB_add_u16(&groups_bytes, group)) {
David Benjamin31640932017-10-11 13:22:39 -04002572 return false;
Adam Langley273d49c2015-07-20 16:38:52 -07002573 }
2574 }
2575
2576 return CBB_flush(out);
2577}
2578
David Benjamin31640932017-10-11 13:22:39 -04002579static bool ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2580 uint8_t *out_alert,
2581 CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002582 // This extension is not expected to be echoed by servers in TLS 1.2, but some
2583 // BigIP servers send it nonetheless, so do not enforce this.
David Benjamin31640932017-10-11 13:22:39 -04002584 return true;
Adam Langley273d49c2015-07-20 16:38:52 -07002585}
2586
David Benjaminb1cf48e2017-09-21 11:37:46 -04002587static bool parse_u16_array(const CBS *cbs, Array<uint16_t> *out) {
2588 CBS copy = *cbs;
2589 if ((CBS_len(&copy) & 1) != 0) {
2590 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2591 return false;
2592 }
2593
2594 Array<uint16_t> ret;
2595 if (!ret.Init(CBS_len(&copy) / 2)) {
2596 return false;
2597 }
2598 for (size_t i = 0; i < ret.size(); i++) {
2599 if (!CBS_get_u16(&copy, &ret[i])) {
2600 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2601 return false;
2602 }
2603 }
2604
2605 assert(CBS_len(&copy) == 0);
2606 *out = std::move(ret);
2607 return 1;
2608}
2609
David Benjamin31640932017-10-11 13:22:39 -04002610static bool ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002611 uint8_t *out_alert,
David Benjamin31640932017-10-11 13:22:39 -04002612 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002613 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04002614 return true;
Adam Langley273d49c2015-07-20 16:38:52 -07002615 }
2616
Steven Valdezce902a92016-05-17 11:47:53 -04002617 CBS supported_group_list;
2618 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2619 CBS_len(&supported_group_list) == 0 ||
David Benjaminb1cf48e2017-09-21 11:37:46 -04002620 CBS_len(contents) != 0 ||
2621 !parse_u16_array(&supported_group_list, &hs->peer_supported_group_list)) {
David Benjamin31640932017-10-11 13:22:39 -04002622 return false;
Adam Langley273d49c2015-07-20 16:38:52 -07002623 }
2624
David Benjamin31640932017-10-11 13:22:39 -04002625 return true;
Adam Langley273d49c2015-07-20 16:38:52 -07002626}
2627
2628
Nick Harper3c034b22017-12-22 15:50:43 -08002629// QUIC Transport Parameters
2630
David Schinazi3d8b8c32021-01-14 11:25:49 -08002631static bool ext_quic_transport_params_add_clienthello_impl(
2632 SSL_HANDSHAKE *hs, CBB *out, bool use_legacy_codepoint) {
Nick Harper72cff812020-03-26 18:06:16 -07002633 if (hs->config->quic_transport_params.empty() && !hs->ssl->quic_method) {
Nick Harper3c034b22017-12-22 15:50:43 -08002634 return true;
2635 }
Nick Harper72cff812020-03-26 18:06:16 -07002636 if (hs->config->quic_transport_params.empty() || !hs->ssl->quic_method) {
2637 // QUIC Transport Parameters must be sent over QUIC, and they must not be
2638 // sent over non-QUIC transports. If transport params are set, then
2639 // SSL(_CTX)_set_quic_method must also be called.
2640 OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_TRANSPORT_PARAMETERS_MISCONFIGURED);
2641 return false;
2642 }
2643 assert(hs->min_version > TLS1_2_VERSION);
David Schinazi3d8b8c32021-01-14 11:25:49 -08002644 if (use_legacy_codepoint != hs->config->quic_use_legacy_codepoint) {
2645 // Do nothing, we'll send the other codepoint.
2646 return true;
2647 }
2648
David Benjaminc47bfce2021-01-20 17:10:32 -05002649 uint16_t extension_type = TLSEXT_TYPE_quic_transport_parameters_standard;
David Schinazi3d8b8c32021-01-14 11:25:49 -08002650 if (hs->config->quic_use_legacy_codepoint) {
2651 extension_type = TLSEXT_TYPE_quic_transport_parameters_legacy;
2652 }
Nick Harper3c034b22017-12-22 15:50:43 -08002653
2654 CBB contents;
David Schinazi3d8b8c32021-01-14 11:25:49 -08002655 if (!CBB_add_u16(out, extension_type) ||
Nick Harper3c034b22017-12-22 15:50:43 -08002656 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0ce090a2018-07-02 20:24:40 -04002657 !CBB_add_bytes(&contents, hs->config->quic_transport_params.data(),
2658 hs->config->quic_transport_params.size()) ||
Nick Harper3c034b22017-12-22 15:50:43 -08002659 !CBB_flush(out)) {
2660 return false;
2661 }
2662 return true;
2663}
2664
David Schinazi3d8b8c32021-01-14 11:25:49 -08002665static bool ext_quic_transport_params_add_clienthello(SSL_HANDSHAKE *hs,
2666 CBB *out) {
2667 return ext_quic_transport_params_add_clienthello_impl(
2668 hs, out, /*use_legacy_codepoint=*/false);
2669}
2670
2671static bool ext_quic_transport_params_add_clienthello_legacy(SSL_HANDSHAKE *hs,
2672 CBB *out) {
2673 return ext_quic_transport_params_add_clienthello_impl(
2674 hs, out, /*use_legacy_codepoint=*/true);
2675}
2676
2677static bool ext_quic_transport_params_parse_serverhello_impl(
2678 SSL_HANDSHAKE *hs, uint8_t *out_alert, CBS *contents,
2679 bool used_legacy_codepoint) {
Adam Langleyca058c02020-12-16 10:11:08 -08002680 SSL *const ssl = hs->ssl;
2681 if (contents == nullptr) {
David Schinazi3d8b8c32021-01-14 11:25:49 -08002682 if (used_legacy_codepoint != hs->config->quic_use_legacy_codepoint) {
2683 // Silently ignore because we expect the other QUIC codepoint.
2684 return true;
2685 }
Adam Langleyca058c02020-12-16 10:11:08 -08002686 if (!ssl->quic_method) {
2687 return true;
2688 }
Adam Langleyca058c02020-12-16 10:11:08 -08002689 *out_alert = SSL_AD_MISSING_EXTENSION;
2690 return false;
David Schinazi7ba96a62020-12-14 14:46:56 -08002691 }
David Schinazi3d8b8c32021-01-14 11:25:49 -08002692 // The extensions parser will check for unsolicited extensions before
2693 // calling the callback.
2694 assert(ssl->quic_method != nullptr);
Adam Langleyca058c02020-12-16 10:11:08 -08002695 assert(ssl_protocol_version(ssl) == TLS1_3_VERSION);
David Schinazi3d8b8c32021-01-14 11:25:49 -08002696 assert(used_legacy_codepoint == hs->config->quic_use_legacy_codepoint);
Adam Langleyca058c02020-12-16 10:11:08 -08002697 return ssl->s3->peer_quic_transport_params.CopyFrom(*contents);
2698}
2699
David Schinazi3d8b8c32021-01-14 11:25:49 -08002700static bool ext_quic_transport_params_parse_serverhello(SSL_HANDSHAKE *hs,
Adam Langleyca058c02020-12-16 10:11:08 -08002701 uint8_t *out_alert,
2702 CBS *contents) {
David Schinazi3d8b8c32021-01-14 11:25:49 -08002703 return ext_quic_transport_params_parse_serverhello_impl(
2704 hs, out_alert, contents, /*used_legacy_codepoint=*/false);
2705}
2706
2707static bool ext_quic_transport_params_parse_serverhello_legacy(
2708 SSL_HANDSHAKE *hs, uint8_t *out_alert, CBS *contents) {
2709 return ext_quic_transport_params_parse_serverhello_impl(
2710 hs, out_alert, contents, /*used_legacy_codepoint=*/true);
2711}
2712
2713static bool ext_quic_transport_params_parse_clienthello_impl(
2714 SSL_HANDSHAKE *hs, uint8_t *out_alert, CBS *contents,
2715 bool used_legacy_codepoint) {
Nick Harper3c034b22017-12-22 15:50:43 -08002716 SSL *const ssl = hs->ssl;
Nick Harper72cff812020-03-26 18:06:16 -07002717 if (!contents) {
2718 if (!ssl->quic_method) {
2719 if (hs->config->quic_transport_params.empty()) {
2720 return true;
2721 }
2722 // QUIC transport parameters must not be set if |ssl| is not configured
2723 // for QUIC.
2724 OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_TRANSPORT_PARAMETERS_MISCONFIGURED);
2725 *out_alert = SSL_AD_INTERNAL_ERROR;
David Schinazi3d8b8c32021-01-14 11:25:49 -08002726 return false;
2727 }
2728 if (used_legacy_codepoint != hs->config->quic_use_legacy_codepoint) {
2729 // Silently ignore because we expect the other QUIC codepoint.
2730 return true;
Nick Harper72cff812020-03-26 18:06:16 -07002731 }
2732 *out_alert = SSL_AD_MISSING_EXTENSION;
2733 return false;
2734 }
2735 if (!ssl->quic_method) {
David Schinazi3d8b8c32021-01-14 11:25:49 -08002736 if (used_legacy_codepoint) {
2737 // Ignore the legacy private-use codepoint because that could be sent
2738 // to mean something else than QUIC transport parameters.
2739 return true;
2740 }
2741 // Fail if we received the codepoint registered with IANA for QUIC
2742 // because that is not allowed outside of QUIC.
Nick Harper72cff812020-03-26 18:06:16 -07002743 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2744 return false;
Nick Harper3c034b22017-12-22 15:50:43 -08002745 }
Nick Harper80ddfc72020-03-11 18:26:31 -07002746 assert(ssl_protocol_version(ssl) == TLS1_3_VERSION);
David Schinazi3d8b8c32021-01-14 11:25:49 -08002747 if (used_legacy_codepoint != hs->config->quic_use_legacy_codepoint) {
2748 // Silently ignore because we expect the other QUIC codepoint.
2749 return true;
2750 }
Nick Harper3c034b22017-12-22 15:50:43 -08002751 return ssl->s3->peer_quic_transport_params.CopyFrom(*contents);
2752}
2753
David Schinazi3d8b8c32021-01-14 11:25:49 -08002754static bool ext_quic_transport_params_parse_clienthello(SSL_HANDSHAKE *hs,
2755 uint8_t *out_alert,
2756 CBS *contents) {
2757 return ext_quic_transport_params_parse_clienthello_impl(
2758 hs, out_alert, contents, /*used_legacy_codepoint=*/false);
2759}
2760
2761static bool ext_quic_transport_params_parse_clienthello_legacy(
2762 SSL_HANDSHAKE *hs, uint8_t *out_alert, CBS *contents) {
2763 return ext_quic_transport_params_parse_clienthello_impl(
2764 hs, out_alert, contents, /*used_legacy_codepoint=*/true);
2765}
2766
2767static bool ext_quic_transport_params_add_serverhello_impl(
2768 SSL_HANDSHAKE *hs, CBB *out, bool use_legacy_codepoint) {
2769 if (hs->ssl->quic_method == nullptr && use_legacy_codepoint) {
2770 // Ignore the legacy private-use codepoint because that could be sent
2771 // to mean something else than QUIC transport parameters.
2772 return true;
2773 }
Nick Harper72cff812020-03-26 18:06:16 -07002774 assert(hs->ssl->quic_method != nullptr);
David Benjamin0ce090a2018-07-02 20:24:40 -04002775 if (hs->config->quic_transport_params.empty()) {
Nick Harper72cff812020-03-26 18:06:16 -07002776 // Transport parameters must be set when using QUIC.
2777 OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_TRANSPORT_PARAMETERS_MISCONFIGURED);
2778 return false;
Nick Harper3c034b22017-12-22 15:50:43 -08002779 }
David Schinazi3d8b8c32021-01-14 11:25:49 -08002780 if (use_legacy_codepoint != hs->config->quic_use_legacy_codepoint) {
2781 // Do nothing, we'll send the other codepoint.
2782 return true;
2783 }
2784
David Benjaminc47bfce2021-01-20 17:10:32 -05002785 uint16_t extension_type = TLSEXT_TYPE_quic_transport_parameters_standard;
David Schinazi3d8b8c32021-01-14 11:25:49 -08002786 if (hs->config->quic_use_legacy_codepoint) {
2787 extension_type = TLSEXT_TYPE_quic_transport_parameters_legacy;
2788 }
Nick Harper3c034b22017-12-22 15:50:43 -08002789
2790 CBB contents;
David Schinazi3d8b8c32021-01-14 11:25:49 -08002791 if (!CBB_add_u16(out, extension_type) ||
Nick Harper3c034b22017-12-22 15:50:43 -08002792 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0ce090a2018-07-02 20:24:40 -04002793 !CBB_add_bytes(&contents, hs->config->quic_transport_params.data(),
2794 hs->config->quic_transport_params.size()) ||
Nick Harper3c034b22017-12-22 15:50:43 -08002795 !CBB_flush(out)) {
2796 return false;
2797 }
2798
2799 return true;
2800}
2801
David Schinazi3d8b8c32021-01-14 11:25:49 -08002802static bool ext_quic_transport_params_add_serverhello(SSL_HANDSHAKE *hs,
2803 CBB *out) {
2804 return ext_quic_transport_params_add_serverhello_impl(
2805 hs, out, /*use_legacy_codepoint=*/false);
2806}
2807
2808static bool ext_quic_transport_params_add_serverhello_legacy(SSL_HANDSHAKE *hs,
2809 CBB *out) {
2810 return ext_quic_transport_params_add_serverhello_impl(
2811 hs, out, /*use_legacy_codepoint=*/true);
2812}
2813
Christopher Patton6c1b3762018-07-17 12:49:41 -07002814// Delegated credentials.
2815//
2816// https://tools.ietf.org/html/draft-ietf-tls-subcerts
2817
2818static bool ext_delegated_credential_add_clienthello(SSL_HANDSHAKE *hs,
2819 CBB *out) {
2820 return true;
2821}
2822
2823static bool ext_delegated_credential_parse_clienthello(SSL_HANDSHAKE *hs,
2824 uint8_t *out_alert,
2825 CBS *contents) {
Christopher Patton6c1b3762018-07-17 12:49:41 -07002826 if (contents == nullptr || ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) {
2827 // Don't use delegated credentials unless we're negotiating TLS 1.3 or
2828 // higher.
2829 return true;
2830 }
2831
Watson Ladddcd6e442020-08-10 15:12:45 -04002832 // The contents of the extension are the signature algorithms the client will
2833 // accept for a delegated credential.
2834 CBS sigalg_list;
2835 if (!CBS_get_u16_length_prefixed(contents, &sigalg_list) ||
2836 CBS_len(&sigalg_list) == 0 ||
2837 CBS_len(contents) != 0 ||
2838 !parse_u16_array(&sigalg_list, &hs->peer_delegated_credential_sigalgs)) {
2839 return false;
2840 }
2841
Christopher Patton6c1b3762018-07-17 12:49:41 -07002842 hs->delegated_credential_requested = true;
2843 return true;
2844}
2845
Adam Langleya307cb72018-05-02 09:06:48 -07002846// Certificate compression
2847
2848static bool cert_compression_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley0080d832018-06-07 16:39:49 -07002849 bool first = true;
2850 CBB contents, algs;
2851
David Benjamin8fe15842019-10-08 16:57:38 -04002852 for (const auto &alg : hs->ssl->ctx->cert_compression_algs) {
2853 if (alg.decompress == nullptr) {
Adam Langley0080d832018-06-07 16:39:49 -07002854 continue;
2855 }
2856
2857 if (first && (!CBB_add_u16(out, TLSEXT_TYPE_cert_compression) ||
2858 !CBB_add_u16_length_prefixed(out, &contents) ||
2859 !CBB_add_u8_length_prefixed(&contents, &algs))) {
2860 return false;
2861 }
2862 first = false;
David Benjamin8fe15842019-10-08 16:57:38 -04002863 if (!CBB_add_u16(&algs, alg.alg_id)) {
Adam Langley0080d832018-06-07 16:39:49 -07002864 return false;
2865 }
2866 }
2867
2868 return first || CBB_flush(out);
Adam Langleya307cb72018-05-02 09:06:48 -07002869}
2870
2871static bool cert_compression_parse_serverhello(SSL_HANDSHAKE *hs,
2872 uint8_t *out_alert,
2873 CBS *contents) {
2874 if (contents == nullptr) {
2875 return true;
2876 }
2877
2878 // The server may not echo this extension. Any server to client negotiation is
2879 // advertised in the CertificateRequest message.
2880 return false;
2881}
2882
2883static bool cert_compression_parse_clienthello(SSL_HANDSHAKE *hs,
2884 uint8_t *out_alert,
2885 CBS *contents) {
2886 if (contents == nullptr) {
2887 return true;
2888 }
2889
David Benjamin8fe15842019-10-08 16:57:38 -04002890 const SSL_CTX *ctx = hs->ssl->ctx.get();
2891 const size_t num_algs = ctx->cert_compression_algs.size();
Adam Langleya307cb72018-05-02 09:06:48 -07002892
2893 CBS alg_ids;
2894 if (!CBS_get_u8_length_prefixed(contents, &alg_ids) ||
2895 CBS_len(contents) != 0 ||
2896 CBS_len(&alg_ids) == 0 ||
2897 CBS_len(&alg_ids) % 2 == 1) {
2898 return false;
2899 }
2900
2901 const size_t num_given_alg_ids = CBS_len(&alg_ids) / 2;
2902 Array<uint16_t> given_alg_ids;
2903 if (!given_alg_ids.Init(num_given_alg_ids)) {
2904 return false;
2905 }
2906
2907 size_t best_index = num_algs;
2908 size_t given_alg_idx = 0;
2909
2910 while (CBS_len(&alg_ids) > 0) {
2911 uint16_t alg_id;
2912 if (!CBS_get_u16(&alg_ids, &alg_id)) {
2913 return false;
2914 }
2915
2916 given_alg_ids[given_alg_idx++] = alg_id;
2917
2918 for (size_t i = 0; i < num_algs; i++) {
David Benjamin8fe15842019-10-08 16:57:38 -04002919 const auto &alg = ctx->cert_compression_algs[i];
2920 if (alg.alg_id == alg_id && alg.compress != nullptr) {
Adam Langleya307cb72018-05-02 09:06:48 -07002921 if (i < best_index) {
2922 best_index = i;
2923 }
2924 break;
2925 }
2926 }
2927 }
2928
2929 qsort(given_alg_ids.data(), given_alg_ids.size(), sizeof(uint16_t),
2930 compare_uint16_t);
2931 for (size_t i = 1; i < num_given_alg_ids; i++) {
2932 if (given_alg_ids[i - 1] == given_alg_ids[i]) {
2933 return false;
2934 }
2935 }
2936
2937 if (best_index < num_algs &&
2938 ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
2939 hs->cert_compression_negotiated = true;
David Benjamin8fe15842019-10-08 16:57:38 -04002940 hs->cert_compression_alg_id = ctx->cert_compression_algs[best_index].alg_id;
Adam Langleya307cb72018-05-02 09:06:48 -07002941 }
2942
2943 return true;
2944}
2945
2946static bool cert_compression_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2947 return true;
2948}
Nick Harper3c034b22017-12-22 15:50:43 -08002949
Steven Valdez51607f12020-08-05 10:46:05 -04002950// Application-level Protocol Settings
2951//
2952// https://tools.ietf.org/html/draft-vvv-tls-alps-01
2953
2954static bool ext_alps_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2955 SSL *const ssl = hs->ssl;
2956 if (// ALPS requires TLS 1.3.
2957 hs->max_version < TLS1_3_VERSION ||
2958 // Do not offer ALPS without ALPN.
2959 hs->config->alpn_client_proto_list.empty() ||
2960 // Do not offer ALPS if not configured.
2961 hs->config->alps_configs.empty() ||
2962 // Do not offer ALPS on renegotiation handshakes.
2963 ssl->s3->initial_handshake_complete) {
2964 return true;
2965 }
2966
2967 CBB contents, proto_list, proto;
2968 if (!CBB_add_u16(out, TLSEXT_TYPE_application_settings) ||
2969 !CBB_add_u16_length_prefixed(out, &contents) ||
2970 !CBB_add_u16_length_prefixed(&contents, &proto_list)) {
2971 return false;
2972 }
2973
2974 for (const ALPSConfig &config : hs->config->alps_configs) {
2975 if (!CBB_add_u8_length_prefixed(&proto_list, &proto) ||
2976 !CBB_add_bytes(&proto, config.protocol.data(),
2977 config.protocol.size())) {
2978 return false;
2979 }
2980 }
2981
2982 return CBB_flush(out);
2983}
2984
2985static bool ext_alps_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
2986 CBS *contents) {
2987 SSL *const ssl = hs->ssl;
2988 if (contents == nullptr) {
2989 return true;
2990 }
2991
2992 assert(!ssl->s3->initial_handshake_complete);
2993 assert(!hs->config->alpn_client_proto_list.empty());
2994 assert(!hs->config->alps_configs.empty());
2995
2996 // ALPS requires TLS 1.3.
2997 if (ssl_protocol_version(ssl) < TLS1_3_VERSION) {
2998 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2999 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3000 return false;
3001 }
3002
3003 // Note extension callbacks may run in any order, so we defer checking
3004 // consistency with ALPN to |ssl_check_serverhello_tlsext|.
3005 if (!hs->new_session->peer_application_settings.CopyFrom(*contents)) {
3006 *out_alert = SSL_AD_INTERNAL_ERROR;
3007 return false;
3008 }
3009
3010 hs->new_session->has_application_settings = true;
3011 return true;
3012}
3013
3014static bool ext_alps_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
3015 SSL *const ssl = hs->ssl;
3016 // If early data is accepted, we omit the ALPS extension. It is implicitly
3017 // carried over from the previous connection.
3018 if (hs->new_session == nullptr ||
3019 !hs->new_session->has_application_settings ||
3020 ssl->s3->early_data_accepted) {
3021 return true;
3022 }
3023
3024 CBB contents;
3025 if (!CBB_add_u16(out, TLSEXT_TYPE_application_settings) ||
3026 !CBB_add_u16_length_prefixed(out, &contents) ||
3027 !CBB_add_bytes(&contents,
3028 hs->new_session->local_application_settings.data(),
3029 hs->new_session->local_application_settings.size()) ||
3030 !CBB_flush(out)) {
3031 return false;
3032 }
3033
3034 return true;
3035}
3036
3037bool ssl_negotiate_alps(SSL_HANDSHAKE *hs, uint8_t *out_alert,
3038 const SSL_CLIENT_HELLO *client_hello) {
3039 SSL *const ssl = hs->ssl;
3040 if (ssl->s3->alpn_selected.empty()) {
3041 return true;
3042 }
3043
3044 // If we negotiate ALPN over TLS 1.3, try to negotiate ALPS.
3045 CBS alps_contents;
3046 Span<const uint8_t> settings;
3047 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION &&
3048 ssl_get_local_application_settings(hs, &settings,
3049 ssl->s3->alpn_selected) &&
3050 ssl_client_hello_get_extension(client_hello, &alps_contents,
3051 TLSEXT_TYPE_application_settings)) {
3052 // Check if the client supports ALPS with the selected ALPN.
3053 bool found = false;
3054 CBS alps_list;
3055 if (!CBS_get_u16_length_prefixed(&alps_contents, &alps_list) ||
3056 CBS_len(&alps_contents) != 0 ||
3057 CBS_len(&alps_list) == 0) {
3058 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3059 *out_alert = SSL_AD_DECODE_ERROR;
3060 return false;
3061 }
3062 while (CBS_len(&alps_list) > 0) {
3063 CBS protocol_name;
3064 if (!CBS_get_u8_length_prefixed(&alps_list, &protocol_name) ||
3065 // Empty protocol names are forbidden.
3066 CBS_len(&protocol_name) == 0) {
3067 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3068 *out_alert = SSL_AD_DECODE_ERROR;
3069 return false;
3070 }
3071 if (protocol_name == MakeConstSpan(ssl->s3->alpn_selected)) {
3072 found = true;
3073 }
3074 }
3075
3076 // Negotiate ALPS if both client also supports ALPS for this protocol.
3077 if (found) {
3078 hs->new_session->has_application_settings = true;
3079 if (!hs->new_session->local_application_settings.CopyFrom(settings)) {
3080 *out_alert = SSL_AD_INTERNAL_ERROR;
3081 return false;
3082 }
3083 }
3084 }
3085
3086 return true;
3087}
Adam Langleya86c6982019-07-16 15:26:21 -07003088
David Benjaminc11ea9422017-08-29 16:33:21 -04003089// kExtensions contains all the supported extensions.
Adam Langley614c66a2015-06-12 15:26:58 -07003090static const struct tls_extension kExtensions[] = {
3091 {
3092 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04003093 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07003094 ext_sni_add_clienthello,
3095 ext_sni_parse_serverhello,
3096 ext_sni_parse_clienthello,
3097 ext_sni_add_serverhello,
3098 },
Adam Langley0a056712015-07-01 15:03:33 -07003099 {
Dan McArdle1920c6f2020-03-11 17:29:40 -04003100 TLSEXT_TYPE_encrypted_client_hello,
3101 NULL,
3102 ext_ech_add_clienthello,
3103 ext_ech_parse_serverhello,
Dan McArdlec2959352020-10-29 14:31:31 -04003104 ext_ech_parse_clienthello,
Daniel McArdle00e434d2021-02-18 11:47:18 -05003105 ext_ech_add_serverhello,
Dan McArdlec2959352020-10-29 14:31:31 -04003106 },
3107 {
3108 TLSEXT_TYPE_ech_is_inner,
3109 NULL,
3110 ext_ech_is_inner_add_clienthello,
3111 forbid_parse_serverhello,
3112 ext_ech_is_inner_parse_clienthello,
Dan McArdle1920c6f2020-03-11 17:29:40 -04003113 dont_add_serverhello,
3114 },
3115 {
Adam Langley0a056712015-07-01 15:03:33 -07003116 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04003117 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07003118 ext_ems_add_clienthello,
3119 ext_ems_parse_serverhello,
3120 ext_ems_parse_clienthello,
3121 ext_ems_add_serverhello,
3122 },
Adam Langley9b05bc52015-07-01 15:25:33 -07003123 {
Adam Langleyd6680952018-08-23 08:01:23 -07003124 TLSEXT_TYPE_renegotiate,
3125 NULL,
3126 ext_ri_add_clienthello,
3127 ext_ri_parse_serverhello,
3128 ext_ri_parse_clienthello,
3129 ext_ri_add_serverhello,
3130 },
3131 {
3132 TLSEXT_TYPE_supported_groups,
3133 NULL,
3134 ext_supported_groups_add_clienthello,
3135 ext_supported_groups_parse_serverhello,
3136 ext_supported_groups_parse_clienthello,
3137 dont_add_serverhello,
3138 },
3139 {
3140 TLSEXT_TYPE_ec_point_formats,
3141 NULL,
3142 ext_ec_point_add_clienthello,
3143 ext_ec_point_parse_serverhello,
3144 ext_ec_point_parse_clienthello,
3145 ext_ec_point_add_serverhello,
3146 },
3147 {
Adam Langley9b05bc52015-07-01 15:25:33 -07003148 TLSEXT_TYPE_session_ticket,
3149 NULL,
3150 ext_ticket_add_clienthello,
3151 ext_ticket_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04003152 // Ticket extension client parsing is handled in ssl_session.c
Steven Valdez6b8509a2016-07-12 13:38:32 -04003153 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07003154 ext_ticket_add_serverhello,
3155 },
Adam Langley2e857bd2015-07-01 16:09:19 -07003156 {
Adam Langleyd6680952018-08-23 08:01:23 -07003157 TLSEXT_TYPE_application_layer_protocol_negotiation,
3158 NULL,
3159 ext_alpn_add_clienthello,
3160 ext_alpn_parse_serverhello,
3161 // ALPN is negotiated late in |ssl_negotiate_alpn|.
3162 ignore_parse_clienthello,
3163 ext_alpn_add_serverhello,
3164 },
3165 {
3166 TLSEXT_TYPE_status_request,
3167 NULL,
3168 ext_ocsp_add_clienthello,
3169 ext_ocsp_parse_serverhello,
3170 ext_ocsp_parse_clienthello,
3171 ext_ocsp_add_serverhello,
3172 },
3173 {
Adam Langley2e857bd2015-07-01 16:09:19 -07003174 TLSEXT_TYPE_signature_algorithms,
3175 NULL,
3176 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04003177 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07003178 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04003179 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07003180 },
Adam Langleybb0bd042015-07-01 16:21:03 -07003181 {
Adam Langley97dfcbf2015-07-01 18:35:20 -07003182 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04003183 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07003184 ext_npn_add_clienthello,
3185 ext_npn_parse_serverhello,
3186 ext_npn_parse_clienthello,
3187 ext_npn_add_serverhello,
3188 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07003189 {
3190 TLSEXT_TYPE_certificate_timestamp,
3191 NULL,
3192 ext_sct_add_clienthello,
3193 ext_sct_parse_serverhello,
3194 ext_sct_parse_clienthello,
3195 ext_sct_add_serverhello,
3196 },
Adam Langleyf18e4532015-07-10 13:39:53 -07003197 {
Adam Langley49c7af12015-07-10 14:33:46 -07003198 TLSEXT_TYPE_channel_id,
3199 ext_channel_id_init,
3200 ext_channel_id_add_clienthello,
3201 ext_channel_id_parse_serverhello,
3202 ext_channel_id_parse_clienthello,
3203 ext_channel_id_add_serverhello,
3204 },
Adam Langley391250d2015-07-15 19:06:07 -07003205 {
3206 TLSEXT_TYPE_srtp,
3207 ext_srtp_init,
3208 ext_srtp_add_clienthello,
3209 ext_srtp_parse_serverhello,
3210 ext_srtp_parse_clienthello,
3211 ext_srtp_add_serverhello,
3212 },
Adam Langleybdd5d662015-07-20 16:19:08 -07003213 {
Steven Valdez7e5dd252018-01-22 15:20:31 -05003214 TLSEXT_TYPE_key_share,
Steven Valdez143e8b32016-07-11 13:19:03 -04003215 NULL,
3216 ext_key_share_add_clienthello,
3217 forbid_parse_serverhello,
3218 ignore_parse_clienthello,
3219 dont_add_serverhello,
3220 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04003221 {
Steven Valdeza833c352016-11-01 13:39:36 -04003222 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04003223 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04003224 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04003225 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09003226 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04003227 dont_add_serverhello,
3228 },
Steven Valdezfdd10992016-09-15 16:27:05 -04003229 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05003230 TLSEXT_TYPE_early_data,
3231 NULL,
3232 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05003233 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05003234 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05003235 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05003236 },
3237 {
Steven Valdezfdd10992016-09-15 16:27:05 -04003238 TLSEXT_TYPE_supported_versions,
3239 NULL,
3240 ext_supported_versions_add_clienthello,
3241 forbid_parse_serverhello,
3242 ignore_parse_clienthello,
3243 dont_add_serverhello,
3244 },
David Benjamin3baa6e12016-10-07 21:10:38 -04003245 {
3246 TLSEXT_TYPE_cookie,
3247 NULL,
3248 ext_cookie_add_clienthello,
3249 forbid_parse_serverhello,
3250 ignore_parse_clienthello,
3251 dont_add_serverhello,
3252 },
Adam Langley512a2892017-12-30 08:04:39 -08003253 {
David Benjaminc47bfce2021-01-20 17:10:32 -05003254 TLSEXT_TYPE_quic_transport_parameters_standard,
Nick Harper3c034b22017-12-22 15:50:43 -08003255 NULL,
3256 ext_quic_transport_params_add_clienthello,
3257 ext_quic_transport_params_parse_serverhello,
3258 ext_quic_transport_params_parse_clienthello,
3259 ext_quic_transport_params_add_serverhello,
3260 },
Adam Langley273d49c2015-07-20 16:38:52 -07003261 {
David Schinazi3d8b8c32021-01-14 11:25:49 -08003262 TLSEXT_TYPE_quic_transport_parameters_legacy,
3263 NULL,
3264 ext_quic_transport_params_add_clienthello_legacy,
3265 ext_quic_transport_params_parse_serverhello_legacy,
3266 ext_quic_transport_params_parse_clienthello_legacy,
3267 ext_quic_transport_params_add_serverhello_legacy,
3268 },
3269 {
Adam Langleya307cb72018-05-02 09:06:48 -07003270 TLSEXT_TYPE_cert_compression,
3271 NULL,
3272 cert_compression_add_clienthello,
3273 cert_compression_parse_serverhello,
3274 cert_compression_parse_clienthello,
3275 cert_compression_add_serverhello,
3276 },
Christopher Patton6c1b3762018-07-17 12:49:41 -07003277 {
3278 TLSEXT_TYPE_delegated_credential,
3279 NULL,
3280 ext_delegated_credential_add_clienthello,
3281 forbid_parse_serverhello,
3282 ext_delegated_credential_parse_clienthello,
3283 dont_add_serverhello,
3284 },
Steven Valdez51607f12020-08-05 10:46:05 -04003285 {
3286 TLSEXT_TYPE_application_settings,
3287 NULL,
3288 ext_alps_add_clienthello,
3289 ext_alps_parse_serverhello,
3290 // ALPS is negotiated late in |ssl_negotiate_alpn|.
3291 ignore_parse_clienthello,
3292 ext_alps_add_serverhello,
3293 },
Adam Langley614c66a2015-06-12 15:26:58 -07003294};
3295
3296#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
3297
David Benjamina3d76d02017-07-14 19:36:07 -04003298static_assert(kNumExtensions <=
3299 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
3300 "too many extensions for sent bitset");
3301static_assert(kNumExtensions <=
3302 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
3303 "too many extensions for received bitset");
Adam Langley4cfa96b2015-07-01 11:56:55 -07003304
Adam Langley614c66a2015-06-12 15:26:58 -07003305static const struct tls_extension *tls_extension_find(uint32_t *out_index,
3306 uint16_t value) {
3307 unsigned i;
3308 for (i = 0; i < kNumExtensions; i++) {
3309 if (kExtensions[i].value == value) {
3310 *out_index = i;
3311 return &kExtensions[i];
3312 }
3313 }
3314
3315 return NULL;
3316}
3317
David Benjamin861abcc2018-07-14 17:40:26 -04003318bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out,
3319 size_t header_len) {
David Benjamin8c880a22016-12-03 02:20:34 -05003320 SSL *const ssl = hs->ssl;
David Benjamine8d53502015-10-10 14:13:23 -04003321 CBB extensions;
3322 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003323 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04003324 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003325 }
Adam Langley95c29f32014-06-20 12:00:00 -07003326
David Benjamin64770122019-05-04 11:00:04 -05003327 // Note we may send multiple ClientHellos for DTLS HelloVerifyRequest and TLS
3328 // 1.3 HelloRetryRequest. For the latter, the extensions may change, so it is
3329 // important to reset this value.
David Benjamin8c880a22016-12-03 02:20:34 -05003330 hs->extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07003331
David Benjamin54091232016-09-05 12:47:25 -04003332 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003333 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05003334 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07003335 }
3336 }
Adam Langley95c29f32014-06-20 12:00:00 -07003337
David Benjamin65ac9972016-09-02 21:35:25 -04003338 uint16_t grease_ext1 = 0;
3339 if (ssl->ctx->grease_enabled) {
David Benjamin3675eb32021-05-18 14:01:07 -04003340 // Add a fake empty extension. See RFC 8701.
David Benjamina7bc9442018-01-18 10:08:53 -05003341 grease_ext1 = ssl_get_grease_value(hs, ssl_grease_extension1);
David Benjamin65ac9972016-09-02 21:35:25 -04003342 if (!CBB_add_u16(&extensions, grease_ext1) ||
3343 !CBB_add_u16(&extensions, 0 /* zero length */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003344 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04003345 return false;
David Benjamin65ac9972016-09-02 21:35:25 -04003346 }
3347 }
3348
Adam Langley7f4f41f2018-08-23 08:58:49 -07003349 bool last_was_empty = false;
David Benjamin54091232016-09-05 12:47:25 -04003350 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003351 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05003352 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003353 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003354 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin861abcc2018-07-14 17:40:26 -04003355 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003356 }
Adam Langley95c29f32014-06-20 12:00:00 -07003357
Adam Langley7f4f41f2018-08-23 08:58:49 -07003358 const size_t bytes_written = CBB_len(&extensions) - len_before;
3359 if (bytes_written != 0) {
David Benjamin8c880a22016-12-03 02:20:34 -05003360 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07003361 }
Adam Langley7f4f41f2018-08-23 08:58:49 -07003362 // If the difference in lengths is only four bytes then the extension had
3363 // an empty body.
3364 last_was_empty = (bytes_written == 4);
Adam Langleyfcf25832014-12-18 17:42:32 -08003365 }
Adam Langley75712922014-10-10 16:23:43 -07003366
David Benjamin65ac9972016-09-02 21:35:25 -04003367 if (ssl->ctx->grease_enabled) {
David Benjamin3675eb32021-05-18 14:01:07 -04003368 // Add a fake non-empty extension. See RFC 8701.
David Benjamina7bc9442018-01-18 10:08:53 -05003369 uint16_t grease_ext2 = ssl_get_grease_value(hs, ssl_grease_extension2);
David Benjamin65ac9972016-09-02 21:35:25 -04003370
David Benjaminc11ea9422017-08-29 16:33:21 -04003371 // The two fake extensions must not have the same value. GREASE values are
3372 // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
3373 // one.
David Benjamin65ac9972016-09-02 21:35:25 -04003374 if (grease_ext1 == grease_ext2) {
3375 grease_ext2 ^= 0x1010;
3376 }
3377
3378 if (!CBB_add_u16(&extensions, grease_ext2) ||
3379 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
3380 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003381 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04003382 return false;
David Benjamin65ac9972016-09-02 21:35:25 -04003383 }
Adam Langley7f4f41f2018-08-23 08:58:49 -07003384
3385 last_was_empty = false;
David Benjamin65ac9972016-09-02 21:35:25 -04003386 }
3387
Nick Harper281a8f52020-07-28 10:12:49 -07003388 if (!SSL_is_dtls(ssl) && !ssl->quic_method) {
David Benjamin8c880a22016-12-03 02:20:34 -05003389 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04003390 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langley7f4f41f2018-08-23 08:58:49 -07003391 size_t padding_len = 0;
3392
3393 // The final extension must be non-empty. WebSphere Application
3394 // Server 7.0 is intolerant to the last extension being zero-length. See
3395 // https://crbug.com/363583.
3396 if (last_was_empty && psk_extension_len == 0) {
3397 padding_len = 1;
3398 // The addition of the padding extension may push us into the F5 bug.
3399 header_len += 4 + padding_len;
3400 }
3401
3402 // Add padding to workaround bugs in F5 terminators. See RFC 7685.
3403 //
3404 // NB: because this code works out the length of all existing extensions
3405 // it MUST always appear last (save for any PSK extension).
Adam Langleyfcf25832014-12-18 17:42:32 -08003406 if (header_len > 0xff && header_len < 0x200) {
Adam Langley7f4f41f2018-08-23 08:58:49 -07003407 // If our calculations already included a padding extension, remove that
3408 // factor because we're about to change its length.
3409 if (padding_len != 0) {
3410 header_len -= 4 + padding_len;
3411 }
3412 padding_len = 0x200 - header_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04003413 // Extensions take at least four bytes to encode. Always include at least
3414 // one byte of data if including the extension. WebSphere Application
3415 // Server 7.0 is intolerant to the last extension being zero-length. See
3416 // https://crbug.com/363583.
Adam Langleyfcf25832014-12-18 17:42:32 -08003417 if (padding_len >= 4 + 1) {
3418 padding_len -= 4;
3419 } else {
3420 padding_len = 1;
3421 }
Adam Langley7f4f41f2018-08-23 08:58:49 -07003422 }
Adam Langley95c29f32014-06-20 12:00:00 -07003423
Adam Langley7f4f41f2018-08-23 08:58:49 -07003424 if (padding_len != 0) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003425 uint8_t *padding_bytes;
3426 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
3427 !CBB_add_u16(&extensions, padding_len) ||
3428 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003429 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04003430 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003431 }
Adam Langley75712922014-10-10 16:23:43 -07003432
David Benjamin17cf2cb2016-12-13 01:07:13 -05003433 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08003434 }
3435 }
Adam Langley75712922014-10-10 16:23:43 -07003436
David Benjaminc11ea9422017-08-29 16:33:21 -04003437 // The PSK extension must be last, including after the padding.
David Benjamin8c880a22016-12-03 02:20:34 -05003438 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003439 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04003440 return false;
Steven Valdeza833c352016-11-01 13:39:36 -04003441 }
3442
David Benjaminc11ea9422017-08-29 16:33:21 -04003443 // Discard empty extensions blocks.
David Benjamina01deee2015-12-08 18:56:31 -05003444 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04003445 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07003446 }
3447
David Benjamine8d53502015-10-10 14:13:23 -04003448 return CBB_flush(out);
Adam Langleyfcf25832014-12-18 17:42:32 -08003449}
Adam Langley95c29f32014-06-20 12:00:00 -07003450
David Benjamin861abcc2018-07-14 17:40:26 -04003451bool ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05003452 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04003453 CBB extensions;
3454 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003455 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07003456 }
3457
David Benjamin8c880a22016-12-03 02:20:34 -05003458 for (unsigned i = 0; i < kNumExtensions; i++) {
3459 if (!(hs->extensions.received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003460 // Don't send extensions that were not received.
Adam Langley614c66a2015-06-12 15:26:58 -07003461 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08003462 }
Adam Langley95c29f32014-06-20 12:00:00 -07003463
David Benjamin8c880a22016-12-03 02:20:34 -05003464 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003465 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003466 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07003467 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07003468 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003469 }
Adam Langley95c29f32014-06-20 12:00:00 -07003470
David Benjaminc11ea9422017-08-29 16:33:21 -04003471 // Discard empty extensions blocks before TLS 1.3.
David Benjamind1e3ce12017-10-06 18:31:15 -04003472 if (ssl_protocol_version(ssl) < TLS1_3_VERSION &&
Steven Valdez143e8b32016-07-11 13:19:03 -04003473 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04003474 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07003475 }
3476
David Benjamin56380462015-10-10 14:59:09 -04003477 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07003478
3479err:
Adam Langley33ad2b52015-07-20 17:43:53 -07003480 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04003481 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003482}
Adam Langley95c29f32014-06-20 12:00:00 -07003483
David Benjamin861abcc2018-07-14 17:40:26 -04003484static bool ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
3485 const SSL_CLIENT_HELLO *client_hello,
3486 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04003487 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003488 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05003489 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07003490 }
3491 }
3492
David Benjamin8c880a22016-12-03 02:20:34 -05003493 hs->extensions.received = 0;
David Benjamine14ff062016-08-09 16:21:24 -04003494 CBS extensions;
3495 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
3496 while (CBS_len(&extensions) != 0) {
3497 uint16_t type;
3498 CBS extension;
3499
David Benjaminc11ea9422017-08-29 16:33:21 -04003500 // Decode the next extension.
David Benjamine14ff062016-08-09 16:21:24 -04003501 if (!CBS_get_u16(&extensions, &type) ||
3502 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003503 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin861abcc2018-07-14 17:40:26 -04003504 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003505 }
Adam Langley95c29f32014-06-20 12:00:00 -07003506
David Benjamine14ff062016-08-09 16:21:24 -04003507 unsigned ext_index;
3508 const struct tls_extension *const ext =
3509 tls_extension_find(&ext_index, type);
David Benjamine14ff062016-08-09 16:21:24 -04003510 if (ext == NULL) {
David Benjamine14ff062016-08-09 16:21:24 -04003511 continue;
3512 }
3513
David Benjamin8c880a22016-12-03 02:20:34 -05003514 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04003515 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003516 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04003517 *out_alert = alert;
3518 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003519 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamin861abcc2018-07-14 17:40:26 -04003520 return false;
Adam Langley614c66a2015-06-12 15:26:58 -07003521 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003522 }
Adam Langley75712922014-10-10 16:23:43 -07003523
David Benjamin1deb41b2016-08-09 19:36:38 -04003524 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05003525 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04003526 continue;
3527 }
3528
3529 CBS *contents = NULL, fake_contents;
3530 static const uint8_t kFakeRenegotiateExtension[] = {0};
3531 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
3532 ssl_client_cipher_list_contains_cipher(client_hello,
3533 SSL3_CK_SCSV & 0xffff)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003534 // The renegotiation SCSV was received so pretend that we received a
3535 // renegotiation extension.
David Benjamin1deb41b2016-08-09 19:36:38 -04003536 CBS_init(&fake_contents, kFakeRenegotiateExtension,
3537 sizeof(kFakeRenegotiateExtension));
3538 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05003539 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04003540 }
3541
David Benjaminc11ea9422017-08-29 16:33:21 -04003542 // Extension wasn't observed so call the callback with a NULL
3543 // parameter.
David Benjamin1deb41b2016-08-09 19:36:38 -04003544 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003545 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04003546 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003547 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04003548 *out_alert = alert;
David Benjamin861abcc2018-07-14 17:40:26 -04003549 return false;
Adam Langley614c66a2015-06-12 15:26:58 -07003550 }
3551 }
3552
David Benjamin861abcc2018-07-14 17:40:26 -04003553 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08003554}
Adam Langley95c29f32014-06-20 12:00:00 -07003555
David Benjamin861abcc2018-07-14 17:40:26 -04003556bool ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
3557 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05003558 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08003559 int alert = SSL_AD_DECODE_ERROR;
David Benjamin861abcc2018-07-14 17:40:26 -04003560 if (!ssl_scan_clienthello_tlsext(hs, client_hello, &alert)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04003561 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin861abcc2018-07-14 17:40:26 -04003562 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003563 }
Adam Langley95c29f32014-06-20 12:00:00 -07003564
David Benjamin861abcc2018-07-14 17:40:26 -04003565 if (!ssl_check_clienthello_tlsext(hs)) {
David Benjamin3570d732015-06-29 00:28:17 -04003566 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
David Benjamin861abcc2018-07-14 17:40:26 -04003567 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003568 }
Adam Langley95c29f32014-06-20 12:00:00 -07003569
David Benjamin861abcc2018-07-14 17:40:26 -04003570 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08003571}
Adam Langley95c29f32014-06-20 12:00:00 -07003572
David Benjamin861abcc2018-07-14 17:40:26 -04003573static bool ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
3574 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05003575 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003576 // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty.
David Benjamind1e3ce12017-10-06 18:31:15 -04003577 if (CBS_len(cbs) == 0 && ssl_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjamin861abcc2018-07-14 17:40:26 -04003578 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04003579 }
Adam Langley614c66a2015-06-12 15:26:58 -07003580
David Benjaminc11ea9422017-08-29 16:33:21 -04003581 // Decode the extensions block and check it is valid.
Steven Valdez143e8b32016-07-11 13:19:03 -04003582 CBS extensions;
3583 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
3584 !tls1_check_duplicate_extensions(&extensions)) {
3585 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin861abcc2018-07-14 17:40:26 -04003586 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04003587 }
3588
3589 uint32_t received = 0;
3590 while (CBS_len(&extensions) != 0) {
3591 uint16_t type;
3592 CBS extension;
3593
David Benjaminc11ea9422017-08-29 16:33:21 -04003594 // Decode the next extension.
Steven Valdez143e8b32016-07-11 13:19:03 -04003595 if (!CBS_get_u16(&extensions, &type) ||
3596 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003597 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin861abcc2018-07-14 17:40:26 -04003598 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003599 }
Adam Langley95c29f32014-06-20 12:00:00 -07003600
Steven Valdez143e8b32016-07-11 13:19:03 -04003601 unsigned ext_index;
3602 const struct tls_extension *const ext =
3603 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07003604
Steven Valdez143e8b32016-07-11 13:19:03 -04003605 if (ext == NULL) {
David Benjamin0a3e07a2018-07-09 16:28:22 -04003606 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3607 ERR_add_error_dataf("extension %u", (unsigned)type);
3608 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin861abcc2018-07-14 17:40:26 -04003609 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04003610 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003611
David Benjamina3d76d02017-07-14 19:36:07 -04003612 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
3613 "too many bits");
David Benjamin5db7c9b2017-01-24 16:17:03 -05003614
David Benjaminc59b9aa2018-07-16 21:34:03 -04003615 if (!(hs->extensions.sent & (1u << ext_index))) {
3616 // If the extension was never sent then it is illegal.
Steven Valdez143e8b32016-07-11 13:19:03 -04003617 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3618 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04003619 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin861abcc2018-07-14 17:40:26 -04003620 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04003621 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003622
Steven Valdez143e8b32016-07-11 13:19:03 -04003623 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07003624
Steven Valdez143e8b32016-07-11 13:19:03 -04003625 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003626 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04003627 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003628 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04003629 *out_alert = alert;
David Benjamin861abcc2018-07-14 17:40:26 -04003630 return false;
Adam Langley614c66a2015-06-12 15:26:58 -07003631 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003632 }
Adam Langley95c29f32014-06-20 12:00:00 -07003633
David Benjamin54091232016-09-05 12:47:25 -04003634 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003635 if (!(received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003636 // Extension wasn't observed so call the callback with a NULL
3637 // parameter.
Adam Langley614c66a2015-06-12 15:26:58 -07003638 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003639 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003640 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003641 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07003642 *out_alert = alert;
David Benjamin861abcc2018-07-14 17:40:26 -04003643 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003644 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003645 }
3646 }
Adam Langley95c29f32014-06-20 12:00:00 -07003647
David Benjamin861abcc2018-07-14 17:40:26 -04003648 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08003649}
Adam Langley95c29f32014-06-20 12:00:00 -07003650
David Benjamin861abcc2018-07-14 17:40:26 -04003651static bool ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
David Benjamin8c880a22016-12-03 02:20:34 -05003652 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003653 int ret = SSL_TLSEXT_ERR_NOACK;
3654 int al = SSL_AD_UNRECOGNIZED_NAME;
David Benjamin46853762018-07-03 14:01:26 -04003655 if (ssl->ctx->servername_callback != 0) {
3656 ret = ssl->ctx->servername_callback(ssl, &al, ssl->ctx->servername_arg);
3657 } else if (ssl->session_ctx->servername_callback != 0) {
3658 ret = ssl->session_ctx->servername_callback(
3659 ssl, &al, ssl->session_ctx->servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003660 }
Adam Langley95c29f32014-06-20 12:00:00 -07003661
Adam Langleyfcf25832014-12-18 17:42:32 -08003662 switch (ret) {
3663 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamind1e3ce12017-10-06 18:31:15 -04003664 ssl_send_alert(ssl, SSL3_AL_FATAL, al);
David Benjamin861abcc2018-07-14 17:40:26 -04003665 return false;
Adam Langley95c29f32014-06-20 12:00:00 -07003666
Adam Langleyfcf25832014-12-18 17:42:32 -08003667 case SSL_TLSEXT_ERR_NOACK:
David Benjaminfd45ee72017-08-31 14:49:09 -04003668 hs->should_ack_sni = false;
David Benjamin861abcc2018-07-14 17:40:26 -04003669 return true;
Adam Langley95c29f32014-06-20 12:00:00 -07003670
Adam Langleyfcf25832014-12-18 17:42:32 -08003671 default:
David Benjamin861abcc2018-07-14 17:40:26 -04003672 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08003673 }
3674}
Adam Langleyed8270a2014-09-02 13:52:56 -07003675
Steven Valdez51607f12020-08-05 10:46:05 -04003676static bool ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs) {
3677 SSL *const ssl = hs->ssl;
3678 // ALPS and ALPN have a dependency between each other, so we defer checking
3679 // consistency to after the callbacks run.
3680 if (hs->new_session != nullptr && hs->new_session->has_application_settings) {
3681 // ALPN must be negotiated.
3682 if (ssl->s3->alpn_selected.empty()) {
3683 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_ALPS_WITHOUT_ALPN);
3684 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
3685 return false;
3686 }
3687
3688 // The negotiated protocol must be one of the ones we advertised for ALPS.
3689 Span<const uint8_t> settings;
3690 if (!ssl_get_local_application_settings(hs, &settings,
3691 ssl->s3->alpn_selected)) {
3692 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
3693 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
3694 return false;
3695 }
3696
3697 if (!hs->new_session->local_application_settings.CopyFrom(settings)) {
3698 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3699 return false;
3700 }
3701 }
3702
3703 return true;
3704}
3705
David Benjamin861abcc2018-07-14 17:40:26 -04003706bool ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
David Benjamin8c880a22016-12-03 02:20:34 -05003707 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08003708 int alert = SSL_AD_DECODE_ERROR;
David Benjamin861abcc2018-07-14 17:40:26 -04003709 if (!ssl_scan_serverhello_tlsext(hs, cbs, &alert)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04003710 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin861abcc2018-07-14 17:40:26 -04003711 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003712 }
3713
Steven Valdez51607f12020-08-05 10:46:05 -04003714 if (!ssl_check_serverhello_tlsext(hs)) {
3715 return false;
3716 }
3717
David Benjamin861abcc2018-07-14 17:40:26 -04003718 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08003719}
Adam Langley95c29f32014-06-20 12:00:00 -07003720
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003721static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
David Benjamin28655672018-07-18 23:23:25 -04003722 Array<uint8_t> *out, EVP_CIPHER_CTX *cipher_ctx, HMAC_CTX *hmac_ctx,
3723 Span<const uint8_t> ticket) {
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003724 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003725
David Benjaminc11ea9422017-08-29 16:33:21 -04003726 // Check the MAC at the end of the ticket.
David Benjamine3aa1d92015-06-16 15:34:50 -04003727 uint8_t mac[EVP_MAX_MD_SIZE];
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003728 size_t mac_len = HMAC_size(hmac_ctx);
David Benjamin28655672018-07-18 23:23:25 -04003729 if (ticket.size() < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003730 // The ticket must be large enough for key name, IV, data, and MAC.
David Benjamin81678aa2017-07-12 22:43:42 -04003731 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003732 }
David Benjamin28655672018-07-18 23:23:25 -04003733 // Split the ticket into the ticket and the MAC.
3734 auto ticket_mac = ticket.subspan(ticket.size() - mac_len);
3735 ticket = ticket.subspan(0, ticket.size() - mac_len);
3736 HMAC_Update(hmac_ctx, ticket.data(), ticket.size());
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003737 HMAC_Final(hmac_ctx, mac, NULL);
David Benjamin28655672018-07-18 23:23:25 -04003738 assert(mac_len == ticket_mac.size());
3739 bool mac_ok = CRYPTO_memcmp(mac, ticket_mac.data(), mac_len) == 0;
David Benjaminfbc45d72016-09-22 01:21:24 -04003740#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin861abcc2018-07-14 17:40:26 -04003741 mac_ok = true;
David Benjaminfbc45d72016-09-22 01:21:24 -04003742#endif
3743 if (!mac_ok) {
David Benjamin81678aa2017-07-12 22:43:42 -04003744 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003745 }
3746
David Benjaminc11ea9422017-08-29 16:33:21 -04003747 // Decrypt the session data.
David Benjamin28655672018-07-18 23:23:25 -04003748 auto ciphertext = ticket.subspan(SSL_TICKET_KEY_NAME_LEN + iv_len);
3749 Array<uint8_t> plaintext;
3750#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3751 if (!plaintext.CopyFrom(ciphertext)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003752 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08003753 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003754#else
David Benjamin28655672018-07-18 23:23:25 -04003755 if (ciphertext.size() >= INT_MAX) {
David Benjamin81678aa2017-07-12 22:43:42 -04003756 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003757 }
David Benjamin28655672018-07-18 23:23:25 -04003758 if (!plaintext.Init(ciphertext.size())) {
3759 return ssl_ticket_aead_error;
3760 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003761 int len1, len2;
David Benjamin28655672018-07-18 23:23:25 -04003762 if (!EVP_DecryptUpdate(cipher_ctx, plaintext.data(), &len1, ciphertext.data(),
3763 (int)ciphertext.size()) ||
3764 !EVP_DecryptFinal_ex(cipher_ctx, plaintext.data() + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003765 ERR_clear_error();
David Benjamin81678aa2017-07-12 22:43:42 -04003766 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003767 }
David Benjamin28655672018-07-18 23:23:25 -04003768 plaintext.Shrink(static_cast<size_t>(len1) + len2);
David Benjaminfbc45d72016-09-22 01:21:24 -04003769#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003770
David Benjamin28655672018-07-18 23:23:25 -04003771 *out = std::move(plaintext);
David Benjamin81678aa2017-07-12 22:43:42 -04003772 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08003773}
3774
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003775static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
David Benjamin28655672018-07-18 23:23:25 -04003776 SSL_HANDSHAKE *hs, Array<uint8_t> *out, bool *out_renew_ticket,
3777 Span<const uint8_t> ticket) {
3778 assert(ticket.size() >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003779 ScopedEVP_CIPHER_CTX cipher_ctx;
3780 ScopedHMAC_CTX hmac_ctx;
David Benjamin28655672018-07-18 23:23:25 -04003781 auto name = ticket.subspan(0, SSL_TICKET_KEY_NAME_LEN);
3782 // The actual IV is shorter, but the length is determined by the callback's
3783 // chosen cipher. Instead we pass in |EVP_MAX_IV_LENGTH| worth of IV to ensure
3784 // the callback has enough.
3785 auto iv = ticket.subspan(SSL_TICKET_KEY_NAME_LEN, EVP_MAX_IV_LENGTH);
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003786 int cb_ret = hs->ssl->session_ctx->ticket_key_cb(
David Benjamin28655672018-07-18 23:23:25 -04003787 hs->ssl, const_cast<uint8_t *>(name.data()),
3788 const_cast<uint8_t *>(iv.data()), cipher_ctx.get(), hmac_ctx.get(),
3789 0 /* decrypt */);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003790 if (cb_ret < 0) {
3791 return ssl_ticket_aead_error;
3792 } else if (cb_ret == 0) {
3793 return ssl_ticket_aead_ignore_ticket;
3794 } else if (cb_ret == 2) {
David Benjaminfd45ee72017-08-31 14:49:09 -04003795 *out_renew_ticket = true;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003796 } else {
3797 assert(cb_ret == 1);
3798 }
David Benjamin28655672018-07-18 23:23:25 -04003799 return decrypt_ticket_with_cipher_ctx(out, cipher_ctx.get(), hmac_ctx.get(),
3800 ticket);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003801}
3802
3803static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
David Benjamin28655672018-07-18 23:23:25 -04003804 SSL_HANDSHAKE *hs, Array<uint8_t> *out, Span<const uint8_t> ticket) {
3805 assert(ticket.size() >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
David Benjamin50596f82018-07-02 19:47:27 -04003806 SSL_CTX *ctx = hs->ssl->session_ctx.get();
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003807
David Benjaminc11ea9422017-08-29 16:33:21 -04003808 // Rotate the ticket key if necessary.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003809 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
3810 return ssl_ticket_aead_error;
3811 }
3812
David Benjamin28655672018-07-18 23:23:25 -04003813 const EVP_CIPHER *cipher = EVP_aes_128_cbc();
3814 auto name = ticket.subspan(0, SSL_TICKET_KEY_NAME_LEN);
3815 auto iv =
3816 ticket.subspan(SSL_TICKET_KEY_NAME_LEN, EVP_CIPHER_iv_length(cipher));
3817
David Benjaminc11ea9422017-08-29 16:33:21 -04003818 // Pick the matching ticket key and decrypt.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003819 ScopedEVP_CIPHER_CTX cipher_ctx;
3820 ScopedHMAC_CTX hmac_ctx;
3821 {
3822 MutexReadLock lock(&ctx->lock);
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003823 const TicketKey *key;
David Benjamin28655672018-07-18 23:23:25 -04003824 if (ctx->ticket_key_current && name == ctx->ticket_key_current->name) {
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003825 key = ctx->ticket_key_current.get();
David Benjamin28655672018-07-18 23:23:25 -04003826 } else if (ctx->ticket_key_prev && name == ctx->ticket_key_prev->name) {
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003827 key = ctx->ticket_key_prev.get();
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003828 } else {
3829 return ssl_ticket_aead_ignore_ticket;
3830 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003831 if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
3832 tlsext_tick_md(), NULL) ||
David Benjamin28655672018-07-18 23:23:25 -04003833 !EVP_DecryptInit_ex(cipher_ctx.get(), cipher, NULL,
3834 key->aes_key, iv.data())) {
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003835 return ssl_ticket_aead_error;
3836 }
3837 }
David Benjamin28655672018-07-18 23:23:25 -04003838 return decrypt_ticket_with_cipher_ctx(out, cipher_ctx.get(), hmac_ctx.get(),
3839 ticket);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003840}
3841
Adam Langley4c341d02017-03-08 19:33:21 -08003842static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
David Benjamin28655672018-07-18 23:23:25 -04003843 SSL_HANDSHAKE *hs, Array<uint8_t> *out, bool *out_renew_ticket,
3844 Span<const uint8_t> ticket) {
3845 Array<uint8_t> plaintext;
3846 if (!plaintext.Init(ticket.size())) {
Adam Langley4c341d02017-03-08 19:33:21 -08003847 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3848 return ssl_ticket_aead_error;
3849 }
3850
3851 size_t plaintext_len;
3852 const enum ssl_ticket_aead_result_t result =
David Benjamin98472cb2018-05-02 16:05:36 -04003853 hs->ssl->session_ctx->ticket_aead_method->open(
David Benjamin28655672018-07-18 23:23:25 -04003854 hs->ssl, plaintext.data(), &plaintext_len, ticket.size(),
3855 ticket.data(), ticket.size());
3856 if (result != ssl_ticket_aead_success) {
3857 return result;
Adam Langley4c341d02017-03-08 19:33:21 -08003858 }
3859
David Benjamin28655672018-07-18 23:23:25 -04003860 plaintext.Shrink(plaintext_len);
3861 *out = std::move(plaintext);
3862 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08003863}
3864
3865enum ssl_ticket_aead_result_t ssl_process_ticket(
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003866 SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session,
David Benjamin28655672018-07-18 23:23:25 -04003867 bool *out_renew_ticket, Span<const uint8_t> ticket,
3868 Span<const uint8_t> session_id) {
David Benjaminb571e772021-03-25 19:42:16 -04003869 SSL *const ssl = hs->ssl;
David Benjaminfd45ee72017-08-31 14:49:09 -04003870 *out_renew_ticket = false;
David Benjamin37af90f2017-07-29 01:42:16 -04003871 out_session->reset();
Adam Langley4c341d02017-03-08 19:33:21 -08003872
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003873 if ((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) ||
David Benjamin28655672018-07-18 23:23:25 -04003874 session_id.size() > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langley4c341d02017-03-08 19:33:21 -08003875 return ssl_ticket_aead_ignore_ticket;
3876 }
3877
David Benjaminb571e772021-03-25 19:42:16 -04003878 // Tickets in TLS 1.3 are tied into pre-shared keys (PSKs), unlike in TLS 1.2
3879 // where that concept doesn't exist. The |decrypted_psk| and |ignore_psk|
3880 // hints only apply to PSKs. We check the version to determine which this is.
3881 const bool is_psk = ssl_protocol_version(ssl) >= TLS1_3_VERSION;
3882
David Benjamin28655672018-07-18 23:23:25 -04003883 Array<uint8_t> plaintext;
Adam Langley4c341d02017-03-08 19:33:21 -08003884 enum ssl_ticket_aead_result_t result;
David Benjaminb571e772021-03-25 19:42:16 -04003885 SSL_HANDSHAKE_HINTS *const hints = hs->hints.get();
3886 if (is_psk && hints && !hs->hints_requested &&
3887 !hints->decrypted_psk.empty()) {
3888 result = plaintext.CopyFrom(hints->decrypted_psk) ? ssl_ticket_aead_success
3889 : ssl_ticket_aead_error;
3890 } else if (is_psk && hints && !hs->hints_requested && hints->ignore_psk) {
3891 result = ssl_ticket_aead_ignore_ticket;
3892 } else if (ssl->session_ctx->ticket_aead_method != NULL) {
David Benjamin28655672018-07-18 23:23:25 -04003893 result = ssl_decrypt_ticket_with_method(hs, &plaintext, out_renew_ticket,
3894 ticket);
Adam Langley4c341d02017-03-08 19:33:21 -08003895 } else {
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003896 // Ensure there is room for the key name and the largest IV |ticket_key_cb|
3897 // may try to consume. The real limit may be lower, but the maximum IV
3898 // length should be well under the minimum size for the session material and
3899 // HMAC.
David Benjamin28655672018-07-18 23:23:25 -04003900 if (ticket.size() < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
David Benjaminb571e772021-03-25 19:42:16 -04003901 result = ssl_ticket_aead_ignore_ticket;
3902 } else if (ssl->session_ctx->ticket_key_cb != NULL) {
David Benjamin28655672018-07-18 23:23:25 -04003903 result =
3904 ssl_decrypt_ticket_with_cb(hs, &plaintext, out_renew_ticket, ticket);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003905 } else {
David Benjamin28655672018-07-18 23:23:25 -04003906 result = ssl_decrypt_ticket_with_ticket_keys(hs, &plaintext, ticket);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003907 }
Adam Langley4c341d02017-03-08 19:33:21 -08003908 }
3909
David Benjaminb571e772021-03-25 19:42:16 -04003910 if (is_psk && hints && hs->hints_requested) {
3911 if (result == ssl_ticket_aead_ignore_ticket) {
3912 hints->ignore_psk = true;
3913 } else if (result == ssl_ticket_aead_success &&
3914 !hints->decrypted_psk.CopyFrom(plaintext)) {
3915 return ssl_ticket_aead_error;
3916 }
3917 }
3918
Adam Langley4c341d02017-03-08 19:33:21 -08003919 if (result != ssl_ticket_aead_success) {
3920 return result;
3921 }
3922
David Benjaminc11ea9422017-08-29 16:33:21 -04003923 // Decode the session.
David Benjamin28655672018-07-18 23:23:25 -04003924 UniquePtr<SSL_SESSION> session(SSL_SESSION_from_bytes(
David Benjaminb571e772021-03-25 19:42:16 -04003925 plaintext.data(), plaintext.size(), ssl->ctx.get()));
David Benjamin37af90f2017-07-29 01:42:16 -04003926 if (!session) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003927 ERR_clear_error(); // Don't leave an error on the queue.
Adam Langley4c341d02017-03-08 19:33:21 -08003928 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003929 }
3930
Adam Langley47cefed2021-05-26 13:36:40 -07003931 // Envoy's tests expect the session to have a session ID that matches the
3932 // placeholder used by the client. It's unclear whether this is a good idea,
3933 // but we maintain it for now.
3934 SHA256(ticket.data(), ticket.size(), session->session_id);
3935 // Other consumers may expect a non-empty session ID to indicate resumption.
3936 session->session_id_length = SHA256_DIGEST_LENGTH;
David Benjamine3aa1d92015-06-16 15:34:50 -04003937
David Benjamin37af90f2017-07-29 01:42:16 -04003938 *out_session = std::move(session);
Adam Langley4c341d02017-03-08 19:33:21 -08003939 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003940}
Adam Langley95c29f32014-06-20 12:00:00 -07003941
David Benjamin610cdbb2018-01-22 19:08:38 -05003942bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003943 // Extension ignored for inappropriate versions
David Benjamind1e3ce12017-10-06 18:31:15 -04003944 if (ssl_protocol_version(hs->ssl) < TLS1_2_VERSION) {
David Benjamin610cdbb2018-01-22 19:08:38 -05003945 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08003946 }
David Benjamincd996942014-07-20 16:23:51 -04003947
David Benjamin602f4662018-12-07 12:06:22 -06003948 // In all contexts, the signature algorithms list may not be empty. (It may be
3949 // omitted by clients in TLS 1.2, but then the entire extension is omitted.)
3950 return CBS_len(in_sigalgs) != 0 &&
3951 parse_u16_array(in_sigalgs, &hs->peer_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08003952}
David Benjaminec2f27d2014-11-13 19:17:25 -05003953
David Benjamin610cdbb2018-01-22 19:08:38 -05003954bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
David Benjamina3651382017-04-20 17:49:36 -04003955 switch (EVP_PKEY_id(pkey)) {
3956 case EVP_PKEY_RSA:
3957 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin610cdbb2018-01-22 19:08:38 -05003958 return true;
David Benjamina3651382017-04-20 17:49:36 -04003959 case EVP_PKEY_EC:
3960 *out = SSL_SIGN_ECDSA_SHA1;
David Benjamin610cdbb2018-01-22 19:08:38 -05003961 return true;
David Benjamina3651382017-04-20 17:49:36 -04003962 default:
David Benjamin610cdbb2018-01-22 19:08:38 -05003963 return false;
David Benjamina3651382017-04-20 17:49:36 -04003964 }
3965}
3966
David Benjamin610cdbb2018-01-22 19:08:38 -05003967bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003968 SSL *const ssl = hs->ssl;
David Benjamin0ce090a2018-07-02 20:24:40 -04003969 CERT *cert = hs->config->cert.get();
Christopher Patton6c1b3762018-07-17 12:49:41 -07003970 DC *dc = cert->dc.get();
David Benjaminec2f27d2014-11-13 19:17:25 -05003971
David Benjaminc11ea9422017-08-29 16:33:21 -04003972 // Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3973 // handshake.
David Benjamind1e3ce12017-10-06 18:31:15 -04003974 if (ssl_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin31b0c9b2017-07-20 14:49:15 -04003975 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
David Benjamina3651382017-04-20 17:49:36 -04003976 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
David Benjamin610cdbb2018-01-22 19:08:38 -05003977 return false;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003978 }
David Benjamin610cdbb2018-01-22 19:08:38 -05003979 return true;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003980 }
3981
David Benjaminb1cf48e2017-09-21 11:37:46 -04003982 Span<const uint16_t> sigalgs = kSignSignatureAlgorithms;
Christopher Patton6c1b3762018-07-17 12:49:41 -07003983 if (ssl_signing_with_dc(hs)) {
3984 sigalgs = MakeConstSpan(&dc->expected_cert_verify_algorithm, 1);
3985 } else if (!cert->sigalgs.empty()) {
David Benjamine325c3f2018-04-12 16:11:15 -04003986 sigalgs = cert->sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003987 }
3988
Christopher Patton6c1b3762018-07-17 12:49:41 -07003989 Span<const uint16_t> peer_sigalgs = tls1_get_peer_verify_algorithms(hs);
David Benjaminea9a0d52016-07-08 15:52:59 -07003990
David Benjaminb1cf48e2017-09-21 11:37:46 -04003991 for (uint16_t sigalg : sigalgs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003992 // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3993 // negotiated.
David Benjamin1fb125c2016-07-08 18:52:12 -07003994 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjaminb1cf48e2017-09-21 11:37:46 -04003995 !ssl_private_key_supports_signature_algorithm(hs, sigalg)) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003996 continue;
3997 }
3998
David Benjaminb1cf48e2017-09-21 11:37:46 -04003999 for (uint16_t peer_sigalg : peer_sigalgs) {
4000 if (sigalg == peer_sigalg) {
David Benjamin1fb125c2016-07-08 18:52:12 -07004001 *out = sigalg;
David Benjamin610cdbb2018-01-22 19:08:38 -05004002 return true;
Steven Valdezf0451ca2016-06-29 13:16:27 -04004003 }
Adam Langleyfcf25832014-12-18 17:42:32 -08004004 }
Adam Langleyfcf25832014-12-18 17:42:32 -08004005 }
Adam Langley95c29f32014-06-20 12:00:00 -07004006
David Benjaminea9a0d52016-07-08 15:52:59 -07004007 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
David Benjamin610cdbb2018-01-22 19:08:38 -05004008 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08004009}
Adam Langley95c29f32014-06-20 12:00:00 -07004010
Christopher Patton6c1b3762018-07-17 12:49:41 -07004011Span<const uint16_t> tls1_get_peer_verify_algorithms(const SSL_HANDSHAKE *hs) {
4012 Span<const uint16_t> peer_sigalgs = hs->peer_sigalgs;
4013 if (peer_sigalgs.empty() && ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) {
4014 // If the client didn't specify any signature_algorithms extension then
4015 // we can assume that it supports SHA1. See
4016 // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
4017 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
4018 SSL_SIGN_ECDSA_SHA1};
4019 peer_sigalgs = kDefaultPeerAlgorithms;
4020 }
4021 return peer_sigalgs;
4022}
4023
David Benjamin861abcc2018-07-14 17:40:26 -04004024bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
Steven Valdez908ac192017-01-12 13:17:07 -05004025 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04004026 // A Channel ID handshake message is structured to contain multiple
4027 // extensions, but the only one that can be present is Channel ID.
David Benjamin7934f082017-08-01 16:32:25 -04004028 uint16_t extension_type;
4029 CBS channel_id = msg.body, extension;
Nick Harper60a85cb2016-09-23 16:25:11 -07004030 if (!CBS_get_u16(&channel_id, &extension_type) ||
4031 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
4032 CBS_len(&channel_id) != 0 ||
4033 extension_type != TLSEXT_TYPE_channel_id ||
4034 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
4035 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04004036 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04004037 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07004038 }
4039
David Benjamin86e95b82017-07-18 16:34:25 -04004040 UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Nick Harper60a85cb2016-09-23 16:25:11 -07004041 if (!p256) {
4042 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
David Benjamin861abcc2018-07-14 17:40:26 -04004043 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07004044 }
4045
David Benjamin86e95b82017-07-18 16:34:25 -04004046 UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
4047 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
David Benjamin81678aa2017-07-12 22:43:42 -04004048 if (!sig || !x || !y) {
David Benjamin861abcc2018-07-14 17:40:26 -04004049 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07004050 }
4051
4052 const uint8_t *p = CBS_data(&extension);
David Benjamin81678aa2017-07-12 22:43:42 -04004053 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
4054 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
4055 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
4056 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
David Benjamin861abcc2018-07-14 17:40:26 -04004057 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07004058 }
4059
David Benjamin86e95b82017-07-18 16:34:25 -04004060 UniquePtr<EC_KEY> key(EC_KEY_new());
4061 UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
David Benjamin81678aa2017-07-12 22:43:42 -04004062 if (!key || !point ||
4063 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
4064 y.get(), nullptr) ||
4065 !EC_KEY_set_group(key.get(), p256.get()) ||
4066 !EC_KEY_set_public_key(key.get(), point.get())) {
David Benjamin861abcc2018-07-14 17:40:26 -04004067 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07004068 }
4069
4070 uint8_t digest[EVP_MAX_MD_SIZE];
4071 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05004072 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjamin861abcc2018-07-14 17:40:26 -04004073 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07004074 }
4075
David Benjamin861abcc2018-07-14 17:40:26 -04004076 bool sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
Nick Harper60a85cb2016-09-23 16:25:11 -07004077#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin861abcc2018-07-14 17:40:26 -04004078 sig_ok = true;
David Benjamind90b8032017-12-18 16:47:51 -05004079 ERR_clear_error();
Nick Harper60a85cb2016-09-23 16:25:11 -07004080#endif
4081 if (!sig_ok) {
4082 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
David Benjamind1e3ce12017-10-06 18:31:15 -04004083 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
David Benjamin46853762018-07-03 14:01:26 -04004084 ssl->s3->channel_id_valid = false;
David Benjamin861abcc2018-07-14 17:40:26 -04004085 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07004086 }
4087
David Benjamin46853762018-07-03 14:01:26 -04004088 OPENSSL_memcpy(ssl->s3->channel_id, p, 64);
David Benjamin861abcc2018-07-14 17:40:26 -04004089 return true;
Nick Harper60a85cb2016-09-23 16:25:11 -07004090}
4091
David Benjaminf1db1a32017-10-27 01:12:34 -04004092bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
Nick Harper60a85cb2016-09-23 16:25:11 -07004093 uint8_t digest[EVP_MAX_MD_SIZE];
4094 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05004095 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjaminf1db1a32017-10-27 01:12:34 -04004096 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07004097 }
4098
David Benjamin46853762018-07-03 14:01:26 -04004099 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(hs->config->channel_id_private.get());
David Benjaminf1db1a32017-10-27 01:12:34 -04004100 if (ec_key == nullptr) {
Nick Harper60a85cb2016-09-23 16:25:11 -07004101 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminf1db1a32017-10-27 01:12:34 -04004102 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07004103 }
4104
David Benjaminf1db1a32017-10-27 01:12:34 -04004105 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
4106 if (!x || !y ||
Nick Harper60a85cb2016-09-23 16:25:11 -07004107 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
4108 EC_KEY_get0_public_key(ec_key),
David Benjaminf1db1a32017-10-27 01:12:34 -04004109 x.get(), y.get(), nullptr)) {
4110 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07004111 }
4112
David Benjaminf1db1a32017-10-27 01:12:34 -04004113 UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign(digest, digest_len, ec_key));
4114 if (!sig) {
4115 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07004116 }
4117
4118 CBB child;
4119 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
4120 !CBB_add_u16_length_prefixed(cbb, &child) ||
David Benjaminf1db1a32017-10-27 01:12:34 -04004121 !BN_bn2cbb_padded(&child, 32, x.get()) ||
4122 !BN_bn2cbb_padded(&child, 32, y.get()) ||
Nick Harper60a85cb2016-09-23 16:25:11 -07004123 !BN_bn2cbb_padded(&child, 32, sig->r) ||
4124 !BN_bn2cbb_padded(&child, 32, sig->s) ||
4125 !CBB_flush(cbb)) {
David Benjaminf1db1a32017-10-27 01:12:34 -04004126 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07004127 }
4128
David Benjaminf1db1a32017-10-27 01:12:34 -04004129 return true;
Nick Harper60a85cb2016-09-23 16:25:11 -07004130}
4131
David Benjamin861abcc2018-07-14 17:40:26 -04004132bool tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
Steven Valdez908ac192017-01-12 13:17:07 -05004133 SSL *const ssl = hs->ssl;
David Benjamind1e3ce12017-10-06 18:31:15 -04004134 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin75a1f232017-10-11 17:19:19 -04004135 Array<uint8_t> msg;
4136 if (!tls13_get_cert_verify_signature_input(hs, &msg,
Nick Harper60a85cb2016-09-23 16:25:11 -07004137 ssl_cert_verify_channel_id)) {
David Benjamin861abcc2018-07-14 17:40:26 -04004138 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07004139 }
David Benjamin75a1f232017-10-11 17:19:19 -04004140 SHA256(msg.data(), msg.size(), out);
Nick Harper60a85cb2016-09-23 16:25:11 -07004141 *out_len = SHA256_DIGEST_LENGTH;
David Benjamin861abcc2018-07-14 17:40:26 -04004142 return true;
Nick Harper60a85cb2016-09-23 16:25:11 -07004143 }
4144
Nick Harper95594012016-10-20 14:07:13 -07004145 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08004146
Nick Harper95594012016-10-20 14:07:13 -07004147 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04004148 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07004149 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04004150
Steven Valdez87eab492016-06-27 16:34:59 -04004151 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04004152 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07004153 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04004154 if (ssl->session->original_handshake_hash_len == 0) {
4155 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04004156 return false;
David Benjamind6a4ae92015-08-06 11:10:51 -04004157 }
Nick Harper95594012016-10-20 14:07:13 -07004158 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
4159 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04004160 }
4161
Steven Valdez908ac192017-01-12 13:17:07 -05004162 uint8_t hs_hash[EVP_MAX_MD_SIZE];
4163 size_t hs_hash_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04004164 if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
David Benjamin861abcc2018-07-14 17:40:26 -04004165 return false;
David Benjamind6a4ae92015-08-06 11:10:51 -04004166 }
Steven Valdez908ac192017-01-12 13:17:07 -05004167 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07004168 SHA256_Final(out, &ctx);
4169 *out_len = SHA256_DIGEST_LENGTH;
David Benjamin861abcc2018-07-14 17:40:26 -04004170 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08004171}
Adam Langley1258b6a2014-06-20 12:00:00 -07004172
David Benjamin861abcc2018-07-14 17:40:26 -04004173bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
Steven Valdez908ac192017-01-12 13:17:07 -05004174 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04004175 // This function should never be called for a resumed session because the
4176 // handshake hashes that we wish to record are for the original, full
4177 // handshake.
Steven Valdez87eab492016-06-27 16:34:59 -04004178 if (ssl->session != NULL) {
David Benjamin861abcc2018-07-14 17:40:26 -04004179 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08004180 }
Adam Langley1258b6a2014-06-20 12:00:00 -07004181
David Benjamina3d76d02017-07-14 19:36:07 -04004182 static_assert(
David Benjamin45738dd2017-02-09 20:01:26 -05004183 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
David Benjamina3d76d02017-07-14 19:36:07 -04004184 "original_handshake_hash is too small");
Steven Valdez908ac192017-01-12 13:17:07 -05004185
4186 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04004187 if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
4188 &digest_len)) {
David Benjamin861abcc2018-07-14 17:40:26 -04004189 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08004190 }
Adam Langley1258b6a2014-06-20 12:00:00 -07004191
David Benjamina3d76d02017-07-14 19:36:07 -04004192 static_assert(EVP_MAX_MD_SIZE <= 0xff,
4193 "EVP_MAX_MD_SIZE does not fit in uint8_t");
David Benjamin45738dd2017-02-09 20:01:26 -05004194 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07004195
David Benjamin861abcc2018-07-14 17:40:26 -04004196 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08004197}
Nick Harper60a85cb2016-09-23 16:25:11 -07004198
David Benjamin861abcc2018-07-14 17:40:26 -04004199bool ssl_do_channel_id_callback(SSL_HANDSHAKE *hs) {
David Benjamin46853762018-07-03 14:01:26 -04004200 if (hs->config->channel_id_private != NULL ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07004201 hs->ssl->ctx->channel_id_cb == NULL) {
David Benjamin861abcc2018-07-14 17:40:26 -04004202 return true;
Nick Harper60a85cb2016-09-23 16:25:11 -07004203 }
4204
4205 EVP_PKEY *key = NULL;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07004206 hs->ssl->ctx->channel_id_cb(hs->ssl, &key);
Nick Harper60a85cb2016-09-23 16:25:11 -07004207 if (key == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04004208 // The caller should try again later.
David Benjamin861abcc2018-07-14 17:40:26 -04004209 return true;
Nick Harper60a85cb2016-09-23 16:25:11 -07004210 }
4211
David Benjamin861abcc2018-07-14 17:40:26 -04004212 UniquePtr<EVP_PKEY> free_key(key);
4213 return SSL_set1_tls_channel_id(hs->ssl, key);
Nick Harper60a85cb2016-09-23 16:25:11 -07004214}
Adam Langleycfa08c32016-11-17 13:21:27 -08004215
David Benjamin861abcc2018-07-14 17:40:26 -04004216bool ssl_is_sct_list_valid(const CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04004217 // Shallow parse the SCT list for sanity. By the RFC
4218 // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
4219 // of the SCTs may be empty.
Adam Langleycfa08c32016-11-17 13:21:27 -08004220 CBS copy = *contents;
4221 CBS sct_list;
4222 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
4223 CBS_len(&copy) != 0 ||
4224 CBS_len(&sct_list) == 0) {
David Benjamin861abcc2018-07-14 17:40:26 -04004225 return false;
Adam Langleycfa08c32016-11-17 13:21:27 -08004226 }
4227
4228 while (CBS_len(&sct_list) > 0) {
4229 CBS sct;
4230 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
4231 CBS_len(&sct) == 0) {
David Benjamin861abcc2018-07-14 17:40:26 -04004232 return false;
Adam Langleycfa08c32016-11-17 13:21:27 -08004233 }
4234 }
4235
David Benjamin861abcc2018-07-14 17:40:26 -04004236 return true;
Adam Langleycfa08c32016-11-17 13:21:27 -08004237}
David Benjamin86e95b82017-07-18 16:34:25 -04004238
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07004239BSSL_NAMESPACE_END
David Benjamin86e95b82017-07-18 16:34:25 -04004240
4241using namespace bssl;
4242
4243int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
4244 uint16_t extension_type,
4245 const uint8_t **out_data,
4246 size_t *out_len) {
4247 CBS cbs;
4248 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
4249 return 0;
4250 }
4251
4252 *out_data = CBS_data(&cbs);
4253 *out_len = CBS_len(&cbs);
4254 return 1;
4255}