blob: 0b27165b6ab57cb428060cefa434914feb8c7c73 [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 Benjamind7573dc2016-07-20 19:05:22 +0200206int ssl_early_callback_init(SSL *ssl, struct ssl_early_callback_ctx *ctx,
207 const uint8_t *in, size_t in_len) {
208 memset(ctx, 0, sizeof(*ctx));
209 ctx->ssl = ssl;
210 ctx->client_hello = in;
211 ctx->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;
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
David Benjamine14ff062016-08-09 16:21:24 -0400215 if (!CBS_get_u16(&client_hello, &ctx->version) ||
216 !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 Benjamine14ff062016-08-09 16:21:24 -0400222 ctx->random = CBS_data(&random);
223 ctx->random_len = CBS_len(&random);
Adam Langleyfcf25832014-12-18 17:42:32 -0800224 ctx->session_id = CBS_data(&session_id);
225 ctx->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 Benjamince079fd2016-08-02 16:22:34 -0400228 if (SSL_is_dtls(ctx->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
244 ctx->cipher_suites = CBS_data(&cipher_suites);
245 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 ctx->compression_methods = CBS_data(&compression_methods);
247 ctx->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) {
252 ctx->extensions = NULL;
253 ctx->extensions_len = 0;
254 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
Adam Langleyfcf25832014-12-18 17:42:32 -0800265 ctx->extensions = CBS_data(&extensions);
266 ctx->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 Benjamincec73442016-08-02 17:41:33 -0400271int ssl_early_callback_get_extension(const struct ssl_early_callback_ctx *ctx,
272 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 CBS_init(&extensions, ctx->extensions, ctx->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 Benjamincec73442016-08-02 17:41:33 -0400293int SSL_early_callback_ctx_extension_get(
294 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
295 const uint8_t **out_data, size_t *out_len) {
296 CBS cbs;
297 if (!ssl_early_callback_get_extension(ctx, &cbs, extension_type)) {
298 return 0;
299 }
300
301 *out_data = CBS_data(&cbs);
302 *out_len = CBS_len(&cbs);
303 return 1;
304}
305
Steven Valdezce902a92016-05-17 11:47:53 -0400306static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400307 SSL_CURVE_X25519,
308 SSL_CURVE_SECP256R1,
309 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700310#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin9e68f192016-06-30 14:55:33 -0400311 SSL_CURVE_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700312#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800313};
Adam Langley95c29f32014-06-20 12:00:00 -0700314
David Benjaminf04976b2016-10-07 00:37:55 -0400315void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
Steven Valdez5440fe02016-07-18 12:40:30 -0400316 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400317 *out_group_ids = ssl->supported_group_list;
318 *out_group_ids_len = ssl->supported_group_list_len;
319 if (!*out_group_ids) {
320 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400321 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 }
323}
David Benjamined439582014-07-14 19:13:02 -0400324
Steven Valdezce902a92016-05-17 11:47:53 -0400325int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) {
David Benjaminf04976b2016-10-07 00:37:55 -0400326 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400327
David Benjaminf04976b2016-10-07 00:37:55 -0400328 const uint16_t *groups, *pref, *supp;
329 size_t groups_len, pref_len, supp_len;
330 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800331
David Benjaminf04976b2016-10-07 00:37:55 -0400332 /* Clients are not required to send a supported_groups extension. In this
333 * case, the server is free to pick any group it likes. See RFC 4492,
334 * section 4, paragraph 3.
335 *
336 * However, in the interests of compatibility, we will skip ECDH if the
337 * client didn't send an extension because we can't be sure that they'll
338 * support our favoured group. Thus we do not special-case an emtpy
339 * |peer_supported_group_list|. */
David Benjamin55a43642015-04-20 14:45:55 -0400340
David Benjamin4298d772015-12-19 00:18:25 -0500341 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400342 pref = groups;
343 pref_len = groups_len;
David Benjamin43612b62016-10-07 00:41:50 -0400344 supp = ssl->s3->hs->peer_supported_group_list;
345 supp_len = ssl->s3->hs->peer_supported_group_list_len;
David Benjamin55a43642015-04-20 14:45:55 -0400346 } else {
David Benjamin43612b62016-10-07 00:41:50 -0400347 pref = ssl->s3->hs->peer_supported_group_list;
348 pref_len = ssl->s3->hs->peer_supported_group_list_len;
Steven Valdezce902a92016-05-17 11:47:53 -0400349 supp = groups;
350 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400351 }
352
David Benjaminf04976b2016-10-07 00:37:55 -0400353 for (size_t i = 0; i < pref_len; i++) {
354 for (size_t j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800355 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400356 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500357 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800358 }
359 }
360 }
361
David Benjamin4298d772015-12-19 00:18:25 -0500362 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800363}
Adam Langley95c29f32014-06-20 12:00:00 -0700364
Steven Valdezce902a92016-05-17 11:47:53 -0400365int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400367 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800368
Steven Valdezce902a92016-05-17 11:47:53 -0400369 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
370 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 return 0;
372 }
373
David Benjamin54091232016-09-05 12:47:25 -0400374 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400375 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
376 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 return 0;
378 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 }
380
Steven Valdezce902a92016-05-17 11:47:53 -0400381 OPENSSL_free(*out_group_ids);
382 *out_group_ids = group_ids;
383 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800384
385 return 1;
386}
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100388int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
389 const char *curves) {
390 uint16_t *group_ids = NULL;
391 size_t ncurves = 0;
392
393 const char *col;
394 const char *ptr = curves;
395
396 do {
397 col = strchr(ptr, ':');
398
399 uint16_t group_id;
400 if (!ssl_name_to_group_id(&group_id, ptr,
401 col ? (size_t)(col - ptr) : strlen(ptr))) {
402 goto err;
403 }
404
405 uint16_t *new_group_ids = OPENSSL_realloc(group_ids,
406 (ncurves + 1) * sizeof(uint16_t));
407 if (new_group_ids == NULL) {
408 goto err;
409 }
410 group_ids = new_group_ids;
411
412 group_ids[ncurves] = group_id;
413 ncurves++;
414
415 if (col) {
416 ptr = col + 1;
417 }
418 } while (col);
419
420 OPENSSL_free(*out_group_ids);
421 *out_group_ids = group_ids;
422 *out_group_ids_len = ncurves;
423
424 return 1;
425
426err:
427 OPENSSL_free(group_ids);
428 return 0;
429}
430
Steven Valdezce902a92016-05-17 11:47:53 -0400431int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
432 const uint16_t *groups;
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400433 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -0400434 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400435 for (size_t i = 0; i < groups_len; i++) {
436 if (groups[i] == group_id) {
437 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800438 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800439 }
David Benjamin033e5f42014-11-13 18:47:41 -0500440
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400441 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800442}
David Benjamin033e5f42014-11-13 18:47:41 -0500443
David Benjamin3ef76972016-10-17 17:59:54 -0400444/* kVerifySignatureAlgorithms is the default list of accepted signature
David Benjamin3a322f52016-10-26 12:45:35 -0400445 * algorithms for verifying.
446 *
447 * For now, RSA-PSS signature algorithms are not enabled on Android's system
448 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
449 * restore them. */
David Benjamin3ef76972016-10-17 17:59:54 -0400450static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjamin3a322f52016-10-26 12:45:35 -0400451 /* Prefer SHA-256 algorithms. */
452 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400453#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400454 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400455#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400456 SSL_SIGN_RSA_PKCS1_SHA256,
457
458 /* Larger hashes are acceptable. */
459 SSL_SIGN_ECDSA_SECP384R1_SHA384,
460#if !defined(BORINGSSL_ANDROID_SYSTEM)
461 SSL_SIGN_RSA_PSS_SHA384,
462#endif
463 SSL_SIGN_RSA_PKCS1_SHA384,
464
David Benjamin3ef76972016-10-17 17:59:54 -0400465 /* TODO(davidben): Remove this entry and SSL_CURVE_SECP521R1 from
466 * kDefaultGroups. */
467#if defined(BORINGSSL_ANDROID_SYSTEM)
468 SSL_SIGN_ECDSA_SECP521R1_SHA512,
469#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400470#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400471 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400472#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400473 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400474
David Benjamin3a322f52016-10-26 12:45:35 -0400475 /* For now, SHA-1 is still accepted but least preferable. */
476 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400477
David Benjamin3a322f52016-10-26 12:45:35 -0400478};
479
480/* kSignSignatureAlgorithms is the default list of supported signature
481 * algorithms for signing.
482 *
483 * For now, RSA-PSS signature algorithms are not enabled on Android's system
484 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
485 * restore them. */
486static const uint16_t kSignSignatureAlgorithms[] = {
487 /* Prefer SHA-256 algorithms. */
488 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400489#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400490 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400491#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400492 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400493
David Benjamin3a322f52016-10-26 12:45:35 -0400494 /* If needed, sign larger hashes.
495 *
496 * TODO(davidben): Determine which of these may be pruned. */
497 SSL_SIGN_ECDSA_SECP384R1_SHA384,
498#if !defined(BORINGSSL_ANDROID_SYSTEM)
499 SSL_SIGN_RSA_PSS_SHA384,
500#endif
501 SSL_SIGN_RSA_PKCS1_SHA384,
502
503 SSL_SIGN_ECDSA_SECP521R1_SHA512,
504#if !defined(BORINGSSL_ANDROID_SYSTEM)
505 SSL_SIGN_RSA_PSS_SHA512,
506#endif
507 SSL_SIGN_RSA_PKCS1_SHA512,
508
509 /* If the peer supports nothing else, sign with SHA-1. */
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400510 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400511 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400512};
513
David Benjamin3ef76972016-10-17 17:59:54 -0400514size_t tls12_get_verify_sigalgs(const SSL *ssl, const uint16_t **out) {
515 *out = kVerifySignatureAlgorithms;
516 return OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
Adam Langleyfcf25832014-12-18 17:42:32 -0800517}
Adam Langley95c29f32014-06-20 12:00:00 -0700518
David Benjamin887c3002016-07-08 16:15:32 -0700519int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
David Benjamin3ef76972016-10-17 17:59:54 -0400520 const uint16_t *verify_sigalgs;
521 size_t num_verify_sigalgs = tls12_get_verify_sigalgs(ssl, &verify_sigalgs);
522 for (size_t i = 0; i < num_verify_sigalgs; i++) {
523 if (sigalg == verify_sigalgs[i]) {
524 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 }
526 }
527
David Benjamin3ef76972016-10-17 17:59:54 -0400528 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
529 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
530 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800531}
532
533/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
534 * supported or doesn't appear in supported signature algorithms. Unlike
535 * ssl_cipher_get_disabled this applies to a specific session and not global
536 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500537void ssl_set_client_disabled(SSL *ssl) {
538 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 int have_rsa = 0, have_ecdsa = 0;
540 c->mask_a = 0;
541 c->mask_k = 0;
542
David Benjamin3ef76972016-10-17 17:59:54 -0400543 /* Now go through all signature algorithms seeing if we support any for RSA or
544 * ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin0fc37ef2016-08-17 15:29:46 -0400545 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -0400546 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
David Benjamin0fc37ef2016-08-17 15:29:46 -0400547 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -0400548 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400549 case SSL_SIGN_RSA_PSS_SHA512:
550 case SSL_SIGN_RSA_PSS_SHA384:
551 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400552 case SSL_SIGN_RSA_PKCS1_SHA512:
553 case SSL_SIGN_RSA_PKCS1_SHA384:
554 case SSL_SIGN_RSA_PKCS1_SHA256:
555 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800556 have_rsa = 1;
557 break;
558
Steven Valdez02563852016-06-23 13:33:05 -0400559 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
560 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
561 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
562 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800563 have_ecdsa = 1;
564 break;
565 }
566 }
567
568 /* Disable auth if we don't include any appropriate signature algorithms. */
569 if (!have_rsa) {
570 c->mask_a |= SSL_aRSA;
571 }
572 if (!have_ecdsa) {
573 c->mask_a |= SSL_aECDSA;
574 }
575
576 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500577 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800578 c->mask_a |= SSL_aPSK;
579 c->mask_k |= SSL_kPSK;
580 }
581}
Adam Langley95c29f32014-06-20 12:00:00 -0700582
Adam Langley614c66a2015-06-12 15:26:58 -0700583/* tls_extension represents a TLS extension that is handled internally. The
584 * |init| function is called for each handshake, before any other functions of
585 * the extension. Then the add and parse callbacks are called as needed.
586 *
587 * The parse callbacks receive a |CBS| that contains the contents of the
588 * extension (i.e. not including the type and length bytes). If an extension is
589 * not received then the parse callbacks will be called with a NULL CBS so that
590 * they can do any processing needed to handle the absence of an extension.
591 *
592 * The add callbacks receive a |CBB| to which the extension can be appended but
593 * the function is responsible for appending the type and length bytes too.
594 *
595 * All callbacks return one for success and zero for error. If a parse function
596 * returns zero then a fatal alert with value |*out_alert| will be sent. If
597 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
598struct tls_extension {
599 uint16_t value;
David Benjamin8c880a22016-12-03 02:20:34 -0500600 void (*init)(SSL_HANDSHAKE *hs);
Adam Langley614c66a2015-06-12 15:26:58 -0700601
David Benjamin8c880a22016-12-03 02:20:34 -0500602 int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
603 int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
604 CBS *contents);
Adam Langley614c66a2015-06-12 15:26:58 -0700605
David Benjamin8c880a22016-12-03 02:20:34 -0500606 int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
607 CBS *contents);
608 int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
Adam Langley614c66a2015-06-12 15:26:58 -0700609};
610
David Benjamin8c880a22016-12-03 02:20:34 -0500611static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
612 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400613 if (contents != NULL) {
614 /* Servers MUST NOT send this extension. */
615 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
616 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
617 return 0;
618 }
619
620 return 1;
621}
622
David Benjamin8c880a22016-12-03 02:20:34 -0500623static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
624 CBS *contents) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400625 /* This extension from the client is handled elsewhere. */
626 return 1;
627}
628
David Benjamin8c880a22016-12-03 02:20:34 -0500629static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdez6b8509a2016-07-12 13:38:32 -0400630 return 1;
631}
Adam Langley614c66a2015-06-12 15:26:58 -0700632
633/* Server name indication (SNI).
634 *
635 * https://tools.ietf.org/html/rfc6066#section-3. */
636
David Benjamin8c880a22016-12-03 02:20:34 -0500637static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
638 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700639 if (ssl->tlsext_hostname == NULL) {
640 return 1;
641 }
642
643 CBB contents, server_name_list, name;
644 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
645 !CBB_add_u16_length_prefixed(out, &contents) ||
646 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
647 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
648 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
649 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
650 strlen(ssl->tlsext_hostname)) ||
651 !CBB_flush(out)) {
652 return 0;
653 }
654
655 return 1;
656}
657
David Benjamin8c880a22016-12-03 02:20:34 -0500658static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500659 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500660 SSL *const ssl = hs->ssl;
Adam Langley614c66a2015-06-12 15:26:58 -0700661 if (contents == NULL) {
662 return 1;
663 }
664
665 if (CBS_len(contents) != 0) {
666 return 0;
667 }
668
669 assert(ssl->tlsext_hostname != NULL);
670
Steven Valdez87eab492016-06-27 16:34:59 -0400671 if (ssl->session == NULL) {
672 assert(ssl->s3->new_session->tlsext_hostname == NULL);
673 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
674 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700675 *out_alert = SSL_AD_INTERNAL_ERROR;
676 return 0;
677 }
678 }
679
680 return 1;
681}
682
David Benjamin8c880a22016-12-03 02:20:34 -0500683static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500684 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700685 if (contents == NULL) {
686 return 1;
687 }
688
David Benjamin9b611e22016-03-03 08:48:30 -0500689 CBS server_name_list, host_name;
690 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700691 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500692 !CBS_get_u8(&server_name_list, &name_type) ||
693 /* Although the server_name extension was intended to be extensible to
694 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
695 * different name types will cause an error. Further, RFC 4366 originally
696 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
697 * adding new name types is no longer feasible.
698 *
699 * Act as if the extensibility does not exist to simplify parsing. */
700 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
701 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700702 CBS_len(contents) != 0) {
703 return 0;
704 }
705
David Benjamin9b611e22016-03-03 08:48:30 -0500706 if (name_type != TLSEXT_NAMETYPE_host_name ||
707 CBS_len(&host_name) == 0 ||
708 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
709 CBS_contains_zero_byte(&host_name)) {
710 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
711 return 0;
712 }
Adam Langley614c66a2015-06-12 15:26:58 -0700713
David Benjamin4eb95cc2016-11-16 17:08:23 +0900714 /* Copy the hostname as a string. */
David Benjamin8c880a22016-12-03 02:20:34 -0500715 if (!CBS_strdup(&host_name, &hs->hostname)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900716 *out_alert = SSL_AD_INTERNAL_ERROR;
717 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700718 }
719
David Benjamin8c880a22016-12-03 02:20:34 -0500720 hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700721 return 1;
722}
723
David Benjamin8c880a22016-12-03 02:20:34 -0500724static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
725 if (hs->ssl->s3->session_reused ||
726 !hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700727 return 1;
728 }
729
730 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
731 !CBB_add_u16(out, 0 /* length */)) {
732 return 0;
733 }
734
735 return 1;
736}
737
738
Adam Langley5021b222015-06-12 18:27:58 -0700739/* Renegotiation indication.
740 *
741 * https://tools.ietf.org/html/rfc5746 */
742
David Benjamin8c880a22016-12-03 02:20:34 -0500743static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
744 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400745 uint16_t min_version, max_version;
746 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
747 return 0;
748 }
749
750 /* Renegotiation indication is not necessary in TLS 1.3. */
751 if (min_version >= TLS1_3_VERSION) {
752 return 1;
753 }
754
David Benjamin52bf6902016-10-08 12:05:03 -0400755 assert(ssl->s3->initial_handshake_complete ==
756 (ssl->s3->previous_client_finished_len != 0));
757
Adam Langley5021b222015-06-12 18:27:58 -0700758 CBB contents, prev_finished;
759 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
760 !CBB_add_u16_length_prefixed(out, &contents) ||
761 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
762 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
763 ssl->s3->previous_client_finished_len) ||
764 !CBB_flush(out)) {
765 return 0;
766 }
767
768 return 1;
769}
770
David Benjamin8c880a22016-12-03 02:20:34 -0500771static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700772 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500773 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -0400774 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
775 return 0;
776 }
777
David Benjamin3e052de2015-11-25 20:10:31 -0500778 /* Servers may not switch between omitting the extension and supporting it.
779 * See RFC 5746, sections 3.5 and 4.2. */
780 if (ssl->s3->initial_handshake_complete &&
781 (contents != NULL) != ssl->s3->send_connection_binding) {
782 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
783 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
784 return 0;
785 }
786
Adam Langley5021b222015-06-12 18:27:58 -0700787 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500788 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700789 * RI even on initial ServerHello because the client doesn't see any
790 * renegotiation during an attack. However this would mean we could not
791 * connect to any server which doesn't support RI.
792 *
David Benjamine9cddb82015-11-23 14:36:40 -0500793 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
794 * practical terms every client sets it so it's just assumed here. */
795 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700796 }
797
798 const size_t expected_len = ssl->s3->previous_client_finished_len +
799 ssl->s3->previous_server_finished_len;
800
801 /* Check for logic errors */
802 assert(!expected_len || ssl->s3->previous_client_finished_len);
803 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400804 assert(ssl->s3->initial_handshake_complete ==
805 (ssl->s3->previous_client_finished_len != 0));
806 assert(ssl->s3->initial_handshake_complete ==
807 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700808
809 /* Parse out the extension contents. */
810 CBS renegotiated_connection;
811 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
812 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400813 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700814 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
815 return 0;
816 }
817
818 /* Check that the extension matches. */
819 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400820 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700821 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
822 return 0;
823 }
824
825 const uint8_t *d = CBS_data(&renegotiated_connection);
826 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
827 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400828 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700829 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
830 return 0;
831 }
832 d += ssl->s3->previous_client_finished_len;
833
834 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
835 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400836 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700837 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
838 return 0;
839 }
840 ssl->s3->send_connection_binding = 1;
841
842 return 1;
843}
844
David Benjamin8c880a22016-12-03 02:20:34 -0500845static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley5021b222015-06-12 18:27:58 -0700846 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500847 SSL *const ssl = hs->ssl;
Adam Langley5021b222015-06-12 18:27:58 -0700848 /* Renegotiation isn't supported as a server so this function should never be
849 * called after the initial handshake. */
850 assert(!ssl->s3->initial_handshake_complete);
851
Steven Valdez143e8b32016-07-11 13:19:03 -0400852 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
853 return 1;
854 }
855
Adam Langley5021b222015-06-12 18:27:58 -0700856 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400857 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700858 }
859
860 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700861 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
862 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400863 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700864 return 0;
865 }
866
David Benjamin52bf6902016-10-08 12:05:03 -0400867 /* Check that the extension matches. We do not support renegotiation as a
868 * server, so this must be empty. */
869 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400870 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700871 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
872 return 0;
873 }
874
875 ssl->s3->send_connection_binding = 1;
876
877 return 1;
878}
879
David Benjamin8c880a22016-12-03 02:20:34 -0500880static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
881 SSL *const ssl = hs->ssl;
David Benjamin52bf6902016-10-08 12:05:03 -0400882 /* Renegotiation isn't supported as a server so this function should never be
883 * called after the initial handshake. */
884 assert(!ssl->s3->initial_handshake_complete);
885
Steven Valdez143e8b32016-07-11 13:19:03 -0400886 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
887 return 1;
888 }
889
Adam Langley5021b222015-06-12 18:27:58 -0700890 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400891 !CBB_add_u16(out, 1 /* length */) ||
892 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700893 return 0;
894 }
895
896 return 1;
897}
898
Adam Langley0a056712015-07-01 15:03:33 -0700899
900/* Extended Master Secret.
901 *
David Benjamin43946d42016-02-01 08:42:19 -0500902 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700903
David Benjamin8c880a22016-12-03 02:20:34 -0500904static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400905 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -0500906 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400907 return 0;
908 }
909
910 /* Extended master secret is not necessary in TLS 1.3. */
911 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700912 return 1;
913 }
914
915 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
916 !CBB_add_u16(out, 0 /* length */)) {
917 return 0;
918 }
919
920 return 1;
921}
922
David Benjamin8c880a22016-12-03 02:20:34 -0500923static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley0a056712015-07-01 15:03:33 -0700924 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500925 SSL *const ssl = hs->ssl;
David Benjamin163c9562016-08-29 23:14:17 -0400926 /* Whether EMS is negotiated may not change on renegotation. */
927 if (ssl->s3->initial_handshake_complete) {
928 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
929 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
930 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
931 return 0;
932 }
933
934 return 1;
935 }
936
Adam Langley0a056712015-07-01 15:03:33 -0700937 if (contents == NULL) {
938 return 1;
939 }
940
Steven Valdez143e8b32016-07-11 13:19:03 -0400941 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
942 ssl->version == SSL3_VERSION) {
943 return 0;
944 }
945
946 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700947 return 0;
948 }
949
950 ssl->s3->tmp.extended_master_secret = 1;
951 return 1;
952}
953
David Benjamin8c880a22016-12-03 02:20:34 -0500954static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin0d56f882015-12-19 17:05:56 -0500955 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -0500956 SSL *const ssl = hs->ssl;
957 uint16_t version = ssl3_protocol_version(ssl);
958 if (version >= TLS1_3_VERSION ||
959 version == SSL3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400960 return 1;
961 }
962
963 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700964 return 1;
965 }
966
967 if (CBS_len(contents) != 0) {
968 return 0;
969 }
970
971 ssl->s3->tmp.extended_master_secret = 1;
972 return 1;
973}
974
David Benjamin8c880a22016-12-03 02:20:34 -0500975static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
976 if (!hs->ssl->s3->tmp.extended_master_secret) {
Adam Langley0a056712015-07-01 15:03:33 -0700977 return 1;
978 }
979
980 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
981 !CBB_add_u16(out, 0 /* length */)) {
982 return 0;
983 }
984
985 return 1;
986}
987
Adam Langley9b05bc52015-07-01 15:25:33 -0700988
989/* Session tickets.
990 *
991 * https://tools.ietf.org/html/rfc5077 */
992
David Benjamin8c880a22016-12-03 02:20:34 -0500993static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
994 SSL *const ssl = hs->ssl;
David Benjamin7c7d8312016-08-20 13:39:03 -0400995 uint16_t min_version, max_version;
996 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
997 return 0;
998 }
999
1000 /* TLS 1.3 uses a different ticket extension. */
1001 if (min_version >= TLS1_3_VERSION ||
1002 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001003 return 1;
1004 }
1005
1006 const uint8_t *ticket_data = NULL;
1007 int ticket_len = 0;
1008
1009 /* Renegotiation does not participate in session resumption. However, still
1010 * advertise the extension to avoid potentially breaking servers which carry
1011 * over the state from the previous handshake, such as OpenSSL servers
1012 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -04001013 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -07001014 if (!ssl->s3->initial_handshake_complete &&
1015 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001016 ssl->session->tlsext_tick != NULL &&
1017 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -04001018 ssl->method->version_from_wire(&session_version,
1019 ssl->session->ssl_version) &&
1020 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001021 ticket_data = ssl->session->tlsext_tick;
1022 ticket_len = ssl->session->tlsext_ticklen;
1023 }
1024
1025 CBB ticket;
1026 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1027 !CBB_add_u16_length_prefixed(out, &ticket) ||
1028 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1029 !CBB_flush(out)) {
1030 return 0;
1031 }
1032
1033 return 1;
1034}
1035
David Benjamin8c880a22016-12-03 02:20:34 -05001036static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley9b05bc52015-07-01 15:25:33 -07001037 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001038 SSL *const ssl = hs->ssl;
Adam Langley9b05bc52015-07-01 15:25:33 -07001039 if (contents == NULL) {
1040 return 1;
1041 }
1042
Steven Valdez143e8b32016-07-11 13:19:03 -04001043 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1044 return 0;
1045 }
1046
Adam Langley9b05bc52015-07-01 15:25:33 -07001047 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1048 * this function should never be called, even if the server tries to send the
1049 * extension. */
1050 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1051
1052 if (CBS_len(contents) != 0) {
1053 return 0;
1054 }
1055
David Benjamin8c880a22016-12-03 02:20:34 -05001056 hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001057 return 1;
1058}
1059
David Benjamin8c880a22016-12-03 02:20:34 -05001060static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1061 if (!hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001062 return 1;
1063 }
1064
David Benjamin78476f62016-11-12 11:20:55 +09001065 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
David Benjamin8c880a22016-12-03 02:20:34 -05001066 assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
Adam Langley9b05bc52015-07-01 15:25:33 -07001067
1068 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1069 !CBB_add_u16(out, 0 /* length */)) {
1070 return 0;
1071 }
1072
1073 return 1;
1074}
1075
1076
Adam Langley2e857bd2015-07-01 16:09:19 -07001077/* Signature Algorithms.
1078 *
1079 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1080
David Benjamin8c880a22016-12-03 02:20:34 -05001081static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1082 SSL *const ssl = hs->ssl;
David Benjamin2dc02042016-09-19 19:57:37 -04001083 uint16_t min_version, max_version;
1084 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1085 return 0;
1086 }
1087
1088 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001089 return 1;
1090 }
1091
David Benjamin0fc37ef2016-08-17 15:29:46 -04001092 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001093 const size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001094
David Benjamin0fc37ef2016-08-17 15:29:46 -04001095 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001096 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1097 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001098 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001099 return 0;
1100 }
1101
David Benjamin0fc37ef2016-08-17 15:29:46 -04001102 for (size_t i = 0; i < num_sigalgs; i++) {
1103 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001104 return 0;
1105 }
1106 }
1107
1108 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001109 return 0;
1110 }
1111
1112 return 1;
1113}
1114
David Benjamin8c880a22016-12-03 02:20:34 -05001115static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley2e857bd2015-07-01 16:09:19 -07001116 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001117 SSL *const ssl = hs->ssl;
1118 OPENSSL_free(hs->peer_sigalgs);
1119 hs->peer_sigalgs = NULL;
1120 hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001121
Adam Langley2e857bd2015-07-01 16:09:19 -07001122 if (contents == NULL) {
1123 return 1;
1124 }
1125
1126 CBS supported_signature_algorithms;
1127 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001128 CBS_len(contents) != 0 ||
1129 CBS_len(&supported_signature_algorithms) == 0 ||
1130 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001131 return 0;
1132 }
1133
1134 return 1;
1135}
1136
Adam Langley2e857bd2015-07-01 16:09:19 -07001137
Adam Langleybb0bd042015-07-01 16:21:03 -07001138/* OCSP Stapling.
1139 *
1140 * https://tools.ietf.org/html/rfc6066#section-8 */
1141
David Benjamin8c880a22016-12-03 02:20:34 -05001142static void ext_ocsp_init(SSL_HANDSHAKE *hs) {
1143 hs->ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001144}
1145
David Benjamin8c880a22016-12-03 02:20:34 -05001146static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1147 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001148 if (!ssl->ocsp_stapling_enabled) {
1149 return 1;
1150 }
1151
1152 CBB contents;
1153 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1154 !CBB_add_u16_length_prefixed(out, &contents) ||
1155 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1156 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1157 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1158 !CBB_flush(out)) {
1159 return 0;
1160 }
1161
Adam Langleyce9d85e2016-01-24 15:58:39 -08001162 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001163 return 1;
1164}
1165
David Benjamin8c880a22016-12-03 02:20:34 -05001166static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001167 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001168 SSL *const ssl = hs->ssl;
Adam Langleybb0bd042015-07-01 16:21:03 -07001169 if (contents == NULL) {
1170 return 1;
1171 }
1172
Steven Valdeza833c352016-11-01 13:39:36 -04001173 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1174 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001175 return 0;
1176 }
1177
Steven Valdeza833c352016-11-01 13:39:36 -04001178 /* OCSP stapling is forbidden on non-certificate ciphers. */
1179 if (CBS_len(contents) != 0 ||
1180 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001181 return 0;
1182 }
1183
Steven Valdeza833c352016-11-01 13:39:36 -04001184 /* Note this does not check for resumption in TLS 1.2. Sending
1185 * status_request here does not make sense, but OpenSSL does so and the
1186 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001187
David Benjamin8c880a22016-12-03 02:20:34 -05001188 hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001189 return 1;
1190}
1191
David Benjamin8c880a22016-12-03 02:20:34 -05001192static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybb0bd042015-07-01 16:21:03 -07001193 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001194 if (contents == NULL) {
1195 return 1;
1196 }
1197
1198 uint8_t status_type;
1199 if (!CBS_get_u8(contents, &status_type)) {
1200 return 0;
1201 }
1202
1203 /* We cannot decide whether OCSP stapling will occur yet because the correct
1204 * SSL_CTX might not have been selected. */
David Benjamin8c880a22016-12-03 02:20:34 -05001205 hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001206
Adam Langleybb0bd042015-07-01 16:21:03 -07001207 return 1;
1208}
1209
David Benjamin8c880a22016-12-03 02:20:34 -05001210static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1211 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001212 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
David Benjamin8c880a22016-12-03 02:20:34 -05001213 !hs->ocsp_stapling_requested ||
David Benjamin942f4ed2016-07-16 19:03:49 +03001214 ssl->ctx->ocsp_response_length == 0 ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001215 ssl->s3->session_reused ||
Steven Valdeza833c352016-11-01 13:39:36 -04001216 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001217 return 1;
1218 }
1219
David Benjamin8c880a22016-12-03 02:20:34 -05001220 hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001221
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001222 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001223 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001224}
1225
1226
Adam Langley97dfcbf2015-07-01 18:35:20 -07001227/* Next protocol negotiation.
1228 *
1229 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1230
David Benjamin8c880a22016-12-03 02:20:34 -05001231static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1232 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001233 if (ssl->s3->initial_handshake_complete ||
1234 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin8c880a22016-12-03 02:20:34 -05001235 (ssl->options & SSL_OP_DISABLE_NPN) || SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001236 return 1;
1237 }
1238
1239 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1240 !CBB_add_u16(out, 0 /* length */)) {
1241 return 0;
1242 }
1243
1244 return 1;
1245}
1246
David Benjamin8c880a22016-12-03 02:20:34 -05001247static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley97dfcbf2015-07-01 18:35:20 -07001248 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001249 SSL *const ssl = hs->ssl;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001250 if (contents == NULL) {
1251 return 1;
1252 }
1253
Steven Valdez143e8b32016-07-11 13:19:03 -04001254 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1255 return 0;
1256 }
1257
Adam Langley97dfcbf2015-07-01 18:35:20 -07001258 /* If any of these are false then we should never have sent the NPN
1259 * extension in the ClientHello and thus this function should never have been
1260 * called. */
1261 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001262 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001263 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001264 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001265
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 Benjamin9ef31f02016-10-31 18:01:13 -04001535int ssl_negotiate_alpn(SSL *ssl, uint8_t *out_alert,
1536 const struct ssl_early_callback_ctx *client_hello) {
1537 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001538 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin9ef31f02016-10-31 18:01:13 -04001539 !ssl_early_callback_get_extension(
1540 client_hello, &contents,
1541 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1542 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001543 return 1;
1544 }
1545
1546 /* ALPN takes precedence over NPN. */
David Benjaminb74b0812016-10-06 19:43:48 -04001547 ssl->s3->hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001548
1549 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001550 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1551 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001552 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001553 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1554 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001555 return 0;
1556 }
1557
1558 /* Validate the protocol list. */
1559 CBS protocol_name_list_copy = protocol_name_list;
1560 while (CBS_len(&protocol_name_list_copy) > 0) {
1561 CBS protocol_name;
1562
1563 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1564 /* Empty protocol names are forbidden. */
1565 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001566 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1567 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001568 return 0;
1569 }
1570 }
1571
1572 const uint8_t *selected;
1573 uint8_t selected_len;
1574 if (ssl->ctx->alpn_select_cb(
1575 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1576 CBS_len(&protocol_name_list),
1577 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1578 OPENSSL_free(ssl->s3->alpn_selected);
1579 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1580 if (ssl->s3->alpn_selected == NULL) {
1581 *out_alert = SSL_AD_INTERNAL_ERROR;
1582 return 0;
1583 }
1584 ssl->s3->alpn_selected_len = selected_len;
1585 }
1586
1587 return 1;
1588}
1589
David Benjamin8c880a22016-12-03 02:20:34 -05001590static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1591 SSL *const ssl = hs->ssl;
Adam Langleyf18e4532015-07-10 13:39:53 -07001592 if (ssl->s3->alpn_selected == NULL) {
1593 return 1;
1594 }
1595
1596 CBB contents, proto_list, proto;
1597 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1598 !CBB_add_u16_length_prefixed(out, &contents) ||
1599 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1600 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001601 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1602 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001603 !CBB_flush(out)) {
1604 return 0;
1605 }
1606
1607 return 1;
1608}
1609
1610
Adam Langley49c7af12015-07-10 14:33:46 -07001611/* Channel ID.
1612 *
1613 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1614
David Benjamin8c880a22016-12-03 02:20:34 -05001615static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1616 hs->ssl->s3->tlsext_channel_id_valid = 0;
Adam Langley49c7af12015-07-10 14:33:46 -07001617}
1618
David Benjamin8c880a22016-12-03 02:20:34 -05001619static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1620 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001621 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001622 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001623 return 1;
1624 }
1625
1626 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1627 !CBB_add_u16(out, 0 /* length */)) {
1628 return 0;
1629 }
1630
1631 return 1;
1632}
1633
David Benjamin8c880a22016-12-03 02:20:34 -05001634static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1635 uint8_t *out_alert, CBS *contents) {
1636 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001637 if (contents == NULL) {
1638 return 1;
1639 }
1640
David Benjamince079fd2016-08-02 16:22:34 -04001641 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001642 assert(ssl->tlsext_channel_id_enabled);
1643
1644 if (CBS_len(contents) != 0) {
1645 return 0;
1646 }
1647
1648 ssl->s3->tlsext_channel_id_valid = 1;
1649 return 1;
1650}
1651
David Benjamin8c880a22016-12-03 02:20:34 -05001652static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1653 uint8_t *out_alert, CBS *contents) {
1654 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001655 if (contents == NULL ||
1656 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001657 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001658 return 1;
1659 }
1660
1661 if (CBS_len(contents) != 0) {
1662 return 0;
1663 }
1664
1665 ssl->s3->tlsext_channel_id_valid = 1;
1666 return 1;
1667}
1668
David Benjamin8c880a22016-12-03 02:20:34 -05001669static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1670 SSL *const ssl = hs->ssl;
Adam Langley49c7af12015-07-10 14:33:46 -07001671 if (!ssl->s3->tlsext_channel_id_valid) {
1672 return 1;
1673 }
1674
1675 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1676 !CBB_add_u16(out, 0 /* length */)) {
1677 return 0;
1678 }
1679
1680 return 1;
1681}
1682
Adam Langley391250d2015-07-15 19:06:07 -07001683
1684/* Secure Real-time Transport Protocol (SRTP) extension.
1685 *
1686 * https://tools.ietf.org/html/rfc5764 */
1687
Adam Langley391250d2015-07-15 19:06:07 -07001688
David Benjamin8c880a22016-12-03 02:20:34 -05001689static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1690 hs->ssl->srtp_profile = NULL;
Adam Langley391250d2015-07-15 19:06:07 -07001691}
1692
David Benjamin8c880a22016-12-03 02:20:34 -05001693static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1694 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001695 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1696 if (profiles == NULL) {
1697 return 1;
1698 }
1699 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1700 if (num_profiles == 0) {
1701 return 1;
1702 }
1703
1704 CBB contents, profile_ids;
1705 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1706 !CBB_add_u16_length_prefixed(out, &contents) ||
1707 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1708 return 0;
1709 }
1710
David Benjamin54091232016-09-05 12:47:25 -04001711 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001712 if (!CBB_add_u16(&profile_ids,
1713 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1714 return 0;
1715 }
1716 }
1717
1718 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1719 !CBB_flush(out)) {
1720 return 0;
1721 }
1722
1723 return 1;
1724}
1725
David Benjamin8c880a22016-12-03 02:20:34 -05001726static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001727 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001728 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001729 if (contents == NULL) {
1730 return 1;
1731 }
1732
1733 /* The extension consists of a u16-prefixed profile ID list containing a
1734 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1735 *
1736 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1737 CBS profile_ids, srtp_mki;
1738 uint16_t profile_id;
1739 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1740 !CBS_get_u16(&profile_ids, &profile_id) ||
1741 CBS_len(&profile_ids) != 0 ||
1742 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1743 CBS_len(contents) != 0) {
1744 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1745 return 0;
1746 }
1747
1748 if (CBS_len(&srtp_mki) != 0) {
1749 /* Must be no MKI, since we never offer one. */
1750 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1751 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1752 return 0;
1753 }
1754
1755 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1756
1757 /* Check to see if the server gave us something we support (and presumably
1758 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001759 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001760 const SRTP_PROTECTION_PROFILE *profile =
1761 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1762
1763 if (profile->id == profile_id) {
1764 ssl->srtp_profile = profile;
1765 return 1;
1766 }
1767 }
1768
1769 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1770 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1771 return 0;
1772}
1773
David Benjamin8c880a22016-12-03 02:20:34 -05001774static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langley391250d2015-07-15 19:06:07 -07001775 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001776 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001777 if (contents == NULL) {
1778 return 1;
1779 }
1780
1781 CBS profile_ids, srtp_mki;
1782 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1783 CBS_len(&profile_ids) < 2 ||
1784 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1785 CBS_len(contents) != 0) {
1786 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1787 return 0;
1788 }
1789 /* Discard the MKI value for now. */
1790
1791 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1792 SSL_get_srtp_profiles(ssl);
1793
1794 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001795 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001796 const SRTP_PROTECTION_PROFILE *server_profile =
1797 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1798
1799 CBS profile_ids_tmp;
1800 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1801
1802 while (CBS_len(&profile_ids_tmp) > 0) {
1803 uint16_t profile_id;
1804 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1805 return 0;
1806 }
1807
1808 if (server_profile->id == profile_id) {
1809 ssl->srtp_profile = server_profile;
1810 return 1;
1811 }
1812 }
1813 }
1814
1815 return 1;
1816}
1817
David Benjamin8c880a22016-12-03 02:20:34 -05001818static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1819 SSL *const ssl = hs->ssl;
Adam Langley391250d2015-07-15 19:06:07 -07001820 if (ssl->srtp_profile == NULL) {
1821 return 1;
1822 }
1823
1824 CBB contents, profile_ids;
1825 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1826 !CBB_add_u16_length_prefixed(out, &contents) ||
1827 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1828 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1829 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1830 !CBB_flush(out)) {
1831 return 0;
1832 }
1833
1834 return 1;
1835}
1836
Adam Langleybdd5d662015-07-20 16:19:08 -07001837
1838/* EC point formats.
1839 *
1840 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1841
David Benjamin8c880a22016-12-03 02:20:34 -05001842static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001843 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001844 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1845 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001846 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1847 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001848 !CBB_flush(out)) {
1849 return 0;
1850 }
1851
1852 return 1;
1853}
1854
David Benjamin8c880a22016-12-03 02:20:34 -05001855static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001856 uint16_t min_version, max_version;
David Benjamin8c880a22016-12-03 02:20:34 -05001857 if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) {
David Benjamin70aba262016-11-01 12:08:15 -04001858 return 0;
1859 }
1860
1861 /* The point format extension is unneccessary in TLS 1.3. */
1862 if (min_version >= TLS1_3_VERSION) {
1863 return 1;
1864 }
1865
David Benjamin8c880a22016-12-03 02:20:34 -05001866 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001867}
1868
David Benjamin8c880a22016-12-03 02:20:34 -05001869static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001870 CBS *contents) {
1871 if (contents == NULL) {
1872 return 1;
1873 }
1874
David Benjamin8c880a22016-12-03 02:20:34 -05001875 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001876 return 0;
1877 }
1878
Adam Langleybdd5d662015-07-20 16:19:08 -07001879 CBS ec_point_format_list;
1880 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1881 CBS_len(contents) != 0) {
1882 return 0;
1883 }
1884
David Benjaminfc059942015-07-30 23:01:59 -04001885 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1886 * point format. */
1887 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1888 CBS_len(&ec_point_format_list)) == NULL) {
1889 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001890 return 0;
1891 }
1892
1893 return 1;
1894}
1895
David Benjamin8c880a22016-12-03 02:20:34 -05001896static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
Adam Langleybdd5d662015-07-20 16:19:08 -07001897 CBS *contents) {
David Benjamin8c880a22016-12-03 02:20:34 -05001898 if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001899 return 1;
1900 }
1901
David Benjamin8c880a22016-12-03 02:20:34 -05001902 return ext_ec_point_parse_serverhello(hs, out_alert, contents);
Adam Langleybdd5d662015-07-20 16:19:08 -07001903}
1904
David Benjamin8c880a22016-12-03 02:20:34 -05001905static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1906 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04001907 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1908 return 1;
1909 }
1910
Adam Langleybdd5d662015-07-20 16:19:08 -07001911 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1912 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001913 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001914
1915 if (!using_ecc) {
1916 return 1;
1917 }
1918
David Benjamin8c880a22016-12-03 02:20:34 -05001919 return ext_ec_point_add_extension(hs, out);
Adam Langleybdd5d662015-07-20 16:19:08 -07001920}
1921
Steven Valdeza833c352016-11-01 13:39:36 -04001922
Steven Valdez4aa154e2016-07-29 14:32:55 -04001923/* Pre Shared Key
1924 *
Steven Valdeza833c352016-11-01 13:39:36 -04001925 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001926
David Benjamin8c880a22016-12-03 02:20:34 -05001927static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1928 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04001929 uint16_t min_version, max_version;
1930 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1931 return 0;
1932 }
1933
1934 uint16_t session_version;
1935 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1936 !ssl->method->version_from_wire(&session_version,
1937 ssl->session->ssl_version) ||
1938 session_version < TLS1_3_VERSION) {
1939 return 0;
1940 }
1941
1942 const EVP_MD *digest =
1943 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1944 size_t binder_len = EVP_MD_size(digest);
1945 return 15 + ssl->session->tlsext_ticklen + binder_len;
1946}
1947
David Benjamin8c880a22016-12-03 02:20:34 -05001948static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1949 SSL *const ssl = hs->ssl;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001950 uint16_t min_version, max_version;
1951 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1952 return 0;
1953 }
1954
David Benjamin2dc02042016-09-19 19:57:37 -04001955 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001956 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001957 !ssl->method->version_from_wire(&session_version,
1958 ssl->session->ssl_version) ||
1959 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001960 return 1;
1961 }
1962
Steven Valdeza833c352016-11-01 13:39:36 -04001963 struct timeval now;
1964 ssl_get_current_time(ssl, &now);
1965 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1966 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1967
1968 /* Fill in a placeholder zero binder of the appropriate length. It will be
1969 * computed and filled in later after length prefixes are computed. */
1970 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
1971 const EVP_MD *digest =
1972 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1973 size_t binder_len = EVP_MD_size(digest);
1974
1975 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001976 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1977 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001978 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001979 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1980 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001981 ssl->session->tlsext_ticklen) ||
1982 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1983 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1984 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1985 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001986 return 0;
1987 }
1988
David Benjamin8c880a22016-12-03 02:20:34 -05001989 hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001990 return CBB_flush(out);
1991}
1992
David Benjamin8baf9632016-11-17 17:11:16 +09001993int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
1994 uint8_t *out_alert,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001995 CBS *contents) {
1996 uint16_t psk_id;
1997 if (!CBS_get_u16(contents, &psk_id) ||
1998 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001999 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002000 *out_alert = SSL_AD_DECODE_ERROR;
2001 return 0;
2002 }
2003
David Benjamineab773a2016-11-09 18:32:35 -05002004 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04002005 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04002006 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002007 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2008 return 0;
2009 }
2010
2011 return 1;
2012}
2013
David Benjamin8baf9632016-11-17 17:11:16 +09002014int ssl_ext_pre_shared_key_parse_clienthello(SSL_HANDSHAKE *hs,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002015 SSL_SESSION **out_session,
Steven Valdeza833c352016-11-01 13:39:36 -04002016 CBS *out_binders,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002017 uint8_t *out_alert,
2018 CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09002019 SSL *const ssl = hs->ssl;
Steven Valdez5b986082016-09-01 12:29:49 -04002020 /* We only process the first PSK identity since we don't support pure PSK. */
Steven Valdeza833c352016-11-01 13:39:36 -04002021 uint32_t obfuscated_ticket_age;
David Benjaminaedf3032016-12-01 16:47:56 -05002022 CBS identities, ticket, binders;
2023 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2024 !CBS_get_u16_length_prefixed(&identities, &ticket) ||
2025 !CBS_get_u32(&identities, &obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04002026 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05002027 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04002028 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04002029 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002030 *out_alert = SSL_AD_DECODE_ERROR;
2031 return 0;
2032 }
2033
Steven Valdeza833c352016-11-01 13:39:36 -04002034 *out_binders = binders;
2035
David Benjaminaedf3032016-12-01 16:47:56 -05002036 /* Check the syntax of the remaining identities, but do not process them. */
2037 size_t num_identities = 1;
2038 while (CBS_len(&identities) != 0) {
2039 CBS unused_ticket;
2040 uint32_t unused_obfuscated_ticket_age;
2041 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
2042 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
2043 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2044 *out_alert = SSL_AD_DECODE_ERROR;
2045 return 0;
2046 }
2047
2048 num_identities++;
2049 }
2050
2051 /* Check the syntax of the binders. The value will be checked later if
2052 * resuming. */
2053 size_t num_binders = 0;
2054 while (CBS_len(&binders) != 0) {
2055 CBS binder;
2056 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
2057 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2058 *out_alert = SSL_AD_DECODE_ERROR;
2059 return 0;
2060 }
2061
2062 num_binders++;
2063 }
2064
2065 if (num_identities != num_binders) {
2066 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
2067 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04002068 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04002069 }
2070
Steven Valdeza833c352016-11-01 13:39:36 -04002071 /* TODO(svaldez): Check that the ticket_age is valid when attempting to use
2072 * the PSK for 0-RTT. http://crbug.com/boringssl/113 */
2073
Steven Valdez4aa154e2016-07-29 14:32:55 -04002074 /* TLS 1.3 session tickets are renewed separately as part of the
2075 * NewSessionTicket. */
David Benjamine7f60a22016-11-16 18:54:25 +09002076 int unused_renew;
2077 if (!tls_process_ticket(ssl, out_session, &unused_renew, CBS_data(&ticket),
2078 CBS_len(&ticket), NULL, 0)) {
2079 *out_alert = SSL_AD_INTERNAL_ERROR;
2080 return 0;
2081 }
2082
2083 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002084}
2085
David Benjamin8baf9632016-11-17 17:11:16 +09002086int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2087 if (!hs->ssl->s3->session_reused) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002088 return 1;
2089 }
2090
2091 CBB contents;
2092 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2093 !CBB_add_u16_length_prefixed(out, &contents) ||
2094 /* We only consider the first identity for resumption */
2095 !CBB_add_u16(&contents, 0) ||
2096 !CBB_flush(out)) {
2097 return 0;
2098 }
2099
2100 return 1;
2101}
2102
2103
Steven Valdeza833c352016-11-01 13:39:36 -04002104/* Pre-Shared Key Exchange Modes
2105 *
2106 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002107
David Benjamin8c880a22016-12-03 02:20:34 -05002108static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2109 CBB *out) {
2110 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04002111 uint16_t min_version, max_version;
2112 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2113 return 0;
2114 }
2115
2116 if (max_version < TLS1_3_VERSION) {
2117 return 1;
2118 }
2119
2120 CBB contents, ke_modes;
2121 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2122 !CBB_add_u16_length_prefixed(out, &contents) ||
2123 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2124 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2125 return 0;
2126 }
2127
2128 return CBB_flush(out);
2129}
2130
David Benjamin8c880a22016-12-03 02:20:34 -05002131static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002132 uint8_t *out_alert,
2133 CBS *contents) {
2134 if (contents == NULL) {
2135 return 1;
2136 }
2137
Steven Valdeza833c352016-11-01 13:39:36 -04002138 CBS ke_modes;
2139 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2140 CBS_len(&ke_modes) == 0 ||
2141 CBS_len(contents) != 0) {
2142 *out_alert = SSL_AD_DECODE_ERROR;
2143 return 0;
2144 }
2145
2146 /* We only support tickets with PSK_DHE_KE. */
David Benjamin8c880a22016-12-03 02:20:34 -05002147 hs->accept_psk_mode =
Steven Valdeza833c352016-11-01 13:39:36 -04002148 memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, CBS_len(&ke_modes)) != NULL;
2149
2150 return 1;
2151}
2152
2153
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002154/* Early Data Indication
2155 *
2156 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2157
David Benjamin8c880a22016-12-03 02:20:34 -05002158static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002159 /* TODO(svaldez): Support 0RTT. */
2160 return 1;
2161}
2162
David Benjamin8c880a22016-12-03 02:20:34 -05002163static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2164 uint8_t *out_alert, CBS *contents) {
2165 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002166 if (contents == NULL) {
2167 return 1;
2168 }
2169
2170 if (CBS_len(contents) != 0) {
2171 *out_alert = SSL_AD_DECODE_ERROR;
2172 return 0;
2173 }
2174
2175 /* Since we don't currently accept 0-RTT, we have to skip past any early data
2176 * the client might have sent. */
2177 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
2178 ssl->s3->skip_early_data = 1;
2179 }
2180 return 1;
2181}
2182
2183
Steven Valdez143e8b32016-07-11 13:19:03 -04002184/* Key Share
2185 *
David Benjamina128a552016-10-13 14:26:33 -04002186 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002187
David Benjamin8c880a22016-12-03 02:20:34 -05002188static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2189 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002190 uint16_t min_version, max_version;
2191 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2192 return 0;
2193 }
2194
David Benjamin53a2dfc2016-10-07 21:04:12 -04002195 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002196 return 1;
2197 }
2198
2199 CBB contents, kse_bytes;
2200 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2201 !CBB_add_u16_length_prefixed(out, &contents) ||
2202 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2203 return 0;
2204 }
2205
David Benjamin8c880a22016-12-03 02:20:34 -05002206 uint16_t group_id = hs->retry_group;
2207 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002208 /* We received a HelloRetryRequest without a new curve, so there is no new
2209 * share to append. Leave |ecdh_ctx| as-is. */
2210 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002211 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2212 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002213 return 0;
2214 }
David Benjamin8c880a22016-12-03 02:20:34 -05002215 OPENSSL_free(hs->key_share_bytes);
2216 hs->key_share_bytes = NULL;
2217 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002218 if (group_id == 0) {
2219 return CBB_flush(out);
2220 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002221 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002222 /* Add a fake group. See draft-davidben-tls-grease-01. */
2223 if (ssl->ctx->grease_enabled &&
2224 (!CBB_add_u16(&kse_bytes,
2225 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2226 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2227 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2228 return 0;
2229 }
2230
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002231 /* Predict the most preferred group. */
2232 const uint16_t *groups;
2233 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002234 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002235 if (groups_len == 0) {
2236 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2237 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002238 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002239
2240 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002241 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002242
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002243 CBB key_exchange;
2244 if (!CBB_add_u16(&kse_bytes, group_id) ||
2245 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002246 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2247 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002248 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002249 return 0;
2250 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002251
David Benjamin8c880a22016-12-03 02:20:34 -05002252 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002253 /* Save the contents of the extension to repeat it in the second
2254 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002255 hs->key_share_bytes_len = CBB_len(&kse_bytes);
2256 hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
2257 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002258 return 0;
2259 }
2260 }
2261
Steven Valdez143e8b32016-07-11 13:19:03 -04002262 return CBB_flush(out);
2263}
2264
David Benjamin8baf9632016-11-17 17:11:16 +09002265int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002266 size_t *out_secret_len,
2267 uint8_t *out_alert, CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09002268 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002269 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002270 uint16_t group_id;
2271 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002272 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2273 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002274 *out_alert = SSL_AD_DECODE_ERROR;
2275 return 0;
2276 }
2277
David Benjamin8baf9632016-11-17 17:11:16 +09002278 if (SSL_ECDH_CTX_get_id(&hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002279 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2280 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2281 return 0;
2282 }
2283
David Benjamin8baf9632016-11-17 17:11:16 +09002284 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, out_secret, out_secret_len, out_alert,
2285 CBS_data(&peer_key), CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002286 *out_alert = SSL_AD_INTERNAL_ERROR;
2287 return 0;
2288 }
2289
David Benjamin5c4e8572016-08-19 17:44:53 -04002290 ssl->s3->new_session->key_exchange_info = group_id;
David Benjamin8baf9632016-11-17 17:11:16 +09002291 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002292 return 1;
2293}
2294
David Benjamin8baf9632016-11-17 17:11:16 +09002295int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
Steven Valdez7259f2f2016-08-02 16:55:05 -04002296 uint8_t **out_secret,
2297 size_t *out_secret_len,
2298 uint8_t *out_alert, CBS *contents) {
David Benjamin8baf9632016-11-17 17:11:16 +09002299 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002300 uint16_t group_id;
2301 CBS key_shares;
Steven Valdez803c77a2016-09-06 14:13:43 -04002302 if (!tls1_get_shared_group(ssl, &group_id)) {
2303 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;
2376 if (!tls1_get_shared_group(ssl, &group_id) ||
2377 !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 Benjamine14ff062016-08-09 16:21:24 -04002885static int ssl_scan_clienthello_tlsext(
David Benjamin8c880a22016-12-03 02:20:34 -05002886 SSL_HANDSHAKE *hs, const struct ssl_early_callback_ctx *client_hello,
David Benjamine14ff062016-08-09 16:21:24 -04002887 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 Benjamine14ff062016-08-09 16:21:24 -04002971int ssl_parse_clienthello_tlsext(
David Benjamin8c880a22016-12-03 02:20:34 -05002972 SSL_HANDSHAKE *hs, const struct ssl_early_callback_ctx *client_hello) {
2973 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
Steven Valdez0d62f262015-09-04 12:41:04 -04003271int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003272 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05003273 if (ssl3_protocol_version(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 SSL_HANDSHAKE *hs = ssl->s3->hs;
3278 OPENSSL_free(hs->peer_sigalgs);
3279 hs->peer_sigalgs = NULL;
3280 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003281
3282 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003283 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003284 return 0;
3285 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003286 num_sigalgs /= 2;
3287
3288 /* supported_signature_algorithms in the certificate request is
3289 * allowed to be empty. */
3290 if (num_sigalgs == 0) {
3291 return 1;
3292 }
3293
Steven Valdez02563852016-06-23 13:33:05 -04003294 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3295 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003296 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3297 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003298 return 0;
3299 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003300 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003301
3302 CBS sigalgs;
3303 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003304 for (size_t i = 0; i < num_sigalgs; i++) {
3305 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003306 return 0;
3307 }
3308 }
Adam Langley95c29f32014-06-20 12:00:00 -07003309
Adam Langleyfcf25832014-12-18 17:42:32 -08003310 return 1;
3311}
David Benjaminec2f27d2014-11-13 19:17:25 -05003312
David Benjaminea9a0d52016-07-08 15:52:59 -07003313int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003314 CERT *cert = ssl->cert;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003315 SSL_HANDSHAKE *hs = ssl->s3->hs;
David Benjaminec2f27d2014-11-13 19:17:25 -05003316
Steven Valdezf0451ca2016-06-29 13:16:27 -04003317 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3318 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3319 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003320 int type = ssl_private_key_type(ssl);
3321 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003322 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003323 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003324 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003325 if (ssl_is_ecdsa_key_type(type)) {
3326 *out = SSL_SIGN_ECDSA_SHA1;
3327 return 1;
3328 }
3329 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3330 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003331 }
3332
David Benjamin3ef76972016-10-17 17:59:54 -04003333 const uint16_t *sigalgs = cert->sigalgs;
3334 size_t num_sigalgs = cert->num_sigalgs;
3335 if (sigalgs == NULL) {
3336 sigalgs = kSignSignatureAlgorithms;
3337 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003338 }
3339
David Benjamin0fc37ef2016-08-17 15:29:46 -04003340 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3341 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3342 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003343 /* If the client didn't specify any signature_algorithms extension then
3344 * we can assume that it supports SHA1. See
3345 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3346 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3347 SSL_SIGN_ECDSA_SHA1};
3348 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003349 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003350 }
3351
David Benjamin0fc37ef2016-08-17 15:29:46 -04003352 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003353 uint16_t sigalg = sigalgs[i];
3354 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3355 * negotiated. */
3356 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3357 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3358 continue;
3359 }
3360
David Benjamin0fc37ef2016-08-17 15:29:46 -04003361 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003362 if (sigalg == peer_sigalgs[j]) {
3363 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003364 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003365 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003366 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003367 }
Adam Langley95c29f32014-06-20 12:00:00 -07003368
David Benjaminea9a0d52016-07-08 15:52:59 -07003369 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3370 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003371}
Adam Langley95c29f32014-06-20 12:00:00 -07003372
Nick Harper60a85cb2016-09-23 16:25:11 -07003373int tls1_verify_channel_id(SSL *ssl) {
3374 int ret = 0;
3375 uint16_t extension_type;
3376 CBS extension, channel_id;
3377
3378 /* A Channel ID handshake message is structured to contain multiple
3379 * extensions, but the only one that can be present is Channel ID. */
3380 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3381 if (!CBS_get_u16(&channel_id, &extension_type) ||
3382 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3383 CBS_len(&channel_id) != 0 ||
3384 extension_type != TLSEXT_TYPE_channel_id ||
3385 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3386 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3387 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3388 return 0;
3389 }
3390
3391 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3392 if (!p256) {
3393 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3394 return 0;
3395 }
3396
3397 EC_KEY *key = NULL;
3398 EC_POINT *point = NULL;
3399 BIGNUM x, y;
3400 ECDSA_SIG sig;
3401 BN_init(&x);
3402 BN_init(&y);
3403 sig.r = BN_new();
3404 sig.s = BN_new();
3405 if (sig.r == NULL || sig.s == NULL) {
3406 goto err;
3407 }
3408
3409 const uint8_t *p = CBS_data(&extension);
3410 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3411 BN_bin2bn(p + 32, 32, &y) == NULL ||
3412 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3413 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3414 goto err;
3415 }
3416
3417 point = EC_POINT_new(p256);
3418 if (point == NULL ||
3419 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3420 goto err;
3421 }
3422
3423 key = EC_KEY_new();
3424 if (key == NULL ||
3425 !EC_KEY_set_group(key, p256) ||
3426 !EC_KEY_set_public_key(key, point)) {
3427 goto err;
3428 }
3429
3430 uint8_t digest[EVP_MAX_MD_SIZE];
3431 size_t digest_len;
3432 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3433 goto err;
3434 }
3435
3436 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3437#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3438 sig_ok = 1;
3439#endif
3440 if (!sig_ok) {
3441 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3442 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3443 ssl->s3->tlsext_channel_id_valid = 0;
3444 goto err;
3445 }
3446
3447 memcpy(ssl->s3->tlsext_channel_id, p, 64);
3448 ret = 1;
3449
3450err:
3451 BN_free(&x);
3452 BN_free(&y);
3453 BN_free(sig.r);
3454 BN_free(sig.s);
3455 EC_KEY_free(key);
3456 EC_POINT_free(point);
3457 EC_GROUP_free(p256);
3458 return ret;
3459}
3460
3461int tls1_write_channel_id(SSL *ssl, CBB *cbb) {
3462 uint8_t digest[EVP_MAX_MD_SIZE];
3463 size_t digest_len;
3464 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3465 return 0;
3466 }
3467
3468 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3469 if (ec_key == NULL) {
3470 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3471 return 0;
3472 }
3473
3474 int ret = 0;
3475 BIGNUM *x = BN_new();
3476 BIGNUM *y = BN_new();
3477 ECDSA_SIG *sig = NULL;
3478 if (x == NULL || y == NULL ||
3479 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3480 EC_KEY_get0_public_key(ec_key),
3481 x, y, NULL)) {
3482 goto err;
3483 }
3484
3485 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3486 if (sig == NULL) {
3487 goto err;
3488 }
3489
3490 CBB child;
3491 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3492 !CBB_add_u16_length_prefixed(cbb, &child) ||
3493 !BN_bn2cbb_padded(&child, 32, x) ||
3494 !BN_bn2cbb_padded(&child, 32, y) ||
3495 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3496 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3497 !CBB_flush(cbb)) {
3498 goto err;
3499 }
3500
3501 ret = 1;
3502
3503err:
3504 BN_free(x);
3505 BN_free(y);
3506 ECDSA_SIG_free(sig);
3507 return ret;
3508}
3509
David Benjamind6a4ae92015-08-06 11:10:51 -04003510int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003511 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3512 uint8_t *msg;
3513 size_t msg_len;
3514 if (!tls13_get_cert_verify_signature_input(ssl, &msg, &msg_len,
3515 ssl_cert_verify_channel_id)) {
3516 return 0;
3517 }
3518 SHA256(msg, msg_len, out);
3519 *out_len = SHA256_DIGEST_LENGTH;
3520 OPENSSL_free(msg);
3521 return 1;
3522 }
3523
Nick Harper95594012016-10-20 14:07:13 -07003524 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003525
Nick Harper95594012016-10-20 14:07:13 -07003526 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003527 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003528 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003529
Steven Valdez87eab492016-06-27 16:34:59 -04003530 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003531 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003532 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003533 if (ssl->session->original_handshake_hash_len == 0) {
3534 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003535 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003536 }
Nick Harper95594012016-10-20 14:07:13 -07003537 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3538 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003539 }
3540
3541 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3542 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3543 sizeof(handshake_hash));
3544 if (handshake_hash_len < 0) {
Nick Harper95594012016-10-20 14:07:13 -07003545 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003546 }
Nick Harper95594012016-10-20 14:07:13 -07003547 SHA256_Update(&ctx, handshake_hash, (size_t)handshake_hash_len);
3548 SHA256_Final(out, &ctx);
3549 *out_len = SHA256_DIGEST_LENGTH;
3550 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003551}
Adam Langley1258b6a2014-06-20 12:00:00 -07003552
3553/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003554 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003555 * data. */
3556int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003557 int digest_len;
3558 /* This function should never be called for a resumed session because the
3559 * handshake hashes that we wish to record are for the original, full
3560 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003561 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003562 return -1;
3563 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003564
Adam Langleyfcf25832014-12-18 17:42:32 -08003565 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003566 tls1_handshake_digest(
3567 ssl, ssl->s3->new_session->original_handshake_hash,
3568 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003569 if (digest_len < 0) {
3570 return -1;
3571 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003572
Steven Valdez87eab492016-06-27 16:34:59 -04003573 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003574
Adam Langleyfcf25832014-12-18 17:42:32 -08003575 return 1;
3576}
Nick Harper60a85cb2016-09-23 16:25:11 -07003577
3578int ssl_do_channel_id_callback(SSL *ssl) {
3579 if (ssl->tlsext_channel_id_private != NULL ||
3580 ssl->ctx->channel_id_cb == NULL) {
3581 return 1;
3582 }
3583
3584 EVP_PKEY *key = NULL;
3585 ssl->ctx->channel_id_cb(ssl, &key);
3586 if (key == NULL) {
3587 /* The caller should try again later. */
3588 return 1;
3589 }
3590
3591 int ret = SSL_set1_tls_channel_id(ssl, key);
3592 EVP_PKEY_free(key);
3593 return ret;
3594}
Adam Langleycfa08c32016-11-17 13:21:27 -08003595
3596int ssl_is_sct_list_valid(const CBS *contents) {
3597 /* Shallow parse the SCT list for sanity. By the RFC
3598 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3599 * of the SCTs may be empty. */
3600 CBS copy = *contents;
3601 CBS sct_list;
3602 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3603 CBS_len(&copy) != 0 ||
3604 CBS_len(&sct_list) == 0) {
3605 return 0;
3606 }
3607
3608 while (CBS_len(&sct_list) > 0) {
3609 CBS sct;
3610 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3611 CBS_len(&sct) == 0) {
3612 return 0;
3613 }
3614 }
3615
3616 return 1;
3617}