blob: c3d39a3feb7b1a632891c7d5107ec7950f41a041 [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
David Benjamin3ef76972016-10-17 17:59:54 -0400444/* kVerifySignatureAlgorithms is the default list of accepted signature
445 * algorithms for verifying. */
446static const uint16_t kVerifySignatureAlgorithms[] = {
447 /* For now, do not enable RSA-PSS signature algorithms on Android's system
448 * BoringSSL. Once TLS 1.3 is finalized and the change in Chrome has stuck,
449 * restore them. */
450#if !defined(BORINGSSL_ANDROID_SYSTEM)
451 SSL_SIGN_RSA_PSS_SHA512,
452#endif
453 SSL_SIGN_RSA_PKCS1_SHA512,
454 /* TODO(davidben): Remove this entry and SSL_CURVE_SECP521R1 from
455 * kDefaultGroups. */
456#if defined(BORINGSSL_ANDROID_SYSTEM)
457 SSL_SIGN_ECDSA_SECP521R1_SHA512,
458#endif
Adam Langley95c29f32014-06-20 12:00:00 -0700459
David Benjamin3ef76972016-10-17 17:59:54 -0400460#if !defined(BORINGSSL_ANDROID_SYSTEM)
461 SSL_SIGN_RSA_PSS_SHA384,
462#endif
463 SSL_SIGN_RSA_PKCS1_SHA384,
464 SSL_SIGN_ECDSA_SECP384R1_SHA384,
465
466#if !defined(BORINGSSL_ANDROID_SYSTEM)
467 SSL_SIGN_RSA_PSS_SHA256,
468#endif
469 SSL_SIGN_RSA_PKCS1_SHA256,
470 SSL_SIGN_ECDSA_SECP256R1_SHA256,
471
472 SSL_SIGN_RSA_PKCS1_SHA1,
473};
474
475/* kSignSignatureAlgorithms is the default list of supported signature
476 * algorithms for signing. */
477static const uint16_t kSignSignatureAlgorithms[] = {
478 /* For now, do not enable RSA-PSS signature algorithms on Android's system
David Benjamin57e929f2016-08-30 00:30:38 -0400479 * BoringSSL. Once TLS 1.3 is finalized and the change in Chrome has stuck,
480 * restore them. */
481#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400482 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400483#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400484 SSL_SIGN_RSA_PKCS1_SHA512,
485 SSL_SIGN_ECDSA_SECP521R1_SHA512,
486
David Benjamin57e929f2016-08-30 00:30:38 -0400487#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400488 SSL_SIGN_RSA_PSS_SHA384,
David Benjamin57e929f2016-08-30 00:30:38 -0400489#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400490 SSL_SIGN_RSA_PKCS1_SHA384,
491 SSL_SIGN_ECDSA_SECP384R1_SHA384,
492
David Benjamin57e929f2016-08-30 00:30:38 -0400493#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400494 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400495#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400496 SSL_SIGN_RSA_PKCS1_SHA256,
497 SSL_SIGN_ECDSA_SECP256R1_SHA256,
498
499 SSL_SIGN_RSA_PKCS1_SHA1,
500 SSL_SIGN_ECDSA_SHA1,
501};
502
David Benjamin3ef76972016-10-17 17:59:54 -0400503size_t tls12_get_verify_sigalgs(const SSL *ssl, const uint16_t **out) {
504 *out = kVerifySignatureAlgorithms;
505 return OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
Adam Langleyfcf25832014-12-18 17:42:32 -0800506}
Adam Langley95c29f32014-06-20 12:00:00 -0700507
David Benjamin887c3002016-07-08 16:15:32 -0700508int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
David Benjamin3ef76972016-10-17 17:59:54 -0400509 const uint16_t *verify_sigalgs;
510 size_t num_verify_sigalgs = tls12_get_verify_sigalgs(ssl, &verify_sigalgs);
511 for (size_t i = 0; i < num_verify_sigalgs; i++) {
512 if (sigalg == verify_sigalgs[i]) {
513 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800514 }
515 }
516
David Benjamin3ef76972016-10-17 17:59:54 -0400517 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
518 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
519 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800520}
521
522/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
523 * supported or doesn't appear in supported signature algorithms. Unlike
524 * ssl_cipher_get_disabled this applies to a specific session and not global
525 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500526void ssl_set_client_disabled(SSL *ssl) {
527 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 int have_rsa = 0, have_ecdsa = 0;
529 c->mask_a = 0;
530 c->mask_k = 0;
531
David Benjamin3ef76972016-10-17 17:59:54 -0400532 /* Now go through all signature algorithms seeing if we support any for RSA or
533 * ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin0fc37ef2016-08-17 15:29:46 -0400534 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -0400535 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
David Benjamin0fc37ef2016-08-17 15:29:46 -0400536 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -0400537 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400538 case SSL_SIGN_RSA_PSS_SHA512:
539 case SSL_SIGN_RSA_PSS_SHA384:
540 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400541 case SSL_SIGN_RSA_PKCS1_SHA512:
542 case SSL_SIGN_RSA_PKCS1_SHA384:
543 case SSL_SIGN_RSA_PKCS1_SHA256:
544 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 have_rsa = 1;
546 break;
547
Steven Valdez02563852016-06-23 13:33:05 -0400548 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
549 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
550 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
551 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800552 have_ecdsa = 1;
553 break;
554 }
555 }
556
557 /* Disable auth if we don't include any appropriate signature algorithms. */
558 if (!have_rsa) {
559 c->mask_a |= SSL_aRSA;
560 }
561 if (!have_ecdsa) {
562 c->mask_a |= SSL_aECDSA;
563 }
564
565 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500566 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800567 c->mask_a |= SSL_aPSK;
568 c->mask_k |= SSL_kPSK;
569 }
570}
Adam Langley95c29f32014-06-20 12:00:00 -0700571
Adam Langley614c66a2015-06-12 15:26:58 -0700572/* tls_extension represents a TLS extension that is handled internally. The
573 * |init| function is called for each handshake, before any other functions of
574 * the extension. Then the add and parse callbacks are called as needed.
575 *
576 * The parse callbacks receive a |CBS| that contains the contents of the
577 * extension (i.e. not including the type and length bytes). If an extension is
578 * not received then the parse callbacks will be called with a NULL CBS so that
579 * they can do any processing needed to handle the absence of an extension.
580 *
581 * The add callbacks receive a |CBB| to which the extension can be appended but
582 * the function is responsible for appending the type and length bytes too.
583 *
584 * All callbacks return one for success and zero for error. If a parse function
585 * returns zero then a fatal alert with value |*out_alert| will be sent. If
586 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
587struct tls_extension {
588 uint16_t value;
589 void (*init)(SSL *ssl);
590
591 int (*add_clienthello)(SSL *ssl, CBB *out);
592 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
593
594 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
595 int (*add_serverhello)(SSL *ssl, CBB *out);
596};
597
Steven Valdez6b8509a2016-07-12 13:38:32 -0400598static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
599 if (contents != NULL) {
600 /* Servers MUST NOT send this extension. */
601 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
602 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
603 return 0;
604 }
605
606 return 1;
607}
608
609static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
610 /* This extension from the client is handled elsewhere. */
611 return 1;
612}
613
614static int dont_add_serverhello(SSL *ssl, CBB *out) {
615 return 1;
616}
Adam Langley614c66a2015-06-12 15:26:58 -0700617
618/* Server name indication (SNI).
619 *
620 * https://tools.ietf.org/html/rfc6066#section-3. */
621
Adam Langley614c66a2015-06-12 15:26:58 -0700622static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
623 if (ssl->tlsext_hostname == NULL) {
624 return 1;
625 }
626
627 CBB contents, server_name_list, name;
628 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
629 !CBB_add_u16_length_prefixed(out, &contents) ||
630 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
631 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
632 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
633 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
634 strlen(ssl->tlsext_hostname)) ||
635 !CBB_flush(out)) {
636 return 0;
637 }
638
639 return 1;
640}
641
David Benjamin0d56f882015-12-19 17:05:56 -0500642static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
643 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700644 if (contents == NULL) {
645 return 1;
646 }
647
648 if (CBS_len(contents) != 0) {
649 return 0;
650 }
651
652 assert(ssl->tlsext_hostname != NULL);
653
Steven Valdez87eab492016-06-27 16:34:59 -0400654 if (ssl->session == NULL) {
655 assert(ssl->s3->new_session->tlsext_hostname == NULL);
656 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
657 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700658 *out_alert = SSL_AD_INTERNAL_ERROR;
659 return 0;
660 }
661 }
662
663 return 1;
664}
665
David Benjamin0d56f882015-12-19 17:05:56 -0500666static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
667 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700668 if (contents == NULL) {
669 return 1;
670 }
671
David Benjamin9b611e22016-03-03 08:48:30 -0500672 CBS server_name_list, host_name;
673 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700674 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500675 !CBS_get_u8(&server_name_list, &name_type) ||
676 /* Although the server_name extension was intended to be extensible to
677 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
678 * different name types will cause an error. Further, RFC 4366 originally
679 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
680 * adding new name types is no longer feasible.
681 *
682 * Act as if the extensibility does not exist to simplify parsing. */
683 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
684 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700685 CBS_len(contents) != 0) {
686 return 0;
687 }
688
David Benjamin9b611e22016-03-03 08:48:30 -0500689 if (name_type != TLSEXT_NAMETYPE_host_name ||
690 CBS_len(&host_name) == 0 ||
691 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
692 CBS_contains_zero_byte(&host_name)) {
693 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
694 return 0;
695 }
Adam Langley614c66a2015-06-12 15:26:58 -0700696
David Benjamin9b611e22016-03-03 08:48:30 -0500697 /* TODO(davidben): SNI should be resolved before resumption. We have the
698 * early callback as a replacement, but we should fix the current callback
699 * and avoid the need for |SSL_CTX_set_session_id_context|. */
Steven Valdez87eab492016-06-27 16:34:59 -0400700 if (ssl->session == NULL) {
701 assert(ssl->s3->new_session->tlsext_hostname == NULL);
David Benjamin9b611e22016-03-03 08:48:30 -0500702
703 /* Copy the hostname as a string. */
Steven Valdez87eab492016-06-27 16:34:59 -0400704 if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) {
David Benjamin9b611e22016-03-03 08:48:30 -0500705 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700706 return 0;
707 }
708
David Benjamina0486782016-10-06 19:11:32 -0400709 ssl->s3->hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700710 }
711
712 return 1;
713}
714
715static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez87eab492016-06-27 16:34:59 -0400716 if (ssl->session != NULL ||
David Benjamina0486782016-10-06 19:11:32 -0400717 !ssl->s3->hs->should_ack_sni ||
Steven Valdez87eab492016-06-27 16:34:59 -0400718 ssl->s3->new_session->tlsext_hostname == NULL) {
Adam Langley614c66a2015-06-12 15:26:58 -0700719 return 1;
720 }
721
722 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
723 !CBB_add_u16(out, 0 /* length */)) {
724 return 0;
725 }
726
727 return 1;
728}
729
730
Adam Langley5021b222015-06-12 18:27:58 -0700731/* Renegotiation indication.
732 *
733 * https://tools.ietf.org/html/rfc5746 */
734
735static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400736 uint16_t min_version, max_version;
737 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
738 return 0;
739 }
740
741 /* Renegotiation indication is not necessary in TLS 1.3. */
742 if (min_version >= TLS1_3_VERSION) {
743 return 1;
744 }
745
David Benjamin52bf6902016-10-08 12:05:03 -0400746 assert(ssl->s3->initial_handshake_complete ==
747 (ssl->s3->previous_client_finished_len != 0));
748
Adam Langley5021b222015-06-12 18:27:58 -0700749 CBB contents, prev_finished;
750 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
751 !CBB_add_u16_length_prefixed(out, &contents) ||
752 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
753 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
754 ssl->s3->previous_client_finished_len) ||
755 !CBB_flush(out)) {
756 return 0;
757 }
758
759 return 1;
760}
761
762static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
763 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400764 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
765 return 0;
766 }
767
David Benjamin3e052de2015-11-25 20:10:31 -0500768 /* Servers may not switch between omitting the extension and supporting it.
769 * See RFC 5746, sections 3.5 and 4.2. */
770 if (ssl->s3->initial_handshake_complete &&
771 (contents != NULL) != ssl->s3->send_connection_binding) {
772 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
773 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
774 return 0;
775 }
776
Adam Langley5021b222015-06-12 18:27:58 -0700777 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500778 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700779 * RI even on initial ServerHello because the client doesn't see any
780 * renegotiation during an attack. However this would mean we could not
781 * connect to any server which doesn't support RI.
782 *
David Benjamine9cddb82015-11-23 14:36:40 -0500783 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
784 * practical terms every client sets it so it's just assumed here. */
785 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700786 }
787
788 const size_t expected_len = ssl->s3->previous_client_finished_len +
789 ssl->s3->previous_server_finished_len;
790
791 /* Check for logic errors */
792 assert(!expected_len || ssl->s3->previous_client_finished_len);
793 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400794 assert(ssl->s3->initial_handshake_complete ==
795 (ssl->s3->previous_client_finished_len != 0));
796 assert(ssl->s3->initial_handshake_complete ==
797 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700798
799 /* Parse out the extension contents. */
800 CBS renegotiated_connection;
801 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
802 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400803 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700804 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
805 return 0;
806 }
807
808 /* Check that the extension matches. */
809 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400810 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700811 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
812 return 0;
813 }
814
815 const uint8_t *d = CBS_data(&renegotiated_connection);
816 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
817 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400818 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700819 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
820 return 0;
821 }
822 d += ssl->s3->previous_client_finished_len;
823
824 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
825 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400826 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700827 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
828 return 0;
829 }
830 ssl->s3->send_connection_binding = 1;
831
832 return 1;
833}
834
835static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
836 CBS *contents) {
837 /* Renegotiation isn't supported as a server so this function should never be
838 * called after the initial handshake. */
839 assert(!ssl->s3->initial_handshake_complete);
840
Steven Valdez143e8b32016-07-11 13:19:03 -0400841 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
842 return 1;
843 }
844
Adam Langley5021b222015-06-12 18:27:58 -0700845 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400846 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700847 }
848
849 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700850 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
851 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400852 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700853 return 0;
854 }
855
David Benjamin52bf6902016-10-08 12:05:03 -0400856 /* Check that the extension matches. We do not support renegotiation as a
857 * server, so this must be empty. */
858 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400859 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700860 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
861 return 0;
862 }
863
864 ssl->s3->send_connection_binding = 1;
865
866 return 1;
867}
868
869static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin52bf6902016-10-08 12:05:03 -0400870 /* Renegotiation isn't supported as a server so this function should never be
871 * called after the initial handshake. */
872 assert(!ssl->s3->initial_handshake_complete);
873
Steven Valdez143e8b32016-07-11 13:19:03 -0400874 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
875 return 1;
876 }
877
Adam Langley5021b222015-06-12 18:27:58 -0700878 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400879 !CBB_add_u16(out, 1 /* length */) ||
880 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700881 return 0;
882 }
883
884 return 1;
885}
886
Adam Langley0a056712015-07-01 15:03:33 -0700887
888/* Extended Master Secret.
889 *
David Benjamin43946d42016-02-01 08:42:19 -0500890 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700891
Adam Langley0a056712015-07-01 15:03:33 -0700892static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400893 uint16_t min_version, max_version;
894 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
895 return 0;
896 }
897
898 /* Extended master secret is not necessary in TLS 1.3. */
899 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700900 return 1;
901 }
902
903 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
904 !CBB_add_u16(out, 0 /* length */)) {
905 return 0;
906 }
907
908 return 1;
909}
910
911static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
912 CBS *contents) {
David Benjamin163c9562016-08-29 23:14:17 -0400913 /* Whether EMS is negotiated may not change on renegotation. */
914 if (ssl->s3->initial_handshake_complete) {
915 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
916 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
917 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
918 return 0;
919 }
920
921 return 1;
922 }
923
Adam Langley0a056712015-07-01 15:03:33 -0700924 if (contents == NULL) {
925 return 1;
926 }
927
Steven Valdez143e8b32016-07-11 13:19:03 -0400928 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
929 ssl->version == SSL3_VERSION) {
930 return 0;
931 }
932
933 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700934 return 0;
935 }
936
937 ssl->s3->tmp.extended_master_secret = 1;
938 return 1;
939}
940
David Benjamin0d56f882015-12-19 17:05:56 -0500941static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
942 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400943 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
944 ssl->version == SSL3_VERSION) {
945 return 1;
946 }
947
948 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700949 return 1;
950 }
951
952 if (CBS_len(contents) != 0) {
953 return 0;
954 }
955
956 ssl->s3->tmp.extended_master_secret = 1;
957 return 1;
958}
959
960static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
961 if (!ssl->s3->tmp.extended_master_secret) {
962 return 1;
963 }
964
965 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
966 !CBB_add_u16(out, 0 /* length */)) {
967 return 0;
968 }
969
970 return 1;
971}
972
Adam Langley9b05bc52015-07-01 15:25:33 -0700973
974/* Session tickets.
975 *
976 * https://tools.ietf.org/html/rfc5077 */
977
978static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400979 uint16_t min_version, max_version;
980 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
981 return 0;
982 }
983
984 /* TLS 1.3 uses a different ticket extension. */
985 if (min_version >= TLS1_3_VERSION ||
986 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700987 return 1;
988 }
989
990 const uint8_t *ticket_data = NULL;
991 int ticket_len = 0;
992
993 /* Renegotiation does not participate in session resumption. However, still
994 * advertise the extension to avoid potentially breaking servers which carry
995 * over the state from the previous handshake, such as OpenSSL servers
996 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -0400997 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -0700998 if (!ssl->s3->initial_handshake_complete &&
999 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001000 ssl->session->tlsext_tick != NULL &&
1001 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -04001002 ssl->method->version_from_wire(&session_version,
1003 ssl->session->ssl_version) &&
1004 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001005 ticket_data = ssl->session->tlsext_tick;
1006 ticket_len = ssl->session->tlsext_ticklen;
1007 }
1008
1009 CBB ticket;
1010 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1011 !CBB_add_u16_length_prefixed(out, &ticket) ||
1012 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1013 !CBB_flush(out)) {
1014 return 0;
1015 }
1016
1017 return 1;
1018}
1019
1020static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1021 CBS *contents) {
1022 ssl->tlsext_ticket_expected = 0;
1023
1024 if (contents == NULL) {
1025 return 1;
1026 }
1027
Steven Valdez143e8b32016-07-11 13:19:03 -04001028 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1029 return 0;
1030 }
1031
Adam Langley9b05bc52015-07-01 15:25:33 -07001032 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1033 * this function should never be called, even if the server tries to send the
1034 * extension. */
1035 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1036
1037 if (CBS_len(contents) != 0) {
1038 return 0;
1039 }
1040
1041 ssl->tlsext_ticket_expected = 1;
1042 return 1;
1043}
1044
Adam Langley9b05bc52015-07-01 15:25:33 -07001045static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1046 if (!ssl->tlsext_ticket_expected) {
1047 return 1;
1048 }
1049
1050 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1051 * true. */
1052 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1053
1054 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1055 !CBB_add_u16(out, 0 /* length */)) {
1056 return 0;
1057 }
1058
1059 return 1;
1060}
1061
1062
Adam Langley2e857bd2015-07-01 16:09:19 -07001063/* Signature Algorithms.
1064 *
1065 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1066
1067static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin2dc02042016-09-19 19:57:37 -04001068 uint16_t min_version, max_version;
1069 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1070 return 0;
1071 }
1072
1073 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001074 return 1;
1075 }
1076
David Benjamin0fc37ef2016-08-17 15:29:46 -04001077 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001078 const size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001079
David Benjamin0fc37ef2016-08-17 15:29:46 -04001080 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001081 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1082 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001083 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001084 return 0;
1085 }
1086
David Benjamin0fc37ef2016-08-17 15:29:46 -04001087 for (size_t i = 0; i < num_sigalgs; i++) {
1088 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001089 return 0;
1090 }
1091 }
1092
1093 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001094 return 0;
1095 }
1096
1097 return 1;
1098}
1099
Adam Langley2e857bd2015-07-01 16:09:19 -07001100static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1101 CBS *contents) {
David Benjamin0fc37ef2016-08-17 15:29:46 -04001102 OPENSSL_free(ssl->s3->hs->peer_sigalgs);
1103 ssl->s3->hs->peer_sigalgs = NULL;
1104 ssl->s3->hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001105
Adam Langley2e857bd2015-07-01 16:09:19 -07001106 if (contents == NULL) {
1107 return 1;
1108 }
1109
1110 CBS supported_signature_algorithms;
1111 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001112 CBS_len(contents) != 0 ||
1113 CBS_len(&supported_signature_algorithms) == 0 ||
1114 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001115 return 0;
1116 }
1117
1118 return 1;
1119}
1120
Adam Langley2e857bd2015-07-01 16:09:19 -07001121
Adam Langleybb0bd042015-07-01 16:21:03 -07001122/* OCSP Stapling.
1123 *
1124 * https://tools.ietf.org/html/rfc6066#section-8 */
1125
1126static void ext_ocsp_init(SSL *ssl) {
Adam Langleyce9d85e2016-01-24 15:58:39 -08001127 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001128}
1129
1130static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1131 if (!ssl->ocsp_stapling_enabled) {
1132 return 1;
1133 }
1134
1135 CBB contents;
1136 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1137 !CBB_add_u16_length_prefixed(out, &contents) ||
1138 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1139 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1140 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1141 !CBB_flush(out)) {
1142 return 0;
1143 }
1144
Adam Langleyce9d85e2016-01-24 15:58:39 -08001145 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001146 return 1;
1147}
1148
1149static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001150 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001151 if (contents == NULL) {
1152 return 1;
1153 }
1154
David Benjamin942f4ed2016-07-16 19:03:49 +03001155 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001156 /* OCSP stapling is forbidden on non-certificate ciphers. */
1157 if (CBS_len(contents) != 0 ||
1158 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001159 return 0;
1160 }
1161
David Benjamindaa88502016-10-04 16:32:16 -04001162 /* Note this does not check for resumption in TLS 1.2. Sending
1163 * status_request here does not make sense, but OpenSSL does so and the
1164 * specification does not say anything. Tolerate it but ignore it. */
1165
David Benjamina0486782016-10-06 19:11:32 -04001166 ssl->s3->hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001167 return 1;
1168 }
1169
Steven Valdez803c77a2016-09-06 14:13:43 -04001170 /* In TLS 1.3, OCSP stapling is forbidden on resumption. */
1171 if (ssl->s3->session_reused) {
1172 return 0;
1173 }
1174
David Benjamin942f4ed2016-07-16 19:03:49 +03001175 uint8_t status_type;
1176 CBS ocsp_response;
1177 if (!CBS_get_u8(contents, &status_type) ||
1178 status_type != TLSEXT_STATUSTYPE_ocsp ||
1179 !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1180 CBS_len(&ocsp_response) == 0 ||
1181 CBS_len(contents) != 0) {
1182 return 0;
1183 }
1184
Steven Valdez87eab492016-06-27 16:34:59 -04001185 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1186 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001187 *out_alert = SSL_AD_INTERNAL_ERROR;
1188 return 0;
1189 }
1190
Adam Langleybb0bd042015-07-01 16:21:03 -07001191 return 1;
1192}
1193
1194static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1195 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001196 if (contents == NULL) {
1197 return 1;
1198 }
1199
1200 uint8_t status_type;
1201 if (!CBS_get_u8(contents, &status_type)) {
1202 return 0;
1203 }
1204
1205 /* We cannot decide whether OCSP stapling will occur yet because the correct
1206 * SSL_CTX might not have been selected. */
David Benjamina0486782016-10-06 19:11:32 -04001207 ssl->s3->hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001208
Adam Langleybb0bd042015-07-01 16:21:03 -07001209 return 1;
1210}
1211
1212static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
David Benjamina0486782016-10-06 19:11:32 -04001213 if (!ssl->s3->hs->ocsp_stapling_requested ||
David Benjamin942f4ed2016-07-16 19:03:49 +03001214 ssl->ctx->ocsp_response_length == 0 ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001215 ssl->s3->session_reused ||
1216 (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
1217 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001218 return 1;
1219 }
1220
David Benjamin942f4ed2016-07-16 19:03:49 +03001221 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1222 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez87eab492016-06-27 16:34:59 -04001223 if (ssl->session != NULL) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001224 return 1;
1225 }
1226
David Benjamina0486782016-10-06 19:11:32 -04001227 ssl->s3->hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001228
1229 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1230 CBB_add_u16(out, 0 /* length */);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001231 }
1232
David Benjamin942f4ed2016-07-16 19:03:49 +03001233 CBB body, ocsp_response;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001234 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
David Benjamin942f4ed2016-07-16 19:03:49 +03001235 CBB_add_u16_length_prefixed(out, &body) &&
1236 CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1237 CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1238 CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1239 ssl->ctx->ocsp_response_length) &&
1240 CBB_flush(out);
Adam Langleybb0bd042015-07-01 16:21:03 -07001241}
1242
1243
Adam Langley97dfcbf2015-07-01 18:35:20 -07001244/* Next protocol negotiation.
1245 *
1246 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1247
Adam Langley97dfcbf2015-07-01 18:35:20 -07001248static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1249 if (ssl->s3->initial_handshake_complete ||
1250 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001251 (ssl->options & SSL_OP_DISABLE_NPN) ||
David Benjamince079fd2016-08-02 16:22:34 -04001252 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001253 return 1;
1254 }
1255
1256 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1257 !CBB_add_u16(out, 0 /* length */)) {
1258 return 0;
1259 }
1260
1261 return 1;
1262}
1263
1264static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1265 CBS *contents) {
1266 if (contents == NULL) {
1267 return 1;
1268 }
1269
Steven Valdez143e8b32016-07-11 13:19:03 -04001270 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1271 return 0;
1272 }
1273
Adam Langley97dfcbf2015-07-01 18:35:20 -07001274 /* If any of these are false then we should never have sent the NPN
1275 * extension in the ClientHello and thus this function should never have been
1276 * called. */
1277 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001278 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001279 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001280 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001281
David Benjamin76c2efc2015-08-31 14:24:29 -04001282 if (ssl->s3->alpn_selected != NULL) {
1283 /* NPN and ALPN may not be negotiated in the same connection. */
1284 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1285 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1286 return 0;
1287 }
1288
Adam Langley97dfcbf2015-07-01 18:35:20 -07001289 const uint8_t *const orig_contents = CBS_data(contents);
1290 const size_t orig_len = CBS_len(contents);
1291
1292 while (CBS_len(contents) != 0) {
1293 CBS proto;
1294 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1295 CBS_len(&proto) == 0) {
1296 return 0;
1297 }
1298 }
1299
1300 uint8_t *selected;
1301 uint8_t selected_len;
1302 if (ssl->ctx->next_proto_select_cb(
1303 ssl, &selected, &selected_len, orig_contents, orig_len,
1304 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1305 *out_alert = SSL_AD_INTERNAL_ERROR;
1306 return 0;
1307 }
1308
David Benjamin79978df2015-12-25 15:56:49 -05001309 OPENSSL_free(ssl->s3->next_proto_negotiated);
1310 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1311 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001312 *out_alert = SSL_AD_INTERNAL_ERROR;
1313 return 0;
1314 }
1315
David Benjamin79978df2015-12-25 15:56:49 -05001316 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjaminb74b0812016-10-06 19:43:48 -04001317 ssl->s3->hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001318
1319 return 1;
1320}
1321
1322static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1323 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001324 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1325 return 1;
1326 }
1327
Adam Langley97dfcbf2015-07-01 18:35:20 -07001328 if (contents != NULL && CBS_len(contents) != 0) {
1329 return 0;
1330 }
1331
1332 if (contents == NULL ||
1333 ssl->s3->initial_handshake_complete ||
1334 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1335 * afterwards, parsing the ALPN extension will clear
1336 * |next_proto_neg_seen|. */
1337 ssl->s3->alpn_selected != NULL ||
1338 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001339 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001340 return 1;
1341 }
1342
David Benjaminb74b0812016-10-06 19:43:48 -04001343 ssl->s3->hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001344 return 1;
1345}
1346
1347static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1348 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1349 * parsed. */
David Benjaminb74b0812016-10-06 19:43:48 -04001350 if (!ssl->s3->hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001351 return 1;
1352 }
1353
1354 const uint8_t *npa;
1355 unsigned npa_len;
1356
1357 if (ssl->ctx->next_protos_advertised_cb(
1358 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1359 SSL_TLSEXT_ERR_OK) {
David Benjaminb74b0812016-10-06 19:43:48 -04001360 ssl->s3->hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001361 return 1;
1362 }
1363
1364 CBB contents;
1365 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1366 !CBB_add_u16_length_prefixed(out, &contents) ||
1367 !CBB_add_bytes(&contents, npa, npa_len) ||
1368 !CBB_flush(out)) {
1369 return 0;
1370 }
1371
1372 return 1;
1373}
1374
1375
Adam Langleyab8d87d2015-07-10 12:21:39 -07001376/* Signed certificate timestamps.
1377 *
1378 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1379
1380static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1381 if (!ssl->signed_cert_timestamps_enabled) {
1382 return 1;
1383 }
1384
1385 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1386 !CBB_add_u16(out, 0 /* length */)) {
1387 return 0;
1388 }
1389
1390 return 1;
1391}
1392
1393static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1394 CBS *contents) {
1395 if (contents == NULL) {
1396 return 1;
1397 }
1398
1399 /* If this is false then we should never have sent the SCT extension in the
1400 * ClientHello and thus this function should never have been called. */
1401 assert(ssl->signed_cert_timestamps_enabled);
1402
1403 if (CBS_len(contents) == 0) {
1404 *out_alert = SSL_AD_DECODE_ERROR;
1405 return 0;
1406 }
1407
David Benjamindaa88502016-10-04 16:32:16 -04001408 /* Session resumption uses the original session information. The extension
1409 * should not be sent on resumption, but RFC 6962 did not make it a
1410 * requirement, so tolerate this.
1411 *
1412 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001413 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001414 !CBS_stow(
1415 contents,
1416 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1417 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001418 *out_alert = SSL_AD_INTERNAL_ERROR;
1419 return 0;
1420 }
1421
1422 return 1;
1423}
1424
1425static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1426 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001427 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001428}
1429
1430static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001431 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001432 if (ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001433 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001434 return 1;
1435 }
1436
1437 CBB contents;
1438 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1439 CBB_add_u16_length_prefixed(out, &contents) &&
1440 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1441 ssl->ctx->signed_cert_timestamp_list_length) &&
1442 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001443}
1444
1445
Adam Langleyf18e4532015-07-10 13:39:53 -07001446/* Application-level Protocol Negotiation.
1447 *
1448 * https://tools.ietf.org/html/rfc7301 */
1449
1450static void ext_alpn_init(SSL *ssl) {
1451 OPENSSL_free(ssl->s3->alpn_selected);
1452 ssl->s3->alpn_selected = NULL;
1453}
1454
1455static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1456 if (ssl->alpn_client_proto_list == NULL ||
1457 ssl->s3->initial_handshake_complete) {
1458 return 1;
1459 }
1460
1461 CBB contents, proto_list;
1462 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1463 !CBB_add_u16_length_prefixed(out, &contents) ||
1464 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1465 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1466 ssl->alpn_client_proto_list_len) ||
1467 !CBB_flush(out)) {
1468 return 0;
1469 }
1470
1471 return 1;
1472}
1473
1474static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1475 CBS *contents) {
1476 if (contents == NULL) {
1477 return 1;
1478 }
1479
1480 assert(!ssl->s3->initial_handshake_complete);
1481 assert(ssl->alpn_client_proto_list != NULL);
1482
David Benjaminb74b0812016-10-06 19:43:48 -04001483 if (ssl->s3->hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001484 /* NPN and ALPN may not be negotiated in the same connection. */
1485 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1486 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1487 return 0;
1488 }
1489
Adam Langleyf18e4532015-07-10 13:39:53 -07001490 /* The extension data consists of a ProtocolNameList which must have
1491 * exactly one ProtocolName. Each of these is length-prefixed. */
1492 CBS protocol_name_list, protocol_name;
1493 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1494 CBS_len(contents) != 0 ||
1495 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1496 /* Empty protocol names are forbidden. */
1497 CBS_len(&protocol_name) == 0 ||
1498 CBS_len(&protocol_name_list) != 0) {
1499 return 0;
1500 }
1501
David Benjamin3e517572016-08-11 11:52:23 -04001502 /* Check that the protcol name is one of the ones we advertised. */
1503 int protocol_ok = 0;
1504 CBS client_protocol_name_list, client_protocol_name;
1505 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1506 ssl->alpn_client_proto_list_len);
1507 while (CBS_len(&client_protocol_name_list) > 0) {
1508 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1509 &client_protocol_name)) {
1510 *out_alert = SSL_AD_INTERNAL_ERROR;
1511 return 0;
1512 }
1513
1514 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1515 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1516 CBS_len(&protocol_name)) == 0) {
1517 protocol_ok = 1;
1518 break;
1519 }
1520 }
1521
1522 if (!protocol_ok) {
1523 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1524 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1525 return 0;
1526 }
1527
Adam Langleyf18e4532015-07-10 13:39:53 -07001528 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1529 &ssl->s3->alpn_selected_len)) {
1530 *out_alert = SSL_AD_INTERNAL_ERROR;
1531 return 0;
1532 }
1533
1534 return 1;
1535}
1536
1537static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1538 CBS *contents) {
1539 if (contents == NULL) {
1540 return 1;
1541 }
1542
1543 if (ssl->ctx->alpn_select_cb == NULL ||
1544 ssl->s3->initial_handshake_complete) {
1545 return 1;
1546 }
1547
1548 /* ALPN takes precedence over NPN. */
David Benjaminb74b0812016-10-06 19:43:48 -04001549 ssl->s3->hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001550
1551 CBS protocol_name_list;
1552 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1553 CBS_len(contents) != 0 ||
1554 CBS_len(&protocol_name_list) < 2) {
1555 return 0;
1556 }
1557
1558 /* Validate the protocol list. */
1559 CBS protocol_name_list_copy = protocol_name_list;
1560 while (CBS_len(&protocol_name_list_copy) > 0) {
1561 CBS protocol_name;
1562
1563 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1564 /* Empty protocol names are forbidden. */
1565 CBS_len(&protocol_name) == 0) {
1566 return 0;
1567 }
1568 }
1569
1570 const uint8_t *selected;
1571 uint8_t selected_len;
1572 if (ssl->ctx->alpn_select_cb(
1573 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1574 CBS_len(&protocol_name_list),
1575 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1576 OPENSSL_free(ssl->s3->alpn_selected);
1577 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1578 if (ssl->s3->alpn_selected == NULL) {
1579 *out_alert = SSL_AD_INTERNAL_ERROR;
1580 return 0;
1581 }
1582 ssl->s3->alpn_selected_len = selected_len;
1583 }
1584
1585 return 1;
1586}
1587
1588static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1589 if (ssl->s3->alpn_selected == NULL) {
1590 return 1;
1591 }
1592
1593 CBB contents, proto_list, proto;
1594 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1595 !CBB_add_u16_length_prefixed(out, &contents) ||
1596 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1597 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001598 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1599 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001600 !CBB_flush(out)) {
1601 return 0;
1602 }
1603
1604 return 1;
1605}
1606
1607
Adam Langley49c7af12015-07-10 14:33:46 -07001608/* Channel ID.
1609 *
1610 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1611
1612static void ext_channel_id_init(SSL *ssl) {
1613 ssl->s3->tlsext_channel_id_valid = 0;
1614}
1615
1616static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1617 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001618 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001619 return 1;
1620 }
1621
1622 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1623 !CBB_add_u16(out, 0 /* length */)) {
1624 return 0;
1625 }
1626
1627 return 1;
1628}
1629
1630static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1631 CBS *contents) {
1632 if (contents == NULL) {
1633 return 1;
1634 }
1635
David Benjamince079fd2016-08-02 16:22:34 -04001636 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001637 assert(ssl->tlsext_channel_id_enabled);
1638
1639 if (CBS_len(contents) != 0) {
1640 return 0;
1641 }
1642
1643 ssl->s3->tlsext_channel_id_valid = 1;
1644 return 1;
1645}
1646
1647static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1648 CBS *contents) {
1649 if (contents == NULL ||
1650 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001651 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001652 return 1;
1653 }
1654
1655 if (CBS_len(contents) != 0) {
1656 return 0;
1657 }
1658
1659 ssl->s3->tlsext_channel_id_valid = 1;
1660 return 1;
1661}
1662
1663static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1664 if (!ssl->s3->tlsext_channel_id_valid) {
1665 return 1;
1666 }
1667
1668 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1669 !CBB_add_u16(out, 0 /* length */)) {
1670 return 0;
1671 }
1672
1673 return 1;
1674}
1675
Adam Langley391250d2015-07-15 19:06:07 -07001676
1677/* Secure Real-time Transport Protocol (SRTP) extension.
1678 *
1679 * https://tools.ietf.org/html/rfc5764 */
1680
Adam Langley391250d2015-07-15 19:06:07 -07001681
1682static void ext_srtp_init(SSL *ssl) {
1683 ssl->srtp_profile = NULL;
1684}
1685
1686static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1687 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1688 if (profiles == NULL) {
1689 return 1;
1690 }
1691 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1692 if (num_profiles == 0) {
1693 return 1;
1694 }
1695
1696 CBB contents, profile_ids;
1697 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1698 !CBB_add_u16_length_prefixed(out, &contents) ||
1699 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1700 return 0;
1701 }
1702
David Benjamin54091232016-09-05 12:47:25 -04001703 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001704 if (!CBB_add_u16(&profile_ids,
1705 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1706 return 0;
1707 }
1708 }
1709
1710 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1711 !CBB_flush(out)) {
1712 return 0;
1713 }
1714
1715 return 1;
1716}
1717
1718static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1719 CBS *contents) {
1720 if (contents == NULL) {
1721 return 1;
1722 }
1723
1724 /* The extension consists of a u16-prefixed profile ID list containing a
1725 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1726 *
1727 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1728 CBS profile_ids, srtp_mki;
1729 uint16_t profile_id;
1730 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1731 !CBS_get_u16(&profile_ids, &profile_id) ||
1732 CBS_len(&profile_ids) != 0 ||
1733 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1734 CBS_len(contents) != 0) {
1735 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1736 return 0;
1737 }
1738
1739 if (CBS_len(&srtp_mki) != 0) {
1740 /* Must be no MKI, since we never offer one. */
1741 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1742 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1743 return 0;
1744 }
1745
1746 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1747
1748 /* Check to see if the server gave us something we support (and presumably
1749 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001750 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001751 const SRTP_PROTECTION_PROFILE *profile =
1752 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1753
1754 if (profile->id == profile_id) {
1755 ssl->srtp_profile = profile;
1756 return 1;
1757 }
1758 }
1759
1760 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1761 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1762 return 0;
1763}
1764
1765static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1766 CBS *contents) {
1767 if (contents == NULL) {
1768 return 1;
1769 }
1770
1771 CBS profile_ids, srtp_mki;
1772 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1773 CBS_len(&profile_ids) < 2 ||
1774 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1775 CBS_len(contents) != 0) {
1776 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1777 return 0;
1778 }
1779 /* Discard the MKI value for now. */
1780
1781 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1782 SSL_get_srtp_profiles(ssl);
1783
1784 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001785 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001786 const SRTP_PROTECTION_PROFILE *server_profile =
1787 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1788
1789 CBS profile_ids_tmp;
1790 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1791
1792 while (CBS_len(&profile_ids_tmp) > 0) {
1793 uint16_t profile_id;
1794 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1795 return 0;
1796 }
1797
1798 if (server_profile->id == profile_id) {
1799 ssl->srtp_profile = server_profile;
1800 return 1;
1801 }
1802 }
1803 }
1804
1805 return 1;
1806}
1807
1808static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1809 if (ssl->srtp_profile == NULL) {
1810 return 1;
1811 }
1812
1813 CBB contents, profile_ids;
1814 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1815 !CBB_add_u16_length_prefixed(out, &contents) ||
1816 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1817 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1818 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1819 !CBB_flush(out)) {
1820 return 0;
1821 }
1822
1823 return 1;
1824}
1825
Adam Langleybdd5d662015-07-20 16:19:08 -07001826
1827/* EC point formats.
1828 *
1829 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1830
1831static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
David Benjamince079fd2016-08-02 16:22:34 -04001832 if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001833 return 0;
1834 }
1835
1836 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1837
David Benjamin54091232016-09-05 12:47:25 -04001838 for (size_t i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001839 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1840
1841 const uint32_t alg_k = cipher->algorithm_mkey;
1842 const uint32_t alg_a = cipher->algorithm_auth;
1843 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1844 return 1;
1845 }
1846 }
1847
1848 return 0;
1849}
1850
Adam Langleybdd5d662015-07-20 16:19:08 -07001851static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001852 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001853 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1854 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001855 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1856 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001857 !CBB_flush(out)) {
1858 return 0;
1859 }
1860
1861 return 1;
1862}
1863
1864static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1865 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1866 return 1;
1867 }
1868
1869 return ext_ec_point_add_extension(ssl, out);
1870}
1871
1872static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1873 CBS *contents) {
1874 if (contents == NULL) {
1875 return 1;
1876 }
1877
Steven Valdez143e8b32016-07-11 13:19:03 -04001878 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1879 return 0;
1880 }
1881
Adam Langleybdd5d662015-07-20 16:19:08 -07001882 CBS ec_point_format_list;
1883 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1884 CBS_len(contents) != 0) {
1885 return 0;
1886 }
1887
David Benjaminfc059942015-07-30 23:01:59 -04001888 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1889 * point format. */
1890 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1891 CBS_len(&ec_point_format_list)) == NULL) {
1892 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001893 return 0;
1894 }
1895
1896 return 1;
1897}
1898
1899static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1900 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001901 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1902 return 1;
1903 }
1904
Adam Langleybdd5d662015-07-20 16:19:08 -07001905 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1906}
1907
1908static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001909 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1910 return 1;
1911 }
1912
Adam Langleybdd5d662015-07-20 16:19:08 -07001913 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1914 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001915 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001916
1917 if (!using_ecc) {
1918 return 1;
1919 }
1920
1921 return ext_ec_point_add_extension(ssl, out);
1922}
1923
Steven Valdez4aa154e2016-07-29 14:32:55 -04001924/* Pre Shared Key
1925 *
David Benjamina128a552016-10-13 14:26:33 -04001926 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001927
1928static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
1929 uint16_t min_version, max_version;
1930 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1931 return 0;
1932 }
1933
David Benjamin2dc02042016-09-19 19:57:37 -04001934 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001935 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001936 !ssl->method->version_from_wire(&session_version,
1937 ssl->session->ssl_version) ||
1938 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001939 return 1;
1940 }
1941
Steven Valdez5b986082016-09-01 12:29:49 -04001942 CBB contents, identity, ke_modes, auth_modes, ticket;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001943 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1944 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001945 !CBB_add_u16_length_prefixed(&contents, &identity) ||
1946 !CBB_add_u8_length_prefixed(&identity, &ke_modes) ||
1947 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE) ||
1948 !CBB_add_u8_length_prefixed(&identity, &auth_modes) ||
1949 !CBB_add_u8(&auth_modes, SSL_PSK_AUTH) ||
1950 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1951 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001952 ssl->session->tlsext_ticklen)) {
1953 return 0;
1954 }
1955
1956 return CBB_flush(out);
1957}
1958
1959int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1960 CBS *contents) {
1961 uint16_t psk_id;
1962 if (!CBS_get_u16(contents, &psk_id) ||
1963 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001964 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001965 *out_alert = SSL_AD_DECODE_ERROR;
1966 return 0;
1967 }
1968
1969 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001970 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001971 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1972 return 0;
1973 }
1974
1975 return 1;
1976}
1977
1978int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
1979 SSL_SESSION **out_session,
1980 uint8_t *out_alert,
1981 CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04001982 /* We only process the first PSK identity since we don't support pure PSK. */
1983 CBS identity, ke_modes, auth_modes, ticket;
1984 if (!CBS_get_u16_length_prefixed(contents, &identity) ||
1985 !CBS_get_u8_length_prefixed(&identity, &ke_modes) ||
1986 !CBS_get_u8_length_prefixed(&identity, &auth_modes) ||
1987 !CBS_get_u16_length_prefixed(&identity, &ticket) ||
1988 CBS_len(&identity) != 0) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001989 *out_alert = SSL_AD_DECODE_ERROR;
1990 return 0;
1991 }
1992
Steven Valdez5b986082016-09-01 12:29:49 -04001993 /* We only support tickets with PSK_DHE_KE and PSK_AUTH. */
1994 if (memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, CBS_len(&ke_modes)) == NULL ||
1995 memchr(CBS_data(&auth_modes), SSL_PSK_AUTH, CBS_len(&auth_modes)) ==
1996 NULL) {
1997 *out_session = NULL;
1998 return 1;
1999 }
2000
Steven Valdez4aa154e2016-07-29 14:32:55 -04002001 /* TLS 1.3 session tickets are renewed separately as part of the
2002 * NewSessionTicket. */
2003 int renew;
Steven Valdez5b986082016-09-01 12:29:49 -04002004 return tls_process_ticket(ssl, out_session, &renew, CBS_data(&ticket),
2005 CBS_len(&ticket), NULL, 0);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002006}
2007
2008int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
2009 if (!ssl->s3->session_reused) {
2010 return 1;
2011 }
2012
2013 CBB contents;
2014 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2015 !CBB_add_u16_length_prefixed(out, &contents) ||
2016 /* We only consider the first identity for resumption */
2017 !CBB_add_u16(&contents, 0) ||
2018 !CBB_flush(out)) {
2019 return 0;
2020 }
2021
2022 return 1;
2023}
2024
2025
Steven Valdez143e8b32016-07-11 13:19:03 -04002026/* Key Share
2027 *
David Benjamina128a552016-10-13 14:26:33 -04002028 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002029
2030static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
2031 uint16_t min_version, max_version;
2032 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2033 return 0;
2034 }
2035
David Benjamin53a2dfc2016-10-07 21:04:12 -04002036 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002037 return 1;
2038 }
2039
2040 CBB contents, kse_bytes;
2041 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2042 !CBB_add_u16_length_prefixed(out, &contents) ||
2043 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2044 return 0;
2045 }
2046
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002047 uint16_t group_id;
David Benjamin3baa6e12016-10-07 21:10:38 -04002048 if (ssl->s3->hs->received_hello_retry_request) {
2049 /* Replay the old key shares. */
Steven Valdez5440fe02016-07-18 12:40:30 -04002050 if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
2051 ssl->s3->hs->key_share_bytes_len)) {
2052 return 0;
2053 }
2054 OPENSSL_free(ssl->s3->hs->key_share_bytes);
2055 ssl->s3->hs->key_share_bytes = NULL;
David Benjamin4fe3c902016-08-16 02:17:03 -04002056 ssl->s3->hs->key_share_bytes_len = 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002057
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002058 group_id = ssl->s3->hs->retry_group;
David Benjamin3baa6e12016-10-07 21:10:38 -04002059
2060 /* We received a HelloRetryRequest without a new curve, so there is no new
2061 * share to append. Leave |ecdh_ctx| as-is. */
2062 if (group_id == 0) {
2063 return CBB_flush(out);
2064 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002065 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002066 /* Add a fake group. See draft-davidben-tls-grease-01. */
2067 if (ssl->ctx->grease_enabled &&
2068 (!CBB_add_u16(&kse_bytes,
2069 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2070 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2071 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2072 return 0;
2073 }
2074
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002075 /* Predict the most preferred group. */
2076 const uint16_t *groups;
2077 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002078 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002079 if (groups_len == 0) {
2080 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2081 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002082 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002083
2084 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002085 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002086
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002087 CBB key_exchange;
2088 if (!CBB_add_u16(&kse_bytes, group_id) ||
2089 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2090 !SSL_ECDH_CTX_init(&ssl->s3->hs->ecdh_ctx, group_id) ||
2091 !SSL_ECDH_CTX_offer(&ssl->s3->hs->ecdh_ctx, &key_exchange) ||
2092 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002093 return 0;
2094 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002095
David Benjamin3baa6e12016-10-07 21:10:38 -04002096 if (!ssl->s3->hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002097 /* Save the contents of the extension to repeat it in the second
2098 * ClientHello. */
2099 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2100 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2101 CBB_len(&kse_bytes));
2102 if (ssl->s3->hs->key_share_bytes == NULL) {
2103 return 0;
2104 }
2105 }
2106
Steven Valdez143e8b32016-07-11 13:19:03 -04002107 return CBB_flush(out);
2108}
2109
Steven Valdez7259f2f2016-08-02 16:55:05 -04002110int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2111 size_t *out_secret_len,
2112 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002113 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002114 uint16_t group_id;
2115 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002116 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2117 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002118 *out_alert = SSL_AD_DECODE_ERROR;
2119 return 0;
2120 }
2121
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002122 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002123 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2124 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2125 return 0;
2126 }
2127
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002128 if (!SSL_ECDH_CTX_finish(&ssl->s3->hs->ecdh_ctx, out_secret, out_secret_len,
2129 out_alert, CBS_data(&peer_key),
2130 CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002131 *out_alert = SSL_AD_INTERNAL_ERROR;
2132 return 0;
2133 }
2134
David Benjamin5c4e8572016-08-19 17:44:53 -04002135 ssl->s3->new_session->key_exchange_info = group_id;
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002136 SSL_ECDH_CTX_cleanup(&ssl->s3->hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002137 return 1;
2138}
2139
Steven Valdez7259f2f2016-08-02 16:55:05 -04002140int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2141 uint8_t **out_secret,
2142 size_t *out_secret_len,
2143 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002144 uint16_t group_id;
2145 CBS key_shares;
Steven Valdez803c77a2016-09-06 14:13:43 -04002146 if (!tls1_get_shared_group(ssl, &group_id)) {
2147 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2148 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2149 return 0;
2150 }
2151
2152 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002153 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002154 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002155 return 0;
2156 }
2157
David Benjamin7e1f9842016-09-20 19:24:40 -04002158 /* Find the corresponding key share. */
2159 int found = 0;
2160 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002161 while (CBS_len(&key_shares) > 0) {
2162 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002163 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002164 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002165 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2166 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002167 return 0;
2168 }
2169
David Benjamin7e1f9842016-09-20 19:24:40 -04002170 if (id == group_id) {
2171 if (found) {
2172 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2173 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2174 return 0;
2175 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002176
David Benjamin7e1f9842016-09-20 19:24:40 -04002177 found = 1;
2178 peer_key = peer_key_tmp;
2179 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002180 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002181 }
2182
David Benjamin7e1f9842016-09-20 19:24:40 -04002183 if (!found) {
2184 *out_found = 0;
2185 *out_secret = NULL;
2186 *out_secret_len = 0;
2187 return 1;
2188 }
2189
2190 /* Compute the DH secret. */
2191 uint8_t *secret = NULL;
2192 size_t secret_len;
2193 SSL_ECDH_CTX group;
2194 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2195 CBB public_key;
2196 if (!CBB_init(&public_key, 32) ||
2197 !SSL_ECDH_CTX_init(&group, group_id) ||
2198 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len,
2199 out_alert, CBS_data(&peer_key),
2200 CBS_len(&peer_key)) ||
2201 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2202 &ssl->s3->hs->public_key_len)) {
2203 OPENSSL_free(secret);
2204 SSL_ECDH_CTX_cleanup(&group);
2205 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002206 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002207 return 0;
2208 }
2209
2210 SSL_ECDH_CTX_cleanup(&group);
2211
2212 *out_secret = secret;
2213 *out_secret_len = secret_len;
2214 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002215 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002216}
2217
Steven Valdez7259f2f2016-08-02 16:55:05 -04002218int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002219 uint16_t group_id;
2220 CBB kse_bytes, public_key;
2221 if (!tls1_get_shared_group(ssl, &group_id) ||
2222 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2223 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2224 !CBB_add_u16(&kse_bytes, group_id) ||
2225 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2226 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2227 ssl->s3->hs->public_key_len) ||
2228 !CBB_flush(out)) {
2229 return 0;
2230 }
2231
David Benjamin4fe3c902016-08-16 02:17:03 -04002232 OPENSSL_free(ssl->s3->hs->public_key);
2233 ssl->s3->hs->public_key = NULL;
2234 ssl->s3->hs->public_key_len = 0;
2235
David Benjamin5c4e8572016-08-19 17:44:53 -04002236 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002237 return 1;
2238}
2239
2240
Steven Valdezfdd10992016-09-15 16:27:05 -04002241/* Supported Versions
2242 *
2243 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2244
2245static int ext_supported_versions_add_clienthello(SSL *ssl, CBB *out) {
2246 uint16_t min_version, max_version;
2247 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2248 return 0;
2249 }
2250
2251 if (max_version <= TLS1_2_VERSION) {
2252 return 1;
2253 }
2254
2255 CBB contents, versions;
2256 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2257 !CBB_add_u16_length_prefixed(out, &contents) ||
2258 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2259 return 0;
2260 }
2261
David Benjamind9791bf2016-09-27 16:39:52 -04002262 /* Add a fake version. See draft-davidben-tls-grease-01. */
2263 if (ssl->ctx->grease_enabled &&
2264 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2265 return 0;
2266 }
2267
Steven Valdezfdd10992016-09-15 16:27:05 -04002268 for (uint16_t version = max_version; version >= min_version; version--) {
2269 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2270 return 0;
2271 }
2272 }
2273
2274 if (!CBB_flush(out)) {
2275 return 0;
2276 }
2277
2278 return 1;
2279}
2280
2281
David Benjamin3baa6e12016-10-07 21:10:38 -04002282/* Cookie
2283 *
2284 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2285
2286static int ext_cookie_add_clienthello(SSL *ssl, CBB *out) {
2287 if (ssl->s3->hs->cookie == NULL) {
2288 return 1;
2289 }
2290
2291 CBB contents, cookie;
2292 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2293 !CBB_add_u16_length_prefixed(out, &contents) ||
2294 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
2295 !CBB_add_bytes(&cookie, ssl->s3->hs->cookie, ssl->s3->hs->cookie_len) ||
2296 !CBB_flush(out)) {
2297 return 0;
2298 }
2299
2300 /* The cookie is no longer needed in memory. */
2301 OPENSSL_free(ssl->s3->hs->cookie);
2302 ssl->s3->hs->cookie = NULL;
2303 ssl->s3->hs->cookie_len = 0;
2304 return 1;
2305}
2306
2307
Steven Valdezce902a92016-05-17 11:47:53 -04002308/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002309 *
Steven Valdezce902a92016-05-17 11:47:53 -04002310 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002311 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002312
Steven Valdezce902a92016-05-17 11:47:53 -04002313static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002314 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2315 return 1;
2316 }
2317
Steven Valdezce902a92016-05-17 11:47:53 -04002318 CBB contents, groups_bytes;
2319 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002320 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002321 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002322 return 0;
2323 }
2324
David Benjamin65ac9972016-09-02 21:35:25 -04002325 /* Add a fake group. See draft-davidben-tls-grease-01. */
2326 if (ssl->ctx->grease_enabled &&
2327 !CBB_add_u16(&groups_bytes,
2328 ssl_get_grease_value(ssl, ssl_grease_group))) {
2329 return 0;
2330 }
2331
Steven Valdezce902a92016-05-17 11:47:53 -04002332 const uint16_t *groups;
2333 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002334 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002335
David Benjamin54091232016-09-05 12:47:25 -04002336 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002337 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002338 return 0;
2339 }
2340 }
2341
2342 return CBB_flush(out);
2343}
2344
Steven Valdezce902a92016-05-17 11:47:53 -04002345static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2346 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002347 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2348 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002349 return 1;
2350}
2351
Steven Valdezce902a92016-05-17 11:47:53 -04002352static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2353 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002354 if (contents == NULL) {
2355 return 1;
2356 }
2357
Steven Valdezce902a92016-05-17 11:47:53 -04002358 CBS supported_group_list;
2359 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2360 CBS_len(&supported_group_list) == 0 ||
2361 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002362 CBS_len(contents) != 0) {
2363 return 0;
2364 }
2365
David Benjamin43612b62016-10-07 00:41:50 -04002366 ssl->s3->hs->peer_supported_group_list = OPENSSL_malloc(
Steven Valdezce902a92016-05-17 11:47:53 -04002367 CBS_len(&supported_group_list));
David Benjamin43612b62016-10-07 00:41:50 -04002368 if (ssl->s3->hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002369 *out_alert = SSL_AD_INTERNAL_ERROR;
2370 return 0;
2371 }
2372
Steven Valdezce902a92016-05-17 11:47:53 -04002373 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002374 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002375 if (!CBS_get_u16(&supported_group_list,
David Benjamin43612b62016-10-07 00:41:50 -04002376 &ssl->s3->hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002377 goto err;
2378 }
2379 }
2380
Steven Valdezce902a92016-05-17 11:47:53 -04002381 assert(CBS_len(&supported_group_list) == 0);
David Benjamin43612b62016-10-07 00:41:50 -04002382 ssl->s3->hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002383
2384 return 1;
2385
2386err:
David Benjamin43612b62016-10-07 00:41:50 -04002387 OPENSSL_free(ssl->s3->hs->peer_supported_group_list);
2388 ssl->s3->hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002389 *out_alert = SSL_AD_INTERNAL_ERROR;
2390 return 0;
2391}
2392
Steven Valdezce902a92016-05-17 11:47:53 -04002393static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002394 /* Servers don't echo this extension. */
2395 return 1;
2396}
2397
2398
Adam Langley614c66a2015-06-12 15:26:58 -07002399/* kExtensions contains all the supported extensions. */
2400static const struct tls_extension kExtensions[] = {
2401 {
Adam Langley5021b222015-06-12 18:27:58 -07002402 TLSEXT_TYPE_renegotiate,
2403 NULL,
2404 ext_ri_add_clienthello,
2405 ext_ri_parse_serverhello,
2406 ext_ri_parse_clienthello,
2407 ext_ri_add_serverhello,
2408 },
2409 {
Adam Langley614c66a2015-06-12 15:26:58 -07002410 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002411 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002412 ext_sni_add_clienthello,
2413 ext_sni_parse_serverhello,
2414 ext_sni_parse_clienthello,
2415 ext_sni_add_serverhello,
2416 },
Adam Langley0a056712015-07-01 15:03:33 -07002417 {
2418 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002419 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002420 ext_ems_add_clienthello,
2421 ext_ems_parse_serverhello,
2422 ext_ems_parse_clienthello,
2423 ext_ems_add_serverhello,
2424 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002425 {
2426 TLSEXT_TYPE_session_ticket,
2427 NULL,
2428 ext_ticket_add_clienthello,
2429 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002430 /* Ticket extension client parsing is handled in ssl_session.c */
2431 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002432 ext_ticket_add_serverhello,
2433 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002434 {
2435 TLSEXT_TYPE_signature_algorithms,
2436 NULL,
2437 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002438 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002439 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002440 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002441 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002442 {
2443 TLSEXT_TYPE_status_request,
2444 ext_ocsp_init,
2445 ext_ocsp_add_clienthello,
2446 ext_ocsp_parse_serverhello,
2447 ext_ocsp_parse_clienthello,
2448 ext_ocsp_add_serverhello,
2449 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002450 {
2451 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002452 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002453 ext_npn_add_clienthello,
2454 ext_npn_parse_serverhello,
2455 ext_npn_parse_clienthello,
2456 ext_npn_add_serverhello,
2457 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002458 {
2459 TLSEXT_TYPE_certificate_timestamp,
2460 NULL,
2461 ext_sct_add_clienthello,
2462 ext_sct_parse_serverhello,
2463 ext_sct_parse_clienthello,
2464 ext_sct_add_serverhello,
2465 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002466 {
2467 TLSEXT_TYPE_application_layer_protocol_negotiation,
2468 ext_alpn_init,
2469 ext_alpn_add_clienthello,
2470 ext_alpn_parse_serverhello,
2471 ext_alpn_parse_clienthello,
2472 ext_alpn_add_serverhello,
2473 },
Adam Langley49c7af12015-07-10 14:33:46 -07002474 {
2475 TLSEXT_TYPE_channel_id,
2476 ext_channel_id_init,
2477 ext_channel_id_add_clienthello,
2478 ext_channel_id_parse_serverhello,
2479 ext_channel_id_parse_clienthello,
2480 ext_channel_id_add_serverhello,
2481 },
Adam Langley391250d2015-07-15 19:06:07 -07002482 {
2483 TLSEXT_TYPE_srtp,
2484 ext_srtp_init,
2485 ext_srtp_add_clienthello,
2486 ext_srtp_parse_serverhello,
2487 ext_srtp_parse_clienthello,
2488 ext_srtp_add_serverhello,
2489 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002490 {
2491 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002492 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002493 ext_ec_point_add_clienthello,
2494 ext_ec_point_parse_serverhello,
2495 ext_ec_point_parse_clienthello,
2496 ext_ec_point_add_serverhello,
2497 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002498 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002499 TLSEXT_TYPE_key_share,
2500 NULL,
2501 ext_key_share_add_clienthello,
2502 forbid_parse_serverhello,
2503 ignore_parse_clienthello,
2504 dont_add_serverhello,
2505 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002506 {
2507 TLSEXT_TYPE_pre_shared_key,
2508 NULL,
2509 ext_pre_shared_key_add_clienthello,
2510 forbid_parse_serverhello,
2511 ignore_parse_clienthello,
2512 dont_add_serverhello,
2513 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002514 {
2515 TLSEXT_TYPE_supported_versions,
2516 NULL,
2517 ext_supported_versions_add_clienthello,
2518 forbid_parse_serverhello,
2519 ignore_parse_clienthello,
2520 dont_add_serverhello,
2521 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002522 {
2523 TLSEXT_TYPE_cookie,
2524 NULL,
2525 ext_cookie_add_clienthello,
2526 forbid_parse_serverhello,
2527 ignore_parse_clienthello,
2528 dont_add_serverhello,
2529 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002530 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2531 * intolerant to the last extension being zero-length. See
2532 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002533 {
Steven Valdezce902a92016-05-17 11:47:53 -04002534 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002535 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002536 ext_supported_groups_add_clienthello,
2537 ext_supported_groups_parse_serverhello,
2538 ext_supported_groups_parse_clienthello,
2539 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002540 },
Adam Langley614c66a2015-06-12 15:26:58 -07002541};
2542
2543#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2544
Adam Langley4cfa96b2015-07-01 11:56:55 -07002545OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002546 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002547 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002548OPENSSL_COMPILE_ASSERT(
2549 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2550 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002551
Adam Langley614c66a2015-06-12 15:26:58 -07002552static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2553 uint16_t value) {
2554 unsigned i;
2555 for (i = 0; i < kNumExtensions; i++) {
2556 if (kExtensions[i].value == value) {
2557 *out_index = i;
2558 return &kExtensions[i];
2559 }
2560 }
2561
2562 return NULL;
2563}
2564
Adam Langley09505632015-07-30 18:10:13 -07002565int SSL_extension_supported(unsigned extension_value) {
2566 uint32_t index;
2567 return extension_value == TLSEXT_TYPE_padding ||
2568 tls_extension_find(&index, extension_value) != NULL;
2569}
2570
David Benjamine8d53502015-10-10 14:13:23 -04002571int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002572 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002573 if (ssl->client_version == SSL3_VERSION &&
2574 !ssl->s3->send_connection_binding) {
2575 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002576 }
Adam Langley95c29f32014-06-20 12:00:00 -07002577
David Benjamine8d53502015-10-10 14:13:23 -04002578 CBB extensions;
2579 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002580 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002581 }
Adam Langley95c29f32014-06-20 12:00:00 -07002582
David Benjaminf5d2cd02016-10-06 19:39:20 -04002583 ssl->s3->hs->extensions.sent = 0;
2584 ssl->s3->hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002585
David Benjamin54091232016-09-05 12:47:25 -04002586 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002587 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002588 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002589 }
2590 }
Adam Langley95c29f32014-06-20 12:00:00 -07002591
David Benjamin65ac9972016-09-02 21:35:25 -04002592 uint16_t grease_ext1 = 0;
2593 if (ssl->ctx->grease_enabled) {
2594 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2595 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2596 if (!CBB_add_u16(&extensions, grease_ext1) ||
2597 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2598 goto err;
2599 }
2600 }
2601
David Benjamin54091232016-09-05 12:47:25 -04002602 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002603 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002604 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002605 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2606 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2607 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002608 }
Adam Langley95c29f32014-06-20 12:00:00 -07002609
Adam Langley33ad2b52015-07-20 17:43:53 -07002610 if (CBB_len(&extensions) != len_before) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002611 ssl->s3->hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002612 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002613 }
Adam Langley75712922014-10-10 16:23:43 -07002614
David Benjamine8d53502015-10-10 14:13:23 -04002615 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002616 goto err;
2617 }
2618
David Benjamin65ac9972016-09-02 21:35:25 -04002619 if (ssl->ctx->grease_enabled) {
2620 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2621 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2622
2623 /* The two fake extensions must not have the same value. GREASE values are
2624 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2625 * one. */
2626 if (grease_ext1 == grease_ext2) {
2627 grease_ext2 ^= 0x1010;
2628 }
2629
2630 if (!CBB_add_u16(&extensions, grease_ext2) ||
2631 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2632 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2633 goto err;
2634 }
2635 }
2636
David Benjamince079fd2016-08-02 16:22:34 -04002637 if (!SSL_is_dtls(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002638 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002639 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002640 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002641 *
2642 * NB: because this code works out the length of all existing extensions
2643 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002644 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002645 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002646 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002647 * Server 7.0 is intolerant to the last extension being zero-length. See
2648 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002649 if (padding_len >= 4 + 1) {
2650 padding_len -= 4;
2651 } else {
2652 padding_len = 1;
2653 }
Adam Langley95c29f32014-06-20 12:00:00 -07002654
Adam Langley33ad2b52015-07-20 17:43:53 -07002655 uint8_t *padding_bytes;
2656 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2657 !CBB_add_u16(&extensions, padding_len) ||
2658 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2659 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002660 }
Adam Langley75712922014-10-10 16:23:43 -07002661
Adam Langley33ad2b52015-07-20 17:43:53 -07002662 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002663 }
2664 }
Adam Langley75712922014-10-10 16:23:43 -07002665
David Benjamina01deee2015-12-08 18:56:31 -05002666 /* Discard empty extensions blocks. */
2667 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002668 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002669 }
2670
David Benjamine8d53502015-10-10 14:13:23 -04002671 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002672
2673err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002674 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002675 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002676}
Adam Langley95c29f32014-06-20 12:00:00 -07002677
David Benjamin56380462015-10-10 14:59:09 -04002678int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002679 CBB extensions;
2680 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002681 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002682 }
2683
2684 unsigned i;
2685 for (i = 0; i < kNumExtensions; i++) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002686 if (!(ssl->s3->hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002687 /* Don't send extensions that were not received. */
2688 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002689 }
Adam Langley95c29f32014-06-20 12:00:00 -07002690
David Benjamin56380462015-10-10 14:59:09 -04002691 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002692 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2693 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2694 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002695 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002696 }
Adam Langley95c29f32014-06-20 12:00:00 -07002697
David Benjamin56380462015-10-10 14:59:09 -04002698 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002699 goto err;
2700 }
2701
Steven Valdez143e8b32016-07-11 13:19:03 -04002702 /* Discard empty extensions blocks before TLS 1.3. */
2703 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2704 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002705 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002706 }
2707
David Benjamin56380462015-10-10 14:59:09 -04002708 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002709
2710err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002711 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002712 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002713}
Adam Langley95c29f32014-06-20 12:00:00 -07002714
David Benjamine14ff062016-08-09 16:21:24 -04002715static int ssl_scan_clienthello_tlsext(
2716 SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
2717 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002718 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002719 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002720 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002721 }
2722 }
2723
David Benjaminf5d2cd02016-10-06 19:39:20 -04002724 ssl->s3->hs->extensions.received = 0;
2725 ssl->s3->hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002726
David Benjamine14ff062016-08-09 16:21:24 -04002727 CBS extensions;
2728 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2729 while (CBS_len(&extensions) != 0) {
2730 uint16_t type;
2731 CBS extension;
2732
2733 /* Decode the next extension. */
2734 if (!CBS_get_u16(&extensions, &type) ||
2735 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002736 *out_alert = SSL_AD_DECODE_ERROR;
2737 return 0;
2738 }
Adam Langley95c29f32014-06-20 12:00:00 -07002739
David Benjamine14ff062016-08-09 16:21:24 -04002740 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2741 * ambiguous. Ignore all but the renegotiation_info extension. */
2742 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2743 continue;
2744 }
Adam Langley95c29f32014-06-20 12:00:00 -07002745
David Benjamine14ff062016-08-09 16:21:24 -04002746 unsigned ext_index;
2747 const struct tls_extension *const ext =
2748 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002749
David Benjamine14ff062016-08-09 16:21:24 -04002750 if (ext == NULL) {
2751 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002752 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002753 return 0;
2754 }
David Benjamine14ff062016-08-09 16:21:24 -04002755 continue;
2756 }
2757
David Benjaminf5d2cd02016-10-06 19:39:20 -04002758 ssl->s3->hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002759 uint8_t alert = SSL_AD_DECODE_ERROR;
2760 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2761 *out_alert = alert;
2762 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2763 ERR_add_error_dataf("extension: %u", (unsigned)type);
2764 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002765 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002766 }
Adam Langley75712922014-10-10 16:23:43 -07002767
David Benjamin1deb41b2016-08-09 19:36:38 -04002768 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002769 if (ssl->s3->hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002770 continue;
2771 }
2772
2773 CBS *contents = NULL, fake_contents;
2774 static const uint8_t kFakeRenegotiateExtension[] = {0};
2775 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2776 ssl_client_cipher_list_contains_cipher(client_hello,
2777 SSL3_CK_SCSV & 0xffff)) {
2778 /* The renegotiation SCSV was received so pretend that we received a
2779 * renegotiation extension. */
2780 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2781 sizeof(kFakeRenegotiateExtension));
2782 contents = &fake_contents;
David Benjaminf5d2cd02016-10-06 19:39:20 -04002783 ssl->s3->hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002784 }
2785
2786 /* Extension wasn't observed so call the callback with a NULL
2787 * parameter. */
2788 uint8_t alert = SSL_AD_DECODE_ERROR;
2789 if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
2790 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2791 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2792 *out_alert = alert;
2793 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002794 }
2795 }
2796
Adam Langleyfcf25832014-12-18 17:42:32 -08002797 return 1;
2798}
Adam Langley95c29f32014-06-20 12:00:00 -07002799
David Benjamine14ff062016-08-09 16:21:24 -04002800int ssl_parse_clienthello_tlsext(
2801 SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002802 int alert = -1;
David Benjamine14ff062016-08-09 16:21:24 -04002803 if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002804 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002805 return 0;
2806 }
Adam Langley95c29f32014-06-20 12:00:00 -07002807
David Benjamin0d56f882015-12-19 17:05:56 -05002808 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002809 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002810 return 0;
2811 }
Adam Langley95c29f32014-06-20 12:00:00 -07002812
Adam Langleyfcf25832014-12-18 17:42:32 -08002813 return 1;
2814}
Adam Langley95c29f32014-06-20 12:00:00 -07002815
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002816OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2817
David Benjamin0d56f882015-12-19 17:05:56 -05002818static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002819 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2820 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2821 return 1;
2822 }
Adam Langley614c66a2015-06-12 15:26:58 -07002823
Steven Valdez143e8b32016-07-11 13:19:03 -04002824 /* Decode the extensions block and check it is valid. */
2825 CBS extensions;
2826 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2827 !tls1_check_duplicate_extensions(&extensions)) {
2828 *out_alert = SSL_AD_DECODE_ERROR;
2829 return 0;
2830 }
2831
2832 uint32_t received = 0;
2833 while (CBS_len(&extensions) != 0) {
2834 uint16_t type;
2835 CBS extension;
2836
2837 /* Decode the next extension. */
2838 if (!CBS_get_u16(&extensions, &type) ||
2839 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002840 *out_alert = SSL_AD_DECODE_ERROR;
2841 return 0;
2842 }
Adam Langley95c29f32014-06-20 12:00:00 -07002843
Steven Valdez143e8b32016-07-11 13:19:03 -04002844 unsigned ext_index;
2845 const struct tls_extension *const ext =
2846 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002847
Steven Valdez143e8b32016-07-11 13:19:03 -04002848 if (ext == NULL) {
2849 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002850 return 0;
2851 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002852 continue;
2853 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002854
David Benjaminf5d2cd02016-10-06 19:39:20 -04002855 if (!(ssl->s3->hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002856 type != TLSEXT_TYPE_renegotiate) {
2857 /* If the extension was never sent then it is illegal, except for the
2858 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002859 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2860 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002861 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002862 return 0;
2863 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002864
Steven Valdez143e8b32016-07-11 13:19:03 -04002865 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002866
Steven Valdez143e8b32016-07-11 13:19:03 -04002867 uint8_t alert = SSL_AD_DECODE_ERROR;
2868 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2869 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2870 ERR_add_error_dataf("extension: %u", (unsigned)type);
2871 *out_alert = alert;
2872 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002873 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002874 }
Adam Langley95c29f32014-06-20 12:00:00 -07002875
David Benjamin54091232016-09-05 12:47:25 -04002876 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002877 if (!(received & (1u << i))) {
2878 /* Extension wasn't observed so call the callback with a NULL
2879 * parameter. */
2880 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002881 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002882 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2883 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002884 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002885 return 0;
2886 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002887 }
2888 }
Adam Langley95c29f32014-06-20 12:00:00 -07002889
Adam Langleyfcf25832014-12-18 17:42:32 -08002890 return 1;
2891}
Adam Langley95c29f32014-06-20 12:00:00 -07002892
David Benjamin0d56f882015-12-19 17:05:56 -05002893static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002894 int ret = SSL_TLSEXT_ERR_NOACK;
2895 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002896
David Benjamin78f8aab2016-03-10 16:33:58 -05002897 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002898 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002899 ssl->ctx->tlsext_servername_arg);
2900 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002901 ret = ssl->initial_ctx->tlsext_servername_callback(
2902 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002903 }
Adam Langley95c29f32014-06-20 12:00:00 -07002904
Adam Langleyfcf25832014-12-18 17:42:32 -08002905 switch (ret) {
2906 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002907 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002908 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002909
Adam Langleyfcf25832014-12-18 17:42:32 -08002910 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002911 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002912 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002913
Adam Langleyfcf25832014-12-18 17:42:32 -08002914 case SSL_TLSEXT_ERR_NOACK:
David Benjamina0486782016-10-06 19:11:32 -04002915 ssl->s3->hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002916 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002917
Adam Langleyfcf25832014-12-18 17:42:32 -08002918 default:
2919 return 1;
2920 }
2921}
Adam Langleyed8270a2014-09-02 13:52:56 -07002922
David Benjamin0d56f882015-12-19 17:05:56 -05002923static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002924 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002925 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002926
David Benjamin78f8aab2016-03-10 16:33:58 -05002927 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002928 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002929 ssl->ctx->tlsext_servername_arg);
2930 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002931 ret = ssl->initial_ctx->tlsext_servername_callback(
2932 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002933 }
Adam Langley95c29f32014-06-20 12:00:00 -07002934
Adam Langleyfcf25832014-12-18 17:42:32 -08002935 switch (ret) {
2936 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002937 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002938 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002939
Adam Langleyfcf25832014-12-18 17:42:32 -08002940 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002941 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002942 return 1;
2943
2944 default:
2945 return 1;
2946 }
2947}
2948
David Benjamin0d56f882015-12-19 17:05:56 -05002949int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002950 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002951 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2952 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002953 return 0;
2954 }
2955
David Benjamin0d56f882015-12-19 17:05:56 -05002956 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002957 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002958 return 0;
2959 }
2960
2961 return 1;
2962}
Adam Langley95c29f32014-06-20 12:00:00 -07002963
David Benjamine3aa1d92015-06-16 15:34:50 -04002964int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002965 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002966 size_t ticket_len, const uint8_t *session_id,
2967 size_t session_id_len) {
2968 int ret = 1; /* Most errors are non-fatal. */
2969 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2970 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002971
David Benjamine3aa1d92015-06-16 15:34:50 -04002972 HMAC_CTX hmac_ctx;
2973 HMAC_CTX_init(&hmac_ctx);
2974 EVP_CIPHER_CTX cipher_ctx;
2975 EVP_CIPHER_CTX_init(&cipher_ctx);
2976
David Benjaminef1b0092015-11-21 14:05:44 -05002977 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002978 *out_session = NULL;
2979
Steven Valdez4aa154e2016-07-29 14:32:55 -04002980 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
2981 goto done;
2982 }
2983
David Benjamine3aa1d92015-06-16 15:34:50 -04002984 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2985 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002986 }
2987
David Benjaminadcc3952015-04-26 13:07:57 -04002988 /* Ensure there is room for the key name and the largest IV
2989 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2990 * the maximum IV length should be well under the minimum size for the
2991 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002992 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2993 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002994 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002995 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002996
David Benjamine3aa1d92015-06-16 15:34:50 -04002997 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002998 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2999 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3000 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003001 if (cb_ret < 0) {
3002 ret = 0;
3003 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003004 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003005 if (cb_ret == 0) {
3006 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003007 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003008 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003009 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003010 }
3011 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003012 /* Check the key name matches. */
3013 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3014 SSL_TICKET_KEY_NAME_LEN) != 0) {
3015 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003016 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003017 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3018 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003019 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003020 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3021 ssl_ctx->tlsext_tick_aes_key, iv)) {
3022 ret = 0;
3023 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003024 }
3025 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003026 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003027
David Benjamine3aa1d92015-06-16 15:34:50 -04003028 /* Check the MAC at the end of the ticket. */
3029 uint8_t mac[EVP_MAX_MD_SIZE];
3030 size_t mac_len = HMAC_size(&hmac_ctx);
3031 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003032 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003033 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003034 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003035 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3036 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003037 int mac_ok =
3038 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3039#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3040 mac_ok = 1;
3041#endif
3042 if (!mac_ok) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003043 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003044 }
3045
David Benjamine3aa1d92015-06-16 15:34:50 -04003046 /* Decrypt the session data. */
3047 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3048 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3049 mac_len;
3050 plaintext = OPENSSL_malloc(ciphertext_len);
3051 if (plaintext == NULL) {
3052 ret = 0;
3053 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003054 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003055 size_t plaintext_len;
3056#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3057 memcpy(plaintext, ciphertext, ciphertext_len);
3058 plaintext_len = ciphertext_len;
3059#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003060 if (ciphertext_len >= INT_MAX) {
3061 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003062 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003063 int len1, len2;
3064 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3065 (int)ciphertext_len) ||
3066 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
3067 ERR_clear_error(); /* Don't leave an error on the queue. */
3068 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003069 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003070 plaintext_len = (size_t)(len1 + len2);
3071#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003072
David Benjamine3aa1d92015-06-16 15:34:50 -04003073 /* Decode the session. */
David Benjaminfbc45d72016-09-22 01:21:24 -04003074 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003075 if (session == NULL) {
3076 ERR_clear_error(); /* Don't leave an error on the queue. */
3077 goto done;
3078 }
3079
3080 /* Copy the client's session ID into the new session, to denote the ticket has
3081 * been accepted. */
3082 memcpy(session->session_id, session_id, session_id_len);
3083 session->session_id_length = session_id_len;
3084
Steven Valdez4aa154e2016-07-29 14:32:55 -04003085 if (!ssl_session_is_context_valid(ssl, session) ||
3086 !ssl_session_is_time_valid(ssl, session)) {
3087 SSL_SESSION_free(session);
3088 session = NULL;
3089 }
3090
David Benjamine3aa1d92015-06-16 15:34:50 -04003091 *out_session = session;
3092
3093done:
3094 OPENSSL_free(plaintext);
3095 HMAC_CTX_cleanup(&hmac_ctx);
3096 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3097 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003098}
Adam Langley95c29f32014-06-20 12:00:00 -07003099
Steven Valdez0d62f262015-09-04 12:41:04 -04003100int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003101 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05003102 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003103 return 1;
3104 }
David Benjamincd996942014-07-20 16:23:51 -04003105
David Benjamin0fc37ef2016-08-17 15:29:46 -04003106 SSL_HANDSHAKE *hs = ssl->s3->hs;
3107 OPENSSL_free(hs->peer_sigalgs);
3108 hs->peer_sigalgs = NULL;
3109 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003110
3111 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003112 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003113 return 0;
3114 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003115 num_sigalgs /= 2;
3116
3117 /* supported_signature_algorithms in the certificate request is
3118 * allowed to be empty. */
3119 if (num_sigalgs == 0) {
3120 return 1;
3121 }
3122
Steven Valdez02563852016-06-23 13:33:05 -04003123 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3124 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003125 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3126 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003127 return 0;
3128 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003129 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003130
3131 CBS sigalgs;
3132 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003133 for (size_t i = 0; i < num_sigalgs; i++) {
3134 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003135 return 0;
3136 }
3137 }
Adam Langley95c29f32014-06-20 12:00:00 -07003138
Adam Langleyfcf25832014-12-18 17:42:32 -08003139 return 1;
3140}
David Benjaminec2f27d2014-11-13 19:17:25 -05003141
David Benjaminea9a0d52016-07-08 15:52:59 -07003142int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003143 CERT *cert = ssl->cert;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003144 SSL_HANDSHAKE *hs = ssl->s3->hs;
David Benjaminec2f27d2014-11-13 19:17:25 -05003145
Steven Valdezf0451ca2016-06-29 13:16:27 -04003146 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3147 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3148 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003149 int type = ssl_private_key_type(ssl);
3150 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003151 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003152 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003153 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003154 if (ssl_is_ecdsa_key_type(type)) {
3155 *out = SSL_SIGN_ECDSA_SHA1;
3156 return 1;
3157 }
3158 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3159 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003160 }
3161
David Benjamin3ef76972016-10-17 17:59:54 -04003162 const uint16_t *sigalgs = cert->sigalgs;
3163 size_t num_sigalgs = cert->num_sigalgs;
3164 if (sigalgs == NULL) {
3165 sigalgs = kSignSignatureAlgorithms;
3166 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003167 }
3168
David Benjamin0fc37ef2016-08-17 15:29:46 -04003169 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3170 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3171 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003172 /* If the client didn't specify any signature_algorithms extension then
3173 * we can assume that it supports SHA1. See
3174 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3175 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3176 SSL_SIGN_ECDSA_SHA1};
3177 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003178 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003179 }
3180
David Benjamin0fc37ef2016-08-17 15:29:46 -04003181 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003182 uint16_t sigalg = sigalgs[i];
3183 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3184 * negotiated. */
3185 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3186 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3187 continue;
3188 }
3189
David Benjamin0fc37ef2016-08-17 15:29:46 -04003190 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003191 if (sigalg == peer_sigalgs[j]) {
3192 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003193 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003194 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003195 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003196 }
Adam Langley95c29f32014-06-20 12:00:00 -07003197
David Benjaminea9a0d52016-07-08 15:52:59 -07003198 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3199 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003200}
Adam Langley95c29f32014-06-20 12:00:00 -07003201
Nick Harper60a85cb2016-09-23 16:25:11 -07003202int tls1_verify_channel_id(SSL *ssl) {
3203 int ret = 0;
3204 uint16_t extension_type;
3205 CBS extension, channel_id;
3206
3207 /* A Channel ID handshake message is structured to contain multiple
3208 * extensions, but the only one that can be present is Channel ID. */
3209 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3210 if (!CBS_get_u16(&channel_id, &extension_type) ||
3211 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3212 CBS_len(&channel_id) != 0 ||
3213 extension_type != TLSEXT_TYPE_channel_id ||
3214 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3215 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3216 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3217 return 0;
3218 }
3219
3220 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3221 if (!p256) {
3222 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3223 return 0;
3224 }
3225
3226 EC_KEY *key = NULL;
3227 EC_POINT *point = NULL;
3228 BIGNUM x, y;
3229 ECDSA_SIG sig;
3230 BN_init(&x);
3231 BN_init(&y);
3232 sig.r = BN_new();
3233 sig.s = BN_new();
3234 if (sig.r == NULL || sig.s == NULL) {
3235 goto err;
3236 }
3237
3238 const uint8_t *p = CBS_data(&extension);
3239 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3240 BN_bin2bn(p + 32, 32, &y) == NULL ||
3241 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3242 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3243 goto err;
3244 }
3245
3246 point = EC_POINT_new(p256);
3247 if (point == NULL ||
3248 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3249 goto err;
3250 }
3251
3252 key = EC_KEY_new();
3253 if (key == NULL ||
3254 !EC_KEY_set_group(key, p256) ||
3255 !EC_KEY_set_public_key(key, point)) {
3256 goto err;
3257 }
3258
3259 uint8_t digest[EVP_MAX_MD_SIZE];
3260 size_t digest_len;
3261 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3262 goto err;
3263 }
3264
3265 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3266#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3267 sig_ok = 1;
3268#endif
3269 if (!sig_ok) {
3270 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3271 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3272 ssl->s3->tlsext_channel_id_valid = 0;
3273 goto err;
3274 }
3275
3276 memcpy(ssl->s3->tlsext_channel_id, p, 64);
3277 ret = 1;
3278
3279err:
3280 BN_free(&x);
3281 BN_free(&y);
3282 BN_free(sig.r);
3283 BN_free(sig.s);
3284 EC_KEY_free(key);
3285 EC_POINT_free(point);
3286 EC_GROUP_free(p256);
3287 return ret;
3288}
3289
3290int tls1_write_channel_id(SSL *ssl, CBB *cbb) {
3291 uint8_t digest[EVP_MAX_MD_SIZE];
3292 size_t digest_len;
3293 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3294 return 0;
3295 }
3296
3297 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3298 if (ec_key == NULL) {
3299 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3300 return 0;
3301 }
3302
3303 int ret = 0;
3304 BIGNUM *x = BN_new();
3305 BIGNUM *y = BN_new();
3306 ECDSA_SIG *sig = NULL;
3307 if (x == NULL || y == NULL ||
3308 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3309 EC_KEY_get0_public_key(ec_key),
3310 x, y, NULL)) {
3311 goto err;
3312 }
3313
3314 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3315 if (sig == NULL) {
3316 goto err;
3317 }
3318
3319 CBB child;
3320 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3321 !CBB_add_u16_length_prefixed(cbb, &child) ||
3322 !BN_bn2cbb_padded(&child, 32, x) ||
3323 !BN_bn2cbb_padded(&child, 32, y) ||
3324 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3325 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3326 !CBB_flush(cbb)) {
3327 goto err;
3328 }
3329
3330 ret = 1;
3331
3332err:
3333 BN_free(x);
3334 BN_free(y);
3335 ECDSA_SIG_free(sig);
3336 return ret;
3337}
3338
David Benjamind6a4ae92015-08-06 11:10:51 -04003339int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003340 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3341 uint8_t *msg;
3342 size_t msg_len;
3343 if (!tls13_get_cert_verify_signature_input(ssl, &msg, &msg_len,
3344 ssl_cert_verify_channel_id)) {
3345 return 0;
3346 }
3347 SHA256(msg, msg_len, out);
3348 *out_len = SHA256_DIGEST_LENGTH;
3349 OPENSSL_free(msg);
3350 return 1;
3351 }
3352
Nick Harper95594012016-10-20 14:07:13 -07003353 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003354
Nick Harper95594012016-10-20 14:07:13 -07003355 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003356 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003357 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003358
Steven Valdez87eab492016-06-27 16:34:59 -04003359 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003360 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003361 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003362 if (ssl->session->original_handshake_hash_len == 0) {
3363 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003364 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003365 }
Nick Harper95594012016-10-20 14:07:13 -07003366 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3367 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003368 }
3369
3370 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3371 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3372 sizeof(handshake_hash));
3373 if (handshake_hash_len < 0) {
Nick Harper95594012016-10-20 14:07:13 -07003374 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003375 }
Nick Harper95594012016-10-20 14:07:13 -07003376 SHA256_Update(&ctx, handshake_hash, (size_t)handshake_hash_len);
3377 SHA256_Final(out, &ctx);
3378 *out_len = SHA256_DIGEST_LENGTH;
3379 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003380}
Adam Langley1258b6a2014-06-20 12:00:00 -07003381
3382/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003383 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003384 * data. */
3385int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003386 int digest_len;
3387 /* This function should never be called for a resumed session because the
3388 * handshake hashes that we wish to record are for the original, full
3389 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003390 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003391 return -1;
3392 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003393
Adam Langleyfcf25832014-12-18 17:42:32 -08003394 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003395 tls1_handshake_digest(
3396 ssl, ssl->s3->new_session->original_handshake_hash,
3397 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003398 if (digest_len < 0) {
3399 return -1;
3400 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003401
Steven Valdez87eab492016-06-27 16:34:59 -04003402 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003403
Adam Langleyfcf25832014-12-18 17:42:32 -08003404 return 1;
3405}
Nick Harper60a85cb2016-09-23 16:25:11 -07003406
3407int ssl_do_channel_id_callback(SSL *ssl) {
3408 if (ssl->tlsext_channel_id_private != NULL ||
3409 ssl->ctx->channel_id_cb == NULL) {
3410 return 1;
3411 }
3412
3413 EVP_PKEY *key = NULL;
3414 ssl->ctx->channel_id_cb(ssl, &key);
3415 if (key == NULL) {
3416 /* The caller should try again later. */
3417 return 1;
3418 }
3419
3420 int ret = SSL_set1_tls_channel_id(ssl, key);
3421 EVP_PKEY_free(key);
3422 return ret;
3423}