blob: a908a99b5865768bda5d8fef528f4804efa7bda8 [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 Benjamin3e51757d2016-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
1993int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1994 CBS *contents) {
1995 uint16_t psk_id;
1996 if (!CBS_get_u16(contents, &psk_id) ||
1997 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001998 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001999 *out_alert = SSL_AD_DECODE_ERROR;
2000 return 0;
2001 }
2002
David Benjamineab773a2016-11-09 18:32:35 -05002003 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04002004 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04002005 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002006 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2007 return 0;
2008 }
2009
2010 return 1;
2011}
2012
2013int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
2014 SSL_SESSION **out_session,
Steven Valdeza833c352016-11-01 13:39:36 -04002015 CBS *out_binders,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002016 uint8_t *out_alert,
2017 CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04002018 /* We only process the first PSK identity since we don't support pure PSK. */
Steven Valdeza833c352016-11-01 13:39:36 -04002019 uint32_t obfuscated_ticket_age;
David Benjaminaedf3032016-12-01 16:47:56 -05002020 CBS identities, ticket, binders;
2021 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2022 !CBS_get_u16_length_prefixed(&identities, &ticket) ||
2023 !CBS_get_u32(&identities, &obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04002024 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05002025 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04002026 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04002027 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002028 *out_alert = SSL_AD_DECODE_ERROR;
2029 return 0;
2030 }
2031
Steven Valdeza833c352016-11-01 13:39:36 -04002032 *out_binders = binders;
2033
David Benjaminaedf3032016-12-01 16:47:56 -05002034 /* Check the syntax of the remaining identities, but do not process them. */
2035 size_t num_identities = 1;
2036 while (CBS_len(&identities) != 0) {
2037 CBS unused_ticket;
2038 uint32_t unused_obfuscated_ticket_age;
2039 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
2040 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
2041 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2042 *out_alert = SSL_AD_DECODE_ERROR;
2043 return 0;
2044 }
2045
2046 num_identities++;
2047 }
2048
2049 /* Check the syntax of the binders. The value will be checked later if
2050 * resuming. */
2051 size_t num_binders = 0;
2052 while (CBS_len(&binders) != 0) {
2053 CBS binder;
2054 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
2055 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2056 *out_alert = SSL_AD_DECODE_ERROR;
2057 return 0;
2058 }
2059
2060 num_binders++;
2061 }
2062
2063 if (num_identities != num_binders) {
2064 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
2065 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04002066 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04002067 }
2068
Steven Valdeza833c352016-11-01 13:39:36 -04002069 /* TODO(svaldez): Check that the ticket_age is valid when attempting to use
2070 * the PSK for 0-RTT. http://crbug.com/boringssl/113 */
2071
Steven Valdez4aa154e2016-07-29 14:32:55 -04002072 /* TLS 1.3 session tickets are renewed separately as part of the
2073 * NewSessionTicket. */
David Benjamine7f60a22016-11-16 18:54:25 +09002074 int unused_renew;
2075 if (!tls_process_ticket(ssl, out_session, &unused_renew, CBS_data(&ticket),
2076 CBS_len(&ticket), NULL, 0)) {
2077 *out_alert = SSL_AD_INTERNAL_ERROR;
2078 return 0;
2079 }
2080
2081 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002082}
2083
2084int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
2085 if (!ssl->s3->session_reused) {
2086 return 1;
2087 }
2088
2089 CBB contents;
2090 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2091 !CBB_add_u16_length_prefixed(out, &contents) ||
2092 /* We only consider the first identity for resumption */
2093 !CBB_add_u16(&contents, 0) ||
2094 !CBB_flush(out)) {
2095 return 0;
2096 }
2097
2098 return 1;
2099}
2100
2101
Steven Valdeza833c352016-11-01 13:39:36 -04002102/* Pre-Shared Key Exchange Modes
2103 *
2104 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002105
David Benjamin8c880a22016-12-03 02:20:34 -05002106static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2107 CBB *out) {
2108 SSL *const ssl = hs->ssl;
Steven Valdeza833c352016-11-01 13:39:36 -04002109 uint16_t min_version, max_version;
2110 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2111 return 0;
2112 }
2113
2114 if (max_version < TLS1_3_VERSION) {
2115 return 1;
2116 }
2117
2118 CBB contents, ke_modes;
2119 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2120 !CBB_add_u16_length_prefixed(out, &contents) ||
2121 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2122 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2123 return 0;
2124 }
2125
2126 return CBB_flush(out);
2127}
2128
David Benjamin8c880a22016-12-03 02:20:34 -05002129static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002130 uint8_t *out_alert,
2131 CBS *contents) {
2132 if (contents == NULL) {
2133 return 1;
2134 }
2135
Steven Valdeza833c352016-11-01 13:39:36 -04002136 CBS ke_modes;
2137 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2138 CBS_len(&ke_modes) == 0 ||
2139 CBS_len(contents) != 0) {
2140 *out_alert = SSL_AD_DECODE_ERROR;
2141 return 0;
2142 }
2143
2144 /* We only support tickets with PSK_DHE_KE. */
David Benjamin8c880a22016-12-03 02:20:34 -05002145 hs->accept_psk_mode =
Steven Valdeza833c352016-11-01 13:39:36 -04002146 memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, CBS_len(&ke_modes)) != NULL;
2147
2148 return 1;
2149}
2150
2151
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002152/* Early Data Indication
2153 *
2154 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2155
David Benjamin8c880a22016-12-03 02:20:34 -05002156static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002157 /* TODO(svaldez): Support 0RTT. */
2158 return 1;
2159}
2160
David Benjamin8c880a22016-12-03 02:20:34 -05002161static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
2162 uint8_t *out_alert, CBS *contents) {
2163 SSL *const ssl = hs->ssl;
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002164 if (contents == NULL) {
2165 return 1;
2166 }
2167
2168 if (CBS_len(contents) != 0) {
2169 *out_alert = SSL_AD_DECODE_ERROR;
2170 return 0;
2171 }
2172
2173 /* Since we don't currently accept 0-RTT, we have to skip past any early data
2174 * the client might have sent. */
2175 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
2176 ssl->s3->skip_early_data = 1;
2177 }
2178 return 1;
2179}
2180
2181
Steven Valdez143e8b32016-07-11 13:19:03 -04002182/* Key Share
2183 *
David Benjamina128a552016-10-13 14:26:33 -04002184 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002185
David Benjamin8c880a22016-12-03 02:20:34 -05002186static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2187 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002188 uint16_t min_version, max_version;
2189 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2190 return 0;
2191 }
2192
David Benjamin53a2dfc2016-10-07 21:04:12 -04002193 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002194 return 1;
2195 }
2196
2197 CBB contents, kse_bytes;
2198 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2199 !CBB_add_u16_length_prefixed(out, &contents) ||
2200 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2201 return 0;
2202 }
2203
David Benjamin8c880a22016-12-03 02:20:34 -05002204 uint16_t group_id = hs->retry_group;
2205 if (hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002206 /* We received a HelloRetryRequest without a new curve, so there is no new
2207 * share to append. Leave |ecdh_ctx| as-is. */
2208 if (group_id == 0 &&
David Benjamin8c880a22016-12-03 02:20:34 -05002209 !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
2210 hs->key_share_bytes_len)) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002211 return 0;
2212 }
David Benjamin8c880a22016-12-03 02:20:34 -05002213 OPENSSL_free(hs->key_share_bytes);
2214 hs->key_share_bytes = NULL;
2215 hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002216 if (group_id == 0) {
2217 return CBB_flush(out);
2218 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002219 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002220 /* Add a fake group. See draft-davidben-tls-grease-01. */
2221 if (ssl->ctx->grease_enabled &&
2222 (!CBB_add_u16(&kse_bytes,
2223 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2224 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2225 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2226 return 0;
2227 }
2228
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002229 /* Predict the most preferred group. */
2230 const uint16_t *groups;
2231 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002232 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002233 if (groups_len == 0) {
2234 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2235 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002236 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002237
2238 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002239 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002240
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002241 CBB key_exchange;
2242 if (!CBB_add_u16(&kse_bytes, group_id) ||
2243 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002244 !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
2245 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) ||
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002246 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002247 return 0;
2248 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002249
David Benjamin8c880a22016-12-03 02:20:34 -05002250 if (!hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002251 /* Save the contents of the extension to repeat it in the second
2252 * ClientHello. */
David Benjamin8c880a22016-12-03 02:20:34 -05002253 hs->key_share_bytes_len = CBB_len(&kse_bytes);
2254 hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
2255 if (hs->key_share_bytes == NULL) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002256 return 0;
2257 }
2258 }
2259
Steven Valdez143e8b32016-07-11 13:19:03 -04002260 return CBB_flush(out);
2261}
2262
Steven Valdez7259f2f2016-08-02 16:55:05 -04002263int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2264 size_t *out_secret_len,
2265 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002266 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002267 uint16_t group_id;
2268 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002269 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2270 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002271 *out_alert = SSL_AD_DECODE_ERROR;
2272 return 0;
2273 }
2274
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002275 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002276 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2277 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2278 return 0;
2279 }
2280
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002281 if (!SSL_ECDH_CTX_finish(&ssl->s3->hs->ecdh_ctx, out_secret, out_secret_len,
2282 out_alert, CBS_data(&peer_key),
2283 CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002284 *out_alert = SSL_AD_INTERNAL_ERROR;
2285 return 0;
2286 }
2287
David Benjamin5c4e8572016-08-19 17:44:53 -04002288 ssl->s3->new_session->key_exchange_info = group_id;
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002289 SSL_ECDH_CTX_cleanup(&ssl->s3->hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002290 return 1;
2291}
2292
Steven Valdez7259f2f2016-08-02 16:55:05 -04002293int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2294 uint8_t **out_secret,
2295 size_t *out_secret_len,
2296 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002297 uint16_t group_id;
2298 CBS key_shares;
Steven Valdez803c77a2016-09-06 14:13:43 -04002299 if (!tls1_get_shared_group(ssl, &group_id)) {
2300 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2301 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2302 return 0;
2303 }
2304
2305 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002306 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002307 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002308 return 0;
2309 }
2310
David Benjamin7e1f9842016-09-20 19:24:40 -04002311 /* Find the corresponding key share. */
2312 int found = 0;
2313 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002314 while (CBS_len(&key_shares) > 0) {
2315 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002316 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002317 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002318 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2319 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002320 return 0;
2321 }
2322
David Benjamin7e1f9842016-09-20 19:24:40 -04002323 if (id == group_id) {
2324 if (found) {
2325 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2326 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2327 return 0;
2328 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002329
David Benjamin7e1f9842016-09-20 19:24:40 -04002330 found = 1;
2331 peer_key = peer_key_tmp;
2332 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002333 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002334 }
2335
David Benjamin7e1f9842016-09-20 19:24:40 -04002336 if (!found) {
2337 *out_found = 0;
2338 *out_secret = NULL;
2339 *out_secret_len = 0;
2340 return 1;
2341 }
2342
2343 /* Compute the DH secret. */
2344 uint8_t *secret = NULL;
2345 size_t secret_len;
2346 SSL_ECDH_CTX group;
2347 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2348 CBB public_key;
2349 if (!CBB_init(&public_key, 32) ||
2350 !SSL_ECDH_CTX_init(&group, group_id) ||
2351 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len,
2352 out_alert, CBS_data(&peer_key),
2353 CBS_len(&peer_key)) ||
2354 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2355 &ssl->s3->hs->public_key_len)) {
2356 OPENSSL_free(secret);
2357 SSL_ECDH_CTX_cleanup(&group);
2358 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002359 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002360 return 0;
2361 }
2362
2363 SSL_ECDH_CTX_cleanup(&group);
2364
2365 *out_secret = secret;
2366 *out_secret_len = secret_len;
2367 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002368 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002369}
2370
Steven Valdez7259f2f2016-08-02 16:55:05 -04002371int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002372 uint16_t group_id;
2373 CBB kse_bytes, public_key;
2374 if (!tls1_get_shared_group(ssl, &group_id) ||
2375 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2376 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2377 !CBB_add_u16(&kse_bytes, group_id) ||
2378 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2379 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2380 ssl->s3->hs->public_key_len) ||
2381 !CBB_flush(out)) {
2382 return 0;
2383 }
2384
David Benjamin4fe3c902016-08-16 02:17:03 -04002385 OPENSSL_free(ssl->s3->hs->public_key);
2386 ssl->s3->hs->public_key = NULL;
2387 ssl->s3->hs->public_key_len = 0;
2388
David Benjamin5c4e8572016-08-19 17:44:53 -04002389 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002390 return 1;
2391}
2392
2393
Steven Valdezfdd10992016-09-15 16:27:05 -04002394/* Supported Versions
2395 *
2396 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2397
David Benjamin8c880a22016-12-03 02:20:34 -05002398static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2399 SSL *const ssl = hs->ssl;
Steven Valdezfdd10992016-09-15 16:27:05 -04002400 uint16_t min_version, max_version;
2401 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2402 return 0;
2403 }
2404
2405 if (max_version <= TLS1_2_VERSION) {
2406 return 1;
2407 }
2408
2409 CBB contents, versions;
2410 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2411 !CBB_add_u16_length_prefixed(out, &contents) ||
2412 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2413 return 0;
2414 }
2415
David Benjamind9791bf2016-09-27 16:39:52 -04002416 /* Add a fake version. See draft-davidben-tls-grease-01. */
2417 if (ssl->ctx->grease_enabled &&
2418 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2419 return 0;
2420 }
2421
Steven Valdezfdd10992016-09-15 16:27:05 -04002422 for (uint16_t version = max_version; version >= min_version; version--) {
2423 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2424 return 0;
2425 }
2426 }
2427
2428 if (!CBB_flush(out)) {
2429 return 0;
2430 }
2431
2432 return 1;
2433}
2434
2435
David Benjamin3baa6e12016-10-07 21:10:38 -04002436/* Cookie
2437 *
2438 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2439
David Benjamin8c880a22016-12-03 02:20:34 -05002440static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2441 if (hs->cookie == NULL) {
David Benjamin3baa6e12016-10-07 21:10:38 -04002442 return 1;
2443 }
2444
2445 CBB contents, cookie;
2446 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2447 !CBB_add_u16_length_prefixed(out, &contents) ||
2448 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
David Benjamin8c880a22016-12-03 02:20:34 -05002449 !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
David Benjamin3baa6e12016-10-07 21:10:38 -04002450 !CBB_flush(out)) {
2451 return 0;
2452 }
2453
2454 /* The cookie is no longer needed in memory. */
David Benjamin8c880a22016-12-03 02:20:34 -05002455 OPENSSL_free(hs->cookie);
2456 hs->cookie = NULL;
2457 hs->cookie_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002458 return 1;
2459}
2460
2461
Steven Valdezce902a92016-05-17 11:47:53 -04002462/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002463 *
Steven Valdezce902a92016-05-17 11:47:53 -04002464 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002465 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002466
David Benjamin8c880a22016-12-03 02:20:34 -05002467static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2468 SSL *const ssl = hs->ssl;
Steven Valdezce902a92016-05-17 11:47:53 -04002469 CBB contents, groups_bytes;
2470 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002471 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002472 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002473 return 0;
2474 }
2475
David Benjamin65ac9972016-09-02 21:35:25 -04002476 /* Add a fake group. See draft-davidben-tls-grease-01. */
2477 if (ssl->ctx->grease_enabled &&
2478 !CBB_add_u16(&groups_bytes,
2479 ssl_get_grease_value(ssl, ssl_grease_group))) {
2480 return 0;
2481 }
2482
Steven Valdezce902a92016-05-17 11:47:53 -04002483 const uint16_t *groups;
2484 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002485 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002486
David Benjamin54091232016-09-05 12:47:25 -04002487 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002488 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002489 return 0;
2490 }
2491 }
2492
2493 return CBB_flush(out);
2494}
2495
David Benjamin8c880a22016-12-03 02:20:34 -05002496static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
2497 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002498 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002499 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2500 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002501 return 1;
2502}
2503
David Benjamin8c880a22016-12-03 02:20:34 -05002504static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2505 uint8_t *out_alert,
Steven Valdezce902a92016-05-17 11:47:53 -04002506 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002507 if (contents == NULL) {
2508 return 1;
2509 }
2510
Steven Valdezce902a92016-05-17 11:47:53 -04002511 CBS supported_group_list;
2512 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2513 CBS_len(&supported_group_list) == 0 ||
2514 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002515 CBS_len(contents) != 0) {
2516 return 0;
2517 }
2518
David Benjamin8c880a22016-12-03 02:20:34 -05002519 hs->peer_supported_group_list =
2520 OPENSSL_malloc(CBS_len(&supported_group_list));
2521 if (hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002522 *out_alert = SSL_AD_INTERNAL_ERROR;
2523 return 0;
2524 }
2525
Steven Valdezce902a92016-05-17 11:47:53 -04002526 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002527 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002528 if (!CBS_get_u16(&supported_group_list,
David Benjamin8c880a22016-12-03 02:20:34 -05002529 &hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002530 goto err;
2531 }
2532 }
2533
Steven Valdezce902a92016-05-17 11:47:53 -04002534 assert(CBS_len(&supported_group_list) == 0);
David Benjamin8c880a22016-12-03 02:20:34 -05002535 hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002536
2537 return 1;
2538
2539err:
David Benjamin8c880a22016-12-03 02:20:34 -05002540 OPENSSL_free(hs->peer_supported_group_list);
2541 hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002542 *out_alert = SSL_AD_INTERNAL_ERROR;
2543 return 0;
2544}
2545
David Benjamin8c880a22016-12-03 02:20:34 -05002546static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002547 /* Servers don't echo this extension. */
2548 return 1;
2549}
2550
2551
Adam Langley614c66a2015-06-12 15:26:58 -07002552/* kExtensions contains all the supported extensions. */
2553static const struct tls_extension kExtensions[] = {
2554 {
Adam Langley5021b222015-06-12 18:27:58 -07002555 TLSEXT_TYPE_renegotiate,
2556 NULL,
2557 ext_ri_add_clienthello,
2558 ext_ri_parse_serverhello,
2559 ext_ri_parse_clienthello,
2560 ext_ri_add_serverhello,
2561 },
2562 {
Adam Langley614c66a2015-06-12 15:26:58 -07002563 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002564 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002565 ext_sni_add_clienthello,
2566 ext_sni_parse_serverhello,
2567 ext_sni_parse_clienthello,
2568 ext_sni_add_serverhello,
2569 },
Adam Langley0a056712015-07-01 15:03:33 -07002570 {
2571 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002572 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002573 ext_ems_add_clienthello,
2574 ext_ems_parse_serverhello,
2575 ext_ems_parse_clienthello,
2576 ext_ems_add_serverhello,
2577 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002578 {
2579 TLSEXT_TYPE_session_ticket,
2580 NULL,
2581 ext_ticket_add_clienthello,
2582 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002583 /* Ticket extension client parsing is handled in ssl_session.c */
2584 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002585 ext_ticket_add_serverhello,
2586 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002587 {
2588 TLSEXT_TYPE_signature_algorithms,
2589 NULL,
2590 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002591 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002592 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002593 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002594 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002595 {
2596 TLSEXT_TYPE_status_request,
2597 ext_ocsp_init,
2598 ext_ocsp_add_clienthello,
2599 ext_ocsp_parse_serverhello,
2600 ext_ocsp_parse_clienthello,
2601 ext_ocsp_add_serverhello,
2602 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002603 {
2604 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002605 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002606 ext_npn_add_clienthello,
2607 ext_npn_parse_serverhello,
2608 ext_npn_parse_clienthello,
2609 ext_npn_add_serverhello,
2610 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002611 {
2612 TLSEXT_TYPE_certificate_timestamp,
2613 NULL,
2614 ext_sct_add_clienthello,
2615 ext_sct_parse_serverhello,
2616 ext_sct_parse_clienthello,
2617 ext_sct_add_serverhello,
2618 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002619 {
2620 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002621 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002622 ext_alpn_add_clienthello,
2623 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002624 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2625 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002626 ext_alpn_add_serverhello,
2627 },
Adam Langley49c7af12015-07-10 14:33:46 -07002628 {
2629 TLSEXT_TYPE_channel_id,
2630 ext_channel_id_init,
2631 ext_channel_id_add_clienthello,
2632 ext_channel_id_parse_serverhello,
2633 ext_channel_id_parse_clienthello,
2634 ext_channel_id_add_serverhello,
2635 },
Adam Langley391250d2015-07-15 19:06:07 -07002636 {
2637 TLSEXT_TYPE_srtp,
2638 ext_srtp_init,
2639 ext_srtp_add_clienthello,
2640 ext_srtp_parse_serverhello,
2641 ext_srtp_parse_clienthello,
2642 ext_srtp_add_serverhello,
2643 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002644 {
2645 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002646 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002647 ext_ec_point_add_clienthello,
2648 ext_ec_point_parse_serverhello,
2649 ext_ec_point_parse_clienthello,
2650 ext_ec_point_add_serverhello,
2651 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002652 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002653 TLSEXT_TYPE_key_share,
2654 NULL,
2655 ext_key_share_add_clienthello,
2656 forbid_parse_serverhello,
2657 ignore_parse_clienthello,
2658 dont_add_serverhello,
2659 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002660 {
Steven Valdeza833c352016-11-01 13:39:36 -04002661 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002662 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002663 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002664 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002665 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002666 dont_add_serverhello,
2667 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002668 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002669 TLSEXT_TYPE_early_data,
2670 NULL,
2671 ext_early_data_add_clienthello,
2672 forbid_parse_serverhello,
2673 ext_early_data_parse_clienthello,
2674 dont_add_serverhello,
2675 },
2676 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002677 TLSEXT_TYPE_supported_versions,
2678 NULL,
2679 ext_supported_versions_add_clienthello,
2680 forbid_parse_serverhello,
2681 ignore_parse_clienthello,
2682 dont_add_serverhello,
2683 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002684 {
2685 TLSEXT_TYPE_cookie,
2686 NULL,
2687 ext_cookie_add_clienthello,
2688 forbid_parse_serverhello,
2689 ignore_parse_clienthello,
2690 dont_add_serverhello,
2691 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002692 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2693 * intolerant to the last extension being zero-length. See
2694 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002695 {
Steven Valdezce902a92016-05-17 11:47:53 -04002696 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002697 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002698 ext_supported_groups_add_clienthello,
2699 ext_supported_groups_parse_serverhello,
2700 ext_supported_groups_parse_clienthello,
2701 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002702 },
Adam Langley614c66a2015-06-12 15:26:58 -07002703};
2704
2705#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2706
Adam Langley4cfa96b2015-07-01 11:56:55 -07002707OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002708 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002709 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002710OPENSSL_COMPILE_ASSERT(
2711 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2712 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002713
Adam Langley614c66a2015-06-12 15:26:58 -07002714static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2715 uint16_t value) {
2716 unsigned i;
2717 for (i = 0; i < kNumExtensions; i++) {
2718 if (kExtensions[i].value == value) {
2719 *out_index = i;
2720 return &kExtensions[i];
2721 }
2722 }
2723
2724 return NULL;
2725}
2726
Adam Langley09505632015-07-30 18:10:13 -07002727int SSL_extension_supported(unsigned extension_value) {
2728 uint32_t index;
2729 return extension_value == TLSEXT_TYPE_padding ||
2730 tls_extension_find(&index, extension_value) != NULL;
2731}
2732
David Benjamin8c880a22016-12-03 02:20:34 -05002733int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
2734 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002735 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002736 if (ssl->client_version == SSL3_VERSION &&
2737 !ssl->s3->send_connection_binding) {
2738 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002739 }
Adam Langley95c29f32014-06-20 12:00:00 -07002740
David Benjamine8d53502015-10-10 14:13:23 -04002741 CBB extensions;
2742 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002743 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002744 }
Adam Langley95c29f32014-06-20 12:00:00 -07002745
David Benjamin8c880a22016-12-03 02:20:34 -05002746 hs->extensions.sent = 0;
2747 hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002748
David Benjamin54091232016-09-05 12:47:25 -04002749 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002750 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002751 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002752 }
2753 }
Adam Langley95c29f32014-06-20 12:00:00 -07002754
David Benjamin65ac9972016-09-02 21:35:25 -04002755 uint16_t grease_ext1 = 0;
2756 if (ssl->ctx->grease_enabled) {
2757 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2758 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2759 if (!CBB_add_u16(&extensions, grease_ext1) ||
2760 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2761 goto err;
2762 }
2763 }
2764
David Benjamin54091232016-09-05 12:47:25 -04002765 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002766 const size_t len_before = CBB_len(&extensions);
David Benjamin8c880a22016-12-03 02:20:34 -05002767 if (!kExtensions[i].add_clienthello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002768 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002769 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002770 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002771 }
Adam Langley95c29f32014-06-20 12:00:00 -07002772
Adam Langley33ad2b52015-07-20 17:43:53 -07002773 if (CBB_len(&extensions) != len_before) {
David Benjamin8c880a22016-12-03 02:20:34 -05002774 hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002775 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002776 }
Adam Langley75712922014-10-10 16:23:43 -07002777
David Benjamine8d53502015-10-10 14:13:23 -04002778 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002779 goto err;
2780 }
2781
David Benjamin65ac9972016-09-02 21:35:25 -04002782 if (ssl->ctx->grease_enabled) {
2783 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2784 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2785
2786 /* The two fake extensions must not have the same value. GREASE values are
2787 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2788 * one. */
2789 if (grease_ext1 == grease_ext2) {
2790 grease_ext2 ^= 0x1010;
2791 }
2792
2793 if (!CBB_add_u16(&extensions, grease_ext2) ||
2794 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2795 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2796 goto err;
2797 }
2798 }
2799
David Benjamince079fd2016-08-02 16:22:34 -04002800 if (!SSL_is_dtls(ssl)) {
David Benjamin8c880a22016-12-03 02:20:34 -05002801 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
Steven Valdeza833c352016-11-01 13:39:36 -04002802 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002803 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002804 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002805 *
2806 * NB: because this code works out the length of all existing extensions
2807 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002808 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002809 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002810 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002811 * Server 7.0 is intolerant to the last extension being zero-length. See
2812 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002813 if (padding_len >= 4 + 1) {
2814 padding_len -= 4;
2815 } else {
2816 padding_len = 1;
2817 }
Adam Langley95c29f32014-06-20 12:00:00 -07002818
Adam Langley33ad2b52015-07-20 17:43:53 -07002819 uint8_t *padding_bytes;
2820 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2821 !CBB_add_u16(&extensions, padding_len) ||
2822 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2823 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002824 }
Adam Langley75712922014-10-10 16:23:43 -07002825
Adam Langley33ad2b52015-07-20 17:43:53 -07002826 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002827 }
2828 }
Adam Langley75712922014-10-10 16:23:43 -07002829
Steven Valdeza833c352016-11-01 13:39:36 -04002830 /* The PSK extension must be last, including after the padding. */
David Benjamin8c880a22016-12-03 02:20:34 -05002831 if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002832 goto err;
2833 }
2834
David Benjamina01deee2015-12-08 18:56:31 -05002835 /* Discard empty extensions blocks. */
2836 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002837 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002838 }
2839
David Benjamine8d53502015-10-10 14:13:23 -04002840 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002841
2842err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002843 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002844 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002845}
Adam Langley95c29f32014-06-20 12:00:00 -07002846
David Benjamin8c880a22016-12-03 02:20:34 -05002847int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
2848 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04002849 CBB extensions;
2850 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002851 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002852 }
2853
David Benjamin8c880a22016-12-03 02:20:34 -05002854 for (unsigned i = 0; i < kNumExtensions; i++) {
2855 if (!(hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002856 /* Don't send extensions that were not received. */
2857 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002858 }
Adam Langley95c29f32014-06-20 12:00:00 -07002859
David Benjamin8c880a22016-12-03 02:20:34 -05002860 if (!kExtensions[i].add_serverhello(hs, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002861 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002862 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002863 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002864 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002865 }
Adam Langley95c29f32014-06-20 12:00:00 -07002866
David Benjamin56380462015-10-10 14:59:09 -04002867 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002868 goto err;
2869 }
2870
Steven Valdez143e8b32016-07-11 13:19:03 -04002871 /* Discard empty extensions blocks before TLS 1.3. */
2872 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2873 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002874 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002875 }
2876
David Benjamin56380462015-10-10 14:59:09 -04002877 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002878
2879err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002880 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002881 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002882}
Adam Langley95c29f32014-06-20 12:00:00 -07002883
David Benjamine14ff062016-08-09 16:21:24 -04002884static int ssl_scan_clienthello_tlsext(
David Benjamin8c880a22016-12-03 02:20:34 -05002885 SSL_HANDSHAKE *hs, const struct ssl_early_callback_ctx *client_hello,
David Benjamine14ff062016-08-09 16:21:24 -04002886 int *out_alert) {
David Benjamin8c880a22016-12-03 02:20:34 -05002887 SSL *const ssl = hs->ssl;
David Benjamin1deb41b2016-08-09 19:36:38 -04002888 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002889 if (kExtensions[i].init != NULL) {
David Benjamin8c880a22016-12-03 02:20:34 -05002890 kExtensions[i].init(hs);
Adam Langley614c66a2015-06-12 15:26:58 -07002891 }
2892 }
2893
David Benjamin8c880a22016-12-03 02:20:34 -05002894 hs->extensions.received = 0;
2895 hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002896
David Benjamine14ff062016-08-09 16:21:24 -04002897 CBS extensions;
2898 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2899 while (CBS_len(&extensions) != 0) {
2900 uint16_t type;
2901 CBS extension;
2902
2903 /* Decode the next extension. */
2904 if (!CBS_get_u16(&extensions, &type) ||
2905 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002906 *out_alert = SSL_AD_DECODE_ERROR;
2907 return 0;
2908 }
Adam Langley95c29f32014-06-20 12:00:00 -07002909
David Benjamine14ff062016-08-09 16:21:24 -04002910 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2911 * ambiguous. Ignore all but the renegotiation_info extension. */
2912 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2913 continue;
2914 }
Adam Langley95c29f32014-06-20 12:00:00 -07002915
David Benjamine14ff062016-08-09 16:21:24 -04002916 unsigned ext_index;
2917 const struct tls_extension *const ext =
2918 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002919
David Benjamine14ff062016-08-09 16:21:24 -04002920 if (ext == NULL) {
2921 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002922 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002923 return 0;
2924 }
David Benjamine14ff062016-08-09 16:21:24 -04002925 continue;
2926 }
2927
David Benjamin8c880a22016-12-03 02:20:34 -05002928 hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002929 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002930 if (!ext->parse_clienthello(hs, &alert, &extension)) {
David Benjamine14ff062016-08-09 16:21:24 -04002931 *out_alert = alert;
2932 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002933 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002934 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002935 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002936 }
Adam Langley75712922014-10-10 16:23:43 -07002937
David Benjamin1deb41b2016-08-09 19:36:38 -04002938 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjamin8c880a22016-12-03 02:20:34 -05002939 if (hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002940 continue;
2941 }
2942
2943 CBS *contents = NULL, fake_contents;
2944 static const uint8_t kFakeRenegotiateExtension[] = {0};
2945 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2946 ssl_client_cipher_list_contains_cipher(client_hello,
2947 SSL3_CK_SCSV & 0xffff)) {
2948 /* The renegotiation SCSV was received so pretend that we received a
2949 * renegotiation extension. */
2950 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2951 sizeof(kFakeRenegotiateExtension));
2952 contents = &fake_contents;
David Benjamin8c880a22016-12-03 02:20:34 -05002953 hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002954 }
2955
2956 /* Extension wasn't observed so call the callback with a NULL
2957 * parameter. */
2958 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05002959 if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002960 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002961 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002962 *out_alert = alert;
2963 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002964 }
2965 }
2966
Adam Langleyfcf25832014-12-18 17:42:32 -08002967 return 1;
2968}
Adam Langley95c29f32014-06-20 12:00:00 -07002969
David Benjamine14ff062016-08-09 16:21:24 -04002970int ssl_parse_clienthello_tlsext(
David Benjamin8c880a22016-12-03 02:20:34 -05002971 SSL_HANDSHAKE *hs, const struct ssl_early_callback_ctx *client_hello) {
2972 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08002973 int alert = -1;
David Benjamin8c880a22016-12-03 02:20:34 -05002974 if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002975 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002976 return 0;
2977 }
Adam Langley95c29f32014-06-20 12:00:00 -07002978
David Benjamin8c880a22016-12-03 02:20:34 -05002979 if (ssl_check_clienthello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002980 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002981 return 0;
2982 }
Adam Langley95c29f32014-06-20 12:00:00 -07002983
Adam Langleyfcf25832014-12-18 17:42:32 -08002984 return 1;
2985}
Adam Langley95c29f32014-06-20 12:00:00 -07002986
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002987OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2988
David Benjamin8c880a22016-12-03 02:20:34 -05002989static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
2990 int *out_alert) {
2991 SSL *const ssl = hs->ssl;
Steven Valdez143e8b32016-07-11 13:19:03 -04002992 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2993 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2994 return 1;
2995 }
Adam Langley614c66a2015-06-12 15:26:58 -07002996
Steven Valdez143e8b32016-07-11 13:19:03 -04002997 /* Decode the extensions block and check it is valid. */
2998 CBS extensions;
2999 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
3000 !tls1_check_duplicate_extensions(&extensions)) {
3001 *out_alert = SSL_AD_DECODE_ERROR;
3002 return 0;
3003 }
3004
3005 uint32_t received = 0;
3006 while (CBS_len(&extensions) != 0) {
3007 uint16_t type;
3008 CBS extension;
3009
3010 /* Decode the next extension. */
3011 if (!CBS_get_u16(&extensions, &type) ||
3012 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003013 *out_alert = SSL_AD_DECODE_ERROR;
3014 return 0;
3015 }
Adam Langley95c29f32014-06-20 12:00:00 -07003016
Steven Valdez143e8b32016-07-11 13:19:03 -04003017 unsigned ext_index;
3018 const struct tls_extension *const ext =
3019 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07003020
Steven Valdez143e8b32016-07-11 13:19:03 -04003021 if (ext == NULL) {
3022 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003023 return 0;
3024 }
Steven Valdez143e8b32016-07-11 13:19:03 -04003025 continue;
3026 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003027
David Benjamin8c880a22016-12-03 02:20:34 -05003028 if (!(hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04003029 type != TLSEXT_TYPE_renegotiate) {
3030 /* If the extension was never sent then it is illegal, except for the
3031 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04003032 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
3033 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04003034 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04003035 return 0;
3036 }
Adam Langley33ad2b52015-07-20 17:43:53 -07003037
Steven Valdez143e8b32016-07-11 13:19:03 -04003038 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07003039
Steven Valdez143e8b32016-07-11 13:19:03 -04003040 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003041 if (!ext->parse_serverhello(hs, &alert, &extension)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04003042 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003043 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04003044 *out_alert = alert;
3045 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07003046 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003047 }
Adam Langley95c29f32014-06-20 12:00:00 -07003048
David Benjamin54091232016-09-05 12:47:25 -04003049 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07003050 if (!(received & (1u << i))) {
3051 /* Extension wasn't observed so call the callback with a NULL
3052 * parameter. */
3053 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin8c880a22016-12-03 02:20:34 -05003054 if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003055 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003056 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07003057 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08003058 return 0;
3059 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003060 }
3061 }
Adam Langley95c29f32014-06-20 12:00:00 -07003062
Adam Langleyfcf25832014-12-18 17:42:32 -08003063 return 1;
3064}
Adam Langley95c29f32014-06-20 12:00:00 -07003065
David Benjamin8c880a22016-12-03 02:20:34 -05003066static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3067 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003068 int ret = SSL_TLSEXT_ERR_NOACK;
3069 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003070
David Benjamin78f8aab2016-03-10 16:33:58 -05003071 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003072 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003073 ssl->ctx->tlsext_servername_arg);
3074 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003075 ret = ssl->initial_ctx->tlsext_servername_callback(
3076 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003077 }
Adam Langley95c29f32014-06-20 12:00:00 -07003078
Adam Langleyfcf25832014-12-18 17:42:32 -08003079 switch (ret) {
3080 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003081 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003082 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003083
Adam Langleyfcf25832014-12-18 17:42:32 -08003084 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003085 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003086 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003087
Adam Langleyfcf25832014-12-18 17:42:32 -08003088 case SSL_TLSEXT_ERR_NOACK:
David Benjamin8c880a22016-12-03 02:20:34 -05003089 hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003090 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003091
Adam Langleyfcf25832014-12-18 17:42:32 -08003092 default:
3093 return 1;
3094 }
3095}
Adam Langleyed8270a2014-09-02 13:52:56 -07003096
David Benjamin8c880a22016-12-03 02:20:34 -05003097static int ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs) {
3098 SSL *const ssl = hs->ssl;
David Benjaminfc059942015-07-30 23:01:59 -04003099 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08003100 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07003101
David Benjamin78f8aab2016-03-10 16:33:58 -05003102 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003103 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003104 ssl->ctx->tlsext_servername_arg);
3105 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003106 ret = ssl->initial_ctx->tlsext_servername_callback(
3107 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003108 }
Adam Langley95c29f32014-06-20 12:00:00 -07003109
Adam Langleyfcf25832014-12-18 17:42:32 -08003110 switch (ret) {
3111 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003112 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003113 return -1;
David Benjamin03973092014-06-24 23:27:17 -04003114
Adam Langleyfcf25832014-12-18 17:42:32 -08003115 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003116 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003117 return 1;
3118
3119 default:
3120 return 1;
3121 }
3122}
3123
David Benjamin8c880a22016-12-03 02:20:34 -05003124int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
3125 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08003126 int alert = -1;
David Benjamin8c880a22016-12-03 02:20:34 -05003127 if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003128 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003129 return 0;
3130 }
3131
David Benjamin8c880a22016-12-03 02:20:34 -05003132 if (ssl_check_serverhello_tlsext(hs) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04003133 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08003134 return 0;
3135 }
3136
3137 return 1;
3138}
Adam Langley95c29f32014-06-20 12:00:00 -07003139
David Benjamine3aa1d92015-06-16 15:34:50 -04003140int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05003141 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04003142 size_t ticket_len, const uint8_t *session_id,
3143 size_t session_id_len) {
3144 int ret = 1; /* Most errors are non-fatal. */
3145 SSL_CTX *ssl_ctx = ssl->initial_ctx;
3146 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003147
David Benjamine3aa1d92015-06-16 15:34:50 -04003148 HMAC_CTX hmac_ctx;
3149 HMAC_CTX_init(&hmac_ctx);
3150 EVP_CIPHER_CTX cipher_ctx;
3151 EVP_CIPHER_CTX_init(&cipher_ctx);
3152
David Benjaminef1b0092015-11-21 14:05:44 -05003153 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04003154 *out_session = NULL;
3155
Steven Valdez4aa154e2016-07-29 14:32:55 -04003156 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
3157 goto done;
3158 }
3159
David Benjamine3aa1d92015-06-16 15:34:50 -04003160 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3161 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003162 }
3163
David Benjaminadcc3952015-04-26 13:07:57 -04003164 /* Ensure there is room for the key name and the largest IV
3165 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3166 * the maximum IV length should be well under the minimum size for the
3167 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003168 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3169 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003170 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003171 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003172
David Benjamine3aa1d92015-06-16 15:34:50 -04003173 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003174 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3175 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3176 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003177 if (cb_ret < 0) {
3178 ret = 0;
3179 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003180 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003181 if (cb_ret == 0) {
3182 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003183 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003184 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003185 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003186 }
3187 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003188 /* Check the key name matches. */
3189 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3190 SSL_TICKET_KEY_NAME_LEN) != 0) {
3191 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003192 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003193 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3194 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003195 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003196 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3197 ssl_ctx->tlsext_tick_aes_key, iv)) {
3198 ret = 0;
3199 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003200 }
3201 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003202 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003203
David Benjamine3aa1d92015-06-16 15:34:50 -04003204 /* Check the MAC at the end of the ticket. */
3205 uint8_t mac[EVP_MAX_MD_SIZE];
3206 size_t mac_len = HMAC_size(&hmac_ctx);
3207 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003208 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003209 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003210 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003211 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3212 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003213 int mac_ok =
3214 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3215#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3216 mac_ok = 1;
3217#endif
3218 if (!mac_ok) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003219 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003220 }
3221
David Benjamine3aa1d92015-06-16 15:34:50 -04003222 /* Decrypt the session data. */
3223 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3224 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3225 mac_len;
3226 plaintext = OPENSSL_malloc(ciphertext_len);
3227 if (plaintext == NULL) {
3228 ret = 0;
3229 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003230 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003231 size_t plaintext_len;
3232#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3233 memcpy(plaintext, ciphertext, ciphertext_len);
3234 plaintext_len = ciphertext_len;
3235#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003236 if (ciphertext_len >= INT_MAX) {
3237 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003238 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003239 int len1, len2;
3240 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3241 (int)ciphertext_len) ||
3242 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
3243 ERR_clear_error(); /* Don't leave an error on the queue. */
3244 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003245 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003246 plaintext_len = (size_t)(len1 + len2);
3247#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003248
David Benjamine3aa1d92015-06-16 15:34:50 -04003249 /* Decode the session. */
David Benjaminfbc45d72016-09-22 01:21:24 -04003250 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003251 if (session == NULL) {
3252 ERR_clear_error(); /* Don't leave an error on the queue. */
3253 goto done;
3254 }
3255
3256 /* Copy the client's session ID into the new session, to denote the ticket has
3257 * been accepted. */
3258 memcpy(session->session_id, session_id, session_id_len);
3259 session->session_id_length = session_id_len;
3260
3261 *out_session = session;
3262
3263done:
3264 OPENSSL_free(plaintext);
3265 HMAC_CTX_cleanup(&hmac_ctx);
3266 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3267 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003268}
Adam Langley95c29f32014-06-20 12:00:00 -07003269
Steven Valdez0d62f262015-09-04 12:41:04 -04003270int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003271 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05003272 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003273 return 1;
3274 }
David Benjamincd996942014-07-20 16:23:51 -04003275
David Benjamin0fc37ef2016-08-17 15:29:46 -04003276 SSL_HANDSHAKE *hs = ssl->s3->hs;
3277 OPENSSL_free(hs->peer_sigalgs);
3278 hs->peer_sigalgs = NULL;
3279 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003280
3281 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003282 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003283 return 0;
3284 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003285 num_sigalgs /= 2;
3286
3287 /* supported_signature_algorithms in the certificate request is
3288 * allowed to be empty. */
3289 if (num_sigalgs == 0) {
3290 return 1;
3291 }
3292
Steven Valdez02563852016-06-23 13:33:05 -04003293 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3294 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003295 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3296 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003297 return 0;
3298 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003299 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003300
3301 CBS sigalgs;
3302 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003303 for (size_t i = 0; i < num_sigalgs; i++) {
3304 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003305 return 0;
3306 }
3307 }
Adam Langley95c29f32014-06-20 12:00:00 -07003308
Adam Langleyfcf25832014-12-18 17:42:32 -08003309 return 1;
3310}
David Benjaminec2f27d2014-11-13 19:17:25 -05003311
David Benjaminea9a0d52016-07-08 15:52:59 -07003312int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003313 CERT *cert = ssl->cert;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003314 SSL_HANDSHAKE *hs = ssl->s3->hs;
David Benjaminec2f27d2014-11-13 19:17:25 -05003315
Steven Valdezf0451ca2016-06-29 13:16:27 -04003316 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3317 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3318 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003319 int type = ssl_private_key_type(ssl);
3320 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003321 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003322 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003323 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003324 if (ssl_is_ecdsa_key_type(type)) {
3325 *out = SSL_SIGN_ECDSA_SHA1;
3326 return 1;
3327 }
3328 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3329 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003330 }
3331
David Benjamin3ef76972016-10-17 17:59:54 -04003332 const uint16_t *sigalgs = cert->sigalgs;
3333 size_t num_sigalgs = cert->num_sigalgs;
3334 if (sigalgs == NULL) {
3335 sigalgs = kSignSignatureAlgorithms;
3336 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003337 }
3338
David Benjamin0fc37ef2016-08-17 15:29:46 -04003339 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3340 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3341 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003342 /* If the client didn't specify any signature_algorithms extension then
3343 * we can assume that it supports SHA1. See
3344 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3345 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3346 SSL_SIGN_ECDSA_SHA1};
3347 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003348 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003349 }
3350
David Benjamin0fc37ef2016-08-17 15:29:46 -04003351 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003352 uint16_t sigalg = sigalgs[i];
3353 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3354 * negotiated. */
3355 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3356 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3357 continue;
3358 }
3359
David Benjamin0fc37ef2016-08-17 15:29:46 -04003360 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003361 if (sigalg == peer_sigalgs[j]) {
3362 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003363 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003364 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003365 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003366 }
Adam Langley95c29f32014-06-20 12:00:00 -07003367
David Benjaminea9a0d52016-07-08 15:52:59 -07003368 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3369 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003370}
Adam Langley95c29f32014-06-20 12:00:00 -07003371
Nick Harper60a85cb2016-09-23 16:25:11 -07003372int tls1_verify_channel_id(SSL *ssl) {
3373 int ret = 0;
3374 uint16_t extension_type;
3375 CBS extension, channel_id;
3376
3377 /* A Channel ID handshake message is structured to contain multiple
3378 * extensions, but the only one that can be present is Channel ID. */
3379 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3380 if (!CBS_get_u16(&channel_id, &extension_type) ||
3381 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3382 CBS_len(&channel_id) != 0 ||
3383 extension_type != TLSEXT_TYPE_channel_id ||
3384 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3385 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3386 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3387 return 0;
3388 }
3389
3390 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3391 if (!p256) {
3392 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3393 return 0;
3394 }
3395
3396 EC_KEY *key = NULL;
3397 EC_POINT *point = NULL;
3398 BIGNUM x, y;
3399 ECDSA_SIG sig;
3400 BN_init(&x);
3401 BN_init(&y);
3402 sig.r = BN_new();
3403 sig.s = BN_new();
3404 if (sig.r == NULL || sig.s == NULL) {
3405 goto err;
3406 }
3407
3408 const uint8_t *p = CBS_data(&extension);
3409 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3410 BN_bin2bn(p + 32, 32, &y) == NULL ||
3411 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3412 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3413 goto err;
3414 }
3415
3416 point = EC_POINT_new(p256);
3417 if (point == NULL ||
3418 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3419 goto err;
3420 }
3421
3422 key = EC_KEY_new();
3423 if (key == NULL ||
3424 !EC_KEY_set_group(key, p256) ||
3425 !EC_KEY_set_public_key(key, point)) {
3426 goto err;
3427 }
3428
3429 uint8_t digest[EVP_MAX_MD_SIZE];
3430 size_t digest_len;
3431 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3432 goto err;
3433 }
3434
3435 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3436#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3437 sig_ok = 1;
3438#endif
3439 if (!sig_ok) {
3440 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3441 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3442 ssl->s3->tlsext_channel_id_valid = 0;
3443 goto err;
3444 }
3445
3446 memcpy(ssl->s3->tlsext_channel_id, p, 64);
3447 ret = 1;
3448
3449err:
3450 BN_free(&x);
3451 BN_free(&y);
3452 BN_free(sig.r);
3453 BN_free(sig.s);
3454 EC_KEY_free(key);
3455 EC_POINT_free(point);
3456 EC_GROUP_free(p256);
3457 return ret;
3458}
3459
3460int tls1_write_channel_id(SSL *ssl, CBB *cbb) {
3461 uint8_t digest[EVP_MAX_MD_SIZE];
3462 size_t digest_len;
3463 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3464 return 0;
3465 }
3466
3467 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3468 if (ec_key == NULL) {
3469 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3470 return 0;
3471 }
3472
3473 int ret = 0;
3474 BIGNUM *x = BN_new();
3475 BIGNUM *y = BN_new();
3476 ECDSA_SIG *sig = NULL;
3477 if (x == NULL || y == NULL ||
3478 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3479 EC_KEY_get0_public_key(ec_key),
3480 x, y, NULL)) {
3481 goto err;
3482 }
3483
3484 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3485 if (sig == NULL) {
3486 goto err;
3487 }
3488
3489 CBB child;
3490 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3491 !CBB_add_u16_length_prefixed(cbb, &child) ||
3492 !BN_bn2cbb_padded(&child, 32, x) ||
3493 !BN_bn2cbb_padded(&child, 32, y) ||
3494 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3495 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3496 !CBB_flush(cbb)) {
3497 goto err;
3498 }
3499
3500 ret = 1;
3501
3502err:
3503 BN_free(x);
3504 BN_free(y);
3505 ECDSA_SIG_free(sig);
3506 return ret;
3507}
3508
David Benjamind6a4ae92015-08-06 11:10:51 -04003509int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003510 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3511 uint8_t *msg;
3512 size_t msg_len;
3513 if (!tls13_get_cert_verify_signature_input(ssl, &msg, &msg_len,
3514 ssl_cert_verify_channel_id)) {
3515 return 0;
3516 }
3517 SHA256(msg, msg_len, out);
3518 *out_len = SHA256_DIGEST_LENGTH;
3519 OPENSSL_free(msg);
3520 return 1;
3521 }
3522
Nick Harper95594012016-10-20 14:07:13 -07003523 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003524
Nick Harper95594012016-10-20 14:07:13 -07003525 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003526 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003527 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003528
Steven Valdez87eab492016-06-27 16:34:59 -04003529 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003530 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003531 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003532 if (ssl->session->original_handshake_hash_len == 0) {
3533 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003534 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003535 }
Nick Harper95594012016-10-20 14:07:13 -07003536 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3537 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003538 }
3539
3540 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3541 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3542 sizeof(handshake_hash));
3543 if (handshake_hash_len < 0) {
Nick Harper95594012016-10-20 14:07:13 -07003544 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003545 }
Nick Harper95594012016-10-20 14:07:13 -07003546 SHA256_Update(&ctx, handshake_hash, (size_t)handshake_hash_len);
3547 SHA256_Final(out, &ctx);
3548 *out_len = SHA256_DIGEST_LENGTH;
3549 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003550}
Adam Langley1258b6a2014-06-20 12:00:00 -07003551
3552/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003553 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003554 * data. */
3555int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003556 int digest_len;
3557 /* This function should never be called for a resumed session because the
3558 * handshake hashes that we wish to record are for the original, full
3559 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003560 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003561 return -1;
3562 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003563
Adam Langleyfcf25832014-12-18 17:42:32 -08003564 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003565 tls1_handshake_digest(
3566 ssl, ssl->s3->new_session->original_handshake_hash,
3567 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003568 if (digest_len < 0) {
3569 return -1;
3570 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003571
Steven Valdez87eab492016-06-27 16:34:59 -04003572 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003573
Adam Langleyfcf25832014-12-18 17:42:32 -08003574 return 1;
3575}
Nick Harper60a85cb2016-09-23 16:25:11 -07003576
3577int ssl_do_channel_id_callback(SSL *ssl) {
3578 if (ssl->tlsext_channel_id_private != NULL ||
3579 ssl->ctx->channel_id_cb == NULL) {
3580 return 1;
3581 }
3582
3583 EVP_PKEY *key = NULL;
3584 ssl->ctx->channel_id_cb(ssl, &key);
3585 if (key == NULL) {
3586 /* The caller should try again later. */
3587 return 1;
3588 }
3589
3590 int ret = SSL_set1_tls_channel_id(ssl, key);
3591 EVP_PKEY_free(key);
3592 return ret;
3593}
Adam Langleycfa08c32016-11-17 13:21:27 -08003594
3595int ssl_is_sct_list_valid(const CBS *contents) {
3596 /* Shallow parse the SCT list for sanity. By the RFC
3597 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3598 * of the SCTs may be empty. */
3599 CBS copy = *contents;
3600 CBS sct_list;
3601 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3602 CBS_len(&copy) != 0 ||
3603 CBS_len(&sct_list) == 0) {
3604 return 0;
3605 }
3606
3607 while (CBS_len(&sct_list) > 0) {
3608 CBS sct;
3609 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3610 CBS_len(&sct) == 0) {
3611 return 0;
3612 }
3613 }
3614
3615 return 1;
3616}