blob: 7a83ed42ea7cd8e9c3ba3eb9635f3867cbfc8fa4 [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) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400152 // First pass: count the extensions.
David Benjamin08f5c762017-09-21 02:43:05 -0400153 size_t num_extensions = 0;
154 CBS extensions = *cbs;
Adam Langleyfcf25832014-12-18 17:42:32 -0800155 while (CBS_len(&extensions) > 0) {
156 uint16_t type;
157 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400158
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 if (!CBS_get_u16(&extensions, &type) ||
160 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
David Benjamin08f5c762017-09-21 02:43:05 -0400161 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800162 }
David Benjamin35a7a442014-07-05 00:23:20 -0400163
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 num_extensions++;
165 }
David Benjamin35a7a442014-07-05 00:23:20 -0400166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 if (num_extensions == 0) {
168 return 1;
169 }
David Benjamin9a373592014-07-25 04:27:53 -0400170
David Benjamin08f5c762017-09-21 02:43:05 -0400171 Array<uint16_t> extension_types;
172 if (!extension_types.Init(num_extensions)) {
173 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800174 }
David Benjamin35a7a442014-07-05 00:23:20 -0400175
David Benjaminc11ea9422017-08-29 16:33:21 -0400176 // Second pass: gather the extension types.
Adam Langleyfcf25832014-12-18 17:42:32 -0800177 extensions = *cbs;
David Benjamin08f5c762017-09-21 02:43:05 -0400178 for (size_t i = 0; i < extension_types.size(); i++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800179 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400180
Adam Langleyfcf25832014-12-18 17:42:32 -0800181 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
182 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400183 // This should not happen.
David Benjamin08f5c762017-09-21 02:43:05 -0400184 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800185 }
186 }
187 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400188
David Benjaminc11ea9422017-08-29 16:33:21 -0400189 // Sort the extensions and make sure there are no duplicates.
David Benjamin08f5c762017-09-21 02:43:05 -0400190 qsort(extension_types.data(), extension_types.size(), sizeof(uint16_t),
191 compare_uint16_t);
192 for (size_t i = 1; i < num_extensions; i++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800193 if (extension_types[i - 1] == extension_types[i]) {
David Benjamin08f5c762017-09-21 02:43:05 -0400194 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800195 }
196 }
David Benjamin35a7a442014-07-05 00:23:20 -0400197
David Benjamin08f5c762017-09-21 02:43:05 -0400198 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800199}
David Benjamin35a7a442014-07-05 00:23:20 -0400200
David Benjamin7934f082017-08-01 16:32:25 -0400201int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out,
202 const SSLMessage &msg) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500203 OPENSSL_memset(out, 0, sizeof(*out));
David Benjamin731058e2016-12-03 23:15:13 -0500204 out->ssl = ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400205 out->client_hello = CBS_data(&msg.body);
206 out->client_hello_len = CBS_len(&msg.body);
David Benjamin8f2c20e2014-07-09 09:30:38 -0400207
David Benjamine14ff062016-08-09 16:21:24 -0400208 CBS client_hello, random, session_id;
David Benjamin731058e2016-12-03 23:15:13 -0500209 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
210 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjamine14ff062016-08-09 16:21:24 -0400211 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
212 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
213 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 return 0;
215 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700216
David Benjamin731058e2016-12-03 23:15:13 -0500217 out->random = CBS_data(&random);
218 out->random_len = CBS_len(&random);
219 out->session_id = CBS_data(&session_id);
220 out->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700221
David Benjaminc11ea9422017-08-29 16:33:21 -0400222 // Skip past DTLS cookie
David Benjamin731058e2016-12-03 23:15:13 -0500223 if (SSL_is_dtls(out->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800224 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400225 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
226 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 return 0;
228 }
229 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700230
David Benjamine14ff062016-08-09 16:21:24 -0400231 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400233 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
234 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800235 CBS_len(&compression_methods) < 1) {
236 return 0;
237 }
David Benjamine14ff062016-08-09 16:21:24 -0400238
David Benjamin731058e2016-12-03 23:15:13 -0500239 out->cipher_suites = CBS_data(&cipher_suites);
240 out->cipher_suites_len = CBS_len(&cipher_suites);
241 out->compression_methods = CBS_data(&compression_methods);
242 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700243
David Benjaminc11ea9422017-08-29 16:33:21 -0400244 // If the ClientHello ends here then it's valid, but doesn't have any
245 // extensions. (E.g. SSLv3.)
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 if (CBS_len(&client_hello) == 0) {
David Benjamin731058e2016-12-03 23:15:13 -0500247 out->extensions = NULL;
248 out->extensions_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 return 1;
250 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700251
David Benjaminc11ea9422017-08-29 16:33:21 -0400252 // Extract extensions and check it is valid.
David Benjamine14ff062016-08-09 16:21:24 -0400253 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
255 !tls1_check_duplicate_extensions(&extensions) ||
256 CBS_len(&client_hello) != 0) {
257 return 0;
258 }
David Benjamine14ff062016-08-09 16:21:24 -0400259
David Benjamin731058e2016-12-03 23:15:13 -0500260 out->extensions = CBS_data(&extensions);
261 out->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700262
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 return 1;
264}
Adam Langleydc9b1412014-06-20 12:00:00 -0700265
David Benjamin731058e2016-12-03 23:15:13 -0500266int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
267 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 CBS extensions;
David Benjamin731058e2016-12-03 23:15:13 -0500269 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800270 while (CBS_len(&extensions) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400271 // Decode the next extension.
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 uint16_t type;
273 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 if (!CBS_get_u16(&extensions, &type) ||
275 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
276 return 0;
277 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700278
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400280 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800281 return 1;
282 }
283 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700284
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 return 0;
286}
Adam Langley95c29f32014-06-20 12:00:00 -0700287
Steven Valdezce902a92016-05-17 11:47:53 -0400288static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400289 SSL_CURVE_X25519,
290 SSL_CURVE_SECP256R1,
291 SSL_CURVE_SECP384R1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800292};
Adam Langley95c29f32014-06-20 12:00:00 -0700293
David Benjamincf0ce672017-09-21 02:25:59 -0400294Span<const uint16_t> tls1_get_grouplist(const SSL *ssl) {
295 if (ssl->supported_group_list != nullptr) {
296 return MakeConstSpan(ssl->supported_group_list,
297 ssl->supported_group_list_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800298 }
David Benjamincf0ce672017-09-21 02:25:59 -0400299 return Span<const uint16_t>(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800300}
David Benjamined439582014-07-14 19:13:02 -0400301
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900302int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
303 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400304 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400305
David Benjaminc11ea9422017-08-29 16:33:21 -0400306 // Clients are not required to send a supported_groups extension. In this
307 // case, the server is free to pick any group it likes. See RFC 4492,
308 // section 4, paragraph 3.
309 //
310 // However, in the interests of compatibility, we will skip ECDH if the
311 // client didn't send an extension because we can't be sure that they'll
312 // support our favoured group. Thus we do not special-case an emtpy
313 // |peer_supported_group_list|.
David Benjamin55a43642015-04-20 14:45:55 -0400314
David Benjamincf0ce672017-09-21 02:25:59 -0400315 Span<const uint16_t> groups = tls1_get_grouplist(ssl);
316 Span<const uint16_t> pref, supp;
David Benjamin4298d772015-12-19 00:18:25 -0500317 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400318 pref = groups;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900319 supp = hs->peer_supported_group_list;
David Benjamin55a43642015-04-20 14:45:55 -0400320 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900321 pref = hs->peer_supported_group_list;
Steven Valdezce902a92016-05-17 11:47:53 -0400322 supp = groups;
David Benjamin55a43642015-04-20 14:45:55 -0400323 }
324
David Benjamincf0ce672017-09-21 02:25:59 -0400325 for (uint16_t pref_group : pref) {
326 for (uint16_t supp_group : supp) {
327 if (pref_group == supp_group) {
328 *out_group_id = pref_group;
David Benjamin4298d772015-12-19 00:18:25 -0500329 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 }
331 }
332 }
333
David Benjamin4298d772015-12-19 00:18:25 -0500334 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800335}
Adam Langley95c29f32014-06-20 12:00:00 -0700336
Steven Valdezce902a92016-05-17 11:47:53 -0400337int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800338 const int *curves, size_t ncurves) {
David Benjamin81678aa2017-07-12 22:43:42 -0400339 uint16_t *group_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
Steven Valdezce902a92016-05-17 11:47:53 -0400340 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800341 return 0;
342 }
343
David Benjamin54091232016-09-05 12:47:25 -0400344 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400345 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
346 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800347 return 0;
348 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800349 }
350
Steven Valdezce902a92016-05-17 11:47:53 -0400351 OPENSSL_free(*out_group_ids);
352 *out_group_ids = group_ids;
353 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800354
355 return 1;
356}
Adam Langley95c29f32014-06-20 12:00:00 -0700357
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100358int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
359 const char *curves) {
360 uint16_t *group_ids = NULL;
361 size_t ncurves = 0;
362
363 const char *col;
364 const char *ptr = curves;
365
366 do {
367 col = strchr(ptr, ':');
368
369 uint16_t group_id;
370 if (!ssl_name_to_group_id(&group_id, ptr,
371 col ? (size_t)(col - ptr) : strlen(ptr))) {
372 goto err;
373 }
374
David Benjamin81678aa2017-07-12 22:43:42 -0400375 uint16_t *new_group_ids = (uint16_t *)OPENSSL_realloc(
376 group_ids, (ncurves + 1) * sizeof(uint16_t));
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100377 if (new_group_ids == NULL) {
378 goto err;
379 }
380 group_ids = new_group_ids;
381
382 group_ids[ncurves] = group_id;
383 ncurves++;
384
385 if (col) {
386 ptr = col + 1;
387 }
388 } while (col);
389
390 OPENSSL_free(*out_group_ids);
391 *out_group_ids = group_ids;
392 *out_group_ids_len = ncurves;
393
394 return 1;
395
396err:
397 OPENSSL_free(group_ids);
398 return 0;
399}
400
David Benjamincf0ce672017-09-21 02:25:59 -0400401int tls1_check_group_id(const SSL *ssl, uint16_t group_id) {
402 for (uint16_t supported : tls1_get_grouplist(ssl)) {
403 if (supported == group_id) {
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400404 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800405 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 }
David Benjamin033e5f42014-11-13 18:47:41 -0500407
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400408 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800409}
David Benjamin033e5f42014-11-13 18:47:41 -0500410
David Benjaminc11ea9422017-08-29 16:33:21 -0400411// kVerifySignatureAlgorithms is the default list of accepted signature
412// algorithms for verifying.
413//
414// For now, RSA-PSS signature algorithms are not enabled on Android's system
415// BoringSSL. Once the change in Chrome has stuck and the values are finalized,
416// restore them.
David Benjamin3ef76972016-10-17 17:59:54 -0400417static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400418 // List our preferred algorithms first.
David Benjamin69522112017-03-28 15:38:29 -0500419 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400420 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400421#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400422 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400423#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400424 SSL_SIGN_RSA_PKCS1_SHA256,
425
David Benjaminc11ea9422017-08-29 16:33:21 -0400426 // Larger hashes are acceptable.
David Benjamin3a322f52016-10-26 12:45:35 -0400427 SSL_SIGN_ECDSA_SECP384R1_SHA384,
428#if !defined(BORINGSSL_ANDROID_SYSTEM)
429 SSL_SIGN_RSA_PSS_SHA384,
430#endif
431 SSL_SIGN_RSA_PKCS1_SHA384,
432
David Benjaminc11ea9422017-08-29 16:33:21 -0400433 // TODO(davidben): Remove this.
David Benjamin3ef76972016-10-17 17:59:54 -0400434#if defined(BORINGSSL_ANDROID_SYSTEM)
435 SSL_SIGN_ECDSA_SECP521R1_SHA512,
436#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400437#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400438 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400439#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400440 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400441
David Benjaminc11ea9422017-08-29 16:33:21 -0400442 // For now, SHA-1 is still accepted but least preferable.
David Benjamin3a322f52016-10-26 12:45:35 -0400443 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400444
David Benjamin3a322f52016-10-26 12:45:35 -0400445};
446
David Benjaminc11ea9422017-08-29 16:33:21 -0400447// kSignSignatureAlgorithms is the default list of supported signature
448// algorithms for signing.
449//
450// For now, RSA-PSS signature algorithms are not enabled on Android's system
451// BoringSSL. Once the change in Chrome has stuck and the values are finalized,
452// restore them.
David Benjamin3a322f52016-10-26 12:45:35 -0400453static const uint16_t kSignSignatureAlgorithms[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400454 // List our preferred algorithms first.
David Benjamin69522112017-03-28 15:38:29 -0500455 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400456 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400457#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400458 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400459#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400460 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400461
David Benjaminc11ea9422017-08-29 16:33:21 -0400462 // If needed, sign larger hashes.
463 //
464 // TODO(davidben): Determine which of these may be pruned.
David Benjamin3a322f52016-10-26 12:45:35 -0400465 SSL_SIGN_ECDSA_SECP384R1_SHA384,
466#if !defined(BORINGSSL_ANDROID_SYSTEM)
467 SSL_SIGN_RSA_PSS_SHA384,
468#endif
469 SSL_SIGN_RSA_PKCS1_SHA384,
470
471 SSL_SIGN_ECDSA_SECP521R1_SHA512,
472#if !defined(BORINGSSL_ANDROID_SYSTEM)
473 SSL_SIGN_RSA_PSS_SHA512,
474#endif
475 SSL_SIGN_RSA_PKCS1_SHA512,
476
David Benjaminc11ea9422017-08-29 16:33:21 -0400477 // If the peer supports nothing else, sign with SHA-1.
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400478 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400479 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400480};
481
David Benjamin69522112017-03-28 15:38:29 -0500482int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) {
David Benjamin71c21b42017-04-14 17:05:40 -0400483 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
484 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
485 if (ssl->ctx->num_verify_sigalgs != 0) {
486 sigalgs = ssl->ctx->verify_sigalgs;
487 num_sigalgs = ssl->ctx->num_verify_sigalgs;
488 }
489
490 for (size_t i = 0; i < num_sigalgs; i++) {
491 if (sigalgs == kVerifySignatureAlgorithms &&
492 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500493 !ssl->ctx->ed25519_enabled) {
494 continue;
495 }
David Benjamin71c21b42017-04-14 17:05:40 -0400496 if (!CBB_add_u16(out, sigalgs[i])) {
David Benjamin69522112017-03-28 15:38:29 -0500497 return 0;
498 }
499 }
500
501 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800502}
Adam Langley95c29f32014-06-20 12:00:00 -0700503
David Benjamin8d606e32017-06-15 22:43:04 -0400504int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg) {
David Benjamin71c21b42017-04-14 17:05:40 -0400505 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
506 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
507 if (ssl->ctx->num_verify_sigalgs != 0) {
508 sigalgs = ssl->ctx->verify_sigalgs;
509 num_sigalgs = ssl->ctx->num_verify_sigalgs;
510 }
511
512 for (size_t i = 0; i < num_sigalgs; i++) {
513 if (sigalgs == kVerifySignatureAlgorithms &&
514 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500515 !ssl->ctx->ed25519_enabled) {
516 continue;
517 }
David Benjamin71c21b42017-04-14 17:05:40 -0400518 if (sigalg == sigalgs[i]) {
David Benjamin3ef76972016-10-17 17:59:54 -0400519 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800520 }
521 }
522
David Benjamin3ef76972016-10-17 17:59:54 -0400523 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
524 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
525 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800526}
527
David Benjaminc11ea9422017-08-29 16:33:21 -0400528// tls_extension represents a TLS extension that is handled internally. The
529// |init| function is called for each handshake, before any other functions of
530// the extension. Then the add and parse callbacks are called as needed.
531//
532// The parse callbacks receive a |CBS| that contains the contents of the
533// extension (i.e. not including the type and length bytes). If an extension is
534// not received then the parse callbacks will be called with a NULL CBS so that
535// they can do any processing needed to handle the absence of an extension.
536//
537// The add callbacks receive a |CBB| to which the extension can be appended but
538// the function is responsible for appending the type and length bytes too.
539//
540// All callbacks return one for success and zero for error. If a parse function
541// returns zero then a fatal alert with value |*out_alert| will be sent. If
542// |*out_alert| isn't set, then a |decode_error| alert will be sent.
Adam Langley614c66a2015-06-12 15:26:58 -0700543struct tls_extension {
544 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500545 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700546
David Benjamin8c880a22016-12-03 02:20:34 -0500547 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
548 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
549 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700550
David Benjamin8c880a22016-12-03 02:20:34 -0500551 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
552 CBS *contents);
553 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700554};
555
David Benjamin8c880a22016-12-03 02:20:34 -0500556static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
557 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400558 if (contents != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400559 // Servers MUST NOT send this extension.
Steven Valdez6b8509a2016-07-12 13:38:32 -0400560 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
561 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
562 return 0;
563 }
564
565 return 1;
566}
567
David Benjamin8c880a22016-12-03 02:20:34 -0500568static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
569 CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400570 // This extension from the client is handled elsewhere.
Steven Valdez6b8509a2016-07-12 13:38:32 -0400571 return 1;
572}
573
David Benjamin8c880a22016-12-03 02:20:34 -0500574static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400575 return 1;
576}
Adam Langley614c66a2015-06-12 15:26:58 -0700577
David Benjaminc11ea9422017-08-29 16:33:21 -0400578// Server name indication (SNI).
579//
580// https://tools.ietf.org/html/rfc6066#section-3.
Adam Langley614c66a2015-06-12 15:26:58 -0700581
David Benjamin8c880a22016-12-03 02:20:34 -0500582static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
583 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700584 if (ssl->tlsext_hostname == NULL) {
585 return 1;
586 }
587
588 CBB contents, server_name_list, name;
589 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
590 !CBB_add_u16_length_prefixed(out, &contents) ||
591 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
592 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
593 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
594 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
595 strlen(ssl->tlsext_hostname)) ||
596 !CBB_flush(out)) {
597 return 0;
598 }
599
600 return 1;
601}
602
David Benjamin8c880a22016-12-03 02:20:34 -0500603static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500604 CBS *contents) {
David Benjamina8614602017-09-06 15:40:19 -0400605 // The server may acknowledge SNI with an empty extension. We check the syntax
606 // but otherwise ignore this signal.
607 return contents == NULL || CBS_len(contents) == 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700608}
609
David Benjamin8c880a22016-12-03 02:20:34 -0500610static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500611 CBS *contents) {
David Benjamina8614602017-09-06 15:40:19 -0400612 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700613 if (contents == NULL) {
614 return 1;
615 }
616
David Benjamin9b611e22016-03-03 08:48:30 -0500617 CBS server_name_list, host_name;
618 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700619 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500620 !CBS_get_u8(&server_name_list, &name_type) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400621 // Although the server_name extension was intended to be extensible to
622 // new name types and multiple names, OpenSSL 1.0.x had a bug which meant
623 // different name types will cause an error. Further, RFC 4366 originally
624 // defined syntax inextensibly. RFC 6066 corrected this mistake, but
625 // adding new name types is no longer feasible.
626 //
627 // Act as if the extensibility does not exist to simplify parsing.
David Benjamin9b611e22016-03-03 08:48:30 -0500628 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
629 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700630 CBS_len(contents) != 0) {
631 return 0;
632 }
633
David Benjamin9b611e22016-03-03 08:48:30 -0500634 if (name_type != TLSEXT_NAMETYPE_host_name ||
635 CBS_len(&host_name) == 0 ||
636 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
637 CBS_contains_zero_byte(&host_name)) {
638 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
639 return 0;
640 }
Adam Langley614c66a2015-06-12 15:26:58 -0700641
David Benjaminc11ea9422017-08-29 16:33:21 -0400642 // Copy the hostname as a string.
David Benjamina8614602017-09-06 15:40:19 -0400643 if (!CBS_strdup(&host_name, &ssl->s3->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900644 *out_alert = SSL_AD_INTERNAL_ERROR;
645 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700646 }
647
David Benjaminfd45ee72017-08-31 14:49:09 -0400648 hs->should_ack_sni = true;
Adam Langley614c66a2015-06-12 15:26:58 -0700649 return 1;
650}
651
David Benjamin8c880a22016-12-03 02:20:34 -0500652static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
653 if (hs->ssl->s3->session_reused ||
654 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700655 return 1;
656 }
657
658 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
659 !CBB_add_u16(out, 0 /* length */)) {
660 return 0;
661 }
662
663 return 1;
664}
665
666
David Benjaminc11ea9422017-08-29 16:33:21 -0400667// Renegotiation indication.
668//
669// https://tools.ietf.org/html/rfc5746
Adam Langley5021b222015-06-12 18:27:58 -0700670
David Benjamin8c880a22016-12-03 02:20:34 -0500671static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
672 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400673 // Renegotiation indication is not necessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -0400674 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400675 return 1;
676 }
677
David Benjamin52bf6902016-10-08 12:05:03 -0400678 assert(ssl->s3->initial_handshake_complete ==
679 (ssl->s3->previous_client_finished_len != 0));
680
Adam Langley5021b222015-06-12 18:27:58 -0700681 CBB contents, prev_finished;
682 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
683 !CBB_add_u16_length_prefixed(out, &contents) ||
684 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
685 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
686 ssl->s3->previous_client_finished_len) ||
687 !CBB_flush(out)) {
688 return 0;
689 }
690
691 return 1;
692}
693
David Benjamin8c880a22016-12-03 02:20:34 -0500694static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700695 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500696 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400697 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez246eeee2017-03-26 12:49:17 -0500698 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400699 return 0;
700 }
701
David Benjaminc11ea9422017-08-29 16:33:21 -0400702 // Servers may not switch between omitting the extension and supporting it.
703 // See RFC 5746, sections 3.5 and 4.2.
David Benjamin3e052de2015-11-25 20:10:31 -0500704 if (ssl->s3->initial_handshake_complete &&
705 (contents != NULL) != ssl->s3->send_connection_binding) {
706 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
707 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
708 return 0;
709 }
710
Adam Langley5021b222015-06-12 18:27:58 -0700711 if (contents == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400712 // Strictly speaking, if we want to avoid an attack we should *always* see
713 // RI even on initial ServerHello because the client doesn't see any
714 // renegotiation during an attack. However this would mean we could not
715 // connect to any server which doesn't support RI.
716 //
717 // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
718 // practical terms every client sets it so it's just assumed here.
David Benjamine9cddb82015-11-23 14:36:40 -0500719 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700720 }
721
722 const size_t expected_len = ssl->s3->previous_client_finished_len +
723 ssl->s3->previous_server_finished_len;
724
David Benjaminc11ea9422017-08-29 16:33:21 -0400725 // Check for logic errors
Adam Langley5021b222015-06-12 18:27:58 -0700726 assert(!expected_len || ssl->s3->previous_client_finished_len);
727 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400728 assert(ssl->s3->initial_handshake_complete ==
729 (ssl->s3->previous_client_finished_len != 0));
730 assert(ssl->s3->initial_handshake_complete ==
731 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700732
David Benjaminc11ea9422017-08-29 16:33:21 -0400733 // Parse out the extension contents.
Adam Langley5021b222015-06-12 18:27:58 -0700734 CBS renegotiated_connection;
735 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
736 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400737 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700738 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
739 return 0;
740 }
741
David Benjaminc11ea9422017-08-29 16:33:21 -0400742 // Check that the extension matches.
Adam Langley5021b222015-06-12 18:27:58 -0700743 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400744 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700745 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
746 return 0;
747 }
748
749 const uint8_t *d = CBS_data(&renegotiated_connection);
David Benjamin9343b0b2017-07-01 00:31:27 -0400750 int ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
751 ssl->s3->previous_client_finished_len) == 0;
752#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
753 ok = 1;
754#endif
755 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400756 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700757 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
758 return 0;
759 }
760 d += ssl->s3->previous_client_finished_len;
761
David Benjamin9343b0b2017-07-01 00:31:27 -0400762 ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
763 ssl->s3->previous_server_finished_len) == 0;
764#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
765 ok = 1;
766#endif
767 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400768 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley10e10602017-07-25 13:33:21 -0700769 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Adam Langley5021b222015-06-12 18:27:58 -0700770 return 0;
771 }
David Benjamin046bc1f2017-08-31 15:06:42 -0400772 ssl->s3->send_connection_binding = true;
Adam Langley5021b222015-06-12 18:27:58 -0700773
774 return 1;
775}
776
David Benjamin8c880a22016-12-03 02:20:34 -0500777static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700778 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500779 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400780 // Renegotiation isn't supported as a server so this function should never be
781 // called after the initial handshake.
Adam Langley5021b222015-06-12 18:27:58 -0700782 assert(!ssl->s3->initial_handshake_complete);
783
Steven Valdez143e8b32016-07-11 13:19:03 -0400784 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
785 return 1;
786 }
787
Adam Langley5021b222015-06-12 18:27:58 -0700788 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400789 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700790 }
791
792 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700793 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
794 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400795 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700796 return 0;
797 }
798
David Benjaminc11ea9422017-08-29 16:33:21 -0400799 // Check that the extension matches. We do not support renegotiation as a
800 // server, so this must be empty.
David Benjamin52bf6902016-10-08 12:05:03 -0400801 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400802 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700803 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
804 return 0;
805 }
806
David Benjamin046bc1f2017-08-31 15:06:42 -0400807 ssl->s3->send_connection_binding = true;
Adam Langley5021b222015-06-12 18:27:58 -0700808
809 return 1;
810}
811
David Benjamin8c880a22016-12-03 02:20:34 -0500812static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
813 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400814 // Renegotiation isn't supported as a server so this function should never be
815 // called after the initial handshake.
David Benjamin52bf6902016-10-08 12:05:03 -0400816 assert(!ssl->s3->initial_handshake_complete);
817
Steven Valdez143e8b32016-07-11 13:19:03 -0400818 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
819 return 1;
820 }
821
Adam Langley5021b222015-06-12 18:27:58 -0700822 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400823 !CBB_add_u16(out, 1 /* length */) ||
824 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700825 return 0;
826 }
827
828 return 1;
829}
830
Adam Langley0a056712015-07-01 15:03:33 -0700831
David Benjaminc11ea9422017-08-29 16:33:21 -0400832// Extended Master Secret.
833//
834// https://tools.ietf.org/html/rfc7627
Adam Langley0a056712015-07-01 15:03:33 -0700835
David Benjamin8c880a22016-12-03 02:20:34 -0500836static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400837 // Extended master secret is not necessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -0400838 if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700839 return 1;
840 }
841
842 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
843 !CBB_add_u16(out, 0 /* length */)) {
844 return 0;
845 }
846
847 return 1;
848}
849
David Benjamin8c880a22016-12-03 02:20:34 -0500850static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700851 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500852 SSL *const ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500853
854 if (contents != NULL) {
855 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
856 ssl->version == SSL3_VERSION ||
857 CBS_len(contents) != 0) {
David Benjamin163c9562016-08-29 23:14:17 -0400858 return 0;
859 }
860
David Benjaminfd45ee72017-08-31 14:49:09 -0400861 hs->extended_master_secret = true;
David Benjamin163c9562016-08-29 23:14:17 -0400862 }
863
David Benjaminc11ea9422017-08-29 16:33:21 -0400864 // Whether EMS is negotiated may not change on renegotiation.
David Benjaminfc02b592017-02-17 16:26:01 -0500865 if (ssl->s3->established_session != NULL &&
866 hs->extended_master_secret !=
David Benjaminfd45ee72017-08-31 14:49:09 -0400867 !!ssl->s3->established_session->extended_master_secret) {
David Benjaminfc02b592017-02-17 16:26:01 -0500868 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
869 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez143e8b32016-07-11 13:19:03 -0400870 return 0;
871 }
872
Adam Langley0a056712015-07-01 15:03:33 -0700873 return 1;
874}
875
David Benjamin8c880a22016-12-03 02:20:34 -0500876static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500877 CBS *contents) {
David Benjaminfc02b592017-02-17 16:26:01 -0500878 uint16_t version = ssl3_protocol_version(hs->ssl);
David Benjamin8c880a22016-12-03 02:20:34 -0500879 if (version >= TLS1_3_VERSION ||
880 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400881 return 1;
882 }
883
884 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700885 return 1;
886 }
887
888 if (CBS_len(contents) != 0) {
889 return 0;
890 }
891
David Benjaminfd45ee72017-08-31 14:49:09 -0400892 hs->extended_master_secret = true;
Adam Langley0a056712015-07-01 15:03:33 -0700893 return 1;
894}
895
David Benjamin8c880a22016-12-03 02:20:34 -0500896static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -0500897 if (!hs->extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700898 return 1;
899 }
900
901 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
902 !CBB_add_u16(out, 0 /* length */)) {
903 return 0;
904 }
905
906 return 1;
907}
908
Adam Langley9b05bc52015-07-01 15:25:33 -0700909
David Benjaminc11ea9422017-08-29 16:33:21 -0400910// Session tickets.
911//
912// https://tools.ietf.org/html/rfc5077
Adam Langley9b05bc52015-07-01 15:25:33 -0700913
David Benjamin8c880a22016-12-03 02:20:34 -0500914static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
915 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400916 // TLS 1.3 uses a different ticket extension.
David Benjamin68161cb2017-06-20 14:49:43 -0400917 if (hs->min_version >= TLS1_3_VERSION ||
David Benjamin7c7d8312016-08-20 13:39:03 -0400918 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700919 return 1;
920 }
921
922 const uint8_t *ticket_data = NULL;
923 int ticket_len = 0;
924
David Benjaminc11ea9422017-08-29 16:33:21 -0400925 // Renegotiation does not participate in session resumption. However, still
926 // advertise the extension to avoid potentially breaking servers which carry
927 // over the state from the previous handshake, such as OpenSSL servers
928 // without upstream's 3c3f0259238594d77264a78944d409f2127642c4.
Adam Langley9b05bc52015-07-01 15:25:33 -0700929 if (!ssl->s3->initial_handshake_complete &&
930 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400931 ssl->session->tlsext_tick != NULL &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400932 // Don't send TLS 1.3 session tickets in the ticket extension.
Steven Valdez8f36c512017-06-20 10:55:02 -0400933 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700934 ticket_data = ssl->session->tlsext_tick;
935 ticket_len = ssl->session->tlsext_ticklen;
936 }
937
938 CBB ticket;
939 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
940 !CBB_add_u16_length_prefixed(out, &ticket) ||
941 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
942 !CBB_flush(out)) {
943 return 0;
944 }
945
946 return 1;
947}
948
David Benjamin8c880a22016-12-03 02:20:34 -0500949static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -0700950 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500951 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -0700952 if (contents == NULL) {
953 return 1;
954 }
955
Steven Valdez143e8b32016-07-11 13:19:03 -0400956 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
957 return 0;
958 }
959
David Benjaminc11ea9422017-08-29 16:33:21 -0400960 // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
961 // this function should never be called, even if the server tries to send the
962 // extension.
Adam Langley9b05bc52015-07-01 15:25:33 -0700963 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
964
965 if (CBS_len(contents) != 0) {
966 return 0;
967 }
968
David Benjaminfd45ee72017-08-31 14:49:09 -0400969 hs->ticket_expected = true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700970 return 1;
971}
972
David Benjamin8c880a22016-12-03 02:20:34 -0500973static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
974 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700975 return 1;
976 }
977
David Benjaminc11ea9422017-08-29 16:33:21 -0400978 // If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true.
David Benjamin8c880a22016-12-03 02:20:34 -0500979 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -0700980
981 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
982 !CBB_add_u16(out, 0 /* length */)) {
983 return 0;
984 }
985
986 return 1;
987}
988
989
David Benjaminc11ea9422017-08-29 16:33:21 -0400990// Signature Algorithms.
991//
992// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
Adam Langley2e857bd2015-07-01 16:09:19 -0700993
David Benjamin8c880a22016-12-03 02:20:34 -0500994static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
995 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -0400996 if (hs->max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -0700997 return 1;
998 }
999
David Benjamin0fc37ef2016-08-17 15:29:46 -04001000 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001001 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1002 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin69522112017-03-28 15:38:29 -05001003 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
1004 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
1005 !CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001006 return 0;
1007 }
1008
1009 return 1;
1010}
1011
David Benjamin8c880a22016-12-03 02:20:34 -05001012static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001013 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001014 OPENSSL_free(hs->peer_sigalgs);
1015 hs->peer_sigalgs = NULL;
1016 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001017
Adam Langley2e857bd2015-07-01 16:09:19 -07001018 if (contents == NULL) {
1019 return 1;
1020 }
1021
1022 CBS supported_signature_algorithms;
1023 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001024 CBS_len(contents) != 0 ||
1025 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001026 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001027 return 0;
1028 }
1029
1030 return 1;
1031}
1032
Adam Langley2e857bd2015-07-01 16:09:19 -07001033
David Benjaminc11ea9422017-08-29 16:33:21 -04001034// OCSP Stapling.
1035//
1036// https://tools.ietf.org/html/rfc6066#section-8
Adam Langleybb0bd042015-07-01 16:21:03 -07001037
David Benjamin8c880a22016-12-03 02:20:34 -05001038static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1039 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001040 if (!ssl->ocsp_stapling_enabled) {
1041 return 1;
1042 }
1043
1044 CBB contents;
1045 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1046 !CBB_add_u16_length_prefixed(out, &contents) ||
1047 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1048 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1049 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1050 !CBB_flush(out)) {
1051 return 0;
1052 }
1053
1054 return 1;
1055}
1056
David Benjamin8c880a22016-12-03 02:20:34 -05001057static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001058 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001059 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001060 if (contents == NULL) {
1061 return 1;
1062 }
1063
David Benjaminc11ea9422017-08-29 16:33:21 -04001064 // TLS 1.3 OCSP responses are included in the Certificate extensions.
Steven Valdeza833c352016-11-01 13:39:36 -04001065 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001066 return 0;
1067 }
1068
David Benjaminc11ea9422017-08-29 16:33:21 -04001069 // OCSP stapling is forbidden on non-certificate ciphers.
Steven Valdeza833c352016-11-01 13:39:36 -04001070 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001071 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001072 return 0;
1073 }
1074
David Benjaminc11ea9422017-08-29 16:33:21 -04001075 // Note this does not check for resumption in TLS 1.2. Sending
1076 // status_request here does not make sense, but OpenSSL does so and the
1077 // specification does not say anything. Tolerate it but ignore it.
David Benjamin942f4ed2016-07-16 19:03:49 +03001078
David Benjaminfd45ee72017-08-31 14:49:09 -04001079 hs->certificate_status_expected = true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001080 return 1;
1081}
1082
David Benjamin8c880a22016-12-03 02:20:34 -05001083static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001084 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001085 if (contents == NULL) {
1086 return 1;
1087 }
1088
1089 uint8_t status_type;
1090 if (!CBS_get_u8(contents, &status_type)) {
1091 return 0;
1092 }
1093
David Benjaminc11ea9422017-08-29 16:33:21 -04001094 // We cannot decide whether OCSP stapling will occur yet because the correct
1095 // SSL_CTX might not have been selected.
David Benjamin8c880a22016-12-03 02:20:34 -05001096 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001097
Adam Langleybb0bd042015-07-01 16:21:03 -07001098 return 1;
1099}
1100
David Benjamin8c880a22016-12-03 02:20:34 -05001101static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1102 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001103 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001104 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001105 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001106 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001107 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001108 return 1;
1109 }
1110
David Benjaminfd45ee72017-08-31 14:49:09 -04001111 hs->certificate_status_expected = true;
David Benjamin942f4ed2016-07-16 19:03:49 +03001112
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001113 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001114 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001115}
1116
1117
David Benjaminc11ea9422017-08-29 16:33:21 -04001118// Next protocol negotiation.
1119//
1120// https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
Adam Langley97dfcbf2015-07-01 18:35:20 -07001121
David Benjamin8c880a22016-12-03 02:20:34 -05001122static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1123 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001124 if (ssl->s3->initial_handshake_complete ||
1125 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001126 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001127 return 1;
1128 }
1129
1130 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1131 !CBB_add_u16(out, 0 /* length */)) {
1132 return 0;
1133 }
1134
1135 return 1;
1136}
1137
David Benjamin8c880a22016-12-03 02:20:34 -05001138static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001139 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001140 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001141 if (contents == NULL) {
1142 return 1;
1143 }
1144
Steven Valdez143e8b32016-07-11 13:19:03 -04001145 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1146 return 0;
1147 }
1148
David Benjaminc11ea9422017-08-29 16:33:21 -04001149 // If any of these are false then we should never have sent the NPN
1150 // extension in the ClientHello and thus this function should never have been
1151 // called.
Adam Langley97dfcbf2015-07-01 18:35:20 -07001152 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001153 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001154 assert(ssl->ctx->next_proto_select_cb != NULL);
1155
David Benjamin76c2efc2015-08-31 14:24:29 -04001156 if (ssl->s3->alpn_selected != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001157 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001158 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1159 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1160 return 0;
1161 }
1162
Adam Langley97dfcbf2015-07-01 18:35:20 -07001163 const uint8_t *const orig_contents = CBS_data(contents);
1164 const size_t orig_len = CBS_len(contents);
1165
1166 while (CBS_len(contents) != 0) {
1167 CBS proto;
1168 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1169 CBS_len(&proto) == 0) {
1170 return 0;
1171 }
1172 }
1173
1174 uint8_t *selected;
1175 uint8_t selected_len;
1176 if (ssl->ctx->next_proto_select_cb(
1177 ssl, &selected, &selected_len, orig_contents, orig_len,
1178 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1179 *out_alert = SSL_AD_INTERNAL_ERROR;
1180 return 0;
1181 }
1182
David Benjamin79978df2015-12-25 15:56:49 -05001183 OPENSSL_free(ssl->s3->next_proto_negotiated);
David Benjamin81678aa2017-07-12 22:43:42 -04001184 ssl->s3->next_proto_negotiated =
1185 (uint8_t *)BUF_memdup(selected, selected_len);
David Benjamin79978df2015-12-25 15:56:49 -05001186 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001187 *out_alert = SSL_AD_INTERNAL_ERROR;
1188 return 0;
1189 }
1190
David Benjamin79978df2015-12-25 15:56:49 -05001191 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjaminfd45ee72017-08-31 14:49:09 -04001192 hs->next_proto_neg_seen = true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001193
1194 return 1;
1195}
1196
David Benjamin8c880a22016-12-03 02:20:34 -05001197static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001198 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001199 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001200 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1201 return 1;
1202 }
1203
Adam Langley97dfcbf2015-07-01 18:35:20 -07001204 if (contents != NULL && CBS_len(contents) != 0) {
1205 return 0;
1206 }
1207
1208 if (contents == NULL ||
1209 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001210 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001211 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001212 return 1;
1213 }
1214
David Benjaminfd45ee72017-08-31 14:49:09 -04001215 hs->next_proto_neg_seen = true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001216 return 1;
1217}
1218
David Benjamin8c880a22016-12-03 02:20:34 -05001219static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1220 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001221 // |next_proto_neg_seen| might have been cleared when an ALPN extension was
1222 // parsed.
David Benjamin8c880a22016-12-03 02:20:34 -05001223 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001224 return 1;
1225 }
1226
1227 const uint8_t *npa;
1228 unsigned npa_len;
1229
1230 if (ssl->ctx->next_protos_advertised_cb(
1231 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1232 SSL_TLSEXT_ERR_OK) {
David Benjaminfd45ee72017-08-31 14:49:09 -04001233 hs->next_proto_neg_seen = false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001234 return 1;
1235 }
1236
1237 CBB contents;
1238 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1239 !CBB_add_u16_length_prefixed(out, &contents) ||
1240 !CBB_add_bytes(&contents, npa, npa_len) ||
1241 !CBB_flush(out)) {
1242 return 0;
1243 }
1244
1245 return 1;
1246}
1247
1248
David Benjaminc11ea9422017-08-29 16:33:21 -04001249// Signed certificate timestamps.
1250//
1251// https://tools.ietf.org/html/rfc6962#section-3.3.1
Adam Langleyab8d87d2015-07-10 12:21:39 -07001252
David Benjamin8c880a22016-12-03 02:20:34 -05001253static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1254 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001255 if (!ssl->signed_cert_timestamps_enabled) {
1256 return 1;
1257 }
1258
1259 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1260 !CBB_add_u16(out, 0 /* length */)) {
1261 return 0;
1262 }
1263
1264 return 1;
1265}
1266
David Benjamin8c880a22016-12-03 02:20:34 -05001267static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001268 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001269 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001270 if (contents == NULL) {
1271 return 1;
1272 }
1273
David Benjaminc11ea9422017-08-29 16:33:21 -04001274 // TLS 1.3 SCTs are included in the Certificate extensions.
Steven Valdeza833c352016-11-01 13:39:36 -04001275 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001276 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001277 return 0;
1278 }
1279
David Benjaminc11ea9422017-08-29 16:33:21 -04001280 // If this is false then we should never have sent the SCT extension in the
1281 // ClientHello and thus this function should never have been called.
Adam Langleyab8d87d2015-07-10 12:21:39 -07001282 assert(ssl->signed_cert_timestamps_enabled);
1283
Adam Langleycfa08c32016-11-17 13:21:27 -08001284 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001285 *out_alert = SSL_AD_DECODE_ERROR;
1286 return 0;
1287 }
1288
David Benjaminc11ea9422017-08-29 16:33:21 -04001289 // Session resumption uses the original session information. The extension
1290 // should not be sent on resumption, but RFC 6962 did not make it a
1291 // requirement, so tolerate this.
1292 //
1293 // TODO(davidben): Enforce this anyway.
David Benjamin8fc2dc02017-08-22 15:07:51 -07001294 if (!ssl->s3->session_reused) {
1295 CRYPTO_BUFFER_free(hs->new_session->signed_cert_timestamp_list);
1296 hs->new_session->signed_cert_timestamp_list =
1297 CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool);
1298 if (hs->new_session->signed_cert_timestamp_list == nullptr) {
1299 *out_alert = SSL_AD_INTERNAL_ERROR;
1300 return 0;
1301 }
Adam Langleyab8d87d2015-07-10 12:21:39 -07001302 }
1303
1304 return 1;
1305}
1306
David Benjamin8c880a22016-12-03 02:20:34 -05001307static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001308 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001309 if (contents == NULL) {
1310 return 1;
1311 }
1312
1313 if (CBS_len(contents) != 0) {
1314 return 0;
1315 }
1316
David Benjaminfd45ee72017-08-31 14:49:09 -04001317 hs->scts_requested = true;
David Benjamin53210cb2016-11-16 09:01:48 +09001318 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001319}
1320
David Benjamin8c880a22016-12-03 02:20:34 -05001321static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1322 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001323 // The extension shouldn't be sent when resuming sessions.
Steven Valdeza833c352016-11-01 13:39:36 -04001324 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1325 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001326 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001327 return 1;
1328 }
1329
1330 CBB contents;
1331 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1332 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001333 CBB_add_bytes(
1334 &contents,
1335 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1336 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001337 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001338}
1339
1340
David Benjaminc11ea9422017-08-29 16:33:21 -04001341// Application-level Protocol Negotiation.
1342//
1343// https://tools.ietf.org/html/rfc7301
Adam Langleyf18e4532015-07-10 13:39:53 -07001344
David Benjamin8c880a22016-12-03 02:20:34 -05001345static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1346 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001347 if (ssl->alpn_client_proto_list == NULL ||
1348 ssl->s3->initial_handshake_complete) {
1349 return 1;
1350 }
1351
1352 CBB contents, proto_list;
1353 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1354 !CBB_add_u16_length_prefixed(out, &contents) ||
1355 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1356 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1357 ssl->alpn_client_proto_list_len) ||
1358 !CBB_flush(out)) {
1359 return 0;
1360 }
1361
1362 return 1;
1363}
1364
David Benjamin8c880a22016-12-03 02:20:34 -05001365static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001366 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001367 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001368 if (contents == NULL) {
1369 return 1;
1370 }
1371
1372 assert(!ssl->s3->initial_handshake_complete);
1373 assert(ssl->alpn_client_proto_list != NULL);
1374
David Benjamin8c880a22016-12-03 02:20:34 -05001375 if (hs->next_proto_neg_seen) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001376 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001377 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1378 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1379 return 0;
1380 }
1381
David Benjaminc11ea9422017-08-29 16:33:21 -04001382 // The extension data consists of a ProtocolNameList which must have
1383 // exactly one ProtocolName. Each of these is length-prefixed.
Adam Langleyf18e4532015-07-10 13:39:53 -07001384 CBS protocol_name_list, protocol_name;
1385 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1386 CBS_len(contents) != 0 ||
1387 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001388 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001389 CBS_len(&protocol_name) == 0 ||
1390 CBS_len(&protocol_name_list) != 0) {
1391 return 0;
1392 }
1393
David Benjaminc8ff30c2017-04-04 13:52:36 -04001394 if (!ssl->ctx->allow_unknown_alpn_protos) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001395 // Check that the protocol name is one of the ones we advertised.
David Benjaminc8ff30c2017-04-04 13:52:36 -04001396 int protocol_ok = 0;
1397 CBS client_protocol_name_list, client_protocol_name;
1398 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1399 ssl->alpn_client_proto_list_len);
1400 while (CBS_len(&client_protocol_name_list) > 0) {
1401 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1402 &client_protocol_name)) {
1403 *out_alert = SSL_AD_INTERNAL_ERROR;
1404 return 0;
1405 }
1406
1407 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1408 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1409 CBS_data(&protocol_name),
1410 CBS_len(&protocol_name)) == 0) {
1411 protocol_ok = 1;
1412 break;
1413 }
1414 }
1415
1416 if (!protocol_ok) {
1417 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1418 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3e517572016-08-11 11:52:23 -04001419 return 0;
1420 }
David Benjamin3e517572016-08-11 11:52:23 -04001421 }
1422
Adam Langleyf18e4532015-07-10 13:39:53 -07001423 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1424 &ssl->s3->alpn_selected_len)) {
1425 *out_alert = SSL_AD_INTERNAL_ERROR;
1426 return 0;
1427 }
1428
1429 return 1;
1430}
1431
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001432int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001433 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001434 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001435 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001436 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001437 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001438 client_hello, &contents,
1439 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001440 // Ignore ALPN if not configured or no extension was supplied.
Adam Langleyf18e4532015-07-10 13:39:53 -07001441 return 1;
1442 }
1443
David Benjaminc11ea9422017-08-29 16:33:21 -04001444 // ALPN takes precedence over NPN.
David Benjaminfd45ee72017-08-31 14:49:09 -04001445 hs->next_proto_neg_seen = false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001446
1447 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001448 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1449 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001450 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001451 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1452 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001453 return 0;
1454 }
1455
David Benjaminc11ea9422017-08-29 16:33:21 -04001456 // Validate the protocol list.
Adam Langleyf18e4532015-07-10 13:39:53 -07001457 CBS protocol_name_list_copy = protocol_name_list;
1458 while (CBS_len(&protocol_name_list_copy) > 0) {
1459 CBS protocol_name;
1460
1461 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001462 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001463 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001464 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1465 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001466 return 0;
1467 }
1468 }
1469
1470 const uint8_t *selected;
1471 uint8_t selected_len;
1472 if (ssl->ctx->alpn_select_cb(
1473 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1474 CBS_len(&protocol_name_list),
1475 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1476 OPENSSL_free(ssl->s3->alpn_selected);
David Benjamin81678aa2017-07-12 22:43:42 -04001477 ssl->s3->alpn_selected = (uint8_t *)BUF_memdup(selected, selected_len);
Adam Langleyf18e4532015-07-10 13:39:53 -07001478 if (ssl->s3->alpn_selected == NULL) {
1479 *out_alert = SSL_AD_INTERNAL_ERROR;
1480 return 0;
1481 }
1482 ssl->s3->alpn_selected_len = selected_len;
1483 }
1484
1485 return 1;
1486}
1487
David Benjamin8c880a22016-12-03 02:20:34 -05001488static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1489 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001490 if (ssl->s3->alpn_selected == NULL) {
1491 return 1;
1492 }
1493
1494 CBB contents, proto_list, proto;
1495 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1496 !CBB_add_u16_length_prefixed(out, &contents) ||
1497 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1498 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001499 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1500 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001501 !CBB_flush(out)) {
1502 return 0;
1503 }
1504
1505 return 1;
1506}
1507
1508
David Benjaminc11ea9422017-08-29 16:33:21 -04001509// Channel ID.
1510//
1511// https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
Adam Langley49c7af12015-07-10 14:33:46 -07001512
David Benjamin8c880a22016-12-03 02:20:34 -05001513static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
David Benjamin046bc1f2017-08-31 15:06:42 -04001514 hs->ssl->s3->tlsext_channel_id_valid = false;
Adam Langley49c7af12015-07-10 14:33:46 -07001515}
1516
David Benjamin8c880a22016-12-03 02:20:34 -05001517static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1518 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001519 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001520 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001521 return 1;
1522 }
1523
1524 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1525 !CBB_add_u16(out, 0 /* length */)) {
1526 return 0;
1527 }
1528
1529 return 1;
1530}
1531
David Benjamin8c880a22016-12-03 02:20:34 -05001532static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1533 uint8_t *out_alert, CBS *contents) {
1534 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001535 if (contents == NULL) {
1536 return 1;
1537 }
1538
David Benjamince079fd2016-08-02 16:22:34 -04001539 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001540 assert(ssl->tlsext_channel_id_enabled);
1541
1542 if (CBS_len(contents) != 0) {
1543 return 0;
1544 }
1545
David Benjamin046bc1f2017-08-31 15:06:42 -04001546 ssl->s3->tlsext_channel_id_valid = true;
Adam Langley49c7af12015-07-10 14:33:46 -07001547 return 1;
1548}
1549
David Benjamin8c880a22016-12-03 02:20:34 -05001550static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1551 uint8_t *out_alert, CBS *contents) {
1552 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001553 if (contents == NULL ||
1554 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001555 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001556 return 1;
1557 }
1558
1559 if (CBS_len(contents) != 0) {
1560 return 0;
1561 }
1562
David Benjamin046bc1f2017-08-31 15:06:42 -04001563 ssl->s3->tlsext_channel_id_valid = true;
Adam Langley49c7af12015-07-10 14:33:46 -07001564 return 1;
1565}
1566
David Benjamin8c880a22016-12-03 02:20:34 -05001567static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1568 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001569 if (!ssl->s3->tlsext_channel_id_valid) {
1570 return 1;
1571 }
1572
1573 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1574 !CBB_add_u16(out, 0 /* length */)) {
1575 return 0;
1576 }
1577
1578 return 1;
1579}
1580
Adam Langley391250d2015-07-15 19:06:07 -07001581
David Benjaminc11ea9422017-08-29 16:33:21 -04001582// Secure Real-time Transport Protocol (SRTP) extension.
1583//
1584// https://tools.ietf.org/html/rfc5764
Adam Langley391250d2015-07-15 19:06:07 -07001585
Adam Langley391250d2015-07-15 19:06:07 -07001586
David Benjamin8c880a22016-12-03 02:20:34 -05001587static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1588 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001589}
1590
David Benjamin8c880a22016-12-03 02:20:34 -05001591static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1592 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001593 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
David Benjaminee910bf2017-07-25 22:36:00 -04001594 if (profiles == NULL ||
1595 sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
Adam Langley391250d2015-07-15 19:06:07 -07001596 return 1;
1597 }
1598
1599 CBB contents, profile_ids;
1600 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1601 !CBB_add_u16_length_prefixed(out, &contents) ||
1602 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1603 return 0;
1604 }
1605
David Benjaminee910bf2017-07-25 22:36:00 -04001606 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
1607 if (!CBB_add_u16(&profile_ids, profile->id)) {
Adam Langley391250d2015-07-15 19:06:07 -07001608 return 0;
1609 }
1610 }
1611
1612 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1613 !CBB_flush(out)) {
1614 return 0;
1615 }
1616
1617 return 1;
1618}
1619
David Benjamin8c880a22016-12-03 02:20:34 -05001620static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001621 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001622 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001623 if (contents == NULL) {
1624 return 1;
1625 }
1626
David Benjaminc11ea9422017-08-29 16:33:21 -04001627 // The extension consists of a u16-prefixed profile ID list containing a
1628 // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1629 //
1630 // See https://tools.ietf.org/html/rfc5764#section-4.1.1
Adam Langley391250d2015-07-15 19:06:07 -07001631 CBS profile_ids, srtp_mki;
1632 uint16_t profile_id;
1633 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1634 !CBS_get_u16(&profile_ids, &profile_id) ||
1635 CBS_len(&profile_ids) != 0 ||
1636 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1637 CBS_len(contents) != 0) {
1638 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1639 return 0;
1640 }
1641
1642 if (CBS_len(&srtp_mki) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001643 // Must be no MKI, since we never offer one.
Adam Langley391250d2015-07-15 19:06:07 -07001644 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1645 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1646 return 0;
1647 }
1648
1649 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1650
David Benjaminc11ea9422017-08-29 16:33:21 -04001651 // Check to see if the server gave us something we support (and presumably
1652 // offered).
David Benjaminee910bf2017-07-25 22:36:00 -04001653 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001654 if (profile->id == profile_id) {
1655 ssl->srtp_profile = profile;
1656 return 1;
1657 }
1658 }
1659
1660 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1661 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1662 return 0;
1663}
1664
David Benjamin8c880a22016-12-03 02:20:34 -05001665static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001666 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001667 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001668 if (contents == NULL) {
1669 return 1;
1670 }
1671
1672 CBS profile_ids, srtp_mki;
1673 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1674 CBS_len(&profile_ids) < 2 ||
1675 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1676 CBS_len(contents) != 0) {
1677 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1678 return 0;
1679 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001680 // Discard the MKI value for now.
Adam Langley391250d2015-07-15 19:06:07 -07001681
1682 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1683 SSL_get_srtp_profiles(ssl);
1684
David Benjaminc11ea9422017-08-29 16:33:21 -04001685 // Pick the server's most preferred profile.
David Benjaminee910bf2017-07-25 22:36:00 -04001686 for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001687 CBS profile_ids_tmp;
1688 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1689
1690 while (CBS_len(&profile_ids_tmp) > 0) {
1691 uint16_t profile_id;
1692 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1693 return 0;
1694 }
1695
1696 if (server_profile->id == profile_id) {
1697 ssl->srtp_profile = server_profile;
1698 return 1;
1699 }
1700 }
1701 }
1702
1703 return 1;
1704}
1705
David Benjamin8c880a22016-12-03 02:20:34 -05001706static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1707 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001708 if (ssl->srtp_profile == NULL) {
1709 return 1;
1710 }
1711
1712 CBB contents, profile_ids;
1713 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1714 !CBB_add_u16_length_prefixed(out, &contents) ||
1715 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1716 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1717 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1718 !CBB_flush(out)) {
1719 return 0;
1720 }
1721
1722 return 1;
1723}
1724
Adam Langleybdd5d662015-07-20 16:19:08 -07001725
David Benjaminc11ea9422017-08-29 16:33:21 -04001726// EC point formats.
1727//
1728// https://tools.ietf.org/html/rfc4492#section-5.1.2
Adam Langleybdd5d662015-07-20 16:19:08 -07001729
David Benjamin8c880a22016-12-03 02:20:34 -05001730static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001731 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001732 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1733 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001734 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1735 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001736 !CBB_flush(out)) {
1737 return 0;
1738 }
1739
1740 return 1;
1741}
1742
David Benjamin8c880a22016-12-03 02:20:34 -05001743static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001744 // The point format extension is unneccessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -04001745 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin70aba262016-11-01 12:08:15 -04001746 return 1;
1747 }
1748
David Benjamin8c880a22016-12-03 02:20:34 -05001749 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001750}
1751
David Benjamin8c880a22016-12-03 02:20:34 -05001752static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001753 CBS *contents) {
1754 if (contents == NULL) {
1755 return 1;
1756 }
1757
David Benjamin8c880a22016-12-03 02:20:34 -05001758 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001759 return 0;
1760 }
1761
Adam Langleybdd5d662015-07-20 16:19:08 -07001762 CBS ec_point_format_list;
1763 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1764 CBS_len(contents) != 0) {
1765 return 0;
1766 }
1767
David Benjaminc11ea9422017-08-29 16:33:21 -04001768 // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1769 // point format.
David Benjamin17cf2cb2016-12-13 01:07:13 -05001770 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1771 TLSEXT_ECPOINTFORMAT_uncompressed,
1772 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001773 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001774 return 0;
1775 }
1776
1777 return 1;
1778}
1779
David Benjamin8c880a22016-12-03 02:20:34 -05001780static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001781 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001782 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001783 return 1;
1784 }
1785
David Benjamin8c880a22016-12-03 02:20:34 -05001786 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001787}
1788
David Benjamin8c880a22016-12-03 02:20:34 -05001789static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1790 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001791 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1792 return 1;
1793 }
1794
David Benjamin45738dd2017-02-09 20:01:26 -05001795 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1796 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001797 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001798
1799 if (!using_ecc) {
1800 return 1;
1801 }
1802
David Benjamin8c880a22016-12-03 02:20:34 -05001803 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001804}
1805
Steven Valdeza833c352016-11-01 13:39:36 -04001806
David Benjaminc11ea9422017-08-29 16:33:21 -04001807// Pre Shared Key
1808//
1809// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
Steven Valdez4aa154e2016-07-29 14:32:55 -04001810
David Benjamin8c880a22016-12-03 02:20:34 -05001811static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1812 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001813 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001814 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001815 return 0;
1816 }
1817
Steven Valdez8f36c512017-06-20 10:55:02 -04001818 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001819 return 15 + ssl->session->tlsext_ticklen + binder_len;
1820}
1821
David Benjamin8c880a22016-12-03 02:20:34 -05001822static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1823 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001824 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001825 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001826 return 1;
1827 }
1828
David Benjaminad8f5e12017-02-20 17:00:20 -05001829 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001830 ssl_get_current_time(ssl, &now);
1831 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1832 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1833
David Benjaminc11ea9422017-08-29 16:33:21 -04001834 // Fill in a placeholder zero binder of the appropriate length. It will be
1835 // computed and filled in later after length prefixes are computed.
Steven Valdeza833c352016-11-01 13:39:36 -04001836 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez8f36c512017-06-20 10:55:02 -04001837 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001838
1839 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001840 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1841 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001842 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001843 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1844 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001845 ssl->session->tlsext_ticklen) ||
1846 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1847 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1848 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1849 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001850 return 0;
1851 }
1852
David Benjaminfd45ee72017-08-31 14:49:09 -04001853 hs->needs_psk_binder = true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001854 return CBB_flush(out);
1855}
1856
David Benjamin8baf9632016-11-17 17:11:16 +09001857int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1858 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001859 CBS *contents) {
1860 uint16_t psk_id;
1861 if (!CBS_get_u16(contents, &psk_id) ||
1862 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001863 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001864 *out_alert = SSL_AD_DECODE_ERROR;
1865 return 0;
1866 }
1867
David Benjaminc11ea9422017-08-29 16:33:21 -04001868 // We only advertise one PSK identity, so the only legal index is zero.
Steven Valdez4aa154e2016-07-29 14:32:55 -04001869 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001870 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001871 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1872 return 0;
1873 }
1874
1875 return 1;
1876}
1877
David Benjamin35ac5b72017-03-03 15:05:56 -05001878int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001879 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001880 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001881 // We only process the first PSK identity since we don't support pure PSK.
David Benjamin707af292017-03-10 17:47:18 -05001882 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001883 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001884 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001885 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001886 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001887 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001888 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001889 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001890 *out_alert = SSL_AD_DECODE_ERROR;
1891 return 0;
1892 }
1893
Steven Valdeza833c352016-11-01 13:39:36 -04001894 *out_binders = binders;
1895
David Benjaminc11ea9422017-08-29 16:33:21 -04001896 // Check the syntax of the remaining identities, but do not process them.
David Benjaminaedf3032016-12-01 16:47:56 -05001897 size_t num_identities = 1;
1898 while (CBS_len(&identities) != 0) {
1899 CBS unused_ticket;
1900 uint32_t unused_obfuscated_ticket_age;
1901 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1902 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1903 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1904 *out_alert = SSL_AD_DECODE_ERROR;
1905 return 0;
1906 }
1907
1908 num_identities++;
1909 }
1910
David Benjaminc11ea9422017-08-29 16:33:21 -04001911 // Check the syntax of the binders. The value will be checked later if
1912 // resuming.
David Benjaminaedf3032016-12-01 16:47:56 -05001913 size_t num_binders = 0;
1914 while (CBS_len(&binders) != 0) {
1915 CBS binder;
1916 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1917 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1918 *out_alert = SSL_AD_DECODE_ERROR;
1919 return 0;
1920 }
1921
1922 num_binders++;
1923 }
1924
1925 if (num_identities != num_binders) {
1926 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1927 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04001928 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04001929 }
1930
David Benjamine7f60a22016-11-16 18:54:25 +09001931 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001932}
1933
David Benjamin8baf9632016-11-17 17:11:16 +09001934int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1935 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001936 return 1;
1937 }
1938
1939 CBB contents;
1940 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1941 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001942 // We only consider the first identity for resumption
Steven Valdez4aa154e2016-07-29 14:32:55 -04001943 !CBB_add_u16(&contents, 0) ||
1944 !CBB_flush(out)) {
1945 return 0;
1946 }
1947
1948 return 1;
1949}
1950
1951
David Benjaminc11ea9422017-08-29 16:33:21 -04001952// Pre-Shared Key Exchange Modes
1953//
1954// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7
Steven Valdeza4ee74d2016-11-29 13:36:45 -05001955
David Benjamin8c880a22016-12-03 02:20:34 -05001956static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
1957 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04001958 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001959 return 1;
1960 }
1961
1962 CBB contents, ke_modes;
1963 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
1964 !CBB_add_u16_length_prefixed(out, &contents) ||
1965 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
1966 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
1967 return 0;
1968 }
1969
1970 return CBB_flush(out);
1971}
1972
David Benjamin8c880a22016-12-03 02:20:34 -05001973static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09001974 uint8_t *out_alert,
1975 CBS *contents) {
1976 if (contents == NULL) {
1977 return 1;
1978 }
1979
Steven Valdeza833c352016-11-01 13:39:36 -04001980 CBS ke_modes;
1981 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
1982 CBS_len(&ke_modes) == 0 ||
1983 CBS_len(contents) != 0) {
1984 *out_alert = SSL_AD_DECODE_ERROR;
1985 return 0;
1986 }
1987
David Benjaminc11ea9422017-08-29 16:33:21 -04001988 // We only support tickets with PSK_DHE_KE.
David Benjamin17cf2cb2016-12-13 01:07:13 -05001989 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
1990 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04001991
1992 return 1;
1993}
1994
1995
David Benjaminc11ea9422017-08-29 16:33:21 -04001996// Early Data Indication
1997//
1998// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
Steven Valdeza4ee74d2016-11-29 13:36:45 -05001999
David Benjamin8c880a22016-12-03 02:20:34 -05002000static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002001 SSL *const ssl = hs->ssl;
Steven Valdez2d850622017-01-11 11:34:52 -05002002 if (ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04002003 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION ||
Steven Valdez2d850622017-01-11 11:34:52 -05002004 ssl->session->ticket_max_early_data == 0 ||
2005 hs->received_hello_retry_request ||
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05002006 !ssl->cert->enable_early_data) {
Steven Valdez2d850622017-01-11 11:34:52 -05002007 return 1;
2008 }
2009
David Benjaminfd45ee72017-08-31 14:49:09 -04002010 hs->early_data_offered = true;
Steven Valdez2d850622017-01-11 11:34:52 -05002011
2012 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2013 !CBB_add_u16(out, 0) ||
2014 !CBB_flush(out)) {
2015 return 0;
2016 }
2017
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002018 return 1;
2019}
2020
Steven Valdez2d850622017-01-11 11:34:52 -05002021static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002022 uint8_t *out_alert, CBS *contents) {
2023 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002024 if (contents == NULL) {
2025 return 1;
2026 }
2027
2028 if (CBS_len(contents) != 0) {
2029 *out_alert = SSL_AD_DECODE_ERROR;
2030 return 0;
2031 }
2032
Steven Valdez2d850622017-01-11 11:34:52 -05002033 if (!ssl->s3->session_reused) {
2034 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2035 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2036 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002037 }
Steven Valdez2d850622017-01-11 11:34:52 -05002038
2039 ssl->early_data_accepted = 1;
2040 return 1;
2041}
2042
2043static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2044 uint8_t *out_alert, CBS *contents) {
2045 SSL *const ssl = hs->ssl;
2046 if (contents == NULL ||
2047 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2048 return 1;
2049 }
2050
2051 if (CBS_len(contents) != 0) {
2052 *out_alert = SSL_AD_DECODE_ERROR;
2053 return 0;
2054 }
2055
David Benjaminfd45ee72017-08-31 14:49:09 -04002056 hs->early_data_offered = true;
Steven Valdez2d850622017-01-11 11:34:52 -05002057 return 1;
2058}
2059
2060static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2061 if (!hs->ssl->early_data_accepted) {
2062 return 1;
2063 }
2064
2065 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2066 !CBB_add_u16(out, 0) ||
2067 !CBB_flush(out)) {
2068 return 0;
2069 }
2070
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002071 return 1;
2072}
2073
2074
David Benjaminc11ea9422017-08-29 16:33:21 -04002075// Key Share
2076//
2077// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5
Steven Valdez143e8b32016-07-11 13:19:03 -04002078
David Benjamin8c880a22016-12-03 02:20:34 -05002079static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2080 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002081 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002082 return 1;
2083 }
2084
2085 CBB contents, kse_bytes;
2086 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2087 !CBB_add_u16_length_prefixed(out, &contents) ||
2088 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2089 return 0;
2090 }
2091
David Benjamin8c880a22016-12-03 02:20:34 -05002092 uint16_t group_id = hs->retry_group;
2093 if (hs->received_hello_retry_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002094 // We received a HelloRetryRequest without a new curve, so there is no new
2095 // share to append. Leave |hs->key_share| as-is.
Steven Valdeza833c352016-11-01 13:39:36 -04002096 if (group_id == 0 &&
David Benjamin08f5c762017-09-21 02:43:05 -04002097 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(),
2098 hs->key_share_bytes.size())) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002099 return 0;
2100 }
David Benjamin08f5c762017-09-21 02:43:05 -04002101 hs->key_share_bytes.Reset();
David Benjamin3baa6e12016-10-07 21:10:38 -04002102 if (group_id == 0) {
2103 return CBB_flush(out);
2104 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002105 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04002106 // Add a fake group. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002107 if (ssl->ctx->grease_enabled &&
2108 (!CBB_add_u16(&kse_bytes,
2109 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2110 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2111 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2112 return 0;
2113 }
2114
David Benjaminc11ea9422017-08-29 16:33:21 -04002115 // Predict the most preferred group.
David Benjamincf0ce672017-09-21 02:25:59 -04002116 Span<const uint16_t> groups = tls1_get_grouplist(ssl);
2117 if (groups.size() == 0) {
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002118 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2119 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002120 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002121
2122 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002123 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002124
David Benjaminc642aca2017-07-19 23:28:43 -04002125 hs->key_share = SSLKeyShare::Create(group_id);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002126 CBB key_exchange;
David Benjaminc642aca2017-07-19 23:28:43 -04002127 if (!hs->key_share ||
2128 !CBB_add_u16(&kse_bytes, group_id) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002129 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjaminc642aca2017-07-19 23:28:43 -04002130 !hs->key_share->Offer(&key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002131 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002132 return 0;
2133 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002134
David Benjamin08f5c762017-09-21 02:43:05 -04002135 // Save the contents of the extension to repeat it in the second ClientHello.
2136 if (!hs->received_hello_retry_request &&
2137 !hs->key_share_bytes.CopyFrom(
2138 MakeConstSpan(CBB_data(&kse_bytes), CBB_len(&kse_bytes)))) {
2139 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002140 }
2141
Steven Valdez143e8b32016-07-11 13:19:03 -04002142 return CBB_flush(out);
2143}
2144
David Benjamin499742c2017-07-22 12:45:38 -04002145int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
2146 Array<uint8_t> *out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002147 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002148 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002149 uint16_t group_id;
2150 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002151 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2152 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002153 *out_alert = SSL_AD_DECODE_ERROR;
2154 return 0;
2155 }
2156
David Benjaminc642aca2017-07-19 23:28:43 -04002157 if (hs->key_share->GroupID() != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002158 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2159 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2160 return 0;
2161 }
2162
David Benjamin6b3ab722017-09-21 02:37:58 -04002163 if (!hs->key_share->Finish(out_secret, out_alert, peer_key)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002164 *out_alert = SSL_AD_INTERNAL_ERROR;
2165 return 0;
2166 }
2167
David Benjamin45738dd2017-02-09 20:01:26 -05002168 hs->new_session->group_id = group_id;
David Benjaminc642aca2017-07-19 23:28:43 -04002169 hs->key_share.reset();
Steven Valdez143e8b32016-07-11 13:19:03 -04002170 return 1;
2171}
2172
David Benjamin74795b32017-08-31 15:13:12 -04002173int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
David Benjamin499742c2017-07-22 12:45:38 -04002174 Array<uint8_t> *out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002175 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002176 uint16_t group_id;
2177 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002178 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002179 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2180 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2181 return 0;
2182 }
2183
2184 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002185 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002186 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002187 return 0;
2188 }
2189
David Benjaminc11ea9422017-08-29 16:33:21 -04002190 // Find the corresponding key share.
David Benjamin74795b32017-08-31 15:13:12 -04002191 bool found = false;
David Benjamin7e1f9842016-09-20 19:24:40 -04002192 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002193 while (CBS_len(&key_shares) > 0) {
2194 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002195 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002196 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002197 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2198 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002199 return 0;
2200 }
2201
David Benjamin7e1f9842016-09-20 19:24:40 -04002202 if (id == group_id) {
2203 if (found) {
2204 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2205 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2206 return 0;
2207 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002208
David Benjamin74795b32017-08-31 15:13:12 -04002209 found = true;
David Benjamin7e1f9842016-09-20 19:24:40 -04002210 peer_key = peer_key_tmp;
David Benjaminc11ea9422017-08-29 16:33:21 -04002211 // Continue parsing the structure to keep peers honest.
Steven Valdez143e8b32016-07-11 13:19:03 -04002212 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002213 }
2214
David Benjamin7e1f9842016-09-20 19:24:40 -04002215 if (!found) {
David Benjamin74795b32017-08-31 15:13:12 -04002216 *out_found = false;
David Benjamin499742c2017-07-22 12:45:38 -04002217 out_secret->Reset();
David Benjamin7e1f9842016-09-20 19:24:40 -04002218 return 1;
2219 }
2220
David Benjaminc11ea9422017-08-29 16:33:21 -04002221 // Compute the DH secret.
David Benjamin499742c2017-07-22 12:45:38 -04002222 Array<uint8_t> secret;
David Benjamin1386aad2017-07-19 23:57:40 -04002223 ScopedCBB public_key;
David Benjaminc642aca2017-07-19 23:28:43 -04002224 UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
David Benjamin6b3ab722017-09-21 02:37:58 -04002225 if (!key_share ||
2226 !CBB_init(public_key.get(), 32) ||
2227 !key_share->Accept(public_key.get(), &secret, out_alert, peer_key) ||
David Benjamin879efc32017-09-21 11:20:53 -04002228 !CBBFinishArray(public_key.get(), &hs->ecdh_public_key)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002229 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002230 return 0;
2231 }
2232
David Benjamin499742c2017-07-22 12:45:38 -04002233 *out_secret = std::move(secret);
David Benjamin74795b32017-08-31 15:13:12 -04002234 *out_found = true;
Steven Valdez5440fe02016-07-18 12:40:30 -04002235 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002236}
2237
David Benjamin8baf9632016-11-17 17:11:16 +09002238int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002239 uint16_t group_id;
2240 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002241 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002242 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2243 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2244 !CBB_add_u16(&kse_bytes, group_id) ||
2245 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjamin879efc32017-09-21 11:20:53 -04002246 !CBB_add_bytes(&public_key, hs->ecdh_public_key.data(),
2247 hs->ecdh_public_key.size()) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002248 !CBB_flush(out)) {
2249 return 0;
2250 }
2251
David Benjamin879efc32017-09-21 11:20:53 -04002252 hs->ecdh_public_key.Reset();
David Benjamin4fe3c902016-08-16 02:17:03 -04002253
David Benjamin45738dd2017-02-09 20:01:26 -05002254 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002255 return 1;
2256}
2257
2258
David Benjaminc11ea9422017-08-29 16:33:21 -04002259// Supported Versions
2260//
2261// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1
Steven Valdezfdd10992016-09-15 16:27:05 -04002262
David Benjamin8c880a22016-12-03 02:20:34 -05002263static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2264 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002265 if (hs->max_version <= TLS1_2_VERSION) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002266 return 1;
2267 }
2268
2269 CBB contents, versions;
2270 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2271 !CBB_add_u16_length_prefixed(out, &contents) ||
2272 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2273 return 0;
2274 }
2275
David Benjaminc11ea9422017-08-29 16:33:21 -04002276 // Add a fake version. See draft-davidben-tls-grease-01.
David Benjamind9791bf2016-09-27 16:39:52 -04002277 if (ssl->ctx->grease_enabled &&
2278 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2279 return 0;
2280 }
2281
Steven Valdez8f36c512017-06-20 10:55:02 -04002282 if (!ssl_add_supported_versions(hs, &versions) ||
2283 !CBB_flush(out)) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002284 return 0;
2285 }
2286
2287 return 1;
2288}
2289
2290
David Benjaminc11ea9422017-08-29 16:33:21 -04002291// Cookie
2292//
2293// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2
David Benjamin3baa6e12016-10-07 21:10:38 -04002294
David Benjamin8c880a22016-12-03 02:20:34 -05002295static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin08f5c762017-09-21 02:43:05 -04002296 if (hs->cookie.size() == 0) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002297 return 1;
2298 }
2299
2300 CBB contents, cookie;
2301 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2302 !CBB_add_u16_length_prefixed(out, &contents) ||
2303 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin08f5c762017-09-21 02:43:05 -04002304 !CBB_add_bytes(&cookie, hs->cookie.data(), hs->cookie.size()) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002305 !CBB_flush(out)) {
2306 return 0;
2307 }
2308
David Benjaminc11ea9422017-08-29 16:33:21 -04002309 // The cookie is no longer needed in memory.
David Benjamin08f5c762017-09-21 02:43:05 -04002310 hs->cookie.Reset();
David Benjamin3baa6e12016-10-07 21:10:38 -04002311 return 1;
2312}
2313
2314
David Benjaminc11ea9422017-08-29 16:33:21 -04002315// Negotiated Groups
2316//
2317// https://tools.ietf.org/html/rfc4492#section-5.1.2
2318// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4
Adam Langley273d49c2015-07-20 16:38:52 -07002319
David Benjamin8c880a22016-12-03 02:20:34 -05002320static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2321 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002322 CBB contents, groups_bytes;
2323 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002324 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002325 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002326 return 0;
2327 }
2328
David Benjaminc11ea9422017-08-29 16:33:21 -04002329 // Add a fake group. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002330 if (ssl->ctx->grease_enabled &&
2331 !CBB_add_u16(&groups_bytes,
2332 ssl_get_grease_value(ssl, ssl_grease_group))) {
2333 return 0;
2334 }
2335
David Benjamincf0ce672017-09-21 02:25:59 -04002336 for (uint16_t group : tls1_get_grouplist(ssl)) {
2337 if (!CBB_add_u16(&groups_bytes, group)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002338 return 0;
2339 }
2340 }
2341
2342 return CBB_flush(out);
2343}
2344
David Benjamin8c880a22016-12-03 02:20:34 -05002345static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2346 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002347 CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002348 // This extension is not expected to be echoed by servers in TLS 1.2, but some
2349 // BigIP servers send it nonetheless, so do not enforce this.
Adam Langley273d49c2015-07-20 16:38:52 -07002350 return 1;
2351}
2352
David Benjamin8c880a22016-12-03 02:20:34 -05002353static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2354 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002355 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002356 if (contents == NULL) {
2357 return 1;
2358 }
2359
Steven Valdezce902a92016-05-17 11:47:53 -04002360 CBS supported_group_list;
2361 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2362 CBS_len(&supported_group_list) == 0 ||
2363 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002364 CBS_len(contents) != 0) {
2365 return 0;
2366 }
2367
David Benjamincf0ce672017-09-21 02:25:59 -04002368 Array<uint16_t> groups;
2369 if (!groups.Init(CBS_len(&supported_group_list) / 2)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002370 return 0;
2371 }
David Benjamincf0ce672017-09-21 02:25:59 -04002372 for (size_t i = 0; i < groups.size(); i++) {
2373 if (!CBS_get_u16(&supported_group_list, &groups[i])) {
2374 *out_alert = SSL_AD_INTERNAL_ERROR;
2375 return 0;
Adam Langley273d49c2015-07-20 16:38:52 -07002376 }
2377 }
2378
Steven Valdezce902a92016-05-17 11:47:53 -04002379 assert(CBS_len(&supported_group_list) == 0);
David Benjamincf0ce672017-09-21 02:25:59 -04002380 hs->peer_supported_group_list = std::move(groups);
Adam Langley273d49c2015-07-20 16:38:52 -07002381 return 1;
Adam Langley273d49c2015-07-20 16:38:52 -07002382}
2383
David Benjamin8c880a22016-12-03 02:20:34 -05002384static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002385 // Servers don't echo this extension.
Adam Langley273d49c2015-07-20 16:38:52 -07002386 return 1;
2387}
2388
2389
David Benjaminc11ea9422017-08-29 16:33:21 -04002390// kExtensions contains all the supported extensions.
Adam Langley614c66a2015-06-12 15:26:58 -07002391static const struct tls_extension kExtensions[] = {
2392 {
Adam Langley5021b222015-06-12 18:27:58 -07002393 TLSEXT_TYPE_renegotiate,
2394 NULL,
2395 ext_ri_add_clienthello,
2396 ext_ri_parse_serverhello,
2397 ext_ri_parse_clienthello,
2398 ext_ri_add_serverhello,
2399 },
2400 {
Adam Langley614c66a2015-06-12 15:26:58 -07002401 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002402 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002403 ext_sni_add_clienthello,
2404 ext_sni_parse_serverhello,
2405 ext_sni_parse_clienthello,
2406 ext_sni_add_serverhello,
2407 },
Adam Langley0a056712015-07-01 15:03:33 -07002408 {
2409 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002410 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002411 ext_ems_add_clienthello,
2412 ext_ems_parse_serverhello,
2413 ext_ems_parse_clienthello,
2414 ext_ems_add_serverhello,
2415 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002416 {
2417 TLSEXT_TYPE_session_ticket,
2418 NULL,
2419 ext_ticket_add_clienthello,
2420 ext_ticket_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04002421 // Ticket extension client parsing is handled in ssl_session.c
Steven Valdez6b8509a2016-07-12 13:38:32 -04002422 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002423 ext_ticket_add_serverhello,
2424 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002425 {
2426 TLSEXT_TYPE_signature_algorithms,
2427 NULL,
2428 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002429 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002430 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002431 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002432 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002433 {
2434 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002435 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002436 ext_ocsp_add_clienthello,
2437 ext_ocsp_parse_serverhello,
2438 ext_ocsp_parse_clienthello,
2439 ext_ocsp_add_serverhello,
2440 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002441 {
2442 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002443 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002444 ext_npn_add_clienthello,
2445 ext_npn_parse_serverhello,
2446 ext_npn_parse_clienthello,
2447 ext_npn_add_serverhello,
2448 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002449 {
2450 TLSEXT_TYPE_certificate_timestamp,
2451 NULL,
2452 ext_sct_add_clienthello,
2453 ext_sct_parse_serverhello,
2454 ext_sct_parse_clienthello,
2455 ext_sct_add_serverhello,
2456 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002457 {
2458 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002459 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002460 ext_alpn_add_clienthello,
2461 ext_alpn_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04002462 // ALPN is negotiated late in |ssl_negotiate_alpn|.
David Benjamin9ef31f02016-10-31 18:01:13 -04002463 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002464 ext_alpn_add_serverhello,
2465 },
Adam Langley49c7af12015-07-10 14:33:46 -07002466 {
2467 TLSEXT_TYPE_channel_id,
2468 ext_channel_id_init,
2469 ext_channel_id_add_clienthello,
2470 ext_channel_id_parse_serverhello,
2471 ext_channel_id_parse_clienthello,
2472 ext_channel_id_add_serverhello,
2473 },
Adam Langley391250d2015-07-15 19:06:07 -07002474 {
2475 TLSEXT_TYPE_srtp,
2476 ext_srtp_init,
2477 ext_srtp_add_clienthello,
2478 ext_srtp_parse_serverhello,
2479 ext_srtp_parse_clienthello,
2480 ext_srtp_add_serverhello,
2481 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002482 {
2483 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002484 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002485 ext_ec_point_add_clienthello,
2486 ext_ec_point_parse_serverhello,
2487 ext_ec_point_parse_clienthello,
2488 ext_ec_point_add_serverhello,
2489 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002490 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002491 TLSEXT_TYPE_key_share,
2492 NULL,
2493 ext_key_share_add_clienthello,
2494 forbid_parse_serverhello,
2495 ignore_parse_clienthello,
2496 dont_add_serverhello,
2497 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002498 {
Steven Valdeza833c352016-11-01 13:39:36 -04002499 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002500 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002501 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002502 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002503 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002504 dont_add_serverhello,
2505 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002506 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002507 TLSEXT_TYPE_early_data,
2508 NULL,
2509 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002510 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002511 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002512 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002513 },
2514 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002515 TLSEXT_TYPE_supported_versions,
2516 NULL,
2517 ext_supported_versions_add_clienthello,
2518 forbid_parse_serverhello,
2519 ignore_parse_clienthello,
2520 dont_add_serverhello,
2521 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002522 {
2523 TLSEXT_TYPE_cookie,
2524 NULL,
2525 ext_cookie_add_clienthello,
2526 forbid_parse_serverhello,
2527 ignore_parse_clienthello,
2528 dont_add_serverhello,
2529 },
David Benjaminc11ea9422017-08-29 16:33:21 -04002530 // The final extension must be non-empty. WebSphere Application Server 7.0 is
2531 // intolerant to the last extension being zero-length. See
2532 // https://crbug.com/363583.
Adam Langley273d49c2015-07-20 16:38:52 -07002533 {
Steven Valdezce902a92016-05-17 11:47:53 -04002534 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002535 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002536 ext_supported_groups_add_clienthello,
2537 ext_supported_groups_parse_serverhello,
2538 ext_supported_groups_parse_clienthello,
2539 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002540 },
Adam Langley614c66a2015-06-12 15:26:58 -07002541};
2542
2543#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2544
David Benjamina3d76d02017-07-14 19:36:07 -04002545static_assert(kNumExtensions <=
2546 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
2547 "too many extensions for sent bitset");
2548static_assert(kNumExtensions <=
2549 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2550 "too many extensions for received bitset");
Adam Langley4cfa96b2015-07-01 11:56:55 -07002551
Adam Langley614c66a2015-06-12 15:26:58 -07002552static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2553 uint16_t value) {
2554 unsigned i;
2555 for (i = 0; i < kNumExtensions; i++) {
2556 if (kExtensions[i].value == value) {
2557 *out_index = i;
2558 return &kExtensions[i];
2559 }
2560 }
2561
2562 return NULL;
2563}
2564
David Benjamin8c880a22016-12-03 02:20:34 -05002565int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2566 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04002567 // Don't add extensions for SSLv3 unless doing secure renegotiation.
David Benjaminf04c2e92016-12-06 13:35:25 -05002568 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002569 !ssl->s3->send_connection_binding) {
2570 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002571 }
Adam Langley95c29f32014-06-20 12:00:00 -07002572
David Benjamine8d53502015-10-10 14:13:23 -04002573 CBB extensions;
2574 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002575 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2576 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002577 }
Adam Langley95c29f32014-06-20 12:00:00 -07002578
David Benjamin8c880a22016-12-03 02:20:34 -05002579 hs->extensions.sent = 0;
2580 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002581
David Benjamin54091232016-09-05 12:47:25 -04002582 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002583 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002584 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002585 }
2586 }
Adam Langley95c29f32014-06-20 12:00:00 -07002587
David Benjamin65ac9972016-09-02 21:35:25 -04002588 uint16_t grease_ext1 = 0;
2589 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002590 // Add a fake empty extension. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002591 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2592 if (!CBB_add_u16(&extensions, grease_ext1) ||
2593 !CBB_add_u16(&extensions, 0 /* zero length */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002594 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2595 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002596 }
2597 }
2598
David Benjamin54091232016-09-05 12:47:25 -04002599 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002600 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002601 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002602 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002603 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin81678aa2017-07-12 22:43:42 -04002604 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002605 }
Adam Langley95c29f32014-06-20 12:00:00 -07002606
Adam Langley33ad2b52015-07-20 17:43:53 -07002607 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002608 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002609 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002610 }
Adam Langley75712922014-10-10 16:23:43 -07002611
David Benjamin2bd19172016-11-17 16:47:15 +09002612 if (!custom_ext_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002613 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2614 return 0;
Adam Langley09505632015-07-30 18:10:13 -07002615 }
2616
David Benjamin65ac9972016-09-02 21:35:25 -04002617 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002618 // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002619 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2620
David Benjaminc11ea9422017-08-29 16:33:21 -04002621 // The two fake extensions must not have the same value. GREASE values are
2622 // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2623 // one.
David Benjamin65ac9972016-09-02 21:35:25 -04002624 if (grease_ext1 == grease_ext2) {
2625 grease_ext2 ^= 0x1010;
2626 }
2627
2628 if (!CBB_add_u16(&extensions, grease_ext2) ||
2629 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2630 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002631 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2632 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002633 }
2634 }
2635
David Benjamince079fd2016-08-02 16:22:34 -04002636 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002637 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002638 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002639 if (header_len > 0xff && header_len < 0x200) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002640 // Add padding to workaround bugs in F5 terminators. See RFC 7685.
2641 //
2642 // NB: because this code works out the length of all existing extensions
2643 // it MUST always appear last.
David Benjamin0a968592015-07-21 22:06:19 -04002644 size_t padding_len = 0x200 - header_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04002645 // Extensions take at least four bytes to encode. Always include at least
2646 // one byte of data if including the extension. WebSphere Application
2647 // Server 7.0 is intolerant to the last extension being zero-length. See
2648 // https://crbug.com/363583.
Adam Langleyfcf25832014-12-18 17:42:32 -08002649 if (padding_len >= 4 + 1) {
2650 padding_len -= 4;
2651 } else {
2652 padding_len = 1;
2653 }
Adam Langley95c29f32014-06-20 12:00:00 -07002654
Adam Langley33ad2b52015-07-20 17:43:53 -07002655 uint8_t *padding_bytes;
2656 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2657 !CBB_add_u16(&extensions, padding_len) ||
2658 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002659 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2660 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002661 }
Adam Langley75712922014-10-10 16:23:43 -07002662
David Benjamin17cf2cb2016-12-13 01:07:13 -05002663 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002664 }
2665 }
Adam Langley75712922014-10-10 16:23:43 -07002666
David Benjaminc11ea9422017-08-29 16:33:21 -04002667 // The PSK extension must be last, including after the padding.
David Benjamin8c880a22016-12-03 02:20:34 -05002668 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002669 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2670 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -04002671 }
2672
David Benjaminc11ea9422017-08-29 16:33:21 -04002673 // Discard empty extensions blocks.
David Benjamina01deee2015-12-08 18:56:31 -05002674 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002675 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002676 }
2677
David Benjamine8d53502015-10-10 14:13:23 -04002678 return CBB_flush(out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002679}
Adam Langley95c29f32014-06-20 12:00:00 -07002680
David Benjamin8c880a22016-12-03 02:20:34 -05002681int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2682 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002683 CBB extensions;
2684 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002685 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002686 }
2687
David Benjamin8c880a22016-12-03 02:20:34 -05002688 for (unsigned i = 0; i < kNumExtensions; i++) {
2689 if (!(hs->extensions.received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002690 // Don't send extensions that were not received.
Adam Langley614c66a2015-06-12 15:26:58 -07002691 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002692 }
Adam Langley95c29f32014-06-20 12:00:00 -07002693
David Benjamin8c880a22016-12-03 02:20:34 -05002694 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002695 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002696 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002697 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002698 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002699 }
Adam Langley95c29f32014-06-20 12:00:00 -07002700
David Benjamin2bd19172016-11-17 16:47:15 +09002701 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002702 goto err;
2703 }
2704
David Benjaminc11ea9422017-08-29 16:33:21 -04002705 // Discard empty extensions blocks before TLS 1.3.
Steven Valdez143e8b32016-07-11 13:19:03 -04002706 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2707 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002708 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002709 }
2710
David Benjamin56380462015-10-10 14:59:09 -04002711 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002712
2713err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002714 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002715 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002716}
Adam Langley95c29f32014-06-20 12:00:00 -07002717
David Benjamin731058e2016-12-03 23:15:13 -05002718static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2719 const SSL_CLIENT_HELLO *client_hello,
2720 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002721 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002722 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002723 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002724 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002725 }
2726 }
2727
David Benjamin8c880a22016-12-03 02:20:34 -05002728 hs->extensions.received = 0;
2729 hs->custom_extensions.received = 0;
David Benjamine14ff062016-08-09 16:21:24 -04002730 CBS extensions;
2731 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2732 while (CBS_len(&extensions) != 0) {
2733 uint16_t type;
2734 CBS extension;
2735
David Benjaminc11ea9422017-08-29 16:33:21 -04002736 // Decode the next extension.
David Benjamine14ff062016-08-09 16:21:24 -04002737 if (!CBS_get_u16(&extensions, &type) ||
2738 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002739 *out_alert = SSL_AD_DECODE_ERROR;
2740 return 0;
2741 }
Adam Langley95c29f32014-06-20 12:00:00 -07002742
David Benjaminc11ea9422017-08-29 16:33:21 -04002743 // RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2744 // ambiguous. Ignore all but the renegotiation_info extension.
David Benjamine14ff062016-08-09 16:21:24 -04002745 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2746 continue;
2747 }
Adam Langley95c29f32014-06-20 12:00:00 -07002748
David Benjamine14ff062016-08-09 16:21:24 -04002749 unsigned ext_index;
2750 const struct tls_extension *const ext =
2751 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002752
David Benjamine14ff062016-08-09 16:21:24 -04002753 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002754 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002755 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002756 return 0;
2757 }
David Benjamine14ff062016-08-09 16:21:24 -04002758 continue;
2759 }
2760
David Benjamin8c880a22016-12-03 02:20:34 -05002761 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002762 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002763 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002764 *out_alert = alert;
2765 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002766 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002767 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002768 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002769 }
Adam Langley75712922014-10-10 16:23:43 -07002770
David Benjamin1deb41b2016-08-09 19:36:38 -04002771 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002772 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002773 continue;
2774 }
2775
2776 CBS *contents = NULL, fake_contents;
2777 static const uint8_t kFakeRenegotiateExtension[] = {0};
2778 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2779 ssl_client_cipher_list_contains_cipher(client_hello,
2780 SSL3_CK_SCSV & 0xffff)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002781 // The renegotiation SCSV was received so pretend that we received a
2782 // renegotiation extension.
David Benjamin1deb41b2016-08-09 19:36:38 -04002783 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2784 sizeof(kFakeRenegotiateExtension));
2785 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002786 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002787 }
2788
David Benjaminc11ea9422017-08-29 16:33:21 -04002789 // Extension wasn't observed so call the callback with a NULL
2790 // parameter.
David Benjamin1deb41b2016-08-09 19:36:38 -04002791 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002792 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002793 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002794 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002795 *out_alert = alert;
2796 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002797 }
2798 }
2799
Adam Langleyfcf25832014-12-18 17:42:32 -08002800 return 1;
2801}
Adam Langley95c29f32014-06-20 12:00:00 -07002802
David Benjamin731058e2016-12-03 23:15:13 -05002803int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2804 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002805 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002806 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002807 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002808 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002809 return 0;
2810 }
Adam Langley95c29f32014-06-20 12:00:00 -07002811
David Benjamin8c880a22016-12-03 02:20:34 -05002812 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002813 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002814 return 0;
2815 }
Adam Langley95c29f32014-06-20 12:00:00 -07002816
Adam Langleyfcf25832014-12-18 17:42:32 -08002817 return 1;
2818}
Adam Langley95c29f32014-06-20 12:00:00 -07002819
David Benjamin8c880a22016-12-03 02:20:34 -05002820static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2821 int *out_alert) {
2822 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04002823 // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty.
Steven Valdez143e8b32016-07-11 13:19:03 -04002824 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2825 return 1;
2826 }
Adam Langley614c66a2015-06-12 15:26:58 -07002827
David Benjaminc11ea9422017-08-29 16:33:21 -04002828 // Decode the extensions block and check it is valid.
Steven Valdez143e8b32016-07-11 13:19:03 -04002829 CBS extensions;
2830 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2831 !tls1_check_duplicate_extensions(&extensions)) {
2832 *out_alert = SSL_AD_DECODE_ERROR;
2833 return 0;
2834 }
2835
2836 uint32_t received = 0;
2837 while (CBS_len(&extensions) != 0) {
2838 uint16_t type;
2839 CBS extension;
2840
David Benjaminc11ea9422017-08-29 16:33:21 -04002841 // Decode the next extension.
Steven Valdez143e8b32016-07-11 13:19:03 -04002842 if (!CBS_get_u16(&extensions, &type) ||
2843 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002844 *out_alert = SSL_AD_DECODE_ERROR;
2845 return 0;
2846 }
Adam Langley95c29f32014-06-20 12:00:00 -07002847
Steven Valdez143e8b32016-07-11 13:19:03 -04002848 unsigned ext_index;
2849 const struct tls_extension *const ext =
2850 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002851
Steven Valdez143e8b32016-07-11 13:19:03 -04002852 if (ext == NULL) {
David Benjaminfd45ee72017-08-31 14:49:09 -04002853 hs->received_custom_extension = true;
David Benjamin2bd19172016-11-17 16:47:15 +09002854 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002855 return 0;
2856 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002857 continue;
2858 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002859
David Benjamina3d76d02017-07-14 19:36:07 -04002860 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
2861 "too many bits");
David Benjamin5db7c9b2017-01-24 16:17:03 -05002862
David Benjamin8c880a22016-12-03 02:20:34 -05002863 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002864 type != TLSEXT_TYPE_renegotiate) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002865 // If the extension was never sent then it is illegal, except for the
2866 // renegotiation extension which, in SSL 3.0, is signaled via SCSV.
Steven Valdez143e8b32016-07-11 13:19:03 -04002867 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2868 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002869 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002870 return 0;
2871 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002872
Steven Valdez143e8b32016-07-11 13:19:03 -04002873 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002874
Steven Valdez143e8b32016-07-11 13:19:03 -04002875 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002876 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002877 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002878 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04002879 *out_alert = alert;
2880 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002881 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002882 }
Adam Langley95c29f32014-06-20 12:00:00 -07002883
David Benjamin54091232016-09-05 12:47:25 -04002884 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002885 if (!(received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002886 // Extension wasn't observed so call the callback with a NULL
2887 // parameter.
Adam Langley614c66a2015-06-12 15:26:58 -07002888 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002889 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002890 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002891 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002892 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002893 return 0;
2894 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002895 }
2896 }
Adam Langley95c29f32014-06-20 12:00:00 -07002897
Adam Langleyfcf25832014-12-18 17:42:32 -08002898 return 1;
2899}
Adam Langley95c29f32014-06-20 12:00:00 -07002900
David Benjamin8c880a22016-12-03 02:20:34 -05002901static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
2902 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002903 int ret = SSL_TLSEXT_ERR_NOACK;
2904 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002905
David Benjamin78f8aab2016-03-10 16:33:58 -05002906 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002907 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002908 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05002909 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
2910 ret = ssl->session_ctx->tlsext_servername_callback(
2911 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002912 }
Adam Langley95c29f32014-06-20 12:00:00 -07002913
Adam Langleyfcf25832014-12-18 17:42:32 -08002914 switch (ret) {
2915 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002916 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002917 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002918
Adam Langleyfcf25832014-12-18 17:42:32 -08002919 case SSL_TLSEXT_ERR_NOACK:
David Benjaminfd45ee72017-08-31 14:49:09 -04002920 hs->should_ack_sni = false;
Adam Langleyfcf25832014-12-18 17:42:32 -08002921 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002922
Adam Langleyfcf25832014-12-18 17:42:32 -08002923 default:
2924 return 1;
2925 }
2926}
Adam Langleyed8270a2014-09-02 13:52:56 -07002927
David Benjamin8c880a22016-12-03 02:20:34 -05002928int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
2929 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002930 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002931 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002932 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002933 return 0;
2934 }
2935
Adam Langleyfcf25832014-12-18 17:42:32 -08002936 return 1;
2937}
Adam Langley95c29f32014-06-20 12:00:00 -07002938
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002939static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
2940 uint8_t **out, size_t *out_len, EVP_CIPHER_CTX *cipher_ctx,
2941 HMAC_CTX *hmac_ctx, const uint8_t *ticket, size_t ticket_len) {
2942 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002943
David Benjaminc11ea9422017-08-29 16:33:21 -04002944 // Check the MAC at the end of the ticket.
David Benjamine3aa1d92015-06-16 15:34:50 -04002945 uint8_t mac[EVP_MAX_MD_SIZE];
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002946 size_t mac_len = HMAC_size(hmac_ctx);
David Benjamine3aa1d92015-06-16 15:34:50 -04002947 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002948 // The ticket must be large enough for key name, IV, data, and MAC.
David Benjamin81678aa2017-07-12 22:43:42 -04002949 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08002950 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002951 HMAC_Update(hmac_ctx, ticket, ticket_len - mac_len);
2952 HMAC_Final(hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04002953 int mac_ok =
2954 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
2955#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
2956 mac_ok = 1;
2957#endif
2958 if (!mac_ok) {
David Benjamin81678aa2017-07-12 22:43:42 -04002959 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08002960 }
2961
David Benjaminc11ea9422017-08-29 16:33:21 -04002962 // Decrypt the session data.
David Benjamine3aa1d92015-06-16 15:34:50 -04002963 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2964 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2965 mac_len;
David Benjamin86e95b82017-07-18 16:34:25 -04002966 UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len));
David Benjamin81678aa2017-07-12 22:43:42 -04002967 if (!plaintext) {
2968 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08002969 }
David Benjaminfbc45d72016-09-22 01:21:24 -04002970 size_t plaintext_len;
2971#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin81678aa2017-07-12 22:43:42 -04002972 OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04002973 plaintext_len = ciphertext_len;
2974#else
David Benjamine3aa1d92015-06-16 15:34:50 -04002975 if (ciphertext_len >= INT_MAX) {
David Benjamin81678aa2017-07-12 22:43:42 -04002976 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08002977 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002978 int len1, len2;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002979 if (!EVP_DecryptUpdate(cipher_ctx, plaintext.get(), &len1, ciphertext,
David Benjamine3aa1d92015-06-16 15:34:50 -04002980 (int)ciphertext_len) ||
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002981 !EVP_DecryptFinal_ex(cipher_ctx, plaintext.get() + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08002982 ERR_clear_error();
David Benjamin81678aa2017-07-12 22:43:42 -04002983 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08002984 }
Adam Langley4c341d02017-03-08 19:33:21 -08002985 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04002986#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08002987
David Benjamin81678aa2017-07-12 22:43:42 -04002988 *out = plaintext.release();
Adam Langley4c341d02017-03-08 19:33:21 -08002989 *out_len = plaintext_len;
David Benjamin81678aa2017-07-12 22:43:42 -04002990 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08002991}
2992
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002993static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
David Benjaminfd45ee72017-08-31 14:49:09 -04002994 SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002995 const uint8_t *ticket, size_t ticket_len) {
2996 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
2997 ScopedEVP_CIPHER_CTX cipher_ctx;
2998 ScopedHMAC_CTX hmac_ctx;
2999 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3000 int cb_ret = ssl->session_ctx->tlsext_ticket_key_cb(
3001 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(),
3002 hmac_ctx.get(), 0 /* decrypt */);
3003 if (cb_ret < 0) {
3004 return ssl_ticket_aead_error;
3005 } else if (cb_ret == 0) {
3006 return ssl_ticket_aead_ignore_ticket;
3007 } else if (cb_ret == 2) {
David Benjaminfd45ee72017-08-31 14:49:09 -04003008 *out_renew_ticket = true;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003009 } else {
3010 assert(cb_ret == 1);
3011 }
3012 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3013 hmac_ctx.get(), ticket, ticket_len);
3014}
3015
3016static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
3017 SSL *ssl, uint8_t **out, size_t *out_len, const uint8_t *ticket,
3018 size_t ticket_len) {
3019 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3020 SSL_CTX *ctx = ssl->session_ctx;
3021
David Benjaminc11ea9422017-08-29 16:33:21 -04003022 // Rotate the ticket key if necessary.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003023 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
3024 return ssl_ticket_aead_error;
3025 }
3026
David Benjaminc11ea9422017-08-29 16:33:21 -04003027 // Pick the matching ticket key and decrypt.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003028 ScopedEVP_CIPHER_CTX cipher_ctx;
3029 ScopedHMAC_CTX hmac_ctx;
3030 {
3031 MutexReadLock lock(&ctx->lock);
3032 const tlsext_ticket_key *key;
3033 if (ctx->tlsext_ticket_key_current &&
3034 !OPENSSL_memcmp(ctx->tlsext_ticket_key_current->name, ticket,
3035 SSL_TICKET_KEY_NAME_LEN)) {
3036 key = ctx->tlsext_ticket_key_current;
3037 } else if (ctx->tlsext_ticket_key_prev &&
3038 !OPENSSL_memcmp(ctx->tlsext_ticket_key_prev->name, ticket,
3039 SSL_TICKET_KEY_NAME_LEN)) {
3040 key = ctx->tlsext_ticket_key_prev;
3041 } else {
3042 return ssl_ticket_aead_ignore_ticket;
3043 }
3044 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3045 if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
3046 tlsext_tick_md(), NULL) ||
3047 !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL,
3048 key->aes_key, iv)) {
3049 return ssl_ticket_aead_error;
3050 }
3051 }
3052 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3053 hmac_ctx.get(), ticket, ticket_len);
3054}
3055
Adam Langley4c341d02017-03-08 19:33:21 -08003056static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
David Benjaminfd45ee72017-08-31 14:49:09 -04003057 SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
Adam Langley4c341d02017-03-08 19:33:21 -08003058 const uint8_t *ticket, size_t ticket_len) {
David Benjamin81678aa2017-07-12 22:43:42 -04003059 uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len);
Adam Langley4c341d02017-03-08 19:33:21 -08003060 if (plaintext == NULL) {
3061 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3062 return ssl_ticket_aead_error;
3063 }
3064
3065 size_t plaintext_len;
3066 const enum ssl_ticket_aead_result_t result =
3067 ssl->session_ctx->ticket_aead_method->open(
3068 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3069
3070 if (result == ssl_ticket_aead_success) {
3071 *out = plaintext;
3072 plaintext = NULL;
3073 *out_len = plaintext_len;
3074 }
3075
3076 OPENSSL_free(plaintext);
3077 return result;
3078}
3079
3080enum ssl_ticket_aead_result_t ssl_process_ticket(
David Benjaminfd45ee72017-08-31 14:49:09 -04003081 SSL *ssl, UniquePtr<SSL_SESSION> *out_session, bool *out_renew_ticket,
Adam Langley4c341d02017-03-08 19:33:21 -08003082 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3083 size_t session_id_len) {
David Benjaminfd45ee72017-08-31 14:49:09 -04003084 *out_renew_ticket = false;
David Benjamin37af90f2017-07-29 01:42:16 -04003085 out_session->reset();
Adam Langley4c341d02017-03-08 19:33:21 -08003086
3087 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3088 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3089 return ssl_ticket_aead_ignore_ticket;
3090 }
3091
3092 uint8_t *plaintext = NULL;
3093 size_t plaintext_len;
3094 enum ssl_ticket_aead_result_t result;
3095 if (ssl->session_ctx->ticket_aead_method != NULL) {
3096 result = ssl_decrypt_ticket_with_method(
3097 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3098 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04003099 // Ensure there is room for the key name and the largest IV
3100 // |tlsext_ticket_key_cb| may try to consume. The real limit may be lower,
3101 // but the maximum IV length should be well under the minimum size for the
3102 // session material and HMAC.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003103 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3104 return ssl_ticket_aead_ignore_ticket;
3105 }
3106 if (ssl->session_ctx->tlsext_ticket_key_cb != NULL) {
3107 result = ssl_decrypt_ticket_with_cb(ssl, &plaintext, &plaintext_len,
3108 out_renew_ticket, ticket, ticket_len);
3109 } else {
3110 result = ssl_decrypt_ticket_with_ticket_keys(
3111 ssl, &plaintext, &plaintext_len, ticket, ticket_len);
3112 }
Adam Langley4c341d02017-03-08 19:33:21 -08003113 }
3114
3115 if (result != ssl_ticket_aead_success) {
3116 return result;
3117 }
3118
David Benjaminc11ea9422017-08-29 16:33:21 -04003119 // Decode the session.
David Benjamin37af90f2017-07-29 01:42:16 -04003120 UniquePtr<SSL_SESSION> session(
3121 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx));
Adam Langley4c341d02017-03-08 19:33:21 -08003122 OPENSSL_free(plaintext);
3123
David Benjamin37af90f2017-07-29 01:42:16 -04003124 if (!session) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003125 ERR_clear_error(); // Don't leave an error on the queue.
Adam Langley4c341d02017-03-08 19:33:21 -08003126 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003127 }
3128
David Benjaminc11ea9422017-08-29 16:33:21 -04003129 // Copy the client's session ID into the new session, to denote the ticket has
3130 // been accepted.
David Benjamin17cf2cb2016-12-13 01:07:13 -05003131 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003132 session->session_id_length = session_id_len;
3133
David Benjamin37af90f2017-07-29 01:42:16 -04003134 *out_session = std::move(session);
Adam Langley4c341d02017-03-08 19:33:21 -08003135 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003136}
Adam Langley95c29f32014-06-20 12:00:00 -07003137
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003138int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003139 // Extension ignored for inappropriate versions
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003140 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003141 return 1;
3142 }
David Benjamincd996942014-07-20 16:23:51 -04003143
David Benjamin0fc37ef2016-08-17 15:29:46 -04003144 OPENSSL_free(hs->peer_sigalgs);
3145 hs->peer_sigalgs = NULL;
3146 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003147
3148 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003149 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003150 return 0;
3151 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003152 num_sigalgs /= 2;
3153
David Benjaminc11ea9422017-08-29 16:33:21 -04003154 // supported_signature_algorithms in the certificate request is
3155 // allowed to be empty.
Steven Valdez0d62f262015-09-04 12:41:04 -04003156 if (num_sigalgs == 0) {
3157 return 1;
3158 }
3159
David Benjaminc11ea9422017-08-29 16:33:21 -04003160 // This multiplication doesn't overflow because sizeof(uint16_t) is two
3161 // and we just divided |num_sigalgs| by two.
David Benjamin81678aa2017-07-12 22:43:42 -04003162 hs->peer_sigalgs = (uint16_t *)OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003163 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003164 return 0;
3165 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003166 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003167
3168 CBS sigalgs;
3169 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003170 for (size_t i = 0; i < num_sigalgs; i++) {
3171 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003172 return 0;
3173 }
3174 }
Adam Langley95c29f32014-06-20 12:00:00 -07003175
Adam Langleyfcf25832014-12-18 17:42:32 -08003176 return 1;
3177}
David Benjaminec2f27d2014-11-13 19:17:25 -05003178
David Benjamina3651382017-04-20 17:49:36 -04003179int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
3180 switch (EVP_PKEY_id(pkey)) {
3181 case EVP_PKEY_RSA:
3182 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3183 return 1;
3184 case EVP_PKEY_EC:
3185 *out = SSL_SIGN_ECDSA_SHA1;
3186 return 1;
3187 default:
3188 return 0;
3189 }
3190}
3191
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003192int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3193 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003194 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003195
David Benjaminc11ea9422017-08-29 16:33:21 -04003196 // Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3197 // handshake.
Steven Valdezf0451ca2016-06-29 13:16:27 -04003198 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin31b0c9b2017-07-20 14:49:15 -04003199 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
David Benjamina3651382017-04-20 17:49:36 -04003200 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3201 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003202 }
David Benjamina3651382017-04-20 17:49:36 -04003203 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003204 }
3205
David Benjamin3ef76972016-10-17 17:59:54 -04003206 const uint16_t *sigalgs = cert->sigalgs;
3207 size_t num_sigalgs = cert->num_sigalgs;
3208 if (sigalgs == NULL) {
3209 sigalgs = kSignSignatureAlgorithms;
3210 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003211 }
3212
David Benjamin0fc37ef2016-08-17 15:29:46 -04003213 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3214 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3215 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003216 // If the client didn't specify any signature_algorithms extension then
3217 // we can assume that it supports SHA1. See
3218 // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
David Benjaminea9a0d52016-07-08 15:52:59 -07003219 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3220 SSL_SIGN_ECDSA_SHA1};
3221 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003222 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003223 }
3224
David Benjamin0fc37ef2016-08-17 15:29:46 -04003225 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003226 uint16_t sigalg = sigalgs[i];
David Benjaminc11ea9422017-08-29 16:33:21 -04003227 // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3228 // negotiated.
David Benjamin1fb125c2016-07-08 18:52:12 -07003229 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjamina232a712017-03-30 15:51:53 -05003230 !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003231 continue;
3232 }
3233
David Benjamin0fc37ef2016-08-17 15:29:46 -04003234 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003235 if (sigalg == peer_sigalgs[j]) {
3236 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003237 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003238 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003239 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003240 }
Adam Langley95c29f32014-06-20 12:00:00 -07003241
David Benjaminea9a0d52016-07-08 15:52:59 -07003242 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3243 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003244}
Adam Langley95c29f32014-06-20 12:00:00 -07003245
David Benjamin7934f082017-08-01 16:32:25 -04003246int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
Steven Valdez908ac192017-01-12 13:17:07 -05003247 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003248 // A Channel ID handshake message is structured to contain multiple
3249 // extensions, but the only one that can be present is Channel ID.
David Benjamin7934f082017-08-01 16:32:25 -04003250 uint16_t extension_type;
3251 CBS channel_id = msg.body, extension;
Nick Harper60a85cb2016-09-23 16:25:11 -07003252 if (!CBS_get_u16(&channel_id, &extension_type) ||
3253 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3254 CBS_len(&channel_id) != 0 ||
3255 extension_type != TLSEXT_TYPE_channel_id ||
3256 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3257 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3258 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3259 return 0;
3260 }
3261
David Benjamin86e95b82017-07-18 16:34:25 -04003262 UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Nick Harper60a85cb2016-09-23 16:25:11 -07003263 if (!p256) {
3264 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3265 return 0;
3266 }
3267
David Benjamin86e95b82017-07-18 16:34:25 -04003268 UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3269 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
David Benjamin81678aa2017-07-12 22:43:42 -04003270 if (!sig || !x || !y) {
3271 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003272 }
3273
3274 const uint8_t *p = CBS_data(&extension);
David Benjamin81678aa2017-07-12 22:43:42 -04003275 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3276 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3277 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3278 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
3279 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003280 }
3281
David Benjamin86e95b82017-07-18 16:34:25 -04003282 UniquePtr<EC_KEY> key(EC_KEY_new());
3283 UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
David Benjamin81678aa2017-07-12 22:43:42 -04003284 if (!key || !point ||
3285 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3286 y.get(), nullptr) ||
3287 !EC_KEY_set_group(key.get(), p256.get()) ||
3288 !EC_KEY_set_public_key(key.get(), point.get())) {
3289 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003290 }
3291
3292 uint8_t digest[EVP_MAX_MD_SIZE];
3293 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003294 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003295 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003296 }
3297
David Benjamin81678aa2017-07-12 22:43:42 -04003298 int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
Nick Harper60a85cb2016-09-23 16:25:11 -07003299#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3300 sig_ok = 1;
3301#endif
3302 if (!sig_ok) {
3303 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3304 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
David Benjamin046bc1f2017-08-31 15:06:42 -04003305 ssl->s3->tlsext_channel_id_valid = false;
David Benjamin81678aa2017-07-12 22:43:42 -04003306 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003307 }
3308
David Benjamin17cf2cb2016-12-13 01:07:13 -05003309 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
David Benjamin81678aa2017-07-12 22:43:42 -04003310 return 1;
Nick Harper60a85cb2016-09-23 16:25:11 -07003311}
3312
Steven Valdez908ac192017-01-12 13:17:07 -05003313int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3314 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003315 uint8_t digest[EVP_MAX_MD_SIZE];
3316 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003317 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003318 return 0;
3319 }
3320
3321 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3322 if (ec_key == NULL) {
3323 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3324 return 0;
3325 }
3326
3327 int ret = 0;
3328 BIGNUM *x = BN_new();
3329 BIGNUM *y = BN_new();
3330 ECDSA_SIG *sig = NULL;
3331 if (x == NULL || y == NULL ||
3332 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3333 EC_KEY_get0_public_key(ec_key),
3334 x, y, NULL)) {
3335 goto err;
3336 }
3337
3338 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3339 if (sig == NULL) {
3340 goto err;
3341 }
3342
3343 CBB child;
3344 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3345 !CBB_add_u16_length_prefixed(cbb, &child) ||
3346 !BN_bn2cbb_padded(&child, 32, x) ||
3347 !BN_bn2cbb_padded(&child, 32, y) ||
3348 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3349 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3350 !CBB_flush(cbb)) {
3351 goto err;
3352 }
3353
3354 ret = 1;
3355
3356err:
3357 BN_free(x);
3358 BN_free(y);
3359 ECDSA_SIG_free(sig);
3360 return ret;
3361}
3362
Steven Valdez908ac192017-01-12 13:17:07 -05003363int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3364 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003365 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3366 uint8_t *msg;
3367 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003368 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003369 ssl_cert_verify_channel_id)) {
3370 return 0;
3371 }
3372 SHA256(msg, msg_len, out);
3373 *out_len = SHA256_DIGEST_LENGTH;
3374 OPENSSL_free(msg);
3375 return 1;
3376 }
3377
Nick Harper95594012016-10-20 14:07:13 -07003378 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003379
Nick Harper95594012016-10-20 14:07:13 -07003380 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003381 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003382 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003383
Steven Valdez87eab492016-06-27 16:34:59 -04003384 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003385 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003386 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003387 if (ssl->session->original_handshake_hash_len == 0) {
3388 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003389 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003390 }
Nick Harper95594012016-10-20 14:07:13 -07003391 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3392 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003393 }
3394
Steven Valdez908ac192017-01-12 13:17:07 -05003395 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3396 size_t hs_hash_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003397 if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003398 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003399 }
Steven Valdez908ac192017-01-12 13:17:07 -05003400 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003401 SHA256_Final(out, &ctx);
3402 *out_len = SHA256_DIGEST_LENGTH;
3403 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003404}
Adam Langley1258b6a2014-06-20 12:00:00 -07003405
David Benjaminc11ea9422017-08-29 16:33:21 -04003406// tls1_record_handshake_hashes_for_channel_id records the current handshake
3407// hashes in |hs->new_session| so that Channel ID resumptions can sign that
3408// data.
Steven Valdez908ac192017-01-12 13:17:07 -05003409int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3410 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003411 // This function should never be called for a resumed session because the
3412 // handshake hashes that we wish to record are for the original, full
3413 // handshake.
Steven Valdez87eab492016-06-27 16:34:59 -04003414 if (ssl->session != NULL) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04003415 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003416 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003417
David Benjamina3d76d02017-07-14 19:36:07 -04003418 static_assert(
David Benjamin45738dd2017-02-09 20:01:26 -05003419 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
David Benjamina3d76d02017-07-14 19:36:07 -04003420 "original_handshake_hash is too small");
Steven Valdez908ac192017-01-12 13:17:07 -05003421
3422 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003423 if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
3424 &digest_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04003425 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003426 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003427
David Benjamina3d76d02017-07-14 19:36:07 -04003428 static_assert(EVP_MAX_MD_SIZE <= 0xff,
3429 "EVP_MAX_MD_SIZE does not fit in uint8_t");
David Benjamin45738dd2017-02-09 20:01:26 -05003430 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003431
Adam Langleyfcf25832014-12-18 17:42:32 -08003432 return 1;
3433}
Nick Harper60a85cb2016-09-23 16:25:11 -07003434
3435int ssl_do_channel_id_callback(SSL *ssl) {
3436 if (ssl->tlsext_channel_id_private != NULL ||
3437 ssl->ctx->channel_id_cb == NULL) {
3438 return 1;
3439 }
3440
3441 EVP_PKEY *key = NULL;
3442 ssl->ctx->channel_id_cb(ssl, &key);
3443 if (key == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003444 // The caller should try again later.
Nick Harper60a85cb2016-09-23 16:25:11 -07003445 return 1;
3446 }
3447
3448 int ret = SSL_set1_tls_channel_id(ssl, key);
3449 EVP_PKEY_free(key);
3450 return ret;
3451}
Adam Langleycfa08c32016-11-17 13:21:27 -08003452
3453int ssl_is_sct_list_valid(const CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003454 // Shallow parse the SCT list for sanity. By the RFC
3455 // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3456 // of the SCTs may be empty.
Adam Langleycfa08c32016-11-17 13:21:27 -08003457 CBS copy = *contents;
3458 CBS sct_list;
3459 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3460 CBS_len(&copy) != 0 ||
3461 CBS_len(&sct_list) == 0) {
3462 return 0;
3463 }
3464
3465 while (CBS_len(&sct_list) > 0) {
3466 CBS sct;
3467 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3468 CBS_len(&sct) == 0) {
3469 return 0;
3470 }
3471 }
3472
3473 return 1;
3474}
David Benjamin86e95b82017-07-18 16:34:25 -04003475
3476} // namespace bssl
3477
3478using namespace bssl;
3479
3480int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
3481 uint16_t extension_type,
3482 const uint8_t **out_data,
3483 size_t *out_len) {
3484 CBS cbs;
3485 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
3486 return 0;
3487 }
3488
3489 *out_data = CBS_data(&cbs);
3490 *out_len = CBS_len(&cbs);
3491 return 1;
3492}
3493
3494void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
3495 ctx->ed25519_enabled = !!enabled;
3496}
3497
3498int SSL_extension_supported(unsigned extension_value) {
3499 uint32_t index;
3500 return extension_value == TLSEXT_TYPE_padding ||
3501 tls_extension_find(&index, extension_value) != NULL;
3502}