blob: 014432e7648b72253830eb7af267c2ab6dff9665 [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.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
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
116#include <openssl/bytestring.h>
Kenny Rootb8494592015-09-25 02:29:14 +0000117#include <openssl/digest.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700118#include <openssl/err.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800119#include <openssl/evp.h>
120#include <openssl/hmac.h>
121#include <openssl/mem.h>
David Benjamin4969cc92016-04-22 15:02:23 -0400122#include <openssl/nid.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800123#include <openssl/rand.h>
Kenny Roote99801b2015-11-06 15:31:15 -0800124#include <openssl/type_check.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800125
Adam Langleye9ada862015-05-11 17:20:37 -0700126#include "internal.h"
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400127#include "../crypto/internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800128
129
David Benjamin1b249672016-12-06 18:25:50 -0500130static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800131
Adam Langleyd9e397b2015-01-22 14:27:53 -0800132static int compare_uint16_t(const void *p1, const void *p2) {
133 uint16_t u1 = *((const uint16_t *)p1);
134 uint16_t u2 = *((const uint16_t *)p2);
135 if (u1 < u2) {
136 return -1;
137 } else if (u1 > u2) {
138 return 1;
139 } else {
140 return 0;
141 }
142}
143
144/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
145 * more than one extension of the same type in a ClientHello or ServerHello.
146 * This function does an initial scan over the extensions block to filter those
147 * out. */
148static int tls1_check_duplicate_extensions(const CBS *cbs) {
149 CBS extensions = *cbs;
150 size_t num_extensions = 0, i = 0;
151 uint16_t *extension_types = NULL;
152 int ret = 0;
153
154 /* First pass: count the extensions. */
155 while (CBS_len(&extensions) > 0) {
156 uint16_t type;
157 CBS extension;
158
159 if (!CBS_get_u16(&extensions, &type) ||
160 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
161 goto done;
162 }
163
164 num_extensions++;
165 }
166
167 if (num_extensions == 0) {
168 return 1;
169 }
170
David Benjamin4969cc92016-04-22 15:02:23 -0400171 extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800172 if (extension_types == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000173 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800174 goto done;
175 }
176
177 /* Second pass: gather the extension types. */
178 extensions = *cbs;
179 for (i = 0; i < num_extensions; i++) {
180 CBS extension;
181
182 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
183 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
184 /* This should not happen. */
185 goto done;
186 }
187 }
188 assert(CBS_len(&extensions) == 0);
189
190 /* Sort the extensions and make sure there are no duplicates. */
191 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
192 for (i = 1; i < num_extensions; i++) {
193 if (extension_types[i - 1] == extension_types[i]) {
194 goto done;
195 }
196 }
197
198 ret = 1;
199
200done:
Adam Langleye9ada862015-05-11 17:20:37 -0700201 OPENSSL_free(extension_types);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800202 return ret;
203}
204
David Benjamin1b249672016-12-06 18:25:50 -0500205int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out, const uint8_t *in,
206 size_t in_len) {
Robert Sloan69939df2017-01-09 10:53:07 -0800207 OPENSSL_memset(out, 0, sizeof(*out));
David Benjamin1b249672016-12-06 18:25:50 -0500208 out->ssl = ssl;
209 out->client_hello = in;
210 out->client_hello_len = in_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800211
David Benjaminc895d6b2016-08-11 13:26:41 -0400212 CBS client_hello, random, session_id;
David Benjamin1b249672016-12-06 18:25:50 -0500213 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
214 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400215 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
216 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
217 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800218 return 0;
219 }
220
David Benjamin1b249672016-12-06 18:25:50 -0500221 out->random = CBS_data(&random);
222 out->random_len = CBS_len(&random);
223 out->session_id = CBS_data(&session_id);
224 out->session_id_len = CBS_len(&session_id);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800225
226 /* Skip past DTLS cookie */
David Benjamin1b249672016-12-06 18:25:50 -0500227 if (SSL_is_dtls(out->ssl)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800228 CBS cookie;
David Benjaminc895d6b2016-08-11 13:26:41 -0400229 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
230 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800231 return 0;
232 }
233 }
234
David Benjaminc895d6b2016-08-11 13:26:41 -0400235 CBS cipher_suites, compression_methods;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800236 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400237 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
238 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -0800239 CBS_len(&compression_methods) < 1) {
240 return 0;
241 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400242
David Benjamin1b249672016-12-06 18:25:50 -0500243 out->cipher_suites = CBS_data(&cipher_suites);
244 out->cipher_suites_len = CBS_len(&cipher_suites);
245 out->compression_methods = CBS_data(&compression_methods);
246 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800247
248 /* If the ClientHello ends here then it's valid, but doesn't have any
249 * extensions. (E.g. SSLv3.) */
250 if (CBS_len(&client_hello) == 0) {
David Benjamin1b249672016-12-06 18:25:50 -0500251 out->extensions = NULL;
252 out->extensions_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800253 return 1;
254 }
255
256 /* Extract extensions and check it is valid. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400257 CBS extensions;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800258 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
259 !tls1_check_duplicate_extensions(&extensions) ||
260 CBS_len(&client_hello) != 0) {
261 return 0;
262 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400263
David Benjamin1b249672016-12-06 18:25:50 -0500264 out->extensions = CBS_data(&extensions);
265 out->extensions_len = CBS_len(&extensions);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800266
267 return 1;
268}
269
David Benjamin1b249672016-12-06 18:25:50 -0500270int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
271 CBS *out, uint16_t extension_type) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800272 CBS extensions;
David Benjamin1b249672016-12-06 18:25:50 -0500273 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800274 while (CBS_len(&extensions) != 0) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400275 /* Decode the next extension. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800276 uint16_t type;
277 CBS extension;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800278 if (!CBS_get_u16(&extensions, &type) ||
279 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
280 return 0;
281 }
282
283 if (type == extension_type) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400284 *out = extension;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800285 return 1;
286 }
287 }
288
289 return 0;
290}
291
David Benjamin1b249672016-12-06 18:25:50 -0500292int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
293 uint16_t extension_type,
294 const uint8_t **out_data,
295 size_t *out_len) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400296 CBS cbs;
David Benjamin1b249672016-12-06 18:25:50 -0500297 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400298 return 0;
299 }
300
301 *out_data = CBS_data(&cbs);
302 *out_len = CBS_len(&cbs);
303 return 1;
304}
305
David Benjamind316cba2016-06-02 16:17:39 -0400306static const uint16_t kDefaultGroups[] = {
David Benjaminc895d6b2016-08-11 13:26:41 -0400307 SSL_CURVE_X25519,
308 SSL_CURVE_SECP256R1,
309 SSL_CURVE_SECP384R1,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800310};
311
David Benjamin95add822016-10-19 01:09:12 -0400312void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
David Benjaminc895d6b2016-08-11 13:26:41 -0400313 size_t *out_group_ids_len) {
David Benjamind316cba2016-06-02 16:17:39 -0400314 *out_group_ids = ssl->supported_group_list;
315 *out_group_ids_len = ssl->supported_group_list_len;
316 if (!*out_group_ids) {
317 *out_group_ids = kDefaultGroups;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400318 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800319 }
320}
321
David Benjamin1b249672016-12-06 18:25:50 -0500322int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
323 SSL *const ssl = hs->ssl;
David Benjamin95add822016-10-19 01:09:12 -0400324 assert(ssl->server);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800325
David Benjamin95add822016-10-19 01:09:12 -0400326 const uint16_t *groups, *pref, *supp;
327 size_t groups_len, pref_len, supp_len;
328 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800329
David Benjamin95add822016-10-19 01:09:12 -0400330 /* Clients are not required to send a supported_groups extension. In this
331 * case, the server is free to pick any group it likes. See RFC 4492,
332 * section 4, paragraph 3.
333 *
334 * However, in the interests of compatibility, we will skip ECDH if the
335 * client didn't send an extension because we can't be sure that they'll
336 * support our favoured group. Thus we do not special-case an emtpy
337 * |peer_supported_group_list|. */
Adam Langleye9ada862015-05-11 17:20:37 -0700338
Adam Langley4139edb2016-01-13 15:00:54 -0800339 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
David Benjamind316cba2016-06-02 16:17:39 -0400340 pref = groups;
341 pref_len = groups_len;
David Benjamin1b249672016-12-06 18:25:50 -0500342 supp = hs->peer_supported_group_list;
343 supp_len = hs->peer_supported_group_list_len;
Adam Langleye9ada862015-05-11 17:20:37 -0700344 } else {
David Benjamin1b249672016-12-06 18:25:50 -0500345 pref = hs->peer_supported_group_list;
346 pref_len = hs->peer_supported_group_list_len;
David Benjamind316cba2016-06-02 16:17:39 -0400347 supp = groups;
348 supp_len = groups_len;
Adam Langleye9ada862015-05-11 17:20:37 -0700349 }
350
David Benjamin95add822016-10-19 01:09:12 -0400351 for (size_t i = 0; i < pref_len; i++) {
352 for (size_t j = 0; j < supp_len; j++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800353 if (pref[i] == supp[j]) {
David Benjamind316cba2016-06-02 16:17:39 -0400354 *out_group_id = pref[i];
Adam Langley4139edb2016-01-13 15:00:54 -0800355 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800356 }
357 }
358 }
359
Adam Langley4139edb2016-01-13 15:00:54 -0800360 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800361}
362
David Benjamind316cba2016-06-02 16:17:39 -0400363int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800364 const int *curves, size_t ncurves) {
David Benjamind316cba2016-06-02 16:17:39 -0400365 uint16_t *group_ids;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800366
David Benjamind316cba2016-06-02 16:17:39 -0400367 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
368 if (group_ids == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800369 return 0;
370 }
371
David Benjamin7c0d06c2016-08-11 13:26:41 -0400372 for (size_t i = 0; i < ncurves; i++) {
David Benjamind316cba2016-06-02 16:17:39 -0400373 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
374 OPENSSL_free(group_ids);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800375 return 0;
376 }
377 }
378
David Benjamind316cba2016-06-02 16:17:39 -0400379 OPENSSL_free(*out_group_ids);
380 *out_group_ids = group_ids;
381 *out_group_ids_len = ncurves;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800382
383 return 1;
384}
385
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400386int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
387 const char *curves) {
388 uint16_t *group_ids = NULL;
389 size_t ncurves = 0;
390
391 const char *col;
392 const char *ptr = curves;
393
394 do {
395 col = strchr(ptr, ':');
396
397 uint16_t group_id;
398 if (!ssl_name_to_group_id(&group_id, ptr,
399 col ? (size_t)(col - ptr) : strlen(ptr))) {
400 goto err;
401 }
402
403 uint16_t *new_group_ids = OPENSSL_realloc(group_ids,
404 (ncurves + 1) * sizeof(uint16_t));
405 if (new_group_ids == NULL) {
406 goto err;
407 }
408 group_ids = new_group_ids;
409
410 group_ids[ncurves] = group_id;
411 ncurves++;
412
413 if (col) {
414 ptr = col + 1;
415 }
416 } while (col);
417
418 OPENSSL_free(*out_group_ids);
419 *out_group_ids = group_ids;
420 *out_group_ids_len = ncurves;
421
422 return 1;
423
424err:
425 OPENSSL_free(group_ids);
426 return 0;
427}
428
David Benjamind316cba2016-06-02 16:17:39 -0400429int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
430 const uint16_t *groups;
David Benjamin95add822016-10-19 01:09:12 -0400431 size_t groups_len;
432 tls1_get_grouplist(ssl, &groups, &groups_len);
433 for (size_t i = 0; i < groups_len; i++) {
434 if (groups[i] == group_id) {
435 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800436 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800437 }
438
David Benjamin95add822016-10-19 01:09:12 -0400439 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800440}
441
David Benjamin95add822016-10-19 01:09:12 -0400442/* kVerifySignatureAlgorithms is the default list of accepted signature
Steven Valdez909b19f2016-11-21 15:35:44 -0500443 * algorithms for verifying.
444 *
445 * For now, RSA-PSS signature algorithms are not enabled on Android's system
446 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
447 * restore them. */
David Benjamin95add822016-10-19 01:09:12 -0400448static const uint16_t kVerifySignatureAlgorithms[] = {
Steven Valdez909b19f2016-11-21 15:35:44 -0500449 /* Prefer SHA-256 algorithms. */
450 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin95add822016-10-19 01:09:12 -0400451#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdez909b19f2016-11-21 15:35:44 -0500452 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin95add822016-10-19 01:09:12 -0400453#endif
Steven Valdez909b19f2016-11-21 15:35:44 -0500454 SSL_SIGN_RSA_PKCS1_SHA256,
455
456 /* Larger hashes are acceptable. */
457 SSL_SIGN_ECDSA_SECP384R1_SHA384,
458#if !defined(BORINGSSL_ANDROID_SYSTEM)
459 SSL_SIGN_RSA_PSS_SHA384,
460#endif
461 SSL_SIGN_RSA_PKCS1_SHA384,
462
Robert Sloan69939df2017-01-09 10:53:07 -0800463 /* TODO(davidben): Remove this. */
David Benjamin95add822016-10-19 01:09:12 -0400464#if defined(BORINGSSL_ANDROID_SYSTEM)
465 SSL_SIGN_ECDSA_SECP521R1_SHA512,
466#endif
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400467#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjaminc895d6b2016-08-11 13:26:41 -0400468 SSL_SIGN_RSA_PSS_SHA512,
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400469#endif
David Benjaminc895d6b2016-08-11 13:26:41 -0400470 SSL_SIGN_RSA_PKCS1_SHA512,
David Benjaminc895d6b2016-08-11 13:26:41 -0400471
Steven Valdez909b19f2016-11-21 15:35:44 -0500472 /* For now, SHA-1 is still accepted but least preferable. */
473 SSL_SIGN_RSA_PKCS1_SHA1,
David Benjaminc895d6b2016-08-11 13:26:41 -0400474
Steven Valdez909b19f2016-11-21 15:35:44 -0500475};
476
477/* kSignSignatureAlgorithms is the default list of supported signature
478 * algorithms for signing.
479 *
480 * For now, RSA-PSS signature algorithms are not enabled on Android's system
481 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
482 * restore them. */
483static const uint16_t kSignSignatureAlgorithms[] = {
484 /* Prefer SHA-256 algorithms. */
485 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400486#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjaminc895d6b2016-08-11 13:26:41 -0400487 SSL_SIGN_RSA_PSS_SHA256,
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400488#endif
David Benjaminc895d6b2016-08-11 13:26:41 -0400489 SSL_SIGN_RSA_PKCS1_SHA256,
David Benjaminc895d6b2016-08-11 13:26:41 -0400490
Steven Valdez909b19f2016-11-21 15:35:44 -0500491 /* If needed, sign larger hashes.
492 *
493 * TODO(davidben): Determine which of these may be pruned. */
494 SSL_SIGN_ECDSA_SECP384R1_SHA384,
495#if !defined(BORINGSSL_ANDROID_SYSTEM)
496 SSL_SIGN_RSA_PSS_SHA384,
497#endif
498 SSL_SIGN_RSA_PKCS1_SHA384,
499
500 SSL_SIGN_ECDSA_SECP521R1_SHA512,
501#if !defined(BORINGSSL_ANDROID_SYSTEM)
502 SSL_SIGN_RSA_PSS_SHA512,
503#endif
504 SSL_SIGN_RSA_PKCS1_SHA512,
505
506 /* If the peer supports nothing else, sign with SHA-1. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400507 SSL_SIGN_ECDSA_SHA1,
Steven Valdez909b19f2016-11-21 15:35:44 -0500508 SSL_SIGN_RSA_PKCS1_SHA1,
David Benjaminc895d6b2016-08-11 13:26:41 -0400509};
510
David Benjamin95add822016-10-19 01:09:12 -0400511size_t tls12_get_verify_sigalgs(const SSL *ssl, const uint16_t **out) {
512 *out = kVerifySignatureAlgorithms;
513 return OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800514}
515
David Benjaminc895d6b2016-08-11 13:26:41 -0400516int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
David Benjamin95add822016-10-19 01:09:12 -0400517 const uint16_t *verify_sigalgs;
518 size_t num_verify_sigalgs = tls12_get_verify_sigalgs(ssl, &verify_sigalgs);
519 for (size_t i = 0; i < num_verify_sigalgs; i++) {
520 if (sigalg == verify_sigalgs[i]) {
521 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800522 }
523 }
524
David Benjamin95add822016-10-19 01:09:12 -0400525 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
526 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
527 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800528}
529
Kenny Rootb8494592015-09-25 02:29:14 +0000530/* tls_extension represents a TLS extension that is handled internally. The
531 * |init| function is called for each handshake, before any other functions of
532 * the extension. Then the add and parse callbacks are called as needed.
533 *
534 * The parse callbacks receive a |CBS| that contains the contents of the
535 * extension (i.e. not including the type and length bytes). If an extension is
536 * not received then the parse callbacks will be called with a NULL CBS so that
537 * they can do any processing needed to handle the absence of an extension.
538 *
539 * The add callbacks receive a |CBB| to which the extension can be appended but
540 * the function is responsible for appending the type and length bytes too.
541 *
542 * All callbacks return one for success and zero for error. If a parse function
543 * returns zero then a fatal alert with value |*out_alert| will be sent. If
544 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
545struct tls_extension {
546 uint16_t value;
David Benjamin1b249672016-12-06 18:25:50 -0500547 void (*init)(SSL_HANDSHAKE *hs);
Kenny Roota04d78d2015-09-25 00:26:37 +0000548
David Benjamin1b249672016-12-06 18:25:50 -0500549 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
550 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
551 CBS *contents);
Kenny Roota04d78d2015-09-25 00:26:37 +0000552
David Benjamin1b249672016-12-06 18:25:50 -0500553 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
554 CBS *contents);
555 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Kenny Rootb8494592015-09-25 02:29:14 +0000556};
Kenny Roota04d78d2015-09-25 00:26:37 +0000557
David Benjamin1b249672016-12-06 18:25:50 -0500558static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
559 CBS *contents) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400560 if (contents != NULL) {
561 /* Servers MUST NOT send this extension. */
562 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
563 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
564 return 0;
565 }
566
567 return 1;
568}
569
David Benjamin1b249672016-12-06 18:25:50 -0500570static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
571 CBS *contents) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400572 /* This extension from the client is handled elsewhere. */
573 return 1;
574}
575
David Benjamin1b249672016-12-06 18:25:50 -0500576static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400577 return 1;
578}
Kenny Rootb8494592015-09-25 02:29:14 +0000579
580/* Server name indication (SNI).
581 *
582 * https://tools.ietf.org/html/rfc6066#section-3. */
583
David Benjamin1b249672016-12-06 18:25:50 -0500584static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
585 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +0000586 if (ssl->tlsext_hostname == NULL) {
587 return 1;
588 }
589
590 CBB contents, server_name_list, name;
591 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
592 !CBB_add_u16_length_prefixed(out, &contents) ||
593 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
594 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
595 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
596 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
597 strlen(ssl->tlsext_hostname)) ||
598 !CBB_flush(out)) {
599 return 0;
600 }
601
602 return 1;
603}
604
David Benjamin1b249672016-12-06 18:25:50 -0500605static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley4139edb2016-01-13 15:00:54 -0800606 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500607 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +0000608 if (contents == NULL) {
609 return 1;
610 }
611
612 if (CBS_len(contents) != 0) {
613 return 0;
614 }
615
616 assert(ssl->tlsext_hostname != NULL);
617
David Benjaminc895d6b2016-08-11 13:26:41 -0400618 if (ssl->session == NULL) {
Robert Sloana94fe052017-02-21 08:49:28 -0800619 OPENSSL_free(hs->new_session->tlsext_hostname);
620 hs->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
621 if (!hs->new_session->tlsext_hostname) {
Kenny Rootb8494592015-09-25 02:29:14 +0000622 *out_alert = SSL_AD_INTERNAL_ERROR;
623 return 0;
624 }
625 }
626
627 return 1;
628}
629
David Benjamin1b249672016-12-06 18:25:50 -0500630static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley4139edb2016-01-13 15:00:54 -0800631 CBS *contents) {
Kenny Rootb8494592015-09-25 02:29:14 +0000632 if (contents == NULL) {
633 return 1;
634 }
635
David Benjamin4969cc92016-04-22 15:02:23 -0400636 CBS server_name_list, host_name;
637 uint8_t name_type;
Kenny Rootb8494592015-09-25 02:29:14 +0000638 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin4969cc92016-04-22 15:02:23 -0400639 !CBS_get_u8(&server_name_list, &name_type) ||
640 /* Although the server_name extension was intended to be extensible to
641 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
642 * different name types will cause an error. Further, RFC 4366 originally
643 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
644 * adding new name types is no longer feasible.
645 *
646 * Act as if the extensibility does not exist to simplify parsing. */
647 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
648 CBS_len(&server_name_list) != 0 ||
Kenny Rootb8494592015-09-25 02:29:14 +0000649 CBS_len(contents) != 0) {
650 return 0;
651 }
652
David Benjamin4969cc92016-04-22 15:02:23 -0400653 if (name_type != TLSEXT_NAMETYPE_host_name ||
654 CBS_len(&host_name) == 0 ||
655 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
656 CBS_contains_zero_byte(&host_name)) {
657 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
658 return 0;
659 }
Kenny Rootb8494592015-09-25 02:29:14 +0000660
Steven Valdez909b19f2016-11-21 15:35:44 -0500661 /* Copy the hostname as a string. */
David Benjamin1b249672016-12-06 18:25:50 -0500662 if (!CBS_strdup(&host_name, &hs->hostname)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500663 *out_alert = SSL_AD_INTERNAL_ERROR;
664 return 0;
Kenny Rootb8494592015-09-25 02:29:14 +0000665 }
666
David Benjamin1b249672016-12-06 18:25:50 -0500667 hs->should_ack_sni = 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000668 return 1;
669}
670
David Benjamin1b249672016-12-06 18:25:50 -0500671static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
672 if (hs->ssl->s3->session_reused ||
673 !hs->should_ack_sni) {
Kenny Rootb8494592015-09-25 02:29:14 +0000674 return 1;
675 }
676
677 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
678 !CBB_add_u16(out, 0 /* length */)) {
679 return 0;
680 }
681
682 return 1;
683}
684
685
686/* Renegotiation indication.
687 *
688 * https://tools.ietf.org/html/rfc5746 */
689
David Benjamin1b249672016-12-06 18:25:50 -0500690static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
691 SSL *const ssl = hs->ssl;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400692 uint16_t min_version, max_version;
693 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
694 return 0;
695 }
696
697 /* Renegotiation indication is not necessary in TLS 1.3. */
698 if (min_version >= TLS1_3_VERSION) {
699 return 1;
700 }
701
David Benjamin95add822016-10-19 01:09:12 -0400702 assert(ssl->s3->initial_handshake_complete ==
703 (ssl->s3->previous_client_finished_len != 0));
704
Kenny Rootb8494592015-09-25 02:29:14 +0000705 CBB contents, prev_finished;
706 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
707 !CBB_add_u16_length_prefixed(out, &contents) ||
708 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
709 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
710 ssl->s3->previous_client_finished_len) ||
711 !CBB_flush(out)) {
712 return 0;
713 }
714
715 return 1;
716}
717
David Benjamin1b249672016-12-06 18:25:50 -0500718static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +0000719 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500720 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -0400721 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700722 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjaminc895d6b2016-08-11 13:26:41 -0400723 return 0;
724 }
725
Adam Langley4139edb2016-01-13 15:00:54 -0800726 /* Servers may not switch between omitting the extension and supporting it.
727 * See RFC 5746, sections 3.5 and 4.2. */
728 if (ssl->s3->initial_handshake_complete &&
729 (contents != NULL) != ssl->s3->send_connection_binding) {
730 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
731 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
732 return 0;
733 }
734
Kenny Rootb8494592015-09-25 02:29:14 +0000735 if (contents == NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800736 /* Strictly speaking, if we want to avoid an attack we should *always* see
Kenny Rootb8494592015-09-25 02:29:14 +0000737 * RI even on initial ServerHello because the client doesn't see any
738 * renegotiation during an attack. However this would mean we could not
739 * connect to any server which doesn't support RI.
740 *
Adam Langley4139edb2016-01-13 15:00:54 -0800741 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
742 * practical terms every client sets it so it's just assumed here. */
743 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000744 }
745
746 const size_t expected_len = ssl->s3->previous_client_finished_len +
747 ssl->s3->previous_server_finished_len;
748
749 /* Check for logic errors */
750 assert(!expected_len || ssl->s3->previous_client_finished_len);
751 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin95add822016-10-19 01:09:12 -0400752 assert(ssl->s3->initial_handshake_complete ==
753 (ssl->s3->previous_client_finished_len != 0));
754 assert(ssl->s3->initial_handshake_complete ==
755 (ssl->s3->previous_server_finished_len != 0));
Kenny Rootb8494592015-09-25 02:29:14 +0000756
757 /* Parse out the extension contents. */
758 CBS renegotiated_connection;
759 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
760 CBS_len(contents) != 0) {
761 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
762 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
763 return 0;
764 }
765
766 /* Check that the extension matches. */
767 if (CBS_len(&renegotiated_connection) != expected_len) {
768 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
769 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
770 return 0;
771 }
772
773 const uint8_t *d = CBS_data(&renegotiated_connection);
774 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
775 ssl->s3->previous_client_finished_len)) {
776 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
777 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
778 return 0;
779 }
780 d += ssl->s3->previous_client_finished_len;
781
782 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
783 ssl->s3->previous_server_finished_len)) {
784 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
785 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
786 return 0;
787 }
788 ssl->s3->send_connection_binding = 1;
789
790 return 1;
791}
792
David Benjamin1b249672016-12-06 18:25:50 -0500793static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +0000794 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500795 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +0000796 /* Renegotiation isn't supported as a server so this function should never be
797 * called after the initial handshake. */
798 assert(!ssl->s3->initial_handshake_complete);
799
David Benjaminc895d6b2016-08-11 13:26:41 -0400800 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
801 return 1;
802 }
Kenny Rootb8494592015-09-25 02:29:14 +0000803
804 if (contents == NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400805 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000806 }
807
808 CBS renegotiated_connection;
Kenny Rootb8494592015-09-25 02:29:14 +0000809 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
810 CBS_len(contents) != 0) {
811 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
812 return 0;
813 }
814
David Benjamin95add822016-10-19 01:09:12 -0400815 /* Check that the extension matches. We do not support renegotiation as a
816 * server, so this must be empty. */
817 if (CBS_len(&renegotiated_connection) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000818 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
819 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
820 return 0;
821 }
822
823 ssl->s3->send_connection_binding = 1;
824
825 return 1;
826}
827
David Benjamin1b249672016-12-06 18:25:50 -0500828static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
829 SSL *const ssl = hs->ssl;
David Benjamin95add822016-10-19 01:09:12 -0400830 /* Renegotiation isn't supported as a server so this function should never be
831 * called after the initial handshake. */
832 assert(!ssl->s3->initial_handshake_complete);
833
David Benjaminc895d6b2016-08-11 13:26:41 -0400834 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
835 return 1;
836 }
837
Kenny Rootb8494592015-09-25 02:29:14 +0000838 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin95add822016-10-19 01:09:12 -0400839 !CBB_add_u16(out, 1 /* length */) ||
840 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000841 return 0;
842 }
843
844 return 1;
845}
846
847
848/* Extended Master Secret.
849 *
David Benjamin4969cc92016-04-22 15:02:23 -0400850 * https://tools.ietf.org/html/rfc7627 */
Kenny Rootb8494592015-09-25 02:29:14 +0000851
David Benjamin1b249672016-12-06 18:25:50 -0500852static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400853 uint16_t min_version, max_version;
David Benjamin1b249672016-12-06 18:25:50 -0500854 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400855 return 0;
856 }
857
858 /* Extended master secret is not necessary in TLS 1.3. */
859 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Kenny Rootb8494592015-09-25 02:29:14 +0000860 return 1;
861 }
862
863 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
864 !CBB_add_u16(out, 0 /* length */)) {
865 return 0;
866 }
867
868 return 1;
869}
870
David Benjamin1b249672016-12-06 18:25:50 -0500871static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +0000872 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500873 SSL *const ssl = hs->ssl;
Robert Sloana94fe052017-02-21 08:49:28 -0800874
875 if (contents != NULL) {
876 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
877 ssl->version == SSL3_VERSION ||
878 CBS_len(contents) != 0) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400879 return 0;
880 }
881
Robert Sloana94fe052017-02-21 08:49:28 -0800882 hs->extended_master_secret = 1;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400883 }
884
Robert Sloana94fe052017-02-21 08:49:28 -0800885 /* Whether EMS is negotiated may not change on renegotiation. */
886 if (ssl->s3->established_session != NULL &&
887 hs->extended_master_secret !=
888 ssl->s3->established_session->extended_master_secret) {
889 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
890 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjaminc895d6b2016-08-11 13:26:41 -0400891 return 0;
892 }
893
Kenny Rootb8494592015-09-25 02:29:14 +0000894 return 1;
895}
896
David Benjamin1b249672016-12-06 18:25:50 -0500897static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley4139edb2016-01-13 15:00:54 -0800898 CBS *contents) {
Robert Sloana94fe052017-02-21 08:49:28 -0800899 uint16_t version = ssl3_protocol_version(hs->ssl);
David Benjamin1b249672016-12-06 18:25:50 -0500900 if (version >= TLS1_3_VERSION ||
901 version == SSL3_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400902 return 1;
903 }
904
905 if (contents == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000906 return 1;
907 }
908
909 if (CBS_len(contents) != 0) {
910 return 0;
911 }
912
Robert Sloana94fe052017-02-21 08:49:28 -0800913 hs->extended_master_secret = 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000914 return 1;
915}
916
David Benjamin1b249672016-12-06 18:25:50 -0500917static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloana94fe052017-02-21 08:49:28 -0800918 if (!hs->extended_master_secret) {
Kenny Rootb8494592015-09-25 02:29:14 +0000919 return 1;
920 }
921
922 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
923 !CBB_add_u16(out, 0 /* length */)) {
924 return 0;
925 }
926
927 return 1;
928}
929
930
931/* Session tickets.
932 *
933 * https://tools.ietf.org/html/rfc5077 */
934
David Benjamin1b249672016-12-06 18:25:50 -0500935static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
936 SSL *const ssl = hs->ssl;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400937 uint16_t min_version, max_version;
938 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
939 return 0;
940 }
941
942 /* TLS 1.3 uses a different ticket extension. */
943 if (min_version >= TLS1_3_VERSION ||
944 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Kenny Rootb8494592015-09-25 02:29:14 +0000945 return 1;
946 }
947
948 const uint8_t *ticket_data = NULL;
949 int ticket_len = 0;
950
951 /* Renegotiation does not participate in session resumption. However, still
952 * advertise the extension to avoid potentially breaking servers which carry
953 * over the state from the previous handshake, such as OpenSSL servers
954 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin7c0d06c2016-08-11 13:26:41 -0400955 uint16_t session_version;
Kenny Rootb8494592015-09-25 02:29:14 +0000956 if (!ssl->s3->initial_handshake_complete &&
957 ssl->session != NULL &&
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400958 ssl->session->tlsext_tick != NULL &&
959 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin7c0d06c2016-08-11 13:26:41 -0400960 ssl->method->version_from_wire(&session_version,
961 ssl->session->ssl_version) &&
962 session_version < TLS1_3_VERSION) {
Kenny Rootb8494592015-09-25 02:29:14 +0000963 ticket_data = ssl->session->tlsext_tick;
964 ticket_len = ssl->session->tlsext_ticklen;
965 }
966
967 CBB ticket;
968 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
969 !CBB_add_u16_length_prefixed(out, &ticket) ||
970 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
971 !CBB_flush(out)) {
972 return 0;
973 }
974
975 return 1;
976}
977
David Benjamin1b249672016-12-06 18:25:50 -0500978static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +0000979 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -0500980 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +0000981 if (contents == NULL) {
982 return 1;
983 }
984
David Benjaminc895d6b2016-08-11 13:26:41 -0400985 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
986 return 0;
987 }
988
Kenny Rootb8494592015-09-25 02:29:14 +0000989 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
990 * this function should never be called, even if the server tries to send the
991 * extension. */
992 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
993
994 if (CBS_len(contents) != 0) {
995 return 0;
996 }
997
David Benjamin1b249672016-12-06 18:25:50 -0500998 hs->ticket_expected = 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000999 return 1;
1000}
1001
David Benjamin1b249672016-12-06 18:25:50 -05001002static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1003 if (!hs->ticket_expected) {
Kenny Rootb8494592015-09-25 02:29:14 +00001004 return 1;
1005 }
1006
Steven Valdez909b19f2016-11-21 15:35:44 -05001007 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin1b249672016-12-06 18:25:50 -05001008 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Kenny Rootb8494592015-09-25 02:29:14 +00001009
1010 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1011 !CBB_add_u16(out, 0 /* length */)) {
1012 return 0;
1013 }
1014
1015 return 1;
1016}
1017
1018
1019/* Signature Algorithms.
1020 *
1021 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1022
David Benjamin1b249672016-12-06 18:25:50 -05001023static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1024 SSL *const ssl = hs->ssl;
David Benjamin7c0d06c2016-08-11 13:26:41 -04001025 uint16_t min_version, max_version;
1026 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1027 return 0;
1028 }
1029
1030 if (max_version < TLS1_2_VERSION) {
Kenny Rootb8494592015-09-25 02:29:14 +00001031 return 1;
1032 }
1033
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001034 const uint16_t *sigalgs;
David Benjamin95add822016-10-19 01:09:12 -04001035 const size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Kenny Rootb8494592015-09-25 02:29:14 +00001036
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001037 CBB contents, sigalgs_cbb;
Kenny Rootb8494592015-09-25 02:29:14 +00001038 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1039 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001040 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001041 return 0;
1042 }
1043
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001044 for (size_t i = 0; i < num_sigalgs; i++) {
1045 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001046 return 0;
1047 }
1048 }
Kenny Rootb8494592015-09-25 02:29:14 +00001049
David Benjaminc895d6b2016-08-11 13:26:41 -04001050 if (!CBB_flush(out)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001051 return 0;
1052 }
1053
1054 return 1;
1055}
1056
David Benjamin1b249672016-12-06 18:25:50 -05001057static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +00001058 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001059 OPENSSL_free(hs->peer_sigalgs);
1060 hs->peer_sigalgs = NULL;
1061 hs->num_peer_sigalgs = 0;
Kenny Rootb8494592015-09-25 02:29:14 +00001062
1063 if (contents == NULL) {
1064 return 1;
1065 }
1066
1067 CBS supported_signature_algorithms;
1068 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
1069 CBS_len(contents) != 0 ||
1070 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjamin1b249672016-12-06 18:25:50 -05001071 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001072 return 0;
1073 }
1074
1075 return 1;
1076}
1077
Kenny Rootb8494592015-09-25 02:29:14 +00001078
1079/* OCSP Stapling.
1080 *
1081 * https://tools.ietf.org/html/rfc6066#section-8 */
1082
David Benjamin1b249672016-12-06 18:25:50 -05001083static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1084 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001085 if (!ssl->ocsp_stapling_enabled) {
1086 return 1;
1087 }
1088
1089 CBB contents;
1090 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1091 !CBB_add_u16_length_prefixed(out, &contents) ||
1092 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1093 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1094 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1095 !CBB_flush(out)) {
1096 return 0;
1097 }
1098
1099 return 1;
1100}
1101
David Benjamin1b249672016-12-06 18:25:50 -05001102static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +00001103 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001104 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001105 if (contents == NULL) {
1106 return 1;
1107 }
1108
Steven Valdez909b19f2016-11-21 15:35:44 -05001109 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1110 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001111 return 0;
1112 }
1113
Steven Valdez909b19f2016-11-21 15:35:44 -05001114 /* OCSP stapling is forbidden on non-certificate ciphers. */
1115 if (CBS_len(contents) != 0 ||
Robert Sloana94fe052017-02-21 08:49:28 -08001116 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001117 return 0;
1118 }
1119
Steven Valdez909b19f2016-11-21 15:35:44 -05001120 /* Note this does not check for resumption in TLS 1.2. Sending
1121 * status_request here does not make sense, but OpenSSL does so and the
1122 * specification does not say anything. Tolerate it but ignore it. */
David Benjaminc895d6b2016-08-11 13:26:41 -04001123
David Benjamin1b249672016-12-06 18:25:50 -05001124 hs->certificate_status_expected = 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001125 return 1;
1126}
1127
David Benjamin1b249672016-12-06 18:25:50 -05001128static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +00001129 CBS *contents) {
1130 if (contents == NULL) {
1131 return 1;
1132 }
1133
1134 uint8_t status_type;
1135 if (!CBS_get_u8(contents, &status_type)) {
1136 return 0;
1137 }
1138
1139 /* We cannot decide whether OCSP stapling will occur yet because the correct
1140 * SSL_CTX might not have been selected. */
David Benjamin1b249672016-12-06 18:25:50 -05001141 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Kenny Rootb8494592015-09-25 02:29:14 +00001142
1143 return 1;
1144}
1145
David Benjamin1b249672016-12-06 18:25:50 -05001146static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1147 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -05001148 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin1b249672016-12-06 18:25:50 -05001149 !hs->ocsp_stapling_requested ||
Robert Sloana94fe052017-02-21 08:49:28 -08001150 ssl->cert->ocsp_response == NULL ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001151 ssl->s3->session_reused ||
Robert Sloana94fe052017-02-21 08:49:28 -08001152 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001153 return 1;
1154 }
1155
David Benjamin1b249672016-12-06 18:25:50 -05001156 hs->certificate_status_expected = 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001157
1158 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdez909b19f2016-11-21 15:35:44 -05001159 CBB_add_u16(out, 0 /* length */);
Kenny Rootb8494592015-09-25 02:29:14 +00001160}
1161
1162
1163/* Next protocol negotiation.
1164 *
1165 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1166
David Benjamin1b249672016-12-06 18:25:50 -05001167static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1168 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001169 if (ssl->s3->initial_handshake_complete ||
1170 ssl->ctx->next_proto_select_cb == NULL ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001171 SSL_is_dtls(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001172 return 1;
1173 }
1174
1175 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1176 !CBB_add_u16(out, 0 /* length */)) {
1177 return 0;
1178 }
1179
1180 return 1;
1181}
1182
David Benjamin1b249672016-12-06 18:25:50 -05001183static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +00001184 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001185 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001186 if (contents == NULL) {
1187 return 1;
1188 }
1189
David Benjaminc895d6b2016-08-11 13:26:41 -04001190 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1191 return 0;
1192 }
1193
Kenny Rootb8494592015-09-25 02:29:14 +00001194 /* If any of these are false then we should never have sent the NPN
1195 * extension in the ClientHello and thus this function should never have been
1196 * called. */
1197 assert(!ssl->s3->initial_handshake_complete);
David Benjaminc895d6b2016-08-11 13:26:41 -04001198 assert(!SSL_is_dtls(ssl));
Kenny Rootb8494592015-09-25 02:29:14 +00001199 assert(ssl->ctx->next_proto_select_cb != NULL);
1200
1201 if (ssl->s3->alpn_selected != NULL) {
1202 /* NPN and ALPN may not be negotiated in the same connection. */
1203 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1204 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1205 return 0;
1206 }
1207
1208 const uint8_t *const orig_contents = CBS_data(contents);
1209 const size_t orig_len = CBS_len(contents);
1210
1211 while (CBS_len(contents) != 0) {
1212 CBS proto;
1213 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1214 CBS_len(&proto) == 0) {
1215 return 0;
1216 }
1217 }
1218
1219 uint8_t *selected;
1220 uint8_t selected_len;
1221 if (ssl->ctx->next_proto_select_cb(
1222 ssl, &selected, &selected_len, orig_contents, orig_len,
1223 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1224 *out_alert = SSL_AD_INTERNAL_ERROR;
1225 return 0;
1226 }
1227
David Benjamin4969cc92016-04-22 15:02:23 -04001228 OPENSSL_free(ssl->s3->next_proto_negotiated);
1229 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1230 if (ssl->s3->next_proto_negotiated == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001231 *out_alert = SSL_AD_INTERNAL_ERROR;
1232 return 0;
1233 }
1234
David Benjamin4969cc92016-04-22 15:02:23 -04001235 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin1b249672016-12-06 18:25:50 -05001236 hs->next_proto_neg_seen = 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001237
1238 return 1;
1239}
1240
David Benjamin1b249672016-12-06 18:25:50 -05001241static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +00001242 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001243 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04001244 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1245 return 1;
1246 }
1247
Kenny Rootb8494592015-09-25 02:29:14 +00001248 if (contents != NULL && CBS_len(contents) != 0) {
1249 return 0;
1250 }
1251
1252 if (contents == NULL ||
1253 ssl->s3->initial_handshake_complete ||
Kenny Rootb8494592015-09-25 02:29:14 +00001254 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001255 SSL_is_dtls(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001256 return 1;
1257 }
1258
David Benjamin1b249672016-12-06 18:25:50 -05001259 hs->next_proto_neg_seen = 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001260 return 1;
1261}
1262
David Benjamin1b249672016-12-06 18:25:50 -05001263static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1264 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001265 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1266 * parsed. */
David Benjamin1b249672016-12-06 18:25:50 -05001267 if (!hs->next_proto_neg_seen) {
Kenny Rootb8494592015-09-25 02:29:14 +00001268 return 1;
1269 }
1270
1271 const uint8_t *npa;
1272 unsigned npa_len;
1273
1274 if (ssl->ctx->next_protos_advertised_cb(
1275 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1276 SSL_TLSEXT_ERR_OK) {
David Benjamin1b249672016-12-06 18:25:50 -05001277 hs->next_proto_neg_seen = 0;
Kenny Rootb8494592015-09-25 02:29:14 +00001278 return 1;
1279 }
1280
1281 CBB contents;
1282 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1283 !CBB_add_u16_length_prefixed(out, &contents) ||
1284 !CBB_add_bytes(&contents, npa, npa_len) ||
1285 !CBB_flush(out)) {
1286 return 0;
1287 }
1288
1289 return 1;
1290}
1291
1292
1293/* Signed certificate timestamps.
1294 *
1295 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1296
David Benjamin1b249672016-12-06 18:25:50 -05001297static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1298 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001299 if (!ssl->signed_cert_timestamps_enabled) {
1300 return 1;
1301 }
1302
1303 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1304 !CBB_add_u16(out, 0 /* length */)) {
1305 return 0;
1306 }
1307
1308 return 1;
1309}
1310
David Benjamin1b249672016-12-06 18:25:50 -05001311static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +00001312 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001313 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001314 if (contents == NULL) {
1315 return 1;
1316 }
1317
Steven Valdez909b19f2016-11-21 15:35:44 -05001318 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1319 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1320 *out_alert = SSL_AD_DECODE_ERROR;
1321 return 0;
1322 }
1323
Kenny Rootb8494592015-09-25 02:29:14 +00001324 /* If this is false then we should never have sent the SCT extension in the
1325 * ClientHello and thus this function should never have been called. */
1326 assert(ssl->signed_cert_timestamps_enabled);
1327
Steven Valdez909b19f2016-11-21 15:35:44 -05001328 if (!ssl_is_sct_list_valid(contents)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001329 *out_alert = SSL_AD_DECODE_ERROR;
1330 return 0;
1331 }
1332
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001333 /* Session resumption uses the original session information. The extension
1334 * should not be sent on resumption, but RFC 6962 did not make it a
1335 * requirement, so tolerate this.
1336 *
1337 * TODO(davidben): Enforce this anyway. */
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001338 if (!ssl->s3->session_reused &&
Robert Sloana94fe052017-02-21 08:49:28 -08001339 !CBS_stow(contents, &hs->new_session->tlsext_signed_cert_timestamp_list,
1340 &hs->new_session->tlsext_signed_cert_timestamp_list_length)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001341 *out_alert = SSL_AD_INTERNAL_ERROR;
1342 return 0;
1343 }
1344
1345 return 1;
1346}
1347
David Benjamin1b249672016-12-06 18:25:50 -05001348static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +00001349 CBS *contents) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001350 if (contents == NULL) {
1351 return 1;
1352 }
1353
1354 if (CBS_len(contents) != 0) {
1355 return 0;
1356 }
1357
David Benjamin1b249672016-12-06 18:25:50 -05001358 hs->scts_requested = 1;
Steven Valdez909b19f2016-11-21 15:35:44 -05001359 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001360}
1361
David Benjamin1b249672016-12-06 18:25:50 -05001362static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1363 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001364 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez909b19f2016-11-21 15:35:44 -05001365 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1366 ssl->s3->session_reused ||
Robert Sloana94fe052017-02-21 08:49:28 -08001367 ssl->cert->signed_cert_timestamp_list == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001368 return 1;
1369 }
1370
1371 CBB contents;
1372 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1373 CBB_add_u16_length_prefixed(out, &contents) &&
Robert Sloana94fe052017-02-21 08:49:28 -08001374 CBB_add_bytes(
1375 &contents,
1376 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
1377 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
Kenny Rootb8494592015-09-25 02:29:14 +00001378 CBB_flush(out);
1379}
1380
1381
1382/* Application-level Protocol Negotiation.
1383 *
1384 * https://tools.ietf.org/html/rfc7301 */
1385
David Benjamin1b249672016-12-06 18:25:50 -05001386static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1387 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001388 if (ssl->alpn_client_proto_list == NULL ||
1389 ssl->s3->initial_handshake_complete) {
1390 return 1;
1391 }
1392
1393 CBB contents, proto_list;
1394 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1395 !CBB_add_u16_length_prefixed(out, &contents) ||
1396 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1397 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1398 ssl->alpn_client_proto_list_len) ||
1399 !CBB_flush(out)) {
1400 return 0;
1401 }
1402
1403 return 1;
1404}
1405
David Benjamin1b249672016-12-06 18:25:50 -05001406static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +00001407 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001408 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001409 if (contents == NULL) {
1410 return 1;
1411 }
1412
1413 assert(!ssl->s3->initial_handshake_complete);
1414 assert(ssl->alpn_client_proto_list != NULL);
1415
David Benjamin1b249672016-12-06 18:25:50 -05001416 if (hs->next_proto_neg_seen) {
Kenny Rootb8494592015-09-25 02:29:14 +00001417 /* NPN and ALPN may not be negotiated in the same connection. */
1418 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1419 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1420 return 0;
1421 }
1422
1423 /* The extension data consists of a ProtocolNameList which must have
1424 * exactly one ProtocolName. Each of these is length-prefixed. */
1425 CBS protocol_name_list, protocol_name;
1426 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1427 CBS_len(contents) != 0 ||
1428 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1429 /* Empty protocol names are forbidden. */
1430 CBS_len(&protocol_name) == 0 ||
1431 CBS_len(&protocol_name_list) != 0) {
1432 return 0;
1433 }
1434
David Benjaminc895d6b2016-08-11 13:26:41 -04001435 /* Check that the protcol name is one of the ones we advertised. */
1436 int protocol_ok = 0;
1437 CBS client_protocol_name_list, client_protocol_name;
1438 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1439 ssl->alpn_client_proto_list_len);
1440 while (CBS_len(&client_protocol_name_list) > 0) {
1441 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1442 &client_protocol_name)) {
1443 *out_alert = SSL_AD_INTERNAL_ERROR;
1444 return 0;
1445 }
1446
1447 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
Robert Sloan69939df2017-01-09 10:53:07 -08001448 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1449 CBS_data(&protocol_name),
1450 CBS_len(&protocol_name)) == 0) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001451 protocol_ok = 1;
1452 break;
1453 }
1454 }
1455
1456 if (!protocol_ok) {
1457 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1458 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1459 return 0;
1460 }
1461
Kenny Rootb8494592015-09-25 02:29:14 +00001462 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1463 &ssl->s3->alpn_selected_len)) {
1464 *out_alert = SSL_AD_INTERNAL_ERROR;
1465 return 0;
1466 }
1467
1468 return 1;
1469}
1470
David Benjamin1b249672016-12-06 18:25:50 -05001471int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1472 const SSL_CLIENT_HELLO *client_hello) {
1473 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -05001474 CBS contents;
Kenny Rootb8494592015-09-25 02:29:14 +00001475 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin1b249672016-12-06 18:25:50 -05001476 !ssl_client_hello_get_extension(
Steven Valdez909b19f2016-11-21 15:35:44 -05001477 client_hello, &contents,
1478 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1479 /* Ignore ALPN if not configured or no extension was supplied. */
Kenny Rootb8494592015-09-25 02:29:14 +00001480 return 1;
1481 }
1482
1483 /* ALPN takes precedence over NPN. */
David Benjamin1b249672016-12-06 18:25:50 -05001484 hs->next_proto_neg_seen = 0;
Kenny Rootb8494592015-09-25 02:29:14 +00001485
1486 CBS protocol_name_list;
Steven Valdez909b19f2016-11-21 15:35:44 -05001487 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1488 CBS_len(&contents) != 0 ||
Kenny Rootb8494592015-09-25 02:29:14 +00001489 CBS_len(&protocol_name_list) < 2) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001490 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1491 *out_alert = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001492 return 0;
1493 }
1494
1495 /* Validate the protocol list. */
1496 CBS protocol_name_list_copy = protocol_name_list;
1497 while (CBS_len(&protocol_name_list_copy) > 0) {
1498 CBS protocol_name;
1499
1500 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1501 /* Empty protocol names are forbidden. */
1502 CBS_len(&protocol_name) == 0) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001503 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1504 *out_alert = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001505 return 0;
1506 }
1507 }
1508
1509 const uint8_t *selected;
1510 uint8_t selected_len;
1511 if (ssl->ctx->alpn_select_cb(
1512 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1513 CBS_len(&protocol_name_list),
1514 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1515 OPENSSL_free(ssl->s3->alpn_selected);
1516 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1517 if (ssl->s3->alpn_selected == NULL) {
1518 *out_alert = SSL_AD_INTERNAL_ERROR;
1519 return 0;
1520 }
1521 ssl->s3->alpn_selected_len = selected_len;
1522 }
1523
1524 return 1;
1525}
1526
David Benjamin1b249672016-12-06 18:25:50 -05001527static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1528 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001529 if (ssl->s3->alpn_selected == NULL) {
1530 return 1;
1531 }
1532
1533 CBB contents, proto_list, proto;
1534 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1535 !CBB_add_u16_length_prefixed(out, &contents) ||
1536 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1537 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001538 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1539 ssl->s3->alpn_selected_len) ||
Kenny Rootb8494592015-09-25 02:29:14 +00001540 !CBB_flush(out)) {
1541 return 0;
1542 }
1543
1544 return 1;
1545}
1546
1547
1548/* Channel ID.
1549 *
1550 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1551
David Benjamin1b249672016-12-06 18:25:50 -05001552static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1553 hs->ssl->s3->tlsext_channel_id_valid = 0;
Kenny Rootb8494592015-09-25 02:29:14 +00001554}
1555
David Benjamin1b249672016-12-06 18:25:50 -05001556static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1557 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001558 if (!ssl->tlsext_channel_id_enabled ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001559 SSL_is_dtls(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001560 return 1;
1561 }
1562
1563 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1564 !CBB_add_u16(out, 0 /* length */)) {
1565 return 0;
1566 }
1567
1568 return 1;
1569}
1570
David Benjamin1b249672016-12-06 18:25:50 -05001571static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1572 uint8_t *out_alert, CBS *contents) {
1573 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001574 if (contents == NULL) {
1575 return 1;
1576 }
1577
David Benjaminc895d6b2016-08-11 13:26:41 -04001578 assert(!SSL_is_dtls(ssl));
Kenny Rootb8494592015-09-25 02:29:14 +00001579 assert(ssl->tlsext_channel_id_enabled);
1580
1581 if (CBS_len(contents) != 0) {
1582 return 0;
1583 }
1584
1585 ssl->s3->tlsext_channel_id_valid = 1;
1586 return 1;
1587}
1588
David Benjamin1b249672016-12-06 18:25:50 -05001589static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1590 uint8_t *out_alert, CBS *contents) {
1591 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001592 if (contents == NULL ||
1593 !ssl->tlsext_channel_id_enabled ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001594 SSL_is_dtls(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001595 return 1;
1596 }
1597
1598 if (CBS_len(contents) != 0) {
1599 return 0;
1600 }
1601
1602 ssl->s3->tlsext_channel_id_valid = 1;
1603 return 1;
1604}
1605
David Benjamin1b249672016-12-06 18:25:50 -05001606static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1607 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001608 if (!ssl->s3->tlsext_channel_id_valid) {
1609 return 1;
1610 }
1611
1612 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1613 !CBB_add_u16(out, 0 /* length */)) {
1614 return 0;
1615 }
1616
1617 return 1;
1618}
1619
1620
1621/* Secure Real-time Transport Protocol (SRTP) extension.
1622 *
1623 * https://tools.ietf.org/html/rfc5764 */
1624
1625
David Benjamin1b249672016-12-06 18:25:50 -05001626static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1627 hs->ssl->srtp_profile = NULL;
Kenny Rootb8494592015-09-25 02:29:14 +00001628}
1629
David Benjamin1b249672016-12-06 18:25:50 -05001630static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1631 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001632 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1633 if (profiles == NULL) {
1634 return 1;
1635 }
1636 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1637 if (num_profiles == 0) {
1638 return 1;
1639 }
1640
1641 CBB contents, profile_ids;
1642 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1643 !CBB_add_u16_length_prefixed(out, &contents) ||
1644 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1645 return 0;
1646 }
1647
David Benjamin7c0d06c2016-08-11 13:26:41 -04001648 for (size_t i = 0; i < num_profiles; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00001649 if (!CBB_add_u16(&profile_ids,
1650 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1651 return 0;
1652 }
1653 }
1654
1655 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1656 !CBB_flush(out)) {
1657 return 0;
1658 }
1659
1660 return 1;
1661}
1662
David Benjamin1b249672016-12-06 18:25:50 -05001663static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +00001664 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001665 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001666 if (contents == NULL) {
1667 return 1;
1668 }
1669
1670 /* The extension consists of a u16-prefixed profile ID list containing a
1671 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1672 *
1673 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1674 CBS profile_ids, srtp_mki;
1675 uint16_t profile_id;
1676 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1677 !CBS_get_u16(&profile_ids, &profile_id) ||
1678 CBS_len(&profile_ids) != 0 ||
1679 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1680 CBS_len(contents) != 0) {
1681 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1682 return 0;
1683 }
1684
1685 if (CBS_len(&srtp_mki) != 0) {
1686 /* Must be no MKI, since we never offer one. */
1687 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1688 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1689 return 0;
1690 }
1691
1692 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1693
1694 /* Check to see if the server gave us something we support (and presumably
1695 * offered). */
David Benjamin7c0d06c2016-08-11 13:26:41 -04001696 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00001697 const SRTP_PROTECTION_PROFILE *profile =
1698 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1699
1700 if (profile->id == profile_id) {
1701 ssl->srtp_profile = profile;
1702 return 1;
1703 }
1704 }
1705
1706 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1707 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1708 return 0;
1709}
1710
David Benjamin1b249672016-12-06 18:25:50 -05001711static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +00001712 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001713 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001714 if (contents == NULL) {
1715 return 1;
1716 }
1717
1718 CBS profile_ids, srtp_mki;
1719 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1720 CBS_len(&profile_ids) < 2 ||
1721 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1722 CBS_len(contents) != 0) {
1723 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1724 return 0;
1725 }
1726 /* Discard the MKI value for now. */
1727
1728 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1729 SSL_get_srtp_profiles(ssl);
1730
1731 /* Pick the server's most preferred profile. */
David Benjamin7c0d06c2016-08-11 13:26:41 -04001732 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00001733 const SRTP_PROTECTION_PROFILE *server_profile =
1734 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1735
1736 CBS profile_ids_tmp;
1737 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1738
1739 while (CBS_len(&profile_ids_tmp) > 0) {
1740 uint16_t profile_id;
1741 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1742 return 0;
1743 }
1744
1745 if (server_profile->id == profile_id) {
1746 ssl->srtp_profile = server_profile;
1747 return 1;
Kenny Roota04d78d2015-09-25 00:26:37 +00001748 }
1749 }
1750 }
1751
Kenny Rootb8494592015-09-25 02:29:14 +00001752 return 1;
1753}
1754
David Benjamin1b249672016-12-06 18:25:50 -05001755static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1756 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00001757 if (ssl->srtp_profile == NULL) {
1758 return 1;
1759 }
1760
1761 CBB contents, profile_ids;
1762 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1763 !CBB_add_u16_length_prefixed(out, &contents) ||
1764 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1765 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1766 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1767 !CBB_flush(out)) {
1768 return 0;
1769 }
1770
1771 return 1;
1772}
1773
1774
1775/* EC point formats.
1776 *
1777 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1778
David Benjamin1b249672016-12-06 18:25:50 -05001779static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
Kenny Rootb8494592015-09-25 02:29:14 +00001780 CBB contents, formats;
1781 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1782 !CBB_add_u16_length_prefixed(out, &contents) ||
1783 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1784 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
1785 !CBB_flush(out)) {
1786 return 0;
1787 }
1788
1789 return 1;
1790}
1791
David Benjamin1b249672016-12-06 18:25:50 -05001792static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001793 uint16_t min_version, max_version;
David Benjamin1b249672016-12-06 18:25:50 -05001794 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
Steven Valdez909b19f2016-11-21 15:35:44 -05001795 return 0;
1796 }
1797
1798 /* The point format extension is unneccessary in TLS 1.3. */
1799 if (min_version >= TLS1_3_VERSION) {
Kenny Rootb8494592015-09-25 02:29:14 +00001800 return 1;
1801 }
1802
David Benjamin1b249672016-12-06 18:25:50 -05001803 return ext_ec_point_add_extension(hs, out);
Kenny Rootb8494592015-09-25 02:29:14 +00001804}
1805
David Benjamin1b249672016-12-06 18:25:50 -05001806static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +00001807 CBS *contents) {
1808 if (contents == NULL) {
1809 return 1;
1810 }
1811
David Benjamin1b249672016-12-06 18:25:50 -05001812 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001813 return 0;
1814 }
1815
Kenny Rootb8494592015-09-25 02:29:14 +00001816 CBS ec_point_format_list;
1817 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1818 CBS_len(contents) != 0) {
1819 return 0;
1820 }
1821
1822 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1823 * point format. */
Robert Sloan69939df2017-01-09 10:53:07 -08001824 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1825 TLSEXT_ECPOINTFORMAT_uncompressed,
1826 CBS_len(&ec_point_format_list)) == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001827 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1828 return 0;
1829 }
1830
1831 return 1;
1832}
1833
David Benjamin1b249672016-12-06 18:25:50 -05001834static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Kenny Rootb8494592015-09-25 02:29:14 +00001835 CBS *contents) {
David Benjamin1b249672016-12-06 18:25:50 -05001836 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001837 return 1;
1838 }
1839
David Benjamin1b249672016-12-06 18:25:50 -05001840 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Kenny Rootb8494592015-09-25 02:29:14 +00001841}
1842
David Benjamin1b249672016-12-06 18:25:50 -05001843static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1844 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04001845 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1846 return 1;
1847 }
1848
Robert Sloana94fe052017-02-21 08:49:28 -08001849 const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1850 const uint32_t alg_a = hs->new_cipher->algorithm_auth;
Kenny Rootb8494592015-09-25 02:29:14 +00001851 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
1852
1853 if (!using_ecc) {
1854 return 1;
1855 }
1856
David Benjamin1b249672016-12-06 18:25:50 -05001857 return ext_ec_point_add_extension(hs, out);
Kenny Rootb8494592015-09-25 02:29:14 +00001858}
1859
Steven Valdez909b19f2016-11-21 15:35:44 -05001860
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001861/* Pre Shared Key
1862 *
Steven Valdez909b19f2016-11-21 15:35:44 -05001863 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
1864
David Benjamin1b249672016-12-06 18:25:50 -05001865static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1866 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -05001867 uint16_t min_version, max_version;
1868 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1869 return 0;
1870 }
1871
1872 uint16_t session_version;
1873 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1874 !ssl->method->version_from_wire(&session_version,
1875 ssl->session->ssl_version) ||
1876 session_version < TLS1_3_VERSION) {
1877 return 0;
1878 }
1879
Robert Sloan5d625782017-02-13 09:55:39 -08001880 const EVP_MD *digest = SSL_SESSION_get_digest(ssl->session, ssl);
1881 if (digest == NULL) {
1882 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1883 return 0;
1884 }
1885
Steven Valdez909b19f2016-11-21 15:35:44 -05001886 size_t binder_len = EVP_MD_size(digest);
1887 return 15 + ssl->session->tlsext_ticklen + binder_len;
1888}
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001889
David Benjamin1b249672016-12-06 18:25:50 -05001890static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1891 SSL *const ssl = hs->ssl;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001892 uint16_t min_version, max_version;
1893 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1894 return 0;
1895 }
1896
David Benjamin7c0d06c2016-08-11 13:26:41 -04001897 uint16_t session_version;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001898 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04001899 !ssl->method->version_from_wire(&session_version,
1900 ssl->session->ssl_version) ||
1901 session_version < TLS1_3_VERSION) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001902 return 1;
1903 }
1904
Robert Sloan7d422bc2017-03-06 10:04:29 -08001905 struct OPENSSL_timeval now;
Steven Valdez909b19f2016-11-21 15:35:44 -05001906 ssl_get_current_time(ssl, &now);
1907 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1908 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1909
1910 /* Fill in a placeholder zero binder of the appropriate length. It will be
1911 * computed and filled in later after length prefixes are computed. */
1912 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
Robert Sloan5d625782017-02-13 09:55:39 -08001913
1914 const EVP_MD *digest = SSL_SESSION_get_digest(ssl->session, ssl);
1915 if (digest == NULL) {
1916 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1917 return 0;
1918 }
1919
Steven Valdez909b19f2016-11-21 15:35:44 -05001920 size_t binder_len = EVP_MD_size(digest);
1921
1922 CBB contents, identity, ticket, binders, binder;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001923 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1924 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001925 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001926 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1927 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdez909b19f2016-11-21 15:35:44 -05001928 ssl->session->tlsext_ticklen) ||
1929 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1930 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1931 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1932 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001933 return 0;
1934 }
1935
David Benjamin1b249672016-12-06 18:25:50 -05001936 hs->needs_psk_binder = 1;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001937 return CBB_flush(out);
1938}
1939
David Benjamin1b249672016-12-06 18:25:50 -05001940int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1941 uint8_t *out_alert,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001942 CBS *contents) {
1943 uint16_t psk_id;
1944 if (!CBS_get_u16(contents, &psk_id) ||
1945 CBS_len(contents) != 0) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001946 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001947 *out_alert = SSL_AD_DECODE_ERROR;
1948 return 0;
1949 }
1950
Steven Valdez909b19f2016-11-21 15:35:44 -05001951 /* We only advertise one PSK identity, so the only legal index is zero. */
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001952 if (psk_id != 0) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001953 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001954 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1955 return 0;
1956 }
1957
1958 return 1;
1959}
1960
Robert Sloan1c9db532017-03-13 08:03:59 -07001961int ssl_ext_pre_shared_key_parse_clienthello(
1962 SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
1963 uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04001964 /* We only process the first PSK identity since we don't support pure PSK. */
Robert Sloan1c9db532017-03-13 08:03:59 -07001965 CBS identities, binders;
David Benjamin1b249672016-12-06 18:25:50 -05001966 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
Robert Sloan1c9db532017-03-13 08:03:59 -07001967 !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
1968 !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
Steven Valdez909b19f2016-11-21 15:35:44 -05001969 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjamin1b249672016-12-06 18:25:50 -05001970 CBS_len(&binders) == 0 ||
Steven Valdez909b19f2016-11-21 15:35:44 -05001971 CBS_len(contents) != 0) {
1972 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001973 *out_alert = SSL_AD_DECODE_ERROR;
1974 return 0;
1975 }
1976
Steven Valdez909b19f2016-11-21 15:35:44 -05001977 *out_binders = binders;
1978
David Benjamin1b249672016-12-06 18:25:50 -05001979 /* Check the syntax of the remaining identities, but do not process them. */
1980 size_t num_identities = 1;
1981 while (CBS_len(&identities) != 0) {
1982 CBS unused_ticket;
1983 uint32_t unused_obfuscated_ticket_age;
1984 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
1985 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
1986 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1987 *out_alert = SSL_AD_DECODE_ERROR;
1988 return 0;
1989 }
1990
1991 num_identities++;
1992 }
1993
1994 /* Check the syntax of the binders. The value will be checked later if
1995 * resuming. */
1996 size_t num_binders = 0;
1997 while (CBS_len(&binders) != 0) {
1998 CBS binder;
1999 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
2000 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2001 *out_alert = SSL_AD_DECODE_ERROR;
2002 return 0;
2003 }
2004
2005 num_binders++;
2006 }
2007
2008 if (num_identities != num_binders) {
2009 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
2010 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdez909b19f2016-11-21 15:35:44 -05002011 return 0;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002012 }
2013
Steven Valdez909b19f2016-11-21 15:35:44 -05002014 return 1;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002015}
2016
David Benjamin1b249672016-12-06 18:25:50 -05002017int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2018 if (!hs->ssl->s3->session_reused) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002019 return 1;
2020 }
2021
2022 CBB contents;
2023 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2024 !CBB_add_u16_length_prefixed(out, &contents) ||
2025 /* We only consider the first identity for resumption */
2026 !CBB_add_u16(&contents, 0) ||
2027 !CBB_flush(out)) {
2028 return 0;
2029 }
2030
2031 return 1;
2032}
2033
2034
Steven Valdez909b19f2016-11-21 15:35:44 -05002035/* Pre-Shared Key Exchange Modes
2036 *
2037 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
David Benjamin1b249672016-12-06 18:25:50 -05002038
2039static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2040 CBB *out) {
2041 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -05002042 uint16_t min_version, max_version;
2043 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2044 return 0;
2045 }
2046
2047 if (max_version < TLS1_3_VERSION) {
2048 return 1;
2049 }
2050
2051 CBB contents, ke_modes;
2052 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2053 !CBB_add_u16_length_prefixed(out, &contents) ||
2054 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2055 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2056 return 0;
2057 }
2058
2059 return CBB_flush(out);
2060}
2061
David Benjamin1b249672016-12-06 18:25:50 -05002062static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
Steven Valdez909b19f2016-11-21 15:35:44 -05002063 uint8_t *out_alert,
2064 CBS *contents) {
2065 if (contents == NULL) {
2066 return 1;
2067 }
2068
2069 CBS ke_modes;
2070 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2071 CBS_len(&ke_modes) == 0 ||
2072 CBS_len(contents) != 0) {
2073 *out_alert = SSL_AD_DECODE_ERROR;
2074 return 0;
2075 }
2076
2077 /* We only support tickets with PSK_DHE_KE. */
Robert Sloan69939df2017-01-09 10:53:07 -08002078 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2079 CBS_len(&ke_modes)) != NULL;
Steven Valdez909b19f2016-11-21 15:35:44 -05002080
2081 return 1;
2082}
2083
2084
David Benjamin1b249672016-12-06 18:25:50 -05002085/* Early Data Indication
2086 *
2087 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2088
2089static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002090 SSL *const ssl = hs->ssl;
2091 uint16_t session_version;
2092 if (ssl->session == NULL ||
2093 !ssl->method->version_from_wire(&session_version,
2094 ssl->session->ssl_version) ||
2095 session_version < TLS1_3_VERSION ||
2096 ssl->session->ticket_max_early_data == 0 ||
2097 hs->received_hello_retry_request ||
2098 !ssl->ctx->enable_early_data) {
2099 return 1;
2100 }
2101
2102 hs->early_data_offered = 1;
2103
2104 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2105 !CBB_add_u16(out, 0) ||
2106 !CBB_flush(out)) {
2107 return 0;
2108 }
2109
David Benjamin1b249672016-12-06 18:25:50 -05002110 return 1;
2111}
2112
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002113static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
David Benjamin1b249672016-12-06 18:25:50 -05002114 uint8_t *out_alert, CBS *contents) {
2115 SSL *const ssl = hs->ssl;
2116 if (contents == NULL) {
2117 return 1;
2118 }
2119
2120 if (CBS_len(contents) != 0) {
2121 *out_alert = SSL_AD_DECODE_ERROR;
2122 return 0;
2123 }
2124
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002125 if (!ssl->s3->session_reused) {
2126 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2127 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2128 return 0;
David Benjamin1b249672016-12-06 18:25:50 -05002129 }
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002130
2131 ssl->early_data_accepted = 1;
2132 return 1;
2133}
2134
2135static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2136 uint8_t *out_alert, CBS *contents) {
2137 SSL *const ssl = hs->ssl;
2138 if (contents == NULL ||
2139 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2140 return 1;
2141 }
2142
2143 if (CBS_len(contents) != 0) {
2144 *out_alert = SSL_AD_DECODE_ERROR;
2145 return 0;
2146 }
2147
2148 hs->early_data_offered = 1;
2149 return 1;
2150}
2151
2152static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2153 if (!hs->ssl->early_data_accepted) {
2154 return 1;
2155 }
2156
2157 if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2158 !CBB_add_u16(out, 0) ||
2159 !CBB_flush(out)) {
2160 return 0;
2161 }
2162
David Benjamin1b249672016-12-06 18:25:50 -05002163 return 1;
2164}
2165
2166
David Benjaminc895d6b2016-08-11 13:26:41 -04002167/* Key Share
2168 *
David Benjamin95add822016-10-19 01:09:12 -04002169 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
David Benjaminc895d6b2016-08-11 13:26:41 -04002170
David Benjamin1b249672016-12-06 18:25:50 -05002171static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2172 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04002173 uint16_t min_version, max_version;
2174 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2175 return 0;
2176 }
2177
David Benjamin95add822016-10-19 01:09:12 -04002178 if (max_version < TLS1_3_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002179 return 1;
2180 }
2181
2182 CBB contents, kse_bytes;
2183 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2184 !CBB_add_u16_length_prefixed(out, &contents) ||
2185 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2186 return 0;
2187 }
2188
David Benjamin1b249672016-12-06 18:25:50 -05002189 uint16_t group_id = hs->retry_group;
2190 if (hs->received_hello_retry_request) {
Steven Valdez909b19f2016-11-21 15:35:44 -05002191 /* We received a HelloRetryRequest without a new curve, so there is no new
2192 * share to append. Leave |ecdh_ctx| as-is. */
2193 if (group_id == 0 &&
David Benjamin1b249672016-12-06 18:25:50 -05002194 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2195 hs->key_share_bytes_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002196 return 0;
2197 }
David Benjamin1b249672016-12-06 18:25:50 -05002198 OPENSSL_free(hs->key_share_bytes);
2199 hs->key_share_bytes = NULL;
2200 hs->key_share_bytes_len = 0;
David Benjamin95add822016-10-19 01:09:12 -04002201 if (group_id == 0) {
2202 return CBB_flush(out);
2203 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002204 } else {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002205 /* Add a fake group. See draft-davidben-tls-grease-01. */
2206 if (ssl->ctx->grease_enabled &&
2207 (!CBB_add_u16(&kse_bytes,
2208 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2209 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2210 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2211 return 0;
2212 }
2213
David Benjamin7c0d06c2016-08-11 13:26:41 -04002214 /* Predict the most preferred group. */
2215 const uint16_t *groups;
2216 size_t groups_len;
David Benjamin95add822016-10-19 01:09:12 -04002217 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin7c0d06c2016-08-11 13:26:41 -04002218 if (groups_len == 0) {
2219 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2220 return 0;
David Benjaminc895d6b2016-08-11 13:26:41 -04002221 }
David Benjamin7c0d06c2016-08-11 13:26:41 -04002222
2223 group_id = groups[0];
David Benjaminc895d6b2016-08-11 13:26:41 -04002224 }
2225
David Benjamin7c0d06c2016-08-11 13:26:41 -04002226 CBB key_exchange;
2227 if (!CBB_add_u16(&kse_bytes, group_id) ||
2228 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin1b249672016-12-06 18:25:50 -05002229 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2230 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04002231 !CBB_flush(&kse_bytes)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002232 return 0;
2233 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002234
David Benjamin1b249672016-12-06 18:25:50 -05002235 if (!hs->received_hello_retry_request) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002236 /* Save the contents of the extension to repeat it in the second
2237 * ClientHello. */
David Benjamin1b249672016-12-06 18:25:50 -05002238 hs->key_share_bytes_len = CBB_len(&kse_bytes);
2239 hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
2240 if (hs->key_share_bytes == NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002241 return 0;
2242 }
2243 }
2244
2245 return CBB_flush(out);
2246}
2247
David Benjamin1b249672016-12-06 18:25:50 -05002248int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
David Benjaminc895d6b2016-08-11 13:26:41 -04002249 size_t *out_secret_len,
2250 uint8_t *out_alert, CBS *contents) {
2251 CBS peer_key;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002252 uint16_t group_id;
2253 if (!CBS_get_u16(contents, &group_id) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002254 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2255 CBS_len(contents) != 0) {
2256 *out_alert = SSL_AD_DECODE_ERROR;
2257 return 0;
2258 }
2259
David Benjamin1b249672016-12-06 18:25:50 -05002260 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002261 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2262 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2263 return 0;
2264 }
2265
David Benjamin1b249672016-12-06 18:25:50 -05002266 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2267 CBS_data(&peer_key), CBS_len(&peer_key))) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002268 *out_alert = SSL_AD_INTERNAL_ERROR;
2269 return 0;
2270 }
2271
Robert Sloana94fe052017-02-21 08:49:28 -08002272 hs->new_session->group_id = group_id;
David Benjamin1b249672016-12-06 18:25:50 -05002273 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
David Benjaminc895d6b2016-08-11 13:26:41 -04002274 return 1;
2275}
2276
David Benjamin1b249672016-12-06 18:25:50 -05002277int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
David Benjaminc895d6b2016-08-11 13:26:41 -04002278 uint8_t **out_secret,
2279 size_t *out_secret_len,
2280 uint8_t *out_alert, CBS *contents) {
2281 uint16_t group_id;
2282 CBS key_shares;
David Benjamin1b249672016-12-06 18:25:50 -05002283 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002284 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2285 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2286 return 0;
2287 }
2288
2289 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002290 CBS_len(contents) != 0) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04002291 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04002292 return 0;
2293 }
2294
David Benjamin7c0d06c2016-08-11 13:26:41 -04002295 /* Find the corresponding key share. */
2296 int found = 0;
2297 CBS peer_key;
David Benjaminc895d6b2016-08-11 13:26:41 -04002298 while (CBS_len(&key_shares) > 0) {
2299 uint16_t id;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002300 CBS peer_key_tmp;
David Benjaminc895d6b2016-08-11 13:26:41 -04002301 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04002302 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2303 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04002304 return 0;
2305 }
2306
David Benjamin7c0d06c2016-08-11 13:26:41 -04002307 if (id == group_id) {
2308 if (found) {
2309 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2310 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2311 return 0;
2312 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002313
David Benjamin7c0d06c2016-08-11 13:26:41 -04002314 found = 1;
2315 peer_key = peer_key_tmp;
2316 /* Continue parsing the structure to keep peers honest. */
David Benjaminc895d6b2016-08-11 13:26:41 -04002317 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002318 }
2319
David Benjamin7c0d06c2016-08-11 13:26:41 -04002320 if (!found) {
2321 *out_found = 0;
2322 *out_secret = NULL;
2323 *out_secret_len = 0;
2324 return 1;
2325 }
2326
2327 /* Compute the DH secret. */
2328 uint8_t *secret = NULL;
2329 size_t secret_len;
2330 SSL_ECDH_CTX group;
Robert Sloan69939df2017-01-09 10:53:07 -08002331 OPENSSL_memset(&group, 0, sizeof(SSL_ECDH_CTX));
David Benjamin7c0d06c2016-08-11 13:26:41 -04002332 CBB public_key;
2333 if (!CBB_init(&public_key, 32) ||
2334 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin1b249672016-12-06 18:25:50 -05002335 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
2336 CBS_data(&peer_key), CBS_len(&peer_key)) ||
2337 !CBB_finish(&public_key, &hs->public_key, &hs->public_key_len)) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04002338 OPENSSL_free(secret);
2339 SSL_ECDH_CTX_cleanup(&group);
2340 CBB_cleanup(&public_key);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002341 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002342 return 0;
2343 }
2344
2345 SSL_ECDH_CTX_cleanup(&group);
2346
2347 *out_secret = secret;
2348 *out_secret_len = secret_len;
2349 *out_found = 1;
David Benjaminc895d6b2016-08-11 13:26:41 -04002350 return 1;
2351}
2352
David Benjamin1b249672016-12-06 18:25:50 -05002353int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002354 uint16_t group_id;
2355 CBB kse_bytes, public_key;
David Benjamin1b249672016-12-06 18:25:50 -05002356 if (!tls1_get_shared_group(hs, &group_id) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002357 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2358 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2359 !CBB_add_u16(&kse_bytes, group_id) ||
2360 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjamin1b249672016-12-06 18:25:50 -05002361 !CBB_add_bytes(&public_key, hs->public_key, hs->public_key_len) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04002362 !CBB_flush(out)) {
2363 return 0;
2364 }
2365
David Benjamin1b249672016-12-06 18:25:50 -05002366 OPENSSL_free(hs->public_key);
2367 hs->public_key = NULL;
2368 hs->public_key_len = 0;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002369
Robert Sloana94fe052017-02-21 08:49:28 -08002370 hs->new_session->group_id = group_id;
David Benjaminc895d6b2016-08-11 13:26:41 -04002371 return 1;
2372}
2373
2374
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002375/* Supported Versions
2376 *
2377 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2378
David Benjamin1b249672016-12-06 18:25:50 -05002379static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2380 SSL *const ssl = hs->ssl;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002381 uint16_t min_version, max_version;
2382 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2383 return 0;
2384 }
2385
2386 if (max_version <= TLS1_2_VERSION) {
2387 return 1;
2388 }
2389
2390 CBB contents, versions;
2391 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2392 !CBB_add_u16_length_prefixed(out, &contents) ||
2393 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2394 return 0;
2395 }
2396
2397 /* Add a fake version. See draft-davidben-tls-grease-01. */
2398 if (ssl->ctx->grease_enabled &&
2399 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2400 return 0;
2401 }
2402
2403 for (uint16_t version = max_version; version >= min_version; version--) {
2404 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2405 return 0;
2406 }
2407 }
2408
2409 if (!CBB_flush(out)) {
2410 return 0;
2411 }
2412
2413 return 1;
2414}
2415
2416
David Benjamin95add822016-10-19 01:09:12 -04002417/* Cookie
2418 *
2419 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2420
David Benjamin1b249672016-12-06 18:25:50 -05002421static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2422 if (hs->cookie == NULL) {
David Benjamin95add822016-10-19 01:09:12 -04002423 return 1;
2424 }
2425
2426 CBB contents, cookie;
2427 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2428 !CBB_add_u16_length_prefixed(out, &contents) ||
2429 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin1b249672016-12-06 18:25:50 -05002430 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin95add822016-10-19 01:09:12 -04002431 !CBB_flush(out)) {
2432 return 0;
2433 }
2434
2435 /* The cookie is no longer needed in memory. */
David Benjamin1b249672016-12-06 18:25:50 -05002436 OPENSSL_free(hs->cookie);
2437 hs->cookie = NULL;
2438 hs->cookie_len = 0;
David Benjamin95add822016-10-19 01:09:12 -04002439 return 1;
2440}
2441
2442
David Benjamind316cba2016-06-02 16:17:39 -04002443/* Negotiated Groups
Kenny Rootb8494592015-09-25 02:29:14 +00002444 *
David Benjamind316cba2016-06-02 16:17:39 -04002445 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamin95add822016-10-19 01:09:12 -04002446 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Kenny Rootb8494592015-09-25 02:29:14 +00002447
David Benjamin1b249672016-12-06 18:25:50 -05002448static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2449 SSL *const ssl = hs->ssl;
David Benjamind316cba2016-06-02 16:17:39 -04002450 CBB contents, groups_bytes;
2451 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Kenny Rootb8494592015-09-25 02:29:14 +00002452 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamind316cba2016-06-02 16:17:39 -04002453 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002454 return 0;
2455 }
2456
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002457 /* Add a fake group. See draft-davidben-tls-grease-01. */
2458 if (ssl->ctx->grease_enabled &&
2459 !CBB_add_u16(&groups_bytes,
2460 ssl_get_grease_value(ssl, ssl_grease_group))) {
2461 return 0;
2462 }
2463
David Benjamind316cba2016-06-02 16:17:39 -04002464 const uint16_t *groups;
2465 size_t groups_len;
David Benjamin95add822016-10-19 01:09:12 -04002466 tls1_get_grouplist(ssl, &groups, &groups_len);
Kenny Rootb8494592015-09-25 02:29:14 +00002467
David Benjamin7c0d06c2016-08-11 13:26:41 -04002468 for (size_t i = 0; i < groups_len; i++) {
David Benjamind316cba2016-06-02 16:17:39 -04002469 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Kenny Rootb8494592015-09-25 02:29:14 +00002470 return 0;
2471 }
2472 }
2473
2474 return CBB_flush(out);
2475}
2476
David Benjamin1b249672016-12-06 18:25:50 -05002477static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2478 uint8_t *out_alert,
David Benjamind316cba2016-06-02 16:17:39 -04002479 CBS *contents) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002480 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2481 * BigIP servers send it nonetheless, so do not enforce this. */
Kenny Rootb8494592015-09-25 02:29:14 +00002482 return 1;
2483}
2484
David Benjamin1b249672016-12-06 18:25:50 -05002485static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2486 uint8_t *out_alert,
David Benjamind316cba2016-06-02 16:17:39 -04002487 CBS *contents) {
Kenny Rootb8494592015-09-25 02:29:14 +00002488 if (contents == NULL) {
2489 return 1;
2490 }
2491
David Benjamind316cba2016-06-02 16:17:39 -04002492 CBS supported_group_list;
2493 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2494 CBS_len(&supported_group_list) == 0 ||
2495 (CBS_len(&supported_group_list) & 1) != 0 ||
Kenny Rootb8494592015-09-25 02:29:14 +00002496 CBS_len(contents) != 0) {
2497 return 0;
2498 }
2499
David Benjamin1b249672016-12-06 18:25:50 -05002500 hs->peer_supported_group_list =
2501 OPENSSL_malloc(CBS_len(&supported_group_list));
2502 if (hs->peer_supported_group_list == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00002503 *out_alert = SSL_AD_INTERNAL_ERROR;
2504 return 0;
2505 }
2506
David Benjamind316cba2016-06-02 16:17:39 -04002507 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin7c0d06c2016-08-11 13:26:41 -04002508 for (size_t i = 0; i < num_groups; i++) {
David Benjamind316cba2016-06-02 16:17:39 -04002509 if (!CBS_get_u16(&supported_group_list,
David Benjamin1b249672016-12-06 18:25:50 -05002510 &hs->peer_supported_group_list[i])) {
Kenny Rootb8494592015-09-25 02:29:14 +00002511 goto err;
2512 }
2513 }
2514
David Benjamind316cba2016-06-02 16:17:39 -04002515 assert(CBS_len(&supported_group_list) == 0);
David Benjamin1b249672016-12-06 18:25:50 -05002516 hs->peer_supported_group_list_len = num_groups;
Kenny Rootb8494592015-09-25 02:29:14 +00002517
2518 return 1;
2519
2520err:
David Benjamin1b249672016-12-06 18:25:50 -05002521 OPENSSL_free(hs->peer_supported_group_list);
2522 hs->peer_supported_group_list = NULL;
Kenny Rootb8494592015-09-25 02:29:14 +00002523 *out_alert = SSL_AD_INTERNAL_ERROR;
2524 return 0;
2525}
2526
David Benjamin1b249672016-12-06 18:25:50 -05002527static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Kenny Rootb8494592015-09-25 02:29:14 +00002528 /* Servers don't echo this extension. */
2529 return 1;
2530}
2531
2532
2533/* kExtensions contains all the supported extensions. */
2534static const struct tls_extension kExtensions[] = {
2535 {
Kenny Rootb8494592015-09-25 02:29:14 +00002536 TLSEXT_TYPE_renegotiate,
2537 NULL,
2538 ext_ri_add_clienthello,
2539 ext_ri_parse_serverhello,
2540 ext_ri_parse_clienthello,
2541 ext_ri_add_serverhello,
2542 },
2543 {
2544 TLSEXT_TYPE_server_name,
David Benjamin95add822016-10-19 01:09:12 -04002545 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002546 ext_sni_add_clienthello,
2547 ext_sni_parse_serverhello,
2548 ext_sni_parse_clienthello,
2549 ext_sni_add_serverhello,
2550 },
2551 {
2552 TLSEXT_TYPE_extended_master_secret,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002553 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002554 ext_ems_add_clienthello,
2555 ext_ems_parse_serverhello,
2556 ext_ems_parse_clienthello,
2557 ext_ems_add_serverhello,
2558 },
2559 {
2560 TLSEXT_TYPE_session_ticket,
2561 NULL,
2562 ext_ticket_add_clienthello,
2563 ext_ticket_parse_serverhello,
David Benjaminc895d6b2016-08-11 13:26:41 -04002564 /* Ticket extension client parsing is handled in ssl_session.c */
2565 ignore_parse_clienthello,
Kenny Rootb8494592015-09-25 02:29:14 +00002566 ext_ticket_add_serverhello,
2567 },
2568 {
2569 TLSEXT_TYPE_signature_algorithms,
2570 NULL,
2571 ext_sigalgs_add_clienthello,
David Benjaminc895d6b2016-08-11 13:26:41 -04002572 forbid_parse_serverhello,
Kenny Rootb8494592015-09-25 02:29:14 +00002573 ext_sigalgs_parse_clienthello,
David Benjaminc895d6b2016-08-11 13:26:41 -04002574 dont_add_serverhello,
Kenny Rootb8494592015-09-25 02:29:14 +00002575 },
2576 {
2577 TLSEXT_TYPE_status_request,
Steven Valdezb0b45c62017-01-17 16:23:54 -05002578 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002579 ext_ocsp_add_clienthello,
2580 ext_ocsp_parse_serverhello,
2581 ext_ocsp_parse_clienthello,
2582 ext_ocsp_add_serverhello,
2583 },
2584 {
2585 TLSEXT_TYPE_next_proto_neg,
David Benjamin95add822016-10-19 01:09:12 -04002586 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002587 ext_npn_add_clienthello,
2588 ext_npn_parse_serverhello,
2589 ext_npn_parse_clienthello,
2590 ext_npn_add_serverhello,
2591 },
2592 {
2593 TLSEXT_TYPE_certificate_timestamp,
2594 NULL,
2595 ext_sct_add_clienthello,
2596 ext_sct_parse_serverhello,
2597 ext_sct_parse_clienthello,
2598 ext_sct_add_serverhello,
2599 },
2600 {
2601 TLSEXT_TYPE_application_layer_protocol_negotiation,
Steven Valdez909b19f2016-11-21 15:35:44 -05002602 NULL,
Kenny Rootb8494592015-09-25 02:29:14 +00002603 ext_alpn_add_clienthello,
2604 ext_alpn_parse_serverhello,
Steven Valdez909b19f2016-11-21 15:35:44 -05002605 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2606 ignore_parse_clienthello,
Kenny Rootb8494592015-09-25 02:29:14 +00002607 ext_alpn_add_serverhello,
2608 },
2609 {
2610 TLSEXT_TYPE_channel_id,
2611 ext_channel_id_init,
2612 ext_channel_id_add_clienthello,
2613 ext_channel_id_parse_serverhello,
2614 ext_channel_id_parse_clienthello,
2615 ext_channel_id_add_serverhello,
2616 },
2617 {
2618 TLSEXT_TYPE_srtp,
2619 ext_srtp_init,
2620 ext_srtp_add_clienthello,
2621 ext_srtp_parse_serverhello,
2622 ext_srtp_parse_clienthello,
2623 ext_srtp_add_serverhello,
2624 },
2625 {
2626 TLSEXT_TYPE_ec_point_formats,
2627 NULL,
2628 ext_ec_point_add_clienthello,
2629 ext_ec_point_parse_serverhello,
2630 ext_ec_point_parse_clienthello,
2631 ext_ec_point_add_serverhello,
2632 },
David Benjaminc895d6b2016-08-11 13:26:41 -04002633 {
David Benjaminc895d6b2016-08-11 13:26:41 -04002634 TLSEXT_TYPE_key_share,
2635 NULL,
2636 ext_key_share_add_clienthello,
2637 forbid_parse_serverhello,
2638 ignore_parse_clienthello,
2639 dont_add_serverhello,
2640 },
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002641 {
Steven Valdez909b19f2016-11-21 15:35:44 -05002642 TLSEXT_TYPE_psk_key_exchange_modes,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002643 NULL,
Steven Valdez909b19f2016-11-21 15:35:44 -05002644 ext_psk_key_exchange_modes_add_clienthello,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002645 forbid_parse_serverhello,
Steven Valdez909b19f2016-11-21 15:35:44 -05002646 ext_psk_key_exchange_modes_parse_clienthello,
David Benjaminf0c4a6c2016-08-11 13:26:41 -04002647 dont_add_serverhello,
2648 },
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002649 {
David Benjamin1b249672016-12-06 18:25:50 -05002650 TLSEXT_TYPE_early_data,
2651 NULL,
2652 ext_early_data_add_clienthello,
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002653 ext_early_data_parse_serverhello,
David Benjamin1b249672016-12-06 18:25:50 -05002654 ext_early_data_parse_clienthello,
Robert Sloan6d0d00e2017-03-27 07:13:07 -07002655 ext_early_data_add_serverhello,
David Benjamin1b249672016-12-06 18:25:50 -05002656 },
2657 {
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002658 TLSEXT_TYPE_supported_versions,
2659 NULL,
2660 ext_supported_versions_add_clienthello,
2661 forbid_parse_serverhello,
2662 ignore_parse_clienthello,
2663 dont_add_serverhello,
2664 },
David Benjamin95add822016-10-19 01:09:12 -04002665 {
2666 TLSEXT_TYPE_cookie,
2667 NULL,
2668 ext_cookie_add_clienthello,
2669 forbid_parse_serverhello,
2670 ignore_parse_clienthello,
2671 dont_add_serverhello,
2672 },
David Benjamin4969cc92016-04-22 15:02:23 -04002673 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2674 * intolerant to the last extension being zero-length. See
2675 * https://crbug.com/363583. */
Kenny Rootb8494592015-09-25 02:29:14 +00002676 {
David Benjamind316cba2016-06-02 16:17:39 -04002677 TLSEXT_TYPE_supported_groups,
David Benjamin95add822016-10-19 01:09:12 -04002678 NULL,
David Benjamind316cba2016-06-02 16:17:39 -04002679 ext_supported_groups_add_clienthello,
2680 ext_supported_groups_parse_serverhello,
2681 ext_supported_groups_parse_clienthello,
2682 ext_supported_groups_add_serverhello,
Kenny Rootb8494592015-09-25 02:29:14 +00002683 },
2684};
2685
2686#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2687
2688OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjamin95add822016-10-19 01:09:12 -04002689 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
Kenny Rootb8494592015-09-25 02:29:14 +00002690 too_many_extensions_for_sent_bitset);
David Benjamin95add822016-10-19 01:09:12 -04002691OPENSSL_COMPILE_ASSERT(
2692 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2693 too_many_extensions_for_received_bitset);
Kenny Rootb8494592015-09-25 02:29:14 +00002694
2695static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2696 uint16_t value) {
2697 unsigned i;
2698 for (i = 0; i < kNumExtensions; i++) {
2699 if (kExtensions[i].value == value) {
2700 *out_index = i;
2701 return &kExtensions[i];
2702 }
2703 }
2704
2705 return NULL;
2706}
2707
2708int SSL_extension_supported(unsigned extension_value) {
2709 uint32_t index;
2710 return extension_value == TLSEXT_TYPE_padding ||
2711 tls_extension_find(&index, extension_value) != NULL;
2712}
2713
David Benjamin1b249672016-12-06 18:25:50 -05002714int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2715 SSL *const ssl = hs->ssl;
2716 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2717 if (hs->client_version == SSL3_VERSION &&
Kenny Roote99801b2015-11-06 15:31:15 -08002718 !ssl->s3->send_connection_binding) {
2719 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002720 }
2721
Kenny Roote99801b2015-11-06 15:31:15 -08002722 CBB extensions;
2723 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002724 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002725 }
2726
David Benjamin1b249672016-12-06 18:25:50 -05002727 hs->extensions.sent = 0;
2728 hs->custom_extensions.sent = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002729
David Benjamin7c0d06c2016-08-11 13:26:41 -04002730 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00002731 if (kExtensions[i].init != NULL) {
David Benjamin1b249672016-12-06 18:25:50 -05002732 kExtensions[i].init(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002733 }
2734 }
2735
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002736 uint16_t grease_ext1 = 0;
2737 if (ssl->ctx->grease_enabled) {
2738 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2739 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2740 if (!CBB_add_u16(&extensions, grease_ext1) ||
2741 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2742 goto err;
2743 }
2744 }
2745
David Benjamin7c0d06c2016-08-11 13:26:41 -04002746 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00002747 const size_t len_before = CBB_len(&extensions);
David Benjamin1b249672016-12-06 18:25:50 -05002748 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002749 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05002750 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Kenny Rootb8494592015-09-25 02:29:14 +00002751 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002752 }
Kenny Rootb8494592015-09-25 02:29:14 +00002753
2754 if (CBB_len(&extensions) != len_before) {
David Benjamin1b249672016-12-06 18:25:50 -05002755 hs->extensions.sent |= (1u << i);
Kenny Rootb8494592015-09-25 02:29:14 +00002756 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002757 }
2758
David Benjamin1b249672016-12-06 18:25:50 -05002759 if (!custom_ext_add_clienthello(hs, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002760 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002761 }
2762
Steven Valdezbb1ceac2016-10-07 10:34:51 -04002763 if (ssl->ctx->grease_enabled) {
2764 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2765 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2766
2767 /* The two fake extensions must not have the same value. GREASE values are
2768 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2769 * one. */
2770 if (grease_ext1 == grease_ext2) {
2771 grease_ext2 ^= 0x1010;
2772 }
2773
2774 if (!CBB_add_u16(&extensions, grease_ext2) ||
2775 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2776 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2777 goto err;
2778 }
2779 }
2780
David Benjaminc895d6b2016-08-11 13:26:41 -04002781 if (!SSL_is_dtls(ssl)) {
David Benjamin1b249672016-12-06 18:25:50 -05002782 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdez909b19f2016-11-21 15:35:44 -05002783 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002784 if (header_len > 0xff && header_len < 0x200) {
Kenny Roote99801b2015-11-06 15:31:15 -08002785 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyd9e397b2015-01-22 14:27:53 -08002786 *
2787 * NB: because this code works out the length of all existing extensions
2788 * it MUST always appear last. */
Kenny Rootb8494592015-09-25 02:29:14 +00002789 size_t padding_len = 0x200 - header_len;
David Benjamin4969cc92016-04-22 15:02:23 -04002790 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyd9e397b2015-01-22 14:27:53 -08002791 * one byte of data if including the extension. WebSphere Application
David Benjamin4969cc92016-04-22 15:02:23 -04002792 * Server 7.0 is intolerant to the last extension being zero-length. See
2793 * https://crbug.com/363583. */
Adam Langleyd9e397b2015-01-22 14:27:53 -08002794 if (padding_len >= 4 + 1) {
2795 padding_len -= 4;
2796 } else {
2797 padding_len = 1;
2798 }
2799
Kenny Rootb8494592015-09-25 02:29:14 +00002800 uint8_t *padding_bytes;
2801 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2802 !CBB_add_u16(&extensions, padding_len) ||
2803 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2804 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002805 }
2806
Robert Sloan69939df2017-01-09 10:53:07 -08002807 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002808 }
2809 }
2810
Steven Valdez909b19f2016-11-21 15:35:44 -05002811 /* The PSK extension must be last, including after the padding. */
David Benjamin1b249672016-12-06 18:25:50 -05002812 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Steven Valdez909b19f2016-11-21 15:35:44 -05002813 goto err;
2814 }
2815
Adam Langley4139edb2016-01-13 15:00:54 -08002816 /* Discard empty extensions blocks. */
2817 if (CBB_len(&extensions) == 0) {
Kenny Roote99801b2015-11-06 15:31:15 -08002818 CBB_discard_child(out);
Kenny Rootb8494592015-09-25 02:29:14 +00002819 }
2820
Kenny Roote99801b2015-11-06 15:31:15 -08002821 return CBB_flush(out);
Kenny Rootb8494592015-09-25 02:29:14 +00002822
2823err:
Kenny Rootb8494592015-09-25 02:29:14 +00002824 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Kenny Roote99801b2015-11-06 15:31:15 -08002825 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002826}
2827
David Benjamin1b249672016-12-06 18:25:50 -05002828int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2829 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -08002830 CBB extensions;
2831 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002832 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002833 }
2834
David Benjamin1b249672016-12-06 18:25:50 -05002835 for (unsigned i = 0; i < kNumExtensions; i++) {
2836 if (!(hs->extensions.received & (1u << i))) {
Kenny Rootb8494592015-09-25 02:29:14 +00002837 /* Don't send extensions that were not received. */
2838 continue;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002839 }
2840
David Benjamin1b249672016-12-06 18:25:50 -05002841 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002842 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05002843 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Kenny Rootb8494592015-09-25 02:29:14 +00002844 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002845 }
2846 }
2847
David Benjamin1b249672016-12-06 18:25:50 -05002848 if (!custom_ext_add_serverhello(hs, &extensions)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002849 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002850 }
2851
David Benjaminc895d6b2016-08-11 13:26:41 -04002852 /* Discard empty extensions blocks before TLS 1.3. */
2853 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2854 CBB_len(&extensions) == 0) {
Kenny Roote99801b2015-11-06 15:31:15 -08002855 CBB_discard_child(out);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002856 }
2857
Kenny Roote99801b2015-11-06 15:31:15 -08002858 return CBB_flush(out);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002859
Kenny Rootb8494592015-09-25 02:29:14 +00002860err:
Kenny Rootb8494592015-09-25 02:29:14 +00002861 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Kenny Roote99801b2015-11-06 15:31:15 -08002862 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002863}
2864
David Benjamin1b249672016-12-06 18:25:50 -05002865static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2866 const SSL_CLIENT_HELLO *client_hello,
2867 int *out_alert) {
2868 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04002869 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00002870 if (kExtensions[i].init != NULL) {
David Benjamin1b249672016-12-06 18:25:50 -05002871 kExtensions[i].init(hs);
Kenny Rootb8494592015-09-25 02:29:14 +00002872 }
2873 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002874
David Benjamin1b249672016-12-06 18:25:50 -05002875 hs->extensions.received = 0;
2876 hs->custom_extensions.received = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002877
David Benjaminc895d6b2016-08-11 13:26:41 -04002878 CBS extensions;
2879 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2880 while (CBS_len(&extensions) != 0) {
2881 uint16_t type;
2882 CBS extension;
2883
2884 /* Decode the next extension. */
2885 if (!CBS_get_u16(&extensions, &type) ||
2886 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08002887 *out_alert = SSL_AD_DECODE_ERROR;
2888 return 0;
2889 }
2890
David Benjaminc895d6b2016-08-11 13:26:41 -04002891 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2892 * ambiguous. Ignore all but the renegotiation_info extension. */
2893 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2894 continue;
2895 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002896
David Benjaminc895d6b2016-08-11 13:26:41 -04002897 unsigned ext_index;
2898 const struct tls_extension *const ext =
2899 tls_extension_find(&ext_index, type);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002900
David Benjaminc895d6b2016-08-11 13:26:41 -04002901 if (ext == NULL) {
David Benjamin1b249672016-12-06 18:25:50 -05002902 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Kenny Rootb8494592015-09-25 02:29:14 +00002903 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002904 return 0;
2905 }
David Benjaminc895d6b2016-08-11 13:26:41 -04002906 continue;
2907 }
2908
David Benjamin1b249672016-12-06 18:25:50 -05002909 hs->extensions.received |= (1u << ext_index);
David Benjaminc895d6b2016-08-11 13:26:41 -04002910 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05002911 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002912 *out_alert = alert;
2913 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05002914 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjaminc895d6b2016-08-11 13:26:41 -04002915 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08002916 }
2917 }
2918
David Benjaminc895d6b2016-08-11 13:26:41 -04002919 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin1b249672016-12-06 18:25:50 -05002920 if (hs->extensions.received & (1u << i)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002921 continue;
2922 }
2923
2924 CBS *contents = NULL, fake_contents;
2925 static const uint8_t kFakeRenegotiateExtension[] = {0};
2926 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2927 ssl_client_cipher_list_contains_cipher(client_hello,
2928 SSL3_CK_SCSV & 0xffff)) {
2929 /* The renegotiation SCSV was received so pretend that we received a
2930 * renegotiation extension. */
2931 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2932 sizeof(kFakeRenegotiateExtension));
2933 contents = &fake_contents;
David Benjamin1b249672016-12-06 18:25:50 -05002934 hs->extensions.received |= (1u << i);
David Benjaminc895d6b2016-08-11 13:26:41 -04002935 }
2936
2937 /* Extension wasn't observed so call the callback with a NULL
2938 * parameter. */
2939 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05002940 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04002941 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05002942 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjaminc895d6b2016-08-11 13:26:41 -04002943 *out_alert = alert;
2944 return 0;
Kenny Rootb8494592015-09-25 02:29:14 +00002945 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08002946 }
2947
2948 return 1;
2949}
2950
David Benjamin1b249672016-12-06 18:25:50 -05002951int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2952 const SSL_CLIENT_HELLO *client_hello) {
2953 SSL *const ssl = hs->ssl;
Robert Sloan5d625782017-02-13 09:55:39 -08002954 int alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05002955 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08002956 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002957 return 0;
2958 }
2959
David Benjamin1b249672016-12-06 18:25:50 -05002960 if (ssl_check_clienthello_tlsext(hs) <= 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00002961 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyd9e397b2015-01-22 14:27:53 -08002962 return 0;
2963 }
2964
2965 return 1;
2966}
2967
David Benjamin1b249672016-12-06 18:25:50 -05002968static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2969 int *out_alert) {
2970 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04002971 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2972 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2973 return 1;
2974 }
Kenny Roota04d78d2015-09-25 00:26:37 +00002975
David Benjaminc895d6b2016-08-11 13:26:41 -04002976 /* Decode the extensions block and check it is valid. */
2977 CBS extensions;
2978 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2979 !tls1_check_duplicate_extensions(&extensions)) {
2980 *out_alert = SSL_AD_DECODE_ERROR;
2981 return 0;
2982 }
2983
2984 uint32_t received = 0;
2985 while (CBS_len(&extensions) != 0) {
2986 uint16_t type;
2987 CBS extension;
2988
2989 /* Decode the next extension. */
2990 if (!CBS_get_u16(&extensions, &type) ||
2991 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Kenny Roota04d78d2015-09-25 00:26:37 +00002992 *out_alert = SSL_AD_DECODE_ERROR;
2993 return 0;
2994 }
2995
David Benjaminc895d6b2016-08-11 13:26:41 -04002996 unsigned ext_index;
2997 const struct tls_extension *const ext =
2998 tls_extension_find(&ext_index, type);
Kenny Rootb8494592015-09-25 02:29:14 +00002999
David Benjaminc895d6b2016-08-11 13:26:41 -04003000 if (ext == NULL) {
David Benjamin1b249672016-12-06 18:25:50 -05003001 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Kenny Roota04d78d2015-09-25 00:26:37 +00003002 return 0;
3003 }
David Benjaminc895d6b2016-08-11 13:26:41 -04003004 continue;
3005 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003006
David Benjaminf31229b2017-01-25 14:08:15 -05003007 OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
3008 too_many_bits);
3009
David Benjamin1b249672016-12-06 18:25:50 -05003010 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjaminc895d6b2016-08-11 13:26:41 -04003011 type != TLSEXT_TYPE_renegotiate) {
3012 /* If the extension was never sent then it is illegal, except for the
3013 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
3014 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3015 ERR_add_error_dataf("extension :%u", (unsigned)type);
3016 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
3017 return 0;
3018 }
Kenny Rootb8494592015-09-25 02:29:14 +00003019
David Benjaminc895d6b2016-08-11 13:26:41 -04003020 received |= (1u << ext_index);
Kenny Roota04d78d2015-09-25 00:26:37 +00003021
David Benjaminc895d6b2016-08-11 13:26:41 -04003022 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003023 if (!ext->parse_serverhello(hs, &alert, &extension)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003024 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003025 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjaminc895d6b2016-08-11 13:26:41 -04003026 *out_alert = alert;
3027 return 0;
Kenny Roota04d78d2015-09-25 00:26:37 +00003028 }
3029 }
3030
David Benjamin7c0d06c2016-08-11 13:26:41 -04003031 for (size_t i = 0; i < kNumExtensions; i++) {
Kenny Rootb8494592015-09-25 02:29:14 +00003032 if (!(received & (1u << i))) {
3033 /* Extension wasn't observed so call the callback with a NULL
3034 * parameter. */
3035 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003036 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Kenny Rootb8494592015-09-25 02:29:14 +00003037 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Steven Valdez909b19f2016-11-21 15:35:44 -05003038 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Kenny Rootb8494592015-09-25 02:29:14 +00003039 *out_alert = alert;
Kenny Roota04d78d2015-09-25 00:26:37 +00003040 return 0;
3041 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003042 }
3043 }
3044
Kenny Roota04d78d2015-09-25 00:26:37 +00003045 return 1;
3046}
3047
David Benjamin1b249672016-12-06 18:25:50 -05003048static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3049 SSL *const ssl = hs->ssl;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003050 int ret = SSL_TLSEXT_ERR_NOACK;
3051 int al = SSL_AD_UNRECOGNIZED_NAME;
3052
David Benjamin4969cc92016-04-22 15:02:23 -04003053 if (ssl->ctx->tlsext_servername_callback != 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08003054 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin4969cc92016-04-22 15:02:23 -04003055 ssl->ctx->tlsext_servername_arg);
Robert Sloan1c9db532017-03-13 08:03:59 -07003056 } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
3057 ret = ssl->session_ctx->tlsext_servername_callback(
3058 ssl, &al, ssl->session_ctx->tlsext_servername_arg);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003059 }
3060
3061 switch (ret) {
3062 case SSL_TLSEXT_ERR_ALERT_FATAL:
Adam Langley4139edb2016-01-13 15:00:54 -08003063 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003064 return -1;
3065
Adam Langleyd9e397b2015-01-22 14:27:53 -08003066 case SSL_TLSEXT_ERR_NOACK:
David Benjamin1b249672016-12-06 18:25:50 -05003067 hs->should_ack_sni = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003068 return 1;
3069
3070 default:
3071 return 1;
3072 }
3073}
3074
David Benjamin1b249672016-12-06 18:25:50 -05003075int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3076 SSL *const ssl = hs->ssl;
Robert Sloan5d625782017-02-13 09:55:39 -08003077 int alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05003078 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08003079 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003080 return 0;
3081 }
3082
Adam Langleyd9e397b2015-01-22 14:27:53 -08003083 return 1;
3084}
3085
Robert Sloan1c9db532017-03-13 08:03:59 -07003086static enum ssl_ticket_aead_result_t
3087ssl_decrypt_ticket_with_cipher_ctx(SSL *ssl, uint8_t **out, size_t *out_len,
3088 int *out_renew_ticket, const uint8_t *ticket,
3089 size_t ticket_len) {
3090 enum ssl_ticket_aead_result_t ret = ssl_ticket_aead_ignore_ticket;
3091 const SSL_CTX *const ssl_ctx = ssl->session_ctx;
Kenny Rootb8494592015-09-25 02:29:14 +00003092 uint8_t *plaintext = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003093
Kenny Rootb8494592015-09-25 02:29:14 +00003094 HMAC_CTX hmac_ctx;
3095 HMAC_CTX_init(&hmac_ctx);
3096 EVP_CIPHER_CTX cipher_ctx;
3097 EVP_CIPHER_CTX_init(&cipher_ctx);
3098
Adam Langleye9ada862015-05-11 17:20:37 -07003099 /* Ensure there is room for the key name and the largest IV
3100 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3101 * the maximum IV length should be well under the minimum size for the
3102 * session material and HMAC. */
Kenny Rootb8494592015-09-25 02:29:14 +00003103 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
Robert Sloan1c9db532017-03-13 08:03:59 -07003104 goto out;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003105 }
Kenny Rootb8494592015-09-25 02:29:14 +00003106 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003107
Kenny Rootb8494592015-09-25 02:29:14 +00003108 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -08003109 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3110 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3111 &hmac_ctx, 0 /* decrypt */);
Kenny Rootb8494592015-09-25 02:29:14 +00003112 if (cb_ret < 0) {
Robert Sloan1c9db532017-03-13 08:03:59 -07003113 ret = ssl_ticket_aead_error;
3114 goto out;
3115 } else if (cb_ret == 0) {
3116 goto out;
3117 } else if (cb_ret == 2) {
David Benjamin4969cc92016-04-22 15:02:23 -04003118 *out_renew_ticket = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003119 }
3120 } else {
Kenny Rootb8494592015-09-25 02:29:14 +00003121 /* Check the key name matches. */
Robert Sloan69939df2017-01-09 10:53:07 -08003122 if (OPENSSL_memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3123 SSL_TICKET_KEY_NAME_LEN) != 0) {
Robert Sloan1c9db532017-03-13 08:03:59 -07003124 goto out;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003125 }
Kenny Rootb8494592015-09-25 02:29:14 +00003126 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3127 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyd9e397b2015-01-22 14:27:53 -08003128 NULL) ||
Kenny Rootb8494592015-09-25 02:29:14 +00003129 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3130 ssl_ctx->tlsext_tick_aes_key, iv)) {
Robert Sloan1c9db532017-03-13 08:03:59 -07003131 ret = ssl_ticket_aead_error;
3132 goto out;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003133 }
3134 }
Kenny Rootb8494592015-09-25 02:29:14 +00003135 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -08003136
Kenny Rootb8494592015-09-25 02:29:14 +00003137 /* Check the MAC at the end of the ticket. */
3138 uint8_t mac[EVP_MAX_MD_SIZE];
3139 size_t mac_len = HMAC_size(&hmac_ctx);
3140 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
Adam Langleye9ada862015-05-11 17:20:37 -07003141 /* The ticket must be large enough for key name, IV, data, and MAC. */
Robert Sloan1c9db532017-03-13 08:03:59 -07003142 goto out;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003143 }
Kenny Rootb8494592015-09-25 02:29:14 +00003144 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3145 HMAC_Final(&hmac_ctx, mac, NULL);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003146 int mac_ok =
3147 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3148#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3149 mac_ok = 1;
3150#endif
3151 if (!mac_ok) {
Robert Sloan1c9db532017-03-13 08:03:59 -07003152 goto out;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003153 }
3154
Kenny Rootb8494592015-09-25 02:29:14 +00003155 /* Decrypt the session data. */
3156 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3157 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3158 mac_len;
3159 plaintext = OPENSSL_malloc(ciphertext_len);
3160 if (plaintext == NULL) {
Robert Sloan1c9db532017-03-13 08:03:59 -07003161 ret = ssl_ticket_aead_error;
3162 goto out;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003163 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003164 size_t plaintext_len;
3165#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
Robert Sloan69939df2017-01-09 10:53:07 -08003166 OPENSSL_memcpy(plaintext, ciphertext, ciphertext_len);
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003167 plaintext_len = ciphertext_len;
3168#else
Kenny Rootb8494592015-09-25 02:29:14 +00003169 if (ciphertext_len >= INT_MAX) {
Robert Sloan1c9db532017-03-13 08:03:59 -07003170 goto out;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003171 }
Kenny Rootb8494592015-09-25 02:29:14 +00003172 int len1, len2;
3173 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3174 (int)ciphertext_len) ||
3175 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
Robert Sloan1c9db532017-03-13 08:03:59 -07003176 ERR_clear_error();
3177 goto out;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003178 }
Robert Sloan1c9db532017-03-13 08:03:59 -07003179 plaintext_len = (size_t)(len1) + len2;
Steven Valdezbb1ceac2016-10-07 10:34:51 -04003180#endif
Adam Langleyd9e397b2015-01-22 14:27:53 -08003181
Robert Sloan1c9db532017-03-13 08:03:59 -07003182 *out = plaintext;
3183 plaintext = NULL;
3184 *out_len = plaintext_len;
3185 ret = ssl_ticket_aead_success;
3186
3187out:
3188 OPENSSL_free(plaintext);
3189 HMAC_CTX_cleanup(&hmac_ctx);
3190 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3191 return ret;
3192}
3193
3194static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
3195 SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
3196 const uint8_t *ticket, size_t ticket_len) {
3197 uint8_t *plaintext = OPENSSL_malloc(ticket_len);
3198 if (plaintext == NULL) {
3199 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
3200 return ssl_ticket_aead_error;
3201 }
3202
3203 size_t plaintext_len;
3204 const enum ssl_ticket_aead_result_t result =
3205 ssl->session_ctx->ticket_aead_method->open(
3206 ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
3207
3208 if (result == ssl_ticket_aead_success) {
3209 *out = plaintext;
3210 plaintext = NULL;
3211 *out_len = plaintext_len;
3212 }
3213
3214 OPENSSL_free(plaintext);
3215 return result;
3216}
3217
3218enum ssl_ticket_aead_result_t ssl_process_ticket(
3219 SSL *ssl, SSL_SESSION **out_session, int *out_renew_ticket,
3220 const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
3221 size_t session_id_len) {
3222 *out_renew_ticket = 0;
3223 *out_session = NULL;
3224
3225 if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
3226 session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3227 return ssl_ticket_aead_ignore_ticket;
3228 }
3229
3230 uint8_t *plaintext = NULL;
3231 size_t plaintext_len;
3232 enum ssl_ticket_aead_result_t result;
3233 if (ssl->session_ctx->ticket_aead_method != NULL) {
3234 result = ssl_decrypt_ticket_with_method(
3235 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3236 } else {
3237 result = ssl_decrypt_ticket_with_cipher_ctx(
3238 ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
3239 }
3240
3241 if (result != ssl_ticket_aead_success) {
3242 return result;
3243 }
3244
Kenny Rootb8494592015-09-25 02:29:14 +00003245 /* Decode the session. */
Robert Sloan5d625782017-02-13 09:55:39 -08003246 SSL_SESSION *session =
3247 SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx);
Robert Sloan1c9db532017-03-13 08:03:59 -07003248 OPENSSL_free(plaintext);
3249
Kenny Rootb8494592015-09-25 02:29:14 +00003250 if (session == NULL) {
3251 ERR_clear_error(); /* Don't leave an error on the queue. */
Robert Sloan1c9db532017-03-13 08:03:59 -07003252 return ssl_ticket_aead_ignore_ticket;
Kenny Rootb8494592015-09-25 02:29:14 +00003253 }
3254
3255 /* Copy the client's session ID into the new session, to denote the ticket has
3256 * been accepted. */
Robert Sloan69939df2017-01-09 10:53:07 -08003257 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
Kenny Rootb8494592015-09-25 02:29:14 +00003258 session->session_id_length = session_id_len;
3259
3260 *out_session = session;
Robert Sloan1c9db532017-03-13 08:03:59 -07003261 return ssl_ticket_aead_success;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003262}
3263
David Benjamin1b249672016-12-06 18:25:50 -05003264int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Kenny Roota04d78d2015-09-25 00:26:37 +00003265 /* Extension ignored for inappropriate versions */
David Benjamin1b249672016-12-06 18:25:50 -05003266 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Kenny Roota04d78d2015-09-25 00:26:37 +00003267 return 1;
3268 }
3269
David Benjaminf0c4a6c2016-08-11 13:26:41 -04003270 OPENSSL_free(hs->peer_sigalgs);
3271 hs->peer_sigalgs = NULL;
3272 hs->num_peer_sigalgs = 0;
Kenny Rootb8494592015-09-25 02:29:14 +00003273
3274 size_t num_sigalgs = CBS_len(in_sigalgs);
Kenny Rootb8494592015-09-25 02:29:14 +00003275 if (num_sigalgs % 2 != 0) {
Kenny Roota04d78d2015-09-25 00:26:37 +00003276 return 0;
3277 }
Kenny Rootb8494592015-09-25 02:29:14 +00003278 num_sigalgs /= 2;
3279
3280 /* supported_signature_algorithms in the certificate request is
3281 * allowed to be empty. */
3282 if (num_sigalgs == 0) {
3283 return 1;
3284 }
3285
David Benjaminc895d6b2016-08-11 13:26:41 -04003286 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3287 * and we just divided |num_sigalgs| by two. */
David Benjaminf0c4a6c2016-08-11 13:26:41 -04003288 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3289 if (hs->peer_sigalgs == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00003290 return 0;
3291 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -04003292 hs->num_peer_sigalgs = num_sigalgs;
Kenny Rootb8494592015-09-25 02:29:14 +00003293
3294 CBS sigalgs;
3295 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjaminf0c4a6c2016-08-11 13:26:41 -04003296 for (size_t i = 0; i < num_sigalgs; i++) {
3297 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Kenny Rootb8494592015-09-25 02:29:14 +00003298 return 0;
3299 }
3300 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003301
Adam Langleyd9e397b2015-01-22 14:27:53 -08003302 return 1;
3303}
3304
David Benjamin1b249672016-12-06 18:25:50 -05003305int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3306 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00003307 CERT *cert = ssl->cert;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003308
David Benjaminc895d6b2016-08-11 13:26:41 -04003309 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3310 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3311 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
3312 int type = ssl_private_key_type(ssl);
3313 if (type == NID_rsaEncryption) {
3314 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3315 return 1;
3316 }
3317 if (ssl_is_ecdsa_key_type(type)) {
3318 *out = SSL_SIGN_ECDSA_SHA1;
3319 return 1;
3320 }
3321 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3322 return 0;
Kenny Rootb8494592015-09-25 02:29:14 +00003323 }
3324
David Benjamin95add822016-10-19 01:09:12 -04003325 const uint16_t *sigalgs = cert->sigalgs;
3326 size_t num_sigalgs = cert->num_sigalgs;
3327 if (sigalgs == NULL) {
3328 sigalgs = kSignSignatureAlgorithms;
3329 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
David Benjaminc895d6b2016-08-11 13:26:41 -04003330 }
Kenny Rootb8494592015-09-25 02:29:14 +00003331
David Benjaminf0c4a6c2016-08-11 13:26:41 -04003332 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3333 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3334 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003335 /* If the client didn't specify any signature_algorithms extension then
3336 * we can assume that it supports SHA1. See
3337 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3338 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3339 SSL_SIGN_ECDSA_SHA1};
3340 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04003341 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminc895d6b2016-08-11 13:26:41 -04003342 }
3343
David Benjaminf0c4a6c2016-08-11 13:26:41 -04003344 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003345 uint16_t sigalg = sigalgs[i];
3346 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3347 * negotiated. */
3348 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3349 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3350 continue;
3351 }
3352
David Benjaminf0c4a6c2016-08-11 13:26:41 -04003353 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjaminc895d6b2016-08-11 13:26:41 -04003354 if (sigalg == peer_sigalgs[j]) {
3355 *out = sigalg;
3356 return 1;
3357 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003358 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08003359 }
3360
David Benjaminc895d6b2016-08-11 13:26:41 -04003361 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3362 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003363}
3364
Robert Sloan5d625782017-02-13 09:55:39 -08003365int tls1_verify_channel_id(SSL_HANDSHAKE *hs) {
3366 SSL *const ssl = hs->ssl;
Kenny Rootb8494592015-09-25 02:29:14 +00003367 int ret = 0;
Steven Valdez909b19f2016-11-21 15:35:44 -05003368 uint16_t extension_type;
3369 CBS extension, channel_id;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003370
Steven Valdez909b19f2016-11-21 15:35:44 -05003371 /* A Channel ID handshake message is structured to contain multiple
3372 * extensions, but the only one that can be present is Channel ID. */
3373 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3374 if (!CBS_get_u16(&channel_id, &extension_type) ||
3375 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3376 CBS_len(&channel_id) != 0 ||
3377 extension_type != TLSEXT_TYPE_channel_id ||
3378 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3379 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3380 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3381 return 0;
3382 }
3383
3384 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3385 if (!p256) {
3386 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3387 return 0;
3388 }
3389
3390 EC_KEY *key = NULL;
3391 EC_POINT *point = NULL;
3392 BIGNUM x, y;
3393 ECDSA_SIG sig;
3394 BN_init(&x);
3395 BN_init(&y);
3396 sig.r = BN_new();
3397 sig.s = BN_new();
3398 if (sig.r == NULL || sig.s == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00003399 goto err;
Adam Langley1e4884f2015-09-24 10:57:52 -07003400 }
Kenny Roota04d78d2015-09-25 00:26:37 +00003401
Steven Valdez909b19f2016-11-21 15:35:44 -05003402 const uint8_t *p = CBS_data(&extension);
3403 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3404 BN_bin2bn(p + 32, 32, &y) == NULL ||
3405 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3406 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3407 goto err;
3408 }
3409
3410 point = EC_POINT_new(p256);
3411 if (point == NULL ||
3412 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3413 goto err;
3414 }
3415
3416 key = EC_KEY_new();
3417 if (key == NULL ||
3418 !EC_KEY_set_group(key, p256) ||
3419 !EC_KEY_set_public_key(key, point)) {
3420 goto err;
3421 }
3422
3423 uint8_t digest[EVP_MAX_MD_SIZE];
3424 size_t digest_len;
Robert Sloan5d625782017-02-13 09:55:39 -08003425 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Steven Valdez909b19f2016-11-21 15:35:44 -05003426 goto err;
3427 }
3428
3429 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3430#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3431 sig_ok = 1;
3432#endif
3433 if (!sig_ok) {
3434 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3435 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3436 ssl->s3->tlsext_channel_id_valid = 0;
3437 goto err;
3438 }
3439
Robert Sloan69939df2017-01-09 10:53:07 -08003440 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
Steven Valdez909b19f2016-11-21 15:35:44 -05003441 ret = 1;
3442
3443err:
3444 BN_free(&x);
3445 BN_free(&y);
3446 BN_free(sig.r);
3447 BN_free(sig.s);
3448 EC_KEY_free(key);
3449 EC_POINT_free(point);
3450 EC_GROUP_free(p256);
3451 return ret;
3452}
3453
Robert Sloan5d625782017-02-13 09:55:39 -08003454int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
3455 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -05003456 uint8_t digest[EVP_MAX_MD_SIZE];
3457 size_t digest_len;
Robert Sloan5d625782017-02-13 09:55:39 -08003458 if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
Steven Valdez909b19f2016-11-21 15:35:44 -05003459 return 0;
3460 }
3461
3462 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3463 if (ec_key == NULL) {
3464 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3465 return 0;
3466 }
3467
3468 int ret = 0;
3469 BIGNUM *x = BN_new();
3470 BIGNUM *y = BN_new();
3471 ECDSA_SIG *sig = NULL;
3472 if (x == NULL || y == NULL ||
3473 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3474 EC_KEY_get0_public_key(ec_key),
3475 x, y, NULL)) {
3476 goto err;
3477 }
3478
3479 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3480 if (sig == NULL) {
3481 goto err;
3482 }
3483
3484 CBB child;
3485 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3486 !CBB_add_u16_length_prefixed(cbb, &child) ||
3487 !BN_bn2cbb_padded(&child, 32, x) ||
3488 !BN_bn2cbb_padded(&child, 32, y) ||
3489 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3490 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3491 !CBB_flush(cbb)) {
3492 goto err;
3493 }
3494
3495 ret = 1;
3496
3497err:
3498 BN_free(x);
3499 BN_free(y);
3500 ECDSA_SIG_free(sig);
3501 return ret;
3502}
3503
Robert Sloan5d625782017-02-13 09:55:39 -08003504int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
3505 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -05003506 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3507 uint8_t *msg;
3508 size_t msg_len;
Robert Sloan5d625782017-02-13 09:55:39 -08003509 if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
Steven Valdez909b19f2016-11-21 15:35:44 -05003510 ssl_cert_verify_channel_id)) {
3511 return 0;
3512 }
3513 SHA256(msg, msg_len, out);
3514 *out_len = SHA256_DIGEST_LENGTH;
3515 OPENSSL_free(msg);
3516 return 1;
3517 }
3518
3519 SHA256_CTX ctx;
3520
3521 SHA256_Init(&ctx);
Kenny Rootb8494592015-09-25 02:29:14 +00003522 static const char kClientIDMagic[] = "TLS Channel ID signature";
Steven Valdez909b19f2016-11-21 15:35:44 -05003523 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
Kenny Rootb8494592015-09-25 02:29:14 +00003524
David Benjaminc895d6b2016-08-11 13:26:41 -04003525 if (ssl->session != NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00003526 static const char kResumptionMagic[] = "Resumption";
Steven Valdez909b19f2016-11-21 15:35:44 -05003527 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
Kenny Rootb8494592015-09-25 02:29:14 +00003528 if (ssl->session->original_handshake_hash_len == 0) {
3529 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez909b19f2016-11-21 15:35:44 -05003530 return 0;
Kenny Rootb8494592015-09-25 02:29:14 +00003531 }
Steven Valdez909b19f2016-11-21 15:35:44 -05003532 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3533 ssl->session->original_handshake_hash_len);
Kenny Rootb8494592015-09-25 02:29:14 +00003534 }
3535
Robert Sloan5d625782017-02-13 09:55:39 -08003536 uint8_t hs_hash[EVP_MAX_MD_SIZE];
3537 size_t hs_hash_len;
3538 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript, hs_hash, &hs_hash_len)) {
Steven Valdez909b19f2016-11-21 15:35:44 -05003539 return 0;
Kenny Rootb8494592015-09-25 02:29:14 +00003540 }
Robert Sloan5d625782017-02-13 09:55:39 -08003541 SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
Steven Valdez909b19f2016-11-21 15:35:44 -05003542 SHA256_Final(out, &ctx);
3543 *out_len = SHA256_DIGEST_LENGTH;
3544 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003545}
3546
3547/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Robert Sloana94fe052017-02-21 08:49:28 -08003548 * hashes in |hs->new_session| so that Channel ID resumptions can sign that
Adam Langley4139edb2016-01-13 15:00:54 -08003549 * data. */
Robert Sloan5d625782017-02-13 09:55:39 -08003550int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
3551 SSL *const ssl = hs->ssl;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003552 /* This function should never be called for a resumed session because the
3553 * handshake hashes that we wish to record are for the original, full
3554 * handshake. */
David Benjaminc895d6b2016-08-11 13:26:41 -04003555 if (ssl->session != NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08003556 return -1;
3557 }
3558
Robert Sloan5d625782017-02-13 09:55:39 -08003559 OPENSSL_COMPILE_ASSERT(
Robert Sloana94fe052017-02-21 08:49:28 -08003560 sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
Robert Sloan5d625782017-02-13 09:55:39 -08003561 original_handshake_hash_is_too_small);
3562
3563 size_t digest_len;
3564 if (!SSL_TRANSCRIPT_get_hash(&hs->transcript,
Robert Sloana94fe052017-02-21 08:49:28 -08003565 hs->new_session->original_handshake_hash,
Robert Sloan5d625782017-02-13 09:55:39 -08003566 &digest_len)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08003567 return -1;
3568 }
3569
Robert Sloan5d625782017-02-13 09:55:39 -08003570 OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE <= 0xff, max_md_size_is_too_large);
Robert Sloana94fe052017-02-21 08:49:28 -08003571 hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -08003572
3573 return 1;
3574}
Steven Valdez909b19f2016-11-21 15:35:44 -05003575
3576int ssl_do_channel_id_callback(SSL *ssl) {
3577 if (ssl->tlsext_channel_id_private != NULL ||
3578 ssl->ctx->channel_id_cb == NULL) {
3579 return 1;
3580 }
3581
3582 EVP_PKEY *key = NULL;
3583 ssl->ctx->channel_id_cb(ssl, &key);
3584 if (key == NULL) {
3585 /* The caller should try again later. */
3586 return 1;
3587 }
3588
3589 int ret = SSL_set1_tls_channel_id(ssl, key);
3590 EVP_PKEY_free(key);
3591 return ret;
3592}
3593
3594int ssl_is_sct_list_valid(const CBS *contents) {
3595 /* Shallow parse the SCT list for sanity. By the RFC
3596 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3597 * of the SCTs may be empty. */
3598 CBS copy = *contents;
3599 CBS sct_list;
3600 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3601 CBS_len(&copy) != 0 ||
3602 CBS_len(&sct_list) == 0) {
3603 return 0;
3604 }
3605
3606 while (CBS_len(&sct_list) > 0) {
3607 CBS sct;
3608 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3609 CBS_len(&sct) == 0) {
3610 return 0;
3611 }
3612 }
3613
3614 return 1;
3615}