blob: 87f188893323b0c8e729e30ad4d15332fd8b7826 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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.
Robert Sloanab8b8882018-03-26 11:39:51 -07007 *
Adam Langleyd9e397b2015-01-22 14:27:53 -08008 * 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).
Robert Sloanab8b8882018-03-26 11:39:51 -070014 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080015 * 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.
Robert Sloanab8b8882018-03-26 11:39:51 -070021 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080022 * 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 :-).
Robert Sloanab8b8882018-03-26 11:39:51 -070036 * 4. If you include any Windows specific code (or a derivative thereof) from
Adam Langleyd9e397b2015-01-22 14:27:53 -080037 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Robert Sloanab8b8882018-03-26 11:39:51 -070039 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080040 * 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.
Robert Sloanab8b8882018-03-26 11:39:51 -070051 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080052 * 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
Robert Sloanab8b8882018-03-26 11:39:51 -070065 * notice, this list of conditions and the following disclaimer.
Adam Langleyd9e397b2015-01-22 14:27:53 -080066 *
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
Kenny Rootb8494592015-09-25 02:29:14 +0000109#include <openssl/ssl.h>
110
Adam Langleye9ada862015-05-11 17:20:37 -0700111#include <assert.h>
Kenny Rootb8494592015-09-25 02:29:14 +0000112#include <limits.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800113#include <stdlib.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700114#include <string.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800115
Robert Sloan4562e9d2017-10-02 10:26:51 -0700116#include <utility>
117
Adam Langleyd9e397b2015-01-22 14:27:53 -0800118#include <openssl/bytestring.h>
Robert Sloan0db7f542018-01-16 15:48:33 -0800119#include <openssl/chacha.h>
Kenny Rootb8494592015-09-25 02:29:14 +0000120#include <openssl/digest.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700121#include <openssl/err.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800122#include <openssl/evp.h>
123#include <openssl/hmac.h>
124#include <openssl/mem.h>
David Benjamin4969cc92016-04-22 15:02:23 -0400125#include <openssl/nid.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800126#include <openssl/rand.h>
127
Adam Langleye9ada862015-05-11 17:20:37 -0700128#include "internal.h"
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400129#include "../crypto/internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800130
131
Robert Sloan726e9d12018-09-11 11:45:04 -0700132BSSL_NAMESPACE_BEGIN
Robert Sloanb6d070c2017-07-24 08:40:01 -0700133
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100134static bool ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800135
Adam Langleyd9e397b2015-01-22 14:27:53 -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}
147
Robert Sloana27a6a42017-09-05 08:39:28 -0700148// 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.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100152static bool tls1_check_duplicate_extensions(const CBS *cbs) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700153 // First pass: count the extensions.
Robert Sloan4562e9d2017-10-02 10:26:51 -0700154 size_t num_extensions = 0;
155 CBS extensions = *cbs;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800156 while (CBS_len(&extensions) > 0) {
157 uint16_t type;
158 CBS extension;
159
160 if (!CBS_get_u16(&extensions, &type) ||
161 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100162 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800163 }
164
165 num_extensions++;
166 }
167
168 if (num_extensions == 0) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100169 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800170 }
171
Robert Sloan4562e9d2017-10-02 10:26:51 -0700172 Array<uint16_t> extension_types;
173 if (!extension_types.Init(num_extensions)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100174 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800175 }
176
Robert Sloana27a6a42017-09-05 08:39:28 -0700177 // Second pass: gather the extension types.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800178 extensions = *cbs;
Robert Sloan4562e9d2017-10-02 10:26:51 -0700179 for (size_t i = 0; i < extension_types.size(); i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800180 CBS extension;
181
182 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
183 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700184 // This should not happen.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100185 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800186 }
187 }
188 assert(CBS_len(&extensions) == 0);
189
Robert Sloana27a6a42017-09-05 08:39:28 -0700190 // Sort the extensions and make sure there are no duplicates.
Robert Sloan4562e9d2017-10-02 10:26:51 -0700191 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 Langleyd9e397b2015-01-22 14:27:53 -0800194 if (extension_types[i - 1] == extension_types[i]) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100195 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800196 }
197 }
198
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100199 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800200}
201
Robert Sloanf63bd1f2019-04-16 09:26:20 -0700202bool ssl_client_hello_init(const SSL *ssl, SSL_CLIENT_HELLO *out,
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100203 const SSLMessage &msg) {
Robert Sloan69939df2017-01-09 10:53:07 -0800204 OPENSSL_memset(out, 0, sizeof(*out));
Robert Sloanf63bd1f2019-04-16 09:26:20 -0700205 out->ssl = const_cast<SSL *>(ssl);
Robert Sloan84377092017-08-14 09:33:19 -0700206 out->client_hello = CBS_data(&msg.body);
207 out->client_hello_len = CBS_len(&msg.body);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800208
David Benjaminc895d6b2016-08-11 13:26:41 -0400209 CBS client_hello, random, session_id;
David Benjamin1b249672016-12-06 18:25:50 -0500210 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
211 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400212 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
213 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
214 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100215 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800216 }
217
David Benjamin1b249672016-12-06 18:25:50 -0500218 out->random = CBS_data(&random);
219 out->random_len = CBS_len(&random);
220 out->session_id = CBS_data(&session_id);
221 out->session_id_len = CBS_len(&session_id);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800222
Robert Sloana27a6a42017-09-05 08:39:28 -0700223 // Skip past DTLS cookie
David Benjamin1b249672016-12-06 18:25:50 -0500224 if (SSL_is_dtls(out->ssl)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800225 CBS cookie;
David Benjaminc895d6b2016-08-11 13:26:41 -0400226 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
227 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100228 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800229 }
230 }
231
David Benjaminc895d6b2016-08-11 13:26:41 -0400232 CBS cipher_suites, compression_methods;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800233 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400234 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
235 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -0800236 CBS_len(&compression_methods) < 1) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100237 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800238 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400239
David Benjamin1b249672016-12-06 18:25:50 -0500240 out->cipher_suites = CBS_data(&cipher_suites);
241 out->cipher_suites_len = CBS_len(&cipher_suites);
242 out->compression_methods = CBS_data(&compression_methods);
243 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800244
Robert Sloana27a6a42017-09-05 08:39:28 -0700245 // If the ClientHello ends here then it's valid, but doesn't have any
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100246 // extensions.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800247 if (CBS_len(&client_hello) == 0) {
David Benjamin1b249672016-12-06 18:25:50 -0500248 out->extensions = NULL;
249 out->extensions_len = 0;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100250 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800251 }
252
Robert Sloana27a6a42017-09-05 08:39:28 -0700253 // Extract extensions and check it is valid.
David Benjaminc895d6b2016-08-11 13:26:41 -0400254 CBS extensions;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800255 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
256 !tls1_check_duplicate_extensions(&extensions) ||
257 CBS_len(&client_hello) != 0) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100258 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800259 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400260
David Benjamin1b249672016-12-06 18:25:50 -0500261 out->extensions = CBS_data(&extensions);
262 out->extensions_len = CBS_len(&extensions);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800263
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100264 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800265}
266
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100267bool ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
268 CBS *out, uint16_t extension_type) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800269 CBS extensions;
David Benjamin1b249672016-12-06 18:25:50 -0500270 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800271 while (CBS_len(&extensions) != 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700272 // Decode the next extension.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800273 uint16_t type;
274 CBS extension;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800275 if (!CBS_get_u16(&extensions, &type) ||
276 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100277 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800278 }
279
280 if (type == extension_type) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400281 *out = extension;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100282 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800283 }
284 }
285
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100286 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800287}
288
David Benjamind316cba2016-06-02 16:17:39 -0400289static const uint16_t kDefaultGroups[] = {
David Benjaminc895d6b2016-08-11 13:26:41 -0400290 SSL_CURVE_X25519,
291 SSL_CURVE_SECP256R1,
292 SSL_CURVE_SECP384R1,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800293};
294
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100295Span<const uint16_t> tls1_get_grouplist(const SSL_HANDSHAKE *hs) {
296 if (!hs->config->supported_group_list.empty()) {
297 return hs->config->supported_group_list;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800298 }
Robert Sloan4562e9d2017-10-02 10:26:51 -0700299 return Span<const uint16_t>(kDefaultGroups);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800300}
301
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100302bool tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
David Benjamin1b249672016-12-06 18:25:50 -0500303 SSL *const ssl = hs->ssl;
David Benjamin95add822016-10-19 01:09:12 -0400304 assert(ssl->server);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800305
Robert Sloana27a6a42017-09-05 08:39:28 -0700306 // Clients are not required to send a supported_groups extension. In this
307 // case, the server is free to pick any group it likes. See RFC 4492,
308 // section 4, paragraph 3.
309 //
310 // However, in the interests of compatibility, we will skip ECDH if the
311 // client didn't send an extension because we can't be sure that they'll
312 // support our favoured group. Thus we do not special-case an emtpy
313 // |peer_supported_group_list|.
Adam Langleye9ada862015-05-11 17:20:37 -0700314
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100315 Span<const uint16_t> groups = tls1_get_grouplist(hs);
Robert Sloan4562e9d2017-10-02 10:26:51 -0700316 Span<const uint16_t> pref, supp;
Adam Langley4139edb2016-01-13 15:00:54 -0800317 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
David Benjamind316cba2016-06-02 16:17:39 -0400318 pref = groups;
David Benjamin1b249672016-12-06 18:25:50 -0500319 supp = hs->peer_supported_group_list;
Adam Langleye9ada862015-05-11 17:20:37 -0700320 } else {
David Benjamin1b249672016-12-06 18:25:50 -0500321 pref = hs->peer_supported_group_list;
David Benjamind316cba2016-06-02 16:17:39 -0400322 supp = groups;
Adam Langleye9ada862015-05-11 17:20:37 -0700323 }
324
Robert Sloan4562e9d2017-10-02 10:26:51 -0700325 for (uint16_t pref_group : pref) {
326 for (uint16_t supp_group : supp) {
Robert Sloan11c28bd2018-12-17 12:09:20 -0800327 if (pref_group == supp_group &&
Srinivas Paladugudd42a612019-08-09 19:30:39 +0000328 // CECPQ2 doesn't fit in the u8-length-prefixed ECPoint field in TLS
329 // 1.2 and below.
Robert Sloan11c28bd2018-12-17 12:09:20 -0800330 (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
Srinivas Paladugudd42a612019-08-09 19:30:39 +0000331 pref_group != SSL_CURVE_CECPQ2)) {
Robert Sloan4562e9d2017-10-02 10:26:51 -0700332 *out_group_id = pref_group;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100333 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800334 }
335 }
336 }
337
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100338 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800339}
340
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100341bool tls1_set_curves(Array<uint16_t> *out_group_ids, Span<const int> curves) {
342 Array<uint16_t> group_ids;
343 if (!group_ids.Init(curves.size())) {
344 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800345 }
346
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100347 for (size_t i = 0; i < curves.size(); i++) {
David Benjamind316cba2016-06-02 16:17:39 -0400348 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100349 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800350 }
351 }
352
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100353 *out_group_ids = std::move(group_ids);
354 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800355}
356
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100357bool tls1_set_curves_list(Array<uint16_t> *out_group_ids, const char *curves) {
358 // Count the number of curves in the list.
359 size_t count = 0;
360 const char *ptr = curves, *col;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400361 do {
362 col = strchr(ptr, ':');
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100363 count++;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400364 if (col) {
365 ptr = col + 1;
366 }
367 } while (col);
368
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100369 Array<uint16_t> group_ids;
370 if (!group_ids.Init(count)) {
371 return false;
372 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400373
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100374 size_t i = 0;
375 ptr = curves;
376 do {
377 col = strchr(ptr, ':');
378 if (!ssl_name_to_group_id(&group_ids[i++], ptr,
379 col ? (size_t)(col - ptr) : strlen(ptr))) {
380 return false;
381 }
382 if (col) {
383 ptr = col + 1;
384 }
385 } while (col);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400386
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100387 assert(i == count);
388 *out_group_ids = std::move(group_ids);
389 return true;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400390}
391
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100392bool tls1_check_group_id(const SSL_HANDSHAKE *hs, uint16_t group_id) {
Srinivas Paladugudd42a612019-08-09 19:30:39 +0000393 if (group_id == SSL_CURVE_CECPQ2 &&
Robert Sloan11c28bd2018-12-17 12:09:20 -0800394 ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) {
Srinivas Paladugudd42a612019-08-09 19:30:39 +0000395 // CECPQ2 requires TLS 1.3.
Robert Sloan11c28bd2018-12-17 12:09:20 -0800396 return false;
397 }
398
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100399 for (uint16_t supported : tls1_get_grouplist(hs)) {
Robert Sloan4562e9d2017-10-02 10:26:51 -0700400 if (supported == group_id) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100401 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800402 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800403 }
404
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100405 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800406}
407
Robert Sloana27a6a42017-09-05 08:39:28 -0700408// kVerifySignatureAlgorithms is the default list of accepted signature
409// algorithms for verifying.
410//
411// For now, RSA-PSS signature algorithms are not enabled on Android's system
412// BoringSSL. Once the change in Chrome has stuck and the values are finalized,
413// restore them.
David Benjamin95add822016-10-19 01:09:12 -0400414static const uint16_t kVerifySignatureAlgorithms[] = {
Robert Sloana27a6a42017-09-05 08:39:28 -0700415 // List our preferred algorithms first.
Robert Sloan572a4e22017-04-17 10:52:19 -0700416 SSL_SIGN_ED25519,
Steven Valdez909b19f2016-11-21 15:35:44 -0500417 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700418 SSL_SIGN_RSA_PSS_RSAE_SHA256,
Steven Valdez909b19f2016-11-21 15:35:44 -0500419 SSL_SIGN_RSA_PKCS1_SHA256,
420
Robert Sloana27a6a42017-09-05 08:39:28 -0700421 // Larger hashes are acceptable.
Steven Valdez909b19f2016-11-21 15:35:44 -0500422 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700423 SSL_SIGN_RSA_PSS_RSAE_SHA384,
Steven Valdez909b19f2016-11-21 15:35:44 -0500424 SSL_SIGN_RSA_PKCS1_SHA384,
425
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700426 SSL_SIGN_RSA_PSS_RSAE_SHA512,
David Benjaminc895d6b2016-08-11 13:26:41 -0400427 SSL_SIGN_RSA_PKCS1_SHA512,
David Benjaminc895d6b2016-08-11 13:26:41 -0400428
Robert Sloana27a6a42017-09-05 08:39:28 -0700429 // For now, SHA-1 is still accepted but least preferable.
Steven Valdez909b19f2016-11-21 15:35:44 -0500430 SSL_SIGN_RSA_PKCS1_SHA1,
David Benjaminc895d6b2016-08-11 13:26:41 -0400431
Steven Valdez909b19f2016-11-21 15:35:44 -0500432};
433
Robert Sloana27a6a42017-09-05 08:39:28 -0700434// kSignSignatureAlgorithms is the default list of supported signature
435// algorithms for signing.
436//
437// For now, RSA-PSS signature algorithms are not enabled on Android's system
438// BoringSSL. Once the change in Chrome has stuck and the values are finalized,
439// restore them.
Steven Valdez909b19f2016-11-21 15:35:44 -0500440static const uint16_t kSignSignatureAlgorithms[] = {
Robert Sloana27a6a42017-09-05 08:39:28 -0700441 // List our preferred algorithms first.
Robert Sloan572a4e22017-04-17 10:52:19 -0700442 SSL_SIGN_ED25519,
Steven Valdez909b19f2016-11-21 15:35:44 -0500443 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700444 SSL_SIGN_RSA_PSS_RSAE_SHA256,
David Benjaminc895d6b2016-08-11 13:26:41 -0400445 SSL_SIGN_RSA_PKCS1_SHA256,
David Benjaminc895d6b2016-08-11 13:26:41 -0400446
Robert Sloana27a6a42017-09-05 08:39:28 -0700447 // If needed, sign larger hashes.
448 //
449 // TODO(davidben): Determine which of these may be pruned.
Steven Valdez909b19f2016-11-21 15:35:44 -0500450 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700451 SSL_SIGN_RSA_PSS_RSAE_SHA384,
Steven Valdez909b19f2016-11-21 15:35:44 -0500452 SSL_SIGN_RSA_PKCS1_SHA384,
453
454 SSL_SIGN_ECDSA_SECP521R1_SHA512,
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700455 SSL_SIGN_RSA_PSS_RSAE_SHA512,
Steven Valdez909b19f2016-11-21 15:35:44 -0500456 SSL_SIGN_RSA_PKCS1_SHA512,
457
Robert Sloana27a6a42017-09-05 08:39:28 -0700458 // If the peer supports nothing else, sign with SHA-1.
David Benjaminc895d6b2016-08-11 13:26:41 -0400459 SSL_SIGN_ECDSA_SHA1,
Steven Valdez909b19f2016-11-21 15:35:44 -0500460 SSL_SIGN_RSA_PKCS1_SHA1,
David Benjaminc895d6b2016-08-11 13:26:41 -0400461};
462
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700463struct SSLSignatureAlgorithmList {
464 bool Next(uint16_t *out) {
465 while (!list.empty()) {
466 uint16_t sigalg = list[0];
467 list = list.subspan(1);
468 if (skip_ed25519 && sigalg == SSL_SIGN_ED25519) {
469 continue;
470 }
471 if (skip_rsa_pss_rsae && SSL_is_signature_algorithm_rsa_pss(sigalg)) {
472 continue;
473 }
474 *out = sigalg;
475 return true;
476 }
477 return false;
Robert Sloan572a4e22017-04-17 10:52:19 -0700478 }
479
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700480 bool operator==(const SSLSignatureAlgorithmList &other) const {
481 SSLSignatureAlgorithmList a = *this;
482 SSLSignatureAlgorithmList b = other;
483 uint16_t a_val, b_val;
484 while (a.Next(&a_val)) {
485 if (!b.Next(&b_val) ||
486 a_val != b_val) {
487 return false;
488 }
Robert Sloan572a4e22017-04-17 10:52:19 -0700489 }
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700490 return !b.Next(&b_val);
491 }
492
493 bool operator!=(const SSLSignatureAlgorithmList &other) const {
494 return !(*this == other);
495 }
496
497 Span<const uint16_t> list;
498 bool skip_ed25519 = false;
499 bool skip_rsa_pss_rsae = false;
500};
501
502static SSLSignatureAlgorithmList tls12_get_verify_sigalgs(const SSL *ssl,
503 bool for_certs) {
504 SSLSignatureAlgorithmList ret;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100505 if (!ssl->config->verify_sigalgs.empty()) {
506 ret.list = ssl->config->verify_sigalgs;
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700507 } else {
508 ret.list = kVerifySignatureAlgorithms;
509 ret.skip_ed25519 = !ssl->ctx->ed25519_enabled;
510 }
511 if (for_certs) {
512 ret.skip_rsa_pss_rsae = !ssl->ctx->rsa_pss_rsae_certs_enabled;
513 }
514 return ret;
515}
516
517bool tls12_add_verify_sigalgs(const SSL *ssl, CBB *out, bool for_certs) {
518 SSLSignatureAlgorithmList list = tls12_get_verify_sigalgs(ssl, for_certs);
519 uint16_t sigalg;
520 while (list.Next(&sigalg)) {
Robert Sloan309a31e2018-01-29 10:22:47 -0800521 if (!CBB_add_u16(out, sigalg)) {
522 return false;
Robert Sloan572a4e22017-04-17 10:52:19 -0700523 }
524 }
Robert Sloan309a31e2018-01-29 10:22:47 -0800525 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800526}
527
Robert Sloan309a31e2018-01-29 10:22:47 -0800528bool tls12_check_peer_sigalg(const SSL *ssl, uint8_t *out_alert,
529 uint16_t sigalg) {
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700530 SSLSignatureAlgorithmList list = tls12_get_verify_sigalgs(ssl, false);
531 uint16_t verify_sigalg;
532 while (list.Next(&verify_sigalg)) {
533 if (verify_sigalg == sigalg) {
Robert Sloan309a31e2018-01-29 10:22:47 -0800534 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800535 }
536 }
537
David Benjamin95add822016-10-19 01:09:12 -0400538 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
539 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan309a31e2018-01-29 10:22:47 -0800540 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800541}
542
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700543bool tls12_has_different_verify_sigalgs_for_certs(const SSL *ssl) {
544 return tls12_get_verify_sigalgs(ssl, true) !=
545 tls12_get_verify_sigalgs(ssl, false);
546}
547
Robert Sloana27a6a42017-09-05 08:39:28 -0700548// tls_extension represents a TLS extension that is handled internally. The
549// |init| function is called for each handshake, before any other functions of
550// the extension. Then the add and parse callbacks are called as needed.
551//
552// The parse callbacks receive a |CBS| that contains the contents of the
553// extension (i.e. not including the type and length bytes). If an extension is
554// not received then the parse callbacks will be called with a NULL CBS so that
555// they can do any processing needed to handle the absence of an extension.
556//
557// The add callbacks receive a |CBB| to which the extension can be appended but
558// the function is responsible for appending the type and length bytes too.
559//
Robert Sloan921ef2c2017-10-17 09:02:20 -0700560// All callbacks return true for success and false for error. If a parse
561// function returns zero then a fatal alert with value |*out_alert| will be
562// sent. If |*out_alert| isn't set, then a |decode_error| alert will be sent.
Kenny Rootb8494592015-09-25 02:29:14 +0000563struct tls_extension {
564 uint16_t value;
David Benjamin1b249672016-12-06 18:25:50 -0500565 void (*init)(SSL_HANDSHAKE *hs);
Kenny Roota04d78d2015-09-25 00:26:37 +0000566
Robert Sloan921ef2c2017-10-17 09:02:20 -0700567 bool (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
568 bool (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
569 CBS *contents);
Kenny Roota04d78d2015-09-25 00:26:37 +0000570
Robert Sloan921ef2c2017-10-17 09:02:20 -0700571 bool (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
572 CBS *contents);
573 bool (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Kenny Rootb8494592015-09-25 02:29:14 +0000574};
Kenny Roota04d78d2015-09-25 00:26:37 +0000575
Robert Sloan921ef2c2017-10-17 09:02:20 -0700576static bool forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin1b249672016-12-06 18:25:50 -0500577 CBS *contents) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400578 if (contents != NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700579 // Servers MUST NOT send this extension.
David Benjaminc895d6b2016-08-11 13:26:41 -0400580 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
581 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700582 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400583 }
584
Robert Sloan921ef2c2017-10-17 09:02:20 -0700585 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400586}
587
Robert Sloan921ef2c2017-10-17 09:02:20 -0700588static bool ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin1b249672016-12-06 18:25:50 -0500589 CBS *contents) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700590 // This extension from the client is handled elsewhere.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700591 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400592}
593
Robert Sloan921ef2c2017-10-17 09:02:20 -0700594static bool dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
595 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400596}
Kenny Rootb8494592015-09-25 02:29:14 +0000597
Robert Sloana27a6a42017-09-05 08:39:28 -0700598// Server name indication (SNI).
599//
600// https://tools.ietf.org/html/rfc6066#section-3.
Kenny Rootb8494592015-09-25 02:29:14 +0000601
Robert Sloan921ef2c2017-10-17 09:02:20 -0700602static bool ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500603 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100604 if (ssl->hostname == nullptr) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700605 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000606 }
607
608 CBB contents, server_name_list, name;
609 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
610 !CBB_add_u16_length_prefixed(out, &contents) ||
611 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
612 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
613 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100614 !CBB_add_bytes(&name, (const uint8_t *)ssl->hostname.get(),
615 strlen(ssl->hostname.get())) ||
Kenny Rootb8494592015-09-25 02:29:14 +0000616 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700617 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000618 }
619
Robert Sloan921ef2c2017-10-17 09:02:20 -0700620 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000621}
622
Robert Sloan921ef2c2017-10-17 09:02:20 -0700623static bool ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
624 CBS *contents) {
Robert Sloand1d118f2017-09-11 09:00:48 -0700625 // The server may acknowledge SNI with an empty extension. We check the syntax
626 // but otherwise ignore this signal.
627 return contents == NULL || CBS_len(contents) == 0;
Kenny Rootb8494592015-09-25 02:29:14 +0000628}
629
Robert Sloan921ef2c2017-10-17 09:02:20 -0700630static bool ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
631 CBS *contents) {
Srinivas Paladugudd42a612019-08-09 19:30:39 +0000632 SSL *const ssl = hs->ssl;
633 if (contents == NULL) {
634 return true;
635 }
636
637 CBS server_name_list, host_name;
638 uint8_t name_type;
639 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
640 !CBS_get_u8(&server_name_list, &name_type) ||
641 // Although the server_name extension was intended to be extensible to
642 // new name types and multiple names, OpenSSL 1.0.x had a bug which meant
643 // different name types will cause an error. Further, RFC 4366 originally
644 // defined syntax inextensibly. RFC 6066 corrected this mistake, but
645 // adding new name types is no longer feasible.
646 //
647 // Act as if the extensibility does not exist to simplify parsing.
648 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
649 CBS_len(&server_name_list) != 0 ||
650 CBS_len(contents) != 0) {
651 return false;
652 }
653
654 if (name_type != TLSEXT_NAMETYPE_host_name ||
655 CBS_len(&host_name) == 0 ||
656 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
657 CBS_contains_zero_byte(&host_name)) {
658 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
659 return false;
660 }
661
662 // Copy the hostname as a string.
663 char *raw = nullptr;
664 if (!CBS_strdup(&host_name, &raw)) {
665 *out_alert = SSL_AD_INTERNAL_ERROR;
666 return false;
667 }
668 ssl->s3->hostname.reset(raw);
669
670 hs->should_ack_sni = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700671 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000672}
673
Robert Sloan921ef2c2017-10-17 09:02:20 -0700674static bool ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500675 if (hs->ssl->s3->session_reused ||
676 !hs->should_ack_sni) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700677 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000678 }
679
680 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
681 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700682 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000683 }
684
Robert Sloan921ef2c2017-10-17 09:02:20 -0700685 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000686}
687
688
Robert Sloana27a6a42017-09-05 08:39:28 -0700689// Renegotiation indication.
690//
691// https://tools.ietf.org/html/rfc5746
Kenny Rootb8494592015-09-25 02:29:14 +0000692
Robert Sloan921ef2c2017-10-17 09:02:20 -0700693static bool ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500694 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700695 // Renegotiation indication is not necessary in TLS 1.3.
Robert Sloane56da3e2017-06-26 08:26:42 -0700696 if (hs->min_version >= TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700697 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400698 }
699
David Benjamin95add822016-10-19 01:09:12 -0400700 assert(ssl->s3->initial_handshake_complete ==
701 (ssl->s3->previous_client_finished_len != 0));
702
Kenny Rootb8494592015-09-25 02:29:14 +0000703 CBB contents, prev_finished;
704 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
705 !CBB_add_u16_length_prefixed(out, &contents) ||
706 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
707 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
708 ssl->s3->previous_client_finished_len) ||
709 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700710 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000711 }
712
Robert Sloan921ef2c2017-10-17 09:02:20 -0700713 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000714}
715
Robert Sloan921ef2c2017-10-17 09:02:20 -0700716static bool ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
717 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500718 SSL *const ssl = hs->ssl;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700719 if (contents != NULL && ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700720 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700721 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400722 }
723
Robert Sloana27a6a42017-09-05 08:39:28 -0700724 // Servers may not switch between omitting the extension and supporting it.
725 // See RFC 5746, sections 3.5 and 4.2.
Adam Langley4139edb2016-01-13 15:00:54 -0800726 if (ssl->s3->initial_handshake_complete &&
727 (contents != NULL) != ssl->s3->send_connection_binding) {
728 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
729 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700730 return false;
Adam Langley4139edb2016-01-13 15:00:54 -0800731 }
732
Kenny Rootb8494592015-09-25 02:29:14 +0000733 if (contents == NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700734 // Strictly speaking, if we want to avoid an attack we should *always* see
735 // RI even on initial ServerHello because the client doesn't see any
736 // renegotiation during an attack. However this would mean we could not
737 // connect to any server which doesn't support RI.
738 //
739 // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
740 // practical terms every client sets it so it's just assumed here.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700741 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000742 }
743
744 const size_t expected_len = ssl->s3->previous_client_finished_len +
745 ssl->s3->previous_server_finished_len;
746
Robert Sloana27a6a42017-09-05 08:39:28 -0700747 // Check for logic errors
Kenny Rootb8494592015-09-25 02:29:14 +0000748 assert(!expected_len || ssl->s3->previous_client_finished_len);
749 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin95add822016-10-19 01:09:12 -0400750 assert(ssl->s3->initial_handshake_complete ==
751 (ssl->s3->previous_client_finished_len != 0));
752 assert(ssl->s3->initial_handshake_complete ==
753 (ssl->s3->previous_server_finished_len != 0));
Kenny Rootb8494592015-09-25 02:29:14 +0000754
Robert Sloana27a6a42017-09-05 08:39:28 -0700755 // Parse out the extension contents.
Kenny Rootb8494592015-09-25 02:29:14 +0000756 CBS renegotiated_connection;
757 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
758 CBS_len(contents) != 0) {
759 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
760 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700761 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000762 }
763
Robert Sloana27a6a42017-09-05 08:39:28 -0700764 // Check that the extension matches.
Kenny Rootb8494592015-09-25 02:29:14 +0000765 if (CBS_len(&renegotiated_connection) != expected_len) {
766 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
767 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700768 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000769 }
770
771 const uint8_t *d = CBS_data(&renegotiated_connection);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700772 bool ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
773 ssl->s3->previous_client_finished_len) == 0;
Robert Sloanf6200e72017-07-10 08:09:18 -0700774#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
Robert Sloan921ef2c2017-10-17 09:02:20 -0700775 ok = true;
Robert Sloanf6200e72017-07-10 08:09:18 -0700776#endif
777 if (!ok) {
Kenny Rootb8494592015-09-25 02:29:14 +0000778 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
779 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700780 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000781 }
782 d += ssl->s3->previous_client_finished_len;
783
Robert Sloanf6200e72017-07-10 08:09:18 -0700784 ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
785 ssl->s3->previous_server_finished_len) == 0;
786#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
Robert Sloan921ef2c2017-10-17 09:02:20 -0700787 ok = true;
Robert Sloanf6200e72017-07-10 08:09:18 -0700788#endif
789 if (!ok) {
Kenny Rootb8494592015-09-25 02:29:14 +0000790 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700791 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700792 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000793 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700794 ssl->s3->send_connection_binding = true;
Kenny Rootb8494592015-09-25 02:29:14 +0000795
Robert Sloan921ef2c2017-10-17 09:02:20 -0700796 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000797}
798
Robert Sloan921ef2c2017-10-17 09:02:20 -0700799static bool ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
800 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500801 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700802 // Renegotiation isn't supported as a server so this function should never be
803 // called after the initial handshake.
Kenny Rootb8494592015-09-25 02:29:14 +0000804 assert(!ssl->s3->initial_handshake_complete);
805
Robert Sloan921ef2c2017-10-17 09:02:20 -0700806 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
807 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400808 }
Kenny Rootb8494592015-09-25 02:29:14 +0000809
810 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700811 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000812 }
813
814 CBS renegotiated_connection;
Kenny Rootb8494592015-09-25 02:29:14 +0000815 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
816 CBS_len(contents) != 0) {
817 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700818 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000819 }
820
Robert Sloana27a6a42017-09-05 08:39:28 -0700821 // Check that the extension matches. We do not support renegotiation as a
822 // server, so this must be empty.
David Benjamin95add822016-10-19 01:09:12 -0400823 if (CBS_len(&renegotiated_connection) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000824 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
825 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700826 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000827 }
828
Robert Sloana27a6a42017-09-05 08:39:28 -0700829 ssl->s3->send_connection_binding = true;
Kenny Rootb8494592015-09-25 02:29:14 +0000830
Robert Sloan921ef2c2017-10-17 09:02:20 -0700831 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000832}
833
Robert Sloan921ef2c2017-10-17 09:02:20 -0700834static bool ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500835 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700836 // Renegotiation isn't supported as a server so this function should never be
837 // called after the initial handshake.
David Benjamin95add822016-10-19 01:09:12 -0400838 assert(!ssl->s3->initial_handshake_complete);
839
Robert Sloan921ef2c2017-10-17 09:02:20 -0700840 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
841 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400842 }
843
Kenny Rootb8494592015-09-25 02:29:14 +0000844 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin95add822016-10-19 01:09:12 -0400845 !CBB_add_u16(out, 1 /* length */) ||
846 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700847 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000848 }
849
Robert Sloan921ef2c2017-10-17 09:02:20 -0700850 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000851}
852
853
Robert Sloana27a6a42017-09-05 08:39:28 -0700854// Extended Master Secret.
855//
856// https://tools.ietf.org/html/rfc7627
Kenny Rootb8494592015-09-25 02:29:14 +0000857
Robert Sloan921ef2c2017-10-17 09:02:20 -0700858static bool ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700859 // Extended master secret is not necessary in TLS 1.3.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100860 if (hs->min_version >= TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700861 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000862 }
863
864 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
865 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700866 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000867 }
868
Robert Sloan921ef2c2017-10-17 09:02:20 -0700869 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000870}
871
Robert Sloan921ef2c2017-10-17 09:02:20 -0700872static bool ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
873 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500874 SSL *const ssl = hs->ssl;
Robert Sloana94fe052017-02-21 08:49:28 -0800875
876 if (contents != NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700877 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
Robert Sloana94fe052017-02-21 08:49:28 -0800878 CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700879 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400880 }
881
Robert Sloana27a6a42017-09-05 08:39:28 -0700882 hs->extended_master_secret = true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400883 }
884
Robert Sloana27a6a42017-09-05 08:39:28 -0700885 // Whether EMS is negotiated may not change on renegotiation.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700886 if (ssl->s3->established_session != nullptr &&
Robert Sloana94fe052017-02-21 08:49:28 -0800887 hs->extended_master_secret !=
Robert Sloana27a6a42017-09-05 08:39:28 -0700888 !!ssl->s3->established_session->extended_master_secret) {
Robert Sloana94fe052017-02-21 08:49:28 -0800889 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
890 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700891 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400892 }
893
Robert Sloan921ef2c2017-10-17 09:02:20 -0700894 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000895}
896
Robert Sloan921ef2c2017-10-17 09:02:20 -0700897static bool ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
898 CBS *contents) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100899 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700900 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400901 }
902
903 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700904 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000905 }
906
907 if (CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700908 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000909 }
910
Robert Sloana27a6a42017-09-05 08:39:28 -0700911 hs->extended_master_secret = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700912 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000913}
914
Robert Sloan921ef2c2017-10-17 09:02:20 -0700915static bool ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloana94fe052017-02-21 08:49:28 -0800916 if (!hs->extended_master_secret) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700917 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000918 }
919
920 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
921 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700922 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000923 }
924
Robert Sloan921ef2c2017-10-17 09:02:20 -0700925 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000926}
927
928
Robert Sloana27a6a42017-09-05 08:39:28 -0700929// Session tickets.
930//
931// https://tools.ietf.org/html/rfc5077
Kenny Rootb8494592015-09-25 02:29:14 +0000932
Robert Sloan921ef2c2017-10-17 09:02:20 -0700933static bool ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500934 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700935 // TLS 1.3 uses a different ticket extension.
Robert Sloane56da3e2017-06-26 08:26:42 -0700936 if (hs->min_version >= TLS1_3_VERSION ||
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400937 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700938 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000939 }
940
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100941 Span<const uint8_t> ticket;
Kenny Rootb8494592015-09-25 02:29:14 +0000942
Robert Sloana27a6a42017-09-05 08:39:28 -0700943 // Renegotiation does not participate in session resumption. However, still
944 // advertise the extension to avoid potentially breaking servers which carry
945 // over the state from the previous handshake, such as OpenSSL servers
946 // without upstream's 3c3f0259238594d77264a78944d409f2127642c4.
Kenny Rootb8494592015-09-25 02:29:14 +0000947 if (!ssl->s3->initial_handshake_complete &&
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100948 ssl->session != nullptr &&
949 !ssl->session->ticket.empty() &&
Robert Sloana27a6a42017-09-05 08:39:28 -0700950 // Don't send TLS 1.3 session tickets in the ticket extension.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100951 ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) {
952 ticket = ssl->session->ticket;
Kenny Rootb8494592015-09-25 02:29:14 +0000953 }
954
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100955 CBB ticket_cbb;
Kenny Rootb8494592015-09-25 02:29:14 +0000956 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100957 !CBB_add_u16_length_prefixed(out, &ticket_cbb) ||
958 !CBB_add_bytes(&ticket_cbb, ticket.data(), ticket.size()) ||
Kenny Rootb8494592015-09-25 02:29:14 +0000959 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700960 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000961 }
962
Robert Sloan921ef2c2017-10-17 09:02:20 -0700963 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000964}
965
Robert Sloan921ef2c2017-10-17 09:02:20 -0700966static bool ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
967 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500968 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +0000969 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700970 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000971 }
972
Robert Sloan921ef2c2017-10-17 09:02:20 -0700973 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
974 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400975 }
976
Robert Sloana27a6a42017-09-05 08:39:28 -0700977 // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
978 // this function should never be called, even if the server tries to send the
979 // extension.
Kenny Rootb8494592015-09-25 02:29:14 +0000980 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
981
982 if (CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700983 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000984 }
985
Robert Sloana27a6a42017-09-05 08:39:28 -0700986 hs->ticket_expected = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700987 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000988}
989
Robert Sloan921ef2c2017-10-17 09:02:20 -0700990static bool ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500991 if (!hs->ticket_expected) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700992 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000993 }
994
Robert Sloana27a6a42017-09-05 08:39:28 -0700995 // If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true.
David Benjamin1b249672016-12-06 18:25:50 -0500996 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Kenny Rootb8494592015-09-25 02:29:14 +0000997
998 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
999 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001000 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001001 }
1002
Robert Sloan921ef2c2017-10-17 09:02:20 -07001003 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001004}
1005
1006
Robert Sloana27a6a42017-09-05 08:39:28 -07001007// Signature Algorithms.
1008//
1009// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
Kenny Rootb8494592015-09-25 02:29:14 +00001010
Robert Sloan921ef2c2017-10-17 09:02:20 -07001011static bool ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001012 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -07001013 if (hs->max_version < TLS1_2_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001014 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001015 }
1016
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001017 // Prior to TLS 1.3, there was no way to signal different signature algorithm
1018 // preferences between the online signature and certificates. If we do not
1019 // send the signature_algorithms_cert extension, use the potentially more
1020 // restrictive certificate list.
1021 //
1022 // TODO(davidben): When TLS 1.3 is finalized, we can likely remove the TLS 1.3
1023 // check both here and in signature_algorithms_cert. |hs->max_version| is not
1024 // the negotiated version. Rather the expectation is that any server consuming
1025 // signature algorithms added in TLS 1.3 will also know to look at
1026 // signature_algorithms_cert. For now, TLS 1.3 is not quite yet final and it
1027 // seems prudent to condition this new extension on it.
1028 bool for_certs = hs->max_version < TLS1_3_VERSION;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001029 CBB contents, sigalgs_cbb;
Kenny Rootb8494592015-09-25 02:29:14 +00001030 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1031 !CBB_add_u16_length_prefixed(out, &contents) ||
Robert Sloan572a4e22017-04-17 10:52:19 -07001032 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001033 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb, for_certs) ||
Robert Sloan572a4e22017-04-17 10:52:19 -07001034 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001035 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001036 }
1037
Robert Sloan921ef2c2017-10-17 09:02:20 -07001038 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001039}
1040
Robert Sloan921ef2c2017-10-17 09:02:20 -07001041static bool ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1042 CBS *contents) {
Robert Sloan4562e9d2017-10-02 10:26:51 -07001043 hs->peer_sigalgs.Reset();
Kenny Rootb8494592015-09-25 02:29:14 +00001044 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001045 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001046 }
1047
1048 CBS supported_signature_algorithms;
1049 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
1050 CBS_len(contents) != 0 ||
David Benjamin1b249672016-12-06 18:25:50 -05001051 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001052 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001053 }
1054
Robert Sloan921ef2c2017-10-17 09:02:20 -07001055 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001056}
1057
Kenny Rootb8494592015-09-25 02:29:14 +00001058
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001059// Signature Algorithms for Certificates.
1060//
Robert Sloand9e572d2018-08-27 12:27:00 -07001061// https://tools.ietf.org/html/rfc8446#section-4.2.3
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001062
1063static bool ext_sigalgs_cert_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1064 SSL *const ssl = hs->ssl;
1065 // If this extension is omitted, it defaults to the signature_algorithms
1066 // extension, so only emit it if the list is different.
1067 //
1068 // This extension is also new in TLS 1.3, so omit it if TLS 1.3 is disabled.
1069 // There is a corresponding version check in |ext_sigalgs_add_clienthello|.
1070 if (hs->max_version < TLS1_3_VERSION ||
1071 !tls12_has_different_verify_sigalgs_for_certs(ssl)) {
1072 return true;
1073 }
1074
1075 CBB contents, sigalgs_cbb;
1076 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms_cert) ||
1077 !CBB_add_u16_length_prefixed(out, &contents) ||
1078 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
1079 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb, true /* certs */) ||
1080 !CBB_flush(out)) {
1081 return false;
1082 }
1083
1084 return true;
1085}
1086
1087
Robert Sloana27a6a42017-09-05 08:39:28 -07001088// OCSP Stapling.
1089//
1090// https://tools.ietf.org/html/rfc6066#section-8
Kenny Rootb8494592015-09-25 02:29:14 +00001091
Robert Sloan921ef2c2017-10-17 09:02:20 -07001092static bool ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001093 if (!hs->config->ocsp_stapling_enabled) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001094 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001095 }
1096
1097 CBB contents;
1098 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1099 !CBB_add_u16_length_prefixed(out, &contents) ||
1100 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1101 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1102 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1103 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001104 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001105 }
1106
Robert Sloan921ef2c2017-10-17 09:02:20 -07001107 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001108}
1109
Robert Sloan921ef2c2017-10-17 09:02:20 -07001110static bool ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1111 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001112 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001113 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001114 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001115 }
1116
Robert Sloana27a6a42017-09-05 08:39:28 -07001117 // TLS 1.3 OCSP responses are included in the Certificate extensions.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001118 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1119 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001120 }
1121
Robert Sloana27a6a42017-09-05 08:39:28 -07001122 // OCSP stapling is forbidden on non-certificate ciphers.
Steven Valdez909b19f2016-11-21 15:35:44 -05001123 if (CBS_len(contents) != 0 ||
Robert Sloana94fe052017-02-21 08:49:28 -08001124 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001125 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04001126 }
1127
Robert Sloana27a6a42017-09-05 08:39:28 -07001128 // Note this does not check for resumption in TLS 1.2. Sending
1129 // status_request here does not make sense, but OpenSSL does so and the
1130 // specification does not say anything. Tolerate it but ignore it.
David Benjaminc895d6b2016-08-11 13:26:41 -04001131
Robert Sloana27a6a42017-09-05 08:39:28 -07001132 hs->certificate_status_expected = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001133 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001134}
1135
Robert Sloan921ef2c2017-10-17 09:02:20 -07001136static bool ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1137 CBS *contents) {
Kenny Rootb8494592015-09-25 02:29:14 +00001138 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001139 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001140 }
1141
1142 uint8_t status_type;
1143 if (!CBS_get_u8(contents, &status_type)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001144 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001145 }
1146
Robert Sloana27a6a42017-09-05 08:39:28 -07001147 // We cannot decide whether OCSP stapling will occur yet because the correct
1148 // SSL_CTX might not have been selected.
David Benjamin1b249672016-12-06 18:25:50 -05001149 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Kenny Rootb8494592015-09-25 02:29:14 +00001150
Robert Sloan921ef2c2017-10-17 09:02:20 -07001151 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001152}
1153
Robert Sloan921ef2c2017-10-17 09:02:20 -07001154static bool ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001155 SSL *const ssl = hs->ssl;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001156 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001157 !hs->ocsp_stapling_requested || hs->config->cert->ocsp_response == NULL ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001158 ssl->s3->session_reused ||
Robert Sloana94fe052017-02-21 08:49:28 -08001159 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001160 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001161 }
1162
Robert Sloana27a6a42017-09-05 08:39:28 -07001163 hs->certificate_status_expected = true;
Kenny Rootb8494592015-09-25 02:29:14 +00001164
1165 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdez909b19f2016-11-21 15:35:44 -05001166 CBB_add_u16(out, 0 /* length */);
Kenny Rootb8494592015-09-25 02:29:14 +00001167}
1168
1169
Robert Sloana27a6a42017-09-05 08:39:28 -07001170// Next protocol negotiation.
1171//
1172// https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
Kenny Rootb8494592015-09-25 02:29:14 +00001173
Robert Sloan921ef2c2017-10-17 09:02:20 -07001174static bool ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001175 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001176 if (ssl->s3->initial_handshake_complete ||
1177 ssl->ctx->next_proto_select_cb == NULL ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001178 SSL_is_dtls(ssl)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001179 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001180 }
1181
1182 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1183 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001184 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001185 }
1186
Robert Sloan921ef2c2017-10-17 09:02:20 -07001187 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001188}
1189
Robert Sloan921ef2c2017-10-17 09:02:20 -07001190static bool ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1191 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001192 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001193 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001194 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001195 }
1196
Robert Sloan921ef2c2017-10-17 09:02:20 -07001197 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1198 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04001199 }
1200
Robert Sloana27a6a42017-09-05 08:39:28 -07001201 // If any of these are false then we should never have sent the NPN
1202 // extension in the ClientHello and thus this function should never have been
1203 // called.
Kenny Rootb8494592015-09-25 02:29:14 +00001204 assert(!ssl->s3->initial_handshake_complete);
David Benjaminc895d6b2016-08-11 13:26:41 -04001205 assert(!SSL_is_dtls(ssl));
Kenny Rootb8494592015-09-25 02:29:14 +00001206 assert(ssl->ctx->next_proto_select_cb != NULL);
1207
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001208 if (!ssl->s3->alpn_selected.empty()) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001209 // NPN and ALPN may not be negotiated in the same connection.
Kenny Rootb8494592015-09-25 02:29:14 +00001210 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1211 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001212 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001213 }
1214
1215 const uint8_t *const orig_contents = CBS_data(contents);
1216 const size_t orig_len = CBS_len(contents);
1217
1218 while (CBS_len(contents) != 0) {
1219 CBS proto;
1220 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1221 CBS_len(&proto) == 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001222 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001223 }
1224 }
1225
1226 uint8_t *selected;
1227 uint8_t selected_len;
1228 if (ssl->ctx->next_proto_select_cb(
1229 ssl, &selected, &selected_len, orig_contents, orig_len,
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001230 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK ||
1231 !ssl->s3->next_proto_negotiated.CopyFrom(
1232 MakeConstSpan(selected, selected_len))) {
Kenny Rootb8494592015-09-25 02:29:14 +00001233 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001234 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001235 }
1236
Robert Sloana27a6a42017-09-05 08:39:28 -07001237 hs->next_proto_neg_seen = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001238 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001239}
1240
Robert Sloan921ef2c2017-10-17 09:02:20 -07001241static bool ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1242 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001243 SSL *const ssl = hs->ssl;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001244 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1245 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04001246 }
1247
Kenny Rootb8494592015-09-25 02:29:14 +00001248 if (contents != NULL && CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001249 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001250 }
1251
1252 if (contents == NULL ||
1253 ssl->s3->initial_handshake_complete ||
Kenny Rootb8494592015-09-25 02:29:14 +00001254 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001255 SSL_is_dtls(ssl)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001256 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001257 }
1258
Robert Sloana27a6a42017-09-05 08:39:28 -07001259 hs->next_proto_neg_seen = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001260 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001261}
1262
Robert Sloan921ef2c2017-10-17 09:02:20 -07001263static bool ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001264 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001265 // |next_proto_neg_seen| might have been cleared when an ALPN extension was
1266 // parsed.
David Benjamin1b249672016-12-06 18:25:50 -05001267 if (!hs->next_proto_neg_seen) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001268 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001269 }
1270
1271 const uint8_t *npa;
1272 unsigned npa_len;
1273
1274 if (ssl->ctx->next_protos_advertised_cb(
1275 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1276 SSL_TLSEXT_ERR_OK) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001277 hs->next_proto_neg_seen = false;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001278 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001279 }
1280
1281 CBB contents;
1282 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1283 !CBB_add_u16_length_prefixed(out, &contents) ||
1284 !CBB_add_bytes(&contents, npa, npa_len) ||
1285 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001286 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001287 }
1288
Robert Sloan921ef2c2017-10-17 09:02:20 -07001289 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001290}
1291
1292
Robert Sloana27a6a42017-09-05 08:39:28 -07001293// Signed certificate timestamps.
1294//
1295// https://tools.ietf.org/html/rfc6962#section-3.3.1
Kenny Rootb8494592015-09-25 02:29:14 +00001296
Robert Sloan921ef2c2017-10-17 09:02:20 -07001297static bool ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001298 if (!hs->config->signed_cert_timestamps_enabled) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001299 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001300 }
1301
1302 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1303 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001304 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001305 }
1306
Robert Sloan921ef2c2017-10-17 09:02:20 -07001307 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001308}
1309
Robert Sloan921ef2c2017-10-17 09:02:20 -07001310static bool ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1311 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001312 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001313 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001314 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001315 }
1316
Robert Sloana27a6a42017-09-05 08:39:28 -07001317 // TLS 1.3 SCTs are included in the Certificate extensions.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001318 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001319 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001320 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05001321 }
1322
Robert Sloana27a6a42017-09-05 08:39:28 -07001323 // If this is false then we should never have sent the SCT extension in the
1324 // ClientHello and thus this function should never have been called.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001325 assert(hs->config->signed_cert_timestamps_enabled);
Kenny Rootb8494592015-09-25 02:29:14 +00001326
Steven Valdez909b19f2016-11-21 15:35:44 -05001327 if (!ssl_is_sct_list_valid(contents)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001328 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001329 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001330 }
1331
Robert Sloana27a6a42017-09-05 08:39:28 -07001332 // Session resumption uses the original session information. The extension
1333 // should not be sent on resumption, but RFC 6962 did not make it a
1334 // requirement, so tolerate this.
1335 //
1336 // TODO(davidben): Enforce this anyway.
Robert Sloan8f860b12017-08-28 07:37:06 -07001337 if (!ssl->s3->session_reused) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001338 hs->new_session->signed_cert_timestamp_list.reset(
1339 CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool));
Robert Sloan8f860b12017-08-28 07:37:06 -07001340 if (hs->new_session->signed_cert_timestamp_list == nullptr) {
1341 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001342 return false;
Robert Sloan8f860b12017-08-28 07:37:06 -07001343 }
Kenny Rootb8494592015-09-25 02:29:14 +00001344 }
1345
Robert Sloan921ef2c2017-10-17 09:02:20 -07001346 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001347}
1348
Robert Sloan921ef2c2017-10-17 09:02:20 -07001349static bool ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1350 CBS *contents) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001351 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001352 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05001353 }
1354
1355 if (CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001356 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05001357 }
1358
Robert Sloana27a6a42017-09-05 08:39:28 -07001359 hs->scts_requested = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001360 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001361}
1362
Robert Sloan921ef2c2017-10-17 09:02:20 -07001363static bool ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001364 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001365 // The extension shouldn't be sent when resuming sessions.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001366 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION || ssl->s3->session_reused ||
1367 hs->config->cert->signed_cert_timestamp_list == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001368 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001369 }
1370
1371 CBB contents;
1372 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1373 CBB_add_u16_length_prefixed(out, &contents) &&
Robert Sloana94fe052017-02-21 08:49:28 -08001374 CBB_add_bytes(
1375 &contents,
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001376 CRYPTO_BUFFER_data(
1377 hs->config->cert->signed_cert_timestamp_list.get()),
1378 CRYPTO_BUFFER_len(
1379 hs->config->cert->signed_cert_timestamp_list.get())) &&
Kenny Rootb8494592015-09-25 02:29:14 +00001380 CBB_flush(out);
1381}
1382
1383
Robert Sloana27a6a42017-09-05 08:39:28 -07001384// Application-level Protocol Negotiation.
1385//
1386// https://tools.ietf.org/html/rfc7301
Kenny Rootb8494592015-09-25 02:29:14 +00001387
Robert Sloan921ef2c2017-10-17 09:02:20 -07001388static bool ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001389 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001390 if (hs->config->alpn_client_proto_list.empty() ||
Kenny Rootb8494592015-09-25 02:29:14 +00001391 ssl->s3->initial_handshake_complete) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001392 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001393 }
1394
1395 CBB contents, proto_list;
1396 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1397 !CBB_add_u16_length_prefixed(out, &contents) ||
1398 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001399 !CBB_add_bytes(&proto_list, hs->config->alpn_client_proto_list.data(),
1400 hs->config->alpn_client_proto_list.size()) ||
Kenny Rootb8494592015-09-25 02:29:14 +00001401 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001402 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001403 }
1404
Robert Sloan921ef2c2017-10-17 09:02:20 -07001405 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001406}
1407
Robert Sloan921ef2c2017-10-17 09:02:20 -07001408static bool ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1409 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001410 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001411 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001412 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001413 }
1414
1415 assert(!ssl->s3->initial_handshake_complete);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001416 assert(!hs->config->alpn_client_proto_list.empty());
Kenny Rootb8494592015-09-25 02:29:14 +00001417
David Benjamin1b249672016-12-06 18:25:50 -05001418 if (hs->next_proto_neg_seen) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001419 // NPN and ALPN may not be negotiated in the same connection.
Kenny Rootb8494592015-09-25 02:29:14 +00001420 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1421 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001422 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001423 }
1424
Robert Sloana27a6a42017-09-05 08:39:28 -07001425 // The extension data consists of a ProtocolNameList which must have
1426 // exactly one ProtocolName. Each of these is length-prefixed.
Kenny Rootb8494592015-09-25 02:29:14 +00001427 CBS protocol_name_list, protocol_name;
1428 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1429 CBS_len(contents) != 0 ||
1430 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
Robert Sloana27a6a42017-09-05 08:39:28 -07001431 // Empty protocol names are forbidden.
Kenny Rootb8494592015-09-25 02:29:14 +00001432 CBS_len(&protocol_name) == 0 ||
1433 CBS_len(&protocol_name_list) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001434 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001435 }
1436
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001437 if (!ssl_is_alpn_protocol_allowed(hs, protocol_name)) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001438 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1439 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1440 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04001441 }
1442
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001443 if (!ssl->s3->alpn_selected.CopyFrom(protocol_name)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001444 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001445 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001446 }
1447
Robert Sloan921ef2c2017-10-17 09:02:20 -07001448 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001449}
1450
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001451bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs,
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001452 Span<const uint8_t> protocol) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001453 if (hs->config->alpn_client_proto_list.empty()) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001454 return false;
1455 }
1456
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001457 if (hs->ssl->ctx->allow_unknown_alpn_protos) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001458 return true;
1459 }
1460
1461 // Check that the protocol name is one of the ones we advertised.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001462 CBS client_protocol_name_list =
1463 MakeConstSpan(hs->config->alpn_client_proto_list),
1464 client_protocol_name;
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001465 while (CBS_len(&client_protocol_name_list) > 0) {
1466 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1467 &client_protocol_name)) {
1468 return false;
1469 }
1470
1471 if (client_protocol_name == protocol) {
1472 return true;
1473 }
1474 }
1475
1476 return false;
1477}
1478
Robert Sloan921ef2c2017-10-17 09:02:20 -07001479bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1480 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin1b249672016-12-06 18:25:50 -05001481 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -05001482 CBS contents;
Kenny Rootb8494592015-09-25 02:29:14 +00001483 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin1b249672016-12-06 18:25:50 -05001484 !ssl_client_hello_get_extension(
Steven Valdez909b19f2016-11-21 15:35:44 -05001485 client_hello, &contents,
1486 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001487 // Ignore ALPN if not configured or no extension was supplied.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001488 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001489 }
1490
Robert Sloana27a6a42017-09-05 08:39:28 -07001491 // ALPN takes precedence over NPN.
1492 hs->next_proto_neg_seen = false;
Kenny Rootb8494592015-09-25 02:29:14 +00001493
1494 CBS protocol_name_list;
Steven Valdez909b19f2016-11-21 15:35:44 -05001495 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1496 CBS_len(&contents) != 0 ||
Kenny Rootb8494592015-09-25 02:29:14 +00001497 CBS_len(&protocol_name_list) < 2) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001498 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1499 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001500 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001501 }
1502
Robert Sloana27a6a42017-09-05 08:39:28 -07001503 // Validate the protocol list.
Kenny Rootb8494592015-09-25 02:29:14 +00001504 CBS protocol_name_list_copy = protocol_name_list;
1505 while (CBS_len(&protocol_name_list_copy) > 0) {
1506 CBS protocol_name;
1507
1508 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
Robert Sloana27a6a42017-09-05 08:39:28 -07001509 // Empty protocol names are forbidden.
Kenny Rootb8494592015-09-25 02:29:14 +00001510 CBS_len(&protocol_name) == 0) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001511 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1512 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001513 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001514 }
1515 }
1516
1517 const uint8_t *selected;
1518 uint8_t selected_len;
1519 if (ssl->ctx->alpn_select_cb(
1520 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1521 CBS_len(&protocol_name_list),
1522 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001523 if (selected_len == 0) {
1524 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1525 *out_alert = SSL_AD_INTERNAL_ERROR;
1526 return false;
1527 }
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001528 if (!ssl->s3->alpn_selected.CopyFrom(
1529 MakeConstSpan(selected, selected_len))) {
Kenny Rootb8494592015-09-25 02:29:14 +00001530 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001531 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001532 }
Kenny Rootb8494592015-09-25 02:29:14 +00001533 }
1534
Robert Sloan921ef2c2017-10-17 09:02:20 -07001535 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001536}
1537
Robert Sloan921ef2c2017-10-17 09:02:20 -07001538static bool ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001539 SSL *const ssl = hs->ssl;
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001540 if (ssl->s3->alpn_selected.empty()) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001541 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001542 }
1543
1544 CBB contents, proto_list, proto;
1545 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1546 !CBB_add_u16_length_prefixed(out, &contents) ||
1547 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1548 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001549 !CBB_add_bytes(&proto, ssl->s3->alpn_selected.data(),
1550 ssl->s3->alpn_selected.size()) ||
Kenny Rootb8494592015-09-25 02:29:14 +00001551 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001552 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001553 }
1554
Robert Sloan921ef2c2017-10-17 09:02:20 -07001555 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001556}
1557
1558
Robert Sloana27a6a42017-09-05 08:39:28 -07001559// Channel ID.
1560//
1561// https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
Kenny Rootb8494592015-09-25 02:29:14 +00001562
David Benjamin1b249672016-12-06 18:25:50 -05001563static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001564 hs->ssl->s3->channel_id_valid = false;
Kenny Rootb8494592015-09-25 02:29:14 +00001565}
1566
Robert Sloan921ef2c2017-10-17 09:02:20 -07001567static bool ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001568 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001569 if (!hs->config->channel_id_enabled || SSL_is_dtls(ssl)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001570 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001571 }
1572
1573 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1574 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001575 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001576 }
1577
Robert Sloan921ef2c2017-10-17 09:02:20 -07001578 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001579}
1580
Robert Sloan921ef2c2017-10-17 09:02:20 -07001581static bool ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1582 uint8_t *out_alert,
1583 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001584 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001585 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001586 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001587 }
1588
David Benjaminc895d6b2016-08-11 13:26:41 -04001589 assert(!SSL_is_dtls(ssl));
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001590 assert(hs->config->channel_id_enabled);
Kenny Rootb8494592015-09-25 02:29:14 +00001591
1592 if (CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001593 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001594 }
1595
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001596 ssl->s3->channel_id_valid = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001597 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001598}
1599
Robert Sloan921ef2c2017-10-17 09:02:20 -07001600static bool ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1601 uint8_t *out_alert,
1602 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001603 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001604 if (contents == NULL || !hs->config->channel_id_enabled || SSL_is_dtls(ssl)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001605 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001606 }
1607
1608 if (CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001609 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001610 }
1611
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001612 ssl->s3->channel_id_valid = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001613 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001614}
1615
Robert Sloan921ef2c2017-10-17 09:02:20 -07001616static bool ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001617 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001618 if (!ssl->s3->channel_id_valid) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001619 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001620 }
1621
1622 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1623 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001624 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001625 }
1626
Robert Sloan921ef2c2017-10-17 09:02:20 -07001627 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001628}
1629
1630
Robert Sloana27a6a42017-09-05 08:39:28 -07001631// Secure Real-time Transport Protocol (SRTP) extension.
1632//
1633// https://tools.ietf.org/html/rfc5764
Kenny Rootb8494592015-09-25 02:29:14 +00001634
1635
David Benjamin1b249672016-12-06 18:25:50 -05001636static void ext_srtp_init(SSL_HANDSHAKE *hs) {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001637 hs->ssl->s3->srtp_profile = NULL;
Kenny Rootb8494592015-09-25 02:29:14 +00001638}
1639
Robert Sloan921ef2c2017-10-17 09:02:20 -07001640static bool ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001641 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001642 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
Robert Sloanfe7cd212017-08-07 09:03:39 -07001643 if (profiles == NULL ||
1644 sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001645 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001646 }
1647
1648 CBB contents, profile_ids;
1649 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1650 !CBB_add_u16_length_prefixed(out, &contents) ||
1651 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001652 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001653 }
1654
Robert Sloanfe7cd212017-08-07 09:03:39 -07001655 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
1656 if (!CBB_add_u16(&profile_ids, profile->id)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001657 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001658 }
1659 }
1660
1661 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1662 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001663 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001664 }
1665
Robert Sloan921ef2c2017-10-17 09:02:20 -07001666 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001667}
1668
Robert Sloan921ef2c2017-10-17 09:02:20 -07001669static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1670 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001671 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001672 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001673 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001674 }
1675
Robert Sloana27a6a42017-09-05 08:39:28 -07001676 // The extension consists of a u16-prefixed profile ID list containing a
1677 // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1678 //
1679 // See https://tools.ietf.org/html/rfc5764#section-4.1.1
Kenny Rootb8494592015-09-25 02:29:14 +00001680 CBS profile_ids, srtp_mki;
1681 uint16_t profile_id;
1682 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1683 !CBS_get_u16(&profile_ids, &profile_id) ||
1684 CBS_len(&profile_ids) != 0 ||
1685 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1686 CBS_len(contents) != 0) {
1687 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001688 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001689 }
1690
1691 if (CBS_len(&srtp_mki) != 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001692 // Must be no MKI, since we never offer one.
Kenny Rootb8494592015-09-25 02:29:14 +00001693 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1694 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001695 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001696 }
1697
1698 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1699
Robert Sloana27a6a42017-09-05 08:39:28 -07001700 // Check to see if the server gave us something we support (and presumably
1701 // offered).
Robert Sloanfe7cd212017-08-07 09:03:39 -07001702 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
Kenny Rootb8494592015-09-25 02:29:14 +00001703 if (profile->id == profile_id) {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001704 ssl->s3->srtp_profile = profile;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001705 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001706 }
1707 }
1708
1709 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1710 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001711 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001712}
1713
Robert Sloan921ef2c2017-10-17 09:02:20 -07001714static bool ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1715 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001716 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001717 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001718 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001719 }
1720
1721 CBS profile_ids, srtp_mki;
1722 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1723 CBS_len(&profile_ids) < 2 ||
1724 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1725 CBS_len(contents) != 0) {
1726 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001727 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001728 }
Robert Sloana27a6a42017-09-05 08:39:28 -07001729 // Discard the MKI value for now.
Kenny Rootb8494592015-09-25 02:29:14 +00001730
1731 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1732 SSL_get_srtp_profiles(ssl);
1733
Robert Sloana27a6a42017-09-05 08:39:28 -07001734 // Pick the server's most preferred profile.
Robert Sloanfe7cd212017-08-07 09:03:39 -07001735 for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) {
Kenny Rootb8494592015-09-25 02:29:14 +00001736 CBS profile_ids_tmp;
1737 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1738
1739 while (CBS_len(&profile_ids_tmp) > 0) {
1740 uint16_t profile_id;
1741 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001742 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001743 }
1744
1745 if (server_profile->id == profile_id) {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001746 ssl->s3->srtp_profile = server_profile;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001747 return true;
Kenny Roota04d78d2015-09-25 00:26:37 +00001748 }
1749 }
1750 }
1751
Robert Sloan921ef2c2017-10-17 09:02:20 -07001752 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001753}
1754
Robert Sloan921ef2c2017-10-17 09:02:20 -07001755static bool ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001756 SSL *const ssl = hs->ssl;
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001757 if (ssl->s3->srtp_profile == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001758 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001759 }
1760
1761 CBB contents, profile_ids;
1762 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1763 !CBB_add_u16_length_prefixed(out, &contents) ||
1764 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001765 !CBB_add_u16(&profile_ids, ssl->s3->srtp_profile->id) ||
Kenny Rootb8494592015-09-25 02:29:14 +00001766 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1767 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001768 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001769 }
1770
Robert Sloan921ef2c2017-10-17 09:02:20 -07001771 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001772}
1773
1774
Robert Sloana27a6a42017-09-05 08:39:28 -07001775// EC point formats.
1776//
1777// https://tools.ietf.org/html/rfc4492#section-5.1.2
Kenny Rootb8494592015-09-25 02:29:14 +00001778
Robert Sloan921ef2c2017-10-17 09:02:20 -07001779static bool ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
Kenny Rootb8494592015-09-25 02:29:14 +00001780 CBB contents, formats;
1781 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1782 !CBB_add_u16_length_prefixed(out, &contents) ||
1783 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1784 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
1785 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001786 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001787 }
1788
Robert Sloan921ef2c2017-10-17 09:02:20 -07001789 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001790}
1791
Robert Sloan921ef2c2017-10-17 09:02:20 -07001792static bool ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Srinivas Paladugudd42a612019-08-09 19:30:39 +00001793 // The point format extension is unneccessary in TLS 1.3.
Robert Sloane56da3e2017-06-26 08:26:42 -07001794 if (hs->min_version >= TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001795 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001796 }
1797
David Benjamin1b249672016-12-06 18:25:50 -05001798 return ext_ec_point_add_extension(hs, out);
Kenny Rootb8494592015-09-25 02:29:14 +00001799}
1800
Robert Sloan921ef2c2017-10-17 09:02:20 -07001801static bool ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1802 CBS *contents) {
Kenny Rootb8494592015-09-25 02:29:14 +00001803 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001804 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001805 }
1806
Robert Sloan921ef2c2017-10-17 09:02:20 -07001807 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
1808 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04001809 }
1810
Kenny Rootb8494592015-09-25 02:29:14 +00001811 CBS ec_point_format_list;
1812 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1813 CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001814 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001815 }
1816
Robert Sloana27a6a42017-09-05 08:39:28 -07001817 // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1818 // point format.
Robert Sloan69939df2017-01-09 10:53:07 -08001819 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1820 TLSEXT_ECPOINTFORMAT_uncompressed,
1821 CBS_len(&ec_point_format_list)) == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001822 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001823 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001824 }
1825
Robert Sloan921ef2c2017-10-17 09:02:20 -07001826 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001827}
1828
Robert Sloan921ef2c2017-10-17 09:02:20 -07001829static bool ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +00001830 CBS *contents) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001831 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
1832 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04001833 }
1834
David Benjamin1b249672016-12-06 18:25:50 -05001835 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Kenny Rootb8494592015-09-25 02:29:14 +00001836}
1837
Robert Sloan921ef2c2017-10-17 09:02:20 -07001838static bool ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001839 SSL *const ssl = hs->ssl;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001840 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1841 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04001842 }
1843
Robert Sloana94fe052017-02-21 08:49:28 -08001844 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1845 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001846 const bool using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Kenny Rootb8494592015-09-25 02:29:14 +00001847
1848 if (!using_ecc) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001849 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001850 }
1851
David Benjamin1b249672016-12-06 18:25:50 -05001852 return ext_ec_point_add_extension(hs, out);
Kenny Rootb8494592015-09-25 02:29:14 +00001853}
1854
Steven Valdez909b19f2016-11-21 15:35:44 -05001855
Robert Sloana27a6a42017-09-05 08:39:28 -07001856// Pre Shared Key
1857//
Robert Sloand9e572d2018-08-27 12:27:00 -07001858// https://tools.ietf.org/html/rfc8446#section-4.2.11
Steven Valdez909b19f2016-11-21 15:35:44 -05001859
David Benjamin1b249672016-12-06 18:25:50 -05001860static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1861 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001862 if (hs->max_version < TLS1_3_VERSION || ssl->session == nullptr ||
1863 ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001864 return 0;
1865 }
1866
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001867 size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session.get()));
1868 return 15 + ssl->session->ticket.size() + binder_len;
Steven Valdez909b19f2016-11-21 15:35:44 -05001869}
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001870
Robert Sloan921ef2c2017-10-17 09:02:20 -07001871static bool ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001872 SSL *const ssl = hs->ssl;
Robert Sloanb1b54b82017-11-06 13:50:02 -08001873 hs->needs_psk_binder = false;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001874 if (hs->max_version < TLS1_3_VERSION || ssl->session == nullptr ||
1875 ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001876 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001877 }
1878
Robert Sloand9e572d2018-08-27 12:27:00 -07001879 // Per RFC 8446 section 4.1.4, skip offering the session if the selected
1880 // cipher in HelloRetryRequest does not match. This avoids performing the
1881 // transcript hash transformation for multiple hashes.
Robert Sloanb1b54b82017-11-06 13:50:02 -08001882 if (hs->received_hello_retry_request &&
Robert Sloanb1b54b82017-11-06 13:50:02 -08001883 ssl->session->cipher->algorithm_prf != hs->new_cipher->algorithm_prf) {
1884 return true;
1885 }
1886
Robert Sloan7d422bc2017-03-06 10:04:29 -08001887 struct OPENSSL_timeval now;
Steven Valdez909b19f2016-11-21 15:35:44 -05001888 ssl_get_current_time(ssl, &now);
1889 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1890 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1891
Robert Sloana27a6a42017-09-05 08:39:28 -07001892 // Fill in a placeholder zero binder of the appropriate length. It will be
1893 // computed and filled in later after length prefixes are computed.
Steven Valdez909b19f2016-11-21 15:35:44 -05001894 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001895 size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session.get()));
Steven Valdez909b19f2016-11-21 15:35:44 -05001896
1897 CBB contents, identity, ticket, binders, binder;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001898 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1899 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001900 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001901 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001902 !CBB_add_bytes(&ticket, ssl->session->ticket.data(),
1903 ssl->session->ticket.size()) ||
Steven Valdez909b19f2016-11-21 15:35:44 -05001904 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1905 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1906 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1907 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001908 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001909 }
1910
Robert Sloana27a6a42017-09-05 08:39:28 -07001911 hs->needs_psk_binder = true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001912 return CBB_flush(out);
1913}
1914
Robert Sloan921ef2c2017-10-17 09:02:20 -07001915bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1916 uint8_t *out_alert,
1917 CBS *contents) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001918 uint16_t psk_id;
1919 if (!CBS_get_u16(contents, &psk_id) ||
1920 CBS_len(contents) != 0) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001921 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001922 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001923 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001924 }
1925
Robert Sloana27a6a42017-09-05 08:39:28 -07001926 // We only advertise one PSK identity, so the only legal index is zero.
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001927 if (psk_id != 0) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001928 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001929 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001930 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001931 }
1932
Robert Sloan921ef2c2017-10-17 09:02:20 -07001933 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001934}
1935
Robert Sloan921ef2c2017-10-17 09:02:20 -07001936bool ssl_ext_pre_shared_key_parse_clienthello(
Robert Sloan1c9db532017-03-13 08:03:59 -07001937 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
1938 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001939 // We only process the first PSK identity since we don't support pure PSK.
Robert Sloan1c9db532017-03-13 08:03:59 -07001940 CBS identities, binders;
David Benjamin1b249672016-12-06 18:25:50 -05001941 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
Robert Sloan1c9db532017-03-13 08:03:59 -07001942 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
1943 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdez909b19f2016-11-21 15:35:44 -05001944 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjamin1b249672016-12-06 18:25:50 -05001945 CBS_len(&binders) == 0 ||
Steven Valdez909b19f2016-11-21 15:35:44 -05001946 CBS_len(contents) != 0) {
1947 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001948 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001949 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001950 }
1951
Steven Valdez909b19f2016-11-21 15:35:44 -05001952 *out_binders = binders;
1953
Robert Sloana27a6a42017-09-05 08:39:28 -07001954 // Check the syntax of the remaining identities, but do not process them.
David Benjamin1b249672016-12-06 18:25:50 -05001955 size_t num_identities = 1;
1956 while (CBS_len(&identities) != 0) {
1957 CBS unused_ticket;
1958 uint32_t unused_obfuscated_ticket_age;
1959 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1960 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1961 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1962 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001963 return false;
David Benjamin1b249672016-12-06 18:25:50 -05001964 }
1965
1966 num_identities++;
1967 }
1968
Robert Sloana27a6a42017-09-05 08:39:28 -07001969 // Check the syntax of the binders. The value will be checked later if
1970 // resuming.
David Benjamin1b249672016-12-06 18:25:50 -05001971 size_t num_binders = 0;
1972 while (CBS_len(&binders) != 0) {
1973 CBS binder;
1974 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1975 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1976 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001977 return false;
David Benjamin1b249672016-12-06 18:25:50 -05001978 }
1979
1980 num_binders++;
1981 }
1982
1983 if (num_identities != num_binders) {
1984 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1985 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001986 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001987 }
1988
Robert Sloan921ef2c2017-10-17 09:02:20 -07001989 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001990}
1991
Robert Sloan921ef2c2017-10-17 09:02:20 -07001992bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001993 if (!hs->ssl->s3->session_reused) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001994 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001995 }
1996
1997 CBB contents;
1998 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1999 !CBB_add_u16_length_prefixed(out, &contents) ||
Robert Sloana27a6a42017-09-05 08:39:28 -07002000 // We only consider the first identity for resumption
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002001 !CBB_add_u16(&contents, 0) ||
2002 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002003 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002004 }
2005
Robert Sloan921ef2c2017-10-17 09:02:20 -07002006 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002007}
2008
2009
Robert Sloana27a6a42017-09-05 08:39:28 -07002010// Pre-Shared Key Exchange Modes
2011//
Robert Sloand9e572d2018-08-27 12:27:00 -07002012// https://tools.ietf.org/html/rfc8446#section-4.2.9
David Benjamin1b249672016-12-06 18:25:50 -05002013
Robert Sloan921ef2c2017-10-17 09:02:20 -07002014static bool ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2015 CBB *out) {
Robert Sloane56da3e2017-06-26 08:26:42 -07002016 if (hs->max_version < TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002017 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05002018 }
2019
2020 CBB contents, ke_modes;
2021 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2022 !CBB_add_u16_length_prefixed(out, &contents) ||
2023 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2024 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002025 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05002026 }
2027
2028 return CBB_flush(out);
2029}
2030
Robert Sloan921ef2c2017-10-17 09:02:20 -07002031static bool ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
2032 uint8_t *out_alert,
2033 CBS *contents) {
Steven Valdez909b19f2016-11-21 15:35:44 -05002034 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002035 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05002036 }
2037
2038 CBS ke_modes;
2039 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2040 CBS_len(&ke_modes) == 0 ||
2041 CBS_len(contents) != 0) {
2042 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002043 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05002044 }
2045
Robert Sloana27a6a42017-09-05 08:39:28 -07002046 // We only support tickets with PSK_DHE_KE.
Robert Sloan69939df2017-01-09 10:53:07 -08002047 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2048 CBS_len(&ke_modes)) != NULL;
Steven Valdez909b19f2016-11-21 15:35:44 -05002049
Robert Sloan921ef2c2017-10-17 09:02:20 -07002050 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05002051}
2052
2053
Robert Sloana27a6a42017-09-05 08:39:28 -07002054// Early Data Indication
2055//
Robert Sloand9e572d2018-08-27 12:27:00 -07002056// https://tools.ietf.org/html/rfc8446#section-4.2.10
David Benjamin1b249672016-12-06 18:25:50 -05002057
Robert Sloan921ef2c2017-10-17 09:02:20 -07002058static bool ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002059 SSL *const ssl = hs->ssl;
Srinivas Paladugudd42a612019-08-09 19:30:39 +00002060 if (!ssl->enable_early_data ||
2061 // Session must be 0-RTT capable.
2062 ssl->session == nullptr ||
2063 ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION ||
2064 ssl->session->ticket_max_early_data == 0 ||
2065 // The second ClientHello never offers early data.
2066 hs->received_hello_retry_request ||
2067 // In case ALPN preferences changed since this session was established,
2068 // avoid reporting a confusing value in |SSL_get0_alpn_selected|.
2069 (!ssl->session->early_alpn.empty() &&
2070 !ssl_is_alpn_protocol_allowed(hs, ssl->session->early_alpn))) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002071 return true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002072 }
2073
Robert Sloana27a6a42017-09-05 08:39:28 -07002074 hs->early_data_offered = true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002075
2076 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2077 !CBB_add_u16(out, 0) ||
2078 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002079 return false;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002080 }
2081
Robert Sloan921ef2c2017-10-17 09:02:20 -07002082 return true;
David Benjamin1b249672016-12-06 18:25:50 -05002083}
2084
Robert Sloan921ef2c2017-10-17 09:02:20 -07002085static bool ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
Srinivas Paladugudd42a612019-08-09 19:30:39 +00002086 uint8_t *out_alert, CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05002087 SSL *const ssl = hs->ssl;
2088 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002089 return true;
David Benjamin1b249672016-12-06 18:25:50 -05002090 }
2091
2092 if (CBS_len(contents) != 0) {
2093 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002094 return false;
David Benjamin1b249672016-12-06 18:25:50 -05002095 }
2096
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002097 if (!ssl->s3->session_reused) {
2098 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2099 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002100 return false;
David Benjamin1b249672016-12-06 18:25:50 -05002101 }
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002102
Robert Sloan0da43952018-01-03 15:13:14 -08002103 ssl->s3->early_data_accepted = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002104 return true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002105}
2106
Robert Sloan921ef2c2017-10-17 09:02:20 -07002107static bool ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2108 uint8_t *out_alert, CBS *contents) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002109 SSL *const ssl = hs->ssl;
2110 if (contents == NULL ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07002111 ssl_protocol_version(ssl) < TLS1_3_VERSION) {
2112 return true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002113 }
2114
2115 if (CBS_len(contents) != 0) {
2116 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002117 return false;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002118 }
2119
Robert Sloana27a6a42017-09-05 08:39:28 -07002120 hs->early_data_offered = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002121 return true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002122}
2123
Robert Sloan921ef2c2017-10-17 09:02:20 -07002124static bool ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloan0da43952018-01-03 15:13:14 -08002125 if (!hs->ssl->s3->early_data_accepted) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002126 return true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002127 }
2128
2129 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2130 !CBB_add_u16(out, 0) ||
2131 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002132 return false;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002133 }
2134
Robert Sloan921ef2c2017-10-17 09:02:20 -07002135 return true;
David Benjamin1b249672016-12-06 18:25:50 -05002136}
2137
2138
Robert Sloana27a6a42017-09-05 08:39:28 -07002139// Key Share
2140//
Robert Sloand9e572d2018-08-27 12:27:00 -07002141// https://tools.ietf.org/html/rfc8446#section-4.2.8
David Benjaminc895d6b2016-08-11 13:26:41 -04002142
Robert Sloan921ef2c2017-10-17 09:02:20 -07002143static bool ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05002144 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -07002145 if (hs->max_version < TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002146 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04002147 }
2148
2149 CBB contents, kse_bytes;
Robert Sloan8542c082018-02-05 09:07:34 -08002150 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002151 !CBB_add_u16_length_prefixed(out, &contents) ||
2152 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002153 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002154 }
2155
David Benjamin1b249672016-12-06 18:25:50 -05002156 uint16_t group_id = hs->retry_group;
Robert Sloan11c28bd2018-12-17 12:09:20 -08002157 uint16_t second_group_id = 0;
David Benjamin1b249672016-12-06 18:25:50 -05002158 if (hs->received_hello_retry_request) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002159 // We received a HelloRetryRequest without a new curve, so there is no new
2160 // share to append. Leave |hs->key_share| as-is.
Steven Valdez909b19f2016-11-21 15:35:44 -05002161 if (group_id == 0 &&
Robert Sloan4562e9d2017-10-02 10:26:51 -07002162 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(),
2163 hs->key_share_bytes.size())) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002164 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002165 }
Robert Sloan4562e9d2017-10-02 10:26:51 -07002166 hs->key_share_bytes.Reset();
David Benjamin95add822016-10-19 01:09:12 -04002167 if (group_id == 0) {
2168 return CBB_flush(out);
2169 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002170 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -07002171 // Add a fake group. See draft-davidben-tls-grease-01.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002172 if (ssl->ctx->grease_enabled &&
2173 (!CBB_add_u16(&kse_bytes,
Robert Sloan309a31e2018-01-29 10:22:47 -08002174 ssl_get_grease_value(hs, ssl_grease_group)) ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002175 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2176 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002177 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002178 }
2179
Robert Sloana27a6a42017-09-05 08:39:28 -07002180 // Predict the most preferred group.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002181 Span<const uint16_t> groups = tls1_get_grouplist(hs);
Robert Sloanae1abf92017-10-05 12:50:08 -07002182 if (groups.empty()) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04002183 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002184 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002185 }
David Benjamin7c0d06c2016-08-11 13:26:41 -04002186
2187 group_id = groups[0];
Robert Sloan11c28bd2018-12-17 12:09:20 -08002188
Srinivas Paladugudd42a612019-08-09 19:30:39 +00002189 if (group_id == SSL_CURVE_CECPQ2 && groups.size() >= 2) {
2190 // CECPQ2 is not sent as the only initial key share. We'll include the
Robert Sloan11c28bd2018-12-17 12:09:20 -08002191 // 2nd preference group too to avoid round-trips.
2192 second_group_id = groups[1];
2193 assert(second_group_id != group_id);
2194 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002195 }
2196
David Benjamin7c0d06c2016-08-11 13:26:41 -04002197 CBB key_exchange;
Robert Sloan11c28bd2018-12-17 12:09:20 -08002198 hs->key_shares[0] = SSLKeyShare::Create(group_id);
2199 if (!hs->key_shares[0] ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07002200 !CBB_add_u16(&kse_bytes, group_id) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04002201 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
Robert Sloan11c28bd2018-12-17 12:09:20 -08002202 !hs->key_shares[0]->Offer(&key_exchange) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04002203 !CBB_flush(&kse_bytes)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002204 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002205 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002206
Robert Sloan11c28bd2018-12-17 12:09:20 -08002207 if (second_group_id != 0) {
2208 hs->key_shares[1] = SSLKeyShare::Create(second_group_id);
2209 if (!hs->key_shares[1] ||
2210 !CBB_add_u16(&kse_bytes, second_group_id) ||
2211 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2212 !hs->key_shares[1]->Offer(&key_exchange) ||
2213 !CBB_flush(&kse_bytes)) {
2214 return false;
2215 }
2216 }
2217
2218 // Save the contents of the extension to repeat it in the second
2219 // ClientHello.
Robert Sloan4562e9d2017-10-02 10:26:51 -07002220 if (!hs->received_hello_retry_request &&
2221 !hs->key_share_bytes.CopyFrom(
2222 MakeConstSpan(CBB_data(&kse_bytes), CBB_len(&kse_bytes)))) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002223 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002224 }
2225
2226 return CBB_flush(out);
2227}
2228
Robert Sloan921ef2c2017-10-17 09:02:20 -07002229bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
2230 Array<uint8_t> *out_secret,
2231 uint8_t *out_alert, CBS *contents) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002232 CBS peer_key;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002233 uint16_t group_id;
2234 if (!CBS_get_u16(contents, &group_id) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002235 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2236 CBS_len(contents) != 0) {
Robert Sloan99319a12017-11-27 10:32:46 -08002237 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04002238 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002239 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002240 }
2241
Robert Sloan11c28bd2018-12-17 12:09:20 -08002242 SSLKeyShare *key_share = hs->key_shares[0].get();
2243 if (key_share->GroupID() != group_id) {
2244 if (!hs->key_shares[1] || hs->key_shares[1]->GroupID() != group_id) {
2245 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2246 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2247 return false;
2248 }
2249 key_share = hs->key_shares[1].get();
David Benjaminc895d6b2016-08-11 13:26:41 -04002250 }
2251
Robert Sloan11c28bd2018-12-17 12:09:20 -08002252 if (!key_share->Finish(out_secret, out_alert, peer_key)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002253 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002254 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002255 }
2256
Robert Sloana94fe052017-02-21 08:49:28 -08002257 hs->new_session->group_id = group_id;
Robert Sloan11c28bd2018-12-17 12:09:20 -08002258 hs->key_shares[0].reset();
2259 hs->key_shares[1].reset();
Robert Sloan921ef2c2017-10-17 09:02:20 -07002260 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04002261}
2262
Robert Sloan921ef2c2017-10-17 09:02:20 -07002263bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
2264 Array<uint8_t> *out_secret,
2265 uint8_t *out_alert, CBS *contents) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002266 uint16_t group_id;
2267 CBS key_shares;
David Benjamin1b249672016-12-06 18:25:50 -05002268 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002269 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2270 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002271 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002272 }
2273
2274 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002275 CBS_len(contents) != 0) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04002276 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002277 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002278 }
2279
Robert Sloana27a6a42017-09-05 08:39:28 -07002280 // Find the corresponding key share.
David Benjamin7c0d06c2016-08-11 13:26:41 -04002281 CBS peer_key;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002282 CBS_init(&peer_key, NULL, 0);
David Benjaminc895d6b2016-08-11 13:26:41 -04002283 while (CBS_len(&key_shares) > 0) {
2284 uint16_t id;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002285 CBS peer_key_tmp;
David Benjaminc895d6b2016-08-11 13:26:41 -04002286 if (!CBS_get_u16(&key_shares, &id) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07002287 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp) ||
2288 CBS_len(&peer_key_tmp) == 0) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04002289 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002290 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002291 }
2292
David Benjamin7c0d06c2016-08-11 13:26:41 -04002293 if (id == group_id) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002294 if (CBS_len(&peer_key) != 0) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04002295 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2296 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002297 return false;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002298 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002299
David Benjamin7c0d06c2016-08-11 13:26:41 -04002300 peer_key = peer_key_tmp;
Robert Sloana27a6a42017-09-05 08:39:28 -07002301 // Continue parsing the structure to keep peers honest.
David Benjaminc895d6b2016-08-11 13:26:41 -04002302 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002303 }
2304
Robert Sloan921ef2c2017-10-17 09:02:20 -07002305 if (CBS_len(&peer_key) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002306 *out_found = false;
Robert Sloan4562e9d2017-10-02 10:26:51 -07002307 out_secret->Reset();
Robert Sloan921ef2c2017-10-17 09:02:20 -07002308 return true;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002309 }
2310
Robert Sloana27a6a42017-09-05 08:39:28 -07002311 // Compute the DH secret.
Robert Sloan4562e9d2017-10-02 10:26:51 -07002312 Array<uint8_t> secret;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002313 ScopedCBB public_key;
2314 UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
2315 if (!key_share ||
2316 !CBB_init(public_key.get(), 32) ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07002317 !key_share->Accept(public_key.get(), &secret, out_alert, peer_key) ||
2318 !CBBFinishArray(public_key.get(), &hs->ecdh_public_key)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002319 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002320 return false;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002321 }
2322
Robert Sloan4562e9d2017-10-02 10:26:51 -07002323 *out_secret = std::move(secret);
Robert Sloana27a6a42017-09-05 08:39:28 -07002324 *out_found = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002325 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04002326}
2327
Robert Sloan921ef2c2017-10-17 09:02:20 -07002328bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002329 uint16_t group_id;
2330 CBB kse_bytes, public_key;
David Benjamin1b249672016-12-06 18:25:50 -05002331 if (!tls1_get_shared_group(hs, &group_id) ||
Robert Sloan8542c082018-02-05 09:07:34 -08002332 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002333 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2334 !CBB_add_u16(&kse_bytes, group_id) ||
2335 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07002336 !CBB_add_bytes(&public_key, hs->ecdh_public_key.data(),
2337 hs->ecdh_public_key.size()) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002338 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002339 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002340 }
2341
Robert Sloan4562e9d2017-10-02 10:26:51 -07002342 hs->ecdh_public_key.Reset();
David Benjamin7c0d06c2016-08-11 13:26:41 -04002343
Robert Sloana94fe052017-02-21 08:49:28 -08002344 hs->new_session->group_id = group_id;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002345 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04002346}
2347
2348
Robert Sloana27a6a42017-09-05 08:39:28 -07002349// Supported Versions
2350//
Robert Sloand9e572d2018-08-27 12:27:00 -07002351// https://tools.ietf.org/html/rfc8446#section-4.2.1
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002352
Robert Sloan921ef2c2017-10-17 09:02:20 -07002353static bool ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05002354 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -07002355 if (hs->max_version <= TLS1_2_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002356 return true;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002357 }
2358
2359 CBB contents, versions;
2360 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2361 !CBB_add_u16_length_prefixed(out, &contents) ||
2362 !CBB_add_u8_length_prefixed(&contents, &versions)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002363 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002364 }
2365
Robert Sloana27a6a42017-09-05 08:39:28 -07002366 // Add a fake version. See draft-davidben-tls-grease-01.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002367 if (ssl->ctx->grease_enabled &&
Robert Sloan309a31e2018-01-29 10:22:47 -08002368 !CBB_add_u16(&versions, ssl_get_grease_value(hs, ssl_grease_version))) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002369 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002370 }
2371
Robert Sloanf6200e72017-07-10 08:09:18 -07002372 if (!ssl_add_supported_versions(hs, &versions) ||
2373 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002374 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002375 }
2376
Robert Sloan921ef2c2017-10-17 09:02:20 -07002377 return true;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002378}
2379
2380
Robert Sloana27a6a42017-09-05 08:39:28 -07002381// Cookie
2382//
Robert Sloand9e572d2018-08-27 12:27:00 -07002383// https://tools.ietf.org/html/rfc8446#section-4.2.2
David Benjamin95add822016-10-19 01:09:12 -04002384
Robert Sloan921ef2c2017-10-17 09:02:20 -07002385static bool ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloanae1abf92017-10-05 12:50:08 -07002386 if (hs->cookie.empty()) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002387 return true;
David Benjamin95add822016-10-19 01:09:12 -04002388 }
2389
2390 CBB contents, cookie;
2391 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2392 !CBB_add_u16_length_prefixed(out, &contents) ||
2393 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07002394 !CBB_add_bytes(&cookie, hs->cookie.data(), hs->cookie.size()) ||
David Benjamin95add822016-10-19 01:09:12 -04002395 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002396 return false;
David Benjamin95add822016-10-19 01:09:12 -04002397 }
2398
Robert Sloana27a6a42017-09-05 08:39:28 -07002399 // The cookie is no longer needed in memory.
Robert Sloan4562e9d2017-10-02 10:26:51 -07002400 hs->cookie.Reset();
Robert Sloan921ef2c2017-10-17 09:02:20 -07002401 return true;
David Benjamin95add822016-10-19 01:09:12 -04002402}
2403
2404
Robert Sloand9e572d2018-08-27 12:27:00 -07002405// Supported Groups
Robert Sloan0db7f542018-01-16 15:48:33 -08002406//
Robert Sloand9e572d2018-08-27 12:27:00 -07002407// https://tools.ietf.org/html/rfc4492#section-5.1.1
2408// https://tools.ietf.org/html/rfc8446#section-4.2.7
Kenny Rootb8494592015-09-25 02:29:14 +00002409
Robert Sloan921ef2c2017-10-17 09:02:20 -07002410static bool ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05002411 SSL *const ssl = hs->ssl;
David Benjamind316cba2016-06-02 16:17:39 -04002412 CBB contents, groups_bytes;
2413 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Kenny Rootb8494592015-09-25 02:29:14 +00002414 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamind316cba2016-06-02 16:17:39 -04002415 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002416 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00002417 }
2418
Robert Sloana27a6a42017-09-05 08:39:28 -07002419 // Add a fake group. See draft-davidben-tls-grease-01.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002420 if (ssl->ctx->grease_enabled &&
2421 !CBB_add_u16(&groups_bytes,
Robert Sloan309a31e2018-01-29 10:22:47 -08002422 ssl_get_grease_value(hs, ssl_grease_group))) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002423 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002424 }
2425
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002426 for (uint16_t group : tls1_get_grouplist(hs)) {
Srinivas Paladugudd42a612019-08-09 19:30:39 +00002427 if (group == SSL_CURVE_CECPQ2 &&
Robert Sloan11c28bd2018-12-17 12:09:20 -08002428 hs->max_version < TLS1_3_VERSION) {
2429 continue;
2430 }
Robert Sloan4562e9d2017-10-02 10:26:51 -07002431 if (!CBB_add_u16(&groups_bytes, group)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002432 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00002433 }
2434 }
2435
2436 return CBB_flush(out);
2437}
2438
Robert Sloan921ef2c2017-10-17 09:02:20 -07002439static bool ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2440 uint8_t *out_alert,
2441 CBS *contents) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002442 // This extension is not expected to be echoed by servers in TLS 1.2, but some
2443 // BigIP servers send it nonetheless, so do not enforce this.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002444 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00002445}
2446
Robert Sloan4562e9d2017-10-02 10:26:51 -07002447static bool parse_u16_array(const CBS *cbs, Array<uint16_t> *out) {
2448 CBS copy = *cbs;
2449 if ((CBS_len(&copy) & 1) != 0) {
2450 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2451 return false;
2452 }
2453
2454 Array<uint16_t> ret;
2455 if (!ret.Init(CBS_len(&copy) / 2)) {
2456 return false;
2457 }
2458 for (size_t i = 0; i < ret.size(); i++) {
2459 if (!CBS_get_u16(&copy, &ret[i])) {
2460 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2461 return false;
2462 }
2463 }
2464
2465 assert(CBS_len(&copy) == 0);
2466 *out = std::move(ret);
2467 return 1;
2468}
2469
Robert Sloan921ef2c2017-10-17 09:02:20 -07002470static bool ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin1b249672016-12-06 18:25:50 -05002471 uint8_t *out_alert,
Robert Sloan921ef2c2017-10-17 09:02:20 -07002472 CBS *contents) {
Kenny Rootb8494592015-09-25 02:29:14 +00002473 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002474 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00002475 }
2476
David Benjamind316cba2016-06-02 16:17:39 -04002477 CBS supported_group_list;
2478 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2479 CBS_len(&supported_group_list) == 0 ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07002480 CBS_len(contents) != 0 ||
2481 !parse_u16_array(&supported_group_list, &hs->peer_supported_group_list)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002482 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00002483 }
2484
Robert Sloan921ef2c2017-10-17 09:02:20 -07002485 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00002486}
2487
Robert Sloan978112c2018-01-22 12:53:01 -08002488// Token Binding
2489//
2490// https://tools.ietf.org/html/draft-ietf-tokbind-negotiation-10
2491
2492// The Token Binding version number currently matches the draft number of
2493// draft-ietf-tokbind-protocol, and when published as an RFC it will be 0x0100.
2494// Since there are no wire changes to the protocol from draft 13 through the
2495// current draft (16), this implementation supports all versions in that range.
2496static uint16_t kTokenBindingMaxVersion = 16;
2497static uint16_t kTokenBindingMinVersion = 13;
2498
2499static bool ext_token_binding_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2500 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002501 if (hs->config->token_binding_params.empty() || SSL_is_dtls(ssl)) {
Robert Sloan978112c2018-01-22 12:53:01 -08002502 return true;
2503 }
2504
2505 CBB contents, params;
2506 if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) ||
2507 !CBB_add_u16_length_prefixed(out, &contents) ||
2508 !CBB_add_u16(&contents, kTokenBindingMaxVersion) ||
2509 !CBB_add_u8_length_prefixed(&contents, &params) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002510 !CBB_add_bytes(&params, hs->config->token_binding_params.data(),
2511 hs->config->token_binding_params.size()) ||
Robert Sloan978112c2018-01-22 12:53:01 -08002512 !CBB_flush(out)) {
2513 return false;
2514 }
2515
2516 return true;
2517}
2518
2519static bool ext_token_binding_parse_serverhello(SSL_HANDSHAKE *hs,
2520 uint8_t *out_alert,
2521 CBS *contents) {
2522 SSL *const ssl = hs->ssl;
2523 if (contents == nullptr) {
2524 return true;
2525 }
2526
2527 CBS params_list;
2528 uint16_t version;
2529 uint8_t param;
2530 if (!CBS_get_u16(contents, &version) ||
2531 !CBS_get_u8_length_prefixed(contents, &params_list) ||
2532 !CBS_get_u8(&params_list, &param) ||
2533 CBS_len(&params_list) > 0 ||
2534 CBS_len(contents) > 0) {
2535 *out_alert = SSL_AD_DECODE_ERROR;
2536 return false;
2537 }
2538
2539 // The server-negotiated version must be less than or equal to our version.
2540 if (version > kTokenBindingMaxVersion) {
2541 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2542 return false;
2543 }
2544
2545 // If the server-selected version is less than what we support, then Token
2546 // Binding wasn't negotiated (but the extension was parsed successfully).
2547 if (version < kTokenBindingMinVersion) {
2548 return true;
2549 }
2550
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002551 for (uint8_t config_param : hs->config->token_binding_params) {
2552 if (param == config_param) {
Robert Sloan15c0b352018-04-16 08:36:46 -07002553 ssl->s3->negotiated_token_binding_param = param;
2554 ssl->s3->token_binding_negotiated = true;
Robert Sloan978112c2018-01-22 12:53:01 -08002555 return true;
2556 }
2557 }
2558
2559 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2560 return false;
2561}
2562
2563// select_tb_param looks for the first token binding param in
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002564// |hs->ssl->token_binding_params| that is also in |params| and puts it in
2565// |hs->ssl->negotiated_token_binding_param|. It returns true if a token binding
Robert Sloan978112c2018-01-22 12:53:01 -08002566// param is found, and false otherwise.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002567static bool select_tb_param(SSL_HANDSHAKE *hs,
2568 Span<const uint8_t> peer_params) {
2569 for (uint8_t tb_param : hs->config->token_binding_params) {
Robert Sloan978112c2018-01-22 12:53:01 -08002570 for (uint8_t peer_param : peer_params) {
2571 if (tb_param == peer_param) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002572 hs->ssl->s3->negotiated_token_binding_param = tb_param;
Robert Sloan978112c2018-01-22 12:53:01 -08002573 return true;
2574 }
2575 }
2576 }
2577 return false;
2578}
2579
2580static bool ext_token_binding_parse_clienthello(SSL_HANDSHAKE *hs,
2581 uint8_t *out_alert,
2582 CBS *contents) {
2583 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002584 if (contents == nullptr || hs->config->token_binding_params.empty()) {
Robert Sloan978112c2018-01-22 12:53:01 -08002585 return true;
2586 }
2587
2588 CBS params;
2589 uint16_t version;
2590 if (!CBS_get_u16(contents, &version) ||
2591 !CBS_get_u8_length_prefixed(contents, &params) ||
2592 CBS_len(&params) == 0 ||
2593 CBS_len(contents) > 0) {
2594 *out_alert = SSL_AD_DECODE_ERROR;
2595 return false;
2596 }
2597
2598 // If the client-selected version is less than what we support, then Token
2599 // Binding wasn't negotiated (but the extension was parsed successfully).
2600 if (version < kTokenBindingMinVersion) {
2601 return true;
2602 }
2603
2604 // If the client-selected version is higher than we support, use our max
2605 // version. Otherwise, use the client's version.
2606 hs->negotiated_token_binding_version =
2607 std::min(version, kTokenBindingMaxVersion);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002608 if (!select_tb_param(hs, params)) {
Robert Sloan978112c2018-01-22 12:53:01 -08002609 return true;
2610 }
2611
Robert Sloan15c0b352018-04-16 08:36:46 -07002612 ssl->s3->token_binding_negotiated = true;
Robert Sloan978112c2018-01-22 12:53:01 -08002613 return true;
2614}
2615
2616static bool ext_token_binding_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2617 SSL *const ssl = hs->ssl;
2618
Robert Sloan15c0b352018-04-16 08:36:46 -07002619 if (!ssl->s3->token_binding_negotiated) {
Robert Sloan978112c2018-01-22 12:53:01 -08002620 return true;
2621 }
2622
2623 CBB contents, params;
2624 if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) ||
2625 !CBB_add_u16_length_prefixed(out, &contents) ||
2626 !CBB_add_u16(&contents, hs->negotiated_token_binding_version) ||
2627 !CBB_add_u8_length_prefixed(&contents, &params) ||
Robert Sloan15c0b352018-04-16 08:36:46 -07002628 !CBB_add_u8(&params, ssl->s3->negotiated_token_binding_param) ||
Robert Sloan978112c2018-01-22 12:53:01 -08002629 !CBB_flush(out)) {
2630 return false;
2631 }
2632
2633 return true;
2634}
Kenny Rootb8494592015-09-25 02:29:14 +00002635
Robert Sloan8542c082018-02-05 09:07:34 -08002636// QUIC Transport Parameters
2637
2638static bool ext_quic_transport_params_add_clienthello(SSL_HANDSHAKE *hs,
2639 CBB *out) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002640 if (hs->config->quic_transport_params.empty() ||
2641 hs->max_version <= TLS1_2_VERSION) {
Robert Sloan8542c082018-02-05 09:07:34 -08002642 return true;
2643 }
2644
2645 CBB contents;
2646 if (!CBB_add_u16(out, TLSEXT_TYPE_quic_transport_parameters) ||
2647 !CBB_add_u16_length_prefixed(out, &contents) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002648 !CBB_add_bytes(&contents, hs->config->quic_transport_params.data(),
2649 hs->config->quic_transport_params.size()) ||
Robert Sloan8542c082018-02-05 09:07:34 -08002650 !CBB_flush(out)) {
2651 return false;
2652 }
2653 return true;
2654}
2655
2656static bool ext_quic_transport_params_parse_serverhello(SSL_HANDSHAKE *hs,
2657 uint8_t *out_alert,
2658 CBS *contents) {
2659 SSL *const ssl = hs->ssl;
2660 if (contents == nullptr) {
2661 return true;
2662 }
2663 // QUIC requires TLS 1.3.
2664 if (ssl_protocol_version(ssl) < TLS1_3_VERSION) {
2665 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2666 return false;
2667 }
2668
2669 return ssl->s3->peer_quic_transport_params.CopyFrom(*contents);
2670}
2671
2672static bool ext_quic_transport_params_parse_clienthello(SSL_HANDSHAKE *hs,
2673 uint8_t *out_alert,
2674 CBS *contents) {
2675 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002676 if (!contents || hs->config->quic_transport_params.empty()) {
Robert Sloan8542c082018-02-05 09:07:34 -08002677 return true;
2678 }
2679 // Ignore the extension before TLS 1.3.
2680 if (ssl_protocol_version(ssl) < TLS1_3_VERSION) {
2681 return true;
2682 }
2683
2684 return ssl->s3->peer_quic_transport_params.CopyFrom(*contents);
2685}
2686
2687static bool ext_quic_transport_params_add_serverhello(SSL_HANDSHAKE *hs,
2688 CBB *out) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002689 if (hs->config->quic_transport_params.empty()) {
Robert Sloan8542c082018-02-05 09:07:34 -08002690 return true;
2691 }
2692
2693 CBB contents;
2694 if (!CBB_add_u16(out, TLSEXT_TYPE_quic_transport_parameters) ||
2695 !CBB_add_u16_length_prefixed(out, &contents) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002696 !CBB_add_bytes(&contents, hs->config->quic_transport_params.data(),
2697 hs->config->quic_transport_params.size()) ||
Robert Sloan8542c082018-02-05 09:07:34 -08002698 !CBB_flush(out)) {
2699 return false;
2700 }
2701
2702 return true;
2703}
2704
Robert Sloan4c22c5f2019-03-01 15:53:37 -08002705// Delegated credentials.
2706//
2707// https://tools.ietf.org/html/draft-ietf-tls-subcerts
2708
2709static bool ext_delegated_credential_add_clienthello(SSL_HANDSHAKE *hs,
2710 CBB *out) {
2711 return true;
2712}
2713
2714static bool ext_delegated_credential_parse_clienthello(SSL_HANDSHAKE *hs,
2715 uint8_t *out_alert,
2716 CBS *contents) {
2717 assert(TLSEXT_TYPE_delegated_credential == 0xff02);
2718 // TODO: Check that the extension is empty.
2719 //
Robert Sloan4726ed32019-04-08 12:43:32 -07002720 // As of draft-03, the client sends an empty extension in order indicate
Robert Sloan4c22c5f2019-03-01 15:53:37 -08002721 // support for delegated credentials. This could change, however, since the
2722 // spec is not yet finalized. This assertion is here to remind us to enforce
2723 // this check once the extension ID is assigned.
2724
2725 if (contents == nullptr || ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) {
2726 // Don't use delegated credentials unless we're negotiating TLS 1.3 or
2727 // higher.
2728 return true;
2729 }
2730
2731 hs->delegated_credential_requested = true;
2732 return true;
2733}
2734
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002735// Certificate compression
2736
2737static bool cert_compression_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2738 bool first = true;
2739 CBB contents, algs;
2740
2741 for (const auto *alg : hs->ssl->ctx->cert_compression_algs.get()) {
2742 if (alg->decompress == nullptr) {
2743 continue;
2744 }
2745
2746 if (first && (!CBB_add_u16(out, TLSEXT_TYPE_cert_compression) ||
2747 !CBB_add_u16_length_prefixed(out, &contents) ||
2748 !CBB_add_u8_length_prefixed(&contents, &algs))) {
2749 return false;
2750 }
2751 first = false;
2752 if (!CBB_add_u16(&algs, alg->alg_id)) {
2753 return false;
2754 }
2755 }
2756
2757 return first || CBB_flush(out);
2758}
2759
2760static bool cert_compression_parse_serverhello(SSL_HANDSHAKE *hs,
2761 uint8_t *out_alert,
2762 CBS *contents) {
2763 if (contents == nullptr) {
2764 return true;
2765 }
2766
2767 // The server may not echo this extension. Any server to client negotiation is
2768 // advertised in the CertificateRequest message.
2769 return false;
2770}
2771
2772static bool cert_compression_parse_clienthello(SSL_HANDSHAKE *hs,
2773 uint8_t *out_alert,
2774 CBS *contents) {
2775 if (contents == nullptr) {
2776 return true;
2777 }
2778
2779 const size_t num_algs =
2780 sk_CertCompressionAlg_num(hs->ssl->ctx->cert_compression_algs.get());
2781
2782 CBS alg_ids;
2783 if (!CBS_get_u8_length_prefixed(contents, &alg_ids) ||
2784 CBS_len(contents) != 0 ||
2785 CBS_len(&alg_ids) == 0 ||
2786 CBS_len(&alg_ids) % 2 == 1) {
2787 return false;
2788 }
2789
2790 const size_t num_given_alg_ids = CBS_len(&alg_ids) / 2;
2791 Array<uint16_t> given_alg_ids;
2792 if (!given_alg_ids.Init(num_given_alg_ids)) {
2793 return false;
2794 }
2795
2796 size_t best_index = num_algs;
2797 size_t given_alg_idx = 0;
2798
2799 while (CBS_len(&alg_ids) > 0) {
2800 uint16_t alg_id;
2801 if (!CBS_get_u16(&alg_ids, &alg_id)) {
2802 return false;
2803 }
2804
2805 given_alg_ids[given_alg_idx++] = alg_id;
2806
2807 for (size_t i = 0; i < num_algs; i++) {
2808 const auto *alg = sk_CertCompressionAlg_value(
2809 hs->ssl->ctx->cert_compression_algs.get(), i);
2810 if (alg->alg_id == alg_id && alg->compress != nullptr) {
2811 if (i < best_index) {
2812 best_index = i;
2813 }
2814 break;
2815 }
2816 }
2817 }
2818
2819 qsort(given_alg_ids.data(), given_alg_ids.size(), sizeof(uint16_t),
2820 compare_uint16_t);
2821 for (size_t i = 1; i < num_given_alg_ids; i++) {
2822 if (given_alg_ids[i - 1] == given_alg_ids[i]) {
2823 return false;
2824 }
2825 }
2826
2827 if (best_index < num_algs &&
2828 ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
2829 hs->cert_compression_negotiated = true;
2830 hs->cert_compression_alg_id =
2831 sk_CertCompressionAlg_value(hs->ssl->ctx->cert_compression_algs.get(),
2832 best_index)
2833 ->alg_id;
2834 }
2835
2836 return true;
2837}
2838
2839static bool cert_compression_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2840 return true;
2841}
Robert Sloan8542c082018-02-05 09:07:34 -08002842
Robert Sloana27a6a42017-09-05 08:39:28 -07002843// kExtensions contains all the supported extensions.
Kenny Rootb8494592015-09-25 02:29:14 +00002844static const struct tls_extension kExtensions[] = {
2845 {
Kenny Rootb8494592015-09-25 02:29:14 +00002846 TLSEXT_TYPE_server_name,
David Benjamin95add822016-10-19 01:09:12 -04002847 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002848 ext_sni_add_clienthello,
2849 ext_sni_parse_serverhello,
2850 ext_sni_parse_clienthello,
2851 ext_sni_add_serverhello,
2852 },
2853 {
2854 TLSEXT_TYPE_extended_master_secret,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002855 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002856 ext_ems_add_clienthello,
2857 ext_ems_parse_serverhello,
2858 ext_ems_parse_clienthello,
2859 ext_ems_add_serverhello,
2860 },
2861 {
Robert Sloan726e9d12018-09-11 11:45:04 -07002862 TLSEXT_TYPE_renegotiate,
2863 NULL,
2864 ext_ri_add_clienthello,
2865 ext_ri_parse_serverhello,
2866 ext_ri_parse_clienthello,
2867 ext_ri_add_serverhello,
2868 },
2869 {
2870 TLSEXT_TYPE_supported_groups,
2871 NULL,
2872 ext_supported_groups_add_clienthello,
2873 ext_supported_groups_parse_serverhello,
2874 ext_supported_groups_parse_clienthello,
2875 dont_add_serverhello,
2876 },
2877 {
2878 TLSEXT_TYPE_ec_point_formats,
2879 NULL,
2880 ext_ec_point_add_clienthello,
2881 ext_ec_point_parse_serverhello,
2882 ext_ec_point_parse_clienthello,
2883 ext_ec_point_add_serverhello,
2884 },
2885 {
Kenny Rootb8494592015-09-25 02:29:14 +00002886 TLSEXT_TYPE_session_ticket,
2887 NULL,
2888 ext_ticket_add_clienthello,
2889 ext_ticket_parse_serverhello,
Robert Sloana27a6a42017-09-05 08:39:28 -07002890 // Ticket extension client parsing is handled in ssl_session.c
David Benjaminc895d6b2016-08-11 13:26:41 -04002891 ignore_parse_clienthello,
Kenny Rootb8494592015-09-25 02:29:14 +00002892 ext_ticket_add_serverhello,
2893 },
2894 {
Robert Sloan726e9d12018-09-11 11:45:04 -07002895 TLSEXT_TYPE_application_layer_protocol_negotiation,
2896 NULL,
2897 ext_alpn_add_clienthello,
2898 ext_alpn_parse_serverhello,
2899 // ALPN is negotiated late in |ssl_negotiate_alpn|.
2900 ignore_parse_clienthello,
2901 ext_alpn_add_serverhello,
2902 },
2903 {
2904 TLSEXT_TYPE_status_request,
2905 NULL,
2906 ext_ocsp_add_clienthello,
2907 ext_ocsp_parse_serverhello,
2908 ext_ocsp_parse_clienthello,
2909 ext_ocsp_add_serverhello,
2910 },
2911 {
Kenny Rootb8494592015-09-25 02:29:14 +00002912 TLSEXT_TYPE_signature_algorithms,
2913 NULL,
2914 ext_sigalgs_add_clienthello,
David Benjaminc895d6b2016-08-11 13:26:41 -04002915 forbid_parse_serverhello,
Kenny Rootb8494592015-09-25 02:29:14 +00002916 ext_sigalgs_parse_clienthello,
David Benjaminc895d6b2016-08-11 13:26:41 -04002917 dont_add_serverhello,
Kenny Rootb8494592015-09-25 02:29:14 +00002918 },
2919 {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07002920 TLSEXT_TYPE_signature_algorithms_cert,
2921 NULL,
2922 ext_sigalgs_cert_add_clienthello,
2923 forbid_parse_serverhello,
2924 ignore_parse_clienthello,
2925 dont_add_serverhello,
2926 },
2927 {
Kenny Rootb8494592015-09-25 02:29:14 +00002928 TLSEXT_TYPE_next_proto_neg,
David Benjamin95add822016-10-19 01:09:12 -04002929 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002930 ext_npn_add_clienthello,
2931 ext_npn_parse_serverhello,
2932 ext_npn_parse_clienthello,
2933 ext_npn_add_serverhello,
2934 },
2935 {
2936 TLSEXT_TYPE_certificate_timestamp,
2937 NULL,
2938 ext_sct_add_clienthello,
2939 ext_sct_parse_serverhello,
2940 ext_sct_parse_clienthello,
2941 ext_sct_add_serverhello,
2942 },
2943 {
Kenny Rootb8494592015-09-25 02:29:14 +00002944 TLSEXT_TYPE_channel_id,
2945 ext_channel_id_init,
2946 ext_channel_id_add_clienthello,
2947 ext_channel_id_parse_serverhello,
2948 ext_channel_id_parse_clienthello,
2949 ext_channel_id_add_serverhello,
2950 },
2951 {
2952 TLSEXT_TYPE_srtp,
2953 ext_srtp_init,
2954 ext_srtp_add_clienthello,
2955 ext_srtp_parse_serverhello,
2956 ext_srtp_parse_clienthello,
2957 ext_srtp_add_serverhello,
2958 },
2959 {
Robert Sloan8542c082018-02-05 09:07:34 -08002960 TLSEXT_TYPE_key_share,
David Benjaminc895d6b2016-08-11 13:26:41 -04002961 NULL,
2962 ext_key_share_add_clienthello,
2963 forbid_parse_serverhello,
2964 ignore_parse_clienthello,
2965 dont_add_serverhello,
2966 },
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002967 {
Steven Valdez909b19f2016-11-21 15:35:44 -05002968 TLSEXT_TYPE_psk_key_exchange_modes,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002969 NULL,
Steven Valdez909b19f2016-11-21 15:35:44 -05002970 ext_psk_key_exchange_modes_add_clienthello,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002971 forbid_parse_serverhello,
Steven Valdez909b19f2016-11-21 15:35:44 -05002972 ext_psk_key_exchange_modes_parse_clienthello,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002973 dont_add_serverhello,
2974 },
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002975 {
David Benjamin1b249672016-12-06 18:25:50 -05002976 TLSEXT_TYPE_early_data,
2977 NULL,
2978 ext_early_data_add_clienthello,
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002979 ext_early_data_parse_serverhello,
David Benjamin1b249672016-12-06 18:25:50 -05002980 ext_early_data_parse_clienthello,
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002981 ext_early_data_add_serverhello,
David Benjamin1b249672016-12-06 18:25:50 -05002982 },
2983 {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002984 TLSEXT_TYPE_supported_versions,
2985 NULL,
2986 ext_supported_versions_add_clienthello,
2987 forbid_parse_serverhello,
2988 ignore_parse_clienthello,
2989 dont_add_serverhello,
2990 },
David Benjamin95add822016-10-19 01:09:12 -04002991 {
2992 TLSEXT_TYPE_cookie,
2993 NULL,
2994 ext_cookie_add_clienthello,
2995 forbid_parse_serverhello,
2996 ignore_parse_clienthello,
2997 dont_add_serverhello,
2998 },
Robert Sloan0db7f542018-01-16 15:48:33 -08002999 {
Robert Sloan8542c082018-02-05 09:07:34 -08003000 TLSEXT_TYPE_quic_transport_parameters,
3001 NULL,
3002 ext_quic_transport_params_add_clienthello,
3003 ext_quic_transport_params_parse_serverhello,
3004 ext_quic_transport_params_parse_clienthello,
3005 ext_quic_transport_params_add_serverhello,
3006 },
Kenny Rootb8494592015-09-25 02:29:14 +00003007 {
Robert Sloan978112c2018-01-22 12:53:01 -08003008 TLSEXT_TYPE_token_binding,
3009 NULL,
3010 ext_token_binding_add_clienthello,
3011 ext_token_binding_parse_serverhello,
3012 ext_token_binding_parse_clienthello,
3013 ext_token_binding_add_serverhello,
3014 },
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003015 {
3016 TLSEXT_TYPE_cert_compression,
3017 NULL,
3018 cert_compression_add_clienthello,
3019 cert_compression_parse_serverhello,
3020 cert_compression_parse_clienthello,
3021 cert_compression_add_serverhello,
3022 },
Robert Sloan4c22c5f2019-03-01 15:53:37 -08003023 {
3024 TLSEXT_TYPE_delegated_credential,
3025 NULL,
3026 ext_delegated_credential_add_clienthello,
3027 forbid_parse_serverhello,
3028 ext_delegated_credential_parse_clienthello,
3029 dont_add_serverhello,
3030 },
Kenny Rootb8494592015-09-25 02:29:14 +00003031};
3032
3033#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
3034
Robert Sloana12bf462017-07-17 07:08:26 -07003035static_assert(kNumExtensions <=
3036 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
3037 "too many extensions for sent bitset");
3038static_assert(kNumExtensions <=
3039 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
3040 "too many extensions for received bitset");
Kenny Rootb8494592015-09-25 02:29:14 +00003041
3042static const struct tls_extension *tls_extension_find(uint32_t *out_index,
3043 uint16_t value) {
3044 unsigned i;
3045 for (i = 0; i < kNumExtensions; i++) {
3046 if (kExtensions[i].value == value) {
3047 *out_index = i;
3048 return &kExtensions[i];
3049 }
3050 }
3051
3052 return NULL;
3053}
3054
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003055bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out,
3056 size_t header_len) {
David Benjamin1b249672016-12-06 18:25:50 -05003057 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -08003058 CBB extensions;
3059 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Robert Sloana12bf462017-07-17 07:08:26 -07003060 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003061 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003062 }
3063
David Benjamin1b249672016-12-06 18:25:50 -05003064 hs->extensions.sent = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003065
David Benjamin7c0d06c2016-08-11 13:26:41 -04003066 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00003067 if (kExtensions[i].init != NULL) {
David Benjamin1b249672016-12-06 18:25:50 -05003068 kExtensions[i].init(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003069 }
3070 }
3071
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003072 uint16_t grease_ext1 = 0;
3073 if (ssl->ctx->grease_enabled) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003074 // Add a fake empty extension. See draft-davidben-tls-grease-01.
Robert Sloan309a31e2018-01-29 10:22:47 -08003075 grease_ext1 = ssl_get_grease_value(hs, ssl_grease_extension1);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003076 if (!CBB_add_u16(&extensions, grease_ext1) ||
3077 !CBB_add_u16(&extensions, 0 /* zero length */)) {
Robert Sloana12bf462017-07-17 07:08:26 -07003078 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003079 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003080 }
3081 }
3082
Robert Sloand9e572d2018-08-27 12:27:00 -07003083 bool last_was_empty = false;
David Benjamin7c0d06c2016-08-11 13:26:41 -04003084 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00003085 const size_t len_before = CBB_len(&extensions);
David Benjamin1b249672016-12-06 18:25:50 -05003086 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003087 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003088 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003089 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003090 }
Kenny Rootb8494592015-09-25 02:29:14 +00003091
Robert Sloand9e572d2018-08-27 12:27:00 -07003092 const size_t bytes_written = CBB_len(&extensions) - len_before;
3093 if (bytes_written != 0) {
David Benjamin1b249672016-12-06 18:25:50 -05003094 hs->extensions.sent |= (1u << i);
Kenny Rootb8494592015-09-25 02:29:14 +00003095 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003096 // If the difference in lengths is only four bytes then the extension had
3097 // an empty body.
3098 last_was_empty = (bytes_written == 4);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003099 }
3100
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003101 if (ssl->ctx->grease_enabled) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003102 // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
Robert Sloan309a31e2018-01-29 10:22:47 -08003103 uint16_t grease_ext2 = ssl_get_grease_value(hs, ssl_grease_extension2);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003104
Robert Sloana27a6a42017-09-05 08:39:28 -07003105 // The two fake extensions must not have the same value. GREASE values are
3106 // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
3107 // one.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003108 if (grease_ext1 == grease_ext2) {
3109 grease_ext2 ^= 0x1010;
3110 }
3111
3112 if (!CBB_add_u16(&extensions, grease_ext2) ||
3113 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
3114 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
Robert Sloana12bf462017-07-17 07:08:26 -07003115 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003116 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003117 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003118
3119 last_was_empty = false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003120 }
3121
David Benjaminc895d6b2016-08-11 13:26:41 -04003122 if (!SSL_is_dtls(ssl)) {
David Benjamin1b249672016-12-06 18:25:50 -05003123 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdez909b19f2016-11-21 15:35:44 -05003124 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Robert Sloand9e572d2018-08-27 12:27:00 -07003125 size_t padding_len = 0;
3126
3127 // The final extension must be non-empty. WebSphere Application
3128 // Server 7.0 is intolerant to the last extension being zero-length. See
3129 // https://crbug.com/363583.
3130 if (last_was_empty && psk_extension_len == 0) {
3131 padding_len = 1;
3132 // The addition of the padding extension may push us into the F5 bug.
3133 header_len += 4 + padding_len;
3134 }
3135
3136 // Add padding to workaround bugs in F5 terminators. See RFC 7685.
3137 //
3138 // NB: because this code works out the length of all existing extensions
3139 // it MUST always appear last (save for any PSK extension).
Adam Langleyd9e397b2015-01-22 14:27:53 -08003140 if (header_len > 0xff && header_len < 0x200) {
Robert Sloand9e572d2018-08-27 12:27:00 -07003141 // If our calculations already included a padding extension, remove that
3142 // factor because we're about to change its length.
3143 if (padding_len != 0) {
3144 header_len -= 4 + padding_len;
3145 }
3146 padding_len = 0x200 - header_len;
Robert Sloana27a6a42017-09-05 08:39:28 -07003147 // Extensions take at least four bytes to encode. Always include at least
3148 // one byte of data if including the extension. WebSphere Application
3149 // Server 7.0 is intolerant to the last extension being zero-length. See
3150 // https://crbug.com/363583.
Adam Langleyd9e397b2015-01-22 14:27:53 -08003151 if (padding_len >= 4 + 1) {
3152 padding_len -= 4;
3153 } else {
3154 padding_len = 1;
3155 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003156 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003157
Robert Sloand9e572d2018-08-27 12:27:00 -07003158 if (padding_len != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00003159 uint8_t *padding_bytes;
3160 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
3161 !CBB_add_u16(&extensions, padding_len) ||
3162 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
Robert Sloana12bf462017-07-17 07:08:26 -07003163 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003164 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003165 }
3166
Robert Sloan69939df2017-01-09 10:53:07 -08003167 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003168 }
3169 }
3170
Robert Sloana27a6a42017-09-05 08:39:28 -07003171 // The PSK extension must be last, including after the padding.
David Benjamin1b249672016-12-06 18:25:50 -05003172 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Robert Sloana12bf462017-07-17 07:08:26 -07003173 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003174 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003175 }
3176
Robert Sloana27a6a42017-09-05 08:39:28 -07003177 // Discard empty extensions blocks.
Adam Langley4139edb2016-01-13 15:00:54 -08003178 if (CBB_len(&extensions) == 0) {
Kenny Roote99801b2015-11-06 15:31:15 -08003179 CBB_discard_child(out);
Kenny Rootb8494592015-09-25 02:29:14 +00003180 }
3181
Kenny Roote99801b2015-11-06 15:31:15 -08003182 return CBB_flush(out);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003183}
3184
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003185bool ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05003186 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -08003187 CBB extensions;
3188 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003189 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003190 }
3191
David Benjamin1b249672016-12-06 18:25:50 -05003192 for (unsigned i = 0; i < kNumExtensions; i++) {
3193 if (!(hs->extensions.received & (1u << i))) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003194 // Don't send extensions that were not received.
Kenny Rootb8494592015-09-25 02:29:14 +00003195 continue;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003196 }
3197
David Benjamin1b249672016-12-06 18:25:50 -05003198 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003199 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003200 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Kenny Rootb8494592015-09-25 02:29:14 +00003201 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003202 }
3203 }
3204
Robert Sloana27a6a42017-09-05 08:39:28 -07003205 // Discard empty extensions blocks before TLS 1.3.
Robert Sloan921ef2c2017-10-17 09:02:20 -07003206 if (ssl_protocol_version(ssl) < TLS1_3_VERSION &&
David Benjaminc895d6b2016-08-11 13:26:41 -04003207 CBB_len(&extensions) == 0) {
Kenny Roote99801b2015-11-06 15:31:15 -08003208 CBB_discard_child(out);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003209 }
3210
Kenny Roote99801b2015-11-06 15:31:15 -08003211 return CBB_flush(out);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003212
Kenny Rootb8494592015-09-25 02:29:14 +00003213err:
Kenny Rootb8494592015-09-25 02:29:14 +00003214 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003215 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003216}
3217
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003218static bool ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
3219 const SSL_CLIENT_HELLO *client_hello,
3220 int *out_alert) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003221 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00003222 if (kExtensions[i].init != NULL) {
David Benjamin1b249672016-12-06 18:25:50 -05003223 kExtensions[i].init(hs);
Kenny Rootb8494592015-09-25 02:29:14 +00003224 }
3225 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003226
David Benjamin1b249672016-12-06 18:25:50 -05003227 hs->extensions.received = 0;
David Benjaminc895d6b2016-08-11 13:26:41 -04003228 CBS extensions;
3229 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
3230 while (CBS_len(&extensions) != 0) {
3231 uint16_t type;
3232 CBS extension;
3233
Robert Sloana27a6a42017-09-05 08:39:28 -07003234 // Decode the next extension.
David Benjaminc895d6b2016-08-11 13:26:41 -04003235 if (!CBS_get_u16(&extensions, &type) ||
3236 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08003237 *out_alert = SSL_AD_DECODE_ERROR;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003238 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04003239 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003240
David Benjaminc895d6b2016-08-11 13:26:41 -04003241 unsigned ext_index;
3242 const struct tls_extension *const ext =
3243 tls_extension_find(&ext_index, type);
David Benjaminc895d6b2016-08-11 13:26:41 -04003244 if (ext == NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003245 continue;
3246 }
3247
David Benjamin1b249672016-12-06 18:25:50 -05003248 hs->extensions.received |= (1u << ext_index);
David Benjaminc895d6b2016-08-11 13:26:41 -04003249 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003250 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003251 *out_alert = alert;
3252 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003253 ERR_add_error_dataf("extension %u", (unsigned)type);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003254 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003255 }
3256 }
3257
David Benjaminc895d6b2016-08-11 13:26:41 -04003258 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin1b249672016-12-06 18:25:50 -05003259 if (hs->extensions.received & (1u << i)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003260 continue;
3261 }
3262
3263 CBS *contents = NULL, fake_contents;
3264 static const uint8_t kFakeRenegotiateExtension[] = {0};
3265 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
3266 ssl_client_cipher_list_contains_cipher(client_hello,
3267 SSL3_CK_SCSV & 0xffff)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003268 // The renegotiation SCSV was received so pretend that we received a
3269 // renegotiation extension.
David Benjaminc895d6b2016-08-11 13:26:41 -04003270 CBS_init(&fake_contents, kFakeRenegotiateExtension,
3271 sizeof(kFakeRenegotiateExtension));
3272 contents = &fake_contents;
David Benjamin1b249672016-12-06 18:25:50 -05003273 hs->extensions.received |= (1u << i);
David Benjaminc895d6b2016-08-11 13:26:41 -04003274 }
3275
Robert Sloana27a6a42017-09-05 08:39:28 -07003276 // Extension wasn't observed so call the callback with a NULL
3277 // parameter.
David Benjaminc895d6b2016-08-11 13:26:41 -04003278 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003279 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003280 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003281 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjaminc895d6b2016-08-11 13:26:41 -04003282 *out_alert = alert;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003283 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00003284 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003285 }
3286
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003287 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003288}
3289
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003290bool ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
3291 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin1b249672016-12-06 18:25:50 -05003292 SSL *const ssl = hs->ssl;
Robert Sloan5d625782017-02-13 09:55:39 -08003293 int alert = SSL_AD_DECODE_ERROR;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003294 if (!ssl_scan_clienthello_tlsext(hs, client_hello, &alert)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07003295 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003296 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003297 }
3298
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003299 if (!ssl_check_clienthello_tlsext(hs)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003300 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003301 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003302 }
3303
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003304 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003305}
3306
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003307static bool ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
3308 int *out_alert) {
David Benjamin1b249672016-12-06 18:25:50 -05003309 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07003310 // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty.
Robert Sloan921ef2c2017-10-17 09:02:20 -07003311 if (CBS_len(cbs) == 0 && ssl_protocol_version(ssl) < TLS1_3_VERSION) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003312 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04003313 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003314
Robert Sloana27a6a42017-09-05 08:39:28 -07003315 // Decode the extensions block and check it is valid.
David Benjaminc895d6b2016-08-11 13:26:41 -04003316 CBS extensions;
3317 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
3318 !tls1_check_duplicate_extensions(&extensions)) {
3319 *out_alert = SSL_AD_DECODE_ERROR;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003320 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04003321 }
3322
3323 uint32_t received = 0;
3324 while (CBS_len(&extensions) != 0) {
3325 uint16_t type;
3326 CBS extension;
3327
Robert Sloana27a6a42017-09-05 08:39:28 -07003328 // Decode the next extension.
David Benjaminc895d6b2016-08-11 13:26:41 -04003329 if (!CBS_get_u16(&extensions, &type) ||
3330 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Kenny Roota04d78d2015-09-25 00:26:37 +00003331 *out_alert = SSL_AD_DECODE_ERROR;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003332 return false;
Kenny Roota04d78d2015-09-25 00:26:37 +00003333 }
3334
David Benjaminc895d6b2016-08-11 13:26:41 -04003335 unsigned ext_index;
3336 const struct tls_extension *const ext =
3337 tls_extension_find(&ext_index, type);
Kenny Rootb8494592015-09-25 02:29:14 +00003338
David Benjaminc895d6b2016-08-11 13:26:41 -04003339 if (ext == NULL) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003340 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3341 ERR_add_error_dataf("extension %u", (unsigned)type);
3342 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
3343 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04003344 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003345
Robert Sloana12bf462017-07-17 07:08:26 -07003346 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
3347 "too many bits");
David Benjaminf31229b2017-01-25 14:08:15 -05003348
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003349 if (!(hs->extensions.sent & (1u << ext_index))) {
3350 // If the extension was never sent then it is illegal.
David Benjaminc895d6b2016-08-11 13:26:41 -04003351 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3352 ERR_add_error_dataf("extension :%u", (unsigned)type);
3353 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003354 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04003355 }
Kenny Rootb8494592015-09-25 02:29:14 +00003356
David Benjaminc895d6b2016-08-11 13:26:41 -04003357 received |= (1u << ext_index);
Kenny Roota04d78d2015-09-25 00:26:37 +00003358
David Benjaminc895d6b2016-08-11 13:26:41 -04003359 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003360 if (!ext->parse_serverhello(hs, &alert, &extension)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003361 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003362 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjaminc895d6b2016-08-11 13:26:41 -04003363 *out_alert = alert;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003364 return false;
Kenny Roota04d78d2015-09-25 00:26:37 +00003365 }
3366 }
3367
David Benjamin7c0d06c2016-08-11 13:26:41 -04003368 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00003369 if (!(received & (1u << i))) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003370 // Extension wasn't observed so call the callback with a NULL
3371 // parameter.
Kenny Rootb8494592015-09-25 02:29:14 +00003372 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003373 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003374 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003375 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Kenny Rootb8494592015-09-25 02:29:14 +00003376 *out_alert = alert;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003377 return false;
Kenny Roota04d78d2015-09-25 00:26:37 +00003378 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003379 }
3380 }
3381
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003382 return true;
Kenny Roota04d78d2015-09-25 00:26:37 +00003383}
3384
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003385static bool ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05003386 SSL *const ssl = hs->ssl;
Robert Sloan978112c2018-01-22 12:53:01 -08003387
Robert Sloan15c0b352018-04-16 08:36:46 -07003388 if (ssl->s3->token_binding_negotiated &&
Robert Sloan978112c2018-01-22 12:53:01 -08003389 !(SSL_get_secure_renegotiation_support(ssl) &&
3390 SSL_get_extms_support(ssl))) {
3391 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_TB_WITHOUT_EMS_OR_RI);
3392 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003393 return false;
Robert Sloan978112c2018-01-22 12:53:01 -08003394 }
3395
Adam Langleyd9e397b2015-01-22 14:27:53 -08003396 int ret = SSL_TLSEXT_ERR_NOACK;
3397 int al = SSL_AD_UNRECOGNIZED_NAME;
3398
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003399 if (ssl->ctx->servername_callback != 0) {
3400 ret = ssl->ctx->servername_callback(ssl, &al, ssl->ctx->servername_arg);
3401 } else if (ssl->session_ctx->servername_callback != 0) {
3402 ret = ssl->session_ctx->servername_callback(
3403 ssl, &al, ssl->session_ctx->servername_arg);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003404 }
3405
3406 switch (ret) {
3407 case SSL_TLSEXT_ERR_ALERT_FATAL:
Robert Sloan921ef2c2017-10-17 09:02:20 -07003408 ssl_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003409 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003410
Adam Langleyd9e397b2015-01-22 14:27:53 -08003411 case SSL_TLSEXT_ERR_NOACK:
Robert Sloana27a6a42017-09-05 08:39:28 -07003412 hs->should_ack_sni = false;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003413 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003414
3415 default:
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003416 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003417 }
3418}
3419
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003420bool ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
David Benjamin1b249672016-12-06 18:25:50 -05003421 SSL *const ssl = hs->ssl;
Robert Sloan5d625782017-02-13 09:55:39 -08003422 int alert = SSL_AD_DECODE_ERROR;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003423 if (!ssl_scan_serverhello_tlsext(hs, cbs, &alert)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07003424 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003425 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003426 }
3427
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003428 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003429}
3430
Robert Sloan8f860b12017-08-28 07:37:06 -07003431static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
Robert Sloand9e572d2018-08-27 12:27:00 -07003432 Array<uint8_t> *out, EVP_CIPHER_CTX *cipher_ctx, HMAC_CTX *hmac_ctx,
3433 Span<const uint8_t> ticket) {
Robert Sloan8f860b12017-08-28 07:37:06 -07003434 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003435
Robert Sloana27a6a42017-09-05 08:39:28 -07003436 // Check the MAC at the end of the ticket.
Kenny Rootb8494592015-09-25 02:29:14 +00003437 uint8_t mac[EVP_MAX_MD_SIZE];
Robert Sloan8f860b12017-08-28 07:37:06 -07003438 size_t mac_len = HMAC_size(hmac_ctx);
Robert Sloand9e572d2018-08-27 12:27:00 -07003439 if (ticket.size() < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003440 // The ticket must be large enough for key name, IV, data, and MAC.
Robert Sloana12bf462017-07-17 07:08:26 -07003441 return ssl_ticket_aead_ignore_ticket;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003442 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003443 // Split the ticket into the ticket and the MAC.
3444 auto ticket_mac = ticket.subspan(ticket.size() - mac_len);
3445 ticket = ticket.subspan(0, ticket.size() - mac_len);
3446 HMAC_Update(hmac_ctx, ticket.data(), ticket.size());
Robert Sloan8f860b12017-08-28 07:37:06 -07003447 HMAC_Final(hmac_ctx, mac, NULL);
Robert Sloand9e572d2018-08-27 12:27:00 -07003448 assert(mac_len == ticket_mac.size());
3449 bool mac_ok = CRYPTO_memcmp(mac, ticket_mac.data(), mac_len) == 0;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003450#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003451 mac_ok = true;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003452#endif
3453 if (!mac_ok) {
Robert Sloana12bf462017-07-17 07:08:26 -07003454 return ssl_ticket_aead_ignore_ticket;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003455 }
3456
Robert Sloana27a6a42017-09-05 08:39:28 -07003457 // Decrypt the session data.
Robert Sloand9e572d2018-08-27 12:27:00 -07003458 auto ciphertext = ticket.subspan(SSL_TICKET_KEY_NAME_LEN + iv_len);
3459 Array<uint8_t> plaintext;
3460#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3461 if (!plaintext.CopyFrom(ciphertext)) {
Robert Sloana12bf462017-07-17 07:08:26 -07003462 return ssl_ticket_aead_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003463 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003464#else
Robert Sloand9e572d2018-08-27 12:27:00 -07003465 if (ciphertext.size() >= INT_MAX) {
Robert Sloana12bf462017-07-17 07:08:26 -07003466 return ssl_ticket_aead_ignore_ticket;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003467 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003468 if (!plaintext.Init(ciphertext.size())) {
3469 return ssl_ticket_aead_error;
3470 }
Kenny Rootb8494592015-09-25 02:29:14 +00003471 int len1, len2;
Robert Sloand9e572d2018-08-27 12:27:00 -07003472 if (!EVP_DecryptUpdate(cipher_ctx, plaintext.data(), &len1, ciphertext.data(),
3473 (int)ciphertext.size()) ||
3474 !EVP_DecryptFinal_ex(cipher_ctx, plaintext.data() + len1, &len2)) {
Robert Sloan1c9db532017-03-13 08:03:59 -07003475 ERR_clear_error();
Robert Sloana12bf462017-07-17 07:08:26 -07003476 return ssl_ticket_aead_ignore_ticket;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003477 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003478 plaintext.Shrink(static_cast<size_t>(len1) + len2);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003479#endif
Adam Langleyd9e397b2015-01-22 14:27:53 -08003480
Robert Sloand9e572d2018-08-27 12:27:00 -07003481 *out = std::move(plaintext);
Robert Sloana12bf462017-07-17 07:08:26 -07003482 return ssl_ticket_aead_success;
Robert Sloan1c9db532017-03-13 08:03:59 -07003483}
3484
Robert Sloan8f860b12017-08-28 07:37:06 -07003485static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
Robert Sloand9e572d2018-08-27 12:27:00 -07003486 SSL_HANDSHAKE *hs, Array<uint8_t> *out, bool *out_renew_ticket,
3487 Span<const uint8_t> ticket) {
3488 assert(ticket.size() >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
Robert Sloan8f860b12017-08-28 07:37:06 -07003489 ScopedEVP_CIPHER_CTX cipher_ctx;
3490 ScopedHMAC_CTX hmac_ctx;
Robert Sloand9e572d2018-08-27 12:27:00 -07003491 auto name = ticket.subspan(0, SSL_TICKET_KEY_NAME_LEN);
3492 // The actual IV is shorter, but the length is determined by the callback's
3493 // chosen cipher. Instead we pass in |EVP_MAX_IV_LENGTH| worth of IV to ensure
3494 // the callback has enough.
3495 auto iv = ticket.subspan(SSL_TICKET_KEY_NAME_LEN, EVP_MAX_IV_LENGTH);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003496 int cb_ret = hs->ssl->session_ctx->ticket_key_cb(
Robert Sloand9e572d2018-08-27 12:27:00 -07003497 hs->ssl, const_cast<uint8_t *>(name.data()),
3498 const_cast<uint8_t *>(iv.data()), cipher_ctx.get(), hmac_ctx.get(),
3499 0 /* decrypt */);
Robert Sloan8f860b12017-08-28 07:37:06 -07003500 if (cb_ret < 0) {
3501 return ssl_ticket_aead_error;
3502 } else if (cb_ret == 0) {
3503 return ssl_ticket_aead_ignore_ticket;
3504 } else if (cb_ret == 2) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003505 *out_renew_ticket = true;
Robert Sloan8f860b12017-08-28 07:37:06 -07003506 } else {
3507 assert(cb_ret == 1);
3508 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003509 return decrypt_ticket_with_cipher_ctx(out, cipher_ctx.get(), hmac_ctx.get(),
3510 ticket);
Robert Sloan8f860b12017-08-28 07:37:06 -07003511}
3512
3513static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
Robert Sloand9e572d2018-08-27 12:27:00 -07003514 SSL_HANDSHAKE *hs, Array<uint8_t> *out, Span<const uint8_t> ticket) {
3515 assert(ticket.size() >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003516 SSL_CTX *ctx = hs->ssl->session_ctx.get();
Robert Sloan8f860b12017-08-28 07:37:06 -07003517
Robert Sloana27a6a42017-09-05 08:39:28 -07003518 // Rotate the ticket key if necessary.
Robert Sloan8f860b12017-08-28 07:37:06 -07003519 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
3520 return ssl_ticket_aead_error;
3521 }
3522
Robert Sloand9e572d2018-08-27 12:27:00 -07003523 const EVP_CIPHER *cipher = EVP_aes_128_cbc();
3524 auto name = ticket.subspan(0, SSL_TICKET_KEY_NAME_LEN);
3525 auto iv =
3526 ticket.subspan(SSL_TICKET_KEY_NAME_LEN, EVP_CIPHER_iv_length(cipher));
3527
Robert Sloana27a6a42017-09-05 08:39:28 -07003528 // Pick the matching ticket key and decrypt.
Robert Sloan8f860b12017-08-28 07:37:06 -07003529 ScopedEVP_CIPHER_CTX cipher_ctx;
3530 ScopedHMAC_CTX hmac_ctx;
3531 {
3532 MutexReadLock lock(&ctx->lock);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003533 const TicketKey *key;
Robert Sloand9e572d2018-08-27 12:27:00 -07003534 if (ctx->ticket_key_current && name == ctx->ticket_key_current->name) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003535 key = ctx->ticket_key_current.get();
Robert Sloand9e572d2018-08-27 12:27:00 -07003536 } else if (ctx->ticket_key_prev && name == ctx->ticket_key_prev->name) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003537 key = ctx->ticket_key_prev.get();
Robert Sloan8f860b12017-08-28 07:37:06 -07003538 } else {
3539 return ssl_ticket_aead_ignore_ticket;
3540 }
Robert Sloan8f860b12017-08-28 07:37:06 -07003541 if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
3542 tlsext_tick_md(), NULL) ||
Robert Sloand9e572d2018-08-27 12:27:00 -07003543 !EVP_DecryptInit_ex(cipher_ctx.get(), cipher, NULL,
3544 key->aes_key, iv.data())) {
Robert Sloan8f860b12017-08-28 07:37:06 -07003545 return ssl_ticket_aead_error;
3546 }
3547 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003548 return decrypt_ticket_with_cipher_ctx(out, cipher_ctx.get(), hmac_ctx.get(),
3549 ticket);
Robert Sloan8f860b12017-08-28 07:37:06 -07003550}
3551
Robert Sloan1c9db532017-03-13 08:03:59 -07003552static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
Robert Sloand9e572d2018-08-27 12:27:00 -07003553 SSL_HANDSHAKE *hs, Array<uint8_t> *out, bool *out_renew_ticket,
3554 Span<const uint8_t> ticket) {
3555 Array<uint8_t> plaintext;
3556 if (!plaintext.Init(ticket.size())) {
Robert Sloan1c9db532017-03-13 08:03:59 -07003557 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3558 return ssl_ticket_aead_error;
3559 }
3560
3561 size_t plaintext_len;
3562 const enum ssl_ticket_aead_result_t result =
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003563 hs->ssl->session_ctx->ticket_aead_method->open(
Robert Sloand9e572d2018-08-27 12:27:00 -07003564 hs->ssl, plaintext.data(), &plaintext_len, ticket.size(),
3565 ticket.data(), ticket.size());
3566 if (result != ssl_ticket_aead_success) {
3567 return result;
Robert Sloan1c9db532017-03-13 08:03:59 -07003568 }
3569
Robert Sloand9e572d2018-08-27 12:27:00 -07003570 plaintext.Shrink(plaintext_len);
3571 *out = std::move(plaintext);
3572 return ssl_ticket_aead_success;
Robert Sloan1c9db532017-03-13 08:03:59 -07003573}
3574
3575enum ssl_ticket_aead_result_t ssl_process_ticket(
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003576 SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session,
Robert Sloand9e572d2018-08-27 12:27:00 -07003577 bool *out_renew_ticket, Span<const uint8_t> ticket,
3578 Span<const uint8_t> session_id) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003579 *out_renew_ticket = false;
Robert Sloanfe7cd212017-08-07 09:03:39 -07003580 out_session->reset();
Robert Sloan1c9db532017-03-13 08:03:59 -07003581
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003582 if ((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) ||
Robert Sloand9e572d2018-08-27 12:27:00 -07003583 session_id.size() > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Robert Sloan1c9db532017-03-13 08:03:59 -07003584 return ssl_ticket_aead_ignore_ticket;
3585 }
3586
Robert Sloand9e572d2018-08-27 12:27:00 -07003587 Array<uint8_t> plaintext;
Robert Sloan1c9db532017-03-13 08:03:59 -07003588 enum ssl_ticket_aead_result_t result;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003589 if (hs->ssl->session_ctx->ticket_aead_method != NULL) {
Robert Sloand9e572d2018-08-27 12:27:00 -07003590 result = ssl_decrypt_ticket_with_method(hs, &plaintext, out_renew_ticket,
3591 ticket);
Robert Sloan1c9db532017-03-13 08:03:59 -07003592 } else {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003593 // Ensure there is room for the key name and the largest IV |ticket_key_cb|
3594 // may try to consume. The real limit may be lower, but the maximum IV
3595 // length should be well under the minimum size for the session material and
3596 // HMAC.
Robert Sloand9e572d2018-08-27 12:27:00 -07003597 if (ticket.size() < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
Robert Sloan8f860b12017-08-28 07:37:06 -07003598 return ssl_ticket_aead_ignore_ticket;
3599 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003600 if (hs->ssl->session_ctx->ticket_key_cb != NULL) {
Robert Sloand9e572d2018-08-27 12:27:00 -07003601 result =
3602 ssl_decrypt_ticket_with_cb(hs, &plaintext, out_renew_ticket, ticket);
Robert Sloan8f860b12017-08-28 07:37:06 -07003603 } else {
Robert Sloand9e572d2018-08-27 12:27:00 -07003604 result = ssl_decrypt_ticket_with_ticket_keys(hs, &plaintext, ticket);
Robert Sloan8f860b12017-08-28 07:37:06 -07003605 }
Robert Sloan1c9db532017-03-13 08:03:59 -07003606 }
3607
3608 if (result != ssl_ticket_aead_success) {
3609 return result;
3610 }
3611
Robert Sloana27a6a42017-09-05 08:39:28 -07003612 // Decode the session.
Robert Sloand9e572d2018-08-27 12:27:00 -07003613 UniquePtr<SSL_SESSION> session(SSL_SESSION_from_bytes(
3614 plaintext.data(), plaintext.size(), hs->ssl->ctx.get()));
Robert Sloanfe7cd212017-08-07 09:03:39 -07003615 if (!session) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003616 ERR_clear_error(); // Don't leave an error on the queue.
Robert Sloan1c9db532017-03-13 08:03:59 -07003617 return ssl_ticket_aead_ignore_ticket;
Kenny Rootb8494592015-09-25 02:29:14 +00003618 }
3619
Robert Sloana27a6a42017-09-05 08:39:28 -07003620 // Copy the client's session ID into the new session, to denote the ticket has
3621 // been accepted.
Robert Sloand9e572d2018-08-27 12:27:00 -07003622 OPENSSL_memcpy(session->session_id, session_id.data(), session_id.size());
3623 session->session_id_length = session_id.size();
Kenny Rootb8494592015-09-25 02:29:14 +00003624
Robert Sloanfe7cd212017-08-07 09:03:39 -07003625 *out_session = std::move(session);
Robert Sloan1c9db532017-03-13 08:03:59 -07003626 return ssl_ticket_aead_success;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003627}
3628
Robert Sloan309a31e2018-01-29 10:22:47 -08003629bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003630 // Extension ignored for inappropriate versions
Robert Sloan921ef2c2017-10-17 09:02:20 -07003631 if (ssl_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Robert Sloan309a31e2018-01-29 10:22:47 -08003632 return true;
Kenny Roota04d78d2015-09-25 00:26:37 +00003633 }
3634
Robert Sloan11c28bd2018-12-17 12:09:20 -08003635 // In all contexts, the signature algorithms list may not be empty. (It may be
3636 // omitted by clients in TLS 1.2, but then the entire extension is omitted.)
3637 return CBS_len(in_sigalgs) != 0 &&
3638 parse_u16_array(in_sigalgs, &hs->peer_sigalgs);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003639}
3640
Robert Sloan309a31e2018-01-29 10:22:47 -08003641bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
Robert Sloan2424d842017-05-01 07:46:28 -07003642 switch (EVP_PKEY_id(pkey)) {
3643 case EVP_PKEY_RSA:
3644 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
Robert Sloan309a31e2018-01-29 10:22:47 -08003645 return true;
Robert Sloan2424d842017-05-01 07:46:28 -07003646 case EVP_PKEY_EC:
3647 *out = SSL_SIGN_ECDSA_SHA1;
Robert Sloan309a31e2018-01-29 10:22:47 -08003648 return true;
Robert Sloan2424d842017-05-01 07:46:28 -07003649 default:
Robert Sloan309a31e2018-01-29 10:22:47 -08003650 return false;
Robert Sloan2424d842017-05-01 07:46:28 -07003651 }
3652}
3653
Robert Sloan309a31e2018-01-29 10:22:47 -08003654bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
David Benjamin1b249672016-12-06 18:25:50 -05003655 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003656 CERT *cert = hs->config->cert.get();
Robert Sloan4c22c5f2019-03-01 15:53:37 -08003657 DC *dc = cert->dc.get();
Adam Langleyd9e397b2015-01-22 14:27:53 -08003658
Robert Sloana27a6a42017-09-05 08:39:28 -07003659 // Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3660 // handshake.
Robert Sloan921ef2c2017-10-17 09:02:20 -07003661 if (ssl_protocol_version(ssl) < TLS1_2_VERSION) {
Robert Sloanb6d070c2017-07-24 08:40:01 -07003662 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
Robert Sloan2424d842017-05-01 07:46:28 -07003663 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
Robert Sloan309a31e2018-01-29 10:22:47 -08003664 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04003665 }
Robert Sloan309a31e2018-01-29 10:22:47 -08003666 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00003667 }
3668
Robert Sloan4562e9d2017-10-02 10:26:51 -07003669 Span<const uint16_t> sigalgs = kSignSignatureAlgorithms;
Robert Sloan4c22c5f2019-03-01 15:53:37 -08003670 if (ssl_signing_with_dc(hs)) {
3671 sigalgs = MakeConstSpan(&dc->expected_cert_verify_algorithm, 1);
3672 } else if (!cert->sigalgs.empty()) {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07003673 sigalgs = cert->sigalgs;
David Benjaminc895d6b2016-08-11 13:26:41 -04003674 }
Kenny Rootb8494592015-09-25 02:29:14 +00003675
Robert Sloan4c22c5f2019-03-01 15:53:37 -08003676 Span<const uint16_t> peer_sigalgs = tls1_get_peer_verify_algorithms(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -04003677
Robert Sloan4562e9d2017-10-02 10:26:51 -07003678 for (uint16_t sigalg : sigalgs) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003679 // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3680 // negotiated.
David Benjaminc895d6b2016-08-11 13:26:41 -04003681 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07003682 !ssl_private_key_supports_signature_algorithm(hs, sigalg)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003683 continue;
3684 }
3685
Robert Sloan4562e9d2017-10-02 10:26:51 -07003686 for (uint16_t peer_sigalg : peer_sigalgs) {
3687 if (sigalg == peer_sigalg) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003688 *out = sigalg;
Robert Sloan309a31e2018-01-29 10:22:47 -08003689 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04003690 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003691 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003692 }
3693
David Benjaminc895d6b2016-08-11 13:26:41 -04003694 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
Robert Sloan309a31e2018-01-29 10:22:47 -08003695 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003696}
3697
Robert Sloan4c22c5f2019-03-01 15:53:37 -08003698Span<const uint16_t> tls1_get_peer_verify_algorithms(const SSL_HANDSHAKE *hs) {
3699 Span<const uint16_t> peer_sigalgs = hs->peer_sigalgs;
3700 if (peer_sigalgs.empty() && ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) {
3701 // If the client didn't specify any signature_algorithms extension then
3702 // we can assume that it supports SHA1. See
3703 // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
3704 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3705 SSL_SIGN_ECDSA_SHA1};
3706 peer_sigalgs = kDefaultPeerAlgorithms;
3707 }
3708 return peer_sigalgs;
3709}
3710
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003711bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
Robert Sloan5d625782017-02-13 09:55:39 -08003712 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07003713 // A Channel ID handshake message is structured to contain multiple
3714 // extensions, but the only one that can be present is Channel ID.
Robert Sloan84377092017-08-14 09:33:19 -07003715 uint16_t extension_type;
3716 CBS channel_id = msg.body, extension;
Steven Valdez909b19f2016-11-21 15:35:44 -05003717 if (!CBS_get_u16(&channel_id, &extension_type) ||
3718 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3719 CBS_len(&channel_id) != 0 ||
3720 extension_type != TLSEXT_TYPE_channel_id ||
3721 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3722 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07003723 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003724 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003725 }
3726
Robert Sloanb6d070c2017-07-24 08:40:01 -07003727 UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Steven Valdez909b19f2016-11-21 15:35:44 -05003728 if (!p256) {
3729 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003730 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003731 }
3732
Robert Sloanb6d070c2017-07-24 08:40:01 -07003733 UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3734 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
Robert Sloana12bf462017-07-17 07:08:26 -07003735 if (!sig || !x || !y) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003736 return false;
Adam Langley1e4884f2015-09-24 10:57:52 -07003737 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003738
Steven Valdez909b19f2016-11-21 15:35:44 -05003739 const uint8_t *p = CBS_data(&extension);
Robert Sloana12bf462017-07-17 07:08:26 -07003740 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3741 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3742 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3743 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003744 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003745 }
3746
Robert Sloanb6d070c2017-07-24 08:40:01 -07003747 UniquePtr<EC_KEY> key(EC_KEY_new());
3748 UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
Robert Sloana12bf462017-07-17 07:08:26 -07003749 if (!key || !point ||
3750 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3751 y.get(), nullptr) ||
3752 !EC_KEY_set_group(key.get(), p256.get()) ||
3753 !EC_KEY_set_public_key(key.get(), point.get())) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003754 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003755 }
3756
3757 uint8_t digest[EVP_MAX_MD_SIZE];
3758 size_t digest_len;
Robert Sloan5d625782017-02-13 09:55:39 -08003759 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Robert Sloanb1b54b82017-11-06 13:50:02 -08003760 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003761 }
3762
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003763 bool sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
3764#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3765 sig_ok = true;
3766 ERR_clear_error();
3767#endif
3768 if (!sig_ok) {
3769 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3770 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3771 ssl->s3->channel_id_valid = false;
3772 return false;
3773 }
3774
3775 OPENSSL_memcpy(ssl->s3->channel_id, p, 64);
3776 return true;
3777}
3778
3779bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3780 uint8_t digest[EVP_MAX_MD_SIZE];
3781 size_t digest_len;
3782 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
3783 return false;
3784 }
3785
3786 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(hs->config->channel_id_private.get());
Robert Sloanb1b54b82017-11-06 13:50:02 -08003787 if (ec_key == nullptr) {
Steven Valdez909b19f2016-11-21 15:35:44 -05003788 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloanb1b54b82017-11-06 13:50:02 -08003789 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003790 }
3791
Robert Sloanb1b54b82017-11-06 13:50:02 -08003792 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
3793 if (!x || !y ||
Steven Valdez909b19f2016-11-21 15:35:44 -05003794 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3795 EC_KEY_get0_public_key(ec_key),
Robert Sloanb1b54b82017-11-06 13:50:02 -08003796 x.get(), y.get(), nullptr)) {
3797 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003798 }
3799
Robert Sloanb1b54b82017-11-06 13:50:02 -08003800 UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign(digest, digest_len, ec_key));
3801 if (!sig) {
3802 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003803 }
3804
3805 CBB child;
3806 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3807 !CBB_add_u16_length_prefixed(cbb, &child) ||
Robert Sloanb1b54b82017-11-06 13:50:02 -08003808 !BN_bn2cbb_padded(&child, 32, x.get()) ||
3809 !BN_bn2cbb_padded(&child, 32, y.get()) ||
Steven Valdez909b19f2016-11-21 15:35:44 -05003810 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3811 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3812 !CBB_flush(cbb)) {
Robert Sloanb1b54b82017-11-06 13:50:02 -08003813 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003814 }
3815
Robert Sloanb1b54b82017-11-06 13:50:02 -08003816 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05003817}
3818
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003819bool tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
Robert Sloan5d625782017-02-13 09:55:39 -08003820 SSL *const ssl = hs->ssl;
Robert Sloan921ef2c2017-10-17 09:02:20 -07003821 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
3822 Array<uint8_t> msg;
3823 if (!tls13_get_cert_verify_signature_input(hs, &msg,
Steven Valdez909b19f2016-11-21 15:35:44 -05003824 ssl_cert_verify_channel_id)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003825 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003826 }
Robert Sloan921ef2c2017-10-17 09:02:20 -07003827 SHA256(msg.data(), msg.size(), out);
Steven Valdez909b19f2016-11-21 15:35:44 -05003828 *out_len = SHA256_DIGEST_LENGTH;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003829 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05003830 }
3831
3832 SHA256_CTX ctx;
3833
3834 SHA256_Init(&ctx);
Kenny Rootb8494592015-09-25 02:29:14 +00003835 static const char kClientIDMagic[] = "TLS Channel ID signature";
Steven Valdez909b19f2016-11-21 15:35:44 -05003836 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
Kenny Rootb8494592015-09-25 02:29:14 +00003837
David Benjaminc895d6b2016-08-11 13:26:41 -04003838 if (ssl->session != NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00003839 static const char kResumptionMagic[] = "Resumption";
Steven Valdez909b19f2016-11-21 15:35:44 -05003840 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
Kenny Rootb8494592015-09-25 02:29:14 +00003841 if (ssl->session->original_handshake_hash_len == 0) {
3842 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003843 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00003844 }
Steven Valdez909b19f2016-11-21 15:35:44 -05003845 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3846 ssl->session->original_handshake_hash_len);
Kenny Rootb8494592015-09-25 02:29:14 +00003847 }
3848
Robert Sloan5d625782017-02-13 09:55:39 -08003849 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3850 size_t hs_hash_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -07003851 if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003852 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00003853 }
Robert Sloan5d625782017-02-13 09:55:39 -08003854 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Steven Valdez909b19f2016-11-21 15:35:44 -05003855 SHA256_Final(out, &ctx);
3856 *out_len = SHA256_DIGEST_LENGTH;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003857 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003858}
3859
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003860bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
Robert Sloan5d625782017-02-13 09:55:39 -08003861 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07003862 // This function should never be called for a resumed session because the
3863 // handshake hashes that we wish to record are for the original, full
3864 // handshake.
David Benjaminc895d6b2016-08-11 13:26:41 -04003865 if (ssl->session != NULL) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003866 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003867 }
3868
Robert Sloana12bf462017-07-17 07:08:26 -07003869 static_assert(
Robert Sloana94fe052017-02-21 08:49:28 -08003870 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
Robert Sloana12bf462017-07-17 07:08:26 -07003871 "original_handshake_hash is too small");
Robert Sloan5d625782017-02-13 09:55:39 -08003872
3873 size_t digest_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -07003874 if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
3875 &digest_len)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003876 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003877 }
3878
Robert Sloana12bf462017-07-17 07:08:26 -07003879 static_assert(EVP_MAX_MD_SIZE <= 0xff,
3880 "EVP_MAX_MD_SIZE does not fit in uint8_t");
Robert Sloana94fe052017-02-21 08:49:28 -08003881 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003882
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003883 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003884}
Steven Valdez909b19f2016-11-21 15:35:44 -05003885
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003886bool ssl_do_channel_id_callback(SSL_HANDSHAKE *hs) {
3887 if (hs->config->channel_id_private != NULL ||
3888 hs->ssl->ctx->channel_id_cb == NULL) {
3889 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05003890 }
3891
3892 EVP_PKEY *key = NULL;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003893 hs->ssl->ctx->channel_id_cb(hs->ssl, &key);
Steven Valdez909b19f2016-11-21 15:35:44 -05003894 if (key == NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003895 // The caller should try again later.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003896 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05003897 }
3898
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003899 UniquePtr<EVP_PKEY> free_key(key);
3900 return SSL_set1_tls_channel_id(hs->ssl, key);
Steven Valdez909b19f2016-11-21 15:35:44 -05003901}
3902
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003903bool ssl_is_sct_list_valid(const CBS *contents) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003904 // Shallow parse the SCT list for sanity. By the RFC
3905 // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3906 // of the SCTs may be empty.
Steven Valdez909b19f2016-11-21 15:35:44 -05003907 CBS copy = *contents;
3908 CBS sct_list;
3909 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3910 CBS_len(&copy) != 0 ||
3911 CBS_len(&sct_list) == 0) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003912 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003913 }
3914
3915 while (CBS_len(&sct_list) > 0) {
3916 CBS sct;
3917 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3918 CBS_len(&sct) == 0) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003919 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003920 }
3921 }
3922
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003923 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05003924}
Robert Sloanb6d070c2017-07-24 08:40:01 -07003925
Robert Sloan726e9d12018-09-11 11:45:04 -07003926BSSL_NAMESPACE_END
Robert Sloanb6d070c2017-07-24 08:40:01 -07003927
3928using namespace bssl;
3929
3930int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
3931 uint16_t extension_type,
3932 const uint8_t **out_data,
3933 size_t *out_len) {
3934 CBS cbs;
3935 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
3936 return 0;
3937 }
3938
3939 *out_data = CBS_data(&cbs);
3940 *out_len = CBS_len(&cbs);
3941 return 1;
3942}
3943
3944void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
3945 ctx->ed25519_enabled = !!enabled;
3946}
3947
Robert Sloan5cbb5c82018-04-24 11:35:46 -07003948void SSL_CTX_set_rsa_pss_rsae_certs_enabled(SSL_CTX *ctx, int enabled) {
3949 ctx->rsa_pss_rsae_certs_enabled = !!enabled;
3950}