blob: 1b143711e3f61ae3d9e14a24e05ff7f68d501f20 [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>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700124#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400127#include "../crypto/internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800128
129
David Benjamin8c880a22016-12-03 02:20:34 -0500130static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
Adam Langley95c29f32014-06-20 12:00:00 -0700131
Adam Langleyfcf25832014-12-18 17:42:32 -0800132static int compare_uint16_t(const void *p1, const void *p2) {
133 uint16_t u1 = *((const uint16_t *)p1);
134 uint16_t u2 = *((const uint16_t *)p2);
135 if (u1 < u2) {
136 return -1;
137 } else if (u1 > u2) {
138 return 1;
139 } else {
140 return 0;
141 }
142}
David Benjamin35a7a442014-07-05 00:23:20 -0400143
Adam Langleyfcf25832014-12-18 17:42:32 -0800144/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
145 * more than one extension of the same type in a ClientHello or ServerHello.
146 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400147 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800148static int tls1_check_duplicate_extensions(const CBS *cbs) {
149 CBS extensions = *cbs;
150 size_t num_extensions = 0, i = 0;
151 uint16_t *extension_types = NULL;
152 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400153
Adam Langleyfcf25832014-12-18 17:42:32 -0800154 /* First pass: count the extensions. */
155 while (CBS_len(&extensions) > 0) {
156 uint16_t type;
157 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400158
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 if (!CBS_get_u16(&extensions, &type) ||
160 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
161 goto done;
162 }
David Benjamin35a7a442014-07-05 00:23:20 -0400163
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 num_extensions++;
165 }
David Benjamin35a7a442014-07-05 00:23:20 -0400166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 if (num_extensions == 0) {
168 return 1;
169 }
David Benjamin9a373592014-07-25 04:27:53 -0400170
Brian Smith5ba06892016-02-07 09:36:04 -1000171 extension_types = 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) {
Steven Valdezce902a92016-05-17 11:47:53 -0400365 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800366
Steven Valdezce902a92016-05-17 11:47:53 -0400367 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
368 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 return 0;
370 }
371
David Benjamin54091232016-09-05 12:47:25 -0400372 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400373 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
374 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 return 0;
376 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 }
378
Steven Valdezce902a92016-05-17 11:47:53 -0400379 OPENSSL_free(*out_group_ids);
380 *out_group_ids = group_ids;
381 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800382
383 return 1;
384}
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100386int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
387 const char *curves) {
388 uint16_t *group_ids = NULL;
389 size_t ncurves = 0;
390
391 const char *col;
392 const char *ptr = curves;
393
394 do {
395 col = strchr(ptr, ':');
396
397 uint16_t group_id;
398 if (!ssl_name_to_group_id(&group_id, ptr,
399 col ? (size_t)(col - ptr) : strlen(ptr))) {
400 goto err;
401 }
402
403 uint16_t *new_group_ids = OPENSSL_realloc(group_ids,
404 (ncurves + 1) * sizeof(uint16_t));
405 if (new_group_ids == NULL) {
406 goto err;
407 }
408 group_ids = new_group_ids;
409
410 group_ids[ncurves] = group_id;
411 ncurves++;
412
413 if (col) {
414 ptr = col + 1;
415 }
416 } while (col);
417
418 OPENSSL_free(*out_group_ids);
419 *out_group_ids = group_ids;
420 *out_group_ids_len = ncurves;
421
422 return 1;
423
424err:
425 OPENSSL_free(group_ids);
426 return 0;
427}
428
Steven Valdezce902a92016-05-17 11:47:53 -0400429int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
430 const uint16_t *groups;
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400431 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -0400432 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400433 for (size_t i = 0; i < groups_len; i++) {
434 if (groups[i] == group_id) {
435 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800436 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 }
David Benjamin033e5f42014-11-13 18:47:41 -0500438
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400439 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800440}
David Benjamin033e5f42014-11-13 18:47:41 -0500441
David Benjamin3ef76972016-10-17 17:59:54 -0400442/* kVerifySignatureAlgorithms is the default list of accepted signature
David Benjamin3a322f52016-10-26 12:45:35 -0400443 * algorithms for verifying.
444 *
445 * For now, RSA-PSS signature algorithms are not enabled on Android's system
446 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
447 * restore them. */
David Benjamin3ef76972016-10-17 17:59:54 -0400448static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjamin69522112017-03-28 15:38:29 -0500449 /* List our preferred algorithms first. */
450 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400451 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400452#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400453 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400454#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400455 SSL_SIGN_RSA_PKCS1_SHA256,
456
457 /* Larger hashes are acceptable. */
458 SSL_SIGN_ECDSA_SECP384R1_SHA384,
459#if !defined(BORINGSSL_ANDROID_SYSTEM)
460 SSL_SIGN_RSA_PSS_SHA384,
461#endif
462 SSL_SIGN_RSA_PKCS1_SHA384,
463
David Benjamin76bb1412016-09-08 16:03:49 -0400464 /* TODO(davidben): Remove this. */
David Benjamin3ef76972016-10-17 17:59:54 -0400465#if defined(BORINGSSL_ANDROID_SYSTEM)
466 SSL_SIGN_ECDSA_SECP521R1_SHA512,
467#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400468#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400469 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400470#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400471 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400472
David Benjamin3a322f52016-10-26 12:45:35 -0400473 /* For now, SHA-1 is still accepted but least preferable. */
474 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400475
David Benjamin3a322f52016-10-26 12:45:35 -0400476};
477
478/* kSignSignatureAlgorithms is the default list of supported signature
479 * algorithms for signing.
480 *
481 * For now, RSA-PSS signature algorithms are not enabled on Android's system
482 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
483 * restore them. */
484static const uint16_t kSignSignatureAlgorithms[] = {
David Benjamin69522112017-03-28 15:38:29 -0500485 /* List our preferred algorithms first. */
486 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400487 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400488#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400489 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400490#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400491 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400492
David Benjamin3a322f52016-10-26 12:45:35 -0400493 /* If needed, sign larger hashes.
494 *
495 * TODO(davidben): Determine which of these may be pruned. */
496 SSL_SIGN_ECDSA_SECP384R1_SHA384,
497#if !defined(BORINGSSL_ANDROID_SYSTEM)
498 SSL_SIGN_RSA_PSS_SHA384,
499#endif
500 SSL_SIGN_RSA_PKCS1_SHA384,
501
502 SSL_SIGN_ECDSA_SECP521R1_SHA512,
503#if !defined(BORINGSSL_ANDROID_SYSTEM)
504 SSL_SIGN_RSA_PSS_SHA512,
505#endif
506 SSL_SIGN_RSA_PKCS1_SHA512,
507
508 /* If the peer supports nothing else, sign with SHA-1. */
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400509 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400510 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400511};
512
David Benjamin69522112017-03-28 15:38:29 -0500513void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
514 ctx->ed25519_enabled = !!enabled;
515}
516
517int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) {
David Benjamin71c21b42017-04-14 17:05:40 -0400518 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
519 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
520 if (ssl->ctx->num_verify_sigalgs != 0) {
521 sigalgs = ssl->ctx->verify_sigalgs;
522 num_sigalgs = ssl->ctx->num_verify_sigalgs;
523 }
524
525 for (size_t i = 0; i < num_sigalgs; i++) {
526 if (sigalgs == kVerifySignatureAlgorithms &&
527 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500528 !ssl->ctx->ed25519_enabled) {
529 continue;
530 }
David Benjamin71c21b42017-04-14 17:05:40 -0400531 if (!CBB_add_u16(out, sigalgs[i])) {
David Benjamin69522112017-03-28 15:38:29 -0500532 return 0;
533 }
534 }
535
536 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800537}
Adam Langley95c29f32014-06-20 12:00:00 -0700538
David Benjamin8d606e32017-06-15 22:43:04 -0400539int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg) {
David Benjamin71c21b42017-04-14 17:05:40 -0400540 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
541 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
542 if (ssl->ctx->num_verify_sigalgs != 0) {
543 sigalgs = ssl->ctx->verify_sigalgs;
544 num_sigalgs = ssl->ctx->num_verify_sigalgs;
545 }
546
547 for (size_t i = 0; i < num_sigalgs; i++) {
548 if (sigalgs == kVerifySignatureAlgorithms &&
549 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500550 !ssl->ctx->ed25519_enabled) {
551 continue;
552 }
David Benjamin71c21b42017-04-14 17:05:40 -0400553 if (sigalg == sigalgs[i]) {
David Benjamin3ef76972016-10-17 17:59:54 -0400554 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800555 }
556 }
557
David Benjamin3ef76972016-10-17 17:59:54 -0400558 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
559 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
560 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800561}
562
Adam Langley614c66a2015-06-12 15:26:58 -0700563/* tls_extension represents a TLS extension that is handled internally. The
564 * |init| function is called for each handshake, before any other functions of
565 * the extension. Then the add and parse callbacks are called as needed.
566 *
567 * The parse callbacks receive a |CBS| that contains the contents of the
568 * extension (i.e. not including the type and length bytes). If an extension is
569 * not received then the parse callbacks will be called with a NULL CBS so that
570 * they can do any processing needed to handle the absence of an extension.
571 *
572 * The add callbacks receive a |CBB| to which the extension can be appended but
573 * the function is responsible for appending the type and length bytes too.
574 *
575 * All callbacks return one for success and zero for error. If a parse function
576 * returns zero then a fatal alert with value |*out_alert| will be sent. If
577 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
578struct tls_extension {
579 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500580 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700581
David Benjamin8c880a22016-12-03 02:20:34 -0500582 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
583 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
584 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700585
David Benjamin8c880a22016-12-03 02:20:34 -0500586 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
587 CBS *contents);
588 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700589};
590
David Benjamin8c880a22016-12-03 02:20:34 -0500591static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
592 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400593 if (contents != NULL) {
594 /* Servers MUST NOT send this extension. */
595 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
596 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
597 return 0;
598 }
599
600 return 1;
601}
602
David Benjamin8c880a22016-12-03 02:20:34 -0500603static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
604 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400605 /* This extension from the client is handled elsewhere. */
606 return 1;
607}
608
David Benjamin8c880a22016-12-03 02:20:34 -0500609static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400610 return 1;
611}
Adam Langley614c66a2015-06-12 15:26:58 -0700612
613/* Server name indication (SNI).
614 *
615 * https://tools.ietf.org/html/rfc6066#section-3. */
616
David Benjamin8c880a22016-12-03 02:20:34 -0500617static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
618 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700619 if (ssl->tlsext_hostname == NULL) {
620 return 1;
621 }
622
623 CBB contents, server_name_list, name;
624 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
625 !CBB_add_u16_length_prefixed(out, &contents) ||
626 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
627 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
628 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
629 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
630 strlen(ssl->tlsext_hostname)) ||
631 !CBB_flush(out)) {
632 return 0;
633 }
634
635 return 1;
636}
637
David Benjamin8c880a22016-12-03 02:20:34 -0500638static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500639 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500640 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700641 if (contents == NULL) {
642 return 1;
643 }
644
645 if (CBS_len(contents) != 0) {
646 return 0;
647 }
648
649 assert(ssl->tlsext_hostname != NULL);
650
Steven Valdez87eab492016-06-27 16:34:59 -0400651 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500652 OPENSSL_free(hs->new_session->tlsext_hostname);
653 hs->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
654 if (!hs->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700655 *out_alert = SSL_AD_INTERNAL_ERROR;
656 return 0;
657 }
658 }
659
660 return 1;
661}
662
David Benjamin8c880a22016-12-03 02:20:34 -0500663static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500664 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700665 if (contents == NULL) {
666 return 1;
667 }
668
David Benjamin9b611e22016-03-03 08:48:30 -0500669 CBS server_name_list, host_name;
670 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700671 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500672 !CBS_get_u8(&server_name_list, &name_type) ||
673 /* Although the server_name extension was intended to be extensible to
674 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
675 * different name types will cause an error. Further, RFC 4366 originally
676 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
677 * adding new name types is no longer feasible.
678 *
679 * Act as if the extensibility does not exist to simplify parsing. */
680 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
681 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700682 CBS_len(contents) != 0) {
683 return 0;
684 }
685
David Benjamin9b611e22016-03-03 08:48:30 -0500686 if (name_type != TLSEXT_NAMETYPE_host_name ||
687 CBS_len(&host_name) == 0 ||
688 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
689 CBS_contains_zero_byte(&host_name)) {
690 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
691 return 0;
692 }
Adam Langley614c66a2015-06-12 15:26:58 -0700693
David Benjamin4eb95cc2016-11-16 17:08:23 +0900694 /* Copy the hostname as a string. */
David Benjamin8c880a22016-12-03 02:20:34 -0500695 if (!CBS_strdup(&host_name, &hs->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900696 *out_alert = SSL_AD_INTERNAL_ERROR;
697 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700698 }
699
David Benjamin8c880a22016-12-03 02:20:34 -0500700 hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700701 return 1;
702}
703
David Benjamin8c880a22016-12-03 02:20:34 -0500704static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
705 if (hs->ssl->s3->session_reused ||
706 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700707 return 1;
708 }
709
710 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
711 !CBB_add_u16(out, 0 /* length */)) {
712 return 0;
713 }
714
715 return 1;
716}
717
718
Adam Langley5021b222015-06-12 18:27:58 -0700719/* Renegotiation indication.
720 *
721 * https://tools.ietf.org/html/rfc5746 */
722
David Benjamin8c880a22016-12-03 02:20:34 -0500723static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
724 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400725 /* Renegotiation indication is not necessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -0400726 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400727 return 1;
728 }
729
David Benjamin52bf6902016-10-08 12:05:03 -0400730 assert(ssl->s3->initial_handshake_complete ==
731 (ssl->s3->previous_client_finished_len != 0));
732
Adam Langley5021b222015-06-12 18:27:58 -0700733 CBB contents, prev_finished;
734 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
735 !CBB_add_u16_length_prefixed(out, &contents) ||
736 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
737 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
738 ssl->s3->previous_client_finished_len) ||
739 !CBB_flush(out)) {
740 return 0;
741 }
742
743 return 1;
744}
745
David Benjamin8c880a22016-12-03 02:20:34 -0500746static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700747 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500748 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400749 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez246eeee2017-03-26 12:49:17 -0500750 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400751 return 0;
752 }
753
David Benjamin3e052de2015-11-25 20:10:31 -0500754 /* Servers may not switch between omitting the extension and supporting it.
755 * See RFC 5746, sections 3.5 and 4.2. */
756 if (ssl->s3->initial_handshake_complete &&
757 (contents != NULL) != ssl->s3->send_connection_binding) {
758 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
759 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
760 return 0;
761 }
762
Adam Langley5021b222015-06-12 18:27:58 -0700763 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500764 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700765 * RI even on initial ServerHello because the client doesn't see any
766 * renegotiation during an attack. However this would mean we could not
767 * connect to any server which doesn't support RI.
768 *
David Benjamine9cddb82015-11-23 14:36:40 -0500769 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
770 * practical terms every client sets it so it's just assumed here. */
771 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700772 }
773
774 const size_t expected_len = ssl->s3->previous_client_finished_len +
775 ssl->s3->previous_server_finished_len;
776
777 /* Check for logic errors */
778 assert(!expected_len || ssl->s3->previous_client_finished_len);
779 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400780 assert(ssl->s3->initial_handshake_complete ==
781 (ssl->s3->previous_client_finished_len != 0));
782 assert(ssl->s3->initial_handshake_complete ==
783 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700784
785 /* Parse out the extension contents. */
786 CBS renegotiated_connection;
787 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
788 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400789 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700790 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
791 return 0;
792 }
793
794 /* Check that the extension matches. */
795 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400796 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700797 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
798 return 0;
799 }
800
801 const uint8_t *d = CBS_data(&renegotiated_connection);
802 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
803 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400804 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700805 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
806 return 0;
807 }
808 d += ssl->s3->previous_client_finished_len;
809
810 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
811 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400812 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700813 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
814 return 0;
815 }
816 ssl->s3->send_connection_binding = 1;
817
818 return 1;
819}
820
David Benjamin8c880a22016-12-03 02:20:34 -0500821static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700822 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500823 SSL *const ssl = hs->ssl;
Adam Langley5021b222015-06-12 18:27:58 -0700824 /* Renegotiation isn't supported as a server so this function should never be
825 * called after the initial handshake. */
826 assert(!ssl->s3->initial_handshake_complete);
827
Steven Valdez143e8b32016-07-11 13:19:03 -0400828 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
829 return 1;
830 }
831
Adam Langley5021b222015-06-12 18:27:58 -0700832 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400833 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700834 }
835
836 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700837 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
838 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400839 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700840 return 0;
841 }
842
David Benjamin52bf6902016-10-08 12:05:03 -0400843 /* Check that the extension matches. We do not support renegotiation as a
844 * server, so this must be empty. */
845 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400846 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700847 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
848 return 0;
849 }
850
851 ssl->s3->send_connection_binding = 1;
852
853 return 1;
854}
855
David Benjamin8c880a22016-12-03 02:20:34 -0500856static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
857 SSL *const ssl = hs->ssl;
David Benjamin52bf6902016-10-08 12:05:03 -0400858 /* Renegotiation isn't supported as a server so this function should never be
859 * called after the initial handshake. */
860 assert(!ssl->s3->initial_handshake_complete);
861
Steven Valdez143e8b32016-07-11 13:19:03 -0400862 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
863 return 1;
864 }
865
Adam Langley5021b222015-06-12 18:27:58 -0700866 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400867 !CBB_add_u16(out, 1 /* length */) ||
868 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700869 return 0;
870 }
871
872 return 1;
873}
874
Adam Langley0a056712015-07-01 15:03:33 -0700875
876/* Extended Master Secret.
877 *
David Benjamin43946d42016-02-01 08:42:19 -0500878 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700879
David Benjamin8c880a22016-12-03 02:20:34 -0500880static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400881 /* Extended master secret is not necessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -0400882 if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700883 return 1;
884 }
885
886 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
887 !CBB_add_u16(out, 0 /* length */)) {
888 return 0;
889 }
890
891 return 1;
892}
893
David Benjamin8c880a22016-12-03 02:20:34 -0500894static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700895 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500896 SSL *const ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500897
898 if (contents != NULL) {
899 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
900 ssl->version == SSL3_VERSION ||
901 CBS_len(contents) != 0) {
David Benjamin163c9562016-08-29 23:14:17 -0400902 return 0;
903 }
904
David Benjaminfc02b592017-02-17 16:26:01 -0500905 hs->extended_master_secret = 1;
David Benjamin163c9562016-08-29 23:14:17 -0400906 }
907
David Benjaminfc02b592017-02-17 16:26:01 -0500908 /* Whether EMS is negotiated may not change on renegotiation. */
909 if (ssl->s3->established_session != NULL &&
910 hs->extended_master_secret !=
911 ssl->s3->established_session->extended_master_secret) {
912 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
913 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400914 return 0;
915 }
916
Adam Langley0a056712015-07-01 15:03:33 -0700917 return 1;
918}
919
David Benjamin8c880a22016-12-03 02:20:34 -0500920static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500921 CBS *contents) {
David Benjaminfc02b592017-02-17 16:26:01 -0500922 uint16_t version = ssl3_protocol_version(hs->ssl);
David Benjamin8c880a22016-12-03 02:20:34 -0500923 if (version >= TLS1_3_VERSION ||
924 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400925 return 1;
926 }
927
928 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700929 return 1;
930 }
931
932 if (CBS_len(contents) != 0) {
933 return 0;
934 }
935
David Benjaminfc02b592017-02-17 16:26:01 -0500936 hs->extended_master_secret = 1;
Adam Langley0a056712015-07-01 15:03:33 -0700937 return 1;
938}
939
David Benjamin8c880a22016-12-03 02:20:34 -0500940static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -0500941 if (!hs->extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700942 return 1;
943 }
944
945 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
946 !CBB_add_u16(out, 0 /* length */)) {
947 return 0;
948 }
949
950 return 1;
951}
952
Adam Langley9b05bc52015-07-01 15:25:33 -0700953
954/* Session tickets.
955 *
956 * https://tools.ietf.org/html/rfc5077 */
957
David Benjamin8c880a22016-12-03 02:20:34 -0500958static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
959 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400960 /* TLS 1.3 uses a different ticket extension. */
David Benjamin68161cb2017-06-20 14:49:43 -0400961 if (hs->min_version >= TLS1_3_VERSION ||
David Benjamin7c7d8312016-08-20 13:39:03 -0400962 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700963 return 1;
964 }
965
966 const uint8_t *ticket_data = NULL;
967 int ticket_len = 0;
968
969 /* Renegotiation does not participate in session resumption. However, still
970 * advertise the extension to avoid potentially breaking servers which carry
971 * over the state from the previous handshake, such as OpenSSL servers
972 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -0400973 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -0700974 if (!ssl->s3->initial_handshake_complete &&
975 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400976 ssl->session->tlsext_tick != NULL &&
977 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -0400978 ssl->method->version_from_wire(&session_version,
979 ssl->session->ssl_version) &&
980 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700981 ticket_data = ssl->session->tlsext_tick;
982 ticket_len = ssl->session->tlsext_ticklen;
983 }
984
985 CBB ticket;
986 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
987 !CBB_add_u16_length_prefixed(out, &ticket) ||
988 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
989 !CBB_flush(out)) {
990 return 0;
991 }
992
993 return 1;
994}
995
David Benjamin8c880a22016-12-03 02:20:34 -0500996static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -0700997 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500998 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -0700999 if (contents == NULL) {
1000 return 1;
1001 }
1002
Steven Valdez143e8b32016-07-11 13:19:03 -04001003 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1004 return 0;
1005 }
1006
Adam Langley9b05bc52015-07-01 15:25:33 -07001007 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1008 * this function should never be called, even if the server tries to send the
1009 * extension. */
1010 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1011
1012 if (CBS_len(contents) != 0) {
1013 return 0;
1014 }
1015
David Benjamin8c880a22016-12-03 02:20:34 -05001016 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001017 return 1;
1018}
1019
David Benjamin8c880a22016-12-03 02:20:34 -05001020static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1021 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001022 return 1;
1023 }
1024
David Benjamin78476f62016-11-12 11:20:55 +09001025 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin8c880a22016-12-03 02:20:34 -05001026 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001027
1028 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1029 !CBB_add_u16(out, 0 /* length */)) {
1030 return 0;
1031 }
1032
1033 return 1;
1034}
1035
1036
Adam Langley2e857bd2015-07-01 16:09:19 -07001037/* Signature Algorithms.
1038 *
1039 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1040
David Benjamin8c880a22016-12-03 02:20:34 -05001041static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1042 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001043 if (hs->max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001044 return 1;
1045 }
1046
David Benjamin0fc37ef2016-08-17 15:29:46 -04001047 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001048 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1049 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin69522112017-03-28 15:38:29 -05001050 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
1051 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
1052 !CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001053 return 0;
1054 }
1055
1056 return 1;
1057}
1058
David Benjamin8c880a22016-12-03 02:20:34 -05001059static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001060 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001061 OPENSSL_free(hs->peer_sigalgs);
1062 hs->peer_sigalgs = NULL;
1063 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001064
Adam Langley2e857bd2015-07-01 16:09:19 -07001065 if (contents == NULL) {
1066 return 1;
1067 }
1068
1069 CBS supported_signature_algorithms;
1070 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001071 CBS_len(contents) != 0 ||
1072 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001073 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001074 return 0;
1075 }
1076
1077 return 1;
1078}
1079
Adam Langley2e857bd2015-07-01 16:09:19 -07001080
Adam Langleybb0bd042015-07-01 16:21:03 -07001081/* OCSP Stapling.
1082 *
1083 * https://tools.ietf.org/html/rfc6066#section-8 */
1084
David Benjamin8c880a22016-12-03 02:20:34 -05001085static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1086 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001087 if (!ssl->ocsp_stapling_enabled) {
1088 return 1;
1089 }
1090
1091 CBB contents;
1092 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1093 !CBB_add_u16_length_prefixed(out, &contents) ||
1094 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1095 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1096 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1097 !CBB_flush(out)) {
1098 return 0;
1099 }
1100
1101 return 1;
1102}
1103
David Benjamin8c880a22016-12-03 02:20:34 -05001104static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001105 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001106 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001107 if (contents == NULL) {
1108 return 1;
1109 }
1110
Steven Valdeza833c352016-11-01 13:39:36 -04001111 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1112 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001113 return 0;
1114 }
1115
Steven Valdeza833c352016-11-01 13:39:36 -04001116 /* OCSP stapling is forbidden on non-certificate ciphers. */
1117 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001118 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001119 return 0;
1120 }
1121
Steven Valdeza833c352016-11-01 13:39:36 -04001122 /* Note this does not check for resumption in TLS 1.2. Sending
1123 * status_request here does not make sense, but OpenSSL does so and the
1124 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001125
David Benjamin8c880a22016-12-03 02:20:34 -05001126 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001127 return 1;
1128}
1129
David Benjamin8c880a22016-12-03 02:20:34 -05001130static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001131 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001132 if (contents == NULL) {
1133 return 1;
1134 }
1135
1136 uint8_t status_type;
1137 if (!CBS_get_u8(contents, &status_type)) {
1138 return 0;
1139 }
1140
1141 /* We cannot decide whether OCSP stapling will occur yet because the correct
1142 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001143 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001144
Adam Langleybb0bd042015-07-01 16:21:03 -07001145 return 1;
1146}
1147
David Benjamin8c880a22016-12-03 02:20:34 -05001148static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1149 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001150 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001151 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001152 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001153 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001154 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001155 return 1;
1156 }
1157
David Benjamin8c880a22016-12-03 02:20:34 -05001158 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001159
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001160 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001161 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001162}
1163
1164
Adam Langley97dfcbf2015-07-01 18:35:20 -07001165/* Next protocol negotiation.
1166 *
1167 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1168
David Benjamin8c880a22016-12-03 02:20:34 -05001169static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1170 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001171 if (ssl->s3->initial_handshake_complete ||
1172 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001173 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001174 return 1;
1175 }
1176
1177 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1178 !CBB_add_u16(out, 0 /* length */)) {
1179 return 0;
1180 }
1181
1182 return 1;
1183}
1184
David Benjamin8c880a22016-12-03 02:20:34 -05001185static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001186 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001187 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001188 if (contents == NULL) {
1189 return 1;
1190 }
1191
Steven Valdez143e8b32016-07-11 13:19:03 -04001192 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1193 return 0;
1194 }
1195
Adam Langley97dfcbf2015-07-01 18:35:20 -07001196 /* If any of these are false then we should never have sent the NPN
1197 * extension in the ClientHello and thus this function should never have been
1198 * called. */
1199 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001200 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001201 assert(ssl->ctx->next_proto_select_cb != NULL);
1202
David Benjamin76c2efc2015-08-31 14:24:29 -04001203 if (ssl->s3->alpn_selected != NULL) {
1204 /* NPN and ALPN may not be negotiated in the same connection. */
1205 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1206 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1207 return 0;
1208 }
1209
Adam Langley97dfcbf2015-07-01 18:35:20 -07001210 const uint8_t *const orig_contents = CBS_data(contents);
1211 const size_t orig_len = CBS_len(contents);
1212
1213 while (CBS_len(contents) != 0) {
1214 CBS proto;
1215 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1216 CBS_len(&proto) == 0) {
1217 return 0;
1218 }
1219 }
1220
1221 uint8_t *selected;
1222 uint8_t selected_len;
1223 if (ssl->ctx->next_proto_select_cb(
1224 ssl, &selected, &selected_len, orig_contents, orig_len,
1225 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1226 *out_alert = SSL_AD_INTERNAL_ERROR;
1227 return 0;
1228 }
1229
David Benjamin79978df2015-12-25 15:56:49 -05001230 OPENSSL_free(ssl->s3->next_proto_negotiated);
1231 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1232 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001233 *out_alert = SSL_AD_INTERNAL_ERROR;
1234 return 0;
1235 }
1236
David Benjamin79978df2015-12-25 15:56:49 -05001237 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001238 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001239
1240 return 1;
1241}
1242
David Benjamin8c880a22016-12-03 02:20:34 -05001243static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001244 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001245 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001246 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1247 return 1;
1248 }
1249
Adam Langley97dfcbf2015-07-01 18:35:20 -07001250 if (contents != NULL && CBS_len(contents) != 0) {
1251 return 0;
1252 }
1253
1254 if (contents == NULL ||
1255 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001256 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001257 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001258 return 1;
1259 }
1260
David Benjamin8c880a22016-12-03 02:20:34 -05001261 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001262 return 1;
1263}
1264
David Benjamin8c880a22016-12-03 02:20:34 -05001265static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1266 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001267 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1268 * parsed. */
David Benjamin8c880a22016-12-03 02:20:34 -05001269 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001270 return 1;
1271 }
1272
1273 const uint8_t *npa;
1274 unsigned npa_len;
1275
1276 if (ssl->ctx->next_protos_advertised_cb(
1277 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1278 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001279 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001280 return 1;
1281 }
1282
1283 CBB contents;
1284 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1285 !CBB_add_u16_length_prefixed(out, &contents) ||
1286 !CBB_add_bytes(&contents, npa, npa_len) ||
1287 !CBB_flush(out)) {
1288 return 0;
1289 }
1290
1291 return 1;
1292}
1293
1294
Adam Langleyab8d87d2015-07-10 12:21:39 -07001295/* Signed certificate timestamps.
1296 *
1297 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1298
David Benjamin8c880a22016-12-03 02:20:34 -05001299static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1300 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001301 if (!ssl->signed_cert_timestamps_enabled) {
1302 return 1;
1303 }
1304
1305 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1306 !CBB_add_u16(out, 0 /* length */)) {
1307 return 0;
1308 }
1309
1310 return 1;
1311}
1312
David Benjamin8c880a22016-12-03 02:20:34 -05001313static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001314 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001315 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001316 if (contents == NULL) {
1317 return 1;
1318 }
1319
Steven Valdeza833c352016-11-01 13:39:36 -04001320 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1321 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001322 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001323 return 0;
1324 }
1325
Adam Langleyab8d87d2015-07-10 12:21:39 -07001326 /* If this is false then we should never have sent the SCT extension in the
1327 * ClientHello and thus this function should never have been called. */
1328 assert(ssl->signed_cert_timestamps_enabled);
1329
Adam Langleycfa08c32016-11-17 13:21:27 -08001330 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001331 *out_alert = SSL_AD_DECODE_ERROR;
1332 return 0;
1333 }
1334
David Benjamindaa88502016-10-04 16:32:16 -04001335 /* Session resumption uses the original session information. The extension
1336 * should not be sent on resumption, but RFC 6962 did not make it a
1337 * requirement, so tolerate this.
1338 *
1339 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001340 if (!ssl->s3->session_reused &&
David Benjamin45738dd2017-02-09 20:01:26 -05001341 !CBS_stow(contents, &hs->new_session->tlsext_signed_cert_timestamp_list,
1342 &hs->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001343 *out_alert = SSL_AD_INTERNAL_ERROR;
1344 return 0;
1345 }
1346
1347 return 1;
1348}
1349
David Benjamin8c880a22016-12-03 02:20:34 -05001350static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001351 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001352 if (contents == NULL) {
1353 return 1;
1354 }
1355
1356 if (CBS_len(contents) != 0) {
1357 return 0;
1358 }
1359
David Benjamin8c880a22016-12-03 02:20:34 -05001360 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001361 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001362}
1363
David Benjamin8c880a22016-12-03 02:20:34 -05001364static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1365 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001366 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001367 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1368 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001369 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001370 return 1;
1371 }
1372
1373 CBB contents;
1374 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1375 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001376 CBB_add_bytes(
1377 &contents,
1378 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1379 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001380 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001381}
1382
1383
Adam Langleyf18e4532015-07-10 13:39:53 -07001384/* Application-level Protocol Negotiation.
1385 *
1386 * https://tools.ietf.org/html/rfc7301 */
1387
David Benjamin8c880a22016-12-03 02:20:34 -05001388static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1389 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001390 if (ssl->alpn_client_proto_list == NULL ||
1391 ssl->s3->initial_handshake_complete) {
1392 return 1;
1393 }
1394
1395 CBB contents, proto_list;
1396 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1397 !CBB_add_u16_length_prefixed(out, &contents) ||
1398 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1399 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1400 ssl->alpn_client_proto_list_len) ||
1401 !CBB_flush(out)) {
1402 return 0;
1403 }
1404
1405 return 1;
1406}
1407
David Benjamin8c880a22016-12-03 02:20:34 -05001408static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001409 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001410 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001411 if (contents == NULL) {
1412 return 1;
1413 }
1414
1415 assert(!ssl->s3->initial_handshake_complete);
1416 assert(ssl->alpn_client_proto_list != NULL);
1417
David Benjamin8c880a22016-12-03 02:20:34 -05001418 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001419 /* NPN and ALPN may not be negotiated in the same connection. */
1420 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1421 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1422 return 0;
1423 }
1424
Adam Langleyf18e4532015-07-10 13:39:53 -07001425 /* The extension data consists of a ProtocolNameList which must have
1426 * exactly one ProtocolName. Each of these is length-prefixed. */
1427 CBS protocol_name_list, protocol_name;
1428 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1429 CBS_len(contents) != 0 ||
1430 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1431 /* Empty protocol names are forbidden. */
1432 CBS_len(&protocol_name) == 0 ||
1433 CBS_len(&protocol_name_list) != 0) {
1434 return 0;
1435 }
1436
David Benjaminc8ff30c2017-04-04 13:52:36 -04001437 if (!ssl->ctx->allow_unknown_alpn_protos) {
1438 /* Check that the protocol name is one of the ones we advertised. */
1439 int protocol_ok = 0;
1440 CBS client_protocol_name_list, client_protocol_name;
1441 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1442 ssl->alpn_client_proto_list_len);
1443 while (CBS_len(&client_protocol_name_list) > 0) {
1444 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1445 &client_protocol_name)) {
1446 *out_alert = SSL_AD_INTERNAL_ERROR;
1447 return 0;
1448 }
1449
1450 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1451 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1452 CBS_data(&protocol_name),
1453 CBS_len(&protocol_name)) == 0) {
1454 protocol_ok = 1;
1455 break;
1456 }
1457 }
1458
1459 if (!protocol_ok) {
1460 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1461 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3e51757d2016-08-11 11:52:23 -04001462 return 0;
1463 }
David Benjamin3e51757d2016-08-11 11:52:23 -04001464 }
1465
Adam Langleyf18e4532015-07-10 13:39:53 -07001466 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1467 &ssl->s3->alpn_selected_len)) {
1468 *out_alert = SSL_AD_INTERNAL_ERROR;
1469 return 0;
1470 }
1471
1472 return 1;
1473}
1474
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001475int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001476 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001477 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001478 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001479 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001480 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001481 client_hello, &contents,
1482 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1483 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001484 return 1;
1485 }
1486
1487 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001488 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001489
1490 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001491 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1492 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001493 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001494 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1495 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001496 return 0;
1497 }
1498
1499 /* Validate the protocol list. */
1500 CBS protocol_name_list_copy = protocol_name_list;
1501 while (CBS_len(&protocol_name_list_copy) > 0) {
1502 CBS protocol_name;
1503
1504 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1505 /* Empty protocol names are forbidden. */
1506 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001507 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1508 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001509 return 0;
1510 }
1511 }
1512
1513 const uint8_t *selected;
1514 uint8_t selected_len;
1515 if (ssl->ctx->alpn_select_cb(
1516 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1517 CBS_len(&protocol_name_list),
1518 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1519 OPENSSL_free(ssl->s3->alpn_selected);
1520 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1521 if (ssl->s3->alpn_selected == NULL) {
1522 *out_alert = SSL_AD_INTERNAL_ERROR;
1523 return 0;
1524 }
1525 ssl->s3->alpn_selected_len = selected_len;
1526 }
1527
1528 return 1;
1529}
1530
David Benjamin8c880a22016-12-03 02:20:34 -05001531static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1532 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001533 if (ssl->s3->alpn_selected == NULL) {
1534 return 1;
1535 }
1536
1537 CBB contents, proto_list, proto;
1538 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1539 !CBB_add_u16_length_prefixed(out, &contents) ||
1540 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1541 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001542 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1543 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001544 !CBB_flush(out)) {
1545 return 0;
1546 }
1547
1548 return 1;
1549}
1550
1551
Adam Langley49c7af12015-07-10 14:33:46 -07001552/* Channel ID.
1553 *
1554 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1555
David Benjamin8c880a22016-12-03 02:20:34 -05001556static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1557 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001558}
1559
David Benjamin8c880a22016-12-03 02:20:34 -05001560static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1561 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001562 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001563 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001564 return 1;
1565 }
1566
1567 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1568 !CBB_add_u16(out, 0 /* length */)) {
1569 return 0;
1570 }
1571
1572 return 1;
1573}
1574
David Benjamin8c880a22016-12-03 02:20:34 -05001575static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1576 uint8_t *out_alert, CBS *contents) {
1577 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001578 if (contents == NULL) {
1579 return 1;
1580 }
1581
David Benjamince079fd2016-08-02 16:22:34 -04001582 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001583 assert(ssl->tlsext_channel_id_enabled);
1584
1585 if (CBS_len(contents) != 0) {
1586 return 0;
1587 }
1588
1589 ssl->s3->tlsext_channel_id_valid = 1;
1590 return 1;
1591}
1592
David Benjamin8c880a22016-12-03 02:20:34 -05001593static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1594 uint8_t *out_alert, CBS *contents) {
1595 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001596 if (contents == NULL ||
1597 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001598 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001599 return 1;
1600 }
1601
1602 if (CBS_len(contents) != 0) {
1603 return 0;
1604 }
1605
1606 ssl->s3->tlsext_channel_id_valid = 1;
1607 return 1;
1608}
1609
David Benjamin8c880a22016-12-03 02:20:34 -05001610static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1611 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001612 if (!ssl->s3->tlsext_channel_id_valid) {
1613 return 1;
1614 }
1615
1616 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1617 !CBB_add_u16(out, 0 /* length */)) {
1618 return 0;
1619 }
1620
1621 return 1;
1622}
1623
Adam Langley391250d2015-07-15 19:06:07 -07001624
1625/* Secure Real-time Transport Protocol (SRTP) extension.
1626 *
1627 * https://tools.ietf.org/html/rfc5764 */
1628
Adam Langley391250d2015-07-15 19:06:07 -07001629
David Benjamin8c880a22016-12-03 02:20:34 -05001630static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1631 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001632}
1633
David Benjamin8c880a22016-12-03 02:20:34 -05001634static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1635 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001636 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1637 if (profiles == NULL) {
1638 return 1;
1639 }
1640 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1641 if (num_profiles == 0) {
1642 return 1;
1643 }
1644
1645 CBB contents, profile_ids;
1646 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1647 !CBB_add_u16_length_prefixed(out, &contents) ||
1648 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1649 return 0;
1650 }
1651
David Benjamin54091232016-09-05 12:47:25 -04001652 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001653 if (!CBB_add_u16(&profile_ids,
1654 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1655 return 0;
1656 }
1657 }
1658
1659 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1660 !CBB_flush(out)) {
1661 return 0;
1662 }
1663
1664 return 1;
1665}
1666
David Benjamin8c880a22016-12-03 02:20:34 -05001667static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001668 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001669 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001670 if (contents == NULL) {
1671 return 1;
1672 }
1673
1674 /* The extension consists of a u16-prefixed profile ID list containing a
1675 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1676 *
1677 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1678 CBS profile_ids, srtp_mki;
1679 uint16_t profile_id;
1680 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1681 !CBS_get_u16(&profile_ids, &profile_id) ||
1682 CBS_len(&profile_ids) != 0 ||
1683 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1684 CBS_len(contents) != 0) {
1685 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1686 return 0;
1687 }
1688
1689 if (CBS_len(&srtp_mki) != 0) {
1690 /* Must be no MKI, since we never offer one. */
1691 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1692 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1693 return 0;
1694 }
1695
1696 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1697
1698 /* Check to see if the server gave us something we support (and presumably
1699 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001700 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001701 const SRTP_PROTECTION_PROFILE *profile =
1702 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1703
1704 if (profile->id == profile_id) {
1705 ssl->srtp_profile = profile;
1706 return 1;
1707 }
1708 }
1709
1710 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1711 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1712 return 0;
1713}
1714
David Benjamin8c880a22016-12-03 02:20:34 -05001715static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001716 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001717 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001718 if (contents == NULL) {
1719 return 1;
1720 }
1721
1722 CBS profile_ids, srtp_mki;
1723 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1724 CBS_len(&profile_ids) < 2 ||
1725 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1726 CBS_len(contents) != 0) {
1727 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1728 return 0;
1729 }
1730 /* Discard the MKI value for now. */
1731
1732 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1733 SSL_get_srtp_profiles(ssl);
1734
1735 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001736 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001737 const SRTP_PROTECTION_PROFILE *server_profile =
1738 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1739
1740 CBS profile_ids_tmp;
1741 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1742
1743 while (CBS_len(&profile_ids_tmp) > 0) {
1744 uint16_t profile_id;
1745 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1746 return 0;
1747 }
1748
1749 if (server_profile->id == profile_id) {
1750 ssl->srtp_profile = server_profile;
1751 return 1;
1752 }
1753 }
1754 }
1755
1756 return 1;
1757}
1758
David Benjamin8c880a22016-12-03 02:20:34 -05001759static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1760 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001761 if (ssl->srtp_profile == NULL) {
1762 return 1;
1763 }
1764
1765 CBB contents, profile_ids;
1766 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1767 !CBB_add_u16_length_prefixed(out, &contents) ||
1768 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1769 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1770 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1771 !CBB_flush(out)) {
1772 return 0;
1773 }
1774
1775 return 1;
1776}
1777
Adam Langleybdd5d662015-07-20 16:19:08 -07001778
1779/* EC point formats.
1780 *
1781 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1782
David Benjamin8c880a22016-12-03 02:20:34 -05001783static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001784 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001785 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1786 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001787 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1788 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001789 !CBB_flush(out)) {
1790 return 0;
1791 }
1792
1793 return 1;
1794}
1795
David Benjamin8c880a22016-12-03 02:20:34 -05001796static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001797 /* The point format extension is unneccessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -04001798 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin70aba262016-11-01 12:08:15 -04001799 return 1;
1800 }
1801
David Benjamin8c880a22016-12-03 02:20:34 -05001802 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001803}
1804
David Benjamin8c880a22016-12-03 02:20:34 -05001805static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001806 CBS *contents) {
1807 if (contents == NULL) {
1808 return 1;
1809 }
1810
David Benjamin8c880a22016-12-03 02:20:34 -05001811 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001812 return 0;
1813 }
1814
Adam Langleybdd5d662015-07-20 16:19:08 -07001815 CBS ec_point_format_list;
1816 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1817 CBS_len(contents) != 0) {
1818 return 0;
1819 }
1820
David Benjaminfc059942015-07-30 23:01:59 -04001821 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1822 * point format. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05001823 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1824 TLSEXT_ECPOINTFORMAT_uncompressed,
1825 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001826 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001827 return 0;
1828 }
1829
1830 return 1;
1831}
1832
David Benjamin8c880a22016-12-03 02:20:34 -05001833static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001834 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001835 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001836 return 1;
1837 }
1838
David Benjamin8c880a22016-12-03 02:20:34 -05001839 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001840}
1841
David Benjamin8c880a22016-12-03 02:20:34 -05001842static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1843 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001844 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1845 return 1;
1846 }
1847
David Benjamin45738dd2017-02-09 20:01:26 -05001848 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1849 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001850 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001851
1852 if (!using_ecc) {
1853 return 1;
1854 }
1855
David Benjamin8c880a22016-12-03 02:20:34 -05001856 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001857}
1858
Steven Valdeza833c352016-11-01 13:39:36 -04001859
Steven Valdez4aa154e2016-07-29 14:32:55 -04001860/* Pre Shared Key
1861 *
Steven Valdeza833c352016-11-01 13:39:36 -04001862 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001863
David Benjamin8c880a22016-12-03 02:20:34 -05001864static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1865 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001866 uint16_t session_version;
David Benjamin68161cb2017-06-20 14:49:43 -04001867 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdeza833c352016-11-01 13:39:36 -04001868 !ssl->method->version_from_wire(&session_version,
1869 ssl->session->ssl_version) ||
1870 session_version < TLS1_3_VERSION) {
1871 return 0;
1872 }
1873
Steven Valdez908ac192017-01-12 13:17:07 -05001874 const EVP_MD *digest = SSL_SESSION_get_digest(ssl->session, ssl);
1875 if (digest == NULL) {
1876 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1877 return 0;
1878 }
1879
Steven Valdeza833c352016-11-01 13:39:36 -04001880 size_t binder_len = EVP_MD_size(digest);
1881 return 15 + ssl->session->tlsext_ticklen + binder_len;
1882}
1883
David Benjamin8c880a22016-12-03 02:20:34 -05001884static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1885 SSL *const ssl = hs->ssl;
David Benjamin2dc02042016-09-19 19:57:37 -04001886 uint16_t session_version;
David Benjamin68161cb2017-06-20 14:49:43 -04001887 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001888 !ssl->method->version_from_wire(&session_version,
1889 ssl->session->ssl_version) ||
1890 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001891 return 1;
1892 }
1893
David Benjaminad8f5e12017-02-20 17:00:20 -05001894 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001895 ssl_get_current_time(ssl, &now);
1896 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1897 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1898
1899 /* Fill in a placeholder zero binder of the appropriate length. It will be
1900 * computed and filled in later after length prefixes are computed. */
1901 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez908ac192017-01-12 13:17:07 -05001902
1903 const EVP_MD *digest = SSL_SESSION_get_digest(ssl->session, ssl);
1904 if (digest == NULL) {
1905 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1906 return 0;
1907 }
1908
Steven Valdeza833c352016-11-01 13:39:36 -04001909 size_t binder_len = EVP_MD_size(digest);
1910
1911 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001912 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1913 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001914 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001915 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1916 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001917 ssl->session->tlsext_ticklen) ||
1918 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1919 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1920 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1921 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001922 return 0;
1923 }
1924
David Benjamin8c880a22016-12-03 02:20:34 -05001925 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001926 return CBB_flush(out);
1927}
1928
David Benjamin8baf9632016-11-17 17:11:16 +09001929int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1930 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001931 CBS *contents) {
1932 uint16_t psk_id;
1933 if (!CBS_get_u16(contents, &psk_id) ||
1934 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001935 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001936 *out_alert = SSL_AD_DECODE_ERROR;
1937 return 0;
1938 }
1939
David Benjamineab773a2016-11-09 18:32:35 -05001940 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001941 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001942 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001943 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1944 return 0;
1945 }
1946
1947 return 1;
1948}
1949
David Benjamin35ac5b72017-03-03 15:05:56 -05001950int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001951 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001952 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04001953 /* We only process the first PSK identity since we don't support pure PSK. */
David Benjamin707af292017-03-10 17:47:18 -05001954 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001955 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001956 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001957 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001958 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001959 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001960 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001961 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001962 *out_alert = SSL_AD_DECODE_ERROR;
1963 return 0;
1964 }
1965
Steven Valdeza833c352016-11-01 13:39:36 -04001966 *out_binders = binders;
1967
David Benjaminaedf3032016-12-01 16:47:56 -05001968 /* Check the syntax of the remaining identities, but do not process them. */
1969 size_t num_identities = 1;
1970 while (CBS_len(&identities) != 0) {
1971 CBS unused_ticket;
1972 uint32_t unused_obfuscated_ticket_age;
1973 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1974 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1975 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1976 *out_alert = SSL_AD_DECODE_ERROR;
1977 return 0;
1978 }
1979
1980 num_identities++;
1981 }
1982
1983 /* Check the syntax of the binders. The value will be checked later if
1984 * resuming. */
1985 size_t num_binders = 0;
1986 while (CBS_len(&binders) != 0) {
1987 CBS binder;
1988 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1989 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1990 *out_alert = SSL_AD_DECODE_ERROR;
1991 return 0;
1992 }
1993
1994 num_binders++;
1995 }
1996
1997 if (num_identities != num_binders) {
1998 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1999 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04002000 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04002001 }
2002
David Benjamine7f60a22016-11-16 18:54:25 +09002003 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002004}
2005
David Benjamin8baf9632016-11-17 17:11:16 +09002006int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2007 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002008 return 1;
2009 }
2010
2011 CBB contents;
2012 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2013 !CBB_add_u16_length_prefixed(out, &contents) ||
2014 /* We only consider the first identity for resumption */
2015 !CBB_add_u16(&contents, 0) ||
2016 !CBB_flush(out)) {
2017 return 0;
2018 }
2019
2020 return 1;
2021}
2022
2023
Steven Valdeza833c352016-11-01 13:39:36 -04002024/* Pre-Shared Key Exchange Modes
2025 *
2026 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002027
David Benjamin8c880a22016-12-03 02:20:34 -05002028static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2029 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04002030 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04002031 return 1;
2032 }
2033
2034 CBB contents, ke_modes;
2035 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2036 !CBB_add_u16_length_prefixed(out, &contents) ||
2037 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2038 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2039 return 0;
2040 }
2041
2042 return CBB_flush(out);
2043}
2044
David Benjamin8c880a22016-12-03 02:20:34 -05002045static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002046 uint8_t *out_alert,
2047 CBS *contents) {
2048 if (contents == NULL) {
2049 return 1;
2050 }
2051
Steven Valdeza833c352016-11-01 13:39:36 -04002052 CBS ke_modes;
2053 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2054 CBS_len(&ke_modes) == 0 ||
2055 CBS_len(contents) != 0) {
2056 *out_alert = SSL_AD_DECODE_ERROR;
2057 return 0;
2058 }
2059
2060 /* We only support tickets with PSK_DHE_KE. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05002061 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2062 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002063
2064 return 1;
2065}
2066
2067
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002068/* Early Data Indication
2069 *
2070 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2071
David Benjamin8c880a22016-12-03 02:20:34 -05002072static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002073 SSL *const ssl = hs->ssl;
2074 uint16_t session_version;
2075 if (ssl->session == NULL ||
2076 !ssl->method->version_from_wire(&session_version,
2077 ssl->session->ssl_version) ||
2078 session_version < TLS1_3_VERSION ||
2079 ssl->session->ticket_max_early_data == 0 ||
2080 hs->received_hello_retry_request ||
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05002081 !ssl->cert->enable_early_data) {
Steven Valdez2d850622017-01-11 11:34:52 -05002082 return 1;
2083 }
2084
2085 hs->early_data_offered = 1;
2086
2087 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2088 !CBB_add_u16(out, 0) ||
2089 !CBB_flush(out)) {
2090 return 0;
2091 }
2092
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002093 return 1;
2094}
2095
Steven Valdez2d850622017-01-11 11:34:52 -05002096static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002097 uint8_t *out_alert, CBS *contents) {
2098 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002099 if (contents == NULL) {
2100 return 1;
2101 }
2102
2103 if (CBS_len(contents) != 0) {
2104 *out_alert = SSL_AD_DECODE_ERROR;
2105 return 0;
2106 }
2107
Steven Valdez2d850622017-01-11 11:34:52 -05002108 if (!ssl->s3->session_reused) {
2109 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2110 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2111 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002112 }
Steven Valdez2d850622017-01-11 11:34:52 -05002113
2114 ssl->early_data_accepted = 1;
2115 return 1;
2116}
2117
2118static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2119 uint8_t *out_alert, CBS *contents) {
2120 SSL *const ssl = hs->ssl;
2121 if (contents == NULL ||
2122 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2123 return 1;
2124 }
2125
2126 if (CBS_len(contents) != 0) {
2127 *out_alert = SSL_AD_DECODE_ERROR;
2128 return 0;
2129 }
2130
2131 hs->early_data_offered = 1;
2132 return 1;
2133}
2134
2135static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2136 if (!hs->ssl->early_data_accepted) {
2137 return 1;
2138 }
2139
2140 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2141 !CBB_add_u16(out, 0) ||
2142 !CBB_flush(out)) {
2143 return 0;
2144 }
2145
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002146 return 1;
2147}
2148
2149
Steven Valdez143e8b32016-07-11 13:19:03 -04002150/* Key Share
2151 *
David Benjamina128a552016-10-13 14:26:33 -04002152 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002153
David Benjamin8c880a22016-12-03 02:20:34 -05002154static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2155 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002156 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002157 return 1;
2158 }
2159
2160 CBB contents, kse_bytes;
2161 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2162 !CBB_add_u16_length_prefixed(out, &contents) ||
2163 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2164 return 0;
2165 }
2166
David Benjamin8c880a22016-12-03 02:20:34 -05002167 uint16_t group_id = hs->retry_group;
2168 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002169 /* We received a HelloRetryRequest without a new curve, so there is no new
2170 * share to append. Leave |ecdh_ctx| as-is. */
2171 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002172 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2173 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002174 return 0;
2175 }
David Benjamin8c880a22016-12-03 02:20:34 -05002176 OPENSSL_free(hs->key_share_bytes);
2177 hs->key_share_bytes = NULL;
2178 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002179 if (group_id == 0) {
2180 return CBB_flush(out);
2181 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002182 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002183 /* Add a fake group. See draft-davidben-tls-grease-01. */
2184 if (ssl->ctx->grease_enabled &&
2185 (!CBB_add_u16(&kse_bytes,
2186 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2187 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2188 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2189 return 0;
2190 }
2191
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002192 /* Predict the most preferred group. */
2193 const uint16_t *groups;
2194 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002195 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002196 if (groups_len == 0) {
2197 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2198 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002199 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002200
2201 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002202 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002203
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002204 CBB key_exchange;
2205 if (!CBB_add_u16(&kse_bytes, group_id) ||
2206 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002207 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2208 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002209 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002210 return 0;
2211 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002212
David Benjamin8c880a22016-12-03 02:20:34 -05002213 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002214 /* Save the contents of the extension to repeat it in the second
2215 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002216 hs->key_share_bytes_len = CBB_len(&kse_bytes);
2217 hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
2218 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002219 return 0;
2220 }
2221 }
2222
Steven Valdez143e8b32016-07-11 13:19:03 -04002223 return CBB_flush(out);
2224}
2225
David Benjamin8baf9632016-11-17 17:11:16 +09002226int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002227 size_t *out_secret_len,
2228 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002229 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002230 uint16_t group_id;
2231 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002232 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2233 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002234 *out_alert = SSL_AD_DECODE_ERROR;
2235 return 0;
2236 }
2237
David Benjamin8baf9632016-11-17 17:11:16 +09002238 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002239 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2240 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2241 return 0;
2242 }
2243
David Benjamin8baf9632016-11-17 17:11:16 +09002244 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2245 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002246 *out_alert = SSL_AD_INTERNAL_ERROR;
2247 return 0;
2248 }
2249
David Benjamin45738dd2017-02-09 20:01:26 -05002250 hs->new_session->group_id = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002251 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002252 return 1;
2253}
2254
David Benjamin8baf9632016-11-17 17:11:16 +09002255int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002256 uint8_t **out_secret,
2257 size_t *out_secret_len,
2258 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002259 uint16_t group_id;
2260 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002261 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002262 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2263 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2264 return 0;
2265 }
2266
2267 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002268 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002269 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002270 return 0;
2271 }
2272
David Benjamin7e1f9842016-09-20 19:24:40 -04002273 /* Find the corresponding key share. */
2274 int found = 0;
2275 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002276 while (CBS_len(&key_shares) > 0) {
2277 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002278 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002279 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002280 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2281 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002282 return 0;
2283 }
2284
David Benjamin7e1f9842016-09-20 19:24:40 -04002285 if (id == group_id) {
2286 if (found) {
2287 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2288 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2289 return 0;
2290 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002291
David Benjamin7e1f9842016-09-20 19:24:40 -04002292 found = 1;
2293 peer_key = peer_key_tmp;
2294 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002295 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002296 }
2297
David Benjamin7e1f9842016-09-20 19:24:40 -04002298 if (!found) {
2299 *out_found = 0;
2300 *out_secret = NULL;
2301 *out_secret_len = 0;
2302 return 1;
2303 }
2304
2305 /* Compute the DH secret. */
2306 uint8_t *secret = NULL;
2307 size_t secret_len;
2308 SSL_ECDH_CTX group;
David Benjamin17cf2cb2016-12-13 01:07:13 -05002309 OPENSSL_memset(&group, 0, sizeof(SSL_ECDH_CTX));
David Benjamin7e1f9842016-09-20 19:24:40 -04002310 CBB public_key;
2311 if (!CBB_init(&public_key, 32) ||
2312 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002313 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
2314 CBS_data(&peer_key), CBS_len(&peer_key)) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002315 !CBB_finish(&public_key, &hs->ecdh_public_key,
2316 &hs->ecdh_public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002317 OPENSSL_free(secret);
2318 SSL_ECDH_CTX_cleanup(&group);
2319 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002320 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002321 return 0;
2322 }
2323
2324 SSL_ECDH_CTX_cleanup(&group);
2325
2326 *out_secret = secret;
2327 *out_secret_len = secret_len;
2328 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002329 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002330}
2331
David Benjamin8baf9632016-11-17 17:11:16 +09002332int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002333 uint16_t group_id;
2334 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002335 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002336 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2337 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2338 !CBB_add_u16(&kse_bytes, group_id) ||
2339 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002340 !CBB_add_bytes(&public_key, hs->ecdh_public_key,
2341 hs->ecdh_public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002342 !CBB_flush(out)) {
2343 return 0;
2344 }
2345
David Benjaminbf833c32017-03-30 15:45:21 -05002346 OPENSSL_free(hs->ecdh_public_key);
2347 hs->ecdh_public_key = NULL;
2348 hs->ecdh_public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002349
David Benjamin45738dd2017-02-09 20:01:26 -05002350 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002351 return 1;
2352}
2353
2354
Steven Valdezfdd10992016-09-15 16:27:05 -04002355/* Supported Versions
2356 *
2357 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2358
David Benjamin8c880a22016-12-03 02:20:34 -05002359static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2360 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002361 if (hs->max_version <= TLS1_2_VERSION) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002362 return 1;
2363 }
2364
2365 CBB contents, versions;
2366 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2367 !CBB_add_u16_length_prefixed(out, &contents) ||
2368 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2369 return 0;
2370 }
2371
David Benjamind9791bf2016-09-27 16:39:52 -04002372 /* Add a fake version. See draft-davidben-tls-grease-01. */
2373 if (ssl->ctx->grease_enabled &&
2374 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2375 return 0;
2376 }
2377
David Benjamin68161cb2017-06-20 14:49:43 -04002378 for (uint16_t version = hs->max_version; version >= hs->min_version;
2379 version--) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002380 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2381 return 0;
2382 }
2383 }
2384
2385 if (!CBB_flush(out)) {
2386 return 0;
2387 }
2388
2389 return 1;
2390}
2391
2392
David Benjamin3baa6e12016-10-07 21:10:38 -04002393/* Cookie
2394 *
2395 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2396
David Benjamin8c880a22016-12-03 02:20:34 -05002397static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2398 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002399 return 1;
2400 }
2401
2402 CBB contents, cookie;
2403 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2404 !CBB_add_u16_length_prefixed(out, &contents) ||
2405 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002406 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002407 !CBB_flush(out)) {
2408 return 0;
2409 }
2410
2411 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002412 OPENSSL_free(hs->cookie);
2413 hs->cookie = NULL;
2414 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002415 return 1;
2416}
2417
2418
Steven Valdezce902a92016-05-17 11:47:53 -04002419/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002420 *
Steven Valdezce902a92016-05-17 11:47:53 -04002421 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002422 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002423
David Benjamin8c880a22016-12-03 02:20:34 -05002424static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2425 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002426 CBB contents, groups_bytes;
2427 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002428 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002429 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002430 return 0;
2431 }
2432
David Benjamin65ac9972016-09-02 21:35:25 -04002433 /* Add a fake group. See draft-davidben-tls-grease-01. */
2434 if (ssl->ctx->grease_enabled &&
2435 !CBB_add_u16(&groups_bytes,
2436 ssl_get_grease_value(ssl, ssl_grease_group))) {
2437 return 0;
2438 }
2439
Steven Valdezce902a92016-05-17 11:47:53 -04002440 const uint16_t *groups;
2441 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002442 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002443
David Benjamin54091232016-09-05 12:47:25 -04002444 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002445 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002446 return 0;
2447 }
2448 }
2449
2450 return CBB_flush(out);
2451}
2452
David Benjamin8c880a22016-12-03 02:20:34 -05002453static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2454 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002455 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002456 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2457 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002458 return 1;
2459}
2460
David Benjamin8c880a22016-12-03 02:20:34 -05002461static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2462 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002463 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002464 if (contents == NULL) {
2465 return 1;
2466 }
2467
Steven Valdezce902a92016-05-17 11:47:53 -04002468 CBS supported_group_list;
2469 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2470 CBS_len(&supported_group_list) == 0 ||
2471 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002472 CBS_len(contents) != 0) {
2473 return 0;
2474 }
2475
David Benjamin8c880a22016-12-03 02:20:34 -05002476 hs->peer_supported_group_list =
2477 OPENSSL_malloc(CBS_len(&supported_group_list));
2478 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002479 *out_alert = SSL_AD_INTERNAL_ERROR;
2480 return 0;
2481 }
2482
Steven Valdezce902a92016-05-17 11:47:53 -04002483 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002484 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002485 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002486 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002487 goto err;
2488 }
2489 }
2490
Steven Valdezce902a92016-05-17 11:47:53 -04002491 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002492 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002493
2494 return 1;
2495
2496err:
David Benjamin8c880a22016-12-03 02:20:34 -05002497 OPENSSL_free(hs->peer_supported_group_list);
2498 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002499 *out_alert = SSL_AD_INTERNAL_ERROR;
2500 return 0;
2501}
2502
David Benjamin8c880a22016-12-03 02:20:34 -05002503static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002504 /* Servers don't echo this extension. */
2505 return 1;
2506}
2507
2508
Adam Langley614c66a2015-06-12 15:26:58 -07002509/* kExtensions contains all the supported extensions. */
2510static const struct tls_extension kExtensions[] = {
2511 {
Adam Langley5021b222015-06-12 18:27:58 -07002512 TLSEXT_TYPE_renegotiate,
2513 NULL,
2514 ext_ri_add_clienthello,
2515 ext_ri_parse_serverhello,
2516 ext_ri_parse_clienthello,
2517 ext_ri_add_serverhello,
2518 },
2519 {
Adam Langley614c66a2015-06-12 15:26:58 -07002520 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002521 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002522 ext_sni_add_clienthello,
2523 ext_sni_parse_serverhello,
2524 ext_sni_parse_clienthello,
2525 ext_sni_add_serverhello,
2526 },
Adam Langley0a056712015-07-01 15:03:33 -07002527 {
2528 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002529 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002530 ext_ems_add_clienthello,
2531 ext_ems_parse_serverhello,
2532 ext_ems_parse_clienthello,
2533 ext_ems_add_serverhello,
2534 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002535 {
2536 TLSEXT_TYPE_session_ticket,
2537 NULL,
2538 ext_ticket_add_clienthello,
2539 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002540 /* Ticket extension client parsing is handled in ssl_session.c */
2541 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002542 ext_ticket_add_serverhello,
2543 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002544 {
2545 TLSEXT_TYPE_signature_algorithms,
2546 NULL,
2547 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002548 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002549 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002550 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002551 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002552 {
2553 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002554 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002555 ext_ocsp_add_clienthello,
2556 ext_ocsp_parse_serverhello,
2557 ext_ocsp_parse_clienthello,
2558 ext_ocsp_add_serverhello,
2559 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002560 {
2561 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002562 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002563 ext_npn_add_clienthello,
2564 ext_npn_parse_serverhello,
2565 ext_npn_parse_clienthello,
2566 ext_npn_add_serverhello,
2567 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002568 {
2569 TLSEXT_TYPE_certificate_timestamp,
2570 NULL,
2571 ext_sct_add_clienthello,
2572 ext_sct_parse_serverhello,
2573 ext_sct_parse_clienthello,
2574 ext_sct_add_serverhello,
2575 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002576 {
2577 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002578 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002579 ext_alpn_add_clienthello,
2580 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002581 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2582 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002583 ext_alpn_add_serverhello,
2584 },
Adam Langley49c7af12015-07-10 14:33:46 -07002585 {
2586 TLSEXT_TYPE_channel_id,
2587 ext_channel_id_init,
2588 ext_channel_id_add_clienthello,
2589 ext_channel_id_parse_serverhello,
2590 ext_channel_id_parse_clienthello,
2591 ext_channel_id_add_serverhello,
2592 },
Adam Langley391250d2015-07-15 19:06:07 -07002593 {
2594 TLSEXT_TYPE_srtp,
2595 ext_srtp_init,
2596 ext_srtp_add_clienthello,
2597 ext_srtp_parse_serverhello,
2598 ext_srtp_parse_clienthello,
2599 ext_srtp_add_serverhello,
2600 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002601 {
2602 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002603 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002604 ext_ec_point_add_clienthello,
2605 ext_ec_point_parse_serverhello,
2606 ext_ec_point_parse_clienthello,
2607 ext_ec_point_add_serverhello,
2608 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002609 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002610 TLSEXT_TYPE_key_share,
2611 NULL,
2612 ext_key_share_add_clienthello,
2613 forbid_parse_serverhello,
2614 ignore_parse_clienthello,
2615 dont_add_serverhello,
2616 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002617 {
Steven Valdeza833c352016-11-01 13:39:36 -04002618 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002619 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002620 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002621 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002622 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002623 dont_add_serverhello,
2624 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002625 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002626 TLSEXT_TYPE_early_data,
2627 NULL,
2628 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002629 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002630 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002631 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002632 },
2633 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002634 TLSEXT_TYPE_supported_versions,
2635 NULL,
2636 ext_supported_versions_add_clienthello,
2637 forbid_parse_serverhello,
2638 ignore_parse_clienthello,
2639 dont_add_serverhello,
2640 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002641 {
2642 TLSEXT_TYPE_cookie,
2643 NULL,
2644 ext_cookie_add_clienthello,
2645 forbid_parse_serverhello,
2646 ignore_parse_clienthello,
2647 dont_add_serverhello,
2648 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002649 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2650 * intolerant to the last extension being zero-length. See
2651 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002652 {
Steven Valdezce902a92016-05-17 11:47:53 -04002653 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002654 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002655 ext_supported_groups_add_clienthello,
2656 ext_supported_groups_parse_serverhello,
2657 ext_supported_groups_parse_clienthello,
2658 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002659 },
Adam Langley614c66a2015-06-12 15:26:58 -07002660};
2661
2662#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2663
Adam Langley4cfa96b2015-07-01 11:56:55 -07002664OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002665 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002666 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002667OPENSSL_COMPILE_ASSERT(
2668 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2669 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002670
Adam Langley614c66a2015-06-12 15:26:58 -07002671static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2672 uint16_t value) {
2673 unsigned i;
2674 for (i = 0; i < kNumExtensions; i++) {
2675 if (kExtensions[i].value == value) {
2676 *out_index = i;
2677 return &kExtensions[i];
2678 }
2679 }
2680
2681 return NULL;
2682}
2683
Adam Langley09505632015-07-30 18:10:13 -07002684int SSL_extension_supported(unsigned extension_value) {
2685 uint32_t index;
2686 return extension_value == TLSEXT_TYPE_padding ||
2687 tls_extension_find(&index, extension_value) != NULL;
2688}
2689
David Benjamin8c880a22016-12-03 02:20:34 -05002690int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2691 SSL *const ssl = hs->ssl;
David Benjaminf04c2e92016-12-06 13:35:25 -05002692 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2693 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002694 !ssl->s3->send_connection_binding) {
2695 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002696 }
Adam Langley95c29f32014-06-20 12:00:00 -07002697
David Benjamine8d53502015-10-10 14:13:23 -04002698 CBB extensions;
2699 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002700 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002701 }
Adam Langley95c29f32014-06-20 12:00:00 -07002702
David Benjamin8c880a22016-12-03 02:20:34 -05002703 hs->extensions.sent = 0;
2704 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002705
David Benjamin54091232016-09-05 12:47:25 -04002706 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002707 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002708 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002709 }
2710 }
Adam Langley95c29f32014-06-20 12:00:00 -07002711
David Benjamin65ac9972016-09-02 21:35:25 -04002712 uint16_t grease_ext1 = 0;
2713 if (ssl->ctx->grease_enabled) {
2714 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2715 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2716 if (!CBB_add_u16(&extensions, grease_ext1) ||
2717 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2718 goto err;
2719 }
2720 }
2721
David Benjamin54091232016-09-05 12:47:25 -04002722 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002723 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002724 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002725 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002726 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002727 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002728 }
Adam Langley95c29f32014-06-20 12:00:00 -07002729
Adam Langley33ad2b52015-07-20 17:43:53 -07002730 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002731 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002732 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002733 }
Adam Langley75712922014-10-10 16:23:43 -07002734
David Benjamin2bd19172016-11-17 16:47:15 +09002735 if (!custom_ext_add_clienthello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002736 goto err;
2737 }
2738
David Benjamin65ac9972016-09-02 21:35:25 -04002739 if (ssl->ctx->grease_enabled) {
2740 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2741 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2742
2743 /* The two fake extensions must not have the same value. GREASE values are
2744 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2745 * one. */
2746 if (grease_ext1 == grease_ext2) {
2747 grease_ext2 ^= 0x1010;
2748 }
2749
2750 if (!CBB_add_u16(&extensions, grease_ext2) ||
2751 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2752 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2753 goto err;
2754 }
2755 }
2756
David Benjamince079fd2016-08-02 16:22:34 -04002757 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002758 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002759 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002760 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002761 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002762 *
2763 * NB: because this code works out the length of all existing extensions
2764 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002765 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002766 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002767 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002768 * Server 7.0 is intolerant to the last extension being zero-length. See
2769 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002770 if (padding_len >= 4 + 1) {
2771 padding_len -= 4;
2772 } else {
2773 padding_len = 1;
2774 }
Adam Langley95c29f32014-06-20 12:00:00 -07002775
Adam Langley33ad2b52015-07-20 17:43:53 -07002776 uint8_t *padding_bytes;
2777 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2778 !CBB_add_u16(&extensions, padding_len) ||
2779 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2780 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002781 }
Adam Langley75712922014-10-10 16:23:43 -07002782
David Benjamin17cf2cb2016-12-13 01:07:13 -05002783 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002784 }
2785 }
Adam Langley75712922014-10-10 16:23:43 -07002786
Steven Valdeza833c352016-11-01 13:39:36 -04002787 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002788 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002789 goto err;
2790 }
2791
David Benjamina01deee2015-12-08 18:56:31 -05002792 /* Discard empty extensions blocks. */
2793 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002794 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002795 }
2796
David Benjamine8d53502015-10-10 14:13:23 -04002797 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002798
2799err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002800 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002801 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002802}
Adam Langley95c29f32014-06-20 12:00:00 -07002803
David Benjamin8c880a22016-12-03 02:20:34 -05002804int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2805 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002806 CBB extensions;
2807 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002808 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002809 }
2810
David Benjamin8c880a22016-12-03 02:20:34 -05002811 for (unsigned i = 0; i < kNumExtensions; i++) {
2812 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002813 /* Don't send extensions that were not received. */
2814 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002815 }
Adam Langley95c29f32014-06-20 12:00:00 -07002816
David Benjamin8c880a22016-12-03 02:20:34 -05002817 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002818 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002819 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002820 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002821 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002822 }
Adam Langley95c29f32014-06-20 12:00:00 -07002823
David Benjamin2bd19172016-11-17 16:47:15 +09002824 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002825 goto err;
2826 }
2827
Steven Valdez143e8b32016-07-11 13:19:03 -04002828 /* Discard empty extensions blocks before TLS 1.3. */
2829 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2830 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002831 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002832 }
2833
David Benjamin56380462015-10-10 14:59:09 -04002834 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002835
2836err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002837 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002838 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002839}
Adam Langley95c29f32014-06-20 12:00:00 -07002840
David Benjamin731058e2016-12-03 23:15:13 -05002841static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2842 const SSL_CLIENT_HELLO *client_hello,
2843 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002844 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002845 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002846 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002847 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002848 }
2849 }
2850
David Benjamin8c880a22016-12-03 02:20:34 -05002851 hs->extensions.received = 0;
2852 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002853
David Benjamine14ff062016-08-09 16:21:24 -04002854 CBS extensions;
2855 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2856 while (CBS_len(&extensions) != 0) {
2857 uint16_t type;
2858 CBS extension;
2859
2860 /* Decode the next extension. */
2861 if (!CBS_get_u16(&extensions, &type) ||
2862 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002863 *out_alert = SSL_AD_DECODE_ERROR;
2864 return 0;
2865 }
Adam Langley95c29f32014-06-20 12:00:00 -07002866
David Benjamine14ff062016-08-09 16:21:24 -04002867 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2868 * ambiguous. Ignore all but the renegotiation_info extension. */
2869 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2870 continue;
2871 }
Adam Langley95c29f32014-06-20 12:00:00 -07002872
David Benjamine14ff062016-08-09 16:21:24 -04002873 unsigned ext_index;
2874 const struct tls_extension *const ext =
2875 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002876
David Benjamine14ff062016-08-09 16:21:24 -04002877 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002878 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002879 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002880 return 0;
2881 }
David Benjamine14ff062016-08-09 16:21:24 -04002882 continue;
2883 }
2884
David Benjamin8c880a22016-12-03 02:20:34 -05002885 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002886 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002887 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002888 *out_alert = alert;
2889 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002890 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002891 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002892 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002893 }
Adam Langley75712922014-10-10 16:23:43 -07002894
David Benjamin1deb41b2016-08-09 19:36:38 -04002895 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002896 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002897 continue;
2898 }
2899
2900 CBS *contents = NULL, fake_contents;
2901 static const uint8_t kFakeRenegotiateExtension[] = {0};
2902 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2903 ssl_client_cipher_list_contains_cipher(client_hello,
2904 SSL3_CK_SCSV & 0xffff)) {
2905 /* The renegotiation SCSV was received so pretend that we received a
2906 * renegotiation extension. */
2907 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2908 sizeof(kFakeRenegotiateExtension));
2909 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002910 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002911 }
2912
2913 /* Extension wasn't observed so call the callback with a NULL
2914 * parameter. */
2915 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002916 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002917 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002918 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002919 *out_alert = alert;
2920 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002921 }
2922 }
2923
Adam Langleyfcf25832014-12-18 17:42:32 -08002924 return 1;
2925}
Adam Langley95c29f32014-06-20 12:00:00 -07002926
David Benjamin731058e2016-12-03 23:15:13 -05002927int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2928 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002929 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002930 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002931 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002932 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002933 return 0;
2934 }
Adam Langley95c29f32014-06-20 12:00:00 -07002935
David Benjamin8c880a22016-12-03 02:20:34 -05002936 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002937 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002938 return 0;
2939 }
Adam Langley95c29f32014-06-20 12:00:00 -07002940
Adam Langleyfcf25832014-12-18 17:42:32 -08002941 return 1;
2942}
Adam Langley95c29f32014-06-20 12:00:00 -07002943
David Benjamin8c880a22016-12-03 02:20:34 -05002944static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2945 int *out_alert) {
2946 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002947 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2948 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2949 return 1;
2950 }
Adam Langley614c66a2015-06-12 15:26:58 -07002951
Steven Valdez143e8b32016-07-11 13:19:03 -04002952 /* Decode the extensions block and check it is valid. */
2953 CBS extensions;
2954 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2955 !tls1_check_duplicate_extensions(&extensions)) {
2956 *out_alert = SSL_AD_DECODE_ERROR;
2957 return 0;
2958 }
2959
2960 uint32_t received = 0;
2961 while (CBS_len(&extensions) != 0) {
2962 uint16_t type;
2963 CBS extension;
2964
2965 /* Decode the next extension. */
2966 if (!CBS_get_u16(&extensions, &type) ||
2967 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002968 *out_alert = SSL_AD_DECODE_ERROR;
2969 return 0;
2970 }
Adam Langley95c29f32014-06-20 12:00:00 -07002971
Steven Valdez143e8b32016-07-11 13:19:03 -04002972 unsigned ext_index;
2973 const struct tls_extension *const ext =
2974 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002975
Steven Valdez143e8b32016-07-11 13:19:03 -04002976 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002977 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002978 return 0;
2979 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002980 continue;
2981 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002982
David Benjamin5db7c9b2017-01-24 16:17:03 -05002983 OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
2984 too_many_bits);
2985
David Benjamin8c880a22016-12-03 02:20:34 -05002986 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002987 type != TLSEXT_TYPE_renegotiate) {
2988 /* If the extension was never sent then it is illegal, except for the
2989 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002990 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2991 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002992 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002993 return 0;
2994 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002995
Steven Valdez143e8b32016-07-11 13:19:03 -04002996 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002997
Steven Valdez143e8b32016-07-11 13:19:03 -04002998 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002999 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04003000 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003001 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04003002 *out_alert = alert;
3003 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07003004 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003005 }
Adam Langley95c29f32014-06-20 12:00:00 -07003006
David Benjamin54091232016-09-05 12:47:25 -04003007 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003008 if (!(received & (1u << i))) {
3009 /* Extension wasn't observed so call the callback with a NULL
3010 * parameter. */
3011 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003012 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003013 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003014 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07003015 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08003016 return 0;
3017 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003018 }
3019 }
Adam Langley95c29f32014-06-20 12:00:00 -07003020
Adam Langleyfcf25832014-12-18 17:42:32 -08003021 return 1;
3022}
Adam Langley95c29f32014-06-20 12:00:00 -07003023
David Benjamin8c880a22016-12-03 02:20:34 -05003024static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3025 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003026 int ret = SSL_TLSEXT_ERR_NOACK;
3027 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003028
David Benjamin78f8aab2016-03-10 16:33:58 -05003029 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003030 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003031 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05003032 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
3033 ret = ssl->session_ctx->tlsext_servername_callback(
3034 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003035 }
Adam Langley95c29f32014-06-20 12:00:00 -07003036
Adam Langleyfcf25832014-12-18 17:42:32 -08003037 switch (ret) {
3038 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003039 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003040 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003041
Adam Langleyfcf25832014-12-18 17:42:32 -08003042 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003043 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003044 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003045
Adam Langleyfcf25832014-12-18 17:42:32 -08003046 default:
3047 return 1;
3048 }
3049}
Adam Langleyed8270a2014-09-02 13:52:56 -07003050
David Benjamin8c880a22016-12-03 02:20:34 -05003051int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3052 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08003053 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003054 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003055 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003056 return 0;
3057 }
3058
Adam Langleyfcf25832014-12-18 17:42:32 -08003059 return 1;
3060}
Adam Langley95c29f32014-06-20 12:00:00 -07003061
Adam Langley4c341d02017-03-08 19:33:21 -08003062static enum ssl_ticket_aead_result_t
3063ssl_decrypt_ticket_with_cipher_ctx(SSL *ssl, uint8_t **out, size_t *out_len,
3064 int *out_renew_ticket, const uint8_t *ticket,
3065 size_t ticket_len) {
3066 enum ssl_ticket_aead_result_t ret = ssl_ticket_aead_ignore_ticket;
3067 const SSL_CTX *const ssl_ctx = ssl->session_ctx;
David Benjamine3aa1d92015-06-16 15:34:50 -04003068 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003069
David Benjamine3aa1d92015-06-16 15:34:50 -04003070 HMAC_CTX hmac_ctx;
3071 HMAC_CTX_init(&hmac_ctx);
3072 EVP_CIPHER_CTX cipher_ctx;
3073 EVP_CIPHER_CTX_init(&cipher_ctx);
3074
David Benjaminadcc3952015-04-26 13:07:57 -04003075 /* Ensure there is room for the key name and the largest IV
3076 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3077 * the maximum IV length should be well under the minimum size for the
3078 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003079 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
Adam Langley4c341d02017-03-08 19:33:21 -08003080 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003081 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003082 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003083
David Benjamine3aa1d92015-06-16 15:34:50 -04003084 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003085 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3086 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3087 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003088 if (cb_ret < 0) {
Adam Langley4c341d02017-03-08 19:33:21 -08003089 ret = ssl_ticket_aead_error;
3090 goto out;
3091 } else if (cb_ret == 0) {
3092 goto out;
3093 } else if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003094 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003095 }
3096 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003097 /* Check the key name matches. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003098 if (OPENSSL_memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3099 SSL_TICKET_KEY_NAME_LEN) != 0) {
Adam Langley4c341d02017-03-08 19:33:21 -08003100 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003101 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003102 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3103 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003104 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003105 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3106 ssl_ctx->tlsext_tick_aes_key, iv)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003107 ret = ssl_ticket_aead_error;
3108 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003109 }
3110 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003111 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003112
David Benjamine3aa1d92015-06-16 15:34:50 -04003113 /* Check the MAC at the end of the ticket. */
3114 uint8_t mac[EVP_MAX_MD_SIZE];
3115 size_t mac_len = HMAC_size(&hmac_ctx);
3116 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003117 /* The ticket must be large enough for key name, IV, data, and MAC. */
Adam Langley4c341d02017-03-08 19:33:21 -08003118 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003119 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003120 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3121 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003122 int mac_ok =
3123 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3124#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3125 mac_ok = 1;
3126#endif
3127 if (!mac_ok) {
Adam Langley4c341d02017-03-08 19:33:21 -08003128 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003129 }
3130
David Benjamine3aa1d92015-06-16 15:34:50 -04003131 /* Decrypt the session data. */
3132 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3133 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3134 mac_len;
3135 plaintext = OPENSSL_malloc(ciphertext_len);
3136 if (plaintext == NULL) {
Adam Langley4c341d02017-03-08 19:33:21 -08003137 ret = ssl_ticket_aead_error;
3138 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003139 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003140 size_t plaintext_len;
3141#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin17cf2cb2016-12-13 01:07:13 -05003142 OPENSSL_memcpy(plaintext, ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003143 plaintext_len = ciphertext_len;
3144#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003145 if (ciphertext_len >= INT_MAX) {
Adam Langley4c341d02017-03-08 19:33:21 -08003146 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003147 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003148 int len1, len2;
3149 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3150 (int)ciphertext_len) ||
3151 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003152 ERR_clear_error();
3153 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003154 }
Adam Langley4c341d02017-03-08 19:33:21 -08003155 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04003156#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003157
Adam Langley4c341d02017-03-08 19:33:21 -08003158 *out = plaintext;
3159 plaintext = NULL;
3160 *out_len = plaintext_len;
3161 ret = ssl_ticket_aead_success;
3162
3163out:
3164 OPENSSL_free(plaintext);
3165 HMAC_CTX_cleanup(&hmac_ctx);
3166 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3167 return ret;
3168}
3169
3170static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
3171 SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
3172 const uint8_t *ticket, size_t ticket_len) {
3173 uint8_t *plaintext = OPENSSL_malloc(ticket_len);
3174 if (plaintext == NULL) {
3175 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3176 return ssl_ticket_aead_error;
3177 }
3178
3179 size_t plaintext_len;
3180 const enum ssl_ticket_aead_result_t result =
3181 ssl->session_ctx->ticket_aead_method->open(
3182 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3183
3184 if (result == ssl_ticket_aead_success) {
3185 *out = plaintext;
3186 plaintext = NULL;
3187 *out_len = plaintext_len;
3188 }
3189
3190 OPENSSL_free(plaintext);
3191 return result;
3192}
3193
3194enum ssl_ticket_aead_result_t ssl_process_ticket(
3195 SSL *ssl, SSL_SESSION **out_session, int *out_renew_ticket,
3196 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3197 size_t session_id_len) {
3198 *out_renew_ticket = 0;
3199 *out_session = NULL;
3200
3201 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3202 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3203 return ssl_ticket_aead_ignore_ticket;
3204 }
3205
3206 uint8_t *plaintext = NULL;
3207 size_t plaintext_len;
3208 enum ssl_ticket_aead_result_t result;
3209 if (ssl->session_ctx->ticket_aead_method != NULL) {
3210 result = ssl_decrypt_ticket_with_method(
3211 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3212 } else {
3213 result = ssl_decrypt_ticket_with_cipher_ctx(
3214 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3215 }
3216
3217 if (result != ssl_ticket_aead_success) {
3218 return result;
3219 }
3220
David Benjamine3aa1d92015-06-16 15:34:50 -04003221 /* Decode the session. */
Adam Langley46db7af2017-02-01 15:49:37 -08003222 SSL_SESSION *session =
3223 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx);
Adam Langley4c341d02017-03-08 19:33:21 -08003224 OPENSSL_free(plaintext);
3225
David Benjamine3aa1d92015-06-16 15:34:50 -04003226 if (session == NULL) {
3227 ERR_clear_error(); /* Don't leave an error on the queue. */
Adam Langley4c341d02017-03-08 19:33:21 -08003228 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003229 }
3230
3231 /* Copy the client's session ID into the new session, to denote the ticket has
3232 * been accepted. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003233 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003234 session->session_id_length = session_id_len;
3235
3236 *out_session = session;
Adam Langley4c341d02017-03-08 19:33:21 -08003237 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003238}
Adam Langley95c29f32014-06-20 12:00:00 -07003239
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003240int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003241 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003242 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003243 return 1;
3244 }
David Benjamincd996942014-07-20 16:23:51 -04003245
David Benjamin0fc37ef2016-08-17 15:29:46 -04003246 OPENSSL_free(hs->peer_sigalgs);
3247 hs->peer_sigalgs = NULL;
3248 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003249
3250 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003251 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003252 return 0;
3253 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003254 num_sigalgs /= 2;
3255
3256 /* supported_signature_algorithms in the certificate request is
3257 * allowed to be empty. */
3258 if (num_sigalgs == 0) {
3259 return 1;
3260 }
3261
Steven Valdez02563852016-06-23 13:33:05 -04003262 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3263 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003264 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3265 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003266 return 0;
3267 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003268 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003269
3270 CBS sigalgs;
3271 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003272 for (size_t i = 0; i < num_sigalgs; i++) {
3273 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003274 return 0;
3275 }
3276 }
Adam Langley95c29f32014-06-20 12:00:00 -07003277
Adam Langleyfcf25832014-12-18 17:42:32 -08003278 return 1;
3279}
David Benjaminec2f27d2014-11-13 19:17:25 -05003280
David Benjamina3651382017-04-20 17:49:36 -04003281int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
3282 switch (EVP_PKEY_id(pkey)) {
3283 case EVP_PKEY_RSA:
3284 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3285 return 1;
3286 case EVP_PKEY_EC:
3287 *out = SSL_SIGN_ECDSA_SHA1;
3288 return 1;
3289 default:
3290 return 0;
3291 }
3292}
3293
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003294int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3295 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003296 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003297
Steven Valdezf0451ca2016-06-29 13:16:27 -04003298 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
David Benjamina3651382017-04-20 17:49:36 -04003299 * handshake. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04003300 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamina3651382017-04-20 17:49:36 -04003301 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey)) {
3302 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3303 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003304 }
David Benjamina3651382017-04-20 17:49:36 -04003305 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003306 }
3307
David Benjamin3ef76972016-10-17 17:59:54 -04003308 const uint16_t *sigalgs = cert->sigalgs;
3309 size_t num_sigalgs = cert->num_sigalgs;
3310 if (sigalgs == NULL) {
3311 sigalgs = kSignSignatureAlgorithms;
3312 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003313 }
3314
David Benjamin0fc37ef2016-08-17 15:29:46 -04003315 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3316 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3317 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003318 /* If the client didn't specify any signature_algorithms extension then
3319 * we can assume that it supports SHA1. See
3320 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3321 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3322 SSL_SIGN_ECDSA_SHA1};
3323 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003324 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003325 }
3326
David Benjamin0fc37ef2016-08-17 15:29:46 -04003327 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003328 uint16_t sigalg = sigalgs[i];
3329 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3330 * negotiated. */
3331 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjamina232a712017-03-30 15:51:53 -05003332 !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003333 continue;
3334 }
3335
David Benjamin0fc37ef2016-08-17 15:29:46 -04003336 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003337 if (sigalg == peer_sigalgs[j]) {
3338 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003339 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003340 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003341 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003342 }
Adam Langley95c29f32014-06-20 12:00:00 -07003343
David Benjaminea9a0d52016-07-08 15:52:59 -07003344 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3345 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003346}
Adam Langley95c29f32014-06-20 12:00:00 -07003347
Steven Valdez908ac192017-01-12 13:17:07 -05003348int tls1_verify_channel_id(SSL_HANDSHAKE *hs) {
3349 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003350 int ret = 0;
3351 uint16_t extension_type;
3352 CBS extension, channel_id;
3353
3354 /* A Channel ID handshake message is structured to contain multiple
3355 * extensions, but the only one that can be present is Channel ID. */
3356 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3357 if (!CBS_get_u16(&channel_id, &extension_type) ||
3358 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3359 CBS_len(&channel_id) != 0 ||
3360 extension_type != TLSEXT_TYPE_channel_id ||
3361 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3362 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3363 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3364 return 0;
3365 }
3366
3367 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3368 if (!p256) {
3369 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3370 return 0;
3371 }
3372
3373 EC_KEY *key = NULL;
3374 EC_POINT *point = NULL;
3375 BIGNUM x, y;
3376 ECDSA_SIG sig;
3377 BN_init(&x);
3378 BN_init(&y);
3379 sig.r = BN_new();
3380 sig.s = BN_new();
3381 if (sig.r == NULL || sig.s == NULL) {
3382 goto err;
3383 }
3384
3385 const uint8_t *p = CBS_data(&extension);
3386 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3387 BN_bin2bn(p + 32, 32, &y) == NULL ||
3388 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3389 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3390 goto err;
3391 }
3392
3393 point = EC_POINT_new(p256);
3394 if (point == NULL ||
3395 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3396 goto err;
3397 }
3398
3399 key = EC_KEY_new();
3400 if (key == NULL ||
3401 !EC_KEY_set_group(key, p256) ||
3402 !EC_KEY_set_public_key(key, point)) {
3403 goto err;
3404 }
3405
3406 uint8_t digest[EVP_MAX_MD_SIZE];
3407 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003408 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003409 goto err;
3410 }
3411
3412 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3413#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3414 sig_ok = 1;
3415#endif
3416 if (!sig_ok) {
3417 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3418 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3419 ssl->s3->tlsext_channel_id_valid = 0;
3420 goto err;
3421 }
3422
David Benjamin17cf2cb2016-12-13 01:07:13 -05003423 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
Nick Harper60a85cb2016-09-23 16:25:11 -07003424 ret = 1;
3425
3426err:
3427 BN_free(&x);
3428 BN_free(&y);
3429 BN_free(sig.r);
3430 BN_free(sig.s);
3431 EC_KEY_free(key);
3432 EC_POINT_free(point);
3433 EC_GROUP_free(p256);
3434 return ret;
3435}
3436
Steven Valdez908ac192017-01-12 13:17:07 -05003437int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3438 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003439 uint8_t digest[EVP_MAX_MD_SIZE];
3440 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003441 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003442 return 0;
3443 }
3444
3445 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3446 if (ec_key == NULL) {
3447 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3448 return 0;
3449 }
3450
3451 int ret = 0;
3452 BIGNUM *x = BN_new();
3453 BIGNUM *y = BN_new();
3454 ECDSA_SIG *sig = NULL;
3455 if (x == NULL || y == NULL ||
3456 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3457 EC_KEY_get0_public_key(ec_key),
3458 x, y, NULL)) {
3459 goto err;
3460 }
3461
3462 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3463 if (sig == NULL) {
3464 goto err;
3465 }
3466
3467 CBB child;
3468 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3469 !CBB_add_u16_length_prefixed(cbb, &child) ||
3470 !BN_bn2cbb_padded(&child, 32, x) ||
3471 !BN_bn2cbb_padded(&child, 32, y) ||
3472 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3473 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3474 !CBB_flush(cbb)) {
3475 goto err;
3476 }
3477
3478 ret = 1;
3479
3480err:
3481 BN_free(x);
3482 BN_free(y);
3483 ECDSA_SIG_free(sig);
3484 return ret;
3485}
3486
Steven Valdez908ac192017-01-12 13:17:07 -05003487int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3488 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003489 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3490 uint8_t *msg;
3491 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003492 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003493 ssl_cert_verify_channel_id)) {
3494 return 0;
3495 }
3496 SHA256(msg, msg_len, out);
3497 *out_len = SHA256_DIGEST_LENGTH;
3498 OPENSSL_free(msg);
3499 return 1;
3500 }
3501
Nick Harper95594012016-10-20 14:07:13 -07003502 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003503
Nick Harper95594012016-10-20 14:07:13 -07003504 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003505 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003506 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003507
Steven Valdez87eab492016-06-27 16:34:59 -04003508 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003509 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003510 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003511 if (ssl->session->original_handshake_hash_len == 0) {
3512 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003513 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003514 }
Nick Harper95594012016-10-20 14:07:13 -07003515 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3516 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003517 }
3518
Steven Valdez908ac192017-01-12 13:17:07 -05003519 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3520 size_t hs_hash_len;
3521 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript, hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003522 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003523 }
Steven Valdez908ac192017-01-12 13:17:07 -05003524 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003525 SHA256_Final(out, &ctx);
3526 *out_len = SHA256_DIGEST_LENGTH;
3527 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003528}
Adam Langley1258b6a2014-06-20 12:00:00 -07003529
3530/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin45738dd2017-02-09 20:01:26 -05003531 * hashes in |hs->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003532 * data. */
Steven Valdez908ac192017-01-12 13:17:07 -05003533int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3534 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003535 /* This function should never be called for a resumed session because the
3536 * handshake hashes that we wish to record are for the original, full
3537 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003538 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003539 return -1;
3540 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003541
Steven Valdez908ac192017-01-12 13:17:07 -05003542 OPENSSL_COMPILE_ASSERT(
David Benjamin45738dd2017-02-09 20:01:26 -05003543 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
Steven Valdez908ac192017-01-12 13:17:07 -05003544 original_handshake_hash_is_too_small);
3545
3546 size_t digest_len;
3547 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript,
David Benjamin45738dd2017-02-09 20:01:26 -05003548 hs->new_session->original_handshake_hash,
Steven Valdez908ac192017-01-12 13:17:07 -05003549 &digest_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003550 return -1;
3551 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003552
Steven Valdez908ac192017-01-12 13:17:07 -05003553 OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE <= 0xff, max_md_size_is_too_large);
David Benjamin45738dd2017-02-09 20:01:26 -05003554 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003555
Adam Langleyfcf25832014-12-18 17:42:32 -08003556 return 1;
3557}
Nick Harper60a85cb2016-09-23 16:25:11 -07003558
3559int ssl_do_channel_id_callback(SSL *ssl) {
3560 if (ssl->tlsext_channel_id_private != NULL ||
3561 ssl->ctx->channel_id_cb == NULL) {
3562 return 1;
3563 }
3564
3565 EVP_PKEY *key = NULL;
3566 ssl->ctx->channel_id_cb(ssl, &key);
3567 if (key == NULL) {
3568 /* The caller should try again later. */
3569 return 1;
3570 }
3571
3572 int ret = SSL_set1_tls_channel_id(ssl, key);
3573 EVP_PKEY_free(key);
3574 return ret;
3575}
Adam Langleycfa08c32016-11-17 13:21:27 -08003576
3577int ssl_is_sct_list_valid(const CBS *contents) {
3578 /* Shallow parse the SCT list for sanity. By the RFC
3579 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3580 * of the SCTs may be empty. */
3581 CBS copy = *contents;
3582 CBS sct_list;
3583 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3584 CBS_len(&copy) != 0 ||
3585 CBS_len(&sct_list) == 0) {
3586 return 0;
3587 }
3588
3589 while (CBS_len(&sct_list) > 0) {
3590 CBS sct;
3591 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3592 CBS_len(&sct) == 0) {
3593 return 0;
3594 }
3595 }
3596
3597 return 1;
3598}