blob: ec5dce0efaf2ad686f07bf426aa43a0c151ab63a [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
530/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
531 * supported or doesn't appear in supported signature algorithms. Unlike
532 * ssl_cipher_get_disabled this applies to a specific session and not global
533 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500534void ssl_set_client_disabled(SSL *ssl) {
535 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800536 int have_rsa = 0, have_ecdsa = 0;
537 c->mask_a = 0;
538 c->mask_k = 0;
539
David Benjamin3ef76972016-10-17 17:59:54 -0400540 /* Now go through all signature algorithms seeing if we support any for RSA or
541 * ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin0fc37ef2016-08-17 15:29:46 -0400542 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -0400543 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
David Benjamin0fc37ef2016-08-17 15:29:46 -0400544 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -0400545 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400546 case SSL_SIGN_RSA_PSS_SHA512:
547 case SSL_SIGN_RSA_PSS_SHA384:
548 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400549 case SSL_SIGN_RSA_PKCS1_SHA512:
550 case SSL_SIGN_RSA_PKCS1_SHA384:
551 case SSL_SIGN_RSA_PKCS1_SHA256:
552 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 have_rsa = 1;
554 break;
555
Steven Valdez02563852016-06-23 13:33:05 -0400556 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
557 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
558 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
559 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800560 have_ecdsa = 1;
561 break;
562 }
563 }
564
565 /* Disable auth if we don't include any appropriate signature algorithms. */
566 if (!have_rsa) {
567 c->mask_a |= SSL_aRSA;
568 }
569 if (!have_ecdsa) {
570 c->mask_a |= SSL_aECDSA;
571 }
572
573 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500574 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800575 c->mask_a |= SSL_aPSK;
576 c->mask_k |= SSL_kPSK;
577 }
578}
Adam Langley95c29f32014-06-20 12:00:00 -0700579
Adam Langley614c66a2015-06-12 15:26:58 -0700580/* tls_extension represents a TLS extension that is handled internally. The
581 * |init| function is called for each handshake, before any other functions of
582 * the extension. Then the add and parse callbacks are called as needed.
583 *
584 * The parse callbacks receive a |CBS| that contains the contents of the
585 * extension (i.e. not including the type and length bytes). If an extension is
586 * not received then the parse callbacks will be called with a NULL CBS so that
587 * they can do any processing needed to handle the absence of an extension.
588 *
589 * The add callbacks receive a |CBB| to which the extension can be appended but
590 * the function is responsible for appending the type and length bytes too.
591 *
592 * All callbacks return one for success and zero for error. If a parse function
593 * returns zero then a fatal alert with value |*out_alert| will be sent. If
594 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
595struct tls_extension {
596 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500597 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700598
David Benjamin8c880a22016-12-03 02:20:34 -0500599 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
600 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
601 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700602
David Benjamin8c880a22016-12-03 02:20:34 -0500603 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
604 CBS *contents);
605 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700606};
607
David Benjamin8c880a22016-12-03 02:20:34 -0500608static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
609 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400610 if (contents != NULL) {
611 /* Servers MUST NOT send this extension. */
612 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
613 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
614 return 0;
615 }
616
617 return 1;
618}
619
David Benjamin8c880a22016-12-03 02:20:34 -0500620static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
621 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400622 /* This extension from the client is handled elsewhere. */
623 return 1;
624}
625
David Benjamin8c880a22016-12-03 02:20:34 -0500626static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400627 return 1;
628}
Adam Langley614c66a2015-06-12 15:26:58 -0700629
630/* Server name indication (SNI).
631 *
632 * https://tools.ietf.org/html/rfc6066#section-3. */
633
David Benjamin8c880a22016-12-03 02:20:34 -0500634static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
635 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700636 if (ssl->tlsext_hostname == NULL) {
637 return 1;
638 }
639
640 CBB contents, server_name_list, name;
641 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
642 !CBB_add_u16_length_prefixed(out, &contents) ||
643 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
644 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
645 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
646 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
647 strlen(ssl->tlsext_hostname)) ||
648 !CBB_flush(out)) {
649 return 0;
650 }
651
652 return 1;
653}
654
David Benjamin8c880a22016-12-03 02:20:34 -0500655static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500656 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500657 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700658 if (contents == NULL) {
659 return 1;
660 }
661
662 if (CBS_len(contents) != 0) {
663 return 0;
664 }
665
666 assert(ssl->tlsext_hostname != NULL);
667
Steven Valdez87eab492016-06-27 16:34:59 -0400668 if (ssl->session == NULL) {
669 assert(ssl->s3->new_session->tlsext_hostname == NULL);
670 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
671 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700672 *out_alert = SSL_AD_INTERNAL_ERROR;
673 return 0;
674 }
675 }
676
677 return 1;
678}
679
David Benjamin8c880a22016-12-03 02:20:34 -0500680static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500681 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700682 if (contents == NULL) {
683 return 1;
684 }
685
David Benjamin9b611e22016-03-03 08:48:30 -0500686 CBS server_name_list, host_name;
687 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700688 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500689 !CBS_get_u8(&server_name_list, &name_type) ||
690 /* Although the server_name extension was intended to be extensible to
691 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
692 * different name types will cause an error. Further, RFC 4366 originally
693 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
694 * adding new name types is no longer feasible.
695 *
696 * Act as if the extensibility does not exist to simplify parsing. */
697 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
698 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700699 CBS_len(contents) != 0) {
700 return 0;
701 }
702
David Benjamin9b611e22016-03-03 08:48:30 -0500703 if (name_type != TLSEXT_NAMETYPE_host_name ||
704 CBS_len(&host_name) == 0 ||
705 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
706 CBS_contains_zero_byte(&host_name)) {
707 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
708 return 0;
709 }
Adam Langley614c66a2015-06-12 15:26:58 -0700710
David Benjamin4eb95cc2016-11-16 17:08:23 +0900711 /* Copy the hostname as a string. */
David Benjamin8c880a22016-12-03 02:20:34 -0500712 if (!CBS_strdup(&host_name, &hs->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900713 *out_alert = SSL_AD_INTERNAL_ERROR;
714 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700715 }
716
David Benjamin8c880a22016-12-03 02:20:34 -0500717 hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700718 return 1;
719}
720
David Benjamin8c880a22016-12-03 02:20:34 -0500721static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
722 if (hs->ssl->s3->session_reused ||
723 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700724 return 1;
725 }
726
727 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
728 !CBB_add_u16(out, 0 /* length */)) {
729 return 0;
730 }
731
732 return 1;
733}
734
735
Adam Langley5021b222015-06-12 18:27:58 -0700736/* Renegotiation indication.
737 *
738 * https://tools.ietf.org/html/rfc5746 */
739
David Benjamin8c880a22016-12-03 02:20:34 -0500740static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
741 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400742 uint16_t min_version, max_version;
743 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
744 return 0;
745 }
746
747 /* Renegotiation indication is not necessary in TLS 1.3. */
748 if (min_version >= TLS1_3_VERSION) {
749 return 1;
750 }
751
David Benjamin52bf6902016-10-08 12:05:03 -0400752 assert(ssl->s3->initial_handshake_complete ==
753 (ssl->s3->previous_client_finished_len != 0));
754
Adam Langley5021b222015-06-12 18:27:58 -0700755 CBB contents, prev_finished;
756 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
757 !CBB_add_u16_length_prefixed(out, &contents) ||
758 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
759 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
760 ssl->s3->previous_client_finished_len) ||
761 !CBB_flush(out)) {
762 return 0;
763 }
764
765 return 1;
766}
767
David Benjamin8c880a22016-12-03 02:20:34 -0500768static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700769 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500770 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400771 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
772 return 0;
773 }
774
David Benjamin3e052de2015-11-25 20:10:31 -0500775 /* Servers may not switch between omitting the extension and supporting it.
776 * See RFC 5746, sections 3.5 and 4.2. */
777 if (ssl->s3->initial_handshake_complete &&
778 (contents != NULL) != ssl->s3->send_connection_binding) {
779 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
780 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
781 return 0;
782 }
783
Adam Langley5021b222015-06-12 18:27:58 -0700784 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500785 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700786 * RI even on initial ServerHello because the client doesn't see any
787 * renegotiation during an attack. However this would mean we could not
788 * connect to any server which doesn't support RI.
789 *
David Benjamine9cddb82015-11-23 14:36:40 -0500790 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
791 * practical terms every client sets it so it's just assumed here. */
792 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700793 }
794
795 const size_t expected_len = ssl->s3->previous_client_finished_len +
796 ssl->s3->previous_server_finished_len;
797
798 /* Check for logic errors */
799 assert(!expected_len || ssl->s3->previous_client_finished_len);
800 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400801 assert(ssl->s3->initial_handshake_complete ==
802 (ssl->s3->previous_client_finished_len != 0));
803 assert(ssl->s3->initial_handshake_complete ==
804 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700805
806 /* Parse out the extension contents. */
807 CBS renegotiated_connection;
808 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 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
812 return 0;
813 }
814
815 /* Check that the extension matches. */
816 if (CBS_len(&renegotiated_connection) != expected_len) {
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 const uint8_t *d = CBS_data(&renegotiated_connection);
823 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
824 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400825 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700826 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
827 return 0;
828 }
829 d += ssl->s3->previous_client_finished_len;
830
831 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
832 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400833 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700834 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
835 return 0;
836 }
837 ssl->s3->send_connection_binding = 1;
838
839 return 1;
840}
841
David Benjamin8c880a22016-12-03 02:20:34 -0500842static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700843 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500844 SSL *const ssl = hs->ssl;
Adam Langley5021b222015-06-12 18:27:58 -0700845 /* Renegotiation isn't supported as a server so this function should never be
846 * called after the initial handshake. */
847 assert(!ssl->s3->initial_handshake_complete);
848
Steven Valdez143e8b32016-07-11 13:19:03 -0400849 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
850 return 1;
851 }
852
Adam Langley5021b222015-06-12 18:27:58 -0700853 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400854 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700855 }
856
857 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700858 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
859 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400860 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700861 return 0;
862 }
863
David Benjamin52bf6902016-10-08 12:05:03 -0400864 /* Check that the extension matches. We do not support renegotiation as a
865 * server, so this must be empty. */
866 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400867 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700868 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
869 return 0;
870 }
871
872 ssl->s3->send_connection_binding = 1;
873
874 return 1;
875}
876
David Benjamin8c880a22016-12-03 02:20:34 -0500877static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
878 SSL *const ssl = hs->ssl;
David Benjamin52bf6902016-10-08 12:05:03 -0400879 /* Renegotiation isn't supported as a server so this function should never be
880 * called after the initial handshake. */
881 assert(!ssl->s3->initial_handshake_complete);
882
Steven Valdez143e8b32016-07-11 13:19:03 -0400883 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
884 return 1;
885 }
886
Adam Langley5021b222015-06-12 18:27:58 -0700887 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400888 !CBB_add_u16(out, 1 /* length */) ||
889 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700890 return 0;
891 }
892
893 return 1;
894}
895
Adam Langley0a056712015-07-01 15:03:33 -0700896
897/* Extended Master Secret.
898 *
David Benjamin43946d42016-02-01 08:42:19 -0500899 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700900
David Benjamin8c880a22016-12-03 02:20:34 -0500901static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400902 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -0500903 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400904 return 0;
905 }
906
907 /* Extended master secret is not necessary in TLS 1.3. */
908 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700909 return 1;
910 }
911
912 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
913 !CBB_add_u16(out, 0 /* length */)) {
914 return 0;
915 }
916
917 return 1;
918}
919
David Benjamin8c880a22016-12-03 02:20:34 -0500920static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700921 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500922 SSL *const ssl = hs->ssl;
David Benjamin163c9562016-08-29 23:14:17 -0400923 /* Whether EMS is negotiated may not change on renegotation. */
924 if (ssl->s3->initial_handshake_complete) {
925 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
926 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
927 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
928 return 0;
929 }
930
931 return 1;
932 }
933
Adam Langley0a056712015-07-01 15:03:33 -0700934 if (contents == NULL) {
935 return 1;
936 }
937
Steven Valdez143e8b32016-07-11 13:19:03 -0400938 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
939 ssl->version == SSL3_VERSION) {
940 return 0;
941 }
942
943 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700944 return 0;
945 }
946
947 ssl->s3->tmp.extended_master_secret = 1;
948 return 1;
949}
950
David Benjamin8c880a22016-12-03 02:20:34 -0500951static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500952 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500953 SSL *const ssl = hs->ssl;
954 uint16_t version = ssl3_protocol_version(ssl);
955 if (version >= TLS1_3_VERSION ||
956 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400957 return 1;
958 }
959
960 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700961 return 1;
962 }
963
964 if (CBS_len(contents) != 0) {
965 return 0;
966 }
967
968 ssl->s3->tmp.extended_master_secret = 1;
969 return 1;
970}
971
David Benjamin8c880a22016-12-03 02:20:34 -0500972static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
973 if (!hs->ssl->s3->tmp.extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700974 return 1;
975 }
976
977 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
978 !CBB_add_u16(out, 0 /* length */)) {
979 return 0;
980 }
981
982 return 1;
983}
984
Adam Langley9b05bc52015-07-01 15:25:33 -0700985
986/* Session tickets.
987 *
988 * https://tools.ietf.org/html/rfc5077 */
989
David Benjamin8c880a22016-12-03 02:20:34 -0500990static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
991 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400992 uint16_t min_version, max_version;
993 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
994 return 0;
995 }
996
997 /* TLS 1.3 uses a different ticket extension. */
998 if (min_version >= TLS1_3_VERSION ||
999 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001000 return 1;
1001 }
1002
1003 const uint8_t *ticket_data = NULL;
1004 int ticket_len = 0;
1005
1006 /* Renegotiation does not participate in session resumption. However, still
1007 * advertise the extension to avoid potentially breaking servers which carry
1008 * over the state from the previous handshake, such as OpenSSL servers
1009 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -04001010 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -07001011 if (!ssl->s3->initial_handshake_complete &&
1012 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001013 ssl->session->tlsext_tick != NULL &&
1014 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -04001015 ssl->method->version_from_wire(&session_version,
1016 ssl->session->ssl_version) &&
1017 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001018 ticket_data = ssl->session->tlsext_tick;
1019 ticket_len = ssl->session->tlsext_ticklen;
1020 }
1021
1022 CBB ticket;
1023 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1024 !CBB_add_u16_length_prefixed(out, &ticket) ||
1025 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1026 !CBB_flush(out)) {
1027 return 0;
1028 }
1029
1030 return 1;
1031}
1032
David Benjamin8c880a22016-12-03 02:20:34 -05001033static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -07001034 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001035 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -07001036 if (contents == NULL) {
1037 return 1;
1038 }
1039
Steven Valdez143e8b32016-07-11 13:19:03 -04001040 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1041 return 0;
1042 }
1043
Adam Langley9b05bc52015-07-01 15:25:33 -07001044 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1045 * this function should never be called, even if the server tries to send the
1046 * extension. */
1047 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1048
1049 if (CBS_len(contents) != 0) {
1050 return 0;
1051 }
1052
David Benjamin8c880a22016-12-03 02:20:34 -05001053 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001054 return 1;
1055}
1056
David Benjamin8c880a22016-12-03 02:20:34 -05001057static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1058 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001059 return 1;
1060 }
1061
David Benjamin78476f62016-11-12 11:20:55 +09001062 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin8c880a22016-12-03 02:20:34 -05001063 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001064
1065 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1066 !CBB_add_u16(out, 0 /* length */)) {
1067 return 0;
1068 }
1069
1070 return 1;
1071}
1072
1073
Adam Langley2e857bd2015-07-01 16:09:19 -07001074/* Signature Algorithms.
1075 *
1076 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1077
David Benjamin8c880a22016-12-03 02:20:34 -05001078static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1079 SSL *const ssl = hs->ssl;
David Benjamin2dc02042016-09-19 19:57:37 -04001080 uint16_t min_version, max_version;
1081 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1082 return 0;
1083 }
1084
1085 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001086 return 1;
1087 }
1088
David Benjamin0fc37ef2016-08-17 15:29:46 -04001089 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001090 const size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001091
David Benjamin0fc37ef2016-08-17 15:29:46 -04001092 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001093 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1094 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001095 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001096 return 0;
1097 }
1098
David Benjamin0fc37ef2016-08-17 15:29:46 -04001099 for (size_t i = 0; i < num_sigalgs; i++) {
1100 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001101 return 0;
1102 }
1103 }
1104
1105 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001106 return 0;
1107 }
1108
1109 return 1;
1110}
1111
David Benjamin8c880a22016-12-03 02:20:34 -05001112static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001113 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001114 OPENSSL_free(hs->peer_sigalgs);
1115 hs->peer_sigalgs = NULL;
1116 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001117
Adam Langley2e857bd2015-07-01 16:09:19 -07001118 if (contents == NULL) {
1119 return 1;
1120 }
1121
1122 CBS supported_signature_algorithms;
1123 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001124 CBS_len(contents) != 0 ||
1125 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001126 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001127 return 0;
1128 }
1129
1130 return 1;
1131}
1132
Adam Langley2e857bd2015-07-01 16:09:19 -07001133
Adam Langleybb0bd042015-07-01 16:21:03 -07001134/* OCSP Stapling.
1135 *
1136 * https://tools.ietf.org/html/rfc6066#section-8 */
1137
David Benjamin8c880a22016-12-03 02:20:34 -05001138static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1139 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001140 if (!ssl->ocsp_stapling_enabled) {
1141 return 1;
1142 }
1143
1144 CBB contents;
1145 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1146 !CBB_add_u16_length_prefixed(out, &contents) ||
1147 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1148 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1149 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1150 !CBB_flush(out)) {
1151 return 0;
1152 }
1153
1154 return 1;
1155}
1156
David Benjamin8c880a22016-12-03 02:20:34 -05001157static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001158 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001159 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001160 if (contents == NULL) {
1161 return 1;
1162 }
1163
Steven Valdeza833c352016-11-01 13:39:36 -04001164 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1165 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001166 return 0;
1167 }
1168
Steven Valdeza833c352016-11-01 13:39:36 -04001169 /* OCSP stapling is forbidden on non-certificate ciphers. */
1170 if (CBS_len(contents) != 0 ||
1171 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001172 return 0;
1173 }
1174
Steven Valdeza833c352016-11-01 13:39:36 -04001175 /* Note this does not check for resumption in TLS 1.2. Sending
1176 * status_request here does not make sense, but OpenSSL does so and the
1177 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001178
David Benjamin8c880a22016-12-03 02:20:34 -05001179 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001180 return 1;
1181}
1182
David Benjamin8c880a22016-12-03 02:20:34 -05001183static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001184 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001185 if (contents == NULL) {
1186 return 1;
1187 }
1188
1189 uint8_t status_type;
1190 if (!CBS_get_u8(contents, &status_type)) {
1191 return 0;
1192 }
1193
1194 /* We cannot decide whether OCSP stapling will occur yet because the correct
1195 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001196 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001197
Adam Langleybb0bd042015-07-01 16:21:03 -07001198 return 1;
1199}
1200
David Benjamin8c880a22016-12-03 02:20:34 -05001201static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1202 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001203 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001204 !hs->ocsp_stapling_requested ||
Alessandro Ghedini559f0642016-12-07 12:55:32 +00001205 ssl->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001206 ssl->s3->session_reused ||
Steven Valdeza833c352016-11-01 13:39:36 -04001207 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001208 return 1;
1209 }
1210
David Benjamin8c880a22016-12-03 02:20:34 -05001211 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001212
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001213 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001214 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001215}
1216
1217
Adam Langley97dfcbf2015-07-01 18:35:20 -07001218/* Next protocol negotiation.
1219 *
1220 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1221
David Benjamin8c880a22016-12-03 02:20:34 -05001222static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1223 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001224 if (ssl->s3->initial_handshake_complete ||
1225 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001226 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001227 return 1;
1228 }
1229
1230 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1231 !CBB_add_u16(out, 0 /* length */)) {
1232 return 0;
1233 }
1234
1235 return 1;
1236}
1237
David Benjamin8c880a22016-12-03 02:20:34 -05001238static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001239 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001240 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001241 if (contents == NULL) {
1242 return 1;
1243 }
1244
Steven Valdez143e8b32016-07-11 13:19:03 -04001245 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1246 return 0;
1247 }
1248
Adam Langley97dfcbf2015-07-01 18:35:20 -07001249 /* If any of these are false then we should never have sent the NPN
1250 * extension in the ClientHello and thus this function should never have been
1251 * called. */
1252 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001253 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001254 assert(ssl->ctx->next_proto_select_cb != NULL);
1255
David Benjamin76c2efc2015-08-31 14:24:29 -04001256 if (ssl->s3->alpn_selected != NULL) {
1257 /* NPN and ALPN may not be negotiated in the same connection. */
1258 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1259 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1260 return 0;
1261 }
1262
Adam Langley97dfcbf2015-07-01 18:35:20 -07001263 const uint8_t *const orig_contents = CBS_data(contents);
1264 const size_t orig_len = CBS_len(contents);
1265
1266 while (CBS_len(contents) != 0) {
1267 CBS proto;
1268 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1269 CBS_len(&proto) == 0) {
1270 return 0;
1271 }
1272 }
1273
1274 uint8_t *selected;
1275 uint8_t selected_len;
1276 if (ssl->ctx->next_proto_select_cb(
1277 ssl, &selected, &selected_len, orig_contents, orig_len,
1278 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1279 *out_alert = SSL_AD_INTERNAL_ERROR;
1280 return 0;
1281 }
1282
David Benjamin79978df2015-12-25 15:56:49 -05001283 OPENSSL_free(ssl->s3->next_proto_negotiated);
1284 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1285 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001286 *out_alert = SSL_AD_INTERNAL_ERROR;
1287 return 0;
1288 }
1289
David Benjamin79978df2015-12-25 15:56:49 -05001290 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001291 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001292
1293 return 1;
1294}
1295
David Benjamin8c880a22016-12-03 02:20:34 -05001296static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001297 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001298 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001299 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1300 return 1;
1301 }
1302
Adam Langley97dfcbf2015-07-01 18:35:20 -07001303 if (contents != NULL && CBS_len(contents) != 0) {
1304 return 0;
1305 }
1306
1307 if (contents == NULL ||
1308 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001309 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001310 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001311 return 1;
1312 }
1313
David Benjamin8c880a22016-12-03 02:20:34 -05001314 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001315 return 1;
1316}
1317
David Benjamin8c880a22016-12-03 02:20:34 -05001318static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1319 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001320 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1321 * parsed. */
David Benjamin8c880a22016-12-03 02:20:34 -05001322 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001323 return 1;
1324 }
1325
1326 const uint8_t *npa;
1327 unsigned npa_len;
1328
1329 if (ssl->ctx->next_protos_advertised_cb(
1330 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1331 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001332 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001333 return 1;
1334 }
1335
1336 CBB contents;
1337 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1338 !CBB_add_u16_length_prefixed(out, &contents) ||
1339 !CBB_add_bytes(&contents, npa, npa_len) ||
1340 !CBB_flush(out)) {
1341 return 0;
1342 }
1343
1344 return 1;
1345}
1346
1347
Adam Langleyab8d87d2015-07-10 12:21:39 -07001348/* Signed certificate timestamps.
1349 *
1350 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1351
David Benjamin8c880a22016-12-03 02:20:34 -05001352static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1353 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001354 if (!ssl->signed_cert_timestamps_enabled) {
1355 return 1;
1356 }
1357
1358 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1359 !CBB_add_u16(out, 0 /* length */)) {
1360 return 0;
1361 }
1362
1363 return 1;
1364}
1365
David Benjamin8c880a22016-12-03 02:20:34 -05001366static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001367 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001368 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001369 if (contents == NULL) {
1370 return 1;
1371 }
1372
Steven Valdeza833c352016-11-01 13:39:36 -04001373 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1374 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001375 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001376 return 0;
1377 }
1378
Adam Langleyab8d87d2015-07-10 12:21:39 -07001379 /* If this is false then we should never have sent the SCT extension in the
1380 * ClientHello and thus this function should never have been called. */
1381 assert(ssl->signed_cert_timestamps_enabled);
1382
Adam Langleycfa08c32016-11-17 13:21:27 -08001383 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001384 *out_alert = SSL_AD_DECODE_ERROR;
1385 return 0;
1386 }
1387
David Benjamindaa88502016-10-04 16:32:16 -04001388 /* Session resumption uses the original session information. The extension
1389 * should not be sent on resumption, but RFC 6962 did not make it a
1390 * requirement, so tolerate this.
1391 *
1392 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001393 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001394 !CBS_stow(
1395 contents,
1396 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1397 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001398 *out_alert = SSL_AD_INTERNAL_ERROR;
1399 return 0;
1400 }
1401
1402 return 1;
1403}
1404
David Benjamin8c880a22016-12-03 02:20:34 -05001405static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001406 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001407 if (contents == NULL) {
1408 return 1;
1409 }
1410
1411 if (CBS_len(contents) != 0) {
1412 return 0;
1413 }
1414
David Benjamin8c880a22016-12-03 02:20:34 -05001415 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001416 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001417}
1418
David Benjamin8c880a22016-12-03 02:20:34 -05001419static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1420 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001421 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001422 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1423 ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001424 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001425 return 1;
1426 }
1427
1428 CBB contents;
1429 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1430 CBB_add_u16_length_prefixed(out, &contents) &&
1431 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1432 ssl->ctx->signed_cert_timestamp_list_length) &&
1433 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001434}
1435
1436
Adam Langleyf18e4532015-07-10 13:39:53 -07001437/* Application-level Protocol Negotiation.
1438 *
1439 * https://tools.ietf.org/html/rfc7301 */
1440
David Benjamin8c880a22016-12-03 02:20:34 -05001441static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1442 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001443 if (ssl->alpn_client_proto_list == NULL ||
1444 ssl->s3->initial_handshake_complete) {
1445 return 1;
1446 }
1447
1448 CBB contents, proto_list;
1449 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1450 !CBB_add_u16_length_prefixed(out, &contents) ||
1451 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1452 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1453 ssl->alpn_client_proto_list_len) ||
1454 !CBB_flush(out)) {
1455 return 0;
1456 }
1457
1458 return 1;
1459}
1460
David Benjamin8c880a22016-12-03 02:20:34 -05001461static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001462 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001463 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001464 if (contents == NULL) {
1465 return 1;
1466 }
1467
1468 assert(!ssl->s3->initial_handshake_complete);
1469 assert(ssl->alpn_client_proto_list != NULL);
1470
David Benjamin8c880a22016-12-03 02:20:34 -05001471 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001472 /* NPN and ALPN may not be negotiated in the same connection. */
1473 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1474 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1475 return 0;
1476 }
1477
Adam Langleyf18e4532015-07-10 13:39:53 -07001478 /* The extension data consists of a ProtocolNameList which must have
1479 * exactly one ProtocolName. Each of these is length-prefixed. */
1480 CBS protocol_name_list, protocol_name;
1481 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1482 CBS_len(contents) != 0 ||
1483 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1484 /* Empty protocol names are forbidden. */
1485 CBS_len(&protocol_name) == 0 ||
1486 CBS_len(&protocol_name_list) != 0) {
1487 return 0;
1488 }
1489
David Benjamin3e51757d2016-08-11 11:52:23 -04001490 /* Check that the protcol name is one of the ones we advertised. */
1491 int protocol_ok = 0;
1492 CBS client_protocol_name_list, client_protocol_name;
1493 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1494 ssl->alpn_client_proto_list_len);
1495 while (CBS_len(&client_protocol_name_list) > 0) {
1496 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1497 &client_protocol_name)) {
1498 *out_alert = SSL_AD_INTERNAL_ERROR;
1499 return 0;
1500 }
1501
1502 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
David Benjamin17cf2cb2016-12-13 01:07:13 -05001503 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1504 CBS_data(&protocol_name),
1505 CBS_len(&protocol_name)) == 0) {
David Benjamin3e51757d2016-08-11 11:52:23 -04001506 protocol_ok = 1;
1507 break;
1508 }
1509 }
1510
1511 if (!protocol_ok) {
1512 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1513 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1514 return 0;
1515 }
1516
Adam Langleyf18e4532015-07-10 13:39:53 -07001517 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1518 &ssl->s3->alpn_selected_len)) {
1519 *out_alert = SSL_AD_INTERNAL_ERROR;
1520 return 0;
1521 }
1522
1523 return 1;
1524}
1525
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001526int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001527 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001528 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001529 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001530 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001531 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001532 client_hello, &contents,
1533 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1534 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001535 return 1;
1536 }
1537
1538 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001539 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001540
1541 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001542 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1543 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001544 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001545 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1546 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001547 return 0;
1548 }
1549
1550 /* Validate the protocol list. */
1551 CBS protocol_name_list_copy = protocol_name_list;
1552 while (CBS_len(&protocol_name_list_copy) > 0) {
1553 CBS protocol_name;
1554
1555 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1556 /* Empty protocol names are forbidden. */
1557 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001558 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1559 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001560 return 0;
1561 }
1562 }
1563
1564 const uint8_t *selected;
1565 uint8_t selected_len;
1566 if (ssl->ctx->alpn_select_cb(
1567 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1568 CBS_len(&protocol_name_list),
1569 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1570 OPENSSL_free(ssl->s3->alpn_selected);
1571 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1572 if (ssl->s3->alpn_selected == NULL) {
1573 *out_alert = SSL_AD_INTERNAL_ERROR;
1574 return 0;
1575 }
1576 ssl->s3->alpn_selected_len = selected_len;
1577 }
1578
1579 return 1;
1580}
1581
David Benjamin8c880a22016-12-03 02:20:34 -05001582static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1583 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001584 if (ssl->s3->alpn_selected == NULL) {
1585 return 1;
1586 }
1587
1588 CBB contents, proto_list, proto;
1589 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1590 !CBB_add_u16_length_prefixed(out, &contents) ||
1591 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1592 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001593 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1594 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001595 !CBB_flush(out)) {
1596 return 0;
1597 }
1598
1599 return 1;
1600}
1601
1602
Adam Langley49c7af12015-07-10 14:33:46 -07001603/* Channel ID.
1604 *
1605 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1606
David Benjamin8c880a22016-12-03 02:20:34 -05001607static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1608 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001609}
1610
David Benjamin8c880a22016-12-03 02:20:34 -05001611static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1612 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001613 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001614 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001615 return 1;
1616 }
1617
1618 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1619 !CBB_add_u16(out, 0 /* length */)) {
1620 return 0;
1621 }
1622
1623 return 1;
1624}
1625
David Benjamin8c880a22016-12-03 02:20:34 -05001626static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1627 uint8_t *out_alert, CBS *contents) {
1628 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001629 if (contents == NULL) {
1630 return 1;
1631 }
1632
David Benjamince079fd2016-08-02 16:22:34 -04001633 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001634 assert(ssl->tlsext_channel_id_enabled);
1635
1636 if (CBS_len(contents) != 0) {
1637 return 0;
1638 }
1639
1640 ssl->s3->tlsext_channel_id_valid = 1;
1641 return 1;
1642}
1643
David Benjamin8c880a22016-12-03 02:20:34 -05001644static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1645 uint8_t *out_alert, CBS *contents) {
1646 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001647 if (contents == NULL ||
1648 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001649 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001650 return 1;
1651 }
1652
1653 if (CBS_len(contents) != 0) {
1654 return 0;
1655 }
1656
1657 ssl->s3->tlsext_channel_id_valid = 1;
1658 return 1;
1659}
1660
David Benjamin8c880a22016-12-03 02:20:34 -05001661static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1662 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001663 if (!ssl->s3->tlsext_channel_id_valid) {
1664 return 1;
1665 }
1666
1667 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1668 !CBB_add_u16(out, 0 /* length */)) {
1669 return 0;
1670 }
1671
1672 return 1;
1673}
1674
Adam Langley391250d2015-07-15 19:06:07 -07001675
1676/* Secure Real-time Transport Protocol (SRTP) extension.
1677 *
1678 * https://tools.ietf.org/html/rfc5764 */
1679
Adam Langley391250d2015-07-15 19:06:07 -07001680
David Benjamin8c880a22016-12-03 02:20:34 -05001681static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1682 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001683}
1684
David Benjamin8c880a22016-12-03 02:20:34 -05001685static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1686 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001687 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1688 if (profiles == NULL) {
1689 return 1;
1690 }
1691 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1692 if (num_profiles == 0) {
1693 return 1;
1694 }
1695
1696 CBB contents, profile_ids;
1697 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1698 !CBB_add_u16_length_prefixed(out, &contents) ||
1699 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1700 return 0;
1701 }
1702
David Benjamin54091232016-09-05 12:47:25 -04001703 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001704 if (!CBB_add_u16(&profile_ids,
1705 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1706 return 0;
1707 }
1708 }
1709
1710 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1711 !CBB_flush(out)) {
1712 return 0;
1713 }
1714
1715 return 1;
1716}
1717
David Benjamin8c880a22016-12-03 02:20:34 -05001718static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001719 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001720 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001721 if (contents == NULL) {
1722 return 1;
1723 }
1724
1725 /* The extension consists of a u16-prefixed profile ID list containing a
1726 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1727 *
1728 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1729 CBS profile_ids, srtp_mki;
1730 uint16_t profile_id;
1731 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1732 !CBS_get_u16(&profile_ids, &profile_id) ||
1733 CBS_len(&profile_ids) != 0 ||
1734 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1735 CBS_len(contents) != 0) {
1736 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1737 return 0;
1738 }
1739
1740 if (CBS_len(&srtp_mki) != 0) {
1741 /* Must be no MKI, since we never offer one. */
1742 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1743 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1744 return 0;
1745 }
1746
1747 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1748
1749 /* Check to see if the server gave us something we support (and presumably
1750 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001751 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001752 const SRTP_PROTECTION_PROFILE *profile =
1753 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1754
1755 if (profile->id == profile_id) {
1756 ssl->srtp_profile = profile;
1757 return 1;
1758 }
1759 }
1760
1761 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1762 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1763 return 0;
1764}
1765
David Benjamin8c880a22016-12-03 02:20:34 -05001766static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001767 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001768 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001769 if (contents == NULL) {
1770 return 1;
1771 }
1772
1773 CBS profile_ids, srtp_mki;
1774 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1775 CBS_len(&profile_ids) < 2 ||
1776 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1777 CBS_len(contents) != 0) {
1778 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1779 return 0;
1780 }
1781 /* Discard the MKI value for now. */
1782
1783 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1784 SSL_get_srtp_profiles(ssl);
1785
1786 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001787 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001788 const SRTP_PROTECTION_PROFILE *server_profile =
1789 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1790
1791 CBS profile_ids_tmp;
1792 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1793
1794 while (CBS_len(&profile_ids_tmp) > 0) {
1795 uint16_t profile_id;
1796 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1797 return 0;
1798 }
1799
1800 if (server_profile->id == profile_id) {
1801 ssl->srtp_profile = server_profile;
1802 return 1;
1803 }
1804 }
1805 }
1806
1807 return 1;
1808}
1809
David Benjamin8c880a22016-12-03 02:20:34 -05001810static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1811 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001812 if (ssl->srtp_profile == NULL) {
1813 return 1;
1814 }
1815
1816 CBB contents, profile_ids;
1817 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1818 !CBB_add_u16_length_prefixed(out, &contents) ||
1819 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1820 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1821 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1822 !CBB_flush(out)) {
1823 return 0;
1824 }
1825
1826 return 1;
1827}
1828
Adam Langleybdd5d662015-07-20 16:19:08 -07001829
1830/* EC point formats.
1831 *
1832 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1833
David Benjamin8c880a22016-12-03 02:20:34 -05001834static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001835 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001836 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1837 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001838 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1839 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001840 !CBB_flush(out)) {
1841 return 0;
1842 }
1843
1844 return 1;
1845}
1846
David Benjamin8c880a22016-12-03 02:20:34 -05001847static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001848 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -05001849 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin70aba262016-11-01 12:08:15 -04001850 return 0;
1851 }
1852
1853 /* The point format extension is unneccessary in TLS 1.3. */
1854 if (min_version >= TLS1_3_VERSION) {
1855 return 1;
1856 }
1857
David Benjamin8c880a22016-12-03 02:20:34 -05001858 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001859}
1860
David Benjamin8c880a22016-12-03 02:20:34 -05001861static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001862 CBS *contents) {
1863 if (contents == NULL) {
1864 return 1;
1865 }
1866
David Benjamin8c880a22016-12-03 02:20:34 -05001867 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001868 return 0;
1869 }
1870
Adam Langleybdd5d662015-07-20 16:19:08 -07001871 CBS ec_point_format_list;
1872 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1873 CBS_len(contents) != 0) {
1874 return 0;
1875 }
1876
David Benjaminfc059942015-07-30 23:01:59 -04001877 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1878 * point format. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05001879 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1880 TLSEXT_ECPOINTFORMAT_uncompressed,
1881 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001882 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001883 return 0;
1884 }
1885
1886 return 1;
1887}
1888
David Benjamin8c880a22016-12-03 02:20:34 -05001889static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001890 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001891 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001892 return 1;
1893 }
1894
David Benjamin8c880a22016-12-03 02:20:34 -05001895 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001896}
1897
David Benjamin8c880a22016-12-03 02:20:34 -05001898static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1899 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001900 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1901 return 1;
1902 }
1903
Adam Langleybdd5d662015-07-20 16:19:08 -07001904 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1905 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001906 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001907
1908 if (!using_ecc) {
1909 return 1;
1910 }
1911
David Benjamin8c880a22016-12-03 02:20:34 -05001912 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001913}
1914
Steven Valdeza833c352016-11-01 13:39:36 -04001915
Steven Valdez4aa154e2016-07-29 14:32:55 -04001916/* Pre Shared Key
1917 *
Steven Valdeza833c352016-11-01 13:39:36 -04001918 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001919
David Benjamin8c880a22016-12-03 02:20:34 -05001920static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1921 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001922 uint16_t min_version, max_version;
1923 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1924 return 0;
1925 }
1926
1927 uint16_t session_version;
1928 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1929 !ssl->method->version_from_wire(&session_version,
1930 ssl->session->ssl_version) ||
1931 session_version < TLS1_3_VERSION) {
1932 return 0;
1933 }
1934
1935 const EVP_MD *digest =
1936 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1937 size_t binder_len = EVP_MD_size(digest);
1938 return 15 + ssl->session->tlsext_ticklen + binder_len;
1939}
1940
David Benjamin8c880a22016-12-03 02:20:34 -05001941static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1942 SSL *const ssl = hs->ssl;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001943 uint16_t min_version, max_version;
1944 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1945 return 0;
1946 }
1947
David Benjamin2dc02042016-09-19 19:57:37 -04001948 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001949 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001950 !ssl->method->version_from_wire(&session_version,
1951 ssl->session->ssl_version) ||
1952 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001953 return 1;
1954 }
1955
Steven Valdeza833c352016-11-01 13:39:36 -04001956 struct timeval now;
1957 ssl_get_current_time(ssl, &now);
1958 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1959 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1960
1961 /* Fill in a placeholder zero binder of the appropriate length. It will be
1962 * computed and filled in later after length prefixes are computed. */
1963 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
1964 const EVP_MD *digest =
1965 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1966 size_t binder_len = EVP_MD_size(digest);
1967
1968 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001969 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1970 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001971 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001972 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1973 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001974 ssl->session->tlsext_ticklen) ||
1975 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1976 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1977 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1978 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001979 return 0;
1980 }
1981
David Benjamin8c880a22016-12-03 02:20:34 -05001982 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001983 return CBB_flush(out);
1984}
1985
David Benjamin8baf9632016-11-17 17:11:16 +09001986int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1987 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001988 CBS *contents) {
1989 uint16_t psk_id;
1990 if (!CBS_get_u16(contents, &psk_id) ||
1991 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001992 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001993 *out_alert = SSL_AD_DECODE_ERROR;
1994 return 0;
1995 }
1996
David Benjamineab773a2016-11-09 18:32:35 -05001997 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001998 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001999 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002000 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2001 return 0;
2002 }
2003
2004 return 1;
2005}
2006
David Benjamin8baf9632016-11-17 17:11:16 +09002007int ssl_ext_pre_shared_key_parse_clienthello(SSL_HANDSHAKE *hs,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002008 SSL_SESSION **out_session,
Steven Valdeza833c352016-11-01 13:39:36 -04002009 CBS *out_binders,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002010 uint8_t *out_alert,
2011 CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09002012 SSL *const ssl = hs->ssl;
Steven Valdez5b986082016-09-01 12:29:49 -04002013 /* We only process the first PSK identity since we don't support pure PSK. */
Steven Valdeza833c352016-11-01 13:39:36 -04002014 uint32_t obfuscated_ticket_age;
David Benjaminaedf3032016-12-01 16:47:56 -05002015 CBS identities, ticket, binders;
2016 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2017 !CBS_get_u16_length_prefixed(&identities, &ticket) ||
2018 !CBS_get_u32(&identities, &obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04002019 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05002020 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04002021 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04002022 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002023 *out_alert = SSL_AD_DECODE_ERROR;
2024 return 0;
2025 }
2026
Steven Valdeza833c352016-11-01 13:39:36 -04002027 *out_binders = binders;
2028
David Benjaminaedf3032016-12-01 16:47:56 -05002029 /* Check the syntax of the remaining identities, but do not process them. */
2030 size_t num_identities = 1;
2031 while (CBS_len(&identities) != 0) {
2032 CBS unused_ticket;
2033 uint32_t unused_obfuscated_ticket_age;
2034 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
2035 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
2036 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2037 *out_alert = SSL_AD_DECODE_ERROR;
2038 return 0;
2039 }
2040
2041 num_identities++;
2042 }
2043
2044 /* Check the syntax of the binders. The value will be checked later if
2045 * resuming. */
2046 size_t num_binders = 0;
2047 while (CBS_len(&binders) != 0) {
2048 CBS binder;
2049 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
2050 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2051 *out_alert = SSL_AD_DECODE_ERROR;
2052 return 0;
2053 }
2054
2055 num_binders++;
2056 }
2057
2058 if (num_identities != num_binders) {
2059 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
2060 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04002061 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04002062 }
2063
Steven Valdeza833c352016-11-01 13:39:36 -04002064 /* TODO(svaldez): Check that the ticket_age is valid when attempting to use
2065 * the PSK for 0-RTT. http://crbug.com/boringssl/113 */
2066
Steven Valdez4aa154e2016-07-29 14:32:55 -04002067 /* TLS 1.3 session tickets are renewed separately as part of the
2068 * NewSessionTicket. */
David Benjamine7f60a22016-11-16 18:54:25 +09002069 int unused_renew;
2070 if (!tls_process_ticket(ssl, out_session, &unused_renew, CBS_data(&ticket),
2071 CBS_len(&ticket), NULL, 0)) {
2072 *out_alert = SSL_AD_INTERNAL_ERROR;
2073 return 0;
2074 }
2075
2076 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002077}
2078
David Benjamin8baf9632016-11-17 17:11:16 +09002079int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2080 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002081 return 1;
2082 }
2083
2084 CBB contents;
2085 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2086 !CBB_add_u16_length_prefixed(out, &contents) ||
2087 /* We only consider the first identity for resumption */
2088 !CBB_add_u16(&contents, 0) ||
2089 !CBB_flush(out)) {
2090 return 0;
2091 }
2092
2093 return 1;
2094}
2095
2096
Steven Valdeza833c352016-11-01 13:39:36 -04002097/* Pre-Shared Key Exchange Modes
2098 *
2099 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002100
David Benjamin8c880a22016-12-03 02:20:34 -05002101static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2102 CBB *out) {
2103 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04002104 uint16_t min_version, max_version;
2105 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2106 return 0;
2107 }
2108
2109 if (max_version < TLS1_3_VERSION) {
2110 return 1;
2111 }
2112
2113 CBB contents, ke_modes;
2114 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2115 !CBB_add_u16_length_prefixed(out, &contents) ||
2116 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2117 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2118 return 0;
2119 }
2120
2121 return CBB_flush(out);
2122}
2123
David Benjamin8c880a22016-12-03 02:20:34 -05002124static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002125 uint8_t *out_alert,
2126 CBS *contents) {
2127 if (contents == NULL) {
2128 return 1;
2129 }
2130
Steven Valdeza833c352016-11-01 13:39:36 -04002131 CBS ke_modes;
2132 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2133 CBS_len(&ke_modes) == 0 ||
2134 CBS_len(contents) != 0) {
2135 *out_alert = SSL_AD_DECODE_ERROR;
2136 return 0;
2137 }
2138
2139 /* We only support tickets with PSK_DHE_KE. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05002140 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2141 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002142
2143 return 1;
2144}
2145
2146
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002147/* Early Data Indication
2148 *
2149 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2150
David Benjamin8c880a22016-12-03 02:20:34 -05002151static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002152 /* TODO(svaldez): Support 0RTT. */
2153 return 1;
2154}
2155
David Benjamin8c880a22016-12-03 02:20:34 -05002156static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2157 uint8_t *out_alert, CBS *contents) {
2158 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002159 if (contents == NULL) {
2160 return 1;
2161 }
2162
2163 if (CBS_len(contents) != 0) {
2164 *out_alert = SSL_AD_DECODE_ERROR;
2165 return 0;
2166 }
2167
2168 /* Since we don't currently accept 0-RTT, we have to skip past any early data
2169 * the client might have sent. */
2170 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
2171 ssl->s3->skip_early_data = 1;
2172 }
2173 return 1;
2174}
2175
2176
Steven Valdez143e8b32016-07-11 13:19:03 -04002177/* Key Share
2178 *
David Benjamina128a552016-10-13 14:26:33 -04002179 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002180
David Benjamin8c880a22016-12-03 02:20:34 -05002181static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2182 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002183 uint16_t min_version, max_version;
2184 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2185 return 0;
2186 }
2187
David Benjamin53a2dfc2016-10-07 21:04:12 -04002188 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002189 return 1;
2190 }
2191
2192 CBB contents, kse_bytes;
2193 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2194 !CBB_add_u16_length_prefixed(out, &contents) ||
2195 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2196 return 0;
2197 }
2198
David Benjamin8c880a22016-12-03 02:20:34 -05002199 uint16_t group_id = hs->retry_group;
2200 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002201 /* We received a HelloRetryRequest without a new curve, so there is no new
2202 * share to append. Leave |ecdh_ctx| as-is. */
2203 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002204 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2205 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002206 return 0;
2207 }
David Benjamin8c880a22016-12-03 02:20:34 -05002208 OPENSSL_free(hs->key_share_bytes);
2209 hs->key_share_bytes = NULL;
2210 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002211 if (group_id == 0) {
2212 return CBB_flush(out);
2213 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002214 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002215 /* Add a fake group. See draft-davidben-tls-grease-01. */
2216 if (ssl->ctx->grease_enabled &&
2217 (!CBB_add_u16(&kse_bytes,
2218 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2219 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2220 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2221 return 0;
2222 }
2223
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002224 /* Predict the most preferred group. */
2225 const uint16_t *groups;
2226 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002227 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002228 if (groups_len == 0) {
2229 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2230 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002231 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002232
2233 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002234 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002235
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002236 CBB key_exchange;
2237 if (!CBB_add_u16(&kse_bytes, group_id) ||
2238 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002239 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2240 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002241 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002242 return 0;
2243 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002244
David Benjamin8c880a22016-12-03 02:20:34 -05002245 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002246 /* Save the contents of the extension to repeat it in the second
2247 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002248 hs->key_share_bytes_len = CBB_len(&kse_bytes);
2249 hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
2250 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002251 return 0;
2252 }
2253 }
2254
Steven Valdez143e8b32016-07-11 13:19:03 -04002255 return CBB_flush(out);
2256}
2257
David Benjamin8baf9632016-11-17 17:11:16 +09002258int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002259 size_t *out_secret_len,
2260 uint8_t *out_alert, CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09002261 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002262 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002263 uint16_t group_id;
2264 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002265 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2266 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002267 *out_alert = SSL_AD_DECODE_ERROR;
2268 return 0;
2269 }
2270
David Benjamin8baf9632016-11-17 17:11:16 +09002271 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002272 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2273 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2274 return 0;
2275 }
2276
David Benjamin8baf9632016-11-17 17:11:16 +09002277 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2278 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002279 *out_alert = SSL_AD_INTERNAL_ERROR;
2280 return 0;
2281 }
2282
David Benjamin4882a6c2016-12-11 02:48:12 -05002283 ssl->s3->new_session->group_id = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002284 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002285 return 1;
2286}
2287
David Benjamin8baf9632016-11-17 17:11:16 +09002288int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002289 uint8_t **out_secret,
2290 size_t *out_secret_len,
2291 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002292 uint16_t group_id;
2293 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002294 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002295 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2296 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2297 return 0;
2298 }
2299
2300 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002301 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002302 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 /* Find the corresponding key share. */
2307 int found = 0;
2308 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002309 while (CBS_len(&key_shares) > 0) {
2310 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002311 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002312 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002313 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2314 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002315 return 0;
2316 }
2317
David Benjamin7e1f9842016-09-20 19:24:40 -04002318 if (id == group_id) {
2319 if (found) {
2320 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2321 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2322 return 0;
2323 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002324
David Benjamin7e1f9842016-09-20 19:24:40 -04002325 found = 1;
2326 peer_key = peer_key_tmp;
2327 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002328 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002329 }
2330
David Benjamin7e1f9842016-09-20 19:24:40 -04002331 if (!found) {
2332 *out_found = 0;
2333 *out_secret = NULL;
2334 *out_secret_len = 0;
2335 return 1;
2336 }
2337
2338 /* Compute the DH secret. */
2339 uint8_t *secret = NULL;
2340 size_t secret_len;
2341 SSL_ECDH_CTX group;
David Benjamin17cf2cb2016-12-13 01:07:13 -05002342 OPENSSL_memset(&group, 0, sizeof(SSL_ECDH_CTX));
David Benjamin7e1f9842016-09-20 19:24:40 -04002343 CBB public_key;
2344 if (!CBB_init(&public_key, 32) ||
2345 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002346 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
2347 CBS_data(&peer_key), CBS_len(&peer_key)) ||
2348 !CBB_finish(&public_key, &hs->public_key, &hs->public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002349 OPENSSL_free(secret);
2350 SSL_ECDH_CTX_cleanup(&group);
2351 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002352 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002353 return 0;
2354 }
2355
2356 SSL_ECDH_CTX_cleanup(&group);
2357
2358 *out_secret = secret;
2359 *out_secret_len = secret_len;
2360 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002361 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002362}
2363
David Benjamin8baf9632016-11-17 17:11:16 +09002364int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2365 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002366 uint16_t group_id;
2367 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002368 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002369 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2370 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2371 !CBB_add_u16(&kse_bytes, group_id) ||
2372 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002373 !CBB_add_bytes(&public_key, hs->public_key, hs->public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002374 !CBB_flush(out)) {
2375 return 0;
2376 }
2377
David Benjamin8baf9632016-11-17 17:11:16 +09002378 OPENSSL_free(hs->public_key);
2379 hs->public_key = NULL;
2380 hs->public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002381
David Benjamin4882a6c2016-12-11 02:48:12 -05002382 ssl->s3->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002383 return 1;
2384}
2385
2386
Steven Valdezfdd10992016-09-15 16:27:05 -04002387/* Supported Versions
2388 *
2389 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2390
David Benjamin8c880a22016-12-03 02:20:34 -05002391static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2392 SSL *const ssl = hs->ssl;
Steven Valdezfdd10992016-09-15 16:27:05 -04002393 uint16_t min_version, max_version;
2394 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2395 return 0;
2396 }
2397
2398 if (max_version <= TLS1_2_VERSION) {
2399 return 1;
2400 }
2401
2402 CBB contents, versions;
2403 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2404 !CBB_add_u16_length_prefixed(out, &contents) ||
2405 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2406 return 0;
2407 }
2408
David Benjamind9791bf2016-09-27 16:39:52 -04002409 /* Add a fake version. See draft-davidben-tls-grease-01. */
2410 if (ssl->ctx->grease_enabled &&
2411 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2412 return 0;
2413 }
2414
Steven Valdezfdd10992016-09-15 16:27:05 -04002415 for (uint16_t version = max_version; version >= min_version; version--) {
2416 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2417 return 0;
2418 }
2419 }
2420
2421 if (!CBB_flush(out)) {
2422 return 0;
2423 }
2424
2425 return 1;
2426}
2427
2428
David Benjamin3baa6e12016-10-07 21:10:38 -04002429/* Cookie
2430 *
2431 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2432
David Benjamin8c880a22016-12-03 02:20:34 -05002433static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2434 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002435 return 1;
2436 }
2437
2438 CBB contents, cookie;
2439 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2440 !CBB_add_u16_length_prefixed(out, &contents) ||
2441 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002442 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002443 !CBB_flush(out)) {
2444 return 0;
2445 }
2446
2447 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002448 OPENSSL_free(hs->cookie);
2449 hs->cookie = NULL;
2450 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002451 return 1;
2452}
2453
2454
David Benjamin6f600d62016-12-21 16:06:54 -05002455/* Short record headers
2456 *
2457 * This is a non-standard extension which negotiates
2458 * https://github.com/tlswg/tls13-spec/pull/762 for experimenting. */
2459
2460static int ext_short_header_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2461 SSL *const ssl = hs->ssl;
2462 uint16_t min_version, max_version;
2463 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2464 return 0;
2465 }
2466
2467 if (max_version < TLS1_3_VERSION ||
2468 !ssl->ctx->short_header_enabled) {
2469 return 1;
2470 }
2471
2472 return CBB_add_u16(out, TLSEXT_TYPE_short_header) &&
2473 CBB_add_u16(out, 0 /* empty extension */);
2474}
2475
2476static int ext_short_header_parse_clienthello(SSL_HANDSHAKE *hs,
2477 uint8_t *out_alert,
2478 CBS *contents) {
2479 SSL *const ssl = hs->ssl;
2480 if (contents == NULL ||
2481 !ssl->ctx->short_header_enabled ||
2482 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2483 return 1;
2484 }
2485
2486 if (CBS_len(contents) != 0) {
2487 return 0;
2488 }
2489
2490 ssl->s3->short_header = 1;
2491 return 1;
2492}
2493
2494
Steven Valdezce902a92016-05-17 11:47:53 -04002495/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002496 *
Steven Valdezce902a92016-05-17 11:47:53 -04002497 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002498 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002499
David Benjamin8c880a22016-12-03 02:20:34 -05002500static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2501 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002502 CBB contents, groups_bytes;
2503 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002504 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002505 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002506 return 0;
2507 }
2508
David Benjamin65ac9972016-09-02 21:35:25 -04002509 /* Add a fake group. See draft-davidben-tls-grease-01. */
2510 if (ssl->ctx->grease_enabled &&
2511 !CBB_add_u16(&groups_bytes,
2512 ssl_get_grease_value(ssl, ssl_grease_group))) {
2513 return 0;
2514 }
2515
Steven Valdezce902a92016-05-17 11:47:53 -04002516 const uint16_t *groups;
2517 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002518 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002519
David Benjamin54091232016-09-05 12:47:25 -04002520 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002521 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002522 return 0;
2523 }
2524 }
2525
2526 return CBB_flush(out);
2527}
2528
David Benjamin8c880a22016-12-03 02:20:34 -05002529static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2530 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002531 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002532 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2533 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002534 return 1;
2535}
2536
David Benjamin8c880a22016-12-03 02:20:34 -05002537static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2538 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002539 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002540 if (contents == NULL) {
2541 return 1;
2542 }
2543
Steven Valdezce902a92016-05-17 11:47:53 -04002544 CBS supported_group_list;
2545 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2546 CBS_len(&supported_group_list) == 0 ||
2547 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002548 CBS_len(contents) != 0) {
2549 return 0;
2550 }
2551
David Benjamin8c880a22016-12-03 02:20:34 -05002552 hs->peer_supported_group_list =
2553 OPENSSL_malloc(CBS_len(&supported_group_list));
2554 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002555 *out_alert = SSL_AD_INTERNAL_ERROR;
2556 return 0;
2557 }
2558
Steven Valdezce902a92016-05-17 11:47:53 -04002559 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002560 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002561 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002562 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002563 goto err;
2564 }
2565 }
2566
Steven Valdezce902a92016-05-17 11:47:53 -04002567 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002568 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002569
2570 return 1;
2571
2572err:
David Benjamin8c880a22016-12-03 02:20:34 -05002573 OPENSSL_free(hs->peer_supported_group_list);
2574 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002575 *out_alert = SSL_AD_INTERNAL_ERROR;
2576 return 0;
2577}
2578
David Benjamin8c880a22016-12-03 02:20:34 -05002579static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002580 /* Servers don't echo this extension. */
2581 return 1;
2582}
2583
2584
Adam Langley614c66a2015-06-12 15:26:58 -07002585/* kExtensions contains all the supported extensions. */
2586static const struct tls_extension kExtensions[] = {
2587 {
Adam Langley5021b222015-06-12 18:27:58 -07002588 TLSEXT_TYPE_renegotiate,
2589 NULL,
2590 ext_ri_add_clienthello,
2591 ext_ri_parse_serverhello,
2592 ext_ri_parse_clienthello,
2593 ext_ri_add_serverhello,
2594 },
2595 {
Adam Langley614c66a2015-06-12 15:26:58 -07002596 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002597 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002598 ext_sni_add_clienthello,
2599 ext_sni_parse_serverhello,
2600 ext_sni_parse_clienthello,
2601 ext_sni_add_serverhello,
2602 },
Adam Langley0a056712015-07-01 15:03:33 -07002603 {
2604 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002605 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002606 ext_ems_add_clienthello,
2607 ext_ems_parse_serverhello,
2608 ext_ems_parse_clienthello,
2609 ext_ems_add_serverhello,
2610 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002611 {
2612 TLSEXT_TYPE_session_ticket,
2613 NULL,
2614 ext_ticket_add_clienthello,
2615 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002616 /* Ticket extension client parsing is handled in ssl_session.c */
2617 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002618 ext_ticket_add_serverhello,
2619 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002620 {
2621 TLSEXT_TYPE_signature_algorithms,
2622 NULL,
2623 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002624 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002625 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002626 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002627 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002628 {
2629 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002630 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002631 ext_ocsp_add_clienthello,
2632 ext_ocsp_parse_serverhello,
2633 ext_ocsp_parse_clienthello,
2634 ext_ocsp_add_serverhello,
2635 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002636 {
2637 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002638 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002639 ext_npn_add_clienthello,
2640 ext_npn_parse_serverhello,
2641 ext_npn_parse_clienthello,
2642 ext_npn_add_serverhello,
2643 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002644 {
2645 TLSEXT_TYPE_certificate_timestamp,
2646 NULL,
2647 ext_sct_add_clienthello,
2648 ext_sct_parse_serverhello,
2649 ext_sct_parse_clienthello,
2650 ext_sct_add_serverhello,
2651 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002652 {
2653 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002654 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002655 ext_alpn_add_clienthello,
2656 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002657 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2658 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002659 ext_alpn_add_serverhello,
2660 },
Adam Langley49c7af12015-07-10 14:33:46 -07002661 {
2662 TLSEXT_TYPE_channel_id,
2663 ext_channel_id_init,
2664 ext_channel_id_add_clienthello,
2665 ext_channel_id_parse_serverhello,
2666 ext_channel_id_parse_clienthello,
2667 ext_channel_id_add_serverhello,
2668 },
Adam Langley391250d2015-07-15 19:06:07 -07002669 {
2670 TLSEXT_TYPE_srtp,
2671 ext_srtp_init,
2672 ext_srtp_add_clienthello,
2673 ext_srtp_parse_serverhello,
2674 ext_srtp_parse_clienthello,
2675 ext_srtp_add_serverhello,
2676 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002677 {
2678 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002679 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002680 ext_ec_point_add_clienthello,
2681 ext_ec_point_parse_serverhello,
2682 ext_ec_point_parse_clienthello,
2683 ext_ec_point_add_serverhello,
2684 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002685 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002686 TLSEXT_TYPE_key_share,
2687 NULL,
2688 ext_key_share_add_clienthello,
2689 forbid_parse_serverhello,
2690 ignore_parse_clienthello,
2691 dont_add_serverhello,
2692 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002693 {
Steven Valdeza833c352016-11-01 13:39:36 -04002694 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002695 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002696 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002697 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002698 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002699 dont_add_serverhello,
2700 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002701 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002702 TLSEXT_TYPE_early_data,
2703 NULL,
2704 ext_early_data_add_clienthello,
2705 forbid_parse_serverhello,
2706 ext_early_data_parse_clienthello,
2707 dont_add_serverhello,
2708 },
2709 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002710 TLSEXT_TYPE_supported_versions,
2711 NULL,
2712 ext_supported_versions_add_clienthello,
2713 forbid_parse_serverhello,
2714 ignore_parse_clienthello,
2715 dont_add_serverhello,
2716 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002717 {
2718 TLSEXT_TYPE_cookie,
2719 NULL,
2720 ext_cookie_add_clienthello,
2721 forbid_parse_serverhello,
2722 ignore_parse_clienthello,
2723 dont_add_serverhello,
2724 },
David Benjamin6f600d62016-12-21 16:06:54 -05002725 {
2726 TLSEXT_TYPE_short_header,
2727 NULL,
2728 ext_short_header_add_clienthello,
2729 forbid_parse_serverhello,
2730 ext_short_header_parse_clienthello,
2731 dont_add_serverhello,
2732 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002733 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2734 * intolerant to the last extension being zero-length. See
2735 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002736 {
Steven Valdezce902a92016-05-17 11:47:53 -04002737 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002738 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002739 ext_supported_groups_add_clienthello,
2740 ext_supported_groups_parse_serverhello,
2741 ext_supported_groups_parse_clienthello,
2742 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002743 },
Adam Langley614c66a2015-06-12 15:26:58 -07002744};
2745
2746#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2747
Adam Langley4cfa96b2015-07-01 11:56:55 -07002748OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002749 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002750 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002751OPENSSL_COMPILE_ASSERT(
2752 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2753 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002754
Adam Langley614c66a2015-06-12 15:26:58 -07002755static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2756 uint16_t value) {
2757 unsigned i;
2758 for (i = 0; i < kNumExtensions; i++) {
2759 if (kExtensions[i].value == value) {
2760 *out_index = i;
2761 return &kExtensions[i];
2762 }
2763 }
2764
2765 return NULL;
2766}
2767
Adam Langley09505632015-07-30 18:10:13 -07002768int SSL_extension_supported(unsigned extension_value) {
2769 uint32_t index;
2770 return extension_value == TLSEXT_TYPE_padding ||
2771 tls_extension_find(&index, extension_value) != NULL;
2772}
2773
David Benjamin8c880a22016-12-03 02:20:34 -05002774int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2775 SSL *const ssl = hs->ssl;
David Benjaminf04c2e92016-12-06 13:35:25 -05002776 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2777 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002778 !ssl->s3->send_connection_binding) {
2779 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002780 }
Adam Langley95c29f32014-06-20 12:00:00 -07002781
David Benjamine8d53502015-10-10 14:13:23 -04002782 CBB extensions;
2783 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002784 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002785 }
Adam Langley95c29f32014-06-20 12:00:00 -07002786
David Benjamin8c880a22016-12-03 02:20:34 -05002787 hs->extensions.sent = 0;
2788 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002789
David Benjamin54091232016-09-05 12:47:25 -04002790 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002791 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002792 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002793 }
2794 }
Adam Langley95c29f32014-06-20 12:00:00 -07002795
David Benjamin65ac9972016-09-02 21:35:25 -04002796 uint16_t grease_ext1 = 0;
2797 if (ssl->ctx->grease_enabled) {
2798 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2799 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2800 if (!CBB_add_u16(&extensions, grease_ext1) ||
2801 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2802 goto err;
2803 }
2804 }
2805
David Benjamin54091232016-09-05 12:47:25 -04002806 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002807 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002808 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002809 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002810 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002811 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002812 }
Adam Langley95c29f32014-06-20 12:00:00 -07002813
Adam Langley33ad2b52015-07-20 17:43:53 -07002814 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002815 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002816 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002817 }
Adam Langley75712922014-10-10 16:23:43 -07002818
David Benjamin2bd19172016-11-17 16:47:15 +09002819 if (!custom_ext_add_clienthello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002820 goto err;
2821 }
2822
David Benjamin65ac9972016-09-02 21:35:25 -04002823 if (ssl->ctx->grease_enabled) {
2824 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2825 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2826
2827 /* The two fake extensions must not have the same value. GREASE values are
2828 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2829 * one. */
2830 if (grease_ext1 == grease_ext2) {
2831 grease_ext2 ^= 0x1010;
2832 }
2833
2834 if (!CBB_add_u16(&extensions, grease_ext2) ||
2835 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2836 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2837 goto err;
2838 }
2839 }
2840
David Benjamince079fd2016-08-02 16:22:34 -04002841 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002842 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002843 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002844 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002845 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002846 *
2847 * NB: because this code works out the length of all existing extensions
2848 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002849 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002850 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002851 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002852 * Server 7.0 is intolerant to the last extension being zero-length. See
2853 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002854 if (padding_len >= 4 + 1) {
2855 padding_len -= 4;
2856 } else {
2857 padding_len = 1;
2858 }
Adam Langley95c29f32014-06-20 12:00:00 -07002859
Adam Langley33ad2b52015-07-20 17:43:53 -07002860 uint8_t *padding_bytes;
2861 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2862 !CBB_add_u16(&extensions, padding_len) ||
2863 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2864 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002865 }
Adam Langley75712922014-10-10 16:23:43 -07002866
David Benjamin17cf2cb2016-12-13 01:07:13 -05002867 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002868 }
2869 }
Adam Langley75712922014-10-10 16:23:43 -07002870
Steven Valdeza833c352016-11-01 13:39:36 -04002871 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002872 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002873 goto err;
2874 }
2875
David Benjamina01deee2015-12-08 18:56:31 -05002876 /* Discard empty extensions blocks. */
2877 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002878 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002879 }
2880
David Benjamine8d53502015-10-10 14:13:23 -04002881 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002882
2883err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002884 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002885 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002886}
Adam Langley95c29f32014-06-20 12:00:00 -07002887
David Benjamin8c880a22016-12-03 02:20:34 -05002888int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2889 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002890 CBB extensions;
2891 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002892 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002893 }
2894
David Benjamin8c880a22016-12-03 02:20:34 -05002895 for (unsigned i = 0; i < kNumExtensions; i++) {
2896 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002897 /* Don't send extensions that were not received. */
2898 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002899 }
Adam Langley95c29f32014-06-20 12:00:00 -07002900
David Benjamin8c880a22016-12-03 02:20:34 -05002901 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002902 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002903 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002904 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002905 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002906 }
Adam Langley95c29f32014-06-20 12:00:00 -07002907
David Benjamin2bd19172016-11-17 16:47:15 +09002908 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002909 goto err;
2910 }
2911
Steven Valdez143e8b32016-07-11 13:19:03 -04002912 /* Discard empty extensions blocks before TLS 1.3. */
2913 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2914 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002915 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002916 }
2917
David Benjamin56380462015-10-10 14:59:09 -04002918 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002919
2920err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002921 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002922 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002923}
Adam Langley95c29f32014-06-20 12:00:00 -07002924
David Benjamin731058e2016-12-03 23:15:13 -05002925static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2926 const SSL_CLIENT_HELLO *client_hello,
2927 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002928 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002929 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002930 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002931 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002932 }
2933 }
2934
David Benjamin8c880a22016-12-03 02:20:34 -05002935 hs->extensions.received = 0;
2936 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002937
David Benjamine14ff062016-08-09 16:21:24 -04002938 CBS extensions;
2939 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2940 while (CBS_len(&extensions) != 0) {
2941 uint16_t type;
2942 CBS extension;
2943
2944 /* Decode the next extension. */
2945 if (!CBS_get_u16(&extensions, &type) ||
2946 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002947 *out_alert = SSL_AD_DECODE_ERROR;
2948 return 0;
2949 }
Adam Langley95c29f32014-06-20 12:00:00 -07002950
David Benjamine14ff062016-08-09 16:21:24 -04002951 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2952 * ambiguous. Ignore all but the renegotiation_info extension. */
2953 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2954 continue;
2955 }
Adam Langley95c29f32014-06-20 12:00:00 -07002956
David Benjamine14ff062016-08-09 16:21:24 -04002957 unsigned ext_index;
2958 const struct tls_extension *const ext =
2959 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002960
David Benjamine14ff062016-08-09 16:21:24 -04002961 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002962 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002963 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002964 return 0;
2965 }
David Benjamine14ff062016-08-09 16:21:24 -04002966 continue;
2967 }
2968
David Benjamin8c880a22016-12-03 02:20:34 -05002969 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002970 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002971 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002972 *out_alert = alert;
2973 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002974 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002975 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002976 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002977 }
Adam Langley75712922014-10-10 16:23:43 -07002978
David Benjamin1deb41b2016-08-09 19:36:38 -04002979 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002980 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002981 continue;
2982 }
2983
2984 CBS *contents = NULL, fake_contents;
2985 static const uint8_t kFakeRenegotiateExtension[] = {0};
2986 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2987 ssl_client_cipher_list_contains_cipher(client_hello,
2988 SSL3_CK_SCSV & 0xffff)) {
2989 /* The renegotiation SCSV was received so pretend that we received a
2990 * renegotiation extension. */
2991 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2992 sizeof(kFakeRenegotiateExtension));
2993 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002994 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002995 }
2996
2997 /* Extension wasn't observed so call the callback with a NULL
2998 * parameter. */
2999 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003000 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04003001 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003002 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04003003 *out_alert = alert;
3004 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07003005 }
3006 }
3007
Adam Langleyfcf25832014-12-18 17:42:32 -08003008 return 1;
3009}
Adam Langley95c29f32014-06-20 12:00:00 -07003010
David Benjamin731058e2016-12-03 23:15:13 -05003011int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
3012 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05003013 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003014 int alert = -1;
David Benjamin8c880a22016-12-03 02:20:34 -05003015 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003016 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003017 return 0;
3018 }
Adam Langley95c29f32014-06-20 12:00:00 -07003019
David Benjamin8c880a22016-12-03 02:20:34 -05003020 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04003021 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08003022 return 0;
3023 }
Adam Langley95c29f32014-06-20 12:00:00 -07003024
Adam Langleyfcf25832014-12-18 17:42:32 -08003025 return 1;
3026}
Adam Langley95c29f32014-06-20 12:00:00 -07003027
David Benjamin8c880a22016-12-03 02:20:34 -05003028static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
3029 int *out_alert) {
3030 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04003031 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
3032 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
3033 return 1;
3034 }
Adam Langley614c66a2015-06-12 15:26:58 -07003035
Steven Valdez143e8b32016-07-11 13:19:03 -04003036 /* Decode the extensions block and check it is valid. */
3037 CBS extensions;
3038 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
3039 !tls1_check_duplicate_extensions(&extensions)) {
3040 *out_alert = SSL_AD_DECODE_ERROR;
3041 return 0;
3042 }
3043
3044 uint32_t received = 0;
3045 while (CBS_len(&extensions) != 0) {
3046 uint16_t type;
3047 CBS extension;
3048
3049 /* Decode the next extension. */
3050 if (!CBS_get_u16(&extensions, &type) ||
3051 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003052 *out_alert = SSL_AD_DECODE_ERROR;
3053 return 0;
3054 }
Adam Langley95c29f32014-06-20 12:00:00 -07003055
Steven Valdez143e8b32016-07-11 13:19:03 -04003056 unsigned ext_index;
3057 const struct tls_extension *const ext =
3058 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07003059
Steven Valdez143e8b32016-07-11 13:19:03 -04003060 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09003061 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003062 return 0;
3063 }
Steven Valdez143e8b32016-07-11 13:19:03 -04003064 continue;
3065 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003066
David Benjamin5db7c9b2017-01-24 16:17:03 -05003067 OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
3068 too_many_bits);
3069
David Benjamin8c880a22016-12-03 02:20:34 -05003070 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04003071 type != TLSEXT_TYPE_renegotiate) {
3072 /* If the extension was never sent then it is illegal, except for the
3073 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04003074 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3075 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04003076 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04003077 return 0;
3078 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003079
Steven Valdez143e8b32016-07-11 13:19:03 -04003080 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07003081
Steven Valdez143e8b32016-07-11 13:19:03 -04003082 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003083 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04003084 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003085 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04003086 *out_alert = alert;
3087 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07003088 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003089 }
Adam Langley95c29f32014-06-20 12:00:00 -07003090
David Benjamin54091232016-09-05 12:47:25 -04003091 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003092 if (!(received & (1u << i))) {
3093 /* Extension wasn't observed so call the callback with a NULL
3094 * parameter. */
3095 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003096 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003097 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003098 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07003099 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08003100 return 0;
3101 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003102 }
3103 }
Adam Langley95c29f32014-06-20 12:00:00 -07003104
Adam Langleyfcf25832014-12-18 17:42:32 -08003105 return 1;
3106}
Adam Langley95c29f32014-06-20 12:00:00 -07003107
David Benjamin8c880a22016-12-03 02:20:34 -05003108static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3109 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003110 int ret = SSL_TLSEXT_ERR_NOACK;
3111 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003112
David Benjamin78f8aab2016-03-10 16:33:58 -05003113 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003114 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003115 ssl->ctx->tlsext_servername_arg);
3116 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003117 ret = ssl->initial_ctx->tlsext_servername_callback(
3118 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003119 }
Adam Langley95c29f32014-06-20 12:00:00 -07003120
Adam Langleyfcf25832014-12-18 17:42:32 -08003121 switch (ret) {
3122 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003123 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003124 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003125
Adam Langleyfcf25832014-12-18 17:42:32 -08003126 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003127 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003128 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003129
Adam Langleyfcf25832014-12-18 17:42:32 -08003130 default:
3131 return 1;
3132 }
3133}
Adam Langleyed8270a2014-09-02 13:52:56 -07003134
David Benjamin8c880a22016-12-03 02:20:34 -05003135int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3136 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003137 int alert = -1;
David Benjamin8c880a22016-12-03 02:20:34 -05003138 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003139 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003140 return 0;
3141 }
3142
Adam Langleyfcf25832014-12-18 17:42:32 -08003143 return 1;
3144}
Adam Langley95c29f32014-06-20 12:00:00 -07003145
David Benjamine3aa1d92015-06-16 15:34:50 -04003146int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05003147 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04003148 size_t ticket_len, const uint8_t *session_id,
3149 size_t session_id_len) {
3150 int ret = 1; /* Most errors are non-fatal. */
3151 SSL_CTX *ssl_ctx = ssl->initial_ctx;
3152 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003153
David Benjamine3aa1d92015-06-16 15:34:50 -04003154 HMAC_CTX hmac_ctx;
3155 HMAC_CTX_init(&hmac_ctx);
3156 EVP_CIPHER_CTX cipher_ctx;
3157 EVP_CIPHER_CTX_init(&cipher_ctx);
3158
David Benjaminef1b0092015-11-21 14:05:44 -05003159 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04003160 *out_session = NULL;
3161
Steven Valdez4aa154e2016-07-29 14:32:55 -04003162 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
3163 goto done;
3164 }
3165
David Benjamine3aa1d92015-06-16 15:34:50 -04003166 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3167 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003168 }
3169
David Benjaminadcc3952015-04-26 13:07:57 -04003170 /* Ensure there is room for the key name and the largest IV
3171 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3172 * the maximum IV length should be well under the minimum size for the
3173 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003174 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3175 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003176 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003177 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003178
David Benjamine3aa1d92015-06-16 15:34:50 -04003179 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003180 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3181 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3182 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003183 if (cb_ret < 0) {
3184 ret = 0;
3185 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003186 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003187 if (cb_ret == 0) {
3188 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003189 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003190 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003191 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003192 }
3193 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003194 /* Check the key name matches. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003195 if (OPENSSL_memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3196 SSL_TICKET_KEY_NAME_LEN) != 0) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003197 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003198 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003199 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3200 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003201 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003202 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3203 ssl_ctx->tlsext_tick_aes_key, iv)) {
3204 ret = 0;
3205 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003206 }
3207 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003208 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003209
David Benjamine3aa1d92015-06-16 15:34:50 -04003210 /* Check the MAC at the end of the ticket. */
3211 uint8_t mac[EVP_MAX_MD_SIZE];
3212 size_t mac_len = HMAC_size(&hmac_ctx);
3213 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003214 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003215 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003216 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003217 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3218 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003219 int mac_ok =
3220 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3221#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3222 mac_ok = 1;
3223#endif
3224 if (!mac_ok) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003225 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003226 }
3227
David Benjamine3aa1d92015-06-16 15:34:50 -04003228 /* Decrypt the session data. */
3229 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3230 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3231 mac_len;
3232 plaintext = OPENSSL_malloc(ciphertext_len);
3233 if (plaintext == NULL) {
3234 ret = 0;
3235 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003236 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003237 size_t plaintext_len;
3238#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin17cf2cb2016-12-13 01:07:13 -05003239 OPENSSL_memcpy(plaintext, ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003240 plaintext_len = ciphertext_len;
3241#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003242 if (ciphertext_len >= INT_MAX) {
3243 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003244 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003245 int len1, len2;
3246 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3247 (int)ciphertext_len) ||
3248 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
3249 ERR_clear_error(); /* Don't leave an error on the queue. */
3250 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003251 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003252 plaintext_len = (size_t)(len1 + len2);
3253#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003254
David Benjamine3aa1d92015-06-16 15:34:50 -04003255 /* Decode the session. */
David Benjaminfbc45d72016-09-22 01:21:24 -04003256 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003257 if (session == NULL) {
3258 ERR_clear_error(); /* Don't leave an error on the queue. */
3259 goto done;
3260 }
3261
3262 /* Copy the client's session ID into the new session, to denote the ticket has
3263 * been accepted. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003264 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003265 session->session_id_length = session_id_len;
3266
3267 *out_session = session;
3268
3269done:
3270 OPENSSL_free(plaintext);
3271 HMAC_CTX_cleanup(&hmac_ctx);
3272 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3273 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003274}
Adam Langley95c29f32014-06-20 12:00:00 -07003275
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003276int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003277 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003278 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003279 return 1;
3280 }
David Benjamincd996942014-07-20 16:23:51 -04003281
David Benjamin0fc37ef2016-08-17 15:29:46 -04003282 OPENSSL_free(hs->peer_sigalgs);
3283 hs->peer_sigalgs = NULL;
3284 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003285
3286 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003287 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003288 return 0;
3289 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003290 num_sigalgs /= 2;
3291
3292 /* supported_signature_algorithms in the certificate request is
3293 * allowed to be empty. */
3294 if (num_sigalgs == 0) {
3295 return 1;
3296 }
3297
Steven Valdez02563852016-06-23 13:33:05 -04003298 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3299 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003300 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3301 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003302 return 0;
3303 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003304 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003305
3306 CBS sigalgs;
3307 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003308 for (size_t i = 0; i < num_sigalgs; i++) {
3309 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003310 return 0;
3311 }
3312 }
Adam Langley95c29f32014-06-20 12:00:00 -07003313
Adam Langleyfcf25832014-12-18 17:42:32 -08003314 return 1;
3315}
David Benjaminec2f27d2014-11-13 19:17:25 -05003316
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003317int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3318 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003319 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003320
Steven Valdezf0451ca2016-06-29 13:16:27 -04003321 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3322 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3323 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003324 int type = ssl_private_key_type(ssl);
3325 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003326 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003327 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003328 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003329 if (ssl_is_ecdsa_key_type(type)) {
3330 *out = SSL_SIGN_ECDSA_SHA1;
3331 return 1;
3332 }
3333 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3334 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003335 }
3336
David Benjamin3ef76972016-10-17 17:59:54 -04003337 const uint16_t *sigalgs = cert->sigalgs;
3338 size_t num_sigalgs = cert->num_sigalgs;
3339 if (sigalgs == NULL) {
3340 sigalgs = kSignSignatureAlgorithms;
3341 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003342 }
3343
David Benjamin0fc37ef2016-08-17 15:29:46 -04003344 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3345 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3346 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003347 /* If the client didn't specify any signature_algorithms extension then
3348 * we can assume that it supports SHA1. See
3349 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3350 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3351 SSL_SIGN_ECDSA_SHA1};
3352 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003353 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003354 }
3355
David Benjamin0fc37ef2016-08-17 15:29:46 -04003356 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003357 uint16_t sigalg = sigalgs[i];
3358 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3359 * negotiated. */
3360 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3361 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3362 continue;
3363 }
3364
David Benjamin0fc37ef2016-08-17 15:29:46 -04003365 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003366 if (sigalg == peer_sigalgs[j]) {
3367 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003368 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003369 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003370 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003371 }
Adam Langley95c29f32014-06-20 12:00:00 -07003372
David Benjaminea9a0d52016-07-08 15:52:59 -07003373 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3374 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003375}
Adam Langley95c29f32014-06-20 12:00:00 -07003376
Nick Harper60a85cb2016-09-23 16:25:11 -07003377int tls1_verify_channel_id(SSL *ssl) {
3378 int ret = 0;
3379 uint16_t extension_type;
3380 CBS extension, channel_id;
3381
3382 /* A Channel ID handshake message is structured to contain multiple
3383 * extensions, but the only one that can be present is Channel ID. */
3384 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3385 if (!CBS_get_u16(&channel_id, &extension_type) ||
3386 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3387 CBS_len(&channel_id) != 0 ||
3388 extension_type != TLSEXT_TYPE_channel_id ||
3389 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3390 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3391 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3392 return 0;
3393 }
3394
3395 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3396 if (!p256) {
3397 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3398 return 0;
3399 }
3400
3401 EC_KEY *key = NULL;
3402 EC_POINT *point = NULL;
3403 BIGNUM x, y;
3404 ECDSA_SIG sig;
3405 BN_init(&x);
3406 BN_init(&y);
3407 sig.r = BN_new();
3408 sig.s = BN_new();
3409 if (sig.r == NULL || sig.s == NULL) {
3410 goto err;
3411 }
3412
3413 const uint8_t *p = CBS_data(&extension);
3414 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3415 BN_bin2bn(p + 32, 32, &y) == NULL ||
3416 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3417 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3418 goto err;
3419 }
3420
3421 point = EC_POINT_new(p256);
3422 if (point == NULL ||
3423 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3424 goto err;
3425 }
3426
3427 key = EC_KEY_new();
3428 if (key == NULL ||
3429 !EC_KEY_set_group(key, p256) ||
3430 !EC_KEY_set_public_key(key, point)) {
3431 goto err;
3432 }
3433
3434 uint8_t digest[EVP_MAX_MD_SIZE];
3435 size_t digest_len;
3436 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3437 goto err;
3438 }
3439
3440 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3441#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3442 sig_ok = 1;
3443#endif
3444 if (!sig_ok) {
3445 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3446 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3447 ssl->s3->tlsext_channel_id_valid = 0;
3448 goto err;
3449 }
3450
David Benjamin17cf2cb2016-12-13 01:07:13 -05003451 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
Nick Harper60a85cb2016-09-23 16:25:11 -07003452 ret = 1;
3453
3454err:
3455 BN_free(&x);
3456 BN_free(&y);
3457 BN_free(sig.r);
3458 BN_free(sig.s);
3459 EC_KEY_free(key);
3460 EC_POINT_free(point);
3461 EC_GROUP_free(p256);
3462 return ret;
3463}
3464
3465int tls1_write_channel_id(SSL *ssl, CBB *cbb) {
3466 uint8_t digest[EVP_MAX_MD_SIZE];
3467 size_t digest_len;
3468 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3469 return 0;
3470 }
3471
3472 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3473 if (ec_key == NULL) {
3474 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3475 return 0;
3476 }
3477
3478 int ret = 0;
3479 BIGNUM *x = BN_new();
3480 BIGNUM *y = BN_new();
3481 ECDSA_SIG *sig = NULL;
3482 if (x == NULL || y == NULL ||
3483 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3484 EC_KEY_get0_public_key(ec_key),
3485 x, y, NULL)) {
3486 goto err;
3487 }
3488
3489 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3490 if (sig == NULL) {
3491 goto err;
3492 }
3493
3494 CBB child;
3495 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3496 !CBB_add_u16_length_prefixed(cbb, &child) ||
3497 !BN_bn2cbb_padded(&child, 32, x) ||
3498 !BN_bn2cbb_padded(&child, 32, y) ||
3499 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3500 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3501 !CBB_flush(cbb)) {
3502 goto err;
3503 }
3504
3505 ret = 1;
3506
3507err:
3508 BN_free(x);
3509 BN_free(y);
3510 ECDSA_SIG_free(sig);
3511 return ret;
3512}
3513
David Benjamind6a4ae92015-08-06 11:10:51 -04003514int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003515 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3516 uint8_t *msg;
3517 size_t msg_len;
3518 if (!tls13_get_cert_verify_signature_input(ssl, &msg, &msg_len,
3519 ssl_cert_verify_channel_id)) {
3520 return 0;
3521 }
3522 SHA256(msg, msg_len, out);
3523 *out_len = SHA256_DIGEST_LENGTH;
3524 OPENSSL_free(msg);
3525 return 1;
3526 }
3527
Nick Harper95594012016-10-20 14:07:13 -07003528 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003529
Nick Harper95594012016-10-20 14:07:13 -07003530 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003531 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003532 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003533
Steven Valdez87eab492016-06-27 16:34:59 -04003534 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003535 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003536 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003537 if (ssl->session->original_handshake_hash_len == 0) {
3538 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003539 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003540 }
Nick Harper95594012016-10-20 14:07:13 -07003541 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3542 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003543 }
3544
3545 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3546 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3547 sizeof(handshake_hash));
3548 if (handshake_hash_len < 0) {
Nick Harper95594012016-10-20 14:07:13 -07003549 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003550 }
Nick Harper95594012016-10-20 14:07:13 -07003551 SHA256_Update(&ctx, handshake_hash, (size_t)handshake_hash_len);
3552 SHA256_Final(out, &ctx);
3553 *out_len = SHA256_DIGEST_LENGTH;
3554 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003555}
Adam Langley1258b6a2014-06-20 12:00:00 -07003556
3557/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003558 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003559 * data. */
3560int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003561 int digest_len;
3562 /* This function should never be called for a resumed session because the
3563 * handshake hashes that we wish to record are for the original, full
3564 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003565 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003566 return -1;
3567 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003568
Adam Langleyfcf25832014-12-18 17:42:32 -08003569 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003570 tls1_handshake_digest(
3571 ssl, ssl->s3->new_session->original_handshake_hash,
3572 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003573 if (digest_len < 0) {
3574 return -1;
3575 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003576
David Benjamin30c4c302016-12-07 22:35:24 -05003577 assert(sizeof(ssl->s3->new_session->original_handshake_hash) < 256);
3578 ssl->s3->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003579
Adam Langleyfcf25832014-12-18 17:42:32 -08003580 return 1;
3581}
Nick Harper60a85cb2016-09-23 16:25:11 -07003582
3583int ssl_do_channel_id_callback(SSL *ssl) {
3584 if (ssl->tlsext_channel_id_private != NULL ||
3585 ssl->ctx->channel_id_cb == NULL) {
3586 return 1;
3587 }
3588
3589 EVP_PKEY *key = NULL;
3590 ssl->ctx->channel_id_cb(ssl, &key);
3591 if (key == NULL) {
3592 /* The caller should try again later. */
3593 return 1;
3594 }
3595
3596 int ret = SSL_set1_tls_channel_id(ssl, key);
3597 EVP_PKEY_free(key);
3598 return ret;
3599}
Adam Langleycfa08c32016-11-17 13:21:27 -08003600
3601int ssl_is_sct_list_valid(const CBS *contents) {
3602 /* Shallow parse the SCT list for sanity. By the RFC
3603 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3604 * of the SCTs may be empty. */
3605 CBS copy = *contents;
3606 CBS sct_list;
3607 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3608 CBS_len(&copy) != 0 ||
3609 CBS_len(&sct_list) == 0) {
3610 return 0;
3611 }
3612
3613 while (CBS_len(&sct_list) > 0) {
3614 CBS sct;
3615 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3616 CBS_len(&sct) == 0) {
3617 return 0;
3618 }
3619 }
3620
3621 return 1;
3622}