blob: 7a9f83a682e97393d2c46abb1d2d2e18c1d19bec [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin03973092014-06-24 23:27:17 -0400116#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400117#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/evp.h>
120#include <openssl/hmac.h>
121#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400122#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/rand.h>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700124#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400127#include "../crypto/internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800128
129
David Benjamin8c880a22016-12-03 02:20:34 -0500130static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
Adam Langley95c29f32014-06-20 12:00:00 -0700131
Adam Langleyfcf25832014-12-18 17:42:32 -0800132static int compare_uint16_t(const void *p1, const void *p2) {
133 uint16_t u1 = *((const uint16_t *)p1);
134 uint16_t u2 = *((const uint16_t *)p2);
135 if (u1 < u2) {
136 return -1;
137 } else if (u1 > u2) {
138 return 1;
139 } else {
140 return 0;
141 }
142}
David Benjamin35a7a442014-07-05 00:23:20 -0400143
Adam Langleyfcf25832014-12-18 17:42:32 -0800144/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
145 * more than one extension of the same type in a ClientHello or ServerHello.
146 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400147 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800148static int tls1_check_duplicate_extensions(const CBS *cbs) {
149 CBS extensions = *cbs;
150 size_t num_extensions = 0, i = 0;
151 uint16_t *extension_types = NULL;
152 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400153
Adam Langleyfcf25832014-12-18 17:42:32 -0800154 /* First pass: count the extensions. */
155 while (CBS_len(&extensions) > 0) {
156 uint16_t type;
157 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400158
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 if (!CBS_get_u16(&extensions, &type) ||
160 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
161 goto done;
162 }
David Benjamin35a7a442014-07-05 00:23:20 -0400163
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 num_extensions++;
165 }
David Benjamin35a7a442014-07-05 00:23:20 -0400166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 if (num_extensions == 0) {
168 return 1;
169 }
David Benjamin9a373592014-07-25 04:27:53 -0400170
Brian Smith5ba06892016-02-07 09:36:04 -1000171 extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800172 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400173 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800174 goto done;
175 }
David Benjamin35a7a442014-07-05 00:23:20 -0400176
Adam Langleyfcf25832014-12-18 17:42:32 -0800177 /* Second pass: gather the extension types. */
178 extensions = *cbs;
179 for (i = 0; i < num_extensions; i++) {
180 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400181
Adam Langleyfcf25832014-12-18 17:42:32 -0800182 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
183 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
184 /* This should not happen. */
185 goto done;
186 }
187 }
188 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400189
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 /* Sort the extensions and make sure there are no duplicates. */
191 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
192 for (i = 1; i < num_extensions; i++) {
193 if (extension_types[i - 1] == extension_types[i]) {
194 goto done;
195 }
196 }
David Benjamin35a7a442014-07-05 00:23:20 -0400197
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 ret = 1;
199
David Benjamin35a7a442014-07-05 00:23:20 -0400200done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400201 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 return ret;
203}
David Benjamin35a7a442014-07-05 00:23:20 -0400204
David Benjamin731058e2016-12-03 23:15:13 -0500205int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out, const uint8_t *in,
206 size_t in_len) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500207 OPENSSL_memset(out, 0, sizeof(*out));
David Benjamin731058e2016-12-03 23:15:13 -0500208 out->ssl = ssl;
209 out->client_hello = in;
210 out->client_hello_len = in_len;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400211
David Benjamine14ff062016-08-09 16:21:24 -0400212 CBS client_hello, random, session_id;
David Benjamin731058e2016-12-03 23:15:13 -0500213 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
214 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjamine14ff062016-08-09 16:21:24 -0400215 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
216 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
217 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 return 0;
219 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700220
David Benjamin731058e2016-12-03 23:15:13 -0500221 out->random = CBS_data(&random);
222 out->random_len = CBS_len(&random);
223 out->session_id = CBS_data(&session_id);
224 out->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 /* Skip past DTLS cookie */
David Benjamin731058e2016-12-03 23:15:13 -0500227 if (SSL_is_dtls(out->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800228 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400229 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
230 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 return 0;
232 }
233 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700234
David Benjamine14ff062016-08-09 16:21:24 -0400235 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800236 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400237 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
238 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 CBS_len(&compression_methods) < 1) {
240 return 0;
241 }
David Benjamine14ff062016-08-09 16:21:24 -0400242
David Benjamin731058e2016-12-03 23:15:13 -0500243 out->cipher_suites = CBS_data(&cipher_suites);
244 out->cipher_suites_len = CBS_len(&cipher_suites);
245 out->compression_methods = CBS_data(&compression_methods);
246 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 /* If the ClientHello ends here then it's valid, but doesn't have any
249 * extensions. (E.g. SSLv3.) */
250 if (CBS_len(&client_hello) == 0) {
David Benjamin731058e2016-12-03 23:15:13 -0500251 out->extensions = NULL;
252 out->extensions_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800253 return 1;
254 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700255
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 /* Extract extensions and check it is valid. */
David Benjamine14ff062016-08-09 16:21:24 -0400257 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
259 !tls1_check_duplicate_extensions(&extensions) ||
260 CBS_len(&client_hello) != 0) {
261 return 0;
262 }
David Benjamine14ff062016-08-09 16:21:24 -0400263
David Benjamin731058e2016-12-03 23:15:13 -0500264 out->extensions = CBS_data(&extensions);
265 out->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700266
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 return 1;
268}
Adam Langleydc9b1412014-06-20 12:00:00 -0700269
David Benjamin731058e2016-12-03 23:15:13 -0500270int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
271 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 CBS extensions;
David Benjamin731058e2016-12-03 23:15:13 -0500273 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 while (CBS_len(&extensions) != 0) {
David Benjamincec73442016-08-02 17:41:33 -0400275 /* Decode the next extension. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 uint16_t type;
277 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 if (!CBS_get_u16(&extensions, &type) ||
279 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
280 return 0;
281 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700282
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400284 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 return 1;
286 }
287 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700288
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 return 0;
290}
Adam Langley95c29f32014-06-20 12:00:00 -0700291
David Benjamin731058e2016-12-03 23:15:13 -0500292int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
293 uint16_t extension_type,
294 const uint8_t **out_data,
295 size_t *out_len) {
David Benjamincec73442016-08-02 17:41:33 -0400296 CBS cbs;
David Benjamin731058e2016-12-03 23:15:13 -0500297 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
David Benjamincec73442016-08-02 17:41:33 -0400298 return 0;
299 }
300
301 *out_data = CBS_data(&cbs);
302 *out_len = CBS_len(&cbs);
303 return 1;
304}
305
Steven Valdezce902a92016-05-17 11:47:53 -0400306static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400307 SSL_CURVE_X25519,
308 SSL_CURVE_SECP256R1,
309 SSL_CURVE_SECP384R1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800310};
Adam Langley95c29f32014-06-20 12:00:00 -0700311
David Benjaminf04976b2016-10-07 00:37:55 -0400312void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
Steven Valdez5440fe02016-07-18 12:40:30 -0400313 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400314 *out_group_ids = ssl->supported_group_list;
315 *out_group_ids_len = ssl->supported_group_list_len;
316 if (!*out_group_ids) {
317 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400318 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 }
320}
David Benjamined439582014-07-14 19:13:02 -0400321
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900322int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
323 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400324 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400325
David Benjaminf04976b2016-10-07 00:37:55 -0400326 const uint16_t *groups, *pref, *supp;
327 size_t groups_len, pref_len, supp_len;
328 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800329
David Benjaminf04976b2016-10-07 00:37:55 -0400330 /* Clients are not required to send a supported_groups extension. In this
331 * case, the server is free to pick any group it likes. See RFC 4492,
332 * section 4, paragraph 3.
333 *
334 * However, in the interests of compatibility, we will skip ECDH if the
335 * client didn't send an extension because we can't be sure that they'll
336 * support our favoured group. Thus we do not special-case an emtpy
337 * |peer_supported_group_list|. */
David Benjamin55a43642015-04-20 14:45:55 -0400338
David Benjamin4298d772015-12-19 00:18:25 -0500339 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400340 pref = groups;
341 pref_len = groups_len;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900342 supp = hs->peer_supported_group_list;
343 supp_len = hs->peer_supported_group_list_len;
David Benjamin55a43642015-04-20 14:45:55 -0400344 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900345 pref = hs->peer_supported_group_list;
346 pref_len = hs->peer_supported_group_list_len;
Steven Valdezce902a92016-05-17 11:47:53 -0400347 supp = groups;
348 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400349 }
350
David Benjaminf04976b2016-10-07 00:37:55 -0400351 for (size_t i = 0; i < pref_len; i++) {
352 for (size_t j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800353 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400354 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500355 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800356 }
357 }
358 }
359
David Benjamin4298d772015-12-19 00:18:25 -0500360 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800361}
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Steven Valdezce902a92016-05-17 11:47:53 -0400363int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400365 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800366
Steven Valdezce902a92016-05-17 11:47:53 -0400367 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
368 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 return 0;
370 }
371
David Benjamin54091232016-09-05 12:47:25 -0400372 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400373 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
374 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 return 0;
376 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 }
378
Steven Valdezce902a92016-05-17 11:47:53 -0400379 OPENSSL_free(*out_group_ids);
380 *out_group_ids = group_ids;
381 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800382
383 return 1;
384}
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100386int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
387 const char *curves) {
388 uint16_t *group_ids = NULL;
389 size_t ncurves = 0;
390
391 const char *col;
392 const char *ptr = curves;
393
394 do {
395 col = strchr(ptr, ':');
396
397 uint16_t group_id;
398 if (!ssl_name_to_group_id(&group_id, ptr,
399 col ? (size_t)(col - ptr) : strlen(ptr))) {
400 goto err;
401 }
402
403 uint16_t *new_group_ids = OPENSSL_realloc(group_ids,
404 (ncurves + 1) * sizeof(uint16_t));
405 if (new_group_ids == NULL) {
406 goto err;
407 }
408 group_ids = new_group_ids;
409
410 group_ids[ncurves] = group_id;
411 ncurves++;
412
413 if (col) {
414 ptr = col + 1;
415 }
416 } while (col);
417
418 OPENSSL_free(*out_group_ids);
419 *out_group_ids = group_ids;
420 *out_group_ids_len = ncurves;
421
422 return 1;
423
424err:
425 OPENSSL_free(group_ids);
426 return 0;
427}
428
Steven Valdezce902a92016-05-17 11:47:53 -0400429int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
430 const uint16_t *groups;
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400431 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -0400432 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400433 for (size_t i = 0; i < groups_len; i++) {
434 if (groups[i] == group_id) {
435 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800436 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 }
David Benjamin033e5f42014-11-13 18:47:41 -0500438
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400439 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800440}
David Benjamin033e5f42014-11-13 18:47:41 -0500441
David Benjamin3ef76972016-10-17 17:59:54 -0400442/* kVerifySignatureAlgorithms is the default list of accepted signature
David Benjamin3a322f52016-10-26 12:45:35 -0400443 * algorithms for verifying.
444 *
445 * For now, RSA-PSS signature algorithms are not enabled on Android's system
446 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
447 * restore them. */
David Benjamin3ef76972016-10-17 17:59:54 -0400448static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjamin3a322f52016-10-26 12:45:35 -0400449 /* Prefer SHA-256 algorithms. */
450 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400451#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400452 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400453#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400454 SSL_SIGN_RSA_PKCS1_SHA256,
455
456 /* Larger hashes are acceptable. */
457 SSL_SIGN_ECDSA_SECP384R1_SHA384,
458#if !defined(BORINGSSL_ANDROID_SYSTEM)
459 SSL_SIGN_RSA_PSS_SHA384,
460#endif
461 SSL_SIGN_RSA_PKCS1_SHA384,
462
David Benjamin76bb1412016-09-08 16:03:49 -0400463 /* TODO(davidben): Remove this. */
David Benjamin3ef76972016-10-17 17:59:54 -0400464#if defined(BORINGSSL_ANDROID_SYSTEM)
465 SSL_SIGN_ECDSA_SECP521R1_SHA512,
466#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400467#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400468 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400469#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400470 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400471
David Benjamin3a322f52016-10-26 12:45:35 -0400472 /* For now, SHA-1 is still accepted but least preferable. */
473 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400474
David Benjamin3a322f52016-10-26 12:45:35 -0400475};
476
477/* kSignSignatureAlgorithms is the default list of supported signature
478 * algorithms for signing.
479 *
480 * For now, RSA-PSS signature algorithms are not enabled on Android's system
481 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
482 * restore them. */
483static const uint16_t kSignSignatureAlgorithms[] = {
484 /* Prefer SHA-256 algorithms. */
485 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400486#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400487 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400488#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400489 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400490
David Benjamin3a322f52016-10-26 12:45:35 -0400491 /* If needed, sign larger hashes.
492 *
493 * TODO(davidben): Determine which of these may be pruned. */
494 SSL_SIGN_ECDSA_SECP384R1_SHA384,
495#if !defined(BORINGSSL_ANDROID_SYSTEM)
496 SSL_SIGN_RSA_PSS_SHA384,
497#endif
498 SSL_SIGN_RSA_PKCS1_SHA384,
499
500 SSL_SIGN_ECDSA_SECP521R1_SHA512,
501#if !defined(BORINGSSL_ANDROID_SYSTEM)
502 SSL_SIGN_RSA_PSS_SHA512,
503#endif
504 SSL_SIGN_RSA_PKCS1_SHA512,
505
506 /* If the peer supports nothing else, sign with SHA-1. */
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400507 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400508 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400509};
510
David Benjamin3ef76972016-10-17 17:59:54 -0400511size_t tls12_get_verify_sigalgs(const SSL *ssl, const uint16_t **out) {
512 *out = kVerifySignatureAlgorithms;
513 return OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
Adam Langleyfcf25832014-12-18 17:42:32 -0800514}
Adam Langley95c29f32014-06-20 12:00:00 -0700515
David Benjamin887c3002016-07-08 16:15:32 -0700516int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
David Benjamin3ef76972016-10-17 17:59:54 -0400517 const uint16_t *verify_sigalgs;
518 size_t num_verify_sigalgs = tls12_get_verify_sigalgs(ssl, &verify_sigalgs);
519 for (size_t i = 0; i < num_verify_sigalgs; i++) {
520 if (sigalg == verify_sigalgs[i]) {
521 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 }
523 }
524
David Benjamin3ef76972016-10-17 17:59:54 -0400525 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
526 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
527 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800528}
529
Adam Langley614c66a2015-06-12 15:26:58 -0700530/* tls_extension represents a TLS extension that is handled internally. The
531 * |init| function is called for each handshake, before any other functions of
532 * the extension. Then the add and parse callbacks are called as needed.
533 *
534 * The parse callbacks receive a |CBS| that contains the contents of the
535 * extension (i.e. not including the type and length bytes). If an extension is
536 * not received then the parse callbacks will be called with a NULL CBS so that
537 * they can do any processing needed to handle the absence of an extension.
538 *
539 * The add callbacks receive a |CBB| to which the extension can be appended but
540 * the function is responsible for appending the type and length bytes too.
541 *
542 * All callbacks return one for success and zero for error. If a parse function
543 * returns zero then a fatal alert with value |*out_alert| will be sent. If
544 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
545struct tls_extension {
546 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500547 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700548
David Benjamin8c880a22016-12-03 02:20:34 -0500549 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
550 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
551 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700552
David Benjamin8c880a22016-12-03 02:20:34 -0500553 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
554 CBS *contents);
555 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700556};
557
David Benjamin8c880a22016-12-03 02:20:34 -0500558static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
559 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400560 if (contents != NULL) {
561 /* Servers MUST NOT send this extension. */
562 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
563 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
564 return 0;
565 }
566
567 return 1;
568}
569
David Benjamin8c880a22016-12-03 02:20:34 -0500570static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
571 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400572 /* This extension from the client is handled elsewhere. */
573 return 1;
574}
575
David Benjamin8c880a22016-12-03 02:20:34 -0500576static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400577 return 1;
578}
Adam Langley614c66a2015-06-12 15:26:58 -0700579
580/* Server name indication (SNI).
581 *
582 * https://tools.ietf.org/html/rfc6066#section-3. */
583
David Benjamin8c880a22016-12-03 02:20:34 -0500584static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
585 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700586 if (ssl->tlsext_hostname == NULL) {
587 return 1;
588 }
589
590 CBB contents, server_name_list, name;
591 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
592 !CBB_add_u16_length_prefixed(out, &contents) ||
593 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
594 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
595 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
596 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
597 strlen(ssl->tlsext_hostname)) ||
598 !CBB_flush(out)) {
599 return 0;
600 }
601
602 return 1;
603}
604
David Benjamin8c880a22016-12-03 02:20:34 -0500605static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500606 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500607 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700608 if (contents == NULL) {
609 return 1;
610 }
611
612 if (CBS_len(contents) != 0) {
613 return 0;
614 }
615
616 assert(ssl->tlsext_hostname != NULL);
617
Steven Valdez87eab492016-06-27 16:34:59 -0400618 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500619 OPENSSL_free(hs->new_session->tlsext_hostname);
620 hs->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
621 if (!hs->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700622 *out_alert = SSL_AD_INTERNAL_ERROR;
623 return 0;
624 }
625 }
626
627 return 1;
628}
629
David Benjamin8c880a22016-12-03 02:20:34 -0500630static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500631 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700632 if (contents == NULL) {
633 return 1;
634 }
635
David Benjamin9b611e22016-03-03 08:48:30 -0500636 CBS server_name_list, host_name;
637 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700638 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500639 !CBS_get_u8(&server_name_list, &name_type) ||
640 /* Although the server_name extension was intended to be extensible to
641 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
642 * different name types will cause an error. Further, RFC 4366 originally
643 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
644 * adding new name types is no longer feasible.
645 *
646 * Act as if the extensibility does not exist to simplify parsing. */
647 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
648 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700649 CBS_len(contents) != 0) {
650 return 0;
651 }
652
David Benjamin9b611e22016-03-03 08:48:30 -0500653 if (name_type != TLSEXT_NAMETYPE_host_name ||
654 CBS_len(&host_name) == 0 ||
655 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
656 CBS_contains_zero_byte(&host_name)) {
657 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
658 return 0;
659 }
Adam Langley614c66a2015-06-12 15:26:58 -0700660
David Benjamin4eb95cc2016-11-16 17:08:23 +0900661 /* Copy the hostname as a string. */
David Benjamin8c880a22016-12-03 02:20:34 -0500662 if (!CBS_strdup(&host_name, &hs->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900663 *out_alert = SSL_AD_INTERNAL_ERROR;
664 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700665 }
666
David Benjamin8c880a22016-12-03 02:20:34 -0500667 hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700668 return 1;
669}
670
David Benjamin8c880a22016-12-03 02:20:34 -0500671static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
672 if (hs->ssl->s3->session_reused ||
673 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700674 return 1;
675 }
676
677 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
678 !CBB_add_u16(out, 0 /* length */)) {
679 return 0;
680 }
681
682 return 1;
683}
684
685
Adam Langley5021b222015-06-12 18:27:58 -0700686/* Renegotiation indication.
687 *
688 * https://tools.ietf.org/html/rfc5746 */
689
David Benjamin8c880a22016-12-03 02:20:34 -0500690static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
691 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400692 uint16_t min_version, max_version;
693 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
694 return 0;
695 }
696
697 /* Renegotiation indication is not necessary in TLS 1.3. */
698 if (min_version >= TLS1_3_VERSION) {
699 return 1;
700 }
701
David Benjamin52bf6902016-10-08 12:05:03 -0400702 assert(ssl->s3->initial_handshake_complete ==
703 (ssl->s3->previous_client_finished_len != 0));
704
Adam Langley5021b222015-06-12 18:27:58 -0700705 CBB contents, prev_finished;
706 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
707 !CBB_add_u16_length_prefixed(out, &contents) ||
708 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
709 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
710 ssl->s3->previous_client_finished_len) ||
711 !CBB_flush(out)) {
712 return 0;
713 }
714
715 return 1;
716}
717
David Benjamin8c880a22016-12-03 02:20:34 -0500718static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700719 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500720 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400721 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
722 return 0;
723 }
724
David Benjamin3e052de2015-11-25 20:10:31 -0500725 /* Servers may not switch between omitting the extension and supporting it.
726 * See RFC 5746, sections 3.5 and 4.2. */
727 if (ssl->s3->initial_handshake_complete &&
728 (contents != NULL) != ssl->s3->send_connection_binding) {
729 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
730 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
731 return 0;
732 }
733
Adam Langley5021b222015-06-12 18:27:58 -0700734 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500735 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700736 * RI even on initial ServerHello because the client doesn't see any
737 * renegotiation during an attack. However this would mean we could not
738 * connect to any server which doesn't support RI.
739 *
David Benjamine9cddb82015-11-23 14:36:40 -0500740 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
741 * practical terms every client sets it so it's just assumed here. */
742 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700743 }
744
745 const size_t expected_len = ssl->s3->previous_client_finished_len +
746 ssl->s3->previous_server_finished_len;
747
748 /* Check for logic errors */
749 assert(!expected_len || ssl->s3->previous_client_finished_len);
750 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400751 assert(ssl->s3->initial_handshake_complete ==
752 (ssl->s3->previous_client_finished_len != 0));
753 assert(ssl->s3->initial_handshake_complete ==
754 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700755
756 /* Parse out the extension contents. */
757 CBS renegotiated_connection;
758 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
759 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400760 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700761 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
762 return 0;
763 }
764
765 /* Check that the extension matches. */
766 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400767 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700768 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
769 return 0;
770 }
771
772 const uint8_t *d = CBS_data(&renegotiated_connection);
773 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
774 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400775 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700776 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
777 return 0;
778 }
779 d += ssl->s3->previous_client_finished_len;
780
781 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
782 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400783 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700784 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
785 return 0;
786 }
787 ssl->s3->send_connection_binding = 1;
788
789 return 1;
790}
791
David Benjamin8c880a22016-12-03 02:20:34 -0500792static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700793 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500794 SSL *const ssl = hs->ssl;
Adam Langley5021b222015-06-12 18:27:58 -0700795 /* Renegotiation isn't supported as a server so this function should never be
796 * called after the initial handshake. */
797 assert(!ssl->s3->initial_handshake_complete);
798
Steven Valdez143e8b32016-07-11 13:19:03 -0400799 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
800 return 1;
801 }
802
Adam Langley5021b222015-06-12 18:27:58 -0700803 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400804 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700805 }
806
807 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700808 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
809 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400810 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700811 return 0;
812 }
813
David Benjamin52bf6902016-10-08 12:05:03 -0400814 /* Check that the extension matches. We do not support renegotiation as a
815 * server, so this must be empty. */
816 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400817 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700818 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
819 return 0;
820 }
821
822 ssl->s3->send_connection_binding = 1;
823
824 return 1;
825}
826
David Benjamin8c880a22016-12-03 02:20:34 -0500827static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
828 SSL *const ssl = hs->ssl;
David Benjamin52bf6902016-10-08 12:05:03 -0400829 /* Renegotiation isn't supported as a server so this function should never be
830 * called after the initial handshake. */
831 assert(!ssl->s3->initial_handshake_complete);
832
Steven Valdez143e8b32016-07-11 13:19:03 -0400833 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
834 return 1;
835 }
836
Adam Langley5021b222015-06-12 18:27:58 -0700837 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400838 !CBB_add_u16(out, 1 /* length */) ||
839 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700840 return 0;
841 }
842
843 return 1;
844}
845
Adam Langley0a056712015-07-01 15:03:33 -0700846
847/* Extended Master Secret.
848 *
David Benjamin43946d42016-02-01 08:42:19 -0500849 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700850
David Benjamin8c880a22016-12-03 02:20:34 -0500851static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400852 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -0500853 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400854 return 0;
855 }
856
857 /* Extended master secret is not necessary in TLS 1.3. */
858 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700859 return 1;
860 }
861
862 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
863 !CBB_add_u16(out, 0 /* length */)) {
864 return 0;
865 }
866
867 return 1;
868}
869
David Benjamin8c880a22016-12-03 02:20:34 -0500870static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700871 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500872 SSL *const ssl = hs->ssl;
David Benjamin163c9562016-08-29 23:14:17 -0400873 /* Whether EMS is negotiated may not change on renegotation. */
874 if (ssl->s3->initial_handshake_complete) {
875 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
876 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
877 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
878 return 0;
879 }
880
881 return 1;
882 }
883
Adam Langley0a056712015-07-01 15:03:33 -0700884 if (contents == NULL) {
885 return 1;
886 }
887
Steven Valdez143e8b32016-07-11 13:19:03 -0400888 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
889 ssl->version == SSL3_VERSION) {
890 return 0;
891 }
892
893 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700894 return 0;
895 }
896
897 ssl->s3->tmp.extended_master_secret = 1;
898 return 1;
899}
900
David Benjamin8c880a22016-12-03 02:20:34 -0500901static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500902 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500903 SSL *const ssl = hs->ssl;
904 uint16_t version = ssl3_protocol_version(ssl);
905 if (version >= TLS1_3_VERSION ||
906 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400907 return 1;
908 }
909
910 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700911 return 1;
912 }
913
914 if (CBS_len(contents) != 0) {
915 return 0;
916 }
917
918 ssl->s3->tmp.extended_master_secret = 1;
919 return 1;
920}
921
David Benjamin8c880a22016-12-03 02:20:34 -0500922static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
923 if (!hs->ssl->s3->tmp.extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700924 return 1;
925 }
926
927 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
928 !CBB_add_u16(out, 0 /* length */)) {
929 return 0;
930 }
931
932 return 1;
933}
934
Adam Langley9b05bc52015-07-01 15:25:33 -0700935
936/* Session tickets.
937 *
938 * https://tools.ietf.org/html/rfc5077 */
939
David Benjamin8c880a22016-12-03 02:20:34 -0500940static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
941 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400942 uint16_t min_version, max_version;
943 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
944 return 0;
945 }
946
947 /* TLS 1.3 uses a different ticket extension. */
948 if (min_version >= TLS1_3_VERSION ||
949 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700950 return 1;
951 }
952
953 const uint8_t *ticket_data = NULL;
954 int ticket_len = 0;
955
956 /* Renegotiation does not participate in session resumption. However, still
957 * advertise the extension to avoid potentially breaking servers which carry
958 * over the state from the previous handshake, such as OpenSSL servers
959 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -0400960 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -0700961 if (!ssl->s3->initial_handshake_complete &&
962 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400963 ssl->session->tlsext_tick != NULL &&
964 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -0400965 ssl->method->version_from_wire(&session_version,
966 ssl->session->ssl_version) &&
967 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700968 ticket_data = ssl->session->tlsext_tick;
969 ticket_len = ssl->session->tlsext_ticklen;
970 }
971
972 CBB ticket;
973 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
974 !CBB_add_u16_length_prefixed(out, &ticket) ||
975 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
976 !CBB_flush(out)) {
977 return 0;
978 }
979
980 return 1;
981}
982
David Benjamin8c880a22016-12-03 02:20:34 -0500983static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -0700984 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500985 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -0700986 if (contents == NULL) {
987 return 1;
988 }
989
Steven Valdez143e8b32016-07-11 13:19:03 -0400990 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
991 return 0;
992 }
993
Adam Langley9b05bc52015-07-01 15:25:33 -0700994 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
995 * this function should never be called, even if the server tries to send the
996 * extension. */
997 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
998
999 if (CBS_len(contents) != 0) {
1000 return 0;
1001 }
1002
David Benjamin8c880a22016-12-03 02:20:34 -05001003 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001004 return 1;
1005}
1006
David Benjamin8c880a22016-12-03 02:20:34 -05001007static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1008 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001009 return 1;
1010 }
1011
David Benjamin78476f62016-11-12 11:20:55 +09001012 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin8c880a22016-12-03 02:20:34 -05001013 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001014
1015 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1016 !CBB_add_u16(out, 0 /* length */)) {
1017 return 0;
1018 }
1019
1020 return 1;
1021}
1022
1023
Adam Langley2e857bd2015-07-01 16:09:19 -07001024/* Signature Algorithms.
1025 *
1026 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1027
David Benjamin8c880a22016-12-03 02:20:34 -05001028static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1029 SSL *const ssl = hs->ssl;
David Benjamin2dc02042016-09-19 19:57:37 -04001030 uint16_t min_version, max_version;
1031 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1032 return 0;
1033 }
1034
1035 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001036 return 1;
1037 }
1038
David Benjamin0fc37ef2016-08-17 15:29:46 -04001039 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001040 const size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001041
David Benjamin0fc37ef2016-08-17 15:29:46 -04001042 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001043 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1044 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001045 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001046 return 0;
1047 }
1048
David Benjamin0fc37ef2016-08-17 15:29:46 -04001049 for (size_t i = 0; i < num_sigalgs; i++) {
1050 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001051 return 0;
1052 }
1053 }
1054
1055 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001056 return 0;
1057 }
1058
1059 return 1;
1060}
1061
David Benjamin8c880a22016-12-03 02:20:34 -05001062static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001063 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001064 OPENSSL_free(hs->peer_sigalgs);
1065 hs->peer_sigalgs = NULL;
1066 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001067
Adam Langley2e857bd2015-07-01 16:09:19 -07001068 if (contents == NULL) {
1069 return 1;
1070 }
1071
1072 CBS supported_signature_algorithms;
1073 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001074 CBS_len(contents) != 0 ||
1075 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001076 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001077 return 0;
1078 }
1079
1080 return 1;
1081}
1082
Adam Langley2e857bd2015-07-01 16:09:19 -07001083
Adam Langleybb0bd042015-07-01 16:21:03 -07001084/* OCSP Stapling.
1085 *
1086 * https://tools.ietf.org/html/rfc6066#section-8 */
1087
David Benjamin8c880a22016-12-03 02:20:34 -05001088static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1089 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001090 if (!ssl->ocsp_stapling_enabled) {
1091 return 1;
1092 }
1093
1094 CBB contents;
1095 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1096 !CBB_add_u16_length_prefixed(out, &contents) ||
1097 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1098 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1099 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1100 !CBB_flush(out)) {
1101 return 0;
1102 }
1103
1104 return 1;
1105}
1106
David Benjamin8c880a22016-12-03 02:20:34 -05001107static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001108 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001109 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001110 if (contents == NULL) {
1111 return 1;
1112 }
1113
Steven Valdeza833c352016-11-01 13:39:36 -04001114 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1115 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001116 return 0;
1117 }
1118
Steven Valdeza833c352016-11-01 13:39:36 -04001119 /* OCSP stapling is forbidden on non-certificate ciphers. */
1120 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001121 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001122 return 0;
1123 }
1124
Steven Valdeza833c352016-11-01 13:39:36 -04001125 /* Note this does not check for resumption in TLS 1.2. Sending
1126 * status_request here does not make sense, but OpenSSL does so and the
1127 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001128
David Benjamin8c880a22016-12-03 02:20:34 -05001129 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001130 return 1;
1131}
1132
David Benjamin8c880a22016-12-03 02:20:34 -05001133static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001134 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001135 if (contents == NULL) {
1136 return 1;
1137 }
1138
1139 uint8_t status_type;
1140 if (!CBS_get_u8(contents, &status_type)) {
1141 return 0;
1142 }
1143
1144 /* We cannot decide whether OCSP stapling will occur yet because the correct
1145 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001146 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001147
Adam Langleybb0bd042015-07-01 16:21:03 -07001148 return 1;
1149}
1150
David Benjamin8c880a22016-12-03 02:20:34 -05001151static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1152 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001153 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001154 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001155 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001156 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001157 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001158 return 1;
1159 }
1160
David Benjamin8c880a22016-12-03 02:20:34 -05001161 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001162
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001163 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001164 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001165}
1166
1167
Adam Langley97dfcbf2015-07-01 18:35:20 -07001168/* Next protocol negotiation.
1169 *
1170 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1171
David Benjamin8c880a22016-12-03 02:20:34 -05001172static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1173 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001174 if (ssl->s3->initial_handshake_complete ||
1175 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001176 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001177 return 1;
1178 }
1179
1180 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1181 !CBB_add_u16(out, 0 /* length */)) {
1182 return 0;
1183 }
1184
1185 return 1;
1186}
1187
David Benjamin8c880a22016-12-03 02:20:34 -05001188static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001189 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001190 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001191 if (contents == NULL) {
1192 return 1;
1193 }
1194
Steven Valdez143e8b32016-07-11 13:19:03 -04001195 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1196 return 0;
1197 }
1198
Adam Langley97dfcbf2015-07-01 18:35:20 -07001199 /* If any of these are false then we should never have sent the NPN
1200 * extension in the ClientHello and thus this function should never have been
1201 * called. */
1202 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001203 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001204 assert(ssl->ctx->next_proto_select_cb != NULL);
1205
David Benjamin76c2efc2015-08-31 14:24:29 -04001206 if (ssl->s3->alpn_selected != NULL) {
1207 /* NPN and ALPN may not be negotiated in the same connection. */
1208 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1209 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1210 return 0;
1211 }
1212
Adam Langley97dfcbf2015-07-01 18:35:20 -07001213 const uint8_t *const orig_contents = CBS_data(contents);
1214 const size_t orig_len = CBS_len(contents);
1215
1216 while (CBS_len(contents) != 0) {
1217 CBS proto;
1218 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1219 CBS_len(&proto) == 0) {
1220 return 0;
1221 }
1222 }
1223
1224 uint8_t *selected;
1225 uint8_t selected_len;
1226 if (ssl->ctx->next_proto_select_cb(
1227 ssl, &selected, &selected_len, orig_contents, orig_len,
1228 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1229 *out_alert = SSL_AD_INTERNAL_ERROR;
1230 return 0;
1231 }
1232
David Benjamin79978df2015-12-25 15:56:49 -05001233 OPENSSL_free(ssl->s3->next_proto_negotiated);
1234 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1235 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001236 *out_alert = SSL_AD_INTERNAL_ERROR;
1237 return 0;
1238 }
1239
David Benjamin79978df2015-12-25 15:56:49 -05001240 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001241 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001242
1243 return 1;
1244}
1245
David Benjamin8c880a22016-12-03 02:20:34 -05001246static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001247 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001248 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001249 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1250 return 1;
1251 }
1252
Adam Langley97dfcbf2015-07-01 18:35:20 -07001253 if (contents != NULL && CBS_len(contents) != 0) {
1254 return 0;
1255 }
1256
1257 if (contents == NULL ||
1258 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001259 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001260 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001261 return 1;
1262 }
1263
David Benjamin8c880a22016-12-03 02:20:34 -05001264 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001265 return 1;
1266}
1267
David Benjamin8c880a22016-12-03 02:20:34 -05001268static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1269 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001270 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1271 * parsed. */
David Benjamin8c880a22016-12-03 02:20:34 -05001272 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001273 return 1;
1274 }
1275
1276 const uint8_t *npa;
1277 unsigned npa_len;
1278
1279 if (ssl->ctx->next_protos_advertised_cb(
1280 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1281 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001282 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001283 return 1;
1284 }
1285
1286 CBB contents;
1287 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1288 !CBB_add_u16_length_prefixed(out, &contents) ||
1289 !CBB_add_bytes(&contents, npa, npa_len) ||
1290 !CBB_flush(out)) {
1291 return 0;
1292 }
1293
1294 return 1;
1295}
1296
1297
Adam Langleyab8d87d2015-07-10 12:21:39 -07001298/* Signed certificate timestamps.
1299 *
1300 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1301
David Benjamin8c880a22016-12-03 02:20:34 -05001302static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1303 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001304 if (!ssl->signed_cert_timestamps_enabled) {
1305 return 1;
1306 }
1307
1308 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1309 !CBB_add_u16(out, 0 /* length */)) {
1310 return 0;
1311 }
1312
1313 return 1;
1314}
1315
David Benjamin8c880a22016-12-03 02:20:34 -05001316static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001317 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001318 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001319 if (contents == NULL) {
1320 return 1;
1321 }
1322
Steven Valdeza833c352016-11-01 13:39:36 -04001323 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1324 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001325 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001326 return 0;
1327 }
1328
Adam Langleyab8d87d2015-07-10 12:21:39 -07001329 /* If this is false then we should never have sent the SCT extension in the
1330 * ClientHello and thus this function should never have been called. */
1331 assert(ssl->signed_cert_timestamps_enabled);
1332
Adam Langleycfa08c32016-11-17 13:21:27 -08001333 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001334 *out_alert = SSL_AD_DECODE_ERROR;
1335 return 0;
1336 }
1337
David Benjamindaa88502016-10-04 16:32:16 -04001338 /* Session resumption uses the original session information. The extension
1339 * should not be sent on resumption, but RFC 6962 did not make it a
1340 * requirement, so tolerate this.
1341 *
1342 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001343 if (!ssl->s3->session_reused &&
David Benjamin45738dd2017-02-09 20:01:26 -05001344 !CBS_stow(contents, &hs->new_session->tlsext_signed_cert_timestamp_list,
1345 &hs->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001346 *out_alert = SSL_AD_INTERNAL_ERROR;
1347 return 0;
1348 }
1349
1350 return 1;
1351}
1352
David Benjamin8c880a22016-12-03 02:20:34 -05001353static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001354 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001355 if (contents == NULL) {
1356 return 1;
1357 }
1358
1359 if (CBS_len(contents) != 0) {
1360 return 0;
1361 }
1362
David Benjamin8c880a22016-12-03 02:20:34 -05001363 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001364 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001365}
1366
David Benjamin8c880a22016-12-03 02:20:34 -05001367static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1368 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001369 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001370 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1371 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001372 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001373 return 1;
1374 }
1375
1376 CBB contents;
1377 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1378 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001379 CBB_add_bytes(
1380 &contents,
1381 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1382 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001383 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001384}
1385
1386
Adam Langleyf18e4532015-07-10 13:39:53 -07001387/* Application-level Protocol Negotiation.
1388 *
1389 * https://tools.ietf.org/html/rfc7301 */
1390
David Benjamin8c880a22016-12-03 02:20:34 -05001391static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1392 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001393 if (ssl->alpn_client_proto_list == NULL ||
1394 ssl->s3->initial_handshake_complete) {
1395 return 1;
1396 }
1397
1398 CBB contents, proto_list;
1399 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1400 !CBB_add_u16_length_prefixed(out, &contents) ||
1401 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1402 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1403 ssl->alpn_client_proto_list_len) ||
1404 !CBB_flush(out)) {
1405 return 0;
1406 }
1407
1408 return 1;
1409}
1410
David Benjamin8c880a22016-12-03 02:20:34 -05001411static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001412 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001413 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001414 if (contents == NULL) {
1415 return 1;
1416 }
1417
1418 assert(!ssl->s3->initial_handshake_complete);
1419 assert(ssl->alpn_client_proto_list != NULL);
1420
David Benjamin8c880a22016-12-03 02:20:34 -05001421 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001422 /* NPN and ALPN may not be negotiated in the same connection. */
1423 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1424 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1425 return 0;
1426 }
1427
Adam Langleyf18e4532015-07-10 13:39:53 -07001428 /* The extension data consists of a ProtocolNameList which must have
1429 * exactly one ProtocolName. Each of these is length-prefixed. */
1430 CBS protocol_name_list, protocol_name;
1431 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1432 CBS_len(contents) != 0 ||
1433 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1434 /* Empty protocol names are forbidden. */
1435 CBS_len(&protocol_name) == 0 ||
1436 CBS_len(&protocol_name_list) != 0) {
1437 return 0;
1438 }
1439
David Benjamin3e517572016-08-11 11:52:23 -04001440 /* Check that the protcol name is one of the ones we advertised. */
1441 int protocol_ok = 0;
1442 CBS client_protocol_name_list, client_protocol_name;
1443 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1444 ssl->alpn_client_proto_list_len);
1445 while (CBS_len(&client_protocol_name_list) > 0) {
1446 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1447 &client_protocol_name)) {
1448 *out_alert = SSL_AD_INTERNAL_ERROR;
1449 return 0;
1450 }
1451
1452 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
David Benjamin17cf2cb2016-12-13 01:07:13 -05001453 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1454 CBS_data(&protocol_name),
1455 CBS_len(&protocol_name)) == 0) {
David Benjamin3e517572016-08-11 11:52:23 -04001456 protocol_ok = 1;
1457 break;
1458 }
1459 }
1460
1461 if (!protocol_ok) {
1462 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1463 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1464 return 0;
1465 }
1466
Adam Langleyf18e4532015-07-10 13:39:53 -07001467 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1468 &ssl->s3->alpn_selected_len)) {
1469 *out_alert = SSL_AD_INTERNAL_ERROR;
1470 return 0;
1471 }
1472
1473 return 1;
1474}
1475
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001476int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001477 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001478 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001479 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001480 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001481 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001482 client_hello, &contents,
1483 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1484 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001485 return 1;
1486 }
1487
1488 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001489 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001490
1491 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001492 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1493 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001494 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001495 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1496 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001497 return 0;
1498 }
1499
1500 /* Validate the protocol list. */
1501 CBS protocol_name_list_copy = protocol_name_list;
1502 while (CBS_len(&protocol_name_list_copy) > 0) {
1503 CBS protocol_name;
1504
1505 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1506 /* Empty protocol names are forbidden. */
1507 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001508 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1509 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001510 return 0;
1511 }
1512 }
1513
1514 const uint8_t *selected;
1515 uint8_t selected_len;
1516 if (ssl->ctx->alpn_select_cb(
1517 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1518 CBS_len(&protocol_name_list),
1519 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1520 OPENSSL_free(ssl->s3->alpn_selected);
1521 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1522 if (ssl->s3->alpn_selected == NULL) {
1523 *out_alert = SSL_AD_INTERNAL_ERROR;
1524 return 0;
1525 }
1526 ssl->s3->alpn_selected_len = selected_len;
1527 }
1528
1529 return 1;
1530}
1531
David Benjamin8c880a22016-12-03 02:20:34 -05001532static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1533 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001534 if (ssl->s3->alpn_selected == NULL) {
1535 return 1;
1536 }
1537
1538 CBB contents, proto_list, proto;
1539 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1540 !CBB_add_u16_length_prefixed(out, &contents) ||
1541 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1542 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001543 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1544 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001545 !CBB_flush(out)) {
1546 return 0;
1547 }
1548
1549 return 1;
1550}
1551
1552
Adam Langley49c7af12015-07-10 14:33:46 -07001553/* Channel ID.
1554 *
1555 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1556
David Benjamin8c880a22016-12-03 02:20:34 -05001557static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1558 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001559}
1560
David Benjamin8c880a22016-12-03 02:20:34 -05001561static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1562 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001563 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001564 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001565 return 1;
1566 }
1567
1568 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1569 !CBB_add_u16(out, 0 /* length */)) {
1570 return 0;
1571 }
1572
1573 return 1;
1574}
1575
David Benjamin8c880a22016-12-03 02:20:34 -05001576static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1577 uint8_t *out_alert, CBS *contents) {
1578 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001579 if (contents == NULL) {
1580 return 1;
1581 }
1582
David Benjamince079fd2016-08-02 16:22:34 -04001583 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001584 assert(ssl->tlsext_channel_id_enabled);
1585
1586 if (CBS_len(contents) != 0) {
1587 return 0;
1588 }
1589
1590 ssl->s3->tlsext_channel_id_valid = 1;
1591 return 1;
1592}
1593
David Benjamin8c880a22016-12-03 02:20:34 -05001594static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1595 uint8_t *out_alert, CBS *contents) {
1596 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001597 if (contents == NULL ||
1598 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001599 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001600 return 1;
1601 }
1602
1603 if (CBS_len(contents) != 0) {
1604 return 0;
1605 }
1606
1607 ssl->s3->tlsext_channel_id_valid = 1;
1608 return 1;
1609}
1610
David Benjamin8c880a22016-12-03 02:20:34 -05001611static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1612 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001613 if (!ssl->s3->tlsext_channel_id_valid) {
1614 return 1;
1615 }
1616
1617 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1618 !CBB_add_u16(out, 0 /* length */)) {
1619 return 0;
1620 }
1621
1622 return 1;
1623}
1624
Adam Langley391250d2015-07-15 19:06:07 -07001625
1626/* Secure Real-time Transport Protocol (SRTP) extension.
1627 *
1628 * https://tools.ietf.org/html/rfc5764 */
1629
Adam Langley391250d2015-07-15 19:06:07 -07001630
David Benjamin8c880a22016-12-03 02:20:34 -05001631static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1632 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001633}
1634
David Benjamin8c880a22016-12-03 02:20:34 -05001635static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1636 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001637 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1638 if (profiles == NULL) {
1639 return 1;
1640 }
1641 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1642 if (num_profiles == 0) {
1643 return 1;
1644 }
1645
1646 CBB contents, profile_ids;
1647 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1648 !CBB_add_u16_length_prefixed(out, &contents) ||
1649 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1650 return 0;
1651 }
1652
David Benjamin54091232016-09-05 12:47:25 -04001653 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001654 if (!CBB_add_u16(&profile_ids,
1655 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1656 return 0;
1657 }
1658 }
1659
1660 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1661 !CBB_flush(out)) {
1662 return 0;
1663 }
1664
1665 return 1;
1666}
1667
David Benjamin8c880a22016-12-03 02:20:34 -05001668static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001669 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001670 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001671 if (contents == NULL) {
1672 return 1;
1673 }
1674
1675 /* The extension consists of a u16-prefixed profile ID list containing a
1676 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1677 *
1678 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1679 CBS profile_ids, srtp_mki;
1680 uint16_t profile_id;
1681 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1682 !CBS_get_u16(&profile_ids, &profile_id) ||
1683 CBS_len(&profile_ids) != 0 ||
1684 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1685 CBS_len(contents) != 0) {
1686 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1687 return 0;
1688 }
1689
1690 if (CBS_len(&srtp_mki) != 0) {
1691 /* Must be no MKI, since we never offer one. */
1692 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1693 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1694 return 0;
1695 }
1696
1697 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1698
1699 /* Check to see if the server gave us something we support (and presumably
1700 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001701 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001702 const SRTP_PROTECTION_PROFILE *profile =
1703 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1704
1705 if (profile->id == profile_id) {
1706 ssl->srtp_profile = profile;
1707 return 1;
1708 }
1709 }
1710
1711 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1712 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1713 return 0;
1714}
1715
David Benjamin8c880a22016-12-03 02:20:34 -05001716static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001717 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001718 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001719 if (contents == NULL) {
1720 return 1;
1721 }
1722
1723 CBS profile_ids, srtp_mki;
1724 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1725 CBS_len(&profile_ids) < 2 ||
1726 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1727 CBS_len(contents) != 0) {
1728 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1729 return 0;
1730 }
1731 /* Discard the MKI value for now. */
1732
1733 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1734 SSL_get_srtp_profiles(ssl);
1735
1736 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001737 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001738 const SRTP_PROTECTION_PROFILE *server_profile =
1739 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1740
1741 CBS profile_ids_tmp;
1742 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1743
1744 while (CBS_len(&profile_ids_tmp) > 0) {
1745 uint16_t profile_id;
1746 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1747 return 0;
1748 }
1749
1750 if (server_profile->id == profile_id) {
1751 ssl->srtp_profile = server_profile;
1752 return 1;
1753 }
1754 }
1755 }
1756
1757 return 1;
1758}
1759
David Benjamin8c880a22016-12-03 02:20:34 -05001760static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1761 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001762 if (ssl->srtp_profile == NULL) {
1763 return 1;
1764 }
1765
1766 CBB contents, profile_ids;
1767 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1768 !CBB_add_u16_length_prefixed(out, &contents) ||
1769 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1770 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1771 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1772 !CBB_flush(out)) {
1773 return 0;
1774 }
1775
1776 return 1;
1777}
1778
Adam Langleybdd5d662015-07-20 16:19:08 -07001779
1780/* EC point formats.
1781 *
1782 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1783
David Benjamin8c880a22016-12-03 02:20:34 -05001784static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001785 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001786 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1787 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001788 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1789 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001790 !CBB_flush(out)) {
1791 return 0;
1792 }
1793
1794 return 1;
1795}
1796
David Benjamin8c880a22016-12-03 02:20:34 -05001797static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001798 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -05001799 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin70aba262016-11-01 12:08:15 -04001800 return 0;
1801 }
1802
1803 /* The point format extension is unneccessary in TLS 1.3. */
1804 if (min_version >= TLS1_3_VERSION) {
1805 return 1;
1806 }
1807
David Benjamin8c880a22016-12-03 02:20:34 -05001808 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001809}
1810
David Benjamin8c880a22016-12-03 02:20:34 -05001811static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001812 CBS *contents) {
1813 if (contents == NULL) {
1814 return 1;
1815 }
1816
David Benjamin8c880a22016-12-03 02:20:34 -05001817 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001818 return 0;
1819 }
1820
Adam Langleybdd5d662015-07-20 16:19:08 -07001821 CBS ec_point_format_list;
1822 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1823 CBS_len(contents) != 0) {
1824 return 0;
1825 }
1826
David Benjaminfc059942015-07-30 23:01:59 -04001827 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1828 * point format. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05001829 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1830 TLSEXT_ECPOINTFORMAT_uncompressed,
1831 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001832 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001833 return 0;
1834 }
1835
1836 return 1;
1837}
1838
David Benjamin8c880a22016-12-03 02:20:34 -05001839static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001840 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001841 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001842 return 1;
1843 }
1844
David Benjamin8c880a22016-12-03 02:20:34 -05001845 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001846}
1847
David Benjamin8c880a22016-12-03 02:20:34 -05001848static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1849 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001850 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1851 return 1;
1852 }
1853
David Benjamin45738dd2017-02-09 20:01:26 -05001854 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1855 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001856 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001857
1858 if (!using_ecc) {
1859 return 1;
1860 }
1861
David Benjamin8c880a22016-12-03 02:20:34 -05001862 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001863}
1864
Steven Valdeza833c352016-11-01 13:39:36 -04001865
Steven Valdez4aa154e2016-07-29 14:32:55 -04001866/* Pre Shared Key
1867 *
Steven Valdeza833c352016-11-01 13:39:36 -04001868 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001869
David Benjamin8c880a22016-12-03 02:20:34 -05001870static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1871 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001872 uint16_t min_version, max_version;
1873 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1874 return 0;
1875 }
1876
1877 uint16_t session_version;
1878 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1879 !ssl->method->version_from_wire(&session_version,
1880 ssl->session->ssl_version) ||
1881 session_version < TLS1_3_VERSION) {
1882 return 0;
1883 }
1884
Steven Valdez908ac192017-01-12 13:17:07 -05001885 const EVP_MD *digest = SSL_SESSION_get_digest(ssl->session, ssl);
1886 if (digest == NULL) {
1887 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1888 return 0;
1889 }
1890
Steven Valdeza833c352016-11-01 13:39:36 -04001891 size_t binder_len = EVP_MD_size(digest);
1892 return 15 + ssl->session->tlsext_ticklen + binder_len;
1893}
1894
David Benjamin8c880a22016-12-03 02:20:34 -05001895static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1896 SSL *const ssl = hs->ssl;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001897 uint16_t min_version, max_version;
1898 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1899 return 0;
1900 }
1901
David Benjamin2dc02042016-09-19 19:57:37 -04001902 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001903 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001904 !ssl->method->version_from_wire(&session_version,
1905 ssl->session->ssl_version) ||
1906 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001907 return 1;
1908 }
1909
Steven Valdeza833c352016-11-01 13:39:36 -04001910 struct timeval now;
1911 ssl_get_current_time(ssl, &now);
1912 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1913 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1914
1915 /* Fill in a placeholder zero binder of the appropriate length. It will be
1916 * computed and filled in later after length prefixes are computed. */
1917 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez908ac192017-01-12 13:17:07 -05001918
1919 const EVP_MD *digest = SSL_SESSION_get_digest(ssl->session, ssl);
1920 if (digest == NULL) {
1921 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1922 return 0;
1923 }
1924
Steven Valdeza833c352016-11-01 13:39:36 -04001925 size_t binder_len = EVP_MD_size(digest);
1926
1927 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001928 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1929 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001930 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001931 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1932 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001933 ssl->session->tlsext_ticklen) ||
1934 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1935 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1936 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1937 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001938 return 0;
1939 }
1940
David Benjamin8c880a22016-12-03 02:20:34 -05001941 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001942 return CBB_flush(out);
1943}
1944
David Benjamin8baf9632016-11-17 17:11:16 +09001945int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1946 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001947 CBS *contents) {
1948 uint16_t psk_id;
1949 if (!CBS_get_u16(contents, &psk_id) ||
1950 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001951 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001952 *out_alert = SSL_AD_DECODE_ERROR;
1953 return 0;
1954 }
1955
David Benjamineab773a2016-11-09 18:32:35 -05001956 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001957 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001958 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001959 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1960 return 0;
1961 }
1962
1963 return 1;
1964}
1965
David Benjamin8baf9632016-11-17 17:11:16 +09001966int ssl_ext_pre_shared_key_parse_clienthello(SSL_HANDSHAKE *hs,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001967 SSL_SESSION **out_session,
Steven Valdeza833c352016-11-01 13:39:36 -04001968 CBS *out_binders,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001969 uint8_t *out_alert,
1970 CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09001971 SSL *const ssl = hs->ssl;
Steven Valdez5b986082016-09-01 12:29:49 -04001972 /* We only process the first PSK identity since we don't support pure PSK. */
Steven Valdeza833c352016-11-01 13:39:36 -04001973 uint32_t obfuscated_ticket_age;
David Benjaminaedf3032016-12-01 16:47:56 -05001974 CBS identities, ticket, binders;
1975 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
1976 !CBS_get_u16_length_prefixed(&identities, &ticket) ||
1977 !CBS_get_u32(&identities, &obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001978 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001979 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001980 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001981 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001982 *out_alert = SSL_AD_DECODE_ERROR;
1983 return 0;
1984 }
1985
Steven Valdeza833c352016-11-01 13:39:36 -04001986 *out_binders = binders;
1987
David Benjaminaedf3032016-12-01 16:47:56 -05001988 /* Check the syntax of the remaining identities, but do not process them. */
1989 size_t num_identities = 1;
1990 while (CBS_len(&identities) != 0) {
1991 CBS unused_ticket;
1992 uint32_t unused_obfuscated_ticket_age;
1993 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1994 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1995 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1996 *out_alert = SSL_AD_DECODE_ERROR;
1997 return 0;
1998 }
1999
2000 num_identities++;
2001 }
2002
2003 /* Check the syntax of the binders. The value will be checked later if
2004 * resuming. */
2005 size_t num_binders = 0;
2006 while (CBS_len(&binders) != 0) {
2007 CBS binder;
2008 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
2009 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2010 *out_alert = SSL_AD_DECODE_ERROR;
2011 return 0;
2012 }
2013
2014 num_binders++;
2015 }
2016
2017 if (num_identities != num_binders) {
2018 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
2019 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04002020 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04002021 }
2022
Steven Valdeza833c352016-11-01 13:39:36 -04002023 /* TODO(svaldez): Check that the ticket_age is valid when attempting to use
2024 * the PSK for 0-RTT. http://crbug.com/boringssl/113 */
2025
Steven Valdez4aa154e2016-07-29 14:32:55 -04002026 /* TLS 1.3 session tickets are renewed separately as part of the
2027 * NewSessionTicket. */
David Benjamine7f60a22016-11-16 18:54:25 +09002028 int unused_renew;
2029 if (!tls_process_ticket(ssl, out_session, &unused_renew, CBS_data(&ticket),
2030 CBS_len(&ticket), NULL, 0)) {
2031 *out_alert = SSL_AD_INTERNAL_ERROR;
2032 return 0;
2033 }
2034
2035 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002036}
2037
David Benjamin8baf9632016-11-17 17:11:16 +09002038int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2039 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002040 return 1;
2041 }
2042
2043 CBB contents;
2044 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2045 !CBB_add_u16_length_prefixed(out, &contents) ||
2046 /* We only consider the first identity for resumption */
2047 !CBB_add_u16(&contents, 0) ||
2048 !CBB_flush(out)) {
2049 return 0;
2050 }
2051
2052 return 1;
2053}
2054
2055
Steven Valdeza833c352016-11-01 13:39:36 -04002056/* Pre-Shared Key Exchange Modes
2057 *
2058 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002059
David Benjamin8c880a22016-12-03 02:20:34 -05002060static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2061 CBB *out) {
2062 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04002063 uint16_t min_version, max_version;
2064 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2065 return 0;
2066 }
2067
2068 if (max_version < TLS1_3_VERSION) {
2069 return 1;
2070 }
2071
2072 CBB contents, ke_modes;
2073 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2074 !CBB_add_u16_length_prefixed(out, &contents) ||
2075 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2076 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2077 return 0;
2078 }
2079
2080 return CBB_flush(out);
2081}
2082
David Benjamin8c880a22016-12-03 02:20:34 -05002083static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002084 uint8_t *out_alert,
2085 CBS *contents) {
2086 if (contents == NULL) {
2087 return 1;
2088 }
2089
Steven Valdeza833c352016-11-01 13:39:36 -04002090 CBS ke_modes;
2091 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2092 CBS_len(&ke_modes) == 0 ||
2093 CBS_len(contents) != 0) {
2094 *out_alert = SSL_AD_DECODE_ERROR;
2095 return 0;
2096 }
2097
2098 /* We only support tickets with PSK_DHE_KE. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05002099 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2100 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002101
2102 return 1;
2103}
2104
2105
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002106/* Early Data Indication
2107 *
2108 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2109
David Benjamin8c880a22016-12-03 02:20:34 -05002110static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002111 /* TODO(svaldez): Support 0RTT. */
2112 return 1;
2113}
2114
David Benjamin8c880a22016-12-03 02:20:34 -05002115static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2116 uint8_t *out_alert, CBS *contents) {
2117 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002118 if (contents == NULL) {
2119 return 1;
2120 }
2121
2122 if (CBS_len(contents) != 0) {
2123 *out_alert = SSL_AD_DECODE_ERROR;
2124 return 0;
2125 }
2126
2127 /* Since we don't currently accept 0-RTT, we have to skip past any early data
2128 * the client might have sent. */
2129 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
2130 ssl->s3->skip_early_data = 1;
2131 }
2132 return 1;
2133}
2134
2135
Steven Valdez143e8b32016-07-11 13:19:03 -04002136/* Key Share
2137 *
David Benjamina128a552016-10-13 14:26:33 -04002138 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002139
David Benjamin8c880a22016-12-03 02:20:34 -05002140static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2141 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002142 uint16_t min_version, max_version;
2143 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2144 return 0;
2145 }
2146
David Benjamin53a2dfc2016-10-07 21:04:12 -04002147 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002148 return 1;
2149 }
2150
2151 CBB contents, kse_bytes;
2152 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2153 !CBB_add_u16_length_prefixed(out, &contents) ||
2154 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2155 return 0;
2156 }
2157
David Benjamin8c880a22016-12-03 02:20:34 -05002158 uint16_t group_id = hs->retry_group;
2159 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002160 /* We received a HelloRetryRequest without a new curve, so there is no new
2161 * share to append. Leave |ecdh_ctx| as-is. */
2162 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002163 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2164 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002165 return 0;
2166 }
David Benjamin8c880a22016-12-03 02:20:34 -05002167 OPENSSL_free(hs->key_share_bytes);
2168 hs->key_share_bytes = NULL;
2169 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002170 if (group_id == 0) {
2171 return CBB_flush(out);
2172 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002173 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002174 /* Add a fake group. See draft-davidben-tls-grease-01. */
2175 if (ssl->ctx->grease_enabled &&
2176 (!CBB_add_u16(&kse_bytes,
2177 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2178 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2179 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2180 return 0;
2181 }
2182
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002183 /* Predict the most preferred group. */
2184 const uint16_t *groups;
2185 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002186 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002187 if (groups_len == 0) {
2188 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2189 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002190 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002191
2192 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002193 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002194
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002195 CBB key_exchange;
2196 if (!CBB_add_u16(&kse_bytes, group_id) ||
2197 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002198 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2199 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002200 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002201 return 0;
2202 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002203
David Benjamin8c880a22016-12-03 02:20:34 -05002204 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002205 /* Save the contents of the extension to repeat it in the second
2206 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002207 hs->key_share_bytes_len = CBB_len(&kse_bytes);
2208 hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
2209 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002210 return 0;
2211 }
2212 }
2213
Steven Valdez143e8b32016-07-11 13:19:03 -04002214 return CBB_flush(out);
2215}
2216
David Benjamin8baf9632016-11-17 17:11:16 +09002217int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002218 size_t *out_secret_len,
2219 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002220 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002221 uint16_t group_id;
2222 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002223 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2224 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002225 *out_alert = SSL_AD_DECODE_ERROR;
2226 return 0;
2227 }
2228
David Benjamin8baf9632016-11-17 17:11:16 +09002229 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002230 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2231 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2232 return 0;
2233 }
2234
David Benjamin8baf9632016-11-17 17:11:16 +09002235 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2236 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002237 *out_alert = SSL_AD_INTERNAL_ERROR;
2238 return 0;
2239 }
2240
David Benjamin45738dd2017-02-09 20:01:26 -05002241 hs->new_session->group_id = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002242 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002243 return 1;
2244}
2245
David Benjamin8baf9632016-11-17 17:11:16 +09002246int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002247 uint8_t **out_secret,
2248 size_t *out_secret_len,
2249 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002250 uint16_t group_id;
2251 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002252 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002253 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2254 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2255 return 0;
2256 }
2257
2258 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002259 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002260 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002261 return 0;
2262 }
2263
David Benjamin7e1f9842016-09-20 19:24:40 -04002264 /* Find the corresponding key share. */
2265 int found = 0;
2266 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002267 while (CBS_len(&key_shares) > 0) {
2268 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002269 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002270 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002271 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2272 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002273 return 0;
2274 }
2275
David Benjamin7e1f9842016-09-20 19:24:40 -04002276 if (id == group_id) {
2277 if (found) {
2278 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2279 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2280 return 0;
2281 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002282
David Benjamin7e1f9842016-09-20 19:24:40 -04002283 found = 1;
2284 peer_key = peer_key_tmp;
2285 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002286 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002287 }
2288
David Benjamin7e1f9842016-09-20 19:24:40 -04002289 if (!found) {
2290 *out_found = 0;
2291 *out_secret = NULL;
2292 *out_secret_len = 0;
2293 return 1;
2294 }
2295
2296 /* Compute the DH secret. */
2297 uint8_t *secret = NULL;
2298 size_t secret_len;
2299 SSL_ECDH_CTX group;
David Benjamin17cf2cb2016-12-13 01:07:13 -05002300 OPENSSL_memset(&group, 0, sizeof(SSL_ECDH_CTX));
David Benjamin7e1f9842016-09-20 19:24:40 -04002301 CBB public_key;
2302 if (!CBB_init(&public_key, 32) ||
2303 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002304 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
2305 CBS_data(&peer_key), CBS_len(&peer_key)) ||
2306 !CBB_finish(&public_key, &hs->public_key, &hs->public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002307 OPENSSL_free(secret);
2308 SSL_ECDH_CTX_cleanup(&group);
2309 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002310 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002311 return 0;
2312 }
2313
2314 SSL_ECDH_CTX_cleanup(&group);
2315
2316 *out_secret = secret;
2317 *out_secret_len = secret_len;
2318 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002319 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002320}
2321
David Benjamin8baf9632016-11-17 17:11:16 +09002322int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002323 uint16_t group_id;
2324 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002325 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002326 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2327 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2328 !CBB_add_u16(&kse_bytes, group_id) ||
2329 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002330 !CBB_add_bytes(&public_key, hs->public_key, hs->public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002331 !CBB_flush(out)) {
2332 return 0;
2333 }
2334
David Benjamin8baf9632016-11-17 17:11:16 +09002335 OPENSSL_free(hs->public_key);
2336 hs->public_key = NULL;
2337 hs->public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002338
David Benjamin45738dd2017-02-09 20:01:26 -05002339 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002340 return 1;
2341}
2342
2343
Steven Valdezfdd10992016-09-15 16:27:05 -04002344/* Supported Versions
2345 *
2346 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2347
David Benjamin8c880a22016-12-03 02:20:34 -05002348static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2349 SSL *const ssl = hs->ssl;
Steven Valdezfdd10992016-09-15 16:27:05 -04002350 uint16_t min_version, max_version;
2351 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2352 return 0;
2353 }
2354
2355 if (max_version <= TLS1_2_VERSION) {
2356 return 1;
2357 }
2358
2359 CBB contents, versions;
2360 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2361 !CBB_add_u16_length_prefixed(out, &contents) ||
2362 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2363 return 0;
2364 }
2365
David Benjamind9791bf2016-09-27 16:39:52 -04002366 /* Add a fake version. See draft-davidben-tls-grease-01. */
2367 if (ssl->ctx->grease_enabled &&
2368 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2369 return 0;
2370 }
2371
Steven Valdezfdd10992016-09-15 16:27:05 -04002372 for (uint16_t version = max_version; version >= min_version; version--) {
2373 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2374 return 0;
2375 }
2376 }
2377
2378 if (!CBB_flush(out)) {
2379 return 0;
2380 }
2381
2382 return 1;
2383}
2384
2385
David Benjamin3baa6e12016-10-07 21:10:38 -04002386/* Cookie
2387 *
2388 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2389
David Benjamin8c880a22016-12-03 02:20:34 -05002390static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2391 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002392 return 1;
2393 }
2394
2395 CBB contents, cookie;
2396 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2397 !CBB_add_u16_length_prefixed(out, &contents) ||
2398 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002399 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002400 !CBB_flush(out)) {
2401 return 0;
2402 }
2403
2404 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002405 OPENSSL_free(hs->cookie);
2406 hs->cookie = NULL;
2407 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002408 return 1;
2409}
2410
2411
David Benjamin6f600d62016-12-21 16:06:54 -05002412/* Short record headers
2413 *
2414 * This is a non-standard extension which negotiates
2415 * https://github.com/tlswg/tls13-spec/pull/762 for experimenting. */
2416
2417static int ext_short_header_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2418 SSL *const ssl = hs->ssl;
2419 uint16_t min_version, max_version;
2420 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2421 return 0;
2422 }
2423
2424 if (max_version < TLS1_3_VERSION ||
2425 !ssl->ctx->short_header_enabled) {
2426 return 1;
2427 }
2428
2429 return CBB_add_u16(out, TLSEXT_TYPE_short_header) &&
2430 CBB_add_u16(out, 0 /* empty extension */);
2431}
2432
2433static int ext_short_header_parse_clienthello(SSL_HANDSHAKE *hs,
2434 uint8_t *out_alert,
2435 CBS *contents) {
2436 SSL *const ssl = hs->ssl;
2437 if (contents == NULL ||
2438 !ssl->ctx->short_header_enabled ||
2439 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2440 return 1;
2441 }
2442
2443 if (CBS_len(contents) != 0) {
2444 return 0;
2445 }
2446
2447 ssl->s3->short_header = 1;
2448 return 1;
2449}
2450
2451
Steven Valdezce902a92016-05-17 11:47:53 -04002452/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002453 *
Steven Valdezce902a92016-05-17 11:47:53 -04002454 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002455 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002456
David Benjamin8c880a22016-12-03 02:20:34 -05002457static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2458 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002459 CBB contents, groups_bytes;
2460 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002461 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002462 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002463 return 0;
2464 }
2465
David Benjamin65ac9972016-09-02 21:35:25 -04002466 /* Add a fake group. See draft-davidben-tls-grease-01. */
2467 if (ssl->ctx->grease_enabled &&
2468 !CBB_add_u16(&groups_bytes,
2469 ssl_get_grease_value(ssl, ssl_grease_group))) {
2470 return 0;
2471 }
2472
Steven Valdezce902a92016-05-17 11:47:53 -04002473 const uint16_t *groups;
2474 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002475 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002476
David Benjamin54091232016-09-05 12:47:25 -04002477 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002478 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002479 return 0;
2480 }
2481 }
2482
2483 return CBB_flush(out);
2484}
2485
David Benjamin8c880a22016-12-03 02:20:34 -05002486static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2487 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002488 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002489 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2490 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002491 return 1;
2492}
2493
David Benjamin8c880a22016-12-03 02:20:34 -05002494static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2495 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002496 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002497 if (contents == NULL) {
2498 return 1;
2499 }
2500
Steven Valdezce902a92016-05-17 11:47:53 -04002501 CBS supported_group_list;
2502 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2503 CBS_len(&supported_group_list) == 0 ||
2504 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002505 CBS_len(contents) != 0) {
2506 return 0;
2507 }
2508
David Benjamin8c880a22016-12-03 02:20:34 -05002509 hs->peer_supported_group_list =
2510 OPENSSL_malloc(CBS_len(&supported_group_list));
2511 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002512 *out_alert = SSL_AD_INTERNAL_ERROR;
2513 return 0;
2514 }
2515
Steven Valdezce902a92016-05-17 11:47:53 -04002516 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002517 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002518 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002519 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002520 goto err;
2521 }
2522 }
2523
Steven Valdezce902a92016-05-17 11:47:53 -04002524 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002525 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002526
2527 return 1;
2528
2529err:
David Benjamin8c880a22016-12-03 02:20:34 -05002530 OPENSSL_free(hs->peer_supported_group_list);
2531 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002532 *out_alert = SSL_AD_INTERNAL_ERROR;
2533 return 0;
2534}
2535
David Benjamin8c880a22016-12-03 02:20:34 -05002536static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002537 /* Servers don't echo this extension. */
2538 return 1;
2539}
2540
2541
Adam Langley614c66a2015-06-12 15:26:58 -07002542/* kExtensions contains all the supported extensions. */
2543static const struct tls_extension kExtensions[] = {
2544 {
Adam Langley5021b222015-06-12 18:27:58 -07002545 TLSEXT_TYPE_renegotiate,
2546 NULL,
2547 ext_ri_add_clienthello,
2548 ext_ri_parse_serverhello,
2549 ext_ri_parse_clienthello,
2550 ext_ri_add_serverhello,
2551 },
2552 {
Adam Langley614c66a2015-06-12 15:26:58 -07002553 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002554 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002555 ext_sni_add_clienthello,
2556 ext_sni_parse_serverhello,
2557 ext_sni_parse_clienthello,
2558 ext_sni_add_serverhello,
2559 },
Adam Langley0a056712015-07-01 15:03:33 -07002560 {
2561 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002562 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002563 ext_ems_add_clienthello,
2564 ext_ems_parse_serverhello,
2565 ext_ems_parse_clienthello,
2566 ext_ems_add_serverhello,
2567 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002568 {
2569 TLSEXT_TYPE_session_ticket,
2570 NULL,
2571 ext_ticket_add_clienthello,
2572 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002573 /* Ticket extension client parsing is handled in ssl_session.c */
2574 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002575 ext_ticket_add_serverhello,
2576 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002577 {
2578 TLSEXT_TYPE_signature_algorithms,
2579 NULL,
2580 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002581 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002582 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002583 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002584 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002585 {
2586 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002587 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002588 ext_ocsp_add_clienthello,
2589 ext_ocsp_parse_serverhello,
2590 ext_ocsp_parse_clienthello,
2591 ext_ocsp_add_serverhello,
2592 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002593 {
2594 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002595 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002596 ext_npn_add_clienthello,
2597 ext_npn_parse_serverhello,
2598 ext_npn_parse_clienthello,
2599 ext_npn_add_serverhello,
2600 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002601 {
2602 TLSEXT_TYPE_certificate_timestamp,
2603 NULL,
2604 ext_sct_add_clienthello,
2605 ext_sct_parse_serverhello,
2606 ext_sct_parse_clienthello,
2607 ext_sct_add_serverhello,
2608 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002609 {
2610 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002611 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002612 ext_alpn_add_clienthello,
2613 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002614 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2615 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002616 ext_alpn_add_serverhello,
2617 },
Adam Langley49c7af12015-07-10 14:33:46 -07002618 {
2619 TLSEXT_TYPE_channel_id,
2620 ext_channel_id_init,
2621 ext_channel_id_add_clienthello,
2622 ext_channel_id_parse_serverhello,
2623 ext_channel_id_parse_clienthello,
2624 ext_channel_id_add_serverhello,
2625 },
Adam Langley391250d2015-07-15 19:06:07 -07002626 {
2627 TLSEXT_TYPE_srtp,
2628 ext_srtp_init,
2629 ext_srtp_add_clienthello,
2630 ext_srtp_parse_serverhello,
2631 ext_srtp_parse_clienthello,
2632 ext_srtp_add_serverhello,
2633 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002634 {
2635 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002636 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002637 ext_ec_point_add_clienthello,
2638 ext_ec_point_parse_serverhello,
2639 ext_ec_point_parse_clienthello,
2640 ext_ec_point_add_serverhello,
2641 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002642 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002643 TLSEXT_TYPE_key_share,
2644 NULL,
2645 ext_key_share_add_clienthello,
2646 forbid_parse_serverhello,
2647 ignore_parse_clienthello,
2648 dont_add_serverhello,
2649 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002650 {
Steven Valdeza833c352016-11-01 13:39:36 -04002651 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002652 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002653 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002654 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002655 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002656 dont_add_serverhello,
2657 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002658 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002659 TLSEXT_TYPE_early_data,
2660 NULL,
2661 ext_early_data_add_clienthello,
2662 forbid_parse_serverhello,
2663 ext_early_data_parse_clienthello,
2664 dont_add_serverhello,
2665 },
2666 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002667 TLSEXT_TYPE_supported_versions,
2668 NULL,
2669 ext_supported_versions_add_clienthello,
2670 forbid_parse_serverhello,
2671 ignore_parse_clienthello,
2672 dont_add_serverhello,
2673 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002674 {
2675 TLSEXT_TYPE_cookie,
2676 NULL,
2677 ext_cookie_add_clienthello,
2678 forbid_parse_serverhello,
2679 ignore_parse_clienthello,
2680 dont_add_serverhello,
2681 },
David Benjamin6f600d62016-12-21 16:06:54 -05002682 {
2683 TLSEXT_TYPE_short_header,
2684 NULL,
2685 ext_short_header_add_clienthello,
2686 forbid_parse_serverhello,
2687 ext_short_header_parse_clienthello,
2688 dont_add_serverhello,
2689 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002690 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2691 * intolerant to the last extension being zero-length. See
2692 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002693 {
Steven Valdezce902a92016-05-17 11:47:53 -04002694 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002695 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002696 ext_supported_groups_add_clienthello,
2697 ext_supported_groups_parse_serverhello,
2698 ext_supported_groups_parse_clienthello,
2699 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002700 },
Adam Langley614c66a2015-06-12 15:26:58 -07002701};
2702
2703#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2704
Adam Langley4cfa96b2015-07-01 11:56:55 -07002705OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002706 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002707 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002708OPENSSL_COMPILE_ASSERT(
2709 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2710 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002711
Adam Langley614c66a2015-06-12 15:26:58 -07002712static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2713 uint16_t value) {
2714 unsigned i;
2715 for (i = 0; i < kNumExtensions; i++) {
2716 if (kExtensions[i].value == value) {
2717 *out_index = i;
2718 return &kExtensions[i];
2719 }
2720 }
2721
2722 return NULL;
2723}
2724
Adam Langley09505632015-07-30 18:10:13 -07002725int SSL_extension_supported(unsigned extension_value) {
2726 uint32_t index;
2727 return extension_value == TLSEXT_TYPE_padding ||
2728 tls_extension_find(&index, extension_value) != NULL;
2729}
2730
David Benjamin8c880a22016-12-03 02:20:34 -05002731int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2732 SSL *const ssl = hs->ssl;
David Benjaminf04c2e92016-12-06 13:35:25 -05002733 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2734 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002735 !ssl->s3->send_connection_binding) {
2736 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002737 }
Adam Langley95c29f32014-06-20 12:00:00 -07002738
David Benjamine8d53502015-10-10 14:13:23 -04002739 CBB extensions;
2740 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002741 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002742 }
Adam Langley95c29f32014-06-20 12:00:00 -07002743
David Benjamin8c880a22016-12-03 02:20:34 -05002744 hs->extensions.sent = 0;
2745 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002746
David Benjamin54091232016-09-05 12:47:25 -04002747 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002748 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002749 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002750 }
2751 }
Adam Langley95c29f32014-06-20 12:00:00 -07002752
David Benjamin65ac9972016-09-02 21:35:25 -04002753 uint16_t grease_ext1 = 0;
2754 if (ssl->ctx->grease_enabled) {
2755 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2756 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2757 if (!CBB_add_u16(&extensions, grease_ext1) ||
2758 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2759 goto err;
2760 }
2761 }
2762
David Benjamin54091232016-09-05 12:47:25 -04002763 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002764 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002765 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002766 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002767 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002768 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002769 }
Adam Langley95c29f32014-06-20 12:00:00 -07002770
Adam Langley33ad2b52015-07-20 17:43:53 -07002771 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002772 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002773 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002774 }
Adam Langley75712922014-10-10 16:23:43 -07002775
David Benjamin2bd19172016-11-17 16:47:15 +09002776 if (!custom_ext_add_clienthello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002777 goto err;
2778 }
2779
David Benjamin65ac9972016-09-02 21:35:25 -04002780 if (ssl->ctx->grease_enabled) {
2781 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2782 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2783
2784 /* The two fake extensions must not have the same value. GREASE values are
2785 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2786 * one. */
2787 if (grease_ext1 == grease_ext2) {
2788 grease_ext2 ^= 0x1010;
2789 }
2790
2791 if (!CBB_add_u16(&extensions, grease_ext2) ||
2792 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2793 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2794 goto err;
2795 }
2796 }
2797
David Benjamince079fd2016-08-02 16:22:34 -04002798 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002799 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002800 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002801 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002802 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002803 *
2804 * NB: because this code works out the length of all existing extensions
2805 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002806 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002807 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002808 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002809 * Server 7.0 is intolerant to the last extension being zero-length. See
2810 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002811 if (padding_len >= 4 + 1) {
2812 padding_len -= 4;
2813 } else {
2814 padding_len = 1;
2815 }
Adam Langley95c29f32014-06-20 12:00:00 -07002816
Adam Langley33ad2b52015-07-20 17:43:53 -07002817 uint8_t *padding_bytes;
2818 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2819 !CBB_add_u16(&extensions, padding_len) ||
2820 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2821 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002822 }
Adam Langley75712922014-10-10 16:23:43 -07002823
David Benjamin17cf2cb2016-12-13 01:07:13 -05002824 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002825 }
2826 }
Adam Langley75712922014-10-10 16:23:43 -07002827
Steven Valdeza833c352016-11-01 13:39:36 -04002828 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002829 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002830 goto err;
2831 }
2832
David Benjamina01deee2015-12-08 18:56:31 -05002833 /* Discard empty extensions blocks. */
2834 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002835 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002836 }
2837
David Benjamine8d53502015-10-10 14:13:23 -04002838 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002839
2840err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002841 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002842 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002843}
Adam Langley95c29f32014-06-20 12:00:00 -07002844
David Benjamin8c880a22016-12-03 02:20:34 -05002845int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2846 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002847 CBB extensions;
2848 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002849 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002850 }
2851
David Benjamin8c880a22016-12-03 02:20:34 -05002852 for (unsigned i = 0; i < kNumExtensions; i++) {
2853 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002854 /* Don't send extensions that were not received. */
2855 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002856 }
Adam Langley95c29f32014-06-20 12:00:00 -07002857
David Benjamin8c880a22016-12-03 02:20:34 -05002858 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002859 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002860 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002861 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002862 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002863 }
Adam Langley95c29f32014-06-20 12:00:00 -07002864
David Benjamin2bd19172016-11-17 16:47:15 +09002865 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002866 goto err;
2867 }
2868
Steven Valdez143e8b32016-07-11 13:19:03 -04002869 /* Discard empty extensions blocks before TLS 1.3. */
2870 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2871 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002872 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002873 }
2874
David Benjamin56380462015-10-10 14:59:09 -04002875 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002876
2877err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002878 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002879 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002880}
Adam Langley95c29f32014-06-20 12:00:00 -07002881
David Benjamin731058e2016-12-03 23:15:13 -05002882static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2883 const SSL_CLIENT_HELLO *client_hello,
2884 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002885 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002886 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002887 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002888 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002889 }
2890 }
2891
David Benjamin8c880a22016-12-03 02:20:34 -05002892 hs->extensions.received = 0;
2893 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002894
David Benjamine14ff062016-08-09 16:21:24 -04002895 CBS extensions;
2896 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2897 while (CBS_len(&extensions) != 0) {
2898 uint16_t type;
2899 CBS extension;
2900
2901 /* Decode the next extension. */
2902 if (!CBS_get_u16(&extensions, &type) ||
2903 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002904 *out_alert = SSL_AD_DECODE_ERROR;
2905 return 0;
2906 }
Adam Langley95c29f32014-06-20 12:00:00 -07002907
David Benjamine14ff062016-08-09 16:21:24 -04002908 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2909 * ambiguous. Ignore all but the renegotiation_info extension. */
2910 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2911 continue;
2912 }
Adam Langley95c29f32014-06-20 12:00:00 -07002913
David Benjamine14ff062016-08-09 16:21:24 -04002914 unsigned ext_index;
2915 const struct tls_extension *const ext =
2916 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002917
David Benjamine14ff062016-08-09 16:21:24 -04002918 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002919 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002920 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002921 return 0;
2922 }
David Benjamine14ff062016-08-09 16:21:24 -04002923 continue;
2924 }
2925
David Benjamin8c880a22016-12-03 02:20:34 -05002926 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002927 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002928 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002929 *out_alert = alert;
2930 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002931 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002932 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002933 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002934 }
Adam Langley75712922014-10-10 16:23:43 -07002935
David Benjamin1deb41b2016-08-09 19:36:38 -04002936 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002937 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002938 continue;
2939 }
2940
2941 CBS *contents = NULL, fake_contents;
2942 static const uint8_t kFakeRenegotiateExtension[] = {0};
2943 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2944 ssl_client_cipher_list_contains_cipher(client_hello,
2945 SSL3_CK_SCSV & 0xffff)) {
2946 /* The renegotiation SCSV was received so pretend that we received a
2947 * renegotiation extension. */
2948 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2949 sizeof(kFakeRenegotiateExtension));
2950 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002951 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002952 }
2953
2954 /* Extension wasn't observed so call the callback with a NULL
2955 * parameter. */
2956 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002957 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002958 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002959 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002960 *out_alert = alert;
2961 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002962 }
2963 }
2964
Adam Langleyfcf25832014-12-18 17:42:32 -08002965 return 1;
2966}
Adam Langley95c29f32014-06-20 12:00:00 -07002967
David Benjamin731058e2016-12-03 23:15:13 -05002968int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2969 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002970 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002971 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002972 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002973 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002974 return 0;
2975 }
Adam Langley95c29f32014-06-20 12:00:00 -07002976
David Benjamin8c880a22016-12-03 02:20:34 -05002977 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002978 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002979 return 0;
2980 }
Adam Langley95c29f32014-06-20 12:00:00 -07002981
Adam Langleyfcf25832014-12-18 17:42:32 -08002982 return 1;
2983}
Adam Langley95c29f32014-06-20 12:00:00 -07002984
David Benjamin8c880a22016-12-03 02:20:34 -05002985static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2986 int *out_alert) {
2987 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002988 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2989 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2990 return 1;
2991 }
Adam Langley614c66a2015-06-12 15:26:58 -07002992
Steven Valdez143e8b32016-07-11 13:19:03 -04002993 /* Decode the extensions block and check it is valid. */
2994 CBS extensions;
2995 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2996 !tls1_check_duplicate_extensions(&extensions)) {
2997 *out_alert = SSL_AD_DECODE_ERROR;
2998 return 0;
2999 }
3000
3001 uint32_t received = 0;
3002 while (CBS_len(&extensions) != 0) {
3003 uint16_t type;
3004 CBS extension;
3005
3006 /* Decode the next extension. */
3007 if (!CBS_get_u16(&extensions, &type) ||
3008 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003009 *out_alert = SSL_AD_DECODE_ERROR;
3010 return 0;
3011 }
Adam Langley95c29f32014-06-20 12:00:00 -07003012
Steven Valdez143e8b32016-07-11 13:19:03 -04003013 unsigned ext_index;
3014 const struct tls_extension *const ext =
3015 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07003016
Steven Valdez143e8b32016-07-11 13:19:03 -04003017 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09003018 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003019 return 0;
3020 }
Steven Valdez143e8b32016-07-11 13:19:03 -04003021 continue;
3022 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003023
David Benjamin5db7c9b2017-01-24 16:17:03 -05003024 OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
3025 too_many_bits);
3026
David Benjamin8c880a22016-12-03 02:20:34 -05003027 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04003028 type != TLSEXT_TYPE_renegotiate) {
3029 /* If the extension was never sent then it is illegal, except for the
3030 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04003031 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3032 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04003033 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04003034 return 0;
3035 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003036
Steven Valdez143e8b32016-07-11 13:19:03 -04003037 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07003038
Steven Valdez143e8b32016-07-11 13:19:03 -04003039 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003040 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04003041 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003042 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04003043 *out_alert = alert;
3044 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07003045 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003046 }
Adam Langley95c29f32014-06-20 12:00:00 -07003047
David Benjamin54091232016-09-05 12:47:25 -04003048 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003049 if (!(received & (1u << i))) {
3050 /* Extension wasn't observed so call the callback with a NULL
3051 * parameter. */
3052 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003053 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003054 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003055 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07003056 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08003057 return 0;
3058 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003059 }
3060 }
Adam Langley95c29f32014-06-20 12:00:00 -07003061
Adam Langleyfcf25832014-12-18 17:42:32 -08003062 return 1;
3063}
Adam Langley95c29f32014-06-20 12:00:00 -07003064
David Benjamin8c880a22016-12-03 02:20:34 -05003065static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3066 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003067 int ret = SSL_TLSEXT_ERR_NOACK;
3068 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003069
David Benjamin78f8aab2016-03-10 16:33:58 -05003070 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003071 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003072 ssl->ctx->tlsext_servername_arg);
3073 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003074 ret = ssl->initial_ctx->tlsext_servername_callback(
3075 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003076 }
Adam Langley95c29f32014-06-20 12:00:00 -07003077
Adam Langleyfcf25832014-12-18 17:42:32 -08003078 switch (ret) {
3079 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003080 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003081 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003082
Adam Langleyfcf25832014-12-18 17:42:32 -08003083 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003084 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003085 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003086
Adam Langleyfcf25832014-12-18 17:42:32 -08003087 default:
3088 return 1;
3089 }
3090}
Adam Langleyed8270a2014-09-02 13:52:56 -07003091
David Benjamin8c880a22016-12-03 02:20:34 -05003092int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3093 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08003094 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003095 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003096 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003097 return 0;
3098 }
3099
Adam Langleyfcf25832014-12-18 17:42:32 -08003100 return 1;
3101}
Adam Langley95c29f32014-06-20 12:00:00 -07003102
David Benjamine3aa1d92015-06-16 15:34:50 -04003103int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05003104 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04003105 size_t ticket_len, const uint8_t *session_id,
3106 size_t session_id_len) {
3107 int ret = 1; /* Most errors are non-fatal. */
3108 SSL_CTX *ssl_ctx = ssl->initial_ctx;
3109 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003110
David Benjamine3aa1d92015-06-16 15:34:50 -04003111 HMAC_CTX hmac_ctx;
3112 HMAC_CTX_init(&hmac_ctx);
3113 EVP_CIPHER_CTX cipher_ctx;
3114 EVP_CIPHER_CTX_init(&cipher_ctx);
3115
David Benjaminef1b0092015-11-21 14:05:44 -05003116 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04003117 *out_session = NULL;
3118
Steven Valdez4aa154e2016-07-29 14:32:55 -04003119 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
3120 goto done;
3121 }
3122
David Benjamine3aa1d92015-06-16 15:34:50 -04003123 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3124 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003125 }
3126
David Benjaminadcc3952015-04-26 13:07:57 -04003127 /* Ensure there is room for the key name and the largest IV
3128 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3129 * the maximum IV length should be well under the minimum size for the
3130 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003131 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3132 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003133 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003134 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003135
David Benjamine3aa1d92015-06-16 15:34:50 -04003136 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003137 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3138 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3139 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003140 if (cb_ret < 0) {
3141 ret = 0;
3142 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003143 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003144 if (cb_ret == 0) {
3145 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003146 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003147 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003148 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003149 }
3150 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003151 /* Check the key name matches. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003152 if (OPENSSL_memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3153 SSL_TICKET_KEY_NAME_LEN) != 0) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003154 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003155 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003156 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3157 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003158 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003159 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3160 ssl_ctx->tlsext_tick_aes_key, iv)) {
3161 ret = 0;
3162 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003163 }
3164 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003165 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003166
David Benjamine3aa1d92015-06-16 15:34:50 -04003167 /* Check the MAC at the end of the ticket. */
3168 uint8_t mac[EVP_MAX_MD_SIZE];
3169 size_t mac_len = HMAC_size(&hmac_ctx);
3170 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003171 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003172 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003173 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003174 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3175 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003176 int mac_ok =
3177 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3178#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3179 mac_ok = 1;
3180#endif
3181 if (!mac_ok) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003182 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003183 }
3184
David Benjamine3aa1d92015-06-16 15:34:50 -04003185 /* Decrypt the session data. */
3186 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3187 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3188 mac_len;
3189 plaintext = OPENSSL_malloc(ciphertext_len);
3190 if (plaintext == NULL) {
3191 ret = 0;
3192 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003193 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003194 size_t plaintext_len;
3195#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin17cf2cb2016-12-13 01:07:13 -05003196 OPENSSL_memcpy(plaintext, ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003197 plaintext_len = ciphertext_len;
3198#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003199 if (ciphertext_len >= INT_MAX) {
3200 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003201 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003202 int len1, len2;
3203 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3204 (int)ciphertext_len) ||
3205 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
3206 ERR_clear_error(); /* Don't leave an error on the queue. */
3207 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003208 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003209 plaintext_len = (size_t)(len1 + len2);
3210#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003211
David Benjamine3aa1d92015-06-16 15:34:50 -04003212 /* Decode the session. */
Adam Langley46db7af2017-02-01 15:49:37 -08003213 SSL_SESSION *session =
3214 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx);
David Benjamine3aa1d92015-06-16 15:34:50 -04003215 if (session == NULL) {
3216 ERR_clear_error(); /* Don't leave an error on the queue. */
3217 goto done;
3218 }
3219
3220 /* Copy the client's session ID into the new session, to denote the ticket has
3221 * been accepted. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003222 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003223 session->session_id_length = session_id_len;
3224
3225 *out_session = session;
3226
3227done:
3228 OPENSSL_free(plaintext);
3229 HMAC_CTX_cleanup(&hmac_ctx);
3230 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3231 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003232}
Adam Langley95c29f32014-06-20 12:00:00 -07003233
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003234int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003235 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003236 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003237 return 1;
3238 }
David Benjamincd996942014-07-20 16:23:51 -04003239
David Benjamin0fc37ef2016-08-17 15:29:46 -04003240 OPENSSL_free(hs->peer_sigalgs);
3241 hs->peer_sigalgs = NULL;
3242 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003243
3244 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003245 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003246 return 0;
3247 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003248 num_sigalgs /= 2;
3249
3250 /* supported_signature_algorithms in the certificate request is
3251 * allowed to be empty. */
3252 if (num_sigalgs == 0) {
3253 return 1;
3254 }
3255
Steven Valdez02563852016-06-23 13:33:05 -04003256 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3257 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003258 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3259 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003260 return 0;
3261 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003262 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003263
3264 CBS sigalgs;
3265 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003266 for (size_t i = 0; i < num_sigalgs; i++) {
3267 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003268 return 0;
3269 }
3270 }
Adam Langley95c29f32014-06-20 12:00:00 -07003271
Adam Langleyfcf25832014-12-18 17:42:32 -08003272 return 1;
3273}
David Benjaminec2f27d2014-11-13 19:17:25 -05003274
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003275int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3276 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003277 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003278
Steven Valdezf0451ca2016-06-29 13:16:27 -04003279 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3280 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3281 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003282 int type = ssl_private_key_type(ssl);
3283 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003284 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003285 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003286 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003287 if (ssl_is_ecdsa_key_type(type)) {
3288 *out = SSL_SIGN_ECDSA_SHA1;
3289 return 1;
3290 }
3291 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3292 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003293 }
3294
David Benjamin3ef76972016-10-17 17:59:54 -04003295 const uint16_t *sigalgs = cert->sigalgs;
3296 size_t num_sigalgs = cert->num_sigalgs;
3297 if (sigalgs == NULL) {
3298 sigalgs = kSignSignatureAlgorithms;
3299 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003300 }
3301
David Benjamin0fc37ef2016-08-17 15:29:46 -04003302 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3303 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3304 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003305 /* If the client didn't specify any signature_algorithms extension then
3306 * we can assume that it supports SHA1. See
3307 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3308 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3309 SSL_SIGN_ECDSA_SHA1};
3310 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003311 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003312 }
3313
David Benjamin0fc37ef2016-08-17 15:29:46 -04003314 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003315 uint16_t sigalg = sigalgs[i];
3316 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3317 * negotiated. */
3318 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3319 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3320 continue;
3321 }
3322
David Benjamin0fc37ef2016-08-17 15:29:46 -04003323 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003324 if (sigalg == peer_sigalgs[j]) {
3325 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003326 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003327 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003328 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003329 }
Adam Langley95c29f32014-06-20 12:00:00 -07003330
David Benjaminea9a0d52016-07-08 15:52:59 -07003331 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3332 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003333}
Adam Langley95c29f32014-06-20 12:00:00 -07003334
Steven Valdez908ac192017-01-12 13:17:07 -05003335int tls1_verify_channel_id(SSL_HANDSHAKE *hs) {
3336 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003337 int ret = 0;
3338 uint16_t extension_type;
3339 CBS extension, channel_id;
3340
3341 /* A Channel ID handshake message is structured to contain multiple
3342 * extensions, but the only one that can be present is Channel ID. */
3343 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3344 if (!CBS_get_u16(&channel_id, &extension_type) ||
3345 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3346 CBS_len(&channel_id) != 0 ||
3347 extension_type != TLSEXT_TYPE_channel_id ||
3348 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3349 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3350 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3351 return 0;
3352 }
3353
3354 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3355 if (!p256) {
3356 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3357 return 0;
3358 }
3359
3360 EC_KEY *key = NULL;
3361 EC_POINT *point = NULL;
3362 BIGNUM x, y;
3363 ECDSA_SIG sig;
3364 BN_init(&x);
3365 BN_init(&y);
3366 sig.r = BN_new();
3367 sig.s = BN_new();
3368 if (sig.r == NULL || sig.s == NULL) {
3369 goto err;
3370 }
3371
3372 const uint8_t *p = CBS_data(&extension);
3373 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3374 BN_bin2bn(p + 32, 32, &y) == NULL ||
3375 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3376 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3377 goto err;
3378 }
3379
3380 point = EC_POINT_new(p256);
3381 if (point == NULL ||
3382 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3383 goto err;
3384 }
3385
3386 key = EC_KEY_new();
3387 if (key == NULL ||
3388 !EC_KEY_set_group(key, p256) ||
3389 !EC_KEY_set_public_key(key, point)) {
3390 goto err;
3391 }
3392
3393 uint8_t digest[EVP_MAX_MD_SIZE];
3394 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003395 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003396 goto err;
3397 }
3398
3399 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3400#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3401 sig_ok = 1;
3402#endif
3403 if (!sig_ok) {
3404 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3405 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3406 ssl->s3->tlsext_channel_id_valid = 0;
3407 goto err;
3408 }
3409
David Benjamin17cf2cb2016-12-13 01:07:13 -05003410 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
Nick Harper60a85cb2016-09-23 16:25:11 -07003411 ret = 1;
3412
3413err:
3414 BN_free(&x);
3415 BN_free(&y);
3416 BN_free(sig.r);
3417 BN_free(sig.s);
3418 EC_KEY_free(key);
3419 EC_POINT_free(point);
3420 EC_GROUP_free(p256);
3421 return ret;
3422}
3423
Steven Valdez908ac192017-01-12 13:17:07 -05003424int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3425 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003426 uint8_t digest[EVP_MAX_MD_SIZE];
3427 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003428 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003429 return 0;
3430 }
3431
3432 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3433 if (ec_key == NULL) {
3434 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3435 return 0;
3436 }
3437
3438 int ret = 0;
3439 BIGNUM *x = BN_new();
3440 BIGNUM *y = BN_new();
3441 ECDSA_SIG *sig = NULL;
3442 if (x == NULL || y == NULL ||
3443 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3444 EC_KEY_get0_public_key(ec_key),
3445 x, y, NULL)) {
3446 goto err;
3447 }
3448
3449 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3450 if (sig == NULL) {
3451 goto err;
3452 }
3453
3454 CBB child;
3455 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3456 !CBB_add_u16_length_prefixed(cbb, &child) ||
3457 !BN_bn2cbb_padded(&child, 32, x) ||
3458 !BN_bn2cbb_padded(&child, 32, y) ||
3459 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3460 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3461 !CBB_flush(cbb)) {
3462 goto err;
3463 }
3464
3465 ret = 1;
3466
3467err:
3468 BN_free(x);
3469 BN_free(y);
3470 ECDSA_SIG_free(sig);
3471 return ret;
3472}
3473
Steven Valdez908ac192017-01-12 13:17:07 -05003474int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3475 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003476 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3477 uint8_t *msg;
3478 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003479 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003480 ssl_cert_verify_channel_id)) {
3481 return 0;
3482 }
3483 SHA256(msg, msg_len, out);
3484 *out_len = SHA256_DIGEST_LENGTH;
3485 OPENSSL_free(msg);
3486 return 1;
3487 }
3488
Nick Harper95594012016-10-20 14:07:13 -07003489 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003490
Nick Harper95594012016-10-20 14:07:13 -07003491 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003492 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003493 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003494
Steven Valdez87eab492016-06-27 16:34:59 -04003495 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003496 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003497 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003498 if (ssl->session->original_handshake_hash_len == 0) {
3499 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003500 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003501 }
Nick Harper95594012016-10-20 14:07:13 -07003502 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3503 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003504 }
3505
Steven Valdez908ac192017-01-12 13:17:07 -05003506 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3507 size_t hs_hash_len;
3508 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript, hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003509 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003510 }
Steven Valdez908ac192017-01-12 13:17:07 -05003511 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003512 SHA256_Final(out, &ctx);
3513 *out_len = SHA256_DIGEST_LENGTH;
3514 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003515}
Adam Langley1258b6a2014-06-20 12:00:00 -07003516
3517/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin45738dd2017-02-09 20:01:26 -05003518 * hashes in |hs->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003519 * data. */
Steven Valdez908ac192017-01-12 13:17:07 -05003520int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3521 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003522 /* This function should never be called for a resumed session because the
3523 * handshake hashes that we wish to record are for the original, full
3524 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003525 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003526 return -1;
3527 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003528
Steven Valdez908ac192017-01-12 13:17:07 -05003529 OPENSSL_COMPILE_ASSERT(
David Benjamin45738dd2017-02-09 20:01:26 -05003530 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
Steven Valdez908ac192017-01-12 13:17:07 -05003531 original_handshake_hash_is_too_small);
3532
3533 size_t digest_len;
3534 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript,
David Benjamin45738dd2017-02-09 20:01:26 -05003535 hs->new_session->original_handshake_hash,
Steven Valdez908ac192017-01-12 13:17:07 -05003536 &digest_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003537 return -1;
3538 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003539
Steven Valdez908ac192017-01-12 13:17:07 -05003540 OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE <= 0xff, max_md_size_is_too_large);
David Benjamin45738dd2017-02-09 20:01:26 -05003541 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003542
Adam Langleyfcf25832014-12-18 17:42:32 -08003543 return 1;
3544}
Nick Harper60a85cb2016-09-23 16:25:11 -07003545
3546int ssl_do_channel_id_callback(SSL *ssl) {
3547 if (ssl->tlsext_channel_id_private != NULL ||
3548 ssl->ctx->channel_id_cb == NULL) {
3549 return 1;
3550 }
3551
3552 EVP_PKEY *key = NULL;
3553 ssl->ctx->channel_id_cb(ssl, &key);
3554 if (key == NULL) {
3555 /* The caller should try again later. */
3556 return 1;
3557 }
3558
3559 int ret = SSL_set1_tls_channel_id(ssl, key);
3560 EVP_PKEY_free(key);
3561 return ret;
3562}
Adam Langleycfa08c32016-11-17 13:21:27 -08003563
3564int ssl_is_sct_list_valid(const CBS *contents) {
3565 /* Shallow parse the SCT list for sanity. By the RFC
3566 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3567 * of the SCTs may be empty. */
3568 CBS copy = *contents;
3569 CBS sct_list;
3570 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3571 CBS_len(&copy) != 0 ||
3572 CBS_len(&sct_list) == 0) {
3573 return 0;
3574 }
3575
3576 while (CBS_len(&sct_list) > 0) {
3577 CBS sct;
3578 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3579 CBS_len(&sct) == 0) {
3580 return 0;
3581 }
3582 }
3583
3584 return 1;
3585}