blob: e47937d6b051cb47d9f36fc25f13a36526a3b602 [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 Benjamin3a322f52016-10-26 12:45:35 -0400449 /* Prefer SHA-256 algorithms. */
450 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400451#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400452 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400453#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400454 SSL_SIGN_RSA_PKCS1_SHA256,
455
456 /* Larger hashes are acceptable. */
457 SSL_SIGN_ECDSA_SECP384R1_SHA384,
458#if !defined(BORINGSSL_ANDROID_SYSTEM)
459 SSL_SIGN_RSA_PSS_SHA384,
460#endif
461 SSL_SIGN_RSA_PKCS1_SHA384,
462
David Benjamin76bb1412016-09-08 16:03:49 -0400463 /* TODO(davidben): Remove this. */
David Benjamin3ef76972016-10-17 17:59:54 -0400464#if defined(BORINGSSL_ANDROID_SYSTEM)
465 SSL_SIGN_ECDSA_SECP521R1_SHA512,
466#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400467#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400468 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400469#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400470 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400471
David Benjamin3a322f52016-10-26 12:45:35 -0400472 /* For now, SHA-1 is still accepted but least preferable. */
473 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400474
David Benjamin3a322f52016-10-26 12:45:35 -0400475};
476
477/* kSignSignatureAlgorithms is the default list of supported signature
478 * algorithms for signing.
479 *
480 * For now, RSA-PSS signature algorithms are not enabled on Android's system
481 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
482 * restore them. */
483static const uint16_t kSignSignatureAlgorithms[] = {
484 /* Prefer SHA-256 algorithms. */
485 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400486#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400487 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400488#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400489 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400490
David Benjamin3a322f52016-10-26 12:45:35 -0400491 /* If needed, sign larger hashes.
492 *
493 * TODO(davidben): Determine which of these may be pruned. */
494 SSL_SIGN_ECDSA_SECP384R1_SHA384,
495#if !defined(BORINGSSL_ANDROID_SYSTEM)
496 SSL_SIGN_RSA_PSS_SHA384,
497#endif
498 SSL_SIGN_RSA_PKCS1_SHA384,
499
500 SSL_SIGN_ECDSA_SECP521R1_SHA512,
501#if !defined(BORINGSSL_ANDROID_SYSTEM)
502 SSL_SIGN_RSA_PSS_SHA512,
503#endif
504 SSL_SIGN_RSA_PKCS1_SHA512,
505
506 /* If the peer supports nothing else, sign with SHA-1. */
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400507 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400508 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400509};
510
David Benjamin3ef76972016-10-17 17:59:54 -0400511size_t tls12_get_verify_sigalgs(const SSL *ssl, const uint16_t **out) {
512 *out = kVerifySignatureAlgorithms;
513 return OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
Adam Langleyfcf25832014-12-18 17:42:32 -0800514}
Adam Langley95c29f32014-06-20 12:00:00 -0700515
David Benjamin887c3002016-07-08 16:15:32 -0700516int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
David Benjamin3ef76972016-10-17 17:59:54 -0400517 const uint16_t *verify_sigalgs;
518 size_t num_verify_sigalgs = tls12_get_verify_sigalgs(ssl, &verify_sigalgs);
519 for (size_t i = 0; i < num_verify_sigalgs; i++) {
520 if (sigalg == verify_sigalgs[i]) {
521 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 }
523 }
524
David Benjamin3ef76972016-10-17 17:59:54 -0400525 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
526 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
527 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800528}
529
Adam Langley614c66a2015-06-12 15:26:58 -0700530/* tls_extension represents a TLS extension that is handled internally. The
531 * |init| function is called for each handshake, before any other functions of
532 * the extension. Then the add and parse callbacks are called as needed.
533 *
534 * The parse callbacks receive a |CBS| that contains the contents of the
535 * extension (i.e. not including the type and length bytes). If an extension is
536 * not received then the parse callbacks will be called with a NULL CBS so that
537 * they can do any processing needed to handle the absence of an extension.
538 *
539 * The add callbacks receive a |CBB| to which the extension can be appended but
540 * the function is responsible for appending the type and length bytes too.
541 *
542 * All callbacks return one for success and zero for error. If a parse function
543 * returns zero then a fatal alert with value |*out_alert| will be sent. If
544 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
545struct tls_extension {
546 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500547 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700548
David Benjamin8c880a22016-12-03 02:20:34 -0500549 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
550 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
551 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700552
David Benjamin8c880a22016-12-03 02:20:34 -0500553 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
554 CBS *contents);
555 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700556};
557
David Benjamin8c880a22016-12-03 02:20:34 -0500558static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
559 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400560 if (contents != NULL) {
561 /* Servers MUST NOT send this extension. */
562 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
563 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
564 return 0;
565 }
566
567 return 1;
568}
569
David Benjamin8c880a22016-12-03 02:20:34 -0500570static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
571 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400572 /* This extension from the client is handled elsewhere. */
573 return 1;
574}
575
David Benjamin8c880a22016-12-03 02:20:34 -0500576static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400577 return 1;
578}
Adam Langley614c66a2015-06-12 15:26:58 -0700579
580/* Server name indication (SNI).
581 *
582 * https://tools.ietf.org/html/rfc6066#section-3. */
583
David Benjamin8c880a22016-12-03 02:20:34 -0500584static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
585 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700586 if (ssl->tlsext_hostname == NULL) {
587 return 1;
588 }
589
590 CBB contents, server_name_list, name;
591 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
592 !CBB_add_u16_length_prefixed(out, &contents) ||
593 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
594 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
595 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
596 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
597 strlen(ssl->tlsext_hostname)) ||
598 !CBB_flush(out)) {
599 return 0;
600 }
601
602 return 1;
603}
604
David Benjamin8c880a22016-12-03 02:20:34 -0500605static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500606 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500607 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700608 if (contents == NULL) {
609 return 1;
610 }
611
612 if (CBS_len(contents) != 0) {
613 return 0;
614 }
615
616 assert(ssl->tlsext_hostname != NULL);
617
Steven Valdez87eab492016-06-27 16:34:59 -0400618 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500619 OPENSSL_free(hs->new_session->tlsext_hostname);
620 hs->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
621 if (!hs->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700622 *out_alert = SSL_AD_INTERNAL_ERROR;
623 return 0;
624 }
625 }
626
627 return 1;
628}
629
David Benjamin8c880a22016-12-03 02:20:34 -0500630static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500631 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700632 if (contents == NULL) {
633 return 1;
634 }
635
David Benjamin9b611e22016-03-03 08:48:30 -0500636 CBS server_name_list, host_name;
637 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700638 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500639 !CBS_get_u8(&server_name_list, &name_type) ||
640 /* Although the server_name extension was intended to be extensible to
641 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
642 * different name types will cause an error. Further, RFC 4366 originally
643 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
644 * adding new name types is no longer feasible.
645 *
646 * Act as if the extensibility does not exist to simplify parsing. */
647 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
648 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700649 CBS_len(contents) != 0) {
650 return 0;
651 }
652
David Benjamin9b611e22016-03-03 08:48:30 -0500653 if (name_type != TLSEXT_NAMETYPE_host_name ||
654 CBS_len(&host_name) == 0 ||
655 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
656 CBS_contains_zero_byte(&host_name)) {
657 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
658 return 0;
659 }
Adam Langley614c66a2015-06-12 15:26:58 -0700660
David Benjamin4eb95cc2016-11-16 17:08:23 +0900661 /* Copy the hostname as a string. */
David Benjamin8c880a22016-12-03 02:20:34 -0500662 if (!CBS_strdup(&host_name, &hs->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900663 *out_alert = SSL_AD_INTERNAL_ERROR;
664 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700665 }
666
David Benjamin8c880a22016-12-03 02:20:34 -0500667 hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700668 return 1;
669}
670
David Benjamin8c880a22016-12-03 02:20:34 -0500671static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
672 if (hs->ssl->s3->session_reused ||
673 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700674 return 1;
675 }
676
677 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
678 !CBB_add_u16(out, 0 /* length */)) {
679 return 0;
680 }
681
682 return 1;
683}
684
685
Adam Langley5021b222015-06-12 18:27:58 -0700686/* Renegotiation indication.
687 *
688 * https://tools.ietf.org/html/rfc5746 */
689
David Benjamin8c880a22016-12-03 02:20:34 -0500690static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
691 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400692 uint16_t min_version, max_version;
693 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
694 return 0;
695 }
696
697 /* Renegotiation indication is not necessary in TLS 1.3. */
698 if (min_version >= TLS1_3_VERSION) {
699 return 1;
700 }
701
David Benjamin52bf6902016-10-08 12:05:03 -0400702 assert(ssl->s3->initial_handshake_complete ==
703 (ssl->s3->previous_client_finished_len != 0));
704
Adam Langley5021b222015-06-12 18:27:58 -0700705 CBB contents, prev_finished;
706 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
707 !CBB_add_u16_length_prefixed(out, &contents) ||
708 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
709 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
710 ssl->s3->previous_client_finished_len) ||
711 !CBB_flush(out)) {
712 return 0;
713 }
714
715 return 1;
716}
717
David Benjamin8c880a22016-12-03 02:20:34 -0500718static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700719 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500720 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400721 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez246eeee2017-03-26 12:49:17 -0500722 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400723 return 0;
724 }
725
David Benjamin3e052de2015-11-25 20:10:31 -0500726 /* Servers may not switch between omitting the extension and supporting it.
727 * See RFC 5746, sections 3.5 and 4.2. */
728 if (ssl->s3->initial_handshake_complete &&
729 (contents != NULL) != ssl->s3->send_connection_binding) {
730 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
731 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
732 return 0;
733 }
734
Adam Langley5021b222015-06-12 18:27:58 -0700735 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500736 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700737 * RI even on initial ServerHello because the client doesn't see any
738 * renegotiation during an attack. However this would mean we could not
739 * connect to any server which doesn't support RI.
740 *
David Benjamine9cddb82015-11-23 14:36:40 -0500741 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
742 * practical terms every client sets it so it's just assumed here. */
743 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700744 }
745
746 const size_t expected_len = ssl->s3->previous_client_finished_len +
747 ssl->s3->previous_server_finished_len;
748
749 /* Check for logic errors */
750 assert(!expected_len || ssl->s3->previous_client_finished_len);
751 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400752 assert(ssl->s3->initial_handshake_complete ==
753 (ssl->s3->previous_client_finished_len != 0));
754 assert(ssl->s3->initial_handshake_complete ==
755 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700756
757 /* Parse out the extension contents. */
758 CBS renegotiated_connection;
759 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
760 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400761 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700762 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
763 return 0;
764 }
765
766 /* Check that the extension matches. */
767 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400768 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700769 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
770 return 0;
771 }
772
773 const uint8_t *d = CBS_data(&renegotiated_connection);
774 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
775 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400776 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700777 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
778 return 0;
779 }
780 d += ssl->s3->previous_client_finished_len;
781
782 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
783 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400784 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700785 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
786 return 0;
787 }
788 ssl->s3->send_connection_binding = 1;
789
790 return 1;
791}
792
David Benjamin8c880a22016-12-03 02:20:34 -0500793static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700794 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500795 SSL *const ssl = hs->ssl;
Adam Langley5021b222015-06-12 18:27:58 -0700796 /* Renegotiation isn't supported as a server so this function should never be
797 * called after the initial handshake. */
798 assert(!ssl->s3->initial_handshake_complete);
799
Steven Valdez143e8b32016-07-11 13:19:03 -0400800 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
801 return 1;
802 }
803
Adam Langley5021b222015-06-12 18:27:58 -0700804 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400805 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700806 }
807
808 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700809 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
810 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400811 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700812 return 0;
813 }
814
David Benjamin52bf6902016-10-08 12:05:03 -0400815 /* Check that the extension matches. We do not support renegotiation as a
816 * server, so this must be empty. */
817 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400818 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700819 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
820 return 0;
821 }
822
823 ssl->s3->send_connection_binding = 1;
824
825 return 1;
826}
827
David Benjamin8c880a22016-12-03 02:20:34 -0500828static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
829 SSL *const ssl = hs->ssl;
David Benjamin52bf6902016-10-08 12:05:03 -0400830 /* Renegotiation isn't supported as a server so this function should never be
831 * called after the initial handshake. */
832 assert(!ssl->s3->initial_handshake_complete);
833
Steven Valdez143e8b32016-07-11 13:19:03 -0400834 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
835 return 1;
836 }
837
Adam Langley5021b222015-06-12 18:27:58 -0700838 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400839 !CBB_add_u16(out, 1 /* length */) ||
840 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700841 return 0;
842 }
843
844 return 1;
845}
846
Adam Langley0a056712015-07-01 15:03:33 -0700847
848/* Extended Master Secret.
849 *
David Benjamin43946d42016-02-01 08:42:19 -0500850 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700851
David Benjamin8c880a22016-12-03 02:20:34 -0500852static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400853 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -0500854 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400855 return 0;
856 }
857
858 /* Extended master secret is not necessary in TLS 1.3. */
859 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700860 return 1;
861 }
862
863 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
864 !CBB_add_u16(out, 0 /* length */)) {
865 return 0;
866 }
867
868 return 1;
869}
870
David Benjamin8c880a22016-12-03 02:20:34 -0500871static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700872 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500873 SSL *const ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500874
875 if (contents != NULL) {
876 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
877 ssl->version == SSL3_VERSION ||
878 CBS_len(contents) != 0) {
David Benjamin163c9562016-08-29 23:14:17 -0400879 return 0;
880 }
881
David Benjaminfc02b592017-02-17 16:26:01 -0500882 hs->extended_master_secret = 1;
David Benjamin163c9562016-08-29 23:14:17 -0400883 }
884
David Benjaminfc02b592017-02-17 16:26:01 -0500885 /* Whether EMS is negotiated may not change on renegotiation. */
886 if (ssl->s3->established_session != NULL &&
887 hs->extended_master_secret !=
888 ssl->s3->established_session->extended_master_secret) {
889 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
890 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400891 return 0;
892 }
893
Adam Langley0a056712015-07-01 15:03:33 -0700894 return 1;
895}
896
David Benjamin8c880a22016-12-03 02:20:34 -0500897static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500898 CBS *contents) {
David Benjaminfc02b592017-02-17 16:26:01 -0500899 uint16_t version = ssl3_protocol_version(hs->ssl);
David Benjamin8c880a22016-12-03 02:20:34 -0500900 if (version >= TLS1_3_VERSION ||
901 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400902 return 1;
903 }
904
905 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700906 return 1;
907 }
908
909 if (CBS_len(contents) != 0) {
910 return 0;
911 }
912
David Benjaminfc02b592017-02-17 16:26:01 -0500913 hs->extended_master_secret = 1;
Adam Langley0a056712015-07-01 15:03:33 -0700914 return 1;
915}
916
David Benjamin8c880a22016-12-03 02:20:34 -0500917static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -0500918 if (!hs->extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700919 return 1;
920 }
921
922 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
923 !CBB_add_u16(out, 0 /* length */)) {
924 return 0;
925 }
926
927 return 1;
928}
929
Adam Langley9b05bc52015-07-01 15:25:33 -0700930
931/* Session tickets.
932 *
933 * https://tools.ietf.org/html/rfc5077 */
934
David Benjamin8c880a22016-12-03 02:20:34 -0500935static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
936 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400937 uint16_t min_version, max_version;
938 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
939 return 0;
940 }
941
942 /* TLS 1.3 uses a different ticket extension. */
943 if (min_version >= TLS1_3_VERSION ||
944 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700945 return 1;
946 }
947
948 const uint8_t *ticket_data = NULL;
949 int ticket_len = 0;
950
951 /* Renegotiation does not participate in session resumption. However, still
952 * advertise the extension to avoid potentially breaking servers which carry
953 * over the state from the previous handshake, such as OpenSSL servers
954 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -0400955 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -0700956 if (!ssl->s3->initial_handshake_complete &&
957 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400958 ssl->session->tlsext_tick != NULL &&
959 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -0400960 ssl->method->version_from_wire(&session_version,
961 ssl->session->ssl_version) &&
962 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700963 ticket_data = ssl->session->tlsext_tick;
964 ticket_len = ssl->session->tlsext_ticklen;
965 }
966
967 CBB ticket;
968 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
969 !CBB_add_u16_length_prefixed(out, &ticket) ||
970 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
971 !CBB_flush(out)) {
972 return 0;
973 }
974
975 return 1;
976}
977
David Benjamin8c880a22016-12-03 02:20:34 -0500978static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -0700979 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500980 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -0700981 if (contents == NULL) {
982 return 1;
983 }
984
Steven Valdez143e8b32016-07-11 13:19:03 -0400985 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
986 return 0;
987 }
988
Adam Langley9b05bc52015-07-01 15:25:33 -0700989 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
990 * this function should never be called, even if the server tries to send the
991 * extension. */
992 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
993
994 if (CBS_len(contents) != 0) {
995 return 0;
996 }
997
David Benjamin8c880a22016-12-03 02:20:34 -0500998 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -0700999 return 1;
1000}
1001
David Benjamin8c880a22016-12-03 02:20:34 -05001002static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1003 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001004 return 1;
1005 }
1006
David Benjamin78476f62016-11-12 11:20:55 +09001007 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin8c880a22016-12-03 02:20:34 -05001008 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001009
1010 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1011 !CBB_add_u16(out, 0 /* length */)) {
1012 return 0;
1013 }
1014
1015 return 1;
1016}
1017
1018
Adam Langley2e857bd2015-07-01 16:09:19 -07001019/* Signature Algorithms.
1020 *
1021 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1022
David Benjamin8c880a22016-12-03 02:20:34 -05001023static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1024 SSL *const ssl = hs->ssl;
David Benjamin2dc02042016-09-19 19:57:37 -04001025 uint16_t min_version, max_version;
1026 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1027 return 0;
1028 }
1029
1030 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001031 return 1;
1032 }
1033
David Benjamin0fc37ef2016-08-17 15:29:46 -04001034 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001035 const size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001036
David Benjamin0fc37ef2016-08-17 15:29:46 -04001037 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001038 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1039 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001040 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001041 return 0;
1042 }
1043
David Benjamin0fc37ef2016-08-17 15:29:46 -04001044 for (size_t i = 0; i < num_sigalgs; i++) {
1045 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001046 return 0;
1047 }
1048 }
1049
1050 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001051 return 0;
1052 }
1053
1054 return 1;
1055}
1056
David Benjamin8c880a22016-12-03 02:20:34 -05001057static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001058 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001059 OPENSSL_free(hs->peer_sigalgs);
1060 hs->peer_sigalgs = NULL;
1061 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001062
Adam Langley2e857bd2015-07-01 16:09:19 -07001063 if (contents == NULL) {
1064 return 1;
1065 }
1066
1067 CBS supported_signature_algorithms;
1068 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001069 CBS_len(contents) != 0 ||
1070 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001071 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001072 return 0;
1073 }
1074
1075 return 1;
1076}
1077
Adam Langley2e857bd2015-07-01 16:09:19 -07001078
Adam Langleybb0bd042015-07-01 16:21:03 -07001079/* OCSP Stapling.
1080 *
1081 * https://tools.ietf.org/html/rfc6066#section-8 */
1082
David Benjamin8c880a22016-12-03 02:20:34 -05001083static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1084 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001085 if (!ssl->ocsp_stapling_enabled) {
1086 return 1;
1087 }
1088
1089 CBB contents;
1090 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1091 !CBB_add_u16_length_prefixed(out, &contents) ||
1092 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1093 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1094 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1095 !CBB_flush(out)) {
1096 return 0;
1097 }
1098
1099 return 1;
1100}
1101
David Benjamin8c880a22016-12-03 02:20:34 -05001102static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001103 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001104 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001105 if (contents == NULL) {
1106 return 1;
1107 }
1108
Steven Valdeza833c352016-11-01 13:39:36 -04001109 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1110 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001111 return 0;
1112 }
1113
Steven Valdeza833c352016-11-01 13:39:36 -04001114 /* OCSP stapling is forbidden on non-certificate ciphers. */
1115 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001116 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001117 return 0;
1118 }
1119
Steven Valdeza833c352016-11-01 13:39:36 -04001120 /* Note this does not check for resumption in TLS 1.2. Sending
1121 * status_request here does not make sense, but OpenSSL does so and the
1122 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001123
David Benjamin8c880a22016-12-03 02:20:34 -05001124 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001125 return 1;
1126}
1127
David Benjamin8c880a22016-12-03 02:20:34 -05001128static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001129 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001130 if (contents == NULL) {
1131 return 1;
1132 }
1133
1134 uint8_t status_type;
1135 if (!CBS_get_u8(contents, &status_type)) {
1136 return 0;
1137 }
1138
1139 /* We cannot decide whether OCSP stapling will occur yet because the correct
1140 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001141 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001142
Adam Langleybb0bd042015-07-01 16:21:03 -07001143 return 1;
1144}
1145
David Benjamin8c880a22016-12-03 02:20:34 -05001146static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1147 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001148 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001149 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001150 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001151 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001152 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001153 return 1;
1154 }
1155
David Benjamin8c880a22016-12-03 02:20:34 -05001156 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001157
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001158 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001159 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001160}
1161
1162
Adam Langley97dfcbf2015-07-01 18:35:20 -07001163/* Next protocol negotiation.
1164 *
1165 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1166
David Benjamin8c880a22016-12-03 02:20:34 -05001167static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1168 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001169 if (ssl->s3->initial_handshake_complete ||
1170 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001171 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001172 return 1;
1173 }
1174
1175 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1176 !CBB_add_u16(out, 0 /* length */)) {
1177 return 0;
1178 }
1179
1180 return 1;
1181}
1182
David Benjamin8c880a22016-12-03 02:20:34 -05001183static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001184 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001185 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001186 if (contents == NULL) {
1187 return 1;
1188 }
1189
Steven Valdez143e8b32016-07-11 13:19:03 -04001190 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1191 return 0;
1192 }
1193
Adam Langley97dfcbf2015-07-01 18:35:20 -07001194 /* If any of these are false then we should never have sent the NPN
1195 * extension in the ClientHello and thus this function should never have been
1196 * called. */
1197 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001198 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001199 assert(ssl->ctx->next_proto_select_cb != NULL);
1200
David Benjamin76c2efc2015-08-31 14:24:29 -04001201 if (ssl->s3->alpn_selected != NULL) {
1202 /* NPN and ALPN may not be negotiated in the same connection. */
1203 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1204 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1205 return 0;
1206 }
1207
Adam Langley97dfcbf2015-07-01 18:35:20 -07001208 const uint8_t *const orig_contents = CBS_data(contents);
1209 const size_t orig_len = CBS_len(contents);
1210
1211 while (CBS_len(contents) != 0) {
1212 CBS proto;
1213 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1214 CBS_len(&proto) == 0) {
1215 return 0;
1216 }
1217 }
1218
1219 uint8_t *selected;
1220 uint8_t selected_len;
1221 if (ssl->ctx->next_proto_select_cb(
1222 ssl, &selected, &selected_len, orig_contents, orig_len,
1223 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1224 *out_alert = SSL_AD_INTERNAL_ERROR;
1225 return 0;
1226 }
1227
David Benjamin79978df2015-12-25 15:56:49 -05001228 OPENSSL_free(ssl->s3->next_proto_negotiated);
1229 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1230 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001231 *out_alert = SSL_AD_INTERNAL_ERROR;
1232 return 0;
1233 }
1234
David Benjamin79978df2015-12-25 15:56:49 -05001235 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001236 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001237
1238 return 1;
1239}
1240
David Benjamin8c880a22016-12-03 02:20:34 -05001241static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001242 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001243 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001244 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1245 return 1;
1246 }
1247
Adam Langley97dfcbf2015-07-01 18:35:20 -07001248 if (contents != NULL && CBS_len(contents) != 0) {
1249 return 0;
1250 }
1251
1252 if (contents == NULL ||
1253 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001254 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001255 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001256 return 1;
1257 }
1258
David Benjamin8c880a22016-12-03 02:20:34 -05001259 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001260 return 1;
1261}
1262
David Benjamin8c880a22016-12-03 02:20:34 -05001263static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1264 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001265 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1266 * parsed. */
David Benjamin8c880a22016-12-03 02:20:34 -05001267 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001268 return 1;
1269 }
1270
1271 const uint8_t *npa;
1272 unsigned npa_len;
1273
1274 if (ssl->ctx->next_protos_advertised_cb(
1275 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1276 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001277 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001278 return 1;
1279 }
1280
1281 CBB contents;
1282 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1283 !CBB_add_u16_length_prefixed(out, &contents) ||
1284 !CBB_add_bytes(&contents, npa, npa_len) ||
1285 !CBB_flush(out)) {
1286 return 0;
1287 }
1288
1289 return 1;
1290}
1291
1292
Adam Langleyab8d87d2015-07-10 12:21:39 -07001293/* Signed certificate timestamps.
1294 *
1295 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1296
David Benjamin8c880a22016-12-03 02:20:34 -05001297static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1298 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001299 if (!ssl->signed_cert_timestamps_enabled) {
1300 return 1;
1301 }
1302
1303 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1304 !CBB_add_u16(out, 0 /* length */)) {
1305 return 0;
1306 }
1307
1308 return 1;
1309}
1310
David Benjamin8c880a22016-12-03 02:20:34 -05001311static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001312 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001313 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001314 if (contents == NULL) {
1315 return 1;
1316 }
1317
Steven Valdeza833c352016-11-01 13:39:36 -04001318 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1319 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001320 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001321 return 0;
1322 }
1323
Adam Langleyab8d87d2015-07-10 12:21:39 -07001324 /* If this is false then we should never have sent the SCT extension in the
1325 * ClientHello and thus this function should never have been called. */
1326 assert(ssl->signed_cert_timestamps_enabled);
1327
Adam Langleycfa08c32016-11-17 13:21:27 -08001328 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001329 *out_alert = SSL_AD_DECODE_ERROR;
1330 return 0;
1331 }
1332
David Benjamindaa88502016-10-04 16:32:16 -04001333 /* Session resumption uses the original session information. The extension
1334 * should not be sent on resumption, but RFC 6962 did not make it a
1335 * requirement, so tolerate this.
1336 *
1337 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001338 if (!ssl->s3->session_reused &&
David Benjamin45738dd2017-02-09 20:01:26 -05001339 !CBS_stow(contents, &hs->new_session->tlsext_signed_cert_timestamp_list,
1340 &hs->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001341 *out_alert = SSL_AD_INTERNAL_ERROR;
1342 return 0;
1343 }
1344
1345 return 1;
1346}
1347
David Benjamin8c880a22016-12-03 02:20:34 -05001348static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001349 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001350 if (contents == NULL) {
1351 return 1;
1352 }
1353
1354 if (CBS_len(contents) != 0) {
1355 return 0;
1356 }
1357
David Benjamin8c880a22016-12-03 02:20:34 -05001358 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001359 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001360}
1361
David Benjamin8c880a22016-12-03 02:20:34 -05001362static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1363 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001364 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001365 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1366 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001367 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001368 return 1;
1369 }
1370
1371 CBB contents;
1372 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1373 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001374 CBB_add_bytes(
1375 &contents,
1376 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1377 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001378 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001379}
1380
1381
Adam Langleyf18e4532015-07-10 13:39:53 -07001382/* Application-level Protocol Negotiation.
1383 *
1384 * https://tools.ietf.org/html/rfc7301 */
1385
David Benjamin8c880a22016-12-03 02:20:34 -05001386static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1387 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001388 if (ssl->alpn_client_proto_list == NULL ||
1389 ssl->s3->initial_handshake_complete) {
1390 return 1;
1391 }
1392
1393 CBB contents, proto_list;
1394 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1395 !CBB_add_u16_length_prefixed(out, &contents) ||
1396 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1397 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1398 ssl->alpn_client_proto_list_len) ||
1399 !CBB_flush(out)) {
1400 return 0;
1401 }
1402
1403 return 1;
1404}
1405
David Benjamin8c880a22016-12-03 02:20:34 -05001406static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001407 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001408 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001409 if (contents == NULL) {
1410 return 1;
1411 }
1412
1413 assert(!ssl->s3->initial_handshake_complete);
1414 assert(ssl->alpn_client_proto_list != NULL);
1415
David Benjamin8c880a22016-12-03 02:20:34 -05001416 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001417 /* NPN and ALPN may not be negotiated in the same connection. */
1418 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1419 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1420 return 0;
1421 }
1422
Adam Langleyf18e4532015-07-10 13:39:53 -07001423 /* The extension data consists of a ProtocolNameList which must have
1424 * exactly one ProtocolName. Each of these is length-prefixed. */
1425 CBS protocol_name_list, protocol_name;
1426 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1427 CBS_len(contents) != 0 ||
1428 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1429 /* Empty protocol names are forbidden. */
1430 CBS_len(&protocol_name) == 0 ||
1431 CBS_len(&protocol_name_list) != 0) {
1432 return 0;
1433 }
1434
David Benjaminc8ff30c2017-04-04 13:52:36 -04001435 if (!ssl->ctx->allow_unknown_alpn_protos) {
1436 /* Check that the protocol name is one of the ones we advertised. */
1437 int protocol_ok = 0;
1438 CBS client_protocol_name_list, client_protocol_name;
1439 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1440 ssl->alpn_client_proto_list_len);
1441 while (CBS_len(&client_protocol_name_list) > 0) {
1442 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1443 &client_protocol_name)) {
1444 *out_alert = SSL_AD_INTERNAL_ERROR;
1445 return 0;
1446 }
1447
1448 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1449 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1450 CBS_data(&protocol_name),
1451 CBS_len(&protocol_name)) == 0) {
1452 protocol_ok = 1;
1453 break;
1454 }
1455 }
1456
1457 if (!protocol_ok) {
1458 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1459 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3e517572016-08-11 11:52:23 -04001460 return 0;
1461 }
David Benjamin3e517572016-08-11 11:52:23 -04001462 }
1463
Adam Langleyf18e4532015-07-10 13:39:53 -07001464 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1465 &ssl->s3->alpn_selected_len)) {
1466 *out_alert = SSL_AD_INTERNAL_ERROR;
1467 return 0;
1468 }
1469
1470 return 1;
1471}
1472
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001473int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001474 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001475 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001476 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001477 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001478 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001479 client_hello, &contents,
1480 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1481 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001482 return 1;
1483 }
1484
1485 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001486 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001487
1488 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001489 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1490 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001491 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001492 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1493 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001494 return 0;
1495 }
1496
1497 /* Validate the protocol list. */
1498 CBS protocol_name_list_copy = protocol_name_list;
1499 while (CBS_len(&protocol_name_list_copy) > 0) {
1500 CBS protocol_name;
1501
1502 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1503 /* Empty protocol names are forbidden. */
1504 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001505 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1506 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001507 return 0;
1508 }
1509 }
1510
1511 const uint8_t *selected;
1512 uint8_t selected_len;
1513 if (ssl->ctx->alpn_select_cb(
1514 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1515 CBS_len(&protocol_name_list),
1516 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1517 OPENSSL_free(ssl->s3->alpn_selected);
1518 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1519 if (ssl->s3->alpn_selected == NULL) {
1520 *out_alert = SSL_AD_INTERNAL_ERROR;
1521 return 0;
1522 }
1523 ssl->s3->alpn_selected_len = selected_len;
1524 }
1525
1526 return 1;
1527}
1528
David Benjamin8c880a22016-12-03 02:20:34 -05001529static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1530 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001531 if (ssl->s3->alpn_selected == NULL) {
1532 return 1;
1533 }
1534
1535 CBB contents, proto_list, proto;
1536 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1537 !CBB_add_u16_length_prefixed(out, &contents) ||
1538 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1539 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001540 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1541 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001542 !CBB_flush(out)) {
1543 return 0;
1544 }
1545
1546 return 1;
1547}
1548
1549
Adam Langley49c7af12015-07-10 14:33:46 -07001550/* Channel ID.
1551 *
1552 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1553
David Benjamin8c880a22016-12-03 02:20:34 -05001554static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1555 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001556}
1557
David Benjamin8c880a22016-12-03 02:20:34 -05001558static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1559 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001560 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001561 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001562 return 1;
1563 }
1564
1565 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1566 !CBB_add_u16(out, 0 /* length */)) {
1567 return 0;
1568 }
1569
1570 return 1;
1571}
1572
David Benjamin8c880a22016-12-03 02:20:34 -05001573static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1574 uint8_t *out_alert, CBS *contents) {
1575 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001576 if (contents == NULL) {
1577 return 1;
1578 }
1579
David Benjamince079fd2016-08-02 16:22:34 -04001580 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001581 assert(ssl->tlsext_channel_id_enabled);
1582
1583 if (CBS_len(contents) != 0) {
1584 return 0;
1585 }
1586
1587 ssl->s3->tlsext_channel_id_valid = 1;
1588 return 1;
1589}
1590
David Benjamin8c880a22016-12-03 02:20:34 -05001591static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1592 uint8_t *out_alert, CBS *contents) {
1593 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001594 if (contents == NULL ||
1595 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001596 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001597 return 1;
1598 }
1599
1600 if (CBS_len(contents) != 0) {
1601 return 0;
1602 }
1603
1604 ssl->s3->tlsext_channel_id_valid = 1;
1605 return 1;
1606}
1607
David Benjamin8c880a22016-12-03 02:20:34 -05001608static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1609 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001610 if (!ssl->s3->tlsext_channel_id_valid) {
1611 return 1;
1612 }
1613
1614 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1615 !CBB_add_u16(out, 0 /* length */)) {
1616 return 0;
1617 }
1618
1619 return 1;
1620}
1621
Adam Langley391250d2015-07-15 19:06:07 -07001622
1623/* Secure Real-time Transport Protocol (SRTP) extension.
1624 *
1625 * https://tools.ietf.org/html/rfc5764 */
1626
Adam Langley391250d2015-07-15 19:06:07 -07001627
David Benjamin8c880a22016-12-03 02:20:34 -05001628static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1629 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001630}
1631
David Benjamin8c880a22016-12-03 02:20:34 -05001632static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1633 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001634 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1635 if (profiles == NULL) {
1636 return 1;
1637 }
1638 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1639 if (num_profiles == 0) {
1640 return 1;
1641 }
1642
1643 CBB contents, profile_ids;
1644 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1645 !CBB_add_u16_length_prefixed(out, &contents) ||
1646 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1647 return 0;
1648 }
1649
David Benjamin54091232016-09-05 12:47:25 -04001650 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001651 if (!CBB_add_u16(&profile_ids,
1652 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1653 return 0;
1654 }
1655 }
1656
1657 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1658 !CBB_flush(out)) {
1659 return 0;
1660 }
1661
1662 return 1;
1663}
1664
David Benjamin8c880a22016-12-03 02:20:34 -05001665static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001666 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001667 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001668 if (contents == NULL) {
1669 return 1;
1670 }
1671
1672 /* The extension consists of a u16-prefixed profile ID list containing a
1673 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1674 *
1675 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1676 CBS profile_ids, srtp_mki;
1677 uint16_t profile_id;
1678 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1679 !CBS_get_u16(&profile_ids, &profile_id) ||
1680 CBS_len(&profile_ids) != 0 ||
1681 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1682 CBS_len(contents) != 0) {
1683 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1684 return 0;
1685 }
1686
1687 if (CBS_len(&srtp_mki) != 0) {
1688 /* Must be no MKI, since we never offer one. */
1689 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1690 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1691 return 0;
1692 }
1693
1694 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1695
1696 /* Check to see if the server gave us something we support (and presumably
1697 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001698 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001699 const SRTP_PROTECTION_PROFILE *profile =
1700 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1701
1702 if (profile->id == profile_id) {
1703 ssl->srtp_profile = profile;
1704 return 1;
1705 }
1706 }
1707
1708 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1709 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1710 return 0;
1711}
1712
David Benjamin8c880a22016-12-03 02:20:34 -05001713static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001714 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001715 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001716 if (contents == NULL) {
1717 return 1;
1718 }
1719
1720 CBS profile_ids, srtp_mki;
1721 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1722 CBS_len(&profile_ids) < 2 ||
1723 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1724 CBS_len(contents) != 0) {
1725 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1726 return 0;
1727 }
1728 /* Discard the MKI value for now. */
1729
1730 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1731 SSL_get_srtp_profiles(ssl);
1732
1733 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001734 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001735 const SRTP_PROTECTION_PROFILE *server_profile =
1736 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1737
1738 CBS profile_ids_tmp;
1739 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1740
1741 while (CBS_len(&profile_ids_tmp) > 0) {
1742 uint16_t profile_id;
1743 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1744 return 0;
1745 }
1746
1747 if (server_profile->id == profile_id) {
1748 ssl->srtp_profile = server_profile;
1749 return 1;
1750 }
1751 }
1752 }
1753
1754 return 1;
1755}
1756
David Benjamin8c880a22016-12-03 02:20:34 -05001757static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1758 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001759 if (ssl->srtp_profile == NULL) {
1760 return 1;
1761 }
1762
1763 CBB contents, profile_ids;
1764 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1765 !CBB_add_u16_length_prefixed(out, &contents) ||
1766 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1767 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1768 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1769 !CBB_flush(out)) {
1770 return 0;
1771 }
1772
1773 return 1;
1774}
1775
Adam Langleybdd5d662015-07-20 16:19:08 -07001776
1777/* EC point formats.
1778 *
1779 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1780
David Benjamin8c880a22016-12-03 02:20:34 -05001781static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001782 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001783 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1784 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001785 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1786 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001787 !CBB_flush(out)) {
1788 return 0;
1789 }
1790
1791 return 1;
1792}
1793
David Benjamin8c880a22016-12-03 02:20:34 -05001794static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001795 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -05001796 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin70aba262016-11-01 12:08:15 -04001797 return 0;
1798 }
1799
1800 /* The point format extension is unneccessary in TLS 1.3. */
1801 if (min_version >= TLS1_3_VERSION) {
1802 return 1;
1803 }
1804
David Benjamin8c880a22016-12-03 02:20:34 -05001805 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001806}
1807
David Benjamin8c880a22016-12-03 02:20:34 -05001808static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001809 CBS *contents) {
1810 if (contents == NULL) {
1811 return 1;
1812 }
1813
David Benjamin8c880a22016-12-03 02:20:34 -05001814 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001815 return 0;
1816 }
1817
Adam Langleybdd5d662015-07-20 16:19:08 -07001818 CBS ec_point_format_list;
1819 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1820 CBS_len(contents) != 0) {
1821 return 0;
1822 }
1823
David Benjaminfc059942015-07-30 23:01:59 -04001824 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1825 * point format. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05001826 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1827 TLSEXT_ECPOINTFORMAT_uncompressed,
1828 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001829 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001830 return 0;
1831 }
1832
1833 return 1;
1834}
1835
David Benjamin8c880a22016-12-03 02:20:34 -05001836static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001837 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001838 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001839 return 1;
1840 }
1841
David Benjamin8c880a22016-12-03 02:20:34 -05001842 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001843}
1844
David Benjamin8c880a22016-12-03 02:20:34 -05001845static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1846 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001847 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1848 return 1;
1849 }
1850
David Benjamin45738dd2017-02-09 20:01:26 -05001851 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1852 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001853 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001854
1855 if (!using_ecc) {
1856 return 1;
1857 }
1858
David Benjamin8c880a22016-12-03 02:20:34 -05001859 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001860}
1861
Steven Valdeza833c352016-11-01 13:39:36 -04001862
Steven Valdez4aa154e2016-07-29 14:32:55 -04001863/* Pre Shared Key
1864 *
Steven Valdeza833c352016-11-01 13:39:36 -04001865 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001866
David Benjamin8c880a22016-12-03 02:20:34 -05001867static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1868 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001869 uint16_t min_version, max_version;
1870 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1871 return 0;
1872 }
1873
1874 uint16_t session_version;
1875 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1876 !ssl->method->version_from_wire(&session_version,
1877 ssl->session->ssl_version) ||
1878 session_version < TLS1_3_VERSION) {
1879 return 0;
1880 }
1881
Steven Valdez908ac192017-01-12 13:17:07 -05001882 const EVP_MD *digest = SSL_SESSION_get_digest(ssl->session, ssl);
1883 if (digest == NULL) {
1884 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1885 return 0;
1886 }
1887
Steven Valdeza833c352016-11-01 13:39:36 -04001888 size_t binder_len = EVP_MD_size(digest);
1889 return 15 + ssl->session->tlsext_ticklen + binder_len;
1890}
1891
David Benjamin8c880a22016-12-03 02:20:34 -05001892static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1893 SSL *const ssl = hs->ssl;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001894 uint16_t min_version, max_version;
1895 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1896 return 0;
1897 }
1898
David Benjamin2dc02042016-09-19 19:57:37 -04001899 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001900 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001901 !ssl->method->version_from_wire(&session_version,
1902 ssl->session->ssl_version) ||
1903 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001904 return 1;
1905 }
1906
David Benjaminad8f5e12017-02-20 17:00:20 -05001907 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001908 ssl_get_current_time(ssl, &now);
1909 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1910 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1911
1912 /* Fill in a placeholder zero binder of the appropriate length. It will be
1913 * computed and filled in later after length prefixes are computed. */
1914 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez908ac192017-01-12 13:17:07 -05001915
1916 const EVP_MD *digest = SSL_SESSION_get_digest(ssl->session, ssl);
1917 if (digest == NULL) {
1918 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1919 return 0;
1920 }
1921
Steven Valdeza833c352016-11-01 13:39:36 -04001922 size_t binder_len = EVP_MD_size(digest);
1923
1924 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001925 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1926 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001927 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001928 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1929 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001930 ssl->session->tlsext_ticklen) ||
1931 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1932 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1933 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1934 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001935 return 0;
1936 }
1937
David Benjamin8c880a22016-12-03 02:20:34 -05001938 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001939 return CBB_flush(out);
1940}
1941
David Benjamin8baf9632016-11-17 17:11:16 +09001942int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1943 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001944 CBS *contents) {
1945 uint16_t psk_id;
1946 if (!CBS_get_u16(contents, &psk_id) ||
1947 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001948 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001949 *out_alert = SSL_AD_DECODE_ERROR;
1950 return 0;
1951 }
1952
David Benjamineab773a2016-11-09 18:32:35 -05001953 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001954 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001955 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001956 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1957 return 0;
1958 }
1959
1960 return 1;
1961}
1962
David Benjamin35ac5b72017-03-03 15:05:56 -05001963int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001964 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001965 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04001966 /* We only process the first PSK identity since we don't support pure PSK. */
David Benjamin707af292017-03-10 17:47:18 -05001967 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001968 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001969 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001970 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001971 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001972 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001973 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001974 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001975 *out_alert = SSL_AD_DECODE_ERROR;
1976 return 0;
1977 }
1978
Steven Valdeza833c352016-11-01 13:39:36 -04001979 *out_binders = binders;
1980
David Benjaminaedf3032016-12-01 16:47:56 -05001981 /* Check the syntax of the remaining identities, but do not process them. */
1982 size_t num_identities = 1;
1983 while (CBS_len(&identities) != 0) {
1984 CBS unused_ticket;
1985 uint32_t unused_obfuscated_ticket_age;
1986 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1987 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1988 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1989 *out_alert = SSL_AD_DECODE_ERROR;
1990 return 0;
1991 }
1992
1993 num_identities++;
1994 }
1995
1996 /* Check the syntax of the binders. The value will be checked later if
1997 * resuming. */
1998 size_t num_binders = 0;
1999 while (CBS_len(&binders) != 0) {
2000 CBS binder;
2001 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
2002 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2003 *out_alert = SSL_AD_DECODE_ERROR;
2004 return 0;
2005 }
2006
2007 num_binders++;
2008 }
2009
2010 if (num_identities != num_binders) {
2011 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
2012 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04002013 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04002014 }
2015
David Benjamine7f60a22016-11-16 18:54:25 +09002016 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002017}
2018
David Benjamin8baf9632016-11-17 17:11:16 +09002019int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2020 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002021 return 1;
2022 }
2023
2024 CBB contents;
2025 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2026 !CBB_add_u16_length_prefixed(out, &contents) ||
2027 /* We only consider the first identity for resumption */
2028 !CBB_add_u16(&contents, 0) ||
2029 !CBB_flush(out)) {
2030 return 0;
2031 }
2032
2033 return 1;
2034}
2035
2036
Steven Valdeza833c352016-11-01 13:39:36 -04002037/* Pre-Shared Key Exchange Modes
2038 *
2039 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002040
David Benjamin8c880a22016-12-03 02:20:34 -05002041static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2042 CBB *out) {
2043 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04002044 uint16_t min_version, max_version;
2045 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2046 return 0;
2047 }
2048
2049 if (max_version < TLS1_3_VERSION) {
2050 return 1;
2051 }
2052
2053 CBB contents, ke_modes;
2054 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2055 !CBB_add_u16_length_prefixed(out, &contents) ||
2056 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2057 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2058 return 0;
2059 }
2060
2061 return CBB_flush(out);
2062}
2063
David Benjamin8c880a22016-12-03 02:20:34 -05002064static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002065 uint8_t *out_alert,
2066 CBS *contents) {
2067 if (contents == NULL) {
2068 return 1;
2069 }
2070
Steven Valdeza833c352016-11-01 13:39:36 -04002071 CBS ke_modes;
2072 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2073 CBS_len(&ke_modes) == 0 ||
2074 CBS_len(contents) != 0) {
2075 *out_alert = SSL_AD_DECODE_ERROR;
2076 return 0;
2077 }
2078
2079 /* We only support tickets with PSK_DHE_KE. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05002080 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2081 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002082
2083 return 1;
2084}
2085
2086
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002087/* Early Data Indication
2088 *
2089 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2090
David Benjamin8c880a22016-12-03 02:20:34 -05002091static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002092 SSL *const ssl = hs->ssl;
2093 uint16_t session_version;
2094 if (ssl->session == NULL ||
2095 !ssl->method->version_from_wire(&session_version,
2096 ssl->session->ssl_version) ||
2097 session_version < TLS1_3_VERSION ||
2098 ssl->session->ticket_max_early_data == 0 ||
2099 hs->received_hello_retry_request ||
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05002100 !ssl->cert->enable_early_data) {
Steven Valdez2d850622017-01-11 11:34:52 -05002101 return 1;
2102 }
2103
2104 hs->early_data_offered = 1;
2105
2106 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2107 !CBB_add_u16(out, 0) ||
2108 !CBB_flush(out)) {
2109 return 0;
2110 }
2111
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002112 return 1;
2113}
2114
Steven Valdez2d850622017-01-11 11:34:52 -05002115static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002116 uint8_t *out_alert, CBS *contents) {
2117 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002118 if (contents == NULL) {
2119 return 1;
2120 }
2121
2122 if (CBS_len(contents) != 0) {
2123 *out_alert = SSL_AD_DECODE_ERROR;
2124 return 0;
2125 }
2126
Steven Valdez2d850622017-01-11 11:34:52 -05002127 if (!ssl->s3->session_reused) {
2128 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2129 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2130 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002131 }
Steven Valdez2d850622017-01-11 11:34:52 -05002132
2133 ssl->early_data_accepted = 1;
2134 return 1;
2135}
2136
2137static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2138 uint8_t *out_alert, CBS *contents) {
2139 SSL *const ssl = hs->ssl;
2140 if (contents == NULL ||
2141 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2142 return 1;
2143 }
2144
2145 if (CBS_len(contents) != 0) {
2146 *out_alert = SSL_AD_DECODE_ERROR;
2147 return 0;
2148 }
2149
2150 hs->early_data_offered = 1;
2151 return 1;
2152}
2153
2154static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2155 if (!hs->ssl->early_data_accepted) {
2156 return 1;
2157 }
2158
2159 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2160 !CBB_add_u16(out, 0) ||
2161 !CBB_flush(out)) {
2162 return 0;
2163 }
2164
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002165 return 1;
2166}
2167
2168
Steven Valdez143e8b32016-07-11 13:19:03 -04002169/* Key Share
2170 *
David Benjamina128a552016-10-13 14:26:33 -04002171 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002172
David Benjamin8c880a22016-12-03 02:20:34 -05002173static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2174 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002175 uint16_t min_version, max_version;
2176 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2177 return 0;
2178 }
2179
David Benjamin53a2dfc2016-10-07 21:04:12 -04002180 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002181 return 1;
2182 }
2183
2184 CBB contents, kse_bytes;
2185 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2186 !CBB_add_u16_length_prefixed(out, &contents) ||
2187 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2188 return 0;
2189 }
2190
David Benjamin8c880a22016-12-03 02:20:34 -05002191 uint16_t group_id = hs->retry_group;
2192 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002193 /* We received a HelloRetryRequest without a new curve, so there is no new
2194 * share to append. Leave |ecdh_ctx| as-is. */
2195 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002196 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2197 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002198 return 0;
2199 }
David Benjamin8c880a22016-12-03 02:20:34 -05002200 OPENSSL_free(hs->key_share_bytes);
2201 hs->key_share_bytes = NULL;
2202 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002203 if (group_id == 0) {
2204 return CBB_flush(out);
2205 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002206 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002207 /* Add a fake group. See draft-davidben-tls-grease-01. */
2208 if (ssl->ctx->grease_enabled &&
2209 (!CBB_add_u16(&kse_bytes,
2210 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2211 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2212 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2213 return 0;
2214 }
2215
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002216 /* Predict the most preferred group. */
2217 const uint16_t *groups;
2218 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002219 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002220 if (groups_len == 0) {
2221 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2222 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002223 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002224
2225 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002226 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002227
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002228 CBB key_exchange;
2229 if (!CBB_add_u16(&kse_bytes, group_id) ||
2230 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002231 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2232 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002233 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002234 return 0;
2235 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002236
David Benjamin8c880a22016-12-03 02:20:34 -05002237 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002238 /* Save the contents of the extension to repeat it in the second
2239 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002240 hs->key_share_bytes_len = CBB_len(&kse_bytes);
2241 hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
2242 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002243 return 0;
2244 }
2245 }
2246
Steven Valdez143e8b32016-07-11 13:19:03 -04002247 return CBB_flush(out);
2248}
2249
David Benjamin8baf9632016-11-17 17:11:16 +09002250int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002251 size_t *out_secret_len,
2252 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002253 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002254 uint16_t group_id;
2255 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002256 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2257 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002258 *out_alert = SSL_AD_DECODE_ERROR;
2259 return 0;
2260 }
2261
David Benjamin8baf9632016-11-17 17:11:16 +09002262 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002263 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2264 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2265 return 0;
2266 }
2267
David Benjamin8baf9632016-11-17 17:11:16 +09002268 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2269 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002270 *out_alert = SSL_AD_INTERNAL_ERROR;
2271 return 0;
2272 }
2273
David Benjamin45738dd2017-02-09 20:01:26 -05002274 hs->new_session->group_id = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002275 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002276 return 1;
2277}
2278
David Benjamin8baf9632016-11-17 17:11:16 +09002279int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002280 uint8_t **out_secret,
2281 size_t *out_secret_len,
2282 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002283 uint16_t group_id;
2284 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002285 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002286 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2287 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2288 return 0;
2289 }
2290
2291 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002292 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002293 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002294 return 0;
2295 }
2296
David Benjamin7e1f9842016-09-20 19:24:40 -04002297 /* Find the corresponding key share. */
2298 int found = 0;
2299 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002300 while (CBS_len(&key_shares) > 0) {
2301 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002302 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002303 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002304 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2305 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002306 return 0;
2307 }
2308
David Benjamin7e1f9842016-09-20 19:24:40 -04002309 if (id == group_id) {
2310 if (found) {
2311 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2312 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2313 return 0;
2314 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002315
David Benjamin7e1f9842016-09-20 19:24:40 -04002316 found = 1;
2317 peer_key = peer_key_tmp;
2318 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002319 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002320 }
2321
David Benjamin7e1f9842016-09-20 19:24:40 -04002322 if (!found) {
2323 *out_found = 0;
2324 *out_secret = NULL;
2325 *out_secret_len = 0;
2326 return 1;
2327 }
2328
2329 /* Compute the DH secret. */
2330 uint8_t *secret = NULL;
2331 size_t secret_len;
2332 SSL_ECDH_CTX group;
David Benjamin17cf2cb2016-12-13 01:07:13 -05002333 OPENSSL_memset(&group, 0, sizeof(SSL_ECDH_CTX));
David Benjamin7e1f9842016-09-20 19:24:40 -04002334 CBB public_key;
2335 if (!CBB_init(&public_key, 32) ||
2336 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002337 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
2338 CBS_data(&peer_key), CBS_len(&peer_key)) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002339 !CBB_finish(&public_key, &hs->ecdh_public_key,
2340 &hs->ecdh_public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002341 OPENSSL_free(secret);
2342 SSL_ECDH_CTX_cleanup(&group);
2343 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002344 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002345 return 0;
2346 }
2347
2348 SSL_ECDH_CTX_cleanup(&group);
2349
2350 *out_secret = secret;
2351 *out_secret_len = secret_len;
2352 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002353 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002354}
2355
David Benjamin8baf9632016-11-17 17:11:16 +09002356int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002357 uint16_t group_id;
2358 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002359 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002360 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2361 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2362 !CBB_add_u16(&kse_bytes, group_id) ||
2363 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002364 !CBB_add_bytes(&public_key, hs->ecdh_public_key,
2365 hs->ecdh_public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002366 !CBB_flush(out)) {
2367 return 0;
2368 }
2369
David Benjaminbf833c32017-03-30 15:45:21 -05002370 OPENSSL_free(hs->ecdh_public_key);
2371 hs->ecdh_public_key = NULL;
2372 hs->ecdh_public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002373
David Benjamin45738dd2017-02-09 20:01:26 -05002374 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002375 return 1;
2376}
2377
2378
Steven Valdezfdd10992016-09-15 16:27:05 -04002379/* Supported Versions
2380 *
2381 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2382
David Benjamin8c880a22016-12-03 02:20:34 -05002383static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2384 SSL *const ssl = hs->ssl;
Steven Valdezfdd10992016-09-15 16:27:05 -04002385 uint16_t min_version, max_version;
2386 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2387 return 0;
2388 }
2389
2390 if (max_version <= TLS1_2_VERSION) {
2391 return 1;
2392 }
2393
2394 CBB contents, versions;
2395 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2396 !CBB_add_u16_length_prefixed(out, &contents) ||
2397 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2398 return 0;
2399 }
2400
David Benjamind9791bf2016-09-27 16:39:52 -04002401 /* Add a fake version. See draft-davidben-tls-grease-01. */
2402 if (ssl->ctx->grease_enabled &&
2403 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2404 return 0;
2405 }
2406
Steven Valdezfdd10992016-09-15 16:27:05 -04002407 for (uint16_t version = max_version; version >= min_version; version--) {
2408 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2409 return 0;
2410 }
2411 }
2412
2413 if (!CBB_flush(out)) {
2414 return 0;
2415 }
2416
2417 return 1;
2418}
2419
2420
David Benjamin3baa6e12016-10-07 21:10:38 -04002421/* Cookie
2422 *
2423 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2424
David Benjamin8c880a22016-12-03 02:20:34 -05002425static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2426 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002427 return 1;
2428 }
2429
2430 CBB contents, cookie;
2431 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2432 !CBB_add_u16_length_prefixed(out, &contents) ||
2433 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002434 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002435 !CBB_flush(out)) {
2436 return 0;
2437 }
2438
2439 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002440 OPENSSL_free(hs->cookie);
2441 hs->cookie = NULL;
2442 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002443 return 1;
2444}
2445
2446
Steven Valdezce902a92016-05-17 11:47:53 -04002447/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002448 *
Steven Valdezce902a92016-05-17 11:47:53 -04002449 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002450 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002451
David Benjamin8c880a22016-12-03 02:20:34 -05002452static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2453 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002454 CBB contents, groups_bytes;
2455 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002456 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002457 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002458 return 0;
2459 }
2460
David Benjamin65ac9972016-09-02 21:35:25 -04002461 /* Add a fake group. See draft-davidben-tls-grease-01. */
2462 if (ssl->ctx->grease_enabled &&
2463 !CBB_add_u16(&groups_bytes,
2464 ssl_get_grease_value(ssl, ssl_grease_group))) {
2465 return 0;
2466 }
2467
Steven Valdezce902a92016-05-17 11:47:53 -04002468 const uint16_t *groups;
2469 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002470 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002471
David Benjamin54091232016-09-05 12:47:25 -04002472 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002473 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002474 return 0;
2475 }
2476 }
2477
2478 return CBB_flush(out);
2479}
2480
David Benjamin8c880a22016-12-03 02:20:34 -05002481static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2482 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002483 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002484 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2485 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002486 return 1;
2487}
2488
David Benjamin8c880a22016-12-03 02:20:34 -05002489static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2490 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002491 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002492 if (contents == NULL) {
2493 return 1;
2494 }
2495
Steven Valdezce902a92016-05-17 11:47:53 -04002496 CBS supported_group_list;
2497 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2498 CBS_len(&supported_group_list) == 0 ||
2499 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002500 CBS_len(contents) != 0) {
2501 return 0;
2502 }
2503
David Benjamin8c880a22016-12-03 02:20:34 -05002504 hs->peer_supported_group_list =
2505 OPENSSL_malloc(CBS_len(&supported_group_list));
2506 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002507 *out_alert = SSL_AD_INTERNAL_ERROR;
2508 return 0;
2509 }
2510
Steven Valdezce902a92016-05-17 11:47:53 -04002511 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002512 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002513 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002514 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002515 goto err;
2516 }
2517 }
2518
Steven Valdezce902a92016-05-17 11:47:53 -04002519 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002520 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002521
2522 return 1;
2523
2524err:
David Benjamin8c880a22016-12-03 02:20:34 -05002525 OPENSSL_free(hs->peer_supported_group_list);
2526 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002527 *out_alert = SSL_AD_INTERNAL_ERROR;
2528 return 0;
2529}
2530
David Benjamin8c880a22016-12-03 02:20:34 -05002531static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002532 /* Servers don't echo this extension. */
2533 return 1;
2534}
2535
2536
Adam Langley614c66a2015-06-12 15:26:58 -07002537/* kExtensions contains all the supported extensions. */
2538static const struct tls_extension kExtensions[] = {
2539 {
Adam Langley5021b222015-06-12 18:27:58 -07002540 TLSEXT_TYPE_renegotiate,
2541 NULL,
2542 ext_ri_add_clienthello,
2543 ext_ri_parse_serverhello,
2544 ext_ri_parse_clienthello,
2545 ext_ri_add_serverhello,
2546 },
2547 {
Adam Langley614c66a2015-06-12 15:26:58 -07002548 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002549 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002550 ext_sni_add_clienthello,
2551 ext_sni_parse_serverhello,
2552 ext_sni_parse_clienthello,
2553 ext_sni_add_serverhello,
2554 },
Adam Langley0a056712015-07-01 15:03:33 -07002555 {
2556 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002557 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002558 ext_ems_add_clienthello,
2559 ext_ems_parse_serverhello,
2560 ext_ems_parse_clienthello,
2561 ext_ems_add_serverhello,
2562 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002563 {
2564 TLSEXT_TYPE_session_ticket,
2565 NULL,
2566 ext_ticket_add_clienthello,
2567 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002568 /* Ticket extension client parsing is handled in ssl_session.c */
2569 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002570 ext_ticket_add_serverhello,
2571 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002572 {
2573 TLSEXT_TYPE_signature_algorithms,
2574 NULL,
2575 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002576 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002577 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002578 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002579 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002580 {
2581 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002582 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002583 ext_ocsp_add_clienthello,
2584 ext_ocsp_parse_serverhello,
2585 ext_ocsp_parse_clienthello,
2586 ext_ocsp_add_serverhello,
2587 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002588 {
2589 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002590 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002591 ext_npn_add_clienthello,
2592 ext_npn_parse_serverhello,
2593 ext_npn_parse_clienthello,
2594 ext_npn_add_serverhello,
2595 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002596 {
2597 TLSEXT_TYPE_certificate_timestamp,
2598 NULL,
2599 ext_sct_add_clienthello,
2600 ext_sct_parse_serverhello,
2601 ext_sct_parse_clienthello,
2602 ext_sct_add_serverhello,
2603 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002604 {
2605 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002606 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002607 ext_alpn_add_clienthello,
2608 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002609 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2610 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002611 ext_alpn_add_serverhello,
2612 },
Adam Langley49c7af12015-07-10 14:33:46 -07002613 {
2614 TLSEXT_TYPE_channel_id,
2615 ext_channel_id_init,
2616 ext_channel_id_add_clienthello,
2617 ext_channel_id_parse_serverhello,
2618 ext_channel_id_parse_clienthello,
2619 ext_channel_id_add_serverhello,
2620 },
Adam Langley391250d2015-07-15 19:06:07 -07002621 {
2622 TLSEXT_TYPE_srtp,
2623 ext_srtp_init,
2624 ext_srtp_add_clienthello,
2625 ext_srtp_parse_serverhello,
2626 ext_srtp_parse_clienthello,
2627 ext_srtp_add_serverhello,
2628 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002629 {
2630 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002631 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002632 ext_ec_point_add_clienthello,
2633 ext_ec_point_parse_serverhello,
2634 ext_ec_point_parse_clienthello,
2635 ext_ec_point_add_serverhello,
2636 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002637 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002638 TLSEXT_TYPE_key_share,
2639 NULL,
2640 ext_key_share_add_clienthello,
2641 forbid_parse_serverhello,
2642 ignore_parse_clienthello,
2643 dont_add_serverhello,
2644 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002645 {
Steven Valdeza833c352016-11-01 13:39:36 -04002646 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002647 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002648 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002649 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002650 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002651 dont_add_serverhello,
2652 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002653 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002654 TLSEXT_TYPE_early_data,
2655 NULL,
2656 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002657 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002658 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002659 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002660 },
2661 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002662 TLSEXT_TYPE_supported_versions,
2663 NULL,
2664 ext_supported_versions_add_clienthello,
2665 forbid_parse_serverhello,
2666 ignore_parse_clienthello,
2667 dont_add_serverhello,
2668 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002669 {
2670 TLSEXT_TYPE_cookie,
2671 NULL,
2672 ext_cookie_add_clienthello,
2673 forbid_parse_serverhello,
2674 ignore_parse_clienthello,
2675 dont_add_serverhello,
2676 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002677 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2678 * intolerant to the last extension being zero-length. See
2679 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002680 {
Steven Valdezce902a92016-05-17 11:47:53 -04002681 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002682 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002683 ext_supported_groups_add_clienthello,
2684 ext_supported_groups_parse_serverhello,
2685 ext_supported_groups_parse_clienthello,
2686 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002687 },
Adam Langley614c66a2015-06-12 15:26:58 -07002688};
2689
2690#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2691
Adam Langley4cfa96b2015-07-01 11:56:55 -07002692OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002693 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002694 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002695OPENSSL_COMPILE_ASSERT(
2696 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2697 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002698
Adam Langley614c66a2015-06-12 15:26:58 -07002699static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2700 uint16_t value) {
2701 unsigned i;
2702 for (i = 0; i < kNumExtensions; i++) {
2703 if (kExtensions[i].value == value) {
2704 *out_index = i;
2705 return &kExtensions[i];
2706 }
2707 }
2708
2709 return NULL;
2710}
2711
Adam Langley09505632015-07-30 18:10:13 -07002712int SSL_extension_supported(unsigned extension_value) {
2713 uint32_t index;
2714 return extension_value == TLSEXT_TYPE_padding ||
2715 tls_extension_find(&index, extension_value) != NULL;
2716}
2717
David Benjamin8c880a22016-12-03 02:20:34 -05002718int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2719 SSL *const ssl = hs->ssl;
David Benjaminf04c2e92016-12-06 13:35:25 -05002720 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2721 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002722 !ssl->s3->send_connection_binding) {
2723 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002724 }
Adam Langley95c29f32014-06-20 12:00:00 -07002725
David Benjamine8d53502015-10-10 14:13:23 -04002726 CBB extensions;
2727 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002728 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002729 }
Adam Langley95c29f32014-06-20 12:00:00 -07002730
David Benjamin8c880a22016-12-03 02:20:34 -05002731 hs->extensions.sent = 0;
2732 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002733
David Benjamin54091232016-09-05 12:47:25 -04002734 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002735 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002736 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002737 }
2738 }
Adam Langley95c29f32014-06-20 12:00:00 -07002739
David Benjamin65ac9972016-09-02 21:35:25 -04002740 uint16_t grease_ext1 = 0;
2741 if (ssl->ctx->grease_enabled) {
2742 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2743 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2744 if (!CBB_add_u16(&extensions, grease_ext1) ||
2745 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2746 goto err;
2747 }
2748 }
2749
David Benjamin54091232016-09-05 12:47:25 -04002750 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002751 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002752 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002753 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002754 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002755 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002756 }
Adam Langley95c29f32014-06-20 12:00:00 -07002757
Adam Langley33ad2b52015-07-20 17:43:53 -07002758 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002759 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002760 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002761 }
Adam Langley75712922014-10-10 16:23:43 -07002762
David Benjamin2bd19172016-11-17 16:47:15 +09002763 if (!custom_ext_add_clienthello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002764 goto err;
2765 }
2766
David Benjamin65ac9972016-09-02 21:35:25 -04002767 if (ssl->ctx->grease_enabled) {
2768 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2769 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2770
2771 /* The two fake extensions must not have the same value. GREASE values are
2772 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2773 * one. */
2774 if (grease_ext1 == grease_ext2) {
2775 grease_ext2 ^= 0x1010;
2776 }
2777
2778 if (!CBB_add_u16(&extensions, grease_ext2) ||
2779 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2780 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2781 goto err;
2782 }
2783 }
2784
David Benjamince079fd2016-08-02 16:22:34 -04002785 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002786 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002787 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002788 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002789 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002790 *
2791 * NB: because this code works out the length of all existing extensions
2792 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002793 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002794 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002795 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002796 * Server 7.0 is intolerant to the last extension being zero-length. See
2797 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002798 if (padding_len >= 4 + 1) {
2799 padding_len -= 4;
2800 } else {
2801 padding_len = 1;
2802 }
Adam Langley95c29f32014-06-20 12:00:00 -07002803
Adam Langley33ad2b52015-07-20 17:43:53 -07002804 uint8_t *padding_bytes;
2805 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2806 !CBB_add_u16(&extensions, padding_len) ||
2807 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2808 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002809 }
Adam Langley75712922014-10-10 16:23:43 -07002810
David Benjamin17cf2cb2016-12-13 01:07:13 -05002811 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002812 }
2813 }
Adam Langley75712922014-10-10 16:23:43 -07002814
Steven Valdeza833c352016-11-01 13:39:36 -04002815 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002816 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002817 goto err;
2818 }
2819
David Benjamina01deee2015-12-08 18:56:31 -05002820 /* Discard empty extensions blocks. */
2821 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002822 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002823 }
2824
David Benjamine8d53502015-10-10 14:13:23 -04002825 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002826
2827err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002828 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002829 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002830}
Adam Langley95c29f32014-06-20 12:00:00 -07002831
David Benjamin8c880a22016-12-03 02:20:34 -05002832int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2833 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002834 CBB extensions;
2835 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002836 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002837 }
2838
David Benjamin8c880a22016-12-03 02:20:34 -05002839 for (unsigned i = 0; i < kNumExtensions; i++) {
2840 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002841 /* Don't send extensions that were not received. */
2842 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002843 }
Adam Langley95c29f32014-06-20 12:00:00 -07002844
David Benjamin8c880a22016-12-03 02:20:34 -05002845 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002846 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002847 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002848 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002849 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002850 }
Adam Langley95c29f32014-06-20 12:00:00 -07002851
David Benjamin2bd19172016-11-17 16:47:15 +09002852 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002853 goto err;
2854 }
2855
Steven Valdez143e8b32016-07-11 13:19:03 -04002856 /* Discard empty extensions blocks before TLS 1.3. */
2857 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2858 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002859 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002860 }
2861
David Benjamin56380462015-10-10 14:59:09 -04002862 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002863
2864err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002865 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002866 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002867}
Adam Langley95c29f32014-06-20 12:00:00 -07002868
David Benjamin731058e2016-12-03 23:15:13 -05002869static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2870 const SSL_CLIENT_HELLO *client_hello,
2871 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002872 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002873 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002874 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002875 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002876 }
2877 }
2878
David Benjamin8c880a22016-12-03 02:20:34 -05002879 hs->extensions.received = 0;
2880 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002881
David Benjamine14ff062016-08-09 16:21:24 -04002882 CBS extensions;
2883 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2884 while (CBS_len(&extensions) != 0) {
2885 uint16_t type;
2886 CBS extension;
2887
2888 /* Decode the next extension. */
2889 if (!CBS_get_u16(&extensions, &type) ||
2890 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002891 *out_alert = SSL_AD_DECODE_ERROR;
2892 return 0;
2893 }
Adam Langley95c29f32014-06-20 12:00:00 -07002894
David Benjamine14ff062016-08-09 16:21:24 -04002895 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2896 * ambiguous. Ignore all but the renegotiation_info extension. */
2897 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2898 continue;
2899 }
Adam Langley95c29f32014-06-20 12:00:00 -07002900
David Benjamine14ff062016-08-09 16:21:24 -04002901 unsigned ext_index;
2902 const struct tls_extension *const ext =
2903 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002904
David Benjamine14ff062016-08-09 16:21:24 -04002905 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002906 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002907 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002908 return 0;
2909 }
David Benjamine14ff062016-08-09 16:21:24 -04002910 continue;
2911 }
2912
David Benjamin8c880a22016-12-03 02:20:34 -05002913 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002914 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002915 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002916 *out_alert = alert;
2917 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002918 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002919 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002920 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002921 }
Adam Langley75712922014-10-10 16:23:43 -07002922
David Benjamin1deb41b2016-08-09 19:36:38 -04002923 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002924 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002925 continue;
2926 }
2927
2928 CBS *contents = NULL, fake_contents;
2929 static const uint8_t kFakeRenegotiateExtension[] = {0};
2930 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2931 ssl_client_cipher_list_contains_cipher(client_hello,
2932 SSL3_CK_SCSV & 0xffff)) {
2933 /* The renegotiation SCSV was received so pretend that we received a
2934 * renegotiation extension. */
2935 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2936 sizeof(kFakeRenegotiateExtension));
2937 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002938 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002939 }
2940
2941 /* Extension wasn't observed so call the callback with a NULL
2942 * parameter. */
2943 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002944 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002945 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002946 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002947 *out_alert = alert;
2948 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002949 }
2950 }
2951
Adam Langleyfcf25832014-12-18 17:42:32 -08002952 return 1;
2953}
Adam Langley95c29f32014-06-20 12:00:00 -07002954
David Benjamin731058e2016-12-03 23:15:13 -05002955int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2956 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002957 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002958 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002959 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002960 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002961 return 0;
2962 }
Adam Langley95c29f32014-06-20 12:00:00 -07002963
David Benjamin8c880a22016-12-03 02:20:34 -05002964 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002965 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002966 return 0;
2967 }
Adam Langley95c29f32014-06-20 12:00:00 -07002968
Adam Langleyfcf25832014-12-18 17:42:32 -08002969 return 1;
2970}
Adam Langley95c29f32014-06-20 12:00:00 -07002971
David Benjamin8c880a22016-12-03 02:20:34 -05002972static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2973 int *out_alert) {
2974 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002975 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2976 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2977 return 1;
2978 }
Adam Langley614c66a2015-06-12 15:26:58 -07002979
Steven Valdez143e8b32016-07-11 13:19:03 -04002980 /* Decode the extensions block and check it is valid. */
2981 CBS extensions;
2982 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2983 !tls1_check_duplicate_extensions(&extensions)) {
2984 *out_alert = SSL_AD_DECODE_ERROR;
2985 return 0;
2986 }
2987
2988 uint32_t received = 0;
2989 while (CBS_len(&extensions) != 0) {
2990 uint16_t type;
2991 CBS extension;
2992
2993 /* Decode the next extension. */
2994 if (!CBS_get_u16(&extensions, &type) ||
2995 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002996 *out_alert = SSL_AD_DECODE_ERROR;
2997 return 0;
2998 }
Adam Langley95c29f32014-06-20 12:00:00 -07002999
Steven Valdez143e8b32016-07-11 13:19:03 -04003000 unsigned ext_index;
3001 const struct tls_extension *const ext =
3002 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07003003
Steven Valdez143e8b32016-07-11 13:19:03 -04003004 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09003005 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003006 return 0;
3007 }
Steven Valdez143e8b32016-07-11 13:19:03 -04003008 continue;
3009 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003010
David Benjamin5db7c9b2017-01-24 16:17:03 -05003011 OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
3012 too_many_bits);
3013
David Benjamin8c880a22016-12-03 02:20:34 -05003014 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04003015 type != TLSEXT_TYPE_renegotiate) {
3016 /* If the extension was never sent then it is illegal, except for the
3017 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04003018 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3019 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04003020 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04003021 return 0;
3022 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003023
Steven Valdez143e8b32016-07-11 13:19:03 -04003024 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07003025
Steven Valdez143e8b32016-07-11 13:19:03 -04003026 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003027 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04003028 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003029 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04003030 *out_alert = alert;
3031 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07003032 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003033 }
Adam Langley95c29f32014-06-20 12:00:00 -07003034
David Benjamin54091232016-09-05 12:47:25 -04003035 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003036 if (!(received & (1u << i))) {
3037 /* Extension wasn't observed so call the callback with a NULL
3038 * parameter. */
3039 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003040 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003041 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003042 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07003043 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08003044 return 0;
3045 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003046 }
3047 }
Adam Langley95c29f32014-06-20 12:00:00 -07003048
Adam Langleyfcf25832014-12-18 17:42:32 -08003049 return 1;
3050}
Adam Langley95c29f32014-06-20 12:00:00 -07003051
David Benjamin8c880a22016-12-03 02:20:34 -05003052static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3053 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003054 int ret = SSL_TLSEXT_ERR_NOACK;
3055 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003056
David Benjamin78f8aab2016-03-10 16:33:58 -05003057 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003058 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003059 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05003060 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
3061 ret = ssl->session_ctx->tlsext_servername_callback(
3062 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003063 }
Adam Langley95c29f32014-06-20 12:00:00 -07003064
Adam Langleyfcf25832014-12-18 17:42:32 -08003065 switch (ret) {
3066 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003067 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003068 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003069
Adam Langleyfcf25832014-12-18 17:42:32 -08003070 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003071 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003072 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003073
Adam Langleyfcf25832014-12-18 17:42:32 -08003074 default:
3075 return 1;
3076 }
3077}
Adam Langleyed8270a2014-09-02 13:52:56 -07003078
David Benjamin8c880a22016-12-03 02:20:34 -05003079int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3080 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08003081 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003082 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003083 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003084 return 0;
3085 }
3086
Adam Langleyfcf25832014-12-18 17:42:32 -08003087 return 1;
3088}
Adam Langley95c29f32014-06-20 12:00:00 -07003089
Adam Langley4c341d02017-03-08 19:33:21 -08003090static enum ssl_ticket_aead_result_t
3091ssl_decrypt_ticket_with_cipher_ctx(SSL *ssl, uint8_t **out, size_t *out_len,
3092 int *out_renew_ticket, const uint8_t *ticket,
3093 size_t ticket_len) {
3094 enum ssl_ticket_aead_result_t ret = ssl_ticket_aead_ignore_ticket;
3095 const SSL_CTX *const ssl_ctx = ssl->session_ctx;
David Benjamine3aa1d92015-06-16 15:34:50 -04003096 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003097
David Benjamine3aa1d92015-06-16 15:34:50 -04003098 HMAC_CTX hmac_ctx;
3099 HMAC_CTX_init(&hmac_ctx);
3100 EVP_CIPHER_CTX cipher_ctx;
3101 EVP_CIPHER_CTX_init(&cipher_ctx);
3102
David Benjaminadcc3952015-04-26 13:07:57 -04003103 /* Ensure there is room for the key name and the largest IV
3104 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3105 * the maximum IV length should be well under the minimum size for the
3106 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003107 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
Adam Langley4c341d02017-03-08 19:33:21 -08003108 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003109 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003110 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003111
David Benjamine3aa1d92015-06-16 15:34:50 -04003112 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003113 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3114 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3115 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003116 if (cb_ret < 0) {
Adam Langley4c341d02017-03-08 19:33:21 -08003117 ret = ssl_ticket_aead_error;
3118 goto out;
3119 } else if (cb_ret == 0) {
3120 goto out;
3121 } else if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003122 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003123 }
3124 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003125 /* Check the key name matches. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003126 if (OPENSSL_memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3127 SSL_TICKET_KEY_NAME_LEN) != 0) {
Adam Langley4c341d02017-03-08 19:33:21 -08003128 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003129 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003130 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3131 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003132 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003133 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3134 ssl_ctx->tlsext_tick_aes_key, iv)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003135 ret = ssl_ticket_aead_error;
3136 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003137 }
3138 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003139 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003140
David Benjamine3aa1d92015-06-16 15:34:50 -04003141 /* Check the MAC at the end of the ticket. */
3142 uint8_t mac[EVP_MAX_MD_SIZE];
3143 size_t mac_len = HMAC_size(&hmac_ctx);
3144 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003145 /* The ticket must be large enough for key name, IV, data, and MAC. */
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 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3149 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003150 int mac_ok =
3151 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3152#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3153 mac_ok = 1;
3154#endif
3155 if (!mac_ok) {
Adam Langley4c341d02017-03-08 19:33:21 -08003156 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003157 }
3158
David Benjamine3aa1d92015-06-16 15:34:50 -04003159 /* Decrypt the session data. */
3160 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3161 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3162 mac_len;
3163 plaintext = OPENSSL_malloc(ciphertext_len);
3164 if (plaintext == NULL) {
Adam Langley4c341d02017-03-08 19:33:21 -08003165 ret = ssl_ticket_aead_error;
3166 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003167 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003168 size_t plaintext_len;
3169#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin17cf2cb2016-12-13 01:07:13 -05003170 OPENSSL_memcpy(plaintext, ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003171 plaintext_len = ciphertext_len;
3172#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003173 if (ciphertext_len >= INT_MAX) {
Adam Langley4c341d02017-03-08 19:33:21 -08003174 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003175 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003176 int len1, len2;
3177 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3178 (int)ciphertext_len) ||
3179 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003180 ERR_clear_error();
3181 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003182 }
Adam Langley4c341d02017-03-08 19:33:21 -08003183 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04003184#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003185
Adam Langley4c341d02017-03-08 19:33:21 -08003186 *out = plaintext;
3187 plaintext = NULL;
3188 *out_len = plaintext_len;
3189 ret = ssl_ticket_aead_success;
3190
3191out:
3192 OPENSSL_free(plaintext);
3193 HMAC_CTX_cleanup(&hmac_ctx);
3194 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3195 return ret;
3196}
3197
3198static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
3199 SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
3200 const uint8_t *ticket, size_t ticket_len) {
3201 uint8_t *plaintext = OPENSSL_malloc(ticket_len);
3202 if (plaintext == NULL) {
3203 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3204 return ssl_ticket_aead_error;
3205 }
3206
3207 size_t plaintext_len;
3208 const enum ssl_ticket_aead_result_t result =
3209 ssl->session_ctx->ticket_aead_method->open(
3210 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3211
3212 if (result == ssl_ticket_aead_success) {
3213 *out = plaintext;
3214 plaintext = NULL;
3215 *out_len = plaintext_len;
3216 }
3217
3218 OPENSSL_free(plaintext);
3219 return result;
3220}
3221
3222enum ssl_ticket_aead_result_t ssl_process_ticket(
3223 SSL *ssl, SSL_SESSION **out_session, int *out_renew_ticket,
3224 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3225 size_t session_id_len) {
3226 *out_renew_ticket = 0;
3227 *out_session = NULL;
3228
3229 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3230 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3231 return ssl_ticket_aead_ignore_ticket;
3232 }
3233
3234 uint8_t *plaintext = NULL;
3235 size_t plaintext_len;
3236 enum ssl_ticket_aead_result_t result;
3237 if (ssl->session_ctx->ticket_aead_method != NULL) {
3238 result = ssl_decrypt_ticket_with_method(
3239 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3240 } else {
3241 result = ssl_decrypt_ticket_with_cipher_ctx(
3242 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3243 }
3244
3245 if (result != ssl_ticket_aead_success) {
3246 return result;
3247 }
3248
David Benjamine3aa1d92015-06-16 15:34:50 -04003249 /* Decode the session. */
Adam Langley46db7af2017-02-01 15:49:37 -08003250 SSL_SESSION *session =
3251 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx);
Adam Langley4c341d02017-03-08 19:33:21 -08003252 OPENSSL_free(plaintext);
3253
David Benjamine3aa1d92015-06-16 15:34:50 -04003254 if (session == NULL) {
3255 ERR_clear_error(); /* Don't leave an error on the queue. */
Adam Langley4c341d02017-03-08 19:33:21 -08003256 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003257 }
3258
3259 /* Copy the client's session ID into the new session, to denote the ticket has
3260 * been accepted. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003261 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003262 session->session_id_length = session_id_len;
3263
3264 *out_session = session;
Adam Langley4c341d02017-03-08 19:33:21 -08003265 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003266}
Adam Langley95c29f32014-06-20 12:00:00 -07003267
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003268int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003269 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003270 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003271 return 1;
3272 }
David Benjamincd996942014-07-20 16:23:51 -04003273
David Benjamin0fc37ef2016-08-17 15:29:46 -04003274 OPENSSL_free(hs->peer_sigalgs);
3275 hs->peer_sigalgs = NULL;
3276 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003277
3278 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003279 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003280 return 0;
3281 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003282 num_sigalgs /= 2;
3283
3284 /* supported_signature_algorithms in the certificate request is
3285 * allowed to be empty. */
3286 if (num_sigalgs == 0) {
3287 return 1;
3288 }
3289
Steven Valdez02563852016-06-23 13:33:05 -04003290 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3291 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003292 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3293 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003294 return 0;
3295 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003296 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003297
3298 CBS sigalgs;
3299 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003300 for (size_t i = 0; i < num_sigalgs; i++) {
3301 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003302 return 0;
3303 }
3304 }
Adam Langley95c29f32014-06-20 12:00:00 -07003305
Adam Langleyfcf25832014-12-18 17:42:32 -08003306 return 1;
3307}
David Benjaminec2f27d2014-11-13 19:17:25 -05003308
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003309int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3310 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003311 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003312
Steven Valdezf0451ca2016-06-29 13:16:27 -04003313 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3314 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3315 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamina232a712017-03-30 15:51:53 -05003316 switch (EVP_PKEY_id(hs->local_pubkey)) {
3317 case EVP_PKEY_RSA:
3318 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3319 return 1;
3320 case EVP_PKEY_EC:
3321 *out = SSL_SIGN_ECDSA_SHA1;
3322 return 1;
3323 default:
3324 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3325 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003326 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04003327 }
3328
David Benjamin3ef76972016-10-17 17:59:54 -04003329 const uint16_t *sigalgs = cert->sigalgs;
3330 size_t num_sigalgs = cert->num_sigalgs;
3331 if (sigalgs == NULL) {
3332 sigalgs = kSignSignatureAlgorithms;
3333 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003334 }
3335
David Benjamin0fc37ef2016-08-17 15:29:46 -04003336 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3337 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3338 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003339 /* If the client didn't specify any signature_algorithms extension then
3340 * we can assume that it supports SHA1. See
3341 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3342 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3343 SSL_SIGN_ECDSA_SHA1};
3344 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003345 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003346 }
3347
David Benjamin0fc37ef2016-08-17 15:29:46 -04003348 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003349 uint16_t sigalg = sigalgs[i];
3350 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3351 * negotiated. */
3352 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjamina232a712017-03-30 15:51:53 -05003353 !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003354 continue;
3355 }
3356
David Benjamin0fc37ef2016-08-17 15:29:46 -04003357 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003358 if (sigalg == peer_sigalgs[j]) {
3359 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003360 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003361 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003362 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003363 }
Adam Langley95c29f32014-06-20 12:00:00 -07003364
David Benjaminea9a0d52016-07-08 15:52:59 -07003365 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3366 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003367}
Adam Langley95c29f32014-06-20 12:00:00 -07003368
Steven Valdez908ac192017-01-12 13:17:07 -05003369int tls1_verify_channel_id(SSL_HANDSHAKE *hs) {
3370 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003371 int ret = 0;
3372 uint16_t extension_type;
3373 CBS extension, channel_id;
3374
3375 /* A Channel ID handshake message is structured to contain multiple
3376 * extensions, but the only one that can be present is Channel ID. */
3377 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3378 if (!CBS_get_u16(&channel_id, &extension_type) ||
3379 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3380 CBS_len(&channel_id) != 0 ||
3381 extension_type != TLSEXT_TYPE_channel_id ||
3382 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3383 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3384 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3385 return 0;
3386 }
3387
3388 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3389 if (!p256) {
3390 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3391 return 0;
3392 }
3393
3394 EC_KEY *key = NULL;
3395 EC_POINT *point = NULL;
3396 BIGNUM x, y;
3397 ECDSA_SIG sig;
3398 BN_init(&x);
3399 BN_init(&y);
3400 sig.r = BN_new();
3401 sig.s = BN_new();
3402 if (sig.r == NULL || sig.s == NULL) {
3403 goto err;
3404 }
3405
3406 const uint8_t *p = CBS_data(&extension);
3407 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3408 BN_bin2bn(p + 32, 32, &y) == NULL ||
3409 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3410 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3411 goto err;
3412 }
3413
3414 point = EC_POINT_new(p256);
3415 if (point == NULL ||
3416 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3417 goto err;
3418 }
3419
3420 key = EC_KEY_new();
3421 if (key == NULL ||
3422 !EC_KEY_set_group(key, p256) ||
3423 !EC_KEY_set_public_key(key, point)) {
3424 goto err;
3425 }
3426
3427 uint8_t digest[EVP_MAX_MD_SIZE];
3428 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003429 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003430 goto err;
3431 }
3432
3433 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3434#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3435 sig_ok = 1;
3436#endif
3437 if (!sig_ok) {
3438 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3439 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3440 ssl->s3->tlsext_channel_id_valid = 0;
3441 goto err;
3442 }
3443
David Benjamin17cf2cb2016-12-13 01:07:13 -05003444 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
Nick Harper60a85cb2016-09-23 16:25:11 -07003445 ret = 1;
3446
3447err:
3448 BN_free(&x);
3449 BN_free(&y);
3450 BN_free(sig.r);
3451 BN_free(sig.s);
3452 EC_KEY_free(key);
3453 EC_POINT_free(point);
3454 EC_GROUP_free(p256);
3455 return ret;
3456}
3457
Steven Valdez908ac192017-01-12 13:17:07 -05003458int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3459 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003460 uint8_t digest[EVP_MAX_MD_SIZE];
3461 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003462 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003463 return 0;
3464 }
3465
3466 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3467 if (ec_key == NULL) {
3468 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3469 return 0;
3470 }
3471
3472 int ret = 0;
3473 BIGNUM *x = BN_new();
3474 BIGNUM *y = BN_new();
3475 ECDSA_SIG *sig = NULL;
3476 if (x == NULL || y == NULL ||
3477 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3478 EC_KEY_get0_public_key(ec_key),
3479 x, y, NULL)) {
3480 goto err;
3481 }
3482
3483 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3484 if (sig == NULL) {
3485 goto err;
3486 }
3487
3488 CBB child;
3489 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3490 !CBB_add_u16_length_prefixed(cbb, &child) ||
3491 !BN_bn2cbb_padded(&child, 32, x) ||
3492 !BN_bn2cbb_padded(&child, 32, y) ||
3493 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3494 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3495 !CBB_flush(cbb)) {
3496 goto err;
3497 }
3498
3499 ret = 1;
3500
3501err:
3502 BN_free(x);
3503 BN_free(y);
3504 ECDSA_SIG_free(sig);
3505 return ret;
3506}
3507
Steven Valdez908ac192017-01-12 13:17:07 -05003508int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3509 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003510 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3511 uint8_t *msg;
3512 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003513 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003514 ssl_cert_verify_channel_id)) {
3515 return 0;
3516 }
3517 SHA256(msg, msg_len, out);
3518 *out_len = SHA256_DIGEST_LENGTH;
3519 OPENSSL_free(msg);
3520 return 1;
3521 }
3522
Nick Harper95594012016-10-20 14:07:13 -07003523 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003524
Nick Harper95594012016-10-20 14:07:13 -07003525 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003526 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003527 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003528
Steven Valdez87eab492016-06-27 16:34:59 -04003529 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003530 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003531 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003532 if (ssl->session->original_handshake_hash_len == 0) {
3533 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003534 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003535 }
Nick Harper95594012016-10-20 14:07:13 -07003536 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3537 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003538 }
3539
Steven Valdez908ac192017-01-12 13:17:07 -05003540 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3541 size_t hs_hash_len;
3542 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript, hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003543 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003544 }
Steven Valdez908ac192017-01-12 13:17:07 -05003545 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003546 SHA256_Final(out, &ctx);
3547 *out_len = SHA256_DIGEST_LENGTH;
3548 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003549}
Adam Langley1258b6a2014-06-20 12:00:00 -07003550
3551/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin45738dd2017-02-09 20:01:26 -05003552 * hashes in |hs->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003553 * data. */
Steven Valdez908ac192017-01-12 13:17:07 -05003554int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3555 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003556 /* This function should never be called for a resumed session because the
3557 * handshake hashes that we wish to record are for the original, full
3558 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003559 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003560 return -1;
3561 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003562
Steven Valdez908ac192017-01-12 13:17:07 -05003563 OPENSSL_COMPILE_ASSERT(
David Benjamin45738dd2017-02-09 20:01:26 -05003564 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
Steven Valdez908ac192017-01-12 13:17:07 -05003565 original_handshake_hash_is_too_small);
3566
3567 size_t digest_len;
3568 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript,
David Benjamin45738dd2017-02-09 20:01:26 -05003569 hs->new_session->original_handshake_hash,
Steven Valdez908ac192017-01-12 13:17:07 -05003570 &digest_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003571 return -1;
3572 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003573
Steven Valdez908ac192017-01-12 13:17:07 -05003574 OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE <= 0xff, max_md_size_is_too_large);
David Benjamin45738dd2017-02-09 20:01:26 -05003575 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003576
Adam Langleyfcf25832014-12-18 17:42:32 -08003577 return 1;
3578}
Nick Harper60a85cb2016-09-23 16:25:11 -07003579
3580int ssl_do_channel_id_callback(SSL *ssl) {
3581 if (ssl->tlsext_channel_id_private != NULL ||
3582 ssl->ctx->channel_id_cb == NULL) {
3583 return 1;
3584 }
3585
3586 EVP_PKEY *key = NULL;
3587 ssl->ctx->channel_id_cb(ssl, &key);
3588 if (key == NULL) {
3589 /* The caller should try again later. */
3590 return 1;
3591 }
3592
3593 int ret = SSL_set1_tls_channel_id(ssl, key);
3594 EVP_PKEY_free(key);
3595 return ret;
3596}
Adam Langleycfa08c32016-11-17 13:21:27 -08003597
3598int ssl_is_sct_list_valid(const CBS *contents) {
3599 /* Shallow parse the SCT list for sanity. By the RFC
3600 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3601 * of the SCTs may be empty. */
3602 CBS copy = *contents;
3603 CBS sct_list;
3604 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3605 CBS_len(&copy) != 0 ||
3606 CBS_len(&sct_list) == 0) {
3607 return 0;
3608 }
3609
3610 while (CBS_len(&sct_list) > 0) {
3611 CBS sct;
3612 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3613 CBS_len(&sct) == 0) {
3614 return 0;
3615 }
3616 }
3617
3618 return 1;
3619}