blob: 24e4548d2eda94edaf527dacc155a47a0cd9054a [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
David Benjamin3a322f52016-10-26 12:45:35 -0400445 * algorithms for verifying.
446 *
447 * For now, RSA-PSS signature algorithms are not enabled on Android's system
448 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
449 * restore them. */
David Benjamin3ef76972016-10-17 17:59:54 -0400450static const uint16_t kVerifySignatureAlgorithms[] = {
David Benjamin3a322f52016-10-26 12:45:35 -0400451 /* Prefer SHA-256 algorithms. */
452 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400453#if !defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin3a322f52016-10-26 12:45:35 -0400454 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin3ef76972016-10-17 17:59:54 -0400455#endif
David Benjamin3a322f52016-10-26 12:45:35 -0400456 SSL_SIGN_RSA_PKCS1_SHA256,
457
458 /* Larger hashes are acceptable. */
459 SSL_SIGN_ECDSA_SECP384R1_SHA384,
460#if !defined(BORINGSSL_ANDROID_SYSTEM)
461 SSL_SIGN_RSA_PSS_SHA384,
462#endif
463 SSL_SIGN_RSA_PKCS1_SHA384,
464
David Benjamin3ef76972016-10-17 17:59:54 -0400465 /* TODO(davidben): Remove this entry and SSL_CURVE_SECP521R1 from
466 * kDefaultGroups. */
467#if defined(BORINGSSL_ANDROID_SYSTEM)
468 SSL_SIGN_ECDSA_SECP521R1_SHA512,
469#endif
David Benjamin57e929f2016-08-30 00:30:38 -0400470#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400471 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400472#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400473 SSL_SIGN_RSA_PKCS1_SHA512,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400474
David Benjamin3a322f52016-10-26 12:45:35 -0400475 /* For now, SHA-1 is still accepted but least preferable. */
476 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400477
David Benjamin3a322f52016-10-26 12:45:35 -0400478};
479
480/* kSignSignatureAlgorithms is the default list of supported signature
481 * algorithms for signing.
482 *
483 * For now, RSA-PSS signature algorithms are not enabled on Android's system
484 * BoringSSL. Once the change in Chrome has stuck and the values are finalized,
485 * restore them. */
486static const uint16_t kSignSignatureAlgorithms[] = {
487 /* Prefer SHA-256 algorithms. */
488 SSL_SIGN_ECDSA_SECP256R1_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400489#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400490 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400491#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400492 SSL_SIGN_RSA_PKCS1_SHA256,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400493
David Benjamin3a322f52016-10-26 12:45:35 -0400494 /* If needed, sign larger hashes.
495 *
496 * TODO(davidben): Determine which of these may be pruned. */
497 SSL_SIGN_ECDSA_SECP384R1_SHA384,
498#if !defined(BORINGSSL_ANDROID_SYSTEM)
499 SSL_SIGN_RSA_PSS_SHA384,
500#endif
501 SSL_SIGN_RSA_PKCS1_SHA384,
502
503 SSL_SIGN_ECDSA_SECP521R1_SHA512,
504#if !defined(BORINGSSL_ANDROID_SYSTEM)
505 SSL_SIGN_RSA_PSS_SHA512,
506#endif
507 SSL_SIGN_RSA_PKCS1_SHA512,
508
509 /* If the peer supports nothing else, sign with SHA-1. */
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400510 SSL_SIGN_ECDSA_SHA1,
David Benjamin3a322f52016-10-26 12:45:35 -0400511 SSL_SIGN_RSA_PKCS1_SHA1,
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400512};
513
David Benjamin3ef76972016-10-17 17:59:54 -0400514size_t tls12_get_verify_sigalgs(const SSL *ssl, const uint16_t **out) {
515 *out = kVerifySignatureAlgorithms;
516 return OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
Adam Langleyfcf25832014-12-18 17:42:32 -0800517}
Adam Langley95c29f32014-06-20 12:00:00 -0700518
David Benjamin887c3002016-07-08 16:15:32 -0700519int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
David Benjamin3ef76972016-10-17 17:59:54 -0400520 const uint16_t *verify_sigalgs;
521 size_t num_verify_sigalgs = tls12_get_verify_sigalgs(ssl, &verify_sigalgs);
522 for (size_t i = 0; i < num_verify_sigalgs; i++) {
523 if (sigalg == verify_sigalgs[i]) {
524 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 }
526 }
527
David Benjamin3ef76972016-10-17 17:59:54 -0400528 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
529 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
530 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800531}
532
533/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
534 * supported or doesn't appear in supported signature algorithms. Unlike
535 * ssl_cipher_get_disabled this applies to a specific session and not global
536 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500537void ssl_set_client_disabled(SSL *ssl) {
538 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 int have_rsa = 0, have_ecdsa = 0;
540 c->mask_a = 0;
541 c->mask_k = 0;
542
David Benjamin3ef76972016-10-17 17:59:54 -0400543 /* Now go through all signature algorithms seeing if we support any for RSA or
544 * ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin0fc37ef2016-08-17 15:29:46 -0400545 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -0400546 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
David Benjamin0fc37ef2016-08-17 15:29:46 -0400547 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -0400548 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400549 case SSL_SIGN_RSA_PSS_SHA512:
550 case SSL_SIGN_RSA_PSS_SHA384:
551 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400552 case SSL_SIGN_RSA_PKCS1_SHA512:
553 case SSL_SIGN_RSA_PKCS1_SHA384:
554 case SSL_SIGN_RSA_PKCS1_SHA256:
555 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800556 have_rsa = 1;
557 break;
558
Steven Valdez02563852016-06-23 13:33:05 -0400559 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
560 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
561 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
562 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800563 have_ecdsa = 1;
564 break;
565 }
566 }
567
568 /* Disable auth if we don't include any appropriate signature algorithms. */
569 if (!have_rsa) {
570 c->mask_a |= SSL_aRSA;
571 }
572 if (!have_ecdsa) {
573 c->mask_a |= SSL_aECDSA;
574 }
575
576 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500577 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800578 c->mask_a |= SSL_aPSK;
579 c->mask_k |= SSL_kPSK;
580 }
581}
Adam Langley95c29f32014-06-20 12:00:00 -0700582
Adam Langley614c66a2015-06-12 15:26:58 -0700583/* tls_extension represents a TLS extension that is handled internally. The
584 * |init| function is called for each handshake, before any other functions of
585 * the extension. Then the add and parse callbacks are called as needed.
586 *
587 * The parse callbacks receive a |CBS| that contains the contents of the
588 * extension (i.e. not including the type and length bytes). If an extension is
589 * not received then the parse callbacks will be called with a NULL CBS so that
590 * they can do any processing needed to handle the absence of an extension.
591 *
592 * The add callbacks receive a |CBB| to which the extension can be appended but
593 * the function is responsible for appending the type and length bytes too.
594 *
595 * All callbacks return one for success and zero for error. If a parse function
596 * returns zero then a fatal alert with value |*out_alert| will be sent. If
597 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
598struct tls_extension {
599 uint16_t value;
600 void (*init)(SSL *ssl);
601
602 int (*add_clienthello)(SSL *ssl, CBB *out);
603 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
604
605 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
606 int (*add_serverhello)(SSL *ssl, CBB *out);
607};
608
Steven Valdez6b8509a2016-07-12 13:38:32 -0400609static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
610 if (contents != NULL) {
611 /* Servers MUST NOT send this extension. */
612 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
613 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
614 return 0;
615 }
616
617 return 1;
618}
619
620static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
621 /* This extension from the client is handled elsewhere. */
622 return 1;
623}
624
625static int dont_add_serverhello(SSL *ssl, CBB *out) {
626 return 1;
627}
Adam Langley614c66a2015-06-12 15:26:58 -0700628
629/* Server name indication (SNI).
630 *
631 * https://tools.ietf.org/html/rfc6066#section-3. */
632
Adam Langley614c66a2015-06-12 15:26:58 -0700633static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
634 if (ssl->tlsext_hostname == NULL) {
635 return 1;
636 }
637
638 CBB contents, server_name_list, name;
639 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
640 !CBB_add_u16_length_prefixed(out, &contents) ||
641 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
642 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
643 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
644 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
645 strlen(ssl->tlsext_hostname)) ||
646 !CBB_flush(out)) {
647 return 0;
648 }
649
650 return 1;
651}
652
David Benjamin0d56f882015-12-19 17:05:56 -0500653static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
654 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700655 if (contents == NULL) {
656 return 1;
657 }
658
659 if (CBS_len(contents) != 0) {
660 return 0;
661 }
662
663 assert(ssl->tlsext_hostname != NULL);
664
Steven Valdez87eab492016-06-27 16:34:59 -0400665 if (ssl->session == NULL) {
666 assert(ssl->s3->new_session->tlsext_hostname == NULL);
667 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
668 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700669 *out_alert = SSL_AD_INTERNAL_ERROR;
670 return 0;
671 }
672 }
673
674 return 1;
675}
676
David Benjamin0d56f882015-12-19 17:05:56 -0500677static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
678 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700679 if (contents == NULL) {
680 return 1;
681 }
682
David Benjamin9b611e22016-03-03 08:48:30 -0500683 CBS server_name_list, host_name;
684 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700685 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500686 !CBS_get_u8(&server_name_list, &name_type) ||
687 /* Although the server_name extension was intended to be extensible to
688 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
689 * different name types will cause an error. Further, RFC 4366 originally
690 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
691 * adding new name types is no longer feasible.
692 *
693 * Act as if the extensibility does not exist to simplify parsing. */
694 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
695 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700696 CBS_len(contents) != 0) {
697 return 0;
698 }
699
David Benjamin9b611e22016-03-03 08:48:30 -0500700 if (name_type != TLSEXT_NAMETYPE_host_name ||
701 CBS_len(&host_name) == 0 ||
702 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
703 CBS_contains_zero_byte(&host_name)) {
704 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
705 return 0;
706 }
Adam Langley614c66a2015-06-12 15:26:58 -0700707
David Benjamin9b611e22016-03-03 08:48:30 -0500708 /* TODO(davidben): SNI should be resolved before resumption. We have the
709 * early callback as a replacement, but we should fix the current callback
710 * and avoid the need for |SSL_CTX_set_session_id_context|. */
Steven Valdez87eab492016-06-27 16:34:59 -0400711 if (ssl->session == NULL) {
712 assert(ssl->s3->new_session->tlsext_hostname == NULL);
David Benjamin9b611e22016-03-03 08:48:30 -0500713
714 /* Copy the hostname as a string. */
Steven Valdez87eab492016-06-27 16:34:59 -0400715 if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) {
David Benjamin9b611e22016-03-03 08:48:30 -0500716 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700717 return 0;
718 }
719
David Benjamina0486782016-10-06 19:11:32 -0400720 ssl->s3->hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700721 }
722
723 return 1;
724}
725
726static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez87eab492016-06-27 16:34:59 -0400727 if (ssl->session != NULL ||
David Benjamina0486782016-10-06 19:11:32 -0400728 !ssl->s3->hs->should_ack_sni ||
Steven Valdez87eab492016-06-27 16:34:59 -0400729 ssl->s3->new_session->tlsext_hostname == NULL) {
Adam Langley614c66a2015-06-12 15:26:58 -0700730 return 1;
731 }
732
733 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
734 !CBB_add_u16(out, 0 /* length */)) {
735 return 0;
736 }
737
738 return 1;
739}
740
741
Adam Langley5021b222015-06-12 18:27:58 -0700742/* Renegotiation indication.
743 *
744 * https://tools.ietf.org/html/rfc5746 */
745
746static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400747 uint16_t min_version, max_version;
748 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
749 return 0;
750 }
751
752 /* Renegotiation indication is not necessary in TLS 1.3. */
753 if (min_version >= TLS1_3_VERSION) {
754 return 1;
755 }
756
David Benjamin52bf6902016-10-08 12:05:03 -0400757 assert(ssl->s3->initial_handshake_complete ==
758 (ssl->s3->previous_client_finished_len != 0));
759
Adam Langley5021b222015-06-12 18:27:58 -0700760 CBB contents, prev_finished;
761 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
762 !CBB_add_u16_length_prefixed(out, &contents) ||
763 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
764 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
765 ssl->s3->previous_client_finished_len) ||
766 !CBB_flush(out)) {
767 return 0;
768 }
769
770 return 1;
771}
772
773static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
774 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400775 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
776 return 0;
777 }
778
David Benjamin3e052de2015-11-25 20:10:31 -0500779 /* Servers may not switch between omitting the extension and supporting it.
780 * See RFC 5746, sections 3.5 and 4.2. */
781 if (ssl->s3->initial_handshake_complete &&
782 (contents != NULL) != ssl->s3->send_connection_binding) {
783 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
784 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
785 return 0;
786 }
787
Adam Langley5021b222015-06-12 18:27:58 -0700788 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500789 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700790 * RI even on initial ServerHello because the client doesn't see any
791 * renegotiation during an attack. However this would mean we could not
792 * connect to any server which doesn't support RI.
793 *
David Benjamine9cddb82015-11-23 14:36:40 -0500794 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
795 * practical terms every client sets it so it's just assumed here. */
796 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700797 }
798
799 const size_t expected_len = ssl->s3->previous_client_finished_len +
800 ssl->s3->previous_server_finished_len;
801
802 /* Check for logic errors */
803 assert(!expected_len || ssl->s3->previous_client_finished_len);
804 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400805 assert(ssl->s3->initial_handshake_complete ==
806 (ssl->s3->previous_client_finished_len != 0));
807 assert(ssl->s3->initial_handshake_complete ==
808 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700809
810 /* Parse out the extension contents. */
811 CBS renegotiated_connection;
812 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
813 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400814 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700815 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
816 return 0;
817 }
818
819 /* Check that the extension matches. */
820 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400821 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700822 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
823 return 0;
824 }
825
826 const uint8_t *d = CBS_data(&renegotiated_connection);
827 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
828 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400829 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700830 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
831 return 0;
832 }
833 d += ssl->s3->previous_client_finished_len;
834
835 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
836 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400837 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700838 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
839 return 0;
840 }
841 ssl->s3->send_connection_binding = 1;
842
843 return 1;
844}
845
846static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
847 CBS *contents) {
848 /* Renegotiation isn't supported as a server so this function should never be
849 * called after the initial handshake. */
850 assert(!ssl->s3->initial_handshake_complete);
851
Steven Valdez143e8b32016-07-11 13:19:03 -0400852 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
853 return 1;
854 }
855
Adam Langley5021b222015-06-12 18:27:58 -0700856 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400857 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700858 }
859
860 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700861 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
862 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400863 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700864 return 0;
865 }
866
David Benjamin52bf6902016-10-08 12:05:03 -0400867 /* Check that the extension matches. We do not support renegotiation as a
868 * server, so this must be empty. */
869 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400870 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700871 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
872 return 0;
873 }
874
875 ssl->s3->send_connection_binding = 1;
876
877 return 1;
878}
879
880static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin52bf6902016-10-08 12:05:03 -0400881 /* Renegotiation isn't supported as a server so this function should never be
882 * called after the initial handshake. */
883 assert(!ssl->s3->initial_handshake_complete);
884
Steven Valdez143e8b32016-07-11 13:19:03 -0400885 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
886 return 1;
887 }
888
Adam Langley5021b222015-06-12 18:27:58 -0700889 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400890 !CBB_add_u16(out, 1 /* length */) ||
891 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700892 return 0;
893 }
894
895 return 1;
896}
897
Adam Langley0a056712015-07-01 15:03:33 -0700898
899/* Extended Master Secret.
900 *
David Benjamin43946d42016-02-01 08:42:19 -0500901 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700902
Adam Langley0a056712015-07-01 15:03:33 -0700903static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400904 uint16_t min_version, max_version;
905 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
906 return 0;
907 }
908
909 /* Extended master secret is not necessary in TLS 1.3. */
910 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700911 return 1;
912 }
913
914 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
915 !CBB_add_u16(out, 0 /* length */)) {
916 return 0;
917 }
918
919 return 1;
920}
921
922static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
923 CBS *contents) {
David Benjamin163c9562016-08-29 23:14:17 -0400924 /* Whether EMS is negotiated may not change on renegotation. */
925 if (ssl->s3->initial_handshake_complete) {
926 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
927 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
928 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
929 return 0;
930 }
931
932 return 1;
933 }
934
Adam Langley0a056712015-07-01 15:03:33 -0700935 if (contents == NULL) {
936 return 1;
937 }
938
Steven Valdez143e8b32016-07-11 13:19:03 -0400939 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
940 ssl->version == SSL3_VERSION) {
941 return 0;
942 }
943
944 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700945 return 0;
946 }
947
948 ssl->s3->tmp.extended_master_secret = 1;
949 return 1;
950}
951
David Benjamin0d56f882015-12-19 17:05:56 -0500952static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
953 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400954 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
955 ssl->version == SSL3_VERSION) {
956 return 1;
957 }
958
959 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700960 return 1;
961 }
962
963 if (CBS_len(contents) != 0) {
964 return 0;
965 }
966
967 ssl->s3->tmp.extended_master_secret = 1;
968 return 1;
969}
970
971static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
972 if (!ssl->s3->tmp.extended_master_secret) {
973 return 1;
974 }
975
976 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
977 !CBB_add_u16(out, 0 /* length */)) {
978 return 0;
979 }
980
981 return 1;
982}
983
Adam Langley9b05bc52015-07-01 15:25:33 -0700984
985/* Session tickets.
986 *
987 * https://tools.ietf.org/html/rfc5077 */
988
989static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400990 uint16_t min_version, max_version;
991 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
992 return 0;
993 }
994
995 /* TLS 1.3 uses a different ticket extension. */
996 if (min_version >= TLS1_3_VERSION ||
997 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700998 return 1;
999 }
1000
1001 const uint8_t *ticket_data = NULL;
1002 int ticket_len = 0;
1003
1004 /* Renegotiation does not participate in session resumption. However, still
1005 * advertise the extension to avoid potentially breaking servers which carry
1006 * over the state from the previous handshake, such as OpenSSL servers
1007 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -04001008 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -07001009 if (!ssl->s3->initial_handshake_complete &&
1010 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001011 ssl->session->tlsext_tick != NULL &&
1012 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -04001013 ssl->method->version_from_wire(&session_version,
1014 ssl->session->ssl_version) &&
1015 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001016 ticket_data = ssl->session->tlsext_tick;
1017 ticket_len = ssl->session->tlsext_ticklen;
1018 }
1019
1020 CBB ticket;
1021 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1022 !CBB_add_u16_length_prefixed(out, &ticket) ||
1023 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1024 !CBB_flush(out)) {
1025 return 0;
1026 }
1027
1028 return 1;
1029}
1030
1031static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1032 CBS *contents) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001033 if (contents == NULL) {
1034 return 1;
1035 }
1036
Steven Valdez143e8b32016-07-11 13:19:03 -04001037 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1038 return 0;
1039 }
1040
Adam Langley9b05bc52015-07-01 15:25:33 -07001041 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1042 * this function should never be called, even if the server tries to send the
1043 * extension. */
1044 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1045
1046 if (CBS_len(contents) != 0) {
1047 return 0;
1048 }
1049
David Benjamin78476f62016-11-12 11:20:55 +09001050 ssl->s3->hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001051 return 1;
1052}
1053
Adam Langley9b05bc52015-07-01 15:25:33 -07001054static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin78476f62016-11-12 11:20:55 +09001055 if (!ssl->s3->hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001056 return 1;
1057 }
1058
David Benjamin78476f62016-11-12 11:20:55 +09001059 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
Adam Langley9b05bc52015-07-01 15:25:33 -07001060 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1061
1062 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1063 !CBB_add_u16(out, 0 /* length */)) {
1064 return 0;
1065 }
1066
1067 return 1;
1068}
1069
1070
Adam Langley2e857bd2015-07-01 16:09:19 -07001071/* Signature Algorithms.
1072 *
1073 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1074
1075static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin2dc02042016-09-19 19:57:37 -04001076 uint16_t min_version, max_version;
1077 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1078 return 0;
1079 }
1080
1081 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001082 return 1;
1083 }
1084
David Benjamin0fc37ef2016-08-17 15:29:46 -04001085 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001086 const size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001087
David Benjamin0fc37ef2016-08-17 15:29:46 -04001088 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001089 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1090 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001091 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001092 return 0;
1093 }
1094
David Benjamin0fc37ef2016-08-17 15:29:46 -04001095 for (size_t i = 0; i < num_sigalgs; i++) {
1096 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001097 return 0;
1098 }
1099 }
1100
1101 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001102 return 0;
1103 }
1104
1105 return 1;
1106}
1107
Adam Langley2e857bd2015-07-01 16:09:19 -07001108static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1109 CBS *contents) {
David Benjamin0fc37ef2016-08-17 15:29:46 -04001110 OPENSSL_free(ssl->s3->hs->peer_sigalgs);
1111 ssl->s3->hs->peer_sigalgs = NULL;
1112 ssl->s3->hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001113
Adam Langley2e857bd2015-07-01 16:09:19 -07001114 if (contents == NULL) {
1115 return 1;
1116 }
1117
1118 CBS supported_signature_algorithms;
1119 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001120 CBS_len(contents) != 0 ||
1121 CBS_len(&supported_signature_algorithms) == 0 ||
1122 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001123 return 0;
1124 }
1125
1126 return 1;
1127}
1128
Adam Langley2e857bd2015-07-01 16:09:19 -07001129
Adam Langleybb0bd042015-07-01 16:21:03 -07001130/* OCSP Stapling.
1131 *
1132 * https://tools.ietf.org/html/rfc6066#section-8 */
1133
1134static void ext_ocsp_init(SSL *ssl) {
Adam Langleyce9d85e2016-01-24 15:58:39 -08001135 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001136}
1137
1138static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1139 if (!ssl->ocsp_stapling_enabled) {
1140 return 1;
1141 }
1142
1143 CBB contents;
1144 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1145 !CBB_add_u16_length_prefixed(out, &contents) ||
1146 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1147 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1148 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1149 !CBB_flush(out)) {
1150 return 0;
1151 }
1152
Adam Langleyce9d85e2016-01-24 15:58:39 -08001153 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001154 return 1;
1155}
1156
1157static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001158 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001159 if (contents == NULL) {
1160 return 1;
1161 }
1162
Steven Valdeza833c352016-11-01 13:39:36 -04001163 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1164 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001165 return 0;
1166 }
1167
Steven Valdeza833c352016-11-01 13:39:36 -04001168 /* OCSP stapling is forbidden on non-certificate ciphers. */
1169 if (CBS_len(contents) != 0 ||
1170 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001171 return 0;
1172 }
1173
Steven Valdeza833c352016-11-01 13:39:36 -04001174 /* Note this does not check for resumption in TLS 1.2. Sending
1175 * status_request here does not make sense, but OpenSSL does so and the
1176 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001177
Steven Valdeza833c352016-11-01 13:39:36 -04001178 ssl->s3->hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001179 return 1;
1180}
1181
1182static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1183 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001184 if (contents == NULL) {
1185 return 1;
1186 }
1187
1188 uint8_t status_type;
1189 if (!CBS_get_u8(contents, &status_type)) {
1190 return 0;
1191 }
1192
1193 /* We cannot decide whether OCSP stapling will occur yet because the correct
1194 * SSL_CTX might not have been selected. */
David Benjamina0486782016-10-06 19:11:32 -04001195 ssl->s3->hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001196
Adam Langleybb0bd042015-07-01 16:21:03 -07001197 return 1;
1198}
1199
1200static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdeza833c352016-11-01 13:39:36 -04001201 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1202 !ssl->s3->hs->ocsp_stapling_requested ||
David Benjamin942f4ed2016-07-16 19:03:49 +03001203 ssl->ctx->ocsp_response_length == 0 ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001204 ssl->s3->session_reused ||
Steven Valdeza833c352016-11-01 13:39:36 -04001205 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001206 return 1;
1207 }
1208
Steven Valdeza833c352016-11-01 13:39:36 -04001209 ssl->s3->hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001210
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001211 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001212 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001213}
1214
1215
Adam Langley97dfcbf2015-07-01 18:35:20 -07001216/* Next protocol negotiation.
1217 *
1218 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1219
Adam Langley97dfcbf2015-07-01 18:35:20 -07001220static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1221 if (ssl->s3->initial_handshake_complete ||
1222 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001223 (ssl->options & SSL_OP_DISABLE_NPN) ||
David Benjamince079fd2016-08-02 16:22:34 -04001224 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001225 return 1;
1226 }
1227
1228 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1229 !CBB_add_u16(out, 0 /* length */)) {
1230 return 0;
1231 }
1232
1233 return 1;
1234}
1235
1236static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1237 CBS *contents) {
1238 if (contents == NULL) {
1239 return 1;
1240 }
1241
Steven Valdez143e8b32016-07-11 13:19:03 -04001242 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1243 return 0;
1244 }
1245
Adam Langley97dfcbf2015-07-01 18:35:20 -07001246 /* If any of these are false then we should never have sent the NPN
1247 * extension in the ClientHello and thus this function should never have been
1248 * called. */
1249 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001250 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001251 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001252 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001253
David Benjamin76c2efc2015-08-31 14:24:29 -04001254 if (ssl->s3->alpn_selected != NULL) {
1255 /* NPN and ALPN may not be negotiated in the same connection. */
1256 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1257 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1258 return 0;
1259 }
1260
Adam Langley97dfcbf2015-07-01 18:35:20 -07001261 const uint8_t *const orig_contents = CBS_data(contents);
1262 const size_t orig_len = CBS_len(contents);
1263
1264 while (CBS_len(contents) != 0) {
1265 CBS proto;
1266 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1267 CBS_len(&proto) == 0) {
1268 return 0;
1269 }
1270 }
1271
1272 uint8_t *selected;
1273 uint8_t selected_len;
1274 if (ssl->ctx->next_proto_select_cb(
1275 ssl, &selected, &selected_len, orig_contents, orig_len,
1276 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1277 *out_alert = SSL_AD_INTERNAL_ERROR;
1278 return 0;
1279 }
1280
David Benjamin79978df2015-12-25 15:56:49 -05001281 OPENSSL_free(ssl->s3->next_proto_negotiated);
1282 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1283 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001284 *out_alert = SSL_AD_INTERNAL_ERROR;
1285 return 0;
1286 }
1287
David Benjamin79978df2015-12-25 15:56:49 -05001288 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjaminb74b0812016-10-06 19:43:48 -04001289 ssl->s3->hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001290
1291 return 1;
1292}
1293
1294static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1295 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001296 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1297 return 1;
1298 }
1299
Adam Langley97dfcbf2015-07-01 18:35:20 -07001300 if (contents != NULL && CBS_len(contents) != 0) {
1301 return 0;
1302 }
1303
1304 if (contents == NULL ||
1305 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001306 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001307 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001308 return 1;
1309 }
1310
David Benjaminb74b0812016-10-06 19:43:48 -04001311 ssl->s3->hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001312 return 1;
1313}
1314
1315static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1316 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1317 * parsed. */
David Benjaminb74b0812016-10-06 19:43:48 -04001318 if (!ssl->s3->hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001319 return 1;
1320 }
1321
1322 const uint8_t *npa;
1323 unsigned npa_len;
1324
1325 if (ssl->ctx->next_protos_advertised_cb(
1326 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1327 SSL_TLSEXT_ERR_OK) {
David Benjaminb74b0812016-10-06 19:43:48 -04001328 ssl->s3->hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001329 return 1;
1330 }
1331
1332 CBB contents;
1333 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1334 !CBB_add_u16_length_prefixed(out, &contents) ||
1335 !CBB_add_bytes(&contents, npa, npa_len) ||
1336 !CBB_flush(out)) {
1337 return 0;
1338 }
1339
1340 return 1;
1341}
1342
1343
Adam Langleyab8d87d2015-07-10 12:21:39 -07001344/* Signed certificate timestamps.
1345 *
1346 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1347
1348static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1349 if (!ssl->signed_cert_timestamps_enabled) {
1350 return 1;
1351 }
1352
1353 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1354 !CBB_add_u16(out, 0 /* length */)) {
1355 return 0;
1356 }
1357
1358 return 1;
1359}
1360
1361static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1362 CBS *contents) {
1363 if (contents == NULL) {
1364 return 1;
1365 }
1366
Steven Valdeza833c352016-11-01 13:39:36 -04001367 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1368 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1369 return 0;
1370 }
1371
Adam Langleyab8d87d2015-07-10 12:21:39 -07001372 /* If this is false then we should never have sent the SCT extension in the
1373 * ClientHello and thus this function should never have been called. */
1374 assert(ssl->signed_cert_timestamps_enabled);
1375
1376 if (CBS_len(contents) == 0) {
1377 *out_alert = SSL_AD_DECODE_ERROR;
1378 return 0;
1379 }
1380
David Benjamindaa88502016-10-04 16:32:16 -04001381 /* Session resumption uses the original session information. The extension
1382 * should not be sent on resumption, but RFC 6962 did not make it a
1383 * requirement, so tolerate this.
1384 *
1385 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001386 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001387 !CBS_stow(
1388 contents,
1389 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1390 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001391 *out_alert = SSL_AD_INTERNAL_ERROR;
1392 return 0;
1393 }
1394
1395 return 1;
1396}
1397
1398static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1399 CBS *contents) {
Steven Valdeza833c352016-11-01 13:39:36 -04001400 ssl->s3->hs->scts_requested = 1;
Paul Lietar4fac72e2015-09-09 13:44:55 +01001401 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001402}
1403
1404static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001405 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdeza833c352016-11-01 13:39:36 -04001406 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1407 ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001408 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001409 return 1;
1410 }
1411
1412 CBB contents;
1413 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1414 CBB_add_u16_length_prefixed(out, &contents) &&
1415 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1416 ssl->ctx->signed_cert_timestamp_list_length) &&
1417 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001418}
1419
1420
Adam Langleyf18e4532015-07-10 13:39:53 -07001421/* Application-level Protocol Negotiation.
1422 *
1423 * https://tools.ietf.org/html/rfc7301 */
1424
1425static void ext_alpn_init(SSL *ssl) {
1426 OPENSSL_free(ssl->s3->alpn_selected);
1427 ssl->s3->alpn_selected = NULL;
1428}
1429
1430static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1431 if (ssl->alpn_client_proto_list == NULL ||
1432 ssl->s3->initial_handshake_complete) {
1433 return 1;
1434 }
1435
1436 CBB contents, proto_list;
1437 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1438 !CBB_add_u16_length_prefixed(out, &contents) ||
1439 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1440 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1441 ssl->alpn_client_proto_list_len) ||
1442 !CBB_flush(out)) {
1443 return 0;
1444 }
1445
1446 return 1;
1447}
1448
1449static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1450 CBS *contents) {
1451 if (contents == NULL) {
1452 return 1;
1453 }
1454
1455 assert(!ssl->s3->initial_handshake_complete);
1456 assert(ssl->alpn_client_proto_list != NULL);
1457
David Benjaminb74b0812016-10-06 19:43:48 -04001458 if (ssl->s3->hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001459 /* NPN and ALPN may not be negotiated in the same connection. */
1460 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1461 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1462 return 0;
1463 }
1464
Adam Langleyf18e4532015-07-10 13:39:53 -07001465 /* The extension data consists of a ProtocolNameList which must have
1466 * exactly one ProtocolName. Each of these is length-prefixed. */
1467 CBS protocol_name_list, protocol_name;
1468 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1469 CBS_len(contents) != 0 ||
1470 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1471 /* Empty protocol names are forbidden. */
1472 CBS_len(&protocol_name) == 0 ||
1473 CBS_len(&protocol_name_list) != 0) {
1474 return 0;
1475 }
1476
David Benjamin3e517572016-08-11 11:52:23 -04001477 /* Check that the protcol name is one of the ones we advertised. */
1478 int protocol_ok = 0;
1479 CBS client_protocol_name_list, client_protocol_name;
1480 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1481 ssl->alpn_client_proto_list_len);
1482 while (CBS_len(&client_protocol_name_list) > 0) {
1483 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1484 &client_protocol_name)) {
1485 *out_alert = SSL_AD_INTERNAL_ERROR;
1486 return 0;
1487 }
1488
1489 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1490 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1491 CBS_len(&protocol_name)) == 0) {
1492 protocol_ok = 1;
1493 break;
1494 }
1495 }
1496
1497 if (!protocol_ok) {
1498 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1499 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1500 return 0;
1501 }
1502
Adam Langleyf18e4532015-07-10 13:39:53 -07001503 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1504 &ssl->s3->alpn_selected_len)) {
1505 *out_alert = SSL_AD_INTERNAL_ERROR;
1506 return 0;
1507 }
1508
1509 return 1;
1510}
1511
David Benjamin9ef31f02016-10-31 18:01:13 -04001512int ssl_negotiate_alpn(SSL *ssl, uint8_t *out_alert,
1513 const struct ssl_early_callback_ctx *client_hello) {
1514 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001515 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin9ef31f02016-10-31 18:01:13 -04001516 !ssl_early_callback_get_extension(
1517 client_hello, &contents,
1518 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1519 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001520 return 1;
1521 }
1522
1523 /* ALPN takes precedence over NPN. */
David Benjaminb74b0812016-10-06 19:43:48 -04001524 ssl->s3->hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001525
1526 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001527 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1528 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001529 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001530 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1531 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001532 return 0;
1533 }
1534
1535 /* Validate the protocol list. */
1536 CBS protocol_name_list_copy = protocol_name_list;
1537 while (CBS_len(&protocol_name_list_copy) > 0) {
1538 CBS protocol_name;
1539
1540 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1541 /* Empty protocol names are forbidden. */
1542 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001543 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1544 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001545 return 0;
1546 }
1547 }
1548
1549 const uint8_t *selected;
1550 uint8_t selected_len;
1551 if (ssl->ctx->alpn_select_cb(
1552 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1553 CBS_len(&protocol_name_list),
1554 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1555 OPENSSL_free(ssl->s3->alpn_selected);
1556 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1557 if (ssl->s3->alpn_selected == NULL) {
1558 *out_alert = SSL_AD_INTERNAL_ERROR;
1559 return 0;
1560 }
1561 ssl->s3->alpn_selected_len = selected_len;
1562 }
1563
1564 return 1;
1565}
1566
1567static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1568 if (ssl->s3->alpn_selected == NULL) {
1569 return 1;
1570 }
1571
1572 CBB contents, proto_list, proto;
1573 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1574 !CBB_add_u16_length_prefixed(out, &contents) ||
1575 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1576 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001577 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1578 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001579 !CBB_flush(out)) {
1580 return 0;
1581 }
1582
1583 return 1;
1584}
1585
1586
Adam Langley49c7af12015-07-10 14:33:46 -07001587/* Channel ID.
1588 *
1589 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1590
1591static void ext_channel_id_init(SSL *ssl) {
1592 ssl->s3->tlsext_channel_id_valid = 0;
1593}
1594
1595static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1596 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001597 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001598 return 1;
1599 }
1600
1601 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1602 !CBB_add_u16(out, 0 /* length */)) {
1603 return 0;
1604 }
1605
1606 return 1;
1607}
1608
1609static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1610 CBS *contents) {
1611 if (contents == NULL) {
1612 return 1;
1613 }
1614
David Benjamince079fd2016-08-02 16:22:34 -04001615 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001616 assert(ssl->tlsext_channel_id_enabled);
1617
1618 if (CBS_len(contents) != 0) {
1619 return 0;
1620 }
1621
1622 ssl->s3->tlsext_channel_id_valid = 1;
1623 return 1;
1624}
1625
1626static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1627 CBS *contents) {
1628 if (contents == NULL ||
1629 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001630 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001631 return 1;
1632 }
1633
1634 if (CBS_len(contents) != 0) {
1635 return 0;
1636 }
1637
1638 ssl->s3->tlsext_channel_id_valid = 1;
1639 return 1;
1640}
1641
1642static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1643 if (!ssl->s3->tlsext_channel_id_valid) {
1644 return 1;
1645 }
1646
1647 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1648 !CBB_add_u16(out, 0 /* length */)) {
1649 return 0;
1650 }
1651
1652 return 1;
1653}
1654
Adam Langley391250d2015-07-15 19:06:07 -07001655
1656/* Secure Real-time Transport Protocol (SRTP) extension.
1657 *
1658 * https://tools.ietf.org/html/rfc5764 */
1659
Adam Langley391250d2015-07-15 19:06:07 -07001660
1661static void ext_srtp_init(SSL *ssl) {
1662 ssl->srtp_profile = NULL;
1663}
1664
1665static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1666 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1667 if (profiles == NULL) {
1668 return 1;
1669 }
1670 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1671 if (num_profiles == 0) {
1672 return 1;
1673 }
1674
1675 CBB contents, profile_ids;
1676 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1677 !CBB_add_u16_length_prefixed(out, &contents) ||
1678 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1679 return 0;
1680 }
1681
David Benjamin54091232016-09-05 12:47:25 -04001682 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001683 if (!CBB_add_u16(&profile_ids,
1684 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1685 return 0;
1686 }
1687 }
1688
1689 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1690 !CBB_flush(out)) {
1691 return 0;
1692 }
1693
1694 return 1;
1695}
1696
1697static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1698 CBS *contents) {
1699 if (contents == NULL) {
1700 return 1;
1701 }
1702
1703 /* The extension consists of a u16-prefixed profile ID list containing a
1704 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1705 *
1706 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1707 CBS profile_ids, srtp_mki;
1708 uint16_t profile_id;
1709 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1710 !CBS_get_u16(&profile_ids, &profile_id) ||
1711 CBS_len(&profile_ids) != 0 ||
1712 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1713 CBS_len(contents) != 0) {
1714 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1715 return 0;
1716 }
1717
1718 if (CBS_len(&srtp_mki) != 0) {
1719 /* Must be no MKI, since we never offer one. */
1720 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1721 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1722 return 0;
1723 }
1724
1725 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1726
1727 /* Check to see if the server gave us something we support (and presumably
1728 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001729 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001730 const SRTP_PROTECTION_PROFILE *profile =
1731 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1732
1733 if (profile->id == profile_id) {
1734 ssl->srtp_profile = profile;
1735 return 1;
1736 }
1737 }
1738
1739 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1740 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1741 return 0;
1742}
1743
1744static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1745 CBS *contents) {
1746 if (contents == NULL) {
1747 return 1;
1748 }
1749
1750 CBS profile_ids, srtp_mki;
1751 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1752 CBS_len(&profile_ids) < 2 ||
1753 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1754 CBS_len(contents) != 0) {
1755 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1756 return 0;
1757 }
1758 /* Discard the MKI value for now. */
1759
1760 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1761 SSL_get_srtp_profiles(ssl);
1762
1763 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001764 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001765 const SRTP_PROTECTION_PROFILE *server_profile =
1766 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1767
1768 CBS profile_ids_tmp;
1769 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1770
1771 while (CBS_len(&profile_ids_tmp) > 0) {
1772 uint16_t profile_id;
1773 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1774 return 0;
1775 }
1776
1777 if (server_profile->id == profile_id) {
1778 ssl->srtp_profile = server_profile;
1779 return 1;
1780 }
1781 }
1782 }
1783
1784 return 1;
1785}
1786
1787static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1788 if (ssl->srtp_profile == NULL) {
1789 return 1;
1790 }
1791
1792 CBB contents, profile_ids;
1793 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1794 !CBB_add_u16_length_prefixed(out, &contents) ||
1795 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1796 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1797 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1798 !CBB_flush(out)) {
1799 return 0;
1800 }
1801
1802 return 1;
1803}
1804
Adam Langleybdd5d662015-07-20 16:19:08 -07001805
1806/* EC point formats.
1807 *
1808 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1809
Adam Langleybdd5d662015-07-20 16:19:08 -07001810static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001811 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001812 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1813 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001814 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1815 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001816 !CBB_flush(out)) {
1817 return 0;
1818 }
1819
1820 return 1;
1821}
1822
1823static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001824 uint16_t min_version, max_version;
1825 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1826 return 0;
1827 }
1828
1829 /* The point format extension is unneccessary in TLS 1.3. */
1830 if (min_version >= TLS1_3_VERSION) {
1831 return 1;
1832 }
1833
Adam Langleybdd5d662015-07-20 16:19:08 -07001834 return ext_ec_point_add_extension(ssl, out);
1835}
1836
1837static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1838 CBS *contents) {
1839 if (contents == NULL) {
1840 return 1;
1841 }
1842
Steven Valdez143e8b32016-07-11 13:19:03 -04001843 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1844 return 0;
1845 }
1846
Adam Langleybdd5d662015-07-20 16:19:08 -07001847 CBS ec_point_format_list;
1848 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1849 CBS_len(contents) != 0) {
1850 return 0;
1851 }
1852
David Benjaminfc059942015-07-30 23:01:59 -04001853 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1854 * point format. */
1855 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1856 CBS_len(&ec_point_format_list)) == NULL) {
1857 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001858 return 0;
1859 }
1860
1861 return 1;
1862}
1863
1864static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1865 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001866 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1867 return 1;
1868 }
1869
Adam Langleybdd5d662015-07-20 16:19:08 -07001870 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1871}
1872
1873static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001874 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1875 return 1;
1876 }
1877
Adam Langleybdd5d662015-07-20 16:19:08 -07001878 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1879 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001880 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001881
1882 if (!using_ecc) {
1883 return 1;
1884 }
1885
1886 return ext_ec_point_add_extension(ssl, out);
1887}
1888
Steven Valdeza833c352016-11-01 13:39:36 -04001889
Steven Valdez4aa154e2016-07-29 14:32:55 -04001890/* Pre Shared Key
1891 *
Steven Valdeza833c352016-11-01 13:39:36 -04001892 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001893
Steven Valdeza833c352016-11-01 13:39:36 -04001894static size_t ext_pre_shared_key_clienthello_length(SSL *ssl) {
1895 uint16_t min_version, max_version;
1896 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1897 return 0;
1898 }
1899
1900 uint16_t session_version;
1901 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1902 !ssl->method->version_from_wire(&session_version,
1903 ssl->session->ssl_version) ||
1904 session_version < TLS1_3_VERSION) {
1905 return 0;
1906 }
1907
1908 const EVP_MD *digest =
1909 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1910 size_t binder_len = EVP_MD_size(digest);
1911 return 15 + ssl->session->tlsext_ticklen + binder_len;
1912}
1913
1914int ssl_ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001915 uint16_t min_version, max_version;
1916 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1917 return 0;
1918 }
1919
David Benjamin2dc02042016-09-19 19:57:37 -04001920 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001921 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001922 !ssl->method->version_from_wire(&session_version,
1923 ssl->session->ssl_version) ||
1924 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001925 return 1;
1926 }
1927
Steven Valdeza833c352016-11-01 13:39:36 -04001928 struct timeval now;
1929 ssl_get_current_time(ssl, &now);
1930 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1931 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1932
1933 /* Fill in a placeholder zero binder of the appropriate length. It will be
1934 * computed and filled in later after length prefixes are computed. */
1935 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
1936 const EVP_MD *digest =
1937 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1938 size_t binder_len = EVP_MD_size(digest);
1939
1940 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001941 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1942 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001943 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001944 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1945 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001946 ssl->session->tlsext_ticklen) ||
1947 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1948 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1949 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1950 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001951 return 0;
1952 }
1953
Steven Valdeza833c352016-11-01 13:39:36 -04001954 ssl->s3->hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001955 return CBB_flush(out);
1956}
1957
1958int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1959 CBS *contents) {
1960 uint16_t psk_id;
1961 if (!CBS_get_u16(contents, &psk_id) ||
1962 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001963 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001964 *out_alert = SSL_AD_DECODE_ERROR;
1965 return 0;
1966 }
1967
David Benjamineab773a2016-11-09 18:32:35 -05001968 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001969 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,
Steven Valdeza833c352016-11-01 13:39:36 -04001980 CBS *out_binders,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001981 uint8_t *out_alert,
1982 CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04001983 /* We only process the first PSK identity since we don't support pure PSK. */
Steven Valdeza833c352016-11-01 13:39:36 -04001984 uint32_t obfuscated_ticket_age;
1985 CBS identity, ticket, binders;
Steven Valdez5b986082016-09-01 12:29:49 -04001986 if (!CBS_get_u16_length_prefixed(contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001987 !CBS_get_u16_length_prefixed(&identity, &ticket) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001988 !CBS_get_u32(&identity, &obfuscated_ticket_age) ||
1989 !CBS_get_u16_length_prefixed(contents, &binders) ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001990 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001991 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001992 *out_alert = SSL_AD_DECODE_ERROR;
1993 return 0;
1994 }
1995
Steven Valdeza833c352016-11-01 13:39:36 -04001996 *out_binders = binders;
1997
1998 /* The PSK identity must have a corresponding binder. */
1999 CBS binder;
2000 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
2001 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2002 *out_alert = SSL_AD_DECODE_ERROR;
2003 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04002004 }
2005
Steven Valdeza833c352016-11-01 13:39:36 -04002006 /* TODO(svaldez): Check that the ticket_age is valid when attempting to use
2007 * the PSK for 0-RTT. http://crbug.com/boringssl/113 */
2008
Steven Valdez4aa154e2016-07-29 14:32:55 -04002009 /* TLS 1.3 session tickets are renewed separately as part of the
2010 * NewSessionTicket. */
2011 int renew;
Steven Valdez5b986082016-09-01 12:29:49 -04002012 return tls_process_ticket(ssl, out_session, &renew, CBS_data(&ticket),
2013 CBS_len(&ticket), NULL, 0);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002014}
2015
2016int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
2017 if (!ssl->s3->session_reused) {
2018 return 1;
2019 }
2020
2021 CBB contents;
2022 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2023 !CBB_add_u16_length_prefixed(out, &contents) ||
2024 /* We only consider the first identity for resumption */
2025 !CBB_add_u16(&contents, 0) ||
2026 !CBB_flush(out)) {
2027 return 0;
2028 }
2029
2030 return 1;
2031}
2032
2033
Steven Valdeza833c352016-11-01 13:39:36 -04002034/* Pre-Shared Key Exchange Modes
2035 *
2036 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
2037static int ext_psk_key_exchange_modes_add_clienthello(SSL *ssl, CBB *out) {
2038 uint16_t min_version, max_version;
2039 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2040 return 0;
2041 }
2042
2043 if (max_version < TLS1_3_VERSION) {
2044 return 1;
2045 }
2046
2047 CBB contents, ke_modes;
2048 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2049 !CBB_add_u16_length_prefixed(out, &contents) ||
2050 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2051 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2052 return 0;
2053 }
2054
2055 return CBB_flush(out);
2056}
2057
2058int ssl_ext_psk_key_exchange_modes_parse_clienthello(SSL *ssl,
2059 uint8_t *out_alert,
2060 CBS *contents) {
2061 CBS ke_modes;
2062 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2063 CBS_len(&ke_modes) == 0 ||
2064 CBS_len(contents) != 0) {
2065 *out_alert = SSL_AD_DECODE_ERROR;
2066 return 0;
2067 }
2068
2069 /* We only support tickets with PSK_DHE_KE. */
2070 ssl->s3->hs->accept_psk_mode =
2071 memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, CBS_len(&ke_modes)) != NULL;
2072
2073 return 1;
2074}
2075
2076
Steven Valdez143e8b32016-07-11 13:19:03 -04002077/* Key Share
2078 *
David Benjamina128a552016-10-13 14:26:33 -04002079 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002080
2081static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
2082 uint16_t min_version, max_version;
2083 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2084 return 0;
2085 }
2086
David Benjamin53a2dfc2016-10-07 21:04:12 -04002087 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002088 return 1;
2089 }
2090
2091 CBB contents, kse_bytes;
2092 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2093 !CBB_add_u16_length_prefixed(out, &contents) ||
2094 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2095 return 0;
2096 }
2097
Steven Valdeza833c352016-11-01 13:39:36 -04002098 uint16_t group_id = ssl->s3->hs->retry_group;
David Benjamin3baa6e12016-10-07 21:10:38 -04002099 if (ssl->s3->hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002100 /* We received a HelloRetryRequest without a new curve, so there is no new
2101 * share to append. Leave |ecdh_ctx| as-is. */
2102 if (group_id == 0 &&
2103 !CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
Steven Valdez5440fe02016-07-18 12:40:30 -04002104 ssl->s3->hs->key_share_bytes_len)) {
2105 return 0;
2106 }
2107 OPENSSL_free(ssl->s3->hs->key_share_bytes);
2108 ssl->s3->hs->key_share_bytes = NULL;
David Benjamin4fe3c902016-08-16 02:17:03 -04002109 ssl->s3->hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002110 if (group_id == 0) {
2111 return CBB_flush(out);
2112 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002113 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002114 /* Add a fake group. See draft-davidben-tls-grease-01. */
2115 if (ssl->ctx->grease_enabled &&
2116 (!CBB_add_u16(&kse_bytes,
2117 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2118 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2119 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2120 return 0;
2121 }
2122
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002123 /* Predict the most preferred group. */
2124 const uint16_t *groups;
2125 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002126 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002127 if (groups_len == 0) {
2128 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2129 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002130 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002131
2132 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002133 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002134
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002135 CBB key_exchange;
2136 if (!CBB_add_u16(&kse_bytes, group_id) ||
2137 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2138 !SSL_ECDH_CTX_init(&ssl->s3->hs->ecdh_ctx, group_id) ||
2139 !SSL_ECDH_CTX_offer(&ssl->s3->hs->ecdh_ctx, &key_exchange) ||
2140 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002141 return 0;
2142 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002143
David Benjamin3baa6e12016-10-07 21:10:38 -04002144 if (!ssl->s3->hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002145 /* Save the contents of the extension to repeat it in the second
2146 * ClientHello. */
2147 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2148 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2149 CBB_len(&kse_bytes));
2150 if (ssl->s3->hs->key_share_bytes == NULL) {
2151 return 0;
2152 }
2153 }
2154
Steven Valdez143e8b32016-07-11 13:19:03 -04002155 return CBB_flush(out);
2156}
2157
Steven Valdez7259f2f2016-08-02 16:55:05 -04002158int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2159 size_t *out_secret_len,
2160 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002161 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002162 uint16_t group_id;
2163 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002164 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2165 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002166 *out_alert = SSL_AD_DECODE_ERROR;
2167 return 0;
2168 }
2169
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002170 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002171 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2172 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2173 return 0;
2174 }
2175
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002176 if (!SSL_ECDH_CTX_finish(&ssl->s3->hs->ecdh_ctx, out_secret, out_secret_len,
2177 out_alert, CBS_data(&peer_key),
2178 CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002179 *out_alert = SSL_AD_INTERNAL_ERROR;
2180 return 0;
2181 }
2182
David Benjamin5c4e8572016-08-19 17:44:53 -04002183 ssl->s3->new_session->key_exchange_info = group_id;
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002184 SSL_ECDH_CTX_cleanup(&ssl->s3->hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002185 return 1;
2186}
2187
Steven Valdez7259f2f2016-08-02 16:55:05 -04002188int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2189 uint8_t **out_secret,
2190 size_t *out_secret_len,
2191 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002192 uint16_t group_id;
2193 CBS key_shares;
Steven Valdez803c77a2016-09-06 14:13:43 -04002194 if (!tls1_get_shared_group(ssl, &group_id)) {
2195 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2196 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2197 return 0;
2198 }
2199
2200 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002201 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002202 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002203 return 0;
2204 }
2205
David Benjamin7e1f9842016-09-20 19:24:40 -04002206 /* Find the corresponding key share. */
2207 int found = 0;
2208 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002209 while (CBS_len(&key_shares) > 0) {
2210 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002211 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002212 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002213 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2214 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002215 return 0;
2216 }
2217
David Benjamin7e1f9842016-09-20 19:24:40 -04002218 if (id == group_id) {
2219 if (found) {
2220 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2221 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2222 return 0;
2223 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002224
David Benjamin7e1f9842016-09-20 19:24:40 -04002225 found = 1;
2226 peer_key = peer_key_tmp;
2227 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002228 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002229 }
2230
David Benjamin7e1f9842016-09-20 19:24:40 -04002231 if (!found) {
2232 *out_found = 0;
2233 *out_secret = NULL;
2234 *out_secret_len = 0;
2235 return 1;
2236 }
2237
2238 /* Compute the DH secret. */
2239 uint8_t *secret = NULL;
2240 size_t secret_len;
2241 SSL_ECDH_CTX group;
2242 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2243 CBB public_key;
2244 if (!CBB_init(&public_key, 32) ||
2245 !SSL_ECDH_CTX_init(&group, group_id) ||
2246 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len,
2247 out_alert, CBS_data(&peer_key),
2248 CBS_len(&peer_key)) ||
2249 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2250 &ssl->s3->hs->public_key_len)) {
2251 OPENSSL_free(secret);
2252 SSL_ECDH_CTX_cleanup(&group);
2253 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002254 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002255 return 0;
2256 }
2257
2258 SSL_ECDH_CTX_cleanup(&group);
2259
2260 *out_secret = secret;
2261 *out_secret_len = secret_len;
2262 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002263 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002264}
2265
Steven Valdez7259f2f2016-08-02 16:55:05 -04002266int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002267 uint16_t group_id;
2268 CBB kse_bytes, public_key;
2269 if (!tls1_get_shared_group(ssl, &group_id) ||
2270 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2271 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2272 !CBB_add_u16(&kse_bytes, group_id) ||
2273 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2274 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2275 ssl->s3->hs->public_key_len) ||
2276 !CBB_flush(out)) {
2277 return 0;
2278 }
2279
David Benjamin4fe3c902016-08-16 02:17:03 -04002280 OPENSSL_free(ssl->s3->hs->public_key);
2281 ssl->s3->hs->public_key = NULL;
2282 ssl->s3->hs->public_key_len = 0;
2283
David Benjamin5c4e8572016-08-19 17:44:53 -04002284 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002285 return 1;
2286}
2287
2288
Steven Valdezfdd10992016-09-15 16:27:05 -04002289/* Supported Versions
2290 *
2291 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2292
2293static int ext_supported_versions_add_clienthello(SSL *ssl, CBB *out) {
2294 uint16_t min_version, max_version;
2295 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2296 return 0;
2297 }
2298
2299 if (max_version <= TLS1_2_VERSION) {
2300 return 1;
2301 }
2302
2303 CBB contents, versions;
2304 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2305 !CBB_add_u16_length_prefixed(out, &contents) ||
2306 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2307 return 0;
2308 }
2309
David Benjamind9791bf2016-09-27 16:39:52 -04002310 /* Add a fake version. See draft-davidben-tls-grease-01. */
2311 if (ssl->ctx->grease_enabled &&
2312 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2313 return 0;
2314 }
2315
Steven Valdezfdd10992016-09-15 16:27:05 -04002316 for (uint16_t version = max_version; version >= min_version; version--) {
2317 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2318 return 0;
2319 }
2320 }
2321
2322 if (!CBB_flush(out)) {
2323 return 0;
2324 }
2325
2326 return 1;
2327}
2328
2329
David Benjamin3baa6e12016-10-07 21:10:38 -04002330/* Cookie
2331 *
2332 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2333
2334static int ext_cookie_add_clienthello(SSL *ssl, CBB *out) {
2335 if (ssl->s3->hs->cookie == NULL) {
2336 return 1;
2337 }
2338
2339 CBB contents, cookie;
2340 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2341 !CBB_add_u16_length_prefixed(out, &contents) ||
2342 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
2343 !CBB_add_bytes(&cookie, ssl->s3->hs->cookie, ssl->s3->hs->cookie_len) ||
2344 !CBB_flush(out)) {
2345 return 0;
2346 }
2347
2348 /* The cookie is no longer needed in memory. */
2349 OPENSSL_free(ssl->s3->hs->cookie);
2350 ssl->s3->hs->cookie = NULL;
2351 ssl->s3->hs->cookie_len = 0;
2352 return 1;
2353}
2354
2355
Steven Valdezce902a92016-05-17 11:47:53 -04002356/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002357 *
Steven Valdezce902a92016-05-17 11:47:53 -04002358 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002359 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002360
Steven Valdezce902a92016-05-17 11:47:53 -04002361static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Steven Valdezce902a92016-05-17 11:47:53 -04002362 CBB contents, groups_bytes;
2363 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002364 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002365 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002366 return 0;
2367 }
2368
David Benjamin65ac9972016-09-02 21:35:25 -04002369 /* Add a fake group. See draft-davidben-tls-grease-01. */
2370 if (ssl->ctx->grease_enabled &&
2371 !CBB_add_u16(&groups_bytes,
2372 ssl_get_grease_value(ssl, ssl_grease_group))) {
2373 return 0;
2374 }
2375
Steven Valdezce902a92016-05-17 11:47:53 -04002376 const uint16_t *groups;
2377 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002378 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002379
David Benjamin54091232016-09-05 12:47:25 -04002380 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002381 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002382 return 0;
2383 }
2384 }
2385
2386 return CBB_flush(out);
2387}
2388
Steven Valdezce902a92016-05-17 11:47:53 -04002389static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2390 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002391 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2392 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002393 return 1;
2394}
2395
Steven Valdezce902a92016-05-17 11:47:53 -04002396static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2397 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002398 if (contents == NULL) {
2399 return 1;
2400 }
2401
Steven Valdezce902a92016-05-17 11:47:53 -04002402 CBS supported_group_list;
2403 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2404 CBS_len(&supported_group_list) == 0 ||
2405 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002406 CBS_len(contents) != 0) {
2407 return 0;
2408 }
2409
David Benjamin43612b62016-10-07 00:41:50 -04002410 ssl->s3->hs->peer_supported_group_list = OPENSSL_malloc(
Steven Valdezce902a92016-05-17 11:47:53 -04002411 CBS_len(&supported_group_list));
David Benjamin43612b62016-10-07 00:41:50 -04002412 if (ssl->s3->hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002413 *out_alert = SSL_AD_INTERNAL_ERROR;
2414 return 0;
2415 }
2416
Steven Valdezce902a92016-05-17 11:47:53 -04002417 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002418 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002419 if (!CBS_get_u16(&supported_group_list,
David Benjamin43612b62016-10-07 00:41:50 -04002420 &ssl->s3->hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002421 goto err;
2422 }
2423 }
2424
Steven Valdezce902a92016-05-17 11:47:53 -04002425 assert(CBS_len(&supported_group_list) == 0);
David Benjamin43612b62016-10-07 00:41:50 -04002426 ssl->s3->hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002427
2428 return 1;
2429
2430err:
David Benjamin43612b62016-10-07 00:41:50 -04002431 OPENSSL_free(ssl->s3->hs->peer_supported_group_list);
2432 ssl->s3->hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002433 *out_alert = SSL_AD_INTERNAL_ERROR;
2434 return 0;
2435}
2436
Steven Valdezce902a92016-05-17 11:47:53 -04002437static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002438 /* Servers don't echo this extension. */
2439 return 1;
2440}
2441
2442
Adam Langley614c66a2015-06-12 15:26:58 -07002443/* kExtensions contains all the supported extensions. */
2444static const struct tls_extension kExtensions[] = {
2445 {
Adam Langley5021b222015-06-12 18:27:58 -07002446 TLSEXT_TYPE_renegotiate,
2447 NULL,
2448 ext_ri_add_clienthello,
2449 ext_ri_parse_serverhello,
2450 ext_ri_parse_clienthello,
2451 ext_ri_add_serverhello,
2452 },
2453 {
Adam Langley614c66a2015-06-12 15:26:58 -07002454 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002455 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002456 ext_sni_add_clienthello,
2457 ext_sni_parse_serverhello,
2458 ext_sni_parse_clienthello,
2459 ext_sni_add_serverhello,
2460 },
Adam Langley0a056712015-07-01 15:03:33 -07002461 {
2462 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002463 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002464 ext_ems_add_clienthello,
2465 ext_ems_parse_serverhello,
2466 ext_ems_parse_clienthello,
2467 ext_ems_add_serverhello,
2468 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002469 {
2470 TLSEXT_TYPE_session_ticket,
2471 NULL,
2472 ext_ticket_add_clienthello,
2473 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002474 /* Ticket extension client parsing is handled in ssl_session.c */
2475 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002476 ext_ticket_add_serverhello,
2477 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002478 {
2479 TLSEXT_TYPE_signature_algorithms,
2480 NULL,
2481 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002482 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002483 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002484 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002485 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002486 {
2487 TLSEXT_TYPE_status_request,
2488 ext_ocsp_init,
2489 ext_ocsp_add_clienthello,
2490 ext_ocsp_parse_serverhello,
2491 ext_ocsp_parse_clienthello,
2492 ext_ocsp_add_serverhello,
2493 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002494 {
2495 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002496 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002497 ext_npn_add_clienthello,
2498 ext_npn_parse_serverhello,
2499 ext_npn_parse_clienthello,
2500 ext_npn_add_serverhello,
2501 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002502 {
2503 TLSEXT_TYPE_certificate_timestamp,
2504 NULL,
2505 ext_sct_add_clienthello,
2506 ext_sct_parse_serverhello,
2507 ext_sct_parse_clienthello,
2508 ext_sct_add_serverhello,
2509 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002510 {
2511 TLSEXT_TYPE_application_layer_protocol_negotiation,
2512 ext_alpn_init,
2513 ext_alpn_add_clienthello,
2514 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002515 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2516 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002517 ext_alpn_add_serverhello,
2518 },
Adam Langley49c7af12015-07-10 14:33:46 -07002519 {
2520 TLSEXT_TYPE_channel_id,
2521 ext_channel_id_init,
2522 ext_channel_id_add_clienthello,
2523 ext_channel_id_parse_serverhello,
2524 ext_channel_id_parse_clienthello,
2525 ext_channel_id_add_serverhello,
2526 },
Adam Langley391250d2015-07-15 19:06:07 -07002527 {
2528 TLSEXT_TYPE_srtp,
2529 ext_srtp_init,
2530 ext_srtp_add_clienthello,
2531 ext_srtp_parse_serverhello,
2532 ext_srtp_parse_clienthello,
2533 ext_srtp_add_serverhello,
2534 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002535 {
2536 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002537 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002538 ext_ec_point_add_clienthello,
2539 ext_ec_point_parse_serverhello,
2540 ext_ec_point_parse_clienthello,
2541 ext_ec_point_add_serverhello,
2542 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002543 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002544 TLSEXT_TYPE_key_share,
2545 NULL,
2546 ext_key_share_add_clienthello,
2547 forbid_parse_serverhello,
2548 ignore_parse_clienthello,
2549 dont_add_serverhello,
2550 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002551 {
Steven Valdeza833c352016-11-01 13:39:36 -04002552 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002553 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002554 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002555 forbid_parse_serverhello,
2556 ignore_parse_clienthello,
2557 dont_add_serverhello,
2558 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002559 {
2560 TLSEXT_TYPE_supported_versions,
2561 NULL,
2562 ext_supported_versions_add_clienthello,
2563 forbid_parse_serverhello,
2564 ignore_parse_clienthello,
2565 dont_add_serverhello,
2566 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002567 {
2568 TLSEXT_TYPE_cookie,
2569 NULL,
2570 ext_cookie_add_clienthello,
2571 forbid_parse_serverhello,
2572 ignore_parse_clienthello,
2573 dont_add_serverhello,
2574 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002575 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2576 * intolerant to the last extension being zero-length. See
2577 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002578 {
Steven Valdezce902a92016-05-17 11:47:53 -04002579 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002580 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002581 ext_supported_groups_add_clienthello,
2582 ext_supported_groups_parse_serverhello,
2583 ext_supported_groups_parse_clienthello,
2584 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002585 },
Adam Langley614c66a2015-06-12 15:26:58 -07002586};
2587
2588#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2589
Adam Langley4cfa96b2015-07-01 11:56:55 -07002590OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002591 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002592 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002593OPENSSL_COMPILE_ASSERT(
2594 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2595 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002596
Adam Langley614c66a2015-06-12 15:26:58 -07002597static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2598 uint16_t value) {
2599 unsigned i;
2600 for (i = 0; i < kNumExtensions; i++) {
2601 if (kExtensions[i].value == value) {
2602 *out_index = i;
2603 return &kExtensions[i];
2604 }
2605 }
2606
2607 return NULL;
2608}
2609
Adam Langley09505632015-07-30 18:10:13 -07002610int SSL_extension_supported(unsigned extension_value) {
2611 uint32_t index;
2612 return extension_value == TLSEXT_TYPE_padding ||
2613 tls_extension_find(&index, extension_value) != NULL;
2614}
2615
David Benjamine8d53502015-10-10 14:13:23 -04002616int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002617 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002618 if (ssl->client_version == SSL3_VERSION &&
2619 !ssl->s3->send_connection_binding) {
2620 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002621 }
Adam Langley95c29f32014-06-20 12:00:00 -07002622
David Benjamine8d53502015-10-10 14:13:23 -04002623 CBB extensions;
2624 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002625 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002626 }
Adam Langley95c29f32014-06-20 12:00:00 -07002627
David Benjaminf5d2cd02016-10-06 19:39:20 -04002628 ssl->s3->hs->extensions.sent = 0;
2629 ssl->s3->hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002630
David Benjamin54091232016-09-05 12:47:25 -04002631 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002632 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002633 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002634 }
2635 }
Adam Langley95c29f32014-06-20 12:00:00 -07002636
David Benjamin65ac9972016-09-02 21:35:25 -04002637 uint16_t grease_ext1 = 0;
2638 if (ssl->ctx->grease_enabled) {
2639 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2640 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2641 if (!CBB_add_u16(&extensions, grease_ext1) ||
2642 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2643 goto err;
2644 }
2645 }
2646
David Benjamin54091232016-09-05 12:47:25 -04002647 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002648 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002649 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002650 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2651 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2652 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002653 }
Adam Langley95c29f32014-06-20 12:00:00 -07002654
Adam Langley33ad2b52015-07-20 17:43:53 -07002655 if (CBB_len(&extensions) != len_before) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002656 ssl->s3->hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002657 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002658 }
Adam Langley75712922014-10-10 16:23:43 -07002659
David Benjamine8d53502015-10-10 14:13:23 -04002660 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002661 goto err;
2662 }
2663
David Benjamin65ac9972016-09-02 21:35:25 -04002664 if (ssl->ctx->grease_enabled) {
2665 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2666 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2667
2668 /* The two fake extensions must not have the same value. GREASE values are
2669 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2670 * one. */
2671 if (grease_ext1 == grease_ext2) {
2672 grease_ext2 ^= 0x1010;
2673 }
2674
2675 if (!CBB_add_u16(&extensions, grease_ext2) ||
2676 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2677 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2678 goto err;
2679 }
2680 }
2681
David Benjamince079fd2016-08-02 16:22:34 -04002682 if (!SSL_is_dtls(ssl)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002683 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(ssl);
2684 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002685 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002686 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002687 *
2688 * NB: because this code works out the length of all existing extensions
2689 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002690 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002691 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002692 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002693 * Server 7.0 is intolerant to the last extension being zero-length. See
2694 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002695 if (padding_len >= 4 + 1) {
2696 padding_len -= 4;
2697 } else {
2698 padding_len = 1;
2699 }
Adam Langley95c29f32014-06-20 12:00:00 -07002700
Adam Langley33ad2b52015-07-20 17:43:53 -07002701 uint8_t *padding_bytes;
2702 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2703 !CBB_add_u16(&extensions, padding_len) ||
2704 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2705 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002706 }
Adam Langley75712922014-10-10 16:23:43 -07002707
Adam Langley33ad2b52015-07-20 17:43:53 -07002708 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002709 }
2710 }
Adam Langley75712922014-10-10 16:23:43 -07002711
Steven Valdeza833c352016-11-01 13:39:36 -04002712 /* The PSK extension must be last, including after the padding. */
2713 if (!ssl_ext_pre_shared_key_add_clienthello(ssl, &extensions)) {
2714 goto err;
2715 }
2716
David Benjamina01deee2015-12-08 18:56:31 -05002717 /* Discard empty extensions blocks. */
2718 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002719 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002720 }
2721
David Benjamine8d53502015-10-10 14:13:23 -04002722 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002723
2724err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002725 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002726 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002727}
Adam Langley95c29f32014-06-20 12:00:00 -07002728
David Benjamin56380462015-10-10 14:59:09 -04002729int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002730 CBB extensions;
2731 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002732 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002733 }
2734
2735 unsigned i;
2736 for (i = 0; i < kNumExtensions; i++) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002737 if (!(ssl->s3->hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002738 /* Don't send extensions that were not received. */
2739 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002740 }
Adam Langley95c29f32014-06-20 12:00:00 -07002741
David Benjamin56380462015-10-10 14:59:09 -04002742 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002743 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2744 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2745 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002746 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002747 }
Adam Langley95c29f32014-06-20 12:00:00 -07002748
David Benjamin56380462015-10-10 14:59:09 -04002749 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002750 goto err;
2751 }
2752
Steven Valdez143e8b32016-07-11 13:19:03 -04002753 /* Discard empty extensions blocks before TLS 1.3. */
2754 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2755 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002756 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002757 }
2758
David Benjamin56380462015-10-10 14:59:09 -04002759 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002760
2761err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002762 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002763 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002764}
Adam Langley95c29f32014-06-20 12:00:00 -07002765
David Benjamine14ff062016-08-09 16:21:24 -04002766static int ssl_scan_clienthello_tlsext(
2767 SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
2768 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002769 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002770 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002771 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002772 }
2773 }
2774
David Benjaminf5d2cd02016-10-06 19:39:20 -04002775 ssl->s3->hs->extensions.received = 0;
2776 ssl->s3->hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002777
David Benjamine14ff062016-08-09 16:21:24 -04002778 CBS extensions;
2779 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2780 while (CBS_len(&extensions) != 0) {
2781 uint16_t type;
2782 CBS extension;
2783
2784 /* Decode the next extension. */
2785 if (!CBS_get_u16(&extensions, &type) ||
2786 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002787 *out_alert = SSL_AD_DECODE_ERROR;
2788 return 0;
2789 }
Adam Langley95c29f32014-06-20 12:00:00 -07002790
David Benjamine14ff062016-08-09 16:21:24 -04002791 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2792 * ambiguous. Ignore all but the renegotiation_info extension. */
2793 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2794 continue;
2795 }
Adam Langley95c29f32014-06-20 12:00:00 -07002796
David Benjamine14ff062016-08-09 16:21:24 -04002797 unsigned ext_index;
2798 const struct tls_extension *const ext =
2799 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002800
David Benjamine14ff062016-08-09 16:21:24 -04002801 if (ext == NULL) {
2802 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002803 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002804 return 0;
2805 }
David Benjamine14ff062016-08-09 16:21:24 -04002806 continue;
2807 }
2808
David Benjaminf5d2cd02016-10-06 19:39:20 -04002809 ssl->s3->hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002810 uint8_t alert = SSL_AD_DECODE_ERROR;
2811 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2812 *out_alert = alert;
2813 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2814 ERR_add_error_dataf("extension: %u", (unsigned)type);
2815 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002816 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002817 }
Adam Langley75712922014-10-10 16:23:43 -07002818
David Benjamin1deb41b2016-08-09 19:36:38 -04002819 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002820 if (ssl->s3->hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002821 continue;
2822 }
2823
2824 CBS *contents = NULL, fake_contents;
2825 static const uint8_t kFakeRenegotiateExtension[] = {0};
2826 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2827 ssl_client_cipher_list_contains_cipher(client_hello,
2828 SSL3_CK_SCSV & 0xffff)) {
2829 /* The renegotiation SCSV was received so pretend that we received a
2830 * renegotiation extension. */
2831 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2832 sizeof(kFakeRenegotiateExtension));
2833 contents = &fake_contents;
David Benjaminf5d2cd02016-10-06 19:39:20 -04002834 ssl->s3->hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002835 }
2836
2837 /* Extension wasn't observed so call the callback with a NULL
2838 * parameter. */
2839 uint8_t alert = SSL_AD_DECODE_ERROR;
2840 if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
2841 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2842 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2843 *out_alert = alert;
2844 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002845 }
2846 }
2847
Adam Langleyfcf25832014-12-18 17:42:32 -08002848 return 1;
2849}
Adam Langley95c29f32014-06-20 12:00:00 -07002850
David Benjamine14ff062016-08-09 16:21:24 -04002851int ssl_parse_clienthello_tlsext(
2852 SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002853 int alert = -1;
David Benjamine14ff062016-08-09 16:21:24 -04002854 if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002855 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002856 return 0;
2857 }
Adam Langley95c29f32014-06-20 12:00:00 -07002858
David Benjamin0d56f882015-12-19 17:05:56 -05002859 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002860 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002861 return 0;
2862 }
Adam Langley95c29f32014-06-20 12:00:00 -07002863
Adam Langleyfcf25832014-12-18 17:42:32 -08002864 return 1;
2865}
Adam Langley95c29f32014-06-20 12:00:00 -07002866
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002867OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2868
David Benjamin0d56f882015-12-19 17:05:56 -05002869static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002870 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2871 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2872 return 1;
2873 }
Adam Langley614c66a2015-06-12 15:26:58 -07002874
Steven Valdez143e8b32016-07-11 13:19:03 -04002875 /* Decode the extensions block and check it is valid. */
2876 CBS extensions;
2877 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2878 !tls1_check_duplicate_extensions(&extensions)) {
2879 *out_alert = SSL_AD_DECODE_ERROR;
2880 return 0;
2881 }
2882
2883 uint32_t received = 0;
2884 while (CBS_len(&extensions) != 0) {
2885 uint16_t type;
2886 CBS extension;
2887
2888 /* Decode the next extension. */
2889 if (!CBS_get_u16(&extensions, &type) ||
2890 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002891 *out_alert = SSL_AD_DECODE_ERROR;
2892 return 0;
2893 }
Adam Langley95c29f32014-06-20 12:00:00 -07002894
Steven Valdez143e8b32016-07-11 13:19:03 -04002895 unsigned ext_index;
2896 const struct tls_extension *const ext =
2897 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002898
Steven Valdez143e8b32016-07-11 13:19:03 -04002899 if (ext == NULL) {
2900 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002901 return 0;
2902 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002903 continue;
2904 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002905
David Benjaminf5d2cd02016-10-06 19:39:20 -04002906 if (!(ssl->s3->hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002907 type != TLSEXT_TYPE_renegotiate) {
2908 /* If the extension was never sent then it is illegal, except for the
2909 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002910 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2911 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002912 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002913 return 0;
2914 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002915
Steven Valdez143e8b32016-07-11 13:19:03 -04002916 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002917
Steven Valdez143e8b32016-07-11 13:19:03 -04002918 uint8_t alert = SSL_AD_DECODE_ERROR;
2919 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2920 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2921 ERR_add_error_dataf("extension: %u", (unsigned)type);
2922 *out_alert = alert;
2923 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002924 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002925 }
Adam Langley95c29f32014-06-20 12:00:00 -07002926
David Benjamin54091232016-09-05 12:47:25 -04002927 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002928 if (!(received & (1u << i))) {
2929 /* Extension wasn't observed so call the callback with a NULL
2930 * parameter. */
2931 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002932 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002933 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2934 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002935 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002936 return 0;
2937 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002938 }
2939 }
Adam Langley95c29f32014-06-20 12:00:00 -07002940
Adam Langleyfcf25832014-12-18 17:42:32 -08002941 return 1;
2942}
Adam Langley95c29f32014-06-20 12:00:00 -07002943
David Benjamin0d56f882015-12-19 17:05:56 -05002944static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002945 int ret = SSL_TLSEXT_ERR_NOACK;
2946 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002947
David Benjamin78f8aab2016-03-10 16:33:58 -05002948 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002949 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002950 ssl->ctx->tlsext_servername_arg);
2951 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002952 ret = ssl->initial_ctx->tlsext_servername_callback(
2953 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002954 }
Adam Langley95c29f32014-06-20 12:00:00 -07002955
Adam Langleyfcf25832014-12-18 17:42:32 -08002956 switch (ret) {
2957 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002958 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002959 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002960
Adam Langleyfcf25832014-12-18 17:42:32 -08002961 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002962 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002963 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002964
Adam Langleyfcf25832014-12-18 17:42:32 -08002965 case SSL_TLSEXT_ERR_NOACK:
David Benjamina0486782016-10-06 19:11:32 -04002966 ssl->s3->hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002967 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002968
Adam Langleyfcf25832014-12-18 17:42:32 -08002969 default:
2970 return 1;
2971 }
2972}
Adam Langleyed8270a2014-09-02 13:52:56 -07002973
David Benjamin0d56f882015-12-19 17:05:56 -05002974static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002975 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002976 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002977
David Benjamin78f8aab2016-03-10 16:33:58 -05002978 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002979 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002980 ssl->ctx->tlsext_servername_arg);
2981 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002982 ret = ssl->initial_ctx->tlsext_servername_callback(
2983 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002984 }
Adam Langley95c29f32014-06-20 12:00:00 -07002985
Adam Langleyfcf25832014-12-18 17:42:32 -08002986 switch (ret) {
2987 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002988 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002989 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002990
Adam Langleyfcf25832014-12-18 17:42:32 -08002991 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002992 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002993 return 1;
2994
2995 default:
2996 return 1;
2997 }
2998}
2999
David Benjamin0d56f882015-12-19 17:05:56 -05003000int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003001 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05003002 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
3003 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003004 return 0;
3005 }
3006
David Benjamin0d56f882015-12-19 17:05:56 -05003007 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04003008 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08003009 return 0;
3010 }
3011
3012 return 1;
3013}
Adam Langley95c29f32014-06-20 12:00:00 -07003014
David Benjamine3aa1d92015-06-16 15:34:50 -04003015int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05003016 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04003017 size_t ticket_len, const uint8_t *session_id,
3018 size_t session_id_len) {
3019 int ret = 1; /* Most errors are non-fatal. */
3020 SSL_CTX *ssl_ctx = ssl->initial_ctx;
3021 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003022
David Benjamine3aa1d92015-06-16 15:34:50 -04003023 HMAC_CTX hmac_ctx;
3024 HMAC_CTX_init(&hmac_ctx);
3025 EVP_CIPHER_CTX cipher_ctx;
3026 EVP_CIPHER_CTX_init(&cipher_ctx);
3027
David Benjaminef1b0092015-11-21 14:05:44 -05003028 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04003029 *out_session = NULL;
3030
Steven Valdez4aa154e2016-07-29 14:32:55 -04003031 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
3032 goto done;
3033 }
3034
David Benjamine3aa1d92015-06-16 15:34:50 -04003035 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3036 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003037 }
3038
David Benjaminadcc3952015-04-26 13:07:57 -04003039 /* Ensure there is room for the key name and the largest IV
3040 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3041 * the maximum IV length should be well under the minimum size for the
3042 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003043 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3044 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003045 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003046 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003047
David Benjamine3aa1d92015-06-16 15:34:50 -04003048 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003049 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3050 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3051 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003052 if (cb_ret < 0) {
3053 ret = 0;
3054 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003055 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003056 if (cb_ret == 0) {
3057 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003058 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003059 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003060 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003061 }
3062 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003063 /* Check the key name matches. */
3064 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3065 SSL_TICKET_KEY_NAME_LEN) != 0) {
3066 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003067 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003068 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3069 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003070 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003071 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3072 ssl_ctx->tlsext_tick_aes_key, iv)) {
3073 ret = 0;
3074 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003075 }
3076 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003077 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003078
David Benjamine3aa1d92015-06-16 15:34:50 -04003079 /* Check the MAC at the end of the ticket. */
3080 uint8_t mac[EVP_MAX_MD_SIZE];
3081 size_t mac_len = HMAC_size(&hmac_ctx);
3082 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003083 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003084 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003085 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003086 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3087 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003088 int mac_ok =
3089 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3090#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3091 mac_ok = 1;
3092#endif
3093 if (!mac_ok) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003094 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003095 }
3096
David Benjamine3aa1d92015-06-16 15:34:50 -04003097 /* Decrypt the session data. */
3098 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3099 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3100 mac_len;
3101 plaintext = OPENSSL_malloc(ciphertext_len);
3102 if (plaintext == NULL) {
3103 ret = 0;
3104 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003105 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003106 size_t plaintext_len;
3107#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3108 memcpy(plaintext, ciphertext, ciphertext_len);
3109 plaintext_len = ciphertext_len;
3110#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003111 if (ciphertext_len >= INT_MAX) {
3112 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003113 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003114 int len1, len2;
3115 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3116 (int)ciphertext_len) ||
3117 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
3118 ERR_clear_error(); /* Don't leave an error on the queue. */
3119 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003120 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003121 plaintext_len = (size_t)(len1 + len2);
3122#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003123
David Benjamine3aa1d92015-06-16 15:34:50 -04003124 /* Decode the session. */
David Benjaminfbc45d72016-09-22 01:21:24 -04003125 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003126 if (session == NULL) {
3127 ERR_clear_error(); /* Don't leave an error on the queue. */
3128 goto done;
3129 }
3130
3131 /* Copy the client's session ID into the new session, to denote the ticket has
3132 * been accepted. */
3133 memcpy(session->session_id, session_id, session_id_len);
3134 session->session_id_length = session_id_len;
3135
Steven Valdez4aa154e2016-07-29 14:32:55 -04003136 if (!ssl_session_is_context_valid(ssl, session) ||
3137 !ssl_session_is_time_valid(ssl, session)) {
3138 SSL_SESSION_free(session);
3139 session = NULL;
3140 }
3141
David Benjamine3aa1d92015-06-16 15:34:50 -04003142 *out_session = session;
3143
3144done:
3145 OPENSSL_free(plaintext);
3146 HMAC_CTX_cleanup(&hmac_ctx);
3147 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3148 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003149}
Adam Langley95c29f32014-06-20 12:00:00 -07003150
Steven Valdez0d62f262015-09-04 12:41:04 -04003151int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003152 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05003153 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003154 return 1;
3155 }
David Benjamincd996942014-07-20 16:23:51 -04003156
David Benjamin0fc37ef2016-08-17 15:29:46 -04003157 SSL_HANDSHAKE *hs = ssl->s3->hs;
3158 OPENSSL_free(hs->peer_sigalgs);
3159 hs->peer_sigalgs = NULL;
3160 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003161
3162 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003163 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003164 return 0;
3165 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003166 num_sigalgs /= 2;
3167
3168 /* supported_signature_algorithms in the certificate request is
3169 * allowed to be empty. */
3170 if (num_sigalgs == 0) {
3171 return 1;
3172 }
3173
Steven Valdez02563852016-06-23 13:33:05 -04003174 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3175 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003176 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3177 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003178 return 0;
3179 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003180 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003181
3182 CBS sigalgs;
3183 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003184 for (size_t i = 0; i < num_sigalgs; i++) {
3185 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003186 return 0;
3187 }
3188 }
Adam Langley95c29f32014-06-20 12:00:00 -07003189
Adam Langleyfcf25832014-12-18 17:42:32 -08003190 return 1;
3191}
David Benjaminec2f27d2014-11-13 19:17:25 -05003192
David Benjaminea9a0d52016-07-08 15:52:59 -07003193int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003194 CERT *cert = ssl->cert;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003195 SSL_HANDSHAKE *hs = ssl->s3->hs;
David Benjaminec2f27d2014-11-13 19:17:25 -05003196
Steven Valdezf0451ca2016-06-29 13:16:27 -04003197 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3198 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3199 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003200 int type = ssl_private_key_type(ssl);
3201 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003202 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003203 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003204 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003205 if (ssl_is_ecdsa_key_type(type)) {
3206 *out = SSL_SIGN_ECDSA_SHA1;
3207 return 1;
3208 }
3209 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3210 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003211 }
3212
David Benjamin3ef76972016-10-17 17:59:54 -04003213 const uint16_t *sigalgs = cert->sigalgs;
3214 size_t num_sigalgs = cert->num_sigalgs;
3215 if (sigalgs == NULL) {
3216 sigalgs = kSignSignatureAlgorithms;
3217 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003218 }
3219
David Benjamin0fc37ef2016-08-17 15:29:46 -04003220 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3221 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3222 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003223 /* If the client didn't specify any signature_algorithms extension then
3224 * we can assume that it supports SHA1. See
3225 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3226 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3227 SSL_SIGN_ECDSA_SHA1};
3228 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003229 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003230 }
3231
David Benjamin0fc37ef2016-08-17 15:29:46 -04003232 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003233 uint16_t sigalg = sigalgs[i];
3234 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3235 * negotiated. */
3236 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3237 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3238 continue;
3239 }
3240
David Benjamin0fc37ef2016-08-17 15:29:46 -04003241 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003242 if (sigalg == peer_sigalgs[j]) {
3243 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003244 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003245 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003246 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003247 }
Adam Langley95c29f32014-06-20 12:00:00 -07003248
David Benjaminea9a0d52016-07-08 15:52:59 -07003249 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3250 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003251}
Adam Langley95c29f32014-06-20 12:00:00 -07003252
Nick Harper60a85cb2016-09-23 16:25:11 -07003253int tls1_verify_channel_id(SSL *ssl) {
3254 int ret = 0;
3255 uint16_t extension_type;
3256 CBS extension, channel_id;
3257
3258 /* A Channel ID handshake message is structured to contain multiple
3259 * extensions, but the only one that can be present is Channel ID. */
3260 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3261 if (!CBS_get_u16(&channel_id, &extension_type) ||
3262 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3263 CBS_len(&channel_id) != 0 ||
3264 extension_type != TLSEXT_TYPE_channel_id ||
3265 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3266 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3267 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3268 return 0;
3269 }
3270
3271 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3272 if (!p256) {
3273 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3274 return 0;
3275 }
3276
3277 EC_KEY *key = NULL;
3278 EC_POINT *point = NULL;
3279 BIGNUM x, y;
3280 ECDSA_SIG sig;
3281 BN_init(&x);
3282 BN_init(&y);
3283 sig.r = BN_new();
3284 sig.s = BN_new();
3285 if (sig.r == NULL || sig.s == NULL) {
3286 goto err;
3287 }
3288
3289 const uint8_t *p = CBS_data(&extension);
3290 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3291 BN_bin2bn(p + 32, 32, &y) == NULL ||
3292 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3293 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3294 goto err;
3295 }
3296
3297 point = EC_POINT_new(p256);
3298 if (point == NULL ||
3299 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3300 goto err;
3301 }
3302
3303 key = EC_KEY_new();
3304 if (key == NULL ||
3305 !EC_KEY_set_group(key, p256) ||
3306 !EC_KEY_set_public_key(key, point)) {
3307 goto err;
3308 }
3309
3310 uint8_t digest[EVP_MAX_MD_SIZE];
3311 size_t digest_len;
3312 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3313 goto err;
3314 }
3315
3316 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3317#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3318 sig_ok = 1;
3319#endif
3320 if (!sig_ok) {
3321 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3322 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3323 ssl->s3->tlsext_channel_id_valid = 0;
3324 goto err;
3325 }
3326
3327 memcpy(ssl->s3->tlsext_channel_id, p, 64);
3328 ret = 1;
3329
3330err:
3331 BN_free(&x);
3332 BN_free(&y);
3333 BN_free(sig.r);
3334 BN_free(sig.s);
3335 EC_KEY_free(key);
3336 EC_POINT_free(point);
3337 EC_GROUP_free(p256);
3338 return ret;
3339}
3340
3341int tls1_write_channel_id(SSL *ssl, CBB *cbb) {
3342 uint8_t digest[EVP_MAX_MD_SIZE];
3343 size_t digest_len;
3344 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3345 return 0;
3346 }
3347
3348 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3349 if (ec_key == NULL) {
3350 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3351 return 0;
3352 }
3353
3354 int ret = 0;
3355 BIGNUM *x = BN_new();
3356 BIGNUM *y = BN_new();
3357 ECDSA_SIG *sig = NULL;
3358 if (x == NULL || y == NULL ||
3359 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3360 EC_KEY_get0_public_key(ec_key),
3361 x, y, NULL)) {
3362 goto err;
3363 }
3364
3365 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3366 if (sig == NULL) {
3367 goto err;
3368 }
3369
3370 CBB child;
3371 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3372 !CBB_add_u16_length_prefixed(cbb, &child) ||
3373 !BN_bn2cbb_padded(&child, 32, x) ||
3374 !BN_bn2cbb_padded(&child, 32, y) ||
3375 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3376 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3377 !CBB_flush(cbb)) {
3378 goto err;
3379 }
3380
3381 ret = 1;
3382
3383err:
3384 BN_free(x);
3385 BN_free(y);
3386 ECDSA_SIG_free(sig);
3387 return ret;
3388}
3389
David Benjamind6a4ae92015-08-06 11:10:51 -04003390int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003391 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3392 uint8_t *msg;
3393 size_t msg_len;
3394 if (!tls13_get_cert_verify_signature_input(ssl, &msg, &msg_len,
3395 ssl_cert_verify_channel_id)) {
3396 return 0;
3397 }
3398 SHA256(msg, msg_len, out);
3399 *out_len = SHA256_DIGEST_LENGTH;
3400 OPENSSL_free(msg);
3401 return 1;
3402 }
3403
Nick Harper95594012016-10-20 14:07:13 -07003404 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003405
Nick Harper95594012016-10-20 14:07:13 -07003406 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003407 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003408 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003409
Steven Valdez87eab492016-06-27 16:34:59 -04003410 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003411 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003412 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003413 if (ssl->session->original_handshake_hash_len == 0) {
3414 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003415 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003416 }
Nick Harper95594012016-10-20 14:07:13 -07003417 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3418 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003419 }
3420
3421 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3422 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3423 sizeof(handshake_hash));
3424 if (handshake_hash_len < 0) {
Nick Harper95594012016-10-20 14:07:13 -07003425 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003426 }
Nick Harper95594012016-10-20 14:07:13 -07003427 SHA256_Update(&ctx, handshake_hash, (size_t)handshake_hash_len);
3428 SHA256_Final(out, &ctx);
3429 *out_len = SHA256_DIGEST_LENGTH;
3430 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003431}
Adam Langley1258b6a2014-06-20 12:00:00 -07003432
3433/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003434 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003435 * data. */
3436int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003437 int digest_len;
3438 /* This function should never be called for a resumed session because the
3439 * handshake hashes that we wish to record are for the original, full
3440 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003441 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003442 return -1;
3443 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003444
Adam Langleyfcf25832014-12-18 17:42:32 -08003445 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003446 tls1_handshake_digest(
3447 ssl, ssl->s3->new_session->original_handshake_hash,
3448 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003449 if (digest_len < 0) {
3450 return -1;
3451 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003452
Steven Valdez87eab492016-06-27 16:34:59 -04003453 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003454
Adam Langleyfcf25832014-12-18 17:42:32 -08003455 return 1;
3456}
Nick Harper60a85cb2016-09-23 16:25:11 -07003457
3458int ssl_do_channel_id_callback(SSL *ssl) {
3459 if (ssl->tlsext_channel_id_private != NULL ||
3460 ssl->ctx->channel_id_cb == NULL) {
3461 return 1;
3462 }
3463
3464 EVP_PKEY *key = NULL;
3465 ssl->ctx->channel_id_cb(ssl, &key);
3466 if (key == NULL) {
3467 /* The caller should try again later. */
3468 return 1;
3469 }
3470
3471 int ret = SSL_set1_tls_channel_id(ssl, key);
3472 EVP_PKEY_free(key);
3473 return ret;
3474}