blob: 02f5a30d2b5ce8b6a1f2a7e14c4dd232102068b8 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin03973092014-06-24 23:27:17 -0400116#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400117#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/evp.h>
120#include <openssl/hmac.h>
121#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400122#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/rand.h>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700124#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400127#include "../crypto/internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800128
129
David Benjamin8c880a22016-12-03 02:20:34 -0500130static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
Adam Langley95c29f32014-06-20 12:00:00 -0700131
Adam Langleyfcf25832014-12-18 17:42:32 -0800132static int compare_uint16_t(const void *p1, const void *p2) {
133 uint16_t u1 = *((const uint16_t *)p1);
134 uint16_t u2 = *((const uint16_t *)p2);
135 if (u1 < u2) {
136 return -1;
137 } else if (u1 > u2) {
138 return 1;
139 } else {
140 return 0;
141 }
142}
David Benjamin35a7a442014-07-05 00:23:20 -0400143
Adam Langleyfcf25832014-12-18 17:42:32 -0800144/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
145 * more than one extension of the same type in a ClientHello or ServerHello.
146 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400147 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800148static int tls1_check_duplicate_extensions(const CBS *cbs) {
149 CBS extensions = *cbs;
150 size_t num_extensions = 0, i = 0;
151 uint16_t *extension_types = NULL;
152 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400153
Adam Langleyfcf25832014-12-18 17:42:32 -0800154 /* First pass: count the extensions. */
155 while (CBS_len(&extensions) > 0) {
156 uint16_t type;
157 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400158
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 if (!CBS_get_u16(&extensions, &type) ||
160 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
161 goto done;
162 }
David Benjamin35a7a442014-07-05 00:23:20 -0400163
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 num_extensions++;
165 }
David Benjamin35a7a442014-07-05 00:23:20 -0400166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 if (num_extensions == 0) {
168 return 1;
169 }
David Benjamin9a373592014-07-25 04:27:53 -0400170
Brian Smith5ba06892016-02-07 09:36:04 -1000171 extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800172 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400173 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800174 goto done;
175 }
David Benjamin35a7a442014-07-05 00:23:20 -0400176
Adam Langleyfcf25832014-12-18 17:42:32 -0800177 /* Second pass: gather the extension types. */
178 extensions = *cbs;
179 for (i = 0; i < num_extensions; i++) {
180 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400181
Adam Langleyfcf25832014-12-18 17:42:32 -0800182 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
183 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
184 /* This should not happen. */
185 goto done;
186 }
187 }
188 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400189
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 /* Sort the extensions and make sure there are no duplicates. */
191 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
192 for (i = 1; i < num_extensions; i++) {
193 if (extension_types[i - 1] == extension_types[i]) {
194 goto done;
195 }
196 }
David Benjamin35a7a442014-07-05 00:23:20 -0400197
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 ret = 1;
199
David Benjamin35a7a442014-07-05 00:23:20 -0400200done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400201 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 return ret;
203}
David Benjamin35a7a442014-07-05 00:23:20 -0400204
David Benjamin731058e2016-12-03 23:15:13 -0500205int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out, const uint8_t *in,
206 size_t in_len) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500207 OPENSSL_memset(out, 0, sizeof(*out));
David Benjamin731058e2016-12-03 23:15:13 -0500208 out->ssl = ssl;
209 out->client_hello = in;
210 out->client_hello_len = in_len;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400211
David Benjamine14ff062016-08-09 16:21:24 -0400212 CBS client_hello, random, session_id;
David Benjamin731058e2016-12-03 23:15:13 -0500213 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
214 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjamine14ff062016-08-09 16:21:24 -0400215 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
216 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
217 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 return 0;
219 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700220
David Benjamin731058e2016-12-03 23:15:13 -0500221 out->random = CBS_data(&random);
222 out->random_len = CBS_len(&random);
223 out->session_id = CBS_data(&session_id);
224 out->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 /* Skip past DTLS cookie */
David Benjamin731058e2016-12-03 23:15:13 -0500227 if (SSL_is_dtls(out->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800228 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400229 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
230 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 return 0;
232 }
233 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700234
David Benjamine14ff062016-08-09 16:21:24 -0400235 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800236 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400237 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
238 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 CBS_len(&compression_methods) < 1) {
240 return 0;
241 }
David Benjamine14ff062016-08-09 16:21:24 -0400242
David Benjamin731058e2016-12-03 23:15:13 -0500243 out->cipher_suites = CBS_data(&cipher_suites);
244 out->cipher_suites_len = CBS_len(&cipher_suites);
245 out->compression_methods = CBS_data(&compression_methods);
246 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 /* If the ClientHello ends here then it's valid, but doesn't have any
249 * extensions. (E.g. SSLv3.) */
250 if (CBS_len(&client_hello) == 0) {
David Benjamin731058e2016-12-03 23:15:13 -0500251 out->extensions = NULL;
252 out->extensions_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800253 return 1;
254 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700255
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 /* Extract extensions and check it is valid. */
David Benjamine14ff062016-08-09 16:21:24 -0400257 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
259 !tls1_check_duplicate_extensions(&extensions) ||
260 CBS_len(&client_hello) != 0) {
261 return 0;
262 }
David Benjamine14ff062016-08-09 16:21:24 -0400263
David Benjamin731058e2016-12-03 23:15:13 -0500264 out->extensions = CBS_data(&extensions);
265 out->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700266
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 return 1;
268}
Adam Langleydc9b1412014-06-20 12:00:00 -0700269
David Benjamin731058e2016-12-03 23:15:13 -0500270int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
271 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 CBS extensions;
David Benjamin731058e2016-12-03 23:15:13 -0500273 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 while (CBS_len(&extensions) != 0) {
David Benjamincec73442016-08-02 17:41:33 -0400275 /* Decode the next extension. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 uint16_t type;
277 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 if (!CBS_get_u16(&extensions, &type) ||
279 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
280 return 0;
281 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700282
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400284 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 return 1;
286 }
287 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700288
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 return 0;
290}
Adam Langley95c29f32014-06-20 12:00:00 -0700291
David Benjamin731058e2016-12-03 23:15:13 -0500292int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
293 uint16_t extension_type,
294 const uint8_t **out_data,
295 size_t *out_len) {
David Benjamincec73442016-08-02 17:41:33 -0400296 CBS cbs;
David Benjamin731058e2016-12-03 23:15:13 -0500297 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
David Benjamincec73442016-08-02 17:41:33 -0400298 return 0;
299 }
300
301 *out_data = CBS_data(&cbs);
302 *out_len = CBS_len(&cbs);
303 return 1;
304}
305
Steven Valdezce902a92016-05-17 11:47:53 -0400306static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400307 SSL_CURVE_X25519,
308 SSL_CURVE_SECP256R1,
309 SSL_CURVE_SECP384R1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800310};
Adam Langley95c29f32014-06-20 12:00:00 -0700311
David Benjaminf04976b2016-10-07 00:37:55 -0400312void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
Steven Valdez5440fe02016-07-18 12:40:30 -0400313 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400314 *out_group_ids = ssl->supported_group_list;
315 *out_group_ids_len = ssl->supported_group_list_len;
316 if (!*out_group_ids) {
317 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400318 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 }
320}
David Benjamined439582014-07-14 19:13:02 -0400321
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900322int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
323 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400324 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400325
David Benjaminf04976b2016-10-07 00:37:55 -0400326 const uint16_t *groups, *pref, *supp;
327 size_t groups_len, pref_len, supp_len;
328 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800329
David Benjaminf04976b2016-10-07 00:37:55 -0400330 /* Clients are not required to send a supported_groups extension. In this
331 * case, the server is free to pick any group it likes. See RFC 4492,
332 * section 4, paragraph 3.
333 *
334 * However, in the interests of compatibility, we will skip ECDH if the
335 * client didn't send an extension because we can't be sure that they'll
336 * support our favoured group. Thus we do not special-case an emtpy
337 * |peer_supported_group_list|. */
David Benjamin55a43642015-04-20 14:45:55 -0400338
David Benjamin4298d772015-12-19 00:18:25 -0500339 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400340 pref = groups;
341 pref_len = groups_len;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900342 supp = hs->peer_supported_group_list;
343 supp_len = hs->peer_supported_group_list_len;
David Benjamin55a43642015-04-20 14:45:55 -0400344 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900345 pref = hs->peer_supported_group_list;
346 pref_len = hs->peer_supported_group_list_len;
Steven Valdezce902a92016-05-17 11:47:53 -0400347 supp = groups;
348 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400349 }
350
David Benjaminf04976b2016-10-07 00:37:55 -0400351 for (size_t i = 0; i < pref_len; i++) {
352 for (size_t j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800353 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400354 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500355 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800356 }
357 }
358 }
359
David Benjamin4298d772015-12-19 00:18:25 -0500360 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800361}
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Steven Valdezce902a92016-05-17 11:47:53 -0400363int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400365 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800366
Steven Valdezce902a92016-05-17 11:47:53 -0400367 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
368 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 return 0;
370 }
371
David Benjamin54091232016-09-05 12:47:25 -0400372 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400373 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
374 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 return 0;
376 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 }
378
Steven Valdezce902a92016-05-17 11:47:53 -0400379 OPENSSL_free(*out_group_ids);
380 *out_group_ids = group_ids;
381 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800382
383 return 1;
384}
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100386int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
387 const char *curves) {
388 uint16_t *group_ids = NULL;
389 size_t ncurves = 0;
390
391 const char *col;
392 const char *ptr = curves;
393
394 do {
395 col = strchr(ptr, ':');
396
397 uint16_t group_id;
398 if (!ssl_name_to_group_id(&group_id, ptr,
399 col ? (size_t)(col - ptr) : strlen(ptr))) {
400 goto err;
401 }
402
403 uint16_t *new_group_ids = OPENSSL_realloc(group_ids,
404 (ncurves + 1) * sizeof(uint16_t));
405 if (new_group_ids == NULL) {
406 goto err;
407 }
408 group_ids = new_group_ids;
409
410 group_ids[ncurves] = group_id;
411 ncurves++;
412
413 if (col) {
414 ptr = col + 1;
415 }
416 } while (col);
417
418 OPENSSL_free(*out_group_ids);
419 *out_group_ids = group_ids;
420 *out_group_ids_len = ncurves;
421
422 return 1;
423
424err:
425 OPENSSL_free(group_ids);
426 return 0;
427}
428
Steven Valdezce902a92016-05-17 11:47:53 -0400429int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
430 const uint16_t *groups;
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400431 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -0400432 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400433 for (size_t i = 0; i < groups_len; i++) {
434 if (groups[i] == group_id) {
435 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800436 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 }
David Benjamin033e5f42014-11-13 18:47:41 -0500438
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400439 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800440}
David Benjamin033e5f42014-11-13 18:47:41 -0500441
David Benjamin3ef76972016-10-17 17:59:54 -0400442/* kVerifySignatureAlgorithms is the default list of accepted signature
David Benjamin3a322f52016-10-26 12:45:35 -0400443 * algorithms for verifying.
444 *
445 * For now, RSA-PSS signature algorithms are not enabled on Android's system
446 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
447 * restore them. */
David Benjamin3ef76972016-10-17 17:59:54 -0400448static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjamin69522112017-03-28 15:38:29 -0500449 /* List our preferred algorithms first. */
450 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400451 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400452#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400453 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400454#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400455 SSL_SIGN_RSA_PKCS1_SHA256,
456
457 /* Larger hashes are acceptable. */
458 SSL_SIGN_ECDSA_SECP384R1_SHA384,
459#if !defined(BORINGSSL_ANDROID_SYSTEM)
460 SSL_SIGN_RSA_PSS_SHA384,
461#endif
462 SSL_SIGN_RSA_PKCS1_SHA384,
463
David Benjamin76bb1412016-09-08 16:03:49 -0400464 /* TODO(davidben): Remove this. */
David Benjamin3ef76972016-10-17 17:59:54 -0400465#if defined(BORINGSSL_ANDROID_SYSTEM)
466 SSL_SIGN_ECDSA_SECP521R1_SHA512,
467#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400468#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400469 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400470#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400471 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400472
David Benjamin3a322f52016-10-26 12:45:35 -0400473 /* For now, SHA-1 is still accepted but least preferable. */
474 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400475
David Benjamin3a322f52016-10-26 12:45:35 -0400476};
477
478/* kSignSignatureAlgorithms is the default list of supported signature
479 * algorithms for signing.
480 *
481 * For now, RSA-PSS signature algorithms are not enabled on Android's system
482 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
483 * restore them. */
484static const uint16_t kSignSignatureAlgorithms[] = {
David Benjamin69522112017-03-28 15:38:29 -0500485 /* List our preferred algorithms first. */
486 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400487 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400488#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400489 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400490#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400491 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400492
David Benjamin3a322f52016-10-26 12:45:35 -0400493 /* If needed, sign larger hashes.
494 *
495 * TODO(davidben): Determine which of these may be pruned. */
496 SSL_SIGN_ECDSA_SECP384R1_SHA384,
497#if !defined(BORINGSSL_ANDROID_SYSTEM)
498 SSL_SIGN_RSA_PSS_SHA384,
499#endif
500 SSL_SIGN_RSA_PKCS1_SHA384,
501
502 SSL_SIGN_ECDSA_SECP521R1_SHA512,
503#if !defined(BORINGSSL_ANDROID_SYSTEM)
504 SSL_SIGN_RSA_PSS_SHA512,
505#endif
506 SSL_SIGN_RSA_PKCS1_SHA512,
507
508 /* If the peer supports nothing else, sign with SHA-1. */
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400509 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400510 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400511};
512
David Benjamin69522112017-03-28 15:38:29 -0500513void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
514 ctx->ed25519_enabled = !!enabled;
515}
516
517int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) {
David Benjamin71c21b42017-04-14 17:05:40 -0400518 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
519 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
520 if (ssl->ctx->num_verify_sigalgs != 0) {
521 sigalgs = ssl->ctx->verify_sigalgs;
522 num_sigalgs = ssl->ctx->num_verify_sigalgs;
523 }
524
525 for (size_t i = 0; i < num_sigalgs; i++) {
526 if (sigalgs == kVerifySignatureAlgorithms &&
527 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500528 !ssl->ctx->ed25519_enabled) {
529 continue;
530 }
David Benjamin71c21b42017-04-14 17:05:40 -0400531 if (!CBB_add_u16(out, sigalgs[i])) {
David Benjamin69522112017-03-28 15:38:29 -0500532 return 0;
533 }
534 }
535
536 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800537}
Adam Langley95c29f32014-06-20 12:00:00 -0700538
David Benjamin8d606e32017-06-15 22:43:04 -0400539int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg) {
David Benjamin71c21b42017-04-14 17:05:40 -0400540 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
541 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
542 if (ssl->ctx->num_verify_sigalgs != 0) {
543 sigalgs = ssl->ctx->verify_sigalgs;
544 num_sigalgs = ssl->ctx->num_verify_sigalgs;
545 }
546
547 for (size_t i = 0; i < num_sigalgs; i++) {
548 if (sigalgs == kVerifySignatureAlgorithms &&
549 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500550 !ssl->ctx->ed25519_enabled) {
551 continue;
552 }
David Benjamin71c21b42017-04-14 17:05:40 -0400553 if (sigalg == sigalgs[i]) {
David Benjamin3ef76972016-10-17 17:59:54 -0400554 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800555 }
556 }
557
David Benjamin3ef76972016-10-17 17:59:54 -0400558 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
559 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
560 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800561}
562
Adam Langley614c66a2015-06-12 15:26:58 -0700563/* tls_extension represents a TLS extension that is handled internally. The
564 * |init| function is called for each handshake, before any other functions of
565 * the extension. Then the add and parse callbacks are called as needed.
566 *
567 * The parse callbacks receive a |CBS| that contains the contents of the
568 * extension (i.e. not including the type and length bytes). If an extension is
569 * not received then the parse callbacks will be called with a NULL CBS so that
570 * they can do any processing needed to handle the absence of an extension.
571 *
572 * The add callbacks receive a |CBB| to which the extension can be appended but
573 * the function is responsible for appending the type and length bytes too.
574 *
575 * All callbacks return one for success and zero for error. If a parse function
576 * returns zero then a fatal alert with value |*out_alert| will be sent. If
577 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
578struct tls_extension {
579 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500580 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700581
David Benjamin8c880a22016-12-03 02:20:34 -0500582 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
583 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
584 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700585
David Benjamin8c880a22016-12-03 02:20:34 -0500586 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
587 CBS *contents);
588 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700589};
590
David Benjamin8c880a22016-12-03 02:20:34 -0500591static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
592 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400593 if (contents != NULL) {
594 /* Servers MUST NOT send this extension. */
595 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
596 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
597 return 0;
598 }
599
600 return 1;
601}
602
David Benjamin8c880a22016-12-03 02:20:34 -0500603static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
604 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400605 /* This extension from the client is handled elsewhere. */
606 return 1;
607}
608
David Benjamin8c880a22016-12-03 02:20:34 -0500609static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400610 return 1;
611}
Adam Langley614c66a2015-06-12 15:26:58 -0700612
613/* Server name indication (SNI).
614 *
615 * https://tools.ietf.org/html/rfc6066#section-3. */
616
David Benjamin8c880a22016-12-03 02:20:34 -0500617static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
618 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700619 if (ssl->tlsext_hostname == NULL) {
620 return 1;
621 }
622
623 CBB contents, server_name_list, name;
624 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
625 !CBB_add_u16_length_prefixed(out, &contents) ||
626 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
627 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
628 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
629 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
630 strlen(ssl->tlsext_hostname)) ||
631 !CBB_flush(out)) {
632 return 0;
633 }
634
635 return 1;
636}
637
David Benjamin8c880a22016-12-03 02:20:34 -0500638static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500639 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500640 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700641 if (contents == NULL) {
642 return 1;
643 }
644
645 if (CBS_len(contents) != 0) {
646 return 0;
647 }
648
649 assert(ssl->tlsext_hostname != NULL);
650
Steven Valdez87eab492016-06-27 16:34:59 -0400651 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500652 OPENSSL_free(hs->new_session->tlsext_hostname);
653 hs->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
654 if (!hs->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700655 *out_alert = SSL_AD_INTERNAL_ERROR;
656 return 0;
657 }
658 }
659
660 return 1;
661}
662
David Benjamin8c880a22016-12-03 02:20:34 -0500663static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500664 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700665 if (contents == NULL) {
666 return 1;
667 }
668
David Benjamin9b611e22016-03-03 08:48:30 -0500669 CBS server_name_list, host_name;
670 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700671 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500672 !CBS_get_u8(&server_name_list, &name_type) ||
673 /* Although the server_name extension was intended to be extensible to
674 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
675 * different name types will cause an error. Further, RFC 4366 originally
676 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
677 * adding new name types is no longer feasible.
678 *
679 * Act as if the extensibility does not exist to simplify parsing. */
680 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
681 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700682 CBS_len(contents) != 0) {
683 return 0;
684 }
685
David Benjamin9b611e22016-03-03 08:48:30 -0500686 if (name_type != TLSEXT_NAMETYPE_host_name ||
687 CBS_len(&host_name) == 0 ||
688 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
689 CBS_contains_zero_byte(&host_name)) {
690 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
691 return 0;
692 }
Adam Langley614c66a2015-06-12 15:26:58 -0700693
David Benjamin4eb95cc2016-11-16 17:08:23 +0900694 /* Copy the hostname as a string. */
David Benjamin8c880a22016-12-03 02:20:34 -0500695 if (!CBS_strdup(&host_name, &hs->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900696 *out_alert = SSL_AD_INTERNAL_ERROR;
697 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700698 }
699
David Benjamin8c880a22016-12-03 02:20:34 -0500700 hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700701 return 1;
702}
703
David Benjamin8c880a22016-12-03 02:20:34 -0500704static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
705 if (hs->ssl->s3->session_reused ||
706 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700707 return 1;
708 }
709
710 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
711 !CBB_add_u16(out, 0 /* length */)) {
712 return 0;
713 }
714
715 return 1;
716}
717
718
Adam Langley5021b222015-06-12 18:27:58 -0700719/* Renegotiation indication.
720 *
721 * https://tools.ietf.org/html/rfc5746 */
722
David Benjamin8c880a22016-12-03 02:20:34 -0500723static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
724 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400725 /* Renegotiation indication is not necessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -0400726 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400727 return 1;
728 }
729
David Benjamin52bf6902016-10-08 12:05:03 -0400730 assert(ssl->s3->initial_handshake_complete ==
731 (ssl->s3->previous_client_finished_len != 0));
732
Adam Langley5021b222015-06-12 18:27:58 -0700733 CBB contents, prev_finished;
734 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
735 !CBB_add_u16_length_prefixed(out, &contents) ||
736 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
737 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
738 ssl->s3->previous_client_finished_len) ||
739 !CBB_flush(out)) {
740 return 0;
741 }
742
743 return 1;
744}
745
David Benjamin8c880a22016-12-03 02:20:34 -0500746static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700747 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500748 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400749 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez246eeee2017-03-26 12:49:17 -0500750 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400751 return 0;
752 }
753
David Benjamin3e052de2015-11-25 20:10:31 -0500754 /* Servers may not switch between omitting the extension and supporting it.
755 * See RFC 5746, sections 3.5 and 4.2. */
756 if (ssl->s3->initial_handshake_complete &&
757 (contents != NULL) != ssl->s3->send_connection_binding) {
758 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
759 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
760 return 0;
761 }
762
Adam Langley5021b222015-06-12 18:27:58 -0700763 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500764 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700765 * RI even on initial ServerHello because the client doesn't see any
766 * renegotiation during an attack. However this would mean we could not
767 * connect to any server which doesn't support RI.
768 *
David Benjamine9cddb82015-11-23 14:36:40 -0500769 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
770 * practical terms every client sets it so it's just assumed here. */
771 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700772 }
773
774 const size_t expected_len = ssl->s3->previous_client_finished_len +
775 ssl->s3->previous_server_finished_len;
776
777 /* Check for logic errors */
778 assert(!expected_len || ssl->s3->previous_client_finished_len);
779 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400780 assert(ssl->s3->initial_handshake_complete ==
781 (ssl->s3->previous_client_finished_len != 0));
782 assert(ssl->s3->initial_handshake_complete ==
783 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700784
785 /* Parse out the extension contents. */
786 CBS renegotiated_connection;
787 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
788 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400789 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700790 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
791 return 0;
792 }
793
794 /* Check that the extension matches. */
795 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400796 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700797 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
798 return 0;
799 }
800
801 const uint8_t *d = CBS_data(&renegotiated_connection);
802 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
803 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400804 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700805 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
806 return 0;
807 }
808 d += ssl->s3->previous_client_finished_len;
809
810 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
811 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400812 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700813 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
814 return 0;
815 }
816 ssl->s3->send_connection_binding = 1;
817
818 return 1;
819}
820
David Benjamin8c880a22016-12-03 02:20:34 -0500821static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700822 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500823 SSL *const ssl = hs->ssl;
Adam Langley5021b222015-06-12 18:27:58 -0700824 /* Renegotiation isn't supported as a server so this function should never be
825 * called after the initial handshake. */
826 assert(!ssl->s3->initial_handshake_complete);
827
Steven Valdez143e8b32016-07-11 13:19:03 -0400828 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
829 return 1;
830 }
831
Adam Langley5021b222015-06-12 18:27:58 -0700832 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400833 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700834 }
835
836 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700837 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
838 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400839 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700840 return 0;
841 }
842
David Benjamin52bf6902016-10-08 12:05:03 -0400843 /* Check that the extension matches. We do not support renegotiation as a
844 * server, so this must be empty. */
845 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400846 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700847 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
848 return 0;
849 }
850
851 ssl->s3->send_connection_binding = 1;
852
853 return 1;
854}
855
David Benjamin8c880a22016-12-03 02:20:34 -0500856static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
857 SSL *const ssl = hs->ssl;
David Benjamin52bf6902016-10-08 12:05:03 -0400858 /* Renegotiation isn't supported as a server so this function should never be
859 * called after the initial handshake. */
860 assert(!ssl->s3->initial_handshake_complete);
861
Steven Valdez143e8b32016-07-11 13:19:03 -0400862 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
863 return 1;
864 }
865
Adam Langley5021b222015-06-12 18:27:58 -0700866 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400867 !CBB_add_u16(out, 1 /* length */) ||
868 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700869 return 0;
870 }
871
872 return 1;
873}
874
Adam Langley0a056712015-07-01 15:03:33 -0700875
876/* Extended Master Secret.
877 *
David Benjamin43946d42016-02-01 08:42:19 -0500878 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700879
David Benjamin8c880a22016-12-03 02:20:34 -0500880static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400881 /* Extended master secret is not necessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -0400882 if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700883 return 1;
884 }
885
886 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
887 !CBB_add_u16(out, 0 /* length */)) {
888 return 0;
889 }
890
891 return 1;
892}
893
David Benjamin8c880a22016-12-03 02:20:34 -0500894static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700895 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500896 SSL *const ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500897
898 if (contents != NULL) {
899 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
900 ssl->version == SSL3_VERSION ||
901 CBS_len(contents) != 0) {
David Benjamin163c9562016-08-29 23:14:17 -0400902 return 0;
903 }
904
David Benjaminfc02b592017-02-17 16:26:01 -0500905 hs->extended_master_secret = 1;
David Benjamin163c9562016-08-29 23:14:17 -0400906 }
907
David Benjaminfc02b592017-02-17 16:26:01 -0500908 /* Whether EMS is negotiated may not change on renegotiation. */
909 if (ssl->s3->established_session != NULL &&
910 hs->extended_master_secret !=
911 ssl->s3->established_session->extended_master_secret) {
912 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
913 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400914 return 0;
915 }
916
Adam Langley0a056712015-07-01 15:03:33 -0700917 return 1;
918}
919
David Benjamin8c880a22016-12-03 02:20:34 -0500920static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500921 CBS *contents) {
David Benjaminfc02b592017-02-17 16:26:01 -0500922 uint16_t version = ssl3_protocol_version(hs->ssl);
David Benjamin8c880a22016-12-03 02:20:34 -0500923 if (version >= TLS1_3_VERSION ||
924 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400925 return 1;
926 }
927
928 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700929 return 1;
930 }
931
932 if (CBS_len(contents) != 0) {
933 return 0;
934 }
935
David Benjaminfc02b592017-02-17 16:26:01 -0500936 hs->extended_master_secret = 1;
Adam Langley0a056712015-07-01 15:03:33 -0700937 return 1;
938}
939
David Benjamin8c880a22016-12-03 02:20:34 -0500940static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -0500941 if (!hs->extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700942 return 1;
943 }
944
945 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
946 !CBB_add_u16(out, 0 /* length */)) {
947 return 0;
948 }
949
950 return 1;
951}
952
Adam Langley9b05bc52015-07-01 15:25:33 -0700953
954/* Session tickets.
955 *
956 * https://tools.ietf.org/html/rfc5077 */
957
David Benjamin8c880a22016-12-03 02:20:34 -0500958static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
959 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400960 /* TLS 1.3 uses a different ticket extension. */
David Benjamin68161cb2017-06-20 14:49:43 -0400961 if (hs->min_version >= TLS1_3_VERSION ||
David Benjamin7c7d8312016-08-20 13:39:03 -0400962 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700963 return 1;
964 }
965
966 const uint8_t *ticket_data = NULL;
967 int ticket_len = 0;
968
969 /* Renegotiation does not participate in session resumption. However, still
970 * advertise the extension to avoid potentially breaking servers which carry
971 * over the state from the previous handshake, such as OpenSSL servers
972 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
973 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. */
Steven Valdez8f36c512017-06-20 10:55:02 -0400977 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700978 ticket_data = ssl->session->tlsext_tick;
979 ticket_len = ssl->session->tlsext_ticklen;
980 }
981
982 CBB ticket;
983 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
984 !CBB_add_u16_length_prefixed(out, &ticket) ||
985 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
986 !CBB_flush(out)) {
987 return 0;
988 }
989
990 return 1;
991}
992
David Benjamin8c880a22016-12-03 02:20:34 -0500993static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -0700994 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500995 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -0700996 if (contents == NULL) {
997 return 1;
998 }
999
Steven Valdez143e8b32016-07-11 13:19:03 -04001000 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1001 return 0;
1002 }
1003
Adam Langley9b05bc52015-07-01 15:25:33 -07001004 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1005 * this function should never be called, even if the server tries to send the
1006 * extension. */
1007 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1008
1009 if (CBS_len(contents) != 0) {
1010 return 0;
1011 }
1012
David Benjamin8c880a22016-12-03 02:20:34 -05001013 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001014 return 1;
1015}
1016
David Benjamin8c880a22016-12-03 02:20:34 -05001017static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1018 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001019 return 1;
1020 }
1021
David Benjamin78476f62016-11-12 11:20:55 +09001022 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin8c880a22016-12-03 02:20:34 -05001023 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001024
1025 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1026 !CBB_add_u16(out, 0 /* length */)) {
1027 return 0;
1028 }
1029
1030 return 1;
1031}
1032
1033
Adam Langley2e857bd2015-07-01 16:09:19 -07001034/* Signature Algorithms.
1035 *
1036 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1037
David Benjamin8c880a22016-12-03 02:20:34 -05001038static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1039 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001040 if (hs->max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001041 return 1;
1042 }
1043
David Benjamin0fc37ef2016-08-17 15:29:46 -04001044 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001045 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1046 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin69522112017-03-28 15:38:29 -05001047 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
1048 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
1049 !CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001050 return 0;
1051 }
1052
1053 return 1;
1054}
1055
David Benjamin8c880a22016-12-03 02:20:34 -05001056static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001057 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001058 OPENSSL_free(hs->peer_sigalgs);
1059 hs->peer_sigalgs = NULL;
1060 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001061
Adam Langley2e857bd2015-07-01 16:09:19 -07001062 if (contents == NULL) {
1063 return 1;
1064 }
1065
1066 CBS supported_signature_algorithms;
1067 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001068 CBS_len(contents) != 0 ||
1069 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001070 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001071 return 0;
1072 }
1073
1074 return 1;
1075}
1076
Adam Langley2e857bd2015-07-01 16:09:19 -07001077
Adam Langleybb0bd042015-07-01 16:21:03 -07001078/* OCSP Stapling.
1079 *
1080 * https://tools.ietf.org/html/rfc6066#section-8 */
1081
David Benjamin8c880a22016-12-03 02:20:34 -05001082static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1083 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001084 if (!ssl->ocsp_stapling_enabled) {
1085 return 1;
1086 }
1087
1088 CBB contents;
1089 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1090 !CBB_add_u16_length_prefixed(out, &contents) ||
1091 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1092 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1093 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1094 !CBB_flush(out)) {
1095 return 0;
1096 }
1097
1098 return 1;
1099}
1100
David Benjamin8c880a22016-12-03 02:20:34 -05001101static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001102 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001103 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001104 if (contents == NULL) {
1105 return 1;
1106 }
1107
Steven Valdeza833c352016-11-01 13:39:36 -04001108 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1109 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001110 return 0;
1111 }
1112
Steven Valdeza833c352016-11-01 13:39:36 -04001113 /* OCSP stapling is forbidden on non-certificate ciphers. */
1114 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001115 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001116 return 0;
1117 }
1118
Steven Valdeza833c352016-11-01 13:39:36 -04001119 /* Note this does not check for resumption in TLS 1.2. Sending
1120 * status_request here does not make sense, but OpenSSL does so and the
1121 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001122
David Benjamin8c880a22016-12-03 02:20:34 -05001123 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001124 return 1;
1125}
1126
David Benjamin8c880a22016-12-03 02:20:34 -05001127static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001128 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001129 if (contents == NULL) {
1130 return 1;
1131 }
1132
1133 uint8_t status_type;
1134 if (!CBS_get_u8(contents, &status_type)) {
1135 return 0;
1136 }
1137
1138 /* We cannot decide whether OCSP stapling will occur yet because the correct
1139 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001140 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001141
Adam Langleybb0bd042015-07-01 16:21:03 -07001142 return 1;
1143}
1144
David Benjamin8c880a22016-12-03 02:20:34 -05001145static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1146 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001147 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001148 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001149 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001150 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001151 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001152 return 1;
1153 }
1154
David Benjamin8c880a22016-12-03 02:20:34 -05001155 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001156
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001157 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001158 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001159}
1160
1161
Adam Langley97dfcbf2015-07-01 18:35:20 -07001162/* Next protocol negotiation.
1163 *
1164 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1165
David Benjamin8c880a22016-12-03 02:20:34 -05001166static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1167 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001168 if (ssl->s3->initial_handshake_complete ||
1169 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001170 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001171 return 1;
1172 }
1173
1174 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1175 !CBB_add_u16(out, 0 /* length */)) {
1176 return 0;
1177 }
1178
1179 return 1;
1180}
1181
David Benjamin8c880a22016-12-03 02:20:34 -05001182static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001183 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001184 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001185 if (contents == NULL) {
1186 return 1;
1187 }
1188
Steven Valdez143e8b32016-07-11 13:19:03 -04001189 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1190 return 0;
1191 }
1192
Adam Langley97dfcbf2015-07-01 18:35:20 -07001193 /* If any of these are false then we should never have sent the NPN
1194 * extension in the ClientHello and thus this function should never have been
1195 * called. */
1196 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001197 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001198 assert(ssl->ctx->next_proto_select_cb != NULL);
1199
David Benjamin76c2efc2015-08-31 14:24:29 -04001200 if (ssl->s3->alpn_selected != NULL) {
1201 /* NPN and ALPN may not be negotiated in the same connection. */
1202 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1203 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1204 return 0;
1205 }
1206
Adam Langley97dfcbf2015-07-01 18:35:20 -07001207 const uint8_t *const orig_contents = CBS_data(contents);
1208 const size_t orig_len = CBS_len(contents);
1209
1210 while (CBS_len(contents) != 0) {
1211 CBS proto;
1212 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1213 CBS_len(&proto) == 0) {
1214 return 0;
1215 }
1216 }
1217
1218 uint8_t *selected;
1219 uint8_t selected_len;
1220 if (ssl->ctx->next_proto_select_cb(
1221 ssl, &selected, &selected_len, orig_contents, orig_len,
1222 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1223 *out_alert = SSL_AD_INTERNAL_ERROR;
1224 return 0;
1225 }
1226
David Benjamin79978df2015-12-25 15:56:49 -05001227 OPENSSL_free(ssl->s3->next_proto_negotiated);
1228 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1229 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001230 *out_alert = SSL_AD_INTERNAL_ERROR;
1231 return 0;
1232 }
1233
David Benjamin79978df2015-12-25 15:56:49 -05001234 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001235 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001236
1237 return 1;
1238}
1239
David Benjamin8c880a22016-12-03 02:20:34 -05001240static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001241 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001242 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001243 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1244 return 1;
1245 }
1246
Adam Langley97dfcbf2015-07-01 18:35:20 -07001247 if (contents != NULL && CBS_len(contents) != 0) {
1248 return 0;
1249 }
1250
1251 if (contents == NULL ||
1252 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001253 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001254 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001255 return 1;
1256 }
1257
David Benjamin8c880a22016-12-03 02:20:34 -05001258 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001259 return 1;
1260}
1261
David Benjamin8c880a22016-12-03 02:20:34 -05001262static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1263 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001264 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1265 * parsed. */
David Benjamin8c880a22016-12-03 02:20:34 -05001266 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001267 return 1;
1268 }
1269
1270 const uint8_t *npa;
1271 unsigned npa_len;
1272
1273 if (ssl->ctx->next_protos_advertised_cb(
1274 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1275 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001276 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001277 return 1;
1278 }
1279
1280 CBB contents;
1281 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1282 !CBB_add_u16_length_prefixed(out, &contents) ||
1283 !CBB_add_bytes(&contents, npa, npa_len) ||
1284 !CBB_flush(out)) {
1285 return 0;
1286 }
1287
1288 return 1;
1289}
1290
1291
Adam Langleyab8d87d2015-07-10 12:21:39 -07001292/* Signed certificate timestamps.
1293 *
1294 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1295
David Benjamin8c880a22016-12-03 02:20:34 -05001296static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1297 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001298 if (!ssl->signed_cert_timestamps_enabled) {
1299 return 1;
1300 }
1301
1302 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1303 !CBB_add_u16(out, 0 /* length */)) {
1304 return 0;
1305 }
1306
1307 return 1;
1308}
1309
David Benjamin8c880a22016-12-03 02:20:34 -05001310static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001311 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001312 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001313 if (contents == NULL) {
1314 return 1;
1315 }
1316
Steven Valdeza833c352016-11-01 13:39:36 -04001317 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1318 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001319 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001320 return 0;
1321 }
1322
Adam Langleyab8d87d2015-07-10 12:21:39 -07001323 /* If this is false then we should never have sent the SCT extension in the
1324 * ClientHello and thus this function should never have been called. */
1325 assert(ssl->signed_cert_timestamps_enabled);
1326
Adam Langleycfa08c32016-11-17 13:21:27 -08001327 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001328 *out_alert = SSL_AD_DECODE_ERROR;
1329 return 0;
1330 }
1331
David Benjamindaa88502016-10-04 16:32:16 -04001332 /* Session resumption uses the original session information. The extension
1333 * should not be sent on resumption, but RFC 6962 did not make it a
1334 * requirement, so tolerate this.
1335 *
1336 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001337 if (!ssl->s3->session_reused &&
David Benjamin45738dd2017-02-09 20:01:26 -05001338 !CBS_stow(contents, &hs->new_session->tlsext_signed_cert_timestamp_list,
1339 &hs->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001340 *out_alert = SSL_AD_INTERNAL_ERROR;
1341 return 0;
1342 }
1343
1344 return 1;
1345}
1346
David Benjamin8c880a22016-12-03 02:20:34 -05001347static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001348 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001349 if (contents == NULL) {
1350 return 1;
1351 }
1352
1353 if (CBS_len(contents) != 0) {
1354 return 0;
1355 }
1356
David Benjamin8c880a22016-12-03 02:20:34 -05001357 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001358 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001359}
1360
David Benjamin8c880a22016-12-03 02:20:34 -05001361static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1362 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001363 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001364 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1365 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001366 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001367 return 1;
1368 }
1369
1370 CBB contents;
1371 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1372 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001373 CBB_add_bytes(
1374 &contents,
1375 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1376 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001377 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001378}
1379
1380
Adam Langleyf18e4532015-07-10 13:39:53 -07001381/* Application-level Protocol Negotiation.
1382 *
1383 * https://tools.ietf.org/html/rfc7301 */
1384
David Benjamin8c880a22016-12-03 02:20:34 -05001385static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1386 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001387 if (ssl->alpn_client_proto_list == NULL ||
1388 ssl->s3->initial_handshake_complete) {
1389 return 1;
1390 }
1391
1392 CBB contents, proto_list;
1393 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1394 !CBB_add_u16_length_prefixed(out, &contents) ||
1395 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1396 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1397 ssl->alpn_client_proto_list_len) ||
1398 !CBB_flush(out)) {
1399 return 0;
1400 }
1401
1402 return 1;
1403}
1404
David Benjamin8c880a22016-12-03 02:20:34 -05001405static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001406 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001407 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001408 if (contents == NULL) {
1409 return 1;
1410 }
1411
1412 assert(!ssl->s3->initial_handshake_complete);
1413 assert(ssl->alpn_client_proto_list != NULL);
1414
David Benjamin8c880a22016-12-03 02:20:34 -05001415 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001416 /* NPN and ALPN may not be negotiated in the same connection. */
1417 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1418 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1419 return 0;
1420 }
1421
Adam Langleyf18e4532015-07-10 13:39:53 -07001422 /* The extension data consists of a ProtocolNameList which must have
1423 * exactly one ProtocolName. Each of these is length-prefixed. */
1424 CBS protocol_name_list, protocol_name;
1425 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1426 CBS_len(contents) != 0 ||
1427 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1428 /* Empty protocol names are forbidden. */
1429 CBS_len(&protocol_name) == 0 ||
1430 CBS_len(&protocol_name_list) != 0) {
1431 return 0;
1432 }
1433
David Benjaminc8ff30c2017-04-04 13:52:36 -04001434 if (!ssl->ctx->allow_unknown_alpn_protos) {
1435 /* Check that the protocol name is one of the ones we advertised. */
1436 int protocol_ok = 0;
1437 CBS client_protocol_name_list, client_protocol_name;
1438 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1439 ssl->alpn_client_proto_list_len);
1440 while (CBS_len(&client_protocol_name_list) > 0) {
1441 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1442 &client_protocol_name)) {
1443 *out_alert = SSL_AD_INTERNAL_ERROR;
1444 return 0;
1445 }
1446
1447 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1448 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1449 CBS_data(&protocol_name),
1450 CBS_len(&protocol_name)) == 0) {
1451 protocol_ok = 1;
1452 break;
1453 }
1454 }
1455
1456 if (!protocol_ok) {
1457 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1458 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3e51757d2016-08-11 11:52:23 -04001459 return 0;
1460 }
David Benjamin3e51757d2016-08-11 11:52:23 -04001461 }
1462
Adam Langleyf18e4532015-07-10 13:39:53 -07001463 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1464 &ssl->s3->alpn_selected_len)) {
1465 *out_alert = SSL_AD_INTERNAL_ERROR;
1466 return 0;
1467 }
1468
1469 return 1;
1470}
1471
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001472int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001473 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001474 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001475 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001476 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001477 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001478 client_hello, &contents,
1479 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1480 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001481 return 1;
1482 }
1483
1484 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001485 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001486
1487 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001488 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1489 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001490 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001491 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1492 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001493 return 0;
1494 }
1495
1496 /* Validate the protocol list. */
1497 CBS protocol_name_list_copy = protocol_name_list;
1498 while (CBS_len(&protocol_name_list_copy) > 0) {
1499 CBS protocol_name;
1500
1501 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1502 /* Empty protocol names are forbidden. */
1503 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001504 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1505 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001506 return 0;
1507 }
1508 }
1509
1510 const uint8_t *selected;
1511 uint8_t selected_len;
1512 if (ssl->ctx->alpn_select_cb(
1513 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1514 CBS_len(&protocol_name_list),
1515 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1516 OPENSSL_free(ssl->s3->alpn_selected);
1517 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1518 if (ssl->s3->alpn_selected == NULL) {
1519 *out_alert = SSL_AD_INTERNAL_ERROR;
1520 return 0;
1521 }
1522 ssl->s3->alpn_selected_len = selected_len;
1523 }
1524
1525 return 1;
1526}
1527
David Benjamin8c880a22016-12-03 02:20:34 -05001528static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1529 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001530 if (ssl->s3->alpn_selected == NULL) {
1531 return 1;
1532 }
1533
1534 CBB contents, proto_list, proto;
1535 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1536 !CBB_add_u16_length_prefixed(out, &contents) ||
1537 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1538 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001539 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1540 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001541 !CBB_flush(out)) {
1542 return 0;
1543 }
1544
1545 return 1;
1546}
1547
1548
Adam Langley49c7af12015-07-10 14:33:46 -07001549/* Channel ID.
1550 *
1551 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1552
David Benjamin8c880a22016-12-03 02:20:34 -05001553static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1554 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001555}
1556
David Benjamin8c880a22016-12-03 02:20:34 -05001557static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1558 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001559 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001560 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001561 return 1;
1562 }
1563
1564 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1565 !CBB_add_u16(out, 0 /* length */)) {
1566 return 0;
1567 }
1568
1569 return 1;
1570}
1571
David Benjamin8c880a22016-12-03 02:20:34 -05001572static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1573 uint8_t *out_alert, CBS *contents) {
1574 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001575 if (contents == NULL) {
1576 return 1;
1577 }
1578
David Benjamince079fd2016-08-02 16:22:34 -04001579 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001580 assert(ssl->tlsext_channel_id_enabled);
1581
1582 if (CBS_len(contents) != 0) {
1583 return 0;
1584 }
1585
1586 ssl->s3->tlsext_channel_id_valid = 1;
1587 return 1;
1588}
1589
David Benjamin8c880a22016-12-03 02:20:34 -05001590static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1591 uint8_t *out_alert, CBS *contents) {
1592 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001593 if (contents == NULL ||
1594 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001595 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001596 return 1;
1597 }
1598
1599 if (CBS_len(contents) != 0) {
1600 return 0;
1601 }
1602
1603 ssl->s3->tlsext_channel_id_valid = 1;
1604 return 1;
1605}
1606
David Benjamin8c880a22016-12-03 02:20:34 -05001607static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1608 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001609 if (!ssl->s3->tlsext_channel_id_valid) {
1610 return 1;
1611 }
1612
1613 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1614 !CBB_add_u16(out, 0 /* length */)) {
1615 return 0;
1616 }
1617
1618 return 1;
1619}
1620
Adam Langley391250d2015-07-15 19:06:07 -07001621
1622/* Secure Real-time Transport Protocol (SRTP) extension.
1623 *
1624 * https://tools.ietf.org/html/rfc5764 */
1625
Adam Langley391250d2015-07-15 19:06:07 -07001626
David Benjamin8c880a22016-12-03 02:20:34 -05001627static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1628 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001629}
1630
David Benjamin8c880a22016-12-03 02:20:34 -05001631static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1632 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001633 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1634 if (profiles == NULL) {
1635 return 1;
1636 }
1637 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1638 if (num_profiles == 0) {
1639 return 1;
1640 }
1641
1642 CBB contents, profile_ids;
1643 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1644 !CBB_add_u16_length_prefixed(out, &contents) ||
1645 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1646 return 0;
1647 }
1648
David Benjamin54091232016-09-05 12:47:25 -04001649 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001650 if (!CBB_add_u16(&profile_ids,
1651 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1652 return 0;
1653 }
1654 }
1655
1656 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1657 !CBB_flush(out)) {
1658 return 0;
1659 }
1660
1661 return 1;
1662}
1663
David Benjamin8c880a22016-12-03 02:20:34 -05001664static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001665 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001666 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001667 if (contents == NULL) {
1668 return 1;
1669 }
1670
1671 /* The extension consists of a u16-prefixed profile ID list containing a
1672 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1673 *
1674 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1675 CBS profile_ids, srtp_mki;
1676 uint16_t profile_id;
1677 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1678 !CBS_get_u16(&profile_ids, &profile_id) ||
1679 CBS_len(&profile_ids) != 0 ||
1680 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1681 CBS_len(contents) != 0) {
1682 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1683 return 0;
1684 }
1685
1686 if (CBS_len(&srtp_mki) != 0) {
1687 /* Must be no MKI, since we never offer one. */
1688 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1689 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1690 return 0;
1691 }
1692
1693 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1694
1695 /* Check to see if the server gave us something we support (and presumably
1696 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001697 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001698 const SRTP_PROTECTION_PROFILE *profile =
1699 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1700
1701 if (profile->id == profile_id) {
1702 ssl->srtp_profile = profile;
1703 return 1;
1704 }
1705 }
1706
1707 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1708 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1709 return 0;
1710}
1711
David Benjamin8c880a22016-12-03 02:20:34 -05001712static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001713 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001714 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001715 if (contents == NULL) {
1716 return 1;
1717 }
1718
1719 CBS profile_ids, srtp_mki;
1720 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1721 CBS_len(&profile_ids) < 2 ||
1722 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1723 CBS_len(contents) != 0) {
1724 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1725 return 0;
1726 }
1727 /* Discard the MKI value for now. */
1728
1729 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1730 SSL_get_srtp_profiles(ssl);
1731
1732 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001733 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001734 const SRTP_PROTECTION_PROFILE *server_profile =
1735 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1736
1737 CBS profile_ids_tmp;
1738 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1739
1740 while (CBS_len(&profile_ids_tmp) > 0) {
1741 uint16_t profile_id;
1742 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1743 return 0;
1744 }
1745
1746 if (server_profile->id == profile_id) {
1747 ssl->srtp_profile = server_profile;
1748 return 1;
1749 }
1750 }
1751 }
1752
1753 return 1;
1754}
1755
David Benjamin8c880a22016-12-03 02:20:34 -05001756static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1757 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001758 if (ssl->srtp_profile == NULL) {
1759 return 1;
1760 }
1761
1762 CBB contents, profile_ids;
1763 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1764 !CBB_add_u16_length_prefixed(out, &contents) ||
1765 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1766 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1767 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1768 !CBB_flush(out)) {
1769 return 0;
1770 }
1771
1772 return 1;
1773}
1774
Adam Langleybdd5d662015-07-20 16:19:08 -07001775
1776/* EC point formats.
1777 *
1778 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1779
David Benjamin8c880a22016-12-03 02:20:34 -05001780static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001781 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001782 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1783 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001784 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1785 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001786 !CBB_flush(out)) {
1787 return 0;
1788 }
1789
1790 return 1;
1791}
1792
David Benjamin8c880a22016-12-03 02:20:34 -05001793static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001794 /* The point format extension is unneccessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -04001795 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin70aba262016-11-01 12:08:15 -04001796 return 1;
1797 }
1798
David Benjamin8c880a22016-12-03 02:20:34 -05001799 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001800}
1801
David Benjamin8c880a22016-12-03 02:20:34 -05001802static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001803 CBS *contents) {
1804 if (contents == NULL) {
1805 return 1;
1806 }
1807
David Benjamin8c880a22016-12-03 02:20:34 -05001808 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001809 return 0;
1810 }
1811
Adam Langleybdd5d662015-07-20 16:19:08 -07001812 CBS ec_point_format_list;
1813 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1814 CBS_len(contents) != 0) {
1815 return 0;
1816 }
1817
David Benjaminfc059942015-07-30 23:01:59 -04001818 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1819 * point format. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05001820 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1821 TLSEXT_ECPOINTFORMAT_uncompressed,
1822 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001823 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001824 return 0;
1825 }
1826
1827 return 1;
1828}
1829
David Benjamin8c880a22016-12-03 02:20:34 -05001830static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001831 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001832 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001833 return 1;
1834 }
1835
David Benjamin8c880a22016-12-03 02:20:34 -05001836 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001837}
1838
David Benjamin8c880a22016-12-03 02:20:34 -05001839static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1840 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001841 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1842 return 1;
1843 }
1844
David Benjamin45738dd2017-02-09 20:01:26 -05001845 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1846 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001847 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001848
1849 if (!using_ecc) {
1850 return 1;
1851 }
1852
David Benjamin8c880a22016-12-03 02:20:34 -05001853 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001854}
1855
Steven Valdeza833c352016-11-01 13:39:36 -04001856
Steven Valdez4aa154e2016-07-29 14:32:55 -04001857/* Pre Shared Key
1858 *
Steven Valdeza833c352016-11-01 13:39:36 -04001859 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001860
David Benjamin8c880a22016-12-03 02:20:34 -05001861static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1862 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001863 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001864 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001865 return 0;
1866 }
1867
Steven Valdez8f36c512017-06-20 10:55:02 -04001868 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001869 return 15 + ssl->session->tlsext_ticklen + binder_len;
1870}
1871
David Benjamin8c880a22016-12-03 02:20:34 -05001872static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1873 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001874 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001875 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001876 return 1;
1877 }
1878
David Benjaminad8f5e12017-02-20 17:00:20 -05001879 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001880 ssl_get_current_time(ssl, &now);
1881 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1882 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1883
1884 /* Fill in a placeholder zero binder of the appropriate length. It will be
1885 * computed and filled in later after length prefixes are computed. */
1886 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez8f36c512017-06-20 10:55:02 -04001887 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001888
1889 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001890 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1891 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001892 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001893 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1894 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001895 ssl->session->tlsext_ticklen) ||
1896 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1897 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1898 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1899 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001900 return 0;
1901 }
1902
David Benjamin8c880a22016-12-03 02:20:34 -05001903 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001904 return CBB_flush(out);
1905}
1906
David Benjamin8baf9632016-11-17 17:11:16 +09001907int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1908 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001909 CBS *contents) {
1910 uint16_t psk_id;
1911 if (!CBS_get_u16(contents, &psk_id) ||
1912 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001913 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001914 *out_alert = SSL_AD_DECODE_ERROR;
1915 return 0;
1916 }
1917
David Benjamineab773a2016-11-09 18:32:35 -05001918 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001919 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001920 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001921 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1922 return 0;
1923 }
1924
1925 return 1;
1926}
1927
David Benjamin35ac5b72017-03-03 15:05:56 -05001928int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001929 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001930 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04001931 /* We only process the first PSK identity since we don't support pure PSK. */
David Benjamin707af292017-03-10 17:47:18 -05001932 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001933 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001934 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001935 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001936 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001937 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001938 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001939 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001940 *out_alert = SSL_AD_DECODE_ERROR;
1941 return 0;
1942 }
1943
Steven Valdeza833c352016-11-01 13:39:36 -04001944 *out_binders = binders;
1945
David Benjaminaedf3032016-12-01 16:47:56 -05001946 /* Check the syntax of the remaining identities, but do not process them. */
1947 size_t num_identities = 1;
1948 while (CBS_len(&identities) != 0) {
1949 CBS unused_ticket;
1950 uint32_t unused_obfuscated_ticket_age;
1951 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1952 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1953 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1954 *out_alert = SSL_AD_DECODE_ERROR;
1955 return 0;
1956 }
1957
1958 num_identities++;
1959 }
1960
1961 /* Check the syntax of the binders. The value will be checked later if
1962 * resuming. */
1963 size_t num_binders = 0;
1964 while (CBS_len(&binders) != 0) {
1965 CBS binder;
1966 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1967 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1968 *out_alert = SSL_AD_DECODE_ERROR;
1969 return 0;
1970 }
1971
1972 num_binders++;
1973 }
1974
1975 if (num_identities != num_binders) {
1976 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1977 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04001978 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04001979 }
1980
David Benjamine7f60a22016-11-16 18:54:25 +09001981 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001982}
1983
David Benjamin8baf9632016-11-17 17:11:16 +09001984int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1985 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001986 return 1;
1987 }
1988
1989 CBB contents;
1990 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1991 !CBB_add_u16_length_prefixed(out, &contents) ||
1992 /* We only consider the first identity for resumption */
1993 !CBB_add_u16(&contents, 0) ||
1994 !CBB_flush(out)) {
1995 return 0;
1996 }
1997
1998 return 1;
1999}
2000
2001
Steven Valdeza833c352016-11-01 13:39:36 -04002002/* Pre-Shared Key Exchange Modes
2003 *
2004 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002005
David Benjamin8c880a22016-12-03 02:20:34 -05002006static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2007 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04002008 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04002009 return 1;
2010 }
2011
2012 CBB contents, ke_modes;
2013 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2014 !CBB_add_u16_length_prefixed(out, &contents) ||
2015 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2016 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2017 return 0;
2018 }
2019
2020 return CBB_flush(out);
2021}
2022
David Benjamin8c880a22016-12-03 02:20:34 -05002023static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002024 uint8_t *out_alert,
2025 CBS *contents) {
2026 if (contents == NULL) {
2027 return 1;
2028 }
2029
Steven Valdeza833c352016-11-01 13:39:36 -04002030 CBS ke_modes;
2031 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2032 CBS_len(&ke_modes) == 0 ||
2033 CBS_len(contents) != 0) {
2034 *out_alert = SSL_AD_DECODE_ERROR;
2035 return 0;
2036 }
2037
2038 /* We only support tickets with PSK_DHE_KE. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05002039 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2040 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002041
2042 return 1;
2043}
2044
2045
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002046/* Early Data Indication
2047 *
2048 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2049
David Benjamin8c880a22016-12-03 02:20:34 -05002050static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002051 SSL *const ssl = hs->ssl;
Steven Valdez2d850622017-01-11 11:34:52 -05002052 if (ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04002053 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION ||
Steven Valdez2d850622017-01-11 11:34:52 -05002054 ssl->session->ticket_max_early_data == 0 ||
2055 hs->received_hello_retry_request ||
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05002056 !ssl->cert->enable_early_data) {
Steven Valdez2d850622017-01-11 11:34:52 -05002057 return 1;
2058 }
2059
2060 hs->early_data_offered = 1;
2061
2062 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2063 !CBB_add_u16(out, 0) ||
2064 !CBB_flush(out)) {
2065 return 0;
2066 }
2067
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002068 return 1;
2069}
2070
Steven Valdez2d850622017-01-11 11:34:52 -05002071static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002072 uint8_t *out_alert, CBS *contents) {
2073 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002074 if (contents == NULL) {
2075 return 1;
2076 }
2077
2078 if (CBS_len(contents) != 0) {
2079 *out_alert = SSL_AD_DECODE_ERROR;
2080 return 0;
2081 }
2082
Steven Valdez2d850622017-01-11 11:34:52 -05002083 if (!ssl->s3->session_reused) {
2084 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2085 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2086 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002087 }
Steven Valdez2d850622017-01-11 11:34:52 -05002088
2089 ssl->early_data_accepted = 1;
2090 return 1;
2091}
2092
2093static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2094 uint8_t *out_alert, CBS *contents) {
2095 SSL *const ssl = hs->ssl;
2096 if (contents == NULL ||
2097 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2098 return 1;
2099 }
2100
2101 if (CBS_len(contents) != 0) {
2102 *out_alert = SSL_AD_DECODE_ERROR;
2103 return 0;
2104 }
2105
2106 hs->early_data_offered = 1;
2107 return 1;
2108}
2109
2110static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2111 if (!hs->ssl->early_data_accepted) {
2112 return 1;
2113 }
2114
2115 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2116 !CBB_add_u16(out, 0) ||
2117 !CBB_flush(out)) {
2118 return 0;
2119 }
2120
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002121 return 1;
2122}
2123
2124
Steven Valdez143e8b32016-07-11 13:19:03 -04002125/* Key Share
2126 *
David Benjamina128a552016-10-13 14:26:33 -04002127 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002128
David Benjamin8c880a22016-12-03 02:20:34 -05002129static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2130 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002131 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002132 return 1;
2133 }
2134
2135 CBB contents, kse_bytes;
2136 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2137 !CBB_add_u16_length_prefixed(out, &contents) ||
2138 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2139 return 0;
2140 }
2141
David Benjamin8c880a22016-12-03 02:20:34 -05002142 uint16_t group_id = hs->retry_group;
2143 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002144 /* We received a HelloRetryRequest without a new curve, so there is no new
2145 * share to append. Leave |ecdh_ctx| as-is. */
2146 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002147 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2148 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002149 return 0;
2150 }
David Benjamin8c880a22016-12-03 02:20:34 -05002151 OPENSSL_free(hs->key_share_bytes);
2152 hs->key_share_bytes = NULL;
2153 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002154 if (group_id == 0) {
2155 return CBB_flush(out);
2156 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002157 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002158 /* Add a fake group. See draft-davidben-tls-grease-01. */
2159 if (ssl->ctx->grease_enabled &&
2160 (!CBB_add_u16(&kse_bytes,
2161 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2162 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2163 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2164 return 0;
2165 }
2166
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002167 /* Predict the most preferred group. */
2168 const uint16_t *groups;
2169 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002170 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002171 if (groups_len == 0) {
2172 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2173 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002174 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002175
2176 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002177 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002178
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002179 CBB key_exchange;
2180 if (!CBB_add_u16(&kse_bytes, group_id) ||
2181 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002182 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2183 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002184 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002185 return 0;
2186 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002187
David Benjamin8c880a22016-12-03 02:20:34 -05002188 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002189 /* Save the contents of the extension to repeat it in the second
2190 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002191 hs->key_share_bytes_len = CBB_len(&kse_bytes);
2192 hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
2193 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002194 return 0;
2195 }
2196 }
2197
Steven Valdez143e8b32016-07-11 13:19:03 -04002198 return CBB_flush(out);
2199}
2200
David Benjamin8baf9632016-11-17 17:11:16 +09002201int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002202 size_t *out_secret_len,
2203 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002204 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002205 uint16_t group_id;
2206 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002207 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2208 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002209 *out_alert = SSL_AD_DECODE_ERROR;
2210 return 0;
2211 }
2212
David Benjamin8baf9632016-11-17 17:11:16 +09002213 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002214 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2215 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2216 return 0;
2217 }
2218
David Benjamin8baf9632016-11-17 17:11:16 +09002219 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2220 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002221 *out_alert = SSL_AD_INTERNAL_ERROR;
2222 return 0;
2223 }
2224
David Benjamin45738dd2017-02-09 20:01:26 -05002225 hs->new_session->group_id = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002226 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002227 return 1;
2228}
2229
David Benjamin8baf9632016-11-17 17:11:16 +09002230int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002231 uint8_t **out_secret,
2232 size_t *out_secret_len,
2233 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002234 uint16_t group_id;
2235 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002236 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002237 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2238 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2239 return 0;
2240 }
2241
2242 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002243 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002244 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002245 return 0;
2246 }
2247
David Benjamin7e1f9842016-09-20 19:24:40 -04002248 /* Find the corresponding key share. */
2249 int found = 0;
2250 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002251 while (CBS_len(&key_shares) > 0) {
2252 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002253 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002254 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002255 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2256 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002257 return 0;
2258 }
2259
David Benjamin7e1f9842016-09-20 19:24:40 -04002260 if (id == group_id) {
2261 if (found) {
2262 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2263 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2264 return 0;
2265 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002266
David Benjamin7e1f9842016-09-20 19:24:40 -04002267 found = 1;
2268 peer_key = peer_key_tmp;
2269 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002270 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002271 }
2272
David Benjamin7e1f9842016-09-20 19:24:40 -04002273 if (!found) {
2274 *out_found = 0;
2275 *out_secret = NULL;
2276 *out_secret_len = 0;
2277 return 1;
2278 }
2279
2280 /* Compute the DH secret. */
2281 uint8_t *secret = NULL;
2282 size_t secret_len;
2283 SSL_ECDH_CTX group;
David Benjamin17cf2cb2016-12-13 01:07:13 -05002284 OPENSSL_memset(&group, 0, sizeof(SSL_ECDH_CTX));
David Benjamin7e1f9842016-09-20 19:24:40 -04002285 CBB public_key;
2286 if (!CBB_init(&public_key, 32) ||
2287 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002288 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
2289 CBS_data(&peer_key), CBS_len(&peer_key)) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002290 !CBB_finish(&public_key, &hs->ecdh_public_key,
2291 &hs->ecdh_public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002292 OPENSSL_free(secret);
2293 SSL_ECDH_CTX_cleanup(&group);
2294 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002295 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002296 return 0;
2297 }
2298
2299 SSL_ECDH_CTX_cleanup(&group);
2300
2301 *out_secret = secret;
2302 *out_secret_len = secret_len;
2303 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002304 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002305}
2306
David Benjamin8baf9632016-11-17 17:11:16 +09002307int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002308 uint16_t group_id;
2309 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002310 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002311 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2312 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2313 !CBB_add_u16(&kse_bytes, group_id) ||
2314 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002315 !CBB_add_bytes(&public_key, hs->ecdh_public_key,
2316 hs->ecdh_public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002317 !CBB_flush(out)) {
2318 return 0;
2319 }
2320
David Benjaminbf833c32017-03-30 15:45:21 -05002321 OPENSSL_free(hs->ecdh_public_key);
2322 hs->ecdh_public_key = NULL;
2323 hs->ecdh_public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002324
David Benjamin45738dd2017-02-09 20:01:26 -05002325 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002326 return 1;
2327}
2328
2329
Steven Valdezfdd10992016-09-15 16:27:05 -04002330/* Supported Versions
2331 *
2332 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2333
David Benjamin8c880a22016-12-03 02:20:34 -05002334static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2335 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002336 if (hs->max_version <= TLS1_2_VERSION) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002337 return 1;
2338 }
2339
2340 CBB contents, versions;
2341 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2342 !CBB_add_u16_length_prefixed(out, &contents) ||
2343 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2344 return 0;
2345 }
2346
David Benjamind9791bf2016-09-27 16:39:52 -04002347 /* Add a fake version. See draft-davidben-tls-grease-01. */
2348 if (ssl->ctx->grease_enabled &&
2349 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2350 return 0;
2351 }
2352
Steven Valdez8f36c512017-06-20 10:55:02 -04002353 if (!ssl_add_supported_versions(hs, &versions) ||
2354 !CBB_flush(out)) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002355 return 0;
2356 }
2357
2358 return 1;
2359}
2360
2361
David Benjamin3baa6e12016-10-07 21:10:38 -04002362/* Cookie
2363 *
2364 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2365
David Benjamin8c880a22016-12-03 02:20:34 -05002366static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2367 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002368 return 1;
2369 }
2370
2371 CBB contents, cookie;
2372 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2373 !CBB_add_u16_length_prefixed(out, &contents) ||
2374 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002375 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002376 !CBB_flush(out)) {
2377 return 0;
2378 }
2379
2380 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002381 OPENSSL_free(hs->cookie);
2382 hs->cookie = NULL;
2383 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002384 return 1;
2385}
2386
2387
Steven Valdezce902a92016-05-17 11:47:53 -04002388/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002389 *
Steven Valdezce902a92016-05-17 11:47:53 -04002390 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002391 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002392
David Benjamin8c880a22016-12-03 02:20:34 -05002393static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2394 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002395 CBB contents, groups_bytes;
2396 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002397 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002398 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002399 return 0;
2400 }
2401
David Benjamin65ac9972016-09-02 21:35:25 -04002402 /* Add a fake group. See draft-davidben-tls-grease-01. */
2403 if (ssl->ctx->grease_enabled &&
2404 !CBB_add_u16(&groups_bytes,
2405 ssl_get_grease_value(ssl, ssl_grease_group))) {
2406 return 0;
2407 }
2408
Steven Valdezce902a92016-05-17 11:47:53 -04002409 const uint16_t *groups;
2410 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002411 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002412
David Benjamin54091232016-09-05 12:47:25 -04002413 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002414 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002415 return 0;
2416 }
2417 }
2418
2419 return CBB_flush(out);
2420}
2421
David Benjamin8c880a22016-12-03 02:20:34 -05002422static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2423 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002424 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002425 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2426 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002427 return 1;
2428}
2429
David Benjamin8c880a22016-12-03 02:20:34 -05002430static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2431 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002432 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002433 if (contents == NULL) {
2434 return 1;
2435 }
2436
Steven Valdezce902a92016-05-17 11:47:53 -04002437 CBS supported_group_list;
2438 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2439 CBS_len(&supported_group_list) == 0 ||
2440 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002441 CBS_len(contents) != 0) {
2442 return 0;
2443 }
2444
David Benjamin8c880a22016-12-03 02:20:34 -05002445 hs->peer_supported_group_list =
2446 OPENSSL_malloc(CBS_len(&supported_group_list));
2447 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002448 *out_alert = SSL_AD_INTERNAL_ERROR;
2449 return 0;
2450 }
2451
Steven Valdezce902a92016-05-17 11:47:53 -04002452 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002453 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002454 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002455 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002456 goto err;
2457 }
2458 }
2459
Steven Valdezce902a92016-05-17 11:47:53 -04002460 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002461 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002462
2463 return 1;
2464
2465err:
David Benjamin8c880a22016-12-03 02:20:34 -05002466 OPENSSL_free(hs->peer_supported_group_list);
2467 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002468 *out_alert = SSL_AD_INTERNAL_ERROR;
2469 return 0;
2470}
2471
David Benjamin8c880a22016-12-03 02:20:34 -05002472static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002473 /* Servers don't echo this extension. */
2474 return 1;
2475}
2476
2477
Adam Langley614c66a2015-06-12 15:26:58 -07002478/* kExtensions contains all the supported extensions. */
2479static const struct tls_extension kExtensions[] = {
2480 {
Adam Langley5021b222015-06-12 18:27:58 -07002481 TLSEXT_TYPE_renegotiate,
2482 NULL,
2483 ext_ri_add_clienthello,
2484 ext_ri_parse_serverhello,
2485 ext_ri_parse_clienthello,
2486 ext_ri_add_serverhello,
2487 },
2488 {
Adam Langley614c66a2015-06-12 15:26:58 -07002489 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002490 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002491 ext_sni_add_clienthello,
2492 ext_sni_parse_serverhello,
2493 ext_sni_parse_clienthello,
2494 ext_sni_add_serverhello,
2495 },
Adam Langley0a056712015-07-01 15:03:33 -07002496 {
2497 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002498 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002499 ext_ems_add_clienthello,
2500 ext_ems_parse_serverhello,
2501 ext_ems_parse_clienthello,
2502 ext_ems_add_serverhello,
2503 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002504 {
2505 TLSEXT_TYPE_session_ticket,
2506 NULL,
2507 ext_ticket_add_clienthello,
2508 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002509 /* Ticket extension client parsing is handled in ssl_session.c */
2510 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002511 ext_ticket_add_serverhello,
2512 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002513 {
2514 TLSEXT_TYPE_signature_algorithms,
2515 NULL,
2516 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002517 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002518 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002519 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002520 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002521 {
2522 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002523 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002524 ext_ocsp_add_clienthello,
2525 ext_ocsp_parse_serverhello,
2526 ext_ocsp_parse_clienthello,
2527 ext_ocsp_add_serverhello,
2528 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002529 {
2530 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002531 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002532 ext_npn_add_clienthello,
2533 ext_npn_parse_serverhello,
2534 ext_npn_parse_clienthello,
2535 ext_npn_add_serverhello,
2536 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002537 {
2538 TLSEXT_TYPE_certificate_timestamp,
2539 NULL,
2540 ext_sct_add_clienthello,
2541 ext_sct_parse_serverhello,
2542 ext_sct_parse_clienthello,
2543 ext_sct_add_serverhello,
2544 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002545 {
2546 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002547 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002548 ext_alpn_add_clienthello,
2549 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002550 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2551 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002552 ext_alpn_add_serverhello,
2553 },
Adam Langley49c7af12015-07-10 14:33:46 -07002554 {
2555 TLSEXT_TYPE_channel_id,
2556 ext_channel_id_init,
2557 ext_channel_id_add_clienthello,
2558 ext_channel_id_parse_serverhello,
2559 ext_channel_id_parse_clienthello,
2560 ext_channel_id_add_serverhello,
2561 },
Adam Langley391250d2015-07-15 19:06:07 -07002562 {
2563 TLSEXT_TYPE_srtp,
2564 ext_srtp_init,
2565 ext_srtp_add_clienthello,
2566 ext_srtp_parse_serverhello,
2567 ext_srtp_parse_clienthello,
2568 ext_srtp_add_serverhello,
2569 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002570 {
2571 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002572 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002573 ext_ec_point_add_clienthello,
2574 ext_ec_point_parse_serverhello,
2575 ext_ec_point_parse_clienthello,
2576 ext_ec_point_add_serverhello,
2577 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002578 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002579 TLSEXT_TYPE_key_share,
2580 NULL,
2581 ext_key_share_add_clienthello,
2582 forbid_parse_serverhello,
2583 ignore_parse_clienthello,
2584 dont_add_serverhello,
2585 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002586 {
Steven Valdeza833c352016-11-01 13:39:36 -04002587 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002588 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002589 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002590 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002591 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002592 dont_add_serverhello,
2593 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002594 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002595 TLSEXT_TYPE_early_data,
2596 NULL,
2597 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002598 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002599 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002600 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002601 },
2602 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002603 TLSEXT_TYPE_supported_versions,
2604 NULL,
2605 ext_supported_versions_add_clienthello,
2606 forbid_parse_serverhello,
2607 ignore_parse_clienthello,
2608 dont_add_serverhello,
2609 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002610 {
2611 TLSEXT_TYPE_cookie,
2612 NULL,
2613 ext_cookie_add_clienthello,
2614 forbid_parse_serverhello,
2615 ignore_parse_clienthello,
2616 dont_add_serverhello,
2617 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002618 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2619 * intolerant to the last extension being zero-length. See
2620 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002621 {
Steven Valdezce902a92016-05-17 11:47:53 -04002622 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002623 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002624 ext_supported_groups_add_clienthello,
2625 ext_supported_groups_parse_serverhello,
2626 ext_supported_groups_parse_clienthello,
2627 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002628 },
Adam Langley614c66a2015-06-12 15:26:58 -07002629};
2630
2631#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2632
Adam Langley4cfa96b2015-07-01 11:56:55 -07002633OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002634 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002635 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002636OPENSSL_COMPILE_ASSERT(
2637 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2638 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002639
Adam Langley614c66a2015-06-12 15:26:58 -07002640static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2641 uint16_t value) {
2642 unsigned i;
2643 for (i = 0; i < kNumExtensions; i++) {
2644 if (kExtensions[i].value == value) {
2645 *out_index = i;
2646 return &kExtensions[i];
2647 }
2648 }
2649
2650 return NULL;
2651}
2652
Adam Langley09505632015-07-30 18:10:13 -07002653int SSL_extension_supported(unsigned extension_value) {
2654 uint32_t index;
2655 return extension_value == TLSEXT_TYPE_padding ||
2656 tls_extension_find(&index, extension_value) != NULL;
2657}
2658
David Benjamin8c880a22016-12-03 02:20:34 -05002659int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2660 SSL *const ssl = hs->ssl;
David Benjaminf04c2e92016-12-06 13:35:25 -05002661 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2662 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002663 !ssl->s3->send_connection_binding) {
2664 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002665 }
Adam Langley95c29f32014-06-20 12:00:00 -07002666
David Benjamine8d53502015-10-10 14:13:23 -04002667 CBB extensions;
2668 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002669 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002670 }
Adam Langley95c29f32014-06-20 12:00:00 -07002671
David Benjamin8c880a22016-12-03 02:20:34 -05002672 hs->extensions.sent = 0;
2673 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002674
David Benjamin54091232016-09-05 12:47:25 -04002675 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002676 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002677 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002678 }
2679 }
Adam Langley95c29f32014-06-20 12:00:00 -07002680
David Benjamin65ac9972016-09-02 21:35:25 -04002681 uint16_t grease_ext1 = 0;
2682 if (ssl->ctx->grease_enabled) {
2683 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2684 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2685 if (!CBB_add_u16(&extensions, grease_ext1) ||
2686 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2687 goto err;
2688 }
2689 }
2690
David Benjamin54091232016-09-05 12:47:25 -04002691 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002692 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002693 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002694 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002695 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002696 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002697 }
Adam Langley95c29f32014-06-20 12:00:00 -07002698
Adam Langley33ad2b52015-07-20 17:43:53 -07002699 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002700 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002701 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002702 }
Adam Langley75712922014-10-10 16:23:43 -07002703
David Benjamin2bd19172016-11-17 16:47:15 +09002704 if (!custom_ext_add_clienthello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002705 goto err;
2706 }
2707
David Benjamin65ac9972016-09-02 21:35:25 -04002708 if (ssl->ctx->grease_enabled) {
2709 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2710 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2711
2712 /* The two fake extensions must not have the same value. GREASE values are
2713 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2714 * one. */
2715 if (grease_ext1 == grease_ext2) {
2716 grease_ext2 ^= 0x1010;
2717 }
2718
2719 if (!CBB_add_u16(&extensions, grease_ext2) ||
2720 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2721 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2722 goto err;
2723 }
2724 }
2725
David Benjamince079fd2016-08-02 16:22:34 -04002726 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002727 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002728 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002729 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002730 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002731 *
2732 * NB: because this code works out the length of all existing extensions
2733 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002734 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002735 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002736 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002737 * Server 7.0 is intolerant to the last extension being zero-length. See
2738 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002739 if (padding_len >= 4 + 1) {
2740 padding_len -= 4;
2741 } else {
2742 padding_len = 1;
2743 }
Adam Langley95c29f32014-06-20 12:00:00 -07002744
Adam Langley33ad2b52015-07-20 17:43:53 -07002745 uint8_t *padding_bytes;
2746 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2747 !CBB_add_u16(&extensions, padding_len) ||
2748 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2749 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002750 }
Adam Langley75712922014-10-10 16:23:43 -07002751
David Benjamin17cf2cb2016-12-13 01:07:13 -05002752 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002753 }
2754 }
Adam Langley75712922014-10-10 16:23:43 -07002755
Steven Valdeza833c352016-11-01 13:39:36 -04002756 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002757 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002758 goto err;
2759 }
2760
David Benjamina01deee2015-12-08 18:56:31 -05002761 /* Discard empty extensions blocks. */
2762 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002763 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002764 }
2765
David Benjamine8d53502015-10-10 14:13:23 -04002766 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002767
2768err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002769 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002770 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002771}
Adam Langley95c29f32014-06-20 12:00:00 -07002772
David Benjamin8c880a22016-12-03 02:20:34 -05002773int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2774 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002775 CBB extensions;
2776 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002777 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002778 }
2779
David Benjamin8c880a22016-12-03 02:20:34 -05002780 for (unsigned i = 0; i < kNumExtensions; i++) {
2781 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002782 /* Don't send extensions that were not received. */
2783 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002784 }
Adam Langley95c29f32014-06-20 12:00:00 -07002785
David Benjamin8c880a22016-12-03 02:20:34 -05002786 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002787 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002788 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002789 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002790 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002791 }
Adam Langley95c29f32014-06-20 12:00:00 -07002792
David Benjamin2bd19172016-11-17 16:47:15 +09002793 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002794 goto err;
2795 }
2796
Steven Valdez143e8b32016-07-11 13:19:03 -04002797 /* Discard empty extensions blocks before TLS 1.3. */
2798 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2799 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002800 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002801 }
2802
David Benjamin56380462015-10-10 14:59:09 -04002803 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002804
2805err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002806 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002807 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002808}
Adam Langley95c29f32014-06-20 12:00:00 -07002809
David Benjamin731058e2016-12-03 23:15:13 -05002810static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2811 const SSL_CLIENT_HELLO *client_hello,
2812 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002813 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002814 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002815 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002816 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002817 }
2818 }
2819
David Benjamin8c880a22016-12-03 02:20:34 -05002820 hs->extensions.received = 0;
2821 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002822
David Benjamine14ff062016-08-09 16:21:24 -04002823 CBS extensions;
2824 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2825 while (CBS_len(&extensions) != 0) {
2826 uint16_t type;
2827 CBS extension;
2828
2829 /* Decode the next extension. */
2830 if (!CBS_get_u16(&extensions, &type) ||
2831 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002832 *out_alert = SSL_AD_DECODE_ERROR;
2833 return 0;
2834 }
Adam Langley95c29f32014-06-20 12:00:00 -07002835
David Benjamine14ff062016-08-09 16:21:24 -04002836 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2837 * ambiguous. Ignore all but the renegotiation_info extension. */
2838 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2839 continue;
2840 }
Adam Langley95c29f32014-06-20 12:00:00 -07002841
David Benjamine14ff062016-08-09 16:21:24 -04002842 unsigned ext_index;
2843 const struct tls_extension *const ext =
2844 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002845
David Benjamine14ff062016-08-09 16:21:24 -04002846 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002847 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002848 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002849 return 0;
2850 }
David Benjamine14ff062016-08-09 16:21:24 -04002851 continue;
2852 }
2853
David Benjamin8c880a22016-12-03 02:20:34 -05002854 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002855 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002856 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002857 *out_alert = alert;
2858 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002859 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002860 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002861 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002862 }
Adam Langley75712922014-10-10 16:23:43 -07002863
David Benjamin1deb41b2016-08-09 19:36:38 -04002864 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002865 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002866 continue;
2867 }
2868
2869 CBS *contents = NULL, fake_contents;
2870 static const uint8_t kFakeRenegotiateExtension[] = {0};
2871 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2872 ssl_client_cipher_list_contains_cipher(client_hello,
2873 SSL3_CK_SCSV & 0xffff)) {
2874 /* The renegotiation SCSV was received so pretend that we received a
2875 * renegotiation extension. */
2876 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2877 sizeof(kFakeRenegotiateExtension));
2878 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002879 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002880 }
2881
2882 /* Extension wasn't observed so call the callback with a NULL
2883 * parameter. */
2884 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002885 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002886 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002887 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002888 *out_alert = alert;
2889 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002890 }
2891 }
2892
Adam Langleyfcf25832014-12-18 17:42:32 -08002893 return 1;
2894}
Adam Langley95c29f32014-06-20 12:00:00 -07002895
David Benjamin731058e2016-12-03 23:15:13 -05002896int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2897 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002898 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002899 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002900 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002901 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002902 return 0;
2903 }
Adam Langley95c29f32014-06-20 12:00:00 -07002904
David Benjamin8c880a22016-12-03 02:20:34 -05002905 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002906 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002907 return 0;
2908 }
Adam Langley95c29f32014-06-20 12:00:00 -07002909
Adam Langleyfcf25832014-12-18 17:42:32 -08002910 return 1;
2911}
Adam Langley95c29f32014-06-20 12:00:00 -07002912
David Benjamin8c880a22016-12-03 02:20:34 -05002913static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2914 int *out_alert) {
2915 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002916 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2917 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2918 return 1;
2919 }
Adam Langley614c66a2015-06-12 15:26:58 -07002920
Steven Valdez143e8b32016-07-11 13:19:03 -04002921 /* Decode the extensions block and check it is valid. */
2922 CBS extensions;
2923 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2924 !tls1_check_duplicate_extensions(&extensions)) {
2925 *out_alert = SSL_AD_DECODE_ERROR;
2926 return 0;
2927 }
2928
2929 uint32_t received = 0;
2930 while (CBS_len(&extensions) != 0) {
2931 uint16_t type;
2932 CBS extension;
2933
2934 /* Decode the next extension. */
2935 if (!CBS_get_u16(&extensions, &type) ||
2936 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002937 *out_alert = SSL_AD_DECODE_ERROR;
2938 return 0;
2939 }
Adam Langley95c29f32014-06-20 12:00:00 -07002940
Steven Valdez143e8b32016-07-11 13:19:03 -04002941 unsigned ext_index;
2942 const struct tls_extension *const ext =
2943 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002944
Steven Valdez143e8b32016-07-11 13:19:03 -04002945 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002946 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002947 return 0;
2948 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002949 continue;
2950 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002951
David Benjamin5db7c9b2017-01-24 16:17:03 -05002952 OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
2953 too_many_bits);
2954
David Benjamin8c880a22016-12-03 02:20:34 -05002955 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002956 type != TLSEXT_TYPE_renegotiate) {
2957 /* If the extension was never sent then it is illegal, except for the
2958 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002959 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2960 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002961 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002962 return 0;
2963 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002964
Steven Valdez143e8b32016-07-11 13:19:03 -04002965 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002966
Steven Valdez143e8b32016-07-11 13:19:03 -04002967 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002968 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002969 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002970 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04002971 *out_alert = alert;
2972 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002973 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002974 }
Adam Langley95c29f32014-06-20 12:00:00 -07002975
David Benjamin54091232016-09-05 12:47:25 -04002976 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002977 if (!(received & (1u << i))) {
2978 /* Extension wasn't observed so call the callback with a NULL
2979 * parameter. */
2980 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002981 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002982 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002983 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002984 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002985 return 0;
2986 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002987 }
2988 }
Adam Langley95c29f32014-06-20 12:00:00 -07002989
Adam Langleyfcf25832014-12-18 17:42:32 -08002990 return 1;
2991}
Adam Langley95c29f32014-06-20 12:00:00 -07002992
David Benjamin8c880a22016-12-03 02:20:34 -05002993static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
2994 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002995 int ret = SSL_TLSEXT_ERR_NOACK;
2996 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002997
David Benjamin78f8aab2016-03-10 16:33:58 -05002998 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002999 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003000 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05003001 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
3002 ret = ssl->session_ctx->tlsext_servername_callback(
3003 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003004 }
Adam Langley95c29f32014-06-20 12:00:00 -07003005
Adam Langleyfcf25832014-12-18 17:42:32 -08003006 switch (ret) {
3007 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003008 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003009 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003010
Adam Langleyfcf25832014-12-18 17:42:32 -08003011 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003012 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003013 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003014
Adam Langleyfcf25832014-12-18 17:42:32 -08003015 default:
3016 return 1;
3017 }
3018}
Adam Langleyed8270a2014-09-02 13:52:56 -07003019
David Benjamin8c880a22016-12-03 02:20:34 -05003020int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3021 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08003022 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003023 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003024 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003025 return 0;
3026 }
3027
Adam Langleyfcf25832014-12-18 17:42:32 -08003028 return 1;
3029}
Adam Langley95c29f32014-06-20 12:00:00 -07003030
Adam Langley4c341d02017-03-08 19:33:21 -08003031static enum ssl_ticket_aead_result_t
3032ssl_decrypt_ticket_with_cipher_ctx(SSL *ssl, uint8_t **out, size_t *out_len,
3033 int *out_renew_ticket, const uint8_t *ticket,
3034 size_t ticket_len) {
3035 enum ssl_ticket_aead_result_t ret = ssl_ticket_aead_ignore_ticket;
3036 const SSL_CTX *const ssl_ctx = ssl->session_ctx;
David Benjamine3aa1d92015-06-16 15:34:50 -04003037 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003038
David Benjamine3aa1d92015-06-16 15:34:50 -04003039 HMAC_CTX hmac_ctx;
3040 HMAC_CTX_init(&hmac_ctx);
3041 EVP_CIPHER_CTX cipher_ctx;
3042 EVP_CIPHER_CTX_init(&cipher_ctx);
3043
David Benjaminadcc3952015-04-26 13:07:57 -04003044 /* Ensure there is room for the key name and the largest IV
3045 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3046 * the maximum IV length should be well under the minimum size for the
3047 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003048 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
Adam Langley4c341d02017-03-08 19:33:21 -08003049 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003050 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003051 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003052
David Benjamine3aa1d92015-06-16 15:34:50 -04003053 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003054 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3055 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3056 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003057 if (cb_ret < 0) {
Adam Langley4c341d02017-03-08 19:33:21 -08003058 ret = ssl_ticket_aead_error;
3059 goto out;
3060 } else if (cb_ret == 0) {
3061 goto out;
3062 } else if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003063 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003064 }
3065 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003066 /* Check the key name matches. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003067 if (OPENSSL_memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3068 SSL_TICKET_KEY_NAME_LEN) != 0) {
Adam Langley4c341d02017-03-08 19:33:21 -08003069 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003070 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003071 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3072 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003073 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003074 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3075 ssl_ctx->tlsext_tick_aes_key, iv)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003076 ret = ssl_ticket_aead_error;
3077 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003078 }
3079 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003080 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003081
David Benjamine3aa1d92015-06-16 15:34:50 -04003082 /* Check the MAC at the end of the ticket. */
3083 uint8_t mac[EVP_MAX_MD_SIZE];
3084 size_t mac_len = HMAC_size(&hmac_ctx);
3085 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003086 /* The ticket must be large enough for key name, IV, data, and MAC. */
Adam Langley4c341d02017-03-08 19:33:21 -08003087 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003088 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003089 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3090 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003091 int mac_ok =
3092 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3093#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3094 mac_ok = 1;
3095#endif
3096 if (!mac_ok) {
Adam Langley4c341d02017-03-08 19:33:21 -08003097 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003098 }
3099
David Benjamine3aa1d92015-06-16 15:34:50 -04003100 /* Decrypt the session data. */
3101 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3102 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3103 mac_len;
3104 plaintext = OPENSSL_malloc(ciphertext_len);
3105 if (plaintext == NULL) {
Adam Langley4c341d02017-03-08 19:33:21 -08003106 ret = ssl_ticket_aead_error;
3107 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003108 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003109 size_t plaintext_len;
3110#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin17cf2cb2016-12-13 01:07:13 -05003111 OPENSSL_memcpy(plaintext, ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003112 plaintext_len = ciphertext_len;
3113#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003114 if (ciphertext_len >= INT_MAX) {
Adam Langley4c341d02017-03-08 19:33:21 -08003115 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003116 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003117 int len1, len2;
3118 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3119 (int)ciphertext_len) ||
3120 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003121 ERR_clear_error();
3122 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003123 }
Adam Langley4c341d02017-03-08 19:33:21 -08003124 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04003125#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003126
Adam Langley4c341d02017-03-08 19:33:21 -08003127 *out = plaintext;
3128 plaintext = NULL;
3129 *out_len = plaintext_len;
3130 ret = ssl_ticket_aead_success;
3131
3132out:
3133 OPENSSL_free(plaintext);
3134 HMAC_CTX_cleanup(&hmac_ctx);
3135 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3136 return ret;
3137}
3138
3139static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
3140 SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
3141 const uint8_t *ticket, size_t ticket_len) {
3142 uint8_t *plaintext = OPENSSL_malloc(ticket_len);
3143 if (plaintext == NULL) {
3144 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3145 return ssl_ticket_aead_error;
3146 }
3147
3148 size_t plaintext_len;
3149 const enum ssl_ticket_aead_result_t result =
3150 ssl->session_ctx->ticket_aead_method->open(
3151 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3152
3153 if (result == ssl_ticket_aead_success) {
3154 *out = plaintext;
3155 plaintext = NULL;
3156 *out_len = plaintext_len;
3157 }
3158
3159 OPENSSL_free(plaintext);
3160 return result;
3161}
3162
3163enum ssl_ticket_aead_result_t ssl_process_ticket(
3164 SSL *ssl, SSL_SESSION **out_session, int *out_renew_ticket,
3165 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3166 size_t session_id_len) {
3167 *out_renew_ticket = 0;
3168 *out_session = NULL;
3169
3170 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3171 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3172 return ssl_ticket_aead_ignore_ticket;
3173 }
3174
3175 uint8_t *plaintext = NULL;
3176 size_t plaintext_len;
3177 enum ssl_ticket_aead_result_t result;
3178 if (ssl->session_ctx->ticket_aead_method != NULL) {
3179 result = ssl_decrypt_ticket_with_method(
3180 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3181 } else {
3182 result = ssl_decrypt_ticket_with_cipher_ctx(
3183 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3184 }
3185
3186 if (result != ssl_ticket_aead_success) {
3187 return result;
3188 }
3189
David Benjamine3aa1d92015-06-16 15:34:50 -04003190 /* Decode the session. */
Adam Langley46db7af2017-02-01 15:49:37 -08003191 SSL_SESSION *session =
3192 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx);
Adam Langley4c341d02017-03-08 19:33:21 -08003193 OPENSSL_free(plaintext);
3194
David Benjamine3aa1d92015-06-16 15:34:50 -04003195 if (session == NULL) {
3196 ERR_clear_error(); /* Don't leave an error on the queue. */
Adam Langley4c341d02017-03-08 19:33:21 -08003197 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003198 }
3199
3200 /* Copy the client's session ID into the new session, to denote the ticket has
3201 * been accepted. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003202 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003203 session->session_id_length = session_id_len;
3204
3205 *out_session = session;
Adam Langley4c341d02017-03-08 19:33:21 -08003206 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003207}
Adam Langley95c29f32014-06-20 12:00:00 -07003208
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003209int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003210 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003211 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003212 return 1;
3213 }
David Benjamincd996942014-07-20 16:23:51 -04003214
David Benjamin0fc37ef2016-08-17 15:29:46 -04003215 OPENSSL_free(hs->peer_sigalgs);
3216 hs->peer_sigalgs = NULL;
3217 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003218
3219 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003220 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003221 return 0;
3222 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003223 num_sigalgs /= 2;
3224
3225 /* supported_signature_algorithms in the certificate request is
3226 * allowed to be empty. */
3227 if (num_sigalgs == 0) {
3228 return 1;
3229 }
3230
Steven Valdez02563852016-06-23 13:33:05 -04003231 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3232 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003233 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3234 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003235 return 0;
3236 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003237 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003238
3239 CBS sigalgs;
3240 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003241 for (size_t i = 0; i < num_sigalgs; i++) {
3242 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003243 return 0;
3244 }
3245 }
Adam Langley95c29f32014-06-20 12:00:00 -07003246
Adam Langleyfcf25832014-12-18 17:42:32 -08003247 return 1;
3248}
David Benjaminec2f27d2014-11-13 19:17:25 -05003249
David Benjamina3651382017-04-20 17:49:36 -04003250int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
3251 switch (EVP_PKEY_id(pkey)) {
3252 case EVP_PKEY_RSA:
3253 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3254 return 1;
3255 case EVP_PKEY_EC:
3256 *out = SSL_SIGN_ECDSA_SHA1;
3257 return 1;
3258 default:
3259 return 0;
3260 }
3261}
3262
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003263int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3264 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003265 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003266
Steven Valdezf0451ca2016-06-29 13:16:27 -04003267 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
David Benjamina3651382017-04-20 17:49:36 -04003268 * handshake. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04003269 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamina3651382017-04-20 17:49:36 -04003270 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey)) {
3271 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3272 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003273 }
David Benjamina3651382017-04-20 17:49:36 -04003274 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003275 }
3276
David Benjamin3ef76972016-10-17 17:59:54 -04003277 const uint16_t *sigalgs = cert->sigalgs;
3278 size_t num_sigalgs = cert->num_sigalgs;
3279 if (sigalgs == NULL) {
3280 sigalgs = kSignSignatureAlgorithms;
3281 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003282 }
3283
David Benjamin0fc37ef2016-08-17 15:29:46 -04003284 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3285 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3286 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003287 /* If the client didn't specify any signature_algorithms extension then
3288 * we can assume that it supports SHA1. See
3289 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3290 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3291 SSL_SIGN_ECDSA_SHA1};
3292 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003293 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003294 }
3295
David Benjamin0fc37ef2016-08-17 15:29:46 -04003296 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003297 uint16_t sigalg = sigalgs[i];
3298 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3299 * negotiated. */
3300 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjamina232a712017-03-30 15:51:53 -05003301 !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003302 continue;
3303 }
3304
David Benjamin0fc37ef2016-08-17 15:29:46 -04003305 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003306 if (sigalg == peer_sigalgs[j]) {
3307 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003308 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003309 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003310 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003311 }
Adam Langley95c29f32014-06-20 12:00:00 -07003312
David Benjaminea9a0d52016-07-08 15:52:59 -07003313 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3314 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003315}
Adam Langley95c29f32014-06-20 12:00:00 -07003316
Steven Valdez908ac192017-01-12 13:17:07 -05003317int tls1_verify_channel_id(SSL_HANDSHAKE *hs) {
3318 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003319 int ret = 0;
3320 uint16_t extension_type;
3321 CBS extension, channel_id;
3322
3323 /* A Channel ID handshake message is structured to contain multiple
3324 * extensions, but the only one that can be present is Channel ID. */
3325 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3326 if (!CBS_get_u16(&channel_id, &extension_type) ||
3327 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3328 CBS_len(&channel_id) != 0 ||
3329 extension_type != TLSEXT_TYPE_channel_id ||
3330 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3331 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3332 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3333 return 0;
3334 }
3335
3336 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3337 if (!p256) {
3338 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3339 return 0;
3340 }
3341
3342 EC_KEY *key = NULL;
3343 EC_POINT *point = NULL;
3344 BIGNUM x, y;
3345 ECDSA_SIG sig;
3346 BN_init(&x);
3347 BN_init(&y);
3348 sig.r = BN_new();
3349 sig.s = BN_new();
3350 if (sig.r == NULL || sig.s == NULL) {
3351 goto err;
3352 }
3353
3354 const uint8_t *p = CBS_data(&extension);
3355 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3356 BN_bin2bn(p + 32, 32, &y) == NULL ||
3357 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3358 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3359 goto err;
3360 }
3361
3362 point = EC_POINT_new(p256);
3363 if (point == NULL ||
3364 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3365 goto err;
3366 }
3367
3368 key = EC_KEY_new();
3369 if (key == NULL ||
3370 !EC_KEY_set_group(key, p256) ||
3371 !EC_KEY_set_public_key(key, point)) {
3372 goto err;
3373 }
3374
3375 uint8_t digest[EVP_MAX_MD_SIZE];
3376 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003377 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003378 goto err;
3379 }
3380
3381 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3382#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3383 sig_ok = 1;
3384#endif
3385 if (!sig_ok) {
3386 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3387 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3388 ssl->s3->tlsext_channel_id_valid = 0;
3389 goto err;
3390 }
3391
David Benjamin17cf2cb2016-12-13 01:07:13 -05003392 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
Nick Harper60a85cb2016-09-23 16:25:11 -07003393 ret = 1;
3394
3395err:
3396 BN_free(&x);
3397 BN_free(&y);
3398 BN_free(sig.r);
3399 BN_free(sig.s);
3400 EC_KEY_free(key);
3401 EC_POINT_free(point);
3402 EC_GROUP_free(p256);
3403 return ret;
3404}
3405
Steven Valdez908ac192017-01-12 13:17:07 -05003406int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3407 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003408 uint8_t digest[EVP_MAX_MD_SIZE];
3409 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003410 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003411 return 0;
3412 }
3413
3414 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3415 if (ec_key == NULL) {
3416 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3417 return 0;
3418 }
3419
3420 int ret = 0;
3421 BIGNUM *x = BN_new();
3422 BIGNUM *y = BN_new();
3423 ECDSA_SIG *sig = NULL;
3424 if (x == NULL || y == NULL ||
3425 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3426 EC_KEY_get0_public_key(ec_key),
3427 x, y, NULL)) {
3428 goto err;
3429 }
3430
3431 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3432 if (sig == NULL) {
3433 goto err;
3434 }
3435
3436 CBB child;
3437 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3438 !CBB_add_u16_length_prefixed(cbb, &child) ||
3439 !BN_bn2cbb_padded(&child, 32, x) ||
3440 !BN_bn2cbb_padded(&child, 32, y) ||
3441 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3442 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3443 !CBB_flush(cbb)) {
3444 goto err;
3445 }
3446
3447 ret = 1;
3448
3449err:
3450 BN_free(x);
3451 BN_free(y);
3452 ECDSA_SIG_free(sig);
3453 return ret;
3454}
3455
Steven Valdez908ac192017-01-12 13:17:07 -05003456int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3457 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003458 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3459 uint8_t *msg;
3460 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003461 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003462 ssl_cert_verify_channel_id)) {
3463 return 0;
3464 }
3465 SHA256(msg, msg_len, out);
3466 *out_len = SHA256_DIGEST_LENGTH;
3467 OPENSSL_free(msg);
3468 return 1;
3469 }
3470
Nick Harper95594012016-10-20 14:07:13 -07003471 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003472
Nick Harper95594012016-10-20 14:07:13 -07003473 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003474 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003475 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003476
Steven Valdez87eab492016-06-27 16:34:59 -04003477 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003478 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003479 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003480 if (ssl->session->original_handshake_hash_len == 0) {
3481 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003482 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003483 }
Nick Harper95594012016-10-20 14:07:13 -07003484 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3485 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003486 }
3487
Steven Valdez908ac192017-01-12 13:17:07 -05003488 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3489 size_t hs_hash_len;
3490 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript, hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003491 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003492 }
Steven Valdez908ac192017-01-12 13:17:07 -05003493 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003494 SHA256_Final(out, &ctx);
3495 *out_len = SHA256_DIGEST_LENGTH;
3496 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003497}
Adam Langley1258b6a2014-06-20 12:00:00 -07003498
3499/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin45738dd2017-02-09 20:01:26 -05003500 * hashes in |hs->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003501 * data. */
Steven Valdez908ac192017-01-12 13:17:07 -05003502int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3503 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003504 /* This function should never be called for a resumed session because the
3505 * handshake hashes that we wish to record are for the original, full
3506 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003507 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003508 return -1;
3509 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003510
Steven Valdez908ac192017-01-12 13:17:07 -05003511 OPENSSL_COMPILE_ASSERT(
David Benjamin45738dd2017-02-09 20:01:26 -05003512 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
Steven Valdez908ac192017-01-12 13:17:07 -05003513 original_handshake_hash_is_too_small);
3514
3515 size_t digest_len;
3516 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript,
David Benjamin45738dd2017-02-09 20:01:26 -05003517 hs->new_session->original_handshake_hash,
Steven Valdez908ac192017-01-12 13:17:07 -05003518 &digest_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003519 return -1;
3520 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003521
Steven Valdez908ac192017-01-12 13:17:07 -05003522 OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE <= 0xff, max_md_size_is_too_large);
David Benjamin45738dd2017-02-09 20:01:26 -05003523 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003524
Adam Langleyfcf25832014-12-18 17:42:32 -08003525 return 1;
3526}
Nick Harper60a85cb2016-09-23 16:25:11 -07003527
3528int ssl_do_channel_id_callback(SSL *ssl) {
3529 if (ssl->tlsext_channel_id_private != NULL ||
3530 ssl->ctx->channel_id_cb == NULL) {
3531 return 1;
3532 }
3533
3534 EVP_PKEY *key = NULL;
3535 ssl->ctx->channel_id_cb(ssl, &key);
3536 if (key == NULL) {
3537 /* The caller should try again later. */
3538 return 1;
3539 }
3540
3541 int ret = SSL_set1_tls_channel_id(ssl, key);
3542 EVP_PKEY_free(key);
3543 return ret;
3544}
Adam Langleycfa08c32016-11-17 13:21:27 -08003545
3546int ssl_is_sct_list_valid(const CBS *contents) {
3547 /* Shallow parse the SCT list for sanity. By the RFC
3548 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3549 * of the SCTs may be empty. */
3550 CBS copy = *contents;
3551 CBS sct_list;
3552 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3553 CBS_len(&copy) != 0 ||
3554 CBS_len(&sct_list) == 0) {
3555 return 0;
3556 }
3557
3558 while (CBS_len(&sct_list) > 0) {
3559 CBS sct;
3560 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3561 CBS_len(&sct) == 0) {
3562 return 0;
3563 }
3564 }
3565
3566 return 1;
3567}