blob: 32311ff600a65fcb12665fbe9d31209753796f06 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin499742c2017-07-22 12:45:38 -0400116#include <utility>
117
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
David Benjaminc11ea9422017-08-29 16:33:21 -0400147// 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
150// 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
David Benjaminc11ea9422017-08-29 16:33:21 -0400157 // First pass: count the extensions.
Adam Langleyfcf25832014-12-18 17:42:32 -0800158 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
David Benjaminc11ea9422017-08-29 16:33:21 -0400181 // Second pass: gather the extension types.
Adam Langleyfcf25832014-12-18 17:42:32 -0800182 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)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400188 // This should not happen.
Adam Langleyfcf25832014-12-18 17:42:32 -0800189 goto done;
190 }
191 }
192 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400193
David Benjaminc11ea9422017-08-29 16:33:21 -0400194 // Sort the extensions and make sure there are no duplicates.
Adam Langleyfcf25832014-12-18 17:42:32 -0800195 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 Benjamin7934f082017-08-01 16:32:25 -0400209int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out,
210 const SSLMessage &msg) {
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;
David Benjamin7934f082017-08-01 16:32:25 -0400213 out->client_hello = CBS_data(&msg.body);
214 out->client_hello_len = CBS_len(&msg.body);
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
David Benjaminc11ea9422017-08-29 16:33:21 -0400230 // 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
David Benjaminc11ea9422017-08-29 16:33:21 -0400252 // If the ClientHello ends here then it's valid, but doesn't have any
253 // extensions. (E.g. SSLv3.)
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 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
David Benjaminc11ea9422017-08-29 16:33:21 -0400260 // 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 Benjaminc11ea9422017-08-29 16:33:21 -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 Benjamincf0ce672017-09-21 02:25:59 -0400302Span<const uint16_t> tls1_get_grouplist(const SSL *ssl) {
303 if (ssl->supported_group_list != nullptr) {
304 return MakeConstSpan(ssl->supported_group_list,
305 ssl->supported_group_list_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800306 }
David Benjamincf0ce672017-09-21 02:25:59 -0400307 return Span<const uint16_t>(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800308}
David Benjamined439582014-07-14 19:13:02 -0400309
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900310int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
311 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400312 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400313
David Benjaminc11ea9422017-08-29 16:33:21 -0400314 // Clients are not required to send a supported_groups extension. In this
315 // case, the server is free to pick any group it likes. See RFC 4492,
316 // section 4, paragraph 3.
317 //
318 // However, in the interests of compatibility, we will skip ECDH if the
319 // client didn't send an extension because we can't be sure that they'll
320 // support our favoured group. Thus we do not special-case an emtpy
321 // |peer_supported_group_list|.
David Benjamin55a43642015-04-20 14:45:55 -0400322
David Benjamincf0ce672017-09-21 02:25:59 -0400323 Span<const uint16_t> groups = tls1_get_grouplist(ssl);
324 Span<const uint16_t> pref, supp;
David Benjamin4298d772015-12-19 00:18:25 -0500325 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400326 pref = groups;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900327 supp = hs->peer_supported_group_list;
David Benjamin55a43642015-04-20 14:45:55 -0400328 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900329 pref = hs->peer_supported_group_list;
Steven Valdezce902a92016-05-17 11:47:53 -0400330 supp = groups;
David Benjamin55a43642015-04-20 14:45:55 -0400331 }
332
David Benjamincf0ce672017-09-21 02:25:59 -0400333 for (uint16_t pref_group : pref) {
334 for (uint16_t supp_group : supp) {
335 if (pref_group == supp_group) {
336 *out_group_id = pref_group;
David Benjamin4298d772015-12-19 00:18:25 -0500337 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800338 }
339 }
340 }
341
David Benjamin4298d772015-12-19 00:18:25 -0500342 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800343}
Adam Langley95c29f32014-06-20 12:00:00 -0700344
Steven Valdezce902a92016-05-17 11:47:53 -0400345int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800346 const int *curves, size_t ncurves) {
David Benjamin81678aa2017-07-12 22:43:42 -0400347 uint16_t *group_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
Steven Valdezce902a92016-05-17 11:47:53 -0400348 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800349 return 0;
350 }
351
David Benjamin54091232016-09-05 12:47:25 -0400352 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400353 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
354 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800355 return 0;
356 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 }
358
Steven Valdezce902a92016-05-17 11:47:53 -0400359 OPENSSL_free(*out_group_ids);
360 *out_group_ids = group_ids;
361 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800362
363 return 1;
364}
Adam Langley95c29f32014-06-20 12:00:00 -0700365
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100366int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
367 const char *curves) {
368 uint16_t *group_ids = NULL;
369 size_t ncurves = 0;
370
371 const char *col;
372 const char *ptr = curves;
373
374 do {
375 col = strchr(ptr, ':');
376
377 uint16_t group_id;
378 if (!ssl_name_to_group_id(&group_id, ptr,
379 col ? (size_t)(col - ptr) : strlen(ptr))) {
380 goto err;
381 }
382
David Benjamin81678aa2017-07-12 22:43:42 -0400383 uint16_t *new_group_ids = (uint16_t *)OPENSSL_realloc(
384 group_ids, (ncurves + 1) * sizeof(uint16_t));
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100385 if (new_group_ids == NULL) {
386 goto err;
387 }
388 group_ids = new_group_ids;
389
390 group_ids[ncurves] = group_id;
391 ncurves++;
392
393 if (col) {
394 ptr = col + 1;
395 }
396 } while (col);
397
398 OPENSSL_free(*out_group_ids);
399 *out_group_ids = group_ids;
400 *out_group_ids_len = ncurves;
401
402 return 1;
403
404err:
405 OPENSSL_free(group_ids);
406 return 0;
407}
408
David Benjamincf0ce672017-09-21 02:25:59 -0400409int tls1_check_group_id(const SSL *ssl, uint16_t group_id) {
410 for (uint16_t supported : tls1_get_grouplist(ssl)) {
411 if (supported == group_id) {
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400412 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 }
David Benjamin033e5f42014-11-13 18:47:41 -0500415
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400416 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800417}
David Benjamin033e5f42014-11-13 18:47:41 -0500418
David Benjaminc11ea9422017-08-29 16:33:21 -0400419// kVerifySignatureAlgorithms is the default list of accepted signature
420// algorithms for verifying.
421//
422// For now, RSA-PSS signature algorithms are not enabled on Android's system
423// BoringSSL. Once the change in Chrome has stuck and the values are finalized,
424// restore them.
David Benjamin3ef76972016-10-17 17:59:54 -0400425static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400426 // List our preferred algorithms first.
David Benjamin69522112017-03-28 15:38:29 -0500427 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400428 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400429#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400430 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400431#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400432 SSL_SIGN_RSA_PKCS1_SHA256,
433
David Benjaminc11ea9422017-08-29 16:33:21 -0400434 // Larger hashes are acceptable.
David Benjamin3a322f52016-10-26 12:45:35 -0400435 SSL_SIGN_ECDSA_SECP384R1_SHA384,
436#if !defined(BORINGSSL_ANDROID_SYSTEM)
437 SSL_SIGN_RSA_PSS_SHA384,
438#endif
439 SSL_SIGN_RSA_PKCS1_SHA384,
440
David Benjaminc11ea9422017-08-29 16:33:21 -0400441 // TODO(davidben): Remove this.
David Benjamin3ef76972016-10-17 17:59:54 -0400442#if defined(BORINGSSL_ANDROID_SYSTEM)
443 SSL_SIGN_ECDSA_SECP521R1_SHA512,
444#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400445#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400446 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400447#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400448 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400449
David Benjaminc11ea9422017-08-29 16:33:21 -0400450 // For now, SHA-1 is still accepted but least preferable.
David Benjamin3a322f52016-10-26 12:45:35 -0400451 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400452
David Benjamin3a322f52016-10-26 12:45:35 -0400453};
454
David Benjaminc11ea9422017-08-29 16:33:21 -0400455// kSignSignatureAlgorithms is the default list of supported signature
456// algorithms for signing.
457//
458// For now, RSA-PSS signature algorithms are not enabled on Android's system
459// BoringSSL. Once the change in Chrome has stuck and the values are finalized,
460// restore them.
David Benjamin3a322f52016-10-26 12:45:35 -0400461static const uint16_t kSignSignatureAlgorithms[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400462 // List our preferred algorithms first.
David Benjamin69522112017-03-28 15:38:29 -0500463 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400464 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400465#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400466 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400467#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400468 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400469
David Benjaminc11ea9422017-08-29 16:33:21 -0400470 // If needed, sign larger hashes.
471 //
472 // TODO(davidben): Determine which of these may be pruned.
David Benjamin3a322f52016-10-26 12:45:35 -0400473 SSL_SIGN_ECDSA_SECP384R1_SHA384,
474#if !defined(BORINGSSL_ANDROID_SYSTEM)
475 SSL_SIGN_RSA_PSS_SHA384,
476#endif
477 SSL_SIGN_RSA_PKCS1_SHA384,
478
479 SSL_SIGN_ECDSA_SECP521R1_SHA512,
480#if !defined(BORINGSSL_ANDROID_SYSTEM)
481 SSL_SIGN_RSA_PSS_SHA512,
482#endif
483 SSL_SIGN_RSA_PKCS1_SHA512,
484
David Benjaminc11ea9422017-08-29 16:33:21 -0400485 // If the peer supports nothing else, sign with SHA-1.
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400486 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400487 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400488};
489
David Benjamin69522112017-03-28 15:38:29 -0500490int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) {
David Benjamin71c21b42017-04-14 17:05:40 -0400491 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
492 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
493 if (ssl->ctx->num_verify_sigalgs != 0) {
494 sigalgs = ssl->ctx->verify_sigalgs;
495 num_sigalgs = ssl->ctx->num_verify_sigalgs;
496 }
497
498 for (size_t i = 0; i < num_sigalgs; i++) {
499 if (sigalgs == kVerifySignatureAlgorithms &&
500 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500501 !ssl->ctx->ed25519_enabled) {
502 continue;
503 }
David Benjamin71c21b42017-04-14 17:05:40 -0400504 if (!CBB_add_u16(out, sigalgs[i])) {
David Benjamin69522112017-03-28 15:38:29 -0500505 return 0;
506 }
507 }
508
509 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800510}
Adam Langley95c29f32014-06-20 12:00:00 -0700511
David Benjamin8d606e32017-06-15 22:43:04 -0400512int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg) {
David Benjamin71c21b42017-04-14 17:05:40 -0400513 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
514 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
515 if (ssl->ctx->num_verify_sigalgs != 0) {
516 sigalgs = ssl->ctx->verify_sigalgs;
517 num_sigalgs = ssl->ctx->num_verify_sigalgs;
518 }
519
520 for (size_t i = 0; i < num_sigalgs; i++) {
521 if (sigalgs == kVerifySignatureAlgorithms &&
522 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500523 !ssl->ctx->ed25519_enabled) {
524 continue;
525 }
David Benjamin71c21b42017-04-14 17:05:40 -0400526 if (sigalg == sigalgs[i]) {
David Benjamin3ef76972016-10-17 17:59:54 -0400527 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 }
529 }
530
David Benjamin3ef76972016-10-17 17:59:54 -0400531 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
532 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
533 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800534}
535
David Benjaminc11ea9422017-08-29 16:33:21 -0400536// tls_extension represents a TLS extension that is handled internally. The
537// |init| function is called for each handshake, before any other functions of
538// the extension. Then the add and parse callbacks are called as needed.
539//
540// The parse callbacks receive a |CBS| that contains the contents of the
541// extension (i.e. not including the type and length bytes). If an extension is
542// not received then the parse callbacks will be called with a NULL CBS so that
543// they can do any processing needed to handle the absence of an extension.
544//
545// The add callbacks receive a |CBB| to which the extension can be appended but
546// the function is responsible for appending the type and length bytes too.
547//
548// All callbacks return one for success and zero for error. If a parse function
549// returns zero then a fatal alert with value |*out_alert| will be sent. If
550// |*out_alert| isn't set, then a |decode_error| alert will be sent.
Adam Langley614c66a2015-06-12 15:26:58 -0700551struct tls_extension {
552 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500553 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700554
David Benjamin8c880a22016-12-03 02:20:34 -0500555 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
556 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
557 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700558
David Benjamin8c880a22016-12-03 02:20:34 -0500559 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
560 CBS *contents);
561 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700562};
563
David Benjamin8c880a22016-12-03 02:20:34 -0500564static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
565 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400566 if (contents != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400567 // Servers MUST NOT send this extension.
Steven Valdez6b8509a2016-07-12 13:38:32 -0400568 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
569 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
570 return 0;
571 }
572
573 return 1;
574}
575
David Benjamin8c880a22016-12-03 02:20:34 -0500576static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
577 CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400578 // This extension from the client is handled elsewhere.
Steven Valdez6b8509a2016-07-12 13:38:32 -0400579 return 1;
580}
581
David Benjamin8c880a22016-12-03 02:20:34 -0500582static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400583 return 1;
584}
Adam Langley614c66a2015-06-12 15:26:58 -0700585
David Benjaminc11ea9422017-08-29 16:33:21 -0400586// Server name indication (SNI).
587//
588// https://tools.ietf.org/html/rfc6066#section-3.
Adam Langley614c66a2015-06-12 15:26:58 -0700589
David Benjamin8c880a22016-12-03 02:20:34 -0500590static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
591 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700592 if (ssl->tlsext_hostname == NULL) {
593 return 1;
594 }
595
596 CBB contents, server_name_list, name;
597 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
598 !CBB_add_u16_length_prefixed(out, &contents) ||
599 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
600 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
601 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
602 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
603 strlen(ssl->tlsext_hostname)) ||
604 !CBB_flush(out)) {
605 return 0;
606 }
607
608 return 1;
609}
610
David Benjamin8c880a22016-12-03 02:20:34 -0500611static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500612 CBS *contents) {
David Benjamina8614602017-09-06 15:40:19 -0400613 // The server may acknowledge SNI with an empty extension. We check the syntax
614 // but otherwise ignore this signal.
615 return contents == NULL || CBS_len(contents) == 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700616}
617
David Benjamin8c880a22016-12-03 02:20:34 -0500618static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500619 CBS *contents) {
David Benjamina8614602017-09-06 15:40:19 -0400620 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700621 if (contents == NULL) {
622 return 1;
623 }
624
David Benjamin9b611e22016-03-03 08:48:30 -0500625 CBS server_name_list, host_name;
626 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700627 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500628 !CBS_get_u8(&server_name_list, &name_type) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400629 // Although the server_name extension was intended to be extensible to
630 // new name types and multiple names, OpenSSL 1.0.x had a bug which meant
631 // different name types will cause an error. Further, RFC 4366 originally
632 // defined syntax inextensibly. RFC 6066 corrected this mistake, but
633 // adding new name types is no longer feasible.
634 //
635 // Act as if the extensibility does not exist to simplify parsing.
David Benjamin9b611e22016-03-03 08:48:30 -0500636 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
637 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700638 CBS_len(contents) != 0) {
639 return 0;
640 }
641
David Benjamin9b611e22016-03-03 08:48:30 -0500642 if (name_type != TLSEXT_NAMETYPE_host_name ||
643 CBS_len(&host_name) == 0 ||
644 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
645 CBS_contains_zero_byte(&host_name)) {
646 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
647 return 0;
648 }
Adam Langley614c66a2015-06-12 15:26:58 -0700649
David Benjaminc11ea9422017-08-29 16:33:21 -0400650 // Copy the hostname as a string.
David Benjamina8614602017-09-06 15:40:19 -0400651 if (!CBS_strdup(&host_name, &ssl->s3->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900652 *out_alert = SSL_AD_INTERNAL_ERROR;
653 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700654 }
655
David Benjaminfd45ee72017-08-31 14:49:09 -0400656 hs->should_ack_sni = true;
Adam Langley614c66a2015-06-12 15:26:58 -0700657 return 1;
658}
659
David Benjamin8c880a22016-12-03 02:20:34 -0500660static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
661 if (hs->ssl->s3->session_reused ||
662 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700663 return 1;
664 }
665
666 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
667 !CBB_add_u16(out, 0 /* length */)) {
668 return 0;
669 }
670
671 return 1;
672}
673
674
David Benjaminc11ea9422017-08-29 16:33:21 -0400675// Renegotiation indication.
676//
677// https://tools.ietf.org/html/rfc5746
Adam Langley5021b222015-06-12 18:27:58 -0700678
David Benjamin8c880a22016-12-03 02:20:34 -0500679static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
680 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400681 // Renegotiation indication is not necessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -0400682 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400683 return 1;
684 }
685
David Benjamin52bf6902016-10-08 12:05:03 -0400686 assert(ssl->s3->initial_handshake_complete ==
687 (ssl->s3->previous_client_finished_len != 0));
688
Adam Langley5021b222015-06-12 18:27:58 -0700689 CBB contents, prev_finished;
690 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
691 !CBB_add_u16_length_prefixed(out, &contents) ||
692 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
693 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
694 ssl->s3->previous_client_finished_len) ||
695 !CBB_flush(out)) {
696 return 0;
697 }
698
699 return 1;
700}
701
David Benjamin8c880a22016-12-03 02:20:34 -0500702static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700703 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500704 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400705 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez246eeee2017-03-26 12:49:17 -0500706 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400707 return 0;
708 }
709
David Benjaminc11ea9422017-08-29 16:33:21 -0400710 // Servers may not switch between omitting the extension and supporting it.
711 // See RFC 5746, sections 3.5 and 4.2.
David Benjamin3e052de2015-11-25 20:10:31 -0500712 if (ssl->s3->initial_handshake_complete &&
713 (contents != NULL) != ssl->s3->send_connection_binding) {
714 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
715 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
716 return 0;
717 }
718
Adam Langley5021b222015-06-12 18:27:58 -0700719 if (contents == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400720 // Strictly speaking, if we want to avoid an attack we should *always* see
721 // RI even on initial ServerHello because the client doesn't see any
722 // renegotiation during an attack. However this would mean we could not
723 // connect to any server which doesn't support RI.
724 //
725 // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
726 // practical terms every client sets it so it's just assumed here.
David Benjamine9cddb82015-11-23 14:36:40 -0500727 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700728 }
729
730 const size_t expected_len = ssl->s3->previous_client_finished_len +
731 ssl->s3->previous_server_finished_len;
732
David Benjaminc11ea9422017-08-29 16:33:21 -0400733 // Check for logic errors
Adam Langley5021b222015-06-12 18:27:58 -0700734 assert(!expected_len || ssl->s3->previous_client_finished_len);
735 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400736 assert(ssl->s3->initial_handshake_complete ==
737 (ssl->s3->previous_client_finished_len != 0));
738 assert(ssl->s3->initial_handshake_complete ==
739 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700740
David Benjaminc11ea9422017-08-29 16:33:21 -0400741 // Parse out the extension contents.
Adam Langley5021b222015-06-12 18:27:58 -0700742 CBS renegotiated_connection;
743 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
744 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400745 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700746 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
747 return 0;
748 }
749
David Benjaminc11ea9422017-08-29 16:33:21 -0400750 // Check that the extension matches.
Adam Langley5021b222015-06-12 18:27:58 -0700751 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400752 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700753 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
754 return 0;
755 }
756
757 const uint8_t *d = CBS_data(&renegotiated_connection);
David Benjamin9343b0b2017-07-01 00:31:27 -0400758 int ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
759 ssl->s3->previous_client_finished_len) == 0;
760#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
761 ok = 1;
762#endif
763 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400764 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700765 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
766 return 0;
767 }
768 d += ssl->s3->previous_client_finished_len;
769
David Benjamin9343b0b2017-07-01 00:31:27 -0400770 ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
771 ssl->s3->previous_server_finished_len) == 0;
772#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
773 ok = 1;
774#endif
775 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400776 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley10e10602017-07-25 13:33:21 -0700777 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Adam Langley5021b222015-06-12 18:27:58 -0700778 return 0;
779 }
David Benjamin046bc1f2017-08-31 15:06:42 -0400780 ssl->s3->send_connection_binding = true;
Adam Langley5021b222015-06-12 18:27:58 -0700781
782 return 1;
783}
784
David Benjamin8c880a22016-12-03 02:20:34 -0500785static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700786 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500787 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400788 // Renegotiation isn't supported as a server so this function should never be
789 // called after the initial handshake.
Adam Langley5021b222015-06-12 18:27:58 -0700790 assert(!ssl->s3->initial_handshake_complete);
791
Steven Valdez143e8b32016-07-11 13:19:03 -0400792 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
793 return 1;
794 }
795
Adam Langley5021b222015-06-12 18:27:58 -0700796 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400797 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700798 }
799
800 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700801 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
802 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400803 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700804 return 0;
805 }
806
David Benjaminc11ea9422017-08-29 16:33:21 -0400807 // Check that the extension matches. We do not support renegotiation as a
808 // server, so this must be empty.
David Benjamin52bf6902016-10-08 12:05:03 -0400809 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400810 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700811 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
812 return 0;
813 }
814
David Benjamin046bc1f2017-08-31 15:06:42 -0400815 ssl->s3->send_connection_binding = true;
Adam Langley5021b222015-06-12 18:27:58 -0700816
817 return 1;
818}
819
David Benjamin8c880a22016-12-03 02:20:34 -0500820static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
821 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400822 // Renegotiation isn't supported as a server so this function should never be
823 // called after the initial handshake.
David Benjamin52bf6902016-10-08 12:05:03 -0400824 assert(!ssl->s3->initial_handshake_complete);
825
Steven Valdez143e8b32016-07-11 13:19:03 -0400826 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
827 return 1;
828 }
829
Adam Langley5021b222015-06-12 18:27:58 -0700830 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400831 !CBB_add_u16(out, 1 /* length */) ||
832 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700833 return 0;
834 }
835
836 return 1;
837}
838
Adam Langley0a056712015-07-01 15:03:33 -0700839
David Benjaminc11ea9422017-08-29 16:33:21 -0400840// Extended Master Secret.
841//
842// https://tools.ietf.org/html/rfc7627
Adam Langley0a056712015-07-01 15:03:33 -0700843
David Benjamin8c880a22016-12-03 02:20:34 -0500844static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400845 // Extended master secret is not necessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -0400846 if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700847 return 1;
848 }
849
850 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
851 !CBB_add_u16(out, 0 /* length */)) {
852 return 0;
853 }
854
855 return 1;
856}
857
David Benjamin8c880a22016-12-03 02:20:34 -0500858static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700859 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500860 SSL *const ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500861
862 if (contents != NULL) {
863 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
864 ssl->version == SSL3_VERSION ||
865 CBS_len(contents) != 0) {
David Benjamin163c9562016-08-29 23:14:17 -0400866 return 0;
867 }
868
David Benjaminfd45ee72017-08-31 14:49:09 -0400869 hs->extended_master_secret = true;
David Benjamin163c9562016-08-29 23:14:17 -0400870 }
871
David Benjaminc11ea9422017-08-29 16:33:21 -0400872 // Whether EMS is negotiated may not change on renegotiation.
David Benjaminfc02b592017-02-17 16:26:01 -0500873 if (ssl->s3->established_session != NULL &&
874 hs->extended_master_secret !=
David Benjaminfd45ee72017-08-31 14:49:09 -0400875 !!ssl->s3->established_session->extended_master_secret) {
David Benjaminfc02b592017-02-17 16:26:01 -0500876 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
877 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400878 return 0;
879 }
880
Adam Langley0a056712015-07-01 15:03:33 -0700881 return 1;
882}
883
David Benjamin8c880a22016-12-03 02:20:34 -0500884static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500885 CBS *contents) {
David Benjaminfc02b592017-02-17 16:26:01 -0500886 uint16_t version = ssl3_protocol_version(hs->ssl);
David Benjamin8c880a22016-12-03 02:20:34 -0500887 if (version >= TLS1_3_VERSION ||
888 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400889 return 1;
890 }
891
892 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700893 return 1;
894 }
895
896 if (CBS_len(contents) != 0) {
897 return 0;
898 }
899
David Benjaminfd45ee72017-08-31 14:49:09 -0400900 hs->extended_master_secret = true;
Adam Langley0a056712015-07-01 15:03:33 -0700901 return 1;
902}
903
David Benjamin8c880a22016-12-03 02:20:34 -0500904static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -0500905 if (!hs->extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700906 return 1;
907 }
908
909 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
910 !CBB_add_u16(out, 0 /* length */)) {
911 return 0;
912 }
913
914 return 1;
915}
916
Adam Langley9b05bc52015-07-01 15:25:33 -0700917
David Benjaminc11ea9422017-08-29 16:33:21 -0400918// Session tickets.
919//
920// https://tools.ietf.org/html/rfc5077
Adam Langley9b05bc52015-07-01 15:25:33 -0700921
David Benjamin8c880a22016-12-03 02:20:34 -0500922static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
923 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400924 // TLS 1.3 uses a different ticket extension.
David Benjamin68161cb2017-06-20 14:49:43 -0400925 if (hs->min_version >= TLS1_3_VERSION ||
David Benjamin7c7d8312016-08-20 13:39:03 -0400926 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700927 return 1;
928 }
929
930 const uint8_t *ticket_data = NULL;
931 int ticket_len = 0;
932
David Benjaminc11ea9422017-08-29 16:33:21 -0400933 // Renegotiation does not participate in session resumption. However, still
934 // advertise the extension to avoid potentially breaking servers which carry
935 // over the state from the previous handshake, such as OpenSSL servers
936 // without upstream's 3c3f0259238594d77264a78944d409f2127642c4.
Adam Langley9b05bc52015-07-01 15:25:33 -0700937 if (!ssl->s3->initial_handshake_complete &&
938 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400939 ssl->session->tlsext_tick != NULL &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400940 // Don't send TLS 1.3 session tickets in the ticket extension.
Steven Valdez8f36c512017-06-20 10:55:02 -0400941 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700942 ticket_data = ssl->session->tlsext_tick;
943 ticket_len = ssl->session->tlsext_ticklen;
944 }
945
946 CBB ticket;
947 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
948 !CBB_add_u16_length_prefixed(out, &ticket) ||
949 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
950 !CBB_flush(out)) {
951 return 0;
952 }
953
954 return 1;
955}
956
David Benjamin8c880a22016-12-03 02:20:34 -0500957static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -0700958 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500959 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -0700960 if (contents == NULL) {
961 return 1;
962 }
963
Steven Valdez143e8b32016-07-11 13:19:03 -0400964 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
965 return 0;
966 }
967
David Benjaminc11ea9422017-08-29 16:33:21 -0400968 // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
969 // this function should never be called, even if the server tries to send the
970 // extension.
Adam Langley9b05bc52015-07-01 15:25:33 -0700971 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
972
973 if (CBS_len(contents) != 0) {
974 return 0;
975 }
976
David Benjaminfd45ee72017-08-31 14:49:09 -0400977 hs->ticket_expected = true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700978 return 1;
979}
980
David Benjamin8c880a22016-12-03 02:20:34 -0500981static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
982 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700983 return 1;
984 }
985
David Benjaminc11ea9422017-08-29 16:33:21 -0400986 // If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true.
David Benjamin8c880a22016-12-03 02:20:34 -0500987 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -0700988
989 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
990 !CBB_add_u16(out, 0 /* length */)) {
991 return 0;
992 }
993
994 return 1;
995}
996
997
David Benjaminc11ea9422017-08-29 16:33:21 -0400998// Signature Algorithms.
999//
1000// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
Adam Langley2e857bd2015-07-01 16:09:19 -07001001
David Benjamin8c880a22016-12-03 02:20:34 -05001002static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1003 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001004 if (hs->max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001005 return 1;
1006 }
1007
David Benjamin0fc37ef2016-08-17 15:29:46 -04001008 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001009 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1010 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin69522112017-03-28 15:38:29 -05001011 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
1012 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
1013 !CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001014 return 0;
1015 }
1016
1017 return 1;
1018}
1019
David Benjamin8c880a22016-12-03 02:20:34 -05001020static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001021 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001022 OPENSSL_free(hs->peer_sigalgs);
1023 hs->peer_sigalgs = NULL;
1024 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001025
Adam Langley2e857bd2015-07-01 16:09:19 -07001026 if (contents == NULL) {
1027 return 1;
1028 }
1029
1030 CBS supported_signature_algorithms;
1031 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001032 CBS_len(contents) != 0 ||
1033 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001034 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001035 return 0;
1036 }
1037
1038 return 1;
1039}
1040
Adam Langley2e857bd2015-07-01 16:09:19 -07001041
David Benjaminc11ea9422017-08-29 16:33:21 -04001042// OCSP Stapling.
1043//
1044// https://tools.ietf.org/html/rfc6066#section-8
Adam Langleybb0bd042015-07-01 16:21:03 -07001045
David Benjamin8c880a22016-12-03 02:20:34 -05001046static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1047 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001048 if (!ssl->ocsp_stapling_enabled) {
1049 return 1;
1050 }
1051
1052 CBB contents;
1053 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1054 !CBB_add_u16_length_prefixed(out, &contents) ||
1055 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1056 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1057 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1058 !CBB_flush(out)) {
1059 return 0;
1060 }
1061
1062 return 1;
1063}
1064
David Benjamin8c880a22016-12-03 02:20:34 -05001065static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001066 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001067 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001068 if (contents == NULL) {
1069 return 1;
1070 }
1071
David Benjaminc11ea9422017-08-29 16:33:21 -04001072 // TLS 1.3 OCSP responses are included in the Certificate extensions.
Steven Valdeza833c352016-11-01 13:39:36 -04001073 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001074 return 0;
1075 }
1076
David Benjaminc11ea9422017-08-29 16:33:21 -04001077 // OCSP stapling is forbidden on non-certificate ciphers.
Steven Valdeza833c352016-11-01 13:39:36 -04001078 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001079 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001080 return 0;
1081 }
1082
David Benjaminc11ea9422017-08-29 16:33:21 -04001083 // Note this does not check for resumption in TLS 1.2. Sending
1084 // status_request here does not make sense, but OpenSSL does so and the
1085 // specification does not say anything. Tolerate it but ignore it.
David Benjamin942f4ed2016-07-16 19:03:49 +03001086
David Benjaminfd45ee72017-08-31 14:49:09 -04001087 hs->certificate_status_expected = true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001088 return 1;
1089}
1090
David Benjamin8c880a22016-12-03 02:20:34 -05001091static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001092 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001093 if (contents == NULL) {
1094 return 1;
1095 }
1096
1097 uint8_t status_type;
1098 if (!CBS_get_u8(contents, &status_type)) {
1099 return 0;
1100 }
1101
David Benjaminc11ea9422017-08-29 16:33:21 -04001102 // We cannot decide whether OCSP stapling will occur yet because the correct
1103 // SSL_CTX might not have been selected.
David Benjamin8c880a22016-12-03 02:20:34 -05001104 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001105
Adam Langleybb0bd042015-07-01 16:21:03 -07001106 return 1;
1107}
1108
David Benjamin8c880a22016-12-03 02:20:34 -05001109static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1110 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001111 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001112 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001113 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001114 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001115 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001116 return 1;
1117 }
1118
David Benjaminfd45ee72017-08-31 14:49:09 -04001119 hs->certificate_status_expected = true;
David Benjamin942f4ed2016-07-16 19:03:49 +03001120
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001121 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001122 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001123}
1124
1125
David Benjaminc11ea9422017-08-29 16:33:21 -04001126// Next protocol negotiation.
1127//
1128// https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
Adam Langley97dfcbf2015-07-01 18:35:20 -07001129
David Benjamin8c880a22016-12-03 02:20:34 -05001130static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1131 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001132 if (ssl->s3->initial_handshake_complete ||
1133 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001134 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001135 return 1;
1136 }
1137
1138 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1139 !CBB_add_u16(out, 0 /* length */)) {
1140 return 0;
1141 }
1142
1143 return 1;
1144}
1145
David Benjamin8c880a22016-12-03 02:20:34 -05001146static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001147 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001148 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001149 if (contents == NULL) {
1150 return 1;
1151 }
1152
Steven Valdez143e8b32016-07-11 13:19:03 -04001153 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1154 return 0;
1155 }
1156
David Benjaminc11ea9422017-08-29 16:33:21 -04001157 // If any of these are false then we should never have sent the NPN
1158 // extension in the ClientHello and thus this function should never have been
1159 // called.
Adam Langley97dfcbf2015-07-01 18:35:20 -07001160 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001161 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001162 assert(ssl->ctx->next_proto_select_cb != NULL);
1163
David Benjamin76c2efc2015-08-31 14:24:29 -04001164 if (ssl->s3->alpn_selected != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001165 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001166 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1167 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1168 return 0;
1169 }
1170
Adam Langley97dfcbf2015-07-01 18:35:20 -07001171 const uint8_t *const orig_contents = CBS_data(contents);
1172 const size_t orig_len = CBS_len(contents);
1173
1174 while (CBS_len(contents) != 0) {
1175 CBS proto;
1176 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1177 CBS_len(&proto) == 0) {
1178 return 0;
1179 }
1180 }
1181
1182 uint8_t *selected;
1183 uint8_t selected_len;
1184 if (ssl->ctx->next_proto_select_cb(
1185 ssl, &selected, &selected_len, orig_contents, orig_len,
1186 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1187 *out_alert = SSL_AD_INTERNAL_ERROR;
1188 return 0;
1189 }
1190
David Benjamin79978df2015-12-25 15:56:49 -05001191 OPENSSL_free(ssl->s3->next_proto_negotiated);
David Benjamin81678aa2017-07-12 22:43:42 -04001192 ssl->s3->next_proto_negotiated =
1193 (uint8_t *)BUF_memdup(selected, selected_len);
David Benjamin79978df2015-12-25 15:56:49 -05001194 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001195 *out_alert = SSL_AD_INTERNAL_ERROR;
1196 return 0;
1197 }
1198
David Benjamin79978df2015-12-25 15:56:49 -05001199 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjaminfd45ee72017-08-31 14:49:09 -04001200 hs->next_proto_neg_seen = true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001201
1202 return 1;
1203}
1204
David Benjamin8c880a22016-12-03 02:20:34 -05001205static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001206 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001207 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001208 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1209 return 1;
1210 }
1211
Adam Langley97dfcbf2015-07-01 18:35:20 -07001212 if (contents != NULL && CBS_len(contents) != 0) {
1213 return 0;
1214 }
1215
1216 if (contents == NULL ||
1217 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001218 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001219 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001220 return 1;
1221 }
1222
David Benjaminfd45ee72017-08-31 14:49:09 -04001223 hs->next_proto_neg_seen = true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001224 return 1;
1225}
1226
David Benjamin8c880a22016-12-03 02:20:34 -05001227static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1228 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001229 // |next_proto_neg_seen| might have been cleared when an ALPN extension was
1230 // parsed.
David Benjamin8c880a22016-12-03 02:20:34 -05001231 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001232 return 1;
1233 }
1234
1235 const uint8_t *npa;
1236 unsigned npa_len;
1237
1238 if (ssl->ctx->next_protos_advertised_cb(
1239 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1240 SSL_TLSEXT_ERR_OK) {
David Benjaminfd45ee72017-08-31 14:49:09 -04001241 hs->next_proto_neg_seen = false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001242 return 1;
1243 }
1244
1245 CBB contents;
1246 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1247 !CBB_add_u16_length_prefixed(out, &contents) ||
1248 !CBB_add_bytes(&contents, npa, npa_len) ||
1249 !CBB_flush(out)) {
1250 return 0;
1251 }
1252
1253 return 1;
1254}
1255
1256
David Benjaminc11ea9422017-08-29 16:33:21 -04001257// Signed certificate timestamps.
1258//
1259// https://tools.ietf.org/html/rfc6962#section-3.3.1
Adam Langleyab8d87d2015-07-10 12:21:39 -07001260
David Benjamin8c880a22016-12-03 02:20:34 -05001261static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1262 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001263 if (!ssl->signed_cert_timestamps_enabled) {
1264 return 1;
1265 }
1266
1267 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1268 !CBB_add_u16(out, 0 /* length */)) {
1269 return 0;
1270 }
1271
1272 return 1;
1273}
1274
David Benjamin8c880a22016-12-03 02:20:34 -05001275static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001276 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001277 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001278 if (contents == NULL) {
1279 return 1;
1280 }
1281
David Benjaminc11ea9422017-08-29 16:33:21 -04001282 // TLS 1.3 SCTs are included in the Certificate extensions.
Steven Valdeza833c352016-11-01 13:39:36 -04001283 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001284 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001285 return 0;
1286 }
1287
David Benjaminc11ea9422017-08-29 16:33:21 -04001288 // If this is false then we should never have sent the SCT extension in the
1289 // ClientHello and thus this function should never have been called.
Adam Langleyab8d87d2015-07-10 12:21:39 -07001290 assert(ssl->signed_cert_timestamps_enabled);
1291
Adam Langleycfa08c32016-11-17 13:21:27 -08001292 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001293 *out_alert = SSL_AD_DECODE_ERROR;
1294 return 0;
1295 }
1296
David Benjaminc11ea9422017-08-29 16:33:21 -04001297 // Session resumption uses the original session information. The extension
1298 // should not be sent on resumption, but RFC 6962 did not make it a
1299 // requirement, so tolerate this.
1300 //
1301 // TODO(davidben): Enforce this anyway.
David Benjamin8fc2dc02017-08-22 15:07:51 -07001302 if (!ssl->s3->session_reused) {
1303 CRYPTO_BUFFER_free(hs->new_session->signed_cert_timestamp_list);
1304 hs->new_session->signed_cert_timestamp_list =
1305 CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool);
1306 if (hs->new_session->signed_cert_timestamp_list == nullptr) {
1307 *out_alert = SSL_AD_INTERNAL_ERROR;
1308 return 0;
1309 }
Adam Langleyab8d87d2015-07-10 12:21:39 -07001310 }
1311
1312 return 1;
1313}
1314
David Benjamin8c880a22016-12-03 02:20:34 -05001315static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001316 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001317 if (contents == NULL) {
1318 return 1;
1319 }
1320
1321 if (CBS_len(contents) != 0) {
1322 return 0;
1323 }
1324
David Benjaminfd45ee72017-08-31 14:49:09 -04001325 hs->scts_requested = true;
David Benjamin53210cb2016-11-16 09:01:48 +09001326 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001327}
1328
David Benjamin8c880a22016-12-03 02:20:34 -05001329static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1330 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001331 // The extension shouldn't be sent when resuming sessions.
Steven Valdeza833c352016-11-01 13:39:36 -04001332 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1333 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001334 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001335 return 1;
1336 }
1337
1338 CBB contents;
1339 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1340 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001341 CBB_add_bytes(
1342 &contents,
1343 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1344 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001345 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001346}
1347
1348
David Benjaminc11ea9422017-08-29 16:33:21 -04001349// Application-level Protocol Negotiation.
1350//
1351// https://tools.ietf.org/html/rfc7301
Adam Langleyf18e4532015-07-10 13:39:53 -07001352
David Benjamin8c880a22016-12-03 02:20:34 -05001353static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1354 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001355 if (ssl->alpn_client_proto_list == NULL ||
1356 ssl->s3->initial_handshake_complete) {
1357 return 1;
1358 }
1359
1360 CBB contents, proto_list;
1361 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1362 !CBB_add_u16_length_prefixed(out, &contents) ||
1363 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1364 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1365 ssl->alpn_client_proto_list_len) ||
1366 !CBB_flush(out)) {
1367 return 0;
1368 }
1369
1370 return 1;
1371}
1372
David Benjamin8c880a22016-12-03 02:20:34 -05001373static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001374 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001375 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001376 if (contents == NULL) {
1377 return 1;
1378 }
1379
1380 assert(!ssl->s3->initial_handshake_complete);
1381 assert(ssl->alpn_client_proto_list != NULL);
1382
David Benjamin8c880a22016-12-03 02:20:34 -05001383 if (hs->next_proto_neg_seen) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001384 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001385 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1386 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1387 return 0;
1388 }
1389
David Benjaminc11ea9422017-08-29 16:33:21 -04001390 // The extension data consists of a ProtocolNameList which must have
1391 // exactly one ProtocolName. Each of these is length-prefixed.
Adam Langleyf18e4532015-07-10 13:39:53 -07001392 CBS protocol_name_list, protocol_name;
1393 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1394 CBS_len(contents) != 0 ||
1395 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001396 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001397 CBS_len(&protocol_name) == 0 ||
1398 CBS_len(&protocol_name_list) != 0) {
1399 return 0;
1400 }
1401
David Benjaminc8ff30c2017-04-04 13:52:36 -04001402 if (!ssl->ctx->allow_unknown_alpn_protos) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001403 // Check that the protocol name is one of the ones we advertised.
David Benjaminc8ff30c2017-04-04 13:52:36 -04001404 int protocol_ok = 0;
1405 CBS client_protocol_name_list, client_protocol_name;
1406 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1407 ssl->alpn_client_proto_list_len);
1408 while (CBS_len(&client_protocol_name_list) > 0) {
1409 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1410 &client_protocol_name)) {
1411 *out_alert = SSL_AD_INTERNAL_ERROR;
1412 return 0;
1413 }
1414
1415 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1416 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1417 CBS_data(&protocol_name),
1418 CBS_len(&protocol_name)) == 0) {
1419 protocol_ok = 1;
1420 break;
1421 }
1422 }
1423
1424 if (!protocol_ok) {
1425 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1426 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3e517572016-08-11 11:52:23 -04001427 return 0;
1428 }
David Benjamin3e517572016-08-11 11:52:23 -04001429 }
1430
Adam Langleyf18e4532015-07-10 13:39:53 -07001431 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1432 &ssl->s3->alpn_selected_len)) {
1433 *out_alert = SSL_AD_INTERNAL_ERROR;
1434 return 0;
1435 }
1436
1437 return 1;
1438}
1439
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001440int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001441 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001442 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001443 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001444 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001445 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001446 client_hello, &contents,
1447 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001448 // Ignore ALPN if not configured or no extension was supplied.
Adam Langleyf18e4532015-07-10 13:39:53 -07001449 return 1;
1450 }
1451
David Benjaminc11ea9422017-08-29 16:33:21 -04001452 // ALPN takes precedence over NPN.
David Benjaminfd45ee72017-08-31 14:49:09 -04001453 hs->next_proto_neg_seen = false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001454
1455 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001456 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1457 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001458 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001459 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1460 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001461 return 0;
1462 }
1463
David Benjaminc11ea9422017-08-29 16:33:21 -04001464 // Validate the protocol list.
Adam Langleyf18e4532015-07-10 13:39:53 -07001465 CBS protocol_name_list_copy = protocol_name_list;
1466 while (CBS_len(&protocol_name_list_copy) > 0) {
1467 CBS protocol_name;
1468
1469 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001470 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001471 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001472 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1473 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001474 return 0;
1475 }
1476 }
1477
1478 const uint8_t *selected;
1479 uint8_t selected_len;
1480 if (ssl->ctx->alpn_select_cb(
1481 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1482 CBS_len(&protocol_name_list),
1483 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1484 OPENSSL_free(ssl->s3->alpn_selected);
David Benjamin81678aa2017-07-12 22:43:42 -04001485 ssl->s3->alpn_selected = (uint8_t *)BUF_memdup(selected, selected_len);
Adam Langleyf18e4532015-07-10 13:39:53 -07001486 if (ssl->s3->alpn_selected == NULL) {
1487 *out_alert = SSL_AD_INTERNAL_ERROR;
1488 return 0;
1489 }
1490 ssl->s3->alpn_selected_len = selected_len;
1491 }
1492
1493 return 1;
1494}
1495
David Benjamin8c880a22016-12-03 02:20:34 -05001496static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1497 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001498 if (ssl->s3->alpn_selected == NULL) {
1499 return 1;
1500 }
1501
1502 CBB contents, proto_list, proto;
1503 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1504 !CBB_add_u16_length_prefixed(out, &contents) ||
1505 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1506 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001507 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1508 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001509 !CBB_flush(out)) {
1510 return 0;
1511 }
1512
1513 return 1;
1514}
1515
1516
David Benjaminc11ea9422017-08-29 16:33:21 -04001517// Channel ID.
1518//
1519// https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
Adam Langley49c7af12015-07-10 14:33:46 -07001520
David Benjamin8c880a22016-12-03 02:20:34 -05001521static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
David Benjamin046bc1f2017-08-31 15:06:42 -04001522 hs->ssl->s3->tlsext_channel_id_valid = false;
Adam Langley49c7af12015-07-10 14:33:46 -07001523}
1524
David Benjamin8c880a22016-12-03 02:20:34 -05001525static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1526 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001527 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001528 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001529 return 1;
1530 }
1531
1532 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1533 !CBB_add_u16(out, 0 /* length */)) {
1534 return 0;
1535 }
1536
1537 return 1;
1538}
1539
David Benjamin8c880a22016-12-03 02:20:34 -05001540static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1541 uint8_t *out_alert, CBS *contents) {
1542 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001543 if (contents == NULL) {
1544 return 1;
1545 }
1546
David Benjamince079fd2016-08-02 16:22:34 -04001547 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001548 assert(ssl->tlsext_channel_id_enabled);
1549
1550 if (CBS_len(contents) != 0) {
1551 return 0;
1552 }
1553
David Benjamin046bc1f2017-08-31 15:06:42 -04001554 ssl->s3->tlsext_channel_id_valid = true;
Adam Langley49c7af12015-07-10 14:33:46 -07001555 return 1;
1556}
1557
David Benjamin8c880a22016-12-03 02:20:34 -05001558static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1559 uint8_t *out_alert, CBS *contents) {
1560 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001561 if (contents == NULL ||
1562 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001563 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001564 return 1;
1565 }
1566
1567 if (CBS_len(contents) != 0) {
1568 return 0;
1569 }
1570
David Benjamin046bc1f2017-08-31 15:06:42 -04001571 ssl->s3->tlsext_channel_id_valid = true;
Adam Langley49c7af12015-07-10 14:33:46 -07001572 return 1;
1573}
1574
David Benjamin8c880a22016-12-03 02:20:34 -05001575static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1576 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001577 if (!ssl->s3->tlsext_channel_id_valid) {
1578 return 1;
1579 }
1580
1581 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1582 !CBB_add_u16(out, 0 /* length */)) {
1583 return 0;
1584 }
1585
1586 return 1;
1587}
1588
Adam Langley391250d2015-07-15 19:06:07 -07001589
David Benjaminc11ea9422017-08-29 16:33:21 -04001590// Secure Real-time Transport Protocol (SRTP) extension.
1591//
1592// https://tools.ietf.org/html/rfc5764
Adam Langley391250d2015-07-15 19:06:07 -07001593
Adam Langley391250d2015-07-15 19:06:07 -07001594
David Benjamin8c880a22016-12-03 02:20:34 -05001595static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1596 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001597}
1598
David Benjamin8c880a22016-12-03 02:20:34 -05001599static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1600 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001601 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
David Benjaminee910bf2017-07-25 22:36:00 -04001602 if (profiles == NULL ||
1603 sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
Adam Langley391250d2015-07-15 19:06:07 -07001604 return 1;
1605 }
1606
1607 CBB contents, profile_ids;
1608 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1609 !CBB_add_u16_length_prefixed(out, &contents) ||
1610 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1611 return 0;
1612 }
1613
David Benjaminee910bf2017-07-25 22:36:00 -04001614 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
1615 if (!CBB_add_u16(&profile_ids, profile->id)) {
Adam Langley391250d2015-07-15 19:06:07 -07001616 return 0;
1617 }
1618 }
1619
1620 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1621 !CBB_flush(out)) {
1622 return 0;
1623 }
1624
1625 return 1;
1626}
1627
David Benjamin8c880a22016-12-03 02:20:34 -05001628static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001629 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001630 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001631 if (contents == NULL) {
1632 return 1;
1633 }
1634
David Benjaminc11ea9422017-08-29 16:33:21 -04001635 // The extension consists of a u16-prefixed profile ID list containing a
1636 // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1637 //
1638 // See https://tools.ietf.org/html/rfc5764#section-4.1.1
Adam Langley391250d2015-07-15 19:06:07 -07001639 CBS profile_ids, srtp_mki;
1640 uint16_t profile_id;
1641 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1642 !CBS_get_u16(&profile_ids, &profile_id) ||
1643 CBS_len(&profile_ids) != 0 ||
1644 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1645 CBS_len(contents) != 0) {
1646 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1647 return 0;
1648 }
1649
1650 if (CBS_len(&srtp_mki) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001651 // Must be no MKI, since we never offer one.
Adam Langley391250d2015-07-15 19:06:07 -07001652 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1653 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1654 return 0;
1655 }
1656
1657 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1658
David Benjaminc11ea9422017-08-29 16:33:21 -04001659 // Check to see if the server gave us something we support (and presumably
1660 // offered).
David Benjaminee910bf2017-07-25 22:36:00 -04001661 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001662 if (profile->id == profile_id) {
1663 ssl->srtp_profile = profile;
1664 return 1;
1665 }
1666 }
1667
1668 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1669 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1670 return 0;
1671}
1672
David Benjamin8c880a22016-12-03 02:20:34 -05001673static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001674 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001675 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001676 if (contents == NULL) {
1677 return 1;
1678 }
1679
1680 CBS profile_ids, srtp_mki;
1681 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1682 CBS_len(&profile_ids) < 2 ||
1683 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1684 CBS_len(contents) != 0) {
1685 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1686 return 0;
1687 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001688 // Discard the MKI value for now.
Adam Langley391250d2015-07-15 19:06:07 -07001689
1690 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1691 SSL_get_srtp_profiles(ssl);
1692
David Benjaminc11ea9422017-08-29 16:33:21 -04001693 // Pick the server's most preferred profile.
David Benjaminee910bf2017-07-25 22:36:00 -04001694 for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001695 CBS profile_ids_tmp;
1696 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1697
1698 while (CBS_len(&profile_ids_tmp) > 0) {
1699 uint16_t profile_id;
1700 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1701 return 0;
1702 }
1703
1704 if (server_profile->id == profile_id) {
1705 ssl->srtp_profile = server_profile;
1706 return 1;
1707 }
1708 }
1709 }
1710
1711 return 1;
1712}
1713
David Benjamin8c880a22016-12-03 02:20:34 -05001714static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1715 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001716 if (ssl->srtp_profile == NULL) {
1717 return 1;
1718 }
1719
1720 CBB contents, profile_ids;
1721 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1722 !CBB_add_u16_length_prefixed(out, &contents) ||
1723 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1724 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1725 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1726 !CBB_flush(out)) {
1727 return 0;
1728 }
1729
1730 return 1;
1731}
1732
Adam Langleybdd5d662015-07-20 16:19:08 -07001733
David Benjaminc11ea9422017-08-29 16:33:21 -04001734// EC point formats.
1735//
1736// https://tools.ietf.org/html/rfc4492#section-5.1.2
Adam Langleybdd5d662015-07-20 16:19:08 -07001737
David Benjamin8c880a22016-12-03 02:20:34 -05001738static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001739 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001740 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1741 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001742 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1743 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001744 !CBB_flush(out)) {
1745 return 0;
1746 }
1747
1748 return 1;
1749}
1750
David Benjamin8c880a22016-12-03 02:20:34 -05001751static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001752 // The point format extension is unneccessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -04001753 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin70aba262016-11-01 12:08:15 -04001754 return 1;
1755 }
1756
David Benjamin8c880a22016-12-03 02:20:34 -05001757 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001758}
1759
David Benjamin8c880a22016-12-03 02:20:34 -05001760static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001761 CBS *contents) {
1762 if (contents == NULL) {
1763 return 1;
1764 }
1765
David Benjamin8c880a22016-12-03 02:20:34 -05001766 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001767 return 0;
1768 }
1769
Adam Langleybdd5d662015-07-20 16:19:08 -07001770 CBS ec_point_format_list;
1771 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1772 CBS_len(contents) != 0) {
1773 return 0;
1774 }
1775
David Benjaminc11ea9422017-08-29 16:33:21 -04001776 // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1777 // point format.
David Benjamin17cf2cb2016-12-13 01:07:13 -05001778 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1779 TLSEXT_ECPOINTFORMAT_uncompressed,
1780 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001781 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001782 return 0;
1783 }
1784
1785 return 1;
1786}
1787
David Benjamin8c880a22016-12-03 02:20:34 -05001788static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001789 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001790 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001791 return 1;
1792 }
1793
David Benjamin8c880a22016-12-03 02:20:34 -05001794 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001795}
1796
David Benjamin8c880a22016-12-03 02:20:34 -05001797static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1798 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001799 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1800 return 1;
1801 }
1802
David Benjamin45738dd2017-02-09 20:01:26 -05001803 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1804 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001805 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001806
1807 if (!using_ecc) {
1808 return 1;
1809 }
1810
David Benjamin8c880a22016-12-03 02:20:34 -05001811 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001812}
1813
Steven Valdeza833c352016-11-01 13:39:36 -04001814
David Benjaminc11ea9422017-08-29 16:33:21 -04001815// Pre Shared Key
1816//
1817// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
Steven Valdez4aa154e2016-07-29 14:32:55 -04001818
David Benjamin8c880a22016-12-03 02:20:34 -05001819static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1820 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001821 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001822 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001823 return 0;
1824 }
1825
Steven Valdez8f36c512017-06-20 10:55:02 -04001826 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001827 return 15 + ssl->session->tlsext_ticklen + binder_len;
1828}
1829
David Benjamin8c880a22016-12-03 02:20:34 -05001830static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1831 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001832 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001833 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001834 return 1;
1835 }
1836
David Benjaminad8f5e12017-02-20 17:00:20 -05001837 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001838 ssl_get_current_time(ssl, &now);
1839 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1840 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1841
David Benjaminc11ea9422017-08-29 16:33:21 -04001842 // Fill in a placeholder zero binder of the appropriate length. It will be
1843 // computed and filled in later after length prefixes are computed.
Steven Valdeza833c352016-11-01 13:39:36 -04001844 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez8f36c512017-06-20 10:55:02 -04001845 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001846
1847 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001848 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1849 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001850 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001851 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1852 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001853 ssl->session->tlsext_ticklen) ||
1854 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1855 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1856 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1857 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001858 return 0;
1859 }
1860
David Benjaminfd45ee72017-08-31 14:49:09 -04001861 hs->needs_psk_binder = true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001862 return CBB_flush(out);
1863}
1864
David Benjamin8baf9632016-11-17 17:11:16 +09001865int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1866 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001867 CBS *contents) {
1868 uint16_t psk_id;
1869 if (!CBS_get_u16(contents, &psk_id) ||
1870 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001871 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001872 *out_alert = SSL_AD_DECODE_ERROR;
1873 return 0;
1874 }
1875
David Benjaminc11ea9422017-08-29 16:33:21 -04001876 // We only advertise one PSK identity, so the only legal index is zero.
Steven Valdez4aa154e2016-07-29 14:32:55 -04001877 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001878 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001879 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1880 return 0;
1881 }
1882
1883 return 1;
1884}
1885
David Benjamin35ac5b72017-03-03 15:05:56 -05001886int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001887 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001888 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001889 // We only process the first PSK identity since we don't support pure PSK.
David Benjamin707af292017-03-10 17:47:18 -05001890 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001891 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001892 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001893 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001894 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001895 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001896 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001897 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001898 *out_alert = SSL_AD_DECODE_ERROR;
1899 return 0;
1900 }
1901
Steven Valdeza833c352016-11-01 13:39:36 -04001902 *out_binders = binders;
1903
David Benjaminc11ea9422017-08-29 16:33:21 -04001904 // Check the syntax of the remaining identities, but do not process them.
David Benjaminaedf3032016-12-01 16:47:56 -05001905 size_t num_identities = 1;
1906 while (CBS_len(&identities) != 0) {
1907 CBS unused_ticket;
1908 uint32_t unused_obfuscated_ticket_age;
1909 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1910 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1911 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1912 *out_alert = SSL_AD_DECODE_ERROR;
1913 return 0;
1914 }
1915
1916 num_identities++;
1917 }
1918
David Benjaminc11ea9422017-08-29 16:33:21 -04001919 // Check the syntax of the binders. The value will be checked later if
1920 // resuming.
David Benjaminaedf3032016-12-01 16:47:56 -05001921 size_t num_binders = 0;
1922 while (CBS_len(&binders) != 0) {
1923 CBS binder;
1924 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1925 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1926 *out_alert = SSL_AD_DECODE_ERROR;
1927 return 0;
1928 }
1929
1930 num_binders++;
1931 }
1932
1933 if (num_identities != num_binders) {
1934 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1935 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04001936 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04001937 }
1938
David Benjamine7f60a22016-11-16 18:54:25 +09001939 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001940}
1941
David Benjamin8baf9632016-11-17 17:11:16 +09001942int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1943 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001944 return 1;
1945 }
1946
1947 CBB contents;
1948 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1949 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001950 // We only consider the first identity for resumption
Steven Valdez4aa154e2016-07-29 14:32:55 -04001951 !CBB_add_u16(&contents, 0) ||
1952 !CBB_flush(out)) {
1953 return 0;
1954 }
1955
1956 return 1;
1957}
1958
1959
David Benjaminc11ea9422017-08-29 16:33:21 -04001960// Pre-Shared Key Exchange Modes
1961//
1962// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7
Steven Valdeza4ee74d2016-11-29 13:36:45 -05001963
David Benjamin8c880a22016-12-03 02:20:34 -05001964static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
1965 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04001966 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001967 return 1;
1968 }
1969
1970 CBB contents, ke_modes;
1971 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
1972 !CBB_add_u16_length_prefixed(out, &contents) ||
1973 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
1974 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
1975 return 0;
1976 }
1977
1978 return CBB_flush(out);
1979}
1980
David Benjamin8c880a22016-12-03 02:20:34 -05001981static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09001982 uint8_t *out_alert,
1983 CBS *contents) {
1984 if (contents == NULL) {
1985 return 1;
1986 }
1987
Steven Valdeza833c352016-11-01 13:39:36 -04001988 CBS ke_modes;
1989 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
1990 CBS_len(&ke_modes) == 0 ||
1991 CBS_len(contents) != 0) {
1992 *out_alert = SSL_AD_DECODE_ERROR;
1993 return 0;
1994 }
1995
David Benjaminc11ea9422017-08-29 16:33:21 -04001996 // We only support tickets with PSK_DHE_KE.
David Benjamin17cf2cb2016-12-13 01:07:13 -05001997 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
1998 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04001999
2000 return 1;
2001}
2002
2003
David Benjaminc11ea9422017-08-29 16:33:21 -04002004// Early Data Indication
2005//
2006// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002007
David Benjamin8c880a22016-12-03 02:20:34 -05002008static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002009 SSL *const ssl = hs->ssl;
Steven Valdez2d850622017-01-11 11:34:52 -05002010 if (ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04002011 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION ||
Steven Valdez2d850622017-01-11 11:34:52 -05002012 ssl->session->ticket_max_early_data == 0 ||
2013 hs->received_hello_retry_request ||
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05002014 !ssl->cert->enable_early_data) {
Steven Valdez2d850622017-01-11 11:34:52 -05002015 return 1;
2016 }
2017
David Benjaminfd45ee72017-08-31 14:49:09 -04002018 hs->early_data_offered = true;
Steven Valdez2d850622017-01-11 11:34:52 -05002019
2020 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2021 !CBB_add_u16(out, 0) ||
2022 !CBB_flush(out)) {
2023 return 0;
2024 }
2025
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002026 return 1;
2027}
2028
Steven Valdez2d850622017-01-11 11:34:52 -05002029static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002030 uint8_t *out_alert, CBS *contents) {
2031 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002032 if (contents == NULL) {
2033 return 1;
2034 }
2035
2036 if (CBS_len(contents) != 0) {
2037 *out_alert = SSL_AD_DECODE_ERROR;
2038 return 0;
2039 }
2040
Steven Valdez2d850622017-01-11 11:34:52 -05002041 if (!ssl->s3->session_reused) {
2042 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2043 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2044 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002045 }
Steven Valdez2d850622017-01-11 11:34:52 -05002046
2047 ssl->early_data_accepted = 1;
2048 return 1;
2049}
2050
2051static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2052 uint8_t *out_alert, CBS *contents) {
2053 SSL *const ssl = hs->ssl;
2054 if (contents == NULL ||
2055 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2056 return 1;
2057 }
2058
2059 if (CBS_len(contents) != 0) {
2060 *out_alert = SSL_AD_DECODE_ERROR;
2061 return 0;
2062 }
2063
David Benjaminfd45ee72017-08-31 14:49:09 -04002064 hs->early_data_offered = true;
Steven Valdez2d850622017-01-11 11:34:52 -05002065 return 1;
2066}
2067
2068static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2069 if (!hs->ssl->early_data_accepted) {
2070 return 1;
2071 }
2072
2073 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2074 !CBB_add_u16(out, 0) ||
2075 !CBB_flush(out)) {
2076 return 0;
2077 }
2078
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002079 return 1;
2080}
2081
2082
David Benjaminc11ea9422017-08-29 16:33:21 -04002083// Key Share
2084//
2085// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5
Steven Valdez143e8b32016-07-11 13:19:03 -04002086
David Benjamin8c880a22016-12-03 02:20:34 -05002087static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2088 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002089 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002090 return 1;
2091 }
2092
2093 CBB contents, kse_bytes;
2094 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2095 !CBB_add_u16_length_prefixed(out, &contents) ||
2096 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2097 return 0;
2098 }
2099
David Benjamin8c880a22016-12-03 02:20:34 -05002100 uint16_t group_id = hs->retry_group;
2101 if (hs->received_hello_retry_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002102 // We received a HelloRetryRequest without a new curve, so there is no new
2103 // share to append. Leave |hs->key_share| as-is.
Steven Valdeza833c352016-11-01 13:39:36 -04002104 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002105 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2106 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002107 return 0;
2108 }
David Benjamin8c880a22016-12-03 02:20:34 -05002109 OPENSSL_free(hs->key_share_bytes);
2110 hs->key_share_bytes = NULL;
2111 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002112 if (group_id == 0) {
2113 return CBB_flush(out);
2114 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002115 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04002116 // Add a fake group. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002117 if (ssl->ctx->grease_enabled &&
2118 (!CBB_add_u16(&kse_bytes,
2119 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2120 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2121 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2122 return 0;
2123 }
2124
David Benjaminc11ea9422017-08-29 16:33:21 -04002125 // Predict the most preferred group.
David Benjamincf0ce672017-09-21 02:25:59 -04002126 Span<const uint16_t> groups = tls1_get_grouplist(ssl);
2127 if (groups.size() == 0) {
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002128 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2129 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002130 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002131
2132 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002133 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002134
David Benjaminc642aca2017-07-19 23:28:43 -04002135 hs->key_share = SSLKeyShare::Create(group_id);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002136 CBB key_exchange;
David Benjaminc642aca2017-07-19 23:28:43 -04002137 if (!hs->key_share ||
2138 !CBB_add_u16(&kse_bytes, group_id) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002139 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjaminc642aca2017-07-19 23:28:43 -04002140 !hs->key_share->Offer(&key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002141 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002142 return 0;
2143 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002144
David Benjamin8c880a22016-12-03 02:20:34 -05002145 if (!hs->received_hello_retry_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002146 // Save the contents of the extension to repeat it in the second
2147 // ClientHello.
David Benjamin8c880a22016-12-03 02:20:34 -05002148 hs->key_share_bytes_len = CBB_len(&kse_bytes);
David Benjamin81678aa2017-07-12 22:43:42 -04002149 hs->key_share_bytes =
2150 (uint8_t *)BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
David Benjamin8c880a22016-12-03 02:20:34 -05002151 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002152 return 0;
2153 }
2154 }
2155
Steven Valdez143e8b32016-07-11 13:19:03 -04002156 return CBB_flush(out);
2157}
2158
David Benjamin499742c2017-07-22 12:45:38 -04002159int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
2160 Array<uint8_t> *out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002161 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002162 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002163 uint16_t group_id;
2164 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002165 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2166 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002167 *out_alert = SSL_AD_DECODE_ERROR;
2168 return 0;
2169 }
2170
David Benjaminc642aca2017-07-19 23:28:43 -04002171 if (hs->key_share->GroupID() != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002172 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2173 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2174 return 0;
2175 }
2176
David Benjamin499742c2017-07-22 12:45:38 -04002177 if (!hs->key_share->Finish(
2178 out_secret, out_alert,
2179 MakeConstSpan(CBS_data(&peer_key), CBS_len(&peer_key)))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002180 *out_alert = SSL_AD_INTERNAL_ERROR;
2181 return 0;
2182 }
2183
David Benjamin45738dd2017-02-09 20:01:26 -05002184 hs->new_session->group_id = group_id;
David Benjaminc642aca2017-07-19 23:28:43 -04002185 hs->key_share.reset();
Steven Valdez143e8b32016-07-11 13:19:03 -04002186 return 1;
2187}
2188
David Benjamin74795b32017-08-31 15:13:12 -04002189int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
David Benjamin499742c2017-07-22 12:45:38 -04002190 Array<uint8_t> *out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002191 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002192 uint16_t group_id;
2193 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002194 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002195 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2196 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2197 return 0;
2198 }
2199
2200 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002201 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002202 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002203 return 0;
2204 }
2205
David Benjaminc11ea9422017-08-29 16:33:21 -04002206 // Find the corresponding key share.
David Benjamin74795b32017-08-31 15:13:12 -04002207 bool found = false;
David Benjamin7e1f9842016-09-20 19:24:40 -04002208 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002209 while (CBS_len(&key_shares) > 0) {
2210 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002211 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002212 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002213 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2214 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002215 return 0;
2216 }
2217
David Benjamin7e1f9842016-09-20 19:24:40 -04002218 if (id == group_id) {
2219 if (found) {
2220 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2221 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2222 return 0;
2223 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002224
David Benjamin74795b32017-08-31 15:13:12 -04002225 found = true;
David Benjamin7e1f9842016-09-20 19:24:40 -04002226 peer_key = peer_key_tmp;
David Benjaminc11ea9422017-08-29 16:33:21 -04002227 // Continue parsing the structure to keep peers honest.
Steven Valdez143e8b32016-07-11 13:19:03 -04002228 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002229 }
2230
David Benjamin7e1f9842016-09-20 19:24:40 -04002231 if (!found) {
David Benjamin74795b32017-08-31 15:13:12 -04002232 *out_found = false;
David Benjamin499742c2017-07-22 12:45:38 -04002233 out_secret->Reset();
David Benjamin7e1f9842016-09-20 19:24:40 -04002234 return 1;
2235 }
2236
David Benjaminc11ea9422017-08-29 16:33:21 -04002237 // Compute the DH secret.
David Benjamin499742c2017-07-22 12:45:38 -04002238 Array<uint8_t> secret;
David Benjamin1386aad2017-07-19 23:57:40 -04002239 ScopedCBB public_key;
David Benjaminc642aca2017-07-19 23:28:43 -04002240 UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
David Benjamin499742c2017-07-22 12:45:38 -04002241 if (!key_share || !CBB_init(public_key.get(), 32) ||
2242 !key_share->Accept(
2243 public_key.get(), &secret, out_alert,
2244 MakeConstSpan(CBS_data(&peer_key), CBS_len(&peer_key))) ||
David Benjamin1386aad2017-07-19 23:57:40 -04002245 !CBB_finish(public_key.get(), &hs->ecdh_public_key,
David Benjaminbf833c32017-03-30 15:45:21 -05002246 &hs->ecdh_public_key_len)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002247 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002248 return 0;
2249 }
2250
David Benjamin499742c2017-07-22 12:45:38 -04002251 *out_secret = std::move(secret);
David Benjamin74795b32017-08-31 15:13:12 -04002252 *out_found = true;
Steven Valdez5440fe02016-07-18 12:40:30 -04002253 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002254}
2255
David Benjamin8baf9632016-11-17 17:11:16 +09002256int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002257 uint16_t group_id;
2258 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002259 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002260 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2261 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2262 !CBB_add_u16(&kse_bytes, group_id) ||
2263 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjaminbf833c32017-03-30 15:45:21 -05002264 !CBB_add_bytes(&public_key, hs->ecdh_public_key,
2265 hs->ecdh_public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002266 !CBB_flush(out)) {
2267 return 0;
2268 }
2269
David Benjaminbf833c32017-03-30 15:45:21 -05002270 OPENSSL_free(hs->ecdh_public_key);
2271 hs->ecdh_public_key = NULL;
2272 hs->ecdh_public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002273
David Benjamin45738dd2017-02-09 20:01:26 -05002274 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002275 return 1;
2276}
2277
2278
David Benjaminc11ea9422017-08-29 16:33:21 -04002279// Supported Versions
2280//
2281// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1
Steven Valdezfdd10992016-09-15 16:27:05 -04002282
David Benjamin8c880a22016-12-03 02:20:34 -05002283static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2284 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002285 if (hs->max_version <= TLS1_2_VERSION) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002286 return 1;
2287 }
2288
2289 CBB contents, versions;
2290 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2291 !CBB_add_u16_length_prefixed(out, &contents) ||
2292 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2293 return 0;
2294 }
2295
David Benjaminc11ea9422017-08-29 16:33:21 -04002296 // Add a fake version. See draft-davidben-tls-grease-01.
David Benjamind9791bf2016-09-27 16:39:52 -04002297 if (ssl->ctx->grease_enabled &&
2298 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2299 return 0;
2300 }
2301
Steven Valdez8f36c512017-06-20 10:55:02 -04002302 if (!ssl_add_supported_versions(hs, &versions) ||
2303 !CBB_flush(out)) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002304 return 0;
2305 }
2306
2307 return 1;
2308}
2309
2310
David Benjaminc11ea9422017-08-29 16:33:21 -04002311// Cookie
2312//
2313// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2
David Benjamin3baa6e12016-10-07 21:10:38 -04002314
David Benjamin8c880a22016-12-03 02:20:34 -05002315static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2316 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002317 return 1;
2318 }
2319
2320 CBB contents, cookie;
2321 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2322 !CBB_add_u16_length_prefixed(out, &contents) ||
2323 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002324 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002325 !CBB_flush(out)) {
2326 return 0;
2327 }
2328
David Benjaminc11ea9422017-08-29 16:33:21 -04002329 // The cookie is no longer needed in memory.
David Benjamin8c880a22016-12-03 02:20:34 -05002330 OPENSSL_free(hs->cookie);
2331 hs->cookie = NULL;
2332 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002333 return 1;
2334}
2335
2336
David Benjaminc11ea9422017-08-29 16:33:21 -04002337// Negotiated Groups
2338//
2339// https://tools.ietf.org/html/rfc4492#section-5.1.2
2340// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4
Adam Langley273d49c2015-07-20 16:38:52 -07002341
David Benjamin8c880a22016-12-03 02:20:34 -05002342static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2343 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002344 CBB contents, groups_bytes;
2345 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002346 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002347 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002348 return 0;
2349 }
2350
David Benjaminc11ea9422017-08-29 16:33:21 -04002351 // Add a fake group. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002352 if (ssl->ctx->grease_enabled &&
2353 !CBB_add_u16(&groups_bytes,
2354 ssl_get_grease_value(ssl, ssl_grease_group))) {
2355 return 0;
2356 }
2357
David Benjamincf0ce672017-09-21 02:25:59 -04002358 for (uint16_t group : tls1_get_grouplist(ssl)) {
2359 if (!CBB_add_u16(&groups_bytes, group)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002360 return 0;
2361 }
2362 }
2363
2364 return CBB_flush(out);
2365}
2366
David Benjamin8c880a22016-12-03 02:20:34 -05002367static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2368 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002369 CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002370 // This extension is not expected to be echoed by servers in TLS 1.2, but some
2371 // BigIP servers send it nonetheless, so do not enforce this.
Adam Langley273d49c2015-07-20 16:38:52 -07002372 return 1;
2373}
2374
David Benjamin8c880a22016-12-03 02:20:34 -05002375static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2376 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002377 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002378 if (contents == NULL) {
2379 return 1;
2380 }
2381
Steven Valdezce902a92016-05-17 11:47:53 -04002382 CBS supported_group_list;
2383 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2384 CBS_len(&supported_group_list) == 0 ||
2385 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002386 CBS_len(contents) != 0) {
2387 return 0;
2388 }
2389
David Benjamincf0ce672017-09-21 02:25:59 -04002390 Array<uint16_t> groups;
2391 if (!groups.Init(CBS_len(&supported_group_list) / 2)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002392 return 0;
2393 }
David Benjamincf0ce672017-09-21 02:25:59 -04002394 for (size_t i = 0; i < groups.size(); i++) {
2395 if (!CBS_get_u16(&supported_group_list, &groups[i])) {
2396 *out_alert = SSL_AD_INTERNAL_ERROR;
2397 return 0;
Adam Langley273d49c2015-07-20 16:38:52 -07002398 }
2399 }
2400
Steven Valdezce902a92016-05-17 11:47:53 -04002401 assert(CBS_len(&supported_group_list) == 0);
David Benjamincf0ce672017-09-21 02:25:59 -04002402 hs->peer_supported_group_list = std::move(groups);
Adam Langley273d49c2015-07-20 16:38:52 -07002403 return 1;
Adam Langley273d49c2015-07-20 16:38:52 -07002404}
2405
David Benjamin8c880a22016-12-03 02:20:34 -05002406static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002407 // Servers don't echo this extension.
Adam Langley273d49c2015-07-20 16:38:52 -07002408 return 1;
2409}
2410
2411
David Benjaminc11ea9422017-08-29 16:33:21 -04002412// kExtensions contains all the supported extensions.
Adam Langley614c66a2015-06-12 15:26:58 -07002413static const struct tls_extension kExtensions[] = {
2414 {
Adam Langley5021b222015-06-12 18:27:58 -07002415 TLSEXT_TYPE_renegotiate,
2416 NULL,
2417 ext_ri_add_clienthello,
2418 ext_ri_parse_serverhello,
2419 ext_ri_parse_clienthello,
2420 ext_ri_add_serverhello,
2421 },
2422 {
Adam Langley614c66a2015-06-12 15:26:58 -07002423 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002424 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002425 ext_sni_add_clienthello,
2426 ext_sni_parse_serverhello,
2427 ext_sni_parse_clienthello,
2428 ext_sni_add_serverhello,
2429 },
Adam Langley0a056712015-07-01 15:03:33 -07002430 {
2431 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002432 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002433 ext_ems_add_clienthello,
2434 ext_ems_parse_serverhello,
2435 ext_ems_parse_clienthello,
2436 ext_ems_add_serverhello,
2437 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002438 {
2439 TLSEXT_TYPE_session_ticket,
2440 NULL,
2441 ext_ticket_add_clienthello,
2442 ext_ticket_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04002443 // Ticket extension client parsing is handled in ssl_session.c
Steven Valdez6b8509a2016-07-12 13:38:32 -04002444 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002445 ext_ticket_add_serverhello,
2446 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002447 {
2448 TLSEXT_TYPE_signature_algorithms,
2449 NULL,
2450 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002451 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002452 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002453 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002454 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002455 {
2456 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002457 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002458 ext_ocsp_add_clienthello,
2459 ext_ocsp_parse_serverhello,
2460 ext_ocsp_parse_clienthello,
2461 ext_ocsp_add_serverhello,
2462 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002463 {
2464 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002465 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002466 ext_npn_add_clienthello,
2467 ext_npn_parse_serverhello,
2468 ext_npn_parse_clienthello,
2469 ext_npn_add_serverhello,
2470 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002471 {
2472 TLSEXT_TYPE_certificate_timestamp,
2473 NULL,
2474 ext_sct_add_clienthello,
2475 ext_sct_parse_serverhello,
2476 ext_sct_parse_clienthello,
2477 ext_sct_add_serverhello,
2478 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002479 {
2480 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002481 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002482 ext_alpn_add_clienthello,
2483 ext_alpn_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04002484 // ALPN is negotiated late in |ssl_negotiate_alpn|.
David Benjamin9ef31f02016-10-31 18:01:13 -04002485 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002486 ext_alpn_add_serverhello,
2487 },
Adam Langley49c7af12015-07-10 14:33:46 -07002488 {
2489 TLSEXT_TYPE_channel_id,
2490 ext_channel_id_init,
2491 ext_channel_id_add_clienthello,
2492 ext_channel_id_parse_serverhello,
2493 ext_channel_id_parse_clienthello,
2494 ext_channel_id_add_serverhello,
2495 },
Adam Langley391250d2015-07-15 19:06:07 -07002496 {
2497 TLSEXT_TYPE_srtp,
2498 ext_srtp_init,
2499 ext_srtp_add_clienthello,
2500 ext_srtp_parse_serverhello,
2501 ext_srtp_parse_clienthello,
2502 ext_srtp_add_serverhello,
2503 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002504 {
2505 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002506 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002507 ext_ec_point_add_clienthello,
2508 ext_ec_point_parse_serverhello,
2509 ext_ec_point_parse_clienthello,
2510 ext_ec_point_add_serverhello,
2511 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002512 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002513 TLSEXT_TYPE_key_share,
2514 NULL,
2515 ext_key_share_add_clienthello,
2516 forbid_parse_serverhello,
2517 ignore_parse_clienthello,
2518 dont_add_serverhello,
2519 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002520 {
Steven Valdeza833c352016-11-01 13:39:36 -04002521 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002522 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002523 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002524 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002525 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002526 dont_add_serverhello,
2527 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002528 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002529 TLSEXT_TYPE_early_data,
2530 NULL,
2531 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002532 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002533 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002534 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002535 },
2536 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002537 TLSEXT_TYPE_supported_versions,
2538 NULL,
2539 ext_supported_versions_add_clienthello,
2540 forbid_parse_serverhello,
2541 ignore_parse_clienthello,
2542 dont_add_serverhello,
2543 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002544 {
2545 TLSEXT_TYPE_cookie,
2546 NULL,
2547 ext_cookie_add_clienthello,
2548 forbid_parse_serverhello,
2549 ignore_parse_clienthello,
2550 dont_add_serverhello,
2551 },
David Benjaminc11ea9422017-08-29 16:33:21 -04002552 // The final extension must be non-empty. WebSphere Application Server 7.0 is
2553 // intolerant to the last extension being zero-length. See
2554 // https://crbug.com/363583.
Adam Langley273d49c2015-07-20 16:38:52 -07002555 {
Steven Valdezce902a92016-05-17 11:47:53 -04002556 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002557 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002558 ext_supported_groups_add_clienthello,
2559 ext_supported_groups_parse_serverhello,
2560 ext_supported_groups_parse_clienthello,
2561 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002562 },
Adam Langley614c66a2015-06-12 15:26:58 -07002563};
2564
2565#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2566
David Benjamina3d76d02017-07-14 19:36:07 -04002567static_assert(kNumExtensions <=
2568 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
2569 "too many extensions for sent bitset");
2570static_assert(kNumExtensions <=
2571 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2572 "too many extensions for received bitset");
Adam Langley4cfa96b2015-07-01 11:56:55 -07002573
Adam Langley614c66a2015-06-12 15:26:58 -07002574static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2575 uint16_t value) {
2576 unsigned i;
2577 for (i = 0; i < kNumExtensions; i++) {
2578 if (kExtensions[i].value == value) {
2579 *out_index = i;
2580 return &kExtensions[i];
2581 }
2582 }
2583
2584 return NULL;
2585}
2586
David Benjamin8c880a22016-12-03 02:20:34 -05002587int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2588 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04002589 // Don't add extensions for SSLv3 unless doing secure renegotiation.
David Benjaminf04c2e92016-12-06 13:35:25 -05002590 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002591 !ssl->s3->send_connection_binding) {
2592 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002593 }
Adam Langley95c29f32014-06-20 12:00:00 -07002594
David Benjamine8d53502015-10-10 14:13:23 -04002595 CBB extensions;
2596 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002597 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2598 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002599 }
Adam Langley95c29f32014-06-20 12:00:00 -07002600
David Benjamin8c880a22016-12-03 02:20:34 -05002601 hs->extensions.sent = 0;
2602 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002603
David Benjamin54091232016-09-05 12:47:25 -04002604 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002605 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002606 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002607 }
2608 }
Adam Langley95c29f32014-06-20 12:00:00 -07002609
David Benjamin65ac9972016-09-02 21:35:25 -04002610 uint16_t grease_ext1 = 0;
2611 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002612 // Add a fake empty extension. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002613 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2614 if (!CBB_add_u16(&extensions, grease_ext1) ||
2615 !CBB_add_u16(&extensions, 0 /* zero length */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002616 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2617 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002618 }
2619 }
2620
David Benjamin54091232016-09-05 12:47:25 -04002621 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002622 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002623 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002624 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002625 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin81678aa2017-07-12 22:43:42 -04002626 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002627 }
Adam Langley95c29f32014-06-20 12:00:00 -07002628
Adam Langley33ad2b52015-07-20 17:43:53 -07002629 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002630 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002631 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002632 }
Adam Langley75712922014-10-10 16:23:43 -07002633
David Benjamin2bd19172016-11-17 16:47:15 +09002634 if (!custom_ext_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002635 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2636 return 0;
Adam Langley09505632015-07-30 18:10:13 -07002637 }
2638
David Benjamin65ac9972016-09-02 21:35:25 -04002639 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002640 // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002641 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2642
David Benjaminc11ea9422017-08-29 16:33:21 -04002643 // The two fake extensions must not have the same value. GREASE values are
2644 // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2645 // one.
David Benjamin65ac9972016-09-02 21:35:25 -04002646 if (grease_ext1 == grease_ext2) {
2647 grease_ext2 ^= 0x1010;
2648 }
2649
2650 if (!CBB_add_u16(&extensions, grease_ext2) ||
2651 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2652 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002653 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2654 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002655 }
2656 }
2657
David Benjamince079fd2016-08-02 16:22:34 -04002658 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002659 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002660 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002661 if (header_len > 0xff && header_len < 0x200) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002662 // Add padding to workaround bugs in F5 terminators. See RFC 7685.
2663 //
2664 // NB: because this code works out the length of all existing extensions
2665 // it MUST always appear last.
David Benjamin0a968592015-07-21 22:06:19 -04002666 size_t padding_len = 0x200 - header_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04002667 // Extensions take at least four bytes to encode. Always include at least
2668 // one byte of data if including the extension. WebSphere Application
2669 // Server 7.0 is intolerant to the last extension being zero-length. See
2670 // https://crbug.com/363583.
Adam Langleyfcf25832014-12-18 17:42:32 -08002671 if (padding_len >= 4 + 1) {
2672 padding_len -= 4;
2673 } else {
2674 padding_len = 1;
2675 }
Adam Langley95c29f32014-06-20 12:00:00 -07002676
Adam Langley33ad2b52015-07-20 17:43:53 -07002677 uint8_t *padding_bytes;
2678 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2679 !CBB_add_u16(&extensions, padding_len) ||
2680 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002681 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2682 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002683 }
Adam Langley75712922014-10-10 16:23:43 -07002684
David Benjamin17cf2cb2016-12-13 01:07:13 -05002685 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002686 }
2687 }
Adam Langley75712922014-10-10 16:23:43 -07002688
David Benjaminc11ea9422017-08-29 16:33:21 -04002689 // The PSK extension must be last, including after the padding.
David Benjamin8c880a22016-12-03 02:20:34 -05002690 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002691 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2692 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -04002693 }
2694
David Benjaminc11ea9422017-08-29 16:33:21 -04002695 // Discard empty extensions blocks.
David Benjamina01deee2015-12-08 18:56:31 -05002696 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002697 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002698 }
2699
David Benjamine8d53502015-10-10 14:13:23 -04002700 return CBB_flush(out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002701}
Adam Langley95c29f32014-06-20 12:00:00 -07002702
David Benjamin8c880a22016-12-03 02:20:34 -05002703int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2704 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002705 CBB extensions;
2706 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002707 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002708 }
2709
David Benjamin8c880a22016-12-03 02:20:34 -05002710 for (unsigned i = 0; i < kNumExtensions; i++) {
2711 if (!(hs->extensions.received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002712 // Don't send extensions that were not received.
Adam Langley614c66a2015-06-12 15:26:58 -07002713 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002714 }
Adam Langley95c29f32014-06-20 12:00:00 -07002715
David Benjamin8c880a22016-12-03 02:20:34 -05002716 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002717 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002718 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002719 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002720 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002721 }
Adam Langley95c29f32014-06-20 12:00:00 -07002722
David Benjamin2bd19172016-11-17 16:47:15 +09002723 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002724 goto err;
2725 }
2726
David Benjaminc11ea9422017-08-29 16:33:21 -04002727 // Discard empty extensions blocks before TLS 1.3.
Steven Valdez143e8b32016-07-11 13:19:03 -04002728 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2729 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002730 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002731 }
2732
David Benjamin56380462015-10-10 14:59:09 -04002733 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002734
2735err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002736 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002737 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002738}
Adam Langley95c29f32014-06-20 12:00:00 -07002739
David Benjamin731058e2016-12-03 23:15:13 -05002740static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2741 const SSL_CLIENT_HELLO *client_hello,
2742 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002743 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002744 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002745 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002746 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002747 }
2748 }
2749
David Benjamin8c880a22016-12-03 02:20:34 -05002750 hs->extensions.received = 0;
2751 hs->custom_extensions.received = 0;
David Benjamine14ff062016-08-09 16:21:24 -04002752 CBS extensions;
2753 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2754 while (CBS_len(&extensions) != 0) {
2755 uint16_t type;
2756 CBS extension;
2757
David Benjaminc11ea9422017-08-29 16:33:21 -04002758 // Decode the next extension.
David Benjamine14ff062016-08-09 16:21:24 -04002759 if (!CBS_get_u16(&extensions, &type) ||
2760 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002761 *out_alert = SSL_AD_DECODE_ERROR;
2762 return 0;
2763 }
Adam Langley95c29f32014-06-20 12:00:00 -07002764
David Benjaminc11ea9422017-08-29 16:33:21 -04002765 // RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2766 // ambiguous. Ignore all but the renegotiation_info extension.
David Benjamine14ff062016-08-09 16:21:24 -04002767 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2768 continue;
2769 }
Adam Langley95c29f32014-06-20 12:00:00 -07002770
David Benjamine14ff062016-08-09 16:21:24 -04002771 unsigned ext_index;
2772 const struct tls_extension *const ext =
2773 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002774
David Benjamine14ff062016-08-09 16:21:24 -04002775 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002776 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002777 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002778 return 0;
2779 }
David Benjamine14ff062016-08-09 16:21:24 -04002780 continue;
2781 }
2782
David Benjamin8c880a22016-12-03 02:20:34 -05002783 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002784 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002785 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002786 *out_alert = alert;
2787 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002788 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002789 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002790 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002791 }
Adam Langley75712922014-10-10 16:23:43 -07002792
David Benjamin1deb41b2016-08-09 19:36:38 -04002793 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002794 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002795 continue;
2796 }
2797
2798 CBS *contents = NULL, fake_contents;
2799 static const uint8_t kFakeRenegotiateExtension[] = {0};
2800 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2801 ssl_client_cipher_list_contains_cipher(client_hello,
2802 SSL3_CK_SCSV & 0xffff)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002803 // The renegotiation SCSV was received so pretend that we received a
2804 // renegotiation extension.
David Benjamin1deb41b2016-08-09 19:36:38 -04002805 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2806 sizeof(kFakeRenegotiateExtension));
2807 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002808 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002809 }
2810
David Benjaminc11ea9422017-08-29 16:33:21 -04002811 // Extension wasn't observed so call the callback with a NULL
2812 // parameter.
David Benjamin1deb41b2016-08-09 19:36:38 -04002813 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002814 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002815 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002816 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002817 *out_alert = alert;
2818 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002819 }
2820 }
2821
Adam Langleyfcf25832014-12-18 17:42:32 -08002822 return 1;
2823}
Adam Langley95c29f32014-06-20 12:00:00 -07002824
David Benjamin731058e2016-12-03 23:15:13 -05002825int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2826 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002827 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002828 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002829 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002830 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002831 return 0;
2832 }
Adam Langley95c29f32014-06-20 12:00:00 -07002833
David Benjamin8c880a22016-12-03 02:20:34 -05002834 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002835 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002836 return 0;
2837 }
Adam Langley95c29f32014-06-20 12:00:00 -07002838
Adam Langleyfcf25832014-12-18 17:42:32 -08002839 return 1;
2840}
Adam Langley95c29f32014-06-20 12:00:00 -07002841
David Benjamin8c880a22016-12-03 02:20:34 -05002842static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2843 int *out_alert) {
2844 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04002845 // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty.
Steven Valdez143e8b32016-07-11 13:19:03 -04002846 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2847 return 1;
2848 }
Adam Langley614c66a2015-06-12 15:26:58 -07002849
David Benjaminc11ea9422017-08-29 16:33:21 -04002850 // Decode the extensions block and check it is valid.
Steven Valdez143e8b32016-07-11 13:19:03 -04002851 CBS extensions;
2852 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2853 !tls1_check_duplicate_extensions(&extensions)) {
2854 *out_alert = SSL_AD_DECODE_ERROR;
2855 return 0;
2856 }
2857
2858 uint32_t received = 0;
2859 while (CBS_len(&extensions) != 0) {
2860 uint16_t type;
2861 CBS extension;
2862
David Benjaminc11ea9422017-08-29 16:33:21 -04002863 // Decode the next extension.
Steven Valdez143e8b32016-07-11 13:19:03 -04002864 if (!CBS_get_u16(&extensions, &type) ||
2865 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002866 *out_alert = SSL_AD_DECODE_ERROR;
2867 return 0;
2868 }
Adam Langley95c29f32014-06-20 12:00:00 -07002869
Steven Valdez143e8b32016-07-11 13:19:03 -04002870 unsigned ext_index;
2871 const struct tls_extension *const ext =
2872 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002873
Steven Valdez143e8b32016-07-11 13:19:03 -04002874 if (ext == NULL) {
David Benjaminfd45ee72017-08-31 14:49:09 -04002875 hs->received_custom_extension = true;
David Benjamin2bd19172016-11-17 16:47:15 +09002876 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002877 return 0;
2878 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002879 continue;
2880 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002881
David Benjamina3d76d02017-07-14 19:36:07 -04002882 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
2883 "too many bits");
David Benjamin5db7c9b2017-01-24 16:17:03 -05002884
David Benjamin8c880a22016-12-03 02:20:34 -05002885 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002886 type != TLSEXT_TYPE_renegotiate) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002887 // If the extension was never sent then it is illegal, except for the
2888 // renegotiation extension which, in SSL 3.0, is signaled via SCSV.
Steven Valdez143e8b32016-07-11 13:19:03 -04002889 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2890 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002891 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002892 return 0;
2893 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002894
Steven Valdez143e8b32016-07-11 13:19:03 -04002895 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002896
Steven Valdez143e8b32016-07-11 13:19:03 -04002897 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002898 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002899 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002900 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04002901 *out_alert = alert;
2902 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002903 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002904 }
Adam Langley95c29f32014-06-20 12:00:00 -07002905
David Benjamin54091232016-09-05 12:47:25 -04002906 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002907 if (!(received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002908 // Extension wasn't observed so call the callback with a NULL
2909 // parameter.
Adam Langley614c66a2015-06-12 15:26:58 -07002910 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002911 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002912 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002913 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002914 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002915 return 0;
2916 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002917 }
2918 }
Adam Langley95c29f32014-06-20 12:00:00 -07002919
Adam Langleyfcf25832014-12-18 17:42:32 -08002920 return 1;
2921}
Adam Langley95c29f32014-06-20 12:00:00 -07002922
David Benjamin8c880a22016-12-03 02:20:34 -05002923static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
2924 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002925 int ret = SSL_TLSEXT_ERR_NOACK;
2926 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002927
David Benjamin78f8aab2016-03-10 16:33:58 -05002928 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002929 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002930 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05002931 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
2932 ret = ssl->session_ctx->tlsext_servername_callback(
2933 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002934 }
Adam Langley95c29f32014-06-20 12:00:00 -07002935
Adam Langleyfcf25832014-12-18 17:42:32 -08002936 switch (ret) {
2937 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002938 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002939 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002940
Adam Langleyfcf25832014-12-18 17:42:32 -08002941 case SSL_TLSEXT_ERR_NOACK:
David Benjaminfd45ee72017-08-31 14:49:09 -04002942 hs->should_ack_sni = false;
Adam Langleyfcf25832014-12-18 17:42:32 -08002943 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002944
Adam Langleyfcf25832014-12-18 17:42:32 -08002945 default:
2946 return 1;
2947 }
2948}
Adam Langleyed8270a2014-09-02 13:52:56 -07002949
David Benjamin8c880a22016-12-03 02:20:34 -05002950int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
2951 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002952 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002953 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002954 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002955 return 0;
2956 }
2957
Adam Langleyfcf25832014-12-18 17:42:32 -08002958 return 1;
2959}
Adam Langley95c29f32014-06-20 12:00:00 -07002960
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002961static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
2962 uint8_t **out, size_t *out_len, EVP_CIPHER_CTX *cipher_ctx,
2963 HMAC_CTX *hmac_ctx, const uint8_t *ticket, size_t ticket_len) {
2964 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002965
David Benjaminc11ea9422017-08-29 16:33:21 -04002966 // Check the MAC at the end of the ticket.
David Benjamine3aa1d92015-06-16 15:34:50 -04002967 uint8_t mac[EVP_MAX_MD_SIZE];
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002968 size_t mac_len = HMAC_size(hmac_ctx);
David Benjamine3aa1d92015-06-16 15:34:50 -04002969 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002970 // The ticket must be large enough for key name, IV, data, and MAC.
David Benjamin81678aa2017-07-12 22:43:42 -04002971 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08002972 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002973 HMAC_Update(hmac_ctx, ticket, ticket_len - mac_len);
2974 HMAC_Final(hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04002975 int mac_ok =
2976 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
2977#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
2978 mac_ok = 1;
2979#endif
2980 if (!mac_ok) {
David Benjamin81678aa2017-07-12 22:43:42 -04002981 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08002982 }
2983
David Benjaminc11ea9422017-08-29 16:33:21 -04002984 // Decrypt the session data.
David Benjamine3aa1d92015-06-16 15:34:50 -04002985 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2986 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2987 mac_len;
David Benjamin86e95b82017-07-18 16:34:25 -04002988 UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len));
David Benjamin81678aa2017-07-12 22:43:42 -04002989 if (!plaintext) {
2990 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08002991 }
David Benjaminfbc45d72016-09-22 01:21:24 -04002992 size_t plaintext_len;
2993#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin81678aa2017-07-12 22:43:42 -04002994 OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04002995 plaintext_len = ciphertext_len;
2996#else
David Benjamine3aa1d92015-06-16 15:34:50 -04002997 if (ciphertext_len >= INT_MAX) {
David Benjamin81678aa2017-07-12 22:43:42 -04002998 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08002999 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003000 int len1, len2;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003001 if (!EVP_DecryptUpdate(cipher_ctx, plaintext.get(), &len1, ciphertext,
David Benjamine3aa1d92015-06-16 15:34:50 -04003002 (int)ciphertext_len) ||
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003003 !EVP_DecryptFinal_ex(cipher_ctx, plaintext.get() + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08003004 ERR_clear_error();
David Benjamin81678aa2017-07-12 22:43:42 -04003005 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08003006 }
Adam Langley4c341d02017-03-08 19:33:21 -08003007 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04003008#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003009
David Benjamin81678aa2017-07-12 22:43:42 -04003010 *out = plaintext.release();
Adam Langley4c341d02017-03-08 19:33:21 -08003011 *out_len = plaintext_len;
David Benjamin81678aa2017-07-12 22:43:42 -04003012 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08003013}
3014
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003015static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
David Benjaminfd45ee72017-08-31 14:49:09 -04003016 SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003017 const uint8_t *ticket, size_t ticket_len) {
3018 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3019 ScopedEVP_CIPHER_CTX cipher_ctx;
3020 ScopedHMAC_CTX hmac_ctx;
3021 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3022 int cb_ret = ssl->session_ctx->tlsext_ticket_key_cb(
3023 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(),
3024 hmac_ctx.get(), 0 /* decrypt */);
3025 if (cb_ret < 0) {
3026 return ssl_ticket_aead_error;
3027 } else if (cb_ret == 0) {
3028 return ssl_ticket_aead_ignore_ticket;
3029 } else if (cb_ret == 2) {
David Benjaminfd45ee72017-08-31 14:49:09 -04003030 *out_renew_ticket = true;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003031 } else {
3032 assert(cb_ret == 1);
3033 }
3034 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3035 hmac_ctx.get(), ticket, ticket_len);
3036}
3037
3038static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
3039 SSL *ssl, uint8_t **out, size_t *out_len, const uint8_t *ticket,
3040 size_t ticket_len) {
3041 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3042 SSL_CTX *ctx = ssl->session_ctx;
3043
David Benjaminc11ea9422017-08-29 16:33:21 -04003044 // Rotate the ticket key if necessary.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003045 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
3046 return ssl_ticket_aead_error;
3047 }
3048
David Benjaminc11ea9422017-08-29 16:33:21 -04003049 // Pick the matching ticket key and decrypt.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003050 ScopedEVP_CIPHER_CTX cipher_ctx;
3051 ScopedHMAC_CTX hmac_ctx;
3052 {
3053 MutexReadLock lock(&ctx->lock);
3054 const tlsext_ticket_key *key;
3055 if (ctx->tlsext_ticket_key_current &&
3056 !OPENSSL_memcmp(ctx->tlsext_ticket_key_current->name, ticket,
3057 SSL_TICKET_KEY_NAME_LEN)) {
3058 key = ctx->tlsext_ticket_key_current;
3059 } else if (ctx->tlsext_ticket_key_prev &&
3060 !OPENSSL_memcmp(ctx->tlsext_ticket_key_prev->name, ticket,
3061 SSL_TICKET_KEY_NAME_LEN)) {
3062 key = ctx->tlsext_ticket_key_prev;
3063 } else {
3064 return ssl_ticket_aead_ignore_ticket;
3065 }
3066 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3067 if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
3068 tlsext_tick_md(), NULL) ||
3069 !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL,
3070 key->aes_key, iv)) {
3071 return ssl_ticket_aead_error;
3072 }
3073 }
3074 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3075 hmac_ctx.get(), ticket, ticket_len);
3076}
3077
Adam Langley4c341d02017-03-08 19:33:21 -08003078static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
David Benjaminfd45ee72017-08-31 14:49:09 -04003079 SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
Adam Langley4c341d02017-03-08 19:33:21 -08003080 const uint8_t *ticket, size_t ticket_len) {
David Benjamin81678aa2017-07-12 22:43:42 -04003081 uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len);
Adam Langley4c341d02017-03-08 19:33:21 -08003082 if (plaintext == NULL) {
3083 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3084 return ssl_ticket_aead_error;
3085 }
3086
3087 size_t plaintext_len;
3088 const enum ssl_ticket_aead_result_t result =
3089 ssl->session_ctx->ticket_aead_method->open(
3090 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3091
3092 if (result == ssl_ticket_aead_success) {
3093 *out = plaintext;
3094 plaintext = NULL;
3095 *out_len = plaintext_len;
3096 }
3097
3098 OPENSSL_free(plaintext);
3099 return result;
3100}
3101
3102enum ssl_ticket_aead_result_t ssl_process_ticket(
David Benjaminfd45ee72017-08-31 14:49:09 -04003103 SSL *ssl, UniquePtr<SSL_SESSION> *out_session, bool *out_renew_ticket,
Adam Langley4c341d02017-03-08 19:33:21 -08003104 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3105 size_t session_id_len) {
David Benjaminfd45ee72017-08-31 14:49:09 -04003106 *out_renew_ticket = false;
David Benjamin37af90f2017-07-29 01:42:16 -04003107 out_session->reset();
Adam Langley4c341d02017-03-08 19:33:21 -08003108
3109 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3110 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3111 return ssl_ticket_aead_ignore_ticket;
3112 }
3113
3114 uint8_t *plaintext = NULL;
3115 size_t plaintext_len;
3116 enum ssl_ticket_aead_result_t result;
3117 if (ssl->session_ctx->ticket_aead_method != NULL) {
3118 result = ssl_decrypt_ticket_with_method(
3119 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3120 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04003121 // Ensure there is room for the key name and the largest IV
3122 // |tlsext_ticket_key_cb| may try to consume. The real limit may be lower,
3123 // but the maximum IV length should be well under the minimum size for the
3124 // session material and HMAC.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003125 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3126 return ssl_ticket_aead_ignore_ticket;
3127 }
3128 if (ssl->session_ctx->tlsext_ticket_key_cb != NULL) {
3129 result = ssl_decrypt_ticket_with_cb(ssl, &plaintext, &plaintext_len,
3130 out_renew_ticket, ticket, ticket_len);
3131 } else {
3132 result = ssl_decrypt_ticket_with_ticket_keys(
3133 ssl, &plaintext, &plaintext_len, ticket, ticket_len);
3134 }
Adam Langley4c341d02017-03-08 19:33:21 -08003135 }
3136
3137 if (result != ssl_ticket_aead_success) {
3138 return result;
3139 }
3140
David Benjaminc11ea9422017-08-29 16:33:21 -04003141 // Decode the session.
David Benjamin37af90f2017-07-29 01:42:16 -04003142 UniquePtr<SSL_SESSION> session(
3143 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx));
Adam Langley4c341d02017-03-08 19:33:21 -08003144 OPENSSL_free(plaintext);
3145
David Benjamin37af90f2017-07-29 01:42:16 -04003146 if (!session) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003147 ERR_clear_error(); // Don't leave an error on the queue.
Adam Langley4c341d02017-03-08 19:33:21 -08003148 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003149 }
3150
David Benjaminc11ea9422017-08-29 16:33:21 -04003151 // Copy the client's session ID into the new session, to denote the ticket has
3152 // been accepted.
David Benjamin17cf2cb2016-12-13 01:07:13 -05003153 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003154 session->session_id_length = session_id_len;
3155
David Benjamin37af90f2017-07-29 01:42:16 -04003156 *out_session = std::move(session);
Adam Langley4c341d02017-03-08 19:33:21 -08003157 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003158}
Adam Langley95c29f32014-06-20 12:00:00 -07003159
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003160int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003161 // Extension ignored for inappropriate versions
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003162 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003163 return 1;
3164 }
David Benjamincd996942014-07-20 16:23:51 -04003165
David Benjamin0fc37ef2016-08-17 15:29:46 -04003166 OPENSSL_free(hs->peer_sigalgs);
3167 hs->peer_sigalgs = NULL;
3168 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003169
3170 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003171 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003172 return 0;
3173 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003174 num_sigalgs /= 2;
3175
David Benjaminc11ea9422017-08-29 16:33:21 -04003176 // supported_signature_algorithms in the certificate request is
3177 // allowed to be empty.
Steven Valdez0d62f262015-09-04 12:41:04 -04003178 if (num_sigalgs == 0) {
3179 return 1;
3180 }
3181
David Benjaminc11ea9422017-08-29 16:33:21 -04003182 // This multiplication doesn't overflow because sizeof(uint16_t) is two
3183 // and we just divided |num_sigalgs| by two.
David Benjamin81678aa2017-07-12 22:43:42 -04003184 hs->peer_sigalgs = (uint16_t *)OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003185 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003186 return 0;
3187 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003188 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003189
3190 CBS sigalgs;
3191 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003192 for (size_t i = 0; i < num_sigalgs; i++) {
3193 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003194 return 0;
3195 }
3196 }
Adam Langley95c29f32014-06-20 12:00:00 -07003197
Adam Langleyfcf25832014-12-18 17:42:32 -08003198 return 1;
3199}
David Benjaminec2f27d2014-11-13 19:17:25 -05003200
David Benjamina3651382017-04-20 17:49:36 -04003201int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
3202 switch (EVP_PKEY_id(pkey)) {
3203 case EVP_PKEY_RSA:
3204 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3205 return 1;
3206 case EVP_PKEY_EC:
3207 *out = SSL_SIGN_ECDSA_SHA1;
3208 return 1;
3209 default:
3210 return 0;
3211 }
3212}
3213
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003214int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3215 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003216 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003217
David Benjaminc11ea9422017-08-29 16:33:21 -04003218 // Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3219 // handshake.
Steven Valdezf0451ca2016-06-29 13:16:27 -04003220 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin31b0c9b2017-07-20 14:49:15 -04003221 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
David Benjamina3651382017-04-20 17:49:36 -04003222 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3223 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003224 }
David Benjamina3651382017-04-20 17:49:36 -04003225 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003226 }
3227
David Benjamin3ef76972016-10-17 17:59:54 -04003228 const uint16_t *sigalgs = cert->sigalgs;
3229 size_t num_sigalgs = cert->num_sigalgs;
3230 if (sigalgs == NULL) {
3231 sigalgs = kSignSignatureAlgorithms;
3232 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003233 }
3234
David Benjamin0fc37ef2016-08-17 15:29:46 -04003235 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3236 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3237 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003238 // If the client didn't specify any signature_algorithms extension then
3239 // we can assume that it supports SHA1. See
3240 // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
David Benjaminea9a0d52016-07-08 15:52:59 -07003241 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3242 SSL_SIGN_ECDSA_SHA1};
3243 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003244 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003245 }
3246
David Benjamin0fc37ef2016-08-17 15:29:46 -04003247 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003248 uint16_t sigalg = sigalgs[i];
David Benjaminc11ea9422017-08-29 16:33:21 -04003249 // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3250 // negotiated.
David Benjamin1fb125c2016-07-08 18:52:12 -07003251 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjamina232a712017-03-30 15:51:53 -05003252 !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003253 continue;
3254 }
3255
David Benjamin0fc37ef2016-08-17 15:29:46 -04003256 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003257 if (sigalg == peer_sigalgs[j]) {
3258 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003259 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003260 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003261 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003262 }
Adam Langley95c29f32014-06-20 12:00:00 -07003263
David Benjaminea9a0d52016-07-08 15:52:59 -07003264 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3265 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003266}
Adam Langley95c29f32014-06-20 12:00:00 -07003267
David Benjamin7934f082017-08-01 16:32:25 -04003268int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
Steven Valdez908ac192017-01-12 13:17:07 -05003269 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003270 // A Channel ID handshake message is structured to contain multiple
3271 // extensions, but the only one that can be present is Channel ID.
David Benjamin7934f082017-08-01 16:32:25 -04003272 uint16_t extension_type;
3273 CBS channel_id = msg.body, extension;
Nick Harper60a85cb2016-09-23 16:25:11 -07003274 if (!CBS_get_u16(&channel_id, &extension_type) ||
3275 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3276 CBS_len(&channel_id) != 0 ||
3277 extension_type != TLSEXT_TYPE_channel_id ||
3278 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3279 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3280 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3281 return 0;
3282 }
3283
David Benjamin86e95b82017-07-18 16:34:25 -04003284 UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Nick Harper60a85cb2016-09-23 16:25:11 -07003285 if (!p256) {
3286 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3287 return 0;
3288 }
3289
David Benjamin86e95b82017-07-18 16:34:25 -04003290 UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3291 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
David Benjamin81678aa2017-07-12 22:43:42 -04003292 if (!sig || !x || !y) {
3293 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003294 }
3295
3296 const uint8_t *p = CBS_data(&extension);
David Benjamin81678aa2017-07-12 22:43:42 -04003297 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3298 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3299 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3300 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
3301 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003302 }
3303
David Benjamin86e95b82017-07-18 16:34:25 -04003304 UniquePtr<EC_KEY> key(EC_KEY_new());
3305 UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
David Benjamin81678aa2017-07-12 22:43:42 -04003306 if (!key || !point ||
3307 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3308 y.get(), nullptr) ||
3309 !EC_KEY_set_group(key.get(), p256.get()) ||
3310 !EC_KEY_set_public_key(key.get(), point.get())) {
3311 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003312 }
3313
3314 uint8_t digest[EVP_MAX_MD_SIZE];
3315 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003316 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003317 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003318 }
3319
David Benjamin81678aa2017-07-12 22:43:42 -04003320 int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
Nick Harper60a85cb2016-09-23 16:25:11 -07003321#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3322 sig_ok = 1;
3323#endif
3324 if (!sig_ok) {
3325 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3326 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
David Benjamin046bc1f2017-08-31 15:06:42 -04003327 ssl->s3->tlsext_channel_id_valid = false;
David Benjamin81678aa2017-07-12 22:43:42 -04003328 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003329 }
3330
David Benjamin17cf2cb2016-12-13 01:07:13 -05003331 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
David Benjamin81678aa2017-07-12 22:43:42 -04003332 return 1;
Nick Harper60a85cb2016-09-23 16:25:11 -07003333}
3334
Steven Valdez908ac192017-01-12 13:17:07 -05003335int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3336 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003337 uint8_t digest[EVP_MAX_MD_SIZE];
3338 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003339 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003340 return 0;
3341 }
3342
3343 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3344 if (ec_key == NULL) {
3345 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3346 return 0;
3347 }
3348
3349 int ret = 0;
3350 BIGNUM *x = BN_new();
3351 BIGNUM *y = BN_new();
3352 ECDSA_SIG *sig = NULL;
3353 if (x == NULL || y == NULL ||
3354 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3355 EC_KEY_get0_public_key(ec_key),
3356 x, y, NULL)) {
3357 goto err;
3358 }
3359
3360 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3361 if (sig == NULL) {
3362 goto err;
3363 }
3364
3365 CBB child;
3366 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3367 !CBB_add_u16_length_prefixed(cbb, &child) ||
3368 !BN_bn2cbb_padded(&child, 32, x) ||
3369 !BN_bn2cbb_padded(&child, 32, y) ||
3370 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3371 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3372 !CBB_flush(cbb)) {
3373 goto err;
3374 }
3375
3376 ret = 1;
3377
3378err:
3379 BN_free(x);
3380 BN_free(y);
3381 ECDSA_SIG_free(sig);
3382 return ret;
3383}
3384
Steven Valdez908ac192017-01-12 13:17:07 -05003385int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3386 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003387 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3388 uint8_t *msg;
3389 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003390 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003391 ssl_cert_verify_channel_id)) {
3392 return 0;
3393 }
3394 SHA256(msg, msg_len, out);
3395 *out_len = SHA256_DIGEST_LENGTH;
3396 OPENSSL_free(msg);
3397 return 1;
3398 }
3399
Nick Harper95594012016-10-20 14:07:13 -07003400 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003401
Nick Harper95594012016-10-20 14:07:13 -07003402 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003403 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003404 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003405
Steven Valdez87eab492016-06-27 16:34:59 -04003406 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003407 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003408 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003409 if (ssl->session->original_handshake_hash_len == 0) {
3410 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003411 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003412 }
Nick Harper95594012016-10-20 14:07:13 -07003413 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3414 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003415 }
3416
Steven Valdez908ac192017-01-12 13:17:07 -05003417 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3418 size_t hs_hash_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003419 if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003420 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003421 }
Steven Valdez908ac192017-01-12 13:17:07 -05003422 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003423 SHA256_Final(out, &ctx);
3424 *out_len = SHA256_DIGEST_LENGTH;
3425 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003426}
Adam Langley1258b6a2014-06-20 12:00:00 -07003427
David Benjaminc11ea9422017-08-29 16:33:21 -04003428// tls1_record_handshake_hashes_for_channel_id records the current handshake
3429// hashes in |hs->new_session| so that Channel ID resumptions can sign that
3430// data.
Steven Valdez908ac192017-01-12 13:17:07 -05003431int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3432 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003433 // This function should never be called for a resumed session because the
3434 // handshake hashes that we wish to record are for the original, full
3435 // handshake.
Steven Valdez87eab492016-06-27 16:34:59 -04003436 if (ssl->session != NULL) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04003437 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003438 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003439
David Benjamina3d76d02017-07-14 19:36:07 -04003440 static_assert(
David Benjamin45738dd2017-02-09 20:01:26 -05003441 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
David Benjamina3d76d02017-07-14 19:36:07 -04003442 "original_handshake_hash is too small");
Steven Valdez908ac192017-01-12 13:17:07 -05003443
3444 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003445 if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
3446 &digest_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04003447 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003448 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003449
David Benjamina3d76d02017-07-14 19:36:07 -04003450 static_assert(EVP_MAX_MD_SIZE <= 0xff,
3451 "EVP_MAX_MD_SIZE does not fit in uint8_t");
David Benjamin45738dd2017-02-09 20:01:26 -05003452 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003453
Adam Langleyfcf25832014-12-18 17:42:32 -08003454 return 1;
3455}
Nick Harper60a85cb2016-09-23 16:25:11 -07003456
3457int ssl_do_channel_id_callback(SSL *ssl) {
3458 if (ssl->tlsext_channel_id_private != NULL ||
3459 ssl->ctx->channel_id_cb == NULL) {
3460 return 1;
3461 }
3462
3463 EVP_PKEY *key = NULL;
3464 ssl->ctx->channel_id_cb(ssl, &key);
3465 if (key == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003466 // The caller should try again later.
Nick Harper60a85cb2016-09-23 16:25:11 -07003467 return 1;
3468 }
3469
3470 int ret = SSL_set1_tls_channel_id(ssl, key);
3471 EVP_PKEY_free(key);
3472 return ret;
3473}
Adam Langleycfa08c32016-11-17 13:21:27 -08003474
3475int ssl_is_sct_list_valid(const CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003476 // Shallow parse the SCT list for sanity. By the RFC
3477 // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3478 // of the SCTs may be empty.
Adam Langleycfa08c32016-11-17 13:21:27 -08003479 CBS copy = *contents;
3480 CBS sct_list;
3481 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3482 CBS_len(&copy) != 0 ||
3483 CBS_len(&sct_list) == 0) {
3484 return 0;
3485 }
3486
3487 while (CBS_len(&sct_list) > 0) {
3488 CBS sct;
3489 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3490 CBS_len(&sct) == 0) {
3491 return 0;
3492 }
3493 }
3494
3495 return 1;
3496}
David Benjamin86e95b82017-07-18 16:34:25 -04003497
3498} // namespace bssl
3499
3500using namespace bssl;
3501
3502int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
3503 uint16_t extension_type,
3504 const uint8_t **out_data,
3505 size_t *out_len) {
3506 CBS cbs;
3507 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
3508 return 0;
3509 }
3510
3511 *out_data = CBS_data(&cbs);
3512 *out_len = CBS_len(&cbs);
3513 return 1;
3514}
3515
3516void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
3517 ctx->ed25519_enabled = !!enabled;
3518}
3519
3520int SSL_extension_supported(unsigned extension_value) {
3521 uint32_t index;
3522 return extension_value == TLSEXT_TYPE_padding ||
3523 tls_extension_find(&index, extension_value) != NULL;
3524}