blob: c27e84d9e7e4ecf0427aa62afa5910ac1cf3f987 [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) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500208 OPENSSL_memset(out, 0, sizeof(*out));
David Benjamin731058e2016-12-03 23:15:13 -0500209 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 Langleyfcf25832014-12-18 17:42:32 -0800311};
Adam Langley95c29f32014-06-20 12:00:00 -0700312
David Benjaminf04976b2016-10-07 00:37:55 -0400313void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
Steven Valdez5440fe02016-07-18 12:40:30 -0400314 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400315 *out_group_ids = ssl->supported_group_list;
316 *out_group_ids_len = ssl->supported_group_list_len;
317 if (!*out_group_ids) {
318 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400319 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 }
321}
David Benjamined439582014-07-14 19:13:02 -0400322
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900323int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
324 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400325 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400326
David Benjaminf04976b2016-10-07 00:37:55 -0400327 const uint16_t *groups, *pref, *supp;
328 size_t groups_len, pref_len, supp_len;
329 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800330
David Benjaminf04976b2016-10-07 00:37:55 -0400331 /* Clients are not required to send a supported_groups extension. In this
332 * case, the server is free to pick any group it likes. See RFC 4492,
333 * section 4, paragraph 3.
334 *
335 * However, in the interests of compatibility, we will skip ECDH if the
336 * client didn't send an extension because we can't be sure that they'll
337 * support our favoured group. Thus we do not special-case an emtpy
338 * |peer_supported_group_list|. */
David Benjamin55a43642015-04-20 14:45:55 -0400339
David Benjamin4298d772015-12-19 00:18:25 -0500340 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400341 pref = groups;
342 pref_len = groups_len;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900343 supp = hs->peer_supported_group_list;
344 supp_len = hs->peer_supported_group_list_len;
David Benjamin55a43642015-04-20 14:45:55 -0400345 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900346 pref = hs->peer_supported_group_list;
347 pref_len = hs->peer_supported_group_list_len;
Steven Valdezce902a92016-05-17 11:47:53 -0400348 supp = groups;
349 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400350 }
351
David Benjaminf04976b2016-10-07 00:37:55 -0400352 for (size_t i = 0; i < pref_len; i++) {
353 for (size_t j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400355 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500356 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 }
358 }
359 }
360
David Benjamin4298d772015-12-19 00:18:25 -0500361 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800362}
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Steven Valdezce902a92016-05-17 11:47:53 -0400364int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400366 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800367
Steven Valdezce902a92016-05-17 11:47:53 -0400368 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
369 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 return 0;
371 }
372
David Benjamin54091232016-09-05 12:47:25 -0400373 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400374 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
375 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 return 0;
377 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 }
379
Steven Valdezce902a92016-05-17 11:47:53 -0400380 OPENSSL_free(*out_group_ids);
381 *out_group_ids = group_ids;
382 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800383
384 return 1;
385}
Adam Langley95c29f32014-06-20 12:00:00 -0700386
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100387int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
388 const char *curves) {
389 uint16_t *group_ids = NULL;
390 size_t ncurves = 0;
391
392 const char *col;
393 const char *ptr = curves;
394
395 do {
396 col = strchr(ptr, ':');
397
398 uint16_t group_id;
399 if (!ssl_name_to_group_id(&group_id, ptr,
400 col ? (size_t)(col - ptr) : strlen(ptr))) {
401 goto err;
402 }
403
404 uint16_t *new_group_ids = OPENSSL_realloc(group_ids,
405 (ncurves + 1) * sizeof(uint16_t));
406 if (new_group_ids == NULL) {
407 goto err;
408 }
409 group_ids = new_group_ids;
410
411 group_ids[ncurves] = group_id;
412 ncurves++;
413
414 if (col) {
415 ptr = col + 1;
416 }
417 } while (col);
418
419 OPENSSL_free(*out_group_ids);
420 *out_group_ids = group_ids;
421 *out_group_ids_len = ncurves;
422
423 return 1;
424
425err:
426 OPENSSL_free(group_ids);
427 return 0;
428}
429
Steven Valdezce902a92016-05-17 11:47:53 -0400430int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
431 const uint16_t *groups;
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400432 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -0400433 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400434 for (size_t i = 0; i < groups_len; i++) {
435 if (groups[i] == group_id) {
436 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800438 }
David Benjamin033e5f42014-11-13 18:47:41 -0500439
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400440 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800441}
David Benjamin033e5f42014-11-13 18:47:41 -0500442
David Benjamin3ef76972016-10-17 17:59:54 -0400443/* kVerifySignatureAlgorithms is the default list of accepted signature
David Benjamin3a322f52016-10-26 12:45:35 -0400444 * algorithms for verifying.
445 *
446 * For now, RSA-PSS signature algorithms are not enabled on Android's system
447 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
448 * restore them. */
David Benjamin3ef76972016-10-17 17:59:54 -0400449static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjamin3a322f52016-10-26 12:45:35 -0400450 /* Prefer SHA-256 algorithms. */
451 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400452#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400453 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400454#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400455 SSL_SIGN_RSA_PKCS1_SHA256,
456
457 /* Larger hashes are acceptable. */
458 SSL_SIGN_ECDSA_SECP384R1_SHA384,
459#if !defined(BORINGSSL_ANDROID_SYSTEM)
460 SSL_SIGN_RSA_PSS_SHA384,
461#endif
462 SSL_SIGN_RSA_PKCS1_SHA384,
463
David Benjamin76bb1412016-09-08 16:03:49 -0400464 /* TODO(davidben): Remove this. */
David Benjamin3ef76972016-10-17 17:59:54 -0400465#if defined(BORINGSSL_ANDROID_SYSTEM)
466 SSL_SIGN_ECDSA_SECP521R1_SHA512,
467#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400468#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400469 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400470#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400471 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400472
David Benjamin3a322f52016-10-26 12:45:35 -0400473 /* For now, SHA-1 is still accepted but least preferable. */
474 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400475
David Benjamin3a322f52016-10-26 12:45:35 -0400476};
477
478/* kSignSignatureAlgorithms is the default list of supported signature
479 * algorithms for signing.
480 *
481 * For now, RSA-PSS signature algorithms are not enabled on Android's system
482 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
483 * restore them. */
484static const uint16_t kSignSignatureAlgorithms[] = {
485 /* Prefer SHA-256 algorithms. */
486 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400487#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400488 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400489#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400490 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400491
David Benjamin3a322f52016-10-26 12:45:35 -0400492 /* If needed, sign larger hashes.
493 *
494 * TODO(davidben): Determine which of these may be pruned. */
495 SSL_SIGN_ECDSA_SECP384R1_SHA384,
496#if !defined(BORINGSSL_ANDROID_SYSTEM)
497 SSL_SIGN_RSA_PSS_SHA384,
498#endif
499 SSL_SIGN_RSA_PKCS1_SHA384,
500
501 SSL_SIGN_ECDSA_SECP521R1_SHA512,
502#if !defined(BORINGSSL_ANDROID_SYSTEM)
503 SSL_SIGN_RSA_PSS_SHA512,
504#endif
505 SSL_SIGN_RSA_PKCS1_SHA512,
506
507 /* If the peer supports nothing else, sign with SHA-1. */
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400508 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400509 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400510};
511
David Benjamin3ef76972016-10-17 17:59:54 -0400512size_t tls12_get_verify_sigalgs(const SSL *ssl, const uint16_t **out) {
513 *out = kVerifySignatureAlgorithms;
514 return OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
Adam Langleyfcf25832014-12-18 17:42:32 -0800515}
Adam Langley95c29f32014-06-20 12:00:00 -0700516
David Benjamin887c3002016-07-08 16:15:32 -0700517int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
David Benjamin3ef76972016-10-17 17:59:54 -0400518 const uint16_t *verify_sigalgs;
519 size_t num_verify_sigalgs = tls12_get_verify_sigalgs(ssl, &verify_sigalgs);
520 for (size_t i = 0; i < num_verify_sigalgs; i++) {
521 if (sigalg == verify_sigalgs[i]) {
522 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800523 }
524 }
525
David Benjamin3ef76972016-10-17 17:59:54 -0400526 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
527 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
528 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800529}
530
531/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
532 * supported or doesn't appear in supported signature algorithms. Unlike
533 * ssl_cipher_get_disabled this applies to a specific session and not global
534 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500535void ssl_set_client_disabled(SSL *ssl) {
536 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800537 int have_rsa = 0, have_ecdsa = 0;
538 c->mask_a = 0;
539 c->mask_k = 0;
540
David Benjamin3ef76972016-10-17 17:59:54 -0400541 /* Now go through all signature algorithms seeing if we support any for RSA or
542 * ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin0fc37ef2016-08-17 15:29:46 -0400543 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -0400544 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
David Benjamin0fc37ef2016-08-17 15:29:46 -0400545 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -0400546 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400547 case SSL_SIGN_RSA_PSS_SHA512:
548 case SSL_SIGN_RSA_PSS_SHA384:
549 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400550 case SSL_SIGN_RSA_PKCS1_SHA512:
551 case SSL_SIGN_RSA_PKCS1_SHA384:
552 case SSL_SIGN_RSA_PKCS1_SHA256:
553 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800554 have_rsa = 1;
555 break;
556
Steven Valdez02563852016-06-23 13:33:05 -0400557 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
558 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
559 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
560 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 have_ecdsa = 1;
562 break;
563 }
564 }
565
566 /* Disable auth if we don't include any appropriate signature algorithms. */
567 if (!have_rsa) {
568 c->mask_a |= SSL_aRSA;
569 }
570 if (!have_ecdsa) {
571 c->mask_a |= SSL_aECDSA;
572 }
573
574 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500575 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 c->mask_a |= SSL_aPSK;
577 c->mask_k |= SSL_kPSK;
578 }
579}
Adam Langley95c29f32014-06-20 12:00:00 -0700580
Adam Langley614c66a2015-06-12 15:26:58 -0700581/* tls_extension represents a TLS extension that is handled internally. The
582 * |init| function is called for each handshake, before any other functions of
583 * the extension. Then the add and parse callbacks are called as needed.
584 *
585 * The parse callbacks receive a |CBS| that contains the contents of the
586 * extension (i.e. not including the type and length bytes). If an extension is
587 * not received then the parse callbacks will be called with a NULL CBS so that
588 * they can do any processing needed to handle the absence of an extension.
589 *
590 * The add callbacks receive a |CBB| to which the extension can be appended but
591 * the function is responsible for appending the type and length bytes too.
592 *
593 * All callbacks return one for success and zero for error. If a parse function
594 * returns zero then a fatal alert with value |*out_alert| will be sent. If
595 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
596struct tls_extension {
597 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500598 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700599
David Benjamin8c880a22016-12-03 02:20:34 -0500600 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
601 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
602 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700603
David Benjamin8c880a22016-12-03 02:20:34 -0500604 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
605 CBS *contents);
606 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700607};
608
David Benjamin8c880a22016-12-03 02:20:34 -0500609static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
610 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400611 if (contents != NULL) {
612 /* Servers MUST NOT send this extension. */
613 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
614 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
615 return 0;
616 }
617
618 return 1;
619}
620
David Benjamin8c880a22016-12-03 02:20:34 -0500621static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
622 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400623 /* This extension from the client is handled elsewhere. */
624 return 1;
625}
626
David Benjamin8c880a22016-12-03 02:20:34 -0500627static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400628 return 1;
629}
Adam Langley614c66a2015-06-12 15:26:58 -0700630
631/* Server name indication (SNI).
632 *
633 * https://tools.ietf.org/html/rfc6066#section-3. */
634
David Benjamin8c880a22016-12-03 02:20:34 -0500635static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
636 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700637 if (ssl->tlsext_hostname == NULL) {
638 return 1;
639 }
640
641 CBB contents, server_name_list, name;
642 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
643 !CBB_add_u16_length_prefixed(out, &contents) ||
644 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
645 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
646 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
647 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
648 strlen(ssl->tlsext_hostname)) ||
649 !CBB_flush(out)) {
650 return 0;
651 }
652
653 return 1;
654}
655
David Benjamin8c880a22016-12-03 02:20:34 -0500656static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500657 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500658 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700659 if (contents == NULL) {
660 return 1;
661 }
662
663 if (CBS_len(contents) != 0) {
664 return 0;
665 }
666
667 assert(ssl->tlsext_hostname != NULL);
668
Steven Valdez87eab492016-06-27 16:34:59 -0400669 if (ssl->session == NULL) {
670 assert(ssl->s3->new_session->tlsext_hostname == NULL);
671 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
672 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700673 *out_alert = SSL_AD_INTERNAL_ERROR;
674 return 0;
675 }
676 }
677
678 return 1;
679}
680
David Benjamin8c880a22016-12-03 02:20:34 -0500681static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500682 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700683 if (contents == NULL) {
684 return 1;
685 }
686
David Benjamin9b611e22016-03-03 08:48:30 -0500687 CBS server_name_list, host_name;
688 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700689 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500690 !CBS_get_u8(&server_name_list, &name_type) ||
691 /* Although the server_name extension was intended to be extensible to
692 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
693 * different name types will cause an error. Further, RFC 4366 originally
694 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
695 * adding new name types is no longer feasible.
696 *
697 * Act as if the extensibility does not exist to simplify parsing. */
698 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
699 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700700 CBS_len(contents) != 0) {
701 return 0;
702 }
703
David Benjamin9b611e22016-03-03 08:48:30 -0500704 if (name_type != TLSEXT_NAMETYPE_host_name ||
705 CBS_len(&host_name) == 0 ||
706 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
707 CBS_contains_zero_byte(&host_name)) {
708 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
709 return 0;
710 }
Adam Langley614c66a2015-06-12 15:26:58 -0700711
David Benjamin4eb95cc2016-11-16 17:08:23 +0900712 /* Copy the hostname as a string. */
David Benjamin8c880a22016-12-03 02:20:34 -0500713 if (!CBS_strdup(&host_name, &hs->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900714 *out_alert = SSL_AD_INTERNAL_ERROR;
715 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700716 }
717
David Benjamin8c880a22016-12-03 02:20:34 -0500718 hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700719 return 1;
720}
721
David Benjamin8c880a22016-12-03 02:20:34 -0500722static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
723 if (hs->ssl->s3->session_reused ||
724 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700725 return 1;
726 }
727
728 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
729 !CBB_add_u16(out, 0 /* length */)) {
730 return 0;
731 }
732
733 return 1;
734}
735
736
Adam Langley5021b222015-06-12 18:27:58 -0700737/* Renegotiation indication.
738 *
739 * https://tools.ietf.org/html/rfc5746 */
740
David Benjamin8c880a22016-12-03 02:20:34 -0500741static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
742 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400743 uint16_t min_version, max_version;
744 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
745 return 0;
746 }
747
748 /* Renegotiation indication is not necessary in TLS 1.3. */
749 if (min_version >= TLS1_3_VERSION) {
750 return 1;
751 }
752
David Benjamin52bf6902016-10-08 12:05:03 -0400753 assert(ssl->s3->initial_handshake_complete ==
754 (ssl->s3->previous_client_finished_len != 0));
755
Adam Langley5021b222015-06-12 18:27:58 -0700756 CBB contents, prev_finished;
757 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
758 !CBB_add_u16_length_prefixed(out, &contents) ||
759 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
760 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
761 ssl->s3->previous_client_finished_len) ||
762 !CBB_flush(out)) {
763 return 0;
764 }
765
766 return 1;
767}
768
David Benjamin8c880a22016-12-03 02:20:34 -0500769static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700770 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500771 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400772 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
773 return 0;
774 }
775
David Benjamin3e052de2015-11-25 20:10:31 -0500776 /* Servers may not switch between omitting the extension and supporting it.
777 * See RFC 5746, sections 3.5 and 4.2. */
778 if (ssl->s3->initial_handshake_complete &&
779 (contents != NULL) != ssl->s3->send_connection_binding) {
780 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
781 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
782 return 0;
783 }
784
Adam Langley5021b222015-06-12 18:27:58 -0700785 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500786 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700787 * RI even on initial ServerHello because the client doesn't see any
788 * renegotiation during an attack. However this would mean we could not
789 * connect to any server which doesn't support RI.
790 *
David Benjamine9cddb82015-11-23 14:36:40 -0500791 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
792 * practical terms every client sets it so it's just assumed here. */
793 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700794 }
795
796 const size_t expected_len = ssl->s3->previous_client_finished_len +
797 ssl->s3->previous_server_finished_len;
798
799 /* Check for logic errors */
800 assert(!expected_len || ssl->s3->previous_client_finished_len);
801 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400802 assert(ssl->s3->initial_handshake_complete ==
803 (ssl->s3->previous_client_finished_len != 0));
804 assert(ssl->s3->initial_handshake_complete ==
805 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700806
807 /* Parse out the extension contents. */
808 CBS renegotiated_connection;
809 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
810 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400811 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700812 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
813 return 0;
814 }
815
816 /* Check that the extension matches. */
817 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400818 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700819 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
820 return 0;
821 }
822
823 const uint8_t *d = CBS_data(&renegotiated_connection);
824 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
825 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400826 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700827 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
828 return 0;
829 }
830 d += ssl->s3->previous_client_finished_len;
831
832 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
833 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400834 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700835 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
836 return 0;
837 }
838 ssl->s3->send_connection_binding = 1;
839
840 return 1;
841}
842
David Benjamin8c880a22016-12-03 02:20:34 -0500843static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700844 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500845 SSL *const ssl = hs->ssl;
Adam Langley5021b222015-06-12 18:27:58 -0700846 /* Renegotiation isn't supported as a server so this function should never be
847 * called after the initial handshake. */
848 assert(!ssl->s3->initial_handshake_complete);
849
Steven Valdez143e8b32016-07-11 13:19:03 -0400850 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
851 return 1;
852 }
853
Adam Langley5021b222015-06-12 18:27:58 -0700854 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400855 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700856 }
857
858 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700859 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
860 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400861 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700862 return 0;
863 }
864
David Benjamin52bf6902016-10-08 12:05:03 -0400865 /* Check that the extension matches. We do not support renegotiation as a
866 * server, so this must be empty. */
867 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400868 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700869 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
870 return 0;
871 }
872
873 ssl->s3->send_connection_binding = 1;
874
875 return 1;
876}
877
David Benjamin8c880a22016-12-03 02:20:34 -0500878static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
879 SSL *const ssl = hs->ssl;
David Benjamin52bf6902016-10-08 12:05:03 -0400880 /* Renegotiation isn't supported as a server so this function should never be
881 * called after the initial handshake. */
882 assert(!ssl->s3->initial_handshake_complete);
883
Steven Valdez143e8b32016-07-11 13:19:03 -0400884 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
885 return 1;
886 }
887
Adam Langley5021b222015-06-12 18:27:58 -0700888 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400889 !CBB_add_u16(out, 1 /* length */) ||
890 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700891 return 0;
892 }
893
894 return 1;
895}
896
Adam Langley0a056712015-07-01 15:03:33 -0700897
898/* Extended Master Secret.
899 *
David Benjamin43946d42016-02-01 08:42:19 -0500900 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700901
David Benjamin8c880a22016-12-03 02:20:34 -0500902static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400903 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -0500904 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400905 return 0;
906 }
907
908 /* Extended master secret is not necessary in TLS 1.3. */
909 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700910 return 1;
911 }
912
913 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
914 !CBB_add_u16(out, 0 /* length */)) {
915 return 0;
916 }
917
918 return 1;
919}
920
David Benjamin8c880a22016-12-03 02:20:34 -0500921static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700922 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500923 SSL *const ssl = hs->ssl;
David Benjamin163c9562016-08-29 23:14:17 -0400924 /* Whether EMS is negotiated may not change on renegotation. */
925 if (ssl->s3->initial_handshake_complete) {
926 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
927 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
928 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
929 return 0;
930 }
931
932 return 1;
933 }
934
Adam Langley0a056712015-07-01 15:03:33 -0700935 if (contents == NULL) {
936 return 1;
937 }
938
Steven Valdez143e8b32016-07-11 13:19:03 -0400939 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
940 ssl->version == SSL3_VERSION) {
941 return 0;
942 }
943
944 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700945 return 0;
946 }
947
948 ssl->s3->tmp.extended_master_secret = 1;
949 return 1;
950}
951
David Benjamin8c880a22016-12-03 02:20:34 -0500952static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500953 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500954 SSL *const ssl = hs->ssl;
955 uint16_t version = ssl3_protocol_version(ssl);
956 if (version >= TLS1_3_VERSION ||
957 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400958 return 1;
959 }
960
961 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700962 return 1;
963 }
964
965 if (CBS_len(contents) != 0) {
966 return 0;
967 }
968
969 ssl->s3->tmp.extended_master_secret = 1;
970 return 1;
971}
972
David Benjamin8c880a22016-12-03 02:20:34 -0500973static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
974 if (!hs->ssl->s3->tmp.extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700975 return 1;
976 }
977
978 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
979 !CBB_add_u16(out, 0 /* length */)) {
980 return 0;
981 }
982
983 return 1;
984}
985
Adam Langley9b05bc52015-07-01 15:25:33 -0700986
987/* Session tickets.
988 *
989 * https://tools.ietf.org/html/rfc5077 */
990
David Benjamin8c880a22016-12-03 02:20:34 -0500991static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
992 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400993 uint16_t min_version, max_version;
994 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
995 return 0;
996 }
997
998 /* TLS 1.3 uses a different ticket extension. */
999 if (min_version >= TLS1_3_VERSION ||
1000 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001001 return 1;
1002 }
1003
1004 const uint8_t *ticket_data = NULL;
1005 int ticket_len = 0;
1006
1007 /* Renegotiation does not participate in session resumption. However, still
1008 * advertise the extension to avoid potentially breaking servers which carry
1009 * over the state from the previous handshake, such as OpenSSL servers
1010 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -04001011 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -07001012 if (!ssl->s3->initial_handshake_complete &&
1013 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001014 ssl->session->tlsext_tick != NULL &&
1015 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -04001016 ssl->method->version_from_wire(&session_version,
1017 ssl->session->ssl_version) &&
1018 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001019 ticket_data = ssl->session->tlsext_tick;
1020 ticket_len = ssl->session->tlsext_ticklen;
1021 }
1022
1023 CBB ticket;
1024 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1025 !CBB_add_u16_length_prefixed(out, &ticket) ||
1026 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1027 !CBB_flush(out)) {
1028 return 0;
1029 }
1030
1031 return 1;
1032}
1033
David Benjamin8c880a22016-12-03 02:20:34 -05001034static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -07001035 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001036 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -07001037 if (contents == NULL) {
1038 return 1;
1039 }
1040
Steven Valdez143e8b32016-07-11 13:19:03 -04001041 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1042 return 0;
1043 }
1044
Adam Langley9b05bc52015-07-01 15:25:33 -07001045 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1046 * this function should never be called, even if the server tries to send the
1047 * extension. */
1048 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1049
1050 if (CBS_len(contents) != 0) {
1051 return 0;
1052 }
1053
David Benjamin8c880a22016-12-03 02:20:34 -05001054 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001055 return 1;
1056}
1057
David Benjamin8c880a22016-12-03 02:20:34 -05001058static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1059 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001060 return 1;
1061 }
1062
David Benjamin78476f62016-11-12 11:20:55 +09001063 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin8c880a22016-12-03 02:20:34 -05001064 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001065
1066 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1067 !CBB_add_u16(out, 0 /* length */)) {
1068 return 0;
1069 }
1070
1071 return 1;
1072}
1073
1074
Adam Langley2e857bd2015-07-01 16:09:19 -07001075/* Signature Algorithms.
1076 *
1077 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1078
David Benjamin8c880a22016-12-03 02:20:34 -05001079static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1080 SSL *const ssl = hs->ssl;
David Benjamin2dc02042016-09-19 19:57:37 -04001081 uint16_t min_version, max_version;
1082 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1083 return 0;
1084 }
1085
1086 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001087 return 1;
1088 }
1089
David Benjamin0fc37ef2016-08-17 15:29:46 -04001090 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001091 const size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001092
David Benjamin0fc37ef2016-08-17 15:29:46 -04001093 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001094 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1095 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001096 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001097 return 0;
1098 }
1099
David Benjamin0fc37ef2016-08-17 15:29:46 -04001100 for (size_t i = 0; i < num_sigalgs; i++) {
1101 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001102 return 0;
1103 }
1104 }
1105
1106 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001107 return 0;
1108 }
1109
1110 return 1;
1111}
1112
David Benjamin8c880a22016-12-03 02:20:34 -05001113static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001114 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001115 OPENSSL_free(hs->peer_sigalgs);
1116 hs->peer_sigalgs = NULL;
1117 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001118
Adam Langley2e857bd2015-07-01 16:09:19 -07001119 if (contents == NULL) {
1120 return 1;
1121 }
1122
1123 CBS supported_signature_algorithms;
1124 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001125 CBS_len(contents) != 0 ||
1126 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001127 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001128 return 0;
1129 }
1130
1131 return 1;
1132}
1133
Adam Langley2e857bd2015-07-01 16:09:19 -07001134
Adam Langleybb0bd042015-07-01 16:21:03 -07001135/* OCSP Stapling.
1136 *
1137 * https://tools.ietf.org/html/rfc6066#section-8 */
1138
David Benjamin8c880a22016-12-03 02:20:34 -05001139static void ext_ocsp_init(SSL_HANDSHAKE *hs) {
1140 hs->ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001141}
1142
David Benjamin8c880a22016-12-03 02:20:34 -05001143static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1144 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001145 if (!ssl->ocsp_stapling_enabled) {
1146 return 1;
1147 }
1148
1149 CBB contents;
1150 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1151 !CBB_add_u16_length_prefixed(out, &contents) ||
1152 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1153 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1154 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1155 !CBB_flush(out)) {
1156 return 0;
1157 }
1158
Adam Langleyce9d85e2016-01-24 15:58:39 -08001159 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001160 return 1;
1161}
1162
David Benjamin8c880a22016-12-03 02:20:34 -05001163static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001164 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001165 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001166 if (contents == NULL) {
1167 return 1;
1168 }
1169
Steven Valdeza833c352016-11-01 13:39:36 -04001170 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1171 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001172 return 0;
1173 }
1174
Steven Valdeza833c352016-11-01 13:39:36 -04001175 /* OCSP stapling is forbidden on non-certificate ciphers. */
1176 if (CBS_len(contents) != 0 ||
1177 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001178 return 0;
1179 }
1180
Steven Valdeza833c352016-11-01 13:39:36 -04001181 /* Note this does not check for resumption in TLS 1.2. Sending
1182 * status_request here does not make sense, but OpenSSL does so and the
1183 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001184
David Benjamin8c880a22016-12-03 02:20:34 -05001185 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001186 return 1;
1187}
1188
David Benjamin8c880a22016-12-03 02:20:34 -05001189static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001190 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001191 if (contents == NULL) {
1192 return 1;
1193 }
1194
1195 uint8_t status_type;
1196 if (!CBS_get_u8(contents, &status_type)) {
1197 return 0;
1198 }
1199
1200 /* We cannot decide whether OCSP stapling will occur yet because the correct
1201 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001202 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001203
Adam Langleybb0bd042015-07-01 16:21:03 -07001204 return 1;
1205}
1206
David Benjamin8c880a22016-12-03 02:20:34 -05001207static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1208 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001209 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001210 !hs->ocsp_stapling_requested ||
Alessandro Ghedini559f0642016-12-07 12:55:32 +00001211 ssl->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001212 ssl->s3->session_reused ||
Steven Valdeza833c352016-11-01 13:39:36 -04001213 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001214 return 1;
1215 }
1216
David Benjamin8c880a22016-12-03 02:20:34 -05001217 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001218
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001219 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001220 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001221}
1222
1223
Adam Langley97dfcbf2015-07-01 18:35:20 -07001224/* Next protocol negotiation.
1225 *
1226 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1227
David Benjamin8c880a22016-12-03 02:20:34 -05001228static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1229 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001230 if (ssl->s3->initial_handshake_complete ||
1231 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001232 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001233 return 1;
1234 }
1235
1236 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1237 !CBB_add_u16(out, 0 /* length */)) {
1238 return 0;
1239 }
1240
1241 return 1;
1242}
1243
David Benjamin8c880a22016-12-03 02:20:34 -05001244static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001245 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001246 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001247 if (contents == NULL) {
1248 return 1;
1249 }
1250
Steven Valdez143e8b32016-07-11 13:19:03 -04001251 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1252 return 0;
1253 }
1254
Adam Langley97dfcbf2015-07-01 18:35:20 -07001255 /* If any of these are false then we should never have sent the NPN
1256 * extension in the ClientHello and thus this function should never have been
1257 * called. */
1258 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001259 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001260 assert(ssl->ctx->next_proto_select_cb != NULL);
1261
David Benjamin76c2efc2015-08-31 14:24:29 -04001262 if (ssl->s3->alpn_selected != NULL) {
1263 /* NPN and ALPN may not be negotiated in the same connection. */
1264 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1265 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1266 return 0;
1267 }
1268
Adam Langley97dfcbf2015-07-01 18:35:20 -07001269 const uint8_t *const orig_contents = CBS_data(contents);
1270 const size_t orig_len = CBS_len(contents);
1271
1272 while (CBS_len(contents) != 0) {
1273 CBS proto;
1274 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1275 CBS_len(&proto) == 0) {
1276 return 0;
1277 }
1278 }
1279
1280 uint8_t *selected;
1281 uint8_t selected_len;
1282 if (ssl->ctx->next_proto_select_cb(
1283 ssl, &selected, &selected_len, orig_contents, orig_len,
1284 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1285 *out_alert = SSL_AD_INTERNAL_ERROR;
1286 return 0;
1287 }
1288
David Benjamin79978df2015-12-25 15:56:49 -05001289 OPENSSL_free(ssl->s3->next_proto_negotiated);
1290 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1291 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001292 *out_alert = SSL_AD_INTERNAL_ERROR;
1293 return 0;
1294 }
1295
David Benjamin79978df2015-12-25 15:56:49 -05001296 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001297 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001298
1299 return 1;
1300}
1301
David Benjamin8c880a22016-12-03 02:20:34 -05001302static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001303 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001304 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001305 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1306 return 1;
1307 }
1308
Adam Langley97dfcbf2015-07-01 18:35:20 -07001309 if (contents != NULL && CBS_len(contents) != 0) {
1310 return 0;
1311 }
1312
1313 if (contents == NULL ||
1314 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001315 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001316 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001317 return 1;
1318 }
1319
David Benjamin8c880a22016-12-03 02:20:34 -05001320 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001321 return 1;
1322}
1323
David Benjamin8c880a22016-12-03 02:20:34 -05001324static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1325 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001326 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1327 * parsed. */
David Benjamin8c880a22016-12-03 02:20:34 -05001328 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001329 return 1;
1330 }
1331
1332 const uint8_t *npa;
1333 unsigned npa_len;
1334
1335 if (ssl->ctx->next_protos_advertised_cb(
1336 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1337 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001338 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001339 return 1;
1340 }
1341
1342 CBB contents;
1343 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1344 !CBB_add_u16_length_prefixed(out, &contents) ||
1345 !CBB_add_bytes(&contents, npa, npa_len) ||
1346 !CBB_flush(out)) {
1347 return 0;
1348 }
1349
1350 return 1;
1351}
1352
1353
Adam Langleyab8d87d2015-07-10 12:21:39 -07001354/* Signed certificate timestamps.
1355 *
1356 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1357
David Benjamin8c880a22016-12-03 02:20:34 -05001358static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1359 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001360 if (!ssl->signed_cert_timestamps_enabled) {
1361 return 1;
1362 }
1363
1364 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1365 !CBB_add_u16(out, 0 /* length */)) {
1366 return 0;
1367 }
1368
1369 return 1;
1370}
1371
David Benjamin8c880a22016-12-03 02:20:34 -05001372static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001373 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001374 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001375 if (contents == NULL) {
1376 return 1;
1377 }
1378
Steven Valdeza833c352016-11-01 13:39:36 -04001379 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1380 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001381 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001382 return 0;
1383 }
1384
Adam Langleyab8d87d2015-07-10 12:21:39 -07001385 /* If this is false then we should never have sent the SCT extension in the
1386 * ClientHello and thus this function should never have been called. */
1387 assert(ssl->signed_cert_timestamps_enabled);
1388
Adam Langleycfa08c32016-11-17 13:21:27 -08001389 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001390 *out_alert = SSL_AD_DECODE_ERROR;
1391 return 0;
1392 }
1393
David Benjamindaa88502016-10-04 16:32:16 -04001394 /* Session resumption uses the original session information. The extension
1395 * should not be sent on resumption, but RFC 6962 did not make it a
1396 * requirement, so tolerate this.
1397 *
1398 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001399 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001400 !CBS_stow(
1401 contents,
1402 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1403 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001404 *out_alert = SSL_AD_INTERNAL_ERROR;
1405 return 0;
1406 }
1407
1408 return 1;
1409}
1410
David Benjamin8c880a22016-12-03 02:20:34 -05001411static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001412 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001413 if (contents == NULL) {
1414 return 1;
1415 }
1416
1417 if (CBS_len(contents) != 0) {
1418 return 0;
1419 }
1420
David Benjamin8c880a22016-12-03 02:20:34 -05001421 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001422 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001423}
1424
David Benjamin8c880a22016-12-03 02:20:34 -05001425static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1426 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001427 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001428 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1429 ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001430 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001431 return 1;
1432 }
1433
1434 CBB contents;
1435 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1436 CBB_add_u16_length_prefixed(out, &contents) &&
1437 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1438 ssl->ctx->signed_cert_timestamp_list_length) &&
1439 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001440}
1441
1442
Adam Langleyf18e4532015-07-10 13:39:53 -07001443/* Application-level Protocol Negotiation.
1444 *
1445 * https://tools.ietf.org/html/rfc7301 */
1446
David Benjamin8c880a22016-12-03 02:20:34 -05001447static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1448 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001449 if (ssl->alpn_client_proto_list == NULL ||
1450 ssl->s3->initial_handshake_complete) {
1451 return 1;
1452 }
1453
1454 CBB contents, proto_list;
1455 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1456 !CBB_add_u16_length_prefixed(out, &contents) ||
1457 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1458 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1459 ssl->alpn_client_proto_list_len) ||
1460 !CBB_flush(out)) {
1461 return 0;
1462 }
1463
1464 return 1;
1465}
1466
David Benjamin8c880a22016-12-03 02:20:34 -05001467static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001468 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001469 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001470 if (contents == NULL) {
1471 return 1;
1472 }
1473
1474 assert(!ssl->s3->initial_handshake_complete);
1475 assert(ssl->alpn_client_proto_list != NULL);
1476
David Benjamin8c880a22016-12-03 02:20:34 -05001477 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001478 /* NPN and ALPN may not be negotiated in the same connection. */
1479 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1480 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1481 return 0;
1482 }
1483
Adam Langleyf18e4532015-07-10 13:39:53 -07001484 /* The extension data consists of a ProtocolNameList which must have
1485 * exactly one ProtocolName. Each of these is length-prefixed. */
1486 CBS protocol_name_list, protocol_name;
1487 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1488 CBS_len(contents) != 0 ||
1489 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1490 /* Empty protocol names are forbidden. */
1491 CBS_len(&protocol_name) == 0 ||
1492 CBS_len(&protocol_name_list) != 0) {
1493 return 0;
1494 }
1495
David Benjamin3e517572016-08-11 11:52:23 -04001496 /* Check that the protcol name is one of the ones we advertised. */
1497 int protocol_ok = 0;
1498 CBS client_protocol_name_list, client_protocol_name;
1499 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1500 ssl->alpn_client_proto_list_len);
1501 while (CBS_len(&client_protocol_name_list) > 0) {
1502 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1503 &client_protocol_name)) {
1504 *out_alert = SSL_AD_INTERNAL_ERROR;
1505 return 0;
1506 }
1507
1508 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
David Benjamin17cf2cb2016-12-13 01:07:13 -05001509 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1510 CBS_data(&protocol_name),
1511 CBS_len(&protocol_name)) == 0) {
David Benjamin3e517572016-08-11 11:52:23 -04001512 protocol_ok = 1;
1513 break;
1514 }
1515 }
1516
1517 if (!protocol_ok) {
1518 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1519 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1520 return 0;
1521 }
1522
Adam Langleyf18e4532015-07-10 13:39:53 -07001523 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1524 &ssl->s3->alpn_selected_len)) {
1525 *out_alert = SSL_AD_INTERNAL_ERROR;
1526 return 0;
1527 }
1528
1529 return 1;
1530}
1531
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001532int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001533 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001534 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001535 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001536 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001537 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001538 client_hello, &contents,
1539 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1540 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001541 return 1;
1542 }
1543
1544 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001545 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001546
1547 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001548 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1549 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001550 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001551 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1552 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001553 return 0;
1554 }
1555
1556 /* Validate the protocol list. */
1557 CBS protocol_name_list_copy = protocol_name_list;
1558 while (CBS_len(&protocol_name_list_copy) > 0) {
1559 CBS protocol_name;
1560
1561 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1562 /* Empty protocol names are forbidden. */
1563 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001564 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1565 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001566 return 0;
1567 }
1568 }
1569
1570 const uint8_t *selected;
1571 uint8_t selected_len;
1572 if (ssl->ctx->alpn_select_cb(
1573 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1574 CBS_len(&protocol_name_list),
1575 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1576 OPENSSL_free(ssl->s3->alpn_selected);
1577 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1578 if (ssl->s3->alpn_selected == NULL) {
1579 *out_alert = SSL_AD_INTERNAL_ERROR;
1580 return 0;
1581 }
1582 ssl->s3->alpn_selected_len = selected_len;
1583 }
1584
1585 return 1;
1586}
1587
David Benjamin8c880a22016-12-03 02:20:34 -05001588static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1589 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001590 if (ssl->s3->alpn_selected == NULL) {
1591 return 1;
1592 }
1593
1594 CBB contents, proto_list, proto;
1595 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1596 !CBB_add_u16_length_prefixed(out, &contents) ||
1597 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1598 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001599 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1600 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001601 !CBB_flush(out)) {
1602 return 0;
1603 }
1604
1605 return 1;
1606}
1607
1608
Adam Langley49c7af12015-07-10 14:33:46 -07001609/* Channel ID.
1610 *
1611 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1612
David Benjamin8c880a22016-12-03 02:20:34 -05001613static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1614 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001615}
1616
David Benjamin8c880a22016-12-03 02:20:34 -05001617static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1618 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001619 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001620 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001621 return 1;
1622 }
1623
1624 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1625 !CBB_add_u16(out, 0 /* length */)) {
1626 return 0;
1627 }
1628
1629 return 1;
1630}
1631
David Benjamin8c880a22016-12-03 02:20:34 -05001632static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1633 uint8_t *out_alert, CBS *contents) {
1634 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001635 if (contents == NULL) {
1636 return 1;
1637 }
1638
David Benjamince079fd2016-08-02 16:22:34 -04001639 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001640 assert(ssl->tlsext_channel_id_enabled);
1641
1642 if (CBS_len(contents) != 0) {
1643 return 0;
1644 }
1645
1646 ssl->s3->tlsext_channel_id_valid = 1;
1647 return 1;
1648}
1649
David Benjamin8c880a22016-12-03 02:20:34 -05001650static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1651 uint8_t *out_alert, CBS *contents) {
1652 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001653 if (contents == NULL ||
1654 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001655 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001656 return 1;
1657 }
1658
1659 if (CBS_len(contents) != 0) {
1660 return 0;
1661 }
1662
1663 ssl->s3->tlsext_channel_id_valid = 1;
1664 return 1;
1665}
1666
David Benjamin8c880a22016-12-03 02:20:34 -05001667static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1668 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001669 if (!ssl->s3->tlsext_channel_id_valid) {
1670 return 1;
1671 }
1672
1673 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1674 !CBB_add_u16(out, 0 /* length */)) {
1675 return 0;
1676 }
1677
1678 return 1;
1679}
1680
Adam Langley391250d2015-07-15 19:06:07 -07001681
1682/* Secure Real-time Transport Protocol (SRTP) extension.
1683 *
1684 * https://tools.ietf.org/html/rfc5764 */
1685
Adam Langley391250d2015-07-15 19:06:07 -07001686
David Benjamin8c880a22016-12-03 02:20:34 -05001687static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1688 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001689}
1690
David Benjamin8c880a22016-12-03 02:20:34 -05001691static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1692 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001693 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1694 if (profiles == NULL) {
1695 return 1;
1696 }
1697 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1698 if (num_profiles == 0) {
1699 return 1;
1700 }
1701
1702 CBB contents, profile_ids;
1703 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1704 !CBB_add_u16_length_prefixed(out, &contents) ||
1705 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1706 return 0;
1707 }
1708
David Benjamin54091232016-09-05 12:47:25 -04001709 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001710 if (!CBB_add_u16(&profile_ids,
1711 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1712 return 0;
1713 }
1714 }
1715
1716 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1717 !CBB_flush(out)) {
1718 return 0;
1719 }
1720
1721 return 1;
1722}
1723
David Benjamin8c880a22016-12-03 02:20:34 -05001724static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001725 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001726 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001727 if (contents == NULL) {
1728 return 1;
1729 }
1730
1731 /* The extension consists of a u16-prefixed profile ID list containing a
1732 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1733 *
1734 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1735 CBS profile_ids, srtp_mki;
1736 uint16_t profile_id;
1737 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1738 !CBS_get_u16(&profile_ids, &profile_id) ||
1739 CBS_len(&profile_ids) != 0 ||
1740 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1741 CBS_len(contents) != 0) {
1742 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1743 return 0;
1744 }
1745
1746 if (CBS_len(&srtp_mki) != 0) {
1747 /* Must be no MKI, since we never offer one. */
1748 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1749 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1750 return 0;
1751 }
1752
1753 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1754
1755 /* Check to see if the server gave us something we support (and presumably
1756 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001757 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001758 const SRTP_PROTECTION_PROFILE *profile =
1759 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1760
1761 if (profile->id == profile_id) {
1762 ssl->srtp_profile = profile;
1763 return 1;
1764 }
1765 }
1766
1767 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1768 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1769 return 0;
1770}
1771
David Benjamin8c880a22016-12-03 02:20:34 -05001772static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001773 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001774 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001775 if (contents == NULL) {
1776 return 1;
1777 }
1778
1779 CBS profile_ids, srtp_mki;
1780 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1781 CBS_len(&profile_ids) < 2 ||
1782 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1783 CBS_len(contents) != 0) {
1784 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1785 return 0;
1786 }
1787 /* Discard the MKI value for now. */
1788
1789 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1790 SSL_get_srtp_profiles(ssl);
1791
1792 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001793 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001794 const SRTP_PROTECTION_PROFILE *server_profile =
1795 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1796
1797 CBS profile_ids_tmp;
1798 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1799
1800 while (CBS_len(&profile_ids_tmp) > 0) {
1801 uint16_t profile_id;
1802 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1803 return 0;
1804 }
1805
1806 if (server_profile->id == profile_id) {
1807 ssl->srtp_profile = server_profile;
1808 return 1;
1809 }
1810 }
1811 }
1812
1813 return 1;
1814}
1815
David Benjamin8c880a22016-12-03 02:20:34 -05001816static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1817 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001818 if (ssl->srtp_profile == NULL) {
1819 return 1;
1820 }
1821
1822 CBB contents, profile_ids;
1823 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1824 !CBB_add_u16_length_prefixed(out, &contents) ||
1825 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1826 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1827 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1828 !CBB_flush(out)) {
1829 return 0;
1830 }
1831
1832 return 1;
1833}
1834
Adam Langleybdd5d662015-07-20 16:19:08 -07001835
1836/* EC point formats.
1837 *
1838 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1839
David Benjamin8c880a22016-12-03 02:20:34 -05001840static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001841 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001842 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1843 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001844 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1845 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001846 !CBB_flush(out)) {
1847 return 0;
1848 }
1849
1850 return 1;
1851}
1852
David Benjamin8c880a22016-12-03 02:20:34 -05001853static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001854 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -05001855 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin70aba262016-11-01 12:08:15 -04001856 return 0;
1857 }
1858
1859 /* The point format extension is unneccessary in TLS 1.3. */
1860 if (min_version >= TLS1_3_VERSION) {
1861 return 1;
1862 }
1863
David Benjamin8c880a22016-12-03 02:20:34 -05001864 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001865}
1866
David Benjamin8c880a22016-12-03 02:20:34 -05001867static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001868 CBS *contents) {
1869 if (contents == NULL) {
1870 return 1;
1871 }
1872
David Benjamin8c880a22016-12-03 02:20:34 -05001873 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001874 return 0;
1875 }
1876
Adam Langleybdd5d662015-07-20 16:19:08 -07001877 CBS ec_point_format_list;
1878 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1879 CBS_len(contents) != 0) {
1880 return 0;
1881 }
1882
David Benjaminfc059942015-07-30 23:01:59 -04001883 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1884 * point format. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05001885 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1886 TLSEXT_ECPOINTFORMAT_uncompressed,
1887 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001888 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001889 return 0;
1890 }
1891
1892 return 1;
1893}
1894
David Benjamin8c880a22016-12-03 02:20:34 -05001895static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001896 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001897 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001898 return 1;
1899 }
1900
David Benjamin8c880a22016-12-03 02:20:34 -05001901 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001902}
1903
David Benjamin8c880a22016-12-03 02:20:34 -05001904static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1905 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001906 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1907 return 1;
1908 }
1909
Adam Langleybdd5d662015-07-20 16:19:08 -07001910 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1911 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001912 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001913
1914 if (!using_ecc) {
1915 return 1;
1916 }
1917
David Benjamin8c880a22016-12-03 02:20:34 -05001918 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001919}
1920
Steven Valdeza833c352016-11-01 13:39:36 -04001921
Steven Valdez4aa154e2016-07-29 14:32:55 -04001922/* Pre Shared Key
1923 *
Steven Valdeza833c352016-11-01 13:39:36 -04001924 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001925
David Benjamin8c880a22016-12-03 02:20:34 -05001926static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1927 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001928 uint16_t min_version, max_version;
1929 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1930 return 0;
1931 }
1932
1933 uint16_t session_version;
1934 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1935 !ssl->method->version_from_wire(&session_version,
1936 ssl->session->ssl_version) ||
1937 session_version < TLS1_3_VERSION) {
1938 return 0;
1939 }
1940
1941 const EVP_MD *digest =
1942 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1943 size_t binder_len = EVP_MD_size(digest);
1944 return 15 + ssl->session->tlsext_ticklen + binder_len;
1945}
1946
David Benjamin8c880a22016-12-03 02:20:34 -05001947static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1948 SSL *const ssl = hs->ssl;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001949 uint16_t min_version, max_version;
1950 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1951 return 0;
1952 }
1953
David Benjamin2dc02042016-09-19 19:57:37 -04001954 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001955 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001956 !ssl->method->version_from_wire(&session_version,
1957 ssl->session->ssl_version) ||
1958 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001959 return 1;
1960 }
1961
Steven Valdeza833c352016-11-01 13:39:36 -04001962 struct timeval now;
1963 ssl_get_current_time(ssl, &now);
1964 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1965 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1966
1967 /* Fill in a placeholder zero binder of the appropriate length. It will be
1968 * computed and filled in later after length prefixes are computed. */
1969 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
1970 const EVP_MD *digest =
1971 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1972 size_t binder_len = EVP_MD_size(digest);
1973
1974 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001975 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1976 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001977 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001978 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1979 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001980 ssl->session->tlsext_ticklen) ||
1981 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1982 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1983 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1984 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001985 return 0;
1986 }
1987
David Benjamin8c880a22016-12-03 02:20:34 -05001988 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001989 return CBB_flush(out);
1990}
1991
David Benjamin8baf9632016-11-17 17:11:16 +09001992int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1993 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001994 CBS *contents) {
1995 uint16_t psk_id;
1996 if (!CBS_get_u16(contents, &psk_id) ||
1997 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001998 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001999 *out_alert = SSL_AD_DECODE_ERROR;
2000 return 0;
2001 }
2002
David Benjamineab773a2016-11-09 18:32:35 -05002003 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04002004 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04002005 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002006 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2007 return 0;
2008 }
2009
2010 return 1;
2011}
2012
David Benjamin8baf9632016-11-17 17:11:16 +09002013int ssl_ext_pre_shared_key_parse_clienthello(SSL_HANDSHAKE *hs,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002014 SSL_SESSION **out_session,
Steven Valdeza833c352016-11-01 13:39:36 -04002015 CBS *out_binders,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002016 uint8_t *out_alert,
2017 CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09002018 SSL *const ssl = hs->ssl;
Steven Valdez5b986082016-09-01 12:29:49 -04002019 /* We only process the first PSK identity since we don't support pure PSK. */
Steven Valdeza833c352016-11-01 13:39:36 -04002020 uint32_t obfuscated_ticket_age;
David Benjaminaedf3032016-12-01 16:47:56 -05002021 CBS identities, ticket, binders;
2022 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2023 !CBS_get_u16_length_prefixed(&identities, &ticket) ||
2024 !CBS_get_u32(&identities, &obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04002025 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05002026 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04002027 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04002028 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002029 *out_alert = SSL_AD_DECODE_ERROR;
2030 return 0;
2031 }
2032
Steven Valdeza833c352016-11-01 13:39:36 -04002033 *out_binders = binders;
2034
David Benjaminaedf3032016-12-01 16:47:56 -05002035 /* Check the syntax of the remaining identities, but do not process them. */
2036 size_t num_identities = 1;
2037 while (CBS_len(&identities) != 0) {
2038 CBS unused_ticket;
2039 uint32_t unused_obfuscated_ticket_age;
2040 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
2041 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
2042 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2043 *out_alert = SSL_AD_DECODE_ERROR;
2044 return 0;
2045 }
2046
2047 num_identities++;
2048 }
2049
2050 /* Check the syntax of the binders. The value will be checked later if
2051 * resuming. */
2052 size_t num_binders = 0;
2053 while (CBS_len(&binders) != 0) {
2054 CBS binder;
2055 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
2056 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2057 *out_alert = SSL_AD_DECODE_ERROR;
2058 return 0;
2059 }
2060
2061 num_binders++;
2062 }
2063
2064 if (num_identities != num_binders) {
2065 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
2066 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04002067 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04002068 }
2069
Steven Valdeza833c352016-11-01 13:39:36 -04002070 /* TODO(svaldez): Check that the ticket_age is valid when attempting to use
2071 * the PSK for 0-RTT. http://crbug.com/boringssl/113 */
2072
Steven Valdez4aa154e2016-07-29 14:32:55 -04002073 /* TLS 1.3 session tickets are renewed separately as part of the
2074 * NewSessionTicket. */
David Benjamine7f60a22016-11-16 18:54:25 +09002075 int unused_renew;
2076 if (!tls_process_ticket(ssl, out_session, &unused_renew, CBS_data(&ticket),
2077 CBS_len(&ticket), NULL, 0)) {
2078 *out_alert = SSL_AD_INTERNAL_ERROR;
2079 return 0;
2080 }
2081
2082 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002083}
2084
David Benjamin8baf9632016-11-17 17:11:16 +09002085int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2086 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002087 return 1;
2088 }
2089
2090 CBB contents;
2091 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2092 !CBB_add_u16_length_prefixed(out, &contents) ||
2093 /* We only consider the first identity for resumption */
2094 !CBB_add_u16(&contents, 0) ||
2095 !CBB_flush(out)) {
2096 return 0;
2097 }
2098
2099 return 1;
2100}
2101
2102
Steven Valdeza833c352016-11-01 13:39:36 -04002103/* Pre-Shared Key Exchange Modes
2104 *
2105 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002106
David Benjamin8c880a22016-12-03 02:20:34 -05002107static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2108 CBB *out) {
2109 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04002110 uint16_t min_version, max_version;
2111 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2112 return 0;
2113 }
2114
2115 if (max_version < TLS1_3_VERSION) {
2116 return 1;
2117 }
2118
2119 CBB contents, ke_modes;
2120 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2121 !CBB_add_u16_length_prefixed(out, &contents) ||
2122 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2123 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2124 return 0;
2125 }
2126
2127 return CBB_flush(out);
2128}
2129
David Benjamin8c880a22016-12-03 02:20:34 -05002130static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002131 uint8_t *out_alert,
2132 CBS *contents) {
2133 if (contents == NULL) {
2134 return 1;
2135 }
2136
Steven Valdeza833c352016-11-01 13:39:36 -04002137 CBS ke_modes;
2138 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2139 CBS_len(&ke_modes) == 0 ||
2140 CBS_len(contents) != 0) {
2141 *out_alert = SSL_AD_DECODE_ERROR;
2142 return 0;
2143 }
2144
2145 /* We only support tickets with PSK_DHE_KE. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05002146 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2147 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002148
2149 return 1;
2150}
2151
2152
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002153/* Early Data Indication
2154 *
2155 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2156
David Benjamin8c880a22016-12-03 02:20:34 -05002157static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002158 /* TODO(svaldez): Support 0RTT. */
2159 return 1;
2160}
2161
David Benjamin8c880a22016-12-03 02:20:34 -05002162static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2163 uint8_t *out_alert, CBS *contents) {
2164 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002165 if (contents == NULL) {
2166 return 1;
2167 }
2168
2169 if (CBS_len(contents) != 0) {
2170 *out_alert = SSL_AD_DECODE_ERROR;
2171 return 0;
2172 }
2173
2174 /* Since we don't currently accept 0-RTT, we have to skip past any early data
2175 * the client might have sent. */
2176 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
2177 ssl->s3->skip_early_data = 1;
2178 }
2179 return 1;
2180}
2181
2182
Steven Valdez143e8b32016-07-11 13:19:03 -04002183/* Key Share
2184 *
David Benjamina128a552016-10-13 14:26:33 -04002185 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002186
David Benjamin8c880a22016-12-03 02:20:34 -05002187static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2188 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002189 uint16_t min_version, max_version;
2190 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2191 return 0;
2192 }
2193
David Benjamin53a2dfc2016-10-07 21:04:12 -04002194 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002195 return 1;
2196 }
2197
2198 CBB contents, kse_bytes;
2199 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2200 !CBB_add_u16_length_prefixed(out, &contents) ||
2201 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2202 return 0;
2203 }
2204
David Benjamin8c880a22016-12-03 02:20:34 -05002205 uint16_t group_id = hs->retry_group;
2206 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002207 /* We received a HelloRetryRequest without a new curve, so there is no new
2208 * share to append. Leave |ecdh_ctx| as-is. */
2209 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002210 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2211 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002212 return 0;
2213 }
David Benjamin8c880a22016-12-03 02:20:34 -05002214 OPENSSL_free(hs->key_share_bytes);
2215 hs->key_share_bytes = NULL;
2216 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002217 if (group_id == 0) {
2218 return CBB_flush(out);
2219 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002220 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002221 /* Add a fake group. See draft-davidben-tls-grease-01. */
2222 if (ssl->ctx->grease_enabled &&
2223 (!CBB_add_u16(&kse_bytes,
2224 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2225 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2226 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2227 return 0;
2228 }
2229
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002230 /* Predict the most preferred group. */
2231 const uint16_t *groups;
2232 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002233 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002234 if (groups_len == 0) {
2235 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2236 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002237 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002238
2239 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002240 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002241
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002242 CBB key_exchange;
2243 if (!CBB_add_u16(&kse_bytes, group_id) ||
2244 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002245 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2246 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002247 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002248 return 0;
2249 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002250
David Benjamin8c880a22016-12-03 02:20:34 -05002251 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002252 /* Save the contents of the extension to repeat it in the second
2253 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002254 hs->key_share_bytes_len = CBB_len(&kse_bytes);
2255 hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
2256 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002257 return 0;
2258 }
2259 }
2260
Steven Valdez143e8b32016-07-11 13:19:03 -04002261 return CBB_flush(out);
2262}
2263
David Benjamin8baf9632016-11-17 17:11:16 +09002264int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002265 size_t *out_secret_len,
2266 uint8_t *out_alert, CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09002267 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002268 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002269 uint16_t group_id;
2270 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002271 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2272 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002273 *out_alert = SSL_AD_DECODE_ERROR;
2274 return 0;
2275 }
2276
David Benjamin8baf9632016-11-17 17:11:16 +09002277 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002278 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2279 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2280 return 0;
2281 }
2282
David Benjamin8baf9632016-11-17 17:11:16 +09002283 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2284 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002285 *out_alert = SSL_AD_INTERNAL_ERROR;
2286 return 0;
2287 }
2288
David Benjamin4882a6c2016-12-11 02:48:12 -05002289 ssl->s3->new_session->group_id = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002290 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002291 return 1;
2292}
2293
David Benjamin8baf9632016-11-17 17:11:16 +09002294int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002295 uint8_t **out_secret,
2296 size_t *out_secret_len,
2297 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002298 uint16_t group_id;
2299 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002300 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002301 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2302 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2303 return 0;
2304 }
2305
2306 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002307 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002308 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002309 return 0;
2310 }
2311
David Benjamin7e1f9842016-09-20 19:24:40 -04002312 /* Find the corresponding key share. */
2313 int found = 0;
2314 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002315 while (CBS_len(&key_shares) > 0) {
2316 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002317 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002318 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002319 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2320 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002321 return 0;
2322 }
2323
David Benjamin7e1f9842016-09-20 19:24:40 -04002324 if (id == group_id) {
2325 if (found) {
2326 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2327 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2328 return 0;
2329 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002330
David Benjamin7e1f9842016-09-20 19:24:40 -04002331 found = 1;
2332 peer_key = peer_key_tmp;
2333 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002334 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002335 }
2336
David Benjamin7e1f9842016-09-20 19:24:40 -04002337 if (!found) {
2338 *out_found = 0;
2339 *out_secret = NULL;
2340 *out_secret_len = 0;
2341 return 1;
2342 }
2343
2344 /* Compute the DH secret. */
2345 uint8_t *secret = NULL;
2346 size_t secret_len;
2347 SSL_ECDH_CTX group;
David Benjamin17cf2cb2016-12-13 01:07:13 -05002348 OPENSSL_memset(&group, 0, sizeof(SSL_ECDH_CTX));
David Benjamin7e1f9842016-09-20 19:24:40 -04002349 CBB public_key;
2350 if (!CBB_init(&public_key, 32) ||
2351 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002352 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
2353 CBS_data(&peer_key), CBS_len(&peer_key)) ||
2354 !CBB_finish(&public_key, &hs->public_key, &hs->public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002355 OPENSSL_free(secret);
2356 SSL_ECDH_CTX_cleanup(&group);
2357 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002358 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002359 return 0;
2360 }
2361
2362 SSL_ECDH_CTX_cleanup(&group);
2363
2364 *out_secret = secret;
2365 *out_secret_len = secret_len;
2366 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002367 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002368}
2369
David Benjamin8baf9632016-11-17 17:11:16 +09002370int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2371 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002372 uint16_t group_id;
2373 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002374 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002375 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2376 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2377 !CBB_add_u16(&kse_bytes, group_id) ||
2378 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002379 !CBB_add_bytes(&public_key, hs->public_key, hs->public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002380 !CBB_flush(out)) {
2381 return 0;
2382 }
2383
David Benjamin8baf9632016-11-17 17:11:16 +09002384 OPENSSL_free(hs->public_key);
2385 hs->public_key = NULL;
2386 hs->public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002387
David Benjamin4882a6c2016-12-11 02:48:12 -05002388 ssl->s3->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002389 return 1;
2390}
2391
2392
Steven Valdezfdd10992016-09-15 16:27:05 -04002393/* Supported Versions
2394 *
2395 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2396
David Benjamin8c880a22016-12-03 02:20:34 -05002397static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2398 SSL *const ssl = hs->ssl;
Steven Valdezfdd10992016-09-15 16:27:05 -04002399 uint16_t min_version, max_version;
2400 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2401 return 0;
2402 }
2403
2404 if (max_version <= TLS1_2_VERSION) {
2405 return 1;
2406 }
2407
2408 CBB contents, versions;
2409 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2410 !CBB_add_u16_length_prefixed(out, &contents) ||
2411 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2412 return 0;
2413 }
2414
David Benjamind9791bf2016-09-27 16:39:52 -04002415 /* Add a fake version. See draft-davidben-tls-grease-01. */
2416 if (ssl->ctx->grease_enabled &&
2417 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2418 return 0;
2419 }
2420
Steven Valdezfdd10992016-09-15 16:27:05 -04002421 for (uint16_t version = max_version; version >= min_version; version--) {
2422 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2423 return 0;
2424 }
2425 }
2426
2427 if (!CBB_flush(out)) {
2428 return 0;
2429 }
2430
2431 return 1;
2432}
2433
2434
David Benjamin3baa6e12016-10-07 21:10:38 -04002435/* Cookie
2436 *
2437 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2438
David Benjamin8c880a22016-12-03 02:20:34 -05002439static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2440 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002441 return 1;
2442 }
2443
2444 CBB contents, cookie;
2445 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2446 !CBB_add_u16_length_prefixed(out, &contents) ||
2447 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002448 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002449 !CBB_flush(out)) {
2450 return 0;
2451 }
2452
2453 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002454 OPENSSL_free(hs->cookie);
2455 hs->cookie = NULL;
2456 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002457 return 1;
2458}
2459
2460
Steven Valdezce902a92016-05-17 11:47:53 -04002461/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002462 *
Steven Valdezce902a92016-05-17 11:47:53 -04002463 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002464 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002465
David Benjamin8c880a22016-12-03 02:20:34 -05002466static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2467 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002468 CBB contents, groups_bytes;
2469 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002470 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002471 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002472 return 0;
2473 }
2474
David Benjamin65ac9972016-09-02 21:35:25 -04002475 /* Add a fake group. See draft-davidben-tls-grease-01. */
2476 if (ssl->ctx->grease_enabled &&
2477 !CBB_add_u16(&groups_bytes,
2478 ssl_get_grease_value(ssl, ssl_grease_group))) {
2479 return 0;
2480 }
2481
Steven Valdezce902a92016-05-17 11:47:53 -04002482 const uint16_t *groups;
2483 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002484 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002485
David Benjamin54091232016-09-05 12:47:25 -04002486 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002487 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002488 return 0;
2489 }
2490 }
2491
2492 return CBB_flush(out);
2493}
2494
David Benjamin8c880a22016-12-03 02:20:34 -05002495static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2496 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002497 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002498 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2499 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002500 return 1;
2501}
2502
David Benjamin8c880a22016-12-03 02:20:34 -05002503static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2504 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002505 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002506 if (contents == NULL) {
2507 return 1;
2508 }
2509
Steven Valdezce902a92016-05-17 11:47:53 -04002510 CBS supported_group_list;
2511 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2512 CBS_len(&supported_group_list) == 0 ||
2513 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002514 CBS_len(contents) != 0) {
2515 return 0;
2516 }
2517
David Benjamin8c880a22016-12-03 02:20:34 -05002518 hs->peer_supported_group_list =
2519 OPENSSL_malloc(CBS_len(&supported_group_list));
2520 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002521 *out_alert = SSL_AD_INTERNAL_ERROR;
2522 return 0;
2523 }
2524
Steven Valdezce902a92016-05-17 11:47:53 -04002525 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002526 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002527 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002528 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002529 goto err;
2530 }
2531 }
2532
Steven Valdezce902a92016-05-17 11:47:53 -04002533 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002534 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002535
2536 return 1;
2537
2538err:
David Benjamin8c880a22016-12-03 02:20:34 -05002539 OPENSSL_free(hs->peer_supported_group_list);
2540 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002541 *out_alert = SSL_AD_INTERNAL_ERROR;
2542 return 0;
2543}
2544
David Benjamin8c880a22016-12-03 02:20:34 -05002545static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002546 /* Servers don't echo this extension. */
2547 return 1;
2548}
2549
2550
Adam Langley614c66a2015-06-12 15:26:58 -07002551/* kExtensions contains all the supported extensions. */
2552static const struct tls_extension kExtensions[] = {
2553 {
Adam Langley5021b222015-06-12 18:27:58 -07002554 TLSEXT_TYPE_renegotiate,
2555 NULL,
2556 ext_ri_add_clienthello,
2557 ext_ri_parse_serverhello,
2558 ext_ri_parse_clienthello,
2559 ext_ri_add_serverhello,
2560 },
2561 {
Adam Langley614c66a2015-06-12 15:26:58 -07002562 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002563 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002564 ext_sni_add_clienthello,
2565 ext_sni_parse_serverhello,
2566 ext_sni_parse_clienthello,
2567 ext_sni_add_serverhello,
2568 },
Adam Langley0a056712015-07-01 15:03:33 -07002569 {
2570 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002571 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002572 ext_ems_add_clienthello,
2573 ext_ems_parse_serverhello,
2574 ext_ems_parse_clienthello,
2575 ext_ems_add_serverhello,
2576 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002577 {
2578 TLSEXT_TYPE_session_ticket,
2579 NULL,
2580 ext_ticket_add_clienthello,
2581 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002582 /* Ticket extension client parsing is handled in ssl_session.c */
2583 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002584 ext_ticket_add_serverhello,
2585 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002586 {
2587 TLSEXT_TYPE_signature_algorithms,
2588 NULL,
2589 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002590 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002591 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002592 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002593 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002594 {
2595 TLSEXT_TYPE_status_request,
2596 ext_ocsp_init,
2597 ext_ocsp_add_clienthello,
2598 ext_ocsp_parse_serverhello,
2599 ext_ocsp_parse_clienthello,
2600 ext_ocsp_add_serverhello,
2601 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002602 {
2603 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002604 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002605 ext_npn_add_clienthello,
2606 ext_npn_parse_serverhello,
2607 ext_npn_parse_clienthello,
2608 ext_npn_add_serverhello,
2609 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002610 {
2611 TLSEXT_TYPE_certificate_timestamp,
2612 NULL,
2613 ext_sct_add_clienthello,
2614 ext_sct_parse_serverhello,
2615 ext_sct_parse_clienthello,
2616 ext_sct_add_serverhello,
2617 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002618 {
2619 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002620 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002621 ext_alpn_add_clienthello,
2622 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002623 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2624 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002625 ext_alpn_add_serverhello,
2626 },
Adam Langley49c7af12015-07-10 14:33:46 -07002627 {
2628 TLSEXT_TYPE_channel_id,
2629 ext_channel_id_init,
2630 ext_channel_id_add_clienthello,
2631 ext_channel_id_parse_serverhello,
2632 ext_channel_id_parse_clienthello,
2633 ext_channel_id_add_serverhello,
2634 },
Adam Langley391250d2015-07-15 19:06:07 -07002635 {
2636 TLSEXT_TYPE_srtp,
2637 ext_srtp_init,
2638 ext_srtp_add_clienthello,
2639 ext_srtp_parse_serverhello,
2640 ext_srtp_parse_clienthello,
2641 ext_srtp_add_serverhello,
2642 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002643 {
2644 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002645 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002646 ext_ec_point_add_clienthello,
2647 ext_ec_point_parse_serverhello,
2648 ext_ec_point_parse_clienthello,
2649 ext_ec_point_add_serverhello,
2650 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002651 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002652 TLSEXT_TYPE_key_share,
2653 NULL,
2654 ext_key_share_add_clienthello,
2655 forbid_parse_serverhello,
2656 ignore_parse_clienthello,
2657 dont_add_serverhello,
2658 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002659 {
Steven Valdeza833c352016-11-01 13:39:36 -04002660 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002661 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002662 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002663 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002664 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002665 dont_add_serverhello,
2666 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002667 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002668 TLSEXT_TYPE_early_data,
2669 NULL,
2670 ext_early_data_add_clienthello,
2671 forbid_parse_serverhello,
2672 ext_early_data_parse_clienthello,
2673 dont_add_serverhello,
2674 },
2675 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002676 TLSEXT_TYPE_supported_versions,
2677 NULL,
2678 ext_supported_versions_add_clienthello,
2679 forbid_parse_serverhello,
2680 ignore_parse_clienthello,
2681 dont_add_serverhello,
2682 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002683 {
2684 TLSEXT_TYPE_cookie,
2685 NULL,
2686 ext_cookie_add_clienthello,
2687 forbid_parse_serverhello,
2688 ignore_parse_clienthello,
2689 dont_add_serverhello,
2690 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002691 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2692 * intolerant to the last extension being zero-length. See
2693 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002694 {
Steven Valdezce902a92016-05-17 11:47:53 -04002695 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002696 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002697 ext_supported_groups_add_clienthello,
2698 ext_supported_groups_parse_serverhello,
2699 ext_supported_groups_parse_clienthello,
2700 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002701 },
Adam Langley614c66a2015-06-12 15:26:58 -07002702};
2703
2704#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2705
Adam Langley4cfa96b2015-07-01 11:56:55 -07002706OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002707 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002708 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002709OPENSSL_COMPILE_ASSERT(
2710 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2711 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002712
Adam Langley614c66a2015-06-12 15:26:58 -07002713static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2714 uint16_t value) {
2715 unsigned i;
2716 for (i = 0; i < kNumExtensions; i++) {
2717 if (kExtensions[i].value == value) {
2718 *out_index = i;
2719 return &kExtensions[i];
2720 }
2721 }
2722
2723 return NULL;
2724}
2725
Adam Langley09505632015-07-30 18:10:13 -07002726int SSL_extension_supported(unsigned extension_value) {
2727 uint32_t index;
2728 return extension_value == TLSEXT_TYPE_padding ||
2729 tls_extension_find(&index, extension_value) != NULL;
2730}
2731
David Benjamin8c880a22016-12-03 02:20:34 -05002732int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2733 SSL *const ssl = hs->ssl;
David Benjaminf04c2e92016-12-06 13:35:25 -05002734 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2735 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002736 !ssl->s3->send_connection_binding) {
2737 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002738 }
Adam Langley95c29f32014-06-20 12:00:00 -07002739
David Benjamine8d53502015-10-10 14:13:23 -04002740 CBB extensions;
2741 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002742 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002743 }
Adam Langley95c29f32014-06-20 12:00:00 -07002744
David Benjamin8c880a22016-12-03 02:20:34 -05002745 hs->extensions.sent = 0;
2746 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002747
David Benjamin54091232016-09-05 12:47:25 -04002748 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002749 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002750 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002751 }
2752 }
Adam Langley95c29f32014-06-20 12:00:00 -07002753
David Benjamin65ac9972016-09-02 21:35:25 -04002754 uint16_t grease_ext1 = 0;
2755 if (ssl->ctx->grease_enabled) {
2756 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2757 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2758 if (!CBB_add_u16(&extensions, grease_ext1) ||
2759 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2760 goto err;
2761 }
2762 }
2763
David Benjamin54091232016-09-05 12:47:25 -04002764 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002765 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002766 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002767 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002768 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002769 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002770 }
Adam Langley95c29f32014-06-20 12:00:00 -07002771
Adam Langley33ad2b52015-07-20 17:43:53 -07002772 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002773 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002774 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002775 }
Adam Langley75712922014-10-10 16:23:43 -07002776
David Benjamin2bd19172016-11-17 16:47:15 +09002777 if (!custom_ext_add_clienthello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002778 goto err;
2779 }
2780
David Benjamin65ac9972016-09-02 21:35:25 -04002781 if (ssl->ctx->grease_enabled) {
2782 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2783 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2784
2785 /* The two fake extensions must not have the same value. GREASE values are
2786 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2787 * one. */
2788 if (grease_ext1 == grease_ext2) {
2789 grease_ext2 ^= 0x1010;
2790 }
2791
2792 if (!CBB_add_u16(&extensions, grease_ext2) ||
2793 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2794 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2795 goto err;
2796 }
2797 }
2798
David Benjamince079fd2016-08-02 16:22:34 -04002799 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002800 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002801 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002802 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002803 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002804 *
2805 * NB: because this code works out the length of all existing extensions
2806 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002807 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002808 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002809 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002810 * Server 7.0 is intolerant to the last extension being zero-length. See
2811 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002812 if (padding_len >= 4 + 1) {
2813 padding_len -= 4;
2814 } else {
2815 padding_len = 1;
2816 }
Adam Langley95c29f32014-06-20 12:00:00 -07002817
Adam Langley33ad2b52015-07-20 17:43:53 -07002818 uint8_t *padding_bytes;
2819 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2820 !CBB_add_u16(&extensions, padding_len) ||
2821 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2822 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002823 }
Adam Langley75712922014-10-10 16:23:43 -07002824
David Benjamin17cf2cb2016-12-13 01:07:13 -05002825 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002826 }
2827 }
Adam Langley75712922014-10-10 16:23:43 -07002828
Steven Valdeza833c352016-11-01 13:39:36 -04002829 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002830 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002831 goto err;
2832 }
2833
David Benjamina01deee2015-12-08 18:56:31 -05002834 /* Discard empty extensions blocks. */
2835 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002836 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002837 }
2838
David Benjamine8d53502015-10-10 14:13:23 -04002839 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002840
2841err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002842 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002843 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002844}
Adam Langley95c29f32014-06-20 12:00:00 -07002845
David Benjamin8c880a22016-12-03 02:20:34 -05002846int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2847 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002848 CBB extensions;
2849 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002850 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002851 }
2852
David Benjamin8c880a22016-12-03 02:20:34 -05002853 for (unsigned i = 0; i < kNumExtensions; i++) {
2854 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002855 /* Don't send extensions that were not received. */
2856 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002857 }
Adam Langley95c29f32014-06-20 12:00:00 -07002858
David Benjamin8c880a22016-12-03 02:20:34 -05002859 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002860 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002861 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002862 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002863 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002864 }
Adam Langley95c29f32014-06-20 12:00:00 -07002865
David Benjamin2bd19172016-11-17 16:47:15 +09002866 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002867 goto err;
2868 }
2869
Steven Valdez143e8b32016-07-11 13:19:03 -04002870 /* Discard empty extensions blocks before TLS 1.3. */
2871 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2872 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002873 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002874 }
2875
David Benjamin56380462015-10-10 14:59:09 -04002876 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002877
2878err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002879 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002880 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002881}
Adam Langley95c29f32014-06-20 12:00:00 -07002882
David Benjamin731058e2016-12-03 23:15:13 -05002883static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2884 const SSL_CLIENT_HELLO *client_hello,
2885 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002886 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002887 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002888 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002889 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002890 }
2891 }
2892
David Benjamin8c880a22016-12-03 02:20:34 -05002893 hs->extensions.received = 0;
2894 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002895
David Benjamine14ff062016-08-09 16:21:24 -04002896 CBS extensions;
2897 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2898 while (CBS_len(&extensions) != 0) {
2899 uint16_t type;
2900 CBS extension;
2901
2902 /* Decode the next extension. */
2903 if (!CBS_get_u16(&extensions, &type) ||
2904 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002905 *out_alert = SSL_AD_DECODE_ERROR;
2906 return 0;
2907 }
Adam Langley95c29f32014-06-20 12:00:00 -07002908
David Benjamine14ff062016-08-09 16:21:24 -04002909 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2910 * ambiguous. Ignore all but the renegotiation_info extension. */
2911 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2912 continue;
2913 }
Adam Langley95c29f32014-06-20 12:00:00 -07002914
David Benjamine14ff062016-08-09 16:21:24 -04002915 unsigned ext_index;
2916 const struct tls_extension *const ext =
2917 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002918
David Benjamine14ff062016-08-09 16:21:24 -04002919 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002920 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002921 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002922 return 0;
2923 }
David Benjamine14ff062016-08-09 16:21:24 -04002924 continue;
2925 }
2926
David Benjamin8c880a22016-12-03 02:20:34 -05002927 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002928 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002929 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002930 *out_alert = alert;
2931 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002932 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002933 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002934 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002935 }
Adam Langley75712922014-10-10 16:23:43 -07002936
David Benjamin1deb41b2016-08-09 19:36:38 -04002937 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002938 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002939 continue;
2940 }
2941
2942 CBS *contents = NULL, fake_contents;
2943 static const uint8_t kFakeRenegotiateExtension[] = {0};
2944 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2945 ssl_client_cipher_list_contains_cipher(client_hello,
2946 SSL3_CK_SCSV & 0xffff)) {
2947 /* The renegotiation SCSV was received so pretend that we received a
2948 * renegotiation extension. */
2949 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2950 sizeof(kFakeRenegotiateExtension));
2951 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002952 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002953 }
2954
2955 /* Extension wasn't observed so call the callback with a NULL
2956 * parameter. */
2957 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002958 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002959 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002960 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002961 *out_alert = alert;
2962 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002963 }
2964 }
2965
Adam Langleyfcf25832014-12-18 17:42:32 -08002966 return 1;
2967}
Adam Langley95c29f32014-06-20 12:00:00 -07002968
David Benjamin731058e2016-12-03 23:15:13 -05002969int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2970 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002971 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002972 int alert = -1;
David Benjamin8c880a22016-12-03 02:20:34 -05002973 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002974 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002975 return 0;
2976 }
Adam Langley95c29f32014-06-20 12:00:00 -07002977
David Benjamin8c880a22016-12-03 02:20:34 -05002978 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002979 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002980 return 0;
2981 }
Adam Langley95c29f32014-06-20 12:00:00 -07002982
Adam Langleyfcf25832014-12-18 17:42:32 -08002983 return 1;
2984}
Adam Langley95c29f32014-06-20 12:00:00 -07002985
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002986OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2987
David Benjamin8c880a22016-12-03 02:20:34 -05002988static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2989 int *out_alert) {
2990 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002991 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2992 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2993 return 1;
2994 }
Adam Langley614c66a2015-06-12 15:26:58 -07002995
Steven Valdez143e8b32016-07-11 13:19:03 -04002996 /* Decode the extensions block and check it is valid. */
2997 CBS extensions;
2998 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2999 !tls1_check_duplicate_extensions(&extensions)) {
3000 *out_alert = SSL_AD_DECODE_ERROR;
3001 return 0;
3002 }
3003
3004 uint32_t received = 0;
3005 while (CBS_len(&extensions) != 0) {
3006 uint16_t type;
3007 CBS extension;
3008
3009 /* Decode the next extension. */
3010 if (!CBS_get_u16(&extensions, &type) ||
3011 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003012 *out_alert = SSL_AD_DECODE_ERROR;
3013 return 0;
3014 }
Adam Langley95c29f32014-06-20 12:00:00 -07003015
Steven Valdez143e8b32016-07-11 13:19:03 -04003016 unsigned ext_index;
3017 const struct tls_extension *const ext =
3018 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07003019
Steven Valdez143e8b32016-07-11 13:19:03 -04003020 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09003021 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003022 return 0;
3023 }
Steven Valdez143e8b32016-07-11 13:19:03 -04003024 continue;
3025 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003026
David Benjamin8c880a22016-12-03 02:20:34 -05003027 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04003028 type != TLSEXT_TYPE_renegotiate) {
3029 /* If the extension was never sent then it is illegal, except for the
3030 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04003031 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3032 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04003033 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04003034 return 0;
3035 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003036
Steven Valdez143e8b32016-07-11 13:19:03 -04003037 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07003038
Steven Valdez143e8b32016-07-11 13:19:03 -04003039 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003040 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04003041 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003042 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04003043 *out_alert = alert;
3044 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07003045 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003046 }
Adam Langley95c29f32014-06-20 12:00:00 -07003047
David Benjamin54091232016-09-05 12:47:25 -04003048 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003049 if (!(received & (1u << i))) {
3050 /* Extension wasn't observed so call the callback with a NULL
3051 * parameter. */
3052 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003053 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003054 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003055 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07003056 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08003057 return 0;
3058 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003059 }
3060 }
Adam Langley95c29f32014-06-20 12:00:00 -07003061
Adam Langleyfcf25832014-12-18 17:42:32 -08003062 return 1;
3063}
Adam Langley95c29f32014-06-20 12:00:00 -07003064
David Benjamin8c880a22016-12-03 02:20:34 -05003065static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3066 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003067 int ret = SSL_TLSEXT_ERR_NOACK;
3068 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003069
David Benjamin78f8aab2016-03-10 16:33:58 -05003070 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003071 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003072 ssl->ctx->tlsext_servername_arg);
3073 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003074 ret = ssl->initial_ctx->tlsext_servername_callback(
3075 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003076 }
Adam Langley95c29f32014-06-20 12:00:00 -07003077
Adam Langleyfcf25832014-12-18 17:42:32 -08003078 switch (ret) {
3079 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003080 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003081 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003082
Adam Langleyfcf25832014-12-18 17:42:32 -08003083 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003084 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003085 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003086
Adam Langleyfcf25832014-12-18 17:42:32 -08003087 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003088 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003089 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003090
Adam Langleyfcf25832014-12-18 17:42:32 -08003091 default:
3092 return 1;
3093 }
3094}
Adam Langleyed8270a2014-09-02 13:52:56 -07003095
David Benjamin8c880a22016-12-03 02:20:34 -05003096static int ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs) {
3097 SSL *const ssl = hs->ssl;
David Benjaminfc059942015-07-30 23:01:59 -04003098 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08003099 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07003100
David Benjamin78f8aab2016-03-10 16:33:58 -05003101 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003102 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003103 ssl->ctx->tlsext_servername_arg);
3104 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003105 ret = ssl->initial_ctx->tlsext_servername_callback(
3106 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003107 }
Adam Langley95c29f32014-06-20 12:00:00 -07003108
Adam Langleyfcf25832014-12-18 17:42:32 -08003109 switch (ret) {
3110 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003111 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003112 return -1;
David Benjamin03973092014-06-24 23:27:17 -04003113
Adam Langleyfcf25832014-12-18 17:42:32 -08003114 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003115 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003116 return 1;
3117
3118 default:
3119 return 1;
3120 }
3121}
3122
David Benjamin8c880a22016-12-03 02:20:34 -05003123int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3124 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003125 int alert = -1;
David Benjamin8c880a22016-12-03 02:20:34 -05003126 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003127 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003128 return 0;
3129 }
3130
David Benjamin8c880a22016-12-03 02:20:34 -05003131 if (ssl_check_serverhello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04003132 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08003133 return 0;
3134 }
3135
3136 return 1;
3137}
Adam Langley95c29f32014-06-20 12:00:00 -07003138
David Benjamine3aa1d92015-06-16 15:34:50 -04003139int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05003140 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04003141 size_t ticket_len, const uint8_t *session_id,
3142 size_t session_id_len) {
3143 int ret = 1; /* Most errors are non-fatal. */
3144 SSL_CTX *ssl_ctx = ssl->initial_ctx;
3145 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003146
David Benjamine3aa1d92015-06-16 15:34:50 -04003147 HMAC_CTX hmac_ctx;
3148 HMAC_CTX_init(&hmac_ctx);
3149 EVP_CIPHER_CTX cipher_ctx;
3150 EVP_CIPHER_CTX_init(&cipher_ctx);
3151
David Benjaminef1b0092015-11-21 14:05:44 -05003152 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04003153 *out_session = NULL;
3154
Steven Valdez4aa154e2016-07-29 14:32:55 -04003155 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
3156 goto done;
3157 }
3158
David Benjamine3aa1d92015-06-16 15:34:50 -04003159 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3160 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003161 }
3162
David Benjaminadcc3952015-04-26 13:07:57 -04003163 /* Ensure there is room for the key name and the largest IV
3164 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3165 * the maximum IV length should be well under the minimum size for the
3166 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003167 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3168 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003169 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003170 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003171
David Benjamine3aa1d92015-06-16 15:34:50 -04003172 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003173 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3174 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3175 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003176 if (cb_ret < 0) {
3177 ret = 0;
3178 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003179 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003180 if (cb_ret == 0) {
3181 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003182 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003183 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003184 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003185 }
3186 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003187 /* Check the key name matches. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003188 if (OPENSSL_memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3189 SSL_TICKET_KEY_NAME_LEN) != 0) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003190 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003191 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003192 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3193 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003194 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003195 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3196 ssl_ctx->tlsext_tick_aes_key, iv)) {
3197 ret = 0;
3198 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003199 }
3200 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003201 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003202
David Benjamine3aa1d92015-06-16 15:34:50 -04003203 /* Check the MAC at the end of the ticket. */
3204 uint8_t mac[EVP_MAX_MD_SIZE];
3205 size_t mac_len = HMAC_size(&hmac_ctx);
3206 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003207 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003208 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003209 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003210 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3211 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003212 int mac_ok =
3213 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3214#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3215 mac_ok = 1;
3216#endif
3217 if (!mac_ok) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003218 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003219 }
3220
David Benjamine3aa1d92015-06-16 15:34:50 -04003221 /* Decrypt the session data. */
3222 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3223 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3224 mac_len;
3225 plaintext = OPENSSL_malloc(ciphertext_len);
3226 if (plaintext == NULL) {
3227 ret = 0;
3228 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003229 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003230 size_t plaintext_len;
3231#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin17cf2cb2016-12-13 01:07:13 -05003232 OPENSSL_memcpy(plaintext, ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003233 plaintext_len = ciphertext_len;
3234#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003235 if (ciphertext_len >= INT_MAX) {
3236 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003237 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003238 int len1, len2;
3239 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3240 (int)ciphertext_len) ||
3241 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
3242 ERR_clear_error(); /* Don't leave an error on the queue. */
3243 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003244 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003245 plaintext_len = (size_t)(len1 + len2);
3246#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003247
David Benjamine3aa1d92015-06-16 15:34:50 -04003248 /* Decode the session. */
David Benjaminfbc45d72016-09-22 01:21:24 -04003249 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003250 if (session == NULL) {
3251 ERR_clear_error(); /* Don't leave an error on the queue. */
3252 goto done;
3253 }
3254
3255 /* Copy the client's session ID into the new session, to denote the ticket has
3256 * been accepted. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003257 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003258 session->session_id_length = session_id_len;
3259
3260 *out_session = session;
3261
3262done:
3263 OPENSSL_free(plaintext);
3264 HMAC_CTX_cleanup(&hmac_ctx);
3265 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3266 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003267}
Adam Langley95c29f32014-06-20 12:00:00 -07003268
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003269int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003270 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003271 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003272 return 1;
3273 }
David Benjamincd996942014-07-20 16:23:51 -04003274
David Benjamin0fc37ef2016-08-17 15:29:46 -04003275 OPENSSL_free(hs->peer_sigalgs);
3276 hs->peer_sigalgs = NULL;
3277 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003278
3279 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003280 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003281 return 0;
3282 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003283 num_sigalgs /= 2;
3284
3285 /* supported_signature_algorithms in the certificate request is
3286 * allowed to be empty. */
3287 if (num_sigalgs == 0) {
3288 return 1;
3289 }
3290
Steven Valdez02563852016-06-23 13:33:05 -04003291 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3292 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003293 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3294 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003295 return 0;
3296 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003297 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003298
3299 CBS sigalgs;
3300 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003301 for (size_t i = 0; i < num_sigalgs; i++) {
3302 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003303 return 0;
3304 }
3305 }
Adam Langley95c29f32014-06-20 12:00:00 -07003306
Adam Langleyfcf25832014-12-18 17:42:32 -08003307 return 1;
3308}
David Benjaminec2f27d2014-11-13 19:17:25 -05003309
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003310int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3311 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003312 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003313
Steven Valdezf0451ca2016-06-29 13:16:27 -04003314 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3315 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3316 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003317 int type = ssl_private_key_type(ssl);
3318 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003319 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003320 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003321 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003322 if (ssl_is_ecdsa_key_type(type)) {
3323 *out = SSL_SIGN_ECDSA_SHA1;
3324 return 1;
3325 }
3326 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3327 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003328 }
3329
David Benjamin3ef76972016-10-17 17:59:54 -04003330 const uint16_t *sigalgs = cert->sigalgs;
3331 size_t num_sigalgs = cert->num_sigalgs;
3332 if (sigalgs == NULL) {
3333 sigalgs = kSignSignatureAlgorithms;
3334 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003335 }
3336
David Benjamin0fc37ef2016-08-17 15:29:46 -04003337 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3338 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3339 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003340 /* If the client didn't specify any signature_algorithms extension then
3341 * we can assume that it supports SHA1. See
3342 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3343 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3344 SSL_SIGN_ECDSA_SHA1};
3345 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003346 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003347 }
3348
David Benjamin0fc37ef2016-08-17 15:29:46 -04003349 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003350 uint16_t sigalg = sigalgs[i];
3351 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3352 * negotiated. */
3353 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3354 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3355 continue;
3356 }
3357
David Benjamin0fc37ef2016-08-17 15:29:46 -04003358 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003359 if (sigalg == peer_sigalgs[j]) {
3360 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003361 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003362 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003363 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003364 }
Adam Langley95c29f32014-06-20 12:00:00 -07003365
David Benjaminea9a0d52016-07-08 15:52:59 -07003366 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3367 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003368}
Adam Langley95c29f32014-06-20 12:00:00 -07003369
Nick Harper60a85cb2016-09-23 16:25:11 -07003370int tls1_verify_channel_id(SSL *ssl) {
3371 int ret = 0;
3372 uint16_t extension_type;
3373 CBS extension, channel_id;
3374
3375 /* A Channel ID handshake message is structured to contain multiple
3376 * extensions, but the only one that can be present is Channel ID. */
3377 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3378 if (!CBS_get_u16(&channel_id, &extension_type) ||
3379 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3380 CBS_len(&channel_id) != 0 ||
3381 extension_type != TLSEXT_TYPE_channel_id ||
3382 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3383 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3384 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3385 return 0;
3386 }
3387
3388 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3389 if (!p256) {
3390 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3391 return 0;
3392 }
3393
3394 EC_KEY *key = NULL;
3395 EC_POINT *point = NULL;
3396 BIGNUM x, y;
3397 ECDSA_SIG sig;
3398 BN_init(&x);
3399 BN_init(&y);
3400 sig.r = BN_new();
3401 sig.s = BN_new();
3402 if (sig.r == NULL || sig.s == NULL) {
3403 goto err;
3404 }
3405
3406 const uint8_t *p = CBS_data(&extension);
3407 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3408 BN_bin2bn(p + 32, 32, &y) == NULL ||
3409 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3410 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3411 goto err;
3412 }
3413
3414 point = EC_POINT_new(p256);
3415 if (point == NULL ||
3416 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3417 goto err;
3418 }
3419
3420 key = EC_KEY_new();
3421 if (key == NULL ||
3422 !EC_KEY_set_group(key, p256) ||
3423 !EC_KEY_set_public_key(key, point)) {
3424 goto err;
3425 }
3426
3427 uint8_t digest[EVP_MAX_MD_SIZE];
3428 size_t digest_len;
3429 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3430 goto err;
3431 }
3432
3433 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3434#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3435 sig_ok = 1;
3436#endif
3437 if (!sig_ok) {
3438 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3439 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3440 ssl->s3->tlsext_channel_id_valid = 0;
3441 goto err;
3442 }
3443
David Benjamin17cf2cb2016-12-13 01:07:13 -05003444 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
Nick Harper60a85cb2016-09-23 16:25:11 -07003445 ret = 1;
3446
3447err:
3448 BN_free(&x);
3449 BN_free(&y);
3450 BN_free(sig.r);
3451 BN_free(sig.s);
3452 EC_KEY_free(key);
3453 EC_POINT_free(point);
3454 EC_GROUP_free(p256);
3455 return ret;
3456}
3457
3458int tls1_write_channel_id(SSL *ssl, CBB *cbb) {
3459 uint8_t digest[EVP_MAX_MD_SIZE];
3460 size_t digest_len;
3461 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3462 return 0;
3463 }
3464
3465 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3466 if (ec_key == NULL) {
3467 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3468 return 0;
3469 }
3470
3471 int ret = 0;
3472 BIGNUM *x = BN_new();
3473 BIGNUM *y = BN_new();
3474 ECDSA_SIG *sig = NULL;
3475 if (x == NULL || y == NULL ||
3476 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3477 EC_KEY_get0_public_key(ec_key),
3478 x, y, NULL)) {
3479 goto err;
3480 }
3481
3482 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3483 if (sig == NULL) {
3484 goto err;
3485 }
3486
3487 CBB child;
3488 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3489 !CBB_add_u16_length_prefixed(cbb, &child) ||
3490 !BN_bn2cbb_padded(&child, 32, x) ||
3491 !BN_bn2cbb_padded(&child, 32, y) ||
3492 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3493 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3494 !CBB_flush(cbb)) {
3495 goto err;
3496 }
3497
3498 ret = 1;
3499
3500err:
3501 BN_free(x);
3502 BN_free(y);
3503 ECDSA_SIG_free(sig);
3504 return ret;
3505}
3506
David Benjamind6a4ae92015-08-06 11:10:51 -04003507int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003508 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3509 uint8_t *msg;
3510 size_t msg_len;
3511 if (!tls13_get_cert_verify_signature_input(ssl, &msg, &msg_len,
3512 ssl_cert_verify_channel_id)) {
3513 return 0;
3514 }
3515 SHA256(msg, msg_len, out);
3516 *out_len = SHA256_DIGEST_LENGTH;
3517 OPENSSL_free(msg);
3518 return 1;
3519 }
3520
Nick Harper95594012016-10-20 14:07:13 -07003521 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003522
Nick Harper95594012016-10-20 14:07:13 -07003523 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003524 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003525 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003526
Steven Valdez87eab492016-06-27 16:34:59 -04003527 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003528 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003529 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003530 if (ssl->session->original_handshake_hash_len == 0) {
3531 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003532 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003533 }
Nick Harper95594012016-10-20 14:07:13 -07003534 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3535 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003536 }
3537
3538 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3539 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3540 sizeof(handshake_hash));
3541 if (handshake_hash_len < 0) {
Nick Harper95594012016-10-20 14:07:13 -07003542 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003543 }
Nick Harper95594012016-10-20 14:07:13 -07003544 SHA256_Update(&ctx, handshake_hash, (size_t)handshake_hash_len);
3545 SHA256_Final(out, &ctx);
3546 *out_len = SHA256_DIGEST_LENGTH;
3547 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003548}
Adam Langley1258b6a2014-06-20 12:00:00 -07003549
3550/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003551 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003552 * data. */
3553int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003554 int digest_len;
3555 /* This function should never be called for a resumed session because the
3556 * handshake hashes that we wish to record are for the original, full
3557 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003558 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003559 return -1;
3560 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003561
Adam Langleyfcf25832014-12-18 17:42:32 -08003562 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003563 tls1_handshake_digest(
3564 ssl, ssl->s3->new_session->original_handshake_hash,
3565 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003566 if (digest_len < 0) {
3567 return -1;
3568 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003569
David Benjamin30c4c302016-12-07 22:35:24 -05003570 assert(sizeof(ssl->s3->new_session->original_handshake_hash) < 256);
3571 ssl->s3->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003572
Adam Langleyfcf25832014-12-18 17:42:32 -08003573 return 1;
3574}
Nick Harper60a85cb2016-09-23 16:25:11 -07003575
3576int ssl_do_channel_id_callback(SSL *ssl) {
3577 if (ssl->tlsext_channel_id_private != NULL ||
3578 ssl->ctx->channel_id_cb == NULL) {
3579 return 1;
3580 }
3581
3582 EVP_PKEY *key = NULL;
3583 ssl->ctx->channel_id_cb(ssl, &key);
3584 if (key == NULL) {
3585 /* The caller should try again later. */
3586 return 1;
3587 }
3588
3589 int ret = SSL_set1_tls_channel_id(ssl, key);
3590 EVP_PKEY_free(key);
3591 return ret;
3592}
Adam Langleycfa08c32016-11-17 13:21:27 -08003593
3594int ssl_is_sct_list_valid(const CBS *contents) {
3595 /* Shallow parse the SCT list for sanity. By the RFC
3596 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3597 * of the SCTs may be empty. */
3598 CBS copy = *contents;
3599 CBS sct_list;
3600 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3601 CBS_len(&copy) != 0 ||
3602 CBS_len(&sct_list) == 0) {
3603 return 0;
3604 }
3605
3606 while (CBS_len(&sct_list) > 0) {
3607 CBS sct;
3608 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3609 CBS_len(&sct) == 0) {
3610 return 0;
3611 }
3612 }
3613
3614 return 1;
3615}