blob: 76469ebb739361a2675ee376d0261d29793c9000 [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 Benjamin8c880a22016-12-03 02:20:34 -0500129static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
Adam Langley95c29f32014-06-20 12:00:00 -0700130
Adam Langleyfcf25832014-12-18 17:42:32 -0800131static int compare_uint16_t(const void *p1, const void *p2) {
132 uint16_t u1 = *((const uint16_t *)p1);
133 uint16_t u2 = *((const uint16_t *)p2);
134 if (u1 < u2) {
135 return -1;
136 } else if (u1 > u2) {
137 return 1;
138 } else {
139 return 0;
140 }
141}
David Benjamin35a7a442014-07-05 00:23:20 -0400142
Adam Langleyfcf25832014-12-18 17:42:32 -0800143/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
144 * more than one extension of the same type in a ClientHello or ServerHello.
145 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400146 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800147static int tls1_check_duplicate_extensions(const CBS *cbs) {
148 CBS extensions = *cbs;
149 size_t num_extensions = 0, i = 0;
150 uint16_t *extension_types = NULL;
151 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400152
Adam Langleyfcf25832014-12-18 17:42:32 -0800153 /* First pass: count the extensions. */
154 while (CBS_len(&extensions) > 0) {
155 uint16_t type;
156 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400157
Adam Langleyfcf25832014-12-18 17:42:32 -0800158 if (!CBS_get_u16(&extensions, &type) ||
159 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
160 goto done;
161 }
David Benjamin35a7a442014-07-05 00:23:20 -0400162
Adam Langleyfcf25832014-12-18 17:42:32 -0800163 num_extensions++;
164 }
David Benjamin35a7a442014-07-05 00:23:20 -0400165
Adam Langleyfcf25832014-12-18 17:42:32 -0800166 if (num_extensions == 0) {
167 return 1;
168 }
David Benjamin9a373592014-07-25 04:27:53 -0400169
David Benjamin81678aa2017-07-12 22:43:42 -0400170 extension_types =
171 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800172 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400173 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800174 goto done;
175 }
David Benjamin35a7a442014-07-05 00:23:20 -0400176
Adam Langleyfcf25832014-12-18 17:42:32 -0800177 /* Second pass: gather the extension types. */
178 extensions = *cbs;
179 for (i = 0; i < num_extensions; i++) {
180 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400181
Adam Langleyfcf25832014-12-18 17:42:32 -0800182 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
183 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
184 /* This should not happen. */
185 goto done;
186 }
187 }
188 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400189
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 /* Sort the extensions and make sure there are no duplicates. */
191 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
192 for (i = 1; i < num_extensions; i++) {
193 if (extension_types[i - 1] == extension_types[i]) {
194 goto done;
195 }
196 }
David Benjamin35a7a442014-07-05 00:23:20 -0400197
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 ret = 1;
199
David Benjamin35a7a442014-07-05 00:23:20 -0400200done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400201 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 return ret;
203}
David Benjamin35a7a442014-07-05 00:23:20 -0400204
David Benjamin731058e2016-12-03 23:15:13 -0500205int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out, const uint8_t *in,
206 size_t in_len) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500207 OPENSSL_memset(out, 0, sizeof(*out));
David Benjamin731058e2016-12-03 23:15:13 -0500208 out->ssl = ssl;
209 out->client_hello = in;
210 out->client_hello_len = in_len;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400211
David Benjamine14ff062016-08-09 16:21:24 -0400212 CBS client_hello, random, session_id;
David Benjamin731058e2016-12-03 23:15:13 -0500213 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
214 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjamine14ff062016-08-09 16:21:24 -0400215 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
216 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
217 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 return 0;
219 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700220
David Benjamin731058e2016-12-03 23:15:13 -0500221 out->random = CBS_data(&random);
222 out->random_len = CBS_len(&random);
223 out->session_id = CBS_data(&session_id);
224 out->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 /* Skip past DTLS cookie */
David Benjamin731058e2016-12-03 23:15:13 -0500227 if (SSL_is_dtls(out->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800228 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400229 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
230 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 return 0;
232 }
233 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700234
David Benjamine14ff062016-08-09 16:21:24 -0400235 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800236 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400237 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
238 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 CBS_len(&compression_methods) < 1) {
240 return 0;
241 }
David Benjamine14ff062016-08-09 16:21:24 -0400242
David Benjamin731058e2016-12-03 23:15:13 -0500243 out->cipher_suites = CBS_data(&cipher_suites);
244 out->cipher_suites_len = CBS_len(&cipher_suites);
245 out->compression_methods = CBS_data(&compression_methods);
246 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 /* If the ClientHello ends here then it's valid, but doesn't have any
249 * extensions. (E.g. SSLv3.) */
250 if (CBS_len(&client_hello) == 0) {
David Benjamin731058e2016-12-03 23:15:13 -0500251 out->extensions = NULL;
252 out->extensions_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800253 return 1;
254 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700255
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 /* Extract extensions and check it is valid. */
David Benjamine14ff062016-08-09 16:21:24 -0400257 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
259 !tls1_check_duplicate_extensions(&extensions) ||
260 CBS_len(&client_hello) != 0) {
261 return 0;
262 }
David Benjamine14ff062016-08-09 16:21:24 -0400263
David Benjamin731058e2016-12-03 23:15:13 -0500264 out->extensions = CBS_data(&extensions);
265 out->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700266
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 return 1;
268}
Adam Langleydc9b1412014-06-20 12:00:00 -0700269
David Benjamin731058e2016-12-03 23:15:13 -0500270int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
271 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 CBS extensions;
David Benjamin731058e2016-12-03 23:15:13 -0500273 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 while (CBS_len(&extensions) != 0) {
David Benjamincec73442016-08-02 17:41:33 -0400275 /* Decode the next extension. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 uint16_t type;
277 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 if (!CBS_get_u16(&extensions, &type) ||
279 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
280 return 0;
281 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700282
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400284 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 return 1;
286 }
287 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700288
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 return 0;
290}
Adam Langley95c29f32014-06-20 12:00:00 -0700291
David Benjamin731058e2016-12-03 23:15:13 -0500292int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
293 uint16_t extension_type,
294 const uint8_t **out_data,
295 size_t *out_len) {
David Benjamincec73442016-08-02 17:41:33 -0400296 CBS cbs;
David Benjamin731058e2016-12-03 23:15:13 -0500297 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
David Benjamincec73442016-08-02 17:41:33 -0400298 return 0;
299 }
300
301 *out_data = CBS_data(&cbs);
302 *out_len = CBS_len(&cbs);
303 return 1;
304}
305
Steven Valdezce902a92016-05-17 11:47:53 -0400306static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400307 SSL_CURVE_X25519,
308 SSL_CURVE_SECP256R1,
309 SSL_CURVE_SECP384R1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800310};
Adam Langley95c29f32014-06-20 12:00:00 -0700311
David Benjaminf04976b2016-10-07 00:37:55 -0400312void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
Steven Valdez5440fe02016-07-18 12:40:30 -0400313 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400314 *out_group_ids = ssl->supported_group_list;
315 *out_group_ids_len = ssl->supported_group_list_len;
316 if (!*out_group_ids) {
317 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400318 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 }
320}
David Benjamined439582014-07-14 19:13:02 -0400321
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900322int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
323 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400324 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400325
David Benjaminf04976b2016-10-07 00:37:55 -0400326 const uint16_t *groups, *pref, *supp;
327 size_t groups_len, pref_len, supp_len;
328 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800329
David Benjaminf04976b2016-10-07 00:37:55 -0400330 /* Clients are not required to send a supported_groups extension. In this
331 * case, the server is free to pick any group it likes. See RFC 4492,
332 * section 4, paragraph 3.
333 *
334 * However, in the interests of compatibility, we will skip ECDH if the
335 * client didn't send an extension because we can't be sure that they'll
336 * support our favoured group. Thus we do not special-case an emtpy
337 * |peer_supported_group_list|. */
David Benjamin55a43642015-04-20 14:45:55 -0400338
David Benjamin4298d772015-12-19 00:18:25 -0500339 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400340 pref = groups;
341 pref_len = groups_len;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900342 supp = hs->peer_supported_group_list;
343 supp_len = hs->peer_supported_group_list_len;
David Benjamin55a43642015-04-20 14:45:55 -0400344 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900345 pref = hs->peer_supported_group_list;
346 pref_len = hs->peer_supported_group_list_len;
Steven Valdezce902a92016-05-17 11:47:53 -0400347 supp = groups;
348 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400349 }
350
David Benjaminf04976b2016-10-07 00:37:55 -0400351 for (size_t i = 0; i < pref_len; i++) {
352 for (size_t j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800353 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400354 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500355 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800356 }
357 }
358 }
359
David Benjamin4298d772015-12-19 00:18:25 -0500360 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800361}
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Steven Valdezce902a92016-05-17 11:47:53 -0400363int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 const int *curves, size_t ncurves) {
David Benjamin81678aa2017-07-12 22:43:42 -0400365 uint16_t *group_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
Steven Valdezce902a92016-05-17 11:47:53 -0400366 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800367 return 0;
368 }
369
David Benjamin54091232016-09-05 12:47:25 -0400370 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400371 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
372 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 return 0;
374 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 }
376
Steven Valdezce902a92016-05-17 11:47:53 -0400377 OPENSSL_free(*out_group_ids);
378 *out_group_ids = group_ids;
379 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800380
381 return 1;
382}
Adam Langley95c29f32014-06-20 12:00:00 -0700383
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100384int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
385 const char *curves) {
386 uint16_t *group_ids = NULL;
387 size_t ncurves = 0;
388
389 const char *col;
390 const char *ptr = curves;
391
392 do {
393 col = strchr(ptr, ':');
394
395 uint16_t group_id;
396 if (!ssl_name_to_group_id(&group_id, ptr,
397 col ? (size_t)(col - ptr) : strlen(ptr))) {
398 goto err;
399 }
400
David Benjamin81678aa2017-07-12 22:43:42 -0400401 uint16_t *new_group_ids = (uint16_t *)OPENSSL_realloc(
402 group_ids, (ncurves + 1) * sizeof(uint16_t));
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100403 if (new_group_ids == NULL) {
404 goto err;
405 }
406 group_ids = new_group_ids;
407
408 group_ids[ncurves] = group_id;
409 ncurves++;
410
411 if (col) {
412 ptr = col + 1;
413 }
414 } while (col);
415
416 OPENSSL_free(*out_group_ids);
417 *out_group_ids = group_ids;
418 *out_group_ids_len = ncurves;
419
420 return 1;
421
422err:
423 OPENSSL_free(group_ids);
424 return 0;
425}
426
Steven Valdezce902a92016-05-17 11:47:53 -0400427int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
428 const uint16_t *groups;
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400429 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -0400430 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400431 for (size_t i = 0; i < groups_len; i++) {
432 if (groups[i] == group_id) {
433 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800434 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800435 }
David Benjamin033e5f42014-11-13 18:47:41 -0500436
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400437 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800438}
David Benjamin033e5f42014-11-13 18:47:41 -0500439
David Benjamin3ef76972016-10-17 17:59:54 -0400440/* kVerifySignatureAlgorithms is the default list of accepted signature
David Benjamin3a322f52016-10-26 12:45:35 -0400441 * algorithms for verifying.
442 *
443 * For now, RSA-PSS signature algorithms are not enabled on Android's system
444 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
445 * restore them. */
David Benjamin3ef76972016-10-17 17:59:54 -0400446static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjamin69522112017-03-28 15:38:29 -0500447 /* List our preferred algorithms first. */
448 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400449 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400450#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400451 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400452#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400453 SSL_SIGN_RSA_PKCS1_SHA256,
454
455 /* Larger hashes are acceptable. */
456 SSL_SIGN_ECDSA_SECP384R1_SHA384,
457#if !defined(BORINGSSL_ANDROID_SYSTEM)
458 SSL_SIGN_RSA_PSS_SHA384,
459#endif
460 SSL_SIGN_RSA_PKCS1_SHA384,
461
David Benjamin76bb1412016-09-08 16:03:49 -0400462 /* TODO(davidben): Remove this. */
David Benjamin3ef76972016-10-17 17:59:54 -0400463#if defined(BORINGSSL_ANDROID_SYSTEM)
464 SSL_SIGN_ECDSA_SECP521R1_SHA512,
465#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400466#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400467 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400468#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400469 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400470
David Benjamin3a322f52016-10-26 12:45:35 -0400471 /* For now, SHA-1 is still accepted but least preferable. */
472 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400473
David Benjamin3a322f52016-10-26 12:45:35 -0400474};
475
476/* kSignSignatureAlgorithms is the default list of supported signature
477 * algorithms for signing.
478 *
479 * For now, RSA-PSS signature algorithms are not enabled on Android's system
480 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
481 * restore them. */
482static const uint16_t kSignSignatureAlgorithms[] = {
David Benjamin69522112017-03-28 15:38:29 -0500483 /* List our preferred algorithms first. */
484 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400485 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400486#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400487 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400488#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400489 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400490
David Benjamin3a322f52016-10-26 12:45:35 -0400491 /* If needed, sign larger hashes.
492 *
493 * TODO(davidben): Determine which of these may be pruned. */
494 SSL_SIGN_ECDSA_SECP384R1_SHA384,
495#if !defined(BORINGSSL_ANDROID_SYSTEM)
496 SSL_SIGN_RSA_PSS_SHA384,
497#endif
498 SSL_SIGN_RSA_PKCS1_SHA384,
499
500 SSL_SIGN_ECDSA_SECP521R1_SHA512,
501#if !defined(BORINGSSL_ANDROID_SYSTEM)
502 SSL_SIGN_RSA_PSS_SHA512,
503#endif
504 SSL_SIGN_RSA_PKCS1_SHA512,
505
506 /* If the peer supports nothing else, sign with SHA-1. */
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400507 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400508 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400509};
510
David Benjamin69522112017-03-28 15:38:29 -0500511void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
512 ctx->ed25519_enabled = !!enabled;
513}
514
515int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) {
David Benjamin71c21b42017-04-14 17:05:40 -0400516 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
517 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
518 if (ssl->ctx->num_verify_sigalgs != 0) {
519 sigalgs = ssl->ctx->verify_sigalgs;
520 num_sigalgs = ssl->ctx->num_verify_sigalgs;
521 }
522
523 for (size_t i = 0; i < num_sigalgs; i++) {
524 if (sigalgs == kVerifySignatureAlgorithms &&
525 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500526 !ssl->ctx->ed25519_enabled) {
527 continue;
528 }
David Benjamin71c21b42017-04-14 17:05:40 -0400529 if (!CBB_add_u16(out, sigalgs[i])) {
David Benjamin69522112017-03-28 15:38:29 -0500530 return 0;
531 }
532 }
533
534 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800535}
Adam Langley95c29f32014-06-20 12:00:00 -0700536
David Benjamin8d606e32017-06-15 22:43:04 -0400537int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg) {
David Benjamin71c21b42017-04-14 17:05:40 -0400538 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
539 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
540 if (ssl->ctx->num_verify_sigalgs != 0) {
541 sigalgs = ssl->ctx->verify_sigalgs;
542 num_sigalgs = ssl->ctx->num_verify_sigalgs;
543 }
544
545 for (size_t i = 0; i < num_sigalgs; i++) {
546 if (sigalgs == kVerifySignatureAlgorithms &&
547 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500548 !ssl->ctx->ed25519_enabled) {
549 continue;
550 }
David Benjamin71c21b42017-04-14 17:05:40 -0400551 if (sigalg == sigalgs[i]) {
David Benjamin3ef76972016-10-17 17:59:54 -0400552 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 }
554 }
555
David Benjamin3ef76972016-10-17 17:59:54 -0400556 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
557 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
558 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800559}
560
Adam Langley614c66a2015-06-12 15:26:58 -0700561/* tls_extension represents a TLS extension that is handled internally. The
562 * |init| function is called for each handshake, before any other functions of
563 * the extension. Then the add and parse callbacks are called as needed.
564 *
565 * The parse callbacks receive a |CBS| that contains the contents of the
566 * extension (i.e. not including the type and length bytes). If an extension is
567 * not received then the parse callbacks will be called with a NULL CBS so that
568 * they can do any processing needed to handle the absence of an extension.
569 *
570 * The add callbacks receive a |CBB| to which the extension can be appended but
571 * the function is responsible for appending the type and length bytes too.
572 *
573 * All callbacks return one for success and zero for error. If a parse function
574 * returns zero then a fatal alert with value |*out_alert| will be sent. If
575 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
576struct tls_extension {
577 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500578 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700579
David Benjamin8c880a22016-12-03 02:20:34 -0500580 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
581 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
582 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700583
David Benjamin8c880a22016-12-03 02:20:34 -0500584 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
585 CBS *contents);
586 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700587};
588
David Benjamin8c880a22016-12-03 02:20:34 -0500589static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
590 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400591 if (contents != NULL) {
592 /* Servers MUST NOT send this extension. */
593 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
594 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
595 return 0;
596 }
597
598 return 1;
599}
600
David Benjamin8c880a22016-12-03 02:20:34 -0500601static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
602 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400603 /* This extension from the client is handled elsewhere. */
604 return 1;
605}
606
David Benjamin8c880a22016-12-03 02:20:34 -0500607static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400608 return 1;
609}
Adam Langley614c66a2015-06-12 15:26:58 -0700610
611/* Server name indication (SNI).
612 *
613 * https://tools.ietf.org/html/rfc6066#section-3. */
614
David Benjamin8c880a22016-12-03 02:20:34 -0500615static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
616 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700617 if (ssl->tlsext_hostname == NULL) {
618 return 1;
619 }
620
621 CBB contents, server_name_list, name;
622 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
623 !CBB_add_u16_length_prefixed(out, &contents) ||
624 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
625 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
626 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
627 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
628 strlen(ssl->tlsext_hostname)) ||
629 !CBB_flush(out)) {
630 return 0;
631 }
632
633 return 1;
634}
635
David Benjamin8c880a22016-12-03 02:20:34 -0500636static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500637 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500638 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700639 if (contents == NULL) {
640 return 1;
641 }
642
643 if (CBS_len(contents) != 0) {
644 return 0;
645 }
646
647 assert(ssl->tlsext_hostname != NULL);
648
Steven Valdez87eab492016-06-27 16:34:59 -0400649 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500650 OPENSSL_free(hs->new_session->tlsext_hostname);
651 hs->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
652 if (!hs->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700653 *out_alert = SSL_AD_INTERNAL_ERROR;
654 return 0;
655 }
656 }
657
658 return 1;
659}
660
David Benjamin8c880a22016-12-03 02:20:34 -0500661static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500662 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700663 if (contents == NULL) {
664 return 1;
665 }
666
David Benjamin9b611e22016-03-03 08:48:30 -0500667 CBS server_name_list, host_name;
668 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700669 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500670 !CBS_get_u8(&server_name_list, &name_type) ||
671 /* Although the server_name extension was intended to be extensible to
672 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
673 * different name types will cause an error. Further, RFC 4366 originally
674 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
675 * adding new name types is no longer feasible.
676 *
677 * Act as if the extensibility does not exist to simplify parsing. */
678 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
679 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700680 CBS_len(contents) != 0) {
681 return 0;
682 }
683
David Benjamin9b611e22016-03-03 08:48:30 -0500684 if (name_type != TLSEXT_NAMETYPE_host_name ||
685 CBS_len(&host_name) == 0 ||
686 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
687 CBS_contains_zero_byte(&host_name)) {
688 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
689 return 0;
690 }
Adam Langley614c66a2015-06-12 15:26:58 -0700691
David Benjamin4eb95cc2016-11-16 17:08:23 +0900692 /* Copy the hostname as a string. */
David Benjamin8c880a22016-12-03 02:20:34 -0500693 if (!CBS_strdup(&host_name, &hs->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900694 *out_alert = SSL_AD_INTERNAL_ERROR;
695 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700696 }
697
David Benjamin8c880a22016-12-03 02:20:34 -0500698 hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700699 return 1;
700}
701
David Benjamin8c880a22016-12-03 02:20:34 -0500702static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
703 if (hs->ssl->s3->session_reused ||
704 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700705 return 1;
706 }
707
708 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
709 !CBB_add_u16(out, 0 /* length */)) {
710 return 0;
711 }
712
713 return 1;
714}
715
716
Adam Langley5021b222015-06-12 18:27:58 -0700717/* Renegotiation indication.
718 *
719 * https://tools.ietf.org/html/rfc5746 */
720
David Benjamin8c880a22016-12-03 02:20:34 -0500721static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
722 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400723 /* Renegotiation indication is not necessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -0400724 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400725 return 1;
726 }
727
David Benjamin52bf6902016-10-08 12:05:03 -0400728 assert(ssl->s3->initial_handshake_complete ==
729 (ssl->s3->previous_client_finished_len != 0));
730
Adam Langley5021b222015-06-12 18:27:58 -0700731 CBB contents, prev_finished;
732 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
733 !CBB_add_u16_length_prefixed(out, &contents) ||
734 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
735 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
736 ssl->s3->previous_client_finished_len) ||
737 !CBB_flush(out)) {
738 return 0;
739 }
740
741 return 1;
742}
743
David Benjamin8c880a22016-12-03 02:20:34 -0500744static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700745 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500746 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400747 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez246eeee2017-03-26 12:49:17 -0500748 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400749 return 0;
750 }
751
David Benjamin3e052de2015-11-25 20:10:31 -0500752 /* Servers may not switch between omitting the extension and supporting it.
753 * See RFC 5746, sections 3.5 and 4.2. */
754 if (ssl->s3->initial_handshake_complete &&
755 (contents != NULL) != ssl->s3->send_connection_binding) {
756 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
757 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
758 return 0;
759 }
760
Adam Langley5021b222015-06-12 18:27:58 -0700761 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500762 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700763 * RI even on initial ServerHello because the client doesn't see any
764 * renegotiation during an attack. However this would mean we could not
765 * connect to any server which doesn't support RI.
766 *
David Benjamine9cddb82015-11-23 14:36:40 -0500767 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
768 * practical terms every client sets it so it's just assumed here. */
769 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700770 }
771
772 const size_t expected_len = ssl->s3->previous_client_finished_len +
773 ssl->s3->previous_server_finished_len;
774
775 /* Check for logic errors */
776 assert(!expected_len || ssl->s3->previous_client_finished_len);
777 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400778 assert(ssl->s3->initial_handshake_complete ==
779 (ssl->s3->previous_client_finished_len != 0));
780 assert(ssl->s3->initial_handshake_complete ==
781 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700782
783 /* Parse out the extension contents. */
784 CBS renegotiated_connection;
785 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
786 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400787 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700788 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
789 return 0;
790 }
791
792 /* Check that the extension matches. */
793 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400794 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700795 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
796 return 0;
797 }
798
799 const uint8_t *d = CBS_data(&renegotiated_connection);
David Benjamin9343b0b2017-07-01 00:31:27 -0400800 int ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
801 ssl->s3->previous_client_finished_len) == 0;
802#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
803 ok = 1;
804#endif
805 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400806 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700807 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
808 return 0;
809 }
810 d += ssl->s3->previous_client_finished_len;
811
David Benjamin9343b0b2017-07-01 00:31:27 -0400812 ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
813 ssl->s3->previous_server_finished_len) == 0;
814#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
815 ok = 1;
816#endif
817 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400818 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700819 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
820 return 0;
821 }
822 ssl->s3->send_connection_binding = 1;
823
824 return 1;
825}
826
David Benjamin8c880a22016-12-03 02:20:34 -0500827static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700828 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500829 SSL *const ssl = hs->ssl;
Adam Langley5021b222015-06-12 18:27:58 -0700830 /* Renegotiation isn't supported as a server so this function should never be
831 * called after the initial handshake. */
832 assert(!ssl->s3->initial_handshake_complete);
833
Steven Valdez143e8b32016-07-11 13:19:03 -0400834 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
835 return 1;
836 }
837
Adam Langley5021b222015-06-12 18:27:58 -0700838 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400839 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700840 }
841
842 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700843 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
844 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400845 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700846 return 0;
847 }
848
David Benjamin52bf6902016-10-08 12:05:03 -0400849 /* Check that the extension matches. We do not support renegotiation as a
850 * server, so this must be empty. */
851 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400852 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700853 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
854 return 0;
855 }
856
857 ssl->s3->send_connection_binding = 1;
858
859 return 1;
860}
861
David Benjamin8c880a22016-12-03 02:20:34 -0500862static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
863 SSL *const ssl = hs->ssl;
David Benjamin52bf6902016-10-08 12:05:03 -0400864 /* Renegotiation isn't supported as a server so this function should never be
865 * called after the initial handshake. */
866 assert(!ssl->s3->initial_handshake_complete);
867
Steven Valdez143e8b32016-07-11 13:19:03 -0400868 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
869 return 1;
870 }
871
Adam Langley5021b222015-06-12 18:27:58 -0700872 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400873 !CBB_add_u16(out, 1 /* length */) ||
874 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700875 return 0;
876 }
877
878 return 1;
879}
880
Adam Langley0a056712015-07-01 15:03:33 -0700881
882/* Extended Master Secret.
883 *
David Benjamin43946d42016-02-01 08:42:19 -0500884 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700885
David Benjamin8c880a22016-12-03 02:20:34 -0500886static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400887 /* Extended master secret is not necessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -0400888 if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700889 return 1;
890 }
891
892 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
893 !CBB_add_u16(out, 0 /* length */)) {
894 return 0;
895 }
896
897 return 1;
898}
899
David Benjamin8c880a22016-12-03 02:20:34 -0500900static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700901 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500902 SSL *const ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500903
904 if (contents != NULL) {
905 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
906 ssl->version == SSL3_VERSION ||
907 CBS_len(contents) != 0) {
David Benjamin163c9562016-08-29 23:14:17 -0400908 return 0;
909 }
910
David Benjaminfc02b592017-02-17 16:26:01 -0500911 hs->extended_master_secret = 1;
David Benjamin163c9562016-08-29 23:14:17 -0400912 }
913
David Benjaminfc02b592017-02-17 16:26:01 -0500914 /* Whether EMS is negotiated may not change on renegotiation. */
915 if (ssl->s3->established_session != NULL &&
916 hs->extended_master_secret !=
917 ssl->s3->established_session->extended_master_secret) {
918 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
919 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400920 return 0;
921 }
922
Adam Langley0a056712015-07-01 15:03:33 -0700923 return 1;
924}
925
David Benjamin8c880a22016-12-03 02:20:34 -0500926static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500927 CBS *contents) {
David Benjaminfc02b592017-02-17 16:26:01 -0500928 uint16_t version = ssl3_protocol_version(hs->ssl);
David Benjamin8c880a22016-12-03 02:20:34 -0500929 if (version >= TLS1_3_VERSION ||
930 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400931 return 1;
932 }
933
934 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700935 return 1;
936 }
937
938 if (CBS_len(contents) != 0) {
939 return 0;
940 }
941
David Benjaminfc02b592017-02-17 16:26:01 -0500942 hs->extended_master_secret = 1;
Adam Langley0a056712015-07-01 15:03:33 -0700943 return 1;
944}
945
David Benjamin8c880a22016-12-03 02:20:34 -0500946static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -0500947 if (!hs->extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700948 return 1;
949 }
950
951 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
952 !CBB_add_u16(out, 0 /* length */)) {
953 return 0;
954 }
955
956 return 1;
957}
958
Adam Langley9b05bc52015-07-01 15:25:33 -0700959
960/* Session tickets.
961 *
962 * https://tools.ietf.org/html/rfc5077 */
963
David Benjamin8c880a22016-12-03 02:20:34 -0500964static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
965 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400966 /* TLS 1.3 uses a different ticket extension. */
David Benjamin68161cb2017-06-20 14:49:43 -0400967 if (hs->min_version >= TLS1_3_VERSION ||
David Benjamin7c7d8312016-08-20 13:39:03 -0400968 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700969 return 1;
970 }
971
972 const uint8_t *ticket_data = NULL;
973 int ticket_len = 0;
974
975 /* Renegotiation does not participate in session resumption. However, still
976 * advertise the extension to avoid potentially breaking servers which carry
977 * over the state from the previous handshake, such as OpenSSL servers
978 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
979 if (!ssl->s3->initial_handshake_complete &&
980 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400981 ssl->session->tlsext_tick != NULL &&
982 /* Don't send TLS 1.3 session tickets in the ticket extension. */
Steven Valdez8f36c512017-06-20 10:55:02 -0400983 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700984 ticket_data = ssl->session->tlsext_tick;
985 ticket_len = ssl->session->tlsext_ticklen;
986 }
987
988 CBB ticket;
989 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
990 !CBB_add_u16_length_prefixed(out, &ticket) ||
991 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
992 !CBB_flush(out)) {
993 return 0;
994 }
995
996 return 1;
997}
998
David Benjamin8c880a22016-12-03 02:20:34 -0500999static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -07001000 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001001 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -07001002 if (contents == NULL) {
1003 return 1;
1004 }
1005
Steven Valdez143e8b32016-07-11 13:19:03 -04001006 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1007 return 0;
1008 }
1009
Adam Langley9b05bc52015-07-01 15:25:33 -07001010 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1011 * this function should never be called, even if the server tries to send the
1012 * extension. */
1013 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1014
1015 if (CBS_len(contents) != 0) {
1016 return 0;
1017 }
1018
David Benjamin8c880a22016-12-03 02:20:34 -05001019 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001020 return 1;
1021}
1022
David Benjamin8c880a22016-12-03 02:20:34 -05001023static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1024 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001025 return 1;
1026 }
1027
David Benjamin78476f62016-11-12 11:20:55 +09001028 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin8c880a22016-12-03 02:20:34 -05001029 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001030
1031 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1032 !CBB_add_u16(out, 0 /* length */)) {
1033 return 0;
1034 }
1035
1036 return 1;
1037}
1038
1039
Adam Langley2e857bd2015-07-01 16:09:19 -07001040/* Signature Algorithms.
1041 *
1042 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1043
David Benjamin8c880a22016-12-03 02:20:34 -05001044static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1045 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001046 if (hs->max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001047 return 1;
1048 }
1049
David Benjamin0fc37ef2016-08-17 15:29:46 -04001050 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001051 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1052 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin69522112017-03-28 15:38:29 -05001053 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
1054 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
1055 !CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001056 return 0;
1057 }
1058
1059 return 1;
1060}
1061
David Benjamin8c880a22016-12-03 02:20:34 -05001062static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001063 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001064 OPENSSL_free(hs->peer_sigalgs);
1065 hs->peer_sigalgs = NULL;
1066 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001067
Adam Langley2e857bd2015-07-01 16:09:19 -07001068 if (contents == NULL) {
1069 return 1;
1070 }
1071
1072 CBS supported_signature_algorithms;
1073 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001074 CBS_len(contents) != 0 ||
1075 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001076 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001077 return 0;
1078 }
1079
1080 return 1;
1081}
1082
Adam Langley2e857bd2015-07-01 16:09:19 -07001083
Adam Langleybb0bd042015-07-01 16:21:03 -07001084/* OCSP Stapling.
1085 *
1086 * https://tools.ietf.org/html/rfc6066#section-8 */
1087
David Benjamin8c880a22016-12-03 02:20:34 -05001088static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1089 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001090 if (!ssl->ocsp_stapling_enabled) {
1091 return 1;
1092 }
1093
1094 CBB contents;
1095 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1096 !CBB_add_u16_length_prefixed(out, &contents) ||
1097 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1098 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1099 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1100 !CBB_flush(out)) {
1101 return 0;
1102 }
1103
1104 return 1;
1105}
1106
David Benjamin8c880a22016-12-03 02:20:34 -05001107static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001108 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001109 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001110 if (contents == NULL) {
1111 return 1;
1112 }
1113
Steven Valdeza833c352016-11-01 13:39:36 -04001114 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1115 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001116 return 0;
1117 }
1118
Steven Valdeza833c352016-11-01 13:39:36 -04001119 /* OCSP stapling is forbidden on non-certificate ciphers. */
1120 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001121 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001122 return 0;
1123 }
1124
Steven Valdeza833c352016-11-01 13:39:36 -04001125 /* Note this does not check for resumption in TLS 1.2. Sending
1126 * status_request here does not make sense, but OpenSSL does so and the
1127 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001128
David Benjamin8c880a22016-12-03 02:20:34 -05001129 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001130 return 1;
1131}
1132
David Benjamin8c880a22016-12-03 02:20:34 -05001133static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001134 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001135 if (contents == NULL) {
1136 return 1;
1137 }
1138
1139 uint8_t status_type;
1140 if (!CBS_get_u8(contents, &status_type)) {
1141 return 0;
1142 }
1143
1144 /* We cannot decide whether OCSP stapling will occur yet because the correct
1145 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001146 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001147
Adam Langleybb0bd042015-07-01 16:21:03 -07001148 return 1;
1149}
1150
David Benjamin8c880a22016-12-03 02:20:34 -05001151static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1152 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001153 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001154 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001155 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001156 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001157 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001158 return 1;
1159 }
1160
David Benjamin8c880a22016-12-03 02:20:34 -05001161 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001162
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001163 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001164 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001165}
1166
1167
Adam Langley97dfcbf2015-07-01 18:35:20 -07001168/* Next protocol negotiation.
1169 *
1170 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1171
David Benjamin8c880a22016-12-03 02:20:34 -05001172static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1173 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001174 if (ssl->s3->initial_handshake_complete ||
1175 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001176 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001177 return 1;
1178 }
1179
1180 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1181 !CBB_add_u16(out, 0 /* length */)) {
1182 return 0;
1183 }
1184
1185 return 1;
1186}
1187
David Benjamin8c880a22016-12-03 02:20:34 -05001188static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001189 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001190 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001191 if (contents == NULL) {
1192 return 1;
1193 }
1194
Steven Valdez143e8b32016-07-11 13:19:03 -04001195 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1196 return 0;
1197 }
1198
Adam Langley97dfcbf2015-07-01 18:35:20 -07001199 /* If any of these are false then we should never have sent the NPN
1200 * extension in the ClientHello and thus this function should never have been
1201 * called. */
1202 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001203 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001204 assert(ssl->ctx->next_proto_select_cb != NULL);
1205
David Benjamin76c2efc2015-08-31 14:24:29 -04001206 if (ssl->s3->alpn_selected != NULL) {
1207 /* NPN and ALPN may not be negotiated in the same connection. */
1208 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1209 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1210 return 0;
1211 }
1212
Adam Langley97dfcbf2015-07-01 18:35:20 -07001213 const uint8_t *const orig_contents = CBS_data(contents);
1214 const size_t orig_len = CBS_len(contents);
1215
1216 while (CBS_len(contents) != 0) {
1217 CBS proto;
1218 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1219 CBS_len(&proto) == 0) {
1220 return 0;
1221 }
1222 }
1223
1224 uint8_t *selected;
1225 uint8_t selected_len;
1226 if (ssl->ctx->next_proto_select_cb(
1227 ssl, &selected, &selected_len, orig_contents, orig_len,
1228 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1229 *out_alert = SSL_AD_INTERNAL_ERROR;
1230 return 0;
1231 }
1232
David Benjamin79978df2015-12-25 15:56:49 -05001233 OPENSSL_free(ssl->s3->next_proto_negotiated);
David Benjamin81678aa2017-07-12 22:43:42 -04001234 ssl->s3->next_proto_negotiated =
1235 (uint8_t *)BUF_memdup(selected, selected_len);
David Benjamin79978df2015-12-25 15:56:49 -05001236 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001237 *out_alert = SSL_AD_INTERNAL_ERROR;
1238 return 0;
1239 }
1240
David Benjamin79978df2015-12-25 15:56:49 -05001241 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001242 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001243
1244 return 1;
1245}
1246
David Benjamin8c880a22016-12-03 02:20:34 -05001247static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001248 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001249 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001250 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1251 return 1;
1252 }
1253
Adam Langley97dfcbf2015-07-01 18:35:20 -07001254 if (contents != NULL && CBS_len(contents) != 0) {
1255 return 0;
1256 }
1257
1258 if (contents == NULL ||
1259 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001260 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001261 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001262 return 1;
1263 }
1264
David Benjamin8c880a22016-12-03 02:20:34 -05001265 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001266 return 1;
1267}
1268
David Benjamin8c880a22016-12-03 02:20:34 -05001269static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1270 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001271 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1272 * parsed. */
David Benjamin8c880a22016-12-03 02:20:34 -05001273 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001274 return 1;
1275 }
1276
1277 const uint8_t *npa;
1278 unsigned npa_len;
1279
1280 if (ssl->ctx->next_protos_advertised_cb(
1281 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1282 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001283 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001284 return 1;
1285 }
1286
1287 CBB contents;
1288 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1289 !CBB_add_u16_length_prefixed(out, &contents) ||
1290 !CBB_add_bytes(&contents, npa, npa_len) ||
1291 !CBB_flush(out)) {
1292 return 0;
1293 }
1294
1295 return 1;
1296}
1297
1298
Adam Langleyab8d87d2015-07-10 12:21:39 -07001299/* Signed certificate timestamps.
1300 *
1301 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1302
David Benjamin8c880a22016-12-03 02:20:34 -05001303static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1304 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001305 if (!ssl->signed_cert_timestamps_enabled) {
1306 return 1;
1307 }
1308
1309 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1310 !CBB_add_u16(out, 0 /* length */)) {
1311 return 0;
1312 }
1313
1314 return 1;
1315}
1316
David Benjamin8c880a22016-12-03 02:20:34 -05001317static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001318 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001319 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001320 if (contents == NULL) {
1321 return 1;
1322 }
1323
Steven Valdeza833c352016-11-01 13:39:36 -04001324 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1325 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001326 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001327 return 0;
1328 }
1329
Adam Langleyab8d87d2015-07-10 12:21:39 -07001330 /* If this is false then we should never have sent the SCT extension in the
1331 * ClientHello and thus this function should never have been called. */
1332 assert(ssl->signed_cert_timestamps_enabled);
1333
Adam Langleycfa08c32016-11-17 13:21:27 -08001334 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001335 *out_alert = SSL_AD_DECODE_ERROR;
1336 return 0;
1337 }
1338
David Benjamindaa88502016-10-04 16:32:16 -04001339 /* Session resumption uses the original session information. The extension
1340 * should not be sent on resumption, but RFC 6962 did not make it a
1341 * requirement, so tolerate this.
1342 *
1343 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001344 if (!ssl->s3->session_reused &&
David Benjamin45738dd2017-02-09 20:01:26 -05001345 !CBS_stow(contents, &hs->new_session->tlsext_signed_cert_timestamp_list,
1346 &hs->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001347 *out_alert = SSL_AD_INTERNAL_ERROR;
1348 return 0;
1349 }
1350
1351 return 1;
1352}
1353
David Benjamin8c880a22016-12-03 02:20:34 -05001354static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001355 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001356 if (contents == NULL) {
1357 return 1;
1358 }
1359
1360 if (CBS_len(contents) != 0) {
1361 return 0;
1362 }
1363
David Benjamin8c880a22016-12-03 02:20:34 -05001364 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001365 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001366}
1367
David Benjamin8c880a22016-12-03 02:20:34 -05001368static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1369 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001370 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001371 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1372 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001373 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001374 return 1;
1375 }
1376
1377 CBB contents;
1378 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1379 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001380 CBB_add_bytes(
1381 &contents,
1382 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1383 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001384 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001385}
1386
1387
Adam Langleyf18e4532015-07-10 13:39:53 -07001388/* Application-level Protocol Negotiation.
1389 *
1390 * https://tools.ietf.org/html/rfc7301 */
1391
David Benjamin8c880a22016-12-03 02:20:34 -05001392static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1393 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001394 if (ssl->alpn_client_proto_list == NULL ||
1395 ssl->s3->initial_handshake_complete) {
1396 return 1;
1397 }
1398
1399 CBB contents, proto_list;
1400 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1401 !CBB_add_u16_length_prefixed(out, &contents) ||
1402 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1403 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1404 ssl->alpn_client_proto_list_len) ||
1405 !CBB_flush(out)) {
1406 return 0;
1407 }
1408
1409 return 1;
1410}
1411
David Benjamin8c880a22016-12-03 02:20:34 -05001412static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001413 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001414 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001415 if (contents == NULL) {
1416 return 1;
1417 }
1418
1419 assert(!ssl->s3->initial_handshake_complete);
1420 assert(ssl->alpn_client_proto_list != NULL);
1421
David Benjamin8c880a22016-12-03 02:20:34 -05001422 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001423 /* NPN and ALPN may not be negotiated in the same connection. */
1424 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1425 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1426 return 0;
1427 }
1428
Adam Langleyf18e4532015-07-10 13:39:53 -07001429 /* The extension data consists of a ProtocolNameList which must have
1430 * exactly one ProtocolName. Each of these is length-prefixed. */
1431 CBS protocol_name_list, protocol_name;
1432 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1433 CBS_len(contents) != 0 ||
1434 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1435 /* Empty protocol names are forbidden. */
1436 CBS_len(&protocol_name) == 0 ||
1437 CBS_len(&protocol_name_list) != 0) {
1438 return 0;
1439 }
1440
David Benjaminc8ff30c2017-04-04 13:52:36 -04001441 if (!ssl->ctx->allow_unknown_alpn_protos) {
1442 /* Check that the protocol name is one of the ones we advertised. */
1443 int protocol_ok = 0;
1444 CBS client_protocol_name_list, client_protocol_name;
1445 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1446 ssl->alpn_client_proto_list_len);
1447 while (CBS_len(&client_protocol_name_list) > 0) {
1448 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1449 &client_protocol_name)) {
1450 *out_alert = SSL_AD_INTERNAL_ERROR;
1451 return 0;
1452 }
1453
1454 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1455 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1456 CBS_data(&protocol_name),
1457 CBS_len(&protocol_name)) == 0) {
1458 protocol_ok = 1;
1459 break;
1460 }
1461 }
1462
1463 if (!protocol_ok) {
1464 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1465 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3e517572016-08-11 11:52:23 -04001466 return 0;
1467 }
David Benjamin3e517572016-08-11 11:52:23 -04001468 }
1469
Adam Langleyf18e4532015-07-10 13:39:53 -07001470 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1471 &ssl->s3->alpn_selected_len)) {
1472 *out_alert = SSL_AD_INTERNAL_ERROR;
1473 return 0;
1474 }
1475
1476 return 1;
1477}
1478
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001479int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001480 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001481 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001482 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001483 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001484 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001485 client_hello, &contents,
1486 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1487 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001488 return 1;
1489 }
1490
1491 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001492 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001493
1494 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001495 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1496 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001497 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001498 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1499 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001500 return 0;
1501 }
1502
1503 /* Validate the protocol list. */
1504 CBS protocol_name_list_copy = protocol_name_list;
1505 while (CBS_len(&protocol_name_list_copy) > 0) {
1506 CBS protocol_name;
1507
1508 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1509 /* Empty protocol names are forbidden. */
1510 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001511 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1512 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001513 return 0;
1514 }
1515 }
1516
1517 const uint8_t *selected;
1518 uint8_t selected_len;
1519 if (ssl->ctx->alpn_select_cb(
1520 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1521 CBS_len(&protocol_name_list),
1522 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1523 OPENSSL_free(ssl->s3->alpn_selected);
David Benjamin81678aa2017-07-12 22:43:42 -04001524 ssl->s3->alpn_selected = (uint8_t *)BUF_memdup(selected, selected_len);
Adam Langleyf18e4532015-07-10 13:39:53 -07001525 if (ssl->s3->alpn_selected == NULL) {
1526 *out_alert = SSL_AD_INTERNAL_ERROR;
1527 return 0;
1528 }
1529 ssl->s3->alpn_selected_len = selected_len;
1530 }
1531
1532 return 1;
1533}
1534
David Benjamin8c880a22016-12-03 02:20:34 -05001535static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1536 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001537 if (ssl->s3->alpn_selected == NULL) {
1538 return 1;
1539 }
1540
1541 CBB contents, proto_list, proto;
1542 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1543 !CBB_add_u16_length_prefixed(out, &contents) ||
1544 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1545 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001546 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1547 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001548 !CBB_flush(out)) {
1549 return 0;
1550 }
1551
1552 return 1;
1553}
1554
1555
Adam Langley49c7af12015-07-10 14:33:46 -07001556/* Channel ID.
1557 *
1558 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1559
David Benjamin8c880a22016-12-03 02:20:34 -05001560static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1561 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001562}
1563
David Benjamin8c880a22016-12-03 02:20:34 -05001564static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1565 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001566 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001567 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001568 return 1;
1569 }
1570
1571 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1572 !CBB_add_u16(out, 0 /* length */)) {
1573 return 0;
1574 }
1575
1576 return 1;
1577}
1578
David Benjamin8c880a22016-12-03 02:20:34 -05001579static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1580 uint8_t *out_alert, CBS *contents) {
1581 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001582 if (contents == NULL) {
1583 return 1;
1584 }
1585
David Benjamince079fd2016-08-02 16:22:34 -04001586 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001587 assert(ssl->tlsext_channel_id_enabled);
1588
1589 if (CBS_len(contents) != 0) {
1590 return 0;
1591 }
1592
1593 ssl->s3->tlsext_channel_id_valid = 1;
1594 return 1;
1595}
1596
David Benjamin8c880a22016-12-03 02:20:34 -05001597static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1598 uint8_t *out_alert, CBS *contents) {
1599 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001600 if (contents == NULL ||
1601 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001602 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001603 return 1;
1604 }
1605
1606 if (CBS_len(contents) != 0) {
1607 return 0;
1608 }
1609
1610 ssl->s3->tlsext_channel_id_valid = 1;
1611 return 1;
1612}
1613
David Benjamin8c880a22016-12-03 02:20:34 -05001614static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1615 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001616 if (!ssl->s3->tlsext_channel_id_valid) {
1617 return 1;
1618 }
1619
1620 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1621 !CBB_add_u16(out, 0 /* length */)) {
1622 return 0;
1623 }
1624
1625 return 1;
1626}
1627
Adam Langley391250d2015-07-15 19:06:07 -07001628
1629/* Secure Real-time Transport Protocol (SRTP) extension.
1630 *
1631 * https://tools.ietf.org/html/rfc5764 */
1632
Adam Langley391250d2015-07-15 19:06:07 -07001633
David Benjamin8c880a22016-12-03 02:20:34 -05001634static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1635 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001636}
1637
David Benjamin8c880a22016-12-03 02:20:34 -05001638static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1639 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001640 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1641 if (profiles == NULL) {
1642 return 1;
1643 }
1644 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1645 if (num_profiles == 0) {
1646 return 1;
1647 }
1648
1649 CBB contents, profile_ids;
1650 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1651 !CBB_add_u16_length_prefixed(out, &contents) ||
1652 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1653 return 0;
1654 }
1655
David Benjamin54091232016-09-05 12:47:25 -04001656 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001657 if (!CBB_add_u16(&profile_ids,
1658 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1659 return 0;
1660 }
1661 }
1662
1663 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1664 !CBB_flush(out)) {
1665 return 0;
1666 }
1667
1668 return 1;
1669}
1670
David Benjamin8c880a22016-12-03 02:20:34 -05001671static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001672 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001673 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001674 if (contents == NULL) {
1675 return 1;
1676 }
1677
1678 /* The extension consists of a u16-prefixed profile ID list containing a
1679 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1680 *
1681 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1682 CBS profile_ids, srtp_mki;
1683 uint16_t profile_id;
1684 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1685 !CBS_get_u16(&profile_ids, &profile_id) ||
1686 CBS_len(&profile_ids) != 0 ||
1687 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1688 CBS_len(contents) != 0) {
1689 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1690 return 0;
1691 }
1692
1693 if (CBS_len(&srtp_mki) != 0) {
1694 /* Must be no MKI, since we never offer one. */
1695 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1696 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1697 return 0;
1698 }
1699
1700 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1701
1702 /* Check to see if the server gave us something we support (and presumably
1703 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001704 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001705 const SRTP_PROTECTION_PROFILE *profile =
1706 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1707
1708 if (profile->id == profile_id) {
1709 ssl->srtp_profile = profile;
1710 return 1;
1711 }
1712 }
1713
1714 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1715 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1716 return 0;
1717}
1718
David Benjamin8c880a22016-12-03 02:20:34 -05001719static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001720 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001721 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001722 if (contents == NULL) {
1723 return 1;
1724 }
1725
1726 CBS profile_ids, srtp_mki;
1727 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1728 CBS_len(&profile_ids) < 2 ||
1729 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1730 CBS_len(contents) != 0) {
1731 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1732 return 0;
1733 }
1734 /* Discard the MKI value for now. */
1735
1736 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1737 SSL_get_srtp_profiles(ssl);
1738
1739 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001740 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001741 const SRTP_PROTECTION_PROFILE *server_profile =
1742 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1743
1744 CBS profile_ids_tmp;
1745 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1746
1747 while (CBS_len(&profile_ids_tmp) > 0) {
1748 uint16_t profile_id;
1749 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1750 return 0;
1751 }
1752
1753 if (server_profile->id == profile_id) {
1754 ssl->srtp_profile = server_profile;
1755 return 1;
1756 }
1757 }
1758 }
1759
1760 return 1;
1761}
1762
David Benjamin8c880a22016-12-03 02:20:34 -05001763static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1764 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001765 if (ssl->srtp_profile == NULL) {
1766 return 1;
1767 }
1768
1769 CBB contents, profile_ids;
1770 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1771 !CBB_add_u16_length_prefixed(out, &contents) ||
1772 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1773 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1774 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1775 !CBB_flush(out)) {
1776 return 0;
1777 }
1778
1779 return 1;
1780}
1781
Adam Langleybdd5d662015-07-20 16:19:08 -07001782
1783/* EC point formats.
1784 *
1785 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1786
David Benjamin8c880a22016-12-03 02:20:34 -05001787static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001788 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001789 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1790 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001791 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1792 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001793 !CBB_flush(out)) {
1794 return 0;
1795 }
1796
1797 return 1;
1798}
1799
David Benjamin8c880a22016-12-03 02:20:34 -05001800static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001801 /* The point format extension is unneccessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -04001802 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin70aba262016-11-01 12:08:15 -04001803 return 1;
1804 }
1805
David Benjamin8c880a22016-12-03 02:20:34 -05001806 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001807}
1808
David Benjamin8c880a22016-12-03 02:20:34 -05001809static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001810 CBS *contents) {
1811 if (contents == NULL) {
1812 return 1;
1813 }
1814
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 0;
1817 }
1818
Adam Langleybdd5d662015-07-20 16:19:08 -07001819 CBS ec_point_format_list;
1820 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1821 CBS_len(contents) != 0) {
1822 return 0;
1823 }
1824
David Benjaminfc059942015-07-30 23:01:59 -04001825 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1826 * point format. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05001827 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1828 TLSEXT_ECPOINTFORMAT_uncompressed,
1829 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001830 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001831 return 0;
1832 }
1833
1834 return 1;
1835}
1836
David Benjamin8c880a22016-12-03 02:20:34 -05001837static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001838 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001839 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001840 return 1;
1841 }
1842
David Benjamin8c880a22016-12-03 02:20:34 -05001843 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001844}
1845
David Benjamin8c880a22016-12-03 02:20:34 -05001846static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1847 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001848 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1849 return 1;
1850 }
1851
David Benjamin45738dd2017-02-09 20:01:26 -05001852 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1853 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001854 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001855
1856 if (!using_ecc) {
1857 return 1;
1858 }
1859
David Benjamin8c880a22016-12-03 02:20:34 -05001860 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001861}
1862
Steven Valdeza833c352016-11-01 13:39:36 -04001863
Steven Valdez4aa154e2016-07-29 14:32:55 -04001864/* Pre Shared Key
1865 *
Steven Valdeza833c352016-11-01 13:39:36 -04001866 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001867
David Benjamin8c880a22016-12-03 02:20:34 -05001868static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1869 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001870 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001871 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001872 return 0;
1873 }
1874
Steven Valdez8f36c512017-06-20 10:55:02 -04001875 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001876 return 15 + ssl->session->tlsext_ticklen + binder_len;
1877}
1878
David Benjamin8c880a22016-12-03 02:20:34 -05001879static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1880 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001881 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001882 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001883 return 1;
1884 }
1885
David Benjaminad8f5e12017-02-20 17:00:20 -05001886 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001887 ssl_get_current_time(ssl, &now);
1888 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1889 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1890
1891 /* Fill in a placeholder zero binder of the appropriate length. It will be
1892 * computed and filled in later after length prefixes are computed. */
1893 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez8f36c512017-06-20 10:55:02 -04001894 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001895
1896 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001897 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1898 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001899 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001900 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1901 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001902 ssl->session->tlsext_ticklen) ||
1903 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1904 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1905 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1906 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001907 return 0;
1908 }
1909
David Benjamin8c880a22016-12-03 02:20:34 -05001910 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001911 return CBB_flush(out);
1912}
1913
David Benjamin8baf9632016-11-17 17:11:16 +09001914int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1915 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001916 CBS *contents) {
1917 uint16_t psk_id;
1918 if (!CBS_get_u16(contents, &psk_id) ||
1919 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001920 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001921 *out_alert = SSL_AD_DECODE_ERROR;
1922 return 0;
1923 }
1924
David Benjamineab773a2016-11-09 18:32:35 -05001925 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001926 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001927 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001928 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1929 return 0;
1930 }
1931
1932 return 1;
1933}
1934
David Benjamin35ac5b72017-03-03 15:05:56 -05001935int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001936 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001937 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04001938 /* We only process the first PSK identity since we don't support pure PSK. */
David Benjamin707af292017-03-10 17:47:18 -05001939 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001940 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001941 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001942 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001943 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001944 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001945 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001946 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001947 *out_alert = SSL_AD_DECODE_ERROR;
1948 return 0;
1949 }
1950
Steven Valdeza833c352016-11-01 13:39:36 -04001951 *out_binders = binders;
1952
David Benjaminaedf3032016-12-01 16:47:56 -05001953 /* Check the syntax of the remaining identities, but do not process them. */
1954 size_t num_identities = 1;
1955 while (CBS_len(&identities) != 0) {
1956 CBS unused_ticket;
1957 uint32_t unused_obfuscated_ticket_age;
1958 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1959 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1960 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1961 *out_alert = SSL_AD_DECODE_ERROR;
1962 return 0;
1963 }
1964
1965 num_identities++;
1966 }
1967
1968 /* Check the syntax of the binders. The value will be checked later if
1969 * resuming. */
1970 size_t num_binders = 0;
1971 while (CBS_len(&binders) != 0) {
1972 CBS binder;
1973 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1974 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1975 *out_alert = SSL_AD_DECODE_ERROR;
1976 return 0;
1977 }
1978
1979 num_binders++;
1980 }
1981
1982 if (num_identities != num_binders) {
1983 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1984 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04001985 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04001986 }
1987
David Benjamine7f60a22016-11-16 18:54:25 +09001988 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001989}
1990
David Benjamin8baf9632016-11-17 17:11:16 +09001991int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1992 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001993 return 1;
1994 }
1995
1996 CBB contents;
1997 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1998 !CBB_add_u16_length_prefixed(out, &contents) ||
1999 /* We only consider the first identity for resumption */
2000 !CBB_add_u16(&contents, 0) ||
2001 !CBB_flush(out)) {
2002 return 0;
2003 }
2004
2005 return 1;
2006}
2007
2008
Steven Valdeza833c352016-11-01 13:39:36 -04002009/* Pre-Shared Key Exchange Modes
2010 *
2011 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002012
David Benjamin8c880a22016-12-03 02:20:34 -05002013static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2014 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04002015 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04002016 return 1;
2017 }
2018
2019 CBB contents, ke_modes;
2020 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2021 !CBB_add_u16_length_prefixed(out, &contents) ||
2022 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2023 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2024 return 0;
2025 }
2026
2027 return CBB_flush(out);
2028}
2029
David Benjamin8c880a22016-12-03 02:20:34 -05002030static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002031 uint8_t *out_alert,
2032 CBS *contents) {
2033 if (contents == NULL) {
2034 return 1;
2035 }
2036
Steven Valdeza833c352016-11-01 13:39:36 -04002037 CBS ke_modes;
2038 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2039 CBS_len(&ke_modes) == 0 ||
2040 CBS_len(contents) != 0) {
2041 *out_alert = SSL_AD_DECODE_ERROR;
2042 return 0;
2043 }
2044
2045 /* We only support tickets with PSK_DHE_KE. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05002046 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2047 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002048
2049 return 1;
2050}
2051
2052
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002053/* Early Data Indication
2054 *
2055 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2056
David Benjamin8c880a22016-12-03 02:20:34 -05002057static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002058 SSL *const ssl = hs->ssl;
Steven Valdez2d850622017-01-11 11:34:52 -05002059 if (ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04002060 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION ||
Steven Valdez2d850622017-01-11 11:34:52 -05002061 ssl->session->ticket_max_early_data == 0 ||
2062 hs->received_hello_retry_request ||
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05002063 !ssl->cert->enable_early_data) {
Steven Valdez2d850622017-01-11 11:34:52 -05002064 return 1;
2065 }
2066
2067 hs->early_data_offered = 1;
2068
2069 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2070 !CBB_add_u16(out, 0) ||
2071 !CBB_flush(out)) {
2072 return 0;
2073 }
2074
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002075 return 1;
2076}
2077
Steven Valdez2d850622017-01-11 11:34:52 -05002078static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002079 uint8_t *out_alert, CBS *contents) {
2080 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002081 if (contents == NULL) {
2082 return 1;
2083 }
2084
2085 if (CBS_len(contents) != 0) {
2086 *out_alert = SSL_AD_DECODE_ERROR;
2087 return 0;
2088 }
2089
Steven Valdez2d850622017-01-11 11:34:52 -05002090 if (!ssl->s3->session_reused) {
2091 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2092 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2093 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002094 }
Steven Valdez2d850622017-01-11 11:34:52 -05002095
2096 ssl->early_data_accepted = 1;
2097 return 1;
2098}
2099
2100static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2101 uint8_t *out_alert, CBS *contents) {
2102 SSL *const ssl = hs->ssl;
2103 if (contents == NULL ||
2104 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2105 return 1;
2106 }
2107
2108 if (CBS_len(contents) != 0) {
2109 *out_alert = SSL_AD_DECODE_ERROR;
2110 return 0;
2111 }
2112
2113 hs->early_data_offered = 1;
2114 return 1;
2115}
2116
2117static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2118 if (!hs->ssl->early_data_accepted) {
2119 return 1;
2120 }
2121
2122 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2123 !CBB_add_u16(out, 0) ||
2124 !CBB_flush(out)) {
2125 return 0;
2126 }
2127
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002128 return 1;
2129}
2130
2131
Steven Valdez143e8b32016-07-11 13:19:03 -04002132/* Key Share
2133 *
David Benjamina128a552016-10-13 14:26:33 -04002134 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002135
David Benjamin8c880a22016-12-03 02:20:34 -05002136static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2137 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002138 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002139 return 1;
2140 }
2141
2142 CBB contents, kse_bytes;
2143 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2144 !CBB_add_u16_length_prefixed(out, &contents) ||
2145 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2146 return 0;
2147 }
2148
David Benjamin8c880a22016-12-03 02:20:34 -05002149 uint16_t group_id = hs->retry_group;
2150 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002151 /* We received a HelloRetryRequest without a new curve, so there is no new
2152 * share to append. Leave |ecdh_ctx| as-is. */
2153 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002154 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2155 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002156 return 0;
2157 }
David Benjamin8c880a22016-12-03 02:20:34 -05002158 OPENSSL_free(hs->key_share_bytes);
2159 hs->key_share_bytes = NULL;
2160 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002161 if (group_id == 0) {
2162 return CBB_flush(out);
2163 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002164 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002165 /* Add a fake group. See draft-davidben-tls-grease-01. */
2166 if (ssl->ctx->grease_enabled &&
2167 (!CBB_add_u16(&kse_bytes,
2168 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2169 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2170 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2171 return 0;
2172 }
2173
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002174 /* Predict the most preferred group. */
2175 const uint16_t *groups;
2176 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002177 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002178 if (groups_len == 0) {
2179 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2180 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002181 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002182
2183 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002184 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002185
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002186 CBB key_exchange;
2187 if (!CBB_add_u16(&kse_bytes, group_id) ||
2188 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002189 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2190 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002191 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002192 return 0;
2193 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002194
David Benjamin8c880a22016-12-03 02:20:34 -05002195 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002196 /* Save the contents of the extension to repeat it in the second
2197 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002198 hs->key_share_bytes_len = CBB_len(&kse_bytes);
David Benjamin81678aa2017-07-12 22:43:42 -04002199 hs->key_share_bytes =
2200 (uint8_t *)BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
David Benjamin8c880a22016-12-03 02:20:34 -05002201 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002202 return 0;
2203 }
2204 }
2205
Steven Valdez143e8b32016-07-11 13:19:03 -04002206 return CBB_flush(out);
2207}
2208
David Benjamin8baf9632016-11-17 17:11:16 +09002209int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002210 size_t *out_secret_len,
2211 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002212 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002213 uint16_t group_id;
2214 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002215 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2216 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002217 *out_alert = SSL_AD_DECODE_ERROR;
2218 return 0;
2219 }
2220
David Benjamin8baf9632016-11-17 17:11:16 +09002221 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002222 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2223 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2224 return 0;
2225 }
2226
David Benjamin8baf9632016-11-17 17:11:16 +09002227 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2228 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002229 *out_alert = SSL_AD_INTERNAL_ERROR;
2230 return 0;
2231 }
2232
David Benjamin45738dd2017-02-09 20:01:26 -05002233 hs->new_session->group_id = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002234 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002235 return 1;
2236}
2237
David Benjamin8baf9632016-11-17 17:11:16 +09002238int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002239 uint8_t **out_secret,
2240 size_t *out_secret_len,
2241 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002242 uint16_t group_id;
2243 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002244 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002245 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2246 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2247 return 0;
2248 }
2249
2250 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002251 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002252 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002253 return 0;
2254 }
2255
David Benjamin7e1f9842016-09-20 19:24:40 -04002256 /* Find the corresponding key share. */
2257 int found = 0;
2258 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002259 while (CBS_len(&key_shares) > 0) {
2260 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002261 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002262 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002263 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2264 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002265 return 0;
2266 }
2267
David Benjamin7e1f9842016-09-20 19:24:40 -04002268 if (id == group_id) {
2269 if (found) {
2270 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2271 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2272 return 0;
2273 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002274
David Benjamin7e1f9842016-09-20 19:24:40 -04002275 found = 1;
2276 peer_key = peer_key_tmp;
2277 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002278 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002279 }
2280
David Benjamin7e1f9842016-09-20 19:24:40 -04002281 if (!found) {
2282 *out_found = 0;
2283 *out_secret = NULL;
2284 *out_secret_len = 0;
2285 return 1;
2286 }
2287
2288 /* Compute the DH secret. */
2289 uint8_t *secret = NULL;
2290 size_t secret_len;
2291 SSL_ECDH_CTX group;
David Benjamin17cf2cb2016-12-13 01:07:13 -05002292 OPENSSL_memset(&group, 0, sizeof(SSL_ECDH_CTX));
David Benjamin7e1f9842016-09-20 19:24:40 -04002293 CBB public_key;
2294 if (!CBB_init(&public_key, 32) ||
2295 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002296 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
2297 CBS_data(&peer_key), CBS_len(&peer_key)) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002298 !CBB_finish(&public_key, &hs->ecdh_public_key,
2299 &hs->ecdh_public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002300 OPENSSL_free(secret);
2301 SSL_ECDH_CTX_cleanup(&group);
2302 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002303 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002304 return 0;
2305 }
2306
2307 SSL_ECDH_CTX_cleanup(&group);
2308
2309 *out_secret = secret;
2310 *out_secret_len = secret_len;
2311 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002312 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002313}
2314
David Benjamin8baf9632016-11-17 17:11:16 +09002315int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002316 uint16_t group_id;
2317 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002318 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002319 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2320 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2321 !CBB_add_u16(&kse_bytes, group_id) ||
2322 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002323 !CBB_add_bytes(&public_key, hs->ecdh_public_key,
2324 hs->ecdh_public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002325 !CBB_flush(out)) {
2326 return 0;
2327 }
2328
David Benjaminbf833c32017-03-30 15:45:21 -05002329 OPENSSL_free(hs->ecdh_public_key);
2330 hs->ecdh_public_key = NULL;
2331 hs->ecdh_public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002332
David Benjamin45738dd2017-02-09 20:01:26 -05002333 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002334 return 1;
2335}
2336
2337
Steven Valdezfdd10992016-09-15 16:27:05 -04002338/* Supported Versions
2339 *
2340 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2341
David Benjamin8c880a22016-12-03 02:20:34 -05002342static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2343 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002344 if (hs->max_version <= TLS1_2_VERSION) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002345 return 1;
2346 }
2347
2348 CBB contents, versions;
2349 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2350 !CBB_add_u16_length_prefixed(out, &contents) ||
2351 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2352 return 0;
2353 }
2354
David Benjamind9791bf2016-09-27 16:39:52 -04002355 /* Add a fake version. See draft-davidben-tls-grease-01. */
2356 if (ssl->ctx->grease_enabled &&
2357 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2358 return 0;
2359 }
2360
Steven Valdez8f36c512017-06-20 10:55:02 -04002361 if (!ssl_add_supported_versions(hs, &versions) ||
2362 !CBB_flush(out)) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002363 return 0;
2364 }
2365
2366 return 1;
2367}
2368
2369
David Benjamin3baa6e12016-10-07 21:10:38 -04002370/* Cookie
2371 *
2372 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2373
David Benjamin8c880a22016-12-03 02:20:34 -05002374static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2375 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002376 return 1;
2377 }
2378
2379 CBB contents, cookie;
2380 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2381 !CBB_add_u16_length_prefixed(out, &contents) ||
2382 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002383 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002384 !CBB_flush(out)) {
2385 return 0;
2386 }
2387
2388 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002389 OPENSSL_free(hs->cookie);
2390 hs->cookie = NULL;
2391 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002392 return 1;
2393}
2394
2395
Steven Valdezce902a92016-05-17 11:47:53 -04002396/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002397 *
Steven Valdezce902a92016-05-17 11:47:53 -04002398 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002399 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002400
David Benjamin8c880a22016-12-03 02:20:34 -05002401static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2402 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002403 CBB contents, groups_bytes;
2404 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002405 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002406 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002407 return 0;
2408 }
2409
David Benjamin65ac9972016-09-02 21:35:25 -04002410 /* Add a fake group. See draft-davidben-tls-grease-01. */
2411 if (ssl->ctx->grease_enabled &&
2412 !CBB_add_u16(&groups_bytes,
2413 ssl_get_grease_value(ssl, ssl_grease_group))) {
2414 return 0;
2415 }
2416
Steven Valdezce902a92016-05-17 11:47:53 -04002417 const uint16_t *groups;
2418 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002419 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002420
David Benjamin54091232016-09-05 12:47:25 -04002421 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002422 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002423 return 0;
2424 }
2425 }
2426
2427 return CBB_flush(out);
2428}
2429
David Benjamin8c880a22016-12-03 02:20:34 -05002430static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2431 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002432 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002433 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2434 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002435 return 1;
2436}
2437
David Benjamin8c880a22016-12-03 02:20:34 -05002438static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2439 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002440 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002441 if (contents == NULL) {
2442 return 1;
2443 }
2444
Steven Valdezce902a92016-05-17 11:47:53 -04002445 CBS supported_group_list;
2446 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2447 CBS_len(&supported_group_list) == 0 ||
2448 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002449 CBS_len(contents) != 0) {
2450 return 0;
2451 }
2452
David Benjamin8c880a22016-12-03 02:20:34 -05002453 hs->peer_supported_group_list =
David Benjamin81678aa2017-07-12 22:43:42 -04002454 (uint16_t *)OPENSSL_malloc(CBS_len(&supported_group_list));
David Benjamin8c880a22016-12-03 02:20:34 -05002455 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002456 *out_alert = SSL_AD_INTERNAL_ERROR;
2457 return 0;
2458 }
2459
Steven Valdezce902a92016-05-17 11:47:53 -04002460 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002461 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002462 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002463 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002464 goto err;
2465 }
2466 }
2467
Steven Valdezce902a92016-05-17 11:47:53 -04002468 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002469 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002470
2471 return 1;
2472
2473err:
David Benjamin8c880a22016-12-03 02:20:34 -05002474 OPENSSL_free(hs->peer_supported_group_list);
2475 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002476 *out_alert = SSL_AD_INTERNAL_ERROR;
2477 return 0;
2478}
2479
David Benjamin8c880a22016-12-03 02:20:34 -05002480static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002481 /* Servers don't echo this extension. */
2482 return 1;
2483}
2484
2485
Adam Langley614c66a2015-06-12 15:26:58 -07002486/* kExtensions contains all the supported extensions. */
2487static const struct tls_extension kExtensions[] = {
2488 {
Adam Langley5021b222015-06-12 18:27:58 -07002489 TLSEXT_TYPE_renegotiate,
2490 NULL,
2491 ext_ri_add_clienthello,
2492 ext_ri_parse_serverhello,
2493 ext_ri_parse_clienthello,
2494 ext_ri_add_serverhello,
2495 },
2496 {
Adam Langley614c66a2015-06-12 15:26:58 -07002497 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002498 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002499 ext_sni_add_clienthello,
2500 ext_sni_parse_serverhello,
2501 ext_sni_parse_clienthello,
2502 ext_sni_add_serverhello,
2503 },
Adam Langley0a056712015-07-01 15:03:33 -07002504 {
2505 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002506 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002507 ext_ems_add_clienthello,
2508 ext_ems_parse_serverhello,
2509 ext_ems_parse_clienthello,
2510 ext_ems_add_serverhello,
2511 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002512 {
2513 TLSEXT_TYPE_session_ticket,
2514 NULL,
2515 ext_ticket_add_clienthello,
2516 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002517 /* Ticket extension client parsing is handled in ssl_session.c */
2518 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002519 ext_ticket_add_serverhello,
2520 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002521 {
2522 TLSEXT_TYPE_signature_algorithms,
2523 NULL,
2524 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002525 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002526 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002527 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002528 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002529 {
2530 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002531 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002532 ext_ocsp_add_clienthello,
2533 ext_ocsp_parse_serverhello,
2534 ext_ocsp_parse_clienthello,
2535 ext_ocsp_add_serverhello,
2536 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002537 {
2538 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002539 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002540 ext_npn_add_clienthello,
2541 ext_npn_parse_serverhello,
2542 ext_npn_parse_clienthello,
2543 ext_npn_add_serverhello,
2544 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002545 {
2546 TLSEXT_TYPE_certificate_timestamp,
2547 NULL,
2548 ext_sct_add_clienthello,
2549 ext_sct_parse_serverhello,
2550 ext_sct_parse_clienthello,
2551 ext_sct_add_serverhello,
2552 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002553 {
2554 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002555 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002556 ext_alpn_add_clienthello,
2557 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002558 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2559 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002560 ext_alpn_add_serverhello,
2561 },
Adam Langley49c7af12015-07-10 14:33:46 -07002562 {
2563 TLSEXT_TYPE_channel_id,
2564 ext_channel_id_init,
2565 ext_channel_id_add_clienthello,
2566 ext_channel_id_parse_serverhello,
2567 ext_channel_id_parse_clienthello,
2568 ext_channel_id_add_serverhello,
2569 },
Adam Langley391250d2015-07-15 19:06:07 -07002570 {
2571 TLSEXT_TYPE_srtp,
2572 ext_srtp_init,
2573 ext_srtp_add_clienthello,
2574 ext_srtp_parse_serverhello,
2575 ext_srtp_parse_clienthello,
2576 ext_srtp_add_serverhello,
2577 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002578 {
2579 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002580 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002581 ext_ec_point_add_clienthello,
2582 ext_ec_point_parse_serverhello,
2583 ext_ec_point_parse_clienthello,
2584 ext_ec_point_add_serverhello,
2585 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002586 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002587 TLSEXT_TYPE_key_share,
2588 NULL,
2589 ext_key_share_add_clienthello,
2590 forbid_parse_serverhello,
2591 ignore_parse_clienthello,
2592 dont_add_serverhello,
2593 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002594 {
Steven Valdeza833c352016-11-01 13:39:36 -04002595 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002596 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002597 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002598 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002599 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002600 dont_add_serverhello,
2601 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002602 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002603 TLSEXT_TYPE_early_data,
2604 NULL,
2605 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002606 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002607 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002608 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002609 },
2610 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002611 TLSEXT_TYPE_supported_versions,
2612 NULL,
2613 ext_supported_versions_add_clienthello,
2614 forbid_parse_serverhello,
2615 ignore_parse_clienthello,
2616 dont_add_serverhello,
2617 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002618 {
2619 TLSEXT_TYPE_cookie,
2620 NULL,
2621 ext_cookie_add_clienthello,
2622 forbid_parse_serverhello,
2623 ignore_parse_clienthello,
2624 dont_add_serverhello,
2625 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002626 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2627 * intolerant to the last extension being zero-length. See
2628 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002629 {
Steven Valdezce902a92016-05-17 11:47:53 -04002630 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002631 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002632 ext_supported_groups_add_clienthello,
2633 ext_supported_groups_parse_serverhello,
2634 ext_supported_groups_parse_clienthello,
2635 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002636 },
Adam Langley614c66a2015-06-12 15:26:58 -07002637};
2638
2639#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2640
David Benjamina3d76d02017-07-14 19:36:07 -04002641static_assert(kNumExtensions <=
2642 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
2643 "too many extensions for sent bitset");
2644static_assert(kNumExtensions <=
2645 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2646 "too many extensions for received bitset");
Adam Langley4cfa96b2015-07-01 11:56:55 -07002647
Adam Langley614c66a2015-06-12 15:26:58 -07002648static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2649 uint16_t value) {
2650 unsigned i;
2651 for (i = 0; i < kNumExtensions; i++) {
2652 if (kExtensions[i].value == value) {
2653 *out_index = i;
2654 return &kExtensions[i];
2655 }
2656 }
2657
2658 return NULL;
2659}
2660
Adam Langley09505632015-07-30 18:10:13 -07002661int SSL_extension_supported(unsigned extension_value) {
2662 uint32_t index;
2663 return extension_value == TLSEXT_TYPE_padding ||
2664 tls_extension_find(&index, extension_value) != NULL;
2665}
2666
David Benjamin8c880a22016-12-03 02:20:34 -05002667int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2668 SSL *const ssl = hs->ssl;
David Benjaminf04c2e92016-12-06 13:35:25 -05002669 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2670 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002671 !ssl->s3->send_connection_binding) {
2672 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002673 }
Adam Langley95c29f32014-06-20 12:00:00 -07002674
David Benjamine8d53502015-10-10 14:13:23 -04002675 CBB extensions;
2676 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002677 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2678 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002679 }
Adam Langley95c29f32014-06-20 12:00:00 -07002680
David Benjamin8c880a22016-12-03 02:20:34 -05002681 hs->extensions.sent = 0;
2682 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002683
David Benjamin54091232016-09-05 12:47:25 -04002684 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002685 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002686 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002687 }
2688 }
Adam Langley95c29f32014-06-20 12:00:00 -07002689
David Benjamin65ac9972016-09-02 21:35:25 -04002690 uint16_t grease_ext1 = 0;
2691 if (ssl->ctx->grease_enabled) {
2692 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2693 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2694 if (!CBB_add_u16(&extensions, grease_ext1) ||
2695 !CBB_add_u16(&extensions, 0 /* zero length */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002696 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2697 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002698 }
2699 }
2700
David Benjamin54091232016-09-05 12:47:25 -04002701 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002702 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002703 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002704 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002705 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin81678aa2017-07-12 22:43:42 -04002706 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002707 }
Adam Langley95c29f32014-06-20 12:00:00 -07002708
Adam Langley33ad2b52015-07-20 17:43:53 -07002709 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002710 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002711 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002712 }
Adam Langley75712922014-10-10 16:23:43 -07002713
David Benjamin2bd19172016-11-17 16:47:15 +09002714 if (!custom_ext_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002715 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2716 return 0;
Adam Langley09505632015-07-30 18:10:13 -07002717 }
2718
David Benjamin65ac9972016-09-02 21:35:25 -04002719 if (ssl->ctx->grease_enabled) {
2720 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2721 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2722
2723 /* The two fake extensions must not have the same value. GREASE values are
2724 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2725 * one. */
2726 if (grease_ext1 == grease_ext2) {
2727 grease_ext2 ^= 0x1010;
2728 }
2729
2730 if (!CBB_add_u16(&extensions, grease_ext2) ||
2731 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2732 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002733 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2734 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002735 }
2736 }
2737
David Benjamince079fd2016-08-02 16:22:34 -04002738 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002739 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002740 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002741 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002742 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002743 *
2744 * NB: because this code works out the length of all existing extensions
2745 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002746 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002747 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002748 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002749 * Server 7.0 is intolerant to the last extension being zero-length. See
2750 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002751 if (padding_len >= 4 + 1) {
2752 padding_len -= 4;
2753 } else {
2754 padding_len = 1;
2755 }
Adam Langley95c29f32014-06-20 12:00:00 -07002756
Adam Langley33ad2b52015-07-20 17:43:53 -07002757 uint8_t *padding_bytes;
2758 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2759 !CBB_add_u16(&extensions, padding_len) ||
2760 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002761 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2762 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002763 }
Adam Langley75712922014-10-10 16:23:43 -07002764
David Benjamin17cf2cb2016-12-13 01:07:13 -05002765 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002766 }
2767 }
Adam Langley75712922014-10-10 16:23:43 -07002768
Steven Valdeza833c352016-11-01 13:39:36 -04002769 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002770 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002771 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2772 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -04002773 }
2774
David Benjamina01deee2015-12-08 18:56:31 -05002775 /* Discard empty extensions blocks. */
2776 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002777 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002778 }
2779
David Benjamine8d53502015-10-10 14:13:23 -04002780 return CBB_flush(out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002781}
Adam Langley95c29f32014-06-20 12:00:00 -07002782
David Benjamin8c880a22016-12-03 02:20:34 -05002783int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2784 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002785 CBB extensions;
2786 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002787 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002788 }
2789
David Benjamin8c880a22016-12-03 02:20:34 -05002790 for (unsigned i = 0; i < kNumExtensions; i++) {
2791 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002792 /* Don't send extensions that were not received. */
2793 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002794 }
Adam Langley95c29f32014-06-20 12:00:00 -07002795
David Benjamin8c880a22016-12-03 02:20:34 -05002796 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002797 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002798 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002799 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002800 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002801 }
Adam Langley95c29f32014-06-20 12:00:00 -07002802
David Benjamin2bd19172016-11-17 16:47:15 +09002803 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002804 goto err;
2805 }
2806
Steven Valdez143e8b32016-07-11 13:19:03 -04002807 /* Discard empty extensions blocks before TLS 1.3. */
2808 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2809 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002810 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002811 }
2812
David Benjamin56380462015-10-10 14:59:09 -04002813 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002814
2815err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002816 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002817 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002818}
Adam Langley95c29f32014-06-20 12:00:00 -07002819
David Benjamin731058e2016-12-03 23:15:13 -05002820static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2821 const SSL_CLIENT_HELLO *client_hello,
2822 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002823 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002824 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002825 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002826 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002827 }
2828 }
2829
David Benjamin8c880a22016-12-03 02:20:34 -05002830 hs->extensions.received = 0;
2831 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002832
David Benjamine14ff062016-08-09 16:21:24 -04002833 CBS extensions;
2834 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2835 while (CBS_len(&extensions) != 0) {
2836 uint16_t type;
2837 CBS extension;
2838
2839 /* Decode the next extension. */
2840 if (!CBS_get_u16(&extensions, &type) ||
2841 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002842 *out_alert = SSL_AD_DECODE_ERROR;
2843 return 0;
2844 }
Adam Langley95c29f32014-06-20 12:00:00 -07002845
David Benjamine14ff062016-08-09 16:21:24 -04002846 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2847 * ambiguous. Ignore all but the renegotiation_info extension. */
2848 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2849 continue;
2850 }
Adam Langley95c29f32014-06-20 12:00:00 -07002851
David Benjamine14ff062016-08-09 16:21:24 -04002852 unsigned ext_index;
2853 const struct tls_extension *const ext =
2854 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002855
David Benjamine14ff062016-08-09 16:21:24 -04002856 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002857 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002858 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002859 return 0;
2860 }
David Benjamine14ff062016-08-09 16:21:24 -04002861 continue;
2862 }
2863
David Benjamin8c880a22016-12-03 02:20:34 -05002864 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002865 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002866 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002867 *out_alert = alert;
2868 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002869 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002870 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002871 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002872 }
Adam Langley75712922014-10-10 16:23:43 -07002873
David Benjamin1deb41b2016-08-09 19:36:38 -04002874 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002875 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002876 continue;
2877 }
2878
2879 CBS *contents = NULL, fake_contents;
2880 static const uint8_t kFakeRenegotiateExtension[] = {0};
2881 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2882 ssl_client_cipher_list_contains_cipher(client_hello,
2883 SSL3_CK_SCSV & 0xffff)) {
2884 /* The renegotiation SCSV was received so pretend that we received a
2885 * renegotiation extension. */
2886 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2887 sizeof(kFakeRenegotiateExtension));
2888 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002889 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002890 }
2891
2892 /* Extension wasn't observed so call the callback with a NULL
2893 * parameter. */
2894 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002895 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002896 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002897 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002898 *out_alert = alert;
2899 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002900 }
2901 }
2902
Adam Langleyfcf25832014-12-18 17:42:32 -08002903 return 1;
2904}
Adam Langley95c29f32014-06-20 12:00:00 -07002905
David Benjamin731058e2016-12-03 23:15:13 -05002906int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2907 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002908 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002909 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002910 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002911 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002912 return 0;
2913 }
Adam Langley95c29f32014-06-20 12:00:00 -07002914
David Benjamin8c880a22016-12-03 02:20:34 -05002915 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002916 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002917 return 0;
2918 }
Adam Langley95c29f32014-06-20 12:00:00 -07002919
Adam Langleyfcf25832014-12-18 17:42:32 -08002920 return 1;
2921}
Adam Langley95c29f32014-06-20 12:00:00 -07002922
David Benjamin8c880a22016-12-03 02:20:34 -05002923static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2924 int *out_alert) {
2925 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002926 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2927 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2928 return 1;
2929 }
Adam Langley614c66a2015-06-12 15:26:58 -07002930
Steven Valdez143e8b32016-07-11 13:19:03 -04002931 /* Decode the extensions block and check it is valid. */
2932 CBS extensions;
2933 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2934 !tls1_check_duplicate_extensions(&extensions)) {
2935 *out_alert = SSL_AD_DECODE_ERROR;
2936 return 0;
2937 }
2938
2939 uint32_t received = 0;
2940 while (CBS_len(&extensions) != 0) {
2941 uint16_t type;
2942 CBS extension;
2943
2944 /* Decode the next extension. */
2945 if (!CBS_get_u16(&extensions, &type) ||
2946 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002947 *out_alert = SSL_AD_DECODE_ERROR;
2948 return 0;
2949 }
Adam Langley95c29f32014-06-20 12:00:00 -07002950
Steven Valdez143e8b32016-07-11 13:19:03 -04002951 unsigned ext_index;
2952 const struct tls_extension *const ext =
2953 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002954
Steven Valdez143e8b32016-07-11 13:19:03 -04002955 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002956 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002957 return 0;
2958 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002959 continue;
2960 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002961
David Benjamina3d76d02017-07-14 19:36:07 -04002962 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
2963 "too many bits");
David Benjamin5db7c9b2017-01-24 16:17:03 -05002964
David Benjamin8c880a22016-12-03 02:20:34 -05002965 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002966 type != TLSEXT_TYPE_renegotiate) {
2967 /* If the extension was never sent then it is illegal, except for the
2968 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002969 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2970 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002971 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002972 return 0;
2973 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002974
Steven Valdez143e8b32016-07-11 13:19:03 -04002975 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002976
Steven Valdez143e8b32016-07-11 13:19:03 -04002977 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002978 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002979 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002980 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04002981 *out_alert = alert;
2982 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002983 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002984 }
Adam Langley95c29f32014-06-20 12:00:00 -07002985
David Benjamin54091232016-09-05 12:47:25 -04002986 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002987 if (!(received & (1u << i))) {
2988 /* Extension wasn't observed so call the callback with a NULL
2989 * parameter. */
2990 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002991 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002992 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002993 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002994 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002995 return 0;
2996 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002997 }
2998 }
Adam Langley95c29f32014-06-20 12:00:00 -07002999
Adam Langleyfcf25832014-12-18 17:42:32 -08003000 return 1;
3001}
Adam Langley95c29f32014-06-20 12:00:00 -07003002
David Benjamin8c880a22016-12-03 02:20:34 -05003003static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3004 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003005 int ret = SSL_TLSEXT_ERR_NOACK;
3006 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003007
David Benjamin78f8aab2016-03-10 16:33:58 -05003008 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003009 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003010 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05003011 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
3012 ret = ssl->session_ctx->tlsext_servername_callback(
3013 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003014 }
Adam Langley95c29f32014-06-20 12:00:00 -07003015
Adam Langleyfcf25832014-12-18 17:42:32 -08003016 switch (ret) {
3017 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003018 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003019 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003020
Adam Langleyfcf25832014-12-18 17:42:32 -08003021 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003022 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003023 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003024
Adam Langleyfcf25832014-12-18 17:42:32 -08003025 default:
3026 return 1;
3027 }
3028}
Adam Langleyed8270a2014-09-02 13:52:56 -07003029
David Benjamin8c880a22016-12-03 02:20:34 -05003030int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3031 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08003032 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003033 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003034 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003035 return 0;
3036 }
3037
Adam Langleyfcf25832014-12-18 17:42:32 -08003038 return 1;
3039}
Adam Langley95c29f32014-06-20 12:00:00 -07003040
Adam Langley4c341d02017-03-08 19:33:21 -08003041static enum ssl_ticket_aead_result_t
3042ssl_decrypt_ticket_with_cipher_ctx(SSL *ssl, uint8_t **out, size_t *out_len,
3043 int *out_renew_ticket, const uint8_t *ticket,
3044 size_t ticket_len) {
Adam Langley4c341d02017-03-08 19:33:21 -08003045 const SSL_CTX *const ssl_ctx = ssl->session_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07003046
David Benjamin81678aa2017-07-12 22:43:42 -04003047 bssl::ScopedHMAC_CTX hmac_ctx;
3048 bssl::ScopedEVP_CIPHER_CTX cipher_ctx;
David Benjamine3aa1d92015-06-16 15:34:50 -04003049
David Benjaminadcc3952015-04-26 13:07:57 -04003050 /* Ensure there is room for the key name and the largest IV
3051 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3052 * the maximum IV length should be well under the minimum size for the
3053 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003054 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
David Benjamin81678aa2017-07-12 22:43:42 -04003055 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003056 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003057 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003058
David Benjamine3aa1d92015-06-16 15:34:50 -04003059 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003060 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
David Benjamin81678aa2017-07-12 22:43:42 -04003061 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(),
3062 hmac_ctx.get(), 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003063 if (cb_ret < 0) {
David Benjamin81678aa2017-07-12 22:43:42 -04003064 return ssl_ticket_aead_error;
Adam Langley4c341d02017-03-08 19:33:21 -08003065 } else if (cb_ret == 0) {
David Benjamin81678aa2017-07-12 22:43:42 -04003066 return ssl_ticket_aead_ignore_ticket;
Adam Langley4c341d02017-03-08 19:33:21 -08003067 } else if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003068 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003069 }
3070 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003071 /* Check the key name matches. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003072 if (OPENSSL_memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3073 SSL_TICKET_KEY_NAME_LEN) != 0) {
David Benjamin81678aa2017-07-12 22:43:42 -04003074 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003075 }
David Benjamin81678aa2017-07-12 22:43:42 -04003076 if (!HMAC_Init_ex(hmac_ctx.get(), ssl_ctx->tlsext_tick_hmac_key,
David Benjamine3aa1d92015-06-16 15:34:50 -04003077 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003078 NULL) ||
David Benjamin81678aa2017-07-12 22:43:42 -04003079 !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL,
David Benjamine3aa1d92015-06-16 15:34:50 -04003080 ssl_ctx->tlsext_tick_aes_key, iv)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003081 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08003082 }
3083 }
David Benjamin81678aa2017-07-12 22:43:42 -04003084 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx.get());
Adam Langleyfcf25832014-12-18 17:42:32 -08003085
David Benjamine3aa1d92015-06-16 15:34:50 -04003086 /* Check the MAC at the end of the ticket. */
3087 uint8_t mac[EVP_MAX_MD_SIZE];
David Benjamin81678aa2017-07-12 22:43:42 -04003088 size_t mac_len = HMAC_size(hmac_ctx.get());
David Benjamine3aa1d92015-06-16 15:34:50 -04003089 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003090 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamin81678aa2017-07-12 22:43:42 -04003091 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003092 }
David Benjamin81678aa2017-07-12 22:43:42 -04003093 HMAC_Update(hmac_ctx.get(), ticket, ticket_len - mac_len);
3094 HMAC_Final(hmac_ctx.get(), mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003095 int mac_ok =
3096 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3097#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3098 mac_ok = 1;
3099#endif
3100 if (!mac_ok) {
David Benjamin81678aa2017-07-12 22:43:42 -04003101 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003102 }
3103
David Benjamine3aa1d92015-06-16 15:34:50 -04003104 /* Decrypt the session data. */
3105 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3106 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3107 mac_len;
David Benjamin81678aa2017-07-12 22:43:42 -04003108 bssl::UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len));
3109 if (!plaintext) {
3110 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08003111 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003112 size_t plaintext_len;
3113#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin81678aa2017-07-12 22:43:42 -04003114 OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003115 plaintext_len = ciphertext_len;
3116#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003117 if (ciphertext_len >= INT_MAX) {
David Benjamin81678aa2017-07-12 22:43:42 -04003118 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003119 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003120 int len1, len2;
David Benjamin81678aa2017-07-12 22:43:42 -04003121 if (!EVP_DecryptUpdate(cipher_ctx.get(), plaintext.get(), &len1, ciphertext,
David Benjamine3aa1d92015-06-16 15:34:50 -04003122 (int)ciphertext_len) ||
David Benjamin81678aa2017-07-12 22:43:42 -04003123 !EVP_DecryptFinal_ex(cipher_ctx.get(), plaintext.get() + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003124 ERR_clear_error();
David Benjamin81678aa2017-07-12 22:43:42 -04003125 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003126 }
Adam Langley4c341d02017-03-08 19:33:21 -08003127 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04003128#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003129
David Benjamin81678aa2017-07-12 22:43:42 -04003130 *out = plaintext.release();
Adam Langley4c341d02017-03-08 19:33:21 -08003131 *out_len = plaintext_len;
David Benjamin81678aa2017-07-12 22:43:42 -04003132 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08003133}
3134
3135static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
3136 SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
3137 const uint8_t *ticket, size_t ticket_len) {
David Benjamin81678aa2017-07-12 22:43:42 -04003138 uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len);
Adam Langley4c341d02017-03-08 19:33:21 -08003139 if (plaintext == NULL) {
3140 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3141 return ssl_ticket_aead_error;
3142 }
3143
3144 size_t plaintext_len;
3145 const enum ssl_ticket_aead_result_t result =
3146 ssl->session_ctx->ticket_aead_method->open(
3147 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3148
3149 if (result == ssl_ticket_aead_success) {
3150 *out = plaintext;
3151 plaintext = NULL;
3152 *out_len = plaintext_len;
3153 }
3154
3155 OPENSSL_free(plaintext);
3156 return result;
3157}
3158
3159enum ssl_ticket_aead_result_t ssl_process_ticket(
3160 SSL *ssl, SSL_SESSION **out_session, int *out_renew_ticket,
3161 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3162 size_t session_id_len) {
3163 *out_renew_ticket = 0;
3164 *out_session = NULL;
3165
3166 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3167 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3168 return ssl_ticket_aead_ignore_ticket;
3169 }
3170
3171 uint8_t *plaintext = NULL;
3172 size_t plaintext_len;
3173 enum ssl_ticket_aead_result_t result;
3174 if (ssl->session_ctx->ticket_aead_method != NULL) {
3175 result = ssl_decrypt_ticket_with_method(
3176 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3177 } else {
3178 result = ssl_decrypt_ticket_with_cipher_ctx(
3179 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3180 }
3181
3182 if (result != ssl_ticket_aead_success) {
3183 return result;
3184 }
3185
David Benjamine3aa1d92015-06-16 15:34:50 -04003186 /* Decode the session. */
Adam Langley46db7af2017-02-01 15:49:37 -08003187 SSL_SESSION *session =
3188 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx);
Adam Langley4c341d02017-03-08 19:33:21 -08003189 OPENSSL_free(plaintext);
3190
David Benjamine3aa1d92015-06-16 15:34:50 -04003191 if (session == NULL) {
3192 ERR_clear_error(); /* Don't leave an error on the queue. */
Adam Langley4c341d02017-03-08 19:33:21 -08003193 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003194 }
3195
3196 /* Copy the client's session ID into the new session, to denote the ticket has
3197 * been accepted. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003198 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003199 session->session_id_length = session_id_len;
3200
3201 *out_session = session;
Adam Langley4c341d02017-03-08 19:33:21 -08003202 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003203}
Adam Langley95c29f32014-06-20 12:00:00 -07003204
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003205int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003206 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003207 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003208 return 1;
3209 }
David Benjamincd996942014-07-20 16:23:51 -04003210
David Benjamin0fc37ef2016-08-17 15:29:46 -04003211 OPENSSL_free(hs->peer_sigalgs);
3212 hs->peer_sigalgs = NULL;
3213 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003214
3215 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003216 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003217 return 0;
3218 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003219 num_sigalgs /= 2;
3220
3221 /* supported_signature_algorithms in the certificate request is
3222 * allowed to be empty. */
3223 if (num_sigalgs == 0) {
3224 return 1;
3225 }
3226
Steven Valdez02563852016-06-23 13:33:05 -04003227 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3228 * and we just divided |num_sigalgs| by two. */
David Benjamin81678aa2017-07-12 22:43:42 -04003229 hs->peer_sigalgs = (uint16_t *)OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003230 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003231 return 0;
3232 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003233 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003234
3235 CBS sigalgs;
3236 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003237 for (size_t i = 0; i < num_sigalgs; i++) {
3238 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003239 return 0;
3240 }
3241 }
Adam Langley95c29f32014-06-20 12:00:00 -07003242
Adam Langleyfcf25832014-12-18 17:42:32 -08003243 return 1;
3244}
David Benjaminec2f27d2014-11-13 19:17:25 -05003245
David Benjamina3651382017-04-20 17:49:36 -04003246int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
3247 switch (EVP_PKEY_id(pkey)) {
3248 case EVP_PKEY_RSA:
3249 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3250 return 1;
3251 case EVP_PKEY_EC:
3252 *out = SSL_SIGN_ECDSA_SHA1;
3253 return 1;
3254 default:
3255 return 0;
3256 }
3257}
3258
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003259int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3260 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003261 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003262
Steven Valdezf0451ca2016-06-29 13:16:27 -04003263 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
David Benjamina3651382017-04-20 17:49:36 -04003264 * handshake. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04003265 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamina3651382017-04-20 17:49:36 -04003266 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey)) {
3267 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3268 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003269 }
David Benjamina3651382017-04-20 17:49:36 -04003270 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003271 }
3272
David Benjamin3ef76972016-10-17 17:59:54 -04003273 const uint16_t *sigalgs = cert->sigalgs;
3274 size_t num_sigalgs = cert->num_sigalgs;
3275 if (sigalgs == NULL) {
3276 sigalgs = kSignSignatureAlgorithms;
3277 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003278 }
3279
David Benjamin0fc37ef2016-08-17 15:29:46 -04003280 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3281 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3282 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003283 /* If the client didn't specify any signature_algorithms extension then
3284 * we can assume that it supports SHA1. See
3285 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3286 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3287 SSL_SIGN_ECDSA_SHA1};
3288 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003289 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003290 }
3291
David Benjamin0fc37ef2016-08-17 15:29:46 -04003292 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003293 uint16_t sigalg = sigalgs[i];
3294 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3295 * negotiated. */
3296 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjamina232a712017-03-30 15:51:53 -05003297 !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003298 continue;
3299 }
3300
David Benjamin0fc37ef2016-08-17 15:29:46 -04003301 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003302 if (sigalg == peer_sigalgs[j]) {
3303 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003304 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003305 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003306 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003307 }
Adam Langley95c29f32014-06-20 12:00:00 -07003308
David Benjaminea9a0d52016-07-08 15:52:59 -07003309 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3310 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003311}
Adam Langley95c29f32014-06-20 12:00:00 -07003312
Steven Valdez908ac192017-01-12 13:17:07 -05003313int tls1_verify_channel_id(SSL_HANDSHAKE *hs) {
3314 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003315 uint16_t extension_type;
3316 CBS extension, channel_id;
3317
3318 /* A Channel ID handshake message is structured to contain multiple
3319 * extensions, but the only one that can be present is Channel ID. */
3320 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3321 if (!CBS_get_u16(&channel_id, &extension_type) ||
3322 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3323 CBS_len(&channel_id) != 0 ||
3324 extension_type != TLSEXT_TYPE_channel_id ||
3325 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3326 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3327 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3328 return 0;
3329 }
3330
David Benjamin81678aa2017-07-12 22:43:42 -04003331 bssl::UniquePtr<EC_GROUP> p256(
3332 EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Nick Harper60a85cb2016-09-23 16:25:11 -07003333 if (!p256) {
3334 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3335 return 0;
3336 }
3337
David Benjamin81678aa2017-07-12 22:43:42 -04003338 bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3339 bssl::UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
3340 if (!sig || !x || !y) {
3341 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003342 }
3343
3344 const uint8_t *p = CBS_data(&extension);
David Benjamin81678aa2017-07-12 22:43:42 -04003345 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3346 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3347 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3348 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
3349 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003350 }
3351
David Benjamin81678aa2017-07-12 22:43:42 -04003352 bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
3353 bssl::UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
3354 if (!key || !point ||
3355 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3356 y.get(), nullptr) ||
3357 !EC_KEY_set_group(key.get(), p256.get()) ||
3358 !EC_KEY_set_public_key(key.get(), point.get())) {
3359 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003360 }
3361
3362 uint8_t digest[EVP_MAX_MD_SIZE];
3363 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003364 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003365 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003366 }
3367
David Benjamin81678aa2017-07-12 22:43:42 -04003368 int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
Nick Harper60a85cb2016-09-23 16:25:11 -07003369#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3370 sig_ok = 1;
3371#endif
3372 if (!sig_ok) {
3373 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3374 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3375 ssl->s3->tlsext_channel_id_valid = 0;
David Benjamin81678aa2017-07-12 22:43:42 -04003376 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003377 }
3378
David Benjamin17cf2cb2016-12-13 01:07:13 -05003379 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
David Benjamin81678aa2017-07-12 22:43:42 -04003380 return 1;
Nick Harper60a85cb2016-09-23 16:25:11 -07003381}
3382
Steven Valdez908ac192017-01-12 13:17:07 -05003383int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3384 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003385 uint8_t digest[EVP_MAX_MD_SIZE];
3386 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003387 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003388 return 0;
3389 }
3390
3391 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3392 if (ec_key == NULL) {
3393 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3394 return 0;
3395 }
3396
3397 int ret = 0;
3398 BIGNUM *x = BN_new();
3399 BIGNUM *y = BN_new();
3400 ECDSA_SIG *sig = NULL;
3401 if (x == NULL || y == NULL ||
3402 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3403 EC_KEY_get0_public_key(ec_key),
3404 x, y, NULL)) {
3405 goto err;
3406 }
3407
3408 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3409 if (sig == NULL) {
3410 goto err;
3411 }
3412
3413 CBB child;
3414 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3415 !CBB_add_u16_length_prefixed(cbb, &child) ||
3416 !BN_bn2cbb_padded(&child, 32, x) ||
3417 !BN_bn2cbb_padded(&child, 32, y) ||
3418 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3419 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3420 !CBB_flush(cbb)) {
3421 goto err;
3422 }
3423
3424 ret = 1;
3425
3426err:
3427 BN_free(x);
3428 BN_free(y);
3429 ECDSA_SIG_free(sig);
3430 return ret;
3431}
3432
Steven Valdez908ac192017-01-12 13:17:07 -05003433int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3434 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003435 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3436 uint8_t *msg;
3437 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003438 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003439 ssl_cert_verify_channel_id)) {
3440 return 0;
3441 }
3442 SHA256(msg, msg_len, out);
3443 *out_len = SHA256_DIGEST_LENGTH;
3444 OPENSSL_free(msg);
3445 return 1;
3446 }
3447
Nick Harper95594012016-10-20 14:07:13 -07003448 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003449
Nick Harper95594012016-10-20 14:07:13 -07003450 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003451 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003452 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003453
Steven Valdez87eab492016-06-27 16:34:59 -04003454 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003455 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003456 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003457 if (ssl->session->original_handshake_hash_len == 0) {
3458 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003459 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003460 }
Nick Harper95594012016-10-20 14:07:13 -07003461 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3462 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003463 }
3464
Steven Valdez908ac192017-01-12 13:17:07 -05003465 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3466 size_t hs_hash_len;
3467 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript, hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003468 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003469 }
Steven Valdez908ac192017-01-12 13:17:07 -05003470 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003471 SHA256_Final(out, &ctx);
3472 *out_len = SHA256_DIGEST_LENGTH;
3473 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003474}
Adam Langley1258b6a2014-06-20 12:00:00 -07003475
3476/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin45738dd2017-02-09 20:01:26 -05003477 * hashes in |hs->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003478 * data. */
Steven Valdez908ac192017-01-12 13:17:07 -05003479int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3480 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003481 /* This function should never be called for a resumed session because the
3482 * handshake hashes that we wish to record are for the original, full
3483 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003484 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003485 return -1;
3486 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003487
David Benjamina3d76d02017-07-14 19:36:07 -04003488 static_assert(
David Benjamin45738dd2017-02-09 20:01:26 -05003489 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
David Benjamina3d76d02017-07-14 19:36:07 -04003490 "original_handshake_hash is too small");
Steven Valdez908ac192017-01-12 13:17:07 -05003491
3492 size_t digest_len;
3493 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript,
David Benjamin45738dd2017-02-09 20:01:26 -05003494 hs->new_session->original_handshake_hash,
Steven Valdez908ac192017-01-12 13:17:07 -05003495 &digest_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003496 return -1;
3497 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003498
David Benjamina3d76d02017-07-14 19:36:07 -04003499 static_assert(EVP_MAX_MD_SIZE <= 0xff,
3500 "EVP_MAX_MD_SIZE does not fit in uint8_t");
David Benjamin45738dd2017-02-09 20:01:26 -05003501 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003502
Adam Langleyfcf25832014-12-18 17:42:32 -08003503 return 1;
3504}
Nick Harper60a85cb2016-09-23 16:25:11 -07003505
3506int ssl_do_channel_id_callback(SSL *ssl) {
3507 if (ssl->tlsext_channel_id_private != NULL ||
3508 ssl->ctx->channel_id_cb == NULL) {
3509 return 1;
3510 }
3511
3512 EVP_PKEY *key = NULL;
3513 ssl->ctx->channel_id_cb(ssl, &key);
3514 if (key == NULL) {
3515 /* The caller should try again later. */
3516 return 1;
3517 }
3518
3519 int ret = SSL_set1_tls_channel_id(ssl, key);
3520 EVP_PKEY_free(key);
3521 return ret;
3522}
Adam Langleycfa08c32016-11-17 13:21:27 -08003523
3524int ssl_is_sct_list_valid(const CBS *contents) {
3525 /* Shallow parse the SCT list for sanity. By the RFC
3526 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3527 * of the SCTs may be empty. */
3528 CBS copy = *contents;
3529 CBS sct_list;
3530 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3531 CBS_len(&copy) != 0 ||
3532 CBS_len(&sct_list) == 0) {
3533 return 0;
3534 }
3535
3536 while (CBS_len(&sct_list) > 0) {
3537 CBS sct;
3538 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3539 CBS_len(&sct) == 0) {
3540 return 0;
3541 }
3542 }
3543
3544 return 1;
3545}