blob: 8d91cfc40ac46ed3e30832c20107b22eb604c31c [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin03973092014-06-24 23:27:17 -0400116#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400117#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/evp.h>
120#include <openssl/hmac.h>
121#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400122#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/rand.h>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700124#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400127#include "../crypto/internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800128
129
David Benjamin8c880a22016-12-03 02:20:34 -0500130static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
131static int ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs);
Adam Langley95c29f32014-06-20 12:00:00 -0700132
Adam Langleyfcf25832014-12-18 17:42:32 -0800133static int compare_uint16_t(const void *p1, const void *p2) {
134 uint16_t u1 = *((const uint16_t *)p1);
135 uint16_t u2 = *((const uint16_t *)p2);
136 if (u1 < u2) {
137 return -1;
138 } else if (u1 > u2) {
139 return 1;
140 } else {
141 return 0;
142 }
143}
David Benjamin35a7a442014-07-05 00:23:20 -0400144
Adam Langleyfcf25832014-12-18 17:42:32 -0800145/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
146 * more than one extension of the same type in a ClientHello or ServerHello.
147 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400148 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800149static int tls1_check_duplicate_extensions(const CBS *cbs) {
150 CBS extensions = *cbs;
151 size_t num_extensions = 0, i = 0;
152 uint16_t *extension_types = NULL;
153 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400154
Adam Langleyfcf25832014-12-18 17:42:32 -0800155 /* First pass: count the extensions. */
156 while (CBS_len(&extensions) > 0) {
157 uint16_t type;
158 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400159
Adam Langleyfcf25832014-12-18 17:42:32 -0800160 if (!CBS_get_u16(&extensions, &type) ||
161 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
162 goto done;
163 }
David Benjamin35a7a442014-07-05 00:23:20 -0400164
Adam Langleyfcf25832014-12-18 17:42:32 -0800165 num_extensions++;
166 }
David Benjamin35a7a442014-07-05 00:23:20 -0400167
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 if (num_extensions == 0) {
169 return 1;
170 }
David Benjamin9a373592014-07-25 04:27:53 -0400171
Brian Smith5ba06892016-02-07 09:36:04 -1000172 extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400174 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800175 goto done;
176 }
David Benjamin35a7a442014-07-05 00:23:20 -0400177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 /* Second pass: gather the extension types. */
179 extensions = *cbs;
180 for (i = 0; i < num_extensions; i++) {
181 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400182
Adam Langleyfcf25832014-12-18 17:42:32 -0800183 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
184 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
185 /* This should not happen. */
186 goto done;
187 }
188 }
189 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400190
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 /* Sort the extensions and make sure there are no duplicates. */
192 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
193 for (i = 1; i < num_extensions; i++) {
194 if (extension_types[i - 1] == extension_types[i]) {
195 goto done;
196 }
197 }
David Benjamin35a7a442014-07-05 00:23:20 -0400198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 ret = 1;
200
David Benjamin35a7a442014-07-05 00:23:20 -0400201done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400202 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 return ret;
204}
David Benjamin35a7a442014-07-05 00:23:20 -0400205
David Benjamin731058e2016-12-03 23:15:13 -0500206int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out, const uint8_t *in,
207 size_t in_len) {
208 memset(out, 0, sizeof(*out));
209 out->ssl = ssl;
210 out->client_hello = in;
211 out->client_hello_len = in_len;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400212
David Benjamine14ff062016-08-09 16:21:24 -0400213 CBS client_hello, random, session_id;
David Benjamin731058e2016-12-03 23:15:13 -0500214 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
215 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjamine14ff062016-08-09 16:21:24 -0400216 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
217 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
218 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 return 0;
220 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700221
David Benjamin731058e2016-12-03 23:15:13 -0500222 out->random = CBS_data(&random);
223 out->random_len = CBS_len(&random);
224 out->session_id = CBS_data(&session_id);
225 out->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700226
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 /* Skip past DTLS cookie */
David Benjamin731058e2016-12-03 23:15:13 -0500228 if (SSL_is_dtls(out->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400230 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
231 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 return 0;
233 }
234 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700235
David Benjamine14ff062016-08-09 16:21:24 -0400236 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400238 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
239 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 CBS_len(&compression_methods) < 1) {
241 return 0;
242 }
David Benjamine14ff062016-08-09 16:21:24 -0400243
David Benjamin731058e2016-12-03 23:15:13 -0500244 out->cipher_suites = CBS_data(&cipher_suites);
245 out->cipher_suites_len = CBS_len(&cipher_suites);
246 out->compression_methods = CBS_data(&compression_methods);
247 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 /* If the ClientHello ends here then it's valid, but doesn't have any
250 * extensions. (E.g. SSLv3.) */
251 if (CBS_len(&client_hello) == 0) {
David Benjamin731058e2016-12-03 23:15:13 -0500252 out->extensions = NULL;
253 out->extensions_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 return 1;
255 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 /* Extract extensions and check it is valid. */
David Benjamine14ff062016-08-09 16:21:24 -0400258 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
260 !tls1_check_duplicate_extensions(&extensions) ||
261 CBS_len(&client_hello) != 0) {
262 return 0;
263 }
David Benjamine14ff062016-08-09 16:21:24 -0400264
David Benjamin731058e2016-12-03 23:15:13 -0500265 out->extensions = CBS_data(&extensions);
266 out->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700267
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 return 1;
269}
Adam Langleydc9b1412014-06-20 12:00:00 -0700270
David Benjamin731058e2016-12-03 23:15:13 -0500271int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
272 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 CBS extensions;
David Benjamin731058e2016-12-03 23:15:13 -0500274 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 while (CBS_len(&extensions) != 0) {
David Benjamincec73442016-08-02 17:41:33 -0400276 /* Decode the next extension. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 uint16_t type;
278 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 if (!CBS_get_u16(&extensions, &type) ||
280 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
281 return 0;
282 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400285 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800286 return 1;
287 }
288 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700289
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 return 0;
291}
Adam Langley95c29f32014-06-20 12:00:00 -0700292
David Benjamin731058e2016-12-03 23:15:13 -0500293int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
294 uint16_t extension_type,
295 const uint8_t **out_data,
296 size_t *out_len) {
David Benjamincec73442016-08-02 17:41:33 -0400297 CBS cbs;
David Benjamin731058e2016-12-03 23:15:13 -0500298 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
David Benjamincec73442016-08-02 17:41:33 -0400299 return 0;
300 }
301
302 *out_data = CBS_data(&cbs);
303 *out_len = CBS_len(&cbs);
304 return 1;
305}
306
Steven Valdezce902a92016-05-17 11:47:53 -0400307static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400308 SSL_CURVE_X25519,
309 SSL_CURVE_SECP256R1,
310 SSL_CURVE_SECP384R1,
Adam Langleyfcf25832014-12-18 17:42:32 -0800311};
Adam Langley95c29f32014-06-20 12:00:00 -0700312
David Benjaminf04976b2016-10-07 00:37:55 -0400313void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
Steven Valdez5440fe02016-07-18 12:40:30 -0400314 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400315 *out_group_ids = ssl->supported_group_list;
316 *out_group_ids_len = ssl->supported_group_list_len;
317 if (!*out_group_ids) {
318 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400319 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 }
321}
David Benjamined439582014-07-14 19:13:02 -0400322
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900323int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
324 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400325 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400326
David Benjaminf04976b2016-10-07 00:37:55 -0400327 const uint16_t *groups, *pref, *supp;
328 size_t groups_len, pref_len, supp_len;
329 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800330
David Benjaminf04976b2016-10-07 00:37:55 -0400331 /* Clients are not required to send a supported_groups extension. In this
332 * case, the server is free to pick any group it likes. See RFC 4492,
333 * section 4, paragraph 3.
334 *
335 * However, in the interests of compatibility, we will skip ECDH if the
336 * client didn't send an extension because we can't be sure that they'll
337 * support our favoured group. Thus we do not special-case an emtpy
338 * |peer_supported_group_list|. */
David Benjamin55a43642015-04-20 14:45:55 -0400339
David Benjamin4298d772015-12-19 00:18:25 -0500340 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400341 pref = groups;
342 pref_len = groups_len;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900343 supp = hs->peer_supported_group_list;
344 supp_len = hs->peer_supported_group_list_len;
David Benjamin55a43642015-04-20 14:45:55 -0400345 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900346 pref = hs->peer_supported_group_list;
347 pref_len = hs->peer_supported_group_list_len;
Steven Valdezce902a92016-05-17 11:47:53 -0400348 supp = groups;
349 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400350 }
351
David Benjaminf04976b2016-10-07 00:37:55 -0400352 for (size_t i = 0; i < pref_len; i++) {
353 for (size_t j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400355 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500356 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 }
358 }
359 }
360
David Benjamin4298d772015-12-19 00:18:25 -0500361 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800362}
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Steven Valdezce902a92016-05-17 11:47:53 -0400364int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400366 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800367
Steven Valdezce902a92016-05-17 11:47:53 -0400368 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
369 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 return 0;
371 }
372
David Benjamin54091232016-09-05 12:47:25 -0400373 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400374 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
375 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 return 0;
377 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 }
379
Steven Valdezce902a92016-05-17 11:47:53 -0400380 OPENSSL_free(*out_group_ids);
381 *out_group_ids = group_ids;
382 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800383
384 return 1;
385}
Adam Langley95c29f32014-06-20 12:00:00 -0700386
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100387int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
388 const char *curves) {
389 uint16_t *group_ids = NULL;
390 size_t ncurves = 0;
391
392 const char *col;
393 const char *ptr = curves;
394
395 do {
396 col = strchr(ptr, ':');
397
398 uint16_t group_id;
399 if (!ssl_name_to_group_id(&group_id, ptr,
400 col ? (size_t)(col - ptr) : strlen(ptr))) {
401 goto err;
402 }
403
404 uint16_t *new_group_ids = OPENSSL_realloc(group_ids,
405 (ncurves + 1) * sizeof(uint16_t));
406 if (new_group_ids == NULL) {
407 goto err;
408 }
409 group_ids = new_group_ids;
410
411 group_ids[ncurves] = group_id;
412 ncurves++;
413
414 if (col) {
415 ptr = col + 1;
416 }
417 } while (col);
418
419 OPENSSL_free(*out_group_ids);
420 *out_group_ids = group_ids;
421 *out_group_ids_len = ncurves;
422
423 return 1;
424
425err:
426 OPENSSL_free(group_ids);
427 return 0;
428}
429
Steven Valdezce902a92016-05-17 11:47:53 -0400430int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
431 const uint16_t *groups;
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400432 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -0400433 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400434 for (size_t i = 0; i < groups_len; i++) {
435 if (groups[i] == group_id) {
436 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800438 }
David Benjamin033e5f42014-11-13 18:47:41 -0500439
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400440 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800441}
David Benjamin033e5f42014-11-13 18:47:41 -0500442
David Benjamin3ef76972016-10-17 17:59:54 -0400443/* kVerifySignatureAlgorithms is the default list of accepted signature
David Benjamin3a322f52016-10-26 12:45:35 -0400444 * algorithms for verifying.
445 *
446 * For now, RSA-PSS signature algorithms are not enabled on Android's system
447 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
448 * restore them. */
David Benjamin3ef76972016-10-17 17:59:54 -0400449static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjamin3a322f52016-10-26 12:45:35 -0400450 /* Prefer SHA-256 algorithms. */
451 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400452#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400453 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400454#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400455 SSL_SIGN_RSA_PKCS1_SHA256,
456
457 /* Larger hashes are acceptable. */
458 SSL_SIGN_ECDSA_SECP384R1_SHA384,
459#if !defined(BORINGSSL_ANDROID_SYSTEM)
460 SSL_SIGN_RSA_PSS_SHA384,
461#endif
462 SSL_SIGN_RSA_PKCS1_SHA384,
463
David Benjamin76bb1412016-09-08 16:03:49 -0400464 /* TODO(davidben): Remove this. */
David Benjamin3ef76972016-10-17 17:59:54 -0400465#if defined(BORINGSSL_ANDROID_SYSTEM)
466 SSL_SIGN_ECDSA_SECP521R1_SHA512,
467#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400468#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400469 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400470#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400471 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400472
David Benjamin3a322f52016-10-26 12:45:35 -0400473 /* For now, SHA-1 is still accepted but least preferable. */
474 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400475
David Benjamin3a322f52016-10-26 12:45:35 -0400476};
477
478/* kSignSignatureAlgorithms is the default list of supported signature
479 * algorithms for signing.
480 *
481 * For now, RSA-PSS signature algorithms are not enabled on Android's system
482 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
483 * restore them. */
484static const uint16_t kSignSignatureAlgorithms[] = {
485 /* Prefer SHA-256 algorithms. */
486 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400487#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400488 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400489#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400490 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400491
David Benjamin3a322f52016-10-26 12:45:35 -0400492 /* If needed, sign larger hashes.
493 *
494 * TODO(davidben): Determine which of these may be pruned. */
495 SSL_SIGN_ECDSA_SECP384R1_SHA384,
496#if !defined(BORINGSSL_ANDROID_SYSTEM)
497 SSL_SIGN_RSA_PSS_SHA384,
498#endif
499 SSL_SIGN_RSA_PKCS1_SHA384,
500
501 SSL_SIGN_ECDSA_SECP521R1_SHA512,
502#if !defined(BORINGSSL_ANDROID_SYSTEM)
503 SSL_SIGN_RSA_PSS_SHA512,
504#endif
505 SSL_SIGN_RSA_PKCS1_SHA512,
506
507 /* If the peer supports nothing else, sign with SHA-1. */
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400508 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400509 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400510};
511
David Benjamin3ef76972016-10-17 17:59:54 -0400512size_t tls12_get_verify_sigalgs(const SSL *ssl, const uint16_t **out) {
513 *out = kVerifySignatureAlgorithms;
514 return OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
Adam Langleyfcf25832014-12-18 17:42:32 -0800515}
Adam Langley95c29f32014-06-20 12:00:00 -0700516
David Benjamin887c3002016-07-08 16:15:32 -0700517int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
David Benjamin3ef76972016-10-17 17:59:54 -0400518 const uint16_t *verify_sigalgs;
519 size_t num_verify_sigalgs = tls12_get_verify_sigalgs(ssl, &verify_sigalgs);
520 for (size_t i = 0; i < num_verify_sigalgs; i++) {
521 if (sigalg == verify_sigalgs[i]) {
522 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800523 }
524 }
525
David Benjamin3ef76972016-10-17 17:59:54 -0400526 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
527 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
528 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800529}
530
531/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
532 * supported or doesn't appear in supported signature algorithms. Unlike
533 * ssl_cipher_get_disabled this applies to a specific session and not global
534 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500535void ssl_set_client_disabled(SSL *ssl) {
536 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800537 int have_rsa = 0, have_ecdsa = 0;
538 c->mask_a = 0;
539 c->mask_k = 0;
540
David Benjamin3ef76972016-10-17 17:59:54 -0400541 /* Now go through all signature algorithms seeing if we support any for RSA or
542 * ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin0fc37ef2016-08-17 15:29:46 -0400543 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -0400544 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
David Benjamin0fc37ef2016-08-17 15:29:46 -0400545 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -0400546 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400547 case SSL_SIGN_RSA_PSS_SHA512:
548 case SSL_SIGN_RSA_PSS_SHA384:
549 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400550 case SSL_SIGN_RSA_PKCS1_SHA512:
551 case SSL_SIGN_RSA_PKCS1_SHA384:
552 case SSL_SIGN_RSA_PKCS1_SHA256:
553 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800554 have_rsa = 1;
555 break;
556
Steven Valdez02563852016-06-23 13:33:05 -0400557 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
558 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
559 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
560 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 have_ecdsa = 1;
562 break;
563 }
564 }
565
566 /* Disable auth if we don't include any appropriate signature algorithms. */
567 if (!have_rsa) {
568 c->mask_a |= SSL_aRSA;
569 }
570 if (!have_ecdsa) {
571 c->mask_a |= SSL_aECDSA;
572 }
573
574 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500575 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 c->mask_a |= SSL_aPSK;
577 c->mask_k |= SSL_kPSK;
578 }
579}
Adam Langley95c29f32014-06-20 12:00:00 -0700580
Adam Langley614c66a2015-06-12 15:26:58 -0700581/* tls_extension represents a TLS extension that is handled internally. The
582 * |init| function is called for each handshake, before any other functions of
583 * the extension. Then the add and parse callbacks are called as needed.
584 *
585 * The parse callbacks receive a |CBS| that contains the contents of the
586 * extension (i.e. not including the type and length bytes). If an extension is
587 * not received then the parse callbacks will be called with a NULL CBS so that
588 * they can do any processing needed to handle the absence of an extension.
589 *
590 * The add callbacks receive a |CBB| to which the extension can be appended but
591 * the function is responsible for appending the type and length bytes too.
592 *
593 * All callbacks return one for success and zero for error. If a parse function
594 * returns zero then a fatal alert with value |*out_alert| will be sent. If
595 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
596struct tls_extension {
597 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500598 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700599
David Benjamin8c880a22016-12-03 02:20:34 -0500600 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
601 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
602 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700603
David Benjamin8c880a22016-12-03 02:20:34 -0500604 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
605 CBS *contents);
606 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700607};
608
David Benjamin8c880a22016-12-03 02:20:34 -0500609static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
610 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400611 if (contents != NULL) {
612 /* Servers MUST NOT send this extension. */
613 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
614 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
615 return 0;
616 }
617
618 return 1;
619}
620
David Benjamin8c880a22016-12-03 02:20:34 -0500621static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
622 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400623 /* This extension from the client is handled elsewhere. */
624 return 1;
625}
626
David Benjamin8c880a22016-12-03 02:20:34 -0500627static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400628 return 1;
629}
Adam Langley614c66a2015-06-12 15:26:58 -0700630
631/* Server name indication (SNI).
632 *
633 * https://tools.ietf.org/html/rfc6066#section-3. */
634
David Benjamin8c880a22016-12-03 02:20:34 -0500635static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
636 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700637 if (ssl->tlsext_hostname == NULL) {
638 return 1;
639 }
640
641 CBB contents, server_name_list, name;
642 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
643 !CBB_add_u16_length_prefixed(out, &contents) ||
644 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
645 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
646 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
647 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
648 strlen(ssl->tlsext_hostname)) ||
649 !CBB_flush(out)) {
650 return 0;
651 }
652
653 return 1;
654}
655
David Benjamin8c880a22016-12-03 02:20:34 -0500656static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500657 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500658 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700659 if (contents == NULL) {
660 return 1;
661 }
662
663 if (CBS_len(contents) != 0) {
664 return 0;
665 }
666
667 assert(ssl->tlsext_hostname != NULL);
668
Steven Valdez87eab492016-06-27 16:34:59 -0400669 if (ssl->session == NULL) {
670 assert(ssl->s3->new_session->tlsext_hostname == NULL);
671 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
672 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700673 *out_alert = SSL_AD_INTERNAL_ERROR;
674 return 0;
675 }
676 }
677
678 return 1;
679}
680
David Benjamin8c880a22016-12-03 02:20:34 -0500681static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500682 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700683 if (contents == NULL) {
684 return 1;
685 }
686
David Benjamin9b611e22016-03-03 08:48:30 -0500687 CBS server_name_list, host_name;
688 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700689 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500690 !CBS_get_u8(&server_name_list, &name_type) ||
691 /* Although the server_name extension was intended to be extensible to
692 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
693 * different name types will cause an error. Further, RFC 4366 originally
694 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
695 * adding new name types is no longer feasible.
696 *
697 * Act as if the extensibility does not exist to simplify parsing. */
698 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
699 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700700 CBS_len(contents) != 0) {
701 return 0;
702 }
703
David Benjamin9b611e22016-03-03 08:48:30 -0500704 if (name_type != TLSEXT_NAMETYPE_host_name ||
705 CBS_len(&host_name) == 0 ||
706 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
707 CBS_contains_zero_byte(&host_name)) {
708 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
709 return 0;
710 }
Adam Langley614c66a2015-06-12 15:26:58 -0700711
David Benjamin4eb95cc2016-11-16 17:08:23 +0900712 /* Copy the hostname as a string. */
David Benjamin8c880a22016-12-03 02:20:34 -0500713 if (!CBS_strdup(&host_name, &hs->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900714 *out_alert = SSL_AD_INTERNAL_ERROR;
715 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700716 }
717
David Benjamin8c880a22016-12-03 02:20:34 -0500718 hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700719 return 1;
720}
721
David Benjamin8c880a22016-12-03 02:20:34 -0500722static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
723 if (hs->ssl->s3->session_reused ||
724 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700725 return 1;
726 }
727
728 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
729 !CBB_add_u16(out, 0 /* length */)) {
730 return 0;
731 }
732
733 return 1;
734}
735
736
Adam Langley5021b222015-06-12 18:27:58 -0700737/* Renegotiation indication.
738 *
739 * https://tools.ietf.org/html/rfc5746 */
740
David Benjamin8c880a22016-12-03 02:20:34 -0500741static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
742 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400743 uint16_t min_version, max_version;
744 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
745 return 0;
746 }
747
748 /* Renegotiation indication is not necessary in TLS 1.3. */
749 if (min_version >= TLS1_3_VERSION) {
750 return 1;
751 }
752
David Benjamin52bf6902016-10-08 12:05:03 -0400753 assert(ssl->s3->initial_handshake_complete ==
754 (ssl->s3->previous_client_finished_len != 0));
755
Adam Langley5021b222015-06-12 18:27:58 -0700756 CBB contents, prev_finished;
757 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
758 !CBB_add_u16_length_prefixed(out, &contents) ||
759 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
760 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
761 ssl->s3->previous_client_finished_len) ||
762 !CBB_flush(out)) {
763 return 0;
764 }
765
766 return 1;
767}
768
David Benjamin8c880a22016-12-03 02:20:34 -0500769static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700770 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500771 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400772 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
773 return 0;
774 }
775
David Benjamin3e052de2015-11-25 20:10:31 -0500776 /* Servers may not switch between omitting the extension and supporting it.
777 * See RFC 5746, sections 3.5 and 4.2. */
778 if (ssl->s3->initial_handshake_complete &&
779 (contents != NULL) != ssl->s3->send_connection_binding) {
780 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
781 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
782 return 0;
783 }
784
Adam Langley5021b222015-06-12 18:27:58 -0700785 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500786 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700787 * RI even on initial ServerHello because the client doesn't see any
788 * renegotiation during an attack. However this would mean we could not
789 * connect to any server which doesn't support RI.
790 *
David Benjamine9cddb82015-11-23 14:36:40 -0500791 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
792 * practical terms every client sets it so it's just assumed here. */
793 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700794 }
795
796 const size_t expected_len = ssl->s3->previous_client_finished_len +
797 ssl->s3->previous_server_finished_len;
798
799 /* Check for logic errors */
800 assert(!expected_len || ssl->s3->previous_client_finished_len);
801 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400802 assert(ssl->s3->initial_handshake_complete ==
803 (ssl->s3->previous_client_finished_len != 0));
804 assert(ssl->s3->initial_handshake_complete ==
805 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700806
807 /* Parse out the extension contents. */
808 CBS renegotiated_connection;
809 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
810 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400811 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700812 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
813 return 0;
814 }
815
816 /* Check that the extension matches. */
817 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400818 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700819 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
820 return 0;
821 }
822
823 const uint8_t *d = CBS_data(&renegotiated_connection);
824 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
825 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400826 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700827 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
828 return 0;
829 }
830 d += ssl->s3->previous_client_finished_len;
831
832 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
833 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400834 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700835 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
836 return 0;
837 }
838 ssl->s3->send_connection_binding = 1;
839
840 return 1;
841}
842
David Benjamin8c880a22016-12-03 02:20:34 -0500843static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700844 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500845 SSL *const ssl = hs->ssl;
Adam Langley5021b222015-06-12 18:27:58 -0700846 /* Renegotiation isn't supported as a server so this function should never be
847 * called after the initial handshake. */
848 assert(!ssl->s3->initial_handshake_complete);
849
Steven Valdez143e8b32016-07-11 13:19:03 -0400850 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
851 return 1;
852 }
853
Adam Langley5021b222015-06-12 18:27:58 -0700854 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400855 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700856 }
857
858 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700859 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
860 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400861 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700862 return 0;
863 }
864
David Benjamin52bf6902016-10-08 12:05:03 -0400865 /* Check that the extension matches. We do not support renegotiation as a
866 * server, so this must be empty. */
867 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400868 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700869 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
870 return 0;
871 }
872
873 ssl->s3->send_connection_binding = 1;
874
875 return 1;
876}
877
David Benjamin8c880a22016-12-03 02:20:34 -0500878static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
879 SSL *const ssl = hs->ssl;
David Benjamin52bf6902016-10-08 12:05:03 -0400880 /* Renegotiation isn't supported as a server so this function should never be
881 * called after the initial handshake. */
882 assert(!ssl->s3->initial_handshake_complete);
883
Steven Valdez143e8b32016-07-11 13:19:03 -0400884 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
885 return 1;
886 }
887
Adam Langley5021b222015-06-12 18:27:58 -0700888 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400889 !CBB_add_u16(out, 1 /* length */) ||
890 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700891 return 0;
892 }
893
894 return 1;
895}
896
Adam Langley0a056712015-07-01 15:03:33 -0700897
898/* Extended Master Secret.
899 *
David Benjamin43946d42016-02-01 08:42:19 -0500900 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700901
David Benjamin8c880a22016-12-03 02:20:34 -0500902static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400903 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -0500904 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400905 return 0;
906 }
907
908 /* Extended master secret is not necessary in TLS 1.3. */
909 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700910 return 1;
911 }
912
913 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
914 !CBB_add_u16(out, 0 /* length */)) {
915 return 0;
916 }
917
918 return 1;
919}
920
David Benjamin8c880a22016-12-03 02:20:34 -0500921static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700922 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500923 SSL *const ssl = hs->ssl;
David Benjamin163c9562016-08-29 23:14:17 -0400924 /* Whether EMS is negotiated may not change on renegotation. */
925 if (ssl->s3->initial_handshake_complete) {
926 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
927 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
928 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
929 return 0;
930 }
931
932 return 1;
933 }
934
Adam Langley0a056712015-07-01 15:03:33 -0700935 if (contents == NULL) {
936 return 1;
937 }
938
Steven Valdez143e8b32016-07-11 13:19:03 -0400939 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
940 ssl->version == SSL3_VERSION) {
941 return 0;
942 }
943
944 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700945 return 0;
946 }
947
948 ssl->s3->tmp.extended_master_secret = 1;
949 return 1;
950}
951
David Benjamin8c880a22016-12-03 02:20:34 -0500952static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500953 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500954 SSL *const ssl = hs->ssl;
955 uint16_t version = ssl3_protocol_version(ssl);
956 if (version >= TLS1_3_VERSION ||
957 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400958 return 1;
959 }
960
961 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700962 return 1;
963 }
964
965 if (CBS_len(contents) != 0) {
966 return 0;
967 }
968
969 ssl->s3->tmp.extended_master_secret = 1;
970 return 1;
971}
972
David Benjamin8c880a22016-12-03 02:20:34 -0500973static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
974 if (!hs->ssl->s3->tmp.extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700975 return 1;
976 }
977
978 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
979 !CBB_add_u16(out, 0 /* length */)) {
980 return 0;
981 }
982
983 return 1;
984}
985
Adam Langley9b05bc52015-07-01 15:25:33 -0700986
987/* Session tickets.
988 *
989 * https://tools.ietf.org/html/rfc5077 */
990
David Benjamin8c880a22016-12-03 02:20:34 -0500991static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
992 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400993 uint16_t min_version, max_version;
994 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
995 return 0;
996 }
997
998 /* TLS 1.3 uses a different ticket extension. */
999 if (min_version >= TLS1_3_VERSION ||
1000 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001001 return 1;
1002 }
1003
1004 const uint8_t *ticket_data = NULL;
1005 int ticket_len = 0;
1006
1007 /* Renegotiation does not participate in session resumption. However, still
1008 * advertise the extension to avoid potentially breaking servers which carry
1009 * over the state from the previous handshake, such as OpenSSL servers
1010 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -04001011 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -07001012 if (!ssl->s3->initial_handshake_complete &&
1013 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001014 ssl->session->tlsext_tick != NULL &&
1015 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -04001016 ssl->method->version_from_wire(&session_version,
1017 ssl->session->ssl_version) &&
1018 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001019 ticket_data = ssl->session->tlsext_tick;
1020 ticket_len = ssl->session->tlsext_ticklen;
1021 }
1022
1023 CBB ticket;
1024 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1025 !CBB_add_u16_length_prefixed(out, &ticket) ||
1026 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1027 !CBB_flush(out)) {
1028 return 0;
1029 }
1030
1031 return 1;
1032}
1033
David Benjamin8c880a22016-12-03 02:20:34 -05001034static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -07001035 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001036 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -07001037 if (contents == NULL) {
1038 return 1;
1039 }
1040
Steven Valdez143e8b32016-07-11 13:19:03 -04001041 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1042 return 0;
1043 }
1044
Adam Langley9b05bc52015-07-01 15:25:33 -07001045 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1046 * this function should never be called, even if the server tries to send the
1047 * extension. */
1048 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1049
1050 if (CBS_len(contents) != 0) {
1051 return 0;
1052 }
1053
David Benjamin8c880a22016-12-03 02:20:34 -05001054 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001055 return 1;
1056}
1057
David Benjamin8c880a22016-12-03 02:20:34 -05001058static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1059 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001060 return 1;
1061 }
1062
David Benjamin78476f62016-11-12 11:20:55 +09001063 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin8c880a22016-12-03 02:20:34 -05001064 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001065
1066 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1067 !CBB_add_u16(out, 0 /* length */)) {
1068 return 0;
1069 }
1070
1071 return 1;
1072}
1073
1074
Adam Langley2e857bd2015-07-01 16:09:19 -07001075/* Signature Algorithms.
1076 *
1077 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1078
David Benjamin8c880a22016-12-03 02:20:34 -05001079static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1080 SSL *const ssl = hs->ssl;
David Benjamin2dc02042016-09-19 19:57:37 -04001081 uint16_t min_version, max_version;
1082 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1083 return 0;
1084 }
1085
1086 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001087 return 1;
1088 }
1089
David Benjamin0fc37ef2016-08-17 15:29:46 -04001090 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001091 const size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001092
David Benjamin0fc37ef2016-08-17 15:29:46 -04001093 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001094 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1095 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001096 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001097 return 0;
1098 }
1099
David Benjamin0fc37ef2016-08-17 15:29:46 -04001100 for (size_t i = 0; i < num_sigalgs; i++) {
1101 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001102 return 0;
1103 }
1104 }
1105
1106 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001107 return 0;
1108 }
1109
1110 return 1;
1111}
1112
David Benjamin8c880a22016-12-03 02:20:34 -05001113static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001114 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001115 OPENSSL_free(hs->peer_sigalgs);
1116 hs->peer_sigalgs = NULL;
1117 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001118
Adam Langley2e857bd2015-07-01 16:09:19 -07001119 if (contents == NULL) {
1120 return 1;
1121 }
1122
1123 CBS supported_signature_algorithms;
1124 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001125 CBS_len(contents) != 0 ||
1126 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001127 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001128 return 0;
1129 }
1130
1131 return 1;
1132}
1133
Adam Langley2e857bd2015-07-01 16:09:19 -07001134
Adam Langleybb0bd042015-07-01 16:21:03 -07001135/* OCSP Stapling.
1136 *
1137 * https://tools.ietf.org/html/rfc6066#section-8 */
1138
David Benjamin8c880a22016-12-03 02:20:34 -05001139static void ext_ocsp_init(SSL_HANDSHAKE *hs) {
1140 hs->ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001141}
1142
David Benjamin8c880a22016-12-03 02:20:34 -05001143static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1144 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001145 if (!ssl->ocsp_stapling_enabled) {
1146 return 1;
1147 }
1148
1149 CBB contents;
1150 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1151 !CBB_add_u16_length_prefixed(out, &contents) ||
1152 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1153 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1154 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1155 !CBB_flush(out)) {
1156 return 0;
1157 }
1158
Adam Langleyce9d85e2016-01-24 15:58:39 -08001159 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001160 return 1;
1161}
1162
David Benjamin8c880a22016-12-03 02:20:34 -05001163static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001164 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001165 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001166 if (contents == NULL) {
1167 return 1;
1168 }
1169
Steven Valdeza833c352016-11-01 13:39:36 -04001170 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1171 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001172 return 0;
1173 }
1174
Steven Valdeza833c352016-11-01 13:39:36 -04001175 /* OCSP stapling is forbidden on non-certificate ciphers. */
1176 if (CBS_len(contents) != 0 ||
1177 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001178 return 0;
1179 }
1180
Steven Valdeza833c352016-11-01 13:39:36 -04001181 /* Note this does not check for resumption in TLS 1.2. Sending
1182 * status_request here does not make sense, but OpenSSL does so and the
1183 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001184
David Benjamin8c880a22016-12-03 02:20:34 -05001185 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001186 return 1;
1187}
1188
David Benjamin8c880a22016-12-03 02:20:34 -05001189static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001190 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001191 if (contents == NULL) {
1192 return 1;
1193 }
1194
1195 uint8_t status_type;
1196 if (!CBS_get_u8(contents, &status_type)) {
1197 return 0;
1198 }
1199
1200 /* We cannot decide whether OCSP stapling will occur yet because the correct
1201 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001202 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001203
Adam Langleybb0bd042015-07-01 16:21:03 -07001204 return 1;
1205}
1206
David Benjamin8c880a22016-12-03 02:20:34 -05001207static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1208 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001209 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001210 !hs->ocsp_stapling_requested ||
Alessandro Ghedini559f0642016-12-07 12:55:32 +00001211 ssl->ocsp_response == NULL ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001212 ssl->s3->session_reused ||
Steven Valdeza833c352016-11-01 13:39:36 -04001213 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001214 return 1;
1215 }
1216
David Benjamin8c880a22016-12-03 02:20:34 -05001217 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001218
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001219 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001220 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001221}
1222
1223
Adam Langley97dfcbf2015-07-01 18:35:20 -07001224/* Next protocol negotiation.
1225 *
1226 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1227
David Benjamin8c880a22016-12-03 02:20:34 -05001228static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1229 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001230 if (ssl->s3->initial_handshake_complete ||
1231 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001232 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001233 return 1;
1234 }
1235
1236 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1237 !CBB_add_u16(out, 0 /* length */)) {
1238 return 0;
1239 }
1240
1241 return 1;
1242}
1243
David Benjamin8c880a22016-12-03 02:20:34 -05001244static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001245 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001246 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001247 if (contents == NULL) {
1248 return 1;
1249 }
1250
Steven Valdez143e8b32016-07-11 13:19:03 -04001251 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1252 return 0;
1253 }
1254
Adam Langley97dfcbf2015-07-01 18:35:20 -07001255 /* If any of these are false then we should never have sent the NPN
1256 * extension in the ClientHello and thus this function should never have been
1257 * called. */
1258 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001259 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001260 assert(ssl->ctx->next_proto_select_cb != NULL);
1261
David Benjamin76c2efc2015-08-31 14:24:29 -04001262 if (ssl->s3->alpn_selected != NULL) {
1263 /* NPN and ALPN may not be negotiated in the same connection. */
1264 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1265 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1266 return 0;
1267 }
1268
Adam Langley97dfcbf2015-07-01 18:35:20 -07001269 const uint8_t *const orig_contents = CBS_data(contents);
1270 const size_t orig_len = CBS_len(contents);
1271
1272 while (CBS_len(contents) != 0) {
1273 CBS proto;
1274 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1275 CBS_len(&proto) == 0) {
1276 return 0;
1277 }
1278 }
1279
1280 uint8_t *selected;
1281 uint8_t selected_len;
1282 if (ssl->ctx->next_proto_select_cb(
1283 ssl, &selected, &selected_len, orig_contents, orig_len,
1284 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1285 *out_alert = SSL_AD_INTERNAL_ERROR;
1286 return 0;
1287 }
1288
David Benjamin79978df2015-12-25 15:56:49 -05001289 OPENSSL_free(ssl->s3->next_proto_negotiated);
1290 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1291 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001292 *out_alert = SSL_AD_INTERNAL_ERROR;
1293 return 0;
1294 }
1295
David Benjamin79978df2015-12-25 15:56:49 -05001296 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001297 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001298
1299 return 1;
1300}
1301
David Benjamin8c880a22016-12-03 02:20:34 -05001302static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001303 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001304 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001305 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1306 return 1;
1307 }
1308
Adam Langley97dfcbf2015-07-01 18:35:20 -07001309 if (contents != NULL && CBS_len(contents) != 0) {
1310 return 0;
1311 }
1312
1313 if (contents == NULL ||
1314 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001315 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001316 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001317 return 1;
1318 }
1319
David Benjamin8c880a22016-12-03 02:20:34 -05001320 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001321 return 1;
1322}
1323
David Benjamin8c880a22016-12-03 02:20:34 -05001324static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1325 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001326 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1327 * parsed. */
David Benjamin8c880a22016-12-03 02:20:34 -05001328 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001329 return 1;
1330 }
1331
1332 const uint8_t *npa;
1333 unsigned npa_len;
1334
1335 if (ssl->ctx->next_protos_advertised_cb(
1336 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1337 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001338 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001339 return 1;
1340 }
1341
1342 CBB contents;
1343 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1344 !CBB_add_u16_length_prefixed(out, &contents) ||
1345 !CBB_add_bytes(&contents, npa, npa_len) ||
1346 !CBB_flush(out)) {
1347 return 0;
1348 }
1349
1350 return 1;
1351}
1352
1353
Adam Langleyab8d87d2015-07-10 12:21:39 -07001354/* Signed certificate timestamps.
1355 *
1356 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1357
David Benjamin8c880a22016-12-03 02:20:34 -05001358static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1359 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001360 if (!ssl->signed_cert_timestamps_enabled) {
1361 return 1;
1362 }
1363
1364 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1365 !CBB_add_u16(out, 0 /* length */)) {
1366 return 0;
1367 }
1368
1369 return 1;
1370}
1371
David Benjamin8c880a22016-12-03 02:20:34 -05001372static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001373 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001374 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001375 if (contents == NULL) {
1376 return 1;
1377 }
1378
Steven Valdeza833c352016-11-01 13:39:36 -04001379 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1380 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001381 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001382 return 0;
1383 }
1384
Adam Langleyab8d87d2015-07-10 12:21:39 -07001385 /* If this is false then we should never have sent the SCT extension in the
1386 * ClientHello and thus this function should never have been called. */
1387 assert(ssl->signed_cert_timestamps_enabled);
1388
Adam Langleycfa08c32016-11-17 13:21:27 -08001389 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001390 *out_alert = SSL_AD_DECODE_ERROR;
1391 return 0;
1392 }
1393
David Benjamindaa88502016-10-04 16:32:16 -04001394 /* Session resumption uses the original session information. The extension
1395 * should not be sent on resumption, but RFC 6962 did not make it a
1396 * requirement, so tolerate this.
1397 *
1398 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001399 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001400 !CBS_stow(
1401 contents,
1402 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1403 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001404 *out_alert = SSL_AD_INTERNAL_ERROR;
1405 return 0;
1406 }
1407
1408 return 1;
1409}
1410
David Benjamin8c880a22016-12-03 02:20:34 -05001411static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001412 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001413 if (contents == NULL) {
1414 return 1;
1415 }
1416
1417 if (CBS_len(contents) != 0) {
1418 return 0;
1419 }
1420
David Benjamin8c880a22016-12-03 02:20:34 -05001421 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001422 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001423}
1424
David Benjamin8c880a22016-12-03 02:20:34 -05001425static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1426 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001427 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001428 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1429 ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001430 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001431 return 1;
1432 }
1433
1434 CBB contents;
1435 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1436 CBB_add_u16_length_prefixed(out, &contents) &&
1437 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1438 ssl->ctx->signed_cert_timestamp_list_length) &&
1439 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001440}
1441
1442
Adam Langleyf18e4532015-07-10 13:39:53 -07001443/* Application-level Protocol Negotiation.
1444 *
1445 * https://tools.ietf.org/html/rfc7301 */
1446
David Benjamin8c880a22016-12-03 02:20:34 -05001447static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1448 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001449 if (ssl->alpn_client_proto_list == NULL ||
1450 ssl->s3->initial_handshake_complete) {
1451 return 1;
1452 }
1453
1454 CBB contents, proto_list;
1455 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1456 !CBB_add_u16_length_prefixed(out, &contents) ||
1457 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1458 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1459 ssl->alpn_client_proto_list_len) ||
1460 !CBB_flush(out)) {
1461 return 0;
1462 }
1463
1464 return 1;
1465}
1466
David Benjamin8c880a22016-12-03 02:20:34 -05001467static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001468 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001469 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001470 if (contents == NULL) {
1471 return 1;
1472 }
1473
1474 assert(!ssl->s3->initial_handshake_complete);
1475 assert(ssl->alpn_client_proto_list != NULL);
1476
David Benjamin8c880a22016-12-03 02:20:34 -05001477 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001478 /* NPN and ALPN may not be negotiated in the same connection. */
1479 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1480 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1481 return 0;
1482 }
1483
Adam Langleyf18e4532015-07-10 13:39:53 -07001484 /* The extension data consists of a ProtocolNameList which must have
1485 * exactly one ProtocolName. Each of these is length-prefixed. */
1486 CBS protocol_name_list, protocol_name;
1487 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1488 CBS_len(contents) != 0 ||
1489 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1490 /* Empty protocol names are forbidden. */
1491 CBS_len(&protocol_name) == 0 ||
1492 CBS_len(&protocol_name_list) != 0) {
1493 return 0;
1494 }
1495
David Benjamin3e517572016-08-11 11:52:23 -04001496 /* Check that the protcol name is one of the ones we advertised. */
1497 int protocol_ok = 0;
1498 CBS client_protocol_name_list, client_protocol_name;
1499 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1500 ssl->alpn_client_proto_list_len);
1501 while (CBS_len(&client_protocol_name_list) > 0) {
1502 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1503 &client_protocol_name)) {
1504 *out_alert = SSL_AD_INTERNAL_ERROR;
1505 return 0;
1506 }
1507
1508 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1509 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1510 CBS_len(&protocol_name)) == 0) {
1511 protocol_ok = 1;
1512 break;
1513 }
1514 }
1515
1516 if (!protocol_ok) {
1517 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1518 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1519 return 0;
1520 }
1521
Adam Langleyf18e4532015-07-10 13:39:53 -07001522 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1523 &ssl->s3->alpn_selected_len)) {
1524 *out_alert = SSL_AD_INTERNAL_ERROR;
1525 return 0;
1526 }
1527
1528 return 1;
1529}
1530
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001531int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001532 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001533 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001534 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001535 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001536 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001537 client_hello, &contents,
1538 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1539 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001540 return 1;
1541 }
1542
1543 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001544 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001545
1546 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001547 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1548 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001549 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001550 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1551 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001552 return 0;
1553 }
1554
1555 /* Validate the protocol list. */
1556 CBS protocol_name_list_copy = protocol_name_list;
1557 while (CBS_len(&protocol_name_list_copy) > 0) {
1558 CBS protocol_name;
1559
1560 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1561 /* Empty protocol names are forbidden. */
1562 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001563 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1564 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001565 return 0;
1566 }
1567 }
1568
1569 const uint8_t *selected;
1570 uint8_t selected_len;
1571 if (ssl->ctx->alpn_select_cb(
1572 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1573 CBS_len(&protocol_name_list),
1574 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1575 OPENSSL_free(ssl->s3->alpn_selected);
1576 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1577 if (ssl->s3->alpn_selected == NULL) {
1578 *out_alert = SSL_AD_INTERNAL_ERROR;
1579 return 0;
1580 }
1581 ssl->s3->alpn_selected_len = selected_len;
1582 }
1583
1584 return 1;
1585}
1586
David Benjamin8c880a22016-12-03 02:20:34 -05001587static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1588 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001589 if (ssl->s3->alpn_selected == NULL) {
1590 return 1;
1591 }
1592
1593 CBB contents, proto_list, proto;
1594 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1595 !CBB_add_u16_length_prefixed(out, &contents) ||
1596 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1597 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001598 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1599 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001600 !CBB_flush(out)) {
1601 return 0;
1602 }
1603
1604 return 1;
1605}
1606
1607
Adam Langley49c7af12015-07-10 14:33:46 -07001608/* Channel ID.
1609 *
1610 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1611
David Benjamin8c880a22016-12-03 02:20:34 -05001612static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1613 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001614}
1615
David Benjamin8c880a22016-12-03 02:20:34 -05001616static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1617 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001618 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001619 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001620 return 1;
1621 }
1622
1623 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1624 !CBB_add_u16(out, 0 /* length */)) {
1625 return 0;
1626 }
1627
1628 return 1;
1629}
1630
David Benjamin8c880a22016-12-03 02:20:34 -05001631static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1632 uint8_t *out_alert, CBS *contents) {
1633 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001634 if (contents == NULL) {
1635 return 1;
1636 }
1637
David Benjamince079fd2016-08-02 16:22:34 -04001638 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001639 assert(ssl->tlsext_channel_id_enabled);
1640
1641 if (CBS_len(contents) != 0) {
1642 return 0;
1643 }
1644
1645 ssl->s3->tlsext_channel_id_valid = 1;
1646 return 1;
1647}
1648
David Benjamin8c880a22016-12-03 02:20:34 -05001649static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1650 uint8_t *out_alert, CBS *contents) {
1651 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001652 if (contents == NULL ||
1653 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001654 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001655 return 1;
1656 }
1657
1658 if (CBS_len(contents) != 0) {
1659 return 0;
1660 }
1661
1662 ssl->s3->tlsext_channel_id_valid = 1;
1663 return 1;
1664}
1665
David Benjamin8c880a22016-12-03 02:20:34 -05001666static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1667 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001668 if (!ssl->s3->tlsext_channel_id_valid) {
1669 return 1;
1670 }
1671
1672 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1673 !CBB_add_u16(out, 0 /* length */)) {
1674 return 0;
1675 }
1676
1677 return 1;
1678}
1679
Adam Langley391250d2015-07-15 19:06:07 -07001680
1681/* Secure Real-time Transport Protocol (SRTP) extension.
1682 *
1683 * https://tools.ietf.org/html/rfc5764 */
1684
Adam Langley391250d2015-07-15 19:06:07 -07001685
David Benjamin8c880a22016-12-03 02:20:34 -05001686static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1687 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001688}
1689
David Benjamin8c880a22016-12-03 02:20:34 -05001690static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1691 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001692 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1693 if (profiles == NULL) {
1694 return 1;
1695 }
1696 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1697 if (num_profiles == 0) {
1698 return 1;
1699 }
1700
1701 CBB contents, profile_ids;
1702 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1703 !CBB_add_u16_length_prefixed(out, &contents) ||
1704 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1705 return 0;
1706 }
1707
David Benjamin54091232016-09-05 12:47:25 -04001708 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001709 if (!CBB_add_u16(&profile_ids,
1710 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1711 return 0;
1712 }
1713 }
1714
1715 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1716 !CBB_flush(out)) {
1717 return 0;
1718 }
1719
1720 return 1;
1721}
1722
David Benjamin8c880a22016-12-03 02:20:34 -05001723static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001724 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001725 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001726 if (contents == NULL) {
1727 return 1;
1728 }
1729
1730 /* The extension consists of a u16-prefixed profile ID list containing a
1731 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1732 *
1733 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1734 CBS profile_ids, srtp_mki;
1735 uint16_t profile_id;
1736 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1737 !CBS_get_u16(&profile_ids, &profile_id) ||
1738 CBS_len(&profile_ids) != 0 ||
1739 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1740 CBS_len(contents) != 0) {
1741 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1742 return 0;
1743 }
1744
1745 if (CBS_len(&srtp_mki) != 0) {
1746 /* Must be no MKI, since we never offer one. */
1747 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1748 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1749 return 0;
1750 }
1751
1752 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1753
1754 /* Check to see if the server gave us something we support (and presumably
1755 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001756 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001757 const SRTP_PROTECTION_PROFILE *profile =
1758 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1759
1760 if (profile->id == profile_id) {
1761 ssl->srtp_profile = profile;
1762 return 1;
1763 }
1764 }
1765
1766 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1767 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1768 return 0;
1769}
1770
David Benjamin8c880a22016-12-03 02:20:34 -05001771static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001772 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001773 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001774 if (contents == NULL) {
1775 return 1;
1776 }
1777
1778 CBS profile_ids, srtp_mki;
1779 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1780 CBS_len(&profile_ids) < 2 ||
1781 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1782 CBS_len(contents) != 0) {
1783 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1784 return 0;
1785 }
1786 /* Discard the MKI value for now. */
1787
1788 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1789 SSL_get_srtp_profiles(ssl);
1790
1791 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001792 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001793 const SRTP_PROTECTION_PROFILE *server_profile =
1794 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1795
1796 CBS profile_ids_tmp;
1797 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1798
1799 while (CBS_len(&profile_ids_tmp) > 0) {
1800 uint16_t profile_id;
1801 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1802 return 0;
1803 }
1804
1805 if (server_profile->id == profile_id) {
1806 ssl->srtp_profile = server_profile;
1807 return 1;
1808 }
1809 }
1810 }
1811
1812 return 1;
1813}
1814
David Benjamin8c880a22016-12-03 02:20:34 -05001815static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1816 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001817 if (ssl->srtp_profile == NULL) {
1818 return 1;
1819 }
1820
1821 CBB contents, profile_ids;
1822 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1823 !CBB_add_u16_length_prefixed(out, &contents) ||
1824 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1825 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1826 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1827 !CBB_flush(out)) {
1828 return 0;
1829 }
1830
1831 return 1;
1832}
1833
Adam Langleybdd5d662015-07-20 16:19:08 -07001834
1835/* EC point formats.
1836 *
1837 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1838
David Benjamin8c880a22016-12-03 02:20:34 -05001839static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001840 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001841 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1842 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001843 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1844 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001845 !CBB_flush(out)) {
1846 return 0;
1847 }
1848
1849 return 1;
1850}
1851
David Benjamin8c880a22016-12-03 02:20:34 -05001852static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001853 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -05001854 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin70aba262016-11-01 12:08:15 -04001855 return 0;
1856 }
1857
1858 /* The point format extension is unneccessary in TLS 1.3. */
1859 if (min_version >= TLS1_3_VERSION) {
1860 return 1;
1861 }
1862
David Benjamin8c880a22016-12-03 02:20:34 -05001863 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001864}
1865
David Benjamin8c880a22016-12-03 02:20:34 -05001866static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001867 CBS *contents) {
1868 if (contents == NULL) {
1869 return 1;
1870 }
1871
David Benjamin8c880a22016-12-03 02:20:34 -05001872 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001873 return 0;
1874 }
1875
Adam Langleybdd5d662015-07-20 16:19:08 -07001876 CBS ec_point_format_list;
1877 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1878 CBS_len(contents) != 0) {
1879 return 0;
1880 }
1881
David Benjaminfc059942015-07-30 23:01:59 -04001882 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1883 * point format. */
1884 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1885 CBS_len(&ec_point_format_list)) == NULL) {
1886 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001887 return 0;
1888 }
1889
1890 return 1;
1891}
1892
David Benjamin8c880a22016-12-03 02:20:34 -05001893static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001894 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001895 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001896 return 1;
1897 }
1898
David Benjamin8c880a22016-12-03 02:20:34 -05001899 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001900}
1901
David Benjamin8c880a22016-12-03 02:20:34 -05001902static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1903 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001904 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1905 return 1;
1906 }
1907
Adam Langleybdd5d662015-07-20 16:19:08 -07001908 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1909 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001910 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001911
1912 if (!using_ecc) {
1913 return 1;
1914 }
1915
David Benjamin8c880a22016-12-03 02:20:34 -05001916 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001917}
1918
Steven Valdeza833c352016-11-01 13:39:36 -04001919
Steven Valdez4aa154e2016-07-29 14:32:55 -04001920/* Pre Shared Key
1921 *
Steven Valdeza833c352016-11-01 13:39:36 -04001922 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001923
David Benjamin8c880a22016-12-03 02:20:34 -05001924static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1925 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001926 uint16_t min_version, max_version;
1927 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1928 return 0;
1929 }
1930
1931 uint16_t session_version;
1932 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1933 !ssl->method->version_from_wire(&session_version,
1934 ssl->session->ssl_version) ||
1935 session_version < TLS1_3_VERSION) {
1936 return 0;
1937 }
1938
1939 const EVP_MD *digest =
1940 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1941 size_t binder_len = EVP_MD_size(digest);
1942 return 15 + ssl->session->tlsext_ticklen + binder_len;
1943}
1944
David Benjamin8c880a22016-12-03 02:20:34 -05001945static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1946 SSL *const ssl = hs->ssl;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001947 uint16_t min_version, max_version;
1948 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1949 return 0;
1950 }
1951
David Benjamin2dc02042016-09-19 19:57:37 -04001952 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001953 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001954 !ssl->method->version_from_wire(&session_version,
1955 ssl->session->ssl_version) ||
1956 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001957 return 1;
1958 }
1959
Steven Valdeza833c352016-11-01 13:39:36 -04001960 struct timeval now;
1961 ssl_get_current_time(ssl, &now);
1962 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1963 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1964
1965 /* Fill in a placeholder zero binder of the appropriate length. It will be
1966 * computed and filled in later after length prefixes are computed. */
1967 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
1968 const EVP_MD *digest =
1969 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1970 size_t binder_len = EVP_MD_size(digest);
1971
1972 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001973 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1974 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001975 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001976 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1977 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001978 ssl->session->tlsext_ticklen) ||
1979 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1980 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1981 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1982 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001983 return 0;
1984 }
1985
David Benjamin8c880a22016-12-03 02:20:34 -05001986 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001987 return CBB_flush(out);
1988}
1989
David Benjamin8baf9632016-11-17 17:11:16 +09001990int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1991 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001992 CBS *contents) {
1993 uint16_t psk_id;
1994 if (!CBS_get_u16(contents, &psk_id) ||
1995 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001996 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001997 *out_alert = SSL_AD_DECODE_ERROR;
1998 return 0;
1999 }
2000
David Benjamineab773a2016-11-09 18:32:35 -05002001 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04002002 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04002003 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002004 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2005 return 0;
2006 }
2007
2008 return 1;
2009}
2010
David Benjamin8baf9632016-11-17 17:11:16 +09002011int ssl_ext_pre_shared_key_parse_clienthello(SSL_HANDSHAKE *hs,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002012 SSL_SESSION **out_session,
Steven Valdeza833c352016-11-01 13:39:36 -04002013 CBS *out_binders,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002014 uint8_t *out_alert,
2015 CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09002016 SSL *const ssl = hs->ssl;
Steven Valdez5b986082016-09-01 12:29:49 -04002017 /* We only process the first PSK identity since we don't support pure PSK. */
Steven Valdeza833c352016-11-01 13:39:36 -04002018 uint32_t obfuscated_ticket_age;
David Benjaminaedf3032016-12-01 16:47:56 -05002019 CBS identities, ticket, binders;
2020 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2021 !CBS_get_u16_length_prefixed(&identities, &ticket) ||
2022 !CBS_get_u32(&identities, &obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04002023 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05002024 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04002025 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04002026 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002027 *out_alert = SSL_AD_DECODE_ERROR;
2028 return 0;
2029 }
2030
Steven Valdeza833c352016-11-01 13:39:36 -04002031 *out_binders = binders;
2032
David Benjaminaedf3032016-12-01 16:47:56 -05002033 /* Check the syntax of the remaining identities, but do not process them. */
2034 size_t num_identities = 1;
2035 while (CBS_len(&identities) != 0) {
2036 CBS unused_ticket;
2037 uint32_t unused_obfuscated_ticket_age;
2038 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
2039 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
2040 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2041 *out_alert = SSL_AD_DECODE_ERROR;
2042 return 0;
2043 }
2044
2045 num_identities++;
2046 }
2047
2048 /* Check the syntax of the binders. The value will be checked later if
2049 * resuming. */
2050 size_t num_binders = 0;
2051 while (CBS_len(&binders) != 0) {
2052 CBS binder;
2053 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
2054 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2055 *out_alert = SSL_AD_DECODE_ERROR;
2056 return 0;
2057 }
2058
2059 num_binders++;
2060 }
2061
2062 if (num_identities != num_binders) {
2063 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
2064 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04002065 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04002066 }
2067
Steven Valdeza833c352016-11-01 13:39:36 -04002068 /* TODO(svaldez): Check that the ticket_age is valid when attempting to use
2069 * the PSK for 0-RTT. http://crbug.com/boringssl/113 */
2070
Steven Valdez4aa154e2016-07-29 14:32:55 -04002071 /* TLS 1.3 session tickets are renewed separately as part of the
2072 * NewSessionTicket. */
David Benjamine7f60a22016-11-16 18:54:25 +09002073 int unused_renew;
2074 if (!tls_process_ticket(ssl, out_session, &unused_renew, CBS_data(&ticket),
2075 CBS_len(&ticket), NULL, 0)) {
2076 *out_alert = SSL_AD_INTERNAL_ERROR;
2077 return 0;
2078 }
2079
2080 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002081}
2082
David Benjamin8baf9632016-11-17 17:11:16 +09002083int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2084 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002085 return 1;
2086 }
2087
2088 CBB contents;
2089 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2090 !CBB_add_u16_length_prefixed(out, &contents) ||
2091 /* We only consider the first identity for resumption */
2092 !CBB_add_u16(&contents, 0) ||
2093 !CBB_flush(out)) {
2094 return 0;
2095 }
2096
2097 return 1;
2098}
2099
2100
Steven Valdeza833c352016-11-01 13:39:36 -04002101/* Pre-Shared Key Exchange Modes
2102 *
2103 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002104
David Benjamin8c880a22016-12-03 02:20:34 -05002105static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2106 CBB *out) {
2107 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04002108 uint16_t min_version, max_version;
2109 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2110 return 0;
2111 }
2112
2113 if (max_version < TLS1_3_VERSION) {
2114 return 1;
2115 }
2116
2117 CBB contents, ke_modes;
2118 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2119 !CBB_add_u16_length_prefixed(out, &contents) ||
2120 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2121 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2122 return 0;
2123 }
2124
2125 return CBB_flush(out);
2126}
2127
David Benjamin8c880a22016-12-03 02:20:34 -05002128static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002129 uint8_t *out_alert,
2130 CBS *contents) {
2131 if (contents == NULL) {
2132 return 1;
2133 }
2134
Steven Valdeza833c352016-11-01 13:39:36 -04002135 CBS ke_modes;
2136 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2137 CBS_len(&ke_modes) == 0 ||
2138 CBS_len(contents) != 0) {
2139 *out_alert = SSL_AD_DECODE_ERROR;
2140 return 0;
2141 }
2142
2143 /* We only support tickets with PSK_DHE_KE. */
David Benjamin8c880a22016-12-03 02:20:34 -05002144 hs->accept_psk_mode =
Steven Valdeza833c352016-11-01 13:39:36 -04002145 memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, CBS_len(&ke_modes)) != NULL;
2146
2147 return 1;
2148}
2149
2150
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002151/* Early Data Indication
2152 *
2153 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2154
David Benjamin8c880a22016-12-03 02:20:34 -05002155static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002156 /* TODO(svaldez): Support 0RTT. */
2157 return 1;
2158}
2159
David Benjamin8c880a22016-12-03 02:20:34 -05002160static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2161 uint8_t *out_alert, CBS *contents) {
2162 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002163 if (contents == NULL) {
2164 return 1;
2165 }
2166
2167 if (CBS_len(contents) != 0) {
2168 *out_alert = SSL_AD_DECODE_ERROR;
2169 return 0;
2170 }
2171
2172 /* Since we don't currently accept 0-RTT, we have to skip past any early data
2173 * the client might have sent. */
2174 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
2175 ssl->s3->skip_early_data = 1;
2176 }
2177 return 1;
2178}
2179
2180
Steven Valdez143e8b32016-07-11 13:19:03 -04002181/* Key Share
2182 *
David Benjamina128a552016-10-13 14:26:33 -04002183 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002184
David Benjamin8c880a22016-12-03 02:20:34 -05002185static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2186 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002187 uint16_t min_version, max_version;
2188 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2189 return 0;
2190 }
2191
David Benjamin53a2dfc2016-10-07 21:04:12 -04002192 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002193 return 1;
2194 }
2195
2196 CBB contents, kse_bytes;
2197 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2198 !CBB_add_u16_length_prefixed(out, &contents) ||
2199 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2200 return 0;
2201 }
2202
David Benjamin8c880a22016-12-03 02:20:34 -05002203 uint16_t group_id = hs->retry_group;
2204 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002205 /* We received a HelloRetryRequest without a new curve, so there is no new
2206 * share to append. Leave |ecdh_ctx| as-is. */
2207 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002208 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2209 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002210 return 0;
2211 }
David Benjamin8c880a22016-12-03 02:20:34 -05002212 OPENSSL_free(hs->key_share_bytes);
2213 hs->key_share_bytes = NULL;
2214 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002215 if (group_id == 0) {
2216 return CBB_flush(out);
2217 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002218 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002219 /* Add a fake group. See draft-davidben-tls-grease-01. */
2220 if (ssl->ctx->grease_enabled &&
2221 (!CBB_add_u16(&kse_bytes,
2222 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2223 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2224 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2225 return 0;
2226 }
2227
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002228 /* Predict the most preferred group. */
2229 const uint16_t *groups;
2230 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002231 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002232 if (groups_len == 0) {
2233 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2234 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002235 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002236
2237 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002238 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002239
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002240 CBB key_exchange;
2241 if (!CBB_add_u16(&kse_bytes, group_id) ||
2242 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002243 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2244 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002245 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002246 return 0;
2247 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002248
David Benjamin8c880a22016-12-03 02:20:34 -05002249 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002250 /* Save the contents of the extension to repeat it in the second
2251 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002252 hs->key_share_bytes_len = CBB_len(&kse_bytes);
2253 hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
2254 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002255 return 0;
2256 }
2257 }
2258
Steven Valdez143e8b32016-07-11 13:19:03 -04002259 return CBB_flush(out);
2260}
2261
David Benjamin8baf9632016-11-17 17:11:16 +09002262int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002263 size_t *out_secret_len,
2264 uint8_t *out_alert, CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09002265 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002266 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002267 uint16_t group_id;
2268 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002269 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2270 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002271 *out_alert = SSL_AD_DECODE_ERROR;
2272 return 0;
2273 }
2274
David Benjamin8baf9632016-11-17 17:11:16 +09002275 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002276 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2277 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2278 return 0;
2279 }
2280
David Benjamin8baf9632016-11-17 17:11:16 +09002281 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2282 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002283 *out_alert = SSL_AD_INTERNAL_ERROR;
2284 return 0;
2285 }
2286
David Benjamin4882a6c2016-12-11 02:48:12 -05002287 ssl->s3->new_session->group_id = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002288 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002289 return 1;
2290}
2291
David Benjamin8baf9632016-11-17 17:11:16 +09002292int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002293 uint8_t **out_secret,
2294 size_t *out_secret_len,
2295 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002296 uint16_t group_id;
2297 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002298 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002299 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2300 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2301 return 0;
2302 }
2303
2304 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002305 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002306 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002307 return 0;
2308 }
2309
David Benjamin7e1f9842016-09-20 19:24:40 -04002310 /* Find the corresponding key share. */
2311 int found = 0;
2312 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002313 while (CBS_len(&key_shares) > 0) {
2314 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002315 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002316 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002317 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2318 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002319 return 0;
2320 }
2321
David Benjamin7e1f9842016-09-20 19:24:40 -04002322 if (id == group_id) {
2323 if (found) {
2324 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2325 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2326 return 0;
2327 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002328
David Benjamin7e1f9842016-09-20 19:24:40 -04002329 found = 1;
2330 peer_key = peer_key_tmp;
2331 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002332 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002333 }
2334
David Benjamin7e1f9842016-09-20 19:24:40 -04002335 if (!found) {
2336 *out_found = 0;
2337 *out_secret = NULL;
2338 *out_secret_len = 0;
2339 return 1;
2340 }
2341
2342 /* Compute the DH secret. */
2343 uint8_t *secret = NULL;
2344 size_t secret_len;
2345 SSL_ECDH_CTX group;
2346 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2347 CBB public_key;
2348 if (!CBB_init(&public_key, 32) ||
2349 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002350 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
2351 CBS_data(&peer_key), CBS_len(&peer_key)) ||
2352 !CBB_finish(&public_key, &hs->public_key, &hs->public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002353 OPENSSL_free(secret);
2354 SSL_ECDH_CTX_cleanup(&group);
2355 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002356 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002357 return 0;
2358 }
2359
2360 SSL_ECDH_CTX_cleanup(&group);
2361
2362 *out_secret = secret;
2363 *out_secret_len = secret_len;
2364 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002365 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002366}
2367
David Benjamin8baf9632016-11-17 17:11:16 +09002368int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2369 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002370 uint16_t group_id;
2371 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002372 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002373 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2374 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2375 !CBB_add_u16(&kse_bytes, group_id) ||
2376 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002377 !CBB_add_bytes(&public_key, hs->public_key, hs->public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002378 !CBB_flush(out)) {
2379 return 0;
2380 }
2381
David Benjamin8baf9632016-11-17 17:11:16 +09002382 OPENSSL_free(hs->public_key);
2383 hs->public_key = NULL;
2384 hs->public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002385
David Benjamin4882a6c2016-12-11 02:48:12 -05002386 ssl->s3->new_session->group_id = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002387 return 1;
2388}
2389
2390
Steven Valdezfdd10992016-09-15 16:27:05 -04002391/* Supported Versions
2392 *
2393 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2394
David Benjamin8c880a22016-12-03 02:20:34 -05002395static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2396 SSL *const ssl = hs->ssl;
Steven Valdezfdd10992016-09-15 16:27:05 -04002397 uint16_t min_version, max_version;
2398 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2399 return 0;
2400 }
2401
2402 if (max_version <= TLS1_2_VERSION) {
2403 return 1;
2404 }
2405
2406 CBB contents, versions;
2407 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2408 !CBB_add_u16_length_prefixed(out, &contents) ||
2409 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2410 return 0;
2411 }
2412
David Benjamind9791bf2016-09-27 16:39:52 -04002413 /* Add a fake version. See draft-davidben-tls-grease-01. */
2414 if (ssl->ctx->grease_enabled &&
2415 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2416 return 0;
2417 }
2418
Steven Valdezfdd10992016-09-15 16:27:05 -04002419 for (uint16_t version = max_version; version >= min_version; version--) {
2420 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2421 return 0;
2422 }
2423 }
2424
2425 if (!CBB_flush(out)) {
2426 return 0;
2427 }
2428
2429 return 1;
2430}
2431
2432
David Benjamin3baa6e12016-10-07 21:10:38 -04002433/* Cookie
2434 *
2435 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2436
David Benjamin8c880a22016-12-03 02:20:34 -05002437static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2438 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002439 return 1;
2440 }
2441
2442 CBB contents, cookie;
2443 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2444 !CBB_add_u16_length_prefixed(out, &contents) ||
2445 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002446 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002447 !CBB_flush(out)) {
2448 return 0;
2449 }
2450
2451 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002452 OPENSSL_free(hs->cookie);
2453 hs->cookie = NULL;
2454 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002455 return 1;
2456}
2457
2458
Steven Valdezce902a92016-05-17 11:47:53 -04002459/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002460 *
Steven Valdezce902a92016-05-17 11:47:53 -04002461 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002462 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002463
David Benjamin8c880a22016-12-03 02:20:34 -05002464static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2465 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002466 CBB contents, groups_bytes;
2467 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002468 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002469 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002470 return 0;
2471 }
2472
David Benjamin65ac9972016-09-02 21:35:25 -04002473 /* Add a fake group. See draft-davidben-tls-grease-01. */
2474 if (ssl->ctx->grease_enabled &&
2475 !CBB_add_u16(&groups_bytes,
2476 ssl_get_grease_value(ssl, ssl_grease_group))) {
2477 return 0;
2478 }
2479
Steven Valdezce902a92016-05-17 11:47:53 -04002480 const uint16_t *groups;
2481 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002482 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002483
David Benjamin54091232016-09-05 12:47:25 -04002484 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002485 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002486 return 0;
2487 }
2488 }
2489
2490 return CBB_flush(out);
2491}
2492
David Benjamin8c880a22016-12-03 02:20:34 -05002493static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2494 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002495 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002496 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2497 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002498 return 1;
2499}
2500
David Benjamin8c880a22016-12-03 02:20:34 -05002501static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2502 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002503 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002504 if (contents == NULL) {
2505 return 1;
2506 }
2507
Steven Valdezce902a92016-05-17 11:47:53 -04002508 CBS supported_group_list;
2509 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2510 CBS_len(&supported_group_list) == 0 ||
2511 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002512 CBS_len(contents) != 0) {
2513 return 0;
2514 }
2515
David Benjamin8c880a22016-12-03 02:20:34 -05002516 hs->peer_supported_group_list =
2517 OPENSSL_malloc(CBS_len(&supported_group_list));
2518 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002519 *out_alert = SSL_AD_INTERNAL_ERROR;
2520 return 0;
2521 }
2522
Steven Valdezce902a92016-05-17 11:47:53 -04002523 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002524 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002525 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002526 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002527 goto err;
2528 }
2529 }
2530
Steven Valdezce902a92016-05-17 11:47:53 -04002531 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002532 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002533
2534 return 1;
2535
2536err:
David Benjamin8c880a22016-12-03 02:20:34 -05002537 OPENSSL_free(hs->peer_supported_group_list);
2538 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002539 *out_alert = SSL_AD_INTERNAL_ERROR;
2540 return 0;
2541}
2542
David Benjamin8c880a22016-12-03 02:20:34 -05002543static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002544 /* Servers don't echo this extension. */
2545 return 1;
2546}
2547
2548
Adam Langley614c66a2015-06-12 15:26:58 -07002549/* kExtensions contains all the supported extensions. */
2550static const struct tls_extension kExtensions[] = {
2551 {
Adam Langley5021b222015-06-12 18:27:58 -07002552 TLSEXT_TYPE_renegotiate,
2553 NULL,
2554 ext_ri_add_clienthello,
2555 ext_ri_parse_serverhello,
2556 ext_ri_parse_clienthello,
2557 ext_ri_add_serverhello,
2558 },
2559 {
Adam Langley614c66a2015-06-12 15:26:58 -07002560 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002561 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002562 ext_sni_add_clienthello,
2563 ext_sni_parse_serverhello,
2564 ext_sni_parse_clienthello,
2565 ext_sni_add_serverhello,
2566 },
Adam Langley0a056712015-07-01 15:03:33 -07002567 {
2568 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002569 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002570 ext_ems_add_clienthello,
2571 ext_ems_parse_serverhello,
2572 ext_ems_parse_clienthello,
2573 ext_ems_add_serverhello,
2574 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002575 {
2576 TLSEXT_TYPE_session_ticket,
2577 NULL,
2578 ext_ticket_add_clienthello,
2579 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002580 /* Ticket extension client parsing is handled in ssl_session.c */
2581 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002582 ext_ticket_add_serverhello,
2583 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002584 {
2585 TLSEXT_TYPE_signature_algorithms,
2586 NULL,
2587 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002588 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002589 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002590 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002591 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002592 {
2593 TLSEXT_TYPE_status_request,
2594 ext_ocsp_init,
2595 ext_ocsp_add_clienthello,
2596 ext_ocsp_parse_serverhello,
2597 ext_ocsp_parse_clienthello,
2598 ext_ocsp_add_serverhello,
2599 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002600 {
2601 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002602 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002603 ext_npn_add_clienthello,
2604 ext_npn_parse_serverhello,
2605 ext_npn_parse_clienthello,
2606 ext_npn_add_serverhello,
2607 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002608 {
2609 TLSEXT_TYPE_certificate_timestamp,
2610 NULL,
2611 ext_sct_add_clienthello,
2612 ext_sct_parse_serverhello,
2613 ext_sct_parse_clienthello,
2614 ext_sct_add_serverhello,
2615 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002616 {
2617 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002618 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002619 ext_alpn_add_clienthello,
2620 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002621 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2622 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002623 ext_alpn_add_serverhello,
2624 },
Adam Langley49c7af12015-07-10 14:33:46 -07002625 {
2626 TLSEXT_TYPE_channel_id,
2627 ext_channel_id_init,
2628 ext_channel_id_add_clienthello,
2629 ext_channel_id_parse_serverhello,
2630 ext_channel_id_parse_clienthello,
2631 ext_channel_id_add_serverhello,
2632 },
Adam Langley391250d2015-07-15 19:06:07 -07002633 {
2634 TLSEXT_TYPE_srtp,
2635 ext_srtp_init,
2636 ext_srtp_add_clienthello,
2637 ext_srtp_parse_serverhello,
2638 ext_srtp_parse_clienthello,
2639 ext_srtp_add_serverhello,
2640 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002641 {
2642 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002643 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002644 ext_ec_point_add_clienthello,
2645 ext_ec_point_parse_serverhello,
2646 ext_ec_point_parse_clienthello,
2647 ext_ec_point_add_serverhello,
2648 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002649 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002650 TLSEXT_TYPE_key_share,
2651 NULL,
2652 ext_key_share_add_clienthello,
2653 forbid_parse_serverhello,
2654 ignore_parse_clienthello,
2655 dont_add_serverhello,
2656 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002657 {
Steven Valdeza833c352016-11-01 13:39:36 -04002658 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002659 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002660 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002661 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002662 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002663 dont_add_serverhello,
2664 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002665 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002666 TLSEXT_TYPE_early_data,
2667 NULL,
2668 ext_early_data_add_clienthello,
2669 forbid_parse_serverhello,
2670 ext_early_data_parse_clienthello,
2671 dont_add_serverhello,
2672 },
2673 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002674 TLSEXT_TYPE_supported_versions,
2675 NULL,
2676 ext_supported_versions_add_clienthello,
2677 forbid_parse_serverhello,
2678 ignore_parse_clienthello,
2679 dont_add_serverhello,
2680 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002681 {
2682 TLSEXT_TYPE_cookie,
2683 NULL,
2684 ext_cookie_add_clienthello,
2685 forbid_parse_serverhello,
2686 ignore_parse_clienthello,
2687 dont_add_serverhello,
2688 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002689 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2690 * intolerant to the last extension being zero-length. See
2691 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002692 {
Steven Valdezce902a92016-05-17 11:47:53 -04002693 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002694 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002695 ext_supported_groups_add_clienthello,
2696 ext_supported_groups_parse_serverhello,
2697 ext_supported_groups_parse_clienthello,
2698 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002699 },
Adam Langley614c66a2015-06-12 15:26:58 -07002700};
2701
2702#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2703
Adam Langley4cfa96b2015-07-01 11:56:55 -07002704OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002705 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002706 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002707OPENSSL_COMPILE_ASSERT(
2708 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2709 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002710
Adam Langley614c66a2015-06-12 15:26:58 -07002711static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2712 uint16_t value) {
2713 unsigned i;
2714 for (i = 0; i < kNumExtensions; i++) {
2715 if (kExtensions[i].value == value) {
2716 *out_index = i;
2717 return &kExtensions[i];
2718 }
2719 }
2720
2721 return NULL;
2722}
2723
Adam Langley09505632015-07-30 18:10:13 -07002724int SSL_extension_supported(unsigned extension_value) {
2725 uint32_t index;
2726 return extension_value == TLSEXT_TYPE_padding ||
2727 tls_extension_find(&index, extension_value) != NULL;
2728}
2729
David Benjamin8c880a22016-12-03 02:20:34 -05002730int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2731 SSL *const ssl = hs->ssl;
David Benjaminf04c2e92016-12-06 13:35:25 -05002732 /* Don't add extensions for SSLv3 unless doing secure renegotiation. */
2733 if (hs->client_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -04002734 !ssl->s3->send_connection_binding) {
2735 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002736 }
Adam Langley95c29f32014-06-20 12:00:00 -07002737
David Benjamine8d53502015-10-10 14:13:23 -04002738 CBB extensions;
2739 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002740 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002741 }
Adam Langley95c29f32014-06-20 12:00:00 -07002742
David Benjamin8c880a22016-12-03 02:20:34 -05002743 hs->extensions.sent = 0;
2744 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002745
David Benjamin54091232016-09-05 12:47:25 -04002746 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002747 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002748 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002749 }
2750 }
Adam Langley95c29f32014-06-20 12:00:00 -07002751
David Benjamin65ac9972016-09-02 21:35:25 -04002752 uint16_t grease_ext1 = 0;
2753 if (ssl->ctx->grease_enabled) {
2754 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2755 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2756 if (!CBB_add_u16(&extensions, grease_ext1) ||
2757 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2758 goto err;
2759 }
2760 }
2761
David Benjamin54091232016-09-05 12:47:25 -04002762 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002763 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002764 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002765 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002766 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002767 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002768 }
Adam Langley95c29f32014-06-20 12:00:00 -07002769
Adam Langley33ad2b52015-07-20 17:43:53 -07002770 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002771 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002772 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002773 }
Adam Langley75712922014-10-10 16:23:43 -07002774
David Benjamin2bd19172016-11-17 16:47:15 +09002775 if (!custom_ext_add_clienthello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002776 goto err;
2777 }
2778
David Benjamin65ac9972016-09-02 21:35:25 -04002779 if (ssl->ctx->grease_enabled) {
2780 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2781 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2782
2783 /* The two fake extensions must not have the same value. GREASE values are
2784 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2785 * one. */
2786 if (grease_ext1 == grease_ext2) {
2787 grease_ext2 ^= 0x1010;
2788 }
2789
2790 if (!CBB_add_u16(&extensions, grease_ext2) ||
2791 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2792 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2793 goto err;
2794 }
2795 }
2796
David Benjamince079fd2016-08-02 16:22:34 -04002797 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002798 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002799 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002800 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002801 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002802 *
2803 * NB: because this code works out the length of all existing extensions
2804 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002805 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002806 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002807 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002808 * Server 7.0 is intolerant to the last extension being zero-length. See
2809 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002810 if (padding_len >= 4 + 1) {
2811 padding_len -= 4;
2812 } else {
2813 padding_len = 1;
2814 }
Adam Langley95c29f32014-06-20 12:00:00 -07002815
Adam Langley33ad2b52015-07-20 17:43:53 -07002816 uint8_t *padding_bytes;
2817 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2818 !CBB_add_u16(&extensions, padding_len) ||
2819 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2820 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002821 }
Adam Langley75712922014-10-10 16:23:43 -07002822
Adam Langley33ad2b52015-07-20 17:43:53 -07002823 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002824 }
2825 }
Adam Langley75712922014-10-10 16:23:43 -07002826
Steven Valdeza833c352016-11-01 13:39:36 -04002827 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002828 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002829 goto err;
2830 }
2831
David Benjamina01deee2015-12-08 18:56:31 -05002832 /* Discard empty extensions blocks. */
2833 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002834 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002835 }
2836
David Benjamine8d53502015-10-10 14:13:23 -04002837 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002838
2839err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002840 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002841 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002842}
Adam Langley95c29f32014-06-20 12:00:00 -07002843
David Benjamin8c880a22016-12-03 02:20:34 -05002844int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2845 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002846 CBB extensions;
2847 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002848 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002849 }
2850
David Benjamin8c880a22016-12-03 02:20:34 -05002851 for (unsigned i = 0; i < kNumExtensions; i++) {
2852 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002853 /* Don't send extensions that were not received. */
2854 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002855 }
Adam Langley95c29f32014-06-20 12:00:00 -07002856
David Benjamin8c880a22016-12-03 02:20:34 -05002857 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002858 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002859 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002860 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002861 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002862 }
Adam Langley95c29f32014-06-20 12:00:00 -07002863
David Benjamin2bd19172016-11-17 16:47:15 +09002864 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002865 goto err;
2866 }
2867
Steven Valdez143e8b32016-07-11 13:19:03 -04002868 /* Discard empty extensions blocks before TLS 1.3. */
2869 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2870 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002871 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002872 }
2873
David Benjamin56380462015-10-10 14:59:09 -04002874 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002875
2876err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002877 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002878 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002879}
Adam Langley95c29f32014-06-20 12:00:00 -07002880
David Benjamin731058e2016-12-03 23:15:13 -05002881static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2882 const SSL_CLIENT_HELLO *client_hello,
2883 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002884 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002885 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002886 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002887 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002888 }
2889 }
2890
David Benjamin8c880a22016-12-03 02:20:34 -05002891 hs->extensions.received = 0;
2892 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002893
David Benjamine14ff062016-08-09 16:21:24 -04002894 CBS extensions;
2895 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2896 while (CBS_len(&extensions) != 0) {
2897 uint16_t type;
2898 CBS extension;
2899
2900 /* Decode the next extension. */
2901 if (!CBS_get_u16(&extensions, &type) ||
2902 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002903 *out_alert = SSL_AD_DECODE_ERROR;
2904 return 0;
2905 }
Adam Langley95c29f32014-06-20 12:00:00 -07002906
David Benjamine14ff062016-08-09 16:21:24 -04002907 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2908 * ambiguous. Ignore all but the renegotiation_info extension. */
2909 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2910 continue;
2911 }
Adam Langley95c29f32014-06-20 12:00:00 -07002912
David Benjamine14ff062016-08-09 16:21:24 -04002913 unsigned ext_index;
2914 const struct tls_extension *const ext =
2915 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002916
David Benjamine14ff062016-08-09 16:21:24 -04002917 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002918 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002919 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002920 return 0;
2921 }
David Benjamine14ff062016-08-09 16:21:24 -04002922 continue;
2923 }
2924
David Benjamin8c880a22016-12-03 02:20:34 -05002925 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002926 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002927 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002928 *out_alert = alert;
2929 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002930 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002931 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002932 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002933 }
Adam Langley75712922014-10-10 16:23:43 -07002934
David Benjamin1deb41b2016-08-09 19:36:38 -04002935 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002936 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002937 continue;
2938 }
2939
2940 CBS *contents = NULL, fake_contents;
2941 static const uint8_t kFakeRenegotiateExtension[] = {0};
2942 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2943 ssl_client_cipher_list_contains_cipher(client_hello,
2944 SSL3_CK_SCSV & 0xffff)) {
2945 /* The renegotiation SCSV was received so pretend that we received a
2946 * renegotiation extension. */
2947 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2948 sizeof(kFakeRenegotiateExtension));
2949 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002950 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002951 }
2952
2953 /* Extension wasn't observed so call the callback with a NULL
2954 * parameter. */
2955 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002956 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002957 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002958 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002959 *out_alert = alert;
2960 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002961 }
2962 }
2963
Adam Langleyfcf25832014-12-18 17:42:32 -08002964 return 1;
2965}
Adam Langley95c29f32014-06-20 12:00:00 -07002966
David Benjamin731058e2016-12-03 23:15:13 -05002967int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2968 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002969 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002970 int alert = -1;
David Benjamin8c880a22016-12-03 02:20:34 -05002971 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002972 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002973 return 0;
2974 }
Adam Langley95c29f32014-06-20 12:00:00 -07002975
David Benjamin8c880a22016-12-03 02:20:34 -05002976 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002977 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002978 return 0;
2979 }
Adam Langley95c29f32014-06-20 12:00:00 -07002980
Adam Langleyfcf25832014-12-18 17:42:32 -08002981 return 1;
2982}
Adam Langley95c29f32014-06-20 12:00:00 -07002983
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002984OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2985
David Benjamin8c880a22016-12-03 02:20:34 -05002986static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2987 int *out_alert) {
2988 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002989 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2990 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2991 return 1;
2992 }
Adam Langley614c66a2015-06-12 15:26:58 -07002993
Steven Valdez143e8b32016-07-11 13:19:03 -04002994 /* Decode the extensions block and check it is valid. */
2995 CBS extensions;
2996 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2997 !tls1_check_duplicate_extensions(&extensions)) {
2998 *out_alert = SSL_AD_DECODE_ERROR;
2999 return 0;
3000 }
3001
3002 uint32_t received = 0;
3003 while (CBS_len(&extensions) != 0) {
3004 uint16_t type;
3005 CBS extension;
3006
3007 /* Decode the next extension. */
3008 if (!CBS_get_u16(&extensions, &type) ||
3009 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003010 *out_alert = SSL_AD_DECODE_ERROR;
3011 return 0;
3012 }
Adam Langley95c29f32014-06-20 12:00:00 -07003013
Steven Valdez143e8b32016-07-11 13:19:03 -04003014 unsigned ext_index;
3015 const struct tls_extension *const ext =
3016 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07003017
Steven Valdez143e8b32016-07-11 13:19:03 -04003018 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09003019 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003020 return 0;
3021 }
Steven Valdez143e8b32016-07-11 13:19:03 -04003022 continue;
3023 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003024
David Benjamin8c880a22016-12-03 02:20:34 -05003025 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04003026 type != TLSEXT_TYPE_renegotiate) {
3027 /* If the extension was never sent then it is illegal, except for the
3028 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04003029 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3030 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04003031 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04003032 return 0;
3033 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003034
Steven Valdez143e8b32016-07-11 13:19:03 -04003035 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07003036
Steven Valdez143e8b32016-07-11 13:19:03 -04003037 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003038 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04003039 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003040 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04003041 *out_alert = alert;
3042 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07003043 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003044 }
Adam Langley95c29f32014-06-20 12:00:00 -07003045
David Benjamin54091232016-09-05 12:47:25 -04003046 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003047 if (!(received & (1u << i))) {
3048 /* Extension wasn't observed so call the callback with a NULL
3049 * parameter. */
3050 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003051 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003052 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003053 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07003054 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08003055 return 0;
3056 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003057 }
3058 }
Adam Langley95c29f32014-06-20 12:00:00 -07003059
Adam Langleyfcf25832014-12-18 17:42:32 -08003060 return 1;
3061}
Adam Langley95c29f32014-06-20 12:00:00 -07003062
David Benjamin8c880a22016-12-03 02:20:34 -05003063static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3064 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003065 int ret = SSL_TLSEXT_ERR_NOACK;
3066 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003067
David Benjamin78f8aab2016-03-10 16:33:58 -05003068 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003069 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003070 ssl->ctx->tlsext_servername_arg);
3071 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003072 ret = ssl->initial_ctx->tlsext_servername_callback(
3073 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003074 }
Adam Langley95c29f32014-06-20 12:00:00 -07003075
Adam Langleyfcf25832014-12-18 17:42:32 -08003076 switch (ret) {
3077 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003078 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003079 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003080
Adam Langleyfcf25832014-12-18 17:42:32 -08003081 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003082 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003083 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003084
Adam Langleyfcf25832014-12-18 17:42:32 -08003085 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003086 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003087 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003088
Adam Langleyfcf25832014-12-18 17:42:32 -08003089 default:
3090 return 1;
3091 }
3092}
Adam Langleyed8270a2014-09-02 13:52:56 -07003093
David Benjamin8c880a22016-12-03 02:20:34 -05003094static int ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs) {
3095 SSL *const ssl = hs->ssl;
David Benjaminfc059942015-07-30 23:01:59 -04003096 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08003097 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07003098
David Benjamin78f8aab2016-03-10 16:33:58 -05003099 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003100 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003101 ssl->ctx->tlsext_servername_arg);
3102 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003103 ret = ssl->initial_ctx->tlsext_servername_callback(
3104 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003105 }
Adam Langley95c29f32014-06-20 12:00:00 -07003106
Adam Langleyfcf25832014-12-18 17:42:32 -08003107 switch (ret) {
3108 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003109 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003110 return -1;
David Benjamin03973092014-06-24 23:27:17 -04003111
Adam Langleyfcf25832014-12-18 17:42:32 -08003112 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003113 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003114 return 1;
3115
3116 default:
3117 return 1;
3118 }
3119}
3120
David Benjamin8c880a22016-12-03 02:20:34 -05003121int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3122 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003123 int alert = -1;
David Benjamin8c880a22016-12-03 02:20:34 -05003124 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003125 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003126 return 0;
3127 }
3128
David Benjamin8c880a22016-12-03 02:20:34 -05003129 if (ssl_check_serverhello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04003130 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08003131 return 0;
3132 }
3133
3134 return 1;
3135}
Adam Langley95c29f32014-06-20 12:00:00 -07003136
David Benjamine3aa1d92015-06-16 15:34:50 -04003137int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05003138 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04003139 size_t ticket_len, const uint8_t *session_id,
3140 size_t session_id_len) {
3141 int ret = 1; /* Most errors are non-fatal. */
3142 SSL_CTX *ssl_ctx = ssl->initial_ctx;
3143 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003144
David Benjamine3aa1d92015-06-16 15:34:50 -04003145 HMAC_CTX hmac_ctx;
3146 HMAC_CTX_init(&hmac_ctx);
3147 EVP_CIPHER_CTX cipher_ctx;
3148 EVP_CIPHER_CTX_init(&cipher_ctx);
3149
David Benjaminef1b0092015-11-21 14:05:44 -05003150 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04003151 *out_session = NULL;
3152
Steven Valdez4aa154e2016-07-29 14:32:55 -04003153 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
3154 goto done;
3155 }
3156
David Benjamine3aa1d92015-06-16 15:34:50 -04003157 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3158 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003159 }
3160
David Benjaminadcc3952015-04-26 13:07:57 -04003161 /* Ensure there is room for the key name and the largest IV
3162 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3163 * the maximum IV length should be well under the minimum size for the
3164 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003165 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3166 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003167 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003168 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003169
David Benjamine3aa1d92015-06-16 15:34:50 -04003170 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003171 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3172 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3173 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003174 if (cb_ret < 0) {
3175 ret = 0;
3176 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003177 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003178 if (cb_ret == 0) {
3179 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003180 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003181 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003182 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003183 }
3184 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003185 /* Check the key name matches. */
3186 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3187 SSL_TICKET_KEY_NAME_LEN) != 0) {
3188 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003189 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003190 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3191 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003192 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003193 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3194 ssl_ctx->tlsext_tick_aes_key, iv)) {
3195 ret = 0;
3196 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003197 }
3198 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003199 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003200
David Benjamine3aa1d92015-06-16 15:34:50 -04003201 /* Check the MAC at the end of the ticket. */
3202 uint8_t mac[EVP_MAX_MD_SIZE];
3203 size_t mac_len = HMAC_size(&hmac_ctx);
3204 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003205 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003206 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003207 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003208 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3209 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003210 int mac_ok =
3211 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3212#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3213 mac_ok = 1;
3214#endif
3215 if (!mac_ok) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003216 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003217 }
3218
David Benjamine3aa1d92015-06-16 15:34:50 -04003219 /* Decrypt the session data. */
3220 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3221 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3222 mac_len;
3223 plaintext = OPENSSL_malloc(ciphertext_len);
3224 if (plaintext == NULL) {
3225 ret = 0;
3226 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003227 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003228 size_t plaintext_len;
3229#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3230 memcpy(plaintext, ciphertext, ciphertext_len);
3231 plaintext_len = ciphertext_len;
3232#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003233 if (ciphertext_len >= INT_MAX) {
3234 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003235 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003236 int len1, len2;
3237 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3238 (int)ciphertext_len) ||
3239 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
3240 ERR_clear_error(); /* Don't leave an error on the queue. */
3241 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003242 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003243 plaintext_len = (size_t)(len1 + len2);
3244#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003245
David Benjamine3aa1d92015-06-16 15:34:50 -04003246 /* Decode the session. */
David Benjaminfbc45d72016-09-22 01:21:24 -04003247 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003248 if (session == NULL) {
3249 ERR_clear_error(); /* Don't leave an error on the queue. */
3250 goto done;
3251 }
3252
3253 /* Copy the client's session ID into the new session, to denote the ticket has
3254 * been accepted. */
3255 memcpy(session->session_id, session_id, session_id_len);
3256 session->session_id_length = session_id_len;
3257
3258 *out_session = session;
3259
3260done:
3261 OPENSSL_free(plaintext);
3262 HMAC_CTX_cleanup(&hmac_ctx);
3263 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3264 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003265}
Adam Langley95c29f32014-06-20 12:00:00 -07003266
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003267int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003268 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003269 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003270 return 1;
3271 }
David Benjamincd996942014-07-20 16:23:51 -04003272
David Benjamin0fc37ef2016-08-17 15:29:46 -04003273 OPENSSL_free(hs->peer_sigalgs);
3274 hs->peer_sigalgs = NULL;
3275 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003276
3277 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003278 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003279 return 0;
3280 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003281 num_sigalgs /= 2;
3282
3283 /* supported_signature_algorithms in the certificate request is
3284 * allowed to be empty. */
3285 if (num_sigalgs == 0) {
3286 return 1;
3287 }
3288
Steven Valdez02563852016-06-23 13:33:05 -04003289 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3290 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003291 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3292 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003293 return 0;
3294 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003295 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003296
3297 CBS sigalgs;
3298 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003299 for (size_t i = 0; i < num_sigalgs; i++) {
3300 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003301 return 0;
3302 }
3303 }
Adam Langley95c29f32014-06-20 12:00:00 -07003304
Adam Langleyfcf25832014-12-18 17:42:32 -08003305 return 1;
3306}
David Benjaminec2f27d2014-11-13 19:17:25 -05003307
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003308int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3309 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003310 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003311
Steven Valdezf0451ca2016-06-29 13:16:27 -04003312 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3313 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3314 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003315 int type = ssl_private_key_type(ssl);
3316 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003317 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003318 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003319 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003320 if (ssl_is_ecdsa_key_type(type)) {
3321 *out = SSL_SIGN_ECDSA_SHA1;
3322 return 1;
3323 }
3324 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3325 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003326 }
3327
David Benjamin3ef76972016-10-17 17:59:54 -04003328 const uint16_t *sigalgs = cert->sigalgs;
3329 size_t num_sigalgs = cert->num_sigalgs;
3330 if (sigalgs == NULL) {
3331 sigalgs = kSignSignatureAlgorithms;
3332 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003333 }
3334
David Benjamin0fc37ef2016-08-17 15:29:46 -04003335 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3336 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3337 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003338 /* If the client didn't specify any signature_algorithms extension then
3339 * we can assume that it supports SHA1. See
3340 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3341 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3342 SSL_SIGN_ECDSA_SHA1};
3343 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003344 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003345 }
3346
David Benjamin0fc37ef2016-08-17 15:29:46 -04003347 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003348 uint16_t sigalg = sigalgs[i];
3349 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3350 * negotiated. */
3351 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3352 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3353 continue;
3354 }
3355
David Benjamin0fc37ef2016-08-17 15:29:46 -04003356 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003357 if (sigalg == peer_sigalgs[j]) {
3358 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003359 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003360 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003361 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003362 }
Adam Langley95c29f32014-06-20 12:00:00 -07003363
David Benjaminea9a0d52016-07-08 15:52:59 -07003364 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3365 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003366}
Adam Langley95c29f32014-06-20 12:00:00 -07003367
Nick Harper60a85cb2016-09-23 16:25:11 -07003368int tls1_verify_channel_id(SSL *ssl) {
3369 int ret = 0;
3370 uint16_t extension_type;
3371 CBS extension, channel_id;
3372
3373 /* A Channel ID handshake message is structured to contain multiple
3374 * extensions, but the only one that can be present is Channel ID. */
3375 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3376 if (!CBS_get_u16(&channel_id, &extension_type) ||
3377 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3378 CBS_len(&channel_id) != 0 ||
3379 extension_type != TLSEXT_TYPE_channel_id ||
3380 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3381 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3382 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3383 return 0;
3384 }
3385
3386 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3387 if (!p256) {
3388 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3389 return 0;
3390 }
3391
3392 EC_KEY *key = NULL;
3393 EC_POINT *point = NULL;
3394 BIGNUM x, y;
3395 ECDSA_SIG sig;
3396 BN_init(&x);
3397 BN_init(&y);
3398 sig.r = BN_new();
3399 sig.s = BN_new();
3400 if (sig.r == NULL || sig.s == NULL) {
3401 goto err;
3402 }
3403
3404 const uint8_t *p = CBS_data(&extension);
3405 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3406 BN_bin2bn(p + 32, 32, &y) == NULL ||
3407 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3408 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3409 goto err;
3410 }
3411
3412 point = EC_POINT_new(p256);
3413 if (point == NULL ||
3414 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3415 goto err;
3416 }
3417
3418 key = EC_KEY_new();
3419 if (key == NULL ||
3420 !EC_KEY_set_group(key, p256) ||
3421 !EC_KEY_set_public_key(key, point)) {
3422 goto err;
3423 }
3424
3425 uint8_t digest[EVP_MAX_MD_SIZE];
3426 size_t digest_len;
3427 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3428 goto err;
3429 }
3430
3431 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3432#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3433 sig_ok = 1;
3434#endif
3435 if (!sig_ok) {
3436 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3437 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3438 ssl->s3->tlsext_channel_id_valid = 0;
3439 goto err;
3440 }
3441
3442 memcpy(ssl->s3->tlsext_channel_id, p, 64);
3443 ret = 1;
3444
3445err:
3446 BN_free(&x);
3447 BN_free(&y);
3448 BN_free(sig.r);
3449 BN_free(sig.s);
3450 EC_KEY_free(key);
3451 EC_POINT_free(point);
3452 EC_GROUP_free(p256);
3453 return ret;
3454}
3455
3456int tls1_write_channel_id(SSL *ssl, CBB *cbb) {
3457 uint8_t digest[EVP_MAX_MD_SIZE];
3458 size_t digest_len;
3459 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3460 return 0;
3461 }
3462
3463 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3464 if (ec_key == NULL) {
3465 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3466 return 0;
3467 }
3468
3469 int ret = 0;
3470 BIGNUM *x = BN_new();
3471 BIGNUM *y = BN_new();
3472 ECDSA_SIG *sig = NULL;
3473 if (x == NULL || y == NULL ||
3474 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3475 EC_KEY_get0_public_key(ec_key),
3476 x, y, NULL)) {
3477 goto err;
3478 }
3479
3480 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3481 if (sig == NULL) {
3482 goto err;
3483 }
3484
3485 CBB child;
3486 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3487 !CBB_add_u16_length_prefixed(cbb, &child) ||
3488 !BN_bn2cbb_padded(&child, 32, x) ||
3489 !BN_bn2cbb_padded(&child, 32, y) ||
3490 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3491 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3492 !CBB_flush(cbb)) {
3493 goto err;
3494 }
3495
3496 ret = 1;
3497
3498err:
3499 BN_free(x);
3500 BN_free(y);
3501 ECDSA_SIG_free(sig);
3502 return ret;
3503}
3504
David Benjamind6a4ae92015-08-06 11:10:51 -04003505int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003506 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3507 uint8_t *msg;
3508 size_t msg_len;
3509 if (!tls13_get_cert_verify_signature_input(ssl, &msg, &msg_len,
3510 ssl_cert_verify_channel_id)) {
3511 return 0;
3512 }
3513 SHA256(msg, msg_len, out);
3514 *out_len = SHA256_DIGEST_LENGTH;
3515 OPENSSL_free(msg);
3516 return 1;
3517 }
3518
Nick Harper95594012016-10-20 14:07:13 -07003519 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003520
Nick Harper95594012016-10-20 14:07:13 -07003521 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003522 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003523 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003524
Steven Valdez87eab492016-06-27 16:34:59 -04003525 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003526 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003527 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003528 if (ssl->session->original_handshake_hash_len == 0) {
3529 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003530 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003531 }
Nick Harper95594012016-10-20 14:07:13 -07003532 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3533 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003534 }
3535
3536 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3537 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3538 sizeof(handshake_hash));
3539 if (handshake_hash_len < 0) {
Nick Harper95594012016-10-20 14:07:13 -07003540 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003541 }
Nick Harper95594012016-10-20 14:07:13 -07003542 SHA256_Update(&ctx, handshake_hash, (size_t)handshake_hash_len);
3543 SHA256_Final(out, &ctx);
3544 *out_len = SHA256_DIGEST_LENGTH;
3545 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003546}
Adam Langley1258b6a2014-06-20 12:00:00 -07003547
3548/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003549 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003550 * data. */
3551int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003552 int digest_len;
3553 /* This function should never be called for a resumed session because the
3554 * handshake hashes that we wish to record are for the original, full
3555 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003556 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003557 return -1;
3558 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003559
Adam Langleyfcf25832014-12-18 17:42:32 -08003560 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003561 tls1_handshake_digest(
3562 ssl, ssl->s3->new_session->original_handshake_hash,
3563 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003564 if (digest_len < 0) {
3565 return -1;
3566 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003567
David Benjamin30c4c302016-12-07 22:35:24 -05003568 assert(sizeof(ssl->s3->new_session->original_handshake_hash) < 256);
3569 ssl->s3->new_session->original_handshake_hash_len = (uint8_t)digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003570
Adam Langleyfcf25832014-12-18 17:42:32 -08003571 return 1;
3572}
Nick Harper60a85cb2016-09-23 16:25:11 -07003573
3574int ssl_do_channel_id_callback(SSL *ssl) {
3575 if (ssl->tlsext_channel_id_private != NULL ||
3576 ssl->ctx->channel_id_cb == NULL) {
3577 return 1;
3578 }
3579
3580 EVP_PKEY *key = NULL;
3581 ssl->ctx->channel_id_cb(ssl, &key);
3582 if (key == NULL) {
3583 /* The caller should try again later. */
3584 return 1;
3585 }
3586
3587 int ret = SSL_set1_tls_channel_id(ssl, key);
3588 EVP_PKEY_free(key);
3589 return ret;
3590}
Adam Langleycfa08c32016-11-17 13:21:27 -08003591
3592int ssl_is_sct_list_valid(const CBS *contents) {
3593 /* Shallow parse the SCT list for sanity. By the RFC
3594 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3595 * of the SCTs may be empty. */
3596 CBS copy = *contents;
3597 CBS sct_list;
3598 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3599 CBS_len(&copy) != 0 ||
3600 CBS_len(&sct_list) == 0) {
3601 return 0;
3602 }
3603
3604 while (CBS_len(&sct_list) > 0) {
3605 CBS sct;
3606 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3607 CBS_len(&sct) == 0) {
3608 return 0;
3609 }
3610 }
3611
3612 return 1;
3613}