blob: c9f5bbfe99fb4079967a5dcdaae57f76f9eedee3 [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 Benjamin0d56f882015-12-19 17:05:56 -0500130static int ssl_check_clienthello_tlsext(SSL *ssl);
131static int ssl_check_serverhello_tlsext(SSL *ssl);
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
Adam Langley95c29f32014-06-20 12:00:00 -0700444/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700446
Steven Valdez02563852016-06-23 13:33:05 -0400447static const uint16_t kDefaultSignatureAlgorithms[] = {
David Benjamin57e929f2016-08-30 00:30:38 -0400448 /* For now, do not ship RSA-PSS signature algorithms on Android's system
449 * BoringSSL. Once TLS 1.3 is finalized and the change in Chrome has stuck,
450 * restore them. */
451#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400452 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400453#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400454 SSL_SIGN_RSA_PKCS1_SHA512,
455 SSL_SIGN_ECDSA_SECP521R1_SHA512,
456
David Benjamin57e929f2016-08-30 00:30:38 -0400457#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400458 SSL_SIGN_RSA_PSS_SHA384,
David Benjamin57e929f2016-08-30 00:30:38 -0400459#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400460 SSL_SIGN_RSA_PKCS1_SHA384,
461 SSL_SIGN_ECDSA_SECP384R1_SHA384,
462
David Benjamin57e929f2016-08-30 00:30:38 -0400463#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400464 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400465#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400466 SSL_SIGN_RSA_PKCS1_SHA256,
467 SSL_SIGN_ECDSA_SECP256R1_SHA256,
468
469 SSL_SIGN_RSA_PKCS1_SHA1,
470 SSL_SIGN_ECDSA_SHA1,
471};
472
Steven Valdez02563852016-06-23 13:33:05 -0400473size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
474 *psigs = kDefaultSignatureAlgorithms;
Steven Valdezcb966542016-08-17 16:56:14 -0400475 return OPENSSL_ARRAY_SIZE(kDefaultSignatureAlgorithms);
Adam Langleyfcf25832014-12-18 17:42:32 -0800476}
Adam Langley95c29f32014-06-20 12:00:00 -0700477
David Benjamin887c3002016-07-08 16:15:32 -0700478int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
Steven Valdez02563852016-06-23 13:33:05 -0400479 const uint16_t *sent_sigs;
480 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800481
Adam Langleyfcf25832014-12-18 17:42:32 -0800482 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500483 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400484 for (i = 0; i < sent_sigslen; i++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400485 if (sigalg == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800486 break;
487 }
488 }
489
David Benjamin788be4a2015-10-30 17:50:57 -0400490 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400491 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800492 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
493 return 0;
494 }
495
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 return 1;
497}
498
499/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
500 * supported or doesn't appear in supported signature algorithms. Unlike
501 * ssl_cipher_get_disabled this applies to a specific session and not global
502 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500503void ssl_set_client_disabled(SSL *ssl) {
504 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800505 int have_rsa = 0, have_ecdsa = 0;
506 c->mask_a = 0;
507 c->mask_k = 0;
508
Adam Langleyfcf25832014-12-18 17:42:32 -0800509 /* Now go through all signature algorithms seeing if we support any for RSA,
510 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin0fc37ef2016-08-17 15:29:46 -0400511 const uint16_t *sigalgs;
512 size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
513 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -0400514 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400515 case SSL_SIGN_RSA_PSS_SHA512:
516 case SSL_SIGN_RSA_PSS_SHA384:
517 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400518 case SSL_SIGN_RSA_PKCS1_SHA512:
519 case SSL_SIGN_RSA_PKCS1_SHA384:
520 case SSL_SIGN_RSA_PKCS1_SHA256:
521 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 have_rsa = 1;
523 break;
524
Steven Valdez02563852016-06-23 13:33:05 -0400525 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
526 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
527 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
528 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800529 have_ecdsa = 1;
530 break;
531 }
532 }
533
534 /* Disable auth if we don't include any appropriate signature algorithms. */
535 if (!have_rsa) {
536 c->mask_a |= SSL_aRSA;
537 }
538 if (!have_ecdsa) {
539 c->mask_a |= SSL_aECDSA;
540 }
541
542 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500543 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800544 c->mask_a |= SSL_aPSK;
545 c->mask_k |= SSL_kPSK;
546 }
547}
Adam Langley95c29f32014-06-20 12:00:00 -0700548
Adam Langley614c66a2015-06-12 15:26:58 -0700549/* tls_extension represents a TLS extension that is handled internally. The
550 * |init| function is called for each handshake, before any other functions of
551 * the extension. Then the add and parse callbacks are called as needed.
552 *
553 * The parse callbacks receive a |CBS| that contains the contents of the
554 * extension (i.e. not including the type and length bytes). If an extension is
555 * not received then the parse callbacks will be called with a NULL CBS so that
556 * they can do any processing needed to handle the absence of an extension.
557 *
558 * The add callbacks receive a |CBB| to which the extension can be appended but
559 * the function is responsible for appending the type and length bytes too.
560 *
561 * All callbacks return one for success and zero for error. If a parse function
562 * returns zero then a fatal alert with value |*out_alert| will be sent. If
563 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
564struct tls_extension {
565 uint16_t value;
566 void (*init)(SSL *ssl);
567
568 int (*add_clienthello)(SSL *ssl, CBB *out);
569 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
570
571 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
572 int (*add_serverhello)(SSL *ssl, CBB *out);
573};
574
Steven Valdez6b8509a2016-07-12 13:38:32 -0400575static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
576 if (contents != NULL) {
577 /* Servers MUST NOT send this extension. */
578 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
579 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
580 return 0;
581 }
582
583 return 1;
584}
585
586static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
587 /* This extension from the client is handled elsewhere. */
588 return 1;
589}
590
591static int dont_add_serverhello(SSL *ssl, CBB *out) {
592 return 1;
593}
Adam Langley614c66a2015-06-12 15:26:58 -0700594
595/* Server name indication (SNI).
596 *
597 * https://tools.ietf.org/html/rfc6066#section-3. */
598
Adam Langley614c66a2015-06-12 15:26:58 -0700599static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
600 if (ssl->tlsext_hostname == NULL) {
601 return 1;
602 }
603
604 CBB contents, server_name_list, name;
605 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
606 !CBB_add_u16_length_prefixed(out, &contents) ||
607 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
608 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
609 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
610 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
611 strlen(ssl->tlsext_hostname)) ||
612 !CBB_flush(out)) {
613 return 0;
614 }
615
616 return 1;
617}
618
David Benjamin0d56f882015-12-19 17:05:56 -0500619static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
620 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700621 if (contents == NULL) {
622 return 1;
623 }
624
625 if (CBS_len(contents) != 0) {
626 return 0;
627 }
628
629 assert(ssl->tlsext_hostname != NULL);
630
Steven Valdez87eab492016-06-27 16:34:59 -0400631 if (ssl->session == NULL) {
632 assert(ssl->s3->new_session->tlsext_hostname == NULL);
633 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
634 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700635 *out_alert = SSL_AD_INTERNAL_ERROR;
636 return 0;
637 }
638 }
639
640 return 1;
641}
642
David Benjamin0d56f882015-12-19 17:05:56 -0500643static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
644 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700645 if (contents == NULL) {
646 return 1;
647 }
648
David Benjamin9b611e22016-03-03 08:48:30 -0500649 CBS server_name_list, host_name;
650 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700651 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500652 !CBS_get_u8(&server_name_list, &name_type) ||
653 /* Although the server_name extension was intended to be extensible to
654 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
655 * different name types will cause an error. Further, RFC 4366 originally
656 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
657 * adding new name types is no longer feasible.
658 *
659 * Act as if the extensibility does not exist to simplify parsing. */
660 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
661 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700662 CBS_len(contents) != 0) {
663 return 0;
664 }
665
David Benjamin9b611e22016-03-03 08:48:30 -0500666 if (name_type != TLSEXT_NAMETYPE_host_name ||
667 CBS_len(&host_name) == 0 ||
668 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
669 CBS_contains_zero_byte(&host_name)) {
670 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
671 return 0;
672 }
Adam Langley614c66a2015-06-12 15:26:58 -0700673
David Benjamin9b611e22016-03-03 08:48:30 -0500674 /* TODO(davidben): SNI should be resolved before resumption. We have the
675 * early callback as a replacement, but we should fix the current callback
676 * and avoid the need for |SSL_CTX_set_session_id_context|. */
Steven Valdez87eab492016-06-27 16:34:59 -0400677 if (ssl->session == NULL) {
678 assert(ssl->s3->new_session->tlsext_hostname == NULL);
David Benjamin9b611e22016-03-03 08:48:30 -0500679
680 /* Copy the hostname as a string. */
Steven Valdez87eab492016-06-27 16:34:59 -0400681 if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) {
David Benjamin9b611e22016-03-03 08:48:30 -0500682 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700683 return 0;
684 }
685
David Benjamina0486782016-10-06 19:11:32 -0400686 ssl->s3->hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700687 }
688
689 return 1;
690}
691
692static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez87eab492016-06-27 16:34:59 -0400693 if (ssl->session != NULL ||
David Benjamina0486782016-10-06 19:11:32 -0400694 !ssl->s3->hs->should_ack_sni ||
Steven Valdez87eab492016-06-27 16:34:59 -0400695 ssl->s3->new_session->tlsext_hostname == NULL) {
Adam Langley614c66a2015-06-12 15:26:58 -0700696 return 1;
697 }
698
699 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
700 !CBB_add_u16(out, 0 /* length */)) {
701 return 0;
702 }
703
704 return 1;
705}
706
707
Adam Langley5021b222015-06-12 18:27:58 -0700708/* Renegotiation indication.
709 *
710 * https://tools.ietf.org/html/rfc5746 */
711
712static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400713 uint16_t min_version, max_version;
714 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
715 return 0;
716 }
717
718 /* Renegotiation indication is not necessary in TLS 1.3. */
719 if (min_version >= TLS1_3_VERSION) {
720 return 1;
721 }
722
David Benjamin52bf6902016-10-08 12:05:03 -0400723 assert(ssl->s3->initial_handshake_complete ==
724 (ssl->s3->previous_client_finished_len != 0));
725
Adam Langley5021b222015-06-12 18:27:58 -0700726 CBB contents, prev_finished;
727 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
728 !CBB_add_u16_length_prefixed(out, &contents) ||
729 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
730 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
731 ssl->s3->previous_client_finished_len) ||
732 !CBB_flush(out)) {
733 return 0;
734 }
735
736 return 1;
737}
738
739static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
740 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400741 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
742 return 0;
743 }
744
David Benjamin3e052de2015-11-25 20:10:31 -0500745 /* Servers may not switch between omitting the extension and supporting it.
746 * See RFC 5746, sections 3.5 and 4.2. */
747 if (ssl->s3->initial_handshake_complete &&
748 (contents != NULL) != ssl->s3->send_connection_binding) {
749 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
750 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
751 return 0;
752 }
753
Adam Langley5021b222015-06-12 18:27:58 -0700754 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500755 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700756 * RI even on initial ServerHello because the client doesn't see any
757 * renegotiation during an attack. However this would mean we could not
758 * connect to any server which doesn't support RI.
759 *
David Benjamine9cddb82015-11-23 14:36:40 -0500760 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
761 * practical terms every client sets it so it's just assumed here. */
762 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700763 }
764
765 const size_t expected_len = ssl->s3->previous_client_finished_len +
766 ssl->s3->previous_server_finished_len;
767
768 /* Check for logic errors */
769 assert(!expected_len || ssl->s3->previous_client_finished_len);
770 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400771 assert(ssl->s3->initial_handshake_complete ==
772 (ssl->s3->previous_client_finished_len != 0));
773 assert(ssl->s3->initial_handshake_complete ==
774 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700775
776 /* Parse out the extension contents. */
777 CBS renegotiated_connection;
778 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
779 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400780 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700781 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
782 return 0;
783 }
784
785 /* Check that the extension matches. */
786 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400787 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700788 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
789 return 0;
790 }
791
792 const uint8_t *d = CBS_data(&renegotiated_connection);
793 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
794 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400795 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700796 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
797 return 0;
798 }
799 d += ssl->s3->previous_client_finished_len;
800
801 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
802 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400803 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700804 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
805 return 0;
806 }
807 ssl->s3->send_connection_binding = 1;
808
809 return 1;
810}
811
812static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
813 CBS *contents) {
814 /* Renegotiation isn't supported as a server so this function should never be
815 * called after the initial handshake. */
816 assert(!ssl->s3->initial_handshake_complete);
817
Steven Valdez143e8b32016-07-11 13:19:03 -0400818 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
819 return 1;
820 }
821
Adam Langley5021b222015-06-12 18:27:58 -0700822 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400823 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700824 }
825
826 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700827 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
828 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400829 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700830 return 0;
831 }
832
David Benjamin52bf6902016-10-08 12:05:03 -0400833 /* Check that the extension matches. We do not support renegotiation as a
834 * server, so this must be empty. */
835 if (CBS_len(&renegotiated_connection) != 0) {
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_HANDSHAKE_FAILURE;
838 return 0;
839 }
840
841 ssl->s3->send_connection_binding = 1;
842
843 return 1;
844}
845
846static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin52bf6902016-10-08 12:05:03 -0400847 /* Renegotiation isn't supported as a server so this function should never be
848 * called after the initial handshake. */
849 assert(!ssl->s3->initial_handshake_complete);
850
Steven Valdez143e8b32016-07-11 13:19:03 -0400851 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
852 return 1;
853 }
854
Adam Langley5021b222015-06-12 18:27:58 -0700855 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400856 !CBB_add_u16(out, 1 /* length */) ||
857 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700858 return 0;
859 }
860
861 return 1;
862}
863
Adam Langley0a056712015-07-01 15:03:33 -0700864
865/* Extended Master Secret.
866 *
David Benjamin43946d42016-02-01 08:42:19 -0500867 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700868
Adam Langley0a056712015-07-01 15:03:33 -0700869static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400870 uint16_t min_version, max_version;
871 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
872 return 0;
873 }
874
875 /* Extended master secret is not necessary in TLS 1.3. */
876 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700877 return 1;
878 }
879
880 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
881 !CBB_add_u16(out, 0 /* length */)) {
882 return 0;
883 }
884
885 return 1;
886}
887
888static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
889 CBS *contents) {
David Benjamin163c9562016-08-29 23:14:17 -0400890 /* Whether EMS is negotiated may not change on renegotation. */
891 if (ssl->s3->initial_handshake_complete) {
892 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
893 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
894 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
895 return 0;
896 }
897
898 return 1;
899 }
900
Adam Langley0a056712015-07-01 15:03:33 -0700901 if (contents == NULL) {
902 return 1;
903 }
904
Steven Valdez143e8b32016-07-11 13:19:03 -0400905 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
906 ssl->version == SSL3_VERSION) {
907 return 0;
908 }
909
910 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700911 return 0;
912 }
913
914 ssl->s3->tmp.extended_master_secret = 1;
915 return 1;
916}
917
David Benjamin0d56f882015-12-19 17:05:56 -0500918static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
919 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400920 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
921 ssl->version == SSL3_VERSION) {
922 return 1;
923 }
924
925 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700926 return 1;
927 }
928
929 if (CBS_len(contents) != 0) {
930 return 0;
931 }
932
933 ssl->s3->tmp.extended_master_secret = 1;
934 return 1;
935}
936
937static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
938 if (!ssl->s3->tmp.extended_master_secret) {
939 return 1;
940 }
941
942 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
943 !CBB_add_u16(out, 0 /* length */)) {
944 return 0;
945 }
946
947 return 1;
948}
949
Adam Langley9b05bc52015-07-01 15:25:33 -0700950
951/* Session tickets.
952 *
953 * https://tools.ietf.org/html/rfc5077 */
954
955static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400956 uint16_t min_version, max_version;
957 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
958 return 0;
959 }
960
961 /* TLS 1.3 uses a different ticket extension. */
962 if (min_version >= TLS1_3_VERSION ||
963 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700964 return 1;
965 }
966
967 const uint8_t *ticket_data = NULL;
968 int ticket_len = 0;
969
970 /* Renegotiation does not participate in session resumption. However, still
971 * advertise the extension to avoid potentially breaking servers which carry
972 * over the state from the previous handshake, such as OpenSSL servers
973 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -0400974 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -0700975 if (!ssl->s3->initial_handshake_complete &&
976 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400977 ssl->session->tlsext_tick != NULL &&
978 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -0400979 ssl->method->version_from_wire(&session_version,
980 ssl->session->ssl_version) &&
981 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700982 ticket_data = ssl->session->tlsext_tick;
983 ticket_len = ssl->session->tlsext_ticklen;
984 }
985
986 CBB ticket;
987 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
988 !CBB_add_u16_length_prefixed(out, &ticket) ||
989 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
990 !CBB_flush(out)) {
991 return 0;
992 }
993
994 return 1;
995}
996
997static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
998 CBS *contents) {
999 ssl->tlsext_ticket_expected = 0;
1000
1001 if (contents == NULL) {
1002 return 1;
1003 }
1004
Steven Valdez143e8b32016-07-11 13:19:03 -04001005 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1006 return 0;
1007 }
1008
Adam Langley9b05bc52015-07-01 15:25:33 -07001009 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1010 * this function should never be called, even if the server tries to send the
1011 * extension. */
1012 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1013
1014 if (CBS_len(contents) != 0) {
1015 return 0;
1016 }
1017
1018 ssl->tlsext_ticket_expected = 1;
1019 return 1;
1020}
1021
Adam Langley9b05bc52015-07-01 15:25:33 -07001022static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1023 if (!ssl->tlsext_ticket_expected) {
1024 return 1;
1025 }
1026
1027 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1028 * true. */
1029 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1030
1031 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1032 !CBB_add_u16(out, 0 /* length */)) {
1033 return 0;
1034 }
1035
1036 return 1;
1037}
1038
1039
Adam Langley2e857bd2015-07-01 16:09:19 -07001040/* Signature Algorithms.
1041 *
1042 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1043
1044static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin2dc02042016-09-19 19:57:37 -04001045 uint16_t min_version, max_version;
1046 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1047 return 0;
1048 }
1049
1050 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001051 return 1;
1052 }
1053
David Benjamin0fc37ef2016-08-17 15:29:46 -04001054 const uint16_t *sigalgs;
1055 const size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001056
David Benjamin0fc37ef2016-08-17 15:29:46 -04001057 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001058 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1059 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001060 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001061 return 0;
1062 }
1063
David Benjamin0fc37ef2016-08-17 15:29:46 -04001064 for (size_t i = 0; i < num_sigalgs; i++) {
1065 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001066 return 0;
1067 }
1068 }
1069
1070 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001071 return 0;
1072 }
1073
1074 return 1;
1075}
1076
Adam Langley2e857bd2015-07-01 16:09:19 -07001077static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1078 CBS *contents) {
David Benjamin0fc37ef2016-08-17 15:29:46 -04001079 OPENSSL_free(ssl->s3->hs->peer_sigalgs);
1080 ssl->s3->hs->peer_sigalgs = NULL;
1081 ssl->s3->hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001082
Adam Langley2e857bd2015-07-01 16:09:19 -07001083 if (contents == NULL) {
1084 return 1;
1085 }
1086
1087 CBS supported_signature_algorithms;
1088 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001089 CBS_len(contents) != 0 ||
1090 CBS_len(&supported_signature_algorithms) == 0 ||
1091 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001092 return 0;
1093 }
1094
1095 return 1;
1096}
1097
Adam Langley2e857bd2015-07-01 16:09:19 -07001098
Adam Langleybb0bd042015-07-01 16:21:03 -07001099/* OCSP Stapling.
1100 *
1101 * https://tools.ietf.org/html/rfc6066#section-8 */
1102
1103static void ext_ocsp_init(SSL *ssl) {
Adam Langleyce9d85e2016-01-24 15:58:39 -08001104 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001105}
1106
1107static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1108 if (!ssl->ocsp_stapling_enabled) {
1109 return 1;
1110 }
1111
1112 CBB contents;
1113 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1114 !CBB_add_u16_length_prefixed(out, &contents) ||
1115 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1116 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1117 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1118 !CBB_flush(out)) {
1119 return 0;
1120 }
1121
Adam Langleyce9d85e2016-01-24 15:58:39 -08001122 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001123 return 1;
1124}
1125
1126static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001127 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001128 if (contents == NULL) {
1129 return 1;
1130 }
1131
David Benjamin942f4ed2016-07-16 19:03:49 +03001132 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001133 /* OCSP stapling is forbidden on non-certificate ciphers. */
1134 if (CBS_len(contents) != 0 ||
1135 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001136 return 0;
1137 }
1138
David Benjamindaa88502016-10-04 16:32:16 -04001139 /* Note this does not check for resumption in TLS 1.2. Sending
1140 * status_request here does not make sense, but OpenSSL does so and the
1141 * specification does not say anything. Tolerate it but ignore it. */
1142
David Benjamina0486782016-10-06 19:11:32 -04001143 ssl->s3->hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001144 return 1;
1145 }
1146
Steven Valdez803c77a2016-09-06 14:13:43 -04001147 /* In TLS 1.3, OCSP stapling is forbidden on resumption. */
1148 if (ssl->s3->session_reused) {
1149 return 0;
1150 }
1151
David Benjamin942f4ed2016-07-16 19:03:49 +03001152 uint8_t status_type;
1153 CBS ocsp_response;
1154 if (!CBS_get_u8(contents, &status_type) ||
1155 status_type != TLSEXT_STATUSTYPE_ocsp ||
1156 !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1157 CBS_len(&ocsp_response) == 0 ||
1158 CBS_len(contents) != 0) {
1159 return 0;
1160 }
1161
Steven Valdez87eab492016-06-27 16:34:59 -04001162 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1163 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001164 *out_alert = SSL_AD_INTERNAL_ERROR;
1165 return 0;
1166 }
1167
Adam Langleybb0bd042015-07-01 16:21:03 -07001168 return 1;
1169}
1170
1171static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1172 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001173 if (contents == NULL) {
1174 return 1;
1175 }
1176
1177 uint8_t status_type;
1178 if (!CBS_get_u8(contents, &status_type)) {
1179 return 0;
1180 }
1181
1182 /* We cannot decide whether OCSP stapling will occur yet because the correct
1183 * SSL_CTX might not have been selected. */
David Benjamina0486782016-10-06 19:11:32 -04001184 ssl->s3->hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001185
Adam Langleybb0bd042015-07-01 16:21:03 -07001186 return 1;
1187}
1188
1189static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
David Benjamina0486782016-10-06 19:11:32 -04001190 if (!ssl->s3->hs->ocsp_stapling_requested ||
David Benjamin942f4ed2016-07-16 19:03:49 +03001191 ssl->ctx->ocsp_response_length == 0 ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001192 ssl->s3->session_reused ||
1193 (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
1194 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001195 return 1;
1196 }
1197
David Benjamin942f4ed2016-07-16 19:03:49 +03001198 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1199 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez87eab492016-06-27 16:34:59 -04001200 if (ssl->session != NULL) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001201 return 1;
1202 }
1203
David Benjamina0486782016-10-06 19:11:32 -04001204 ssl->s3->hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001205
1206 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1207 CBB_add_u16(out, 0 /* length */);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001208 }
1209
David Benjamin942f4ed2016-07-16 19:03:49 +03001210 CBB body, ocsp_response;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001211 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
David Benjamin942f4ed2016-07-16 19:03:49 +03001212 CBB_add_u16_length_prefixed(out, &body) &&
1213 CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1214 CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1215 CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1216 ssl->ctx->ocsp_response_length) &&
1217 CBB_flush(out);
Adam Langleybb0bd042015-07-01 16:21:03 -07001218}
1219
1220
Adam Langley97dfcbf2015-07-01 18:35:20 -07001221/* Next protocol negotiation.
1222 *
1223 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1224
Adam Langley97dfcbf2015-07-01 18:35:20 -07001225static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1226 if (ssl->s3->initial_handshake_complete ||
1227 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001228 (ssl->options & SSL_OP_DISABLE_NPN) ||
David Benjamince079fd2016-08-02 16:22:34 -04001229 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001230 return 1;
1231 }
1232
1233 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1234 !CBB_add_u16(out, 0 /* length */)) {
1235 return 0;
1236 }
1237
1238 return 1;
1239}
1240
1241static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1242 CBS *contents) {
1243 if (contents == NULL) {
1244 return 1;
1245 }
1246
Steven Valdez143e8b32016-07-11 13:19:03 -04001247 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1248 return 0;
1249 }
1250
Adam Langley97dfcbf2015-07-01 18:35:20 -07001251 /* If any of these are false then we should never have sent the NPN
1252 * extension in the ClientHello and thus this function should never have been
1253 * called. */
1254 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001255 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001256 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001257 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001258
David Benjamin76c2efc2015-08-31 14:24:29 -04001259 if (ssl->s3->alpn_selected != NULL) {
1260 /* NPN and ALPN may not be negotiated in the same connection. */
1261 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1262 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1263 return 0;
1264 }
1265
Adam Langley97dfcbf2015-07-01 18:35:20 -07001266 const uint8_t *const orig_contents = CBS_data(contents);
1267 const size_t orig_len = CBS_len(contents);
1268
1269 while (CBS_len(contents) != 0) {
1270 CBS proto;
1271 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1272 CBS_len(&proto) == 0) {
1273 return 0;
1274 }
1275 }
1276
1277 uint8_t *selected;
1278 uint8_t selected_len;
1279 if (ssl->ctx->next_proto_select_cb(
1280 ssl, &selected, &selected_len, orig_contents, orig_len,
1281 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1282 *out_alert = SSL_AD_INTERNAL_ERROR;
1283 return 0;
1284 }
1285
David Benjamin79978df2015-12-25 15:56:49 -05001286 OPENSSL_free(ssl->s3->next_proto_negotiated);
1287 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1288 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001289 *out_alert = SSL_AD_INTERNAL_ERROR;
1290 return 0;
1291 }
1292
David Benjamin79978df2015-12-25 15:56:49 -05001293 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjaminb74b0812016-10-06 19:43:48 -04001294 ssl->s3->hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001295
1296 return 1;
1297}
1298
1299static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1300 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001301 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1302 return 1;
1303 }
1304
Adam Langley97dfcbf2015-07-01 18:35:20 -07001305 if (contents != NULL && CBS_len(contents) != 0) {
1306 return 0;
1307 }
1308
1309 if (contents == NULL ||
1310 ssl->s3->initial_handshake_complete ||
1311 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1312 * afterwards, parsing the ALPN extension will clear
1313 * |next_proto_neg_seen|. */
1314 ssl->s3->alpn_selected != NULL ||
1315 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001316 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001317 return 1;
1318 }
1319
David Benjaminb74b0812016-10-06 19:43:48 -04001320 ssl->s3->hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001321 return 1;
1322}
1323
1324static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1325 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1326 * parsed. */
David Benjaminb74b0812016-10-06 19:43:48 -04001327 if (!ssl->s3->hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001328 return 1;
1329 }
1330
1331 const uint8_t *npa;
1332 unsigned npa_len;
1333
1334 if (ssl->ctx->next_protos_advertised_cb(
1335 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1336 SSL_TLSEXT_ERR_OK) {
David Benjaminb74b0812016-10-06 19:43:48 -04001337 ssl->s3->hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001338 return 1;
1339 }
1340
1341 CBB contents;
1342 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1343 !CBB_add_u16_length_prefixed(out, &contents) ||
1344 !CBB_add_bytes(&contents, npa, npa_len) ||
1345 !CBB_flush(out)) {
1346 return 0;
1347 }
1348
1349 return 1;
1350}
1351
1352
Adam Langleyab8d87d2015-07-10 12:21:39 -07001353/* Signed certificate timestamps.
1354 *
1355 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1356
1357static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1358 if (!ssl->signed_cert_timestamps_enabled) {
1359 return 1;
1360 }
1361
1362 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1363 !CBB_add_u16(out, 0 /* length */)) {
1364 return 0;
1365 }
1366
1367 return 1;
1368}
1369
1370static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1371 CBS *contents) {
1372 if (contents == NULL) {
1373 return 1;
1374 }
1375
1376 /* If this is false then we should never have sent the SCT extension in the
1377 * ClientHello and thus this function should never have been called. */
1378 assert(ssl->signed_cert_timestamps_enabled);
1379
1380 if (CBS_len(contents) == 0) {
1381 *out_alert = SSL_AD_DECODE_ERROR;
1382 return 0;
1383 }
1384
David Benjamindaa88502016-10-04 16:32:16 -04001385 /* Session resumption uses the original session information. The extension
1386 * should not be sent on resumption, but RFC 6962 did not make it a
1387 * requirement, so tolerate this.
1388 *
1389 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001390 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001391 !CBS_stow(
1392 contents,
1393 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1394 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001395 *out_alert = SSL_AD_INTERNAL_ERROR;
1396 return 0;
1397 }
1398
1399 return 1;
1400}
1401
1402static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1403 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001404 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001405}
1406
1407static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001408 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001409 if (ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001410 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001411 return 1;
1412 }
1413
1414 CBB contents;
1415 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1416 CBB_add_u16_length_prefixed(out, &contents) &&
1417 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1418 ssl->ctx->signed_cert_timestamp_list_length) &&
1419 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001420}
1421
1422
Adam Langleyf18e4532015-07-10 13:39:53 -07001423/* Application-level Protocol Negotiation.
1424 *
1425 * https://tools.ietf.org/html/rfc7301 */
1426
1427static void ext_alpn_init(SSL *ssl) {
1428 OPENSSL_free(ssl->s3->alpn_selected);
1429 ssl->s3->alpn_selected = NULL;
1430}
1431
1432static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1433 if (ssl->alpn_client_proto_list == NULL ||
1434 ssl->s3->initial_handshake_complete) {
1435 return 1;
1436 }
1437
1438 CBB contents, proto_list;
1439 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1440 !CBB_add_u16_length_prefixed(out, &contents) ||
1441 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1442 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1443 ssl->alpn_client_proto_list_len) ||
1444 !CBB_flush(out)) {
1445 return 0;
1446 }
1447
1448 return 1;
1449}
1450
1451static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1452 CBS *contents) {
1453 if (contents == NULL) {
1454 return 1;
1455 }
1456
1457 assert(!ssl->s3->initial_handshake_complete);
1458 assert(ssl->alpn_client_proto_list != NULL);
1459
David Benjaminb74b0812016-10-06 19:43:48 -04001460 if (ssl->s3->hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001461 /* NPN and ALPN may not be negotiated in the same connection. */
1462 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1463 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1464 return 0;
1465 }
1466
Adam Langleyf18e4532015-07-10 13:39:53 -07001467 /* The extension data consists of a ProtocolNameList which must have
1468 * exactly one ProtocolName. Each of these is length-prefixed. */
1469 CBS protocol_name_list, protocol_name;
1470 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1471 CBS_len(contents) != 0 ||
1472 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1473 /* Empty protocol names are forbidden. */
1474 CBS_len(&protocol_name) == 0 ||
1475 CBS_len(&protocol_name_list) != 0) {
1476 return 0;
1477 }
1478
David Benjamin3e517572016-08-11 11:52:23 -04001479 /* Check that the protcol name is one of the ones we advertised. */
1480 int protocol_ok = 0;
1481 CBS client_protocol_name_list, client_protocol_name;
1482 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1483 ssl->alpn_client_proto_list_len);
1484 while (CBS_len(&client_protocol_name_list) > 0) {
1485 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1486 &client_protocol_name)) {
1487 *out_alert = SSL_AD_INTERNAL_ERROR;
1488 return 0;
1489 }
1490
1491 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1492 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1493 CBS_len(&protocol_name)) == 0) {
1494 protocol_ok = 1;
1495 break;
1496 }
1497 }
1498
1499 if (!protocol_ok) {
1500 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1501 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1502 return 0;
1503 }
1504
Adam Langleyf18e4532015-07-10 13:39:53 -07001505 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1506 &ssl->s3->alpn_selected_len)) {
1507 *out_alert = SSL_AD_INTERNAL_ERROR;
1508 return 0;
1509 }
1510
1511 return 1;
1512}
1513
1514static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1515 CBS *contents) {
1516 if (contents == NULL) {
1517 return 1;
1518 }
1519
1520 if (ssl->ctx->alpn_select_cb == NULL ||
1521 ssl->s3->initial_handshake_complete) {
1522 return 1;
1523 }
1524
1525 /* ALPN takes precedence over NPN. */
David Benjaminb74b0812016-10-06 19:43:48 -04001526 ssl->s3->hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001527
1528 CBS protocol_name_list;
1529 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1530 CBS_len(contents) != 0 ||
1531 CBS_len(&protocol_name_list) < 2) {
1532 return 0;
1533 }
1534
1535 /* Validate the protocol list. */
1536 CBS protocol_name_list_copy = protocol_name_list;
1537 while (CBS_len(&protocol_name_list_copy) > 0) {
1538 CBS protocol_name;
1539
1540 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1541 /* Empty protocol names are forbidden. */
1542 CBS_len(&protocol_name) == 0) {
1543 return 0;
1544 }
1545 }
1546
1547 const uint8_t *selected;
1548 uint8_t selected_len;
1549 if (ssl->ctx->alpn_select_cb(
1550 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1551 CBS_len(&protocol_name_list),
1552 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1553 OPENSSL_free(ssl->s3->alpn_selected);
1554 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1555 if (ssl->s3->alpn_selected == NULL) {
1556 *out_alert = SSL_AD_INTERNAL_ERROR;
1557 return 0;
1558 }
1559 ssl->s3->alpn_selected_len = selected_len;
1560 }
1561
1562 return 1;
1563}
1564
1565static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1566 if (ssl->s3->alpn_selected == NULL) {
1567 return 1;
1568 }
1569
1570 CBB contents, proto_list, proto;
1571 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1572 !CBB_add_u16_length_prefixed(out, &contents) ||
1573 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1574 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001575 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1576 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001577 !CBB_flush(out)) {
1578 return 0;
1579 }
1580
1581 return 1;
1582}
1583
1584
Adam Langley49c7af12015-07-10 14:33:46 -07001585/* Channel ID.
1586 *
1587 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1588
1589static void ext_channel_id_init(SSL *ssl) {
1590 ssl->s3->tlsext_channel_id_valid = 0;
1591}
1592
1593static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1594 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001595 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001596 return 1;
1597 }
1598
1599 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1600 !CBB_add_u16(out, 0 /* length */)) {
1601 return 0;
1602 }
1603
1604 return 1;
1605}
1606
1607static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1608 CBS *contents) {
1609 if (contents == NULL) {
1610 return 1;
1611 }
1612
Steven Valdez143e8b32016-07-11 13:19:03 -04001613 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1614 return 0;
1615 }
1616
David Benjamince079fd2016-08-02 16:22:34 -04001617 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001618 assert(ssl->tlsext_channel_id_enabled);
1619
1620 if (CBS_len(contents) != 0) {
1621 return 0;
1622 }
1623
1624 ssl->s3->tlsext_channel_id_valid = 1;
1625 return 1;
1626}
1627
1628static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1629 CBS *contents) {
1630 if (contents == NULL ||
1631 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001632 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001633 return 1;
1634 }
1635
1636 if (CBS_len(contents) != 0) {
1637 return 0;
1638 }
1639
1640 ssl->s3->tlsext_channel_id_valid = 1;
1641 return 1;
1642}
1643
1644static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001645 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1646 return 1;
1647 }
1648
Adam Langley49c7af12015-07-10 14:33:46 -07001649 if (!ssl->s3->tlsext_channel_id_valid) {
1650 return 1;
1651 }
1652
1653 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1654 !CBB_add_u16(out, 0 /* length */)) {
1655 return 0;
1656 }
1657
1658 return 1;
1659}
1660
Adam Langley391250d2015-07-15 19:06:07 -07001661
1662/* Secure Real-time Transport Protocol (SRTP) extension.
1663 *
1664 * https://tools.ietf.org/html/rfc5764 */
1665
Adam Langley391250d2015-07-15 19:06:07 -07001666
1667static void ext_srtp_init(SSL *ssl) {
1668 ssl->srtp_profile = NULL;
1669}
1670
1671static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1672 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1673 if (profiles == NULL) {
1674 return 1;
1675 }
1676 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1677 if (num_profiles == 0) {
1678 return 1;
1679 }
1680
1681 CBB contents, profile_ids;
1682 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1683 !CBB_add_u16_length_prefixed(out, &contents) ||
1684 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1685 return 0;
1686 }
1687
David Benjamin54091232016-09-05 12:47:25 -04001688 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001689 if (!CBB_add_u16(&profile_ids,
1690 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1691 return 0;
1692 }
1693 }
1694
1695 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1696 !CBB_flush(out)) {
1697 return 0;
1698 }
1699
1700 return 1;
1701}
1702
1703static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1704 CBS *contents) {
1705 if (contents == NULL) {
1706 return 1;
1707 }
1708
1709 /* The extension consists of a u16-prefixed profile ID list containing a
1710 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1711 *
1712 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1713 CBS profile_ids, srtp_mki;
1714 uint16_t profile_id;
1715 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1716 !CBS_get_u16(&profile_ids, &profile_id) ||
1717 CBS_len(&profile_ids) != 0 ||
1718 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1719 CBS_len(contents) != 0) {
1720 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1721 return 0;
1722 }
1723
1724 if (CBS_len(&srtp_mki) != 0) {
1725 /* Must be no MKI, since we never offer one. */
1726 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1727 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1728 return 0;
1729 }
1730
1731 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1732
1733 /* Check to see if the server gave us something we support (and presumably
1734 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001735 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001736 const SRTP_PROTECTION_PROFILE *profile =
1737 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1738
1739 if (profile->id == profile_id) {
1740 ssl->srtp_profile = profile;
1741 return 1;
1742 }
1743 }
1744
1745 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1746 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1747 return 0;
1748}
1749
1750static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1751 CBS *contents) {
1752 if (contents == NULL) {
1753 return 1;
1754 }
1755
1756 CBS profile_ids, srtp_mki;
1757 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1758 CBS_len(&profile_ids) < 2 ||
1759 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1760 CBS_len(contents) != 0) {
1761 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1762 return 0;
1763 }
1764 /* Discard the MKI value for now. */
1765
1766 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1767 SSL_get_srtp_profiles(ssl);
1768
1769 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001770 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001771 const SRTP_PROTECTION_PROFILE *server_profile =
1772 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1773
1774 CBS profile_ids_tmp;
1775 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1776
1777 while (CBS_len(&profile_ids_tmp) > 0) {
1778 uint16_t profile_id;
1779 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1780 return 0;
1781 }
1782
1783 if (server_profile->id == profile_id) {
1784 ssl->srtp_profile = server_profile;
1785 return 1;
1786 }
1787 }
1788 }
1789
1790 return 1;
1791}
1792
1793static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1794 if (ssl->srtp_profile == NULL) {
1795 return 1;
1796 }
1797
1798 CBB contents, profile_ids;
1799 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1800 !CBB_add_u16_length_prefixed(out, &contents) ||
1801 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1802 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1803 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1804 !CBB_flush(out)) {
1805 return 0;
1806 }
1807
1808 return 1;
1809}
1810
Adam Langleybdd5d662015-07-20 16:19:08 -07001811
1812/* EC point formats.
1813 *
1814 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1815
1816static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
David Benjamince079fd2016-08-02 16:22:34 -04001817 if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001818 return 0;
1819 }
1820
1821 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1822
David Benjamin54091232016-09-05 12:47:25 -04001823 for (size_t i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001824 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1825
1826 const uint32_t alg_k = cipher->algorithm_mkey;
1827 const uint32_t alg_a = cipher->algorithm_auth;
1828 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1829 return 1;
1830 }
1831 }
1832
1833 return 0;
1834}
1835
Adam Langleybdd5d662015-07-20 16:19:08 -07001836static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001837 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001838 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1839 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001840 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1841 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001842 !CBB_flush(out)) {
1843 return 0;
1844 }
1845
1846 return 1;
1847}
1848
1849static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1850 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1851 return 1;
1852 }
1853
1854 return ext_ec_point_add_extension(ssl, out);
1855}
1856
1857static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1858 CBS *contents) {
1859 if (contents == NULL) {
1860 return 1;
1861 }
1862
Steven Valdez143e8b32016-07-11 13:19:03 -04001863 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1864 return 0;
1865 }
1866
Adam Langleybdd5d662015-07-20 16:19:08 -07001867 CBS ec_point_format_list;
1868 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1869 CBS_len(contents) != 0) {
1870 return 0;
1871 }
1872
David Benjaminfc059942015-07-30 23:01:59 -04001873 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1874 * point format. */
1875 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1876 CBS_len(&ec_point_format_list)) == NULL) {
1877 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001878 return 0;
1879 }
1880
1881 return 1;
1882}
1883
1884static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1885 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001886 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1887 return 1;
1888 }
1889
Adam Langleybdd5d662015-07-20 16:19:08 -07001890 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1891}
1892
1893static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001894 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1895 return 1;
1896 }
1897
Adam Langleybdd5d662015-07-20 16:19:08 -07001898 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1899 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001900 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001901
1902 if (!using_ecc) {
1903 return 1;
1904 }
1905
1906 return ext_ec_point_add_extension(ssl, out);
1907}
1908
Steven Valdez4aa154e2016-07-29 14:32:55 -04001909/* Pre Shared Key
1910 *
Steven Valdez5b986082016-09-01 12:29:49 -04001911 * https://tools.ietf.org/html/draft-ietf-tls-tls13-15 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001912
1913static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
1914 uint16_t min_version, max_version;
1915 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1916 return 0;
1917 }
1918
David Benjamin2dc02042016-09-19 19:57:37 -04001919 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001920 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001921 !ssl->method->version_from_wire(&session_version,
1922 ssl->session->ssl_version) ||
1923 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001924 return 1;
1925 }
1926
Steven Valdez5b986082016-09-01 12:29:49 -04001927 CBB contents, identity, ke_modes, auth_modes, ticket;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001928 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1929 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001930 !CBB_add_u16_length_prefixed(&contents, &identity) ||
1931 !CBB_add_u8_length_prefixed(&identity, &ke_modes) ||
1932 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE) ||
1933 !CBB_add_u8_length_prefixed(&identity, &auth_modes) ||
1934 !CBB_add_u8(&auth_modes, SSL_PSK_AUTH) ||
1935 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1936 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001937 ssl->session->tlsext_ticklen)) {
1938 return 0;
1939 }
1940
1941 return CBB_flush(out);
1942}
1943
1944int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1945 CBS *contents) {
1946 uint16_t psk_id;
1947 if (!CBS_get_u16(contents, &psk_id) ||
1948 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001949 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001950 *out_alert = SSL_AD_DECODE_ERROR;
1951 return 0;
1952 }
1953
1954 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001955 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001956 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1957 return 0;
1958 }
1959
1960 return 1;
1961}
1962
1963int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
1964 SSL_SESSION **out_session,
1965 uint8_t *out_alert,
1966 CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04001967 /* We only process the first PSK identity since we don't support pure PSK. */
1968 CBS identity, ke_modes, auth_modes, ticket;
1969 if (!CBS_get_u16_length_prefixed(contents, &identity) ||
1970 !CBS_get_u8_length_prefixed(&identity, &ke_modes) ||
1971 !CBS_get_u8_length_prefixed(&identity, &auth_modes) ||
1972 !CBS_get_u16_length_prefixed(&identity, &ticket) ||
1973 CBS_len(&identity) != 0) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001974 *out_alert = SSL_AD_DECODE_ERROR;
1975 return 0;
1976 }
1977
Steven Valdez5b986082016-09-01 12:29:49 -04001978 /* We only support tickets with PSK_DHE_KE and PSK_AUTH. */
1979 if (memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, CBS_len(&ke_modes)) == NULL ||
1980 memchr(CBS_data(&auth_modes), SSL_PSK_AUTH, CBS_len(&auth_modes)) ==
1981 NULL) {
1982 *out_session = NULL;
1983 return 1;
1984 }
1985
Steven Valdez4aa154e2016-07-29 14:32:55 -04001986 /* TLS 1.3 session tickets are renewed separately as part of the
1987 * NewSessionTicket. */
1988 int renew;
Steven Valdez5b986082016-09-01 12:29:49 -04001989 return tls_process_ticket(ssl, out_session, &renew, CBS_data(&ticket),
1990 CBS_len(&ticket), NULL, 0);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001991}
1992
1993int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
1994 if (!ssl->s3->session_reused) {
1995 return 1;
1996 }
1997
1998 CBB contents;
1999 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2000 !CBB_add_u16_length_prefixed(out, &contents) ||
2001 /* We only consider the first identity for resumption */
2002 !CBB_add_u16(&contents, 0) ||
2003 !CBB_flush(out)) {
2004 return 0;
2005 }
2006
2007 return 1;
2008}
2009
2010
Steven Valdez143e8b32016-07-11 13:19:03 -04002011/* Key Share
2012 *
2013 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
2014
2015static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
2016 uint16_t min_version, max_version;
2017 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2018 return 0;
2019 }
2020
David Benjamin53a2dfc2016-10-07 21:04:12 -04002021 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002022 return 1;
2023 }
2024
2025 CBB contents, kse_bytes;
2026 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2027 !CBB_add_u16_length_prefixed(out, &contents) ||
2028 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2029 return 0;
2030 }
2031
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002032 uint16_t group_id;
David Benjamin3baa6e12016-10-07 21:10:38 -04002033 if (ssl->s3->hs->received_hello_retry_request) {
2034 /* Replay the old key shares. */
Steven Valdez5440fe02016-07-18 12:40:30 -04002035 if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
2036 ssl->s3->hs->key_share_bytes_len)) {
2037 return 0;
2038 }
2039 OPENSSL_free(ssl->s3->hs->key_share_bytes);
2040 ssl->s3->hs->key_share_bytes = NULL;
David Benjamin4fe3c902016-08-16 02:17:03 -04002041 ssl->s3->hs->key_share_bytes_len = 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002042
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002043 group_id = ssl->s3->hs->retry_group;
David Benjamin3baa6e12016-10-07 21:10:38 -04002044
2045 /* We received a HelloRetryRequest without a new curve, so there is no new
2046 * share to append. Leave |ecdh_ctx| as-is. */
2047 if (group_id == 0) {
2048 return CBB_flush(out);
2049 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002050 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002051 /* Add a fake group. See draft-davidben-tls-grease-01. */
2052 if (ssl->ctx->grease_enabled &&
2053 (!CBB_add_u16(&kse_bytes,
2054 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2055 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2056 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2057 return 0;
2058 }
2059
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002060 /* Predict the most preferred group. */
2061 const uint16_t *groups;
2062 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002063 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002064 if (groups_len == 0) {
2065 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2066 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002067 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002068
2069 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002070 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002071
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002072 CBB key_exchange;
2073 if (!CBB_add_u16(&kse_bytes, group_id) ||
2074 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2075 !SSL_ECDH_CTX_init(&ssl->s3->hs->ecdh_ctx, group_id) ||
2076 !SSL_ECDH_CTX_offer(&ssl->s3->hs->ecdh_ctx, &key_exchange) ||
2077 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002078 return 0;
2079 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002080
David Benjamin3baa6e12016-10-07 21:10:38 -04002081 if (!ssl->s3->hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002082 /* Save the contents of the extension to repeat it in the second
2083 * ClientHello. */
2084 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2085 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2086 CBB_len(&kse_bytes));
2087 if (ssl->s3->hs->key_share_bytes == NULL) {
2088 return 0;
2089 }
2090 }
2091
Steven Valdez143e8b32016-07-11 13:19:03 -04002092 return CBB_flush(out);
2093}
2094
Steven Valdez7259f2f2016-08-02 16:55:05 -04002095int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2096 size_t *out_secret_len,
2097 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002098 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002099 uint16_t group_id;
2100 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002101 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2102 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002103 *out_alert = SSL_AD_DECODE_ERROR;
2104 return 0;
2105 }
2106
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002107 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002108 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2109 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2110 return 0;
2111 }
2112
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002113 if (!SSL_ECDH_CTX_finish(&ssl->s3->hs->ecdh_ctx, out_secret, out_secret_len,
2114 out_alert, CBS_data(&peer_key),
2115 CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002116 *out_alert = SSL_AD_INTERNAL_ERROR;
2117 return 0;
2118 }
2119
David Benjamin5c4e8572016-08-19 17:44:53 -04002120 ssl->s3->new_session->key_exchange_info = group_id;
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002121 SSL_ECDH_CTX_cleanup(&ssl->s3->hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002122 return 1;
2123}
2124
Steven Valdez7259f2f2016-08-02 16:55:05 -04002125int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2126 uint8_t **out_secret,
2127 size_t *out_secret_len,
2128 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002129 uint16_t group_id;
2130 CBS key_shares;
Steven Valdez803c77a2016-09-06 14:13:43 -04002131 if (!tls1_get_shared_group(ssl, &group_id)) {
2132 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2133 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2134 return 0;
2135 }
2136
2137 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002138 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002139 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002140 return 0;
2141 }
2142
David Benjamin7e1f9842016-09-20 19:24:40 -04002143 /* Find the corresponding key share. */
2144 int found = 0;
2145 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002146 while (CBS_len(&key_shares) > 0) {
2147 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002148 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002149 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002150 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2151 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002152 return 0;
2153 }
2154
David Benjamin7e1f9842016-09-20 19:24:40 -04002155 if (id == group_id) {
2156 if (found) {
2157 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2158 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2159 return 0;
2160 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002161
David Benjamin7e1f9842016-09-20 19:24:40 -04002162 found = 1;
2163 peer_key = peer_key_tmp;
2164 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002165 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002166 }
2167
David Benjamin7e1f9842016-09-20 19:24:40 -04002168 if (!found) {
2169 *out_found = 0;
2170 *out_secret = NULL;
2171 *out_secret_len = 0;
2172 return 1;
2173 }
2174
2175 /* Compute the DH secret. */
2176 uint8_t *secret = NULL;
2177 size_t secret_len;
2178 SSL_ECDH_CTX group;
2179 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2180 CBB public_key;
2181 if (!CBB_init(&public_key, 32) ||
2182 !SSL_ECDH_CTX_init(&group, group_id) ||
2183 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len,
2184 out_alert, CBS_data(&peer_key),
2185 CBS_len(&peer_key)) ||
2186 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2187 &ssl->s3->hs->public_key_len)) {
2188 OPENSSL_free(secret);
2189 SSL_ECDH_CTX_cleanup(&group);
2190 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002191 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002192 return 0;
2193 }
2194
2195 SSL_ECDH_CTX_cleanup(&group);
2196
2197 *out_secret = secret;
2198 *out_secret_len = secret_len;
2199 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002200 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002201}
2202
Steven Valdez7259f2f2016-08-02 16:55:05 -04002203int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002204 uint16_t group_id;
2205 CBB kse_bytes, public_key;
2206 if (!tls1_get_shared_group(ssl, &group_id) ||
2207 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2208 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2209 !CBB_add_u16(&kse_bytes, group_id) ||
2210 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2211 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2212 ssl->s3->hs->public_key_len) ||
2213 !CBB_flush(out)) {
2214 return 0;
2215 }
2216
David Benjamin4fe3c902016-08-16 02:17:03 -04002217 OPENSSL_free(ssl->s3->hs->public_key);
2218 ssl->s3->hs->public_key = NULL;
2219 ssl->s3->hs->public_key_len = 0;
2220
David Benjamin5c4e8572016-08-19 17:44:53 -04002221 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002222 return 1;
2223}
2224
2225
Steven Valdezfdd10992016-09-15 16:27:05 -04002226/* Supported Versions
2227 *
2228 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2229
2230static int ext_supported_versions_add_clienthello(SSL *ssl, CBB *out) {
2231 uint16_t min_version, max_version;
2232 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2233 return 0;
2234 }
2235
2236 if (max_version <= TLS1_2_VERSION) {
2237 return 1;
2238 }
2239
2240 CBB contents, versions;
2241 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2242 !CBB_add_u16_length_prefixed(out, &contents) ||
2243 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2244 return 0;
2245 }
2246
David Benjamind9791bf2016-09-27 16:39:52 -04002247 /* Add a fake version. See draft-davidben-tls-grease-01. */
2248 if (ssl->ctx->grease_enabled &&
2249 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2250 return 0;
2251 }
2252
Steven Valdezfdd10992016-09-15 16:27:05 -04002253 for (uint16_t version = max_version; version >= min_version; version--) {
2254 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2255 return 0;
2256 }
2257 }
2258
2259 if (!CBB_flush(out)) {
2260 return 0;
2261 }
2262
2263 return 1;
2264}
2265
2266
David Benjamin3baa6e12016-10-07 21:10:38 -04002267/* Cookie
2268 *
2269 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2270
2271static int ext_cookie_add_clienthello(SSL *ssl, CBB *out) {
2272 if (ssl->s3->hs->cookie == NULL) {
2273 return 1;
2274 }
2275
2276 CBB contents, cookie;
2277 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2278 !CBB_add_u16_length_prefixed(out, &contents) ||
2279 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
2280 !CBB_add_bytes(&cookie, ssl->s3->hs->cookie, ssl->s3->hs->cookie_len) ||
2281 !CBB_flush(out)) {
2282 return 0;
2283 }
2284
2285 /* The cookie is no longer needed in memory. */
2286 OPENSSL_free(ssl->s3->hs->cookie);
2287 ssl->s3->hs->cookie = NULL;
2288 ssl->s3->hs->cookie_len = 0;
2289 return 1;
2290}
2291
2292
Steven Valdezce902a92016-05-17 11:47:53 -04002293/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002294 *
Steven Valdezce902a92016-05-17 11:47:53 -04002295 * https://tools.ietf.org/html/rfc4492#section-5.1.2
2296 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07002297
Steven Valdezce902a92016-05-17 11:47:53 -04002298static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002299 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2300 return 1;
2301 }
2302
Steven Valdezce902a92016-05-17 11:47:53 -04002303 CBB contents, groups_bytes;
2304 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002305 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002306 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002307 return 0;
2308 }
2309
David Benjamin65ac9972016-09-02 21:35:25 -04002310 /* Add a fake group. See draft-davidben-tls-grease-01. */
2311 if (ssl->ctx->grease_enabled &&
2312 !CBB_add_u16(&groups_bytes,
2313 ssl_get_grease_value(ssl, ssl_grease_group))) {
2314 return 0;
2315 }
2316
Steven Valdezce902a92016-05-17 11:47:53 -04002317 const uint16_t *groups;
2318 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002319 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002320
David Benjamin54091232016-09-05 12:47:25 -04002321 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002322 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002323 return 0;
2324 }
2325 }
2326
2327 return CBB_flush(out);
2328}
2329
Steven Valdezce902a92016-05-17 11:47:53 -04002330static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2331 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002332 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2333 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002334 return 1;
2335}
2336
Steven Valdezce902a92016-05-17 11:47:53 -04002337static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2338 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002339 if (contents == NULL) {
2340 return 1;
2341 }
2342
Steven Valdezce902a92016-05-17 11:47:53 -04002343 CBS supported_group_list;
2344 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2345 CBS_len(&supported_group_list) == 0 ||
2346 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002347 CBS_len(contents) != 0) {
2348 return 0;
2349 }
2350
David Benjamin43612b62016-10-07 00:41:50 -04002351 ssl->s3->hs->peer_supported_group_list = OPENSSL_malloc(
Steven Valdezce902a92016-05-17 11:47:53 -04002352 CBS_len(&supported_group_list));
David Benjamin43612b62016-10-07 00:41:50 -04002353 if (ssl->s3->hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002354 *out_alert = SSL_AD_INTERNAL_ERROR;
2355 return 0;
2356 }
2357
Steven Valdezce902a92016-05-17 11:47:53 -04002358 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002359 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002360 if (!CBS_get_u16(&supported_group_list,
David Benjamin43612b62016-10-07 00:41:50 -04002361 &ssl->s3->hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002362 goto err;
2363 }
2364 }
2365
Steven Valdezce902a92016-05-17 11:47:53 -04002366 assert(CBS_len(&supported_group_list) == 0);
David Benjamin43612b62016-10-07 00:41:50 -04002367 ssl->s3->hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002368
2369 return 1;
2370
2371err:
David Benjamin43612b62016-10-07 00:41:50 -04002372 OPENSSL_free(ssl->s3->hs->peer_supported_group_list);
2373 ssl->s3->hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002374 *out_alert = SSL_AD_INTERNAL_ERROR;
2375 return 0;
2376}
2377
Steven Valdezce902a92016-05-17 11:47:53 -04002378static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002379 /* Servers don't echo this extension. */
2380 return 1;
2381}
2382
2383
Adam Langley614c66a2015-06-12 15:26:58 -07002384/* kExtensions contains all the supported extensions. */
2385static const struct tls_extension kExtensions[] = {
2386 {
Adam Langley5021b222015-06-12 18:27:58 -07002387 TLSEXT_TYPE_renegotiate,
2388 NULL,
2389 ext_ri_add_clienthello,
2390 ext_ri_parse_serverhello,
2391 ext_ri_parse_clienthello,
2392 ext_ri_add_serverhello,
2393 },
2394 {
Adam Langley614c66a2015-06-12 15:26:58 -07002395 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002396 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002397 ext_sni_add_clienthello,
2398 ext_sni_parse_serverhello,
2399 ext_sni_parse_clienthello,
2400 ext_sni_add_serverhello,
2401 },
Adam Langley0a056712015-07-01 15:03:33 -07002402 {
2403 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002404 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002405 ext_ems_add_clienthello,
2406 ext_ems_parse_serverhello,
2407 ext_ems_parse_clienthello,
2408 ext_ems_add_serverhello,
2409 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002410 {
2411 TLSEXT_TYPE_session_ticket,
2412 NULL,
2413 ext_ticket_add_clienthello,
2414 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002415 /* Ticket extension client parsing is handled in ssl_session.c */
2416 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002417 ext_ticket_add_serverhello,
2418 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002419 {
2420 TLSEXT_TYPE_signature_algorithms,
2421 NULL,
2422 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002423 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002424 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002425 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002426 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002427 {
2428 TLSEXT_TYPE_status_request,
2429 ext_ocsp_init,
2430 ext_ocsp_add_clienthello,
2431 ext_ocsp_parse_serverhello,
2432 ext_ocsp_parse_clienthello,
2433 ext_ocsp_add_serverhello,
2434 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002435 {
2436 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002437 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002438 ext_npn_add_clienthello,
2439 ext_npn_parse_serverhello,
2440 ext_npn_parse_clienthello,
2441 ext_npn_add_serverhello,
2442 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002443 {
2444 TLSEXT_TYPE_certificate_timestamp,
2445 NULL,
2446 ext_sct_add_clienthello,
2447 ext_sct_parse_serverhello,
2448 ext_sct_parse_clienthello,
2449 ext_sct_add_serverhello,
2450 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002451 {
2452 TLSEXT_TYPE_application_layer_protocol_negotiation,
2453 ext_alpn_init,
2454 ext_alpn_add_clienthello,
2455 ext_alpn_parse_serverhello,
2456 ext_alpn_parse_clienthello,
2457 ext_alpn_add_serverhello,
2458 },
Adam Langley49c7af12015-07-10 14:33:46 -07002459 {
2460 TLSEXT_TYPE_channel_id,
2461 ext_channel_id_init,
2462 ext_channel_id_add_clienthello,
2463 ext_channel_id_parse_serverhello,
2464 ext_channel_id_parse_clienthello,
2465 ext_channel_id_add_serverhello,
2466 },
Adam Langley391250d2015-07-15 19:06:07 -07002467 {
2468 TLSEXT_TYPE_srtp,
2469 ext_srtp_init,
2470 ext_srtp_add_clienthello,
2471 ext_srtp_parse_serverhello,
2472 ext_srtp_parse_clienthello,
2473 ext_srtp_add_serverhello,
2474 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002475 {
2476 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002477 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002478 ext_ec_point_add_clienthello,
2479 ext_ec_point_parse_serverhello,
2480 ext_ec_point_parse_clienthello,
2481 ext_ec_point_add_serverhello,
2482 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002483 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002484 TLSEXT_TYPE_key_share,
2485 NULL,
2486 ext_key_share_add_clienthello,
2487 forbid_parse_serverhello,
2488 ignore_parse_clienthello,
2489 dont_add_serverhello,
2490 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002491 {
2492 TLSEXT_TYPE_pre_shared_key,
2493 NULL,
2494 ext_pre_shared_key_add_clienthello,
2495 forbid_parse_serverhello,
2496 ignore_parse_clienthello,
2497 dont_add_serverhello,
2498 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002499 {
2500 TLSEXT_TYPE_supported_versions,
2501 NULL,
2502 ext_supported_versions_add_clienthello,
2503 forbid_parse_serverhello,
2504 ignore_parse_clienthello,
2505 dont_add_serverhello,
2506 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002507 {
2508 TLSEXT_TYPE_cookie,
2509 NULL,
2510 ext_cookie_add_clienthello,
2511 forbid_parse_serverhello,
2512 ignore_parse_clienthello,
2513 dont_add_serverhello,
2514 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002515 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2516 * intolerant to the last extension being zero-length. See
2517 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002518 {
Steven Valdezce902a92016-05-17 11:47:53 -04002519 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002520 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002521 ext_supported_groups_add_clienthello,
2522 ext_supported_groups_parse_serverhello,
2523 ext_supported_groups_parse_clienthello,
2524 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002525 },
Adam Langley614c66a2015-06-12 15:26:58 -07002526};
2527
2528#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2529
Adam Langley4cfa96b2015-07-01 11:56:55 -07002530OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002531 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002532 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002533OPENSSL_COMPILE_ASSERT(
2534 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2535 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002536
Adam Langley614c66a2015-06-12 15:26:58 -07002537static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2538 uint16_t value) {
2539 unsigned i;
2540 for (i = 0; i < kNumExtensions; i++) {
2541 if (kExtensions[i].value == value) {
2542 *out_index = i;
2543 return &kExtensions[i];
2544 }
2545 }
2546
2547 return NULL;
2548}
2549
Adam Langley09505632015-07-30 18:10:13 -07002550int SSL_extension_supported(unsigned extension_value) {
2551 uint32_t index;
2552 return extension_value == TLSEXT_TYPE_padding ||
2553 tls_extension_find(&index, extension_value) != NULL;
2554}
2555
David Benjamine8d53502015-10-10 14:13:23 -04002556int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002557 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002558 if (ssl->client_version == SSL3_VERSION &&
2559 !ssl->s3->send_connection_binding) {
2560 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002561 }
Adam Langley95c29f32014-06-20 12:00:00 -07002562
David Benjamine8d53502015-10-10 14:13:23 -04002563 CBB extensions;
2564 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002565 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002566 }
Adam Langley95c29f32014-06-20 12:00:00 -07002567
David Benjaminf5d2cd02016-10-06 19:39:20 -04002568 ssl->s3->hs->extensions.sent = 0;
2569 ssl->s3->hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002570
David Benjamin54091232016-09-05 12:47:25 -04002571 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002572 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002573 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002574 }
2575 }
Adam Langley95c29f32014-06-20 12:00:00 -07002576
David Benjamin65ac9972016-09-02 21:35:25 -04002577 uint16_t grease_ext1 = 0;
2578 if (ssl->ctx->grease_enabled) {
2579 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2580 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2581 if (!CBB_add_u16(&extensions, grease_ext1) ||
2582 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2583 goto err;
2584 }
2585 }
2586
David Benjamin54091232016-09-05 12:47:25 -04002587 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002588 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002589 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002590 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2591 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2592 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002593 }
Adam Langley95c29f32014-06-20 12:00:00 -07002594
Adam Langley33ad2b52015-07-20 17:43:53 -07002595 if (CBB_len(&extensions) != len_before) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002596 ssl->s3->hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002597 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002598 }
Adam Langley75712922014-10-10 16:23:43 -07002599
David Benjamine8d53502015-10-10 14:13:23 -04002600 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002601 goto err;
2602 }
2603
David Benjamin65ac9972016-09-02 21:35:25 -04002604 if (ssl->ctx->grease_enabled) {
2605 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2606 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2607
2608 /* The two fake extensions must not have the same value. GREASE values are
2609 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2610 * one. */
2611 if (grease_ext1 == grease_ext2) {
2612 grease_ext2 ^= 0x1010;
2613 }
2614
2615 if (!CBB_add_u16(&extensions, grease_ext2) ||
2616 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2617 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2618 goto err;
2619 }
2620 }
2621
David Benjamince079fd2016-08-02 16:22:34 -04002622 if (!SSL_is_dtls(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002623 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002624 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002625 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002626 *
2627 * NB: because this code works out the length of all existing extensions
2628 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002629 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002630 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002631 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002632 * Server 7.0 is intolerant to the last extension being zero-length. See
2633 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002634 if (padding_len >= 4 + 1) {
2635 padding_len -= 4;
2636 } else {
2637 padding_len = 1;
2638 }
Adam Langley95c29f32014-06-20 12:00:00 -07002639
Adam Langley33ad2b52015-07-20 17:43:53 -07002640 uint8_t *padding_bytes;
2641 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2642 !CBB_add_u16(&extensions, padding_len) ||
2643 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2644 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002645 }
Adam Langley75712922014-10-10 16:23:43 -07002646
Adam Langley33ad2b52015-07-20 17:43:53 -07002647 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002648 }
2649 }
Adam Langley75712922014-10-10 16:23:43 -07002650
David Benjamina01deee2015-12-08 18:56:31 -05002651 /* Discard empty extensions blocks. */
2652 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002653 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002654 }
2655
David Benjamine8d53502015-10-10 14:13:23 -04002656 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002657
2658err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002659 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002660 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002661}
Adam Langley95c29f32014-06-20 12:00:00 -07002662
David Benjamin56380462015-10-10 14:59:09 -04002663int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002664 CBB extensions;
2665 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002666 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002667 }
2668
2669 unsigned i;
2670 for (i = 0; i < kNumExtensions; i++) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002671 if (!(ssl->s3->hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002672 /* Don't send extensions that were not received. */
2673 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002674 }
Adam Langley95c29f32014-06-20 12:00:00 -07002675
David Benjamin56380462015-10-10 14:59:09 -04002676 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002677 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2678 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2679 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002680 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002681 }
Adam Langley95c29f32014-06-20 12:00:00 -07002682
David Benjamin56380462015-10-10 14:59:09 -04002683 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002684 goto err;
2685 }
2686
Steven Valdez143e8b32016-07-11 13:19:03 -04002687 /* Discard empty extensions blocks before TLS 1.3. */
2688 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2689 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002690 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002691 }
2692
David Benjamin56380462015-10-10 14:59:09 -04002693 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002694
2695err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002696 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002697 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002698}
Adam Langley95c29f32014-06-20 12:00:00 -07002699
David Benjamine14ff062016-08-09 16:21:24 -04002700static int ssl_scan_clienthello_tlsext(
2701 SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
2702 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002703 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002704 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002705 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002706 }
2707 }
2708
David Benjaminf5d2cd02016-10-06 19:39:20 -04002709 ssl->s3->hs->extensions.received = 0;
2710 ssl->s3->hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002711
David Benjamine14ff062016-08-09 16:21:24 -04002712 CBS extensions;
2713 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2714 while (CBS_len(&extensions) != 0) {
2715 uint16_t type;
2716 CBS extension;
2717
2718 /* Decode the next extension. */
2719 if (!CBS_get_u16(&extensions, &type) ||
2720 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002721 *out_alert = SSL_AD_DECODE_ERROR;
2722 return 0;
2723 }
Adam Langley95c29f32014-06-20 12:00:00 -07002724
David Benjamine14ff062016-08-09 16:21:24 -04002725 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2726 * ambiguous. Ignore all but the renegotiation_info extension. */
2727 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2728 continue;
2729 }
Adam Langley95c29f32014-06-20 12:00:00 -07002730
David Benjamine14ff062016-08-09 16:21:24 -04002731 unsigned ext_index;
2732 const struct tls_extension *const ext =
2733 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002734
David Benjamine14ff062016-08-09 16:21:24 -04002735 if (ext == NULL) {
2736 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002737 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002738 return 0;
2739 }
David Benjamine14ff062016-08-09 16:21:24 -04002740 continue;
2741 }
2742
David Benjaminf5d2cd02016-10-06 19:39:20 -04002743 ssl->s3->hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002744 uint8_t alert = SSL_AD_DECODE_ERROR;
2745 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2746 *out_alert = alert;
2747 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2748 ERR_add_error_dataf("extension: %u", (unsigned)type);
2749 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002750 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002751 }
Adam Langley75712922014-10-10 16:23:43 -07002752
David Benjamin1deb41b2016-08-09 19:36:38 -04002753 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002754 if (ssl->s3->hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002755 continue;
2756 }
2757
2758 CBS *contents = NULL, fake_contents;
2759 static const uint8_t kFakeRenegotiateExtension[] = {0};
2760 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2761 ssl_client_cipher_list_contains_cipher(client_hello,
2762 SSL3_CK_SCSV & 0xffff)) {
2763 /* The renegotiation SCSV was received so pretend that we received a
2764 * renegotiation extension. */
2765 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2766 sizeof(kFakeRenegotiateExtension));
2767 contents = &fake_contents;
David Benjaminf5d2cd02016-10-06 19:39:20 -04002768 ssl->s3->hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002769 }
2770
2771 /* Extension wasn't observed so call the callback with a NULL
2772 * parameter. */
2773 uint8_t alert = SSL_AD_DECODE_ERROR;
2774 if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
2775 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2776 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2777 *out_alert = alert;
2778 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002779 }
2780 }
2781
Adam Langleyfcf25832014-12-18 17:42:32 -08002782 return 1;
2783}
Adam Langley95c29f32014-06-20 12:00:00 -07002784
David Benjamine14ff062016-08-09 16:21:24 -04002785int ssl_parse_clienthello_tlsext(
2786 SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002787 int alert = -1;
David Benjamine14ff062016-08-09 16:21:24 -04002788 if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002789 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002790 return 0;
2791 }
Adam Langley95c29f32014-06-20 12:00:00 -07002792
David Benjamin0d56f882015-12-19 17:05:56 -05002793 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002794 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002795 return 0;
2796 }
Adam Langley95c29f32014-06-20 12:00:00 -07002797
Adam Langleyfcf25832014-12-18 17:42:32 -08002798 return 1;
2799}
Adam Langley95c29f32014-06-20 12:00:00 -07002800
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002801OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2802
David Benjamin0d56f882015-12-19 17:05:56 -05002803static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002804 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2805 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2806 return 1;
2807 }
Adam Langley614c66a2015-06-12 15:26:58 -07002808
Steven Valdez143e8b32016-07-11 13:19:03 -04002809 /* Decode the extensions block and check it is valid. */
2810 CBS extensions;
2811 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2812 !tls1_check_duplicate_extensions(&extensions)) {
2813 *out_alert = SSL_AD_DECODE_ERROR;
2814 return 0;
2815 }
2816
2817 uint32_t received = 0;
2818 while (CBS_len(&extensions) != 0) {
2819 uint16_t type;
2820 CBS extension;
2821
2822 /* Decode the next extension. */
2823 if (!CBS_get_u16(&extensions, &type) ||
2824 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002825 *out_alert = SSL_AD_DECODE_ERROR;
2826 return 0;
2827 }
Adam Langley95c29f32014-06-20 12:00:00 -07002828
Steven Valdez143e8b32016-07-11 13:19:03 -04002829 unsigned ext_index;
2830 const struct tls_extension *const ext =
2831 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002832
Steven Valdez143e8b32016-07-11 13:19:03 -04002833 if (ext == NULL) {
2834 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002835 return 0;
2836 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002837 continue;
2838 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002839
David Benjaminf5d2cd02016-10-06 19:39:20 -04002840 if (!(ssl->s3->hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002841 type != TLSEXT_TYPE_renegotiate) {
2842 /* If the extension was never sent then it is illegal, except for the
2843 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002844 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2845 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002846 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002847 return 0;
2848 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002849
Steven Valdez143e8b32016-07-11 13:19:03 -04002850 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002851
Steven Valdez143e8b32016-07-11 13:19:03 -04002852 uint8_t alert = SSL_AD_DECODE_ERROR;
2853 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2854 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2855 ERR_add_error_dataf("extension: %u", (unsigned)type);
2856 *out_alert = alert;
2857 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002858 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002859 }
Adam Langley95c29f32014-06-20 12:00:00 -07002860
David Benjamin54091232016-09-05 12:47:25 -04002861 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002862 if (!(received & (1u << i))) {
2863 /* Extension wasn't observed so call the callback with a NULL
2864 * parameter. */
2865 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002866 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002867 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2868 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002869 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002870 return 0;
2871 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002872 }
2873 }
Adam Langley95c29f32014-06-20 12:00:00 -07002874
Adam Langleyfcf25832014-12-18 17:42:32 -08002875 return 1;
2876}
Adam Langley95c29f32014-06-20 12:00:00 -07002877
David Benjamin0d56f882015-12-19 17:05:56 -05002878static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002879 int ret = SSL_TLSEXT_ERR_NOACK;
2880 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002881
David Benjamin78f8aab2016-03-10 16:33:58 -05002882 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002883 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002884 ssl->ctx->tlsext_servername_arg);
2885 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002886 ret = ssl->initial_ctx->tlsext_servername_callback(
2887 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002888 }
Adam Langley95c29f32014-06-20 12:00:00 -07002889
Adam Langleyfcf25832014-12-18 17:42:32 -08002890 switch (ret) {
2891 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002892 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002893 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002894
Adam Langleyfcf25832014-12-18 17:42:32 -08002895 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002896 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002897 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002898
Adam Langleyfcf25832014-12-18 17:42:32 -08002899 case SSL_TLSEXT_ERR_NOACK:
David Benjamina0486782016-10-06 19:11:32 -04002900 ssl->s3->hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002901 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002902
Adam Langleyfcf25832014-12-18 17:42:32 -08002903 default:
2904 return 1;
2905 }
2906}
Adam Langleyed8270a2014-09-02 13:52:56 -07002907
David Benjamin0d56f882015-12-19 17:05:56 -05002908static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002909 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002910 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002911
David Benjamin78f8aab2016-03-10 16:33:58 -05002912 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002913 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002914 ssl->ctx->tlsext_servername_arg);
2915 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002916 ret = ssl->initial_ctx->tlsext_servername_callback(
2917 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002918 }
Adam Langley95c29f32014-06-20 12:00:00 -07002919
Adam Langleyfcf25832014-12-18 17:42:32 -08002920 switch (ret) {
2921 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002922 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002923 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002924
Adam Langleyfcf25832014-12-18 17:42:32 -08002925 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002926 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002927 return 1;
2928
2929 default:
2930 return 1;
2931 }
2932}
2933
David Benjamin0d56f882015-12-19 17:05:56 -05002934int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002935 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002936 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2937 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002938 return 0;
2939 }
2940
David Benjamin0d56f882015-12-19 17:05:56 -05002941 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002942 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002943 return 0;
2944 }
2945
2946 return 1;
2947}
Adam Langley95c29f32014-06-20 12:00:00 -07002948
David Benjamine3aa1d92015-06-16 15:34:50 -04002949int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002950 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002951 size_t ticket_len, const uint8_t *session_id,
2952 size_t session_id_len) {
2953 int ret = 1; /* Most errors are non-fatal. */
2954 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2955 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002956
David Benjamine3aa1d92015-06-16 15:34:50 -04002957 HMAC_CTX hmac_ctx;
2958 HMAC_CTX_init(&hmac_ctx);
2959 EVP_CIPHER_CTX cipher_ctx;
2960 EVP_CIPHER_CTX_init(&cipher_ctx);
2961
David Benjaminef1b0092015-11-21 14:05:44 -05002962 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002963 *out_session = NULL;
2964
Steven Valdez4aa154e2016-07-29 14:32:55 -04002965 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
2966 goto done;
2967 }
2968
David Benjamine3aa1d92015-06-16 15:34:50 -04002969 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2970 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002971 }
2972
David Benjaminadcc3952015-04-26 13:07:57 -04002973 /* Ensure there is room for the key name and the largest IV
2974 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2975 * the maximum IV length should be well under the minimum size for the
2976 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002977 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2978 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002979 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002980 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002981
David Benjamine3aa1d92015-06-16 15:34:50 -04002982 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002983 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2984 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2985 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002986 if (cb_ret < 0) {
2987 ret = 0;
2988 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002989 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002990 if (cb_ret == 0) {
2991 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002992 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002993 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002994 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002995 }
2996 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002997 /* Check the key name matches. */
2998 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2999 SSL_TICKET_KEY_NAME_LEN) != 0) {
3000 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003001 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003002 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3003 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003004 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003005 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3006 ssl_ctx->tlsext_tick_aes_key, iv)) {
3007 ret = 0;
3008 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003009 }
3010 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003011 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003012
David Benjamine3aa1d92015-06-16 15:34:50 -04003013 /* Check the MAC at the end of the ticket. */
3014 uint8_t mac[EVP_MAX_MD_SIZE];
3015 size_t mac_len = HMAC_size(&hmac_ctx);
3016 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003017 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003018 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003019 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003020 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3021 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003022 int mac_ok =
3023 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3024#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3025 mac_ok = 1;
3026#endif
3027 if (!mac_ok) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003028 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003029 }
3030
David Benjamine3aa1d92015-06-16 15:34:50 -04003031 /* Decrypt the session data. */
3032 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3033 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3034 mac_len;
3035 plaintext = OPENSSL_malloc(ciphertext_len);
3036 if (plaintext == NULL) {
3037 ret = 0;
3038 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003039 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003040 size_t plaintext_len;
3041#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3042 memcpy(plaintext, ciphertext, ciphertext_len);
3043 plaintext_len = ciphertext_len;
3044#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003045 if (ciphertext_len >= INT_MAX) {
3046 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003047 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003048 int len1, len2;
3049 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3050 (int)ciphertext_len) ||
3051 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
3052 ERR_clear_error(); /* Don't leave an error on the queue. */
3053 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003054 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003055 plaintext_len = (size_t)(len1 + len2);
3056#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003057
David Benjamine3aa1d92015-06-16 15:34:50 -04003058 /* Decode the session. */
David Benjaminfbc45d72016-09-22 01:21:24 -04003059 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003060 if (session == NULL) {
3061 ERR_clear_error(); /* Don't leave an error on the queue. */
3062 goto done;
3063 }
3064
3065 /* Copy the client's session ID into the new session, to denote the ticket has
3066 * been accepted. */
3067 memcpy(session->session_id, session_id, session_id_len);
3068 session->session_id_length = session_id_len;
3069
Steven Valdez4aa154e2016-07-29 14:32:55 -04003070 if (!ssl_session_is_context_valid(ssl, session) ||
3071 !ssl_session_is_time_valid(ssl, session)) {
3072 SSL_SESSION_free(session);
3073 session = NULL;
3074 }
3075
David Benjamine3aa1d92015-06-16 15:34:50 -04003076 *out_session = session;
3077
3078done:
3079 OPENSSL_free(plaintext);
3080 HMAC_CTX_cleanup(&hmac_ctx);
3081 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3082 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003083}
Adam Langley95c29f32014-06-20 12:00:00 -07003084
Steven Valdez0d62f262015-09-04 12:41:04 -04003085int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003086 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05003087 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003088 return 1;
3089 }
David Benjamincd996942014-07-20 16:23:51 -04003090
David Benjamin0fc37ef2016-08-17 15:29:46 -04003091 SSL_HANDSHAKE *hs = ssl->s3->hs;
3092 OPENSSL_free(hs->peer_sigalgs);
3093 hs->peer_sigalgs = NULL;
3094 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003095
3096 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003097 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003098 return 0;
3099 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003100 num_sigalgs /= 2;
3101
3102 /* supported_signature_algorithms in the certificate request is
3103 * allowed to be empty. */
3104 if (num_sigalgs == 0) {
3105 return 1;
3106 }
3107
Steven Valdez02563852016-06-23 13:33:05 -04003108 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3109 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003110 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3111 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003112 return 0;
3113 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003114 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003115
3116 CBS sigalgs;
3117 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003118 for (size_t i = 0; i < num_sigalgs; i++) {
3119 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003120 return 0;
3121 }
3122 }
Adam Langley95c29f32014-06-20 12:00:00 -07003123
Adam Langleyfcf25832014-12-18 17:42:32 -08003124 return 1;
3125}
David Benjaminec2f27d2014-11-13 19:17:25 -05003126
David Benjaminea9a0d52016-07-08 15:52:59 -07003127int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003128 CERT *cert = ssl->cert;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003129 SSL_HANDSHAKE *hs = ssl->s3->hs;
David Benjaminec2f27d2014-11-13 19:17:25 -05003130
Steven Valdezf0451ca2016-06-29 13:16:27 -04003131 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3132 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3133 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003134 int type = ssl_private_key_type(ssl);
3135 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003136 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003137 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003138 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003139 if (ssl_is_ecdsa_key_type(type)) {
3140 *out = SSL_SIGN_ECDSA_SHA1;
3141 return 1;
3142 }
3143 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3144 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003145 }
3146
Steven Valdezeff1e8d2016-07-06 14:24:47 -04003147 const uint16_t *sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003148 size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07003149 if (cert->sigalgs != NULL) {
3150 sigalgs = cert->sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003151 num_sigalgs = cert->num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003152 }
3153
David Benjamin0fc37ef2016-08-17 15:29:46 -04003154 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3155 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3156 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003157 /* If the client didn't specify any signature_algorithms extension then
3158 * we can assume that it supports SHA1. See
3159 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3160 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3161 SSL_SIGN_ECDSA_SHA1};
3162 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003163 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003164 }
3165
David Benjamin0fc37ef2016-08-17 15:29:46 -04003166 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003167 uint16_t sigalg = sigalgs[i];
3168 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3169 * negotiated. */
3170 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3171 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3172 continue;
3173 }
3174
David Benjamin0fc37ef2016-08-17 15:29:46 -04003175 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003176 if (sigalg == peer_sigalgs[j]) {
3177 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003178 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003179 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003180 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003181 }
Adam Langley95c29f32014-06-20 12:00:00 -07003182
David Benjaminea9a0d52016-07-08 15:52:59 -07003183 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3184 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003185}
Adam Langley95c29f32014-06-20 12:00:00 -07003186
David Benjamind6a4ae92015-08-06 11:10:51 -04003187int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
3188 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003189 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003190
3191 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003192 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
3193 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08003194 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003195
David Benjamind6a4ae92015-08-06 11:10:51 -04003196 static const char kClientIDMagic[] = "TLS Channel ID signature";
3197 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
3198
Steven Valdez87eab492016-06-27 16:34:59 -04003199 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003200 static const char kResumptionMagic[] = "Resumption";
3201 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
3202 if (ssl->session->original_handshake_hash_len == 0) {
3203 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3204 goto err;
3205 }
3206 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
3207 ssl->session->original_handshake_hash_len);
3208 }
3209
3210 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3211 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3212 sizeof(handshake_hash));
3213 if (handshake_hash_len < 0) {
3214 goto err;
3215 }
3216 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
3217 unsigned len_u;
3218 EVP_DigestFinal_ex(&ctx, out, &len_u);
3219 *out_len = len_u;
3220
3221 ret = 1;
3222
3223err:
3224 EVP_MD_CTX_cleanup(&ctx);
3225 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003226}
Adam Langley1258b6a2014-06-20 12:00:00 -07003227
3228/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003229 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003230 * data. */
3231int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003232 int digest_len;
3233 /* This function should never be called for a resumed session because the
3234 * handshake hashes that we wish to record are for the original, full
3235 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003236 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003237 return -1;
3238 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003239
Adam Langleyfcf25832014-12-18 17:42:32 -08003240 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003241 tls1_handshake_digest(
3242 ssl, ssl->s3->new_session->original_handshake_hash,
3243 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003244 if (digest_len < 0) {
3245 return -1;
3246 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003247
Steven Valdez87eab492016-06-27 16:34:59 -04003248 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003249
Adam Langleyfcf25832014-12-18 17:42:32 -08003250 return 1;
3251}