blob: 97c0c4b9ac1c2c253d08d9d1e4f5cde933669b6c [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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.
Robert Sloanab8b8882018-03-26 11:39:51 -07007 *
Adam Langleyd9e397b2015-01-22 14:27:53 -08008 * 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).
Robert Sloanab8b8882018-03-26 11:39:51 -070014 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080015 * 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.
Robert Sloanab8b8882018-03-26 11:39:51 -070021 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080022 * 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 :-).
Robert Sloanab8b8882018-03-26 11:39:51 -070036 * 4. If you include any Windows specific code (or a derivative thereof) from
Adam Langleyd9e397b2015-01-22 14:27:53 -080037 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Robert Sloanab8b8882018-03-26 11:39:51 -070039 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080040 * 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.
Robert Sloanab8b8882018-03-26 11:39:51 -070051 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080052 * 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
Robert Sloanab8b8882018-03-26 11:39:51 -070065 * notice, this list of conditions and the following disclaimer.
Adam Langleyd9e397b2015-01-22 14:27:53 -080066 *
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
Kenny Rootb8494592015-09-25 02:29:14 +0000109#include <openssl/ssl.h>
110
Adam Langleye9ada862015-05-11 17:20:37 -0700111#include <assert.h>
Kenny Rootb8494592015-09-25 02:29:14 +0000112#include <limits.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800113#include <stdlib.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700114#include <string.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800115
Robert Sloan4562e9d2017-10-02 10:26:51 -0700116#include <utility>
117
Adam Langleyd9e397b2015-01-22 14:27:53 -0800118#include <openssl/bytestring.h>
Robert Sloan0db7f542018-01-16 15:48:33 -0800119#include <openssl/chacha.h>
Kenny Rootb8494592015-09-25 02:29:14 +0000120#include <openssl/digest.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700121#include <openssl/err.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800122#include <openssl/evp.h>
123#include <openssl/hmac.h>
124#include <openssl/mem.h>
David Benjamin4969cc92016-04-22 15:02:23 -0400125#include <openssl/nid.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800126#include <openssl/rand.h>
127
Adam Langleye9ada862015-05-11 17:20:37 -0700128#include "internal.h"
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400129#include "../crypto/internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800130
131
Robert Sloanb6d070c2017-07-24 08:40:01 -0700132namespace bssl {
133
David Benjamin1b249672016-12-06 18:25:50 -0500134static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800135
Adam Langleyd9e397b2015-01-22 14:27:53 -0800136static int compare_uint16_t(const void *p1, const void *p2) {
137 uint16_t u1 = *((const uint16_t *)p1);
138 uint16_t u2 = *((const uint16_t *)p2);
139 if (u1 < u2) {
140 return -1;
141 } else if (u1 > u2) {
142 return 1;
143 } else {
144 return 0;
145 }
146}
147
Robert Sloana27a6a42017-09-05 08:39:28 -0700148// Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
149// more than one extension of the same type in a ClientHello or ServerHello.
150// This function does an initial scan over the extensions block to filter those
151// out.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800152static int tls1_check_duplicate_extensions(const CBS *cbs) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700153 // First pass: count the extensions.
Robert Sloan4562e9d2017-10-02 10:26:51 -0700154 size_t num_extensions = 0;
155 CBS extensions = *cbs;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800156 while (CBS_len(&extensions) > 0) {
157 uint16_t type;
158 CBS extension;
159
160 if (!CBS_get_u16(&extensions, &type) ||
161 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Robert Sloan4562e9d2017-10-02 10:26:51 -0700162 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800163 }
164
165 num_extensions++;
166 }
167
168 if (num_extensions == 0) {
169 return 1;
170 }
171
Robert Sloan4562e9d2017-10-02 10:26:51 -0700172 Array<uint16_t> extension_types;
173 if (!extension_types.Init(num_extensions)) {
174 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800175 }
176
Robert Sloana27a6a42017-09-05 08:39:28 -0700177 // Second pass: gather the extension types.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800178 extensions = *cbs;
Robert Sloan4562e9d2017-10-02 10:26:51 -0700179 for (size_t i = 0; i < extension_types.size(); i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800180 CBS extension;
181
182 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
183 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700184 // This should not happen.
Robert Sloan4562e9d2017-10-02 10:26:51 -0700185 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800186 }
187 }
188 assert(CBS_len(&extensions) == 0);
189
Robert Sloana27a6a42017-09-05 08:39:28 -0700190 // Sort the extensions and make sure there are no duplicates.
Robert Sloan4562e9d2017-10-02 10:26:51 -0700191 qsort(extension_types.data(), extension_types.size(), sizeof(uint16_t),
192 compare_uint16_t);
193 for (size_t i = 1; i < num_extensions; i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800194 if (extension_types[i - 1] == extension_types[i]) {
Robert Sloan4562e9d2017-10-02 10:26:51 -0700195 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800196 }
197 }
198
Robert Sloan4562e9d2017-10-02 10:26:51 -0700199 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800200}
201
Robert Sloan84377092017-08-14 09:33:19 -0700202int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out,
203 const SSLMessage &msg) {
Robert Sloan69939df2017-01-09 10:53:07 -0800204 OPENSSL_memset(out, 0, sizeof(*out));
David Benjamin1b249672016-12-06 18:25:50 -0500205 out->ssl = ssl;
Robert Sloan84377092017-08-14 09:33:19 -0700206 out->client_hello = CBS_data(&msg.body);
207 out->client_hello_len = CBS_len(&msg.body);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800208
David Benjaminc895d6b2016-08-11 13:26:41 -0400209 CBS client_hello, random, session_id;
David Benjamin1b249672016-12-06 18:25:50 -0500210 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
211 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400212 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
213 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
214 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800215 return 0;
216 }
217
David Benjamin1b249672016-12-06 18:25:50 -0500218 out->random = CBS_data(&random);
219 out->random_len = CBS_len(&random);
220 out->session_id = CBS_data(&session_id);
221 out->session_id_len = CBS_len(&session_id);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800222
Robert Sloana27a6a42017-09-05 08:39:28 -0700223 // Skip past DTLS cookie
David Benjamin1b249672016-12-06 18:25:50 -0500224 if (SSL_is_dtls(out->ssl)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800225 CBS cookie;
David Benjaminc895d6b2016-08-11 13:26:41 -0400226 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
227 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800228 return 0;
229 }
230 }
231
David Benjaminc895d6b2016-08-11 13:26:41 -0400232 CBS cipher_suites, compression_methods;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800233 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400234 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
235 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -0800236 CBS_len(&compression_methods) < 1) {
237 return 0;
238 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400239
David Benjamin1b249672016-12-06 18:25:50 -0500240 out->cipher_suites = CBS_data(&cipher_suites);
241 out->cipher_suites_len = CBS_len(&cipher_suites);
242 out->compression_methods = CBS_data(&compression_methods);
243 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800244
Robert Sloana27a6a42017-09-05 08:39:28 -0700245 // If the ClientHello ends here then it's valid, but doesn't have any
246 // extensions. (E.g. SSLv3.)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800247 if (CBS_len(&client_hello) == 0) {
David Benjamin1b249672016-12-06 18:25:50 -0500248 out->extensions = NULL;
249 out->extensions_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800250 return 1;
251 }
252
Robert Sloana27a6a42017-09-05 08:39:28 -0700253 // Extract extensions and check it is valid.
David Benjaminc895d6b2016-08-11 13:26:41 -0400254 CBS extensions;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800255 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
256 !tls1_check_duplicate_extensions(&extensions) ||
257 CBS_len(&client_hello) != 0) {
258 return 0;
259 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400260
David Benjamin1b249672016-12-06 18:25:50 -0500261 out->extensions = CBS_data(&extensions);
262 out->extensions_len = CBS_len(&extensions);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800263
264 return 1;
265}
266
David Benjamin1b249672016-12-06 18:25:50 -0500267int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
268 CBS *out, uint16_t extension_type) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800269 CBS extensions;
David Benjamin1b249672016-12-06 18:25:50 -0500270 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800271 while (CBS_len(&extensions) != 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700272 // Decode the next extension.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800273 uint16_t type;
274 CBS extension;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800275 if (!CBS_get_u16(&extensions, &type) ||
276 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
277 return 0;
278 }
279
280 if (type == extension_type) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400281 *out = extension;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800282 return 1;
283 }
284 }
285
286 return 0;
287}
288
David Benjamind316cba2016-06-02 16:17:39 -0400289static const uint16_t kDefaultGroups[] = {
David Benjaminc895d6b2016-08-11 13:26:41 -0400290 SSL_CURVE_X25519,
291 SSL_CURVE_SECP256R1,
292 SSL_CURVE_SECP384R1,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800293};
294
Robert Sloan4562e9d2017-10-02 10:26:51 -0700295Span<const uint16_t> tls1_get_grouplist(const SSL *ssl) {
296 if (ssl->supported_group_list != nullptr) {
297 return MakeConstSpan(ssl->supported_group_list,
298 ssl->supported_group_list_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800299 }
Robert Sloan4562e9d2017-10-02 10:26:51 -0700300 return Span<const uint16_t>(kDefaultGroups);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800301}
302
David Benjamin1b249672016-12-06 18:25:50 -0500303int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
304 SSL *const ssl = hs->ssl;
David Benjamin95add822016-10-19 01:09:12 -0400305 assert(ssl->server);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800306
Robert Sloana27a6a42017-09-05 08:39:28 -0700307 // Clients are not required to send a supported_groups extension. In this
308 // case, the server is free to pick any group it likes. See RFC 4492,
309 // section 4, paragraph 3.
310 //
311 // However, in the interests of compatibility, we will skip ECDH if the
312 // client didn't send an extension because we can't be sure that they'll
313 // support our favoured group. Thus we do not special-case an emtpy
314 // |peer_supported_group_list|.
Adam Langleye9ada862015-05-11 17:20:37 -0700315
Robert Sloan4562e9d2017-10-02 10:26:51 -0700316 Span<const uint16_t> groups = tls1_get_grouplist(ssl);
317 Span<const uint16_t> pref, supp;
Adam Langley4139edb2016-01-13 15:00:54 -0800318 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
David Benjamind316cba2016-06-02 16:17:39 -0400319 pref = groups;
David Benjamin1b249672016-12-06 18:25:50 -0500320 supp = hs->peer_supported_group_list;
Adam Langleye9ada862015-05-11 17:20:37 -0700321 } else {
David Benjamin1b249672016-12-06 18:25:50 -0500322 pref = hs->peer_supported_group_list;
David Benjamind316cba2016-06-02 16:17:39 -0400323 supp = groups;
Adam Langleye9ada862015-05-11 17:20:37 -0700324 }
325
Robert Sloan4562e9d2017-10-02 10:26:51 -0700326 for (uint16_t pref_group : pref) {
327 for (uint16_t supp_group : supp) {
328 if (pref_group == supp_group) {
329 *out_group_id = pref_group;
Adam Langley4139edb2016-01-13 15:00:54 -0800330 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800331 }
332 }
333 }
334
Adam Langley4139edb2016-01-13 15:00:54 -0800335 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800336}
337
David Benjamind316cba2016-06-02 16:17:39 -0400338int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800339 const int *curves, size_t ncurves) {
Robert Sloana12bf462017-07-17 07:08:26 -0700340 uint16_t *group_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
David Benjamind316cba2016-06-02 16:17:39 -0400341 if (group_ids == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800342 return 0;
343 }
344
David Benjamin7c0d06c2016-08-11 13:26:41 -0400345 for (size_t i = 0; i < ncurves; i++) {
David Benjamind316cba2016-06-02 16:17:39 -0400346 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
347 OPENSSL_free(group_ids);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800348 return 0;
349 }
350 }
351
David Benjamind316cba2016-06-02 16:17:39 -0400352 OPENSSL_free(*out_group_ids);
353 *out_group_ids = group_ids;
354 *out_group_ids_len = ncurves;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800355
356 return 1;
357}
358
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400359int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
360 const char *curves) {
361 uint16_t *group_ids = NULL;
362 size_t ncurves = 0;
363
364 const char *col;
365 const char *ptr = curves;
366
367 do {
368 col = strchr(ptr, ':');
369
370 uint16_t group_id;
371 if (!ssl_name_to_group_id(&group_id, ptr,
372 col ? (size_t)(col - ptr) : strlen(ptr))) {
373 goto err;
374 }
375
Robert Sloana12bf462017-07-17 07:08:26 -0700376 uint16_t *new_group_ids = (uint16_t *)OPENSSL_realloc(
377 group_ids, (ncurves + 1) * sizeof(uint16_t));
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400378 if (new_group_ids == NULL) {
379 goto err;
380 }
381 group_ids = new_group_ids;
382
383 group_ids[ncurves] = group_id;
384 ncurves++;
385
386 if (col) {
387 ptr = col + 1;
388 }
389 } while (col);
390
391 OPENSSL_free(*out_group_ids);
392 *out_group_ids = group_ids;
393 *out_group_ids_len = ncurves;
394
395 return 1;
396
397err:
398 OPENSSL_free(group_ids);
399 return 0;
400}
401
Robert Sloan4562e9d2017-10-02 10:26:51 -0700402int tls1_check_group_id(const SSL *ssl, uint16_t group_id) {
403 for (uint16_t supported : tls1_get_grouplist(ssl)) {
404 if (supported == group_id) {
David Benjamin95add822016-10-19 01:09:12 -0400405 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800406 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800407 }
408
David Benjamin95add822016-10-19 01:09:12 -0400409 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800410}
411
Robert Sloana27a6a42017-09-05 08:39:28 -0700412// kVerifySignatureAlgorithms is the default list of accepted signature
413// algorithms for verifying.
414//
415// For now, RSA-PSS signature algorithms are not enabled on Android's system
416// BoringSSL. Once the change in Chrome has stuck and the values are finalized,
417// restore them.
David Benjamin95add822016-10-19 01:09:12 -0400418static const uint16_t kVerifySignatureAlgorithms[] = {
Robert Sloana27a6a42017-09-05 08:39:28 -0700419 // List our preferred algorithms first.
Robert Sloan572a4e22017-04-17 10:52:19 -0700420 SSL_SIGN_ED25519,
Steven Valdez909b19f2016-11-21 15:35:44 -0500421 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Steven Valdez909b19f2016-11-21 15:35:44 -0500422 SSL_SIGN_RSA_PSS_SHA256,
Steven Valdez909b19f2016-11-21 15:35:44 -0500423 SSL_SIGN_RSA_PKCS1_SHA256,
424
Robert Sloana27a6a42017-09-05 08:39:28 -0700425 // Larger hashes are acceptable.
Steven Valdez909b19f2016-11-21 15:35:44 -0500426 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Steven Valdez909b19f2016-11-21 15:35:44 -0500427 SSL_SIGN_RSA_PSS_SHA384,
Steven Valdez909b19f2016-11-21 15:35:44 -0500428 SSL_SIGN_RSA_PKCS1_SHA384,
429
David Benjaminc895d6b2016-08-11 13:26:41 -0400430 SSL_SIGN_RSA_PSS_SHA512,
431 SSL_SIGN_RSA_PKCS1_SHA512,
David Benjaminc895d6b2016-08-11 13:26:41 -0400432
Robert Sloana27a6a42017-09-05 08:39:28 -0700433 // For now, SHA-1 is still accepted but least preferable.
Steven Valdez909b19f2016-11-21 15:35:44 -0500434 SSL_SIGN_RSA_PKCS1_SHA1,
David Benjaminc895d6b2016-08-11 13:26:41 -0400435
Steven Valdez909b19f2016-11-21 15:35:44 -0500436};
437
Robert Sloana27a6a42017-09-05 08:39:28 -0700438// kSignSignatureAlgorithms is the default list of supported signature
439// algorithms for signing.
440//
441// For now, RSA-PSS signature algorithms are not enabled on Android's system
442// BoringSSL. Once the change in Chrome has stuck and the values are finalized,
443// restore them.
Steven Valdez909b19f2016-11-21 15:35:44 -0500444static const uint16_t kSignSignatureAlgorithms[] = {
Robert Sloana27a6a42017-09-05 08:39:28 -0700445 // List our preferred algorithms first.
Robert Sloan572a4e22017-04-17 10:52:19 -0700446 SSL_SIGN_ED25519,
Steven Valdez909b19f2016-11-21 15:35:44 -0500447 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjaminc895d6b2016-08-11 13:26:41 -0400448 SSL_SIGN_RSA_PSS_SHA256,
449 SSL_SIGN_RSA_PKCS1_SHA256,
David Benjaminc895d6b2016-08-11 13:26:41 -0400450
Robert Sloana27a6a42017-09-05 08:39:28 -0700451 // If needed, sign larger hashes.
452 //
453 // TODO(davidben): Determine which of these may be pruned.
Steven Valdez909b19f2016-11-21 15:35:44 -0500454 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Steven Valdez909b19f2016-11-21 15:35:44 -0500455 SSL_SIGN_RSA_PSS_SHA384,
Steven Valdez909b19f2016-11-21 15:35:44 -0500456 SSL_SIGN_RSA_PKCS1_SHA384,
457
458 SSL_SIGN_ECDSA_SECP521R1_SHA512,
Steven Valdez909b19f2016-11-21 15:35:44 -0500459 SSL_SIGN_RSA_PSS_SHA512,
Steven Valdez909b19f2016-11-21 15:35:44 -0500460 SSL_SIGN_RSA_PKCS1_SHA512,
461
Robert Sloana27a6a42017-09-05 08:39:28 -0700462 // If the peer supports nothing else, sign with SHA-1.
David Benjaminc895d6b2016-08-11 13:26:41 -0400463 SSL_SIGN_ECDSA_SHA1,
Steven Valdez909b19f2016-11-21 15:35:44 -0500464 SSL_SIGN_RSA_PKCS1_SHA1,
David Benjaminc895d6b2016-08-11 13:26:41 -0400465};
466
Robert Sloan309a31e2018-01-29 10:22:47 -0800467bool tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) {
468 bool use_default = ssl->ctx->num_verify_sigalgs == 0;
469 Span<const uint16_t> sigalgs = kVerifySignatureAlgorithms;
470 if (!use_default) {
471 sigalgs = MakeConstSpan(ssl->ctx->verify_sigalgs,
472 ssl->ctx->num_verify_sigalgs);
Robert Sloan572a4e22017-04-17 10:52:19 -0700473 }
474
Robert Sloan309a31e2018-01-29 10:22:47 -0800475 for (uint16_t sigalg : sigalgs) {
476 if (use_default &&
477 sigalg == SSL_SIGN_ED25519 &&
Robert Sloan572a4e22017-04-17 10:52:19 -0700478 !ssl->ctx->ed25519_enabled) {
479 continue;
480 }
Robert Sloan309a31e2018-01-29 10:22:47 -0800481 if (!CBB_add_u16(out, sigalg)) {
482 return false;
Robert Sloan572a4e22017-04-17 10:52:19 -0700483 }
484 }
485
Robert Sloan309a31e2018-01-29 10:22:47 -0800486 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800487}
488
Robert Sloan309a31e2018-01-29 10:22:47 -0800489bool tls12_check_peer_sigalg(const SSL *ssl, uint8_t *out_alert,
490 uint16_t sigalg) {
Robert Sloan572a4e22017-04-17 10:52:19 -0700491 const uint16_t *sigalgs = kVerifySignatureAlgorithms;
492 size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
493 if (ssl->ctx->num_verify_sigalgs != 0) {
494 sigalgs = ssl->ctx->verify_sigalgs;
495 num_sigalgs = ssl->ctx->num_verify_sigalgs;
496 }
497
498 for (size_t i = 0; i < num_sigalgs; i++) {
499 if (sigalgs == kVerifySignatureAlgorithms &&
500 sigalgs[i] == SSL_SIGN_ED25519 &&
501 !ssl->ctx->ed25519_enabled) {
502 continue;
503 }
504 if (sigalg == sigalgs[i]) {
Robert Sloan309a31e2018-01-29 10:22:47 -0800505 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800506 }
507 }
508
David Benjamin95add822016-10-19 01:09:12 -0400509 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
510 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan309a31e2018-01-29 10:22:47 -0800511 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800512}
513
Robert Sloana27a6a42017-09-05 08:39:28 -0700514// tls_extension represents a TLS extension that is handled internally. The
515// |init| function is called for each handshake, before any other functions of
516// the extension. Then the add and parse callbacks are called as needed.
517//
518// The parse callbacks receive a |CBS| that contains the contents of the
519// extension (i.e. not including the type and length bytes). If an extension is
520// not received then the parse callbacks will be called with a NULL CBS so that
521// they can do any processing needed to handle the absence of an extension.
522//
523// The add callbacks receive a |CBB| to which the extension can be appended but
524// the function is responsible for appending the type and length bytes too.
525//
Robert Sloan921ef2c2017-10-17 09:02:20 -0700526// All callbacks return true for success and false for error. If a parse
527// function returns zero then a fatal alert with value |*out_alert| will be
528// sent. If |*out_alert| isn't set, then a |decode_error| alert will be sent.
Kenny Rootb8494592015-09-25 02:29:14 +0000529struct tls_extension {
530 uint16_t value;
David Benjamin1b249672016-12-06 18:25:50 -0500531 void (*init)(SSL_HANDSHAKE *hs);
Kenny Roota04d78d2015-09-25 00:26:37 +0000532
Robert Sloan921ef2c2017-10-17 09:02:20 -0700533 bool (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
534 bool (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
535 CBS *contents);
Kenny Roota04d78d2015-09-25 00:26:37 +0000536
Robert Sloan921ef2c2017-10-17 09:02:20 -0700537 bool (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
538 CBS *contents);
539 bool (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Kenny Rootb8494592015-09-25 02:29:14 +0000540};
Kenny Roota04d78d2015-09-25 00:26:37 +0000541
Robert Sloan921ef2c2017-10-17 09:02:20 -0700542static bool forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin1b249672016-12-06 18:25:50 -0500543 CBS *contents) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400544 if (contents != NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700545 // Servers MUST NOT send this extension.
David Benjaminc895d6b2016-08-11 13:26:41 -0400546 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
547 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700548 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400549 }
550
Robert Sloan921ef2c2017-10-17 09:02:20 -0700551 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400552}
553
Robert Sloan921ef2c2017-10-17 09:02:20 -0700554static bool ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin1b249672016-12-06 18:25:50 -0500555 CBS *contents) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700556 // This extension from the client is handled elsewhere.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700557 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400558}
559
Robert Sloan921ef2c2017-10-17 09:02:20 -0700560static bool dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
561 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400562}
Kenny Rootb8494592015-09-25 02:29:14 +0000563
Robert Sloana27a6a42017-09-05 08:39:28 -0700564// Server name indication (SNI).
565//
566// https://tools.ietf.org/html/rfc6066#section-3.
Kenny Rootb8494592015-09-25 02:29:14 +0000567
Robert Sloan921ef2c2017-10-17 09:02:20 -0700568static bool ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500569 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +0000570 if (ssl->tlsext_hostname == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700571 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000572 }
573
574 CBB contents, server_name_list, name;
575 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
576 !CBB_add_u16_length_prefixed(out, &contents) ||
577 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
578 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
579 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
580 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
581 strlen(ssl->tlsext_hostname)) ||
582 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700583 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000584 }
585
Robert Sloan921ef2c2017-10-17 09:02:20 -0700586 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000587}
588
Robert Sloan921ef2c2017-10-17 09:02:20 -0700589static bool ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
590 CBS *contents) {
Robert Sloand1d118f2017-09-11 09:00:48 -0700591 // The server may acknowledge SNI with an empty extension. We check the syntax
592 // but otherwise ignore this signal.
593 return contents == NULL || CBS_len(contents) == 0;
Kenny Rootb8494592015-09-25 02:29:14 +0000594}
595
Robert Sloan921ef2c2017-10-17 09:02:20 -0700596static bool ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
597 CBS *contents) {
Robert Sloand1d118f2017-09-11 09:00:48 -0700598 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +0000599 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700600 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000601 }
602
David Benjamin4969cc92016-04-22 15:02:23 -0400603 CBS server_name_list, host_name;
604 uint8_t name_type;
Kenny Rootb8494592015-09-25 02:29:14 +0000605 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin4969cc92016-04-22 15:02:23 -0400606 !CBS_get_u8(&server_name_list, &name_type) ||
Robert Sloana27a6a42017-09-05 08:39:28 -0700607 // Although the server_name extension was intended to be extensible to
608 // new name types and multiple names, OpenSSL 1.0.x had a bug which meant
609 // different name types will cause an error. Further, RFC 4366 originally
610 // defined syntax inextensibly. RFC 6066 corrected this mistake, but
611 // adding new name types is no longer feasible.
612 //
613 // Act as if the extensibility does not exist to simplify parsing.
David Benjamin4969cc92016-04-22 15:02:23 -0400614 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
615 CBS_len(&server_name_list) != 0 ||
Kenny Rootb8494592015-09-25 02:29:14 +0000616 CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700617 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000618 }
619
David Benjamin4969cc92016-04-22 15:02:23 -0400620 if (name_type != TLSEXT_NAMETYPE_host_name ||
621 CBS_len(&host_name) == 0 ||
622 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
623 CBS_contains_zero_byte(&host_name)) {
624 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700625 return false;
David Benjamin4969cc92016-04-22 15:02:23 -0400626 }
Kenny Rootb8494592015-09-25 02:29:14 +0000627
Robert Sloana27a6a42017-09-05 08:39:28 -0700628 // Copy the hostname as a string.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700629 char *raw = nullptr;
630 if (!CBS_strdup(&host_name, &raw)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500631 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700632 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000633 }
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700634 ssl->s3->hostname.reset(raw);
Kenny Rootb8494592015-09-25 02:29:14 +0000635
Robert Sloana27a6a42017-09-05 08:39:28 -0700636 hs->should_ack_sni = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700637 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000638}
639
Robert Sloan921ef2c2017-10-17 09:02:20 -0700640static bool ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500641 if (hs->ssl->s3->session_reused ||
642 !hs->should_ack_sni) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700643 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000644 }
645
646 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
647 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700648 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000649 }
650
Robert Sloan921ef2c2017-10-17 09:02:20 -0700651 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000652}
653
654
Robert Sloana27a6a42017-09-05 08:39:28 -0700655// Renegotiation indication.
656//
657// https://tools.ietf.org/html/rfc5746
Kenny Rootb8494592015-09-25 02:29:14 +0000658
Robert Sloan921ef2c2017-10-17 09:02:20 -0700659static bool ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500660 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700661 // Renegotiation indication is not necessary in TLS 1.3.
Robert Sloane56da3e2017-06-26 08:26:42 -0700662 if (hs->min_version >= TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700663 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400664 }
665
David Benjamin95add822016-10-19 01:09:12 -0400666 assert(ssl->s3->initial_handshake_complete ==
667 (ssl->s3->previous_client_finished_len != 0));
668
Kenny Rootb8494592015-09-25 02:29:14 +0000669 CBB contents, prev_finished;
670 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
671 !CBB_add_u16_length_prefixed(out, &contents) ||
672 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
673 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
674 ssl->s3->previous_client_finished_len) ||
675 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700676 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000677 }
678
Robert Sloan921ef2c2017-10-17 09:02:20 -0700679 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000680}
681
Robert Sloan921ef2c2017-10-17 09:02:20 -0700682static bool ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
683 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500684 SSL *const ssl = hs->ssl;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700685 if (contents != NULL && ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700686 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700687 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400688 }
689
Robert Sloana27a6a42017-09-05 08:39:28 -0700690 // Servers may not switch between omitting the extension and supporting it.
691 // See RFC 5746, sections 3.5 and 4.2.
Adam Langley4139edb2016-01-13 15:00:54 -0800692 if (ssl->s3->initial_handshake_complete &&
693 (contents != NULL) != ssl->s3->send_connection_binding) {
694 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
695 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700696 return false;
Adam Langley4139edb2016-01-13 15:00:54 -0800697 }
698
Kenny Rootb8494592015-09-25 02:29:14 +0000699 if (contents == NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700700 // Strictly speaking, if we want to avoid an attack we should *always* see
701 // RI even on initial ServerHello because the client doesn't see any
702 // renegotiation during an attack. However this would mean we could not
703 // connect to any server which doesn't support RI.
704 //
705 // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
706 // practical terms every client sets it so it's just assumed here.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700707 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000708 }
709
710 const size_t expected_len = ssl->s3->previous_client_finished_len +
711 ssl->s3->previous_server_finished_len;
712
Robert Sloana27a6a42017-09-05 08:39:28 -0700713 // Check for logic errors
Kenny Rootb8494592015-09-25 02:29:14 +0000714 assert(!expected_len || ssl->s3->previous_client_finished_len);
715 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin95add822016-10-19 01:09:12 -0400716 assert(ssl->s3->initial_handshake_complete ==
717 (ssl->s3->previous_client_finished_len != 0));
718 assert(ssl->s3->initial_handshake_complete ==
719 (ssl->s3->previous_server_finished_len != 0));
Kenny Rootb8494592015-09-25 02:29:14 +0000720
Robert Sloana27a6a42017-09-05 08:39:28 -0700721 // Parse out the extension contents.
Kenny Rootb8494592015-09-25 02:29:14 +0000722 CBS renegotiated_connection;
723 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
724 CBS_len(contents) != 0) {
725 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
726 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700727 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000728 }
729
Robert Sloana27a6a42017-09-05 08:39:28 -0700730 // Check that the extension matches.
Kenny Rootb8494592015-09-25 02:29:14 +0000731 if (CBS_len(&renegotiated_connection) != expected_len) {
732 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
733 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700734 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000735 }
736
737 const uint8_t *d = CBS_data(&renegotiated_connection);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700738 bool ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
739 ssl->s3->previous_client_finished_len) == 0;
Robert Sloanf6200e72017-07-10 08:09:18 -0700740#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
Robert Sloan921ef2c2017-10-17 09:02:20 -0700741 ok = true;
Robert Sloanf6200e72017-07-10 08:09:18 -0700742#endif
743 if (!ok) {
Kenny Rootb8494592015-09-25 02:29:14 +0000744 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
745 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700746 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000747 }
748 d += ssl->s3->previous_client_finished_len;
749
Robert Sloanf6200e72017-07-10 08:09:18 -0700750 ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
751 ssl->s3->previous_server_finished_len) == 0;
752#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
Robert Sloan921ef2c2017-10-17 09:02:20 -0700753 ok = true;
Robert Sloanf6200e72017-07-10 08:09:18 -0700754#endif
755 if (!ok) {
Kenny Rootb8494592015-09-25 02:29:14 +0000756 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700757 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700758 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000759 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700760 ssl->s3->send_connection_binding = true;
Kenny Rootb8494592015-09-25 02:29:14 +0000761
Robert Sloan921ef2c2017-10-17 09:02:20 -0700762 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000763}
764
Robert Sloan921ef2c2017-10-17 09:02:20 -0700765static bool ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
766 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500767 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700768 // Renegotiation isn't supported as a server so this function should never be
769 // called after the initial handshake.
Kenny Rootb8494592015-09-25 02:29:14 +0000770 assert(!ssl->s3->initial_handshake_complete);
771
Robert Sloan921ef2c2017-10-17 09:02:20 -0700772 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
773 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400774 }
Kenny Rootb8494592015-09-25 02:29:14 +0000775
776 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700777 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000778 }
779
780 CBS renegotiated_connection;
Kenny Rootb8494592015-09-25 02:29:14 +0000781 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
782 CBS_len(contents) != 0) {
783 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700784 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000785 }
786
Robert Sloana27a6a42017-09-05 08:39:28 -0700787 // Check that the extension matches. We do not support renegotiation as a
788 // server, so this must be empty.
David Benjamin95add822016-10-19 01:09:12 -0400789 if (CBS_len(&renegotiated_connection) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000790 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
791 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700792 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000793 }
794
Robert Sloana27a6a42017-09-05 08:39:28 -0700795 ssl->s3->send_connection_binding = true;
Kenny Rootb8494592015-09-25 02:29:14 +0000796
Robert Sloan921ef2c2017-10-17 09:02:20 -0700797 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000798}
799
Robert Sloan921ef2c2017-10-17 09:02:20 -0700800static bool ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500801 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700802 // Renegotiation isn't supported as a server so this function should never be
803 // called after the initial handshake.
David Benjamin95add822016-10-19 01:09:12 -0400804 assert(!ssl->s3->initial_handshake_complete);
805
Robert Sloan921ef2c2017-10-17 09:02:20 -0700806 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
807 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400808 }
809
Kenny Rootb8494592015-09-25 02:29:14 +0000810 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin95add822016-10-19 01:09:12 -0400811 !CBB_add_u16(out, 1 /* length */) ||
812 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700813 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000814 }
815
Robert Sloan921ef2c2017-10-17 09:02:20 -0700816 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000817}
818
819
Robert Sloana27a6a42017-09-05 08:39:28 -0700820// Extended Master Secret.
821//
822// https://tools.ietf.org/html/rfc7627
Kenny Rootb8494592015-09-25 02:29:14 +0000823
Robert Sloan921ef2c2017-10-17 09:02:20 -0700824static bool ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700825 // Extended master secret is not necessary in TLS 1.3.
Robert Sloane56da3e2017-06-26 08:26:42 -0700826 if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700827 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000828 }
829
830 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
831 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700832 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000833 }
834
Robert Sloan921ef2c2017-10-17 09:02:20 -0700835 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000836}
837
Robert Sloan921ef2c2017-10-17 09:02:20 -0700838static bool ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
839 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500840 SSL *const ssl = hs->ssl;
Robert Sloana94fe052017-02-21 08:49:28 -0800841
842 if (contents != NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700843 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
Robert Sloana94fe052017-02-21 08:49:28 -0800844 ssl->version == SSL3_VERSION ||
845 CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700846 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400847 }
848
Robert Sloana27a6a42017-09-05 08:39:28 -0700849 hs->extended_master_secret = true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400850 }
851
Robert Sloana27a6a42017-09-05 08:39:28 -0700852 // Whether EMS is negotiated may not change on renegotiation.
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700853 if (ssl->s3->established_session != nullptr &&
Robert Sloana94fe052017-02-21 08:49:28 -0800854 hs->extended_master_secret !=
Robert Sloana27a6a42017-09-05 08:39:28 -0700855 !!ssl->s3->established_session->extended_master_secret) {
Robert Sloana94fe052017-02-21 08:49:28 -0800856 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
857 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700858 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400859 }
860
Robert Sloan921ef2c2017-10-17 09:02:20 -0700861 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000862}
863
Robert Sloan921ef2c2017-10-17 09:02:20 -0700864static bool ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
865 CBS *contents) {
866 uint16_t version = ssl_protocol_version(hs->ssl);
David Benjamin1b249672016-12-06 18:25:50 -0500867 if (version >= TLS1_3_VERSION ||
868 version == SSL3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700869 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400870 }
871
872 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700873 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000874 }
875
876 if (CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700877 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000878 }
879
Robert Sloana27a6a42017-09-05 08:39:28 -0700880 hs->extended_master_secret = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700881 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000882}
883
Robert Sloan921ef2c2017-10-17 09:02:20 -0700884static bool ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloana94fe052017-02-21 08:49:28 -0800885 if (!hs->extended_master_secret) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700886 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000887 }
888
889 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
890 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700891 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000892 }
893
Robert Sloan921ef2c2017-10-17 09:02:20 -0700894 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000895}
896
897
Robert Sloana27a6a42017-09-05 08:39:28 -0700898// Session tickets.
899//
900// https://tools.ietf.org/html/rfc5077
Kenny Rootb8494592015-09-25 02:29:14 +0000901
Robert Sloan921ef2c2017-10-17 09:02:20 -0700902static bool ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500903 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700904 // TLS 1.3 uses a different ticket extension.
Robert Sloane56da3e2017-06-26 08:26:42 -0700905 if (hs->min_version >= TLS1_3_VERSION ||
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400906 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700907 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000908 }
909
910 const uint8_t *ticket_data = NULL;
911 int ticket_len = 0;
912
Robert Sloana27a6a42017-09-05 08:39:28 -0700913 // Renegotiation does not participate in session resumption. However, still
914 // advertise the extension to avoid potentially breaking servers which carry
915 // over the state from the previous handshake, such as OpenSSL servers
916 // without upstream's 3c3f0259238594d77264a78944d409f2127642c4.
Kenny Rootb8494592015-09-25 02:29:14 +0000917 if (!ssl->s3->initial_handshake_complete &&
918 ssl->session != NULL &&
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400919 ssl->session->tlsext_tick != NULL &&
Robert Sloana27a6a42017-09-05 08:39:28 -0700920 // Don't send TLS 1.3 session tickets in the ticket extension.
Robert Sloanae1abf92017-10-05 12:50:08 -0700921 ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION) {
Kenny Rootb8494592015-09-25 02:29:14 +0000922 ticket_data = ssl->session->tlsext_tick;
923 ticket_len = ssl->session->tlsext_ticklen;
924 }
925
926 CBB ticket;
927 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
928 !CBB_add_u16_length_prefixed(out, &ticket) ||
929 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
930 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700931 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000932 }
933
Robert Sloan921ef2c2017-10-17 09:02:20 -0700934 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000935}
936
Robert Sloan921ef2c2017-10-17 09:02:20 -0700937static bool ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
938 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500939 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +0000940 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700941 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000942 }
943
Robert Sloan921ef2c2017-10-17 09:02:20 -0700944 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
945 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400946 }
947
Robert Sloana27a6a42017-09-05 08:39:28 -0700948 // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
949 // this function should never be called, even if the server tries to send the
950 // extension.
Kenny Rootb8494592015-09-25 02:29:14 +0000951 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
952
953 if (CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700954 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000955 }
956
Robert Sloana27a6a42017-09-05 08:39:28 -0700957 hs->ticket_expected = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -0700958 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000959}
960
Robert Sloan921ef2c2017-10-17 09:02:20 -0700961static bool ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500962 if (!hs->ticket_expected) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700963 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000964 }
965
Robert Sloana27a6a42017-09-05 08:39:28 -0700966 // If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true.
David Benjamin1b249672016-12-06 18:25:50 -0500967 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Kenny Rootb8494592015-09-25 02:29:14 +0000968
969 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
970 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700971 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000972 }
973
Robert Sloan921ef2c2017-10-17 09:02:20 -0700974 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000975}
976
977
Robert Sloana27a6a42017-09-05 08:39:28 -0700978// Signature Algorithms.
979//
980// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
Kenny Rootb8494592015-09-25 02:29:14 +0000981
Robert Sloan921ef2c2017-10-17 09:02:20 -0700982static bool ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -0500983 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -0700984 if (hs->max_version < TLS1_2_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700985 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000986 }
987
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400988 CBB contents, sigalgs_cbb;
Kenny Rootb8494592015-09-25 02:29:14 +0000989 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
990 !CBB_add_u16_length_prefixed(out, &contents) ||
Robert Sloan572a4e22017-04-17 10:52:19 -0700991 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
992 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
993 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700994 return false;
Kenny Rootb8494592015-09-25 02:29:14 +0000995 }
996
Robert Sloan921ef2c2017-10-17 09:02:20 -0700997 return true;
Kenny Rootb8494592015-09-25 02:29:14 +0000998}
999
Robert Sloan921ef2c2017-10-17 09:02:20 -07001000static bool ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1001 CBS *contents) {
Robert Sloan4562e9d2017-10-02 10:26:51 -07001002 hs->peer_sigalgs.Reset();
Kenny Rootb8494592015-09-25 02:29:14 +00001003 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001004 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001005 }
1006
1007 CBS supported_signature_algorithms;
1008 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
1009 CBS_len(contents) != 0 ||
1010 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjamin1b249672016-12-06 18:25:50 -05001011 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001012 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001013 }
1014
Robert Sloan921ef2c2017-10-17 09:02:20 -07001015 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001016}
1017
Kenny Rootb8494592015-09-25 02:29:14 +00001018
Robert Sloana27a6a42017-09-05 08:39:28 -07001019// OCSP Stapling.
1020//
1021// https://tools.ietf.org/html/rfc6066#section-8
Kenny Rootb8494592015-09-25 02:29:14 +00001022
Robert Sloan921ef2c2017-10-17 09:02:20 -07001023static bool ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001024 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001025 if (!ssl->ocsp_stapling_enabled) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001026 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001027 }
1028
1029 CBB contents;
1030 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1031 !CBB_add_u16_length_prefixed(out, &contents) ||
1032 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1033 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1034 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1035 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001036 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001037 }
1038
Robert Sloan921ef2c2017-10-17 09:02:20 -07001039 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001040}
1041
Robert Sloan921ef2c2017-10-17 09:02:20 -07001042static bool ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1043 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001044 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001045 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001046 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001047 }
1048
Robert Sloana27a6a42017-09-05 08:39:28 -07001049 // TLS 1.3 OCSP responses are included in the Certificate extensions.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001050 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1051 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001052 }
1053
Robert Sloana27a6a42017-09-05 08:39:28 -07001054 // OCSP stapling is forbidden on non-certificate ciphers.
Steven Valdez909b19f2016-11-21 15:35:44 -05001055 if (CBS_len(contents) != 0 ||
Robert Sloana94fe052017-02-21 08:49:28 -08001056 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001057 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04001058 }
1059
Robert Sloana27a6a42017-09-05 08:39:28 -07001060 // Note this does not check for resumption in TLS 1.2. Sending
1061 // status_request here does not make sense, but OpenSSL does so and the
1062 // specification does not say anything. Tolerate it but ignore it.
David Benjaminc895d6b2016-08-11 13:26:41 -04001063
Robert Sloana27a6a42017-09-05 08:39:28 -07001064 hs->certificate_status_expected = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001065 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001066}
1067
Robert Sloan921ef2c2017-10-17 09:02:20 -07001068static bool ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1069 CBS *contents) {
Kenny Rootb8494592015-09-25 02:29:14 +00001070 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001071 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001072 }
1073
1074 uint8_t status_type;
1075 if (!CBS_get_u8(contents, &status_type)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001076 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001077 }
1078
Robert Sloana27a6a42017-09-05 08:39:28 -07001079 // We cannot decide whether OCSP stapling will occur yet because the correct
1080 // SSL_CTX might not have been selected.
David Benjamin1b249672016-12-06 18:25:50 -05001081 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Kenny Rootb8494592015-09-25 02:29:14 +00001082
Robert Sloan921ef2c2017-10-17 09:02:20 -07001083 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001084}
1085
Robert Sloan921ef2c2017-10-17 09:02:20 -07001086static bool ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001087 SSL *const ssl = hs->ssl;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001088 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin1b249672016-12-06 18:25:50 -05001089 !hs->ocsp_stapling_requested ||
Robert Sloana94fe052017-02-21 08:49:28 -08001090 ssl->cert->ocsp_response == NULL ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001091 ssl->s3->session_reused ||
Robert Sloana94fe052017-02-21 08:49:28 -08001092 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001093 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001094 }
1095
Robert Sloana27a6a42017-09-05 08:39:28 -07001096 hs->certificate_status_expected = true;
Kenny Rootb8494592015-09-25 02:29:14 +00001097
1098 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdez909b19f2016-11-21 15:35:44 -05001099 CBB_add_u16(out, 0 /* length */);
Kenny Rootb8494592015-09-25 02:29:14 +00001100}
1101
1102
Robert Sloana27a6a42017-09-05 08:39:28 -07001103// Next protocol negotiation.
1104//
1105// https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
Kenny Rootb8494592015-09-25 02:29:14 +00001106
Robert Sloan921ef2c2017-10-17 09:02:20 -07001107static bool ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001108 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001109 if (ssl->s3->initial_handshake_complete ||
1110 ssl->ctx->next_proto_select_cb == NULL ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001111 SSL_is_dtls(ssl)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001112 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001113 }
1114
1115 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1116 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001117 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001118 }
1119
Robert Sloan921ef2c2017-10-17 09:02:20 -07001120 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001121}
1122
Robert Sloan921ef2c2017-10-17 09:02:20 -07001123static bool ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1124 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001125 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001126 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001127 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001128 }
1129
Robert Sloan921ef2c2017-10-17 09:02:20 -07001130 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1131 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04001132 }
1133
Robert Sloana27a6a42017-09-05 08:39:28 -07001134 // If any of these are false then we should never have sent the NPN
1135 // extension in the ClientHello and thus this function should never have been
1136 // called.
Kenny Rootb8494592015-09-25 02:29:14 +00001137 assert(!ssl->s3->initial_handshake_complete);
David Benjaminc895d6b2016-08-11 13:26:41 -04001138 assert(!SSL_is_dtls(ssl));
Kenny Rootb8494592015-09-25 02:29:14 +00001139 assert(ssl->ctx->next_proto_select_cb != NULL);
1140
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001141 if (!ssl->s3->alpn_selected.empty()) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001142 // NPN and ALPN may not be negotiated in the same connection.
Kenny Rootb8494592015-09-25 02:29:14 +00001143 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1144 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001145 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001146 }
1147
1148 const uint8_t *const orig_contents = CBS_data(contents);
1149 const size_t orig_len = CBS_len(contents);
1150
1151 while (CBS_len(contents) != 0) {
1152 CBS proto;
1153 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1154 CBS_len(&proto) == 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001155 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001156 }
1157 }
1158
1159 uint8_t *selected;
1160 uint8_t selected_len;
1161 if (ssl->ctx->next_proto_select_cb(
1162 ssl, &selected, &selected_len, orig_contents, orig_len,
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001163 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK ||
1164 !ssl->s3->next_proto_negotiated.CopyFrom(
1165 MakeConstSpan(selected, selected_len))) {
Kenny Rootb8494592015-09-25 02:29:14 +00001166 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001167 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001168 }
1169
Robert Sloana27a6a42017-09-05 08:39:28 -07001170 hs->next_proto_neg_seen = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001171 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001172}
1173
Robert Sloan921ef2c2017-10-17 09:02:20 -07001174static bool ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1175 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001176 SSL *const ssl = hs->ssl;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001177 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1178 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04001179 }
1180
Kenny Rootb8494592015-09-25 02:29:14 +00001181 if (contents != NULL && CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001182 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001183 }
1184
1185 if (contents == NULL ||
1186 ssl->s3->initial_handshake_complete ||
Kenny Rootb8494592015-09-25 02:29:14 +00001187 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001188 SSL_is_dtls(ssl)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001189 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001190 }
1191
Robert Sloana27a6a42017-09-05 08:39:28 -07001192 hs->next_proto_neg_seen = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001193 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001194}
1195
Robert Sloan921ef2c2017-10-17 09:02:20 -07001196static bool ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001197 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001198 // |next_proto_neg_seen| might have been cleared when an ALPN extension was
1199 // parsed.
David Benjamin1b249672016-12-06 18:25:50 -05001200 if (!hs->next_proto_neg_seen) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001201 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001202 }
1203
1204 const uint8_t *npa;
1205 unsigned npa_len;
1206
1207 if (ssl->ctx->next_protos_advertised_cb(
1208 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1209 SSL_TLSEXT_ERR_OK) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001210 hs->next_proto_neg_seen = false;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001211 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001212 }
1213
1214 CBB contents;
1215 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1216 !CBB_add_u16_length_prefixed(out, &contents) ||
1217 !CBB_add_bytes(&contents, npa, npa_len) ||
1218 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001219 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001220 }
1221
Robert Sloan921ef2c2017-10-17 09:02:20 -07001222 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001223}
1224
1225
Robert Sloana27a6a42017-09-05 08:39:28 -07001226// Signed certificate timestamps.
1227//
1228// https://tools.ietf.org/html/rfc6962#section-3.3.1
Kenny Rootb8494592015-09-25 02:29:14 +00001229
Robert Sloan921ef2c2017-10-17 09:02:20 -07001230static bool ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001231 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001232 if (!ssl->signed_cert_timestamps_enabled) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001233 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001234 }
1235
1236 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1237 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001238 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001239 }
1240
Robert Sloan921ef2c2017-10-17 09:02:20 -07001241 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001242}
1243
Robert Sloan921ef2c2017-10-17 09:02:20 -07001244static bool ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1245 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001246 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001247 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001248 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001249 }
1250
Robert Sloana27a6a42017-09-05 08:39:28 -07001251 // TLS 1.3 SCTs are included in the Certificate extensions.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001252 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001253 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001254 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05001255 }
1256
Robert Sloana27a6a42017-09-05 08:39:28 -07001257 // If this is false then we should never have sent the SCT extension in the
1258 // ClientHello and thus this function should never have been called.
Kenny Rootb8494592015-09-25 02:29:14 +00001259 assert(ssl->signed_cert_timestamps_enabled);
1260
Steven Valdez909b19f2016-11-21 15:35:44 -05001261 if (!ssl_is_sct_list_valid(contents)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001262 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001263 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001264 }
1265
Robert Sloana27a6a42017-09-05 08:39:28 -07001266 // Session resumption uses the original session information. The extension
1267 // should not be sent on resumption, but RFC 6962 did not make it a
1268 // requirement, so tolerate this.
1269 //
1270 // TODO(davidben): Enforce this anyway.
Robert Sloan8f860b12017-08-28 07:37:06 -07001271 if (!ssl->s3->session_reused) {
1272 CRYPTO_BUFFER_free(hs->new_session->signed_cert_timestamp_list);
1273 hs->new_session->signed_cert_timestamp_list =
1274 CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool);
1275 if (hs->new_session->signed_cert_timestamp_list == nullptr) {
1276 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001277 return false;
Robert Sloan8f860b12017-08-28 07:37:06 -07001278 }
Kenny Rootb8494592015-09-25 02:29:14 +00001279 }
1280
Robert Sloan921ef2c2017-10-17 09:02:20 -07001281 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001282}
1283
Robert Sloan921ef2c2017-10-17 09:02:20 -07001284static bool ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1285 CBS *contents) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001286 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001287 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05001288 }
1289
1290 if (CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001291 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05001292 }
1293
Robert Sloana27a6a42017-09-05 08:39:28 -07001294 hs->scts_requested = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001295 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001296}
1297
Robert Sloan921ef2c2017-10-17 09:02:20 -07001298static bool ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001299 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001300 // The extension shouldn't be sent when resuming sessions.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001301 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
Steven Valdez909b19f2016-11-21 15:35:44 -05001302 ssl->s3->session_reused ||
Robert Sloana94fe052017-02-21 08:49:28 -08001303 ssl->cert->signed_cert_timestamp_list == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001304 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001305 }
1306
1307 CBB contents;
1308 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1309 CBB_add_u16_length_prefixed(out, &contents) &&
Robert Sloana94fe052017-02-21 08:49:28 -08001310 CBB_add_bytes(
1311 &contents,
1312 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1313 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Kenny Rootb8494592015-09-25 02:29:14 +00001314 CBB_flush(out);
1315}
1316
1317
Robert Sloana27a6a42017-09-05 08:39:28 -07001318// Application-level Protocol Negotiation.
1319//
1320// https://tools.ietf.org/html/rfc7301
Kenny Rootb8494592015-09-25 02:29:14 +00001321
Robert Sloan921ef2c2017-10-17 09:02:20 -07001322static bool ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001323 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001324 if (ssl->alpn_client_proto_list == NULL ||
1325 ssl->s3->initial_handshake_complete) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001326 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001327 }
1328
1329 CBB contents, proto_list;
1330 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1331 !CBB_add_u16_length_prefixed(out, &contents) ||
1332 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1333 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1334 ssl->alpn_client_proto_list_len) ||
1335 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001336 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001337 }
1338
Robert Sloan921ef2c2017-10-17 09:02:20 -07001339 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001340}
1341
Robert Sloan921ef2c2017-10-17 09:02:20 -07001342static bool ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1343 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001344 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001345 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001346 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001347 }
1348
1349 assert(!ssl->s3->initial_handshake_complete);
1350 assert(ssl->alpn_client_proto_list != NULL);
1351
David Benjamin1b249672016-12-06 18:25:50 -05001352 if (hs->next_proto_neg_seen) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001353 // NPN and ALPN may not be negotiated in the same connection.
Kenny Rootb8494592015-09-25 02:29:14 +00001354 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1355 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001356 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001357 }
1358
Robert Sloana27a6a42017-09-05 08:39:28 -07001359 // The extension data consists of a ProtocolNameList which must have
1360 // exactly one ProtocolName. Each of these is length-prefixed.
Kenny Rootb8494592015-09-25 02:29:14 +00001361 CBS protocol_name_list, protocol_name;
1362 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1363 CBS_len(contents) != 0 ||
1364 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
Robert Sloana27a6a42017-09-05 08:39:28 -07001365 // Empty protocol names are forbidden.
Kenny Rootb8494592015-09-25 02:29:14 +00001366 CBS_len(&protocol_name) == 0 ||
1367 CBS_len(&protocol_name_list) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001368 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001369 }
1370
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001371 if (!ssl_is_alpn_protocol_allowed(ssl, protocol_name)) {
1372 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1373 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1374 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04001375 }
1376
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001377 if (!ssl->s3->alpn_selected.CopyFrom(protocol_name)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001378 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001379 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001380 }
1381
Robert Sloan921ef2c2017-10-17 09:02:20 -07001382 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001383}
1384
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001385bool ssl_is_alpn_protocol_allowed(const SSL *ssl,
1386 Span<const uint8_t> protocol) {
1387 if (ssl->alpn_client_proto_list == nullptr) {
1388 return false;
1389 }
1390
1391 if (ssl->ctx->allow_unknown_alpn_protos) {
1392 return true;
1393 }
1394
1395 // Check that the protocol name is one of the ones we advertised.
1396 CBS client_protocol_name_list, client_protocol_name;
1397 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1398 ssl->alpn_client_proto_list_len);
1399 while (CBS_len(&client_protocol_name_list) > 0) {
1400 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1401 &client_protocol_name)) {
1402 return false;
1403 }
1404
1405 if (client_protocol_name == protocol) {
1406 return true;
1407 }
1408 }
1409
1410 return false;
1411}
1412
Robert Sloan921ef2c2017-10-17 09:02:20 -07001413bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1414 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin1b249672016-12-06 18:25:50 -05001415 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -05001416 CBS contents;
Kenny Rootb8494592015-09-25 02:29:14 +00001417 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin1b249672016-12-06 18:25:50 -05001418 !ssl_client_hello_get_extension(
Steven Valdez909b19f2016-11-21 15:35:44 -05001419 client_hello, &contents,
1420 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001421 // Ignore ALPN if not configured or no extension was supplied.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001422 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001423 }
1424
Robert Sloana27a6a42017-09-05 08:39:28 -07001425 // ALPN takes precedence over NPN.
1426 hs->next_proto_neg_seen = false;
Kenny Rootb8494592015-09-25 02:29:14 +00001427
1428 CBS protocol_name_list;
Steven Valdez909b19f2016-11-21 15:35:44 -05001429 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1430 CBS_len(&contents) != 0 ||
Kenny Rootb8494592015-09-25 02:29:14 +00001431 CBS_len(&protocol_name_list) < 2) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001432 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1433 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001434 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001435 }
1436
Robert Sloana27a6a42017-09-05 08:39:28 -07001437 // Validate the protocol list.
Kenny Rootb8494592015-09-25 02:29:14 +00001438 CBS protocol_name_list_copy = protocol_name_list;
1439 while (CBS_len(&protocol_name_list_copy) > 0) {
1440 CBS protocol_name;
1441
1442 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
Robert Sloana27a6a42017-09-05 08:39:28 -07001443 // Empty protocol names are forbidden.
Kenny Rootb8494592015-09-25 02:29:14 +00001444 CBS_len(&protocol_name) == 0) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001445 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1446 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001447 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001448 }
1449 }
1450
1451 const uint8_t *selected;
1452 uint8_t selected_len;
1453 if (ssl->ctx->alpn_select_cb(
1454 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1455 CBS_len(&protocol_name_list),
1456 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001457 if (!ssl->s3->alpn_selected.CopyFrom(
1458 MakeConstSpan(selected, selected_len))) {
Kenny Rootb8494592015-09-25 02:29:14 +00001459 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001460 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001461 }
Kenny Rootb8494592015-09-25 02:29:14 +00001462 }
1463
Robert Sloan921ef2c2017-10-17 09:02:20 -07001464 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001465}
1466
Robert Sloan921ef2c2017-10-17 09:02:20 -07001467static bool ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001468 SSL *const ssl = hs->ssl;
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001469 if (ssl->s3->alpn_selected.empty()) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001470 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001471 }
1472
1473 CBB contents, proto_list, proto;
1474 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1475 !CBB_add_u16_length_prefixed(out, &contents) ||
1476 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1477 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001478 !CBB_add_bytes(&proto, ssl->s3->alpn_selected.data(),
1479 ssl->s3->alpn_selected.size()) ||
Kenny Rootb8494592015-09-25 02:29:14 +00001480 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001481 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001482 }
1483
Robert Sloan921ef2c2017-10-17 09:02:20 -07001484 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001485}
1486
1487
Robert Sloana27a6a42017-09-05 08:39:28 -07001488// Channel ID.
1489//
1490// https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
Kenny Rootb8494592015-09-25 02:29:14 +00001491
David Benjamin1b249672016-12-06 18:25:50 -05001492static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001493 hs->ssl->s3->tlsext_channel_id_valid = false;
Kenny Rootb8494592015-09-25 02:29:14 +00001494}
1495
Robert Sloan921ef2c2017-10-17 09:02:20 -07001496static bool ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001497 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001498 if (!ssl->tlsext_channel_id_enabled ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001499 SSL_is_dtls(ssl)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001500 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001501 }
1502
1503 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1504 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001505 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001506 }
1507
Robert Sloan921ef2c2017-10-17 09:02:20 -07001508 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001509}
1510
Robert Sloan921ef2c2017-10-17 09:02:20 -07001511static bool ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1512 uint8_t *out_alert,
1513 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001514 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001515 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001516 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001517 }
1518
David Benjaminc895d6b2016-08-11 13:26:41 -04001519 assert(!SSL_is_dtls(ssl));
Kenny Rootb8494592015-09-25 02:29:14 +00001520 assert(ssl->tlsext_channel_id_enabled);
1521
1522 if (CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001523 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001524 }
1525
Robert Sloana27a6a42017-09-05 08:39:28 -07001526 ssl->s3->tlsext_channel_id_valid = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001527 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001528}
1529
Robert Sloan921ef2c2017-10-17 09:02:20 -07001530static bool ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1531 uint8_t *out_alert,
1532 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001533 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001534 if (contents == NULL ||
1535 !ssl->tlsext_channel_id_enabled ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001536 SSL_is_dtls(ssl)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001537 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001538 }
1539
1540 if (CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001541 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001542 }
1543
Robert Sloana27a6a42017-09-05 08:39:28 -07001544 ssl->s3->tlsext_channel_id_valid = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001545 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001546}
1547
Robert Sloan921ef2c2017-10-17 09:02:20 -07001548static bool ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001549 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001550 if (!ssl->s3->tlsext_channel_id_valid) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001551 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001552 }
1553
1554 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1555 !CBB_add_u16(out, 0 /* length */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001556 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001557 }
1558
Robert Sloan921ef2c2017-10-17 09:02:20 -07001559 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001560}
1561
1562
Robert Sloana27a6a42017-09-05 08:39:28 -07001563// Secure Real-time Transport Protocol (SRTP) extension.
1564//
1565// https://tools.ietf.org/html/rfc5764
Kenny Rootb8494592015-09-25 02:29:14 +00001566
1567
David Benjamin1b249672016-12-06 18:25:50 -05001568static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1569 hs->ssl->srtp_profile = NULL;
Kenny Rootb8494592015-09-25 02:29:14 +00001570}
1571
Robert Sloan921ef2c2017-10-17 09:02:20 -07001572static bool ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001573 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001574 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
Robert Sloanfe7cd212017-08-07 09:03:39 -07001575 if (profiles == NULL ||
1576 sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001577 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001578 }
1579
1580 CBB contents, profile_ids;
1581 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1582 !CBB_add_u16_length_prefixed(out, &contents) ||
1583 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001584 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001585 }
1586
Robert Sloanfe7cd212017-08-07 09:03:39 -07001587 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
1588 if (!CBB_add_u16(&profile_ids, profile->id)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001589 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001590 }
1591 }
1592
1593 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1594 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001595 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001596 }
1597
Robert Sloan921ef2c2017-10-17 09:02:20 -07001598 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001599}
1600
Robert Sloan921ef2c2017-10-17 09:02:20 -07001601static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1602 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001603 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001604 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001605 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001606 }
1607
Robert Sloana27a6a42017-09-05 08:39:28 -07001608 // The extension consists of a u16-prefixed profile ID list containing a
1609 // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1610 //
1611 // See https://tools.ietf.org/html/rfc5764#section-4.1.1
Kenny Rootb8494592015-09-25 02:29:14 +00001612 CBS profile_ids, srtp_mki;
1613 uint16_t profile_id;
1614 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1615 !CBS_get_u16(&profile_ids, &profile_id) ||
1616 CBS_len(&profile_ids) != 0 ||
1617 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1618 CBS_len(contents) != 0) {
1619 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001620 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001621 }
1622
1623 if (CBS_len(&srtp_mki) != 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001624 // Must be no MKI, since we never offer one.
Kenny Rootb8494592015-09-25 02:29:14 +00001625 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1626 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001627 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001628 }
1629
1630 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1631
Robert Sloana27a6a42017-09-05 08:39:28 -07001632 // Check to see if the server gave us something we support (and presumably
1633 // offered).
Robert Sloanfe7cd212017-08-07 09:03:39 -07001634 for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
Kenny Rootb8494592015-09-25 02:29:14 +00001635 if (profile->id == profile_id) {
1636 ssl->srtp_profile = profile;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001637 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001638 }
1639 }
1640
1641 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1642 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001643 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001644}
1645
Robert Sloan921ef2c2017-10-17 09:02:20 -07001646static bool ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1647 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001648 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001649 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001650 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001651 }
1652
1653 CBS profile_ids, srtp_mki;
1654 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1655 CBS_len(&profile_ids) < 2 ||
1656 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1657 CBS_len(contents) != 0) {
1658 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001659 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001660 }
Robert Sloana27a6a42017-09-05 08:39:28 -07001661 // Discard the MKI value for now.
Kenny Rootb8494592015-09-25 02:29:14 +00001662
1663 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1664 SSL_get_srtp_profiles(ssl);
1665
Robert Sloana27a6a42017-09-05 08:39:28 -07001666 // Pick the server's most preferred profile.
Robert Sloanfe7cd212017-08-07 09:03:39 -07001667 for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) {
Kenny Rootb8494592015-09-25 02:29:14 +00001668 CBS profile_ids_tmp;
1669 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1670
1671 while (CBS_len(&profile_ids_tmp) > 0) {
1672 uint16_t profile_id;
1673 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001674 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001675 }
1676
1677 if (server_profile->id == profile_id) {
1678 ssl->srtp_profile = server_profile;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001679 return true;
Kenny Roota04d78d2015-09-25 00:26:37 +00001680 }
1681 }
1682 }
1683
Robert Sloan921ef2c2017-10-17 09:02:20 -07001684 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001685}
1686
Robert Sloan921ef2c2017-10-17 09:02:20 -07001687static bool ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001688 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001689 if (ssl->srtp_profile == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001690 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001691 }
1692
1693 CBB contents, profile_ids;
1694 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1695 !CBB_add_u16_length_prefixed(out, &contents) ||
1696 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1697 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1698 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1699 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001700 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001701 }
1702
Robert Sloan921ef2c2017-10-17 09:02:20 -07001703 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001704}
1705
1706
Robert Sloana27a6a42017-09-05 08:39:28 -07001707// EC point formats.
1708//
1709// https://tools.ietf.org/html/rfc4492#section-5.1.2
Kenny Rootb8494592015-09-25 02:29:14 +00001710
Robert Sloan921ef2c2017-10-17 09:02:20 -07001711static bool ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
Kenny Rootb8494592015-09-25 02:29:14 +00001712 CBB contents, formats;
1713 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1714 !CBB_add_u16_length_prefixed(out, &contents) ||
1715 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1716 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
1717 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001718 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001719 }
1720
Robert Sloan921ef2c2017-10-17 09:02:20 -07001721 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001722}
1723
Robert Sloan921ef2c2017-10-17 09:02:20 -07001724static bool ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001725 // The point format extension is unneccessary in TLS 1.3.
Robert Sloane56da3e2017-06-26 08:26:42 -07001726 if (hs->min_version >= TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001727 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001728 }
1729
David Benjamin1b249672016-12-06 18:25:50 -05001730 return ext_ec_point_add_extension(hs, out);
Kenny Rootb8494592015-09-25 02:29:14 +00001731}
1732
Robert Sloan921ef2c2017-10-17 09:02:20 -07001733static bool ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1734 CBS *contents) {
Kenny Rootb8494592015-09-25 02:29:14 +00001735 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001736 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001737 }
1738
Robert Sloan921ef2c2017-10-17 09:02:20 -07001739 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
1740 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04001741 }
1742
Kenny Rootb8494592015-09-25 02:29:14 +00001743 CBS ec_point_format_list;
1744 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1745 CBS_len(contents) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001746 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001747 }
1748
Robert Sloana27a6a42017-09-05 08:39:28 -07001749 // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1750 // point format.
Robert Sloan69939df2017-01-09 10:53:07 -08001751 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1752 TLSEXT_ECPOINTFORMAT_uncompressed,
1753 CBS_len(&ec_point_format_list)) == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001754 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001755 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00001756 }
1757
Robert Sloan921ef2c2017-10-17 09:02:20 -07001758 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001759}
1760
Robert Sloan921ef2c2017-10-17 09:02:20 -07001761static bool ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +00001762 CBS *contents) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001763 if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
1764 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04001765 }
1766
David Benjamin1b249672016-12-06 18:25:50 -05001767 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Kenny Rootb8494592015-09-25 02:29:14 +00001768}
1769
Robert Sloan921ef2c2017-10-17 09:02:20 -07001770static bool ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001771 SSL *const ssl = hs->ssl;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001772 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
1773 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04001774 }
1775
Robert Sloana94fe052017-02-21 08:49:28 -08001776 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1777 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001778 const bool using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Kenny Rootb8494592015-09-25 02:29:14 +00001779
1780 if (!using_ecc) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001781 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00001782 }
1783
David Benjamin1b249672016-12-06 18:25:50 -05001784 return ext_ec_point_add_extension(hs, out);
Kenny Rootb8494592015-09-25 02:29:14 +00001785}
1786
Steven Valdez909b19f2016-11-21 15:35:44 -05001787
Robert Sloana27a6a42017-09-05 08:39:28 -07001788// Pre Shared Key
1789//
1790// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
Steven Valdez909b19f2016-11-21 15:35:44 -05001791
David Benjamin1b249672016-12-06 18:25:50 -05001792static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1793 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -07001794 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Robert Sloanae1abf92017-10-05 12:50:08 -07001795 ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001796 return 0;
1797 }
1798
Robert Sloanae1abf92017-10-05 12:50:08 -07001799 size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session));
Steven Valdez909b19f2016-11-21 15:35:44 -05001800 return 15 + ssl->session->tlsext_ticklen + binder_len;
1801}
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001802
Robert Sloan921ef2c2017-10-17 09:02:20 -07001803static bool ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001804 SSL *const ssl = hs->ssl;
Robert Sloanb1b54b82017-11-06 13:50:02 -08001805 hs->needs_psk_binder = false;
Robert Sloane56da3e2017-06-26 08:26:42 -07001806 if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
Robert Sloanae1abf92017-10-05 12:50:08 -07001807 ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001808 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001809 }
1810
Robert Sloanb1b54b82017-11-06 13:50:02 -08001811 // Per draft-ietf-tls-tls13-21 section 4.1.4, skip offering the session if the
1812 // selected cipher in HelloRetryRequest does not match. This avoids performing
1813 // the transcript hash transformation for multiple hashes.
1814 if (hs->received_hello_retry_request &&
Robert Sloanb1b54b82017-11-06 13:50:02 -08001815 ssl->session->cipher->algorithm_prf != hs->new_cipher->algorithm_prf) {
1816 return true;
1817 }
1818
Robert Sloan7d422bc2017-03-06 10:04:29 -08001819 struct OPENSSL_timeval now;
Steven Valdez909b19f2016-11-21 15:35:44 -05001820 ssl_get_current_time(ssl, &now);
1821 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1822 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1823
Robert Sloana27a6a42017-09-05 08:39:28 -07001824 // Fill in a placeholder zero binder of the appropriate length. It will be
1825 // computed and filled in later after length prefixes are computed.
Steven Valdez909b19f2016-11-21 15:35:44 -05001826 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Robert Sloanae1abf92017-10-05 12:50:08 -07001827 size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session));
Steven Valdez909b19f2016-11-21 15:35:44 -05001828
1829 CBB contents, identity, ticket, binders, binder;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001830 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1831 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001832 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001833 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1834 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdez909b19f2016-11-21 15:35:44 -05001835 ssl->session->tlsext_ticklen) ||
1836 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1837 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1838 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1839 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001840 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001841 }
1842
Robert Sloana27a6a42017-09-05 08:39:28 -07001843 hs->needs_psk_binder = true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001844 return CBB_flush(out);
1845}
1846
Robert Sloan921ef2c2017-10-17 09:02:20 -07001847bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1848 uint8_t *out_alert,
1849 CBS *contents) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001850 uint16_t psk_id;
1851 if (!CBS_get_u16(contents, &psk_id) ||
1852 CBS_len(contents) != 0) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001853 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001854 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001855 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001856 }
1857
Robert Sloana27a6a42017-09-05 08:39:28 -07001858 // We only advertise one PSK identity, so the only legal index is zero.
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001859 if (psk_id != 0) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001860 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001861 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001862 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001863 }
1864
Robert Sloan921ef2c2017-10-17 09:02:20 -07001865 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001866}
1867
Robert Sloan921ef2c2017-10-17 09:02:20 -07001868bool ssl_ext_pre_shared_key_parse_clienthello(
Robert Sloan1c9db532017-03-13 08:03:59 -07001869 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
1870 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001871 // We only process the first PSK identity since we don't support pure PSK.
Robert Sloan1c9db532017-03-13 08:03:59 -07001872 CBS identities, binders;
David Benjamin1b249672016-12-06 18:25:50 -05001873 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
Robert Sloan1c9db532017-03-13 08:03:59 -07001874 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
1875 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdez909b19f2016-11-21 15:35:44 -05001876 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjamin1b249672016-12-06 18:25:50 -05001877 CBS_len(&binders) == 0 ||
Steven Valdez909b19f2016-11-21 15:35:44 -05001878 CBS_len(contents) != 0) {
1879 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001880 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001881 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001882 }
1883
Steven Valdez909b19f2016-11-21 15:35:44 -05001884 *out_binders = binders;
1885
Robert Sloana27a6a42017-09-05 08:39:28 -07001886 // Check the syntax of the remaining identities, but do not process them.
David Benjamin1b249672016-12-06 18:25:50 -05001887 size_t num_identities = 1;
1888 while (CBS_len(&identities) != 0) {
1889 CBS unused_ticket;
1890 uint32_t unused_obfuscated_ticket_age;
1891 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1892 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1893 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1894 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001895 return false;
David Benjamin1b249672016-12-06 18:25:50 -05001896 }
1897
1898 num_identities++;
1899 }
1900
Robert Sloana27a6a42017-09-05 08:39:28 -07001901 // Check the syntax of the binders. The value will be checked later if
1902 // resuming.
David Benjamin1b249672016-12-06 18:25:50 -05001903 size_t num_binders = 0;
1904 while (CBS_len(&binders) != 0) {
1905 CBS binder;
1906 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
1907 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1908 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001909 return false;
David Benjamin1b249672016-12-06 18:25:50 -05001910 }
1911
1912 num_binders++;
1913 }
1914
1915 if (num_identities != num_binders) {
1916 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
1917 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001918 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001919 }
1920
Robert Sloan921ef2c2017-10-17 09:02:20 -07001921 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001922}
1923
Robert Sloan921ef2c2017-10-17 09:02:20 -07001924bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05001925 if (!hs->ssl->s3->session_reused) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001926 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001927 }
1928
1929 CBB contents;
1930 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1931 !CBB_add_u16_length_prefixed(out, &contents) ||
Robert Sloana27a6a42017-09-05 08:39:28 -07001932 // We only consider the first identity for resumption
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001933 !CBB_add_u16(&contents, 0) ||
1934 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001935 return false;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001936 }
1937
Robert Sloan921ef2c2017-10-17 09:02:20 -07001938 return true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001939}
1940
1941
Robert Sloana27a6a42017-09-05 08:39:28 -07001942// Pre-Shared Key Exchange Modes
1943//
1944// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7
David Benjamin1b249672016-12-06 18:25:50 -05001945
Robert Sloan921ef2c2017-10-17 09:02:20 -07001946static bool ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
1947 CBB *out) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001948 if (hs->max_version < TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001949 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05001950 }
1951
1952 CBB contents, ke_modes;
1953 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
1954 !CBB_add_u16_length_prefixed(out, &contents) ||
1955 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
1956 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001957 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05001958 }
1959
1960 return CBB_flush(out);
1961}
1962
Robert Sloan921ef2c2017-10-17 09:02:20 -07001963static bool ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
1964 uint8_t *out_alert,
1965 CBS *contents) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001966 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001967 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05001968 }
1969
1970 CBS ke_modes;
1971 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
1972 CBS_len(&ke_modes) == 0 ||
1973 CBS_len(contents) != 0) {
1974 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001975 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05001976 }
1977
Robert Sloana27a6a42017-09-05 08:39:28 -07001978 // We only support tickets with PSK_DHE_KE.
Robert Sloan69939df2017-01-09 10:53:07 -08001979 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
1980 CBS_len(&ke_modes)) != NULL;
Steven Valdez909b19f2016-11-21 15:35:44 -05001981
Robert Sloan921ef2c2017-10-17 09:02:20 -07001982 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05001983}
1984
1985
Robert Sloana27a6a42017-09-05 08:39:28 -07001986// Early Data Indication
1987//
1988// https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
David Benjamin1b249672016-12-06 18:25:50 -05001989
Robert Sloan921ef2c2017-10-17 09:02:20 -07001990static bool ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001991 SSL *const ssl = hs->ssl;
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001992 if (!ssl->cert->enable_early_data ||
1993 // Session must be 0-RTT capable.
1994 ssl->session == NULL ||
Robert Sloanae1abf92017-10-05 12:50:08 -07001995 ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION ||
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001996 ssl->session->ticket_max_early_data == 0 ||
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001997 // The second ClientHello never offers early data.
Robert Sloan6d0d00e2017-03-27 07:13:07 -07001998 hs->received_hello_retry_request ||
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001999 // In case ALPN preferences changed since this session was established,
2000 // avoid reporting a confusing value in |SSL_get0_alpn_selected|.
2001 (ssl->session->early_alpn_len != 0 &&
2002 !ssl_is_alpn_protocol_allowed(
2003 ssl, MakeConstSpan(ssl->session->early_alpn,
2004 ssl->session->early_alpn_len)))) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002005 return true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002006 }
2007
Robert Sloana27a6a42017-09-05 08:39:28 -07002008 hs->early_data_offered = true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002009
2010 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2011 !CBB_add_u16(out, 0) ||
2012 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002013 return false;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002014 }
2015
Robert Sloan921ef2c2017-10-17 09:02:20 -07002016 return true;
David Benjamin1b249672016-12-06 18:25:50 -05002017}
2018
Robert Sloan921ef2c2017-10-17 09:02:20 -07002019static bool ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
2020 uint8_t *out_alert, CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05002021 SSL *const ssl = hs->ssl;
2022 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002023 return true;
David Benjamin1b249672016-12-06 18:25:50 -05002024 }
2025
2026 if (CBS_len(contents) != 0) {
2027 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002028 return false;
David Benjamin1b249672016-12-06 18:25:50 -05002029 }
2030
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002031 if (!ssl->s3->session_reused) {
2032 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2033 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002034 return false;
David Benjamin1b249672016-12-06 18:25:50 -05002035 }
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002036
Robert Sloan0da43952018-01-03 15:13:14 -08002037 ssl->s3->early_data_accepted = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002038 return true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002039}
2040
Robert Sloan921ef2c2017-10-17 09:02:20 -07002041static bool ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2042 uint8_t *out_alert, CBS *contents) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002043 SSL *const ssl = hs->ssl;
2044 if (contents == NULL ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07002045 ssl_protocol_version(ssl) < TLS1_3_VERSION) {
2046 return true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002047 }
2048
2049 if (CBS_len(contents) != 0) {
2050 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002051 return false;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002052 }
2053
Robert Sloana27a6a42017-09-05 08:39:28 -07002054 hs->early_data_offered = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002055 return true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002056}
2057
Robert Sloan921ef2c2017-10-17 09:02:20 -07002058static bool ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloan0da43952018-01-03 15:13:14 -08002059 if (!hs->ssl->s3->early_data_accepted) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002060 return true;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002061 }
2062
2063 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2064 !CBB_add_u16(out, 0) ||
2065 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002066 return false;
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002067 }
2068
Robert Sloan921ef2c2017-10-17 09:02:20 -07002069 return true;
David Benjamin1b249672016-12-06 18:25:50 -05002070}
2071
2072
Robert Sloana27a6a42017-09-05 08:39:28 -07002073// Key Share
2074//
2075// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5
David Benjaminc895d6b2016-08-11 13:26:41 -04002076
Robert Sloan921ef2c2017-10-17 09:02:20 -07002077static bool ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05002078 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -07002079 if (hs->max_version < TLS1_3_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002080 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04002081 }
2082
2083 CBB contents, kse_bytes;
Robert Sloan8542c082018-02-05 09:07:34 -08002084 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002085 !CBB_add_u16_length_prefixed(out, &contents) ||
2086 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002087 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002088 }
2089
David Benjamin1b249672016-12-06 18:25:50 -05002090 uint16_t group_id = hs->retry_group;
2091 if (hs->received_hello_retry_request) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002092 // We received a HelloRetryRequest without a new curve, so there is no new
2093 // share to append. Leave |hs->key_share| as-is.
Steven Valdez909b19f2016-11-21 15:35:44 -05002094 if (group_id == 0 &&
Robert Sloan4562e9d2017-10-02 10:26:51 -07002095 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(),
2096 hs->key_share_bytes.size())) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002097 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002098 }
Robert Sloan4562e9d2017-10-02 10:26:51 -07002099 hs->key_share_bytes.Reset();
David Benjamin95add822016-10-19 01:09:12 -04002100 if (group_id == 0) {
2101 return CBB_flush(out);
2102 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002103 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -07002104 // Add a fake group. See draft-davidben-tls-grease-01.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002105 if (ssl->ctx->grease_enabled &&
2106 (!CBB_add_u16(&kse_bytes,
Robert Sloan309a31e2018-01-29 10:22:47 -08002107 ssl_get_grease_value(hs, ssl_grease_group)) ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002108 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2109 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002110 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002111 }
2112
Robert Sloana27a6a42017-09-05 08:39:28 -07002113 // Predict the most preferred group.
Robert Sloan4562e9d2017-10-02 10:26:51 -07002114 Span<const uint16_t> groups = tls1_get_grouplist(ssl);
Robert Sloanae1abf92017-10-05 12:50:08 -07002115 if (groups.empty()) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04002116 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002117 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002118 }
David Benjamin7c0d06c2016-08-11 13:26:41 -04002119
2120 group_id = groups[0];
David Benjaminc895d6b2016-08-11 13:26:41 -04002121 }
2122
Robert Sloanb6d070c2017-07-24 08:40:01 -07002123 hs->key_share = SSLKeyShare::Create(group_id);
David Benjamin7c0d06c2016-08-11 13:26:41 -04002124 CBB key_exchange;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002125 if (!hs->key_share ||
2126 !CBB_add_u16(&kse_bytes, group_id) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04002127 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07002128 !hs->key_share->Offer(&key_exchange) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04002129 !CBB_flush(&kse_bytes)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002130 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002131 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002132
Robert Sloan4562e9d2017-10-02 10:26:51 -07002133 // Save the contents of the extension to repeat it in the second ClientHello.
2134 if (!hs->received_hello_retry_request &&
2135 !hs->key_share_bytes.CopyFrom(
2136 MakeConstSpan(CBB_data(&kse_bytes), CBB_len(&kse_bytes)))) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002137 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002138 }
2139
2140 return CBB_flush(out);
2141}
2142
Robert Sloan921ef2c2017-10-17 09:02:20 -07002143bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
2144 Array<uint8_t> *out_secret,
2145 uint8_t *out_alert, CBS *contents) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002146 CBS peer_key;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002147 uint16_t group_id;
2148 if (!CBS_get_u16(contents, &group_id) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002149 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2150 CBS_len(contents) != 0) {
Robert Sloan99319a12017-11-27 10:32:46 -08002151 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04002152 *out_alert = SSL_AD_DECODE_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002153 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002154 }
2155
Robert Sloanb6d070c2017-07-24 08:40:01 -07002156 if (hs->key_share->GroupID() != group_id) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002157 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2158 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002159 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002160 }
2161
Robert Sloan4562e9d2017-10-02 10:26:51 -07002162 if (!hs->key_share->Finish(out_secret, out_alert, peer_key)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002163 *out_alert = SSL_AD_INTERNAL_ERROR;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002164 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002165 }
2166
Robert Sloana94fe052017-02-21 08:49:28 -08002167 hs->new_session->group_id = group_id;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002168 hs->key_share.reset();
Robert Sloan921ef2c2017-10-17 09:02:20 -07002169 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04002170}
2171
Robert Sloan921ef2c2017-10-17 09:02:20 -07002172bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
2173 Array<uint8_t> *out_secret,
2174 uint8_t *out_alert, CBS *contents) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002175 uint16_t group_id;
2176 CBS key_shares;
David Benjamin1b249672016-12-06 18:25:50 -05002177 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002178 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2179 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002180 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002181 }
2182
2183 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002184 CBS_len(contents) != 0) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04002185 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002186 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002187 }
2188
Robert Sloana27a6a42017-09-05 08:39:28 -07002189 // Find the corresponding key share.
David Benjamin7c0d06c2016-08-11 13:26:41 -04002190 CBS peer_key;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002191 CBS_init(&peer_key, NULL, 0);
David Benjaminc895d6b2016-08-11 13:26:41 -04002192 while (CBS_len(&key_shares) > 0) {
2193 uint16_t id;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002194 CBS peer_key_tmp;
David Benjaminc895d6b2016-08-11 13:26:41 -04002195 if (!CBS_get_u16(&key_shares, &id) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07002196 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp) ||
2197 CBS_len(&peer_key_tmp) == 0) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04002198 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07002199 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002200 }
2201
David Benjamin7c0d06c2016-08-11 13:26:41 -04002202 if (id == group_id) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002203 if (CBS_len(&peer_key) != 0) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04002204 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2205 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002206 return false;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002207 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002208
David Benjamin7c0d06c2016-08-11 13:26:41 -04002209 peer_key = peer_key_tmp;
Robert Sloana27a6a42017-09-05 08:39:28 -07002210 // Continue parsing the structure to keep peers honest.
David Benjaminc895d6b2016-08-11 13:26:41 -04002211 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002212 }
2213
Robert Sloan921ef2c2017-10-17 09:02:20 -07002214 if (CBS_len(&peer_key) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002215 *out_found = false;
Robert Sloan4562e9d2017-10-02 10:26:51 -07002216 out_secret->Reset();
Robert Sloan921ef2c2017-10-17 09:02:20 -07002217 return true;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002218 }
2219
Robert Sloana27a6a42017-09-05 08:39:28 -07002220 // Compute the DH secret.
Robert Sloan4562e9d2017-10-02 10:26:51 -07002221 Array<uint8_t> secret;
Robert Sloanb6d070c2017-07-24 08:40:01 -07002222 ScopedCBB public_key;
2223 UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
2224 if (!key_share ||
2225 !CBB_init(public_key.get(), 32) ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07002226 !key_share->Accept(public_key.get(), &secret, out_alert, peer_key) ||
2227 !CBBFinishArray(public_key.get(), &hs->ecdh_public_key)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002228 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002229 return false;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002230 }
2231
Robert Sloan4562e9d2017-10-02 10:26:51 -07002232 *out_secret = std::move(secret);
Robert Sloana27a6a42017-09-05 08:39:28 -07002233 *out_found = true;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002234 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04002235}
2236
Robert Sloan921ef2c2017-10-17 09:02:20 -07002237bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002238 uint16_t group_id;
2239 CBB kse_bytes, public_key;
David Benjamin1b249672016-12-06 18:25:50 -05002240 if (!tls1_get_shared_group(hs, &group_id) ||
Robert Sloan8542c082018-02-05 09:07:34 -08002241 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002242 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2243 !CBB_add_u16(&kse_bytes, group_id) ||
2244 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07002245 !CBB_add_bytes(&public_key, hs->ecdh_public_key.data(),
2246 hs->ecdh_public_key.size()) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002247 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002248 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04002249 }
2250
Robert Sloan4562e9d2017-10-02 10:26:51 -07002251 hs->ecdh_public_key.Reset();
David Benjamin7c0d06c2016-08-11 13:26:41 -04002252
Robert Sloana94fe052017-02-21 08:49:28 -08002253 hs->new_session->group_id = group_id;
Robert Sloan921ef2c2017-10-17 09:02:20 -07002254 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04002255}
2256
2257
Robert Sloana27a6a42017-09-05 08:39:28 -07002258// Supported Versions
2259//
2260// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002261
Robert Sloan921ef2c2017-10-17 09:02:20 -07002262static bool ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05002263 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -07002264 if (hs->max_version <= TLS1_2_VERSION) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002265 return true;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002266 }
2267
2268 CBB contents, versions;
2269 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2270 !CBB_add_u16_length_prefixed(out, &contents) ||
2271 !CBB_add_u8_length_prefixed(&contents, &versions)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002272 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002273 }
2274
Robert Sloana27a6a42017-09-05 08:39:28 -07002275 // Add a fake version. See draft-davidben-tls-grease-01.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002276 if (ssl->ctx->grease_enabled &&
Robert Sloan309a31e2018-01-29 10:22:47 -08002277 !CBB_add_u16(&versions, ssl_get_grease_value(hs, ssl_grease_version))) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002278 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002279 }
2280
Robert Sloanf6200e72017-07-10 08:09:18 -07002281 if (!ssl_add_supported_versions(hs, &versions) ||
2282 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002283 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002284 }
2285
Robert Sloan921ef2c2017-10-17 09:02:20 -07002286 return true;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002287}
2288
2289
Robert Sloana27a6a42017-09-05 08:39:28 -07002290// Cookie
2291//
2292// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2
David Benjamin95add822016-10-19 01:09:12 -04002293
Robert Sloan921ef2c2017-10-17 09:02:20 -07002294static bool ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloanae1abf92017-10-05 12:50:08 -07002295 if (hs->cookie.empty()) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002296 return true;
David Benjamin95add822016-10-19 01:09:12 -04002297 }
2298
2299 CBB contents, cookie;
2300 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2301 !CBB_add_u16_length_prefixed(out, &contents) ||
2302 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07002303 !CBB_add_bytes(&cookie, hs->cookie.data(), hs->cookie.size()) ||
David Benjamin95add822016-10-19 01:09:12 -04002304 !CBB_flush(out)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002305 return false;
David Benjamin95add822016-10-19 01:09:12 -04002306 }
2307
Robert Sloana27a6a42017-09-05 08:39:28 -07002308 // The cookie is no longer needed in memory.
Robert Sloan4562e9d2017-10-02 10:26:51 -07002309 hs->cookie.Reset();
Robert Sloan921ef2c2017-10-17 09:02:20 -07002310 return true;
David Benjamin95add822016-10-19 01:09:12 -04002311}
2312
2313
Robert Sloan0db7f542018-01-16 15:48:33 -08002314// Dummy PQ Padding extension
2315//
2316// Dummy post-quantum padding invovles the client (and later server) sending
2317// useless, random-looking bytes in an extension in their ClientHello or
2318// ServerHello. These extensions are sized to simulate a post-quantum
2319// key-exchange and so enable measurement of the latency impact of the
2320// additional bandwidth.
2321
Robert Sloanab8b8882018-03-26 11:39:51 -07002322static bool ext_dummy_pq_padding_add(CBB *out, size_t len) {
Robert Sloan0db7f542018-01-16 15:48:33 -08002323 CBB contents;
2324 uint8_t *buffer;
2325 if (!CBB_add_u16(out, TLSEXT_TYPE_dummy_pq_padding) ||
2326 !CBB_add_u16_length_prefixed(out, &contents) ||
2327 !CBB_add_space(&contents, &buffer, len)) {
2328 return false;
2329 }
2330
2331 // The length is used as the nonce so that different length extensions have
2332 // different contents. There's no reason this has to be the case, it just
2333 // makes things a little more obvious in a packet dump.
2334 uint8_t nonce[12] = {0};
2335 memcpy(nonce, &len, sizeof(len));
2336
2337 memset(buffer, 0, len);
2338 static const uint8_t kZeroKey[32] = {0};
2339 CRYPTO_chacha_20(buffer, buffer, len, kZeroKey, nonce, 0);
2340
2341 return CBB_flush(out);
2342}
2343
Robert Sloanab8b8882018-03-26 11:39:51 -07002344static bool ext_dummy_pq_padding_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2345 const size_t len = hs->ssl->dummy_pq_padding_len;
2346 if (len == 0) {
2347 return true;
2348 }
2349
2350 return ext_dummy_pq_padding_add(out, len);
2351}
2352
2353static bool ext_dummy_pq_padding_parse_serverhello(SSL_HANDSHAKE *hs,
2354 uint8_t *out_alert,
2355 CBS *contents) {
2356 if (contents == nullptr) {
2357 return true;
2358 }
2359
2360 if (CBS_len(contents) != hs->ssl->dummy_pq_padding_len) {
2361 return false;
2362 }
2363
2364 hs->ssl->did_dummy_pq_padding = true;
2365 return true;
2366}
2367
2368static bool ext_dummy_pq_padding_parse_clienthello(SSL_HANDSHAKE *hs,
2369 uint8_t *out_alert,
2370 CBS *contents) {
2371 if (contents != nullptr &&
2372 0 < CBS_len(contents) && CBS_len(contents) < (1 << 12)) {
2373 hs->dummy_pq_padding_len = CBS_len(contents);
2374 }
2375
2376 return true;
2377}
2378
2379static bool ext_dummy_pq_padding_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2380 if (!hs->dummy_pq_padding_len) {
2381 return true;
2382 }
2383
2384 return ext_dummy_pq_padding_add(out, hs->dummy_pq_padding_len);
2385}
Robert Sloan0db7f542018-01-16 15:48:33 -08002386
Robert Sloana27a6a42017-09-05 08:39:28 -07002387// Negotiated Groups
2388//
2389// https://tools.ietf.org/html/rfc4492#section-5.1.2
2390// https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4
Kenny Rootb8494592015-09-25 02:29:14 +00002391
Robert Sloan921ef2c2017-10-17 09:02:20 -07002392static bool ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin1b249672016-12-06 18:25:50 -05002393 SSL *const ssl = hs->ssl;
David Benjamind316cba2016-06-02 16:17:39 -04002394 CBB contents, groups_bytes;
2395 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Kenny Rootb8494592015-09-25 02:29:14 +00002396 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamind316cba2016-06-02 16:17:39 -04002397 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002398 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00002399 }
2400
Robert Sloana27a6a42017-09-05 08:39:28 -07002401 // Add a fake group. See draft-davidben-tls-grease-01.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002402 if (ssl->ctx->grease_enabled &&
2403 !CBB_add_u16(&groups_bytes,
Robert Sloan309a31e2018-01-29 10:22:47 -08002404 ssl_get_grease_value(hs, ssl_grease_group))) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002405 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002406 }
2407
Robert Sloan4562e9d2017-10-02 10:26:51 -07002408 for (uint16_t group : tls1_get_grouplist(ssl)) {
2409 if (!CBB_add_u16(&groups_bytes, group)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002410 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00002411 }
2412 }
2413
2414 return CBB_flush(out);
2415}
2416
Robert Sloan921ef2c2017-10-17 09:02:20 -07002417static bool ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2418 uint8_t *out_alert,
2419 CBS *contents) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002420 // This extension is not expected to be echoed by servers in TLS 1.2, but some
2421 // BigIP servers send it nonetheless, so do not enforce this.
Robert Sloan921ef2c2017-10-17 09:02:20 -07002422 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00002423}
2424
Robert Sloan4562e9d2017-10-02 10:26:51 -07002425static bool parse_u16_array(const CBS *cbs, Array<uint16_t> *out) {
2426 CBS copy = *cbs;
2427 if ((CBS_len(&copy) & 1) != 0) {
2428 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2429 return false;
2430 }
2431
2432 Array<uint16_t> ret;
2433 if (!ret.Init(CBS_len(&copy) / 2)) {
2434 return false;
2435 }
2436 for (size_t i = 0; i < ret.size(); i++) {
2437 if (!CBS_get_u16(&copy, &ret[i])) {
2438 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2439 return false;
2440 }
2441 }
2442
2443 assert(CBS_len(&copy) == 0);
2444 *out = std::move(ret);
2445 return 1;
2446}
2447
Robert Sloan921ef2c2017-10-17 09:02:20 -07002448static bool ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin1b249672016-12-06 18:25:50 -05002449 uint8_t *out_alert,
Robert Sloan921ef2c2017-10-17 09:02:20 -07002450 CBS *contents) {
Kenny Rootb8494592015-09-25 02:29:14 +00002451 if (contents == NULL) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002452 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00002453 }
2454
David Benjamind316cba2016-06-02 16:17:39 -04002455 CBS supported_group_list;
2456 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2457 CBS_len(&supported_group_list) == 0 ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07002458 CBS_len(contents) != 0 ||
2459 !parse_u16_array(&supported_group_list, &hs->peer_supported_group_list)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07002460 return false;
Kenny Rootb8494592015-09-25 02:29:14 +00002461 }
2462
Robert Sloan921ef2c2017-10-17 09:02:20 -07002463 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00002464}
2465
Robert Sloan978112c2018-01-22 12:53:01 -08002466// Token Binding
2467//
2468// https://tools.ietf.org/html/draft-ietf-tokbind-negotiation-10
2469
2470// The Token Binding version number currently matches the draft number of
2471// draft-ietf-tokbind-protocol, and when published as an RFC it will be 0x0100.
2472// Since there are no wire changes to the protocol from draft 13 through the
2473// current draft (16), this implementation supports all versions in that range.
2474static uint16_t kTokenBindingMaxVersion = 16;
2475static uint16_t kTokenBindingMinVersion = 13;
2476
2477static bool ext_token_binding_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2478 SSL *const ssl = hs->ssl;
2479 if (ssl->token_binding_params == nullptr || SSL_is_dtls(ssl)) {
2480 return true;
2481 }
2482
2483 CBB contents, params;
2484 if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) ||
2485 !CBB_add_u16_length_prefixed(out, &contents) ||
2486 !CBB_add_u16(&contents, kTokenBindingMaxVersion) ||
2487 !CBB_add_u8_length_prefixed(&contents, &params) ||
2488 !CBB_add_bytes(&params, ssl->token_binding_params,
2489 ssl->token_binding_params_len) ||
2490 !CBB_flush(out)) {
2491 return false;
2492 }
2493
2494 return true;
2495}
2496
2497static bool ext_token_binding_parse_serverhello(SSL_HANDSHAKE *hs,
2498 uint8_t *out_alert,
2499 CBS *contents) {
2500 SSL *const ssl = hs->ssl;
2501 if (contents == nullptr) {
2502 return true;
2503 }
2504
2505 CBS params_list;
2506 uint16_t version;
2507 uint8_t param;
2508 if (!CBS_get_u16(contents, &version) ||
2509 !CBS_get_u8_length_prefixed(contents, &params_list) ||
2510 !CBS_get_u8(&params_list, &param) ||
2511 CBS_len(&params_list) > 0 ||
2512 CBS_len(contents) > 0) {
2513 *out_alert = SSL_AD_DECODE_ERROR;
2514 return false;
2515 }
2516
2517 // The server-negotiated version must be less than or equal to our version.
2518 if (version > kTokenBindingMaxVersion) {
2519 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2520 return false;
2521 }
2522
2523 // If the server-selected version is less than what we support, then Token
2524 // Binding wasn't negotiated (but the extension was parsed successfully).
2525 if (version < kTokenBindingMinVersion) {
2526 return true;
2527 }
2528
2529 for (size_t i = 0; i < ssl->token_binding_params_len; ++i) {
2530 if (param == ssl->token_binding_params[i]) {
2531 ssl->negotiated_token_binding_param = param;
2532 ssl->token_binding_negotiated = true;
2533 return true;
2534 }
2535 }
2536
2537 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2538 return false;
2539}
2540
2541// select_tb_param looks for the first token binding param in
2542// |ssl->token_binding_params| that is also in |params| and puts it in
2543// |ssl->negotiated_token_binding_param|. It returns true if a token binding
2544// param is found, and false otherwise.
2545static bool select_tb_param(SSL *ssl, Span<const uint8_t> peer_params) {
2546 for (size_t i = 0; i < ssl->token_binding_params_len; ++i) {
2547 uint8_t tb_param = ssl->token_binding_params[i];
2548 for (uint8_t peer_param : peer_params) {
2549 if (tb_param == peer_param) {
2550 ssl->negotiated_token_binding_param = tb_param;
2551 return true;
2552 }
2553 }
2554 }
2555 return false;
2556}
2557
2558static bool ext_token_binding_parse_clienthello(SSL_HANDSHAKE *hs,
2559 uint8_t *out_alert,
2560 CBS *contents) {
2561 SSL *const ssl = hs->ssl;
2562 if (contents == nullptr || ssl->token_binding_params == nullptr) {
2563 return true;
2564 }
2565
2566 CBS params;
2567 uint16_t version;
2568 if (!CBS_get_u16(contents, &version) ||
2569 !CBS_get_u8_length_prefixed(contents, &params) ||
2570 CBS_len(&params) == 0 ||
2571 CBS_len(contents) > 0) {
2572 *out_alert = SSL_AD_DECODE_ERROR;
2573 return false;
2574 }
2575
2576 // If the client-selected version is less than what we support, then Token
2577 // Binding wasn't negotiated (but the extension was parsed successfully).
2578 if (version < kTokenBindingMinVersion) {
2579 return true;
2580 }
2581
2582 // If the client-selected version is higher than we support, use our max
2583 // version. Otherwise, use the client's version.
2584 hs->negotiated_token_binding_version =
2585 std::min(version, kTokenBindingMaxVersion);
2586 if (!select_tb_param(ssl, params)) {
2587 return true;
2588 }
2589
2590 ssl->token_binding_negotiated = true;
2591 return true;
2592}
2593
2594static bool ext_token_binding_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2595 SSL *const ssl = hs->ssl;
2596
2597 if (!ssl->token_binding_negotiated) {
2598 return true;
2599 }
2600
2601 CBB contents, params;
2602 if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) ||
2603 !CBB_add_u16_length_prefixed(out, &contents) ||
2604 !CBB_add_u16(&contents, hs->negotiated_token_binding_version) ||
2605 !CBB_add_u8_length_prefixed(&contents, &params) ||
2606 !CBB_add_u8(&params, ssl->negotiated_token_binding_param) ||
2607 !CBB_flush(out)) {
2608 return false;
2609 }
2610
2611 return true;
2612}
Kenny Rootb8494592015-09-25 02:29:14 +00002613
Robert Sloan8542c082018-02-05 09:07:34 -08002614// QUIC Transport Parameters
2615
2616static bool ext_quic_transport_params_add_clienthello(SSL_HANDSHAKE *hs,
2617 CBB *out) {
2618 SSL *const ssl = hs->ssl;
2619 if (!ssl->quic_transport_params || hs->max_version <= TLS1_2_VERSION) {
2620 return true;
2621 }
2622
2623 CBB contents;
2624 if (!CBB_add_u16(out, TLSEXT_TYPE_quic_transport_parameters) ||
2625 !CBB_add_u16_length_prefixed(out, &contents) ||
2626 !CBB_add_bytes(&contents, ssl->quic_transport_params,
2627 ssl->quic_transport_params_len) ||
2628 !CBB_flush(out)) {
2629 return false;
2630 }
2631 return true;
2632}
2633
2634static bool ext_quic_transport_params_parse_serverhello(SSL_HANDSHAKE *hs,
2635 uint8_t *out_alert,
2636 CBS *contents) {
2637 SSL *const ssl = hs->ssl;
2638 if (contents == nullptr) {
2639 return true;
2640 }
2641 // QUIC requires TLS 1.3.
2642 if (ssl_protocol_version(ssl) < TLS1_3_VERSION) {
2643 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2644 return false;
2645 }
2646
2647 return ssl->s3->peer_quic_transport_params.CopyFrom(*contents);
2648}
2649
2650static bool ext_quic_transport_params_parse_clienthello(SSL_HANDSHAKE *hs,
2651 uint8_t *out_alert,
2652 CBS *contents) {
2653 SSL *const ssl = hs->ssl;
2654 if (!contents || !ssl->quic_transport_params) {
2655 return true;
2656 }
2657 // Ignore the extension before TLS 1.3.
2658 if (ssl_protocol_version(ssl) < TLS1_3_VERSION) {
2659 return true;
2660 }
2661
2662 return ssl->s3->peer_quic_transport_params.CopyFrom(*contents);
2663}
2664
2665static bool ext_quic_transport_params_add_serverhello(SSL_HANDSHAKE *hs,
2666 CBB *out) {
2667 SSL *const ssl = hs->ssl;
2668 if (!ssl->quic_transport_params) {
2669 return true;
2670 }
2671
2672 CBB contents;
2673 if (!CBB_add_u16(out, TLSEXT_TYPE_quic_transport_parameters) ||
2674 !CBB_add_u16_length_prefixed(out, &contents) ||
2675 !CBB_add_bytes(&contents, ssl->quic_transport_params,
2676 ssl->quic_transport_params_len) ||
2677 !CBB_flush(out)) {
2678 return false;
2679 }
2680
2681 return true;
2682}
2683
2684
Robert Sloana27a6a42017-09-05 08:39:28 -07002685// kExtensions contains all the supported extensions.
Kenny Rootb8494592015-09-25 02:29:14 +00002686static const struct tls_extension kExtensions[] = {
2687 {
Kenny Rootb8494592015-09-25 02:29:14 +00002688 TLSEXT_TYPE_renegotiate,
2689 NULL,
2690 ext_ri_add_clienthello,
2691 ext_ri_parse_serverhello,
2692 ext_ri_parse_clienthello,
2693 ext_ri_add_serverhello,
2694 },
2695 {
2696 TLSEXT_TYPE_server_name,
David Benjamin95add822016-10-19 01:09:12 -04002697 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002698 ext_sni_add_clienthello,
2699 ext_sni_parse_serverhello,
2700 ext_sni_parse_clienthello,
2701 ext_sni_add_serverhello,
2702 },
2703 {
2704 TLSEXT_TYPE_extended_master_secret,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002705 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002706 ext_ems_add_clienthello,
2707 ext_ems_parse_serverhello,
2708 ext_ems_parse_clienthello,
2709 ext_ems_add_serverhello,
2710 },
2711 {
2712 TLSEXT_TYPE_session_ticket,
2713 NULL,
2714 ext_ticket_add_clienthello,
2715 ext_ticket_parse_serverhello,
Robert Sloana27a6a42017-09-05 08:39:28 -07002716 // Ticket extension client parsing is handled in ssl_session.c
David Benjaminc895d6b2016-08-11 13:26:41 -04002717 ignore_parse_clienthello,
Kenny Rootb8494592015-09-25 02:29:14 +00002718 ext_ticket_add_serverhello,
2719 },
2720 {
2721 TLSEXT_TYPE_signature_algorithms,
2722 NULL,
2723 ext_sigalgs_add_clienthello,
David Benjaminc895d6b2016-08-11 13:26:41 -04002724 forbid_parse_serverhello,
Kenny Rootb8494592015-09-25 02:29:14 +00002725 ext_sigalgs_parse_clienthello,
David Benjaminc895d6b2016-08-11 13:26:41 -04002726 dont_add_serverhello,
Kenny Rootb8494592015-09-25 02:29:14 +00002727 },
2728 {
2729 TLSEXT_TYPE_status_request,
Steven Valdezb0b45c62017-01-17 16:23:54 -05002730 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002731 ext_ocsp_add_clienthello,
2732 ext_ocsp_parse_serverhello,
2733 ext_ocsp_parse_clienthello,
2734 ext_ocsp_add_serverhello,
2735 },
2736 {
2737 TLSEXT_TYPE_next_proto_neg,
David Benjamin95add822016-10-19 01:09:12 -04002738 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002739 ext_npn_add_clienthello,
2740 ext_npn_parse_serverhello,
2741 ext_npn_parse_clienthello,
2742 ext_npn_add_serverhello,
2743 },
2744 {
2745 TLSEXT_TYPE_certificate_timestamp,
2746 NULL,
2747 ext_sct_add_clienthello,
2748 ext_sct_parse_serverhello,
2749 ext_sct_parse_clienthello,
2750 ext_sct_add_serverhello,
2751 },
2752 {
2753 TLSEXT_TYPE_application_layer_protocol_negotiation,
Steven Valdez909b19f2016-11-21 15:35:44 -05002754 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002755 ext_alpn_add_clienthello,
2756 ext_alpn_parse_serverhello,
Robert Sloana27a6a42017-09-05 08:39:28 -07002757 // ALPN is negotiated late in |ssl_negotiate_alpn|.
Steven Valdez909b19f2016-11-21 15:35:44 -05002758 ignore_parse_clienthello,
Kenny Rootb8494592015-09-25 02:29:14 +00002759 ext_alpn_add_serverhello,
2760 },
2761 {
2762 TLSEXT_TYPE_channel_id,
2763 ext_channel_id_init,
2764 ext_channel_id_add_clienthello,
2765 ext_channel_id_parse_serverhello,
2766 ext_channel_id_parse_clienthello,
2767 ext_channel_id_add_serverhello,
2768 },
2769 {
2770 TLSEXT_TYPE_srtp,
2771 ext_srtp_init,
2772 ext_srtp_add_clienthello,
2773 ext_srtp_parse_serverhello,
2774 ext_srtp_parse_clienthello,
2775 ext_srtp_add_serverhello,
2776 },
2777 {
2778 TLSEXT_TYPE_ec_point_formats,
2779 NULL,
2780 ext_ec_point_add_clienthello,
2781 ext_ec_point_parse_serverhello,
2782 ext_ec_point_parse_clienthello,
2783 ext_ec_point_add_serverhello,
2784 },
David Benjaminc895d6b2016-08-11 13:26:41 -04002785 {
Robert Sloan8542c082018-02-05 09:07:34 -08002786 TLSEXT_TYPE_key_share,
David Benjaminc895d6b2016-08-11 13:26:41 -04002787 NULL,
2788 ext_key_share_add_clienthello,
2789 forbid_parse_serverhello,
2790 ignore_parse_clienthello,
2791 dont_add_serverhello,
2792 },
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002793 {
Steven Valdez909b19f2016-11-21 15:35:44 -05002794 TLSEXT_TYPE_psk_key_exchange_modes,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002795 NULL,
Steven Valdez909b19f2016-11-21 15:35:44 -05002796 ext_psk_key_exchange_modes_add_clienthello,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002797 forbid_parse_serverhello,
Steven Valdez909b19f2016-11-21 15:35:44 -05002798 ext_psk_key_exchange_modes_parse_clienthello,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002799 dont_add_serverhello,
2800 },
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002801 {
David Benjamin1b249672016-12-06 18:25:50 -05002802 TLSEXT_TYPE_early_data,
2803 NULL,
2804 ext_early_data_add_clienthello,
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002805 ext_early_data_parse_serverhello,
David Benjamin1b249672016-12-06 18:25:50 -05002806 ext_early_data_parse_clienthello,
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002807 ext_early_data_add_serverhello,
David Benjamin1b249672016-12-06 18:25:50 -05002808 },
2809 {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002810 TLSEXT_TYPE_supported_versions,
2811 NULL,
2812 ext_supported_versions_add_clienthello,
2813 forbid_parse_serverhello,
2814 ignore_parse_clienthello,
2815 dont_add_serverhello,
2816 },
David Benjamin95add822016-10-19 01:09:12 -04002817 {
2818 TLSEXT_TYPE_cookie,
2819 NULL,
2820 ext_cookie_add_clienthello,
2821 forbid_parse_serverhello,
2822 ignore_parse_clienthello,
2823 dont_add_serverhello,
2824 },
Robert Sloan0db7f542018-01-16 15:48:33 -08002825 {
2826 TLSEXT_TYPE_dummy_pq_padding,
2827 NULL,
2828 ext_dummy_pq_padding_add_clienthello,
Robert Sloanab8b8882018-03-26 11:39:51 -07002829 ext_dummy_pq_padding_parse_serverhello,
2830 ext_dummy_pq_padding_parse_clienthello,
2831 ext_dummy_pq_padding_add_serverhello,
Robert Sloan0db7f542018-01-16 15:48:33 -08002832 },
Robert Sloan8542c082018-02-05 09:07:34 -08002833 {
2834 TLSEXT_TYPE_quic_transport_parameters,
2835 NULL,
2836 ext_quic_transport_params_add_clienthello,
2837 ext_quic_transport_params_parse_serverhello,
2838 ext_quic_transport_params_parse_clienthello,
2839 ext_quic_transport_params_add_serverhello,
2840 },
Robert Sloana27a6a42017-09-05 08:39:28 -07002841 // The final extension must be non-empty. WebSphere Application Server 7.0 is
2842 // intolerant to the last extension being zero-length. See
2843 // https://crbug.com/363583.
Kenny Rootb8494592015-09-25 02:29:14 +00002844 {
David Benjamind316cba2016-06-02 16:17:39 -04002845 TLSEXT_TYPE_supported_groups,
David Benjamin95add822016-10-19 01:09:12 -04002846 NULL,
David Benjamind316cba2016-06-02 16:17:39 -04002847 ext_supported_groups_add_clienthello,
2848 ext_supported_groups_parse_serverhello,
2849 ext_supported_groups_parse_clienthello,
Robert Sloan921ef2c2017-10-17 09:02:20 -07002850 dont_add_serverhello,
Kenny Rootb8494592015-09-25 02:29:14 +00002851 },
Robert Sloan978112c2018-01-22 12:53:01 -08002852 {
2853 TLSEXT_TYPE_token_binding,
2854 NULL,
2855 ext_token_binding_add_clienthello,
2856 ext_token_binding_parse_serverhello,
2857 ext_token_binding_parse_clienthello,
2858 ext_token_binding_add_serverhello,
2859 },
Kenny Rootb8494592015-09-25 02:29:14 +00002860};
2861
2862#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2863
Robert Sloana12bf462017-07-17 07:08:26 -07002864static_assert(kNumExtensions <=
2865 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
2866 "too many extensions for sent bitset");
2867static_assert(kNumExtensions <=
2868 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2869 "too many extensions for received bitset");
Kenny Rootb8494592015-09-25 02:29:14 +00002870
2871static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2872 uint16_t value) {
2873 unsigned i;
2874 for (i = 0; i < kNumExtensions; i++) {
2875 if (kExtensions[i].value == value) {
2876 *out_index = i;
2877 return &kExtensions[i];
2878 }
2879 }
2880
2881 return NULL;
2882}
2883
David Benjamin1b249672016-12-06 18:25:50 -05002884int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2885 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07002886 // Don't add extensions for SSLv3 unless doing secure renegotiation.
David Benjamin1b249672016-12-06 18:25:50 -05002887 if (hs->client_version == SSL3_VERSION &&
Kenny Roote99801b2015-11-06 15:31:15 -08002888 !ssl->s3->send_connection_binding) {
2889 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002890 }
2891
Kenny Roote99801b2015-11-06 15:31:15 -08002892 CBB extensions;
2893 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Robert Sloana12bf462017-07-17 07:08:26 -07002894 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2895 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002896 }
2897
David Benjamin1b249672016-12-06 18:25:50 -05002898 hs->extensions.sent = 0;
2899 hs->custom_extensions.sent = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002900
David Benjamin7c0d06c2016-08-11 13:26:41 -04002901 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00002902 if (kExtensions[i].init != NULL) {
David Benjamin1b249672016-12-06 18:25:50 -05002903 kExtensions[i].init(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002904 }
2905 }
2906
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002907 uint16_t grease_ext1 = 0;
2908 if (ssl->ctx->grease_enabled) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002909 // Add a fake empty extension. See draft-davidben-tls-grease-01.
Robert Sloan309a31e2018-01-29 10:22:47 -08002910 grease_ext1 = ssl_get_grease_value(hs, ssl_grease_extension1);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002911 if (!CBB_add_u16(&extensions, grease_ext1) ||
2912 !CBB_add_u16(&extensions, 0 /* zero length */)) {
Robert Sloana12bf462017-07-17 07:08:26 -07002913 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2914 return 0;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002915 }
2916 }
2917
David Benjamin7c0d06c2016-08-11 13:26:41 -04002918 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00002919 const size_t len_before = CBB_len(&extensions);
David Benjamin1b249672016-12-06 18:25:50 -05002920 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002921 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05002922 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Robert Sloana12bf462017-07-17 07:08:26 -07002923 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002924 }
Kenny Rootb8494592015-09-25 02:29:14 +00002925
2926 if (CBB_len(&extensions) != len_before) {
David Benjamin1b249672016-12-06 18:25:50 -05002927 hs->extensions.sent |= (1u << i);
Kenny Rootb8494592015-09-25 02:29:14 +00002928 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002929 }
2930
David Benjamin1b249672016-12-06 18:25:50 -05002931 if (!custom_ext_add_clienthello(hs, &extensions)) {
Robert Sloana12bf462017-07-17 07:08:26 -07002932 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2933 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002934 }
2935
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002936 if (ssl->ctx->grease_enabled) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002937 // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
Robert Sloan309a31e2018-01-29 10:22:47 -08002938 uint16_t grease_ext2 = ssl_get_grease_value(hs, ssl_grease_extension2);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002939
Robert Sloana27a6a42017-09-05 08:39:28 -07002940 // The two fake extensions must not have the same value. GREASE values are
2941 // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2942 // one.
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002943 if (grease_ext1 == grease_ext2) {
2944 grease_ext2 ^= 0x1010;
2945 }
2946
2947 if (!CBB_add_u16(&extensions, grease_ext2) ||
2948 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2949 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
Robert Sloana12bf462017-07-17 07:08:26 -07002950 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2951 return 0;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002952 }
2953 }
2954
David Benjaminc895d6b2016-08-11 13:26:41 -04002955 if (!SSL_is_dtls(ssl)) {
David Benjamin1b249672016-12-06 18:25:50 -05002956 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdez909b19f2016-11-21 15:35:44 -05002957 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002958 if (header_len > 0xff && header_len < 0x200) {
Robert Sloana27a6a42017-09-05 08:39:28 -07002959 // Add padding to workaround bugs in F5 terminators. See RFC 7685.
2960 //
2961 // NB: because this code works out the length of all existing extensions
2962 // it MUST always appear last.
Kenny Rootb8494592015-09-25 02:29:14 +00002963 size_t padding_len = 0x200 - header_len;
Robert Sloana27a6a42017-09-05 08:39:28 -07002964 // Extensions take at least four bytes to encode. Always include at least
2965 // one byte of data if including the extension. WebSphere Application
2966 // Server 7.0 is intolerant to the last extension being zero-length. See
2967 // https://crbug.com/363583.
Adam Langleyd9e397b2015-01-22 14:27:53 -08002968 if (padding_len >= 4 + 1) {
2969 padding_len -= 4;
2970 } else {
2971 padding_len = 1;
2972 }
2973
Kenny Rootb8494592015-09-25 02:29:14 +00002974 uint8_t *padding_bytes;
2975 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2976 !CBB_add_u16(&extensions, padding_len) ||
2977 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
Robert Sloana12bf462017-07-17 07:08:26 -07002978 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2979 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002980 }
2981
Robert Sloan69939df2017-01-09 10:53:07 -08002982 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002983 }
2984 }
2985
Robert Sloana27a6a42017-09-05 08:39:28 -07002986 // The PSK extension must be last, including after the padding.
David Benjamin1b249672016-12-06 18:25:50 -05002987 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Robert Sloana12bf462017-07-17 07:08:26 -07002988 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2989 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -05002990 }
2991
Robert Sloana27a6a42017-09-05 08:39:28 -07002992 // Discard empty extensions blocks.
Adam Langley4139edb2016-01-13 15:00:54 -08002993 if (CBB_len(&extensions) == 0) {
Kenny Roote99801b2015-11-06 15:31:15 -08002994 CBB_discard_child(out);
Kenny Rootb8494592015-09-25 02:29:14 +00002995 }
2996
Kenny Roote99801b2015-11-06 15:31:15 -08002997 return CBB_flush(out);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002998}
2999
David Benjamin1b249672016-12-06 18:25:50 -05003000int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
3001 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -08003002 CBB extensions;
3003 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003004 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003005 }
3006
David Benjamin1b249672016-12-06 18:25:50 -05003007 for (unsigned i = 0; i < kNumExtensions; i++) {
3008 if (!(hs->extensions.received & (1u << i))) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003009 // Don't send extensions that were not received.
Kenny Rootb8494592015-09-25 02:29:14 +00003010 continue;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003011 }
3012
David Benjamin1b249672016-12-06 18:25:50 -05003013 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003014 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003015 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Kenny Rootb8494592015-09-25 02:29:14 +00003016 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003017 }
3018 }
3019
David Benjamin1b249672016-12-06 18:25:50 -05003020 if (!custom_ext_add_serverhello(hs, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003021 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003022 }
3023
Robert Sloana27a6a42017-09-05 08:39:28 -07003024 // Discard empty extensions blocks before TLS 1.3.
Robert Sloan921ef2c2017-10-17 09:02:20 -07003025 if (ssl_protocol_version(ssl) < TLS1_3_VERSION &&
David Benjaminc895d6b2016-08-11 13:26:41 -04003026 CBB_len(&extensions) == 0) {
Kenny Roote99801b2015-11-06 15:31:15 -08003027 CBB_discard_child(out);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003028 }
3029
Kenny Roote99801b2015-11-06 15:31:15 -08003030 return CBB_flush(out);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003031
Kenny Rootb8494592015-09-25 02:29:14 +00003032err:
Kenny Rootb8494592015-09-25 02:29:14 +00003033 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Kenny Roote99801b2015-11-06 15:31:15 -08003034 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003035}
3036
David Benjamin1b249672016-12-06 18:25:50 -05003037static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
3038 const SSL_CLIENT_HELLO *client_hello,
3039 int *out_alert) {
3040 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04003041 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00003042 if (kExtensions[i].init != NULL) {
David Benjamin1b249672016-12-06 18:25:50 -05003043 kExtensions[i].init(hs);
Kenny Rootb8494592015-09-25 02:29:14 +00003044 }
3045 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003046
David Benjamin1b249672016-12-06 18:25:50 -05003047 hs->extensions.received = 0;
3048 hs->custom_extensions.received = 0;
David Benjaminc895d6b2016-08-11 13:26:41 -04003049 CBS extensions;
3050 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
3051 while (CBS_len(&extensions) != 0) {
3052 uint16_t type;
3053 CBS extension;
3054
Robert Sloana27a6a42017-09-05 08:39:28 -07003055 // Decode the next extension.
David Benjaminc895d6b2016-08-11 13:26:41 -04003056 if (!CBS_get_u16(&extensions, &type) ||
3057 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08003058 *out_alert = SSL_AD_DECODE_ERROR;
3059 return 0;
3060 }
3061
Robert Sloana27a6a42017-09-05 08:39:28 -07003062 // RFC 5746 made the existence of extensions in SSL 3.0 somewhat
3063 // ambiguous. Ignore all but the renegotiation_info extension.
David Benjaminc895d6b2016-08-11 13:26:41 -04003064 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
3065 continue;
3066 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003067
David Benjaminc895d6b2016-08-11 13:26:41 -04003068 unsigned ext_index;
3069 const struct tls_extension *const ext =
3070 tls_extension_find(&ext_index, type);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003071
David Benjaminc895d6b2016-08-11 13:26:41 -04003072 if (ext == NULL) {
David Benjamin1b249672016-12-06 18:25:50 -05003073 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003074 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003075 return 0;
3076 }
David Benjaminc895d6b2016-08-11 13:26:41 -04003077 continue;
3078 }
3079
David Benjamin1b249672016-12-06 18:25:50 -05003080 hs->extensions.received |= (1u << ext_index);
David Benjaminc895d6b2016-08-11 13:26:41 -04003081 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003082 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003083 *out_alert = alert;
3084 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003085 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjaminc895d6b2016-08-11 13:26:41 -04003086 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003087 }
3088 }
3089
David Benjaminc895d6b2016-08-11 13:26:41 -04003090 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin1b249672016-12-06 18:25:50 -05003091 if (hs->extensions.received & (1u << i)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003092 continue;
3093 }
3094
3095 CBS *contents = NULL, fake_contents;
3096 static const uint8_t kFakeRenegotiateExtension[] = {0};
3097 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
3098 ssl_client_cipher_list_contains_cipher(client_hello,
3099 SSL3_CK_SCSV & 0xffff)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003100 // The renegotiation SCSV was received so pretend that we received a
3101 // renegotiation extension.
David Benjaminc895d6b2016-08-11 13:26:41 -04003102 CBS_init(&fake_contents, kFakeRenegotiateExtension,
3103 sizeof(kFakeRenegotiateExtension));
3104 contents = &fake_contents;
David Benjamin1b249672016-12-06 18:25:50 -05003105 hs->extensions.received |= (1u << i);
David Benjaminc895d6b2016-08-11 13:26:41 -04003106 }
3107
Robert Sloana27a6a42017-09-05 08:39:28 -07003108 // Extension wasn't observed so call the callback with a NULL
3109 // parameter.
David Benjaminc895d6b2016-08-11 13:26:41 -04003110 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003111 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003112 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003113 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjaminc895d6b2016-08-11 13:26:41 -04003114 *out_alert = alert;
3115 return 0;
Kenny Rootb8494592015-09-25 02:29:14 +00003116 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003117 }
3118
3119 return 1;
3120}
3121
David Benjamin1b249672016-12-06 18:25:50 -05003122int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
3123 const SSL_CLIENT_HELLO *client_hello) {
3124 SSL *const ssl = hs->ssl;
Robert Sloan5d625782017-02-13 09:55:39 -08003125 int alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003126 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07003127 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003128 return 0;
3129 }
3130
David Benjamin1b249672016-12-06 18:25:50 -05003131 if (ssl_check_clienthello_tlsext(hs) <= 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00003132 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003133 return 0;
3134 }
3135
3136 return 1;
3137}
3138
David Benjamin1b249672016-12-06 18:25:50 -05003139static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
3140 int *out_alert) {
3141 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07003142 // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty.
Robert Sloan921ef2c2017-10-17 09:02:20 -07003143 if (CBS_len(cbs) == 0 && ssl_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003144 return 1;
3145 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003146
Robert Sloana27a6a42017-09-05 08:39:28 -07003147 // Decode the extensions block and check it is valid.
David Benjaminc895d6b2016-08-11 13:26:41 -04003148 CBS extensions;
3149 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
3150 !tls1_check_duplicate_extensions(&extensions)) {
3151 *out_alert = SSL_AD_DECODE_ERROR;
3152 return 0;
3153 }
3154
3155 uint32_t received = 0;
3156 while (CBS_len(&extensions) != 0) {
3157 uint16_t type;
3158 CBS extension;
3159
Robert Sloana27a6a42017-09-05 08:39:28 -07003160 // Decode the next extension.
David Benjaminc895d6b2016-08-11 13:26:41 -04003161 if (!CBS_get_u16(&extensions, &type) ||
3162 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Kenny Roota04d78d2015-09-25 00:26:37 +00003163 *out_alert = SSL_AD_DECODE_ERROR;
3164 return 0;
3165 }
3166
David Benjaminc895d6b2016-08-11 13:26:41 -04003167 unsigned ext_index;
3168 const struct tls_extension *const ext =
3169 tls_extension_find(&ext_index, type);
Kenny Rootb8494592015-09-25 02:29:14 +00003170
David Benjaminc895d6b2016-08-11 13:26:41 -04003171 if (ext == NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003172 hs->received_custom_extension = true;
David Benjamin1b249672016-12-06 18:25:50 -05003173 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Kenny Roota04d78d2015-09-25 00:26:37 +00003174 return 0;
3175 }
David Benjaminc895d6b2016-08-11 13:26:41 -04003176 continue;
3177 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003178
Robert Sloana12bf462017-07-17 07:08:26 -07003179 static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
3180 "too many bits");
David Benjaminf31229b2017-01-25 14:08:15 -05003181
David Benjamin1b249672016-12-06 18:25:50 -05003182 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjaminc895d6b2016-08-11 13:26:41 -04003183 type != TLSEXT_TYPE_renegotiate) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003184 // If the extension was never sent then it is illegal, except for the
3185 // renegotiation extension which, in SSL 3.0, is signaled via SCSV.
David Benjaminc895d6b2016-08-11 13:26:41 -04003186 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3187 ERR_add_error_dataf("extension :%u", (unsigned)type);
3188 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
3189 return 0;
3190 }
Kenny Rootb8494592015-09-25 02:29:14 +00003191
David Benjaminc895d6b2016-08-11 13:26:41 -04003192 received |= (1u << ext_index);
Kenny Roota04d78d2015-09-25 00:26:37 +00003193
David Benjaminc895d6b2016-08-11 13:26:41 -04003194 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003195 if (!ext->parse_serverhello(hs, &alert, &extension)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003196 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003197 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjaminc895d6b2016-08-11 13:26:41 -04003198 *out_alert = alert;
3199 return 0;
Kenny Roota04d78d2015-09-25 00:26:37 +00003200 }
3201 }
3202
David Benjamin7c0d06c2016-08-11 13:26:41 -04003203 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00003204 if (!(received & (1u << i))) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003205 // Extension wasn't observed so call the callback with a NULL
3206 // parameter.
Kenny Rootb8494592015-09-25 02:29:14 +00003207 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003208 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003209 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003210 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Kenny Rootb8494592015-09-25 02:29:14 +00003211 *out_alert = alert;
Kenny Roota04d78d2015-09-25 00:26:37 +00003212 return 0;
3213 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003214 }
3215 }
3216
Kenny Roota04d78d2015-09-25 00:26:37 +00003217 return 1;
3218}
3219
David Benjamin1b249672016-12-06 18:25:50 -05003220static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3221 SSL *const ssl = hs->ssl;
Robert Sloan978112c2018-01-22 12:53:01 -08003222
3223 if (ssl->token_binding_negotiated &&
3224 !(SSL_get_secure_renegotiation_support(ssl) &&
3225 SSL_get_extms_support(ssl))) {
3226 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_TB_WITHOUT_EMS_OR_RI);
3227 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
3228 return -1;
3229 }
3230
Adam Langleyd9e397b2015-01-22 14:27:53 -08003231 int ret = SSL_TLSEXT_ERR_NOACK;
3232 int al = SSL_AD_UNRECOGNIZED_NAME;
3233
David Benjamin4969cc92016-04-22 15:02:23 -04003234 if (ssl->ctx->tlsext_servername_callback != 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08003235 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin4969cc92016-04-22 15:02:23 -04003236 ssl->ctx->tlsext_servername_arg);
Robert Sloan1c9db532017-03-13 08:03:59 -07003237 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
3238 ret = ssl->session_ctx->tlsext_servername_callback(
3239 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003240 }
3241
3242 switch (ret) {
3243 case SSL_TLSEXT_ERR_ALERT_FATAL:
Robert Sloan921ef2c2017-10-17 09:02:20 -07003244 ssl_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003245 return -1;
3246
Adam Langleyd9e397b2015-01-22 14:27:53 -08003247 case SSL_TLSEXT_ERR_NOACK:
Robert Sloana27a6a42017-09-05 08:39:28 -07003248 hs->should_ack_sni = false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003249 return 1;
3250
3251 default:
3252 return 1;
3253 }
3254}
3255
David Benjamin1b249672016-12-06 18:25:50 -05003256int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3257 SSL *const ssl = hs->ssl;
Robert Sloan5d625782017-02-13 09:55:39 -08003258 int alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003259 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07003260 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003261 return 0;
3262 }
3263
Adam Langleyd9e397b2015-01-22 14:27:53 -08003264 return 1;
3265}
3266
Robert Sloan8f860b12017-08-28 07:37:06 -07003267static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
3268 uint8_t **out, size_t *out_len, EVP_CIPHER_CTX *cipher_ctx,
3269 HMAC_CTX *hmac_ctx, const uint8_t *ticket, size_t ticket_len) {
3270 size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003271
Robert Sloana27a6a42017-09-05 08:39:28 -07003272 // Check the MAC at the end of the ticket.
Kenny Rootb8494592015-09-25 02:29:14 +00003273 uint8_t mac[EVP_MAX_MD_SIZE];
Robert Sloan8f860b12017-08-28 07:37:06 -07003274 size_t mac_len = HMAC_size(hmac_ctx);
Kenny Rootb8494592015-09-25 02:29:14 +00003275 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003276 // The ticket must be large enough for key name, IV, data, and MAC.
Robert Sloana12bf462017-07-17 07:08:26 -07003277 return ssl_ticket_aead_ignore_ticket;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003278 }
Robert Sloan8f860b12017-08-28 07:37:06 -07003279 HMAC_Update(hmac_ctx, ticket, ticket_len - mac_len);
3280 HMAC_Final(hmac_ctx, mac, NULL);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003281 int mac_ok =
3282 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3283#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3284 mac_ok = 1;
3285#endif
3286 if (!mac_ok) {
Robert Sloana12bf462017-07-17 07:08:26 -07003287 return ssl_ticket_aead_ignore_ticket;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003288 }
3289
Robert Sloana27a6a42017-09-05 08:39:28 -07003290 // Decrypt the session data.
Kenny Rootb8494592015-09-25 02:29:14 +00003291 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3292 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3293 mac_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -07003294 UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len));
Robert Sloana12bf462017-07-17 07:08:26 -07003295 if (!plaintext) {
3296 return ssl_ticket_aead_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003297 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003298 size_t plaintext_len;
3299#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
Robert Sloana12bf462017-07-17 07:08:26 -07003300 OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003301 plaintext_len = ciphertext_len;
3302#else
Kenny Rootb8494592015-09-25 02:29:14 +00003303 if (ciphertext_len >= INT_MAX) {
Robert Sloana12bf462017-07-17 07:08:26 -07003304 return ssl_ticket_aead_ignore_ticket;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003305 }
Kenny Rootb8494592015-09-25 02:29:14 +00003306 int len1, len2;
Robert Sloan8f860b12017-08-28 07:37:06 -07003307 if (!EVP_DecryptUpdate(cipher_ctx, plaintext.get(), &len1, ciphertext,
Kenny Rootb8494592015-09-25 02:29:14 +00003308 (int)ciphertext_len) ||
Robert Sloan8f860b12017-08-28 07:37:06 -07003309 !EVP_DecryptFinal_ex(cipher_ctx, plaintext.get() + len1, &len2)) {
Robert Sloan1c9db532017-03-13 08:03:59 -07003310 ERR_clear_error();
Robert Sloana12bf462017-07-17 07:08:26 -07003311 return ssl_ticket_aead_ignore_ticket;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003312 }
Robert Sloan1c9db532017-03-13 08:03:59 -07003313 plaintext_len = (size_t)(len1) + len2;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003314#endif
Adam Langleyd9e397b2015-01-22 14:27:53 -08003315
Robert Sloana12bf462017-07-17 07:08:26 -07003316 *out = plaintext.release();
Robert Sloan1c9db532017-03-13 08:03:59 -07003317 *out_len = plaintext_len;
Robert Sloana12bf462017-07-17 07:08:26 -07003318 return ssl_ticket_aead_success;
Robert Sloan1c9db532017-03-13 08:03:59 -07003319}
3320
Robert Sloan8f860b12017-08-28 07:37:06 -07003321static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
Robert Sloana27a6a42017-09-05 08:39:28 -07003322 SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
Robert Sloan8f860b12017-08-28 07:37:06 -07003323 const uint8_t *ticket, size_t ticket_len) {
3324 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3325 ScopedEVP_CIPHER_CTX cipher_ctx;
3326 ScopedHMAC_CTX hmac_ctx;
3327 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3328 int cb_ret = ssl->session_ctx->tlsext_ticket_key_cb(
3329 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(),
3330 hmac_ctx.get(), 0 /* decrypt */);
3331 if (cb_ret < 0) {
3332 return ssl_ticket_aead_error;
3333 } else if (cb_ret == 0) {
3334 return ssl_ticket_aead_ignore_ticket;
3335 } else if (cb_ret == 2) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003336 *out_renew_ticket = true;
Robert Sloan8f860b12017-08-28 07:37:06 -07003337 } else {
3338 assert(cb_ret == 1);
3339 }
3340 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3341 hmac_ctx.get(), ticket, ticket_len);
3342}
3343
3344static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
3345 SSL *ssl, uint8_t **out, size_t *out_len, const uint8_t *ticket,
3346 size_t ticket_len) {
3347 assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
3348 SSL_CTX *ctx = ssl->session_ctx;
3349
Robert Sloana27a6a42017-09-05 08:39:28 -07003350 // Rotate the ticket key if necessary.
Robert Sloan8f860b12017-08-28 07:37:06 -07003351 if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
3352 return ssl_ticket_aead_error;
3353 }
3354
Robert Sloana27a6a42017-09-05 08:39:28 -07003355 // Pick the matching ticket key and decrypt.
Robert Sloan8f860b12017-08-28 07:37:06 -07003356 ScopedEVP_CIPHER_CTX cipher_ctx;
3357 ScopedHMAC_CTX hmac_ctx;
3358 {
3359 MutexReadLock lock(&ctx->lock);
3360 const tlsext_ticket_key *key;
3361 if (ctx->tlsext_ticket_key_current &&
3362 !OPENSSL_memcmp(ctx->tlsext_ticket_key_current->name, ticket,
3363 SSL_TICKET_KEY_NAME_LEN)) {
3364 key = ctx->tlsext_ticket_key_current;
3365 } else if (ctx->tlsext_ticket_key_prev &&
3366 !OPENSSL_memcmp(ctx->tlsext_ticket_key_prev->name, ticket,
3367 SSL_TICKET_KEY_NAME_LEN)) {
3368 key = ctx->tlsext_ticket_key_prev;
3369 } else {
3370 return ssl_ticket_aead_ignore_ticket;
3371 }
3372 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
3373 if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
3374 tlsext_tick_md(), NULL) ||
3375 !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL,
3376 key->aes_key, iv)) {
3377 return ssl_ticket_aead_error;
3378 }
3379 }
3380 return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
3381 hmac_ctx.get(), ticket, ticket_len);
3382}
3383
Robert Sloan1c9db532017-03-13 08:03:59 -07003384static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
Robert Sloana27a6a42017-09-05 08:39:28 -07003385 SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket,
Robert Sloan1c9db532017-03-13 08:03:59 -07003386 const uint8_t *ticket, size_t ticket_len) {
Robert Sloana12bf462017-07-17 07:08:26 -07003387 uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len);
Robert Sloan1c9db532017-03-13 08:03:59 -07003388 if (plaintext == NULL) {
3389 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3390 return ssl_ticket_aead_error;
3391 }
3392
3393 size_t plaintext_len;
3394 const enum ssl_ticket_aead_result_t result =
3395 ssl->session_ctx->ticket_aead_method->open(
3396 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3397
3398 if (result == ssl_ticket_aead_success) {
3399 *out = plaintext;
3400 plaintext = NULL;
3401 *out_len = plaintext_len;
3402 }
3403
3404 OPENSSL_free(plaintext);
3405 return result;
3406}
3407
3408enum ssl_ticket_aead_result_t ssl_process_ticket(
Robert Sloana27a6a42017-09-05 08:39:28 -07003409 SSL *ssl, UniquePtr<SSL_SESSION> *out_session, bool *out_renew_ticket,
Robert Sloan1c9db532017-03-13 08:03:59 -07003410 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3411 size_t session_id_len) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003412 *out_renew_ticket = false;
Robert Sloanfe7cd212017-08-07 09:03:39 -07003413 out_session->reset();
Robert Sloan1c9db532017-03-13 08:03:59 -07003414
3415 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3416 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3417 return ssl_ticket_aead_ignore_ticket;
3418 }
3419
3420 uint8_t *plaintext = NULL;
3421 size_t plaintext_len;
3422 enum ssl_ticket_aead_result_t result;
3423 if (ssl->session_ctx->ticket_aead_method != NULL) {
3424 result = ssl_decrypt_ticket_with_method(
3425 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3426 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -07003427 // Ensure there is room for the key name and the largest IV
3428 // |tlsext_ticket_key_cb| may try to consume. The real limit may be lower,
3429 // but the maximum IV length should be well under the minimum size for the
3430 // session material and HMAC.
Robert Sloan8f860b12017-08-28 07:37:06 -07003431 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3432 return ssl_ticket_aead_ignore_ticket;
3433 }
3434 if (ssl->session_ctx->tlsext_ticket_key_cb != NULL) {
3435 result = ssl_decrypt_ticket_with_cb(ssl, &plaintext, &plaintext_len,
3436 out_renew_ticket, ticket, ticket_len);
3437 } else {
3438 result = ssl_decrypt_ticket_with_ticket_keys(
3439 ssl, &plaintext, &plaintext_len, ticket, ticket_len);
3440 }
Robert Sloan1c9db532017-03-13 08:03:59 -07003441 }
3442
3443 if (result != ssl_ticket_aead_success) {
3444 return result;
3445 }
3446
Robert Sloana27a6a42017-09-05 08:39:28 -07003447 // Decode the session.
Robert Sloanfe7cd212017-08-07 09:03:39 -07003448 UniquePtr<SSL_SESSION> session(
3449 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx));
Robert Sloan1c9db532017-03-13 08:03:59 -07003450 OPENSSL_free(plaintext);
3451
Robert Sloanfe7cd212017-08-07 09:03:39 -07003452 if (!session) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003453 ERR_clear_error(); // Don't leave an error on the queue.
Robert Sloan1c9db532017-03-13 08:03:59 -07003454 return ssl_ticket_aead_ignore_ticket;
Kenny Rootb8494592015-09-25 02:29:14 +00003455 }
3456
Robert Sloana27a6a42017-09-05 08:39:28 -07003457 // Copy the client's session ID into the new session, to denote the ticket has
3458 // been accepted.
Robert Sloan69939df2017-01-09 10:53:07 -08003459 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
Kenny Rootb8494592015-09-25 02:29:14 +00003460 session->session_id_length = session_id_len;
3461
Robert Sloanfe7cd212017-08-07 09:03:39 -07003462 *out_session = std::move(session);
Robert Sloan1c9db532017-03-13 08:03:59 -07003463 return ssl_ticket_aead_success;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003464}
3465
Robert Sloan309a31e2018-01-29 10:22:47 -08003466bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003467 // Extension ignored for inappropriate versions
Robert Sloan921ef2c2017-10-17 09:02:20 -07003468 if (ssl_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Robert Sloan309a31e2018-01-29 10:22:47 -08003469 return true;
Kenny Roota04d78d2015-09-25 00:26:37 +00003470 }
3471
Robert Sloan4562e9d2017-10-02 10:26:51 -07003472 return parse_u16_array(in_sigalgs, &hs->peer_sigalgs);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003473}
3474
Robert Sloan309a31e2018-01-29 10:22:47 -08003475bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
Robert Sloan2424d842017-05-01 07:46:28 -07003476 switch (EVP_PKEY_id(pkey)) {
3477 case EVP_PKEY_RSA:
3478 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
Robert Sloan309a31e2018-01-29 10:22:47 -08003479 return true;
Robert Sloan2424d842017-05-01 07:46:28 -07003480 case EVP_PKEY_EC:
3481 *out = SSL_SIGN_ECDSA_SHA1;
Robert Sloan309a31e2018-01-29 10:22:47 -08003482 return true;
Robert Sloan2424d842017-05-01 07:46:28 -07003483 default:
Robert Sloan309a31e2018-01-29 10:22:47 -08003484 return false;
Robert Sloan2424d842017-05-01 07:46:28 -07003485 }
3486}
3487
Robert Sloan309a31e2018-01-29 10:22:47 -08003488bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
David Benjamin1b249672016-12-06 18:25:50 -05003489 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00003490 CERT *cert = ssl->cert;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003491
Robert Sloana27a6a42017-09-05 08:39:28 -07003492 // Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3493 // handshake.
Robert Sloan921ef2c2017-10-17 09:02:20 -07003494 if (ssl_protocol_version(ssl) < TLS1_2_VERSION) {
Robert Sloanb6d070c2017-07-24 08:40:01 -07003495 if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
Robert Sloan2424d842017-05-01 07:46:28 -07003496 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
Robert Sloan309a31e2018-01-29 10:22:47 -08003497 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -04003498 }
Robert Sloan309a31e2018-01-29 10:22:47 -08003499 return true;
Kenny Rootb8494592015-09-25 02:29:14 +00003500 }
3501
Robert Sloan4562e9d2017-10-02 10:26:51 -07003502 Span<const uint16_t> sigalgs = kSignSignatureAlgorithms;
3503 if (cert->sigalgs != nullptr) {
3504 sigalgs = MakeConstSpan(cert->sigalgs, cert->num_sigalgs);
David Benjaminc895d6b2016-08-11 13:26:41 -04003505 }
Kenny Rootb8494592015-09-25 02:29:14 +00003506
Robert Sloan4562e9d2017-10-02 10:26:51 -07003507 Span<const uint16_t> peer_sigalgs = hs->peer_sigalgs;
Robert Sloan921ef2c2017-10-17 09:02:20 -07003508 if (peer_sigalgs.empty() && ssl_protocol_version(ssl) < TLS1_3_VERSION) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003509 // If the client didn't specify any signature_algorithms extension then
3510 // we can assume that it supports SHA1. See
3511 // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
David Benjaminc895d6b2016-08-11 13:26:41 -04003512 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3513 SSL_SIGN_ECDSA_SHA1};
3514 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjaminc895d6b2016-08-11 13:26:41 -04003515 }
3516
Robert Sloan4562e9d2017-10-02 10:26:51 -07003517 for (uint16_t sigalg : sigalgs) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003518 // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3519 // negotiated.
David Benjaminc895d6b2016-08-11 13:26:41 -04003520 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07003521 !ssl_private_key_supports_signature_algorithm(hs, sigalg)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003522 continue;
3523 }
3524
Robert Sloan4562e9d2017-10-02 10:26:51 -07003525 for (uint16_t peer_sigalg : peer_sigalgs) {
3526 if (sigalg == peer_sigalg) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003527 *out = sigalg;
Robert Sloan309a31e2018-01-29 10:22:47 -08003528 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -04003529 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003530 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003531 }
3532
David Benjaminc895d6b2016-08-11 13:26:41 -04003533 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
Robert Sloan309a31e2018-01-29 10:22:47 -08003534 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003535}
3536
Robert Sloan84377092017-08-14 09:33:19 -07003537int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
Robert Sloan5d625782017-02-13 09:55:39 -08003538 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07003539 // A Channel ID handshake message is structured to contain multiple
3540 // extensions, but the only one that can be present is Channel ID.
Robert Sloan84377092017-08-14 09:33:19 -07003541 uint16_t extension_type;
3542 CBS channel_id = msg.body, extension;
Steven Valdez909b19f2016-11-21 15:35:44 -05003543 if (!CBS_get_u16(&channel_id, &extension_type) ||
3544 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3545 CBS_len(&channel_id) != 0 ||
3546 extension_type != TLSEXT_TYPE_channel_id ||
3547 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3548 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07003549 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez909b19f2016-11-21 15:35:44 -05003550 return 0;
3551 }
3552
Robert Sloanb6d070c2017-07-24 08:40:01 -07003553 UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
Steven Valdez909b19f2016-11-21 15:35:44 -05003554 if (!p256) {
3555 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3556 return 0;
3557 }
3558
Robert Sloanb6d070c2017-07-24 08:40:01 -07003559 UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
3560 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
Robert Sloana12bf462017-07-17 07:08:26 -07003561 if (!sig || !x || !y) {
3562 return 0;
Adam Langley1e4884f2015-09-24 10:57:52 -07003563 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003564
Steven Valdez909b19f2016-11-21 15:35:44 -05003565 const uint8_t *p = CBS_data(&extension);
Robert Sloana12bf462017-07-17 07:08:26 -07003566 if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
3567 BN_bin2bn(p + 32, 32, y.get()) == NULL ||
3568 BN_bin2bn(p + 64, 32, sig->r) == NULL ||
3569 BN_bin2bn(p + 96, 32, sig->s) == NULL) {
3570 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -05003571 }
3572
Robert Sloanb6d070c2017-07-24 08:40:01 -07003573 UniquePtr<EC_KEY> key(EC_KEY_new());
3574 UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
Robert Sloana12bf462017-07-17 07:08:26 -07003575 if (!key || !point ||
3576 !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
3577 y.get(), nullptr) ||
3578 !EC_KEY_set_group(key.get(), p256.get()) ||
3579 !EC_KEY_set_public_key(key.get(), point.get())) {
3580 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -05003581 }
3582
3583 uint8_t digest[EVP_MAX_MD_SIZE];
3584 size_t digest_len;
Robert Sloan5d625782017-02-13 09:55:39 -08003585 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Robert Sloana12bf462017-07-17 07:08:26 -07003586 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -05003587 }
3588
Robert Sloana12bf462017-07-17 07:08:26 -07003589 int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
Steven Valdez909b19f2016-11-21 15:35:44 -05003590#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3591 sig_ok = 1;
Robert Sloan0da43952018-01-03 15:13:14 -08003592 ERR_clear_error();
Steven Valdez909b19f2016-11-21 15:35:44 -05003593#endif
3594 if (!sig_ok) {
3595 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
Robert Sloan921ef2c2017-10-17 09:02:20 -07003596 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07003597 ssl->s3->tlsext_channel_id_valid = false;
Robert Sloana12bf462017-07-17 07:08:26 -07003598 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -05003599 }
3600
Robert Sloan69939df2017-01-09 10:53:07 -08003601 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
Robert Sloana12bf462017-07-17 07:08:26 -07003602 return 1;
Steven Valdez909b19f2016-11-21 15:35:44 -05003603}
3604
Robert Sloanb1b54b82017-11-06 13:50:02 -08003605bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
Robert Sloan5d625782017-02-13 09:55:39 -08003606 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -05003607 uint8_t digest[EVP_MAX_MD_SIZE];
3608 size_t digest_len;
Robert Sloan5d625782017-02-13 09:55:39 -08003609 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Robert Sloanb1b54b82017-11-06 13:50:02 -08003610 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003611 }
3612
3613 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
Robert Sloanb1b54b82017-11-06 13:50:02 -08003614 if (ec_key == nullptr) {
Steven Valdez909b19f2016-11-21 15:35:44 -05003615 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloanb1b54b82017-11-06 13:50:02 -08003616 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003617 }
3618
Robert Sloanb1b54b82017-11-06 13:50:02 -08003619 UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
3620 if (!x || !y ||
Steven Valdez909b19f2016-11-21 15:35:44 -05003621 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3622 EC_KEY_get0_public_key(ec_key),
Robert Sloanb1b54b82017-11-06 13:50:02 -08003623 x.get(), y.get(), nullptr)) {
3624 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003625 }
3626
Robert Sloanb1b54b82017-11-06 13:50:02 -08003627 UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign(digest, digest_len, ec_key));
3628 if (!sig) {
3629 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003630 }
3631
3632 CBB child;
3633 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3634 !CBB_add_u16_length_prefixed(cbb, &child) ||
Robert Sloanb1b54b82017-11-06 13:50:02 -08003635 !BN_bn2cbb_padded(&child, 32, x.get()) ||
3636 !BN_bn2cbb_padded(&child, 32, y.get()) ||
Steven Valdez909b19f2016-11-21 15:35:44 -05003637 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3638 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3639 !CBB_flush(cbb)) {
Robert Sloanb1b54b82017-11-06 13:50:02 -08003640 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -05003641 }
3642
Robert Sloanb1b54b82017-11-06 13:50:02 -08003643 return true;
Steven Valdez909b19f2016-11-21 15:35:44 -05003644}
3645
Robert Sloan5d625782017-02-13 09:55:39 -08003646int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3647 SSL *const ssl = hs->ssl;
Robert Sloan921ef2c2017-10-17 09:02:20 -07003648 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
3649 Array<uint8_t> msg;
3650 if (!tls13_get_cert_verify_signature_input(hs, &msg,
Steven Valdez909b19f2016-11-21 15:35:44 -05003651 ssl_cert_verify_channel_id)) {
3652 return 0;
3653 }
Robert Sloan921ef2c2017-10-17 09:02:20 -07003654 SHA256(msg.data(), msg.size(), out);
Steven Valdez909b19f2016-11-21 15:35:44 -05003655 *out_len = SHA256_DIGEST_LENGTH;
Steven Valdez909b19f2016-11-21 15:35:44 -05003656 return 1;
3657 }
3658
3659 SHA256_CTX ctx;
3660
3661 SHA256_Init(&ctx);
Kenny Rootb8494592015-09-25 02:29:14 +00003662 static const char kClientIDMagic[] = "TLS Channel ID signature";
Steven Valdez909b19f2016-11-21 15:35:44 -05003663 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
Kenny Rootb8494592015-09-25 02:29:14 +00003664
David Benjaminc895d6b2016-08-11 13:26:41 -04003665 if (ssl->session != NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00003666 static const char kResumptionMagic[] = "Resumption";
Steven Valdez909b19f2016-11-21 15:35:44 -05003667 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
Kenny Rootb8494592015-09-25 02:29:14 +00003668 if (ssl->session->original_handshake_hash_len == 0) {
3669 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez909b19f2016-11-21 15:35:44 -05003670 return 0;
Kenny Rootb8494592015-09-25 02:29:14 +00003671 }
Steven Valdez909b19f2016-11-21 15:35:44 -05003672 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3673 ssl->session->original_handshake_hash_len);
Kenny Rootb8494592015-09-25 02:29:14 +00003674 }
3675
Robert Sloan5d625782017-02-13 09:55:39 -08003676 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3677 size_t hs_hash_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -07003678 if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
Steven Valdez909b19f2016-11-21 15:35:44 -05003679 return 0;
Kenny Rootb8494592015-09-25 02:29:14 +00003680 }
Robert Sloan5d625782017-02-13 09:55:39 -08003681 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Steven Valdez909b19f2016-11-21 15:35:44 -05003682 SHA256_Final(out, &ctx);
3683 *out_len = SHA256_DIGEST_LENGTH;
3684 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003685}
3686
Robert Sloana27a6a42017-09-05 08:39:28 -07003687// tls1_record_handshake_hashes_for_channel_id records the current handshake
3688// hashes in |hs->new_session| so that Channel ID resumptions can sign that
3689// data.
Robert Sloan5d625782017-02-13 09:55:39 -08003690int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3691 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07003692 // This function should never be called for a resumed session because the
3693 // handshake hashes that we wish to record are for the original, full
3694 // handshake.
David Benjaminc895d6b2016-08-11 13:26:41 -04003695 if (ssl->session != NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003696 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003697 }
3698
Robert Sloana12bf462017-07-17 07:08:26 -07003699 static_assert(
Robert Sloana94fe052017-02-21 08:49:28 -08003700 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
Robert Sloana12bf462017-07-17 07:08:26 -07003701 "original_handshake_hash is too small");
Robert Sloan5d625782017-02-13 09:55:39 -08003702
3703 size_t digest_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -07003704 if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
3705 &digest_len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003706 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003707 }
3708
Robert Sloana12bf462017-07-17 07:08:26 -07003709 static_assert(EVP_MAX_MD_SIZE <= 0xff,
3710 "EVP_MAX_MD_SIZE does not fit in uint8_t");
Robert Sloana94fe052017-02-21 08:49:28 -08003711 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003712
3713 return 1;
3714}
Steven Valdez909b19f2016-11-21 15:35:44 -05003715
3716int ssl_do_channel_id_callback(SSL *ssl) {
3717 if (ssl->tlsext_channel_id_private != NULL ||
3718 ssl->ctx->channel_id_cb == NULL) {
3719 return 1;
3720 }
3721
3722 EVP_PKEY *key = NULL;
3723 ssl->ctx->channel_id_cb(ssl, &key);
3724 if (key == NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003725 // The caller should try again later.
Steven Valdez909b19f2016-11-21 15:35:44 -05003726 return 1;
3727 }
3728
3729 int ret = SSL_set1_tls_channel_id(ssl, key);
3730 EVP_PKEY_free(key);
3731 return ret;
3732}
3733
3734int ssl_is_sct_list_valid(const CBS *contents) {
Robert Sloana27a6a42017-09-05 08:39:28 -07003735 // Shallow parse the SCT list for sanity. By the RFC
3736 // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3737 // of the SCTs may be empty.
Steven Valdez909b19f2016-11-21 15:35:44 -05003738 CBS copy = *contents;
3739 CBS sct_list;
3740 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3741 CBS_len(&copy) != 0 ||
3742 CBS_len(&sct_list) == 0) {
3743 return 0;
3744 }
3745
3746 while (CBS_len(&sct_list) > 0) {
3747 CBS sct;
3748 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3749 CBS_len(&sct) == 0) {
3750 return 0;
3751 }
3752 }
3753
3754 return 1;
3755}
Robert Sloanb6d070c2017-07-24 08:40:01 -07003756
3757} // namespace bssl
3758
3759using namespace bssl;
3760
3761int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
3762 uint16_t extension_type,
3763 const uint8_t **out_data,
3764 size_t *out_len) {
3765 CBS cbs;
3766 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
3767 return 0;
3768 }
3769
3770 *out_data = CBS_data(&cbs);
3771 *out_len = CBS_len(&cbs);
3772 return 1;
3773}
3774
3775void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
3776 ctx->ed25519_enabled = !!enabled;
3777}
3778
3779int SSL_extension_supported(unsigned extension_value) {
3780 uint32_t index;
3781 return extension_value == TLSEXT_TYPE_padding ||
3782 tls_extension_find(&index, extension_value) != NULL;
3783}