blob: fa1c0672983e06b2eb7b54a8b32a4ef7b15ff1b5 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin03973092014-06-24 23:27:17 -0400116#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400117#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/evp.h>
120#include <openssl/hmac.h>
121#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400122#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/rand.h>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700124#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400127#include "../crypto/internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800128
129
David Benjamin8c880a22016-12-03 02:20:34 -0500130static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
131static int ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs);
Adam Langley95c29f32014-06-20 12:00:00 -0700132
Adam Langleyfcf25832014-12-18 17:42:32 -0800133static int compare_uint16_t(const void *p1, const void *p2) {
134 uint16_t u1 = *((const uint16_t *)p1);
135 uint16_t u2 = *((const uint16_t *)p2);
136 if (u1 < u2) {
137 return -1;
138 } else if (u1 > u2) {
139 return 1;
140 } else {
141 return 0;
142 }
143}
David Benjamin35a7a442014-07-05 00:23:20 -0400144
Adam Langleyfcf25832014-12-18 17:42:32 -0800145/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
146 * more than one extension of the same type in a ClientHello or ServerHello.
147 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400148 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800149static int tls1_check_duplicate_extensions(const CBS *cbs) {
150 CBS extensions = *cbs;
151 size_t num_extensions = 0, i = 0;
152 uint16_t *extension_types = NULL;
153 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400154
Adam Langleyfcf25832014-12-18 17:42:32 -0800155 /* First pass: count the extensions. */
156 while (CBS_len(&extensions) > 0) {
157 uint16_t type;
158 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400159
Adam Langleyfcf25832014-12-18 17:42:32 -0800160 if (!CBS_get_u16(&extensions, &type) ||
161 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
162 goto done;
163 }
David Benjamin35a7a442014-07-05 00:23:20 -0400164
Adam Langleyfcf25832014-12-18 17:42:32 -0800165 num_extensions++;
166 }
David Benjamin35a7a442014-07-05 00:23:20 -0400167
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 if (num_extensions == 0) {
169 return 1;
170 }
David Benjamin9a373592014-07-25 04:27:53 -0400171
Brian Smith5ba06892016-02-07 09:36:04 -1000172 extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400174 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800175 goto done;
176 }
David Benjamin35a7a442014-07-05 00:23:20 -0400177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 /* Second pass: gather the extension types. */
179 extensions = *cbs;
180 for (i = 0; i < num_extensions; i++) {
181 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400182
Adam Langleyfcf25832014-12-18 17:42:32 -0800183 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
184 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
185 /* This should not happen. */
186 goto done;
187 }
188 }
189 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400190
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 /* Sort the extensions and make sure there are no duplicates. */
192 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
193 for (i = 1; i < num_extensions; i++) {
194 if (extension_types[i - 1] == extension_types[i]) {
195 goto done;
196 }
197 }
David Benjamin35a7a442014-07-05 00:23:20 -0400198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 ret = 1;
200
David Benjamin35a7a442014-07-05 00:23:20 -0400201done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400202 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 return ret;
204}
David Benjamin35a7a442014-07-05 00:23:20 -0400205
David Benjamin731058e2016-12-03 23:15:13 -0500206int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out, const uint8_t *in,
207 size_t in_len) {
208 memset(out, 0, sizeof(*out));
209 out->ssl = ssl;
210 out->client_hello = in;
211 out->client_hello_len = in_len;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400212
David Benjamine14ff062016-08-09 16:21:24 -0400213 CBS client_hello, random, session_id;
David Benjamin731058e2016-12-03 23:15:13 -0500214 CBS_init(&client_hello, out->client_hello, out->client_hello_len);
215 if (!CBS_get_u16(&client_hello, &out->version) ||
David Benjamine14ff062016-08-09 16:21:24 -0400216 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
217 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
218 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 return 0;
220 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700221
David Benjamin731058e2016-12-03 23:15:13 -0500222 out->random = CBS_data(&random);
223 out->random_len = CBS_len(&random);
224 out->session_id = CBS_data(&session_id);
225 out->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700226
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 /* Skip past DTLS cookie */
David Benjamin731058e2016-12-03 23:15:13 -0500228 if (SSL_is_dtls(out->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400230 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
231 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 return 0;
233 }
234 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700235
David Benjamine14ff062016-08-09 16:21:24 -0400236 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400238 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
239 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 CBS_len(&compression_methods) < 1) {
241 return 0;
242 }
David Benjamine14ff062016-08-09 16:21:24 -0400243
David Benjamin731058e2016-12-03 23:15:13 -0500244 out->cipher_suites = CBS_data(&cipher_suites);
245 out->cipher_suites_len = CBS_len(&cipher_suites);
246 out->compression_methods = CBS_data(&compression_methods);
247 out->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 /* If the ClientHello ends here then it's valid, but doesn't have any
250 * extensions. (E.g. SSLv3.) */
251 if (CBS_len(&client_hello) == 0) {
David Benjamin731058e2016-12-03 23:15:13 -0500252 out->extensions = NULL;
253 out->extensions_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 return 1;
255 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 /* Extract extensions and check it is valid. */
David Benjamine14ff062016-08-09 16:21:24 -0400258 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
260 !tls1_check_duplicate_extensions(&extensions) ||
261 CBS_len(&client_hello) != 0) {
262 return 0;
263 }
David Benjamine14ff062016-08-09 16:21:24 -0400264
David Benjamin731058e2016-12-03 23:15:13 -0500265 out->extensions = CBS_data(&extensions);
266 out->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700267
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 return 1;
269}
Adam Langleydc9b1412014-06-20 12:00:00 -0700270
David Benjamin731058e2016-12-03 23:15:13 -0500271int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
272 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 CBS extensions;
David Benjamin731058e2016-12-03 23:15:13 -0500274 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 while (CBS_len(&extensions) != 0) {
David Benjamincec73442016-08-02 17:41:33 -0400276 /* Decode the next extension. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 uint16_t type;
278 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 if (!CBS_get_u16(&extensions, &type) ||
280 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
281 return 0;
282 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400285 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800286 return 1;
287 }
288 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700289
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 return 0;
291}
Adam Langley95c29f32014-06-20 12:00:00 -0700292
David Benjamin731058e2016-12-03 23:15:13 -0500293int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
294 uint16_t extension_type,
295 const uint8_t **out_data,
296 size_t *out_len) {
David Benjamincec73442016-08-02 17:41:33 -0400297 CBS cbs;
David Benjamin731058e2016-12-03 23:15:13 -0500298 if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
David Benjamincec73442016-08-02 17:41:33 -0400299 return 0;
300 }
301
302 *out_data = CBS_data(&cbs);
303 *out_len = CBS_len(&cbs);
304 return 1;
305}
306
Steven Valdezce902a92016-05-17 11:47:53 -0400307static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400308 SSL_CURVE_X25519,
309 SSL_CURVE_SECP256R1,
310 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700311#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin9e68f192016-06-30 14:55:33 -0400312 SSL_CURVE_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700313#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800314};
Adam Langley95c29f32014-06-20 12:00:00 -0700315
David Benjaminf04976b2016-10-07 00:37:55 -0400316void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
Steven Valdez5440fe02016-07-18 12:40:30 -0400317 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400318 *out_group_ids = ssl->supported_group_list;
319 *out_group_ids_len = ssl->supported_group_list_len;
320 if (!*out_group_ids) {
321 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400322 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 }
324}
David Benjamined439582014-07-14 19:13:02 -0400325
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900326int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
327 SSL *const ssl = hs->ssl;
David Benjaminf04976b2016-10-07 00:37:55 -0400328 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400329
David Benjaminf04976b2016-10-07 00:37:55 -0400330 const uint16_t *groups, *pref, *supp;
331 size_t groups_len, pref_len, supp_len;
332 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800333
David Benjaminf04976b2016-10-07 00:37:55 -0400334 /* Clients are not required to send a supported_groups extension. In this
335 * case, the server is free to pick any group it likes. See RFC 4492,
336 * section 4, paragraph 3.
337 *
338 * However, in the interests of compatibility, we will skip ECDH if the
339 * client didn't send an extension because we can't be sure that they'll
340 * support our favoured group. Thus we do not special-case an emtpy
341 * |peer_supported_group_list|. */
David Benjamin55a43642015-04-20 14:45:55 -0400342
David Benjamin4298d772015-12-19 00:18:25 -0500343 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400344 pref = groups;
345 pref_len = groups_len;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900346 supp = hs->peer_supported_group_list;
347 supp_len = hs->peer_supported_group_list_len;
David Benjamin55a43642015-04-20 14:45:55 -0400348 } else {
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900349 pref = hs->peer_supported_group_list;
350 pref_len = hs->peer_supported_group_list_len;
Steven Valdezce902a92016-05-17 11:47:53 -0400351 supp = groups;
352 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400353 }
354
David Benjaminf04976b2016-10-07 00:37:55 -0400355 for (size_t i = 0; i < pref_len; i++) {
356 for (size_t j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400358 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500359 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800360 }
361 }
362 }
363
David Benjamin4298d772015-12-19 00:18:25 -0500364 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800365}
Adam Langley95c29f32014-06-20 12:00:00 -0700366
Steven Valdezce902a92016-05-17 11:47:53 -0400367int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400369 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800370
Steven Valdezce902a92016-05-17 11:47:53 -0400371 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
372 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 return 0;
374 }
375
David Benjamin54091232016-09-05 12:47:25 -0400376 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400377 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
378 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 return 0;
380 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 }
382
Steven Valdezce902a92016-05-17 11:47:53 -0400383 OPENSSL_free(*out_group_ids);
384 *out_group_ids = group_ids;
385 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800386
387 return 1;
388}
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100390int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
391 const char *curves) {
392 uint16_t *group_ids = NULL;
393 size_t ncurves = 0;
394
395 const char *col;
396 const char *ptr = curves;
397
398 do {
399 col = strchr(ptr, ':');
400
401 uint16_t group_id;
402 if (!ssl_name_to_group_id(&group_id, ptr,
403 col ? (size_t)(col - ptr) : strlen(ptr))) {
404 goto err;
405 }
406
407 uint16_t *new_group_ids = OPENSSL_realloc(group_ids,
408 (ncurves + 1) * sizeof(uint16_t));
409 if (new_group_ids == NULL) {
410 goto err;
411 }
412 group_ids = new_group_ids;
413
414 group_ids[ncurves] = group_id;
415 ncurves++;
416
417 if (col) {
418 ptr = col + 1;
419 }
420 } while (col);
421
422 OPENSSL_free(*out_group_ids);
423 *out_group_ids = group_ids;
424 *out_group_ids_len = ncurves;
425
426 return 1;
427
428err:
429 OPENSSL_free(group_ids);
430 return 0;
431}
432
Steven Valdezce902a92016-05-17 11:47:53 -0400433int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
434 const uint16_t *groups;
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400435 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -0400436 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400437 for (size_t i = 0; i < groups_len; i++) {
438 if (groups[i] == group_id) {
439 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800440 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 }
David Benjamin033e5f42014-11-13 18:47:41 -0500442
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400443 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800444}
David Benjamin033e5f42014-11-13 18:47:41 -0500445
David Benjamin3ef76972016-10-17 17:59:54 -0400446/* kVerifySignatureAlgorithms is the default list of accepted signature
David Benjamin3a322f52016-10-26 12:45:35 -0400447 * algorithms for verifying.
448 *
449 * For now, RSA-PSS signature algorithms are not enabled on Android's system
450 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
451 * restore them. */
David Benjamin3ef76972016-10-17 17:59:54 -0400452static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjamin3a322f52016-10-26 12:45:35 -0400453 /* Prefer SHA-256 algorithms. */
454 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400455#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400456 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400457#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400458 SSL_SIGN_RSA_PKCS1_SHA256,
459
460 /* Larger hashes are acceptable. */
461 SSL_SIGN_ECDSA_SECP384R1_SHA384,
462#if !defined(BORINGSSL_ANDROID_SYSTEM)
463 SSL_SIGN_RSA_PSS_SHA384,
464#endif
465 SSL_SIGN_RSA_PKCS1_SHA384,
466
David Benjamin3ef76972016-10-17 17:59:54 -0400467 /* TODO(davidben): Remove this entry and SSL_CURVE_SECP521R1 from
468 * kDefaultGroups. */
469#if defined(BORINGSSL_ANDROID_SYSTEM)
470 SSL_SIGN_ECDSA_SECP521R1_SHA512,
471#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400472#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400473 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400474#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400475 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400476
David Benjamin3a322f52016-10-26 12:45:35 -0400477 /* For now, SHA-1 is still accepted but least preferable. */
478 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400479
David Benjamin3a322f52016-10-26 12:45:35 -0400480};
481
482/* kSignSignatureAlgorithms is the default list of supported signature
483 * algorithms for signing.
484 *
485 * For now, RSA-PSS signature algorithms are not enabled on Android's system
486 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
487 * restore them. */
488static const uint16_t kSignSignatureAlgorithms[] = {
489 /* Prefer SHA-256 algorithms. */
490 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400491#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400492 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400493#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400494 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400495
David Benjamin3a322f52016-10-26 12:45:35 -0400496 /* If needed, sign larger hashes.
497 *
498 * TODO(davidben): Determine which of these may be pruned. */
499 SSL_SIGN_ECDSA_SECP384R1_SHA384,
500#if !defined(BORINGSSL_ANDROID_SYSTEM)
501 SSL_SIGN_RSA_PSS_SHA384,
502#endif
503 SSL_SIGN_RSA_PKCS1_SHA384,
504
505 SSL_SIGN_ECDSA_SECP521R1_SHA512,
506#if !defined(BORINGSSL_ANDROID_SYSTEM)
507 SSL_SIGN_RSA_PSS_SHA512,
508#endif
509 SSL_SIGN_RSA_PKCS1_SHA512,
510
511 /* If the peer supports nothing else, sign with SHA-1. */
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400512 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400513 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400514};
515
David Benjamin3ef76972016-10-17 17:59:54 -0400516size_t tls12_get_verify_sigalgs(const SSL *ssl, const uint16_t **out) {
517 *out = kVerifySignatureAlgorithms;
518 return OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
Adam Langleyfcf25832014-12-18 17:42:32 -0800519}
Adam Langley95c29f32014-06-20 12:00:00 -0700520
David Benjamin887c3002016-07-08 16:15:32 -0700521int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
David Benjamin3ef76972016-10-17 17:59:54 -0400522 const uint16_t *verify_sigalgs;
523 size_t num_verify_sigalgs = tls12_get_verify_sigalgs(ssl, &verify_sigalgs);
524 for (size_t i = 0; i < num_verify_sigalgs; i++) {
525 if (sigalg == verify_sigalgs[i]) {
526 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800527 }
528 }
529
David Benjamin3ef76972016-10-17 17:59:54 -0400530 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
531 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
532 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800533}
534
535/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
536 * supported or doesn't appear in supported signature algorithms. Unlike
537 * ssl_cipher_get_disabled this applies to a specific session and not global
538 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500539void ssl_set_client_disabled(SSL *ssl) {
540 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800541 int have_rsa = 0, have_ecdsa = 0;
542 c->mask_a = 0;
543 c->mask_k = 0;
544
David Benjamin3ef76972016-10-17 17:59:54 -0400545 /* Now go through all signature algorithms seeing if we support any for RSA or
546 * ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin0fc37ef2016-08-17 15:29:46 -0400547 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -0400548 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
David Benjamin0fc37ef2016-08-17 15:29:46 -0400549 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -0400550 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400551 case SSL_SIGN_RSA_PSS_SHA512:
552 case SSL_SIGN_RSA_PSS_SHA384:
553 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400554 case SSL_SIGN_RSA_PKCS1_SHA512:
555 case SSL_SIGN_RSA_PKCS1_SHA384:
556 case SSL_SIGN_RSA_PKCS1_SHA256:
557 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800558 have_rsa = 1;
559 break;
560
Steven Valdez02563852016-06-23 13:33:05 -0400561 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
562 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
563 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
564 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800565 have_ecdsa = 1;
566 break;
567 }
568 }
569
570 /* Disable auth if we don't include any appropriate signature algorithms. */
571 if (!have_rsa) {
572 c->mask_a |= SSL_aRSA;
573 }
574 if (!have_ecdsa) {
575 c->mask_a |= SSL_aECDSA;
576 }
577
578 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500579 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800580 c->mask_a |= SSL_aPSK;
581 c->mask_k |= SSL_kPSK;
582 }
583}
Adam Langley95c29f32014-06-20 12:00:00 -0700584
Adam Langley614c66a2015-06-12 15:26:58 -0700585/* tls_extension represents a TLS extension that is handled internally. The
586 * |init| function is called for each handshake, before any other functions of
587 * the extension. Then the add and parse callbacks are called as needed.
588 *
589 * The parse callbacks receive a |CBS| that contains the contents of the
590 * extension (i.e. not including the type and length bytes). If an extension is
591 * not received then the parse callbacks will be called with a NULL CBS so that
592 * they can do any processing needed to handle the absence of an extension.
593 *
594 * The add callbacks receive a |CBB| to which the extension can be appended but
595 * the function is responsible for appending the type and length bytes too.
596 *
597 * All callbacks return one for success and zero for error. If a parse function
598 * returns zero then a fatal alert with value |*out_alert| will be sent. If
599 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
600struct tls_extension {
601 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500602 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700603
David Benjamin8c880a22016-12-03 02:20:34 -0500604 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
605 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
606 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700607
David Benjamin8c880a22016-12-03 02:20:34 -0500608 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
609 CBS *contents);
610 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700611};
612
David Benjamin8c880a22016-12-03 02:20:34 -0500613static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
614 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400615 if (contents != NULL) {
616 /* Servers MUST NOT send this extension. */
617 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
618 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
619 return 0;
620 }
621
622 return 1;
623}
624
David Benjamin8c880a22016-12-03 02:20:34 -0500625static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
626 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400627 /* This extension from the client is handled elsewhere. */
628 return 1;
629}
630
David Benjamin8c880a22016-12-03 02:20:34 -0500631static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400632 return 1;
633}
Adam Langley614c66a2015-06-12 15:26:58 -0700634
635/* Server name indication (SNI).
636 *
637 * https://tools.ietf.org/html/rfc6066#section-3. */
638
David Benjamin8c880a22016-12-03 02:20:34 -0500639static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
640 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700641 if (ssl->tlsext_hostname == NULL) {
642 return 1;
643 }
644
645 CBB contents, server_name_list, name;
646 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
647 !CBB_add_u16_length_prefixed(out, &contents) ||
648 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
649 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
650 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
651 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
652 strlen(ssl->tlsext_hostname)) ||
653 !CBB_flush(out)) {
654 return 0;
655 }
656
657 return 1;
658}
659
David Benjamin8c880a22016-12-03 02:20:34 -0500660static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500661 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500662 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700663 if (contents == NULL) {
664 return 1;
665 }
666
667 if (CBS_len(contents) != 0) {
668 return 0;
669 }
670
671 assert(ssl->tlsext_hostname != NULL);
672
Steven Valdez87eab492016-06-27 16:34:59 -0400673 if (ssl->session == NULL) {
674 assert(ssl->s3->new_session->tlsext_hostname == NULL);
675 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
676 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700677 *out_alert = SSL_AD_INTERNAL_ERROR;
678 return 0;
679 }
680 }
681
682 return 1;
683}
684
David Benjamin8c880a22016-12-03 02:20:34 -0500685static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500686 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700687 if (contents == NULL) {
688 return 1;
689 }
690
David Benjamin9b611e22016-03-03 08:48:30 -0500691 CBS server_name_list, host_name;
692 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700693 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500694 !CBS_get_u8(&server_name_list, &name_type) ||
695 /* Although the server_name extension was intended to be extensible to
696 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
697 * different name types will cause an error. Further, RFC 4366 originally
698 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
699 * adding new name types is no longer feasible.
700 *
701 * Act as if the extensibility does not exist to simplify parsing. */
702 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
703 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700704 CBS_len(contents) != 0) {
705 return 0;
706 }
707
David Benjamin9b611e22016-03-03 08:48:30 -0500708 if (name_type != TLSEXT_NAMETYPE_host_name ||
709 CBS_len(&host_name) == 0 ||
710 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
711 CBS_contains_zero_byte(&host_name)) {
712 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
713 return 0;
714 }
Adam Langley614c66a2015-06-12 15:26:58 -0700715
David Benjamin4eb95cc2016-11-16 17:08:23 +0900716 /* Copy the hostname as a string. */
David Benjamin8c880a22016-12-03 02:20:34 -0500717 if (!CBS_strdup(&host_name, &hs->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900718 *out_alert = SSL_AD_INTERNAL_ERROR;
719 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700720 }
721
David Benjamin8c880a22016-12-03 02:20:34 -0500722 hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700723 return 1;
724}
725
David Benjamin8c880a22016-12-03 02:20:34 -0500726static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
727 if (hs->ssl->s3->session_reused ||
728 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700729 return 1;
730 }
731
732 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
733 !CBB_add_u16(out, 0 /* length */)) {
734 return 0;
735 }
736
737 return 1;
738}
739
740
Adam Langley5021b222015-06-12 18:27:58 -0700741/* Renegotiation indication.
742 *
743 * https://tools.ietf.org/html/rfc5746 */
744
David Benjamin8c880a22016-12-03 02:20:34 -0500745static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
746 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400747 uint16_t min_version, max_version;
748 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
749 return 0;
750 }
751
752 /* Renegotiation indication is not necessary in TLS 1.3. */
753 if (min_version >= TLS1_3_VERSION) {
754 return 1;
755 }
756
David Benjamin52bf6902016-10-08 12:05:03 -0400757 assert(ssl->s3->initial_handshake_complete ==
758 (ssl->s3->previous_client_finished_len != 0));
759
Adam Langley5021b222015-06-12 18:27:58 -0700760 CBB contents, prev_finished;
761 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
762 !CBB_add_u16_length_prefixed(out, &contents) ||
763 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
764 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
765 ssl->s3->previous_client_finished_len) ||
766 !CBB_flush(out)) {
767 return 0;
768 }
769
770 return 1;
771}
772
David Benjamin8c880a22016-12-03 02:20:34 -0500773static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700774 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500775 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400776 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
777 return 0;
778 }
779
David Benjamin3e052de2015-11-25 20:10:31 -0500780 /* Servers may not switch between omitting the extension and supporting it.
781 * See RFC 5746, sections 3.5 and 4.2. */
782 if (ssl->s3->initial_handshake_complete &&
783 (contents != NULL) != ssl->s3->send_connection_binding) {
784 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
785 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
786 return 0;
787 }
788
Adam Langley5021b222015-06-12 18:27:58 -0700789 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500790 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700791 * RI even on initial ServerHello because the client doesn't see any
792 * renegotiation during an attack. However this would mean we could not
793 * connect to any server which doesn't support RI.
794 *
David Benjamine9cddb82015-11-23 14:36:40 -0500795 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
796 * practical terms every client sets it so it's just assumed here. */
797 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700798 }
799
800 const size_t expected_len = ssl->s3->previous_client_finished_len +
801 ssl->s3->previous_server_finished_len;
802
803 /* Check for logic errors */
804 assert(!expected_len || ssl->s3->previous_client_finished_len);
805 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400806 assert(ssl->s3->initial_handshake_complete ==
807 (ssl->s3->previous_client_finished_len != 0));
808 assert(ssl->s3->initial_handshake_complete ==
809 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700810
811 /* Parse out the extension contents. */
812 CBS renegotiated_connection;
813 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
814 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400815 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700816 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
817 return 0;
818 }
819
820 /* Check that the extension matches. */
821 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400822 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700823 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
824 return 0;
825 }
826
827 const uint8_t *d = CBS_data(&renegotiated_connection);
828 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
829 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400830 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700831 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
832 return 0;
833 }
834 d += ssl->s3->previous_client_finished_len;
835
836 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
837 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400838 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700839 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
840 return 0;
841 }
842 ssl->s3->send_connection_binding = 1;
843
844 return 1;
845}
846
David Benjamin8c880a22016-12-03 02:20:34 -0500847static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700848 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500849 SSL *const ssl = hs->ssl;
Adam Langley5021b222015-06-12 18:27:58 -0700850 /* Renegotiation isn't supported as a server so this function should never be
851 * called after the initial handshake. */
852 assert(!ssl->s3->initial_handshake_complete);
853
Steven Valdez143e8b32016-07-11 13:19:03 -0400854 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
855 return 1;
856 }
857
Adam Langley5021b222015-06-12 18:27:58 -0700858 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400859 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700860 }
861
862 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700863 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
864 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400865 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700866 return 0;
867 }
868
David Benjamin52bf6902016-10-08 12:05:03 -0400869 /* Check that the extension matches. We do not support renegotiation as a
870 * server, so this must be empty. */
871 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400872 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700873 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
874 return 0;
875 }
876
877 ssl->s3->send_connection_binding = 1;
878
879 return 1;
880}
881
David Benjamin8c880a22016-12-03 02:20:34 -0500882static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
883 SSL *const ssl = hs->ssl;
David Benjamin52bf6902016-10-08 12:05:03 -0400884 /* Renegotiation isn't supported as a server so this function should never be
885 * called after the initial handshake. */
886 assert(!ssl->s3->initial_handshake_complete);
887
Steven Valdez143e8b32016-07-11 13:19:03 -0400888 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
889 return 1;
890 }
891
Adam Langley5021b222015-06-12 18:27:58 -0700892 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400893 !CBB_add_u16(out, 1 /* length */) ||
894 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700895 return 0;
896 }
897
898 return 1;
899}
900
Adam Langley0a056712015-07-01 15:03:33 -0700901
902/* Extended Master Secret.
903 *
David Benjamin43946d42016-02-01 08:42:19 -0500904 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700905
David Benjamin8c880a22016-12-03 02:20:34 -0500906static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400907 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -0500908 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400909 return 0;
910 }
911
912 /* Extended master secret is not necessary in TLS 1.3. */
913 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700914 return 1;
915 }
916
917 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
918 !CBB_add_u16(out, 0 /* length */)) {
919 return 0;
920 }
921
922 return 1;
923}
924
David Benjamin8c880a22016-12-03 02:20:34 -0500925static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700926 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500927 SSL *const ssl = hs->ssl;
David Benjamin163c9562016-08-29 23:14:17 -0400928 /* Whether EMS is negotiated may not change on renegotation. */
929 if (ssl->s3->initial_handshake_complete) {
930 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
931 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
932 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
933 return 0;
934 }
935
936 return 1;
937 }
938
Adam Langley0a056712015-07-01 15:03:33 -0700939 if (contents == NULL) {
940 return 1;
941 }
942
Steven Valdez143e8b32016-07-11 13:19:03 -0400943 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
944 ssl->version == SSL3_VERSION) {
945 return 0;
946 }
947
948 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700949 return 0;
950 }
951
952 ssl->s3->tmp.extended_master_secret = 1;
953 return 1;
954}
955
David Benjamin8c880a22016-12-03 02:20:34 -0500956static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500957 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500958 SSL *const ssl = hs->ssl;
959 uint16_t version = ssl3_protocol_version(ssl);
960 if (version >= TLS1_3_VERSION ||
961 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400962 return 1;
963 }
964
965 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700966 return 1;
967 }
968
969 if (CBS_len(contents) != 0) {
970 return 0;
971 }
972
973 ssl->s3->tmp.extended_master_secret = 1;
974 return 1;
975}
976
David Benjamin8c880a22016-12-03 02:20:34 -0500977static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
978 if (!hs->ssl->s3->tmp.extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700979 return 1;
980 }
981
982 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
983 !CBB_add_u16(out, 0 /* length */)) {
984 return 0;
985 }
986
987 return 1;
988}
989
Adam Langley9b05bc52015-07-01 15:25:33 -0700990
991/* Session tickets.
992 *
993 * https://tools.ietf.org/html/rfc5077 */
994
David Benjamin8c880a22016-12-03 02:20:34 -0500995static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
996 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400997 uint16_t min_version, max_version;
998 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
999 return 0;
1000 }
1001
1002 /* TLS 1.3 uses a different ticket extension. */
1003 if (min_version >= TLS1_3_VERSION ||
1004 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001005 return 1;
1006 }
1007
1008 const uint8_t *ticket_data = NULL;
1009 int ticket_len = 0;
1010
1011 /* Renegotiation does not participate in session resumption. However, still
1012 * advertise the extension to avoid potentially breaking servers which carry
1013 * over the state from the previous handshake, such as OpenSSL servers
1014 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -04001015 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -07001016 if (!ssl->s3->initial_handshake_complete &&
1017 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001018 ssl->session->tlsext_tick != NULL &&
1019 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -04001020 ssl->method->version_from_wire(&session_version,
1021 ssl->session->ssl_version) &&
1022 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001023 ticket_data = ssl->session->tlsext_tick;
1024 ticket_len = ssl->session->tlsext_ticklen;
1025 }
1026
1027 CBB ticket;
1028 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1029 !CBB_add_u16_length_prefixed(out, &ticket) ||
1030 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1031 !CBB_flush(out)) {
1032 return 0;
1033 }
1034
1035 return 1;
1036}
1037
David Benjamin8c880a22016-12-03 02:20:34 -05001038static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -07001039 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001040 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -07001041 if (contents == NULL) {
1042 return 1;
1043 }
1044
Steven Valdez143e8b32016-07-11 13:19:03 -04001045 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1046 return 0;
1047 }
1048
Adam Langley9b05bc52015-07-01 15:25:33 -07001049 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1050 * this function should never be called, even if the server tries to send the
1051 * extension. */
1052 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1053
1054 if (CBS_len(contents) != 0) {
1055 return 0;
1056 }
1057
David Benjamin8c880a22016-12-03 02:20:34 -05001058 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001059 return 1;
1060}
1061
David Benjamin8c880a22016-12-03 02:20:34 -05001062static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1063 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001064 return 1;
1065 }
1066
David Benjamin78476f62016-11-12 11:20:55 +09001067 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin8c880a22016-12-03 02:20:34 -05001068 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001069
1070 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1071 !CBB_add_u16(out, 0 /* length */)) {
1072 return 0;
1073 }
1074
1075 return 1;
1076}
1077
1078
Adam Langley2e857bd2015-07-01 16:09:19 -07001079/* Signature Algorithms.
1080 *
1081 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1082
David Benjamin8c880a22016-12-03 02:20:34 -05001083static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1084 SSL *const ssl = hs->ssl;
David Benjamin2dc02042016-09-19 19:57:37 -04001085 uint16_t min_version, max_version;
1086 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1087 return 0;
1088 }
1089
1090 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001091 return 1;
1092 }
1093
David Benjamin0fc37ef2016-08-17 15:29:46 -04001094 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001095 const size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001096
David Benjamin0fc37ef2016-08-17 15:29:46 -04001097 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001098 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1099 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001100 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001101 return 0;
1102 }
1103
David Benjamin0fc37ef2016-08-17 15:29:46 -04001104 for (size_t i = 0; i < num_sigalgs; i++) {
1105 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001106 return 0;
1107 }
1108 }
1109
1110 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001111 return 0;
1112 }
1113
1114 return 1;
1115}
1116
David Benjamin8c880a22016-12-03 02:20:34 -05001117static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001118 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001119 OPENSSL_free(hs->peer_sigalgs);
1120 hs->peer_sigalgs = NULL;
1121 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001122
Adam Langley2e857bd2015-07-01 16:09:19 -07001123 if (contents == NULL) {
1124 return 1;
1125 }
1126
1127 CBS supported_signature_algorithms;
1128 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001129 CBS_len(contents) != 0 ||
1130 CBS_len(&supported_signature_algorithms) == 0 ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001131 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001132 return 0;
1133 }
1134
1135 return 1;
1136}
1137
Adam Langley2e857bd2015-07-01 16:09:19 -07001138
Adam Langleybb0bd042015-07-01 16:21:03 -07001139/* OCSP Stapling.
1140 *
1141 * https://tools.ietf.org/html/rfc6066#section-8 */
1142
David Benjamin8c880a22016-12-03 02:20:34 -05001143static void ext_ocsp_init(SSL_HANDSHAKE *hs) {
1144 hs->ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001145}
1146
David Benjamin8c880a22016-12-03 02:20:34 -05001147static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1148 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001149 if (!ssl->ocsp_stapling_enabled) {
1150 return 1;
1151 }
1152
1153 CBB contents;
1154 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1155 !CBB_add_u16_length_prefixed(out, &contents) ||
1156 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1157 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1158 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1159 !CBB_flush(out)) {
1160 return 0;
1161 }
1162
Adam Langleyce9d85e2016-01-24 15:58:39 -08001163 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001164 return 1;
1165}
1166
David Benjamin8c880a22016-12-03 02:20:34 -05001167static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001168 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001169 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001170 if (contents == NULL) {
1171 return 1;
1172 }
1173
Steven Valdeza833c352016-11-01 13:39:36 -04001174 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1175 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001176 return 0;
1177 }
1178
Steven Valdeza833c352016-11-01 13:39:36 -04001179 /* OCSP stapling is forbidden on non-certificate ciphers. */
1180 if (CBS_len(contents) != 0 ||
1181 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001182 return 0;
1183 }
1184
Steven Valdeza833c352016-11-01 13:39:36 -04001185 /* Note this does not check for resumption in TLS 1.2. Sending
1186 * status_request here does not make sense, but OpenSSL does so and the
1187 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001188
David Benjamin8c880a22016-12-03 02:20:34 -05001189 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001190 return 1;
1191}
1192
David Benjamin8c880a22016-12-03 02:20:34 -05001193static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001194 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001195 if (contents == NULL) {
1196 return 1;
1197 }
1198
1199 uint8_t status_type;
1200 if (!CBS_get_u8(contents, &status_type)) {
1201 return 0;
1202 }
1203
1204 /* We cannot decide whether OCSP stapling will occur yet because the correct
1205 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001206 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001207
Adam Langleybb0bd042015-07-01 16:21:03 -07001208 return 1;
1209}
1210
David Benjamin8c880a22016-12-03 02:20:34 -05001211static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1212 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001213 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001214 !hs->ocsp_stapling_requested ||
David Benjamin942f4ed2016-07-16 19:03:49 +03001215 ssl->ctx->ocsp_response_length == 0 ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001216 ssl->s3->session_reused ||
Steven Valdeza833c352016-11-01 13:39:36 -04001217 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001218 return 1;
1219 }
1220
David Benjamin8c880a22016-12-03 02:20:34 -05001221 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001222
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001223 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001224 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001225}
1226
1227
Adam Langley97dfcbf2015-07-01 18:35:20 -07001228/* Next protocol negotiation.
1229 *
1230 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1231
David Benjamin8c880a22016-12-03 02:20:34 -05001232static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1233 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001234 if (ssl->s3->initial_handshake_complete ||
1235 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin9d125dc2016-12-07 21:32:37 -05001236 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001237 return 1;
1238 }
1239
1240 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1241 !CBB_add_u16(out, 0 /* length */)) {
1242 return 0;
1243 }
1244
1245 return 1;
1246}
1247
David Benjamin8c880a22016-12-03 02:20:34 -05001248static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001249 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001250 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001251 if (contents == NULL) {
1252 return 1;
1253 }
1254
Steven Valdez143e8b32016-07-11 13:19:03 -04001255 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1256 return 0;
1257 }
1258
Adam Langley97dfcbf2015-07-01 18:35:20 -07001259 /* If any of these are false then we should never have sent the NPN
1260 * extension in the ClientHello and thus this function should never have been
1261 * called. */
1262 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001263 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001264 assert(ssl->ctx->next_proto_select_cb != NULL);
1265
David Benjamin76c2efc2015-08-31 14:24:29 -04001266 if (ssl->s3->alpn_selected != NULL) {
1267 /* NPN and ALPN may not be negotiated in the same connection. */
1268 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1269 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1270 return 0;
1271 }
1272
Adam Langley97dfcbf2015-07-01 18:35:20 -07001273 const uint8_t *const orig_contents = CBS_data(contents);
1274 const size_t orig_len = CBS_len(contents);
1275
1276 while (CBS_len(contents) != 0) {
1277 CBS proto;
1278 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1279 CBS_len(&proto) == 0) {
1280 return 0;
1281 }
1282 }
1283
1284 uint8_t *selected;
1285 uint8_t selected_len;
1286 if (ssl->ctx->next_proto_select_cb(
1287 ssl, &selected, &selected_len, orig_contents, orig_len,
1288 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1289 *out_alert = SSL_AD_INTERNAL_ERROR;
1290 return 0;
1291 }
1292
David Benjamin79978df2015-12-25 15:56:49 -05001293 OPENSSL_free(ssl->s3->next_proto_negotiated);
1294 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1295 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001296 *out_alert = SSL_AD_INTERNAL_ERROR;
1297 return 0;
1298 }
1299
David Benjamin79978df2015-12-25 15:56:49 -05001300 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjamin8c880a22016-12-03 02:20:34 -05001301 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001302
1303 return 1;
1304}
1305
David Benjamin8c880a22016-12-03 02:20:34 -05001306static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001307 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001308 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001309 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1310 return 1;
1311 }
1312
Adam Langley97dfcbf2015-07-01 18:35:20 -07001313 if (contents != NULL && CBS_len(contents) != 0) {
1314 return 0;
1315 }
1316
1317 if (contents == NULL ||
1318 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001319 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001320 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001321 return 1;
1322 }
1323
David Benjamin8c880a22016-12-03 02:20:34 -05001324 hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001325 return 1;
1326}
1327
David Benjamin8c880a22016-12-03 02:20:34 -05001328static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1329 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001330 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1331 * parsed. */
David Benjamin8c880a22016-12-03 02:20:34 -05001332 if (!hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001333 return 1;
1334 }
1335
1336 const uint8_t *npa;
1337 unsigned npa_len;
1338
1339 if (ssl->ctx->next_protos_advertised_cb(
1340 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1341 SSL_TLSEXT_ERR_OK) {
David Benjamin8c880a22016-12-03 02:20:34 -05001342 hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001343 return 1;
1344 }
1345
1346 CBB contents;
1347 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1348 !CBB_add_u16_length_prefixed(out, &contents) ||
1349 !CBB_add_bytes(&contents, npa, npa_len) ||
1350 !CBB_flush(out)) {
1351 return 0;
1352 }
1353
1354 return 1;
1355}
1356
1357
Adam Langleyab8d87d2015-07-10 12:21:39 -07001358/* Signed certificate timestamps.
1359 *
1360 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1361
David Benjamin8c880a22016-12-03 02:20:34 -05001362static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1363 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001364 if (!ssl->signed_cert_timestamps_enabled) {
1365 return 1;
1366 }
1367
1368 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1369 !CBB_add_u16(out, 0 /* length */)) {
1370 return 0;
1371 }
1372
1373 return 1;
1374}
1375
David Benjamin8c880a22016-12-03 02:20:34 -05001376static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001377 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001378 SSL *const ssl = hs->ssl;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001379 if (contents == NULL) {
1380 return 1;
1381 }
1382
Steven Valdeza833c352016-11-01 13:39:36 -04001383 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1384 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001385 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001386 return 0;
1387 }
1388
Adam Langleyab8d87d2015-07-10 12:21:39 -07001389 /* If this is false then we should never have sent the SCT extension in the
1390 * ClientHello and thus this function should never have been called. */
1391 assert(ssl->signed_cert_timestamps_enabled);
1392
Adam Langleycfa08c32016-11-17 13:21:27 -08001393 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001394 *out_alert = SSL_AD_DECODE_ERROR;
1395 return 0;
1396 }
1397
David Benjamindaa88502016-10-04 16:32:16 -04001398 /* Session resumption uses the original session information. The extension
1399 * should not be sent on resumption, but RFC 6962 did not make it a
1400 * requirement, so tolerate this.
1401 *
1402 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001403 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001404 !CBS_stow(
1405 contents,
1406 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1407 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001408 *out_alert = SSL_AD_INTERNAL_ERROR;
1409 return 0;
1410 }
1411
1412 return 1;
1413}
1414
David Benjamin8c880a22016-12-03 02:20:34 -05001415static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyab8d87d2015-07-10 12:21:39 -07001416 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001417 if (contents == NULL) {
1418 return 1;
1419 }
1420
1421 if (CBS_len(contents) != 0) {
1422 return 0;
1423 }
1424
David Benjamin8c880a22016-12-03 02:20:34 -05001425 hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001426 return 1;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001427}
1428
David Benjamin8c880a22016-12-03 02:20:34 -05001429static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1430 SSL *const ssl = hs->ssl;
Paul Lietar62be8ac2015-09-16 10:03:30 +01001431 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001432 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1433 ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001434 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001435 return 1;
1436 }
1437
1438 CBB contents;
1439 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1440 CBB_add_u16_length_prefixed(out, &contents) &&
1441 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1442 ssl->ctx->signed_cert_timestamp_list_length) &&
1443 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001444}
1445
1446
Adam Langleyf18e4532015-07-10 13:39:53 -07001447/* Application-level Protocol Negotiation.
1448 *
1449 * https://tools.ietf.org/html/rfc7301 */
1450
David Benjamin8c880a22016-12-03 02:20:34 -05001451static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1452 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001453 if (ssl->alpn_client_proto_list == NULL ||
1454 ssl->s3->initial_handshake_complete) {
1455 return 1;
1456 }
1457
1458 CBB contents, proto_list;
1459 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1460 !CBB_add_u16_length_prefixed(out, &contents) ||
1461 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1462 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1463 ssl->alpn_client_proto_list_len) ||
1464 !CBB_flush(out)) {
1465 return 0;
1466 }
1467
1468 return 1;
1469}
1470
David Benjamin8c880a22016-12-03 02:20:34 -05001471static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleyf18e4532015-07-10 13:39:53 -07001472 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001473 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001474 if (contents == NULL) {
1475 return 1;
1476 }
1477
1478 assert(!ssl->s3->initial_handshake_complete);
1479 assert(ssl->alpn_client_proto_list != NULL);
1480
David Benjamin8c880a22016-12-03 02:20:34 -05001481 if (hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001482 /* NPN and ALPN may not be negotiated in the same connection. */
1483 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1484 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1485 return 0;
1486 }
1487
Adam Langleyf18e4532015-07-10 13:39:53 -07001488 /* The extension data consists of a ProtocolNameList which must have
1489 * exactly one ProtocolName. Each of these is length-prefixed. */
1490 CBS protocol_name_list, protocol_name;
1491 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1492 CBS_len(contents) != 0 ||
1493 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1494 /* Empty protocol names are forbidden. */
1495 CBS_len(&protocol_name) == 0 ||
1496 CBS_len(&protocol_name_list) != 0) {
1497 return 0;
1498 }
1499
David Benjamin3e517572016-08-11 11:52:23 -04001500 /* Check that the protcol name is one of the ones we advertised. */
1501 int protocol_ok = 0;
1502 CBS client_protocol_name_list, client_protocol_name;
1503 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1504 ssl->alpn_client_proto_list_len);
1505 while (CBS_len(&client_protocol_name_list) > 0) {
1506 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1507 &client_protocol_name)) {
1508 *out_alert = SSL_AD_INTERNAL_ERROR;
1509 return 0;
1510 }
1511
1512 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1513 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1514 CBS_len(&protocol_name)) == 0) {
1515 protocol_ok = 1;
1516 break;
1517 }
1518 }
1519
1520 if (!protocol_ok) {
1521 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1522 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1523 return 0;
1524 }
1525
Adam Langleyf18e4532015-07-10 13:39:53 -07001526 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1527 &ssl->s3->alpn_selected_len)) {
1528 *out_alert = SSL_AD_INTERNAL_ERROR;
1529 return 0;
1530 }
1531
1532 return 1;
1533}
1534
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001535int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -05001536 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001537 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -04001538 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001539 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin731058e2016-12-03 23:15:13 -05001540 !ssl_client_hello_get_extension(
David Benjamin9ef31f02016-10-31 18:01:13 -04001541 client_hello, &contents,
1542 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1543 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001544 return 1;
1545 }
1546
1547 /* ALPN takes precedence over NPN. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001548 hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001549
1550 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001551 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1552 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001553 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001554 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1555 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001556 return 0;
1557 }
1558
1559 /* Validate the protocol list. */
1560 CBS protocol_name_list_copy = protocol_name_list;
1561 while (CBS_len(&protocol_name_list_copy) > 0) {
1562 CBS protocol_name;
1563
1564 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1565 /* Empty protocol names are forbidden. */
1566 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001567 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1568 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001569 return 0;
1570 }
1571 }
1572
1573 const uint8_t *selected;
1574 uint8_t selected_len;
1575 if (ssl->ctx->alpn_select_cb(
1576 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1577 CBS_len(&protocol_name_list),
1578 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1579 OPENSSL_free(ssl->s3->alpn_selected);
1580 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1581 if (ssl->s3->alpn_selected == NULL) {
1582 *out_alert = SSL_AD_INTERNAL_ERROR;
1583 return 0;
1584 }
1585 ssl->s3->alpn_selected_len = selected_len;
1586 }
1587
1588 return 1;
1589}
1590
David Benjamin8c880a22016-12-03 02:20:34 -05001591static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1592 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001593 if (ssl->s3->alpn_selected == NULL) {
1594 return 1;
1595 }
1596
1597 CBB contents, proto_list, proto;
1598 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1599 !CBB_add_u16_length_prefixed(out, &contents) ||
1600 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1601 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001602 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1603 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001604 !CBB_flush(out)) {
1605 return 0;
1606 }
1607
1608 return 1;
1609}
1610
1611
Adam Langley49c7af12015-07-10 14:33:46 -07001612/* Channel ID.
1613 *
1614 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1615
David Benjamin8c880a22016-12-03 02:20:34 -05001616static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1617 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001618}
1619
David Benjamin8c880a22016-12-03 02:20:34 -05001620static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1621 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001622 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001623 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001624 return 1;
1625 }
1626
1627 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1628 !CBB_add_u16(out, 0 /* length */)) {
1629 return 0;
1630 }
1631
1632 return 1;
1633}
1634
David Benjamin8c880a22016-12-03 02:20:34 -05001635static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1636 uint8_t *out_alert, CBS *contents) {
1637 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001638 if (contents == NULL) {
1639 return 1;
1640 }
1641
David Benjamince079fd2016-08-02 16:22:34 -04001642 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001643 assert(ssl->tlsext_channel_id_enabled);
1644
1645 if (CBS_len(contents) != 0) {
1646 return 0;
1647 }
1648
1649 ssl->s3->tlsext_channel_id_valid = 1;
1650 return 1;
1651}
1652
David Benjamin8c880a22016-12-03 02:20:34 -05001653static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1654 uint8_t *out_alert, CBS *contents) {
1655 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001656 if (contents == NULL ||
1657 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001658 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001659 return 1;
1660 }
1661
1662 if (CBS_len(contents) != 0) {
1663 return 0;
1664 }
1665
1666 ssl->s3->tlsext_channel_id_valid = 1;
1667 return 1;
1668}
1669
David Benjamin8c880a22016-12-03 02:20:34 -05001670static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1671 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001672 if (!ssl->s3->tlsext_channel_id_valid) {
1673 return 1;
1674 }
1675
1676 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1677 !CBB_add_u16(out, 0 /* length */)) {
1678 return 0;
1679 }
1680
1681 return 1;
1682}
1683
Adam Langley391250d2015-07-15 19:06:07 -07001684
1685/* Secure Real-time Transport Protocol (SRTP) extension.
1686 *
1687 * https://tools.ietf.org/html/rfc5764 */
1688
Adam Langley391250d2015-07-15 19:06:07 -07001689
David Benjamin8c880a22016-12-03 02:20:34 -05001690static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1691 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001692}
1693
David Benjamin8c880a22016-12-03 02:20:34 -05001694static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1695 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001696 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1697 if (profiles == NULL) {
1698 return 1;
1699 }
1700 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1701 if (num_profiles == 0) {
1702 return 1;
1703 }
1704
1705 CBB contents, profile_ids;
1706 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1707 !CBB_add_u16_length_prefixed(out, &contents) ||
1708 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1709 return 0;
1710 }
1711
David Benjamin54091232016-09-05 12:47:25 -04001712 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001713 if (!CBB_add_u16(&profile_ids,
1714 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1715 return 0;
1716 }
1717 }
1718
1719 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1720 !CBB_flush(out)) {
1721 return 0;
1722 }
1723
1724 return 1;
1725}
1726
David Benjamin8c880a22016-12-03 02:20:34 -05001727static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001728 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001729 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001730 if (contents == NULL) {
1731 return 1;
1732 }
1733
1734 /* The extension consists of a u16-prefixed profile ID list containing a
1735 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1736 *
1737 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1738 CBS profile_ids, srtp_mki;
1739 uint16_t profile_id;
1740 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1741 !CBS_get_u16(&profile_ids, &profile_id) ||
1742 CBS_len(&profile_ids) != 0 ||
1743 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1744 CBS_len(contents) != 0) {
1745 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1746 return 0;
1747 }
1748
1749 if (CBS_len(&srtp_mki) != 0) {
1750 /* Must be no MKI, since we never offer one. */
1751 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1752 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1753 return 0;
1754 }
1755
1756 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1757
1758 /* Check to see if the server gave us something we support (and presumably
1759 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001760 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001761 const SRTP_PROTECTION_PROFILE *profile =
1762 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1763
1764 if (profile->id == profile_id) {
1765 ssl->srtp_profile = profile;
1766 return 1;
1767 }
1768 }
1769
1770 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1771 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1772 return 0;
1773}
1774
David Benjamin8c880a22016-12-03 02:20:34 -05001775static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001776 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001777 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001778 if (contents == NULL) {
1779 return 1;
1780 }
1781
1782 CBS profile_ids, srtp_mki;
1783 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1784 CBS_len(&profile_ids) < 2 ||
1785 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1786 CBS_len(contents) != 0) {
1787 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1788 return 0;
1789 }
1790 /* Discard the MKI value for now. */
1791
1792 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1793 SSL_get_srtp_profiles(ssl);
1794
1795 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001796 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001797 const SRTP_PROTECTION_PROFILE *server_profile =
1798 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1799
1800 CBS profile_ids_tmp;
1801 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1802
1803 while (CBS_len(&profile_ids_tmp) > 0) {
1804 uint16_t profile_id;
1805 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1806 return 0;
1807 }
1808
1809 if (server_profile->id == profile_id) {
1810 ssl->srtp_profile = server_profile;
1811 return 1;
1812 }
1813 }
1814 }
1815
1816 return 1;
1817}
1818
David Benjamin8c880a22016-12-03 02:20:34 -05001819static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1820 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001821 if (ssl->srtp_profile == NULL) {
1822 return 1;
1823 }
1824
1825 CBB contents, profile_ids;
1826 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1827 !CBB_add_u16_length_prefixed(out, &contents) ||
1828 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1829 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1830 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1831 !CBB_flush(out)) {
1832 return 0;
1833 }
1834
1835 return 1;
1836}
1837
Adam Langleybdd5d662015-07-20 16:19:08 -07001838
1839/* EC point formats.
1840 *
1841 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1842
David Benjamin8c880a22016-12-03 02:20:34 -05001843static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001844 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001845 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1846 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001847 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1848 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001849 !CBB_flush(out)) {
1850 return 0;
1851 }
1852
1853 return 1;
1854}
1855
David Benjamin8c880a22016-12-03 02:20:34 -05001856static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001857 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -05001858 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin70aba262016-11-01 12:08:15 -04001859 return 0;
1860 }
1861
1862 /* The point format extension is unneccessary in TLS 1.3. */
1863 if (min_version >= TLS1_3_VERSION) {
1864 return 1;
1865 }
1866
David Benjamin8c880a22016-12-03 02:20:34 -05001867 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001868}
1869
David Benjamin8c880a22016-12-03 02:20:34 -05001870static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001871 CBS *contents) {
1872 if (contents == NULL) {
1873 return 1;
1874 }
1875
David Benjamin8c880a22016-12-03 02:20:34 -05001876 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001877 return 0;
1878 }
1879
Adam Langleybdd5d662015-07-20 16:19:08 -07001880 CBS ec_point_format_list;
1881 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1882 CBS_len(contents) != 0) {
1883 return 0;
1884 }
1885
David Benjaminfc059942015-07-30 23:01:59 -04001886 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1887 * point format. */
1888 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1889 CBS_len(&ec_point_format_list)) == NULL) {
1890 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001891 return 0;
1892 }
1893
1894 return 1;
1895}
1896
David Benjamin8c880a22016-12-03 02:20:34 -05001897static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001898 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001899 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001900 return 1;
1901 }
1902
David Benjamin8c880a22016-12-03 02:20:34 -05001903 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001904}
1905
David Benjamin8c880a22016-12-03 02:20:34 -05001906static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1907 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001908 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1909 return 1;
1910 }
1911
Adam Langleybdd5d662015-07-20 16:19:08 -07001912 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1913 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001914 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001915
1916 if (!using_ecc) {
1917 return 1;
1918 }
1919
David Benjamin8c880a22016-12-03 02:20:34 -05001920 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001921}
1922
Steven Valdeza833c352016-11-01 13:39:36 -04001923
Steven Valdez4aa154e2016-07-29 14:32:55 -04001924/* Pre Shared Key
1925 *
Steven Valdeza833c352016-11-01 13:39:36 -04001926 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001927
David Benjamin8c880a22016-12-03 02:20:34 -05001928static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1929 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001930 uint16_t min_version, max_version;
1931 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1932 return 0;
1933 }
1934
1935 uint16_t session_version;
1936 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1937 !ssl->method->version_from_wire(&session_version,
1938 ssl->session->ssl_version) ||
1939 session_version < TLS1_3_VERSION) {
1940 return 0;
1941 }
1942
1943 const EVP_MD *digest =
1944 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1945 size_t binder_len = EVP_MD_size(digest);
1946 return 15 + ssl->session->tlsext_ticklen + binder_len;
1947}
1948
David Benjamin8c880a22016-12-03 02:20:34 -05001949static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1950 SSL *const ssl = hs->ssl;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001951 uint16_t min_version, max_version;
1952 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1953 return 0;
1954 }
1955
David Benjamin2dc02042016-09-19 19:57:37 -04001956 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001957 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001958 !ssl->method->version_from_wire(&session_version,
1959 ssl->session->ssl_version) ||
1960 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001961 return 1;
1962 }
1963
Steven Valdeza833c352016-11-01 13:39:36 -04001964 struct timeval now;
1965 ssl_get_current_time(ssl, &now);
1966 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1967 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1968
1969 /* Fill in a placeholder zero binder of the appropriate length. It will be
1970 * computed and filled in later after length prefixes are computed. */
1971 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
1972 const EVP_MD *digest =
1973 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1974 size_t binder_len = EVP_MD_size(digest);
1975
1976 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001977 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1978 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001979 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001980 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1981 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001982 ssl->session->tlsext_ticklen) ||
1983 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1984 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1985 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1986 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001987 return 0;
1988 }
1989
David Benjamin8c880a22016-12-03 02:20:34 -05001990 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001991 return CBB_flush(out);
1992}
1993
David Benjamin8baf9632016-11-17 17:11:16 +09001994int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1995 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001996 CBS *contents) {
1997 uint16_t psk_id;
1998 if (!CBS_get_u16(contents, &psk_id) ||
1999 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04002000 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002001 *out_alert = SSL_AD_DECODE_ERROR;
2002 return 0;
2003 }
2004
David Benjamineab773a2016-11-09 18:32:35 -05002005 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04002006 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04002007 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002008 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2009 return 0;
2010 }
2011
2012 return 1;
2013}
2014
David Benjamin8baf9632016-11-17 17:11:16 +09002015int ssl_ext_pre_shared_key_parse_clienthello(SSL_HANDSHAKE *hs,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002016 SSL_SESSION **out_session,
Steven Valdeza833c352016-11-01 13:39:36 -04002017 CBS *out_binders,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002018 uint8_t *out_alert,
2019 CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09002020 SSL *const ssl = hs->ssl;
Steven Valdez5b986082016-09-01 12:29:49 -04002021 /* We only process the first PSK identity since we don't support pure PSK. */
Steven Valdeza833c352016-11-01 13:39:36 -04002022 uint32_t obfuscated_ticket_age;
David Benjaminaedf3032016-12-01 16:47:56 -05002023 CBS identities, ticket, binders;
2024 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2025 !CBS_get_u16_length_prefixed(&identities, &ticket) ||
2026 !CBS_get_u32(&identities, &obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04002027 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05002028 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04002029 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04002030 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002031 *out_alert = SSL_AD_DECODE_ERROR;
2032 return 0;
2033 }
2034
Steven Valdeza833c352016-11-01 13:39:36 -04002035 *out_binders = binders;
2036
David Benjaminaedf3032016-12-01 16:47:56 -05002037 /* Check the syntax of the remaining identities, but do not process them. */
2038 size_t num_identities = 1;
2039 while (CBS_len(&identities) != 0) {
2040 CBS unused_ticket;
2041 uint32_t unused_obfuscated_ticket_age;
2042 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
2043 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
2044 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2045 *out_alert = SSL_AD_DECODE_ERROR;
2046 return 0;
2047 }
2048
2049 num_identities++;
2050 }
2051
2052 /* Check the syntax of the binders. The value will be checked later if
2053 * resuming. */
2054 size_t num_binders = 0;
2055 while (CBS_len(&binders) != 0) {
2056 CBS binder;
2057 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
2058 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2059 *out_alert = SSL_AD_DECODE_ERROR;
2060 return 0;
2061 }
2062
2063 num_binders++;
2064 }
2065
2066 if (num_identities != num_binders) {
2067 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
2068 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04002069 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04002070 }
2071
Steven Valdeza833c352016-11-01 13:39:36 -04002072 /* TODO(svaldez): Check that the ticket_age is valid when attempting to use
2073 * the PSK for 0-RTT. http://crbug.com/boringssl/113 */
2074
Steven Valdez4aa154e2016-07-29 14:32:55 -04002075 /* TLS 1.3 session tickets are renewed separately as part of the
2076 * NewSessionTicket. */
David Benjamine7f60a22016-11-16 18:54:25 +09002077 int unused_renew;
2078 if (!tls_process_ticket(ssl, out_session, &unused_renew, CBS_data(&ticket),
2079 CBS_len(&ticket), NULL, 0)) {
2080 *out_alert = SSL_AD_INTERNAL_ERROR;
2081 return 0;
2082 }
2083
2084 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002085}
2086
David Benjamin8baf9632016-11-17 17:11:16 +09002087int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2088 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002089 return 1;
2090 }
2091
2092 CBB contents;
2093 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2094 !CBB_add_u16_length_prefixed(out, &contents) ||
2095 /* We only consider the first identity for resumption */
2096 !CBB_add_u16(&contents, 0) ||
2097 !CBB_flush(out)) {
2098 return 0;
2099 }
2100
2101 return 1;
2102}
2103
2104
Steven Valdeza833c352016-11-01 13:39:36 -04002105/* Pre-Shared Key Exchange Modes
2106 *
2107 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002108
David Benjamin8c880a22016-12-03 02:20:34 -05002109static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2110 CBB *out) {
2111 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04002112 uint16_t min_version, max_version;
2113 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2114 return 0;
2115 }
2116
2117 if (max_version < TLS1_3_VERSION) {
2118 return 1;
2119 }
2120
2121 CBB contents, ke_modes;
2122 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2123 !CBB_add_u16_length_prefixed(out, &contents) ||
2124 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2125 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2126 return 0;
2127 }
2128
2129 return CBB_flush(out);
2130}
2131
David Benjamin8c880a22016-12-03 02:20:34 -05002132static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002133 uint8_t *out_alert,
2134 CBS *contents) {
2135 if (contents == NULL) {
2136 return 1;
2137 }
2138
Steven Valdeza833c352016-11-01 13:39:36 -04002139 CBS ke_modes;
2140 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2141 CBS_len(&ke_modes) == 0 ||
2142 CBS_len(contents) != 0) {
2143 *out_alert = SSL_AD_DECODE_ERROR;
2144 return 0;
2145 }
2146
2147 /* We only support tickets with PSK_DHE_KE. */
David Benjamin8c880a22016-12-03 02:20:34 -05002148 hs->accept_psk_mode =
Steven Valdeza833c352016-11-01 13:39:36 -04002149 memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, CBS_len(&ke_modes)) != NULL;
2150
2151 return 1;
2152}
2153
2154
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002155/* Early Data Indication
2156 *
2157 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2158
David Benjamin8c880a22016-12-03 02:20:34 -05002159static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002160 /* TODO(svaldez): Support 0RTT. */
2161 return 1;
2162}
2163
David Benjamin8c880a22016-12-03 02:20:34 -05002164static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2165 uint8_t *out_alert, CBS *contents) {
2166 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002167 if (contents == NULL) {
2168 return 1;
2169 }
2170
2171 if (CBS_len(contents) != 0) {
2172 *out_alert = SSL_AD_DECODE_ERROR;
2173 return 0;
2174 }
2175
2176 /* Since we don't currently accept 0-RTT, we have to skip past any early data
2177 * the client might have sent. */
2178 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
2179 ssl->s3->skip_early_data = 1;
2180 }
2181 return 1;
2182}
2183
2184
Steven Valdez143e8b32016-07-11 13:19:03 -04002185/* Key Share
2186 *
David Benjamina128a552016-10-13 14:26:33 -04002187 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002188
David Benjamin8c880a22016-12-03 02:20:34 -05002189static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2190 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002191 uint16_t min_version, max_version;
2192 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2193 return 0;
2194 }
2195
David Benjamin53a2dfc2016-10-07 21:04:12 -04002196 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002197 return 1;
2198 }
2199
2200 CBB contents, kse_bytes;
2201 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2202 !CBB_add_u16_length_prefixed(out, &contents) ||
2203 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2204 return 0;
2205 }
2206
David Benjamin8c880a22016-12-03 02:20:34 -05002207 uint16_t group_id = hs->retry_group;
2208 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002209 /* We received a HelloRetryRequest without a new curve, so there is no new
2210 * share to append. Leave |ecdh_ctx| as-is. */
2211 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002212 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2213 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002214 return 0;
2215 }
David Benjamin8c880a22016-12-03 02:20:34 -05002216 OPENSSL_free(hs->key_share_bytes);
2217 hs->key_share_bytes = NULL;
2218 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002219 if (group_id == 0) {
2220 return CBB_flush(out);
2221 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002222 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002223 /* Add a fake group. See draft-davidben-tls-grease-01. */
2224 if (ssl->ctx->grease_enabled &&
2225 (!CBB_add_u16(&kse_bytes,
2226 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2227 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2228 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2229 return 0;
2230 }
2231
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002232 /* Predict the most preferred group. */
2233 const uint16_t *groups;
2234 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002235 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002236 if (groups_len == 0) {
2237 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2238 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002239 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002240
2241 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002242 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002243
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002244 CBB key_exchange;
2245 if (!CBB_add_u16(&kse_bytes, group_id) ||
2246 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002247 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2248 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002249 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002250 return 0;
2251 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002252
David Benjamin8c880a22016-12-03 02:20:34 -05002253 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002254 /* Save the contents of the extension to repeat it in the second
2255 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002256 hs->key_share_bytes_len = CBB_len(&kse_bytes);
2257 hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
2258 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002259 return 0;
2260 }
2261 }
2262
Steven Valdez143e8b32016-07-11 13:19:03 -04002263 return CBB_flush(out);
2264}
2265
David Benjamin8baf9632016-11-17 17:11:16 +09002266int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002267 size_t *out_secret_len,
2268 uint8_t *out_alert, CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09002269 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002270 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002271 uint16_t group_id;
2272 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002273 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2274 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002275 *out_alert = SSL_AD_DECODE_ERROR;
2276 return 0;
2277 }
2278
David Benjamin8baf9632016-11-17 17:11:16 +09002279 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002280 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2281 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2282 return 0;
2283 }
2284
David Benjamin8baf9632016-11-17 17:11:16 +09002285 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2286 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002287 *out_alert = SSL_AD_INTERNAL_ERROR;
2288 return 0;
2289 }
2290
David Benjamin5c4e8572016-08-19 17:44:53 -04002291 ssl->s3->new_session->key_exchange_info = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002292 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002293 return 1;
2294}
2295
David Benjamin8baf9632016-11-17 17:11:16 +09002296int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002297 uint8_t **out_secret,
2298 size_t *out_secret_len,
2299 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002300 uint16_t group_id;
2301 CBS key_shares;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002302 if (!tls1_get_shared_group(hs, &group_id)) {
Steven Valdez803c77a2016-09-06 14:13:43 -04002303 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2304 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2305 return 0;
2306 }
2307
2308 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002309 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002310 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002311 return 0;
2312 }
2313
David Benjamin7e1f9842016-09-20 19:24:40 -04002314 /* Find the corresponding key share. */
2315 int found = 0;
2316 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002317 while (CBS_len(&key_shares) > 0) {
2318 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002319 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002320 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002321 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2322 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002323 return 0;
2324 }
2325
David Benjamin7e1f9842016-09-20 19:24:40 -04002326 if (id == group_id) {
2327 if (found) {
2328 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2329 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2330 return 0;
2331 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002332
David Benjamin7e1f9842016-09-20 19:24:40 -04002333 found = 1;
2334 peer_key = peer_key_tmp;
2335 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002336 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002337 }
2338
David Benjamin7e1f9842016-09-20 19:24:40 -04002339 if (!found) {
2340 *out_found = 0;
2341 *out_secret = NULL;
2342 *out_secret_len = 0;
2343 return 1;
2344 }
2345
2346 /* Compute the DH secret. */
2347 uint8_t *secret = NULL;
2348 size_t secret_len;
2349 SSL_ECDH_CTX group;
2350 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2351 CBB public_key;
2352 if (!CBB_init(&public_key, 32) ||
2353 !SSL_ECDH_CTX_init(&group, group_id) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002354 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, out_alert,
2355 CBS_data(&peer_key), CBS_len(&peer_key)) ||
2356 !CBB_finish(&public_key, &hs->public_key, &hs->public_key_len)) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002357 OPENSSL_free(secret);
2358 SSL_ECDH_CTX_cleanup(&group);
2359 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002360 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002361 return 0;
2362 }
2363
2364 SSL_ECDH_CTX_cleanup(&group);
2365
2366 *out_secret = secret;
2367 *out_secret_len = secret_len;
2368 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002369 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002370}
2371
David Benjamin8baf9632016-11-17 17:11:16 +09002372int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2373 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002374 uint16_t group_id;
2375 CBB kse_bytes, public_key;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09002376 if (!tls1_get_shared_group(hs, &group_id) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002377 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2378 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2379 !CBB_add_u16(&kse_bytes, group_id) ||
2380 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
David Benjamin8baf9632016-11-17 17:11:16 +09002381 !CBB_add_bytes(&public_key, hs->public_key, hs->public_key_len) ||
Steven Valdez143e8b32016-07-11 13:19:03 -04002382 !CBB_flush(out)) {
2383 return 0;
2384 }
2385
David Benjamin8baf9632016-11-17 17:11:16 +09002386 OPENSSL_free(hs->public_key);
2387 hs->public_key = NULL;
2388 hs->public_key_len = 0;
David Benjamin4fe3c902016-08-16 02:17:03 -04002389
David Benjamin5c4e8572016-08-19 17:44:53 -04002390 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002391 return 1;
2392}
2393
2394
Steven Valdezfdd10992016-09-15 16:27:05 -04002395/* Supported Versions
2396 *
2397 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2398
David Benjamin8c880a22016-12-03 02:20:34 -05002399static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2400 SSL *const ssl = hs->ssl;
Steven Valdezfdd10992016-09-15 16:27:05 -04002401 uint16_t min_version, max_version;
2402 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2403 return 0;
2404 }
2405
2406 if (max_version <= TLS1_2_VERSION) {
2407 return 1;
2408 }
2409
2410 CBB contents, versions;
2411 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2412 !CBB_add_u16_length_prefixed(out, &contents) ||
2413 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2414 return 0;
2415 }
2416
David Benjamind9791bf2016-09-27 16:39:52 -04002417 /* Add a fake version. See draft-davidben-tls-grease-01. */
2418 if (ssl->ctx->grease_enabled &&
2419 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2420 return 0;
2421 }
2422
Steven Valdezfdd10992016-09-15 16:27:05 -04002423 for (uint16_t version = max_version; version >= min_version; version--) {
2424 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2425 return 0;
2426 }
2427 }
2428
2429 if (!CBB_flush(out)) {
2430 return 0;
2431 }
2432
2433 return 1;
2434}
2435
2436
David Benjamin3baa6e12016-10-07 21:10:38 -04002437/* Cookie
2438 *
2439 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2440
David Benjamin8c880a22016-12-03 02:20:34 -05002441static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2442 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002443 return 1;
2444 }
2445
2446 CBB contents, cookie;
2447 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2448 !CBB_add_u16_length_prefixed(out, &contents) ||
2449 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002450 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002451 !CBB_flush(out)) {
2452 return 0;
2453 }
2454
2455 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002456 OPENSSL_free(hs->cookie);
2457 hs->cookie = NULL;
2458 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002459 return 1;
2460}
2461
2462
Steven Valdezce902a92016-05-17 11:47:53 -04002463/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002464 *
Steven Valdezce902a92016-05-17 11:47:53 -04002465 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002466 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002467
David Benjamin8c880a22016-12-03 02:20:34 -05002468static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2469 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002470 CBB contents, groups_bytes;
2471 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002472 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002473 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002474 return 0;
2475 }
2476
David Benjamin65ac9972016-09-02 21:35:25 -04002477 /* Add a fake group. See draft-davidben-tls-grease-01. */
2478 if (ssl->ctx->grease_enabled &&
2479 !CBB_add_u16(&groups_bytes,
2480 ssl_get_grease_value(ssl, ssl_grease_group))) {
2481 return 0;
2482 }
2483
Steven Valdezce902a92016-05-17 11:47:53 -04002484 const uint16_t *groups;
2485 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002486 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002487
David Benjamin54091232016-09-05 12:47:25 -04002488 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002489 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002490 return 0;
2491 }
2492 }
2493
2494 return CBB_flush(out);
2495}
2496
David Benjamin8c880a22016-12-03 02:20:34 -05002497static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2498 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002499 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002500 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2501 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002502 return 1;
2503}
2504
David Benjamin8c880a22016-12-03 02:20:34 -05002505static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2506 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002507 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002508 if (contents == NULL) {
2509 return 1;
2510 }
2511
Steven Valdezce902a92016-05-17 11:47:53 -04002512 CBS supported_group_list;
2513 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2514 CBS_len(&supported_group_list) == 0 ||
2515 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002516 CBS_len(contents) != 0) {
2517 return 0;
2518 }
2519
David Benjamin8c880a22016-12-03 02:20:34 -05002520 hs->peer_supported_group_list =
2521 OPENSSL_malloc(CBS_len(&supported_group_list));
2522 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002523 *out_alert = SSL_AD_INTERNAL_ERROR;
2524 return 0;
2525 }
2526
Steven Valdezce902a92016-05-17 11:47:53 -04002527 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002528 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002529 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002530 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002531 goto err;
2532 }
2533 }
2534
Steven Valdezce902a92016-05-17 11:47:53 -04002535 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002536 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002537
2538 return 1;
2539
2540err:
David Benjamin8c880a22016-12-03 02:20:34 -05002541 OPENSSL_free(hs->peer_supported_group_list);
2542 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002543 *out_alert = SSL_AD_INTERNAL_ERROR;
2544 return 0;
2545}
2546
David Benjamin8c880a22016-12-03 02:20:34 -05002547static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002548 /* Servers don't echo this extension. */
2549 return 1;
2550}
2551
2552
Adam Langley614c66a2015-06-12 15:26:58 -07002553/* kExtensions contains all the supported extensions. */
2554static const struct tls_extension kExtensions[] = {
2555 {
Adam Langley5021b222015-06-12 18:27:58 -07002556 TLSEXT_TYPE_renegotiate,
2557 NULL,
2558 ext_ri_add_clienthello,
2559 ext_ri_parse_serverhello,
2560 ext_ri_parse_clienthello,
2561 ext_ri_add_serverhello,
2562 },
2563 {
Adam Langley614c66a2015-06-12 15:26:58 -07002564 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002565 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002566 ext_sni_add_clienthello,
2567 ext_sni_parse_serverhello,
2568 ext_sni_parse_clienthello,
2569 ext_sni_add_serverhello,
2570 },
Adam Langley0a056712015-07-01 15:03:33 -07002571 {
2572 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002573 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002574 ext_ems_add_clienthello,
2575 ext_ems_parse_serverhello,
2576 ext_ems_parse_clienthello,
2577 ext_ems_add_serverhello,
2578 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002579 {
2580 TLSEXT_TYPE_session_ticket,
2581 NULL,
2582 ext_ticket_add_clienthello,
2583 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002584 /* Ticket extension client parsing is handled in ssl_session.c */
2585 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002586 ext_ticket_add_serverhello,
2587 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002588 {
2589 TLSEXT_TYPE_signature_algorithms,
2590 NULL,
2591 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002592 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002593 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002594 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002595 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002596 {
2597 TLSEXT_TYPE_status_request,
2598 ext_ocsp_init,
2599 ext_ocsp_add_clienthello,
2600 ext_ocsp_parse_serverhello,
2601 ext_ocsp_parse_clienthello,
2602 ext_ocsp_add_serverhello,
2603 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002604 {
2605 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002606 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002607 ext_npn_add_clienthello,
2608 ext_npn_parse_serverhello,
2609 ext_npn_parse_clienthello,
2610 ext_npn_add_serverhello,
2611 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002612 {
2613 TLSEXT_TYPE_certificate_timestamp,
2614 NULL,
2615 ext_sct_add_clienthello,
2616 ext_sct_parse_serverhello,
2617 ext_sct_parse_clienthello,
2618 ext_sct_add_serverhello,
2619 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002620 {
2621 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002622 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002623 ext_alpn_add_clienthello,
2624 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002625 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2626 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002627 ext_alpn_add_serverhello,
2628 },
Adam Langley49c7af12015-07-10 14:33:46 -07002629 {
2630 TLSEXT_TYPE_channel_id,
2631 ext_channel_id_init,
2632 ext_channel_id_add_clienthello,
2633 ext_channel_id_parse_serverhello,
2634 ext_channel_id_parse_clienthello,
2635 ext_channel_id_add_serverhello,
2636 },
Adam Langley391250d2015-07-15 19:06:07 -07002637 {
2638 TLSEXT_TYPE_srtp,
2639 ext_srtp_init,
2640 ext_srtp_add_clienthello,
2641 ext_srtp_parse_serverhello,
2642 ext_srtp_parse_clienthello,
2643 ext_srtp_add_serverhello,
2644 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002645 {
2646 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002647 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002648 ext_ec_point_add_clienthello,
2649 ext_ec_point_parse_serverhello,
2650 ext_ec_point_parse_clienthello,
2651 ext_ec_point_add_serverhello,
2652 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002653 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002654 TLSEXT_TYPE_key_share,
2655 NULL,
2656 ext_key_share_add_clienthello,
2657 forbid_parse_serverhello,
2658 ignore_parse_clienthello,
2659 dont_add_serverhello,
2660 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002661 {
Steven Valdeza833c352016-11-01 13:39:36 -04002662 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002663 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002664 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002665 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002666 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002667 dont_add_serverhello,
2668 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002669 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002670 TLSEXT_TYPE_early_data,
2671 NULL,
2672 ext_early_data_add_clienthello,
2673 forbid_parse_serverhello,
2674 ext_early_data_parse_clienthello,
2675 dont_add_serverhello,
2676 },
2677 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002678 TLSEXT_TYPE_supported_versions,
2679 NULL,
2680 ext_supported_versions_add_clienthello,
2681 forbid_parse_serverhello,
2682 ignore_parse_clienthello,
2683 dont_add_serverhello,
2684 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002685 {
2686 TLSEXT_TYPE_cookie,
2687 NULL,
2688 ext_cookie_add_clienthello,
2689 forbid_parse_serverhello,
2690 ignore_parse_clienthello,
2691 dont_add_serverhello,
2692 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002693 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2694 * intolerant to the last extension being zero-length. See
2695 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002696 {
Steven Valdezce902a92016-05-17 11:47:53 -04002697 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002698 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002699 ext_supported_groups_add_clienthello,
2700 ext_supported_groups_parse_serverhello,
2701 ext_supported_groups_parse_clienthello,
2702 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002703 },
Adam Langley614c66a2015-06-12 15:26:58 -07002704};
2705
2706#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2707
Adam Langley4cfa96b2015-07-01 11:56:55 -07002708OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002709 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002710 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002711OPENSSL_COMPILE_ASSERT(
2712 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2713 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002714
Adam Langley614c66a2015-06-12 15:26:58 -07002715static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2716 uint16_t value) {
2717 unsigned i;
2718 for (i = 0; i < kNumExtensions; i++) {
2719 if (kExtensions[i].value == value) {
2720 *out_index = i;
2721 return &kExtensions[i];
2722 }
2723 }
2724
2725 return NULL;
2726}
2727
Adam Langley09505632015-07-30 18:10:13 -07002728int SSL_extension_supported(unsigned extension_value) {
2729 uint32_t index;
2730 return extension_value == TLSEXT_TYPE_padding ||
2731 tls_extension_find(&index, extension_value) != NULL;
2732}
2733
David Benjamin8c880a22016-12-03 02:20:34 -05002734int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2735 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002736 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002737 if (ssl->client_version == SSL3_VERSION &&
2738 !ssl->s3->send_connection_binding) {
2739 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002740 }
Adam Langley95c29f32014-06-20 12:00:00 -07002741
David Benjamine8d53502015-10-10 14:13:23 -04002742 CBB extensions;
2743 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002744 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002745 }
Adam Langley95c29f32014-06-20 12:00:00 -07002746
David Benjamin8c880a22016-12-03 02:20:34 -05002747 hs->extensions.sent = 0;
2748 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002749
David Benjamin54091232016-09-05 12:47:25 -04002750 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002751 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002752 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002753 }
2754 }
Adam Langley95c29f32014-06-20 12:00:00 -07002755
David Benjamin65ac9972016-09-02 21:35:25 -04002756 uint16_t grease_ext1 = 0;
2757 if (ssl->ctx->grease_enabled) {
2758 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2759 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2760 if (!CBB_add_u16(&extensions, grease_ext1) ||
2761 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2762 goto err;
2763 }
2764 }
2765
David Benjamin54091232016-09-05 12:47:25 -04002766 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002767 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002768 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002769 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002770 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002771 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002772 }
Adam Langley95c29f32014-06-20 12:00:00 -07002773
Adam Langley33ad2b52015-07-20 17:43:53 -07002774 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002775 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002776 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002777 }
Adam Langley75712922014-10-10 16:23:43 -07002778
David Benjamin2bd19172016-11-17 16:47:15 +09002779 if (!custom_ext_add_clienthello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002780 goto err;
2781 }
2782
David Benjamin65ac9972016-09-02 21:35:25 -04002783 if (ssl->ctx->grease_enabled) {
2784 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2785 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2786
2787 /* The two fake extensions must not have the same value. GREASE values are
2788 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2789 * one. */
2790 if (grease_ext1 == grease_ext2) {
2791 grease_ext2 ^= 0x1010;
2792 }
2793
2794 if (!CBB_add_u16(&extensions, grease_ext2) ||
2795 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2796 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2797 goto err;
2798 }
2799 }
2800
David Benjamince079fd2016-08-02 16:22:34 -04002801 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002802 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002803 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002804 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002805 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002806 *
2807 * NB: because this code works out the length of all existing extensions
2808 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002809 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002810 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002811 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002812 * Server 7.0 is intolerant to the last extension being zero-length. See
2813 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002814 if (padding_len >= 4 + 1) {
2815 padding_len -= 4;
2816 } else {
2817 padding_len = 1;
2818 }
Adam Langley95c29f32014-06-20 12:00:00 -07002819
Adam Langley33ad2b52015-07-20 17:43:53 -07002820 uint8_t *padding_bytes;
2821 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2822 !CBB_add_u16(&extensions, padding_len) ||
2823 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2824 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002825 }
Adam Langley75712922014-10-10 16:23:43 -07002826
Adam Langley33ad2b52015-07-20 17:43:53 -07002827 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002828 }
2829 }
Adam Langley75712922014-10-10 16:23:43 -07002830
Steven Valdeza833c352016-11-01 13:39:36 -04002831 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002832 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002833 goto err;
2834 }
2835
David Benjamina01deee2015-12-08 18:56:31 -05002836 /* Discard empty extensions blocks. */
2837 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002838 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002839 }
2840
David Benjamine8d53502015-10-10 14:13:23 -04002841 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002842
2843err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002844 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002845 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002846}
Adam Langley95c29f32014-06-20 12:00:00 -07002847
David Benjamin8c880a22016-12-03 02:20:34 -05002848int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2849 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002850 CBB extensions;
2851 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002852 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002853 }
2854
David Benjamin8c880a22016-12-03 02:20:34 -05002855 for (unsigned i = 0; i < kNumExtensions; i++) {
2856 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002857 /* Don't send extensions that were not received. */
2858 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002859 }
Adam Langley95c29f32014-06-20 12:00:00 -07002860
David Benjamin8c880a22016-12-03 02:20:34 -05002861 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002862 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002863 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002864 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002865 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002866 }
Adam Langley95c29f32014-06-20 12:00:00 -07002867
David Benjamin2bd19172016-11-17 16:47:15 +09002868 if (!custom_ext_add_serverhello(hs, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002869 goto err;
2870 }
2871
Steven Valdez143e8b32016-07-11 13:19:03 -04002872 /* Discard empty extensions blocks before TLS 1.3. */
2873 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2874 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002875 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002876 }
2877
David Benjamin56380462015-10-10 14:59:09 -04002878 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002879
2880err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002881 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002882 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002883}
Adam Langley95c29f32014-06-20 12:00:00 -07002884
David Benjamin731058e2016-12-03 23:15:13 -05002885static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
2886 const SSL_CLIENT_HELLO *client_hello,
2887 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002888 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002889 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002890 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002891 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002892 }
2893 }
2894
David Benjamin8c880a22016-12-03 02:20:34 -05002895 hs->extensions.received = 0;
2896 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002897
David Benjamine14ff062016-08-09 16:21:24 -04002898 CBS extensions;
2899 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2900 while (CBS_len(&extensions) != 0) {
2901 uint16_t type;
2902 CBS extension;
2903
2904 /* Decode the next extension. */
2905 if (!CBS_get_u16(&extensions, &type) ||
2906 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002907 *out_alert = SSL_AD_DECODE_ERROR;
2908 return 0;
2909 }
Adam Langley95c29f32014-06-20 12:00:00 -07002910
David Benjamine14ff062016-08-09 16:21:24 -04002911 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2912 * ambiguous. Ignore all but the renegotiation_info extension. */
2913 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2914 continue;
2915 }
Adam Langley95c29f32014-06-20 12:00:00 -07002916
David Benjamine14ff062016-08-09 16:21:24 -04002917 unsigned ext_index;
2918 const struct tls_extension *const ext =
2919 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002920
David Benjamine14ff062016-08-09 16:21:24 -04002921 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09002922 if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002923 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002924 return 0;
2925 }
David Benjamine14ff062016-08-09 16:21:24 -04002926 continue;
2927 }
2928
David Benjamin8c880a22016-12-03 02:20:34 -05002929 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002930 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002931 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002932 *out_alert = alert;
2933 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002934 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002935 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002936 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002937 }
Adam Langley75712922014-10-10 16:23:43 -07002938
David Benjamin1deb41b2016-08-09 19:36:38 -04002939 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002940 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002941 continue;
2942 }
2943
2944 CBS *contents = NULL, fake_contents;
2945 static const uint8_t kFakeRenegotiateExtension[] = {0};
2946 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2947 ssl_client_cipher_list_contains_cipher(client_hello,
2948 SSL3_CK_SCSV & 0xffff)) {
2949 /* The renegotiation SCSV was received so pretend that we received a
2950 * renegotiation extension. */
2951 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2952 sizeof(kFakeRenegotiateExtension));
2953 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002954 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002955 }
2956
2957 /* Extension wasn't observed so call the callback with a NULL
2958 * parameter. */
2959 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002960 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002961 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002962 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002963 *out_alert = alert;
2964 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002965 }
2966 }
2967
Adam Langleyfcf25832014-12-18 17:42:32 -08002968 return 1;
2969}
Adam Langley95c29f32014-06-20 12:00:00 -07002970
David Benjamin731058e2016-12-03 23:15:13 -05002971int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
2972 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin8c880a22016-12-03 02:20:34 -05002973 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002974 int alert = -1;
David Benjamin8c880a22016-12-03 02:20:34 -05002975 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002976 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002977 return 0;
2978 }
Adam Langley95c29f32014-06-20 12:00:00 -07002979
David Benjamin8c880a22016-12-03 02:20:34 -05002980 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002981 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002982 return 0;
2983 }
Adam Langley95c29f32014-06-20 12:00:00 -07002984
Adam Langleyfcf25832014-12-18 17:42:32 -08002985 return 1;
2986}
Adam Langley95c29f32014-06-20 12:00:00 -07002987
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002988OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2989
David Benjamin8c880a22016-12-03 02:20:34 -05002990static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2991 int *out_alert) {
2992 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002993 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2994 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2995 return 1;
2996 }
Adam Langley614c66a2015-06-12 15:26:58 -07002997
Steven Valdez143e8b32016-07-11 13:19:03 -04002998 /* Decode the extensions block and check it is valid. */
2999 CBS extensions;
3000 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
3001 !tls1_check_duplicate_extensions(&extensions)) {
3002 *out_alert = SSL_AD_DECODE_ERROR;
3003 return 0;
3004 }
3005
3006 uint32_t received = 0;
3007 while (CBS_len(&extensions) != 0) {
3008 uint16_t type;
3009 CBS extension;
3010
3011 /* Decode the next extension. */
3012 if (!CBS_get_u16(&extensions, &type) ||
3013 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003014 *out_alert = SSL_AD_DECODE_ERROR;
3015 return 0;
3016 }
Adam Langley95c29f32014-06-20 12:00:00 -07003017
Steven Valdez143e8b32016-07-11 13:19:03 -04003018 unsigned ext_index;
3019 const struct tls_extension *const ext =
3020 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07003021
Steven Valdez143e8b32016-07-11 13:19:03 -04003022 if (ext == NULL) {
David Benjamin2bd19172016-11-17 16:47:15 +09003023 if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003024 return 0;
3025 }
Steven Valdez143e8b32016-07-11 13:19:03 -04003026 continue;
3027 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003028
David Benjamin8c880a22016-12-03 02:20:34 -05003029 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04003030 type != TLSEXT_TYPE_renegotiate) {
3031 /* If the extension was never sent then it is illegal, except for the
3032 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04003033 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3034 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04003035 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04003036 return 0;
3037 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003038
Steven Valdez143e8b32016-07-11 13:19:03 -04003039 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07003040
Steven Valdez143e8b32016-07-11 13:19:03 -04003041 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003042 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04003043 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003044 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04003045 *out_alert = alert;
3046 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07003047 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003048 }
Adam Langley95c29f32014-06-20 12:00:00 -07003049
David Benjamin54091232016-09-05 12:47:25 -04003050 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003051 if (!(received & (1u << i))) {
3052 /* Extension wasn't observed so call the callback with a NULL
3053 * parameter. */
3054 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003055 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003056 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003057 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07003058 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08003059 return 0;
3060 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003061 }
3062 }
Adam Langley95c29f32014-06-20 12:00:00 -07003063
Adam Langleyfcf25832014-12-18 17:42:32 -08003064 return 1;
3065}
Adam Langley95c29f32014-06-20 12:00:00 -07003066
David Benjamin8c880a22016-12-03 02:20:34 -05003067static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3068 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003069 int ret = SSL_TLSEXT_ERR_NOACK;
3070 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003071
David Benjamin78f8aab2016-03-10 16:33:58 -05003072 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003073 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003074 ssl->ctx->tlsext_servername_arg);
3075 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003076 ret = ssl->initial_ctx->tlsext_servername_callback(
3077 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003078 }
Adam Langley95c29f32014-06-20 12:00:00 -07003079
Adam Langleyfcf25832014-12-18 17:42:32 -08003080 switch (ret) {
3081 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003082 ssl3_send_alert(ssl, SSL3_AL_FATAL, 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_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003086 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
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 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003090 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003091 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003092
Adam Langleyfcf25832014-12-18 17:42:32 -08003093 default:
3094 return 1;
3095 }
3096}
Adam Langleyed8270a2014-09-02 13:52:56 -07003097
David Benjamin8c880a22016-12-03 02:20:34 -05003098static int ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs) {
3099 SSL *const ssl = hs->ssl;
David Benjaminfc059942015-07-30 23:01:59 -04003100 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08003101 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07003102
David Benjamin78f8aab2016-03-10 16:33:58 -05003103 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003104 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003105 ssl->ctx->tlsext_servername_arg);
3106 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003107 ret = ssl->initial_ctx->tlsext_servername_callback(
3108 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003109 }
Adam Langley95c29f32014-06-20 12:00:00 -07003110
Adam Langleyfcf25832014-12-18 17:42:32 -08003111 switch (ret) {
3112 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003113 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003114 return -1;
David Benjamin03973092014-06-24 23:27:17 -04003115
Adam Langleyfcf25832014-12-18 17:42:32 -08003116 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003117 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003118 return 1;
3119
3120 default:
3121 return 1;
3122 }
3123}
3124
David Benjamin8c880a22016-12-03 02:20:34 -05003125int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3126 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003127 int alert = -1;
David Benjamin8c880a22016-12-03 02:20:34 -05003128 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003129 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003130 return 0;
3131 }
3132
David Benjamin8c880a22016-12-03 02:20:34 -05003133 if (ssl_check_serverhello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04003134 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08003135 return 0;
3136 }
3137
3138 return 1;
3139}
Adam Langley95c29f32014-06-20 12:00:00 -07003140
David Benjamine3aa1d92015-06-16 15:34:50 -04003141int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05003142 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04003143 size_t ticket_len, const uint8_t *session_id,
3144 size_t session_id_len) {
3145 int ret = 1; /* Most errors are non-fatal. */
3146 SSL_CTX *ssl_ctx = ssl->initial_ctx;
3147 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003148
David Benjamine3aa1d92015-06-16 15:34:50 -04003149 HMAC_CTX hmac_ctx;
3150 HMAC_CTX_init(&hmac_ctx);
3151 EVP_CIPHER_CTX cipher_ctx;
3152 EVP_CIPHER_CTX_init(&cipher_ctx);
3153
David Benjaminef1b0092015-11-21 14:05:44 -05003154 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04003155 *out_session = NULL;
3156
Steven Valdez4aa154e2016-07-29 14:32:55 -04003157 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
3158 goto done;
3159 }
3160
David Benjamine3aa1d92015-06-16 15:34:50 -04003161 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3162 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003163 }
3164
David Benjaminadcc3952015-04-26 13:07:57 -04003165 /* Ensure there is room for the key name and the largest IV
3166 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3167 * the maximum IV length should be well under the minimum size for the
3168 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003169 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3170 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003171 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003172 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003173
David Benjamine3aa1d92015-06-16 15:34:50 -04003174 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003175 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3176 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3177 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003178 if (cb_ret < 0) {
3179 ret = 0;
3180 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003181 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003182 if (cb_ret == 0) {
3183 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003184 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003185 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003186 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003187 }
3188 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003189 /* Check the key name matches. */
3190 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3191 SSL_TICKET_KEY_NAME_LEN) != 0) {
3192 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003193 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003194 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3195 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003196 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003197 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3198 ssl_ctx->tlsext_tick_aes_key, iv)) {
3199 ret = 0;
3200 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003201 }
3202 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003203 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003204
David Benjamine3aa1d92015-06-16 15:34:50 -04003205 /* Check the MAC at the end of the ticket. */
3206 uint8_t mac[EVP_MAX_MD_SIZE];
3207 size_t mac_len = HMAC_size(&hmac_ctx);
3208 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003209 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003210 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003211 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003212 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3213 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003214 int mac_ok =
3215 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3216#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3217 mac_ok = 1;
3218#endif
3219 if (!mac_ok) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003220 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003221 }
3222
David Benjamine3aa1d92015-06-16 15:34:50 -04003223 /* Decrypt the session data. */
3224 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3225 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3226 mac_len;
3227 plaintext = OPENSSL_malloc(ciphertext_len);
3228 if (plaintext == NULL) {
3229 ret = 0;
3230 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003231 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003232 size_t plaintext_len;
3233#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3234 memcpy(plaintext, ciphertext, ciphertext_len);
3235 plaintext_len = ciphertext_len;
3236#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003237 if (ciphertext_len >= INT_MAX) {
3238 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003239 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003240 int len1, len2;
3241 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3242 (int)ciphertext_len) ||
3243 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
3244 ERR_clear_error(); /* Don't leave an error on the queue. */
3245 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003246 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003247 plaintext_len = (size_t)(len1 + len2);
3248#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003249
David Benjamine3aa1d92015-06-16 15:34:50 -04003250 /* Decode the session. */
David Benjaminfbc45d72016-09-22 01:21:24 -04003251 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003252 if (session == NULL) {
3253 ERR_clear_error(); /* Don't leave an error on the queue. */
3254 goto done;
3255 }
3256
3257 /* Copy the client's session ID into the new session, to denote the ticket has
3258 * been accepted. */
3259 memcpy(session->session_id, session_id, session_id_len);
3260 session->session_id_length = session_id_len;
3261
3262 *out_session = session;
3263
3264done:
3265 OPENSSL_free(plaintext);
3266 HMAC_CTX_cleanup(&hmac_ctx);
3267 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3268 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003269}
Adam Langley95c29f32014-06-20 12:00:00 -07003270
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003271int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003272 /* Extension ignored for inappropriate versions */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003273 if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003274 return 1;
3275 }
David Benjamincd996942014-07-20 16:23:51 -04003276
David Benjamin0fc37ef2016-08-17 15:29:46 -04003277 OPENSSL_free(hs->peer_sigalgs);
3278 hs->peer_sigalgs = NULL;
3279 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003280
3281 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003282 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003283 return 0;
3284 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003285 num_sigalgs /= 2;
3286
3287 /* supported_signature_algorithms in the certificate request is
3288 * allowed to be empty. */
3289 if (num_sigalgs == 0) {
3290 return 1;
3291 }
3292
Steven Valdez02563852016-06-23 13:33:05 -04003293 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3294 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003295 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3296 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003297 return 0;
3298 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003299 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003300
3301 CBS sigalgs;
3302 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003303 for (size_t i = 0; i < num_sigalgs; i++) {
3304 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003305 return 0;
3306 }
3307 }
Adam Langley95c29f32014-06-20 12:00:00 -07003308
Adam Langleyfcf25832014-12-18 17:42:32 -08003309 return 1;
3310}
David Benjaminec2f27d2014-11-13 19:17:25 -05003311
David Benjaminf3c8f8d2016-11-17 17:20:47 +09003312int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
3313 SSL *const ssl = hs->ssl;
David Benjamind1d80782015-07-05 11:54:09 -04003314 CERT *cert = ssl->cert;
David Benjaminec2f27d2014-11-13 19:17:25 -05003315
Steven Valdezf0451ca2016-06-29 13:16:27 -04003316 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3317 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3318 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003319 int type = ssl_private_key_type(ssl);
3320 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003321 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003322 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003323 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003324 if (ssl_is_ecdsa_key_type(type)) {
3325 *out = SSL_SIGN_ECDSA_SHA1;
3326 return 1;
3327 }
3328 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3329 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003330 }
3331
David Benjamin3ef76972016-10-17 17:59:54 -04003332 const uint16_t *sigalgs = cert->sigalgs;
3333 size_t num_sigalgs = cert->num_sigalgs;
3334 if (sigalgs == NULL) {
3335 sigalgs = kSignSignatureAlgorithms;
3336 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003337 }
3338
David Benjamin0fc37ef2016-08-17 15:29:46 -04003339 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3340 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3341 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003342 /* If the client didn't specify any signature_algorithms extension then
3343 * we can assume that it supports SHA1. See
3344 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3345 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3346 SSL_SIGN_ECDSA_SHA1};
3347 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003348 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003349 }
3350
David Benjamin0fc37ef2016-08-17 15:29:46 -04003351 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003352 uint16_t sigalg = sigalgs[i];
3353 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3354 * negotiated. */
3355 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3356 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3357 continue;
3358 }
3359
David Benjamin0fc37ef2016-08-17 15:29:46 -04003360 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003361 if (sigalg == peer_sigalgs[j]) {
3362 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003363 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003364 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003365 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003366 }
Adam Langley95c29f32014-06-20 12:00:00 -07003367
David Benjaminea9a0d52016-07-08 15:52:59 -07003368 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3369 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003370}
Adam Langley95c29f32014-06-20 12:00:00 -07003371
Nick Harper60a85cb2016-09-23 16:25:11 -07003372int tls1_verify_channel_id(SSL *ssl) {
3373 int ret = 0;
3374 uint16_t extension_type;
3375 CBS extension, channel_id;
3376
3377 /* A Channel ID handshake message is structured to contain multiple
3378 * extensions, but the only one that can be present is Channel ID. */
3379 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3380 if (!CBS_get_u16(&channel_id, &extension_type) ||
3381 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3382 CBS_len(&channel_id) != 0 ||
3383 extension_type != TLSEXT_TYPE_channel_id ||
3384 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3385 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3386 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3387 return 0;
3388 }
3389
3390 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3391 if (!p256) {
3392 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3393 return 0;
3394 }
3395
3396 EC_KEY *key = NULL;
3397 EC_POINT *point = NULL;
3398 BIGNUM x, y;
3399 ECDSA_SIG sig;
3400 BN_init(&x);
3401 BN_init(&y);
3402 sig.r = BN_new();
3403 sig.s = BN_new();
3404 if (sig.r == NULL || sig.s == NULL) {
3405 goto err;
3406 }
3407
3408 const uint8_t *p = CBS_data(&extension);
3409 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3410 BN_bin2bn(p + 32, 32, &y) == NULL ||
3411 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3412 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3413 goto err;
3414 }
3415
3416 point = EC_POINT_new(p256);
3417 if (point == NULL ||
3418 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3419 goto err;
3420 }
3421
3422 key = EC_KEY_new();
3423 if (key == NULL ||
3424 !EC_KEY_set_group(key, p256) ||
3425 !EC_KEY_set_public_key(key, point)) {
3426 goto err;
3427 }
3428
3429 uint8_t digest[EVP_MAX_MD_SIZE];
3430 size_t digest_len;
3431 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3432 goto err;
3433 }
3434
3435 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3436#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3437 sig_ok = 1;
3438#endif
3439 if (!sig_ok) {
3440 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3441 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3442 ssl->s3->tlsext_channel_id_valid = 0;
3443 goto err;
3444 }
3445
3446 memcpy(ssl->s3->tlsext_channel_id, p, 64);
3447 ret = 1;
3448
3449err:
3450 BN_free(&x);
3451 BN_free(&y);
3452 BN_free(sig.r);
3453 BN_free(sig.s);
3454 EC_KEY_free(key);
3455 EC_POINT_free(point);
3456 EC_GROUP_free(p256);
3457 return ret;
3458}
3459
3460int tls1_write_channel_id(SSL *ssl, CBB *cbb) {
3461 uint8_t digest[EVP_MAX_MD_SIZE];
3462 size_t digest_len;
3463 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3464 return 0;
3465 }
3466
3467 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3468 if (ec_key == NULL) {
3469 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3470 return 0;
3471 }
3472
3473 int ret = 0;
3474 BIGNUM *x = BN_new();
3475 BIGNUM *y = BN_new();
3476 ECDSA_SIG *sig = NULL;
3477 if (x == NULL || y == NULL ||
3478 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3479 EC_KEY_get0_public_key(ec_key),
3480 x, y, NULL)) {
3481 goto err;
3482 }
3483
3484 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3485 if (sig == NULL) {
3486 goto err;
3487 }
3488
3489 CBB child;
3490 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3491 !CBB_add_u16_length_prefixed(cbb, &child) ||
3492 !BN_bn2cbb_padded(&child, 32, x) ||
3493 !BN_bn2cbb_padded(&child, 32, y) ||
3494 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3495 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3496 !CBB_flush(cbb)) {
3497 goto err;
3498 }
3499
3500 ret = 1;
3501
3502err:
3503 BN_free(x);
3504 BN_free(y);
3505 ECDSA_SIG_free(sig);
3506 return ret;
3507}
3508
David Benjamind6a4ae92015-08-06 11:10:51 -04003509int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003510 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3511 uint8_t *msg;
3512 size_t msg_len;
3513 if (!tls13_get_cert_verify_signature_input(ssl, &msg, &msg_len,
3514 ssl_cert_verify_channel_id)) {
3515 return 0;
3516 }
3517 SHA256(msg, msg_len, out);
3518 *out_len = SHA256_DIGEST_LENGTH;
3519 OPENSSL_free(msg);
3520 return 1;
3521 }
3522
Nick Harper95594012016-10-20 14:07:13 -07003523 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003524
Nick Harper95594012016-10-20 14:07:13 -07003525 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003526 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003527 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003528
Steven Valdez87eab492016-06-27 16:34:59 -04003529 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003530 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003531 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003532 if (ssl->session->original_handshake_hash_len == 0) {
3533 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003534 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003535 }
Nick Harper95594012016-10-20 14:07:13 -07003536 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3537 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003538 }
3539
3540 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3541 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3542 sizeof(handshake_hash));
3543 if (handshake_hash_len < 0) {
Nick Harper95594012016-10-20 14:07:13 -07003544 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003545 }
Nick Harper95594012016-10-20 14:07:13 -07003546 SHA256_Update(&ctx, handshake_hash, (size_t)handshake_hash_len);
3547 SHA256_Final(out, &ctx);
3548 *out_len = SHA256_DIGEST_LENGTH;
3549 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003550}
Adam Langley1258b6a2014-06-20 12:00:00 -07003551
3552/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003553 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003554 * data. */
3555int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003556 int digest_len;
3557 /* This function should never be called for a resumed session because the
3558 * handshake hashes that we wish to record are for the original, full
3559 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003560 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003561 return -1;
3562 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003563
Adam Langleyfcf25832014-12-18 17:42:32 -08003564 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003565 tls1_handshake_digest(
3566 ssl, ssl->s3->new_session->original_handshake_hash,
3567 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003568 if (digest_len < 0) {
3569 return -1;
3570 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003571
Steven Valdez87eab492016-06-27 16:34:59 -04003572 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003573
Adam Langleyfcf25832014-12-18 17:42:32 -08003574 return 1;
3575}
Nick Harper60a85cb2016-09-23 16:25:11 -07003576
3577int ssl_do_channel_id_callback(SSL *ssl) {
3578 if (ssl->tlsext_channel_id_private != NULL ||
3579 ssl->ctx->channel_id_cb == NULL) {
3580 return 1;
3581 }
3582
3583 EVP_PKEY *key = NULL;
3584 ssl->ctx->channel_id_cb(ssl, &key);
3585 if (key == NULL) {
3586 /* The caller should try again later. */
3587 return 1;
3588 }
3589
3590 int ret = SSL_set1_tls_channel_id(ssl, key);
3591 EVP_PKEY_free(key);
3592 return ret;
3593}
Adam Langleycfa08c32016-11-17 13:21:27 -08003594
3595int ssl_is_sct_list_valid(const CBS *contents) {
3596 /* Shallow parse the SCT list for sanity. By the RFC
3597 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3598 * of the SCTs may be empty. */
3599 CBS copy = *contents;
3600 CBS sct_list;
3601 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3602 CBS_len(&copy) != 0 ||
3603 CBS_len(&sct_list) == 0) {
3604 return 0;
3605 }
3606
3607 while (CBS_len(&sct_list) > 0) {
3608 CBS sct;
3609 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3610 CBS_len(&sct) == 0) {
3611 return 0;
3612 }
3613 }
3614
3615 return 1;
3616}