blob: 4a2bbcf8684b71871cfcd2b420075a6c81cc151a [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
David Benjamin499742c2017-07-22 12:45:38 -0400116#include <utility>
117
David Benjamin03973092014-06-24 23:27:17 -0400118#include <openssl/bytestring.h>
Adam Langley512a2892017-12-30 08:04:39 -0800119#include <openssl/chacha.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400120#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400121#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700122#include <openssl/evp.h>
123#include <openssl/hmac.h>
124#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400125#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700126#include <openssl/rand.h>
127
David Benjamin2ee94aa2015-04-07 22:38:30 -0400128#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400129#include "../crypto/internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800130
131
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700132BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -0400133
David Benjamin861abcc2018-07-14 17:40:26 -0400134static bool ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
Adam Langley95c29f32014-06-20 12:00:00 -0700135
Adam Langleyfcf25832014-12-18 17:42:32 -0800136static int compare_uint16_t(const void *p1, const void *p2) {
137 uint16_t u1 = *((const uint16_t *)p1);
138 uint16_t u2 = *((const uint16_t *)p2);
139 if (u1 < u2) {
140 return -1;
141 } else if (u1 > u2) {
142 return 1;
143 } else {
144 return 0;
145 }
146}
David Benjamin35a7a442014-07-05 00:23:20 -0400147
David Benjaminc11ea9422017-08-29 16:33:21 -0400148// Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
149// more than one extension of the same type in a ClientHello or ServerHello.
150// This function does an initial scan over the extensions block to filter those
151// out.
David Benjamin861abcc2018-07-14 17:40:26 -0400152static bool tls1_check_duplicate_extensions(const CBS *cbs) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400153 // First pass: count the extensions.
David Benjamin08f5c762017-09-21 02:43:05 -0400154 size_t num_extensions = 0;
155 CBS extensions = *cbs;
Adam Langleyfcf25832014-12-18 17:42:32 -0800156 while (CBS_len(&extensions) > 0) {
157 uint16_t type;
158 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400159
Adam Langleyfcf25832014-12-18 17:42:32 -0800160 if (!CBS_get_u16(&extensions, &type) ||
161 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
David Benjamin861abcc2018-07-14 17:40:26 -0400162 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800163 }
David Benjamin35a7a442014-07-05 00:23:20 -0400164
Adam Langleyfcf25832014-12-18 17:42:32 -0800165 num_extensions++;
166 }
David Benjamin35a7a442014-07-05 00:23:20 -0400167
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 if (num_extensions == 0) {
David Benjamin861abcc2018-07-14 17:40:26 -0400169 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800170 }
David Benjamin9a373592014-07-25 04:27:53 -0400171
David Benjamin08f5c762017-09-21 02:43:05 -0400172 Array<uint16_t> extension_types;
173 if (!extension_types.Init(num_extensions)) {
David Benjamin861abcc2018-07-14 17:40:26 -0400174 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800175 }
David Benjamin35a7a442014-07-05 00:23:20 -0400176
David Benjaminc11ea9422017-08-29 16:33:21 -0400177 // Second pass: gather the extension types.
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 extensions = *cbs;
David Benjamin08f5c762017-09-21 02:43:05 -0400179 for (size_t i = 0; i < extension_types.size(); i++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800180 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400181
Adam Langleyfcf25832014-12-18 17:42:32 -0800182 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
183 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400184 // This should not happen.
David Benjamin861abcc2018-07-14 17:40:26 -0400185 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800186 }
187 }
188 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400189
David Benjaminc11ea9422017-08-29 16:33:21 -0400190 // Sort the extensions and make sure there are no duplicates.
David Benjamin08f5c762017-09-21 02:43:05 -0400191 qsort(extension_types.data(), extension_types.size(), sizeof(uint16_t),
192 compare_uint16_t);
193 for (size_t i = 1; i < num_extensions; i++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800194 if (extension_types[i - 1] == extension_types[i]) {
David Benjamin861abcc2018-07-14 17:40:26 -0400195 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800196 }
197 }
David Benjamin35a7a442014-07-05 00:23:20 -0400198
David Benjamin861abcc2018-07-14 17:40:26 -0400199 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800200}
David Benjamin35a7a442014-07-05 00:23:20 -0400201
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +0000202static bool is_post_quantum_group(uint16_t id) {
Adam Langley7f028812019-10-18 14:48:11 -0700203 return id == SSL_CURVE_CECPQ2;
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +0000204}
205
Adam Langleyc9827e02019-04-12 14:46:50 -0700206bool ssl_client_hello_init(const SSL *ssl, SSL_CLIENT_HELLO *out,
David Benjamin861abcc2018-07-14 17:40:26 -0400207 const SSLMessage &msg) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500208 OPENSSL_memset(out, 0, sizeof(*out));
Adam Langleyc9827e02019-04-12 14:46:50 -0700209 out->ssl = const_cast<SSL *>(ssl);
David Benjamin7934f082017-08-01 16:32:25 -0400210 out->client_hello = CBS_data(&msg.body);
211 out->client_hello_len = CBS_len(&msg.body);
David Benjamin8f2c20e2014-07-09 09:30:38 -0400212
David Benjamine14ff062016-08-09 16:21:24 -0400213 CBS client_hello, random, session_id;
David Benjamin731058e2016-12-03 23:15:13 -0500214 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
215 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjamine14ff062016-08-09 16:21:24 -0400216 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
217 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
218 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
David Benjamin861abcc2018-07-14 17:40:26 -0400219 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800220 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700221
David Benjamin731058e2016-12-03 23:15:13 -0500222 out->random = CBS_data(&random);
223 out->random_len = CBS_len(&random);
224 out->session_id = CBS_data(&session_id);
225 out->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700226
David Benjaminc11ea9422017-08-29 16:33:21 -0400227 // Skip past DTLS cookie
David Benjamin731058e2016-12-03 23:15:13 -0500228 if (SSL_is_dtls(out->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400230 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
231 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
David Benjamin861abcc2018-07-14 17:40:26 -0400232 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800233 }
234 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700235
David Benjamine14ff062016-08-09 16:21:24 -0400236 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400238 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
239 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 CBS_len(&compression_methods) < 1) {
David Benjamin861abcc2018-07-14 17:40:26 -0400241 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 }
David Benjamine14ff062016-08-09 16:21:24 -0400243
David Benjamin731058e2016-12-03 23:15:13 -0500244 out->cipher_suites = CBS_data(&cipher_suites);
245 out->cipher_suites_len = CBS_len(&cipher_suites);
246 out->compression_methods = CBS_data(&compression_methods);
247 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700248
David Benjaminc11ea9422017-08-29 16:33:21 -0400249 // If the ClientHello ends here then it's valid, but doesn't have any
David Benjamin9bb15f52018-06-26 00:07:40 -0400250 // extensions.
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 if (CBS_len(&client_hello) == 0) {
David Benjamin731058e2016-12-03 23:15:13 -0500252 out->extensions = NULL;
253 out->extensions_len = 0;
David Benjamin861abcc2018-07-14 17:40:26 -0400254 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700256
David Benjaminc11ea9422017-08-29 16:33:21 -0400257 // Extract extensions and check it is valid.
David Benjamine14ff062016-08-09 16:21:24 -0400258 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
260 !tls1_check_duplicate_extensions(&extensions) ||
261 CBS_len(&client_hello) != 0) {
David Benjamin861abcc2018-07-14 17:40:26 -0400262 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 }
David Benjamine14ff062016-08-09 16:21:24 -0400264
David Benjamin731058e2016-12-03 23:15:13 -0500265 out->extensions = CBS_data(&extensions);
266 out->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700267
David Benjamin861abcc2018-07-14 17:40:26 -0400268 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800269}
Adam Langleydc9b1412014-06-20 12:00:00 -0700270
David Benjamin861abcc2018-07-14 17:40:26 -0400271bool ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
272 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 CBS extensions;
David Benjamin731058e2016-12-03 23:15:13 -0500274 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 while (CBS_len(&extensions) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400276 // Decode the next extension.
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 uint16_t type;
278 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 if (!CBS_get_u16(&extensions, &type) ||
280 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
David Benjamin861abcc2018-07-14 17:40:26 -0400281 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800282 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400285 *out = extension;
David Benjamin861abcc2018-07-14 17:40:26 -0400286 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800287 }
288 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700289
David Benjamin861abcc2018-07-14 17:40:26 -0400290 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800291}
Adam Langley95c29f32014-06-20 12:00:00 -0700292
Steven Valdezce902a92016-05-17 11:47:53 -0400293static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400294 SSL_CURVE_X25519,
295 SSL_CURVE_SECP256R1,
296 SSL_CURVE_SECP384R1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800297};
Adam Langley95c29f32014-06-20 12:00:00 -0700298
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700299Span<const uint16_t> tls1_get_grouplist(const SSL_HANDSHAKE *hs) {
David Benjamin0ce090a2018-07-02 20:24:40 -0400300 if (!hs->config->supported_group_list.empty()) {
301 return hs->config->supported_group_list;
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 }
David Benjamincf0ce672017-09-21 02:25:59 -0400303 return Span<const uint16_t>(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800304}
David Benjamined439582014-07-14 19:13:02 -0400305
David Benjamin861abcc2018-07-14 17:40:26 -0400306bool tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900307 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400308 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400309
David Benjaminc11ea9422017-08-29 16:33:21 -0400310 // Clients are not required to send a supported_groups extension. In this
311 // case, the server is free to pick any group it likes. See RFC 4492,
312 // section 4, paragraph 3.
313 //
314 // However, in the interests of compatibility, we will skip ECDH if the
315 // client didn't send an extension because we can't be sure that they'll
316 // support our favoured group. Thus we do not special-case an emtpy
317 // |peer_supported_group_list|.
David Benjamin55a43642015-04-20 14:45:55 -0400318
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700319 Span<const uint16_t> groups = tls1_get_grouplist(hs);
David Benjamincf0ce672017-09-21 02:25:59 -0400320 Span<const uint16_t> pref, supp;
David Benjamin4298d772015-12-19 00:18:25 -0500321 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400322 pref = groups;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900323 supp = hs->peer_supported_group_list;
David Benjamin55a43642015-04-20 14:45:55 -0400324 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900325 pref = hs->peer_supported_group_list;
Steven Valdezce902a92016-05-17 11:47:53 -0400326 supp = groups;
David Benjamin55a43642015-04-20 14:45:55 -0400327 }
328
David Benjamincf0ce672017-09-21 02:25:59 -0400329 for (uint16_t pref_group : pref) {
330 for (uint16_t supp_group : supp) {
Adam Langley7b935932018-11-12 13:53:42 -0800331 if (pref_group == supp_group &&
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +0000332 // CECPQ2(b) doesn't fit in the u8-length-prefixed ECPoint field in
333 // TLS 1.2 and below.
Adam Langley7b935932018-11-12 13:53:42 -0800334 (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +0000335 !is_post_quantum_group(pref_group))) {
David Benjamincf0ce672017-09-21 02:25:59 -0400336 *out_group_id = pref_group;
David Benjamin861abcc2018-07-14 17:40:26 -0400337 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800338 }
339 }
340 }
341
David Benjamin861abcc2018-07-14 17:40:26 -0400342 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800343}
Adam Langley95c29f32014-06-20 12:00:00 -0700344
David Benjamin0ce090a2018-07-02 20:24:40 -0400345bool tls1_set_curves(Array<uint16_t> *out_group_ids, Span<const int> curves) {
346 Array<uint16_t> group_ids;
347 if (!group_ids.Init(curves.size())) {
348 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800349 }
350
David Benjamin0ce090a2018-07-02 20:24:40 -0400351 for (size_t i = 0; i < curves.size(); i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400352 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
David Benjamin0ce090a2018-07-02 20:24:40 -0400353 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800355 }
356
David Benjamin0ce090a2018-07-02 20:24:40 -0400357 *out_group_ids = std::move(group_ids);
358 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800359}
Adam Langley95c29f32014-06-20 12:00:00 -0700360
David Benjamin0ce090a2018-07-02 20:24:40 -0400361bool tls1_set_curves_list(Array<uint16_t> *out_group_ids, const char *curves) {
362 // Count the number of curves in the list.
363 size_t count = 0;
364 const char *ptr = curves, *col;
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100365 do {
366 col = strchr(ptr, ':');
David Benjamin0ce090a2018-07-02 20:24:40 -0400367 count++;
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100368 if (col) {
369 ptr = col + 1;
370 }
371 } while (col);
372
David Benjamin0ce090a2018-07-02 20:24:40 -0400373 Array<uint16_t> group_ids;
374 if (!group_ids.Init(count)) {
375 return false;
376 }
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100377
David Benjamin0ce090a2018-07-02 20:24:40 -0400378 size_t i = 0;
379 ptr = curves;
380 do {
381 col = strchr(ptr, ':');
382 if (!ssl_name_to_group_id(&group_ids[i++], ptr,
383 col ? (size_t)(col - ptr) : strlen(ptr))) {
384 return false;
385 }
386 if (col) {
387 ptr = col + 1;
388 }
389 } while (col);
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100390
David Benjamin0ce090a2018-07-02 20:24:40 -0400391 assert(i == count);
392 *out_group_ids = std::move(group_ids);
393 return true;
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100394}
395
David Benjamin861abcc2018-07-14 17:40:26 -0400396bool tls1_check_group_id(const SSL_HANDSHAKE *hs, uint16_t group_id) {
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +0000397 if (is_post_quantum_group(group_id) &&
Adam Langley7b935932018-11-12 13:53:42 -0800398 ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) {
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +0000399 // CECPQ2(b) requires TLS 1.3.
Adam Langley7b935932018-11-12 13:53:42 -0800400 return false;
401 }
402
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700403 for (uint16_t supported : tls1_get_grouplist(hs)) {
David Benjamincf0ce672017-09-21 02:25:59 -0400404 if (supported == group_id) {
David Benjamin861abcc2018-07-14 17:40:26 -0400405 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800407 }
David Benjamin033e5f42014-11-13 18:47:41 -0500408
David Benjamin861abcc2018-07-14 17:40:26 -0400409 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800410}
David Benjamin033e5f42014-11-13 18:47:41 -0500411
David Benjaminc11ea9422017-08-29 16:33:21 -0400412// kVerifySignatureAlgorithms is the default list of accepted signature
413// algorithms for verifying.
David Benjamin3ef76972016-10-17 17:59:54 -0400414static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400415 // List our preferred algorithms first.
David Benjamin3a322f52016-10-26 12:45:35 -0400416 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin6879e192018-04-13 16:01:02 -0400417 SSL_SIGN_RSA_PSS_RSAE_SHA256,
David Benjamin3a322f52016-10-26 12:45:35 -0400418 SSL_SIGN_RSA_PKCS1_SHA256,
419
David Benjaminc11ea9422017-08-29 16:33:21 -0400420 // Larger hashes are acceptable.
David Benjamin3a322f52016-10-26 12:45:35 -0400421 SSL_SIGN_ECDSA_SECP384R1_SHA384,
David Benjamin6879e192018-04-13 16:01:02 -0400422 SSL_SIGN_RSA_PSS_RSAE_SHA384,
David Benjamin3a322f52016-10-26 12:45:35 -0400423 SSL_SIGN_RSA_PKCS1_SHA384,
424
David Benjamin6879e192018-04-13 16:01:02 -0400425 SSL_SIGN_RSA_PSS_RSAE_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400426 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400427
David Benjaminc11ea9422017-08-29 16:33:21 -0400428 // For now, SHA-1 is still accepted but least preferable.
David Benjamin3a322f52016-10-26 12:45:35 -0400429 SSL_SIGN_RSA_PKCS1_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400430};
431
David Benjaminc11ea9422017-08-29 16:33:21 -0400432// kSignSignatureAlgorithms is the default list of supported signature
433// algorithms for signing.
David Benjamin3a322f52016-10-26 12:45:35 -0400434static const uint16_t kSignSignatureAlgorithms[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400435 // List our preferred algorithms first.
David Benjamin69522112017-03-28 15:38:29 -0500436 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400437 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin6879e192018-04-13 16:01:02 -0400438 SSL_SIGN_RSA_PSS_RSAE_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400439 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400440
David Benjaminc11ea9422017-08-29 16:33:21 -0400441 // If needed, sign larger hashes.
442 //
443 // TODO(davidben): Determine which of these may be pruned.
David Benjamin3a322f52016-10-26 12:45:35 -0400444 SSL_SIGN_ECDSA_SECP384R1_SHA384,
David Benjamin6879e192018-04-13 16:01:02 -0400445 SSL_SIGN_RSA_PSS_RSAE_SHA384,
David Benjamin3a322f52016-10-26 12:45:35 -0400446 SSL_SIGN_RSA_PKCS1_SHA384,
447
448 SSL_SIGN_ECDSA_SECP521R1_SHA512,
David Benjamin6879e192018-04-13 16:01:02 -0400449 SSL_SIGN_RSA_PSS_RSAE_SHA512,
David Benjamin3a322f52016-10-26 12:45:35 -0400450 SSL_SIGN_RSA_PKCS1_SHA512,
451
David Benjaminc11ea9422017-08-29 16:33:21 -0400452 // If the peer supports nothing else, sign with SHA-1.
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400453 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400454 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400455};
456
David Benjamin17669352020-02-05 17:33:36 -0500457static Span<const uint16_t> tls12_get_verify_sigalgs(const SSL_HANDSHAKE *hs) {
458 if (hs->config->verify_sigalgs.empty()) {
459 return Span<const uint16_t>(kVerifySignatureAlgorithms);
David Benjamin71c21b42017-04-14 17:05:40 -0400460 }
David Benjamin17669352020-02-05 17:33:36 -0500461 return hs->config->verify_sigalgs;
David Benjamine28552d2018-04-08 13:59:25 -0400462}
463
David Benjaminebad5082020-02-03 19:32:19 -0500464bool tls12_add_verify_sigalgs(const SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin17669352020-02-05 17:33:36 -0500465 for (uint16_t sigalg : tls12_get_verify_sigalgs(hs)) {
David Benjamin610cdbb2018-01-22 19:08:38 -0500466 if (!CBB_add_u16(out, sigalg)) {
467 return false;
David Benjamin69522112017-03-28 15:38:29 -0500468 }
469 }
David Benjamin610cdbb2018-01-22 19:08:38 -0500470 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800471}
Adam Langley95c29f32014-06-20 12:00:00 -0700472
David Benjaminebad5082020-02-03 19:32:19 -0500473bool tls12_check_peer_sigalg(const SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin610cdbb2018-01-22 19:08:38 -0500474 uint16_t sigalg) {
David Benjamin17669352020-02-05 17:33:36 -0500475 for (uint16_t verify_sigalg : tls12_get_verify_sigalgs(hs)) {
David Benjamine28552d2018-04-08 13:59:25 -0400476 if (verify_sigalg == sigalg) {
David Benjamin610cdbb2018-01-22 19:08:38 -0500477 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 }
479 }
480
David Benjamin3ef76972016-10-17 17:59:54 -0400481 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
482 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin610cdbb2018-01-22 19:08:38 -0500483 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800484}
485
David Benjaminc11ea9422017-08-29 16:33:21 -0400486// tls_extension represents a TLS extension that is handled internally. The
487// |init| function is called for each handshake, before any other functions of
488// the extension. Then the add and parse callbacks are called as needed.
489//
490// The parse callbacks receive a |CBS| that contains the contents of the
491// extension (i.e. not including the type and length bytes). If an extension is
492// not received then the parse callbacks will be called with a NULL CBS so that
493// they can do any processing needed to handle the absence of an extension.
494//
495// The add callbacks receive a |CBB| to which the extension can be appended but
496// the function is responsible for appending the type and length bytes too.
497//
David Benjamin31640932017-10-11 13:22:39 -0400498// All callbacks return true for success and false for error. If a parse
499// function returns zero then a fatal alert with value |*out_alert| will be
500// sent. If |*out_alert| isn't set, then a |decode_error| alert will be sent.
Adam Langley614c66a2015-06-12 15:26:58 -0700501struct tls_extension {
502 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500503 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700504
David Benjamin31640932017-10-11 13:22:39 -0400505 bool (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
506 bool (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
507 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700508
David Benjamin31640932017-10-11 13:22:39 -0400509 bool (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
510 CBS *contents);
511 bool (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700512};
513
David Benjamin31640932017-10-11 13:22:39 -0400514static bool forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin8c880a22016-12-03 02:20:34 -0500515 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400516 if (contents != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400517 // Servers MUST NOT send this extension.
Steven Valdez6b8509a2016-07-12 13:38:32 -0400518 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
519 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
David Benjamin31640932017-10-11 13:22:39 -0400520 return false;
Steven Valdez6b8509a2016-07-12 13:38:32 -0400521 }
522
David Benjamin31640932017-10-11 13:22:39 -0400523 return true;
Steven Valdez6b8509a2016-07-12 13:38:32 -0400524}
525
David Benjamin31640932017-10-11 13:22:39 -0400526static bool ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin8c880a22016-12-03 02:20:34 -0500527 CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400528 // This extension from the client is handled elsewhere.
David Benjamin31640932017-10-11 13:22:39 -0400529 return true;
Steven Valdez6b8509a2016-07-12 13:38:32 -0400530}
531
David Benjamin31640932017-10-11 13:22:39 -0400532static bool dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
533 return true;
Steven Valdez6b8509a2016-07-12 13:38:32 -0400534}
Adam Langley614c66a2015-06-12 15:26:58 -0700535
David Benjaminc11ea9422017-08-29 16:33:21 -0400536// Server name indication (SNI).
537//
538// https://tools.ietf.org/html/rfc6066#section-3.
Adam Langley614c66a2015-06-12 15:26:58 -0700539
David Benjamin31640932017-10-11 13:22:39 -0400540static bool ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500541 SSL *const ssl = hs->ssl;
David Benjamin46853762018-07-03 14:01:26 -0400542 if (ssl->hostname == nullptr) {
David Benjamin31640932017-10-11 13:22:39 -0400543 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700544 }
545
546 CBB contents, server_name_list, name;
547 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
548 !CBB_add_u16_length_prefixed(out, &contents) ||
549 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
550 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
551 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
David Benjamin46853762018-07-03 14:01:26 -0400552 !CBB_add_bytes(&name, (const uint8_t *)ssl->hostname.get(),
553 strlen(ssl->hostname.get())) ||
Adam Langley614c66a2015-06-12 15:26:58 -0700554 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -0400555 return false;
Adam Langley614c66a2015-06-12 15:26:58 -0700556 }
557
David Benjamin31640932017-10-11 13:22:39 -0400558 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700559}
560
David Benjamin31640932017-10-11 13:22:39 -0400561static bool ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
562 CBS *contents) {
David Benjamina8614602017-09-06 15:40:19 -0400563 // The server may acknowledge SNI with an empty extension. We check the syntax
564 // but otherwise ignore this signal.
565 return contents == NULL || CBS_len(contents) == 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700566}
567
David Benjamin31640932017-10-11 13:22:39 -0400568static bool ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
569 CBS *contents) {
David Benjaminef0183c2019-07-20 09:11:05 -0400570 // SNI has already been parsed earlier in the handshake. See |extract_sni|.
David Benjamin31640932017-10-11 13:22:39 -0400571 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700572}
573
David Benjamin31640932017-10-11 13:22:39 -0400574static bool ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500575 if (hs->ssl->s3->session_reused ||
576 !hs->should_ack_sni) {
David Benjamin31640932017-10-11 13:22:39 -0400577 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700578 }
579
580 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
581 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -0400582 return false;
Adam Langley614c66a2015-06-12 15:26:58 -0700583 }
584
David Benjamin31640932017-10-11 13:22:39 -0400585 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700586}
587
588
David Benjaminc11ea9422017-08-29 16:33:21 -0400589// Renegotiation indication.
590//
591// https://tools.ietf.org/html/rfc5746
Adam Langley5021b222015-06-12 18:27:58 -0700592
David Benjamin31640932017-10-11 13:22:39 -0400593static bool ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500594 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400595 // Renegotiation indication is not necessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -0400596 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400597 return true;
David Benjamin7c7d8312016-08-20 13:39:03 -0400598 }
599
David Benjamin52bf6902016-10-08 12:05:03 -0400600 assert(ssl->s3->initial_handshake_complete ==
601 (ssl->s3->previous_client_finished_len != 0));
602
Adam Langley5021b222015-06-12 18:27:58 -0700603 CBB contents, prev_finished;
604 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
605 !CBB_add_u16_length_prefixed(out, &contents) ||
606 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
607 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
608 ssl->s3->previous_client_finished_len) ||
609 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -0400610 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700611 }
612
David Benjamin31640932017-10-11 13:22:39 -0400613 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700614}
615
David Benjamin31640932017-10-11 13:22:39 -0400616static bool ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
617 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500618 SSL *const ssl = hs->ssl;
David Benjamind1e3ce12017-10-06 18:31:15 -0400619 if (contents != NULL && ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez246eeee2017-03-26 12:49:17 -0500620 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -0400621 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -0400622 }
623
David Benjaminc11ea9422017-08-29 16:33:21 -0400624 // Servers may not switch between omitting the extension and supporting it.
625 // See RFC 5746, sections 3.5 and 4.2.
David Benjamin3e052de2015-11-25 20:10:31 -0500626 if (ssl->s3->initial_handshake_complete &&
627 (contents != NULL) != ssl->s3->send_connection_binding) {
628 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
629 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
David Benjamin31640932017-10-11 13:22:39 -0400630 return false;
David Benjamin3e052de2015-11-25 20:10:31 -0500631 }
632
Adam Langley5021b222015-06-12 18:27:58 -0700633 if (contents == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400634 // Strictly speaking, if we want to avoid an attack we should *always* see
635 // RI even on initial ServerHello because the client doesn't see any
636 // renegotiation during an attack. However this would mean we could not
637 // connect to any server which doesn't support RI.
638 //
639 // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
640 // practical terms every client sets it so it's just assumed here.
David Benjamin31640932017-10-11 13:22:39 -0400641 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700642 }
643
644 const size_t expected_len = ssl->s3->previous_client_finished_len +
645 ssl->s3->previous_server_finished_len;
646
David Benjaminc11ea9422017-08-29 16:33:21 -0400647 // Check for logic errors
Adam Langley5021b222015-06-12 18:27:58 -0700648 assert(!expected_len || ssl->s3->previous_client_finished_len);
649 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400650 assert(ssl->s3->initial_handshake_complete ==
651 (ssl->s3->previous_client_finished_len != 0));
652 assert(ssl->s3->initial_handshake_complete ==
653 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700654
David Benjaminc11ea9422017-08-29 16:33:21 -0400655 // Parse out the extension contents.
Adam Langley5021b222015-06-12 18:27:58 -0700656 CBS renegotiated_connection;
657 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
658 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400659 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700660 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -0400661 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700662 }
663
David Benjaminc11ea9422017-08-29 16:33:21 -0400664 // Check that the extension matches.
Adam Langley5021b222015-06-12 18:27:58 -0700665 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400666 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700667 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin31640932017-10-11 13:22:39 -0400668 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700669 }
670
671 const uint8_t *d = CBS_data(&renegotiated_connection);
David Benjamin31640932017-10-11 13:22:39 -0400672 bool ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
673 ssl->s3->previous_client_finished_len) == 0;
David Benjamin9343b0b2017-07-01 00:31:27 -0400674#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin31640932017-10-11 13:22:39 -0400675 ok = true;
David Benjamin9343b0b2017-07-01 00:31:27 -0400676#endif
677 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400678 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700679 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin31640932017-10-11 13:22:39 -0400680 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700681 }
682 d += ssl->s3->previous_client_finished_len;
683
David Benjamin9343b0b2017-07-01 00:31:27 -0400684 ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
685 ssl->s3->previous_server_finished_len) == 0;
686#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin31640932017-10-11 13:22:39 -0400687 ok = true;
David Benjamin9343b0b2017-07-01 00:31:27 -0400688#endif
689 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400690 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley10e10602017-07-25 13:33:21 -0700691 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin31640932017-10-11 13:22:39 -0400692 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700693 }
David Benjamin046bc1f2017-08-31 15:06:42 -0400694 ssl->s3->send_connection_binding = true;
Adam Langley5021b222015-06-12 18:27:58 -0700695
David Benjamin31640932017-10-11 13:22:39 -0400696 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700697}
698
David Benjamin31640932017-10-11 13:22:39 -0400699static bool ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
700 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500701 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400702 // Renegotiation isn't supported as a server so this function should never be
703 // called after the initial handshake.
Adam Langley5021b222015-06-12 18:27:58 -0700704 assert(!ssl->s3->initial_handshake_complete);
705
David Benjamind1e3ce12017-10-06 18:31:15 -0400706 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400707 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -0400708 }
709
Adam Langley5021b222015-06-12 18:27:58 -0700710 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -0400711 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700712 }
713
714 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700715 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
716 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400717 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
David Benjamin31640932017-10-11 13:22:39 -0400718 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700719 }
720
David Benjaminc11ea9422017-08-29 16:33:21 -0400721 // Check that the extension matches. We do not support renegotiation as a
722 // server, so this must be empty.
David Benjamin52bf6902016-10-08 12:05:03 -0400723 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400724 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700725 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin31640932017-10-11 13:22:39 -0400726 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700727 }
728
David Benjamin046bc1f2017-08-31 15:06:42 -0400729 ssl->s3->send_connection_binding = true;
Adam Langley5021b222015-06-12 18:27:58 -0700730
David Benjamin31640932017-10-11 13:22:39 -0400731 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700732}
733
David Benjamin31640932017-10-11 13:22:39 -0400734static bool ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500735 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400736 // Renegotiation isn't supported as a server so this function should never be
737 // called after the initial handshake.
David Benjamin52bf6902016-10-08 12:05:03 -0400738 assert(!ssl->s3->initial_handshake_complete);
739
David Benjamind1e3ce12017-10-06 18:31:15 -0400740 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400741 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -0400742 }
743
Adam Langley5021b222015-06-12 18:27:58 -0700744 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400745 !CBB_add_u16(out, 1 /* length */) ||
746 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
David Benjamin31640932017-10-11 13:22:39 -0400747 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700748 }
749
David Benjamin31640932017-10-11 13:22:39 -0400750 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700751}
752
Adam Langley0a056712015-07-01 15:03:33 -0700753
David Benjaminc11ea9422017-08-29 16:33:21 -0400754// Extended Master Secret.
755//
756// https://tools.ietf.org/html/rfc7627
Adam Langley0a056712015-07-01 15:03:33 -0700757
David Benjamin31640932017-10-11 13:22:39 -0400758static bool ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400759 // Extended master secret is not necessary in TLS 1.3.
David Benjamin9bb15f52018-06-26 00:07:40 -0400760 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400761 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700762 }
763
764 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
765 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -0400766 return false;
Adam Langley0a056712015-07-01 15:03:33 -0700767 }
768
David Benjamin31640932017-10-11 13:22:39 -0400769 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700770}
771
David Benjamin31640932017-10-11 13:22:39 -0400772static bool ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
773 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500774 SSL *const ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500775
776 if (contents != NULL) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400777 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjaminfc02b592017-02-17 16:26:01 -0500778 CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -0400779 return false;
David Benjamin163c9562016-08-29 23:14:17 -0400780 }
781
David Benjaminfd45ee72017-08-31 14:49:09 -0400782 hs->extended_master_secret = true;
David Benjamin163c9562016-08-29 23:14:17 -0400783 }
784
David Benjaminc11ea9422017-08-29 16:33:21 -0400785 // Whether EMS is negotiated may not change on renegotiation.
David Benjamin8e7bbba2017-10-13 17:18:35 -0400786 if (ssl->s3->established_session != nullptr &&
David Benjaminfc02b592017-02-17 16:26:01 -0500787 hs->extended_master_secret !=
David Benjaminfd45ee72017-08-31 14:49:09 -0400788 !!ssl->s3->established_session->extended_master_secret) {
David Benjaminfc02b592017-02-17 16:26:01 -0500789 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
790 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -0400791 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -0400792 }
793
David Benjamin31640932017-10-11 13:22:39 -0400794 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700795}
796
David Benjamin31640932017-10-11 13:22:39 -0400797static bool ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
798 CBS *contents) {
David Benjamin9bb15f52018-06-26 00:07:40 -0400799 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400800 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -0400801 }
802
803 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -0400804 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700805 }
806
807 if (CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -0400808 return false;
Adam Langley0a056712015-07-01 15:03:33 -0700809 }
810
David Benjaminfd45ee72017-08-31 14:49:09 -0400811 hs->extended_master_secret = true;
David Benjamin31640932017-10-11 13:22:39 -0400812 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700813}
814
David Benjamin31640932017-10-11 13:22:39 -0400815static bool ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -0500816 if (!hs->extended_master_secret) {
David Benjamin31640932017-10-11 13:22:39 -0400817 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700818 }
819
820 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
821 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -0400822 return false;
Adam Langley0a056712015-07-01 15:03:33 -0700823 }
824
David Benjamin31640932017-10-11 13:22:39 -0400825 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700826}
827
Adam Langley9b05bc52015-07-01 15:25:33 -0700828
David Benjaminc11ea9422017-08-29 16:33:21 -0400829// Session tickets.
830//
831// https://tools.ietf.org/html/rfc5077
Adam Langley9b05bc52015-07-01 15:25:33 -0700832
David Benjamin31640932017-10-11 13:22:39 -0400833static bool ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500834 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400835 // TLS 1.3 uses a different ticket extension.
David Benjamin68161cb2017-06-20 14:49:43 -0400836 if (hs->min_version >= TLS1_3_VERSION ||
David Benjamin7c7d8312016-08-20 13:39:03 -0400837 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
David Benjamin31640932017-10-11 13:22:39 -0400838 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700839 }
840
David Benjaminbfdd1a92018-06-29 16:26:38 -0400841 Span<const uint8_t> ticket;
Adam Langley9b05bc52015-07-01 15:25:33 -0700842
David Benjaminc11ea9422017-08-29 16:33:21 -0400843 // Renegotiation does not participate in session resumption. However, still
844 // advertise the extension to avoid potentially breaking servers which carry
845 // over the state from the previous handshake, such as OpenSSL servers
846 // without upstream's 3c3f0259238594d77264a78944d409f2127642c4.
Adam Langley9b05bc52015-07-01 15:25:33 -0700847 if (!ssl->s3->initial_handshake_complete &&
David Benjamin50596f82018-07-02 19:47:27 -0400848 ssl->session != nullptr &&
David Benjaminbfdd1a92018-06-29 16:26:38 -0400849 !ssl->session->ticket.empty() &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400850 // Don't send TLS 1.3 session tickets in the ticket extension.
David Benjamin50596f82018-07-02 19:47:27 -0400851 ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) {
David Benjaminbfdd1a92018-06-29 16:26:38 -0400852 ticket = ssl->session->ticket;
Adam Langley9b05bc52015-07-01 15:25:33 -0700853 }
854
David Benjaminbfdd1a92018-06-29 16:26:38 -0400855 CBB ticket_cbb;
Adam Langley9b05bc52015-07-01 15:25:33 -0700856 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
David Benjaminbfdd1a92018-06-29 16:26:38 -0400857 !CBB_add_u16_length_prefixed(out, &ticket_cbb) ||
858 !CBB_add_bytes(&ticket_cbb, ticket.data(), ticket.size()) ||
Adam Langley9b05bc52015-07-01 15:25:33 -0700859 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -0400860 return false;
Adam Langley9b05bc52015-07-01 15:25:33 -0700861 }
862
David Benjamin31640932017-10-11 13:22:39 -0400863 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700864}
865
David Benjamin31640932017-10-11 13:22:39 -0400866static bool ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
867 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500868 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -0700869 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -0400870 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700871 }
872
David Benjamind1e3ce12017-10-06 18:31:15 -0400873 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400874 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -0400875 }
876
David Benjaminc11ea9422017-08-29 16:33:21 -0400877 // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
878 // this function should never be called, even if the server tries to send the
879 // extension.
Adam Langley9b05bc52015-07-01 15:25:33 -0700880 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
881
882 if (CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -0400883 return false;
Adam Langley9b05bc52015-07-01 15:25:33 -0700884 }
885
David Benjaminfd45ee72017-08-31 14:49:09 -0400886 hs->ticket_expected = true;
David Benjamin31640932017-10-11 13:22:39 -0400887 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700888}
889
David Benjamin31640932017-10-11 13:22:39 -0400890static bool ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500891 if (!hs->ticket_expected) {
David Benjamin31640932017-10-11 13:22:39 -0400892 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700893 }
894
David Benjaminc11ea9422017-08-29 16:33:21 -0400895 // If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true.
David Benjamin8c880a22016-12-03 02:20:34 -0500896 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -0700897
898 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
899 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -0400900 return false;
Adam Langley9b05bc52015-07-01 15:25:33 -0700901 }
902
David Benjamin31640932017-10-11 13:22:39 -0400903 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700904}
905
906
David Benjaminc11ea9422017-08-29 16:33:21 -0400907// Signature Algorithms.
908//
909// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
Adam Langley2e857bd2015-07-01 16:09:19 -0700910
David Benjamin31640932017-10-11 13:22:39 -0400911static bool ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -0400912 if (hs->max_version < TLS1_2_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400913 return true;
Adam Langley2e857bd2015-07-01 16:09:19 -0700914 }
915
David Benjamin0fc37ef2016-08-17 15:29:46 -0400916 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -0700917 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
918 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin69522112017-03-28 15:38:29 -0500919 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
David Benjaminebad5082020-02-03 19:32:19 -0500920 !tls12_add_verify_sigalgs(hs, &sigalgs_cbb) ||
David Benjamin69522112017-03-28 15:38:29 -0500921 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -0400922 return false;
Adam Langley2e857bd2015-07-01 16:09:19 -0700923 }
924
David Benjamin31640932017-10-11 13:22:39 -0400925 return true;
Adam Langley2e857bd2015-07-01 16:09:19 -0700926}
927
David Benjamin31640932017-10-11 13:22:39 -0400928static bool ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
929 CBS *contents) {
David Benjaminb1cf48e2017-09-21 11:37:46 -0400930 hs->peer_sigalgs.Reset();
Adam Langley2e857bd2015-07-01 16:09:19 -0700931 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -0400932 return true;
Adam Langley2e857bd2015-07-01 16:09:19 -0700933 }
934
935 CBS supported_signature_algorithms;
936 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -0400937 CBS_len(contents) != 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900938 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin31640932017-10-11 13:22:39 -0400939 return false;
Adam Langley2e857bd2015-07-01 16:09:19 -0700940 }
941
David Benjamin31640932017-10-11 13:22:39 -0400942 return true;
Adam Langley2e857bd2015-07-01 16:09:19 -0700943}
944
Adam Langley2e857bd2015-07-01 16:09:19 -0700945
David Benjaminc11ea9422017-08-29 16:33:21 -0400946// OCSP Stapling.
947//
948// https://tools.ietf.org/html/rfc6066#section-8
Adam Langleybb0bd042015-07-01 16:21:03 -0700949
David Benjamin31640932017-10-11 13:22:39 -0400950static bool ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700951 if (!hs->config->ocsp_stapling_enabled) {
David Benjamin31640932017-10-11 13:22:39 -0400952 return true;
Adam Langleybb0bd042015-07-01 16:21:03 -0700953 }
954
955 CBB contents;
956 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
957 !CBB_add_u16_length_prefixed(out, &contents) ||
958 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
959 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
960 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
961 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -0400962 return false;
Adam Langleybb0bd042015-07-01 16:21:03 -0700963 }
964
David Benjamin31640932017-10-11 13:22:39 -0400965 return true;
Adam Langleybb0bd042015-07-01 16:21:03 -0700966}
967
David Benjamin31640932017-10-11 13:22:39 -0400968static bool ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
969 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500970 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -0700971 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -0400972 return true;
Adam Langleybb0bd042015-07-01 16:21:03 -0700973 }
974
David Benjaminc11ea9422017-08-29 16:33:21 -0400975 // TLS 1.3 OCSP responses are included in the Certificate extensions.
David Benjamind1e3ce12017-10-06 18:31:15 -0400976 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400977 return false;
Steven Valdez803c77a2016-09-06 14:13:43 -0400978 }
979
David Benjaminc11ea9422017-08-29 16:33:21 -0400980 // OCSP stapling is forbidden on non-certificate ciphers.
Steven Valdeza833c352016-11-01 13:39:36 -0400981 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -0500982 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin31640932017-10-11 13:22:39 -0400983 return false;
David Benjamin942f4ed2016-07-16 19:03:49 +0300984 }
985
David Benjaminc11ea9422017-08-29 16:33:21 -0400986 // Note this does not check for resumption in TLS 1.2. Sending
987 // status_request here does not make sense, but OpenSSL does so and the
988 // specification does not say anything. Tolerate it but ignore it.
David Benjamin942f4ed2016-07-16 19:03:49 +0300989
David Benjaminfd45ee72017-08-31 14:49:09 -0400990 hs->certificate_status_expected = true;
David Benjamin31640932017-10-11 13:22:39 -0400991 return true;
Adam Langleybb0bd042015-07-01 16:21:03 -0700992}
993
David Benjamin31640932017-10-11 13:22:39 -0400994static bool ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
995 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100996 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -0400997 return true;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100998 }
999
1000 uint8_t status_type;
1001 if (!CBS_get_u8(contents, &status_type)) {
David Benjamin31640932017-10-11 13:22:39 -04001002 return false;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001003 }
1004
David Benjaminc11ea9422017-08-29 16:33:21 -04001005 // We cannot decide whether OCSP stapling will occur yet because the correct
1006 // SSL_CTX might not have been selected.
David Benjamin8c880a22016-12-03 02:20:34 -05001007 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001008
David Benjamin31640932017-10-11 13:22:39 -04001009 return true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001010}
1011
David Benjamin31640932017-10-11 13:22:39 -04001012static bool ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001013 SSL *const ssl = hs->ssl;
David Benjamind1e3ce12017-10-06 18:31:15 -04001014 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001015 !hs->ocsp_stapling_requested || hs->config->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001016 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001017 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin31640932017-10-11 13:22:39 -04001018 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04001019 }
1020
David Benjaminfd45ee72017-08-31 14:49:09 -04001021 hs->certificate_status_expected = true;
David Benjamin942f4ed2016-07-16 19:03:49 +03001022
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001023 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001024 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001025}
1026
1027
David Benjaminc11ea9422017-08-29 16:33:21 -04001028// Next protocol negotiation.
1029//
1030// https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
Adam Langley97dfcbf2015-07-01 18:35:20 -07001031
David Benjamin31640932017-10-11 13:22:39 -04001032static bool ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001033 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001034 if (ssl->s3->initial_handshake_complete ||
1035 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001036 SSL_is_dtls(ssl)) {
David Benjamin31640932017-10-11 13:22:39 -04001037 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001038 }
1039
1040 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1041 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -04001042 return false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001043 }
1044
David Benjamin31640932017-10-11 13:22:39 -04001045 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001046}
1047
David Benjamin31640932017-10-11 13:22:39 -04001048static bool ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1049 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001050 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001051 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001052 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001053 }
1054
David Benjamind1e3ce12017-10-06 18:31:15 -04001055 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001056 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04001057 }
1058
David Benjaminc11ea9422017-08-29 16:33:21 -04001059 // If any of these are false then we should never have sent the NPN
1060 // extension in the ClientHello and thus this function should never have been
1061 // called.
Adam Langley97dfcbf2015-07-01 18:35:20 -07001062 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001063 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001064 assert(ssl->ctx->next_proto_select_cb != NULL);
1065
David Benjamin8e7bbba2017-10-13 17:18:35 -04001066 if (!ssl->s3->alpn_selected.empty()) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001067 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001068 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1069 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
David Benjamin31640932017-10-11 13:22:39 -04001070 return false;
David Benjamin76c2efc2015-08-31 14:24:29 -04001071 }
1072
Adam Langley97dfcbf2015-07-01 18:35:20 -07001073 const uint8_t *const orig_contents = CBS_data(contents);
1074 const size_t orig_len = CBS_len(contents);
1075
1076 while (CBS_len(contents) != 0) {
1077 CBS proto;
1078 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1079 CBS_len(&proto) == 0) {
David Benjamin31640932017-10-11 13:22:39 -04001080 return false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001081 }
1082 }
1083
1084 uint8_t *selected;
1085 uint8_t selected_len;
1086 if (ssl->ctx->next_proto_select_cb(
1087 ssl, &selected, &selected_len, orig_contents, orig_len,
David Benjamin8e7bbba2017-10-13 17:18:35 -04001088 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK ||
1089 !ssl->s3->next_proto_negotiated.CopyFrom(
1090 MakeConstSpan(selected, selected_len))) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001091 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001092 return false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001093 }
1094
David Benjaminfd45ee72017-08-31 14:49:09 -04001095 hs->next_proto_neg_seen = true;
David Benjamin31640932017-10-11 13:22:39 -04001096 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001097}
1098
David Benjamin31640932017-10-11 13:22:39 -04001099static bool ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1100 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001101 SSL *const ssl = hs->ssl;
David Benjamind1e3ce12017-10-06 18:31:15 -04001102 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001103 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04001104 }
1105
Adam Langley97dfcbf2015-07-01 18:35:20 -07001106 if (contents != NULL && CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001107 return false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001108 }
1109
1110 if (contents == NULL ||
1111 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001112 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001113 SSL_is_dtls(ssl)) {
David Benjamin31640932017-10-11 13:22:39 -04001114 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001115 }
1116
David Benjaminfd45ee72017-08-31 14:49:09 -04001117 hs->next_proto_neg_seen = true;
David Benjamin31640932017-10-11 13:22:39 -04001118 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001119}
1120
David Benjamin31640932017-10-11 13:22:39 -04001121static bool ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001122 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001123 // |next_proto_neg_seen| might have been cleared when an ALPN extension was
1124 // parsed.
David Benjamin8c880a22016-12-03 02:20:34 -05001125 if (!hs->next_proto_neg_seen) {
David Benjamin31640932017-10-11 13:22:39 -04001126 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001127 }
1128
1129 const uint8_t *npa;
1130 unsigned npa_len;
1131
1132 if (ssl->ctx->next_protos_advertised_cb(
1133 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1134 SSL_TLSEXT_ERR_OK) {
David Benjaminfd45ee72017-08-31 14:49:09 -04001135 hs->next_proto_neg_seen = false;
David Benjamin31640932017-10-11 13:22:39 -04001136 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001137 }
1138
1139 CBB contents;
1140 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1141 !CBB_add_u16_length_prefixed(out, &contents) ||
1142 !CBB_add_bytes(&contents, npa, npa_len) ||
1143 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001144 return false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001145 }
1146
David Benjamin31640932017-10-11 13:22:39 -04001147 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001148}
1149
1150
David Benjaminc11ea9422017-08-29 16:33:21 -04001151// Signed certificate timestamps.
1152//
1153// https://tools.ietf.org/html/rfc6962#section-3.3.1
Adam Langleyab8d87d2015-07-10 12:21:39 -07001154
David Benjamin31640932017-10-11 13:22:39 -04001155static bool ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001156 if (!hs->config->signed_cert_timestamps_enabled) {
David Benjamin31640932017-10-11 13:22:39 -04001157 return true;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001158 }
1159
1160 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1161 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -04001162 return false;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001163 }
1164
David Benjamin31640932017-10-11 13:22:39 -04001165 return true;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001166}
1167
David Benjamin31640932017-10-11 13:22:39 -04001168static bool ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1169 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001170 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001171 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001172 return true;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001173 }
1174
David Benjaminc11ea9422017-08-29 16:33:21 -04001175 // TLS 1.3 SCTs are included in the Certificate extensions.
David Benjamind1e3ce12017-10-06 18:31:15 -04001176 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001177 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001178 return false;
Steven Valdeza833c352016-11-01 13:39:36 -04001179 }
1180
David Benjaminc11ea9422017-08-29 16:33:21 -04001181 // If this is false then we should never have sent the SCT extension in the
1182 // ClientHello and thus this function should never have been called.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001183 assert(hs->config->signed_cert_timestamps_enabled);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001184
Adam Langleycfa08c32016-11-17 13:21:27 -08001185 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001186 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001187 return false;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001188 }
1189
David Benjaminc11ea9422017-08-29 16:33:21 -04001190 // Session resumption uses the original session information. The extension
1191 // should not be sent on resumption, but RFC 6962 did not make it a
1192 // requirement, so tolerate this.
1193 //
1194 // TODO(davidben): Enforce this anyway.
David Benjamin8fc2dc02017-08-22 15:07:51 -07001195 if (!ssl->s3->session_reused) {
David Benjaminbfdd1a92018-06-29 16:26:38 -04001196 hs->new_session->signed_cert_timestamp_list.reset(
1197 CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool));
David Benjamin8fc2dc02017-08-22 15:07:51 -07001198 if (hs->new_session->signed_cert_timestamp_list == nullptr) {
1199 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001200 return false;
David Benjamin8fc2dc02017-08-22 15:07:51 -07001201 }
Adam Langleyab8d87d2015-07-10 12:21:39 -07001202 }
1203
David Benjamin31640932017-10-11 13:22:39 -04001204 return true;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001205}
1206
David Benjamin31640932017-10-11 13:22:39 -04001207static bool ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1208 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001209 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001210 return true;
David Benjamin53210cb2016-11-16 09:01:48 +09001211 }
1212
1213 if (CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001214 return false;
David Benjamin53210cb2016-11-16 09:01:48 +09001215 }
1216
David Benjaminfd45ee72017-08-31 14:49:09 -04001217 hs->scts_requested = true;
David Benjamin31640932017-10-11 13:22:39 -04001218 return true;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001219}
1220
David Benjamin31640932017-10-11 13:22:39 -04001221static bool ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001222 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001223 // The extension shouldn't be sent when resuming sessions.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001224 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION || ssl->s3->session_reused ||
1225 hs->config->cert->signed_cert_timestamp_list == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001226 return true;
Paul Lietar4fac72e2015-09-09 13:44:55 +01001227 }
1228
1229 CBB contents;
1230 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1231 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001232 CBB_add_bytes(
1233 &contents,
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001234 CRYPTO_BUFFER_data(
1235 hs->config->cert->signed_cert_timestamp_list.get()),
1236 CRYPTO_BUFFER_len(
1237 hs->config->cert->signed_cert_timestamp_list.get())) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001238 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001239}
1240
1241
David Benjaminc11ea9422017-08-29 16:33:21 -04001242// Application-level Protocol Negotiation.
1243//
1244// https://tools.ietf.org/html/rfc7301
Adam Langleyf18e4532015-07-10 13:39:53 -07001245
David Benjamin31640932017-10-11 13:22:39 -04001246static bool ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001247 SSL *const ssl = hs->ssl;
Nick Harper74161f42020-07-24 15:35:27 -07001248 if (hs->config->alpn_client_proto_list.empty() && ssl->quic_method) {
1249 // ALPN MUST be used with QUIC.
1250 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_ALPN);
1251 return false;
1252 }
1253
David Benjamin0ce090a2018-07-02 20:24:40 -04001254 if (hs->config->alpn_client_proto_list.empty() ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001255 ssl->s3->initial_handshake_complete) {
David Benjamin31640932017-10-11 13:22:39 -04001256 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001257 }
1258
1259 CBB contents, proto_list;
1260 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1261 !CBB_add_u16_length_prefixed(out, &contents) ||
1262 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
David Benjamin0ce090a2018-07-02 20:24:40 -04001263 !CBB_add_bytes(&proto_list, hs->config->alpn_client_proto_list.data(),
1264 hs->config->alpn_client_proto_list.size()) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001265 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001266 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001267 }
1268
David Benjamin31640932017-10-11 13:22:39 -04001269 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001270}
1271
David Benjamin31640932017-10-11 13:22:39 -04001272static bool ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1273 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001274 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001275 if (contents == NULL) {
Nick Harper74161f42020-07-24 15:35:27 -07001276 if (ssl->quic_method) {
1277 // ALPN is required when QUIC is used.
1278 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_ALPN);
1279 *out_alert = SSL_AD_NO_APPLICATION_PROTOCOL;
1280 return false;
1281 }
David Benjamin31640932017-10-11 13:22:39 -04001282 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001283 }
1284
1285 assert(!ssl->s3->initial_handshake_complete);
David Benjamin0ce090a2018-07-02 20:24:40 -04001286 assert(!hs->config->alpn_client_proto_list.empty());
Adam Langleyf18e4532015-07-10 13:39:53 -07001287
David Benjamin8c880a22016-12-03 02:20:34 -05001288 if (hs->next_proto_neg_seen) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001289 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001290 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1291 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
David Benjamin31640932017-10-11 13:22:39 -04001292 return false;
David Benjamin76c2efc2015-08-31 14:24:29 -04001293 }
1294
David Benjaminc11ea9422017-08-29 16:33:21 -04001295 // The extension data consists of a ProtocolNameList which must have
1296 // exactly one ProtocolName. Each of these is length-prefixed.
Adam Langleyf18e4532015-07-10 13:39:53 -07001297 CBS protocol_name_list, protocol_name;
1298 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1299 CBS_len(contents) != 0 ||
1300 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001301 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001302 CBS_len(&protocol_name) == 0 ||
1303 CBS_len(&protocol_name_list) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001304 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001305 }
1306
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001307 if (!ssl_is_alpn_protocol_allowed(hs, protocol_name)) {
David Benjamindd6c2e82017-10-17 15:48:46 -04001308 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1309 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1310 return false;
David Benjamin3e51757d2016-08-11 11:52:23 -04001311 }
1312
David Benjamin8e7bbba2017-10-13 17:18:35 -04001313 if (!ssl->s3->alpn_selected.CopyFrom(protocol_name)) {
Adam Langleyf18e4532015-07-10 13:39:53 -07001314 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001315 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001316 }
1317
David Benjamin31640932017-10-11 13:22:39 -04001318 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001319}
1320
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001321bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs,
David Benjamindd6c2e82017-10-17 15:48:46 -04001322 Span<const uint8_t> protocol) {
David Benjamin0ce090a2018-07-02 20:24:40 -04001323 if (hs->config->alpn_client_proto_list.empty()) {
David Benjamindd6c2e82017-10-17 15:48:46 -04001324 return false;
1325 }
1326
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001327 if (hs->ssl->ctx->allow_unknown_alpn_protos) {
David Benjamindd6c2e82017-10-17 15:48:46 -04001328 return true;
1329 }
1330
1331 // Check that the protocol name is one of the ones we advertised.
David Benjamin0ce090a2018-07-02 20:24:40 -04001332 CBS client_protocol_name_list =
1333 MakeConstSpan(hs->config->alpn_client_proto_list),
1334 client_protocol_name;
David Benjamindd6c2e82017-10-17 15:48:46 -04001335 while (CBS_len(&client_protocol_name_list) > 0) {
1336 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1337 &client_protocol_name)) {
1338 return false;
1339 }
1340
1341 if (client_protocol_name == protocol) {
1342 return true;
1343 }
1344 }
1345
1346 return false;
1347}
1348
David Benjamin31640932017-10-11 13:22:39 -04001349bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1350 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001351 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001352 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001353 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001354 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001355 client_hello, &contents,
1356 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
Nick Harper74161f42020-07-24 15:35:27 -07001357 if (ssl->quic_method) {
1358 // ALPN is required when QUIC is used.
1359 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_ALPN);
1360 *out_alert = SSL_AD_NO_APPLICATION_PROTOCOL;
1361 return false;
1362 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001363 // Ignore ALPN if not configured or no extension was supplied.
David Benjamin31640932017-10-11 13:22:39 -04001364 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001365 }
1366
David Benjaminc11ea9422017-08-29 16:33:21 -04001367 // ALPN takes precedence over NPN.
David Benjaminfd45ee72017-08-31 14:49:09 -04001368 hs->next_proto_neg_seen = false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001369
1370 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001371 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1372 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001373 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001374 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1375 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001376 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001377 }
1378
David Benjaminc11ea9422017-08-29 16:33:21 -04001379 // Validate the protocol list.
Adam Langleyf18e4532015-07-10 13:39:53 -07001380 CBS protocol_name_list_copy = protocol_name_list;
1381 while (CBS_len(&protocol_name_list_copy) > 0) {
1382 CBS protocol_name;
1383
1384 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001385 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001386 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001387 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1388 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001389 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001390 }
1391 }
1392
1393 const uint8_t *selected;
1394 uint8_t selected_len;
1395 if (ssl->ctx->alpn_select_cb(
1396 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1397 CBS_len(&protocol_name_list),
1398 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
David Benjaminfa544f12018-05-15 15:06:28 -04001399 if (selected_len == 0) {
1400 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1401 *out_alert = SSL_AD_INTERNAL_ERROR;
1402 return false;
1403 }
David Benjamin8e7bbba2017-10-13 17:18:35 -04001404 if (!ssl->s3->alpn_selected.CopyFrom(
1405 MakeConstSpan(selected, selected_len))) {
Adam Langleyf18e4532015-07-10 13:39:53 -07001406 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001407 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001408 }
Nick Harper74161f42020-07-24 15:35:27 -07001409 } else if (ssl->quic_method) {
1410 // ALPN is required when QUIC is used.
1411 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_ALPN);
1412 *out_alert = SSL_AD_NO_APPLICATION_PROTOCOL;
1413 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001414 }
1415
David Benjamin31640932017-10-11 13:22:39 -04001416 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001417}
1418
David Benjamin31640932017-10-11 13:22:39 -04001419static bool ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001420 SSL *const ssl = hs->ssl;
David Benjamin8e7bbba2017-10-13 17:18:35 -04001421 if (ssl->s3->alpn_selected.empty()) {
David Benjamin31640932017-10-11 13:22:39 -04001422 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001423 }
1424
1425 CBB contents, proto_list, proto;
1426 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1427 !CBB_add_u16_length_prefixed(out, &contents) ||
1428 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1429 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin8e7bbba2017-10-13 17:18:35 -04001430 !CBB_add_bytes(&proto, ssl->s3->alpn_selected.data(),
1431 ssl->s3->alpn_selected.size()) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001432 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001433 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001434 }
1435
David Benjamin31640932017-10-11 13:22:39 -04001436 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001437}
1438
1439
David Benjaminc11ea9422017-08-29 16:33:21 -04001440// Channel ID.
1441//
1442// https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
Adam Langley49c7af12015-07-10 14:33:46 -07001443
David Benjamin8c880a22016-12-03 02:20:34 -05001444static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
David Benjamin46853762018-07-03 14:01:26 -04001445 hs->ssl->s3->channel_id_valid = false;
Adam Langley49c7af12015-07-10 14:33:46 -07001446}
1447
David Benjamin31640932017-10-11 13:22:39 -04001448static bool ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001449 SSL *const ssl = hs->ssl;
David Benjamin46853762018-07-03 14:01:26 -04001450 if (!hs->config->channel_id_enabled || SSL_is_dtls(ssl)) {
David Benjamin31640932017-10-11 13:22:39 -04001451 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001452 }
1453
1454 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1455 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -04001456 return false;
Adam Langley49c7af12015-07-10 14:33:46 -07001457 }
1458
David Benjamin31640932017-10-11 13:22:39 -04001459 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001460}
1461
David Benjamin31640932017-10-11 13:22:39 -04001462static bool ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1463 uint8_t *out_alert,
1464 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001465 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001466 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001467 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001468 }
1469
David Benjamince079fd2016-08-02 16:22:34 -04001470 assert(!SSL_is_dtls(ssl));
David Benjamin46853762018-07-03 14:01:26 -04001471 assert(hs->config->channel_id_enabled);
Adam Langley49c7af12015-07-10 14:33:46 -07001472
1473 if (CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001474 return false;
Adam Langley49c7af12015-07-10 14:33:46 -07001475 }
1476
David Benjamin46853762018-07-03 14:01:26 -04001477 ssl->s3->channel_id_valid = true;
David Benjamin31640932017-10-11 13:22:39 -04001478 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001479}
1480
David Benjamin31640932017-10-11 13:22:39 -04001481static bool ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1482 uint8_t *out_alert,
1483 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001484 SSL *const ssl = hs->ssl;
David Benjamin46853762018-07-03 14:01:26 -04001485 if (contents == NULL || !hs->config->channel_id_enabled || SSL_is_dtls(ssl)) {
David Benjamin31640932017-10-11 13:22:39 -04001486 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001487 }
1488
1489 if (CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001490 return false;
Adam Langley49c7af12015-07-10 14:33:46 -07001491 }
1492
David Benjamin46853762018-07-03 14:01:26 -04001493 ssl->s3->channel_id_valid = true;
David Benjamin31640932017-10-11 13:22:39 -04001494 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001495}
1496
David Benjamin31640932017-10-11 13:22:39 -04001497static bool ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001498 SSL *const ssl = hs->ssl;
David Benjamin46853762018-07-03 14:01:26 -04001499 if (!ssl->s3->channel_id_valid) {
David Benjamin31640932017-10-11 13:22:39 -04001500 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001501 }
1502
1503 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1504 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -04001505 return false;
Adam Langley49c7af12015-07-10 14:33:46 -07001506 }
1507
David Benjamin31640932017-10-11 13:22:39 -04001508 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001509}
1510
Adam Langley391250d2015-07-15 19:06:07 -07001511
David Benjaminc11ea9422017-08-29 16:33:21 -04001512// Secure Real-time Transport Protocol (SRTP) extension.
1513//
1514// https://tools.ietf.org/html/rfc5764
Adam Langley391250d2015-07-15 19:06:07 -07001515
Adam Langley391250d2015-07-15 19:06:07 -07001516
David Benjamin8c880a22016-12-03 02:20:34 -05001517static void ext_srtp_init(SSL_HANDSHAKE *hs) {
David Benjaminfceca8e2018-04-12 16:37:19 -04001518 hs->ssl->s3->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001519}
1520
David Benjamin31640932017-10-11 13:22:39 -04001521static bool ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001522 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001523 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
David Benjaminee910bf2017-07-25 22:36:00 -04001524 if (profiles == NULL ||
1525 sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
David Benjamin31640932017-10-11 13:22:39 -04001526 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001527 }
1528
1529 CBB contents, profile_ids;
1530 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1531 !CBB_add_u16_length_prefixed(out, &contents) ||
1532 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
David Benjamin31640932017-10-11 13:22:39 -04001533 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001534 }
1535
David Benjaminee910bf2017-07-25 22:36:00 -04001536 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
1537 if (!CBB_add_u16(&profile_ids, profile->id)) {
David Benjamin31640932017-10-11 13:22:39 -04001538 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001539 }
1540 }
1541
1542 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1543 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001544 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001545 }
1546
David Benjamin31640932017-10-11 13:22:39 -04001547 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001548}
1549
David Benjamin31640932017-10-11 13:22:39 -04001550static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1551 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001552 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001553 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001554 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001555 }
1556
David Benjaminc11ea9422017-08-29 16:33:21 -04001557 // The extension consists of a u16-prefixed profile ID list containing a
1558 // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1559 //
1560 // See https://tools.ietf.org/html/rfc5764#section-4.1.1
Adam Langley391250d2015-07-15 19:06:07 -07001561 CBS profile_ids, srtp_mki;
1562 uint16_t profile_id;
1563 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1564 !CBS_get_u16(&profile_ids, &profile_id) ||
1565 CBS_len(&profile_ids) != 0 ||
1566 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1567 CBS_len(contents) != 0) {
1568 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
David Benjamin31640932017-10-11 13:22:39 -04001569 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001570 }
1571
1572 if (CBS_len(&srtp_mki) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001573 // Must be no MKI, since we never offer one.
Adam Langley391250d2015-07-15 19:06:07 -07001574 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1575 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04001576 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001577 }
1578
1579 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1580
David Benjaminc11ea9422017-08-29 16:33:21 -04001581 // Check to see if the server gave us something we support (and presumably
1582 // offered).
David Benjaminee910bf2017-07-25 22:36:00 -04001583 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001584 if (profile->id == profile_id) {
David Benjaminfceca8e2018-04-12 16:37:19 -04001585 ssl->s3->srtp_profile = profile;
David Benjamin31640932017-10-11 13:22:39 -04001586 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001587 }
1588 }
1589
1590 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1591 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04001592 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001593}
1594
David Benjamin31640932017-10-11 13:22:39 -04001595static bool ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1596 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001597 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001598 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001599 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001600 }
1601
1602 CBS profile_ids, srtp_mki;
1603 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1604 CBS_len(&profile_ids) < 2 ||
1605 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1606 CBS_len(contents) != 0) {
1607 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
David Benjamin31640932017-10-11 13:22:39 -04001608 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001609 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001610 // Discard the MKI value for now.
Adam Langley391250d2015-07-15 19:06:07 -07001611
1612 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1613 SSL_get_srtp_profiles(ssl);
1614
David Benjaminc11ea9422017-08-29 16:33:21 -04001615 // Pick the server's most preferred profile.
David Benjaminee910bf2017-07-25 22:36:00 -04001616 for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001617 CBS profile_ids_tmp;
1618 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1619
1620 while (CBS_len(&profile_ids_tmp) > 0) {
1621 uint16_t profile_id;
1622 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
David Benjamin31640932017-10-11 13:22:39 -04001623 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001624 }
1625
1626 if (server_profile->id == profile_id) {
David Benjaminfceca8e2018-04-12 16:37:19 -04001627 ssl->s3->srtp_profile = server_profile;
David Benjamin31640932017-10-11 13:22:39 -04001628 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001629 }
1630 }
1631 }
1632
David Benjamin31640932017-10-11 13:22:39 -04001633 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001634}
1635
David Benjamin31640932017-10-11 13:22:39 -04001636static bool ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001637 SSL *const ssl = hs->ssl;
David Benjaminfceca8e2018-04-12 16:37:19 -04001638 if (ssl->s3->srtp_profile == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001639 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001640 }
1641
1642 CBB contents, profile_ids;
1643 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1644 !CBB_add_u16_length_prefixed(out, &contents) ||
1645 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
David Benjaminfceca8e2018-04-12 16:37:19 -04001646 !CBB_add_u16(&profile_ids, ssl->s3->srtp_profile->id) ||
Adam Langley391250d2015-07-15 19:06:07 -07001647 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1648 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001649 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001650 }
1651
David Benjamin31640932017-10-11 13:22:39 -04001652 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001653}
1654
Adam Langleybdd5d662015-07-20 16:19:08 -07001655
David Benjaminc11ea9422017-08-29 16:33:21 -04001656// EC point formats.
1657//
1658// https://tools.ietf.org/html/rfc4492#section-5.1.2
Adam Langleybdd5d662015-07-20 16:19:08 -07001659
David Benjamin31640932017-10-11 13:22:39 -04001660static bool ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001661 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001662 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1663 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001664 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1665 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001666 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001667 return false;
Adam Langleybdd5d662015-07-20 16:19:08 -07001668 }
1669
David Benjamin31640932017-10-11 13:22:39 -04001670 return true;
Adam Langleybdd5d662015-07-20 16:19:08 -07001671}
1672
David Benjamin31640932017-10-11 13:22:39 -04001673static bool ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langleyffe384c2019-05-01 11:13:12 -07001674 // The point format extension is unnecessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -04001675 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001676 return true;
David Benjamin70aba262016-11-01 12:08:15 -04001677 }
1678
David Benjamin8c880a22016-12-03 02:20:34 -05001679 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001680}
1681
David Benjamin31640932017-10-11 13:22:39 -04001682static bool ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1683 CBS *contents) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001684 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001685 return true;
Adam Langleybdd5d662015-07-20 16:19:08 -07001686 }
1687
David Benjamind1e3ce12017-10-06 18:31:15 -04001688 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001689 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04001690 }
1691
Adam Langleybdd5d662015-07-20 16:19:08 -07001692 CBS ec_point_format_list;
1693 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1694 CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001695 return false;
Adam Langleybdd5d662015-07-20 16:19:08 -07001696 }
1697
David Benjaminc11ea9422017-08-29 16:33:21 -04001698 // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1699 // point format.
David Benjamin17cf2cb2016-12-13 01:07:13 -05001700 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1701 TLSEXT_ECPOINTFORMAT_uncompressed,
1702 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001703 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04001704 return false;
Adam Langleybdd5d662015-07-20 16:19:08 -07001705 }
1706
David Benjamin31640932017-10-11 13:22:39 -04001707 return true;
Adam Langleybdd5d662015-07-20 16:19:08 -07001708}
1709
David Benjamin31640932017-10-11 13:22:39 -04001710static bool ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001711 CBS *contents) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001712 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001713 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04001714 }
1715
David Benjamin8c880a22016-12-03 02:20:34 -05001716 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001717}
1718
David Benjamin31640932017-10-11 13:22:39 -04001719static bool ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001720 SSL *const ssl = hs->ssl;
David Benjamind1e3ce12017-10-06 18:31:15 -04001721 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001722 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04001723 }
1724
David Benjamin45738dd2017-02-09 20:01:26 -05001725 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1726 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjamin31640932017-10-11 13:22:39 -04001727 const bool using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001728
1729 if (!using_ecc) {
David Benjamin31640932017-10-11 13:22:39 -04001730 return true;
Adam Langleybdd5d662015-07-20 16:19:08 -07001731 }
1732
David Benjamin8c880a22016-12-03 02:20:34 -05001733 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001734}
1735
Steven Valdeza833c352016-11-01 13:39:36 -04001736
David Benjaminc11ea9422017-08-29 16:33:21 -04001737// Pre Shared Key
1738//
David Benjamina130ce02018-08-14 22:26:39 -05001739// https://tools.ietf.org/html/rfc8446#section-4.2.11
Steven Valdez4aa154e2016-07-29 14:32:55 -04001740
David Benjamin8c880a22016-12-03 02:20:34 -05001741static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1742 SSL *const ssl = hs->ssl;
David Benjamin50596f82018-07-02 19:47:27 -04001743 if (hs->max_version < TLS1_3_VERSION || ssl->session == nullptr ||
1744 ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001745 return 0;
1746 }
1747
David Benjamin50596f82018-07-02 19:47:27 -04001748 size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session.get()));
David Benjaminbfdd1a92018-06-29 16:26:38 -04001749 return 15 + ssl->session->ticket.size() + binder_len;
Steven Valdeza833c352016-11-01 13:39:36 -04001750}
1751
David Benjamin31640932017-10-11 13:22:39 -04001752static bool ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001753 SSL *const ssl = hs->ssl;
Steven Valdezcd8470f2017-10-11 12:29:36 -04001754 hs->needs_psk_binder = false;
David Benjamin50596f82018-07-02 19:47:27 -04001755 if (hs->max_version < TLS1_3_VERSION || ssl->session == nullptr ||
1756 ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001757 return true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001758 }
1759
David Benjamina130ce02018-08-14 22:26:39 -05001760 // Per RFC 8446 section 4.1.4, skip offering the session if the selected
1761 // cipher in HelloRetryRequest does not match. This avoids performing the
1762 // transcript hash transformation for multiple hashes.
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +01001763 if (ssl->s3 && ssl->s3->used_hello_retry_request &&
Steven Valdezcd8470f2017-10-11 12:29:36 -04001764 ssl->session->cipher->algorithm_prf != hs->new_cipher->algorithm_prf) {
1765 return true;
1766 }
1767
David Benjaminad8f5e12017-02-20 17:00:20 -05001768 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001769 ssl_get_current_time(ssl, &now);
1770 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1771 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1772
David Benjaminc11ea9422017-08-29 16:33:21 -04001773 // Fill in a placeholder zero binder of the appropriate length. It will be
1774 // computed and filled in later after length prefixes are computed.
Steven Valdeza833c352016-11-01 13:39:36 -04001775 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
David Benjamin50596f82018-07-02 19:47:27 -04001776 size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session.get()));
Steven Valdeza833c352016-11-01 13:39:36 -04001777
1778 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001779 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1780 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001781 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001782 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
David Benjaminbfdd1a92018-06-29 16:26:38 -04001783 !CBB_add_bytes(&ticket, ssl->session->ticket.data(),
1784 ssl->session->ticket.size()) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001785 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1786 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1787 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1788 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
David Benjamin31640932017-10-11 13:22:39 -04001789 return false;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001790 }
1791
David Benjaminfd45ee72017-08-31 14:49:09 -04001792 hs->needs_psk_binder = true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001793 return CBB_flush(out);
1794}
1795
David Benjamin31640932017-10-11 13:22:39 -04001796bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1797 uint8_t *out_alert,
1798 CBS *contents) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001799 uint16_t psk_id;
1800 if (!CBS_get_u16(contents, &psk_id) ||
1801 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001802 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001803 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001804 return false;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001805 }
1806
David Benjaminc11ea9422017-08-29 16:33:21 -04001807 // We only advertise one PSK identity, so the only legal index is zero.
Steven Valdez4aa154e2016-07-29 14:32:55 -04001808 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001809 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001810 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
David Benjamin31640932017-10-11 13:22:39 -04001811 return false;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001812 }
1813
David Benjamin31640932017-10-11 13:22:39 -04001814 return true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001815}
1816
David Benjamin31640932017-10-11 13:22:39 -04001817bool ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001818 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin9806ae02019-08-16 15:32:03 -04001819 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert,
1820 const SSL_CLIENT_HELLO *client_hello, CBS *contents) {
1821 // Verify that the pre_shared_key extension is the last extension in
1822 // ClientHello.
1823 if (CBS_data(contents) + CBS_len(contents) !=
1824 client_hello->extensions + client_hello->extensions_len) {
1825 OPENSSL_PUT_ERROR(SSL, SSL_R_PRE_SHARED_KEY_MUST_BE_LAST);
1826 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1827 return false;
1828 }
1829
David Benjaminc11ea9422017-08-29 16:33:21 -04001830 // We only process the first PSK identity since we don't support pure PSK.
David Benjamin707af292017-03-10 17:47:18 -05001831 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001832 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001833 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001834 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001835 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001836 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001837 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001838 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001839 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001840 return false;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001841 }
1842
Steven Valdeza833c352016-11-01 13:39:36 -04001843 *out_binders = binders;
1844
David Benjaminc11ea9422017-08-29 16:33:21 -04001845 // Check the syntax of the remaining identities, but do not process them.
David Benjaminaedf3032016-12-01 16:47:56 -05001846 size_t num_identities = 1;
1847 while (CBS_len(&identities) != 0) {
1848 CBS unused_ticket;
1849 uint32_t unused_obfuscated_ticket_age;
1850 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1851 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1852 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1853 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001854 return false;
David Benjaminaedf3032016-12-01 16:47:56 -05001855 }
1856
1857 num_identities++;
1858 }
1859
David Benjaminc11ea9422017-08-29 16:33:21 -04001860 // Check the syntax of the binders. The value will be checked later if
1861 // resuming.
David Benjaminaedf3032016-12-01 16:47:56 -05001862 size_t num_binders = 0;
1863 while (CBS_len(&binders) != 0) {
1864 CBS binder;
1865 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1866 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1867 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001868 return false;
David Benjaminaedf3032016-12-01 16:47:56 -05001869 }
1870
1871 num_binders++;
1872 }
1873
1874 if (num_identities != num_binders) {
1875 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1876 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04001877 return false;
Steven Valdez5b986082016-09-01 12:29:49 -04001878 }
1879
David Benjamin31640932017-10-11 13:22:39 -04001880 return true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001881}
1882
David Benjamin31640932017-10-11 13:22:39 -04001883bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8baf9632016-11-17 17:11:16 +09001884 if (!hs->ssl->s3->session_reused) {
David Benjamin31640932017-10-11 13:22:39 -04001885 return true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001886 }
1887
1888 CBB contents;
1889 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1890 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001891 // We only consider the first identity for resumption
Steven Valdez4aa154e2016-07-29 14:32:55 -04001892 !CBB_add_u16(&contents, 0) ||
1893 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001894 return false;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001895 }
1896
David Benjamin31640932017-10-11 13:22:39 -04001897 return true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001898}
1899
1900
David Benjaminc11ea9422017-08-29 16:33:21 -04001901// Pre-Shared Key Exchange Modes
1902//
David Benjamina130ce02018-08-14 22:26:39 -05001903// https://tools.ietf.org/html/rfc8446#section-4.2.9
Steven Valdeza4ee74d2016-11-29 13:36:45 -05001904
David Benjamin31640932017-10-11 13:22:39 -04001905static bool ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
1906 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04001907 if (hs->max_version < TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001908 return true;
Steven Valdeza833c352016-11-01 13:39:36 -04001909 }
1910
1911 CBB contents, ke_modes;
1912 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
1913 !CBB_add_u16_length_prefixed(out, &contents) ||
1914 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
1915 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
David Benjamin31640932017-10-11 13:22:39 -04001916 return false;
Steven Valdeza833c352016-11-01 13:39:36 -04001917 }
1918
1919 return CBB_flush(out);
1920}
1921
David Benjamin31640932017-10-11 13:22:39 -04001922static bool ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
1923 uint8_t *out_alert,
1924 CBS *contents) {
David Benjamin4eb95cc2016-11-16 17:08:23 +09001925 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001926 return true;
David Benjamin4eb95cc2016-11-16 17:08:23 +09001927 }
1928
Steven Valdeza833c352016-11-01 13:39:36 -04001929 CBS ke_modes;
1930 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
1931 CBS_len(&ke_modes) == 0 ||
1932 CBS_len(contents) != 0) {
1933 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001934 return false;
Steven Valdeza833c352016-11-01 13:39:36 -04001935 }
1936
David Benjaminc11ea9422017-08-29 16:33:21 -04001937 // We only support tickets with PSK_DHE_KE.
David Benjamin17cf2cb2016-12-13 01:07:13 -05001938 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
1939 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04001940
David Benjamin31640932017-10-11 13:22:39 -04001941 return true;
Steven Valdeza833c352016-11-01 13:39:36 -04001942}
1943
1944
David Benjaminc11ea9422017-08-29 16:33:21 -04001945// Early Data Indication
1946//
David Benjamina130ce02018-08-14 22:26:39 -05001947// https://tools.ietf.org/html/rfc8446#section-4.2.10
Steven Valdeza4ee74d2016-11-29 13:36:45 -05001948
David Benjamin31640932017-10-11 13:22:39 -04001949static bool ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05001950 SSL *const ssl = hs->ssl;
David Benjamin64770122019-05-04 11:00:04 -05001951 // The second ClientHello never offers early data, and we must have already
1952 // filled in |early_data_reason| by this point.
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +01001953 if (ssl->s3->used_hello_retry_request) {
David Benjamin64770122019-05-04 11:00:04 -05001954 assert(ssl->s3->early_data_reason != ssl_early_data_unknown);
David Benjamin31640932017-10-11 13:22:39 -04001955 return true;
Steven Valdez2d850622017-01-11 11:34:52 -05001956 }
1957
David Benjamin64770122019-05-04 11:00:04 -05001958 if (!ssl->enable_early_data) {
1959 ssl->s3->early_data_reason = ssl_early_data_disabled;
1960 return true;
1961 }
1962
1963 if (hs->max_version < TLS1_3_VERSION) {
1964 // We discard inapplicable sessions, so this is redundant with the session
1965 // checks below, but we check give a more useful reason.
1966 ssl->s3->early_data_reason = ssl_early_data_protocol_version;
1967 return true;
1968 }
1969
1970 if (ssl->session == nullptr) {
1971 ssl->s3->early_data_reason = ssl_early_data_no_session_offered;
1972 return true;
1973 }
1974
1975 if (ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION ||
1976 ssl->session->ticket_max_early_data == 0) {
1977 ssl->s3->early_data_reason = ssl_early_data_unsupported_for_session;
1978 return true;
1979 }
1980
1981 // In case ALPN preferences changed since this session was established, avoid
1982 // reporting a confusing value in |SSL_get0_alpn_selected| and sending early
1983 // data we know will be rejected.
1984 if (!ssl->session->early_alpn.empty() &&
1985 !ssl_is_alpn_protocol_allowed(hs, ssl->session->early_alpn)) {
1986 ssl->s3->early_data_reason = ssl_early_data_alpn_mismatch;
1987 return true;
1988 }
1989
1990 // |early_data_reason| will be filled in later when the server responds.
David Benjaminfd45ee72017-08-31 14:49:09 -04001991 hs->early_data_offered = true;
Steven Valdez2d850622017-01-11 11:34:52 -05001992
1993 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
1994 !CBB_add_u16(out, 0) ||
1995 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001996 return false;
Steven Valdez2d850622017-01-11 11:34:52 -05001997 }
1998
David Benjamin31640932017-10-11 13:22:39 -04001999 return true;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002000}
2001
David Benjamin31640932017-10-11 13:22:39 -04002002static bool ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin64770122019-05-04 11:00:04 -05002003 uint8_t *out_alert,
2004 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05002005 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002006 if (contents == NULL) {
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +01002007 if (hs->early_data_offered && !ssl->s3->used_hello_retry_request) {
David Benjamin64770122019-05-04 11:00:04 -05002008 ssl->s3->early_data_reason = ssl->s3->session_reused
2009 ? ssl_early_data_peer_declined
2010 : ssl_early_data_session_not_resumed;
2011 } else {
2012 // We already filled in |early_data_reason| when declining to offer 0-RTT
2013 // or handling the implicit HelloRetryRequest reject.
2014 assert(ssl->s3->early_data_reason != ssl_early_data_unknown);
2015 }
David Benjamin31640932017-10-11 13:22:39 -04002016 return true;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002017 }
2018
David Benjamin64770122019-05-04 11:00:04 -05002019 // If we received an HRR, the second ClientHello never offers early data, so
2020 // the extensions logic will automatically reject early data extensions as
2021 // unsolicited. This covered by the ServerAcceptsEarlyDataOnHRR test.
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +01002022 assert(!ssl->s3->used_hello_retry_request);
David Benjamin64770122019-05-04 11:00:04 -05002023
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002024 if (CBS_len(contents) != 0) {
2025 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04002026 return false;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002027 }
2028
Steven Valdez2d850622017-01-11 11:34:52 -05002029 if (!ssl->s3->session_reused) {
2030 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2031 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
David Benjamin31640932017-10-11 13:22:39 -04002032 return false;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002033 }
Steven Valdez2d850622017-01-11 11:34:52 -05002034
David Benjamin64770122019-05-04 11:00:04 -05002035 ssl->s3->early_data_reason = ssl_early_data_accepted;
David Benjamin02e62562017-12-18 18:04:01 -05002036 ssl->s3->early_data_accepted = true;
David Benjamin31640932017-10-11 13:22:39 -04002037 return true;
Steven Valdez2d850622017-01-11 11:34:52 -05002038}
2039
David Benjamin31640932017-10-11 13:22:39 -04002040static bool ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2041 uint8_t *out_alert, CBS *contents) {
Steven Valdez2d850622017-01-11 11:34:52 -05002042 SSL *const ssl = hs->ssl;
2043 if (contents == NULL ||
David Benjamind1e3ce12017-10-06 18:31:15 -04002044 ssl_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04002045 return true;
Steven Valdez2d850622017-01-11 11:34:52 -05002046 }
2047
2048 if (CBS_len(contents) != 0) {
2049 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04002050 return false;
Steven Valdez2d850622017-01-11 11:34:52 -05002051 }
2052
David Benjaminfd45ee72017-08-31 14:49:09 -04002053 hs->early_data_offered = true;
David Benjamin31640932017-10-11 13:22:39 -04002054 return true;
Steven Valdez2d850622017-01-11 11:34:52 -05002055}
2056
David Benjamin31640932017-10-11 13:22:39 -04002057static bool ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin02e62562017-12-18 18:04:01 -05002058 if (!hs->ssl->s3->early_data_accepted) {
David Benjamin31640932017-10-11 13:22:39 -04002059 return true;
Steven Valdez2d850622017-01-11 11:34:52 -05002060 }
2061
2062 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2063 !CBB_add_u16(out, 0) ||
2064 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04002065 return false;
Steven Valdez2d850622017-01-11 11:34:52 -05002066 }
2067
David Benjamin31640932017-10-11 13:22:39 -04002068 return true;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002069}
2070
2071
David Benjaminc11ea9422017-08-29 16:33:21 -04002072// Key Share
2073//
David Benjamina130ce02018-08-14 22:26:39 -05002074// https://tools.ietf.org/html/rfc8446#section-4.2.8
Steven Valdez143e8b32016-07-11 13:19:03 -04002075
David Benjamin31640932017-10-11 13:22:39 -04002076static bool ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05002077 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002078 if (hs->max_version < TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04002079 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04002080 }
2081
2082 CBB contents, kse_bytes;
Steven Valdez7e5dd252018-01-22 15:20:31 -05002083 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002084 !CBB_add_u16_length_prefixed(out, &contents) ||
2085 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
David Benjamin31640932017-10-11 13:22:39 -04002086 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002087 }
2088
David Benjamin8c880a22016-12-03 02:20:34 -05002089 uint16_t group_id = hs->retry_group;
Adam Langley7b935932018-11-12 13:53:42 -08002090 uint16_t second_group_id = 0;
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +01002091 if (ssl->s3 && ssl->s3->used_hello_retry_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002092 // We received a HelloRetryRequest without a new curve, so there is no new
2093 // share to append. Leave |hs->key_share| as-is.
Steven Valdeza833c352016-11-01 13:39:36 -04002094 if (group_id == 0 &&
David Benjamin08f5c762017-09-21 02:43:05 -04002095 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(),
2096 hs->key_share_bytes.size())) {
David Benjamin31640932017-10-11 13:22:39 -04002097 return false;
Steven Valdez5440fe02016-07-18 12:40:30 -04002098 }
David Benjamin08f5c762017-09-21 02:43:05 -04002099 hs->key_share_bytes.Reset();
David Benjamin3baa6e12016-10-07 21:10:38 -04002100 if (group_id == 0) {
2101 return CBB_flush(out);
2102 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002103 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04002104 // Add a fake group. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002105 if (ssl->ctx->grease_enabled &&
2106 (!CBB_add_u16(&kse_bytes,
David Benjamina7bc9442018-01-18 10:08:53 -05002107 ssl_get_grease_value(hs, ssl_grease_group)) ||
David Benjamin65ac9972016-09-02 21:35:25 -04002108 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2109 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
David Benjamin31640932017-10-11 13:22:39 -04002110 return false;
David Benjamin65ac9972016-09-02 21:35:25 -04002111 }
2112
David Benjaminc11ea9422017-08-29 16:33:21 -04002113 // Predict the most preferred group.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002114 Span<const uint16_t> groups = tls1_get_grouplist(hs);
David Benjaminb9493552017-09-27 19:02:51 -04002115 if (groups.empty()) {
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002116 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
David Benjamin31640932017-10-11 13:22:39 -04002117 return false;
Steven Valdez5440fe02016-07-18 12:40:30 -04002118 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002119
2120 group_id = groups[0];
Adam Langley7b935932018-11-12 13:53:42 -08002121
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +00002122 if (is_post_quantum_group(group_id) && groups.size() >= 2) {
2123 // CECPQ2(b) is not sent as the only initial key share. We'll include the
Adam Langley7b935932018-11-12 13:53:42 -08002124 // 2nd preference group too to avoid round-trips.
2125 second_group_id = groups[1];
2126 assert(second_group_id != group_id);
2127 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002128 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002129
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002130 CBB key_exchange;
Adam Langley7b935932018-11-12 13:53:42 -08002131 hs->key_shares[0] = SSLKeyShare::Create(group_id);
2132 if (!hs->key_shares[0] ||
David Benjaminc642aca2017-07-19 23:28:43 -04002133 !CBB_add_u16(&kse_bytes, group_id) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002134 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
Adam Langley7b935932018-11-12 13:53:42 -08002135 !hs->key_shares[0]->Offer(&key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002136 !CBB_flush(&kse_bytes)) {
David Benjamin31640932017-10-11 13:22:39 -04002137 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002138 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002139
Adam Langley7b935932018-11-12 13:53:42 -08002140 if (second_group_id != 0) {
2141 hs->key_shares[1] = SSLKeyShare::Create(second_group_id);
2142 if (!hs->key_shares[1] ||
2143 !CBB_add_u16(&kse_bytes, second_group_id) ||
2144 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2145 !hs->key_shares[1]->Offer(&key_exchange) ||
2146 !CBB_flush(&kse_bytes)) {
2147 return false;
2148 }
2149 }
2150
2151 // Save the contents of the extension to repeat it in the second
2152 // ClientHello.
Kris Kwiatkowskib11902a2019-08-24 11:01:04 +01002153 if (ssl->s3 && !ssl->s3->used_hello_retry_request &&
David Benjamin08f5c762017-09-21 02:43:05 -04002154 !hs->key_share_bytes.CopyFrom(
2155 MakeConstSpan(CBB_data(&kse_bytes), CBB_len(&kse_bytes)))) {
David Benjamin31640932017-10-11 13:22:39 -04002156 return false;
Steven Valdez5440fe02016-07-18 12:40:30 -04002157 }
2158
Steven Valdez143e8b32016-07-11 13:19:03 -04002159 return CBB_flush(out);
2160}
2161
David Benjamin31640932017-10-11 13:22:39 -04002162bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
2163 Array<uint8_t> *out_secret,
2164 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002165 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002166 uint16_t group_id;
2167 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002168 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2169 CBS_len(contents) != 0) {
David Benjaminac4d5342017-11-17 01:42:04 +08002170 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002171 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04002172 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002173 }
2174
Adam Langley7b935932018-11-12 13:53:42 -08002175 SSLKeyShare *key_share = hs->key_shares[0].get();
2176 if (key_share->GroupID() != group_id) {
2177 if (!hs->key_shares[1] || hs->key_shares[1]->GroupID() != group_id) {
2178 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2179 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2180 return false;
2181 }
2182 key_share = hs->key_shares[1].get();
Steven Valdez143e8b32016-07-11 13:19:03 -04002183 }
2184
Adam Langley7b935932018-11-12 13:53:42 -08002185 if (!key_share->Finish(out_secret, out_alert, peer_key)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002186 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04002187 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002188 }
2189
David Benjamin45738dd2017-02-09 20:01:26 -05002190 hs->new_session->group_id = group_id;
Adam Langley7b935932018-11-12 13:53:42 -08002191 hs->key_shares[0].reset();
2192 hs->key_shares[1].reset();
David Benjamin31640932017-10-11 13:22:39 -04002193 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04002194}
2195
David Benjamin31640932017-10-11 13:22:39 -04002196bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
2197 Array<uint8_t> *out_secret,
2198 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002199 uint16_t group_id;
2200 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002201 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002202 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2203 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin31640932017-10-11 13:22:39 -04002204 return false;
Steven Valdez803c77a2016-09-06 14:13:43 -04002205 }
2206
2207 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002208 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002209 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin31640932017-10-11 13:22:39 -04002210 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002211 }
2212
David Benjaminc11ea9422017-08-29 16:33:21 -04002213 // Find the corresponding key share.
David Benjamin7e1f9842016-09-20 19:24:40 -04002214 CBS peer_key;
Steven Valdez619c8ce2017-10-16 13:12:33 -04002215 CBS_init(&peer_key, NULL, 0);
Steven Valdez143e8b32016-07-11 13:19:03 -04002216 while (CBS_len(&key_shares) > 0) {
2217 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002218 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002219 if (!CBS_get_u16(&key_shares, &id) ||
Steven Valdez619c8ce2017-10-16 13:12:33 -04002220 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp) ||
2221 CBS_len(&peer_key_tmp) == 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002222 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin31640932017-10-11 13:22:39 -04002223 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002224 }
2225
David Benjamin7e1f9842016-09-20 19:24:40 -04002226 if (id == group_id) {
Steven Valdez619c8ce2017-10-16 13:12:33 -04002227 if (CBS_len(&peer_key) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002228 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2229 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04002230 return false;
David Benjamin7e1f9842016-09-20 19:24:40 -04002231 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002232
David Benjamin7e1f9842016-09-20 19:24:40 -04002233 peer_key = peer_key_tmp;
David Benjaminc11ea9422017-08-29 16:33:21 -04002234 // Continue parsing the structure to keep peers honest.
Steven Valdez143e8b32016-07-11 13:19:03 -04002235 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002236 }
2237
Steven Valdez619c8ce2017-10-16 13:12:33 -04002238 if (CBS_len(&peer_key) == 0) {
David Benjamin74795b32017-08-31 15:13:12 -04002239 *out_found = false;
David Benjamin499742c2017-07-22 12:45:38 -04002240 out_secret->Reset();
David Benjamin31640932017-10-11 13:22:39 -04002241 return true;
David Benjamin7e1f9842016-09-20 19:24:40 -04002242 }
2243
David Benjaminc11ea9422017-08-29 16:33:21 -04002244 // Compute the DH secret.
David Benjamin499742c2017-07-22 12:45:38 -04002245 Array<uint8_t> secret;
David Benjamin1386aad2017-07-19 23:57:40 -04002246 ScopedCBB public_key;
David Benjaminc642aca2017-07-19 23:28:43 -04002247 UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
David Benjamin6b3ab722017-09-21 02:37:58 -04002248 if (!key_share ||
2249 !CBB_init(public_key.get(), 32) ||
2250 !key_share->Accept(public_key.get(), &secret, out_alert, peer_key) ||
David Benjamin879efc32017-09-21 11:20:53 -04002251 !CBBFinishArray(public_key.get(), &hs->ecdh_public_key)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002252 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04002253 return false;
David Benjamin7e1f9842016-09-20 19:24:40 -04002254 }
2255
David Benjamin499742c2017-07-22 12:45:38 -04002256 *out_secret = std::move(secret);
David Benjamin74795b32017-08-31 15:13:12 -04002257 *out_found = true;
David Benjamin31640932017-10-11 13:22:39 -04002258 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04002259}
2260
David Benjamin31640932017-10-11 13:22:39 -04002261bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002262 uint16_t group_id;
2263 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002264 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez7e5dd252018-01-22 15:20:31 -05002265 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002266 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2267 !CBB_add_u16(&kse_bytes, group_id) ||
2268 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjamin879efc32017-09-21 11:20:53 -04002269 !CBB_add_bytes(&public_key, hs->ecdh_public_key.data(),
2270 hs->ecdh_public_key.size()) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002271 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04002272 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002273 }
2274
David Benjamin879efc32017-09-21 11:20:53 -04002275 hs->ecdh_public_key.Reset();
David Benjamin4fe3c902016-08-16 02:17:03 -04002276
David Benjamin45738dd2017-02-09 20:01:26 -05002277 hs->new_session->group_id = group_id;
David Benjamin31640932017-10-11 13:22:39 -04002278 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04002279}
2280
2281
David Benjaminc11ea9422017-08-29 16:33:21 -04002282// Supported Versions
2283//
David Benjamina130ce02018-08-14 22:26:39 -05002284// https://tools.ietf.org/html/rfc8446#section-4.2.1
Steven Valdezfdd10992016-09-15 16:27:05 -04002285
David Benjamin31640932017-10-11 13:22:39 -04002286static bool ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05002287 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002288 if (hs->max_version <= TLS1_2_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04002289 return true;
Steven Valdezfdd10992016-09-15 16:27:05 -04002290 }
2291
2292 CBB contents, versions;
2293 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2294 !CBB_add_u16_length_prefixed(out, &contents) ||
2295 !CBB_add_u8_length_prefixed(&contents, &versions)) {
David Benjamin31640932017-10-11 13:22:39 -04002296 return false;
Steven Valdezfdd10992016-09-15 16:27:05 -04002297 }
2298
David Benjaminc11ea9422017-08-29 16:33:21 -04002299 // Add a fake version. See draft-davidben-tls-grease-01.
David Benjamind9791bf2016-09-27 16:39:52 -04002300 if (ssl->ctx->grease_enabled &&
David Benjamina7bc9442018-01-18 10:08:53 -05002301 !CBB_add_u16(&versions, ssl_get_grease_value(hs, ssl_grease_version))) {
David Benjamin31640932017-10-11 13:22:39 -04002302 return false;
David Benjamind9791bf2016-09-27 16:39:52 -04002303 }
2304
Steven Valdez8f36c512017-06-20 10:55:02 -04002305 if (!ssl_add_supported_versions(hs, &versions) ||
2306 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04002307 return false;
Steven Valdezfdd10992016-09-15 16:27:05 -04002308 }
2309
David Benjamin31640932017-10-11 13:22:39 -04002310 return true;
Steven Valdezfdd10992016-09-15 16:27:05 -04002311}
2312
2313
David Benjaminc11ea9422017-08-29 16:33:21 -04002314// Cookie
2315//
David Benjamina130ce02018-08-14 22:26:39 -05002316// https://tools.ietf.org/html/rfc8446#section-4.2.2
David Benjamin3baa6e12016-10-07 21:10:38 -04002317
David Benjamin31640932017-10-11 13:22:39 -04002318static bool ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminb9493552017-09-27 19:02:51 -04002319 if (hs->cookie.empty()) {
David Benjamin31640932017-10-11 13:22:39 -04002320 return true;
David Benjamin3baa6e12016-10-07 21:10:38 -04002321 }
2322
2323 CBB contents, cookie;
2324 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2325 !CBB_add_u16_length_prefixed(out, &contents) ||
2326 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin08f5c762017-09-21 02:43:05 -04002327 !CBB_add_bytes(&cookie, hs->cookie.data(), hs->cookie.size()) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002328 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04002329 return false;
David Benjamin3baa6e12016-10-07 21:10:38 -04002330 }
2331
David Benjaminc11ea9422017-08-29 16:33:21 -04002332 // The cookie is no longer needed in memory.
David Benjamin08f5c762017-09-21 02:43:05 -04002333 hs->cookie.Reset();
David Benjamin31640932017-10-11 13:22:39 -04002334 return true;
David Benjamin3baa6e12016-10-07 21:10:38 -04002335}
2336
2337
David Benjamina130ce02018-08-14 22:26:39 -05002338// Supported Groups
David Benjaminc11ea9422017-08-29 16:33:21 -04002339//
David Benjamina130ce02018-08-14 22:26:39 -05002340// https://tools.ietf.org/html/rfc4492#section-5.1.1
2341// https://tools.ietf.org/html/rfc8446#section-4.2.7
Adam Langley273d49c2015-07-20 16:38:52 -07002342
David Benjamin31640932017-10-11 13:22:39 -04002343static bool ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05002344 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002345 CBB contents, groups_bytes;
2346 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002347 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002348 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
David Benjamin31640932017-10-11 13:22:39 -04002349 return false;
Adam Langley273d49c2015-07-20 16:38:52 -07002350 }
2351
David Benjaminc11ea9422017-08-29 16:33:21 -04002352 // Add a fake group. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002353 if (ssl->ctx->grease_enabled &&
2354 !CBB_add_u16(&groups_bytes,
David Benjamina7bc9442018-01-18 10:08:53 -05002355 ssl_get_grease_value(hs, ssl_grease_group))) {
David Benjamin31640932017-10-11 13:22:39 -04002356 return false;
David Benjamin65ac9972016-09-02 21:35:25 -04002357 }
2358
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002359 for (uint16_t group : tls1_get_grouplist(hs)) {
Kris Kwiatkowski78c88c92019-03-11 14:40:59 +00002360 if (is_post_quantum_group(group) &&
Adam Langley7b935932018-11-12 13:53:42 -08002361 hs->max_version < TLS1_3_VERSION) {
2362 continue;
2363 }
David Benjamincf0ce672017-09-21 02:25:59 -04002364 if (!CBB_add_u16(&groups_bytes, group)) {
David Benjamin31640932017-10-11 13:22:39 -04002365 return false;
Adam Langley273d49c2015-07-20 16:38:52 -07002366 }
2367 }
2368
2369 return CBB_flush(out);
2370}
2371
David Benjamin31640932017-10-11 13:22:39 -04002372static bool ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2373 uint8_t *out_alert,
2374 CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002375 // This extension is not expected to be echoed by servers in TLS 1.2, but some
2376 // BigIP servers send it nonetheless, so do not enforce this.
David Benjamin31640932017-10-11 13:22:39 -04002377 return true;
Adam Langley273d49c2015-07-20 16:38:52 -07002378}
2379
David Benjaminb1cf48e2017-09-21 11:37:46 -04002380static bool parse_u16_array(const CBS *cbs, Array<uint16_t> *out) {
2381 CBS copy = *cbs;
2382 if ((CBS_len(&copy) & 1) != 0) {
2383 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2384 return false;
2385 }
2386
2387 Array<uint16_t> ret;
2388 if (!ret.Init(CBS_len(&copy) / 2)) {
2389 return false;
2390 }
2391 for (size_t i = 0; i < ret.size(); i++) {
2392 if (!CBS_get_u16(&copy, &ret[i])) {
2393 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2394 return false;
2395 }
2396 }
2397
2398 assert(CBS_len(&copy) == 0);
2399 *out = std::move(ret);
2400 return 1;
2401}
2402
David Benjamin31640932017-10-11 13:22:39 -04002403static bool ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002404 uint8_t *out_alert,
David Benjamin31640932017-10-11 13:22:39 -04002405 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002406 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04002407 return true;
Adam Langley273d49c2015-07-20 16:38:52 -07002408 }
2409
Steven Valdezce902a92016-05-17 11:47:53 -04002410 CBS supported_group_list;
2411 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2412 CBS_len(&supported_group_list) == 0 ||
David Benjaminb1cf48e2017-09-21 11:37:46 -04002413 CBS_len(contents) != 0 ||
2414 !parse_u16_array(&supported_group_list, &hs->peer_supported_group_list)) {
David Benjamin31640932017-10-11 13:22:39 -04002415 return false;
Adam Langley273d49c2015-07-20 16:38:52 -07002416 }
2417
David Benjamin31640932017-10-11 13:22:39 -04002418 return true;
Adam Langley273d49c2015-07-20 16:38:52 -07002419}
2420
Nick Harper36fcc4c2017-09-21 15:02:22 -07002421// Token Binding
2422//
2423// https://tools.ietf.org/html/draft-ietf-tokbind-negotiation-10
2424
2425// The Token Binding version number currently matches the draft number of
2426// draft-ietf-tokbind-protocol, and when published as an RFC it will be 0x0100.
2427// Since there are no wire changes to the protocol from draft 13 through the
2428// current draft (16), this implementation supports all versions in that range.
2429static uint16_t kTokenBindingMaxVersion = 16;
2430static uint16_t kTokenBindingMinVersion = 13;
2431
2432static bool ext_token_binding_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2433 SSL *const ssl = hs->ssl;
David Benjamin0ce090a2018-07-02 20:24:40 -04002434 if (hs->config->token_binding_params.empty() || SSL_is_dtls(ssl)) {
Nick Harper36fcc4c2017-09-21 15:02:22 -07002435 return true;
2436 }
2437
2438 CBB contents, params;
2439 if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) ||
2440 !CBB_add_u16_length_prefixed(out, &contents) ||
2441 !CBB_add_u16(&contents, kTokenBindingMaxVersion) ||
2442 !CBB_add_u8_length_prefixed(&contents, &params) ||
David Benjamin0ce090a2018-07-02 20:24:40 -04002443 !CBB_add_bytes(&params, hs->config->token_binding_params.data(),
2444 hs->config->token_binding_params.size()) ||
Nick Harper36fcc4c2017-09-21 15:02:22 -07002445 !CBB_flush(out)) {
2446 return false;
2447 }
2448
2449 return true;
2450}
2451
2452static bool ext_token_binding_parse_serverhello(SSL_HANDSHAKE *hs,
2453 uint8_t *out_alert,
2454 CBS *contents) {
2455 SSL *const ssl = hs->ssl;
2456 if (contents == nullptr) {
2457 return true;
2458 }
2459
2460 CBS params_list;
2461 uint16_t version;
2462 uint8_t param;
2463 if (!CBS_get_u16(contents, &version) ||
2464 !CBS_get_u8_length_prefixed(contents, &params_list) ||
2465 !CBS_get_u8(&params_list, &param) ||
2466 CBS_len(&params_list) > 0 ||
2467 CBS_len(contents) > 0) {
2468 *out_alert = SSL_AD_DECODE_ERROR;
2469 return false;
2470 }
2471
2472 // The server-negotiated version must be less than or equal to our version.
2473 if (version > kTokenBindingMaxVersion) {
2474 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2475 return false;
2476 }
2477
2478 // If the server-selected version is less than what we support, then Token
2479 // Binding wasn't negotiated (but the extension was parsed successfully).
2480 if (version < kTokenBindingMinVersion) {
2481 return true;
2482 }
2483
David Benjamin0ce090a2018-07-02 20:24:40 -04002484 for (uint8_t config_param : hs->config->token_binding_params) {
2485 if (param == config_param) {
David Benjamin9f0e7cb2018-04-12 15:36:30 -04002486 ssl->s3->negotiated_token_binding_param = param;
2487 ssl->s3->token_binding_negotiated = true;
Nick Harper36fcc4c2017-09-21 15:02:22 -07002488 return true;
2489 }
2490 }
2491
2492 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2493 return false;
2494}
2495
2496// select_tb_param looks for the first token binding param in
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002497// |hs->ssl->token_binding_params| that is also in |params| and puts it in
2498// |hs->ssl->negotiated_token_binding_param|. It returns true if a token binding
Nick Harper36fcc4c2017-09-21 15:02:22 -07002499// param is found, and false otherwise.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002500static bool select_tb_param(SSL_HANDSHAKE *hs,
2501 Span<const uint8_t> peer_params) {
David Benjamin0ce090a2018-07-02 20:24:40 -04002502 for (uint8_t tb_param : hs->config->token_binding_params) {
Nick Harper36fcc4c2017-09-21 15:02:22 -07002503 for (uint8_t peer_param : peer_params) {
2504 if (tb_param == peer_param) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002505 hs->ssl->s3->negotiated_token_binding_param = tb_param;
Nick Harper36fcc4c2017-09-21 15:02:22 -07002506 return true;
2507 }
2508 }
2509 }
2510 return false;
2511}
2512
2513static bool ext_token_binding_parse_clienthello(SSL_HANDSHAKE *hs,
2514 uint8_t *out_alert,
2515 CBS *contents) {
2516 SSL *const ssl = hs->ssl;
David Benjamin0ce090a2018-07-02 20:24:40 -04002517 if (contents == nullptr || hs->config->token_binding_params.empty()) {
Nick Harper36fcc4c2017-09-21 15:02:22 -07002518 return true;
2519 }
2520
2521 CBS params;
2522 uint16_t version;
2523 if (!CBS_get_u16(contents, &version) ||
2524 !CBS_get_u8_length_prefixed(contents, &params) ||
2525 CBS_len(&params) == 0 ||
2526 CBS_len(contents) > 0) {
2527 *out_alert = SSL_AD_DECODE_ERROR;
2528 return false;
2529 }
2530
2531 // If the client-selected version is less than what we support, then Token
2532 // Binding wasn't negotiated (but the extension was parsed successfully).
2533 if (version < kTokenBindingMinVersion) {
2534 return true;
2535 }
2536
2537 // If the client-selected version is higher than we support, use our max
2538 // version. Otherwise, use the client's version.
2539 hs->negotiated_token_binding_version =
2540 std::min(version, kTokenBindingMaxVersion);
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07002541 if (!select_tb_param(hs, params)) {
Nick Harper36fcc4c2017-09-21 15:02:22 -07002542 return true;
2543 }
2544
David Benjamin9f0e7cb2018-04-12 15:36:30 -04002545 ssl->s3->token_binding_negotiated = true;
Nick Harper36fcc4c2017-09-21 15:02:22 -07002546 return true;
2547}
2548
2549static bool ext_token_binding_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2550 SSL *const ssl = hs->ssl;
2551
David Benjamin9f0e7cb2018-04-12 15:36:30 -04002552 if (!ssl->s3->token_binding_negotiated) {
Nick Harper36fcc4c2017-09-21 15:02:22 -07002553 return true;
2554 }
2555
2556 CBB contents, params;
2557 if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) ||
2558 !CBB_add_u16_length_prefixed(out, &contents) ||
2559 !CBB_add_u16(&contents, hs->negotiated_token_binding_version) ||
2560 !CBB_add_u8_length_prefixed(&contents, &params) ||
David Benjamin9f0e7cb2018-04-12 15:36:30 -04002561 !CBB_add_u8(&params, ssl->s3->negotiated_token_binding_param) ||
Nick Harper36fcc4c2017-09-21 15:02:22 -07002562 !CBB_flush(out)) {
2563 return false;
2564 }
2565
2566 return true;
2567}
Adam Langley273d49c2015-07-20 16:38:52 -07002568
Nick Harper3c034b22017-12-22 15:50:43 -08002569// QUIC Transport Parameters
2570
2571static bool ext_quic_transport_params_add_clienthello(SSL_HANDSHAKE *hs,
2572 CBB *out) {
Nick Harper72cff812020-03-26 18:06:16 -07002573 if (hs->config->quic_transport_params.empty() && !hs->ssl->quic_method) {
Nick Harper3c034b22017-12-22 15:50:43 -08002574 return true;
2575 }
Nick Harper72cff812020-03-26 18:06:16 -07002576 if (hs->config->quic_transport_params.empty() || !hs->ssl->quic_method) {
2577 // QUIC Transport Parameters must be sent over QUIC, and they must not be
2578 // sent over non-QUIC transports. If transport params are set, then
2579 // SSL(_CTX)_set_quic_method must also be called.
2580 OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_TRANSPORT_PARAMETERS_MISCONFIGURED);
2581 return false;
2582 }
2583 assert(hs->min_version > TLS1_2_VERSION);
Nick Harper3c034b22017-12-22 15:50:43 -08002584
2585 CBB contents;
2586 if (!CBB_add_u16(out, TLSEXT_TYPE_quic_transport_parameters) ||
2587 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0ce090a2018-07-02 20:24:40 -04002588 !CBB_add_bytes(&contents, hs->config->quic_transport_params.data(),
2589 hs->config->quic_transport_params.size()) ||
Nick Harper3c034b22017-12-22 15:50:43 -08002590 !CBB_flush(out)) {
2591 return false;
2592 }
2593 return true;
2594}
2595
2596static bool ext_quic_transport_params_parse_serverhello(SSL_HANDSHAKE *hs,
2597 uint8_t *out_alert,
2598 CBS *contents) {
2599 SSL *const ssl = hs->ssl;
2600 if (contents == nullptr) {
Nick Harper72cff812020-03-26 18:06:16 -07002601 if (!ssl->quic_method) {
2602 return true;
2603 }
2604 assert(ssl->quic_method);
2605 *out_alert = SSL_AD_MISSING_EXTENSION;
2606 return false;
Nick Harper3c034b22017-12-22 15:50:43 -08002607 }
Nick Harper72cff812020-03-26 18:06:16 -07002608 if (!ssl->quic_method) {
Nick Harper3c034b22017-12-22 15:50:43 -08002609 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2610 return false;
2611 }
Nick Harper72cff812020-03-26 18:06:16 -07002612 // QUIC requires TLS 1.3.
2613 assert(ssl_protocol_version(ssl) == TLS1_3_VERSION);
Nick Harper3c034b22017-12-22 15:50:43 -08002614
2615 return ssl->s3->peer_quic_transport_params.CopyFrom(*contents);
2616}
2617
2618static bool ext_quic_transport_params_parse_clienthello(SSL_HANDSHAKE *hs,
2619 uint8_t *out_alert,
2620 CBS *contents) {
2621 SSL *const ssl = hs->ssl;
Nick Harper72cff812020-03-26 18:06:16 -07002622 if (!contents) {
2623 if (!ssl->quic_method) {
2624 if (hs->config->quic_transport_params.empty()) {
2625 return true;
2626 }
2627 // QUIC transport parameters must not be set if |ssl| is not configured
2628 // for QUIC.
2629 OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_TRANSPORT_PARAMETERS_MISCONFIGURED);
2630 *out_alert = SSL_AD_INTERNAL_ERROR;
2631 }
2632 *out_alert = SSL_AD_MISSING_EXTENSION;
2633 return false;
2634 }
2635 if (!ssl->quic_method) {
2636 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2637 return false;
Nick Harper3c034b22017-12-22 15:50:43 -08002638 }
Nick Harper80ddfc72020-03-11 18:26:31 -07002639 assert(ssl_protocol_version(ssl) == TLS1_3_VERSION);
Nick Harper3c034b22017-12-22 15:50:43 -08002640 return ssl->s3->peer_quic_transport_params.CopyFrom(*contents);
2641}
2642
2643static bool ext_quic_transport_params_add_serverhello(SSL_HANDSHAKE *hs,
2644 CBB *out) {
Nick Harper72cff812020-03-26 18:06:16 -07002645 assert(hs->ssl->quic_method != nullptr);
David Benjamin0ce090a2018-07-02 20:24:40 -04002646 if (hs->config->quic_transport_params.empty()) {
Nick Harper72cff812020-03-26 18:06:16 -07002647 // Transport parameters must be set when using QUIC.
2648 OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_TRANSPORT_PARAMETERS_MISCONFIGURED);
2649 return false;
Nick Harper3c034b22017-12-22 15:50:43 -08002650 }
2651
2652 CBB contents;
2653 if (!CBB_add_u16(out, TLSEXT_TYPE_quic_transport_parameters) ||
2654 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0ce090a2018-07-02 20:24:40 -04002655 !CBB_add_bytes(&contents, hs->config->quic_transport_params.data(),
2656 hs->config->quic_transport_params.size()) ||
Nick Harper3c034b22017-12-22 15:50:43 -08002657 !CBB_flush(out)) {
2658 return false;
2659 }
2660
2661 return true;
2662}
2663
Christopher Patton6c1b3762018-07-17 12:49:41 -07002664// Delegated credentials.
2665//
2666// https://tools.ietf.org/html/draft-ietf-tls-subcerts
2667
2668static bool ext_delegated_credential_add_clienthello(SSL_HANDSHAKE *hs,
2669 CBB *out) {
2670 return true;
2671}
2672
2673static bool ext_delegated_credential_parse_clienthello(SSL_HANDSHAKE *hs,
2674 uint8_t *out_alert,
2675 CBS *contents) {
Christopher Patton6c1b3762018-07-17 12:49:41 -07002676 if (contents == nullptr || ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) {
2677 // Don't use delegated credentials unless we're negotiating TLS 1.3 or
2678 // higher.
2679 return true;
2680 }
2681
Watson Ladddcd6e442020-08-10 15:12:45 -04002682 // The contents of the extension are the signature algorithms the client will
2683 // accept for a delegated credential.
2684 CBS sigalg_list;
2685 if (!CBS_get_u16_length_prefixed(contents, &sigalg_list) ||
2686 CBS_len(&sigalg_list) == 0 ||
2687 CBS_len(contents) != 0 ||
2688 !parse_u16_array(&sigalg_list, &hs->peer_delegated_credential_sigalgs)) {
2689 return false;
2690 }
2691
Christopher Patton6c1b3762018-07-17 12:49:41 -07002692 hs->delegated_credential_requested = true;
2693 return true;
2694}
2695
Adam Langleya307cb72018-05-02 09:06:48 -07002696// Certificate compression
2697
2698static bool cert_compression_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley0080d832018-06-07 16:39:49 -07002699 bool first = true;
2700 CBB contents, algs;
2701
David Benjamin8fe15842019-10-08 16:57:38 -04002702 for (const auto &alg : hs->ssl->ctx->cert_compression_algs) {
2703 if (alg.decompress == nullptr) {
Adam Langley0080d832018-06-07 16:39:49 -07002704 continue;
2705 }
2706
2707 if (first && (!CBB_add_u16(out, TLSEXT_TYPE_cert_compression) ||
2708 !CBB_add_u16_length_prefixed(out, &contents) ||
2709 !CBB_add_u8_length_prefixed(&contents, &algs))) {
2710 return false;
2711 }
2712 first = false;
David Benjamin8fe15842019-10-08 16:57:38 -04002713 if (!CBB_add_u16(&algs, alg.alg_id)) {
Adam Langley0080d832018-06-07 16:39:49 -07002714 return false;
2715 }
2716 }
2717
2718 return first || CBB_flush(out);
Adam Langleya307cb72018-05-02 09:06:48 -07002719}
2720
2721static bool cert_compression_parse_serverhello(SSL_HANDSHAKE *hs,
2722 uint8_t *out_alert,
2723 CBS *contents) {
2724 if (contents == nullptr) {
2725 return true;
2726 }
2727
2728 // The server may not echo this extension. Any server to client negotiation is
2729 // advertised in the CertificateRequest message.
2730 return false;
2731}
2732
2733static bool cert_compression_parse_clienthello(SSL_HANDSHAKE *hs,
2734 uint8_t *out_alert,
2735 CBS *contents) {
2736 if (contents == nullptr) {
2737 return true;
2738 }
2739
David Benjamin8fe15842019-10-08 16:57:38 -04002740 const SSL_CTX *ctx = hs->ssl->ctx.get();
2741 const size_t num_algs = ctx->cert_compression_algs.size();
Adam Langleya307cb72018-05-02 09:06:48 -07002742
2743 CBS alg_ids;
2744 if (!CBS_get_u8_length_prefixed(contents, &alg_ids) ||
2745 CBS_len(contents) != 0 ||
2746 CBS_len(&alg_ids) == 0 ||
2747 CBS_len(&alg_ids) % 2 == 1) {
2748 return false;
2749 }
2750
2751 const size_t num_given_alg_ids = CBS_len(&alg_ids) / 2;
2752 Array<uint16_t> given_alg_ids;
2753 if (!given_alg_ids.Init(num_given_alg_ids)) {
2754 return false;
2755 }
2756
2757 size_t best_index = num_algs;
2758 size_t given_alg_idx = 0;
2759
2760 while (CBS_len(&alg_ids) > 0) {
2761 uint16_t alg_id;
2762 if (!CBS_get_u16(&alg_ids, &alg_id)) {
2763 return false;
2764 }
2765
2766 given_alg_ids[given_alg_idx++] = alg_id;
2767
2768 for (size_t i = 0; i < num_algs; i++) {
David Benjamin8fe15842019-10-08 16:57:38 -04002769 const auto &alg = ctx->cert_compression_algs[i];
2770 if (alg.alg_id == alg_id && alg.compress != nullptr) {
Adam Langleya307cb72018-05-02 09:06:48 -07002771 if (i < best_index) {
2772 best_index = i;
2773 }
2774 break;
2775 }
2776 }
2777 }
2778
2779 qsort(given_alg_ids.data(), given_alg_ids.size(), sizeof(uint16_t),
2780 compare_uint16_t);
2781 for (size_t i = 1; i < num_given_alg_ids; i++) {
2782 if (given_alg_ids[i - 1] == given_alg_ids[i]) {
2783 return false;
2784 }
2785 }
2786
2787 if (best_index < num_algs &&
2788 ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
2789 hs->cert_compression_negotiated = true;
David Benjamin8fe15842019-10-08 16:57:38 -04002790 hs->cert_compression_alg_id = ctx->cert_compression_algs[best_index].alg_id;
Adam Langleya307cb72018-05-02 09:06:48 -07002791 }
2792
2793 return true;
2794}
2795
2796static bool cert_compression_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2797 return true;
2798}
Nick Harper3c034b22017-12-22 15:50:43 -08002799
Adam Langleya86c6982019-07-16 15:26:21 -07002800
David Benjaminc11ea9422017-08-29 16:33:21 -04002801// kExtensions contains all the supported extensions.
Adam Langley614c66a2015-06-12 15:26:58 -07002802static const struct tls_extension kExtensions[] = {
2803 {
2804 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002805 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002806 ext_sni_add_clienthello,
2807 ext_sni_parse_serverhello,
2808 ext_sni_parse_clienthello,
2809 ext_sni_add_serverhello,
2810 },
Adam Langley0a056712015-07-01 15:03:33 -07002811 {
2812 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002813 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002814 ext_ems_add_clienthello,
2815 ext_ems_parse_serverhello,
2816 ext_ems_parse_clienthello,
2817 ext_ems_add_serverhello,
2818 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002819 {
Adam Langleyd6680952018-08-23 08:01:23 -07002820 TLSEXT_TYPE_renegotiate,
2821 NULL,
2822 ext_ri_add_clienthello,
2823 ext_ri_parse_serverhello,
2824 ext_ri_parse_clienthello,
2825 ext_ri_add_serverhello,
2826 },
2827 {
2828 TLSEXT_TYPE_supported_groups,
2829 NULL,
2830 ext_supported_groups_add_clienthello,
2831 ext_supported_groups_parse_serverhello,
2832 ext_supported_groups_parse_clienthello,
2833 dont_add_serverhello,
2834 },
2835 {
2836 TLSEXT_TYPE_ec_point_formats,
2837 NULL,
2838 ext_ec_point_add_clienthello,
2839 ext_ec_point_parse_serverhello,
2840 ext_ec_point_parse_clienthello,
2841 ext_ec_point_add_serverhello,
2842 },
2843 {
Adam Langley9b05bc52015-07-01 15:25:33 -07002844 TLSEXT_TYPE_session_ticket,
2845 NULL,
2846 ext_ticket_add_clienthello,
2847 ext_ticket_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04002848 // Ticket extension client parsing is handled in ssl_session.c
Steven Valdez6b8509a2016-07-12 13:38:32 -04002849 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002850 ext_ticket_add_serverhello,
2851 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002852 {
Adam Langleyd6680952018-08-23 08:01:23 -07002853 TLSEXT_TYPE_application_layer_protocol_negotiation,
2854 NULL,
2855 ext_alpn_add_clienthello,
2856 ext_alpn_parse_serverhello,
2857 // ALPN is negotiated late in |ssl_negotiate_alpn|.
2858 ignore_parse_clienthello,
2859 ext_alpn_add_serverhello,
2860 },
2861 {
2862 TLSEXT_TYPE_status_request,
2863 NULL,
2864 ext_ocsp_add_clienthello,
2865 ext_ocsp_parse_serverhello,
2866 ext_ocsp_parse_clienthello,
2867 ext_ocsp_add_serverhello,
2868 },
2869 {
Adam Langley2e857bd2015-07-01 16:09:19 -07002870 TLSEXT_TYPE_signature_algorithms,
2871 NULL,
2872 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002873 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002874 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002875 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002876 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002877 {
Adam Langley97dfcbf2015-07-01 18:35:20 -07002878 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002879 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002880 ext_npn_add_clienthello,
2881 ext_npn_parse_serverhello,
2882 ext_npn_parse_clienthello,
2883 ext_npn_add_serverhello,
2884 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002885 {
2886 TLSEXT_TYPE_certificate_timestamp,
2887 NULL,
2888 ext_sct_add_clienthello,
2889 ext_sct_parse_serverhello,
2890 ext_sct_parse_clienthello,
2891 ext_sct_add_serverhello,
2892 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002893 {
Adam Langley49c7af12015-07-10 14:33:46 -07002894 TLSEXT_TYPE_channel_id,
2895 ext_channel_id_init,
2896 ext_channel_id_add_clienthello,
2897 ext_channel_id_parse_serverhello,
2898 ext_channel_id_parse_clienthello,
2899 ext_channel_id_add_serverhello,
2900 },
Adam Langley391250d2015-07-15 19:06:07 -07002901 {
2902 TLSEXT_TYPE_srtp,
2903 ext_srtp_init,
2904 ext_srtp_add_clienthello,
2905 ext_srtp_parse_serverhello,
2906 ext_srtp_parse_clienthello,
2907 ext_srtp_add_serverhello,
2908 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002909 {
Steven Valdez7e5dd252018-01-22 15:20:31 -05002910 TLSEXT_TYPE_key_share,
Steven Valdez143e8b32016-07-11 13:19:03 -04002911 NULL,
2912 ext_key_share_add_clienthello,
2913 forbid_parse_serverhello,
2914 ignore_parse_clienthello,
2915 dont_add_serverhello,
2916 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002917 {
Steven Valdeza833c352016-11-01 13:39:36 -04002918 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002919 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002920 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002921 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002922 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002923 dont_add_serverhello,
2924 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002925 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002926 TLSEXT_TYPE_early_data,
2927 NULL,
2928 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002929 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002930 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002931 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002932 },
2933 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002934 TLSEXT_TYPE_supported_versions,
2935 NULL,
2936 ext_supported_versions_add_clienthello,
2937 forbid_parse_serverhello,
2938 ignore_parse_clienthello,
2939 dont_add_serverhello,
2940 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002941 {
2942 TLSEXT_TYPE_cookie,
2943 NULL,
2944 ext_cookie_add_clienthello,
2945 forbid_parse_serverhello,
2946 ignore_parse_clienthello,
2947 dont_add_serverhello,
2948 },
Adam Langley512a2892017-12-30 08:04:39 -08002949 {
Nick Harper3c034b22017-12-22 15:50:43 -08002950 TLSEXT_TYPE_quic_transport_parameters,
2951 NULL,
2952 ext_quic_transport_params_add_clienthello,
2953 ext_quic_transport_params_parse_serverhello,
2954 ext_quic_transport_params_parse_clienthello,
2955 ext_quic_transport_params_add_serverhello,
2956 },
Adam Langley273d49c2015-07-20 16:38:52 -07002957 {
Nick Harper36fcc4c2017-09-21 15:02:22 -07002958 TLSEXT_TYPE_token_binding,
2959 NULL,
2960 ext_token_binding_add_clienthello,
2961 ext_token_binding_parse_serverhello,
2962 ext_token_binding_parse_clienthello,
2963 ext_token_binding_add_serverhello,
2964 },
Adam Langleya307cb72018-05-02 09:06:48 -07002965 {
2966 TLSEXT_TYPE_cert_compression,
2967 NULL,
2968 cert_compression_add_clienthello,
2969 cert_compression_parse_serverhello,
2970 cert_compression_parse_clienthello,
2971 cert_compression_add_serverhello,
2972 },
Christopher Patton6c1b3762018-07-17 12:49:41 -07002973 {
2974 TLSEXT_TYPE_delegated_credential,
2975 NULL,
2976 ext_delegated_credential_add_clienthello,
2977 forbid_parse_serverhello,
2978 ext_delegated_credential_parse_clienthello,
2979 dont_add_serverhello,
2980 },
Adam Langley614c66a2015-06-12 15:26:58 -07002981};
2982
2983#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2984
David Benjamina3d76d02017-07-14 19:36:07 -04002985static_assert(kNumExtensions <=
2986 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
2987 "too many extensions for sent bitset");
2988static_assert(kNumExtensions <=
2989 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2990 "too many extensions for received bitset");
Adam Langley4cfa96b2015-07-01 11:56:55 -07002991
Adam Langley614c66a2015-06-12 15:26:58 -07002992static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2993 uint16_t value) {
2994 unsigned i;
2995 for (i = 0; i < kNumExtensions; i++) {
2996 if (kExtensions[i].value == value) {
2997 *out_index = i;
2998 return &kExtensions[i];
2999 }
3000 }
3001
3002 return NULL;
3003}
3004
David Benjamin861abcc2018-07-14 17:40:26 -04003005bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out,
3006 size_t header_len) {
David Benjamin8c880a22016-12-03 02:20:34 -05003007 SSL *const ssl = hs->ssl;
David Benjamine8d53502015-10-10 14:13:23 -04003008 CBB extensions;
3009 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003010 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04003011 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003012 }
Adam Langley95c29f32014-06-20 12:00:00 -07003013
David Benjamin64770122019-05-04 11:00:04 -05003014 // Note we may send multiple ClientHellos for DTLS HelloVerifyRequest and TLS
3015 // 1.3 HelloRetryRequest. For the latter, the extensions may change, so it is
3016 // important to reset this value.
David Benjamin8c880a22016-12-03 02:20:34 -05003017 hs->extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07003018
David Benjamin54091232016-09-05 12:47:25 -04003019 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003020 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05003021 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07003022 }
3023 }
Adam Langley95c29f32014-06-20 12:00:00 -07003024
David Benjamin65ac9972016-09-02 21:35:25 -04003025 uint16_t grease_ext1 = 0;
3026 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003027 // Add a fake empty extension. See draft-davidben-tls-grease-01.
David Benjamina7bc9442018-01-18 10:08:53 -05003028 grease_ext1 = ssl_get_grease_value(hs, ssl_grease_extension1);
David Benjamin65ac9972016-09-02 21:35:25 -04003029 if (!CBB_add_u16(&extensions, grease_ext1) ||
3030 !CBB_add_u16(&extensions, 0 /* zero length */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003031 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04003032 return false;
David Benjamin65ac9972016-09-02 21:35:25 -04003033 }
3034 }
3035
Adam Langley7f4f41f2018-08-23 08:58:49 -07003036 bool last_was_empty = false;
David Benjamin54091232016-09-05 12:47:25 -04003037 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003038 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05003039 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003040 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003041 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin861abcc2018-07-14 17:40:26 -04003042 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003043 }
Adam Langley95c29f32014-06-20 12:00:00 -07003044
Adam Langley7f4f41f2018-08-23 08:58:49 -07003045 const size_t bytes_written = CBB_len(&extensions) - len_before;
3046 if (bytes_written != 0) {
David Benjamin8c880a22016-12-03 02:20:34 -05003047 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07003048 }
Adam Langley7f4f41f2018-08-23 08:58:49 -07003049 // If the difference in lengths is only four bytes then the extension had
3050 // an empty body.
3051 last_was_empty = (bytes_written == 4);
Adam Langleyfcf25832014-12-18 17:42:32 -08003052 }
Adam Langley75712922014-10-10 16:23:43 -07003053
David Benjamin65ac9972016-09-02 21:35:25 -04003054 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003055 // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
David Benjamina7bc9442018-01-18 10:08:53 -05003056 uint16_t grease_ext2 = ssl_get_grease_value(hs, ssl_grease_extension2);
David Benjamin65ac9972016-09-02 21:35:25 -04003057
David Benjaminc11ea9422017-08-29 16:33:21 -04003058 // The two fake extensions must not have the same value. GREASE values are
3059 // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
3060 // one.
David Benjamin65ac9972016-09-02 21:35:25 -04003061 if (grease_ext1 == grease_ext2) {
3062 grease_ext2 ^= 0x1010;
3063 }
3064
3065 if (!CBB_add_u16(&extensions, grease_ext2) ||
3066 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
3067 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003068 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04003069 return false;
David Benjamin65ac9972016-09-02 21:35:25 -04003070 }
Adam Langley7f4f41f2018-08-23 08:58:49 -07003071
3072 last_was_empty = false;
David Benjamin65ac9972016-09-02 21:35:25 -04003073 }
3074
Nick Harper281a8f52020-07-28 10:12:49 -07003075 if (!SSL_is_dtls(ssl) && !ssl->quic_method) {
David Benjamin8c880a22016-12-03 02:20:34 -05003076 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04003077 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langley7f4f41f2018-08-23 08:58:49 -07003078 size_t padding_len = 0;
3079
3080 // The final extension must be non-empty. WebSphere Application
3081 // Server 7.0 is intolerant to the last extension being zero-length. See
3082 // https://crbug.com/363583.
3083 if (last_was_empty && psk_extension_len == 0) {
3084 padding_len = 1;
3085 // The addition of the padding extension may push us into the F5 bug.
3086 header_len += 4 + padding_len;
3087 }
3088
3089 // Add padding to workaround bugs in F5 terminators. See RFC 7685.
3090 //
3091 // NB: because this code works out the length of all existing extensions
3092 // it MUST always appear last (save for any PSK extension).
Adam Langleyfcf25832014-12-18 17:42:32 -08003093 if (header_len > 0xff && header_len < 0x200) {
Adam Langley7f4f41f2018-08-23 08:58:49 -07003094 // If our calculations already included a padding extension, remove that
3095 // factor because we're about to change its length.
3096 if (padding_len != 0) {
3097 header_len -= 4 + padding_len;
3098 }
3099 padding_len = 0x200 - header_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04003100 // Extensions take at least four bytes to encode. Always include at least
3101 // one byte of data if including the extension. WebSphere Application
3102 // Server 7.0 is intolerant to the last extension being zero-length. See
3103 // https://crbug.com/363583.
Adam Langleyfcf25832014-12-18 17:42:32 -08003104 if (padding_len >= 4 + 1) {
3105 padding_len -= 4;
3106 } else {
3107 padding_len = 1;
3108 }
Adam Langley7f4f41f2018-08-23 08:58:49 -07003109 }
Adam Langley95c29f32014-06-20 12:00:00 -07003110
Adam Langley7f4f41f2018-08-23 08:58:49 -07003111 if (padding_len != 0) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003112 uint8_t *padding_bytes;
3113 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
3114 !CBB_add_u16(&extensions, padding_len) ||
3115 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003116 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04003117 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003118 }
Adam Langley75712922014-10-10 16:23:43 -07003119
David Benjamin17cf2cb2016-12-13 01:07:13 -05003120 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08003121 }
3122 }
Adam Langley75712922014-10-10 16:23:43 -07003123
David Benjaminc11ea9422017-08-29 16:33:21 -04003124 // The PSK extension must be last, including after the padding.
David Benjamin8c880a22016-12-03 02:20:34 -05003125 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003126 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04003127 return false;
Steven Valdeza833c352016-11-01 13:39:36 -04003128 }
3129
David Benjaminc11ea9422017-08-29 16:33:21 -04003130 // Discard empty extensions blocks.
David Benjamina01deee2015-12-08 18:56:31 -05003131 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04003132 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07003133 }
3134
David Benjamine8d53502015-10-10 14:13:23 -04003135 return CBB_flush(out);
Adam Langleyfcf25832014-12-18 17:42:32 -08003136}
Adam Langley95c29f32014-06-20 12:00:00 -07003137
David Benjamin861abcc2018-07-14 17:40:26 -04003138bool ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05003139 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04003140 CBB extensions;
3141 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003142 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07003143 }
3144
David Benjamin8c880a22016-12-03 02:20:34 -05003145 for (unsigned i = 0; i < kNumExtensions; i++) {
3146 if (!(hs->extensions.received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003147 // Don't send extensions that were not received.
Adam Langley614c66a2015-06-12 15:26:58 -07003148 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08003149 }
Adam Langley95c29f32014-06-20 12:00:00 -07003150
David Benjamin8c880a22016-12-03 02:20:34 -05003151 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003152 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003153 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07003154 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07003155 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003156 }
Adam Langley95c29f32014-06-20 12:00:00 -07003157
David Benjaminc11ea9422017-08-29 16:33:21 -04003158 // Discard empty extensions blocks before TLS 1.3.
David Benjamind1e3ce12017-10-06 18:31:15 -04003159 if (ssl_protocol_version(ssl) < TLS1_3_VERSION &&
Steven Valdez143e8b32016-07-11 13:19:03 -04003160 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04003161 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07003162 }
3163
David Benjamin56380462015-10-10 14:59:09 -04003164 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07003165
3166err:
Adam Langley33ad2b52015-07-20 17:43:53 -07003167 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04003168 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003169}
Adam Langley95c29f32014-06-20 12:00:00 -07003170
David Benjamin861abcc2018-07-14 17:40:26 -04003171static bool ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
3172 const SSL_CLIENT_HELLO *client_hello,
3173 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04003174 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003175 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05003176 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07003177 }
3178 }
3179
David Benjamin8c880a22016-12-03 02:20:34 -05003180 hs->extensions.received = 0;
David Benjamine14ff062016-08-09 16:21:24 -04003181 CBS extensions;
3182 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
3183 while (CBS_len(&extensions) != 0) {
3184 uint16_t type;
3185 CBS extension;
3186
David Benjaminc11ea9422017-08-29 16:33:21 -04003187 // Decode the next extension.
David Benjamine14ff062016-08-09 16:21:24 -04003188 if (!CBS_get_u16(&extensions, &type) ||
3189 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003190 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin861abcc2018-07-14 17:40:26 -04003191 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003192 }
Adam Langley95c29f32014-06-20 12:00:00 -07003193
David Benjamine14ff062016-08-09 16:21:24 -04003194 unsigned ext_index;
3195 const struct tls_extension *const ext =
3196 tls_extension_find(&ext_index, type);
David Benjamine14ff062016-08-09 16:21:24 -04003197 if (ext == NULL) {
David Benjamine14ff062016-08-09 16:21:24 -04003198 continue;
3199 }
3200
David Benjamin8c880a22016-12-03 02:20:34 -05003201 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04003202 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003203 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04003204 *out_alert = alert;
3205 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003206 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamin861abcc2018-07-14 17:40:26 -04003207 return false;
Adam Langley614c66a2015-06-12 15:26:58 -07003208 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003209 }
Adam Langley75712922014-10-10 16:23:43 -07003210
David Benjamin1deb41b2016-08-09 19:36:38 -04003211 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05003212 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04003213 continue;
3214 }
3215
3216 CBS *contents = NULL, fake_contents;
3217 static const uint8_t kFakeRenegotiateExtension[] = {0};
3218 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
3219 ssl_client_cipher_list_contains_cipher(client_hello,
3220 SSL3_CK_SCSV & 0xffff)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003221 // The renegotiation SCSV was received so pretend that we received a
3222 // renegotiation extension.
David Benjamin1deb41b2016-08-09 19:36:38 -04003223 CBS_init(&fake_contents, kFakeRenegotiateExtension,
3224 sizeof(kFakeRenegotiateExtension));
3225 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05003226 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04003227 }
3228
David Benjaminc11ea9422017-08-29 16:33:21 -04003229 // Extension wasn't observed so call the callback with a NULL
3230 // parameter.
David Benjamin1deb41b2016-08-09 19:36:38 -04003231 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003232 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04003233 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003234 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04003235 *out_alert = alert;
David Benjamin861abcc2018-07-14 17:40:26 -04003236 return false;
Adam Langley614c66a2015-06-12 15:26:58 -07003237 }
3238 }
3239
David Benjamin861abcc2018-07-14 17:40:26 -04003240 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08003241}
Adam Langley95c29f32014-06-20 12:00:00 -07003242
David Benjamin861abcc2018-07-14 17:40:26 -04003243bool ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
3244 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05003245 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08003246 int alert = SSL_AD_DECODE_ERROR;
David Benjamin861abcc2018-07-14 17:40:26 -04003247 if (!ssl_scan_clienthello_tlsext(hs, client_hello, &alert)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04003248 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin861abcc2018-07-14 17:40:26 -04003249 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003250 }
Adam Langley95c29f32014-06-20 12:00:00 -07003251
David Benjamin861abcc2018-07-14 17:40:26 -04003252 if (!ssl_check_clienthello_tlsext(hs)) {
David Benjamin3570d732015-06-29 00:28:17 -04003253 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
David Benjamin861abcc2018-07-14 17:40:26 -04003254 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003255 }
Adam Langley95c29f32014-06-20 12:00:00 -07003256
David Benjamin861abcc2018-07-14 17:40:26 -04003257 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08003258}
Adam Langley95c29f32014-06-20 12:00:00 -07003259
David Benjamin861abcc2018-07-14 17:40:26 -04003260static bool ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
3261 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05003262 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003263 // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty.
David Benjamind1e3ce12017-10-06 18:31:15 -04003264 if (CBS_len(cbs) == 0 && ssl_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjamin861abcc2018-07-14 17:40:26 -04003265 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04003266 }
Adam Langley614c66a2015-06-12 15:26:58 -07003267
David Benjaminc11ea9422017-08-29 16:33:21 -04003268 // Decode the extensions block and check it is valid.
Steven Valdez143e8b32016-07-11 13:19:03 -04003269 CBS extensions;
3270 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
3271 !tls1_check_duplicate_extensions(&extensions)) {
3272 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin861abcc2018-07-14 17:40:26 -04003273 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04003274 }
3275
3276 uint32_t received = 0;
3277 while (CBS_len(&extensions) != 0) {
3278 uint16_t type;
3279 CBS extension;
3280
David Benjaminc11ea9422017-08-29 16:33:21 -04003281 // Decode the next extension.
Steven Valdez143e8b32016-07-11 13:19:03 -04003282 if (!CBS_get_u16(&extensions, &type) ||
3283 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003284 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin861abcc2018-07-14 17:40:26 -04003285 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003286 }
Adam Langley95c29f32014-06-20 12:00:00 -07003287
Steven Valdez143e8b32016-07-11 13:19:03 -04003288 unsigned ext_index;
3289 const struct tls_extension *const ext =
3290 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07003291
Steven Valdez143e8b32016-07-11 13:19:03 -04003292 if (ext == NULL) {
David Benjamin0a3e07a2018-07-09 16:28:22 -04003293 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3294 ERR_add_error_dataf("extension %u", (unsigned)type);
3295 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin861abcc2018-07-14 17:40:26 -04003296 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04003297 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003298
David Benjamina3d76d02017-07-14 19:36:07 -04003299 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
3300 "too many bits");
David Benjamin5db7c9b2017-01-24 16:17:03 -05003301
David Benjaminc59b9aa2018-07-16 21:34:03 -04003302 if (!(hs->extensions.sent & (1u << ext_index))) {
3303 // If the extension was never sent then it is illegal.
Steven Valdez143e8b32016-07-11 13:19:03 -04003304 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3305 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04003306 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin861abcc2018-07-14 17:40:26 -04003307 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04003308 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003309
Steven Valdez143e8b32016-07-11 13:19:03 -04003310 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07003311
Steven Valdez143e8b32016-07-11 13:19:03 -04003312 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003313 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04003314 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003315 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04003316 *out_alert = alert;
David Benjamin861abcc2018-07-14 17:40:26 -04003317 return false;
Adam Langley614c66a2015-06-12 15:26:58 -07003318 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003319 }
Adam Langley95c29f32014-06-20 12:00:00 -07003320
David Benjamin54091232016-09-05 12:47:25 -04003321 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003322 if (!(received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003323 // Extension wasn't observed so call the callback with a NULL
3324 // parameter.
Adam Langley614c66a2015-06-12 15:26:58 -07003325 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003326 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003327 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003328 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07003329 *out_alert = alert;
David Benjamin861abcc2018-07-14 17:40:26 -04003330 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003331 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003332 }
3333 }
Adam Langley95c29f32014-06-20 12:00:00 -07003334
David Benjamin861abcc2018-07-14 17:40:26 -04003335 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08003336}
Adam Langley95c29f32014-06-20 12:00:00 -07003337
David Benjamin861abcc2018-07-14 17:40:26 -04003338static bool ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
David Benjamin8c880a22016-12-03 02:20:34 -05003339 SSL *const ssl = hs->ssl;
Nick Harper36fcc4c2017-09-21 15:02:22 -07003340
David Benjamin9f0e7cb2018-04-12 15:36:30 -04003341 if (ssl->s3->token_binding_negotiated &&
Nick Harper36fcc4c2017-09-21 15:02:22 -07003342 !(SSL_get_secure_renegotiation_support(ssl) &&
3343 SSL_get_extms_support(ssl))) {
3344 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_TB_WITHOUT_EMS_OR_RI);
3345 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
David Benjamin861abcc2018-07-14 17:40:26 -04003346 return false;
Nick Harper36fcc4c2017-09-21 15:02:22 -07003347 }
3348
Adam Langleyfcf25832014-12-18 17:42:32 -08003349 int ret = SSL_TLSEXT_ERR_NOACK;
3350 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003351
David Benjamin46853762018-07-03 14:01:26 -04003352 if (ssl->ctx->servername_callback != 0) {
3353 ret = ssl->ctx->servername_callback(ssl, &al, ssl->ctx->servername_arg);
3354 } else if (ssl->session_ctx->servername_callback != 0) {
3355 ret = ssl->session_ctx->servername_callback(
3356 ssl, &al, ssl->session_ctx->servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003357 }
Adam Langley95c29f32014-06-20 12:00:00 -07003358
Adam Langleyfcf25832014-12-18 17:42:32 -08003359 switch (ret) {
3360 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamind1e3ce12017-10-06 18:31:15 -04003361 ssl_send_alert(ssl, SSL3_AL_FATAL, al);
David Benjamin861abcc2018-07-14 17:40:26 -04003362 return false;
Adam Langley95c29f32014-06-20 12:00:00 -07003363
Adam Langleyfcf25832014-12-18 17:42:32 -08003364 case SSL_TLSEXT_ERR_NOACK:
David Benjaminfd45ee72017-08-31 14:49:09 -04003365 hs->should_ack_sni = false;
David Benjamin861abcc2018-07-14 17:40:26 -04003366 return true;
Adam Langley95c29f32014-06-20 12:00:00 -07003367
Adam Langleyfcf25832014-12-18 17:42:32 -08003368 default:
David Benjamin861abcc2018-07-14 17:40:26 -04003369 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08003370 }
3371}
Adam Langleyed8270a2014-09-02 13:52:56 -07003372
David Benjamin861abcc2018-07-14 17:40:26 -04003373bool ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
David Benjamin8c880a22016-12-03 02:20:34 -05003374 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08003375 int alert = SSL_AD_DECODE_ERROR;
David Benjamin861abcc2018-07-14 17:40:26 -04003376 if (!ssl_scan_serverhello_tlsext(hs, cbs, &alert)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04003377 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin861abcc2018-07-14 17:40:26 -04003378 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003379 }
3380
David Benjamin861abcc2018-07-14 17:40:26 -04003381 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08003382}
Adam Langley95c29f32014-06-20 12:00:00 -07003383
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003384static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
David Benjamin28655672018-07-18 23:23:25 -04003385 Array<uint8_t> *out, EVP_CIPHER_CTX *cipher_ctx, HMAC_CTX *hmac_ctx,
3386 Span<const uint8_t> ticket) {
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003387 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003388
David Benjaminc11ea9422017-08-29 16:33:21 -04003389 // Check the MAC at the end of the ticket.
David Benjamine3aa1d92015-06-16 15:34:50 -04003390 uint8_t mac[EVP_MAX_MD_SIZE];
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003391 size_t mac_len = HMAC_size(hmac_ctx);
David Benjamin28655672018-07-18 23:23:25 -04003392 if (ticket.size() < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003393 // The ticket must be large enough for key name, IV, data, and MAC.
David Benjamin81678aa2017-07-12 22:43:42 -04003394 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003395 }
David Benjamin28655672018-07-18 23:23:25 -04003396 // Split the ticket into the ticket and the MAC.
3397 auto ticket_mac = ticket.subspan(ticket.size() - mac_len);
3398 ticket = ticket.subspan(0, ticket.size() - mac_len);
3399 HMAC_Update(hmac_ctx, ticket.data(), ticket.size());
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003400 HMAC_Final(hmac_ctx, mac, NULL);
David Benjamin28655672018-07-18 23:23:25 -04003401 assert(mac_len == ticket_mac.size());
3402 bool mac_ok = CRYPTO_memcmp(mac, ticket_mac.data(), mac_len) == 0;
David Benjaminfbc45d72016-09-22 01:21:24 -04003403#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin861abcc2018-07-14 17:40:26 -04003404 mac_ok = true;
David Benjaminfbc45d72016-09-22 01:21:24 -04003405#endif
3406 if (!mac_ok) {
David Benjamin81678aa2017-07-12 22:43:42 -04003407 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003408 }
3409
David Benjaminc11ea9422017-08-29 16:33:21 -04003410 // Decrypt the session data.
David Benjamin28655672018-07-18 23:23:25 -04003411 auto ciphertext = ticket.subspan(SSL_TICKET_KEY_NAME_LEN + iv_len);
3412 Array<uint8_t> plaintext;
3413#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3414 if (!plaintext.CopyFrom(ciphertext)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003415 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08003416 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003417#else
David Benjamin28655672018-07-18 23:23:25 -04003418 if (ciphertext.size() >= INT_MAX) {
David Benjamin81678aa2017-07-12 22:43:42 -04003419 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003420 }
David Benjamin28655672018-07-18 23:23:25 -04003421 if (!plaintext.Init(ciphertext.size())) {
3422 return ssl_ticket_aead_error;
3423 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003424 int len1, len2;
David Benjamin28655672018-07-18 23:23:25 -04003425 if (!EVP_DecryptUpdate(cipher_ctx, plaintext.data(), &len1, ciphertext.data(),
3426 (int)ciphertext.size()) ||
3427 !EVP_DecryptFinal_ex(cipher_ctx, plaintext.data() + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003428 ERR_clear_error();
David Benjamin81678aa2017-07-12 22:43:42 -04003429 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003430 }
David Benjamin28655672018-07-18 23:23:25 -04003431 plaintext.Shrink(static_cast<size_t>(len1) + len2);
David Benjaminfbc45d72016-09-22 01:21:24 -04003432#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003433
David Benjamin28655672018-07-18 23:23:25 -04003434 *out = std::move(plaintext);
David Benjamin81678aa2017-07-12 22:43:42 -04003435 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08003436}
3437
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003438static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
David Benjamin28655672018-07-18 23:23:25 -04003439 SSL_HANDSHAKE *hs, Array<uint8_t> *out, bool *out_renew_ticket,
3440 Span<const uint8_t> ticket) {
3441 assert(ticket.size() >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003442 ScopedEVP_CIPHER_CTX cipher_ctx;
3443 ScopedHMAC_CTX hmac_ctx;
David Benjamin28655672018-07-18 23:23:25 -04003444 auto name = ticket.subspan(0, SSL_TICKET_KEY_NAME_LEN);
3445 // The actual IV is shorter, but the length is determined by the callback's
3446 // chosen cipher. Instead we pass in |EVP_MAX_IV_LENGTH| worth of IV to ensure
3447 // the callback has enough.
3448 auto iv = ticket.subspan(SSL_TICKET_KEY_NAME_LEN, EVP_MAX_IV_LENGTH);
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003449 int cb_ret = hs->ssl->session_ctx->ticket_key_cb(
David Benjamin28655672018-07-18 23:23:25 -04003450 hs->ssl, const_cast<uint8_t *>(name.data()),
3451 const_cast<uint8_t *>(iv.data()), cipher_ctx.get(), hmac_ctx.get(),
3452 0 /* decrypt */);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003453 if (cb_ret < 0) {
3454 return ssl_ticket_aead_error;
3455 } else if (cb_ret == 0) {
3456 return ssl_ticket_aead_ignore_ticket;
3457 } else if (cb_ret == 2) {
David Benjaminfd45ee72017-08-31 14:49:09 -04003458 *out_renew_ticket = true;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003459 } else {
3460 assert(cb_ret == 1);
3461 }
David Benjamin28655672018-07-18 23:23:25 -04003462 return decrypt_ticket_with_cipher_ctx(out, cipher_ctx.get(), hmac_ctx.get(),
3463 ticket);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003464}
3465
3466static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
David Benjamin28655672018-07-18 23:23:25 -04003467 SSL_HANDSHAKE *hs, Array<uint8_t> *out, Span<const uint8_t> ticket) {
3468 assert(ticket.size() >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
David Benjamin50596f82018-07-02 19:47:27 -04003469 SSL_CTX *ctx = hs->ssl->session_ctx.get();
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003470
David Benjaminc11ea9422017-08-29 16:33:21 -04003471 // Rotate the ticket key if necessary.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003472 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
3473 return ssl_ticket_aead_error;
3474 }
3475
David Benjamin28655672018-07-18 23:23:25 -04003476 const EVP_CIPHER *cipher = EVP_aes_128_cbc();
3477 auto name = ticket.subspan(0, SSL_TICKET_KEY_NAME_LEN);
3478 auto iv =
3479 ticket.subspan(SSL_TICKET_KEY_NAME_LEN, EVP_CIPHER_iv_length(cipher));
3480
David Benjaminc11ea9422017-08-29 16:33:21 -04003481 // Pick the matching ticket key and decrypt.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003482 ScopedEVP_CIPHER_CTX cipher_ctx;
3483 ScopedHMAC_CTX hmac_ctx;
3484 {
3485 MutexReadLock lock(&ctx->lock);
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003486 const TicketKey *key;
David Benjamin28655672018-07-18 23:23:25 -04003487 if (ctx->ticket_key_current && name == ctx->ticket_key_current->name) {
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003488 key = ctx->ticket_key_current.get();
David Benjamin28655672018-07-18 23:23:25 -04003489 } else if (ctx->ticket_key_prev && name == ctx->ticket_key_prev->name) {
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003490 key = ctx->ticket_key_prev.get();
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003491 } else {
3492 return ssl_ticket_aead_ignore_ticket;
3493 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003494 if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
3495 tlsext_tick_md(), NULL) ||
David Benjamin28655672018-07-18 23:23:25 -04003496 !EVP_DecryptInit_ex(cipher_ctx.get(), cipher, NULL,
3497 key->aes_key, iv.data())) {
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003498 return ssl_ticket_aead_error;
3499 }
3500 }
David Benjamin28655672018-07-18 23:23:25 -04003501 return decrypt_ticket_with_cipher_ctx(out, cipher_ctx.get(), hmac_ctx.get(),
3502 ticket);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003503}
3504
Adam Langley4c341d02017-03-08 19:33:21 -08003505static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
David Benjamin28655672018-07-18 23:23:25 -04003506 SSL_HANDSHAKE *hs, Array<uint8_t> *out, bool *out_renew_ticket,
3507 Span<const uint8_t> ticket) {
3508 Array<uint8_t> plaintext;
3509 if (!plaintext.Init(ticket.size())) {
Adam Langley4c341d02017-03-08 19:33:21 -08003510 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3511 return ssl_ticket_aead_error;
3512 }
3513
3514 size_t plaintext_len;
3515 const enum ssl_ticket_aead_result_t result =
David Benjamin98472cb2018-05-02 16:05:36 -04003516 hs->ssl->session_ctx->ticket_aead_method->open(
David Benjamin28655672018-07-18 23:23:25 -04003517 hs->ssl, plaintext.data(), &plaintext_len, ticket.size(),
3518 ticket.data(), ticket.size());
3519 if (result != ssl_ticket_aead_success) {
3520 return result;
Adam Langley4c341d02017-03-08 19:33:21 -08003521 }
3522
David Benjamin28655672018-07-18 23:23:25 -04003523 plaintext.Shrink(plaintext_len);
3524 *out = std::move(plaintext);
3525 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08003526}
3527
3528enum ssl_ticket_aead_result_t ssl_process_ticket(
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003529 SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session,
David Benjamin28655672018-07-18 23:23:25 -04003530 bool *out_renew_ticket, Span<const uint8_t> ticket,
3531 Span<const uint8_t> session_id) {
David Benjaminfd45ee72017-08-31 14:49:09 -04003532 *out_renew_ticket = false;
David Benjamin37af90f2017-07-29 01:42:16 -04003533 out_session->reset();
Adam Langley4c341d02017-03-08 19:33:21 -08003534
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003535 if ((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) ||
David Benjamin28655672018-07-18 23:23:25 -04003536 session_id.size() > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langley4c341d02017-03-08 19:33:21 -08003537 return ssl_ticket_aead_ignore_ticket;
3538 }
3539
David Benjamin28655672018-07-18 23:23:25 -04003540 Array<uint8_t> plaintext;
Adam Langley4c341d02017-03-08 19:33:21 -08003541 enum ssl_ticket_aead_result_t result;
David Benjamin98472cb2018-05-02 16:05:36 -04003542 if (hs->ssl->session_ctx->ticket_aead_method != NULL) {
David Benjamin28655672018-07-18 23:23:25 -04003543 result = ssl_decrypt_ticket_with_method(hs, &plaintext, out_renew_ticket,
3544 ticket);
Adam Langley4c341d02017-03-08 19:33:21 -08003545 } else {
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003546 // Ensure there is room for the key name and the largest IV |ticket_key_cb|
3547 // may try to consume. The real limit may be lower, but the maximum IV
3548 // length should be well under the minimum size for the session material and
3549 // HMAC.
David Benjamin28655672018-07-18 23:23:25 -04003550 if (ticket.size() < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003551 return ssl_ticket_aead_ignore_ticket;
3552 }
David Benjamin7bb0fbf2018-07-03 13:55:42 -04003553 if (hs->ssl->session_ctx->ticket_key_cb != NULL) {
David Benjamin28655672018-07-18 23:23:25 -04003554 result =
3555 ssl_decrypt_ticket_with_cb(hs, &plaintext, out_renew_ticket, ticket);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003556 } else {
David Benjamin28655672018-07-18 23:23:25 -04003557 result = ssl_decrypt_ticket_with_ticket_keys(hs, &plaintext, ticket);
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003558 }
Adam Langley4c341d02017-03-08 19:33:21 -08003559 }
3560
3561 if (result != ssl_ticket_aead_success) {
3562 return result;
3563 }
3564
David Benjaminc11ea9422017-08-29 16:33:21 -04003565 // Decode the session.
David Benjamin28655672018-07-18 23:23:25 -04003566 UniquePtr<SSL_SESSION> session(SSL_SESSION_from_bytes(
3567 plaintext.data(), plaintext.size(), hs->ssl->ctx.get()));
David Benjamin37af90f2017-07-29 01:42:16 -04003568 if (!session) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003569 ERR_clear_error(); // Don't leave an error on the queue.
Adam Langley4c341d02017-03-08 19:33:21 -08003570 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003571 }
3572
David Benjaminc11ea9422017-08-29 16:33:21 -04003573 // Copy the client's session ID into the new session, to denote the ticket has
3574 // been accepted.
David Benjamin28655672018-07-18 23:23:25 -04003575 OPENSSL_memcpy(session->session_id, session_id.data(), session_id.size());
3576 session->session_id_length = session_id.size();
David Benjamine3aa1d92015-06-16 15:34:50 -04003577
David Benjamin37af90f2017-07-29 01:42:16 -04003578 *out_session = std::move(session);
Adam Langley4c341d02017-03-08 19:33:21 -08003579 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003580}
Adam Langley95c29f32014-06-20 12:00:00 -07003581
David Benjamin610cdbb2018-01-22 19:08:38 -05003582bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003583 // Extension ignored for inappropriate versions
David Benjamind1e3ce12017-10-06 18:31:15 -04003584 if (ssl_protocol_version(hs->ssl) < TLS1_2_VERSION) {
David Benjamin610cdbb2018-01-22 19:08:38 -05003585 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08003586 }
David Benjamincd996942014-07-20 16:23:51 -04003587
David Benjamin602f4662018-12-07 12:06:22 -06003588 // In all contexts, the signature algorithms list may not be empty. (It may be
3589 // omitted by clients in TLS 1.2, but then the entire extension is omitted.)
3590 return CBS_len(in_sigalgs) != 0 &&
3591 parse_u16_array(in_sigalgs, &hs->peer_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08003592}
David Benjaminec2f27d2014-11-13 19:17:25 -05003593
David Benjamin610cdbb2018-01-22 19:08:38 -05003594bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
David Benjamina3651382017-04-20 17:49:36 -04003595 switch (EVP_PKEY_id(pkey)) {
3596 case EVP_PKEY_RSA:
3597 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin610cdbb2018-01-22 19:08:38 -05003598 return true;
David Benjamina3651382017-04-20 17:49:36 -04003599 case EVP_PKEY_EC:
3600 *out = SSL_SIGN_ECDSA_SHA1;
David Benjamin610cdbb2018-01-22 19:08:38 -05003601 return true;
David Benjamina3651382017-04-20 17:49:36 -04003602 default:
David Benjamin610cdbb2018-01-22 19:08:38 -05003603 return false;
David Benjamina3651382017-04-20 17:49:36 -04003604 }
3605}
3606
David Benjamin610cdbb2018-01-22 19:08:38 -05003607bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003608 SSL *const ssl = hs->ssl;
David Benjamin0ce090a2018-07-02 20:24:40 -04003609 CERT *cert = hs->config->cert.get();
Christopher Patton6c1b3762018-07-17 12:49:41 -07003610 DC *dc = cert->dc.get();
David Benjaminec2f27d2014-11-13 19:17:25 -05003611
David Benjaminc11ea9422017-08-29 16:33:21 -04003612 // Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3613 // handshake.
David Benjamind1e3ce12017-10-06 18:31:15 -04003614 if (ssl_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin31b0c9b2017-07-20 14:49:15 -04003615 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
David Benjamina3651382017-04-20 17:49:36 -04003616 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
David Benjamin610cdbb2018-01-22 19:08:38 -05003617 return false;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003618 }
David Benjamin610cdbb2018-01-22 19:08:38 -05003619 return true;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003620 }
3621
David Benjaminb1cf48e2017-09-21 11:37:46 -04003622 Span<const uint16_t> sigalgs = kSignSignatureAlgorithms;
Christopher Patton6c1b3762018-07-17 12:49:41 -07003623 if (ssl_signing_with_dc(hs)) {
3624 sigalgs = MakeConstSpan(&dc->expected_cert_verify_algorithm, 1);
3625 } else if (!cert->sigalgs.empty()) {
David Benjamine325c3f2018-04-12 16:11:15 -04003626 sigalgs = cert->sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003627 }
3628
Christopher Patton6c1b3762018-07-17 12:49:41 -07003629 Span<const uint16_t> peer_sigalgs = tls1_get_peer_verify_algorithms(hs);
David Benjaminea9a0d52016-07-08 15:52:59 -07003630
David Benjaminb1cf48e2017-09-21 11:37:46 -04003631 for (uint16_t sigalg : sigalgs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003632 // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3633 // negotiated.
David Benjamin1fb125c2016-07-08 18:52:12 -07003634 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjaminb1cf48e2017-09-21 11:37:46 -04003635 !ssl_private_key_supports_signature_algorithm(hs, sigalg)) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003636 continue;
3637 }
3638
David Benjaminb1cf48e2017-09-21 11:37:46 -04003639 for (uint16_t peer_sigalg : peer_sigalgs) {
3640 if (sigalg == peer_sigalg) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003641 *out = sigalg;
David Benjamin610cdbb2018-01-22 19:08:38 -05003642 return true;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003643 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003644 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003645 }
Adam Langley95c29f32014-06-20 12:00:00 -07003646
David Benjaminea9a0d52016-07-08 15:52:59 -07003647 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
David Benjamin610cdbb2018-01-22 19:08:38 -05003648 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003649}
Adam Langley95c29f32014-06-20 12:00:00 -07003650
Christopher Patton6c1b3762018-07-17 12:49:41 -07003651Span<const uint16_t> tls1_get_peer_verify_algorithms(const SSL_HANDSHAKE *hs) {
3652 Span<const uint16_t> peer_sigalgs = hs->peer_sigalgs;
3653 if (peer_sigalgs.empty() && ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) {
3654 // If the client didn't specify any signature_algorithms extension then
3655 // we can assume that it supports SHA1. See
3656 // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
3657 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3658 SSL_SIGN_ECDSA_SHA1};
3659 peer_sigalgs = kDefaultPeerAlgorithms;
3660 }
3661 return peer_sigalgs;
3662}
3663
David Benjamin861abcc2018-07-14 17:40:26 -04003664bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
Steven Valdez908ac192017-01-12 13:17:07 -05003665 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003666 // A Channel ID handshake message is structured to contain multiple
3667 // extensions, but the only one that can be present is Channel ID.
David Benjamin7934f082017-08-01 16:32:25 -04003668 uint16_t extension_type;
3669 CBS channel_id = msg.body, extension;
Nick Harper60a85cb2016-09-23 16:25:11 -07003670 if (!CBS_get_u16(&channel_id, &extension_type) ||
3671 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3672 CBS_len(&channel_id) != 0 ||
3673 extension_type != TLSEXT_TYPE_channel_id ||
3674 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3675 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04003676 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04003677 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003678 }
3679
David Benjamin86e95b82017-07-18 16:34:25 -04003680 UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Nick Harper60a85cb2016-09-23 16:25:11 -07003681 if (!p256) {
3682 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
David Benjamin861abcc2018-07-14 17:40:26 -04003683 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003684 }
3685
David Benjamin86e95b82017-07-18 16:34:25 -04003686 UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3687 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
David Benjamin81678aa2017-07-12 22:43:42 -04003688 if (!sig || !x || !y) {
David Benjamin861abcc2018-07-14 17:40:26 -04003689 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003690 }
3691
3692 const uint8_t *p = CBS_data(&extension);
David Benjamin81678aa2017-07-12 22:43:42 -04003693 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3694 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3695 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3696 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
David Benjamin861abcc2018-07-14 17:40:26 -04003697 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003698 }
3699
David Benjamin86e95b82017-07-18 16:34:25 -04003700 UniquePtr<EC_KEY> key(EC_KEY_new());
3701 UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
David Benjamin81678aa2017-07-12 22:43:42 -04003702 if (!key || !point ||
3703 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3704 y.get(), nullptr) ||
3705 !EC_KEY_set_group(key.get(), p256.get()) ||
3706 !EC_KEY_set_public_key(key.get(), point.get())) {
David Benjamin861abcc2018-07-14 17:40:26 -04003707 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003708 }
3709
3710 uint8_t digest[EVP_MAX_MD_SIZE];
3711 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003712 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjamin861abcc2018-07-14 17:40:26 -04003713 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003714 }
3715
David Benjamin861abcc2018-07-14 17:40:26 -04003716 bool sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
Nick Harper60a85cb2016-09-23 16:25:11 -07003717#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin861abcc2018-07-14 17:40:26 -04003718 sig_ok = true;
David Benjamind90b8032017-12-18 16:47:51 -05003719 ERR_clear_error();
Nick Harper60a85cb2016-09-23 16:25:11 -07003720#endif
3721 if (!sig_ok) {
3722 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
David Benjamind1e3ce12017-10-06 18:31:15 -04003723 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
David Benjamin46853762018-07-03 14:01:26 -04003724 ssl->s3->channel_id_valid = false;
David Benjamin861abcc2018-07-14 17:40:26 -04003725 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003726 }
3727
David Benjamin46853762018-07-03 14:01:26 -04003728 OPENSSL_memcpy(ssl->s3->channel_id, p, 64);
David Benjamin861abcc2018-07-14 17:40:26 -04003729 return true;
Nick Harper60a85cb2016-09-23 16:25:11 -07003730}
3731
David Benjaminf1db1a32017-10-27 01:12:34 -04003732bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003733 uint8_t digest[EVP_MAX_MD_SIZE];
3734 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003735 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjaminf1db1a32017-10-27 01:12:34 -04003736 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003737 }
3738
David Benjamin46853762018-07-03 14:01:26 -04003739 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(hs->config->channel_id_private.get());
David Benjaminf1db1a32017-10-27 01:12:34 -04003740 if (ec_key == nullptr) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003741 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminf1db1a32017-10-27 01:12:34 -04003742 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003743 }
3744
David Benjaminf1db1a32017-10-27 01:12:34 -04003745 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
3746 if (!x || !y ||
Nick Harper60a85cb2016-09-23 16:25:11 -07003747 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3748 EC_KEY_get0_public_key(ec_key),
David Benjaminf1db1a32017-10-27 01:12:34 -04003749 x.get(), y.get(), nullptr)) {
3750 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003751 }
3752
David Benjaminf1db1a32017-10-27 01:12:34 -04003753 UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign(digest, digest_len, ec_key));
3754 if (!sig) {
3755 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003756 }
3757
3758 CBB child;
3759 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3760 !CBB_add_u16_length_prefixed(cbb, &child) ||
David Benjaminf1db1a32017-10-27 01:12:34 -04003761 !BN_bn2cbb_padded(&child, 32, x.get()) ||
3762 !BN_bn2cbb_padded(&child, 32, y.get()) ||
Nick Harper60a85cb2016-09-23 16:25:11 -07003763 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3764 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3765 !CBB_flush(cbb)) {
David Benjaminf1db1a32017-10-27 01:12:34 -04003766 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003767 }
3768
David Benjaminf1db1a32017-10-27 01:12:34 -04003769 return true;
Nick Harper60a85cb2016-09-23 16:25:11 -07003770}
3771
David Benjamin861abcc2018-07-14 17:40:26 -04003772bool tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
Steven Valdez908ac192017-01-12 13:17:07 -05003773 SSL *const ssl = hs->ssl;
David Benjamind1e3ce12017-10-06 18:31:15 -04003774 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin75a1f232017-10-11 17:19:19 -04003775 Array<uint8_t> msg;
3776 if (!tls13_get_cert_verify_signature_input(hs, &msg,
Nick Harper60a85cb2016-09-23 16:25:11 -07003777 ssl_cert_verify_channel_id)) {
David Benjamin861abcc2018-07-14 17:40:26 -04003778 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003779 }
David Benjamin75a1f232017-10-11 17:19:19 -04003780 SHA256(msg.data(), msg.size(), out);
Nick Harper60a85cb2016-09-23 16:25:11 -07003781 *out_len = SHA256_DIGEST_LENGTH;
David Benjamin861abcc2018-07-14 17:40:26 -04003782 return true;
Nick Harper60a85cb2016-09-23 16:25:11 -07003783 }
3784
Nick Harper95594012016-10-20 14:07:13 -07003785 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003786
Nick Harper95594012016-10-20 14:07:13 -07003787 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003788 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003789 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003790
Steven Valdez87eab492016-06-27 16:34:59 -04003791 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003792 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003793 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003794 if (ssl->session->original_handshake_hash_len == 0) {
3795 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin861abcc2018-07-14 17:40:26 -04003796 return false;
David Benjamind6a4ae92015-08-06 11:10:51 -04003797 }
Nick Harper95594012016-10-20 14:07:13 -07003798 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3799 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003800 }
3801
Steven Valdez908ac192017-01-12 13:17:07 -05003802 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3803 size_t hs_hash_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003804 if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
David Benjamin861abcc2018-07-14 17:40:26 -04003805 return false;
David Benjamind6a4ae92015-08-06 11:10:51 -04003806 }
Steven Valdez908ac192017-01-12 13:17:07 -05003807 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003808 SHA256_Final(out, &ctx);
3809 *out_len = SHA256_DIGEST_LENGTH;
David Benjamin861abcc2018-07-14 17:40:26 -04003810 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08003811}
Adam Langley1258b6a2014-06-20 12:00:00 -07003812
David Benjamin861abcc2018-07-14 17:40:26 -04003813bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
Steven Valdez908ac192017-01-12 13:17:07 -05003814 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003815 // This function should never be called for a resumed session because the
3816 // handshake hashes that we wish to record are for the original, full
3817 // handshake.
Steven Valdez87eab492016-06-27 16:34:59 -04003818 if (ssl->session != NULL) {
David Benjamin861abcc2018-07-14 17:40:26 -04003819 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003820 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003821
David Benjamina3d76d02017-07-14 19:36:07 -04003822 static_assert(
David Benjamin45738dd2017-02-09 20:01:26 -05003823 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
David Benjamina3d76d02017-07-14 19:36:07 -04003824 "original_handshake_hash is too small");
Steven Valdez908ac192017-01-12 13:17:07 -05003825
3826 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003827 if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
3828 &digest_len)) {
David Benjamin861abcc2018-07-14 17:40:26 -04003829 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -08003830 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003831
David Benjamina3d76d02017-07-14 19:36:07 -04003832 static_assert(EVP_MAX_MD_SIZE <= 0xff,
3833 "EVP_MAX_MD_SIZE does not fit in uint8_t");
David Benjamin45738dd2017-02-09 20:01:26 -05003834 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003835
David Benjamin861abcc2018-07-14 17:40:26 -04003836 return true;
Adam Langleyfcf25832014-12-18 17:42:32 -08003837}
Nick Harper60a85cb2016-09-23 16:25:11 -07003838
David Benjamin861abcc2018-07-14 17:40:26 -04003839bool ssl_do_channel_id_callback(SSL_HANDSHAKE *hs) {
David Benjamin46853762018-07-03 14:01:26 -04003840 if (hs->config->channel_id_private != NULL ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003841 hs->ssl->ctx->channel_id_cb == NULL) {
David Benjamin861abcc2018-07-14 17:40:26 -04003842 return true;
Nick Harper60a85cb2016-09-23 16:25:11 -07003843 }
3844
3845 EVP_PKEY *key = NULL;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07003846 hs->ssl->ctx->channel_id_cb(hs->ssl, &key);
Nick Harper60a85cb2016-09-23 16:25:11 -07003847 if (key == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003848 // The caller should try again later.
David Benjamin861abcc2018-07-14 17:40:26 -04003849 return true;
Nick Harper60a85cb2016-09-23 16:25:11 -07003850 }
3851
David Benjamin861abcc2018-07-14 17:40:26 -04003852 UniquePtr<EVP_PKEY> free_key(key);
3853 return SSL_set1_tls_channel_id(hs->ssl, key);
Nick Harper60a85cb2016-09-23 16:25:11 -07003854}
Adam Langleycfa08c32016-11-17 13:21:27 -08003855
David Benjamin861abcc2018-07-14 17:40:26 -04003856bool ssl_is_sct_list_valid(const CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003857 // Shallow parse the SCT list for sanity. By the RFC
3858 // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3859 // of the SCTs may be empty.
Adam Langleycfa08c32016-11-17 13:21:27 -08003860 CBS copy = *contents;
3861 CBS sct_list;
3862 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3863 CBS_len(&copy) != 0 ||
3864 CBS_len(&sct_list) == 0) {
David Benjamin861abcc2018-07-14 17:40:26 -04003865 return false;
Adam Langleycfa08c32016-11-17 13:21:27 -08003866 }
3867
3868 while (CBS_len(&sct_list) > 0) {
3869 CBS sct;
3870 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3871 CBS_len(&sct) == 0) {
David Benjamin861abcc2018-07-14 17:40:26 -04003872 return false;
Adam Langleycfa08c32016-11-17 13:21:27 -08003873 }
3874 }
3875
David Benjamin861abcc2018-07-14 17:40:26 -04003876 return true;
Adam Langleycfa08c32016-11-17 13:21:27 -08003877}
David Benjamin86e95b82017-07-18 16:34:25 -04003878
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07003879BSSL_NAMESPACE_END
David Benjamin86e95b82017-07-18 16:34:25 -04003880
3881using namespace bssl;
3882
3883int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
3884 uint16_t extension_type,
3885 const uint8_t **out_data,
3886 size_t *out_len) {
3887 CBS cbs;
3888 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
3889 return 0;
3890 }
3891
3892 *out_data = CBS_data(&cbs);
3893 *out_len = CBS_len(&cbs);
3894 return 1;
3895}