blob: c1c41a8aa4bff65c34a82ce1f035c01a96b9dc59 [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
Pete Bentley0c61efe2019-08-13 09:32:23 +0100202static bool is_post_quantum_group(uint16_t id) {
203 return id == SSL_CURVE_CECPQ2 || id == SSL_CURVE_CECPQ2b;
204}
205
Robert Sloanf63bd1f2019-04-16 09:26:20 -0700206bool ssl_client_hello_init(const SSL *ssl, SSL_CLIENT_HELLO *out,
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100207 const SSLMessage &msg) {
Robert Sloan69939df2017-01-09 10:53:07 -0800208 OPENSSL_memset(out, 0, sizeof(*out));
Robert Sloanf63bd1f2019-04-16 09:26:20 -0700209 out->ssl = const_cast<SSL *>(ssl);
Robert Sloan84377092017-08-14 09:33:19 -0700210 out->client_hello = CBS_data(&msg.body);
211 out->client_hello_len = CBS_len(&msg.body);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800212
David Benjaminc895d6b2016-08-11 13:26:41 -0400213 CBS client_hello, random, session_id;
David Benjamin1b249672016-12-06 18:25:50 -0500214 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
215 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400216 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
217 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
218 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100219 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800220 }
221
David Benjamin1b249672016-12-06 18:25:50 -0500222 out->random = CBS_data(&random);
223 out->random_len = CBS_len(&random);
224 out->session_id = CBS_data(&session_id);
225 out->session_id_len = CBS_len(&session_id);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800226
Robert Sloana27a6a42017-09-05 08:39:28 -0700227 // Skip past DTLS cookie
David Benjamin1b249672016-12-06 18:25:50 -0500228 if (SSL_is_dtls(out->ssl)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800229 CBS cookie;
David Benjaminc895d6b2016-08-11 13:26:41 -0400230 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
231 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100232 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800233 }
234 }
235
David Benjaminc895d6b2016-08-11 13:26:41 -0400236 CBS cipher_suites, compression_methods;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800237 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400238 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
239 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -0800240 CBS_len(&compression_methods) < 1) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100241 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800242 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400243
David Benjamin1b249672016-12-06 18:25:50 -0500244 out->cipher_suites = CBS_data(&cipher_suites);
245 out->cipher_suites_len = CBS_len(&cipher_suites);
246 out->compression_methods = CBS_data(&compression_methods);
247 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800248
Robert Sloana27a6a42017-09-05 08:39:28 -0700249 // If the ClientHello ends here then it's valid, but doesn't have any
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100250 // extensions.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800251 if (CBS_len(&client_hello) == 0) {
David Benjamin1b249672016-12-06 18:25:50 -0500252 out->extensions = NULL;
253 out->extensions_len = 0;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100254 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800255 }
256
Robert Sloana27a6a42017-09-05 08:39:28 -0700257 // Extract extensions and check it is valid.
David Benjaminc895d6b2016-08-11 13:26:41 -0400258 CBS extensions;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800259 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
260 !tls1_check_duplicate_extensions(&extensions) ||
261 CBS_len(&client_hello) != 0) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100262 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800263 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400264
David Benjamin1b249672016-12-06 18:25:50 -0500265 out->extensions = CBS_data(&extensions);
266 out->extensions_len = CBS_len(&extensions);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800267
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100268 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800269}
270
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100271bool ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
272 CBS *out, uint16_t extension_type) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800273 CBS extensions;
David Benjamin1b249672016-12-06 18:25:50 -0500274 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800275 while (CBS_len(&extensions) != 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700276 // Decode the next extension.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800277 uint16_t type;
278 CBS extension;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800279 if (!CBS_get_u16(&extensions, &type) ||
280 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100281 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800282 }
283
284 if (type == extension_type) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400285 *out = extension;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100286 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800287 }
288 }
289
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100290 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800291}
292
David Benjamind316cba2016-06-02 16:17:39 -0400293static const uint16_t kDefaultGroups[] = {
David Benjaminc895d6b2016-08-11 13:26:41 -0400294 SSL_CURVE_X25519,
295 SSL_CURVE_SECP256R1,
296 SSL_CURVE_SECP384R1,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800297};
298
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100299Span<const uint16_t> tls1_get_grouplist(const SSL_HANDSHAKE *hs) {
300 if (!hs->config->supported_group_list.empty()) {
301 return hs->config->supported_group_list;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800302 }
Robert Sloan4562e9d2017-10-02 10:26:51 -0700303 return Span<const uint16_t>(kDefaultGroups);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800304}
305
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100306bool tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
David Benjamin1b249672016-12-06 18:25:50 -0500307 SSL *const ssl = hs->ssl;
David Benjamin95add822016-10-19 01:09:12 -0400308 assert(ssl->server);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800309
Robert Sloana27a6a42017-09-05 08:39:28 -0700310 // Clients are not required to send a supported_groups extension. In this
311 // case, the server is free to pick any group it likes. See RFC 4492,
312 // section 4, paragraph 3.
313 //
314 // However, in the interests of compatibility, we will skip ECDH if the
315 // client didn't send an extension because we can't be sure that they'll
316 // support our favoured group. Thus we do not special-case an emtpy
317 // |peer_supported_group_list|.
Adam Langleye9ada862015-05-11 17:20:37 -0700318
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100319 Span<const uint16_t> groups = tls1_get_grouplist(hs);
Robert Sloan4562e9d2017-10-02 10:26:51 -0700320 Span<const uint16_t> pref, supp;
Adam Langley4139edb2016-01-13 15:00:54 -0800321 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
David Benjamind316cba2016-06-02 16:17:39 -0400322 pref = groups;
David Benjamin1b249672016-12-06 18:25:50 -0500323 supp = hs->peer_supported_group_list;
Adam Langleye9ada862015-05-11 17:20:37 -0700324 } else {
David Benjamin1b249672016-12-06 18:25:50 -0500325 pref = hs->peer_supported_group_list;
David Benjamind316cba2016-06-02 16:17:39 -0400326 supp = groups;
Adam Langleye9ada862015-05-11 17:20:37 -0700327 }
328
Robert Sloan4562e9d2017-10-02 10:26:51 -0700329 for (uint16_t pref_group : pref) {
330 for (uint16_t supp_group : supp) {
Robert Sloan11c28bd2018-12-17 12:09:20 -0800331 if (pref_group == supp_group &&
Pete Bentley0c61efe2019-08-13 09:32:23 +0100332 // CECPQ2(b) doesn't fit in the u8-length-prefixed ECPoint field in
333 // TLS 1.2 and below.
Robert Sloan11c28bd2018-12-17 12:09:20 -0800334 (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
Pete Bentley0c61efe2019-08-13 09:32:23 +0100335 !is_post_quantum_group(pref_group))) {
Robert Sloan4562e9d2017-10-02 10:26:51 -0700336 *out_group_id = pref_group;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100337 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800338 }
339 }
340 }
341
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100342 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800343}
344
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100345bool tls1_set_curves(Array<uint16_t> *out_group_ids, Span<const int> curves) {
346 Array<uint16_t> group_ids;
347 if (!group_ids.Init(curves.size())) {
348 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800349 }
350
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100351 for (size_t i = 0; i < curves.size(); i++) {
David Benjamind316cba2016-06-02 16:17:39 -0400352 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100353 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800354 }
355 }
356
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100357 *out_group_ids = std::move(group_ids);
358 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800359}
360
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100361bool tls1_set_curves_list(Array<uint16_t> *out_group_ids, const char *curves) {
362 // Count the number of curves in the list.
363 size_t count = 0;
364 const char *ptr = curves, *col;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400365 do {
366 col = strchr(ptr, ':');
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100367 count++;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400368 if (col) {
369 ptr = col + 1;
370 }
371 } while (col);
372
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100373 Array<uint16_t> group_ids;
374 if (!group_ids.Init(count)) {
375 return false;
376 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400377
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100378 size_t i = 0;
379 ptr = curves;
380 do {
381 col = strchr(ptr, ':');
382 if (!ssl_name_to_group_id(&group_ids[i++], ptr,
383 col ? (size_t)(col - ptr) : strlen(ptr))) {
384 return false;
385 }
386 if (col) {
387 ptr = col + 1;
388 }
389 } while (col);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400390
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100391 assert(i == count);
392 *out_group_ids = std::move(group_ids);
393 return true;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400394}
395
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100396bool tls1_check_group_id(const SSL_HANDSHAKE *hs, uint16_t group_id) {
Pete Bentley0c61efe2019-08-13 09:32:23 +0100397 if (is_post_quantum_group(group_id) &&
Robert Sloan11c28bd2018-12-17 12:09:20 -0800398 ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) {
Pete Bentley0c61efe2019-08-13 09:32:23 +0100399 // CECPQ2(b) requires TLS 1.3.
Robert Sloan11c28bd2018-12-17 12:09:20 -0800400 return false;
401 }
402
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100403 for (uint16_t supported : tls1_get_grouplist(hs)) {
Robert Sloan4562e9d2017-10-02 10:26:51 -0700404 if (supported == group_id) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100405 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800406 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800407 }
408
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100409 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800410}
411
Robert Sloana27a6a42017-09-05 08:39:28 -0700412// kVerifySignatureAlgorithms is the default list of accepted signature
413// algorithms for verifying.
414//
415// For now, RSA-PSS signature algorithms are not enabled on Android's system
416// BoringSSL. Once the change in Chrome has stuck and the values are finalized,
417// restore them.
David Benjamin95add822016-10-19 01:09:12 -0400418static const uint16_t kVerifySignatureAlgorithms[] = {
Robert Sloana27a6a42017-09-05 08:39:28 -0700419 // List our preferred algorithms first.
Robert Sloan572a4e22017-04-17 10:52:19 -0700420 SSL_SIGN_ED25519,
Steven Valdez909b19f2016-11-21 15:35:44 -0500421 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700422 SSL_SIGN_RSA_PSS_RSAE_SHA256,
Steven Valdez909b19f2016-11-21 15:35:44 -0500423 SSL_SIGN_RSA_PKCS1_SHA256,
424
Robert Sloana27a6a42017-09-05 08:39:28 -0700425 // Larger hashes are acceptable.
Steven Valdez909b19f2016-11-21 15:35:44 -0500426 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700427 SSL_SIGN_RSA_PSS_RSAE_SHA384,
Steven Valdez909b19f2016-11-21 15:35:44 -0500428 SSL_SIGN_RSA_PKCS1_SHA384,
429
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700430 SSL_SIGN_RSA_PSS_RSAE_SHA512,
David Benjaminc895d6b2016-08-11 13:26:41 -0400431 SSL_SIGN_RSA_PKCS1_SHA512,
David Benjaminc895d6b2016-08-11 13:26:41 -0400432
Robert Sloana27a6a42017-09-05 08:39:28 -0700433 // For now, SHA-1 is still accepted but least preferable.
Steven Valdez909b19f2016-11-21 15:35:44 -0500434 SSL_SIGN_RSA_PKCS1_SHA1,
David Benjaminc895d6b2016-08-11 13:26:41 -0400435
Steven Valdez909b19f2016-11-21 15:35:44 -0500436};
437
Robert Sloana27a6a42017-09-05 08:39:28 -0700438// kSignSignatureAlgorithms is the default list of supported signature
439// algorithms for signing.
440//
441// For now, RSA-PSS signature algorithms are not enabled on Android's system
442// BoringSSL. Once the change in Chrome has stuck and the values are finalized,
443// restore them.
Steven Valdez909b19f2016-11-21 15:35:44 -0500444static const uint16_t kSignSignatureAlgorithms[] = {
Robert Sloana27a6a42017-09-05 08:39:28 -0700445 // List our preferred algorithms first.
Robert Sloan572a4e22017-04-17 10:52:19 -0700446 SSL_SIGN_ED25519,
Steven Valdez909b19f2016-11-21 15:35:44 -0500447 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700448 SSL_SIGN_RSA_PSS_RSAE_SHA256,
David Benjaminc895d6b2016-08-11 13:26:41 -0400449 SSL_SIGN_RSA_PKCS1_SHA256,
David Benjaminc895d6b2016-08-11 13:26:41 -0400450
Robert Sloana27a6a42017-09-05 08:39:28 -0700451 // If needed, sign larger hashes.
452 //
453 // TODO(davidben): Determine which of these may be pruned.
Steven Valdez909b19f2016-11-21 15:35:44 -0500454 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700455 SSL_SIGN_RSA_PSS_RSAE_SHA384,
Steven Valdez909b19f2016-11-21 15:35:44 -0500456 SSL_SIGN_RSA_PKCS1_SHA384,
457
458 SSL_SIGN_ECDSA_SECP521R1_SHA512,
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700459 SSL_SIGN_RSA_PSS_RSAE_SHA512,
Steven Valdez909b19f2016-11-21 15:35:44 -0500460 SSL_SIGN_RSA_PKCS1_SHA512,
461
Robert Sloana27a6a42017-09-05 08:39:28 -0700462 // If the peer supports nothing else, sign with SHA-1.
David Benjaminc895d6b2016-08-11 13:26:41 -0400463 SSL_SIGN_ECDSA_SHA1,
Steven Valdez909b19f2016-11-21 15:35:44 -0500464 SSL_SIGN_RSA_PKCS1_SHA1,
David Benjaminc895d6b2016-08-11 13:26:41 -0400465};
466
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700467struct SSLSignatureAlgorithmList {
468 bool Next(uint16_t *out) {
469 while (!list.empty()) {
470 uint16_t sigalg = list[0];
471 list = list.subspan(1);
472 if (skip_ed25519 && sigalg == SSL_SIGN_ED25519) {
473 continue;
474 }
475 if (skip_rsa_pss_rsae && SSL_is_signature_algorithm_rsa_pss(sigalg)) {
476 continue;
477 }
478 *out = sigalg;
479 return true;
480 }
481 return false;
Robert Sloan572a4e22017-04-17 10:52:19 -0700482 }
483
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700484 bool operator==(const SSLSignatureAlgorithmList &other) const {
485 SSLSignatureAlgorithmList a = *this;
486 SSLSignatureAlgorithmList b = other;
487 uint16_t a_val, b_val;
488 while (a.Next(&a_val)) {
489 if (!b.Next(&b_val) ||
490 a_val != b_val) {
491 return false;
492 }
Robert Sloan572a4e22017-04-17 10:52:19 -0700493 }
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700494 return !b.Next(&b_val);
495 }
496
497 bool operator!=(const SSLSignatureAlgorithmList &other) const {
498 return !(*this == other);
499 }
500
501 Span<const uint16_t> list;
502 bool skip_ed25519 = false;
503 bool skip_rsa_pss_rsae = false;
504};
505
506static SSLSignatureAlgorithmList tls12_get_verify_sigalgs(const SSL *ssl,
507 bool for_certs) {
508 SSLSignatureAlgorithmList ret;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100509 if (!ssl->config->verify_sigalgs.empty()) {
510 ret.list = ssl->config->verify_sigalgs;
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700511 } else {
512 ret.list = kVerifySignatureAlgorithms;
513 ret.skip_ed25519 = !ssl->ctx->ed25519_enabled;
514 }
515 if (for_certs) {
516 ret.skip_rsa_pss_rsae = !ssl->ctx->rsa_pss_rsae_certs_enabled;
517 }
518 return ret;
519}
520
521bool tls12_add_verify_sigalgs(const SSL *ssl, CBB *out, bool for_certs) {
522 SSLSignatureAlgorithmList list = tls12_get_verify_sigalgs(ssl, for_certs);
523 uint16_t sigalg;
524 while (list.Next(&sigalg)) {
Robert Sloan309a31e2018-01-29 10:22:47 -0800525 if (!CBB_add_u16(out, sigalg)) {
526 return false;
Robert Sloan572a4e22017-04-17 10:52:19 -0700527 }
528 }
Robert Sloan309a31e2018-01-29 10:22:47 -0800529 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800530}
531
Robert Sloan309a31e2018-01-29 10:22:47 -0800532bool tls12_check_peer_sigalg(const SSL *ssl, uint8_t *out_alert,
533 uint16_t sigalg) {
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700534 SSLSignatureAlgorithmList list = tls12_get_verify_sigalgs(ssl, false);
535 uint16_t verify_sigalg;
536 while (list.Next(&verify_sigalg)) {
537 if (verify_sigalg == sigalg) {
Robert Sloan309a31e2018-01-29 10:22:47 -0800538 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800539 }
540 }
541
David Benjamin95add822016-10-19 01:09:12 -0400542 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
543 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan309a31e2018-01-29 10:22:47 -0800544 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800545}
546
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700547bool tls12_has_different_verify_sigalgs_for_certs(const SSL *ssl) {
548 return tls12_get_verify_sigalgs(ssl, true) !=
549 tls12_get_verify_sigalgs(ssl, false);
550}
551
Robert Sloana27a6a42017-09-05 08:39:28 -0700552// tls_extension represents a TLS extension that is handled internally. The
553// |init| function is called for each handshake, before any other functions of
554// the extension. Then the add and parse callbacks are called as needed.
555//
556// The parse callbacks receive a |CBS| that contains the contents of the
557// extension (i.e. not including the type and length bytes). If an extension is
558// not received then the parse callbacks will be called with a NULL CBS so that
559// they can do any processing needed to handle the absence of an extension.
560//
561// The add callbacks receive a |CBB| to which the extension can be appended but
562// the function is responsible for appending the type and length bytes too.
563//
Robert Sloan921ef2c2017-10-17 09:02:20 -0700564// All callbacks return true for success and false for error. If a parse
565// function returns zero then a fatal alert with value |*out_alert| will be
566// sent. If |*out_alert| isn't set, then a |decode_error| alert will be sent.
Kenny Rootb8494592015-09-25 02:29:14 +0000567struct tls_extension {
568 uint16_t value;
David Benjamin1b249672016-12-06 18:25:50 -0500569 void (*init)(SSL_HANDSHAKE *hs);
Kenny Roota04d78d2015-09-25 00:26:37 +0000570
Robert Sloan921ef2c2017-10-17 09:02:20 -0700571 bool (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
572 bool (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
573 CBS *contents);
Kenny Roota04d78d2015-09-25 00:26:37 +0000574
Robert Sloan921ef2c2017-10-17 09:02:20 -0700575 bool (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
576 CBS *contents);
577 bool (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Kenny Rootb8494592015-09-25 02:29:14 +0000578};
Kenny Roota04d78d2015-09-25 00:26:37 +0000579
Robert Sloan921ef2c2017-10-17 09:02:20 -0700580static bool forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin1b249672016-12-06 18:25:50 -0500581 CBS *contents) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400582 if (contents != NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700583 // Servers MUST NOT send this extension.
David Benjaminc895d6b2016-08-11 13:26:41 -0400584 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
585 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700586 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400587 }
588
Robert Sloan921ef2c2017-10-17 09:02:20 -0700589 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400590}
591
Robert Sloan921ef2c2017-10-17 09:02:20 -0700592static bool ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin1b249672016-12-06 18:25:50 -0500593 CBS *contents) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700594 // This extension from the client is handled elsewhere.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700595 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400596}
597
Robert Sloan921ef2c2017-10-17 09:02:20 -0700598static bool dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
599 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400600}
Kenny Rootb8494592015-09-25 02:29:14 +0000601
Robert Sloana27a6a42017-09-05 08:39:28 -0700602// Server name indication (SNI).
603//
604// https://tools.ietf.org/html/rfc6066#section-3.
Kenny Rootb8494592015-09-25 02:29:14 +0000605
Robert Sloan921ef2c2017-10-17 09:02:20 -0700606static bool ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500607 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100608 if (ssl->hostname == nullptr) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700609 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000610 }
611
612 CBB contents, server_name_list, name;
613 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
614 !CBB_add_u16_length_prefixed(out, &contents) ||
615 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
616 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
617 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100618 !CBB_add_bytes(&name, (const uint8_t *)ssl->hostname.get(),
619 strlen(ssl->hostname.get())) ||
Kenny Rootb8494592015-09-25 02:29:14 +0000620 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700621 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000622 }
623
Robert Sloan921ef2c2017-10-17 09:02:20 -0700624 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000625}
626
Robert Sloan921ef2c2017-10-17 09:02:20 -0700627static bool ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
628 CBS *contents) {
Robert Sloand1d118f2017-09-11 09:00:48 -0700629 // The server may acknowledge SNI with an empty extension. We check the syntax
630 // but otherwise ignore this signal.
631 return contents == NULL || CBS_len(contents) == 0;
Kenny Rootb8494592015-09-25 02:29:14 +0000632}
633
Robert Sloan921ef2c2017-10-17 09:02:20 -0700634static bool ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
635 CBS *contents) {
Pete Bentley0c61efe2019-08-13 09:32:23 +0100636 // SNI has already been parsed earlier in the handshake. See |extract_sni|.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700637 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000638}
639
Robert Sloan921ef2c2017-10-17 09:02:20 -0700640static bool ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500641 if (hs->ssl->s3->session_reused ||
642 !hs->should_ack_sni) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700643 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000644 }
645
646 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
647 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700648 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000649 }
650
Robert Sloan921ef2c2017-10-17 09:02:20 -0700651 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000652}
653
654
Robert Sloana27a6a42017-09-05 08:39:28 -0700655// Renegotiation indication.
656//
657// https://tools.ietf.org/html/rfc5746
Kenny Rootb8494592015-09-25 02:29:14 +0000658
Robert Sloan921ef2c2017-10-17 09:02:20 -0700659static bool ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500660 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700661 // Renegotiation indication is not necessary in TLS 1.3.
Robert Sloane56da3e2017-06-26 08:26:42 -0700662 if (hs->min_version >= TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700663 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400664 }
665
David Benjamin95add822016-10-19 01:09:12 -0400666 assert(ssl->s3->initial_handshake_complete ==
667 (ssl->s3->previous_client_finished_len != 0));
668
Kenny Rootb8494592015-09-25 02:29:14 +0000669 CBB contents, prev_finished;
670 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
671 !CBB_add_u16_length_prefixed(out, &contents) ||
672 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
673 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
674 ssl->s3->previous_client_finished_len) ||
675 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700676 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000677 }
678
Robert Sloan921ef2c2017-10-17 09:02:20 -0700679 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000680}
681
Robert Sloan921ef2c2017-10-17 09:02:20 -0700682static bool ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
683 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500684 SSL *const ssl = hs->ssl;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700685 if (contents != NULL && ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700686 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700687 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400688 }
689
Robert Sloana27a6a42017-09-05 08:39:28 -0700690 // Servers may not switch between omitting the extension and supporting it.
691 // See RFC 5746, sections 3.5 and 4.2.
Adam Langley4139edb2016-01-13 15:00:54 -0800692 if (ssl->s3->initial_handshake_complete &&
693 (contents != NULL) != ssl->s3->send_connection_binding) {
694 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
695 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700696 return false;
Adam Langley4139edb2016-01-13 15:00:54 -0800697 }
698
Kenny Rootb8494592015-09-25 02:29:14 +0000699 if (contents == NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700700 // Strictly speaking, if we want to avoid an attack we should *always* see
701 // RI even on initial ServerHello because the client doesn't see any
702 // renegotiation during an attack. However this would mean we could not
703 // connect to any server which doesn't support RI.
704 //
705 // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
706 // practical terms every client sets it so it's just assumed here.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700707 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000708 }
709
710 const size_t expected_len = ssl->s3->previous_client_finished_len +
711 ssl->s3->previous_server_finished_len;
712
Robert Sloana27a6a42017-09-05 08:39:28 -0700713 // Check for logic errors
Kenny Rootb8494592015-09-25 02:29:14 +0000714 assert(!expected_len || ssl->s3->previous_client_finished_len);
715 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin95add822016-10-19 01:09:12 -0400716 assert(ssl->s3->initial_handshake_complete ==
717 (ssl->s3->previous_client_finished_len != 0));
718 assert(ssl->s3->initial_handshake_complete ==
719 (ssl->s3->previous_server_finished_len != 0));
Kenny Rootb8494592015-09-25 02:29:14 +0000720
Robert Sloana27a6a42017-09-05 08:39:28 -0700721 // Parse out the extension contents.
Kenny Rootb8494592015-09-25 02:29:14 +0000722 CBS renegotiated_connection;
723 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
724 CBS_len(contents) != 0) {
725 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
726 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700727 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000728 }
729
Robert Sloana27a6a42017-09-05 08:39:28 -0700730 // Check that the extension matches.
Kenny Rootb8494592015-09-25 02:29:14 +0000731 if (CBS_len(&renegotiated_connection) != expected_len) {
732 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
733 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700734 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000735 }
736
737 const uint8_t *d = CBS_data(&renegotiated_connection);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700738 bool ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
739 ssl->s3->previous_client_finished_len) == 0;
Robert Sloanf6200e72017-07-10 08:09:18 -0700740#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
Robert Sloan921ef2c2017-10-17 09:02:20 -0700741 ok = true;
Robert Sloanf6200e72017-07-10 08:09:18 -0700742#endif
743 if (!ok) {
Kenny Rootb8494592015-09-25 02:29:14 +0000744 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
745 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700746 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000747 }
748 d += ssl->s3->previous_client_finished_len;
749
Robert Sloanf6200e72017-07-10 08:09:18 -0700750 ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
751 ssl->s3->previous_server_finished_len) == 0;
752#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
Robert Sloan921ef2c2017-10-17 09:02:20 -0700753 ok = true;
Robert Sloanf6200e72017-07-10 08:09:18 -0700754#endif
755 if (!ok) {
Kenny Rootb8494592015-09-25 02:29:14 +0000756 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700757 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700758 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000759 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700760 ssl->s3->send_connection_binding = true;
Kenny Rootb8494592015-09-25 02:29:14 +0000761
Robert Sloan921ef2c2017-10-17 09:02:20 -0700762 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000763}
764
Robert Sloan921ef2c2017-10-17 09:02:20 -0700765static bool ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
766 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500767 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700768 // Renegotiation isn't supported as a server so this function should never be
769 // called after the initial handshake.
Kenny Rootb8494592015-09-25 02:29:14 +0000770 assert(!ssl->s3->initial_handshake_complete);
771
Robert Sloan921ef2c2017-10-17 09:02:20 -0700772 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
773 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400774 }
Kenny Rootb8494592015-09-25 02:29:14 +0000775
776 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700777 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000778 }
779
780 CBS renegotiated_connection;
Kenny Rootb8494592015-09-25 02:29:14 +0000781 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
782 CBS_len(contents) != 0) {
783 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700784 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000785 }
786
Robert Sloana27a6a42017-09-05 08:39:28 -0700787 // Check that the extension matches. We do not support renegotiation as a
788 // server, so this must be empty.
David Benjamin95add822016-10-19 01:09:12 -0400789 if (CBS_len(&renegotiated_connection) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000790 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
791 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700792 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000793 }
794
Robert Sloana27a6a42017-09-05 08:39:28 -0700795 ssl->s3->send_connection_binding = true;
Kenny Rootb8494592015-09-25 02:29:14 +0000796
Robert Sloan921ef2c2017-10-17 09:02:20 -0700797 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000798}
799
Robert Sloan921ef2c2017-10-17 09:02:20 -0700800static bool ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
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.
David Benjamin95add822016-10-19 01:09:12 -0400804 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 }
809
Kenny Rootb8494592015-09-25 02:29:14 +0000810 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin95add822016-10-19 01:09:12 -0400811 !CBB_add_u16(out, 1 /* length */) ||
812 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700813 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000814 }
815
Robert Sloan921ef2c2017-10-17 09:02:20 -0700816 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000817}
818
819
Robert Sloana27a6a42017-09-05 08:39:28 -0700820// Extended Master Secret.
821//
822// https://tools.ietf.org/html/rfc7627
Kenny Rootb8494592015-09-25 02:29:14 +0000823
Robert Sloan921ef2c2017-10-17 09:02:20 -0700824static bool ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700825 // Extended master secret is not necessary in TLS 1.3.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100826 if (hs->min_version >= TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700827 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000828 }
829
830 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
831 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700832 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000833 }
834
Robert Sloan921ef2c2017-10-17 09:02:20 -0700835 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000836}
837
Robert Sloan921ef2c2017-10-17 09:02:20 -0700838static bool ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
839 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500840 SSL *const ssl = hs->ssl;
Robert Sloana94fe052017-02-21 08:49:28 -0800841
842 if (contents != NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700843 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
Robert Sloana94fe052017-02-21 08:49:28 -0800844 CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700845 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400846 }
847
Robert Sloana27a6a42017-09-05 08:39:28 -0700848 hs->extended_master_secret = true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400849 }
850
Robert Sloana27a6a42017-09-05 08:39:28 -0700851 // Whether EMS is negotiated may not change on renegotiation.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700852 if (ssl->s3->established_session != nullptr &&
Robert Sloana94fe052017-02-21 08:49:28 -0800853 hs->extended_master_secret !=
Robert Sloana27a6a42017-09-05 08:39:28 -0700854 !!ssl->s3->established_session->extended_master_secret) {
Robert Sloana94fe052017-02-21 08:49:28 -0800855 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
856 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700857 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400858 }
859
Robert Sloan921ef2c2017-10-17 09:02:20 -0700860 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000861}
862
Robert Sloan921ef2c2017-10-17 09:02:20 -0700863static bool ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
864 CBS *contents) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100865 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700866 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400867 }
868
869 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700870 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000871 }
872
873 if (CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700874 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000875 }
876
Robert Sloana27a6a42017-09-05 08:39:28 -0700877 hs->extended_master_secret = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700878 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000879}
880
Robert Sloan921ef2c2017-10-17 09:02:20 -0700881static bool ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloana94fe052017-02-21 08:49:28 -0800882 if (!hs->extended_master_secret) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700883 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000884 }
885
886 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
887 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700888 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000889 }
890
Robert Sloan921ef2c2017-10-17 09:02:20 -0700891 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000892}
893
894
Robert Sloana27a6a42017-09-05 08:39:28 -0700895// Session tickets.
896//
897// https://tools.ietf.org/html/rfc5077
Kenny Rootb8494592015-09-25 02:29:14 +0000898
Robert Sloan921ef2c2017-10-17 09:02:20 -0700899static bool ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500900 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700901 // TLS 1.3 uses a different ticket extension.
Robert Sloane56da3e2017-06-26 08:26:42 -0700902 if (hs->min_version >= TLS1_3_VERSION ||
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400903 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700904 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000905 }
906
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100907 Span<const uint8_t> ticket;
Kenny Rootb8494592015-09-25 02:29:14 +0000908
Robert Sloana27a6a42017-09-05 08:39:28 -0700909 // Renegotiation does not participate in session resumption. However, still
910 // advertise the extension to avoid potentially breaking servers which carry
911 // over the state from the previous handshake, such as OpenSSL servers
912 // without upstream's 3c3f0259238594d77264a78944d409f2127642c4.
Kenny Rootb8494592015-09-25 02:29:14 +0000913 if (!ssl->s3->initial_handshake_complete &&
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100914 ssl->session != nullptr &&
915 !ssl->session->ticket.empty() &&
Robert Sloana27a6a42017-09-05 08:39:28 -0700916 // Don't send TLS 1.3 session tickets in the ticket extension.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100917 ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) {
918 ticket = ssl->session->ticket;
Kenny Rootb8494592015-09-25 02:29:14 +0000919 }
920
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100921 CBB ticket_cbb;
Kenny Rootb8494592015-09-25 02:29:14 +0000922 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100923 !CBB_add_u16_length_prefixed(out, &ticket_cbb) ||
924 !CBB_add_bytes(&ticket_cbb, ticket.data(), ticket.size()) ||
Kenny Rootb8494592015-09-25 02:29:14 +0000925 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700926 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000927 }
928
Robert Sloan921ef2c2017-10-17 09:02:20 -0700929 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000930}
931
Robert Sloan921ef2c2017-10-17 09:02:20 -0700932static bool ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
933 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500934 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +0000935 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700936 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000937 }
938
Robert Sloan921ef2c2017-10-17 09:02:20 -0700939 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
940 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400941 }
942
Robert Sloana27a6a42017-09-05 08:39:28 -0700943 // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
944 // this function should never be called, even if the server tries to send the
945 // extension.
Kenny Rootb8494592015-09-25 02:29:14 +0000946 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
947
948 if (CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700949 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000950 }
951
Robert Sloana27a6a42017-09-05 08:39:28 -0700952 hs->ticket_expected = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700953 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000954}
955
Robert Sloan921ef2c2017-10-17 09:02:20 -0700956static bool ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500957 if (!hs->ticket_expected) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700958 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000959 }
960
Robert Sloana27a6a42017-09-05 08:39:28 -0700961 // If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true.
David Benjamin1b249672016-12-06 18:25:50 -0500962 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Kenny Rootb8494592015-09-25 02:29:14 +0000963
964 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
965 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700966 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000967 }
968
Robert Sloan921ef2c2017-10-17 09:02:20 -0700969 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000970}
971
972
Robert Sloana27a6a42017-09-05 08:39:28 -0700973// Signature Algorithms.
974//
975// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
Kenny Rootb8494592015-09-25 02:29:14 +0000976
Robert Sloan921ef2c2017-10-17 09:02:20 -0700977static bool ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500978 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -0700979 if (hs->max_version < TLS1_2_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700980 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000981 }
982
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700983 // Prior to TLS 1.3, there was no way to signal different signature algorithm
984 // preferences between the online signature and certificates. If we do not
985 // send the signature_algorithms_cert extension, use the potentially more
986 // restrictive certificate list.
987 //
988 // TODO(davidben): When TLS 1.3 is finalized, we can likely remove the TLS 1.3
989 // check both here and in signature_algorithms_cert. |hs->max_version| is not
990 // the negotiated version. Rather the expectation is that any server consuming
991 // signature algorithms added in TLS 1.3 will also know to look at
992 // signature_algorithms_cert. For now, TLS 1.3 is not quite yet final and it
993 // seems prudent to condition this new extension on it.
994 bool for_certs = hs->max_version < TLS1_3_VERSION;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400995 CBB contents, sigalgs_cbb;
Kenny Rootb8494592015-09-25 02:29:14 +0000996 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
997 !CBB_add_u16_length_prefixed(out, &contents) ||
Robert Sloan572a4e22017-04-17 10:52:19 -0700998 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700999 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb, for_certs) ||
Robert Sloan572a4e22017-04-17 10:52:19 -07001000 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001001 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001002 }
1003
Robert Sloan921ef2c2017-10-17 09:02:20 -07001004 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001005}
1006
Robert Sloan921ef2c2017-10-17 09:02:20 -07001007static bool ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1008 CBS *contents) {
Robert Sloan4562e9d2017-10-02 10:26:51 -07001009 hs->peer_sigalgs.Reset();
Kenny Rootb8494592015-09-25 02:29:14 +00001010 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001011 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001012 }
1013
1014 CBS supported_signature_algorithms;
1015 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
1016 CBS_len(contents) != 0 ||
David Benjamin1b249672016-12-06 18:25:50 -05001017 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001018 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001019 }
1020
Robert Sloan921ef2c2017-10-17 09:02:20 -07001021 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001022}
1023
Kenny Rootb8494592015-09-25 02:29:14 +00001024
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001025// Signature Algorithms for Certificates.
1026//
Robert Sloand9e572d2018-08-27 12:27:00 -07001027// https://tools.ietf.org/html/rfc8446#section-4.2.3
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001028
1029static bool ext_sigalgs_cert_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1030 SSL *const ssl = hs->ssl;
1031 // If this extension is omitted, it defaults to the signature_algorithms
1032 // extension, so only emit it if the list is different.
1033 //
1034 // This extension is also new in TLS 1.3, so omit it if TLS 1.3 is disabled.
1035 // There is a corresponding version check in |ext_sigalgs_add_clienthello|.
1036 if (hs->max_version < TLS1_3_VERSION ||
1037 !tls12_has_different_verify_sigalgs_for_certs(ssl)) {
1038 return true;
1039 }
1040
1041 CBB contents, sigalgs_cbb;
1042 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms_cert) ||
1043 !CBB_add_u16_length_prefixed(out, &contents) ||
1044 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
1045 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb, true /* certs */) ||
1046 !CBB_flush(out)) {
1047 return false;
1048 }
1049
1050 return true;
1051}
1052
1053
Robert Sloana27a6a42017-09-05 08:39:28 -07001054// OCSP Stapling.
1055//
1056// https://tools.ietf.org/html/rfc6066#section-8
Kenny Rootb8494592015-09-25 02:29:14 +00001057
Robert Sloan921ef2c2017-10-17 09:02:20 -07001058static bool ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001059 if (!hs->config->ocsp_stapling_enabled) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001060 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001061 }
1062
1063 CBB contents;
1064 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1065 !CBB_add_u16_length_prefixed(out, &contents) ||
1066 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1067 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1068 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1069 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001070 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001071 }
1072
Robert Sloan921ef2c2017-10-17 09:02:20 -07001073 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001074}
1075
Robert Sloan921ef2c2017-10-17 09:02:20 -07001076static bool ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1077 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001078 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001079 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001080 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001081 }
1082
Robert Sloana27a6a42017-09-05 08:39:28 -07001083 // TLS 1.3 OCSP responses are included in the Certificate extensions.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001084 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1085 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001086 }
1087
Robert Sloana27a6a42017-09-05 08:39:28 -07001088 // OCSP stapling is forbidden on non-certificate ciphers.
Steven Valdez909b19f2016-11-21 15:35:44 -05001089 if (CBS_len(contents) != 0 ||
Robert Sloana94fe052017-02-21 08:49:28 -08001090 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001091 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04001092 }
1093
Robert Sloana27a6a42017-09-05 08:39:28 -07001094 // Note this does not check for resumption in TLS 1.2. Sending
1095 // status_request here does not make sense, but OpenSSL does so and the
1096 // specification does not say anything. Tolerate it but ignore it.
David Benjaminc895d6b2016-08-11 13:26:41 -04001097
Robert Sloana27a6a42017-09-05 08:39:28 -07001098 hs->certificate_status_expected = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001099 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001100}
1101
Robert Sloan921ef2c2017-10-17 09:02:20 -07001102static bool ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1103 CBS *contents) {
Kenny Rootb8494592015-09-25 02:29:14 +00001104 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001105 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001106 }
1107
1108 uint8_t status_type;
1109 if (!CBS_get_u8(contents, &status_type)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001110 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001111 }
1112
Robert Sloana27a6a42017-09-05 08:39:28 -07001113 // We cannot decide whether OCSP stapling will occur yet because the correct
1114 // SSL_CTX might not have been selected.
David Benjamin1b249672016-12-06 18:25:50 -05001115 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Kenny Rootb8494592015-09-25 02:29:14 +00001116
Robert Sloan921ef2c2017-10-17 09:02:20 -07001117 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001118}
1119
Robert Sloan921ef2c2017-10-17 09:02:20 -07001120static bool ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001121 SSL *const ssl = hs->ssl;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001122 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001123 !hs->ocsp_stapling_requested || hs->config->cert->ocsp_response == NULL ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001124 ssl->s3->session_reused ||
Robert Sloana94fe052017-02-21 08:49:28 -08001125 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001126 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001127 }
1128
Robert Sloana27a6a42017-09-05 08:39:28 -07001129 hs->certificate_status_expected = true;
Kenny Rootb8494592015-09-25 02:29:14 +00001130
1131 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdez909b19f2016-11-21 15:35:44 -05001132 CBB_add_u16(out, 0 /* length */);
Kenny Rootb8494592015-09-25 02:29:14 +00001133}
1134
1135
Robert Sloana27a6a42017-09-05 08:39:28 -07001136// Next protocol negotiation.
1137//
1138// https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
Kenny Rootb8494592015-09-25 02:29:14 +00001139
Robert Sloan921ef2c2017-10-17 09:02:20 -07001140static bool ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001141 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001142 if (ssl->s3->initial_handshake_complete ||
1143 ssl->ctx->next_proto_select_cb == NULL ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001144 SSL_is_dtls(ssl)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001145 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001146 }
1147
1148 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1149 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001150 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001151 }
1152
Robert Sloan921ef2c2017-10-17 09:02:20 -07001153 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001154}
1155
Robert Sloan921ef2c2017-10-17 09:02:20 -07001156static bool ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1157 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001158 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001159 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001160 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001161 }
1162
Robert Sloan921ef2c2017-10-17 09:02:20 -07001163 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1164 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04001165 }
1166
Robert Sloana27a6a42017-09-05 08:39:28 -07001167 // If any of these are false then we should never have sent the NPN
1168 // extension in the ClientHello and thus this function should never have been
1169 // called.
Kenny Rootb8494592015-09-25 02:29:14 +00001170 assert(!ssl->s3->initial_handshake_complete);
David Benjaminc895d6b2016-08-11 13:26:41 -04001171 assert(!SSL_is_dtls(ssl));
Kenny Rootb8494592015-09-25 02:29:14 +00001172 assert(ssl->ctx->next_proto_select_cb != NULL);
1173
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001174 if (!ssl->s3->alpn_selected.empty()) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001175 // NPN and ALPN may not be negotiated in the same connection.
Kenny Rootb8494592015-09-25 02:29:14 +00001176 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1177 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001178 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001179 }
1180
1181 const uint8_t *const orig_contents = CBS_data(contents);
1182 const size_t orig_len = CBS_len(contents);
1183
1184 while (CBS_len(contents) != 0) {
1185 CBS proto;
1186 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1187 CBS_len(&proto) == 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001188 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001189 }
1190 }
1191
1192 uint8_t *selected;
1193 uint8_t selected_len;
1194 if (ssl->ctx->next_proto_select_cb(
1195 ssl, &selected, &selected_len, orig_contents, orig_len,
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001196 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK ||
1197 !ssl->s3->next_proto_negotiated.CopyFrom(
1198 MakeConstSpan(selected, selected_len))) {
Kenny Rootb8494592015-09-25 02:29:14 +00001199 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001200 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001201 }
1202
Robert Sloana27a6a42017-09-05 08:39:28 -07001203 hs->next_proto_neg_seen = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001204 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001205}
1206
Robert Sloan921ef2c2017-10-17 09:02:20 -07001207static bool ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1208 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001209 SSL *const ssl = hs->ssl;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001210 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1211 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04001212 }
1213
Kenny Rootb8494592015-09-25 02:29:14 +00001214 if (contents != NULL && CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001215 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001216 }
1217
1218 if (contents == NULL ||
1219 ssl->s3->initial_handshake_complete ||
Kenny Rootb8494592015-09-25 02:29:14 +00001220 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001221 SSL_is_dtls(ssl)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001222 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001223 }
1224
Robert Sloana27a6a42017-09-05 08:39:28 -07001225 hs->next_proto_neg_seen = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001226 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001227}
1228
Robert Sloan921ef2c2017-10-17 09:02:20 -07001229static bool ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001230 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001231 // |next_proto_neg_seen| might have been cleared when an ALPN extension was
1232 // parsed.
David Benjamin1b249672016-12-06 18:25:50 -05001233 if (!hs->next_proto_neg_seen) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001234 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001235 }
1236
1237 const uint8_t *npa;
1238 unsigned npa_len;
1239
1240 if (ssl->ctx->next_protos_advertised_cb(
1241 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1242 SSL_TLSEXT_ERR_OK) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001243 hs->next_proto_neg_seen = false;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001244 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001245 }
1246
1247 CBB contents;
1248 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1249 !CBB_add_u16_length_prefixed(out, &contents) ||
1250 !CBB_add_bytes(&contents, npa, npa_len) ||
1251 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001252 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001253 }
1254
Robert Sloan921ef2c2017-10-17 09:02:20 -07001255 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001256}
1257
1258
Robert Sloana27a6a42017-09-05 08:39:28 -07001259// Signed certificate timestamps.
1260//
1261// https://tools.ietf.org/html/rfc6962#section-3.3.1
Kenny Rootb8494592015-09-25 02:29:14 +00001262
Robert Sloan921ef2c2017-10-17 09:02:20 -07001263static bool ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001264 if (!hs->config->signed_cert_timestamps_enabled) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001265 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001266 }
1267
1268 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1269 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001270 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001271 }
1272
Robert Sloan921ef2c2017-10-17 09:02:20 -07001273 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001274}
1275
Robert Sloan921ef2c2017-10-17 09:02:20 -07001276static bool ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1277 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001278 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001279 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001280 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001281 }
1282
Robert Sloana27a6a42017-09-05 08:39:28 -07001283 // TLS 1.3 SCTs are included in the Certificate extensions.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001284 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001285 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001286 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05001287 }
1288
Robert Sloana27a6a42017-09-05 08:39:28 -07001289 // If this is false then we should never have sent the SCT extension in the
1290 // ClientHello and thus this function should never have been called.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001291 assert(hs->config->signed_cert_timestamps_enabled);
Kenny Rootb8494592015-09-25 02:29:14 +00001292
Steven Valdez909b19f2016-11-21 15:35:44 -05001293 if (!ssl_is_sct_list_valid(contents)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001294 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001295 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001296 }
1297
Robert Sloana27a6a42017-09-05 08:39:28 -07001298 // Session resumption uses the original session information. The extension
1299 // should not be sent on resumption, but RFC 6962 did not make it a
1300 // requirement, so tolerate this.
1301 //
1302 // TODO(davidben): Enforce this anyway.
Robert Sloan8f860b12017-08-28 07:37:06 -07001303 if (!ssl->s3->session_reused) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001304 hs->new_session->signed_cert_timestamp_list.reset(
1305 CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool));
Robert Sloan8f860b12017-08-28 07:37:06 -07001306 if (hs->new_session->signed_cert_timestamp_list == nullptr) {
1307 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001308 return false;
Robert Sloan8f860b12017-08-28 07:37:06 -07001309 }
Kenny Rootb8494592015-09-25 02:29:14 +00001310 }
1311
Robert Sloan921ef2c2017-10-17 09:02:20 -07001312 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001313}
1314
Robert Sloan921ef2c2017-10-17 09:02:20 -07001315static bool ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1316 CBS *contents) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001317 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001318 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05001319 }
1320
1321 if (CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001322 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05001323 }
1324
Robert Sloana27a6a42017-09-05 08:39:28 -07001325 hs->scts_requested = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001326 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001327}
1328
Robert Sloan921ef2c2017-10-17 09:02:20 -07001329static bool ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001330 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001331 // The extension shouldn't be sent when resuming sessions.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001332 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION || ssl->s3->session_reused ||
1333 hs->config->cert->signed_cert_timestamp_list == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001334 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001335 }
1336
1337 CBB contents;
1338 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1339 CBB_add_u16_length_prefixed(out, &contents) &&
Robert Sloana94fe052017-02-21 08:49:28 -08001340 CBB_add_bytes(
1341 &contents,
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001342 CRYPTO_BUFFER_data(
1343 hs->config->cert->signed_cert_timestamp_list.get()),
1344 CRYPTO_BUFFER_len(
1345 hs->config->cert->signed_cert_timestamp_list.get())) &&
Kenny Rootb8494592015-09-25 02:29:14 +00001346 CBB_flush(out);
1347}
1348
1349
Robert Sloana27a6a42017-09-05 08:39:28 -07001350// Application-level Protocol Negotiation.
1351//
1352// https://tools.ietf.org/html/rfc7301
Kenny Rootb8494592015-09-25 02:29:14 +00001353
Robert Sloan921ef2c2017-10-17 09:02:20 -07001354static bool ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001355 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001356 if (hs->config->alpn_client_proto_list.empty() ||
Kenny Rootb8494592015-09-25 02:29:14 +00001357 ssl->s3->initial_handshake_complete) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001358 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001359 }
1360
1361 CBB contents, proto_list;
1362 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1363 !CBB_add_u16_length_prefixed(out, &contents) ||
1364 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001365 !CBB_add_bytes(&proto_list, hs->config->alpn_client_proto_list.data(),
1366 hs->config->alpn_client_proto_list.size()) ||
Kenny Rootb8494592015-09-25 02:29:14 +00001367 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001368 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001369 }
1370
Robert Sloan921ef2c2017-10-17 09:02:20 -07001371 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001372}
1373
Robert Sloan921ef2c2017-10-17 09:02:20 -07001374static bool ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1375 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001376 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001377 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001378 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001379 }
1380
1381 assert(!ssl->s3->initial_handshake_complete);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001382 assert(!hs->config->alpn_client_proto_list.empty());
Kenny Rootb8494592015-09-25 02:29:14 +00001383
David Benjamin1b249672016-12-06 18:25:50 -05001384 if (hs->next_proto_neg_seen) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001385 // NPN and ALPN may not be negotiated in the same connection.
Kenny Rootb8494592015-09-25 02:29:14 +00001386 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1387 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001388 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001389 }
1390
Robert Sloana27a6a42017-09-05 08:39:28 -07001391 // The extension data consists of a ProtocolNameList which must have
1392 // exactly one ProtocolName. Each of these is length-prefixed.
Kenny Rootb8494592015-09-25 02:29:14 +00001393 CBS protocol_name_list, protocol_name;
1394 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1395 CBS_len(contents) != 0 ||
1396 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
Robert Sloana27a6a42017-09-05 08:39:28 -07001397 // Empty protocol names are forbidden.
Kenny Rootb8494592015-09-25 02:29:14 +00001398 CBS_len(&protocol_name) == 0 ||
1399 CBS_len(&protocol_name_list) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001400 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001401 }
1402
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001403 if (!ssl_is_alpn_protocol_allowed(hs, protocol_name)) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001404 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1405 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1406 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04001407 }
1408
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001409 if (!ssl->s3->alpn_selected.CopyFrom(protocol_name)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001410 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001411 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001412 }
1413
Robert Sloan921ef2c2017-10-17 09:02:20 -07001414 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001415}
1416
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001417bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs,
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001418 Span<const uint8_t> protocol) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001419 if (hs->config->alpn_client_proto_list.empty()) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001420 return false;
1421 }
1422
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001423 if (hs->ssl->ctx->allow_unknown_alpn_protos) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001424 return true;
1425 }
1426
1427 // Check that the protocol name is one of the ones we advertised.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001428 CBS client_protocol_name_list =
1429 MakeConstSpan(hs->config->alpn_client_proto_list),
1430 client_protocol_name;
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001431 while (CBS_len(&client_protocol_name_list) > 0) {
1432 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1433 &client_protocol_name)) {
1434 return false;
1435 }
1436
1437 if (client_protocol_name == protocol) {
1438 return true;
1439 }
1440 }
1441
1442 return false;
1443}
1444
Robert Sloan921ef2c2017-10-17 09:02:20 -07001445bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1446 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin1b249672016-12-06 18:25:50 -05001447 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -05001448 CBS contents;
Kenny Rootb8494592015-09-25 02:29:14 +00001449 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin1b249672016-12-06 18:25:50 -05001450 !ssl_client_hello_get_extension(
Steven Valdez909b19f2016-11-21 15:35:44 -05001451 client_hello, &contents,
1452 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001453 // Ignore ALPN if not configured or no extension was supplied.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001454 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001455 }
1456
Robert Sloana27a6a42017-09-05 08:39:28 -07001457 // ALPN takes precedence over NPN.
1458 hs->next_proto_neg_seen = false;
Kenny Rootb8494592015-09-25 02:29:14 +00001459
1460 CBS protocol_name_list;
Steven Valdez909b19f2016-11-21 15:35:44 -05001461 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1462 CBS_len(&contents) != 0 ||
Kenny Rootb8494592015-09-25 02:29:14 +00001463 CBS_len(&protocol_name_list) < 2) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001464 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1465 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001466 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001467 }
1468
Robert Sloana27a6a42017-09-05 08:39:28 -07001469 // Validate the protocol list.
Kenny Rootb8494592015-09-25 02:29:14 +00001470 CBS protocol_name_list_copy = protocol_name_list;
1471 while (CBS_len(&protocol_name_list_copy) > 0) {
1472 CBS protocol_name;
1473
1474 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
Robert Sloana27a6a42017-09-05 08:39:28 -07001475 // Empty protocol names are forbidden.
Kenny Rootb8494592015-09-25 02:29:14 +00001476 CBS_len(&protocol_name) == 0) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001477 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1478 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001479 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001480 }
1481 }
1482
1483 const uint8_t *selected;
1484 uint8_t selected_len;
1485 if (ssl->ctx->alpn_select_cb(
1486 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1487 CBS_len(&protocol_name_list),
1488 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001489 if (selected_len == 0) {
1490 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1491 *out_alert = SSL_AD_INTERNAL_ERROR;
1492 return false;
1493 }
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001494 if (!ssl->s3->alpn_selected.CopyFrom(
1495 MakeConstSpan(selected, selected_len))) {
Kenny Rootb8494592015-09-25 02:29:14 +00001496 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001497 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001498 }
Kenny Rootb8494592015-09-25 02:29:14 +00001499 }
1500
Robert Sloan921ef2c2017-10-17 09:02:20 -07001501 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001502}
1503
Robert Sloan921ef2c2017-10-17 09:02:20 -07001504static bool ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001505 SSL *const ssl = hs->ssl;
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001506 if (ssl->s3->alpn_selected.empty()) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001507 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001508 }
1509
1510 CBB contents, proto_list, proto;
1511 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1512 !CBB_add_u16_length_prefixed(out, &contents) ||
1513 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1514 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001515 !CBB_add_bytes(&proto, ssl->s3->alpn_selected.data(),
1516 ssl->s3->alpn_selected.size()) ||
Kenny Rootb8494592015-09-25 02:29:14 +00001517 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001518 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001519 }
1520
Robert Sloan921ef2c2017-10-17 09:02:20 -07001521 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001522}
1523
1524
Robert Sloana27a6a42017-09-05 08:39:28 -07001525// Channel ID.
1526//
1527// https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
Kenny Rootb8494592015-09-25 02:29:14 +00001528
David Benjamin1b249672016-12-06 18:25:50 -05001529static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001530 hs->ssl->s3->channel_id_valid = false;
Kenny Rootb8494592015-09-25 02:29:14 +00001531}
1532
Robert Sloan921ef2c2017-10-17 09:02:20 -07001533static bool ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001534 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001535 if (!hs->config->channel_id_enabled || SSL_is_dtls(ssl)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001536 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001537 }
1538
1539 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1540 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001541 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001542 }
1543
Robert Sloan921ef2c2017-10-17 09:02:20 -07001544 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001545}
1546
Robert Sloan921ef2c2017-10-17 09:02:20 -07001547static bool ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1548 uint8_t *out_alert,
1549 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001550 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001551 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001552 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001553 }
1554
David Benjaminc895d6b2016-08-11 13:26:41 -04001555 assert(!SSL_is_dtls(ssl));
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001556 assert(hs->config->channel_id_enabled);
Kenny Rootb8494592015-09-25 02:29:14 +00001557
1558 if (CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001559 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001560 }
1561
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001562 ssl->s3->channel_id_valid = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001563 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001564}
1565
Robert Sloan921ef2c2017-10-17 09:02:20 -07001566static bool ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1567 uint8_t *out_alert,
1568 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001569 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001570 if (contents == NULL || !hs->config->channel_id_enabled || SSL_is_dtls(ssl)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001571 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001572 }
1573
1574 if (CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001575 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001576 }
1577
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001578 ssl->s3->channel_id_valid = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001579 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001580}
1581
Robert Sloan921ef2c2017-10-17 09:02:20 -07001582static bool ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001583 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001584 if (!ssl->s3->channel_id_valid) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001585 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001586 }
1587
1588 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1589 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001590 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001591 }
1592
Robert Sloan921ef2c2017-10-17 09:02:20 -07001593 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001594}
1595
1596
Robert Sloana27a6a42017-09-05 08:39:28 -07001597// Secure Real-time Transport Protocol (SRTP) extension.
1598//
1599// https://tools.ietf.org/html/rfc5764
Kenny Rootb8494592015-09-25 02:29:14 +00001600
1601
David Benjamin1b249672016-12-06 18:25:50 -05001602static void ext_srtp_init(SSL_HANDSHAKE *hs) {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001603 hs->ssl->s3->srtp_profile = NULL;
Kenny Rootb8494592015-09-25 02:29:14 +00001604}
1605
Robert Sloan921ef2c2017-10-17 09:02:20 -07001606static bool ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001607 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001608 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
Robert Sloanfe7cd212017-08-07 09:03:39 -07001609 if (profiles == NULL ||
1610 sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001611 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001612 }
1613
1614 CBB contents, profile_ids;
1615 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1616 !CBB_add_u16_length_prefixed(out, &contents) ||
1617 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001618 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001619 }
1620
Robert Sloanfe7cd212017-08-07 09:03:39 -07001621 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
1622 if (!CBB_add_u16(&profile_ids, profile->id)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001623 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001624 }
1625 }
1626
1627 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1628 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001629 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001630 }
1631
Robert Sloan921ef2c2017-10-17 09:02:20 -07001632 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001633}
1634
Robert Sloan921ef2c2017-10-17 09:02:20 -07001635static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1636 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001637 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001638 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001639 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001640 }
1641
Robert Sloana27a6a42017-09-05 08:39:28 -07001642 // The extension consists of a u16-prefixed profile ID list containing a
1643 // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1644 //
1645 // See https://tools.ietf.org/html/rfc5764#section-4.1.1
Kenny Rootb8494592015-09-25 02:29:14 +00001646 CBS profile_ids, srtp_mki;
1647 uint16_t profile_id;
1648 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1649 !CBS_get_u16(&profile_ids, &profile_id) ||
1650 CBS_len(&profile_ids) != 0 ||
1651 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1652 CBS_len(contents) != 0) {
1653 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001654 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001655 }
1656
1657 if (CBS_len(&srtp_mki) != 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001658 // Must be no MKI, since we never offer one.
Kenny Rootb8494592015-09-25 02:29:14 +00001659 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1660 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001661 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001662 }
1663
1664 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1665
Robert Sloana27a6a42017-09-05 08:39:28 -07001666 // Check to see if the server gave us something we support (and presumably
1667 // offered).
Robert Sloanfe7cd212017-08-07 09:03:39 -07001668 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
Kenny Rootb8494592015-09-25 02:29:14 +00001669 if (profile->id == profile_id) {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001670 ssl->s3->srtp_profile = profile;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001671 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001672 }
1673 }
1674
1675 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1676 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001677 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001678}
1679
Robert Sloan921ef2c2017-10-17 09:02:20 -07001680static bool ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1681 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001682 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001683 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001684 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001685 }
1686
1687 CBS profile_ids, srtp_mki;
1688 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1689 CBS_len(&profile_ids) < 2 ||
1690 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1691 CBS_len(contents) != 0) {
1692 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001693 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001694 }
Robert Sloana27a6a42017-09-05 08:39:28 -07001695 // Discard the MKI value for now.
Kenny Rootb8494592015-09-25 02:29:14 +00001696
1697 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1698 SSL_get_srtp_profiles(ssl);
1699
Robert Sloana27a6a42017-09-05 08:39:28 -07001700 // Pick the server's most preferred profile.
Robert Sloanfe7cd212017-08-07 09:03:39 -07001701 for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) {
Kenny Rootb8494592015-09-25 02:29:14 +00001702 CBS profile_ids_tmp;
1703 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1704
1705 while (CBS_len(&profile_ids_tmp) > 0) {
1706 uint16_t profile_id;
1707 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001708 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001709 }
1710
1711 if (server_profile->id == profile_id) {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001712 ssl->s3->srtp_profile = server_profile;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001713 return true;
Kenny Roota04d78d2015-09-25 00:26:37 +00001714 }
1715 }
1716 }
1717
Robert Sloan921ef2c2017-10-17 09:02:20 -07001718 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001719}
1720
Robert Sloan921ef2c2017-10-17 09:02:20 -07001721static bool ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001722 SSL *const ssl = hs->ssl;
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001723 if (ssl->s3->srtp_profile == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001724 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001725 }
1726
1727 CBB contents, profile_ids;
1728 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1729 !CBB_add_u16_length_prefixed(out, &contents) ||
1730 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001731 !CBB_add_u16(&profile_ids, ssl->s3->srtp_profile->id) ||
Kenny Rootb8494592015-09-25 02:29:14 +00001732 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1733 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001734 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001735 }
1736
Robert Sloan921ef2c2017-10-17 09:02:20 -07001737 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001738}
1739
1740
Robert Sloana27a6a42017-09-05 08:39:28 -07001741// EC point formats.
1742//
1743// https://tools.ietf.org/html/rfc4492#section-5.1.2
Kenny Rootb8494592015-09-25 02:29:14 +00001744
Robert Sloan921ef2c2017-10-17 09:02:20 -07001745static bool ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
Kenny Rootb8494592015-09-25 02:29:14 +00001746 CBB contents, formats;
1747 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1748 !CBB_add_u16_length_prefixed(out, &contents) ||
1749 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1750 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
1751 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001752 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001753 }
1754
Robert Sloan921ef2c2017-10-17 09:02:20 -07001755 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001756}
1757
Robert Sloan921ef2c2017-10-17 09:02:20 -07001758static bool ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Pete Bentley0c61efe2019-08-13 09:32:23 +01001759 // The point format extension is unnecessary in TLS 1.3.
Robert Sloane56da3e2017-06-26 08:26:42 -07001760 if (hs->min_version >= TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001761 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001762 }
1763
David Benjamin1b249672016-12-06 18:25:50 -05001764 return ext_ec_point_add_extension(hs, out);
Kenny Rootb8494592015-09-25 02:29:14 +00001765}
1766
Robert Sloan921ef2c2017-10-17 09:02:20 -07001767static bool ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1768 CBS *contents) {
Kenny Rootb8494592015-09-25 02:29:14 +00001769 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001770 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001771 }
1772
Robert Sloan921ef2c2017-10-17 09:02:20 -07001773 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
1774 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04001775 }
1776
Kenny Rootb8494592015-09-25 02:29:14 +00001777 CBS ec_point_format_list;
1778 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1779 CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001780 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001781 }
1782
Robert Sloana27a6a42017-09-05 08:39:28 -07001783 // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1784 // point format.
Robert Sloan69939df2017-01-09 10:53:07 -08001785 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1786 TLSEXT_ECPOINTFORMAT_uncompressed,
1787 CBS_len(&ec_point_format_list)) == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001788 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001789 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001790 }
1791
Robert Sloan921ef2c2017-10-17 09:02:20 -07001792 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001793}
1794
Robert Sloan921ef2c2017-10-17 09:02:20 -07001795static bool ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +00001796 CBS *contents) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001797 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
1798 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04001799 }
1800
David Benjamin1b249672016-12-06 18:25:50 -05001801 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Kenny Rootb8494592015-09-25 02:29:14 +00001802}
1803
Robert Sloan921ef2c2017-10-17 09:02:20 -07001804static bool ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001805 SSL *const ssl = hs->ssl;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001806 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1807 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04001808 }
1809
Robert Sloana94fe052017-02-21 08:49:28 -08001810 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1811 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001812 const bool using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Kenny Rootb8494592015-09-25 02:29:14 +00001813
1814 if (!using_ecc) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001815 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001816 }
1817
David Benjamin1b249672016-12-06 18:25:50 -05001818 return ext_ec_point_add_extension(hs, out);
Kenny Rootb8494592015-09-25 02:29:14 +00001819}
1820
Steven Valdez909b19f2016-11-21 15:35:44 -05001821
Robert Sloana27a6a42017-09-05 08:39:28 -07001822// Pre Shared Key
1823//
Robert Sloand9e572d2018-08-27 12:27:00 -07001824// https://tools.ietf.org/html/rfc8446#section-4.2.11
Steven Valdez909b19f2016-11-21 15:35:44 -05001825
David Benjamin1b249672016-12-06 18:25:50 -05001826static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1827 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001828 if (hs->max_version < TLS1_3_VERSION || ssl->session == nullptr ||
1829 ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001830 return 0;
1831 }
1832
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001833 size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session.get()));
1834 return 15 + ssl->session->ticket.size() + binder_len;
Steven Valdez909b19f2016-11-21 15:35:44 -05001835}
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001836
Robert Sloan921ef2c2017-10-17 09:02:20 -07001837static bool ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001838 SSL *const ssl = hs->ssl;
Robert Sloanb1b54b82017-11-06 13:50:02 -08001839 hs->needs_psk_binder = false;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001840 if (hs->max_version < TLS1_3_VERSION || ssl->session == nullptr ||
1841 ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001842 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001843 }
1844
Robert Sloand9e572d2018-08-27 12:27:00 -07001845 // Per RFC 8446 section 4.1.4, skip offering the session if the selected
1846 // cipher in HelloRetryRequest does not match. This avoids performing the
1847 // transcript hash transformation for multiple hashes.
Robert Sloanb1b54b82017-11-06 13:50:02 -08001848 if (hs->received_hello_retry_request &&
Robert Sloanb1b54b82017-11-06 13:50:02 -08001849 ssl->session->cipher->algorithm_prf != hs->new_cipher->algorithm_prf) {
1850 return true;
1851 }
1852
Robert Sloan7d422bc2017-03-06 10:04:29 -08001853 struct OPENSSL_timeval now;
Steven Valdez909b19f2016-11-21 15:35:44 -05001854 ssl_get_current_time(ssl, &now);
1855 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1856 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1857
Robert Sloana27a6a42017-09-05 08:39:28 -07001858 // Fill in a placeholder zero binder of the appropriate length. It will be
1859 // computed and filled in later after length prefixes are computed.
Steven Valdez909b19f2016-11-21 15:35:44 -05001860 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001861 size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session.get()));
Steven Valdez909b19f2016-11-21 15:35:44 -05001862
1863 CBB contents, identity, ticket, binders, binder;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001864 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1865 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001866 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001867 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001868 !CBB_add_bytes(&ticket, ssl->session->ticket.data(),
1869 ssl->session->ticket.size()) ||
Steven Valdez909b19f2016-11-21 15:35:44 -05001870 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1871 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1872 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1873 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001874 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001875 }
1876
Robert Sloana27a6a42017-09-05 08:39:28 -07001877 hs->needs_psk_binder = true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001878 return CBB_flush(out);
1879}
1880
Robert Sloan921ef2c2017-10-17 09:02:20 -07001881bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1882 uint8_t *out_alert,
1883 CBS *contents) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001884 uint16_t psk_id;
1885 if (!CBS_get_u16(contents, &psk_id) ||
1886 CBS_len(contents) != 0) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001887 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001888 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001889 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001890 }
1891
Robert Sloana27a6a42017-09-05 08:39:28 -07001892 // We only advertise one PSK identity, so the only legal index is zero.
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001893 if (psk_id != 0) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001894 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001895 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001896 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001897 }
1898
Robert Sloan921ef2c2017-10-17 09:02:20 -07001899 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001900}
1901
Robert Sloan921ef2c2017-10-17 09:02:20 -07001902bool ssl_ext_pre_shared_key_parse_clienthello(
Robert Sloan1c9db532017-03-13 08:03:59 -07001903 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
1904 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001905 // We only process the first PSK identity since we don't support pure PSK.
Robert Sloan1c9db532017-03-13 08:03:59 -07001906 CBS identities, binders;
David Benjamin1b249672016-12-06 18:25:50 -05001907 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
Robert Sloan1c9db532017-03-13 08:03:59 -07001908 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
1909 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdez909b19f2016-11-21 15:35:44 -05001910 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjamin1b249672016-12-06 18:25:50 -05001911 CBS_len(&binders) == 0 ||
Steven Valdez909b19f2016-11-21 15:35:44 -05001912 CBS_len(contents) != 0) {
1913 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001914 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001915 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001916 }
1917
Steven Valdez909b19f2016-11-21 15:35:44 -05001918 *out_binders = binders;
1919
Robert Sloana27a6a42017-09-05 08:39:28 -07001920 // Check the syntax of the remaining identities, but do not process them.
David Benjamin1b249672016-12-06 18:25:50 -05001921 size_t num_identities = 1;
1922 while (CBS_len(&identities) != 0) {
1923 CBS unused_ticket;
1924 uint32_t unused_obfuscated_ticket_age;
1925 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1926 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1927 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1928 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001929 return false;
David Benjamin1b249672016-12-06 18:25:50 -05001930 }
1931
1932 num_identities++;
1933 }
1934
Robert Sloana27a6a42017-09-05 08:39:28 -07001935 // Check the syntax of the binders. The value will be checked later if
1936 // resuming.
David Benjamin1b249672016-12-06 18:25:50 -05001937 size_t num_binders = 0;
1938 while (CBS_len(&binders) != 0) {
1939 CBS binder;
1940 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1941 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1942 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001943 return false;
David Benjamin1b249672016-12-06 18:25:50 -05001944 }
1945
1946 num_binders++;
1947 }
1948
1949 if (num_identities != num_binders) {
1950 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1951 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001952 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001953 }
1954
Robert Sloan921ef2c2017-10-17 09:02:20 -07001955 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001956}
1957
Robert Sloan921ef2c2017-10-17 09:02:20 -07001958bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001959 if (!hs->ssl->s3->session_reused) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001960 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001961 }
1962
1963 CBB contents;
1964 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1965 !CBB_add_u16_length_prefixed(out, &contents) ||
Robert Sloana27a6a42017-09-05 08:39:28 -07001966 // We only consider the first identity for resumption
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001967 !CBB_add_u16(&contents, 0) ||
1968 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001969 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001970 }
1971
Robert Sloan921ef2c2017-10-17 09:02:20 -07001972 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001973}
1974
1975
Robert Sloana27a6a42017-09-05 08:39:28 -07001976// Pre-Shared Key Exchange Modes
1977//
Robert Sloand9e572d2018-08-27 12:27:00 -07001978// https://tools.ietf.org/html/rfc8446#section-4.2.9
David Benjamin1b249672016-12-06 18:25:50 -05001979
Robert Sloan921ef2c2017-10-17 09:02:20 -07001980static bool ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
1981 CBB *out) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001982 if (hs->max_version < TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001983 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05001984 }
1985
1986 CBB contents, ke_modes;
1987 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
1988 !CBB_add_u16_length_prefixed(out, &contents) ||
1989 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
1990 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001991 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05001992 }
1993
1994 return CBB_flush(out);
1995}
1996
Robert Sloan921ef2c2017-10-17 09:02:20 -07001997static bool ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
1998 uint8_t *out_alert,
1999 CBS *contents) {
Steven Valdez909b19f2016-11-21 15:35:44 -05002000 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002001 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05002002 }
2003
2004 CBS ke_modes;
2005 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2006 CBS_len(&ke_modes) == 0 ||
2007 CBS_len(contents) != 0) {
2008 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002009 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05002010 }
2011
Robert Sloana27a6a42017-09-05 08:39:28 -07002012 // We only support tickets with PSK_DHE_KE.
Robert Sloan69939df2017-01-09 10:53:07 -08002013 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2014 CBS_len(&ke_modes)) != NULL;
Steven Valdez909b19f2016-11-21 15:35:44 -05002015
Robert Sloan921ef2c2017-10-17 09:02:20 -07002016 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05002017}
2018
2019
Robert Sloana27a6a42017-09-05 08:39:28 -07002020// Early Data Indication
2021//
Robert Sloand9e572d2018-08-27 12:27:00 -07002022// https://tools.ietf.org/html/rfc8446#section-4.2.10
David Benjamin1b249672016-12-06 18:25:50 -05002023
Robert Sloan921ef2c2017-10-17 09:02:20 -07002024static bool ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002025 SSL *const ssl = hs->ssl;
Pete Bentley0c61efe2019-08-13 09:32:23 +01002026 // The second ClientHello never offers early data, and we must have already
2027 // filled in |early_data_reason| by this point.
2028 if (hs->received_hello_retry_request) {
2029 assert(ssl->s3->early_data_reason != ssl_early_data_unknown);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002030 return true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002031 }
2032
Pete Bentley0c61efe2019-08-13 09:32:23 +01002033 if (!ssl->enable_early_data) {
2034 ssl->s3->early_data_reason = ssl_early_data_disabled;
2035 return true;
2036 }
2037
2038 if (hs->max_version < TLS1_3_VERSION) {
2039 // We discard inapplicable sessions, so this is redundant with the session
2040 // checks below, but we check give a more useful reason.
2041 ssl->s3->early_data_reason = ssl_early_data_protocol_version;
2042 return true;
2043 }
2044
2045 if (ssl->session == nullptr) {
2046 ssl->s3->early_data_reason = ssl_early_data_no_session_offered;
2047 return true;
2048 }
2049
2050 if (ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION ||
2051 ssl->session->ticket_max_early_data == 0) {
2052 ssl->s3->early_data_reason = ssl_early_data_unsupported_for_session;
2053 return true;
2054 }
2055
2056 // In case ALPN preferences changed since this session was established, avoid
2057 // reporting a confusing value in |SSL_get0_alpn_selected| and sending early
2058 // data we know will be rejected.
2059 if (!ssl->session->early_alpn.empty() &&
2060 !ssl_is_alpn_protocol_allowed(hs, ssl->session->early_alpn)) {
2061 ssl->s3->early_data_reason = ssl_early_data_alpn_mismatch;
2062 return true;
2063 }
2064
2065 // |early_data_reason| will be filled in later when the server responds.
Robert Sloana27a6a42017-09-05 08:39:28 -07002066 hs->early_data_offered = true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002067
2068 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2069 !CBB_add_u16(out, 0) ||
2070 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002071 return false;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002072 }
2073
Robert Sloan921ef2c2017-10-17 09:02:20 -07002074 return true;
David Benjamin1b249672016-12-06 18:25:50 -05002075}
2076
Robert Sloan921ef2c2017-10-17 09:02:20 -07002077static bool ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
Pete Bentley0c61efe2019-08-13 09:32:23 +01002078 uint8_t *out_alert,
2079 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05002080 SSL *const ssl = hs->ssl;
2081 if (contents == NULL) {
Pete Bentley0c61efe2019-08-13 09:32:23 +01002082 if (hs->early_data_offered && !hs->received_hello_retry_request) {
2083 ssl->s3->early_data_reason = ssl->s3->session_reused
2084 ? ssl_early_data_peer_declined
2085 : ssl_early_data_session_not_resumed;
2086 } else {
2087 // We already filled in |early_data_reason| when declining to offer 0-RTT
2088 // or handling the implicit HelloRetryRequest reject.
2089 assert(ssl->s3->early_data_reason != ssl_early_data_unknown);
2090 }
Robert Sloan921ef2c2017-10-17 09:02:20 -07002091 return true;
David Benjamin1b249672016-12-06 18:25:50 -05002092 }
2093
Pete Bentley0c61efe2019-08-13 09:32:23 +01002094 // If we received an HRR, the second ClientHello never offers early data, so
2095 // the extensions logic will automatically reject early data extensions as
2096 // unsolicited. This covered by the ServerAcceptsEarlyDataOnHRR test.
2097 assert(!hs->received_hello_retry_request);
2098
David Benjamin1b249672016-12-06 18:25:50 -05002099 if (CBS_len(contents) != 0) {
2100 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002101 return false;
David Benjamin1b249672016-12-06 18:25:50 -05002102 }
2103
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002104 if (!ssl->s3->session_reused) {
2105 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2106 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002107 return false;
David Benjamin1b249672016-12-06 18:25:50 -05002108 }
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002109
Pete Bentley0c61efe2019-08-13 09:32:23 +01002110 ssl->s3->early_data_reason = ssl_early_data_accepted;
Robert Sloan0da43952018-01-03 15:13:14 -08002111 ssl->s3->early_data_accepted = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002112 return true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002113}
2114
Robert Sloan921ef2c2017-10-17 09:02:20 -07002115static bool ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2116 uint8_t *out_alert, CBS *contents) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002117 SSL *const ssl = hs->ssl;
2118 if (contents == NULL ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07002119 ssl_protocol_version(ssl) < TLS1_3_VERSION) {
2120 return true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002121 }
2122
2123 if (CBS_len(contents) != 0) {
2124 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002125 return false;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002126 }
2127
Robert Sloana27a6a42017-09-05 08:39:28 -07002128 hs->early_data_offered = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002129 return true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002130}
2131
Robert Sloan921ef2c2017-10-17 09:02:20 -07002132static bool ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloan0da43952018-01-03 15:13:14 -08002133 if (!hs->ssl->s3->early_data_accepted) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002134 return true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002135 }
2136
2137 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2138 !CBB_add_u16(out, 0) ||
2139 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002140 return false;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002141 }
2142
Robert Sloan921ef2c2017-10-17 09:02:20 -07002143 return true;
David Benjamin1b249672016-12-06 18:25:50 -05002144}
2145
2146
Robert Sloana27a6a42017-09-05 08:39:28 -07002147// Key Share
2148//
Robert Sloand9e572d2018-08-27 12:27:00 -07002149// https://tools.ietf.org/html/rfc8446#section-4.2.8
David Benjaminc895d6b2016-08-11 13:26:41 -04002150
Robert Sloan921ef2c2017-10-17 09:02:20 -07002151static bool ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05002152 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -07002153 if (hs->max_version < TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002154 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04002155 }
2156
2157 CBB contents, kse_bytes;
Robert Sloan8542c082018-02-05 09:07:34 -08002158 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002159 !CBB_add_u16_length_prefixed(out, &contents) ||
2160 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002161 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002162 }
2163
David Benjamin1b249672016-12-06 18:25:50 -05002164 uint16_t group_id = hs->retry_group;
Robert Sloan11c28bd2018-12-17 12:09:20 -08002165 uint16_t second_group_id = 0;
David Benjamin1b249672016-12-06 18:25:50 -05002166 if (hs->received_hello_retry_request) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002167 // We received a HelloRetryRequest without a new curve, so there is no new
2168 // share to append. Leave |hs->key_share| as-is.
Steven Valdez909b19f2016-11-21 15:35:44 -05002169 if (group_id == 0 &&
Robert Sloan4562e9d2017-10-02 10:26:51 -07002170 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(),
2171 hs->key_share_bytes.size())) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002172 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002173 }
Robert Sloan4562e9d2017-10-02 10:26:51 -07002174 hs->key_share_bytes.Reset();
David Benjamin95add822016-10-19 01:09:12 -04002175 if (group_id == 0) {
2176 return CBB_flush(out);
2177 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002178 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -07002179 // Add a fake group. See draft-davidben-tls-grease-01.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002180 if (ssl->ctx->grease_enabled &&
2181 (!CBB_add_u16(&kse_bytes,
Robert Sloan309a31e2018-01-29 10:22:47 -08002182 ssl_get_grease_value(hs, ssl_grease_group)) ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002183 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2184 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002185 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002186 }
2187
Robert Sloana27a6a42017-09-05 08:39:28 -07002188 // Predict the most preferred group.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002189 Span<const uint16_t> groups = tls1_get_grouplist(hs);
Robert Sloanae1abf92017-10-05 12:50:08 -07002190 if (groups.empty()) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04002191 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002192 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002193 }
David Benjamin7c0d06c2016-08-11 13:26:41 -04002194
2195 group_id = groups[0];
Robert Sloan11c28bd2018-12-17 12:09:20 -08002196
Pete Bentley0c61efe2019-08-13 09:32:23 +01002197 if (is_post_quantum_group(group_id) && groups.size() >= 2) {
2198 // CECPQ2(b) is not sent as the only initial key share. We'll include the
Robert Sloan11c28bd2018-12-17 12:09:20 -08002199 // 2nd preference group too to avoid round-trips.
2200 second_group_id = groups[1];
2201 assert(second_group_id != group_id);
2202 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002203 }
2204
David Benjamin7c0d06c2016-08-11 13:26:41 -04002205 CBB key_exchange;
Robert Sloan11c28bd2018-12-17 12:09:20 -08002206 hs->key_shares[0] = SSLKeyShare::Create(group_id);
2207 if (!hs->key_shares[0] ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07002208 !CBB_add_u16(&kse_bytes, group_id) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04002209 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
Robert Sloan11c28bd2018-12-17 12:09:20 -08002210 !hs->key_shares[0]->Offer(&key_exchange) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04002211 !CBB_flush(&kse_bytes)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002212 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002213 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002214
Robert Sloan11c28bd2018-12-17 12:09:20 -08002215 if (second_group_id != 0) {
2216 hs->key_shares[1] = SSLKeyShare::Create(second_group_id);
2217 if (!hs->key_shares[1] ||
2218 !CBB_add_u16(&kse_bytes, second_group_id) ||
2219 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2220 !hs->key_shares[1]->Offer(&key_exchange) ||
2221 !CBB_flush(&kse_bytes)) {
2222 return false;
2223 }
2224 }
2225
2226 // Save the contents of the extension to repeat it in the second
2227 // ClientHello.
Robert Sloan4562e9d2017-10-02 10:26:51 -07002228 if (!hs->received_hello_retry_request &&
2229 !hs->key_share_bytes.CopyFrom(
2230 MakeConstSpan(CBB_data(&kse_bytes), CBB_len(&kse_bytes)))) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002231 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002232 }
2233
2234 return CBB_flush(out);
2235}
2236
Robert Sloan921ef2c2017-10-17 09:02:20 -07002237bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
2238 Array<uint8_t> *out_secret,
2239 uint8_t *out_alert, CBS *contents) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002240 CBS peer_key;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002241 uint16_t group_id;
2242 if (!CBS_get_u16(contents, &group_id) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002243 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2244 CBS_len(contents) != 0) {
Robert Sloan99319a12017-11-27 10:32:46 -08002245 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04002246 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002247 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002248 }
2249
Robert Sloan11c28bd2018-12-17 12:09:20 -08002250 SSLKeyShare *key_share = hs->key_shares[0].get();
2251 if (key_share->GroupID() != group_id) {
2252 if (!hs->key_shares[1] || hs->key_shares[1]->GroupID() != group_id) {
2253 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2254 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2255 return false;
2256 }
2257 key_share = hs->key_shares[1].get();
David Benjaminc895d6b2016-08-11 13:26:41 -04002258 }
2259
Robert Sloan11c28bd2018-12-17 12:09:20 -08002260 if (!key_share->Finish(out_secret, out_alert, peer_key)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002261 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002262 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002263 }
2264
Robert Sloana94fe052017-02-21 08:49:28 -08002265 hs->new_session->group_id = group_id;
Robert Sloan11c28bd2018-12-17 12:09:20 -08002266 hs->key_shares[0].reset();
2267 hs->key_shares[1].reset();
Robert Sloan921ef2c2017-10-17 09:02:20 -07002268 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04002269}
2270
Robert Sloan921ef2c2017-10-17 09:02:20 -07002271bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
2272 Array<uint8_t> *out_secret,
2273 uint8_t *out_alert, CBS *contents) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002274 uint16_t group_id;
2275 CBS key_shares;
David Benjamin1b249672016-12-06 18:25:50 -05002276 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002277 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2278 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002279 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002280 }
2281
2282 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002283 CBS_len(contents) != 0) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04002284 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002285 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002286 }
2287
Robert Sloana27a6a42017-09-05 08:39:28 -07002288 // Find the corresponding key share.
David Benjamin7c0d06c2016-08-11 13:26:41 -04002289 CBS peer_key;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002290 CBS_init(&peer_key, NULL, 0);
David Benjaminc895d6b2016-08-11 13:26:41 -04002291 while (CBS_len(&key_shares) > 0) {
2292 uint16_t id;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002293 CBS peer_key_tmp;
David Benjaminc895d6b2016-08-11 13:26:41 -04002294 if (!CBS_get_u16(&key_shares, &id) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07002295 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp) ||
2296 CBS_len(&peer_key_tmp) == 0) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04002297 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002298 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002299 }
2300
David Benjamin7c0d06c2016-08-11 13:26:41 -04002301 if (id == group_id) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002302 if (CBS_len(&peer_key) != 0) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04002303 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2304 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002305 return false;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002306 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002307
David Benjamin7c0d06c2016-08-11 13:26:41 -04002308 peer_key = peer_key_tmp;
Robert Sloana27a6a42017-09-05 08:39:28 -07002309 // Continue parsing the structure to keep peers honest.
David Benjaminc895d6b2016-08-11 13:26:41 -04002310 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002311 }
2312
Robert Sloan921ef2c2017-10-17 09:02:20 -07002313 if (CBS_len(&peer_key) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002314 *out_found = false;
Robert Sloan4562e9d2017-10-02 10:26:51 -07002315 out_secret->Reset();
Robert Sloan921ef2c2017-10-17 09:02:20 -07002316 return true;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002317 }
2318
Robert Sloana27a6a42017-09-05 08:39:28 -07002319 // Compute the DH secret.
Robert Sloan4562e9d2017-10-02 10:26:51 -07002320 Array<uint8_t> secret;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002321 ScopedCBB public_key;
2322 UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
2323 if (!key_share ||
2324 !CBB_init(public_key.get(), 32) ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07002325 !key_share->Accept(public_key.get(), &secret, out_alert, peer_key) ||
2326 !CBBFinishArray(public_key.get(), &hs->ecdh_public_key)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002327 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002328 return false;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002329 }
2330
Robert Sloan4562e9d2017-10-02 10:26:51 -07002331 *out_secret = std::move(secret);
Robert Sloana27a6a42017-09-05 08:39:28 -07002332 *out_found = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002333 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04002334}
2335
Robert Sloan921ef2c2017-10-17 09:02:20 -07002336bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002337 uint16_t group_id;
2338 CBB kse_bytes, public_key;
David Benjamin1b249672016-12-06 18:25:50 -05002339 if (!tls1_get_shared_group(hs, &group_id) ||
Robert Sloan8542c082018-02-05 09:07:34 -08002340 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002341 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2342 !CBB_add_u16(&kse_bytes, group_id) ||
2343 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07002344 !CBB_add_bytes(&public_key, hs->ecdh_public_key.data(),
2345 hs->ecdh_public_key.size()) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002346 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002347 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002348 }
2349
Robert Sloan4562e9d2017-10-02 10:26:51 -07002350 hs->ecdh_public_key.Reset();
David Benjamin7c0d06c2016-08-11 13:26:41 -04002351
Robert Sloana94fe052017-02-21 08:49:28 -08002352 hs->new_session->group_id = group_id;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002353 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04002354}
2355
2356
Robert Sloana27a6a42017-09-05 08:39:28 -07002357// Supported Versions
2358//
Robert Sloand9e572d2018-08-27 12:27:00 -07002359// https://tools.ietf.org/html/rfc8446#section-4.2.1
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002360
Robert Sloan921ef2c2017-10-17 09:02:20 -07002361static bool ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05002362 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -07002363 if (hs->max_version <= TLS1_2_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002364 return true;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002365 }
2366
2367 CBB contents, versions;
2368 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2369 !CBB_add_u16_length_prefixed(out, &contents) ||
2370 !CBB_add_u8_length_prefixed(&contents, &versions)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002371 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002372 }
2373
Robert Sloana27a6a42017-09-05 08:39:28 -07002374 // Add a fake version. See draft-davidben-tls-grease-01.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002375 if (ssl->ctx->grease_enabled &&
Robert Sloan309a31e2018-01-29 10:22:47 -08002376 !CBB_add_u16(&versions, ssl_get_grease_value(hs, ssl_grease_version))) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002377 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002378 }
2379
Robert Sloanf6200e72017-07-10 08:09:18 -07002380 if (!ssl_add_supported_versions(hs, &versions) ||
2381 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002382 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002383 }
2384
Robert Sloan921ef2c2017-10-17 09:02:20 -07002385 return true;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002386}
2387
2388
Robert Sloana27a6a42017-09-05 08:39:28 -07002389// Cookie
2390//
Robert Sloand9e572d2018-08-27 12:27:00 -07002391// https://tools.ietf.org/html/rfc8446#section-4.2.2
David Benjamin95add822016-10-19 01:09:12 -04002392
Robert Sloan921ef2c2017-10-17 09:02:20 -07002393static bool ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloanae1abf92017-10-05 12:50:08 -07002394 if (hs->cookie.empty()) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002395 return true;
David Benjamin95add822016-10-19 01:09:12 -04002396 }
2397
2398 CBB contents, cookie;
2399 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2400 !CBB_add_u16_length_prefixed(out, &contents) ||
2401 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07002402 !CBB_add_bytes(&cookie, hs->cookie.data(), hs->cookie.size()) ||
David Benjamin95add822016-10-19 01:09:12 -04002403 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002404 return false;
David Benjamin95add822016-10-19 01:09:12 -04002405 }
2406
Robert Sloana27a6a42017-09-05 08:39:28 -07002407 // The cookie is no longer needed in memory.
Robert Sloan4562e9d2017-10-02 10:26:51 -07002408 hs->cookie.Reset();
Robert Sloan921ef2c2017-10-17 09:02:20 -07002409 return true;
David Benjamin95add822016-10-19 01:09:12 -04002410}
2411
2412
Robert Sloand9e572d2018-08-27 12:27:00 -07002413// Supported Groups
Robert Sloan0db7f542018-01-16 15:48:33 -08002414//
Robert Sloand9e572d2018-08-27 12:27:00 -07002415// https://tools.ietf.org/html/rfc4492#section-5.1.1
2416// https://tools.ietf.org/html/rfc8446#section-4.2.7
Kenny Rootb8494592015-09-25 02:29:14 +00002417
Robert Sloan921ef2c2017-10-17 09:02:20 -07002418static bool ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05002419 SSL *const ssl = hs->ssl;
David Benjamind316cba2016-06-02 16:17:39 -04002420 CBB contents, groups_bytes;
2421 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Kenny Rootb8494592015-09-25 02:29:14 +00002422 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamind316cba2016-06-02 16:17:39 -04002423 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002424 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00002425 }
2426
Robert Sloana27a6a42017-09-05 08:39:28 -07002427 // Add a fake group. See draft-davidben-tls-grease-01.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002428 if (ssl->ctx->grease_enabled &&
2429 !CBB_add_u16(&groups_bytes,
Robert Sloan309a31e2018-01-29 10:22:47 -08002430 ssl_get_grease_value(hs, ssl_grease_group))) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002431 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002432 }
2433
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002434 for (uint16_t group : tls1_get_grouplist(hs)) {
Pete Bentley0c61efe2019-08-13 09:32:23 +01002435 if (is_post_quantum_group(group) &&
Robert Sloan11c28bd2018-12-17 12:09:20 -08002436 hs->max_version < TLS1_3_VERSION) {
2437 continue;
2438 }
Robert Sloan4562e9d2017-10-02 10:26:51 -07002439 if (!CBB_add_u16(&groups_bytes, group)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002440 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00002441 }
2442 }
2443
2444 return CBB_flush(out);
2445}
2446
Robert Sloan921ef2c2017-10-17 09:02:20 -07002447static bool ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2448 uint8_t *out_alert,
2449 CBS *contents) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002450 // This extension is not expected to be echoed by servers in TLS 1.2, but some
2451 // BigIP servers send it nonetheless, so do not enforce this.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002452 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00002453}
2454
Robert Sloan4562e9d2017-10-02 10:26:51 -07002455static bool parse_u16_array(const CBS *cbs, Array<uint16_t> *out) {
2456 CBS copy = *cbs;
2457 if ((CBS_len(&copy) & 1) != 0) {
2458 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2459 return false;
2460 }
2461
2462 Array<uint16_t> ret;
2463 if (!ret.Init(CBS_len(&copy) / 2)) {
2464 return false;
2465 }
2466 for (size_t i = 0; i < ret.size(); i++) {
2467 if (!CBS_get_u16(&copy, &ret[i])) {
2468 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2469 return false;
2470 }
2471 }
2472
2473 assert(CBS_len(&copy) == 0);
2474 *out = std::move(ret);
2475 return 1;
2476}
2477
Robert Sloan921ef2c2017-10-17 09:02:20 -07002478static bool ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin1b249672016-12-06 18:25:50 -05002479 uint8_t *out_alert,
Robert Sloan921ef2c2017-10-17 09:02:20 -07002480 CBS *contents) {
Kenny Rootb8494592015-09-25 02:29:14 +00002481 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002482 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00002483 }
2484
David Benjamind316cba2016-06-02 16:17:39 -04002485 CBS supported_group_list;
2486 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2487 CBS_len(&supported_group_list) == 0 ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07002488 CBS_len(contents) != 0 ||
2489 !parse_u16_array(&supported_group_list, &hs->peer_supported_group_list)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002490 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00002491 }
2492
Robert Sloan921ef2c2017-10-17 09:02:20 -07002493 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00002494}
2495
Robert Sloan978112c2018-01-22 12:53:01 -08002496// Token Binding
2497//
2498// https://tools.ietf.org/html/draft-ietf-tokbind-negotiation-10
2499
2500// The Token Binding version number currently matches the draft number of
2501// draft-ietf-tokbind-protocol, and when published as an RFC it will be 0x0100.
2502// Since there are no wire changes to the protocol from draft 13 through the
2503// current draft (16), this implementation supports all versions in that range.
2504static uint16_t kTokenBindingMaxVersion = 16;
2505static uint16_t kTokenBindingMinVersion = 13;
2506
2507static bool ext_token_binding_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2508 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002509 if (hs->config->token_binding_params.empty() || SSL_is_dtls(ssl)) {
Robert Sloan978112c2018-01-22 12:53:01 -08002510 return true;
2511 }
2512
2513 CBB contents, params;
2514 if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) ||
2515 !CBB_add_u16_length_prefixed(out, &contents) ||
2516 !CBB_add_u16(&contents, kTokenBindingMaxVersion) ||
2517 !CBB_add_u8_length_prefixed(&contents, &params) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002518 !CBB_add_bytes(&params, hs->config->token_binding_params.data(),
2519 hs->config->token_binding_params.size()) ||
Robert Sloan978112c2018-01-22 12:53:01 -08002520 !CBB_flush(out)) {
2521 return false;
2522 }
2523
2524 return true;
2525}
2526
2527static bool ext_token_binding_parse_serverhello(SSL_HANDSHAKE *hs,
2528 uint8_t *out_alert,
2529 CBS *contents) {
2530 SSL *const ssl = hs->ssl;
2531 if (contents == nullptr) {
2532 return true;
2533 }
2534
2535 CBS params_list;
2536 uint16_t version;
2537 uint8_t param;
2538 if (!CBS_get_u16(contents, &version) ||
2539 !CBS_get_u8_length_prefixed(contents, &params_list) ||
2540 !CBS_get_u8(&params_list, &param) ||
2541 CBS_len(&params_list) > 0 ||
2542 CBS_len(contents) > 0) {
2543 *out_alert = SSL_AD_DECODE_ERROR;
2544 return false;
2545 }
2546
2547 // The server-negotiated version must be less than or equal to our version.
2548 if (version > kTokenBindingMaxVersion) {
2549 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2550 return false;
2551 }
2552
2553 // If the server-selected version is less than what we support, then Token
2554 // Binding wasn't negotiated (but the extension was parsed successfully).
2555 if (version < kTokenBindingMinVersion) {
2556 return true;
2557 }
2558
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002559 for (uint8_t config_param : hs->config->token_binding_params) {
2560 if (param == config_param) {
Robert Sloan15c0b352018-04-16 08:36:46 -07002561 ssl->s3->negotiated_token_binding_param = param;
2562 ssl->s3->token_binding_negotiated = true;
Robert Sloan978112c2018-01-22 12:53:01 -08002563 return true;
2564 }
2565 }
2566
2567 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2568 return false;
2569}
2570
2571// select_tb_param looks for the first token binding param in
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002572// |hs->ssl->token_binding_params| that is also in |params| and puts it in
2573// |hs->ssl->negotiated_token_binding_param|. It returns true if a token binding
Robert Sloan978112c2018-01-22 12:53:01 -08002574// param is found, and false otherwise.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002575static bool select_tb_param(SSL_HANDSHAKE *hs,
2576 Span<const uint8_t> peer_params) {
2577 for (uint8_t tb_param : hs->config->token_binding_params) {
Robert Sloan978112c2018-01-22 12:53:01 -08002578 for (uint8_t peer_param : peer_params) {
2579 if (tb_param == peer_param) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002580 hs->ssl->s3->negotiated_token_binding_param = tb_param;
Robert Sloan978112c2018-01-22 12:53:01 -08002581 return true;
2582 }
2583 }
2584 }
2585 return false;
2586}
2587
2588static bool ext_token_binding_parse_clienthello(SSL_HANDSHAKE *hs,
2589 uint8_t *out_alert,
2590 CBS *contents) {
2591 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002592 if (contents == nullptr || hs->config->token_binding_params.empty()) {
Robert Sloan978112c2018-01-22 12:53:01 -08002593 return true;
2594 }
2595
2596 CBS params;
2597 uint16_t version;
2598 if (!CBS_get_u16(contents, &version) ||
2599 !CBS_get_u8_length_prefixed(contents, &params) ||
2600 CBS_len(&params) == 0 ||
2601 CBS_len(contents) > 0) {
2602 *out_alert = SSL_AD_DECODE_ERROR;
2603 return false;
2604 }
2605
2606 // If the client-selected version is less than what we support, then Token
2607 // Binding wasn't negotiated (but the extension was parsed successfully).
2608 if (version < kTokenBindingMinVersion) {
2609 return true;
2610 }
2611
2612 // If the client-selected version is higher than we support, use our max
2613 // version. Otherwise, use the client's version.
2614 hs->negotiated_token_binding_version =
2615 std::min(version, kTokenBindingMaxVersion);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002616 if (!select_tb_param(hs, params)) {
Robert Sloan978112c2018-01-22 12:53:01 -08002617 return true;
2618 }
2619
Robert Sloan15c0b352018-04-16 08:36:46 -07002620 ssl->s3->token_binding_negotiated = true;
Robert Sloan978112c2018-01-22 12:53:01 -08002621 return true;
2622}
2623
2624static bool ext_token_binding_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2625 SSL *const ssl = hs->ssl;
2626
Robert Sloan15c0b352018-04-16 08:36:46 -07002627 if (!ssl->s3->token_binding_negotiated) {
Robert Sloan978112c2018-01-22 12:53:01 -08002628 return true;
2629 }
2630
2631 CBB contents, params;
2632 if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) ||
2633 !CBB_add_u16_length_prefixed(out, &contents) ||
2634 !CBB_add_u16(&contents, hs->negotiated_token_binding_version) ||
2635 !CBB_add_u8_length_prefixed(&contents, &params) ||
Robert Sloan15c0b352018-04-16 08:36:46 -07002636 !CBB_add_u8(&params, ssl->s3->negotiated_token_binding_param) ||
Robert Sloan978112c2018-01-22 12:53:01 -08002637 !CBB_flush(out)) {
2638 return false;
2639 }
2640
2641 return true;
2642}
Kenny Rootb8494592015-09-25 02:29:14 +00002643
Robert Sloan8542c082018-02-05 09:07:34 -08002644// QUIC Transport Parameters
2645
2646static bool ext_quic_transport_params_add_clienthello(SSL_HANDSHAKE *hs,
2647 CBB *out) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002648 if (hs->config->quic_transport_params.empty() ||
2649 hs->max_version <= TLS1_2_VERSION) {
Robert Sloan8542c082018-02-05 09:07:34 -08002650 return true;
2651 }
2652
2653 CBB contents;
2654 if (!CBB_add_u16(out, TLSEXT_TYPE_quic_transport_parameters) ||
2655 !CBB_add_u16_length_prefixed(out, &contents) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002656 !CBB_add_bytes(&contents, hs->config->quic_transport_params.data(),
2657 hs->config->quic_transport_params.size()) ||
Robert Sloan8542c082018-02-05 09:07:34 -08002658 !CBB_flush(out)) {
2659 return false;
2660 }
2661 return true;
2662}
2663
2664static bool ext_quic_transport_params_parse_serverhello(SSL_HANDSHAKE *hs,
2665 uint8_t *out_alert,
2666 CBS *contents) {
2667 SSL *const ssl = hs->ssl;
2668 if (contents == nullptr) {
2669 return true;
2670 }
2671 // QUIC requires TLS 1.3.
2672 if (ssl_protocol_version(ssl) < TLS1_3_VERSION) {
2673 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2674 return false;
2675 }
2676
2677 return ssl->s3->peer_quic_transport_params.CopyFrom(*contents);
2678}
2679
2680static bool ext_quic_transport_params_parse_clienthello(SSL_HANDSHAKE *hs,
2681 uint8_t *out_alert,
2682 CBS *contents) {
2683 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002684 if (!contents || hs->config->quic_transport_params.empty()) {
Robert Sloan8542c082018-02-05 09:07:34 -08002685 return true;
2686 }
2687 // Ignore the extension before TLS 1.3.
2688 if (ssl_protocol_version(ssl) < TLS1_3_VERSION) {
2689 return true;
2690 }
2691
2692 return ssl->s3->peer_quic_transport_params.CopyFrom(*contents);
2693}
2694
2695static bool ext_quic_transport_params_add_serverhello(SSL_HANDSHAKE *hs,
2696 CBB *out) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002697 if (hs->config->quic_transport_params.empty()) {
Robert Sloan8542c082018-02-05 09:07:34 -08002698 return true;
2699 }
2700
2701 CBB contents;
2702 if (!CBB_add_u16(out, TLSEXT_TYPE_quic_transport_parameters) ||
2703 !CBB_add_u16_length_prefixed(out, &contents) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002704 !CBB_add_bytes(&contents, hs->config->quic_transport_params.data(),
2705 hs->config->quic_transport_params.size()) ||
Robert Sloan8542c082018-02-05 09:07:34 -08002706 !CBB_flush(out)) {
2707 return false;
2708 }
2709
2710 return true;
2711}
2712
Robert Sloan4c22c5f2019-03-01 15:53:37 -08002713// Delegated credentials.
2714//
2715// https://tools.ietf.org/html/draft-ietf-tls-subcerts
2716
2717static bool ext_delegated_credential_add_clienthello(SSL_HANDSHAKE *hs,
2718 CBB *out) {
2719 return true;
2720}
2721
2722static bool ext_delegated_credential_parse_clienthello(SSL_HANDSHAKE *hs,
2723 uint8_t *out_alert,
2724 CBS *contents) {
2725 assert(TLSEXT_TYPE_delegated_credential == 0xff02);
2726 // TODO: Check that the extension is empty.
2727 //
Robert Sloan4726ed32019-04-08 12:43:32 -07002728 // As of draft-03, the client sends an empty extension in order indicate
Robert Sloan4c22c5f2019-03-01 15:53:37 -08002729 // support for delegated credentials. This could change, however, since the
2730 // spec is not yet finalized. This assertion is here to remind us to enforce
2731 // this check once the extension ID is assigned.
2732
2733 if (contents == nullptr || ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) {
2734 // Don't use delegated credentials unless we're negotiating TLS 1.3 or
2735 // higher.
2736 return true;
2737 }
2738
2739 hs->delegated_credential_requested = true;
2740 return true;
2741}
2742
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01002743// Certificate compression
2744
2745static bool cert_compression_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2746 bool first = true;
2747 CBB contents, algs;
2748
2749 for (const auto *alg : hs->ssl->ctx->cert_compression_algs.get()) {
2750 if (alg->decompress == nullptr) {
2751 continue;
2752 }
2753
2754 if (first && (!CBB_add_u16(out, TLSEXT_TYPE_cert_compression) ||
2755 !CBB_add_u16_length_prefixed(out, &contents) ||
2756 !CBB_add_u8_length_prefixed(&contents, &algs))) {
2757 return false;
2758 }
2759 first = false;
2760 if (!CBB_add_u16(&algs, alg->alg_id)) {
2761 return false;
2762 }
2763 }
2764
2765 return first || CBB_flush(out);
2766}
2767
2768static bool cert_compression_parse_serverhello(SSL_HANDSHAKE *hs,
2769 uint8_t *out_alert,
2770 CBS *contents) {
2771 if (contents == nullptr) {
2772 return true;
2773 }
2774
2775 // The server may not echo this extension. Any server to client negotiation is
2776 // advertised in the CertificateRequest message.
2777 return false;
2778}
2779
2780static bool cert_compression_parse_clienthello(SSL_HANDSHAKE *hs,
2781 uint8_t *out_alert,
2782 CBS *contents) {
2783 if (contents == nullptr) {
2784 return true;
2785 }
2786
2787 const size_t num_algs =
2788 sk_CertCompressionAlg_num(hs->ssl->ctx->cert_compression_algs.get());
2789
2790 CBS alg_ids;
2791 if (!CBS_get_u8_length_prefixed(contents, &alg_ids) ||
2792 CBS_len(contents) != 0 ||
2793 CBS_len(&alg_ids) == 0 ||
2794 CBS_len(&alg_ids) % 2 == 1) {
2795 return false;
2796 }
2797
2798 const size_t num_given_alg_ids = CBS_len(&alg_ids) / 2;
2799 Array<uint16_t> given_alg_ids;
2800 if (!given_alg_ids.Init(num_given_alg_ids)) {
2801 return false;
2802 }
2803
2804 size_t best_index = num_algs;
2805 size_t given_alg_idx = 0;
2806
2807 while (CBS_len(&alg_ids) > 0) {
2808 uint16_t alg_id;
2809 if (!CBS_get_u16(&alg_ids, &alg_id)) {
2810 return false;
2811 }
2812
2813 given_alg_ids[given_alg_idx++] = alg_id;
2814
2815 for (size_t i = 0; i < num_algs; i++) {
2816 const auto *alg = sk_CertCompressionAlg_value(
2817 hs->ssl->ctx->cert_compression_algs.get(), i);
2818 if (alg->alg_id == alg_id && alg->compress != nullptr) {
2819 if (i < best_index) {
2820 best_index = i;
2821 }
2822 break;
2823 }
2824 }
2825 }
2826
2827 qsort(given_alg_ids.data(), given_alg_ids.size(), sizeof(uint16_t),
2828 compare_uint16_t);
2829 for (size_t i = 1; i < num_given_alg_ids; i++) {
2830 if (given_alg_ids[i - 1] == given_alg_ids[i]) {
2831 return false;
2832 }
2833 }
2834
2835 if (best_index < num_algs &&
2836 ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
2837 hs->cert_compression_negotiated = true;
2838 hs->cert_compression_alg_id =
2839 sk_CertCompressionAlg_value(hs->ssl->ctx->cert_compression_algs.get(),
2840 best_index)
2841 ->alg_id;
2842 }
2843
2844 return true;
2845}
2846
2847static bool cert_compression_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2848 return true;
2849}
Robert Sloan8542c082018-02-05 09:07:34 -08002850
Pete Bentley0c61efe2019-08-13 09:32:23 +01002851
2852// Post-quantum experiment signal
2853//
2854// This extension may be used in order to identify a control group for
2855// experimenting with post-quantum key exchange algorithms.
2856
2857static bool ext_pq_experiment_signal_add_clienthello(SSL_HANDSHAKE *hs,
2858 CBB *out) {
2859 if (hs->ssl->ctx->pq_experiment_signal &&
2860 (!CBB_add_u16(out, TLSEXT_TYPE_pq_experiment_signal) ||
2861 !CBB_add_u16(out, 0))) {
2862 return false;
2863 }
2864
2865 return true;
2866}
2867
2868static bool ext_pq_experiment_signal_parse_serverhello(SSL_HANDSHAKE *hs,
2869 uint8_t *out_alert,
2870 CBS *contents) {
2871 if (contents == nullptr) {
2872 return true;
2873 }
2874
2875 if (!hs->ssl->ctx->pq_experiment_signal || CBS_len(contents) != 0) {
2876 return false;
2877 }
2878
2879 hs->ssl->s3->pq_experiment_signal_seen = true;
2880 return true;
2881}
2882
2883static bool ext_pq_experiment_signal_parse_clienthello(SSL_HANDSHAKE *hs,
2884 uint8_t *out_alert,
2885 CBS *contents) {
2886 if (contents == nullptr) {
2887 return true;
2888 }
2889
2890 if (CBS_len(contents) != 0) {
2891 return false;
2892 }
2893
2894 if (hs->ssl->ctx->pq_experiment_signal) {
2895 hs->ssl->s3->pq_experiment_signal_seen = true;
2896 }
2897
2898 return true;
2899}
2900
2901static bool ext_pq_experiment_signal_add_serverhello(SSL_HANDSHAKE *hs,
2902 CBB *out) {
2903 if (hs->ssl->s3->pq_experiment_signal_seen &&
2904 (!CBB_add_u16(out, TLSEXT_TYPE_pq_experiment_signal) ||
2905 !CBB_add_u16(out, 0))) {
2906 return false;
2907 }
2908
2909 return true;
2910}
2911
Robert Sloana27a6a42017-09-05 08:39:28 -07002912// kExtensions contains all the supported extensions.
Kenny Rootb8494592015-09-25 02:29:14 +00002913static const struct tls_extension kExtensions[] = {
2914 {
Kenny Rootb8494592015-09-25 02:29:14 +00002915 TLSEXT_TYPE_server_name,
David Benjamin95add822016-10-19 01:09:12 -04002916 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002917 ext_sni_add_clienthello,
2918 ext_sni_parse_serverhello,
2919 ext_sni_parse_clienthello,
2920 ext_sni_add_serverhello,
2921 },
2922 {
2923 TLSEXT_TYPE_extended_master_secret,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002924 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002925 ext_ems_add_clienthello,
2926 ext_ems_parse_serverhello,
2927 ext_ems_parse_clienthello,
2928 ext_ems_add_serverhello,
2929 },
2930 {
Robert Sloan726e9d12018-09-11 11:45:04 -07002931 TLSEXT_TYPE_renegotiate,
2932 NULL,
2933 ext_ri_add_clienthello,
2934 ext_ri_parse_serverhello,
2935 ext_ri_parse_clienthello,
2936 ext_ri_add_serverhello,
2937 },
2938 {
2939 TLSEXT_TYPE_supported_groups,
2940 NULL,
2941 ext_supported_groups_add_clienthello,
2942 ext_supported_groups_parse_serverhello,
2943 ext_supported_groups_parse_clienthello,
2944 dont_add_serverhello,
2945 },
2946 {
2947 TLSEXT_TYPE_ec_point_formats,
2948 NULL,
2949 ext_ec_point_add_clienthello,
2950 ext_ec_point_parse_serverhello,
2951 ext_ec_point_parse_clienthello,
2952 ext_ec_point_add_serverhello,
2953 },
2954 {
Kenny Rootb8494592015-09-25 02:29:14 +00002955 TLSEXT_TYPE_session_ticket,
2956 NULL,
2957 ext_ticket_add_clienthello,
2958 ext_ticket_parse_serverhello,
Robert Sloana27a6a42017-09-05 08:39:28 -07002959 // Ticket extension client parsing is handled in ssl_session.c
David Benjaminc895d6b2016-08-11 13:26:41 -04002960 ignore_parse_clienthello,
Kenny Rootb8494592015-09-25 02:29:14 +00002961 ext_ticket_add_serverhello,
2962 },
2963 {
Robert Sloan726e9d12018-09-11 11:45:04 -07002964 TLSEXT_TYPE_application_layer_protocol_negotiation,
2965 NULL,
2966 ext_alpn_add_clienthello,
2967 ext_alpn_parse_serverhello,
2968 // ALPN is negotiated late in |ssl_negotiate_alpn|.
2969 ignore_parse_clienthello,
2970 ext_alpn_add_serverhello,
2971 },
2972 {
2973 TLSEXT_TYPE_status_request,
2974 NULL,
2975 ext_ocsp_add_clienthello,
2976 ext_ocsp_parse_serverhello,
2977 ext_ocsp_parse_clienthello,
2978 ext_ocsp_add_serverhello,
2979 },
2980 {
Kenny Rootb8494592015-09-25 02:29:14 +00002981 TLSEXT_TYPE_signature_algorithms,
2982 NULL,
2983 ext_sigalgs_add_clienthello,
David Benjaminc895d6b2016-08-11 13:26:41 -04002984 forbid_parse_serverhello,
Kenny Rootb8494592015-09-25 02:29:14 +00002985 ext_sigalgs_parse_clienthello,
David Benjaminc895d6b2016-08-11 13:26:41 -04002986 dont_add_serverhello,
Kenny Rootb8494592015-09-25 02:29:14 +00002987 },
2988 {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07002989 TLSEXT_TYPE_signature_algorithms_cert,
2990 NULL,
2991 ext_sigalgs_cert_add_clienthello,
2992 forbid_parse_serverhello,
2993 ignore_parse_clienthello,
2994 dont_add_serverhello,
2995 },
2996 {
Kenny Rootb8494592015-09-25 02:29:14 +00002997 TLSEXT_TYPE_next_proto_neg,
David Benjamin95add822016-10-19 01:09:12 -04002998 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002999 ext_npn_add_clienthello,
3000 ext_npn_parse_serverhello,
3001 ext_npn_parse_clienthello,
3002 ext_npn_add_serverhello,
3003 },
3004 {
3005 TLSEXT_TYPE_certificate_timestamp,
3006 NULL,
3007 ext_sct_add_clienthello,
3008 ext_sct_parse_serverhello,
3009 ext_sct_parse_clienthello,
3010 ext_sct_add_serverhello,
3011 },
3012 {
Kenny Rootb8494592015-09-25 02:29:14 +00003013 TLSEXT_TYPE_channel_id,
3014 ext_channel_id_init,
3015 ext_channel_id_add_clienthello,
3016 ext_channel_id_parse_serverhello,
3017 ext_channel_id_parse_clienthello,
3018 ext_channel_id_add_serverhello,
3019 },
3020 {
3021 TLSEXT_TYPE_srtp,
3022 ext_srtp_init,
3023 ext_srtp_add_clienthello,
3024 ext_srtp_parse_serverhello,
3025 ext_srtp_parse_clienthello,
3026 ext_srtp_add_serverhello,
3027 },
3028 {
Robert Sloan8542c082018-02-05 09:07:34 -08003029 TLSEXT_TYPE_key_share,
David Benjaminc895d6b2016-08-11 13:26:41 -04003030 NULL,
3031 ext_key_share_add_clienthello,
3032 forbid_parse_serverhello,
3033 ignore_parse_clienthello,
3034 dont_add_serverhello,
3035 },
David Benjaminf0c4a6c2016-08-11 13:26:41 -04003036 {
Steven Valdez909b19f2016-11-21 15:35:44 -05003037 TLSEXT_TYPE_psk_key_exchange_modes,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04003038 NULL,
Steven Valdez909b19f2016-11-21 15:35:44 -05003039 ext_psk_key_exchange_modes_add_clienthello,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04003040 forbid_parse_serverhello,
Steven Valdez909b19f2016-11-21 15:35:44 -05003041 ext_psk_key_exchange_modes_parse_clienthello,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04003042 dont_add_serverhello,
3043 },
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003044 {
David Benjamin1b249672016-12-06 18:25:50 -05003045 TLSEXT_TYPE_early_data,
3046 NULL,
3047 ext_early_data_add_clienthello,
Robert Sloan6d0d00e2017-03-27 07:13:07 -07003048 ext_early_data_parse_serverhello,
David Benjamin1b249672016-12-06 18:25:50 -05003049 ext_early_data_parse_clienthello,
Robert Sloan6d0d00e2017-03-27 07:13:07 -07003050 ext_early_data_add_serverhello,
David Benjamin1b249672016-12-06 18:25:50 -05003051 },
3052 {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003053 TLSEXT_TYPE_supported_versions,
3054 NULL,
3055 ext_supported_versions_add_clienthello,
3056 forbid_parse_serverhello,
3057 ignore_parse_clienthello,
3058 dont_add_serverhello,
3059 },
David Benjamin95add822016-10-19 01:09:12 -04003060 {
3061 TLSEXT_TYPE_cookie,
3062 NULL,
3063 ext_cookie_add_clienthello,
3064 forbid_parse_serverhello,
3065 ignore_parse_clienthello,
3066 dont_add_serverhello,
3067 },
Robert Sloan0db7f542018-01-16 15:48:33 -08003068 {
Robert Sloan8542c082018-02-05 09:07:34 -08003069 TLSEXT_TYPE_quic_transport_parameters,
3070 NULL,
3071 ext_quic_transport_params_add_clienthello,
3072 ext_quic_transport_params_parse_serverhello,
3073 ext_quic_transport_params_parse_clienthello,
3074 ext_quic_transport_params_add_serverhello,
3075 },
Kenny Rootb8494592015-09-25 02:29:14 +00003076 {
Robert Sloan978112c2018-01-22 12:53:01 -08003077 TLSEXT_TYPE_token_binding,
3078 NULL,
3079 ext_token_binding_add_clienthello,
3080 ext_token_binding_parse_serverhello,
3081 ext_token_binding_parse_clienthello,
3082 ext_token_binding_add_serverhello,
3083 },
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003084 {
3085 TLSEXT_TYPE_cert_compression,
3086 NULL,
3087 cert_compression_add_clienthello,
3088 cert_compression_parse_serverhello,
3089 cert_compression_parse_clienthello,
3090 cert_compression_add_serverhello,
3091 },
Robert Sloan4c22c5f2019-03-01 15:53:37 -08003092 {
3093 TLSEXT_TYPE_delegated_credential,
3094 NULL,
3095 ext_delegated_credential_add_clienthello,
3096 forbid_parse_serverhello,
3097 ext_delegated_credential_parse_clienthello,
3098 dont_add_serverhello,
3099 },
Pete Bentley0c61efe2019-08-13 09:32:23 +01003100 {
3101 TLSEXT_TYPE_pq_experiment_signal,
3102 NULL,
3103 ext_pq_experiment_signal_add_clienthello,
3104 ext_pq_experiment_signal_parse_serverhello,
3105 ext_pq_experiment_signal_parse_clienthello,
3106 ext_pq_experiment_signal_add_serverhello,
3107 },
Kenny Rootb8494592015-09-25 02:29:14 +00003108};
3109
3110#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
3111
Robert Sloana12bf462017-07-17 07:08:26 -07003112static_assert(kNumExtensions <=
3113 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
3114 "too many extensions for sent bitset");
3115static_assert(kNumExtensions <=
3116 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
3117 "too many extensions for received bitset");
Kenny Rootb8494592015-09-25 02:29:14 +00003118
3119static const struct tls_extension *tls_extension_find(uint32_t *out_index,
3120 uint16_t value) {
3121 unsigned i;
3122 for (i = 0; i < kNumExtensions; i++) {
3123 if (kExtensions[i].value == value) {
3124 *out_index = i;
3125 return &kExtensions[i];
3126 }
3127 }
3128
3129 return NULL;
3130}
3131
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003132bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out,
3133 size_t header_len) {
David Benjamin1b249672016-12-06 18:25:50 -05003134 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -08003135 CBB extensions;
3136 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Robert Sloana12bf462017-07-17 07:08:26 -07003137 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003138 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003139 }
3140
Pete Bentley0c61efe2019-08-13 09:32:23 +01003141 // Note we may send multiple ClientHellos for DTLS HelloVerifyRequest and TLS
3142 // 1.3 HelloRetryRequest. For the latter, the extensions may change, so it is
3143 // important to reset this value.
David Benjamin1b249672016-12-06 18:25:50 -05003144 hs->extensions.sent = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003145
David Benjamin7c0d06c2016-08-11 13:26:41 -04003146 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00003147 if (kExtensions[i].init != NULL) {
David Benjamin1b249672016-12-06 18:25:50 -05003148 kExtensions[i].init(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003149 }
3150 }
3151
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003152 uint16_t grease_ext1 = 0;
3153 if (ssl->ctx->grease_enabled) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003154 // Add a fake empty extension. See draft-davidben-tls-grease-01.
Robert Sloan309a31e2018-01-29 10:22:47 -08003155 grease_ext1 = ssl_get_grease_value(hs, ssl_grease_extension1);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003156 if (!CBB_add_u16(&extensions, grease_ext1) ||
3157 !CBB_add_u16(&extensions, 0 /* zero length */)) {
Robert Sloana12bf462017-07-17 07:08:26 -07003158 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003159 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003160 }
3161 }
3162
Robert Sloand9e572d2018-08-27 12:27:00 -07003163 bool last_was_empty = false;
David Benjamin7c0d06c2016-08-11 13:26:41 -04003164 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00003165 const size_t len_before = CBB_len(&extensions);
David Benjamin1b249672016-12-06 18:25:50 -05003166 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003167 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003168 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003169 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003170 }
Kenny Rootb8494592015-09-25 02:29:14 +00003171
Robert Sloand9e572d2018-08-27 12:27:00 -07003172 const size_t bytes_written = CBB_len(&extensions) - len_before;
3173 if (bytes_written != 0) {
David Benjamin1b249672016-12-06 18:25:50 -05003174 hs->extensions.sent |= (1u << i);
Kenny Rootb8494592015-09-25 02:29:14 +00003175 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003176 // If the difference in lengths is only four bytes then the extension had
3177 // an empty body.
3178 last_was_empty = (bytes_written == 4);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003179 }
3180
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003181 if (ssl->ctx->grease_enabled) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003182 // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
Robert Sloan309a31e2018-01-29 10:22:47 -08003183 uint16_t grease_ext2 = ssl_get_grease_value(hs, ssl_grease_extension2);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003184
Robert Sloana27a6a42017-09-05 08:39:28 -07003185 // The two fake extensions must not have the same value. GREASE values are
3186 // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
3187 // one.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003188 if (grease_ext1 == grease_ext2) {
3189 grease_ext2 ^= 0x1010;
3190 }
3191
3192 if (!CBB_add_u16(&extensions, grease_ext2) ||
3193 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
3194 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
Robert Sloana12bf462017-07-17 07:08:26 -07003195 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003196 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003197 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003198
3199 last_was_empty = false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003200 }
3201
David Benjaminc895d6b2016-08-11 13:26:41 -04003202 if (!SSL_is_dtls(ssl)) {
David Benjamin1b249672016-12-06 18:25:50 -05003203 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdez909b19f2016-11-21 15:35:44 -05003204 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Robert Sloand9e572d2018-08-27 12:27:00 -07003205 size_t padding_len = 0;
3206
3207 // The final extension must be non-empty. WebSphere Application
3208 // Server 7.0 is intolerant to the last extension being zero-length. See
3209 // https://crbug.com/363583.
3210 if (last_was_empty && psk_extension_len == 0) {
3211 padding_len = 1;
3212 // The addition of the padding extension may push us into the F5 bug.
3213 header_len += 4 + padding_len;
3214 }
3215
3216 // Add padding to workaround bugs in F5 terminators. See RFC 7685.
3217 //
3218 // NB: because this code works out the length of all existing extensions
3219 // it MUST always appear last (save for any PSK extension).
Adam Langleyd9e397b2015-01-22 14:27:53 -08003220 if (header_len > 0xff && header_len < 0x200) {
Robert Sloand9e572d2018-08-27 12:27:00 -07003221 // If our calculations already included a padding extension, remove that
3222 // factor because we're about to change its length.
3223 if (padding_len != 0) {
3224 header_len -= 4 + padding_len;
3225 }
3226 padding_len = 0x200 - header_len;
Robert Sloana27a6a42017-09-05 08:39:28 -07003227 // Extensions take at least four bytes to encode. Always include at least
3228 // one byte of data if including the extension. WebSphere Application
3229 // Server 7.0 is intolerant to the last extension being zero-length. See
3230 // https://crbug.com/363583.
Adam Langleyd9e397b2015-01-22 14:27:53 -08003231 if (padding_len >= 4 + 1) {
3232 padding_len -= 4;
3233 } else {
3234 padding_len = 1;
3235 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003236 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003237
Robert Sloand9e572d2018-08-27 12:27:00 -07003238 if (padding_len != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00003239 uint8_t *padding_bytes;
3240 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
3241 !CBB_add_u16(&extensions, padding_len) ||
3242 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
Robert Sloana12bf462017-07-17 07:08:26 -07003243 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003244 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003245 }
3246
Robert Sloan69939df2017-01-09 10:53:07 -08003247 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003248 }
3249 }
3250
Robert Sloana27a6a42017-09-05 08:39:28 -07003251 // The PSK extension must be last, including after the padding.
David Benjamin1b249672016-12-06 18:25:50 -05003252 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Robert Sloana12bf462017-07-17 07:08:26 -07003253 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003254 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003255 }
3256
Robert Sloana27a6a42017-09-05 08:39:28 -07003257 // Discard empty extensions blocks.
Adam Langley4139edb2016-01-13 15:00:54 -08003258 if (CBB_len(&extensions) == 0) {
Kenny Roote99801b2015-11-06 15:31:15 -08003259 CBB_discard_child(out);
Kenny Rootb8494592015-09-25 02:29:14 +00003260 }
3261
Kenny Roote99801b2015-11-06 15:31:15 -08003262 return CBB_flush(out);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003263}
3264
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003265bool ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05003266 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -08003267 CBB extensions;
3268 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003269 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003270 }
3271
David Benjamin1b249672016-12-06 18:25:50 -05003272 for (unsigned i = 0; i < kNumExtensions; i++) {
3273 if (!(hs->extensions.received & (1u << i))) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003274 // Don't send extensions that were not received.
Kenny Rootb8494592015-09-25 02:29:14 +00003275 continue;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003276 }
3277
David Benjamin1b249672016-12-06 18:25:50 -05003278 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003279 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003280 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Kenny Rootb8494592015-09-25 02:29:14 +00003281 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003282 }
3283 }
3284
Robert Sloana27a6a42017-09-05 08:39:28 -07003285 // Discard empty extensions blocks before TLS 1.3.
Robert Sloan921ef2c2017-10-17 09:02:20 -07003286 if (ssl_protocol_version(ssl) < TLS1_3_VERSION &&
David Benjaminc895d6b2016-08-11 13:26:41 -04003287 CBB_len(&extensions) == 0) {
Kenny Roote99801b2015-11-06 15:31:15 -08003288 CBB_discard_child(out);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003289 }
3290
Kenny Roote99801b2015-11-06 15:31:15 -08003291 return CBB_flush(out);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003292
Kenny Rootb8494592015-09-25 02:29:14 +00003293err:
Kenny Rootb8494592015-09-25 02:29:14 +00003294 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003295 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003296}
3297
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003298static bool ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
3299 const SSL_CLIENT_HELLO *client_hello,
3300 int *out_alert) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003301 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00003302 if (kExtensions[i].init != NULL) {
David Benjamin1b249672016-12-06 18:25:50 -05003303 kExtensions[i].init(hs);
Kenny Rootb8494592015-09-25 02:29:14 +00003304 }
3305 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003306
David Benjamin1b249672016-12-06 18:25:50 -05003307 hs->extensions.received = 0;
David Benjaminc895d6b2016-08-11 13:26:41 -04003308 CBS extensions;
3309 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
3310 while (CBS_len(&extensions) != 0) {
3311 uint16_t type;
3312 CBS extension;
3313
Robert Sloana27a6a42017-09-05 08:39:28 -07003314 // Decode the next extension.
David Benjaminc895d6b2016-08-11 13:26:41 -04003315 if (!CBS_get_u16(&extensions, &type) ||
3316 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08003317 *out_alert = SSL_AD_DECODE_ERROR;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003318 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04003319 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003320
David Benjaminc895d6b2016-08-11 13:26:41 -04003321 unsigned ext_index;
3322 const struct tls_extension *const ext =
3323 tls_extension_find(&ext_index, type);
David Benjaminc895d6b2016-08-11 13:26:41 -04003324 if (ext == NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003325 continue;
3326 }
3327
David Benjamin1b249672016-12-06 18:25:50 -05003328 hs->extensions.received |= (1u << ext_index);
David Benjaminc895d6b2016-08-11 13:26:41 -04003329 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003330 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003331 *out_alert = alert;
3332 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003333 ERR_add_error_dataf("extension %u", (unsigned)type);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003334 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003335 }
3336 }
3337
David Benjaminc895d6b2016-08-11 13:26:41 -04003338 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin1b249672016-12-06 18:25:50 -05003339 if (hs->extensions.received & (1u << i)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003340 continue;
3341 }
3342
3343 CBS *contents = NULL, fake_contents;
3344 static const uint8_t kFakeRenegotiateExtension[] = {0};
3345 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
3346 ssl_client_cipher_list_contains_cipher(client_hello,
3347 SSL3_CK_SCSV & 0xffff)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003348 // The renegotiation SCSV was received so pretend that we received a
3349 // renegotiation extension.
David Benjaminc895d6b2016-08-11 13:26:41 -04003350 CBS_init(&fake_contents, kFakeRenegotiateExtension,
3351 sizeof(kFakeRenegotiateExtension));
3352 contents = &fake_contents;
David Benjamin1b249672016-12-06 18:25:50 -05003353 hs->extensions.received |= (1u << i);
David Benjaminc895d6b2016-08-11 13:26:41 -04003354 }
3355
Robert Sloana27a6a42017-09-05 08:39:28 -07003356 // Extension wasn't observed so call the callback with a NULL
3357 // parameter.
David Benjaminc895d6b2016-08-11 13:26:41 -04003358 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003359 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003360 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003361 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjaminc895d6b2016-08-11 13:26:41 -04003362 *out_alert = alert;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003363 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00003364 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003365 }
3366
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003367 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003368}
3369
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003370bool ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
3371 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin1b249672016-12-06 18:25:50 -05003372 SSL *const ssl = hs->ssl;
Robert Sloan5d625782017-02-13 09:55:39 -08003373 int alert = SSL_AD_DECODE_ERROR;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003374 if (!ssl_scan_clienthello_tlsext(hs, client_hello, &alert)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07003375 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003376 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003377 }
3378
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003379 if (!ssl_check_clienthello_tlsext(hs)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003380 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003381 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003382 }
3383
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003384 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003385}
3386
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003387static bool ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
3388 int *out_alert) {
David Benjamin1b249672016-12-06 18:25:50 -05003389 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07003390 // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty.
Robert Sloan921ef2c2017-10-17 09:02:20 -07003391 if (CBS_len(cbs) == 0 && ssl_protocol_version(ssl) < TLS1_3_VERSION) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003392 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04003393 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003394
Robert Sloana27a6a42017-09-05 08:39:28 -07003395 // Decode the extensions block and check it is valid.
David Benjaminc895d6b2016-08-11 13:26:41 -04003396 CBS extensions;
3397 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
3398 !tls1_check_duplicate_extensions(&extensions)) {
3399 *out_alert = SSL_AD_DECODE_ERROR;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003400 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04003401 }
3402
3403 uint32_t received = 0;
3404 while (CBS_len(&extensions) != 0) {
3405 uint16_t type;
3406 CBS extension;
3407
Robert Sloana27a6a42017-09-05 08:39:28 -07003408 // Decode the next extension.
David Benjaminc895d6b2016-08-11 13:26:41 -04003409 if (!CBS_get_u16(&extensions, &type) ||
3410 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Kenny Roota04d78d2015-09-25 00:26:37 +00003411 *out_alert = SSL_AD_DECODE_ERROR;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003412 return false;
Kenny Roota04d78d2015-09-25 00:26:37 +00003413 }
3414
David Benjaminc895d6b2016-08-11 13:26:41 -04003415 unsigned ext_index;
3416 const struct tls_extension *const ext =
3417 tls_extension_find(&ext_index, type);
Kenny Rootb8494592015-09-25 02:29:14 +00003418
David Benjaminc895d6b2016-08-11 13:26:41 -04003419 if (ext == NULL) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003420 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3421 ERR_add_error_dataf("extension %u", (unsigned)type);
3422 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
3423 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04003424 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003425
Robert Sloana12bf462017-07-17 07:08:26 -07003426 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
3427 "too many bits");
David Benjaminf31229b2017-01-25 14:08:15 -05003428
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003429 if (!(hs->extensions.sent & (1u << ext_index))) {
3430 // If the extension was never sent then it is illegal.
David Benjaminc895d6b2016-08-11 13:26:41 -04003431 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3432 ERR_add_error_dataf("extension :%u", (unsigned)type);
3433 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003434 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04003435 }
Kenny Rootb8494592015-09-25 02:29:14 +00003436
David Benjaminc895d6b2016-08-11 13:26:41 -04003437 received |= (1u << ext_index);
Kenny Roota04d78d2015-09-25 00:26:37 +00003438
David Benjaminc895d6b2016-08-11 13:26:41 -04003439 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003440 if (!ext->parse_serverhello(hs, &alert, &extension)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003441 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003442 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjaminc895d6b2016-08-11 13:26:41 -04003443 *out_alert = alert;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003444 return false;
Kenny Roota04d78d2015-09-25 00:26:37 +00003445 }
3446 }
3447
David Benjamin7c0d06c2016-08-11 13:26:41 -04003448 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00003449 if (!(received & (1u << i))) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003450 // Extension wasn't observed so call the callback with a NULL
3451 // parameter.
Kenny Rootb8494592015-09-25 02:29:14 +00003452 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003453 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003454 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003455 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Kenny Rootb8494592015-09-25 02:29:14 +00003456 *out_alert = alert;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003457 return false;
Kenny Roota04d78d2015-09-25 00:26:37 +00003458 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003459 }
3460 }
3461
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003462 return true;
Kenny Roota04d78d2015-09-25 00:26:37 +00003463}
3464
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003465static bool ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05003466 SSL *const ssl = hs->ssl;
Robert Sloan978112c2018-01-22 12:53:01 -08003467
Robert Sloan15c0b352018-04-16 08:36:46 -07003468 if (ssl->s3->token_binding_negotiated &&
Robert Sloan978112c2018-01-22 12:53:01 -08003469 !(SSL_get_secure_renegotiation_support(ssl) &&
3470 SSL_get_extms_support(ssl))) {
3471 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_TB_WITHOUT_EMS_OR_RI);
3472 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003473 return false;
Robert Sloan978112c2018-01-22 12:53:01 -08003474 }
3475
Adam Langleyd9e397b2015-01-22 14:27:53 -08003476 int ret = SSL_TLSEXT_ERR_NOACK;
3477 int al = SSL_AD_UNRECOGNIZED_NAME;
3478
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003479 if (ssl->ctx->servername_callback != 0) {
3480 ret = ssl->ctx->servername_callback(ssl, &al, ssl->ctx->servername_arg);
3481 } else if (ssl->session_ctx->servername_callback != 0) {
3482 ret = ssl->session_ctx->servername_callback(
3483 ssl, &al, ssl->session_ctx->servername_arg);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003484 }
3485
3486 switch (ret) {
3487 case SSL_TLSEXT_ERR_ALERT_FATAL:
Robert Sloan921ef2c2017-10-17 09:02:20 -07003488 ssl_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003489 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003490
Adam Langleyd9e397b2015-01-22 14:27:53 -08003491 case SSL_TLSEXT_ERR_NOACK:
Robert Sloana27a6a42017-09-05 08:39:28 -07003492 hs->should_ack_sni = false;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003493 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003494
3495 default:
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003496 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003497 }
3498}
3499
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003500bool ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
David Benjamin1b249672016-12-06 18:25:50 -05003501 SSL *const ssl = hs->ssl;
Robert Sloan5d625782017-02-13 09:55:39 -08003502 int alert = SSL_AD_DECODE_ERROR;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003503 if (!ssl_scan_serverhello_tlsext(hs, cbs, &alert)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07003504 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003505 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003506 }
3507
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003508 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003509}
3510
Robert Sloan8f860b12017-08-28 07:37:06 -07003511static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
Robert Sloand9e572d2018-08-27 12:27:00 -07003512 Array<uint8_t> *out, EVP_CIPHER_CTX *cipher_ctx, HMAC_CTX *hmac_ctx,
3513 Span<const uint8_t> ticket) {
Robert Sloan8f860b12017-08-28 07:37:06 -07003514 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003515
Robert Sloana27a6a42017-09-05 08:39:28 -07003516 // Check the MAC at the end of the ticket.
Kenny Rootb8494592015-09-25 02:29:14 +00003517 uint8_t mac[EVP_MAX_MD_SIZE];
Robert Sloan8f860b12017-08-28 07:37:06 -07003518 size_t mac_len = HMAC_size(hmac_ctx);
Robert Sloand9e572d2018-08-27 12:27:00 -07003519 if (ticket.size() < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003520 // The ticket must be large enough for key name, IV, data, and MAC.
Robert Sloana12bf462017-07-17 07:08:26 -07003521 return ssl_ticket_aead_ignore_ticket;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003522 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003523 // Split the ticket into the ticket and the MAC.
3524 auto ticket_mac = ticket.subspan(ticket.size() - mac_len);
3525 ticket = ticket.subspan(0, ticket.size() - mac_len);
3526 HMAC_Update(hmac_ctx, ticket.data(), ticket.size());
Robert Sloan8f860b12017-08-28 07:37:06 -07003527 HMAC_Final(hmac_ctx, mac, NULL);
Robert Sloand9e572d2018-08-27 12:27:00 -07003528 assert(mac_len == ticket_mac.size());
3529 bool mac_ok = CRYPTO_memcmp(mac, ticket_mac.data(), mac_len) == 0;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003530#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003531 mac_ok = true;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003532#endif
3533 if (!mac_ok) {
Robert Sloana12bf462017-07-17 07:08:26 -07003534 return ssl_ticket_aead_ignore_ticket;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003535 }
3536
Robert Sloana27a6a42017-09-05 08:39:28 -07003537 // Decrypt the session data.
Robert Sloand9e572d2018-08-27 12:27:00 -07003538 auto ciphertext = ticket.subspan(SSL_TICKET_KEY_NAME_LEN + iv_len);
3539 Array<uint8_t> plaintext;
3540#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3541 if (!plaintext.CopyFrom(ciphertext)) {
Robert Sloana12bf462017-07-17 07:08:26 -07003542 return ssl_ticket_aead_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003543 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003544#else
Robert Sloand9e572d2018-08-27 12:27:00 -07003545 if (ciphertext.size() >= INT_MAX) {
Robert Sloana12bf462017-07-17 07:08:26 -07003546 return ssl_ticket_aead_ignore_ticket;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003547 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003548 if (!plaintext.Init(ciphertext.size())) {
3549 return ssl_ticket_aead_error;
3550 }
Kenny Rootb8494592015-09-25 02:29:14 +00003551 int len1, len2;
Robert Sloand9e572d2018-08-27 12:27:00 -07003552 if (!EVP_DecryptUpdate(cipher_ctx, plaintext.data(), &len1, ciphertext.data(),
3553 (int)ciphertext.size()) ||
3554 !EVP_DecryptFinal_ex(cipher_ctx, plaintext.data() + len1, &len2)) {
Robert Sloan1c9db532017-03-13 08:03:59 -07003555 ERR_clear_error();
Robert Sloana12bf462017-07-17 07:08:26 -07003556 return ssl_ticket_aead_ignore_ticket;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003557 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003558 plaintext.Shrink(static_cast<size_t>(len1) + len2);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003559#endif
Adam Langleyd9e397b2015-01-22 14:27:53 -08003560
Robert Sloand9e572d2018-08-27 12:27:00 -07003561 *out = std::move(plaintext);
Robert Sloana12bf462017-07-17 07:08:26 -07003562 return ssl_ticket_aead_success;
Robert Sloan1c9db532017-03-13 08:03:59 -07003563}
3564
Robert Sloan8f860b12017-08-28 07:37:06 -07003565static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
Robert Sloand9e572d2018-08-27 12:27:00 -07003566 SSL_HANDSHAKE *hs, Array<uint8_t> *out, bool *out_renew_ticket,
3567 Span<const uint8_t> ticket) {
3568 assert(ticket.size() >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
Robert Sloan8f860b12017-08-28 07:37:06 -07003569 ScopedEVP_CIPHER_CTX cipher_ctx;
3570 ScopedHMAC_CTX hmac_ctx;
Robert Sloand9e572d2018-08-27 12:27:00 -07003571 auto name = ticket.subspan(0, SSL_TICKET_KEY_NAME_LEN);
3572 // The actual IV is shorter, but the length is determined by the callback's
3573 // chosen cipher. Instead we pass in |EVP_MAX_IV_LENGTH| worth of IV to ensure
3574 // the callback has enough.
3575 auto iv = ticket.subspan(SSL_TICKET_KEY_NAME_LEN, EVP_MAX_IV_LENGTH);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003576 int cb_ret = hs->ssl->session_ctx->ticket_key_cb(
Robert Sloand9e572d2018-08-27 12:27:00 -07003577 hs->ssl, const_cast<uint8_t *>(name.data()),
3578 const_cast<uint8_t *>(iv.data()), cipher_ctx.get(), hmac_ctx.get(),
3579 0 /* decrypt */);
Robert Sloan8f860b12017-08-28 07:37:06 -07003580 if (cb_ret < 0) {
3581 return ssl_ticket_aead_error;
3582 } else if (cb_ret == 0) {
3583 return ssl_ticket_aead_ignore_ticket;
3584 } else if (cb_ret == 2) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003585 *out_renew_ticket = true;
Robert Sloan8f860b12017-08-28 07:37:06 -07003586 } else {
3587 assert(cb_ret == 1);
3588 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003589 return decrypt_ticket_with_cipher_ctx(out, cipher_ctx.get(), hmac_ctx.get(),
3590 ticket);
Robert Sloan8f860b12017-08-28 07:37:06 -07003591}
3592
3593static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
Robert Sloand9e572d2018-08-27 12:27:00 -07003594 SSL_HANDSHAKE *hs, Array<uint8_t> *out, Span<const uint8_t> ticket) {
3595 assert(ticket.size() >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003596 SSL_CTX *ctx = hs->ssl->session_ctx.get();
Robert Sloan8f860b12017-08-28 07:37:06 -07003597
Robert Sloana27a6a42017-09-05 08:39:28 -07003598 // Rotate the ticket key if necessary.
Robert Sloan8f860b12017-08-28 07:37:06 -07003599 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
3600 return ssl_ticket_aead_error;
3601 }
3602
Robert Sloand9e572d2018-08-27 12:27:00 -07003603 const EVP_CIPHER *cipher = EVP_aes_128_cbc();
3604 auto name = ticket.subspan(0, SSL_TICKET_KEY_NAME_LEN);
3605 auto iv =
3606 ticket.subspan(SSL_TICKET_KEY_NAME_LEN, EVP_CIPHER_iv_length(cipher));
3607
Robert Sloana27a6a42017-09-05 08:39:28 -07003608 // Pick the matching ticket key and decrypt.
Robert Sloan8f860b12017-08-28 07:37:06 -07003609 ScopedEVP_CIPHER_CTX cipher_ctx;
3610 ScopedHMAC_CTX hmac_ctx;
3611 {
3612 MutexReadLock lock(&ctx->lock);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003613 const TicketKey *key;
Robert Sloand9e572d2018-08-27 12:27:00 -07003614 if (ctx->ticket_key_current && name == ctx->ticket_key_current->name) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003615 key = ctx->ticket_key_current.get();
Robert Sloand9e572d2018-08-27 12:27:00 -07003616 } else if (ctx->ticket_key_prev && name == ctx->ticket_key_prev->name) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003617 key = ctx->ticket_key_prev.get();
Robert Sloan8f860b12017-08-28 07:37:06 -07003618 } else {
3619 return ssl_ticket_aead_ignore_ticket;
3620 }
Robert Sloan8f860b12017-08-28 07:37:06 -07003621 if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
3622 tlsext_tick_md(), NULL) ||
Robert Sloand9e572d2018-08-27 12:27:00 -07003623 !EVP_DecryptInit_ex(cipher_ctx.get(), cipher, NULL,
3624 key->aes_key, iv.data())) {
Robert Sloan8f860b12017-08-28 07:37:06 -07003625 return ssl_ticket_aead_error;
3626 }
3627 }
Robert Sloand9e572d2018-08-27 12:27:00 -07003628 return decrypt_ticket_with_cipher_ctx(out, cipher_ctx.get(), hmac_ctx.get(),
3629 ticket);
Robert Sloan8f860b12017-08-28 07:37:06 -07003630}
3631
Robert Sloan1c9db532017-03-13 08:03:59 -07003632static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
Robert Sloand9e572d2018-08-27 12:27:00 -07003633 SSL_HANDSHAKE *hs, Array<uint8_t> *out, bool *out_renew_ticket,
3634 Span<const uint8_t> ticket) {
3635 Array<uint8_t> plaintext;
3636 if (!plaintext.Init(ticket.size())) {
Robert Sloan1c9db532017-03-13 08:03:59 -07003637 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3638 return ssl_ticket_aead_error;
3639 }
3640
3641 size_t plaintext_len;
3642 const enum ssl_ticket_aead_result_t result =
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003643 hs->ssl->session_ctx->ticket_aead_method->open(
Robert Sloand9e572d2018-08-27 12:27:00 -07003644 hs->ssl, plaintext.data(), &plaintext_len, ticket.size(),
3645 ticket.data(), ticket.size());
3646 if (result != ssl_ticket_aead_success) {
3647 return result;
Robert Sloan1c9db532017-03-13 08:03:59 -07003648 }
3649
Robert Sloand9e572d2018-08-27 12:27:00 -07003650 plaintext.Shrink(plaintext_len);
3651 *out = std::move(plaintext);
3652 return ssl_ticket_aead_success;
Robert Sloan1c9db532017-03-13 08:03:59 -07003653}
3654
3655enum ssl_ticket_aead_result_t ssl_process_ticket(
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003656 SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session,
Robert Sloand9e572d2018-08-27 12:27:00 -07003657 bool *out_renew_ticket, Span<const uint8_t> ticket,
3658 Span<const uint8_t> session_id) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003659 *out_renew_ticket = false;
Robert Sloanfe7cd212017-08-07 09:03:39 -07003660 out_session->reset();
Robert Sloan1c9db532017-03-13 08:03:59 -07003661
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003662 if ((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) ||
Robert Sloand9e572d2018-08-27 12:27:00 -07003663 session_id.size() > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Robert Sloan1c9db532017-03-13 08:03:59 -07003664 return ssl_ticket_aead_ignore_ticket;
3665 }
3666
Robert Sloand9e572d2018-08-27 12:27:00 -07003667 Array<uint8_t> plaintext;
Robert Sloan1c9db532017-03-13 08:03:59 -07003668 enum ssl_ticket_aead_result_t result;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003669 if (hs->ssl->session_ctx->ticket_aead_method != NULL) {
Robert Sloand9e572d2018-08-27 12:27:00 -07003670 result = ssl_decrypt_ticket_with_method(hs, &plaintext, out_renew_ticket,
3671 ticket);
Robert Sloan1c9db532017-03-13 08:03:59 -07003672 } else {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003673 // Ensure there is room for the key name and the largest IV |ticket_key_cb|
3674 // may try to consume. The real limit may be lower, but the maximum IV
3675 // length should be well under the minimum size for the session material and
3676 // HMAC.
Robert Sloand9e572d2018-08-27 12:27:00 -07003677 if (ticket.size() < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
Robert Sloan8f860b12017-08-28 07:37:06 -07003678 return ssl_ticket_aead_ignore_ticket;
3679 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003680 if (hs->ssl->session_ctx->ticket_key_cb != NULL) {
Robert Sloand9e572d2018-08-27 12:27:00 -07003681 result =
3682 ssl_decrypt_ticket_with_cb(hs, &plaintext, out_renew_ticket, ticket);
Robert Sloan8f860b12017-08-28 07:37:06 -07003683 } else {
Robert Sloand9e572d2018-08-27 12:27:00 -07003684 result = ssl_decrypt_ticket_with_ticket_keys(hs, &plaintext, ticket);
Robert Sloan8f860b12017-08-28 07:37:06 -07003685 }
Robert Sloan1c9db532017-03-13 08:03:59 -07003686 }
3687
3688 if (result != ssl_ticket_aead_success) {
3689 return result;
3690 }
3691
Robert Sloana27a6a42017-09-05 08:39:28 -07003692 // Decode the session.
Robert Sloand9e572d2018-08-27 12:27:00 -07003693 UniquePtr<SSL_SESSION> session(SSL_SESSION_from_bytes(
3694 plaintext.data(), plaintext.size(), hs->ssl->ctx.get()));
Robert Sloanfe7cd212017-08-07 09:03:39 -07003695 if (!session) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003696 ERR_clear_error(); // Don't leave an error on the queue.
Robert Sloan1c9db532017-03-13 08:03:59 -07003697 return ssl_ticket_aead_ignore_ticket;
Kenny Rootb8494592015-09-25 02:29:14 +00003698 }
3699
Robert Sloana27a6a42017-09-05 08:39:28 -07003700 // Copy the client's session ID into the new session, to denote the ticket has
3701 // been accepted.
Robert Sloand9e572d2018-08-27 12:27:00 -07003702 OPENSSL_memcpy(session->session_id, session_id.data(), session_id.size());
3703 session->session_id_length = session_id.size();
Kenny Rootb8494592015-09-25 02:29:14 +00003704
Robert Sloanfe7cd212017-08-07 09:03:39 -07003705 *out_session = std::move(session);
Robert Sloan1c9db532017-03-13 08:03:59 -07003706 return ssl_ticket_aead_success;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003707}
3708
Robert Sloan309a31e2018-01-29 10:22:47 -08003709bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003710 // Extension ignored for inappropriate versions
Robert Sloan921ef2c2017-10-17 09:02:20 -07003711 if (ssl_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Robert Sloan309a31e2018-01-29 10:22:47 -08003712 return true;
Kenny Roota04d78d2015-09-25 00:26:37 +00003713 }
3714
Robert Sloan11c28bd2018-12-17 12:09:20 -08003715 // In all contexts, the signature algorithms list may not be empty. (It may be
3716 // omitted by clients in TLS 1.2, but then the entire extension is omitted.)
3717 return CBS_len(in_sigalgs) != 0 &&
3718 parse_u16_array(in_sigalgs, &hs->peer_sigalgs);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003719}
3720
Robert Sloan309a31e2018-01-29 10:22:47 -08003721bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
Robert Sloan2424d842017-05-01 07:46:28 -07003722 switch (EVP_PKEY_id(pkey)) {
3723 case EVP_PKEY_RSA:
3724 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
Robert Sloan309a31e2018-01-29 10:22:47 -08003725 return true;
Robert Sloan2424d842017-05-01 07:46:28 -07003726 case EVP_PKEY_EC:
3727 *out = SSL_SIGN_ECDSA_SHA1;
Robert Sloan309a31e2018-01-29 10:22:47 -08003728 return true;
Robert Sloan2424d842017-05-01 07:46:28 -07003729 default:
Robert Sloan309a31e2018-01-29 10:22:47 -08003730 return false;
Robert Sloan2424d842017-05-01 07:46:28 -07003731 }
3732}
3733
Robert Sloan309a31e2018-01-29 10:22:47 -08003734bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
David Benjamin1b249672016-12-06 18:25:50 -05003735 SSL *const ssl = hs->ssl;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003736 CERT *cert = hs->config->cert.get();
Robert Sloan4c22c5f2019-03-01 15:53:37 -08003737 DC *dc = cert->dc.get();
Adam Langleyd9e397b2015-01-22 14:27:53 -08003738
Robert Sloana27a6a42017-09-05 08:39:28 -07003739 // Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3740 // handshake.
Robert Sloan921ef2c2017-10-17 09:02:20 -07003741 if (ssl_protocol_version(ssl) < TLS1_2_VERSION) {
Robert Sloanb6d070c2017-07-24 08:40:01 -07003742 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
Robert Sloan2424d842017-05-01 07:46:28 -07003743 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
Robert Sloan309a31e2018-01-29 10:22:47 -08003744 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04003745 }
Robert Sloan309a31e2018-01-29 10:22:47 -08003746 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00003747 }
3748
Robert Sloan4562e9d2017-10-02 10:26:51 -07003749 Span<const uint16_t> sigalgs = kSignSignatureAlgorithms;
Robert Sloan4c22c5f2019-03-01 15:53:37 -08003750 if (ssl_signing_with_dc(hs)) {
3751 sigalgs = MakeConstSpan(&dc->expected_cert_verify_algorithm, 1);
3752 } else if (!cert->sigalgs.empty()) {
Robert Sloan5cbb5c82018-04-24 11:35:46 -07003753 sigalgs = cert->sigalgs;
David Benjaminc895d6b2016-08-11 13:26:41 -04003754 }
Kenny Rootb8494592015-09-25 02:29:14 +00003755
Robert Sloan4c22c5f2019-03-01 15:53:37 -08003756 Span<const uint16_t> peer_sigalgs = tls1_get_peer_verify_algorithms(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -04003757
Robert Sloan4562e9d2017-10-02 10:26:51 -07003758 for (uint16_t sigalg : sigalgs) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003759 // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3760 // negotiated.
David Benjaminc895d6b2016-08-11 13:26:41 -04003761 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07003762 !ssl_private_key_supports_signature_algorithm(hs, sigalg)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003763 continue;
3764 }
3765
Robert Sloan4562e9d2017-10-02 10:26:51 -07003766 for (uint16_t peer_sigalg : peer_sigalgs) {
3767 if (sigalg == peer_sigalg) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003768 *out = sigalg;
Robert Sloan309a31e2018-01-29 10:22:47 -08003769 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04003770 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003771 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003772 }
3773
David Benjaminc895d6b2016-08-11 13:26:41 -04003774 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
Robert Sloan309a31e2018-01-29 10:22:47 -08003775 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003776}
3777
Robert Sloan4c22c5f2019-03-01 15:53:37 -08003778Span<const uint16_t> tls1_get_peer_verify_algorithms(const SSL_HANDSHAKE *hs) {
3779 Span<const uint16_t> peer_sigalgs = hs->peer_sigalgs;
3780 if (peer_sigalgs.empty() && ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) {
3781 // If the client didn't specify any signature_algorithms extension then
3782 // we can assume that it supports SHA1. See
3783 // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
3784 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3785 SSL_SIGN_ECDSA_SHA1};
3786 peer_sigalgs = kDefaultPeerAlgorithms;
3787 }
3788 return peer_sigalgs;
3789}
3790
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003791bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
Robert Sloan5d625782017-02-13 09:55:39 -08003792 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07003793 // A Channel ID handshake message is structured to contain multiple
3794 // extensions, but the only one that can be present is Channel ID.
Robert Sloan84377092017-08-14 09:33:19 -07003795 uint16_t extension_type;
3796 CBS channel_id = msg.body, extension;
Steven Valdez909b19f2016-11-21 15:35:44 -05003797 if (!CBS_get_u16(&channel_id, &extension_type) ||
3798 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3799 CBS_len(&channel_id) != 0 ||
3800 extension_type != TLSEXT_TYPE_channel_id ||
3801 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3802 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07003803 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003804 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003805 }
3806
Robert Sloanb6d070c2017-07-24 08:40:01 -07003807 UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Steven Valdez909b19f2016-11-21 15:35:44 -05003808 if (!p256) {
3809 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003810 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003811 }
3812
Robert Sloanb6d070c2017-07-24 08:40:01 -07003813 UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3814 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
Robert Sloana12bf462017-07-17 07:08:26 -07003815 if (!sig || !x || !y) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003816 return false;
Adam Langley1e4884f2015-09-24 10:57:52 -07003817 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003818
Steven Valdez909b19f2016-11-21 15:35:44 -05003819 const uint8_t *p = CBS_data(&extension);
Robert Sloana12bf462017-07-17 07:08:26 -07003820 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3821 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3822 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3823 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003824 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003825 }
3826
Robert Sloanb6d070c2017-07-24 08:40:01 -07003827 UniquePtr<EC_KEY> key(EC_KEY_new());
3828 UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
Robert Sloana12bf462017-07-17 07:08:26 -07003829 if (!key || !point ||
3830 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3831 y.get(), nullptr) ||
3832 !EC_KEY_set_group(key.get(), p256.get()) ||
3833 !EC_KEY_set_public_key(key.get(), point.get())) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003834 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003835 }
3836
3837 uint8_t digest[EVP_MAX_MD_SIZE];
3838 size_t digest_len;
Robert Sloan5d625782017-02-13 09:55:39 -08003839 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Robert Sloanb1b54b82017-11-06 13:50:02 -08003840 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003841 }
3842
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003843 bool sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
3844#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3845 sig_ok = true;
3846 ERR_clear_error();
3847#endif
3848 if (!sig_ok) {
3849 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3850 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3851 ssl->s3->channel_id_valid = false;
3852 return false;
3853 }
3854
3855 OPENSSL_memcpy(ssl->s3->channel_id, p, 64);
3856 return true;
3857}
3858
3859bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3860 uint8_t digest[EVP_MAX_MD_SIZE];
3861 size_t digest_len;
3862 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
3863 return false;
3864 }
3865
3866 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(hs->config->channel_id_private.get());
Robert Sloanb1b54b82017-11-06 13:50:02 -08003867 if (ec_key == nullptr) {
Steven Valdez909b19f2016-11-21 15:35:44 -05003868 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloanb1b54b82017-11-06 13:50:02 -08003869 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003870 }
3871
Robert Sloanb1b54b82017-11-06 13:50:02 -08003872 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
3873 if (!x || !y ||
Steven Valdez909b19f2016-11-21 15:35:44 -05003874 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3875 EC_KEY_get0_public_key(ec_key),
Robert Sloanb1b54b82017-11-06 13:50:02 -08003876 x.get(), y.get(), nullptr)) {
3877 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003878 }
3879
Robert Sloanb1b54b82017-11-06 13:50:02 -08003880 UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign(digest, digest_len, ec_key));
3881 if (!sig) {
3882 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003883 }
3884
3885 CBB child;
3886 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3887 !CBB_add_u16_length_prefixed(cbb, &child) ||
Robert Sloanb1b54b82017-11-06 13:50:02 -08003888 !BN_bn2cbb_padded(&child, 32, x.get()) ||
3889 !BN_bn2cbb_padded(&child, 32, y.get()) ||
Steven Valdez909b19f2016-11-21 15:35:44 -05003890 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3891 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3892 !CBB_flush(cbb)) {
Robert Sloanb1b54b82017-11-06 13:50:02 -08003893 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003894 }
3895
Robert Sloanb1b54b82017-11-06 13:50:02 -08003896 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05003897}
3898
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003899bool tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
Robert Sloan5d625782017-02-13 09:55:39 -08003900 SSL *const ssl = hs->ssl;
Robert Sloan921ef2c2017-10-17 09:02:20 -07003901 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
3902 Array<uint8_t> msg;
3903 if (!tls13_get_cert_verify_signature_input(hs, &msg,
Steven Valdez909b19f2016-11-21 15:35:44 -05003904 ssl_cert_verify_channel_id)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003905 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003906 }
Robert Sloan921ef2c2017-10-17 09:02:20 -07003907 SHA256(msg.data(), msg.size(), out);
Steven Valdez909b19f2016-11-21 15:35:44 -05003908 *out_len = SHA256_DIGEST_LENGTH;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003909 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05003910 }
3911
3912 SHA256_CTX ctx;
3913
3914 SHA256_Init(&ctx);
Kenny Rootb8494592015-09-25 02:29:14 +00003915 static const char kClientIDMagic[] = "TLS Channel ID signature";
Steven Valdez909b19f2016-11-21 15:35:44 -05003916 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
Kenny Rootb8494592015-09-25 02:29:14 +00003917
David Benjaminc895d6b2016-08-11 13:26:41 -04003918 if (ssl->session != NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00003919 static const char kResumptionMagic[] = "Resumption";
Steven Valdez909b19f2016-11-21 15:35:44 -05003920 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
Kenny Rootb8494592015-09-25 02:29:14 +00003921 if (ssl->session->original_handshake_hash_len == 0) {
3922 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003923 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00003924 }
Steven Valdez909b19f2016-11-21 15:35:44 -05003925 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3926 ssl->session->original_handshake_hash_len);
Kenny Rootb8494592015-09-25 02:29:14 +00003927 }
3928
Robert Sloan5d625782017-02-13 09:55:39 -08003929 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3930 size_t hs_hash_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -07003931 if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003932 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00003933 }
Robert Sloan5d625782017-02-13 09:55:39 -08003934 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Steven Valdez909b19f2016-11-21 15:35:44 -05003935 SHA256_Final(out, &ctx);
3936 *out_len = SHA256_DIGEST_LENGTH;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003937 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003938}
3939
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003940bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
Robert Sloan5d625782017-02-13 09:55:39 -08003941 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07003942 // This function should never be called for a resumed session because the
3943 // handshake hashes that we wish to record are for the original, full
3944 // handshake.
David Benjaminc895d6b2016-08-11 13:26:41 -04003945 if (ssl->session != NULL) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003946 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003947 }
3948
Robert Sloana12bf462017-07-17 07:08:26 -07003949 static_assert(
Robert Sloana94fe052017-02-21 08:49:28 -08003950 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
Robert Sloana12bf462017-07-17 07:08:26 -07003951 "original_handshake_hash is too small");
Robert Sloan5d625782017-02-13 09:55:39 -08003952
3953 size_t digest_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -07003954 if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
3955 &digest_len)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003956 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003957 }
3958
Robert Sloana12bf462017-07-17 07:08:26 -07003959 static_assert(EVP_MAX_MD_SIZE <= 0xff,
3960 "EVP_MAX_MD_SIZE does not fit in uint8_t");
Robert Sloana94fe052017-02-21 08:49:28 -08003961 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003962
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003963 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003964}
Steven Valdez909b19f2016-11-21 15:35:44 -05003965
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003966bool ssl_do_channel_id_callback(SSL_HANDSHAKE *hs) {
3967 if (hs->config->channel_id_private != NULL ||
3968 hs->ssl->ctx->channel_id_cb == NULL) {
3969 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05003970 }
3971
3972 EVP_PKEY *key = NULL;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003973 hs->ssl->ctx->channel_id_cb(hs->ssl, &key);
Steven Valdez909b19f2016-11-21 15:35:44 -05003974 if (key == NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003975 // The caller should try again later.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003976 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05003977 }
3978
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003979 UniquePtr<EVP_PKEY> free_key(key);
3980 return SSL_set1_tls_channel_id(hs->ssl, key);
Steven Valdez909b19f2016-11-21 15:35:44 -05003981}
3982
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003983bool ssl_is_sct_list_valid(const CBS *contents) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003984 // Shallow parse the SCT list for sanity. By the RFC
3985 // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3986 // of the SCTs may be empty.
Steven Valdez909b19f2016-11-21 15:35:44 -05003987 CBS copy = *contents;
3988 CBS sct_list;
3989 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3990 CBS_len(&copy) != 0 ||
3991 CBS_len(&sct_list) == 0) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003992 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003993 }
3994
3995 while (CBS_len(&sct_list) > 0) {
3996 CBS sct;
3997 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3998 CBS_len(&sct) == 0) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01003999 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05004000 }
4001 }
4002
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01004003 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05004004}
Robert Sloanb6d070c2017-07-24 08:40:01 -07004005
Robert Sloan726e9d12018-09-11 11:45:04 -07004006BSSL_NAMESPACE_END
Robert Sloanb6d070c2017-07-24 08:40:01 -07004007
4008using namespace bssl;
4009
4010int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
4011 uint16_t extension_type,
4012 const uint8_t **out_data,
4013 size_t *out_len) {
4014 CBS cbs;
4015 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
4016 return 0;
4017 }
4018
4019 *out_data = CBS_data(&cbs);
4020 *out_len = CBS_len(&cbs);
4021 return 1;
4022}
4023
4024void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
4025 ctx->ed25519_enabled = !!enabled;
4026}
4027
Robert Sloan5cbb5c82018-04-24 11:35:46 -07004028void SSL_CTX_set_rsa_pss_rsae_certs_enabled(SSL_CTX *ctx, int enabled) {
4029 ctx->rsa_pss_rsae_certs_enabled = !!enabled;
4030}