blob: e32ef47f93a592b85b0c181124d42ee766edcf1a [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin499742c2017-07-22 12:45:38 -0400116#include <utility>
117
David Benjamin03973092014-06-24 23:27:17 -0400118#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400119#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400120#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700121#include <openssl/evp.h>
122#include <openssl/hmac.h>
123#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400124#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125#include <openssl/rand.h>
126
David Benjamin2ee94aa2015-04-07 22:38:30 -0400127#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400128#include "../crypto/internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800129
130
David Benjamin86e95b82017-07-18 16:34:25 -0400131namespace bssl {
132
David Benjamin8c880a22016-12-03 02:20:34 -0500133static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
Adam Langley95c29f32014-06-20 12:00:00 -0700134
Adam Langleyfcf25832014-12-18 17:42:32 -0800135static int compare_uint16_t(const void *p1, const void *p2) {
136 uint16_t u1 = *((const uint16_t *)p1);
137 uint16_t u2 = *((const uint16_t *)p2);
138 if (u1 < u2) {
139 return -1;
140 } else if (u1 > u2) {
141 return 1;
142 } else {
143 return 0;
144 }
145}
David Benjamin35a7a442014-07-05 00:23:20 -0400146
David Benjaminc11ea9422017-08-29 16:33:21 -0400147// Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
148// more than one extension of the same type in a ClientHello or ServerHello.
149// This function does an initial scan over the extensions block to filter those
150// out.
Adam Langleyfcf25832014-12-18 17:42:32 -0800151static int tls1_check_duplicate_extensions(const CBS *cbs) {
152 CBS extensions = *cbs;
153 size_t num_extensions = 0, i = 0;
154 uint16_t *extension_types = NULL;
155 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400156
David Benjaminc11ea9422017-08-29 16:33:21 -0400157 // First pass: count the extensions.
Adam Langleyfcf25832014-12-18 17:42:32 -0800158 while (CBS_len(&extensions) > 0) {
159 uint16_t type;
160 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400161
Adam Langleyfcf25832014-12-18 17:42:32 -0800162 if (!CBS_get_u16(&extensions, &type) ||
163 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
164 goto done;
165 }
David Benjamin35a7a442014-07-05 00:23:20 -0400166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 num_extensions++;
168 }
David Benjamin35a7a442014-07-05 00:23:20 -0400169
Adam Langleyfcf25832014-12-18 17:42:32 -0800170 if (num_extensions == 0) {
171 return 1;
172 }
David Benjamin9a373592014-07-25 04:27:53 -0400173
David Benjamin81678aa2017-07-12 22:43:42 -0400174 extension_types =
175 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800176 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400177 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 goto done;
179 }
David Benjamin35a7a442014-07-05 00:23:20 -0400180
David Benjaminc11ea9422017-08-29 16:33:21 -0400181 // Second pass: gather the extension types.
Adam Langleyfcf25832014-12-18 17:42:32 -0800182 extensions = *cbs;
183 for (i = 0; i < num_extensions; i++) {
184 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400185
Adam Langleyfcf25832014-12-18 17:42:32 -0800186 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
187 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400188 // This should not happen.
Adam Langleyfcf25832014-12-18 17:42:32 -0800189 goto done;
190 }
191 }
192 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400193
David Benjaminc11ea9422017-08-29 16:33:21 -0400194 // Sort the extensions and make sure there are no duplicates.
Adam Langleyfcf25832014-12-18 17:42:32 -0800195 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
196 for (i = 1; i < num_extensions; i++) {
197 if (extension_types[i - 1] == extension_types[i]) {
198 goto done;
199 }
200 }
David Benjamin35a7a442014-07-05 00:23:20 -0400201
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 ret = 1;
203
David Benjamin35a7a442014-07-05 00:23:20 -0400204done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400205 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800206 return ret;
207}
David Benjamin35a7a442014-07-05 00:23:20 -0400208
David Benjamin7934f082017-08-01 16:32:25 -0400209int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out,
210 const SSLMessage &msg) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500211 OPENSSL_memset(out, 0, sizeof(*out));
David Benjamin731058e2016-12-03 23:15:13 -0500212 out->ssl = ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400213 out->client_hello = CBS_data(&msg.body);
214 out->client_hello_len = CBS_len(&msg.body);
David Benjamin8f2c20e2014-07-09 09:30:38 -0400215
David Benjamine14ff062016-08-09 16:21:24 -0400216 CBS client_hello, random, session_id;
David Benjamin731058e2016-12-03 23:15:13 -0500217 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
218 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjamine14ff062016-08-09 16:21:24 -0400219 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
220 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
221 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800222 return 0;
223 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700224
David Benjamin731058e2016-12-03 23:15:13 -0500225 out->random = CBS_data(&random);
226 out->random_len = CBS_len(&random);
227 out->session_id = CBS_data(&session_id);
228 out->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700229
David Benjaminc11ea9422017-08-29 16:33:21 -0400230 // Skip past DTLS cookie
David Benjamin731058e2016-12-03 23:15:13 -0500231 if (SSL_is_dtls(out->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400233 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
234 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800235 return 0;
236 }
237 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700238
David Benjamine14ff062016-08-09 16:21:24 -0400239 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400241 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
242 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800243 CBS_len(&compression_methods) < 1) {
244 return 0;
245 }
David Benjamine14ff062016-08-09 16:21:24 -0400246
David Benjamin731058e2016-12-03 23:15:13 -0500247 out->cipher_suites = CBS_data(&cipher_suites);
248 out->cipher_suites_len = CBS_len(&cipher_suites);
249 out->compression_methods = CBS_data(&compression_methods);
250 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700251
David Benjaminc11ea9422017-08-29 16:33:21 -0400252 // If the ClientHello ends here then it's valid, but doesn't have any
253 // extensions. (E.g. SSLv3.)
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 if (CBS_len(&client_hello) == 0) {
David Benjamin731058e2016-12-03 23:15:13 -0500255 out->extensions = NULL;
256 out->extensions_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 return 1;
258 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700259
David Benjaminc11ea9422017-08-29 16:33:21 -0400260 // Extract extensions and check it is valid.
David Benjamine14ff062016-08-09 16:21:24 -0400261 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
263 !tls1_check_duplicate_extensions(&extensions) ||
264 CBS_len(&client_hello) != 0) {
265 return 0;
266 }
David Benjamine14ff062016-08-09 16:21:24 -0400267
David Benjamin731058e2016-12-03 23:15:13 -0500268 out->extensions = CBS_data(&extensions);
269 out->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700270
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 return 1;
272}
Adam Langleydc9b1412014-06-20 12:00:00 -0700273
David Benjamin731058e2016-12-03 23:15:13 -0500274int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
275 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 CBS extensions;
David Benjamin731058e2016-12-03 23:15:13 -0500277 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 while (CBS_len(&extensions) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400279 // Decode the next extension.
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 uint16_t type;
281 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800282 if (!CBS_get_u16(&extensions, &type) ||
283 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
284 return 0;
285 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700286
Adam Langleyfcf25832014-12-18 17:42:32 -0800287 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400288 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 return 1;
290 }
291 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700292
Adam Langleyfcf25832014-12-18 17:42:32 -0800293 return 0;
294}
Adam Langley95c29f32014-06-20 12:00:00 -0700295
Steven Valdezce902a92016-05-17 11:47:53 -0400296static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400297 SSL_CURVE_X25519,
298 SSL_CURVE_SECP256R1,
299 SSL_CURVE_SECP384R1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800300};
Adam Langley95c29f32014-06-20 12:00:00 -0700301
David Benjaminf04976b2016-10-07 00:37:55 -0400302void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
Steven Valdez5440fe02016-07-18 12:40:30 -0400303 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400304 *out_group_ids = ssl->supported_group_list;
305 *out_group_ids_len = ssl->supported_group_list_len;
306 if (!*out_group_ids) {
307 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400308 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800309 }
310}
David Benjamined439582014-07-14 19:13:02 -0400311
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900312int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
313 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400314 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400315
David Benjaminf04976b2016-10-07 00:37:55 -0400316 const uint16_t *groups, *pref, *supp;
317 size_t groups_len, pref_len, supp_len;
318 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800319
David Benjaminc11ea9422017-08-29 16:33:21 -0400320 // Clients are not required to send a supported_groups extension. In this
321 // case, the server is free to pick any group it likes. See RFC 4492,
322 // section 4, paragraph 3.
323 //
324 // However, in the interests of compatibility, we will skip ECDH if the
325 // client didn't send an extension because we can't be sure that they'll
326 // support our favoured group. Thus we do not special-case an emtpy
327 // |peer_supported_group_list|.
David Benjamin55a43642015-04-20 14:45:55 -0400328
David Benjamin4298d772015-12-19 00:18:25 -0500329 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400330 pref = groups;
331 pref_len = groups_len;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900332 supp = hs->peer_supported_group_list;
333 supp_len = hs->peer_supported_group_list_len;
David Benjamin55a43642015-04-20 14:45:55 -0400334 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900335 pref = hs->peer_supported_group_list;
336 pref_len = hs->peer_supported_group_list_len;
Steven Valdezce902a92016-05-17 11:47:53 -0400337 supp = groups;
338 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400339 }
340
David Benjaminf04976b2016-10-07 00:37:55 -0400341 for (size_t i = 0; i < pref_len; i++) {
342 for (size_t j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800343 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400344 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500345 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800346 }
347 }
348 }
349
David Benjamin4298d772015-12-19 00:18:25 -0500350 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800351}
Adam Langley95c29f32014-06-20 12:00:00 -0700352
Steven Valdezce902a92016-05-17 11:47:53 -0400353int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 const int *curves, size_t ncurves) {
David Benjamin81678aa2017-07-12 22:43:42 -0400355 uint16_t *group_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
Steven Valdezce902a92016-05-17 11:47:53 -0400356 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 return 0;
358 }
359
David Benjamin54091232016-09-05 12:47:25 -0400360 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400361 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
362 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 return 0;
364 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 }
366
Steven Valdezce902a92016-05-17 11:47:53 -0400367 OPENSSL_free(*out_group_ids);
368 *out_group_ids = group_ids;
369 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800370
371 return 1;
372}
Adam Langley95c29f32014-06-20 12:00:00 -0700373
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100374int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
375 const char *curves) {
376 uint16_t *group_ids = NULL;
377 size_t ncurves = 0;
378
379 const char *col;
380 const char *ptr = curves;
381
382 do {
383 col = strchr(ptr, ':');
384
385 uint16_t group_id;
386 if (!ssl_name_to_group_id(&group_id, ptr,
387 col ? (size_t)(col - ptr) : strlen(ptr))) {
388 goto err;
389 }
390
David Benjamin81678aa2017-07-12 22:43:42 -0400391 uint16_t *new_group_ids = (uint16_t *)OPENSSL_realloc(
392 group_ids, (ncurves + 1) * sizeof(uint16_t));
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100393 if (new_group_ids == NULL) {
394 goto err;
395 }
396 group_ids = new_group_ids;
397
398 group_ids[ncurves] = group_id;
399 ncurves++;
400
401 if (col) {
402 ptr = col + 1;
403 }
404 } while (col);
405
406 OPENSSL_free(*out_group_ids);
407 *out_group_ids = group_ids;
408 *out_group_ids_len = ncurves;
409
410 return 1;
411
412err:
413 OPENSSL_free(group_ids);
414 return 0;
415}
416
Steven Valdezce902a92016-05-17 11:47:53 -0400417int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
418 const uint16_t *groups;
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400419 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -0400420 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400421 for (size_t i = 0; i < groups_len; i++) {
422 if (groups[i] == group_id) {
423 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800424 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 }
David Benjamin033e5f42014-11-13 18:47:41 -0500426
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400427 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800428}
David Benjamin033e5f42014-11-13 18:47:41 -0500429
David Benjaminc11ea9422017-08-29 16:33:21 -0400430// kVerifySignatureAlgorithms is the default list of accepted signature
431// algorithms for verifying.
432//
433// For now, RSA-PSS signature algorithms are not enabled on Android's system
434// BoringSSL. Once the change in Chrome has stuck and the values are finalized,
435// restore them.
David Benjamin3ef76972016-10-17 17:59:54 -0400436static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400437 // List our preferred algorithms first.
David Benjamin69522112017-03-28 15:38:29 -0500438 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400439 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400440#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400441 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400442#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400443 SSL_SIGN_RSA_PKCS1_SHA256,
444
David Benjaminc11ea9422017-08-29 16:33:21 -0400445 // Larger hashes are acceptable.
David Benjamin3a322f52016-10-26 12:45:35 -0400446 SSL_SIGN_ECDSA_SECP384R1_SHA384,
447#if !defined(BORINGSSL_ANDROID_SYSTEM)
448 SSL_SIGN_RSA_PSS_SHA384,
449#endif
450 SSL_SIGN_RSA_PKCS1_SHA384,
451
David Benjaminc11ea9422017-08-29 16:33:21 -0400452 // TODO(davidben): Remove this.
David Benjamin3ef76972016-10-17 17:59:54 -0400453#if defined(BORINGSSL_ANDROID_SYSTEM)
454 SSL_SIGN_ECDSA_SECP521R1_SHA512,
455#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400456#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400457 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400458#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400459 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400460
David Benjaminc11ea9422017-08-29 16:33:21 -0400461 // For now, SHA-1 is still accepted but least preferable.
David Benjamin3a322f52016-10-26 12:45:35 -0400462 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400463
David Benjamin3a322f52016-10-26 12:45:35 -0400464};
465
David Benjaminc11ea9422017-08-29 16:33:21 -0400466// kSignSignatureAlgorithms is the default list of supported signature
467// algorithms for signing.
468//
469// For now, RSA-PSS signature algorithms are not enabled on Android's system
470// BoringSSL. Once the change in Chrome has stuck and the values are finalized,
471// restore them.
David Benjamin3a322f52016-10-26 12:45:35 -0400472static const uint16_t kSignSignatureAlgorithms[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400473 // List our preferred algorithms first.
David Benjamin69522112017-03-28 15:38:29 -0500474 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400475 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400476#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400477 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400478#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400479 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400480
David Benjaminc11ea9422017-08-29 16:33:21 -0400481 // If needed, sign larger hashes.
482 //
483 // TODO(davidben): Determine which of these may be pruned.
David Benjamin3a322f52016-10-26 12:45:35 -0400484 SSL_SIGN_ECDSA_SECP384R1_SHA384,
485#if !defined(BORINGSSL_ANDROID_SYSTEM)
486 SSL_SIGN_RSA_PSS_SHA384,
487#endif
488 SSL_SIGN_RSA_PKCS1_SHA384,
489
490 SSL_SIGN_ECDSA_SECP521R1_SHA512,
491#if !defined(BORINGSSL_ANDROID_SYSTEM)
492 SSL_SIGN_RSA_PSS_SHA512,
493#endif
494 SSL_SIGN_RSA_PKCS1_SHA512,
495
David Benjaminc11ea9422017-08-29 16:33:21 -0400496 // If the peer supports nothing else, sign with SHA-1.
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400497 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400498 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400499};
500
David Benjamin69522112017-03-28 15:38:29 -0500501int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) {
David Benjamin71c21b42017-04-14 17:05:40 -0400502 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
503 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
504 if (ssl->ctx->num_verify_sigalgs != 0) {
505 sigalgs = ssl->ctx->verify_sigalgs;
506 num_sigalgs = ssl->ctx->num_verify_sigalgs;
507 }
508
509 for (size_t i = 0; i < num_sigalgs; i++) {
510 if (sigalgs == kVerifySignatureAlgorithms &&
511 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500512 !ssl->ctx->ed25519_enabled) {
513 continue;
514 }
David Benjamin71c21b42017-04-14 17:05:40 -0400515 if (!CBB_add_u16(out, sigalgs[i])) {
David Benjamin69522112017-03-28 15:38:29 -0500516 return 0;
517 }
518 }
519
520 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800521}
Adam Langley95c29f32014-06-20 12:00:00 -0700522
David Benjamin8d606e32017-06-15 22:43:04 -0400523int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg) {
David Benjamin71c21b42017-04-14 17:05:40 -0400524 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
525 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
526 if (ssl->ctx->num_verify_sigalgs != 0) {
527 sigalgs = ssl->ctx->verify_sigalgs;
528 num_sigalgs = ssl->ctx->num_verify_sigalgs;
529 }
530
531 for (size_t i = 0; i < num_sigalgs; i++) {
532 if (sigalgs == kVerifySignatureAlgorithms &&
533 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500534 !ssl->ctx->ed25519_enabled) {
535 continue;
536 }
David Benjamin71c21b42017-04-14 17:05:40 -0400537 if (sigalg == sigalgs[i]) {
David Benjamin3ef76972016-10-17 17:59:54 -0400538 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 }
540 }
541
David Benjamin3ef76972016-10-17 17:59:54 -0400542 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
543 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
544 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800545}
546
David Benjaminc11ea9422017-08-29 16:33:21 -0400547// tls_extension represents a TLS extension that is handled internally. The
548// |init| function is called for each handshake, before any other functions of
549// the extension. Then the add and parse callbacks are called as needed.
550//
551// The parse callbacks receive a |CBS| that contains the contents of the
552// extension (i.e. not including the type and length bytes). If an extension is
553// not received then the parse callbacks will be called with a NULL CBS so that
554// they can do any processing needed to handle the absence of an extension.
555//
556// The add callbacks receive a |CBB| to which the extension can be appended but
557// the function is responsible for appending the type and length bytes too.
558//
559// All callbacks return one for success and zero for error. If a parse function
560// returns zero then a fatal alert with value |*out_alert| will be sent. If
561// |*out_alert| isn't set, then a |decode_error| alert will be sent.
Adam Langley614c66a2015-06-12 15:26:58 -0700562struct tls_extension {
563 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500564 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700565
David Benjamin8c880a22016-12-03 02:20:34 -0500566 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
567 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
568 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700569
David Benjamin8c880a22016-12-03 02:20:34 -0500570 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
571 CBS *contents);
572 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700573};
574
David Benjamin8c880a22016-12-03 02:20:34 -0500575static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
576 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400577 if (contents != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400578 // Servers MUST NOT send this extension.
Steven Valdez6b8509a2016-07-12 13:38:32 -0400579 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
580 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
581 return 0;
582 }
583
584 return 1;
585}
586
David Benjamin8c880a22016-12-03 02:20:34 -0500587static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
588 CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400589 // This extension from the client is handled elsewhere.
Steven Valdez6b8509a2016-07-12 13:38:32 -0400590 return 1;
591}
592
David Benjamin8c880a22016-12-03 02:20:34 -0500593static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400594 return 1;
595}
Adam Langley614c66a2015-06-12 15:26:58 -0700596
David Benjaminc11ea9422017-08-29 16:33:21 -0400597// Server name indication (SNI).
598//
599// https://tools.ietf.org/html/rfc6066#section-3.
Adam Langley614c66a2015-06-12 15:26:58 -0700600
David Benjamin8c880a22016-12-03 02:20:34 -0500601static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
602 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700603 if (ssl->tlsext_hostname == NULL) {
604 return 1;
605 }
606
607 CBB contents, server_name_list, name;
608 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
609 !CBB_add_u16_length_prefixed(out, &contents) ||
610 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
611 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
612 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
613 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
614 strlen(ssl->tlsext_hostname)) ||
615 !CBB_flush(out)) {
616 return 0;
617 }
618
619 return 1;
620}
621
David Benjamin8c880a22016-12-03 02:20:34 -0500622static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500623 CBS *contents) {
David Benjamina8614602017-09-06 15:40:19 -0400624 // The server may acknowledge SNI with an empty extension. We check the syntax
625 // but otherwise ignore this signal.
626 return contents == NULL || CBS_len(contents) == 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700627}
628
David Benjamin8c880a22016-12-03 02:20:34 -0500629static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500630 CBS *contents) {
David Benjamina8614602017-09-06 15:40:19 -0400631 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700632 if (contents == NULL) {
633 return 1;
634 }
635
David Benjamin9b611e22016-03-03 08:48:30 -0500636 CBS server_name_list, host_name;
637 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700638 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500639 !CBS_get_u8(&server_name_list, &name_type) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400640 // Although the server_name extension was intended to be extensible to
641 // new name types and multiple names, OpenSSL 1.0.x had a bug which meant
642 // different name types will cause an error. Further, RFC 4366 originally
643 // defined syntax inextensibly. RFC 6066 corrected this mistake, but
644 // adding new name types is no longer feasible.
645 //
646 // Act as if the extensibility does not exist to simplify parsing.
David Benjamin9b611e22016-03-03 08:48:30 -0500647 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
648 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700649 CBS_len(contents) != 0) {
650 return 0;
651 }
652
David Benjamin9b611e22016-03-03 08:48:30 -0500653 if (name_type != TLSEXT_NAMETYPE_host_name ||
654 CBS_len(&host_name) == 0 ||
655 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
656 CBS_contains_zero_byte(&host_name)) {
657 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
658 return 0;
659 }
Adam Langley614c66a2015-06-12 15:26:58 -0700660
David Benjaminc11ea9422017-08-29 16:33:21 -0400661 // Copy the hostname as a string.
David Benjamina8614602017-09-06 15:40:19 -0400662 if (!CBS_strdup(&host_name, &ssl->s3->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900663 *out_alert = SSL_AD_INTERNAL_ERROR;
664 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700665 }
666
David Benjaminfd45ee72017-08-31 14:49:09 -0400667 hs->should_ack_sni = true;
Adam Langley614c66a2015-06-12 15:26:58 -0700668 return 1;
669}
670
David Benjamin8c880a22016-12-03 02:20:34 -0500671static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
672 if (hs->ssl->s3->session_reused ||
673 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700674 return 1;
675 }
676
677 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
678 !CBB_add_u16(out, 0 /* length */)) {
679 return 0;
680 }
681
682 return 1;
683}
684
685
David Benjaminc11ea9422017-08-29 16:33:21 -0400686// Renegotiation indication.
687//
688// https://tools.ietf.org/html/rfc5746
Adam Langley5021b222015-06-12 18:27:58 -0700689
David Benjamin8c880a22016-12-03 02:20:34 -0500690static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
691 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400692 // Renegotiation indication is not necessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -0400693 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400694 return 1;
695 }
696
David Benjamin52bf6902016-10-08 12:05:03 -0400697 assert(ssl->s3->initial_handshake_complete ==
698 (ssl->s3->previous_client_finished_len != 0));
699
Adam Langley5021b222015-06-12 18:27:58 -0700700 CBB contents, prev_finished;
701 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
702 !CBB_add_u16_length_prefixed(out, &contents) ||
703 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
704 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
705 ssl->s3->previous_client_finished_len) ||
706 !CBB_flush(out)) {
707 return 0;
708 }
709
710 return 1;
711}
712
David Benjamin8c880a22016-12-03 02:20:34 -0500713static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700714 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500715 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400716 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez246eeee2017-03-26 12:49:17 -0500717 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400718 return 0;
719 }
720
David Benjaminc11ea9422017-08-29 16:33:21 -0400721 // Servers may not switch between omitting the extension and supporting it.
722 // See RFC 5746, sections 3.5 and 4.2.
David Benjamin3e052de2015-11-25 20:10:31 -0500723 if (ssl->s3->initial_handshake_complete &&
724 (contents != NULL) != ssl->s3->send_connection_binding) {
725 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
726 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
727 return 0;
728 }
729
Adam Langley5021b222015-06-12 18:27:58 -0700730 if (contents == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400731 // Strictly speaking, if we want to avoid an attack we should *always* see
732 // RI even on initial ServerHello because the client doesn't see any
733 // renegotiation during an attack. However this would mean we could not
734 // connect to any server which doesn't support RI.
735 //
736 // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
737 // practical terms every client sets it so it's just assumed here.
David Benjamine9cddb82015-11-23 14:36:40 -0500738 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700739 }
740
741 const size_t expected_len = ssl->s3->previous_client_finished_len +
742 ssl->s3->previous_server_finished_len;
743
David Benjaminc11ea9422017-08-29 16:33:21 -0400744 // Check for logic errors
Adam Langley5021b222015-06-12 18:27:58 -0700745 assert(!expected_len || ssl->s3->previous_client_finished_len);
746 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400747 assert(ssl->s3->initial_handshake_complete ==
748 (ssl->s3->previous_client_finished_len != 0));
749 assert(ssl->s3->initial_handshake_complete ==
750 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700751
David Benjaminc11ea9422017-08-29 16:33:21 -0400752 // Parse out the extension contents.
Adam Langley5021b222015-06-12 18:27:58 -0700753 CBS renegotiated_connection;
754 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
755 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400756 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700757 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
758 return 0;
759 }
760
David Benjaminc11ea9422017-08-29 16:33:21 -0400761 // Check that the extension matches.
Adam Langley5021b222015-06-12 18:27:58 -0700762 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400763 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700764 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
765 return 0;
766 }
767
768 const uint8_t *d = CBS_data(&renegotiated_connection);
David Benjamin9343b0b2017-07-01 00:31:27 -0400769 int ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
770 ssl->s3->previous_client_finished_len) == 0;
771#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
772 ok = 1;
773#endif
774 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400775 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700776 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
777 return 0;
778 }
779 d += ssl->s3->previous_client_finished_len;
780
David Benjamin9343b0b2017-07-01 00:31:27 -0400781 ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
782 ssl->s3->previous_server_finished_len) == 0;
783#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
784 ok = 1;
785#endif
786 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400787 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley10e10602017-07-25 13:33:21 -0700788 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Adam Langley5021b222015-06-12 18:27:58 -0700789 return 0;
790 }
David Benjamin046bc1f2017-08-31 15:06:42 -0400791 ssl->s3->send_connection_binding = true;
Adam Langley5021b222015-06-12 18:27:58 -0700792
793 return 1;
794}
795
David Benjamin8c880a22016-12-03 02:20:34 -0500796static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700797 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500798 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400799 // Renegotiation isn't supported as a server so this function should never be
800 // called after the initial handshake.
Adam Langley5021b222015-06-12 18:27:58 -0700801 assert(!ssl->s3->initial_handshake_complete);
802
Steven Valdez143e8b32016-07-11 13:19:03 -0400803 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
804 return 1;
805 }
806
Adam Langley5021b222015-06-12 18:27:58 -0700807 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400808 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700809 }
810
811 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700812 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
813 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400814 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700815 return 0;
816 }
817
David Benjaminc11ea9422017-08-29 16:33:21 -0400818 // Check that the extension matches. We do not support renegotiation as a
819 // server, so this must be empty.
David Benjamin52bf6902016-10-08 12:05:03 -0400820 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400821 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700822 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
823 return 0;
824 }
825
David Benjamin046bc1f2017-08-31 15:06:42 -0400826 ssl->s3->send_connection_binding = true;
Adam Langley5021b222015-06-12 18:27:58 -0700827
828 return 1;
829}
830
David Benjamin8c880a22016-12-03 02:20:34 -0500831static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
832 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400833 // Renegotiation isn't supported as a server so this function should never be
834 // called after the initial handshake.
David Benjamin52bf6902016-10-08 12:05:03 -0400835 assert(!ssl->s3->initial_handshake_complete);
836
Steven Valdez143e8b32016-07-11 13:19:03 -0400837 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
838 return 1;
839 }
840
Adam Langley5021b222015-06-12 18:27:58 -0700841 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400842 !CBB_add_u16(out, 1 /* length */) ||
843 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700844 return 0;
845 }
846
847 return 1;
848}
849
Adam Langley0a056712015-07-01 15:03:33 -0700850
David Benjaminc11ea9422017-08-29 16:33:21 -0400851// Extended Master Secret.
852//
853// https://tools.ietf.org/html/rfc7627
Adam Langley0a056712015-07-01 15:03:33 -0700854
David Benjamin8c880a22016-12-03 02:20:34 -0500855static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400856 // Extended master secret is not necessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -0400857 if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700858 return 1;
859 }
860
861 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
862 !CBB_add_u16(out, 0 /* length */)) {
863 return 0;
864 }
865
866 return 1;
867}
868
David Benjamin8c880a22016-12-03 02:20:34 -0500869static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700870 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500871 SSL *const ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500872
873 if (contents != NULL) {
874 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
875 ssl->version == SSL3_VERSION ||
876 CBS_len(contents) != 0) {
David Benjamin163c9562016-08-29 23:14:17 -0400877 return 0;
878 }
879
David Benjaminfd45ee72017-08-31 14:49:09 -0400880 hs->extended_master_secret = true;
David Benjamin163c9562016-08-29 23:14:17 -0400881 }
882
David Benjaminc11ea9422017-08-29 16:33:21 -0400883 // Whether EMS is negotiated may not change on renegotiation.
David Benjaminfc02b592017-02-17 16:26:01 -0500884 if (ssl->s3->established_session != NULL &&
885 hs->extended_master_secret !=
David Benjaminfd45ee72017-08-31 14:49:09 -0400886 !!ssl->s3->established_session->extended_master_secret) {
David Benjaminfc02b592017-02-17 16:26:01 -0500887 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
888 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400889 return 0;
890 }
891
Adam Langley0a056712015-07-01 15:03:33 -0700892 return 1;
893}
894
David Benjamin8c880a22016-12-03 02:20:34 -0500895static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500896 CBS *contents) {
David Benjaminfc02b592017-02-17 16:26:01 -0500897 uint16_t version = ssl3_protocol_version(hs->ssl);
David Benjamin8c880a22016-12-03 02:20:34 -0500898 if (version >= TLS1_3_VERSION ||
899 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400900 return 1;
901 }
902
903 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700904 return 1;
905 }
906
907 if (CBS_len(contents) != 0) {
908 return 0;
909 }
910
David Benjaminfd45ee72017-08-31 14:49:09 -0400911 hs->extended_master_secret = true;
Adam Langley0a056712015-07-01 15:03:33 -0700912 return 1;
913}
914
David Benjamin8c880a22016-12-03 02:20:34 -0500915static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -0500916 if (!hs->extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700917 return 1;
918 }
919
920 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
921 !CBB_add_u16(out, 0 /* length */)) {
922 return 0;
923 }
924
925 return 1;
926}
927
Adam Langley9b05bc52015-07-01 15:25:33 -0700928
David Benjaminc11ea9422017-08-29 16:33:21 -0400929// Session tickets.
930//
931// https://tools.ietf.org/html/rfc5077
Adam Langley9b05bc52015-07-01 15:25:33 -0700932
David Benjamin8c880a22016-12-03 02:20:34 -0500933static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
934 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400935 // TLS 1.3 uses a different ticket extension.
David Benjamin68161cb2017-06-20 14:49:43 -0400936 if (hs->min_version >= TLS1_3_VERSION ||
David Benjamin7c7d8312016-08-20 13:39:03 -0400937 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700938 return 1;
939 }
940
941 const uint8_t *ticket_data = NULL;
942 int ticket_len = 0;
943
David Benjaminc11ea9422017-08-29 16:33:21 -0400944 // Renegotiation does not participate in session resumption. However, still
945 // advertise the extension to avoid potentially breaking servers which carry
946 // over the state from the previous handshake, such as OpenSSL servers
947 // without upstream's 3c3f0259238594d77264a78944d409f2127642c4.
Adam Langley9b05bc52015-07-01 15:25:33 -0700948 if (!ssl->s3->initial_handshake_complete &&
949 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400950 ssl->session->tlsext_tick != NULL &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400951 // Don't send TLS 1.3 session tickets in the ticket extension.
Steven Valdez8f36c512017-06-20 10:55:02 -0400952 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700953 ticket_data = ssl->session->tlsext_tick;
954 ticket_len = ssl->session->tlsext_ticklen;
955 }
956
957 CBB ticket;
958 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
959 !CBB_add_u16_length_prefixed(out, &ticket) ||
960 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
961 !CBB_flush(out)) {
962 return 0;
963 }
964
965 return 1;
966}
967
David Benjamin8c880a22016-12-03 02:20:34 -0500968static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -0700969 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500970 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -0700971 if (contents == NULL) {
972 return 1;
973 }
974
Steven Valdez143e8b32016-07-11 13:19:03 -0400975 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
976 return 0;
977 }
978
David Benjaminc11ea9422017-08-29 16:33:21 -0400979 // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
980 // this function should never be called, even if the server tries to send the
981 // extension.
Adam Langley9b05bc52015-07-01 15:25:33 -0700982 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
983
984 if (CBS_len(contents) != 0) {
985 return 0;
986 }
987
David Benjaminfd45ee72017-08-31 14:49:09 -0400988 hs->ticket_expected = true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700989 return 1;
990}
991
David Benjamin8c880a22016-12-03 02:20:34 -0500992static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
993 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700994 return 1;
995 }
996
David Benjaminc11ea9422017-08-29 16:33:21 -0400997 // If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true.
David Benjamin8c880a22016-12-03 02:20:34 -0500998 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -0700999
1000 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1001 !CBB_add_u16(out, 0 /* length */)) {
1002 return 0;
1003 }
1004
1005 return 1;
1006}
1007
1008
David Benjaminc11ea9422017-08-29 16:33:21 -04001009// Signature Algorithms.
1010//
1011// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
Adam Langley2e857bd2015-07-01 16:09:19 -07001012
David Benjamin8c880a22016-12-03 02:20:34 -05001013static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1014 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001015 if (hs->max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001016 return 1;
1017 }
1018
David Benjamin0fc37ef2016-08-17 15:29:46 -04001019 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001020 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1021 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin69522112017-03-28 15:38:29 -05001022 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
1023 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
1024 !CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001025 return 0;
1026 }
1027
1028 return 1;
1029}
1030
David Benjamin8c880a22016-12-03 02:20:34 -05001031static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001032 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001033 OPENSSL_free(hs->peer_sigalgs);
1034 hs->peer_sigalgs = NULL;
1035 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001036
Adam Langley2e857bd2015-07-01 16:09:19 -07001037 if (contents == NULL) {
1038 return 1;
1039 }
1040
1041 CBS supported_signature_algorithms;
1042 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001043 CBS_len(contents) != 0 ||
1044 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001045 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001046 return 0;
1047 }
1048
1049 return 1;
1050}
1051
Adam Langley2e857bd2015-07-01 16:09:19 -07001052
David Benjaminc11ea9422017-08-29 16:33:21 -04001053// OCSP Stapling.
1054//
1055// https://tools.ietf.org/html/rfc6066#section-8
Adam Langleybb0bd042015-07-01 16:21:03 -07001056
David Benjamin8c880a22016-12-03 02:20:34 -05001057static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1058 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001059 if (!ssl->ocsp_stapling_enabled) {
1060 return 1;
1061 }
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)) {
1070 return 0;
1071 }
1072
1073 return 1;
1074}
1075
David Benjamin8c880a22016-12-03 02:20:34 -05001076static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001077 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001078 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001079 if (contents == NULL) {
1080 return 1;
1081 }
1082
David Benjaminc11ea9422017-08-29 16:33:21 -04001083 // TLS 1.3 OCSP responses are included in the Certificate extensions.
Steven Valdeza833c352016-11-01 13:39:36 -04001084 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001085 return 0;
1086 }
1087
David Benjaminc11ea9422017-08-29 16:33:21 -04001088 // OCSP stapling is forbidden on non-certificate ciphers.
Steven Valdeza833c352016-11-01 13:39:36 -04001089 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001090 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001091 return 0;
1092 }
1093
David Benjaminc11ea9422017-08-29 16:33:21 -04001094 // 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 Benjamin942f4ed2016-07-16 19:03:49 +03001097
David Benjaminfd45ee72017-08-31 14:49:09 -04001098 hs->certificate_status_expected = true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001099 return 1;
1100}
1101
David Benjamin8c880a22016-12-03 02:20:34 -05001102static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001103 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001104 if (contents == NULL) {
1105 return 1;
1106 }
1107
1108 uint8_t status_type;
1109 if (!CBS_get_u8(contents, &status_type)) {
1110 return 0;
1111 }
1112
David Benjaminc11ea9422017-08-29 16:33:21 -04001113 // We cannot decide whether OCSP stapling will occur yet because the correct
1114 // SSL_CTX might not have been selected.
David Benjamin8c880a22016-12-03 02:20:34 -05001115 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001116
Adam Langleybb0bd042015-07-01 16:21:03 -07001117 return 1;
1118}
1119
David Benjamin8c880a22016-12-03 02:20:34 -05001120static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1121 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001122 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001123 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001124 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001125 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001126 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001127 return 1;
1128 }
1129
David Benjaminfd45ee72017-08-31 14:49:09 -04001130 hs->certificate_status_expected = true;
David Benjamin942f4ed2016-07-16 19:03:49 +03001131
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001132 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001133 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001134}
1135
1136
David Benjaminc11ea9422017-08-29 16:33:21 -04001137// Next protocol negotiation.
1138//
1139// https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
Adam Langley97dfcbf2015-07-01 18:35:20 -07001140
David Benjamin8c880a22016-12-03 02:20:34 -05001141static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1142 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001143 if (ssl->s3->initial_handshake_complete ||
1144 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001145 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001146 return 1;
1147 }
1148
1149 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1150 !CBB_add_u16(out, 0 /* length */)) {
1151 return 0;
1152 }
1153
1154 return 1;
1155}
1156
David Benjamin8c880a22016-12-03 02:20:34 -05001157static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001158 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001159 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001160 if (contents == NULL) {
1161 return 1;
1162 }
1163
Steven Valdez143e8b32016-07-11 13:19:03 -04001164 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1165 return 0;
1166 }
1167
David Benjaminc11ea9422017-08-29 16:33:21 -04001168 // If any of these are false then we should never have sent the NPN
1169 // extension in the ClientHello and thus this function should never have been
1170 // called.
Adam Langley97dfcbf2015-07-01 18:35:20 -07001171 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001172 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001173 assert(ssl->ctx->next_proto_select_cb != NULL);
1174
David Benjamin76c2efc2015-08-31 14:24:29 -04001175 if (ssl->s3->alpn_selected != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001176 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001177 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1178 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1179 return 0;
1180 }
1181
Adam Langley97dfcbf2015-07-01 18:35:20 -07001182 const uint8_t *const orig_contents = CBS_data(contents);
1183 const size_t orig_len = CBS_len(contents);
1184
1185 while (CBS_len(contents) != 0) {
1186 CBS proto;
1187 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1188 CBS_len(&proto) == 0) {
1189 return 0;
1190 }
1191 }
1192
1193 uint8_t *selected;
1194 uint8_t selected_len;
1195 if (ssl->ctx->next_proto_select_cb(
1196 ssl, &selected, &selected_len, orig_contents, orig_len,
1197 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1198 *out_alert = SSL_AD_INTERNAL_ERROR;
1199 return 0;
1200 }
1201
David Benjamin79978df2015-12-25 15:56:49 -05001202 OPENSSL_free(ssl->s3->next_proto_negotiated);
David Benjamin81678aa2017-07-12 22:43:42 -04001203 ssl->s3->next_proto_negotiated =
1204 (uint8_t *)BUF_memdup(selected, selected_len);
David Benjamin79978df2015-12-25 15:56:49 -05001205 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001206 *out_alert = SSL_AD_INTERNAL_ERROR;
1207 return 0;
1208 }
1209
David Benjamin79978df2015-12-25 15:56:49 -05001210 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjaminfd45ee72017-08-31 14:49:09 -04001211 hs->next_proto_neg_seen = true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001212
1213 return 1;
1214}
1215
David Benjamin8c880a22016-12-03 02:20:34 -05001216static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001217 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001218 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001219 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1220 return 1;
1221 }
1222
Adam Langley97dfcbf2015-07-01 18:35:20 -07001223 if (contents != NULL && CBS_len(contents) != 0) {
1224 return 0;
1225 }
1226
1227 if (contents == NULL ||
1228 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001229 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001230 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001231 return 1;
1232 }
1233
David Benjaminfd45ee72017-08-31 14:49:09 -04001234 hs->next_proto_neg_seen = true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001235 return 1;
1236}
1237
David Benjamin8c880a22016-12-03 02:20:34 -05001238static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1239 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001240 // |next_proto_neg_seen| might have been cleared when an ALPN extension was
1241 // parsed.
David Benjamin8c880a22016-12-03 02:20:34 -05001242 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001243 return 1;
1244 }
1245
1246 const uint8_t *npa;
1247 unsigned npa_len;
1248
1249 if (ssl->ctx->next_protos_advertised_cb(
1250 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1251 SSL_TLSEXT_ERR_OK) {
David Benjaminfd45ee72017-08-31 14:49:09 -04001252 hs->next_proto_neg_seen = false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001253 return 1;
1254 }
1255
1256 CBB contents;
1257 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1258 !CBB_add_u16_length_prefixed(out, &contents) ||
1259 !CBB_add_bytes(&contents, npa, npa_len) ||
1260 !CBB_flush(out)) {
1261 return 0;
1262 }
1263
1264 return 1;
1265}
1266
1267
David Benjaminc11ea9422017-08-29 16:33:21 -04001268// Signed certificate timestamps.
1269//
1270// https://tools.ietf.org/html/rfc6962#section-3.3.1
Adam Langleyab8d87d2015-07-10 12:21:39 -07001271
David Benjamin8c880a22016-12-03 02:20:34 -05001272static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1273 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001274 if (!ssl->signed_cert_timestamps_enabled) {
1275 return 1;
1276 }
1277
1278 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1279 !CBB_add_u16(out, 0 /* length */)) {
1280 return 0;
1281 }
1282
1283 return 1;
1284}
1285
David Benjamin8c880a22016-12-03 02:20:34 -05001286static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001287 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001288 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001289 if (contents == NULL) {
1290 return 1;
1291 }
1292
David Benjaminc11ea9422017-08-29 16:33:21 -04001293 // TLS 1.3 SCTs are included in the Certificate extensions.
Steven Valdeza833c352016-11-01 13:39:36 -04001294 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001295 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001296 return 0;
1297 }
1298
David Benjaminc11ea9422017-08-29 16:33:21 -04001299 // If this is false then we should never have sent the SCT extension in the
1300 // ClientHello and thus this function should never have been called.
Adam Langleyab8d87d2015-07-10 12:21:39 -07001301 assert(ssl->signed_cert_timestamps_enabled);
1302
Adam Langleycfa08c32016-11-17 13:21:27 -08001303 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001304 *out_alert = SSL_AD_DECODE_ERROR;
1305 return 0;
1306 }
1307
David Benjaminc11ea9422017-08-29 16:33:21 -04001308 // Session resumption uses the original session information. The extension
1309 // should not be sent on resumption, but RFC 6962 did not make it a
1310 // requirement, so tolerate this.
1311 //
1312 // TODO(davidben): Enforce this anyway.
David Benjamin8fc2dc02017-08-22 15:07:51 -07001313 if (!ssl->s3->session_reused) {
1314 CRYPTO_BUFFER_free(hs->new_session->signed_cert_timestamp_list);
1315 hs->new_session->signed_cert_timestamp_list =
1316 CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool);
1317 if (hs->new_session->signed_cert_timestamp_list == nullptr) {
1318 *out_alert = SSL_AD_INTERNAL_ERROR;
1319 return 0;
1320 }
Adam Langleyab8d87d2015-07-10 12:21:39 -07001321 }
1322
1323 return 1;
1324}
1325
David Benjamin8c880a22016-12-03 02:20:34 -05001326static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001327 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001328 if (contents == NULL) {
1329 return 1;
1330 }
1331
1332 if (CBS_len(contents) != 0) {
1333 return 0;
1334 }
1335
David Benjaminfd45ee72017-08-31 14:49:09 -04001336 hs->scts_requested = true;
David Benjamin53210cb2016-11-16 09:01:48 +09001337 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001338}
1339
David Benjamin8c880a22016-12-03 02:20:34 -05001340static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1341 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001342 // The extension shouldn't be sent when resuming sessions.
Steven Valdeza833c352016-11-01 13:39:36 -04001343 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1344 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001345 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001346 return 1;
1347 }
1348
1349 CBB contents;
1350 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1351 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001352 CBB_add_bytes(
1353 &contents,
1354 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1355 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001356 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001357}
1358
1359
David Benjaminc11ea9422017-08-29 16:33:21 -04001360// Application-level Protocol Negotiation.
1361//
1362// https://tools.ietf.org/html/rfc7301
Adam Langleyf18e4532015-07-10 13:39:53 -07001363
David Benjamin8c880a22016-12-03 02:20:34 -05001364static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1365 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001366 if (ssl->alpn_client_proto_list == NULL ||
1367 ssl->s3->initial_handshake_complete) {
1368 return 1;
1369 }
1370
1371 CBB contents, proto_list;
1372 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1373 !CBB_add_u16_length_prefixed(out, &contents) ||
1374 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1375 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1376 ssl->alpn_client_proto_list_len) ||
1377 !CBB_flush(out)) {
1378 return 0;
1379 }
1380
1381 return 1;
1382}
1383
David Benjamin8c880a22016-12-03 02:20:34 -05001384static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001385 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001386 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001387 if (contents == NULL) {
1388 return 1;
1389 }
1390
1391 assert(!ssl->s3->initial_handshake_complete);
1392 assert(ssl->alpn_client_proto_list != NULL);
1393
David Benjamin8c880a22016-12-03 02:20:34 -05001394 if (hs->next_proto_neg_seen) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001395 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001396 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1397 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1398 return 0;
1399 }
1400
David Benjaminc11ea9422017-08-29 16:33:21 -04001401 // The extension data consists of a ProtocolNameList which must have
1402 // exactly one ProtocolName. Each of these is length-prefixed.
Adam Langleyf18e4532015-07-10 13:39:53 -07001403 CBS protocol_name_list, protocol_name;
1404 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1405 CBS_len(contents) != 0 ||
1406 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001407 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001408 CBS_len(&protocol_name) == 0 ||
1409 CBS_len(&protocol_name_list) != 0) {
1410 return 0;
1411 }
1412
David Benjaminc8ff30c2017-04-04 13:52:36 -04001413 if (!ssl->ctx->allow_unknown_alpn_protos) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001414 // Check that the protocol name is one of the ones we advertised.
David Benjaminc8ff30c2017-04-04 13:52:36 -04001415 int protocol_ok = 0;
1416 CBS client_protocol_name_list, client_protocol_name;
1417 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1418 ssl->alpn_client_proto_list_len);
1419 while (CBS_len(&client_protocol_name_list) > 0) {
1420 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1421 &client_protocol_name)) {
1422 *out_alert = SSL_AD_INTERNAL_ERROR;
1423 return 0;
1424 }
1425
1426 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1427 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1428 CBS_data(&protocol_name),
1429 CBS_len(&protocol_name)) == 0) {
1430 protocol_ok = 1;
1431 break;
1432 }
1433 }
1434
1435 if (!protocol_ok) {
1436 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1437 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3e517572016-08-11 11:52:23 -04001438 return 0;
1439 }
David Benjamin3e517572016-08-11 11:52:23 -04001440 }
1441
Adam Langleyf18e4532015-07-10 13:39:53 -07001442 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1443 &ssl->s3->alpn_selected_len)) {
1444 *out_alert = SSL_AD_INTERNAL_ERROR;
1445 return 0;
1446 }
1447
1448 return 1;
1449}
1450
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001451int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001452 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001453 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001454 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001455 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001456 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001457 client_hello, &contents,
1458 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001459 // Ignore ALPN if not configured or no extension was supplied.
Adam Langleyf18e4532015-07-10 13:39:53 -07001460 return 1;
1461 }
1462
David Benjaminc11ea9422017-08-29 16:33:21 -04001463 // ALPN takes precedence over NPN.
David Benjaminfd45ee72017-08-31 14:49:09 -04001464 hs->next_proto_neg_seen = false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001465
1466 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001467 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1468 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001469 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001470 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1471 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001472 return 0;
1473 }
1474
David Benjaminc11ea9422017-08-29 16:33:21 -04001475 // Validate the protocol list.
Adam Langleyf18e4532015-07-10 13:39:53 -07001476 CBS protocol_name_list_copy = protocol_name_list;
1477 while (CBS_len(&protocol_name_list_copy) > 0) {
1478 CBS protocol_name;
1479
1480 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001481 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001482 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001483 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1484 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001485 return 0;
1486 }
1487 }
1488
1489 const uint8_t *selected;
1490 uint8_t selected_len;
1491 if (ssl->ctx->alpn_select_cb(
1492 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1493 CBS_len(&protocol_name_list),
1494 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1495 OPENSSL_free(ssl->s3->alpn_selected);
David Benjamin81678aa2017-07-12 22:43:42 -04001496 ssl->s3->alpn_selected = (uint8_t *)BUF_memdup(selected, selected_len);
Adam Langleyf18e4532015-07-10 13:39:53 -07001497 if (ssl->s3->alpn_selected == NULL) {
1498 *out_alert = SSL_AD_INTERNAL_ERROR;
1499 return 0;
1500 }
1501 ssl->s3->alpn_selected_len = selected_len;
1502 }
1503
1504 return 1;
1505}
1506
David Benjamin8c880a22016-12-03 02:20:34 -05001507static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1508 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001509 if (ssl->s3->alpn_selected == NULL) {
1510 return 1;
1511 }
1512
1513 CBB contents, proto_list, proto;
1514 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1515 !CBB_add_u16_length_prefixed(out, &contents) ||
1516 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1517 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001518 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1519 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001520 !CBB_flush(out)) {
1521 return 0;
1522 }
1523
1524 return 1;
1525}
1526
1527
David Benjaminc11ea9422017-08-29 16:33:21 -04001528// Channel ID.
1529//
1530// https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
Adam Langley49c7af12015-07-10 14:33:46 -07001531
David Benjamin8c880a22016-12-03 02:20:34 -05001532static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
David Benjamin046bc1f2017-08-31 15:06:42 -04001533 hs->ssl->s3->tlsext_channel_id_valid = false;
Adam Langley49c7af12015-07-10 14:33:46 -07001534}
1535
David Benjamin8c880a22016-12-03 02:20:34 -05001536static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1537 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001538 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001539 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001540 return 1;
1541 }
1542
1543 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1544 !CBB_add_u16(out, 0 /* length */)) {
1545 return 0;
1546 }
1547
1548 return 1;
1549}
1550
David Benjamin8c880a22016-12-03 02:20:34 -05001551static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1552 uint8_t *out_alert, CBS *contents) {
1553 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001554 if (contents == NULL) {
1555 return 1;
1556 }
1557
David Benjamince079fd2016-08-02 16:22:34 -04001558 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001559 assert(ssl->tlsext_channel_id_enabled);
1560
1561 if (CBS_len(contents) != 0) {
1562 return 0;
1563 }
1564
David Benjamin046bc1f2017-08-31 15:06:42 -04001565 ssl->s3->tlsext_channel_id_valid = true;
Adam Langley49c7af12015-07-10 14:33:46 -07001566 return 1;
1567}
1568
David Benjamin8c880a22016-12-03 02:20:34 -05001569static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1570 uint8_t *out_alert, CBS *contents) {
1571 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001572 if (contents == NULL ||
1573 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001574 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001575 return 1;
1576 }
1577
1578 if (CBS_len(contents) != 0) {
1579 return 0;
1580 }
1581
David Benjamin046bc1f2017-08-31 15:06:42 -04001582 ssl->s3->tlsext_channel_id_valid = true;
Adam Langley49c7af12015-07-10 14:33:46 -07001583 return 1;
1584}
1585
David Benjamin8c880a22016-12-03 02:20:34 -05001586static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1587 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001588 if (!ssl->s3->tlsext_channel_id_valid) {
1589 return 1;
1590 }
1591
1592 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1593 !CBB_add_u16(out, 0 /* length */)) {
1594 return 0;
1595 }
1596
1597 return 1;
1598}
1599
Adam Langley391250d2015-07-15 19:06:07 -07001600
David Benjaminc11ea9422017-08-29 16:33:21 -04001601// Secure Real-time Transport Protocol (SRTP) extension.
1602//
1603// https://tools.ietf.org/html/rfc5764
Adam Langley391250d2015-07-15 19:06:07 -07001604
Adam Langley391250d2015-07-15 19:06:07 -07001605
David Benjamin8c880a22016-12-03 02:20:34 -05001606static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1607 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001608}
1609
David Benjamin8c880a22016-12-03 02:20:34 -05001610static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1611 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001612 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
David Benjaminee910bf2017-07-25 22:36:00 -04001613 if (profiles == NULL ||
1614 sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
Adam Langley391250d2015-07-15 19:06:07 -07001615 return 1;
1616 }
1617
1618 CBB contents, profile_ids;
1619 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1620 !CBB_add_u16_length_prefixed(out, &contents) ||
1621 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1622 return 0;
1623 }
1624
David Benjaminee910bf2017-07-25 22:36:00 -04001625 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
1626 if (!CBB_add_u16(&profile_ids, profile->id)) {
Adam Langley391250d2015-07-15 19:06:07 -07001627 return 0;
1628 }
1629 }
1630
1631 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1632 !CBB_flush(out)) {
1633 return 0;
1634 }
1635
1636 return 1;
1637}
1638
David Benjamin8c880a22016-12-03 02:20:34 -05001639static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001640 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001641 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001642 if (contents == NULL) {
1643 return 1;
1644 }
1645
David Benjaminc11ea9422017-08-29 16:33:21 -04001646 // The extension consists of a u16-prefixed profile ID list containing a
1647 // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1648 //
1649 // See https://tools.ietf.org/html/rfc5764#section-4.1.1
Adam Langley391250d2015-07-15 19:06:07 -07001650 CBS profile_ids, srtp_mki;
1651 uint16_t profile_id;
1652 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1653 !CBS_get_u16(&profile_ids, &profile_id) ||
1654 CBS_len(&profile_ids) != 0 ||
1655 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1656 CBS_len(contents) != 0) {
1657 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1658 return 0;
1659 }
1660
1661 if (CBS_len(&srtp_mki) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001662 // Must be no MKI, since we never offer one.
Adam Langley391250d2015-07-15 19:06:07 -07001663 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1664 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1665 return 0;
1666 }
1667
1668 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1669
David Benjaminc11ea9422017-08-29 16:33:21 -04001670 // Check to see if the server gave us something we support (and presumably
1671 // offered).
David Benjaminee910bf2017-07-25 22:36:00 -04001672 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001673 if (profile->id == profile_id) {
1674 ssl->srtp_profile = profile;
1675 return 1;
1676 }
1677 }
1678
1679 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1680 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1681 return 0;
1682}
1683
David Benjamin8c880a22016-12-03 02:20:34 -05001684static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001685 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001686 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001687 if (contents == NULL) {
1688 return 1;
1689 }
1690
1691 CBS profile_ids, srtp_mki;
1692 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1693 CBS_len(&profile_ids) < 2 ||
1694 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1695 CBS_len(contents) != 0) {
1696 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1697 return 0;
1698 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001699 // Discard the MKI value for now.
Adam Langley391250d2015-07-15 19:06:07 -07001700
1701 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1702 SSL_get_srtp_profiles(ssl);
1703
David Benjaminc11ea9422017-08-29 16:33:21 -04001704 // Pick the server's most preferred profile.
David Benjaminee910bf2017-07-25 22:36:00 -04001705 for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001706 CBS profile_ids_tmp;
1707 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1708
1709 while (CBS_len(&profile_ids_tmp) > 0) {
1710 uint16_t profile_id;
1711 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1712 return 0;
1713 }
1714
1715 if (server_profile->id == profile_id) {
1716 ssl->srtp_profile = server_profile;
1717 return 1;
1718 }
1719 }
1720 }
1721
1722 return 1;
1723}
1724
David Benjamin8c880a22016-12-03 02:20:34 -05001725static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1726 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001727 if (ssl->srtp_profile == NULL) {
1728 return 1;
1729 }
1730
1731 CBB contents, profile_ids;
1732 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1733 !CBB_add_u16_length_prefixed(out, &contents) ||
1734 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1735 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1736 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1737 !CBB_flush(out)) {
1738 return 0;
1739 }
1740
1741 return 1;
1742}
1743
Adam Langleybdd5d662015-07-20 16:19:08 -07001744
David Benjaminc11ea9422017-08-29 16:33:21 -04001745// EC point formats.
1746//
1747// https://tools.ietf.org/html/rfc4492#section-5.1.2
Adam Langleybdd5d662015-07-20 16:19:08 -07001748
David Benjamin8c880a22016-12-03 02:20:34 -05001749static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001750 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001751 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1752 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001753 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1754 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001755 !CBB_flush(out)) {
1756 return 0;
1757 }
1758
1759 return 1;
1760}
1761
David Benjamin8c880a22016-12-03 02:20:34 -05001762static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001763 // The point format extension is unneccessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -04001764 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin70aba262016-11-01 12:08:15 -04001765 return 1;
1766 }
1767
David Benjamin8c880a22016-12-03 02:20:34 -05001768 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001769}
1770
David Benjamin8c880a22016-12-03 02:20:34 -05001771static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001772 CBS *contents) {
1773 if (contents == NULL) {
1774 return 1;
1775 }
1776
David Benjamin8c880a22016-12-03 02:20:34 -05001777 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001778 return 0;
1779 }
1780
Adam Langleybdd5d662015-07-20 16:19:08 -07001781 CBS ec_point_format_list;
1782 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1783 CBS_len(contents) != 0) {
1784 return 0;
1785 }
1786
David Benjaminc11ea9422017-08-29 16:33:21 -04001787 // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1788 // point format.
David Benjamin17cf2cb2016-12-13 01:07:13 -05001789 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1790 TLSEXT_ECPOINTFORMAT_uncompressed,
1791 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001792 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001793 return 0;
1794 }
1795
1796 return 1;
1797}
1798
David Benjamin8c880a22016-12-03 02:20:34 -05001799static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001800 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001801 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001802 return 1;
1803 }
1804
David Benjamin8c880a22016-12-03 02:20:34 -05001805 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001806}
1807
David Benjamin8c880a22016-12-03 02:20:34 -05001808static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1809 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001810 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1811 return 1;
1812 }
1813
David Benjamin45738dd2017-02-09 20:01:26 -05001814 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1815 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001816 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001817
1818 if (!using_ecc) {
1819 return 1;
1820 }
1821
David Benjamin8c880a22016-12-03 02:20:34 -05001822 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001823}
1824
Steven Valdeza833c352016-11-01 13:39:36 -04001825
David Benjaminc11ea9422017-08-29 16:33:21 -04001826// Pre Shared Key
1827//
1828// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
Steven Valdez4aa154e2016-07-29 14:32:55 -04001829
David Benjamin8c880a22016-12-03 02:20:34 -05001830static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1831 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001832 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001833 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001834 return 0;
1835 }
1836
Steven Valdez8f36c512017-06-20 10:55:02 -04001837 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001838 return 15 + ssl->session->tlsext_ticklen + binder_len;
1839}
1840
David Benjamin8c880a22016-12-03 02:20:34 -05001841static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1842 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001843 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001844 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001845 return 1;
1846 }
1847
David Benjaminad8f5e12017-02-20 17:00:20 -05001848 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001849 ssl_get_current_time(ssl, &now);
1850 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1851 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1852
David Benjaminc11ea9422017-08-29 16:33:21 -04001853 // Fill in a placeholder zero binder of the appropriate length. It will be
1854 // computed and filled in later after length prefixes are computed.
Steven Valdeza833c352016-11-01 13:39:36 -04001855 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez8f36c512017-06-20 10:55:02 -04001856 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001857
1858 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001859 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1860 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001861 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001862 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1863 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001864 ssl->session->tlsext_ticklen) ||
1865 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1866 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1867 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1868 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001869 return 0;
1870 }
1871
David Benjaminfd45ee72017-08-31 14:49:09 -04001872 hs->needs_psk_binder = true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001873 return CBB_flush(out);
1874}
1875
David Benjamin8baf9632016-11-17 17:11:16 +09001876int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1877 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001878 CBS *contents) {
1879 uint16_t psk_id;
1880 if (!CBS_get_u16(contents, &psk_id) ||
1881 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001882 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001883 *out_alert = SSL_AD_DECODE_ERROR;
1884 return 0;
1885 }
1886
David Benjaminc11ea9422017-08-29 16:33:21 -04001887 // We only advertise one PSK identity, so the only legal index is zero.
Steven Valdez4aa154e2016-07-29 14:32:55 -04001888 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001889 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001890 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1891 return 0;
1892 }
1893
1894 return 1;
1895}
1896
David Benjamin35ac5b72017-03-03 15:05:56 -05001897int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001898 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001899 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001900 // We only process the first PSK identity since we don't support pure PSK.
David Benjamin707af292017-03-10 17:47:18 -05001901 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001902 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001903 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001904 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001905 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001906 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001907 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001908 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001909 *out_alert = SSL_AD_DECODE_ERROR;
1910 return 0;
1911 }
1912
Steven Valdeza833c352016-11-01 13:39:36 -04001913 *out_binders = binders;
1914
David Benjaminc11ea9422017-08-29 16:33:21 -04001915 // Check the syntax of the remaining identities, but do not process them.
David Benjaminaedf3032016-12-01 16:47:56 -05001916 size_t num_identities = 1;
1917 while (CBS_len(&identities) != 0) {
1918 CBS unused_ticket;
1919 uint32_t unused_obfuscated_ticket_age;
1920 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1921 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1922 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1923 *out_alert = SSL_AD_DECODE_ERROR;
1924 return 0;
1925 }
1926
1927 num_identities++;
1928 }
1929
David Benjaminc11ea9422017-08-29 16:33:21 -04001930 // Check the syntax of the binders. The value will be checked later if
1931 // resuming.
David Benjaminaedf3032016-12-01 16:47:56 -05001932 size_t num_binders = 0;
1933 while (CBS_len(&binders) != 0) {
1934 CBS binder;
1935 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1936 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1937 *out_alert = SSL_AD_DECODE_ERROR;
1938 return 0;
1939 }
1940
1941 num_binders++;
1942 }
1943
1944 if (num_identities != num_binders) {
1945 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1946 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04001947 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04001948 }
1949
David Benjamine7f60a22016-11-16 18:54:25 +09001950 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001951}
1952
David Benjamin8baf9632016-11-17 17:11:16 +09001953int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1954 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001955 return 1;
1956 }
1957
1958 CBB contents;
1959 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1960 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001961 // We only consider the first identity for resumption
Steven Valdez4aa154e2016-07-29 14:32:55 -04001962 !CBB_add_u16(&contents, 0) ||
1963 !CBB_flush(out)) {
1964 return 0;
1965 }
1966
1967 return 1;
1968}
1969
1970
David Benjaminc11ea9422017-08-29 16:33:21 -04001971// Pre-Shared Key Exchange Modes
1972//
1973// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7
Steven Valdeza4ee74d2016-11-29 13:36:45 -05001974
David Benjamin8c880a22016-12-03 02:20:34 -05001975static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
1976 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04001977 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001978 return 1;
1979 }
1980
1981 CBB contents, ke_modes;
1982 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
1983 !CBB_add_u16_length_prefixed(out, &contents) ||
1984 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
1985 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
1986 return 0;
1987 }
1988
1989 return CBB_flush(out);
1990}
1991
David Benjamin8c880a22016-12-03 02:20:34 -05001992static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09001993 uint8_t *out_alert,
1994 CBS *contents) {
1995 if (contents == NULL) {
1996 return 1;
1997 }
1998
Steven Valdeza833c352016-11-01 13:39:36 -04001999 CBS ke_modes;
2000 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2001 CBS_len(&ke_modes) == 0 ||
2002 CBS_len(contents) != 0) {
2003 *out_alert = SSL_AD_DECODE_ERROR;
2004 return 0;
2005 }
2006
David Benjaminc11ea9422017-08-29 16:33:21 -04002007 // We only support tickets with PSK_DHE_KE.
David Benjamin17cf2cb2016-12-13 01:07:13 -05002008 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2009 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002010
2011 return 1;
2012}
2013
2014
David Benjaminc11ea9422017-08-29 16:33:21 -04002015// Early Data Indication
2016//
2017// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002018
David Benjamin8c880a22016-12-03 02:20:34 -05002019static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002020 SSL *const ssl = hs->ssl;
Steven Valdez2d850622017-01-11 11:34:52 -05002021 if (ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04002022 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION ||
Steven Valdez2d850622017-01-11 11:34:52 -05002023 ssl->session->ticket_max_early_data == 0 ||
2024 hs->received_hello_retry_request ||
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05002025 !ssl->cert->enable_early_data) {
Steven Valdez2d850622017-01-11 11:34:52 -05002026 return 1;
2027 }
2028
David Benjaminfd45ee72017-08-31 14:49:09 -04002029 hs->early_data_offered = true;
Steven Valdez2d850622017-01-11 11:34:52 -05002030
2031 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2032 !CBB_add_u16(out, 0) ||
2033 !CBB_flush(out)) {
2034 return 0;
2035 }
2036
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002037 return 1;
2038}
2039
Steven Valdez2d850622017-01-11 11:34:52 -05002040static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002041 uint8_t *out_alert, CBS *contents) {
2042 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002043 if (contents == NULL) {
2044 return 1;
2045 }
2046
2047 if (CBS_len(contents) != 0) {
2048 *out_alert = SSL_AD_DECODE_ERROR;
2049 return 0;
2050 }
2051
Steven Valdez2d850622017-01-11 11:34:52 -05002052 if (!ssl->s3->session_reused) {
2053 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2054 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2055 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002056 }
Steven Valdez2d850622017-01-11 11:34:52 -05002057
2058 ssl->early_data_accepted = 1;
2059 return 1;
2060}
2061
2062static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2063 uint8_t *out_alert, CBS *contents) {
2064 SSL *const ssl = hs->ssl;
2065 if (contents == NULL ||
2066 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2067 return 1;
2068 }
2069
2070 if (CBS_len(contents) != 0) {
2071 *out_alert = SSL_AD_DECODE_ERROR;
2072 return 0;
2073 }
2074
David Benjaminfd45ee72017-08-31 14:49:09 -04002075 hs->early_data_offered = true;
Steven Valdez2d850622017-01-11 11:34:52 -05002076 return 1;
2077}
2078
2079static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2080 if (!hs->ssl->early_data_accepted) {
2081 return 1;
2082 }
2083
2084 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2085 !CBB_add_u16(out, 0) ||
2086 !CBB_flush(out)) {
2087 return 0;
2088 }
2089
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002090 return 1;
2091}
2092
2093
David Benjaminc11ea9422017-08-29 16:33:21 -04002094// Key Share
2095//
2096// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5
Steven Valdez143e8b32016-07-11 13:19:03 -04002097
David Benjamin8c880a22016-12-03 02:20:34 -05002098static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2099 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002100 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002101 return 1;
2102 }
2103
2104 CBB contents, kse_bytes;
2105 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2106 !CBB_add_u16_length_prefixed(out, &contents) ||
2107 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2108 return 0;
2109 }
2110
David Benjamin8c880a22016-12-03 02:20:34 -05002111 uint16_t group_id = hs->retry_group;
2112 if (hs->received_hello_retry_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002113 // We received a HelloRetryRequest without a new curve, so there is no new
2114 // share to append. Leave |hs->key_share| as-is.
Steven Valdeza833c352016-11-01 13:39:36 -04002115 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002116 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2117 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002118 return 0;
2119 }
David Benjamin8c880a22016-12-03 02:20:34 -05002120 OPENSSL_free(hs->key_share_bytes);
2121 hs->key_share_bytes = NULL;
2122 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002123 if (group_id == 0) {
2124 return CBB_flush(out);
2125 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002126 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04002127 // Add a fake group. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002128 if (ssl->ctx->grease_enabled &&
2129 (!CBB_add_u16(&kse_bytes,
2130 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2131 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2132 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2133 return 0;
2134 }
2135
David Benjaminc11ea9422017-08-29 16:33:21 -04002136 // Predict the most preferred group.
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002137 const uint16_t *groups;
2138 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002139 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002140 if (groups_len == 0) {
2141 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2142 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002143 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002144
2145 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002146 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002147
David Benjaminc642aca2017-07-19 23:28:43 -04002148 hs->key_share = SSLKeyShare::Create(group_id);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002149 CBB key_exchange;
David Benjaminc642aca2017-07-19 23:28:43 -04002150 if (!hs->key_share ||
2151 !CBB_add_u16(&kse_bytes, group_id) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002152 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjaminc642aca2017-07-19 23:28:43 -04002153 !hs->key_share->Offer(&key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002154 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002155 return 0;
2156 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002157
David Benjamin8c880a22016-12-03 02:20:34 -05002158 if (!hs->received_hello_retry_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002159 // Save the contents of the extension to repeat it in the second
2160 // ClientHello.
David Benjamin8c880a22016-12-03 02:20:34 -05002161 hs->key_share_bytes_len = CBB_len(&kse_bytes);
David Benjamin81678aa2017-07-12 22:43:42 -04002162 hs->key_share_bytes =
2163 (uint8_t *)BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
David Benjamin8c880a22016-12-03 02:20:34 -05002164 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002165 return 0;
2166 }
2167 }
2168
Steven Valdez143e8b32016-07-11 13:19:03 -04002169 return CBB_flush(out);
2170}
2171
David Benjamin499742c2017-07-22 12:45:38 -04002172int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
2173 Array<uint8_t> *out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002174 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002175 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002176 uint16_t group_id;
2177 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002178 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2179 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002180 *out_alert = SSL_AD_DECODE_ERROR;
2181 return 0;
2182 }
2183
David Benjaminc642aca2017-07-19 23:28:43 -04002184 if (hs->key_share->GroupID() != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002185 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2186 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2187 return 0;
2188 }
2189
David Benjamin499742c2017-07-22 12:45:38 -04002190 if (!hs->key_share->Finish(
2191 out_secret, out_alert,
2192 MakeConstSpan(CBS_data(&peer_key), CBS_len(&peer_key)))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002193 *out_alert = SSL_AD_INTERNAL_ERROR;
2194 return 0;
2195 }
2196
David Benjamin45738dd2017-02-09 20:01:26 -05002197 hs->new_session->group_id = group_id;
David Benjaminc642aca2017-07-19 23:28:43 -04002198 hs->key_share.reset();
Steven Valdez143e8b32016-07-11 13:19:03 -04002199 return 1;
2200}
2201
David Benjamin74795b32017-08-31 15:13:12 -04002202int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
David Benjamin499742c2017-07-22 12:45:38 -04002203 Array<uint8_t> *out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002204 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002205 uint16_t group_id;
2206 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002207 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002208 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2209 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2210 return 0;
2211 }
2212
2213 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002214 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002215 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002216 return 0;
2217 }
2218
David Benjaminc11ea9422017-08-29 16:33:21 -04002219 // Find the corresponding key share.
David Benjamin74795b32017-08-31 15:13:12 -04002220 bool found = false;
David Benjamin7e1f9842016-09-20 19:24:40 -04002221 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002222 while (CBS_len(&key_shares) > 0) {
2223 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002224 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002225 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002226 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2227 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002228 return 0;
2229 }
2230
David Benjamin7e1f9842016-09-20 19:24:40 -04002231 if (id == group_id) {
2232 if (found) {
2233 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2234 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2235 return 0;
2236 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002237
David Benjamin74795b32017-08-31 15:13:12 -04002238 found = true;
David Benjamin7e1f9842016-09-20 19:24:40 -04002239 peer_key = peer_key_tmp;
David Benjaminc11ea9422017-08-29 16:33:21 -04002240 // Continue parsing the structure to keep peers honest.
Steven Valdez143e8b32016-07-11 13:19:03 -04002241 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002242 }
2243
David Benjamin7e1f9842016-09-20 19:24:40 -04002244 if (!found) {
David Benjamin74795b32017-08-31 15:13:12 -04002245 *out_found = false;
David Benjamin499742c2017-07-22 12:45:38 -04002246 out_secret->Reset();
David Benjamin7e1f9842016-09-20 19:24:40 -04002247 return 1;
2248 }
2249
David Benjaminc11ea9422017-08-29 16:33:21 -04002250 // Compute the DH secret.
David Benjamin499742c2017-07-22 12:45:38 -04002251 Array<uint8_t> secret;
David Benjamin1386aad2017-07-19 23:57:40 -04002252 ScopedCBB public_key;
David Benjaminc642aca2017-07-19 23:28:43 -04002253 UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
David Benjamin499742c2017-07-22 12:45:38 -04002254 if (!key_share || !CBB_init(public_key.get(), 32) ||
2255 !key_share->Accept(
2256 public_key.get(), &secret, out_alert,
2257 MakeConstSpan(CBS_data(&peer_key), CBS_len(&peer_key))) ||
David Benjamin1386aad2017-07-19 23:57:40 -04002258 !CBB_finish(public_key.get(), &hs->ecdh_public_key,
David Benjaminbf833c32017-03-30 15:45:21 -05002259 &hs->ecdh_public_key_len)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002260 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002261 return 0;
2262 }
2263
David Benjamin499742c2017-07-22 12:45:38 -04002264 *out_secret = std::move(secret);
David Benjamin74795b32017-08-31 15:13:12 -04002265 *out_found = true;
Steven Valdez5440fe02016-07-18 12:40:30 -04002266 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002267}
2268
David Benjamin8baf9632016-11-17 17:11:16 +09002269int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002270 uint16_t group_id;
2271 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002272 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002273 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2274 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2275 !CBB_add_u16(&kse_bytes, group_id) ||
2276 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002277 !CBB_add_bytes(&public_key, hs->ecdh_public_key,
2278 hs->ecdh_public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002279 !CBB_flush(out)) {
2280 return 0;
2281 }
2282
David Benjaminbf833c32017-03-30 15:45:21 -05002283 OPENSSL_free(hs->ecdh_public_key);
2284 hs->ecdh_public_key = NULL;
2285 hs->ecdh_public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002286
David Benjamin45738dd2017-02-09 20:01:26 -05002287 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002288 return 1;
2289}
2290
2291
David Benjaminc11ea9422017-08-29 16:33:21 -04002292// Supported Versions
2293//
2294// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1
Steven Valdezfdd10992016-09-15 16:27:05 -04002295
David Benjamin8c880a22016-12-03 02:20:34 -05002296static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2297 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002298 if (hs->max_version <= TLS1_2_VERSION) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002299 return 1;
2300 }
2301
2302 CBB contents, versions;
2303 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2304 !CBB_add_u16_length_prefixed(out, &contents) ||
2305 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2306 return 0;
2307 }
2308
David Benjaminc11ea9422017-08-29 16:33:21 -04002309 // Add a fake version. See draft-davidben-tls-grease-01.
David Benjamind9791bf2016-09-27 16:39:52 -04002310 if (ssl->ctx->grease_enabled &&
2311 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2312 return 0;
2313 }
2314
Steven Valdez8f36c512017-06-20 10:55:02 -04002315 if (!ssl_add_supported_versions(hs, &versions) ||
2316 !CBB_flush(out)) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002317 return 0;
2318 }
2319
2320 return 1;
2321}
2322
2323
David Benjaminc11ea9422017-08-29 16:33:21 -04002324// Cookie
2325//
2326// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2
David Benjamin3baa6e12016-10-07 21:10:38 -04002327
David Benjamin8c880a22016-12-03 02:20:34 -05002328static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2329 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002330 return 1;
2331 }
2332
2333 CBB contents, cookie;
2334 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2335 !CBB_add_u16_length_prefixed(out, &contents) ||
2336 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002337 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002338 !CBB_flush(out)) {
2339 return 0;
2340 }
2341
David Benjaminc11ea9422017-08-29 16:33:21 -04002342 // The cookie is no longer needed in memory.
David Benjamin8c880a22016-12-03 02:20:34 -05002343 OPENSSL_free(hs->cookie);
2344 hs->cookie = NULL;
2345 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002346 return 1;
2347}
2348
2349
David Benjaminc11ea9422017-08-29 16:33:21 -04002350// Negotiated Groups
2351//
2352// https://tools.ietf.org/html/rfc4492#section-5.1.2
2353// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4
Adam Langley273d49c2015-07-20 16:38:52 -07002354
David Benjamin8c880a22016-12-03 02:20:34 -05002355static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2356 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002357 CBB contents, groups_bytes;
2358 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002359 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002360 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002361 return 0;
2362 }
2363
David Benjaminc11ea9422017-08-29 16:33:21 -04002364 // Add a fake group. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002365 if (ssl->ctx->grease_enabled &&
2366 !CBB_add_u16(&groups_bytes,
2367 ssl_get_grease_value(ssl, ssl_grease_group))) {
2368 return 0;
2369 }
2370
Steven Valdezce902a92016-05-17 11:47:53 -04002371 const uint16_t *groups;
2372 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002373 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002374
David Benjamin54091232016-09-05 12:47:25 -04002375 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002376 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002377 return 0;
2378 }
2379 }
2380
2381 return CBB_flush(out);
2382}
2383
David Benjamin8c880a22016-12-03 02:20:34 -05002384static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2385 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002386 CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002387 // This extension is not expected to be echoed by servers in TLS 1.2, but some
2388 // BigIP servers send it nonetheless, so do not enforce this.
Adam Langley273d49c2015-07-20 16:38:52 -07002389 return 1;
2390}
2391
David Benjamin8c880a22016-12-03 02:20:34 -05002392static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2393 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002394 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002395 if (contents == NULL) {
2396 return 1;
2397 }
2398
Steven Valdezce902a92016-05-17 11:47:53 -04002399 CBS supported_group_list;
2400 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2401 CBS_len(&supported_group_list) == 0 ||
2402 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002403 CBS_len(contents) != 0) {
2404 return 0;
2405 }
2406
David Benjamin8c880a22016-12-03 02:20:34 -05002407 hs->peer_supported_group_list =
David Benjamin81678aa2017-07-12 22:43:42 -04002408 (uint16_t *)OPENSSL_malloc(CBS_len(&supported_group_list));
David Benjamin8c880a22016-12-03 02:20:34 -05002409 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002410 *out_alert = SSL_AD_INTERNAL_ERROR;
2411 return 0;
2412 }
2413
Steven Valdezce902a92016-05-17 11:47:53 -04002414 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002415 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002416 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002417 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002418 goto err;
2419 }
2420 }
2421
Steven Valdezce902a92016-05-17 11:47:53 -04002422 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002423 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002424
2425 return 1;
2426
2427err:
David Benjamin8c880a22016-12-03 02:20:34 -05002428 OPENSSL_free(hs->peer_supported_group_list);
2429 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002430 *out_alert = SSL_AD_INTERNAL_ERROR;
2431 return 0;
2432}
2433
David Benjamin8c880a22016-12-03 02:20:34 -05002434static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002435 // Servers don't echo this extension.
Adam Langley273d49c2015-07-20 16:38:52 -07002436 return 1;
2437}
2438
2439
David Benjaminc11ea9422017-08-29 16:33:21 -04002440// kExtensions contains all the supported extensions.
Adam Langley614c66a2015-06-12 15:26:58 -07002441static const struct tls_extension kExtensions[] = {
2442 {
Adam Langley5021b222015-06-12 18:27:58 -07002443 TLSEXT_TYPE_renegotiate,
2444 NULL,
2445 ext_ri_add_clienthello,
2446 ext_ri_parse_serverhello,
2447 ext_ri_parse_clienthello,
2448 ext_ri_add_serverhello,
2449 },
2450 {
Adam Langley614c66a2015-06-12 15:26:58 -07002451 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002452 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002453 ext_sni_add_clienthello,
2454 ext_sni_parse_serverhello,
2455 ext_sni_parse_clienthello,
2456 ext_sni_add_serverhello,
2457 },
Adam Langley0a056712015-07-01 15:03:33 -07002458 {
2459 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002460 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002461 ext_ems_add_clienthello,
2462 ext_ems_parse_serverhello,
2463 ext_ems_parse_clienthello,
2464 ext_ems_add_serverhello,
2465 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002466 {
2467 TLSEXT_TYPE_session_ticket,
2468 NULL,
2469 ext_ticket_add_clienthello,
2470 ext_ticket_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04002471 // Ticket extension client parsing is handled in ssl_session.c
Steven Valdez6b8509a2016-07-12 13:38:32 -04002472 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002473 ext_ticket_add_serverhello,
2474 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002475 {
2476 TLSEXT_TYPE_signature_algorithms,
2477 NULL,
2478 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002479 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002480 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002481 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002482 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002483 {
2484 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002485 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002486 ext_ocsp_add_clienthello,
2487 ext_ocsp_parse_serverhello,
2488 ext_ocsp_parse_clienthello,
2489 ext_ocsp_add_serverhello,
2490 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002491 {
2492 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002493 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002494 ext_npn_add_clienthello,
2495 ext_npn_parse_serverhello,
2496 ext_npn_parse_clienthello,
2497 ext_npn_add_serverhello,
2498 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002499 {
2500 TLSEXT_TYPE_certificate_timestamp,
2501 NULL,
2502 ext_sct_add_clienthello,
2503 ext_sct_parse_serverhello,
2504 ext_sct_parse_clienthello,
2505 ext_sct_add_serverhello,
2506 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002507 {
2508 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002509 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002510 ext_alpn_add_clienthello,
2511 ext_alpn_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04002512 // ALPN is negotiated late in |ssl_negotiate_alpn|.
David Benjamin9ef31f02016-10-31 18:01:13 -04002513 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002514 ext_alpn_add_serverhello,
2515 },
Adam Langley49c7af12015-07-10 14:33:46 -07002516 {
2517 TLSEXT_TYPE_channel_id,
2518 ext_channel_id_init,
2519 ext_channel_id_add_clienthello,
2520 ext_channel_id_parse_serverhello,
2521 ext_channel_id_parse_clienthello,
2522 ext_channel_id_add_serverhello,
2523 },
Adam Langley391250d2015-07-15 19:06:07 -07002524 {
2525 TLSEXT_TYPE_srtp,
2526 ext_srtp_init,
2527 ext_srtp_add_clienthello,
2528 ext_srtp_parse_serverhello,
2529 ext_srtp_parse_clienthello,
2530 ext_srtp_add_serverhello,
2531 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002532 {
2533 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002534 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002535 ext_ec_point_add_clienthello,
2536 ext_ec_point_parse_serverhello,
2537 ext_ec_point_parse_clienthello,
2538 ext_ec_point_add_serverhello,
2539 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002540 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002541 TLSEXT_TYPE_key_share,
2542 NULL,
2543 ext_key_share_add_clienthello,
2544 forbid_parse_serverhello,
2545 ignore_parse_clienthello,
2546 dont_add_serverhello,
2547 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002548 {
Steven Valdeza833c352016-11-01 13:39:36 -04002549 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002550 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002551 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002552 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002553 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002554 dont_add_serverhello,
2555 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002556 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002557 TLSEXT_TYPE_early_data,
2558 NULL,
2559 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002560 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002561 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002562 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002563 },
2564 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002565 TLSEXT_TYPE_supported_versions,
2566 NULL,
2567 ext_supported_versions_add_clienthello,
2568 forbid_parse_serverhello,
2569 ignore_parse_clienthello,
2570 dont_add_serverhello,
2571 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002572 {
2573 TLSEXT_TYPE_cookie,
2574 NULL,
2575 ext_cookie_add_clienthello,
2576 forbid_parse_serverhello,
2577 ignore_parse_clienthello,
2578 dont_add_serverhello,
2579 },
David Benjaminc11ea9422017-08-29 16:33:21 -04002580 // The final extension must be non-empty. WebSphere Application Server 7.0 is
2581 // intolerant to the last extension being zero-length. See
2582 // https://crbug.com/363583.
Adam Langley273d49c2015-07-20 16:38:52 -07002583 {
Steven Valdezce902a92016-05-17 11:47:53 -04002584 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002585 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002586 ext_supported_groups_add_clienthello,
2587 ext_supported_groups_parse_serverhello,
2588 ext_supported_groups_parse_clienthello,
2589 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002590 },
Adam Langley614c66a2015-06-12 15:26:58 -07002591};
2592
2593#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2594
David Benjamina3d76d02017-07-14 19:36:07 -04002595static_assert(kNumExtensions <=
2596 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
2597 "too many extensions for sent bitset");
2598static_assert(kNumExtensions <=
2599 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2600 "too many extensions for received bitset");
Adam Langley4cfa96b2015-07-01 11:56:55 -07002601
Adam Langley614c66a2015-06-12 15:26:58 -07002602static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2603 uint16_t value) {
2604 unsigned i;
2605 for (i = 0; i < kNumExtensions; i++) {
2606 if (kExtensions[i].value == value) {
2607 *out_index = i;
2608 return &kExtensions[i];
2609 }
2610 }
2611
2612 return NULL;
2613}
2614
David Benjamin8c880a22016-12-03 02:20:34 -05002615int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2616 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04002617 // Don't add extensions for SSLv3 unless doing secure renegotiation.
David Benjaminf04c2e92016-12-06 13:35:25 -05002618 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002619 !ssl->s3->send_connection_binding) {
2620 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002621 }
Adam Langley95c29f32014-06-20 12:00:00 -07002622
David Benjamine8d53502015-10-10 14:13:23 -04002623 CBB extensions;
2624 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002625 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2626 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002627 }
Adam Langley95c29f32014-06-20 12:00:00 -07002628
David Benjamin8c880a22016-12-03 02:20:34 -05002629 hs->extensions.sent = 0;
2630 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002631
David Benjamin54091232016-09-05 12:47:25 -04002632 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002633 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002634 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002635 }
2636 }
Adam Langley95c29f32014-06-20 12:00:00 -07002637
David Benjamin65ac9972016-09-02 21:35:25 -04002638 uint16_t grease_ext1 = 0;
2639 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002640 // Add a fake empty extension. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002641 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2642 if (!CBB_add_u16(&extensions, grease_ext1) ||
2643 !CBB_add_u16(&extensions, 0 /* zero length */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002644 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2645 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002646 }
2647 }
2648
David Benjamin54091232016-09-05 12:47:25 -04002649 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002650 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002651 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002652 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002653 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin81678aa2017-07-12 22:43:42 -04002654 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002655 }
Adam Langley95c29f32014-06-20 12:00:00 -07002656
Adam Langley33ad2b52015-07-20 17:43:53 -07002657 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002658 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002659 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002660 }
Adam Langley75712922014-10-10 16:23:43 -07002661
David Benjamin2bd19172016-11-17 16:47:15 +09002662 if (!custom_ext_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002663 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2664 return 0;
Adam Langley09505632015-07-30 18:10:13 -07002665 }
2666
David Benjamin65ac9972016-09-02 21:35:25 -04002667 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002668 // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002669 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2670
David Benjaminc11ea9422017-08-29 16:33:21 -04002671 // The two fake extensions must not have the same value. GREASE values are
2672 // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2673 // one.
David Benjamin65ac9972016-09-02 21:35:25 -04002674 if (grease_ext1 == grease_ext2) {
2675 grease_ext2 ^= 0x1010;
2676 }
2677
2678 if (!CBB_add_u16(&extensions, grease_ext2) ||
2679 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2680 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002681 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2682 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002683 }
2684 }
2685
David Benjamince079fd2016-08-02 16:22:34 -04002686 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002687 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002688 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002689 if (header_len > 0xff && header_len < 0x200) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002690 // Add padding to workaround bugs in F5 terminators. See RFC 7685.
2691 //
2692 // NB: because this code works out the length of all existing extensions
2693 // it MUST always appear last.
David Benjamin0a968592015-07-21 22:06:19 -04002694 size_t padding_len = 0x200 - header_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04002695 // Extensions take at least four bytes to encode. Always include at least
2696 // one byte of data if including the extension. WebSphere Application
2697 // Server 7.0 is intolerant to the last extension being zero-length. See
2698 // https://crbug.com/363583.
Adam Langleyfcf25832014-12-18 17:42:32 -08002699 if (padding_len >= 4 + 1) {
2700 padding_len -= 4;
2701 } else {
2702 padding_len = 1;
2703 }
Adam Langley95c29f32014-06-20 12:00:00 -07002704
Adam Langley33ad2b52015-07-20 17:43:53 -07002705 uint8_t *padding_bytes;
2706 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2707 !CBB_add_u16(&extensions, padding_len) ||
2708 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002709 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2710 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002711 }
Adam Langley75712922014-10-10 16:23:43 -07002712
David Benjamin17cf2cb2016-12-13 01:07:13 -05002713 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002714 }
2715 }
Adam Langley75712922014-10-10 16:23:43 -07002716
David Benjaminc11ea9422017-08-29 16:33:21 -04002717 // The PSK extension must be last, including after the padding.
David Benjamin8c880a22016-12-03 02:20:34 -05002718 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002719 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2720 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -04002721 }
2722
David Benjaminc11ea9422017-08-29 16:33:21 -04002723 // Discard empty extensions blocks.
David Benjamina01deee2015-12-08 18:56:31 -05002724 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002725 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002726 }
2727
David Benjamine8d53502015-10-10 14:13:23 -04002728 return CBB_flush(out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002729}
Adam Langley95c29f32014-06-20 12:00:00 -07002730
David Benjamin8c880a22016-12-03 02:20:34 -05002731int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2732 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002733 CBB extensions;
2734 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002735 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002736 }
2737
David Benjamin8c880a22016-12-03 02:20:34 -05002738 for (unsigned i = 0; i < kNumExtensions; i++) {
2739 if (!(hs->extensions.received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002740 // Don't send extensions that were not received.
Adam Langley614c66a2015-06-12 15:26:58 -07002741 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002742 }
Adam Langley95c29f32014-06-20 12:00:00 -07002743
David Benjamin8c880a22016-12-03 02:20:34 -05002744 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002745 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002746 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002747 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002748 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002749 }
Adam Langley95c29f32014-06-20 12:00:00 -07002750
David Benjamin2bd19172016-11-17 16:47:15 +09002751 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002752 goto err;
2753 }
2754
David Benjaminc11ea9422017-08-29 16:33:21 -04002755 // Discard empty extensions blocks before TLS 1.3.
Steven Valdez143e8b32016-07-11 13:19:03 -04002756 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2757 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002758 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002759 }
2760
David Benjamin56380462015-10-10 14:59:09 -04002761 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002762
2763err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002764 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002765 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002766}
Adam Langley95c29f32014-06-20 12:00:00 -07002767
David Benjamin731058e2016-12-03 23:15:13 -05002768static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2769 const SSL_CLIENT_HELLO *client_hello,
2770 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002771 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002772 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002773 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002774 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002775 }
2776 }
2777
David Benjamin8c880a22016-12-03 02:20:34 -05002778 hs->extensions.received = 0;
2779 hs->custom_extensions.received = 0;
David Benjamine14ff062016-08-09 16:21:24 -04002780 CBS extensions;
2781 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2782 while (CBS_len(&extensions) != 0) {
2783 uint16_t type;
2784 CBS extension;
2785
David Benjaminc11ea9422017-08-29 16:33:21 -04002786 // Decode the next extension.
David Benjamine14ff062016-08-09 16:21:24 -04002787 if (!CBS_get_u16(&extensions, &type) ||
2788 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002789 *out_alert = SSL_AD_DECODE_ERROR;
2790 return 0;
2791 }
Adam Langley95c29f32014-06-20 12:00:00 -07002792
David Benjaminc11ea9422017-08-29 16:33:21 -04002793 // RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2794 // ambiguous. Ignore all but the renegotiation_info extension.
David Benjamine14ff062016-08-09 16:21:24 -04002795 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2796 continue;
2797 }
Adam Langley95c29f32014-06-20 12:00:00 -07002798
David Benjamine14ff062016-08-09 16:21:24 -04002799 unsigned ext_index;
2800 const struct tls_extension *const ext =
2801 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002802
David Benjamine14ff062016-08-09 16:21:24 -04002803 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002804 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002805 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002806 return 0;
2807 }
David Benjamine14ff062016-08-09 16:21:24 -04002808 continue;
2809 }
2810
David Benjamin8c880a22016-12-03 02:20:34 -05002811 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002812 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002813 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002814 *out_alert = alert;
2815 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002816 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002817 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002818 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002819 }
Adam Langley75712922014-10-10 16:23:43 -07002820
David Benjamin1deb41b2016-08-09 19:36:38 -04002821 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002822 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002823 continue;
2824 }
2825
2826 CBS *contents = NULL, fake_contents;
2827 static const uint8_t kFakeRenegotiateExtension[] = {0};
2828 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2829 ssl_client_cipher_list_contains_cipher(client_hello,
2830 SSL3_CK_SCSV & 0xffff)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002831 // The renegotiation SCSV was received so pretend that we received a
2832 // renegotiation extension.
David Benjamin1deb41b2016-08-09 19:36:38 -04002833 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2834 sizeof(kFakeRenegotiateExtension));
2835 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002836 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002837 }
2838
David Benjaminc11ea9422017-08-29 16:33:21 -04002839 // Extension wasn't observed so call the callback with a NULL
2840 // parameter.
David Benjamin1deb41b2016-08-09 19:36:38 -04002841 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002842 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002843 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002844 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002845 *out_alert = alert;
2846 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002847 }
2848 }
2849
Adam Langleyfcf25832014-12-18 17:42:32 -08002850 return 1;
2851}
Adam Langley95c29f32014-06-20 12:00:00 -07002852
David Benjamin731058e2016-12-03 23:15:13 -05002853int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2854 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002855 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002856 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002857 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002858 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002859 return 0;
2860 }
Adam Langley95c29f32014-06-20 12:00:00 -07002861
David Benjamin8c880a22016-12-03 02:20:34 -05002862 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002863 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002864 return 0;
2865 }
Adam Langley95c29f32014-06-20 12:00:00 -07002866
Adam Langleyfcf25832014-12-18 17:42:32 -08002867 return 1;
2868}
Adam Langley95c29f32014-06-20 12:00:00 -07002869
David Benjamin8c880a22016-12-03 02:20:34 -05002870static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2871 int *out_alert) {
2872 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04002873 // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty.
Steven Valdez143e8b32016-07-11 13:19:03 -04002874 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2875 return 1;
2876 }
Adam Langley614c66a2015-06-12 15:26:58 -07002877
David Benjaminc11ea9422017-08-29 16:33:21 -04002878 // Decode the extensions block and check it is valid.
Steven Valdez143e8b32016-07-11 13:19:03 -04002879 CBS extensions;
2880 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2881 !tls1_check_duplicate_extensions(&extensions)) {
2882 *out_alert = SSL_AD_DECODE_ERROR;
2883 return 0;
2884 }
2885
2886 uint32_t received = 0;
2887 while (CBS_len(&extensions) != 0) {
2888 uint16_t type;
2889 CBS extension;
2890
David Benjaminc11ea9422017-08-29 16:33:21 -04002891 // Decode the next extension.
Steven Valdez143e8b32016-07-11 13:19:03 -04002892 if (!CBS_get_u16(&extensions, &type) ||
2893 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002894 *out_alert = SSL_AD_DECODE_ERROR;
2895 return 0;
2896 }
Adam Langley95c29f32014-06-20 12:00:00 -07002897
Steven Valdez143e8b32016-07-11 13:19:03 -04002898 unsigned ext_index;
2899 const struct tls_extension *const ext =
2900 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002901
Steven Valdez143e8b32016-07-11 13:19:03 -04002902 if (ext == NULL) {
David Benjaminfd45ee72017-08-31 14:49:09 -04002903 hs->received_custom_extension = true;
David Benjamin2bd19172016-11-17 16:47:15 +09002904 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002905 return 0;
2906 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002907 continue;
2908 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002909
David Benjamina3d76d02017-07-14 19:36:07 -04002910 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
2911 "too many bits");
David Benjamin5db7c9b2017-01-24 16:17:03 -05002912
David Benjamin8c880a22016-12-03 02:20:34 -05002913 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002914 type != TLSEXT_TYPE_renegotiate) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002915 // If the extension was never sent then it is illegal, except for the
2916 // renegotiation extension which, in SSL 3.0, is signaled via SCSV.
Steven Valdez143e8b32016-07-11 13:19:03 -04002917 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2918 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002919 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002920 return 0;
2921 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002922
Steven Valdez143e8b32016-07-11 13:19:03 -04002923 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002924
Steven Valdez143e8b32016-07-11 13:19:03 -04002925 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002926 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002927 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002928 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04002929 *out_alert = alert;
2930 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002931 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002932 }
Adam Langley95c29f32014-06-20 12:00:00 -07002933
David Benjamin54091232016-09-05 12:47:25 -04002934 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002935 if (!(received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002936 // Extension wasn't observed so call the callback with a NULL
2937 // parameter.
Adam Langley614c66a2015-06-12 15:26:58 -07002938 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002939 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002940 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002941 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002942 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002943 return 0;
2944 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002945 }
2946 }
Adam Langley95c29f32014-06-20 12:00:00 -07002947
Adam Langleyfcf25832014-12-18 17:42:32 -08002948 return 1;
2949}
Adam Langley95c29f32014-06-20 12:00:00 -07002950
David Benjamin8c880a22016-12-03 02:20:34 -05002951static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
2952 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002953 int ret = SSL_TLSEXT_ERR_NOACK;
2954 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002955
David Benjamin78f8aab2016-03-10 16:33:58 -05002956 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002957 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002958 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05002959 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
2960 ret = ssl->session_ctx->tlsext_servername_callback(
2961 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002962 }
Adam Langley95c29f32014-06-20 12:00:00 -07002963
Adam Langleyfcf25832014-12-18 17:42:32 -08002964 switch (ret) {
2965 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002966 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002967 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002968
Adam Langleyfcf25832014-12-18 17:42:32 -08002969 case SSL_TLSEXT_ERR_NOACK:
David Benjaminfd45ee72017-08-31 14:49:09 -04002970 hs->should_ack_sni = false;
Adam Langleyfcf25832014-12-18 17:42:32 -08002971 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002972
Adam Langleyfcf25832014-12-18 17:42:32 -08002973 default:
2974 return 1;
2975 }
2976}
Adam Langleyed8270a2014-09-02 13:52:56 -07002977
David Benjamin8c880a22016-12-03 02:20:34 -05002978int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
2979 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002980 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002981 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002982 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002983 return 0;
2984 }
2985
Adam Langleyfcf25832014-12-18 17:42:32 -08002986 return 1;
2987}
Adam Langley95c29f32014-06-20 12:00:00 -07002988
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002989static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
2990 uint8_t **out, size_t *out_len, EVP_CIPHER_CTX *cipher_ctx,
2991 HMAC_CTX *hmac_ctx, const uint8_t *ticket, size_t ticket_len) {
2992 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002993
David Benjaminc11ea9422017-08-29 16:33:21 -04002994 // Check the MAC at the end of the ticket.
David Benjamine3aa1d92015-06-16 15:34:50 -04002995 uint8_t mac[EVP_MAX_MD_SIZE];
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002996 size_t mac_len = HMAC_size(hmac_ctx);
David Benjamine3aa1d92015-06-16 15:34:50 -04002997 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002998 // The ticket must be large enough for key name, IV, data, and MAC.
David Benjamin81678aa2017-07-12 22:43:42 -04002999 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003000 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003001 HMAC_Update(hmac_ctx, ticket, ticket_len - mac_len);
3002 HMAC_Final(hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003003 int mac_ok =
3004 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3005#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3006 mac_ok = 1;
3007#endif
3008 if (!mac_ok) {
David Benjamin81678aa2017-07-12 22:43:42 -04003009 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003010 }
3011
David Benjaminc11ea9422017-08-29 16:33:21 -04003012 // Decrypt the session data.
David Benjamine3aa1d92015-06-16 15:34:50 -04003013 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3014 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3015 mac_len;
David Benjamin86e95b82017-07-18 16:34:25 -04003016 UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len));
David Benjamin81678aa2017-07-12 22:43:42 -04003017 if (!plaintext) {
3018 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08003019 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003020 size_t plaintext_len;
3021#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin81678aa2017-07-12 22:43:42 -04003022 OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003023 plaintext_len = ciphertext_len;
3024#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003025 if (ciphertext_len >= INT_MAX) {
David Benjamin81678aa2017-07-12 22:43:42 -04003026 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003027 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003028 int len1, len2;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003029 if (!EVP_DecryptUpdate(cipher_ctx, plaintext.get(), &len1, ciphertext,
David Benjamine3aa1d92015-06-16 15:34:50 -04003030 (int)ciphertext_len) ||
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003031 !EVP_DecryptFinal_ex(cipher_ctx, plaintext.get() + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003032 ERR_clear_error();
David Benjamin81678aa2017-07-12 22:43:42 -04003033 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003034 }
Adam Langley4c341d02017-03-08 19:33:21 -08003035 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04003036#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003037
David Benjamin81678aa2017-07-12 22:43:42 -04003038 *out = plaintext.release();
Adam Langley4c341d02017-03-08 19:33:21 -08003039 *out_len = plaintext_len;
David Benjamin81678aa2017-07-12 22:43:42 -04003040 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08003041}
3042
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003043static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
David Benjaminfd45ee72017-08-31 14:49:09 -04003044 SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003045 const uint8_t *ticket, size_t ticket_len) {
3046 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3047 ScopedEVP_CIPHER_CTX cipher_ctx;
3048 ScopedHMAC_CTX hmac_ctx;
3049 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3050 int cb_ret = ssl->session_ctx->tlsext_ticket_key_cb(
3051 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(),
3052 hmac_ctx.get(), 0 /* decrypt */);
3053 if (cb_ret < 0) {
3054 return ssl_ticket_aead_error;
3055 } else if (cb_ret == 0) {
3056 return ssl_ticket_aead_ignore_ticket;
3057 } else if (cb_ret == 2) {
David Benjaminfd45ee72017-08-31 14:49:09 -04003058 *out_renew_ticket = true;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003059 } else {
3060 assert(cb_ret == 1);
3061 }
3062 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3063 hmac_ctx.get(), ticket, ticket_len);
3064}
3065
3066static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
3067 SSL *ssl, uint8_t **out, size_t *out_len, const uint8_t *ticket,
3068 size_t ticket_len) {
3069 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3070 SSL_CTX *ctx = ssl->session_ctx;
3071
David Benjaminc11ea9422017-08-29 16:33:21 -04003072 // Rotate the ticket key if necessary.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003073 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
3074 return ssl_ticket_aead_error;
3075 }
3076
David Benjaminc11ea9422017-08-29 16:33:21 -04003077 // Pick the matching ticket key and decrypt.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003078 ScopedEVP_CIPHER_CTX cipher_ctx;
3079 ScopedHMAC_CTX hmac_ctx;
3080 {
3081 MutexReadLock lock(&ctx->lock);
3082 const tlsext_ticket_key *key;
3083 if (ctx->tlsext_ticket_key_current &&
3084 !OPENSSL_memcmp(ctx->tlsext_ticket_key_current->name, ticket,
3085 SSL_TICKET_KEY_NAME_LEN)) {
3086 key = ctx->tlsext_ticket_key_current;
3087 } else if (ctx->tlsext_ticket_key_prev &&
3088 !OPENSSL_memcmp(ctx->tlsext_ticket_key_prev->name, ticket,
3089 SSL_TICKET_KEY_NAME_LEN)) {
3090 key = ctx->tlsext_ticket_key_prev;
3091 } else {
3092 return ssl_ticket_aead_ignore_ticket;
3093 }
3094 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3095 if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
3096 tlsext_tick_md(), NULL) ||
3097 !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL,
3098 key->aes_key, iv)) {
3099 return ssl_ticket_aead_error;
3100 }
3101 }
3102 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3103 hmac_ctx.get(), ticket, ticket_len);
3104}
3105
Adam Langley4c341d02017-03-08 19:33:21 -08003106static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
David Benjaminfd45ee72017-08-31 14:49:09 -04003107 SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
Adam Langley4c341d02017-03-08 19:33:21 -08003108 const uint8_t *ticket, size_t ticket_len) {
David Benjamin81678aa2017-07-12 22:43:42 -04003109 uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len);
Adam Langley4c341d02017-03-08 19:33:21 -08003110 if (plaintext == NULL) {
3111 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3112 return ssl_ticket_aead_error;
3113 }
3114
3115 size_t plaintext_len;
3116 const enum ssl_ticket_aead_result_t result =
3117 ssl->session_ctx->ticket_aead_method->open(
3118 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3119
3120 if (result == ssl_ticket_aead_success) {
3121 *out = plaintext;
3122 plaintext = NULL;
3123 *out_len = plaintext_len;
3124 }
3125
3126 OPENSSL_free(plaintext);
3127 return result;
3128}
3129
3130enum ssl_ticket_aead_result_t ssl_process_ticket(
David Benjaminfd45ee72017-08-31 14:49:09 -04003131 SSL *ssl, UniquePtr<SSL_SESSION> *out_session, bool *out_renew_ticket,
Adam Langley4c341d02017-03-08 19:33:21 -08003132 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3133 size_t session_id_len) {
David Benjaminfd45ee72017-08-31 14:49:09 -04003134 *out_renew_ticket = false;
David Benjamin37af90f2017-07-29 01:42:16 -04003135 out_session->reset();
Adam Langley4c341d02017-03-08 19:33:21 -08003136
3137 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3138 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3139 return ssl_ticket_aead_ignore_ticket;
3140 }
3141
3142 uint8_t *plaintext = NULL;
3143 size_t plaintext_len;
3144 enum ssl_ticket_aead_result_t result;
3145 if (ssl->session_ctx->ticket_aead_method != NULL) {
3146 result = ssl_decrypt_ticket_with_method(
3147 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3148 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04003149 // Ensure there is room for the key name and the largest IV
3150 // |tlsext_ticket_key_cb| may try to consume. The real limit may be lower,
3151 // but the maximum IV length should be well under the minimum size for the
3152 // session material and HMAC.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003153 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3154 return ssl_ticket_aead_ignore_ticket;
3155 }
3156 if (ssl->session_ctx->tlsext_ticket_key_cb != NULL) {
3157 result = ssl_decrypt_ticket_with_cb(ssl, &plaintext, &plaintext_len,
3158 out_renew_ticket, ticket, ticket_len);
3159 } else {
3160 result = ssl_decrypt_ticket_with_ticket_keys(
3161 ssl, &plaintext, &plaintext_len, ticket, ticket_len);
3162 }
Adam Langley4c341d02017-03-08 19:33:21 -08003163 }
3164
3165 if (result != ssl_ticket_aead_success) {
3166 return result;
3167 }
3168
David Benjaminc11ea9422017-08-29 16:33:21 -04003169 // Decode the session.
David Benjamin37af90f2017-07-29 01:42:16 -04003170 UniquePtr<SSL_SESSION> session(
3171 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx));
Adam Langley4c341d02017-03-08 19:33:21 -08003172 OPENSSL_free(plaintext);
3173
David Benjamin37af90f2017-07-29 01:42:16 -04003174 if (!session) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003175 ERR_clear_error(); // Don't leave an error on the queue.
Adam Langley4c341d02017-03-08 19:33:21 -08003176 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003177 }
3178
David Benjaminc11ea9422017-08-29 16:33:21 -04003179 // Copy the client's session ID into the new session, to denote the ticket has
3180 // been accepted.
David Benjamin17cf2cb2016-12-13 01:07:13 -05003181 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003182 session->session_id_length = session_id_len;
3183
David Benjamin37af90f2017-07-29 01:42:16 -04003184 *out_session = std::move(session);
Adam Langley4c341d02017-03-08 19:33:21 -08003185 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003186}
Adam Langley95c29f32014-06-20 12:00:00 -07003187
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003188int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003189 // Extension ignored for inappropriate versions
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003190 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003191 return 1;
3192 }
David Benjamincd996942014-07-20 16:23:51 -04003193
David Benjamin0fc37ef2016-08-17 15:29:46 -04003194 OPENSSL_free(hs->peer_sigalgs);
3195 hs->peer_sigalgs = NULL;
3196 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003197
3198 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003199 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003200 return 0;
3201 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003202 num_sigalgs /= 2;
3203
David Benjaminc11ea9422017-08-29 16:33:21 -04003204 // supported_signature_algorithms in the certificate request is
3205 // allowed to be empty.
Steven Valdez0d62f262015-09-04 12:41:04 -04003206 if (num_sigalgs == 0) {
3207 return 1;
3208 }
3209
David Benjaminc11ea9422017-08-29 16:33:21 -04003210 // This multiplication doesn't overflow because sizeof(uint16_t) is two
3211 // and we just divided |num_sigalgs| by two.
David Benjamin81678aa2017-07-12 22:43:42 -04003212 hs->peer_sigalgs = (uint16_t *)OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003213 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003214 return 0;
3215 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003216 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003217
3218 CBS sigalgs;
3219 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003220 for (size_t i = 0; i < num_sigalgs; i++) {
3221 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003222 return 0;
3223 }
3224 }
Adam Langley95c29f32014-06-20 12:00:00 -07003225
Adam Langleyfcf25832014-12-18 17:42:32 -08003226 return 1;
3227}
David Benjaminec2f27d2014-11-13 19:17:25 -05003228
David Benjamina3651382017-04-20 17:49:36 -04003229int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
3230 switch (EVP_PKEY_id(pkey)) {
3231 case EVP_PKEY_RSA:
3232 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3233 return 1;
3234 case EVP_PKEY_EC:
3235 *out = SSL_SIGN_ECDSA_SHA1;
3236 return 1;
3237 default:
3238 return 0;
3239 }
3240}
3241
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003242int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3243 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003244 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003245
David Benjaminc11ea9422017-08-29 16:33:21 -04003246 // Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3247 // handshake.
Steven Valdezf0451ca2016-06-29 13:16:27 -04003248 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin31b0c9b2017-07-20 14:49:15 -04003249 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
David Benjamina3651382017-04-20 17:49:36 -04003250 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3251 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003252 }
David Benjamina3651382017-04-20 17:49:36 -04003253 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003254 }
3255
David Benjamin3ef76972016-10-17 17:59:54 -04003256 const uint16_t *sigalgs = cert->sigalgs;
3257 size_t num_sigalgs = cert->num_sigalgs;
3258 if (sigalgs == NULL) {
3259 sigalgs = kSignSignatureAlgorithms;
3260 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003261 }
3262
David Benjamin0fc37ef2016-08-17 15:29:46 -04003263 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3264 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3265 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003266 // If the client didn't specify any signature_algorithms extension then
3267 // we can assume that it supports SHA1. See
3268 // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
David Benjaminea9a0d52016-07-08 15:52:59 -07003269 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3270 SSL_SIGN_ECDSA_SHA1};
3271 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003272 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003273 }
3274
David Benjamin0fc37ef2016-08-17 15:29:46 -04003275 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003276 uint16_t sigalg = sigalgs[i];
David Benjaminc11ea9422017-08-29 16:33:21 -04003277 // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3278 // negotiated.
David Benjamin1fb125c2016-07-08 18:52:12 -07003279 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjamina232a712017-03-30 15:51:53 -05003280 !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003281 continue;
3282 }
3283
David Benjamin0fc37ef2016-08-17 15:29:46 -04003284 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003285 if (sigalg == peer_sigalgs[j]) {
3286 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003287 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003288 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003289 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003290 }
Adam Langley95c29f32014-06-20 12:00:00 -07003291
David Benjaminea9a0d52016-07-08 15:52:59 -07003292 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3293 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003294}
Adam Langley95c29f32014-06-20 12:00:00 -07003295
David Benjamin7934f082017-08-01 16:32:25 -04003296int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
Steven Valdez908ac192017-01-12 13:17:07 -05003297 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003298 // A Channel ID handshake message is structured to contain multiple
3299 // extensions, but the only one that can be present is Channel ID.
David Benjamin7934f082017-08-01 16:32:25 -04003300 uint16_t extension_type;
3301 CBS channel_id = msg.body, extension;
Nick Harper60a85cb2016-09-23 16:25:11 -07003302 if (!CBS_get_u16(&channel_id, &extension_type) ||
3303 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3304 CBS_len(&channel_id) != 0 ||
3305 extension_type != TLSEXT_TYPE_channel_id ||
3306 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3307 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3308 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3309 return 0;
3310 }
3311
David Benjamin86e95b82017-07-18 16:34:25 -04003312 UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Nick Harper60a85cb2016-09-23 16:25:11 -07003313 if (!p256) {
3314 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3315 return 0;
3316 }
3317
David Benjamin86e95b82017-07-18 16:34:25 -04003318 UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3319 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
David Benjamin81678aa2017-07-12 22:43:42 -04003320 if (!sig || !x || !y) {
3321 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003322 }
3323
3324 const uint8_t *p = CBS_data(&extension);
David Benjamin81678aa2017-07-12 22:43:42 -04003325 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3326 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3327 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3328 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
3329 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003330 }
3331
David Benjamin86e95b82017-07-18 16:34:25 -04003332 UniquePtr<EC_KEY> key(EC_KEY_new());
3333 UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
David Benjamin81678aa2017-07-12 22:43:42 -04003334 if (!key || !point ||
3335 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3336 y.get(), nullptr) ||
3337 !EC_KEY_set_group(key.get(), p256.get()) ||
3338 !EC_KEY_set_public_key(key.get(), point.get())) {
3339 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003340 }
3341
3342 uint8_t digest[EVP_MAX_MD_SIZE];
3343 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003344 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003345 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003346 }
3347
David Benjamin81678aa2017-07-12 22:43:42 -04003348 int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
Nick Harper60a85cb2016-09-23 16:25:11 -07003349#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3350 sig_ok = 1;
3351#endif
3352 if (!sig_ok) {
3353 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3354 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
David Benjamin046bc1f2017-08-31 15:06:42 -04003355 ssl->s3->tlsext_channel_id_valid = false;
David Benjamin81678aa2017-07-12 22:43:42 -04003356 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003357 }
3358
David Benjamin17cf2cb2016-12-13 01:07:13 -05003359 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
David Benjamin81678aa2017-07-12 22:43:42 -04003360 return 1;
Nick Harper60a85cb2016-09-23 16:25:11 -07003361}
3362
Steven Valdez908ac192017-01-12 13:17:07 -05003363int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3364 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003365 uint8_t digest[EVP_MAX_MD_SIZE];
3366 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003367 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003368 return 0;
3369 }
3370
3371 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3372 if (ec_key == NULL) {
3373 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3374 return 0;
3375 }
3376
3377 int ret = 0;
3378 BIGNUM *x = BN_new();
3379 BIGNUM *y = BN_new();
3380 ECDSA_SIG *sig = NULL;
3381 if (x == NULL || y == NULL ||
3382 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3383 EC_KEY_get0_public_key(ec_key),
3384 x, y, NULL)) {
3385 goto err;
3386 }
3387
3388 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3389 if (sig == NULL) {
3390 goto err;
3391 }
3392
3393 CBB child;
3394 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3395 !CBB_add_u16_length_prefixed(cbb, &child) ||
3396 !BN_bn2cbb_padded(&child, 32, x) ||
3397 !BN_bn2cbb_padded(&child, 32, y) ||
3398 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3399 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3400 !CBB_flush(cbb)) {
3401 goto err;
3402 }
3403
3404 ret = 1;
3405
3406err:
3407 BN_free(x);
3408 BN_free(y);
3409 ECDSA_SIG_free(sig);
3410 return ret;
3411}
3412
Steven Valdez908ac192017-01-12 13:17:07 -05003413int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3414 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003415 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3416 uint8_t *msg;
3417 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003418 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003419 ssl_cert_verify_channel_id)) {
3420 return 0;
3421 }
3422 SHA256(msg, msg_len, out);
3423 *out_len = SHA256_DIGEST_LENGTH;
3424 OPENSSL_free(msg);
3425 return 1;
3426 }
3427
Nick Harper95594012016-10-20 14:07:13 -07003428 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003429
Nick Harper95594012016-10-20 14:07:13 -07003430 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003431 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003432 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003433
Steven Valdez87eab492016-06-27 16:34:59 -04003434 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003435 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003436 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003437 if (ssl->session->original_handshake_hash_len == 0) {
3438 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003439 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003440 }
Nick Harper95594012016-10-20 14:07:13 -07003441 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3442 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003443 }
3444
Steven Valdez908ac192017-01-12 13:17:07 -05003445 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3446 size_t hs_hash_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003447 if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003448 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003449 }
Steven Valdez908ac192017-01-12 13:17:07 -05003450 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003451 SHA256_Final(out, &ctx);
3452 *out_len = SHA256_DIGEST_LENGTH;
3453 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003454}
Adam Langley1258b6a2014-06-20 12:00:00 -07003455
David Benjaminc11ea9422017-08-29 16:33:21 -04003456// tls1_record_handshake_hashes_for_channel_id records the current handshake
3457// hashes in |hs->new_session| so that Channel ID resumptions can sign that
3458// data.
Steven Valdez908ac192017-01-12 13:17:07 -05003459int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3460 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003461 // This function should never be called for a resumed session because the
3462 // handshake hashes that we wish to record are for the original, full
3463 // handshake.
Steven Valdez87eab492016-06-27 16:34:59 -04003464 if (ssl->session != NULL) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04003465 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003466 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003467
David Benjamina3d76d02017-07-14 19:36:07 -04003468 static_assert(
David Benjamin45738dd2017-02-09 20:01:26 -05003469 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
David Benjamina3d76d02017-07-14 19:36:07 -04003470 "original_handshake_hash is too small");
Steven Valdez908ac192017-01-12 13:17:07 -05003471
3472 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003473 if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
3474 &digest_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04003475 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003476 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003477
David Benjamina3d76d02017-07-14 19:36:07 -04003478 static_assert(EVP_MAX_MD_SIZE <= 0xff,
3479 "EVP_MAX_MD_SIZE does not fit in uint8_t");
David Benjamin45738dd2017-02-09 20:01:26 -05003480 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003481
Adam Langleyfcf25832014-12-18 17:42:32 -08003482 return 1;
3483}
Nick Harper60a85cb2016-09-23 16:25:11 -07003484
3485int ssl_do_channel_id_callback(SSL *ssl) {
3486 if (ssl->tlsext_channel_id_private != NULL ||
3487 ssl->ctx->channel_id_cb == NULL) {
3488 return 1;
3489 }
3490
3491 EVP_PKEY *key = NULL;
3492 ssl->ctx->channel_id_cb(ssl, &key);
3493 if (key == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003494 // The caller should try again later.
Nick Harper60a85cb2016-09-23 16:25:11 -07003495 return 1;
3496 }
3497
3498 int ret = SSL_set1_tls_channel_id(ssl, key);
3499 EVP_PKEY_free(key);
3500 return ret;
3501}
Adam Langleycfa08c32016-11-17 13:21:27 -08003502
3503int ssl_is_sct_list_valid(const CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003504 // Shallow parse the SCT list for sanity. By the RFC
3505 // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3506 // of the SCTs may be empty.
Adam Langleycfa08c32016-11-17 13:21:27 -08003507 CBS copy = *contents;
3508 CBS sct_list;
3509 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3510 CBS_len(&copy) != 0 ||
3511 CBS_len(&sct_list) == 0) {
3512 return 0;
3513 }
3514
3515 while (CBS_len(&sct_list) > 0) {
3516 CBS sct;
3517 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3518 CBS_len(&sct) == 0) {
3519 return 0;
3520 }
3521 }
3522
3523 return 1;
3524}
David Benjamin86e95b82017-07-18 16:34:25 -04003525
3526} // namespace bssl
3527
3528using namespace bssl;
3529
3530int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
3531 uint16_t extension_type,
3532 const uint8_t **out_data,
3533 size_t *out_len) {
3534 CBS cbs;
3535 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
3536 return 0;
3537 }
3538
3539 *out_data = CBS_data(&cbs);
3540 *out_len = CBS_len(&cbs);
3541 return 1;
3542}
3543
3544void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
3545 ctx->ed25519_enabled = !!enabled;
3546}
3547
3548int SSL_extension_supported(unsigned extension_value) {
3549 uint32_t index;
3550 return extension_value == TLSEXT_TYPE_padding ||
3551 tls_extension_find(&index, extension_value) != NULL;
3552}