blob: 0f203147ac6e501632b59aefd25377aadef4fc1a [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin03973092014-06-24 23:27:17 -0400116#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400117#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/evp.h>
120#include <openssl/hmac.h>
121#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400122#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/rand.h>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700124#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400127#include "../crypto/internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800128
129
David Benjamin8c880a22016-12-03 02:20:34 -0500130static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
131static int ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs);
Adam Langley95c29f32014-06-20 12:00:00 -0700132
Adam Langleyfcf25832014-12-18 17:42:32 -0800133static int compare_uint16_t(const void *p1, const void *p2) {
134 uint16_t u1 = *((const uint16_t *)p1);
135 uint16_t u2 = *((const uint16_t *)p2);
136 if (u1 < u2) {
137 return -1;
138 } else if (u1 > u2) {
139 return 1;
140 } else {
141 return 0;
142 }
143}
David Benjamin35a7a442014-07-05 00:23:20 -0400144
Adam Langleyfcf25832014-12-18 17:42:32 -0800145/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
146 * more than one extension of the same type in a ClientHello or ServerHello.
147 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400148 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800149static int tls1_check_duplicate_extensions(const CBS *cbs) {
150 CBS extensions = *cbs;
151 size_t num_extensions = 0, i = 0;
152 uint16_t *extension_types = NULL;
153 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400154
Adam Langleyfcf25832014-12-18 17:42:32 -0800155 /* First pass: count the extensions. */
156 while (CBS_len(&extensions) > 0) {
157 uint16_t type;
158 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400159
Adam Langleyfcf25832014-12-18 17:42:32 -0800160 if (!CBS_get_u16(&extensions, &type) ||
161 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
162 goto done;
163 }
David Benjamin35a7a442014-07-05 00:23:20 -0400164
Adam Langleyfcf25832014-12-18 17:42:32 -0800165 num_extensions++;
166 }
David Benjamin35a7a442014-07-05 00:23:20 -0400167
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 if (num_extensions == 0) {
169 return 1;
170 }
David Benjamin9a373592014-07-25 04:27:53 -0400171
Brian Smith5ba06892016-02-07 09:36:04 -1000172 extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400174 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800175 goto done;
176 }
David Benjamin35a7a442014-07-05 00:23:20 -0400177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 /* Second pass: gather the extension types. */
179 extensions = *cbs;
180 for (i = 0; i < num_extensions; i++) {
181 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400182
Adam Langleyfcf25832014-12-18 17:42:32 -0800183 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
184 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
185 /* This should not happen. */
186 goto done;
187 }
188 }
189 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400190
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 /* Sort the extensions and make sure there are no duplicates. */
192 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
193 for (i = 1; i < num_extensions; i++) {
194 if (extension_types[i - 1] == extension_types[i]) {
195 goto done;
196 }
197 }
David Benjamin35a7a442014-07-05 00:23:20 -0400198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 ret = 1;
200
David Benjamin35a7a442014-07-05 00:23:20 -0400201done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400202 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 return ret;
204}
David Benjamin35a7a442014-07-05 00:23:20 -0400205
David Benjamin731058e2016-12-03 23:15:13 -0500206int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out, const uint8_t *in,
207 size_t in_len) {
208 memset(out, 0, sizeof(*out));
209 out->ssl = ssl;
210 out->client_hello = in;
211 out->client_hello_len = in_len;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400212
David Benjamine14ff062016-08-09 16:21:24 -0400213 CBS client_hello, random, session_id;
David Benjamin731058e2016-12-03 23:15:13 -0500214 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
215 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjamine14ff062016-08-09 16:21:24 -0400216 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
217 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
218 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 return 0;
220 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700221
David Benjamin731058e2016-12-03 23:15:13 -0500222 out->random = CBS_data(&random);
223 out->random_len = CBS_len(&random);
224 out->session_id = CBS_data(&session_id);
225 out->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700226
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 /* Skip past DTLS cookie */
David Benjamin731058e2016-12-03 23:15:13 -0500228 if (SSL_is_dtls(out->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400230 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
231 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 return 0;
233 }
234 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700235
David Benjamine14ff062016-08-09 16:21:24 -0400236 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400238 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
239 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 CBS_len(&compression_methods) < 1) {
241 return 0;
242 }
David Benjamine14ff062016-08-09 16:21:24 -0400243
David Benjamin731058e2016-12-03 23:15:13 -0500244 out->cipher_suites = CBS_data(&cipher_suites);
245 out->cipher_suites_len = CBS_len(&cipher_suites);
246 out->compression_methods = CBS_data(&compression_methods);
247 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 /* If the ClientHello ends here then it's valid, but doesn't have any
250 * extensions. (E.g. SSLv3.) */
251 if (CBS_len(&client_hello) == 0) {
David Benjamin731058e2016-12-03 23:15:13 -0500252 out->extensions = NULL;
253 out->extensions_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 return 1;
255 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 /* Extract extensions and check it is valid. */
David Benjamine14ff062016-08-09 16:21:24 -0400258 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
260 !tls1_check_duplicate_extensions(&extensions) ||
261 CBS_len(&client_hello) != 0) {
262 return 0;
263 }
David Benjamine14ff062016-08-09 16:21:24 -0400264
David Benjamin731058e2016-12-03 23:15:13 -0500265 out->extensions = CBS_data(&extensions);
266 out->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700267
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 return 1;
269}
Adam Langleydc9b1412014-06-20 12:00:00 -0700270
David Benjamin731058e2016-12-03 23:15:13 -0500271int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
272 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 CBS extensions;
David Benjamin731058e2016-12-03 23:15:13 -0500274 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 while (CBS_len(&extensions) != 0) {
David Benjamincec73442016-08-02 17:41:33 -0400276 /* Decode the next extension. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 uint16_t type;
278 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 if (!CBS_get_u16(&extensions, &type) ||
280 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
281 return 0;
282 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400285 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800286 return 1;
287 }
288 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700289
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 return 0;
291}
Adam Langley95c29f32014-06-20 12:00:00 -0700292
David Benjamin731058e2016-12-03 23:15:13 -0500293int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
294 uint16_t extension_type,
295 const uint8_t **out_data,
296 size_t *out_len) {
David Benjamincec73442016-08-02 17:41:33 -0400297 CBS cbs;
David Benjamin731058e2016-12-03 23:15:13 -0500298 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
David Benjamincec73442016-08-02 17:41:33 -0400299 return 0;
300 }
301
302 *out_data = CBS_data(&cbs);
303 *out_len = CBS_len(&cbs);
304 return 1;
305}
306
Steven Valdezce902a92016-05-17 11:47:53 -0400307static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400308 SSL_CURVE_X25519,
309 SSL_CURVE_SECP256R1,
310 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700311#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin9e68f192016-06-30 14:55:33 -0400312 SSL_CURVE_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700313#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800314};
Adam Langley95c29f32014-06-20 12:00:00 -0700315
David Benjaminf04976b2016-10-07 00:37:55 -0400316void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
Steven Valdez5440fe02016-07-18 12:40:30 -0400317 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400318 *out_group_ids = ssl->supported_group_list;
319 *out_group_ids_len = ssl->supported_group_list_len;
320 if (!*out_group_ids) {
321 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400322 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 }
324}
David Benjamined439582014-07-14 19:13:02 -0400325
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900326int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
327 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400328 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400329
David Benjaminf04976b2016-10-07 00:37:55 -0400330 const uint16_t *groups, *pref, *supp;
331 size_t groups_len, pref_len, supp_len;
332 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800333
David Benjaminf04976b2016-10-07 00:37:55 -0400334 /* Clients are not required to send a supported_groups extension. In this
335 * case, the server is free to pick any group it likes. See RFC 4492,
336 * section 4, paragraph 3.
337 *
338 * However, in the interests of compatibility, we will skip ECDH if the
339 * client didn't send an extension because we can't be sure that they'll
340 * support our favoured group. Thus we do not special-case an emtpy
341 * |peer_supported_group_list|. */
David Benjamin55a43642015-04-20 14:45:55 -0400342
David Benjamin4298d772015-12-19 00:18:25 -0500343 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400344 pref = groups;
345 pref_len = groups_len;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900346 supp = hs->peer_supported_group_list;
347 supp_len = hs->peer_supported_group_list_len;
David Benjamin55a43642015-04-20 14:45:55 -0400348 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900349 pref = hs->peer_supported_group_list;
350 pref_len = hs->peer_supported_group_list_len;
Steven Valdezce902a92016-05-17 11:47:53 -0400351 supp = groups;
352 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400353 }
354
David Benjaminf04976b2016-10-07 00:37:55 -0400355 for (size_t i = 0; i < pref_len; i++) {
356 for (size_t j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400358 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500359 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800360 }
361 }
362 }
363
David Benjamin4298d772015-12-19 00:18:25 -0500364 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800365}
Adam Langley95c29f32014-06-20 12:00:00 -0700366
Steven Valdezce902a92016-05-17 11:47:53 -0400367int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400369 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800370
Steven Valdezce902a92016-05-17 11:47:53 -0400371 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
372 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 return 0;
374 }
375
David Benjamin54091232016-09-05 12:47:25 -0400376 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400377 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
378 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 return 0;
380 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 }
382
Steven Valdezce902a92016-05-17 11:47:53 -0400383 OPENSSL_free(*out_group_ids);
384 *out_group_ids = group_ids;
385 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800386
387 return 1;
388}
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100390int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
391 const char *curves) {
392 uint16_t *group_ids = NULL;
393 size_t ncurves = 0;
394
395 const char *col;
396 const char *ptr = curves;
397
398 do {
399 col = strchr(ptr, ':');
400
401 uint16_t group_id;
402 if (!ssl_name_to_group_id(&group_id, ptr,
403 col ? (size_t)(col - ptr) : strlen(ptr))) {
404 goto err;
405 }
406
407 uint16_t *new_group_ids = OPENSSL_realloc(group_ids,
408 (ncurves + 1) * sizeof(uint16_t));
409 if (new_group_ids == NULL) {
410 goto err;
411 }
412 group_ids = new_group_ids;
413
414 group_ids[ncurves] = group_id;
415 ncurves++;
416
417 if (col) {
418 ptr = col + 1;
419 }
420 } while (col);
421
422 OPENSSL_free(*out_group_ids);
423 *out_group_ids = group_ids;
424 *out_group_ids_len = ncurves;
425
426 return 1;
427
428err:
429 OPENSSL_free(group_ids);
430 return 0;
431}
432
Steven Valdezce902a92016-05-17 11:47:53 -0400433int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
434 const uint16_t *groups;
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400435 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -0400436 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400437 for (size_t i = 0; i < groups_len; i++) {
438 if (groups[i] == group_id) {
439 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800440 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 }
David Benjamin033e5f42014-11-13 18:47:41 -0500442
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400443 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800444}
David Benjamin033e5f42014-11-13 18:47:41 -0500445
David Benjamin3ef76972016-10-17 17:59:54 -0400446/* kVerifySignatureAlgorithms is the default list of accepted signature
David Benjamin3a322f52016-10-26 12:45:35 -0400447 * algorithms for verifying.
448 *
449 * For now, RSA-PSS signature algorithms are not enabled on Android's system
450 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
451 * restore them. */
David Benjamin3ef76972016-10-17 17:59:54 -0400452static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjamin3a322f52016-10-26 12:45:35 -0400453 /* Prefer SHA-256 algorithms. */
454 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400455#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400456 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400457#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400458 SSL_SIGN_RSA_PKCS1_SHA256,
459
460 /* Larger hashes are acceptable. */
461 SSL_SIGN_ECDSA_SECP384R1_SHA384,
462#if !defined(BORINGSSL_ANDROID_SYSTEM)
463 SSL_SIGN_RSA_PSS_SHA384,
464#endif
465 SSL_SIGN_RSA_PKCS1_SHA384,
466
David Benjamin3ef76972016-10-17 17:59:54 -0400467 /* TODO(davidben): Remove this entry and SSL_CURVE_SECP521R1 from
468 * kDefaultGroups. */
469#if defined(BORINGSSL_ANDROID_SYSTEM)
470 SSL_SIGN_ECDSA_SECP521R1_SHA512,
471#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400472#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400473 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400474#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400475 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400476
David Benjamin3a322f52016-10-26 12:45:35 -0400477 /* For now, SHA-1 is still accepted but least preferable. */
478 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400479
David Benjamin3a322f52016-10-26 12:45:35 -0400480};
481
482/* kSignSignatureAlgorithms is the default list of supported signature
483 * algorithms for signing.
484 *
485 * For now, RSA-PSS signature algorithms are not enabled on Android's system
486 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
487 * restore them. */
488static const uint16_t kSignSignatureAlgorithms[] = {
489 /* Prefer SHA-256 algorithms. */
490 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400491#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400492 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400493#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400494 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400495
David Benjamin3a322f52016-10-26 12:45:35 -0400496 /* If needed, sign larger hashes.
497 *
498 * TODO(davidben): Determine which of these may be pruned. */
499 SSL_SIGN_ECDSA_SECP384R1_SHA384,
500#if !defined(BORINGSSL_ANDROID_SYSTEM)
501 SSL_SIGN_RSA_PSS_SHA384,
502#endif
503 SSL_SIGN_RSA_PKCS1_SHA384,
504
505 SSL_SIGN_ECDSA_SECP521R1_SHA512,
506#if !defined(BORINGSSL_ANDROID_SYSTEM)
507 SSL_SIGN_RSA_PSS_SHA512,
508#endif
509 SSL_SIGN_RSA_PKCS1_SHA512,
510
511 /* If the peer supports nothing else, sign with SHA-1. */
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400512 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400513 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400514};
515
David Benjamin3ef76972016-10-17 17:59:54 -0400516size_t tls12_get_verify_sigalgs(const SSL *ssl, const uint16_t **out) {
517 *out = kVerifySignatureAlgorithms;
518 return OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
Adam Langleyfcf25832014-12-18 17:42:32 -0800519}
Adam Langley95c29f32014-06-20 12:00:00 -0700520
David Benjamin887c3002016-07-08 16:15:32 -0700521int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
David Benjamin3ef76972016-10-17 17:59:54 -0400522 const uint16_t *verify_sigalgs;
523 size_t num_verify_sigalgs = tls12_get_verify_sigalgs(ssl, &verify_sigalgs);
524 for (size_t i = 0; i < num_verify_sigalgs; i++) {
525 if (sigalg == verify_sigalgs[i]) {
526 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800527 }
528 }
529
David Benjamin3ef76972016-10-17 17:59:54 -0400530 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
531 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
532 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800533}
534
535/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
536 * supported or doesn't appear in supported signature algorithms. Unlike
537 * ssl_cipher_get_disabled this applies to a specific session and not global
538 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500539void ssl_set_client_disabled(SSL *ssl) {
540 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800541 int have_rsa = 0, have_ecdsa = 0;
542 c->mask_a = 0;
543 c->mask_k = 0;
544
David Benjamin3ef76972016-10-17 17:59:54 -0400545 /* Now go through all signature algorithms seeing if we support any for RSA or
546 * ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin0fc37ef2016-08-17 15:29:46 -0400547 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -0400548 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
David Benjamin0fc37ef2016-08-17 15:29:46 -0400549 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -0400550 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400551 case SSL_SIGN_RSA_PSS_SHA512:
552 case SSL_SIGN_RSA_PSS_SHA384:
553 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400554 case SSL_SIGN_RSA_PKCS1_SHA512:
555 case SSL_SIGN_RSA_PKCS1_SHA384:
556 case SSL_SIGN_RSA_PKCS1_SHA256:
557 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800558 have_rsa = 1;
559 break;
560
Steven Valdez02563852016-06-23 13:33:05 -0400561 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
562 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
563 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
564 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800565 have_ecdsa = 1;
566 break;
567 }
568 }
569
570 /* Disable auth if we don't include any appropriate signature algorithms. */
571 if (!have_rsa) {
572 c->mask_a |= SSL_aRSA;
573 }
574 if (!have_ecdsa) {
575 c->mask_a |= SSL_aECDSA;
576 }
577
578 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500579 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800580 c->mask_a |= SSL_aPSK;
581 c->mask_k |= SSL_kPSK;
582 }
583}
Adam Langley95c29f32014-06-20 12:00:00 -0700584
Adam Langley614c66a2015-06-12 15:26:58 -0700585/* tls_extension represents a TLS extension that is handled internally. The
586 * |init| function is called for each handshake, before any other functions of
587 * the extension. Then the add and parse callbacks are called as needed.
588 *
589 * The parse callbacks receive a |CBS| that contains the contents of the
590 * extension (i.e. not including the type and length bytes). If an extension is
591 * not received then the parse callbacks will be called with a NULL CBS so that
592 * they can do any processing needed to handle the absence of an extension.
593 *
594 * The add callbacks receive a |CBB| to which the extension can be appended but
595 * the function is responsible for appending the type and length bytes too.
596 *
597 * All callbacks return one for success and zero for error. If a parse function
598 * returns zero then a fatal alert with value |*out_alert| will be sent. If
599 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
600struct tls_extension {
601 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500602 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700603
David Benjamin8c880a22016-12-03 02:20:34 -0500604 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
605 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
606 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700607
David Benjamin8c880a22016-12-03 02:20:34 -0500608 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
609 CBS *contents);
610 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700611};
612
David Benjamin8c880a22016-12-03 02:20:34 -0500613static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
614 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400615 if (contents != NULL) {
616 /* Servers MUST NOT send this extension. */
617 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
618 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
619 return 0;
620 }
621
622 return 1;
623}
624
David Benjamin8c880a22016-12-03 02:20:34 -0500625static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
626 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400627 /* This extension from the client is handled elsewhere. */
628 return 1;
629}
630
David Benjamin8c880a22016-12-03 02:20:34 -0500631static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400632 return 1;
633}
Adam Langley614c66a2015-06-12 15:26:58 -0700634
635/* Server name indication (SNI).
636 *
637 * https://tools.ietf.org/html/rfc6066#section-3. */
638
David Benjamin8c880a22016-12-03 02:20:34 -0500639static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
640 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700641 if (ssl->tlsext_hostname == NULL) {
642 return 1;
643 }
644
645 CBB contents, server_name_list, name;
646 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
647 !CBB_add_u16_length_prefixed(out, &contents) ||
648 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
649 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
650 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
651 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
652 strlen(ssl->tlsext_hostname)) ||
653 !CBB_flush(out)) {
654 return 0;
655 }
656
657 return 1;
658}
659
David Benjamin8c880a22016-12-03 02:20:34 -0500660static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500661 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500662 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700663 if (contents == NULL) {
664 return 1;
665 }
666
667 if (CBS_len(contents) != 0) {
668 return 0;
669 }
670
671 assert(ssl->tlsext_hostname != NULL);
672
Steven Valdez87eab492016-06-27 16:34:59 -0400673 if (ssl->session == NULL) {
674 assert(ssl->s3->new_session->tlsext_hostname == NULL);
675 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
676 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700677 *out_alert = SSL_AD_INTERNAL_ERROR;
678 return 0;
679 }
680 }
681
682 return 1;
683}
684
David Benjamin8c880a22016-12-03 02:20:34 -0500685static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500686 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700687 if (contents == NULL) {
688 return 1;
689 }
690
David Benjamin9b611e22016-03-03 08:48:30 -0500691 CBS server_name_list, host_name;
692 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700693 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500694 !CBS_get_u8(&server_name_list, &name_type) ||
695 /* Although the server_name extension was intended to be extensible to
696 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
697 * different name types will cause an error. Further, RFC 4366 originally
698 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
699 * adding new name types is no longer feasible.
700 *
701 * Act as if the extensibility does not exist to simplify parsing. */
702 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
703 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700704 CBS_len(contents) != 0) {
705 return 0;
706 }
707
David Benjamin9b611e22016-03-03 08:48:30 -0500708 if (name_type != TLSEXT_NAMETYPE_host_name ||
709 CBS_len(&host_name) == 0 ||
710 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
711 CBS_contains_zero_byte(&host_name)) {
712 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
713 return 0;
714 }
Adam Langley614c66a2015-06-12 15:26:58 -0700715
David Benjamin4eb95cc2016-11-16 17:08:23 +0900716 /* Copy the hostname as a string. */
David Benjamin8c880a22016-12-03 02:20:34 -0500717 if (!CBS_strdup(&host_name, &hs->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900718 *out_alert = SSL_AD_INTERNAL_ERROR;
719 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700720 }
721
David Benjamin8c880a22016-12-03 02:20:34 -0500722 hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700723 return 1;
724}
725
David Benjamin8c880a22016-12-03 02:20:34 -0500726static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
727 if (hs->ssl->s3->session_reused ||
728 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700729 return 1;
730 }
731
732 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
733 !CBB_add_u16(out, 0 /* length */)) {
734 return 0;
735 }
736
737 return 1;
738}
739
740
Adam Langley5021b222015-06-12 18:27:58 -0700741/* Renegotiation indication.
742 *
743 * https://tools.ietf.org/html/rfc5746 */
744
David Benjamin8c880a22016-12-03 02:20:34 -0500745static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
746 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400747 uint16_t min_version, max_version;
748 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
749 return 0;
750 }
751
752 /* Renegotiation indication is not necessary in TLS 1.3. */
753 if (min_version >= TLS1_3_VERSION) {
754 return 1;
755 }
756
David Benjamin52bf6902016-10-08 12:05:03 -0400757 assert(ssl->s3->initial_handshake_complete ==
758 (ssl->s3->previous_client_finished_len != 0));
759
Adam Langley5021b222015-06-12 18:27:58 -0700760 CBB contents, prev_finished;
761 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
762 !CBB_add_u16_length_prefixed(out, &contents) ||
763 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
764 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
765 ssl->s3->previous_client_finished_len) ||
766 !CBB_flush(out)) {
767 return 0;
768 }
769
770 return 1;
771}
772
David Benjamin8c880a22016-12-03 02:20:34 -0500773static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700774 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500775 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400776 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
777 return 0;
778 }
779
David Benjamin3e052de2015-11-25 20:10:31 -0500780 /* Servers may not switch between omitting the extension and supporting it.
781 * See RFC 5746, sections 3.5 and 4.2. */
782 if (ssl->s3->initial_handshake_complete &&
783 (contents != NULL) != ssl->s3->send_connection_binding) {
784 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
785 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
786 return 0;
787 }
788
Adam Langley5021b222015-06-12 18:27:58 -0700789 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500790 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700791 * RI even on initial ServerHello because the client doesn't see any
792 * renegotiation during an attack. However this would mean we could not
793 * connect to any server which doesn't support RI.
794 *
David Benjamine9cddb82015-11-23 14:36:40 -0500795 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
796 * practical terms every client sets it so it's just assumed here. */
797 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700798 }
799
800 const size_t expected_len = ssl->s3->previous_client_finished_len +
801 ssl->s3->previous_server_finished_len;
802
803 /* Check for logic errors */
804 assert(!expected_len || ssl->s3->previous_client_finished_len);
805 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400806 assert(ssl->s3->initial_handshake_complete ==
807 (ssl->s3->previous_client_finished_len != 0));
808 assert(ssl->s3->initial_handshake_complete ==
809 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700810
811 /* Parse out the extension contents. */
812 CBS renegotiated_connection;
813 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
814 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400815 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700816 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
817 return 0;
818 }
819
820 /* Check that the extension matches. */
821 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400822 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700823 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
824 return 0;
825 }
826
827 const uint8_t *d = CBS_data(&renegotiated_connection);
828 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
829 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400830 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700831 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
832 return 0;
833 }
834 d += ssl->s3->previous_client_finished_len;
835
836 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
837 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400838 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700839 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
840 return 0;
841 }
842 ssl->s3->send_connection_binding = 1;
843
844 return 1;
845}
846
David Benjamin8c880a22016-12-03 02:20:34 -0500847static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700848 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500849 SSL *const ssl = hs->ssl;
Adam Langley5021b222015-06-12 18:27:58 -0700850 /* Renegotiation isn't supported as a server so this function should never be
851 * called after the initial handshake. */
852 assert(!ssl->s3->initial_handshake_complete);
853
Steven Valdez143e8b32016-07-11 13:19:03 -0400854 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
855 return 1;
856 }
857
Adam Langley5021b222015-06-12 18:27:58 -0700858 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400859 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700860 }
861
862 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700863 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
864 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400865 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700866 return 0;
867 }
868
David Benjamin52bf6902016-10-08 12:05:03 -0400869 /* Check that the extension matches. We do not support renegotiation as a
870 * server, so this must be empty. */
871 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400872 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700873 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
874 return 0;
875 }
876
877 ssl->s3->send_connection_binding = 1;
878
879 return 1;
880}
881
David Benjamin8c880a22016-12-03 02:20:34 -0500882static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
883 SSL *const ssl = hs->ssl;
David Benjamin52bf6902016-10-08 12:05:03 -0400884 /* Renegotiation isn't supported as a server so this function should never be
885 * called after the initial handshake. */
886 assert(!ssl->s3->initial_handshake_complete);
887
Steven Valdez143e8b32016-07-11 13:19:03 -0400888 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
889 return 1;
890 }
891
Adam Langley5021b222015-06-12 18:27:58 -0700892 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400893 !CBB_add_u16(out, 1 /* length */) ||
894 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700895 return 0;
896 }
897
898 return 1;
899}
900
Adam Langley0a056712015-07-01 15:03:33 -0700901
902/* Extended Master Secret.
903 *
David Benjamin43946d42016-02-01 08:42:19 -0500904 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700905
David Benjamin8c880a22016-12-03 02:20:34 -0500906static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400907 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -0500908 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400909 return 0;
910 }
911
912 /* Extended master secret is not necessary in TLS 1.3. */
913 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700914 return 1;
915 }
916
917 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
918 !CBB_add_u16(out, 0 /* length */)) {
919 return 0;
920 }
921
922 return 1;
923}
924
David Benjamin8c880a22016-12-03 02:20:34 -0500925static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700926 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500927 SSL *const ssl = hs->ssl;
David Benjamin163c9562016-08-29 23:14:17 -0400928 /* Whether EMS is negotiated may not change on renegotation. */
929 if (ssl->s3->initial_handshake_complete) {
930 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
931 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
932 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
933 return 0;
934 }
935
936 return 1;
937 }
938
Adam Langley0a056712015-07-01 15:03:33 -0700939 if (contents == NULL) {
940 return 1;
941 }
942
Steven Valdez143e8b32016-07-11 13:19:03 -0400943 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
944 ssl->version == SSL3_VERSION) {
945 return 0;
946 }
947
948 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700949 return 0;
950 }
951
952 ssl->s3->tmp.extended_master_secret = 1;
953 return 1;
954}
955
David Benjamin8c880a22016-12-03 02:20:34 -0500956static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500957 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500958 SSL *const ssl = hs->ssl;
959 uint16_t version = ssl3_protocol_version(ssl);
960 if (version >= TLS1_3_VERSION ||
961 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400962 return 1;
963 }
964
965 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700966 return 1;
967 }
968
969 if (CBS_len(contents) != 0) {
970 return 0;
971 }
972
973 ssl->s3->tmp.extended_master_secret = 1;
974 return 1;
975}
976
David Benjamin8c880a22016-12-03 02:20:34 -0500977static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
978 if (!hs->ssl->s3->tmp.extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700979 return 1;
980 }
981
982 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
983 !CBB_add_u16(out, 0 /* length */)) {
984 return 0;
985 }
986
987 return 1;
988}
989
Adam Langley9b05bc52015-07-01 15:25:33 -0700990
991/* Session tickets.
992 *
993 * https://tools.ietf.org/html/rfc5077 */
994
David Benjamin8c880a22016-12-03 02:20:34 -0500995static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
996 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400997 uint16_t min_version, max_version;
998 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
999 return 0;
1000 }
1001
1002 /* TLS 1.3 uses a different ticket extension. */
1003 if (min_version >= TLS1_3_VERSION ||
1004 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001005 return 1;
1006 }
1007
1008 const uint8_t *ticket_data = NULL;
1009 int ticket_len = 0;
1010
1011 /* Renegotiation does not participate in session resumption. However, still
1012 * advertise the extension to avoid potentially breaking servers which carry
1013 * over the state from the previous handshake, such as OpenSSL servers
1014 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -04001015 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -07001016 if (!ssl->s3->initial_handshake_complete &&
1017 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001018 ssl->session->tlsext_tick != NULL &&
1019 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -04001020 ssl->method->version_from_wire(&session_version,
1021 ssl->session->ssl_version) &&
1022 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001023 ticket_data = ssl->session->tlsext_tick;
1024 ticket_len = ssl->session->tlsext_ticklen;
1025 }
1026
1027 CBB ticket;
1028 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1029 !CBB_add_u16_length_prefixed(out, &ticket) ||
1030 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1031 !CBB_flush(out)) {
1032 return 0;
1033 }
1034
1035 return 1;
1036}
1037
David Benjamin8c880a22016-12-03 02:20:34 -05001038static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -07001039 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001040 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -07001041 if (contents == NULL) {
1042 return 1;
1043 }
1044
Steven Valdez143e8b32016-07-11 13:19:03 -04001045 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1046 return 0;
1047 }
1048
Adam Langley9b05bc52015-07-01 15:25:33 -07001049 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1050 * this function should never be called, even if the server tries to send the
1051 * extension. */
1052 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1053
1054 if (CBS_len(contents) != 0) {
1055 return 0;
1056 }
1057
David Benjamin8c880a22016-12-03 02:20:34 -05001058 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001059 return 1;
1060}
1061
David Benjamin8c880a22016-12-03 02:20:34 -05001062static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1063 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001064 return 1;
1065 }
1066
David Benjamin78476f62016-11-12 11:20:55 +09001067 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin8c880a22016-12-03 02:20:34 -05001068 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001069
1070 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1071 !CBB_add_u16(out, 0 /* length */)) {
1072 return 0;
1073 }
1074
1075 return 1;
1076}
1077
1078
Adam Langley2e857bd2015-07-01 16:09:19 -07001079/* Signature Algorithms.
1080 *
1081 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1082
David Benjamin8c880a22016-12-03 02:20:34 -05001083static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1084 SSL *const ssl = hs->ssl;
David Benjamin2dc02042016-09-19 19:57:37 -04001085 uint16_t min_version, max_version;
1086 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1087 return 0;
1088 }
1089
1090 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001091 return 1;
1092 }
1093
David Benjamin0fc37ef2016-08-17 15:29:46 -04001094 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001095 const size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001096
David Benjamin0fc37ef2016-08-17 15:29:46 -04001097 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001098 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1099 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001100 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001101 return 0;
1102 }
1103
David Benjamin0fc37ef2016-08-17 15:29:46 -04001104 for (size_t i = 0; i < num_sigalgs; i++) {
1105 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001106 return 0;
1107 }
1108 }
1109
1110 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001111 return 0;
1112 }
1113
1114 return 1;
1115}
1116
David Benjamin8c880a22016-12-03 02:20:34 -05001117static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001118 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001119 OPENSSL_free(hs->peer_sigalgs);
1120 hs->peer_sigalgs = NULL;
1121 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001122
Adam Langley2e857bd2015-07-01 16:09:19 -07001123 if (contents == NULL) {
1124 return 1;
1125 }
1126
1127 CBS supported_signature_algorithms;
1128 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001129 CBS_len(contents) != 0 ||
1130 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001131 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001132 return 0;
1133 }
1134
1135 return 1;
1136}
1137
Adam Langley2e857bd2015-07-01 16:09:19 -07001138
Adam Langleybb0bd042015-07-01 16:21:03 -07001139/* OCSP Stapling.
1140 *
1141 * https://tools.ietf.org/html/rfc6066#section-8 */
1142
David Benjamin8c880a22016-12-03 02:20:34 -05001143static void ext_ocsp_init(SSL_HANDSHAKE *hs) {
1144 hs->ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001145}
1146
David Benjamin8c880a22016-12-03 02:20:34 -05001147static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1148 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001149 if (!ssl->ocsp_stapling_enabled) {
1150 return 1;
1151 }
1152
1153 CBB contents;
1154 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1155 !CBB_add_u16_length_prefixed(out, &contents) ||
1156 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1157 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1158 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1159 !CBB_flush(out)) {
1160 return 0;
1161 }
1162
Adam Langleyce9d85e2016-01-24 15:58:39 -08001163 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001164 return 1;
1165}
1166
David Benjamin8c880a22016-12-03 02:20:34 -05001167static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001168 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001169 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001170 if (contents == NULL) {
1171 return 1;
1172 }
1173
Steven Valdeza833c352016-11-01 13:39:36 -04001174 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1175 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001176 return 0;
1177 }
1178
Steven Valdeza833c352016-11-01 13:39:36 -04001179 /* OCSP stapling is forbidden on non-certificate ciphers. */
1180 if (CBS_len(contents) != 0 ||
1181 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001182 return 0;
1183 }
1184
Steven Valdeza833c352016-11-01 13:39:36 -04001185 /* Note this does not check for resumption in TLS 1.2. Sending
1186 * status_request here does not make sense, but OpenSSL does so and the
1187 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001188
David Benjamin8c880a22016-12-03 02:20:34 -05001189 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001190 return 1;
1191}
1192
David Benjamin8c880a22016-12-03 02:20:34 -05001193static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001194 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001195 if (contents == NULL) {
1196 return 1;
1197 }
1198
1199 uint8_t status_type;
1200 if (!CBS_get_u8(contents, &status_type)) {
1201 return 0;
1202 }
1203
1204 /* We cannot decide whether OCSP stapling will occur yet because the correct
1205 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001206 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001207
Adam Langleybb0bd042015-07-01 16:21:03 -07001208 return 1;
1209}
1210
David Benjamin8c880a22016-12-03 02:20:34 -05001211static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1212 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001213 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001214 !hs->ocsp_stapling_requested ||
David Benjamin942f4ed2016-07-16 19:03:49 +03001215 ssl->ctx->ocsp_response_length == 0 ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001216 ssl->s3->session_reused ||
Steven Valdeza833c352016-11-01 13:39:36 -04001217 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001218 return 1;
1219 }
1220
David Benjamin8c880a22016-12-03 02:20:34 -05001221 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001222
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001223 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001224 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001225}
1226
1227
Adam Langley97dfcbf2015-07-01 18:35:20 -07001228/* Next protocol negotiation.
1229 *
1230 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1231
David Benjamin8c880a22016-12-03 02:20:34 -05001232static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1233 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001234 if (ssl->s3->initial_handshake_complete ||
1235 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin8c880a22016-12-03 02:20:34 -05001236 (ssl->options & SSL_OP_DISABLE_NPN) || SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001237 return 1;
1238 }
1239
1240 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1241 !CBB_add_u16(out, 0 /* length */)) {
1242 return 0;
1243 }
1244
1245 return 1;
1246}
1247
David Benjamin8c880a22016-12-03 02:20:34 -05001248static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001249 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001250 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001251 if (contents == NULL) {
1252 return 1;
1253 }
1254
Steven Valdez143e8b32016-07-11 13:19:03 -04001255 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1256 return 0;
1257 }
1258
Adam Langley97dfcbf2015-07-01 18:35:20 -07001259 /* If any of these are false then we should never have sent the NPN
1260 * extension in the ClientHello and thus this function should never have been
1261 * called. */
1262 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001263 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001264 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001265 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001266
David Benjamin76c2efc2015-08-31 14:24:29 -04001267 if (ssl->s3->alpn_selected != NULL) {
1268 /* NPN and ALPN may not be negotiated in the same connection. */
1269 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1270 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1271 return 0;
1272 }
1273
Adam Langley97dfcbf2015-07-01 18:35:20 -07001274 const uint8_t *const orig_contents = CBS_data(contents);
1275 const size_t orig_len = CBS_len(contents);
1276
1277 while (CBS_len(contents) != 0) {
1278 CBS proto;
1279 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1280 CBS_len(&proto) == 0) {
1281 return 0;
1282 }
1283 }
1284
1285 uint8_t *selected;
1286 uint8_t selected_len;
1287 if (ssl->ctx->next_proto_select_cb(
1288 ssl, &selected, &selected_len, orig_contents, orig_len,
1289 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1290 *out_alert = SSL_AD_INTERNAL_ERROR;
1291 return 0;
1292 }
1293
David Benjamin79978df2015-12-25 15:56:49 -05001294 OPENSSL_free(ssl->s3->next_proto_negotiated);
1295 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1296 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001297 *out_alert = SSL_AD_INTERNAL_ERROR;
1298 return 0;
1299 }
1300
David Benjamin79978df2015-12-25 15:56:49 -05001301 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001302 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001303
1304 return 1;
1305}
1306
David Benjamin8c880a22016-12-03 02:20:34 -05001307static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001308 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001309 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001310 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1311 return 1;
1312 }
1313
Adam Langley97dfcbf2015-07-01 18:35:20 -07001314 if (contents != NULL && CBS_len(contents) != 0) {
1315 return 0;
1316 }
1317
1318 if (contents == NULL ||
1319 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001320 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001321 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001322 return 1;
1323 }
1324
David Benjamin8c880a22016-12-03 02:20:34 -05001325 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001326 return 1;
1327}
1328
David Benjamin8c880a22016-12-03 02:20:34 -05001329static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1330 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001331 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1332 * parsed. */
David Benjamin8c880a22016-12-03 02:20:34 -05001333 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001334 return 1;
1335 }
1336
1337 const uint8_t *npa;
1338 unsigned npa_len;
1339
1340 if (ssl->ctx->next_protos_advertised_cb(
1341 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1342 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001343 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001344 return 1;
1345 }
1346
1347 CBB contents;
1348 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1349 !CBB_add_u16_length_prefixed(out, &contents) ||
1350 !CBB_add_bytes(&contents, npa, npa_len) ||
1351 !CBB_flush(out)) {
1352 return 0;
1353 }
1354
1355 return 1;
1356}
1357
1358
Adam Langleyab8d87d2015-07-10 12:21:39 -07001359/* Signed certificate timestamps.
1360 *
1361 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1362
David Benjamin8c880a22016-12-03 02:20:34 -05001363static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1364 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001365 if (!ssl->signed_cert_timestamps_enabled) {
1366 return 1;
1367 }
1368
1369 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1370 !CBB_add_u16(out, 0 /* length */)) {
1371 return 0;
1372 }
1373
1374 return 1;
1375}
1376
David Benjamin8c880a22016-12-03 02:20:34 -05001377static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001378 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001379 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001380 if (contents == NULL) {
1381 return 1;
1382 }
1383
Steven Valdeza833c352016-11-01 13:39:36 -04001384 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1385 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001386 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001387 return 0;
1388 }
1389
Adam Langleyab8d87d2015-07-10 12:21:39 -07001390 /* If this is false then we should never have sent the SCT extension in the
1391 * ClientHello and thus this function should never have been called. */
1392 assert(ssl->signed_cert_timestamps_enabled);
1393
Adam Langleycfa08c32016-11-17 13:21:27 -08001394 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001395 *out_alert = SSL_AD_DECODE_ERROR;
1396 return 0;
1397 }
1398
David Benjamindaa88502016-10-04 16:32:16 -04001399 /* Session resumption uses the original session information. The extension
1400 * should not be sent on resumption, but RFC 6962 did not make it a
1401 * requirement, so tolerate this.
1402 *
1403 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001404 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001405 !CBS_stow(
1406 contents,
1407 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1408 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001409 *out_alert = SSL_AD_INTERNAL_ERROR;
1410 return 0;
1411 }
1412
1413 return 1;
1414}
1415
David Benjamin8c880a22016-12-03 02:20:34 -05001416static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001417 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001418 if (contents == NULL) {
1419 return 1;
1420 }
1421
1422 if (CBS_len(contents) != 0) {
1423 return 0;
1424 }
1425
David Benjamin8c880a22016-12-03 02:20:34 -05001426 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001427 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001428}
1429
David Benjamin8c880a22016-12-03 02:20:34 -05001430static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1431 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001432 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001433 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1434 ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001435 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001436 return 1;
1437 }
1438
1439 CBB contents;
1440 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1441 CBB_add_u16_length_prefixed(out, &contents) &&
1442 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1443 ssl->ctx->signed_cert_timestamp_list_length) &&
1444 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001445}
1446
1447
Adam Langleyf18e4532015-07-10 13:39:53 -07001448/* Application-level Protocol Negotiation.
1449 *
1450 * https://tools.ietf.org/html/rfc7301 */
1451
David Benjamin8c880a22016-12-03 02:20:34 -05001452static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1453 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001454 if (ssl->alpn_client_proto_list == NULL ||
1455 ssl->s3->initial_handshake_complete) {
1456 return 1;
1457 }
1458
1459 CBB contents, proto_list;
1460 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1461 !CBB_add_u16_length_prefixed(out, &contents) ||
1462 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1463 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1464 ssl->alpn_client_proto_list_len) ||
1465 !CBB_flush(out)) {
1466 return 0;
1467 }
1468
1469 return 1;
1470}
1471
David Benjamin8c880a22016-12-03 02:20:34 -05001472static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001473 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001474 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001475 if (contents == NULL) {
1476 return 1;
1477 }
1478
1479 assert(!ssl->s3->initial_handshake_complete);
1480 assert(ssl->alpn_client_proto_list != NULL);
1481
David Benjamin8c880a22016-12-03 02:20:34 -05001482 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001483 /* NPN and ALPN may not be negotiated in the same connection. */
1484 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1485 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1486 return 0;
1487 }
1488
Adam Langleyf18e4532015-07-10 13:39:53 -07001489 /* The extension data consists of a ProtocolNameList which must have
1490 * exactly one ProtocolName. Each of these is length-prefixed. */
1491 CBS protocol_name_list, protocol_name;
1492 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1493 CBS_len(contents) != 0 ||
1494 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1495 /* Empty protocol names are forbidden. */
1496 CBS_len(&protocol_name) == 0 ||
1497 CBS_len(&protocol_name_list) != 0) {
1498 return 0;
1499 }
1500
David Benjamin3e517572016-08-11 11:52:23 -04001501 /* Check that the protcol name is one of the ones we advertised. */
1502 int protocol_ok = 0;
1503 CBS client_protocol_name_list, client_protocol_name;
1504 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1505 ssl->alpn_client_proto_list_len);
1506 while (CBS_len(&client_protocol_name_list) > 0) {
1507 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1508 &client_protocol_name)) {
1509 *out_alert = SSL_AD_INTERNAL_ERROR;
1510 return 0;
1511 }
1512
1513 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1514 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1515 CBS_len(&protocol_name)) == 0) {
1516 protocol_ok = 1;
1517 break;
1518 }
1519 }
1520
1521 if (!protocol_ok) {
1522 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1523 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1524 return 0;
1525 }
1526
Adam Langleyf18e4532015-07-10 13:39:53 -07001527 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1528 &ssl->s3->alpn_selected_len)) {
1529 *out_alert = SSL_AD_INTERNAL_ERROR;
1530 return 0;
1531 }
1532
1533 return 1;
1534}
1535
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001536int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001537 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001538 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001539 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001540 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001541 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001542 client_hello, &contents,
1543 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1544 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001545 return 1;
1546 }
1547
1548 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001549 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001550
1551 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001552 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1553 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001554 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001555 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1556 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001557 return 0;
1558 }
1559
1560 /* Validate the protocol list. */
1561 CBS protocol_name_list_copy = protocol_name_list;
1562 while (CBS_len(&protocol_name_list_copy) > 0) {
1563 CBS protocol_name;
1564
1565 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1566 /* Empty protocol names are forbidden. */
1567 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001568 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1569 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001570 return 0;
1571 }
1572 }
1573
1574 const uint8_t *selected;
1575 uint8_t selected_len;
1576 if (ssl->ctx->alpn_select_cb(
1577 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1578 CBS_len(&protocol_name_list),
1579 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1580 OPENSSL_free(ssl->s3->alpn_selected);
1581 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1582 if (ssl->s3->alpn_selected == NULL) {
1583 *out_alert = SSL_AD_INTERNAL_ERROR;
1584 return 0;
1585 }
1586 ssl->s3->alpn_selected_len = selected_len;
1587 }
1588
1589 return 1;
1590}
1591
David Benjamin8c880a22016-12-03 02:20:34 -05001592static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1593 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001594 if (ssl->s3->alpn_selected == NULL) {
1595 return 1;
1596 }
1597
1598 CBB contents, proto_list, proto;
1599 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1600 !CBB_add_u16_length_prefixed(out, &contents) ||
1601 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1602 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001603 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1604 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001605 !CBB_flush(out)) {
1606 return 0;
1607 }
1608
1609 return 1;
1610}
1611
1612
Adam Langley49c7af12015-07-10 14:33:46 -07001613/* Channel ID.
1614 *
1615 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1616
David Benjamin8c880a22016-12-03 02:20:34 -05001617static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1618 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001619}
1620
David Benjamin8c880a22016-12-03 02:20:34 -05001621static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1622 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001623 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001624 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001625 return 1;
1626 }
1627
1628 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1629 !CBB_add_u16(out, 0 /* length */)) {
1630 return 0;
1631 }
1632
1633 return 1;
1634}
1635
David Benjamin8c880a22016-12-03 02:20:34 -05001636static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1637 uint8_t *out_alert, CBS *contents) {
1638 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001639 if (contents == NULL) {
1640 return 1;
1641 }
1642
David Benjamince079fd2016-08-02 16:22:34 -04001643 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001644 assert(ssl->tlsext_channel_id_enabled);
1645
1646 if (CBS_len(contents) != 0) {
1647 return 0;
1648 }
1649
1650 ssl->s3->tlsext_channel_id_valid = 1;
1651 return 1;
1652}
1653
David Benjamin8c880a22016-12-03 02:20:34 -05001654static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1655 uint8_t *out_alert, CBS *contents) {
1656 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001657 if (contents == NULL ||
1658 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001659 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001660 return 1;
1661 }
1662
1663 if (CBS_len(contents) != 0) {
1664 return 0;
1665 }
1666
1667 ssl->s3->tlsext_channel_id_valid = 1;
1668 return 1;
1669}
1670
David Benjamin8c880a22016-12-03 02:20:34 -05001671static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1672 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001673 if (!ssl->s3->tlsext_channel_id_valid) {
1674 return 1;
1675 }
1676
1677 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1678 !CBB_add_u16(out, 0 /* length */)) {
1679 return 0;
1680 }
1681
1682 return 1;
1683}
1684
Adam Langley391250d2015-07-15 19:06:07 -07001685
1686/* Secure Real-time Transport Protocol (SRTP) extension.
1687 *
1688 * https://tools.ietf.org/html/rfc5764 */
1689
Adam Langley391250d2015-07-15 19:06:07 -07001690
David Benjamin8c880a22016-12-03 02:20:34 -05001691static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1692 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001693}
1694
David Benjamin8c880a22016-12-03 02:20:34 -05001695static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1696 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001697 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1698 if (profiles == NULL) {
1699 return 1;
1700 }
1701 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1702 if (num_profiles == 0) {
1703 return 1;
1704 }
1705
1706 CBB contents, profile_ids;
1707 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1708 !CBB_add_u16_length_prefixed(out, &contents) ||
1709 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1710 return 0;
1711 }
1712
David Benjamin54091232016-09-05 12:47:25 -04001713 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001714 if (!CBB_add_u16(&profile_ids,
1715 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1716 return 0;
1717 }
1718 }
1719
1720 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1721 !CBB_flush(out)) {
1722 return 0;
1723 }
1724
1725 return 1;
1726}
1727
David Benjamin8c880a22016-12-03 02:20:34 -05001728static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001729 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001730 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001731 if (contents == NULL) {
1732 return 1;
1733 }
1734
1735 /* The extension consists of a u16-prefixed profile ID list containing a
1736 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1737 *
1738 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1739 CBS profile_ids, srtp_mki;
1740 uint16_t profile_id;
1741 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1742 !CBS_get_u16(&profile_ids, &profile_id) ||
1743 CBS_len(&profile_ids) != 0 ||
1744 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1745 CBS_len(contents) != 0) {
1746 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1747 return 0;
1748 }
1749
1750 if (CBS_len(&srtp_mki) != 0) {
1751 /* Must be no MKI, since we never offer one. */
1752 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1753 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1754 return 0;
1755 }
1756
1757 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1758
1759 /* Check to see if the server gave us something we support (and presumably
1760 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001761 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001762 const SRTP_PROTECTION_PROFILE *profile =
1763 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1764
1765 if (profile->id == profile_id) {
1766 ssl->srtp_profile = profile;
1767 return 1;
1768 }
1769 }
1770
1771 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1772 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1773 return 0;
1774}
1775
David Benjamin8c880a22016-12-03 02:20:34 -05001776static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001777 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001778 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001779 if (contents == NULL) {
1780 return 1;
1781 }
1782
1783 CBS profile_ids, srtp_mki;
1784 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1785 CBS_len(&profile_ids) < 2 ||
1786 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1787 CBS_len(contents) != 0) {
1788 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1789 return 0;
1790 }
1791 /* Discard the MKI value for now. */
1792
1793 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1794 SSL_get_srtp_profiles(ssl);
1795
1796 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001797 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001798 const SRTP_PROTECTION_PROFILE *server_profile =
1799 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1800
1801 CBS profile_ids_tmp;
1802 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1803
1804 while (CBS_len(&profile_ids_tmp) > 0) {
1805 uint16_t profile_id;
1806 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1807 return 0;
1808 }
1809
1810 if (server_profile->id == profile_id) {
1811 ssl->srtp_profile = server_profile;
1812 return 1;
1813 }
1814 }
1815 }
1816
1817 return 1;
1818}
1819
David Benjamin8c880a22016-12-03 02:20:34 -05001820static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1821 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001822 if (ssl->srtp_profile == NULL) {
1823 return 1;
1824 }
1825
1826 CBB contents, profile_ids;
1827 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1828 !CBB_add_u16_length_prefixed(out, &contents) ||
1829 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1830 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1831 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1832 !CBB_flush(out)) {
1833 return 0;
1834 }
1835
1836 return 1;
1837}
1838
Adam Langleybdd5d662015-07-20 16:19:08 -07001839
1840/* EC point formats.
1841 *
1842 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1843
David Benjamin8c880a22016-12-03 02:20:34 -05001844static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001845 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001846 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1847 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001848 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1849 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001850 !CBB_flush(out)) {
1851 return 0;
1852 }
1853
1854 return 1;
1855}
1856
David Benjamin8c880a22016-12-03 02:20:34 -05001857static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001858 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -05001859 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin70aba262016-11-01 12:08:15 -04001860 return 0;
1861 }
1862
1863 /* The point format extension is unneccessary in TLS 1.3. */
1864 if (min_version >= TLS1_3_VERSION) {
1865 return 1;
1866 }
1867
David Benjamin8c880a22016-12-03 02:20:34 -05001868 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001869}
1870
David Benjamin8c880a22016-12-03 02:20:34 -05001871static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001872 CBS *contents) {
1873 if (contents == NULL) {
1874 return 1;
1875 }
1876
David Benjamin8c880a22016-12-03 02:20:34 -05001877 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001878 return 0;
1879 }
1880
Adam Langleybdd5d662015-07-20 16:19:08 -07001881 CBS ec_point_format_list;
1882 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1883 CBS_len(contents) != 0) {
1884 return 0;
1885 }
1886
David Benjaminfc059942015-07-30 23:01:59 -04001887 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1888 * point format. */
1889 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1890 CBS_len(&ec_point_format_list)) == NULL) {
1891 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001892 return 0;
1893 }
1894
1895 return 1;
1896}
1897
David Benjamin8c880a22016-12-03 02:20:34 -05001898static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001899 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001900 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001901 return 1;
1902 }
1903
David Benjamin8c880a22016-12-03 02:20:34 -05001904 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001905}
1906
David Benjamin8c880a22016-12-03 02:20:34 -05001907static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1908 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001909 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1910 return 1;
1911 }
1912
Adam Langleybdd5d662015-07-20 16:19:08 -07001913 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1914 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001915 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001916
1917 if (!using_ecc) {
1918 return 1;
1919 }
1920
David Benjamin8c880a22016-12-03 02:20:34 -05001921 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001922}
1923
Steven Valdeza833c352016-11-01 13:39:36 -04001924
Steven Valdez4aa154e2016-07-29 14:32:55 -04001925/* Pre Shared Key
1926 *
Steven Valdeza833c352016-11-01 13:39:36 -04001927 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001928
David Benjamin8c880a22016-12-03 02:20:34 -05001929static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1930 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001931 uint16_t min_version, max_version;
1932 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1933 return 0;
1934 }
1935
1936 uint16_t session_version;
1937 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1938 !ssl->method->version_from_wire(&session_version,
1939 ssl->session->ssl_version) ||
1940 session_version < TLS1_3_VERSION) {
1941 return 0;
1942 }
1943
1944 const EVP_MD *digest =
1945 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1946 size_t binder_len = EVP_MD_size(digest);
1947 return 15 + ssl->session->tlsext_ticklen + binder_len;
1948}
1949
David Benjamin8c880a22016-12-03 02:20:34 -05001950static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1951 SSL *const ssl = hs->ssl;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001952 uint16_t min_version, max_version;
1953 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1954 return 0;
1955 }
1956
David Benjamin2dc02042016-09-19 19:57:37 -04001957 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001958 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001959 !ssl->method->version_from_wire(&session_version,
1960 ssl->session->ssl_version) ||
1961 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001962 return 1;
1963 }
1964
Steven Valdeza833c352016-11-01 13:39:36 -04001965 struct timeval now;
1966 ssl_get_current_time(ssl, &now);
1967 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1968 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1969
1970 /* Fill in a placeholder zero binder of the appropriate length. It will be
1971 * computed and filled in later after length prefixes are computed. */
1972 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
1973 const EVP_MD *digest =
1974 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1975 size_t binder_len = EVP_MD_size(digest);
1976
1977 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001978 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1979 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001980 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001981 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1982 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001983 ssl->session->tlsext_ticklen) ||
1984 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1985 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1986 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1987 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001988 return 0;
1989 }
1990
David Benjamin8c880a22016-12-03 02:20:34 -05001991 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001992 return CBB_flush(out);
1993}
1994
David Benjamin8baf9632016-11-17 17:11:16 +09001995int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1996 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001997 CBS *contents) {
1998 uint16_t psk_id;
1999 if (!CBS_get_u16(contents, &psk_id) ||
2000 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04002001 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002002 *out_alert = SSL_AD_DECODE_ERROR;
2003 return 0;
2004 }
2005
David Benjamineab773a2016-11-09 18:32:35 -05002006 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04002007 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04002008 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002009 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2010 return 0;
2011 }
2012
2013 return 1;
2014}
2015
David Benjamin8baf9632016-11-17 17:11:16 +09002016int ssl_ext_pre_shared_key_parse_clienthello(SSL_HANDSHAKE *hs,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002017 SSL_SESSION **out_session,
Steven Valdeza833c352016-11-01 13:39:36 -04002018 CBS *out_binders,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002019 uint8_t *out_alert,
2020 CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09002021 SSL *const ssl = hs->ssl;
Steven Valdez5b986082016-09-01 12:29:49 -04002022 /* We only process the first PSK identity since we don't support pure PSK. */
Steven Valdeza833c352016-11-01 13:39:36 -04002023 uint32_t obfuscated_ticket_age;
David Benjaminaedf3032016-12-01 16:47:56 -05002024 CBS identities, ticket, binders;
2025 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2026 !CBS_get_u16_length_prefixed(&identities, &ticket) ||
2027 !CBS_get_u32(&identities, &obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04002028 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05002029 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04002030 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04002031 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002032 *out_alert = SSL_AD_DECODE_ERROR;
2033 return 0;
2034 }
2035
Steven Valdeza833c352016-11-01 13:39:36 -04002036 *out_binders = binders;
2037
David Benjaminaedf3032016-12-01 16:47:56 -05002038 /* Check the syntax of the remaining identities, but do not process them. */
2039 size_t num_identities = 1;
2040 while (CBS_len(&identities) != 0) {
2041 CBS unused_ticket;
2042 uint32_t unused_obfuscated_ticket_age;
2043 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
2044 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
2045 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2046 *out_alert = SSL_AD_DECODE_ERROR;
2047 return 0;
2048 }
2049
2050 num_identities++;
2051 }
2052
2053 /* Check the syntax of the binders. The value will be checked later if
2054 * resuming. */
2055 size_t num_binders = 0;
2056 while (CBS_len(&binders) != 0) {
2057 CBS binder;
2058 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
2059 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2060 *out_alert = SSL_AD_DECODE_ERROR;
2061 return 0;
2062 }
2063
2064 num_binders++;
2065 }
2066
2067 if (num_identities != num_binders) {
2068 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
2069 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04002070 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04002071 }
2072
Steven Valdeza833c352016-11-01 13:39:36 -04002073 /* TODO(svaldez): Check that the ticket_age is valid when attempting to use
2074 * the PSK for 0-RTT. http://crbug.com/boringssl/113 */
2075
Steven Valdez4aa154e2016-07-29 14:32:55 -04002076 /* TLS 1.3 session tickets are renewed separately as part of the
2077 * NewSessionTicket. */
David Benjamine7f60a22016-11-16 18:54:25 +09002078 int unused_renew;
2079 if (!tls_process_ticket(ssl, out_session, &unused_renew, CBS_data(&ticket),
2080 CBS_len(&ticket), NULL, 0)) {
2081 *out_alert = SSL_AD_INTERNAL_ERROR;
2082 return 0;
2083 }
2084
2085 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002086}
2087
David Benjamin8baf9632016-11-17 17:11:16 +09002088int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2089 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002090 return 1;
2091 }
2092
2093 CBB contents;
2094 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2095 !CBB_add_u16_length_prefixed(out, &contents) ||
2096 /* We only consider the first identity for resumption */
2097 !CBB_add_u16(&contents, 0) ||
2098 !CBB_flush(out)) {
2099 return 0;
2100 }
2101
2102 return 1;
2103}
2104
2105
Steven Valdeza833c352016-11-01 13:39:36 -04002106/* Pre-Shared Key Exchange Modes
2107 *
2108 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002109
David Benjamin8c880a22016-12-03 02:20:34 -05002110static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2111 CBB *out) {
2112 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04002113 uint16_t min_version, max_version;
2114 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2115 return 0;
2116 }
2117
2118 if (max_version < TLS1_3_VERSION) {
2119 return 1;
2120 }
2121
2122 CBB contents, ke_modes;
2123 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2124 !CBB_add_u16_length_prefixed(out, &contents) ||
2125 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2126 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2127 return 0;
2128 }
2129
2130 return CBB_flush(out);
2131}
2132
David Benjamin8c880a22016-12-03 02:20:34 -05002133static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002134 uint8_t *out_alert,
2135 CBS *contents) {
2136 if (contents == NULL) {
2137 return 1;
2138 }
2139
Steven Valdeza833c352016-11-01 13:39:36 -04002140 CBS ke_modes;
2141 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2142 CBS_len(&ke_modes) == 0 ||
2143 CBS_len(contents) != 0) {
2144 *out_alert = SSL_AD_DECODE_ERROR;
2145 return 0;
2146 }
2147
2148 /* We only support tickets with PSK_DHE_KE. */
David Benjamin8c880a22016-12-03 02:20:34 -05002149 hs->accept_psk_mode =
Steven Valdeza833c352016-11-01 13:39:36 -04002150 memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, CBS_len(&ke_modes)) != NULL;
2151
2152 return 1;
2153}
2154
2155
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002156/* Early Data Indication
2157 *
2158 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2159
David Benjamin8c880a22016-12-03 02:20:34 -05002160static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002161 /* TODO(svaldez): Support 0RTT. */
2162 return 1;
2163}
2164
David Benjamin8c880a22016-12-03 02:20:34 -05002165static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2166 uint8_t *out_alert, CBS *contents) {
2167 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002168 if (contents == NULL) {
2169 return 1;
2170 }
2171
2172 if (CBS_len(contents) != 0) {
2173 *out_alert = SSL_AD_DECODE_ERROR;
2174 return 0;
2175 }
2176
2177 /* Since we don't currently accept 0-RTT, we have to skip past any early data
2178 * the client might have sent. */
2179 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
2180 ssl->s3->skip_early_data = 1;
2181 }
2182 return 1;
2183}
2184
2185
Steven Valdez143e8b32016-07-11 13:19:03 -04002186/* Key Share
2187 *
David Benjamina128a552016-10-13 14:26:33 -04002188 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002189
David Benjamin8c880a22016-12-03 02:20:34 -05002190static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2191 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002192 uint16_t min_version, max_version;
2193 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2194 return 0;
2195 }
2196
David Benjamin53a2dfc2016-10-07 21:04:12 -04002197 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002198 return 1;
2199 }
2200
2201 CBB contents, kse_bytes;
2202 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2203 !CBB_add_u16_length_prefixed(out, &contents) ||
2204 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2205 return 0;
2206 }
2207
David Benjamin8c880a22016-12-03 02:20:34 -05002208 uint16_t group_id = hs->retry_group;
2209 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002210 /* We received a HelloRetryRequest without a new curve, so there is no new
2211 * share to append. Leave |ecdh_ctx| as-is. */
2212 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002213 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2214 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002215 return 0;
2216 }
David Benjamin8c880a22016-12-03 02:20:34 -05002217 OPENSSL_free(hs->key_share_bytes);
2218 hs->key_share_bytes = NULL;
2219 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002220 if (group_id == 0) {
2221 return CBB_flush(out);
2222 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002223 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002224 /* Add a fake group. See draft-davidben-tls-grease-01. */
2225 if (ssl->ctx->grease_enabled &&
2226 (!CBB_add_u16(&kse_bytes,
2227 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2228 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2229 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2230 return 0;
2231 }
2232
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002233 /* Predict the most preferred group. */
2234 const uint16_t *groups;
2235 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002236 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002237 if (groups_len == 0) {
2238 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2239 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002240 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002241
2242 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002243 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002244
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002245 CBB key_exchange;
2246 if (!CBB_add_u16(&kse_bytes, group_id) ||
2247 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002248 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2249 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002250 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002251 return 0;
2252 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002253
David Benjamin8c880a22016-12-03 02:20:34 -05002254 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002255 /* Save the contents of the extension to repeat it in the second
2256 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002257 hs->key_share_bytes_len = CBB_len(&kse_bytes);
2258 hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
2259 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002260 return 0;
2261 }
2262 }
2263
Steven Valdez143e8b32016-07-11 13:19:03 -04002264 return CBB_flush(out);
2265}
2266
David Benjamin8baf9632016-11-17 17:11:16 +09002267int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002268 size_t *out_secret_len,
2269 uint8_t *out_alert, CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09002270 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002271 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002272 uint16_t group_id;
2273 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002274 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2275 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002276 *out_alert = SSL_AD_DECODE_ERROR;
2277 return 0;
2278 }
2279
David Benjamin8baf9632016-11-17 17:11:16 +09002280 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002281 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2282 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2283 return 0;
2284 }
2285
David Benjamin8baf9632016-11-17 17:11:16 +09002286 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2287 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002288 *out_alert = SSL_AD_INTERNAL_ERROR;
2289 return 0;
2290 }
2291
David Benjamin5c4e8572016-08-19 17:44:53 -04002292 ssl->s3->new_session->key_exchange_info = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002293 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002294 return 1;
2295}
2296
David Benjamin8baf9632016-11-17 17:11:16 +09002297int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002298 uint8_t **out_secret,
2299 size_t *out_secret_len,
2300 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002301 uint16_t group_id;
2302 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002303 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002304 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2305 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2306 return 0;
2307 }
2308
2309 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002310 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002311 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002312 return 0;
2313 }
2314
David Benjamin7e1f9842016-09-20 19:24:40 -04002315 /* Find the corresponding key share. */
2316 int found = 0;
2317 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002318 while (CBS_len(&key_shares) > 0) {
2319 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002320 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002321 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002322 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2323 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002324 return 0;
2325 }
2326
David Benjamin7e1f9842016-09-20 19:24:40 -04002327 if (id == group_id) {
2328 if (found) {
2329 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2330 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2331 return 0;
2332 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002333
David Benjamin7e1f9842016-09-20 19:24:40 -04002334 found = 1;
2335 peer_key = peer_key_tmp;
2336 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002337 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002338 }
2339
David Benjamin7e1f9842016-09-20 19:24:40 -04002340 if (!found) {
2341 *out_found = 0;
2342 *out_secret = NULL;
2343 *out_secret_len = 0;
2344 return 1;
2345 }
2346
2347 /* Compute the DH secret. */
2348 uint8_t *secret = NULL;
2349 size_t secret_len;
2350 SSL_ECDH_CTX group;
2351 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2352 CBB public_key;
2353 if (!CBB_init(&public_key, 32) ||
2354 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002355 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
2356 CBS_data(&peer_key), CBS_len(&peer_key)) ||
2357 !CBB_finish(&public_key, &hs->public_key, &hs->public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002358 OPENSSL_free(secret);
2359 SSL_ECDH_CTX_cleanup(&group);
2360 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002361 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002362 return 0;
2363 }
2364
2365 SSL_ECDH_CTX_cleanup(&group);
2366
2367 *out_secret = secret;
2368 *out_secret_len = secret_len;
2369 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002370 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002371}
2372
David Benjamin8baf9632016-11-17 17:11:16 +09002373int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2374 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002375 uint16_t group_id;
2376 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002377 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002378 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2379 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2380 !CBB_add_u16(&kse_bytes, group_id) ||
2381 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002382 !CBB_add_bytes(&public_key, hs->public_key, hs->public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002383 !CBB_flush(out)) {
2384 return 0;
2385 }
2386
David Benjamin8baf9632016-11-17 17:11:16 +09002387 OPENSSL_free(hs->public_key);
2388 hs->public_key = NULL;
2389 hs->public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002390
David Benjamin5c4e8572016-08-19 17:44:53 -04002391 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002392 return 1;
2393}
2394
2395
Steven Valdezfdd10992016-09-15 16:27:05 -04002396/* Supported Versions
2397 *
2398 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2399
David Benjamin8c880a22016-12-03 02:20:34 -05002400static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2401 SSL *const ssl = hs->ssl;
Steven Valdezfdd10992016-09-15 16:27:05 -04002402 uint16_t min_version, max_version;
2403 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2404 return 0;
2405 }
2406
2407 if (max_version <= TLS1_2_VERSION) {
2408 return 1;
2409 }
2410
2411 CBB contents, versions;
2412 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2413 !CBB_add_u16_length_prefixed(out, &contents) ||
2414 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2415 return 0;
2416 }
2417
David Benjamind9791bf2016-09-27 16:39:52 -04002418 /* Add a fake version. See draft-davidben-tls-grease-01. */
2419 if (ssl->ctx->grease_enabled &&
2420 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2421 return 0;
2422 }
2423
Steven Valdezfdd10992016-09-15 16:27:05 -04002424 for (uint16_t version = max_version; version >= min_version; version--) {
2425 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2426 return 0;
2427 }
2428 }
2429
2430 if (!CBB_flush(out)) {
2431 return 0;
2432 }
2433
2434 return 1;
2435}
2436
2437
David Benjamin3baa6e12016-10-07 21:10:38 -04002438/* Cookie
2439 *
2440 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2441
David Benjamin8c880a22016-12-03 02:20:34 -05002442static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2443 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002444 return 1;
2445 }
2446
2447 CBB contents, cookie;
2448 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2449 !CBB_add_u16_length_prefixed(out, &contents) ||
2450 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002451 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002452 !CBB_flush(out)) {
2453 return 0;
2454 }
2455
2456 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002457 OPENSSL_free(hs->cookie);
2458 hs->cookie = NULL;
2459 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002460 return 1;
2461}
2462
2463
Steven Valdezce902a92016-05-17 11:47:53 -04002464/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002465 *
Steven Valdezce902a92016-05-17 11:47:53 -04002466 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002467 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002468
David Benjamin8c880a22016-12-03 02:20:34 -05002469static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2470 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002471 CBB contents, groups_bytes;
2472 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002473 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002474 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002475 return 0;
2476 }
2477
David Benjamin65ac9972016-09-02 21:35:25 -04002478 /* Add a fake group. See draft-davidben-tls-grease-01. */
2479 if (ssl->ctx->grease_enabled &&
2480 !CBB_add_u16(&groups_bytes,
2481 ssl_get_grease_value(ssl, ssl_grease_group))) {
2482 return 0;
2483 }
2484
Steven Valdezce902a92016-05-17 11:47:53 -04002485 const uint16_t *groups;
2486 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002487 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002488
David Benjamin54091232016-09-05 12:47:25 -04002489 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002490 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002491 return 0;
2492 }
2493 }
2494
2495 return CBB_flush(out);
2496}
2497
David Benjamin8c880a22016-12-03 02:20:34 -05002498static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2499 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002500 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002501 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2502 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002503 return 1;
2504}
2505
David Benjamin8c880a22016-12-03 02:20:34 -05002506static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2507 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002508 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002509 if (contents == NULL) {
2510 return 1;
2511 }
2512
Steven Valdezce902a92016-05-17 11:47:53 -04002513 CBS supported_group_list;
2514 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2515 CBS_len(&supported_group_list) == 0 ||
2516 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002517 CBS_len(contents) != 0) {
2518 return 0;
2519 }
2520
David Benjamin8c880a22016-12-03 02:20:34 -05002521 hs->peer_supported_group_list =
2522 OPENSSL_malloc(CBS_len(&supported_group_list));
2523 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002524 *out_alert = SSL_AD_INTERNAL_ERROR;
2525 return 0;
2526 }
2527
Steven Valdezce902a92016-05-17 11:47:53 -04002528 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002529 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002530 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002531 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002532 goto err;
2533 }
2534 }
2535
Steven Valdezce902a92016-05-17 11:47:53 -04002536 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002537 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002538
2539 return 1;
2540
2541err:
David Benjamin8c880a22016-12-03 02:20:34 -05002542 OPENSSL_free(hs->peer_supported_group_list);
2543 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002544 *out_alert = SSL_AD_INTERNAL_ERROR;
2545 return 0;
2546}
2547
David Benjamin8c880a22016-12-03 02:20:34 -05002548static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002549 /* Servers don't echo this extension. */
2550 return 1;
2551}
2552
2553
Adam Langley614c66a2015-06-12 15:26:58 -07002554/* kExtensions contains all the supported extensions. */
2555static const struct tls_extension kExtensions[] = {
2556 {
Adam Langley5021b222015-06-12 18:27:58 -07002557 TLSEXT_TYPE_renegotiate,
2558 NULL,
2559 ext_ri_add_clienthello,
2560 ext_ri_parse_serverhello,
2561 ext_ri_parse_clienthello,
2562 ext_ri_add_serverhello,
2563 },
2564 {
Adam Langley614c66a2015-06-12 15:26:58 -07002565 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002566 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002567 ext_sni_add_clienthello,
2568 ext_sni_parse_serverhello,
2569 ext_sni_parse_clienthello,
2570 ext_sni_add_serverhello,
2571 },
Adam Langley0a056712015-07-01 15:03:33 -07002572 {
2573 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002574 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002575 ext_ems_add_clienthello,
2576 ext_ems_parse_serverhello,
2577 ext_ems_parse_clienthello,
2578 ext_ems_add_serverhello,
2579 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002580 {
2581 TLSEXT_TYPE_session_ticket,
2582 NULL,
2583 ext_ticket_add_clienthello,
2584 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002585 /* Ticket extension client parsing is handled in ssl_session.c */
2586 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002587 ext_ticket_add_serverhello,
2588 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002589 {
2590 TLSEXT_TYPE_signature_algorithms,
2591 NULL,
2592 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002593 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002594 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002595 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002596 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002597 {
2598 TLSEXT_TYPE_status_request,
2599 ext_ocsp_init,
2600 ext_ocsp_add_clienthello,
2601 ext_ocsp_parse_serverhello,
2602 ext_ocsp_parse_clienthello,
2603 ext_ocsp_add_serverhello,
2604 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002605 {
2606 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002607 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002608 ext_npn_add_clienthello,
2609 ext_npn_parse_serverhello,
2610 ext_npn_parse_clienthello,
2611 ext_npn_add_serverhello,
2612 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002613 {
2614 TLSEXT_TYPE_certificate_timestamp,
2615 NULL,
2616 ext_sct_add_clienthello,
2617 ext_sct_parse_serverhello,
2618 ext_sct_parse_clienthello,
2619 ext_sct_add_serverhello,
2620 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002621 {
2622 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002623 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002624 ext_alpn_add_clienthello,
2625 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002626 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2627 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002628 ext_alpn_add_serverhello,
2629 },
Adam Langley49c7af12015-07-10 14:33:46 -07002630 {
2631 TLSEXT_TYPE_channel_id,
2632 ext_channel_id_init,
2633 ext_channel_id_add_clienthello,
2634 ext_channel_id_parse_serverhello,
2635 ext_channel_id_parse_clienthello,
2636 ext_channel_id_add_serverhello,
2637 },
Adam Langley391250d2015-07-15 19:06:07 -07002638 {
2639 TLSEXT_TYPE_srtp,
2640 ext_srtp_init,
2641 ext_srtp_add_clienthello,
2642 ext_srtp_parse_serverhello,
2643 ext_srtp_parse_clienthello,
2644 ext_srtp_add_serverhello,
2645 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002646 {
2647 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002648 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002649 ext_ec_point_add_clienthello,
2650 ext_ec_point_parse_serverhello,
2651 ext_ec_point_parse_clienthello,
2652 ext_ec_point_add_serverhello,
2653 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002654 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002655 TLSEXT_TYPE_key_share,
2656 NULL,
2657 ext_key_share_add_clienthello,
2658 forbid_parse_serverhello,
2659 ignore_parse_clienthello,
2660 dont_add_serverhello,
2661 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002662 {
Steven Valdeza833c352016-11-01 13:39:36 -04002663 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002664 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002665 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002666 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002667 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002668 dont_add_serverhello,
2669 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002670 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002671 TLSEXT_TYPE_early_data,
2672 NULL,
2673 ext_early_data_add_clienthello,
2674 forbid_parse_serverhello,
2675 ext_early_data_parse_clienthello,
2676 dont_add_serverhello,
2677 },
2678 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002679 TLSEXT_TYPE_supported_versions,
2680 NULL,
2681 ext_supported_versions_add_clienthello,
2682 forbid_parse_serverhello,
2683 ignore_parse_clienthello,
2684 dont_add_serverhello,
2685 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002686 {
2687 TLSEXT_TYPE_cookie,
2688 NULL,
2689 ext_cookie_add_clienthello,
2690 forbid_parse_serverhello,
2691 ignore_parse_clienthello,
2692 dont_add_serverhello,
2693 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002694 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2695 * intolerant to the last extension being zero-length. See
2696 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002697 {
Steven Valdezce902a92016-05-17 11:47:53 -04002698 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002699 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002700 ext_supported_groups_add_clienthello,
2701 ext_supported_groups_parse_serverhello,
2702 ext_supported_groups_parse_clienthello,
2703 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002704 },
Adam Langley614c66a2015-06-12 15:26:58 -07002705};
2706
2707#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2708
Adam Langley4cfa96b2015-07-01 11:56:55 -07002709OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002710 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002711 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002712OPENSSL_COMPILE_ASSERT(
2713 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2714 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002715
Adam Langley614c66a2015-06-12 15:26:58 -07002716static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2717 uint16_t value) {
2718 unsigned i;
2719 for (i = 0; i < kNumExtensions; i++) {
2720 if (kExtensions[i].value == value) {
2721 *out_index = i;
2722 return &kExtensions[i];
2723 }
2724 }
2725
2726 return NULL;
2727}
2728
Adam Langley09505632015-07-30 18:10:13 -07002729int SSL_extension_supported(unsigned extension_value) {
2730 uint32_t index;
2731 return extension_value == TLSEXT_TYPE_padding ||
2732 tls_extension_find(&index, extension_value) != NULL;
2733}
2734
David Benjamin8c880a22016-12-03 02:20:34 -05002735int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2736 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002737 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002738 if (ssl->client_version == SSL3_VERSION &&
2739 !ssl->s3->send_connection_binding) {
2740 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002741 }
Adam Langley95c29f32014-06-20 12:00:00 -07002742
David Benjamine8d53502015-10-10 14:13:23 -04002743 CBB extensions;
2744 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002745 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002746 }
Adam Langley95c29f32014-06-20 12:00:00 -07002747
David Benjamin8c880a22016-12-03 02:20:34 -05002748 hs->extensions.sent = 0;
2749 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002750
David Benjamin54091232016-09-05 12:47:25 -04002751 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002752 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002753 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002754 }
2755 }
Adam Langley95c29f32014-06-20 12:00:00 -07002756
David Benjamin65ac9972016-09-02 21:35:25 -04002757 uint16_t grease_ext1 = 0;
2758 if (ssl->ctx->grease_enabled) {
2759 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2760 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2761 if (!CBB_add_u16(&extensions, grease_ext1) ||
2762 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2763 goto err;
2764 }
2765 }
2766
David Benjamin54091232016-09-05 12:47:25 -04002767 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002768 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002769 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002770 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002771 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002772 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002773 }
Adam Langley95c29f32014-06-20 12:00:00 -07002774
Adam Langley33ad2b52015-07-20 17:43:53 -07002775 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002776 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002777 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002778 }
Adam Langley75712922014-10-10 16:23:43 -07002779
David Benjamin2bd19172016-11-17 16:47:15 +09002780 if (!custom_ext_add_clienthello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002781 goto err;
2782 }
2783
David Benjamin65ac9972016-09-02 21:35:25 -04002784 if (ssl->ctx->grease_enabled) {
2785 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2786 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2787
2788 /* The two fake extensions must not have the same value. GREASE values are
2789 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2790 * one. */
2791 if (grease_ext1 == grease_ext2) {
2792 grease_ext2 ^= 0x1010;
2793 }
2794
2795 if (!CBB_add_u16(&extensions, grease_ext2) ||
2796 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2797 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2798 goto err;
2799 }
2800 }
2801
David Benjamince079fd2016-08-02 16:22:34 -04002802 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002803 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002804 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002805 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002806 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002807 *
2808 * NB: because this code works out the length of all existing extensions
2809 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002810 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002811 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002812 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002813 * Server 7.0 is intolerant to the last extension being zero-length. See
2814 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002815 if (padding_len >= 4 + 1) {
2816 padding_len -= 4;
2817 } else {
2818 padding_len = 1;
2819 }
Adam Langley95c29f32014-06-20 12:00:00 -07002820
Adam Langley33ad2b52015-07-20 17:43:53 -07002821 uint8_t *padding_bytes;
2822 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2823 !CBB_add_u16(&extensions, padding_len) ||
2824 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2825 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002826 }
Adam Langley75712922014-10-10 16:23:43 -07002827
Adam Langley33ad2b52015-07-20 17:43:53 -07002828 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002829 }
2830 }
Adam Langley75712922014-10-10 16:23:43 -07002831
Steven Valdeza833c352016-11-01 13:39:36 -04002832 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002833 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002834 goto err;
2835 }
2836
David Benjamina01deee2015-12-08 18:56:31 -05002837 /* Discard empty extensions blocks. */
2838 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002839 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002840 }
2841
David Benjamine8d53502015-10-10 14:13:23 -04002842 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002843
2844err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002845 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002846 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002847}
Adam Langley95c29f32014-06-20 12:00:00 -07002848
David Benjamin8c880a22016-12-03 02:20:34 -05002849int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2850 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002851 CBB extensions;
2852 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002853 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002854 }
2855
David Benjamin8c880a22016-12-03 02:20:34 -05002856 for (unsigned i = 0; i < kNumExtensions; i++) {
2857 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002858 /* Don't send extensions that were not received. */
2859 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002860 }
Adam Langley95c29f32014-06-20 12:00:00 -07002861
David Benjamin8c880a22016-12-03 02:20:34 -05002862 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002863 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002864 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002865 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002866 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002867 }
Adam Langley95c29f32014-06-20 12:00:00 -07002868
David Benjamin2bd19172016-11-17 16:47:15 +09002869 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002870 goto err;
2871 }
2872
Steven Valdez143e8b32016-07-11 13:19:03 -04002873 /* Discard empty extensions blocks before TLS 1.3. */
2874 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2875 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002876 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002877 }
2878
David Benjamin56380462015-10-10 14:59:09 -04002879 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002880
2881err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002882 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002883 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002884}
Adam Langley95c29f32014-06-20 12:00:00 -07002885
David Benjamin731058e2016-12-03 23:15:13 -05002886static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2887 const SSL_CLIENT_HELLO *client_hello,
2888 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002889 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002890 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002891 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002892 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002893 }
2894 }
2895
David Benjamin8c880a22016-12-03 02:20:34 -05002896 hs->extensions.received = 0;
2897 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002898
David Benjamine14ff062016-08-09 16:21:24 -04002899 CBS extensions;
2900 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2901 while (CBS_len(&extensions) != 0) {
2902 uint16_t type;
2903 CBS extension;
2904
2905 /* Decode the next extension. */
2906 if (!CBS_get_u16(&extensions, &type) ||
2907 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002908 *out_alert = SSL_AD_DECODE_ERROR;
2909 return 0;
2910 }
Adam Langley95c29f32014-06-20 12:00:00 -07002911
David Benjamine14ff062016-08-09 16:21:24 -04002912 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2913 * ambiguous. Ignore all but the renegotiation_info extension. */
2914 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2915 continue;
2916 }
Adam Langley95c29f32014-06-20 12:00:00 -07002917
David Benjamine14ff062016-08-09 16:21:24 -04002918 unsigned ext_index;
2919 const struct tls_extension *const ext =
2920 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002921
David Benjamine14ff062016-08-09 16:21:24 -04002922 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002923 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002924 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002925 return 0;
2926 }
David Benjamine14ff062016-08-09 16:21:24 -04002927 continue;
2928 }
2929
David Benjamin8c880a22016-12-03 02:20:34 -05002930 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002931 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002932 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002933 *out_alert = alert;
2934 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002935 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002936 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002937 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002938 }
Adam Langley75712922014-10-10 16:23:43 -07002939
David Benjamin1deb41b2016-08-09 19:36:38 -04002940 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002941 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002942 continue;
2943 }
2944
2945 CBS *contents = NULL, fake_contents;
2946 static const uint8_t kFakeRenegotiateExtension[] = {0};
2947 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2948 ssl_client_cipher_list_contains_cipher(client_hello,
2949 SSL3_CK_SCSV & 0xffff)) {
2950 /* The renegotiation SCSV was received so pretend that we received a
2951 * renegotiation extension. */
2952 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2953 sizeof(kFakeRenegotiateExtension));
2954 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002955 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002956 }
2957
2958 /* Extension wasn't observed so call the callback with a NULL
2959 * parameter. */
2960 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002961 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002962 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002963 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002964 *out_alert = alert;
2965 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002966 }
2967 }
2968
Adam Langleyfcf25832014-12-18 17:42:32 -08002969 return 1;
2970}
Adam Langley95c29f32014-06-20 12:00:00 -07002971
David Benjamin731058e2016-12-03 23:15:13 -05002972int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2973 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002974 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002975 int alert = -1;
David Benjamin8c880a22016-12-03 02:20:34 -05002976 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002977 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002978 return 0;
2979 }
Adam Langley95c29f32014-06-20 12:00:00 -07002980
David Benjamin8c880a22016-12-03 02:20:34 -05002981 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002982 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002983 return 0;
2984 }
Adam Langley95c29f32014-06-20 12:00:00 -07002985
Adam Langleyfcf25832014-12-18 17:42:32 -08002986 return 1;
2987}
Adam Langley95c29f32014-06-20 12:00:00 -07002988
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002989OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2990
David Benjamin8c880a22016-12-03 02:20:34 -05002991static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2992 int *out_alert) {
2993 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002994 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2995 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2996 return 1;
2997 }
Adam Langley614c66a2015-06-12 15:26:58 -07002998
Steven Valdez143e8b32016-07-11 13:19:03 -04002999 /* Decode the extensions block and check it is valid. */
3000 CBS extensions;
3001 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
3002 !tls1_check_duplicate_extensions(&extensions)) {
3003 *out_alert = SSL_AD_DECODE_ERROR;
3004 return 0;
3005 }
3006
3007 uint32_t received = 0;
3008 while (CBS_len(&extensions) != 0) {
3009 uint16_t type;
3010 CBS extension;
3011
3012 /* Decode the next extension. */
3013 if (!CBS_get_u16(&extensions, &type) ||
3014 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003015 *out_alert = SSL_AD_DECODE_ERROR;
3016 return 0;
3017 }
Adam Langley95c29f32014-06-20 12:00:00 -07003018
Steven Valdez143e8b32016-07-11 13:19:03 -04003019 unsigned ext_index;
3020 const struct tls_extension *const ext =
3021 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07003022
Steven Valdez143e8b32016-07-11 13:19:03 -04003023 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09003024 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003025 return 0;
3026 }
Steven Valdez143e8b32016-07-11 13:19:03 -04003027 continue;
3028 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003029
David Benjamin8c880a22016-12-03 02:20:34 -05003030 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04003031 type != TLSEXT_TYPE_renegotiate) {
3032 /* If the extension was never sent then it is illegal, except for the
3033 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04003034 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3035 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04003036 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04003037 return 0;
3038 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003039
Steven Valdez143e8b32016-07-11 13:19:03 -04003040 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07003041
Steven Valdez143e8b32016-07-11 13:19:03 -04003042 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003043 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04003044 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003045 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04003046 *out_alert = alert;
3047 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07003048 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003049 }
Adam Langley95c29f32014-06-20 12:00:00 -07003050
David Benjamin54091232016-09-05 12:47:25 -04003051 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003052 if (!(received & (1u << i))) {
3053 /* Extension wasn't observed so call the callback with a NULL
3054 * parameter. */
3055 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003056 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003057 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003058 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07003059 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08003060 return 0;
3061 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003062 }
3063 }
Adam Langley95c29f32014-06-20 12:00:00 -07003064
Adam Langleyfcf25832014-12-18 17:42:32 -08003065 return 1;
3066}
Adam Langley95c29f32014-06-20 12:00:00 -07003067
David Benjamin8c880a22016-12-03 02:20:34 -05003068static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3069 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003070 int ret = SSL_TLSEXT_ERR_NOACK;
3071 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003072
David Benjamin78f8aab2016-03-10 16:33:58 -05003073 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003074 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003075 ssl->ctx->tlsext_servername_arg);
3076 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003077 ret = ssl->initial_ctx->tlsext_servername_callback(
3078 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003079 }
Adam Langley95c29f32014-06-20 12:00:00 -07003080
Adam Langleyfcf25832014-12-18 17:42:32 -08003081 switch (ret) {
3082 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003083 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003084 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003085
Adam Langleyfcf25832014-12-18 17:42:32 -08003086 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003087 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003088 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003089
Adam Langleyfcf25832014-12-18 17:42:32 -08003090 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003091 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003092 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003093
Adam Langleyfcf25832014-12-18 17:42:32 -08003094 default:
3095 return 1;
3096 }
3097}
Adam Langleyed8270a2014-09-02 13:52:56 -07003098
David Benjamin8c880a22016-12-03 02:20:34 -05003099static int ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs) {
3100 SSL *const ssl = hs->ssl;
David Benjaminfc059942015-07-30 23:01:59 -04003101 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08003102 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07003103
David Benjamin78f8aab2016-03-10 16:33:58 -05003104 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003105 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003106 ssl->ctx->tlsext_servername_arg);
3107 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003108 ret = ssl->initial_ctx->tlsext_servername_callback(
3109 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003110 }
Adam Langley95c29f32014-06-20 12:00:00 -07003111
Adam Langleyfcf25832014-12-18 17:42:32 -08003112 switch (ret) {
3113 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003114 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003115 return -1;
David Benjamin03973092014-06-24 23:27:17 -04003116
Adam Langleyfcf25832014-12-18 17:42:32 -08003117 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003118 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003119 return 1;
3120
3121 default:
3122 return 1;
3123 }
3124}
3125
David Benjamin8c880a22016-12-03 02:20:34 -05003126int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3127 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003128 int alert = -1;
David Benjamin8c880a22016-12-03 02:20:34 -05003129 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003130 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003131 return 0;
3132 }
3133
David Benjamin8c880a22016-12-03 02:20:34 -05003134 if (ssl_check_serverhello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04003135 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08003136 return 0;
3137 }
3138
3139 return 1;
3140}
Adam Langley95c29f32014-06-20 12:00:00 -07003141
David Benjamine3aa1d92015-06-16 15:34:50 -04003142int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05003143 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04003144 size_t ticket_len, const uint8_t *session_id,
3145 size_t session_id_len) {
3146 int ret = 1; /* Most errors are non-fatal. */
3147 SSL_CTX *ssl_ctx = ssl->initial_ctx;
3148 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003149
David Benjamine3aa1d92015-06-16 15:34:50 -04003150 HMAC_CTX hmac_ctx;
3151 HMAC_CTX_init(&hmac_ctx);
3152 EVP_CIPHER_CTX cipher_ctx;
3153 EVP_CIPHER_CTX_init(&cipher_ctx);
3154
David Benjaminef1b0092015-11-21 14:05:44 -05003155 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04003156 *out_session = NULL;
3157
Steven Valdez4aa154e2016-07-29 14:32:55 -04003158 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
3159 goto done;
3160 }
3161
David Benjamine3aa1d92015-06-16 15:34:50 -04003162 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3163 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003164 }
3165
David Benjaminadcc3952015-04-26 13:07:57 -04003166 /* Ensure there is room for the key name and the largest IV
3167 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3168 * the maximum IV length should be well under the minimum size for the
3169 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003170 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3171 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003172 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003173 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003174
David Benjamine3aa1d92015-06-16 15:34:50 -04003175 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003176 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3177 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3178 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003179 if (cb_ret < 0) {
3180 ret = 0;
3181 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003182 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003183 if (cb_ret == 0) {
3184 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003185 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003186 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003187 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003188 }
3189 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003190 /* Check the key name matches. */
3191 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3192 SSL_TICKET_KEY_NAME_LEN) != 0) {
3193 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003194 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003195 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3196 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003197 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003198 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3199 ssl_ctx->tlsext_tick_aes_key, iv)) {
3200 ret = 0;
3201 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003202 }
3203 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003204 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003205
David Benjamine3aa1d92015-06-16 15:34:50 -04003206 /* Check the MAC at the end of the ticket. */
3207 uint8_t mac[EVP_MAX_MD_SIZE];
3208 size_t mac_len = HMAC_size(&hmac_ctx);
3209 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003210 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003211 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003212 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003213 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3214 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003215 int mac_ok =
3216 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3217#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3218 mac_ok = 1;
3219#endif
3220 if (!mac_ok) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003221 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003222 }
3223
David Benjamine3aa1d92015-06-16 15:34:50 -04003224 /* Decrypt the session data. */
3225 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3226 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3227 mac_len;
3228 plaintext = OPENSSL_malloc(ciphertext_len);
3229 if (plaintext == NULL) {
3230 ret = 0;
3231 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003232 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003233 size_t plaintext_len;
3234#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3235 memcpy(plaintext, ciphertext, ciphertext_len);
3236 plaintext_len = ciphertext_len;
3237#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003238 if (ciphertext_len >= INT_MAX) {
3239 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003240 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003241 int len1, len2;
3242 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3243 (int)ciphertext_len) ||
3244 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
3245 ERR_clear_error(); /* Don't leave an error on the queue. */
3246 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003247 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003248 plaintext_len = (size_t)(len1 + len2);
3249#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003250
David Benjamine3aa1d92015-06-16 15:34:50 -04003251 /* Decode the session. */
David Benjaminfbc45d72016-09-22 01:21:24 -04003252 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003253 if (session == NULL) {
3254 ERR_clear_error(); /* Don't leave an error on the queue. */
3255 goto done;
3256 }
3257
3258 /* Copy the client's session ID into the new session, to denote the ticket has
3259 * been accepted. */
3260 memcpy(session->session_id, session_id, session_id_len);
3261 session->session_id_length = session_id_len;
3262
3263 *out_session = session;
3264
3265done:
3266 OPENSSL_free(plaintext);
3267 HMAC_CTX_cleanup(&hmac_ctx);
3268 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3269 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003270}
Adam Langley95c29f32014-06-20 12:00:00 -07003271
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003272int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003273 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003274 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003275 return 1;
3276 }
David Benjamincd996942014-07-20 16:23:51 -04003277
David Benjamin0fc37ef2016-08-17 15:29:46 -04003278 OPENSSL_free(hs->peer_sigalgs);
3279 hs->peer_sigalgs = NULL;
3280 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003281
3282 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003283 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003284 return 0;
3285 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003286 num_sigalgs /= 2;
3287
3288 /* supported_signature_algorithms in the certificate request is
3289 * allowed to be empty. */
3290 if (num_sigalgs == 0) {
3291 return 1;
3292 }
3293
Steven Valdez02563852016-06-23 13:33:05 -04003294 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3295 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003296 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3297 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003298 return 0;
3299 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003300 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003301
3302 CBS sigalgs;
3303 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003304 for (size_t i = 0; i < num_sigalgs; i++) {
3305 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003306 return 0;
3307 }
3308 }
Adam Langley95c29f32014-06-20 12:00:00 -07003309
Adam Langleyfcf25832014-12-18 17:42:32 -08003310 return 1;
3311}
David Benjaminec2f27d2014-11-13 19:17:25 -05003312
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003313int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3314 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003315 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003316
Steven Valdezf0451ca2016-06-29 13:16:27 -04003317 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3318 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3319 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003320 int type = ssl_private_key_type(ssl);
3321 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003322 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003323 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003324 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003325 if (ssl_is_ecdsa_key_type(type)) {
3326 *out = SSL_SIGN_ECDSA_SHA1;
3327 return 1;
3328 }
3329 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3330 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003331 }
3332
David Benjamin3ef76972016-10-17 17:59:54 -04003333 const uint16_t *sigalgs = cert->sigalgs;
3334 size_t num_sigalgs = cert->num_sigalgs;
3335 if (sigalgs == NULL) {
3336 sigalgs = kSignSignatureAlgorithms;
3337 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003338 }
3339
David Benjamin0fc37ef2016-08-17 15:29:46 -04003340 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3341 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3342 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003343 /* If the client didn't specify any signature_algorithms extension then
3344 * we can assume that it supports SHA1. See
3345 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3346 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3347 SSL_SIGN_ECDSA_SHA1};
3348 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003349 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003350 }
3351
David Benjamin0fc37ef2016-08-17 15:29:46 -04003352 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003353 uint16_t sigalg = sigalgs[i];
3354 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3355 * negotiated. */
3356 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3357 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3358 continue;
3359 }
3360
David Benjamin0fc37ef2016-08-17 15:29:46 -04003361 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003362 if (sigalg == peer_sigalgs[j]) {
3363 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003364 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003365 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003366 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003367 }
Adam Langley95c29f32014-06-20 12:00:00 -07003368
David Benjaminea9a0d52016-07-08 15:52:59 -07003369 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3370 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003371}
Adam Langley95c29f32014-06-20 12:00:00 -07003372
Nick Harper60a85cb2016-09-23 16:25:11 -07003373int tls1_verify_channel_id(SSL *ssl) {
3374 int ret = 0;
3375 uint16_t extension_type;
3376 CBS extension, channel_id;
3377
3378 /* A Channel ID handshake message is structured to contain multiple
3379 * extensions, but the only one that can be present is Channel ID. */
3380 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3381 if (!CBS_get_u16(&channel_id, &extension_type) ||
3382 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3383 CBS_len(&channel_id) != 0 ||
3384 extension_type != TLSEXT_TYPE_channel_id ||
3385 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3386 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3387 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3388 return 0;
3389 }
3390
3391 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3392 if (!p256) {
3393 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3394 return 0;
3395 }
3396
3397 EC_KEY *key = NULL;
3398 EC_POINT *point = NULL;
3399 BIGNUM x, y;
3400 ECDSA_SIG sig;
3401 BN_init(&x);
3402 BN_init(&y);
3403 sig.r = BN_new();
3404 sig.s = BN_new();
3405 if (sig.r == NULL || sig.s == NULL) {
3406 goto err;
3407 }
3408
3409 const uint8_t *p = CBS_data(&extension);
3410 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3411 BN_bin2bn(p + 32, 32, &y) == NULL ||
3412 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3413 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3414 goto err;
3415 }
3416
3417 point = EC_POINT_new(p256);
3418 if (point == NULL ||
3419 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3420 goto err;
3421 }
3422
3423 key = EC_KEY_new();
3424 if (key == NULL ||
3425 !EC_KEY_set_group(key, p256) ||
3426 !EC_KEY_set_public_key(key, point)) {
3427 goto err;
3428 }
3429
3430 uint8_t digest[EVP_MAX_MD_SIZE];
3431 size_t digest_len;
3432 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3433 goto err;
3434 }
3435
3436 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3437#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3438 sig_ok = 1;
3439#endif
3440 if (!sig_ok) {
3441 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3442 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3443 ssl->s3->tlsext_channel_id_valid = 0;
3444 goto err;
3445 }
3446
3447 memcpy(ssl->s3->tlsext_channel_id, p, 64);
3448 ret = 1;
3449
3450err:
3451 BN_free(&x);
3452 BN_free(&y);
3453 BN_free(sig.r);
3454 BN_free(sig.s);
3455 EC_KEY_free(key);
3456 EC_POINT_free(point);
3457 EC_GROUP_free(p256);
3458 return ret;
3459}
3460
3461int tls1_write_channel_id(SSL *ssl, CBB *cbb) {
3462 uint8_t digest[EVP_MAX_MD_SIZE];
3463 size_t digest_len;
3464 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3465 return 0;
3466 }
3467
3468 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3469 if (ec_key == NULL) {
3470 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3471 return 0;
3472 }
3473
3474 int ret = 0;
3475 BIGNUM *x = BN_new();
3476 BIGNUM *y = BN_new();
3477 ECDSA_SIG *sig = NULL;
3478 if (x == NULL || y == NULL ||
3479 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3480 EC_KEY_get0_public_key(ec_key),
3481 x, y, NULL)) {
3482 goto err;
3483 }
3484
3485 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3486 if (sig == NULL) {
3487 goto err;
3488 }
3489
3490 CBB child;
3491 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3492 !CBB_add_u16_length_prefixed(cbb, &child) ||
3493 !BN_bn2cbb_padded(&child, 32, x) ||
3494 !BN_bn2cbb_padded(&child, 32, y) ||
3495 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3496 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3497 !CBB_flush(cbb)) {
3498 goto err;
3499 }
3500
3501 ret = 1;
3502
3503err:
3504 BN_free(x);
3505 BN_free(y);
3506 ECDSA_SIG_free(sig);
3507 return ret;
3508}
3509
David Benjamind6a4ae92015-08-06 11:10:51 -04003510int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003511 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3512 uint8_t *msg;
3513 size_t msg_len;
3514 if (!tls13_get_cert_verify_signature_input(ssl, &msg, &msg_len,
3515 ssl_cert_verify_channel_id)) {
3516 return 0;
3517 }
3518 SHA256(msg, msg_len, out);
3519 *out_len = SHA256_DIGEST_LENGTH;
3520 OPENSSL_free(msg);
3521 return 1;
3522 }
3523
Nick Harper95594012016-10-20 14:07:13 -07003524 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003525
Nick Harper95594012016-10-20 14:07:13 -07003526 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003527 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003528 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003529
Steven Valdez87eab492016-06-27 16:34:59 -04003530 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003531 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003532 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003533 if (ssl->session->original_handshake_hash_len == 0) {
3534 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003535 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003536 }
Nick Harper95594012016-10-20 14:07:13 -07003537 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3538 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003539 }
3540
3541 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3542 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3543 sizeof(handshake_hash));
3544 if (handshake_hash_len < 0) {
Nick Harper95594012016-10-20 14:07:13 -07003545 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003546 }
Nick Harper95594012016-10-20 14:07:13 -07003547 SHA256_Update(&ctx, handshake_hash, (size_t)handshake_hash_len);
3548 SHA256_Final(out, &ctx);
3549 *out_len = SHA256_DIGEST_LENGTH;
3550 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003551}
Adam Langley1258b6a2014-06-20 12:00:00 -07003552
3553/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003554 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003555 * data. */
3556int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003557 int digest_len;
3558 /* This function should never be called for a resumed session because the
3559 * handshake hashes that we wish to record are for the original, full
3560 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003561 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003562 return -1;
3563 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003564
Adam Langleyfcf25832014-12-18 17:42:32 -08003565 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003566 tls1_handshake_digest(
3567 ssl, ssl->s3->new_session->original_handshake_hash,
3568 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003569 if (digest_len < 0) {
3570 return -1;
3571 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003572
Steven Valdez87eab492016-06-27 16:34:59 -04003573 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003574
Adam Langleyfcf25832014-12-18 17:42:32 -08003575 return 1;
3576}
Nick Harper60a85cb2016-09-23 16:25:11 -07003577
3578int ssl_do_channel_id_callback(SSL *ssl) {
3579 if (ssl->tlsext_channel_id_private != NULL ||
3580 ssl->ctx->channel_id_cb == NULL) {
3581 return 1;
3582 }
3583
3584 EVP_PKEY *key = NULL;
3585 ssl->ctx->channel_id_cb(ssl, &key);
3586 if (key == NULL) {
3587 /* The caller should try again later. */
3588 return 1;
3589 }
3590
3591 int ret = SSL_set1_tls_channel_id(ssl, key);
3592 EVP_PKEY_free(key);
3593 return ret;
3594}
Adam Langleycfa08c32016-11-17 13:21:27 -08003595
3596int ssl_is_sct_list_valid(const CBS *contents) {
3597 /* Shallow parse the SCT list for sanity. By the RFC
3598 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3599 * of the SCTs may be empty. */
3600 CBS copy = *contents;
3601 CBS sct_list;
3602 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3603 CBS_len(&copy) != 0 ||
3604 CBS_len(&sct_list) == 0) {
3605 return 0;
3606 }
3607
3608 while (CBS_len(&sct_list) > 0) {
3609 CBS sct;
3610 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3611 CBS_len(&sct) == 0) {
3612 return 0;
3613 }
3614 }
3615
3616 return 1;
3617}