blob: 0ff5a09817a2bbb42a0e83689f4a32a801a8f3e7 [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 Benjamin8c880a22016-12-03 02:20:34 -0500622 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700623 if (contents == NULL) {
624 return 1;
625 }
626
627 if (CBS_len(contents) != 0) {
628 return 0;
629 }
630
631 assert(ssl->tlsext_hostname != NULL);
632
Steven Valdez87eab492016-06-27 16:34:59 -0400633 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500634 OPENSSL_free(hs->new_session->tlsext_hostname);
635 hs->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
636 if (!hs->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700637 *out_alert = SSL_AD_INTERNAL_ERROR;
638 return 0;
639 }
640 }
641
642 return 1;
643}
644
David Benjamin8c880a22016-12-03 02:20:34 -0500645static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500646 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700647 if (contents == NULL) {
648 return 1;
649 }
650
David Benjamin9b611e22016-03-03 08:48:30 -0500651 CBS server_name_list, host_name;
652 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700653 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500654 !CBS_get_u8(&server_name_list, &name_type) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400655 // Although the server_name extension was intended to be extensible to
656 // new name types and multiple names, OpenSSL 1.0.x had a bug which meant
657 // different name types will cause an error. Further, RFC 4366 originally
658 // defined syntax inextensibly. RFC 6066 corrected this mistake, but
659 // adding new name types is no longer feasible.
660 //
661 // Act as if the extensibility does not exist to simplify parsing.
David Benjamin9b611e22016-03-03 08:48:30 -0500662 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
663 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700664 CBS_len(contents) != 0) {
665 return 0;
666 }
667
David Benjamin9b611e22016-03-03 08:48:30 -0500668 if (name_type != TLSEXT_NAMETYPE_host_name ||
669 CBS_len(&host_name) == 0 ||
670 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
671 CBS_contains_zero_byte(&host_name)) {
672 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
673 return 0;
674 }
Adam Langley614c66a2015-06-12 15:26:58 -0700675
David Benjaminc11ea9422017-08-29 16:33:21 -0400676 // Copy the hostname as a string.
David Benjamin31b0c9b2017-07-20 14:49:15 -0400677 char *hostname_raw = nullptr;
678 if (!CBS_strdup(&host_name, &hostname_raw)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900679 *out_alert = SSL_AD_INTERNAL_ERROR;
680 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700681 }
David Benjamin31b0c9b2017-07-20 14:49:15 -0400682 hs->hostname.reset(hostname_raw);
Adam Langley614c66a2015-06-12 15:26:58 -0700683
David Benjamin8c880a22016-12-03 02:20:34 -0500684 hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700685 return 1;
686}
687
David Benjamin8c880a22016-12-03 02:20:34 -0500688static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
689 if (hs->ssl->s3->session_reused ||
690 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700691 return 1;
692 }
693
694 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
695 !CBB_add_u16(out, 0 /* length */)) {
696 return 0;
697 }
698
699 return 1;
700}
701
702
David Benjaminc11ea9422017-08-29 16:33:21 -0400703// Renegotiation indication.
704//
705// https://tools.ietf.org/html/rfc5746
Adam Langley5021b222015-06-12 18:27:58 -0700706
David Benjamin8c880a22016-12-03 02:20:34 -0500707static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
708 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400709 // Renegotiation indication is not necessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -0400710 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400711 return 1;
712 }
713
David Benjamin52bf6902016-10-08 12:05:03 -0400714 assert(ssl->s3->initial_handshake_complete ==
715 (ssl->s3->previous_client_finished_len != 0));
716
Adam Langley5021b222015-06-12 18:27:58 -0700717 CBB contents, prev_finished;
718 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
719 !CBB_add_u16_length_prefixed(out, &contents) ||
720 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
721 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
722 ssl->s3->previous_client_finished_len) ||
723 !CBB_flush(out)) {
724 return 0;
725 }
726
727 return 1;
728}
729
David Benjamin8c880a22016-12-03 02:20:34 -0500730static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700731 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500732 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400733 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez246eeee2017-03-26 12:49:17 -0500734 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400735 return 0;
736 }
737
David Benjaminc11ea9422017-08-29 16:33:21 -0400738 // Servers may not switch between omitting the extension and supporting it.
739 // See RFC 5746, sections 3.5 and 4.2.
David Benjamin3e052de2015-11-25 20:10:31 -0500740 if (ssl->s3->initial_handshake_complete &&
741 (contents != NULL) != ssl->s3->send_connection_binding) {
742 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
743 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
744 return 0;
745 }
746
Adam Langley5021b222015-06-12 18:27:58 -0700747 if (contents == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400748 // Strictly speaking, if we want to avoid an attack we should *always* see
749 // RI even on initial ServerHello because the client doesn't see any
750 // renegotiation during an attack. However this would mean we could not
751 // connect to any server which doesn't support RI.
752 //
753 // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
754 // practical terms every client sets it so it's just assumed here.
David Benjamine9cddb82015-11-23 14:36:40 -0500755 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700756 }
757
758 const size_t expected_len = ssl->s3->previous_client_finished_len +
759 ssl->s3->previous_server_finished_len;
760
David Benjaminc11ea9422017-08-29 16:33:21 -0400761 // Check for logic errors
Adam Langley5021b222015-06-12 18:27:58 -0700762 assert(!expected_len || ssl->s3->previous_client_finished_len);
763 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400764 assert(ssl->s3->initial_handshake_complete ==
765 (ssl->s3->previous_client_finished_len != 0));
766 assert(ssl->s3->initial_handshake_complete ==
767 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700768
David Benjaminc11ea9422017-08-29 16:33:21 -0400769 // Parse out the extension contents.
Adam Langley5021b222015-06-12 18:27:58 -0700770 CBS renegotiated_connection;
771 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
772 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400773 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700774 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
775 return 0;
776 }
777
David Benjaminc11ea9422017-08-29 16:33:21 -0400778 // Check that the extension matches.
Adam Langley5021b222015-06-12 18:27:58 -0700779 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400780 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700781 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
782 return 0;
783 }
784
785 const uint8_t *d = CBS_data(&renegotiated_connection);
David Benjamin9343b0b2017-07-01 00:31:27 -0400786 int ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
787 ssl->s3->previous_client_finished_len) == 0;
788#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
789 ok = 1;
790#endif
791 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400792 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700793 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
794 return 0;
795 }
796 d += ssl->s3->previous_client_finished_len;
797
David Benjamin9343b0b2017-07-01 00:31:27 -0400798 ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
799 ssl->s3->previous_server_finished_len) == 0;
800#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
801 ok = 1;
802#endif
803 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400804 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley10e10602017-07-25 13:33:21 -0700805 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Adam Langley5021b222015-06-12 18:27:58 -0700806 return 0;
807 }
808 ssl->s3->send_connection_binding = 1;
809
810 return 1;
811}
812
David Benjamin8c880a22016-12-03 02:20:34 -0500813static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700814 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500815 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400816 // Renegotiation isn't supported as a server so this function should never be
817 // called after the initial handshake.
Adam Langley5021b222015-06-12 18:27:58 -0700818 assert(!ssl->s3->initial_handshake_complete);
819
Steven Valdez143e8b32016-07-11 13:19:03 -0400820 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
821 return 1;
822 }
823
Adam Langley5021b222015-06-12 18:27:58 -0700824 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400825 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700826 }
827
828 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700829 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
830 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400831 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700832 return 0;
833 }
834
David Benjaminc11ea9422017-08-29 16:33:21 -0400835 // Check that the extension matches. We do not support renegotiation as a
836 // server, so this must be empty.
David Benjamin52bf6902016-10-08 12:05:03 -0400837 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400838 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700839 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
840 return 0;
841 }
842
843 ssl->s3->send_connection_binding = 1;
844
845 return 1;
846}
847
David Benjamin8c880a22016-12-03 02:20:34 -0500848static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
849 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400850 // Renegotiation isn't supported as a server so this function should never be
851 // called after the initial handshake.
David Benjamin52bf6902016-10-08 12:05:03 -0400852 assert(!ssl->s3->initial_handshake_complete);
853
Steven Valdez143e8b32016-07-11 13:19:03 -0400854 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
855 return 1;
856 }
857
Adam Langley5021b222015-06-12 18:27:58 -0700858 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400859 !CBB_add_u16(out, 1 /* length */) ||
860 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700861 return 0;
862 }
863
864 return 1;
865}
866
Adam Langley0a056712015-07-01 15:03:33 -0700867
David Benjaminc11ea9422017-08-29 16:33:21 -0400868// Extended Master Secret.
869//
870// https://tools.ietf.org/html/rfc7627
Adam Langley0a056712015-07-01 15:03:33 -0700871
David Benjamin8c880a22016-12-03 02:20:34 -0500872static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400873 // Extended master secret is not necessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -0400874 if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700875 return 1;
876 }
877
878 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
879 !CBB_add_u16(out, 0 /* length */)) {
880 return 0;
881 }
882
883 return 1;
884}
885
David Benjamin8c880a22016-12-03 02:20:34 -0500886static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700887 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500888 SSL *const ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500889
890 if (contents != NULL) {
891 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
892 ssl->version == SSL3_VERSION ||
893 CBS_len(contents) != 0) {
David Benjamin163c9562016-08-29 23:14:17 -0400894 return 0;
895 }
896
David Benjaminfc02b592017-02-17 16:26:01 -0500897 hs->extended_master_secret = 1;
David Benjamin163c9562016-08-29 23:14:17 -0400898 }
899
David Benjaminc11ea9422017-08-29 16:33:21 -0400900 // Whether EMS is negotiated may not change on renegotiation.
David Benjaminfc02b592017-02-17 16:26:01 -0500901 if (ssl->s3->established_session != NULL &&
902 hs->extended_master_secret !=
903 ssl->s3->established_session->extended_master_secret) {
904 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
905 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400906 return 0;
907 }
908
Adam Langley0a056712015-07-01 15:03:33 -0700909 return 1;
910}
911
David Benjamin8c880a22016-12-03 02:20:34 -0500912static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500913 CBS *contents) {
David Benjaminfc02b592017-02-17 16:26:01 -0500914 uint16_t version = ssl3_protocol_version(hs->ssl);
David Benjamin8c880a22016-12-03 02:20:34 -0500915 if (version >= TLS1_3_VERSION ||
916 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400917 return 1;
918 }
919
920 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700921 return 1;
922 }
923
924 if (CBS_len(contents) != 0) {
925 return 0;
926 }
927
David Benjaminfc02b592017-02-17 16:26:01 -0500928 hs->extended_master_secret = 1;
Adam Langley0a056712015-07-01 15:03:33 -0700929 return 1;
930}
931
David Benjamin8c880a22016-12-03 02:20:34 -0500932static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -0500933 if (!hs->extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700934 return 1;
935 }
936
937 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
938 !CBB_add_u16(out, 0 /* length */)) {
939 return 0;
940 }
941
942 return 1;
943}
944
Adam Langley9b05bc52015-07-01 15:25:33 -0700945
David Benjaminc11ea9422017-08-29 16:33:21 -0400946// Session tickets.
947//
948// https://tools.ietf.org/html/rfc5077
Adam Langley9b05bc52015-07-01 15:25:33 -0700949
David Benjamin8c880a22016-12-03 02:20:34 -0500950static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
951 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400952 // TLS 1.3 uses a different ticket extension.
David Benjamin68161cb2017-06-20 14:49:43 -0400953 if (hs->min_version >= TLS1_3_VERSION ||
David Benjamin7c7d8312016-08-20 13:39:03 -0400954 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700955 return 1;
956 }
957
958 const uint8_t *ticket_data = NULL;
959 int ticket_len = 0;
960
David Benjaminc11ea9422017-08-29 16:33:21 -0400961 // Renegotiation does not participate in session resumption. However, still
962 // advertise the extension to avoid potentially breaking servers which carry
963 // over the state from the previous handshake, such as OpenSSL servers
964 // without upstream's 3c3f0259238594d77264a78944d409f2127642c4.
Adam Langley9b05bc52015-07-01 15:25:33 -0700965 if (!ssl->s3->initial_handshake_complete &&
966 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400967 ssl->session->tlsext_tick != NULL &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400968 // Don't send TLS 1.3 session tickets in the ticket extension.
Steven Valdez8f36c512017-06-20 10:55:02 -0400969 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700970 ticket_data = ssl->session->tlsext_tick;
971 ticket_len = ssl->session->tlsext_ticklen;
972 }
973
974 CBB ticket;
975 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
976 !CBB_add_u16_length_prefixed(out, &ticket) ||
977 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
978 !CBB_flush(out)) {
979 return 0;
980 }
981
982 return 1;
983}
984
David Benjamin8c880a22016-12-03 02:20:34 -0500985static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -0700986 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500987 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -0700988 if (contents == NULL) {
989 return 1;
990 }
991
Steven Valdez143e8b32016-07-11 13:19:03 -0400992 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
993 return 0;
994 }
995
David Benjaminc11ea9422017-08-29 16:33:21 -0400996 // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
997 // this function should never be called, even if the server tries to send the
998 // extension.
Adam Langley9b05bc52015-07-01 15:25:33 -0700999 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1000
1001 if (CBS_len(contents) != 0) {
1002 return 0;
1003 }
1004
David Benjamin8c880a22016-12-03 02:20:34 -05001005 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001006 return 1;
1007}
1008
David Benjamin8c880a22016-12-03 02:20:34 -05001009static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1010 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001011 return 1;
1012 }
1013
David Benjaminc11ea9422017-08-29 16:33:21 -04001014 // If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true.
David Benjamin8c880a22016-12-03 02:20:34 -05001015 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001016
1017 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1018 !CBB_add_u16(out, 0 /* length */)) {
1019 return 0;
1020 }
1021
1022 return 1;
1023}
1024
1025
David Benjaminc11ea9422017-08-29 16:33:21 -04001026// Signature Algorithms.
1027//
1028// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
Adam Langley2e857bd2015-07-01 16:09:19 -07001029
David Benjamin8c880a22016-12-03 02:20:34 -05001030static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1031 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001032 if (hs->max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001033 return 1;
1034 }
1035
David Benjamin0fc37ef2016-08-17 15:29:46 -04001036 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001037 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1038 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin69522112017-03-28 15:38:29 -05001039 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
1040 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
1041 !CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001042 return 0;
1043 }
1044
1045 return 1;
1046}
1047
David Benjamin8c880a22016-12-03 02:20:34 -05001048static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001049 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001050 OPENSSL_free(hs->peer_sigalgs);
1051 hs->peer_sigalgs = NULL;
1052 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001053
Adam Langley2e857bd2015-07-01 16:09:19 -07001054 if (contents == NULL) {
1055 return 1;
1056 }
1057
1058 CBS supported_signature_algorithms;
1059 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001060 CBS_len(contents) != 0 ||
1061 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001062 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001063 return 0;
1064 }
1065
1066 return 1;
1067}
1068
Adam Langley2e857bd2015-07-01 16:09:19 -07001069
David Benjaminc11ea9422017-08-29 16:33:21 -04001070// OCSP Stapling.
1071//
1072// https://tools.ietf.org/html/rfc6066#section-8
Adam Langleybb0bd042015-07-01 16:21:03 -07001073
David Benjamin8c880a22016-12-03 02:20:34 -05001074static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1075 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001076 if (!ssl->ocsp_stapling_enabled) {
1077 return 1;
1078 }
1079
1080 CBB contents;
1081 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1082 !CBB_add_u16_length_prefixed(out, &contents) ||
1083 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1084 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1085 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1086 !CBB_flush(out)) {
1087 return 0;
1088 }
1089
1090 return 1;
1091}
1092
David Benjamin8c880a22016-12-03 02:20:34 -05001093static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001094 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001095 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001096 if (contents == NULL) {
1097 return 1;
1098 }
1099
David Benjaminc11ea9422017-08-29 16:33:21 -04001100 // TLS 1.3 OCSP responses are included in the Certificate extensions.
Steven Valdeza833c352016-11-01 13:39:36 -04001101 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001102 return 0;
1103 }
1104
David Benjaminc11ea9422017-08-29 16:33:21 -04001105 // OCSP stapling is forbidden on non-certificate ciphers.
Steven Valdeza833c352016-11-01 13:39:36 -04001106 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001107 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001108 return 0;
1109 }
1110
David Benjaminc11ea9422017-08-29 16:33:21 -04001111 // Note this does not check for resumption in TLS 1.2. Sending
1112 // status_request here does not make sense, but OpenSSL does so and the
1113 // specification does not say anything. Tolerate it but ignore it.
David Benjamin942f4ed2016-07-16 19:03:49 +03001114
David Benjamin8c880a22016-12-03 02:20:34 -05001115 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001116 return 1;
1117}
1118
David Benjamin8c880a22016-12-03 02:20:34 -05001119static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001120 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001121 if (contents == NULL) {
1122 return 1;
1123 }
1124
1125 uint8_t status_type;
1126 if (!CBS_get_u8(contents, &status_type)) {
1127 return 0;
1128 }
1129
David Benjaminc11ea9422017-08-29 16:33:21 -04001130 // We cannot decide whether OCSP stapling will occur yet because the correct
1131 // SSL_CTX might not have been selected.
David Benjamin8c880a22016-12-03 02:20:34 -05001132 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001133
Adam Langleybb0bd042015-07-01 16:21:03 -07001134 return 1;
1135}
1136
David Benjamin8c880a22016-12-03 02:20:34 -05001137static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1138 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001139 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001140 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001141 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001142 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001143 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001144 return 1;
1145 }
1146
David Benjamin8c880a22016-12-03 02:20:34 -05001147 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001148
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001149 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001150 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001151}
1152
1153
David Benjaminc11ea9422017-08-29 16:33:21 -04001154// Next protocol negotiation.
1155//
1156// https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
Adam Langley97dfcbf2015-07-01 18:35:20 -07001157
David Benjamin8c880a22016-12-03 02:20:34 -05001158static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1159 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001160 if (ssl->s3->initial_handshake_complete ||
1161 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001162 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001163 return 1;
1164 }
1165
1166 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1167 !CBB_add_u16(out, 0 /* length */)) {
1168 return 0;
1169 }
1170
1171 return 1;
1172}
1173
David Benjamin8c880a22016-12-03 02:20:34 -05001174static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001175 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001176 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001177 if (contents == NULL) {
1178 return 1;
1179 }
1180
Steven Valdez143e8b32016-07-11 13:19:03 -04001181 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1182 return 0;
1183 }
1184
David Benjaminc11ea9422017-08-29 16:33:21 -04001185 // If any of these are false then we should never have sent the NPN
1186 // extension in the ClientHello and thus this function should never have been
1187 // called.
Adam Langley97dfcbf2015-07-01 18:35:20 -07001188 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001189 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001190 assert(ssl->ctx->next_proto_select_cb != NULL);
1191
David Benjamin76c2efc2015-08-31 14:24:29 -04001192 if (ssl->s3->alpn_selected != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001193 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001194 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1195 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1196 return 0;
1197 }
1198
Adam Langley97dfcbf2015-07-01 18:35:20 -07001199 const uint8_t *const orig_contents = CBS_data(contents);
1200 const size_t orig_len = CBS_len(contents);
1201
1202 while (CBS_len(contents) != 0) {
1203 CBS proto;
1204 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1205 CBS_len(&proto) == 0) {
1206 return 0;
1207 }
1208 }
1209
1210 uint8_t *selected;
1211 uint8_t selected_len;
1212 if (ssl->ctx->next_proto_select_cb(
1213 ssl, &selected, &selected_len, orig_contents, orig_len,
1214 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1215 *out_alert = SSL_AD_INTERNAL_ERROR;
1216 return 0;
1217 }
1218
David Benjamin79978df2015-12-25 15:56:49 -05001219 OPENSSL_free(ssl->s3->next_proto_negotiated);
David Benjamin81678aa2017-07-12 22:43:42 -04001220 ssl->s3->next_proto_negotiated =
1221 (uint8_t *)BUF_memdup(selected, selected_len);
David Benjamin79978df2015-12-25 15:56:49 -05001222 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001223 *out_alert = SSL_AD_INTERNAL_ERROR;
1224 return 0;
1225 }
1226
David Benjamin79978df2015-12-25 15:56:49 -05001227 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001228 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001229
1230 return 1;
1231}
1232
David Benjamin8c880a22016-12-03 02:20:34 -05001233static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001234 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001235 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001236 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1237 return 1;
1238 }
1239
Adam Langley97dfcbf2015-07-01 18:35:20 -07001240 if (contents != NULL && CBS_len(contents) != 0) {
1241 return 0;
1242 }
1243
1244 if (contents == NULL ||
1245 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001246 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001247 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001248 return 1;
1249 }
1250
David Benjamin8c880a22016-12-03 02:20:34 -05001251 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001252 return 1;
1253}
1254
David Benjamin8c880a22016-12-03 02:20:34 -05001255static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1256 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001257 // |next_proto_neg_seen| might have been cleared when an ALPN extension was
1258 // parsed.
David Benjamin8c880a22016-12-03 02:20:34 -05001259 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001260 return 1;
1261 }
1262
1263 const uint8_t *npa;
1264 unsigned npa_len;
1265
1266 if (ssl->ctx->next_protos_advertised_cb(
1267 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1268 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001269 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001270 return 1;
1271 }
1272
1273 CBB contents;
1274 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1275 !CBB_add_u16_length_prefixed(out, &contents) ||
1276 !CBB_add_bytes(&contents, npa, npa_len) ||
1277 !CBB_flush(out)) {
1278 return 0;
1279 }
1280
1281 return 1;
1282}
1283
1284
David Benjaminc11ea9422017-08-29 16:33:21 -04001285// Signed certificate timestamps.
1286//
1287// https://tools.ietf.org/html/rfc6962#section-3.3.1
Adam Langleyab8d87d2015-07-10 12:21:39 -07001288
David Benjamin8c880a22016-12-03 02:20:34 -05001289static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1290 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001291 if (!ssl->signed_cert_timestamps_enabled) {
1292 return 1;
1293 }
1294
1295 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1296 !CBB_add_u16(out, 0 /* length */)) {
1297 return 0;
1298 }
1299
1300 return 1;
1301}
1302
David Benjamin8c880a22016-12-03 02:20:34 -05001303static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001304 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001305 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001306 if (contents == NULL) {
1307 return 1;
1308 }
1309
David Benjaminc11ea9422017-08-29 16:33:21 -04001310 // TLS 1.3 SCTs are included in the Certificate extensions.
Steven Valdeza833c352016-11-01 13:39:36 -04001311 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001312 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001313 return 0;
1314 }
1315
David Benjaminc11ea9422017-08-29 16:33:21 -04001316 // If this is false then we should never have sent the SCT extension in the
1317 // ClientHello and thus this function should never have been called.
Adam Langleyab8d87d2015-07-10 12:21:39 -07001318 assert(ssl->signed_cert_timestamps_enabled);
1319
Adam Langleycfa08c32016-11-17 13:21:27 -08001320 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001321 *out_alert = SSL_AD_DECODE_ERROR;
1322 return 0;
1323 }
1324
David Benjaminc11ea9422017-08-29 16:33:21 -04001325 // Session resumption uses the original session information. The extension
1326 // should not be sent on resumption, but RFC 6962 did not make it a
1327 // requirement, so tolerate this.
1328 //
1329 // TODO(davidben): Enforce this anyway.
David Benjamin8fc2dc02017-08-22 15:07:51 -07001330 if (!ssl->s3->session_reused) {
1331 CRYPTO_BUFFER_free(hs->new_session->signed_cert_timestamp_list);
1332 hs->new_session->signed_cert_timestamp_list =
1333 CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool);
1334 if (hs->new_session->signed_cert_timestamp_list == nullptr) {
1335 *out_alert = SSL_AD_INTERNAL_ERROR;
1336 return 0;
1337 }
Adam Langleyab8d87d2015-07-10 12:21:39 -07001338 }
1339
1340 return 1;
1341}
1342
David Benjamin8c880a22016-12-03 02:20:34 -05001343static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001344 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001345 if (contents == NULL) {
1346 return 1;
1347 }
1348
1349 if (CBS_len(contents) != 0) {
1350 return 0;
1351 }
1352
David Benjamin8c880a22016-12-03 02:20:34 -05001353 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001354 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001355}
1356
David Benjamin8c880a22016-12-03 02:20:34 -05001357static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1358 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001359 // The extension shouldn't be sent when resuming sessions.
Steven Valdeza833c352016-11-01 13:39:36 -04001360 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1361 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001362 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001363 return 1;
1364 }
1365
1366 CBB contents;
1367 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1368 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001369 CBB_add_bytes(
1370 &contents,
1371 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1372 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001373 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001374}
1375
1376
David Benjaminc11ea9422017-08-29 16:33:21 -04001377// Application-level Protocol Negotiation.
1378//
1379// https://tools.ietf.org/html/rfc7301
Adam Langleyf18e4532015-07-10 13:39:53 -07001380
David Benjamin8c880a22016-12-03 02:20:34 -05001381static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1382 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001383 if (ssl->alpn_client_proto_list == NULL ||
1384 ssl->s3->initial_handshake_complete) {
1385 return 1;
1386 }
1387
1388 CBB contents, proto_list;
1389 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1390 !CBB_add_u16_length_prefixed(out, &contents) ||
1391 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1392 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1393 ssl->alpn_client_proto_list_len) ||
1394 !CBB_flush(out)) {
1395 return 0;
1396 }
1397
1398 return 1;
1399}
1400
David Benjamin8c880a22016-12-03 02:20:34 -05001401static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001402 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001403 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001404 if (contents == NULL) {
1405 return 1;
1406 }
1407
1408 assert(!ssl->s3->initial_handshake_complete);
1409 assert(ssl->alpn_client_proto_list != NULL);
1410
David Benjamin8c880a22016-12-03 02:20:34 -05001411 if (hs->next_proto_neg_seen) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001412 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001413 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1414 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1415 return 0;
1416 }
1417
David Benjaminc11ea9422017-08-29 16:33:21 -04001418 // The extension data consists of a ProtocolNameList which must have
1419 // exactly one ProtocolName. Each of these is length-prefixed.
Adam Langleyf18e4532015-07-10 13:39:53 -07001420 CBS protocol_name_list, protocol_name;
1421 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1422 CBS_len(contents) != 0 ||
1423 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001424 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001425 CBS_len(&protocol_name) == 0 ||
1426 CBS_len(&protocol_name_list) != 0) {
1427 return 0;
1428 }
1429
David Benjaminc8ff30c2017-04-04 13:52:36 -04001430 if (!ssl->ctx->allow_unknown_alpn_protos) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001431 // Check that the protocol name is one of the ones we advertised.
David Benjaminc8ff30c2017-04-04 13:52:36 -04001432 int protocol_ok = 0;
1433 CBS client_protocol_name_list, client_protocol_name;
1434 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1435 ssl->alpn_client_proto_list_len);
1436 while (CBS_len(&client_protocol_name_list) > 0) {
1437 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1438 &client_protocol_name)) {
1439 *out_alert = SSL_AD_INTERNAL_ERROR;
1440 return 0;
1441 }
1442
1443 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1444 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1445 CBS_data(&protocol_name),
1446 CBS_len(&protocol_name)) == 0) {
1447 protocol_ok = 1;
1448 break;
1449 }
1450 }
1451
1452 if (!protocol_ok) {
1453 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1454 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3e51757d2016-08-11 11:52:23 -04001455 return 0;
1456 }
David Benjamin3e51757d2016-08-11 11:52:23 -04001457 }
1458
Adam Langleyf18e4532015-07-10 13:39:53 -07001459 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1460 &ssl->s3->alpn_selected_len)) {
1461 *out_alert = SSL_AD_INTERNAL_ERROR;
1462 return 0;
1463 }
1464
1465 return 1;
1466}
1467
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001468int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001469 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001470 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001471 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001472 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001473 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001474 client_hello, &contents,
1475 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001476 // Ignore ALPN if not configured or no extension was supplied.
Adam Langleyf18e4532015-07-10 13:39:53 -07001477 return 1;
1478 }
1479
David Benjaminc11ea9422017-08-29 16:33:21 -04001480 // ALPN takes precedence over NPN.
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001481 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001482
1483 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001484 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1485 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001486 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001487 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1488 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001489 return 0;
1490 }
1491
David Benjaminc11ea9422017-08-29 16:33:21 -04001492 // Validate the protocol list.
Adam Langleyf18e4532015-07-10 13:39:53 -07001493 CBS protocol_name_list_copy = protocol_name_list;
1494 while (CBS_len(&protocol_name_list_copy) > 0) {
1495 CBS protocol_name;
1496
1497 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001498 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001499 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001500 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1501 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001502 return 0;
1503 }
1504 }
1505
1506 const uint8_t *selected;
1507 uint8_t selected_len;
1508 if (ssl->ctx->alpn_select_cb(
1509 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1510 CBS_len(&protocol_name_list),
1511 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1512 OPENSSL_free(ssl->s3->alpn_selected);
David Benjamin81678aa2017-07-12 22:43:42 -04001513 ssl->s3->alpn_selected = (uint8_t *)BUF_memdup(selected, selected_len);
Adam Langleyf18e4532015-07-10 13:39:53 -07001514 if (ssl->s3->alpn_selected == NULL) {
1515 *out_alert = SSL_AD_INTERNAL_ERROR;
1516 return 0;
1517 }
1518 ssl->s3->alpn_selected_len = selected_len;
1519 }
1520
1521 return 1;
1522}
1523
David Benjamin8c880a22016-12-03 02:20:34 -05001524static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1525 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001526 if (ssl->s3->alpn_selected == NULL) {
1527 return 1;
1528 }
1529
1530 CBB contents, proto_list, proto;
1531 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1532 !CBB_add_u16_length_prefixed(out, &contents) ||
1533 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1534 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001535 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1536 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001537 !CBB_flush(out)) {
1538 return 0;
1539 }
1540
1541 return 1;
1542}
1543
1544
David Benjaminc11ea9422017-08-29 16:33:21 -04001545// Channel ID.
1546//
1547// https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
Adam Langley49c7af12015-07-10 14:33:46 -07001548
David Benjamin8c880a22016-12-03 02:20:34 -05001549static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1550 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001551}
1552
David Benjamin8c880a22016-12-03 02:20:34 -05001553static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1554 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001555 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001556 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001557 return 1;
1558 }
1559
1560 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1561 !CBB_add_u16(out, 0 /* length */)) {
1562 return 0;
1563 }
1564
1565 return 1;
1566}
1567
David Benjamin8c880a22016-12-03 02:20:34 -05001568static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1569 uint8_t *out_alert, CBS *contents) {
1570 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001571 if (contents == NULL) {
1572 return 1;
1573 }
1574
David Benjamince079fd2016-08-02 16:22:34 -04001575 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001576 assert(ssl->tlsext_channel_id_enabled);
1577
1578 if (CBS_len(contents) != 0) {
1579 return 0;
1580 }
1581
1582 ssl->s3->tlsext_channel_id_valid = 1;
1583 return 1;
1584}
1585
David Benjamin8c880a22016-12-03 02:20:34 -05001586static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1587 uint8_t *out_alert, CBS *contents) {
1588 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001589 if (contents == NULL ||
1590 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001591 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001592 return 1;
1593 }
1594
1595 if (CBS_len(contents) != 0) {
1596 return 0;
1597 }
1598
1599 ssl->s3->tlsext_channel_id_valid = 1;
1600 return 1;
1601}
1602
David Benjamin8c880a22016-12-03 02:20:34 -05001603static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1604 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001605 if (!ssl->s3->tlsext_channel_id_valid) {
1606 return 1;
1607 }
1608
1609 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1610 !CBB_add_u16(out, 0 /* length */)) {
1611 return 0;
1612 }
1613
1614 return 1;
1615}
1616
Adam Langley391250d2015-07-15 19:06:07 -07001617
David Benjaminc11ea9422017-08-29 16:33:21 -04001618// Secure Real-time Transport Protocol (SRTP) extension.
1619//
1620// https://tools.ietf.org/html/rfc5764
Adam Langley391250d2015-07-15 19:06:07 -07001621
Adam Langley391250d2015-07-15 19:06:07 -07001622
David Benjamin8c880a22016-12-03 02:20:34 -05001623static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1624 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001625}
1626
David Benjamin8c880a22016-12-03 02:20:34 -05001627static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1628 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001629 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
David Benjaminee910bf2017-07-25 22:36:00 -04001630 if (profiles == NULL ||
1631 sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
Adam Langley391250d2015-07-15 19:06:07 -07001632 return 1;
1633 }
1634
1635 CBB contents, profile_ids;
1636 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1637 !CBB_add_u16_length_prefixed(out, &contents) ||
1638 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1639 return 0;
1640 }
1641
David Benjaminee910bf2017-07-25 22:36:00 -04001642 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
1643 if (!CBB_add_u16(&profile_ids, profile->id)) {
Adam Langley391250d2015-07-15 19:06:07 -07001644 return 0;
1645 }
1646 }
1647
1648 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1649 !CBB_flush(out)) {
1650 return 0;
1651 }
1652
1653 return 1;
1654}
1655
David Benjamin8c880a22016-12-03 02:20:34 -05001656static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001657 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001658 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001659 if (contents == NULL) {
1660 return 1;
1661 }
1662
David Benjaminc11ea9422017-08-29 16:33:21 -04001663 // The extension consists of a u16-prefixed profile ID list containing a
1664 // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1665 //
1666 // See https://tools.ietf.org/html/rfc5764#section-4.1.1
Adam Langley391250d2015-07-15 19:06:07 -07001667 CBS profile_ids, srtp_mki;
1668 uint16_t profile_id;
1669 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1670 !CBS_get_u16(&profile_ids, &profile_id) ||
1671 CBS_len(&profile_ids) != 0 ||
1672 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1673 CBS_len(contents) != 0) {
1674 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1675 return 0;
1676 }
1677
1678 if (CBS_len(&srtp_mki) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001679 // Must be no MKI, since we never offer one.
Adam Langley391250d2015-07-15 19:06:07 -07001680 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1681 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1682 return 0;
1683 }
1684
1685 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1686
David Benjaminc11ea9422017-08-29 16:33:21 -04001687 // Check to see if the server gave us something we support (and presumably
1688 // offered).
David Benjaminee910bf2017-07-25 22:36:00 -04001689 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001690 if (profile->id == profile_id) {
1691 ssl->srtp_profile = profile;
1692 return 1;
1693 }
1694 }
1695
1696 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1697 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1698 return 0;
1699}
1700
David Benjamin8c880a22016-12-03 02:20:34 -05001701static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001702 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001703 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001704 if (contents == NULL) {
1705 return 1;
1706 }
1707
1708 CBS profile_ids, srtp_mki;
1709 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1710 CBS_len(&profile_ids) < 2 ||
1711 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1712 CBS_len(contents) != 0) {
1713 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1714 return 0;
1715 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001716 // Discard the MKI value for now.
Adam Langley391250d2015-07-15 19:06:07 -07001717
1718 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1719 SSL_get_srtp_profiles(ssl);
1720
David Benjaminc11ea9422017-08-29 16:33:21 -04001721 // Pick the server's most preferred profile.
David Benjaminee910bf2017-07-25 22:36:00 -04001722 for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001723 CBS profile_ids_tmp;
1724 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1725
1726 while (CBS_len(&profile_ids_tmp) > 0) {
1727 uint16_t profile_id;
1728 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1729 return 0;
1730 }
1731
1732 if (server_profile->id == profile_id) {
1733 ssl->srtp_profile = server_profile;
1734 return 1;
1735 }
1736 }
1737 }
1738
1739 return 1;
1740}
1741
David Benjamin8c880a22016-12-03 02:20:34 -05001742static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1743 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001744 if (ssl->srtp_profile == NULL) {
1745 return 1;
1746 }
1747
1748 CBB contents, profile_ids;
1749 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1750 !CBB_add_u16_length_prefixed(out, &contents) ||
1751 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1752 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1753 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1754 !CBB_flush(out)) {
1755 return 0;
1756 }
1757
1758 return 1;
1759}
1760
Adam Langleybdd5d662015-07-20 16:19:08 -07001761
David Benjaminc11ea9422017-08-29 16:33:21 -04001762// EC point formats.
1763//
1764// https://tools.ietf.org/html/rfc4492#section-5.1.2
Adam Langleybdd5d662015-07-20 16:19:08 -07001765
David Benjamin8c880a22016-12-03 02:20:34 -05001766static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001767 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001768 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1769 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001770 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1771 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001772 !CBB_flush(out)) {
1773 return 0;
1774 }
1775
1776 return 1;
1777}
1778
David Benjamin8c880a22016-12-03 02:20:34 -05001779static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001780 // The point format extension is unneccessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -04001781 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin70aba262016-11-01 12:08:15 -04001782 return 1;
1783 }
1784
David Benjamin8c880a22016-12-03 02:20:34 -05001785 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001786}
1787
David Benjamin8c880a22016-12-03 02:20:34 -05001788static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001789 CBS *contents) {
1790 if (contents == NULL) {
1791 return 1;
1792 }
1793
David Benjamin8c880a22016-12-03 02:20:34 -05001794 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001795 return 0;
1796 }
1797
Adam Langleybdd5d662015-07-20 16:19:08 -07001798 CBS ec_point_format_list;
1799 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1800 CBS_len(contents) != 0) {
1801 return 0;
1802 }
1803
David Benjaminc11ea9422017-08-29 16:33:21 -04001804 // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1805 // point format.
David Benjamin17cf2cb2016-12-13 01:07:13 -05001806 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1807 TLSEXT_ECPOINTFORMAT_uncompressed,
1808 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001809 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001810 return 0;
1811 }
1812
1813 return 1;
1814}
1815
David Benjamin8c880a22016-12-03 02:20:34 -05001816static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001817 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001818 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001819 return 1;
1820 }
1821
David Benjamin8c880a22016-12-03 02:20:34 -05001822 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001823}
1824
David Benjamin8c880a22016-12-03 02:20:34 -05001825static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1826 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001827 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1828 return 1;
1829 }
1830
David Benjamin45738dd2017-02-09 20:01:26 -05001831 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1832 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001833 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001834
1835 if (!using_ecc) {
1836 return 1;
1837 }
1838
David Benjamin8c880a22016-12-03 02:20:34 -05001839 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001840}
1841
Steven Valdeza833c352016-11-01 13:39:36 -04001842
David Benjaminc11ea9422017-08-29 16:33:21 -04001843// Pre Shared Key
1844//
1845// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
Steven Valdez4aa154e2016-07-29 14:32:55 -04001846
David Benjamin8c880a22016-12-03 02:20:34 -05001847static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1848 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001849 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001850 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001851 return 0;
1852 }
1853
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 return 15 + ssl->session->tlsext_ticklen + binder_len;
1856}
1857
David Benjamin8c880a22016-12-03 02:20:34 -05001858static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1859 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001860 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001861 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001862 return 1;
1863 }
1864
David Benjaminad8f5e12017-02-20 17:00:20 -05001865 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001866 ssl_get_current_time(ssl, &now);
1867 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1868 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1869
David Benjaminc11ea9422017-08-29 16:33:21 -04001870 // Fill in a placeholder zero binder of the appropriate length. It will be
1871 // computed and filled in later after length prefixes are computed.
Steven Valdeza833c352016-11-01 13:39:36 -04001872 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez8f36c512017-06-20 10:55:02 -04001873 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001874
1875 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001876 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1877 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001878 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001879 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1880 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001881 ssl->session->tlsext_ticklen) ||
1882 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1883 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1884 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1885 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001886 return 0;
1887 }
1888
David Benjamin8c880a22016-12-03 02:20:34 -05001889 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001890 return CBB_flush(out);
1891}
1892
David Benjamin8baf9632016-11-17 17:11:16 +09001893int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1894 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001895 CBS *contents) {
1896 uint16_t psk_id;
1897 if (!CBS_get_u16(contents, &psk_id) ||
1898 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001899 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001900 *out_alert = SSL_AD_DECODE_ERROR;
1901 return 0;
1902 }
1903
David Benjaminc11ea9422017-08-29 16:33:21 -04001904 // We only advertise one PSK identity, so the only legal index is zero.
Steven Valdez4aa154e2016-07-29 14:32:55 -04001905 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001906 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001907 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1908 return 0;
1909 }
1910
1911 return 1;
1912}
1913
David Benjamin35ac5b72017-03-03 15:05:56 -05001914int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001915 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001916 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001917 // We only process the first PSK identity since we don't support pure PSK.
David Benjamin707af292017-03-10 17:47:18 -05001918 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001919 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001920 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001921 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001922 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001923 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001924 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001925 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001926 *out_alert = SSL_AD_DECODE_ERROR;
1927 return 0;
1928 }
1929
Steven Valdeza833c352016-11-01 13:39:36 -04001930 *out_binders = binders;
1931
David Benjaminc11ea9422017-08-29 16:33:21 -04001932 // Check the syntax of the remaining identities, but do not process them.
David Benjaminaedf3032016-12-01 16:47:56 -05001933 size_t num_identities = 1;
1934 while (CBS_len(&identities) != 0) {
1935 CBS unused_ticket;
1936 uint32_t unused_obfuscated_ticket_age;
1937 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1938 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1939 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1940 *out_alert = SSL_AD_DECODE_ERROR;
1941 return 0;
1942 }
1943
1944 num_identities++;
1945 }
1946
David Benjaminc11ea9422017-08-29 16:33:21 -04001947 // Check the syntax of the binders. The value will be checked later if
1948 // resuming.
David Benjaminaedf3032016-12-01 16:47:56 -05001949 size_t num_binders = 0;
1950 while (CBS_len(&binders) != 0) {
1951 CBS binder;
1952 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1953 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1954 *out_alert = SSL_AD_DECODE_ERROR;
1955 return 0;
1956 }
1957
1958 num_binders++;
1959 }
1960
1961 if (num_identities != num_binders) {
1962 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1963 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04001964 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04001965 }
1966
David Benjamine7f60a22016-11-16 18:54:25 +09001967 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001968}
1969
David Benjamin8baf9632016-11-17 17:11:16 +09001970int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1971 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001972 return 1;
1973 }
1974
1975 CBB contents;
1976 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1977 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001978 // We only consider the first identity for resumption
Steven Valdez4aa154e2016-07-29 14:32:55 -04001979 !CBB_add_u16(&contents, 0) ||
1980 !CBB_flush(out)) {
1981 return 0;
1982 }
1983
1984 return 1;
1985}
1986
1987
David Benjaminc11ea9422017-08-29 16:33:21 -04001988// Pre-Shared Key Exchange Modes
1989//
1990// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7
Steven Valdeza4ee74d2016-11-29 13:36:45 -05001991
David Benjamin8c880a22016-12-03 02:20:34 -05001992static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
1993 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04001994 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001995 return 1;
1996 }
1997
1998 CBB contents, ke_modes;
1999 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2000 !CBB_add_u16_length_prefixed(out, &contents) ||
2001 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2002 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2003 return 0;
2004 }
2005
2006 return CBB_flush(out);
2007}
2008
David Benjamin8c880a22016-12-03 02:20:34 -05002009static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002010 uint8_t *out_alert,
2011 CBS *contents) {
2012 if (contents == NULL) {
2013 return 1;
2014 }
2015
Steven Valdeza833c352016-11-01 13:39:36 -04002016 CBS ke_modes;
2017 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2018 CBS_len(&ke_modes) == 0 ||
2019 CBS_len(contents) != 0) {
2020 *out_alert = SSL_AD_DECODE_ERROR;
2021 return 0;
2022 }
2023
David Benjaminc11ea9422017-08-29 16:33:21 -04002024 // We only support tickets with PSK_DHE_KE.
David Benjamin17cf2cb2016-12-13 01:07:13 -05002025 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2026 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002027
2028 return 1;
2029}
2030
2031
David Benjaminc11ea9422017-08-29 16:33:21 -04002032// Early Data Indication
2033//
2034// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002035
David Benjamin8c880a22016-12-03 02:20:34 -05002036static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002037 SSL *const ssl = hs->ssl;
Steven Valdez2d850622017-01-11 11:34:52 -05002038 if (ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04002039 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION ||
Steven Valdez2d850622017-01-11 11:34:52 -05002040 ssl->session->ticket_max_early_data == 0 ||
2041 hs->received_hello_retry_request ||
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05002042 !ssl->cert->enable_early_data) {
Steven Valdez2d850622017-01-11 11:34:52 -05002043 return 1;
2044 }
2045
2046 hs->early_data_offered = 1;
2047
2048 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2049 !CBB_add_u16(out, 0) ||
2050 !CBB_flush(out)) {
2051 return 0;
2052 }
2053
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002054 return 1;
2055}
2056
Steven Valdez2d850622017-01-11 11:34:52 -05002057static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002058 uint8_t *out_alert, CBS *contents) {
2059 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002060 if (contents == NULL) {
2061 return 1;
2062 }
2063
2064 if (CBS_len(contents) != 0) {
2065 *out_alert = SSL_AD_DECODE_ERROR;
2066 return 0;
2067 }
2068
Steven Valdez2d850622017-01-11 11:34:52 -05002069 if (!ssl->s3->session_reused) {
2070 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2071 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2072 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002073 }
Steven Valdez2d850622017-01-11 11:34:52 -05002074
2075 ssl->early_data_accepted = 1;
2076 return 1;
2077}
2078
2079static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2080 uint8_t *out_alert, CBS *contents) {
2081 SSL *const ssl = hs->ssl;
2082 if (contents == NULL ||
2083 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2084 return 1;
2085 }
2086
2087 if (CBS_len(contents) != 0) {
2088 *out_alert = SSL_AD_DECODE_ERROR;
2089 return 0;
2090 }
2091
2092 hs->early_data_offered = 1;
2093 return 1;
2094}
2095
2096static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2097 if (!hs->ssl->early_data_accepted) {
2098 return 1;
2099 }
2100
2101 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2102 !CBB_add_u16(out, 0) ||
2103 !CBB_flush(out)) {
2104 return 0;
2105 }
2106
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002107 return 1;
2108}
2109
2110
David Benjaminc11ea9422017-08-29 16:33:21 -04002111// Key Share
2112//
2113// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5
Steven Valdez143e8b32016-07-11 13:19:03 -04002114
David Benjamin8c880a22016-12-03 02:20:34 -05002115static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2116 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002117 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002118 return 1;
2119 }
2120
2121 CBB contents, kse_bytes;
2122 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2123 !CBB_add_u16_length_prefixed(out, &contents) ||
2124 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2125 return 0;
2126 }
2127
David Benjamin8c880a22016-12-03 02:20:34 -05002128 uint16_t group_id = hs->retry_group;
2129 if (hs->received_hello_retry_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002130 // We received a HelloRetryRequest without a new curve, so there is no new
2131 // share to append. Leave |hs->key_share| as-is.
Steven Valdeza833c352016-11-01 13:39:36 -04002132 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002133 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2134 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002135 return 0;
2136 }
David Benjamin8c880a22016-12-03 02:20:34 -05002137 OPENSSL_free(hs->key_share_bytes);
2138 hs->key_share_bytes = NULL;
2139 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002140 if (group_id == 0) {
2141 return CBB_flush(out);
2142 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002143 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04002144 // Add a fake group. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002145 if (ssl->ctx->grease_enabled &&
2146 (!CBB_add_u16(&kse_bytes,
2147 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2148 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2149 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2150 return 0;
2151 }
2152
David Benjaminc11ea9422017-08-29 16:33:21 -04002153 // Predict the most preferred group.
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002154 const uint16_t *groups;
2155 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002156 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002157 if (groups_len == 0) {
2158 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2159 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002160 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002161
2162 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002163 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002164
David Benjaminc642aca2017-07-19 23:28:43 -04002165 hs->key_share = SSLKeyShare::Create(group_id);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002166 CBB key_exchange;
David Benjaminc642aca2017-07-19 23:28:43 -04002167 if (!hs->key_share ||
2168 !CBB_add_u16(&kse_bytes, group_id) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002169 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjaminc642aca2017-07-19 23:28:43 -04002170 !hs->key_share->Offer(&key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002171 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002172 return 0;
2173 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002174
David Benjamin8c880a22016-12-03 02:20:34 -05002175 if (!hs->received_hello_retry_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002176 // Save the contents of the extension to repeat it in the second
2177 // ClientHello.
David Benjamin8c880a22016-12-03 02:20:34 -05002178 hs->key_share_bytes_len = CBB_len(&kse_bytes);
David Benjamin81678aa2017-07-12 22:43:42 -04002179 hs->key_share_bytes =
2180 (uint8_t *)BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
David Benjamin8c880a22016-12-03 02:20:34 -05002181 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002182 return 0;
2183 }
2184 }
2185
Steven Valdez143e8b32016-07-11 13:19:03 -04002186 return CBB_flush(out);
2187}
2188
David Benjamin8baf9632016-11-17 17:11:16 +09002189int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002190 size_t *out_secret_len,
2191 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002192 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002193 uint16_t group_id;
2194 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002195 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2196 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002197 *out_alert = SSL_AD_DECODE_ERROR;
2198 return 0;
2199 }
2200
David Benjaminc642aca2017-07-19 23:28:43 -04002201 if (hs->key_share->GroupID() != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002202 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2203 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2204 return 0;
2205 }
2206
David Benjaminc642aca2017-07-19 23:28:43 -04002207 if (!hs->key_share->Finish(out_secret, out_secret_len, out_alert,
2208 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002209 *out_alert = SSL_AD_INTERNAL_ERROR;
2210 return 0;
2211 }
2212
David Benjamin45738dd2017-02-09 20:01:26 -05002213 hs->new_session->group_id = group_id;
David Benjaminc642aca2017-07-19 23:28:43 -04002214 hs->key_share.reset();
Steven Valdez143e8b32016-07-11 13:19:03 -04002215 return 1;
2216}
2217
David Benjamin8baf9632016-11-17 17:11:16 +09002218int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002219 uint8_t **out_secret,
2220 size_t *out_secret_len,
2221 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002222 uint16_t group_id;
2223 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002224 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002225 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2226 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2227 return 0;
2228 }
2229
2230 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002231 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002232 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002233 return 0;
2234 }
2235
David Benjaminc11ea9422017-08-29 16:33:21 -04002236 // Find the corresponding key share.
David Benjamin7e1f9842016-09-20 19:24:40 -04002237 int found = 0;
2238 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002239 while (CBS_len(&key_shares) > 0) {
2240 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002241 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002242 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002243 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2244 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002245 return 0;
2246 }
2247
David Benjamin7e1f9842016-09-20 19:24:40 -04002248 if (id == group_id) {
2249 if (found) {
2250 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2251 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2252 return 0;
2253 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002254
David Benjamin7e1f9842016-09-20 19:24:40 -04002255 found = 1;
2256 peer_key = peer_key_tmp;
David Benjaminc11ea9422017-08-29 16:33:21 -04002257 // Continue parsing the structure to keep peers honest.
Steven Valdez143e8b32016-07-11 13:19:03 -04002258 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002259 }
2260
David Benjamin7e1f9842016-09-20 19:24:40 -04002261 if (!found) {
2262 *out_found = 0;
2263 *out_secret = NULL;
2264 *out_secret_len = 0;
2265 return 1;
2266 }
2267
David Benjaminc11ea9422017-08-29 16:33:21 -04002268 // Compute the DH secret.
David Benjamin7e1f9842016-09-20 19:24:40 -04002269 uint8_t *secret = NULL;
2270 size_t secret_len;
David Benjamin1386aad2017-07-19 23:57:40 -04002271 ScopedCBB public_key;
David Benjaminc642aca2017-07-19 23:28:43 -04002272 UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
2273 if (!key_share ||
2274 !CBB_init(public_key.get(), 32) ||
2275 !key_share->Accept(public_key.get(), &secret, &secret_len, out_alert,
2276 CBS_data(&peer_key), CBS_len(&peer_key)) ||
David Benjamin1386aad2017-07-19 23:57:40 -04002277 !CBB_finish(public_key.get(), &hs->ecdh_public_key,
David Benjaminbf833c32017-03-30 15:45:21 -05002278 &hs->ecdh_public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002279 OPENSSL_free(secret);
Steven Valdez803c77a2016-09-06 14:13:43 -04002280 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002281 return 0;
2282 }
2283
David Benjamin7e1f9842016-09-20 19:24:40 -04002284 *out_secret = secret;
2285 *out_secret_len = secret_len;
2286 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002287 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002288}
2289
David Benjamin8baf9632016-11-17 17:11:16 +09002290int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002291 uint16_t group_id;
2292 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002293 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002294 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2295 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2296 !CBB_add_u16(&kse_bytes, group_id) ||
2297 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002298 !CBB_add_bytes(&public_key, hs->ecdh_public_key,
2299 hs->ecdh_public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002300 !CBB_flush(out)) {
2301 return 0;
2302 }
2303
David Benjaminbf833c32017-03-30 15:45:21 -05002304 OPENSSL_free(hs->ecdh_public_key);
2305 hs->ecdh_public_key = NULL;
2306 hs->ecdh_public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002307
David Benjamin45738dd2017-02-09 20:01:26 -05002308 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002309 return 1;
2310}
2311
2312
David Benjaminc11ea9422017-08-29 16:33:21 -04002313// Supported Versions
2314//
2315// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1
Steven Valdezfdd10992016-09-15 16:27:05 -04002316
David Benjamin8c880a22016-12-03 02:20:34 -05002317static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2318 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002319 if (hs->max_version <= TLS1_2_VERSION) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002320 return 1;
2321 }
2322
2323 CBB contents, versions;
2324 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2325 !CBB_add_u16_length_prefixed(out, &contents) ||
2326 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2327 return 0;
2328 }
2329
David Benjaminc11ea9422017-08-29 16:33:21 -04002330 // Add a fake version. See draft-davidben-tls-grease-01.
David Benjamind9791bf2016-09-27 16:39:52 -04002331 if (ssl->ctx->grease_enabled &&
2332 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2333 return 0;
2334 }
2335
Steven Valdez8f36c512017-06-20 10:55:02 -04002336 if (!ssl_add_supported_versions(hs, &versions) ||
2337 !CBB_flush(out)) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002338 return 0;
2339 }
2340
2341 return 1;
2342}
2343
2344
David Benjaminc11ea9422017-08-29 16:33:21 -04002345// Cookie
2346//
2347// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2
David Benjamin3baa6e12016-10-07 21:10:38 -04002348
David Benjamin8c880a22016-12-03 02:20:34 -05002349static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2350 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002351 return 1;
2352 }
2353
2354 CBB contents, cookie;
2355 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2356 !CBB_add_u16_length_prefixed(out, &contents) ||
2357 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002358 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002359 !CBB_flush(out)) {
2360 return 0;
2361 }
2362
David Benjaminc11ea9422017-08-29 16:33:21 -04002363 // The cookie is no longer needed in memory.
David Benjamin8c880a22016-12-03 02:20:34 -05002364 OPENSSL_free(hs->cookie);
2365 hs->cookie = NULL;
2366 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002367 return 1;
2368}
2369
2370
David Benjaminc11ea9422017-08-29 16:33:21 -04002371// Negotiated Groups
2372//
2373// https://tools.ietf.org/html/rfc4492#section-5.1.2
2374// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4
Adam Langley273d49c2015-07-20 16:38:52 -07002375
David Benjamin8c880a22016-12-03 02:20:34 -05002376static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2377 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002378 CBB contents, groups_bytes;
2379 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002380 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002381 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002382 return 0;
2383 }
2384
David Benjaminc11ea9422017-08-29 16:33:21 -04002385 // Add a fake group. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002386 if (ssl->ctx->grease_enabled &&
2387 !CBB_add_u16(&groups_bytes,
2388 ssl_get_grease_value(ssl, ssl_grease_group))) {
2389 return 0;
2390 }
2391
Steven Valdezce902a92016-05-17 11:47:53 -04002392 const uint16_t *groups;
2393 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002394 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002395
David Benjamin54091232016-09-05 12:47:25 -04002396 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002397 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002398 return 0;
2399 }
2400 }
2401
2402 return CBB_flush(out);
2403}
2404
David Benjamin8c880a22016-12-03 02:20:34 -05002405static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2406 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002407 CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002408 // This extension is not expected to be echoed by servers in TLS 1.2, but some
2409 // BigIP servers send it nonetheless, so do not enforce this.
Adam Langley273d49c2015-07-20 16:38:52 -07002410 return 1;
2411}
2412
David Benjamin8c880a22016-12-03 02:20:34 -05002413static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2414 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002415 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002416 if (contents == NULL) {
2417 return 1;
2418 }
2419
Steven Valdezce902a92016-05-17 11:47:53 -04002420 CBS supported_group_list;
2421 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2422 CBS_len(&supported_group_list) == 0 ||
2423 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002424 CBS_len(contents) != 0) {
2425 return 0;
2426 }
2427
David Benjamin8c880a22016-12-03 02:20:34 -05002428 hs->peer_supported_group_list =
David Benjamin81678aa2017-07-12 22:43:42 -04002429 (uint16_t *)OPENSSL_malloc(CBS_len(&supported_group_list));
David Benjamin8c880a22016-12-03 02:20:34 -05002430 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002431 *out_alert = SSL_AD_INTERNAL_ERROR;
2432 return 0;
2433 }
2434
Steven Valdezce902a92016-05-17 11:47:53 -04002435 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002436 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002437 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002438 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002439 goto err;
2440 }
2441 }
2442
Steven Valdezce902a92016-05-17 11:47:53 -04002443 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002444 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002445
2446 return 1;
2447
2448err:
David Benjamin8c880a22016-12-03 02:20:34 -05002449 OPENSSL_free(hs->peer_supported_group_list);
2450 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002451 *out_alert = SSL_AD_INTERNAL_ERROR;
2452 return 0;
2453}
2454
David Benjamin8c880a22016-12-03 02:20:34 -05002455static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002456 // Servers don't echo this extension.
Adam Langley273d49c2015-07-20 16:38:52 -07002457 return 1;
2458}
2459
2460
David Benjaminc11ea9422017-08-29 16:33:21 -04002461// kExtensions contains all the supported extensions.
Adam Langley614c66a2015-06-12 15:26:58 -07002462static const struct tls_extension kExtensions[] = {
2463 {
Adam Langley5021b222015-06-12 18:27:58 -07002464 TLSEXT_TYPE_renegotiate,
2465 NULL,
2466 ext_ri_add_clienthello,
2467 ext_ri_parse_serverhello,
2468 ext_ri_parse_clienthello,
2469 ext_ri_add_serverhello,
2470 },
2471 {
Adam Langley614c66a2015-06-12 15:26:58 -07002472 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002473 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002474 ext_sni_add_clienthello,
2475 ext_sni_parse_serverhello,
2476 ext_sni_parse_clienthello,
2477 ext_sni_add_serverhello,
2478 },
Adam Langley0a056712015-07-01 15:03:33 -07002479 {
2480 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002481 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002482 ext_ems_add_clienthello,
2483 ext_ems_parse_serverhello,
2484 ext_ems_parse_clienthello,
2485 ext_ems_add_serverhello,
2486 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002487 {
2488 TLSEXT_TYPE_session_ticket,
2489 NULL,
2490 ext_ticket_add_clienthello,
2491 ext_ticket_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04002492 // Ticket extension client parsing is handled in ssl_session.c
Steven Valdez6b8509a2016-07-12 13:38:32 -04002493 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002494 ext_ticket_add_serverhello,
2495 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002496 {
2497 TLSEXT_TYPE_signature_algorithms,
2498 NULL,
2499 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002500 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002501 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002502 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002503 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002504 {
2505 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002506 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002507 ext_ocsp_add_clienthello,
2508 ext_ocsp_parse_serverhello,
2509 ext_ocsp_parse_clienthello,
2510 ext_ocsp_add_serverhello,
2511 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002512 {
2513 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002514 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002515 ext_npn_add_clienthello,
2516 ext_npn_parse_serverhello,
2517 ext_npn_parse_clienthello,
2518 ext_npn_add_serverhello,
2519 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002520 {
2521 TLSEXT_TYPE_certificate_timestamp,
2522 NULL,
2523 ext_sct_add_clienthello,
2524 ext_sct_parse_serverhello,
2525 ext_sct_parse_clienthello,
2526 ext_sct_add_serverhello,
2527 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002528 {
2529 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002530 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002531 ext_alpn_add_clienthello,
2532 ext_alpn_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04002533 // ALPN is negotiated late in |ssl_negotiate_alpn|.
David Benjamin9ef31f02016-10-31 18:01:13 -04002534 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002535 ext_alpn_add_serverhello,
2536 },
Adam Langley49c7af12015-07-10 14:33:46 -07002537 {
2538 TLSEXT_TYPE_channel_id,
2539 ext_channel_id_init,
2540 ext_channel_id_add_clienthello,
2541 ext_channel_id_parse_serverhello,
2542 ext_channel_id_parse_clienthello,
2543 ext_channel_id_add_serverhello,
2544 },
Adam Langley391250d2015-07-15 19:06:07 -07002545 {
2546 TLSEXT_TYPE_srtp,
2547 ext_srtp_init,
2548 ext_srtp_add_clienthello,
2549 ext_srtp_parse_serverhello,
2550 ext_srtp_parse_clienthello,
2551 ext_srtp_add_serverhello,
2552 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002553 {
2554 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002555 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002556 ext_ec_point_add_clienthello,
2557 ext_ec_point_parse_serverhello,
2558 ext_ec_point_parse_clienthello,
2559 ext_ec_point_add_serverhello,
2560 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002561 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002562 TLSEXT_TYPE_key_share,
2563 NULL,
2564 ext_key_share_add_clienthello,
2565 forbid_parse_serverhello,
2566 ignore_parse_clienthello,
2567 dont_add_serverhello,
2568 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002569 {
Steven Valdeza833c352016-11-01 13:39:36 -04002570 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002571 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002572 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002573 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002574 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002575 dont_add_serverhello,
2576 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002577 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002578 TLSEXT_TYPE_early_data,
2579 NULL,
2580 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002581 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002582 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002583 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002584 },
2585 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002586 TLSEXT_TYPE_supported_versions,
2587 NULL,
2588 ext_supported_versions_add_clienthello,
2589 forbid_parse_serverhello,
2590 ignore_parse_clienthello,
2591 dont_add_serverhello,
2592 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002593 {
2594 TLSEXT_TYPE_cookie,
2595 NULL,
2596 ext_cookie_add_clienthello,
2597 forbid_parse_serverhello,
2598 ignore_parse_clienthello,
2599 dont_add_serverhello,
2600 },
David Benjaminc11ea9422017-08-29 16:33:21 -04002601 // The final extension must be non-empty. WebSphere Application Server 7.0 is
2602 // intolerant to the last extension being zero-length. See
2603 // https://crbug.com/363583.
Adam Langley273d49c2015-07-20 16:38:52 -07002604 {
Steven Valdezce902a92016-05-17 11:47:53 -04002605 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002606 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002607 ext_supported_groups_add_clienthello,
2608 ext_supported_groups_parse_serverhello,
2609 ext_supported_groups_parse_clienthello,
2610 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002611 },
Adam Langley614c66a2015-06-12 15:26:58 -07002612};
2613
2614#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2615
David Benjamina3d76d02017-07-14 19:36:07 -04002616static_assert(kNumExtensions <=
2617 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
2618 "too many extensions for sent bitset");
2619static_assert(kNumExtensions <=
2620 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2621 "too many extensions for received bitset");
Adam Langley4cfa96b2015-07-01 11:56:55 -07002622
Adam Langley614c66a2015-06-12 15:26:58 -07002623static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2624 uint16_t value) {
2625 unsigned i;
2626 for (i = 0; i < kNumExtensions; i++) {
2627 if (kExtensions[i].value == value) {
2628 *out_index = i;
2629 return &kExtensions[i];
2630 }
2631 }
2632
2633 return NULL;
2634}
2635
David Benjamin8c880a22016-12-03 02:20:34 -05002636int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2637 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04002638 // Don't add extensions for SSLv3 unless doing secure renegotiation.
David Benjaminf04c2e92016-12-06 13:35:25 -05002639 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002640 !ssl->s3->send_connection_binding) {
2641 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002642 }
Adam Langley95c29f32014-06-20 12:00:00 -07002643
David Benjamine8d53502015-10-10 14:13:23 -04002644 CBB extensions;
2645 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002646 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2647 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002648 }
Adam Langley95c29f32014-06-20 12:00:00 -07002649
David Benjamin8c880a22016-12-03 02:20:34 -05002650 hs->extensions.sent = 0;
2651 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002652
David Benjamin54091232016-09-05 12:47:25 -04002653 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002654 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002655 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002656 }
2657 }
Adam Langley95c29f32014-06-20 12:00:00 -07002658
David Benjamin65ac9972016-09-02 21:35:25 -04002659 uint16_t grease_ext1 = 0;
2660 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002661 // Add a fake empty extension. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002662 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2663 if (!CBB_add_u16(&extensions, grease_ext1) ||
2664 !CBB_add_u16(&extensions, 0 /* zero length */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002665 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2666 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002667 }
2668 }
2669
David Benjamin54091232016-09-05 12:47:25 -04002670 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002671 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002672 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002673 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002674 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin81678aa2017-07-12 22:43:42 -04002675 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002676 }
Adam Langley95c29f32014-06-20 12:00:00 -07002677
Adam Langley33ad2b52015-07-20 17:43:53 -07002678 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002679 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002680 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002681 }
Adam Langley75712922014-10-10 16:23:43 -07002682
David Benjamin2bd19172016-11-17 16:47:15 +09002683 if (!custom_ext_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002684 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2685 return 0;
Adam Langley09505632015-07-30 18:10:13 -07002686 }
2687
David Benjamin65ac9972016-09-02 21:35:25 -04002688 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002689 // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002690 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2691
David Benjaminc11ea9422017-08-29 16:33:21 -04002692 // The two fake extensions must not have the same value. GREASE values are
2693 // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2694 // one.
David Benjamin65ac9972016-09-02 21:35:25 -04002695 if (grease_ext1 == grease_ext2) {
2696 grease_ext2 ^= 0x1010;
2697 }
2698
2699 if (!CBB_add_u16(&extensions, grease_ext2) ||
2700 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2701 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002702 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2703 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002704 }
2705 }
2706
David Benjamince079fd2016-08-02 16:22:34 -04002707 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002708 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002709 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002710 if (header_len > 0xff && header_len < 0x200) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002711 // Add padding to workaround bugs in F5 terminators. See RFC 7685.
2712 //
2713 // NB: because this code works out the length of all existing extensions
2714 // it MUST always appear last.
David Benjamin0a968592015-07-21 22:06:19 -04002715 size_t padding_len = 0x200 - header_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04002716 // Extensions take at least four bytes to encode. Always include at least
2717 // one byte of data if including the extension. WebSphere Application
2718 // Server 7.0 is intolerant to the last extension being zero-length. See
2719 // https://crbug.com/363583.
Adam Langleyfcf25832014-12-18 17:42:32 -08002720 if (padding_len >= 4 + 1) {
2721 padding_len -= 4;
2722 } else {
2723 padding_len = 1;
2724 }
Adam Langley95c29f32014-06-20 12:00:00 -07002725
Adam Langley33ad2b52015-07-20 17:43:53 -07002726 uint8_t *padding_bytes;
2727 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2728 !CBB_add_u16(&extensions, padding_len) ||
2729 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002730 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2731 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002732 }
Adam Langley75712922014-10-10 16:23:43 -07002733
David Benjamin17cf2cb2016-12-13 01:07:13 -05002734 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002735 }
2736 }
Adam Langley75712922014-10-10 16:23:43 -07002737
David Benjaminc11ea9422017-08-29 16:33:21 -04002738 // The PSK extension must be last, including after the padding.
David Benjamin8c880a22016-12-03 02:20:34 -05002739 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002740 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2741 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -04002742 }
2743
David Benjaminc11ea9422017-08-29 16:33:21 -04002744 // Discard empty extensions blocks.
David Benjamina01deee2015-12-08 18:56:31 -05002745 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002746 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002747 }
2748
David Benjamine8d53502015-10-10 14:13:23 -04002749 return CBB_flush(out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002750}
Adam Langley95c29f32014-06-20 12:00:00 -07002751
David Benjamin8c880a22016-12-03 02:20:34 -05002752int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2753 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002754 CBB extensions;
2755 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002756 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002757 }
2758
David Benjamin8c880a22016-12-03 02:20:34 -05002759 for (unsigned i = 0; i < kNumExtensions; i++) {
2760 if (!(hs->extensions.received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002761 // Don't send extensions that were not received.
Adam Langley614c66a2015-06-12 15:26:58 -07002762 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002763 }
Adam Langley95c29f32014-06-20 12:00:00 -07002764
David Benjamin8c880a22016-12-03 02:20:34 -05002765 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002766 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002767 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002768 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002769 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002770 }
Adam Langley95c29f32014-06-20 12:00:00 -07002771
David Benjamin2bd19172016-11-17 16:47:15 +09002772 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002773 goto err;
2774 }
2775
David Benjaminc11ea9422017-08-29 16:33:21 -04002776 // Discard empty extensions blocks before TLS 1.3.
Steven Valdez143e8b32016-07-11 13:19:03 -04002777 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2778 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002779 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002780 }
2781
David Benjamin56380462015-10-10 14:59:09 -04002782 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002783
2784err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002785 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002786 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002787}
Adam Langley95c29f32014-06-20 12:00:00 -07002788
David Benjamin731058e2016-12-03 23:15:13 -05002789static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2790 const SSL_CLIENT_HELLO *client_hello,
2791 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002792 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002793 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002794 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002795 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002796 }
2797 }
2798
David Benjamin8c880a22016-12-03 02:20:34 -05002799 hs->extensions.received = 0;
2800 hs->custom_extensions.received = 0;
David Benjamine14ff062016-08-09 16:21:24 -04002801 CBS extensions;
2802 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2803 while (CBS_len(&extensions) != 0) {
2804 uint16_t type;
2805 CBS extension;
2806
David Benjaminc11ea9422017-08-29 16:33:21 -04002807 // Decode the next extension.
David Benjamine14ff062016-08-09 16:21:24 -04002808 if (!CBS_get_u16(&extensions, &type) ||
2809 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002810 *out_alert = SSL_AD_DECODE_ERROR;
2811 return 0;
2812 }
Adam Langley95c29f32014-06-20 12:00:00 -07002813
David Benjaminc11ea9422017-08-29 16:33:21 -04002814 // RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2815 // ambiguous. Ignore all but the renegotiation_info extension.
David Benjamine14ff062016-08-09 16:21:24 -04002816 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2817 continue;
2818 }
Adam Langley95c29f32014-06-20 12:00:00 -07002819
David Benjamine14ff062016-08-09 16:21:24 -04002820 unsigned ext_index;
2821 const struct tls_extension *const ext =
2822 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002823
David Benjamine14ff062016-08-09 16:21:24 -04002824 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002825 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002826 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002827 return 0;
2828 }
David Benjamine14ff062016-08-09 16:21:24 -04002829 continue;
2830 }
2831
David Benjamin8c880a22016-12-03 02:20:34 -05002832 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002833 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002834 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002835 *out_alert = alert;
2836 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002837 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002838 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002839 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002840 }
Adam Langley75712922014-10-10 16:23:43 -07002841
David Benjamin1deb41b2016-08-09 19:36:38 -04002842 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002843 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002844 continue;
2845 }
2846
2847 CBS *contents = NULL, fake_contents;
2848 static const uint8_t kFakeRenegotiateExtension[] = {0};
2849 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2850 ssl_client_cipher_list_contains_cipher(client_hello,
2851 SSL3_CK_SCSV & 0xffff)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002852 // The renegotiation SCSV was received so pretend that we received a
2853 // renegotiation extension.
David Benjamin1deb41b2016-08-09 19:36:38 -04002854 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2855 sizeof(kFakeRenegotiateExtension));
2856 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002857 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002858 }
2859
David Benjaminc11ea9422017-08-29 16:33:21 -04002860 // Extension wasn't observed so call the callback with a NULL
2861 // parameter.
David Benjamin1deb41b2016-08-09 19:36:38 -04002862 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002863 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002864 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002865 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002866 *out_alert = alert;
2867 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002868 }
2869 }
2870
Adam Langleyfcf25832014-12-18 17:42:32 -08002871 return 1;
2872}
Adam Langley95c29f32014-06-20 12:00:00 -07002873
David Benjamin731058e2016-12-03 23:15:13 -05002874int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2875 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002876 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002877 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002878 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002879 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002880 return 0;
2881 }
Adam Langley95c29f32014-06-20 12:00:00 -07002882
David Benjamin8c880a22016-12-03 02:20:34 -05002883 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002884 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002885 return 0;
2886 }
Adam Langley95c29f32014-06-20 12:00:00 -07002887
Adam Langleyfcf25832014-12-18 17:42:32 -08002888 return 1;
2889}
Adam Langley95c29f32014-06-20 12:00:00 -07002890
David Benjamin8c880a22016-12-03 02:20:34 -05002891static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2892 int *out_alert) {
2893 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04002894 // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty.
Steven Valdez143e8b32016-07-11 13:19:03 -04002895 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2896 return 1;
2897 }
Adam Langley614c66a2015-06-12 15:26:58 -07002898
David Benjaminc11ea9422017-08-29 16:33:21 -04002899 // Decode the extensions block and check it is valid.
Steven Valdez143e8b32016-07-11 13:19:03 -04002900 CBS extensions;
2901 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2902 !tls1_check_duplicate_extensions(&extensions)) {
2903 *out_alert = SSL_AD_DECODE_ERROR;
2904 return 0;
2905 }
2906
2907 uint32_t received = 0;
2908 while (CBS_len(&extensions) != 0) {
2909 uint16_t type;
2910 CBS extension;
2911
David Benjaminc11ea9422017-08-29 16:33:21 -04002912 // Decode the next extension.
Steven Valdez143e8b32016-07-11 13:19:03 -04002913 if (!CBS_get_u16(&extensions, &type) ||
2914 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002915 *out_alert = SSL_AD_DECODE_ERROR;
2916 return 0;
2917 }
Adam Langley95c29f32014-06-20 12:00:00 -07002918
Steven Valdez143e8b32016-07-11 13:19:03 -04002919 unsigned ext_index;
2920 const struct tls_extension *const ext =
2921 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002922
Steven Valdez143e8b32016-07-11 13:19:03 -04002923 if (ext == NULL) {
Steven Valdezf4ecc842017-08-10 14:02:56 -04002924 hs->received_custom_extension = 1;
David Benjamin2bd19172016-11-17 16:47:15 +09002925 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002926 return 0;
2927 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002928 continue;
2929 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002930
David Benjamina3d76d02017-07-14 19:36:07 -04002931 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
2932 "too many bits");
David Benjamin5db7c9b2017-01-24 16:17:03 -05002933
David Benjamin8c880a22016-12-03 02:20:34 -05002934 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002935 type != TLSEXT_TYPE_renegotiate) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002936 // If the extension was never sent then it is illegal, except for the
2937 // renegotiation extension which, in SSL 3.0, is signaled via SCSV.
Steven Valdez143e8b32016-07-11 13:19:03 -04002938 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2939 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002940 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002941 return 0;
2942 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002943
Steven Valdez143e8b32016-07-11 13:19:03 -04002944 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002945
Steven Valdez143e8b32016-07-11 13:19:03 -04002946 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002947 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002948 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002949 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04002950 *out_alert = alert;
2951 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002952 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002953 }
Adam Langley95c29f32014-06-20 12:00:00 -07002954
David Benjamin54091232016-09-05 12:47:25 -04002955 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002956 if (!(received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002957 // Extension wasn't observed so call the callback with a NULL
2958 // parameter.
Adam Langley614c66a2015-06-12 15:26:58 -07002959 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002960 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002961 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002962 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002963 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002964 return 0;
2965 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002966 }
2967 }
Adam Langley95c29f32014-06-20 12:00:00 -07002968
Adam Langleyfcf25832014-12-18 17:42:32 -08002969 return 1;
2970}
Adam Langley95c29f32014-06-20 12:00:00 -07002971
David Benjamin8c880a22016-12-03 02:20:34 -05002972static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
2973 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002974 int ret = SSL_TLSEXT_ERR_NOACK;
2975 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002976
David Benjamin78f8aab2016-03-10 16:33:58 -05002977 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002978 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002979 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05002980 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
2981 ret = ssl->session_ctx->tlsext_servername_callback(
2982 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002983 }
Adam Langley95c29f32014-06-20 12:00:00 -07002984
Adam Langleyfcf25832014-12-18 17:42:32 -08002985 switch (ret) {
2986 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002987 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002988 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002989
Adam Langleyfcf25832014-12-18 17:42:32 -08002990 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05002991 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002992 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002993
Adam Langleyfcf25832014-12-18 17:42:32 -08002994 default:
2995 return 1;
2996 }
2997}
Adam Langleyed8270a2014-09-02 13:52:56 -07002998
David Benjamin8c880a22016-12-03 02:20:34 -05002999int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3000 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08003001 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003002 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003003 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003004 return 0;
3005 }
3006
Adam Langleyfcf25832014-12-18 17:42:32 -08003007 return 1;
3008}
Adam Langley95c29f32014-06-20 12:00:00 -07003009
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003010static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
3011 uint8_t **out, size_t *out_len, EVP_CIPHER_CTX *cipher_ctx,
3012 HMAC_CTX *hmac_ctx, const uint8_t *ticket, size_t ticket_len) {
3013 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003014
David Benjaminc11ea9422017-08-29 16:33:21 -04003015 // Check the MAC at the end of the ticket.
David Benjamine3aa1d92015-06-16 15:34:50 -04003016 uint8_t mac[EVP_MAX_MD_SIZE];
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003017 size_t mac_len = HMAC_size(hmac_ctx);
David Benjamine3aa1d92015-06-16 15:34:50 -04003018 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003019 // The ticket must be large enough for key name, IV, data, and MAC.
David Benjamin81678aa2017-07-12 22:43:42 -04003020 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003021 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003022 HMAC_Update(hmac_ctx, ticket, ticket_len - mac_len);
3023 HMAC_Final(hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003024 int mac_ok =
3025 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3026#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3027 mac_ok = 1;
3028#endif
3029 if (!mac_ok) {
David Benjamin81678aa2017-07-12 22:43:42 -04003030 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003031 }
3032
David Benjaminc11ea9422017-08-29 16:33:21 -04003033 // Decrypt the session data.
David Benjamine3aa1d92015-06-16 15:34:50 -04003034 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3035 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3036 mac_len;
David Benjamin86e95b82017-07-18 16:34:25 -04003037 UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len));
David Benjamin81678aa2017-07-12 22:43:42 -04003038 if (!plaintext) {
3039 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08003040 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003041 size_t plaintext_len;
3042#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin81678aa2017-07-12 22:43:42 -04003043 OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003044 plaintext_len = ciphertext_len;
3045#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003046 if (ciphertext_len >= INT_MAX) {
David Benjamin81678aa2017-07-12 22:43:42 -04003047 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003048 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003049 int len1, len2;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003050 if (!EVP_DecryptUpdate(cipher_ctx, plaintext.get(), &len1, ciphertext,
David Benjamine3aa1d92015-06-16 15:34:50 -04003051 (int)ciphertext_len) ||
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003052 !EVP_DecryptFinal_ex(cipher_ctx, plaintext.get() + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003053 ERR_clear_error();
David Benjamin81678aa2017-07-12 22:43:42 -04003054 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003055 }
Adam Langley4c341d02017-03-08 19:33:21 -08003056 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04003057#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003058
David Benjamin81678aa2017-07-12 22:43:42 -04003059 *out = plaintext.release();
Adam Langley4c341d02017-03-08 19:33:21 -08003060 *out_len = plaintext_len;
David Benjamin81678aa2017-07-12 22:43:42 -04003061 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08003062}
3063
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003064static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
3065 SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
3066 const uint8_t *ticket, size_t ticket_len) {
3067 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3068 ScopedEVP_CIPHER_CTX cipher_ctx;
3069 ScopedHMAC_CTX hmac_ctx;
3070 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3071 int cb_ret = ssl->session_ctx->tlsext_ticket_key_cb(
3072 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(),
3073 hmac_ctx.get(), 0 /* decrypt */);
3074 if (cb_ret < 0) {
3075 return ssl_ticket_aead_error;
3076 } else if (cb_ret == 0) {
3077 return ssl_ticket_aead_ignore_ticket;
3078 } else if (cb_ret == 2) {
3079 *out_renew_ticket = 1;
3080 } else {
3081 assert(cb_ret == 1);
3082 }
3083 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3084 hmac_ctx.get(), ticket, ticket_len);
3085}
3086
3087static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
3088 SSL *ssl, uint8_t **out, size_t *out_len, const uint8_t *ticket,
3089 size_t ticket_len) {
3090 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3091 SSL_CTX *ctx = ssl->session_ctx;
3092
David Benjaminc11ea9422017-08-29 16:33:21 -04003093 // Rotate the ticket key if necessary.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003094 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
3095 return ssl_ticket_aead_error;
3096 }
3097
David Benjaminc11ea9422017-08-29 16:33:21 -04003098 // Pick the matching ticket key and decrypt.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003099 ScopedEVP_CIPHER_CTX cipher_ctx;
3100 ScopedHMAC_CTX hmac_ctx;
3101 {
3102 MutexReadLock lock(&ctx->lock);
3103 const tlsext_ticket_key *key;
3104 if (ctx->tlsext_ticket_key_current &&
3105 !OPENSSL_memcmp(ctx->tlsext_ticket_key_current->name, ticket,
3106 SSL_TICKET_KEY_NAME_LEN)) {
3107 key = ctx->tlsext_ticket_key_current;
3108 } else if (ctx->tlsext_ticket_key_prev &&
3109 !OPENSSL_memcmp(ctx->tlsext_ticket_key_prev->name, ticket,
3110 SSL_TICKET_KEY_NAME_LEN)) {
3111 key = ctx->tlsext_ticket_key_prev;
3112 } else {
3113 return ssl_ticket_aead_ignore_ticket;
3114 }
3115 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3116 if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
3117 tlsext_tick_md(), NULL) ||
3118 !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL,
3119 key->aes_key, iv)) {
3120 return ssl_ticket_aead_error;
3121 }
3122 }
3123 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3124 hmac_ctx.get(), ticket, ticket_len);
3125}
3126
Adam Langley4c341d02017-03-08 19:33:21 -08003127static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
3128 SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
3129 const uint8_t *ticket, size_t ticket_len) {
David Benjamin81678aa2017-07-12 22:43:42 -04003130 uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len);
Adam Langley4c341d02017-03-08 19:33:21 -08003131 if (plaintext == NULL) {
3132 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3133 return ssl_ticket_aead_error;
3134 }
3135
3136 size_t plaintext_len;
3137 const enum ssl_ticket_aead_result_t result =
3138 ssl->session_ctx->ticket_aead_method->open(
3139 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3140
3141 if (result == ssl_ticket_aead_success) {
3142 *out = plaintext;
3143 plaintext = NULL;
3144 *out_len = plaintext_len;
3145 }
3146
3147 OPENSSL_free(plaintext);
3148 return result;
3149}
3150
3151enum ssl_ticket_aead_result_t ssl_process_ticket(
David Benjamin37af90f2017-07-29 01:42:16 -04003152 SSL *ssl, UniquePtr<SSL_SESSION> *out_session, int *out_renew_ticket,
Adam Langley4c341d02017-03-08 19:33:21 -08003153 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3154 size_t session_id_len) {
3155 *out_renew_ticket = 0;
David Benjamin37af90f2017-07-29 01:42:16 -04003156 out_session->reset();
Adam Langley4c341d02017-03-08 19:33:21 -08003157
3158 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3159 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3160 return ssl_ticket_aead_ignore_ticket;
3161 }
3162
3163 uint8_t *plaintext = NULL;
3164 size_t plaintext_len;
3165 enum ssl_ticket_aead_result_t result;
3166 if (ssl->session_ctx->ticket_aead_method != NULL) {
3167 result = ssl_decrypt_ticket_with_method(
3168 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3169 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04003170 // Ensure there is room for the key name and the largest IV
3171 // |tlsext_ticket_key_cb| may try to consume. The real limit may be lower,
3172 // but the maximum IV length should be well under the minimum size for the
3173 // session material and HMAC.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003174 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3175 return ssl_ticket_aead_ignore_ticket;
3176 }
3177 if (ssl->session_ctx->tlsext_ticket_key_cb != NULL) {
3178 result = ssl_decrypt_ticket_with_cb(ssl, &plaintext, &plaintext_len,
3179 out_renew_ticket, ticket, ticket_len);
3180 } else {
3181 result = ssl_decrypt_ticket_with_ticket_keys(
3182 ssl, &plaintext, &plaintext_len, ticket, ticket_len);
3183 }
Adam Langley4c341d02017-03-08 19:33:21 -08003184 }
3185
3186 if (result != ssl_ticket_aead_success) {
3187 return result;
3188 }
3189
David Benjaminc11ea9422017-08-29 16:33:21 -04003190 // Decode the session.
David Benjamin37af90f2017-07-29 01:42:16 -04003191 UniquePtr<SSL_SESSION> session(
3192 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx));
Adam Langley4c341d02017-03-08 19:33:21 -08003193 OPENSSL_free(plaintext);
3194
David Benjamin37af90f2017-07-29 01:42:16 -04003195 if (!session) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003196 ERR_clear_error(); // Don't leave an error on the queue.
Adam Langley4c341d02017-03-08 19:33:21 -08003197 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003198 }
3199
David Benjaminc11ea9422017-08-29 16:33:21 -04003200 // Copy the client's session ID into the new session, to denote the ticket has
3201 // been accepted.
David Benjamin17cf2cb2016-12-13 01:07:13 -05003202 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003203 session->session_id_length = session_id_len;
3204
David Benjamin37af90f2017-07-29 01:42:16 -04003205 *out_session = std::move(session);
Adam Langley4c341d02017-03-08 19:33:21 -08003206 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003207}
Adam Langley95c29f32014-06-20 12:00:00 -07003208
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003209int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003210 // Extension ignored for inappropriate versions
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003211 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003212 return 1;
3213 }
David Benjamincd996942014-07-20 16:23:51 -04003214
David Benjamin0fc37ef2016-08-17 15:29:46 -04003215 OPENSSL_free(hs->peer_sigalgs);
3216 hs->peer_sigalgs = NULL;
3217 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003218
3219 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003220 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003221 return 0;
3222 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003223 num_sigalgs /= 2;
3224
David Benjaminc11ea9422017-08-29 16:33:21 -04003225 // supported_signature_algorithms in the certificate request is
3226 // allowed to be empty.
Steven Valdez0d62f262015-09-04 12:41:04 -04003227 if (num_sigalgs == 0) {
3228 return 1;
3229 }
3230
David Benjaminc11ea9422017-08-29 16:33:21 -04003231 // This multiplication doesn't overflow because sizeof(uint16_t) is two
3232 // and we just divided |num_sigalgs| by two.
David Benjamin81678aa2017-07-12 22:43:42 -04003233 hs->peer_sigalgs = (uint16_t *)OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003234 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003235 return 0;
3236 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003237 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003238
3239 CBS sigalgs;
3240 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003241 for (size_t i = 0; i < num_sigalgs; i++) {
3242 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003243 return 0;
3244 }
3245 }
Adam Langley95c29f32014-06-20 12:00:00 -07003246
Adam Langleyfcf25832014-12-18 17:42:32 -08003247 return 1;
3248}
David Benjaminec2f27d2014-11-13 19:17:25 -05003249
David Benjamina3651382017-04-20 17:49:36 -04003250int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
3251 switch (EVP_PKEY_id(pkey)) {
3252 case EVP_PKEY_RSA:
3253 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3254 return 1;
3255 case EVP_PKEY_EC:
3256 *out = SSL_SIGN_ECDSA_SHA1;
3257 return 1;
3258 default:
3259 return 0;
3260 }
3261}
3262
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003263int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3264 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003265 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003266
David Benjaminc11ea9422017-08-29 16:33:21 -04003267 // Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3268 // handshake.
Steven Valdezf0451ca2016-06-29 13:16:27 -04003269 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin31b0c9b2017-07-20 14:49:15 -04003270 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
David Benjamina3651382017-04-20 17:49:36 -04003271 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3272 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003273 }
David Benjamina3651382017-04-20 17:49:36 -04003274 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003275 }
3276
David Benjamin3ef76972016-10-17 17:59:54 -04003277 const uint16_t *sigalgs = cert->sigalgs;
3278 size_t num_sigalgs = cert->num_sigalgs;
3279 if (sigalgs == NULL) {
3280 sigalgs = kSignSignatureAlgorithms;
3281 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003282 }
3283
David Benjamin0fc37ef2016-08-17 15:29:46 -04003284 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3285 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3286 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003287 // If the client didn't specify any signature_algorithms extension then
3288 // we can assume that it supports SHA1. See
3289 // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
David Benjaminea9a0d52016-07-08 15:52:59 -07003290 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3291 SSL_SIGN_ECDSA_SHA1};
3292 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003293 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003294 }
3295
David Benjamin0fc37ef2016-08-17 15:29:46 -04003296 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003297 uint16_t sigalg = sigalgs[i];
David Benjaminc11ea9422017-08-29 16:33:21 -04003298 // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3299 // negotiated.
David Benjamin1fb125c2016-07-08 18:52:12 -07003300 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjamina232a712017-03-30 15:51:53 -05003301 !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003302 continue;
3303 }
3304
David Benjamin0fc37ef2016-08-17 15:29:46 -04003305 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003306 if (sigalg == peer_sigalgs[j]) {
3307 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003308 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003309 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003310 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003311 }
Adam Langley95c29f32014-06-20 12:00:00 -07003312
David Benjaminea9a0d52016-07-08 15:52:59 -07003313 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3314 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003315}
Adam Langley95c29f32014-06-20 12:00:00 -07003316
David Benjamin7934f082017-08-01 16:32:25 -04003317int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
Steven Valdez908ac192017-01-12 13:17:07 -05003318 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003319 // A Channel ID handshake message is structured to contain multiple
3320 // extensions, but the only one that can be present is Channel ID.
David Benjamin7934f082017-08-01 16:32:25 -04003321 uint16_t extension_type;
3322 CBS channel_id = msg.body, extension;
Nick Harper60a85cb2016-09-23 16:25:11 -07003323 if (!CBS_get_u16(&channel_id, &extension_type) ||
3324 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3325 CBS_len(&channel_id) != 0 ||
3326 extension_type != TLSEXT_TYPE_channel_id ||
3327 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3328 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3329 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3330 return 0;
3331 }
3332
David Benjamin86e95b82017-07-18 16:34:25 -04003333 UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Nick Harper60a85cb2016-09-23 16:25:11 -07003334 if (!p256) {
3335 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3336 return 0;
3337 }
3338
David Benjamin86e95b82017-07-18 16:34:25 -04003339 UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3340 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
David Benjamin81678aa2017-07-12 22:43:42 -04003341 if (!sig || !x || !y) {
3342 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003343 }
3344
3345 const uint8_t *p = CBS_data(&extension);
David Benjamin81678aa2017-07-12 22:43:42 -04003346 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3347 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3348 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3349 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
3350 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003351 }
3352
David Benjamin86e95b82017-07-18 16:34:25 -04003353 UniquePtr<EC_KEY> key(EC_KEY_new());
3354 UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
David Benjamin81678aa2017-07-12 22:43:42 -04003355 if (!key || !point ||
3356 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3357 y.get(), nullptr) ||
3358 !EC_KEY_set_group(key.get(), p256.get()) ||
3359 !EC_KEY_set_public_key(key.get(), point.get())) {
3360 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003361 }
3362
3363 uint8_t digest[EVP_MAX_MD_SIZE];
3364 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003365 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003366 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003367 }
3368
David Benjamin81678aa2017-07-12 22:43:42 -04003369 int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
Nick Harper60a85cb2016-09-23 16:25:11 -07003370#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3371 sig_ok = 1;
3372#endif
3373 if (!sig_ok) {
3374 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3375 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3376 ssl->s3->tlsext_channel_id_valid = 0;
David Benjamin81678aa2017-07-12 22:43:42 -04003377 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003378 }
3379
David Benjamin17cf2cb2016-12-13 01:07:13 -05003380 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
David Benjamin81678aa2017-07-12 22:43:42 -04003381 return 1;
Nick Harper60a85cb2016-09-23 16:25:11 -07003382}
3383
Steven Valdez908ac192017-01-12 13:17:07 -05003384int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3385 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003386 uint8_t digest[EVP_MAX_MD_SIZE];
3387 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003388 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003389 return 0;
3390 }
3391
3392 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3393 if (ec_key == NULL) {
3394 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3395 return 0;
3396 }
3397
3398 int ret = 0;
3399 BIGNUM *x = BN_new();
3400 BIGNUM *y = BN_new();
3401 ECDSA_SIG *sig = NULL;
3402 if (x == NULL || y == NULL ||
3403 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3404 EC_KEY_get0_public_key(ec_key),
3405 x, y, NULL)) {
3406 goto err;
3407 }
3408
3409 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3410 if (sig == NULL) {
3411 goto err;
3412 }
3413
3414 CBB child;
3415 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3416 !CBB_add_u16_length_prefixed(cbb, &child) ||
3417 !BN_bn2cbb_padded(&child, 32, x) ||
3418 !BN_bn2cbb_padded(&child, 32, y) ||
3419 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3420 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3421 !CBB_flush(cbb)) {
3422 goto err;
3423 }
3424
3425 ret = 1;
3426
3427err:
3428 BN_free(x);
3429 BN_free(y);
3430 ECDSA_SIG_free(sig);
3431 return ret;
3432}
3433
Steven Valdez908ac192017-01-12 13:17:07 -05003434int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3435 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003436 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3437 uint8_t *msg;
3438 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003439 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003440 ssl_cert_verify_channel_id)) {
3441 return 0;
3442 }
3443 SHA256(msg, msg_len, out);
3444 *out_len = SHA256_DIGEST_LENGTH;
3445 OPENSSL_free(msg);
3446 return 1;
3447 }
3448
Nick Harper95594012016-10-20 14:07:13 -07003449 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003450
Nick Harper95594012016-10-20 14:07:13 -07003451 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003452 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003453 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003454
Steven Valdez87eab492016-06-27 16:34:59 -04003455 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003456 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003457 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003458 if (ssl->session->original_handshake_hash_len == 0) {
3459 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003460 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003461 }
Nick Harper95594012016-10-20 14:07:13 -07003462 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3463 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003464 }
3465
Steven Valdez908ac192017-01-12 13:17:07 -05003466 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3467 size_t hs_hash_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003468 if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003469 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003470 }
Steven Valdez908ac192017-01-12 13:17:07 -05003471 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003472 SHA256_Final(out, &ctx);
3473 *out_len = SHA256_DIGEST_LENGTH;
3474 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003475}
Adam Langley1258b6a2014-06-20 12:00:00 -07003476
David Benjaminc11ea9422017-08-29 16:33:21 -04003477// tls1_record_handshake_hashes_for_channel_id records the current handshake
3478// hashes in |hs->new_session| so that Channel ID resumptions can sign that
3479// data.
Steven Valdez908ac192017-01-12 13:17:07 -05003480int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3481 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003482 // This function should never be called for a resumed session because the
3483 // handshake hashes that we wish to record are for the original, full
3484 // handshake.
Steven Valdez87eab492016-06-27 16:34:59 -04003485 if (ssl->session != NULL) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04003486 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003487 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003488
David Benjamina3d76d02017-07-14 19:36:07 -04003489 static_assert(
David Benjamin45738dd2017-02-09 20:01:26 -05003490 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
David Benjamina3d76d02017-07-14 19:36:07 -04003491 "original_handshake_hash is too small");
Steven Valdez908ac192017-01-12 13:17:07 -05003492
3493 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003494 if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
3495 &digest_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04003496 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003497 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003498
David Benjamina3d76d02017-07-14 19:36:07 -04003499 static_assert(EVP_MAX_MD_SIZE <= 0xff,
3500 "EVP_MAX_MD_SIZE does not fit in uint8_t");
David Benjamin45738dd2017-02-09 20:01:26 -05003501 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003502
Adam Langleyfcf25832014-12-18 17:42:32 -08003503 return 1;
3504}
Nick Harper60a85cb2016-09-23 16:25:11 -07003505
3506int ssl_do_channel_id_callback(SSL *ssl) {
3507 if (ssl->tlsext_channel_id_private != NULL ||
3508 ssl->ctx->channel_id_cb == NULL) {
3509 return 1;
3510 }
3511
3512 EVP_PKEY *key = NULL;
3513 ssl->ctx->channel_id_cb(ssl, &key);
3514 if (key == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003515 // The caller should try again later.
Nick Harper60a85cb2016-09-23 16:25:11 -07003516 return 1;
3517 }
3518
3519 int ret = SSL_set1_tls_channel_id(ssl, key);
3520 EVP_PKEY_free(key);
3521 return ret;
3522}
Adam Langleycfa08c32016-11-17 13:21:27 -08003523
3524int ssl_is_sct_list_valid(const CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003525 // Shallow parse the SCT list for sanity. By the RFC
3526 // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3527 // of the SCTs may be empty.
Adam Langleycfa08c32016-11-17 13:21:27 -08003528 CBS copy = *contents;
3529 CBS sct_list;
3530 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3531 CBS_len(&copy) != 0 ||
3532 CBS_len(&sct_list) == 0) {
3533 return 0;
3534 }
3535
3536 while (CBS_len(&sct_list) > 0) {
3537 CBS sct;
3538 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3539 CBS_len(&sct) == 0) {
3540 return 0;
3541 }
3542 }
3543
3544 return 1;
3545}
David Benjamin86e95b82017-07-18 16:34:25 -04003546
3547} // namespace bssl
3548
3549using namespace bssl;
3550
3551int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
3552 uint16_t extension_type,
3553 const uint8_t **out_data,
3554 size_t *out_len) {
3555 CBS cbs;
3556 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
3557 return 0;
3558 }
3559
3560 *out_data = CBS_data(&cbs);
3561 *out_len = CBS_len(&cbs);
3562 return 1;
3563}
3564
3565void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
3566 ctx->ed25519_enabled = !!enabled;
3567}
3568
3569int SSL_extension_supported(unsigned extension_value) {
3570 uint32_t index;
3571 return extension_value == TLSEXT_TYPE_padding ||
3572 tls_extension_find(&index, extension_value) != NULL;
3573}