blob: ec70d27a69d955b9f8a807b7af16eb6e76427898 [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 Benjamin03973092014-06-24 23:27:17 -0400116#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400117#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/evp.h>
120#include <openssl/hmac.h>
121#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400122#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/rand.h>
124
David Benjamin2ee94aa2015-04-07 22:38:30 -0400125#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400126#include "../crypto/internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800127
128
David Benjamin86e95b82017-07-18 16:34:25 -0400129namespace bssl {
130
David Benjamin8c880a22016-12-03 02:20:34 -0500131static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
Adam Langley95c29f32014-06-20 12:00:00 -0700132
Adam Langleyfcf25832014-12-18 17:42:32 -0800133static int compare_uint16_t(const void *p1, const void *p2) {
134 uint16_t u1 = *((const uint16_t *)p1);
135 uint16_t u2 = *((const uint16_t *)p2);
136 if (u1 < u2) {
137 return -1;
138 } else if (u1 > u2) {
139 return 1;
140 } else {
141 return 0;
142 }
143}
David Benjamin35a7a442014-07-05 00:23:20 -0400144
David Benjaminc11ea9422017-08-29 16:33:21 -0400145// Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
146// more than one extension of the same type in a ClientHello or ServerHello.
147// This function does an initial scan over the extensions block to filter those
148// out.
Adam Langleyfcf25832014-12-18 17:42:32 -0800149static int tls1_check_duplicate_extensions(const CBS *cbs) {
150 CBS extensions = *cbs;
151 size_t num_extensions = 0, i = 0;
152 uint16_t *extension_types = NULL;
153 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400154
David Benjaminc11ea9422017-08-29 16:33:21 -0400155 // First pass: count the extensions.
Adam Langleyfcf25832014-12-18 17:42:32 -0800156 while (CBS_len(&extensions) > 0) {
157 uint16_t type;
158 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400159
Adam Langleyfcf25832014-12-18 17:42:32 -0800160 if (!CBS_get_u16(&extensions, &type) ||
161 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
162 goto done;
163 }
David Benjamin35a7a442014-07-05 00:23:20 -0400164
Adam Langleyfcf25832014-12-18 17:42:32 -0800165 num_extensions++;
166 }
David Benjamin35a7a442014-07-05 00:23:20 -0400167
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 if (num_extensions == 0) {
169 return 1;
170 }
David Benjamin9a373592014-07-25 04:27:53 -0400171
David Benjamin81678aa2017-07-12 22:43:42 -0400172 extension_types =
173 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800174 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400175 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800176 goto done;
177 }
David Benjamin35a7a442014-07-05 00:23:20 -0400178
David Benjaminc11ea9422017-08-29 16:33:21 -0400179 // Second pass: gather the extension types.
Adam Langleyfcf25832014-12-18 17:42:32 -0800180 extensions = *cbs;
181 for (i = 0; i < num_extensions; i++) {
182 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400183
Adam Langleyfcf25832014-12-18 17:42:32 -0800184 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
185 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400186 // This should not happen.
Adam Langleyfcf25832014-12-18 17:42:32 -0800187 goto done;
188 }
189 }
190 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400191
David Benjaminc11ea9422017-08-29 16:33:21 -0400192 // Sort the extensions and make sure there are no duplicates.
Adam Langleyfcf25832014-12-18 17:42:32 -0800193 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
194 for (i = 1; i < num_extensions; i++) {
195 if (extension_types[i - 1] == extension_types[i]) {
196 goto done;
197 }
198 }
David Benjamin35a7a442014-07-05 00:23:20 -0400199
Adam Langleyfcf25832014-12-18 17:42:32 -0800200 ret = 1;
201
David Benjamin35a7a442014-07-05 00:23:20 -0400202done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400203 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800204 return ret;
205}
David Benjamin35a7a442014-07-05 00:23:20 -0400206
David Benjamin7934f082017-08-01 16:32:25 -0400207int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out,
208 const SSLMessage &msg) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500209 OPENSSL_memset(out, 0, sizeof(*out));
David Benjamin731058e2016-12-03 23:15:13 -0500210 out->ssl = ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400211 out->client_hello = CBS_data(&msg.body);
212 out->client_hello_len = CBS_len(&msg.body);
David Benjamin8f2c20e2014-07-09 09:30:38 -0400213
David Benjamine14ff062016-08-09 16:21:24 -0400214 CBS client_hello, random, session_id;
David Benjamin731058e2016-12-03 23:15:13 -0500215 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
216 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjamine14ff062016-08-09 16:21:24 -0400217 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
218 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
219 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800220 return 0;
221 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700222
David Benjamin731058e2016-12-03 23:15:13 -0500223 out->random = CBS_data(&random);
224 out->random_len = CBS_len(&random);
225 out->session_id = CBS_data(&session_id);
226 out->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700227
David Benjaminc11ea9422017-08-29 16:33:21 -0400228 // Skip past DTLS cookie
David Benjamin731058e2016-12-03 23:15:13 -0500229 if (SSL_is_dtls(out->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800230 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400231 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
232 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800233 return 0;
234 }
235 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700236
David Benjamine14ff062016-08-09 16:21:24 -0400237 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800238 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400239 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
240 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800241 CBS_len(&compression_methods) < 1) {
242 return 0;
243 }
David Benjamine14ff062016-08-09 16:21:24 -0400244
David Benjamin731058e2016-12-03 23:15:13 -0500245 out->cipher_suites = CBS_data(&cipher_suites);
246 out->cipher_suites_len = CBS_len(&cipher_suites);
247 out->compression_methods = CBS_data(&compression_methods);
248 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700249
David Benjaminc11ea9422017-08-29 16:33:21 -0400250 // If the ClientHello ends here then it's valid, but doesn't have any
251 // extensions. (E.g. SSLv3.)
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 if (CBS_len(&client_hello) == 0) {
David Benjamin731058e2016-12-03 23:15:13 -0500253 out->extensions = NULL;
254 out->extensions_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 return 1;
256 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700257
David Benjaminc11ea9422017-08-29 16:33:21 -0400258 // Extract extensions and check it is valid.
David Benjamine14ff062016-08-09 16:21:24 -0400259 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
261 !tls1_check_duplicate_extensions(&extensions) ||
262 CBS_len(&client_hello) != 0) {
263 return 0;
264 }
David Benjamine14ff062016-08-09 16:21:24 -0400265
David Benjamin731058e2016-12-03 23:15:13 -0500266 out->extensions = CBS_data(&extensions);
267 out->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700268
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 return 1;
270}
Adam Langleydc9b1412014-06-20 12:00:00 -0700271
David Benjamin731058e2016-12-03 23:15:13 -0500272int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
273 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 CBS extensions;
David Benjamin731058e2016-12-03 23:15:13 -0500275 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 while (CBS_len(&extensions) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400277 // Decode the next extension.
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 uint16_t type;
279 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 if (!CBS_get_u16(&extensions, &type) ||
281 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
282 return 0;
283 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700284
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400286 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800287 return 1;
288 }
289 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700290
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 return 0;
292}
Adam Langley95c29f32014-06-20 12:00:00 -0700293
Steven Valdezce902a92016-05-17 11:47:53 -0400294static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400295 SSL_CURVE_X25519,
296 SSL_CURVE_SECP256R1,
297 SSL_CURVE_SECP384R1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800298};
Adam Langley95c29f32014-06-20 12:00:00 -0700299
David Benjaminf04976b2016-10-07 00:37:55 -0400300void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
Steven Valdez5440fe02016-07-18 12:40:30 -0400301 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400302 *out_group_ids = ssl->supported_group_list;
303 *out_group_ids_len = ssl->supported_group_list_len;
304 if (!*out_group_ids) {
305 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400306 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 }
308}
David Benjamined439582014-07-14 19:13:02 -0400309
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900310int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
311 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400312 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400313
David Benjaminf04976b2016-10-07 00:37:55 -0400314 const uint16_t *groups, *pref, *supp;
315 size_t groups_len, pref_len, supp_len;
316 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800317
David Benjaminc11ea9422017-08-29 16:33:21 -0400318 // Clients are not required to send a supported_groups extension. In this
319 // case, the server is free to pick any group it likes. See RFC 4492,
320 // section 4, paragraph 3.
321 //
322 // However, in the interests of compatibility, we will skip ECDH if the
323 // client didn't send an extension because we can't be sure that they'll
324 // support our favoured group. Thus we do not special-case an emtpy
325 // |peer_supported_group_list|.
David Benjamin55a43642015-04-20 14:45:55 -0400326
David Benjamin4298d772015-12-19 00:18:25 -0500327 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400328 pref = groups;
329 pref_len = groups_len;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900330 supp = hs->peer_supported_group_list;
331 supp_len = hs->peer_supported_group_list_len;
David Benjamin55a43642015-04-20 14:45:55 -0400332 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900333 pref = hs->peer_supported_group_list;
334 pref_len = hs->peer_supported_group_list_len;
Steven Valdezce902a92016-05-17 11:47:53 -0400335 supp = groups;
336 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400337 }
338
David Benjaminf04976b2016-10-07 00:37:55 -0400339 for (size_t i = 0; i < pref_len; i++) {
340 for (size_t j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800341 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400342 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500343 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800344 }
345 }
346 }
347
David Benjamin4298d772015-12-19 00:18:25 -0500348 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800349}
Adam Langley95c29f32014-06-20 12:00:00 -0700350
Steven Valdezce902a92016-05-17 11:47:53 -0400351int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800352 const int *curves, size_t ncurves) {
David Benjamin81678aa2017-07-12 22:43:42 -0400353 uint16_t *group_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
Steven Valdezce902a92016-05-17 11:47:53 -0400354 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800355 return 0;
356 }
357
David Benjamin54091232016-09-05 12:47:25 -0400358 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400359 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
360 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 return 0;
362 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 }
364
Steven Valdezce902a92016-05-17 11:47:53 -0400365 OPENSSL_free(*out_group_ids);
366 *out_group_ids = group_ids;
367 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800368
369 return 1;
370}
Adam Langley95c29f32014-06-20 12:00:00 -0700371
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100372int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
373 const char *curves) {
374 uint16_t *group_ids = NULL;
375 size_t ncurves = 0;
376
377 const char *col;
378 const char *ptr = curves;
379
380 do {
381 col = strchr(ptr, ':');
382
383 uint16_t group_id;
384 if (!ssl_name_to_group_id(&group_id, ptr,
385 col ? (size_t)(col - ptr) : strlen(ptr))) {
386 goto err;
387 }
388
David Benjamin81678aa2017-07-12 22:43:42 -0400389 uint16_t *new_group_ids = (uint16_t *)OPENSSL_realloc(
390 group_ids, (ncurves + 1) * sizeof(uint16_t));
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100391 if (new_group_ids == NULL) {
392 goto err;
393 }
394 group_ids = new_group_ids;
395
396 group_ids[ncurves] = group_id;
397 ncurves++;
398
399 if (col) {
400 ptr = col + 1;
401 }
402 } while (col);
403
404 OPENSSL_free(*out_group_ids);
405 *out_group_ids = group_ids;
406 *out_group_ids_len = ncurves;
407
408 return 1;
409
410err:
411 OPENSSL_free(group_ids);
412 return 0;
413}
414
Steven Valdezce902a92016-05-17 11:47:53 -0400415int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
416 const uint16_t *groups;
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400417 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -0400418 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400419 for (size_t i = 0; i < groups_len; i++) {
420 if (groups[i] == group_id) {
421 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 }
David Benjamin033e5f42014-11-13 18:47:41 -0500424
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400425 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800426}
David Benjamin033e5f42014-11-13 18:47:41 -0500427
David Benjaminc11ea9422017-08-29 16:33:21 -0400428// kVerifySignatureAlgorithms is the default list of accepted signature
429// algorithms for verifying.
430//
431// For now, RSA-PSS signature algorithms are not enabled on Android's system
432// BoringSSL. Once the change in Chrome has stuck and the values are finalized,
433// restore them.
David Benjamin3ef76972016-10-17 17:59:54 -0400434static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400435 // List our preferred algorithms first.
David Benjamin69522112017-03-28 15:38:29 -0500436 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400437 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400438#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400439 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400440#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400441 SSL_SIGN_RSA_PKCS1_SHA256,
442
David Benjaminc11ea9422017-08-29 16:33:21 -0400443 // Larger hashes are acceptable.
David Benjamin3a322f52016-10-26 12:45:35 -0400444 SSL_SIGN_ECDSA_SECP384R1_SHA384,
445#if !defined(BORINGSSL_ANDROID_SYSTEM)
446 SSL_SIGN_RSA_PSS_SHA384,
447#endif
448 SSL_SIGN_RSA_PKCS1_SHA384,
449
David Benjaminc11ea9422017-08-29 16:33:21 -0400450 // TODO(davidben): Remove this.
David Benjamin3ef76972016-10-17 17:59:54 -0400451#if defined(BORINGSSL_ANDROID_SYSTEM)
452 SSL_SIGN_ECDSA_SECP521R1_SHA512,
453#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400454#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400455 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400456#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400457 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400458
David Benjaminc11ea9422017-08-29 16:33:21 -0400459 // For now, SHA-1 is still accepted but least preferable.
David Benjamin3a322f52016-10-26 12:45:35 -0400460 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400461
David Benjamin3a322f52016-10-26 12:45:35 -0400462};
463
David Benjaminc11ea9422017-08-29 16:33:21 -0400464// kSignSignatureAlgorithms is the default list of supported signature
465// algorithms for signing.
466//
467// For now, RSA-PSS signature algorithms are not enabled on Android's system
468// BoringSSL. Once the change in Chrome has stuck and the values are finalized,
469// restore them.
David Benjamin3a322f52016-10-26 12:45:35 -0400470static const uint16_t kSignSignatureAlgorithms[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400471 // List our preferred algorithms first.
David Benjamin69522112017-03-28 15:38:29 -0500472 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400473 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400474#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400475 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400476#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400477 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400478
David Benjaminc11ea9422017-08-29 16:33:21 -0400479 // If needed, sign larger hashes.
480 //
481 // TODO(davidben): Determine which of these may be pruned.
David Benjamin3a322f52016-10-26 12:45:35 -0400482 SSL_SIGN_ECDSA_SECP384R1_SHA384,
483#if !defined(BORINGSSL_ANDROID_SYSTEM)
484 SSL_SIGN_RSA_PSS_SHA384,
485#endif
486 SSL_SIGN_RSA_PKCS1_SHA384,
487
488 SSL_SIGN_ECDSA_SECP521R1_SHA512,
489#if !defined(BORINGSSL_ANDROID_SYSTEM)
490 SSL_SIGN_RSA_PSS_SHA512,
491#endif
492 SSL_SIGN_RSA_PKCS1_SHA512,
493
David Benjaminc11ea9422017-08-29 16:33:21 -0400494 // If the peer supports nothing else, sign with SHA-1.
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400495 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400496 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400497};
498
David Benjamin69522112017-03-28 15:38:29 -0500499int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) {
David Benjamin71c21b42017-04-14 17:05:40 -0400500 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
501 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
502 if (ssl->ctx->num_verify_sigalgs != 0) {
503 sigalgs = ssl->ctx->verify_sigalgs;
504 num_sigalgs = ssl->ctx->num_verify_sigalgs;
505 }
506
507 for (size_t i = 0; i < num_sigalgs; i++) {
508 if (sigalgs == kVerifySignatureAlgorithms &&
509 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500510 !ssl->ctx->ed25519_enabled) {
511 continue;
512 }
David Benjamin71c21b42017-04-14 17:05:40 -0400513 if (!CBB_add_u16(out, sigalgs[i])) {
David Benjamin69522112017-03-28 15:38:29 -0500514 return 0;
515 }
516 }
517
518 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800519}
Adam Langley95c29f32014-06-20 12:00:00 -0700520
David Benjamin8d606e32017-06-15 22:43:04 -0400521int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg) {
David Benjamin71c21b42017-04-14 17:05:40 -0400522 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
523 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
524 if (ssl->ctx->num_verify_sigalgs != 0) {
525 sigalgs = ssl->ctx->verify_sigalgs;
526 num_sigalgs = ssl->ctx->num_verify_sigalgs;
527 }
528
529 for (size_t i = 0; i < num_sigalgs; i++) {
530 if (sigalgs == kVerifySignatureAlgorithms &&
531 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500532 !ssl->ctx->ed25519_enabled) {
533 continue;
534 }
David Benjamin71c21b42017-04-14 17:05:40 -0400535 if (sigalg == sigalgs[i]) {
David Benjamin3ef76972016-10-17 17:59:54 -0400536 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800537 }
538 }
539
David Benjamin3ef76972016-10-17 17:59:54 -0400540 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
541 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
542 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800543}
544
David Benjaminc11ea9422017-08-29 16:33:21 -0400545// tls_extension represents a TLS extension that is handled internally. The
546// |init| function is called for each handshake, before any other functions of
547// the extension. Then the add and parse callbacks are called as needed.
548//
549// The parse callbacks receive a |CBS| that contains the contents of the
550// extension (i.e. not including the type and length bytes). If an extension is
551// not received then the parse callbacks will be called with a NULL CBS so that
552// they can do any processing needed to handle the absence of an extension.
553//
554// The add callbacks receive a |CBB| to which the extension can be appended but
555// the function is responsible for appending the type and length bytes too.
556//
557// All callbacks return one for success and zero for error. If a parse function
558// returns zero then a fatal alert with value |*out_alert| will be sent. If
559// |*out_alert| isn't set, then a |decode_error| alert will be sent.
Adam Langley614c66a2015-06-12 15:26:58 -0700560struct tls_extension {
561 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500562 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700563
David Benjamin8c880a22016-12-03 02:20:34 -0500564 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
565 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
566 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700567
David Benjamin8c880a22016-12-03 02:20:34 -0500568 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
569 CBS *contents);
570 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700571};
572
David Benjamin8c880a22016-12-03 02:20:34 -0500573static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
574 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400575 if (contents != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400576 // Servers MUST NOT send this extension.
Steven Valdez6b8509a2016-07-12 13:38:32 -0400577 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
578 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
579 return 0;
580 }
581
582 return 1;
583}
584
David Benjamin8c880a22016-12-03 02:20:34 -0500585static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
586 CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400587 // This extension from the client is handled elsewhere.
Steven Valdez6b8509a2016-07-12 13:38:32 -0400588 return 1;
589}
590
David Benjamin8c880a22016-12-03 02:20:34 -0500591static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400592 return 1;
593}
Adam Langley614c66a2015-06-12 15:26:58 -0700594
David Benjaminc11ea9422017-08-29 16:33:21 -0400595// Server name indication (SNI).
596//
597// https://tools.ietf.org/html/rfc6066#section-3.
Adam Langley614c66a2015-06-12 15:26:58 -0700598
David Benjamin8c880a22016-12-03 02:20:34 -0500599static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
600 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700601 if (ssl->tlsext_hostname == NULL) {
602 return 1;
603 }
604
605 CBB contents, server_name_list, name;
606 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
607 !CBB_add_u16_length_prefixed(out, &contents) ||
608 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
609 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
610 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
611 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
612 strlen(ssl->tlsext_hostname)) ||
613 !CBB_flush(out)) {
614 return 0;
615 }
616
617 return 1;
618}
619
David Benjamin8c880a22016-12-03 02:20:34 -0500620static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500621 CBS *contents) {
David Benjamina8614602017-09-06 15:40:19 -0400622 // The server may acknowledge SNI with an empty extension. We check the syntax
623 // but otherwise ignore this signal.
624 return contents == NULL || CBS_len(contents) == 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700625}
626
David Benjamin8c880a22016-12-03 02:20:34 -0500627static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500628 CBS *contents) {
David Benjamina8614602017-09-06 15:40:19 -0400629 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700630 if (contents == NULL) {
631 return 1;
632 }
633
David Benjamin9b611e22016-03-03 08:48:30 -0500634 CBS server_name_list, host_name;
635 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700636 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500637 !CBS_get_u8(&server_name_list, &name_type) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400638 // Although the server_name extension was intended to be extensible to
639 // new name types and multiple names, OpenSSL 1.0.x had a bug which meant
640 // different name types will cause an error. Further, RFC 4366 originally
641 // defined syntax inextensibly. RFC 6066 corrected this mistake, but
642 // adding new name types is no longer feasible.
643 //
644 // Act as if the extensibility does not exist to simplify parsing.
David Benjamin9b611e22016-03-03 08:48:30 -0500645 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
646 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700647 CBS_len(contents) != 0) {
648 return 0;
649 }
650
David Benjamin9b611e22016-03-03 08:48:30 -0500651 if (name_type != TLSEXT_NAMETYPE_host_name ||
652 CBS_len(&host_name) == 0 ||
653 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
654 CBS_contains_zero_byte(&host_name)) {
655 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
656 return 0;
657 }
Adam Langley614c66a2015-06-12 15:26:58 -0700658
David Benjaminc11ea9422017-08-29 16:33:21 -0400659 // Copy the hostname as a string.
David Benjamina8614602017-09-06 15:40:19 -0400660 if (!CBS_strdup(&host_name, &ssl->s3->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900661 *out_alert = SSL_AD_INTERNAL_ERROR;
662 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700663 }
664
David Benjaminfd45ee72017-08-31 14:49:09 -0400665 hs->should_ack_sni = true;
Adam Langley614c66a2015-06-12 15:26:58 -0700666 return 1;
667}
668
David Benjamin8c880a22016-12-03 02:20:34 -0500669static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
670 if (hs->ssl->s3->session_reused ||
671 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700672 return 1;
673 }
674
675 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
676 !CBB_add_u16(out, 0 /* length */)) {
677 return 0;
678 }
679
680 return 1;
681}
682
683
David Benjaminc11ea9422017-08-29 16:33:21 -0400684// Renegotiation indication.
685//
686// https://tools.ietf.org/html/rfc5746
Adam Langley5021b222015-06-12 18:27:58 -0700687
David Benjamin8c880a22016-12-03 02:20:34 -0500688static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
689 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400690 // Renegotiation indication is not necessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -0400691 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400692 return 1;
693 }
694
David Benjamin52bf6902016-10-08 12:05:03 -0400695 assert(ssl->s3->initial_handshake_complete ==
696 (ssl->s3->previous_client_finished_len != 0));
697
Adam Langley5021b222015-06-12 18:27:58 -0700698 CBB contents, prev_finished;
699 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
700 !CBB_add_u16_length_prefixed(out, &contents) ||
701 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
702 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
703 ssl->s3->previous_client_finished_len) ||
704 !CBB_flush(out)) {
705 return 0;
706 }
707
708 return 1;
709}
710
David Benjamin8c880a22016-12-03 02:20:34 -0500711static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700712 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500713 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400714 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez246eeee2017-03-26 12:49:17 -0500715 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400716 return 0;
717 }
718
David Benjaminc11ea9422017-08-29 16:33:21 -0400719 // Servers may not switch between omitting the extension and supporting it.
720 // See RFC 5746, sections 3.5 and 4.2.
David Benjamin3e052de2015-11-25 20:10:31 -0500721 if (ssl->s3->initial_handshake_complete &&
722 (contents != NULL) != ssl->s3->send_connection_binding) {
723 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
724 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
725 return 0;
726 }
727
Adam Langley5021b222015-06-12 18:27:58 -0700728 if (contents == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400729 // Strictly speaking, if we want to avoid an attack we should *always* see
730 // RI even on initial ServerHello because the client doesn't see any
731 // renegotiation during an attack. However this would mean we could not
732 // connect to any server which doesn't support RI.
733 //
734 // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
735 // practical terms every client sets it so it's just assumed here.
David Benjamine9cddb82015-11-23 14:36:40 -0500736 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700737 }
738
739 const size_t expected_len = ssl->s3->previous_client_finished_len +
740 ssl->s3->previous_server_finished_len;
741
David Benjaminc11ea9422017-08-29 16:33:21 -0400742 // Check for logic errors
Adam Langley5021b222015-06-12 18:27:58 -0700743 assert(!expected_len || ssl->s3->previous_client_finished_len);
744 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400745 assert(ssl->s3->initial_handshake_complete ==
746 (ssl->s3->previous_client_finished_len != 0));
747 assert(ssl->s3->initial_handshake_complete ==
748 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700749
David Benjaminc11ea9422017-08-29 16:33:21 -0400750 // Parse out the extension contents.
Adam Langley5021b222015-06-12 18:27:58 -0700751 CBS renegotiated_connection;
752 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
753 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400754 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700755 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
756 return 0;
757 }
758
David Benjaminc11ea9422017-08-29 16:33:21 -0400759 // Check that the extension matches.
Adam Langley5021b222015-06-12 18:27:58 -0700760 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400761 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700762 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
763 return 0;
764 }
765
766 const uint8_t *d = CBS_data(&renegotiated_connection);
David Benjamin9343b0b2017-07-01 00:31:27 -0400767 int ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
768 ssl->s3->previous_client_finished_len) == 0;
769#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
770 ok = 1;
771#endif
772 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400773 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700774 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
775 return 0;
776 }
777 d += ssl->s3->previous_client_finished_len;
778
David Benjamin9343b0b2017-07-01 00:31:27 -0400779 ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
780 ssl->s3->previous_server_finished_len) == 0;
781#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
782 ok = 1;
783#endif
784 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400785 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley10e10602017-07-25 13:33:21 -0700786 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Adam Langley5021b222015-06-12 18:27:58 -0700787 return 0;
788 }
David Benjamin046bc1f2017-08-31 15:06:42 -0400789 ssl->s3->send_connection_binding = true;
Adam Langley5021b222015-06-12 18:27:58 -0700790
791 return 1;
792}
793
David Benjamin8c880a22016-12-03 02:20:34 -0500794static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700795 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500796 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400797 // Renegotiation isn't supported as a server so this function should never be
798 // called after the initial handshake.
Adam Langley5021b222015-06-12 18:27:58 -0700799 assert(!ssl->s3->initial_handshake_complete);
800
Steven Valdez143e8b32016-07-11 13:19:03 -0400801 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
802 return 1;
803 }
804
Adam Langley5021b222015-06-12 18:27:58 -0700805 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400806 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700807 }
808
809 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700810 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
811 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400812 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700813 return 0;
814 }
815
David Benjaminc11ea9422017-08-29 16:33:21 -0400816 // Check that the extension matches. We do not support renegotiation as a
817 // server, so this must be empty.
David Benjamin52bf6902016-10-08 12:05:03 -0400818 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400819 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700820 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
821 return 0;
822 }
823
David Benjamin046bc1f2017-08-31 15:06:42 -0400824 ssl->s3->send_connection_binding = true;
Adam Langley5021b222015-06-12 18:27:58 -0700825
826 return 1;
827}
828
David Benjamin8c880a22016-12-03 02:20:34 -0500829static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
830 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400831 // Renegotiation isn't supported as a server so this function should never be
832 // called after the initial handshake.
David Benjamin52bf6902016-10-08 12:05:03 -0400833 assert(!ssl->s3->initial_handshake_complete);
834
Steven Valdez143e8b32016-07-11 13:19:03 -0400835 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
836 return 1;
837 }
838
Adam Langley5021b222015-06-12 18:27:58 -0700839 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400840 !CBB_add_u16(out, 1 /* length */) ||
841 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700842 return 0;
843 }
844
845 return 1;
846}
847
Adam Langley0a056712015-07-01 15:03:33 -0700848
David Benjaminc11ea9422017-08-29 16:33:21 -0400849// Extended Master Secret.
850//
851// https://tools.ietf.org/html/rfc7627
Adam Langley0a056712015-07-01 15:03:33 -0700852
David Benjamin8c880a22016-12-03 02:20:34 -0500853static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400854 // Extended master secret is not necessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -0400855 if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700856 return 1;
857 }
858
859 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
860 !CBB_add_u16(out, 0 /* length */)) {
861 return 0;
862 }
863
864 return 1;
865}
866
David Benjamin8c880a22016-12-03 02:20:34 -0500867static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700868 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500869 SSL *const ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500870
871 if (contents != NULL) {
872 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
873 ssl->version == SSL3_VERSION ||
874 CBS_len(contents) != 0) {
David Benjamin163c9562016-08-29 23:14:17 -0400875 return 0;
876 }
877
David Benjaminfd45ee72017-08-31 14:49:09 -0400878 hs->extended_master_secret = true;
David Benjamin163c9562016-08-29 23:14:17 -0400879 }
880
David Benjaminc11ea9422017-08-29 16:33:21 -0400881 // Whether EMS is negotiated may not change on renegotiation.
David Benjaminfc02b592017-02-17 16:26:01 -0500882 if (ssl->s3->established_session != NULL &&
883 hs->extended_master_secret !=
David Benjaminfd45ee72017-08-31 14:49:09 -0400884 !!ssl->s3->established_session->extended_master_secret) {
David Benjaminfc02b592017-02-17 16:26:01 -0500885 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
886 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400887 return 0;
888 }
889
Adam Langley0a056712015-07-01 15:03:33 -0700890 return 1;
891}
892
David Benjamin8c880a22016-12-03 02:20:34 -0500893static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500894 CBS *contents) {
David Benjaminfc02b592017-02-17 16:26:01 -0500895 uint16_t version = ssl3_protocol_version(hs->ssl);
David Benjamin8c880a22016-12-03 02:20:34 -0500896 if (version >= TLS1_3_VERSION ||
897 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400898 return 1;
899 }
900
901 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700902 return 1;
903 }
904
905 if (CBS_len(contents) != 0) {
906 return 0;
907 }
908
David Benjaminfd45ee72017-08-31 14:49:09 -0400909 hs->extended_master_secret = true;
Adam Langley0a056712015-07-01 15:03:33 -0700910 return 1;
911}
912
David Benjamin8c880a22016-12-03 02:20:34 -0500913static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -0500914 if (!hs->extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700915 return 1;
916 }
917
918 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
919 !CBB_add_u16(out, 0 /* length */)) {
920 return 0;
921 }
922
923 return 1;
924}
925
Adam Langley9b05bc52015-07-01 15:25:33 -0700926
David Benjaminc11ea9422017-08-29 16:33:21 -0400927// Session tickets.
928//
929// https://tools.ietf.org/html/rfc5077
Adam Langley9b05bc52015-07-01 15:25:33 -0700930
David Benjamin8c880a22016-12-03 02:20:34 -0500931static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
932 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400933 // TLS 1.3 uses a different ticket extension.
David Benjamin68161cb2017-06-20 14:49:43 -0400934 if (hs->min_version >= TLS1_3_VERSION ||
David Benjamin7c7d8312016-08-20 13:39:03 -0400935 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700936 return 1;
937 }
938
939 const uint8_t *ticket_data = NULL;
940 int ticket_len = 0;
941
David Benjaminc11ea9422017-08-29 16:33:21 -0400942 // Renegotiation does not participate in session resumption. However, still
943 // advertise the extension to avoid potentially breaking servers which carry
944 // over the state from the previous handshake, such as OpenSSL servers
945 // without upstream's 3c3f0259238594d77264a78944d409f2127642c4.
Adam Langley9b05bc52015-07-01 15:25:33 -0700946 if (!ssl->s3->initial_handshake_complete &&
947 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400948 ssl->session->tlsext_tick != NULL &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400949 // Don't send TLS 1.3 session tickets in the ticket extension.
Steven Valdez8f36c512017-06-20 10:55:02 -0400950 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700951 ticket_data = ssl->session->tlsext_tick;
952 ticket_len = ssl->session->tlsext_ticklen;
953 }
954
955 CBB ticket;
956 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
957 !CBB_add_u16_length_prefixed(out, &ticket) ||
958 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
959 !CBB_flush(out)) {
960 return 0;
961 }
962
963 return 1;
964}
965
David Benjamin8c880a22016-12-03 02:20:34 -0500966static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -0700967 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500968 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -0700969 if (contents == NULL) {
970 return 1;
971 }
972
Steven Valdez143e8b32016-07-11 13:19:03 -0400973 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
974 return 0;
975 }
976
David Benjaminc11ea9422017-08-29 16:33:21 -0400977 // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
978 // this function should never be called, even if the server tries to send the
979 // extension.
Adam Langley9b05bc52015-07-01 15:25:33 -0700980 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
981
982 if (CBS_len(contents) != 0) {
983 return 0;
984 }
985
David Benjaminfd45ee72017-08-31 14:49:09 -0400986 hs->ticket_expected = true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700987 return 1;
988}
989
David Benjamin8c880a22016-12-03 02:20:34 -0500990static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
991 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700992 return 1;
993 }
994
David Benjaminc11ea9422017-08-29 16:33:21 -0400995 // If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true.
David Benjamin8c880a22016-12-03 02:20:34 -0500996 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -0700997
998 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
999 !CBB_add_u16(out, 0 /* length */)) {
1000 return 0;
1001 }
1002
1003 return 1;
1004}
1005
1006
David Benjaminc11ea9422017-08-29 16:33:21 -04001007// Signature Algorithms.
1008//
1009// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
Adam Langley2e857bd2015-07-01 16:09:19 -07001010
David Benjamin8c880a22016-12-03 02:20:34 -05001011static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1012 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001013 if (hs->max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001014 return 1;
1015 }
1016
David Benjamin0fc37ef2016-08-17 15:29:46 -04001017 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001018 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1019 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin69522112017-03-28 15:38:29 -05001020 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
1021 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
1022 !CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001023 return 0;
1024 }
1025
1026 return 1;
1027}
1028
David Benjamin8c880a22016-12-03 02:20:34 -05001029static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001030 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001031 OPENSSL_free(hs->peer_sigalgs);
1032 hs->peer_sigalgs = NULL;
1033 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001034
Adam Langley2e857bd2015-07-01 16:09:19 -07001035 if (contents == NULL) {
1036 return 1;
1037 }
1038
1039 CBS supported_signature_algorithms;
1040 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001041 CBS_len(contents) != 0 ||
1042 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001043 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001044 return 0;
1045 }
1046
1047 return 1;
1048}
1049
Adam Langley2e857bd2015-07-01 16:09:19 -07001050
David Benjaminc11ea9422017-08-29 16:33:21 -04001051// OCSP Stapling.
1052//
1053// https://tools.ietf.org/html/rfc6066#section-8
Adam Langleybb0bd042015-07-01 16:21:03 -07001054
David Benjamin8c880a22016-12-03 02:20:34 -05001055static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1056 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001057 if (!ssl->ocsp_stapling_enabled) {
1058 return 1;
1059 }
1060
1061 CBB contents;
1062 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1063 !CBB_add_u16_length_prefixed(out, &contents) ||
1064 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1065 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1066 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1067 !CBB_flush(out)) {
1068 return 0;
1069 }
1070
1071 return 1;
1072}
1073
David Benjamin8c880a22016-12-03 02:20:34 -05001074static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001075 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001076 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001077 if (contents == NULL) {
1078 return 1;
1079 }
1080
David Benjaminc11ea9422017-08-29 16:33:21 -04001081 // TLS 1.3 OCSP responses are included in the Certificate extensions.
Steven Valdeza833c352016-11-01 13:39:36 -04001082 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001083 return 0;
1084 }
1085
David Benjaminc11ea9422017-08-29 16:33:21 -04001086 // OCSP stapling is forbidden on non-certificate ciphers.
Steven Valdeza833c352016-11-01 13:39:36 -04001087 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001088 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001089 return 0;
1090 }
1091
David Benjaminc11ea9422017-08-29 16:33:21 -04001092 // Note this does not check for resumption in TLS 1.2. Sending
1093 // status_request here does not make sense, but OpenSSL does so and the
1094 // specification does not say anything. Tolerate it but ignore it.
David Benjamin942f4ed2016-07-16 19:03:49 +03001095
David Benjaminfd45ee72017-08-31 14:49:09 -04001096 hs->certificate_status_expected = true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001097 return 1;
1098}
1099
David Benjamin8c880a22016-12-03 02:20:34 -05001100static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001101 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001102 if (contents == NULL) {
1103 return 1;
1104 }
1105
1106 uint8_t status_type;
1107 if (!CBS_get_u8(contents, &status_type)) {
1108 return 0;
1109 }
1110
David Benjaminc11ea9422017-08-29 16:33:21 -04001111 // We cannot decide whether OCSP stapling will occur yet because the correct
1112 // SSL_CTX might not have been selected.
David Benjamin8c880a22016-12-03 02:20:34 -05001113 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001114
Adam Langleybb0bd042015-07-01 16:21:03 -07001115 return 1;
1116}
1117
David Benjamin8c880a22016-12-03 02:20:34 -05001118static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1119 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001120 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001121 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001122 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001123 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001124 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001125 return 1;
1126 }
1127
David Benjaminfd45ee72017-08-31 14:49:09 -04001128 hs->certificate_status_expected = true;
David Benjamin942f4ed2016-07-16 19:03:49 +03001129
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001130 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001131 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001132}
1133
1134
David Benjaminc11ea9422017-08-29 16:33:21 -04001135// Next protocol negotiation.
1136//
1137// https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
Adam Langley97dfcbf2015-07-01 18:35:20 -07001138
David Benjamin8c880a22016-12-03 02:20:34 -05001139static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1140 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001141 if (ssl->s3->initial_handshake_complete ||
1142 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001143 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001144 return 1;
1145 }
1146
1147 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1148 !CBB_add_u16(out, 0 /* length */)) {
1149 return 0;
1150 }
1151
1152 return 1;
1153}
1154
David Benjamin8c880a22016-12-03 02:20:34 -05001155static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001156 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001157 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001158 if (contents == NULL) {
1159 return 1;
1160 }
1161
Steven Valdez143e8b32016-07-11 13:19:03 -04001162 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1163 return 0;
1164 }
1165
David Benjaminc11ea9422017-08-29 16:33:21 -04001166 // If any of these are false then we should never have sent the NPN
1167 // extension in the ClientHello and thus this function should never have been
1168 // called.
Adam Langley97dfcbf2015-07-01 18:35:20 -07001169 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001170 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001171 assert(ssl->ctx->next_proto_select_cb != NULL);
1172
David Benjamin76c2efc2015-08-31 14:24:29 -04001173 if (ssl->s3->alpn_selected != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001174 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001175 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1176 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1177 return 0;
1178 }
1179
Adam Langley97dfcbf2015-07-01 18:35:20 -07001180 const uint8_t *const orig_contents = CBS_data(contents);
1181 const size_t orig_len = CBS_len(contents);
1182
1183 while (CBS_len(contents) != 0) {
1184 CBS proto;
1185 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1186 CBS_len(&proto) == 0) {
1187 return 0;
1188 }
1189 }
1190
1191 uint8_t *selected;
1192 uint8_t selected_len;
1193 if (ssl->ctx->next_proto_select_cb(
1194 ssl, &selected, &selected_len, orig_contents, orig_len,
1195 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1196 *out_alert = SSL_AD_INTERNAL_ERROR;
1197 return 0;
1198 }
1199
David Benjamin79978df2015-12-25 15:56:49 -05001200 OPENSSL_free(ssl->s3->next_proto_negotiated);
David Benjamin81678aa2017-07-12 22:43:42 -04001201 ssl->s3->next_proto_negotiated =
1202 (uint8_t *)BUF_memdup(selected, selected_len);
David Benjamin79978df2015-12-25 15:56:49 -05001203 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001204 *out_alert = SSL_AD_INTERNAL_ERROR;
1205 return 0;
1206 }
1207
David Benjamin79978df2015-12-25 15:56:49 -05001208 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjaminfd45ee72017-08-31 14:49:09 -04001209 hs->next_proto_neg_seen = true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001210
1211 return 1;
1212}
1213
David Benjamin8c880a22016-12-03 02:20:34 -05001214static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001215 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001216 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001217 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1218 return 1;
1219 }
1220
Adam Langley97dfcbf2015-07-01 18:35:20 -07001221 if (contents != NULL && CBS_len(contents) != 0) {
1222 return 0;
1223 }
1224
1225 if (contents == NULL ||
1226 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001227 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001228 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001229 return 1;
1230 }
1231
David Benjaminfd45ee72017-08-31 14:49:09 -04001232 hs->next_proto_neg_seen = true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001233 return 1;
1234}
1235
David Benjamin8c880a22016-12-03 02:20:34 -05001236static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1237 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001238 // |next_proto_neg_seen| might have been cleared when an ALPN extension was
1239 // parsed.
David Benjamin8c880a22016-12-03 02:20:34 -05001240 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001241 return 1;
1242 }
1243
1244 const uint8_t *npa;
1245 unsigned npa_len;
1246
1247 if (ssl->ctx->next_protos_advertised_cb(
1248 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1249 SSL_TLSEXT_ERR_OK) {
David Benjaminfd45ee72017-08-31 14:49:09 -04001250 hs->next_proto_neg_seen = false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001251 return 1;
1252 }
1253
1254 CBB contents;
1255 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1256 !CBB_add_u16_length_prefixed(out, &contents) ||
1257 !CBB_add_bytes(&contents, npa, npa_len) ||
1258 !CBB_flush(out)) {
1259 return 0;
1260 }
1261
1262 return 1;
1263}
1264
1265
David Benjaminc11ea9422017-08-29 16:33:21 -04001266// Signed certificate timestamps.
1267//
1268// https://tools.ietf.org/html/rfc6962#section-3.3.1
Adam Langleyab8d87d2015-07-10 12:21:39 -07001269
David Benjamin8c880a22016-12-03 02:20:34 -05001270static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1271 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001272 if (!ssl->signed_cert_timestamps_enabled) {
1273 return 1;
1274 }
1275
1276 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1277 !CBB_add_u16(out, 0 /* length */)) {
1278 return 0;
1279 }
1280
1281 return 1;
1282}
1283
David Benjamin8c880a22016-12-03 02:20:34 -05001284static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001285 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001286 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001287 if (contents == NULL) {
1288 return 1;
1289 }
1290
David Benjaminc11ea9422017-08-29 16:33:21 -04001291 // TLS 1.3 SCTs are included in the Certificate extensions.
Steven Valdeza833c352016-11-01 13:39:36 -04001292 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001293 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001294 return 0;
1295 }
1296
David Benjaminc11ea9422017-08-29 16:33:21 -04001297 // If this is false then we should never have sent the SCT extension in the
1298 // ClientHello and thus this function should never have been called.
Adam Langleyab8d87d2015-07-10 12:21:39 -07001299 assert(ssl->signed_cert_timestamps_enabled);
1300
Adam Langleycfa08c32016-11-17 13:21:27 -08001301 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001302 *out_alert = SSL_AD_DECODE_ERROR;
1303 return 0;
1304 }
1305
David Benjaminc11ea9422017-08-29 16:33:21 -04001306 // Session resumption uses the original session information. The extension
1307 // should not be sent on resumption, but RFC 6962 did not make it a
1308 // requirement, so tolerate this.
1309 //
1310 // TODO(davidben): Enforce this anyway.
David Benjamin8fc2dc02017-08-22 15:07:51 -07001311 if (!ssl->s3->session_reused) {
1312 CRYPTO_BUFFER_free(hs->new_session->signed_cert_timestamp_list);
1313 hs->new_session->signed_cert_timestamp_list =
1314 CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool);
1315 if (hs->new_session->signed_cert_timestamp_list == nullptr) {
1316 *out_alert = SSL_AD_INTERNAL_ERROR;
1317 return 0;
1318 }
Adam Langleyab8d87d2015-07-10 12:21:39 -07001319 }
1320
1321 return 1;
1322}
1323
David Benjamin8c880a22016-12-03 02:20:34 -05001324static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001325 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001326 if (contents == NULL) {
1327 return 1;
1328 }
1329
1330 if (CBS_len(contents) != 0) {
1331 return 0;
1332 }
1333
David Benjaminfd45ee72017-08-31 14:49:09 -04001334 hs->scts_requested = true;
David Benjamin53210cb2016-11-16 09:01:48 +09001335 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001336}
1337
David Benjamin8c880a22016-12-03 02:20:34 -05001338static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1339 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001340 // The extension shouldn't be sent when resuming sessions.
Steven Valdeza833c352016-11-01 13:39:36 -04001341 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1342 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001343 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001344 return 1;
1345 }
1346
1347 CBB contents;
1348 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1349 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001350 CBB_add_bytes(
1351 &contents,
1352 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1353 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001354 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001355}
1356
1357
David Benjaminc11ea9422017-08-29 16:33:21 -04001358// Application-level Protocol Negotiation.
1359//
1360// https://tools.ietf.org/html/rfc7301
Adam Langleyf18e4532015-07-10 13:39:53 -07001361
David Benjamin8c880a22016-12-03 02:20:34 -05001362static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1363 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001364 if (ssl->alpn_client_proto_list == NULL ||
1365 ssl->s3->initial_handshake_complete) {
1366 return 1;
1367 }
1368
1369 CBB contents, proto_list;
1370 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1371 !CBB_add_u16_length_prefixed(out, &contents) ||
1372 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1373 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1374 ssl->alpn_client_proto_list_len) ||
1375 !CBB_flush(out)) {
1376 return 0;
1377 }
1378
1379 return 1;
1380}
1381
David Benjamin8c880a22016-12-03 02:20:34 -05001382static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001383 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001384 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001385 if (contents == NULL) {
1386 return 1;
1387 }
1388
1389 assert(!ssl->s3->initial_handshake_complete);
1390 assert(ssl->alpn_client_proto_list != NULL);
1391
David Benjamin8c880a22016-12-03 02:20:34 -05001392 if (hs->next_proto_neg_seen) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001393 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001394 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1395 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1396 return 0;
1397 }
1398
David Benjaminc11ea9422017-08-29 16:33:21 -04001399 // The extension data consists of a ProtocolNameList which must have
1400 // exactly one ProtocolName. Each of these is length-prefixed.
Adam Langleyf18e4532015-07-10 13:39:53 -07001401 CBS protocol_name_list, protocol_name;
1402 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1403 CBS_len(contents) != 0 ||
1404 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001405 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001406 CBS_len(&protocol_name) == 0 ||
1407 CBS_len(&protocol_name_list) != 0) {
1408 return 0;
1409 }
1410
David Benjaminc8ff30c2017-04-04 13:52:36 -04001411 if (!ssl->ctx->allow_unknown_alpn_protos) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001412 // Check that the protocol name is one of the ones we advertised.
David Benjaminc8ff30c2017-04-04 13:52:36 -04001413 int protocol_ok = 0;
1414 CBS client_protocol_name_list, client_protocol_name;
1415 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1416 ssl->alpn_client_proto_list_len);
1417 while (CBS_len(&client_protocol_name_list) > 0) {
1418 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1419 &client_protocol_name)) {
1420 *out_alert = SSL_AD_INTERNAL_ERROR;
1421 return 0;
1422 }
1423
1424 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1425 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1426 CBS_data(&protocol_name),
1427 CBS_len(&protocol_name)) == 0) {
1428 protocol_ok = 1;
1429 break;
1430 }
1431 }
1432
1433 if (!protocol_ok) {
1434 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1435 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3e517572016-08-11 11:52:23 -04001436 return 0;
1437 }
David Benjamin3e517572016-08-11 11:52:23 -04001438 }
1439
Adam Langleyf18e4532015-07-10 13:39:53 -07001440 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1441 &ssl->s3->alpn_selected_len)) {
1442 *out_alert = SSL_AD_INTERNAL_ERROR;
1443 return 0;
1444 }
1445
1446 return 1;
1447}
1448
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001449int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001450 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001451 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001452 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001453 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001454 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001455 client_hello, &contents,
1456 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001457 // Ignore ALPN if not configured or no extension was supplied.
Adam Langleyf18e4532015-07-10 13:39:53 -07001458 return 1;
1459 }
1460
David Benjaminc11ea9422017-08-29 16:33:21 -04001461 // ALPN takes precedence over NPN.
David Benjaminfd45ee72017-08-31 14:49:09 -04001462 hs->next_proto_neg_seen = false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001463
1464 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001465 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1466 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001467 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001468 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1469 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001470 return 0;
1471 }
1472
David Benjaminc11ea9422017-08-29 16:33:21 -04001473 // Validate the protocol list.
Adam Langleyf18e4532015-07-10 13:39:53 -07001474 CBS protocol_name_list_copy = protocol_name_list;
1475 while (CBS_len(&protocol_name_list_copy) > 0) {
1476 CBS protocol_name;
1477
1478 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001479 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001480 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001481 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1482 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001483 return 0;
1484 }
1485 }
1486
1487 const uint8_t *selected;
1488 uint8_t selected_len;
1489 if (ssl->ctx->alpn_select_cb(
1490 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1491 CBS_len(&protocol_name_list),
1492 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1493 OPENSSL_free(ssl->s3->alpn_selected);
David Benjamin81678aa2017-07-12 22:43:42 -04001494 ssl->s3->alpn_selected = (uint8_t *)BUF_memdup(selected, selected_len);
Adam Langleyf18e4532015-07-10 13:39:53 -07001495 if (ssl->s3->alpn_selected == NULL) {
1496 *out_alert = SSL_AD_INTERNAL_ERROR;
1497 return 0;
1498 }
1499 ssl->s3->alpn_selected_len = selected_len;
1500 }
1501
1502 return 1;
1503}
1504
David Benjamin8c880a22016-12-03 02:20:34 -05001505static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1506 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001507 if (ssl->s3->alpn_selected == NULL) {
1508 return 1;
1509 }
1510
1511 CBB contents, proto_list, proto;
1512 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1513 !CBB_add_u16_length_prefixed(out, &contents) ||
1514 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1515 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001516 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1517 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001518 !CBB_flush(out)) {
1519 return 0;
1520 }
1521
1522 return 1;
1523}
1524
1525
David Benjaminc11ea9422017-08-29 16:33:21 -04001526// Channel ID.
1527//
1528// https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
Adam Langley49c7af12015-07-10 14:33:46 -07001529
David Benjamin8c880a22016-12-03 02:20:34 -05001530static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
David Benjamin046bc1f2017-08-31 15:06:42 -04001531 hs->ssl->s3->tlsext_channel_id_valid = false;
Adam Langley49c7af12015-07-10 14:33:46 -07001532}
1533
David Benjamin8c880a22016-12-03 02:20:34 -05001534static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1535 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001536 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001537 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001538 return 1;
1539 }
1540
1541 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1542 !CBB_add_u16(out, 0 /* length */)) {
1543 return 0;
1544 }
1545
1546 return 1;
1547}
1548
David Benjamin8c880a22016-12-03 02:20:34 -05001549static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1550 uint8_t *out_alert, CBS *contents) {
1551 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001552 if (contents == NULL) {
1553 return 1;
1554 }
1555
David Benjamince079fd2016-08-02 16:22:34 -04001556 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001557 assert(ssl->tlsext_channel_id_enabled);
1558
1559 if (CBS_len(contents) != 0) {
1560 return 0;
1561 }
1562
David Benjamin046bc1f2017-08-31 15:06:42 -04001563 ssl->s3->tlsext_channel_id_valid = true;
Adam Langley49c7af12015-07-10 14:33:46 -07001564 return 1;
1565}
1566
David Benjamin8c880a22016-12-03 02:20:34 -05001567static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1568 uint8_t *out_alert, CBS *contents) {
1569 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001570 if (contents == NULL ||
1571 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001572 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001573 return 1;
1574 }
1575
1576 if (CBS_len(contents) != 0) {
1577 return 0;
1578 }
1579
David Benjamin046bc1f2017-08-31 15:06:42 -04001580 ssl->s3->tlsext_channel_id_valid = true;
Adam Langley49c7af12015-07-10 14:33:46 -07001581 return 1;
1582}
1583
David Benjamin8c880a22016-12-03 02:20:34 -05001584static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1585 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001586 if (!ssl->s3->tlsext_channel_id_valid) {
1587 return 1;
1588 }
1589
1590 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1591 !CBB_add_u16(out, 0 /* length */)) {
1592 return 0;
1593 }
1594
1595 return 1;
1596}
1597
Adam Langley391250d2015-07-15 19:06:07 -07001598
David Benjaminc11ea9422017-08-29 16:33:21 -04001599// Secure Real-time Transport Protocol (SRTP) extension.
1600//
1601// https://tools.ietf.org/html/rfc5764
Adam Langley391250d2015-07-15 19:06:07 -07001602
Adam Langley391250d2015-07-15 19:06:07 -07001603
David Benjamin8c880a22016-12-03 02:20:34 -05001604static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1605 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001606}
1607
David Benjamin8c880a22016-12-03 02:20:34 -05001608static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1609 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001610 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
David Benjaminee910bf2017-07-25 22:36:00 -04001611 if (profiles == NULL ||
1612 sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
Adam Langley391250d2015-07-15 19:06:07 -07001613 return 1;
1614 }
1615
1616 CBB contents, profile_ids;
1617 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1618 !CBB_add_u16_length_prefixed(out, &contents) ||
1619 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1620 return 0;
1621 }
1622
David Benjaminee910bf2017-07-25 22:36:00 -04001623 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
1624 if (!CBB_add_u16(&profile_ids, profile->id)) {
Adam Langley391250d2015-07-15 19:06:07 -07001625 return 0;
1626 }
1627 }
1628
1629 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1630 !CBB_flush(out)) {
1631 return 0;
1632 }
1633
1634 return 1;
1635}
1636
David Benjamin8c880a22016-12-03 02:20:34 -05001637static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001638 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001639 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001640 if (contents == NULL) {
1641 return 1;
1642 }
1643
David Benjaminc11ea9422017-08-29 16:33:21 -04001644 // The extension consists of a u16-prefixed profile ID list containing a
1645 // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1646 //
1647 // See https://tools.ietf.org/html/rfc5764#section-4.1.1
Adam Langley391250d2015-07-15 19:06:07 -07001648 CBS profile_ids, srtp_mki;
1649 uint16_t profile_id;
1650 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1651 !CBS_get_u16(&profile_ids, &profile_id) ||
1652 CBS_len(&profile_ids) != 0 ||
1653 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1654 CBS_len(contents) != 0) {
1655 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1656 return 0;
1657 }
1658
1659 if (CBS_len(&srtp_mki) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001660 // Must be no MKI, since we never offer one.
Adam Langley391250d2015-07-15 19:06:07 -07001661 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1662 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1663 return 0;
1664 }
1665
1666 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1667
David Benjaminc11ea9422017-08-29 16:33:21 -04001668 // Check to see if the server gave us something we support (and presumably
1669 // offered).
David Benjaminee910bf2017-07-25 22:36:00 -04001670 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001671 if (profile->id == profile_id) {
1672 ssl->srtp_profile = profile;
1673 return 1;
1674 }
1675 }
1676
1677 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1678 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1679 return 0;
1680}
1681
David Benjamin8c880a22016-12-03 02:20:34 -05001682static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001683 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001684 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001685 if (contents == NULL) {
1686 return 1;
1687 }
1688
1689 CBS profile_ids, srtp_mki;
1690 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1691 CBS_len(&profile_ids) < 2 ||
1692 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1693 CBS_len(contents) != 0) {
1694 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1695 return 0;
1696 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001697 // Discard the MKI value for now.
Adam Langley391250d2015-07-15 19:06:07 -07001698
1699 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1700 SSL_get_srtp_profiles(ssl);
1701
David Benjaminc11ea9422017-08-29 16:33:21 -04001702 // Pick the server's most preferred profile.
David Benjaminee910bf2017-07-25 22:36:00 -04001703 for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001704 CBS profile_ids_tmp;
1705 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1706
1707 while (CBS_len(&profile_ids_tmp) > 0) {
1708 uint16_t profile_id;
1709 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1710 return 0;
1711 }
1712
1713 if (server_profile->id == profile_id) {
1714 ssl->srtp_profile = server_profile;
1715 return 1;
1716 }
1717 }
1718 }
1719
1720 return 1;
1721}
1722
David Benjamin8c880a22016-12-03 02:20:34 -05001723static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1724 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001725 if (ssl->srtp_profile == NULL) {
1726 return 1;
1727 }
1728
1729 CBB contents, profile_ids;
1730 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1731 !CBB_add_u16_length_prefixed(out, &contents) ||
1732 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1733 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1734 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1735 !CBB_flush(out)) {
1736 return 0;
1737 }
1738
1739 return 1;
1740}
1741
Adam Langleybdd5d662015-07-20 16:19:08 -07001742
David Benjaminc11ea9422017-08-29 16:33:21 -04001743// EC point formats.
1744//
1745// https://tools.ietf.org/html/rfc4492#section-5.1.2
Adam Langleybdd5d662015-07-20 16:19:08 -07001746
David Benjamin8c880a22016-12-03 02:20:34 -05001747static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001748 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001749 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1750 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001751 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1752 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001753 !CBB_flush(out)) {
1754 return 0;
1755 }
1756
1757 return 1;
1758}
1759
David Benjamin8c880a22016-12-03 02:20:34 -05001760static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001761 // The point format extension is unneccessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -04001762 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin70aba262016-11-01 12:08:15 -04001763 return 1;
1764 }
1765
David Benjamin8c880a22016-12-03 02:20:34 -05001766 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001767}
1768
David Benjamin8c880a22016-12-03 02:20:34 -05001769static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001770 CBS *contents) {
1771 if (contents == NULL) {
1772 return 1;
1773 }
1774
David Benjamin8c880a22016-12-03 02:20:34 -05001775 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001776 return 0;
1777 }
1778
Adam Langleybdd5d662015-07-20 16:19:08 -07001779 CBS ec_point_format_list;
1780 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1781 CBS_len(contents) != 0) {
1782 return 0;
1783 }
1784
David Benjaminc11ea9422017-08-29 16:33:21 -04001785 // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1786 // point format.
David Benjamin17cf2cb2016-12-13 01:07:13 -05001787 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1788 TLSEXT_ECPOINTFORMAT_uncompressed,
1789 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001790 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001791 return 0;
1792 }
1793
1794 return 1;
1795}
1796
David Benjamin8c880a22016-12-03 02:20:34 -05001797static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001798 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001799 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001800 return 1;
1801 }
1802
David Benjamin8c880a22016-12-03 02:20:34 -05001803 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001804}
1805
David Benjamin8c880a22016-12-03 02:20:34 -05001806static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1807 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001808 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1809 return 1;
1810 }
1811
David Benjamin45738dd2017-02-09 20:01:26 -05001812 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1813 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001814 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001815
1816 if (!using_ecc) {
1817 return 1;
1818 }
1819
David Benjamin8c880a22016-12-03 02:20:34 -05001820 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001821}
1822
Steven Valdeza833c352016-11-01 13:39:36 -04001823
David Benjaminc11ea9422017-08-29 16:33:21 -04001824// Pre Shared Key
1825//
1826// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
Steven Valdez4aa154e2016-07-29 14:32:55 -04001827
David Benjamin8c880a22016-12-03 02:20:34 -05001828static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1829 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001830 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001831 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001832 return 0;
1833 }
1834
Steven Valdez8f36c512017-06-20 10:55:02 -04001835 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001836 return 15 + ssl->session->tlsext_ticklen + binder_len;
1837}
1838
David Benjamin8c880a22016-12-03 02:20:34 -05001839static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1840 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001841 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001842 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001843 return 1;
1844 }
1845
David Benjaminad8f5e12017-02-20 17:00:20 -05001846 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001847 ssl_get_current_time(ssl, &now);
1848 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1849 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1850
David Benjaminc11ea9422017-08-29 16:33:21 -04001851 // Fill in a placeholder zero binder of the appropriate length. It will be
1852 // computed and filled in later after length prefixes are computed.
Steven Valdeza833c352016-11-01 13:39:36 -04001853 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez8f36c512017-06-20 10:55:02 -04001854 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001855
1856 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001857 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1858 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001859 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001860 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1861 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001862 ssl->session->tlsext_ticklen) ||
1863 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1864 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1865 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1866 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001867 return 0;
1868 }
1869
David Benjaminfd45ee72017-08-31 14:49:09 -04001870 hs->needs_psk_binder = true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001871 return CBB_flush(out);
1872}
1873
David Benjamin8baf9632016-11-17 17:11:16 +09001874int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1875 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001876 CBS *contents) {
1877 uint16_t psk_id;
1878 if (!CBS_get_u16(contents, &psk_id) ||
1879 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001880 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001881 *out_alert = SSL_AD_DECODE_ERROR;
1882 return 0;
1883 }
1884
David Benjaminc11ea9422017-08-29 16:33:21 -04001885 // We only advertise one PSK identity, so the only legal index is zero.
Steven Valdez4aa154e2016-07-29 14:32:55 -04001886 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001887 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001888 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1889 return 0;
1890 }
1891
1892 return 1;
1893}
1894
David Benjamin35ac5b72017-03-03 15:05:56 -05001895int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001896 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001897 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001898 // We only process the first PSK identity since we don't support pure PSK.
David Benjamin707af292017-03-10 17:47:18 -05001899 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001900 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001901 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001902 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001903 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001904 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001905 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001906 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001907 *out_alert = SSL_AD_DECODE_ERROR;
1908 return 0;
1909 }
1910
Steven Valdeza833c352016-11-01 13:39:36 -04001911 *out_binders = binders;
1912
David Benjaminc11ea9422017-08-29 16:33:21 -04001913 // Check the syntax of the remaining identities, but do not process them.
David Benjaminaedf3032016-12-01 16:47:56 -05001914 size_t num_identities = 1;
1915 while (CBS_len(&identities) != 0) {
1916 CBS unused_ticket;
1917 uint32_t unused_obfuscated_ticket_age;
1918 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1919 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1920 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1921 *out_alert = SSL_AD_DECODE_ERROR;
1922 return 0;
1923 }
1924
1925 num_identities++;
1926 }
1927
David Benjaminc11ea9422017-08-29 16:33:21 -04001928 // Check the syntax of the binders. The value will be checked later if
1929 // resuming.
David Benjaminaedf3032016-12-01 16:47:56 -05001930 size_t num_binders = 0;
1931 while (CBS_len(&binders) != 0) {
1932 CBS binder;
1933 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1934 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1935 *out_alert = SSL_AD_DECODE_ERROR;
1936 return 0;
1937 }
1938
1939 num_binders++;
1940 }
1941
1942 if (num_identities != num_binders) {
1943 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1944 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04001945 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04001946 }
1947
David Benjamine7f60a22016-11-16 18:54:25 +09001948 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001949}
1950
David Benjamin8baf9632016-11-17 17:11:16 +09001951int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1952 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001953 return 1;
1954 }
1955
1956 CBB contents;
1957 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1958 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001959 // We only consider the first identity for resumption
Steven Valdez4aa154e2016-07-29 14:32:55 -04001960 !CBB_add_u16(&contents, 0) ||
1961 !CBB_flush(out)) {
1962 return 0;
1963 }
1964
1965 return 1;
1966}
1967
1968
David Benjaminc11ea9422017-08-29 16:33:21 -04001969// Pre-Shared Key Exchange Modes
1970//
1971// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7
Steven Valdeza4ee74d2016-11-29 13:36:45 -05001972
David Benjamin8c880a22016-12-03 02:20:34 -05001973static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
1974 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04001975 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001976 return 1;
1977 }
1978
1979 CBB contents, ke_modes;
1980 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
1981 !CBB_add_u16_length_prefixed(out, &contents) ||
1982 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
1983 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
1984 return 0;
1985 }
1986
1987 return CBB_flush(out);
1988}
1989
David Benjamin8c880a22016-12-03 02:20:34 -05001990static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09001991 uint8_t *out_alert,
1992 CBS *contents) {
1993 if (contents == NULL) {
1994 return 1;
1995 }
1996
Steven Valdeza833c352016-11-01 13:39:36 -04001997 CBS ke_modes;
1998 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
1999 CBS_len(&ke_modes) == 0 ||
2000 CBS_len(contents) != 0) {
2001 *out_alert = SSL_AD_DECODE_ERROR;
2002 return 0;
2003 }
2004
David Benjaminc11ea9422017-08-29 16:33:21 -04002005 // We only support tickets with PSK_DHE_KE.
David Benjamin17cf2cb2016-12-13 01:07:13 -05002006 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2007 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002008
2009 return 1;
2010}
2011
2012
David Benjaminc11ea9422017-08-29 16:33:21 -04002013// Early Data Indication
2014//
2015// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002016
David Benjamin8c880a22016-12-03 02:20:34 -05002017static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002018 SSL *const ssl = hs->ssl;
Steven Valdez2d850622017-01-11 11:34:52 -05002019 if (ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04002020 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION ||
Steven Valdez2d850622017-01-11 11:34:52 -05002021 ssl->session->ticket_max_early_data == 0 ||
2022 hs->received_hello_retry_request ||
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05002023 !ssl->cert->enable_early_data) {
Steven Valdez2d850622017-01-11 11:34:52 -05002024 return 1;
2025 }
2026
David Benjaminfd45ee72017-08-31 14:49:09 -04002027 hs->early_data_offered = true;
Steven Valdez2d850622017-01-11 11:34:52 -05002028
2029 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2030 !CBB_add_u16(out, 0) ||
2031 !CBB_flush(out)) {
2032 return 0;
2033 }
2034
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002035 return 1;
2036}
2037
Steven Valdez2d850622017-01-11 11:34:52 -05002038static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002039 uint8_t *out_alert, CBS *contents) {
2040 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002041 if (contents == NULL) {
2042 return 1;
2043 }
2044
2045 if (CBS_len(contents) != 0) {
2046 *out_alert = SSL_AD_DECODE_ERROR;
2047 return 0;
2048 }
2049
Steven Valdez2d850622017-01-11 11:34:52 -05002050 if (!ssl->s3->session_reused) {
2051 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2052 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2053 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002054 }
Steven Valdez2d850622017-01-11 11:34:52 -05002055
2056 ssl->early_data_accepted = 1;
2057 return 1;
2058}
2059
2060static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2061 uint8_t *out_alert, CBS *contents) {
2062 SSL *const ssl = hs->ssl;
2063 if (contents == NULL ||
2064 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2065 return 1;
2066 }
2067
2068 if (CBS_len(contents) != 0) {
2069 *out_alert = SSL_AD_DECODE_ERROR;
2070 return 0;
2071 }
2072
David Benjaminfd45ee72017-08-31 14:49:09 -04002073 hs->early_data_offered = true;
Steven Valdez2d850622017-01-11 11:34:52 -05002074 return 1;
2075}
2076
2077static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2078 if (!hs->ssl->early_data_accepted) {
2079 return 1;
2080 }
2081
2082 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2083 !CBB_add_u16(out, 0) ||
2084 !CBB_flush(out)) {
2085 return 0;
2086 }
2087
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002088 return 1;
2089}
2090
2091
David Benjaminc11ea9422017-08-29 16:33:21 -04002092// Key Share
2093//
2094// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5
Steven Valdez143e8b32016-07-11 13:19:03 -04002095
David Benjamin8c880a22016-12-03 02:20:34 -05002096static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2097 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002098 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002099 return 1;
2100 }
2101
2102 CBB contents, kse_bytes;
2103 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2104 !CBB_add_u16_length_prefixed(out, &contents) ||
2105 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2106 return 0;
2107 }
2108
David Benjamin8c880a22016-12-03 02:20:34 -05002109 uint16_t group_id = hs->retry_group;
2110 if (hs->received_hello_retry_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002111 // We received a HelloRetryRequest without a new curve, so there is no new
2112 // share to append. Leave |hs->key_share| as-is.
Steven Valdeza833c352016-11-01 13:39:36 -04002113 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002114 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2115 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002116 return 0;
2117 }
David Benjamin8c880a22016-12-03 02:20:34 -05002118 OPENSSL_free(hs->key_share_bytes);
2119 hs->key_share_bytes = NULL;
2120 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002121 if (group_id == 0) {
2122 return CBB_flush(out);
2123 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002124 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04002125 // Add a fake group. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002126 if (ssl->ctx->grease_enabled &&
2127 (!CBB_add_u16(&kse_bytes,
2128 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2129 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2130 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2131 return 0;
2132 }
2133
David Benjaminc11ea9422017-08-29 16:33:21 -04002134 // Predict the most preferred group.
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002135 const uint16_t *groups;
2136 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002137 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002138 if (groups_len == 0) {
2139 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2140 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002141 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002142
2143 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002144 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002145
David Benjaminc642aca2017-07-19 23:28:43 -04002146 hs->key_share = SSLKeyShare::Create(group_id);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002147 CBB key_exchange;
David Benjaminc642aca2017-07-19 23:28:43 -04002148 if (!hs->key_share ||
2149 !CBB_add_u16(&kse_bytes, group_id) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002150 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjaminc642aca2017-07-19 23:28:43 -04002151 !hs->key_share->Offer(&key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002152 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002153 return 0;
2154 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002155
David Benjamin8c880a22016-12-03 02:20:34 -05002156 if (!hs->received_hello_retry_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002157 // Save the contents of the extension to repeat it in the second
2158 // ClientHello.
David Benjamin8c880a22016-12-03 02:20:34 -05002159 hs->key_share_bytes_len = CBB_len(&kse_bytes);
David Benjamin81678aa2017-07-12 22:43:42 -04002160 hs->key_share_bytes =
2161 (uint8_t *)BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
David Benjamin8c880a22016-12-03 02:20:34 -05002162 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002163 return 0;
2164 }
2165 }
2166
Steven Valdez143e8b32016-07-11 13:19:03 -04002167 return CBB_flush(out);
2168}
2169
David Benjamin8baf9632016-11-17 17:11:16 +09002170int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002171 size_t *out_secret_len,
2172 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002173 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002174 uint16_t group_id;
2175 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002176 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2177 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002178 *out_alert = SSL_AD_DECODE_ERROR;
2179 return 0;
2180 }
2181
David Benjaminc642aca2017-07-19 23:28:43 -04002182 if (hs->key_share->GroupID() != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002183 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2184 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2185 return 0;
2186 }
2187
David Benjaminc642aca2017-07-19 23:28:43 -04002188 if (!hs->key_share->Finish(out_secret, out_secret_len, out_alert,
2189 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002190 *out_alert = SSL_AD_INTERNAL_ERROR;
2191 return 0;
2192 }
2193
David Benjamin45738dd2017-02-09 20:01:26 -05002194 hs->new_session->group_id = group_id;
David Benjaminc642aca2017-07-19 23:28:43 -04002195 hs->key_share.reset();
Steven Valdez143e8b32016-07-11 13:19:03 -04002196 return 1;
2197}
2198
David Benjamin74795b32017-08-31 15:13:12 -04002199int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002200 uint8_t **out_secret,
2201 size_t *out_secret_len,
2202 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002203 uint16_t group_id;
2204 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002205 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002206 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2207 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2208 return 0;
2209 }
2210
2211 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002212 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002213 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002214 return 0;
2215 }
2216
David Benjaminc11ea9422017-08-29 16:33:21 -04002217 // Find the corresponding key share.
David Benjamin74795b32017-08-31 15:13:12 -04002218 bool found = false;
David Benjamin7e1f9842016-09-20 19:24:40 -04002219 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002220 while (CBS_len(&key_shares) > 0) {
2221 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002222 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002223 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002224 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2225 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002226 return 0;
2227 }
2228
David Benjamin7e1f9842016-09-20 19:24:40 -04002229 if (id == group_id) {
2230 if (found) {
2231 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2232 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2233 return 0;
2234 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002235
David Benjamin74795b32017-08-31 15:13:12 -04002236 found = true;
David Benjamin7e1f9842016-09-20 19:24:40 -04002237 peer_key = peer_key_tmp;
David Benjaminc11ea9422017-08-29 16:33:21 -04002238 // Continue parsing the structure to keep peers honest.
Steven Valdez143e8b32016-07-11 13:19:03 -04002239 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002240 }
2241
David Benjamin7e1f9842016-09-20 19:24:40 -04002242 if (!found) {
David Benjamin74795b32017-08-31 15:13:12 -04002243 *out_found = false;
David Benjamin7e1f9842016-09-20 19:24:40 -04002244 *out_secret = NULL;
2245 *out_secret_len = 0;
2246 return 1;
2247 }
2248
David Benjaminc11ea9422017-08-29 16:33:21 -04002249 // Compute the DH secret.
David Benjamin7e1f9842016-09-20 19:24:40 -04002250 uint8_t *secret = NULL;
2251 size_t secret_len;
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);
2254 if (!key_share ||
2255 !CBB_init(public_key.get(), 32) ||
2256 !key_share->Accept(public_key.get(), &secret, &secret_len, out_alert,
2257 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)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002260 OPENSSL_free(secret);
Steven Valdez803c77a2016-09-06 14:13:43 -04002261 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002262 return 0;
2263 }
2264
David Benjamin7e1f9842016-09-20 19:24:40 -04002265 *out_secret = secret;
2266 *out_secret_len = secret_len;
David Benjamin74795b32017-08-31 15:13:12 -04002267 *out_found = true;
Steven Valdez5440fe02016-07-18 12:40:30 -04002268 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002269}
2270
David Benjamin8baf9632016-11-17 17:11:16 +09002271int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002272 uint16_t group_id;
2273 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002274 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002275 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2276 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2277 !CBB_add_u16(&kse_bytes, group_id) ||
2278 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002279 !CBB_add_bytes(&public_key, hs->ecdh_public_key,
2280 hs->ecdh_public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002281 !CBB_flush(out)) {
2282 return 0;
2283 }
2284
David Benjaminbf833c32017-03-30 15:45:21 -05002285 OPENSSL_free(hs->ecdh_public_key);
2286 hs->ecdh_public_key = NULL;
2287 hs->ecdh_public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002288
David Benjamin45738dd2017-02-09 20:01:26 -05002289 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002290 return 1;
2291}
2292
2293
David Benjaminc11ea9422017-08-29 16:33:21 -04002294// Supported Versions
2295//
2296// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1
Steven Valdezfdd10992016-09-15 16:27:05 -04002297
David Benjamin8c880a22016-12-03 02:20:34 -05002298static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2299 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002300 if (hs->max_version <= TLS1_2_VERSION) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002301 return 1;
2302 }
2303
2304 CBB contents, versions;
2305 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2306 !CBB_add_u16_length_prefixed(out, &contents) ||
2307 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2308 return 0;
2309 }
2310
David Benjaminc11ea9422017-08-29 16:33:21 -04002311 // Add a fake version. See draft-davidben-tls-grease-01.
David Benjamind9791bf2016-09-27 16:39:52 -04002312 if (ssl->ctx->grease_enabled &&
2313 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2314 return 0;
2315 }
2316
Steven Valdez8f36c512017-06-20 10:55:02 -04002317 if (!ssl_add_supported_versions(hs, &versions) ||
2318 !CBB_flush(out)) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002319 return 0;
2320 }
2321
2322 return 1;
2323}
2324
2325
David Benjaminc11ea9422017-08-29 16:33:21 -04002326// Cookie
2327//
2328// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2
David Benjamin3baa6e12016-10-07 21:10:38 -04002329
David Benjamin8c880a22016-12-03 02:20:34 -05002330static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2331 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002332 return 1;
2333 }
2334
2335 CBB contents, cookie;
2336 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2337 !CBB_add_u16_length_prefixed(out, &contents) ||
2338 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002339 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002340 !CBB_flush(out)) {
2341 return 0;
2342 }
2343
David Benjaminc11ea9422017-08-29 16:33:21 -04002344 // The cookie is no longer needed in memory.
David Benjamin8c880a22016-12-03 02:20:34 -05002345 OPENSSL_free(hs->cookie);
2346 hs->cookie = NULL;
2347 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002348 return 1;
2349}
2350
2351
David Benjaminc11ea9422017-08-29 16:33:21 -04002352// Negotiated Groups
2353//
2354// https://tools.ietf.org/html/rfc4492#section-5.1.2
2355// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4
Adam Langley273d49c2015-07-20 16:38:52 -07002356
David Benjamin8c880a22016-12-03 02:20:34 -05002357static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2358 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002359 CBB contents, groups_bytes;
2360 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002361 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002362 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002363 return 0;
2364 }
2365
David Benjaminc11ea9422017-08-29 16:33:21 -04002366 // Add a fake group. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002367 if (ssl->ctx->grease_enabled &&
2368 !CBB_add_u16(&groups_bytes,
2369 ssl_get_grease_value(ssl, ssl_grease_group))) {
2370 return 0;
2371 }
2372
Steven Valdezce902a92016-05-17 11:47:53 -04002373 const uint16_t *groups;
2374 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002375 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002376
David Benjamin54091232016-09-05 12:47:25 -04002377 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002378 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002379 return 0;
2380 }
2381 }
2382
2383 return CBB_flush(out);
2384}
2385
David Benjamin8c880a22016-12-03 02:20:34 -05002386static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2387 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002388 CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002389 // This extension is not expected to be echoed by servers in TLS 1.2, but some
2390 // BigIP servers send it nonetheless, so do not enforce this.
Adam Langley273d49c2015-07-20 16:38:52 -07002391 return 1;
2392}
2393
David Benjamin8c880a22016-12-03 02:20:34 -05002394static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2395 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002396 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002397 if (contents == NULL) {
2398 return 1;
2399 }
2400
Steven Valdezce902a92016-05-17 11:47:53 -04002401 CBS supported_group_list;
2402 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2403 CBS_len(&supported_group_list) == 0 ||
2404 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002405 CBS_len(contents) != 0) {
2406 return 0;
2407 }
2408
David Benjamin8c880a22016-12-03 02:20:34 -05002409 hs->peer_supported_group_list =
David Benjamin81678aa2017-07-12 22:43:42 -04002410 (uint16_t *)OPENSSL_malloc(CBS_len(&supported_group_list));
David Benjamin8c880a22016-12-03 02:20:34 -05002411 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002412 *out_alert = SSL_AD_INTERNAL_ERROR;
2413 return 0;
2414 }
2415
Steven Valdezce902a92016-05-17 11:47:53 -04002416 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002417 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002418 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002419 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002420 goto err;
2421 }
2422 }
2423
Steven Valdezce902a92016-05-17 11:47:53 -04002424 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002425 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002426
2427 return 1;
2428
2429err:
David Benjamin8c880a22016-12-03 02:20:34 -05002430 OPENSSL_free(hs->peer_supported_group_list);
2431 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002432 *out_alert = SSL_AD_INTERNAL_ERROR;
2433 return 0;
2434}
2435
David Benjamin8c880a22016-12-03 02:20:34 -05002436static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002437 // Servers don't echo this extension.
Adam Langley273d49c2015-07-20 16:38:52 -07002438 return 1;
2439}
2440
2441
David Benjaminc11ea9422017-08-29 16:33:21 -04002442// kExtensions contains all the supported extensions.
Adam Langley614c66a2015-06-12 15:26:58 -07002443static const struct tls_extension kExtensions[] = {
2444 {
Adam Langley5021b222015-06-12 18:27:58 -07002445 TLSEXT_TYPE_renegotiate,
2446 NULL,
2447 ext_ri_add_clienthello,
2448 ext_ri_parse_serverhello,
2449 ext_ri_parse_clienthello,
2450 ext_ri_add_serverhello,
2451 },
2452 {
Adam Langley614c66a2015-06-12 15:26:58 -07002453 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002454 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002455 ext_sni_add_clienthello,
2456 ext_sni_parse_serverhello,
2457 ext_sni_parse_clienthello,
2458 ext_sni_add_serverhello,
2459 },
Adam Langley0a056712015-07-01 15:03:33 -07002460 {
2461 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002462 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002463 ext_ems_add_clienthello,
2464 ext_ems_parse_serverhello,
2465 ext_ems_parse_clienthello,
2466 ext_ems_add_serverhello,
2467 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002468 {
2469 TLSEXT_TYPE_session_ticket,
2470 NULL,
2471 ext_ticket_add_clienthello,
2472 ext_ticket_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04002473 // Ticket extension client parsing is handled in ssl_session.c
Steven Valdez6b8509a2016-07-12 13:38:32 -04002474 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002475 ext_ticket_add_serverhello,
2476 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002477 {
2478 TLSEXT_TYPE_signature_algorithms,
2479 NULL,
2480 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002481 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002482 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002483 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002484 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002485 {
2486 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002487 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002488 ext_ocsp_add_clienthello,
2489 ext_ocsp_parse_serverhello,
2490 ext_ocsp_parse_clienthello,
2491 ext_ocsp_add_serverhello,
2492 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002493 {
2494 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002495 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002496 ext_npn_add_clienthello,
2497 ext_npn_parse_serverhello,
2498 ext_npn_parse_clienthello,
2499 ext_npn_add_serverhello,
2500 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002501 {
2502 TLSEXT_TYPE_certificate_timestamp,
2503 NULL,
2504 ext_sct_add_clienthello,
2505 ext_sct_parse_serverhello,
2506 ext_sct_parse_clienthello,
2507 ext_sct_add_serverhello,
2508 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002509 {
2510 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002511 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002512 ext_alpn_add_clienthello,
2513 ext_alpn_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04002514 // ALPN is negotiated late in |ssl_negotiate_alpn|.
David Benjamin9ef31f02016-10-31 18:01:13 -04002515 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002516 ext_alpn_add_serverhello,
2517 },
Adam Langley49c7af12015-07-10 14:33:46 -07002518 {
2519 TLSEXT_TYPE_channel_id,
2520 ext_channel_id_init,
2521 ext_channel_id_add_clienthello,
2522 ext_channel_id_parse_serverhello,
2523 ext_channel_id_parse_clienthello,
2524 ext_channel_id_add_serverhello,
2525 },
Adam Langley391250d2015-07-15 19:06:07 -07002526 {
2527 TLSEXT_TYPE_srtp,
2528 ext_srtp_init,
2529 ext_srtp_add_clienthello,
2530 ext_srtp_parse_serverhello,
2531 ext_srtp_parse_clienthello,
2532 ext_srtp_add_serverhello,
2533 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002534 {
2535 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002536 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002537 ext_ec_point_add_clienthello,
2538 ext_ec_point_parse_serverhello,
2539 ext_ec_point_parse_clienthello,
2540 ext_ec_point_add_serverhello,
2541 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002542 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002543 TLSEXT_TYPE_key_share,
2544 NULL,
2545 ext_key_share_add_clienthello,
2546 forbid_parse_serverhello,
2547 ignore_parse_clienthello,
2548 dont_add_serverhello,
2549 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002550 {
Steven Valdeza833c352016-11-01 13:39:36 -04002551 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002552 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002553 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002554 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002555 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002556 dont_add_serverhello,
2557 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002558 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002559 TLSEXT_TYPE_early_data,
2560 NULL,
2561 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002562 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002563 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002564 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002565 },
2566 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002567 TLSEXT_TYPE_supported_versions,
2568 NULL,
2569 ext_supported_versions_add_clienthello,
2570 forbid_parse_serverhello,
2571 ignore_parse_clienthello,
2572 dont_add_serverhello,
2573 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002574 {
2575 TLSEXT_TYPE_cookie,
2576 NULL,
2577 ext_cookie_add_clienthello,
2578 forbid_parse_serverhello,
2579 ignore_parse_clienthello,
2580 dont_add_serverhello,
2581 },
David Benjaminc11ea9422017-08-29 16:33:21 -04002582 // The final extension must be non-empty. WebSphere Application Server 7.0 is
2583 // intolerant to the last extension being zero-length. See
2584 // https://crbug.com/363583.
Adam Langley273d49c2015-07-20 16:38:52 -07002585 {
Steven Valdezce902a92016-05-17 11:47:53 -04002586 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002587 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002588 ext_supported_groups_add_clienthello,
2589 ext_supported_groups_parse_serverhello,
2590 ext_supported_groups_parse_clienthello,
2591 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002592 },
Adam Langley614c66a2015-06-12 15:26:58 -07002593};
2594
2595#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2596
David Benjamina3d76d02017-07-14 19:36:07 -04002597static_assert(kNumExtensions <=
2598 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
2599 "too many extensions for sent bitset");
2600static_assert(kNumExtensions <=
2601 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2602 "too many extensions for received bitset");
Adam Langley4cfa96b2015-07-01 11:56:55 -07002603
Adam Langley614c66a2015-06-12 15:26:58 -07002604static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2605 uint16_t value) {
2606 unsigned i;
2607 for (i = 0; i < kNumExtensions; i++) {
2608 if (kExtensions[i].value == value) {
2609 *out_index = i;
2610 return &kExtensions[i];
2611 }
2612 }
2613
2614 return NULL;
2615}
2616
David Benjamin8c880a22016-12-03 02:20:34 -05002617int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2618 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04002619 // Don't add extensions for SSLv3 unless doing secure renegotiation.
David Benjaminf04c2e92016-12-06 13:35:25 -05002620 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002621 !ssl->s3->send_connection_binding) {
2622 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002623 }
Adam Langley95c29f32014-06-20 12:00:00 -07002624
David Benjamine8d53502015-10-10 14:13:23 -04002625 CBB extensions;
2626 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002627 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2628 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002629 }
Adam Langley95c29f32014-06-20 12:00:00 -07002630
David Benjamin8c880a22016-12-03 02:20:34 -05002631 hs->extensions.sent = 0;
2632 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002633
David Benjamin54091232016-09-05 12:47:25 -04002634 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002635 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002636 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002637 }
2638 }
Adam Langley95c29f32014-06-20 12:00:00 -07002639
David Benjamin65ac9972016-09-02 21:35:25 -04002640 uint16_t grease_ext1 = 0;
2641 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002642 // Add a fake empty extension. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002643 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2644 if (!CBB_add_u16(&extensions, grease_ext1) ||
2645 !CBB_add_u16(&extensions, 0 /* zero length */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002646 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2647 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002648 }
2649 }
2650
David Benjamin54091232016-09-05 12:47:25 -04002651 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002652 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002653 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002654 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002655 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin81678aa2017-07-12 22:43:42 -04002656 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002657 }
Adam Langley95c29f32014-06-20 12:00:00 -07002658
Adam Langley33ad2b52015-07-20 17:43:53 -07002659 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002660 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002661 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002662 }
Adam Langley75712922014-10-10 16:23:43 -07002663
David Benjamin2bd19172016-11-17 16:47:15 +09002664 if (!custom_ext_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002665 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2666 return 0;
Adam Langley09505632015-07-30 18:10:13 -07002667 }
2668
David Benjamin65ac9972016-09-02 21:35:25 -04002669 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002670 // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002671 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2672
David Benjaminc11ea9422017-08-29 16:33:21 -04002673 // The two fake extensions must not have the same value. GREASE values are
2674 // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2675 // one.
David Benjamin65ac9972016-09-02 21:35:25 -04002676 if (grease_ext1 == grease_ext2) {
2677 grease_ext2 ^= 0x1010;
2678 }
2679
2680 if (!CBB_add_u16(&extensions, grease_ext2) ||
2681 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2682 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002683 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2684 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002685 }
2686 }
2687
David Benjamince079fd2016-08-02 16:22:34 -04002688 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002689 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002690 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002691 if (header_len > 0xff && header_len < 0x200) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002692 // Add padding to workaround bugs in F5 terminators. See RFC 7685.
2693 //
2694 // NB: because this code works out the length of all existing extensions
2695 // it MUST always appear last.
David Benjamin0a968592015-07-21 22:06:19 -04002696 size_t padding_len = 0x200 - header_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04002697 // Extensions take at least four bytes to encode. Always include at least
2698 // one byte of data if including the extension. WebSphere Application
2699 // Server 7.0 is intolerant to the last extension being zero-length. See
2700 // https://crbug.com/363583.
Adam Langleyfcf25832014-12-18 17:42:32 -08002701 if (padding_len >= 4 + 1) {
2702 padding_len -= 4;
2703 } else {
2704 padding_len = 1;
2705 }
Adam Langley95c29f32014-06-20 12:00:00 -07002706
Adam Langley33ad2b52015-07-20 17:43:53 -07002707 uint8_t *padding_bytes;
2708 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2709 !CBB_add_u16(&extensions, padding_len) ||
2710 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002711 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2712 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002713 }
Adam Langley75712922014-10-10 16:23:43 -07002714
David Benjamin17cf2cb2016-12-13 01:07:13 -05002715 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002716 }
2717 }
Adam Langley75712922014-10-10 16:23:43 -07002718
David Benjaminc11ea9422017-08-29 16:33:21 -04002719 // The PSK extension must be last, including after the padding.
David Benjamin8c880a22016-12-03 02:20:34 -05002720 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002721 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2722 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -04002723 }
2724
David Benjaminc11ea9422017-08-29 16:33:21 -04002725 // Discard empty extensions blocks.
David Benjamina01deee2015-12-08 18:56:31 -05002726 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002727 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002728 }
2729
David Benjamine8d53502015-10-10 14:13:23 -04002730 return CBB_flush(out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002731}
Adam Langley95c29f32014-06-20 12:00:00 -07002732
David Benjamin8c880a22016-12-03 02:20:34 -05002733int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2734 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002735 CBB extensions;
2736 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002737 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002738 }
2739
David Benjamin8c880a22016-12-03 02:20:34 -05002740 for (unsigned i = 0; i < kNumExtensions; i++) {
2741 if (!(hs->extensions.received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002742 // Don't send extensions that were not received.
Adam Langley614c66a2015-06-12 15:26:58 -07002743 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002744 }
Adam Langley95c29f32014-06-20 12:00:00 -07002745
David Benjamin8c880a22016-12-03 02:20:34 -05002746 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002747 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002748 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002749 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002750 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002751 }
Adam Langley95c29f32014-06-20 12:00:00 -07002752
David Benjamin2bd19172016-11-17 16:47:15 +09002753 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002754 goto err;
2755 }
2756
David Benjaminc11ea9422017-08-29 16:33:21 -04002757 // Discard empty extensions blocks before TLS 1.3.
Steven Valdez143e8b32016-07-11 13:19:03 -04002758 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2759 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002760 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002761 }
2762
David Benjamin56380462015-10-10 14:59:09 -04002763 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002764
2765err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002766 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002767 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002768}
Adam Langley95c29f32014-06-20 12:00:00 -07002769
David Benjamin731058e2016-12-03 23:15:13 -05002770static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2771 const SSL_CLIENT_HELLO *client_hello,
2772 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002773 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002774 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002775 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002776 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002777 }
2778 }
2779
David Benjamin8c880a22016-12-03 02:20:34 -05002780 hs->extensions.received = 0;
2781 hs->custom_extensions.received = 0;
David Benjamine14ff062016-08-09 16:21:24 -04002782 CBS extensions;
2783 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2784 while (CBS_len(&extensions) != 0) {
2785 uint16_t type;
2786 CBS extension;
2787
David Benjaminc11ea9422017-08-29 16:33:21 -04002788 // Decode the next extension.
David Benjamine14ff062016-08-09 16:21:24 -04002789 if (!CBS_get_u16(&extensions, &type) ||
2790 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002791 *out_alert = SSL_AD_DECODE_ERROR;
2792 return 0;
2793 }
Adam Langley95c29f32014-06-20 12:00:00 -07002794
David Benjaminc11ea9422017-08-29 16:33:21 -04002795 // RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2796 // ambiguous. Ignore all but the renegotiation_info extension.
David Benjamine14ff062016-08-09 16:21:24 -04002797 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2798 continue;
2799 }
Adam Langley95c29f32014-06-20 12:00:00 -07002800
David Benjamine14ff062016-08-09 16:21:24 -04002801 unsigned ext_index;
2802 const struct tls_extension *const ext =
2803 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002804
David Benjamine14ff062016-08-09 16:21:24 -04002805 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002806 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002807 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002808 return 0;
2809 }
David Benjamine14ff062016-08-09 16:21:24 -04002810 continue;
2811 }
2812
David Benjamin8c880a22016-12-03 02:20:34 -05002813 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002814 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002815 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002816 *out_alert = alert;
2817 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002818 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002819 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002820 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002821 }
Adam Langley75712922014-10-10 16:23:43 -07002822
David Benjamin1deb41b2016-08-09 19:36:38 -04002823 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002824 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002825 continue;
2826 }
2827
2828 CBS *contents = NULL, fake_contents;
2829 static const uint8_t kFakeRenegotiateExtension[] = {0};
2830 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2831 ssl_client_cipher_list_contains_cipher(client_hello,
2832 SSL3_CK_SCSV & 0xffff)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002833 // The renegotiation SCSV was received so pretend that we received a
2834 // renegotiation extension.
David Benjamin1deb41b2016-08-09 19:36:38 -04002835 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2836 sizeof(kFakeRenegotiateExtension));
2837 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002838 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002839 }
2840
David Benjaminc11ea9422017-08-29 16:33:21 -04002841 // Extension wasn't observed so call the callback with a NULL
2842 // parameter.
David Benjamin1deb41b2016-08-09 19:36:38 -04002843 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002844 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002845 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002846 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002847 *out_alert = alert;
2848 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002849 }
2850 }
2851
Adam Langleyfcf25832014-12-18 17:42:32 -08002852 return 1;
2853}
Adam Langley95c29f32014-06-20 12:00:00 -07002854
David Benjamin731058e2016-12-03 23:15:13 -05002855int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2856 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002857 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002858 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002859 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002860 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002861 return 0;
2862 }
Adam Langley95c29f32014-06-20 12:00:00 -07002863
David Benjamin8c880a22016-12-03 02:20:34 -05002864 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002865 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002866 return 0;
2867 }
Adam Langley95c29f32014-06-20 12:00:00 -07002868
Adam Langleyfcf25832014-12-18 17:42:32 -08002869 return 1;
2870}
Adam Langley95c29f32014-06-20 12:00:00 -07002871
David Benjamin8c880a22016-12-03 02:20:34 -05002872static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2873 int *out_alert) {
2874 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04002875 // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty.
Steven Valdez143e8b32016-07-11 13:19:03 -04002876 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2877 return 1;
2878 }
Adam Langley614c66a2015-06-12 15:26:58 -07002879
David Benjaminc11ea9422017-08-29 16:33:21 -04002880 // Decode the extensions block and check it is valid.
Steven Valdez143e8b32016-07-11 13:19:03 -04002881 CBS extensions;
2882 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2883 !tls1_check_duplicate_extensions(&extensions)) {
2884 *out_alert = SSL_AD_DECODE_ERROR;
2885 return 0;
2886 }
2887
2888 uint32_t received = 0;
2889 while (CBS_len(&extensions) != 0) {
2890 uint16_t type;
2891 CBS extension;
2892
David Benjaminc11ea9422017-08-29 16:33:21 -04002893 // Decode the next extension.
Steven Valdez143e8b32016-07-11 13:19:03 -04002894 if (!CBS_get_u16(&extensions, &type) ||
2895 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002896 *out_alert = SSL_AD_DECODE_ERROR;
2897 return 0;
2898 }
Adam Langley95c29f32014-06-20 12:00:00 -07002899
Steven Valdez143e8b32016-07-11 13:19:03 -04002900 unsigned ext_index;
2901 const struct tls_extension *const ext =
2902 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002903
Steven Valdez143e8b32016-07-11 13:19:03 -04002904 if (ext == NULL) {
David Benjaminfd45ee72017-08-31 14:49:09 -04002905 hs->received_custom_extension = true;
David Benjamin2bd19172016-11-17 16:47:15 +09002906 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002907 return 0;
2908 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002909 continue;
2910 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002911
David Benjamina3d76d02017-07-14 19:36:07 -04002912 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
2913 "too many bits");
David Benjamin5db7c9b2017-01-24 16:17:03 -05002914
David Benjamin8c880a22016-12-03 02:20:34 -05002915 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002916 type != TLSEXT_TYPE_renegotiate) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002917 // If the extension was never sent then it is illegal, except for the
2918 // renegotiation extension which, in SSL 3.0, is signaled via SCSV.
Steven Valdez143e8b32016-07-11 13:19:03 -04002919 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2920 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002921 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002922 return 0;
2923 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002924
Steven Valdez143e8b32016-07-11 13:19:03 -04002925 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002926
Steven Valdez143e8b32016-07-11 13:19:03 -04002927 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002928 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002929 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002930 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04002931 *out_alert = alert;
2932 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002933 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002934 }
Adam Langley95c29f32014-06-20 12:00:00 -07002935
David Benjamin54091232016-09-05 12:47:25 -04002936 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002937 if (!(received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002938 // Extension wasn't observed so call the callback with a NULL
2939 // parameter.
Adam Langley614c66a2015-06-12 15:26:58 -07002940 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002941 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002942 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002943 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002944 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002945 return 0;
2946 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002947 }
2948 }
Adam Langley95c29f32014-06-20 12:00:00 -07002949
Adam Langleyfcf25832014-12-18 17:42:32 -08002950 return 1;
2951}
Adam Langley95c29f32014-06-20 12:00:00 -07002952
David Benjamin8c880a22016-12-03 02:20:34 -05002953static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
2954 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002955 int ret = SSL_TLSEXT_ERR_NOACK;
2956 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002957
David Benjamin78f8aab2016-03-10 16:33:58 -05002958 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002959 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002960 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05002961 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
2962 ret = ssl->session_ctx->tlsext_servername_callback(
2963 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002964 }
Adam Langley95c29f32014-06-20 12:00:00 -07002965
Adam Langleyfcf25832014-12-18 17:42:32 -08002966 switch (ret) {
2967 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002968 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002969 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002970
Adam Langleyfcf25832014-12-18 17:42:32 -08002971 case SSL_TLSEXT_ERR_NOACK:
David Benjaminfd45ee72017-08-31 14:49:09 -04002972 hs->should_ack_sni = false;
Adam Langleyfcf25832014-12-18 17:42:32 -08002973 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002974
Adam Langleyfcf25832014-12-18 17:42:32 -08002975 default:
2976 return 1;
2977 }
2978}
Adam Langleyed8270a2014-09-02 13:52:56 -07002979
David Benjamin8c880a22016-12-03 02:20:34 -05002980int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
2981 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002982 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002983 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002984 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002985 return 0;
2986 }
2987
Adam Langleyfcf25832014-12-18 17:42:32 -08002988 return 1;
2989}
Adam Langley95c29f32014-06-20 12:00:00 -07002990
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002991static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
2992 uint8_t **out, size_t *out_len, EVP_CIPHER_CTX *cipher_ctx,
2993 HMAC_CTX *hmac_ctx, const uint8_t *ticket, size_t ticket_len) {
2994 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002995
David Benjaminc11ea9422017-08-29 16:33:21 -04002996 // Check the MAC at the end of the ticket.
David Benjamine3aa1d92015-06-16 15:34:50 -04002997 uint8_t mac[EVP_MAX_MD_SIZE];
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002998 size_t mac_len = HMAC_size(hmac_ctx);
David Benjamine3aa1d92015-06-16 15:34:50 -04002999 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003000 // The ticket must be large enough for key name, IV, data, and MAC.
David Benjamin81678aa2017-07-12 22:43:42 -04003001 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003002 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003003 HMAC_Update(hmac_ctx, ticket, ticket_len - mac_len);
3004 HMAC_Final(hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003005 int mac_ok =
3006 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3007#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3008 mac_ok = 1;
3009#endif
3010 if (!mac_ok) {
David Benjamin81678aa2017-07-12 22:43:42 -04003011 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003012 }
3013
David Benjaminc11ea9422017-08-29 16:33:21 -04003014 // Decrypt the session data.
David Benjamine3aa1d92015-06-16 15:34:50 -04003015 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3016 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3017 mac_len;
David Benjamin86e95b82017-07-18 16:34:25 -04003018 UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len));
David Benjamin81678aa2017-07-12 22:43:42 -04003019 if (!plaintext) {
3020 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08003021 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003022 size_t plaintext_len;
3023#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin81678aa2017-07-12 22:43:42 -04003024 OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003025 plaintext_len = ciphertext_len;
3026#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003027 if (ciphertext_len >= INT_MAX) {
David Benjamin81678aa2017-07-12 22:43:42 -04003028 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003029 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003030 int len1, len2;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003031 if (!EVP_DecryptUpdate(cipher_ctx, plaintext.get(), &len1, ciphertext,
David Benjamine3aa1d92015-06-16 15:34:50 -04003032 (int)ciphertext_len) ||
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003033 !EVP_DecryptFinal_ex(cipher_ctx, plaintext.get() + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003034 ERR_clear_error();
David Benjamin81678aa2017-07-12 22:43:42 -04003035 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003036 }
Adam Langley4c341d02017-03-08 19:33:21 -08003037 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04003038#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003039
David Benjamin81678aa2017-07-12 22:43:42 -04003040 *out = plaintext.release();
Adam Langley4c341d02017-03-08 19:33:21 -08003041 *out_len = plaintext_len;
David Benjamin81678aa2017-07-12 22:43:42 -04003042 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08003043}
3044
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003045static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
David Benjaminfd45ee72017-08-31 14:49:09 -04003046 SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003047 const uint8_t *ticket, size_t ticket_len) {
3048 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3049 ScopedEVP_CIPHER_CTX cipher_ctx;
3050 ScopedHMAC_CTX hmac_ctx;
3051 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3052 int cb_ret = ssl->session_ctx->tlsext_ticket_key_cb(
3053 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(),
3054 hmac_ctx.get(), 0 /* decrypt */);
3055 if (cb_ret < 0) {
3056 return ssl_ticket_aead_error;
3057 } else if (cb_ret == 0) {
3058 return ssl_ticket_aead_ignore_ticket;
3059 } else if (cb_ret == 2) {
David Benjaminfd45ee72017-08-31 14:49:09 -04003060 *out_renew_ticket = true;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003061 } else {
3062 assert(cb_ret == 1);
3063 }
3064 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3065 hmac_ctx.get(), ticket, ticket_len);
3066}
3067
3068static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
3069 SSL *ssl, uint8_t **out, size_t *out_len, const uint8_t *ticket,
3070 size_t ticket_len) {
3071 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3072 SSL_CTX *ctx = ssl->session_ctx;
3073
David Benjaminc11ea9422017-08-29 16:33:21 -04003074 // Rotate the ticket key if necessary.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003075 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
3076 return ssl_ticket_aead_error;
3077 }
3078
David Benjaminc11ea9422017-08-29 16:33:21 -04003079 // Pick the matching ticket key and decrypt.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003080 ScopedEVP_CIPHER_CTX cipher_ctx;
3081 ScopedHMAC_CTX hmac_ctx;
3082 {
3083 MutexReadLock lock(&ctx->lock);
3084 const tlsext_ticket_key *key;
3085 if (ctx->tlsext_ticket_key_current &&
3086 !OPENSSL_memcmp(ctx->tlsext_ticket_key_current->name, ticket,
3087 SSL_TICKET_KEY_NAME_LEN)) {
3088 key = ctx->tlsext_ticket_key_current;
3089 } else if (ctx->tlsext_ticket_key_prev &&
3090 !OPENSSL_memcmp(ctx->tlsext_ticket_key_prev->name, ticket,
3091 SSL_TICKET_KEY_NAME_LEN)) {
3092 key = ctx->tlsext_ticket_key_prev;
3093 } else {
3094 return ssl_ticket_aead_ignore_ticket;
3095 }
3096 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3097 if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
3098 tlsext_tick_md(), NULL) ||
3099 !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL,
3100 key->aes_key, iv)) {
3101 return ssl_ticket_aead_error;
3102 }
3103 }
3104 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3105 hmac_ctx.get(), ticket, ticket_len);
3106}
3107
Adam Langley4c341d02017-03-08 19:33:21 -08003108static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
David Benjaminfd45ee72017-08-31 14:49:09 -04003109 SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
Adam Langley4c341d02017-03-08 19:33:21 -08003110 const uint8_t *ticket, size_t ticket_len) {
David Benjamin81678aa2017-07-12 22:43:42 -04003111 uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len);
Adam Langley4c341d02017-03-08 19:33:21 -08003112 if (plaintext == NULL) {
3113 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3114 return ssl_ticket_aead_error;
3115 }
3116
3117 size_t plaintext_len;
3118 const enum ssl_ticket_aead_result_t result =
3119 ssl->session_ctx->ticket_aead_method->open(
3120 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3121
3122 if (result == ssl_ticket_aead_success) {
3123 *out = plaintext;
3124 plaintext = NULL;
3125 *out_len = plaintext_len;
3126 }
3127
3128 OPENSSL_free(plaintext);
3129 return result;
3130}
3131
3132enum ssl_ticket_aead_result_t ssl_process_ticket(
David Benjaminfd45ee72017-08-31 14:49:09 -04003133 SSL *ssl, UniquePtr<SSL_SESSION> *out_session, bool *out_renew_ticket,
Adam Langley4c341d02017-03-08 19:33:21 -08003134 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3135 size_t session_id_len) {
David Benjaminfd45ee72017-08-31 14:49:09 -04003136 *out_renew_ticket = false;
David Benjamin37af90f2017-07-29 01:42:16 -04003137 out_session->reset();
Adam Langley4c341d02017-03-08 19:33:21 -08003138
3139 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3140 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3141 return ssl_ticket_aead_ignore_ticket;
3142 }
3143
3144 uint8_t *plaintext = NULL;
3145 size_t plaintext_len;
3146 enum ssl_ticket_aead_result_t result;
3147 if (ssl->session_ctx->ticket_aead_method != NULL) {
3148 result = ssl_decrypt_ticket_with_method(
3149 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3150 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04003151 // Ensure there is room for the key name and the largest IV
3152 // |tlsext_ticket_key_cb| may try to consume. The real limit may be lower,
3153 // but the maximum IV length should be well under the minimum size for the
3154 // session material and HMAC.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003155 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3156 return ssl_ticket_aead_ignore_ticket;
3157 }
3158 if (ssl->session_ctx->tlsext_ticket_key_cb != NULL) {
3159 result = ssl_decrypt_ticket_with_cb(ssl, &plaintext, &plaintext_len,
3160 out_renew_ticket, ticket, ticket_len);
3161 } else {
3162 result = ssl_decrypt_ticket_with_ticket_keys(
3163 ssl, &plaintext, &plaintext_len, ticket, ticket_len);
3164 }
Adam Langley4c341d02017-03-08 19:33:21 -08003165 }
3166
3167 if (result != ssl_ticket_aead_success) {
3168 return result;
3169 }
3170
David Benjaminc11ea9422017-08-29 16:33:21 -04003171 // Decode the session.
David Benjamin37af90f2017-07-29 01:42:16 -04003172 UniquePtr<SSL_SESSION> session(
3173 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx));
Adam Langley4c341d02017-03-08 19:33:21 -08003174 OPENSSL_free(plaintext);
3175
David Benjamin37af90f2017-07-29 01:42:16 -04003176 if (!session) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003177 ERR_clear_error(); // Don't leave an error on the queue.
Adam Langley4c341d02017-03-08 19:33:21 -08003178 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003179 }
3180
David Benjaminc11ea9422017-08-29 16:33:21 -04003181 // Copy the client's session ID into the new session, to denote the ticket has
3182 // been accepted.
David Benjamin17cf2cb2016-12-13 01:07:13 -05003183 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003184 session->session_id_length = session_id_len;
3185
David Benjamin37af90f2017-07-29 01:42:16 -04003186 *out_session = std::move(session);
Adam Langley4c341d02017-03-08 19:33:21 -08003187 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003188}
Adam Langley95c29f32014-06-20 12:00:00 -07003189
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003190int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003191 // Extension ignored for inappropriate versions
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003192 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003193 return 1;
3194 }
David Benjamincd996942014-07-20 16:23:51 -04003195
David Benjamin0fc37ef2016-08-17 15:29:46 -04003196 OPENSSL_free(hs->peer_sigalgs);
3197 hs->peer_sigalgs = NULL;
3198 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003199
3200 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003201 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003202 return 0;
3203 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003204 num_sigalgs /= 2;
3205
David Benjaminc11ea9422017-08-29 16:33:21 -04003206 // supported_signature_algorithms in the certificate request is
3207 // allowed to be empty.
Steven Valdez0d62f262015-09-04 12:41:04 -04003208 if (num_sigalgs == 0) {
3209 return 1;
3210 }
3211
David Benjaminc11ea9422017-08-29 16:33:21 -04003212 // This multiplication doesn't overflow because sizeof(uint16_t) is two
3213 // and we just divided |num_sigalgs| by two.
David Benjamin81678aa2017-07-12 22:43:42 -04003214 hs->peer_sigalgs = (uint16_t *)OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003215 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003216 return 0;
3217 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003218 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003219
3220 CBS sigalgs;
3221 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003222 for (size_t i = 0; i < num_sigalgs; i++) {
3223 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003224 return 0;
3225 }
3226 }
Adam Langley95c29f32014-06-20 12:00:00 -07003227
Adam Langleyfcf25832014-12-18 17:42:32 -08003228 return 1;
3229}
David Benjaminec2f27d2014-11-13 19:17:25 -05003230
David Benjamina3651382017-04-20 17:49:36 -04003231int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
3232 switch (EVP_PKEY_id(pkey)) {
3233 case EVP_PKEY_RSA:
3234 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3235 return 1;
3236 case EVP_PKEY_EC:
3237 *out = SSL_SIGN_ECDSA_SHA1;
3238 return 1;
3239 default:
3240 return 0;
3241 }
3242}
3243
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003244int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3245 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003246 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003247
David Benjaminc11ea9422017-08-29 16:33:21 -04003248 // Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3249 // handshake.
Steven Valdezf0451ca2016-06-29 13:16:27 -04003250 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin31b0c9b2017-07-20 14:49:15 -04003251 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
David Benjamina3651382017-04-20 17:49:36 -04003252 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3253 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003254 }
David Benjamina3651382017-04-20 17:49:36 -04003255 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003256 }
3257
David Benjamin3ef76972016-10-17 17:59:54 -04003258 const uint16_t *sigalgs = cert->sigalgs;
3259 size_t num_sigalgs = cert->num_sigalgs;
3260 if (sigalgs == NULL) {
3261 sigalgs = kSignSignatureAlgorithms;
3262 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003263 }
3264
David Benjamin0fc37ef2016-08-17 15:29:46 -04003265 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3266 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3267 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003268 // If the client didn't specify any signature_algorithms extension then
3269 // we can assume that it supports SHA1. See
3270 // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
David Benjaminea9a0d52016-07-08 15:52:59 -07003271 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3272 SSL_SIGN_ECDSA_SHA1};
3273 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003274 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003275 }
3276
David Benjamin0fc37ef2016-08-17 15:29:46 -04003277 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003278 uint16_t sigalg = sigalgs[i];
David Benjaminc11ea9422017-08-29 16:33:21 -04003279 // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3280 // negotiated.
David Benjamin1fb125c2016-07-08 18:52:12 -07003281 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjamina232a712017-03-30 15:51:53 -05003282 !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003283 continue;
3284 }
3285
David Benjamin0fc37ef2016-08-17 15:29:46 -04003286 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003287 if (sigalg == peer_sigalgs[j]) {
3288 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003289 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003290 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003291 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003292 }
Adam Langley95c29f32014-06-20 12:00:00 -07003293
David Benjaminea9a0d52016-07-08 15:52:59 -07003294 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3295 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003296}
Adam Langley95c29f32014-06-20 12:00:00 -07003297
David Benjamin7934f082017-08-01 16:32:25 -04003298int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
Steven Valdez908ac192017-01-12 13:17:07 -05003299 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003300 // A Channel ID handshake message is structured to contain multiple
3301 // extensions, but the only one that can be present is Channel ID.
David Benjamin7934f082017-08-01 16:32:25 -04003302 uint16_t extension_type;
3303 CBS channel_id = msg.body, extension;
Nick Harper60a85cb2016-09-23 16:25:11 -07003304 if (!CBS_get_u16(&channel_id, &extension_type) ||
3305 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3306 CBS_len(&channel_id) != 0 ||
3307 extension_type != TLSEXT_TYPE_channel_id ||
3308 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3309 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3310 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3311 return 0;
3312 }
3313
David Benjamin86e95b82017-07-18 16:34:25 -04003314 UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Nick Harper60a85cb2016-09-23 16:25:11 -07003315 if (!p256) {
3316 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3317 return 0;
3318 }
3319
David Benjamin86e95b82017-07-18 16:34:25 -04003320 UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3321 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
David Benjamin81678aa2017-07-12 22:43:42 -04003322 if (!sig || !x || !y) {
3323 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003324 }
3325
3326 const uint8_t *p = CBS_data(&extension);
David Benjamin81678aa2017-07-12 22:43:42 -04003327 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3328 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3329 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3330 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
3331 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003332 }
3333
David Benjamin86e95b82017-07-18 16:34:25 -04003334 UniquePtr<EC_KEY> key(EC_KEY_new());
3335 UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
David Benjamin81678aa2017-07-12 22:43:42 -04003336 if (!key || !point ||
3337 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3338 y.get(), nullptr) ||
3339 !EC_KEY_set_group(key.get(), p256.get()) ||
3340 !EC_KEY_set_public_key(key.get(), point.get())) {
3341 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003342 }
3343
3344 uint8_t digest[EVP_MAX_MD_SIZE];
3345 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003346 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003347 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003348 }
3349
David Benjamin81678aa2017-07-12 22:43:42 -04003350 int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
Nick Harper60a85cb2016-09-23 16:25:11 -07003351#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3352 sig_ok = 1;
3353#endif
3354 if (!sig_ok) {
3355 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3356 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
David Benjamin046bc1f2017-08-31 15:06:42 -04003357 ssl->s3->tlsext_channel_id_valid = false;
David Benjamin81678aa2017-07-12 22:43:42 -04003358 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003359 }
3360
David Benjamin17cf2cb2016-12-13 01:07:13 -05003361 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
David Benjamin81678aa2017-07-12 22:43:42 -04003362 return 1;
Nick Harper60a85cb2016-09-23 16:25:11 -07003363}
3364
Steven Valdez908ac192017-01-12 13:17:07 -05003365int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3366 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003367 uint8_t digest[EVP_MAX_MD_SIZE];
3368 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003369 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003370 return 0;
3371 }
3372
3373 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3374 if (ec_key == NULL) {
3375 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3376 return 0;
3377 }
3378
3379 int ret = 0;
3380 BIGNUM *x = BN_new();
3381 BIGNUM *y = BN_new();
3382 ECDSA_SIG *sig = NULL;
3383 if (x == NULL || y == NULL ||
3384 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3385 EC_KEY_get0_public_key(ec_key),
3386 x, y, NULL)) {
3387 goto err;
3388 }
3389
3390 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3391 if (sig == NULL) {
3392 goto err;
3393 }
3394
3395 CBB child;
3396 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3397 !CBB_add_u16_length_prefixed(cbb, &child) ||
3398 !BN_bn2cbb_padded(&child, 32, x) ||
3399 !BN_bn2cbb_padded(&child, 32, y) ||
3400 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3401 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3402 !CBB_flush(cbb)) {
3403 goto err;
3404 }
3405
3406 ret = 1;
3407
3408err:
3409 BN_free(x);
3410 BN_free(y);
3411 ECDSA_SIG_free(sig);
3412 return ret;
3413}
3414
Steven Valdez908ac192017-01-12 13:17:07 -05003415int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3416 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003417 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3418 uint8_t *msg;
3419 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003420 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003421 ssl_cert_verify_channel_id)) {
3422 return 0;
3423 }
3424 SHA256(msg, msg_len, out);
3425 *out_len = SHA256_DIGEST_LENGTH;
3426 OPENSSL_free(msg);
3427 return 1;
3428 }
3429
Nick Harper95594012016-10-20 14:07:13 -07003430 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003431
Nick Harper95594012016-10-20 14:07:13 -07003432 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003433 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003434 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003435
Steven Valdez87eab492016-06-27 16:34:59 -04003436 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003437 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003438 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003439 if (ssl->session->original_handshake_hash_len == 0) {
3440 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003441 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003442 }
Nick Harper95594012016-10-20 14:07:13 -07003443 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3444 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003445 }
3446
Steven Valdez908ac192017-01-12 13:17:07 -05003447 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3448 size_t hs_hash_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003449 if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003450 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003451 }
Steven Valdez908ac192017-01-12 13:17:07 -05003452 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003453 SHA256_Final(out, &ctx);
3454 *out_len = SHA256_DIGEST_LENGTH;
3455 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003456}
Adam Langley1258b6a2014-06-20 12:00:00 -07003457
David Benjaminc11ea9422017-08-29 16:33:21 -04003458// tls1_record_handshake_hashes_for_channel_id records the current handshake
3459// hashes in |hs->new_session| so that Channel ID resumptions can sign that
3460// data.
Steven Valdez908ac192017-01-12 13:17:07 -05003461int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3462 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003463 // This function should never be called for a resumed session because the
3464 // handshake hashes that we wish to record are for the original, full
3465 // handshake.
Steven Valdez87eab492016-06-27 16:34:59 -04003466 if (ssl->session != NULL) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04003467 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003468 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003469
David Benjamina3d76d02017-07-14 19:36:07 -04003470 static_assert(
David Benjamin45738dd2017-02-09 20:01:26 -05003471 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
David Benjamina3d76d02017-07-14 19:36:07 -04003472 "original_handshake_hash is too small");
Steven Valdez908ac192017-01-12 13:17:07 -05003473
3474 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003475 if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
3476 &digest_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04003477 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003478 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003479
David Benjamina3d76d02017-07-14 19:36:07 -04003480 static_assert(EVP_MAX_MD_SIZE <= 0xff,
3481 "EVP_MAX_MD_SIZE does not fit in uint8_t");
David Benjamin45738dd2017-02-09 20:01:26 -05003482 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003483
Adam Langleyfcf25832014-12-18 17:42:32 -08003484 return 1;
3485}
Nick Harper60a85cb2016-09-23 16:25:11 -07003486
3487int ssl_do_channel_id_callback(SSL *ssl) {
3488 if (ssl->tlsext_channel_id_private != NULL ||
3489 ssl->ctx->channel_id_cb == NULL) {
3490 return 1;
3491 }
3492
3493 EVP_PKEY *key = NULL;
3494 ssl->ctx->channel_id_cb(ssl, &key);
3495 if (key == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003496 // The caller should try again later.
Nick Harper60a85cb2016-09-23 16:25:11 -07003497 return 1;
3498 }
3499
3500 int ret = SSL_set1_tls_channel_id(ssl, key);
3501 EVP_PKEY_free(key);
3502 return ret;
3503}
Adam Langleycfa08c32016-11-17 13:21:27 -08003504
3505int ssl_is_sct_list_valid(const CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003506 // Shallow parse the SCT list for sanity. By the RFC
3507 // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3508 // of the SCTs may be empty.
Adam Langleycfa08c32016-11-17 13:21:27 -08003509 CBS copy = *contents;
3510 CBS sct_list;
3511 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3512 CBS_len(&copy) != 0 ||
3513 CBS_len(&sct_list) == 0) {
3514 return 0;
3515 }
3516
3517 while (CBS_len(&sct_list) > 0) {
3518 CBS sct;
3519 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3520 CBS_len(&sct) == 0) {
3521 return 0;
3522 }
3523 }
3524
3525 return 1;
3526}
David Benjamin86e95b82017-07-18 16:34:25 -04003527
3528} // namespace bssl
3529
3530using namespace bssl;
3531
3532int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
3533 uint16_t extension_type,
3534 const uint8_t **out_data,
3535 size_t *out_len) {
3536 CBS cbs;
3537 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
3538 return 0;
3539 }
3540
3541 *out_data = CBS_data(&cbs);
3542 *out_len = CBS_len(&cbs);
3543 return 1;
3544}
3545
3546void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
3547 ctx->ed25519_enabled = !!enabled;
3548}
3549
3550int SSL_extension_supported(unsigned extension_value) {
3551 uint32_t index;
3552 return extension_value == TLSEXT_TYPE_padding ||
3553 tls_extension_find(&index, extension_value) != NULL;
3554}