blob: fb0c8dd220c03df9620e26523abef70c17eb5e96 [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
David Benjamin81678aa2017-07-12 22:43:42 -0400171 extension_types =
172 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400174 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800175 goto done;
176 }
David Benjamin35a7a442014-07-05 00:23:20 -0400177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 /* Second pass: gather the extension types. */
179 extensions = *cbs;
180 for (i = 0; i < num_extensions; i++) {
181 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400182
Adam Langleyfcf25832014-12-18 17:42:32 -0800183 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
184 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
185 /* This should not happen. */
186 goto done;
187 }
188 }
189 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400190
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 /* Sort the extensions and make sure there are no duplicates. */
192 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
193 for (i = 1; i < num_extensions; i++) {
194 if (extension_types[i - 1] == extension_types[i]) {
195 goto done;
196 }
197 }
David Benjamin35a7a442014-07-05 00:23:20 -0400198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 ret = 1;
200
David Benjamin35a7a442014-07-05 00:23:20 -0400201done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400202 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 return ret;
204}
David Benjamin35a7a442014-07-05 00:23:20 -0400205
David Benjamin731058e2016-12-03 23:15:13 -0500206int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out, const uint8_t *in,
207 size_t in_len) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500208 OPENSSL_memset(out, 0, sizeof(*out));
David Benjamin731058e2016-12-03 23:15:13 -0500209 out->ssl = ssl;
210 out->client_hello = in;
211 out->client_hello_len = in_len;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400212
David Benjamine14ff062016-08-09 16:21:24 -0400213 CBS client_hello, random, session_id;
David Benjamin731058e2016-12-03 23:15:13 -0500214 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
215 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjamine14ff062016-08-09 16:21:24 -0400216 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
217 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
218 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 return 0;
220 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700221
David Benjamin731058e2016-12-03 23:15:13 -0500222 out->random = CBS_data(&random);
223 out->random_len = CBS_len(&random);
224 out->session_id = CBS_data(&session_id);
225 out->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700226
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 /* Skip past DTLS cookie */
David Benjamin731058e2016-12-03 23:15:13 -0500228 if (SSL_is_dtls(out->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400230 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
231 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 return 0;
233 }
234 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700235
David Benjamine14ff062016-08-09 16:21:24 -0400236 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400238 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
239 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 CBS_len(&compression_methods) < 1) {
241 return 0;
242 }
David Benjamine14ff062016-08-09 16:21:24 -0400243
David Benjamin731058e2016-12-03 23:15:13 -0500244 out->cipher_suites = CBS_data(&cipher_suites);
245 out->cipher_suites_len = CBS_len(&cipher_suites);
246 out->compression_methods = CBS_data(&compression_methods);
247 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 /* If the ClientHello ends here then it's valid, but doesn't have any
250 * extensions. (E.g. SSLv3.) */
251 if (CBS_len(&client_hello) == 0) {
David Benjamin731058e2016-12-03 23:15:13 -0500252 out->extensions = NULL;
253 out->extensions_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 return 1;
255 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 /* Extract extensions and check it is valid. */
David Benjamine14ff062016-08-09 16:21:24 -0400258 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
260 !tls1_check_duplicate_extensions(&extensions) ||
261 CBS_len(&client_hello) != 0) {
262 return 0;
263 }
David Benjamine14ff062016-08-09 16:21:24 -0400264
David Benjamin731058e2016-12-03 23:15:13 -0500265 out->extensions = CBS_data(&extensions);
266 out->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700267
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 return 1;
269}
Adam Langleydc9b1412014-06-20 12:00:00 -0700270
David Benjamin731058e2016-12-03 23:15:13 -0500271int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
272 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 CBS extensions;
David Benjamin731058e2016-12-03 23:15:13 -0500274 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 while (CBS_len(&extensions) != 0) {
David Benjamincec73442016-08-02 17:41:33 -0400276 /* Decode the next extension. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 uint16_t type;
278 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 if (!CBS_get_u16(&extensions, &type) ||
280 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
281 return 0;
282 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400285 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800286 return 1;
287 }
288 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700289
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 return 0;
291}
Adam Langley95c29f32014-06-20 12:00:00 -0700292
David Benjamin731058e2016-12-03 23:15:13 -0500293int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
294 uint16_t extension_type,
295 const uint8_t **out_data,
296 size_t *out_len) {
David Benjamincec73442016-08-02 17:41:33 -0400297 CBS cbs;
David Benjamin731058e2016-12-03 23:15:13 -0500298 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
David Benjamincec73442016-08-02 17:41:33 -0400299 return 0;
300 }
301
302 *out_data = CBS_data(&cbs);
303 *out_len = CBS_len(&cbs);
304 return 1;
305}
306
Steven Valdezce902a92016-05-17 11:47:53 -0400307static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400308 SSL_CURVE_X25519,
309 SSL_CURVE_SECP256R1,
310 SSL_CURVE_SECP384R1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800311};
Adam Langley95c29f32014-06-20 12:00:00 -0700312
David Benjaminf04976b2016-10-07 00:37:55 -0400313void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
Steven Valdez5440fe02016-07-18 12:40:30 -0400314 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400315 *out_group_ids = ssl->supported_group_list;
316 *out_group_ids_len = ssl->supported_group_list_len;
317 if (!*out_group_ids) {
318 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400319 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 }
321}
David Benjamined439582014-07-14 19:13:02 -0400322
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900323int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
324 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400325 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400326
David Benjaminf04976b2016-10-07 00:37:55 -0400327 const uint16_t *groups, *pref, *supp;
328 size_t groups_len, pref_len, supp_len;
329 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800330
David Benjaminf04976b2016-10-07 00:37:55 -0400331 /* Clients are not required to send a supported_groups extension. In this
332 * case, the server is free to pick any group it likes. See RFC 4492,
333 * section 4, paragraph 3.
334 *
335 * However, in the interests of compatibility, we will skip ECDH if the
336 * client didn't send an extension because we can't be sure that they'll
337 * support our favoured group. Thus we do not special-case an emtpy
338 * |peer_supported_group_list|. */
David Benjamin55a43642015-04-20 14:45:55 -0400339
David Benjamin4298d772015-12-19 00:18:25 -0500340 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400341 pref = groups;
342 pref_len = groups_len;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900343 supp = hs->peer_supported_group_list;
344 supp_len = hs->peer_supported_group_list_len;
David Benjamin55a43642015-04-20 14:45:55 -0400345 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900346 pref = hs->peer_supported_group_list;
347 pref_len = hs->peer_supported_group_list_len;
Steven Valdezce902a92016-05-17 11:47:53 -0400348 supp = groups;
349 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400350 }
351
David Benjaminf04976b2016-10-07 00:37:55 -0400352 for (size_t i = 0; i < pref_len; i++) {
353 for (size_t j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400355 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500356 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 }
358 }
359 }
360
David Benjamin4298d772015-12-19 00:18:25 -0500361 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800362}
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Steven Valdezce902a92016-05-17 11:47:53 -0400364int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 const int *curves, size_t ncurves) {
David Benjamin81678aa2017-07-12 22:43:42 -0400366 uint16_t *group_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
Steven Valdezce902a92016-05-17 11:47:53 -0400367 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 return 0;
369 }
370
David Benjamin54091232016-09-05 12:47:25 -0400371 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400372 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
373 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 return 0;
375 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 }
377
Steven Valdezce902a92016-05-17 11:47:53 -0400378 OPENSSL_free(*out_group_ids);
379 *out_group_ids = group_ids;
380 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800381
382 return 1;
383}
Adam Langley95c29f32014-06-20 12:00:00 -0700384
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100385int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
386 const char *curves) {
387 uint16_t *group_ids = NULL;
388 size_t ncurves = 0;
389
390 const char *col;
391 const char *ptr = curves;
392
393 do {
394 col = strchr(ptr, ':');
395
396 uint16_t group_id;
397 if (!ssl_name_to_group_id(&group_id, ptr,
398 col ? (size_t)(col - ptr) : strlen(ptr))) {
399 goto err;
400 }
401
David Benjamin81678aa2017-07-12 22:43:42 -0400402 uint16_t *new_group_ids = (uint16_t *)OPENSSL_realloc(
403 group_ids, (ncurves + 1) * sizeof(uint16_t));
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100404 if (new_group_ids == NULL) {
405 goto err;
406 }
407 group_ids = new_group_ids;
408
409 group_ids[ncurves] = group_id;
410 ncurves++;
411
412 if (col) {
413 ptr = col + 1;
414 }
415 } while (col);
416
417 OPENSSL_free(*out_group_ids);
418 *out_group_ids = group_ids;
419 *out_group_ids_len = ncurves;
420
421 return 1;
422
423err:
424 OPENSSL_free(group_ids);
425 return 0;
426}
427
Steven Valdezce902a92016-05-17 11:47:53 -0400428int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
429 const uint16_t *groups;
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400430 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -0400431 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400432 for (size_t i = 0; i < groups_len; i++) {
433 if (groups[i] == group_id) {
434 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800435 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800436 }
David Benjamin033e5f42014-11-13 18:47:41 -0500437
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400438 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800439}
David Benjamin033e5f42014-11-13 18:47:41 -0500440
David Benjamin3ef76972016-10-17 17:59:54 -0400441/* kVerifySignatureAlgorithms is the default list of accepted signature
David Benjamin3a322f52016-10-26 12:45:35 -0400442 * algorithms for verifying.
443 *
444 * For now, RSA-PSS signature algorithms are not enabled on Android's system
445 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
446 * restore them. */
David Benjamin3ef76972016-10-17 17:59:54 -0400447static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjamin69522112017-03-28 15:38:29 -0500448 /* List our preferred algorithms first. */
449 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400450 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[] = {
David Benjamin69522112017-03-28 15:38:29 -0500484 /* List our preferred algorithms first. */
485 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400486 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400487#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400488 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400489#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400490 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400491
David Benjamin3a322f52016-10-26 12:45:35 -0400492 /* If needed, sign larger hashes.
493 *
494 * TODO(davidben): Determine which of these may be pruned. */
495 SSL_SIGN_ECDSA_SECP384R1_SHA384,
496#if !defined(BORINGSSL_ANDROID_SYSTEM)
497 SSL_SIGN_RSA_PSS_SHA384,
498#endif
499 SSL_SIGN_RSA_PKCS1_SHA384,
500
501 SSL_SIGN_ECDSA_SECP521R1_SHA512,
502#if !defined(BORINGSSL_ANDROID_SYSTEM)
503 SSL_SIGN_RSA_PSS_SHA512,
504#endif
505 SSL_SIGN_RSA_PKCS1_SHA512,
506
507 /* If the peer supports nothing else, sign with SHA-1. */
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400508 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400509 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400510};
511
David Benjamin69522112017-03-28 15:38:29 -0500512void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
513 ctx->ed25519_enabled = !!enabled;
514}
515
516int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) {
David Benjamin71c21b42017-04-14 17:05:40 -0400517 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
518 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
519 if (ssl->ctx->num_verify_sigalgs != 0) {
520 sigalgs = ssl->ctx->verify_sigalgs;
521 num_sigalgs = ssl->ctx->num_verify_sigalgs;
522 }
523
524 for (size_t i = 0; i < num_sigalgs; i++) {
525 if (sigalgs == kVerifySignatureAlgorithms &&
526 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500527 !ssl->ctx->ed25519_enabled) {
528 continue;
529 }
David Benjamin71c21b42017-04-14 17:05:40 -0400530 if (!CBB_add_u16(out, sigalgs[i])) {
David Benjamin69522112017-03-28 15:38:29 -0500531 return 0;
532 }
533 }
534
535 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800536}
Adam Langley95c29f32014-06-20 12:00:00 -0700537
David Benjamin8d606e32017-06-15 22:43:04 -0400538int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg) {
David Benjamin71c21b42017-04-14 17:05:40 -0400539 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
540 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
541 if (ssl->ctx->num_verify_sigalgs != 0) {
542 sigalgs = ssl->ctx->verify_sigalgs;
543 num_sigalgs = ssl->ctx->num_verify_sigalgs;
544 }
545
546 for (size_t i = 0; i < num_sigalgs; i++) {
547 if (sigalgs == kVerifySignatureAlgorithms &&
548 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500549 !ssl->ctx->ed25519_enabled) {
550 continue;
551 }
David Benjamin71c21b42017-04-14 17:05:40 -0400552 if (sigalg == sigalgs[i]) {
David Benjamin3ef76972016-10-17 17:59:54 -0400553 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800554 }
555 }
556
David Benjamin3ef76972016-10-17 17:59:54 -0400557 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
558 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
559 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800560}
561
Adam Langley614c66a2015-06-12 15:26:58 -0700562/* tls_extension represents a TLS extension that is handled internally. The
563 * |init| function is called for each handshake, before any other functions of
564 * the extension. Then the add and parse callbacks are called as needed.
565 *
566 * The parse callbacks receive a |CBS| that contains the contents of the
567 * extension (i.e. not including the type and length bytes). If an extension is
568 * not received then the parse callbacks will be called with a NULL CBS so that
569 * they can do any processing needed to handle the absence of an extension.
570 *
571 * The add callbacks receive a |CBB| to which the extension can be appended but
572 * the function is responsible for appending the type and length bytes too.
573 *
574 * All callbacks return one for success and zero for error. If a parse function
575 * returns zero then a fatal alert with value |*out_alert| will be sent. If
576 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
577struct tls_extension {
578 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500579 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700580
David Benjamin8c880a22016-12-03 02:20:34 -0500581 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
582 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
583 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700584
David Benjamin8c880a22016-12-03 02:20:34 -0500585 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
586 CBS *contents);
587 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700588};
589
David Benjamin8c880a22016-12-03 02:20:34 -0500590static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
591 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400592 if (contents != NULL) {
593 /* Servers MUST NOT send this extension. */
594 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
595 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
596 return 0;
597 }
598
599 return 1;
600}
601
David Benjamin8c880a22016-12-03 02:20:34 -0500602static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
603 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400604 /* This extension from the client is handled elsewhere. */
605 return 1;
606}
607
David Benjamin8c880a22016-12-03 02:20:34 -0500608static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400609 return 1;
610}
Adam Langley614c66a2015-06-12 15:26:58 -0700611
612/* Server name indication (SNI).
613 *
614 * https://tools.ietf.org/html/rfc6066#section-3. */
615
David Benjamin8c880a22016-12-03 02:20:34 -0500616static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
617 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700618 if (ssl->tlsext_hostname == NULL) {
619 return 1;
620 }
621
622 CBB contents, server_name_list, name;
623 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
624 !CBB_add_u16_length_prefixed(out, &contents) ||
625 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
626 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
627 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
628 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
629 strlen(ssl->tlsext_hostname)) ||
630 !CBB_flush(out)) {
631 return 0;
632 }
633
634 return 1;
635}
636
David Benjamin8c880a22016-12-03 02:20:34 -0500637static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500638 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500639 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700640 if (contents == NULL) {
641 return 1;
642 }
643
644 if (CBS_len(contents) != 0) {
645 return 0;
646 }
647
648 assert(ssl->tlsext_hostname != NULL);
649
Steven Valdez87eab492016-06-27 16:34:59 -0400650 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500651 OPENSSL_free(hs->new_session->tlsext_hostname);
652 hs->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
653 if (!hs->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700654 *out_alert = SSL_AD_INTERNAL_ERROR;
655 return 0;
656 }
657 }
658
659 return 1;
660}
661
David Benjamin8c880a22016-12-03 02:20:34 -0500662static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500663 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700664 if (contents == NULL) {
665 return 1;
666 }
667
David Benjamin9b611e22016-03-03 08:48:30 -0500668 CBS server_name_list, host_name;
669 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700670 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500671 !CBS_get_u8(&server_name_list, &name_type) ||
672 /* Although the server_name extension was intended to be extensible to
673 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
674 * different name types will cause an error. Further, RFC 4366 originally
675 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
676 * adding new name types is no longer feasible.
677 *
678 * Act as if the extensibility does not exist to simplify parsing. */
679 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
680 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700681 CBS_len(contents) != 0) {
682 return 0;
683 }
684
David Benjamin9b611e22016-03-03 08:48:30 -0500685 if (name_type != TLSEXT_NAMETYPE_host_name ||
686 CBS_len(&host_name) == 0 ||
687 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
688 CBS_contains_zero_byte(&host_name)) {
689 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
690 return 0;
691 }
Adam Langley614c66a2015-06-12 15:26:58 -0700692
David Benjamin4eb95cc2016-11-16 17:08:23 +0900693 /* Copy the hostname as a string. */
David Benjamin8c880a22016-12-03 02:20:34 -0500694 if (!CBS_strdup(&host_name, &hs->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900695 *out_alert = SSL_AD_INTERNAL_ERROR;
696 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700697 }
698
David Benjamin8c880a22016-12-03 02:20:34 -0500699 hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700700 return 1;
701}
702
David Benjamin8c880a22016-12-03 02:20:34 -0500703static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
704 if (hs->ssl->s3->session_reused ||
705 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700706 return 1;
707 }
708
709 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
710 !CBB_add_u16(out, 0 /* length */)) {
711 return 0;
712 }
713
714 return 1;
715}
716
717
Adam Langley5021b222015-06-12 18:27:58 -0700718/* Renegotiation indication.
719 *
720 * https://tools.ietf.org/html/rfc5746 */
721
David Benjamin8c880a22016-12-03 02:20:34 -0500722static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
723 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400724 /* Renegotiation indication is not necessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -0400725 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400726 return 1;
727 }
728
David Benjamin52bf6902016-10-08 12:05:03 -0400729 assert(ssl->s3->initial_handshake_complete ==
730 (ssl->s3->previous_client_finished_len != 0));
731
Adam Langley5021b222015-06-12 18:27:58 -0700732 CBB contents, prev_finished;
733 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
734 !CBB_add_u16_length_prefixed(out, &contents) ||
735 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
736 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
737 ssl->s3->previous_client_finished_len) ||
738 !CBB_flush(out)) {
739 return 0;
740 }
741
742 return 1;
743}
744
David Benjamin8c880a22016-12-03 02:20:34 -0500745static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700746 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500747 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400748 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez246eeee2017-03-26 12:49:17 -0500749 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400750 return 0;
751 }
752
David Benjamin3e052de2015-11-25 20:10:31 -0500753 /* Servers may not switch between omitting the extension and supporting it.
754 * See RFC 5746, sections 3.5 and 4.2. */
755 if (ssl->s3->initial_handshake_complete &&
756 (contents != NULL) != ssl->s3->send_connection_binding) {
757 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
758 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
759 return 0;
760 }
761
Adam Langley5021b222015-06-12 18:27:58 -0700762 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500763 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700764 * RI even on initial ServerHello because the client doesn't see any
765 * renegotiation during an attack. However this would mean we could not
766 * connect to any server which doesn't support RI.
767 *
David Benjamine9cddb82015-11-23 14:36:40 -0500768 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
769 * practical terms every client sets it so it's just assumed here. */
770 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700771 }
772
773 const size_t expected_len = ssl->s3->previous_client_finished_len +
774 ssl->s3->previous_server_finished_len;
775
776 /* Check for logic errors */
777 assert(!expected_len || ssl->s3->previous_client_finished_len);
778 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400779 assert(ssl->s3->initial_handshake_complete ==
780 (ssl->s3->previous_client_finished_len != 0));
781 assert(ssl->s3->initial_handshake_complete ==
782 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700783
784 /* Parse out the extension contents. */
785 CBS renegotiated_connection;
786 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
787 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400788 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700789 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
790 return 0;
791 }
792
793 /* Check that the extension matches. */
794 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400795 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700796 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
797 return 0;
798 }
799
800 const uint8_t *d = CBS_data(&renegotiated_connection);
David Benjamin9343b0b2017-07-01 00:31:27 -0400801 int ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
802 ssl->s3->previous_client_finished_len) == 0;
803#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
804 ok = 1;
805#endif
806 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400807 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700808 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
809 return 0;
810 }
811 d += ssl->s3->previous_client_finished_len;
812
David Benjamin9343b0b2017-07-01 00:31:27 -0400813 ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
814 ssl->s3->previous_server_finished_len) == 0;
815#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
816 ok = 1;
817#endif
818 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400819 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700820 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
821 return 0;
822 }
823 ssl->s3->send_connection_binding = 1;
824
825 return 1;
826}
827
David Benjamin8c880a22016-12-03 02:20:34 -0500828static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700829 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500830 SSL *const ssl = hs->ssl;
Adam Langley5021b222015-06-12 18:27:58 -0700831 /* Renegotiation isn't supported as a server so this function should never be
832 * called after the initial handshake. */
833 assert(!ssl->s3->initial_handshake_complete);
834
Steven Valdez143e8b32016-07-11 13:19:03 -0400835 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
836 return 1;
837 }
838
Adam Langley5021b222015-06-12 18:27:58 -0700839 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400840 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700841 }
842
843 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700844 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
845 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400846 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700847 return 0;
848 }
849
David Benjamin52bf6902016-10-08 12:05:03 -0400850 /* Check that the extension matches. We do not support renegotiation as a
851 * server, so this must be empty. */
852 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400853 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700854 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
855 return 0;
856 }
857
858 ssl->s3->send_connection_binding = 1;
859
860 return 1;
861}
862
David Benjamin8c880a22016-12-03 02:20:34 -0500863static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
864 SSL *const ssl = hs->ssl;
David Benjamin52bf6902016-10-08 12:05:03 -0400865 /* Renegotiation isn't supported as a server so this function should never be
866 * called after the initial handshake. */
867 assert(!ssl->s3->initial_handshake_complete);
868
Steven Valdez143e8b32016-07-11 13:19:03 -0400869 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
870 return 1;
871 }
872
Adam Langley5021b222015-06-12 18:27:58 -0700873 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400874 !CBB_add_u16(out, 1 /* length */) ||
875 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700876 return 0;
877 }
878
879 return 1;
880}
881
Adam Langley0a056712015-07-01 15:03:33 -0700882
883/* Extended Master Secret.
884 *
David Benjamin43946d42016-02-01 08:42:19 -0500885 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700886
David Benjamin8c880a22016-12-03 02:20:34 -0500887static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400888 /* Extended master secret is not necessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -0400889 if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700890 return 1;
891 }
892
893 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
894 !CBB_add_u16(out, 0 /* length */)) {
895 return 0;
896 }
897
898 return 1;
899}
900
David Benjamin8c880a22016-12-03 02:20:34 -0500901static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700902 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500903 SSL *const ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500904
905 if (contents != NULL) {
906 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
907 ssl->version == SSL3_VERSION ||
908 CBS_len(contents) != 0) {
David Benjamin163c9562016-08-29 23:14:17 -0400909 return 0;
910 }
911
David Benjaminfc02b592017-02-17 16:26:01 -0500912 hs->extended_master_secret = 1;
David Benjamin163c9562016-08-29 23:14:17 -0400913 }
914
David Benjaminfc02b592017-02-17 16:26:01 -0500915 /* Whether EMS is negotiated may not change on renegotiation. */
916 if (ssl->s3->established_session != NULL &&
917 hs->extended_master_secret !=
918 ssl->s3->established_session->extended_master_secret) {
919 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
920 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400921 return 0;
922 }
923
Adam Langley0a056712015-07-01 15:03:33 -0700924 return 1;
925}
926
David Benjamin8c880a22016-12-03 02:20:34 -0500927static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500928 CBS *contents) {
David Benjaminfc02b592017-02-17 16:26:01 -0500929 uint16_t version = ssl3_protocol_version(hs->ssl);
David Benjamin8c880a22016-12-03 02:20:34 -0500930 if (version >= TLS1_3_VERSION ||
931 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400932 return 1;
933 }
934
935 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700936 return 1;
937 }
938
939 if (CBS_len(contents) != 0) {
940 return 0;
941 }
942
David Benjaminfc02b592017-02-17 16:26:01 -0500943 hs->extended_master_secret = 1;
Adam Langley0a056712015-07-01 15:03:33 -0700944 return 1;
945}
946
David Benjamin8c880a22016-12-03 02:20:34 -0500947static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -0500948 if (!hs->extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700949 return 1;
950 }
951
952 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
953 !CBB_add_u16(out, 0 /* length */)) {
954 return 0;
955 }
956
957 return 1;
958}
959
Adam Langley9b05bc52015-07-01 15:25:33 -0700960
961/* Session tickets.
962 *
963 * https://tools.ietf.org/html/rfc5077 */
964
David Benjamin8c880a22016-12-03 02:20:34 -0500965static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
966 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400967 /* TLS 1.3 uses a different ticket extension. */
David Benjamin68161cb2017-06-20 14:49:43 -0400968 if (hs->min_version >= TLS1_3_VERSION ||
David Benjamin7c7d8312016-08-20 13:39:03 -0400969 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700970 return 1;
971 }
972
973 const uint8_t *ticket_data = NULL;
974 int ticket_len = 0;
975
976 /* Renegotiation does not participate in session resumption. However, still
977 * advertise the extension to avoid potentially breaking servers which carry
978 * over the state from the previous handshake, such as OpenSSL servers
979 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
980 if (!ssl->s3->initial_handshake_complete &&
981 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400982 ssl->session->tlsext_tick != NULL &&
983 /* Don't send TLS 1.3 session tickets in the ticket extension. */
Steven Valdez8f36c512017-06-20 10:55:02 -0400984 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700985 ticket_data = ssl->session->tlsext_tick;
986 ticket_len = ssl->session->tlsext_ticklen;
987 }
988
989 CBB ticket;
990 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
991 !CBB_add_u16_length_prefixed(out, &ticket) ||
992 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
993 !CBB_flush(out)) {
994 return 0;
995 }
996
997 return 1;
998}
999
David Benjamin8c880a22016-12-03 02:20:34 -05001000static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -07001001 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001002 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -07001003 if (contents == NULL) {
1004 return 1;
1005 }
1006
Steven Valdez143e8b32016-07-11 13:19:03 -04001007 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1008 return 0;
1009 }
1010
Adam Langley9b05bc52015-07-01 15:25:33 -07001011 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1012 * this function should never be called, even if the server tries to send the
1013 * extension. */
1014 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1015
1016 if (CBS_len(contents) != 0) {
1017 return 0;
1018 }
1019
David Benjamin8c880a22016-12-03 02:20:34 -05001020 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001021 return 1;
1022}
1023
David Benjamin8c880a22016-12-03 02:20:34 -05001024static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1025 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001026 return 1;
1027 }
1028
David Benjamin78476f62016-11-12 11:20:55 +09001029 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin8c880a22016-12-03 02:20:34 -05001030 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001031
1032 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1033 !CBB_add_u16(out, 0 /* length */)) {
1034 return 0;
1035 }
1036
1037 return 1;
1038}
1039
1040
Adam Langley2e857bd2015-07-01 16:09:19 -07001041/* Signature Algorithms.
1042 *
1043 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1044
David Benjamin8c880a22016-12-03 02:20:34 -05001045static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1046 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001047 if (hs->max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001048 return 1;
1049 }
1050
David Benjamin0fc37ef2016-08-17 15:29:46 -04001051 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001052 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1053 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin69522112017-03-28 15:38:29 -05001054 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
1055 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
1056 !CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001057 return 0;
1058 }
1059
1060 return 1;
1061}
1062
David Benjamin8c880a22016-12-03 02:20:34 -05001063static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001064 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001065 OPENSSL_free(hs->peer_sigalgs);
1066 hs->peer_sigalgs = NULL;
1067 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001068
Adam Langley2e857bd2015-07-01 16:09:19 -07001069 if (contents == NULL) {
1070 return 1;
1071 }
1072
1073 CBS supported_signature_algorithms;
1074 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001075 CBS_len(contents) != 0 ||
1076 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001077 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001078 return 0;
1079 }
1080
1081 return 1;
1082}
1083
Adam Langley2e857bd2015-07-01 16:09:19 -07001084
Adam Langleybb0bd042015-07-01 16:21:03 -07001085/* OCSP Stapling.
1086 *
1087 * https://tools.ietf.org/html/rfc6066#section-8 */
1088
David Benjamin8c880a22016-12-03 02:20:34 -05001089static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1090 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001091 if (!ssl->ocsp_stapling_enabled) {
1092 return 1;
1093 }
1094
1095 CBB contents;
1096 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1097 !CBB_add_u16_length_prefixed(out, &contents) ||
1098 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1099 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1100 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1101 !CBB_flush(out)) {
1102 return 0;
1103 }
1104
1105 return 1;
1106}
1107
David Benjamin8c880a22016-12-03 02:20:34 -05001108static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001109 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001110 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001111 if (contents == NULL) {
1112 return 1;
1113 }
1114
Steven Valdeza833c352016-11-01 13:39:36 -04001115 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1116 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001117 return 0;
1118 }
1119
Steven Valdeza833c352016-11-01 13:39:36 -04001120 /* OCSP stapling is forbidden on non-certificate ciphers. */
1121 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001122 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001123 return 0;
1124 }
1125
Steven Valdeza833c352016-11-01 13:39:36 -04001126 /* Note this does not check for resumption in TLS 1.2. Sending
1127 * status_request here does not make sense, but OpenSSL does so and the
1128 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001129
David Benjamin8c880a22016-12-03 02:20:34 -05001130 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001131 return 1;
1132}
1133
David Benjamin8c880a22016-12-03 02:20:34 -05001134static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001135 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001136 if (contents == NULL) {
1137 return 1;
1138 }
1139
1140 uint8_t status_type;
1141 if (!CBS_get_u8(contents, &status_type)) {
1142 return 0;
1143 }
1144
1145 /* We cannot decide whether OCSP stapling will occur yet because the correct
1146 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001147 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001148
Adam Langleybb0bd042015-07-01 16:21:03 -07001149 return 1;
1150}
1151
David Benjamin8c880a22016-12-03 02:20:34 -05001152static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1153 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001154 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001155 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001156 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001157 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001158 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001159 return 1;
1160 }
1161
David Benjamin8c880a22016-12-03 02:20:34 -05001162 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001163
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001164 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001165 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001166}
1167
1168
Adam Langley97dfcbf2015-07-01 18:35:20 -07001169/* Next protocol negotiation.
1170 *
1171 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1172
David Benjamin8c880a22016-12-03 02:20:34 -05001173static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1174 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001175 if (ssl->s3->initial_handshake_complete ||
1176 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001177 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001178 return 1;
1179 }
1180
1181 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1182 !CBB_add_u16(out, 0 /* length */)) {
1183 return 0;
1184 }
1185
1186 return 1;
1187}
1188
David Benjamin8c880a22016-12-03 02:20:34 -05001189static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001190 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001191 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001192 if (contents == NULL) {
1193 return 1;
1194 }
1195
Steven Valdez143e8b32016-07-11 13:19:03 -04001196 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1197 return 0;
1198 }
1199
Adam Langley97dfcbf2015-07-01 18:35:20 -07001200 /* If any of these are false then we should never have sent the NPN
1201 * extension in the ClientHello and thus this function should never have been
1202 * called. */
1203 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001204 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001205 assert(ssl->ctx->next_proto_select_cb != NULL);
1206
David Benjamin76c2efc2015-08-31 14:24:29 -04001207 if (ssl->s3->alpn_selected != NULL) {
1208 /* NPN and ALPN may not be negotiated in the same connection. */
1209 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1210 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1211 return 0;
1212 }
1213
Adam Langley97dfcbf2015-07-01 18:35:20 -07001214 const uint8_t *const orig_contents = CBS_data(contents);
1215 const size_t orig_len = CBS_len(contents);
1216
1217 while (CBS_len(contents) != 0) {
1218 CBS proto;
1219 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1220 CBS_len(&proto) == 0) {
1221 return 0;
1222 }
1223 }
1224
1225 uint8_t *selected;
1226 uint8_t selected_len;
1227 if (ssl->ctx->next_proto_select_cb(
1228 ssl, &selected, &selected_len, orig_contents, orig_len,
1229 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1230 *out_alert = SSL_AD_INTERNAL_ERROR;
1231 return 0;
1232 }
1233
David Benjamin79978df2015-12-25 15:56:49 -05001234 OPENSSL_free(ssl->s3->next_proto_negotiated);
David Benjamin81678aa2017-07-12 22:43:42 -04001235 ssl->s3->next_proto_negotiated =
1236 (uint8_t *)BUF_memdup(selected, selected_len);
David Benjamin79978df2015-12-25 15:56:49 -05001237 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001238 *out_alert = SSL_AD_INTERNAL_ERROR;
1239 return 0;
1240 }
1241
David Benjamin79978df2015-12-25 15:56:49 -05001242 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001243 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001244
1245 return 1;
1246}
1247
David Benjamin8c880a22016-12-03 02:20:34 -05001248static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001249 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001250 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001251 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1252 return 1;
1253 }
1254
Adam Langley97dfcbf2015-07-01 18:35:20 -07001255 if (contents != NULL && CBS_len(contents) != 0) {
1256 return 0;
1257 }
1258
1259 if (contents == NULL ||
1260 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001261 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001262 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001263 return 1;
1264 }
1265
David Benjamin8c880a22016-12-03 02:20:34 -05001266 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001267 return 1;
1268}
1269
David Benjamin8c880a22016-12-03 02:20:34 -05001270static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1271 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001272 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1273 * parsed. */
David Benjamin8c880a22016-12-03 02:20:34 -05001274 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001275 return 1;
1276 }
1277
1278 const uint8_t *npa;
1279 unsigned npa_len;
1280
1281 if (ssl->ctx->next_protos_advertised_cb(
1282 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1283 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001284 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001285 return 1;
1286 }
1287
1288 CBB contents;
1289 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1290 !CBB_add_u16_length_prefixed(out, &contents) ||
1291 !CBB_add_bytes(&contents, npa, npa_len) ||
1292 !CBB_flush(out)) {
1293 return 0;
1294 }
1295
1296 return 1;
1297}
1298
1299
Adam Langleyab8d87d2015-07-10 12:21:39 -07001300/* Signed certificate timestamps.
1301 *
1302 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1303
David Benjamin8c880a22016-12-03 02:20:34 -05001304static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1305 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001306 if (!ssl->signed_cert_timestamps_enabled) {
1307 return 1;
1308 }
1309
1310 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1311 !CBB_add_u16(out, 0 /* length */)) {
1312 return 0;
1313 }
1314
1315 return 1;
1316}
1317
David Benjamin8c880a22016-12-03 02:20:34 -05001318static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001319 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001320 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001321 if (contents == NULL) {
1322 return 1;
1323 }
1324
Steven Valdeza833c352016-11-01 13:39:36 -04001325 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1326 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001327 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001328 return 0;
1329 }
1330
Adam Langleyab8d87d2015-07-10 12:21:39 -07001331 /* If this is false then we should never have sent the SCT extension in the
1332 * ClientHello and thus this function should never have been called. */
1333 assert(ssl->signed_cert_timestamps_enabled);
1334
Adam Langleycfa08c32016-11-17 13:21:27 -08001335 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001336 *out_alert = SSL_AD_DECODE_ERROR;
1337 return 0;
1338 }
1339
David Benjamindaa88502016-10-04 16:32:16 -04001340 /* Session resumption uses the original session information. The extension
1341 * should not be sent on resumption, but RFC 6962 did not make it a
1342 * requirement, so tolerate this.
1343 *
1344 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001345 if (!ssl->s3->session_reused &&
David Benjamin45738dd2017-02-09 20:01:26 -05001346 !CBS_stow(contents, &hs->new_session->tlsext_signed_cert_timestamp_list,
1347 &hs->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001348 *out_alert = SSL_AD_INTERNAL_ERROR;
1349 return 0;
1350 }
1351
1352 return 1;
1353}
1354
David Benjamin8c880a22016-12-03 02:20:34 -05001355static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001356 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001357 if (contents == NULL) {
1358 return 1;
1359 }
1360
1361 if (CBS_len(contents) != 0) {
1362 return 0;
1363 }
1364
David Benjamin8c880a22016-12-03 02:20:34 -05001365 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001366 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001367}
1368
David Benjamin8c880a22016-12-03 02:20:34 -05001369static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1370 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001371 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001372 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1373 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001374 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001375 return 1;
1376 }
1377
1378 CBB contents;
1379 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1380 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001381 CBB_add_bytes(
1382 &contents,
1383 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1384 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001385 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001386}
1387
1388
Adam Langleyf18e4532015-07-10 13:39:53 -07001389/* Application-level Protocol Negotiation.
1390 *
1391 * https://tools.ietf.org/html/rfc7301 */
1392
David Benjamin8c880a22016-12-03 02:20:34 -05001393static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1394 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001395 if (ssl->alpn_client_proto_list == NULL ||
1396 ssl->s3->initial_handshake_complete) {
1397 return 1;
1398 }
1399
1400 CBB contents, proto_list;
1401 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1402 !CBB_add_u16_length_prefixed(out, &contents) ||
1403 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1404 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1405 ssl->alpn_client_proto_list_len) ||
1406 !CBB_flush(out)) {
1407 return 0;
1408 }
1409
1410 return 1;
1411}
1412
David Benjamin8c880a22016-12-03 02:20:34 -05001413static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001414 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001415 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001416 if (contents == NULL) {
1417 return 1;
1418 }
1419
1420 assert(!ssl->s3->initial_handshake_complete);
1421 assert(ssl->alpn_client_proto_list != NULL);
1422
David Benjamin8c880a22016-12-03 02:20:34 -05001423 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001424 /* NPN and ALPN may not be negotiated in the same connection. */
1425 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1426 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1427 return 0;
1428 }
1429
Adam Langleyf18e4532015-07-10 13:39:53 -07001430 /* The extension data consists of a ProtocolNameList which must have
1431 * exactly one ProtocolName. Each of these is length-prefixed. */
1432 CBS protocol_name_list, protocol_name;
1433 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1434 CBS_len(contents) != 0 ||
1435 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1436 /* Empty protocol names are forbidden. */
1437 CBS_len(&protocol_name) == 0 ||
1438 CBS_len(&protocol_name_list) != 0) {
1439 return 0;
1440 }
1441
David Benjaminc8ff30c2017-04-04 13:52:36 -04001442 if (!ssl->ctx->allow_unknown_alpn_protos) {
1443 /* Check that the protocol name is one of the ones we advertised. */
1444 int protocol_ok = 0;
1445 CBS client_protocol_name_list, client_protocol_name;
1446 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1447 ssl->alpn_client_proto_list_len);
1448 while (CBS_len(&client_protocol_name_list) > 0) {
1449 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1450 &client_protocol_name)) {
1451 *out_alert = SSL_AD_INTERNAL_ERROR;
1452 return 0;
1453 }
1454
1455 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1456 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1457 CBS_data(&protocol_name),
1458 CBS_len(&protocol_name)) == 0) {
1459 protocol_ok = 1;
1460 break;
1461 }
1462 }
1463
1464 if (!protocol_ok) {
1465 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1466 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3e517572016-08-11 11:52:23 -04001467 return 0;
1468 }
David Benjamin3e517572016-08-11 11:52:23 -04001469 }
1470
Adam Langleyf18e4532015-07-10 13:39:53 -07001471 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1472 &ssl->s3->alpn_selected_len)) {
1473 *out_alert = SSL_AD_INTERNAL_ERROR;
1474 return 0;
1475 }
1476
1477 return 1;
1478}
1479
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001480int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001481 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001482 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001483 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001484 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001485 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001486 client_hello, &contents,
1487 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1488 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001489 return 1;
1490 }
1491
1492 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001493 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001494
1495 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001496 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1497 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001498 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001499 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1500 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001501 return 0;
1502 }
1503
1504 /* Validate the protocol list. */
1505 CBS protocol_name_list_copy = protocol_name_list;
1506 while (CBS_len(&protocol_name_list_copy) > 0) {
1507 CBS protocol_name;
1508
1509 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1510 /* Empty protocol names are forbidden. */
1511 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001512 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1513 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001514 return 0;
1515 }
1516 }
1517
1518 const uint8_t *selected;
1519 uint8_t selected_len;
1520 if (ssl->ctx->alpn_select_cb(
1521 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1522 CBS_len(&protocol_name_list),
1523 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1524 OPENSSL_free(ssl->s3->alpn_selected);
David Benjamin81678aa2017-07-12 22:43:42 -04001525 ssl->s3->alpn_selected = (uint8_t *)BUF_memdup(selected, selected_len);
Adam Langleyf18e4532015-07-10 13:39:53 -07001526 if (ssl->s3->alpn_selected == NULL) {
1527 *out_alert = SSL_AD_INTERNAL_ERROR;
1528 return 0;
1529 }
1530 ssl->s3->alpn_selected_len = selected_len;
1531 }
1532
1533 return 1;
1534}
1535
David Benjamin8c880a22016-12-03 02:20:34 -05001536static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1537 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001538 if (ssl->s3->alpn_selected == NULL) {
1539 return 1;
1540 }
1541
1542 CBB contents, proto_list, proto;
1543 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1544 !CBB_add_u16_length_prefixed(out, &contents) ||
1545 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1546 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001547 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1548 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001549 !CBB_flush(out)) {
1550 return 0;
1551 }
1552
1553 return 1;
1554}
1555
1556
Adam Langley49c7af12015-07-10 14:33:46 -07001557/* Channel ID.
1558 *
1559 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1560
David Benjamin8c880a22016-12-03 02:20:34 -05001561static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1562 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001563}
1564
David Benjamin8c880a22016-12-03 02:20:34 -05001565static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1566 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001567 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001568 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001569 return 1;
1570 }
1571
1572 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1573 !CBB_add_u16(out, 0 /* length */)) {
1574 return 0;
1575 }
1576
1577 return 1;
1578}
1579
David Benjamin8c880a22016-12-03 02:20:34 -05001580static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1581 uint8_t *out_alert, CBS *contents) {
1582 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001583 if (contents == NULL) {
1584 return 1;
1585 }
1586
David Benjamince079fd2016-08-02 16:22:34 -04001587 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001588 assert(ssl->tlsext_channel_id_enabled);
1589
1590 if (CBS_len(contents) != 0) {
1591 return 0;
1592 }
1593
1594 ssl->s3->tlsext_channel_id_valid = 1;
1595 return 1;
1596}
1597
David Benjamin8c880a22016-12-03 02:20:34 -05001598static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1599 uint8_t *out_alert, CBS *contents) {
1600 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001601 if (contents == NULL ||
1602 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001603 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001604 return 1;
1605 }
1606
1607 if (CBS_len(contents) != 0) {
1608 return 0;
1609 }
1610
1611 ssl->s3->tlsext_channel_id_valid = 1;
1612 return 1;
1613}
1614
David Benjamin8c880a22016-12-03 02:20:34 -05001615static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1616 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001617 if (!ssl->s3->tlsext_channel_id_valid) {
1618 return 1;
1619 }
1620
1621 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1622 !CBB_add_u16(out, 0 /* length */)) {
1623 return 0;
1624 }
1625
1626 return 1;
1627}
1628
Adam Langley391250d2015-07-15 19:06:07 -07001629
1630/* Secure Real-time Transport Protocol (SRTP) extension.
1631 *
1632 * https://tools.ietf.org/html/rfc5764 */
1633
Adam Langley391250d2015-07-15 19:06:07 -07001634
David Benjamin8c880a22016-12-03 02:20:34 -05001635static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1636 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001637}
1638
David Benjamin8c880a22016-12-03 02:20:34 -05001639static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1640 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001641 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1642 if (profiles == NULL) {
1643 return 1;
1644 }
1645 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1646 if (num_profiles == 0) {
1647 return 1;
1648 }
1649
1650 CBB contents, profile_ids;
1651 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1652 !CBB_add_u16_length_prefixed(out, &contents) ||
1653 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1654 return 0;
1655 }
1656
David Benjamin54091232016-09-05 12:47:25 -04001657 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001658 if (!CBB_add_u16(&profile_ids,
1659 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1660 return 0;
1661 }
1662 }
1663
1664 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1665 !CBB_flush(out)) {
1666 return 0;
1667 }
1668
1669 return 1;
1670}
1671
David Benjamin8c880a22016-12-03 02:20:34 -05001672static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001673 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001674 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001675 if (contents == NULL) {
1676 return 1;
1677 }
1678
1679 /* The extension consists of a u16-prefixed profile ID list containing a
1680 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1681 *
1682 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1683 CBS profile_ids, srtp_mki;
1684 uint16_t profile_id;
1685 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1686 !CBS_get_u16(&profile_ids, &profile_id) ||
1687 CBS_len(&profile_ids) != 0 ||
1688 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1689 CBS_len(contents) != 0) {
1690 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1691 return 0;
1692 }
1693
1694 if (CBS_len(&srtp_mki) != 0) {
1695 /* Must be no MKI, since we never offer one. */
1696 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1697 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1698 return 0;
1699 }
1700
1701 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1702
1703 /* Check to see if the server gave us something we support (and presumably
1704 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001705 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001706 const SRTP_PROTECTION_PROFILE *profile =
1707 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1708
1709 if (profile->id == profile_id) {
1710 ssl->srtp_profile = profile;
1711 return 1;
1712 }
1713 }
1714
1715 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1716 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1717 return 0;
1718}
1719
David Benjamin8c880a22016-12-03 02:20:34 -05001720static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001721 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001722 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001723 if (contents == NULL) {
1724 return 1;
1725 }
1726
1727 CBS profile_ids, srtp_mki;
1728 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1729 CBS_len(&profile_ids) < 2 ||
1730 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1731 CBS_len(contents) != 0) {
1732 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1733 return 0;
1734 }
1735 /* Discard the MKI value for now. */
1736
1737 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1738 SSL_get_srtp_profiles(ssl);
1739
1740 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001741 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001742 const SRTP_PROTECTION_PROFILE *server_profile =
1743 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1744
1745 CBS profile_ids_tmp;
1746 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1747
1748 while (CBS_len(&profile_ids_tmp) > 0) {
1749 uint16_t profile_id;
1750 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1751 return 0;
1752 }
1753
1754 if (server_profile->id == profile_id) {
1755 ssl->srtp_profile = server_profile;
1756 return 1;
1757 }
1758 }
1759 }
1760
1761 return 1;
1762}
1763
David Benjamin8c880a22016-12-03 02:20:34 -05001764static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1765 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001766 if (ssl->srtp_profile == NULL) {
1767 return 1;
1768 }
1769
1770 CBB contents, profile_ids;
1771 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1772 !CBB_add_u16_length_prefixed(out, &contents) ||
1773 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1774 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1775 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1776 !CBB_flush(out)) {
1777 return 0;
1778 }
1779
1780 return 1;
1781}
1782
Adam Langleybdd5d662015-07-20 16:19:08 -07001783
1784/* EC point formats.
1785 *
1786 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1787
David Benjamin8c880a22016-12-03 02:20:34 -05001788static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001789 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001790 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1791 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001792 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1793 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001794 !CBB_flush(out)) {
1795 return 0;
1796 }
1797
1798 return 1;
1799}
1800
David Benjamin8c880a22016-12-03 02:20:34 -05001801static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001802 /* The point format extension is unneccessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -04001803 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin70aba262016-11-01 12:08:15 -04001804 return 1;
1805 }
1806
David Benjamin8c880a22016-12-03 02:20:34 -05001807 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001808}
1809
David Benjamin8c880a22016-12-03 02:20:34 -05001810static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001811 CBS *contents) {
1812 if (contents == NULL) {
1813 return 1;
1814 }
1815
David Benjamin8c880a22016-12-03 02:20:34 -05001816 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001817 return 0;
1818 }
1819
Adam Langleybdd5d662015-07-20 16:19:08 -07001820 CBS ec_point_format_list;
1821 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1822 CBS_len(contents) != 0) {
1823 return 0;
1824 }
1825
David Benjaminfc059942015-07-30 23:01:59 -04001826 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1827 * point format. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05001828 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1829 TLSEXT_ECPOINTFORMAT_uncompressed,
1830 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001831 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001832 return 0;
1833 }
1834
1835 return 1;
1836}
1837
David Benjamin8c880a22016-12-03 02:20:34 -05001838static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001839 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001840 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001841 return 1;
1842 }
1843
David Benjamin8c880a22016-12-03 02:20:34 -05001844 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001845}
1846
David Benjamin8c880a22016-12-03 02:20:34 -05001847static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1848 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001849 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1850 return 1;
1851 }
1852
David Benjamin45738dd2017-02-09 20:01:26 -05001853 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1854 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001855 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001856
1857 if (!using_ecc) {
1858 return 1;
1859 }
1860
David Benjamin8c880a22016-12-03 02:20:34 -05001861 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001862}
1863
Steven Valdeza833c352016-11-01 13:39:36 -04001864
Steven Valdez4aa154e2016-07-29 14:32:55 -04001865/* Pre Shared Key
1866 *
Steven Valdeza833c352016-11-01 13:39:36 -04001867 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001868
David Benjamin8c880a22016-12-03 02:20:34 -05001869static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1870 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001871 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001872 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001873 return 0;
1874 }
1875
Steven Valdez8f36c512017-06-20 10:55:02 -04001876 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001877 return 15 + ssl->session->tlsext_ticklen + binder_len;
1878}
1879
David Benjamin8c880a22016-12-03 02:20:34 -05001880static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1881 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001882 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001883 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001884 return 1;
1885 }
1886
David Benjaminad8f5e12017-02-20 17:00:20 -05001887 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001888 ssl_get_current_time(ssl, &now);
1889 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1890 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1891
1892 /* Fill in a placeholder zero binder of the appropriate length. It will be
1893 * computed and filled in later after length prefixes are computed. */
1894 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez8f36c512017-06-20 10:55:02 -04001895 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001896
1897 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001898 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1899 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001900 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001901 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1902 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001903 ssl->session->tlsext_ticklen) ||
1904 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1905 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1906 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1907 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001908 return 0;
1909 }
1910
David Benjamin8c880a22016-12-03 02:20:34 -05001911 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001912 return CBB_flush(out);
1913}
1914
David Benjamin8baf9632016-11-17 17:11:16 +09001915int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1916 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001917 CBS *contents) {
1918 uint16_t psk_id;
1919 if (!CBS_get_u16(contents, &psk_id) ||
1920 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001921 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001922 *out_alert = SSL_AD_DECODE_ERROR;
1923 return 0;
1924 }
1925
David Benjamineab773a2016-11-09 18:32:35 -05001926 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001927 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001928 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001929 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1930 return 0;
1931 }
1932
1933 return 1;
1934}
1935
David Benjamin35ac5b72017-03-03 15:05:56 -05001936int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001937 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001938 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04001939 /* We only process the first PSK identity since we don't support pure PSK. */
David Benjamin707af292017-03-10 17:47:18 -05001940 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001941 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001942 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001943 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001944 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001945 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001946 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001947 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001948 *out_alert = SSL_AD_DECODE_ERROR;
1949 return 0;
1950 }
1951
Steven Valdeza833c352016-11-01 13:39:36 -04001952 *out_binders = binders;
1953
David Benjaminaedf3032016-12-01 16:47:56 -05001954 /* Check the syntax of the remaining identities, but do not process them. */
1955 size_t num_identities = 1;
1956 while (CBS_len(&identities) != 0) {
1957 CBS unused_ticket;
1958 uint32_t unused_obfuscated_ticket_age;
1959 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1960 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1961 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1962 *out_alert = SSL_AD_DECODE_ERROR;
1963 return 0;
1964 }
1965
1966 num_identities++;
1967 }
1968
1969 /* Check the syntax of the binders. The value will be checked later if
1970 * resuming. */
1971 size_t num_binders = 0;
1972 while (CBS_len(&binders) != 0) {
1973 CBS binder;
1974 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1975 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1976 *out_alert = SSL_AD_DECODE_ERROR;
1977 return 0;
1978 }
1979
1980 num_binders++;
1981 }
1982
1983 if (num_identities != num_binders) {
1984 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1985 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04001986 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04001987 }
1988
David Benjamine7f60a22016-11-16 18:54:25 +09001989 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001990}
1991
David Benjamin8baf9632016-11-17 17:11:16 +09001992int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1993 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001994 return 1;
1995 }
1996
1997 CBB contents;
1998 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1999 !CBB_add_u16_length_prefixed(out, &contents) ||
2000 /* We only consider the first identity for resumption */
2001 !CBB_add_u16(&contents, 0) ||
2002 !CBB_flush(out)) {
2003 return 0;
2004 }
2005
2006 return 1;
2007}
2008
2009
Steven Valdeza833c352016-11-01 13:39:36 -04002010/* Pre-Shared Key Exchange Modes
2011 *
2012 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002013
David Benjamin8c880a22016-12-03 02:20:34 -05002014static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2015 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04002016 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04002017 return 1;
2018 }
2019
2020 CBB contents, ke_modes;
2021 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2022 !CBB_add_u16_length_prefixed(out, &contents) ||
2023 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2024 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2025 return 0;
2026 }
2027
2028 return CBB_flush(out);
2029}
2030
David Benjamin8c880a22016-12-03 02:20:34 -05002031static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002032 uint8_t *out_alert,
2033 CBS *contents) {
2034 if (contents == NULL) {
2035 return 1;
2036 }
2037
Steven Valdeza833c352016-11-01 13:39:36 -04002038 CBS ke_modes;
2039 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2040 CBS_len(&ke_modes) == 0 ||
2041 CBS_len(contents) != 0) {
2042 *out_alert = SSL_AD_DECODE_ERROR;
2043 return 0;
2044 }
2045
2046 /* We only support tickets with PSK_DHE_KE. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05002047 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2048 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002049
2050 return 1;
2051}
2052
2053
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002054/* Early Data Indication
2055 *
2056 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2057
David Benjamin8c880a22016-12-03 02:20:34 -05002058static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002059 SSL *const ssl = hs->ssl;
Steven Valdez2d850622017-01-11 11:34:52 -05002060 if (ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04002061 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION ||
Steven Valdez2d850622017-01-11 11:34:52 -05002062 ssl->session->ticket_max_early_data == 0 ||
2063 hs->received_hello_retry_request ||
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05002064 !ssl->cert->enable_early_data) {
Steven Valdez2d850622017-01-11 11:34:52 -05002065 return 1;
2066 }
2067
2068 hs->early_data_offered = 1;
2069
2070 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2071 !CBB_add_u16(out, 0) ||
2072 !CBB_flush(out)) {
2073 return 0;
2074 }
2075
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002076 return 1;
2077}
2078
Steven Valdez2d850622017-01-11 11:34:52 -05002079static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002080 uint8_t *out_alert, CBS *contents) {
2081 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002082 if (contents == NULL) {
2083 return 1;
2084 }
2085
2086 if (CBS_len(contents) != 0) {
2087 *out_alert = SSL_AD_DECODE_ERROR;
2088 return 0;
2089 }
2090
Steven Valdez2d850622017-01-11 11:34:52 -05002091 if (!ssl->s3->session_reused) {
2092 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2093 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2094 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002095 }
Steven Valdez2d850622017-01-11 11:34:52 -05002096
2097 ssl->early_data_accepted = 1;
2098 return 1;
2099}
2100
2101static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2102 uint8_t *out_alert, CBS *contents) {
2103 SSL *const ssl = hs->ssl;
2104 if (contents == NULL ||
2105 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2106 return 1;
2107 }
2108
2109 if (CBS_len(contents) != 0) {
2110 *out_alert = SSL_AD_DECODE_ERROR;
2111 return 0;
2112 }
2113
2114 hs->early_data_offered = 1;
2115 return 1;
2116}
2117
2118static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2119 if (!hs->ssl->early_data_accepted) {
2120 return 1;
2121 }
2122
2123 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2124 !CBB_add_u16(out, 0) ||
2125 !CBB_flush(out)) {
2126 return 0;
2127 }
2128
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002129 return 1;
2130}
2131
2132
Steven Valdez143e8b32016-07-11 13:19:03 -04002133/* Key Share
2134 *
David Benjamina128a552016-10-13 14:26:33 -04002135 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002136
David Benjamin8c880a22016-12-03 02:20:34 -05002137static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2138 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002139 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002140 return 1;
2141 }
2142
2143 CBB contents, kse_bytes;
2144 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2145 !CBB_add_u16_length_prefixed(out, &contents) ||
2146 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2147 return 0;
2148 }
2149
David Benjamin8c880a22016-12-03 02:20:34 -05002150 uint16_t group_id = hs->retry_group;
2151 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002152 /* We received a HelloRetryRequest without a new curve, so there is no new
2153 * share to append. Leave |ecdh_ctx| as-is. */
2154 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002155 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2156 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002157 return 0;
2158 }
David Benjamin8c880a22016-12-03 02:20:34 -05002159 OPENSSL_free(hs->key_share_bytes);
2160 hs->key_share_bytes = NULL;
2161 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002162 if (group_id == 0) {
2163 return CBB_flush(out);
2164 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002165 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002166 /* Add a fake group. See draft-davidben-tls-grease-01. */
2167 if (ssl->ctx->grease_enabled &&
2168 (!CBB_add_u16(&kse_bytes,
2169 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2170 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2171 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2172 return 0;
2173 }
2174
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002175 /* Predict the most preferred group. */
2176 const uint16_t *groups;
2177 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002178 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002179 if (groups_len == 0) {
2180 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2181 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002182 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002183
2184 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002185 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002186
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002187 CBB key_exchange;
2188 if (!CBB_add_u16(&kse_bytes, group_id) ||
2189 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002190 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2191 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002192 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002193 return 0;
2194 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002195
David Benjamin8c880a22016-12-03 02:20:34 -05002196 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002197 /* Save the contents of the extension to repeat it in the second
2198 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002199 hs->key_share_bytes_len = CBB_len(&kse_bytes);
David Benjamin81678aa2017-07-12 22:43:42 -04002200 hs->key_share_bytes =
2201 (uint8_t *)BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
David Benjamin8c880a22016-12-03 02:20:34 -05002202 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002203 return 0;
2204 }
2205 }
2206
Steven Valdez143e8b32016-07-11 13:19:03 -04002207 return CBB_flush(out);
2208}
2209
David Benjamin8baf9632016-11-17 17:11:16 +09002210int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002211 size_t *out_secret_len,
2212 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002213 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002214 uint16_t group_id;
2215 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002216 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2217 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002218 *out_alert = SSL_AD_DECODE_ERROR;
2219 return 0;
2220 }
2221
David Benjamin8baf9632016-11-17 17:11:16 +09002222 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002223 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2224 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2225 return 0;
2226 }
2227
David Benjamin8baf9632016-11-17 17:11:16 +09002228 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2229 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002230 *out_alert = SSL_AD_INTERNAL_ERROR;
2231 return 0;
2232 }
2233
David Benjamin45738dd2017-02-09 20:01:26 -05002234 hs->new_session->group_id = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002235 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002236 return 1;
2237}
2238
David Benjamin8baf9632016-11-17 17:11:16 +09002239int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002240 uint8_t **out_secret,
2241 size_t *out_secret_len,
2242 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002243 uint16_t group_id;
2244 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002245 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002246 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2247 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2248 return 0;
2249 }
2250
2251 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002252 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002253 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002254 return 0;
2255 }
2256
David Benjamin7e1f9842016-09-20 19:24:40 -04002257 /* Find the corresponding key share. */
2258 int found = 0;
2259 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002260 while (CBS_len(&key_shares) > 0) {
2261 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002262 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002263 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002264 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2265 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002266 return 0;
2267 }
2268
David Benjamin7e1f9842016-09-20 19:24:40 -04002269 if (id == group_id) {
2270 if (found) {
2271 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2272 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2273 return 0;
2274 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002275
David Benjamin7e1f9842016-09-20 19:24:40 -04002276 found = 1;
2277 peer_key = peer_key_tmp;
2278 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002279 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002280 }
2281
David Benjamin7e1f9842016-09-20 19:24:40 -04002282 if (!found) {
2283 *out_found = 0;
2284 *out_secret = NULL;
2285 *out_secret_len = 0;
2286 return 1;
2287 }
2288
2289 /* Compute the DH secret. */
2290 uint8_t *secret = NULL;
2291 size_t secret_len;
2292 SSL_ECDH_CTX group;
David Benjamin17cf2cb2016-12-13 01:07:13 -05002293 OPENSSL_memset(&group, 0, sizeof(SSL_ECDH_CTX));
David Benjamin7e1f9842016-09-20 19:24:40 -04002294 CBB public_key;
2295 if (!CBB_init(&public_key, 32) ||
2296 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002297 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
2298 CBS_data(&peer_key), CBS_len(&peer_key)) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002299 !CBB_finish(&public_key, &hs->ecdh_public_key,
2300 &hs->ecdh_public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002301 OPENSSL_free(secret);
2302 SSL_ECDH_CTX_cleanup(&group);
2303 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002304 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002305 return 0;
2306 }
2307
2308 SSL_ECDH_CTX_cleanup(&group);
2309
2310 *out_secret = secret;
2311 *out_secret_len = secret_len;
2312 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002313 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002314}
2315
David Benjamin8baf9632016-11-17 17:11:16 +09002316int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002317 uint16_t group_id;
2318 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002319 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002320 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2321 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2322 !CBB_add_u16(&kse_bytes, group_id) ||
2323 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002324 !CBB_add_bytes(&public_key, hs->ecdh_public_key,
2325 hs->ecdh_public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002326 !CBB_flush(out)) {
2327 return 0;
2328 }
2329
David Benjaminbf833c32017-03-30 15:45:21 -05002330 OPENSSL_free(hs->ecdh_public_key);
2331 hs->ecdh_public_key = NULL;
2332 hs->ecdh_public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002333
David Benjamin45738dd2017-02-09 20:01:26 -05002334 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002335 return 1;
2336}
2337
2338
Steven Valdezfdd10992016-09-15 16:27:05 -04002339/* Supported Versions
2340 *
2341 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2342
David Benjamin8c880a22016-12-03 02:20:34 -05002343static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2344 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002345 if (hs->max_version <= TLS1_2_VERSION) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002346 return 1;
2347 }
2348
2349 CBB contents, versions;
2350 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2351 !CBB_add_u16_length_prefixed(out, &contents) ||
2352 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2353 return 0;
2354 }
2355
David Benjamind9791bf2016-09-27 16:39:52 -04002356 /* Add a fake version. See draft-davidben-tls-grease-01. */
2357 if (ssl->ctx->grease_enabled &&
2358 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2359 return 0;
2360 }
2361
Steven Valdez8f36c512017-06-20 10:55:02 -04002362 if (!ssl_add_supported_versions(hs, &versions) ||
2363 !CBB_flush(out)) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002364 return 0;
2365 }
2366
2367 return 1;
2368}
2369
2370
David Benjamin3baa6e12016-10-07 21:10:38 -04002371/* Cookie
2372 *
2373 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2374
David Benjamin8c880a22016-12-03 02:20:34 -05002375static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2376 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002377 return 1;
2378 }
2379
2380 CBB contents, cookie;
2381 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2382 !CBB_add_u16_length_prefixed(out, &contents) ||
2383 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002384 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002385 !CBB_flush(out)) {
2386 return 0;
2387 }
2388
2389 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002390 OPENSSL_free(hs->cookie);
2391 hs->cookie = NULL;
2392 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002393 return 1;
2394}
2395
2396
Steven Valdezce902a92016-05-17 11:47:53 -04002397/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002398 *
Steven Valdezce902a92016-05-17 11:47:53 -04002399 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002400 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002401
David Benjamin8c880a22016-12-03 02:20:34 -05002402static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2403 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002404 CBB contents, groups_bytes;
2405 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002406 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002407 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002408 return 0;
2409 }
2410
David Benjamin65ac9972016-09-02 21:35:25 -04002411 /* Add a fake group. See draft-davidben-tls-grease-01. */
2412 if (ssl->ctx->grease_enabled &&
2413 !CBB_add_u16(&groups_bytes,
2414 ssl_get_grease_value(ssl, ssl_grease_group))) {
2415 return 0;
2416 }
2417
Steven Valdezce902a92016-05-17 11:47:53 -04002418 const uint16_t *groups;
2419 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002420 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002421
David Benjamin54091232016-09-05 12:47:25 -04002422 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002423 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002424 return 0;
2425 }
2426 }
2427
2428 return CBB_flush(out);
2429}
2430
David Benjamin8c880a22016-12-03 02:20:34 -05002431static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2432 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002433 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002434 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2435 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002436 return 1;
2437}
2438
David Benjamin8c880a22016-12-03 02:20:34 -05002439static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2440 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002441 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002442 if (contents == NULL) {
2443 return 1;
2444 }
2445
Steven Valdezce902a92016-05-17 11:47:53 -04002446 CBS supported_group_list;
2447 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2448 CBS_len(&supported_group_list) == 0 ||
2449 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002450 CBS_len(contents) != 0) {
2451 return 0;
2452 }
2453
David Benjamin8c880a22016-12-03 02:20:34 -05002454 hs->peer_supported_group_list =
David Benjamin81678aa2017-07-12 22:43:42 -04002455 (uint16_t *)OPENSSL_malloc(CBS_len(&supported_group_list));
David Benjamin8c880a22016-12-03 02:20:34 -05002456 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002457 *out_alert = SSL_AD_INTERNAL_ERROR;
2458 return 0;
2459 }
2460
Steven Valdezce902a92016-05-17 11:47:53 -04002461 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002462 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002463 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002464 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002465 goto err;
2466 }
2467 }
2468
Steven Valdezce902a92016-05-17 11:47:53 -04002469 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002470 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002471
2472 return 1;
2473
2474err:
David Benjamin8c880a22016-12-03 02:20:34 -05002475 OPENSSL_free(hs->peer_supported_group_list);
2476 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002477 *out_alert = SSL_AD_INTERNAL_ERROR;
2478 return 0;
2479}
2480
David Benjamin8c880a22016-12-03 02:20:34 -05002481static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002482 /* Servers don't echo this extension. */
2483 return 1;
2484}
2485
2486
Adam Langley614c66a2015-06-12 15:26:58 -07002487/* kExtensions contains all the supported extensions. */
2488static const struct tls_extension kExtensions[] = {
2489 {
Adam Langley5021b222015-06-12 18:27:58 -07002490 TLSEXT_TYPE_renegotiate,
2491 NULL,
2492 ext_ri_add_clienthello,
2493 ext_ri_parse_serverhello,
2494 ext_ri_parse_clienthello,
2495 ext_ri_add_serverhello,
2496 },
2497 {
Adam Langley614c66a2015-06-12 15:26:58 -07002498 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002499 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002500 ext_sni_add_clienthello,
2501 ext_sni_parse_serverhello,
2502 ext_sni_parse_clienthello,
2503 ext_sni_add_serverhello,
2504 },
Adam Langley0a056712015-07-01 15:03:33 -07002505 {
2506 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002507 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002508 ext_ems_add_clienthello,
2509 ext_ems_parse_serverhello,
2510 ext_ems_parse_clienthello,
2511 ext_ems_add_serverhello,
2512 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002513 {
2514 TLSEXT_TYPE_session_ticket,
2515 NULL,
2516 ext_ticket_add_clienthello,
2517 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002518 /* Ticket extension client parsing is handled in ssl_session.c */
2519 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002520 ext_ticket_add_serverhello,
2521 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002522 {
2523 TLSEXT_TYPE_signature_algorithms,
2524 NULL,
2525 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002526 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002527 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002528 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002529 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002530 {
2531 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002532 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002533 ext_ocsp_add_clienthello,
2534 ext_ocsp_parse_serverhello,
2535 ext_ocsp_parse_clienthello,
2536 ext_ocsp_add_serverhello,
2537 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002538 {
2539 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002540 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002541 ext_npn_add_clienthello,
2542 ext_npn_parse_serverhello,
2543 ext_npn_parse_clienthello,
2544 ext_npn_add_serverhello,
2545 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002546 {
2547 TLSEXT_TYPE_certificate_timestamp,
2548 NULL,
2549 ext_sct_add_clienthello,
2550 ext_sct_parse_serverhello,
2551 ext_sct_parse_clienthello,
2552 ext_sct_add_serverhello,
2553 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002554 {
2555 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002556 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002557 ext_alpn_add_clienthello,
2558 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002559 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2560 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002561 ext_alpn_add_serverhello,
2562 },
Adam Langley49c7af12015-07-10 14:33:46 -07002563 {
2564 TLSEXT_TYPE_channel_id,
2565 ext_channel_id_init,
2566 ext_channel_id_add_clienthello,
2567 ext_channel_id_parse_serverhello,
2568 ext_channel_id_parse_clienthello,
2569 ext_channel_id_add_serverhello,
2570 },
Adam Langley391250d2015-07-15 19:06:07 -07002571 {
2572 TLSEXT_TYPE_srtp,
2573 ext_srtp_init,
2574 ext_srtp_add_clienthello,
2575 ext_srtp_parse_serverhello,
2576 ext_srtp_parse_clienthello,
2577 ext_srtp_add_serverhello,
2578 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002579 {
2580 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002581 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002582 ext_ec_point_add_clienthello,
2583 ext_ec_point_parse_serverhello,
2584 ext_ec_point_parse_clienthello,
2585 ext_ec_point_add_serverhello,
2586 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002587 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002588 TLSEXT_TYPE_key_share,
2589 NULL,
2590 ext_key_share_add_clienthello,
2591 forbid_parse_serverhello,
2592 ignore_parse_clienthello,
2593 dont_add_serverhello,
2594 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002595 {
Steven Valdeza833c352016-11-01 13:39:36 -04002596 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002597 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002598 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002599 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002600 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002601 dont_add_serverhello,
2602 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002603 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002604 TLSEXT_TYPE_early_data,
2605 NULL,
2606 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002607 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002608 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002609 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002610 },
2611 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002612 TLSEXT_TYPE_supported_versions,
2613 NULL,
2614 ext_supported_versions_add_clienthello,
2615 forbid_parse_serverhello,
2616 ignore_parse_clienthello,
2617 dont_add_serverhello,
2618 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002619 {
2620 TLSEXT_TYPE_cookie,
2621 NULL,
2622 ext_cookie_add_clienthello,
2623 forbid_parse_serverhello,
2624 ignore_parse_clienthello,
2625 dont_add_serverhello,
2626 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002627 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2628 * intolerant to the last extension being zero-length. See
2629 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002630 {
Steven Valdezce902a92016-05-17 11:47:53 -04002631 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002632 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002633 ext_supported_groups_add_clienthello,
2634 ext_supported_groups_parse_serverhello,
2635 ext_supported_groups_parse_clienthello,
2636 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002637 },
Adam Langley614c66a2015-06-12 15:26:58 -07002638};
2639
2640#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2641
Adam Langley4cfa96b2015-07-01 11:56:55 -07002642OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002643 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002644 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002645OPENSSL_COMPILE_ASSERT(
2646 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2647 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002648
Adam Langley614c66a2015-06-12 15:26:58 -07002649static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2650 uint16_t value) {
2651 unsigned i;
2652 for (i = 0; i < kNumExtensions; i++) {
2653 if (kExtensions[i].value == value) {
2654 *out_index = i;
2655 return &kExtensions[i];
2656 }
2657 }
2658
2659 return NULL;
2660}
2661
Adam Langley09505632015-07-30 18:10:13 -07002662int SSL_extension_supported(unsigned extension_value) {
2663 uint32_t index;
2664 return extension_value == TLSEXT_TYPE_padding ||
2665 tls_extension_find(&index, extension_value) != NULL;
2666}
2667
David Benjamin8c880a22016-12-03 02:20:34 -05002668int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2669 SSL *const ssl = hs->ssl;
David Benjaminf04c2e92016-12-06 13:35:25 -05002670 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2671 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002672 !ssl->s3->send_connection_binding) {
2673 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002674 }
Adam Langley95c29f32014-06-20 12:00:00 -07002675
David Benjamine8d53502015-10-10 14:13:23 -04002676 CBB extensions;
2677 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002678 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2679 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002680 }
Adam Langley95c29f32014-06-20 12:00:00 -07002681
David Benjamin8c880a22016-12-03 02:20:34 -05002682 hs->extensions.sent = 0;
2683 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002684
David Benjamin54091232016-09-05 12:47:25 -04002685 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002686 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002687 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002688 }
2689 }
Adam Langley95c29f32014-06-20 12:00:00 -07002690
David Benjamin65ac9972016-09-02 21:35:25 -04002691 uint16_t grease_ext1 = 0;
2692 if (ssl->ctx->grease_enabled) {
2693 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2694 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2695 if (!CBB_add_u16(&extensions, grease_ext1) ||
2696 !CBB_add_u16(&extensions, 0 /* zero length */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002697 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2698 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002699 }
2700 }
2701
David Benjamin54091232016-09-05 12:47:25 -04002702 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002703 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002704 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002705 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002706 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin81678aa2017-07-12 22:43:42 -04002707 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002708 }
Adam Langley95c29f32014-06-20 12:00:00 -07002709
Adam Langley33ad2b52015-07-20 17:43:53 -07002710 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002711 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002712 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002713 }
Adam Langley75712922014-10-10 16:23:43 -07002714
David Benjamin2bd19172016-11-17 16:47:15 +09002715 if (!custom_ext_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002716 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2717 return 0;
Adam Langley09505632015-07-30 18:10:13 -07002718 }
2719
David Benjamin65ac9972016-09-02 21:35:25 -04002720 if (ssl->ctx->grease_enabled) {
2721 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2722 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2723
2724 /* The two fake extensions must not have the same value. GREASE values are
2725 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2726 * one. */
2727 if (grease_ext1 == grease_ext2) {
2728 grease_ext2 ^= 0x1010;
2729 }
2730
2731 if (!CBB_add_u16(&extensions, grease_ext2) ||
2732 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2733 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002734 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2735 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002736 }
2737 }
2738
David Benjamince079fd2016-08-02 16:22:34 -04002739 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002740 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002741 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002742 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002743 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002744 *
2745 * NB: because this code works out the length of all existing extensions
2746 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002747 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002748 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002749 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002750 * Server 7.0 is intolerant to the last extension being zero-length. See
2751 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002752 if (padding_len >= 4 + 1) {
2753 padding_len -= 4;
2754 } else {
2755 padding_len = 1;
2756 }
Adam Langley95c29f32014-06-20 12:00:00 -07002757
Adam Langley33ad2b52015-07-20 17:43:53 -07002758 uint8_t *padding_bytes;
2759 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2760 !CBB_add_u16(&extensions, padding_len) ||
2761 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002762 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2763 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002764 }
Adam Langley75712922014-10-10 16:23:43 -07002765
David Benjamin17cf2cb2016-12-13 01:07:13 -05002766 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002767 }
2768 }
Adam Langley75712922014-10-10 16:23:43 -07002769
Steven Valdeza833c352016-11-01 13:39:36 -04002770 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002771 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002772 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2773 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -04002774 }
2775
David Benjamina01deee2015-12-08 18:56:31 -05002776 /* Discard empty extensions blocks. */
2777 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002778 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002779 }
2780
David Benjamine8d53502015-10-10 14:13:23 -04002781 return CBB_flush(out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002782}
Adam Langley95c29f32014-06-20 12:00:00 -07002783
David Benjamin8c880a22016-12-03 02:20:34 -05002784int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2785 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002786 CBB extensions;
2787 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002788 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002789 }
2790
David Benjamin8c880a22016-12-03 02:20:34 -05002791 for (unsigned i = 0; i < kNumExtensions; i++) {
2792 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002793 /* Don't send extensions that were not received. */
2794 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002795 }
Adam Langley95c29f32014-06-20 12:00:00 -07002796
David Benjamin8c880a22016-12-03 02:20:34 -05002797 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002798 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002799 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002800 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002801 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002802 }
Adam Langley95c29f32014-06-20 12:00:00 -07002803
David Benjamin2bd19172016-11-17 16:47:15 +09002804 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002805 goto err;
2806 }
2807
Steven Valdez143e8b32016-07-11 13:19:03 -04002808 /* Discard empty extensions blocks before TLS 1.3. */
2809 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2810 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002811 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002812 }
2813
David Benjamin56380462015-10-10 14:59:09 -04002814 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002815
2816err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002817 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002818 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002819}
Adam Langley95c29f32014-06-20 12:00:00 -07002820
David Benjamin731058e2016-12-03 23:15:13 -05002821static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2822 const SSL_CLIENT_HELLO *client_hello,
2823 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002824 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002825 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002826 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002827 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002828 }
2829 }
2830
David Benjamin8c880a22016-12-03 02:20:34 -05002831 hs->extensions.received = 0;
2832 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002833
David Benjamine14ff062016-08-09 16:21:24 -04002834 CBS extensions;
2835 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2836 while (CBS_len(&extensions) != 0) {
2837 uint16_t type;
2838 CBS extension;
2839
2840 /* Decode the next extension. */
2841 if (!CBS_get_u16(&extensions, &type) ||
2842 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002843 *out_alert = SSL_AD_DECODE_ERROR;
2844 return 0;
2845 }
Adam Langley95c29f32014-06-20 12:00:00 -07002846
David Benjamine14ff062016-08-09 16:21:24 -04002847 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2848 * ambiguous. Ignore all but the renegotiation_info extension. */
2849 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2850 continue;
2851 }
Adam Langley95c29f32014-06-20 12:00:00 -07002852
David Benjamine14ff062016-08-09 16:21:24 -04002853 unsigned ext_index;
2854 const struct tls_extension *const ext =
2855 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002856
David Benjamine14ff062016-08-09 16:21:24 -04002857 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002858 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002859 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002860 return 0;
2861 }
David Benjamine14ff062016-08-09 16:21:24 -04002862 continue;
2863 }
2864
David Benjamin8c880a22016-12-03 02:20:34 -05002865 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002866 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002867 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002868 *out_alert = alert;
2869 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002870 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002871 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002872 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002873 }
Adam Langley75712922014-10-10 16:23:43 -07002874
David Benjamin1deb41b2016-08-09 19:36:38 -04002875 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002876 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002877 continue;
2878 }
2879
2880 CBS *contents = NULL, fake_contents;
2881 static const uint8_t kFakeRenegotiateExtension[] = {0};
2882 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2883 ssl_client_cipher_list_contains_cipher(client_hello,
2884 SSL3_CK_SCSV & 0xffff)) {
2885 /* The renegotiation SCSV was received so pretend that we received a
2886 * renegotiation extension. */
2887 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2888 sizeof(kFakeRenegotiateExtension));
2889 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002890 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002891 }
2892
2893 /* Extension wasn't observed so call the callback with a NULL
2894 * parameter. */
2895 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002896 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002897 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002898 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002899 *out_alert = alert;
2900 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002901 }
2902 }
2903
Adam Langleyfcf25832014-12-18 17:42:32 -08002904 return 1;
2905}
Adam Langley95c29f32014-06-20 12:00:00 -07002906
David Benjamin731058e2016-12-03 23:15:13 -05002907int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2908 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002909 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002910 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002911 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002912 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002913 return 0;
2914 }
Adam Langley95c29f32014-06-20 12:00:00 -07002915
David Benjamin8c880a22016-12-03 02:20:34 -05002916 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002917 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002918 return 0;
2919 }
Adam Langley95c29f32014-06-20 12:00:00 -07002920
Adam Langleyfcf25832014-12-18 17:42:32 -08002921 return 1;
2922}
Adam Langley95c29f32014-06-20 12:00:00 -07002923
David Benjamin8c880a22016-12-03 02:20:34 -05002924static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2925 int *out_alert) {
2926 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002927 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2928 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2929 return 1;
2930 }
Adam Langley614c66a2015-06-12 15:26:58 -07002931
Steven Valdez143e8b32016-07-11 13:19:03 -04002932 /* Decode the extensions block and check it is valid. */
2933 CBS extensions;
2934 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2935 !tls1_check_duplicate_extensions(&extensions)) {
2936 *out_alert = SSL_AD_DECODE_ERROR;
2937 return 0;
2938 }
2939
2940 uint32_t received = 0;
2941 while (CBS_len(&extensions) != 0) {
2942 uint16_t type;
2943 CBS extension;
2944
2945 /* Decode the next extension. */
2946 if (!CBS_get_u16(&extensions, &type) ||
2947 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002948 *out_alert = SSL_AD_DECODE_ERROR;
2949 return 0;
2950 }
Adam Langley95c29f32014-06-20 12:00:00 -07002951
Steven Valdez143e8b32016-07-11 13:19:03 -04002952 unsigned ext_index;
2953 const struct tls_extension *const ext =
2954 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002955
Steven Valdez143e8b32016-07-11 13:19:03 -04002956 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002957 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002958 return 0;
2959 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002960 continue;
2961 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002962
David Benjamin5db7c9b2017-01-24 16:17:03 -05002963 OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
2964 too_many_bits);
2965
David Benjamin8c880a22016-12-03 02:20:34 -05002966 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002967 type != TLSEXT_TYPE_renegotiate) {
2968 /* If the extension was never sent then it is illegal, except for the
2969 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002970 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2971 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002972 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002973 return 0;
2974 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002975
Steven Valdez143e8b32016-07-11 13:19:03 -04002976 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002977
Steven Valdez143e8b32016-07-11 13:19:03 -04002978 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002979 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002980 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002981 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04002982 *out_alert = alert;
2983 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002984 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002985 }
Adam Langley95c29f32014-06-20 12:00:00 -07002986
David Benjamin54091232016-09-05 12:47:25 -04002987 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002988 if (!(received & (1u << i))) {
2989 /* Extension wasn't observed so call the callback with a NULL
2990 * parameter. */
2991 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002992 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002993 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002994 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002995 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002996 return 0;
2997 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002998 }
2999 }
Adam Langley95c29f32014-06-20 12:00:00 -07003000
Adam Langleyfcf25832014-12-18 17:42:32 -08003001 return 1;
3002}
Adam Langley95c29f32014-06-20 12:00:00 -07003003
David Benjamin8c880a22016-12-03 02:20:34 -05003004static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3005 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003006 int ret = SSL_TLSEXT_ERR_NOACK;
3007 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003008
David Benjamin78f8aab2016-03-10 16:33:58 -05003009 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003010 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003011 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05003012 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
3013 ret = ssl->session_ctx->tlsext_servername_callback(
3014 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003015 }
Adam Langley95c29f32014-06-20 12:00:00 -07003016
Adam Langleyfcf25832014-12-18 17:42:32 -08003017 switch (ret) {
3018 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003019 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003020 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003021
Adam Langleyfcf25832014-12-18 17:42:32 -08003022 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003023 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003024 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003025
Adam Langleyfcf25832014-12-18 17:42:32 -08003026 default:
3027 return 1;
3028 }
3029}
Adam Langleyed8270a2014-09-02 13:52:56 -07003030
David Benjamin8c880a22016-12-03 02:20:34 -05003031int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3032 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08003033 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003034 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003035 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003036 return 0;
3037 }
3038
Adam Langleyfcf25832014-12-18 17:42:32 -08003039 return 1;
3040}
Adam Langley95c29f32014-06-20 12:00:00 -07003041
Adam Langley4c341d02017-03-08 19:33:21 -08003042static enum ssl_ticket_aead_result_t
3043ssl_decrypt_ticket_with_cipher_ctx(SSL *ssl, uint8_t **out, size_t *out_len,
3044 int *out_renew_ticket, const uint8_t *ticket,
3045 size_t ticket_len) {
Adam Langley4c341d02017-03-08 19:33:21 -08003046 const SSL_CTX *const ssl_ctx = ssl->session_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07003047
David Benjamin81678aa2017-07-12 22:43:42 -04003048 bssl::ScopedHMAC_CTX hmac_ctx;
3049 bssl::ScopedEVP_CIPHER_CTX cipher_ctx;
David Benjamine3aa1d92015-06-16 15:34:50 -04003050
David Benjaminadcc3952015-04-26 13:07:57 -04003051 /* Ensure there is room for the key name and the largest IV
3052 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3053 * the maximum IV length should be well under the minimum size for the
3054 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003055 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
David Benjamin81678aa2017-07-12 22:43:42 -04003056 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003057 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003058 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003059
David Benjamine3aa1d92015-06-16 15:34:50 -04003060 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003061 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
David Benjamin81678aa2017-07-12 22:43:42 -04003062 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(),
3063 hmac_ctx.get(), 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003064 if (cb_ret < 0) {
David Benjamin81678aa2017-07-12 22:43:42 -04003065 return ssl_ticket_aead_error;
Adam Langley4c341d02017-03-08 19:33:21 -08003066 } else if (cb_ret == 0) {
David Benjamin81678aa2017-07-12 22:43:42 -04003067 return ssl_ticket_aead_ignore_ticket;
Adam Langley4c341d02017-03-08 19:33:21 -08003068 } else if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003069 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003070 }
3071 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003072 /* Check the key name matches. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003073 if (OPENSSL_memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3074 SSL_TICKET_KEY_NAME_LEN) != 0) {
David Benjamin81678aa2017-07-12 22:43:42 -04003075 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003076 }
David Benjamin81678aa2017-07-12 22:43:42 -04003077 if (!HMAC_Init_ex(hmac_ctx.get(), ssl_ctx->tlsext_tick_hmac_key,
David Benjamine3aa1d92015-06-16 15:34:50 -04003078 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003079 NULL) ||
David Benjamin81678aa2017-07-12 22:43:42 -04003080 !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL,
David Benjamine3aa1d92015-06-16 15:34:50 -04003081 ssl_ctx->tlsext_tick_aes_key, iv)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003082 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08003083 }
3084 }
David Benjamin81678aa2017-07-12 22:43:42 -04003085 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx.get());
Adam Langleyfcf25832014-12-18 17:42:32 -08003086
David Benjamine3aa1d92015-06-16 15:34:50 -04003087 /* Check the MAC at the end of the ticket. */
3088 uint8_t mac[EVP_MAX_MD_SIZE];
David Benjamin81678aa2017-07-12 22:43:42 -04003089 size_t mac_len = HMAC_size(hmac_ctx.get());
David Benjamine3aa1d92015-06-16 15:34:50 -04003090 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003091 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamin81678aa2017-07-12 22:43:42 -04003092 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003093 }
David Benjamin81678aa2017-07-12 22:43:42 -04003094 HMAC_Update(hmac_ctx.get(), ticket, ticket_len - mac_len);
3095 HMAC_Final(hmac_ctx.get(), mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003096 int mac_ok =
3097 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3098#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3099 mac_ok = 1;
3100#endif
3101 if (!mac_ok) {
David Benjamin81678aa2017-07-12 22:43:42 -04003102 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003103 }
3104
David Benjamine3aa1d92015-06-16 15:34:50 -04003105 /* Decrypt the session data. */
3106 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3107 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3108 mac_len;
David Benjamin81678aa2017-07-12 22:43:42 -04003109 bssl::UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len));
3110 if (!plaintext) {
3111 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08003112 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003113 size_t plaintext_len;
3114#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin81678aa2017-07-12 22:43:42 -04003115 OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003116 plaintext_len = ciphertext_len;
3117#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003118 if (ciphertext_len >= INT_MAX) {
David Benjamin81678aa2017-07-12 22:43:42 -04003119 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003120 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003121 int len1, len2;
David Benjamin81678aa2017-07-12 22:43:42 -04003122 if (!EVP_DecryptUpdate(cipher_ctx.get(), plaintext.get(), &len1, ciphertext,
David Benjamine3aa1d92015-06-16 15:34:50 -04003123 (int)ciphertext_len) ||
David Benjamin81678aa2017-07-12 22:43:42 -04003124 !EVP_DecryptFinal_ex(cipher_ctx.get(), plaintext.get() + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003125 ERR_clear_error();
David Benjamin81678aa2017-07-12 22:43:42 -04003126 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003127 }
Adam Langley4c341d02017-03-08 19:33:21 -08003128 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04003129#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003130
David Benjamin81678aa2017-07-12 22:43:42 -04003131 *out = plaintext.release();
Adam Langley4c341d02017-03-08 19:33:21 -08003132 *out_len = plaintext_len;
David Benjamin81678aa2017-07-12 22:43:42 -04003133 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08003134}
3135
3136static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
3137 SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
3138 const uint8_t *ticket, size_t ticket_len) {
David Benjamin81678aa2017-07-12 22:43:42 -04003139 uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len);
Adam Langley4c341d02017-03-08 19:33:21 -08003140 if (plaintext == NULL) {
3141 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3142 return ssl_ticket_aead_error;
3143 }
3144
3145 size_t plaintext_len;
3146 const enum ssl_ticket_aead_result_t result =
3147 ssl->session_ctx->ticket_aead_method->open(
3148 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3149
3150 if (result == ssl_ticket_aead_success) {
3151 *out = plaintext;
3152 plaintext = NULL;
3153 *out_len = plaintext_len;
3154 }
3155
3156 OPENSSL_free(plaintext);
3157 return result;
3158}
3159
3160enum ssl_ticket_aead_result_t ssl_process_ticket(
3161 SSL *ssl, SSL_SESSION **out_session, int *out_renew_ticket,
3162 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3163 size_t session_id_len) {
3164 *out_renew_ticket = 0;
3165 *out_session = NULL;
3166
3167 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3168 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3169 return ssl_ticket_aead_ignore_ticket;
3170 }
3171
3172 uint8_t *plaintext = NULL;
3173 size_t plaintext_len;
3174 enum ssl_ticket_aead_result_t result;
3175 if (ssl->session_ctx->ticket_aead_method != NULL) {
3176 result = ssl_decrypt_ticket_with_method(
3177 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3178 } else {
3179 result = ssl_decrypt_ticket_with_cipher_ctx(
3180 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3181 }
3182
3183 if (result != ssl_ticket_aead_success) {
3184 return result;
3185 }
3186
David Benjamine3aa1d92015-06-16 15:34:50 -04003187 /* Decode the session. */
Adam Langley46db7af2017-02-01 15:49:37 -08003188 SSL_SESSION *session =
3189 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx);
Adam Langley4c341d02017-03-08 19:33:21 -08003190 OPENSSL_free(plaintext);
3191
David Benjamine3aa1d92015-06-16 15:34:50 -04003192 if (session == NULL) {
3193 ERR_clear_error(); /* Don't leave an error on the queue. */
Adam Langley4c341d02017-03-08 19:33:21 -08003194 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003195 }
3196
3197 /* Copy the client's session ID into the new session, to denote the ticket has
3198 * been accepted. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003199 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003200 session->session_id_length = session_id_len;
3201
3202 *out_session = session;
Adam Langley4c341d02017-03-08 19:33:21 -08003203 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003204}
Adam Langley95c29f32014-06-20 12:00:00 -07003205
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003206int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003207 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003208 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003209 return 1;
3210 }
David Benjamincd996942014-07-20 16:23:51 -04003211
David Benjamin0fc37ef2016-08-17 15:29:46 -04003212 OPENSSL_free(hs->peer_sigalgs);
3213 hs->peer_sigalgs = NULL;
3214 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003215
3216 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003217 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003218 return 0;
3219 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003220 num_sigalgs /= 2;
3221
3222 /* supported_signature_algorithms in the certificate request is
3223 * allowed to be empty. */
3224 if (num_sigalgs == 0) {
3225 return 1;
3226 }
3227
Steven Valdez02563852016-06-23 13:33:05 -04003228 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3229 * and we just divided |num_sigalgs| by two. */
David Benjamin81678aa2017-07-12 22:43:42 -04003230 hs->peer_sigalgs = (uint16_t *)OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003231 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003232 return 0;
3233 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003234 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003235
3236 CBS sigalgs;
3237 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003238 for (size_t i = 0; i < num_sigalgs; i++) {
3239 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003240 return 0;
3241 }
3242 }
Adam Langley95c29f32014-06-20 12:00:00 -07003243
Adam Langleyfcf25832014-12-18 17:42:32 -08003244 return 1;
3245}
David Benjaminec2f27d2014-11-13 19:17:25 -05003246
David Benjamina3651382017-04-20 17:49:36 -04003247int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
3248 switch (EVP_PKEY_id(pkey)) {
3249 case EVP_PKEY_RSA:
3250 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3251 return 1;
3252 case EVP_PKEY_EC:
3253 *out = SSL_SIGN_ECDSA_SHA1;
3254 return 1;
3255 default:
3256 return 0;
3257 }
3258}
3259
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003260int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3261 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003262 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003263
Steven Valdezf0451ca2016-06-29 13:16:27 -04003264 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
David Benjamina3651382017-04-20 17:49:36 -04003265 * handshake. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04003266 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamina3651382017-04-20 17:49:36 -04003267 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey)) {
3268 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3269 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003270 }
David Benjamina3651382017-04-20 17:49:36 -04003271 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003272 }
3273
David Benjamin3ef76972016-10-17 17:59:54 -04003274 const uint16_t *sigalgs = cert->sigalgs;
3275 size_t num_sigalgs = cert->num_sigalgs;
3276 if (sigalgs == NULL) {
3277 sigalgs = kSignSignatureAlgorithms;
3278 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003279 }
3280
David Benjamin0fc37ef2016-08-17 15:29:46 -04003281 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3282 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3283 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003284 /* If the client didn't specify any signature_algorithms extension then
3285 * we can assume that it supports SHA1. See
3286 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3287 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3288 SSL_SIGN_ECDSA_SHA1};
3289 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003290 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003291 }
3292
David Benjamin0fc37ef2016-08-17 15:29:46 -04003293 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003294 uint16_t sigalg = sigalgs[i];
3295 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3296 * negotiated. */
3297 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjamina232a712017-03-30 15:51:53 -05003298 !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003299 continue;
3300 }
3301
David Benjamin0fc37ef2016-08-17 15:29:46 -04003302 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003303 if (sigalg == peer_sigalgs[j]) {
3304 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003305 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003306 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003307 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003308 }
Adam Langley95c29f32014-06-20 12:00:00 -07003309
David Benjaminea9a0d52016-07-08 15:52:59 -07003310 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3311 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003312}
Adam Langley95c29f32014-06-20 12:00:00 -07003313
Steven Valdez908ac192017-01-12 13:17:07 -05003314int tls1_verify_channel_id(SSL_HANDSHAKE *hs) {
3315 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003316 uint16_t extension_type;
3317 CBS extension, channel_id;
3318
3319 /* A Channel ID handshake message is structured to contain multiple
3320 * extensions, but the only one that can be present is Channel ID. */
3321 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3322 if (!CBS_get_u16(&channel_id, &extension_type) ||
3323 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3324 CBS_len(&channel_id) != 0 ||
3325 extension_type != TLSEXT_TYPE_channel_id ||
3326 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3327 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3328 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3329 return 0;
3330 }
3331
David Benjamin81678aa2017-07-12 22:43:42 -04003332 bssl::UniquePtr<EC_GROUP> p256(
3333 EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Nick Harper60a85cb2016-09-23 16:25:11 -07003334 if (!p256) {
3335 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3336 return 0;
3337 }
3338
David Benjamin81678aa2017-07-12 22:43:42 -04003339 bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3340 bssl::UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
3341 if (!sig || !x || !y) {
3342 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003343 }
3344
3345 const uint8_t *p = CBS_data(&extension);
David Benjamin81678aa2017-07-12 22:43:42 -04003346 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3347 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3348 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3349 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
3350 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003351 }
3352
David Benjamin81678aa2017-07-12 22:43:42 -04003353 bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
3354 bssl::UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
3355 if (!key || !point ||
3356 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3357 y.get(), nullptr) ||
3358 !EC_KEY_set_group(key.get(), p256.get()) ||
3359 !EC_KEY_set_public_key(key.get(), point.get())) {
3360 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003361 }
3362
3363 uint8_t digest[EVP_MAX_MD_SIZE];
3364 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003365 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003366 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003367 }
3368
David Benjamin81678aa2017-07-12 22:43:42 -04003369 int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
Nick Harper60a85cb2016-09-23 16:25:11 -07003370#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3371 sig_ok = 1;
3372#endif
3373 if (!sig_ok) {
3374 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3375 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3376 ssl->s3->tlsext_channel_id_valid = 0;
David Benjamin81678aa2017-07-12 22:43:42 -04003377 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003378 }
3379
David Benjamin17cf2cb2016-12-13 01:07:13 -05003380 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
David Benjamin81678aa2017-07-12 22:43:42 -04003381 return 1;
Nick Harper60a85cb2016-09-23 16:25:11 -07003382}
3383
Steven Valdez908ac192017-01-12 13:17:07 -05003384int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3385 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003386 uint8_t digest[EVP_MAX_MD_SIZE];
3387 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003388 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003389 return 0;
3390 }
3391
3392 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3393 if (ec_key == NULL) {
3394 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3395 return 0;
3396 }
3397
3398 int ret = 0;
3399 BIGNUM *x = BN_new();
3400 BIGNUM *y = BN_new();
3401 ECDSA_SIG *sig = NULL;
3402 if (x == NULL || y == NULL ||
3403 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3404 EC_KEY_get0_public_key(ec_key),
3405 x, y, NULL)) {
3406 goto err;
3407 }
3408
3409 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3410 if (sig == NULL) {
3411 goto err;
3412 }
3413
3414 CBB child;
3415 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3416 !CBB_add_u16_length_prefixed(cbb, &child) ||
3417 !BN_bn2cbb_padded(&child, 32, x) ||
3418 !BN_bn2cbb_padded(&child, 32, y) ||
3419 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3420 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3421 !CBB_flush(cbb)) {
3422 goto err;
3423 }
3424
3425 ret = 1;
3426
3427err:
3428 BN_free(x);
3429 BN_free(y);
3430 ECDSA_SIG_free(sig);
3431 return ret;
3432}
3433
Steven Valdez908ac192017-01-12 13:17:07 -05003434int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3435 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003436 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3437 uint8_t *msg;
3438 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003439 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003440 ssl_cert_verify_channel_id)) {
3441 return 0;
3442 }
3443 SHA256(msg, msg_len, out);
3444 *out_len = SHA256_DIGEST_LENGTH;
3445 OPENSSL_free(msg);
3446 return 1;
3447 }
3448
Nick Harper95594012016-10-20 14:07:13 -07003449 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003450
Nick Harper95594012016-10-20 14:07:13 -07003451 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003452 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003453 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003454
Steven Valdez87eab492016-06-27 16:34:59 -04003455 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003456 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003457 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003458 if (ssl->session->original_handshake_hash_len == 0) {
3459 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003460 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003461 }
Nick Harper95594012016-10-20 14:07:13 -07003462 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3463 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003464 }
3465
Steven Valdez908ac192017-01-12 13:17:07 -05003466 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3467 size_t hs_hash_len;
3468 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript, hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003469 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003470 }
Steven Valdez908ac192017-01-12 13:17:07 -05003471 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003472 SHA256_Final(out, &ctx);
3473 *out_len = SHA256_DIGEST_LENGTH;
3474 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003475}
Adam Langley1258b6a2014-06-20 12:00:00 -07003476
3477/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin45738dd2017-02-09 20:01:26 -05003478 * hashes in |hs->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003479 * data. */
Steven Valdez908ac192017-01-12 13:17:07 -05003480int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3481 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003482 /* This function should never be called for a resumed session because the
3483 * handshake hashes that we wish to record are for the original, full
3484 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003485 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003486 return -1;
3487 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003488
Steven Valdez908ac192017-01-12 13:17:07 -05003489 OPENSSL_COMPILE_ASSERT(
David Benjamin45738dd2017-02-09 20:01:26 -05003490 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
Steven Valdez908ac192017-01-12 13:17:07 -05003491 original_handshake_hash_is_too_small);
3492
3493 size_t digest_len;
3494 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript,
David Benjamin45738dd2017-02-09 20:01:26 -05003495 hs->new_session->original_handshake_hash,
Steven Valdez908ac192017-01-12 13:17:07 -05003496 &digest_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003497 return -1;
3498 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003499
Steven Valdez908ac192017-01-12 13:17:07 -05003500 OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE <= 0xff, max_md_size_is_too_large);
David Benjamin45738dd2017-02-09 20:01:26 -05003501 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003502
Adam Langleyfcf25832014-12-18 17:42:32 -08003503 return 1;
3504}
Nick Harper60a85cb2016-09-23 16:25:11 -07003505
3506int ssl_do_channel_id_callback(SSL *ssl) {
3507 if (ssl->tlsext_channel_id_private != NULL ||
3508 ssl->ctx->channel_id_cb == NULL) {
3509 return 1;
3510 }
3511
3512 EVP_PKEY *key = NULL;
3513 ssl->ctx->channel_id_cb(ssl, &key);
3514 if (key == NULL) {
3515 /* The caller should try again later. */
3516 return 1;
3517 }
3518
3519 int ret = SSL_set1_tls_channel_id(ssl, key);
3520 EVP_PKEY_free(key);
3521 return ret;
3522}
Adam Langleycfa08c32016-11-17 13:21:27 -08003523
3524int ssl_is_sct_list_valid(const CBS *contents) {
3525 /* Shallow parse the SCT list for sanity. By the RFC
3526 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3527 * of the SCTs may be empty. */
3528 CBS copy = *contents;
3529 CBS sct_list;
3530 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3531 CBS_len(&copy) != 0 ||
3532 CBS_len(&sct_list) == 0) {
3533 return 0;
3534 }
3535
3536 while (CBS_len(&sct_list) > 0) {
3537 CBS sct;
3538 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3539 CBS_len(&sct) == 0) {
3540 return 0;
3541 }
3542 }
3543
3544 return 1;
3545}