blob: 19f256dcd91067140d2c46a950ea06708be83733 [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
Adam Langleyfcf25832014-12-18 17:42:32 -0800145/* 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
David Benjamin35a7a442014-07-05 00:23:20 -0400148 * 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
Adam Langleyfcf25832014-12-18 17:42:32 -0800155 /* First pass: count the extensions. */
156 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
Adam Langleyfcf25832014-12-18 17:42:32 -0800179 /* Second pass: gather the extension types. */
180 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)) {
186 /* This should not happen. */
187 goto done;
188 }
189 }
190 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400191
Adam Langleyfcf25832014-12-18 17:42:32 -0800192 /* Sort the extensions and make sure there are no duplicates. */
193 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 Benjamin731058e2016-12-03 23:15:13 -0500207int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out, const uint8_t *in,
208 size_t in_len) {
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;
211 out->client_hello = in;
212 out->client_hello_len = in_len;
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
Adam Langleyfcf25832014-12-18 17:42:32 -0800228 /* 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
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 /* If the ClientHello ends here then it's valid, but doesn't have any
251 * extensions. (E.g. SSLv3.) */
252 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
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 /* 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 Benjamincec73442016-08-02 17:41:33 -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 Benjaminf04976b2016-10-07 00:37:55 -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 Benjamin3ef76972016-10-17 17:59:54 -0400428/* kVerifySignatureAlgorithms is the default list of accepted signature
David Benjamin3a322f52016-10-26 12:45:35 -0400429 * 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 Benjamin69522112017-03-28 15:38:29 -0500435 /* List our preferred algorithms first. */
436 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
443 /* Larger hashes are acceptable. */
444 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 Benjamin76bb1412016-09-08 16:03:49 -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 Benjamin3a322f52016-10-26 12:45:35 -0400459 /* For now, SHA-1 is still accepted but least preferable. */
460 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400461
David Benjamin3a322f52016-10-26 12:45:35 -0400462};
463
464/* 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. */
470static const uint16_t kSignSignatureAlgorithms[] = {
David Benjamin69522112017-03-28 15:38:29 -0500471 /* List our preferred algorithms first. */
472 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 Benjamin3a322f52016-10-26 12:45:35 -0400479 /* If needed, sign larger hashes.
480 *
481 * TODO(davidben): Determine which of these may be pruned. */
482 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
494 /* 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
Adam Langley614c66a2015-06-12 15:26:58 -0700545/* 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. */
560struct 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) {
576 /* Servers MUST NOT send this extension. */
577 *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) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400587 /* This extension from the client is handled elsewhere. */
588 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
595/* Server name indication (SNI).
596 *
597 * https://tools.ietf.org/html/rfc6066#section-3. */
598
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) ||
655 /* 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. */
662 !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 Benjamin4eb95cc2016-11-16 17:08:23 +0900676 /* 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
Adam Langley5021b222015-06-12 18:27:58 -0700703/* Renegotiation indication.
704 *
705 * https://tools.ietf.org/html/rfc5746 */
706
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 Benjamin7c7d8312016-08-20 13:39:03 -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 Benjamin3e052de2015-11-25 20:10:31 -0500738 /* Servers may not switch between omitting the extension and supporting it.
739 * See RFC 5746, sections 3.5 and 4.2. */
740 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 Benjamine9cddb82015-11-23 14:36:40 -0500748 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700749 * 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 *
David Benjamine9cddb82015-11-23 14:36:40 -0500753 * 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. */
755 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
761 /* Check for logic errors */
762 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
769 /* Parse out the extension contents. */
770 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
778 /* Check that the extension matches. */
779 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 Langley5021b222015-06-12 18:27:58 -0700805 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
806 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;
Adam Langley5021b222015-06-12 18:27:58 -0700816 /* Renegotiation isn't supported as a server so this function should never be
817 * called after the initial handshake. */
818 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 Benjamin52bf6902016-10-08 12:05:03 -0400835 /* Check that the extension matches. We do not support renegotiation as a
836 * server, so this must be empty. */
837 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 Benjamin52bf6902016-10-08 12:05:03 -0400850 /* Renegotiation isn't supported as a server so this function should never be
851 * called after the initial handshake. */
852 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
868/* Extended Master Secret.
869 *
David Benjamin43946d42016-02-01 08:42:19 -0500870 * 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 Benjamin7c7d8312016-08-20 13:39:03 -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 Benjaminfc02b592017-02-17 16:26:01 -0500900 /* Whether EMS is negotiated may not change on renegotiation. */
901 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
946/* Session tickets.
947 *
948 * https://tools.ietf.org/html/rfc5077 */
949
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 Benjamin7c7d8312016-08-20 13:39:03 -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
961 /* 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. */
965 if (!ssl->s3->initial_handshake_complete &&
966 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400967 ssl->session->tlsext_tick != NULL &&
968 /* 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
Adam Langley9b05bc52015-07-01 15:25:33 -0700996 /* 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. */
999 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 Benjamin78476f62016-11-12 11:20:55 +09001014 /* 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
Adam Langley2e857bd2015-07-01 16:09:19 -07001026/* Signature Algorithms.
1027 *
1028 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1029
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
Adam Langleybb0bd042015-07-01 16:21:03 -07001070/* OCSP Stapling.
1071 *
1072 * https://tools.ietf.org/html/rfc6066#section-8 */
1073
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
Steven Valdeza833c352016-11-01 13:39:36 -04001100 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1101 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001102 return 0;
1103 }
1104
Steven Valdeza833c352016-11-01 13:39:36 -04001105 /* OCSP stapling is forbidden on non-certificate ciphers. */
1106 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
Steven Valdeza833c352016-11-01 13:39:36 -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
1130 /* 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
Adam Langley97dfcbf2015-07-01 18:35:20 -07001154/* Next protocol negotiation.
1155 *
1156 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1157
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
Adam Langley97dfcbf2015-07-01 18:35:20 -07001185 /* 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. */
1188 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) {
1193 /* NPN and ALPN may not be negotiated in the same connection. */
1194 *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;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001257 /* |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
Adam Langleyab8d87d2015-07-10 12:21:39 -07001285/* Signed certificate timestamps.
1286 *
1287 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1288
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
Steven Valdeza833c352016-11-01 13:39:36 -04001310 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1311 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
Adam Langleyab8d87d2015-07-10 12:21:39 -07001316 /* 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. */
1318 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 Benjamindaa88502016-10-04 16:32:16 -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. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001330 if (!ssl->s3->session_reused &&
David Benjamin45738dd2017-02-09 20:01:26 -05001331 !CBS_stow(contents, &hs->new_session->tlsext_signed_cert_timestamp_list,
1332 &hs->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001333 *out_alert = SSL_AD_INTERNAL_ERROR;
1334 return 0;
1335 }
1336
1337 return 1;
1338}
1339
David Benjamin8c880a22016-12-03 02:20:34 -05001340static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001341 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001342 if (contents == NULL) {
1343 return 1;
1344 }
1345
1346 if (CBS_len(contents) != 0) {
1347 return 0;
1348 }
1349
David Benjamin8c880a22016-12-03 02:20:34 -05001350 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001351 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001352}
1353
David Benjamin8c880a22016-12-03 02:20:34 -05001354static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1355 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001356 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001357 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1358 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001359 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001360 return 1;
1361 }
1362
1363 CBB contents;
1364 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1365 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001366 CBB_add_bytes(
1367 &contents,
1368 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1369 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001370 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001371}
1372
1373
Adam Langleyf18e4532015-07-10 13:39:53 -07001374/* Application-level Protocol Negotiation.
1375 *
1376 * https://tools.ietf.org/html/rfc7301 */
1377
David Benjamin8c880a22016-12-03 02:20:34 -05001378static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1379 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001380 if (ssl->alpn_client_proto_list == NULL ||
1381 ssl->s3->initial_handshake_complete) {
1382 return 1;
1383 }
1384
1385 CBB contents, proto_list;
1386 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1387 !CBB_add_u16_length_prefixed(out, &contents) ||
1388 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1389 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1390 ssl->alpn_client_proto_list_len) ||
1391 !CBB_flush(out)) {
1392 return 0;
1393 }
1394
1395 return 1;
1396}
1397
David Benjamin8c880a22016-12-03 02:20:34 -05001398static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001399 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001400 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001401 if (contents == NULL) {
1402 return 1;
1403 }
1404
1405 assert(!ssl->s3->initial_handshake_complete);
1406 assert(ssl->alpn_client_proto_list != NULL);
1407
David Benjamin8c880a22016-12-03 02:20:34 -05001408 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001409 /* NPN and ALPN may not be negotiated in the same connection. */
1410 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1411 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1412 return 0;
1413 }
1414
Adam Langleyf18e4532015-07-10 13:39:53 -07001415 /* The extension data consists of a ProtocolNameList which must have
1416 * exactly one ProtocolName. Each of these is length-prefixed. */
1417 CBS protocol_name_list, protocol_name;
1418 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1419 CBS_len(contents) != 0 ||
1420 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1421 /* Empty protocol names are forbidden. */
1422 CBS_len(&protocol_name) == 0 ||
1423 CBS_len(&protocol_name_list) != 0) {
1424 return 0;
1425 }
1426
David Benjaminc8ff30c2017-04-04 13:52:36 -04001427 if (!ssl->ctx->allow_unknown_alpn_protos) {
1428 /* Check that the protocol name is one of the ones we advertised. */
1429 int protocol_ok = 0;
1430 CBS client_protocol_name_list, client_protocol_name;
1431 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1432 ssl->alpn_client_proto_list_len);
1433 while (CBS_len(&client_protocol_name_list) > 0) {
1434 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1435 &client_protocol_name)) {
1436 *out_alert = SSL_AD_INTERNAL_ERROR;
1437 return 0;
1438 }
1439
1440 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1441 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1442 CBS_data(&protocol_name),
1443 CBS_len(&protocol_name)) == 0) {
1444 protocol_ok = 1;
1445 break;
1446 }
1447 }
1448
1449 if (!protocol_ok) {
1450 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1451 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3e517572016-08-11 11:52:23 -04001452 return 0;
1453 }
David Benjamin3e517572016-08-11 11:52:23 -04001454 }
1455
Adam Langleyf18e4532015-07-10 13:39:53 -07001456 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1457 &ssl->s3->alpn_selected_len)) {
1458 *out_alert = SSL_AD_INTERNAL_ERROR;
1459 return 0;
1460 }
1461
1462 return 1;
1463}
1464
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001465int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001466 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001467 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001468 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001469 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001470 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001471 client_hello, &contents,
1472 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1473 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001474 return 1;
1475 }
1476
1477 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001478 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001479
1480 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001481 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1482 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001483 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001484 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1485 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001486 return 0;
1487 }
1488
1489 /* Validate the protocol list. */
1490 CBS protocol_name_list_copy = protocol_name_list;
1491 while (CBS_len(&protocol_name_list_copy) > 0) {
1492 CBS protocol_name;
1493
1494 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1495 /* Empty protocol names are forbidden. */
1496 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001497 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1498 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001499 return 0;
1500 }
1501 }
1502
1503 const uint8_t *selected;
1504 uint8_t selected_len;
1505 if (ssl->ctx->alpn_select_cb(
1506 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1507 CBS_len(&protocol_name_list),
1508 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1509 OPENSSL_free(ssl->s3->alpn_selected);
David Benjamin81678aa2017-07-12 22:43:42 -04001510 ssl->s3->alpn_selected = (uint8_t *)BUF_memdup(selected, selected_len);
Adam Langleyf18e4532015-07-10 13:39:53 -07001511 if (ssl->s3->alpn_selected == NULL) {
1512 *out_alert = SSL_AD_INTERNAL_ERROR;
1513 return 0;
1514 }
1515 ssl->s3->alpn_selected_len = selected_len;
1516 }
1517
1518 return 1;
1519}
1520
David Benjamin8c880a22016-12-03 02:20:34 -05001521static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1522 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001523 if (ssl->s3->alpn_selected == NULL) {
1524 return 1;
1525 }
1526
1527 CBB contents, proto_list, proto;
1528 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1529 !CBB_add_u16_length_prefixed(out, &contents) ||
1530 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1531 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001532 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1533 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001534 !CBB_flush(out)) {
1535 return 0;
1536 }
1537
1538 return 1;
1539}
1540
1541
Adam Langley49c7af12015-07-10 14:33:46 -07001542/* Channel ID.
1543 *
1544 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1545
David Benjamin8c880a22016-12-03 02:20:34 -05001546static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1547 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001548}
1549
David Benjamin8c880a22016-12-03 02:20:34 -05001550static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1551 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001552 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001553 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001554 return 1;
1555 }
1556
1557 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1558 !CBB_add_u16(out, 0 /* length */)) {
1559 return 0;
1560 }
1561
1562 return 1;
1563}
1564
David Benjamin8c880a22016-12-03 02:20:34 -05001565static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1566 uint8_t *out_alert, CBS *contents) {
1567 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001568 if (contents == NULL) {
1569 return 1;
1570 }
1571
David Benjamince079fd2016-08-02 16:22:34 -04001572 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001573 assert(ssl->tlsext_channel_id_enabled);
1574
1575 if (CBS_len(contents) != 0) {
1576 return 0;
1577 }
1578
1579 ssl->s3->tlsext_channel_id_valid = 1;
1580 return 1;
1581}
1582
David Benjamin8c880a22016-12-03 02:20:34 -05001583static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1584 uint8_t *out_alert, CBS *contents) {
1585 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001586 if (contents == NULL ||
1587 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001588 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001589 return 1;
1590 }
1591
1592 if (CBS_len(contents) != 0) {
1593 return 0;
1594 }
1595
1596 ssl->s3->tlsext_channel_id_valid = 1;
1597 return 1;
1598}
1599
David Benjamin8c880a22016-12-03 02:20:34 -05001600static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1601 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001602 if (!ssl->s3->tlsext_channel_id_valid) {
1603 return 1;
1604 }
1605
1606 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1607 !CBB_add_u16(out, 0 /* length */)) {
1608 return 0;
1609 }
1610
1611 return 1;
1612}
1613
Adam Langley391250d2015-07-15 19:06:07 -07001614
1615/* Secure Real-time Transport Protocol (SRTP) extension.
1616 *
1617 * https://tools.ietf.org/html/rfc5764 */
1618
Adam Langley391250d2015-07-15 19:06:07 -07001619
David Benjamin8c880a22016-12-03 02:20:34 -05001620static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1621 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001622}
1623
David Benjamin8c880a22016-12-03 02:20:34 -05001624static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1625 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001626 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1627 if (profiles == NULL) {
1628 return 1;
1629 }
1630 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1631 if (num_profiles == 0) {
1632 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 Benjamin54091232016-09-05 12:47:25 -04001642 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001643 if (!CBB_add_u16(&profile_ids,
1644 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1645 return 0;
1646 }
1647 }
1648
1649 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1650 !CBB_flush(out)) {
1651 return 0;
1652 }
1653
1654 return 1;
1655}
1656
David Benjamin8c880a22016-12-03 02:20:34 -05001657static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001658 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001659 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001660 if (contents == NULL) {
1661 return 1;
1662 }
1663
1664 /* The extension consists of a u16-prefixed profile ID list containing a
1665 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1666 *
1667 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1668 CBS profile_ids, srtp_mki;
1669 uint16_t profile_id;
1670 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1671 !CBS_get_u16(&profile_ids, &profile_id) ||
1672 CBS_len(&profile_ids) != 0 ||
1673 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1674 CBS_len(contents) != 0) {
1675 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1676 return 0;
1677 }
1678
1679 if (CBS_len(&srtp_mki) != 0) {
1680 /* Must be no MKI, since we never offer one. */
1681 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1682 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1683 return 0;
1684 }
1685
1686 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1687
1688 /* Check to see if the server gave us something we support (and presumably
1689 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001690 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001691 const SRTP_PROTECTION_PROFILE *profile =
1692 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1693
1694 if (profile->id == profile_id) {
1695 ssl->srtp_profile = profile;
1696 return 1;
1697 }
1698 }
1699
1700 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1701 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1702 return 0;
1703}
1704
David Benjamin8c880a22016-12-03 02:20:34 -05001705static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001706 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001707 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001708 if (contents == NULL) {
1709 return 1;
1710 }
1711
1712 CBS profile_ids, srtp_mki;
1713 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1714 CBS_len(&profile_ids) < 2 ||
1715 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1716 CBS_len(contents) != 0) {
1717 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1718 return 0;
1719 }
1720 /* Discard the MKI value for now. */
1721
1722 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1723 SSL_get_srtp_profiles(ssl);
1724
1725 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001726 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001727 const SRTP_PROTECTION_PROFILE *server_profile =
1728 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1729
1730 CBS profile_ids_tmp;
1731 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1732
1733 while (CBS_len(&profile_ids_tmp) > 0) {
1734 uint16_t profile_id;
1735 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1736 return 0;
1737 }
1738
1739 if (server_profile->id == profile_id) {
1740 ssl->srtp_profile = server_profile;
1741 return 1;
1742 }
1743 }
1744 }
1745
1746 return 1;
1747}
1748
David Benjamin8c880a22016-12-03 02:20:34 -05001749static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1750 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001751 if (ssl->srtp_profile == NULL) {
1752 return 1;
1753 }
1754
1755 CBB contents, profile_ids;
1756 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1757 !CBB_add_u16_length_prefixed(out, &contents) ||
1758 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1759 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1760 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1761 !CBB_flush(out)) {
1762 return 0;
1763 }
1764
1765 return 1;
1766}
1767
Adam Langleybdd5d662015-07-20 16:19:08 -07001768
1769/* EC point formats.
1770 *
1771 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1772
David Benjamin8c880a22016-12-03 02:20:34 -05001773static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001774 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001775 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1776 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001777 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1778 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001779 !CBB_flush(out)) {
1780 return 0;
1781 }
1782
1783 return 1;
1784}
1785
David Benjamin8c880a22016-12-03 02:20:34 -05001786static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001787 /* The point format extension is unneccessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -04001788 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin70aba262016-11-01 12:08:15 -04001789 return 1;
1790 }
1791
David Benjamin8c880a22016-12-03 02:20:34 -05001792 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001793}
1794
David Benjamin8c880a22016-12-03 02:20:34 -05001795static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001796 CBS *contents) {
1797 if (contents == NULL) {
1798 return 1;
1799 }
1800
David Benjamin8c880a22016-12-03 02:20:34 -05001801 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001802 return 0;
1803 }
1804
Adam Langleybdd5d662015-07-20 16:19:08 -07001805 CBS ec_point_format_list;
1806 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1807 CBS_len(contents) != 0) {
1808 return 0;
1809 }
1810
David Benjaminfc059942015-07-30 23:01:59 -04001811 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1812 * point format. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05001813 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1814 TLSEXT_ECPOINTFORMAT_uncompressed,
1815 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001816 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001817 return 0;
1818 }
1819
1820 return 1;
1821}
1822
David Benjamin8c880a22016-12-03 02:20:34 -05001823static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001824 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001825 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001826 return 1;
1827 }
1828
David Benjamin8c880a22016-12-03 02:20:34 -05001829 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001830}
1831
David Benjamin8c880a22016-12-03 02:20:34 -05001832static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1833 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001834 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1835 return 1;
1836 }
1837
David Benjamin45738dd2017-02-09 20:01:26 -05001838 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1839 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001840 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001841
1842 if (!using_ecc) {
1843 return 1;
1844 }
1845
David Benjamin8c880a22016-12-03 02:20:34 -05001846 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001847}
1848
Steven Valdeza833c352016-11-01 13:39:36 -04001849
Steven Valdez4aa154e2016-07-29 14:32:55 -04001850/* Pre Shared Key
1851 *
Steven Valdeza833c352016-11-01 13:39:36 -04001852 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001853
David Benjamin8c880a22016-12-03 02:20:34 -05001854static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1855 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001856 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001857 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001858 return 0;
1859 }
1860
Steven Valdez8f36c512017-06-20 10:55:02 -04001861 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001862 return 15 + ssl->session->tlsext_ticklen + binder_len;
1863}
1864
David Benjamin8c880a22016-12-03 02:20:34 -05001865static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1866 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001867 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001868 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001869 return 1;
1870 }
1871
David Benjaminad8f5e12017-02-20 17:00:20 -05001872 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001873 ssl_get_current_time(ssl, &now);
1874 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1875 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1876
1877 /* Fill in a placeholder zero binder of the appropriate length. It will be
1878 * computed and filled in later after length prefixes are computed. */
1879 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez8f36c512017-06-20 10:55:02 -04001880 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001881
1882 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001883 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1884 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001885 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001886 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1887 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001888 ssl->session->tlsext_ticklen) ||
1889 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1890 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1891 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1892 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001893 return 0;
1894 }
1895
David Benjamin8c880a22016-12-03 02:20:34 -05001896 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001897 return CBB_flush(out);
1898}
1899
David Benjamin8baf9632016-11-17 17:11:16 +09001900int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1901 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001902 CBS *contents) {
1903 uint16_t psk_id;
1904 if (!CBS_get_u16(contents, &psk_id) ||
1905 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001906 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001907 *out_alert = SSL_AD_DECODE_ERROR;
1908 return 0;
1909 }
1910
David Benjamineab773a2016-11-09 18:32:35 -05001911 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001912 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001913 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001914 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1915 return 0;
1916 }
1917
1918 return 1;
1919}
1920
David Benjamin35ac5b72017-03-03 15:05:56 -05001921int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001922 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001923 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04001924 /* We only process the first PSK identity since we don't support pure PSK. */
David Benjamin707af292017-03-10 17:47:18 -05001925 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001926 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001927 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001928 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001929 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001930 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001931 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001932 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001933 *out_alert = SSL_AD_DECODE_ERROR;
1934 return 0;
1935 }
1936
Steven Valdeza833c352016-11-01 13:39:36 -04001937 *out_binders = binders;
1938
David Benjaminaedf3032016-12-01 16:47:56 -05001939 /* Check the syntax of the remaining identities, but do not process them. */
1940 size_t num_identities = 1;
1941 while (CBS_len(&identities) != 0) {
1942 CBS unused_ticket;
1943 uint32_t unused_obfuscated_ticket_age;
1944 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1945 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1946 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1947 *out_alert = SSL_AD_DECODE_ERROR;
1948 return 0;
1949 }
1950
1951 num_identities++;
1952 }
1953
1954 /* Check the syntax of the binders. The value will be checked later if
1955 * resuming. */
1956 size_t num_binders = 0;
1957 while (CBS_len(&binders) != 0) {
1958 CBS binder;
1959 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1960 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1961 *out_alert = SSL_AD_DECODE_ERROR;
1962 return 0;
1963 }
1964
1965 num_binders++;
1966 }
1967
1968 if (num_identities != num_binders) {
1969 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1970 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04001971 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04001972 }
1973
David Benjamine7f60a22016-11-16 18:54:25 +09001974 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001975}
1976
David Benjamin8baf9632016-11-17 17:11:16 +09001977int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1978 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001979 return 1;
1980 }
1981
1982 CBB contents;
1983 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1984 !CBB_add_u16_length_prefixed(out, &contents) ||
1985 /* We only consider the first identity for resumption */
1986 !CBB_add_u16(&contents, 0) ||
1987 !CBB_flush(out)) {
1988 return 0;
1989 }
1990
1991 return 1;
1992}
1993
1994
Steven Valdeza833c352016-11-01 13:39:36 -04001995/* Pre-Shared Key Exchange Modes
1996 *
1997 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05001998
David Benjamin8c880a22016-12-03 02:20:34 -05001999static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2000 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04002001 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04002002 return 1;
2003 }
2004
2005 CBB contents, ke_modes;
2006 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2007 !CBB_add_u16_length_prefixed(out, &contents) ||
2008 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2009 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2010 return 0;
2011 }
2012
2013 return CBB_flush(out);
2014}
2015
David Benjamin8c880a22016-12-03 02:20:34 -05002016static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002017 uint8_t *out_alert,
2018 CBS *contents) {
2019 if (contents == NULL) {
2020 return 1;
2021 }
2022
Steven Valdeza833c352016-11-01 13:39:36 -04002023 CBS ke_modes;
2024 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2025 CBS_len(&ke_modes) == 0 ||
2026 CBS_len(contents) != 0) {
2027 *out_alert = SSL_AD_DECODE_ERROR;
2028 return 0;
2029 }
2030
2031 /* We only support tickets with PSK_DHE_KE. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05002032 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2033 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002034
2035 return 1;
2036}
2037
2038
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002039/* Early Data Indication
2040 *
2041 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2042
David Benjamin8c880a22016-12-03 02:20:34 -05002043static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002044 SSL *const ssl = hs->ssl;
Steven Valdez2d850622017-01-11 11:34:52 -05002045 if (ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04002046 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION ||
Steven Valdez2d850622017-01-11 11:34:52 -05002047 ssl->session->ticket_max_early_data == 0 ||
2048 hs->received_hello_retry_request ||
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05002049 !ssl->cert->enable_early_data) {
Steven Valdez2d850622017-01-11 11:34:52 -05002050 return 1;
2051 }
2052
2053 hs->early_data_offered = 1;
2054
2055 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2056 !CBB_add_u16(out, 0) ||
2057 !CBB_flush(out)) {
2058 return 0;
2059 }
2060
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002061 return 1;
2062}
2063
Steven Valdez2d850622017-01-11 11:34:52 -05002064static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002065 uint8_t *out_alert, CBS *contents) {
2066 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002067 if (contents == NULL) {
2068 return 1;
2069 }
2070
2071 if (CBS_len(contents) != 0) {
2072 *out_alert = SSL_AD_DECODE_ERROR;
2073 return 0;
2074 }
2075
Steven Valdez2d850622017-01-11 11:34:52 -05002076 if (!ssl->s3->session_reused) {
2077 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2078 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2079 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002080 }
Steven Valdez2d850622017-01-11 11:34:52 -05002081
2082 ssl->early_data_accepted = 1;
2083 return 1;
2084}
2085
2086static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2087 uint8_t *out_alert, CBS *contents) {
2088 SSL *const ssl = hs->ssl;
2089 if (contents == NULL ||
2090 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2091 return 1;
2092 }
2093
2094 if (CBS_len(contents) != 0) {
2095 *out_alert = SSL_AD_DECODE_ERROR;
2096 return 0;
2097 }
2098
2099 hs->early_data_offered = 1;
2100 return 1;
2101}
2102
2103static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2104 if (!hs->ssl->early_data_accepted) {
2105 return 1;
2106 }
2107
2108 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2109 !CBB_add_u16(out, 0) ||
2110 !CBB_flush(out)) {
2111 return 0;
2112 }
2113
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002114 return 1;
2115}
2116
2117
Steven Valdez143e8b32016-07-11 13:19:03 -04002118/* Key Share
2119 *
David Benjamina128a552016-10-13 14:26:33 -04002120 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002121
David Benjamin8c880a22016-12-03 02:20:34 -05002122static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2123 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002124 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002125 return 1;
2126 }
2127
2128 CBB contents, kse_bytes;
2129 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2130 !CBB_add_u16_length_prefixed(out, &contents) ||
2131 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2132 return 0;
2133 }
2134
David Benjamin8c880a22016-12-03 02:20:34 -05002135 uint16_t group_id = hs->retry_group;
2136 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002137 /* We received a HelloRetryRequest without a new curve, so there is no new
David Benjaminc642aca2017-07-19 23:28:43 -04002138 * share to append. Leave |hs->key_share| as-is. */
Steven Valdeza833c352016-11-01 13:39:36 -04002139 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002140 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2141 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002142 return 0;
2143 }
David Benjamin8c880a22016-12-03 02:20:34 -05002144 OPENSSL_free(hs->key_share_bytes);
2145 hs->key_share_bytes = NULL;
2146 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002147 if (group_id == 0) {
2148 return CBB_flush(out);
2149 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002150 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002151 /* Add a fake group. See draft-davidben-tls-grease-01. */
2152 if (ssl->ctx->grease_enabled &&
2153 (!CBB_add_u16(&kse_bytes,
2154 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2155 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2156 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2157 return 0;
2158 }
2159
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002160 /* Predict the most preferred group. */
2161 const uint16_t *groups;
2162 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002163 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002164 if (groups_len == 0) {
2165 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2166 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002167 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002168
2169 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002170 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002171
David Benjaminc642aca2017-07-19 23:28:43 -04002172 hs->key_share = SSLKeyShare::Create(group_id);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002173 CBB key_exchange;
David Benjaminc642aca2017-07-19 23:28:43 -04002174 if (!hs->key_share ||
2175 !CBB_add_u16(&kse_bytes, group_id) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002176 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjaminc642aca2017-07-19 23:28:43 -04002177 !hs->key_share->Offer(&key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002178 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002179 return 0;
2180 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002181
David Benjamin8c880a22016-12-03 02:20:34 -05002182 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002183 /* Save the contents of the extension to repeat it in the second
2184 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002185 hs->key_share_bytes_len = CBB_len(&kse_bytes);
David Benjamin81678aa2017-07-12 22:43:42 -04002186 hs->key_share_bytes =
2187 (uint8_t *)BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
David Benjamin8c880a22016-12-03 02:20:34 -05002188 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002189 return 0;
2190 }
2191 }
2192
Steven Valdez143e8b32016-07-11 13:19:03 -04002193 return CBB_flush(out);
2194}
2195
David Benjamin8baf9632016-11-17 17:11:16 +09002196int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002197 size_t *out_secret_len,
2198 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002199 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002200 uint16_t group_id;
2201 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002202 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2203 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002204 *out_alert = SSL_AD_DECODE_ERROR;
2205 return 0;
2206 }
2207
David Benjaminc642aca2017-07-19 23:28:43 -04002208 if (hs->key_share->GroupID() != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002209 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2210 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2211 return 0;
2212 }
2213
David Benjaminc642aca2017-07-19 23:28:43 -04002214 if (!hs->key_share->Finish(out_secret, out_secret_len, out_alert,
2215 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002216 *out_alert = SSL_AD_INTERNAL_ERROR;
2217 return 0;
2218 }
2219
David Benjamin45738dd2017-02-09 20:01:26 -05002220 hs->new_session->group_id = group_id;
David Benjaminc642aca2017-07-19 23:28:43 -04002221 hs->key_share.reset();
Steven Valdez143e8b32016-07-11 13:19:03 -04002222 return 1;
2223}
2224
David Benjamin8baf9632016-11-17 17:11:16 +09002225int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002226 uint8_t **out_secret,
2227 size_t *out_secret_len,
2228 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002229 uint16_t group_id;
2230 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002231 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002232 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2233 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2234 return 0;
2235 }
2236
2237 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002238 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002239 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002240 return 0;
2241 }
2242
David Benjamin7e1f9842016-09-20 19:24:40 -04002243 /* Find the corresponding key share. */
2244 int found = 0;
2245 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002246 while (CBS_len(&key_shares) > 0) {
2247 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002248 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002249 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002250 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2251 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002252 return 0;
2253 }
2254
David Benjamin7e1f9842016-09-20 19:24:40 -04002255 if (id == group_id) {
2256 if (found) {
2257 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2258 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2259 return 0;
2260 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002261
David Benjamin7e1f9842016-09-20 19:24:40 -04002262 found = 1;
2263 peer_key = peer_key_tmp;
2264 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002265 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002266 }
2267
David Benjamin7e1f9842016-09-20 19:24:40 -04002268 if (!found) {
2269 *out_found = 0;
2270 *out_secret = NULL;
2271 *out_secret_len = 0;
2272 return 1;
2273 }
2274
2275 /* Compute the DH secret. */
2276 uint8_t *secret = NULL;
2277 size_t secret_len;
David Benjamin1386aad2017-07-19 23:57:40 -04002278 ScopedCBB public_key;
David Benjaminc642aca2017-07-19 23:28:43 -04002279 UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
2280 if (!key_share ||
2281 !CBB_init(public_key.get(), 32) ||
2282 !key_share->Accept(public_key.get(), &secret, &secret_len, out_alert,
2283 CBS_data(&peer_key), CBS_len(&peer_key)) ||
David Benjamin1386aad2017-07-19 23:57:40 -04002284 !CBB_finish(public_key.get(), &hs->ecdh_public_key,
David Benjaminbf833c32017-03-30 15:45:21 -05002285 &hs->ecdh_public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002286 OPENSSL_free(secret);
Steven Valdez803c77a2016-09-06 14:13:43 -04002287 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002288 return 0;
2289 }
2290
David Benjamin7e1f9842016-09-20 19:24:40 -04002291 *out_secret = secret;
2292 *out_secret_len = secret_len;
2293 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002294 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002295}
2296
David Benjamin8baf9632016-11-17 17:11:16 +09002297int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002298 uint16_t group_id;
2299 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002300 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002301 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2302 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2303 !CBB_add_u16(&kse_bytes, group_id) ||
2304 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002305 !CBB_add_bytes(&public_key, hs->ecdh_public_key,
2306 hs->ecdh_public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002307 !CBB_flush(out)) {
2308 return 0;
2309 }
2310
David Benjaminbf833c32017-03-30 15:45:21 -05002311 OPENSSL_free(hs->ecdh_public_key);
2312 hs->ecdh_public_key = NULL;
2313 hs->ecdh_public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002314
David Benjamin45738dd2017-02-09 20:01:26 -05002315 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002316 return 1;
2317}
2318
2319
Steven Valdezfdd10992016-09-15 16:27:05 -04002320/* Supported Versions
2321 *
2322 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2323
David Benjamin8c880a22016-12-03 02:20:34 -05002324static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2325 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002326 if (hs->max_version <= TLS1_2_VERSION) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002327 return 1;
2328 }
2329
2330 CBB contents, versions;
2331 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2332 !CBB_add_u16_length_prefixed(out, &contents) ||
2333 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2334 return 0;
2335 }
2336
David Benjamind9791bf2016-09-27 16:39:52 -04002337 /* Add a fake version. See draft-davidben-tls-grease-01. */
2338 if (ssl->ctx->grease_enabled &&
2339 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2340 return 0;
2341 }
2342
Steven Valdez8f36c512017-06-20 10:55:02 -04002343 if (!ssl_add_supported_versions(hs, &versions) ||
2344 !CBB_flush(out)) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002345 return 0;
2346 }
2347
2348 return 1;
2349}
2350
2351
David Benjamin3baa6e12016-10-07 21:10:38 -04002352/* Cookie
2353 *
2354 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2355
David Benjamin8c880a22016-12-03 02:20:34 -05002356static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2357 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002358 return 1;
2359 }
2360
2361 CBB contents, cookie;
2362 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2363 !CBB_add_u16_length_prefixed(out, &contents) ||
2364 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002365 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002366 !CBB_flush(out)) {
2367 return 0;
2368 }
2369
2370 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002371 OPENSSL_free(hs->cookie);
2372 hs->cookie = NULL;
2373 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002374 return 1;
2375}
2376
2377
Steven Valdezce902a92016-05-17 11:47:53 -04002378/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002379 *
Steven Valdezce902a92016-05-17 11:47:53 -04002380 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002381 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002382
David Benjamin8c880a22016-12-03 02:20:34 -05002383static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2384 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002385 CBB contents, groups_bytes;
2386 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002387 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002388 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002389 return 0;
2390 }
2391
David Benjamin65ac9972016-09-02 21:35:25 -04002392 /* Add a fake group. See draft-davidben-tls-grease-01. */
2393 if (ssl->ctx->grease_enabled &&
2394 !CBB_add_u16(&groups_bytes,
2395 ssl_get_grease_value(ssl, ssl_grease_group))) {
2396 return 0;
2397 }
2398
Steven Valdezce902a92016-05-17 11:47:53 -04002399 const uint16_t *groups;
2400 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002401 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002402
David Benjamin54091232016-09-05 12:47:25 -04002403 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002404 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002405 return 0;
2406 }
2407 }
2408
2409 return CBB_flush(out);
2410}
2411
David Benjamin8c880a22016-12-03 02:20:34 -05002412static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2413 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002414 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002415 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2416 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002417 return 1;
2418}
2419
David Benjamin8c880a22016-12-03 02:20:34 -05002420static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2421 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002422 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002423 if (contents == NULL) {
2424 return 1;
2425 }
2426
Steven Valdezce902a92016-05-17 11:47:53 -04002427 CBS supported_group_list;
2428 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2429 CBS_len(&supported_group_list) == 0 ||
2430 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002431 CBS_len(contents) != 0) {
2432 return 0;
2433 }
2434
David Benjamin8c880a22016-12-03 02:20:34 -05002435 hs->peer_supported_group_list =
David Benjamin81678aa2017-07-12 22:43:42 -04002436 (uint16_t *)OPENSSL_malloc(CBS_len(&supported_group_list));
David Benjamin8c880a22016-12-03 02:20:34 -05002437 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002438 *out_alert = SSL_AD_INTERNAL_ERROR;
2439 return 0;
2440 }
2441
Steven Valdezce902a92016-05-17 11:47:53 -04002442 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002443 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002444 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002445 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002446 goto err;
2447 }
2448 }
2449
Steven Valdezce902a92016-05-17 11:47:53 -04002450 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002451 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002452
2453 return 1;
2454
2455err:
David Benjamin8c880a22016-12-03 02:20:34 -05002456 OPENSSL_free(hs->peer_supported_group_list);
2457 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002458 *out_alert = SSL_AD_INTERNAL_ERROR;
2459 return 0;
2460}
2461
David Benjamin8c880a22016-12-03 02:20:34 -05002462static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002463 /* Servers don't echo this extension. */
2464 return 1;
2465}
2466
2467
Adam Langley614c66a2015-06-12 15:26:58 -07002468/* kExtensions contains all the supported extensions. */
2469static const struct tls_extension kExtensions[] = {
2470 {
Adam Langley5021b222015-06-12 18:27:58 -07002471 TLSEXT_TYPE_renegotiate,
2472 NULL,
2473 ext_ri_add_clienthello,
2474 ext_ri_parse_serverhello,
2475 ext_ri_parse_clienthello,
2476 ext_ri_add_serverhello,
2477 },
2478 {
Adam Langley614c66a2015-06-12 15:26:58 -07002479 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002480 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002481 ext_sni_add_clienthello,
2482 ext_sni_parse_serverhello,
2483 ext_sni_parse_clienthello,
2484 ext_sni_add_serverhello,
2485 },
Adam Langley0a056712015-07-01 15:03:33 -07002486 {
2487 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002488 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002489 ext_ems_add_clienthello,
2490 ext_ems_parse_serverhello,
2491 ext_ems_parse_clienthello,
2492 ext_ems_add_serverhello,
2493 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002494 {
2495 TLSEXT_TYPE_session_ticket,
2496 NULL,
2497 ext_ticket_add_clienthello,
2498 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002499 /* Ticket extension client parsing is handled in ssl_session.c */
2500 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002501 ext_ticket_add_serverhello,
2502 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002503 {
2504 TLSEXT_TYPE_signature_algorithms,
2505 NULL,
2506 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002507 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002508 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002509 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002510 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002511 {
2512 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002513 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002514 ext_ocsp_add_clienthello,
2515 ext_ocsp_parse_serverhello,
2516 ext_ocsp_parse_clienthello,
2517 ext_ocsp_add_serverhello,
2518 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002519 {
2520 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002521 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002522 ext_npn_add_clienthello,
2523 ext_npn_parse_serverhello,
2524 ext_npn_parse_clienthello,
2525 ext_npn_add_serverhello,
2526 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002527 {
2528 TLSEXT_TYPE_certificate_timestamp,
2529 NULL,
2530 ext_sct_add_clienthello,
2531 ext_sct_parse_serverhello,
2532 ext_sct_parse_clienthello,
2533 ext_sct_add_serverhello,
2534 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002535 {
2536 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002537 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002538 ext_alpn_add_clienthello,
2539 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002540 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2541 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002542 ext_alpn_add_serverhello,
2543 },
Adam Langley49c7af12015-07-10 14:33:46 -07002544 {
2545 TLSEXT_TYPE_channel_id,
2546 ext_channel_id_init,
2547 ext_channel_id_add_clienthello,
2548 ext_channel_id_parse_serverhello,
2549 ext_channel_id_parse_clienthello,
2550 ext_channel_id_add_serverhello,
2551 },
Adam Langley391250d2015-07-15 19:06:07 -07002552 {
2553 TLSEXT_TYPE_srtp,
2554 ext_srtp_init,
2555 ext_srtp_add_clienthello,
2556 ext_srtp_parse_serverhello,
2557 ext_srtp_parse_clienthello,
2558 ext_srtp_add_serverhello,
2559 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002560 {
2561 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002562 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002563 ext_ec_point_add_clienthello,
2564 ext_ec_point_parse_serverhello,
2565 ext_ec_point_parse_clienthello,
2566 ext_ec_point_add_serverhello,
2567 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002568 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002569 TLSEXT_TYPE_key_share,
2570 NULL,
2571 ext_key_share_add_clienthello,
2572 forbid_parse_serverhello,
2573 ignore_parse_clienthello,
2574 dont_add_serverhello,
2575 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002576 {
Steven Valdeza833c352016-11-01 13:39:36 -04002577 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002578 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002579 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002580 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002581 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002582 dont_add_serverhello,
2583 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002584 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002585 TLSEXT_TYPE_early_data,
2586 NULL,
2587 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002588 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002589 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002590 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002591 },
2592 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002593 TLSEXT_TYPE_supported_versions,
2594 NULL,
2595 ext_supported_versions_add_clienthello,
2596 forbid_parse_serverhello,
2597 ignore_parse_clienthello,
2598 dont_add_serverhello,
2599 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002600 {
2601 TLSEXT_TYPE_cookie,
2602 NULL,
2603 ext_cookie_add_clienthello,
2604 forbid_parse_serverhello,
2605 ignore_parse_clienthello,
2606 dont_add_serverhello,
2607 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002608 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2609 * intolerant to the last extension being zero-length. See
2610 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002611 {
Steven Valdezce902a92016-05-17 11:47:53 -04002612 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002613 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002614 ext_supported_groups_add_clienthello,
2615 ext_supported_groups_parse_serverhello,
2616 ext_supported_groups_parse_clienthello,
2617 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002618 },
Adam Langley614c66a2015-06-12 15:26:58 -07002619};
2620
2621#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2622
David Benjamina3d76d02017-07-14 19:36:07 -04002623static_assert(kNumExtensions <=
2624 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
2625 "too many extensions for sent bitset");
2626static_assert(kNumExtensions <=
2627 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2628 "too many extensions for received bitset");
Adam Langley4cfa96b2015-07-01 11:56:55 -07002629
Adam Langley614c66a2015-06-12 15:26:58 -07002630static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2631 uint16_t value) {
2632 unsigned i;
2633 for (i = 0; i < kNumExtensions; i++) {
2634 if (kExtensions[i].value == value) {
2635 *out_index = i;
2636 return &kExtensions[i];
2637 }
2638 }
2639
2640 return NULL;
2641}
2642
David Benjamin8c880a22016-12-03 02:20:34 -05002643int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2644 SSL *const ssl = hs->ssl;
David Benjaminf04c2e92016-12-06 13:35:25 -05002645 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2646 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002647 !ssl->s3->send_connection_binding) {
2648 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002649 }
Adam Langley95c29f32014-06-20 12:00:00 -07002650
David Benjamine8d53502015-10-10 14:13:23 -04002651 CBB extensions;
2652 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002653 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2654 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002655 }
Adam Langley95c29f32014-06-20 12:00:00 -07002656
David Benjamin8c880a22016-12-03 02:20:34 -05002657 hs->extensions.sent = 0;
2658 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002659
David Benjamin54091232016-09-05 12:47:25 -04002660 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002661 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002662 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002663 }
2664 }
Adam Langley95c29f32014-06-20 12:00:00 -07002665
David Benjamin65ac9972016-09-02 21:35:25 -04002666 uint16_t grease_ext1 = 0;
2667 if (ssl->ctx->grease_enabled) {
2668 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2669 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2670 if (!CBB_add_u16(&extensions, grease_ext1) ||
2671 !CBB_add_u16(&extensions, 0 /* zero length */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002672 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2673 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002674 }
2675 }
2676
David Benjamin54091232016-09-05 12:47:25 -04002677 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002678 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002679 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002680 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002681 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin81678aa2017-07-12 22:43:42 -04002682 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002683 }
Adam Langley95c29f32014-06-20 12:00:00 -07002684
Adam Langley33ad2b52015-07-20 17:43:53 -07002685 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002686 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002687 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002688 }
Adam Langley75712922014-10-10 16:23:43 -07002689
David Benjamin2bd19172016-11-17 16:47:15 +09002690 if (!custom_ext_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002691 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2692 return 0;
Adam Langley09505632015-07-30 18:10:13 -07002693 }
2694
David Benjamin65ac9972016-09-02 21:35:25 -04002695 if (ssl->ctx->grease_enabled) {
2696 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2697 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2698
2699 /* The two fake extensions must not have the same value. GREASE values are
2700 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2701 * one. */
2702 if (grease_ext1 == grease_ext2) {
2703 grease_ext2 ^= 0x1010;
2704 }
2705
2706 if (!CBB_add_u16(&extensions, grease_ext2) ||
2707 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2708 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002709 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2710 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002711 }
2712 }
2713
David Benjamince079fd2016-08-02 16:22:34 -04002714 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002715 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002716 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002717 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002718 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002719 *
2720 * NB: because this code works out the length of all existing extensions
2721 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002722 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002723 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002724 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002725 * Server 7.0 is intolerant to the last extension being zero-length. See
2726 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002727 if (padding_len >= 4 + 1) {
2728 padding_len -= 4;
2729 } else {
2730 padding_len = 1;
2731 }
Adam Langley95c29f32014-06-20 12:00:00 -07002732
Adam Langley33ad2b52015-07-20 17:43:53 -07002733 uint8_t *padding_bytes;
2734 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2735 !CBB_add_u16(&extensions, padding_len) ||
2736 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002737 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2738 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002739 }
Adam Langley75712922014-10-10 16:23:43 -07002740
David Benjamin17cf2cb2016-12-13 01:07:13 -05002741 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002742 }
2743 }
Adam Langley75712922014-10-10 16:23:43 -07002744
Steven Valdeza833c352016-11-01 13:39:36 -04002745 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002746 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002747 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2748 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -04002749 }
2750
David Benjamina01deee2015-12-08 18:56:31 -05002751 /* Discard empty extensions blocks. */
2752 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002753 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002754 }
2755
David Benjamine8d53502015-10-10 14:13:23 -04002756 return CBB_flush(out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002757}
Adam Langley95c29f32014-06-20 12:00:00 -07002758
David Benjamin8c880a22016-12-03 02:20:34 -05002759int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2760 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002761 CBB extensions;
2762 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002763 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002764 }
2765
David Benjamin8c880a22016-12-03 02:20:34 -05002766 for (unsigned i = 0; i < kNumExtensions; i++) {
2767 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002768 /* Don't send extensions that were not received. */
2769 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002770 }
Adam Langley95c29f32014-06-20 12:00:00 -07002771
David Benjamin8c880a22016-12-03 02:20:34 -05002772 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002773 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002774 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002775 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002776 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002777 }
Adam Langley95c29f32014-06-20 12:00:00 -07002778
David Benjamin2bd19172016-11-17 16:47:15 +09002779 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002780 goto err;
2781 }
2782
Steven Valdez143e8b32016-07-11 13:19:03 -04002783 /* Discard empty extensions blocks before TLS 1.3. */
2784 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2785 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002786 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002787 }
2788
David Benjamin56380462015-10-10 14:59:09 -04002789 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002790
2791err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002792 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002793 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002794}
Adam Langley95c29f32014-06-20 12:00:00 -07002795
David Benjamin731058e2016-12-03 23:15:13 -05002796static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2797 const SSL_CLIENT_HELLO *client_hello,
2798 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002799 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002800 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002801 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002802 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002803 }
2804 }
2805
David Benjamin8c880a22016-12-03 02:20:34 -05002806 hs->extensions.received = 0;
2807 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002808
David Benjamine14ff062016-08-09 16:21:24 -04002809 CBS extensions;
2810 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2811 while (CBS_len(&extensions) != 0) {
2812 uint16_t type;
2813 CBS extension;
2814
2815 /* Decode the next extension. */
2816 if (!CBS_get_u16(&extensions, &type) ||
2817 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002818 *out_alert = SSL_AD_DECODE_ERROR;
2819 return 0;
2820 }
Adam Langley95c29f32014-06-20 12:00:00 -07002821
David Benjamine14ff062016-08-09 16:21:24 -04002822 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2823 * ambiguous. Ignore all but the renegotiation_info extension. */
2824 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2825 continue;
2826 }
Adam Langley95c29f32014-06-20 12:00:00 -07002827
David Benjamine14ff062016-08-09 16:21:24 -04002828 unsigned ext_index;
2829 const struct tls_extension *const ext =
2830 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002831
David Benjamine14ff062016-08-09 16:21:24 -04002832 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002833 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002834 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002835 return 0;
2836 }
David Benjamine14ff062016-08-09 16:21:24 -04002837 continue;
2838 }
2839
David Benjamin8c880a22016-12-03 02:20:34 -05002840 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002841 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002842 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002843 *out_alert = alert;
2844 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002845 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002846 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002847 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002848 }
Adam Langley75712922014-10-10 16:23:43 -07002849
David Benjamin1deb41b2016-08-09 19:36:38 -04002850 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002851 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002852 continue;
2853 }
2854
2855 CBS *contents = NULL, fake_contents;
2856 static const uint8_t kFakeRenegotiateExtension[] = {0};
2857 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2858 ssl_client_cipher_list_contains_cipher(client_hello,
2859 SSL3_CK_SCSV & 0xffff)) {
2860 /* The renegotiation SCSV was received so pretend that we received a
2861 * renegotiation extension. */
2862 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2863 sizeof(kFakeRenegotiateExtension));
2864 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002865 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002866 }
2867
2868 /* Extension wasn't observed so call the callback with a NULL
2869 * parameter. */
2870 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002871 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002872 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002873 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002874 *out_alert = alert;
2875 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002876 }
2877 }
2878
Adam Langleyfcf25832014-12-18 17:42:32 -08002879 return 1;
2880}
Adam Langley95c29f32014-06-20 12:00:00 -07002881
David Benjamin731058e2016-12-03 23:15:13 -05002882int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2883 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002884 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002885 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002886 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002887 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002888 return 0;
2889 }
Adam Langley95c29f32014-06-20 12:00:00 -07002890
David Benjamin8c880a22016-12-03 02:20:34 -05002891 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002892 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002893 return 0;
2894 }
Adam Langley95c29f32014-06-20 12:00:00 -07002895
Adam Langleyfcf25832014-12-18 17:42:32 -08002896 return 1;
2897}
Adam Langley95c29f32014-06-20 12:00:00 -07002898
David Benjamin8c880a22016-12-03 02:20:34 -05002899static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2900 int *out_alert) {
2901 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002902 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2903 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2904 return 1;
2905 }
Adam Langley614c66a2015-06-12 15:26:58 -07002906
Steven Valdez143e8b32016-07-11 13:19:03 -04002907 /* Decode the extensions block and check it is valid. */
2908 CBS extensions;
2909 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2910 !tls1_check_duplicate_extensions(&extensions)) {
2911 *out_alert = SSL_AD_DECODE_ERROR;
2912 return 0;
2913 }
2914
2915 uint32_t received = 0;
2916 while (CBS_len(&extensions) != 0) {
2917 uint16_t type;
2918 CBS extension;
2919
2920 /* Decode the next extension. */
2921 if (!CBS_get_u16(&extensions, &type) ||
2922 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002923 *out_alert = SSL_AD_DECODE_ERROR;
2924 return 0;
2925 }
Adam Langley95c29f32014-06-20 12:00:00 -07002926
Steven Valdez143e8b32016-07-11 13:19:03 -04002927 unsigned ext_index;
2928 const struct tls_extension *const ext =
2929 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002930
Steven Valdez143e8b32016-07-11 13:19:03 -04002931 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002932 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002933 return 0;
2934 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002935 continue;
2936 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002937
David Benjamina3d76d02017-07-14 19:36:07 -04002938 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
2939 "too many bits");
David Benjamin5db7c9b2017-01-24 16:17:03 -05002940
David Benjamin8c880a22016-12-03 02:20:34 -05002941 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002942 type != TLSEXT_TYPE_renegotiate) {
2943 /* If the extension was never sent then it is illegal, except for the
2944 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002945 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2946 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002947 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002948 return 0;
2949 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002950
Steven Valdez143e8b32016-07-11 13:19:03 -04002951 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002952
Steven Valdez143e8b32016-07-11 13:19:03 -04002953 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002954 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002955 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002956 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04002957 *out_alert = alert;
2958 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002959 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002960 }
Adam Langley95c29f32014-06-20 12:00:00 -07002961
David Benjamin54091232016-09-05 12:47:25 -04002962 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002963 if (!(received & (1u << i))) {
2964 /* Extension wasn't observed so call the callback with a NULL
2965 * parameter. */
2966 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002967 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002968 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002969 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002970 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002971 return 0;
2972 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002973 }
2974 }
Adam Langley95c29f32014-06-20 12:00:00 -07002975
Adam Langleyfcf25832014-12-18 17:42:32 -08002976 return 1;
2977}
Adam Langley95c29f32014-06-20 12:00:00 -07002978
David Benjamin8c880a22016-12-03 02:20:34 -05002979static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
2980 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002981 int ret = SSL_TLSEXT_ERR_NOACK;
2982 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002983
David Benjamin78f8aab2016-03-10 16:33:58 -05002984 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002985 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002986 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05002987 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
2988 ret = ssl->session_ctx->tlsext_servername_callback(
2989 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002990 }
Adam Langley95c29f32014-06-20 12:00:00 -07002991
Adam Langleyfcf25832014-12-18 17:42:32 -08002992 switch (ret) {
2993 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002994 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002995 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002996
Adam Langleyfcf25832014-12-18 17:42:32 -08002997 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05002998 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002999 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003000
Adam Langleyfcf25832014-12-18 17:42:32 -08003001 default:
3002 return 1;
3003 }
3004}
Adam Langleyed8270a2014-09-02 13:52:56 -07003005
David Benjamin8c880a22016-12-03 02:20:34 -05003006int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3007 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08003008 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003009 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003010 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003011 return 0;
3012 }
3013
Adam Langleyfcf25832014-12-18 17:42:32 -08003014 return 1;
3015}
Adam Langley95c29f32014-06-20 12:00:00 -07003016
Adam Langley4c341d02017-03-08 19:33:21 -08003017static enum ssl_ticket_aead_result_t
3018ssl_decrypt_ticket_with_cipher_ctx(SSL *ssl, uint8_t **out, size_t *out_len,
3019 int *out_renew_ticket, const uint8_t *ticket,
3020 size_t ticket_len) {
Adam Langley4c341d02017-03-08 19:33:21 -08003021 const SSL_CTX *const ssl_ctx = ssl->session_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07003022
David Benjamin86e95b82017-07-18 16:34:25 -04003023 ScopedHMAC_CTX hmac_ctx;
3024 ScopedEVP_CIPHER_CTX cipher_ctx;
David Benjamine3aa1d92015-06-16 15:34:50 -04003025
David Benjaminadcc3952015-04-26 13:07:57 -04003026 /* Ensure there is room for the key name and the largest IV
3027 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3028 * the maximum IV length should be well under the minimum size for the
3029 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003030 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
David Benjamin81678aa2017-07-12 22:43:42 -04003031 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003032 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003033 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003034
David Benjamine3aa1d92015-06-16 15:34:50 -04003035 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003036 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
David Benjamin81678aa2017-07-12 22:43:42 -04003037 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(),
3038 hmac_ctx.get(), 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003039 if (cb_ret < 0) {
David Benjamin81678aa2017-07-12 22:43:42 -04003040 return ssl_ticket_aead_error;
Adam Langley4c341d02017-03-08 19:33:21 -08003041 } else if (cb_ret == 0) {
David Benjamin81678aa2017-07-12 22:43:42 -04003042 return ssl_ticket_aead_ignore_ticket;
Adam Langley4c341d02017-03-08 19:33:21 -08003043 } else if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003044 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003045 }
3046 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003047 /* Check the key name matches. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003048 if (OPENSSL_memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3049 SSL_TICKET_KEY_NAME_LEN) != 0) {
David Benjamin81678aa2017-07-12 22:43:42 -04003050 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003051 }
David Benjamin81678aa2017-07-12 22:43:42 -04003052 if (!HMAC_Init_ex(hmac_ctx.get(), ssl_ctx->tlsext_tick_hmac_key,
David Benjamine3aa1d92015-06-16 15:34:50 -04003053 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003054 NULL) ||
David Benjamin81678aa2017-07-12 22:43:42 -04003055 !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL,
David Benjamine3aa1d92015-06-16 15:34:50 -04003056 ssl_ctx->tlsext_tick_aes_key, iv)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003057 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08003058 }
3059 }
David Benjamin81678aa2017-07-12 22:43:42 -04003060 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx.get());
Adam Langleyfcf25832014-12-18 17:42:32 -08003061
David Benjamine3aa1d92015-06-16 15:34:50 -04003062 /* Check the MAC at the end of the ticket. */
3063 uint8_t mac[EVP_MAX_MD_SIZE];
David Benjamin81678aa2017-07-12 22:43:42 -04003064 size_t mac_len = HMAC_size(hmac_ctx.get());
David Benjamine3aa1d92015-06-16 15:34:50 -04003065 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003066 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamin81678aa2017-07-12 22:43:42 -04003067 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003068 }
David Benjamin81678aa2017-07-12 22:43:42 -04003069 HMAC_Update(hmac_ctx.get(), ticket, ticket_len - mac_len);
3070 HMAC_Final(hmac_ctx.get(), mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003071 int mac_ok =
3072 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3073#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3074 mac_ok = 1;
3075#endif
3076 if (!mac_ok) {
David Benjamin81678aa2017-07-12 22:43:42 -04003077 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003078 }
3079
David Benjamine3aa1d92015-06-16 15:34:50 -04003080 /* Decrypt the session data. */
3081 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3082 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3083 mac_len;
David Benjamin86e95b82017-07-18 16:34:25 -04003084 UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len));
David Benjamin81678aa2017-07-12 22:43:42 -04003085 if (!plaintext) {
3086 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08003087 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003088 size_t plaintext_len;
3089#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin81678aa2017-07-12 22:43:42 -04003090 OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003091 plaintext_len = ciphertext_len;
3092#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003093 if (ciphertext_len >= INT_MAX) {
David Benjamin81678aa2017-07-12 22:43:42 -04003094 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003095 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003096 int len1, len2;
David Benjamin81678aa2017-07-12 22:43:42 -04003097 if (!EVP_DecryptUpdate(cipher_ctx.get(), plaintext.get(), &len1, ciphertext,
David Benjamine3aa1d92015-06-16 15:34:50 -04003098 (int)ciphertext_len) ||
David Benjamin81678aa2017-07-12 22:43:42 -04003099 !EVP_DecryptFinal_ex(cipher_ctx.get(), plaintext.get() + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003100 ERR_clear_error();
David Benjamin81678aa2017-07-12 22:43:42 -04003101 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003102 }
Adam Langley4c341d02017-03-08 19:33:21 -08003103 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04003104#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003105
David Benjamin81678aa2017-07-12 22:43:42 -04003106 *out = plaintext.release();
Adam Langley4c341d02017-03-08 19:33:21 -08003107 *out_len = plaintext_len;
David Benjamin81678aa2017-07-12 22:43:42 -04003108 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08003109}
3110
3111static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
3112 SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
3113 const uint8_t *ticket, size_t ticket_len) {
David Benjamin81678aa2017-07-12 22:43:42 -04003114 uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len);
Adam Langley4c341d02017-03-08 19:33:21 -08003115 if (plaintext == NULL) {
3116 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3117 return ssl_ticket_aead_error;
3118 }
3119
3120 size_t plaintext_len;
3121 const enum ssl_ticket_aead_result_t result =
3122 ssl->session_ctx->ticket_aead_method->open(
3123 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3124
3125 if (result == ssl_ticket_aead_success) {
3126 *out = plaintext;
3127 plaintext = NULL;
3128 *out_len = plaintext_len;
3129 }
3130
3131 OPENSSL_free(plaintext);
3132 return result;
3133}
3134
3135enum ssl_ticket_aead_result_t ssl_process_ticket(
3136 SSL *ssl, SSL_SESSION **out_session, int *out_renew_ticket,
3137 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3138 size_t session_id_len) {
3139 *out_renew_ticket = 0;
3140 *out_session = NULL;
3141
3142 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3143 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3144 return ssl_ticket_aead_ignore_ticket;
3145 }
3146
3147 uint8_t *plaintext = NULL;
3148 size_t plaintext_len;
3149 enum ssl_ticket_aead_result_t result;
3150 if (ssl->session_ctx->ticket_aead_method != NULL) {
3151 result = ssl_decrypt_ticket_with_method(
3152 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3153 } else {
3154 result = ssl_decrypt_ticket_with_cipher_ctx(
3155 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3156 }
3157
3158 if (result != ssl_ticket_aead_success) {
3159 return result;
3160 }
3161
David Benjamine3aa1d92015-06-16 15:34:50 -04003162 /* Decode the session. */
Adam Langley46db7af2017-02-01 15:49:37 -08003163 SSL_SESSION *session =
3164 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx);
Adam Langley4c341d02017-03-08 19:33:21 -08003165 OPENSSL_free(plaintext);
3166
David Benjamine3aa1d92015-06-16 15:34:50 -04003167 if (session == NULL) {
3168 ERR_clear_error(); /* Don't leave an error on the queue. */
Adam Langley4c341d02017-03-08 19:33:21 -08003169 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003170 }
3171
3172 /* Copy the client's session ID into the new session, to denote the ticket has
3173 * been accepted. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003174 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003175 session->session_id_length = session_id_len;
3176
3177 *out_session = session;
Adam Langley4c341d02017-03-08 19:33:21 -08003178 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003179}
Adam Langley95c29f32014-06-20 12:00:00 -07003180
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003181int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003182 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003183 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003184 return 1;
3185 }
David Benjamincd996942014-07-20 16:23:51 -04003186
David Benjamin0fc37ef2016-08-17 15:29:46 -04003187 OPENSSL_free(hs->peer_sigalgs);
3188 hs->peer_sigalgs = NULL;
3189 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003190
3191 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003192 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003193 return 0;
3194 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003195 num_sigalgs /= 2;
3196
3197 /* supported_signature_algorithms in the certificate request is
3198 * allowed to be empty. */
3199 if (num_sigalgs == 0) {
3200 return 1;
3201 }
3202
Steven Valdez02563852016-06-23 13:33:05 -04003203 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3204 * and we just divided |num_sigalgs| by two. */
David Benjamin81678aa2017-07-12 22:43:42 -04003205 hs->peer_sigalgs = (uint16_t *)OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003206 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003207 return 0;
3208 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003209 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003210
3211 CBS sigalgs;
3212 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003213 for (size_t i = 0; i < num_sigalgs; i++) {
3214 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003215 return 0;
3216 }
3217 }
Adam Langley95c29f32014-06-20 12:00:00 -07003218
Adam Langleyfcf25832014-12-18 17:42:32 -08003219 return 1;
3220}
David Benjaminec2f27d2014-11-13 19:17:25 -05003221
David Benjamina3651382017-04-20 17:49:36 -04003222int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
3223 switch (EVP_PKEY_id(pkey)) {
3224 case EVP_PKEY_RSA:
3225 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3226 return 1;
3227 case EVP_PKEY_EC:
3228 *out = SSL_SIGN_ECDSA_SHA1;
3229 return 1;
3230 default:
3231 return 0;
3232 }
3233}
3234
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003235int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3236 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003237 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003238
Steven Valdezf0451ca2016-06-29 13:16:27 -04003239 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
David Benjamina3651382017-04-20 17:49:36 -04003240 * handshake. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04003241 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin31b0c9b2017-07-20 14:49:15 -04003242 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
David Benjamina3651382017-04-20 17:49:36 -04003243 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3244 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003245 }
David Benjamina3651382017-04-20 17:49:36 -04003246 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003247 }
3248
David Benjamin3ef76972016-10-17 17:59:54 -04003249 const uint16_t *sigalgs = cert->sigalgs;
3250 size_t num_sigalgs = cert->num_sigalgs;
3251 if (sigalgs == NULL) {
3252 sigalgs = kSignSignatureAlgorithms;
3253 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003254 }
3255
David Benjamin0fc37ef2016-08-17 15:29:46 -04003256 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3257 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3258 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003259 /* If the client didn't specify any signature_algorithms extension then
3260 * we can assume that it supports SHA1. See
3261 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3262 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3263 SSL_SIGN_ECDSA_SHA1};
3264 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003265 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003266 }
3267
David Benjamin0fc37ef2016-08-17 15:29:46 -04003268 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003269 uint16_t sigalg = sigalgs[i];
3270 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3271 * negotiated. */
3272 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjamina232a712017-03-30 15:51:53 -05003273 !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003274 continue;
3275 }
3276
David Benjamin0fc37ef2016-08-17 15:29:46 -04003277 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003278 if (sigalg == peer_sigalgs[j]) {
3279 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003280 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003281 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003282 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003283 }
Adam Langley95c29f32014-06-20 12:00:00 -07003284
David Benjaminea9a0d52016-07-08 15:52:59 -07003285 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3286 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003287}
Adam Langley95c29f32014-06-20 12:00:00 -07003288
Steven Valdez908ac192017-01-12 13:17:07 -05003289int tls1_verify_channel_id(SSL_HANDSHAKE *hs) {
3290 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003291 uint16_t extension_type;
3292 CBS extension, channel_id;
3293
3294 /* A Channel ID handshake message is structured to contain multiple
3295 * extensions, but the only one that can be present is Channel ID. */
3296 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3297 if (!CBS_get_u16(&channel_id, &extension_type) ||
3298 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3299 CBS_len(&channel_id) != 0 ||
3300 extension_type != TLSEXT_TYPE_channel_id ||
3301 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3302 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3303 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3304 return 0;
3305 }
3306
David Benjamin86e95b82017-07-18 16:34:25 -04003307 UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Nick Harper60a85cb2016-09-23 16:25:11 -07003308 if (!p256) {
3309 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3310 return 0;
3311 }
3312
David Benjamin86e95b82017-07-18 16:34:25 -04003313 UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3314 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
David Benjamin81678aa2017-07-12 22:43:42 -04003315 if (!sig || !x || !y) {
3316 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003317 }
3318
3319 const uint8_t *p = CBS_data(&extension);
David Benjamin81678aa2017-07-12 22:43:42 -04003320 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3321 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3322 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3323 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
3324 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003325 }
3326
David Benjamin86e95b82017-07-18 16:34:25 -04003327 UniquePtr<EC_KEY> key(EC_KEY_new());
3328 UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
David Benjamin81678aa2017-07-12 22:43:42 -04003329 if (!key || !point ||
3330 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3331 y.get(), nullptr) ||
3332 !EC_KEY_set_group(key.get(), p256.get()) ||
3333 !EC_KEY_set_public_key(key.get(), point.get())) {
3334 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003335 }
3336
3337 uint8_t digest[EVP_MAX_MD_SIZE];
3338 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003339 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003340 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003341 }
3342
David Benjamin81678aa2017-07-12 22:43:42 -04003343 int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
Nick Harper60a85cb2016-09-23 16:25:11 -07003344#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3345 sig_ok = 1;
3346#endif
3347 if (!sig_ok) {
3348 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3349 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3350 ssl->s3->tlsext_channel_id_valid = 0;
David Benjamin81678aa2017-07-12 22:43:42 -04003351 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003352 }
3353
David Benjamin17cf2cb2016-12-13 01:07:13 -05003354 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
David Benjamin81678aa2017-07-12 22:43:42 -04003355 return 1;
Nick Harper60a85cb2016-09-23 16:25:11 -07003356}
3357
Steven Valdez908ac192017-01-12 13:17:07 -05003358int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3359 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003360 uint8_t digest[EVP_MAX_MD_SIZE];
3361 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003362 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003363 return 0;
3364 }
3365
3366 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3367 if (ec_key == NULL) {
3368 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3369 return 0;
3370 }
3371
3372 int ret = 0;
3373 BIGNUM *x = BN_new();
3374 BIGNUM *y = BN_new();
3375 ECDSA_SIG *sig = NULL;
3376 if (x == NULL || y == NULL ||
3377 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3378 EC_KEY_get0_public_key(ec_key),
3379 x, y, NULL)) {
3380 goto err;
3381 }
3382
3383 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3384 if (sig == NULL) {
3385 goto err;
3386 }
3387
3388 CBB child;
3389 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3390 !CBB_add_u16_length_prefixed(cbb, &child) ||
3391 !BN_bn2cbb_padded(&child, 32, x) ||
3392 !BN_bn2cbb_padded(&child, 32, y) ||
3393 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3394 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3395 !CBB_flush(cbb)) {
3396 goto err;
3397 }
3398
3399 ret = 1;
3400
3401err:
3402 BN_free(x);
3403 BN_free(y);
3404 ECDSA_SIG_free(sig);
3405 return ret;
3406}
3407
Steven Valdez908ac192017-01-12 13:17:07 -05003408int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3409 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003410 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3411 uint8_t *msg;
3412 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003413 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003414 ssl_cert_verify_channel_id)) {
3415 return 0;
3416 }
3417 SHA256(msg, msg_len, out);
3418 *out_len = SHA256_DIGEST_LENGTH;
3419 OPENSSL_free(msg);
3420 return 1;
3421 }
3422
Nick Harper95594012016-10-20 14:07:13 -07003423 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003424
Nick Harper95594012016-10-20 14:07:13 -07003425 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003426 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003427 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003428
Steven Valdez87eab492016-06-27 16:34:59 -04003429 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003430 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003431 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003432 if (ssl->session->original_handshake_hash_len == 0) {
3433 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003434 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003435 }
Nick Harper95594012016-10-20 14:07:13 -07003436 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3437 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003438 }
3439
Steven Valdez908ac192017-01-12 13:17:07 -05003440 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3441 size_t hs_hash_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003442 if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003443 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003444 }
Steven Valdez908ac192017-01-12 13:17:07 -05003445 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003446 SHA256_Final(out, &ctx);
3447 *out_len = SHA256_DIGEST_LENGTH;
3448 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003449}
Adam Langley1258b6a2014-06-20 12:00:00 -07003450
3451/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin45738dd2017-02-09 20:01:26 -05003452 * hashes in |hs->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003453 * data. */
Steven Valdez908ac192017-01-12 13:17:07 -05003454int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3455 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003456 /* This function should never be called for a resumed session because the
3457 * handshake hashes that we wish to record are for the original, full
3458 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003459 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003460 return -1;
3461 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003462
David Benjamina3d76d02017-07-14 19:36:07 -04003463 static_assert(
David Benjamin45738dd2017-02-09 20:01:26 -05003464 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
David Benjamina3d76d02017-07-14 19:36:07 -04003465 "original_handshake_hash is too small");
Steven Valdez908ac192017-01-12 13:17:07 -05003466
3467 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003468 if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
3469 &digest_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003470 return -1;
3471 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003472
David Benjamina3d76d02017-07-14 19:36:07 -04003473 static_assert(EVP_MAX_MD_SIZE <= 0xff,
3474 "EVP_MAX_MD_SIZE does not fit in uint8_t");
David Benjamin45738dd2017-02-09 20:01:26 -05003475 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003476
Adam Langleyfcf25832014-12-18 17:42:32 -08003477 return 1;
3478}
Nick Harper60a85cb2016-09-23 16:25:11 -07003479
3480int ssl_do_channel_id_callback(SSL *ssl) {
3481 if (ssl->tlsext_channel_id_private != NULL ||
3482 ssl->ctx->channel_id_cb == NULL) {
3483 return 1;
3484 }
3485
3486 EVP_PKEY *key = NULL;
3487 ssl->ctx->channel_id_cb(ssl, &key);
3488 if (key == NULL) {
3489 /* The caller should try again later. */
3490 return 1;
3491 }
3492
3493 int ret = SSL_set1_tls_channel_id(ssl, key);
3494 EVP_PKEY_free(key);
3495 return ret;
3496}
Adam Langleycfa08c32016-11-17 13:21:27 -08003497
3498int ssl_is_sct_list_valid(const CBS *contents) {
3499 /* Shallow parse the SCT list for sanity. By the RFC
3500 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3501 * of the SCTs may be empty. */
3502 CBS copy = *contents;
3503 CBS sct_list;
3504 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3505 CBS_len(&copy) != 0 ||
3506 CBS_len(&sct_list) == 0) {
3507 return 0;
3508 }
3509
3510 while (CBS_len(&sct_list) > 0) {
3511 CBS sct;
3512 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3513 CBS_len(&sct) == 0) {
3514 return 0;
3515 }
3516 }
3517
3518 return 1;
3519}
David Benjamin86e95b82017-07-18 16:34:25 -04003520
3521} // namespace bssl
3522
3523using namespace bssl;
3524
3525int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
3526 uint16_t extension_type,
3527 const uint8_t **out_data,
3528 size_t *out_len) {
3529 CBS cbs;
3530 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
3531 return 0;
3532 }
3533
3534 *out_data = CBS_data(&cbs);
3535 *out_len = CBS_len(&cbs);
3536 return 1;
3537}
3538
3539void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
3540 ctx->ed25519_enabled = !!enabled;
3541}
3542
3543int SSL_extension_supported(unsigned extension_value) {
3544 uint32_t index;
3545 return extension_value == TLSEXT_TYPE_padding ||
3546 tls_extension_find(&index, extension_value) != NULL;
3547}