blob: fe038b8b39d59bfc41198bc4d6bc46a48bfa1c3b [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin03973092014-06-24 23:27:17 -0400116#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400117#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/evp.h>
120#include <openssl/hmac.h>
121#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400122#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/rand.h>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700124#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400127#include "../crypto/internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800128
129
David Benjamin8c880a22016-12-03 02:20:34 -0500130static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
Adam Langley95c29f32014-06-20 12:00:00 -0700131
Adam Langleyfcf25832014-12-18 17:42:32 -0800132static int compare_uint16_t(const void *p1, const void *p2) {
133 uint16_t u1 = *((const uint16_t *)p1);
134 uint16_t u2 = *((const uint16_t *)p2);
135 if (u1 < u2) {
136 return -1;
137 } else if (u1 > u2) {
138 return 1;
139 } else {
140 return 0;
141 }
142}
David Benjamin35a7a442014-07-05 00:23:20 -0400143
Adam Langleyfcf25832014-12-18 17:42:32 -0800144/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
145 * more than one extension of the same type in a ClientHello or ServerHello.
146 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400147 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800148static int tls1_check_duplicate_extensions(const CBS *cbs) {
149 CBS extensions = *cbs;
150 size_t num_extensions = 0, i = 0;
151 uint16_t *extension_types = NULL;
152 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400153
Adam Langleyfcf25832014-12-18 17:42:32 -0800154 /* First pass: count the extensions. */
155 while (CBS_len(&extensions) > 0) {
156 uint16_t type;
157 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400158
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 if (!CBS_get_u16(&extensions, &type) ||
160 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
161 goto done;
162 }
David Benjamin35a7a442014-07-05 00:23:20 -0400163
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 num_extensions++;
165 }
David Benjamin35a7a442014-07-05 00:23:20 -0400166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 if (num_extensions == 0) {
168 return 1;
169 }
David Benjamin9a373592014-07-25 04:27:53 -0400170
Brian Smith5ba06892016-02-07 09:36:04 -1000171 extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800172 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400173 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800174 goto done;
175 }
David Benjamin35a7a442014-07-05 00:23:20 -0400176
Adam Langleyfcf25832014-12-18 17:42:32 -0800177 /* Second pass: gather the extension types. */
178 extensions = *cbs;
179 for (i = 0; i < num_extensions; i++) {
180 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400181
Adam Langleyfcf25832014-12-18 17:42:32 -0800182 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
183 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
184 /* This should not happen. */
185 goto done;
186 }
187 }
188 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400189
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 /* Sort the extensions and make sure there are no duplicates. */
191 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
192 for (i = 1; i < num_extensions; i++) {
193 if (extension_types[i - 1] == extension_types[i]) {
194 goto done;
195 }
196 }
David Benjamin35a7a442014-07-05 00:23:20 -0400197
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 ret = 1;
199
David Benjamin35a7a442014-07-05 00:23:20 -0400200done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400201 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 return ret;
203}
David Benjamin35a7a442014-07-05 00:23:20 -0400204
David Benjamin731058e2016-12-03 23:15:13 -0500205int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out, const uint8_t *in,
206 size_t in_len) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500207 OPENSSL_memset(out, 0, sizeof(*out));
David Benjamin731058e2016-12-03 23:15:13 -0500208 out->ssl = ssl;
209 out->client_hello = in;
210 out->client_hello_len = in_len;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400211
David Benjamine14ff062016-08-09 16:21:24 -0400212 CBS client_hello, random, session_id;
David Benjamin731058e2016-12-03 23:15:13 -0500213 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
214 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjamine14ff062016-08-09 16:21:24 -0400215 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
216 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
217 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 return 0;
219 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700220
David Benjamin731058e2016-12-03 23:15:13 -0500221 out->random = CBS_data(&random);
222 out->random_len = CBS_len(&random);
223 out->session_id = CBS_data(&session_id);
224 out->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 /* Skip past DTLS cookie */
David Benjamin731058e2016-12-03 23:15:13 -0500227 if (SSL_is_dtls(out->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800228 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400229 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
230 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 return 0;
232 }
233 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700234
David Benjamine14ff062016-08-09 16:21:24 -0400235 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800236 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400237 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
238 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 CBS_len(&compression_methods) < 1) {
240 return 0;
241 }
David Benjamine14ff062016-08-09 16:21:24 -0400242
David Benjamin731058e2016-12-03 23:15:13 -0500243 out->cipher_suites = CBS_data(&cipher_suites);
244 out->cipher_suites_len = CBS_len(&cipher_suites);
245 out->compression_methods = CBS_data(&compression_methods);
246 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 /* If the ClientHello ends here then it's valid, but doesn't have any
249 * extensions. (E.g. SSLv3.) */
250 if (CBS_len(&client_hello) == 0) {
David Benjamin731058e2016-12-03 23:15:13 -0500251 out->extensions = NULL;
252 out->extensions_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800253 return 1;
254 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700255
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 /* Extract extensions and check it is valid. */
David Benjamine14ff062016-08-09 16:21:24 -0400257 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
259 !tls1_check_duplicate_extensions(&extensions) ||
260 CBS_len(&client_hello) != 0) {
261 return 0;
262 }
David Benjamine14ff062016-08-09 16:21:24 -0400263
David Benjamin731058e2016-12-03 23:15:13 -0500264 out->extensions = CBS_data(&extensions);
265 out->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700266
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 return 1;
268}
Adam Langleydc9b1412014-06-20 12:00:00 -0700269
David Benjamin731058e2016-12-03 23:15:13 -0500270int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
271 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 CBS extensions;
David Benjamin731058e2016-12-03 23:15:13 -0500273 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 while (CBS_len(&extensions) != 0) {
David Benjamincec73442016-08-02 17:41:33 -0400275 /* Decode the next extension. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 uint16_t type;
277 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 if (!CBS_get_u16(&extensions, &type) ||
279 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
280 return 0;
281 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700282
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400284 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 return 1;
286 }
287 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700288
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 return 0;
290}
Adam Langley95c29f32014-06-20 12:00:00 -0700291
David Benjamin731058e2016-12-03 23:15:13 -0500292int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
293 uint16_t extension_type,
294 const uint8_t **out_data,
295 size_t *out_len) {
David Benjamincec73442016-08-02 17:41:33 -0400296 CBS cbs;
David Benjamin731058e2016-12-03 23:15:13 -0500297 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
David Benjamincec73442016-08-02 17:41:33 -0400298 return 0;
299 }
300
301 *out_data = CBS_data(&cbs);
302 *out_len = CBS_len(&cbs);
303 return 1;
304}
305
Steven Valdezce902a92016-05-17 11:47:53 -0400306static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400307 SSL_CURVE_X25519,
308 SSL_CURVE_SECP256R1,
309 SSL_CURVE_SECP384R1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800310};
Adam Langley95c29f32014-06-20 12:00:00 -0700311
David Benjaminf04976b2016-10-07 00:37:55 -0400312void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
Steven Valdez5440fe02016-07-18 12:40:30 -0400313 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400314 *out_group_ids = ssl->supported_group_list;
315 *out_group_ids_len = ssl->supported_group_list_len;
316 if (!*out_group_ids) {
317 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400318 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 }
320}
David Benjamined439582014-07-14 19:13:02 -0400321
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900322int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
323 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400324 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400325
David Benjaminf04976b2016-10-07 00:37:55 -0400326 const uint16_t *groups, *pref, *supp;
327 size_t groups_len, pref_len, supp_len;
328 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800329
David Benjaminf04976b2016-10-07 00:37:55 -0400330 /* Clients are not required to send a supported_groups extension. In this
331 * case, the server is free to pick any group it likes. See RFC 4492,
332 * section 4, paragraph 3.
333 *
334 * However, in the interests of compatibility, we will skip ECDH if the
335 * client didn't send an extension because we can't be sure that they'll
336 * support our favoured group. Thus we do not special-case an emtpy
337 * |peer_supported_group_list|. */
David Benjamin55a43642015-04-20 14:45:55 -0400338
David Benjamin4298d772015-12-19 00:18:25 -0500339 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400340 pref = groups;
341 pref_len = groups_len;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900342 supp = hs->peer_supported_group_list;
343 supp_len = hs->peer_supported_group_list_len;
David Benjamin55a43642015-04-20 14:45:55 -0400344 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900345 pref = hs->peer_supported_group_list;
346 pref_len = hs->peer_supported_group_list_len;
Steven Valdezce902a92016-05-17 11:47:53 -0400347 supp = groups;
348 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400349 }
350
David Benjaminf04976b2016-10-07 00:37:55 -0400351 for (size_t i = 0; i < pref_len; i++) {
352 for (size_t j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800353 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400354 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500355 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800356 }
357 }
358 }
359
David Benjamin4298d772015-12-19 00:18:25 -0500360 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800361}
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Steven Valdezce902a92016-05-17 11:47:53 -0400363int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400365 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800366
Steven Valdezce902a92016-05-17 11:47:53 -0400367 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
368 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 return 0;
370 }
371
David Benjamin54091232016-09-05 12:47:25 -0400372 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400373 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
374 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 return 0;
376 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 }
378
Steven Valdezce902a92016-05-17 11:47:53 -0400379 OPENSSL_free(*out_group_ids);
380 *out_group_ids = group_ids;
381 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800382
383 return 1;
384}
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100386int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
387 const char *curves) {
388 uint16_t *group_ids = NULL;
389 size_t ncurves = 0;
390
391 const char *col;
392 const char *ptr = curves;
393
394 do {
395 col = strchr(ptr, ':');
396
397 uint16_t group_id;
398 if (!ssl_name_to_group_id(&group_id, ptr,
399 col ? (size_t)(col - ptr) : strlen(ptr))) {
400 goto err;
401 }
402
403 uint16_t *new_group_ids = OPENSSL_realloc(group_ids,
404 (ncurves + 1) * sizeof(uint16_t));
405 if (new_group_ids == NULL) {
406 goto err;
407 }
408 group_ids = new_group_ids;
409
410 group_ids[ncurves] = group_id;
411 ncurves++;
412
413 if (col) {
414 ptr = col + 1;
415 }
416 } while (col);
417
418 OPENSSL_free(*out_group_ids);
419 *out_group_ids = group_ids;
420 *out_group_ids_len = ncurves;
421
422 return 1;
423
424err:
425 OPENSSL_free(group_ids);
426 return 0;
427}
428
Steven Valdezce902a92016-05-17 11:47:53 -0400429int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
430 const uint16_t *groups;
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400431 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -0400432 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400433 for (size_t i = 0; i < groups_len; i++) {
434 if (groups[i] == group_id) {
435 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800436 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 }
David Benjamin033e5f42014-11-13 18:47:41 -0500438
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400439 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800440}
David Benjamin033e5f42014-11-13 18:47:41 -0500441
David Benjamin3ef76972016-10-17 17:59:54 -0400442/* kVerifySignatureAlgorithms is the default list of accepted signature
David Benjamin3a322f52016-10-26 12:45:35 -0400443 * algorithms for verifying.
444 *
445 * For now, RSA-PSS signature algorithms are not enabled on Android's system
446 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
447 * restore them. */
David Benjamin3ef76972016-10-17 17:59:54 -0400448static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjamin69522112017-03-28 15:38:29 -0500449 /* List our preferred algorithms first. */
450 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400451 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400452#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400453 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400454#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400455 SSL_SIGN_RSA_PKCS1_SHA256,
456
457 /* Larger hashes are acceptable. */
458 SSL_SIGN_ECDSA_SECP384R1_SHA384,
459#if !defined(BORINGSSL_ANDROID_SYSTEM)
460 SSL_SIGN_RSA_PSS_SHA384,
461#endif
462 SSL_SIGN_RSA_PKCS1_SHA384,
463
David Benjamin76bb1412016-09-08 16:03:49 -0400464 /* TODO(davidben): Remove this. */
David Benjamin3ef76972016-10-17 17:59:54 -0400465#if defined(BORINGSSL_ANDROID_SYSTEM)
466 SSL_SIGN_ECDSA_SECP521R1_SHA512,
467#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400468#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400469 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400470#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400471 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400472
David Benjamin3a322f52016-10-26 12:45:35 -0400473 /* For now, SHA-1 is still accepted but least preferable. */
474 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400475
David Benjamin3a322f52016-10-26 12:45:35 -0400476};
477
478/* kSignSignatureAlgorithms is the default list of supported signature
479 * algorithms for signing.
480 *
481 * For now, RSA-PSS signature algorithms are not enabled on Android's system
482 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
483 * restore them. */
484static const uint16_t kSignSignatureAlgorithms[] = {
David Benjamin69522112017-03-28 15:38:29 -0500485 /* List our preferred algorithms first. */
486 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400487 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400488#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400489 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400490#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400491 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400492
David Benjamin3a322f52016-10-26 12:45:35 -0400493 /* If needed, sign larger hashes.
494 *
495 * TODO(davidben): Determine which of these may be pruned. */
496 SSL_SIGN_ECDSA_SECP384R1_SHA384,
497#if !defined(BORINGSSL_ANDROID_SYSTEM)
498 SSL_SIGN_RSA_PSS_SHA384,
499#endif
500 SSL_SIGN_RSA_PKCS1_SHA384,
501
502 SSL_SIGN_ECDSA_SECP521R1_SHA512,
503#if !defined(BORINGSSL_ANDROID_SYSTEM)
504 SSL_SIGN_RSA_PSS_SHA512,
505#endif
506 SSL_SIGN_RSA_PKCS1_SHA512,
507
508 /* If the peer supports nothing else, sign with SHA-1. */
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400509 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400510 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400511};
512
David Benjamin69522112017-03-28 15:38:29 -0500513void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
514 ctx->ed25519_enabled = !!enabled;
515}
516
517int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) {
518 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms); i++) {
519 if (kVerifySignatureAlgorithms[i] == SSL_SIGN_ED25519 &&
520 !ssl->ctx->ed25519_enabled) {
521 continue;
522 }
523 if (!CBB_add_u16(out, kVerifySignatureAlgorithms[i])) {
524 return 0;
525 }
526 }
527
528 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800529}
Adam Langley95c29f32014-06-20 12:00:00 -0700530
David Benjamin887c3002016-07-08 16:15:32 -0700531int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
David Benjamin69522112017-03-28 15:38:29 -0500532 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms); i++) {
533 if (kVerifySignatureAlgorithms[i] == SSL_SIGN_ED25519 &&
534 !ssl->ctx->ed25519_enabled) {
535 continue;
536 }
537 if (sigalg == kVerifySignatureAlgorithms[i]) {
David Benjamin3ef76972016-10-17 17:59:54 -0400538 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 }
540 }
541
David Benjamin3ef76972016-10-17 17:59:54 -0400542 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
543 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
544 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800545}
546
Adam Langley614c66a2015-06-12 15:26:58 -0700547/* tls_extension represents a TLS extension that is handled internally. The
548 * |init| function is called for each handshake, before any other functions of
549 * the extension. Then the add and parse callbacks are called as needed.
550 *
551 * The parse callbacks receive a |CBS| that contains the contents of the
552 * extension (i.e. not including the type and length bytes). If an extension is
553 * not received then the parse callbacks will be called with a NULL CBS so that
554 * they can do any processing needed to handle the absence of an extension.
555 *
556 * The add callbacks receive a |CBB| to which the extension can be appended but
557 * the function is responsible for appending the type and length bytes too.
558 *
559 * All callbacks return one for success and zero for error. If a parse function
560 * returns zero then a fatal alert with value |*out_alert| will be sent. If
561 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
562struct tls_extension {
563 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500564 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700565
David Benjamin8c880a22016-12-03 02:20:34 -0500566 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
567 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
568 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700569
David Benjamin8c880a22016-12-03 02:20:34 -0500570 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
571 CBS *contents);
572 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700573};
574
David Benjamin8c880a22016-12-03 02:20:34 -0500575static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
576 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400577 if (contents != NULL) {
578 /* Servers MUST NOT send this extension. */
579 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
580 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
581 return 0;
582 }
583
584 return 1;
585}
586
David Benjamin8c880a22016-12-03 02:20:34 -0500587static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
588 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400589 /* This extension from the client is handled elsewhere. */
590 return 1;
591}
592
David Benjamin8c880a22016-12-03 02:20:34 -0500593static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400594 return 1;
595}
Adam Langley614c66a2015-06-12 15:26:58 -0700596
597/* Server name indication (SNI).
598 *
599 * https://tools.ietf.org/html/rfc6066#section-3. */
600
David Benjamin8c880a22016-12-03 02:20:34 -0500601static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
602 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700603 if (ssl->tlsext_hostname == NULL) {
604 return 1;
605 }
606
607 CBB contents, server_name_list, name;
608 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
609 !CBB_add_u16_length_prefixed(out, &contents) ||
610 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
611 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
612 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
613 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
614 strlen(ssl->tlsext_hostname)) ||
615 !CBB_flush(out)) {
616 return 0;
617 }
618
619 return 1;
620}
621
David Benjamin8c880a22016-12-03 02:20:34 -0500622static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500623 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500624 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700625 if (contents == NULL) {
626 return 1;
627 }
628
629 if (CBS_len(contents) != 0) {
630 return 0;
631 }
632
633 assert(ssl->tlsext_hostname != NULL);
634
Steven Valdez87eab492016-06-27 16:34:59 -0400635 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500636 OPENSSL_free(hs->new_session->tlsext_hostname);
637 hs->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
638 if (!hs->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700639 *out_alert = SSL_AD_INTERNAL_ERROR;
640 return 0;
641 }
642 }
643
644 return 1;
645}
646
David Benjamin8c880a22016-12-03 02:20:34 -0500647static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500648 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700649 if (contents == NULL) {
650 return 1;
651 }
652
David Benjamin9b611e22016-03-03 08:48:30 -0500653 CBS server_name_list, host_name;
654 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700655 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500656 !CBS_get_u8(&server_name_list, &name_type) ||
657 /* Although the server_name extension was intended to be extensible to
658 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
659 * different name types will cause an error. Further, RFC 4366 originally
660 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
661 * adding new name types is no longer feasible.
662 *
663 * Act as if the extensibility does not exist to simplify parsing. */
664 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
665 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700666 CBS_len(contents) != 0) {
667 return 0;
668 }
669
David Benjamin9b611e22016-03-03 08:48:30 -0500670 if (name_type != TLSEXT_NAMETYPE_host_name ||
671 CBS_len(&host_name) == 0 ||
672 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
673 CBS_contains_zero_byte(&host_name)) {
674 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
675 return 0;
676 }
Adam Langley614c66a2015-06-12 15:26:58 -0700677
David Benjamin4eb95cc2016-11-16 17:08:23 +0900678 /* Copy the hostname as a string. */
David Benjamin8c880a22016-12-03 02:20:34 -0500679 if (!CBS_strdup(&host_name, &hs->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900680 *out_alert = SSL_AD_INTERNAL_ERROR;
681 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700682 }
683
David Benjamin8c880a22016-12-03 02:20:34 -0500684 hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700685 return 1;
686}
687
David Benjamin8c880a22016-12-03 02:20:34 -0500688static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
689 if (hs->ssl->s3->session_reused ||
690 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700691 return 1;
692 }
693
694 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
695 !CBB_add_u16(out, 0 /* length */)) {
696 return 0;
697 }
698
699 return 1;
700}
701
702
Adam Langley5021b222015-06-12 18:27:58 -0700703/* Renegotiation indication.
704 *
705 * https://tools.ietf.org/html/rfc5746 */
706
David Benjamin8c880a22016-12-03 02:20:34 -0500707static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
708 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400709 uint16_t min_version, max_version;
710 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
711 return 0;
712 }
713
714 /* Renegotiation indication is not necessary in TLS 1.3. */
715 if (min_version >= TLS1_3_VERSION) {
716 return 1;
717 }
718
David Benjamin52bf6902016-10-08 12:05:03 -0400719 assert(ssl->s3->initial_handshake_complete ==
720 (ssl->s3->previous_client_finished_len != 0));
721
Adam Langley5021b222015-06-12 18:27:58 -0700722 CBB contents, prev_finished;
723 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
724 !CBB_add_u16_length_prefixed(out, &contents) ||
725 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
726 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
727 ssl->s3->previous_client_finished_len) ||
728 !CBB_flush(out)) {
729 return 0;
730 }
731
732 return 1;
733}
734
David Benjamin8c880a22016-12-03 02:20:34 -0500735static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700736 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500737 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400738 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez246eeee2017-03-26 12:49:17 -0500739 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400740 return 0;
741 }
742
David Benjamin3e052de2015-11-25 20:10:31 -0500743 /* Servers may not switch between omitting the extension and supporting it.
744 * See RFC 5746, sections 3.5 and 4.2. */
745 if (ssl->s3->initial_handshake_complete &&
746 (contents != NULL) != ssl->s3->send_connection_binding) {
747 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
748 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
749 return 0;
750 }
751
Adam Langley5021b222015-06-12 18:27:58 -0700752 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500753 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700754 * RI even on initial ServerHello because the client doesn't see any
755 * renegotiation during an attack. However this would mean we could not
756 * connect to any server which doesn't support RI.
757 *
David Benjamine9cddb82015-11-23 14:36:40 -0500758 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
759 * practical terms every client sets it so it's just assumed here. */
760 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700761 }
762
763 const size_t expected_len = ssl->s3->previous_client_finished_len +
764 ssl->s3->previous_server_finished_len;
765
766 /* Check for logic errors */
767 assert(!expected_len || ssl->s3->previous_client_finished_len);
768 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400769 assert(ssl->s3->initial_handshake_complete ==
770 (ssl->s3->previous_client_finished_len != 0));
771 assert(ssl->s3->initial_handshake_complete ==
772 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700773
774 /* Parse out the extension contents. */
775 CBS renegotiated_connection;
776 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
777 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400778 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700779 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
780 return 0;
781 }
782
783 /* Check that the extension matches. */
784 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400785 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700786 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
787 return 0;
788 }
789
790 const uint8_t *d = CBS_data(&renegotiated_connection);
791 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
792 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400793 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700794 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
795 return 0;
796 }
797 d += ssl->s3->previous_client_finished_len;
798
799 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
800 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400801 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700802 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
803 return 0;
804 }
805 ssl->s3->send_connection_binding = 1;
806
807 return 1;
808}
809
David Benjamin8c880a22016-12-03 02:20:34 -0500810static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700811 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500812 SSL *const ssl = hs->ssl;
Adam Langley5021b222015-06-12 18:27:58 -0700813 /* Renegotiation isn't supported as a server so this function should never be
814 * called after the initial handshake. */
815 assert(!ssl->s3->initial_handshake_complete);
816
Steven Valdez143e8b32016-07-11 13:19:03 -0400817 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
818 return 1;
819 }
820
Adam Langley5021b222015-06-12 18:27:58 -0700821 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400822 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700823 }
824
825 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700826 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
827 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400828 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700829 return 0;
830 }
831
David Benjamin52bf6902016-10-08 12:05:03 -0400832 /* Check that the extension matches. We do not support renegotiation as a
833 * server, so this must be empty. */
834 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400835 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700836 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
837 return 0;
838 }
839
840 ssl->s3->send_connection_binding = 1;
841
842 return 1;
843}
844
David Benjamin8c880a22016-12-03 02:20:34 -0500845static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
846 SSL *const ssl = hs->ssl;
David Benjamin52bf6902016-10-08 12:05:03 -0400847 /* Renegotiation isn't supported as a server so this function should never be
848 * called after the initial handshake. */
849 assert(!ssl->s3->initial_handshake_complete);
850
Steven Valdez143e8b32016-07-11 13:19:03 -0400851 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
852 return 1;
853 }
854
Adam Langley5021b222015-06-12 18:27:58 -0700855 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400856 !CBB_add_u16(out, 1 /* length */) ||
857 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700858 return 0;
859 }
860
861 return 1;
862}
863
Adam Langley0a056712015-07-01 15:03:33 -0700864
865/* Extended Master Secret.
866 *
David Benjamin43946d42016-02-01 08:42:19 -0500867 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700868
David Benjamin8c880a22016-12-03 02:20:34 -0500869static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400870 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -0500871 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400872 return 0;
873 }
874
875 /* Extended master secret is not necessary in TLS 1.3. */
876 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700877 return 1;
878 }
879
880 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
881 !CBB_add_u16(out, 0 /* length */)) {
882 return 0;
883 }
884
885 return 1;
886}
887
David Benjamin8c880a22016-12-03 02:20:34 -0500888static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700889 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500890 SSL *const ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500891
892 if (contents != NULL) {
893 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
894 ssl->version == SSL3_VERSION ||
895 CBS_len(contents) != 0) {
David Benjamin163c9562016-08-29 23:14:17 -0400896 return 0;
897 }
898
David Benjaminfc02b592017-02-17 16:26:01 -0500899 hs->extended_master_secret = 1;
David Benjamin163c9562016-08-29 23:14:17 -0400900 }
901
David Benjaminfc02b592017-02-17 16:26:01 -0500902 /* Whether EMS is negotiated may not change on renegotiation. */
903 if (ssl->s3->established_session != NULL &&
904 hs->extended_master_secret !=
905 ssl->s3->established_session->extended_master_secret) {
906 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
907 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400908 return 0;
909 }
910
Adam Langley0a056712015-07-01 15:03:33 -0700911 return 1;
912}
913
David Benjamin8c880a22016-12-03 02:20:34 -0500914static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500915 CBS *contents) {
David Benjaminfc02b592017-02-17 16:26:01 -0500916 uint16_t version = ssl3_protocol_version(hs->ssl);
David Benjamin8c880a22016-12-03 02:20:34 -0500917 if (version >= TLS1_3_VERSION ||
918 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400919 return 1;
920 }
921
922 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700923 return 1;
924 }
925
926 if (CBS_len(contents) != 0) {
927 return 0;
928 }
929
David Benjaminfc02b592017-02-17 16:26:01 -0500930 hs->extended_master_secret = 1;
Adam Langley0a056712015-07-01 15:03:33 -0700931 return 1;
932}
933
David Benjamin8c880a22016-12-03 02:20:34 -0500934static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -0500935 if (!hs->extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700936 return 1;
937 }
938
939 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
940 !CBB_add_u16(out, 0 /* length */)) {
941 return 0;
942 }
943
944 return 1;
945}
946
Adam Langley9b05bc52015-07-01 15:25:33 -0700947
948/* Session tickets.
949 *
950 * https://tools.ietf.org/html/rfc5077 */
951
David Benjamin8c880a22016-12-03 02:20:34 -0500952static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
953 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400954 uint16_t min_version, max_version;
955 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
956 return 0;
957 }
958
959 /* TLS 1.3 uses a different ticket extension. */
960 if (min_version >= TLS1_3_VERSION ||
961 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700962 return 1;
963 }
964
965 const uint8_t *ticket_data = NULL;
966 int ticket_len = 0;
967
968 /* Renegotiation does not participate in session resumption. However, still
969 * advertise the extension to avoid potentially breaking servers which carry
970 * over the state from the previous handshake, such as OpenSSL servers
971 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -0400972 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -0700973 if (!ssl->s3->initial_handshake_complete &&
974 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400975 ssl->session->tlsext_tick != NULL &&
976 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -0400977 ssl->method->version_from_wire(&session_version,
978 ssl->session->ssl_version) &&
979 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700980 ticket_data = ssl->session->tlsext_tick;
981 ticket_len = ssl->session->tlsext_ticklen;
982 }
983
984 CBB ticket;
985 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
986 !CBB_add_u16_length_prefixed(out, &ticket) ||
987 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
988 !CBB_flush(out)) {
989 return 0;
990 }
991
992 return 1;
993}
994
David Benjamin8c880a22016-12-03 02:20:34 -0500995static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -0700996 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500997 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -0700998 if (contents == NULL) {
999 return 1;
1000 }
1001
Steven Valdez143e8b32016-07-11 13:19:03 -04001002 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1003 return 0;
1004 }
1005
Adam Langley9b05bc52015-07-01 15:25:33 -07001006 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1007 * this function should never be called, even if the server tries to send the
1008 * extension. */
1009 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1010
1011 if (CBS_len(contents) != 0) {
1012 return 0;
1013 }
1014
David Benjamin8c880a22016-12-03 02:20:34 -05001015 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001016 return 1;
1017}
1018
David Benjamin8c880a22016-12-03 02:20:34 -05001019static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1020 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001021 return 1;
1022 }
1023
David Benjamin78476f62016-11-12 11:20:55 +09001024 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin8c880a22016-12-03 02:20:34 -05001025 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001026
1027 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1028 !CBB_add_u16(out, 0 /* length */)) {
1029 return 0;
1030 }
1031
1032 return 1;
1033}
1034
1035
Adam Langley2e857bd2015-07-01 16:09:19 -07001036/* Signature Algorithms.
1037 *
1038 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1039
David Benjamin8c880a22016-12-03 02:20:34 -05001040static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1041 SSL *const ssl = hs->ssl;
David Benjamin2dc02042016-09-19 19:57:37 -04001042 uint16_t min_version, max_version;
1043 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1044 return 0;
1045 }
1046
1047 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001048 return 1;
1049 }
1050
David Benjamin0fc37ef2016-08-17 15:29:46 -04001051 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001052 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1053 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin69522112017-03-28 15:38:29 -05001054 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
1055 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
1056 !CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001057 return 0;
1058 }
1059
1060 return 1;
1061}
1062
David Benjamin8c880a22016-12-03 02:20:34 -05001063static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001064 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001065 OPENSSL_free(hs->peer_sigalgs);
1066 hs->peer_sigalgs = NULL;
1067 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001068
Adam Langley2e857bd2015-07-01 16:09:19 -07001069 if (contents == NULL) {
1070 return 1;
1071 }
1072
1073 CBS supported_signature_algorithms;
1074 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001075 CBS_len(contents) != 0 ||
1076 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001077 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001078 return 0;
1079 }
1080
1081 return 1;
1082}
1083
Adam Langley2e857bd2015-07-01 16:09:19 -07001084
Adam Langleybb0bd042015-07-01 16:21:03 -07001085/* OCSP Stapling.
1086 *
1087 * https://tools.ietf.org/html/rfc6066#section-8 */
1088
David Benjamin8c880a22016-12-03 02:20:34 -05001089static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1090 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001091 if (!ssl->ocsp_stapling_enabled) {
1092 return 1;
1093 }
1094
1095 CBB contents;
1096 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1097 !CBB_add_u16_length_prefixed(out, &contents) ||
1098 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1099 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1100 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1101 !CBB_flush(out)) {
1102 return 0;
1103 }
1104
1105 return 1;
1106}
1107
David Benjamin8c880a22016-12-03 02:20:34 -05001108static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001109 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001110 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001111 if (contents == NULL) {
1112 return 1;
1113 }
1114
Steven Valdeza833c352016-11-01 13:39:36 -04001115 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1116 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001117 return 0;
1118 }
1119
Steven Valdeza833c352016-11-01 13:39:36 -04001120 /* OCSP stapling is forbidden on non-certificate ciphers. */
1121 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001122 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001123 return 0;
1124 }
1125
Steven Valdeza833c352016-11-01 13:39:36 -04001126 /* Note this does not check for resumption in TLS 1.2. Sending
1127 * status_request here does not make sense, but OpenSSL does so and the
1128 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001129
David Benjamin8c880a22016-12-03 02:20:34 -05001130 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001131 return 1;
1132}
1133
David Benjamin8c880a22016-12-03 02:20:34 -05001134static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001135 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001136 if (contents == NULL) {
1137 return 1;
1138 }
1139
1140 uint8_t status_type;
1141 if (!CBS_get_u8(contents, &status_type)) {
1142 return 0;
1143 }
1144
1145 /* We cannot decide whether OCSP stapling will occur yet because the correct
1146 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001147 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001148
Adam Langleybb0bd042015-07-01 16:21:03 -07001149 return 1;
1150}
1151
David Benjamin8c880a22016-12-03 02:20:34 -05001152static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1153 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001154 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001155 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001156 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001157 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001158 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001159 return 1;
1160 }
1161
David Benjamin8c880a22016-12-03 02:20:34 -05001162 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001163
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001164 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001165 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001166}
1167
1168
Adam Langley97dfcbf2015-07-01 18:35:20 -07001169/* Next protocol negotiation.
1170 *
1171 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1172
David Benjamin8c880a22016-12-03 02:20:34 -05001173static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1174 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001175 if (ssl->s3->initial_handshake_complete ||
1176 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001177 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001178 return 1;
1179 }
1180
1181 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1182 !CBB_add_u16(out, 0 /* length */)) {
1183 return 0;
1184 }
1185
1186 return 1;
1187}
1188
David Benjamin8c880a22016-12-03 02:20:34 -05001189static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001190 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001191 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001192 if (contents == NULL) {
1193 return 1;
1194 }
1195
Steven Valdez143e8b32016-07-11 13:19:03 -04001196 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1197 return 0;
1198 }
1199
Adam Langley97dfcbf2015-07-01 18:35:20 -07001200 /* If any of these are false then we should never have sent the NPN
1201 * extension in the ClientHello and thus this function should never have been
1202 * called. */
1203 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001204 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001205 assert(ssl->ctx->next_proto_select_cb != NULL);
1206
David Benjamin76c2efc2015-08-31 14:24:29 -04001207 if (ssl->s3->alpn_selected != NULL) {
1208 /* NPN and ALPN may not be negotiated in the same connection. */
1209 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1210 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1211 return 0;
1212 }
1213
Adam Langley97dfcbf2015-07-01 18:35:20 -07001214 const uint8_t *const orig_contents = CBS_data(contents);
1215 const size_t orig_len = CBS_len(contents);
1216
1217 while (CBS_len(contents) != 0) {
1218 CBS proto;
1219 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1220 CBS_len(&proto) == 0) {
1221 return 0;
1222 }
1223 }
1224
1225 uint8_t *selected;
1226 uint8_t selected_len;
1227 if (ssl->ctx->next_proto_select_cb(
1228 ssl, &selected, &selected_len, orig_contents, orig_len,
1229 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1230 *out_alert = SSL_AD_INTERNAL_ERROR;
1231 return 0;
1232 }
1233
David Benjamin79978df2015-12-25 15:56:49 -05001234 OPENSSL_free(ssl->s3->next_proto_negotiated);
1235 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1236 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001237 *out_alert = SSL_AD_INTERNAL_ERROR;
1238 return 0;
1239 }
1240
David Benjamin79978df2015-12-25 15:56:49 -05001241 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001242 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001243
1244 return 1;
1245}
1246
David Benjamin8c880a22016-12-03 02:20:34 -05001247static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001248 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001249 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001250 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1251 return 1;
1252 }
1253
Adam Langley97dfcbf2015-07-01 18:35:20 -07001254 if (contents != NULL && CBS_len(contents) != 0) {
1255 return 0;
1256 }
1257
1258 if (contents == NULL ||
1259 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001260 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001261 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001262 return 1;
1263 }
1264
David Benjamin8c880a22016-12-03 02:20:34 -05001265 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001266 return 1;
1267}
1268
David Benjamin8c880a22016-12-03 02:20:34 -05001269static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1270 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001271 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1272 * parsed. */
David Benjamin8c880a22016-12-03 02:20:34 -05001273 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001274 return 1;
1275 }
1276
1277 const uint8_t *npa;
1278 unsigned npa_len;
1279
1280 if (ssl->ctx->next_protos_advertised_cb(
1281 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1282 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001283 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001284 return 1;
1285 }
1286
1287 CBB contents;
1288 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1289 !CBB_add_u16_length_prefixed(out, &contents) ||
1290 !CBB_add_bytes(&contents, npa, npa_len) ||
1291 !CBB_flush(out)) {
1292 return 0;
1293 }
1294
1295 return 1;
1296}
1297
1298
Adam Langleyab8d87d2015-07-10 12:21:39 -07001299/* Signed certificate timestamps.
1300 *
1301 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1302
David Benjamin8c880a22016-12-03 02:20:34 -05001303static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1304 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001305 if (!ssl->signed_cert_timestamps_enabled) {
1306 return 1;
1307 }
1308
1309 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1310 !CBB_add_u16(out, 0 /* length */)) {
1311 return 0;
1312 }
1313
1314 return 1;
1315}
1316
David Benjamin8c880a22016-12-03 02:20:34 -05001317static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001318 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001319 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001320 if (contents == NULL) {
1321 return 1;
1322 }
1323
Steven Valdeza833c352016-11-01 13:39:36 -04001324 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1325 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001326 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001327 return 0;
1328 }
1329
Adam Langleyab8d87d2015-07-10 12:21:39 -07001330 /* If this is false then we should never have sent the SCT extension in the
1331 * ClientHello and thus this function should never have been called. */
1332 assert(ssl->signed_cert_timestamps_enabled);
1333
Adam Langleycfa08c32016-11-17 13:21:27 -08001334 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001335 *out_alert = SSL_AD_DECODE_ERROR;
1336 return 0;
1337 }
1338
David Benjamindaa88502016-10-04 16:32:16 -04001339 /* Session resumption uses the original session information. The extension
1340 * should not be sent on resumption, but RFC 6962 did not make it a
1341 * requirement, so tolerate this.
1342 *
1343 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001344 if (!ssl->s3->session_reused &&
David Benjamin45738dd2017-02-09 20:01:26 -05001345 !CBS_stow(contents, &hs->new_session->tlsext_signed_cert_timestamp_list,
1346 &hs->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001347 *out_alert = SSL_AD_INTERNAL_ERROR;
1348 return 0;
1349 }
1350
1351 return 1;
1352}
1353
David Benjamin8c880a22016-12-03 02:20:34 -05001354static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001355 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001356 if (contents == NULL) {
1357 return 1;
1358 }
1359
1360 if (CBS_len(contents) != 0) {
1361 return 0;
1362 }
1363
David Benjamin8c880a22016-12-03 02:20:34 -05001364 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001365 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001366}
1367
David Benjamin8c880a22016-12-03 02:20:34 -05001368static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1369 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001370 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001371 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1372 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001373 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001374 return 1;
1375 }
1376
1377 CBB contents;
1378 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1379 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001380 CBB_add_bytes(
1381 &contents,
1382 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1383 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001384 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001385}
1386
1387
Adam Langleyf18e4532015-07-10 13:39:53 -07001388/* Application-level Protocol Negotiation.
1389 *
1390 * https://tools.ietf.org/html/rfc7301 */
1391
David Benjamin8c880a22016-12-03 02:20:34 -05001392static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1393 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001394 if (ssl->alpn_client_proto_list == NULL ||
1395 ssl->s3->initial_handshake_complete) {
1396 return 1;
1397 }
1398
1399 CBB contents, proto_list;
1400 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1401 !CBB_add_u16_length_prefixed(out, &contents) ||
1402 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1403 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1404 ssl->alpn_client_proto_list_len) ||
1405 !CBB_flush(out)) {
1406 return 0;
1407 }
1408
1409 return 1;
1410}
1411
David Benjamin8c880a22016-12-03 02:20:34 -05001412static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001413 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001414 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001415 if (contents == NULL) {
1416 return 1;
1417 }
1418
1419 assert(!ssl->s3->initial_handshake_complete);
1420 assert(ssl->alpn_client_proto_list != NULL);
1421
David Benjamin8c880a22016-12-03 02:20:34 -05001422 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001423 /* NPN and ALPN may not be negotiated in the same connection. */
1424 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1425 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1426 return 0;
1427 }
1428
Adam Langleyf18e4532015-07-10 13:39:53 -07001429 /* The extension data consists of a ProtocolNameList which must have
1430 * exactly one ProtocolName. Each of these is length-prefixed. */
1431 CBS protocol_name_list, protocol_name;
1432 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1433 CBS_len(contents) != 0 ||
1434 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1435 /* Empty protocol names are forbidden. */
1436 CBS_len(&protocol_name) == 0 ||
1437 CBS_len(&protocol_name_list) != 0) {
1438 return 0;
1439 }
1440
David Benjaminc8ff30c2017-04-04 13:52:36 -04001441 if (!ssl->ctx->allow_unknown_alpn_protos) {
1442 /* Check that the protocol name is one of the ones we advertised. */
1443 int protocol_ok = 0;
1444 CBS client_protocol_name_list, client_protocol_name;
1445 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1446 ssl->alpn_client_proto_list_len);
1447 while (CBS_len(&client_protocol_name_list) > 0) {
1448 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1449 &client_protocol_name)) {
1450 *out_alert = SSL_AD_INTERNAL_ERROR;
1451 return 0;
1452 }
1453
1454 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1455 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1456 CBS_data(&protocol_name),
1457 CBS_len(&protocol_name)) == 0) {
1458 protocol_ok = 1;
1459 break;
1460 }
1461 }
1462
1463 if (!protocol_ok) {
1464 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1465 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3e517572016-08-11 11:52:23 -04001466 return 0;
1467 }
David Benjamin3e517572016-08-11 11:52:23 -04001468 }
1469
Adam Langleyf18e4532015-07-10 13:39:53 -07001470 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1471 &ssl->s3->alpn_selected_len)) {
1472 *out_alert = SSL_AD_INTERNAL_ERROR;
1473 return 0;
1474 }
1475
1476 return 1;
1477}
1478
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001479int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001480 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001481 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001482 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001483 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001484 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001485 client_hello, &contents,
1486 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1487 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001488 return 1;
1489 }
1490
1491 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001492 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001493
1494 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001495 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1496 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001497 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001498 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1499 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001500 return 0;
1501 }
1502
1503 /* Validate the protocol list. */
1504 CBS protocol_name_list_copy = protocol_name_list;
1505 while (CBS_len(&protocol_name_list_copy) > 0) {
1506 CBS protocol_name;
1507
1508 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1509 /* Empty protocol names are forbidden. */
1510 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001511 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1512 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001513 return 0;
1514 }
1515 }
1516
1517 const uint8_t *selected;
1518 uint8_t selected_len;
1519 if (ssl->ctx->alpn_select_cb(
1520 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1521 CBS_len(&protocol_name_list),
1522 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1523 OPENSSL_free(ssl->s3->alpn_selected);
1524 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1525 if (ssl->s3->alpn_selected == NULL) {
1526 *out_alert = SSL_AD_INTERNAL_ERROR;
1527 return 0;
1528 }
1529 ssl->s3->alpn_selected_len = selected_len;
1530 }
1531
1532 return 1;
1533}
1534
David Benjamin8c880a22016-12-03 02:20:34 -05001535static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1536 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001537 if (ssl->s3->alpn_selected == NULL) {
1538 return 1;
1539 }
1540
1541 CBB contents, proto_list, proto;
1542 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1543 !CBB_add_u16_length_prefixed(out, &contents) ||
1544 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1545 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001546 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1547 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001548 !CBB_flush(out)) {
1549 return 0;
1550 }
1551
1552 return 1;
1553}
1554
1555
Adam Langley49c7af12015-07-10 14:33:46 -07001556/* Channel ID.
1557 *
1558 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1559
David Benjamin8c880a22016-12-03 02:20:34 -05001560static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1561 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001562}
1563
David Benjamin8c880a22016-12-03 02:20:34 -05001564static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1565 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001566 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001567 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001568 return 1;
1569 }
1570
1571 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1572 !CBB_add_u16(out, 0 /* length */)) {
1573 return 0;
1574 }
1575
1576 return 1;
1577}
1578
David Benjamin8c880a22016-12-03 02:20:34 -05001579static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1580 uint8_t *out_alert, CBS *contents) {
1581 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001582 if (contents == NULL) {
1583 return 1;
1584 }
1585
David Benjamince079fd2016-08-02 16:22:34 -04001586 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001587 assert(ssl->tlsext_channel_id_enabled);
1588
1589 if (CBS_len(contents) != 0) {
1590 return 0;
1591 }
1592
1593 ssl->s3->tlsext_channel_id_valid = 1;
1594 return 1;
1595}
1596
David Benjamin8c880a22016-12-03 02:20:34 -05001597static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1598 uint8_t *out_alert, CBS *contents) {
1599 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001600 if (contents == NULL ||
1601 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001602 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001603 return 1;
1604 }
1605
1606 if (CBS_len(contents) != 0) {
1607 return 0;
1608 }
1609
1610 ssl->s3->tlsext_channel_id_valid = 1;
1611 return 1;
1612}
1613
David Benjamin8c880a22016-12-03 02:20:34 -05001614static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1615 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001616 if (!ssl->s3->tlsext_channel_id_valid) {
1617 return 1;
1618 }
1619
1620 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1621 !CBB_add_u16(out, 0 /* length */)) {
1622 return 0;
1623 }
1624
1625 return 1;
1626}
1627
Adam Langley391250d2015-07-15 19:06:07 -07001628
1629/* Secure Real-time Transport Protocol (SRTP) extension.
1630 *
1631 * https://tools.ietf.org/html/rfc5764 */
1632
Adam Langley391250d2015-07-15 19:06:07 -07001633
David Benjamin8c880a22016-12-03 02:20:34 -05001634static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1635 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001636}
1637
David Benjamin8c880a22016-12-03 02:20:34 -05001638static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1639 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001640 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1641 if (profiles == NULL) {
1642 return 1;
1643 }
1644 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1645 if (num_profiles == 0) {
1646 return 1;
1647 }
1648
1649 CBB contents, profile_ids;
1650 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1651 !CBB_add_u16_length_prefixed(out, &contents) ||
1652 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1653 return 0;
1654 }
1655
David Benjamin54091232016-09-05 12:47:25 -04001656 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001657 if (!CBB_add_u16(&profile_ids,
1658 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1659 return 0;
1660 }
1661 }
1662
1663 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1664 !CBB_flush(out)) {
1665 return 0;
1666 }
1667
1668 return 1;
1669}
1670
David Benjamin8c880a22016-12-03 02:20:34 -05001671static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001672 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001673 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001674 if (contents == NULL) {
1675 return 1;
1676 }
1677
1678 /* The extension consists of a u16-prefixed profile ID list containing a
1679 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1680 *
1681 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1682 CBS profile_ids, srtp_mki;
1683 uint16_t profile_id;
1684 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1685 !CBS_get_u16(&profile_ids, &profile_id) ||
1686 CBS_len(&profile_ids) != 0 ||
1687 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1688 CBS_len(contents) != 0) {
1689 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1690 return 0;
1691 }
1692
1693 if (CBS_len(&srtp_mki) != 0) {
1694 /* Must be no MKI, since we never offer one. */
1695 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1696 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1697 return 0;
1698 }
1699
1700 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1701
1702 /* Check to see if the server gave us something we support (and presumably
1703 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001704 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001705 const SRTP_PROTECTION_PROFILE *profile =
1706 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1707
1708 if (profile->id == profile_id) {
1709 ssl->srtp_profile = profile;
1710 return 1;
1711 }
1712 }
1713
1714 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1715 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1716 return 0;
1717}
1718
David Benjamin8c880a22016-12-03 02:20:34 -05001719static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001720 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001721 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001722 if (contents == NULL) {
1723 return 1;
1724 }
1725
1726 CBS profile_ids, srtp_mki;
1727 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1728 CBS_len(&profile_ids) < 2 ||
1729 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1730 CBS_len(contents) != 0) {
1731 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1732 return 0;
1733 }
1734 /* Discard the MKI value for now. */
1735
1736 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1737 SSL_get_srtp_profiles(ssl);
1738
1739 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001740 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001741 const SRTP_PROTECTION_PROFILE *server_profile =
1742 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1743
1744 CBS profile_ids_tmp;
1745 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1746
1747 while (CBS_len(&profile_ids_tmp) > 0) {
1748 uint16_t profile_id;
1749 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1750 return 0;
1751 }
1752
1753 if (server_profile->id == profile_id) {
1754 ssl->srtp_profile = server_profile;
1755 return 1;
1756 }
1757 }
1758 }
1759
1760 return 1;
1761}
1762
David Benjamin8c880a22016-12-03 02:20:34 -05001763static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1764 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001765 if (ssl->srtp_profile == NULL) {
1766 return 1;
1767 }
1768
1769 CBB contents, profile_ids;
1770 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1771 !CBB_add_u16_length_prefixed(out, &contents) ||
1772 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1773 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1774 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1775 !CBB_flush(out)) {
1776 return 0;
1777 }
1778
1779 return 1;
1780}
1781
Adam Langleybdd5d662015-07-20 16:19:08 -07001782
1783/* EC point formats.
1784 *
1785 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1786
David Benjamin8c880a22016-12-03 02:20:34 -05001787static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001788 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001789 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1790 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001791 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1792 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001793 !CBB_flush(out)) {
1794 return 0;
1795 }
1796
1797 return 1;
1798}
1799
David Benjamin8c880a22016-12-03 02:20:34 -05001800static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001801 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -05001802 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin70aba262016-11-01 12:08:15 -04001803 return 0;
1804 }
1805
1806 /* The point format extension is unneccessary in TLS 1.3. */
1807 if (min_version >= TLS1_3_VERSION) {
1808 return 1;
1809 }
1810
David Benjamin8c880a22016-12-03 02:20:34 -05001811 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001812}
1813
David Benjamin8c880a22016-12-03 02:20:34 -05001814static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001815 CBS *contents) {
1816 if (contents == NULL) {
1817 return 1;
1818 }
1819
David Benjamin8c880a22016-12-03 02:20:34 -05001820 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001821 return 0;
1822 }
1823
Adam Langleybdd5d662015-07-20 16:19:08 -07001824 CBS ec_point_format_list;
1825 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1826 CBS_len(contents) != 0) {
1827 return 0;
1828 }
1829
David Benjaminfc059942015-07-30 23:01:59 -04001830 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1831 * point format. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05001832 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1833 TLSEXT_ECPOINTFORMAT_uncompressed,
1834 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001835 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001836 return 0;
1837 }
1838
1839 return 1;
1840}
1841
David Benjamin8c880a22016-12-03 02:20:34 -05001842static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001843 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001844 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001845 return 1;
1846 }
1847
David Benjamin8c880a22016-12-03 02:20:34 -05001848 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001849}
1850
David Benjamin8c880a22016-12-03 02:20:34 -05001851static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1852 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001853 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1854 return 1;
1855 }
1856
David Benjamin45738dd2017-02-09 20:01:26 -05001857 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1858 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001859 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001860
1861 if (!using_ecc) {
1862 return 1;
1863 }
1864
David Benjamin8c880a22016-12-03 02:20:34 -05001865 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001866}
1867
Steven Valdeza833c352016-11-01 13:39:36 -04001868
Steven Valdez4aa154e2016-07-29 14:32:55 -04001869/* Pre Shared Key
1870 *
Steven Valdeza833c352016-11-01 13:39:36 -04001871 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001872
David Benjamin8c880a22016-12-03 02:20:34 -05001873static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1874 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001875 uint16_t min_version, max_version;
1876 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1877 return 0;
1878 }
1879
1880 uint16_t session_version;
1881 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1882 !ssl->method->version_from_wire(&session_version,
1883 ssl->session->ssl_version) ||
1884 session_version < TLS1_3_VERSION) {
1885 return 0;
1886 }
1887
Steven Valdez908ac192017-01-12 13:17:07 -05001888 const EVP_MD *digest = SSL_SESSION_get_digest(ssl->session, ssl);
1889 if (digest == NULL) {
1890 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1891 return 0;
1892 }
1893
Steven Valdeza833c352016-11-01 13:39:36 -04001894 size_t binder_len = EVP_MD_size(digest);
1895 return 15 + ssl->session->tlsext_ticklen + binder_len;
1896}
1897
David Benjamin8c880a22016-12-03 02:20:34 -05001898static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1899 SSL *const ssl = hs->ssl;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001900 uint16_t min_version, max_version;
1901 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1902 return 0;
1903 }
1904
David Benjamin2dc02042016-09-19 19:57:37 -04001905 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001906 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001907 !ssl->method->version_from_wire(&session_version,
1908 ssl->session->ssl_version) ||
1909 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001910 return 1;
1911 }
1912
David Benjaminad8f5e12017-02-20 17:00:20 -05001913 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001914 ssl_get_current_time(ssl, &now);
1915 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1916 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1917
1918 /* Fill in a placeholder zero binder of the appropriate length. It will be
1919 * computed and filled in later after length prefixes are computed. */
1920 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez908ac192017-01-12 13:17:07 -05001921
1922 const EVP_MD *digest = SSL_SESSION_get_digest(ssl->session, ssl);
1923 if (digest == NULL) {
1924 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1925 return 0;
1926 }
1927
Steven Valdeza833c352016-11-01 13:39:36 -04001928 size_t binder_len = EVP_MD_size(digest);
1929
1930 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001931 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1932 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001933 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001934 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1935 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001936 ssl->session->tlsext_ticklen) ||
1937 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1938 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1939 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1940 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001941 return 0;
1942 }
1943
David Benjamin8c880a22016-12-03 02:20:34 -05001944 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001945 return CBB_flush(out);
1946}
1947
David Benjamin8baf9632016-11-17 17:11:16 +09001948int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1949 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001950 CBS *contents) {
1951 uint16_t psk_id;
1952 if (!CBS_get_u16(contents, &psk_id) ||
1953 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001954 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001955 *out_alert = SSL_AD_DECODE_ERROR;
1956 return 0;
1957 }
1958
David Benjamineab773a2016-11-09 18:32:35 -05001959 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001960 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001961 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001962 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1963 return 0;
1964 }
1965
1966 return 1;
1967}
1968
David Benjamin35ac5b72017-03-03 15:05:56 -05001969int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001970 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001971 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04001972 /* We only process the first PSK identity since we don't support pure PSK. */
David Benjamin707af292017-03-10 17:47:18 -05001973 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001974 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001975 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001976 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001977 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001978 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001979 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001980 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001981 *out_alert = SSL_AD_DECODE_ERROR;
1982 return 0;
1983 }
1984
Steven Valdeza833c352016-11-01 13:39:36 -04001985 *out_binders = binders;
1986
David Benjaminaedf3032016-12-01 16:47:56 -05001987 /* Check the syntax of the remaining identities, but do not process them. */
1988 size_t num_identities = 1;
1989 while (CBS_len(&identities) != 0) {
1990 CBS unused_ticket;
1991 uint32_t unused_obfuscated_ticket_age;
1992 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1993 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1994 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1995 *out_alert = SSL_AD_DECODE_ERROR;
1996 return 0;
1997 }
1998
1999 num_identities++;
2000 }
2001
2002 /* Check the syntax of the binders. The value will be checked later if
2003 * resuming. */
2004 size_t num_binders = 0;
2005 while (CBS_len(&binders) != 0) {
2006 CBS binder;
2007 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
2008 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2009 *out_alert = SSL_AD_DECODE_ERROR;
2010 return 0;
2011 }
2012
2013 num_binders++;
2014 }
2015
2016 if (num_identities != num_binders) {
2017 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
2018 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04002019 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04002020 }
2021
David Benjamine7f60a22016-11-16 18:54:25 +09002022 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002023}
2024
David Benjamin8baf9632016-11-17 17:11:16 +09002025int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2026 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002027 return 1;
2028 }
2029
2030 CBB contents;
2031 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2032 !CBB_add_u16_length_prefixed(out, &contents) ||
2033 /* We only consider the first identity for resumption */
2034 !CBB_add_u16(&contents, 0) ||
2035 !CBB_flush(out)) {
2036 return 0;
2037 }
2038
2039 return 1;
2040}
2041
2042
Steven Valdeza833c352016-11-01 13:39:36 -04002043/* Pre-Shared Key Exchange Modes
2044 *
2045 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002046
David Benjamin8c880a22016-12-03 02:20:34 -05002047static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2048 CBB *out) {
2049 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04002050 uint16_t min_version, max_version;
2051 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2052 return 0;
2053 }
2054
2055 if (max_version < TLS1_3_VERSION) {
2056 return 1;
2057 }
2058
2059 CBB contents, ke_modes;
2060 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2061 !CBB_add_u16_length_prefixed(out, &contents) ||
2062 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2063 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2064 return 0;
2065 }
2066
2067 return CBB_flush(out);
2068}
2069
David Benjamin8c880a22016-12-03 02:20:34 -05002070static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002071 uint8_t *out_alert,
2072 CBS *contents) {
2073 if (contents == NULL) {
2074 return 1;
2075 }
2076
Steven Valdeza833c352016-11-01 13:39:36 -04002077 CBS ke_modes;
2078 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2079 CBS_len(&ke_modes) == 0 ||
2080 CBS_len(contents) != 0) {
2081 *out_alert = SSL_AD_DECODE_ERROR;
2082 return 0;
2083 }
2084
2085 /* We only support tickets with PSK_DHE_KE. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05002086 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2087 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002088
2089 return 1;
2090}
2091
2092
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002093/* Early Data Indication
2094 *
2095 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2096
David Benjamin8c880a22016-12-03 02:20:34 -05002097static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002098 SSL *const ssl = hs->ssl;
2099 uint16_t session_version;
2100 if (ssl->session == NULL ||
2101 !ssl->method->version_from_wire(&session_version,
2102 ssl->session->ssl_version) ||
2103 session_version < TLS1_3_VERSION ||
2104 ssl->session->ticket_max_early_data == 0 ||
2105 hs->received_hello_retry_request ||
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05002106 !ssl->cert->enable_early_data) {
Steven Valdez2d850622017-01-11 11:34:52 -05002107 return 1;
2108 }
2109
2110 hs->early_data_offered = 1;
2111
2112 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2113 !CBB_add_u16(out, 0) ||
2114 !CBB_flush(out)) {
2115 return 0;
2116 }
2117
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002118 return 1;
2119}
2120
Steven Valdez2d850622017-01-11 11:34:52 -05002121static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002122 uint8_t *out_alert, CBS *contents) {
2123 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002124 if (contents == NULL) {
2125 return 1;
2126 }
2127
2128 if (CBS_len(contents) != 0) {
2129 *out_alert = SSL_AD_DECODE_ERROR;
2130 return 0;
2131 }
2132
Steven Valdez2d850622017-01-11 11:34:52 -05002133 if (!ssl->s3->session_reused) {
2134 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2135 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2136 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002137 }
Steven Valdez2d850622017-01-11 11:34:52 -05002138
2139 ssl->early_data_accepted = 1;
2140 return 1;
2141}
2142
2143static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2144 uint8_t *out_alert, CBS *contents) {
2145 SSL *const ssl = hs->ssl;
2146 if (contents == NULL ||
2147 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2148 return 1;
2149 }
2150
2151 if (CBS_len(contents) != 0) {
2152 *out_alert = SSL_AD_DECODE_ERROR;
2153 return 0;
2154 }
2155
2156 hs->early_data_offered = 1;
2157 return 1;
2158}
2159
2160static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2161 if (!hs->ssl->early_data_accepted) {
2162 return 1;
2163 }
2164
2165 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2166 !CBB_add_u16(out, 0) ||
2167 !CBB_flush(out)) {
2168 return 0;
2169 }
2170
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002171 return 1;
2172}
2173
2174
Steven Valdez143e8b32016-07-11 13:19:03 -04002175/* Key Share
2176 *
David Benjamina128a552016-10-13 14:26:33 -04002177 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002178
David Benjamin8c880a22016-12-03 02:20:34 -05002179static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2180 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002181 uint16_t min_version, max_version;
2182 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2183 return 0;
2184 }
2185
David Benjamin53a2dfc2016-10-07 21:04:12 -04002186 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002187 return 1;
2188 }
2189
2190 CBB contents, kse_bytes;
2191 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2192 !CBB_add_u16_length_prefixed(out, &contents) ||
2193 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2194 return 0;
2195 }
2196
David Benjamin8c880a22016-12-03 02:20:34 -05002197 uint16_t group_id = hs->retry_group;
2198 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002199 /* We received a HelloRetryRequest without a new curve, so there is no new
2200 * share to append. Leave |ecdh_ctx| as-is. */
2201 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002202 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2203 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002204 return 0;
2205 }
David Benjamin8c880a22016-12-03 02:20:34 -05002206 OPENSSL_free(hs->key_share_bytes);
2207 hs->key_share_bytes = NULL;
2208 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002209 if (group_id == 0) {
2210 return CBB_flush(out);
2211 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002212 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002213 /* Add a fake group. See draft-davidben-tls-grease-01. */
2214 if (ssl->ctx->grease_enabled &&
2215 (!CBB_add_u16(&kse_bytes,
2216 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2217 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2218 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2219 return 0;
2220 }
2221
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002222 /* Predict the most preferred group. */
2223 const uint16_t *groups;
2224 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002225 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002226 if (groups_len == 0) {
2227 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2228 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002229 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002230
2231 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002232 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002233
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002234 CBB key_exchange;
2235 if (!CBB_add_u16(&kse_bytes, group_id) ||
2236 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002237 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2238 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002239 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002240 return 0;
2241 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002242
David Benjamin8c880a22016-12-03 02:20:34 -05002243 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002244 /* Save the contents of the extension to repeat it in the second
2245 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002246 hs->key_share_bytes_len = CBB_len(&kse_bytes);
2247 hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
2248 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002249 return 0;
2250 }
2251 }
2252
Steven Valdez143e8b32016-07-11 13:19:03 -04002253 return CBB_flush(out);
2254}
2255
David Benjamin8baf9632016-11-17 17:11:16 +09002256int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002257 size_t *out_secret_len,
2258 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002259 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002260 uint16_t group_id;
2261 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002262 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2263 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002264 *out_alert = SSL_AD_DECODE_ERROR;
2265 return 0;
2266 }
2267
David Benjamin8baf9632016-11-17 17:11:16 +09002268 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002269 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2270 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2271 return 0;
2272 }
2273
David Benjamin8baf9632016-11-17 17:11:16 +09002274 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2275 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002276 *out_alert = SSL_AD_INTERNAL_ERROR;
2277 return 0;
2278 }
2279
David Benjamin45738dd2017-02-09 20:01:26 -05002280 hs->new_session->group_id = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002281 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002282 return 1;
2283}
2284
David Benjamin8baf9632016-11-17 17:11:16 +09002285int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002286 uint8_t **out_secret,
2287 size_t *out_secret_len,
2288 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002289 uint16_t group_id;
2290 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002291 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002292 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2293 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2294 return 0;
2295 }
2296
2297 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002298 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002299 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002300 return 0;
2301 }
2302
David Benjamin7e1f9842016-09-20 19:24:40 -04002303 /* Find the corresponding key share. */
2304 int found = 0;
2305 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002306 while (CBS_len(&key_shares) > 0) {
2307 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002308 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002309 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002310 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2311 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002312 return 0;
2313 }
2314
David Benjamin7e1f9842016-09-20 19:24:40 -04002315 if (id == group_id) {
2316 if (found) {
2317 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2318 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2319 return 0;
2320 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002321
David Benjamin7e1f9842016-09-20 19:24:40 -04002322 found = 1;
2323 peer_key = peer_key_tmp;
2324 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002325 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002326 }
2327
David Benjamin7e1f9842016-09-20 19:24:40 -04002328 if (!found) {
2329 *out_found = 0;
2330 *out_secret = NULL;
2331 *out_secret_len = 0;
2332 return 1;
2333 }
2334
2335 /* Compute the DH secret. */
2336 uint8_t *secret = NULL;
2337 size_t secret_len;
2338 SSL_ECDH_CTX group;
David Benjamin17cf2cb2016-12-13 01:07:13 -05002339 OPENSSL_memset(&group, 0, sizeof(SSL_ECDH_CTX));
David Benjamin7e1f9842016-09-20 19:24:40 -04002340 CBB public_key;
2341 if (!CBB_init(&public_key, 32) ||
2342 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002343 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
2344 CBS_data(&peer_key), CBS_len(&peer_key)) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002345 !CBB_finish(&public_key, &hs->ecdh_public_key,
2346 &hs->ecdh_public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002347 OPENSSL_free(secret);
2348 SSL_ECDH_CTX_cleanup(&group);
2349 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002350 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002351 return 0;
2352 }
2353
2354 SSL_ECDH_CTX_cleanup(&group);
2355
2356 *out_secret = secret;
2357 *out_secret_len = secret_len;
2358 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002359 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002360}
2361
David Benjamin8baf9632016-11-17 17:11:16 +09002362int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002363 uint16_t group_id;
2364 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002365 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002366 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2367 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2368 !CBB_add_u16(&kse_bytes, group_id) ||
2369 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002370 !CBB_add_bytes(&public_key, hs->ecdh_public_key,
2371 hs->ecdh_public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002372 !CBB_flush(out)) {
2373 return 0;
2374 }
2375
David Benjaminbf833c32017-03-30 15:45:21 -05002376 OPENSSL_free(hs->ecdh_public_key);
2377 hs->ecdh_public_key = NULL;
2378 hs->ecdh_public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002379
David Benjamin45738dd2017-02-09 20:01:26 -05002380 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002381 return 1;
2382}
2383
2384
Steven Valdezfdd10992016-09-15 16:27:05 -04002385/* Supported Versions
2386 *
2387 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2388
David Benjamin8c880a22016-12-03 02:20:34 -05002389static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2390 SSL *const ssl = hs->ssl;
Steven Valdezfdd10992016-09-15 16:27:05 -04002391 uint16_t min_version, max_version;
2392 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2393 return 0;
2394 }
2395
2396 if (max_version <= TLS1_2_VERSION) {
2397 return 1;
2398 }
2399
2400 CBB contents, versions;
2401 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2402 !CBB_add_u16_length_prefixed(out, &contents) ||
2403 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2404 return 0;
2405 }
2406
David Benjamind9791bf2016-09-27 16:39:52 -04002407 /* Add a fake version. See draft-davidben-tls-grease-01. */
2408 if (ssl->ctx->grease_enabled &&
2409 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2410 return 0;
2411 }
2412
Steven Valdezfdd10992016-09-15 16:27:05 -04002413 for (uint16_t version = max_version; version >= min_version; version--) {
2414 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2415 return 0;
2416 }
2417 }
2418
2419 if (!CBB_flush(out)) {
2420 return 0;
2421 }
2422
2423 return 1;
2424}
2425
2426
David Benjamin3baa6e12016-10-07 21:10:38 -04002427/* Cookie
2428 *
2429 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2430
David Benjamin8c880a22016-12-03 02:20:34 -05002431static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2432 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002433 return 1;
2434 }
2435
2436 CBB contents, cookie;
2437 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2438 !CBB_add_u16_length_prefixed(out, &contents) ||
2439 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002440 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002441 !CBB_flush(out)) {
2442 return 0;
2443 }
2444
2445 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002446 OPENSSL_free(hs->cookie);
2447 hs->cookie = NULL;
2448 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002449 return 1;
2450}
2451
2452
Steven Valdezce902a92016-05-17 11:47:53 -04002453/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002454 *
Steven Valdezce902a92016-05-17 11:47:53 -04002455 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002456 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002457
David Benjamin8c880a22016-12-03 02:20:34 -05002458static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2459 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002460 CBB contents, groups_bytes;
2461 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002462 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002463 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002464 return 0;
2465 }
2466
David Benjamin65ac9972016-09-02 21:35:25 -04002467 /* Add a fake group. See draft-davidben-tls-grease-01. */
2468 if (ssl->ctx->grease_enabled &&
2469 !CBB_add_u16(&groups_bytes,
2470 ssl_get_grease_value(ssl, ssl_grease_group))) {
2471 return 0;
2472 }
2473
Steven Valdezce902a92016-05-17 11:47:53 -04002474 const uint16_t *groups;
2475 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002476 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002477
David Benjamin54091232016-09-05 12:47:25 -04002478 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002479 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002480 return 0;
2481 }
2482 }
2483
2484 return CBB_flush(out);
2485}
2486
David Benjamin8c880a22016-12-03 02:20:34 -05002487static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2488 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002489 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002490 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2491 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002492 return 1;
2493}
2494
David Benjamin8c880a22016-12-03 02:20:34 -05002495static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2496 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002497 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002498 if (contents == NULL) {
2499 return 1;
2500 }
2501
Steven Valdezce902a92016-05-17 11:47:53 -04002502 CBS supported_group_list;
2503 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2504 CBS_len(&supported_group_list) == 0 ||
2505 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002506 CBS_len(contents) != 0) {
2507 return 0;
2508 }
2509
David Benjamin8c880a22016-12-03 02:20:34 -05002510 hs->peer_supported_group_list =
2511 OPENSSL_malloc(CBS_len(&supported_group_list));
2512 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002513 *out_alert = SSL_AD_INTERNAL_ERROR;
2514 return 0;
2515 }
2516
Steven Valdezce902a92016-05-17 11:47:53 -04002517 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002518 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002519 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002520 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002521 goto err;
2522 }
2523 }
2524
Steven Valdezce902a92016-05-17 11:47:53 -04002525 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002526 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002527
2528 return 1;
2529
2530err:
David Benjamin8c880a22016-12-03 02:20:34 -05002531 OPENSSL_free(hs->peer_supported_group_list);
2532 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002533 *out_alert = SSL_AD_INTERNAL_ERROR;
2534 return 0;
2535}
2536
David Benjamin8c880a22016-12-03 02:20:34 -05002537static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002538 /* Servers don't echo this extension. */
2539 return 1;
2540}
2541
2542
Adam Langley614c66a2015-06-12 15:26:58 -07002543/* kExtensions contains all the supported extensions. */
2544static const struct tls_extension kExtensions[] = {
2545 {
Adam Langley5021b222015-06-12 18:27:58 -07002546 TLSEXT_TYPE_renegotiate,
2547 NULL,
2548 ext_ri_add_clienthello,
2549 ext_ri_parse_serverhello,
2550 ext_ri_parse_clienthello,
2551 ext_ri_add_serverhello,
2552 },
2553 {
Adam Langley614c66a2015-06-12 15:26:58 -07002554 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002555 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002556 ext_sni_add_clienthello,
2557 ext_sni_parse_serverhello,
2558 ext_sni_parse_clienthello,
2559 ext_sni_add_serverhello,
2560 },
Adam Langley0a056712015-07-01 15:03:33 -07002561 {
2562 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002563 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002564 ext_ems_add_clienthello,
2565 ext_ems_parse_serverhello,
2566 ext_ems_parse_clienthello,
2567 ext_ems_add_serverhello,
2568 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002569 {
2570 TLSEXT_TYPE_session_ticket,
2571 NULL,
2572 ext_ticket_add_clienthello,
2573 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002574 /* Ticket extension client parsing is handled in ssl_session.c */
2575 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002576 ext_ticket_add_serverhello,
2577 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002578 {
2579 TLSEXT_TYPE_signature_algorithms,
2580 NULL,
2581 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002582 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002583 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002584 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002585 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002586 {
2587 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002588 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002589 ext_ocsp_add_clienthello,
2590 ext_ocsp_parse_serverhello,
2591 ext_ocsp_parse_clienthello,
2592 ext_ocsp_add_serverhello,
2593 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002594 {
2595 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002596 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002597 ext_npn_add_clienthello,
2598 ext_npn_parse_serverhello,
2599 ext_npn_parse_clienthello,
2600 ext_npn_add_serverhello,
2601 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002602 {
2603 TLSEXT_TYPE_certificate_timestamp,
2604 NULL,
2605 ext_sct_add_clienthello,
2606 ext_sct_parse_serverhello,
2607 ext_sct_parse_clienthello,
2608 ext_sct_add_serverhello,
2609 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002610 {
2611 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002612 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002613 ext_alpn_add_clienthello,
2614 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002615 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2616 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002617 ext_alpn_add_serverhello,
2618 },
Adam Langley49c7af12015-07-10 14:33:46 -07002619 {
2620 TLSEXT_TYPE_channel_id,
2621 ext_channel_id_init,
2622 ext_channel_id_add_clienthello,
2623 ext_channel_id_parse_serverhello,
2624 ext_channel_id_parse_clienthello,
2625 ext_channel_id_add_serverhello,
2626 },
Adam Langley391250d2015-07-15 19:06:07 -07002627 {
2628 TLSEXT_TYPE_srtp,
2629 ext_srtp_init,
2630 ext_srtp_add_clienthello,
2631 ext_srtp_parse_serverhello,
2632 ext_srtp_parse_clienthello,
2633 ext_srtp_add_serverhello,
2634 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002635 {
2636 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002637 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002638 ext_ec_point_add_clienthello,
2639 ext_ec_point_parse_serverhello,
2640 ext_ec_point_parse_clienthello,
2641 ext_ec_point_add_serverhello,
2642 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002643 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002644 TLSEXT_TYPE_key_share,
2645 NULL,
2646 ext_key_share_add_clienthello,
2647 forbid_parse_serverhello,
2648 ignore_parse_clienthello,
2649 dont_add_serverhello,
2650 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002651 {
Steven Valdeza833c352016-11-01 13:39:36 -04002652 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002653 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002654 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002655 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002656 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002657 dont_add_serverhello,
2658 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002659 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002660 TLSEXT_TYPE_early_data,
2661 NULL,
2662 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002663 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002664 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002665 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002666 },
2667 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002668 TLSEXT_TYPE_supported_versions,
2669 NULL,
2670 ext_supported_versions_add_clienthello,
2671 forbid_parse_serverhello,
2672 ignore_parse_clienthello,
2673 dont_add_serverhello,
2674 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002675 {
2676 TLSEXT_TYPE_cookie,
2677 NULL,
2678 ext_cookie_add_clienthello,
2679 forbid_parse_serverhello,
2680 ignore_parse_clienthello,
2681 dont_add_serverhello,
2682 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002683 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2684 * intolerant to the last extension being zero-length. See
2685 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002686 {
Steven Valdezce902a92016-05-17 11:47:53 -04002687 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002688 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002689 ext_supported_groups_add_clienthello,
2690 ext_supported_groups_parse_serverhello,
2691 ext_supported_groups_parse_clienthello,
2692 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002693 },
Adam Langley614c66a2015-06-12 15:26:58 -07002694};
2695
2696#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2697
Adam Langley4cfa96b2015-07-01 11:56:55 -07002698OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002699 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002700 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002701OPENSSL_COMPILE_ASSERT(
2702 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2703 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002704
Adam Langley614c66a2015-06-12 15:26:58 -07002705static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2706 uint16_t value) {
2707 unsigned i;
2708 for (i = 0; i < kNumExtensions; i++) {
2709 if (kExtensions[i].value == value) {
2710 *out_index = i;
2711 return &kExtensions[i];
2712 }
2713 }
2714
2715 return NULL;
2716}
2717
Adam Langley09505632015-07-30 18:10:13 -07002718int SSL_extension_supported(unsigned extension_value) {
2719 uint32_t index;
2720 return extension_value == TLSEXT_TYPE_padding ||
2721 tls_extension_find(&index, extension_value) != NULL;
2722}
2723
David Benjamin8c880a22016-12-03 02:20:34 -05002724int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2725 SSL *const ssl = hs->ssl;
David Benjaminf04c2e92016-12-06 13:35:25 -05002726 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2727 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002728 !ssl->s3->send_connection_binding) {
2729 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002730 }
Adam Langley95c29f32014-06-20 12:00:00 -07002731
David Benjamine8d53502015-10-10 14:13:23 -04002732 CBB extensions;
2733 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002734 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002735 }
Adam Langley95c29f32014-06-20 12:00:00 -07002736
David Benjamin8c880a22016-12-03 02:20:34 -05002737 hs->extensions.sent = 0;
2738 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002739
David Benjamin54091232016-09-05 12:47:25 -04002740 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002741 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002742 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002743 }
2744 }
Adam Langley95c29f32014-06-20 12:00:00 -07002745
David Benjamin65ac9972016-09-02 21:35:25 -04002746 uint16_t grease_ext1 = 0;
2747 if (ssl->ctx->grease_enabled) {
2748 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2749 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2750 if (!CBB_add_u16(&extensions, grease_ext1) ||
2751 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2752 goto err;
2753 }
2754 }
2755
David Benjamin54091232016-09-05 12:47:25 -04002756 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002757 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002758 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002759 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002760 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002761 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002762 }
Adam Langley95c29f32014-06-20 12:00:00 -07002763
Adam Langley33ad2b52015-07-20 17:43:53 -07002764 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002765 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002766 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002767 }
Adam Langley75712922014-10-10 16:23:43 -07002768
David Benjamin2bd19172016-11-17 16:47:15 +09002769 if (!custom_ext_add_clienthello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002770 goto err;
2771 }
2772
David Benjamin65ac9972016-09-02 21:35:25 -04002773 if (ssl->ctx->grease_enabled) {
2774 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2775 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2776
2777 /* The two fake extensions must not have the same value. GREASE values are
2778 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2779 * one. */
2780 if (grease_ext1 == grease_ext2) {
2781 grease_ext2 ^= 0x1010;
2782 }
2783
2784 if (!CBB_add_u16(&extensions, grease_ext2) ||
2785 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2786 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2787 goto err;
2788 }
2789 }
2790
David Benjamince079fd2016-08-02 16:22:34 -04002791 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002792 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002793 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002794 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002795 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002796 *
2797 * NB: because this code works out the length of all existing extensions
2798 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002799 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002800 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002801 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002802 * Server 7.0 is intolerant to the last extension being zero-length. See
2803 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002804 if (padding_len >= 4 + 1) {
2805 padding_len -= 4;
2806 } else {
2807 padding_len = 1;
2808 }
Adam Langley95c29f32014-06-20 12:00:00 -07002809
Adam Langley33ad2b52015-07-20 17:43:53 -07002810 uint8_t *padding_bytes;
2811 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2812 !CBB_add_u16(&extensions, padding_len) ||
2813 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2814 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002815 }
Adam Langley75712922014-10-10 16:23:43 -07002816
David Benjamin17cf2cb2016-12-13 01:07:13 -05002817 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002818 }
2819 }
Adam Langley75712922014-10-10 16:23:43 -07002820
Steven Valdeza833c352016-11-01 13:39:36 -04002821 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002822 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002823 goto err;
2824 }
2825
David Benjamina01deee2015-12-08 18:56:31 -05002826 /* Discard empty extensions blocks. */
2827 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002828 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002829 }
2830
David Benjamine8d53502015-10-10 14:13:23 -04002831 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002832
2833err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002834 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002835 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002836}
Adam Langley95c29f32014-06-20 12:00:00 -07002837
David Benjamin8c880a22016-12-03 02:20:34 -05002838int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2839 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002840 CBB extensions;
2841 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002842 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002843 }
2844
David Benjamin8c880a22016-12-03 02:20:34 -05002845 for (unsigned i = 0; i < kNumExtensions; i++) {
2846 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002847 /* Don't send extensions that were not received. */
2848 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002849 }
Adam Langley95c29f32014-06-20 12:00:00 -07002850
David Benjamin8c880a22016-12-03 02:20:34 -05002851 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002852 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002853 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002854 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002855 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002856 }
Adam Langley95c29f32014-06-20 12:00:00 -07002857
David Benjamin2bd19172016-11-17 16:47:15 +09002858 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002859 goto err;
2860 }
2861
Steven Valdez143e8b32016-07-11 13:19:03 -04002862 /* Discard empty extensions blocks before TLS 1.3. */
2863 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2864 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002865 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002866 }
2867
David Benjamin56380462015-10-10 14:59:09 -04002868 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002869
2870err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002871 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002872 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002873}
Adam Langley95c29f32014-06-20 12:00:00 -07002874
David Benjamin731058e2016-12-03 23:15:13 -05002875static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2876 const SSL_CLIENT_HELLO *client_hello,
2877 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002878 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002879 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002880 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002881 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002882 }
2883 }
2884
David Benjamin8c880a22016-12-03 02:20:34 -05002885 hs->extensions.received = 0;
2886 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002887
David Benjamine14ff062016-08-09 16:21:24 -04002888 CBS extensions;
2889 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2890 while (CBS_len(&extensions) != 0) {
2891 uint16_t type;
2892 CBS extension;
2893
2894 /* Decode the next extension. */
2895 if (!CBS_get_u16(&extensions, &type) ||
2896 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002897 *out_alert = SSL_AD_DECODE_ERROR;
2898 return 0;
2899 }
Adam Langley95c29f32014-06-20 12:00:00 -07002900
David Benjamine14ff062016-08-09 16:21:24 -04002901 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2902 * ambiguous. Ignore all but the renegotiation_info extension. */
2903 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2904 continue;
2905 }
Adam Langley95c29f32014-06-20 12:00:00 -07002906
David Benjamine14ff062016-08-09 16:21:24 -04002907 unsigned ext_index;
2908 const struct tls_extension *const ext =
2909 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002910
David Benjamine14ff062016-08-09 16:21:24 -04002911 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002912 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002913 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002914 return 0;
2915 }
David Benjamine14ff062016-08-09 16:21:24 -04002916 continue;
2917 }
2918
David Benjamin8c880a22016-12-03 02:20:34 -05002919 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002920 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002921 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002922 *out_alert = alert;
2923 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002924 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002925 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002926 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002927 }
Adam Langley75712922014-10-10 16:23:43 -07002928
David Benjamin1deb41b2016-08-09 19:36:38 -04002929 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002930 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002931 continue;
2932 }
2933
2934 CBS *contents = NULL, fake_contents;
2935 static const uint8_t kFakeRenegotiateExtension[] = {0};
2936 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2937 ssl_client_cipher_list_contains_cipher(client_hello,
2938 SSL3_CK_SCSV & 0xffff)) {
2939 /* The renegotiation SCSV was received so pretend that we received a
2940 * renegotiation extension. */
2941 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2942 sizeof(kFakeRenegotiateExtension));
2943 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002944 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002945 }
2946
2947 /* Extension wasn't observed so call the callback with a NULL
2948 * parameter. */
2949 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002950 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002951 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002952 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002953 *out_alert = alert;
2954 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002955 }
2956 }
2957
Adam Langleyfcf25832014-12-18 17:42:32 -08002958 return 1;
2959}
Adam Langley95c29f32014-06-20 12:00:00 -07002960
David Benjamin731058e2016-12-03 23:15:13 -05002961int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2962 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002963 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002964 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002965 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002966 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002967 return 0;
2968 }
Adam Langley95c29f32014-06-20 12:00:00 -07002969
David Benjamin8c880a22016-12-03 02:20:34 -05002970 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002971 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002972 return 0;
2973 }
Adam Langley95c29f32014-06-20 12:00:00 -07002974
Adam Langleyfcf25832014-12-18 17:42:32 -08002975 return 1;
2976}
Adam Langley95c29f32014-06-20 12:00:00 -07002977
David Benjamin8c880a22016-12-03 02:20:34 -05002978static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2979 int *out_alert) {
2980 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002981 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2982 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2983 return 1;
2984 }
Adam Langley614c66a2015-06-12 15:26:58 -07002985
Steven Valdez143e8b32016-07-11 13:19:03 -04002986 /* Decode the extensions block and check it is valid. */
2987 CBS extensions;
2988 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2989 !tls1_check_duplicate_extensions(&extensions)) {
2990 *out_alert = SSL_AD_DECODE_ERROR;
2991 return 0;
2992 }
2993
2994 uint32_t received = 0;
2995 while (CBS_len(&extensions) != 0) {
2996 uint16_t type;
2997 CBS extension;
2998
2999 /* Decode the next extension. */
3000 if (!CBS_get_u16(&extensions, &type) ||
3001 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003002 *out_alert = SSL_AD_DECODE_ERROR;
3003 return 0;
3004 }
Adam Langley95c29f32014-06-20 12:00:00 -07003005
Steven Valdez143e8b32016-07-11 13:19:03 -04003006 unsigned ext_index;
3007 const struct tls_extension *const ext =
3008 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07003009
Steven Valdez143e8b32016-07-11 13:19:03 -04003010 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09003011 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003012 return 0;
3013 }
Steven Valdez143e8b32016-07-11 13:19:03 -04003014 continue;
3015 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003016
David Benjamin5db7c9b2017-01-24 16:17:03 -05003017 OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
3018 too_many_bits);
3019
David Benjamin8c880a22016-12-03 02:20:34 -05003020 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04003021 type != TLSEXT_TYPE_renegotiate) {
3022 /* If the extension was never sent then it is illegal, except for the
3023 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04003024 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3025 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04003026 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04003027 return 0;
3028 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003029
Steven Valdez143e8b32016-07-11 13:19:03 -04003030 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07003031
Steven Valdez143e8b32016-07-11 13:19:03 -04003032 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003033 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04003034 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003035 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04003036 *out_alert = alert;
3037 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07003038 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003039 }
Adam Langley95c29f32014-06-20 12:00:00 -07003040
David Benjamin54091232016-09-05 12:47:25 -04003041 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003042 if (!(received & (1u << i))) {
3043 /* Extension wasn't observed so call the callback with a NULL
3044 * parameter. */
3045 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003046 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003047 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003048 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07003049 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08003050 return 0;
3051 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003052 }
3053 }
Adam Langley95c29f32014-06-20 12:00:00 -07003054
Adam Langleyfcf25832014-12-18 17:42:32 -08003055 return 1;
3056}
Adam Langley95c29f32014-06-20 12:00:00 -07003057
David Benjamin8c880a22016-12-03 02:20:34 -05003058static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3059 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003060 int ret = SSL_TLSEXT_ERR_NOACK;
3061 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003062
David Benjamin78f8aab2016-03-10 16:33:58 -05003063 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003064 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003065 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05003066 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
3067 ret = ssl->session_ctx->tlsext_servername_callback(
3068 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003069 }
Adam Langley95c29f32014-06-20 12:00:00 -07003070
Adam Langleyfcf25832014-12-18 17:42:32 -08003071 switch (ret) {
3072 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003073 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003074 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003075
Adam Langleyfcf25832014-12-18 17:42:32 -08003076 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003077 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003078 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003079
Adam Langleyfcf25832014-12-18 17:42:32 -08003080 default:
3081 return 1;
3082 }
3083}
Adam Langleyed8270a2014-09-02 13:52:56 -07003084
David Benjamin8c880a22016-12-03 02:20:34 -05003085int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3086 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08003087 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003088 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003089 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003090 return 0;
3091 }
3092
Adam Langleyfcf25832014-12-18 17:42:32 -08003093 return 1;
3094}
Adam Langley95c29f32014-06-20 12:00:00 -07003095
Adam Langley4c341d02017-03-08 19:33:21 -08003096static enum ssl_ticket_aead_result_t
3097ssl_decrypt_ticket_with_cipher_ctx(SSL *ssl, uint8_t **out, size_t *out_len,
3098 int *out_renew_ticket, const uint8_t *ticket,
3099 size_t ticket_len) {
3100 enum ssl_ticket_aead_result_t ret = ssl_ticket_aead_ignore_ticket;
3101 const SSL_CTX *const ssl_ctx = ssl->session_ctx;
David Benjamine3aa1d92015-06-16 15:34:50 -04003102 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003103
David Benjamine3aa1d92015-06-16 15:34:50 -04003104 HMAC_CTX hmac_ctx;
3105 HMAC_CTX_init(&hmac_ctx);
3106 EVP_CIPHER_CTX cipher_ctx;
3107 EVP_CIPHER_CTX_init(&cipher_ctx);
3108
David Benjaminadcc3952015-04-26 13:07:57 -04003109 /* Ensure there is room for the key name and the largest IV
3110 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3111 * the maximum IV length should be well under the minimum size for the
3112 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003113 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
Adam Langley4c341d02017-03-08 19:33:21 -08003114 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003115 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003116 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003117
David Benjamine3aa1d92015-06-16 15:34:50 -04003118 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003119 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3120 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3121 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003122 if (cb_ret < 0) {
Adam Langley4c341d02017-03-08 19:33:21 -08003123 ret = ssl_ticket_aead_error;
3124 goto out;
3125 } else if (cb_ret == 0) {
3126 goto out;
3127 } else if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003128 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003129 }
3130 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003131 /* Check the key name matches. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003132 if (OPENSSL_memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3133 SSL_TICKET_KEY_NAME_LEN) != 0) {
Adam Langley4c341d02017-03-08 19:33:21 -08003134 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003135 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003136 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3137 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003138 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003139 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3140 ssl_ctx->tlsext_tick_aes_key, iv)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003141 ret = ssl_ticket_aead_error;
3142 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003143 }
3144 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003145 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003146
David Benjamine3aa1d92015-06-16 15:34:50 -04003147 /* Check the MAC at the end of the ticket. */
3148 uint8_t mac[EVP_MAX_MD_SIZE];
3149 size_t mac_len = HMAC_size(&hmac_ctx);
3150 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003151 /* The ticket must be large enough for key name, IV, data, and MAC. */
Adam Langley4c341d02017-03-08 19:33:21 -08003152 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003153 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003154 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3155 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003156 int mac_ok =
3157 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3158#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3159 mac_ok = 1;
3160#endif
3161 if (!mac_ok) {
Adam Langley4c341d02017-03-08 19:33:21 -08003162 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003163 }
3164
David Benjamine3aa1d92015-06-16 15:34:50 -04003165 /* Decrypt the session data. */
3166 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3167 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3168 mac_len;
3169 plaintext = OPENSSL_malloc(ciphertext_len);
3170 if (plaintext == NULL) {
Adam Langley4c341d02017-03-08 19:33:21 -08003171 ret = ssl_ticket_aead_error;
3172 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003173 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003174 size_t plaintext_len;
3175#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin17cf2cb2016-12-13 01:07:13 -05003176 OPENSSL_memcpy(plaintext, ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003177 plaintext_len = ciphertext_len;
3178#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003179 if (ciphertext_len >= INT_MAX) {
Adam Langley4c341d02017-03-08 19:33:21 -08003180 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003181 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003182 int len1, len2;
3183 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3184 (int)ciphertext_len) ||
3185 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003186 ERR_clear_error();
3187 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003188 }
Adam Langley4c341d02017-03-08 19:33:21 -08003189 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04003190#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003191
Adam Langley4c341d02017-03-08 19:33:21 -08003192 *out = plaintext;
3193 plaintext = NULL;
3194 *out_len = plaintext_len;
3195 ret = ssl_ticket_aead_success;
3196
3197out:
3198 OPENSSL_free(plaintext);
3199 HMAC_CTX_cleanup(&hmac_ctx);
3200 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3201 return ret;
3202}
3203
3204static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
3205 SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
3206 const uint8_t *ticket, size_t ticket_len) {
3207 uint8_t *plaintext = OPENSSL_malloc(ticket_len);
3208 if (plaintext == NULL) {
3209 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3210 return ssl_ticket_aead_error;
3211 }
3212
3213 size_t plaintext_len;
3214 const enum ssl_ticket_aead_result_t result =
3215 ssl->session_ctx->ticket_aead_method->open(
3216 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3217
3218 if (result == ssl_ticket_aead_success) {
3219 *out = plaintext;
3220 plaintext = NULL;
3221 *out_len = plaintext_len;
3222 }
3223
3224 OPENSSL_free(plaintext);
3225 return result;
3226}
3227
3228enum ssl_ticket_aead_result_t ssl_process_ticket(
3229 SSL *ssl, SSL_SESSION **out_session, int *out_renew_ticket,
3230 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3231 size_t session_id_len) {
3232 *out_renew_ticket = 0;
3233 *out_session = NULL;
3234
3235 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3236 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3237 return ssl_ticket_aead_ignore_ticket;
3238 }
3239
3240 uint8_t *plaintext = NULL;
3241 size_t plaintext_len;
3242 enum ssl_ticket_aead_result_t result;
3243 if (ssl->session_ctx->ticket_aead_method != NULL) {
3244 result = ssl_decrypt_ticket_with_method(
3245 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3246 } else {
3247 result = ssl_decrypt_ticket_with_cipher_ctx(
3248 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3249 }
3250
3251 if (result != ssl_ticket_aead_success) {
3252 return result;
3253 }
3254
David Benjamine3aa1d92015-06-16 15:34:50 -04003255 /* Decode the session. */
Adam Langley46db7af2017-02-01 15:49:37 -08003256 SSL_SESSION *session =
3257 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx);
Adam Langley4c341d02017-03-08 19:33:21 -08003258 OPENSSL_free(plaintext);
3259
David Benjamine3aa1d92015-06-16 15:34:50 -04003260 if (session == NULL) {
3261 ERR_clear_error(); /* Don't leave an error on the queue. */
Adam Langley4c341d02017-03-08 19:33:21 -08003262 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003263 }
3264
3265 /* Copy the client's session ID into the new session, to denote the ticket has
3266 * been accepted. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003267 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003268 session->session_id_length = session_id_len;
3269
3270 *out_session = session;
Adam Langley4c341d02017-03-08 19:33:21 -08003271 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003272}
Adam Langley95c29f32014-06-20 12:00:00 -07003273
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003274int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003275 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003276 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003277 return 1;
3278 }
David Benjamincd996942014-07-20 16:23:51 -04003279
David Benjamin0fc37ef2016-08-17 15:29:46 -04003280 OPENSSL_free(hs->peer_sigalgs);
3281 hs->peer_sigalgs = NULL;
3282 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003283
3284 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003285 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003286 return 0;
3287 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003288 num_sigalgs /= 2;
3289
3290 /* supported_signature_algorithms in the certificate request is
3291 * allowed to be empty. */
3292 if (num_sigalgs == 0) {
3293 return 1;
3294 }
3295
Steven Valdez02563852016-06-23 13:33:05 -04003296 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3297 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003298 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3299 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003300 return 0;
3301 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003302 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003303
3304 CBS sigalgs;
3305 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003306 for (size_t i = 0; i < num_sigalgs; i++) {
3307 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003308 return 0;
3309 }
3310 }
Adam Langley95c29f32014-06-20 12:00:00 -07003311
Adam Langleyfcf25832014-12-18 17:42:32 -08003312 return 1;
3313}
David Benjaminec2f27d2014-11-13 19:17:25 -05003314
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003315int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3316 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003317 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003318
Steven Valdezf0451ca2016-06-29 13:16:27 -04003319 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3320 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3321 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamina232a712017-03-30 15:51:53 -05003322 switch (EVP_PKEY_id(hs->local_pubkey)) {
3323 case EVP_PKEY_RSA:
3324 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3325 return 1;
3326 case EVP_PKEY_EC:
3327 *out = SSL_SIGN_ECDSA_SHA1;
3328 return 1;
3329 default:
3330 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3331 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003332 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04003333 }
3334
David Benjamin3ef76972016-10-17 17:59:54 -04003335 const uint16_t *sigalgs = cert->sigalgs;
3336 size_t num_sigalgs = cert->num_sigalgs;
3337 if (sigalgs == NULL) {
3338 sigalgs = kSignSignatureAlgorithms;
3339 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003340 }
3341
David Benjamin0fc37ef2016-08-17 15:29:46 -04003342 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3343 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3344 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003345 /* If the client didn't specify any signature_algorithms extension then
3346 * we can assume that it supports SHA1. See
3347 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3348 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3349 SSL_SIGN_ECDSA_SHA1};
3350 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003351 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003352 }
3353
David Benjamin0fc37ef2016-08-17 15:29:46 -04003354 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003355 uint16_t sigalg = sigalgs[i];
3356 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3357 * negotiated. */
3358 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjamina232a712017-03-30 15:51:53 -05003359 !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003360 continue;
3361 }
3362
David Benjamin0fc37ef2016-08-17 15:29:46 -04003363 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003364 if (sigalg == peer_sigalgs[j]) {
3365 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003366 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003367 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003368 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003369 }
Adam Langley95c29f32014-06-20 12:00:00 -07003370
David Benjaminea9a0d52016-07-08 15:52:59 -07003371 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3372 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003373}
Adam Langley95c29f32014-06-20 12:00:00 -07003374
Steven Valdez908ac192017-01-12 13:17:07 -05003375int tls1_verify_channel_id(SSL_HANDSHAKE *hs) {
3376 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003377 int ret = 0;
3378 uint16_t extension_type;
3379 CBS extension, channel_id;
3380
3381 /* A Channel ID handshake message is structured to contain multiple
3382 * extensions, but the only one that can be present is Channel ID. */
3383 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3384 if (!CBS_get_u16(&channel_id, &extension_type) ||
3385 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3386 CBS_len(&channel_id) != 0 ||
3387 extension_type != TLSEXT_TYPE_channel_id ||
3388 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3389 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3390 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3391 return 0;
3392 }
3393
3394 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3395 if (!p256) {
3396 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3397 return 0;
3398 }
3399
3400 EC_KEY *key = NULL;
3401 EC_POINT *point = NULL;
3402 BIGNUM x, y;
3403 ECDSA_SIG sig;
3404 BN_init(&x);
3405 BN_init(&y);
3406 sig.r = BN_new();
3407 sig.s = BN_new();
3408 if (sig.r == NULL || sig.s == NULL) {
3409 goto err;
3410 }
3411
3412 const uint8_t *p = CBS_data(&extension);
3413 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3414 BN_bin2bn(p + 32, 32, &y) == NULL ||
3415 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3416 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3417 goto err;
3418 }
3419
3420 point = EC_POINT_new(p256);
3421 if (point == NULL ||
3422 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3423 goto err;
3424 }
3425
3426 key = EC_KEY_new();
3427 if (key == NULL ||
3428 !EC_KEY_set_group(key, p256) ||
3429 !EC_KEY_set_public_key(key, point)) {
3430 goto err;
3431 }
3432
3433 uint8_t digest[EVP_MAX_MD_SIZE];
3434 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003435 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003436 goto err;
3437 }
3438
3439 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3440#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3441 sig_ok = 1;
3442#endif
3443 if (!sig_ok) {
3444 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3445 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3446 ssl->s3->tlsext_channel_id_valid = 0;
3447 goto err;
3448 }
3449
David Benjamin17cf2cb2016-12-13 01:07:13 -05003450 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
Nick Harper60a85cb2016-09-23 16:25:11 -07003451 ret = 1;
3452
3453err:
3454 BN_free(&x);
3455 BN_free(&y);
3456 BN_free(sig.r);
3457 BN_free(sig.s);
3458 EC_KEY_free(key);
3459 EC_POINT_free(point);
3460 EC_GROUP_free(p256);
3461 return ret;
3462}
3463
Steven Valdez908ac192017-01-12 13:17:07 -05003464int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3465 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003466 uint8_t digest[EVP_MAX_MD_SIZE];
3467 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003468 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003469 return 0;
3470 }
3471
3472 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3473 if (ec_key == NULL) {
3474 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3475 return 0;
3476 }
3477
3478 int ret = 0;
3479 BIGNUM *x = BN_new();
3480 BIGNUM *y = BN_new();
3481 ECDSA_SIG *sig = NULL;
3482 if (x == NULL || y == NULL ||
3483 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3484 EC_KEY_get0_public_key(ec_key),
3485 x, y, NULL)) {
3486 goto err;
3487 }
3488
3489 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3490 if (sig == NULL) {
3491 goto err;
3492 }
3493
3494 CBB child;
3495 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3496 !CBB_add_u16_length_prefixed(cbb, &child) ||
3497 !BN_bn2cbb_padded(&child, 32, x) ||
3498 !BN_bn2cbb_padded(&child, 32, y) ||
3499 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3500 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3501 !CBB_flush(cbb)) {
3502 goto err;
3503 }
3504
3505 ret = 1;
3506
3507err:
3508 BN_free(x);
3509 BN_free(y);
3510 ECDSA_SIG_free(sig);
3511 return ret;
3512}
3513
Steven Valdez908ac192017-01-12 13:17:07 -05003514int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3515 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003516 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3517 uint8_t *msg;
3518 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003519 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003520 ssl_cert_verify_channel_id)) {
3521 return 0;
3522 }
3523 SHA256(msg, msg_len, out);
3524 *out_len = SHA256_DIGEST_LENGTH;
3525 OPENSSL_free(msg);
3526 return 1;
3527 }
3528
Nick Harper95594012016-10-20 14:07:13 -07003529 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003530
Nick Harper95594012016-10-20 14:07:13 -07003531 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003532 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003533 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003534
Steven Valdez87eab492016-06-27 16:34:59 -04003535 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003536 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003537 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003538 if (ssl->session->original_handshake_hash_len == 0) {
3539 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003540 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003541 }
Nick Harper95594012016-10-20 14:07:13 -07003542 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3543 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003544 }
3545
Steven Valdez908ac192017-01-12 13:17:07 -05003546 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3547 size_t hs_hash_len;
3548 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript, hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003549 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003550 }
Steven Valdez908ac192017-01-12 13:17:07 -05003551 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003552 SHA256_Final(out, &ctx);
3553 *out_len = SHA256_DIGEST_LENGTH;
3554 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003555}
Adam Langley1258b6a2014-06-20 12:00:00 -07003556
3557/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin45738dd2017-02-09 20:01:26 -05003558 * hashes in |hs->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003559 * data. */
Steven Valdez908ac192017-01-12 13:17:07 -05003560int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3561 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003562 /* This function should never be called for a resumed session because the
3563 * handshake hashes that we wish to record are for the original, full
3564 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003565 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003566 return -1;
3567 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003568
Steven Valdez908ac192017-01-12 13:17:07 -05003569 OPENSSL_COMPILE_ASSERT(
David Benjamin45738dd2017-02-09 20:01:26 -05003570 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
Steven Valdez908ac192017-01-12 13:17:07 -05003571 original_handshake_hash_is_too_small);
3572
3573 size_t digest_len;
3574 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript,
David Benjamin45738dd2017-02-09 20:01:26 -05003575 hs->new_session->original_handshake_hash,
Steven Valdez908ac192017-01-12 13:17:07 -05003576 &digest_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003577 return -1;
3578 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003579
Steven Valdez908ac192017-01-12 13:17:07 -05003580 OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE <= 0xff, max_md_size_is_too_large);
David Benjamin45738dd2017-02-09 20:01:26 -05003581 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003582
Adam Langleyfcf25832014-12-18 17:42:32 -08003583 return 1;
3584}
Nick Harper60a85cb2016-09-23 16:25:11 -07003585
3586int ssl_do_channel_id_callback(SSL *ssl) {
3587 if (ssl->tlsext_channel_id_private != NULL ||
3588 ssl->ctx->channel_id_cb == NULL) {
3589 return 1;
3590 }
3591
3592 EVP_PKEY *key = NULL;
3593 ssl->ctx->channel_id_cb(ssl, &key);
3594 if (key == NULL) {
3595 /* The caller should try again later. */
3596 return 1;
3597 }
3598
3599 int ret = SSL_set1_tls_channel_id(ssl, key);
3600 EVP_PKEY_free(key);
3601 return ret;
3602}
Adam Langleycfa08c32016-11-17 13:21:27 -08003603
3604int ssl_is_sct_list_valid(const CBS *contents) {
3605 /* Shallow parse the SCT list for sanity. By the RFC
3606 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3607 * of the SCTs may be empty. */
3608 CBS copy = *contents;
3609 CBS sct_list;
3610 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3611 CBS_len(&copy) != 0 ||
3612 CBS_len(&sct_list) == 0) {
3613 return 0;
3614 }
3615
3616 while (CBS_len(&sct_list) > 0) {
3617 CBS sct;
3618 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3619 CBS_len(&sct) == 0) {
3620 return 0;
3621 }
3622 }
3623
3624 return 1;
3625}