blob: ef4a88909be7dee2f7d3702c8a618bbadba3b307 [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 Benjaminfc02b592017-02-17 16:26:01 -0500873
874 if (contents != NULL) {
875 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
876 ssl->version == SSL3_VERSION ||
877 CBS_len(contents) != 0) {
David Benjamin163c9562016-08-29 23:14:17 -0400878 return 0;
879 }
880
David Benjaminfc02b592017-02-17 16:26:01 -0500881 hs->extended_master_secret = 1;
David Benjamin163c9562016-08-29 23:14:17 -0400882 }
883
David Benjaminfc02b592017-02-17 16:26:01 -0500884 /* Whether EMS is negotiated may not change on renegotiation. */
885 if (ssl->s3->established_session != NULL &&
886 hs->extended_master_secret !=
887 ssl->s3->established_session->extended_master_secret) {
888 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
889 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400890 return 0;
891 }
892
Adam Langley0a056712015-07-01 15:03:33 -0700893 return 1;
894}
895
David Benjamin8c880a22016-12-03 02:20:34 -0500896static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500897 CBS *contents) {
David Benjaminfc02b592017-02-17 16:26:01 -0500898 uint16_t version = ssl3_protocol_version(hs->ssl);
David Benjamin8c880a22016-12-03 02:20:34 -0500899 if (version >= TLS1_3_VERSION ||
900 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400901 return 1;
902 }
903
904 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700905 return 1;
906 }
907
908 if (CBS_len(contents) != 0) {
909 return 0;
910 }
911
David Benjaminfc02b592017-02-17 16:26:01 -0500912 hs->extended_master_secret = 1;
Adam Langley0a056712015-07-01 15:03:33 -0700913 return 1;
914}
915
David Benjamin8c880a22016-12-03 02:20:34 -0500916static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -0500917 if (!hs->extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700918 return 1;
919 }
920
921 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
922 !CBB_add_u16(out, 0 /* length */)) {
923 return 0;
924 }
925
926 return 1;
927}
928
Adam Langley9b05bc52015-07-01 15:25:33 -0700929
930/* Session tickets.
931 *
932 * https://tools.ietf.org/html/rfc5077 */
933
David Benjamin8c880a22016-12-03 02:20:34 -0500934static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
935 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400936 uint16_t min_version, max_version;
937 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
938 return 0;
939 }
940
941 /* TLS 1.3 uses a different ticket extension. */
942 if (min_version >= TLS1_3_VERSION ||
943 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700944 return 1;
945 }
946
947 const uint8_t *ticket_data = NULL;
948 int ticket_len = 0;
949
950 /* Renegotiation does not participate in session resumption. However, still
951 * advertise the extension to avoid potentially breaking servers which carry
952 * over the state from the previous handshake, such as OpenSSL servers
953 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -0400954 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -0700955 if (!ssl->s3->initial_handshake_complete &&
956 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400957 ssl->session->tlsext_tick != NULL &&
958 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -0400959 ssl->method->version_from_wire(&session_version,
960 ssl->session->ssl_version) &&
961 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700962 ticket_data = ssl->session->tlsext_tick;
963 ticket_len = ssl->session->tlsext_ticklen;
964 }
965
966 CBB ticket;
967 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
968 !CBB_add_u16_length_prefixed(out, &ticket) ||
969 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
970 !CBB_flush(out)) {
971 return 0;
972 }
973
974 return 1;
975}
976
David Benjamin8c880a22016-12-03 02:20:34 -0500977static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -0700978 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500979 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -0700980 if (contents == NULL) {
981 return 1;
982 }
983
Steven Valdez143e8b32016-07-11 13:19:03 -0400984 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
985 return 0;
986 }
987
Adam Langley9b05bc52015-07-01 15:25:33 -0700988 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
989 * this function should never be called, even if the server tries to send the
990 * extension. */
991 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
992
993 if (CBS_len(contents) != 0) {
994 return 0;
995 }
996
David Benjamin8c880a22016-12-03 02:20:34 -0500997 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -0700998 return 1;
999}
1000
David Benjamin8c880a22016-12-03 02:20:34 -05001001static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1002 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001003 return 1;
1004 }
1005
David Benjamin78476f62016-11-12 11:20:55 +09001006 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin8c880a22016-12-03 02:20:34 -05001007 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001008
1009 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1010 !CBB_add_u16(out, 0 /* length */)) {
1011 return 0;
1012 }
1013
1014 return 1;
1015}
1016
1017
Adam Langley2e857bd2015-07-01 16:09:19 -07001018/* Signature Algorithms.
1019 *
1020 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1021
David Benjamin8c880a22016-12-03 02:20:34 -05001022static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1023 SSL *const ssl = hs->ssl;
David Benjamin2dc02042016-09-19 19:57:37 -04001024 uint16_t min_version, max_version;
1025 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1026 return 0;
1027 }
1028
1029 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001030 return 1;
1031 }
1032
David Benjamin0fc37ef2016-08-17 15:29:46 -04001033 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001034 const size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001035
David Benjamin0fc37ef2016-08-17 15:29:46 -04001036 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001037 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1038 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001039 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001040 return 0;
1041 }
1042
David Benjamin0fc37ef2016-08-17 15:29:46 -04001043 for (size_t i = 0; i < num_sigalgs; i++) {
1044 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001045 return 0;
1046 }
1047 }
1048
1049 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001050 return 0;
1051 }
1052
1053 return 1;
1054}
1055
David Benjamin8c880a22016-12-03 02:20:34 -05001056static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001057 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001058 OPENSSL_free(hs->peer_sigalgs);
1059 hs->peer_sigalgs = NULL;
1060 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001061
Adam Langley2e857bd2015-07-01 16:09:19 -07001062 if (contents == NULL) {
1063 return 1;
1064 }
1065
1066 CBS supported_signature_algorithms;
1067 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001068 CBS_len(contents) != 0 ||
1069 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001070 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001071 return 0;
1072 }
1073
1074 return 1;
1075}
1076
Adam Langley2e857bd2015-07-01 16:09:19 -07001077
Adam Langleybb0bd042015-07-01 16:21:03 -07001078/* OCSP Stapling.
1079 *
1080 * https://tools.ietf.org/html/rfc6066#section-8 */
1081
David Benjamin8c880a22016-12-03 02:20:34 -05001082static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1083 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001084 if (!ssl->ocsp_stapling_enabled) {
1085 return 1;
1086 }
1087
1088 CBB contents;
1089 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1090 !CBB_add_u16_length_prefixed(out, &contents) ||
1091 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1092 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1093 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1094 !CBB_flush(out)) {
1095 return 0;
1096 }
1097
1098 return 1;
1099}
1100
David Benjamin8c880a22016-12-03 02:20:34 -05001101static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001102 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001103 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001104 if (contents == NULL) {
1105 return 1;
1106 }
1107
Steven Valdeza833c352016-11-01 13:39:36 -04001108 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1109 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001110 return 0;
1111 }
1112
Steven Valdeza833c352016-11-01 13:39:36 -04001113 /* OCSP stapling is forbidden on non-certificate ciphers. */
1114 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001115 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001116 return 0;
1117 }
1118
Steven Valdeza833c352016-11-01 13:39:36 -04001119 /* Note this does not check for resumption in TLS 1.2. Sending
1120 * status_request here does not make sense, but OpenSSL does so and the
1121 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001122
David Benjamin8c880a22016-12-03 02:20:34 -05001123 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001124 return 1;
1125}
1126
David Benjamin8c880a22016-12-03 02:20:34 -05001127static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001128 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001129 if (contents == NULL) {
1130 return 1;
1131 }
1132
1133 uint8_t status_type;
1134 if (!CBS_get_u8(contents, &status_type)) {
1135 return 0;
1136 }
1137
1138 /* We cannot decide whether OCSP stapling will occur yet because the correct
1139 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001140 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001141
Adam Langleybb0bd042015-07-01 16:21:03 -07001142 return 1;
1143}
1144
David Benjamin8c880a22016-12-03 02:20:34 -05001145static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1146 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001147 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001148 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001149 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001150 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001151 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001152 return 1;
1153 }
1154
David Benjamin8c880a22016-12-03 02:20:34 -05001155 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001156
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001157 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001158 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001159}
1160
1161
Adam Langley97dfcbf2015-07-01 18:35:20 -07001162/* Next protocol negotiation.
1163 *
1164 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1165
David Benjamin8c880a22016-12-03 02:20:34 -05001166static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1167 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001168 if (ssl->s3->initial_handshake_complete ||
1169 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001170 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001171 return 1;
1172 }
1173
1174 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1175 !CBB_add_u16(out, 0 /* length */)) {
1176 return 0;
1177 }
1178
1179 return 1;
1180}
1181
David Benjamin8c880a22016-12-03 02:20:34 -05001182static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001183 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001184 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001185 if (contents == NULL) {
1186 return 1;
1187 }
1188
Steven Valdez143e8b32016-07-11 13:19:03 -04001189 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1190 return 0;
1191 }
1192
Adam Langley97dfcbf2015-07-01 18:35:20 -07001193 /* If any of these are false then we should never have sent the NPN
1194 * extension in the ClientHello and thus this function should never have been
1195 * called. */
1196 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001197 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001198 assert(ssl->ctx->next_proto_select_cb != NULL);
1199
David Benjamin76c2efc2015-08-31 14:24:29 -04001200 if (ssl->s3->alpn_selected != NULL) {
1201 /* NPN and ALPN may not be negotiated in the same connection. */
1202 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1203 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1204 return 0;
1205 }
1206
Adam Langley97dfcbf2015-07-01 18:35:20 -07001207 const uint8_t *const orig_contents = CBS_data(contents);
1208 const size_t orig_len = CBS_len(contents);
1209
1210 while (CBS_len(contents) != 0) {
1211 CBS proto;
1212 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1213 CBS_len(&proto) == 0) {
1214 return 0;
1215 }
1216 }
1217
1218 uint8_t *selected;
1219 uint8_t selected_len;
1220 if (ssl->ctx->next_proto_select_cb(
1221 ssl, &selected, &selected_len, orig_contents, orig_len,
1222 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1223 *out_alert = SSL_AD_INTERNAL_ERROR;
1224 return 0;
1225 }
1226
David Benjamin79978df2015-12-25 15:56:49 -05001227 OPENSSL_free(ssl->s3->next_proto_negotiated);
1228 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1229 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001230 *out_alert = SSL_AD_INTERNAL_ERROR;
1231 return 0;
1232 }
1233
David Benjamin79978df2015-12-25 15:56:49 -05001234 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001235 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001236
1237 return 1;
1238}
1239
David Benjamin8c880a22016-12-03 02:20:34 -05001240static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001241 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001242 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001243 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1244 return 1;
1245 }
1246
Adam Langley97dfcbf2015-07-01 18:35:20 -07001247 if (contents != NULL && CBS_len(contents) != 0) {
1248 return 0;
1249 }
1250
1251 if (contents == NULL ||
1252 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001253 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001254 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001255 return 1;
1256 }
1257
David Benjamin8c880a22016-12-03 02:20:34 -05001258 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001259 return 1;
1260}
1261
David Benjamin8c880a22016-12-03 02:20:34 -05001262static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1263 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001264 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1265 * parsed. */
David Benjamin8c880a22016-12-03 02:20:34 -05001266 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001267 return 1;
1268 }
1269
1270 const uint8_t *npa;
1271 unsigned npa_len;
1272
1273 if (ssl->ctx->next_protos_advertised_cb(
1274 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1275 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001276 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001277 return 1;
1278 }
1279
1280 CBB contents;
1281 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1282 !CBB_add_u16_length_prefixed(out, &contents) ||
1283 !CBB_add_bytes(&contents, npa, npa_len) ||
1284 !CBB_flush(out)) {
1285 return 0;
1286 }
1287
1288 return 1;
1289}
1290
1291
Adam Langleyab8d87d2015-07-10 12:21:39 -07001292/* Signed certificate timestamps.
1293 *
1294 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1295
David Benjamin8c880a22016-12-03 02:20:34 -05001296static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1297 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001298 if (!ssl->signed_cert_timestamps_enabled) {
1299 return 1;
1300 }
1301
1302 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1303 !CBB_add_u16(out, 0 /* length */)) {
1304 return 0;
1305 }
1306
1307 return 1;
1308}
1309
David Benjamin8c880a22016-12-03 02:20:34 -05001310static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001311 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001312 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001313 if (contents == NULL) {
1314 return 1;
1315 }
1316
Steven Valdeza833c352016-11-01 13:39:36 -04001317 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1318 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001319 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001320 return 0;
1321 }
1322
Adam Langleyab8d87d2015-07-10 12:21:39 -07001323 /* If this is false then we should never have sent the SCT extension in the
1324 * ClientHello and thus this function should never have been called. */
1325 assert(ssl->signed_cert_timestamps_enabled);
1326
Adam Langleycfa08c32016-11-17 13:21:27 -08001327 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001328 *out_alert = SSL_AD_DECODE_ERROR;
1329 return 0;
1330 }
1331
David Benjamindaa88502016-10-04 16:32:16 -04001332 /* Session resumption uses the original session information. The extension
1333 * should not be sent on resumption, but RFC 6962 did not make it a
1334 * requirement, so tolerate this.
1335 *
1336 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001337 if (!ssl->s3->session_reused &&
David Benjamin45738dd2017-02-09 20:01:26 -05001338 !CBS_stow(contents, &hs->new_session->tlsext_signed_cert_timestamp_list,
1339 &hs->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001340 *out_alert = SSL_AD_INTERNAL_ERROR;
1341 return 0;
1342 }
1343
1344 return 1;
1345}
1346
David Benjamin8c880a22016-12-03 02:20:34 -05001347static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001348 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001349 if (contents == NULL) {
1350 return 1;
1351 }
1352
1353 if (CBS_len(contents) != 0) {
1354 return 0;
1355 }
1356
David Benjamin8c880a22016-12-03 02:20:34 -05001357 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001358 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001359}
1360
David Benjamin8c880a22016-12-03 02:20:34 -05001361static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1362 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001363 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001364 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1365 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001366 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001367 return 1;
1368 }
1369
1370 CBB contents;
1371 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1372 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001373 CBB_add_bytes(
1374 &contents,
1375 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1376 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001377 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001378}
1379
1380
Adam Langleyf18e4532015-07-10 13:39:53 -07001381/* Application-level Protocol Negotiation.
1382 *
1383 * https://tools.ietf.org/html/rfc7301 */
1384
David Benjamin8c880a22016-12-03 02:20:34 -05001385static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1386 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001387 if (ssl->alpn_client_proto_list == NULL ||
1388 ssl->s3->initial_handshake_complete) {
1389 return 1;
1390 }
1391
1392 CBB contents, proto_list;
1393 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1394 !CBB_add_u16_length_prefixed(out, &contents) ||
1395 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1396 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1397 ssl->alpn_client_proto_list_len) ||
1398 !CBB_flush(out)) {
1399 return 0;
1400 }
1401
1402 return 1;
1403}
1404
David Benjamin8c880a22016-12-03 02:20:34 -05001405static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001406 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001407 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001408 if (contents == NULL) {
1409 return 1;
1410 }
1411
1412 assert(!ssl->s3->initial_handshake_complete);
1413 assert(ssl->alpn_client_proto_list != NULL);
1414
David Benjamin8c880a22016-12-03 02:20:34 -05001415 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001416 /* NPN and ALPN may not be negotiated in the same connection. */
1417 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1418 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1419 return 0;
1420 }
1421
Adam Langleyf18e4532015-07-10 13:39:53 -07001422 /* The extension data consists of a ProtocolNameList which must have
1423 * exactly one ProtocolName. Each of these is length-prefixed. */
1424 CBS protocol_name_list, protocol_name;
1425 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1426 CBS_len(contents) != 0 ||
1427 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1428 /* Empty protocol names are forbidden. */
1429 CBS_len(&protocol_name) == 0 ||
1430 CBS_len(&protocol_name_list) != 0) {
1431 return 0;
1432 }
1433
David Benjamin3e517572016-08-11 11:52:23 -04001434 /* Check that the protcol name is one of the ones we advertised. */
1435 int protocol_ok = 0;
1436 CBS client_protocol_name_list, client_protocol_name;
1437 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1438 ssl->alpn_client_proto_list_len);
1439 while (CBS_len(&client_protocol_name_list) > 0) {
1440 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1441 &client_protocol_name)) {
1442 *out_alert = SSL_AD_INTERNAL_ERROR;
1443 return 0;
1444 }
1445
1446 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
David Benjamin17cf2cb2016-12-13 01:07:13 -05001447 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1448 CBS_data(&protocol_name),
1449 CBS_len(&protocol_name)) == 0) {
David Benjamin3e517572016-08-11 11:52:23 -04001450 protocol_ok = 1;
1451 break;
1452 }
1453 }
1454
1455 if (!protocol_ok) {
1456 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1457 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1458 return 0;
1459 }
1460
Adam Langleyf18e4532015-07-10 13:39:53 -07001461 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1462 &ssl->s3->alpn_selected_len)) {
1463 *out_alert = SSL_AD_INTERNAL_ERROR;
1464 return 0;
1465 }
1466
1467 return 1;
1468}
1469
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001470int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001471 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001472 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001473 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001474 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001475 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001476 client_hello, &contents,
1477 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1478 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001479 return 1;
1480 }
1481
1482 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001483 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001484
1485 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001486 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1487 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001488 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001489 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1490 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001491 return 0;
1492 }
1493
1494 /* Validate the protocol list. */
1495 CBS protocol_name_list_copy = protocol_name_list;
1496 while (CBS_len(&protocol_name_list_copy) > 0) {
1497 CBS protocol_name;
1498
1499 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1500 /* Empty protocol names are forbidden. */
1501 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001502 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1503 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001504 return 0;
1505 }
1506 }
1507
1508 const uint8_t *selected;
1509 uint8_t selected_len;
1510 if (ssl->ctx->alpn_select_cb(
1511 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1512 CBS_len(&protocol_name_list),
1513 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1514 OPENSSL_free(ssl->s3->alpn_selected);
1515 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1516 if (ssl->s3->alpn_selected == NULL) {
1517 *out_alert = SSL_AD_INTERNAL_ERROR;
1518 return 0;
1519 }
1520 ssl->s3->alpn_selected_len = selected_len;
1521 }
1522
1523 return 1;
1524}
1525
David Benjamin8c880a22016-12-03 02:20:34 -05001526static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1527 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001528 if (ssl->s3->alpn_selected == NULL) {
1529 return 1;
1530 }
1531
1532 CBB contents, proto_list, proto;
1533 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1534 !CBB_add_u16_length_prefixed(out, &contents) ||
1535 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1536 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001537 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1538 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001539 !CBB_flush(out)) {
1540 return 0;
1541 }
1542
1543 return 1;
1544}
1545
1546
Adam Langley49c7af12015-07-10 14:33:46 -07001547/* Channel ID.
1548 *
1549 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1550
David Benjamin8c880a22016-12-03 02:20:34 -05001551static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1552 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001553}
1554
David Benjamin8c880a22016-12-03 02:20:34 -05001555static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1556 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001557 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001558 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001559 return 1;
1560 }
1561
1562 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1563 !CBB_add_u16(out, 0 /* length */)) {
1564 return 0;
1565 }
1566
1567 return 1;
1568}
1569
David Benjamin8c880a22016-12-03 02:20:34 -05001570static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1571 uint8_t *out_alert, CBS *contents) {
1572 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001573 if (contents == NULL) {
1574 return 1;
1575 }
1576
David Benjamince079fd2016-08-02 16:22:34 -04001577 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001578 assert(ssl->tlsext_channel_id_enabled);
1579
1580 if (CBS_len(contents) != 0) {
1581 return 0;
1582 }
1583
1584 ssl->s3->tlsext_channel_id_valid = 1;
1585 return 1;
1586}
1587
David Benjamin8c880a22016-12-03 02:20:34 -05001588static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1589 uint8_t *out_alert, CBS *contents) {
1590 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001591 if (contents == NULL ||
1592 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001593 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001594 return 1;
1595 }
1596
1597 if (CBS_len(contents) != 0) {
1598 return 0;
1599 }
1600
1601 ssl->s3->tlsext_channel_id_valid = 1;
1602 return 1;
1603}
1604
David Benjamin8c880a22016-12-03 02:20:34 -05001605static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1606 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001607 if (!ssl->s3->tlsext_channel_id_valid) {
1608 return 1;
1609 }
1610
1611 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1612 !CBB_add_u16(out, 0 /* length */)) {
1613 return 0;
1614 }
1615
1616 return 1;
1617}
1618
Adam Langley391250d2015-07-15 19:06:07 -07001619
1620/* Secure Real-time Transport Protocol (SRTP) extension.
1621 *
1622 * https://tools.ietf.org/html/rfc5764 */
1623
Adam Langley391250d2015-07-15 19:06:07 -07001624
David Benjamin8c880a22016-12-03 02:20:34 -05001625static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1626 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001627}
1628
David Benjamin8c880a22016-12-03 02:20:34 -05001629static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1630 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001631 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1632 if (profiles == NULL) {
1633 return 1;
1634 }
1635 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1636 if (num_profiles == 0) {
1637 return 1;
1638 }
1639
1640 CBB contents, profile_ids;
1641 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1642 !CBB_add_u16_length_prefixed(out, &contents) ||
1643 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1644 return 0;
1645 }
1646
David Benjamin54091232016-09-05 12:47:25 -04001647 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001648 if (!CBB_add_u16(&profile_ids,
1649 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1650 return 0;
1651 }
1652 }
1653
1654 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1655 !CBB_flush(out)) {
1656 return 0;
1657 }
1658
1659 return 1;
1660}
1661
David Benjamin8c880a22016-12-03 02:20:34 -05001662static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001663 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001664 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001665 if (contents == NULL) {
1666 return 1;
1667 }
1668
1669 /* The extension consists of a u16-prefixed profile ID list containing a
1670 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1671 *
1672 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1673 CBS profile_ids, srtp_mki;
1674 uint16_t profile_id;
1675 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1676 !CBS_get_u16(&profile_ids, &profile_id) ||
1677 CBS_len(&profile_ids) != 0 ||
1678 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1679 CBS_len(contents) != 0) {
1680 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1681 return 0;
1682 }
1683
1684 if (CBS_len(&srtp_mki) != 0) {
1685 /* Must be no MKI, since we never offer one. */
1686 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1687 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1688 return 0;
1689 }
1690
1691 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1692
1693 /* Check to see if the server gave us something we support (and presumably
1694 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001695 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001696 const SRTP_PROTECTION_PROFILE *profile =
1697 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1698
1699 if (profile->id == profile_id) {
1700 ssl->srtp_profile = profile;
1701 return 1;
1702 }
1703 }
1704
1705 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1706 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1707 return 0;
1708}
1709
David Benjamin8c880a22016-12-03 02:20:34 -05001710static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001711 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001712 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001713 if (contents == NULL) {
1714 return 1;
1715 }
1716
1717 CBS profile_ids, srtp_mki;
1718 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1719 CBS_len(&profile_ids) < 2 ||
1720 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1721 CBS_len(contents) != 0) {
1722 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1723 return 0;
1724 }
1725 /* Discard the MKI value for now. */
1726
1727 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1728 SSL_get_srtp_profiles(ssl);
1729
1730 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001731 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001732 const SRTP_PROTECTION_PROFILE *server_profile =
1733 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1734
1735 CBS profile_ids_tmp;
1736 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1737
1738 while (CBS_len(&profile_ids_tmp) > 0) {
1739 uint16_t profile_id;
1740 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1741 return 0;
1742 }
1743
1744 if (server_profile->id == profile_id) {
1745 ssl->srtp_profile = server_profile;
1746 return 1;
1747 }
1748 }
1749 }
1750
1751 return 1;
1752}
1753
David Benjamin8c880a22016-12-03 02:20:34 -05001754static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1755 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001756 if (ssl->srtp_profile == NULL) {
1757 return 1;
1758 }
1759
1760 CBB contents, profile_ids;
1761 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1762 !CBB_add_u16_length_prefixed(out, &contents) ||
1763 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1764 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1765 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1766 !CBB_flush(out)) {
1767 return 0;
1768 }
1769
1770 return 1;
1771}
1772
Adam Langleybdd5d662015-07-20 16:19:08 -07001773
1774/* EC point formats.
1775 *
1776 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1777
David Benjamin8c880a22016-12-03 02:20:34 -05001778static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001779 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001780 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1781 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001782 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1783 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001784 !CBB_flush(out)) {
1785 return 0;
1786 }
1787
1788 return 1;
1789}
1790
David Benjamin8c880a22016-12-03 02:20:34 -05001791static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001792 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -05001793 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin70aba262016-11-01 12:08:15 -04001794 return 0;
1795 }
1796
1797 /* The point format extension is unneccessary in TLS 1.3. */
1798 if (min_version >= TLS1_3_VERSION) {
1799 return 1;
1800 }
1801
David Benjamin8c880a22016-12-03 02:20:34 -05001802 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001803}
1804
David Benjamin8c880a22016-12-03 02:20:34 -05001805static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001806 CBS *contents) {
1807 if (contents == NULL) {
1808 return 1;
1809 }
1810
David Benjamin8c880a22016-12-03 02:20:34 -05001811 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001812 return 0;
1813 }
1814
Adam Langleybdd5d662015-07-20 16:19:08 -07001815 CBS ec_point_format_list;
1816 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1817 CBS_len(contents) != 0) {
1818 return 0;
1819 }
1820
David Benjaminfc059942015-07-30 23:01:59 -04001821 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1822 * point format. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05001823 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1824 TLSEXT_ECPOINTFORMAT_uncompressed,
1825 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001826 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001827 return 0;
1828 }
1829
1830 return 1;
1831}
1832
David Benjamin8c880a22016-12-03 02:20:34 -05001833static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001834 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001835 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001836 return 1;
1837 }
1838
David Benjamin8c880a22016-12-03 02:20:34 -05001839 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001840}
1841
David Benjamin8c880a22016-12-03 02:20:34 -05001842static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1843 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001844 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1845 return 1;
1846 }
1847
David Benjamin45738dd2017-02-09 20:01:26 -05001848 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1849 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001850 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001851
1852 if (!using_ecc) {
1853 return 1;
1854 }
1855
David Benjamin8c880a22016-12-03 02:20:34 -05001856 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001857}
1858
Steven Valdeza833c352016-11-01 13:39:36 -04001859
Steven Valdez4aa154e2016-07-29 14:32:55 -04001860/* Pre Shared Key
1861 *
Steven Valdeza833c352016-11-01 13:39:36 -04001862 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001863
David Benjamin8c880a22016-12-03 02:20:34 -05001864static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1865 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001866 uint16_t min_version, max_version;
1867 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1868 return 0;
1869 }
1870
1871 uint16_t session_version;
1872 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1873 !ssl->method->version_from_wire(&session_version,
1874 ssl->session->ssl_version) ||
1875 session_version < TLS1_3_VERSION) {
1876 return 0;
1877 }
1878
Steven Valdez908ac192017-01-12 13:17:07 -05001879 const EVP_MD *digest = SSL_SESSION_get_digest(ssl->session, ssl);
1880 if (digest == NULL) {
1881 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1882 return 0;
1883 }
1884
Steven Valdeza833c352016-11-01 13:39:36 -04001885 size_t binder_len = EVP_MD_size(digest);
1886 return 15 + ssl->session->tlsext_ticklen + binder_len;
1887}
1888
David Benjamin8c880a22016-12-03 02:20:34 -05001889static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1890 SSL *const ssl = hs->ssl;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001891 uint16_t min_version, max_version;
1892 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1893 return 0;
1894 }
1895
David Benjamin2dc02042016-09-19 19:57:37 -04001896 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001897 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001898 !ssl->method->version_from_wire(&session_version,
1899 ssl->session->ssl_version) ||
1900 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001901 return 1;
1902 }
1903
David Benjaminad8f5e12017-02-20 17:00:20 -05001904 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001905 ssl_get_current_time(ssl, &now);
1906 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1907 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1908
1909 /* Fill in a placeholder zero binder of the appropriate length. It will be
1910 * computed and filled in later after length prefixes are computed. */
1911 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez908ac192017-01-12 13:17:07 -05001912
1913 const EVP_MD *digest = SSL_SESSION_get_digest(ssl->session, ssl);
1914 if (digest == NULL) {
1915 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1916 return 0;
1917 }
1918
Steven Valdeza833c352016-11-01 13:39:36 -04001919 size_t binder_len = EVP_MD_size(digest);
1920
1921 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001922 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1923 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001924 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001925 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1926 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001927 ssl->session->tlsext_ticklen) ||
1928 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1929 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1930 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1931 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001932 return 0;
1933 }
1934
David Benjamin8c880a22016-12-03 02:20:34 -05001935 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001936 return CBB_flush(out);
1937}
1938
David Benjamin8baf9632016-11-17 17:11:16 +09001939int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1940 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001941 CBS *contents) {
1942 uint16_t psk_id;
1943 if (!CBS_get_u16(contents, &psk_id) ||
1944 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001945 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001946 *out_alert = SSL_AD_DECODE_ERROR;
1947 return 0;
1948 }
1949
David Benjamineab773a2016-11-09 18:32:35 -05001950 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001951 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001952 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001953 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1954 return 0;
1955 }
1956
1957 return 1;
1958}
1959
David Benjamin35ac5b72017-03-03 15:05:56 -05001960int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001961 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001962 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04001963 /* We only process the first PSK identity since we don't support pure PSK. */
David Benjamin707af292017-03-10 17:47:18 -05001964 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001965 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001966 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001967 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001968 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001969 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001970 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001971 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001972 *out_alert = SSL_AD_DECODE_ERROR;
1973 return 0;
1974 }
1975
Steven Valdeza833c352016-11-01 13:39:36 -04001976 *out_binders = binders;
1977
David Benjaminaedf3032016-12-01 16:47:56 -05001978 /* Check the syntax of the remaining identities, but do not process them. */
1979 size_t num_identities = 1;
1980 while (CBS_len(&identities) != 0) {
1981 CBS unused_ticket;
1982 uint32_t unused_obfuscated_ticket_age;
1983 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1984 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1985 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1986 *out_alert = SSL_AD_DECODE_ERROR;
1987 return 0;
1988 }
1989
1990 num_identities++;
1991 }
1992
1993 /* Check the syntax of the binders. The value will be checked later if
1994 * resuming. */
1995 size_t num_binders = 0;
1996 while (CBS_len(&binders) != 0) {
1997 CBS binder;
1998 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1999 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2000 *out_alert = SSL_AD_DECODE_ERROR;
2001 return 0;
2002 }
2003
2004 num_binders++;
2005 }
2006
2007 if (num_identities != num_binders) {
2008 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
2009 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04002010 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04002011 }
2012
David Benjamine7f60a22016-11-16 18:54:25 +09002013 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002014}
2015
David Benjamin8baf9632016-11-17 17:11:16 +09002016int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2017 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002018 return 1;
2019 }
2020
2021 CBB contents;
2022 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2023 !CBB_add_u16_length_prefixed(out, &contents) ||
2024 /* We only consider the first identity for resumption */
2025 !CBB_add_u16(&contents, 0) ||
2026 !CBB_flush(out)) {
2027 return 0;
2028 }
2029
2030 return 1;
2031}
2032
2033
Steven Valdeza833c352016-11-01 13:39:36 -04002034/* Pre-Shared Key Exchange Modes
2035 *
2036 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002037
David Benjamin8c880a22016-12-03 02:20:34 -05002038static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2039 CBB *out) {
2040 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04002041 uint16_t min_version, max_version;
2042 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2043 return 0;
2044 }
2045
2046 if (max_version < TLS1_3_VERSION) {
2047 return 1;
2048 }
2049
2050 CBB contents, ke_modes;
2051 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2052 !CBB_add_u16_length_prefixed(out, &contents) ||
2053 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2054 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2055 return 0;
2056 }
2057
2058 return CBB_flush(out);
2059}
2060
David Benjamin8c880a22016-12-03 02:20:34 -05002061static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002062 uint8_t *out_alert,
2063 CBS *contents) {
2064 if (contents == NULL) {
2065 return 1;
2066 }
2067
Steven Valdeza833c352016-11-01 13:39:36 -04002068 CBS ke_modes;
2069 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2070 CBS_len(&ke_modes) == 0 ||
2071 CBS_len(contents) != 0) {
2072 *out_alert = SSL_AD_DECODE_ERROR;
2073 return 0;
2074 }
2075
2076 /* We only support tickets with PSK_DHE_KE. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05002077 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2078 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002079
2080 return 1;
2081}
2082
2083
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002084/* Early Data Indication
2085 *
2086 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2087
David Benjamin8c880a22016-12-03 02:20:34 -05002088static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002089 SSL *const ssl = hs->ssl;
2090 uint16_t session_version;
2091 if (ssl->session == NULL ||
2092 !ssl->method->version_from_wire(&session_version,
2093 ssl->session->ssl_version) ||
2094 session_version < TLS1_3_VERSION ||
2095 ssl->session->ticket_max_early_data == 0 ||
2096 hs->received_hello_retry_request ||
2097 !ssl->ctx->enable_early_data) {
2098 return 1;
2099 }
2100
2101 hs->early_data_offered = 1;
2102
2103 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2104 !CBB_add_u16(out, 0) ||
2105 !CBB_flush(out)) {
2106 return 0;
2107 }
2108
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002109 return 1;
2110}
2111
Steven Valdez2d850622017-01-11 11:34:52 -05002112static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002113 uint8_t *out_alert, CBS *contents) {
2114 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002115 if (contents == NULL) {
2116 return 1;
2117 }
2118
2119 if (CBS_len(contents) != 0) {
2120 *out_alert = SSL_AD_DECODE_ERROR;
2121 return 0;
2122 }
2123
Steven Valdez2d850622017-01-11 11:34:52 -05002124 if (!ssl->s3->session_reused) {
2125 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2126 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2127 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002128 }
Steven Valdez2d850622017-01-11 11:34:52 -05002129
2130 ssl->early_data_accepted = 1;
2131 return 1;
2132}
2133
2134static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2135 uint8_t *out_alert, CBS *contents) {
2136 SSL *const ssl = hs->ssl;
2137 if (contents == NULL ||
2138 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2139 return 1;
2140 }
2141
2142 if (CBS_len(contents) != 0) {
2143 *out_alert = SSL_AD_DECODE_ERROR;
2144 return 0;
2145 }
2146
2147 hs->early_data_offered = 1;
2148 return 1;
2149}
2150
2151static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2152 if (!hs->ssl->early_data_accepted) {
2153 return 1;
2154 }
2155
2156 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2157 !CBB_add_u16(out, 0) ||
2158 !CBB_flush(out)) {
2159 return 0;
2160 }
2161
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002162 return 1;
2163}
2164
2165
Steven Valdez143e8b32016-07-11 13:19:03 -04002166/* Key Share
2167 *
David Benjamina128a552016-10-13 14:26:33 -04002168 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002169
David Benjamin8c880a22016-12-03 02:20:34 -05002170static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2171 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002172 uint16_t min_version, max_version;
2173 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2174 return 0;
2175 }
2176
David Benjamin53a2dfc2016-10-07 21:04:12 -04002177 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002178 return 1;
2179 }
2180
2181 CBB contents, kse_bytes;
2182 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2183 !CBB_add_u16_length_prefixed(out, &contents) ||
2184 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2185 return 0;
2186 }
2187
David Benjamin8c880a22016-12-03 02:20:34 -05002188 uint16_t group_id = hs->retry_group;
2189 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002190 /* We received a HelloRetryRequest without a new curve, so there is no new
2191 * share to append. Leave |ecdh_ctx| as-is. */
2192 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002193 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2194 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002195 return 0;
2196 }
David Benjamin8c880a22016-12-03 02:20:34 -05002197 OPENSSL_free(hs->key_share_bytes);
2198 hs->key_share_bytes = NULL;
2199 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002200 if (group_id == 0) {
2201 return CBB_flush(out);
2202 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002203 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002204 /* Add a fake group. See draft-davidben-tls-grease-01. */
2205 if (ssl->ctx->grease_enabled &&
2206 (!CBB_add_u16(&kse_bytes,
2207 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2208 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2209 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2210 return 0;
2211 }
2212
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002213 /* Predict the most preferred group. */
2214 const uint16_t *groups;
2215 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002216 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002217 if (groups_len == 0) {
2218 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2219 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002220 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002221
2222 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002223 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002224
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002225 CBB key_exchange;
2226 if (!CBB_add_u16(&kse_bytes, group_id) ||
2227 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002228 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2229 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002230 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002231 return 0;
2232 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002233
David Benjamin8c880a22016-12-03 02:20:34 -05002234 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002235 /* Save the contents of the extension to repeat it in the second
2236 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002237 hs->key_share_bytes_len = CBB_len(&kse_bytes);
2238 hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
2239 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002240 return 0;
2241 }
2242 }
2243
Steven Valdez143e8b32016-07-11 13:19:03 -04002244 return CBB_flush(out);
2245}
2246
David Benjamin8baf9632016-11-17 17:11:16 +09002247int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002248 size_t *out_secret_len,
2249 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002250 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002251 uint16_t group_id;
2252 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002253 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2254 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002255 *out_alert = SSL_AD_DECODE_ERROR;
2256 return 0;
2257 }
2258
David Benjamin8baf9632016-11-17 17:11:16 +09002259 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002260 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2261 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2262 return 0;
2263 }
2264
David Benjamin8baf9632016-11-17 17:11:16 +09002265 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2266 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002267 *out_alert = SSL_AD_INTERNAL_ERROR;
2268 return 0;
2269 }
2270
David Benjamin45738dd2017-02-09 20:01:26 -05002271 hs->new_session->group_id = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002272 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002273 return 1;
2274}
2275
David Benjamin8baf9632016-11-17 17:11:16 +09002276int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002277 uint8_t **out_secret,
2278 size_t *out_secret_len,
2279 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002280 uint16_t group_id;
2281 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002282 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002283 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2284 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2285 return 0;
2286 }
2287
2288 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002289 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002290 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002291 return 0;
2292 }
2293
David Benjamin7e1f9842016-09-20 19:24:40 -04002294 /* Find the corresponding key share. */
2295 int found = 0;
2296 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002297 while (CBS_len(&key_shares) > 0) {
2298 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002299 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002300 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002301 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2302 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002303 return 0;
2304 }
2305
David Benjamin7e1f9842016-09-20 19:24:40 -04002306 if (id == group_id) {
2307 if (found) {
2308 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2309 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2310 return 0;
2311 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002312
David Benjamin7e1f9842016-09-20 19:24:40 -04002313 found = 1;
2314 peer_key = peer_key_tmp;
2315 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002316 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002317 }
2318
David Benjamin7e1f9842016-09-20 19:24:40 -04002319 if (!found) {
2320 *out_found = 0;
2321 *out_secret = NULL;
2322 *out_secret_len = 0;
2323 return 1;
2324 }
2325
2326 /* Compute the DH secret. */
2327 uint8_t *secret = NULL;
2328 size_t secret_len;
2329 SSL_ECDH_CTX group;
David Benjamin17cf2cb2016-12-13 01:07:13 -05002330 OPENSSL_memset(&group, 0, sizeof(SSL_ECDH_CTX));
David Benjamin7e1f9842016-09-20 19:24:40 -04002331 CBB public_key;
2332 if (!CBB_init(&public_key, 32) ||
2333 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002334 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
2335 CBS_data(&peer_key), CBS_len(&peer_key)) ||
2336 !CBB_finish(&public_key, &hs->public_key, &hs->public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002337 OPENSSL_free(secret);
2338 SSL_ECDH_CTX_cleanup(&group);
2339 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002340 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002341 return 0;
2342 }
2343
2344 SSL_ECDH_CTX_cleanup(&group);
2345
2346 *out_secret = secret;
2347 *out_secret_len = secret_len;
2348 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002349 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002350}
2351
David Benjamin8baf9632016-11-17 17:11:16 +09002352int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002353 uint16_t group_id;
2354 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002355 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002356 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2357 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2358 !CBB_add_u16(&kse_bytes, group_id) ||
2359 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002360 !CBB_add_bytes(&public_key, hs->public_key, hs->public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002361 !CBB_flush(out)) {
2362 return 0;
2363 }
2364
David Benjamin8baf9632016-11-17 17:11:16 +09002365 OPENSSL_free(hs->public_key);
2366 hs->public_key = NULL;
2367 hs->public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002368
David Benjamin45738dd2017-02-09 20:01:26 -05002369 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002370 return 1;
2371}
2372
2373
Steven Valdezfdd10992016-09-15 16:27:05 -04002374/* Supported Versions
2375 *
2376 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2377
David Benjamin8c880a22016-12-03 02:20:34 -05002378static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2379 SSL *const ssl = hs->ssl;
Steven Valdezfdd10992016-09-15 16:27:05 -04002380 uint16_t min_version, max_version;
2381 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2382 return 0;
2383 }
2384
2385 if (max_version <= TLS1_2_VERSION) {
2386 return 1;
2387 }
2388
2389 CBB contents, versions;
2390 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2391 !CBB_add_u16_length_prefixed(out, &contents) ||
2392 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2393 return 0;
2394 }
2395
David Benjamind9791bf2016-09-27 16:39:52 -04002396 /* Add a fake version. See draft-davidben-tls-grease-01. */
2397 if (ssl->ctx->grease_enabled &&
2398 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2399 return 0;
2400 }
2401
Steven Valdezfdd10992016-09-15 16:27:05 -04002402 for (uint16_t version = max_version; version >= min_version; version--) {
2403 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2404 return 0;
2405 }
2406 }
2407
2408 if (!CBB_flush(out)) {
2409 return 0;
2410 }
2411
2412 return 1;
2413}
2414
2415
David Benjamin3baa6e12016-10-07 21:10:38 -04002416/* Cookie
2417 *
2418 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2419
David Benjamin8c880a22016-12-03 02:20:34 -05002420static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2421 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002422 return 1;
2423 }
2424
2425 CBB contents, cookie;
2426 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2427 !CBB_add_u16_length_prefixed(out, &contents) ||
2428 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002429 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002430 !CBB_flush(out)) {
2431 return 0;
2432 }
2433
2434 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002435 OPENSSL_free(hs->cookie);
2436 hs->cookie = NULL;
2437 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002438 return 1;
2439}
2440
2441
Steven Valdezce902a92016-05-17 11:47:53 -04002442/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002443 *
Steven Valdezce902a92016-05-17 11:47:53 -04002444 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002445 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002446
David Benjamin8c880a22016-12-03 02:20:34 -05002447static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2448 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002449 CBB contents, groups_bytes;
2450 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002451 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002452 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002453 return 0;
2454 }
2455
David Benjamin65ac9972016-09-02 21:35:25 -04002456 /* Add a fake group. See draft-davidben-tls-grease-01. */
2457 if (ssl->ctx->grease_enabled &&
2458 !CBB_add_u16(&groups_bytes,
2459 ssl_get_grease_value(ssl, ssl_grease_group))) {
2460 return 0;
2461 }
2462
Steven Valdezce902a92016-05-17 11:47:53 -04002463 const uint16_t *groups;
2464 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002465 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002466
David Benjamin54091232016-09-05 12:47:25 -04002467 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002468 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002469 return 0;
2470 }
2471 }
2472
2473 return CBB_flush(out);
2474}
2475
David Benjamin8c880a22016-12-03 02:20:34 -05002476static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2477 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002478 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002479 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2480 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002481 return 1;
2482}
2483
David Benjamin8c880a22016-12-03 02:20:34 -05002484static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2485 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002486 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002487 if (contents == NULL) {
2488 return 1;
2489 }
2490
Steven Valdezce902a92016-05-17 11:47:53 -04002491 CBS supported_group_list;
2492 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2493 CBS_len(&supported_group_list) == 0 ||
2494 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002495 CBS_len(contents) != 0) {
2496 return 0;
2497 }
2498
David Benjamin8c880a22016-12-03 02:20:34 -05002499 hs->peer_supported_group_list =
2500 OPENSSL_malloc(CBS_len(&supported_group_list));
2501 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002502 *out_alert = SSL_AD_INTERNAL_ERROR;
2503 return 0;
2504 }
2505
Steven Valdezce902a92016-05-17 11:47:53 -04002506 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002507 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002508 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002509 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002510 goto err;
2511 }
2512 }
2513
Steven Valdezce902a92016-05-17 11:47:53 -04002514 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002515 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002516
2517 return 1;
2518
2519err:
David Benjamin8c880a22016-12-03 02:20:34 -05002520 OPENSSL_free(hs->peer_supported_group_list);
2521 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002522 *out_alert = SSL_AD_INTERNAL_ERROR;
2523 return 0;
2524}
2525
David Benjamin8c880a22016-12-03 02:20:34 -05002526static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002527 /* Servers don't echo this extension. */
2528 return 1;
2529}
2530
2531
Adam Langley614c66a2015-06-12 15:26:58 -07002532/* kExtensions contains all the supported extensions. */
2533static const struct tls_extension kExtensions[] = {
2534 {
Adam Langley5021b222015-06-12 18:27:58 -07002535 TLSEXT_TYPE_renegotiate,
2536 NULL,
2537 ext_ri_add_clienthello,
2538 ext_ri_parse_serverhello,
2539 ext_ri_parse_clienthello,
2540 ext_ri_add_serverhello,
2541 },
2542 {
Adam Langley614c66a2015-06-12 15:26:58 -07002543 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002544 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002545 ext_sni_add_clienthello,
2546 ext_sni_parse_serverhello,
2547 ext_sni_parse_clienthello,
2548 ext_sni_add_serverhello,
2549 },
Adam Langley0a056712015-07-01 15:03:33 -07002550 {
2551 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002552 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002553 ext_ems_add_clienthello,
2554 ext_ems_parse_serverhello,
2555 ext_ems_parse_clienthello,
2556 ext_ems_add_serverhello,
2557 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002558 {
2559 TLSEXT_TYPE_session_ticket,
2560 NULL,
2561 ext_ticket_add_clienthello,
2562 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002563 /* Ticket extension client parsing is handled in ssl_session.c */
2564 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002565 ext_ticket_add_serverhello,
2566 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002567 {
2568 TLSEXT_TYPE_signature_algorithms,
2569 NULL,
2570 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002571 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002572 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002573 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002574 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002575 {
2576 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002577 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002578 ext_ocsp_add_clienthello,
2579 ext_ocsp_parse_serverhello,
2580 ext_ocsp_parse_clienthello,
2581 ext_ocsp_add_serverhello,
2582 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002583 {
2584 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002585 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002586 ext_npn_add_clienthello,
2587 ext_npn_parse_serverhello,
2588 ext_npn_parse_clienthello,
2589 ext_npn_add_serverhello,
2590 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002591 {
2592 TLSEXT_TYPE_certificate_timestamp,
2593 NULL,
2594 ext_sct_add_clienthello,
2595 ext_sct_parse_serverhello,
2596 ext_sct_parse_clienthello,
2597 ext_sct_add_serverhello,
2598 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002599 {
2600 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002601 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002602 ext_alpn_add_clienthello,
2603 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002604 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2605 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002606 ext_alpn_add_serverhello,
2607 },
Adam Langley49c7af12015-07-10 14:33:46 -07002608 {
2609 TLSEXT_TYPE_channel_id,
2610 ext_channel_id_init,
2611 ext_channel_id_add_clienthello,
2612 ext_channel_id_parse_serverhello,
2613 ext_channel_id_parse_clienthello,
2614 ext_channel_id_add_serverhello,
2615 },
Adam Langley391250d2015-07-15 19:06:07 -07002616 {
2617 TLSEXT_TYPE_srtp,
2618 ext_srtp_init,
2619 ext_srtp_add_clienthello,
2620 ext_srtp_parse_serverhello,
2621 ext_srtp_parse_clienthello,
2622 ext_srtp_add_serverhello,
2623 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002624 {
2625 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002626 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002627 ext_ec_point_add_clienthello,
2628 ext_ec_point_parse_serverhello,
2629 ext_ec_point_parse_clienthello,
2630 ext_ec_point_add_serverhello,
2631 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002632 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002633 TLSEXT_TYPE_key_share,
2634 NULL,
2635 ext_key_share_add_clienthello,
2636 forbid_parse_serverhello,
2637 ignore_parse_clienthello,
2638 dont_add_serverhello,
2639 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002640 {
Steven Valdeza833c352016-11-01 13:39:36 -04002641 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002642 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002643 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002644 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002645 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002646 dont_add_serverhello,
2647 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002648 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002649 TLSEXT_TYPE_early_data,
2650 NULL,
2651 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002652 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002653 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002654 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002655 },
2656 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002657 TLSEXT_TYPE_supported_versions,
2658 NULL,
2659 ext_supported_versions_add_clienthello,
2660 forbid_parse_serverhello,
2661 ignore_parse_clienthello,
2662 dont_add_serverhello,
2663 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002664 {
2665 TLSEXT_TYPE_cookie,
2666 NULL,
2667 ext_cookie_add_clienthello,
2668 forbid_parse_serverhello,
2669 ignore_parse_clienthello,
2670 dont_add_serverhello,
2671 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002672 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2673 * intolerant to the last extension being zero-length. See
2674 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002675 {
Steven Valdezce902a92016-05-17 11:47:53 -04002676 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002677 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002678 ext_supported_groups_add_clienthello,
2679 ext_supported_groups_parse_serverhello,
2680 ext_supported_groups_parse_clienthello,
2681 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002682 },
Adam Langley614c66a2015-06-12 15:26:58 -07002683};
2684
2685#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2686
Adam Langley4cfa96b2015-07-01 11:56:55 -07002687OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002688 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002689 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002690OPENSSL_COMPILE_ASSERT(
2691 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2692 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002693
Adam Langley614c66a2015-06-12 15:26:58 -07002694static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2695 uint16_t value) {
2696 unsigned i;
2697 for (i = 0; i < kNumExtensions; i++) {
2698 if (kExtensions[i].value == value) {
2699 *out_index = i;
2700 return &kExtensions[i];
2701 }
2702 }
2703
2704 return NULL;
2705}
2706
Adam Langley09505632015-07-30 18:10:13 -07002707int SSL_extension_supported(unsigned extension_value) {
2708 uint32_t index;
2709 return extension_value == TLSEXT_TYPE_padding ||
2710 tls_extension_find(&index, extension_value) != NULL;
2711}
2712
David Benjamin8c880a22016-12-03 02:20:34 -05002713int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2714 SSL *const ssl = hs->ssl;
David Benjaminf04c2e92016-12-06 13:35:25 -05002715 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2716 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002717 !ssl->s3->send_connection_binding) {
2718 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002719 }
Adam Langley95c29f32014-06-20 12:00:00 -07002720
David Benjamine8d53502015-10-10 14:13:23 -04002721 CBB extensions;
2722 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002723 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002724 }
Adam Langley95c29f32014-06-20 12:00:00 -07002725
David Benjamin8c880a22016-12-03 02:20:34 -05002726 hs->extensions.sent = 0;
2727 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002728
David Benjamin54091232016-09-05 12:47:25 -04002729 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002730 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002731 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002732 }
2733 }
Adam Langley95c29f32014-06-20 12:00:00 -07002734
David Benjamin65ac9972016-09-02 21:35:25 -04002735 uint16_t grease_ext1 = 0;
2736 if (ssl->ctx->grease_enabled) {
2737 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2738 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2739 if (!CBB_add_u16(&extensions, grease_ext1) ||
2740 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2741 goto err;
2742 }
2743 }
2744
David Benjamin54091232016-09-05 12:47:25 -04002745 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002746 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002747 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002748 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002749 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002750 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002751 }
Adam Langley95c29f32014-06-20 12:00:00 -07002752
Adam Langley33ad2b52015-07-20 17:43:53 -07002753 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002754 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002755 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002756 }
Adam Langley75712922014-10-10 16:23:43 -07002757
David Benjamin2bd19172016-11-17 16:47:15 +09002758 if (!custom_ext_add_clienthello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002759 goto err;
2760 }
2761
David Benjamin65ac9972016-09-02 21:35:25 -04002762 if (ssl->ctx->grease_enabled) {
2763 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2764 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2765
2766 /* The two fake extensions must not have the same value. GREASE values are
2767 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2768 * one. */
2769 if (grease_ext1 == grease_ext2) {
2770 grease_ext2 ^= 0x1010;
2771 }
2772
2773 if (!CBB_add_u16(&extensions, grease_ext2) ||
2774 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2775 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2776 goto err;
2777 }
2778 }
2779
David Benjamince079fd2016-08-02 16:22:34 -04002780 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002781 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002782 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002783 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002784 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002785 *
2786 * NB: because this code works out the length of all existing extensions
2787 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002788 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002789 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002790 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002791 * Server 7.0 is intolerant to the last extension being zero-length. See
2792 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002793 if (padding_len >= 4 + 1) {
2794 padding_len -= 4;
2795 } else {
2796 padding_len = 1;
2797 }
Adam Langley95c29f32014-06-20 12:00:00 -07002798
Adam Langley33ad2b52015-07-20 17:43:53 -07002799 uint8_t *padding_bytes;
2800 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2801 !CBB_add_u16(&extensions, padding_len) ||
2802 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2803 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002804 }
Adam Langley75712922014-10-10 16:23:43 -07002805
David Benjamin17cf2cb2016-12-13 01:07:13 -05002806 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002807 }
2808 }
Adam Langley75712922014-10-10 16:23:43 -07002809
Steven Valdeza833c352016-11-01 13:39:36 -04002810 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002811 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002812 goto err;
2813 }
2814
David Benjamina01deee2015-12-08 18:56:31 -05002815 /* Discard empty extensions blocks. */
2816 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002817 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002818 }
2819
David Benjamine8d53502015-10-10 14:13:23 -04002820 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002821
2822err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002823 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002824 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002825}
Adam Langley95c29f32014-06-20 12:00:00 -07002826
David Benjamin8c880a22016-12-03 02:20:34 -05002827int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2828 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002829 CBB extensions;
2830 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002831 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002832 }
2833
David Benjamin8c880a22016-12-03 02:20:34 -05002834 for (unsigned i = 0; i < kNumExtensions; i++) {
2835 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002836 /* Don't send extensions that were not received. */
2837 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002838 }
Adam Langley95c29f32014-06-20 12:00:00 -07002839
David Benjamin8c880a22016-12-03 02:20:34 -05002840 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002841 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002842 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002843 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002844 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002845 }
Adam Langley95c29f32014-06-20 12:00:00 -07002846
David Benjamin2bd19172016-11-17 16:47:15 +09002847 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002848 goto err;
2849 }
2850
Steven Valdez143e8b32016-07-11 13:19:03 -04002851 /* Discard empty extensions blocks before TLS 1.3. */
2852 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2853 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002854 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002855 }
2856
David Benjamin56380462015-10-10 14:59:09 -04002857 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002858
2859err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002860 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002861 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002862}
Adam Langley95c29f32014-06-20 12:00:00 -07002863
David Benjamin731058e2016-12-03 23:15:13 -05002864static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2865 const SSL_CLIENT_HELLO *client_hello,
2866 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002867 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002868 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002869 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002870 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002871 }
2872 }
2873
David Benjamin8c880a22016-12-03 02:20:34 -05002874 hs->extensions.received = 0;
2875 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002876
David Benjamine14ff062016-08-09 16:21:24 -04002877 CBS extensions;
2878 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2879 while (CBS_len(&extensions) != 0) {
2880 uint16_t type;
2881 CBS extension;
2882
2883 /* Decode the next extension. */
2884 if (!CBS_get_u16(&extensions, &type) ||
2885 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002886 *out_alert = SSL_AD_DECODE_ERROR;
2887 return 0;
2888 }
Adam Langley95c29f32014-06-20 12:00:00 -07002889
David Benjamine14ff062016-08-09 16:21:24 -04002890 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2891 * ambiguous. Ignore all but the renegotiation_info extension. */
2892 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2893 continue;
2894 }
Adam Langley95c29f32014-06-20 12:00:00 -07002895
David Benjamine14ff062016-08-09 16:21:24 -04002896 unsigned ext_index;
2897 const struct tls_extension *const ext =
2898 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002899
David Benjamine14ff062016-08-09 16:21:24 -04002900 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002901 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002902 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002903 return 0;
2904 }
David Benjamine14ff062016-08-09 16:21:24 -04002905 continue;
2906 }
2907
David Benjamin8c880a22016-12-03 02:20:34 -05002908 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002909 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002910 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002911 *out_alert = alert;
2912 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002913 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002914 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002915 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002916 }
Adam Langley75712922014-10-10 16:23:43 -07002917
David Benjamin1deb41b2016-08-09 19:36:38 -04002918 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002919 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002920 continue;
2921 }
2922
2923 CBS *contents = NULL, fake_contents;
2924 static const uint8_t kFakeRenegotiateExtension[] = {0};
2925 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2926 ssl_client_cipher_list_contains_cipher(client_hello,
2927 SSL3_CK_SCSV & 0xffff)) {
2928 /* The renegotiation SCSV was received so pretend that we received a
2929 * renegotiation extension. */
2930 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2931 sizeof(kFakeRenegotiateExtension));
2932 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002933 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002934 }
2935
2936 /* Extension wasn't observed so call the callback with a NULL
2937 * parameter. */
2938 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002939 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002940 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002941 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002942 *out_alert = alert;
2943 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002944 }
2945 }
2946
Adam Langleyfcf25832014-12-18 17:42:32 -08002947 return 1;
2948}
Adam Langley95c29f32014-06-20 12:00:00 -07002949
David Benjamin731058e2016-12-03 23:15:13 -05002950int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2951 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002952 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002953 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002954 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002955 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002956 return 0;
2957 }
Adam Langley95c29f32014-06-20 12:00:00 -07002958
David Benjamin8c880a22016-12-03 02:20:34 -05002959 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002960 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002961 return 0;
2962 }
Adam Langley95c29f32014-06-20 12:00:00 -07002963
Adam Langleyfcf25832014-12-18 17:42:32 -08002964 return 1;
2965}
Adam Langley95c29f32014-06-20 12:00:00 -07002966
David Benjamin8c880a22016-12-03 02:20:34 -05002967static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2968 int *out_alert) {
2969 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002970 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2971 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2972 return 1;
2973 }
Adam Langley614c66a2015-06-12 15:26:58 -07002974
Steven Valdez143e8b32016-07-11 13:19:03 -04002975 /* Decode the extensions block and check it is valid. */
2976 CBS extensions;
2977 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2978 !tls1_check_duplicate_extensions(&extensions)) {
2979 *out_alert = SSL_AD_DECODE_ERROR;
2980 return 0;
2981 }
2982
2983 uint32_t received = 0;
2984 while (CBS_len(&extensions) != 0) {
2985 uint16_t type;
2986 CBS extension;
2987
2988 /* Decode the next extension. */
2989 if (!CBS_get_u16(&extensions, &type) ||
2990 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002991 *out_alert = SSL_AD_DECODE_ERROR;
2992 return 0;
2993 }
Adam Langley95c29f32014-06-20 12:00:00 -07002994
Steven Valdez143e8b32016-07-11 13:19:03 -04002995 unsigned ext_index;
2996 const struct tls_extension *const ext =
2997 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002998
Steven Valdez143e8b32016-07-11 13:19:03 -04002999 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09003000 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003001 return 0;
3002 }
Steven Valdez143e8b32016-07-11 13:19:03 -04003003 continue;
3004 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003005
David Benjamin5db7c9b2017-01-24 16:17:03 -05003006 OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
3007 too_many_bits);
3008
David Benjamin8c880a22016-12-03 02:20:34 -05003009 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04003010 type != TLSEXT_TYPE_renegotiate) {
3011 /* If the extension was never sent then it is illegal, except for the
3012 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04003013 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3014 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04003015 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04003016 return 0;
3017 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003018
Steven Valdez143e8b32016-07-11 13:19:03 -04003019 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07003020
Steven Valdez143e8b32016-07-11 13:19:03 -04003021 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003022 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04003023 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003024 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04003025 *out_alert = alert;
3026 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07003027 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003028 }
Adam Langley95c29f32014-06-20 12:00:00 -07003029
David Benjamin54091232016-09-05 12:47:25 -04003030 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003031 if (!(received & (1u << i))) {
3032 /* Extension wasn't observed so call the callback with a NULL
3033 * parameter. */
3034 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003035 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003036 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003037 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07003038 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08003039 return 0;
3040 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003041 }
3042 }
Adam Langley95c29f32014-06-20 12:00:00 -07003043
Adam Langleyfcf25832014-12-18 17:42:32 -08003044 return 1;
3045}
Adam Langley95c29f32014-06-20 12:00:00 -07003046
David Benjamin8c880a22016-12-03 02:20:34 -05003047static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3048 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003049 int ret = SSL_TLSEXT_ERR_NOACK;
3050 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003051
David Benjamin78f8aab2016-03-10 16:33:58 -05003052 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003053 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003054 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05003055 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
3056 ret = ssl->session_ctx->tlsext_servername_callback(
3057 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003058 }
Adam Langley95c29f32014-06-20 12:00:00 -07003059
Adam Langleyfcf25832014-12-18 17:42:32 -08003060 switch (ret) {
3061 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003062 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003063 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003064
Adam Langleyfcf25832014-12-18 17:42:32 -08003065 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003066 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003067 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003068
Adam Langleyfcf25832014-12-18 17:42:32 -08003069 default:
3070 return 1;
3071 }
3072}
Adam Langleyed8270a2014-09-02 13:52:56 -07003073
David Benjamin8c880a22016-12-03 02:20:34 -05003074int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3075 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08003076 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003077 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003078 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003079 return 0;
3080 }
3081
Adam Langleyfcf25832014-12-18 17:42:32 -08003082 return 1;
3083}
Adam Langley95c29f32014-06-20 12:00:00 -07003084
Adam Langley4c341d02017-03-08 19:33:21 -08003085static enum ssl_ticket_aead_result_t
3086ssl_decrypt_ticket_with_cipher_ctx(SSL *ssl, uint8_t **out, size_t *out_len,
3087 int *out_renew_ticket, const uint8_t *ticket,
3088 size_t ticket_len) {
3089 enum ssl_ticket_aead_result_t ret = ssl_ticket_aead_ignore_ticket;
3090 const SSL_CTX *const ssl_ctx = ssl->session_ctx;
David Benjamine3aa1d92015-06-16 15:34:50 -04003091 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003092
David Benjamine3aa1d92015-06-16 15:34:50 -04003093 HMAC_CTX hmac_ctx;
3094 HMAC_CTX_init(&hmac_ctx);
3095 EVP_CIPHER_CTX cipher_ctx;
3096 EVP_CIPHER_CTX_init(&cipher_ctx);
3097
David Benjaminadcc3952015-04-26 13:07:57 -04003098 /* Ensure there is room for the key name and the largest IV
3099 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3100 * the maximum IV length should be well under the minimum size for the
3101 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003102 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
Adam Langley4c341d02017-03-08 19:33:21 -08003103 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003104 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003105 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003106
David Benjamine3aa1d92015-06-16 15:34:50 -04003107 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003108 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3109 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3110 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003111 if (cb_ret < 0) {
Adam Langley4c341d02017-03-08 19:33:21 -08003112 ret = ssl_ticket_aead_error;
3113 goto out;
3114 } else if (cb_ret == 0) {
3115 goto out;
3116 } else if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003117 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003118 }
3119 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003120 /* Check the key name matches. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003121 if (OPENSSL_memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3122 SSL_TICKET_KEY_NAME_LEN) != 0) {
Adam Langley4c341d02017-03-08 19:33:21 -08003123 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003124 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003125 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3126 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003127 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003128 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3129 ssl_ctx->tlsext_tick_aes_key, iv)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003130 ret = ssl_ticket_aead_error;
3131 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003132 }
3133 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003134 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003135
David Benjamine3aa1d92015-06-16 15:34:50 -04003136 /* Check the MAC at the end of the ticket. */
3137 uint8_t mac[EVP_MAX_MD_SIZE];
3138 size_t mac_len = HMAC_size(&hmac_ctx);
3139 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003140 /* The ticket must be large enough for key name, IV, data, and MAC. */
Adam Langley4c341d02017-03-08 19:33:21 -08003141 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003142 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003143 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3144 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003145 int mac_ok =
3146 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3147#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3148 mac_ok = 1;
3149#endif
3150 if (!mac_ok) {
Adam Langley4c341d02017-03-08 19:33:21 -08003151 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003152 }
3153
David Benjamine3aa1d92015-06-16 15:34:50 -04003154 /* Decrypt the session data. */
3155 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3156 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3157 mac_len;
3158 plaintext = OPENSSL_malloc(ciphertext_len);
3159 if (plaintext == NULL) {
Adam Langley4c341d02017-03-08 19:33:21 -08003160 ret = ssl_ticket_aead_error;
3161 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003162 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003163 size_t plaintext_len;
3164#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin17cf2cb2016-12-13 01:07:13 -05003165 OPENSSL_memcpy(plaintext, ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003166 plaintext_len = ciphertext_len;
3167#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003168 if (ciphertext_len >= INT_MAX) {
Adam Langley4c341d02017-03-08 19:33:21 -08003169 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003170 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003171 int len1, len2;
3172 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3173 (int)ciphertext_len) ||
3174 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003175 ERR_clear_error();
3176 goto out;
Adam Langleyfcf25832014-12-18 17:42:32 -08003177 }
Adam Langley4c341d02017-03-08 19:33:21 -08003178 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04003179#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003180
Adam Langley4c341d02017-03-08 19:33:21 -08003181 *out = plaintext;
3182 plaintext = NULL;
3183 *out_len = plaintext_len;
3184 ret = ssl_ticket_aead_success;
3185
3186out:
3187 OPENSSL_free(plaintext);
3188 HMAC_CTX_cleanup(&hmac_ctx);
3189 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3190 return ret;
3191}
3192
3193static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
3194 SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
3195 const uint8_t *ticket, size_t ticket_len) {
3196 uint8_t *plaintext = OPENSSL_malloc(ticket_len);
3197 if (plaintext == NULL) {
3198 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3199 return ssl_ticket_aead_error;
3200 }
3201
3202 size_t plaintext_len;
3203 const enum ssl_ticket_aead_result_t result =
3204 ssl->session_ctx->ticket_aead_method->open(
3205 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3206
3207 if (result == ssl_ticket_aead_success) {
3208 *out = plaintext;
3209 plaintext = NULL;
3210 *out_len = plaintext_len;
3211 }
3212
3213 OPENSSL_free(plaintext);
3214 return result;
3215}
3216
3217enum ssl_ticket_aead_result_t ssl_process_ticket(
3218 SSL *ssl, SSL_SESSION **out_session, int *out_renew_ticket,
3219 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3220 size_t session_id_len) {
3221 *out_renew_ticket = 0;
3222 *out_session = NULL;
3223
3224 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3225 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3226 return ssl_ticket_aead_ignore_ticket;
3227 }
3228
3229 uint8_t *plaintext = NULL;
3230 size_t plaintext_len;
3231 enum ssl_ticket_aead_result_t result;
3232 if (ssl->session_ctx->ticket_aead_method != NULL) {
3233 result = ssl_decrypt_ticket_with_method(
3234 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3235 } else {
3236 result = ssl_decrypt_ticket_with_cipher_ctx(
3237 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3238 }
3239
3240 if (result != ssl_ticket_aead_success) {
3241 return result;
3242 }
3243
David Benjamine3aa1d92015-06-16 15:34:50 -04003244 /* Decode the session. */
Adam Langley46db7af2017-02-01 15:49:37 -08003245 SSL_SESSION *session =
3246 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx);
Adam Langley4c341d02017-03-08 19:33:21 -08003247 OPENSSL_free(plaintext);
3248
David Benjamine3aa1d92015-06-16 15:34:50 -04003249 if (session == NULL) {
3250 ERR_clear_error(); /* Don't leave an error on the queue. */
Adam Langley4c341d02017-03-08 19:33:21 -08003251 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003252 }
3253
3254 /* Copy the client's session ID into the new session, to denote the ticket has
3255 * been accepted. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003256 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003257 session->session_id_length = session_id_len;
3258
3259 *out_session = session;
Adam Langley4c341d02017-03-08 19:33:21 -08003260 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003261}
Adam Langley95c29f32014-06-20 12:00:00 -07003262
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003263int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003264 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003265 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003266 return 1;
3267 }
David Benjamincd996942014-07-20 16:23:51 -04003268
David Benjamin0fc37ef2016-08-17 15:29:46 -04003269 OPENSSL_free(hs->peer_sigalgs);
3270 hs->peer_sigalgs = NULL;
3271 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003272
3273 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003274 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003275 return 0;
3276 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003277 num_sigalgs /= 2;
3278
3279 /* supported_signature_algorithms in the certificate request is
3280 * allowed to be empty. */
3281 if (num_sigalgs == 0) {
3282 return 1;
3283 }
3284
Steven Valdez02563852016-06-23 13:33:05 -04003285 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3286 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003287 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3288 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003289 return 0;
3290 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003291 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003292
3293 CBS sigalgs;
3294 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003295 for (size_t i = 0; i < num_sigalgs; i++) {
3296 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003297 return 0;
3298 }
3299 }
Adam Langley95c29f32014-06-20 12:00:00 -07003300
Adam Langleyfcf25832014-12-18 17:42:32 -08003301 return 1;
3302}
David Benjaminec2f27d2014-11-13 19:17:25 -05003303
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003304int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3305 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003306 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003307
Steven Valdezf0451ca2016-06-29 13:16:27 -04003308 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3309 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3310 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003311 int type = ssl_private_key_type(ssl);
3312 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003313 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003314 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003315 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003316 if (ssl_is_ecdsa_key_type(type)) {
3317 *out = SSL_SIGN_ECDSA_SHA1;
3318 return 1;
3319 }
3320 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3321 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003322 }
3323
David Benjamin3ef76972016-10-17 17:59:54 -04003324 const uint16_t *sigalgs = cert->sigalgs;
3325 size_t num_sigalgs = cert->num_sigalgs;
3326 if (sigalgs == NULL) {
3327 sigalgs = kSignSignatureAlgorithms;
3328 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003329 }
3330
David Benjamin0fc37ef2016-08-17 15:29:46 -04003331 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3332 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3333 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003334 /* If the client didn't specify any signature_algorithms extension then
3335 * we can assume that it supports SHA1. See
3336 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3337 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3338 SSL_SIGN_ECDSA_SHA1};
3339 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003340 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003341 }
3342
David Benjamin0fc37ef2016-08-17 15:29:46 -04003343 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003344 uint16_t sigalg = sigalgs[i];
3345 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3346 * negotiated. */
3347 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3348 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3349 continue;
3350 }
3351
David Benjamin0fc37ef2016-08-17 15:29:46 -04003352 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003353 if (sigalg == peer_sigalgs[j]) {
3354 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003355 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003356 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003357 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003358 }
Adam Langley95c29f32014-06-20 12:00:00 -07003359
David Benjaminea9a0d52016-07-08 15:52:59 -07003360 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3361 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003362}
Adam Langley95c29f32014-06-20 12:00:00 -07003363
Steven Valdez908ac192017-01-12 13:17:07 -05003364int tls1_verify_channel_id(SSL_HANDSHAKE *hs) {
3365 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003366 int ret = 0;
3367 uint16_t extension_type;
3368 CBS extension, channel_id;
3369
3370 /* A Channel ID handshake message is structured to contain multiple
3371 * extensions, but the only one that can be present is Channel ID. */
3372 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3373 if (!CBS_get_u16(&channel_id, &extension_type) ||
3374 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3375 CBS_len(&channel_id) != 0 ||
3376 extension_type != TLSEXT_TYPE_channel_id ||
3377 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3378 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3379 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3380 return 0;
3381 }
3382
3383 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3384 if (!p256) {
3385 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3386 return 0;
3387 }
3388
3389 EC_KEY *key = NULL;
3390 EC_POINT *point = NULL;
3391 BIGNUM x, y;
3392 ECDSA_SIG sig;
3393 BN_init(&x);
3394 BN_init(&y);
3395 sig.r = BN_new();
3396 sig.s = BN_new();
3397 if (sig.r == NULL || sig.s == NULL) {
3398 goto err;
3399 }
3400
3401 const uint8_t *p = CBS_data(&extension);
3402 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3403 BN_bin2bn(p + 32, 32, &y) == NULL ||
3404 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3405 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3406 goto err;
3407 }
3408
3409 point = EC_POINT_new(p256);
3410 if (point == NULL ||
3411 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3412 goto err;
3413 }
3414
3415 key = EC_KEY_new();
3416 if (key == NULL ||
3417 !EC_KEY_set_group(key, p256) ||
3418 !EC_KEY_set_public_key(key, point)) {
3419 goto err;
3420 }
3421
3422 uint8_t digest[EVP_MAX_MD_SIZE];
3423 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003424 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003425 goto err;
3426 }
3427
3428 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3429#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3430 sig_ok = 1;
3431#endif
3432 if (!sig_ok) {
3433 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3434 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3435 ssl->s3->tlsext_channel_id_valid = 0;
3436 goto err;
3437 }
3438
David Benjamin17cf2cb2016-12-13 01:07:13 -05003439 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
Nick Harper60a85cb2016-09-23 16:25:11 -07003440 ret = 1;
3441
3442err:
3443 BN_free(&x);
3444 BN_free(&y);
3445 BN_free(sig.r);
3446 BN_free(sig.s);
3447 EC_KEY_free(key);
3448 EC_POINT_free(point);
3449 EC_GROUP_free(p256);
3450 return ret;
3451}
3452
Steven Valdez908ac192017-01-12 13:17:07 -05003453int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3454 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003455 uint8_t digest[EVP_MAX_MD_SIZE];
3456 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003457 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003458 return 0;
3459 }
3460
3461 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3462 if (ec_key == NULL) {
3463 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3464 return 0;
3465 }
3466
3467 int ret = 0;
3468 BIGNUM *x = BN_new();
3469 BIGNUM *y = BN_new();
3470 ECDSA_SIG *sig = NULL;
3471 if (x == NULL || y == NULL ||
3472 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3473 EC_KEY_get0_public_key(ec_key),
3474 x, y, NULL)) {
3475 goto err;
3476 }
3477
3478 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3479 if (sig == NULL) {
3480 goto err;
3481 }
3482
3483 CBB child;
3484 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3485 !CBB_add_u16_length_prefixed(cbb, &child) ||
3486 !BN_bn2cbb_padded(&child, 32, x) ||
3487 !BN_bn2cbb_padded(&child, 32, y) ||
3488 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3489 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3490 !CBB_flush(cbb)) {
3491 goto err;
3492 }
3493
3494 ret = 1;
3495
3496err:
3497 BN_free(x);
3498 BN_free(y);
3499 ECDSA_SIG_free(sig);
3500 return ret;
3501}
3502
Steven Valdez908ac192017-01-12 13:17:07 -05003503int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3504 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003505 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3506 uint8_t *msg;
3507 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003508 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003509 ssl_cert_verify_channel_id)) {
3510 return 0;
3511 }
3512 SHA256(msg, msg_len, out);
3513 *out_len = SHA256_DIGEST_LENGTH;
3514 OPENSSL_free(msg);
3515 return 1;
3516 }
3517
Nick Harper95594012016-10-20 14:07:13 -07003518 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003519
Nick Harper95594012016-10-20 14:07:13 -07003520 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003521 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003522 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003523
Steven Valdez87eab492016-06-27 16:34:59 -04003524 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003525 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003526 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003527 if (ssl->session->original_handshake_hash_len == 0) {
3528 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003529 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003530 }
Nick Harper95594012016-10-20 14:07:13 -07003531 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3532 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003533 }
3534
Steven Valdez908ac192017-01-12 13:17:07 -05003535 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3536 size_t hs_hash_len;
3537 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript, hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003538 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003539 }
Steven Valdez908ac192017-01-12 13:17:07 -05003540 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003541 SHA256_Final(out, &ctx);
3542 *out_len = SHA256_DIGEST_LENGTH;
3543 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003544}
Adam Langley1258b6a2014-06-20 12:00:00 -07003545
3546/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin45738dd2017-02-09 20:01:26 -05003547 * hashes in |hs->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003548 * data. */
Steven Valdez908ac192017-01-12 13:17:07 -05003549int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3550 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003551 /* This function should never be called for a resumed session because the
3552 * handshake hashes that we wish to record are for the original, full
3553 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003554 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003555 return -1;
3556 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003557
Steven Valdez908ac192017-01-12 13:17:07 -05003558 OPENSSL_COMPILE_ASSERT(
David Benjamin45738dd2017-02-09 20:01:26 -05003559 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
Steven Valdez908ac192017-01-12 13:17:07 -05003560 original_handshake_hash_is_too_small);
3561
3562 size_t digest_len;
3563 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript,
David Benjamin45738dd2017-02-09 20:01:26 -05003564 hs->new_session->original_handshake_hash,
Steven Valdez908ac192017-01-12 13:17:07 -05003565 &digest_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003566 return -1;
3567 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003568
Steven Valdez908ac192017-01-12 13:17:07 -05003569 OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE <= 0xff, max_md_size_is_too_large);
David Benjamin45738dd2017-02-09 20:01:26 -05003570 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003571
Adam Langleyfcf25832014-12-18 17:42:32 -08003572 return 1;
3573}
Nick Harper60a85cb2016-09-23 16:25:11 -07003574
3575int ssl_do_channel_id_callback(SSL *ssl) {
3576 if (ssl->tlsext_channel_id_private != NULL ||
3577 ssl->ctx->channel_id_cb == NULL) {
3578 return 1;
3579 }
3580
3581 EVP_PKEY *key = NULL;
3582 ssl->ctx->channel_id_cb(ssl, &key);
3583 if (key == NULL) {
3584 /* The caller should try again later. */
3585 return 1;
3586 }
3587
3588 int ret = SSL_set1_tls_channel_id(ssl, key);
3589 EVP_PKEY_free(key);
3590 return ret;
3591}
Adam Langleycfa08c32016-11-17 13:21:27 -08003592
3593int ssl_is_sct_list_valid(const CBS *contents) {
3594 /* Shallow parse the SCT list for sanity. By the RFC
3595 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3596 * of the SCTs may be empty. */
3597 CBS copy = *contents;
3598 CBS sct_list;
3599 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3600 CBS_len(&copy) != 0 ||
3601 CBS_len(&sct_list) == 0) {
3602 return 0;
3603 }
3604
3605 while (CBS_len(&sct_list) > 0) {
3606 CBS sct;
3607 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3608 CBS_len(&sct) == 0) {
3609 return 0;
3610 }
3611 }
3612
3613 return 1;
3614}