blob: a7bfec4a2e22be35bfa6c74b0e9e6f6ae3b395d6 [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 Benjamin3a322f52016-10-26 12:45:35 -0400421 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3a322f52016-10-26 12:45:35 -0400422 SSL_SIGN_RSA_PKCS1_SHA256,
423
David Benjaminc11ea9422017-08-29 16:33:21 -0400424 // Larger hashes are acceptable.
David Benjamin3a322f52016-10-26 12:45:35 -0400425 SSL_SIGN_ECDSA_SECP384R1_SHA384,
David Benjamin3a322f52016-10-26 12:45:35 -0400426 SSL_SIGN_RSA_PSS_SHA384,
David Benjamin3a322f52016-10-26 12:45:35 -0400427 SSL_SIGN_RSA_PKCS1_SHA384,
428
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400429 SSL_SIGN_RSA_PSS_SHA512,
430 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400431
David Benjaminc11ea9422017-08-29 16:33:21 -0400432 // For now, SHA-1 is still accepted but least preferable.
David Benjamin3a322f52016-10-26 12:45:35 -0400433 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400434
David Benjamin3a322f52016-10-26 12:45:35 -0400435};
436
David Benjaminc11ea9422017-08-29 16:33:21 -0400437// kSignSignatureAlgorithms is the default list of supported signature
438// algorithms for signing.
439//
440// For now, RSA-PSS signature algorithms are not enabled on Android's system
441// BoringSSL. Once the change in Chrome has stuck and the values are finalized,
442// restore them.
David Benjamin3a322f52016-10-26 12:45:35 -0400443static const uint16_t kSignSignatureAlgorithms[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400444 // List our preferred algorithms first.
David Benjamin69522112017-03-28 15:38:29 -0500445 SSL_SIGN_ED25519,
David Benjamin3a322f52016-10-26 12:45:35 -0400446 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400447 SSL_SIGN_RSA_PSS_SHA256,
448 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400449
David Benjaminc11ea9422017-08-29 16:33:21 -0400450 // If needed, sign larger hashes.
451 //
452 // TODO(davidben): Determine which of these may be pruned.
David Benjamin3a322f52016-10-26 12:45:35 -0400453 SSL_SIGN_ECDSA_SECP384R1_SHA384,
David Benjamin3a322f52016-10-26 12:45:35 -0400454 SSL_SIGN_RSA_PSS_SHA384,
David Benjamin3a322f52016-10-26 12:45:35 -0400455 SSL_SIGN_RSA_PKCS1_SHA384,
456
457 SSL_SIGN_ECDSA_SECP521R1_SHA512,
David Benjamin3a322f52016-10-26 12:45:35 -0400458 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin3a322f52016-10-26 12:45:35 -0400459 SSL_SIGN_RSA_PKCS1_SHA512,
460
David Benjaminc11ea9422017-08-29 16:33:21 -0400461 // If the peer supports nothing else, sign with SHA-1.
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400462 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400463 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400464};
465
David Benjamin69522112017-03-28 15:38:29 -0500466int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) {
David Benjamin71c21b42017-04-14 17:05:40 -0400467 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
468 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
469 if (ssl->ctx->num_verify_sigalgs != 0) {
470 sigalgs = ssl->ctx->verify_sigalgs;
471 num_sigalgs = ssl->ctx->num_verify_sigalgs;
472 }
473
474 for (size_t i = 0; i < num_sigalgs; i++) {
475 if (sigalgs == kVerifySignatureAlgorithms &&
476 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500477 !ssl->ctx->ed25519_enabled) {
478 continue;
479 }
David Benjamin71c21b42017-04-14 17:05:40 -0400480 if (!CBB_add_u16(out, sigalgs[i])) {
David Benjamin69522112017-03-28 15:38:29 -0500481 return 0;
482 }
483 }
484
485 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800486}
Adam Langley95c29f32014-06-20 12:00:00 -0700487
David Benjamin8d606e32017-06-15 22:43:04 -0400488int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg) {
David Benjamin71c21b42017-04-14 17:05:40 -0400489 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
490 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
491 if (ssl->ctx->num_verify_sigalgs != 0) {
492 sigalgs = ssl->ctx->verify_sigalgs;
493 num_sigalgs = ssl->ctx->num_verify_sigalgs;
494 }
495
496 for (size_t i = 0; i < num_sigalgs; i++) {
497 if (sigalgs == kVerifySignatureAlgorithms &&
498 sigalgs[i] == SSL_SIGN_ED25519 &&
David Benjamin69522112017-03-28 15:38:29 -0500499 !ssl->ctx->ed25519_enabled) {
500 continue;
501 }
David Benjamin71c21b42017-04-14 17:05:40 -0400502 if (sigalg == sigalgs[i]) {
David Benjamin3ef76972016-10-17 17:59:54 -0400503 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800504 }
505 }
506
David Benjamin3ef76972016-10-17 17:59:54 -0400507 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
508 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
509 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800510}
511
David Benjaminc11ea9422017-08-29 16:33:21 -0400512// tls_extension represents a TLS extension that is handled internally. The
513// |init| function is called for each handshake, before any other functions of
514// the extension. Then the add and parse callbacks are called as needed.
515//
516// The parse callbacks receive a |CBS| that contains the contents of the
517// extension (i.e. not including the type and length bytes). If an extension is
518// not received then the parse callbacks will be called with a NULL CBS so that
519// they can do any processing needed to handle the absence of an extension.
520//
521// The add callbacks receive a |CBB| to which the extension can be appended but
522// the function is responsible for appending the type and length bytes too.
523//
David Benjamin31640932017-10-11 13:22:39 -0400524// All callbacks return true for success and false for error. If a parse
525// function returns zero then a fatal alert with value |*out_alert| will be
526// sent. If |*out_alert| isn't set, then a |decode_error| alert will be sent.
Adam Langley614c66a2015-06-12 15:26:58 -0700527struct tls_extension {
528 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500529 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700530
David Benjamin31640932017-10-11 13:22:39 -0400531 bool (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
532 bool (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
533 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700534
David Benjamin31640932017-10-11 13:22:39 -0400535 bool (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
536 CBS *contents);
537 bool (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700538};
539
David Benjamin31640932017-10-11 13:22:39 -0400540static bool forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin8c880a22016-12-03 02:20:34 -0500541 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400542 if (contents != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400543 // Servers MUST NOT send this extension.
Steven Valdez6b8509a2016-07-12 13:38:32 -0400544 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
545 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
David Benjamin31640932017-10-11 13:22:39 -0400546 return false;
Steven Valdez6b8509a2016-07-12 13:38:32 -0400547 }
548
David Benjamin31640932017-10-11 13:22:39 -0400549 return true;
Steven Valdez6b8509a2016-07-12 13:38:32 -0400550}
551
David Benjamin31640932017-10-11 13:22:39 -0400552static bool ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin8c880a22016-12-03 02:20:34 -0500553 CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400554 // This extension from the client is handled elsewhere.
David Benjamin31640932017-10-11 13:22:39 -0400555 return true;
Steven Valdez6b8509a2016-07-12 13:38:32 -0400556}
557
David Benjamin31640932017-10-11 13:22:39 -0400558static bool dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
559 return true;
Steven Valdez6b8509a2016-07-12 13:38:32 -0400560}
Adam Langley614c66a2015-06-12 15:26:58 -0700561
David Benjaminc11ea9422017-08-29 16:33:21 -0400562// Server name indication (SNI).
563//
564// https://tools.ietf.org/html/rfc6066#section-3.
Adam Langley614c66a2015-06-12 15:26:58 -0700565
David Benjamin31640932017-10-11 13:22:39 -0400566static bool ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500567 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700568 if (ssl->tlsext_hostname == NULL) {
David Benjamin31640932017-10-11 13:22:39 -0400569 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700570 }
571
572 CBB contents, server_name_list, name;
573 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
574 !CBB_add_u16_length_prefixed(out, &contents) ||
575 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
576 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
577 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
578 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
579 strlen(ssl->tlsext_hostname)) ||
580 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -0400581 return false;
Adam Langley614c66a2015-06-12 15:26:58 -0700582 }
583
David Benjamin31640932017-10-11 13:22:39 -0400584 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700585}
586
David Benjamin31640932017-10-11 13:22:39 -0400587static bool ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
588 CBS *contents) {
David Benjamina8614602017-09-06 15:40:19 -0400589 // The server may acknowledge SNI with an empty extension. We check the syntax
590 // but otherwise ignore this signal.
591 return contents == NULL || CBS_len(contents) == 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700592}
593
David Benjamin31640932017-10-11 13:22:39 -0400594static bool ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
595 CBS *contents) {
David Benjamina8614602017-09-06 15:40:19 -0400596 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700597 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -0400598 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700599 }
600
David Benjamin9b611e22016-03-03 08:48:30 -0500601 CBS server_name_list, host_name;
602 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700603 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500604 !CBS_get_u8(&server_name_list, &name_type) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400605 // Although the server_name extension was intended to be extensible to
606 // new name types and multiple names, OpenSSL 1.0.x had a bug which meant
607 // different name types will cause an error. Further, RFC 4366 originally
608 // defined syntax inextensibly. RFC 6066 corrected this mistake, but
609 // adding new name types is no longer feasible.
610 //
611 // Act as if the extensibility does not exist to simplify parsing.
David Benjamin9b611e22016-03-03 08:48:30 -0500612 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
613 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700614 CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -0400615 return false;
Adam Langley614c66a2015-06-12 15:26:58 -0700616 }
617
David Benjamin9b611e22016-03-03 08:48:30 -0500618 if (name_type != TLSEXT_NAMETYPE_host_name ||
619 CBS_len(&host_name) == 0 ||
620 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
621 CBS_contains_zero_byte(&host_name)) {
622 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
David Benjamin31640932017-10-11 13:22:39 -0400623 return false;
David Benjamin9b611e22016-03-03 08:48:30 -0500624 }
Adam Langley614c66a2015-06-12 15:26:58 -0700625
David Benjaminc11ea9422017-08-29 16:33:21 -0400626 // Copy the hostname as a string.
David Benjamin8e7bbba2017-10-13 17:18:35 -0400627 char *raw = nullptr;
628 if (!CBS_strdup(&host_name, &raw)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900629 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin31640932017-10-11 13:22:39 -0400630 return false;
Adam Langley614c66a2015-06-12 15:26:58 -0700631 }
David Benjamin8e7bbba2017-10-13 17:18:35 -0400632 ssl->s3->hostname.reset(raw);
Adam Langley614c66a2015-06-12 15:26:58 -0700633
David Benjaminfd45ee72017-08-31 14:49:09 -0400634 hs->should_ack_sni = true;
David Benjamin31640932017-10-11 13:22:39 -0400635 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700636}
637
David Benjamin31640932017-10-11 13:22:39 -0400638static bool ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500639 if (hs->ssl->s3->session_reused ||
640 !hs->should_ack_sni) {
David Benjamin31640932017-10-11 13:22:39 -0400641 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700642 }
643
644 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
645 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -0400646 return false;
Adam Langley614c66a2015-06-12 15:26:58 -0700647 }
648
David Benjamin31640932017-10-11 13:22:39 -0400649 return true;
Adam Langley614c66a2015-06-12 15:26:58 -0700650}
651
652
David Benjaminc11ea9422017-08-29 16:33:21 -0400653// Renegotiation indication.
654//
655// https://tools.ietf.org/html/rfc5746
Adam Langley5021b222015-06-12 18:27:58 -0700656
David Benjamin31640932017-10-11 13:22:39 -0400657static bool ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500658 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400659 // Renegotiation indication is not necessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -0400660 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400661 return true;
David Benjamin7c7d8312016-08-20 13:39:03 -0400662 }
663
David Benjamin52bf6902016-10-08 12:05:03 -0400664 assert(ssl->s3->initial_handshake_complete ==
665 (ssl->s3->previous_client_finished_len != 0));
666
Adam Langley5021b222015-06-12 18:27:58 -0700667 CBB contents, prev_finished;
668 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
669 !CBB_add_u16_length_prefixed(out, &contents) ||
670 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
671 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
672 ssl->s3->previous_client_finished_len) ||
673 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -0400674 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700675 }
676
David Benjamin31640932017-10-11 13:22:39 -0400677 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700678}
679
David Benjamin31640932017-10-11 13:22:39 -0400680static bool ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
681 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500682 SSL *const ssl = hs->ssl;
David Benjamind1e3ce12017-10-06 18:31:15 -0400683 if (contents != NULL && ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez246eeee2017-03-26 12:49:17 -0500684 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -0400685 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -0400686 }
687
David Benjaminc11ea9422017-08-29 16:33:21 -0400688 // Servers may not switch between omitting the extension and supporting it.
689 // See RFC 5746, sections 3.5 and 4.2.
David Benjamin3e052de2015-11-25 20:10:31 -0500690 if (ssl->s3->initial_handshake_complete &&
691 (contents != NULL) != ssl->s3->send_connection_binding) {
692 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
693 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
David Benjamin31640932017-10-11 13:22:39 -0400694 return false;
David Benjamin3e052de2015-11-25 20:10:31 -0500695 }
696
Adam Langley5021b222015-06-12 18:27:58 -0700697 if (contents == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400698 // Strictly speaking, if we want to avoid an attack we should *always* see
699 // RI even on initial ServerHello because the client doesn't see any
700 // renegotiation during an attack. However this would mean we could not
701 // connect to any server which doesn't support RI.
702 //
703 // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
704 // practical terms every client sets it so it's just assumed here.
David Benjamin31640932017-10-11 13:22:39 -0400705 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700706 }
707
708 const size_t expected_len = ssl->s3->previous_client_finished_len +
709 ssl->s3->previous_server_finished_len;
710
David Benjaminc11ea9422017-08-29 16:33:21 -0400711 // Check for logic errors
Adam Langley5021b222015-06-12 18:27:58 -0700712 assert(!expected_len || ssl->s3->previous_client_finished_len);
713 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400714 assert(ssl->s3->initial_handshake_complete ==
715 (ssl->s3->previous_client_finished_len != 0));
716 assert(ssl->s3->initial_handshake_complete ==
717 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700718
David Benjaminc11ea9422017-08-29 16:33:21 -0400719 // Parse out the extension contents.
Adam Langley5021b222015-06-12 18:27:58 -0700720 CBS renegotiated_connection;
721 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
722 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400723 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700724 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -0400725 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700726 }
727
David Benjaminc11ea9422017-08-29 16:33:21 -0400728 // Check that the extension matches.
Adam Langley5021b222015-06-12 18:27:58 -0700729 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400730 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700731 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin31640932017-10-11 13:22:39 -0400732 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700733 }
734
735 const uint8_t *d = CBS_data(&renegotiated_connection);
David Benjamin31640932017-10-11 13:22:39 -0400736 bool ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
737 ssl->s3->previous_client_finished_len) == 0;
David Benjamin9343b0b2017-07-01 00:31:27 -0400738#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin31640932017-10-11 13:22:39 -0400739 ok = true;
David Benjamin9343b0b2017-07-01 00:31:27 -0400740#endif
741 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400742 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700743 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin31640932017-10-11 13:22:39 -0400744 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700745 }
746 d += ssl->s3->previous_client_finished_len;
747
David Benjamin9343b0b2017-07-01 00:31:27 -0400748 ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
749 ssl->s3->previous_server_finished_len) == 0;
750#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin31640932017-10-11 13:22:39 -0400751 ok = true;
David Benjamin9343b0b2017-07-01 00:31:27 -0400752#endif
753 if (!ok) {
David Benjamin3570d732015-06-29 00:28:17 -0400754 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley10e10602017-07-25 13:33:21 -0700755 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin31640932017-10-11 13:22:39 -0400756 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700757 }
David Benjamin046bc1f2017-08-31 15:06:42 -0400758 ssl->s3->send_connection_binding = true;
Adam Langley5021b222015-06-12 18:27:58 -0700759
David Benjamin31640932017-10-11 13:22:39 -0400760 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700761}
762
David Benjamin31640932017-10-11 13:22:39 -0400763static bool ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
764 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500765 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400766 // Renegotiation isn't supported as a server so this function should never be
767 // called after the initial handshake.
Adam Langley5021b222015-06-12 18:27:58 -0700768 assert(!ssl->s3->initial_handshake_complete);
769
David Benjamind1e3ce12017-10-06 18:31:15 -0400770 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400771 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -0400772 }
773
Adam Langley5021b222015-06-12 18:27:58 -0700774 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -0400775 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700776 }
777
778 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700779 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
780 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400781 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
David Benjamin31640932017-10-11 13:22:39 -0400782 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700783 }
784
David Benjaminc11ea9422017-08-29 16:33:21 -0400785 // Check that the extension matches. We do not support renegotiation as a
786 // server, so this must be empty.
David Benjamin52bf6902016-10-08 12:05:03 -0400787 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400788 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700789 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin31640932017-10-11 13:22:39 -0400790 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700791 }
792
David Benjamin046bc1f2017-08-31 15:06:42 -0400793 ssl->s3->send_connection_binding = true;
Adam Langley5021b222015-06-12 18:27:58 -0700794
David Benjamin31640932017-10-11 13:22:39 -0400795 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700796}
797
David Benjamin31640932017-10-11 13:22:39 -0400798static bool ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500799 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400800 // Renegotiation isn't supported as a server so this function should never be
801 // called after the initial handshake.
David Benjamin52bf6902016-10-08 12:05:03 -0400802 assert(!ssl->s3->initial_handshake_complete);
803
David Benjamind1e3ce12017-10-06 18:31:15 -0400804 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400805 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -0400806 }
807
Adam Langley5021b222015-06-12 18:27:58 -0700808 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400809 !CBB_add_u16(out, 1 /* length */) ||
810 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
David Benjamin31640932017-10-11 13:22:39 -0400811 return false;
Adam Langley5021b222015-06-12 18:27:58 -0700812 }
813
David Benjamin31640932017-10-11 13:22:39 -0400814 return true;
Adam Langley5021b222015-06-12 18:27:58 -0700815}
816
Adam Langley0a056712015-07-01 15:03:33 -0700817
David Benjaminc11ea9422017-08-29 16:33:21 -0400818// Extended Master Secret.
819//
820// https://tools.ietf.org/html/rfc7627
Adam Langley0a056712015-07-01 15:03:33 -0700821
David Benjamin31640932017-10-11 13:22:39 -0400822static bool ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400823 // Extended master secret is not necessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -0400824 if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400825 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700826 }
827
828 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
829 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -0400830 return false;
Adam Langley0a056712015-07-01 15:03:33 -0700831 }
832
David Benjamin31640932017-10-11 13:22:39 -0400833 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700834}
835
David Benjamin31640932017-10-11 13:22:39 -0400836static bool ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
837 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500838 SSL *const ssl = hs->ssl;
David Benjaminfc02b592017-02-17 16:26:01 -0500839
840 if (contents != NULL) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400841 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjaminfc02b592017-02-17 16:26:01 -0500842 ssl->version == SSL3_VERSION ||
843 CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -0400844 return false;
David Benjamin163c9562016-08-29 23:14:17 -0400845 }
846
David Benjaminfd45ee72017-08-31 14:49:09 -0400847 hs->extended_master_secret = true;
David Benjamin163c9562016-08-29 23:14:17 -0400848 }
849
David Benjaminc11ea9422017-08-29 16:33:21 -0400850 // Whether EMS is negotiated may not change on renegotiation.
David Benjamin8e7bbba2017-10-13 17:18:35 -0400851 if (ssl->s3->established_session != nullptr &&
David Benjaminfc02b592017-02-17 16:26:01 -0500852 hs->extended_master_secret !=
David Benjaminfd45ee72017-08-31 14:49:09 -0400853 !!ssl->s3->established_session->extended_master_secret) {
David Benjaminfc02b592017-02-17 16:26:01 -0500854 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
855 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -0400856 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -0400857 }
858
David Benjamin31640932017-10-11 13:22:39 -0400859 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700860}
861
David Benjamin31640932017-10-11 13:22:39 -0400862static bool ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
863 CBS *contents) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400864 uint16_t version = ssl_protocol_version(hs->ssl);
David Benjamin8c880a22016-12-03 02:20:34 -0500865 if (version >= TLS1_3_VERSION ||
866 version == SSL3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400867 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -0400868 }
869
870 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -0400871 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700872 }
873
874 if (CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -0400875 return false;
Adam Langley0a056712015-07-01 15:03:33 -0700876 }
877
David Benjaminfd45ee72017-08-31 14:49:09 -0400878 hs->extended_master_secret = true;
David Benjamin31640932017-10-11 13:22:39 -0400879 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700880}
881
David Benjamin31640932017-10-11 13:22:39 -0400882static bool ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc02b592017-02-17 16:26:01 -0500883 if (!hs->extended_master_secret) {
David Benjamin31640932017-10-11 13:22:39 -0400884 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700885 }
886
887 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
888 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -0400889 return false;
Adam Langley0a056712015-07-01 15:03:33 -0700890 }
891
David Benjamin31640932017-10-11 13:22:39 -0400892 return true;
Adam Langley0a056712015-07-01 15:03:33 -0700893}
894
Adam Langley9b05bc52015-07-01 15:25:33 -0700895
David Benjaminc11ea9422017-08-29 16:33:21 -0400896// Session tickets.
897//
898// https://tools.ietf.org/html/rfc5077
Adam Langley9b05bc52015-07-01 15:25:33 -0700899
David Benjamin31640932017-10-11 13:22:39 -0400900static bool ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500901 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -0400902 // TLS 1.3 uses a different ticket extension.
David Benjamin68161cb2017-06-20 14:49:43 -0400903 if (hs->min_version >= TLS1_3_VERSION ||
David Benjamin7c7d8312016-08-20 13:39:03 -0400904 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
David Benjamin31640932017-10-11 13:22:39 -0400905 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700906 }
907
908 const uint8_t *ticket_data = NULL;
909 int ticket_len = 0;
910
David Benjaminc11ea9422017-08-29 16:33:21 -0400911 // Renegotiation does not participate in session resumption. However, still
912 // advertise the extension to avoid potentially breaking servers which carry
913 // over the state from the previous handshake, such as OpenSSL servers
914 // without upstream's 3c3f0259238594d77264a78944d409f2127642c4.
Adam Langley9b05bc52015-07-01 15:25:33 -0700915 if (!ssl->s3->initial_handshake_complete &&
916 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400917 ssl->session->tlsext_tick != NULL &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400918 // Don't send TLS 1.3 session tickets in the ticket extension.
David Benjamina4bafd32017-10-03 15:06:29 -0400919 ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700920 ticket_data = ssl->session->tlsext_tick;
921 ticket_len = ssl->session->tlsext_ticklen;
922 }
923
924 CBB ticket;
925 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
926 !CBB_add_u16_length_prefixed(out, &ticket) ||
927 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
928 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -0400929 return false;
Adam Langley9b05bc52015-07-01 15:25:33 -0700930 }
931
David Benjamin31640932017-10-11 13:22:39 -0400932 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700933}
934
David Benjamin31640932017-10-11 13:22:39 -0400935static bool ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
936 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500937 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -0700938 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -0400939 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700940 }
941
David Benjamind1e3ce12017-10-06 18:31:15 -0400942 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400943 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -0400944 }
945
David Benjaminc11ea9422017-08-29 16:33:21 -0400946 // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
947 // this function should never be called, even if the server tries to send the
948 // extension.
Adam Langley9b05bc52015-07-01 15:25:33 -0700949 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
950
951 if (CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -0400952 return false;
Adam Langley9b05bc52015-07-01 15:25:33 -0700953 }
954
David Benjaminfd45ee72017-08-31 14:49:09 -0400955 hs->ticket_expected = true;
David Benjamin31640932017-10-11 13:22:39 -0400956 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700957}
958
David Benjamin31640932017-10-11 13:22:39 -0400959static bool ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500960 if (!hs->ticket_expected) {
David Benjamin31640932017-10-11 13:22:39 -0400961 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700962 }
963
David Benjaminc11ea9422017-08-29 16:33:21 -0400964 // If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true.
David Benjamin8c880a22016-12-03 02:20:34 -0500965 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -0700966
967 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
968 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -0400969 return false;
Adam Langley9b05bc52015-07-01 15:25:33 -0700970 }
971
David Benjamin31640932017-10-11 13:22:39 -0400972 return true;
Adam Langley9b05bc52015-07-01 15:25:33 -0700973}
974
975
David Benjaminc11ea9422017-08-29 16:33:21 -0400976// Signature Algorithms.
977//
978// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
Adam Langley2e857bd2015-07-01 16:09:19 -0700979
David Benjamin31640932017-10-11 13:22:39 -0400980static bool ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -0500981 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -0400982 if (hs->max_version < TLS1_2_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -0400983 return true;
Adam Langley2e857bd2015-07-01 16:09:19 -0700984 }
985
David Benjamin0fc37ef2016-08-17 15:29:46 -0400986 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -0700987 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
988 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin69522112017-03-28 15:38:29 -0500989 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
990 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
991 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -0400992 return false;
Adam Langley2e857bd2015-07-01 16:09:19 -0700993 }
994
David Benjamin31640932017-10-11 13:22:39 -0400995 return true;
Adam Langley2e857bd2015-07-01 16:09:19 -0700996}
997
David Benjamin31640932017-10-11 13:22:39 -0400998static bool ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
999 CBS *contents) {
David Benjaminb1cf48e2017-09-21 11:37:46 -04001000 hs->peer_sigalgs.Reset();
Adam Langley2e857bd2015-07-01 16:09:19 -07001001 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001002 return true;
Adam Langley2e857bd2015-07-01 16:09:19 -07001003 }
1004
1005 CBS supported_signature_algorithms;
1006 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001007 CBS_len(contents) != 0 ||
1008 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001009 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin31640932017-10-11 13:22:39 -04001010 return false;
Adam Langley2e857bd2015-07-01 16:09:19 -07001011 }
1012
David Benjamin31640932017-10-11 13:22:39 -04001013 return true;
Adam Langley2e857bd2015-07-01 16:09:19 -07001014}
1015
Adam Langley2e857bd2015-07-01 16:09:19 -07001016
David Benjaminc11ea9422017-08-29 16:33:21 -04001017// OCSP Stapling.
1018//
1019// https://tools.ietf.org/html/rfc6066#section-8
Adam Langleybb0bd042015-07-01 16:21:03 -07001020
David Benjamin31640932017-10-11 13:22:39 -04001021static bool ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001022 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001023 if (!ssl->ocsp_stapling_enabled) {
David Benjamin31640932017-10-11 13:22:39 -04001024 return true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001025 }
1026
1027 CBB contents;
1028 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1029 !CBB_add_u16_length_prefixed(out, &contents) ||
1030 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1031 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1032 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1033 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001034 return false;
Adam Langleybb0bd042015-07-01 16:21:03 -07001035 }
1036
David Benjamin31640932017-10-11 13:22:39 -04001037 return true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001038}
1039
David Benjamin31640932017-10-11 13:22:39 -04001040static bool ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1041 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001042 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001043 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001044 return true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001045 }
1046
David Benjaminc11ea9422017-08-29 16:33:21 -04001047 // TLS 1.3 OCSP responses are included in the Certificate extensions.
David Benjamind1e3ce12017-10-06 18:31:15 -04001048 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001049 return false;
Steven Valdez803c77a2016-09-06 14:13:43 -04001050 }
1051
David Benjaminc11ea9422017-08-29 16:33:21 -04001052 // OCSP stapling is forbidden on non-certificate ciphers.
Steven Valdeza833c352016-11-01 13:39:36 -04001053 if (CBS_len(contents) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001054 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin31640932017-10-11 13:22:39 -04001055 return false;
David Benjamin942f4ed2016-07-16 19:03:49 +03001056 }
1057
David Benjaminc11ea9422017-08-29 16:33:21 -04001058 // Note this does not check for resumption in TLS 1.2. Sending
1059 // status_request here does not make sense, but OpenSSL does so and the
1060 // specification does not say anything. Tolerate it but ignore it.
David Benjamin942f4ed2016-07-16 19:03:49 +03001061
David Benjaminfd45ee72017-08-31 14:49:09 -04001062 hs->certificate_status_expected = true;
David Benjamin31640932017-10-11 13:22:39 -04001063 return true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001064}
1065
David Benjamin31640932017-10-11 13:22:39 -04001066static bool ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1067 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001068 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001069 return true;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001070 }
1071
1072 uint8_t status_type;
1073 if (!CBS_get_u8(contents, &status_type)) {
David Benjamin31640932017-10-11 13:22:39 -04001074 return false;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001075 }
1076
David Benjaminc11ea9422017-08-29 16:33:21 -04001077 // We cannot decide whether OCSP stapling will occur yet because the correct
1078 // SSL_CTX might not have been selected.
David Benjamin8c880a22016-12-03 02:20:34 -05001079 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001080
David Benjamin31640932017-10-11 13:22:39 -04001081 return true;
Adam Langleybb0bd042015-07-01 16:21:03 -07001082}
1083
David Benjamin31640932017-10-11 13:22:39 -04001084static bool ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001085 SSL *const ssl = hs->ssl;
David Benjamind1e3ce12017-10-06 18:31:15 -04001086 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001087 !hs->ocsp_stapling_requested ||
David Benjamin83a32122017-02-14 18:34:54 -05001088 ssl->cert->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001089 ssl->s3->session_reused ||
David Benjamin45738dd2017-02-09 20:01:26 -05001090 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin31640932017-10-11 13:22:39 -04001091 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04001092 }
1093
David Benjaminfd45ee72017-08-31 14:49:09 -04001094 hs->certificate_status_expected = true;
David Benjamin942f4ed2016-07-16 19:03:49 +03001095
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001096 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001097 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001098}
1099
1100
David Benjaminc11ea9422017-08-29 16:33:21 -04001101// Next protocol negotiation.
1102//
1103// https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
Adam Langley97dfcbf2015-07-01 18:35:20 -07001104
David Benjamin31640932017-10-11 13:22:39 -04001105static bool ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001106 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001107 if (ssl->s3->initial_handshake_complete ||
1108 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001109 SSL_is_dtls(ssl)) {
David Benjamin31640932017-10-11 13:22:39 -04001110 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001111 }
1112
1113 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1114 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -04001115 return false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001116 }
1117
David Benjamin31640932017-10-11 13:22:39 -04001118 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001119}
1120
David Benjamin31640932017-10-11 13:22:39 -04001121static bool ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1122 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001123 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001124 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001125 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001126 }
1127
David Benjamind1e3ce12017-10-06 18:31:15 -04001128 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001129 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04001130 }
1131
David Benjaminc11ea9422017-08-29 16:33:21 -04001132 // If any of these are false then we should never have sent the NPN
1133 // extension in the ClientHello and thus this function should never have been
1134 // called.
Adam Langley97dfcbf2015-07-01 18:35:20 -07001135 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001136 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001137 assert(ssl->ctx->next_proto_select_cb != NULL);
1138
David Benjamin8e7bbba2017-10-13 17:18:35 -04001139 if (!ssl->s3->alpn_selected.empty()) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001140 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001141 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1142 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
David Benjamin31640932017-10-11 13:22:39 -04001143 return false;
David Benjamin76c2efc2015-08-31 14:24:29 -04001144 }
1145
Adam Langley97dfcbf2015-07-01 18:35:20 -07001146 const uint8_t *const orig_contents = CBS_data(contents);
1147 const size_t orig_len = CBS_len(contents);
1148
1149 while (CBS_len(contents) != 0) {
1150 CBS proto;
1151 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1152 CBS_len(&proto) == 0) {
David Benjamin31640932017-10-11 13:22:39 -04001153 return false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001154 }
1155 }
1156
1157 uint8_t *selected;
1158 uint8_t selected_len;
1159 if (ssl->ctx->next_proto_select_cb(
1160 ssl, &selected, &selected_len, orig_contents, orig_len,
David Benjamin8e7bbba2017-10-13 17:18:35 -04001161 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK ||
1162 !ssl->s3->next_proto_negotiated.CopyFrom(
1163 MakeConstSpan(selected, selected_len))) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001164 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001165 return false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001166 }
1167
David Benjaminfd45ee72017-08-31 14:49:09 -04001168 hs->next_proto_neg_seen = true;
David Benjamin31640932017-10-11 13:22:39 -04001169 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001170}
1171
David Benjamin31640932017-10-11 13:22:39 -04001172static bool ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1173 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001174 SSL *const ssl = hs->ssl;
David Benjamind1e3ce12017-10-06 18:31:15 -04001175 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001176 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04001177 }
1178
Adam Langley97dfcbf2015-07-01 18:35:20 -07001179 if (contents != NULL && CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001180 return false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001181 }
1182
1183 if (contents == NULL ||
1184 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001185 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001186 SSL_is_dtls(ssl)) {
David Benjamin31640932017-10-11 13:22:39 -04001187 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001188 }
1189
David Benjaminfd45ee72017-08-31 14:49:09 -04001190 hs->next_proto_neg_seen = true;
David Benjamin31640932017-10-11 13:22:39 -04001191 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001192}
1193
David Benjamin31640932017-10-11 13:22:39 -04001194static bool ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001195 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001196 // |next_proto_neg_seen| might have been cleared when an ALPN extension was
1197 // parsed.
David Benjamin8c880a22016-12-03 02:20:34 -05001198 if (!hs->next_proto_neg_seen) {
David Benjamin31640932017-10-11 13:22:39 -04001199 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001200 }
1201
1202 const uint8_t *npa;
1203 unsigned npa_len;
1204
1205 if (ssl->ctx->next_protos_advertised_cb(
1206 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1207 SSL_TLSEXT_ERR_OK) {
David Benjaminfd45ee72017-08-31 14:49:09 -04001208 hs->next_proto_neg_seen = false;
David Benjamin31640932017-10-11 13:22:39 -04001209 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001210 }
1211
1212 CBB contents;
1213 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1214 !CBB_add_u16_length_prefixed(out, &contents) ||
1215 !CBB_add_bytes(&contents, npa, npa_len) ||
1216 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001217 return false;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001218 }
1219
David Benjamin31640932017-10-11 13:22:39 -04001220 return true;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001221}
1222
1223
David Benjaminc11ea9422017-08-29 16:33:21 -04001224// Signed certificate timestamps.
1225//
1226// https://tools.ietf.org/html/rfc6962#section-3.3.1
Adam Langleyab8d87d2015-07-10 12:21:39 -07001227
David Benjamin31640932017-10-11 13:22:39 -04001228static bool ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001229 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001230 if (!ssl->signed_cert_timestamps_enabled) {
David Benjamin31640932017-10-11 13:22:39 -04001231 return true;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001232 }
1233
1234 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1235 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -04001236 return false;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001237 }
1238
David Benjamin31640932017-10-11 13:22:39 -04001239 return true;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001240}
1241
David Benjamin31640932017-10-11 13:22:39 -04001242static bool ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1243 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001244 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001245 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001246 return true;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001247 }
1248
David Benjaminc11ea9422017-08-29 16:33:21 -04001249 // TLS 1.3 SCTs are included in the Certificate extensions.
David Benjamind1e3ce12017-10-06 18:31:15 -04001250 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001251 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001252 return false;
Steven Valdeza833c352016-11-01 13:39:36 -04001253 }
1254
David Benjaminc11ea9422017-08-29 16:33:21 -04001255 // If this is false then we should never have sent the SCT extension in the
1256 // ClientHello and thus this function should never have been called.
Adam Langleyab8d87d2015-07-10 12:21:39 -07001257 assert(ssl->signed_cert_timestamps_enabled);
1258
Adam Langleycfa08c32016-11-17 13:21:27 -08001259 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001260 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001261 return false;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001262 }
1263
David Benjaminc11ea9422017-08-29 16:33:21 -04001264 // Session resumption uses the original session information. The extension
1265 // should not be sent on resumption, but RFC 6962 did not make it a
1266 // requirement, so tolerate this.
1267 //
1268 // TODO(davidben): Enforce this anyway.
David Benjamin8fc2dc02017-08-22 15:07:51 -07001269 if (!ssl->s3->session_reused) {
1270 CRYPTO_BUFFER_free(hs->new_session->signed_cert_timestamp_list);
1271 hs->new_session->signed_cert_timestamp_list =
1272 CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool);
1273 if (hs->new_session->signed_cert_timestamp_list == nullptr) {
1274 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001275 return false;
David Benjamin8fc2dc02017-08-22 15:07:51 -07001276 }
Adam Langleyab8d87d2015-07-10 12:21:39 -07001277 }
1278
David Benjamin31640932017-10-11 13:22:39 -04001279 return true;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001280}
1281
David Benjamin31640932017-10-11 13:22:39 -04001282static bool ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1283 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001284 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001285 return true;
David Benjamin53210cb2016-11-16 09:01:48 +09001286 }
1287
1288 if (CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001289 return false;
David Benjamin53210cb2016-11-16 09:01:48 +09001290 }
1291
David Benjaminfd45ee72017-08-31 14:49:09 -04001292 hs->scts_requested = true;
David Benjamin31640932017-10-11 13:22:39 -04001293 return true;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001294}
1295
David Benjamin31640932017-10-11 13:22:39 -04001296static bool ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001297 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04001298 // The extension shouldn't be sent when resuming sessions.
David Benjamind1e3ce12017-10-06 18:31:15 -04001299 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
Steven Valdeza833c352016-11-01 13:39:36 -04001300 ssl->s3->session_reused ||
David Benjamin83a32122017-02-14 18:34:54 -05001301 ssl->cert->signed_cert_timestamp_list == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001302 return true;
Paul Lietar4fac72e2015-09-09 13:44:55 +01001303 }
1304
1305 CBB contents;
1306 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1307 CBB_add_u16_length_prefixed(out, &contents) &&
David Benjamin83a32122017-02-14 18:34:54 -05001308 CBB_add_bytes(
1309 &contents,
1310 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1311 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Paul Lietar4fac72e2015-09-09 13:44:55 +01001312 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001313}
1314
1315
David Benjaminc11ea9422017-08-29 16:33:21 -04001316// Application-level Protocol Negotiation.
1317//
1318// https://tools.ietf.org/html/rfc7301
Adam Langleyf18e4532015-07-10 13:39:53 -07001319
David Benjamin31640932017-10-11 13:22:39 -04001320static bool ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001321 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001322 if (ssl->alpn_client_proto_list == NULL ||
1323 ssl->s3->initial_handshake_complete) {
David Benjamin31640932017-10-11 13:22:39 -04001324 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001325 }
1326
1327 CBB contents, proto_list;
1328 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1329 !CBB_add_u16_length_prefixed(out, &contents) ||
1330 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1331 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1332 ssl->alpn_client_proto_list_len) ||
1333 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001334 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001335 }
1336
David Benjamin31640932017-10-11 13:22:39 -04001337 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001338}
1339
David Benjamin31640932017-10-11 13:22:39 -04001340static bool ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1341 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001342 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001343 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001344 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001345 }
1346
1347 assert(!ssl->s3->initial_handshake_complete);
1348 assert(ssl->alpn_client_proto_list != NULL);
1349
David Benjamin8c880a22016-12-03 02:20:34 -05001350 if (hs->next_proto_neg_seen) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001351 // NPN and ALPN may not be negotiated in the same connection.
David Benjamin76c2efc2015-08-31 14:24:29 -04001352 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1353 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
David Benjamin31640932017-10-11 13:22:39 -04001354 return false;
David Benjamin76c2efc2015-08-31 14:24:29 -04001355 }
1356
David Benjaminc11ea9422017-08-29 16:33:21 -04001357 // The extension data consists of a ProtocolNameList which must have
1358 // exactly one ProtocolName. Each of these is length-prefixed.
Adam Langleyf18e4532015-07-10 13:39:53 -07001359 CBS protocol_name_list, protocol_name;
1360 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1361 CBS_len(contents) != 0 ||
1362 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001363 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001364 CBS_len(&protocol_name) == 0 ||
1365 CBS_len(&protocol_name_list) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001366 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001367 }
1368
David Benjamindd6c2e82017-10-17 15:48:46 -04001369 if (!ssl_is_alpn_protocol_allowed(ssl, protocol_name)) {
1370 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1371 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1372 return false;
David Benjamin3e51757d2016-08-11 11:52:23 -04001373 }
1374
David Benjamin8e7bbba2017-10-13 17:18:35 -04001375 if (!ssl->s3->alpn_selected.CopyFrom(protocol_name)) {
Adam Langleyf18e4532015-07-10 13:39:53 -07001376 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001377 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001378 }
1379
David Benjamin31640932017-10-11 13:22:39 -04001380 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001381}
1382
David Benjamindd6c2e82017-10-17 15:48:46 -04001383bool ssl_is_alpn_protocol_allowed(const SSL *ssl,
1384 Span<const uint8_t> protocol) {
1385 if (ssl->alpn_client_proto_list == nullptr) {
1386 return false;
1387 }
1388
1389 if (ssl->ctx->allow_unknown_alpn_protos) {
1390 return true;
1391 }
1392
1393 // Check that the protocol name is one of the ones we advertised.
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 return false;
1401 }
1402
1403 if (client_protocol_name == protocol) {
1404 return true;
1405 }
1406 }
1407
1408 return false;
1409}
1410
David Benjamin31640932017-10-11 13:22:39 -04001411bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1412 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001413 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001414 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001415 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001416 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001417 client_hello, &contents,
1418 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001419 // Ignore ALPN if not configured or no extension was supplied.
David Benjamin31640932017-10-11 13:22:39 -04001420 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001421 }
1422
David Benjaminc11ea9422017-08-29 16:33:21 -04001423 // ALPN takes precedence over NPN.
David Benjaminfd45ee72017-08-31 14:49:09 -04001424 hs->next_proto_neg_seen = false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001425
1426 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001427 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1428 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001429 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001430 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1431 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001432 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001433 }
1434
David Benjaminc11ea9422017-08-29 16:33:21 -04001435 // Validate the protocol list.
Adam Langleyf18e4532015-07-10 13:39:53 -07001436 CBS protocol_name_list_copy = protocol_name_list;
1437 while (CBS_len(&protocol_name_list_copy) > 0) {
1438 CBS protocol_name;
1439
1440 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001441 // Empty protocol names are forbidden.
Adam Langleyf18e4532015-07-10 13:39:53 -07001442 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001443 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1444 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001445 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001446 }
1447 }
1448
1449 const uint8_t *selected;
1450 uint8_t selected_len;
1451 if (ssl->ctx->alpn_select_cb(
1452 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1453 CBS_len(&protocol_name_list),
1454 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
David Benjamin8e7bbba2017-10-13 17:18:35 -04001455 if (!ssl->s3->alpn_selected.CopyFrom(
1456 MakeConstSpan(selected, selected_len))) {
Adam Langleyf18e4532015-07-10 13:39:53 -07001457 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001458 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001459 }
Adam Langleyf18e4532015-07-10 13:39:53 -07001460 }
1461
David Benjamin31640932017-10-11 13:22:39 -04001462 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001463}
1464
David Benjamin31640932017-10-11 13:22:39 -04001465static bool ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001466 SSL *const ssl = hs->ssl;
David Benjamin8e7bbba2017-10-13 17:18:35 -04001467 if (ssl->s3->alpn_selected.empty()) {
David Benjamin31640932017-10-11 13:22:39 -04001468 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001469 }
1470
1471 CBB contents, proto_list, proto;
1472 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1473 !CBB_add_u16_length_prefixed(out, &contents) ||
1474 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1475 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin8e7bbba2017-10-13 17:18:35 -04001476 !CBB_add_bytes(&proto, ssl->s3->alpn_selected.data(),
1477 ssl->s3->alpn_selected.size()) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001478 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001479 return false;
Adam Langleyf18e4532015-07-10 13:39:53 -07001480 }
1481
David Benjamin31640932017-10-11 13:22:39 -04001482 return true;
Adam Langleyf18e4532015-07-10 13:39:53 -07001483}
1484
1485
David Benjaminc11ea9422017-08-29 16:33:21 -04001486// Channel ID.
1487//
1488// https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
Adam Langley49c7af12015-07-10 14:33:46 -07001489
David Benjamin8c880a22016-12-03 02:20:34 -05001490static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
David Benjamin046bc1f2017-08-31 15:06:42 -04001491 hs->ssl->s3->tlsext_channel_id_valid = false;
Adam Langley49c7af12015-07-10 14:33:46 -07001492}
1493
David Benjamin31640932017-10-11 13:22:39 -04001494static bool ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001495 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001496 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001497 SSL_is_dtls(ssl)) {
David Benjamin31640932017-10-11 13:22:39 -04001498 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001499 }
1500
1501 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1502 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -04001503 return false;
Adam Langley49c7af12015-07-10 14:33:46 -07001504 }
1505
David Benjamin31640932017-10-11 13:22:39 -04001506 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001507}
1508
David Benjamin31640932017-10-11 13:22:39 -04001509static bool ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1510 uint8_t *out_alert,
1511 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001512 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001513 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001514 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001515 }
1516
David Benjamince079fd2016-08-02 16:22:34 -04001517 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001518 assert(ssl->tlsext_channel_id_enabled);
1519
1520 if (CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001521 return false;
Adam Langley49c7af12015-07-10 14:33:46 -07001522 }
1523
David Benjamin046bc1f2017-08-31 15:06:42 -04001524 ssl->s3->tlsext_channel_id_valid = true;
David Benjamin31640932017-10-11 13:22:39 -04001525 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001526}
1527
David Benjamin31640932017-10-11 13:22:39 -04001528static bool ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1529 uint8_t *out_alert,
1530 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001531 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001532 if (contents == NULL ||
1533 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001534 SSL_is_dtls(ssl)) {
David Benjamin31640932017-10-11 13:22:39 -04001535 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001536 }
1537
1538 if (CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001539 return false;
Adam Langley49c7af12015-07-10 14:33:46 -07001540 }
1541
David Benjamin046bc1f2017-08-31 15:06:42 -04001542 ssl->s3->tlsext_channel_id_valid = true;
David Benjamin31640932017-10-11 13:22:39 -04001543 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001544}
1545
David Benjamin31640932017-10-11 13:22:39 -04001546static bool ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001547 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001548 if (!ssl->s3->tlsext_channel_id_valid) {
David Benjamin31640932017-10-11 13:22:39 -04001549 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001550 }
1551
1552 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1553 !CBB_add_u16(out, 0 /* length */)) {
David Benjamin31640932017-10-11 13:22:39 -04001554 return false;
Adam Langley49c7af12015-07-10 14:33:46 -07001555 }
1556
David Benjamin31640932017-10-11 13:22:39 -04001557 return true;
Adam Langley49c7af12015-07-10 14:33:46 -07001558}
1559
Adam Langley391250d2015-07-15 19:06:07 -07001560
David Benjaminc11ea9422017-08-29 16:33:21 -04001561// Secure Real-time Transport Protocol (SRTP) extension.
1562//
1563// https://tools.ietf.org/html/rfc5764
Adam Langley391250d2015-07-15 19:06:07 -07001564
Adam Langley391250d2015-07-15 19:06:07 -07001565
David Benjamin8c880a22016-12-03 02:20:34 -05001566static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1567 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001568}
1569
David Benjamin31640932017-10-11 13:22:39 -04001570static bool ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001571 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001572 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
David Benjaminee910bf2017-07-25 22:36:00 -04001573 if (profiles == NULL ||
1574 sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
David Benjamin31640932017-10-11 13:22:39 -04001575 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001576 }
1577
1578 CBB contents, profile_ids;
1579 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1580 !CBB_add_u16_length_prefixed(out, &contents) ||
1581 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
David Benjamin31640932017-10-11 13:22:39 -04001582 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001583 }
1584
David Benjaminee910bf2017-07-25 22:36:00 -04001585 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
1586 if (!CBB_add_u16(&profile_ids, profile->id)) {
David Benjamin31640932017-10-11 13:22:39 -04001587 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001588 }
1589 }
1590
1591 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1592 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001593 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001594 }
1595
David Benjamin31640932017-10-11 13:22:39 -04001596 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001597}
1598
David Benjamin31640932017-10-11 13:22:39 -04001599static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1600 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001601 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001602 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001603 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001604 }
1605
David Benjaminc11ea9422017-08-29 16:33:21 -04001606 // The extension consists of a u16-prefixed profile ID list containing a
1607 // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1608 //
1609 // See https://tools.ietf.org/html/rfc5764#section-4.1.1
Adam Langley391250d2015-07-15 19:06:07 -07001610 CBS profile_ids, srtp_mki;
1611 uint16_t profile_id;
1612 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1613 !CBS_get_u16(&profile_ids, &profile_id) ||
1614 CBS_len(&profile_ids) != 0 ||
1615 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1616 CBS_len(contents) != 0) {
1617 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
David Benjamin31640932017-10-11 13:22:39 -04001618 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001619 }
1620
1621 if (CBS_len(&srtp_mki) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001622 // Must be no MKI, since we never offer one.
Adam Langley391250d2015-07-15 19:06:07 -07001623 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1624 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04001625 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001626 }
1627
1628 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1629
David Benjaminc11ea9422017-08-29 16:33:21 -04001630 // Check to see if the server gave us something we support (and presumably
1631 // offered).
David Benjaminee910bf2017-07-25 22:36:00 -04001632 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001633 if (profile->id == profile_id) {
1634 ssl->srtp_profile = profile;
David Benjamin31640932017-10-11 13:22:39 -04001635 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001636 }
1637 }
1638
1639 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1640 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04001641 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001642}
1643
David Benjamin31640932017-10-11 13:22:39 -04001644static bool ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1645 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001646 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001647 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001648 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001649 }
1650
1651 CBS profile_ids, srtp_mki;
1652 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1653 CBS_len(&profile_ids) < 2 ||
1654 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1655 CBS_len(contents) != 0) {
1656 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
David Benjamin31640932017-10-11 13:22:39 -04001657 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001658 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001659 // Discard the MKI value for now.
Adam Langley391250d2015-07-15 19:06:07 -07001660
1661 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1662 SSL_get_srtp_profiles(ssl);
1663
David Benjaminc11ea9422017-08-29 16:33:21 -04001664 // Pick the server's most preferred profile.
David Benjaminee910bf2017-07-25 22:36:00 -04001665 for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) {
Adam Langley391250d2015-07-15 19:06:07 -07001666 CBS profile_ids_tmp;
1667 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1668
1669 while (CBS_len(&profile_ids_tmp) > 0) {
1670 uint16_t profile_id;
1671 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
David Benjamin31640932017-10-11 13:22:39 -04001672 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001673 }
1674
1675 if (server_profile->id == profile_id) {
1676 ssl->srtp_profile = server_profile;
David Benjamin31640932017-10-11 13:22:39 -04001677 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001678 }
1679 }
1680 }
1681
David Benjamin31640932017-10-11 13:22:39 -04001682 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001683}
1684
David Benjamin31640932017-10-11 13:22:39 -04001685static bool ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001686 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001687 if (ssl->srtp_profile == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001688 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001689 }
1690
1691 CBB contents, profile_ids;
1692 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1693 !CBB_add_u16_length_prefixed(out, &contents) ||
1694 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1695 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1696 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1697 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001698 return false;
Adam Langley391250d2015-07-15 19:06:07 -07001699 }
1700
David Benjamin31640932017-10-11 13:22:39 -04001701 return true;
Adam Langley391250d2015-07-15 19:06:07 -07001702}
1703
Adam Langleybdd5d662015-07-20 16:19:08 -07001704
David Benjaminc11ea9422017-08-29 16:33:21 -04001705// EC point formats.
1706//
1707// https://tools.ietf.org/html/rfc4492#section-5.1.2
Adam Langleybdd5d662015-07-20 16:19:08 -07001708
David Benjamin31640932017-10-11 13:22:39 -04001709static bool ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001710 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001711 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1712 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001713 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1714 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001715 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001716 return false;
Adam Langleybdd5d662015-07-20 16:19:08 -07001717 }
1718
David Benjamin31640932017-10-11 13:22:39 -04001719 return true;
Adam Langleybdd5d662015-07-20 16:19:08 -07001720}
1721
David Benjamin31640932017-10-11 13:22:39 -04001722static bool ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001723 // The point format extension is unneccessary in TLS 1.3.
David Benjamin68161cb2017-06-20 14:49:43 -04001724 if (hs->min_version >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001725 return true;
David Benjamin70aba262016-11-01 12:08:15 -04001726 }
1727
David Benjamin8c880a22016-12-03 02:20:34 -05001728 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001729}
1730
David Benjamin31640932017-10-11 13:22:39 -04001731static bool ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1732 CBS *contents) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001733 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001734 return true;
Adam Langleybdd5d662015-07-20 16:19:08 -07001735 }
1736
David Benjamind1e3ce12017-10-06 18:31:15 -04001737 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001738 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04001739 }
1740
Adam Langleybdd5d662015-07-20 16:19:08 -07001741 CBS ec_point_format_list;
1742 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1743 CBS_len(contents) != 0) {
David Benjamin31640932017-10-11 13:22:39 -04001744 return false;
Adam Langleybdd5d662015-07-20 16:19:08 -07001745 }
1746
David Benjaminc11ea9422017-08-29 16:33:21 -04001747 // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1748 // point format.
David Benjamin17cf2cb2016-12-13 01:07:13 -05001749 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1750 TLSEXT_ECPOINTFORMAT_uncompressed,
1751 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001752 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04001753 return false;
Adam Langleybdd5d662015-07-20 16:19:08 -07001754 }
1755
David Benjamin31640932017-10-11 13:22:39 -04001756 return true;
Adam Langleybdd5d662015-07-20 16:19:08 -07001757}
1758
David Benjamin31640932017-10-11 13:22:39 -04001759static bool ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001760 CBS *contents) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001761 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001762 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04001763 }
1764
David Benjamin8c880a22016-12-03 02:20:34 -05001765 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001766}
1767
David Benjamin31640932017-10-11 13:22:39 -04001768static bool ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001769 SSL *const ssl = hs->ssl;
David Benjamind1e3ce12017-10-06 18:31:15 -04001770 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001771 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04001772 }
1773
David Benjamin45738dd2017-02-09 20:01:26 -05001774 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1775 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjamin31640932017-10-11 13:22:39 -04001776 const bool using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001777
1778 if (!using_ecc) {
David Benjamin31640932017-10-11 13:22:39 -04001779 return true;
Adam Langleybdd5d662015-07-20 16:19:08 -07001780 }
1781
David Benjamin8c880a22016-12-03 02:20:34 -05001782 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001783}
1784
Steven Valdeza833c352016-11-01 13:39:36 -04001785
David Benjaminc11ea9422017-08-29 16:33:21 -04001786// Pre Shared Key
1787//
1788// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
Steven Valdez4aa154e2016-07-29 14:32:55 -04001789
David Benjamin8c880a22016-12-03 02:20:34 -05001790static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1791 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04001792 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamina4bafd32017-10-03 15:06:29 -04001793 ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdeza833c352016-11-01 13:39:36 -04001794 return 0;
1795 }
1796
David Benjamina4bafd32017-10-03 15:06:29 -04001797 size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001798 return 15 + ssl->session->tlsext_ticklen + binder_len;
1799}
1800
David Benjamin31640932017-10-11 13:22:39 -04001801static bool ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05001802 SSL *const ssl = hs->ssl;
Steven Valdezcd8470f2017-10-11 12:29:36 -04001803 hs->needs_psk_binder = false;
David Benjamin68161cb2017-06-20 14:49:43 -04001804 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamina4bafd32017-10-03 15:06:29 -04001805 ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001806 return true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001807 }
1808
Steven Valdezcd8470f2017-10-11 12:29:36 -04001809 // Per draft-ietf-tls-tls13-21 section 4.1.4, skip offering the session if the
1810 // selected cipher in HelloRetryRequest does not match. This avoids performing
1811 // the transcript hash transformation for multiple hashes.
1812 if (hs->received_hello_retry_request &&
1813 ssl_is_draft21(ssl->version) &&
1814 ssl->session->cipher->algorithm_prf != hs->new_cipher->algorithm_prf) {
1815 return true;
1816 }
1817
David Benjaminad8f5e12017-02-20 17:00:20 -05001818 struct OPENSSL_timeval now;
Steven Valdeza833c352016-11-01 13:39:36 -04001819 ssl_get_current_time(ssl, &now);
1820 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1821 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1822
David Benjaminc11ea9422017-08-29 16:33:21 -04001823 // Fill in a placeholder zero binder of the appropriate length. It will be
1824 // computed and filled in later after length prefixes are computed.
Steven Valdeza833c352016-11-01 13:39:36 -04001825 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
David Benjamina4bafd32017-10-03 15:06:29 -04001826 size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session));
Steven Valdeza833c352016-11-01 13:39:36 -04001827
1828 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001829 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1830 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001831 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001832 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1833 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001834 ssl->session->tlsext_ticklen) ||
1835 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1836 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1837 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1838 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
David Benjamin31640932017-10-11 13:22:39 -04001839 return false;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001840 }
1841
David Benjaminfd45ee72017-08-31 14:49:09 -04001842 hs->needs_psk_binder = true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001843 return CBB_flush(out);
1844}
1845
David Benjamin31640932017-10-11 13:22:39 -04001846bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1847 uint8_t *out_alert,
1848 CBS *contents) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001849 uint16_t psk_id;
1850 if (!CBS_get_u16(contents, &psk_id) ||
1851 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001852 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001853 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001854 return false;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001855 }
1856
David Benjaminc11ea9422017-08-29 16:33:21 -04001857 // We only advertise one PSK identity, so the only legal index is zero.
Steven Valdez4aa154e2016-07-29 14:32:55 -04001858 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001859 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001860 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
David Benjamin31640932017-10-11 13:22:39 -04001861 return false;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001862 }
1863
David Benjamin31640932017-10-11 13:22:39 -04001864 return true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001865}
1866
David Benjamin31640932017-10-11 13:22:39 -04001867bool ssl_ext_pre_shared_key_parse_clienthello(
David Benjamin707af292017-03-10 17:47:18 -05001868 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
David Benjamin35ac5b72017-03-03 15:05:56 -05001869 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001870 // We only process the first PSK identity since we don't support pure PSK.
David Benjamin707af292017-03-10 17:47:18 -05001871 CBS identities, binders;
David Benjaminaedf3032016-12-01 16:47:56 -05001872 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
David Benjamin707af292017-03-10 17:47:18 -05001873 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
David Benjamin35ac5b72017-03-03 15:05:56 -05001874 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001875 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001876 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001877 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001878 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001879 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001880 return false;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001881 }
1882
Steven Valdeza833c352016-11-01 13:39:36 -04001883 *out_binders = binders;
1884
David Benjaminc11ea9422017-08-29 16:33:21 -04001885 // Check the syntax of the remaining identities, but do not process them.
David Benjaminaedf3032016-12-01 16:47:56 -05001886 size_t num_identities = 1;
1887 while (CBS_len(&identities) != 0) {
1888 CBS unused_ticket;
1889 uint32_t unused_obfuscated_ticket_age;
1890 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1891 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1892 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1893 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001894 return false;
David Benjaminaedf3032016-12-01 16:47:56 -05001895 }
1896
1897 num_identities++;
1898 }
1899
David Benjaminc11ea9422017-08-29 16:33:21 -04001900 // Check the syntax of the binders. The value will be checked later if
1901 // resuming.
David Benjaminaedf3032016-12-01 16:47:56 -05001902 size_t num_binders = 0;
1903 while (CBS_len(&binders) != 0) {
1904 CBS binder;
1905 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1906 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1907 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001908 return false;
David Benjaminaedf3032016-12-01 16:47:56 -05001909 }
1910
1911 num_binders++;
1912 }
1913
1914 if (num_identities != num_binders) {
1915 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1916 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04001917 return false;
Steven Valdez5b986082016-09-01 12:29:49 -04001918 }
1919
David Benjamin31640932017-10-11 13:22:39 -04001920 return true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001921}
1922
David Benjamin31640932017-10-11 13:22:39 -04001923bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8baf9632016-11-17 17:11:16 +09001924 if (!hs->ssl->s3->session_reused) {
David Benjamin31640932017-10-11 13:22:39 -04001925 return true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001926 }
1927
1928 CBB contents;
1929 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1930 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001931 // We only consider the first identity for resumption
Steven Valdez4aa154e2016-07-29 14:32:55 -04001932 !CBB_add_u16(&contents, 0) ||
1933 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04001934 return false;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001935 }
1936
David Benjamin31640932017-10-11 13:22:39 -04001937 return true;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001938}
1939
1940
David Benjaminc11ea9422017-08-29 16:33:21 -04001941// Pre-Shared Key Exchange Modes
1942//
1943// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7
Steven Valdeza4ee74d2016-11-29 13:36:45 -05001944
David Benjamin31640932017-10-11 13:22:39 -04001945static bool ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
1946 CBB *out) {
David Benjamin68161cb2017-06-20 14:49:43 -04001947 if (hs->max_version < TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04001948 return true;
Steven Valdeza833c352016-11-01 13:39:36 -04001949 }
1950
1951 CBB contents, ke_modes;
1952 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
1953 !CBB_add_u16_length_prefixed(out, &contents) ||
1954 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
1955 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
David Benjamin31640932017-10-11 13:22:39 -04001956 return false;
Steven Valdeza833c352016-11-01 13:39:36 -04001957 }
1958
1959 return CBB_flush(out);
1960}
1961
David Benjamin31640932017-10-11 13:22:39 -04001962static bool ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
1963 uint8_t *out_alert,
1964 CBS *contents) {
David Benjamin4eb95cc2016-11-16 17:08:23 +09001965 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04001966 return true;
David Benjamin4eb95cc2016-11-16 17:08:23 +09001967 }
1968
Steven Valdeza833c352016-11-01 13:39:36 -04001969 CBS ke_modes;
1970 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
1971 CBS_len(&ke_modes) == 0 ||
1972 CBS_len(contents) != 0) {
1973 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04001974 return false;
Steven Valdeza833c352016-11-01 13:39:36 -04001975 }
1976
David Benjaminc11ea9422017-08-29 16:33:21 -04001977 // We only support tickets with PSK_DHE_KE.
David Benjamin17cf2cb2016-12-13 01:07:13 -05001978 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
1979 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04001980
David Benjamin31640932017-10-11 13:22:39 -04001981 return true;
Steven Valdeza833c352016-11-01 13:39:36 -04001982}
1983
1984
David Benjaminc11ea9422017-08-29 16:33:21 -04001985// Early Data Indication
1986//
1987// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
Steven Valdeza4ee74d2016-11-29 13:36:45 -05001988
David Benjamin31640932017-10-11 13:22:39 -04001989static bool ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05001990 SSL *const ssl = hs->ssl;
David Benjamindd6c2e82017-10-17 15:48:46 -04001991 if (!ssl->cert->enable_early_data ||
1992 // Session must be 0-RTT capable.
1993 ssl->session == NULL ||
David Benjamina4bafd32017-10-03 15:06:29 -04001994 ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION ||
Steven Valdez2d850622017-01-11 11:34:52 -05001995 ssl->session->ticket_max_early_data == 0 ||
David Benjamindd6c2e82017-10-17 15:48:46 -04001996 // The second ClientHello never offers early data.
Steven Valdez2d850622017-01-11 11:34:52 -05001997 hs->received_hello_retry_request ||
David Benjamindd6c2e82017-10-17 15:48:46 -04001998 // In case ALPN preferences changed since this session was established,
1999 // avoid reporting a confusing value in |SSL_get0_alpn_selected|.
2000 (ssl->session->early_alpn_len != 0 &&
2001 !ssl_is_alpn_protocol_allowed(
2002 ssl, MakeConstSpan(ssl->session->early_alpn,
2003 ssl->session->early_alpn_len)))) {
David Benjamin31640932017-10-11 13:22:39 -04002004 return true;
Steven Valdez2d850622017-01-11 11:34:52 -05002005 }
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)) {
David Benjamin31640932017-10-11 13:22:39 -04002012 return false;
Steven Valdez2d850622017-01-11 11:34:52 -05002013 }
2014
David Benjamin31640932017-10-11 13:22:39 -04002015 return true;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002016}
2017
David Benjamin31640932017-10-11 13:22:39 -04002018static bool ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
2019 uint8_t *out_alert, CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05002020 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002021 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04002022 return true;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002023 }
2024
2025 if (CBS_len(contents) != 0) {
2026 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04002027 return false;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002028 }
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);
David Benjamin31640932017-10-11 13:22:39 -04002033 return false;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002034 }
Steven Valdez2d850622017-01-11 11:34:52 -05002035
David Benjamin7e58c5e2017-10-11 13:01:08 -04002036 ssl->early_data_accepted = true;
David Benjamin31640932017-10-11 13:22:39 -04002037 return true;
Steven Valdez2d850622017-01-11 11:34:52 -05002038}
2039
David Benjamin31640932017-10-11 13:22:39 -04002040static bool ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2041 uint8_t *out_alert, CBS *contents) {
Steven Valdez2d850622017-01-11 11:34:52 -05002042 SSL *const ssl = hs->ssl;
2043 if (contents == NULL ||
David Benjamind1e3ce12017-10-06 18:31:15 -04002044 ssl_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04002045 return true;
Steven Valdez2d850622017-01-11 11:34:52 -05002046 }
2047
2048 if (CBS_len(contents) != 0) {
2049 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31640932017-10-11 13:22:39 -04002050 return false;
Steven Valdez2d850622017-01-11 11:34:52 -05002051 }
2052
David Benjaminfd45ee72017-08-31 14:49:09 -04002053 hs->early_data_offered = true;
David Benjamin31640932017-10-11 13:22:39 -04002054 return true;
Steven Valdez2d850622017-01-11 11:34:52 -05002055}
2056
David Benjamin31640932017-10-11 13:22:39 -04002057static bool ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez2d850622017-01-11 11:34:52 -05002058 if (!hs->ssl->early_data_accepted) {
David Benjamin31640932017-10-11 13:22:39 -04002059 return true;
Steven Valdez2d850622017-01-11 11:34:52 -05002060 }
2061
2062 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2063 !CBB_add_u16(out, 0) ||
2064 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04002065 return false;
Steven Valdez2d850622017-01-11 11:34:52 -05002066 }
2067
David Benjamin31640932017-10-11 13:22:39 -04002068 return true;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002069}
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 Benjamin31640932017-10-11 13:22:39 -04002076static bool ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05002077 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002078 if (hs->max_version < TLS1_3_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04002079 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04002080 }
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)) {
David Benjamin31640932017-10-11 13:22:39 -04002086 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002087 }
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())) {
David Benjamin31640932017-10-11 13:22:39 -04002096 return false;
Steven Valdez5440fe02016-07-18 12:40:30 -04002097 }
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 */))) {
David Benjamin31640932017-10-11 13:22:39 -04002109 return false;
David Benjamin65ac9972016-09-02 21:35:25 -04002110 }
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);
David Benjamin31640932017-10-11 13:22:39 -04002116 return false;
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)) {
David Benjamin31640932017-10-11 13:22:39 -04002129 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002130 }
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)))) {
David Benjamin31640932017-10-11 13:22:39 -04002136 return false;
Steven Valdez5440fe02016-07-18 12:40:30 -04002137 }
2138
Steven Valdez143e8b32016-07-11 13:19:03 -04002139 return CBB_flush(out);
2140}
2141
David Benjamin31640932017-10-11 13:22:39 -04002142bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
2143 Array<uint8_t> *out_secret,
2144 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;
David Benjamin31640932017-10-11 13:22:39 -04002151 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002152 }
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);
David Benjamin31640932017-10-11 13:22:39 -04002157 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002158 }
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;
David Benjamin31640932017-10-11 13:22:39 -04002162 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002163 }
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();
David Benjamin31640932017-10-11 13:22:39 -04002167 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04002168}
2169
David Benjamin31640932017-10-11 13:22:39 -04002170bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
2171 Array<uint8_t> *out_secret,
2172 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;
David Benjamin31640932017-10-11 13:22:39 -04002178 return false;
Steven Valdez803c77a2016-09-06 14:13:43 -04002179 }
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);
David Benjamin31640932017-10-11 13:22:39 -04002184 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002185 }
2186
David Benjaminc11ea9422017-08-29 16:33:21 -04002187 // Find the corresponding key share.
David Benjamin7e1f9842016-09-20 19:24:40 -04002188 CBS peer_key;
Steven Valdez619c8ce2017-10-16 13:12:33 -04002189 CBS_init(&peer_key, NULL, 0);
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) ||
Steven Valdez619c8ce2017-10-16 13:12:33 -04002194 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp) ||
2195 CBS_len(&peer_key_tmp) == 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002196 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin31640932017-10-11 13:22:39 -04002197 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002198 }
2199
David Benjamin7e1f9842016-09-20 19:24:40 -04002200 if (id == group_id) {
Steven Valdez619c8ce2017-10-16 13:12:33 -04002201 if (CBS_len(&peer_key) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002202 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2203 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin31640932017-10-11 13:22:39 -04002204 return false;
David Benjamin7e1f9842016-09-20 19:24:40 -04002205 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002206
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
Steven Valdez619c8ce2017-10-16 13:12:33 -04002212 if (CBS_len(&peer_key) == 0) {
David Benjamin74795b32017-08-31 15:13:12 -04002213 *out_found = false;
David Benjamin499742c2017-07-22 12:45:38 -04002214 out_secret->Reset();
David Benjamin31640932017-10-11 13:22:39 -04002215 return true;
David Benjamin7e1f9842016-09-20 19:24:40 -04002216 }
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 Benjamin31640932017-10-11 13:22:39 -04002227 return false;
David Benjamin7e1f9842016-09-20 19:24:40 -04002228 }
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;
David Benjamin31640932017-10-11 13:22:39 -04002232 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04002233}
2234
David Benjamin31640932017-10-11 13:22:39 -04002235bool 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)) {
David Benjamin31640932017-10-11 13:22:39 -04002246 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -04002247 }
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;
David Benjamin31640932017-10-11 13:22:39 -04002252 return true;
Steven Valdez143e8b32016-07-11 13:19:03 -04002253}
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 Benjamin31640932017-10-11 13:22:39 -04002260static bool ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05002261 SSL *const ssl = hs->ssl;
David Benjamin68161cb2017-06-20 14:49:43 -04002262 if (hs->max_version <= TLS1_2_VERSION) {
David Benjamin31640932017-10-11 13:22:39 -04002263 return true;
Steven Valdezfdd10992016-09-15 16:27:05 -04002264 }
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)) {
David Benjamin31640932017-10-11 13:22:39 -04002270 return false;
Steven Valdezfdd10992016-09-15 16:27:05 -04002271 }
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))) {
David Benjamin31640932017-10-11 13:22:39 -04002276 return false;
David Benjamind9791bf2016-09-27 16:39:52 -04002277 }
2278
Steven Valdez8f36c512017-06-20 10:55:02 -04002279 if (!ssl_add_supported_versions(hs, &versions) ||
2280 !CBB_flush(out)) {
David Benjamin31640932017-10-11 13:22:39 -04002281 return false;
Steven Valdezfdd10992016-09-15 16:27:05 -04002282 }
2283
David Benjamin31640932017-10-11 13:22:39 -04002284 return true;
Steven Valdezfdd10992016-09-15 16:27:05 -04002285}
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 Benjamin31640932017-10-11 13:22:39 -04002292static bool ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminb9493552017-09-27 19:02:51 -04002293 if (hs->cookie.empty()) {
David Benjamin31640932017-10-11 13:22:39 -04002294 return true;
David Benjamin3baa6e12016-10-07 21:10:38 -04002295 }
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)) {
David Benjamin31640932017-10-11 13:22:39 -04002303 return false;
David Benjamin3baa6e12016-10-07 21:10:38 -04002304 }
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 Benjamin31640932017-10-11 13:22:39 -04002308 return true;
David Benjamin3baa6e12016-10-07 21:10:38 -04002309}
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 Benjamin31640932017-10-11 13:22:39 -04002317static bool ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin8c880a22016-12-03 02:20:34 -05002318 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)) {
David Benjamin31640932017-10-11 13:22:39 -04002323 return false;
Adam Langley273d49c2015-07-20 16:38:52 -07002324 }
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))) {
David Benjamin31640932017-10-11 13:22:39 -04002330 return false;
David Benjamin65ac9972016-09-02 21:35:25 -04002331 }
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)) {
David Benjamin31640932017-10-11 13:22:39 -04002335 return false;
Adam Langley273d49c2015-07-20 16:38:52 -07002336 }
2337 }
2338
2339 return CBB_flush(out);
2340}
2341
David Benjamin31640932017-10-11 13:22:39 -04002342static bool ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2343 uint8_t *out_alert,
2344 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.
David Benjamin31640932017-10-11 13:22:39 -04002347 return true;
Adam Langley273d49c2015-07-20 16:38:52 -07002348}
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 Benjamin31640932017-10-11 13:22:39 -04002373static bool ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin8c880a22016-12-03 02:20:34 -05002374 uint8_t *out_alert,
David Benjamin31640932017-10-11 13:22:39 -04002375 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002376 if (contents == NULL) {
David Benjamin31640932017-10-11 13:22:39 -04002377 return true;
Adam Langley273d49c2015-07-20 16:38:52 -07002378 }
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)) {
David Benjamin31640932017-10-11 13:22:39 -04002385 return false;
Adam Langley273d49c2015-07-20 16:38:52 -07002386 }
2387
David Benjamin31640932017-10-11 13:22:39 -04002388 return true;
Adam Langley273d49c2015-07-20 16:38:52 -07002389}
2390
2391
David Benjaminc11ea9422017-08-29 16:33:21 -04002392// kExtensions contains all the supported extensions.
Adam Langley614c66a2015-06-12 15:26:58 -07002393static const struct tls_extension kExtensions[] = {
2394 {
Adam Langley5021b222015-06-12 18:27:58 -07002395 TLSEXT_TYPE_renegotiate,
2396 NULL,
2397 ext_ri_add_clienthello,
2398 ext_ri_parse_serverhello,
2399 ext_ri_parse_clienthello,
2400 ext_ri_add_serverhello,
2401 },
2402 {
Adam Langley614c66a2015-06-12 15:26:58 -07002403 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002404 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002405 ext_sni_add_clienthello,
2406 ext_sni_parse_serverhello,
2407 ext_sni_parse_clienthello,
2408 ext_sni_add_serverhello,
2409 },
Adam Langley0a056712015-07-01 15:03:33 -07002410 {
2411 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002412 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002413 ext_ems_add_clienthello,
2414 ext_ems_parse_serverhello,
2415 ext_ems_parse_clienthello,
2416 ext_ems_add_serverhello,
2417 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002418 {
2419 TLSEXT_TYPE_session_ticket,
2420 NULL,
2421 ext_ticket_add_clienthello,
2422 ext_ticket_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04002423 // Ticket extension client parsing is handled in ssl_session.c
Steven Valdez6b8509a2016-07-12 13:38:32 -04002424 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002425 ext_ticket_add_serverhello,
2426 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002427 {
2428 TLSEXT_TYPE_signature_algorithms,
2429 NULL,
2430 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002431 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002432 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002433 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002434 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002435 {
2436 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002437 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002438 ext_ocsp_add_clienthello,
2439 ext_ocsp_parse_serverhello,
2440 ext_ocsp_parse_clienthello,
2441 ext_ocsp_add_serverhello,
2442 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002443 {
2444 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002445 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002446 ext_npn_add_clienthello,
2447 ext_npn_parse_serverhello,
2448 ext_npn_parse_clienthello,
2449 ext_npn_add_serverhello,
2450 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002451 {
2452 TLSEXT_TYPE_certificate_timestamp,
2453 NULL,
2454 ext_sct_add_clienthello,
2455 ext_sct_parse_serverhello,
2456 ext_sct_parse_clienthello,
2457 ext_sct_add_serverhello,
2458 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002459 {
2460 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002461 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002462 ext_alpn_add_clienthello,
2463 ext_alpn_parse_serverhello,
David Benjaminc11ea9422017-08-29 16:33:21 -04002464 // ALPN is negotiated late in |ssl_negotiate_alpn|.
David Benjamin9ef31f02016-10-31 18:01:13 -04002465 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002466 ext_alpn_add_serverhello,
2467 },
Adam Langley49c7af12015-07-10 14:33:46 -07002468 {
2469 TLSEXT_TYPE_channel_id,
2470 ext_channel_id_init,
2471 ext_channel_id_add_clienthello,
2472 ext_channel_id_parse_serverhello,
2473 ext_channel_id_parse_clienthello,
2474 ext_channel_id_add_serverhello,
2475 },
Adam Langley391250d2015-07-15 19:06:07 -07002476 {
2477 TLSEXT_TYPE_srtp,
2478 ext_srtp_init,
2479 ext_srtp_add_clienthello,
2480 ext_srtp_parse_serverhello,
2481 ext_srtp_parse_clienthello,
2482 ext_srtp_add_serverhello,
2483 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002484 {
2485 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002486 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002487 ext_ec_point_add_clienthello,
2488 ext_ec_point_parse_serverhello,
2489 ext_ec_point_parse_clienthello,
2490 ext_ec_point_add_serverhello,
2491 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002492 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002493 TLSEXT_TYPE_key_share,
2494 NULL,
2495 ext_key_share_add_clienthello,
2496 forbid_parse_serverhello,
2497 ignore_parse_clienthello,
2498 dont_add_serverhello,
2499 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002500 {
Steven Valdeza833c352016-11-01 13:39:36 -04002501 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002502 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002503 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002504 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002505 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002506 dont_add_serverhello,
2507 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002508 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002509 TLSEXT_TYPE_early_data,
2510 NULL,
2511 ext_early_data_add_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002512 ext_early_data_parse_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002513 ext_early_data_parse_clienthello,
Steven Valdez2d850622017-01-11 11:34:52 -05002514 ext_early_data_add_serverhello,
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002515 },
2516 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002517 TLSEXT_TYPE_supported_versions,
2518 NULL,
2519 ext_supported_versions_add_clienthello,
2520 forbid_parse_serverhello,
2521 ignore_parse_clienthello,
2522 dont_add_serverhello,
2523 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002524 {
2525 TLSEXT_TYPE_cookie,
2526 NULL,
2527 ext_cookie_add_clienthello,
2528 forbid_parse_serverhello,
2529 ignore_parse_clienthello,
2530 dont_add_serverhello,
2531 },
David Benjaminc11ea9422017-08-29 16:33:21 -04002532 // The final extension must be non-empty. WebSphere Application Server 7.0 is
2533 // intolerant to the last extension being zero-length. See
2534 // https://crbug.com/363583.
Adam Langley273d49c2015-07-20 16:38:52 -07002535 {
Steven Valdezce902a92016-05-17 11:47:53 -04002536 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002537 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002538 ext_supported_groups_add_clienthello,
2539 ext_supported_groups_parse_serverhello,
2540 ext_supported_groups_parse_clienthello,
David Benjamin31640932017-10-11 13:22:39 -04002541 dont_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002542 },
Adam Langley614c66a2015-06-12 15:26:58 -07002543};
2544
2545#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2546
David Benjamina3d76d02017-07-14 19:36:07 -04002547static_assert(kNumExtensions <=
2548 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
2549 "too many extensions for sent bitset");
2550static_assert(kNumExtensions <=
2551 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2552 "too many extensions for received bitset");
Adam Langley4cfa96b2015-07-01 11:56:55 -07002553
Adam Langley614c66a2015-06-12 15:26:58 -07002554static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2555 uint16_t value) {
2556 unsigned i;
2557 for (i = 0; i < kNumExtensions; i++) {
2558 if (kExtensions[i].value == value) {
2559 *out_index = i;
2560 return &kExtensions[i];
2561 }
2562 }
2563
2564 return NULL;
2565}
2566
David Benjamin8c880a22016-12-03 02:20:34 -05002567int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2568 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04002569 // Don't add extensions for SSLv3 unless doing secure renegotiation.
David Benjaminf04c2e92016-12-06 13:35:25 -05002570 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002571 !ssl->s3->send_connection_binding) {
2572 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002573 }
Adam Langley95c29f32014-06-20 12:00:00 -07002574
David Benjamine8d53502015-10-10 14:13:23 -04002575 CBB extensions;
2576 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002577 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2578 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002579 }
Adam Langley95c29f32014-06-20 12:00:00 -07002580
David Benjamin8c880a22016-12-03 02:20:34 -05002581 hs->extensions.sent = 0;
2582 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002583
David Benjamin54091232016-09-05 12:47:25 -04002584 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002585 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002586 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002587 }
2588 }
Adam Langley95c29f32014-06-20 12:00:00 -07002589
David Benjamin65ac9972016-09-02 21:35:25 -04002590 uint16_t grease_ext1 = 0;
2591 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002592 // Add a fake empty extension. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002593 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2594 if (!CBB_add_u16(&extensions, grease_ext1) ||
2595 !CBB_add_u16(&extensions, 0 /* zero length */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002596 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2597 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002598 }
2599 }
2600
David Benjamin54091232016-09-05 12:47:25 -04002601 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002602 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002603 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002604 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002605 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin81678aa2017-07-12 22:43:42 -04002606 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002607 }
Adam Langley95c29f32014-06-20 12:00:00 -07002608
Adam Langley33ad2b52015-07-20 17:43:53 -07002609 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002610 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002611 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002612 }
Adam Langley75712922014-10-10 16:23:43 -07002613
David Benjamin2bd19172016-11-17 16:47:15 +09002614 if (!custom_ext_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002615 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2616 return 0;
Adam Langley09505632015-07-30 18:10:13 -07002617 }
2618
David Benjamin65ac9972016-09-02 21:35:25 -04002619 if (ssl->ctx->grease_enabled) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002620 // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -04002621 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2622
David Benjaminc11ea9422017-08-29 16:33:21 -04002623 // The two fake extensions must not have the same value. GREASE values are
2624 // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2625 // one.
David Benjamin65ac9972016-09-02 21:35:25 -04002626 if (grease_ext1 == grease_ext2) {
2627 grease_ext2 ^= 0x1010;
2628 }
2629
2630 if (!CBB_add_u16(&extensions, grease_ext2) ||
2631 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2632 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002633 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2634 return 0;
David Benjamin65ac9972016-09-02 21:35:25 -04002635 }
2636 }
2637
David Benjamince079fd2016-08-02 16:22:34 -04002638 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002639 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002640 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002641 if (header_len > 0xff && header_len < 0x200) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002642 // Add padding to workaround bugs in F5 terminators. See RFC 7685.
2643 //
2644 // NB: because this code works out the length of all existing extensions
2645 // it MUST always appear last.
David Benjamin0a968592015-07-21 22:06:19 -04002646 size_t padding_len = 0x200 - header_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04002647 // Extensions take at least four bytes to encode. Always include at least
2648 // one byte of data if including the extension. WebSphere Application
2649 // Server 7.0 is intolerant to the last extension being zero-length. See
2650 // https://crbug.com/363583.
Adam Langleyfcf25832014-12-18 17:42:32 -08002651 if (padding_len >= 4 + 1) {
2652 padding_len -= 4;
2653 } else {
2654 padding_len = 1;
2655 }
Adam Langley95c29f32014-06-20 12:00:00 -07002656
Adam Langley33ad2b52015-07-20 17:43:53 -07002657 uint8_t *padding_bytes;
2658 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2659 !CBB_add_u16(&extensions, padding_len) ||
2660 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002661 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2662 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002663 }
Adam Langley75712922014-10-10 16:23:43 -07002664
David Benjamin17cf2cb2016-12-13 01:07:13 -05002665 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002666 }
2667 }
Adam Langley75712922014-10-10 16:23:43 -07002668
David Benjaminc11ea9422017-08-29 16:33:21 -04002669 // The PSK extension must be last, including after the padding.
David Benjamin8c880a22016-12-03 02:20:34 -05002670 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
David Benjamin81678aa2017-07-12 22:43:42 -04002671 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2672 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -04002673 }
2674
David Benjaminc11ea9422017-08-29 16:33:21 -04002675 // Discard empty extensions blocks.
David Benjamina01deee2015-12-08 18:56:31 -05002676 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002677 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002678 }
2679
David Benjamine8d53502015-10-10 14:13:23 -04002680 return CBB_flush(out);
Adam Langleyfcf25832014-12-18 17:42:32 -08002681}
Adam Langley95c29f32014-06-20 12:00:00 -07002682
David Benjamin8c880a22016-12-03 02:20:34 -05002683int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2684 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002685 CBB extensions;
2686 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002687 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002688 }
2689
David Benjamin8c880a22016-12-03 02:20:34 -05002690 for (unsigned i = 0; i < kNumExtensions; i++) {
2691 if (!(hs->extensions.received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002692 // Don't send extensions that were not received.
Adam Langley614c66a2015-06-12 15:26:58 -07002693 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002694 }
Adam Langley95c29f32014-06-20 12:00:00 -07002695
David Benjamin8c880a22016-12-03 02:20:34 -05002696 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002697 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002698 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002699 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002700 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002701 }
Adam Langley95c29f32014-06-20 12:00:00 -07002702
David Benjamin2bd19172016-11-17 16:47:15 +09002703 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002704 goto err;
2705 }
2706
David Benjaminc11ea9422017-08-29 16:33:21 -04002707 // Discard empty extensions blocks before TLS 1.3.
David Benjamind1e3ce12017-10-06 18:31:15 -04002708 if (ssl_protocol_version(ssl) < TLS1_3_VERSION &&
Steven Valdez143e8b32016-07-11 13:19:03 -04002709 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002710 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002711 }
2712
David Benjamin56380462015-10-10 14:59:09 -04002713 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002714
2715err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002716 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002717 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002718}
Adam Langley95c29f32014-06-20 12:00:00 -07002719
David Benjamin731058e2016-12-03 23:15:13 -05002720static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2721 const SSL_CLIENT_HELLO *client_hello,
2722 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002723 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002724 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002725 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002726 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002727 }
2728 }
2729
David Benjamin8c880a22016-12-03 02:20:34 -05002730 hs->extensions.received = 0;
2731 hs->custom_extensions.received = 0;
David Benjamine14ff062016-08-09 16:21:24 -04002732 CBS extensions;
2733 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2734 while (CBS_len(&extensions) != 0) {
2735 uint16_t type;
2736 CBS extension;
2737
David Benjaminc11ea9422017-08-29 16:33:21 -04002738 // Decode the next extension.
David Benjamine14ff062016-08-09 16:21:24 -04002739 if (!CBS_get_u16(&extensions, &type) ||
2740 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002741 *out_alert = SSL_AD_DECODE_ERROR;
2742 return 0;
2743 }
Adam Langley95c29f32014-06-20 12:00:00 -07002744
David Benjaminc11ea9422017-08-29 16:33:21 -04002745 // RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2746 // ambiguous. Ignore all but the renegotiation_info extension.
David Benjamine14ff062016-08-09 16:21:24 -04002747 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2748 continue;
2749 }
Adam Langley95c29f32014-06-20 12:00:00 -07002750
David Benjamine14ff062016-08-09 16:21:24 -04002751 unsigned ext_index;
2752 const struct tls_extension *const ext =
2753 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002754
David Benjamine14ff062016-08-09 16:21:24 -04002755 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002756 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002757 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002758 return 0;
2759 }
David Benjamine14ff062016-08-09 16:21:24 -04002760 continue;
2761 }
2762
David Benjamin8c880a22016-12-03 02:20:34 -05002763 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002764 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002765 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002766 *out_alert = alert;
2767 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002768 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002769 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002770 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002771 }
Adam Langley75712922014-10-10 16:23:43 -07002772
David Benjamin1deb41b2016-08-09 19:36:38 -04002773 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002774 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002775 continue;
2776 }
2777
2778 CBS *contents = NULL, fake_contents;
2779 static const uint8_t kFakeRenegotiateExtension[] = {0};
2780 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2781 ssl_client_cipher_list_contains_cipher(client_hello,
2782 SSL3_CK_SCSV & 0xffff)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002783 // The renegotiation SCSV was received so pretend that we received a
2784 // renegotiation extension.
David Benjamin1deb41b2016-08-09 19:36:38 -04002785 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2786 sizeof(kFakeRenegotiateExtension));
2787 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002788 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002789 }
2790
David Benjaminc11ea9422017-08-29 16:33:21 -04002791 // Extension wasn't observed so call the callback with a NULL
2792 // parameter.
David Benjamin1deb41b2016-08-09 19:36:38 -04002793 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002794 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002795 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002796 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002797 *out_alert = alert;
2798 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002799 }
2800 }
2801
Adam Langleyfcf25832014-12-18 17:42:32 -08002802 return 1;
2803}
Adam Langley95c29f32014-06-20 12:00:00 -07002804
David Benjamin731058e2016-12-03 23:15:13 -05002805int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2806 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002807 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002808 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002809 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamind1e3ce12017-10-06 18:31:15 -04002810 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002811 return 0;
2812 }
Adam Langley95c29f32014-06-20 12:00:00 -07002813
David Benjamin8c880a22016-12-03 02:20:34 -05002814 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002815 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002816 return 0;
2817 }
Adam Langley95c29f32014-06-20 12:00:00 -07002818
Adam Langleyfcf25832014-12-18 17:42:32 -08002819 return 1;
2820}
Adam Langley95c29f32014-06-20 12:00:00 -07002821
David Benjamin8c880a22016-12-03 02:20:34 -05002822static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2823 int *out_alert) {
2824 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04002825 // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty.
David Benjamind1e3ce12017-10-06 18:31:15 -04002826 if (CBS_len(cbs) == 0 && ssl_protocol_version(ssl) < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002827 return 1;
2828 }
Adam Langley614c66a2015-06-12 15:26:58 -07002829
David Benjaminc11ea9422017-08-29 16:33:21 -04002830 // Decode the extensions block and check it is valid.
Steven Valdez143e8b32016-07-11 13:19:03 -04002831 CBS extensions;
2832 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2833 !tls1_check_duplicate_extensions(&extensions)) {
2834 *out_alert = SSL_AD_DECODE_ERROR;
2835 return 0;
2836 }
2837
2838 uint32_t received = 0;
2839 while (CBS_len(&extensions) != 0) {
2840 uint16_t type;
2841 CBS extension;
2842
David Benjaminc11ea9422017-08-29 16:33:21 -04002843 // Decode the next extension.
Steven Valdez143e8b32016-07-11 13:19:03 -04002844 if (!CBS_get_u16(&extensions, &type) ||
2845 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002846 *out_alert = SSL_AD_DECODE_ERROR;
2847 return 0;
2848 }
Adam Langley95c29f32014-06-20 12:00:00 -07002849
Steven Valdez143e8b32016-07-11 13:19:03 -04002850 unsigned ext_index;
2851 const struct tls_extension *const ext =
2852 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002853
Steven Valdez143e8b32016-07-11 13:19:03 -04002854 if (ext == NULL) {
David Benjaminfd45ee72017-08-31 14:49:09 -04002855 hs->received_custom_extension = true;
David Benjamin2bd19172016-11-17 16:47:15 +09002856 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002857 return 0;
2858 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002859 continue;
2860 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002861
David Benjamina3d76d02017-07-14 19:36:07 -04002862 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
2863 "too many bits");
David Benjamin5db7c9b2017-01-24 16:17:03 -05002864
David Benjamin8c880a22016-12-03 02:20:34 -05002865 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002866 type != TLSEXT_TYPE_renegotiate) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002867 // If the extension was never sent then it is illegal, except for the
2868 // renegotiation extension which, in SSL 3.0, is signaled via SCSV.
Steven Valdez143e8b32016-07-11 13:19:03 -04002869 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2870 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002871 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002872 return 0;
2873 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002874
Steven Valdez143e8b32016-07-11 13:19:03 -04002875 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002876
Steven Valdez143e8b32016-07-11 13:19:03 -04002877 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002878 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002879 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002880 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04002881 *out_alert = alert;
2882 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002883 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002884 }
Adam Langley95c29f32014-06-20 12:00:00 -07002885
David Benjamin54091232016-09-05 12:47:25 -04002886 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002887 if (!(received & (1u << i))) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002888 // Extension wasn't observed so call the callback with a NULL
2889 // parameter.
Adam Langley614c66a2015-06-12 15:26:58 -07002890 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002891 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002892 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002893 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002894 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002895 return 0;
2896 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002897 }
2898 }
Adam Langley95c29f32014-06-20 12:00:00 -07002899
Adam Langleyfcf25832014-12-18 17:42:32 -08002900 return 1;
2901}
Adam Langley95c29f32014-06-20 12:00:00 -07002902
David Benjamin8c880a22016-12-03 02:20:34 -05002903static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
2904 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002905 int ret = SSL_TLSEXT_ERR_NOACK;
2906 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002907
David Benjamin78f8aab2016-03-10 16:33:58 -05002908 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002909 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002910 ssl->ctx->tlsext_servername_arg);
David Benjaminbe497062017-03-10 16:08:36 -05002911 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
2912 ret = ssl->session_ctx->tlsext_servername_callback(
2913 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002914 }
Adam Langley95c29f32014-06-20 12:00:00 -07002915
Adam Langleyfcf25832014-12-18 17:42:32 -08002916 switch (ret) {
2917 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamind1e3ce12017-10-06 18:31:15 -04002918 ssl_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002919 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002920
Adam Langleyfcf25832014-12-18 17:42:32 -08002921 case SSL_TLSEXT_ERR_NOACK:
David Benjaminfd45ee72017-08-31 14:49:09 -04002922 hs->should_ack_sni = false;
Adam Langleyfcf25832014-12-18 17:42:32 -08002923 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002924
Adam Langleyfcf25832014-12-18 17:42:32 -08002925 default:
2926 return 1;
2927 }
2928}
Adam Langleyed8270a2014-09-02 13:52:56 -07002929
David Benjamin8c880a22016-12-03 02:20:34 -05002930int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
2931 SSL *const ssl = hs->ssl;
Adam Langleyc68e5b92017-02-08 13:33:15 -08002932 int alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002933 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamind1e3ce12017-10-06 18:31:15 -04002934 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002935 return 0;
2936 }
2937
Adam Langleyfcf25832014-12-18 17:42:32 -08002938 return 1;
2939}
Adam Langley95c29f32014-06-20 12:00:00 -07002940
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002941static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
2942 uint8_t **out, size_t *out_len, EVP_CIPHER_CTX *cipher_ctx,
2943 HMAC_CTX *hmac_ctx, const uint8_t *ticket, size_t ticket_len) {
2944 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002945
David Benjaminc11ea9422017-08-29 16:33:21 -04002946 // Check the MAC at the end of the ticket.
David Benjamine3aa1d92015-06-16 15:34:50 -04002947 uint8_t mac[EVP_MAX_MD_SIZE];
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002948 size_t mac_len = HMAC_size(hmac_ctx);
David Benjamine3aa1d92015-06-16 15:34:50 -04002949 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminc11ea9422017-08-29 16:33:21 -04002950 // The ticket must be large enough for key name, IV, data, and MAC.
David Benjamin81678aa2017-07-12 22:43:42 -04002951 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08002952 }
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002953 HMAC_Update(hmac_ctx, ticket, ticket_len - mac_len);
2954 HMAC_Final(hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04002955 int mac_ok =
2956 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
2957#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
2958 mac_ok = 1;
2959#endif
2960 if (!mac_ok) {
David Benjamin81678aa2017-07-12 22:43:42 -04002961 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08002962 }
2963
David Benjaminc11ea9422017-08-29 16:33:21 -04002964 // Decrypt the session data.
David Benjamine3aa1d92015-06-16 15:34:50 -04002965 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2966 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2967 mac_len;
David Benjamin86e95b82017-07-18 16:34:25 -04002968 UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len));
David Benjamin81678aa2017-07-12 22:43:42 -04002969 if (!plaintext) {
2970 return ssl_ticket_aead_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08002971 }
David Benjaminfbc45d72016-09-22 01:21:24 -04002972 size_t plaintext_len;
2973#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin81678aa2017-07-12 22:43:42 -04002974 OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04002975 plaintext_len = ciphertext_len;
2976#else
David Benjamine3aa1d92015-06-16 15:34:50 -04002977 if (ciphertext_len >= INT_MAX) {
David Benjamin81678aa2017-07-12 22:43:42 -04002978 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08002979 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002980 int len1, len2;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002981 if (!EVP_DecryptUpdate(cipher_ctx, plaintext.get(), &len1, ciphertext,
David Benjamine3aa1d92015-06-16 15:34:50 -04002982 (int)ciphertext_len) ||
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002983 !EVP_DecryptFinal_ex(cipher_ctx, plaintext.get() + len1, &len2)) {
Adam Langley4c341d02017-03-08 19:33:21 -08002984 ERR_clear_error();
David Benjamin81678aa2017-07-12 22:43:42 -04002985 return ssl_ticket_aead_ignore_ticket;
Adam Langleyfcf25832014-12-18 17:42:32 -08002986 }
Adam Langley4c341d02017-03-08 19:33:21 -08002987 plaintext_len = (size_t)(len1) + len2;
David Benjaminfbc45d72016-09-22 01:21:24 -04002988#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08002989
David Benjamin81678aa2017-07-12 22:43:42 -04002990 *out = plaintext.release();
Adam Langley4c341d02017-03-08 19:33:21 -08002991 *out_len = plaintext_len;
David Benjamin81678aa2017-07-12 22:43:42 -04002992 return ssl_ticket_aead_success;
Adam Langley4c341d02017-03-08 19:33:21 -08002993}
2994
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002995static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
David Benjaminfd45ee72017-08-31 14:49:09 -04002996 SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
Martin Kreichgauer72912d22017-08-04 12:06:43 -07002997 const uint8_t *ticket, size_t ticket_len) {
2998 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
2999 ScopedEVP_CIPHER_CTX cipher_ctx;
3000 ScopedHMAC_CTX hmac_ctx;
3001 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3002 int cb_ret = ssl->session_ctx->tlsext_ticket_key_cb(
3003 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(),
3004 hmac_ctx.get(), 0 /* decrypt */);
3005 if (cb_ret < 0) {
3006 return ssl_ticket_aead_error;
3007 } else if (cb_ret == 0) {
3008 return ssl_ticket_aead_ignore_ticket;
3009 } else if (cb_ret == 2) {
David Benjaminfd45ee72017-08-31 14:49:09 -04003010 *out_renew_ticket = true;
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003011 } else {
3012 assert(cb_ret == 1);
3013 }
3014 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3015 hmac_ctx.get(), ticket, ticket_len);
3016}
3017
3018static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
3019 SSL *ssl, uint8_t **out, size_t *out_len, const uint8_t *ticket,
3020 size_t ticket_len) {
3021 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3022 SSL_CTX *ctx = ssl->session_ctx;
3023
David Benjaminc11ea9422017-08-29 16:33:21 -04003024 // Rotate the ticket key if necessary.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003025 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
3026 return ssl_ticket_aead_error;
3027 }
3028
David Benjaminc11ea9422017-08-29 16:33:21 -04003029 // Pick the matching ticket key and decrypt.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003030 ScopedEVP_CIPHER_CTX cipher_ctx;
3031 ScopedHMAC_CTX hmac_ctx;
3032 {
3033 MutexReadLock lock(&ctx->lock);
3034 const tlsext_ticket_key *key;
3035 if (ctx->tlsext_ticket_key_current &&
3036 !OPENSSL_memcmp(ctx->tlsext_ticket_key_current->name, ticket,
3037 SSL_TICKET_KEY_NAME_LEN)) {
3038 key = ctx->tlsext_ticket_key_current;
3039 } else if (ctx->tlsext_ticket_key_prev &&
3040 !OPENSSL_memcmp(ctx->tlsext_ticket_key_prev->name, ticket,
3041 SSL_TICKET_KEY_NAME_LEN)) {
3042 key = ctx->tlsext_ticket_key_prev;
3043 } else {
3044 return ssl_ticket_aead_ignore_ticket;
3045 }
3046 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3047 if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
3048 tlsext_tick_md(), NULL) ||
3049 !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL,
3050 key->aes_key, iv)) {
3051 return ssl_ticket_aead_error;
3052 }
3053 }
3054 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3055 hmac_ctx.get(), ticket, ticket_len);
3056}
3057
Adam Langley4c341d02017-03-08 19:33:21 -08003058static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
David Benjaminfd45ee72017-08-31 14:49:09 -04003059 SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
Adam Langley4c341d02017-03-08 19:33:21 -08003060 const uint8_t *ticket, size_t ticket_len) {
David Benjamin81678aa2017-07-12 22:43:42 -04003061 uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len);
Adam Langley4c341d02017-03-08 19:33:21 -08003062 if (plaintext == NULL) {
3063 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3064 return ssl_ticket_aead_error;
3065 }
3066
3067 size_t plaintext_len;
3068 const enum ssl_ticket_aead_result_t result =
3069 ssl->session_ctx->ticket_aead_method->open(
3070 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3071
3072 if (result == ssl_ticket_aead_success) {
3073 *out = plaintext;
3074 plaintext = NULL;
3075 *out_len = plaintext_len;
3076 }
3077
3078 OPENSSL_free(plaintext);
3079 return result;
3080}
3081
3082enum ssl_ticket_aead_result_t ssl_process_ticket(
David Benjaminfd45ee72017-08-31 14:49:09 -04003083 SSL *ssl, UniquePtr<SSL_SESSION> *out_session, bool *out_renew_ticket,
Adam Langley4c341d02017-03-08 19:33:21 -08003084 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3085 size_t session_id_len) {
David Benjaminfd45ee72017-08-31 14:49:09 -04003086 *out_renew_ticket = false;
David Benjamin37af90f2017-07-29 01:42:16 -04003087 out_session->reset();
Adam Langley4c341d02017-03-08 19:33:21 -08003088
3089 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3090 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3091 return ssl_ticket_aead_ignore_ticket;
3092 }
3093
3094 uint8_t *plaintext = NULL;
3095 size_t plaintext_len;
3096 enum ssl_ticket_aead_result_t result;
3097 if (ssl->session_ctx->ticket_aead_method != NULL) {
3098 result = ssl_decrypt_ticket_with_method(
3099 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3100 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04003101 // Ensure there is room for the key name and the largest IV
3102 // |tlsext_ticket_key_cb| may try to consume. The real limit may be lower,
3103 // but the maximum IV length should be well under the minimum size for the
3104 // session material and HMAC.
Martin Kreichgauer72912d22017-08-04 12:06:43 -07003105 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3106 return ssl_ticket_aead_ignore_ticket;
3107 }
3108 if (ssl->session_ctx->tlsext_ticket_key_cb != NULL) {
3109 result = ssl_decrypt_ticket_with_cb(ssl, &plaintext, &plaintext_len,
3110 out_renew_ticket, ticket, ticket_len);
3111 } else {
3112 result = ssl_decrypt_ticket_with_ticket_keys(
3113 ssl, &plaintext, &plaintext_len, ticket, ticket_len);
3114 }
Adam Langley4c341d02017-03-08 19:33:21 -08003115 }
3116
3117 if (result != ssl_ticket_aead_success) {
3118 return result;
3119 }
3120
David Benjaminc11ea9422017-08-29 16:33:21 -04003121 // Decode the session.
David Benjamin37af90f2017-07-29 01:42:16 -04003122 UniquePtr<SSL_SESSION> session(
3123 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx));
Adam Langley4c341d02017-03-08 19:33:21 -08003124 OPENSSL_free(plaintext);
3125
David Benjamin37af90f2017-07-29 01:42:16 -04003126 if (!session) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003127 ERR_clear_error(); // Don't leave an error on the queue.
Adam Langley4c341d02017-03-08 19:33:21 -08003128 return ssl_ticket_aead_ignore_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -04003129 }
3130
David Benjaminc11ea9422017-08-29 16:33:21 -04003131 // Copy the client's session ID into the new session, to denote the ticket has
3132 // been accepted.
David Benjamin17cf2cb2016-12-13 01:07:13 -05003133 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003134 session->session_id_length = session_id_len;
3135
David Benjamin37af90f2017-07-29 01:42:16 -04003136 *out_session = std::move(session);
Adam Langley4c341d02017-03-08 19:33:21 -08003137 return ssl_ticket_aead_success;
Adam Langleyfcf25832014-12-18 17:42:32 -08003138}
Adam Langley95c29f32014-06-20 12:00:00 -07003139
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003140int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003141 // Extension ignored for inappropriate versions
David Benjamind1e3ce12017-10-06 18:31:15 -04003142 if (ssl_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003143 return 1;
3144 }
David Benjamincd996942014-07-20 16:23:51 -04003145
David Benjaminb1cf48e2017-09-21 11:37:46 -04003146 return parse_u16_array(in_sigalgs, &hs->peer_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08003147}
David Benjaminec2f27d2014-11-13 19:17:25 -05003148
David Benjamina3651382017-04-20 17:49:36 -04003149int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
3150 switch (EVP_PKEY_id(pkey)) {
3151 case EVP_PKEY_RSA:
3152 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3153 return 1;
3154 case EVP_PKEY_EC:
3155 *out = SSL_SIGN_ECDSA_SHA1;
3156 return 1;
3157 default:
3158 return 0;
3159 }
3160}
3161
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003162int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3163 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003164 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003165
David Benjaminc11ea9422017-08-29 16:33:21 -04003166 // Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3167 // handshake.
David Benjamind1e3ce12017-10-06 18:31:15 -04003168 if (ssl_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin31b0c9b2017-07-20 14:49:15 -04003169 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
David Benjamina3651382017-04-20 17:49:36 -04003170 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3171 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003172 }
David Benjamina3651382017-04-20 17:49:36 -04003173 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003174 }
3175
David Benjaminb1cf48e2017-09-21 11:37:46 -04003176 Span<const uint16_t> sigalgs = kSignSignatureAlgorithms;
3177 if (cert->sigalgs != nullptr) {
3178 sigalgs = MakeConstSpan(cert->sigalgs, cert->num_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003179 }
3180
David Benjaminb1cf48e2017-09-21 11:37:46 -04003181 Span<const uint16_t> peer_sigalgs = hs->peer_sigalgs;
David Benjamind1e3ce12017-10-06 18:31:15 -04003182 if (peer_sigalgs.empty() && ssl_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003183 // If the client didn't specify any signature_algorithms extension then
3184 // we can assume that it supports SHA1. See
3185 // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
David Benjaminea9a0d52016-07-08 15:52:59 -07003186 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3187 SSL_SIGN_ECDSA_SHA1};
3188 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjaminea9a0d52016-07-08 15:52:59 -07003189 }
3190
David Benjaminb1cf48e2017-09-21 11:37:46 -04003191 for (uint16_t sigalg : sigalgs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003192 // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3193 // negotiated.
David Benjamin1fb125c2016-07-08 18:52:12 -07003194 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
David Benjaminb1cf48e2017-09-21 11:37:46 -04003195 !ssl_private_key_supports_signature_algorithm(hs, sigalg)) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003196 continue;
3197 }
3198
David Benjaminb1cf48e2017-09-21 11:37:46 -04003199 for (uint16_t peer_sigalg : peer_sigalgs) {
3200 if (sigalg == peer_sigalg) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003201 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003202 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003203 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003204 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003205 }
Adam Langley95c29f32014-06-20 12:00:00 -07003206
David Benjaminea9a0d52016-07-08 15:52:59 -07003207 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3208 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003209}
Adam Langley95c29f32014-06-20 12:00:00 -07003210
David Benjamin7934f082017-08-01 16:32:25 -04003211int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
Steven Valdez908ac192017-01-12 13:17:07 -05003212 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003213 // A Channel ID handshake message is structured to contain multiple
3214 // extensions, but the only one that can be present is Channel ID.
David Benjamin7934f082017-08-01 16:32:25 -04003215 uint16_t extension_type;
3216 CBS channel_id = msg.body, extension;
Nick Harper60a85cb2016-09-23 16:25:11 -07003217 if (!CBS_get_u16(&channel_id, &extension_type) ||
3218 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3219 CBS_len(&channel_id) != 0 ||
3220 extension_type != TLSEXT_TYPE_channel_id ||
3221 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3222 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04003223 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Nick Harper60a85cb2016-09-23 16:25:11 -07003224 return 0;
3225 }
3226
David Benjamin86e95b82017-07-18 16:34:25 -04003227 UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Nick Harper60a85cb2016-09-23 16:25:11 -07003228 if (!p256) {
3229 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3230 return 0;
3231 }
3232
David Benjamin86e95b82017-07-18 16:34:25 -04003233 UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3234 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
David Benjamin81678aa2017-07-12 22:43:42 -04003235 if (!sig || !x || !y) {
3236 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003237 }
3238
3239 const uint8_t *p = CBS_data(&extension);
David Benjamin81678aa2017-07-12 22:43:42 -04003240 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3241 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3242 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3243 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
3244 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003245 }
3246
David Benjamin86e95b82017-07-18 16:34:25 -04003247 UniquePtr<EC_KEY> key(EC_KEY_new());
3248 UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
David Benjamin81678aa2017-07-12 22:43:42 -04003249 if (!key || !point ||
3250 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3251 y.get(), nullptr) ||
3252 !EC_KEY_set_group(key.get(), p256.get()) ||
3253 !EC_KEY_set_public_key(key.get(), point.get())) {
3254 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003255 }
3256
3257 uint8_t digest[EVP_MAX_MD_SIZE];
3258 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003259 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjamin81678aa2017-07-12 22:43:42 -04003260 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003261 }
3262
David Benjamin81678aa2017-07-12 22:43:42 -04003263 int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
Nick Harper60a85cb2016-09-23 16:25:11 -07003264#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3265 sig_ok = 1;
3266#endif
3267 if (!sig_ok) {
3268 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
David Benjamind1e3ce12017-10-06 18:31:15 -04003269 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
David Benjamin046bc1f2017-08-31 15:06:42 -04003270 ssl->s3->tlsext_channel_id_valid = false;
David Benjamin81678aa2017-07-12 22:43:42 -04003271 return 0;
Nick Harper60a85cb2016-09-23 16:25:11 -07003272 }
3273
David Benjamin17cf2cb2016-12-13 01:07:13 -05003274 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
David Benjamin81678aa2017-07-12 22:43:42 -04003275 return 1;
Nick Harper60a85cb2016-09-23 16:25:11 -07003276}
3277
David Benjaminf1db1a32017-10-27 01:12:34 -04003278bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
Steven Valdez908ac192017-01-12 13:17:07 -05003279 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07003280 uint8_t digest[EVP_MAX_MD_SIZE];
3281 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05003282 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
David Benjaminf1db1a32017-10-27 01:12:34 -04003283 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003284 }
3285
3286 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
David Benjaminf1db1a32017-10-27 01:12:34 -04003287 if (ec_key == nullptr) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003288 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminf1db1a32017-10-27 01:12:34 -04003289 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003290 }
3291
David Benjaminf1db1a32017-10-27 01:12:34 -04003292 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
3293 if (!x || !y ||
Nick Harper60a85cb2016-09-23 16:25:11 -07003294 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3295 EC_KEY_get0_public_key(ec_key),
David Benjaminf1db1a32017-10-27 01:12:34 -04003296 x.get(), y.get(), nullptr)) {
3297 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003298 }
3299
David Benjaminf1db1a32017-10-27 01:12:34 -04003300 UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign(digest, digest_len, ec_key));
3301 if (!sig) {
3302 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003303 }
3304
3305 CBB child;
3306 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3307 !CBB_add_u16_length_prefixed(cbb, &child) ||
David Benjaminf1db1a32017-10-27 01:12:34 -04003308 !BN_bn2cbb_padded(&child, 32, x.get()) ||
3309 !BN_bn2cbb_padded(&child, 32, y.get()) ||
Nick Harper60a85cb2016-09-23 16:25:11 -07003310 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3311 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3312 !CBB_flush(cbb)) {
David Benjaminf1db1a32017-10-27 01:12:34 -04003313 return false;
Nick Harper60a85cb2016-09-23 16:25:11 -07003314 }
3315
David Benjaminf1db1a32017-10-27 01:12:34 -04003316 return true;
Nick Harper60a85cb2016-09-23 16:25:11 -07003317}
3318
Steven Valdez908ac192017-01-12 13:17:07 -05003319int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3320 SSL *const ssl = hs->ssl;
David Benjamind1e3ce12017-10-06 18:31:15 -04003321 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamin75a1f232017-10-11 17:19:19 -04003322 Array<uint8_t> msg;
3323 if (!tls13_get_cert_verify_signature_input(hs, &msg,
Nick Harper60a85cb2016-09-23 16:25:11 -07003324 ssl_cert_verify_channel_id)) {
3325 return 0;
3326 }
David Benjamin75a1f232017-10-11 17:19:19 -04003327 SHA256(msg.data(), msg.size(), out);
Nick Harper60a85cb2016-09-23 16:25:11 -07003328 *out_len = SHA256_DIGEST_LENGTH;
Nick Harper60a85cb2016-09-23 16:25:11 -07003329 return 1;
3330 }
3331
Nick Harper95594012016-10-20 14:07:13 -07003332 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003333
Nick Harper95594012016-10-20 14:07:13 -07003334 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003335 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003336 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003337
Steven Valdez87eab492016-06-27 16:34:59 -04003338 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003339 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003340 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003341 if (ssl->session->original_handshake_hash_len == 0) {
3342 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003343 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003344 }
Nick Harper95594012016-10-20 14:07:13 -07003345 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3346 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003347 }
3348
Steven Valdez908ac192017-01-12 13:17:07 -05003349 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3350 size_t hs_hash_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003351 if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
Nick Harper95594012016-10-20 14:07:13 -07003352 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003353 }
Steven Valdez908ac192017-01-12 13:17:07 -05003354 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Nick Harper95594012016-10-20 14:07:13 -07003355 SHA256_Final(out, &ctx);
3356 *out_len = SHA256_DIGEST_LENGTH;
3357 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003358}
Adam Langley1258b6a2014-06-20 12:00:00 -07003359
David Benjaminc11ea9422017-08-29 16:33:21 -04003360// tls1_record_handshake_hashes_for_channel_id records the current handshake
3361// hashes in |hs->new_session| so that Channel ID resumptions can sign that
3362// data.
Steven Valdez908ac192017-01-12 13:17:07 -05003363int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3364 SSL *const ssl = hs->ssl;
David Benjaminc11ea9422017-08-29 16:33:21 -04003365 // This function should never be called for a resumed session because the
3366 // handshake hashes that we wish to record are for the original, full
3367 // handshake.
Steven Valdez87eab492016-06-27 16:34:59 -04003368 if (ssl->session != NULL) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04003369 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003370 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003371
David Benjamina3d76d02017-07-14 19:36:07 -04003372 static_assert(
David Benjamin45738dd2017-02-09 20:01:26 -05003373 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
David Benjamina3d76d02017-07-14 19:36:07 -04003374 "original_handshake_hash is too small");
Steven Valdez908ac192017-01-12 13:17:07 -05003375
3376 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04003377 if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
3378 &digest_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04003379 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003380 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003381
David Benjamina3d76d02017-07-14 19:36:07 -04003382 static_assert(EVP_MAX_MD_SIZE <= 0xff,
3383 "EVP_MAX_MD_SIZE does not fit in uint8_t");
David Benjamin45738dd2017-02-09 20:01:26 -05003384 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003385
Adam Langleyfcf25832014-12-18 17:42:32 -08003386 return 1;
3387}
Nick Harper60a85cb2016-09-23 16:25:11 -07003388
3389int ssl_do_channel_id_callback(SSL *ssl) {
3390 if (ssl->tlsext_channel_id_private != NULL ||
3391 ssl->ctx->channel_id_cb == NULL) {
3392 return 1;
3393 }
3394
3395 EVP_PKEY *key = NULL;
3396 ssl->ctx->channel_id_cb(ssl, &key);
3397 if (key == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003398 // The caller should try again later.
Nick Harper60a85cb2016-09-23 16:25:11 -07003399 return 1;
3400 }
3401
3402 int ret = SSL_set1_tls_channel_id(ssl, key);
3403 EVP_PKEY_free(key);
3404 return ret;
3405}
Adam Langleycfa08c32016-11-17 13:21:27 -08003406
3407int ssl_is_sct_list_valid(const CBS *contents) {
David Benjaminc11ea9422017-08-29 16:33:21 -04003408 // Shallow parse the SCT list for sanity. By the RFC
3409 // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3410 // of the SCTs may be empty.
Adam Langleycfa08c32016-11-17 13:21:27 -08003411 CBS copy = *contents;
3412 CBS sct_list;
3413 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3414 CBS_len(&copy) != 0 ||
3415 CBS_len(&sct_list) == 0) {
3416 return 0;
3417 }
3418
3419 while (CBS_len(&sct_list) > 0) {
3420 CBS sct;
3421 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3422 CBS_len(&sct) == 0) {
3423 return 0;
3424 }
3425 }
3426
3427 return 1;
3428}
David Benjamin86e95b82017-07-18 16:34:25 -04003429
3430} // namespace bssl
3431
3432using namespace bssl;
3433
3434int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
3435 uint16_t extension_type,
3436 const uint8_t **out_data,
3437 size_t *out_len) {
3438 CBS cbs;
3439 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
3440 return 0;
3441 }
3442
3443 *out_data = CBS_data(&cbs);
3444 *out_len = CBS_len(&cbs);
3445 return 1;
3446}
3447
3448void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
3449 ctx->ed25519_enabled = !!enabled;
3450}
3451
3452int SSL_extension_supported(unsigned extension_value) {
3453 uint32_t index;
3454 return extension_value == TLSEXT_TYPE_padding ||
3455 tls_extension_find(&index, extension_value) != NULL;
3456}