blob: bbe64019a2223dbdad9b605e6ec15a5aeac7d0f4 [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 Benjamin7934f082017-08-01 16:32:25 -0400207int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out,
208 const SSLMessage &msg) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500209 OPENSSL_memset(out, 0, sizeof(*out));
David Benjamin731058e2016-12-03 23:15:13 -0500210 out->ssl = ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400211 out->client_hello = CBS_data(&msg.body);
212 out->client_hello_len = CBS_len(&msg.body);
David Benjamin8f2c20e2014-07-09 09:30:38 -0400213
David Benjamine14ff062016-08-09 16:21:24 -0400214 CBS client_hello, random, session_id;
David Benjamin731058e2016-12-03 23:15:13 -0500215 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
216 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjamine14ff062016-08-09 16:21:24 -0400217 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
218 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
219 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800220 return 0;
221 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700222
David Benjamin731058e2016-12-03 23:15:13 -0500223 out->random = CBS_data(&random);
224 out->random_len = CBS_len(&random);
225 out->session_id = CBS_data(&session_id);
226 out->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700227
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 Langley10e10602017-07-25 13:33:21 -0700805 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Adam Langley5021b222015-06-12 18:27:58 -0700806 return 0;
807 }
808 ssl->s3->send_connection_binding = 1;
809
810 return 1;
811}
812
David Benjamin8c880a22016-12-03 02:20:34 -0500813static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700814 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500815 SSL *const ssl = hs->ssl;
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 Benjamin3e51757d2016-08-11 11:52:23 -04001452 return 0;
1453 }
David Benjamin3e51757d2016-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);
David Benjaminee910bf2017-07-25 22:36:00 -04001627 if (profiles == NULL ||
1628 sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
Adam Langley391250d2015-07-15 19:06:07 -07001629 return 1;
1630 }
1631
1632 CBB contents, profile_ids;
1633 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1634 !CBB_add_u16_length_prefixed(out, &contents) ||
1635 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1636 return 0;
1637 }
1638
David Benjaminee910bf2017-07-25 22:36:00 -04001639 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
1640 if (!CBB_add_u16(&profile_ids, profile->id)) {
Adam Langley391250d2015-07-15 19:06:07 -07001641 return 0;
1642 }
1643 }
1644
1645 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1646 !CBB_flush(out)) {
1647 return 0;
1648 }
1649
1650 return 1;
1651}
1652
David Benjamin8c880a22016-12-03 02:20:34 -05001653static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001654 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001655 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001656 if (contents == NULL) {
1657 return 1;
1658 }
1659
1660 /* The extension consists of a u16-prefixed profile ID list containing a
1661 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1662 *
1663 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1664 CBS profile_ids, srtp_mki;
1665 uint16_t profile_id;
1666 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1667 !CBS_get_u16(&profile_ids, &profile_id) ||
1668 CBS_len(&profile_ids) != 0 ||
1669 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1670 CBS_len(contents) != 0) {
1671 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1672 return 0;
1673 }
1674
1675 if (CBS_len(&srtp_mki) != 0) {
1676 /* Must be no MKI, since we never offer one. */
1677 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1678 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1679 return 0;
1680 }
1681
1682 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1683
1684 /* Check to see if the server gave us something we support (and presumably
1685 * offered). */
David Benjaminee910bf2017-07-25 22:36:00 -04001686 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001687 if (profile->id == profile_id) {
1688 ssl->srtp_profile = profile;
1689 return 1;
1690 }
1691 }
1692
1693 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1694 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1695 return 0;
1696}
1697
David Benjamin8c880a22016-12-03 02:20:34 -05001698static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001699 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001700 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001701 if (contents == NULL) {
1702 return 1;
1703 }
1704
1705 CBS profile_ids, srtp_mki;
1706 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1707 CBS_len(&profile_ids) < 2 ||
1708 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1709 CBS_len(contents) != 0) {
1710 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1711 return 0;
1712 }
1713 /* Discard the MKI value for now. */
1714
1715 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1716 SSL_get_srtp_profiles(ssl);
1717
1718 /* Pick the server's most preferred profile. */
David Benjaminee910bf2017-07-25 22:36:00 -04001719 for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001720 CBS profile_ids_tmp;
1721 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1722
1723 while (CBS_len(&profile_ids_tmp) > 0) {
1724 uint16_t profile_id;
1725 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1726 return 0;
1727 }
1728
1729 if (server_profile->id == profile_id) {
1730 ssl->srtp_profile = server_profile;
1731 return 1;
1732 }
1733 }
1734 }
1735
1736 return 1;
1737}
1738
David Benjamin8c880a22016-12-03 02:20:34 -05001739static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1740 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001741 if (ssl->srtp_profile == NULL) {
1742 return 1;
1743 }
1744
1745 CBB contents, profile_ids;
1746 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1747 !CBB_add_u16_length_prefixed(out, &contents) ||
1748 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1749 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1750 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1751 !CBB_flush(out)) {
1752 return 0;
1753 }
1754
1755 return 1;
1756}
1757
Adam Langleybdd5d662015-07-20 16:19:08 -07001758
1759/* EC point formats.
1760 *
1761 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1762
David Benjamin8c880a22016-12-03 02:20:34 -05001763static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001764 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001765 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1766 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001767 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1768 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001769 !CBB_flush(out)) {
1770 return 0;
1771 }
1772
1773 return 1;
1774}
1775
David Benjamin8c880a22016-12-03 02:20:34 -05001776static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001777 /* The point format extension is unneccessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -04001778 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin70aba262016-11-01 12:08:15 -04001779 return 1;
1780 }
1781
David Benjamin8c880a22016-12-03 02:20:34 -05001782 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001783}
1784
David Benjamin8c880a22016-12-03 02:20:34 -05001785static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001786 CBS *contents) {
1787 if (contents == NULL) {
1788 return 1;
1789 }
1790
David Benjamin8c880a22016-12-03 02:20:34 -05001791 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001792 return 0;
1793 }
1794
Adam Langleybdd5d662015-07-20 16:19:08 -07001795 CBS ec_point_format_list;
1796 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1797 CBS_len(contents) != 0) {
1798 return 0;
1799 }
1800
David Benjaminfc059942015-07-30 23:01:59 -04001801 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1802 * point format. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05001803 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1804 TLSEXT_ECPOINTFORMAT_uncompressed,
1805 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001806 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001807 return 0;
1808 }
1809
1810 return 1;
1811}
1812
David Benjamin8c880a22016-12-03 02:20:34 -05001813static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001814 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001815 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001816 return 1;
1817 }
1818
David Benjamin8c880a22016-12-03 02:20:34 -05001819 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001820}
1821
David Benjamin8c880a22016-12-03 02:20:34 -05001822static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1823 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001824 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1825 return 1;
1826 }
1827
David Benjamin45738dd2017-02-09 20:01:26 -05001828 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1829 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001830 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001831
1832 if (!using_ecc) {
1833 return 1;
1834 }
1835
David Benjamin8c880a22016-12-03 02:20:34 -05001836 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001837}
1838
Steven Valdeza833c352016-11-01 13:39:36 -04001839
Steven Valdez4aa154e2016-07-29 14:32:55 -04001840/* Pre Shared Key
1841 *
Steven Valdeza833c352016-11-01 13:39:36 -04001842 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001843
David Benjamin8c880a22016-12-03 02:20:34 -05001844static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1845 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001846 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001847 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001848 return 0;
1849 }
1850
Steven Valdez8f36c512017-06-20 10:55:02 -04001851 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001852 return 15 + ssl->session->tlsext_ticklen + binder_len;
1853}
1854
David Benjamin8c880a22016-12-03 02:20:34 -05001855static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1856 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001857 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001858 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001859 return 1;
1860 }
1861
David Benjaminad8f5e12017-02-20 17:00:20 -05001862 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001863 ssl_get_current_time(ssl, &now);
1864 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1865 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1866
1867 /* Fill in a placeholder zero binder of the appropriate length. It will be
1868 * computed and filled in later after length prefixes are computed. */
1869 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez8f36c512017-06-20 10:55:02 -04001870 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001871
1872 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001873 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1874 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001875 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001876 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1877 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001878 ssl->session->tlsext_ticklen) ||
1879 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1880 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1881 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1882 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001883 return 0;
1884 }
1885
David Benjamin8c880a22016-12-03 02:20:34 -05001886 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001887 return CBB_flush(out);
1888}
1889
David Benjamin8baf9632016-11-17 17:11:16 +09001890int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1891 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001892 CBS *contents) {
1893 uint16_t psk_id;
1894 if (!CBS_get_u16(contents, &psk_id) ||
1895 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001896 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001897 *out_alert = SSL_AD_DECODE_ERROR;
1898 return 0;
1899 }
1900
David Benjamineab773a2016-11-09 18:32:35 -05001901 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001902 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001903 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001904 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1905 return 0;
1906 }
1907
1908 return 1;
1909}
1910
David Benjamin35ac5b72017-03-03 15:05:56 -05001911int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001912 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001913 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04001914 /* We only process the first PSK identity since we don't support pure PSK. */
David Benjamin707af292017-03-10 17:47:18 -05001915 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001916 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001917 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001918 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001919 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001920 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001921 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001922 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001923 *out_alert = SSL_AD_DECODE_ERROR;
1924 return 0;
1925 }
1926
Steven Valdeza833c352016-11-01 13:39:36 -04001927 *out_binders = binders;
1928
David Benjaminaedf3032016-12-01 16:47:56 -05001929 /* Check the syntax of the remaining identities, but do not process them. */
1930 size_t num_identities = 1;
1931 while (CBS_len(&identities) != 0) {
1932 CBS unused_ticket;
1933 uint32_t unused_obfuscated_ticket_age;
1934 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1935 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1936 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1937 *out_alert = SSL_AD_DECODE_ERROR;
1938 return 0;
1939 }
1940
1941 num_identities++;
1942 }
1943
1944 /* Check the syntax of the binders. The value will be checked later if
1945 * resuming. */
1946 size_t num_binders = 0;
1947 while (CBS_len(&binders) != 0) {
1948 CBS binder;
1949 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1950 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1951 *out_alert = SSL_AD_DECODE_ERROR;
1952 return 0;
1953 }
1954
1955 num_binders++;
1956 }
1957
1958 if (num_identities != num_binders) {
1959 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1960 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04001961 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04001962 }
1963
David Benjamine7f60a22016-11-16 18:54:25 +09001964 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001965}
1966
David Benjamin8baf9632016-11-17 17:11:16 +09001967int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1968 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001969 return 1;
1970 }
1971
1972 CBB contents;
1973 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1974 !CBB_add_u16_length_prefixed(out, &contents) ||
1975 /* We only consider the first identity for resumption */
1976 !CBB_add_u16(&contents, 0) ||
1977 !CBB_flush(out)) {
1978 return 0;
1979 }
1980
1981 return 1;
1982}
1983
1984
Steven Valdeza833c352016-11-01 13:39:36 -04001985/* Pre-Shared Key Exchange Modes
1986 *
1987 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05001988
David Benjamin8c880a22016-12-03 02:20:34 -05001989static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
1990 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04001991 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001992 return 1;
1993 }
1994
1995 CBB contents, ke_modes;
1996 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
1997 !CBB_add_u16_length_prefixed(out, &contents) ||
1998 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
1999 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2000 return 0;
2001 }
2002
2003 return CBB_flush(out);
2004}
2005
David Benjamin8c880a22016-12-03 02:20:34 -05002006static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002007 uint8_t *out_alert,
2008 CBS *contents) {
2009 if (contents == NULL) {
2010 return 1;
2011 }
2012
Steven Valdeza833c352016-11-01 13:39:36 -04002013 CBS ke_modes;
2014 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2015 CBS_len(&ke_modes) == 0 ||
2016 CBS_len(contents) != 0) {
2017 *out_alert = SSL_AD_DECODE_ERROR;
2018 return 0;
2019 }
2020
2021 /* We only support tickets with PSK_DHE_KE. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05002022 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2023 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002024
2025 return 1;
2026}
2027
2028
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002029/* Early Data Indication
2030 *
2031 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2032
David Benjamin8c880a22016-12-03 02:20:34 -05002033static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002034 SSL *const ssl = hs->ssl;
Steven Valdez2d850622017-01-11 11:34:52 -05002035 if (ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04002036 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION ||
Steven Valdez2d850622017-01-11 11:34:52 -05002037 ssl->session->ticket_max_early_data == 0 ||
2038 hs->received_hello_retry_request ||
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05002039 !ssl->cert->enable_early_data) {
Steven Valdez2d850622017-01-11 11:34:52 -05002040 return 1;
2041 }
2042
2043 hs->early_data_offered = 1;
2044
2045 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2046 !CBB_add_u16(out, 0) ||
2047 !CBB_flush(out)) {
2048 return 0;
2049 }
2050
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002051 return 1;
2052}
2053
Steven Valdez2d850622017-01-11 11:34:52 -05002054static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002055 uint8_t *out_alert, CBS *contents) {
2056 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002057 if (contents == NULL) {
2058 return 1;
2059 }
2060
2061 if (CBS_len(contents) != 0) {
2062 *out_alert = SSL_AD_DECODE_ERROR;
2063 return 0;
2064 }
2065
Steven Valdez2d850622017-01-11 11:34:52 -05002066 if (!ssl->s3->session_reused) {
2067 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2068 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2069 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002070 }
Steven Valdez2d850622017-01-11 11:34:52 -05002071
2072 ssl->early_data_accepted = 1;
2073 return 1;
2074}
2075
2076static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2077 uint8_t *out_alert, CBS *contents) {
2078 SSL *const ssl = hs->ssl;
2079 if (contents == NULL ||
2080 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2081 return 1;
2082 }
2083
2084 if (CBS_len(contents) != 0) {
2085 *out_alert = SSL_AD_DECODE_ERROR;
2086 return 0;
2087 }
2088
2089 hs->early_data_offered = 1;
2090 return 1;
2091}
2092
2093static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2094 if (!hs->ssl->early_data_accepted) {
2095 return 1;
2096 }
2097
2098 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2099 !CBB_add_u16(out, 0) ||
2100 !CBB_flush(out)) {
2101 return 0;
2102 }
2103
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002104 return 1;
2105}
2106
2107
Steven Valdez143e8b32016-07-11 13:19:03 -04002108/* Key Share
2109 *
David Benjamina128a552016-10-13 14:26:33 -04002110 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002111
David Benjamin8c880a22016-12-03 02:20:34 -05002112static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2113 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002114 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002115 return 1;
2116 }
2117
2118 CBB contents, kse_bytes;
2119 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2120 !CBB_add_u16_length_prefixed(out, &contents) ||
2121 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2122 return 0;
2123 }
2124
David Benjamin8c880a22016-12-03 02:20:34 -05002125 uint16_t group_id = hs->retry_group;
2126 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002127 /* We received a HelloRetryRequest without a new curve, so there is no new
David Benjaminc642aca2017-07-19 23:28:43 -04002128 * share to append. Leave |hs->key_share| as-is. */
Steven Valdeza833c352016-11-01 13:39:36 -04002129 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002130 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2131 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002132 return 0;
2133 }
David Benjamin8c880a22016-12-03 02:20:34 -05002134 OPENSSL_free(hs->key_share_bytes);
2135 hs->key_share_bytes = NULL;
2136 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002137 if (group_id == 0) {
2138 return CBB_flush(out);
2139 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002140 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002141 /* Add a fake group. See draft-davidben-tls-grease-01. */
2142 if (ssl->ctx->grease_enabled &&
2143 (!CBB_add_u16(&kse_bytes,
2144 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2145 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2146 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2147 return 0;
2148 }
2149
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002150 /* Predict the most preferred group. */
2151 const uint16_t *groups;
2152 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002153 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002154 if (groups_len == 0) {
2155 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2156 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002157 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002158
2159 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002160 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002161
David Benjaminc642aca2017-07-19 23:28:43 -04002162 hs->key_share = SSLKeyShare::Create(group_id);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002163 CBB key_exchange;
David Benjaminc642aca2017-07-19 23:28:43 -04002164 if (!hs->key_share ||
2165 !CBB_add_u16(&kse_bytes, group_id) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002166 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjaminc642aca2017-07-19 23:28:43 -04002167 !hs->key_share->Offer(&key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002168 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002169 return 0;
2170 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002171
David Benjamin8c880a22016-12-03 02:20:34 -05002172 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002173 /* Save the contents of the extension to repeat it in the second
2174 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002175 hs->key_share_bytes_len = CBB_len(&kse_bytes);
David Benjamin81678aa2017-07-12 22:43:42 -04002176 hs->key_share_bytes =
2177 (uint8_t *)BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
David Benjamin8c880a22016-12-03 02:20:34 -05002178 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002179 return 0;
2180 }
2181 }
2182
Steven Valdez143e8b32016-07-11 13:19:03 -04002183 return CBB_flush(out);
2184}
2185
David Benjamin8baf9632016-11-17 17:11:16 +09002186int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002187 size_t *out_secret_len,
2188 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002189 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002190 uint16_t group_id;
2191 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002192 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2193 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002194 *out_alert = SSL_AD_DECODE_ERROR;
2195 return 0;
2196 }
2197
David Benjaminc642aca2017-07-19 23:28:43 -04002198 if (hs->key_share->GroupID() != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002199 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2200 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2201 return 0;
2202 }
2203
David Benjaminc642aca2017-07-19 23:28:43 -04002204 if (!hs->key_share->Finish(out_secret, out_secret_len, out_alert,
2205 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002206 *out_alert = SSL_AD_INTERNAL_ERROR;
2207 return 0;
2208 }
2209
David Benjamin45738dd2017-02-09 20:01:26 -05002210 hs->new_session->group_id = group_id;
David Benjaminc642aca2017-07-19 23:28:43 -04002211 hs->key_share.reset();
Steven Valdez143e8b32016-07-11 13:19:03 -04002212 return 1;
2213}
2214
David Benjamin8baf9632016-11-17 17:11:16 +09002215int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002216 uint8_t **out_secret,
2217 size_t *out_secret_len,
2218 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002219 uint16_t group_id;
2220 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002221 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002222 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2223 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2224 return 0;
2225 }
2226
2227 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002228 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002229 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002230 return 0;
2231 }
2232
David Benjamin7e1f9842016-09-20 19:24:40 -04002233 /* Find the corresponding key share. */
2234 int found = 0;
2235 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002236 while (CBS_len(&key_shares) > 0) {
2237 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002238 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002239 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002240 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2241 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002242 return 0;
2243 }
2244
David Benjamin7e1f9842016-09-20 19:24:40 -04002245 if (id == group_id) {
2246 if (found) {
2247 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2248 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2249 return 0;
2250 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002251
David Benjamin7e1f9842016-09-20 19:24:40 -04002252 found = 1;
2253 peer_key = peer_key_tmp;
2254 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002255 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002256 }
2257
David Benjamin7e1f9842016-09-20 19:24:40 -04002258 if (!found) {
2259 *out_found = 0;
2260 *out_secret = NULL;
2261 *out_secret_len = 0;
2262 return 1;
2263 }
2264
2265 /* Compute the DH secret. */
2266 uint8_t *secret = NULL;
2267 size_t secret_len;
David Benjamin1386aad2017-07-19 23:57:40 -04002268 ScopedCBB public_key;
David Benjaminc642aca2017-07-19 23:28:43 -04002269 UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
2270 if (!key_share ||
2271 !CBB_init(public_key.get(), 32) ||
2272 !key_share->Accept(public_key.get(), &secret, &secret_len, out_alert,
2273 CBS_data(&peer_key), CBS_len(&peer_key)) ||
David Benjamin1386aad2017-07-19 23:57:40 -04002274 !CBB_finish(public_key.get(), &hs->ecdh_public_key,
David Benjaminbf833c32017-03-30 15:45:21 -05002275 &hs->ecdh_public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002276 OPENSSL_free(secret);
Steven Valdez803c77a2016-09-06 14:13:43 -04002277 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002278 return 0;
2279 }
2280
David Benjamin7e1f9842016-09-20 19:24:40 -04002281 *out_secret = secret;
2282 *out_secret_len = secret_len;
2283 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002284 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002285}
2286
David Benjamin8baf9632016-11-17 17:11:16 +09002287int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002288 uint16_t group_id;
2289 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002290 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002291 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2292 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2293 !CBB_add_u16(&kse_bytes, group_id) ||
2294 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002295 !CBB_add_bytes(&public_key, hs->ecdh_public_key,
2296 hs->ecdh_public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002297 !CBB_flush(out)) {
2298 return 0;
2299 }
2300
David Benjaminbf833c32017-03-30 15:45:21 -05002301 OPENSSL_free(hs->ecdh_public_key);
2302 hs->ecdh_public_key = NULL;
2303 hs->ecdh_public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002304
David Benjamin45738dd2017-02-09 20:01:26 -05002305 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002306 return 1;
2307}
2308
2309
Steven Valdezfdd10992016-09-15 16:27:05 -04002310/* Supported Versions
2311 *
2312 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2313
David Benjamin8c880a22016-12-03 02:20:34 -05002314static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2315 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002316 if (hs->max_version <= TLS1_2_VERSION) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002317 return 1;
2318 }
2319
2320 CBB contents, versions;
2321 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2322 !CBB_add_u16_length_prefixed(out, &contents) ||
2323 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2324 return 0;
2325 }
2326
David Benjamind9791bf2016-09-27 16:39:52 -04002327 /* Add a fake version. See draft-davidben-tls-grease-01. */
2328 if (ssl->ctx->grease_enabled &&
2329 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2330 return 0;
2331 }
2332
Steven Valdez8f36c512017-06-20 10:55:02 -04002333 if (!ssl_add_supported_versions(hs, &versions) ||
2334 !CBB_flush(out)) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002335 return 0;
2336 }
2337
2338 return 1;
2339}
2340
2341
David Benjamin3baa6e12016-10-07 21:10:38 -04002342/* Cookie
2343 *
2344 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2345
David Benjamin8c880a22016-12-03 02:20:34 -05002346static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2347 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002348 return 1;
2349 }
2350
2351 CBB contents, cookie;
2352 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2353 !CBB_add_u16_length_prefixed(out, &contents) ||
2354 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002355 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002356 !CBB_flush(out)) {
2357 return 0;
2358 }
2359
2360 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002361 OPENSSL_free(hs->cookie);
2362 hs->cookie = NULL;
2363 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002364 return 1;
2365}
2366
2367
Steven Valdezce902a92016-05-17 11:47:53 -04002368/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002369 *
Steven Valdezce902a92016-05-17 11:47:53 -04002370 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002371 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002372
David Benjamin8c880a22016-12-03 02:20:34 -05002373static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2374 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002375 CBB contents, groups_bytes;
2376 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002377 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002378 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002379 return 0;
2380 }
2381
David Benjamin65ac9972016-09-02 21:35:25 -04002382 /* Add a fake group. See draft-davidben-tls-grease-01. */
2383 if (ssl->ctx->grease_enabled &&
2384 !CBB_add_u16(&groups_bytes,
2385 ssl_get_grease_value(ssl, ssl_grease_group))) {
2386 return 0;
2387 }
2388
Steven Valdezce902a92016-05-17 11:47:53 -04002389 const uint16_t *groups;
2390 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002391 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002392
David Benjamin54091232016-09-05 12:47:25 -04002393 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002394 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002395 return 0;
2396 }
2397 }
2398
2399 return CBB_flush(out);
2400}
2401
David Benjamin8c880a22016-12-03 02:20:34 -05002402static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2403 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002404 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002405 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2406 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002407 return 1;
2408}
2409
David Benjamin8c880a22016-12-03 02:20:34 -05002410static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2411 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002412 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002413 if (contents == NULL) {
2414 return 1;
2415 }
2416
Steven Valdezce902a92016-05-17 11:47:53 -04002417 CBS supported_group_list;
2418 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2419 CBS_len(&supported_group_list) == 0 ||
2420 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002421 CBS_len(contents) != 0) {
2422 return 0;
2423 }
2424
David Benjamin8c880a22016-12-03 02:20:34 -05002425 hs->peer_supported_group_list =
David Benjamin81678aa2017-07-12 22:43:42 -04002426 (uint16_t *)OPENSSL_malloc(CBS_len(&supported_group_list));
David Benjamin8c880a22016-12-03 02:20:34 -05002427 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002428 *out_alert = SSL_AD_INTERNAL_ERROR;
2429 return 0;
2430 }
2431
Steven Valdezce902a92016-05-17 11:47:53 -04002432 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002433 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002434 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002435 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002436 goto err;
2437 }
2438 }
2439
Steven Valdezce902a92016-05-17 11:47:53 -04002440 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002441 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002442
2443 return 1;
2444
2445err:
David Benjamin8c880a22016-12-03 02:20:34 -05002446 OPENSSL_free(hs->peer_supported_group_list);
2447 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002448 *out_alert = SSL_AD_INTERNAL_ERROR;
2449 return 0;
2450}
2451
David Benjamin8c880a22016-12-03 02:20:34 -05002452static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002453 /* Servers don't echo this extension. */
2454 return 1;
2455}
2456
2457
Adam Langley614c66a2015-06-12 15:26:58 -07002458/* kExtensions contains all the supported extensions. */
2459static const struct tls_extension kExtensions[] = {
2460 {
Adam Langley5021b222015-06-12 18:27:58 -07002461 TLSEXT_TYPE_renegotiate,
2462 NULL,
2463 ext_ri_add_clienthello,
2464 ext_ri_parse_serverhello,
2465 ext_ri_parse_clienthello,
2466 ext_ri_add_serverhello,
2467 },
2468 {
Adam Langley614c66a2015-06-12 15:26:58 -07002469 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002470 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002471 ext_sni_add_clienthello,
2472 ext_sni_parse_serverhello,
2473 ext_sni_parse_clienthello,
2474 ext_sni_add_serverhello,
2475 },
Adam Langley0a056712015-07-01 15:03:33 -07002476 {
2477 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002478 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002479 ext_ems_add_clienthello,
2480 ext_ems_parse_serverhello,
2481 ext_ems_parse_clienthello,
2482 ext_ems_add_serverhello,
2483 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002484 {
2485 TLSEXT_TYPE_session_ticket,
2486 NULL,
2487 ext_ticket_add_clienthello,
2488 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002489 /* Ticket extension client parsing is handled in ssl_session.c */
2490 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002491 ext_ticket_add_serverhello,
2492 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002493 {
2494 TLSEXT_TYPE_signature_algorithms,
2495 NULL,
2496 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002497 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002498 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002499 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002500 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002501 {
2502 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002503 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002504 ext_ocsp_add_clienthello,
2505 ext_ocsp_parse_serverhello,
2506 ext_ocsp_parse_clienthello,
2507 ext_ocsp_add_serverhello,
2508 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002509 {
2510 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002511 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002512 ext_npn_add_clienthello,
2513 ext_npn_parse_serverhello,
2514 ext_npn_parse_clienthello,
2515 ext_npn_add_serverhello,
2516 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002517 {
2518 TLSEXT_TYPE_certificate_timestamp,
2519 NULL,
2520 ext_sct_add_clienthello,
2521 ext_sct_parse_serverhello,
2522 ext_sct_parse_clienthello,
2523 ext_sct_add_serverhello,
2524 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002525 {
2526 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002527 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002528 ext_alpn_add_clienthello,
2529 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002530 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2531 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002532 ext_alpn_add_serverhello,
2533 },
Adam Langley49c7af12015-07-10 14:33:46 -07002534 {
2535 TLSEXT_TYPE_channel_id,
2536 ext_channel_id_init,
2537 ext_channel_id_add_clienthello,
2538 ext_channel_id_parse_serverhello,
2539 ext_channel_id_parse_clienthello,
2540 ext_channel_id_add_serverhello,
2541 },
Adam Langley391250d2015-07-15 19:06:07 -07002542 {
2543 TLSEXT_TYPE_srtp,
2544 ext_srtp_init,
2545 ext_srtp_add_clienthello,
2546 ext_srtp_parse_serverhello,
2547 ext_srtp_parse_clienthello,
2548 ext_srtp_add_serverhello,
2549 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002550 {
2551 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002552 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002553 ext_ec_point_add_clienthello,
2554 ext_ec_point_parse_serverhello,
2555 ext_ec_point_parse_clienthello,
2556 ext_ec_point_add_serverhello,
2557 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002558 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002559 TLSEXT_TYPE_key_share,
2560 NULL,
2561 ext_key_share_add_clienthello,
2562 forbid_parse_serverhello,
2563 ignore_parse_clienthello,
2564 dont_add_serverhello,
2565 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002566 {
Steven Valdeza833c352016-11-01 13:39:36 -04002567 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002568 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002569 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002570 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002571 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002572 dont_add_serverhello,
2573 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002574 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002575 TLSEXT_TYPE_early_data,
2576 NULL,
2577 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002578 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002579 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002580 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002581 },
2582 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002583 TLSEXT_TYPE_supported_versions,
2584 NULL,
2585 ext_supported_versions_add_clienthello,
2586 forbid_parse_serverhello,
2587 ignore_parse_clienthello,
2588 dont_add_serverhello,
2589 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002590 {
2591 TLSEXT_TYPE_cookie,
2592 NULL,
2593 ext_cookie_add_clienthello,
2594 forbid_parse_serverhello,
2595 ignore_parse_clienthello,
2596 dont_add_serverhello,
2597 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002598 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2599 * intolerant to the last extension being zero-length. See
2600 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002601 {
Steven Valdezce902a92016-05-17 11:47:53 -04002602 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002603 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002604 ext_supported_groups_add_clienthello,
2605 ext_supported_groups_parse_serverhello,
2606 ext_supported_groups_parse_clienthello,
2607 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002608 },
Adam Langley614c66a2015-06-12 15:26:58 -07002609};
2610
2611#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2612
David Benjamina3d76d02017-07-14 19:36:07 -04002613static_assert(kNumExtensions <=
2614 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
2615 "too many extensions for sent bitset");
2616static_assert(kNumExtensions <=
2617 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2618 "too many extensions for received bitset");
Adam Langley4cfa96b2015-07-01 11:56:55 -07002619
Adam Langley614c66a2015-06-12 15:26:58 -07002620static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2621 uint16_t value) {
2622 unsigned i;
2623 for (i = 0; i < kNumExtensions; i++) {
2624 if (kExtensions[i].value == value) {
2625 *out_index = i;
2626 return &kExtensions[i];
2627 }
2628 }
2629
2630 return NULL;
2631}
2632
David Benjamin8c880a22016-12-03 02:20:34 -05002633int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2634 SSL *const ssl = hs->ssl;
David Benjaminf04c2e92016-12-06 13:35:25 -05002635 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2636 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002637 !ssl->s3->send_connection_binding) {
2638 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002639 }
Adam Langley95c29f32014-06-20 12:00:00 -07002640
David Benjamine8d53502015-10-10 14:13:23 -04002641 CBB extensions;
2642 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002643 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2644 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002645 }
Adam Langley95c29f32014-06-20 12:00:00 -07002646
David Benjamin8c880a22016-12-03 02:20:34 -05002647 hs->extensions.sent = 0;
2648 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002649
David Benjamin54091232016-09-05 12:47:25 -04002650 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002651 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002652 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002653 }
2654 }
Adam Langley95c29f32014-06-20 12:00:00 -07002655
David Benjamin65ac9972016-09-02 21:35:25 -04002656 uint16_t grease_ext1 = 0;
2657 if (ssl->ctx->grease_enabled) {
2658 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2659 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2660 if (!CBB_add_u16(&extensions, grease_ext1) ||
2661 !CBB_add_u16(&extensions, 0 /* zero length */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002662 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2663 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002664 }
2665 }
2666
David Benjamin54091232016-09-05 12:47:25 -04002667 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002668 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002669 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002670 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002671 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin81678aa2017-07-12 22:43:42 -04002672 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002673 }
Adam Langley95c29f32014-06-20 12:00:00 -07002674
Adam Langley33ad2b52015-07-20 17:43:53 -07002675 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002676 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002677 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002678 }
Adam Langley75712922014-10-10 16:23:43 -07002679
David Benjamin2bd19172016-11-17 16:47:15 +09002680 if (!custom_ext_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002681 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2682 return 0;
Adam Langley09505632015-07-30 18:10:13 -07002683 }
2684
David Benjamin65ac9972016-09-02 21:35:25 -04002685 if (ssl->ctx->grease_enabled) {
2686 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2687 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2688
2689 /* The two fake extensions must not have the same value. GREASE values are
2690 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2691 * one. */
2692 if (grease_ext1 == grease_ext2) {
2693 grease_ext2 ^= 0x1010;
2694 }
2695
2696 if (!CBB_add_u16(&extensions, grease_ext2) ||
2697 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2698 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002699 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2700 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002701 }
2702 }
2703
David Benjamince079fd2016-08-02 16:22:34 -04002704 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002705 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002706 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002707 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002708 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002709 *
2710 * NB: because this code works out the length of all existing extensions
2711 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002712 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002713 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002714 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002715 * Server 7.0 is intolerant to the last extension being zero-length. See
2716 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002717 if (padding_len >= 4 + 1) {
2718 padding_len -= 4;
2719 } else {
2720 padding_len = 1;
2721 }
Adam Langley95c29f32014-06-20 12:00:00 -07002722
Adam Langley33ad2b52015-07-20 17:43:53 -07002723 uint8_t *padding_bytes;
2724 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2725 !CBB_add_u16(&extensions, padding_len) ||
2726 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002727 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2728 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002729 }
Adam Langley75712922014-10-10 16:23:43 -07002730
David Benjamin17cf2cb2016-12-13 01:07:13 -05002731 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002732 }
2733 }
Adam Langley75712922014-10-10 16:23:43 -07002734
Steven Valdeza833c352016-11-01 13:39:36 -04002735 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002736 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002737 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2738 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -04002739 }
2740
David Benjamina01deee2015-12-08 18:56:31 -05002741 /* Discard empty extensions blocks. */
2742 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002743 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002744 }
2745
David Benjamine8d53502015-10-10 14:13:23 -04002746 return CBB_flush(out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002747}
Adam Langley95c29f32014-06-20 12:00:00 -07002748
David Benjamin8c880a22016-12-03 02:20:34 -05002749int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2750 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002751 CBB extensions;
2752 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002753 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002754 }
2755
David Benjamin8c880a22016-12-03 02:20:34 -05002756 for (unsigned i = 0; i < kNumExtensions; i++) {
2757 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002758 /* Don't send extensions that were not received. */
2759 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002760 }
Adam Langley95c29f32014-06-20 12:00:00 -07002761
David Benjamin8c880a22016-12-03 02:20:34 -05002762 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002763 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002764 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002765 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002766 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002767 }
Adam Langley95c29f32014-06-20 12:00:00 -07002768
David Benjamin2bd19172016-11-17 16:47:15 +09002769 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002770 goto err;
2771 }
2772
Steven Valdez143e8b32016-07-11 13:19:03 -04002773 /* Discard empty extensions blocks before TLS 1.3. */
2774 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2775 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002776 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002777 }
2778
David Benjamin56380462015-10-10 14:59:09 -04002779 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002780
2781err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002782 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002783 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002784}
Adam Langley95c29f32014-06-20 12:00:00 -07002785
David Benjamin731058e2016-12-03 23:15:13 -05002786static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2787 const SSL_CLIENT_HELLO *client_hello,
2788 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002789 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002790 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002791 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002792 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002793 }
2794 }
2795
David Benjamin8c880a22016-12-03 02:20:34 -05002796 hs->extensions.received = 0;
2797 hs->custom_extensions.received = 0;
David Benjamine14ff062016-08-09 16:21:24 -04002798 CBS extensions;
2799 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2800 while (CBS_len(&extensions) != 0) {
2801 uint16_t type;
2802 CBS extension;
2803
2804 /* Decode the next extension. */
2805 if (!CBS_get_u16(&extensions, &type) ||
2806 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002807 *out_alert = SSL_AD_DECODE_ERROR;
2808 return 0;
2809 }
Adam Langley95c29f32014-06-20 12:00:00 -07002810
David Benjamine14ff062016-08-09 16:21:24 -04002811 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2812 * ambiguous. Ignore all but the renegotiation_info extension. */
2813 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2814 continue;
2815 }
Adam Langley95c29f32014-06-20 12:00:00 -07002816
David Benjamine14ff062016-08-09 16:21:24 -04002817 unsigned ext_index;
2818 const struct tls_extension *const ext =
2819 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002820
David Benjamine14ff062016-08-09 16:21:24 -04002821 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002822 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002823 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002824 return 0;
2825 }
David Benjamine14ff062016-08-09 16:21:24 -04002826 continue;
2827 }
2828
David Benjamin8c880a22016-12-03 02:20:34 -05002829 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002830 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002831 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002832 *out_alert = alert;
2833 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002834 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002835 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002836 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002837 }
Adam Langley75712922014-10-10 16:23:43 -07002838
David Benjamin1deb41b2016-08-09 19:36:38 -04002839 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002840 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002841 continue;
2842 }
2843
2844 CBS *contents = NULL, fake_contents;
2845 static const uint8_t kFakeRenegotiateExtension[] = {0};
2846 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2847 ssl_client_cipher_list_contains_cipher(client_hello,
2848 SSL3_CK_SCSV & 0xffff)) {
2849 /* The renegotiation SCSV was received so pretend that we received a
2850 * renegotiation extension. */
2851 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2852 sizeof(kFakeRenegotiateExtension));
2853 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002854 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002855 }
2856
2857 /* Extension wasn't observed so call the callback with a NULL
2858 * parameter. */
2859 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002860 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002861 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002862 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002863 *out_alert = alert;
2864 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002865 }
2866 }
2867
Adam Langleyfcf25832014-12-18 17:42:32 -08002868 return 1;
2869}
Adam Langley95c29f32014-06-20 12:00:00 -07002870
David Benjamin731058e2016-12-03 23:15:13 -05002871int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2872 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002873 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002874 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002875 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002876 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002877 return 0;
2878 }
Adam Langley95c29f32014-06-20 12:00:00 -07002879
David Benjamin8c880a22016-12-03 02:20:34 -05002880 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002881 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002882 return 0;
2883 }
Adam Langley95c29f32014-06-20 12:00:00 -07002884
Adam Langleyfcf25832014-12-18 17:42:32 -08002885 return 1;
2886}
Adam Langley95c29f32014-06-20 12:00:00 -07002887
David Benjamin8c880a22016-12-03 02:20:34 -05002888static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2889 int *out_alert) {
2890 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002891 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2892 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2893 return 1;
2894 }
Adam Langley614c66a2015-06-12 15:26:58 -07002895
Steven Valdez143e8b32016-07-11 13:19:03 -04002896 /* Decode the extensions block and check it is valid. */
2897 CBS extensions;
2898 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2899 !tls1_check_duplicate_extensions(&extensions)) {
2900 *out_alert = SSL_AD_DECODE_ERROR;
2901 return 0;
2902 }
2903
2904 uint32_t received = 0;
2905 while (CBS_len(&extensions) != 0) {
2906 uint16_t type;
2907 CBS extension;
2908
2909 /* Decode the next extension. */
2910 if (!CBS_get_u16(&extensions, &type) ||
2911 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002912 *out_alert = SSL_AD_DECODE_ERROR;
2913 return 0;
2914 }
Adam Langley95c29f32014-06-20 12:00:00 -07002915
Steven Valdez143e8b32016-07-11 13:19:03 -04002916 unsigned ext_index;
2917 const struct tls_extension *const ext =
2918 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002919
Steven Valdez143e8b32016-07-11 13:19:03 -04002920 if (ext == NULL) {
Steven Valdezf4ecc842017-08-10 14:02:56 -04002921 hs->received_custom_extension = 1;
David Benjamin2bd19172016-11-17 16:47:15 +09002922 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002923 return 0;
2924 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002925 continue;
2926 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002927
David Benjamina3d76d02017-07-14 19:36:07 -04002928 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
2929 "too many bits");
David Benjamin5db7c9b2017-01-24 16:17:03 -05002930
David Benjamin8c880a22016-12-03 02:20:34 -05002931 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002932 type != TLSEXT_TYPE_renegotiate) {
2933 /* If the extension was never sent then it is illegal, except for the
2934 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002935 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2936 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002937 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002938 return 0;
2939 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002940
Steven Valdez143e8b32016-07-11 13:19:03 -04002941 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002942
Steven Valdez143e8b32016-07-11 13:19:03 -04002943 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002944 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002945 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002946 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04002947 *out_alert = alert;
2948 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002949 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002950 }
Adam Langley95c29f32014-06-20 12:00:00 -07002951
David Benjamin54091232016-09-05 12:47:25 -04002952 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002953 if (!(received & (1u << i))) {
2954 /* Extension wasn't observed so call the callback with a NULL
2955 * parameter. */
2956 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002957 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002958 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002959 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002960 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002961 return 0;
2962 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002963 }
2964 }
Adam Langley95c29f32014-06-20 12:00:00 -07002965
Adam Langleyfcf25832014-12-18 17:42:32 -08002966 return 1;
2967}
Adam Langley95c29f32014-06-20 12:00:00 -07002968
David Benjamin8c880a22016-12-03 02:20:34 -05002969static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
2970 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002971 int ret = SSL_TLSEXT_ERR_NOACK;
2972 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002973
David Benjamin78f8aab2016-03-10 16:33:58 -05002974 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002975 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002976 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05002977 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
2978 ret = ssl->session_ctx->tlsext_servername_callback(
2979 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002980 }
Adam Langley95c29f32014-06-20 12:00:00 -07002981
Adam Langleyfcf25832014-12-18 17:42:32 -08002982 switch (ret) {
2983 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002984 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002985 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002986
Adam Langleyfcf25832014-12-18 17:42:32 -08002987 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05002988 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002989 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002990
Adam Langleyfcf25832014-12-18 17:42:32 -08002991 default:
2992 return 1;
2993 }
2994}
Adam Langleyed8270a2014-09-02 13:52:56 -07002995
David Benjamin8c880a22016-12-03 02:20:34 -05002996int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
2997 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002998 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002999 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003000 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003001 return 0;
3002 }
3003
Adam Langleyfcf25832014-12-18 17:42:32 -08003004 return 1;
3005}
Adam Langley95c29f32014-06-20 12:00:00 -07003006
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003007static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
3008 uint8_t **out, size_t *out_len, EVP_CIPHER_CTX *cipher_ctx,
3009 HMAC_CTX *hmac_ctx, const uint8_t *ticket, size_t ticket_len) {
3010 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003011
David Benjamine3aa1d92015-06-16 15:34:50 -04003012 /* Check the MAC at the end of the ticket. */
3013 uint8_t mac[EVP_MAX_MD_SIZE];
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003014 size_t mac_len = HMAC_size(hmac_ctx);
David Benjamine3aa1d92015-06-16 15:34:50 -04003015 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003016 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamin81678aa2017-07-12 22:43:42 -04003017 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003018 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003019 HMAC_Update(hmac_ctx, ticket, ticket_len - mac_len);
3020 HMAC_Final(hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003021 int mac_ok =
3022 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3023#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3024 mac_ok = 1;
3025#endif
3026 if (!mac_ok) {
David Benjamin81678aa2017-07-12 22:43:42 -04003027 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003028 }
3029
David Benjamine3aa1d92015-06-16 15:34:50 -04003030 /* Decrypt the session data. */
3031 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3032 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3033 mac_len;
David Benjamin86e95b82017-07-18 16:34:25 -04003034 UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len));
David Benjamin81678aa2017-07-12 22:43:42 -04003035 if (!plaintext) {
3036 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08003037 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003038 size_t plaintext_len;
3039#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin81678aa2017-07-12 22:43:42 -04003040 OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003041 plaintext_len = ciphertext_len;
3042#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003043 if (ciphertext_len >= INT_MAX) {
David Benjamin81678aa2017-07-12 22:43:42 -04003044 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003045 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003046 int len1, len2;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003047 if (!EVP_DecryptUpdate(cipher_ctx, plaintext.get(), &len1, ciphertext,
David Benjamine3aa1d92015-06-16 15:34:50 -04003048 (int)ciphertext_len) ||
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003049 !EVP_DecryptFinal_ex(cipher_ctx, plaintext.get() + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003050 ERR_clear_error();
David Benjamin81678aa2017-07-12 22:43:42 -04003051 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003052 }
Adam Langley4c341d02017-03-08 19:33:21 -08003053 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04003054#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003055
David Benjamin81678aa2017-07-12 22:43:42 -04003056 *out = plaintext.release();
Adam Langley4c341d02017-03-08 19:33:21 -08003057 *out_len = plaintext_len;
David Benjamin81678aa2017-07-12 22:43:42 -04003058 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08003059}
3060
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003061static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
3062 SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
3063 const uint8_t *ticket, size_t ticket_len) {
3064 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3065 ScopedEVP_CIPHER_CTX cipher_ctx;
3066 ScopedHMAC_CTX hmac_ctx;
3067 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3068 int cb_ret = ssl->session_ctx->tlsext_ticket_key_cb(
3069 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(),
3070 hmac_ctx.get(), 0 /* decrypt */);
3071 if (cb_ret < 0) {
3072 return ssl_ticket_aead_error;
3073 } else if (cb_ret == 0) {
3074 return ssl_ticket_aead_ignore_ticket;
3075 } else if (cb_ret == 2) {
3076 *out_renew_ticket = 1;
3077 } else {
3078 assert(cb_ret == 1);
3079 }
3080 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3081 hmac_ctx.get(), ticket, ticket_len);
3082}
3083
3084static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
3085 SSL *ssl, uint8_t **out, size_t *out_len, const uint8_t *ticket,
3086 size_t ticket_len) {
3087 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3088 SSL_CTX *ctx = ssl->session_ctx;
3089
3090 /* Rotate the ticket key if necessary. */
3091 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
3092 return ssl_ticket_aead_error;
3093 }
3094
3095 /* Pick the matching ticket key and decrypt. */
3096 ScopedEVP_CIPHER_CTX cipher_ctx;
3097 ScopedHMAC_CTX hmac_ctx;
3098 {
3099 MutexReadLock lock(&ctx->lock);
3100 const tlsext_ticket_key *key;
3101 if (ctx->tlsext_ticket_key_current &&
3102 !OPENSSL_memcmp(ctx->tlsext_ticket_key_current->name, ticket,
3103 SSL_TICKET_KEY_NAME_LEN)) {
3104 key = ctx->tlsext_ticket_key_current;
3105 } else if (ctx->tlsext_ticket_key_prev &&
3106 !OPENSSL_memcmp(ctx->tlsext_ticket_key_prev->name, ticket,
3107 SSL_TICKET_KEY_NAME_LEN)) {
3108 key = ctx->tlsext_ticket_key_prev;
3109 } else {
3110 return ssl_ticket_aead_ignore_ticket;
3111 }
3112 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3113 if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
3114 tlsext_tick_md(), NULL) ||
3115 !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL,
3116 key->aes_key, iv)) {
3117 return ssl_ticket_aead_error;
3118 }
3119 }
3120 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3121 hmac_ctx.get(), ticket, ticket_len);
3122}
3123
Adam Langley4c341d02017-03-08 19:33:21 -08003124static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
3125 SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
3126 const uint8_t *ticket, size_t ticket_len) {
David Benjamin81678aa2017-07-12 22:43:42 -04003127 uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len);
Adam Langley4c341d02017-03-08 19:33:21 -08003128 if (plaintext == NULL) {
3129 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3130 return ssl_ticket_aead_error;
3131 }
3132
3133 size_t plaintext_len;
3134 const enum ssl_ticket_aead_result_t result =
3135 ssl->session_ctx->ticket_aead_method->open(
3136 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3137
3138 if (result == ssl_ticket_aead_success) {
3139 *out = plaintext;
3140 plaintext = NULL;
3141 *out_len = plaintext_len;
3142 }
3143
3144 OPENSSL_free(plaintext);
3145 return result;
3146}
3147
3148enum ssl_ticket_aead_result_t ssl_process_ticket(
David Benjamin37af90f2017-07-29 01:42:16 -04003149 SSL *ssl, UniquePtr<SSL_SESSION> *out_session, int *out_renew_ticket,
Adam Langley4c341d02017-03-08 19:33:21 -08003150 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3151 size_t session_id_len) {
3152 *out_renew_ticket = 0;
David Benjamin37af90f2017-07-29 01:42:16 -04003153 out_session->reset();
Adam Langley4c341d02017-03-08 19:33:21 -08003154
3155 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3156 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3157 return ssl_ticket_aead_ignore_ticket;
3158 }
3159
3160 uint8_t *plaintext = NULL;
3161 size_t plaintext_len;
3162 enum ssl_ticket_aead_result_t result;
3163 if (ssl->session_ctx->ticket_aead_method != NULL) {
3164 result = ssl_decrypt_ticket_with_method(
3165 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3166 } else {
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003167 /* Ensure there is room for the key name and the largest IV
3168 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower,
3169 * but the maximum IV length should be well under the minimum size for the
3170 * session material and HMAC. */
3171 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3172 return ssl_ticket_aead_ignore_ticket;
3173 }
3174 if (ssl->session_ctx->tlsext_ticket_key_cb != NULL) {
3175 result = ssl_decrypt_ticket_with_cb(ssl, &plaintext, &plaintext_len,
3176 out_renew_ticket, ticket, ticket_len);
3177 } else {
3178 result = ssl_decrypt_ticket_with_ticket_keys(
3179 ssl, &plaintext, &plaintext_len, ticket, ticket_len);
3180 }
Adam Langley4c341d02017-03-08 19:33:21 -08003181 }
3182
3183 if (result != ssl_ticket_aead_success) {
3184 return result;
3185 }
3186
David Benjamine3aa1d92015-06-16 15:34:50 -04003187 /* Decode the session. */
David Benjamin37af90f2017-07-29 01:42:16 -04003188 UniquePtr<SSL_SESSION> session(
3189 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx));
Adam Langley4c341d02017-03-08 19:33:21 -08003190 OPENSSL_free(plaintext);
3191
David Benjamin37af90f2017-07-29 01:42:16 -04003192 if (!session) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003193 ERR_clear_error(); /* Don't leave an error on the queue. */
Adam Langley4c341d02017-03-08 19:33:21 -08003194 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003195 }
3196
3197 /* Copy the client's session ID into the new session, to denote the ticket has
3198 * been accepted. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003199 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003200 session->session_id_length = session_id_len;
3201
David Benjamin37af90f2017-07-29 01:42:16 -04003202 *out_session = std::move(session);
Adam Langley4c341d02017-03-08 19:33:21 -08003203 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003204}
Adam Langley95c29f32014-06-20 12:00:00 -07003205
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003206int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003207 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003208 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003209 return 1;
3210 }
David Benjamincd996942014-07-20 16:23:51 -04003211
David Benjamin0fc37ef2016-08-17 15:29:46 -04003212 OPENSSL_free(hs->peer_sigalgs);
3213 hs->peer_sigalgs = NULL;
3214 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003215
3216 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003217 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003218 return 0;
3219 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003220 num_sigalgs /= 2;
3221
3222 /* supported_signature_algorithms in the certificate request is
3223 * allowed to be empty. */
3224 if (num_sigalgs == 0) {
3225 return 1;
3226 }
3227
Steven Valdez02563852016-06-23 13:33:05 -04003228 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3229 * and we just divided |num_sigalgs| by two. */
David Benjamin81678aa2017-07-12 22:43:42 -04003230 hs->peer_sigalgs = (uint16_t *)OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003231 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003232 return 0;
3233 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003234 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003235
3236 CBS sigalgs;
3237 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003238 for (size_t i = 0; i < num_sigalgs; i++) {
3239 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003240 return 0;
3241 }
3242 }
Adam Langley95c29f32014-06-20 12:00:00 -07003243
Adam Langleyfcf25832014-12-18 17:42:32 -08003244 return 1;
3245}
David Benjaminec2f27d2014-11-13 19:17:25 -05003246
David Benjamina3651382017-04-20 17:49:36 -04003247int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
3248 switch (EVP_PKEY_id(pkey)) {
3249 case EVP_PKEY_RSA:
3250 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3251 return 1;
3252 case EVP_PKEY_EC:
3253 *out = SSL_SIGN_ECDSA_SHA1;
3254 return 1;
3255 default:
3256 return 0;
3257 }
3258}
3259
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003260int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3261 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003262 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003263
Steven Valdezf0451ca2016-06-29 13:16:27 -04003264 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
David Benjamina3651382017-04-20 17:49:36 -04003265 * handshake. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04003266 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin31b0c9b2017-07-20 14:49:15 -04003267 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
David Benjamina3651382017-04-20 17:49:36 -04003268 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3269 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003270 }
David Benjamina3651382017-04-20 17:49:36 -04003271 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003272 }
3273
David Benjamin3ef76972016-10-17 17:59:54 -04003274 const uint16_t *sigalgs = cert->sigalgs;
3275 size_t num_sigalgs = cert->num_sigalgs;
3276 if (sigalgs == NULL) {
3277 sigalgs = kSignSignatureAlgorithms;
3278 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003279 }
3280
David Benjamin0fc37ef2016-08-17 15:29:46 -04003281 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3282 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3283 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003284 /* If the client didn't specify any signature_algorithms extension then
3285 * we can assume that it supports SHA1. See
3286 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3287 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3288 SSL_SIGN_ECDSA_SHA1};
3289 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003290 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003291 }
3292
David Benjamin0fc37ef2016-08-17 15:29:46 -04003293 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003294 uint16_t sigalg = sigalgs[i];
3295 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3296 * negotiated. */
3297 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjamina232a712017-03-30 15:51:53 -05003298 !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003299 continue;
3300 }
3301
David Benjamin0fc37ef2016-08-17 15:29:46 -04003302 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003303 if (sigalg == peer_sigalgs[j]) {
3304 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003305 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003306 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003307 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003308 }
Adam Langley95c29f32014-06-20 12:00:00 -07003309
David Benjaminea9a0d52016-07-08 15:52:59 -07003310 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3311 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003312}
Adam Langley95c29f32014-06-20 12:00:00 -07003313
David Benjamin7934f082017-08-01 16:32:25 -04003314int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
Steven Valdez908ac192017-01-12 13:17:07 -05003315 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003316 /* A Channel ID handshake message is structured to contain multiple
3317 * extensions, but the only one that can be present is Channel ID. */
David Benjamin7934f082017-08-01 16:32:25 -04003318 uint16_t extension_type;
3319 CBS channel_id = msg.body, extension;
Nick Harper60a85cb2016-09-23 16:25:11 -07003320 if (!CBS_get_u16(&channel_id, &extension_type) ||
3321 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3322 CBS_len(&channel_id) != 0 ||
3323 extension_type != TLSEXT_TYPE_channel_id ||
3324 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3325 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3326 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3327 return 0;
3328 }
3329
David Benjamin86e95b82017-07-18 16:34:25 -04003330 UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Nick Harper60a85cb2016-09-23 16:25:11 -07003331 if (!p256) {
3332 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3333 return 0;
3334 }
3335
David Benjamin86e95b82017-07-18 16:34:25 -04003336 UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3337 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
David Benjamin81678aa2017-07-12 22:43:42 -04003338 if (!sig || !x || !y) {
3339 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003340 }
3341
3342 const uint8_t *p = CBS_data(&extension);
David Benjamin81678aa2017-07-12 22:43:42 -04003343 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3344 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3345 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3346 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
3347 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003348 }
3349
David Benjamin86e95b82017-07-18 16:34:25 -04003350 UniquePtr<EC_KEY> key(EC_KEY_new());
3351 UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
David Benjamin81678aa2017-07-12 22:43:42 -04003352 if (!key || !point ||
3353 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3354 y.get(), nullptr) ||
3355 !EC_KEY_set_group(key.get(), p256.get()) ||
3356 !EC_KEY_set_public_key(key.get(), point.get())) {
3357 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003358 }
3359
3360 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)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003363 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003364 }
3365
David Benjamin81678aa2017-07-12 22:43:42 -04003366 int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
Nick Harper60a85cb2016-09-23 16:25:11 -07003367#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3368 sig_ok = 1;
3369#endif
3370 if (!sig_ok) {
3371 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3372 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3373 ssl->s3->tlsext_channel_id_valid = 0;
David Benjamin81678aa2017-07-12 22:43:42 -04003374 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003375 }
3376
David Benjamin17cf2cb2016-12-13 01:07:13 -05003377 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
David Benjamin81678aa2017-07-12 22:43:42 -04003378 return 1;
Nick Harper60a85cb2016-09-23 16:25:11 -07003379}
3380
Steven Valdez908ac192017-01-12 13:17:07 -05003381int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3382 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003383 uint8_t digest[EVP_MAX_MD_SIZE];
3384 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003385 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003386 return 0;
3387 }
3388
3389 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3390 if (ec_key == NULL) {
3391 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3392 return 0;
3393 }
3394
3395 int ret = 0;
3396 BIGNUM *x = BN_new();
3397 BIGNUM *y = BN_new();
3398 ECDSA_SIG *sig = NULL;
3399 if (x == NULL || y == NULL ||
3400 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3401 EC_KEY_get0_public_key(ec_key),
3402 x, y, NULL)) {
3403 goto err;
3404 }
3405
3406 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3407 if (sig == NULL) {
3408 goto err;
3409 }
3410
3411 CBB child;
3412 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3413 !CBB_add_u16_length_prefixed(cbb, &child) ||
3414 !BN_bn2cbb_padded(&child, 32, x) ||
3415 !BN_bn2cbb_padded(&child, 32, y) ||
3416 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3417 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3418 !CBB_flush(cbb)) {
3419 goto err;
3420 }
3421
3422 ret = 1;
3423
3424err:
3425 BN_free(x);
3426 BN_free(y);
3427 ECDSA_SIG_free(sig);
3428 return ret;
3429}
3430
Steven Valdez908ac192017-01-12 13:17:07 -05003431int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3432 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003433 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3434 uint8_t *msg;
3435 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003436 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003437 ssl_cert_verify_channel_id)) {
3438 return 0;
3439 }
3440 SHA256(msg, msg_len, out);
3441 *out_len = SHA256_DIGEST_LENGTH;
3442 OPENSSL_free(msg);
3443 return 1;
3444 }
3445
Nick Harper95594012016-10-20 14:07:13 -07003446 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003447
Nick Harper95594012016-10-20 14:07:13 -07003448 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003449 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003450 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003451
Steven Valdez87eab492016-06-27 16:34:59 -04003452 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003453 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003454 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003455 if (ssl->session->original_handshake_hash_len == 0) {
3456 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003457 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003458 }
Nick Harper95594012016-10-20 14:07:13 -07003459 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3460 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003461 }
3462
Steven Valdez908ac192017-01-12 13:17:07 -05003463 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3464 size_t hs_hash_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003465 if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003466 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003467 }
Steven Valdez908ac192017-01-12 13:17:07 -05003468 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003469 SHA256_Final(out, &ctx);
3470 *out_len = SHA256_DIGEST_LENGTH;
3471 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003472}
Adam Langley1258b6a2014-06-20 12:00:00 -07003473
3474/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin45738dd2017-02-09 20:01:26 -05003475 * hashes in |hs->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003476 * data. */
Steven Valdez908ac192017-01-12 13:17:07 -05003477int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3478 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003479 /* This function should never be called for a resumed session because the
3480 * handshake hashes that we wish to record are for the original, full
3481 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003482 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003483 return -1;
3484 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003485
David Benjamina3d76d02017-07-14 19:36:07 -04003486 static_assert(
David Benjamin45738dd2017-02-09 20:01:26 -05003487 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
David Benjamina3d76d02017-07-14 19:36:07 -04003488 "original_handshake_hash is too small");
Steven Valdez908ac192017-01-12 13:17:07 -05003489
3490 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003491 if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
3492 &digest_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003493 return -1;
3494 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003495
David Benjamina3d76d02017-07-14 19:36:07 -04003496 static_assert(EVP_MAX_MD_SIZE <= 0xff,
3497 "EVP_MAX_MD_SIZE does not fit in uint8_t");
David Benjamin45738dd2017-02-09 20:01:26 -05003498 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003499
Adam Langleyfcf25832014-12-18 17:42:32 -08003500 return 1;
3501}
Nick Harper60a85cb2016-09-23 16:25:11 -07003502
3503int ssl_do_channel_id_callback(SSL *ssl) {
3504 if (ssl->tlsext_channel_id_private != NULL ||
3505 ssl->ctx->channel_id_cb == NULL) {
3506 return 1;
3507 }
3508
3509 EVP_PKEY *key = NULL;
3510 ssl->ctx->channel_id_cb(ssl, &key);
3511 if (key == NULL) {
3512 /* The caller should try again later. */
3513 return 1;
3514 }
3515
3516 int ret = SSL_set1_tls_channel_id(ssl, key);
3517 EVP_PKEY_free(key);
3518 return ret;
3519}
Adam Langleycfa08c32016-11-17 13:21:27 -08003520
3521int ssl_is_sct_list_valid(const CBS *contents) {
3522 /* Shallow parse the SCT list for sanity. By the RFC
3523 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3524 * of the SCTs may be empty. */
3525 CBS copy = *contents;
3526 CBS sct_list;
3527 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3528 CBS_len(&copy) != 0 ||
3529 CBS_len(&sct_list) == 0) {
3530 return 0;
3531 }
3532
3533 while (CBS_len(&sct_list) > 0) {
3534 CBS sct;
3535 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3536 CBS_len(&sct) == 0) {
3537 return 0;
3538 }
3539 }
3540
3541 return 1;
3542}
David Benjamin86e95b82017-07-18 16:34:25 -04003543
3544} // namespace bssl
3545
3546using namespace bssl;
3547
3548int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
3549 uint16_t extension_type,
3550 const uint8_t **out_data,
3551 size_t *out_len) {
3552 CBS cbs;
3553 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
3554 return 0;
3555 }
3556
3557 *out_data = CBS_data(&cbs);
3558 *out_len = CBS_len(&cbs);
3559 return 1;
3560}
3561
3562void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
3563 ctx->ed25519_enabled = !!enabled;
3564}
3565
3566int SSL_extension_supported(unsigned extension_value) {
3567 uint32_t index;
3568 return extension_value == TLSEXT_TYPE_padding ||
3569 tls_extension_find(&index, extension_value) != NULL;
3570}