blob: 3b0a335b90472967f92d1be6ac3a390b622b362c [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 Benjaminb1cf48e2017-09-21 11:37:46 -04001014 hs->peer_sigalgs.Reset();
Adam Langley2e857bd2015-07-01 16:09:19 -07001015 if (contents == NULL) {
1016 return 1;
1017 }
1018
1019 CBS supported_signature_algorithms;
1020 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001021 CBS_len(contents) != 0 ||
1022 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001023 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001024 return 0;
1025 }
1026
1027 return 1;
1028}
1029
Adam Langley2e857bd2015-07-01 16:09:19 -07001030
David Benjaminc11ea9422017-08-29 16:33:21 -04001031// OCSP Stapling.
1032//
1033// https://tools.ietf.org/html/rfc6066#section-8
Adam Langleybb0bd042015-07-01 16:21:03 -07001034
David Benjamin8c880a22016-12-03 02:20:34 -05001035static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1036 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001037 if (!ssl->ocsp_stapling_enabled) {
1038 return 1;
1039 }
1040
1041 CBB contents;
1042 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1043 !CBB_add_u16_length_prefixed(out, &contents) ||
1044 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1045 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1046 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1047 !CBB_flush(out)) {
1048 return 0;
1049 }
1050
1051 return 1;
1052}
1053
David Benjamin8c880a22016-12-03 02:20:34 -05001054static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001055 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001056 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001057 if (contents == NULL) {
1058 return 1;
1059 }
1060
David Benjaminc11ea9422017-08-29 16:33:21 -04001061 // TLS 1.3 OCSP responses are included in the Certificate extensions.
Steven Valdeza833c352016-11-01 13:39:36 -04001062 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001063 return 0;
1064 }
1065
David Benjaminc11ea9422017-08-29 16:33:21 -04001066 // OCSP stapling is forbidden on non-certificate ciphers.
Steven Valdeza833c352016-11-01 13:39:36 -04001067 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001068 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001069 return 0;
1070 }
1071
David Benjaminc11ea9422017-08-29 16:33:21 -04001072 // Note this does not check for resumption in TLS 1.2. Sending
1073 // status_request here does not make sense, but OpenSSL does so and the
1074 // specification does not say anything. Tolerate it but ignore it.
David Benjamin942f4ed2016-07-16 19:03:49 +03001075
David Benjaminfd45ee72017-08-31 14:49:09 -04001076 hs->certificate_status_expected = true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001077 return 1;
1078}
1079
David Benjamin8c880a22016-12-03 02:20:34 -05001080static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001081 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001082 if (contents == NULL) {
1083 return 1;
1084 }
1085
1086 uint8_t status_type;
1087 if (!CBS_get_u8(contents, &status_type)) {
1088 return 0;
1089 }
1090
David Benjaminc11ea9422017-08-29 16:33:21 -04001091 // We cannot decide whether OCSP stapling will occur yet because the correct
1092 // SSL_CTX might not have been selected.
David Benjamin8c880a22016-12-03 02:20:34 -05001093 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001094
Adam Langleybb0bd042015-07-01 16:21:03 -07001095 return 1;
1096}
1097
David Benjamin8c880a22016-12-03 02:20:34 -05001098static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1099 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001100 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001101 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001102 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001103 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001104 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001105 return 1;
1106 }
1107
David Benjaminfd45ee72017-08-31 14:49:09 -04001108 hs->certificate_status_expected = true;
David Benjamin942f4ed2016-07-16 19:03:49 +03001109
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001110 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001111 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001112}
1113
1114
David Benjaminc11ea9422017-08-29 16:33:21 -04001115// Next protocol negotiation.
1116//
1117// https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
Adam Langley97dfcbf2015-07-01 18:35:20 -07001118
David Benjamin8c880a22016-12-03 02:20:34 -05001119static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1120 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001121 if (ssl->s3->initial_handshake_complete ||
1122 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001123 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001124 return 1;
1125 }
1126
1127 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1128 !CBB_add_u16(out, 0 /* length */)) {
1129 return 0;
1130 }
1131
1132 return 1;
1133}
1134
David Benjamin8c880a22016-12-03 02:20:34 -05001135static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001136 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001137 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001138 if (contents == NULL) {
1139 return 1;
1140 }
1141
Steven Valdez143e8b32016-07-11 13:19:03 -04001142 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1143 return 0;
1144 }
1145
David Benjaminc11ea9422017-08-29 16:33:21 -04001146 // If any of these are false then we should never have sent the NPN
1147 // extension in the ClientHello and thus this function should never have been
1148 // called.
Adam Langley97dfcbf2015-07-01 18:35:20 -07001149 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001150 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001151 assert(ssl->ctx->next_proto_select_cb != NULL);
1152
David Benjamin76c2efc2015-08-31 14:24:29 -04001153 if (ssl->s3->alpn_selected != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001154 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001155 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1156 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1157 return 0;
1158 }
1159
Adam Langley97dfcbf2015-07-01 18:35:20 -07001160 const uint8_t *const orig_contents = CBS_data(contents);
1161 const size_t orig_len = CBS_len(contents);
1162
1163 while (CBS_len(contents) != 0) {
1164 CBS proto;
1165 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1166 CBS_len(&proto) == 0) {
1167 return 0;
1168 }
1169 }
1170
1171 uint8_t *selected;
1172 uint8_t selected_len;
1173 if (ssl->ctx->next_proto_select_cb(
1174 ssl, &selected, &selected_len, orig_contents, orig_len,
1175 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1176 *out_alert = SSL_AD_INTERNAL_ERROR;
1177 return 0;
1178 }
1179
David Benjamin79978df2015-12-25 15:56:49 -05001180 OPENSSL_free(ssl->s3->next_proto_negotiated);
David Benjamin81678aa2017-07-12 22:43:42 -04001181 ssl->s3->next_proto_negotiated =
1182 (uint8_t *)BUF_memdup(selected, selected_len);
David Benjamin79978df2015-12-25 15:56:49 -05001183 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001184 *out_alert = SSL_AD_INTERNAL_ERROR;
1185 return 0;
1186 }
1187
David Benjamin79978df2015-12-25 15:56:49 -05001188 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjaminfd45ee72017-08-31 14:49:09 -04001189 hs->next_proto_neg_seen = true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001190
1191 return 1;
1192}
1193
David Benjamin8c880a22016-12-03 02:20:34 -05001194static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001195 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001196 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001197 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1198 return 1;
1199 }
1200
Adam Langley97dfcbf2015-07-01 18:35:20 -07001201 if (contents != NULL && CBS_len(contents) != 0) {
1202 return 0;
1203 }
1204
1205 if (contents == NULL ||
1206 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001207 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001208 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001209 return 1;
1210 }
1211
David Benjaminfd45ee72017-08-31 14:49:09 -04001212 hs->next_proto_neg_seen = true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001213 return 1;
1214}
1215
David Benjamin8c880a22016-12-03 02:20:34 -05001216static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1217 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001218 // |next_proto_neg_seen| might have been cleared when an ALPN extension was
1219 // parsed.
David Benjamin8c880a22016-12-03 02:20:34 -05001220 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001221 return 1;
1222 }
1223
1224 const uint8_t *npa;
1225 unsigned npa_len;
1226
1227 if (ssl->ctx->next_protos_advertised_cb(
1228 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1229 SSL_TLSEXT_ERR_OK) {
David Benjaminfd45ee72017-08-31 14:49:09 -04001230 hs->next_proto_neg_seen = false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001231 return 1;
1232 }
1233
1234 CBB contents;
1235 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1236 !CBB_add_u16_length_prefixed(out, &contents) ||
1237 !CBB_add_bytes(&contents, npa, npa_len) ||
1238 !CBB_flush(out)) {
1239 return 0;
1240 }
1241
1242 return 1;
1243}
1244
1245
David Benjaminc11ea9422017-08-29 16:33:21 -04001246// Signed certificate timestamps.
1247//
1248// https://tools.ietf.org/html/rfc6962#section-3.3.1
Adam Langleyab8d87d2015-07-10 12:21:39 -07001249
David Benjamin8c880a22016-12-03 02:20:34 -05001250static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1251 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001252 if (!ssl->signed_cert_timestamps_enabled) {
1253 return 1;
1254 }
1255
1256 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1257 !CBB_add_u16(out, 0 /* length */)) {
1258 return 0;
1259 }
1260
1261 return 1;
1262}
1263
David Benjamin8c880a22016-12-03 02:20:34 -05001264static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001265 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001266 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001267 if (contents == NULL) {
1268 return 1;
1269 }
1270
David Benjaminc11ea9422017-08-29 16:33:21 -04001271 // TLS 1.3 SCTs are included in the Certificate extensions.
Steven Valdeza833c352016-11-01 13:39:36 -04001272 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001273 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001274 return 0;
1275 }
1276
David Benjaminc11ea9422017-08-29 16:33:21 -04001277 // If this is false then we should never have sent the SCT extension in the
1278 // ClientHello and thus this function should never have been called.
Adam Langleyab8d87d2015-07-10 12:21:39 -07001279 assert(ssl->signed_cert_timestamps_enabled);
1280
Adam Langleycfa08c32016-11-17 13:21:27 -08001281 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001282 *out_alert = SSL_AD_DECODE_ERROR;
1283 return 0;
1284 }
1285
David Benjaminc11ea9422017-08-29 16:33:21 -04001286 // Session resumption uses the original session information. The extension
1287 // should not be sent on resumption, but RFC 6962 did not make it a
1288 // requirement, so tolerate this.
1289 //
1290 // TODO(davidben): Enforce this anyway.
David Benjamin8fc2dc02017-08-22 15:07:51 -07001291 if (!ssl->s3->session_reused) {
1292 CRYPTO_BUFFER_free(hs->new_session->signed_cert_timestamp_list);
1293 hs->new_session->signed_cert_timestamp_list =
1294 CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool);
1295 if (hs->new_session->signed_cert_timestamp_list == nullptr) {
1296 *out_alert = SSL_AD_INTERNAL_ERROR;
1297 return 0;
1298 }
Adam Langleyab8d87d2015-07-10 12:21:39 -07001299 }
1300
1301 return 1;
1302}
1303
David Benjamin8c880a22016-12-03 02:20:34 -05001304static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001305 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001306 if (contents == NULL) {
1307 return 1;
1308 }
1309
1310 if (CBS_len(contents) != 0) {
1311 return 0;
1312 }
1313
David Benjaminfd45ee72017-08-31 14:49:09 -04001314 hs->scts_requested = true;
David Benjamin53210cb2016-11-16 09:01:48 +09001315 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001316}
1317
David Benjamin8c880a22016-12-03 02:20:34 -05001318static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1319 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001320 // The extension shouldn't be sent when resuming sessions.
Steven Valdeza833c352016-11-01 13:39:36 -04001321 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1322 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001323 ssl->cert->signed_cert_timestamp_list == NULL) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001324 return 1;
1325 }
1326
1327 CBB contents;
1328 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1329 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001330 CBB_add_bytes(
1331 &contents,
1332 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1333 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001334 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001335}
1336
1337
David Benjaminc11ea9422017-08-29 16:33:21 -04001338// Application-level Protocol Negotiation.
1339//
1340// https://tools.ietf.org/html/rfc7301
Adam Langleyf18e4532015-07-10 13:39:53 -07001341
David Benjamin8c880a22016-12-03 02:20:34 -05001342static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1343 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001344 if (ssl->alpn_client_proto_list == NULL ||
1345 ssl->s3->initial_handshake_complete) {
1346 return 1;
1347 }
1348
1349 CBB contents, proto_list;
1350 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1351 !CBB_add_u16_length_prefixed(out, &contents) ||
1352 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1353 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1354 ssl->alpn_client_proto_list_len) ||
1355 !CBB_flush(out)) {
1356 return 0;
1357 }
1358
1359 return 1;
1360}
1361
David Benjamin8c880a22016-12-03 02:20:34 -05001362static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001363 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001364 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001365 if (contents == NULL) {
1366 return 1;
1367 }
1368
1369 assert(!ssl->s3->initial_handshake_complete);
1370 assert(ssl->alpn_client_proto_list != NULL);
1371
David Benjamin8c880a22016-12-03 02:20:34 -05001372 if (hs->next_proto_neg_seen) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001373 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001374 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1375 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1376 return 0;
1377 }
1378
David Benjaminc11ea9422017-08-29 16:33:21 -04001379 // The extension data consists of a ProtocolNameList which must have
1380 // exactly one ProtocolName. Each of these is length-prefixed.
Adam Langleyf18e4532015-07-10 13:39:53 -07001381 CBS protocol_name_list, protocol_name;
1382 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1383 CBS_len(contents) != 0 ||
1384 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001385 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001386 CBS_len(&protocol_name) == 0 ||
1387 CBS_len(&protocol_name_list) != 0) {
1388 return 0;
1389 }
1390
David Benjaminc8ff30c2017-04-04 13:52:36 -04001391 if (!ssl->ctx->allow_unknown_alpn_protos) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001392 // Check that the protocol name is one of the ones we advertised.
David Benjaminc8ff30c2017-04-04 13:52:36 -04001393 int protocol_ok = 0;
1394 CBS client_protocol_name_list, client_protocol_name;
1395 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1396 ssl->alpn_client_proto_list_len);
1397 while (CBS_len(&client_protocol_name_list) > 0) {
1398 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1399 &client_protocol_name)) {
1400 *out_alert = SSL_AD_INTERNAL_ERROR;
1401 return 0;
1402 }
1403
1404 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1405 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1406 CBS_data(&protocol_name),
1407 CBS_len(&protocol_name)) == 0) {
1408 protocol_ok = 1;
1409 break;
1410 }
1411 }
1412
1413 if (!protocol_ok) {
1414 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1415 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3e517572016-08-11 11:52:23 -04001416 return 0;
1417 }
David Benjamin3e517572016-08-11 11:52:23 -04001418 }
1419
Adam Langleyf18e4532015-07-10 13:39:53 -07001420 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1421 &ssl->s3->alpn_selected_len)) {
1422 *out_alert = SSL_AD_INTERNAL_ERROR;
1423 return 0;
1424 }
1425
1426 return 1;
1427}
1428
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001429int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001430 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001431 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001432 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001433 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001434 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001435 client_hello, &contents,
1436 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001437 // Ignore ALPN if not configured or no extension was supplied.
Adam Langleyf18e4532015-07-10 13:39:53 -07001438 return 1;
1439 }
1440
David Benjaminc11ea9422017-08-29 16:33:21 -04001441 // ALPN takes precedence over NPN.
David Benjaminfd45ee72017-08-31 14:49:09 -04001442 hs->next_proto_neg_seen = false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001443
1444 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001445 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1446 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001447 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001448 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1449 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001450 return 0;
1451 }
1452
David Benjaminc11ea9422017-08-29 16:33:21 -04001453 // Validate the protocol list.
Adam Langleyf18e4532015-07-10 13:39:53 -07001454 CBS protocol_name_list_copy = protocol_name_list;
1455 while (CBS_len(&protocol_name_list_copy) > 0) {
1456 CBS protocol_name;
1457
1458 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001459 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001460 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001461 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1462 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001463 return 0;
1464 }
1465 }
1466
1467 const uint8_t *selected;
1468 uint8_t selected_len;
1469 if (ssl->ctx->alpn_select_cb(
1470 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1471 CBS_len(&protocol_name_list),
1472 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1473 OPENSSL_free(ssl->s3->alpn_selected);
David Benjamin81678aa2017-07-12 22:43:42 -04001474 ssl->s3->alpn_selected = (uint8_t *)BUF_memdup(selected, selected_len);
Adam Langleyf18e4532015-07-10 13:39:53 -07001475 if (ssl->s3->alpn_selected == NULL) {
1476 *out_alert = SSL_AD_INTERNAL_ERROR;
1477 return 0;
1478 }
1479 ssl->s3->alpn_selected_len = selected_len;
1480 }
1481
1482 return 1;
1483}
1484
David Benjamin8c880a22016-12-03 02:20:34 -05001485static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1486 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001487 if (ssl->s3->alpn_selected == NULL) {
1488 return 1;
1489 }
1490
1491 CBB contents, proto_list, proto;
1492 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1493 !CBB_add_u16_length_prefixed(out, &contents) ||
1494 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1495 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001496 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1497 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001498 !CBB_flush(out)) {
1499 return 0;
1500 }
1501
1502 return 1;
1503}
1504
1505
David Benjaminc11ea9422017-08-29 16:33:21 -04001506// Channel ID.
1507//
1508// https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
Adam Langley49c7af12015-07-10 14:33:46 -07001509
David Benjamin8c880a22016-12-03 02:20:34 -05001510static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
David Benjamin046bc1f2017-08-31 15:06:42 -04001511 hs->ssl->s3->tlsext_channel_id_valid = false;
Adam Langley49c7af12015-07-10 14:33:46 -07001512}
1513
David Benjamin8c880a22016-12-03 02:20:34 -05001514static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1515 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001516 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001517 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001518 return 1;
1519 }
1520
1521 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1522 !CBB_add_u16(out, 0 /* length */)) {
1523 return 0;
1524 }
1525
1526 return 1;
1527}
1528
David Benjamin8c880a22016-12-03 02:20:34 -05001529static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1530 uint8_t *out_alert, CBS *contents) {
1531 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001532 if (contents == NULL) {
1533 return 1;
1534 }
1535
David Benjamince079fd2016-08-02 16:22:34 -04001536 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001537 assert(ssl->tlsext_channel_id_enabled);
1538
1539 if (CBS_len(contents) != 0) {
1540 return 0;
1541 }
1542
David Benjamin046bc1f2017-08-31 15:06:42 -04001543 ssl->s3->tlsext_channel_id_valid = true;
Adam Langley49c7af12015-07-10 14:33:46 -07001544 return 1;
1545}
1546
David Benjamin8c880a22016-12-03 02:20:34 -05001547static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1548 uint8_t *out_alert, CBS *contents) {
1549 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001550 if (contents == NULL ||
1551 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001552 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001553 return 1;
1554 }
1555
1556 if (CBS_len(contents) != 0) {
1557 return 0;
1558 }
1559
David Benjamin046bc1f2017-08-31 15:06:42 -04001560 ssl->s3->tlsext_channel_id_valid = true;
Adam Langley49c7af12015-07-10 14:33:46 -07001561 return 1;
1562}
1563
David Benjamin8c880a22016-12-03 02:20:34 -05001564static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1565 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001566 if (!ssl->s3->tlsext_channel_id_valid) {
1567 return 1;
1568 }
1569
1570 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1571 !CBB_add_u16(out, 0 /* length */)) {
1572 return 0;
1573 }
1574
1575 return 1;
1576}
1577
Adam Langley391250d2015-07-15 19:06:07 -07001578
David Benjaminc11ea9422017-08-29 16:33:21 -04001579// Secure Real-time Transport Protocol (SRTP) extension.
1580//
1581// https://tools.ietf.org/html/rfc5764
Adam Langley391250d2015-07-15 19:06:07 -07001582
Adam Langley391250d2015-07-15 19:06:07 -07001583
David Benjamin8c880a22016-12-03 02:20:34 -05001584static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1585 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001586}
1587
David Benjamin8c880a22016-12-03 02:20:34 -05001588static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1589 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001590 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
David Benjaminee910bf2017-07-25 22:36:00 -04001591 if (profiles == NULL ||
1592 sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
Adam Langley391250d2015-07-15 19:06:07 -07001593 return 1;
1594 }
1595
1596 CBB contents, profile_ids;
1597 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1598 !CBB_add_u16_length_prefixed(out, &contents) ||
1599 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1600 return 0;
1601 }
1602
David Benjaminee910bf2017-07-25 22:36:00 -04001603 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
1604 if (!CBB_add_u16(&profile_ids, profile->id)) {
Adam Langley391250d2015-07-15 19:06:07 -07001605 return 0;
1606 }
1607 }
1608
1609 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1610 !CBB_flush(out)) {
1611 return 0;
1612 }
1613
1614 return 1;
1615}
1616
David Benjamin8c880a22016-12-03 02:20:34 -05001617static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001618 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001619 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001620 if (contents == NULL) {
1621 return 1;
1622 }
1623
David Benjaminc11ea9422017-08-29 16:33:21 -04001624 // The extension consists of a u16-prefixed profile ID list containing a
1625 // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1626 //
1627 // See https://tools.ietf.org/html/rfc5764#section-4.1.1
Adam Langley391250d2015-07-15 19:06:07 -07001628 CBS profile_ids, srtp_mki;
1629 uint16_t profile_id;
1630 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1631 !CBS_get_u16(&profile_ids, &profile_id) ||
1632 CBS_len(&profile_ids) != 0 ||
1633 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1634 CBS_len(contents) != 0) {
1635 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1636 return 0;
1637 }
1638
1639 if (CBS_len(&srtp_mki) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001640 // Must be no MKI, since we never offer one.
Adam Langley391250d2015-07-15 19:06:07 -07001641 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1642 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1643 return 0;
1644 }
1645
1646 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1647
David Benjaminc11ea9422017-08-29 16:33:21 -04001648 // Check to see if the server gave us something we support (and presumably
1649 // offered).
David Benjaminee910bf2017-07-25 22:36:00 -04001650 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001651 if (profile->id == profile_id) {
1652 ssl->srtp_profile = profile;
1653 return 1;
1654 }
1655 }
1656
1657 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1658 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1659 return 0;
1660}
1661
David Benjamin8c880a22016-12-03 02:20:34 -05001662static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001663 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001664 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001665 if (contents == NULL) {
1666 return 1;
1667 }
1668
1669 CBS profile_ids, srtp_mki;
1670 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1671 CBS_len(&profile_ids) < 2 ||
1672 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1673 CBS_len(contents) != 0) {
1674 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1675 return 0;
1676 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001677 // Discard the MKI value for now.
Adam Langley391250d2015-07-15 19:06:07 -07001678
1679 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1680 SSL_get_srtp_profiles(ssl);
1681
David Benjaminc11ea9422017-08-29 16:33:21 -04001682 // Pick the server's most preferred profile.
David Benjaminee910bf2017-07-25 22:36:00 -04001683 for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001684 CBS profile_ids_tmp;
1685 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1686
1687 while (CBS_len(&profile_ids_tmp) > 0) {
1688 uint16_t profile_id;
1689 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1690 return 0;
1691 }
1692
1693 if (server_profile->id == profile_id) {
1694 ssl->srtp_profile = server_profile;
1695 return 1;
1696 }
1697 }
1698 }
1699
1700 return 1;
1701}
1702
David Benjamin8c880a22016-12-03 02:20:34 -05001703static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1704 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001705 if (ssl->srtp_profile == NULL) {
1706 return 1;
1707 }
1708
1709 CBB contents, profile_ids;
1710 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1711 !CBB_add_u16_length_prefixed(out, &contents) ||
1712 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1713 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1714 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1715 !CBB_flush(out)) {
1716 return 0;
1717 }
1718
1719 return 1;
1720}
1721
Adam Langleybdd5d662015-07-20 16:19:08 -07001722
David Benjaminc11ea9422017-08-29 16:33:21 -04001723// EC point formats.
1724//
1725// https://tools.ietf.org/html/rfc4492#section-5.1.2
Adam Langleybdd5d662015-07-20 16:19:08 -07001726
David Benjamin8c880a22016-12-03 02:20:34 -05001727static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001728 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001729 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1730 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001731 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1732 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001733 !CBB_flush(out)) {
1734 return 0;
1735 }
1736
1737 return 1;
1738}
1739
David Benjamin8c880a22016-12-03 02:20:34 -05001740static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001741 // The point format extension is unneccessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -04001742 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin70aba262016-11-01 12:08:15 -04001743 return 1;
1744 }
1745
David Benjamin8c880a22016-12-03 02:20:34 -05001746 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001747}
1748
David Benjamin8c880a22016-12-03 02:20:34 -05001749static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001750 CBS *contents) {
1751 if (contents == NULL) {
1752 return 1;
1753 }
1754
David Benjamin8c880a22016-12-03 02:20:34 -05001755 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001756 return 0;
1757 }
1758
Adam Langleybdd5d662015-07-20 16:19:08 -07001759 CBS ec_point_format_list;
1760 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1761 CBS_len(contents) != 0) {
1762 return 0;
1763 }
1764
David Benjaminc11ea9422017-08-29 16:33:21 -04001765 // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1766 // point format.
David Benjamin17cf2cb2016-12-13 01:07:13 -05001767 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1768 TLSEXT_ECPOINTFORMAT_uncompressed,
1769 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001770 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001771 return 0;
1772 }
1773
1774 return 1;
1775}
1776
David Benjamin8c880a22016-12-03 02:20:34 -05001777static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001778 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001779 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001780 return 1;
1781 }
1782
David Benjamin8c880a22016-12-03 02:20:34 -05001783 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001784}
1785
David Benjamin8c880a22016-12-03 02:20:34 -05001786static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1787 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001788 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1789 return 1;
1790 }
1791
David Benjamin45738dd2017-02-09 20:01:26 -05001792 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1793 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001794 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001795
1796 if (!using_ecc) {
1797 return 1;
1798 }
1799
David Benjamin8c880a22016-12-03 02:20:34 -05001800 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001801}
1802
Steven Valdeza833c352016-11-01 13:39:36 -04001803
David Benjaminc11ea9422017-08-29 16:33:21 -04001804// Pre Shared Key
1805//
1806// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
Steven Valdez4aa154e2016-07-29 14:32:55 -04001807
David Benjamin8c880a22016-12-03 02:20:34 -05001808static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1809 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001810 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001811 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001812 return 0;
1813 }
1814
Steven Valdez8f36c512017-06-20 10:55:02 -04001815 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001816 return 15 + ssl->session->tlsext_ticklen + binder_len;
1817}
1818
David Benjamin8c880a22016-12-03 02:20:34 -05001819static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1820 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001821 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04001822 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001823 return 1;
1824 }
1825
David Benjaminad8f5e12017-02-20 17:00:20 -05001826 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001827 ssl_get_current_time(ssl, &now);
1828 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1829 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1830
David Benjaminc11ea9422017-08-29 16:33:21 -04001831 // Fill in a placeholder zero binder of the appropriate length. It will be
1832 // computed and filled in later after length prefixes are computed.
Steven Valdeza833c352016-11-01 13:39:36 -04001833 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Steven Valdez8f36c512017-06-20 10:55:02 -04001834 size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001835
1836 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001837 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1838 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001839 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001840 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1841 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001842 ssl->session->tlsext_ticklen) ||
1843 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1844 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1845 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1846 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001847 return 0;
1848 }
1849
David Benjaminfd45ee72017-08-31 14:49:09 -04001850 hs->needs_psk_binder = true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001851 return CBB_flush(out);
1852}
1853
David Benjamin8baf9632016-11-17 17:11:16 +09001854int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1855 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001856 CBS *contents) {
1857 uint16_t psk_id;
1858 if (!CBS_get_u16(contents, &psk_id) ||
1859 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001860 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001861 *out_alert = SSL_AD_DECODE_ERROR;
1862 return 0;
1863 }
1864
David Benjaminc11ea9422017-08-29 16:33:21 -04001865 // We only advertise one PSK identity, so the only legal index is zero.
Steven Valdez4aa154e2016-07-29 14:32:55 -04001866 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001867 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001868 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1869 return 0;
1870 }
1871
1872 return 1;
1873}
1874
David Benjamin35ac5b72017-03-03 15:05:56 -05001875int ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001876 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001877 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001878 // We only process the first PSK identity since we don't support pure PSK.
David Benjamin707af292017-03-10 17:47:18 -05001879 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001880 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001881 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001882 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001883 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001884 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001885 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001886 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001887 *out_alert = SSL_AD_DECODE_ERROR;
1888 return 0;
1889 }
1890
Steven Valdeza833c352016-11-01 13:39:36 -04001891 *out_binders = binders;
1892
David Benjaminc11ea9422017-08-29 16:33:21 -04001893 // Check the syntax of the remaining identities, but do not process them.
David Benjaminaedf3032016-12-01 16:47:56 -05001894 size_t num_identities = 1;
1895 while (CBS_len(&identities) != 0) {
1896 CBS unused_ticket;
1897 uint32_t unused_obfuscated_ticket_age;
1898 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1899 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1900 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1901 *out_alert = SSL_AD_DECODE_ERROR;
1902 return 0;
1903 }
1904
1905 num_identities++;
1906 }
1907
David Benjaminc11ea9422017-08-29 16:33:21 -04001908 // Check the syntax of the binders. The value will be checked later if
1909 // resuming.
David Benjaminaedf3032016-12-01 16:47:56 -05001910 size_t num_binders = 0;
1911 while (CBS_len(&binders) != 0) {
1912 CBS binder;
1913 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1914 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1915 *out_alert = SSL_AD_DECODE_ERROR;
1916 return 0;
1917 }
1918
1919 num_binders++;
1920 }
1921
1922 if (num_identities != num_binders) {
1923 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1924 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04001925 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04001926 }
1927
David Benjamine7f60a22016-11-16 18:54:25 +09001928 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001929}
1930
David Benjamin8baf9632016-11-17 17:11:16 +09001931int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1932 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001933 return 1;
1934 }
1935
1936 CBB contents;
1937 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1938 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001939 // We only consider the first identity for resumption
Steven Valdez4aa154e2016-07-29 14:32:55 -04001940 !CBB_add_u16(&contents, 0) ||
1941 !CBB_flush(out)) {
1942 return 0;
1943 }
1944
1945 return 1;
1946}
1947
1948
David Benjaminc11ea9422017-08-29 16:33:21 -04001949// Pre-Shared Key Exchange Modes
1950//
1951// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7
Steven Valdeza4ee74d2016-11-29 13:36:45 -05001952
David Benjamin8c880a22016-12-03 02:20:34 -05001953static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
1954 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04001955 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001956 return 1;
1957 }
1958
1959 CBB contents, ke_modes;
1960 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
1961 !CBB_add_u16_length_prefixed(out, &contents) ||
1962 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
1963 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
1964 return 0;
1965 }
1966
1967 return CBB_flush(out);
1968}
1969
David Benjamin8c880a22016-12-03 02:20:34 -05001970static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09001971 uint8_t *out_alert,
1972 CBS *contents) {
1973 if (contents == NULL) {
1974 return 1;
1975 }
1976
Steven Valdeza833c352016-11-01 13:39:36 -04001977 CBS ke_modes;
1978 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
1979 CBS_len(&ke_modes) == 0 ||
1980 CBS_len(contents) != 0) {
1981 *out_alert = SSL_AD_DECODE_ERROR;
1982 return 0;
1983 }
1984
David Benjaminc11ea9422017-08-29 16:33:21 -04001985 // We only support tickets with PSK_DHE_KE.
David Benjamin17cf2cb2016-12-13 01:07:13 -05001986 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
1987 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04001988
1989 return 1;
1990}
1991
1992
David Benjaminc11ea9422017-08-29 16:33:21 -04001993// Early Data Indication
1994//
1995// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
Steven Valdeza4ee74d2016-11-29 13:36:45 -05001996
David Benjamin8c880a22016-12-03 02:20:34 -05001997static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05001998 SSL *const ssl = hs->ssl;
Steven Valdez2d850622017-01-11 11:34:52 -05001999 if (ssl->session == NULL ||
Steven Valdez8f36c512017-06-20 10:55:02 -04002000 SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION ||
Steven Valdez2d850622017-01-11 11:34:52 -05002001 ssl->session->ticket_max_early_data == 0 ||
2002 hs->received_hello_retry_request ||
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -05002003 !ssl->cert->enable_early_data) {
Steven Valdez2d850622017-01-11 11:34:52 -05002004 return 1;
2005 }
2006
David Benjaminfd45ee72017-08-31 14:49:09 -04002007 hs->early_data_offered = true;
Steven Valdez2d850622017-01-11 11:34:52 -05002008
2009 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2010 !CBB_add_u16(out, 0) ||
2011 !CBB_flush(out)) {
2012 return 0;
2013 }
2014
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002015 return 1;
2016}
2017
Steven Valdez2d850622017-01-11 11:34:52 -05002018static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002019 uint8_t *out_alert, CBS *contents) {
2020 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002021 if (contents == NULL) {
2022 return 1;
2023 }
2024
2025 if (CBS_len(contents) != 0) {
2026 *out_alert = SSL_AD_DECODE_ERROR;
2027 return 0;
2028 }
2029
Steven Valdez2d850622017-01-11 11:34:52 -05002030 if (!ssl->s3->session_reused) {
2031 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2032 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2033 return 0;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002034 }
Steven Valdez2d850622017-01-11 11:34:52 -05002035
2036 ssl->early_data_accepted = 1;
2037 return 1;
2038}
2039
2040static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2041 uint8_t *out_alert, CBS *contents) {
2042 SSL *const ssl = hs->ssl;
2043 if (contents == NULL ||
2044 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2045 return 1;
2046 }
2047
2048 if (CBS_len(contents) != 0) {
2049 *out_alert = SSL_AD_DECODE_ERROR;
2050 return 0;
2051 }
2052
David Benjaminfd45ee72017-08-31 14:49:09 -04002053 hs->early_data_offered = true;
Steven Valdez2d850622017-01-11 11:34:52 -05002054 return 1;
2055}
2056
2057static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2058 if (!hs->ssl->early_data_accepted) {
2059 return 1;
2060 }
2061
2062 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2063 !CBB_add_u16(out, 0) ||
2064 !CBB_flush(out)) {
2065 return 0;
2066 }
2067
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002068 return 1;
2069}
2070
2071
David Benjaminc11ea9422017-08-29 16:33:21 -04002072// Key Share
2073//
2074// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5
Steven Valdez143e8b32016-07-11 13:19:03 -04002075
David Benjamin8c880a22016-12-03 02:20:34 -05002076static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2077 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002078 if (hs->max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002079 return 1;
2080 }
2081
2082 CBB contents, kse_bytes;
2083 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2084 !CBB_add_u16_length_prefixed(out, &contents) ||
2085 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2086 return 0;
2087 }
2088
David Benjamin8c880a22016-12-03 02:20:34 -05002089 uint16_t group_id = hs->retry_group;
2090 if (hs->received_hello_retry_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002091 // We received a HelloRetryRequest without a new curve, so there is no new
2092 // share to append. Leave |hs->key_share| as-is.
Steven Valdeza833c352016-11-01 13:39:36 -04002093 if (group_id == 0 &&
David Benjamin08f5c762017-09-21 02:43:05 -04002094 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(),
2095 hs->key_share_bytes.size())) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002096 return 0;
2097 }
David Benjamin08f5c762017-09-21 02:43:05 -04002098 hs->key_share_bytes.Reset();
David Benjamin3baa6e12016-10-07 21:10:38 -04002099 if (group_id == 0) {
2100 return CBB_flush(out);
2101 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002102 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04002103 // Add a fake group. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002104 if (ssl->ctx->grease_enabled &&
2105 (!CBB_add_u16(&kse_bytes,
2106 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2107 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2108 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2109 return 0;
2110 }
2111
David Benjaminc11ea9422017-08-29 16:33:21 -04002112 // Predict the most preferred group.
David Benjamincf0ce672017-09-21 02:25:59 -04002113 Span<const uint16_t> groups = tls1_get_grouplist(ssl);
David Benjaminb9493552017-09-27 19:02:51 -04002114 if (groups.empty()) {
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002115 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2116 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002117 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002118
2119 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002120 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002121
David Benjaminc642aca2017-07-19 23:28:43 -04002122 hs->key_share = SSLKeyShare::Create(group_id);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002123 CBB key_exchange;
David Benjaminc642aca2017-07-19 23:28:43 -04002124 if (!hs->key_share ||
2125 !CBB_add_u16(&kse_bytes, group_id) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002126 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjaminc642aca2017-07-19 23:28:43 -04002127 !hs->key_share->Offer(&key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002128 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002129 return 0;
2130 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002131
David Benjamin08f5c762017-09-21 02:43:05 -04002132 // Save the contents of the extension to repeat it in the second ClientHello.
2133 if (!hs->received_hello_retry_request &&
2134 !hs->key_share_bytes.CopyFrom(
2135 MakeConstSpan(CBB_data(&kse_bytes), CBB_len(&kse_bytes)))) {
2136 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002137 }
2138
Steven Valdez143e8b32016-07-11 13:19:03 -04002139 return CBB_flush(out);
2140}
2141
David Benjamin499742c2017-07-22 12:45:38 -04002142int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
2143 Array<uint8_t> *out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002144 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002145 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002146 uint16_t group_id;
2147 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002148 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2149 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002150 *out_alert = SSL_AD_DECODE_ERROR;
2151 return 0;
2152 }
2153
David Benjaminc642aca2017-07-19 23:28:43 -04002154 if (hs->key_share->GroupID() != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002155 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2156 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2157 return 0;
2158 }
2159
David Benjamin6b3ab722017-09-21 02:37:58 -04002160 if (!hs->key_share->Finish(out_secret, out_alert, peer_key)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002161 *out_alert = SSL_AD_INTERNAL_ERROR;
2162 return 0;
2163 }
2164
David Benjamin45738dd2017-02-09 20:01:26 -05002165 hs->new_session->group_id = group_id;
David Benjaminc642aca2017-07-19 23:28:43 -04002166 hs->key_share.reset();
Steven Valdez143e8b32016-07-11 13:19:03 -04002167 return 1;
2168}
2169
David Benjamin74795b32017-08-31 15:13:12 -04002170int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
David Benjamin499742c2017-07-22 12:45:38 -04002171 Array<uint8_t> *out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002172 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002173 uint16_t group_id;
2174 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002175 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002176 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2177 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2178 return 0;
2179 }
2180
2181 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002182 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002183 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002184 return 0;
2185 }
2186
David Benjaminc11ea9422017-08-29 16:33:21 -04002187 // Find the corresponding key share.
David Benjamin74795b32017-08-31 15:13:12 -04002188 bool found = false;
David Benjamin7e1f9842016-09-20 19:24:40 -04002189 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002190 while (CBS_len(&key_shares) > 0) {
2191 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002192 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002193 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002194 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2195 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002196 return 0;
2197 }
2198
David Benjamin7e1f9842016-09-20 19:24:40 -04002199 if (id == group_id) {
2200 if (found) {
2201 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2202 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2203 return 0;
2204 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002205
David Benjamin74795b32017-08-31 15:13:12 -04002206 found = true;
David Benjamin7e1f9842016-09-20 19:24:40 -04002207 peer_key = peer_key_tmp;
David Benjaminc11ea9422017-08-29 16:33:21 -04002208 // Continue parsing the structure to keep peers honest.
Steven Valdez143e8b32016-07-11 13:19:03 -04002209 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002210 }
2211
David Benjamin7e1f9842016-09-20 19:24:40 -04002212 if (!found) {
David Benjamin74795b32017-08-31 15:13:12 -04002213 *out_found = false;
David Benjamin499742c2017-07-22 12:45:38 -04002214 out_secret->Reset();
David Benjamin7e1f9842016-09-20 19:24:40 -04002215 return 1;
2216 }
2217
David Benjaminc11ea9422017-08-29 16:33:21 -04002218 // Compute the DH secret.
David Benjamin499742c2017-07-22 12:45:38 -04002219 Array<uint8_t> secret;
David Benjamin1386aad2017-07-19 23:57:40 -04002220 ScopedCBB public_key;
David Benjaminc642aca2017-07-19 23:28:43 -04002221 UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
David Benjamin6b3ab722017-09-21 02:37:58 -04002222 if (!key_share ||
2223 !CBB_init(public_key.get(), 32) ||
2224 !key_share->Accept(public_key.get(), &secret, out_alert, peer_key) ||
David Benjamin879efc32017-09-21 11:20:53 -04002225 !CBBFinishArray(public_key.get(), &hs->ecdh_public_key)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002226 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002227 return 0;
2228 }
2229
David Benjamin499742c2017-07-22 12:45:38 -04002230 *out_secret = std::move(secret);
David Benjamin74795b32017-08-31 15:13:12 -04002231 *out_found = true;
Steven Valdez5440fe02016-07-18 12:40:30 -04002232 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002233}
2234
David Benjamin8baf9632016-11-17 17:11:16 +09002235int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002236 uint16_t group_id;
2237 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002238 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002239 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2240 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2241 !CBB_add_u16(&kse_bytes, group_id) ||
2242 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjamin879efc32017-09-21 11:20:53 -04002243 !CBB_add_bytes(&public_key, hs->ecdh_public_key.data(),
2244 hs->ecdh_public_key.size()) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002245 !CBB_flush(out)) {
2246 return 0;
2247 }
2248
David Benjamin879efc32017-09-21 11:20:53 -04002249 hs->ecdh_public_key.Reset();
David Benjamin4fe3c902016-08-16 02:17:03 -04002250
David Benjamin45738dd2017-02-09 20:01:26 -05002251 hs->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002252 return 1;
2253}
2254
2255
David Benjaminc11ea9422017-08-29 16:33:21 -04002256// Supported Versions
2257//
2258// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1
Steven Valdezfdd10992016-09-15 16:27:05 -04002259
David Benjamin8c880a22016-12-03 02:20:34 -05002260static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2261 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002262 if (hs->max_version <= TLS1_2_VERSION) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002263 return 1;
2264 }
2265
2266 CBB contents, versions;
2267 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2268 !CBB_add_u16_length_prefixed(out, &contents) ||
2269 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2270 return 0;
2271 }
2272
David Benjaminc11ea9422017-08-29 16:33:21 -04002273 // Add a fake version. See draft-davidben-tls-grease-01.
David Benjamind9791bf2016-09-27 16:39:52 -04002274 if (ssl->ctx->grease_enabled &&
2275 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2276 return 0;
2277 }
2278
Steven Valdez8f36c512017-06-20 10:55:02 -04002279 if (!ssl_add_supported_versions(hs, &versions) ||
2280 !CBB_flush(out)) {
Steven Valdezfdd10992016-09-15 16:27:05 -04002281 return 0;
2282 }
2283
2284 return 1;
2285}
2286
2287
David Benjaminc11ea9422017-08-29 16:33:21 -04002288// Cookie
2289//
2290// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2
David Benjamin3baa6e12016-10-07 21:10:38 -04002291
David Benjamin8c880a22016-12-03 02:20:34 -05002292static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminb9493552017-09-27 19:02:51 -04002293 if (hs->cookie.empty()) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002294 return 1;
2295 }
2296
2297 CBB contents, cookie;
2298 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2299 !CBB_add_u16_length_prefixed(out, &contents) ||
2300 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin08f5c762017-09-21 02:43:05 -04002301 !CBB_add_bytes(&cookie, hs->cookie.data(), hs->cookie.size()) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002302 !CBB_flush(out)) {
2303 return 0;
2304 }
2305
David Benjaminc11ea9422017-08-29 16:33:21 -04002306 // The cookie is no longer needed in memory.
David Benjamin08f5c762017-09-21 02:43:05 -04002307 hs->cookie.Reset();
David Benjamin3baa6e12016-10-07 21:10:38 -04002308 return 1;
2309}
2310
2311
David Benjaminc11ea9422017-08-29 16:33:21 -04002312// Negotiated Groups
2313//
2314// https://tools.ietf.org/html/rfc4492#section-5.1.2
2315// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4
Adam Langley273d49c2015-07-20 16:38:52 -07002316
David Benjamin8c880a22016-12-03 02:20:34 -05002317static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2318 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002319 CBB contents, groups_bytes;
2320 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002321 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002322 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002323 return 0;
2324 }
2325
David Benjaminc11ea9422017-08-29 16:33:21 -04002326 // Add a fake group. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002327 if (ssl->ctx->grease_enabled &&
2328 !CBB_add_u16(&groups_bytes,
2329 ssl_get_grease_value(ssl, ssl_grease_group))) {
2330 return 0;
2331 }
2332
David Benjamincf0ce672017-09-21 02:25:59 -04002333 for (uint16_t group : tls1_get_grouplist(ssl)) {
2334 if (!CBB_add_u16(&groups_bytes, group)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002335 return 0;
2336 }
2337 }
2338
2339 return CBB_flush(out);
2340}
2341
David Benjamin8c880a22016-12-03 02:20:34 -05002342static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2343 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002344 CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002345 // This extension is not expected to be echoed by servers in TLS 1.2, but some
2346 // BigIP servers send it nonetheless, so do not enforce this.
Adam Langley273d49c2015-07-20 16:38:52 -07002347 return 1;
2348}
2349
David Benjaminb1cf48e2017-09-21 11:37:46 -04002350static bool parse_u16_array(const CBS *cbs, Array<uint16_t> *out) {
2351 CBS copy = *cbs;
2352 if ((CBS_len(&copy) & 1) != 0) {
2353 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2354 return false;
2355 }
2356
2357 Array<uint16_t> ret;
2358 if (!ret.Init(CBS_len(&copy) / 2)) {
2359 return false;
2360 }
2361 for (size_t i = 0; i < ret.size(); i++) {
2362 if (!CBS_get_u16(&copy, &ret[i])) {
2363 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2364 return false;
2365 }
2366 }
2367
2368 assert(CBS_len(&copy) == 0);
2369 *out = std::move(ret);
2370 return 1;
2371}
2372
David Benjamin8c880a22016-12-03 02:20:34 -05002373static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2374 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002375 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002376 if (contents == NULL) {
2377 return 1;
2378 }
2379
Steven Valdezce902a92016-05-17 11:47:53 -04002380 CBS supported_group_list;
2381 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2382 CBS_len(&supported_group_list) == 0 ||
David Benjaminb1cf48e2017-09-21 11:37:46 -04002383 CBS_len(contents) != 0 ||
2384 !parse_u16_array(&supported_group_list, &hs->peer_supported_group_list)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002385 return 0;
2386 }
2387
Adam Langley273d49c2015-07-20 16:38:52 -07002388 return 1;
Adam Langley273d49c2015-07-20 16:38:52 -07002389}
2390
David Benjamin8c880a22016-12-03 02:20:34 -05002391static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002392 // Servers don't echo this extension.
Adam Langley273d49c2015-07-20 16:38:52 -07002393 return 1;
2394}
2395
2396
David Benjaminc11ea9422017-08-29 16:33:21 -04002397// kExtensions contains all the supported extensions.
Adam Langley614c66a2015-06-12 15:26:58 -07002398static const struct tls_extension kExtensions[] = {
2399 {
Adam Langley5021b222015-06-12 18:27:58 -07002400 TLSEXT_TYPE_renegotiate,
2401 NULL,
2402 ext_ri_add_clienthello,
2403 ext_ri_parse_serverhello,
2404 ext_ri_parse_clienthello,
2405 ext_ri_add_serverhello,
2406 },
2407 {
Adam Langley614c66a2015-06-12 15:26:58 -07002408 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002409 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002410 ext_sni_add_clienthello,
2411 ext_sni_parse_serverhello,
2412 ext_sni_parse_clienthello,
2413 ext_sni_add_serverhello,
2414 },
Adam Langley0a056712015-07-01 15:03:33 -07002415 {
2416 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002417 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002418 ext_ems_add_clienthello,
2419 ext_ems_parse_serverhello,
2420 ext_ems_parse_clienthello,
2421 ext_ems_add_serverhello,
2422 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002423 {
2424 TLSEXT_TYPE_session_ticket,
2425 NULL,
2426 ext_ticket_add_clienthello,
2427 ext_ticket_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04002428 // Ticket extension client parsing is handled in ssl_session.c
Steven Valdez6b8509a2016-07-12 13:38:32 -04002429 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002430 ext_ticket_add_serverhello,
2431 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002432 {
2433 TLSEXT_TYPE_signature_algorithms,
2434 NULL,
2435 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002436 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002437 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002438 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002439 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002440 {
2441 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002442 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002443 ext_ocsp_add_clienthello,
2444 ext_ocsp_parse_serverhello,
2445 ext_ocsp_parse_clienthello,
2446 ext_ocsp_add_serverhello,
2447 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002448 {
2449 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002450 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002451 ext_npn_add_clienthello,
2452 ext_npn_parse_serverhello,
2453 ext_npn_parse_clienthello,
2454 ext_npn_add_serverhello,
2455 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002456 {
2457 TLSEXT_TYPE_certificate_timestamp,
2458 NULL,
2459 ext_sct_add_clienthello,
2460 ext_sct_parse_serverhello,
2461 ext_sct_parse_clienthello,
2462 ext_sct_add_serverhello,
2463 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002464 {
2465 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002466 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002467 ext_alpn_add_clienthello,
2468 ext_alpn_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04002469 // ALPN is negotiated late in |ssl_negotiate_alpn|.
David Benjamin9ef31f02016-10-31 18:01:13 -04002470 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002471 ext_alpn_add_serverhello,
2472 },
Adam Langley49c7af12015-07-10 14:33:46 -07002473 {
2474 TLSEXT_TYPE_channel_id,
2475 ext_channel_id_init,
2476 ext_channel_id_add_clienthello,
2477 ext_channel_id_parse_serverhello,
2478 ext_channel_id_parse_clienthello,
2479 ext_channel_id_add_serverhello,
2480 },
Adam Langley391250d2015-07-15 19:06:07 -07002481 {
2482 TLSEXT_TYPE_srtp,
2483 ext_srtp_init,
2484 ext_srtp_add_clienthello,
2485 ext_srtp_parse_serverhello,
2486 ext_srtp_parse_clienthello,
2487 ext_srtp_add_serverhello,
2488 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002489 {
2490 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002491 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002492 ext_ec_point_add_clienthello,
2493 ext_ec_point_parse_serverhello,
2494 ext_ec_point_parse_clienthello,
2495 ext_ec_point_add_serverhello,
2496 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002497 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002498 TLSEXT_TYPE_key_share,
2499 NULL,
2500 ext_key_share_add_clienthello,
2501 forbid_parse_serverhello,
2502 ignore_parse_clienthello,
2503 dont_add_serverhello,
2504 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002505 {
Steven Valdeza833c352016-11-01 13:39:36 -04002506 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002507 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002508 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002509 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002510 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002511 dont_add_serverhello,
2512 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002513 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002514 TLSEXT_TYPE_early_data,
2515 NULL,
2516 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002517 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002518 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002519 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002520 },
2521 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002522 TLSEXT_TYPE_supported_versions,
2523 NULL,
2524 ext_supported_versions_add_clienthello,
2525 forbid_parse_serverhello,
2526 ignore_parse_clienthello,
2527 dont_add_serverhello,
2528 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002529 {
2530 TLSEXT_TYPE_cookie,
2531 NULL,
2532 ext_cookie_add_clienthello,
2533 forbid_parse_serverhello,
2534 ignore_parse_clienthello,
2535 dont_add_serverhello,
2536 },
David Benjaminc11ea9422017-08-29 16:33:21 -04002537 // The final extension must be non-empty. WebSphere Application Server 7.0 is
2538 // intolerant to the last extension being zero-length. See
2539 // https://crbug.com/363583.
Adam Langley273d49c2015-07-20 16:38:52 -07002540 {
Steven Valdezce902a92016-05-17 11:47:53 -04002541 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002542 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002543 ext_supported_groups_add_clienthello,
2544 ext_supported_groups_parse_serverhello,
2545 ext_supported_groups_parse_clienthello,
2546 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002547 },
Adam Langley614c66a2015-06-12 15:26:58 -07002548};
2549
2550#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2551
David Benjamina3d76d02017-07-14 19:36:07 -04002552static_assert(kNumExtensions <=
2553 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
2554 "too many extensions for sent bitset");
2555static_assert(kNumExtensions <=
2556 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2557 "too many extensions for received bitset");
Adam Langley4cfa96b2015-07-01 11:56:55 -07002558
Adam Langley614c66a2015-06-12 15:26:58 -07002559static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2560 uint16_t value) {
2561 unsigned i;
2562 for (i = 0; i < kNumExtensions; i++) {
2563 if (kExtensions[i].value == value) {
2564 *out_index = i;
2565 return &kExtensions[i];
2566 }
2567 }
2568
2569 return NULL;
2570}
2571
David Benjamin8c880a22016-12-03 02:20:34 -05002572int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2573 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04002574 // Don't add extensions for SSLv3 unless doing secure renegotiation.
David Benjaminf04c2e92016-12-06 13:35:25 -05002575 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002576 !ssl->s3->send_connection_binding) {
2577 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002578 }
Adam Langley95c29f32014-06-20 12:00:00 -07002579
David Benjamine8d53502015-10-10 14:13:23 -04002580 CBB extensions;
2581 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002582 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2583 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002584 }
Adam Langley95c29f32014-06-20 12:00:00 -07002585
David Benjamin8c880a22016-12-03 02:20:34 -05002586 hs->extensions.sent = 0;
2587 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002588
David Benjamin54091232016-09-05 12:47:25 -04002589 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002590 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002591 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002592 }
2593 }
Adam Langley95c29f32014-06-20 12:00:00 -07002594
David Benjamin65ac9972016-09-02 21:35:25 -04002595 uint16_t grease_ext1 = 0;
2596 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002597 // Add a fake empty extension. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002598 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2599 if (!CBB_add_u16(&extensions, grease_ext1) ||
2600 !CBB_add_u16(&extensions, 0 /* zero length */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002601 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2602 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002603 }
2604 }
2605
David Benjamin54091232016-09-05 12:47:25 -04002606 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002607 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002608 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002609 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002610 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin81678aa2017-07-12 22:43:42 -04002611 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002612 }
Adam Langley95c29f32014-06-20 12:00:00 -07002613
Adam Langley33ad2b52015-07-20 17:43:53 -07002614 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002615 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002616 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002617 }
Adam Langley75712922014-10-10 16:23:43 -07002618
David Benjamin2bd19172016-11-17 16:47:15 +09002619 if (!custom_ext_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002620 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2621 return 0;
Adam Langley09505632015-07-30 18:10:13 -07002622 }
2623
David Benjamin65ac9972016-09-02 21:35:25 -04002624 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002625 // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002626 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2627
David Benjaminc11ea9422017-08-29 16:33:21 -04002628 // The two fake extensions must not have the same value. GREASE values are
2629 // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2630 // one.
David Benjamin65ac9972016-09-02 21:35:25 -04002631 if (grease_ext1 == grease_ext2) {
2632 grease_ext2 ^= 0x1010;
2633 }
2634
2635 if (!CBB_add_u16(&extensions, grease_ext2) ||
2636 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2637 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002638 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2639 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002640 }
2641 }
2642
David Benjamince079fd2016-08-02 16:22:34 -04002643 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002644 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002645 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002646 if (header_len > 0xff && header_len < 0x200) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002647 // Add padding to workaround bugs in F5 terminators. See RFC 7685.
2648 //
2649 // NB: because this code works out the length of all existing extensions
2650 // it MUST always appear last.
David Benjamin0a968592015-07-21 22:06:19 -04002651 size_t padding_len = 0x200 - header_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04002652 // Extensions take at least four bytes to encode. Always include at least
2653 // one byte of data if including the extension. WebSphere Application
2654 // Server 7.0 is intolerant to the last extension being zero-length. See
2655 // https://crbug.com/363583.
Adam Langleyfcf25832014-12-18 17:42:32 -08002656 if (padding_len >= 4 + 1) {
2657 padding_len -= 4;
2658 } else {
2659 padding_len = 1;
2660 }
Adam Langley95c29f32014-06-20 12:00:00 -07002661
Adam Langley33ad2b52015-07-20 17:43:53 -07002662 uint8_t *padding_bytes;
2663 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2664 !CBB_add_u16(&extensions, padding_len) ||
2665 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002666 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2667 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002668 }
Adam Langley75712922014-10-10 16:23:43 -07002669
David Benjamin17cf2cb2016-12-13 01:07:13 -05002670 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002671 }
2672 }
Adam Langley75712922014-10-10 16:23:43 -07002673
David Benjaminc11ea9422017-08-29 16:33:21 -04002674 // The PSK extension must be last, including after the padding.
David Benjamin8c880a22016-12-03 02:20:34 -05002675 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002676 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2677 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -04002678 }
2679
David Benjaminc11ea9422017-08-29 16:33:21 -04002680 // Discard empty extensions blocks.
David Benjamina01deee2015-12-08 18:56:31 -05002681 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002682 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002683 }
2684
David Benjamine8d53502015-10-10 14:13:23 -04002685 return CBB_flush(out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002686}
Adam Langley95c29f32014-06-20 12:00:00 -07002687
David Benjamin8c880a22016-12-03 02:20:34 -05002688int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2689 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002690 CBB extensions;
2691 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002692 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002693 }
2694
David Benjamin8c880a22016-12-03 02:20:34 -05002695 for (unsigned i = 0; i < kNumExtensions; i++) {
2696 if (!(hs->extensions.received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002697 // Don't send extensions that were not received.
Adam Langley614c66a2015-06-12 15:26:58 -07002698 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002699 }
Adam Langley95c29f32014-06-20 12:00:00 -07002700
David Benjamin8c880a22016-12-03 02:20:34 -05002701 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002702 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002703 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002704 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002705 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002706 }
Adam Langley95c29f32014-06-20 12:00:00 -07002707
David Benjamin2bd19172016-11-17 16:47:15 +09002708 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002709 goto err;
2710 }
2711
David Benjaminc11ea9422017-08-29 16:33:21 -04002712 // Discard empty extensions blocks before TLS 1.3.
Steven Valdez143e8b32016-07-11 13:19:03 -04002713 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2714 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002715 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002716 }
2717
David Benjamin56380462015-10-10 14:59:09 -04002718 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002719
2720err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002721 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002722 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002723}
Adam Langley95c29f32014-06-20 12:00:00 -07002724
David Benjamin731058e2016-12-03 23:15:13 -05002725static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2726 const SSL_CLIENT_HELLO *client_hello,
2727 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002728 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002729 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002730 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002731 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002732 }
2733 }
2734
David Benjamin8c880a22016-12-03 02:20:34 -05002735 hs->extensions.received = 0;
2736 hs->custom_extensions.received = 0;
David Benjamine14ff062016-08-09 16:21:24 -04002737 CBS extensions;
2738 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2739 while (CBS_len(&extensions) != 0) {
2740 uint16_t type;
2741 CBS extension;
2742
David Benjaminc11ea9422017-08-29 16:33:21 -04002743 // Decode the next extension.
David Benjamine14ff062016-08-09 16:21:24 -04002744 if (!CBS_get_u16(&extensions, &type) ||
2745 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002746 *out_alert = SSL_AD_DECODE_ERROR;
2747 return 0;
2748 }
Adam Langley95c29f32014-06-20 12:00:00 -07002749
David Benjaminc11ea9422017-08-29 16:33:21 -04002750 // RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2751 // ambiguous. Ignore all but the renegotiation_info extension.
David Benjamine14ff062016-08-09 16:21:24 -04002752 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2753 continue;
2754 }
Adam Langley95c29f32014-06-20 12:00:00 -07002755
David Benjamine14ff062016-08-09 16:21:24 -04002756 unsigned ext_index;
2757 const struct tls_extension *const ext =
2758 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002759
David Benjamine14ff062016-08-09 16:21:24 -04002760 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002761 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002762 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002763 return 0;
2764 }
David Benjamine14ff062016-08-09 16:21:24 -04002765 continue;
2766 }
2767
David Benjamin8c880a22016-12-03 02:20:34 -05002768 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002769 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002770 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002771 *out_alert = alert;
2772 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002773 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002774 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002775 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002776 }
Adam Langley75712922014-10-10 16:23:43 -07002777
David Benjamin1deb41b2016-08-09 19:36:38 -04002778 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002779 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002780 continue;
2781 }
2782
2783 CBS *contents = NULL, fake_contents;
2784 static const uint8_t kFakeRenegotiateExtension[] = {0};
2785 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2786 ssl_client_cipher_list_contains_cipher(client_hello,
2787 SSL3_CK_SCSV & 0xffff)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002788 // The renegotiation SCSV was received so pretend that we received a
2789 // renegotiation extension.
David Benjamin1deb41b2016-08-09 19:36:38 -04002790 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2791 sizeof(kFakeRenegotiateExtension));
2792 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002793 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002794 }
2795
David Benjaminc11ea9422017-08-29 16:33:21 -04002796 // Extension wasn't observed so call the callback with a NULL
2797 // parameter.
David Benjamin1deb41b2016-08-09 19:36:38 -04002798 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002799 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002800 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002801 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002802 *out_alert = alert;
2803 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002804 }
2805 }
2806
Adam Langleyfcf25832014-12-18 17:42:32 -08002807 return 1;
2808}
Adam Langley95c29f32014-06-20 12:00:00 -07002809
David Benjamin731058e2016-12-03 23:15:13 -05002810int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2811 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002812 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002813 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002814 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002815 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002816 return 0;
2817 }
Adam Langley95c29f32014-06-20 12:00:00 -07002818
David Benjamin8c880a22016-12-03 02:20:34 -05002819 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002820 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002821 return 0;
2822 }
Adam Langley95c29f32014-06-20 12:00:00 -07002823
Adam Langleyfcf25832014-12-18 17:42:32 -08002824 return 1;
2825}
Adam Langley95c29f32014-06-20 12:00:00 -07002826
David Benjamin8c880a22016-12-03 02:20:34 -05002827static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2828 int *out_alert) {
2829 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04002830 // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty.
Steven Valdez143e8b32016-07-11 13:19:03 -04002831 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2832 return 1;
2833 }
Adam Langley614c66a2015-06-12 15:26:58 -07002834
David Benjaminc11ea9422017-08-29 16:33:21 -04002835 // Decode the extensions block and check it is valid.
Steven Valdez143e8b32016-07-11 13:19:03 -04002836 CBS extensions;
2837 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2838 !tls1_check_duplicate_extensions(&extensions)) {
2839 *out_alert = SSL_AD_DECODE_ERROR;
2840 return 0;
2841 }
2842
2843 uint32_t received = 0;
2844 while (CBS_len(&extensions) != 0) {
2845 uint16_t type;
2846 CBS extension;
2847
David Benjaminc11ea9422017-08-29 16:33:21 -04002848 // Decode the next extension.
Steven Valdez143e8b32016-07-11 13:19:03 -04002849 if (!CBS_get_u16(&extensions, &type) ||
2850 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002851 *out_alert = SSL_AD_DECODE_ERROR;
2852 return 0;
2853 }
Adam Langley95c29f32014-06-20 12:00:00 -07002854
Steven Valdez143e8b32016-07-11 13:19:03 -04002855 unsigned ext_index;
2856 const struct tls_extension *const ext =
2857 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002858
Steven Valdez143e8b32016-07-11 13:19:03 -04002859 if (ext == NULL) {
David Benjaminfd45ee72017-08-31 14:49:09 -04002860 hs->received_custom_extension = true;
David Benjamin2bd19172016-11-17 16:47:15 +09002861 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002862 return 0;
2863 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002864 continue;
2865 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002866
David Benjamina3d76d02017-07-14 19:36:07 -04002867 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
2868 "too many bits");
David Benjamin5db7c9b2017-01-24 16:17:03 -05002869
David Benjamin8c880a22016-12-03 02:20:34 -05002870 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002871 type != TLSEXT_TYPE_renegotiate) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002872 // If the extension was never sent then it is illegal, except for the
2873 // renegotiation extension which, in SSL 3.0, is signaled via SCSV.
Steven Valdez143e8b32016-07-11 13:19:03 -04002874 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2875 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002876 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002877 return 0;
2878 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002879
Steven Valdez143e8b32016-07-11 13:19:03 -04002880 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002881
Steven Valdez143e8b32016-07-11 13:19:03 -04002882 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002883 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002884 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002885 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04002886 *out_alert = alert;
2887 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002888 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002889 }
Adam Langley95c29f32014-06-20 12:00:00 -07002890
David Benjamin54091232016-09-05 12:47:25 -04002891 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002892 if (!(received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002893 // Extension wasn't observed so call the callback with a NULL
2894 // parameter.
Adam Langley614c66a2015-06-12 15:26:58 -07002895 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002896 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002897 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002898 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002899 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002900 return 0;
2901 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002902 }
2903 }
Adam Langley95c29f32014-06-20 12:00:00 -07002904
Adam Langleyfcf25832014-12-18 17:42:32 -08002905 return 1;
2906}
Adam Langley95c29f32014-06-20 12:00:00 -07002907
David Benjamin8c880a22016-12-03 02:20:34 -05002908static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
2909 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002910 int ret = SSL_TLSEXT_ERR_NOACK;
2911 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002912
David Benjamin78f8aab2016-03-10 16:33:58 -05002913 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002914 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002915 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05002916 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
2917 ret = ssl->session_ctx->tlsext_servername_callback(
2918 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002919 }
Adam Langley95c29f32014-06-20 12:00:00 -07002920
Adam Langleyfcf25832014-12-18 17:42:32 -08002921 switch (ret) {
2922 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002923 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002924 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002925
Adam Langleyfcf25832014-12-18 17:42:32 -08002926 case SSL_TLSEXT_ERR_NOACK:
David Benjaminfd45ee72017-08-31 14:49:09 -04002927 hs->should_ack_sni = false;
Adam Langleyfcf25832014-12-18 17:42:32 -08002928 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002929
Adam Langleyfcf25832014-12-18 17:42:32 -08002930 default:
2931 return 1;
2932 }
2933}
Adam Langleyed8270a2014-09-02 13:52:56 -07002934
David Benjamin8c880a22016-12-03 02:20:34 -05002935int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
2936 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002937 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002938 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002939 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002940 return 0;
2941 }
2942
Adam Langleyfcf25832014-12-18 17:42:32 -08002943 return 1;
2944}
Adam Langley95c29f32014-06-20 12:00:00 -07002945
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002946static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
2947 uint8_t **out, size_t *out_len, EVP_CIPHER_CTX *cipher_ctx,
2948 HMAC_CTX *hmac_ctx, const uint8_t *ticket, size_t ticket_len) {
2949 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002950
David Benjaminc11ea9422017-08-29 16:33:21 -04002951 // Check the MAC at the end of the ticket.
David Benjamine3aa1d92015-06-16 15:34:50 -04002952 uint8_t mac[EVP_MAX_MD_SIZE];
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002953 size_t mac_len = HMAC_size(hmac_ctx);
David Benjamine3aa1d92015-06-16 15:34:50 -04002954 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002955 // The ticket must be large enough for key name, IV, data, and MAC.
David Benjamin81678aa2017-07-12 22:43:42 -04002956 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08002957 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002958 HMAC_Update(hmac_ctx, ticket, ticket_len - mac_len);
2959 HMAC_Final(hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04002960 int mac_ok =
2961 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
2962#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
2963 mac_ok = 1;
2964#endif
2965 if (!mac_ok) {
David Benjamin81678aa2017-07-12 22:43:42 -04002966 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08002967 }
2968
David Benjaminc11ea9422017-08-29 16:33:21 -04002969 // Decrypt the session data.
David Benjamine3aa1d92015-06-16 15:34:50 -04002970 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2971 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2972 mac_len;
David Benjamin86e95b82017-07-18 16:34:25 -04002973 UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len));
David Benjamin81678aa2017-07-12 22:43:42 -04002974 if (!plaintext) {
2975 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08002976 }
David Benjaminfbc45d72016-09-22 01:21:24 -04002977 size_t plaintext_len;
2978#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin81678aa2017-07-12 22:43:42 -04002979 OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04002980 plaintext_len = ciphertext_len;
2981#else
David Benjamine3aa1d92015-06-16 15:34:50 -04002982 if (ciphertext_len >= INT_MAX) {
David Benjamin81678aa2017-07-12 22:43:42 -04002983 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08002984 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002985 int len1, len2;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002986 if (!EVP_DecryptUpdate(cipher_ctx, plaintext.get(), &len1, ciphertext,
David Benjamine3aa1d92015-06-16 15:34:50 -04002987 (int)ciphertext_len) ||
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002988 !EVP_DecryptFinal_ex(cipher_ctx, plaintext.get() + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08002989 ERR_clear_error();
David Benjamin81678aa2017-07-12 22:43:42 -04002990 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08002991 }
Adam Langley4c341d02017-03-08 19:33:21 -08002992 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04002993#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08002994
David Benjamin81678aa2017-07-12 22:43:42 -04002995 *out = plaintext.release();
Adam Langley4c341d02017-03-08 19:33:21 -08002996 *out_len = plaintext_len;
David Benjamin81678aa2017-07-12 22:43:42 -04002997 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08002998}
2999
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003000static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
David Benjaminfd45ee72017-08-31 14:49:09 -04003001 SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003002 const uint8_t *ticket, size_t ticket_len) {
3003 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3004 ScopedEVP_CIPHER_CTX cipher_ctx;
3005 ScopedHMAC_CTX hmac_ctx;
3006 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3007 int cb_ret = ssl->session_ctx->tlsext_ticket_key_cb(
3008 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(),
3009 hmac_ctx.get(), 0 /* decrypt */);
3010 if (cb_ret < 0) {
3011 return ssl_ticket_aead_error;
3012 } else if (cb_ret == 0) {
3013 return ssl_ticket_aead_ignore_ticket;
3014 } else if (cb_ret == 2) {
David Benjaminfd45ee72017-08-31 14:49:09 -04003015 *out_renew_ticket = true;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003016 } else {
3017 assert(cb_ret == 1);
3018 }
3019 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3020 hmac_ctx.get(), ticket, ticket_len);
3021}
3022
3023static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
3024 SSL *ssl, uint8_t **out, size_t *out_len, const uint8_t *ticket,
3025 size_t ticket_len) {
3026 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3027 SSL_CTX *ctx = ssl->session_ctx;
3028
David Benjaminc11ea9422017-08-29 16:33:21 -04003029 // Rotate the ticket key if necessary.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003030 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
3031 return ssl_ticket_aead_error;
3032 }
3033
David Benjaminc11ea9422017-08-29 16:33:21 -04003034 // Pick the matching ticket key and decrypt.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003035 ScopedEVP_CIPHER_CTX cipher_ctx;
3036 ScopedHMAC_CTX hmac_ctx;
3037 {
3038 MutexReadLock lock(&ctx->lock);
3039 const tlsext_ticket_key *key;
3040 if (ctx->tlsext_ticket_key_current &&
3041 !OPENSSL_memcmp(ctx->tlsext_ticket_key_current->name, ticket,
3042 SSL_TICKET_KEY_NAME_LEN)) {
3043 key = ctx->tlsext_ticket_key_current;
3044 } else if (ctx->tlsext_ticket_key_prev &&
3045 !OPENSSL_memcmp(ctx->tlsext_ticket_key_prev->name, ticket,
3046 SSL_TICKET_KEY_NAME_LEN)) {
3047 key = ctx->tlsext_ticket_key_prev;
3048 } else {
3049 return ssl_ticket_aead_ignore_ticket;
3050 }
3051 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3052 if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
3053 tlsext_tick_md(), NULL) ||
3054 !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL,
3055 key->aes_key, iv)) {
3056 return ssl_ticket_aead_error;
3057 }
3058 }
3059 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3060 hmac_ctx.get(), ticket, ticket_len);
3061}
3062
Adam Langley4c341d02017-03-08 19:33:21 -08003063static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
David Benjaminfd45ee72017-08-31 14:49:09 -04003064 SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
Adam Langley4c341d02017-03-08 19:33:21 -08003065 const uint8_t *ticket, size_t ticket_len) {
David Benjamin81678aa2017-07-12 22:43:42 -04003066 uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len);
Adam Langley4c341d02017-03-08 19:33:21 -08003067 if (plaintext == NULL) {
3068 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3069 return ssl_ticket_aead_error;
3070 }
3071
3072 size_t plaintext_len;
3073 const enum ssl_ticket_aead_result_t result =
3074 ssl->session_ctx->ticket_aead_method->open(
3075 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3076
3077 if (result == ssl_ticket_aead_success) {
3078 *out = plaintext;
3079 plaintext = NULL;
3080 *out_len = plaintext_len;
3081 }
3082
3083 OPENSSL_free(plaintext);
3084 return result;
3085}
3086
3087enum ssl_ticket_aead_result_t ssl_process_ticket(
David Benjaminfd45ee72017-08-31 14:49:09 -04003088 SSL *ssl, UniquePtr<SSL_SESSION> *out_session, bool *out_renew_ticket,
Adam Langley4c341d02017-03-08 19:33:21 -08003089 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3090 size_t session_id_len) {
David Benjaminfd45ee72017-08-31 14:49:09 -04003091 *out_renew_ticket = false;
David Benjamin37af90f2017-07-29 01:42:16 -04003092 out_session->reset();
Adam Langley4c341d02017-03-08 19:33:21 -08003093
3094 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3095 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3096 return ssl_ticket_aead_ignore_ticket;
3097 }
3098
3099 uint8_t *plaintext = NULL;
3100 size_t plaintext_len;
3101 enum ssl_ticket_aead_result_t result;
3102 if (ssl->session_ctx->ticket_aead_method != NULL) {
3103 result = ssl_decrypt_ticket_with_method(
3104 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3105 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04003106 // Ensure there is room for the key name and the largest IV
3107 // |tlsext_ticket_key_cb| may try to consume. The real limit may be lower,
3108 // but the maximum IV length should be well under the minimum size for the
3109 // session material and HMAC.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003110 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3111 return ssl_ticket_aead_ignore_ticket;
3112 }
3113 if (ssl->session_ctx->tlsext_ticket_key_cb != NULL) {
3114 result = ssl_decrypt_ticket_with_cb(ssl, &plaintext, &plaintext_len,
3115 out_renew_ticket, ticket, ticket_len);
3116 } else {
3117 result = ssl_decrypt_ticket_with_ticket_keys(
3118 ssl, &plaintext, &plaintext_len, ticket, ticket_len);
3119 }
Adam Langley4c341d02017-03-08 19:33:21 -08003120 }
3121
3122 if (result != ssl_ticket_aead_success) {
3123 return result;
3124 }
3125
David Benjaminc11ea9422017-08-29 16:33:21 -04003126 // Decode the session.
David Benjamin37af90f2017-07-29 01:42:16 -04003127 UniquePtr<SSL_SESSION> session(
3128 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx));
Adam Langley4c341d02017-03-08 19:33:21 -08003129 OPENSSL_free(plaintext);
3130
David Benjamin37af90f2017-07-29 01:42:16 -04003131 if (!session) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003132 ERR_clear_error(); // Don't leave an error on the queue.
Adam Langley4c341d02017-03-08 19:33:21 -08003133 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003134 }
3135
David Benjaminc11ea9422017-08-29 16:33:21 -04003136 // Copy the client's session ID into the new session, to denote the ticket has
3137 // been accepted.
David Benjamin17cf2cb2016-12-13 01:07:13 -05003138 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003139 session->session_id_length = session_id_len;
3140
David Benjamin37af90f2017-07-29 01:42:16 -04003141 *out_session = std::move(session);
Adam Langley4c341d02017-03-08 19:33:21 -08003142 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003143}
Adam Langley95c29f32014-06-20 12:00:00 -07003144
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003145int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003146 // Extension ignored for inappropriate versions
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003147 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003148 return 1;
3149 }
David Benjamincd996942014-07-20 16:23:51 -04003150
David Benjaminb1cf48e2017-09-21 11:37:46 -04003151 return parse_u16_array(in_sigalgs, &hs->peer_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08003152}
David Benjaminec2f27d2014-11-13 19:17:25 -05003153
David Benjamina3651382017-04-20 17:49:36 -04003154int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
3155 switch (EVP_PKEY_id(pkey)) {
3156 case EVP_PKEY_RSA:
3157 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3158 return 1;
3159 case EVP_PKEY_EC:
3160 *out = SSL_SIGN_ECDSA_SHA1;
3161 return 1;
3162 default:
3163 return 0;
3164 }
3165}
3166
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003167int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3168 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003169 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003170
David Benjaminc11ea9422017-08-29 16:33:21 -04003171 // Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3172 // handshake.
Steven Valdezf0451ca2016-06-29 13:16:27 -04003173 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin31b0c9b2017-07-20 14:49:15 -04003174 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
David Benjamina3651382017-04-20 17:49:36 -04003175 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3176 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003177 }
David Benjamina3651382017-04-20 17:49:36 -04003178 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003179 }
3180
David Benjaminb1cf48e2017-09-21 11:37:46 -04003181 Span<const uint16_t> sigalgs = kSignSignatureAlgorithms;
3182 if (cert->sigalgs != nullptr) {
3183 sigalgs = MakeConstSpan(cert->sigalgs, cert->num_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003184 }
3185
David Benjaminb1cf48e2017-09-21 11:37:46 -04003186 Span<const uint16_t> peer_sigalgs = hs->peer_sigalgs;
David Benjaminb9493552017-09-27 19:02:51 -04003187 if (peer_sigalgs.empty() && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003188 // If the client didn't specify any signature_algorithms extension then
3189 // we can assume that it supports SHA1. See
3190 // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
David Benjaminea9a0d52016-07-08 15:52:59 -07003191 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3192 SSL_SIGN_ECDSA_SHA1};
3193 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjaminea9a0d52016-07-08 15:52:59 -07003194 }
3195
David Benjaminb1cf48e2017-09-21 11:37:46 -04003196 for (uint16_t sigalg : sigalgs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003197 // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3198 // negotiated.
David Benjamin1fb125c2016-07-08 18:52:12 -07003199 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjaminb1cf48e2017-09-21 11:37:46 -04003200 !ssl_private_key_supports_signature_algorithm(hs, sigalg)) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003201 continue;
3202 }
3203
David Benjaminb1cf48e2017-09-21 11:37:46 -04003204 for (uint16_t peer_sigalg : peer_sigalgs) {
3205 if (sigalg == peer_sigalg) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003206 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003207 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003208 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003209 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003210 }
Adam Langley95c29f32014-06-20 12:00:00 -07003211
David Benjaminea9a0d52016-07-08 15:52:59 -07003212 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3213 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003214}
Adam Langley95c29f32014-06-20 12:00:00 -07003215
David Benjamin7934f082017-08-01 16:32:25 -04003216int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
Steven Valdez908ac192017-01-12 13:17:07 -05003217 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003218 // A Channel ID handshake message is structured to contain multiple
3219 // extensions, but the only one that can be present is Channel ID.
David Benjamin7934f082017-08-01 16:32:25 -04003220 uint16_t extension_type;
3221 CBS channel_id = msg.body, extension;
Nick Harper60a85cb2016-09-23 16:25:11 -07003222 if (!CBS_get_u16(&channel_id, &extension_type) ||
3223 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3224 CBS_len(&channel_id) != 0 ||
3225 extension_type != TLSEXT_TYPE_channel_id ||
3226 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3227 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3228 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3229 return 0;
3230 }
3231
David Benjamin86e95b82017-07-18 16:34:25 -04003232 UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Nick Harper60a85cb2016-09-23 16:25:11 -07003233 if (!p256) {
3234 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3235 return 0;
3236 }
3237
David Benjamin86e95b82017-07-18 16:34:25 -04003238 UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3239 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
David Benjamin81678aa2017-07-12 22:43:42 -04003240 if (!sig || !x || !y) {
3241 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003242 }
3243
3244 const uint8_t *p = CBS_data(&extension);
David Benjamin81678aa2017-07-12 22:43:42 -04003245 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3246 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3247 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3248 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
3249 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003250 }
3251
David Benjamin86e95b82017-07-18 16:34:25 -04003252 UniquePtr<EC_KEY> key(EC_KEY_new());
3253 UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
David Benjamin81678aa2017-07-12 22:43:42 -04003254 if (!key || !point ||
3255 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3256 y.get(), nullptr) ||
3257 !EC_KEY_set_group(key.get(), p256.get()) ||
3258 !EC_KEY_set_public_key(key.get(), point.get())) {
3259 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003260 }
3261
3262 uint8_t digest[EVP_MAX_MD_SIZE];
3263 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003264 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003265 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003266 }
3267
David Benjamin81678aa2017-07-12 22:43:42 -04003268 int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
Nick Harper60a85cb2016-09-23 16:25:11 -07003269#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3270 sig_ok = 1;
3271#endif
3272 if (!sig_ok) {
3273 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3274 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
David Benjamin046bc1f2017-08-31 15:06:42 -04003275 ssl->s3->tlsext_channel_id_valid = false;
David Benjamin81678aa2017-07-12 22:43:42 -04003276 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003277 }
3278
David Benjamin17cf2cb2016-12-13 01:07:13 -05003279 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
David Benjamin81678aa2017-07-12 22:43:42 -04003280 return 1;
Nick Harper60a85cb2016-09-23 16:25:11 -07003281}
3282
Steven Valdez908ac192017-01-12 13:17:07 -05003283int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3284 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003285 uint8_t digest[EVP_MAX_MD_SIZE];
3286 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003287 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003288 return 0;
3289 }
3290
3291 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3292 if (ec_key == NULL) {
3293 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3294 return 0;
3295 }
3296
3297 int ret = 0;
3298 BIGNUM *x = BN_new();
3299 BIGNUM *y = BN_new();
3300 ECDSA_SIG *sig = NULL;
3301 if (x == NULL || y == NULL ||
3302 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3303 EC_KEY_get0_public_key(ec_key),
3304 x, y, NULL)) {
3305 goto err;
3306 }
3307
3308 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3309 if (sig == NULL) {
3310 goto err;
3311 }
3312
3313 CBB child;
3314 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3315 !CBB_add_u16_length_prefixed(cbb, &child) ||
3316 !BN_bn2cbb_padded(&child, 32, x) ||
3317 !BN_bn2cbb_padded(&child, 32, y) ||
3318 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3319 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3320 !CBB_flush(cbb)) {
3321 goto err;
3322 }
3323
3324 ret = 1;
3325
3326err:
3327 BN_free(x);
3328 BN_free(y);
3329 ECDSA_SIG_free(sig);
3330 return ret;
3331}
3332
Steven Valdez908ac192017-01-12 13:17:07 -05003333int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3334 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003335 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3336 uint8_t *msg;
3337 size_t msg_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003338 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Nick Harper60a85cb2016-09-23 16:25:11 -07003339 ssl_cert_verify_channel_id)) {
3340 return 0;
3341 }
3342 SHA256(msg, msg_len, out);
3343 *out_len = SHA256_DIGEST_LENGTH;
3344 OPENSSL_free(msg);
3345 return 1;
3346 }
3347
Nick Harper95594012016-10-20 14:07:13 -07003348 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003349
Nick Harper95594012016-10-20 14:07:13 -07003350 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003351 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003352 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003353
Steven Valdez87eab492016-06-27 16:34:59 -04003354 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003355 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003356 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003357 if (ssl->session->original_handshake_hash_len == 0) {
3358 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003359 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003360 }
Nick Harper95594012016-10-20 14:07:13 -07003361 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3362 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003363 }
3364
Steven Valdez908ac192017-01-12 13:17:07 -05003365 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3366 size_t hs_hash_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003367 if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003368 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003369 }
Steven Valdez908ac192017-01-12 13:17:07 -05003370 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003371 SHA256_Final(out, &ctx);
3372 *out_len = SHA256_DIGEST_LENGTH;
3373 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003374}
Adam Langley1258b6a2014-06-20 12:00:00 -07003375
David Benjaminc11ea9422017-08-29 16:33:21 -04003376// tls1_record_handshake_hashes_for_channel_id records the current handshake
3377// hashes in |hs->new_session| so that Channel ID resumptions can sign that
3378// data.
Steven Valdez908ac192017-01-12 13:17:07 -05003379int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3380 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003381 // This function should never be called for a resumed session because the
3382 // handshake hashes that we wish to record are for the original, full
3383 // handshake.
Steven Valdez87eab492016-06-27 16:34:59 -04003384 if (ssl->session != NULL) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04003385 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003386 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003387
David Benjamina3d76d02017-07-14 19:36:07 -04003388 static_assert(
David Benjamin45738dd2017-02-09 20:01:26 -05003389 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
David Benjamina3d76d02017-07-14 19:36:07 -04003390 "original_handshake_hash is too small");
Steven Valdez908ac192017-01-12 13:17:07 -05003391
3392 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003393 if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
3394 &digest_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04003395 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003396 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003397
David Benjamina3d76d02017-07-14 19:36:07 -04003398 static_assert(EVP_MAX_MD_SIZE <= 0xff,
3399 "EVP_MAX_MD_SIZE does not fit in uint8_t");
David Benjamin45738dd2017-02-09 20:01:26 -05003400 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003401
Adam Langleyfcf25832014-12-18 17:42:32 -08003402 return 1;
3403}
Nick Harper60a85cb2016-09-23 16:25:11 -07003404
3405int ssl_do_channel_id_callback(SSL *ssl) {
3406 if (ssl->tlsext_channel_id_private != NULL ||
3407 ssl->ctx->channel_id_cb == NULL) {
3408 return 1;
3409 }
3410
3411 EVP_PKEY *key = NULL;
3412 ssl->ctx->channel_id_cb(ssl, &key);
3413 if (key == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003414 // The caller should try again later.
Nick Harper60a85cb2016-09-23 16:25:11 -07003415 return 1;
3416 }
3417
3418 int ret = SSL_set1_tls_channel_id(ssl, key);
3419 EVP_PKEY_free(key);
3420 return ret;
3421}
Adam Langleycfa08c32016-11-17 13:21:27 -08003422
3423int ssl_is_sct_list_valid(const CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003424 // Shallow parse the SCT list for sanity. By the RFC
3425 // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3426 // of the SCTs may be empty.
Adam Langleycfa08c32016-11-17 13:21:27 -08003427 CBS copy = *contents;
3428 CBS sct_list;
3429 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3430 CBS_len(&copy) != 0 ||
3431 CBS_len(&sct_list) == 0) {
3432 return 0;
3433 }
3434
3435 while (CBS_len(&sct_list) > 0) {
3436 CBS sct;
3437 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3438 CBS_len(&sct) == 0) {
3439 return 0;
3440 }
3441 }
3442
3443 return 1;
3444}
David Benjamin86e95b82017-07-18 16:34:25 -04003445
3446} // namespace bssl
3447
3448using namespace bssl;
3449
3450int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
3451 uint16_t extension_type,
3452 const uint8_t **out_data,
3453 size_t *out_len) {
3454 CBS cbs;
3455 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
3456 return 0;
3457 }
3458
3459 *out_data = CBS_data(&cbs);
3460 *out_len = CBS_len(&cbs);
3461 return 1;
3462}
3463
3464void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
3465 ctx->ed25519_enabled = !!enabled;
3466}
3467
3468int SSL_extension_supported(unsigned extension_value) {
3469 uint32_t index;
3470 return extension_value == TLSEXT_TYPE_padding ||
3471 tls_extension_find(&index, extension_value) != NULL;
3472}