blob: dc377c43f8f96f8d19b9fe05169307aed1e49204 [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 int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1140 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001141 if (!ssl->ocsp_stapling_enabled) {
1142 return 1;
1143 }
1144
1145 CBB contents;
1146 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1147 !CBB_add_u16_length_prefixed(out, &contents) ||
1148 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1149 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1150 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1151 !CBB_flush(out)) {
1152 return 0;
1153 }
1154
1155 return 1;
1156}
1157
David Benjamin8c880a22016-12-03 02:20:34 -05001158static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001159 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001160 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001161 if (contents == NULL) {
1162 return 1;
1163 }
1164
Steven Valdeza833c352016-11-01 13:39:36 -04001165 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1166 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001167 return 0;
1168 }
1169
Steven Valdeza833c352016-11-01 13:39:36 -04001170 /* OCSP stapling is forbidden on non-certificate ciphers. */
1171 if (CBS_len(contents) != 0 ||
1172 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001173 return 0;
1174 }
1175
Steven Valdeza833c352016-11-01 13:39:36 -04001176 /* Note this does not check for resumption in TLS 1.2. Sending
1177 * status_request here does not make sense, but OpenSSL does so and the
1178 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001179
David Benjamin8c880a22016-12-03 02:20:34 -05001180 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001181 return 1;
1182}
1183
David Benjamin8c880a22016-12-03 02:20:34 -05001184static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001185 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001186 if (contents == NULL) {
1187 return 1;
1188 }
1189
1190 uint8_t status_type;
1191 if (!CBS_get_u8(contents, &status_type)) {
1192 return 0;
1193 }
1194
1195 /* We cannot decide whether OCSP stapling will occur yet because the correct
1196 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001197 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001198
Adam Langleybb0bd042015-07-01 16:21:03 -07001199 return 1;
1200}
1201
David Benjamin8c880a22016-12-03 02:20:34 -05001202static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1203 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001204 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001205 !hs->ocsp_stapling_requested ||
Alessandro Ghedini559f0642016-12-07 12:55:32 +00001206 ssl->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001207 ssl->s3->session_reused ||
Steven Valdeza833c352016-11-01 13:39:36 -04001208 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001209 return 1;
1210 }
1211
David Benjamin8c880a22016-12-03 02:20:34 -05001212 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001213
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001214 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001215 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001216}
1217
1218
Adam Langley97dfcbf2015-07-01 18:35:20 -07001219/* Next protocol negotiation.
1220 *
1221 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1222
David Benjamin8c880a22016-12-03 02:20:34 -05001223static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1224 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001225 if (ssl->s3->initial_handshake_complete ||
1226 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001227 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001228 return 1;
1229 }
1230
1231 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1232 !CBB_add_u16(out, 0 /* length */)) {
1233 return 0;
1234 }
1235
1236 return 1;
1237}
1238
David Benjamin8c880a22016-12-03 02:20:34 -05001239static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001240 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001241 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001242 if (contents == NULL) {
1243 return 1;
1244 }
1245
Steven Valdez143e8b32016-07-11 13:19:03 -04001246 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1247 return 0;
1248 }
1249
Adam Langley97dfcbf2015-07-01 18:35:20 -07001250 /* If any of these are false then we should never have sent the NPN
1251 * extension in the ClientHello and thus this function should never have been
1252 * called. */
1253 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001254 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001255 assert(ssl->ctx->next_proto_select_cb != NULL);
1256
David Benjamin76c2efc2015-08-31 14:24:29 -04001257 if (ssl->s3->alpn_selected != NULL) {
1258 /* NPN and ALPN may not be negotiated in the same connection. */
1259 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1260 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1261 return 0;
1262 }
1263
Adam Langley97dfcbf2015-07-01 18:35:20 -07001264 const uint8_t *const orig_contents = CBS_data(contents);
1265 const size_t orig_len = CBS_len(contents);
1266
1267 while (CBS_len(contents) != 0) {
1268 CBS proto;
1269 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1270 CBS_len(&proto) == 0) {
1271 return 0;
1272 }
1273 }
1274
1275 uint8_t *selected;
1276 uint8_t selected_len;
1277 if (ssl->ctx->next_proto_select_cb(
1278 ssl, &selected, &selected_len, orig_contents, orig_len,
1279 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1280 *out_alert = SSL_AD_INTERNAL_ERROR;
1281 return 0;
1282 }
1283
David Benjamin79978df2015-12-25 15:56:49 -05001284 OPENSSL_free(ssl->s3->next_proto_negotiated);
1285 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1286 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001287 *out_alert = SSL_AD_INTERNAL_ERROR;
1288 return 0;
1289 }
1290
David Benjamin79978df2015-12-25 15:56:49 -05001291 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001292 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001293
1294 return 1;
1295}
1296
David Benjamin8c880a22016-12-03 02:20:34 -05001297static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001298 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001299 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001300 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1301 return 1;
1302 }
1303
Adam Langley97dfcbf2015-07-01 18:35:20 -07001304 if (contents != NULL && CBS_len(contents) != 0) {
1305 return 0;
1306 }
1307
1308 if (contents == NULL ||
1309 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001310 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001311 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001312 return 1;
1313 }
1314
David Benjamin8c880a22016-12-03 02:20:34 -05001315 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001316 return 1;
1317}
1318
David Benjamin8c880a22016-12-03 02:20:34 -05001319static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1320 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001321 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1322 * parsed. */
David Benjamin8c880a22016-12-03 02:20:34 -05001323 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001324 return 1;
1325 }
1326
1327 const uint8_t *npa;
1328 unsigned npa_len;
1329
1330 if (ssl->ctx->next_protos_advertised_cb(
1331 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1332 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001333 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001334 return 1;
1335 }
1336
1337 CBB contents;
1338 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1339 !CBB_add_u16_length_prefixed(out, &contents) ||
1340 !CBB_add_bytes(&contents, npa, npa_len) ||
1341 !CBB_flush(out)) {
1342 return 0;
1343 }
1344
1345 return 1;
1346}
1347
1348
Adam Langleyab8d87d2015-07-10 12:21:39 -07001349/* Signed certificate timestamps.
1350 *
1351 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1352
David Benjamin8c880a22016-12-03 02:20:34 -05001353static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1354 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001355 if (!ssl->signed_cert_timestamps_enabled) {
1356 return 1;
1357 }
1358
1359 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1360 !CBB_add_u16(out, 0 /* length */)) {
1361 return 0;
1362 }
1363
1364 return 1;
1365}
1366
David Benjamin8c880a22016-12-03 02:20:34 -05001367static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001368 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001369 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001370 if (contents == NULL) {
1371 return 1;
1372 }
1373
Steven Valdeza833c352016-11-01 13:39:36 -04001374 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1375 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001376 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001377 return 0;
1378 }
1379
Adam Langleyab8d87d2015-07-10 12:21:39 -07001380 /* If this is false then we should never have sent the SCT extension in the
1381 * ClientHello and thus this function should never have been called. */
1382 assert(ssl->signed_cert_timestamps_enabled);
1383
Adam Langleycfa08c32016-11-17 13:21:27 -08001384 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001385 *out_alert = SSL_AD_DECODE_ERROR;
1386 return 0;
1387 }
1388
David Benjamindaa88502016-10-04 16:32:16 -04001389 /* Session resumption uses the original session information. The extension
1390 * should not be sent on resumption, but RFC 6962 did not make it a
1391 * requirement, so tolerate this.
1392 *
1393 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001394 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001395 !CBS_stow(
1396 contents,
1397 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1398 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001399 *out_alert = SSL_AD_INTERNAL_ERROR;
1400 return 0;
1401 }
1402
1403 return 1;
1404}
1405
David Benjamin8c880a22016-12-03 02:20:34 -05001406static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001407 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001408 if (contents == NULL) {
1409 return 1;
1410 }
1411
1412 if (CBS_len(contents) != 0) {
1413 return 0;
1414 }
1415
David Benjamin8c880a22016-12-03 02:20:34 -05001416 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001417 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001418}
1419
David Benjamin8c880a22016-12-03 02:20:34 -05001420static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1421 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001422 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001423 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1424 ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001425 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001426 return 1;
1427 }
1428
1429 CBB contents;
1430 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1431 CBB_add_u16_length_prefixed(out, &contents) &&
1432 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1433 ssl->ctx->signed_cert_timestamp_list_length) &&
1434 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001435}
1436
1437
Adam Langleyf18e4532015-07-10 13:39:53 -07001438/* Application-level Protocol Negotiation.
1439 *
1440 * https://tools.ietf.org/html/rfc7301 */
1441
David Benjamin8c880a22016-12-03 02:20:34 -05001442static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1443 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001444 if (ssl->alpn_client_proto_list == NULL ||
1445 ssl->s3->initial_handshake_complete) {
1446 return 1;
1447 }
1448
1449 CBB contents, proto_list;
1450 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1451 !CBB_add_u16_length_prefixed(out, &contents) ||
1452 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1453 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1454 ssl->alpn_client_proto_list_len) ||
1455 !CBB_flush(out)) {
1456 return 0;
1457 }
1458
1459 return 1;
1460}
1461
David Benjamin8c880a22016-12-03 02:20:34 -05001462static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001463 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001464 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001465 if (contents == NULL) {
1466 return 1;
1467 }
1468
1469 assert(!ssl->s3->initial_handshake_complete);
1470 assert(ssl->alpn_client_proto_list != NULL);
1471
David Benjamin8c880a22016-12-03 02:20:34 -05001472 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001473 /* NPN and ALPN may not be negotiated in the same connection. */
1474 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1475 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1476 return 0;
1477 }
1478
Adam Langleyf18e4532015-07-10 13:39:53 -07001479 /* The extension data consists of a ProtocolNameList which must have
1480 * exactly one ProtocolName. Each of these is length-prefixed. */
1481 CBS protocol_name_list, protocol_name;
1482 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1483 CBS_len(contents) != 0 ||
1484 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1485 /* Empty protocol names are forbidden. */
1486 CBS_len(&protocol_name) == 0 ||
1487 CBS_len(&protocol_name_list) != 0) {
1488 return 0;
1489 }
1490
David Benjamin3e517572016-08-11 11:52:23 -04001491 /* Check that the protcol name is one of the ones we advertised. */
1492 int protocol_ok = 0;
1493 CBS client_protocol_name_list, client_protocol_name;
1494 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1495 ssl->alpn_client_proto_list_len);
1496 while (CBS_len(&client_protocol_name_list) > 0) {
1497 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1498 &client_protocol_name)) {
1499 *out_alert = SSL_AD_INTERNAL_ERROR;
1500 return 0;
1501 }
1502
1503 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
David Benjamin17cf2cb2016-12-13 01:07:13 -05001504 OPENSSL_memcmp(CBS_data(&client_protocol_name),
1505 CBS_data(&protocol_name),
1506 CBS_len(&protocol_name)) == 0) {
David Benjamin3e517572016-08-11 11:52:23 -04001507 protocol_ok = 1;
1508 break;
1509 }
1510 }
1511
1512 if (!protocol_ok) {
1513 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1514 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1515 return 0;
1516 }
1517
Adam Langleyf18e4532015-07-10 13:39:53 -07001518 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1519 &ssl->s3->alpn_selected_len)) {
1520 *out_alert = SSL_AD_INTERNAL_ERROR;
1521 return 0;
1522 }
1523
1524 return 1;
1525}
1526
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001527int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001528 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001529 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001530 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001531 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001532 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001533 client_hello, &contents,
1534 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1535 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001536 return 1;
1537 }
1538
1539 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001540 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001541
1542 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001543 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1544 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001545 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001546 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1547 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001548 return 0;
1549 }
1550
1551 /* Validate the protocol list. */
1552 CBS protocol_name_list_copy = protocol_name_list;
1553 while (CBS_len(&protocol_name_list_copy) > 0) {
1554 CBS protocol_name;
1555
1556 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1557 /* Empty protocol names are forbidden. */
1558 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001559 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1560 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001561 return 0;
1562 }
1563 }
1564
1565 const uint8_t *selected;
1566 uint8_t selected_len;
1567 if (ssl->ctx->alpn_select_cb(
1568 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1569 CBS_len(&protocol_name_list),
1570 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1571 OPENSSL_free(ssl->s3->alpn_selected);
1572 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1573 if (ssl->s3->alpn_selected == NULL) {
1574 *out_alert = SSL_AD_INTERNAL_ERROR;
1575 return 0;
1576 }
1577 ssl->s3->alpn_selected_len = selected_len;
1578 }
1579
1580 return 1;
1581}
1582
David Benjamin8c880a22016-12-03 02:20:34 -05001583static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1584 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001585 if (ssl->s3->alpn_selected == NULL) {
1586 return 1;
1587 }
1588
1589 CBB contents, proto_list, proto;
1590 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1591 !CBB_add_u16_length_prefixed(out, &contents) ||
1592 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1593 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001594 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1595 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001596 !CBB_flush(out)) {
1597 return 0;
1598 }
1599
1600 return 1;
1601}
1602
1603
Adam Langley49c7af12015-07-10 14:33:46 -07001604/* Channel ID.
1605 *
1606 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1607
David Benjamin8c880a22016-12-03 02:20:34 -05001608static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1609 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001610}
1611
David Benjamin8c880a22016-12-03 02:20:34 -05001612static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1613 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001614 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001615 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001616 return 1;
1617 }
1618
1619 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1620 !CBB_add_u16(out, 0 /* length */)) {
1621 return 0;
1622 }
1623
1624 return 1;
1625}
1626
David Benjamin8c880a22016-12-03 02:20:34 -05001627static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1628 uint8_t *out_alert, CBS *contents) {
1629 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001630 if (contents == NULL) {
1631 return 1;
1632 }
1633
David Benjamince079fd2016-08-02 16:22:34 -04001634 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001635 assert(ssl->tlsext_channel_id_enabled);
1636
1637 if (CBS_len(contents) != 0) {
1638 return 0;
1639 }
1640
1641 ssl->s3->tlsext_channel_id_valid = 1;
1642 return 1;
1643}
1644
David Benjamin8c880a22016-12-03 02:20:34 -05001645static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1646 uint8_t *out_alert, CBS *contents) {
1647 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001648 if (contents == NULL ||
1649 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001650 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001651 return 1;
1652 }
1653
1654 if (CBS_len(contents) != 0) {
1655 return 0;
1656 }
1657
1658 ssl->s3->tlsext_channel_id_valid = 1;
1659 return 1;
1660}
1661
David Benjamin8c880a22016-12-03 02:20:34 -05001662static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1663 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001664 if (!ssl->s3->tlsext_channel_id_valid) {
1665 return 1;
1666 }
1667
1668 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1669 !CBB_add_u16(out, 0 /* length */)) {
1670 return 0;
1671 }
1672
1673 return 1;
1674}
1675
Adam Langley391250d2015-07-15 19:06:07 -07001676
1677/* Secure Real-time Transport Protocol (SRTP) extension.
1678 *
1679 * https://tools.ietf.org/html/rfc5764 */
1680
Adam Langley391250d2015-07-15 19:06:07 -07001681
David Benjamin8c880a22016-12-03 02:20:34 -05001682static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1683 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001684}
1685
David Benjamin8c880a22016-12-03 02:20:34 -05001686static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1687 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001688 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1689 if (profiles == NULL) {
1690 return 1;
1691 }
1692 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1693 if (num_profiles == 0) {
1694 return 1;
1695 }
1696
1697 CBB contents, profile_ids;
1698 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1699 !CBB_add_u16_length_prefixed(out, &contents) ||
1700 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1701 return 0;
1702 }
1703
David Benjamin54091232016-09-05 12:47:25 -04001704 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001705 if (!CBB_add_u16(&profile_ids,
1706 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1707 return 0;
1708 }
1709 }
1710
1711 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1712 !CBB_flush(out)) {
1713 return 0;
1714 }
1715
1716 return 1;
1717}
1718
David Benjamin8c880a22016-12-03 02:20:34 -05001719static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001720 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001721 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001722 if (contents == NULL) {
1723 return 1;
1724 }
1725
1726 /* The extension consists of a u16-prefixed profile ID list containing a
1727 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1728 *
1729 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1730 CBS profile_ids, srtp_mki;
1731 uint16_t profile_id;
1732 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1733 !CBS_get_u16(&profile_ids, &profile_id) ||
1734 CBS_len(&profile_ids) != 0 ||
1735 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1736 CBS_len(contents) != 0) {
1737 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1738 return 0;
1739 }
1740
1741 if (CBS_len(&srtp_mki) != 0) {
1742 /* Must be no MKI, since we never offer one. */
1743 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1744 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1745 return 0;
1746 }
1747
1748 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1749
1750 /* Check to see if the server gave us something we support (and presumably
1751 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001752 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001753 const SRTP_PROTECTION_PROFILE *profile =
1754 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1755
1756 if (profile->id == profile_id) {
1757 ssl->srtp_profile = profile;
1758 return 1;
1759 }
1760 }
1761
1762 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1763 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1764 return 0;
1765}
1766
David Benjamin8c880a22016-12-03 02:20:34 -05001767static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001768 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001769 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001770 if (contents == NULL) {
1771 return 1;
1772 }
1773
1774 CBS profile_ids, srtp_mki;
1775 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1776 CBS_len(&profile_ids) < 2 ||
1777 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1778 CBS_len(contents) != 0) {
1779 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1780 return 0;
1781 }
1782 /* Discard the MKI value for now. */
1783
1784 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1785 SSL_get_srtp_profiles(ssl);
1786
1787 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001788 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001789 const SRTP_PROTECTION_PROFILE *server_profile =
1790 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1791
1792 CBS profile_ids_tmp;
1793 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1794
1795 while (CBS_len(&profile_ids_tmp) > 0) {
1796 uint16_t profile_id;
1797 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1798 return 0;
1799 }
1800
1801 if (server_profile->id == profile_id) {
1802 ssl->srtp_profile = server_profile;
1803 return 1;
1804 }
1805 }
1806 }
1807
1808 return 1;
1809}
1810
David Benjamin8c880a22016-12-03 02:20:34 -05001811static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1812 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001813 if (ssl->srtp_profile == NULL) {
1814 return 1;
1815 }
1816
1817 CBB contents, profile_ids;
1818 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1819 !CBB_add_u16_length_prefixed(out, &contents) ||
1820 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1821 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1822 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1823 !CBB_flush(out)) {
1824 return 0;
1825 }
1826
1827 return 1;
1828}
1829
Adam Langleybdd5d662015-07-20 16:19:08 -07001830
1831/* EC point formats.
1832 *
1833 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1834
David Benjamin8c880a22016-12-03 02:20:34 -05001835static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001836 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001837 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1838 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001839 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1840 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001841 !CBB_flush(out)) {
1842 return 0;
1843 }
1844
1845 return 1;
1846}
1847
David Benjamin8c880a22016-12-03 02:20:34 -05001848static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001849 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -05001850 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin70aba262016-11-01 12:08:15 -04001851 return 0;
1852 }
1853
1854 /* The point format extension is unneccessary in TLS 1.3. */
1855 if (min_version >= TLS1_3_VERSION) {
1856 return 1;
1857 }
1858
David Benjamin8c880a22016-12-03 02:20:34 -05001859 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001860}
1861
David Benjamin8c880a22016-12-03 02:20:34 -05001862static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001863 CBS *contents) {
1864 if (contents == NULL) {
1865 return 1;
1866 }
1867
David Benjamin8c880a22016-12-03 02:20:34 -05001868 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001869 return 0;
1870 }
1871
Adam Langleybdd5d662015-07-20 16:19:08 -07001872 CBS ec_point_format_list;
1873 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1874 CBS_len(contents) != 0) {
1875 return 0;
1876 }
1877
David Benjaminfc059942015-07-30 23:01:59 -04001878 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1879 * point format. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05001880 if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
1881 TLSEXT_ECPOINTFORMAT_uncompressed,
1882 CBS_len(&ec_point_format_list)) == NULL) {
David Benjaminfc059942015-07-30 23:01:59 -04001883 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001884 return 0;
1885 }
1886
1887 return 1;
1888}
1889
David Benjamin8c880a22016-12-03 02:20:34 -05001890static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001891 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001892 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001893 return 1;
1894 }
1895
David Benjamin8c880a22016-12-03 02:20:34 -05001896 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001897}
1898
David Benjamin8c880a22016-12-03 02:20:34 -05001899static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1900 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001901 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1902 return 1;
1903 }
1904
Adam Langleybdd5d662015-07-20 16:19:08 -07001905 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1906 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001907 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001908
1909 if (!using_ecc) {
1910 return 1;
1911 }
1912
David Benjamin8c880a22016-12-03 02:20:34 -05001913 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001914}
1915
Steven Valdeza833c352016-11-01 13:39:36 -04001916
Steven Valdez4aa154e2016-07-29 14:32:55 -04001917/* Pre Shared Key
1918 *
Steven Valdeza833c352016-11-01 13:39:36 -04001919 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001920
David Benjamin8c880a22016-12-03 02:20:34 -05001921static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1922 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001923 uint16_t min_version, max_version;
1924 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1925 return 0;
1926 }
1927
1928 uint16_t session_version;
1929 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1930 !ssl->method->version_from_wire(&session_version,
1931 ssl->session->ssl_version) ||
1932 session_version < TLS1_3_VERSION) {
1933 return 0;
1934 }
1935
1936 const EVP_MD *digest =
1937 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1938 size_t binder_len = EVP_MD_size(digest);
1939 return 15 + ssl->session->tlsext_ticklen + binder_len;
1940}
1941
David Benjamin8c880a22016-12-03 02:20:34 -05001942static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1943 SSL *const ssl = hs->ssl;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001944 uint16_t min_version, max_version;
1945 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1946 return 0;
1947 }
1948
David Benjamin2dc02042016-09-19 19:57:37 -04001949 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001950 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001951 !ssl->method->version_from_wire(&session_version,
1952 ssl->session->ssl_version) ||
1953 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001954 return 1;
1955 }
1956
Steven Valdeza833c352016-11-01 13:39:36 -04001957 struct timeval now;
1958 ssl_get_current_time(ssl, &now);
1959 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1960 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1961
1962 /* Fill in a placeholder zero binder of the appropriate length. It will be
1963 * computed and filled in later after length prefixes are computed. */
1964 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
1965 const EVP_MD *digest =
1966 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1967 size_t binder_len = EVP_MD_size(digest);
1968
1969 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001970 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1971 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001972 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001973 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1974 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001975 ssl->session->tlsext_ticklen) ||
1976 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1977 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1978 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1979 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001980 return 0;
1981 }
1982
David Benjamin8c880a22016-12-03 02:20:34 -05001983 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001984 return CBB_flush(out);
1985}
1986
David Benjamin8baf9632016-11-17 17:11:16 +09001987int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1988 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001989 CBS *contents) {
1990 uint16_t psk_id;
1991 if (!CBS_get_u16(contents, &psk_id) ||
1992 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001993 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001994 *out_alert = SSL_AD_DECODE_ERROR;
1995 return 0;
1996 }
1997
David Benjamineab773a2016-11-09 18:32:35 -05001998 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001999 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04002000 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002001 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2002 return 0;
2003 }
2004
2005 return 1;
2006}
2007
David Benjamin8baf9632016-11-17 17:11:16 +09002008int ssl_ext_pre_shared_key_parse_clienthello(SSL_HANDSHAKE *hs,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002009 SSL_SESSION **out_session,
Steven Valdeza833c352016-11-01 13:39:36 -04002010 CBS *out_binders,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002011 uint8_t *out_alert,
2012 CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09002013 SSL *const ssl = hs->ssl;
Steven Valdez5b986082016-09-01 12:29:49 -04002014 /* We only process the first PSK identity since we don't support pure PSK. */
Steven Valdeza833c352016-11-01 13:39:36 -04002015 uint32_t obfuscated_ticket_age;
David Benjaminaedf3032016-12-01 16:47:56 -05002016 CBS identities, ticket, binders;
2017 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2018 !CBS_get_u16_length_prefixed(&identities, &ticket) ||
2019 !CBS_get_u32(&identities, &obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04002020 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05002021 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04002022 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04002023 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002024 *out_alert = SSL_AD_DECODE_ERROR;
2025 return 0;
2026 }
2027
Steven Valdeza833c352016-11-01 13:39:36 -04002028 *out_binders = binders;
2029
David Benjaminaedf3032016-12-01 16:47:56 -05002030 /* Check the syntax of the remaining identities, but do not process them. */
2031 size_t num_identities = 1;
2032 while (CBS_len(&identities) != 0) {
2033 CBS unused_ticket;
2034 uint32_t unused_obfuscated_ticket_age;
2035 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
2036 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
2037 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2038 *out_alert = SSL_AD_DECODE_ERROR;
2039 return 0;
2040 }
2041
2042 num_identities++;
2043 }
2044
2045 /* Check the syntax of the binders. The value will be checked later if
2046 * resuming. */
2047 size_t num_binders = 0;
2048 while (CBS_len(&binders) != 0) {
2049 CBS binder;
2050 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
2051 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2052 *out_alert = SSL_AD_DECODE_ERROR;
2053 return 0;
2054 }
2055
2056 num_binders++;
2057 }
2058
2059 if (num_identities != num_binders) {
2060 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
2061 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04002062 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04002063 }
2064
Steven Valdeza833c352016-11-01 13:39:36 -04002065 /* TODO(svaldez): Check that the ticket_age is valid when attempting to use
2066 * the PSK for 0-RTT. http://crbug.com/boringssl/113 */
2067
Steven Valdez4aa154e2016-07-29 14:32:55 -04002068 /* TLS 1.3 session tickets are renewed separately as part of the
2069 * NewSessionTicket. */
David Benjamine7f60a22016-11-16 18:54:25 +09002070 int unused_renew;
2071 if (!tls_process_ticket(ssl, out_session, &unused_renew, CBS_data(&ticket),
2072 CBS_len(&ticket), NULL, 0)) {
2073 *out_alert = SSL_AD_INTERNAL_ERROR;
2074 return 0;
2075 }
2076
2077 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002078}
2079
David Benjamin8baf9632016-11-17 17:11:16 +09002080int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2081 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002082 return 1;
2083 }
2084
2085 CBB contents;
2086 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2087 !CBB_add_u16_length_prefixed(out, &contents) ||
2088 /* We only consider the first identity for resumption */
2089 !CBB_add_u16(&contents, 0) ||
2090 !CBB_flush(out)) {
2091 return 0;
2092 }
2093
2094 return 1;
2095}
2096
2097
Steven Valdeza833c352016-11-01 13:39:36 -04002098/* Pre-Shared Key Exchange Modes
2099 *
2100 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002101
David Benjamin8c880a22016-12-03 02:20:34 -05002102static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2103 CBB *out) {
2104 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04002105 uint16_t min_version, max_version;
2106 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2107 return 0;
2108 }
2109
2110 if (max_version < TLS1_3_VERSION) {
2111 return 1;
2112 }
2113
2114 CBB contents, ke_modes;
2115 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2116 !CBB_add_u16_length_prefixed(out, &contents) ||
2117 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2118 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2119 return 0;
2120 }
2121
2122 return CBB_flush(out);
2123}
2124
David Benjamin8c880a22016-12-03 02:20:34 -05002125static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002126 uint8_t *out_alert,
2127 CBS *contents) {
2128 if (contents == NULL) {
2129 return 1;
2130 }
2131
Steven Valdeza833c352016-11-01 13:39:36 -04002132 CBS ke_modes;
2133 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2134 CBS_len(&ke_modes) == 0 ||
2135 CBS_len(contents) != 0) {
2136 *out_alert = SSL_AD_DECODE_ERROR;
2137 return 0;
2138 }
2139
2140 /* We only support tickets with PSK_DHE_KE. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05002141 hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
2142 CBS_len(&ke_modes)) != NULL;
Steven Valdeza833c352016-11-01 13:39:36 -04002143
2144 return 1;
2145}
2146
2147
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002148/* Early Data Indication
2149 *
2150 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2151
David Benjamin8c880a22016-12-03 02:20:34 -05002152static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002153 /* TODO(svaldez): Support 0RTT. */
2154 return 1;
2155}
2156
David Benjamin8c880a22016-12-03 02:20:34 -05002157static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2158 uint8_t *out_alert, CBS *contents) {
2159 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002160 if (contents == NULL) {
2161 return 1;
2162 }
2163
2164 if (CBS_len(contents) != 0) {
2165 *out_alert = SSL_AD_DECODE_ERROR;
2166 return 0;
2167 }
2168
2169 /* Since we don't currently accept 0-RTT, we have to skip past any early data
2170 * the client might have sent. */
2171 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
2172 ssl->s3->skip_early_data = 1;
2173 }
2174 return 1;
2175}
2176
2177
Steven Valdez143e8b32016-07-11 13:19:03 -04002178/* Key Share
2179 *
David Benjamina128a552016-10-13 14:26:33 -04002180 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002181
David Benjamin8c880a22016-12-03 02:20:34 -05002182static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2183 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002184 uint16_t min_version, max_version;
2185 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2186 return 0;
2187 }
2188
David Benjamin53a2dfc2016-10-07 21:04:12 -04002189 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002190 return 1;
2191 }
2192
2193 CBB contents, kse_bytes;
2194 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2195 !CBB_add_u16_length_prefixed(out, &contents) ||
2196 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2197 return 0;
2198 }
2199
David Benjamin8c880a22016-12-03 02:20:34 -05002200 uint16_t group_id = hs->retry_group;
2201 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002202 /* We received a HelloRetryRequest without a new curve, so there is no new
2203 * share to append. Leave |ecdh_ctx| as-is. */
2204 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002205 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2206 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002207 return 0;
2208 }
David Benjamin8c880a22016-12-03 02:20:34 -05002209 OPENSSL_free(hs->key_share_bytes);
2210 hs->key_share_bytes = NULL;
2211 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002212 if (group_id == 0) {
2213 return CBB_flush(out);
2214 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002215 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002216 /* Add a fake group. See draft-davidben-tls-grease-01. */
2217 if (ssl->ctx->grease_enabled &&
2218 (!CBB_add_u16(&kse_bytes,
2219 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2220 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2221 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2222 return 0;
2223 }
2224
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002225 /* Predict the most preferred group. */
2226 const uint16_t *groups;
2227 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002228 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002229 if (groups_len == 0) {
2230 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2231 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002232 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002233
2234 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002235 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002236
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002237 CBB key_exchange;
2238 if (!CBB_add_u16(&kse_bytes, group_id) ||
2239 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002240 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2241 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002242 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002243 return 0;
2244 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002245
David Benjamin8c880a22016-12-03 02:20:34 -05002246 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002247 /* Save the contents of the extension to repeat it in the second
2248 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002249 hs->key_share_bytes_len = CBB_len(&kse_bytes);
2250 hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
2251 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002252 return 0;
2253 }
2254 }
2255
Steven Valdez143e8b32016-07-11 13:19:03 -04002256 return CBB_flush(out);
2257}
2258
David Benjamin8baf9632016-11-17 17:11:16 +09002259int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002260 size_t *out_secret_len,
2261 uint8_t *out_alert, CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09002262 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002263 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002264 uint16_t group_id;
2265 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002266 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2267 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002268 *out_alert = SSL_AD_DECODE_ERROR;
2269 return 0;
2270 }
2271
David Benjamin8baf9632016-11-17 17:11:16 +09002272 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002273 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2274 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2275 return 0;
2276 }
2277
David Benjamin8baf9632016-11-17 17:11:16 +09002278 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2279 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002280 *out_alert = SSL_AD_INTERNAL_ERROR;
2281 return 0;
2282 }
2283
David Benjamin4882a6c2016-12-11 02:48:12 -05002284 ssl->s3->new_session->group_id = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002285 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002286 return 1;
2287}
2288
David Benjamin8baf9632016-11-17 17:11:16 +09002289int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002290 uint8_t **out_secret,
2291 size_t *out_secret_len,
2292 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002293 uint16_t group_id;
2294 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002295 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002296 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2297 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2298 return 0;
2299 }
2300
2301 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002302 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002303 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002304 return 0;
2305 }
2306
David Benjamin7e1f9842016-09-20 19:24:40 -04002307 /* Find the corresponding key share. */
2308 int found = 0;
2309 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002310 while (CBS_len(&key_shares) > 0) {
2311 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002312 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002313 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002314 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2315 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002316 return 0;
2317 }
2318
David Benjamin7e1f9842016-09-20 19:24:40 -04002319 if (id == group_id) {
2320 if (found) {
2321 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2322 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2323 return 0;
2324 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002325
David Benjamin7e1f9842016-09-20 19:24:40 -04002326 found = 1;
2327 peer_key = peer_key_tmp;
2328 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002329 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002330 }
2331
David Benjamin7e1f9842016-09-20 19:24:40 -04002332 if (!found) {
2333 *out_found = 0;
2334 *out_secret = NULL;
2335 *out_secret_len = 0;
2336 return 1;
2337 }
2338
2339 /* Compute the DH secret. */
2340 uint8_t *secret = NULL;
2341 size_t secret_len;
2342 SSL_ECDH_CTX group;
David Benjamin17cf2cb2016-12-13 01:07:13 -05002343 OPENSSL_memset(&group, 0, sizeof(SSL_ECDH_CTX));
David Benjamin7e1f9842016-09-20 19:24:40 -04002344 CBB public_key;
2345 if (!CBB_init(&public_key, 32) ||
2346 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002347 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
2348 CBS_data(&peer_key), CBS_len(&peer_key)) ||
2349 !CBB_finish(&public_key, &hs->public_key, &hs->public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002350 OPENSSL_free(secret);
2351 SSL_ECDH_CTX_cleanup(&group);
2352 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002353 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002354 return 0;
2355 }
2356
2357 SSL_ECDH_CTX_cleanup(&group);
2358
2359 *out_secret = secret;
2360 *out_secret_len = secret_len;
2361 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002362 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002363}
2364
David Benjamin8baf9632016-11-17 17:11:16 +09002365int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2366 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002367 uint16_t group_id;
2368 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002369 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002370 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2371 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2372 !CBB_add_u16(&kse_bytes, group_id) ||
2373 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002374 !CBB_add_bytes(&public_key, hs->public_key, hs->public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002375 !CBB_flush(out)) {
2376 return 0;
2377 }
2378
David Benjamin8baf9632016-11-17 17:11:16 +09002379 OPENSSL_free(hs->public_key);
2380 hs->public_key = NULL;
2381 hs->public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002382
David Benjamin4882a6c2016-12-11 02:48:12 -05002383 ssl->s3->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002384 return 1;
2385}
2386
2387
Steven Valdezfdd10992016-09-15 16:27:05 -04002388/* Supported Versions
2389 *
2390 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2391
David Benjamin8c880a22016-12-03 02:20:34 -05002392static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2393 SSL *const ssl = hs->ssl;
Steven Valdezfdd10992016-09-15 16:27:05 -04002394 uint16_t min_version, max_version;
2395 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2396 return 0;
2397 }
2398
2399 if (max_version <= TLS1_2_VERSION) {
2400 return 1;
2401 }
2402
2403 CBB contents, versions;
2404 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2405 !CBB_add_u16_length_prefixed(out, &contents) ||
2406 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2407 return 0;
2408 }
2409
David Benjamind9791bf2016-09-27 16:39:52 -04002410 /* Add a fake version. See draft-davidben-tls-grease-01. */
2411 if (ssl->ctx->grease_enabled &&
2412 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2413 return 0;
2414 }
2415
Steven Valdezfdd10992016-09-15 16:27:05 -04002416 for (uint16_t version = max_version; version >= min_version; version--) {
2417 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2418 return 0;
2419 }
2420 }
2421
2422 if (!CBB_flush(out)) {
2423 return 0;
2424 }
2425
2426 return 1;
2427}
2428
2429
David Benjamin3baa6e12016-10-07 21:10:38 -04002430/* Cookie
2431 *
2432 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2433
David Benjamin8c880a22016-12-03 02:20:34 -05002434static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2435 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002436 return 1;
2437 }
2438
2439 CBB contents, cookie;
2440 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2441 !CBB_add_u16_length_prefixed(out, &contents) ||
2442 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002443 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002444 !CBB_flush(out)) {
2445 return 0;
2446 }
2447
2448 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002449 OPENSSL_free(hs->cookie);
2450 hs->cookie = NULL;
2451 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002452 return 1;
2453}
2454
2455
David Benjamin6f600d62016-12-21 16:06:54 -05002456/* Short record headers
2457 *
2458 * This is a non-standard extension which negotiates
2459 * https://github.com/tlswg/tls13-spec/pull/762 for experimenting. */
2460
2461static int ext_short_header_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2462 SSL *const ssl = hs->ssl;
2463 uint16_t min_version, max_version;
2464 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2465 return 0;
2466 }
2467
2468 if (max_version < TLS1_3_VERSION ||
2469 !ssl->ctx->short_header_enabled) {
2470 return 1;
2471 }
2472
2473 return CBB_add_u16(out, TLSEXT_TYPE_short_header) &&
2474 CBB_add_u16(out, 0 /* empty extension */);
2475}
2476
2477static int ext_short_header_parse_clienthello(SSL_HANDSHAKE *hs,
2478 uint8_t *out_alert,
2479 CBS *contents) {
2480 SSL *const ssl = hs->ssl;
2481 if (contents == NULL ||
2482 !ssl->ctx->short_header_enabled ||
2483 ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2484 return 1;
2485 }
2486
2487 if (CBS_len(contents) != 0) {
2488 return 0;
2489 }
2490
2491 ssl->s3->short_header = 1;
2492 return 1;
2493}
2494
2495
Steven Valdezce902a92016-05-17 11:47:53 -04002496/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002497 *
Steven Valdezce902a92016-05-17 11:47:53 -04002498 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002499 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002500
David Benjamin8c880a22016-12-03 02:20:34 -05002501static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2502 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002503 CBB contents, groups_bytes;
2504 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002505 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002506 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002507 return 0;
2508 }
2509
David Benjamin65ac9972016-09-02 21:35:25 -04002510 /* Add a fake group. See draft-davidben-tls-grease-01. */
2511 if (ssl->ctx->grease_enabled &&
2512 !CBB_add_u16(&groups_bytes,
2513 ssl_get_grease_value(ssl, ssl_grease_group))) {
2514 return 0;
2515 }
2516
Steven Valdezce902a92016-05-17 11:47:53 -04002517 const uint16_t *groups;
2518 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002519 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002520
David Benjamin54091232016-09-05 12:47:25 -04002521 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002522 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002523 return 0;
2524 }
2525 }
2526
2527 return CBB_flush(out);
2528}
2529
David Benjamin8c880a22016-12-03 02:20:34 -05002530static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2531 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002532 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002533 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2534 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002535 return 1;
2536}
2537
David Benjamin8c880a22016-12-03 02:20:34 -05002538static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2539 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002540 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002541 if (contents == NULL) {
2542 return 1;
2543 }
2544
Steven Valdezce902a92016-05-17 11:47:53 -04002545 CBS supported_group_list;
2546 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2547 CBS_len(&supported_group_list) == 0 ||
2548 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002549 CBS_len(contents) != 0) {
2550 return 0;
2551 }
2552
David Benjamin8c880a22016-12-03 02:20:34 -05002553 hs->peer_supported_group_list =
2554 OPENSSL_malloc(CBS_len(&supported_group_list));
2555 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002556 *out_alert = SSL_AD_INTERNAL_ERROR;
2557 return 0;
2558 }
2559
Steven Valdezce902a92016-05-17 11:47:53 -04002560 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002561 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002562 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002563 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002564 goto err;
2565 }
2566 }
2567
Steven Valdezce902a92016-05-17 11:47:53 -04002568 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002569 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002570
2571 return 1;
2572
2573err:
David Benjamin8c880a22016-12-03 02:20:34 -05002574 OPENSSL_free(hs->peer_supported_group_list);
2575 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002576 *out_alert = SSL_AD_INTERNAL_ERROR;
2577 return 0;
2578}
2579
David Benjamin8c880a22016-12-03 02:20:34 -05002580static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002581 /* Servers don't echo this extension. */
2582 return 1;
2583}
2584
2585
Adam Langley614c66a2015-06-12 15:26:58 -07002586/* kExtensions contains all the supported extensions. */
2587static const struct tls_extension kExtensions[] = {
2588 {
Adam Langley5021b222015-06-12 18:27:58 -07002589 TLSEXT_TYPE_renegotiate,
2590 NULL,
2591 ext_ri_add_clienthello,
2592 ext_ri_parse_serverhello,
2593 ext_ri_parse_clienthello,
2594 ext_ri_add_serverhello,
2595 },
2596 {
Adam Langley614c66a2015-06-12 15:26:58 -07002597 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002598 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002599 ext_sni_add_clienthello,
2600 ext_sni_parse_serverhello,
2601 ext_sni_parse_clienthello,
2602 ext_sni_add_serverhello,
2603 },
Adam Langley0a056712015-07-01 15:03:33 -07002604 {
2605 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002606 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002607 ext_ems_add_clienthello,
2608 ext_ems_parse_serverhello,
2609 ext_ems_parse_clienthello,
2610 ext_ems_add_serverhello,
2611 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002612 {
2613 TLSEXT_TYPE_session_ticket,
2614 NULL,
2615 ext_ticket_add_clienthello,
2616 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002617 /* Ticket extension client parsing is handled in ssl_session.c */
2618 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002619 ext_ticket_add_serverhello,
2620 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002621 {
2622 TLSEXT_TYPE_signature_algorithms,
2623 NULL,
2624 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002625 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002626 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002627 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002628 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002629 {
2630 TLSEXT_TYPE_status_request,
David Benjaminc2538642017-01-13 16:32:05 -05002631 NULL,
Adam Langleybb0bd042015-07-01 16:21:03 -07002632 ext_ocsp_add_clienthello,
2633 ext_ocsp_parse_serverhello,
2634 ext_ocsp_parse_clienthello,
2635 ext_ocsp_add_serverhello,
2636 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002637 {
2638 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002639 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002640 ext_npn_add_clienthello,
2641 ext_npn_parse_serverhello,
2642 ext_npn_parse_clienthello,
2643 ext_npn_add_serverhello,
2644 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002645 {
2646 TLSEXT_TYPE_certificate_timestamp,
2647 NULL,
2648 ext_sct_add_clienthello,
2649 ext_sct_parse_serverhello,
2650 ext_sct_parse_clienthello,
2651 ext_sct_add_serverhello,
2652 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002653 {
2654 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002655 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002656 ext_alpn_add_clienthello,
2657 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002658 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2659 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002660 ext_alpn_add_serverhello,
2661 },
Adam Langley49c7af12015-07-10 14:33:46 -07002662 {
2663 TLSEXT_TYPE_channel_id,
2664 ext_channel_id_init,
2665 ext_channel_id_add_clienthello,
2666 ext_channel_id_parse_serverhello,
2667 ext_channel_id_parse_clienthello,
2668 ext_channel_id_add_serverhello,
2669 },
Adam Langley391250d2015-07-15 19:06:07 -07002670 {
2671 TLSEXT_TYPE_srtp,
2672 ext_srtp_init,
2673 ext_srtp_add_clienthello,
2674 ext_srtp_parse_serverhello,
2675 ext_srtp_parse_clienthello,
2676 ext_srtp_add_serverhello,
2677 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002678 {
2679 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002680 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002681 ext_ec_point_add_clienthello,
2682 ext_ec_point_parse_serverhello,
2683 ext_ec_point_parse_clienthello,
2684 ext_ec_point_add_serverhello,
2685 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002686 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002687 TLSEXT_TYPE_key_share,
2688 NULL,
2689 ext_key_share_add_clienthello,
2690 forbid_parse_serverhello,
2691 ignore_parse_clienthello,
2692 dont_add_serverhello,
2693 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002694 {
Steven Valdeza833c352016-11-01 13:39:36 -04002695 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002696 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002697 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002698 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002699 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002700 dont_add_serverhello,
2701 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002702 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002703 TLSEXT_TYPE_early_data,
2704 NULL,
2705 ext_early_data_add_clienthello,
2706 forbid_parse_serverhello,
2707 ext_early_data_parse_clienthello,
2708 dont_add_serverhello,
2709 },
2710 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002711 TLSEXT_TYPE_supported_versions,
2712 NULL,
2713 ext_supported_versions_add_clienthello,
2714 forbid_parse_serverhello,
2715 ignore_parse_clienthello,
2716 dont_add_serverhello,
2717 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002718 {
2719 TLSEXT_TYPE_cookie,
2720 NULL,
2721 ext_cookie_add_clienthello,
2722 forbid_parse_serverhello,
2723 ignore_parse_clienthello,
2724 dont_add_serverhello,
2725 },
David Benjamin6f600d62016-12-21 16:06:54 -05002726 {
2727 TLSEXT_TYPE_short_header,
2728 NULL,
2729 ext_short_header_add_clienthello,
2730 forbid_parse_serverhello,
2731 ext_short_header_parse_clienthello,
2732 dont_add_serverhello,
2733 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002734 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2735 * intolerant to the last extension being zero-length. See
2736 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002737 {
Steven Valdezce902a92016-05-17 11:47:53 -04002738 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002739 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002740 ext_supported_groups_add_clienthello,
2741 ext_supported_groups_parse_serverhello,
2742 ext_supported_groups_parse_clienthello,
2743 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002744 },
Adam Langley614c66a2015-06-12 15:26:58 -07002745};
2746
2747#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2748
Adam Langley4cfa96b2015-07-01 11:56:55 -07002749OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002750 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002751 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002752OPENSSL_COMPILE_ASSERT(
2753 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2754 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002755
Adam Langley614c66a2015-06-12 15:26:58 -07002756static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2757 uint16_t value) {
2758 unsigned i;
2759 for (i = 0; i < kNumExtensions; i++) {
2760 if (kExtensions[i].value == value) {
2761 *out_index = i;
2762 return &kExtensions[i];
2763 }
2764 }
2765
2766 return NULL;
2767}
2768
Adam Langley09505632015-07-30 18:10:13 -07002769int SSL_extension_supported(unsigned extension_value) {
2770 uint32_t index;
2771 return extension_value == TLSEXT_TYPE_padding ||
2772 tls_extension_find(&index, extension_value) != NULL;
2773}
2774
David Benjamin8c880a22016-12-03 02:20:34 -05002775int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2776 SSL *const ssl = hs->ssl;
David Benjaminf04c2e92016-12-06 13:35:25 -05002777 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2778 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002779 !ssl->s3->send_connection_binding) {
2780 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002781 }
Adam Langley95c29f32014-06-20 12:00:00 -07002782
David Benjamine8d53502015-10-10 14:13:23 -04002783 CBB extensions;
2784 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002785 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002786 }
Adam Langley95c29f32014-06-20 12:00:00 -07002787
David Benjamin8c880a22016-12-03 02:20:34 -05002788 hs->extensions.sent = 0;
2789 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002790
David Benjamin54091232016-09-05 12:47:25 -04002791 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002792 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002793 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002794 }
2795 }
Adam Langley95c29f32014-06-20 12:00:00 -07002796
David Benjamin65ac9972016-09-02 21:35:25 -04002797 uint16_t grease_ext1 = 0;
2798 if (ssl->ctx->grease_enabled) {
2799 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2800 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2801 if (!CBB_add_u16(&extensions, grease_ext1) ||
2802 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2803 goto err;
2804 }
2805 }
2806
David Benjamin54091232016-09-05 12:47:25 -04002807 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002808 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002809 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002810 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002811 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002812 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002813 }
Adam Langley95c29f32014-06-20 12:00:00 -07002814
Adam Langley33ad2b52015-07-20 17:43:53 -07002815 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002816 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002817 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002818 }
Adam Langley75712922014-10-10 16:23:43 -07002819
David Benjamin2bd19172016-11-17 16:47:15 +09002820 if (!custom_ext_add_clienthello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002821 goto err;
2822 }
2823
David Benjamin65ac9972016-09-02 21:35:25 -04002824 if (ssl->ctx->grease_enabled) {
2825 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2826 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2827
2828 /* The two fake extensions must not have the same value. GREASE values are
2829 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2830 * one. */
2831 if (grease_ext1 == grease_ext2) {
2832 grease_ext2 ^= 0x1010;
2833 }
2834
2835 if (!CBB_add_u16(&extensions, grease_ext2) ||
2836 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2837 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2838 goto err;
2839 }
2840 }
2841
David Benjamince079fd2016-08-02 16:22:34 -04002842 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002843 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002844 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002845 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002846 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002847 *
2848 * NB: because this code works out the length of all existing extensions
2849 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002850 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002851 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002852 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002853 * Server 7.0 is intolerant to the last extension being zero-length. See
2854 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002855 if (padding_len >= 4 + 1) {
2856 padding_len -= 4;
2857 } else {
2858 padding_len = 1;
2859 }
Adam Langley95c29f32014-06-20 12:00:00 -07002860
Adam Langley33ad2b52015-07-20 17:43:53 -07002861 uint8_t *padding_bytes;
2862 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2863 !CBB_add_u16(&extensions, padding_len) ||
2864 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2865 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002866 }
Adam Langley75712922014-10-10 16:23:43 -07002867
David Benjamin17cf2cb2016-12-13 01:07:13 -05002868 OPENSSL_memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002869 }
2870 }
Adam Langley75712922014-10-10 16:23:43 -07002871
Steven Valdeza833c352016-11-01 13:39:36 -04002872 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002873 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002874 goto err;
2875 }
2876
David Benjamina01deee2015-12-08 18:56:31 -05002877 /* Discard empty extensions blocks. */
2878 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002879 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002880 }
2881
David Benjamine8d53502015-10-10 14:13:23 -04002882 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002883
2884err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002885 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002886 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002887}
Adam Langley95c29f32014-06-20 12:00:00 -07002888
David Benjamin8c880a22016-12-03 02:20:34 -05002889int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2890 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002891 CBB extensions;
2892 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002893 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002894 }
2895
David Benjamin8c880a22016-12-03 02:20:34 -05002896 for (unsigned i = 0; i < kNumExtensions; i++) {
2897 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002898 /* Don't send extensions that were not received. */
2899 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002900 }
Adam Langley95c29f32014-06-20 12:00:00 -07002901
David Benjamin8c880a22016-12-03 02:20:34 -05002902 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002903 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002904 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002905 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002906 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002907 }
Adam Langley95c29f32014-06-20 12:00:00 -07002908
David Benjamin2bd19172016-11-17 16:47:15 +09002909 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002910 goto err;
2911 }
2912
Steven Valdez143e8b32016-07-11 13:19:03 -04002913 /* Discard empty extensions blocks before TLS 1.3. */
2914 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2915 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002916 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002917 }
2918
David Benjamin56380462015-10-10 14:59:09 -04002919 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002920
2921err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002922 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002923 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002924}
Adam Langley95c29f32014-06-20 12:00:00 -07002925
David Benjamin731058e2016-12-03 23:15:13 -05002926static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2927 const SSL_CLIENT_HELLO *client_hello,
2928 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002929 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002930 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002931 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002932 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002933 }
2934 }
2935
David Benjamin8c880a22016-12-03 02:20:34 -05002936 hs->extensions.received = 0;
2937 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002938
David Benjamine14ff062016-08-09 16:21:24 -04002939 CBS extensions;
2940 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2941 while (CBS_len(&extensions) != 0) {
2942 uint16_t type;
2943 CBS extension;
2944
2945 /* Decode the next extension. */
2946 if (!CBS_get_u16(&extensions, &type) ||
2947 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002948 *out_alert = SSL_AD_DECODE_ERROR;
2949 return 0;
2950 }
Adam Langley95c29f32014-06-20 12:00:00 -07002951
David Benjamine14ff062016-08-09 16:21:24 -04002952 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2953 * ambiguous. Ignore all but the renegotiation_info extension. */
2954 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2955 continue;
2956 }
Adam Langley95c29f32014-06-20 12:00:00 -07002957
David Benjamine14ff062016-08-09 16:21:24 -04002958 unsigned ext_index;
2959 const struct tls_extension *const ext =
2960 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002961
David Benjamine14ff062016-08-09 16:21:24 -04002962 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002963 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002964 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002965 return 0;
2966 }
David Benjamine14ff062016-08-09 16:21:24 -04002967 continue;
2968 }
2969
David Benjamin8c880a22016-12-03 02:20:34 -05002970 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002971 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002972 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002973 *out_alert = alert;
2974 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002975 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002976 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002977 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002978 }
Adam Langley75712922014-10-10 16:23:43 -07002979
David Benjamin1deb41b2016-08-09 19:36:38 -04002980 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002981 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002982 continue;
2983 }
2984
2985 CBS *contents = NULL, fake_contents;
2986 static const uint8_t kFakeRenegotiateExtension[] = {0};
2987 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2988 ssl_client_cipher_list_contains_cipher(client_hello,
2989 SSL3_CK_SCSV & 0xffff)) {
2990 /* The renegotiation SCSV was received so pretend that we received a
2991 * renegotiation extension. */
2992 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2993 sizeof(kFakeRenegotiateExtension));
2994 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002995 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002996 }
2997
2998 /* Extension wasn't observed so call the callback with a NULL
2999 * parameter. */
3000 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003001 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04003002 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003003 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04003004 *out_alert = alert;
3005 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07003006 }
3007 }
3008
Adam Langleyfcf25832014-12-18 17:42:32 -08003009 return 1;
3010}
Adam Langley95c29f32014-06-20 12:00:00 -07003011
David Benjamin731058e2016-12-03 23:15:13 -05003012int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
3013 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05003014 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003015 int alert = -1;
David Benjamin8c880a22016-12-03 02:20:34 -05003016 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003017 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003018 return 0;
3019 }
Adam Langley95c29f32014-06-20 12:00:00 -07003020
David Benjamin8c880a22016-12-03 02:20:34 -05003021 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04003022 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08003023 return 0;
3024 }
Adam Langley95c29f32014-06-20 12:00:00 -07003025
Adam Langleyfcf25832014-12-18 17:42:32 -08003026 return 1;
3027}
Adam Langley95c29f32014-06-20 12:00:00 -07003028
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07003029OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
3030
David Benjamin8c880a22016-12-03 02:20:34 -05003031static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
3032 int *out_alert) {
3033 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04003034 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
3035 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
3036 return 1;
3037 }
Adam Langley614c66a2015-06-12 15:26:58 -07003038
Steven Valdez143e8b32016-07-11 13:19:03 -04003039 /* Decode the extensions block and check it is valid. */
3040 CBS extensions;
3041 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
3042 !tls1_check_duplicate_extensions(&extensions)) {
3043 *out_alert = SSL_AD_DECODE_ERROR;
3044 return 0;
3045 }
3046
3047 uint32_t received = 0;
3048 while (CBS_len(&extensions) != 0) {
3049 uint16_t type;
3050 CBS extension;
3051
3052 /* Decode the next extension. */
3053 if (!CBS_get_u16(&extensions, &type) ||
3054 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003055 *out_alert = SSL_AD_DECODE_ERROR;
3056 return 0;
3057 }
Adam Langley95c29f32014-06-20 12:00:00 -07003058
Steven Valdez143e8b32016-07-11 13:19:03 -04003059 unsigned ext_index;
3060 const struct tls_extension *const ext =
3061 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07003062
Steven Valdez143e8b32016-07-11 13:19:03 -04003063 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09003064 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003065 return 0;
3066 }
Steven Valdez143e8b32016-07-11 13:19:03 -04003067 continue;
3068 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003069
David Benjamin8c880a22016-12-03 02:20:34 -05003070 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04003071 type != TLSEXT_TYPE_renegotiate) {
3072 /* If the extension was never sent then it is illegal, except for the
3073 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04003074 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3075 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04003076 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04003077 return 0;
3078 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003079
Steven Valdez143e8b32016-07-11 13:19:03 -04003080 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07003081
Steven Valdez143e8b32016-07-11 13:19:03 -04003082 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003083 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04003084 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003085 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04003086 *out_alert = alert;
3087 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07003088 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003089 }
Adam Langley95c29f32014-06-20 12:00:00 -07003090
David Benjamin54091232016-09-05 12:47:25 -04003091 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003092 if (!(received & (1u << i))) {
3093 /* Extension wasn't observed so call the callback with a NULL
3094 * parameter. */
3095 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003096 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003097 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003098 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07003099 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08003100 return 0;
3101 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003102 }
3103 }
Adam Langley95c29f32014-06-20 12:00:00 -07003104
Adam Langleyfcf25832014-12-18 17:42:32 -08003105 return 1;
3106}
Adam Langley95c29f32014-06-20 12:00:00 -07003107
David Benjamin8c880a22016-12-03 02:20:34 -05003108static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3109 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003110 int ret = SSL_TLSEXT_ERR_NOACK;
3111 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003112
David Benjamin78f8aab2016-03-10 16:33:58 -05003113 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003114 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003115 ssl->ctx->tlsext_servername_arg);
3116 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003117 ret = ssl->initial_ctx->tlsext_servername_callback(
3118 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003119 }
Adam Langley95c29f32014-06-20 12:00:00 -07003120
Adam Langleyfcf25832014-12-18 17:42:32 -08003121 switch (ret) {
3122 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003123 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003124 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003125
Adam Langleyfcf25832014-12-18 17:42:32 -08003126 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003127 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003128 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003129
Adam Langleyfcf25832014-12-18 17:42:32 -08003130 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003131 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003132 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003133
Adam Langleyfcf25832014-12-18 17:42:32 -08003134 default:
3135 return 1;
3136 }
3137}
Adam Langleyed8270a2014-09-02 13:52:56 -07003138
David Benjamin8c880a22016-12-03 02:20:34 -05003139static int ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs) {
3140 SSL *const ssl = hs->ssl;
David Benjaminfc059942015-07-30 23:01:59 -04003141 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08003142 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07003143
David Benjamin78f8aab2016-03-10 16:33:58 -05003144 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003145 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003146 ssl->ctx->tlsext_servername_arg);
3147 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003148 ret = ssl->initial_ctx->tlsext_servername_callback(
3149 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003150 }
Adam Langley95c29f32014-06-20 12:00:00 -07003151
Adam Langleyfcf25832014-12-18 17:42:32 -08003152 switch (ret) {
3153 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003154 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003155 return -1;
David Benjamin03973092014-06-24 23:27:17 -04003156
Adam Langleyfcf25832014-12-18 17:42:32 -08003157 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003158 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003159 return 1;
3160
3161 default:
3162 return 1;
3163 }
3164}
3165
David Benjamin8c880a22016-12-03 02:20:34 -05003166int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3167 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003168 int alert = -1;
David Benjamin8c880a22016-12-03 02:20:34 -05003169 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003170 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003171 return 0;
3172 }
3173
David Benjamin8c880a22016-12-03 02:20:34 -05003174 if (ssl_check_serverhello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04003175 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08003176 return 0;
3177 }
3178
3179 return 1;
3180}
Adam Langley95c29f32014-06-20 12:00:00 -07003181
David Benjamine3aa1d92015-06-16 15:34:50 -04003182int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05003183 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04003184 size_t ticket_len, const uint8_t *session_id,
3185 size_t session_id_len) {
3186 int ret = 1; /* Most errors are non-fatal. */
3187 SSL_CTX *ssl_ctx = ssl->initial_ctx;
3188 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003189
David Benjamine3aa1d92015-06-16 15:34:50 -04003190 HMAC_CTX hmac_ctx;
3191 HMAC_CTX_init(&hmac_ctx);
3192 EVP_CIPHER_CTX cipher_ctx;
3193 EVP_CIPHER_CTX_init(&cipher_ctx);
3194
David Benjaminef1b0092015-11-21 14:05:44 -05003195 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04003196 *out_session = NULL;
3197
Steven Valdez4aa154e2016-07-29 14:32:55 -04003198 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
3199 goto done;
3200 }
3201
David Benjamine3aa1d92015-06-16 15:34:50 -04003202 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3203 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003204 }
3205
David Benjaminadcc3952015-04-26 13:07:57 -04003206 /* Ensure there is room for the key name and the largest IV
3207 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3208 * the maximum IV length should be well under the minimum size for the
3209 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003210 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3211 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003212 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003213 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003214
David Benjamine3aa1d92015-06-16 15:34:50 -04003215 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003216 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3217 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3218 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003219 if (cb_ret < 0) {
3220 ret = 0;
3221 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003222 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003223 if (cb_ret == 0) {
3224 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003225 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003226 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003227 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003228 }
3229 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003230 /* Check the key name matches. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003231 if (OPENSSL_memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3232 SSL_TICKET_KEY_NAME_LEN) != 0) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003233 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003234 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003235 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3236 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003237 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003238 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3239 ssl_ctx->tlsext_tick_aes_key, iv)) {
3240 ret = 0;
3241 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003242 }
3243 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003244 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003245
David Benjamine3aa1d92015-06-16 15:34:50 -04003246 /* Check the MAC at the end of the ticket. */
3247 uint8_t mac[EVP_MAX_MD_SIZE];
3248 size_t mac_len = HMAC_size(&hmac_ctx);
3249 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003250 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003251 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003252 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003253 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3254 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003255 int mac_ok =
3256 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3257#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3258 mac_ok = 1;
3259#endif
3260 if (!mac_ok) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003261 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003262 }
3263
David Benjamine3aa1d92015-06-16 15:34:50 -04003264 /* Decrypt the session data. */
3265 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3266 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3267 mac_len;
3268 plaintext = OPENSSL_malloc(ciphertext_len);
3269 if (plaintext == NULL) {
3270 ret = 0;
3271 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003272 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003273 size_t plaintext_len;
3274#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin17cf2cb2016-12-13 01:07:13 -05003275 OPENSSL_memcpy(plaintext, ciphertext, ciphertext_len);
David Benjaminfbc45d72016-09-22 01:21:24 -04003276 plaintext_len = ciphertext_len;
3277#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003278 if (ciphertext_len >= INT_MAX) {
3279 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003280 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003281 int len1, len2;
3282 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3283 (int)ciphertext_len) ||
3284 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
3285 ERR_clear_error(); /* Don't leave an error on the queue. */
3286 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003287 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003288 plaintext_len = (size_t)(len1 + len2);
3289#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003290
David Benjamine3aa1d92015-06-16 15:34:50 -04003291 /* Decode the session. */
David Benjaminfbc45d72016-09-22 01:21:24 -04003292 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003293 if (session == NULL) {
3294 ERR_clear_error(); /* Don't leave an error on the queue. */
3295 goto done;
3296 }
3297
3298 /* Copy the client's session ID into the new session, to denote the ticket has
3299 * been accepted. */
David Benjamin17cf2cb2016-12-13 01:07:13 -05003300 OPENSSL_memcpy(session->session_id, session_id, session_id_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003301 session->session_id_length = session_id_len;
3302
3303 *out_session = session;
3304
3305done:
3306 OPENSSL_free(plaintext);
3307 HMAC_CTX_cleanup(&hmac_ctx);
3308 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3309 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003310}
Adam Langley95c29f32014-06-20 12:00:00 -07003311
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003312int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003313 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003314 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003315 return 1;
3316 }
David Benjamincd996942014-07-20 16:23:51 -04003317
David Benjamin0fc37ef2016-08-17 15:29:46 -04003318 OPENSSL_free(hs->peer_sigalgs);
3319 hs->peer_sigalgs = NULL;
3320 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003321
3322 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003323 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003324 return 0;
3325 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003326 num_sigalgs /= 2;
3327
3328 /* supported_signature_algorithms in the certificate request is
3329 * allowed to be empty. */
3330 if (num_sigalgs == 0) {
3331 return 1;
3332 }
3333
Steven Valdez02563852016-06-23 13:33:05 -04003334 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3335 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003336 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3337 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003338 return 0;
3339 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003340 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003341
3342 CBS sigalgs;
3343 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003344 for (size_t i = 0; i < num_sigalgs; i++) {
3345 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003346 return 0;
3347 }
3348 }
Adam Langley95c29f32014-06-20 12:00:00 -07003349
Adam Langleyfcf25832014-12-18 17:42:32 -08003350 return 1;
3351}
David Benjaminec2f27d2014-11-13 19:17:25 -05003352
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003353int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3354 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003355 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003356
Steven Valdezf0451ca2016-06-29 13:16:27 -04003357 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3358 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3359 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003360 int type = ssl_private_key_type(ssl);
3361 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003362 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003363 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003364 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003365 if (ssl_is_ecdsa_key_type(type)) {
3366 *out = SSL_SIGN_ECDSA_SHA1;
3367 return 1;
3368 }
3369 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3370 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003371 }
3372
David Benjamin3ef76972016-10-17 17:59:54 -04003373 const uint16_t *sigalgs = cert->sigalgs;
3374 size_t num_sigalgs = cert->num_sigalgs;
3375 if (sigalgs == NULL) {
3376 sigalgs = kSignSignatureAlgorithms;
3377 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003378 }
3379
David Benjamin0fc37ef2016-08-17 15:29:46 -04003380 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3381 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3382 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003383 /* If the client didn't specify any signature_algorithms extension then
3384 * we can assume that it supports SHA1. See
3385 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3386 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3387 SSL_SIGN_ECDSA_SHA1};
3388 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003389 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003390 }
3391
David Benjamin0fc37ef2016-08-17 15:29:46 -04003392 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003393 uint16_t sigalg = sigalgs[i];
3394 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3395 * negotiated. */
3396 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3397 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3398 continue;
3399 }
3400
David Benjamin0fc37ef2016-08-17 15:29:46 -04003401 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003402 if (sigalg == peer_sigalgs[j]) {
3403 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003404 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003405 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003406 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003407 }
Adam Langley95c29f32014-06-20 12:00:00 -07003408
David Benjaminea9a0d52016-07-08 15:52:59 -07003409 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3410 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003411}
Adam Langley95c29f32014-06-20 12:00:00 -07003412
Nick Harper60a85cb2016-09-23 16:25:11 -07003413int tls1_verify_channel_id(SSL *ssl) {
3414 int ret = 0;
3415 uint16_t extension_type;
3416 CBS extension, channel_id;
3417
3418 /* A Channel ID handshake message is structured to contain multiple
3419 * extensions, but the only one that can be present is Channel ID. */
3420 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3421 if (!CBS_get_u16(&channel_id, &extension_type) ||
3422 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3423 CBS_len(&channel_id) != 0 ||
3424 extension_type != TLSEXT_TYPE_channel_id ||
3425 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3426 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3427 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3428 return 0;
3429 }
3430
3431 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3432 if (!p256) {
3433 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3434 return 0;
3435 }
3436
3437 EC_KEY *key = NULL;
3438 EC_POINT *point = NULL;
3439 BIGNUM x, y;
3440 ECDSA_SIG sig;
3441 BN_init(&x);
3442 BN_init(&y);
3443 sig.r = BN_new();
3444 sig.s = BN_new();
3445 if (sig.r == NULL || sig.s == NULL) {
3446 goto err;
3447 }
3448
3449 const uint8_t *p = CBS_data(&extension);
3450 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3451 BN_bin2bn(p + 32, 32, &y) == NULL ||
3452 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3453 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3454 goto err;
3455 }
3456
3457 point = EC_POINT_new(p256);
3458 if (point == NULL ||
3459 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3460 goto err;
3461 }
3462
3463 key = EC_KEY_new();
3464 if (key == NULL ||
3465 !EC_KEY_set_group(key, p256) ||
3466 !EC_KEY_set_public_key(key, point)) {
3467 goto err;
3468 }
3469
3470 uint8_t digest[EVP_MAX_MD_SIZE];
3471 size_t digest_len;
3472 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3473 goto err;
3474 }
3475
3476 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3477#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3478 sig_ok = 1;
3479#endif
3480 if (!sig_ok) {
3481 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3482 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3483 ssl->s3->tlsext_channel_id_valid = 0;
3484 goto err;
3485 }
3486
David Benjamin17cf2cb2016-12-13 01:07:13 -05003487 OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
Nick Harper60a85cb2016-09-23 16:25:11 -07003488 ret = 1;
3489
3490err:
3491 BN_free(&x);
3492 BN_free(&y);
3493 BN_free(sig.r);
3494 BN_free(sig.s);
3495 EC_KEY_free(key);
3496 EC_POINT_free(point);
3497 EC_GROUP_free(p256);
3498 return ret;
3499}
3500
3501int tls1_write_channel_id(SSL *ssl, CBB *cbb) {
3502 uint8_t digest[EVP_MAX_MD_SIZE];
3503 size_t digest_len;
3504 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3505 return 0;
3506 }
3507
3508 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3509 if (ec_key == NULL) {
3510 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3511 return 0;
3512 }
3513
3514 int ret = 0;
3515 BIGNUM *x = BN_new();
3516 BIGNUM *y = BN_new();
3517 ECDSA_SIG *sig = NULL;
3518 if (x == NULL || y == NULL ||
3519 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3520 EC_KEY_get0_public_key(ec_key),
3521 x, y, NULL)) {
3522 goto err;
3523 }
3524
3525 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3526 if (sig == NULL) {
3527 goto err;
3528 }
3529
3530 CBB child;
3531 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3532 !CBB_add_u16_length_prefixed(cbb, &child) ||
3533 !BN_bn2cbb_padded(&child, 32, x) ||
3534 !BN_bn2cbb_padded(&child, 32, y) ||
3535 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3536 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3537 !CBB_flush(cbb)) {
3538 goto err;
3539 }
3540
3541 ret = 1;
3542
3543err:
3544 BN_free(x);
3545 BN_free(y);
3546 ECDSA_SIG_free(sig);
3547 return ret;
3548}
3549
David Benjamind6a4ae92015-08-06 11:10:51 -04003550int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003551 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3552 uint8_t *msg;
3553 size_t msg_len;
3554 if (!tls13_get_cert_verify_signature_input(ssl, &msg, &msg_len,
3555 ssl_cert_verify_channel_id)) {
3556 return 0;
3557 }
3558 SHA256(msg, msg_len, out);
3559 *out_len = SHA256_DIGEST_LENGTH;
3560 OPENSSL_free(msg);
3561 return 1;
3562 }
3563
Nick Harper95594012016-10-20 14:07:13 -07003564 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003565
Nick Harper95594012016-10-20 14:07:13 -07003566 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003567 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003568 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003569
Steven Valdez87eab492016-06-27 16:34:59 -04003570 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003571 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003572 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003573 if (ssl->session->original_handshake_hash_len == 0) {
3574 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003575 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003576 }
Nick Harper95594012016-10-20 14:07:13 -07003577 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3578 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003579 }
3580
3581 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3582 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3583 sizeof(handshake_hash));
3584 if (handshake_hash_len < 0) {
Nick Harper95594012016-10-20 14:07:13 -07003585 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003586 }
Nick Harper95594012016-10-20 14:07:13 -07003587 SHA256_Update(&ctx, handshake_hash, (size_t)handshake_hash_len);
3588 SHA256_Final(out, &ctx);
3589 *out_len = SHA256_DIGEST_LENGTH;
3590 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003591}
Adam Langley1258b6a2014-06-20 12:00:00 -07003592
3593/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003594 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003595 * data. */
3596int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003597 int digest_len;
3598 /* This function should never be called for a resumed session because the
3599 * handshake hashes that we wish to record are for the original, full
3600 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003601 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003602 return -1;
3603 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003604
Adam Langleyfcf25832014-12-18 17:42:32 -08003605 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003606 tls1_handshake_digest(
3607 ssl, ssl->s3->new_session->original_handshake_hash,
3608 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003609 if (digest_len < 0) {
3610 return -1;
3611 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003612
David Benjamin30c4c302016-12-07 22:35:24 -05003613 assert(sizeof(ssl->s3->new_session->original_handshake_hash) < 256);
3614 ssl->s3->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003615
Adam Langleyfcf25832014-12-18 17:42:32 -08003616 return 1;
3617}
Nick Harper60a85cb2016-09-23 16:25:11 -07003618
3619int ssl_do_channel_id_callback(SSL *ssl) {
3620 if (ssl->tlsext_channel_id_private != NULL ||
3621 ssl->ctx->channel_id_cb == NULL) {
3622 return 1;
3623 }
3624
3625 EVP_PKEY *key = NULL;
3626 ssl->ctx->channel_id_cb(ssl, &key);
3627 if (key == NULL) {
3628 /* The caller should try again later. */
3629 return 1;
3630 }
3631
3632 int ret = SSL_set1_tls_channel_id(ssl, key);
3633 EVP_PKEY_free(key);
3634 return ret;
3635}
Adam Langleycfa08c32016-11-17 13:21:27 -08003636
3637int ssl_is_sct_list_valid(const CBS *contents) {
3638 /* Shallow parse the SCT list for sanity. By the RFC
3639 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3640 * of the SCTs may be empty. */
3641 CBS copy = *contents;
3642 CBS sct_list;
3643 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3644 CBS_len(&copy) != 0 ||
3645 CBS_len(&sct_list) == 0) {
3646 return 0;
3647 }
3648
3649 while (CBS_len(&sct_list) > 0) {
3650 CBS sct;
3651 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3652 CBS_len(&sct) == 0) {
3653 return 0;
3654 }
3655 }
3656
3657 return 1;
3658}