blob: 338f78e93e5b1584cc29df7987e4c6a07d3fecce [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 Benjamin86e95b82017-07-18 16:34:25 -0400109#define BORINGSSL_INTERNAL_CXX_TYPES
110
David Benjamin9e4e01e2015-09-15 01:48:04 -0400111#include <openssl/ssl.h>
112
David Benjaminf0ae1702015-04-07 23:05:04 -0400113#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400114#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400115#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400116#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700117
David Benjamin03973092014-06-24 23:27:17 -0400118#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400119#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400120#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700121#include <openssl/evp.h>
122#include <openssl/hmac.h>
123#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400124#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125#include <openssl/rand.h>
126
David Benjamin2ee94aa2015-04-07 22:38:30 -0400127#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400128#include "../crypto/internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800129
130
David Benjamin86e95b82017-07-18 16:34:25 -0400131namespace bssl {
132
David Benjamin8c880a22016-12-03 02:20:34 -0500133static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
Adam Langley95c29f32014-06-20 12:00:00 -0700134
Adam Langleyfcf25832014-12-18 17:42:32 -0800135static int compare_uint16_t(const void *p1, const void *p2) {
136 uint16_t u1 = *((const uint16_t *)p1);
137 uint16_t u2 = *((const uint16_t *)p2);
138 if (u1 < u2) {
139 return -1;
140 } else if (u1 > u2) {
141 return 1;
142 } else {
143 return 0;
144 }
145}
David Benjamin35a7a442014-07-05 00:23:20 -0400146
Adam Langleyfcf25832014-12-18 17:42:32 -0800147/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
148 * more than one extension of the same type in a ClientHello or ServerHello.
149 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400150 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800151static int tls1_check_duplicate_extensions(const CBS *cbs) {
152 CBS extensions = *cbs;
153 size_t num_extensions = 0, i = 0;
154 uint16_t *extension_types = NULL;
155 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400156
Adam Langleyfcf25832014-12-18 17:42:32 -0800157 /* First pass: count the extensions. */
158 while (CBS_len(&extensions) > 0) {
159 uint16_t type;
160 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400161
Adam Langleyfcf25832014-12-18 17:42:32 -0800162 if (!CBS_get_u16(&extensions, &type) ||
163 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
164 goto done;
165 }
David Benjamin35a7a442014-07-05 00:23:20 -0400166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 num_extensions++;
168 }
David Benjamin35a7a442014-07-05 00:23:20 -0400169
Adam Langleyfcf25832014-12-18 17:42:32 -0800170 if (num_extensions == 0) {
171 return 1;
172 }
David Benjamin9a373592014-07-25 04:27:53 -0400173
David Benjamin81678aa2017-07-12 22:43:42 -0400174 extension_types =
175 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800176 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400177 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 goto done;
179 }
David Benjamin35a7a442014-07-05 00:23:20 -0400180
Adam Langleyfcf25832014-12-18 17:42:32 -0800181 /* Second pass: gather the extension types. */
182 extensions = *cbs;
183 for (i = 0; i < num_extensions; i++) {
184 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400185
Adam Langleyfcf25832014-12-18 17:42:32 -0800186 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
187 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
188 /* This should not happen. */
189 goto done;
190 }
191 }
192 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400193
Adam Langleyfcf25832014-12-18 17:42:32 -0800194 /* Sort the extensions and make sure there are no duplicates. */
195 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
196 for (i = 1; i < num_extensions; i++) {
197 if (extension_types[i - 1] == extension_types[i]) {
198 goto done;
199 }
200 }
David Benjamin35a7a442014-07-05 00:23:20 -0400201
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 ret = 1;
203
David Benjamin35a7a442014-07-05 00:23:20 -0400204done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400205 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800206 return ret;
207}
David Benjamin35a7a442014-07-05 00:23:20 -0400208
David Benjamin731058e2016-12-03 23:15:13 -0500209int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out, const uint8_t *in,
210 size_t in_len) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500211 OPENSSL_memset(out, 0, sizeof(*out));
David Benjamin731058e2016-12-03 23:15:13 -0500212 out->ssl = ssl;
213 out->client_hello = in;
214 out->client_hello_len = in_len;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400215
David Benjamine14ff062016-08-09 16:21:24 -0400216 CBS client_hello, random, session_id;
David Benjamin731058e2016-12-03 23:15:13 -0500217 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
218 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjamine14ff062016-08-09 16:21:24 -0400219 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
220 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
221 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800222 return 0;
223 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700224
David Benjamin731058e2016-12-03 23:15:13 -0500225 out->random = CBS_data(&random);
226 out->random_len = CBS_len(&random);
227 out->session_id = CBS_data(&session_id);
228 out->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700229
Adam Langleyfcf25832014-12-18 17:42:32 -0800230 /* Skip past DTLS cookie */
David Benjamin731058e2016-12-03 23:15:13 -0500231 if (SSL_is_dtls(out->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400233 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
234 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800235 return 0;
236 }
237 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700238
David Benjamine14ff062016-08-09 16:21:24 -0400239 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400241 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
242 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800243 CBS_len(&compression_methods) < 1) {
244 return 0;
245 }
David Benjamine14ff062016-08-09 16:21:24 -0400246
David Benjamin731058e2016-12-03 23:15:13 -0500247 out->cipher_suites = CBS_data(&cipher_suites);
248 out->cipher_suites_len = CBS_len(&cipher_suites);
249 out->compression_methods = CBS_data(&compression_methods);
250 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700251
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 /* If the ClientHello ends here then it's valid, but doesn't have any
253 * extensions. (E.g. SSLv3.) */
254 if (CBS_len(&client_hello) == 0) {
David Benjamin731058e2016-12-03 23:15:13 -0500255 out->extensions = NULL;
256 out->extensions_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 return 1;
258 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700259
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 /* Extract extensions and check it is valid. */
David Benjamine14ff062016-08-09 16:21:24 -0400261 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
263 !tls1_check_duplicate_extensions(&extensions) ||
264 CBS_len(&client_hello) != 0) {
265 return 0;
266 }
David Benjamine14ff062016-08-09 16:21:24 -0400267
David Benjamin731058e2016-12-03 23:15:13 -0500268 out->extensions = CBS_data(&extensions);
269 out->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700270
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 return 1;
272}
Adam Langleydc9b1412014-06-20 12:00:00 -0700273
David Benjamin731058e2016-12-03 23:15:13 -0500274int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
275 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 CBS extensions;
David Benjamin731058e2016-12-03 23:15:13 -0500277 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 while (CBS_len(&extensions) != 0) {
David Benjamincec73442016-08-02 17:41:33 -0400279 /* Decode the next extension. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 uint16_t type;
281 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800282 if (!CBS_get_u16(&extensions, &type) ||
283 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
284 return 0;
285 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700286
Adam Langleyfcf25832014-12-18 17:42:32 -0800287 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400288 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 return 1;
290 }
291 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700292
Adam Langleyfcf25832014-12-18 17:42:32 -0800293 return 0;
294}
Adam Langley95c29f32014-06-20 12:00:00 -0700295
Steven Valdezce902a92016-05-17 11:47:53 -0400296static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400297 SSL_CURVE_X25519,
298 SSL_CURVE_SECP256R1,
299 SSL_CURVE_SECP384R1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800300};
Adam Langley95c29f32014-06-20 12:00:00 -0700301
David Benjaminf04976b2016-10-07 00:37:55 -0400302void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
Steven Valdez5440fe02016-07-18 12:40:30 -0400303 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400304 *out_group_ids = ssl->supported_group_list;
305 *out_group_ids_len = ssl->supported_group_list_len;
306 if (!*out_group_ids) {
307 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400308 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800309 }
310}
David Benjamined439582014-07-14 19:13:02 -0400311
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900312int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
313 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400314 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400315
David Benjaminf04976b2016-10-07 00:37:55 -0400316 const uint16_t *groups, *pref, *supp;
317 size_t groups_len, pref_len, supp_len;
318 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800319
David Benjaminf04976b2016-10-07 00:37:55 -0400320 /* Clients are not required to send a supported_groups extension. In this
321 * case, the server is free to pick any group it likes. See RFC 4492,
322 * section 4, paragraph 3.
323 *
324 * However, in the interests of compatibility, we will skip ECDH if the
325 * client didn't send an extension because we can't be sure that they'll
326 * support our favoured group. Thus we do not special-case an emtpy
327 * |peer_supported_group_list|. */
David Benjamin55a43642015-04-20 14:45:55 -0400328
David Benjamin4298d772015-12-19 00:18:25 -0500329 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400330 pref = groups;
331 pref_len = groups_len;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900332 supp = hs->peer_supported_group_list;
333 supp_len = hs->peer_supported_group_list_len;
David Benjamin55a43642015-04-20 14:45:55 -0400334 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900335 pref = hs->peer_supported_group_list;
336 pref_len = hs->peer_supported_group_list_len;
Steven Valdezce902a92016-05-17 11:47:53 -0400337 supp = groups;
338 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400339 }
340
David Benjaminf04976b2016-10-07 00:37:55 -0400341 for (size_t i = 0; i < pref_len; i++) {
342 for (size_t j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800343 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400344 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500345 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800346 }
347 }
348 }
349
David Benjamin4298d772015-12-19 00:18:25 -0500350 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800351}
Adam Langley95c29f32014-06-20 12:00:00 -0700352
Steven Valdezce902a92016-05-17 11:47:53 -0400353int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 const int *curves, size_t ncurves) {
David Benjamin81678aa2017-07-12 22:43:42 -0400355 uint16_t *group_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
Steven Valdezce902a92016-05-17 11:47:53 -0400356 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 return 0;
358 }
359
David Benjamin54091232016-09-05 12:47:25 -0400360 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400361 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
362 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 return 0;
364 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 }
366
Steven Valdezce902a92016-05-17 11:47:53 -0400367 OPENSSL_free(*out_group_ids);
368 *out_group_ids = group_ids;
369 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800370
371 return 1;
372}
Adam Langley95c29f32014-06-20 12:00:00 -0700373
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100374int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
375 const char *curves) {
376 uint16_t *group_ids = NULL;
377 size_t ncurves = 0;
378
379 const char *col;
380 const char *ptr = curves;
381
382 do {
383 col = strchr(ptr, ':');
384
385 uint16_t group_id;
386 if (!ssl_name_to_group_id(&group_id, ptr,
387 col ? (size_t)(col - ptr) : strlen(ptr))) {
388 goto err;
389 }
390
David Benjamin81678aa2017-07-12 22:43:42 -0400391 uint16_t *new_group_ids = (uint16_t *)OPENSSL_realloc(
392 group_ids, (ncurves + 1) * sizeof(uint16_t));
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100393 if (new_group_ids == NULL) {
394 goto err;
395 }
396 group_ids = new_group_ids;
397
398 group_ids[ncurves] = group_id;
399 ncurves++;
400
401 if (col) {
402 ptr = col + 1;
403 }
404 } while (col);
405
406 OPENSSL_free(*out_group_ids);
407 *out_group_ids = group_ids;
408 *out_group_ids_len = ncurves;
409
410 return 1;
411
412err:
413 OPENSSL_free(group_ids);
414 return 0;
415}
416
Steven Valdezce902a92016-05-17 11:47:53 -0400417int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
418 const uint16_t *groups;
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400419 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -0400420 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400421 for (size_t i = 0; i < groups_len; i++) {
422 if (groups[i] == group_id) {
423 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800424 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 }
David Benjamin033e5f42014-11-13 18:47:41 -0500426
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400427 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800428}
David Benjamin033e5f42014-11-13 18:47:41 -0500429
David Benjamin3ef76972016-10-17 17:59:54 -0400430/* kVerifySignatureAlgorithms is the default list of accepted signature
David Benjamin3a322f52016-10-26 12:45:35 -0400431 * algorithms for verifying.
432 *
433 * For now, RSA-PSS signature algorithms are not enabled on Android's system
434 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
435 * restore them. */
David Benjamin3ef76972016-10-17 17:59:54 -0400436static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjamin69522112017-03-28 15:38:29 -0500437 /* List our preferred algorithms first. */
438 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400439 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400440#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400441 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400442#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400443 SSL_SIGN_RSA_PKCS1_SHA256,
444
445 /* Larger hashes are acceptable. */
446 SSL_SIGN_ECDSA_SECP384R1_SHA384,
447#if !defined(BORINGSSL_ANDROID_SYSTEM)
448 SSL_SIGN_RSA_PSS_SHA384,
449#endif
450 SSL_SIGN_RSA_PKCS1_SHA384,
451
David Benjamin76bb1412016-09-08 16:03:49 -0400452 /* TODO(davidben): Remove this. */
David Benjamin3ef76972016-10-17 17:59:54 -0400453#if defined(BORINGSSL_ANDROID_SYSTEM)
454 SSL_SIGN_ECDSA_SECP521R1_SHA512,
455#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400456#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400457 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400458#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400459 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400460
David Benjamin3a322f52016-10-26 12:45:35 -0400461 /* For now, SHA-1 is still accepted but least preferable. */
462 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400463
David Benjamin3a322f52016-10-26 12:45:35 -0400464};
465
466/* kSignSignatureAlgorithms is the default list of supported signature
467 * algorithms for signing.
468 *
469 * For now, RSA-PSS signature algorithms are not enabled on Android's system
470 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
471 * restore them. */
472static const uint16_t kSignSignatureAlgorithms[] = {
David Benjamin69522112017-03-28 15:38:29 -0500473 /* List our preferred algorithms first. */
474 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400475 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400476#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400477 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400478#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400479 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400480
David Benjamin3a322f52016-10-26 12:45:35 -0400481 /* If needed, sign larger hashes.
482 *
483 * TODO(davidben): Determine which of these may be pruned. */
484 SSL_SIGN_ECDSA_SECP384R1_SHA384,
485#if !defined(BORINGSSL_ANDROID_SYSTEM)
486 SSL_SIGN_RSA_PSS_SHA384,
487#endif
488 SSL_SIGN_RSA_PKCS1_SHA384,
489
490 SSL_SIGN_ECDSA_SECP521R1_SHA512,
491#if !defined(BORINGSSL_ANDROID_SYSTEM)
492 SSL_SIGN_RSA_PSS_SHA512,
493#endif
494 SSL_SIGN_RSA_PKCS1_SHA512,
495
496 /* If the peer supports nothing else, sign with SHA-1. */
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400497 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400498 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400499};
500
David Benjamin69522112017-03-28 15:38:29 -0500501int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) {
David Benjamin71c21b42017-04-14 17:05:40 -0400502 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
503 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
504 if (ssl->ctx->num_verify_sigalgs != 0) {
505 sigalgs = ssl->ctx->verify_sigalgs;
506 num_sigalgs = ssl->ctx->num_verify_sigalgs;
507 }
508
509 for (size_t i = 0; i < num_sigalgs; i++) {
510 if (sigalgs == kVerifySignatureAlgorithms &&
511 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500512 !ssl->ctx->ed25519_enabled) {
513 continue;
514 }
David Benjamin71c21b42017-04-14 17:05:40 -0400515 if (!CBB_add_u16(out, sigalgs[i])) {
David Benjamin69522112017-03-28 15:38:29 -0500516 return 0;
517 }
518 }
519
520 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800521}
Adam Langley95c29f32014-06-20 12:00:00 -0700522
David Benjamin8d606e32017-06-15 22:43:04 -0400523int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg) {
David Benjamin71c21b42017-04-14 17:05:40 -0400524 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
525 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
526 if (ssl->ctx->num_verify_sigalgs != 0) {
527 sigalgs = ssl->ctx->verify_sigalgs;
528 num_sigalgs = ssl->ctx->num_verify_sigalgs;
529 }
530
531 for (size_t i = 0; i < num_sigalgs; i++) {
532 if (sigalgs == kVerifySignatureAlgorithms &&
533 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500534 !ssl->ctx->ed25519_enabled) {
535 continue;
536 }
David Benjamin71c21b42017-04-14 17:05:40 -0400537 if (sigalg == sigalgs[i]) {
David Benjamin3ef76972016-10-17 17:59:54 -0400538 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 }
540 }
541
David Benjamin3ef76972016-10-17 17:59:54 -0400542 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
543 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
544 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800545}
546
Adam Langley614c66a2015-06-12 15:26:58 -0700547/* tls_extension represents a TLS extension that is handled internally. The
548 * |init| function is called for each handshake, before any other functions of
549 * the extension. Then the add and parse callbacks are called as needed.
550 *
551 * The parse callbacks receive a |CBS| that contains the contents of the
552 * extension (i.e. not including the type and length bytes). If an extension is
553 * not received then the parse callbacks will be called with a NULL CBS so that
554 * they can do any processing needed to handle the absence of an extension.
555 *
556 * The add callbacks receive a |CBB| to which the extension can be appended but
557 * the function is responsible for appending the type and length bytes too.
558 *
559 * All callbacks return one for success and zero for error. If a parse function
560 * returns zero then a fatal alert with value |*out_alert| will be sent. If
561 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
562struct tls_extension {
563 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500564 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700565
David Benjamin8c880a22016-12-03 02:20:34 -0500566 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
567 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
568 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700569
David Benjamin8c880a22016-12-03 02:20:34 -0500570 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
571 CBS *contents);
572 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700573};
574
David Benjamin8c880a22016-12-03 02:20:34 -0500575static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
576 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400577 if (contents != NULL) {
578 /* Servers MUST NOT send this extension. */
579 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
580 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
581 return 0;
582 }
583
584 return 1;
585}
586
David Benjamin8c880a22016-12-03 02:20:34 -0500587static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
588 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400589 /* This extension from the client is handled elsewhere. */
590 return 1;
591}
592
David Benjamin8c880a22016-12-03 02:20:34 -0500593static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400594 return 1;
595}
Adam Langley614c66a2015-06-12 15:26:58 -0700596
597/* Server name indication (SNI).
598 *
599 * https://tools.ietf.org/html/rfc6066#section-3. */
600
David Benjamin8c880a22016-12-03 02:20:34 -0500601static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
602 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700603 if (ssl->tlsext_hostname == NULL) {
604 return 1;
605 }
606
607 CBB contents, server_name_list, name;
608 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
609 !CBB_add_u16_length_prefixed(out, &contents) ||
610 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
611 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
612 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
613 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
614 strlen(ssl->tlsext_hostname)) ||
615 !CBB_flush(out)) {
616 return 0;
617 }
618
619 return 1;
620}
621
David Benjamin8c880a22016-12-03 02:20:34 -0500622static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500623 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500624 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700625 if (contents == NULL) {
626 return 1;
627 }
628
629 if (CBS_len(contents) != 0) {
630 return 0;
631 }
632
633 assert(ssl->tlsext_hostname != NULL);
634
Steven Valdez87eab492016-06-27 16:34:59 -0400635 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500636 OPENSSL_free(hs->new_session->tlsext_hostname);
637 hs->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
638 if (!hs->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700639 *out_alert = SSL_AD_INTERNAL_ERROR;
640 return 0;
641 }
642 }
643
644 return 1;
645}
646
David Benjamin8c880a22016-12-03 02:20:34 -0500647static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500648 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700649 if (contents == NULL) {
650 return 1;
651 }
652
David Benjamin9b611e22016-03-03 08:48:30 -0500653 CBS server_name_list, host_name;
654 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700655 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500656 !CBS_get_u8(&server_name_list, &name_type) ||
657 /* Although the server_name extension was intended to be extensible to
658 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
659 * different name types will cause an error. Further, RFC 4366 originally
660 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
661 * adding new name types is no longer feasible.
662 *
663 * Act as if the extensibility does not exist to simplify parsing. */
664 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
665 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700666 CBS_len(contents) != 0) {
667 return 0;
668 }
669
David Benjamin9b611e22016-03-03 08:48:30 -0500670 if (name_type != TLSEXT_NAMETYPE_host_name ||
671 CBS_len(&host_name) == 0 ||
672 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
673 CBS_contains_zero_byte(&host_name)) {
674 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
675 return 0;
676 }
Adam Langley614c66a2015-06-12 15:26:58 -0700677
David Benjamin4eb95cc2016-11-16 17:08:23 +0900678 /* Copy the hostname as a string. */
David Benjamin8c880a22016-12-03 02:20:34 -0500679 if (!CBS_strdup(&host_name, &hs->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900680 *out_alert = SSL_AD_INTERNAL_ERROR;
681 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700682 }
683
David Benjamin8c880a22016-12-03 02:20:34 -0500684 hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700685 return 1;
686}
687
David Benjamin8c880a22016-12-03 02:20:34 -0500688static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
689 if (hs->ssl->s3->session_reused ||
690 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700691 return 1;
692 }
693
694 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
695 !CBB_add_u16(out, 0 /* length */)) {
696 return 0;
697 }
698
699 return 1;
700}
701
702
Adam Langley5021b222015-06-12 18:27:58 -0700703/* Renegotiation indication.
704 *
705 * https://tools.ietf.org/html/rfc5746 */
706
David Benjamin8c880a22016-12-03 02:20:34 -0500707static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
708 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400709 /* Renegotiation indication is not necessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -0400710 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400711 return 1;
712 }
713
David Benjamin52bf6902016-10-08 12:05:03 -0400714 assert(ssl->s3->initial_handshake_complete ==
715 (ssl->s3->previous_client_finished_len != 0));
716
Adam Langley5021b222015-06-12 18:27:58 -0700717 CBB contents, prev_finished;
718 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
719 !CBB_add_u16_length_prefixed(out, &contents) ||
720 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
721 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
722 ssl->s3->previous_client_finished_len) ||
723 !CBB_flush(out)) {
724 return 0;
725 }
726
727 return 1;
728}
729
David Benjamin8c880a22016-12-03 02:20:34 -0500730static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700731 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500732 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400733 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez246eeee2017-03-26 12:49:17 -0500734 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400735 return 0;
736 }
737
David Benjamin3e052de2015-11-25 20:10:31 -0500738 /* Servers may not switch between omitting the extension and supporting it.
739 * See RFC 5746, sections 3.5 and 4.2. */
740 if (ssl->s3->initial_handshake_complete &&
741 (contents != NULL) != ssl->s3->send_connection_binding) {
742 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
743 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
744 return 0;
745 }
746
Adam Langley5021b222015-06-12 18:27:58 -0700747 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500748 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700749 * RI even on initial ServerHello because the client doesn't see any
750 * renegotiation during an attack. However this would mean we could not
751 * connect to any server which doesn't support RI.
752 *
David Benjamine9cddb82015-11-23 14:36:40 -0500753 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
754 * practical terms every client sets it so it's just assumed here. */
755 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700756 }
757
758 const size_t expected_len = ssl->s3->previous_client_finished_len +
759 ssl->s3->previous_server_finished_len;
760
761 /* Check for logic errors */
762 assert(!expected_len || ssl->s3->previous_client_finished_len);
763 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400764 assert(ssl->s3->initial_handshake_complete ==
765 (ssl->s3->previous_client_finished_len != 0));
766 assert(ssl->s3->initial_handshake_complete ==
767 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700768
769 /* Parse out the extension contents. */
770 CBS renegotiated_connection;
771 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
772 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400773 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700774 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
775 return 0;
776 }
777
778 /* Check that the extension matches. */
779 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400780 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700781 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
782 return 0;
783 }
784
785 const uint8_t *d = CBS_data(&renegotiated_connection);
David Benjamin9343b0b2017-07-01 00:31:27 -0400786 int ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
787 ssl->s3->previous_client_finished_len) == 0;
788#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
789 ok = 1;
790#endif
791 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400792 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700793 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
794 return 0;
795 }
796 d += ssl->s3->previous_client_finished_len;
797
David Benjamin9343b0b2017-07-01 00:31:27 -0400798 ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
799 ssl->s3->previous_server_finished_len) == 0;
800#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
801 ok = 1;
802#endif
803 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400804 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700805 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
806 return 0;
807 }
808 ssl->s3->send_connection_binding = 1;
809
810 return 1;
811}
812
David Benjamin8c880a22016-12-03 02:20:34 -0500813static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700814 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500815 SSL *const ssl = hs->ssl;
Adam Langley5021b222015-06-12 18:27:58 -0700816 /* Renegotiation isn't supported as a server so this function should never be
817 * called after the initial handshake. */
818 assert(!ssl->s3->initial_handshake_complete);
819
Steven Valdez143e8b32016-07-11 13:19:03 -0400820 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
821 return 1;
822 }
823
Adam Langley5021b222015-06-12 18:27:58 -0700824 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400825 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700826 }
827
828 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700829 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
830 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400831 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700832 return 0;
833 }
834
David Benjamin52bf6902016-10-08 12:05:03 -0400835 /* Check that the extension matches. We do not support renegotiation as a
836 * server, so this must be empty. */
837 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400838 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700839 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
840 return 0;
841 }
842
843 ssl->s3->send_connection_binding = 1;
844
845 return 1;
846}
847
David Benjamin8c880a22016-12-03 02:20:34 -0500848static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
849 SSL *const ssl = hs->ssl;
David Benjamin52bf6902016-10-08 12:05:03 -0400850 /* Renegotiation isn't supported as a server so this function should never be
851 * called after the initial handshake. */
852 assert(!ssl->s3->initial_handshake_complete);
853
Steven Valdez143e8b32016-07-11 13:19:03 -0400854 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
855 return 1;
856 }
857
Adam Langley5021b222015-06-12 18:27:58 -0700858 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400859 !CBB_add_u16(out, 1 /* length */) ||
860 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700861 return 0;
862 }
863
864 return 1;
865}
866
Adam Langley0a056712015-07-01 15:03:33 -0700867
868/* Extended Master Secret.
869 *
David Benjamin43946d42016-02-01 08:42:19 -0500870 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700871
David Benjamin8c880a22016-12-03 02:20:34 -0500872static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400873 /* Extended master secret is not necessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -0400874 if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700875 return 1;
876 }
877
878 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
879 !CBB_add_u16(out, 0 /* length */)) {
880 return 0;
881 }
882
883 return 1;
884}
885
David Benjamin8c880a22016-12-03 02:20:34 -0500886static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700887 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500888 SSL *const ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500889
890 if (contents != NULL) {
891 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
892 ssl->version == SSL3_VERSION ||
893 CBS_len(contents) != 0) {
David Benjamin163c9562016-08-29 23:14:17 -0400894 return 0;
895 }
896
David Benjaminfc02b592017-02-17 16:26:01 -0500897 hs->extended_master_secret = 1;
David Benjamin163c9562016-08-29 23:14:17 -0400898 }
899
David Benjaminfc02b592017-02-17 16:26:01 -0500900 /* Whether EMS is negotiated may not change on renegotiation. */
901 if (ssl->s3->established_session != NULL &&
902 hs->extended_master_secret !=
903 ssl->s3->established_session->extended_master_secret) {
904 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
905 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400906 return 0;
907 }
908
Adam Langley0a056712015-07-01 15:03:33 -0700909 return 1;
910}
911
David Benjamin8c880a22016-12-03 02:20:34 -0500912static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500913 CBS *contents) {
David Benjaminfc02b592017-02-17 16:26:01 -0500914 uint16_t version = ssl3_protocol_version(hs->ssl);
David Benjamin8c880a22016-12-03 02:20:34 -0500915 if (version >= TLS1_3_VERSION ||
916 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400917 return 1;
918 }
919
920 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700921 return 1;
922 }
923
924 if (CBS_len(contents) != 0) {
925 return 0;
926 }
927
David Benjaminfc02b592017-02-17 16:26:01 -0500928 hs->extended_master_secret = 1;
Adam Langley0a056712015-07-01 15:03:33 -0700929 return 1;
930}
931
David Benjamin8c880a22016-12-03 02:20:34 -0500932static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -0500933 if (!hs->extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700934 return 1;
935 }
936
937 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
938 !CBB_add_u16(out, 0 /* length */)) {
939 return 0;
940 }
941
942 return 1;
943}
944
Adam Langley9b05bc52015-07-01 15:25:33 -0700945
946/* Session tickets.
947 *
948 * https://tools.ietf.org/html/rfc5077 */
949
David Benjamin8c880a22016-12-03 02:20:34 -0500950static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
951 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400952 /* TLS 1.3 uses a different ticket extension. */
David Benjamin68161cb2017-06-20 14:49:43 -0400953 if (hs->min_version >= TLS1_3_VERSION ||
David Benjamin7c7d8312016-08-20 13:39:03 -0400954 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700955 return 1;
956 }
957
958 const uint8_t *ticket_data = NULL;
959 int ticket_len = 0;
960
961 /* Renegotiation does not participate in session resumption. However, still
962 * advertise the extension to avoid potentially breaking servers which carry
963 * over the state from the previous handshake, such as OpenSSL servers
964 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
965 if (!ssl->s3->initial_handshake_complete &&
966 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400967 ssl->session->tlsext_tick != NULL &&
968 /* Don't send TLS 1.3 session tickets in the ticket extension. */
Steven Valdez8f36c512017-06-20 10:55:02 -0400969 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700970 ticket_data = ssl->session->tlsext_tick;
971 ticket_len = ssl->session->tlsext_ticklen;
972 }
973
974 CBB ticket;
975 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
976 !CBB_add_u16_length_prefixed(out, &ticket) ||
977 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
978 !CBB_flush(out)) {
979 return 0;
980 }
981
982 return 1;
983}
984
David Benjamin8c880a22016-12-03 02:20:34 -0500985static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -0700986 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500987 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -0700988 if (contents == NULL) {
989 return 1;
990 }
991
Steven Valdez143e8b32016-07-11 13:19:03 -0400992 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
993 return 0;
994 }
995
Adam Langley9b05bc52015-07-01 15:25:33 -0700996 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
997 * this function should never be called, even if the server tries to send the
998 * extension. */
999 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1000
1001 if (CBS_len(contents) != 0) {
1002 return 0;
1003 }
1004
David Benjamin8c880a22016-12-03 02:20:34 -05001005 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001006 return 1;
1007}
1008
David Benjamin8c880a22016-12-03 02:20:34 -05001009static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1010 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001011 return 1;
1012 }
1013
David Benjamin78476f62016-11-12 11:20:55 +09001014 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin8c880a22016-12-03 02:20:34 -05001015 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001016
1017 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1018 !CBB_add_u16(out, 0 /* length */)) {
1019 return 0;
1020 }
1021
1022 return 1;
1023}
1024
1025
Adam Langley2e857bd2015-07-01 16:09:19 -07001026/* Signature Algorithms.
1027 *
1028 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1029
David Benjamin8c880a22016-12-03 02:20:34 -05001030static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1031 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001032 if (hs->max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001033 return 1;
1034 }
1035
David Benjamin0fc37ef2016-08-17 15:29:46 -04001036 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001037 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1038 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin69522112017-03-28 15:38:29 -05001039 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
1040 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
1041 !CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001042 return 0;
1043 }
1044
1045 return 1;
1046}
1047
David Benjamin8c880a22016-12-03 02:20:34 -05001048static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001049 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001050 OPENSSL_free(hs->peer_sigalgs);
1051 hs->peer_sigalgs = NULL;
1052 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001053
Adam Langley2e857bd2015-07-01 16:09:19 -07001054 if (contents == NULL) {
1055 return 1;
1056 }
1057
1058 CBS supported_signature_algorithms;
1059 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001060 CBS_len(contents) != 0 ||
1061 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001062 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001063 return 0;
1064 }
1065
1066 return 1;
1067}
1068
Adam Langley2e857bd2015-07-01 16:09:19 -07001069
Adam Langleybb0bd042015-07-01 16:21:03 -07001070/* OCSP Stapling.
1071 *
1072 * https://tools.ietf.org/html/rfc6066#section-8 */
1073
David Benjamin8c880a22016-12-03 02:20:34 -05001074static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1075 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001076 if (!ssl->ocsp_stapling_enabled) {
1077 return 1;
1078 }
1079
1080 CBB contents;
1081 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1082 !CBB_add_u16_length_prefixed(out, &contents) ||
1083 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1084 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1085 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1086 !CBB_flush(out)) {
1087 return 0;
1088 }
1089
1090 return 1;
1091}
1092
David Benjamin8c880a22016-12-03 02:20:34 -05001093static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001094 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001095 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001096 if (contents == NULL) {
1097 return 1;
1098 }
1099
Steven Valdeza833c352016-11-01 13:39:36 -04001100 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1101 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001102 return 0;
1103 }
1104
Steven Valdeza833c352016-11-01 13:39:36 -04001105 /* OCSP stapling is forbidden on non-certificate ciphers. */
1106 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001107 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001108 return 0;
1109 }
1110
Steven Valdeza833c352016-11-01 13:39:36 -04001111 /* Note this does not check for resumption in TLS 1.2. Sending
1112 * status_request here does not make sense, but OpenSSL does so and the
1113 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001114
David Benjamin8c880a22016-12-03 02:20:34 -05001115 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001116 return 1;
1117}
1118
David Benjamin8c880a22016-12-03 02:20:34 -05001119static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001120 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001121 if (contents == NULL) {
1122 return 1;
1123 }
1124
1125 uint8_t status_type;
1126 if (!CBS_get_u8(contents, &status_type)) {
1127 return 0;
1128 }
1129
1130 /* We cannot decide whether OCSP stapling will occur yet because the correct
1131 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001132 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001133
Adam Langleybb0bd042015-07-01 16:21:03 -07001134 return 1;
1135}
1136
David Benjamin8c880a22016-12-03 02:20:34 -05001137static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1138 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001139 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001140 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001141 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001142 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001143 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001144 return 1;
1145 }
1146
David Benjamin8c880a22016-12-03 02:20:34 -05001147 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001148
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001149 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001150 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001151}
1152
1153
Adam Langley97dfcbf2015-07-01 18:35:20 -07001154/* Next protocol negotiation.
1155 *
1156 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1157
David Benjamin8c880a22016-12-03 02:20:34 -05001158static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1159 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001160 if (ssl->s3->initial_handshake_complete ||
1161 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001162 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001163 return 1;
1164 }
1165
1166 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1167 !CBB_add_u16(out, 0 /* length */)) {
1168 return 0;
1169 }
1170
1171 return 1;
1172}
1173
David Benjamin8c880a22016-12-03 02:20:34 -05001174static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001175 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001176 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001177 if (contents == NULL) {
1178 return 1;
1179 }
1180
Steven Valdez143e8b32016-07-11 13:19:03 -04001181 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1182 return 0;
1183 }
1184
Adam Langley97dfcbf2015-07-01 18:35:20 -07001185 /* If any of these are false then we should never have sent the NPN
1186 * extension in the ClientHello and thus this function should never have been
1187 * called. */
1188 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001189 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001190 assert(ssl->ctx->next_proto_select_cb != NULL);
1191
David Benjamin76c2efc2015-08-31 14:24:29 -04001192 if (ssl->s3->alpn_selected != NULL) {
1193 /* NPN and ALPN may not be negotiated in the same connection. */
1194 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1195 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1196 return 0;
1197 }
1198
Adam Langley97dfcbf2015-07-01 18:35:20 -07001199 const uint8_t *const orig_contents = CBS_data(contents);
1200 const size_t orig_len = CBS_len(contents);
1201
1202 while (CBS_len(contents) != 0) {
1203 CBS proto;
1204 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1205 CBS_len(&proto) == 0) {
1206 return 0;
1207 }
1208 }
1209
1210 uint8_t *selected;
1211 uint8_t selected_len;
1212 if (ssl->ctx->next_proto_select_cb(
1213 ssl, &selected, &selected_len, orig_contents, orig_len,
1214 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1215 *out_alert = SSL_AD_INTERNAL_ERROR;
1216 return 0;
1217 }
1218
David Benjamin79978df2015-12-25 15:56:49 -05001219 OPENSSL_free(ssl->s3->next_proto_negotiated);
David Benjamin81678aa2017-07-12 22:43:42 -04001220 ssl->s3->next_proto_negotiated =
1221 (uint8_t *)BUF_memdup(selected, selected_len);
David Benjamin79978df2015-12-25 15:56:49 -05001222 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001223 *out_alert = SSL_AD_INTERNAL_ERROR;
1224 return 0;
1225 }
1226
David Benjamin79978df2015-12-25 15:56:49 -05001227 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001228 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001229
1230 return 1;
1231}
1232
David Benjamin8c880a22016-12-03 02:20:34 -05001233static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001234 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001235 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001236 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1237 return 1;
1238 }
1239
Adam Langley97dfcbf2015-07-01 18:35:20 -07001240 if (contents != NULL && CBS_len(contents) != 0) {
1241 return 0;
1242 }
1243
1244 if (contents == NULL ||
1245 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001246 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001247 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001248 return 1;
1249 }
1250
David Benjamin8c880a22016-12-03 02:20:34 -05001251 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001252 return 1;
1253}
1254
David Benjamin8c880a22016-12-03 02:20:34 -05001255static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1256 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001257 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1258 * parsed. */
David Benjamin8c880a22016-12-03 02:20:34 -05001259 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001260 return 1;
1261 }
1262
1263 const uint8_t *npa;
1264 unsigned npa_len;
1265
1266 if (ssl->ctx->next_protos_advertised_cb(
1267 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1268 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001269 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001270 return 1;
1271 }
1272
1273 CBB contents;
1274 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1275 !CBB_add_u16_length_prefixed(out, &contents) ||
1276 !CBB_add_bytes(&contents, npa, npa_len) ||
1277 !CBB_flush(out)) {
1278 return 0;
1279 }
1280
1281 return 1;
1282}
1283
1284
Adam Langleyab8d87d2015-07-10 12:21:39 -07001285/* Signed certificate timestamps.
1286 *
1287 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1288
David Benjamin8c880a22016-12-03 02:20:34 -05001289static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1290 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001291 if (!ssl->signed_cert_timestamps_enabled) {
1292 return 1;
1293 }
1294
1295 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1296 !CBB_add_u16(out, 0 /* length */)) {
1297 return 0;
1298 }
1299
1300 return 1;
1301}
1302
David Benjamin8c880a22016-12-03 02:20:34 -05001303static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001304 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001305 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001306 if (contents == NULL) {
1307 return 1;
1308 }
1309
Steven Valdeza833c352016-11-01 13:39:36 -04001310 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1311 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001312 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001313 return 0;
1314 }
1315
Adam Langleyab8d87d2015-07-10 12:21:39 -07001316 /* If this is false then we should never have sent the SCT extension in the
1317 * ClientHello and thus this function should never have been called. */
1318 assert(ssl->signed_cert_timestamps_enabled);
1319
Adam Langleycfa08c32016-11-17 13:21:27 -08001320 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001321 *out_alert = SSL_AD_DECODE_ERROR;
1322 return 0;
1323 }
1324
David Benjamindaa88502016-10-04 16:32:16 -04001325 /* Session resumption uses the original session information. The extension
1326 * should not be sent on resumption, but RFC 6962 did not make it a
1327 * requirement, so tolerate this.
1328 *
1329 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001330 if (!ssl->s3->session_reused &&
David Benjamin45738dd2017-02-09 20:01:26 -05001331 !CBS_stow(contents, &hs->new_session->tlsext_signed_cert_timestamp_list,
1332 &hs->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001333 *out_alert = SSL_AD_INTERNAL_ERROR;
1334 return 0;
1335 }
1336
1337 return 1;
1338}
1339
David Benjamin8c880a22016-12-03 02:20:34 -05001340static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001341 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001342 if (contents == NULL) {
1343 return 1;
1344 }
1345
1346 if (CBS_len(contents) != 0) {
1347 return 0;
1348 }
1349
David Benjamin8c880a22016-12-03 02:20:34 -05001350 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001351 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001352}
1353
David Benjamin8c880a22016-12-03 02:20:34 -05001354static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1355 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001356 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001357 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1358 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001359 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001360 return 1;
1361 }
1362
1363 CBB contents;
1364 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1365 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001366 CBB_add_bytes(
1367 &contents,
1368 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1369 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001370 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001371}
1372
1373
Adam Langleyf18e4532015-07-10 13:39:53 -07001374/* Application-level Protocol Negotiation.
1375 *
1376 * https://tools.ietf.org/html/rfc7301 */
1377
David Benjamin8c880a22016-12-03 02:20:34 -05001378static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1379 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001380 if (ssl->alpn_client_proto_list == NULL ||
1381 ssl->s3->initial_handshake_complete) {
1382 return 1;
1383 }
1384
1385 CBB contents, proto_list;
1386 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1387 !CBB_add_u16_length_prefixed(out, &contents) ||
1388 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1389 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1390 ssl->alpn_client_proto_list_len) ||
1391 !CBB_flush(out)) {
1392 return 0;
1393 }
1394
1395 return 1;
1396}
1397
David Benjamin8c880a22016-12-03 02:20:34 -05001398static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001399 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001400 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001401 if (contents == NULL) {
1402 return 1;
1403 }
1404
1405 assert(!ssl->s3->initial_handshake_complete);
1406 assert(ssl->alpn_client_proto_list != NULL);
1407
David Benjamin8c880a22016-12-03 02:20:34 -05001408 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001409 /* NPN and ALPN may not be negotiated in the same connection. */
1410 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1411 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1412 return 0;
1413 }
1414
Adam Langleyf18e4532015-07-10 13:39:53 -07001415 /* The extension data consists of a ProtocolNameList which must have
1416 * exactly one ProtocolName. Each of these is length-prefixed. */
1417 CBS protocol_name_list, protocol_name;
1418 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1419 CBS_len(contents) != 0 ||
1420 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1421 /* Empty protocol names are forbidden. */
1422 CBS_len(&protocol_name) == 0 ||
1423 CBS_len(&protocol_name_list) != 0) {
1424 return 0;
1425 }
1426
David Benjaminc8ff30c2017-04-04 13:52:36 -04001427 if (!ssl->ctx->allow_unknown_alpn_protos) {
1428 /* Check that the protocol name is one of the ones we advertised. */
1429 int protocol_ok = 0;
1430 CBS client_protocol_name_list, client_protocol_name;
1431 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1432 ssl->alpn_client_proto_list_len);
1433 while (CBS_len(&client_protocol_name_list) > 0) {
1434 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1435 &client_protocol_name)) {
1436 *out_alert = SSL_AD_INTERNAL_ERROR;
1437 return 0;
1438 }
1439
1440 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1441 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1442 CBS_data(&protocol_name),
1443 CBS_len(&protocol_name)) == 0) {
1444 protocol_ok = 1;
1445 break;
1446 }
1447 }
1448
1449 if (!protocol_ok) {
1450 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1451 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3e517572016-08-11 11:52:23 -04001452 return 0;
1453 }
David Benjamin3e517572016-08-11 11:52:23 -04001454 }
1455
Adam Langleyf18e4532015-07-10 13:39:53 -07001456 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1457 &ssl->s3->alpn_selected_len)) {
1458 *out_alert = SSL_AD_INTERNAL_ERROR;
1459 return 0;
1460 }
1461
1462 return 1;
1463}
1464
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001465int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001466 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001467 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001468 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001469 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001470 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001471 client_hello, &contents,
1472 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1473 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001474 return 1;
1475 }
1476
1477 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001478 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001479
1480 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001481 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1482 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001483 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001484 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1485 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001486 return 0;
1487 }
1488
1489 /* Validate the protocol list. */
1490 CBS protocol_name_list_copy = protocol_name_list;
1491 while (CBS_len(&protocol_name_list_copy) > 0) {
1492 CBS protocol_name;
1493
1494 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1495 /* Empty protocol names are forbidden. */
1496 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001497 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1498 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001499 return 0;
1500 }
1501 }
1502
1503 const uint8_t *selected;
1504 uint8_t selected_len;
1505 if (ssl->ctx->alpn_select_cb(
1506 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1507 CBS_len(&protocol_name_list),
1508 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1509 OPENSSL_free(ssl->s3->alpn_selected);
David Benjamin81678aa2017-07-12 22:43:42 -04001510 ssl->s3->alpn_selected = (uint8_t *)BUF_memdup(selected, selected_len);
Adam Langleyf18e4532015-07-10 13:39:53 -07001511 if (ssl->s3->alpn_selected == NULL) {
1512 *out_alert = SSL_AD_INTERNAL_ERROR;
1513 return 0;
1514 }
1515 ssl->s3->alpn_selected_len = selected_len;
1516 }
1517
1518 return 1;
1519}
1520
David Benjamin8c880a22016-12-03 02:20:34 -05001521static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1522 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001523 if (ssl->s3->alpn_selected == NULL) {
1524 return 1;
1525 }
1526
1527 CBB contents, proto_list, proto;
1528 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1529 !CBB_add_u16_length_prefixed(out, &contents) ||
1530 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1531 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001532 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1533 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001534 !CBB_flush(out)) {
1535 return 0;
1536 }
1537
1538 return 1;
1539}
1540
1541
Adam Langley49c7af12015-07-10 14:33:46 -07001542/* Channel ID.
1543 *
1544 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1545
David Benjamin8c880a22016-12-03 02:20:34 -05001546static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1547 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001548}
1549
David Benjamin8c880a22016-12-03 02:20:34 -05001550static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1551 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001552 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001553 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001554 return 1;
1555 }
1556
1557 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1558 !CBB_add_u16(out, 0 /* length */)) {
1559 return 0;
1560 }
1561
1562 return 1;
1563}
1564
David Benjamin8c880a22016-12-03 02:20:34 -05001565static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1566 uint8_t *out_alert, CBS *contents) {
1567 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001568 if (contents == NULL) {
1569 return 1;
1570 }
1571
David Benjamince079fd2016-08-02 16:22:34 -04001572 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001573 assert(ssl->tlsext_channel_id_enabled);
1574
1575 if (CBS_len(contents) != 0) {
1576 return 0;
1577 }
1578
1579 ssl->s3->tlsext_channel_id_valid = 1;
1580 return 1;
1581}
1582
David Benjamin8c880a22016-12-03 02:20:34 -05001583static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1584 uint8_t *out_alert, CBS *contents) {
1585 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001586 if (contents == NULL ||
1587 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001588 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001589 return 1;
1590 }
1591
1592 if (CBS_len(contents) != 0) {
1593 return 0;
1594 }
1595
1596 ssl->s3->tlsext_channel_id_valid = 1;
1597 return 1;
1598}
1599
David Benjamin8c880a22016-12-03 02:20:34 -05001600static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1601 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001602 if (!ssl->s3->tlsext_channel_id_valid) {
1603 return 1;
1604 }
1605
1606 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1607 !CBB_add_u16(out, 0 /* length */)) {
1608 return 0;
1609 }
1610
1611 return 1;
1612}
1613
Adam Langley391250d2015-07-15 19:06:07 -07001614
1615/* Secure Real-time Transport Protocol (SRTP) extension.
1616 *
1617 * https://tools.ietf.org/html/rfc5764 */
1618
Adam Langley391250d2015-07-15 19:06:07 -07001619
David Benjamin8c880a22016-12-03 02:20:34 -05001620static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1621 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001622}
1623
David Benjamin8c880a22016-12-03 02:20:34 -05001624static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1625 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001626 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1627 if (profiles == NULL) {
1628 return 1;
1629 }
1630 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1631 if (num_profiles == 0) {
1632 return 1;
1633 }
1634
1635 CBB contents, profile_ids;
1636 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1637 !CBB_add_u16_length_prefixed(out, &contents) ||
1638 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1639 return 0;
1640 }
1641
David Benjamin54091232016-09-05 12:47:25 -04001642 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001643 if (!CBB_add_u16(&profile_ids,
1644 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1645 return 0;
1646 }
1647 }
1648
1649 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1650 !CBB_flush(out)) {
1651 return 0;
1652 }
1653
1654 return 1;
1655}
1656
David Benjamin8c880a22016-12-03 02:20:34 -05001657static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001658 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001659 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001660 if (contents == NULL) {
1661 return 1;
1662 }
1663
1664 /* The extension consists of a u16-prefixed profile ID list containing a
1665 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1666 *
1667 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1668 CBS profile_ids, srtp_mki;
1669 uint16_t profile_id;
1670 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1671 !CBS_get_u16(&profile_ids, &profile_id) ||
1672 CBS_len(&profile_ids) != 0 ||
1673 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1674 CBS_len(contents) != 0) {
1675 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1676 return 0;
1677 }
1678
1679 if (CBS_len(&srtp_mki) != 0) {
1680 /* Must be no MKI, since we never offer one. */
1681 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1682 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1683 return 0;
1684 }
1685
1686 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1687
1688 /* Check to see if the server gave us something we support (and presumably
1689 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001690 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001691 const SRTP_PROTECTION_PROFILE *profile =
1692 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1693
1694 if (profile->id == profile_id) {
1695 ssl->srtp_profile = profile;
1696 return 1;
1697 }
1698 }
1699
1700 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1701 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1702 return 0;
1703}
1704
David Benjamin8c880a22016-12-03 02:20:34 -05001705static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001706 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001707 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001708 if (contents == NULL) {
1709 return 1;
1710 }
1711
1712 CBS profile_ids, srtp_mki;
1713 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1714 CBS_len(&profile_ids) < 2 ||
1715 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1716 CBS_len(contents) != 0) {
1717 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1718 return 0;
1719 }
1720 /* Discard the MKI value for now. */
1721
1722 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1723 SSL_get_srtp_profiles(ssl);
1724
1725 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001726 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001727 const SRTP_PROTECTION_PROFILE *server_profile =
1728 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1729
1730 CBS profile_ids_tmp;
1731 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1732
1733 while (CBS_len(&profile_ids_tmp) > 0) {
1734 uint16_t profile_id;
1735 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1736 return 0;
1737 }
1738
1739 if (server_profile->id == profile_id) {
1740 ssl->srtp_profile = server_profile;
1741 return 1;
1742 }
1743 }
1744 }
1745
1746 return 1;
1747}
1748
David Benjamin8c880a22016-12-03 02:20:34 -05001749static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1750 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001751 if (ssl->srtp_profile == NULL) {
1752 return 1;
1753 }
1754
1755 CBB contents, profile_ids;
1756 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1757 !CBB_add_u16_length_prefixed(out, &contents) ||
1758 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1759 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1760 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1761 !CBB_flush(out)) {
1762 return 0;
1763 }
1764
1765 return 1;
1766}
1767
Adam Langleybdd5d662015-07-20 16:19:08 -07001768
1769/* EC point formats.
1770 *
1771 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1772
David Benjamin8c880a22016-12-03 02:20:34 -05001773static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001774 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001775 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1776 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001777 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1778 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001779 !CBB_flush(out)) {
1780 return 0;
1781 }
1782
1783 return 1;
1784}
1785
David Benjamin8c880a22016-12-03 02:20:34 -05001786static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001787 /* The point format extension is unneccessary in TLS 1.3. */
David Benjamin68161cb2017-06-20 14:49:43 -04001788 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin70aba262016-11-01 12:08:15 -04001789 return 1;
1790 }
1791
David Benjamin8c880a22016-12-03 02:20:34 -05001792 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001793}
1794
David Benjamin8c880a22016-12-03 02:20:34 -05001795static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001796 CBS *contents) {
1797 if (contents == NULL) {
1798 return 1;
1799 }
1800
David Benjamin8c880a22016-12-03 02:20:34 -05001801 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001802 return 0;
1803 }
1804
Adam Langleybdd5d662015-07-20 16:19:08 -07001805 CBS ec_point_format_list;
1806 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1807 CBS_len(contents) != 0) {
1808 return 0;
1809 }
1810
David Benjaminfc059942015-07-30 23:01:59 -04001811 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1812 * point format. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05001813 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1814 TLSEXT_ECPOINTFORMAT_uncompressed,
1815 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001816 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001817 return 0;
1818 }
1819
1820 return 1;
1821}
1822
David Benjamin8c880a22016-12-03 02:20:34 -05001823static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001824 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001825 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001826 return 1;
1827 }
1828
David Benjamin8c880a22016-12-03 02:20:34 -05001829 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001830}
1831
David Benjamin8c880a22016-12-03 02:20:34 -05001832static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1833 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001834 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1835 return 1;
1836 }
1837
David Benjamin45738dd2017-02-09 20:01:26 -05001838 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1839 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001840 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001841
1842 if (!using_ecc) {
1843 return 1;
1844 }
1845
David Benjamin8c880a22016-12-03 02:20:34 -05001846 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001847}
1848
Steven Valdeza833c352016-11-01 13:39:36 -04001849
Steven Valdez4aa154e2016-07-29 14:32:55 -04001850/* Pre Shared Key
1851 *
Steven Valdeza833c352016-11-01 13:39:36 -04001852 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001853
David Benjamin8c880a22016-12-03 02:20:34 -05001854static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1855 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001856 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001857 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001858 return 0;
1859 }
1860
Steven Valdez8f36c512017-06-20 10:55:02 -04001861 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001862 return 15 + ssl->session->tlsext_ticklen + binder_len;
1863}
1864
David Benjamin8c880a22016-12-03 02:20:34 -05001865static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1866 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001867 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001868 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001869 return 1;
1870 }
1871
David Benjaminad8f5e12017-02-20 17:00:20 -05001872 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001873 ssl_get_current_time(ssl, &now);
1874 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1875 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1876
1877 /* Fill in a placeholder zero binder of the appropriate length. It will be
1878 * computed and filled in later after length prefixes are computed. */
1879 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez8f36c512017-06-20 10:55:02 -04001880 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001881
1882 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001883 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1884 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001885 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001886 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1887 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001888 ssl->session->tlsext_ticklen) ||
1889 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1890 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1891 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1892 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001893 return 0;
1894 }
1895
David Benjamin8c880a22016-12-03 02:20:34 -05001896 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001897 return CBB_flush(out);
1898}
1899
David Benjamin8baf9632016-11-17 17:11:16 +09001900int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1901 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001902 CBS *contents) {
1903 uint16_t psk_id;
1904 if (!CBS_get_u16(contents, &psk_id) ||
1905 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001906 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001907 *out_alert = SSL_AD_DECODE_ERROR;
1908 return 0;
1909 }
1910
David Benjamineab773a2016-11-09 18:32:35 -05001911 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001912 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001913 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001914 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1915 return 0;
1916 }
1917
1918 return 1;
1919}
1920
David Benjamin35ac5b72017-03-03 15:05:56 -05001921int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001922 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001923 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04001924 /* We only process the first PSK identity since we don't support pure PSK. */
David Benjamin707af292017-03-10 17:47:18 -05001925 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001926 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001927 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001928 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001929 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001930 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001931 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001932 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001933 *out_alert = SSL_AD_DECODE_ERROR;
1934 return 0;
1935 }
1936
Steven Valdeza833c352016-11-01 13:39:36 -04001937 *out_binders = binders;
1938
David Benjaminaedf3032016-12-01 16:47:56 -05001939 /* Check the syntax of the remaining identities, but do not process them. */
1940 size_t num_identities = 1;
1941 while (CBS_len(&identities) != 0) {
1942 CBS unused_ticket;
1943 uint32_t unused_obfuscated_ticket_age;
1944 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1945 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1946 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1947 *out_alert = SSL_AD_DECODE_ERROR;
1948 return 0;
1949 }
1950
1951 num_identities++;
1952 }
1953
1954 /* Check the syntax of the binders. The value will be checked later if
1955 * resuming. */
1956 size_t num_binders = 0;
1957 while (CBS_len(&binders) != 0) {
1958 CBS binder;
1959 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1960 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1961 *out_alert = SSL_AD_DECODE_ERROR;
1962 return 0;
1963 }
1964
1965 num_binders++;
1966 }
1967
1968 if (num_identities != num_binders) {
1969 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1970 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04001971 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04001972 }
1973
David Benjamine7f60a22016-11-16 18:54:25 +09001974 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001975}
1976
David Benjamin8baf9632016-11-17 17:11:16 +09001977int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1978 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001979 return 1;
1980 }
1981
1982 CBB contents;
1983 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1984 !CBB_add_u16_length_prefixed(out, &contents) ||
1985 /* We only consider the first identity for resumption */
1986 !CBB_add_u16(&contents, 0) ||
1987 !CBB_flush(out)) {
1988 return 0;
1989 }
1990
1991 return 1;
1992}
1993
1994
Steven Valdeza833c352016-11-01 13:39:36 -04001995/* Pre-Shared Key Exchange Modes
1996 *
1997 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05001998
David Benjamin8c880a22016-12-03 02:20:34 -05001999static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2000 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04002001 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04002002 return 1;
2003 }
2004
2005 CBB contents, ke_modes;
2006 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2007 !CBB_add_u16_length_prefixed(out, &contents) ||
2008 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2009 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2010 return 0;
2011 }
2012
2013 return CBB_flush(out);
2014}
2015
David Benjamin8c880a22016-12-03 02:20:34 -05002016static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002017 uint8_t *out_alert,
2018 CBS *contents) {
2019 if (contents == NULL) {
2020 return 1;
2021 }
2022
Steven Valdeza833c352016-11-01 13:39:36 -04002023 CBS ke_modes;
2024 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2025 CBS_len(&ke_modes) == 0 ||
2026 CBS_len(contents) != 0) {
2027 *out_alert = SSL_AD_DECODE_ERROR;
2028 return 0;
2029 }
2030
2031 /* We only support tickets with PSK_DHE_KE. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05002032 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2033 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002034
2035 return 1;
2036}
2037
2038
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002039/* Early Data Indication
2040 *
2041 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2042
David Benjamin8c880a22016-12-03 02:20:34 -05002043static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002044 SSL *const ssl = hs->ssl;
Steven Valdez2d850622017-01-11 11:34:52 -05002045 if (ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04002046 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION ||
Steven Valdez2d850622017-01-11 11:34:52 -05002047 ssl->session->ticket_max_early_data == 0 ||
2048 hs->received_hello_retry_request ||
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05002049 !ssl->cert->enable_early_data) {
Steven Valdez2d850622017-01-11 11:34:52 -05002050 return 1;
2051 }
2052
2053 hs->early_data_offered = 1;
2054
2055 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2056 !CBB_add_u16(out, 0) ||
2057 !CBB_flush(out)) {
2058 return 0;
2059 }
2060
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002061 return 1;
2062}
2063
Steven Valdez2d850622017-01-11 11:34:52 -05002064static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002065 uint8_t *out_alert, CBS *contents) {
2066 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002067 if (contents == NULL) {
2068 return 1;
2069 }
2070
2071 if (CBS_len(contents) != 0) {
2072 *out_alert = SSL_AD_DECODE_ERROR;
2073 return 0;
2074 }
2075
Steven Valdez2d850622017-01-11 11:34:52 -05002076 if (!ssl->s3->session_reused) {
2077 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2078 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2079 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002080 }
Steven Valdez2d850622017-01-11 11:34:52 -05002081
2082 ssl->early_data_accepted = 1;
2083 return 1;
2084}
2085
2086static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2087 uint8_t *out_alert, CBS *contents) {
2088 SSL *const ssl = hs->ssl;
2089 if (contents == NULL ||
2090 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2091 return 1;
2092 }
2093
2094 if (CBS_len(contents) != 0) {
2095 *out_alert = SSL_AD_DECODE_ERROR;
2096 return 0;
2097 }
2098
2099 hs->early_data_offered = 1;
2100 return 1;
2101}
2102
2103static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2104 if (!hs->ssl->early_data_accepted) {
2105 return 1;
2106 }
2107
2108 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2109 !CBB_add_u16(out, 0) ||
2110 !CBB_flush(out)) {
2111 return 0;
2112 }
2113
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002114 return 1;
2115}
2116
2117
Steven Valdez143e8b32016-07-11 13:19:03 -04002118/* Key Share
2119 *
David Benjamina128a552016-10-13 14:26:33 -04002120 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002121
David Benjamin8c880a22016-12-03 02:20:34 -05002122static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2123 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002124 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002125 return 1;
2126 }
2127
2128 CBB contents, kse_bytes;
2129 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2130 !CBB_add_u16_length_prefixed(out, &contents) ||
2131 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2132 return 0;
2133 }
2134
David Benjamin8c880a22016-12-03 02:20:34 -05002135 uint16_t group_id = hs->retry_group;
2136 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002137 /* We received a HelloRetryRequest without a new curve, so there is no new
2138 * share to append. Leave |ecdh_ctx| as-is. */
2139 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002140 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2141 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002142 return 0;
2143 }
David Benjamin8c880a22016-12-03 02:20:34 -05002144 OPENSSL_free(hs->key_share_bytes);
2145 hs->key_share_bytes = NULL;
2146 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002147 if (group_id == 0) {
2148 return CBB_flush(out);
2149 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002150 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002151 /* Add a fake group. See draft-davidben-tls-grease-01. */
2152 if (ssl->ctx->grease_enabled &&
2153 (!CBB_add_u16(&kse_bytes,
2154 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2155 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2156 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2157 return 0;
2158 }
2159
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002160 /* Predict the most preferred group. */
2161 const uint16_t *groups;
2162 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002163 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002164 if (groups_len == 0) {
2165 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2166 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002167 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002168
2169 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002170 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002171
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002172 CBB key_exchange;
2173 if (!CBB_add_u16(&kse_bytes, group_id) ||
2174 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002175 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2176 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002177 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002178 return 0;
2179 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002180
David Benjamin8c880a22016-12-03 02:20:34 -05002181 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002182 /* Save the contents of the extension to repeat it in the second
2183 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002184 hs->key_share_bytes_len = CBB_len(&kse_bytes);
David Benjamin81678aa2017-07-12 22:43:42 -04002185 hs->key_share_bytes =
2186 (uint8_t *)BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
David Benjamin8c880a22016-12-03 02:20:34 -05002187 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002188 return 0;
2189 }
2190 }
2191
Steven Valdez143e8b32016-07-11 13:19:03 -04002192 return CBB_flush(out);
2193}
2194
David Benjamin8baf9632016-11-17 17:11:16 +09002195int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002196 size_t *out_secret_len,
2197 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002198 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002199 uint16_t group_id;
2200 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002201 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2202 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002203 *out_alert = SSL_AD_DECODE_ERROR;
2204 return 0;
2205 }
2206
David Benjamin8baf9632016-11-17 17:11:16 +09002207 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002208 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2209 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2210 return 0;
2211 }
2212
David Benjamin8baf9632016-11-17 17:11:16 +09002213 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2214 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002215 *out_alert = SSL_AD_INTERNAL_ERROR;
2216 return 0;
2217 }
2218
David Benjamin45738dd2017-02-09 20:01:26 -05002219 hs->new_session->group_id = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002220 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002221 return 1;
2222}
2223
David Benjamin8baf9632016-11-17 17:11:16 +09002224int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002225 uint8_t **out_secret,
2226 size_t *out_secret_len,
2227 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002228 uint16_t group_id;
2229 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002230 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002231 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2232 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2233 return 0;
2234 }
2235
2236 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002237 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002238 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002239 return 0;
2240 }
2241
David Benjamin7e1f9842016-09-20 19:24:40 -04002242 /* Find the corresponding key share. */
2243 int found = 0;
2244 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002245 while (CBS_len(&key_shares) > 0) {
2246 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002247 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002248 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002249 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2250 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002251 return 0;
2252 }
2253
David Benjamin7e1f9842016-09-20 19:24:40 -04002254 if (id == group_id) {
2255 if (found) {
2256 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2257 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2258 return 0;
2259 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002260
David Benjamin7e1f9842016-09-20 19:24:40 -04002261 found = 1;
2262 peer_key = peer_key_tmp;
2263 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002264 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002265 }
2266
David Benjamin7e1f9842016-09-20 19:24:40 -04002267 if (!found) {
2268 *out_found = 0;
2269 *out_secret = NULL;
2270 *out_secret_len = 0;
2271 return 1;
2272 }
2273
2274 /* Compute the DH secret. */
2275 uint8_t *secret = NULL;
2276 size_t secret_len;
2277 SSL_ECDH_CTX group;
David Benjamin17cf2cb2016-12-13 01:07:13 -05002278 OPENSSL_memset(&group, 0, sizeof(SSL_ECDH_CTX));
David Benjamin1386aad2017-07-19 23:57:40 -04002279 ScopedCBB public_key;
2280 if (!CBB_init(public_key.get(), 32) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002281 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin1386aad2017-07-19 23:57:40 -04002282 !SSL_ECDH_CTX_accept(&group, public_key.get(), &secret, &secret_len,
2283 out_alert, CBS_data(&peer_key),
2284 CBS_len(&peer_key)) ||
2285 !CBB_finish(public_key.get(), &hs->ecdh_public_key,
David Benjaminbf833c32017-03-30 15:45:21 -05002286 &hs->ecdh_public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002287 OPENSSL_free(secret);
2288 SSL_ECDH_CTX_cleanup(&group);
Steven Valdez803c77a2016-09-06 14:13:43 -04002289 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002290 return 0;
2291 }
2292
2293 SSL_ECDH_CTX_cleanup(&group);
2294
2295 *out_secret = secret;
2296 *out_secret_len = secret_len;
2297 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002298 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002299}
2300
David Benjamin8baf9632016-11-17 17:11:16 +09002301int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002302 uint16_t group_id;
2303 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002304 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002305 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2306 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2307 !CBB_add_u16(&kse_bytes, group_id) ||
2308 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002309 !CBB_add_bytes(&public_key, hs->ecdh_public_key,
2310 hs->ecdh_public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002311 !CBB_flush(out)) {
2312 return 0;
2313 }
2314
David Benjaminbf833c32017-03-30 15:45:21 -05002315 OPENSSL_free(hs->ecdh_public_key);
2316 hs->ecdh_public_key = NULL;
2317 hs->ecdh_public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002318
David Benjamin45738dd2017-02-09 20:01:26 -05002319 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002320 return 1;
2321}
2322
2323
Steven Valdezfdd10992016-09-15 16:27:05 -04002324/* Supported Versions
2325 *
2326 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2327
David Benjamin8c880a22016-12-03 02:20:34 -05002328static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2329 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002330 if (hs->max_version <= TLS1_2_VERSION) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002331 return 1;
2332 }
2333
2334 CBB contents, versions;
2335 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2336 !CBB_add_u16_length_prefixed(out, &contents) ||
2337 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2338 return 0;
2339 }
2340
David Benjamind9791bf2016-09-27 16:39:52 -04002341 /* Add a fake version. See draft-davidben-tls-grease-01. */
2342 if (ssl->ctx->grease_enabled &&
2343 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2344 return 0;
2345 }
2346
Steven Valdez8f36c512017-06-20 10:55:02 -04002347 if (!ssl_add_supported_versions(hs, &versions) ||
2348 !CBB_flush(out)) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002349 return 0;
2350 }
2351
2352 return 1;
2353}
2354
2355
David Benjamin3baa6e12016-10-07 21:10:38 -04002356/* Cookie
2357 *
2358 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2359
David Benjamin8c880a22016-12-03 02:20:34 -05002360static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2361 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002362 return 1;
2363 }
2364
2365 CBB contents, cookie;
2366 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2367 !CBB_add_u16_length_prefixed(out, &contents) ||
2368 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002369 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002370 !CBB_flush(out)) {
2371 return 0;
2372 }
2373
2374 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002375 OPENSSL_free(hs->cookie);
2376 hs->cookie = NULL;
2377 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002378 return 1;
2379}
2380
2381
Steven Valdezce902a92016-05-17 11:47:53 -04002382/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002383 *
Steven Valdezce902a92016-05-17 11:47:53 -04002384 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002385 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002386
David Benjamin8c880a22016-12-03 02:20:34 -05002387static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2388 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002389 CBB contents, groups_bytes;
2390 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002391 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002392 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002393 return 0;
2394 }
2395
David Benjamin65ac9972016-09-02 21:35:25 -04002396 /* Add a fake group. See draft-davidben-tls-grease-01. */
2397 if (ssl->ctx->grease_enabled &&
2398 !CBB_add_u16(&groups_bytes,
2399 ssl_get_grease_value(ssl, ssl_grease_group))) {
2400 return 0;
2401 }
2402
Steven Valdezce902a92016-05-17 11:47:53 -04002403 const uint16_t *groups;
2404 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002405 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002406
David Benjamin54091232016-09-05 12:47:25 -04002407 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002408 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002409 return 0;
2410 }
2411 }
2412
2413 return CBB_flush(out);
2414}
2415
David Benjamin8c880a22016-12-03 02:20:34 -05002416static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2417 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002418 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002419 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2420 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002421 return 1;
2422}
2423
David Benjamin8c880a22016-12-03 02:20:34 -05002424static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2425 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002426 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002427 if (contents == NULL) {
2428 return 1;
2429 }
2430
Steven Valdezce902a92016-05-17 11:47:53 -04002431 CBS supported_group_list;
2432 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2433 CBS_len(&supported_group_list) == 0 ||
2434 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002435 CBS_len(contents) != 0) {
2436 return 0;
2437 }
2438
David Benjamin8c880a22016-12-03 02:20:34 -05002439 hs->peer_supported_group_list =
David Benjamin81678aa2017-07-12 22:43:42 -04002440 (uint16_t *)OPENSSL_malloc(CBS_len(&supported_group_list));
David Benjamin8c880a22016-12-03 02:20:34 -05002441 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002442 *out_alert = SSL_AD_INTERNAL_ERROR;
2443 return 0;
2444 }
2445
Steven Valdezce902a92016-05-17 11:47:53 -04002446 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002447 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002448 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002449 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002450 goto err;
2451 }
2452 }
2453
Steven Valdezce902a92016-05-17 11:47:53 -04002454 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002455 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002456
2457 return 1;
2458
2459err:
David Benjamin8c880a22016-12-03 02:20:34 -05002460 OPENSSL_free(hs->peer_supported_group_list);
2461 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002462 *out_alert = SSL_AD_INTERNAL_ERROR;
2463 return 0;
2464}
2465
David Benjamin8c880a22016-12-03 02:20:34 -05002466static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002467 /* Servers don't echo this extension. */
2468 return 1;
2469}
2470
2471
Adam Langley614c66a2015-06-12 15:26:58 -07002472/* kExtensions contains all the supported extensions. */
2473static const struct tls_extension kExtensions[] = {
2474 {
Adam Langley5021b222015-06-12 18:27:58 -07002475 TLSEXT_TYPE_renegotiate,
2476 NULL,
2477 ext_ri_add_clienthello,
2478 ext_ri_parse_serverhello,
2479 ext_ri_parse_clienthello,
2480 ext_ri_add_serverhello,
2481 },
2482 {
Adam Langley614c66a2015-06-12 15:26:58 -07002483 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002484 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002485 ext_sni_add_clienthello,
2486 ext_sni_parse_serverhello,
2487 ext_sni_parse_clienthello,
2488 ext_sni_add_serverhello,
2489 },
Adam Langley0a056712015-07-01 15:03:33 -07002490 {
2491 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002492 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002493 ext_ems_add_clienthello,
2494 ext_ems_parse_serverhello,
2495 ext_ems_parse_clienthello,
2496 ext_ems_add_serverhello,
2497 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002498 {
2499 TLSEXT_TYPE_session_ticket,
2500 NULL,
2501 ext_ticket_add_clienthello,
2502 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002503 /* Ticket extension client parsing is handled in ssl_session.c */
2504 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002505 ext_ticket_add_serverhello,
2506 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002507 {
2508 TLSEXT_TYPE_signature_algorithms,
2509 NULL,
2510 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002511 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002512 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002513 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002514 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002515 {
2516 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002517 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002518 ext_ocsp_add_clienthello,
2519 ext_ocsp_parse_serverhello,
2520 ext_ocsp_parse_clienthello,
2521 ext_ocsp_add_serverhello,
2522 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002523 {
2524 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002525 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002526 ext_npn_add_clienthello,
2527 ext_npn_parse_serverhello,
2528 ext_npn_parse_clienthello,
2529 ext_npn_add_serverhello,
2530 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002531 {
2532 TLSEXT_TYPE_certificate_timestamp,
2533 NULL,
2534 ext_sct_add_clienthello,
2535 ext_sct_parse_serverhello,
2536 ext_sct_parse_clienthello,
2537 ext_sct_add_serverhello,
2538 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002539 {
2540 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002541 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002542 ext_alpn_add_clienthello,
2543 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002544 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2545 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002546 ext_alpn_add_serverhello,
2547 },
Adam Langley49c7af12015-07-10 14:33:46 -07002548 {
2549 TLSEXT_TYPE_channel_id,
2550 ext_channel_id_init,
2551 ext_channel_id_add_clienthello,
2552 ext_channel_id_parse_serverhello,
2553 ext_channel_id_parse_clienthello,
2554 ext_channel_id_add_serverhello,
2555 },
Adam Langley391250d2015-07-15 19:06:07 -07002556 {
2557 TLSEXT_TYPE_srtp,
2558 ext_srtp_init,
2559 ext_srtp_add_clienthello,
2560 ext_srtp_parse_serverhello,
2561 ext_srtp_parse_clienthello,
2562 ext_srtp_add_serverhello,
2563 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002564 {
2565 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002566 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002567 ext_ec_point_add_clienthello,
2568 ext_ec_point_parse_serverhello,
2569 ext_ec_point_parse_clienthello,
2570 ext_ec_point_add_serverhello,
2571 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002572 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002573 TLSEXT_TYPE_key_share,
2574 NULL,
2575 ext_key_share_add_clienthello,
2576 forbid_parse_serverhello,
2577 ignore_parse_clienthello,
2578 dont_add_serverhello,
2579 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002580 {
Steven Valdeza833c352016-11-01 13:39:36 -04002581 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002582 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002583 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002584 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002585 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002586 dont_add_serverhello,
2587 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002588 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002589 TLSEXT_TYPE_early_data,
2590 NULL,
2591 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002592 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002593 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002594 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002595 },
2596 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002597 TLSEXT_TYPE_supported_versions,
2598 NULL,
2599 ext_supported_versions_add_clienthello,
2600 forbid_parse_serverhello,
2601 ignore_parse_clienthello,
2602 dont_add_serverhello,
2603 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002604 {
2605 TLSEXT_TYPE_cookie,
2606 NULL,
2607 ext_cookie_add_clienthello,
2608 forbid_parse_serverhello,
2609 ignore_parse_clienthello,
2610 dont_add_serverhello,
2611 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002612 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2613 * intolerant to the last extension being zero-length. See
2614 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002615 {
Steven Valdezce902a92016-05-17 11:47:53 -04002616 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002617 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002618 ext_supported_groups_add_clienthello,
2619 ext_supported_groups_parse_serverhello,
2620 ext_supported_groups_parse_clienthello,
2621 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002622 },
Adam Langley614c66a2015-06-12 15:26:58 -07002623};
2624
2625#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2626
David Benjamina3d76d02017-07-14 19:36:07 -04002627static_assert(kNumExtensions <=
2628 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
2629 "too many extensions for sent bitset");
2630static_assert(kNumExtensions <=
2631 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2632 "too many extensions for received bitset");
Adam Langley4cfa96b2015-07-01 11:56:55 -07002633
Adam Langley614c66a2015-06-12 15:26:58 -07002634static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2635 uint16_t value) {
2636 unsigned i;
2637 for (i = 0; i < kNumExtensions; i++) {
2638 if (kExtensions[i].value == value) {
2639 *out_index = i;
2640 return &kExtensions[i];
2641 }
2642 }
2643
2644 return NULL;
2645}
2646
David Benjamin8c880a22016-12-03 02:20:34 -05002647int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2648 SSL *const ssl = hs->ssl;
David Benjaminf04c2e92016-12-06 13:35:25 -05002649 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2650 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002651 !ssl->s3->send_connection_binding) {
2652 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002653 }
Adam Langley95c29f32014-06-20 12:00:00 -07002654
David Benjamine8d53502015-10-10 14:13:23 -04002655 CBB extensions;
2656 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002657 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2658 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002659 }
Adam Langley95c29f32014-06-20 12:00:00 -07002660
David Benjamin8c880a22016-12-03 02:20:34 -05002661 hs->extensions.sent = 0;
2662 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002663
David Benjamin54091232016-09-05 12:47:25 -04002664 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002665 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002666 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002667 }
2668 }
Adam Langley95c29f32014-06-20 12:00:00 -07002669
David Benjamin65ac9972016-09-02 21:35:25 -04002670 uint16_t grease_ext1 = 0;
2671 if (ssl->ctx->grease_enabled) {
2672 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2673 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2674 if (!CBB_add_u16(&extensions, grease_ext1) ||
2675 !CBB_add_u16(&extensions, 0 /* zero length */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002676 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2677 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002678 }
2679 }
2680
David Benjamin54091232016-09-05 12:47:25 -04002681 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002682 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002683 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002684 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002685 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin81678aa2017-07-12 22:43:42 -04002686 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002687 }
Adam Langley95c29f32014-06-20 12:00:00 -07002688
Adam Langley33ad2b52015-07-20 17:43:53 -07002689 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002690 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002691 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002692 }
Adam Langley75712922014-10-10 16:23:43 -07002693
David Benjamin2bd19172016-11-17 16:47:15 +09002694 if (!custom_ext_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002695 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2696 return 0;
Adam Langley09505632015-07-30 18:10:13 -07002697 }
2698
David Benjamin65ac9972016-09-02 21:35:25 -04002699 if (ssl->ctx->grease_enabled) {
2700 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2701 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2702
2703 /* The two fake extensions must not have the same value. GREASE values are
2704 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2705 * one. */
2706 if (grease_ext1 == grease_ext2) {
2707 grease_ext2 ^= 0x1010;
2708 }
2709
2710 if (!CBB_add_u16(&extensions, grease_ext2) ||
2711 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2712 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002713 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2714 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002715 }
2716 }
2717
David Benjamince079fd2016-08-02 16:22:34 -04002718 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002719 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002720 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002721 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002722 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002723 *
2724 * NB: because this code works out the length of all existing extensions
2725 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002726 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002727 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002728 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002729 * Server 7.0 is intolerant to the last extension being zero-length. See
2730 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002731 if (padding_len >= 4 + 1) {
2732 padding_len -= 4;
2733 } else {
2734 padding_len = 1;
2735 }
Adam Langley95c29f32014-06-20 12:00:00 -07002736
Adam Langley33ad2b52015-07-20 17:43:53 -07002737 uint8_t *padding_bytes;
2738 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2739 !CBB_add_u16(&extensions, padding_len) ||
2740 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002741 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2742 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002743 }
Adam Langley75712922014-10-10 16:23:43 -07002744
David Benjamin17cf2cb2016-12-13 01:07:13 -05002745 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002746 }
2747 }
Adam Langley75712922014-10-10 16:23:43 -07002748
Steven Valdeza833c352016-11-01 13:39:36 -04002749 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002750 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002751 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2752 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -04002753 }
2754
David Benjamina01deee2015-12-08 18:56:31 -05002755 /* Discard empty extensions blocks. */
2756 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002757 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002758 }
2759
David Benjamine8d53502015-10-10 14:13:23 -04002760 return CBB_flush(out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002761}
Adam Langley95c29f32014-06-20 12:00:00 -07002762
David Benjamin8c880a22016-12-03 02:20:34 -05002763int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2764 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002765 CBB extensions;
2766 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002767 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002768 }
2769
David Benjamin8c880a22016-12-03 02:20:34 -05002770 for (unsigned i = 0; i < kNumExtensions; i++) {
2771 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002772 /* Don't send extensions that were not received. */
2773 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002774 }
Adam Langley95c29f32014-06-20 12:00:00 -07002775
David Benjamin8c880a22016-12-03 02:20:34 -05002776 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002777 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002778 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002779 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002780 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002781 }
Adam Langley95c29f32014-06-20 12:00:00 -07002782
David Benjamin2bd19172016-11-17 16:47:15 +09002783 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002784 goto err;
2785 }
2786
Steven Valdez143e8b32016-07-11 13:19:03 -04002787 /* Discard empty extensions blocks before TLS 1.3. */
2788 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2789 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002790 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002791 }
2792
David Benjamin56380462015-10-10 14:59:09 -04002793 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002794
2795err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002796 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002797 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002798}
Adam Langley95c29f32014-06-20 12:00:00 -07002799
David Benjamin731058e2016-12-03 23:15:13 -05002800static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2801 const SSL_CLIENT_HELLO *client_hello,
2802 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002803 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002804 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002805 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002806 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002807 }
2808 }
2809
David Benjamin8c880a22016-12-03 02:20:34 -05002810 hs->extensions.received = 0;
2811 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002812
David Benjamine14ff062016-08-09 16:21:24 -04002813 CBS extensions;
2814 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2815 while (CBS_len(&extensions) != 0) {
2816 uint16_t type;
2817 CBS extension;
2818
2819 /* Decode the next extension. */
2820 if (!CBS_get_u16(&extensions, &type) ||
2821 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002822 *out_alert = SSL_AD_DECODE_ERROR;
2823 return 0;
2824 }
Adam Langley95c29f32014-06-20 12:00:00 -07002825
David Benjamine14ff062016-08-09 16:21:24 -04002826 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2827 * ambiguous. Ignore all but the renegotiation_info extension. */
2828 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2829 continue;
2830 }
Adam Langley95c29f32014-06-20 12:00:00 -07002831
David Benjamine14ff062016-08-09 16:21:24 -04002832 unsigned ext_index;
2833 const struct tls_extension *const ext =
2834 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002835
David Benjamine14ff062016-08-09 16:21:24 -04002836 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002837 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002838 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002839 return 0;
2840 }
David Benjamine14ff062016-08-09 16:21:24 -04002841 continue;
2842 }
2843
David Benjamin8c880a22016-12-03 02:20:34 -05002844 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002845 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002846 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002847 *out_alert = alert;
2848 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002849 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002850 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002851 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002852 }
Adam Langley75712922014-10-10 16:23:43 -07002853
David Benjamin1deb41b2016-08-09 19:36:38 -04002854 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002855 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002856 continue;
2857 }
2858
2859 CBS *contents = NULL, fake_contents;
2860 static const uint8_t kFakeRenegotiateExtension[] = {0};
2861 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2862 ssl_client_cipher_list_contains_cipher(client_hello,
2863 SSL3_CK_SCSV & 0xffff)) {
2864 /* The renegotiation SCSV was received so pretend that we received a
2865 * renegotiation extension. */
2866 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2867 sizeof(kFakeRenegotiateExtension));
2868 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002869 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002870 }
2871
2872 /* Extension wasn't observed so call the callback with a NULL
2873 * parameter. */
2874 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002875 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002876 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002877 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002878 *out_alert = alert;
2879 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002880 }
2881 }
2882
Adam Langleyfcf25832014-12-18 17:42:32 -08002883 return 1;
2884}
Adam Langley95c29f32014-06-20 12:00:00 -07002885
David Benjamin731058e2016-12-03 23:15:13 -05002886int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2887 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002888 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002889 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002890 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002891 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002892 return 0;
2893 }
Adam Langley95c29f32014-06-20 12:00:00 -07002894
David Benjamin8c880a22016-12-03 02:20:34 -05002895 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002896 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002897 return 0;
2898 }
Adam Langley95c29f32014-06-20 12:00:00 -07002899
Adam Langleyfcf25832014-12-18 17:42:32 -08002900 return 1;
2901}
Adam Langley95c29f32014-06-20 12:00:00 -07002902
David Benjamin8c880a22016-12-03 02:20:34 -05002903static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2904 int *out_alert) {
2905 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002906 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2907 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2908 return 1;
2909 }
Adam Langley614c66a2015-06-12 15:26:58 -07002910
Steven Valdez143e8b32016-07-11 13:19:03 -04002911 /* Decode the extensions block and check it is valid. */
2912 CBS extensions;
2913 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2914 !tls1_check_duplicate_extensions(&extensions)) {
2915 *out_alert = SSL_AD_DECODE_ERROR;
2916 return 0;
2917 }
2918
2919 uint32_t received = 0;
2920 while (CBS_len(&extensions) != 0) {
2921 uint16_t type;
2922 CBS extension;
2923
2924 /* Decode the next extension. */
2925 if (!CBS_get_u16(&extensions, &type) ||
2926 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002927 *out_alert = SSL_AD_DECODE_ERROR;
2928 return 0;
2929 }
Adam Langley95c29f32014-06-20 12:00:00 -07002930
Steven Valdez143e8b32016-07-11 13:19:03 -04002931 unsigned ext_index;
2932 const struct tls_extension *const ext =
2933 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002934
Steven Valdez143e8b32016-07-11 13:19:03 -04002935 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002936 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002937 return 0;
2938 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002939 continue;
2940 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002941
David Benjamina3d76d02017-07-14 19:36:07 -04002942 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
2943 "too many bits");
David Benjamin5db7c9b2017-01-24 16:17:03 -05002944
David Benjamin8c880a22016-12-03 02:20:34 -05002945 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002946 type != TLSEXT_TYPE_renegotiate) {
2947 /* If the extension was never sent then it is illegal, except for the
2948 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002949 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2950 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002951 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002952 return 0;
2953 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002954
Steven Valdez143e8b32016-07-11 13:19:03 -04002955 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002956
Steven Valdez143e8b32016-07-11 13:19:03 -04002957 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002958 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002959 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002960 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04002961 *out_alert = alert;
2962 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002963 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002964 }
Adam Langley95c29f32014-06-20 12:00:00 -07002965
David Benjamin54091232016-09-05 12:47:25 -04002966 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002967 if (!(received & (1u << i))) {
2968 /* Extension wasn't observed so call the callback with a NULL
2969 * parameter. */
2970 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002971 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002972 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002973 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002974 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002975 return 0;
2976 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002977 }
2978 }
Adam Langley95c29f32014-06-20 12:00:00 -07002979
Adam Langleyfcf25832014-12-18 17:42:32 -08002980 return 1;
2981}
Adam Langley95c29f32014-06-20 12:00:00 -07002982
David Benjamin8c880a22016-12-03 02:20:34 -05002983static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
2984 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002985 int ret = SSL_TLSEXT_ERR_NOACK;
2986 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002987
David Benjamin78f8aab2016-03-10 16:33:58 -05002988 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002989 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002990 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05002991 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
2992 ret = ssl->session_ctx->tlsext_servername_callback(
2993 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002994 }
Adam Langley95c29f32014-06-20 12:00:00 -07002995
Adam Langleyfcf25832014-12-18 17:42:32 -08002996 switch (ret) {
2997 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002998 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002999 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003000
Adam Langleyfcf25832014-12-18 17:42:32 -08003001 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003002 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003003 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003004
Adam Langleyfcf25832014-12-18 17:42:32 -08003005 default:
3006 return 1;
3007 }
3008}
Adam Langleyed8270a2014-09-02 13:52:56 -07003009
David Benjamin8c880a22016-12-03 02:20:34 -05003010int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3011 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08003012 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003013 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003014 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003015 return 0;
3016 }
3017
Adam Langleyfcf25832014-12-18 17:42:32 -08003018 return 1;
3019}
Adam Langley95c29f32014-06-20 12:00:00 -07003020
Adam Langley4c341d02017-03-08 19:33:21 -08003021static enum ssl_ticket_aead_result_t
3022ssl_decrypt_ticket_with_cipher_ctx(SSL *ssl, uint8_t **out, size_t *out_len,
3023 int *out_renew_ticket, const uint8_t *ticket,
3024 size_t ticket_len) {
Adam Langley4c341d02017-03-08 19:33:21 -08003025 const SSL_CTX *const ssl_ctx = ssl->session_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07003026
David Benjamin86e95b82017-07-18 16:34:25 -04003027 ScopedHMAC_CTX hmac_ctx;
3028 ScopedEVP_CIPHER_CTX cipher_ctx;
David Benjamine3aa1d92015-06-16 15:34:50 -04003029
David Benjaminadcc3952015-04-26 13:07:57 -04003030 /* Ensure there is room for the key name and the largest IV
3031 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3032 * the maximum IV length should be well under the minimum size for the
3033 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003034 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
David Benjamin81678aa2017-07-12 22:43:42 -04003035 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003036 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003037 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003038
David Benjamine3aa1d92015-06-16 15:34:50 -04003039 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003040 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
David Benjamin81678aa2017-07-12 22:43:42 -04003041 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(),
3042 hmac_ctx.get(), 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003043 if (cb_ret < 0) {
David Benjamin81678aa2017-07-12 22:43:42 -04003044 return ssl_ticket_aead_error;
Adam Langley4c341d02017-03-08 19:33:21 -08003045 } else if (cb_ret == 0) {
David Benjamin81678aa2017-07-12 22:43:42 -04003046 return ssl_ticket_aead_ignore_ticket;
Adam Langley4c341d02017-03-08 19:33:21 -08003047 } else if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003048 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003049 }
3050 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003051 /* Check the key name matches. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003052 if (OPENSSL_memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3053 SSL_TICKET_KEY_NAME_LEN) != 0) {
David Benjamin81678aa2017-07-12 22:43:42 -04003054 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003055 }
David Benjamin81678aa2017-07-12 22:43:42 -04003056 if (!HMAC_Init_ex(hmac_ctx.get(), ssl_ctx->tlsext_tick_hmac_key,
David Benjamine3aa1d92015-06-16 15:34:50 -04003057 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003058 NULL) ||
David Benjamin81678aa2017-07-12 22:43:42 -04003059 !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL,
David Benjamine3aa1d92015-06-16 15:34:50 -04003060 ssl_ctx->tlsext_tick_aes_key, iv)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003061 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08003062 }
3063 }
David Benjamin81678aa2017-07-12 22:43:42 -04003064 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx.get());
Adam Langleyfcf25832014-12-18 17:42:32 -08003065
David Benjamine3aa1d92015-06-16 15:34:50 -04003066 /* Check the MAC at the end of the ticket. */
3067 uint8_t mac[EVP_MAX_MD_SIZE];
David Benjamin81678aa2017-07-12 22:43:42 -04003068 size_t mac_len = HMAC_size(hmac_ctx.get());
David Benjamine3aa1d92015-06-16 15:34:50 -04003069 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003070 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamin81678aa2017-07-12 22:43:42 -04003071 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003072 }
David Benjamin81678aa2017-07-12 22:43:42 -04003073 HMAC_Update(hmac_ctx.get(), ticket, ticket_len - mac_len);
3074 HMAC_Final(hmac_ctx.get(), mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003075 int mac_ok =
3076 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3077#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3078 mac_ok = 1;
3079#endif
3080 if (!mac_ok) {
David Benjamin81678aa2017-07-12 22:43:42 -04003081 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003082 }
3083
David Benjamine3aa1d92015-06-16 15:34:50 -04003084 /* Decrypt the session data. */
3085 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3086 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3087 mac_len;
David Benjamin86e95b82017-07-18 16:34:25 -04003088 UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len));
David Benjamin81678aa2017-07-12 22:43:42 -04003089 if (!plaintext) {
3090 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08003091 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003092 size_t plaintext_len;
3093#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin81678aa2017-07-12 22:43:42 -04003094 OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003095 plaintext_len = ciphertext_len;
3096#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003097 if (ciphertext_len >= INT_MAX) {
David Benjamin81678aa2017-07-12 22:43:42 -04003098 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003099 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003100 int len1, len2;
David Benjamin81678aa2017-07-12 22:43:42 -04003101 if (!EVP_DecryptUpdate(cipher_ctx.get(), plaintext.get(), &len1, ciphertext,
David Benjamine3aa1d92015-06-16 15:34:50 -04003102 (int)ciphertext_len) ||
David Benjamin81678aa2017-07-12 22:43:42 -04003103 !EVP_DecryptFinal_ex(cipher_ctx.get(), plaintext.get() + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003104 ERR_clear_error();
David Benjamin81678aa2017-07-12 22:43:42 -04003105 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003106 }
Adam Langley4c341d02017-03-08 19:33:21 -08003107 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04003108#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003109
David Benjamin81678aa2017-07-12 22:43:42 -04003110 *out = plaintext.release();
Adam Langley4c341d02017-03-08 19:33:21 -08003111 *out_len = plaintext_len;
David Benjamin81678aa2017-07-12 22:43:42 -04003112 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08003113}
3114
3115static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
3116 SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
3117 const uint8_t *ticket, size_t ticket_len) {
David Benjamin81678aa2017-07-12 22:43:42 -04003118 uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len);
Adam Langley4c341d02017-03-08 19:33:21 -08003119 if (plaintext == NULL) {
3120 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3121 return ssl_ticket_aead_error;
3122 }
3123
3124 size_t plaintext_len;
3125 const enum ssl_ticket_aead_result_t result =
3126 ssl->session_ctx->ticket_aead_method->open(
3127 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3128
3129 if (result == ssl_ticket_aead_success) {
3130 *out = plaintext;
3131 plaintext = NULL;
3132 *out_len = plaintext_len;
3133 }
3134
3135 OPENSSL_free(plaintext);
3136 return result;
3137}
3138
3139enum ssl_ticket_aead_result_t ssl_process_ticket(
3140 SSL *ssl, SSL_SESSION **out_session, int *out_renew_ticket,
3141 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3142 size_t session_id_len) {
3143 *out_renew_ticket = 0;
3144 *out_session = NULL;
3145
3146 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3147 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3148 return ssl_ticket_aead_ignore_ticket;
3149 }
3150
3151 uint8_t *plaintext = NULL;
3152 size_t plaintext_len;
3153 enum ssl_ticket_aead_result_t result;
3154 if (ssl->session_ctx->ticket_aead_method != NULL) {
3155 result = ssl_decrypt_ticket_with_method(
3156 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3157 } else {
3158 result = ssl_decrypt_ticket_with_cipher_ctx(
3159 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3160 }
3161
3162 if (result != ssl_ticket_aead_success) {
3163 return result;
3164 }
3165
David Benjamine3aa1d92015-06-16 15:34:50 -04003166 /* Decode the session. */
Adam Langley46db7af2017-02-01 15:49:37 -08003167 SSL_SESSION *session =
3168 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx);
Adam Langley4c341d02017-03-08 19:33:21 -08003169 OPENSSL_free(plaintext);
3170
David Benjamine3aa1d92015-06-16 15:34:50 -04003171 if (session == NULL) {
3172 ERR_clear_error(); /* Don't leave an error on the queue. */
Adam Langley4c341d02017-03-08 19:33:21 -08003173 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003174 }
3175
3176 /* Copy the client's session ID into the new session, to denote the ticket has
3177 * been accepted. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003178 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003179 session->session_id_length = session_id_len;
3180
3181 *out_session = session;
Adam Langley4c341d02017-03-08 19:33:21 -08003182 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003183}
Adam Langley95c29f32014-06-20 12:00:00 -07003184
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003185int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003186 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003187 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003188 return 1;
3189 }
David Benjamincd996942014-07-20 16:23:51 -04003190
David Benjamin0fc37ef2016-08-17 15:29:46 -04003191 OPENSSL_free(hs->peer_sigalgs);
3192 hs->peer_sigalgs = NULL;
3193 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003194
3195 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003196 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003197 return 0;
3198 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003199 num_sigalgs /= 2;
3200
3201 /* supported_signature_algorithms in the certificate request is
3202 * allowed to be empty. */
3203 if (num_sigalgs == 0) {
3204 return 1;
3205 }
3206
Steven Valdez02563852016-06-23 13:33:05 -04003207 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3208 * and we just divided |num_sigalgs| by two. */
David Benjamin81678aa2017-07-12 22:43:42 -04003209 hs->peer_sigalgs = (uint16_t *)OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003210 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003211 return 0;
3212 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003213 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003214
3215 CBS sigalgs;
3216 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003217 for (size_t i = 0; i < num_sigalgs; i++) {
3218 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003219 return 0;
3220 }
3221 }
Adam Langley95c29f32014-06-20 12:00:00 -07003222
Adam Langleyfcf25832014-12-18 17:42:32 -08003223 return 1;
3224}
David Benjaminec2f27d2014-11-13 19:17:25 -05003225
David Benjamina3651382017-04-20 17:49:36 -04003226int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
3227 switch (EVP_PKEY_id(pkey)) {
3228 case EVP_PKEY_RSA:
3229 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3230 return 1;
3231 case EVP_PKEY_EC:
3232 *out = SSL_SIGN_ECDSA_SHA1;
3233 return 1;
3234 default:
3235 return 0;
3236 }
3237}
3238
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003239int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3240 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003241 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003242
Steven Valdezf0451ca2016-06-29 13:16:27 -04003243 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
David Benjamina3651382017-04-20 17:49:36 -04003244 * handshake. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04003245 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamina3651382017-04-20 17:49:36 -04003246 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey)) {
3247 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3248 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003249 }
David Benjamina3651382017-04-20 17:49:36 -04003250 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003251 }
3252
David Benjamin3ef76972016-10-17 17:59:54 -04003253 const uint16_t *sigalgs = cert->sigalgs;
3254 size_t num_sigalgs = cert->num_sigalgs;
3255 if (sigalgs == NULL) {
3256 sigalgs = kSignSignatureAlgorithms;
3257 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003258 }
3259
David Benjamin0fc37ef2016-08-17 15:29:46 -04003260 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3261 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3262 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003263 /* If the client didn't specify any signature_algorithms extension then
3264 * we can assume that it supports SHA1. See
3265 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3266 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3267 SSL_SIGN_ECDSA_SHA1};
3268 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003269 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003270 }
3271
David Benjamin0fc37ef2016-08-17 15:29:46 -04003272 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003273 uint16_t sigalg = sigalgs[i];
3274 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3275 * negotiated. */
3276 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjamina232a712017-03-30 15:51:53 -05003277 !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003278 continue;
3279 }
3280
David Benjamin0fc37ef2016-08-17 15:29:46 -04003281 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003282 if (sigalg == peer_sigalgs[j]) {
3283 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003284 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003285 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003286 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003287 }
Adam Langley95c29f32014-06-20 12:00:00 -07003288
David Benjaminea9a0d52016-07-08 15:52:59 -07003289 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3290 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003291}
Adam Langley95c29f32014-06-20 12:00:00 -07003292
Steven Valdez908ac192017-01-12 13:17:07 -05003293int tls1_verify_channel_id(SSL_HANDSHAKE *hs) {
3294 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003295 uint16_t extension_type;
3296 CBS extension, channel_id;
3297
3298 /* A Channel ID handshake message is structured to contain multiple
3299 * extensions, but the only one that can be present is Channel ID. */
3300 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3301 if (!CBS_get_u16(&channel_id, &extension_type) ||
3302 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3303 CBS_len(&channel_id) != 0 ||
3304 extension_type != TLSEXT_TYPE_channel_id ||
3305 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3306 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3307 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3308 return 0;
3309 }
3310
David Benjamin86e95b82017-07-18 16:34:25 -04003311 UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Nick Harper60a85cb2016-09-23 16:25:11 -07003312 if (!p256) {
3313 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3314 return 0;
3315 }
3316
David Benjamin86e95b82017-07-18 16:34:25 -04003317 UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3318 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
David Benjamin81678aa2017-07-12 22:43:42 -04003319 if (!sig || !x || !y) {
3320 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003321 }
3322
3323 const uint8_t *p = CBS_data(&extension);
David Benjamin81678aa2017-07-12 22:43:42 -04003324 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3325 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3326 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3327 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
3328 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003329 }
3330
David Benjamin86e95b82017-07-18 16:34:25 -04003331 UniquePtr<EC_KEY> key(EC_KEY_new());
3332 UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
David Benjamin81678aa2017-07-12 22:43:42 -04003333 if (!key || !point ||
3334 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3335 y.get(), nullptr) ||
3336 !EC_KEY_set_group(key.get(), p256.get()) ||
3337 !EC_KEY_set_public_key(key.get(), point.get())) {
3338 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003339 }
3340
3341 uint8_t digest[EVP_MAX_MD_SIZE];
3342 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003343 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003344 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003345 }
3346
David Benjamin81678aa2017-07-12 22:43:42 -04003347 int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
Nick Harper60a85cb2016-09-23 16:25:11 -07003348#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3349 sig_ok = 1;
3350#endif
3351 if (!sig_ok) {
3352 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3353 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3354 ssl->s3->tlsext_channel_id_valid = 0;
David Benjamin81678aa2017-07-12 22:43:42 -04003355 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003356 }
3357
David Benjamin17cf2cb2016-12-13 01:07:13 -05003358 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
David Benjamin81678aa2017-07-12 22:43:42 -04003359 return 1;
Nick Harper60a85cb2016-09-23 16:25:11 -07003360}
3361
Steven Valdez908ac192017-01-12 13:17:07 -05003362int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3363 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003364 uint8_t digest[EVP_MAX_MD_SIZE];
3365 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003366 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003367 return 0;
3368 }
3369
3370 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3371 if (ec_key == NULL) {
3372 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3373 return 0;
3374 }
3375
3376 int ret = 0;
3377 BIGNUM *x = BN_new();
3378 BIGNUM *y = BN_new();
3379 ECDSA_SIG *sig = NULL;
3380 if (x == NULL || y == NULL ||
3381 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3382 EC_KEY_get0_public_key(ec_key),
3383 x, y, NULL)) {
3384 goto err;
3385 }
3386
3387 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3388 if (sig == NULL) {
3389 goto err;
3390 }
3391
3392 CBB child;
3393 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3394 !CBB_add_u16_length_prefixed(cbb, &child) ||
3395 !BN_bn2cbb_padded(&child, 32, x) ||
3396 !BN_bn2cbb_padded(&child, 32, y) ||
3397 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3398 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3399 !CBB_flush(cbb)) {
3400 goto err;
3401 }
3402
3403 ret = 1;
3404
3405err:
3406 BN_free(x);
3407 BN_free(y);
3408 ECDSA_SIG_free(sig);
3409 return ret;
3410}
3411
Steven Valdez908ac192017-01-12 13:17:07 -05003412int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3413 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003414 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3415 uint8_t *msg;
3416 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003417 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003418 ssl_cert_verify_channel_id)) {
3419 return 0;
3420 }
3421 SHA256(msg, msg_len, out);
3422 *out_len = SHA256_DIGEST_LENGTH;
3423 OPENSSL_free(msg);
3424 return 1;
3425 }
3426
Nick Harper95594012016-10-20 14:07:13 -07003427 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003428
Nick Harper95594012016-10-20 14:07:13 -07003429 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003430 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003431 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003432
Steven Valdez87eab492016-06-27 16:34:59 -04003433 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003434 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003435 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003436 if (ssl->session->original_handshake_hash_len == 0) {
3437 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003438 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003439 }
Nick Harper95594012016-10-20 14:07:13 -07003440 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3441 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003442 }
3443
Steven Valdez908ac192017-01-12 13:17:07 -05003444 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3445 size_t hs_hash_len;
3446 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript, hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003447 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003448 }
Steven Valdez908ac192017-01-12 13:17:07 -05003449 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003450 SHA256_Final(out, &ctx);
3451 *out_len = SHA256_DIGEST_LENGTH;
3452 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003453}
Adam Langley1258b6a2014-06-20 12:00:00 -07003454
3455/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin45738dd2017-02-09 20:01:26 -05003456 * hashes in |hs->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003457 * data. */
Steven Valdez908ac192017-01-12 13:17:07 -05003458int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3459 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003460 /* This function should never be called for a resumed session because the
3461 * handshake hashes that we wish to record are for the original, full
3462 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003463 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003464 return -1;
3465 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003466
David Benjamina3d76d02017-07-14 19:36:07 -04003467 static_assert(
David Benjamin45738dd2017-02-09 20:01:26 -05003468 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
David Benjamina3d76d02017-07-14 19:36:07 -04003469 "original_handshake_hash is too small");
Steven Valdez908ac192017-01-12 13:17:07 -05003470
3471 size_t digest_len;
3472 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript,
David Benjamin45738dd2017-02-09 20:01:26 -05003473 hs->new_session->original_handshake_hash,
Steven Valdez908ac192017-01-12 13:17:07 -05003474 &digest_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003475 return -1;
3476 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003477
David Benjamina3d76d02017-07-14 19:36:07 -04003478 static_assert(EVP_MAX_MD_SIZE <= 0xff,
3479 "EVP_MAX_MD_SIZE does not fit in uint8_t");
David Benjamin45738dd2017-02-09 20:01:26 -05003480 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003481
Adam Langleyfcf25832014-12-18 17:42:32 -08003482 return 1;
3483}
Nick Harper60a85cb2016-09-23 16:25:11 -07003484
3485int ssl_do_channel_id_callback(SSL *ssl) {
3486 if (ssl->tlsext_channel_id_private != NULL ||
3487 ssl->ctx->channel_id_cb == NULL) {
3488 return 1;
3489 }
3490
3491 EVP_PKEY *key = NULL;
3492 ssl->ctx->channel_id_cb(ssl, &key);
3493 if (key == NULL) {
3494 /* The caller should try again later. */
3495 return 1;
3496 }
3497
3498 int ret = SSL_set1_tls_channel_id(ssl, key);
3499 EVP_PKEY_free(key);
3500 return ret;
3501}
Adam Langleycfa08c32016-11-17 13:21:27 -08003502
3503int ssl_is_sct_list_valid(const CBS *contents) {
3504 /* Shallow parse the SCT list for sanity. By the RFC
3505 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3506 * of the SCTs may be empty. */
3507 CBS copy = *contents;
3508 CBS sct_list;
3509 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3510 CBS_len(&copy) != 0 ||
3511 CBS_len(&sct_list) == 0) {
3512 return 0;
3513 }
3514
3515 while (CBS_len(&sct_list) > 0) {
3516 CBS sct;
3517 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3518 CBS_len(&sct) == 0) {
3519 return 0;
3520 }
3521 }
3522
3523 return 1;
3524}
David Benjamin86e95b82017-07-18 16:34:25 -04003525
3526} // namespace bssl
3527
3528using namespace bssl;
3529
3530int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
3531 uint16_t extension_type,
3532 const uint8_t **out_data,
3533 size_t *out_len) {
3534 CBS cbs;
3535 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
3536 return 0;
3537 }
3538
3539 *out_data = CBS_data(&cbs);
3540 *out_len = CBS_len(&cbs);
3541 return 1;
3542}
3543
3544void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
3545 ctx->ed25519_enabled = !!enabled;
3546}
3547
3548int SSL_extension_supported(unsigned extension_value) {
3549 uint32_t index;
3550 return extension_value == TLSEXT_TYPE_padding ||
3551 tls_extension_find(&index, extension_value) != NULL;
3552}