blob: 19c36d7636c658cfdc8fe0267c447df72f53395e [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 Benjamin4eb95cc2016-11-16 17:08:23 +0900708 /* Copy the hostname as a string. */
709 if (!CBS_strdup(&host_name, &ssl->s3->hs->hostname)) {
710 *out_alert = SSL_AD_INTERNAL_ERROR;
711 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -0700712 }
713
David Benjamin4eb95cc2016-11-16 17:08:23 +0900714 ssl->s3->hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700715 return 1;
716}
717
718static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900719 if (ssl->s3->session_reused ||
720 !ssl->s3->hs->should_ack_sni) {
Adam Langley614c66a2015-06-12 15:26:58 -0700721 return 1;
722 }
723
724 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
725 !CBB_add_u16(out, 0 /* length */)) {
726 return 0;
727 }
728
729 return 1;
730}
731
732
Adam Langley5021b222015-06-12 18:27:58 -0700733/* Renegotiation indication.
734 *
735 * https://tools.ietf.org/html/rfc5746 */
736
737static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400738 uint16_t min_version, max_version;
739 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
740 return 0;
741 }
742
743 /* Renegotiation indication is not necessary in TLS 1.3. */
744 if (min_version >= TLS1_3_VERSION) {
745 return 1;
746 }
747
David Benjamin52bf6902016-10-08 12:05:03 -0400748 assert(ssl->s3->initial_handshake_complete ==
749 (ssl->s3->previous_client_finished_len != 0));
750
Adam Langley5021b222015-06-12 18:27:58 -0700751 CBB contents, prev_finished;
752 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
753 !CBB_add_u16_length_prefixed(out, &contents) ||
754 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
755 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
756 ssl->s3->previous_client_finished_len) ||
757 !CBB_flush(out)) {
758 return 0;
759 }
760
761 return 1;
762}
763
764static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
765 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400766 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
767 return 0;
768 }
769
David Benjamin3e052de2015-11-25 20:10:31 -0500770 /* Servers may not switch between omitting the extension and supporting it.
771 * See RFC 5746, sections 3.5 and 4.2. */
772 if (ssl->s3->initial_handshake_complete &&
773 (contents != NULL) != ssl->s3->send_connection_binding) {
774 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
775 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
776 return 0;
777 }
778
Adam Langley5021b222015-06-12 18:27:58 -0700779 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500780 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700781 * RI even on initial ServerHello because the client doesn't see any
782 * renegotiation during an attack. However this would mean we could not
783 * connect to any server which doesn't support RI.
784 *
David Benjamine9cddb82015-11-23 14:36:40 -0500785 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
786 * practical terms every client sets it so it's just assumed here. */
787 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700788 }
789
790 const size_t expected_len = ssl->s3->previous_client_finished_len +
791 ssl->s3->previous_server_finished_len;
792
793 /* Check for logic errors */
794 assert(!expected_len || ssl->s3->previous_client_finished_len);
795 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400796 assert(ssl->s3->initial_handshake_complete ==
797 (ssl->s3->previous_client_finished_len != 0));
798 assert(ssl->s3->initial_handshake_complete ==
799 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700800
801 /* Parse out the extension contents. */
802 CBS renegotiated_connection;
803 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
804 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400805 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700806 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
807 return 0;
808 }
809
810 /* Check that the extension matches. */
811 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400812 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700813 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
814 return 0;
815 }
816
817 const uint8_t *d = CBS_data(&renegotiated_connection);
818 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
819 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400820 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700821 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
822 return 0;
823 }
824 d += ssl->s3->previous_client_finished_len;
825
826 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
827 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400828 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700829 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
830 return 0;
831 }
832 ssl->s3->send_connection_binding = 1;
833
834 return 1;
835}
836
837static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
838 CBS *contents) {
839 /* Renegotiation isn't supported as a server so this function should never be
840 * called after the initial handshake. */
841 assert(!ssl->s3->initial_handshake_complete);
842
Steven Valdez143e8b32016-07-11 13:19:03 -0400843 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
844 return 1;
845 }
846
Adam Langley5021b222015-06-12 18:27:58 -0700847 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400848 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700849 }
850
851 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700852 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
853 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400854 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700855 return 0;
856 }
857
David Benjamin52bf6902016-10-08 12:05:03 -0400858 /* Check that the extension matches. We do not support renegotiation as a
859 * server, so this must be empty. */
860 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400861 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700862 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
863 return 0;
864 }
865
866 ssl->s3->send_connection_binding = 1;
867
868 return 1;
869}
870
871static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin52bf6902016-10-08 12:05:03 -0400872 /* Renegotiation isn't supported as a server so this function should never be
873 * called after the initial handshake. */
874 assert(!ssl->s3->initial_handshake_complete);
875
Steven Valdez143e8b32016-07-11 13:19:03 -0400876 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
877 return 1;
878 }
879
Adam Langley5021b222015-06-12 18:27:58 -0700880 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400881 !CBB_add_u16(out, 1 /* length */) ||
882 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700883 return 0;
884 }
885
886 return 1;
887}
888
Adam Langley0a056712015-07-01 15:03:33 -0700889
890/* Extended Master Secret.
891 *
David Benjamin43946d42016-02-01 08:42:19 -0500892 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700893
Adam Langley0a056712015-07-01 15:03:33 -0700894static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400895 uint16_t min_version, max_version;
896 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
897 return 0;
898 }
899
900 /* Extended master secret is not necessary in TLS 1.3. */
901 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700902 return 1;
903 }
904
905 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
906 !CBB_add_u16(out, 0 /* length */)) {
907 return 0;
908 }
909
910 return 1;
911}
912
913static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
914 CBS *contents) {
David Benjamin163c9562016-08-29 23:14:17 -0400915 /* Whether EMS is negotiated may not change on renegotation. */
916 if (ssl->s3->initial_handshake_complete) {
917 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
918 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
919 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
920 return 0;
921 }
922
923 return 1;
924 }
925
Adam Langley0a056712015-07-01 15:03:33 -0700926 if (contents == NULL) {
927 return 1;
928 }
929
Steven Valdez143e8b32016-07-11 13:19:03 -0400930 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
931 ssl->version == SSL3_VERSION) {
932 return 0;
933 }
934
935 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700936 return 0;
937 }
938
939 ssl->s3->tmp.extended_master_secret = 1;
940 return 1;
941}
942
David Benjamin0d56f882015-12-19 17:05:56 -0500943static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
944 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400945 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
946 ssl->version == SSL3_VERSION) {
947 return 1;
948 }
949
950 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700951 return 1;
952 }
953
954 if (CBS_len(contents) != 0) {
955 return 0;
956 }
957
958 ssl->s3->tmp.extended_master_secret = 1;
959 return 1;
960}
961
962static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
963 if (!ssl->s3->tmp.extended_master_secret) {
964 return 1;
965 }
966
967 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
968 !CBB_add_u16(out, 0 /* length */)) {
969 return 0;
970 }
971
972 return 1;
973}
974
Adam Langley9b05bc52015-07-01 15:25:33 -0700975
976/* Session tickets.
977 *
978 * https://tools.ietf.org/html/rfc5077 */
979
980static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400981 uint16_t min_version, max_version;
982 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
983 return 0;
984 }
985
986 /* TLS 1.3 uses a different ticket extension. */
987 if (min_version >= TLS1_3_VERSION ||
988 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700989 return 1;
990 }
991
992 const uint8_t *ticket_data = NULL;
993 int ticket_len = 0;
994
995 /* Renegotiation does not participate in session resumption. However, still
996 * advertise the extension to avoid potentially breaking servers which carry
997 * over the state from the previous handshake, such as OpenSSL servers
998 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -0400999 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -07001000 if (!ssl->s3->initial_handshake_complete &&
1001 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001002 ssl->session->tlsext_tick != NULL &&
1003 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -04001004 ssl->method->version_from_wire(&session_version,
1005 ssl->session->ssl_version) &&
1006 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001007 ticket_data = ssl->session->tlsext_tick;
1008 ticket_len = ssl->session->tlsext_ticklen;
1009 }
1010
1011 CBB ticket;
1012 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1013 !CBB_add_u16_length_prefixed(out, &ticket) ||
1014 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1015 !CBB_flush(out)) {
1016 return 0;
1017 }
1018
1019 return 1;
1020}
1021
1022static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1023 CBS *contents) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001024 if (contents == NULL) {
1025 return 1;
1026 }
1027
Steven Valdez143e8b32016-07-11 13:19:03 -04001028 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1029 return 0;
1030 }
1031
Adam Langley9b05bc52015-07-01 15:25:33 -07001032 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1033 * this function should never be called, even if the server tries to send the
1034 * extension. */
1035 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1036
1037 if (CBS_len(contents) != 0) {
1038 return 0;
1039 }
1040
David Benjamin78476f62016-11-12 11:20:55 +09001041 ssl->s3->hs->ticket_expected = 1;
Adam Langley9b05bc52015-07-01 15:25:33 -07001042 return 1;
1043}
1044
Adam Langley9b05bc52015-07-01 15:25:33 -07001045static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin78476f62016-11-12 11:20:55 +09001046 if (!ssl->s3->hs->ticket_expected) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001047 return 1;
1048 }
1049
David Benjamin78476f62016-11-12 11:20:55 +09001050 /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */
Adam Langley9b05bc52015-07-01 15:25:33 -07001051 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1052
1053 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1054 !CBB_add_u16(out, 0 /* length */)) {
1055 return 0;
1056 }
1057
1058 return 1;
1059}
1060
1061
Adam Langley2e857bd2015-07-01 16:09:19 -07001062/* Signature Algorithms.
1063 *
1064 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1065
1066static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin2dc02042016-09-19 19:57:37 -04001067 uint16_t min_version, max_version;
1068 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1069 return 0;
1070 }
1071
1072 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001073 return 1;
1074 }
1075
David Benjamin0fc37ef2016-08-17 15:29:46 -04001076 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001077 const size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001078
David Benjamin0fc37ef2016-08-17 15:29:46 -04001079 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001080 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1081 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001082 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001083 return 0;
1084 }
1085
David Benjamin0fc37ef2016-08-17 15:29:46 -04001086 for (size_t i = 0; i < num_sigalgs; i++) {
1087 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001088 return 0;
1089 }
1090 }
1091
1092 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001093 return 0;
1094 }
1095
1096 return 1;
1097}
1098
Adam Langley2e857bd2015-07-01 16:09:19 -07001099static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1100 CBS *contents) {
David Benjamin0fc37ef2016-08-17 15:29:46 -04001101 OPENSSL_free(ssl->s3->hs->peer_sigalgs);
1102 ssl->s3->hs->peer_sigalgs = NULL;
1103 ssl->s3->hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001104
Adam Langley2e857bd2015-07-01 16:09:19 -07001105 if (contents == NULL) {
1106 return 1;
1107 }
1108
1109 CBS supported_signature_algorithms;
1110 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001111 CBS_len(contents) != 0 ||
1112 CBS_len(&supported_signature_algorithms) == 0 ||
1113 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001114 return 0;
1115 }
1116
1117 return 1;
1118}
1119
Adam Langley2e857bd2015-07-01 16:09:19 -07001120
Adam Langleybb0bd042015-07-01 16:21:03 -07001121/* OCSP Stapling.
1122 *
1123 * https://tools.ietf.org/html/rfc6066#section-8 */
1124
1125static void ext_ocsp_init(SSL *ssl) {
Adam Langleyce9d85e2016-01-24 15:58:39 -08001126 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001127}
1128
1129static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1130 if (!ssl->ocsp_stapling_enabled) {
1131 return 1;
1132 }
1133
1134 CBB contents;
1135 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1136 !CBB_add_u16_length_prefixed(out, &contents) ||
1137 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1138 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1139 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1140 !CBB_flush(out)) {
1141 return 0;
1142 }
1143
Adam Langleyce9d85e2016-01-24 15:58:39 -08001144 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001145 return 1;
1146}
1147
1148static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001149 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001150 if (contents == NULL) {
1151 return 1;
1152 }
1153
Steven Valdeza833c352016-11-01 13:39:36 -04001154 /* TLS 1.3 OCSP responses are included in the Certificate extensions. */
1155 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001156 return 0;
1157 }
1158
Steven Valdeza833c352016-11-01 13:39:36 -04001159 /* OCSP stapling is forbidden on non-certificate ciphers. */
1160 if (CBS_len(contents) != 0 ||
1161 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001162 return 0;
1163 }
1164
Steven Valdeza833c352016-11-01 13:39:36 -04001165 /* Note this does not check for resumption in TLS 1.2. Sending
1166 * status_request here does not make sense, but OpenSSL does so and the
1167 * specification does not say anything. Tolerate it but ignore it. */
David Benjamin942f4ed2016-07-16 19:03:49 +03001168
Steven Valdeza833c352016-11-01 13:39:36 -04001169 ssl->s3->hs->certificate_status_expected = 1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001170 return 1;
1171}
1172
1173static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1174 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001175 if (contents == NULL) {
1176 return 1;
1177 }
1178
1179 uint8_t status_type;
1180 if (!CBS_get_u8(contents, &status_type)) {
1181 return 0;
1182 }
1183
1184 /* We cannot decide whether OCSP stapling will occur yet because the correct
1185 * SSL_CTX might not have been selected. */
David Benjamina0486782016-10-06 19:11:32 -04001186 ssl->s3->hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001187
Adam Langleybb0bd042015-07-01 16:21:03 -07001188 return 1;
1189}
1190
1191static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdeza833c352016-11-01 13:39:36 -04001192 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1193 !ssl->s3->hs->ocsp_stapling_requested ||
David Benjamin942f4ed2016-07-16 19:03:49 +03001194 ssl->ctx->ocsp_response_length == 0 ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001195 ssl->s3->session_reused ||
Steven Valdeza833c352016-11-01 13:39:36 -04001196 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001197 return 1;
1198 }
1199
Steven Valdeza833c352016-11-01 13:39:36 -04001200 ssl->s3->hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001201
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001202 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
Steven Valdeza833c352016-11-01 13:39:36 -04001203 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001204}
1205
1206
Adam Langley97dfcbf2015-07-01 18:35:20 -07001207/* Next protocol negotiation.
1208 *
1209 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1210
Adam Langley97dfcbf2015-07-01 18:35:20 -07001211static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1212 if (ssl->s3->initial_handshake_complete ||
1213 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001214 (ssl->options & SSL_OP_DISABLE_NPN) ||
David Benjamince079fd2016-08-02 16:22:34 -04001215 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001216 return 1;
1217 }
1218
1219 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1220 !CBB_add_u16(out, 0 /* length */)) {
1221 return 0;
1222 }
1223
1224 return 1;
1225}
1226
1227static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1228 CBS *contents) {
1229 if (contents == NULL) {
1230 return 1;
1231 }
1232
Steven Valdez143e8b32016-07-11 13:19:03 -04001233 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1234 return 0;
1235 }
1236
Adam Langley97dfcbf2015-07-01 18:35:20 -07001237 /* If any of these are false then we should never have sent the NPN
1238 * extension in the ClientHello and thus this function should never have been
1239 * called. */
1240 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001241 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001242 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001243 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001244
David Benjamin76c2efc2015-08-31 14:24:29 -04001245 if (ssl->s3->alpn_selected != NULL) {
1246 /* NPN and ALPN may not be negotiated in the same connection. */
1247 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1248 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1249 return 0;
1250 }
1251
Adam Langley97dfcbf2015-07-01 18:35:20 -07001252 const uint8_t *const orig_contents = CBS_data(contents);
1253 const size_t orig_len = CBS_len(contents);
1254
1255 while (CBS_len(contents) != 0) {
1256 CBS proto;
1257 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1258 CBS_len(&proto) == 0) {
1259 return 0;
1260 }
1261 }
1262
1263 uint8_t *selected;
1264 uint8_t selected_len;
1265 if (ssl->ctx->next_proto_select_cb(
1266 ssl, &selected, &selected_len, orig_contents, orig_len,
1267 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1268 *out_alert = SSL_AD_INTERNAL_ERROR;
1269 return 0;
1270 }
1271
David Benjamin79978df2015-12-25 15:56:49 -05001272 OPENSSL_free(ssl->s3->next_proto_negotiated);
1273 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1274 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001275 *out_alert = SSL_AD_INTERNAL_ERROR;
1276 return 0;
1277 }
1278
David Benjamin79978df2015-12-25 15:56:49 -05001279 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjaminb74b0812016-10-06 19:43:48 -04001280 ssl->s3->hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001281
1282 return 1;
1283}
1284
1285static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1286 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001287 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1288 return 1;
1289 }
1290
Adam Langley97dfcbf2015-07-01 18:35:20 -07001291 if (contents != NULL && CBS_len(contents) != 0) {
1292 return 0;
1293 }
1294
1295 if (contents == NULL ||
1296 ssl->s3->initial_handshake_complete ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001297 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001298 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001299 return 1;
1300 }
1301
David Benjaminb74b0812016-10-06 19:43:48 -04001302 ssl->s3->hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001303 return 1;
1304}
1305
1306static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1307 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1308 * parsed. */
David Benjaminb74b0812016-10-06 19:43:48 -04001309 if (!ssl->s3->hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001310 return 1;
1311 }
1312
1313 const uint8_t *npa;
1314 unsigned npa_len;
1315
1316 if (ssl->ctx->next_protos_advertised_cb(
1317 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1318 SSL_TLSEXT_ERR_OK) {
David Benjaminb74b0812016-10-06 19:43:48 -04001319 ssl->s3->hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001320 return 1;
1321 }
1322
1323 CBB contents;
1324 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1325 !CBB_add_u16_length_prefixed(out, &contents) ||
1326 !CBB_add_bytes(&contents, npa, npa_len) ||
1327 !CBB_flush(out)) {
1328 return 0;
1329 }
1330
1331 return 1;
1332}
1333
1334
Adam Langleyab8d87d2015-07-10 12:21:39 -07001335/* Signed certificate timestamps.
1336 *
1337 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1338
1339static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1340 if (!ssl->signed_cert_timestamps_enabled) {
1341 return 1;
1342 }
1343
1344 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1345 !CBB_add_u16(out, 0 /* length */)) {
1346 return 0;
1347 }
1348
1349 return 1;
1350}
1351
1352static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1353 CBS *contents) {
1354 if (contents == NULL) {
1355 return 1;
1356 }
1357
Steven Valdeza833c352016-11-01 13:39:36 -04001358 /* TLS 1.3 SCTs are included in the Certificate extensions. */
1359 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Adam Langleycfa08c32016-11-17 13:21:27 -08001360 *out_alert = SSL_AD_DECODE_ERROR;
Steven Valdeza833c352016-11-01 13:39:36 -04001361 return 0;
1362 }
1363
Adam Langleyab8d87d2015-07-10 12:21:39 -07001364 /* If this is false then we should never have sent the SCT extension in the
1365 * ClientHello and thus this function should never have been called. */
1366 assert(ssl->signed_cert_timestamps_enabled);
1367
Adam Langleycfa08c32016-11-17 13:21:27 -08001368 if (!ssl_is_sct_list_valid(contents)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001369 *out_alert = SSL_AD_DECODE_ERROR;
1370 return 0;
1371 }
1372
David Benjamindaa88502016-10-04 16:32:16 -04001373 /* Session resumption uses the original session information. The extension
1374 * should not be sent on resumption, but RFC 6962 did not make it a
1375 * requirement, so tolerate this.
1376 *
1377 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001378 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001379 !CBS_stow(
1380 contents,
1381 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1382 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001383 *out_alert = SSL_AD_INTERNAL_ERROR;
1384 return 0;
1385 }
1386
1387 return 1;
1388}
1389
1390static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1391 CBS *contents) {
David Benjamin53210cb2016-11-16 09:01:48 +09001392 if (contents == NULL) {
1393 return 1;
1394 }
1395
1396 if (CBS_len(contents) != 0) {
1397 return 0;
1398 }
1399
Steven Valdeza833c352016-11-01 13:39:36 -04001400 ssl->s3->hs->scts_requested = 1;
David Benjamin53210cb2016-11-16 09:01:48 +09001401 return 1;
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
Adam Langleyf18e4532015-07-10 13:39:53 -07001425static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1426 if (ssl->alpn_client_proto_list == NULL ||
1427 ssl->s3->initial_handshake_complete) {
1428 return 1;
1429 }
1430
1431 CBB contents, proto_list;
1432 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1433 !CBB_add_u16_length_prefixed(out, &contents) ||
1434 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1435 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1436 ssl->alpn_client_proto_list_len) ||
1437 !CBB_flush(out)) {
1438 return 0;
1439 }
1440
1441 return 1;
1442}
1443
1444static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1445 CBS *contents) {
1446 if (contents == NULL) {
1447 return 1;
1448 }
1449
1450 assert(!ssl->s3->initial_handshake_complete);
1451 assert(ssl->alpn_client_proto_list != NULL);
1452
David Benjaminb74b0812016-10-06 19:43:48 -04001453 if (ssl->s3->hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001454 /* NPN and ALPN may not be negotiated in the same connection. */
1455 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1456 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1457 return 0;
1458 }
1459
Adam Langleyf18e4532015-07-10 13:39:53 -07001460 /* The extension data consists of a ProtocolNameList which must have
1461 * exactly one ProtocolName. Each of these is length-prefixed. */
1462 CBS protocol_name_list, protocol_name;
1463 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1464 CBS_len(contents) != 0 ||
1465 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1466 /* Empty protocol names are forbidden. */
1467 CBS_len(&protocol_name) == 0 ||
1468 CBS_len(&protocol_name_list) != 0) {
1469 return 0;
1470 }
1471
David Benjamin3e517572016-08-11 11:52:23 -04001472 /* Check that the protcol name is one of the ones we advertised. */
1473 int protocol_ok = 0;
1474 CBS client_protocol_name_list, client_protocol_name;
1475 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1476 ssl->alpn_client_proto_list_len);
1477 while (CBS_len(&client_protocol_name_list) > 0) {
1478 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1479 &client_protocol_name)) {
1480 *out_alert = SSL_AD_INTERNAL_ERROR;
1481 return 0;
1482 }
1483
1484 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1485 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1486 CBS_len(&protocol_name)) == 0) {
1487 protocol_ok = 1;
1488 break;
1489 }
1490 }
1491
1492 if (!protocol_ok) {
1493 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1494 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1495 return 0;
1496 }
1497
Adam Langleyf18e4532015-07-10 13:39:53 -07001498 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1499 &ssl->s3->alpn_selected_len)) {
1500 *out_alert = SSL_AD_INTERNAL_ERROR;
1501 return 0;
1502 }
1503
1504 return 1;
1505}
1506
David Benjamin9ef31f02016-10-31 18:01:13 -04001507int ssl_negotiate_alpn(SSL *ssl, uint8_t *out_alert,
1508 const struct ssl_early_callback_ctx *client_hello) {
1509 CBS contents;
Adam Langleyf18e4532015-07-10 13:39:53 -07001510 if (ssl->ctx->alpn_select_cb == NULL ||
David Benjamin9ef31f02016-10-31 18:01:13 -04001511 !ssl_early_callback_get_extension(
1512 client_hello, &contents,
1513 TLSEXT_TYPE_application_layer_protocol_negotiation)) {
1514 /* Ignore ALPN if not configured or no extension was supplied. */
Adam Langleyf18e4532015-07-10 13:39:53 -07001515 return 1;
1516 }
1517
1518 /* ALPN takes precedence over NPN. */
David Benjaminb74b0812016-10-06 19:43:48 -04001519 ssl->s3->hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001520
1521 CBS protocol_name_list;
David Benjamin9ef31f02016-10-31 18:01:13 -04001522 if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1523 CBS_len(&contents) != 0 ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001524 CBS_len(&protocol_name_list) < 2) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001525 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1526 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001527 return 0;
1528 }
1529
1530 /* Validate the protocol list. */
1531 CBS protocol_name_list_copy = protocol_name_list;
1532 while (CBS_len(&protocol_name_list_copy) > 0) {
1533 CBS protocol_name;
1534
1535 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1536 /* Empty protocol names are forbidden. */
1537 CBS_len(&protocol_name) == 0) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001538 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1539 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyf18e4532015-07-10 13:39:53 -07001540 return 0;
1541 }
1542 }
1543
1544 const uint8_t *selected;
1545 uint8_t selected_len;
1546 if (ssl->ctx->alpn_select_cb(
1547 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1548 CBS_len(&protocol_name_list),
1549 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1550 OPENSSL_free(ssl->s3->alpn_selected);
1551 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1552 if (ssl->s3->alpn_selected == NULL) {
1553 *out_alert = SSL_AD_INTERNAL_ERROR;
1554 return 0;
1555 }
1556 ssl->s3->alpn_selected_len = selected_len;
1557 }
1558
1559 return 1;
1560}
1561
1562static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1563 if (ssl->s3->alpn_selected == NULL) {
1564 return 1;
1565 }
1566
1567 CBB contents, proto_list, proto;
1568 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1569 !CBB_add_u16_length_prefixed(out, &contents) ||
1570 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1571 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001572 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1573 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001574 !CBB_flush(out)) {
1575 return 0;
1576 }
1577
1578 return 1;
1579}
1580
1581
Adam Langley49c7af12015-07-10 14:33:46 -07001582/* Channel ID.
1583 *
1584 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1585
1586static void ext_channel_id_init(SSL *ssl) {
1587 ssl->s3->tlsext_channel_id_valid = 0;
1588}
1589
1590static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1591 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001592 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001593 return 1;
1594 }
1595
1596 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1597 !CBB_add_u16(out, 0 /* length */)) {
1598 return 0;
1599 }
1600
1601 return 1;
1602}
1603
1604static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1605 CBS *contents) {
1606 if (contents == NULL) {
1607 return 1;
1608 }
1609
David Benjamince079fd2016-08-02 16:22:34 -04001610 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001611 assert(ssl->tlsext_channel_id_enabled);
1612
1613 if (CBS_len(contents) != 0) {
1614 return 0;
1615 }
1616
1617 ssl->s3->tlsext_channel_id_valid = 1;
1618 return 1;
1619}
1620
1621static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1622 CBS *contents) {
1623 if (contents == NULL ||
1624 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001625 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001626 return 1;
1627 }
1628
1629 if (CBS_len(contents) != 0) {
1630 return 0;
1631 }
1632
1633 ssl->s3->tlsext_channel_id_valid = 1;
1634 return 1;
1635}
1636
1637static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1638 if (!ssl->s3->tlsext_channel_id_valid) {
1639 return 1;
1640 }
1641
1642 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1643 !CBB_add_u16(out, 0 /* length */)) {
1644 return 0;
1645 }
1646
1647 return 1;
1648}
1649
Adam Langley391250d2015-07-15 19:06:07 -07001650
1651/* Secure Real-time Transport Protocol (SRTP) extension.
1652 *
1653 * https://tools.ietf.org/html/rfc5764 */
1654
Adam Langley391250d2015-07-15 19:06:07 -07001655
1656static void ext_srtp_init(SSL *ssl) {
1657 ssl->srtp_profile = NULL;
1658}
1659
1660static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1661 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1662 if (profiles == NULL) {
1663 return 1;
1664 }
1665 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1666 if (num_profiles == 0) {
1667 return 1;
1668 }
1669
1670 CBB contents, profile_ids;
1671 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1672 !CBB_add_u16_length_prefixed(out, &contents) ||
1673 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1674 return 0;
1675 }
1676
David Benjamin54091232016-09-05 12:47:25 -04001677 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001678 if (!CBB_add_u16(&profile_ids,
1679 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1680 return 0;
1681 }
1682 }
1683
1684 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1685 !CBB_flush(out)) {
1686 return 0;
1687 }
1688
1689 return 1;
1690}
1691
1692static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1693 CBS *contents) {
1694 if (contents == NULL) {
1695 return 1;
1696 }
1697
1698 /* The extension consists of a u16-prefixed profile ID list containing a
1699 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1700 *
1701 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1702 CBS profile_ids, srtp_mki;
1703 uint16_t profile_id;
1704 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1705 !CBS_get_u16(&profile_ids, &profile_id) ||
1706 CBS_len(&profile_ids) != 0 ||
1707 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1708 CBS_len(contents) != 0) {
1709 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1710 return 0;
1711 }
1712
1713 if (CBS_len(&srtp_mki) != 0) {
1714 /* Must be no MKI, since we never offer one. */
1715 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1716 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1717 return 0;
1718 }
1719
1720 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1721
1722 /* Check to see if the server gave us something we support (and presumably
1723 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001724 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001725 const SRTP_PROTECTION_PROFILE *profile =
1726 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1727
1728 if (profile->id == profile_id) {
1729 ssl->srtp_profile = profile;
1730 return 1;
1731 }
1732 }
1733
1734 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1735 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1736 return 0;
1737}
1738
1739static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1740 CBS *contents) {
1741 if (contents == NULL) {
1742 return 1;
1743 }
1744
1745 CBS profile_ids, srtp_mki;
1746 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1747 CBS_len(&profile_ids) < 2 ||
1748 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1749 CBS_len(contents) != 0) {
1750 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1751 return 0;
1752 }
1753 /* Discard the MKI value for now. */
1754
1755 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1756 SSL_get_srtp_profiles(ssl);
1757
1758 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001759 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001760 const SRTP_PROTECTION_PROFILE *server_profile =
1761 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1762
1763 CBS profile_ids_tmp;
1764 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1765
1766 while (CBS_len(&profile_ids_tmp) > 0) {
1767 uint16_t profile_id;
1768 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1769 return 0;
1770 }
1771
1772 if (server_profile->id == profile_id) {
1773 ssl->srtp_profile = server_profile;
1774 return 1;
1775 }
1776 }
1777 }
1778
1779 return 1;
1780}
1781
1782static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1783 if (ssl->srtp_profile == NULL) {
1784 return 1;
1785 }
1786
1787 CBB contents, profile_ids;
1788 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1789 !CBB_add_u16_length_prefixed(out, &contents) ||
1790 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1791 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1792 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1793 !CBB_flush(out)) {
1794 return 0;
1795 }
1796
1797 return 1;
1798}
1799
Adam Langleybdd5d662015-07-20 16:19:08 -07001800
1801/* EC point formats.
1802 *
1803 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1804
Adam Langleybdd5d662015-07-20 16:19:08 -07001805static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001806 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001807 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1808 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001809 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1810 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001811 !CBB_flush(out)) {
1812 return 0;
1813 }
1814
1815 return 1;
1816}
1817
1818static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin70aba262016-11-01 12:08:15 -04001819 uint16_t min_version, max_version;
1820 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1821 return 0;
1822 }
1823
1824 /* The point format extension is unneccessary in TLS 1.3. */
1825 if (min_version >= TLS1_3_VERSION) {
1826 return 1;
1827 }
1828
Adam Langleybdd5d662015-07-20 16:19:08 -07001829 return ext_ec_point_add_extension(ssl, out);
1830}
1831
1832static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1833 CBS *contents) {
1834 if (contents == NULL) {
1835 return 1;
1836 }
1837
Steven Valdez143e8b32016-07-11 13:19:03 -04001838 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1839 return 0;
1840 }
1841
Adam Langleybdd5d662015-07-20 16:19:08 -07001842 CBS ec_point_format_list;
1843 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1844 CBS_len(contents) != 0) {
1845 return 0;
1846 }
1847
David Benjaminfc059942015-07-30 23:01:59 -04001848 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1849 * point format. */
1850 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1851 CBS_len(&ec_point_format_list)) == NULL) {
1852 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001853 return 0;
1854 }
1855
1856 return 1;
1857}
1858
1859static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1860 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001861 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1862 return 1;
1863 }
1864
Adam Langleybdd5d662015-07-20 16:19:08 -07001865 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1866}
1867
1868static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001869 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1870 return 1;
1871 }
1872
Adam Langleybdd5d662015-07-20 16:19:08 -07001873 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1874 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001875 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001876
1877 if (!using_ecc) {
1878 return 1;
1879 }
1880
1881 return ext_ec_point_add_extension(ssl, out);
1882}
1883
Steven Valdeza833c352016-11-01 13:39:36 -04001884
Steven Valdez4aa154e2016-07-29 14:32:55 -04001885/* Pre Shared Key
1886 *
Steven Valdeza833c352016-11-01 13:39:36 -04001887 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001888
Steven Valdeza833c352016-11-01 13:39:36 -04001889static size_t ext_pre_shared_key_clienthello_length(SSL *ssl) {
1890 uint16_t min_version, max_version;
1891 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1892 return 0;
1893 }
1894
1895 uint16_t session_version;
1896 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1897 !ssl->method->version_from_wire(&session_version,
1898 ssl->session->ssl_version) ||
1899 session_version < TLS1_3_VERSION) {
1900 return 0;
1901 }
1902
1903 const EVP_MD *digest =
1904 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1905 size_t binder_len = EVP_MD_size(digest);
1906 return 15 + ssl->session->tlsext_ticklen + binder_len;
1907}
1908
David Benjamin12d6baf2016-11-17 14:31:41 +09001909static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001910 uint16_t min_version, max_version;
1911 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1912 return 0;
1913 }
1914
David Benjamin2dc02042016-09-19 19:57:37 -04001915 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001916 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001917 !ssl->method->version_from_wire(&session_version,
1918 ssl->session->ssl_version) ||
1919 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001920 return 1;
1921 }
1922
Steven Valdeza833c352016-11-01 13:39:36 -04001923 struct timeval now;
1924 ssl_get_current_time(ssl, &now);
1925 uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
1926 uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
1927
1928 /* Fill in a placeholder zero binder of the appropriate length. It will be
1929 * computed and filled in later after length prefixes are computed. */
1930 uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
1931 const EVP_MD *digest =
1932 ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf);
1933 size_t binder_len = EVP_MD_size(digest);
1934
1935 CBB contents, identity, ticket, binders, binder;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001936 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1937 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001938 !CBB_add_u16_length_prefixed(&contents, &identity) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001939 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1940 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdeza833c352016-11-01 13:39:36 -04001941 ssl->session->tlsext_ticklen) ||
1942 !CBB_add_u32(&identity, obfuscated_ticket_age) ||
1943 !CBB_add_u16_length_prefixed(&contents, &binders) ||
1944 !CBB_add_u8_length_prefixed(&binders, &binder) ||
1945 !CBB_add_bytes(&binder, zero_binder, binder_len)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001946 return 0;
1947 }
1948
Steven Valdeza833c352016-11-01 13:39:36 -04001949 ssl->s3->hs->needs_psk_binder = 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001950 return CBB_flush(out);
1951}
1952
1953int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1954 CBS *contents) {
1955 uint16_t psk_id;
1956 if (!CBS_get_u16(contents, &psk_id) ||
1957 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001958 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001959 *out_alert = SSL_AD_DECODE_ERROR;
1960 return 0;
1961 }
1962
David Benjamineab773a2016-11-09 18:32:35 -05001963 /* We only advertise one PSK identity, so the only legal index is zero. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001964 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001965 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001966 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1967 return 0;
1968 }
1969
1970 return 1;
1971}
1972
1973int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
1974 SSL_SESSION **out_session,
Steven Valdeza833c352016-11-01 13:39:36 -04001975 CBS *out_binders,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001976 uint8_t *out_alert,
1977 CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04001978 /* We only process the first PSK identity since we don't support pure PSK. */
Steven Valdeza833c352016-11-01 13:39:36 -04001979 uint32_t obfuscated_ticket_age;
David Benjaminaedf3032016-12-01 16:47:56 -05001980 CBS identities, ticket, binders;
1981 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
1982 !CBS_get_u16_length_prefixed(&identities, &ticket) ||
1983 !CBS_get_u32(&identities, &obfuscated_ticket_age) ||
Steven Valdeza833c352016-11-01 13:39:36 -04001984 !CBS_get_u16_length_prefixed(contents, &binders) ||
David Benjaminaedf3032016-12-01 16:47:56 -05001985 CBS_len(&binders) == 0 ||
Steven Valdezaf3b8a92016-11-01 12:49:22 -04001986 CBS_len(contents) != 0) {
Steven Valdeza833c352016-11-01 13:39:36 -04001987 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001988 *out_alert = SSL_AD_DECODE_ERROR;
1989 return 0;
1990 }
1991
Steven Valdeza833c352016-11-01 13:39:36 -04001992 *out_binders = binders;
1993
David Benjaminaedf3032016-12-01 16:47:56 -05001994 /* Check the syntax of the remaining identities, but do not process them. */
1995 size_t num_identities = 1;
1996 while (CBS_len(&identities) != 0) {
1997 CBS unused_ticket;
1998 uint32_t unused_obfuscated_ticket_age;
1999 if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
2000 !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
2001 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2002 *out_alert = SSL_AD_DECODE_ERROR;
2003 return 0;
2004 }
2005
2006 num_identities++;
2007 }
2008
2009 /* Check the syntax of the binders. The value will be checked later if
2010 * resuming. */
2011 size_t num_binders = 0;
2012 while (CBS_len(&binders) != 0) {
2013 CBS binder;
2014 if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
2015 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2016 *out_alert = SSL_AD_DECODE_ERROR;
2017 return 0;
2018 }
2019
2020 num_binders++;
2021 }
2022
2023 if (num_identities != num_binders) {
2024 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
2025 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Steven Valdeza833c352016-11-01 13:39:36 -04002026 return 0;
Steven Valdez5b986082016-09-01 12:29:49 -04002027 }
2028
Steven Valdeza833c352016-11-01 13:39:36 -04002029 /* TODO(svaldez): Check that the ticket_age is valid when attempting to use
2030 * the PSK for 0-RTT. http://crbug.com/boringssl/113 */
2031
Steven Valdez4aa154e2016-07-29 14:32:55 -04002032 /* TLS 1.3 session tickets are renewed separately as part of the
2033 * NewSessionTicket. */
David Benjamine7f60a22016-11-16 18:54:25 +09002034 int unused_renew;
2035 if (!tls_process_ticket(ssl, out_session, &unused_renew, CBS_data(&ticket),
2036 CBS_len(&ticket), NULL, 0)) {
2037 *out_alert = SSL_AD_INTERNAL_ERROR;
2038 return 0;
2039 }
2040
2041 return 1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002042}
2043
2044int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
2045 if (!ssl->s3->session_reused) {
2046 return 1;
2047 }
2048
2049 CBB contents;
2050 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2051 !CBB_add_u16_length_prefixed(out, &contents) ||
2052 /* We only consider the first identity for resumption */
2053 !CBB_add_u16(&contents, 0) ||
2054 !CBB_flush(out)) {
2055 return 0;
2056 }
2057
2058 return 1;
2059}
2060
2061
Steven Valdeza833c352016-11-01 13:39:36 -04002062/* Pre-Shared Key Exchange Modes
2063 *
2064 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002065
Steven Valdeza833c352016-11-01 13:39:36 -04002066static int ext_psk_key_exchange_modes_add_clienthello(SSL *ssl, CBB *out) {
2067 uint16_t min_version, max_version;
2068 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2069 return 0;
2070 }
2071
2072 if (max_version < TLS1_3_VERSION) {
2073 return 1;
2074 }
2075
2076 CBB contents, ke_modes;
2077 if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2078 !CBB_add_u16_length_prefixed(out, &contents) ||
2079 !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2080 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2081 return 0;
2082 }
2083
2084 return CBB_flush(out);
2085}
2086
David Benjamin4eb95cc2016-11-16 17:08:23 +09002087static int ext_psk_key_exchange_modes_parse_clienthello(SSL *ssl,
2088 uint8_t *out_alert,
2089 CBS *contents) {
2090 if (contents == NULL) {
2091 return 1;
2092 }
2093
Steven Valdeza833c352016-11-01 13:39:36 -04002094 CBS ke_modes;
2095 if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
2096 CBS_len(&ke_modes) == 0 ||
2097 CBS_len(contents) != 0) {
2098 *out_alert = SSL_AD_DECODE_ERROR;
2099 return 0;
2100 }
2101
2102 /* We only support tickets with PSK_DHE_KE. */
2103 ssl->s3->hs->accept_psk_mode =
2104 memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, CBS_len(&ke_modes)) != NULL;
2105
2106 return 1;
2107}
2108
2109
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002110/* Early Data Indication
2111 *
2112 * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */
2113
2114static int ext_early_data_add_clienthello(SSL *ssl, CBB *out) {
2115 /* TODO(svaldez): Support 0RTT. */
2116 return 1;
2117}
2118
2119static int ext_early_data_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2120 CBS *contents) {
2121 if (contents == NULL) {
2122 return 1;
2123 }
2124
2125 if (CBS_len(contents) != 0) {
2126 *out_alert = SSL_AD_DECODE_ERROR;
2127 return 0;
2128 }
2129
2130 /* Since we don't currently accept 0-RTT, we have to skip past any early data
2131 * the client might have sent. */
2132 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
2133 ssl->s3->skip_early_data = 1;
2134 }
2135 return 1;
2136}
2137
2138
Steven Valdez143e8b32016-07-11 13:19:03 -04002139/* Key Share
2140 *
David Benjamina128a552016-10-13 14:26:33 -04002141 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 */
Steven Valdez143e8b32016-07-11 13:19:03 -04002142
2143static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
2144 uint16_t min_version, max_version;
2145 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2146 return 0;
2147 }
2148
David Benjamin53a2dfc2016-10-07 21:04:12 -04002149 if (max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002150 return 1;
2151 }
2152
2153 CBB contents, kse_bytes;
2154 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2155 !CBB_add_u16_length_prefixed(out, &contents) ||
2156 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2157 return 0;
2158 }
2159
Steven Valdeza833c352016-11-01 13:39:36 -04002160 uint16_t group_id = ssl->s3->hs->retry_group;
David Benjamin3baa6e12016-10-07 21:10:38 -04002161 if (ssl->s3->hs->received_hello_retry_request) {
Steven Valdeza833c352016-11-01 13:39:36 -04002162 /* We received a HelloRetryRequest without a new curve, so there is no new
2163 * share to append. Leave |ecdh_ctx| as-is. */
2164 if (group_id == 0 &&
2165 !CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
Steven Valdez5440fe02016-07-18 12:40:30 -04002166 ssl->s3->hs->key_share_bytes_len)) {
2167 return 0;
2168 }
2169 OPENSSL_free(ssl->s3->hs->key_share_bytes);
2170 ssl->s3->hs->key_share_bytes = NULL;
David Benjamin4fe3c902016-08-16 02:17:03 -04002171 ssl->s3->hs->key_share_bytes_len = 0;
David Benjamin3baa6e12016-10-07 21:10:38 -04002172 if (group_id == 0) {
2173 return CBB_flush(out);
2174 }
Steven Valdez5440fe02016-07-18 12:40:30 -04002175 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002176 /* Add a fake group. See draft-davidben-tls-grease-01. */
2177 if (ssl->ctx->grease_enabled &&
2178 (!CBB_add_u16(&kse_bytes,
2179 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2180 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2181 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2182 return 0;
2183 }
2184
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002185 /* Predict the most preferred group. */
2186 const uint16_t *groups;
2187 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002188 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002189 if (groups_len == 0) {
2190 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2191 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002192 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002193
2194 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002195 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002196
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002197 CBB key_exchange;
2198 if (!CBB_add_u16(&kse_bytes, group_id) ||
2199 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2200 !SSL_ECDH_CTX_init(&ssl->s3->hs->ecdh_ctx, group_id) ||
2201 !SSL_ECDH_CTX_offer(&ssl->s3->hs->ecdh_ctx, &key_exchange) ||
2202 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002203 return 0;
2204 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002205
David Benjamin3baa6e12016-10-07 21:10:38 -04002206 if (!ssl->s3->hs->received_hello_retry_request) {
Steven Valdez5440fe02016-07-18 12:40:30 -04002207 /* Save the contents of the extension to repeat it in the second
2208 * ClientHello. */
2209 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2210 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2211 CBB_len(&kse_bytes));
2212 if (ssl->s3->hs->key_share_bytes == NULL) {
2213 return 0;
2214 }
2215 }
2216
Steven Valdez143e8b32016-07-11 13:19:03 -04002217 return CBB_flush(out);
2218}
2219
Steven Valdez7259f2f2016-08-02 16:55:05 -04002220int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2221 size_t *out_secret_len,
2222 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002223 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002224 uint16_t group_id;
2225 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002226 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2227 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002228 *out_alert = SSL_AD_DECODE_ERROR;
2229 return 0;
2230 }
2231
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002232 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002233 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2234 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2235 return 0;
2236 }
2237
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002238 if (!SSL_ECDH_CTX_finish(&ssl->s3->hs->ecdh_ctx, out_secret, out_secret_len,
2239 out_alert, CBS_data(&peer_key),
2240 CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002241 *out_alert = SSL_AD_INTERNAL_ERROR;
2242 return 0;
2243 }
2244
David Benjamin5c4e8572016-08-19 17:44:53 -04002245 ssl->s3->new_session->key_exchange_info = group_id;
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002246 SSL_ECDH_CTX_cleanup(&ssl->s3->hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002247 return 1;
2248}
2249
Steven Valdez7259f2f2016-08-02 16:55:05 -04002250int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2251 uint8_t **out_secret,
2252 size_t *out_secret_len,
2253 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002254 uint16_t group_id;
2255 CBS key_shares;
Steven Valdez803c77a2016-09-06 14:13:43 -04002256 if (!tls1_get_shared_group(ssl, &group_id)) {
2257 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2258 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2259 return 0;
2260 }
2261
2262 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002263 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002264 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002265 return 0;
2266 }
2267
David Benjamin7e1f9842016-09-20 19:24:40 -04002268 /* Find the corresponding key share. */
2269 int found = 0;
2270 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002271 while (CBS_len(&key_shares) > 0) {
2272 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002273 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002274 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002275 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2276 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002277 return 0;
2278 }
2279
David Benjamin7e1f9842016-09-20 19:24:40 -04002280 if (id == group_id) {
2281 if (found) {
2282 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2283 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2284 return 0;
2285 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002286
David Benjamin7e1f9842016-09-20 19:24:40 -04002287 found = 1;
2288 peer_key = peer_key_tmp;
2289 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002290 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002291 }
2292
David Benjamin7e1f9842016-09-20 19:24:40 -04002293 if (!found) {
2294 *out_found = 0;
2295 *out_secret = NULL;
2296 *out_secret_len = 0;
2297 return 1;
2298 }
2299
2300 /* Compute the DH secret. */
2301 uint8_t *secret = NULL;
2302 size_t secret_len;
2303 SSL_ECDH_CTX group;
2304 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2305 CBB public_key;
2306 if (!CBB_init(&public_key, 32) ||
2307 !SSL_ECDH_CTX_init(&group, group_id) ||
2308 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len,
2309 out_alert, CBS_data(&peer_key),
2310 CBS_len(&peer_key)) ||
2311 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2312 &ssl->s3->hs->public_key_len)) {
2313 OPENSSL_free(secret);
2314 SSL_ECDH_CTX_cleanup(&group);
2315 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002316 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002317 return 0;
2318 }
2319
2320 SSL_ECDH_CTX_cleanup(&group);
2321
2322 *out_secret = secret;
2323 *out_secret_len = secret_len;
2324 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002325 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002326}
2327
Steven Valdez7259f2f2016-08-02 16:55:05 -04002328int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002329 uint16_t group_id;
2330 CBB kse_bytes, public_key;
2331 if (!tls1_get_shared_group(ssl, &group_id) ||
2332 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2333 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2334 !CBB_add_u16(&kse_bytes, group_id) ||
2335 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2336 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2337 ssl->s3->hs->public_key_len) ||
2338 !CBB_flush(out)) {
2339 return 0;
2340 }
2341
David Benjamin4fe3c902016-08-16 02:17:03 -04002342 OPENSSL_free(ssl->s3->hs->public_key);
2343 ssl->s3->hs->public_key = NULL;
2344 ssl->s3->hs->public_key_len = 0;
2345
David Benjamin5c4e8572016-08-19 17:44:53 -04002346 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002347 return 1;
2348}
2349
2350
Steven Valdezfdd10992016-09-15 16:27:05 -04002351/* Supported Versions
2352 *
2353 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2354
2355static int ext_supported_versions_add_clienthello(SSL *ssl, CBB *out) {
2356 uint16_t min_version, max_version;
2357 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2358 return 0;
2359 }
2360
2361 if (max_version <= TLS1_2_VERSION) {
2362 return 1;
2363 }
2364
2365 CBB contents, versions;
2366 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2367 !CBB_add_u16_length_prefixed(out, &contents) ||
2368 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2369 return 0;
2370 }
2371
David Benjamind9791bf2016-09-27 16:39:52 -04002372 /* Add a fake version. See draft-davidben-tls-grease-01. */
2373 if (ssl->ctx->grease_enabled &&
2374 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2375 return 0;
2376 }
2377
Steven Valdezfdd10992016-09-15 16:27:05 -04002378 for (uint16_t version = max_version; version >= min_version; version--) {
2379 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2380 return 0;
2381 }
2382 }
2383
2384 if (!CBB_flush(out)) {
2385 return 0;
2386 }
2387
2388 return 1;
2389}
2390
2391
David Benjamin3baa6e12016-10-07 21:10:38 -04002392/* Cookie
2393 *
2394 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */
2395
2396static int ext_cookie_add_clienthello(SSL *ssl, CBB *out) {
2397 if (ssl->s3->hs->cookie == NULL) {
2398 return 1;
2399 }
2400
2401 CBB contents, cookie;
2402 if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2403 !CBB_add_u16_length_prefixed(out, &contents) ||
2404 !CBB_add_u16_length_prefixed(&contents, &cookie) ||
2405 !CBB_add_bytes(&cookie, ssl->s3->hs->cookie, ssl->s3->hs->cookie_len) ||
2406 !CBB_flush(out)) {
2407 return 0;
2408 }
2409
2410 /* The cookie is no longer needed in memory. */
2411 OPENSSL_free(ssl->s3->hs->cookie);
2412 ssl->s3->hs->cookie = NULL;
2413 ssl->s3->hs->cookie_len = 0;
2414 return 1;
2415}
2416
2417
Steven Valdezce902a92016-05-17 11:47:53 -04002418/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002419 *
Steven Valdezce902a92016-05-17 11:47:53 -04002420 * https://tools.ietf.org/html/rfc4492#section-5.1.2
David Benjamina128a552016-10-13 14:26:33 -04002421 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 */
Adam Langley273d49c2015-07-20 16:38:52 -07002422
Steven Valdezce902a92016-05-17 11:47:53 -04002423static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Steven Valdezce902a92016-05-17 11:47:53 -04002424 CBB contents, groups_bytes;
2425 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002426 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002427 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002428 return 0;
2429 }
2430
David Benjamin65ac9972016-09-02 21:35:25 -04002431 /* Add a fake group. See draft-davidben-tls-grease-01. */
2432 if (ssl->ctx->grease_enabled &&
2433 !CBB_add_u16(&groups_bytes,
2434 ssl_get_grease_value(ssl, ssl_grease_group))) {
2435 return 0;
2436 }
2437
Steven Valdezce902a92016-05-17 11:47:53 -04002438 const uint16_t *groups;
2439 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002440 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002441
David Benjamin54091232016-09-05 12:47:25 -04002442 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002443 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002444 return 0;
2445 }
2446 }
2447
2448 return CBB_flush(out);
2449}
2450
Steven Valdezce902a92016-05-17 11:47:53 -04002451static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2452 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002453 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2454 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002455 return 1;
2456}
2457
Steven Valdezce902a92016-05-17 11:47:53 -04002458static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2459 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002460 if (contents == NULL) {
2461 return 1;
2462 }
2463
Steven Valdezce902a92016-05-17 11:47:53 -04002464 CBS supported_group_list;
2465 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2466 CBS_len(&supported_group_list) == 0 ||
2467 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002468 CBS_len(contents) != 0) {
2469 return 0;
2470 }
2471
David Benjamin43612b62016-10-07 00:41:50 -04002472 ssl->s3->hs->peer_supported_group_list = OPENSSL_malloc(
Steven Valdezce902a92016-05-17 11:47:53 -04002473 CBS_len(&supported_group_list));
David Benjamin43612b62016-10-07 00:41:50 -04002474 if (ssl->s3->hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002475 *out_alert = SSL_AD_INTERNAL_ERROR;
2476 return 0;
2477 }
2478
Steven Valdezce902a92016-05-17 11:47:53 -04002479 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002480 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002481 if (!CBS_get_u16(&supported_group_list,
David Benjamin43612b62016-10-07 00:41:50 -04002482 &ssl->s3->hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002483 goto err;
2484 }
2485 }
2486
Steven Valdezce902a92016-05-17 11:47:53 -04002487 assert(CBS_len(&supported_group_list) == 0);
David Benjamin43612b62016-10-07 00:41:50 -04002488 ssl->s3->hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002489
2490 return 1;
2491
2492err:
David Benjamin43612b62016-10-07 00:41:50 -04002493 OPENSSL_free(ssl->s3->hs->peer_supported_group_list);
2494 ssl->s3->hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002495 *out_alert = SSL_AD_INTERNAL_ERROR;
2496 return 0;
2497}
2498
Steven Valdezce902a92016-05-17 11:47:53 -04002499static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002500 /* Servers don't echo this extension. */
2501 return 1;
2502}
2503
2504
Adam Langley614c66a2015-06-12 15:26:58 -07002505/* kExtensions contains all the supported extensions. */
2506static const struct tls_extension kExtensions[] = {
2507 {
Adam Langley5021b222015-06-12 18:27:58 -07002508 TLSEXT_TYPE_renegotiate,
2509 NULL,
2510 ext_ri_add_clienthello,
2511 ext_ri_parse_serverhello,
2512 ext_ri_parse_clienthello,
2513 ext_ri_add_serverhello,
2514 },
2515 {
Adam Langley614c66a2015-06-12 15:26:58 -07002516 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002517 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002518 ext_sni_add_clienthello,
2519 ext_sni_parse_serverhello,
2520 ext_sni_parse_clienthello,
2521 ext_sni_add_serverhello,
2522 },
Adam Langley0a056712015-07-01 15:03:33 -07002523 {
2524 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002525 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002526 ext_ems_add_clienthello,
2527 ext_ems_parse_serverhello,
2528 ext_ems_parse_clienthello,
2529 ext_ems_add_serverhello,
2530 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002531 {
2532 TLSEXT_TYPE_session_ticket,
2533 NULL,
2534 ext_ticket_add_clienthello,
2535 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002536 /* Ticket extension client parsing is handled in ssl_session.c */
2537 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002538 ext_ticket_add_serverhello,
2539 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002540 {
2541 TLSEXT_TYPE_signature_algorithms,
2542 NULL,
2543 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002544 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002545 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002546 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002547 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002548 {
2549 TLSEXT_TYPE_status_request,
2550 ext_ocsp_init,
2551 ext_ocsp_add_clienthello,
2552 ext_ocsp_parse_serverhello,
2553 ext_ocsp_parse_clienthello,
2554 ext_ocsp_add_serverhello,
2555 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002556 {
2557 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002558 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002559 ext_npn_add_clienthello,
2560 ext_npn_parse_serverhello,
2561 ext_npn_parse_clienthello,
2562 ext_npn_add_serverhello,
2563 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002564 {
2565 TLSEXT_TYPE_certificate_timestamp,
2566 NULL,
2567 ext_sct_add_clienthello,
2568 ext_sct_parse_serverhello,
2569 ext_sct_parse_clienthello,
2570 ext_sct_add_serverhello,
2571 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002572 {
2573 TLSEXT_TYPE_application_layer_protocol_negotiation,
David Benjamin35598ae2016-11-16 15:38:27 +09002574 NULL,
Adam Langleyf18e4532015-07-10 13:39:53 -07002575 ext_alpn_add_clienthello,
2576 ext_alpn_parse_serverhello,
David Benjamin9ef31f02016-10-31 18:01:13 -04002577 /* ALPN is negotiated late in |ssl_negotiate_alpn|. */
2578 ignore_parse_clienthello,
Adam Langleyf18e4532015-07-10 13:39:53 -07002579 ext_alpn_add_serverhello,
2580 },
Adam Langley49c7af12015-07-10 14:33:46 -07002581 {
2582 TLSEXT_TYPE_channel_id,
2583 ext_channel_id_init,
2584 ext_channel_id_add_clienthello,
2585 ext_channel_id_parse_serverhello,
2586 ext_channel_id_parse_clienthello,
2587 ext_channel_id_add_serverhello,
2588 },
Adam Langley391250d2015-07-15 19:06:07 -07002589 {
2590 TLSEXT_TYPE_srtp,
2591 ext_srtp_init,
2592 ext_srtp_add_clienthello,
2593 ext_srtp_parse_serverhello,
2594 ext_srtp_parse_clienthello,
2595 ext_srtp_add_serverhello,
2596 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002597 {
2598 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002599 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002600 ext_ec_point_add_clienthello,
2601 ext_ec_point_parse_serverhello,
2602 ext_ec_point_parse_clienthello,
2603 ext_ec_point_add_serverhello,
2604 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002605 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002606 TLSEXT_TYPE_key_share,
2607 NULL,
2608 ext_key_share_add_clienthello,
2609 forbid_parse_serverhello,
2610 ignore_parse_clienthello,
2611 dont_add_serverhello,
2612 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002613 {
Steven Valdeza833c352016-11-01 13:39:36 -04002614 TLSEXT_TYPE_psk_key_exchange_modes,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002615 NULL,
Steven Valdeza833c352016-11-01 13:39:36 -04002616 ext_psk_key_exchange_modes_add_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002617 forbid_parse_serverhello,
David Benjamin4eb95cc2016-11-16 17:08:23 +09002618 ext_psk_key_exchange_modes_parse_clienthello,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002619 dont_add_serverhello,
2620 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002621 {
Steven Valdeza4ee74d2016-11-29 13:36:45 -05002622 TLSEXT_TYPE_early_data,
2623 NULL,
2624 ext_early_data_add_clienthello,
2625 forbid_parse_serverhello,
2626 ext_early_data_parse_clienthello,
2627 dont_add_serverhello,
2628 },
2629 {
Steven Valdezfdd10992016-09-15 16:27:05 -04002630 TLSEXT_TYPE_supported_versions,
2631 NULL,
2632 ext_supported_versions_add_clienthello,
2633 forbid_parse_serverhello,
2634 ignore_parse_clienthello,
2635 dont_add_serverhello,
2636 },
David Benjamin3baa6e12016-10-07 21:10:38 -04002637 {
2638 TLSEXT_TYPE_cookie,
2639 NULL,
2640 ext_cookie_add_clienthello,
2641 forbid_parse_serverhello,
2642 ignore_parse_clienthello,
2643 dont_add_serverhello,
2644 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002645 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2646 * intolerant to the last extension being zero-length. See
2647 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002648 {
Steven Valdezce902a92016-05-17 11:47:53 -04002649 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002650 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002651 ext_supported_groups_add_clienthello,
2652 ext_supported_groups_parse_serverhello,
2653 ext_supported_groups_parse_clienthello,
2654 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002655 },
Adam Langley614c66a2015-06-12 15:26:58 -07002656};
2657
2658#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2659
Adam Langley4cfa96b2015-07-01 11:56:55 -07002660OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002661 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002662 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002663OPENSSL_COMPILE_ASSERT(
2664 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2665 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002666
Adam Langley614c66a2015-06-12 15:26:58 -07002667static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2668 uint16_t value) {
2669 unsigned i;
2670 for (i = 0; i < kNumExtensions; i++) {
2671 if (kExtensions[i].value == value) {
2672 *out_index = i;
2673 return &kExtensions[i];
2674 }
2675 }
2676
2677 return NULL;
2678}
2679
Adam Langley09505632015-07-30 18:10:13 -07002680int SSL_extension_supported(unsigned extension_value) {
2681 uint32_t index;
2682 return extension_value == TLSEXT_TYPE_padding ||
2683 tls_extension_find(&index, extension_value) != NULL;
2684}
2685
David Benjamine8d53502015-10-10 14:13:23 -04002686int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002687 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002688 if (ssl->client_version == SSL3_VERSION &&
2689 !ssl->s3->send_connection_binding) {
2690 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002691 }
Adam Langley95c29f32014-06-20 12:00:00 -07002692
David Benjamine8d53502015-10-10 14:13:23 -04002693 CBB extensions;
2694 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002695 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002696 }
Adam Langley95c29f32014-06-20 12:00:00 -07002697
David Benjaminf5d2cd02016-10-06 19:39:20 -04002698 ssl->s3->hs->extensions.sent = 0;
2699 ssl->s3->hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002700
David Benjamin54091232016-09-05 12:47:25 -04002701 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002702 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002703 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002704 }
2705 }
Adam Langley95c29f32014-06-20 12:00:00 -07002706
David Benjamin65ac9972016-09-02 21:35:25 -04002707 uint16_t grease_ext1 = 0;
2708 if (ssl->ctx->grease_enabled) {
2709 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2710 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2711 if (!CBB_add_u16(&extensions, grease_ext1) ||
2712 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2713 goto err;
2714 }
2715 }
2716
David Benjamin54091232016-09-05 12:47:25 -04002717 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002718 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002719 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002720 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002721 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002722 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002723 }
Adam Langley95c29f32014-06-20 12:00:00 -07002724
Adam Langley33ad2b52015-07-20 17:43:53 -07002725 if (CBB_len(&extensions) != len_before) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002726 ssl->s3->hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002727 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002728 }
Adam Langley75712922014-10-10 16:23:43 -07002729
David Benjamine8d53502015-10-10 14:13:23 -04002730 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002731 goto err;
2732 }
2733
David Benjamin65ac9972016-09-02 21:35:25 -04002734 if (ssl->ctx->grease_enabled) {
2735 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2736 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2737
2738 /* The two fake extensions must not have the same value. GREASE values are
2739 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2740 * one. */
2741 if (grease_ext1 == grease_ext2) {
2742 grease_ext2 ^= 0x1010;
2743 }
2744
2745 if (!CBB_add_u16(&extensions, grease_ext2) ||
2746 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2747 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2748 goto err;
2749 }
2750 }
2751
David Benjamince079fd2016-08-02 16:22:34 -04002752 if (!SSL_is_dtls(ssl)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002753 size_t psk_extension_len = ext_pre_shared_key_clienthello_length(ssl);
2754 header_len += 2 + CBB_len(&extensions) + psk_extension_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002755 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002756 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002757 *
2758 * NB: because this code works out the length of all existing extensions
2759 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002760 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002761 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002762 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002763 * Server 7.0 is intolerant to the last extension being zero-length. See
2764 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002765 if (padding_len >= 4 + 1) {
2766 padding_len -= 4;
2767 } else {
2768 padding_len = 1;
2769 }
Adam Langley95c29f32014-06-20 12:00:00 -07002770
Adam Langley33ad2b52015-07-20 17:43:53 -07002771 uint8_t *padding_bytes;
2772 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2773 !CBB_add_u16(&extensions, padding_len) ||
2774 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2775 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002776 }
Adam Langley75712922014-10-10 16:23:43 -07002777
Adam Langley33ad2b52015-07-20 17:43:53 -07002778 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002779 }
2780 }
Adam Langley75712922014-10-10 16:23:43 -07002781
Steven Valdeza833c352016-11-01 13:39:36 -04002782 /* The PSK extension must be last, including after the padding. */
David Benjamin12d6baf2016-11-17 14:31:41 +09002783 if (!ext_pre_shared_key_add_clienthello(ssl, &extensions)) {
Steven Valdeza833c352016-11-01 13:39:36 -04002784 goto err;
2785 }
2786
David Benjamina01deee2015-12-08 18:56:31 -05002787 /* Discard empty extensions blocks. */
2788 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002789 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002790 }
2791
David Benjamine8d53502015-10-10 14:13:23 -04002792 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002793
2794err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002795 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002796 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002797}
Adam Langley95c29f32014-06-20 12:00:00 -07002798
David Benjamin56380462015-10-10 14:59:09 -04002799int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002800 CBB extensions;
2801 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002802 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002803 }
2804
2805 unsigned i;
2806 for (i = 0; i < kNumExtensions; i++) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002807 if (!(ssl->s3->hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002808 /* Don't send extensions that were not received. */
2809 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002810 }
Adam Langley95c29f32014-06-20 12:00:00 -07002811
David Benjamin56380462015-10-10 14:59:09 -04002812 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002813 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002814 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley33ad2b52015-07-20 17:43:53 -07002815 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002816 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002817 }
Adam Langley95c29f32014-06-20 12:00:00 -07002818
David Benjamin56380462015-10-10 14:59:09 -04002819 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002820 goto err;
2821 }
2822
Steven Valdez143e8b32016-07-11 13:19:03 -04002823 /* Discard empty extensions blocks before TLS 1.3. */
2824 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2825 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002826 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002827 }
2828
David Benjamin56380462015-10-10 14:59:09 -04002829 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002830
2831err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002832 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002833 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002834}
Adam Langley95c29f32014-06-20 12:00:00 -07002835
David Benjamine14ff062016-08-09 16:21:24 -04002836static int ssl_scan_clienthello_tlsext(
2837 SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
2838 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002839 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002840 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002841 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002842 }
2843 }
2844
David Benjaminf5d2cd02016-10-06 19:39:20 -04002845 ssl->s3->hs->extensions.received = 0;
2846 ssl->s3->hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002847
David Benjamine14ff062016-08-09 16:21:24 -04002848 CBS extensions;
2849 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2850 while (CBS_len(&extensions) != 0) {
2851 uint16_t type;
2852 CBS extension;
2853
2854 /* Decode the next extension. */
2855 if (!CBS_get_u16(&extensions, &type) ||
2856 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002857 *out_alert = SSL_AD_DECODE_ERROR;
2858 return 0;
2859 }
Adam Langley95c29f32014-06-20 12:00:00 -07002860
David Benjamine14ff062016-08-09 16:21:24 -04002861 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2862 * ambiguous. Ignore all but the renegotiation_info extension. */
2863 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2864 continue;
2865 }
Adam Langley95c29f32014-06-20 12:00:00 -07002866
David Benjamine14ff062016-08-09 16:21:24 -04002867 unsigned ext_index;
2868 const struct tls_extension *const ext =
2869 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002870
David Benjamine14ff062016-08-09 16:21:24 -04002871 if (ext == NULL) {
2872 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002873 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002874 return 0;
2875 }
David Benjamine14ff062016-08-09 16:21:24 -04002876 continue;
2877 }
2878
David Benjaminf5d2cd02016-10-06 19:39:20 -04002879 ssl->s3->hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002880 uint8_t alert = SSL_AD_DECODE_ERROR;
2881 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2882 *out_alert = alert;
2883 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002884 ERR_add_error_dataf("extension %u", (unsigned)type);
David Benjamine14ff062016-08-09 16:21:24 -04002885 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002886 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002887 }
Adam Langley75712922014-10-10 16:23:43 -07002888
David Benjamin1deb41b2016-08-09 19:36:38 -04002889 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002890 if (ssl->s3->hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002891 continue;
2892 }
2893
2894 CBS *contents = NULL, fake_contents;
2895 static const uint8_t kFakeRenegotiateExtension[] = {0};
2896 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2897 ssl_client_cipher_list_contains_cipher(client_hello,
2898 SSL3_CK_SCSV & 0xffff)) {
2899 /* The renegotiation SCSV was received so pretend that we received a
2900 * renegotiation extension. */
2901 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2902 sizeof(kFakeRenegotiateExtension));
2903 contents = &fake_contents;
David Benjaminf5d2cd02016-10-06 19:39:20 -04002904 ssl->s3->hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002905 }
2906
2907 /* Extension wasn't observed so call the callback with a NULL
2908 * parameter. */
2909 uint8_t alert = SSL_AD_DECODE_ERROR;
2910 if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
2911 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002912 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
David Benjamin1deb41b2016-08-09 19:36:38 -04002913 *out_alert = alert;
2914 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002915 }
2916 }
2917
Adam Langleyfcf25832014-12-18 17:42:32 -08002918 return 1;
2919}
Adam Langley95c29f32014-06-20 12:00:00 -07002920
David Benjamine14ff062016-08-09 16:21:24 -04002921int ssl_parse_clienthello_tlsext(
2922 SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002923 int alert = -1;
David Benjamine14ff062016-08-09 16:21:24 -04002924 if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002925 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002926 return 0;
2927 }
Adam Langley95c29f32014-06-20 12:00:00 -07002928
David Benjamin0d56f882015-12-19 17:05:56 -05002929 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002930 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002931 return 0;
2932 }
Adam Langley95c29f32014-06-20 12:00:00 -07002933
Adam Langleyfcf25832014-12-18 17:42:32 -08002934 return 1;
2935}
Adam Langley95c29f32014-06-20 12:00:00 -07002936
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002937OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2938
David Benjamin0d56f882015-12-19 17:05:56 -05002939static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002940 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2941 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2942 return 1;
2943 }
Adam Langley614c66a2015-06-12 15:26:58 -07002944
Steven Valdez143e8b32016-07-11 13:19:03 -04002945 /* Decode the extensions block and check it is valid. */
2946 CBS extensions;
2947 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2948 !tls1_check_duplicate_extensions(&extensions)) {
2949 *out_alert = SSL_AD_DECODE_ERROR;
2950 return 0;
2951 }
2952
2953 uint32_t received = 0;
2954 while (CBS_len(&extensions) != 0) {
2955 uint16_t type;
2956 CBS extension;
2957
2958 /* Decode the next extension. */
2959 if (!CBS_get_u16(&extensions, &type) ||
2960 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002961 *out_alert = SSL_AD_DECODE_ERROR;
2962 return 0;
2963 }
Adam Langley95c29f32014-06-20 12:00:00 -07002964
Steven Valdez143e8b32016-07-11 13:19:03 -04002965 unsigned ext_index;
2966 const struct tls_extension *const ext =
2967 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002968
Steven Valdez143e8b32016-07-11 13:19:03 -04002969 if (ext == NULL) {
2970 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002971 return 0;
2972 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002973 continue;
2974 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002975
David Benjaminf5d2cd02016-10-06 19:39:20 -04002976 if (!(ssl->s3->hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002977 type != TLSEXT_TYPE_renegotiate) {
2978 /* If the extension was never sent then it is illegal, except for the
2979 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002980 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2981 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002982 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002983 return 0;
2984 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002985
Steven Valdez143e8b32016-07-11 13:19:03 -04002986 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002987
Steven Valdez143e8b32016-07-11 13:19:03 -04002988 uint8_t alert = SSL_AD_DECODE_ERROR;
2989 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2990 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08002991 ERR_add_error_dataf("extension %u", (unsigned)type);
Steven Valdez143e8b32016-07-11 13:19:03 -04002992 *out_alert = alert;
2993 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002994 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002995 }
Adam Langley95c29f32014-06-20 12:00:00 -07002996
David Benjamin54091232016-09-05 12:47:25 -04002997 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002998 if (!(received & (1u << i))) {
2999 /* Extension wasn't observed so call the callback with a NULL
3000 * parameter. */
3001 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05003002 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07003003 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -08003004 ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07003005 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08003006 return 0;
3007 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003008 }
3009 }
Adam Langley95c29f32014-06-20 12:00:00 -07003010
Adam Langleyfcf25832014-12-18 17:42:32 -08003011 return 1;
3012}
Adam Langley95c29f32014-06-20 12:00:00 -07003013
David Benjamin0d56f882015-12-19 17:05:56 -05003014static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003015 int ret = SSL_TLSEXT_ERR_NOACK;
3016 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07003017
David Benjamin78f8aab2016-03-10 16:33:58 -05003018 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003019 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003020 ssl->ctx->tlsext_servername_arg);
3021 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003022 ret = ssl->initial_ctx->tlsext_servername_callback(
3023 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003024 }
Adam Langley95c29f32014-06-20 12:00:00 -07003025
Adam Langleyfcf25832014-12-18 17:42:32 -08003026 switch (ret) {
3027 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003028 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003029 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003030
Adam Langleyfcf25832014-12-18 17:42:32 -08003031 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003032 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003033 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003034
Adam Langleyfcf25832014-12-18 17:42:32 -08003035 case SSL_TLSEXT_ERR_NOACK:
David Benjamina0486782016-10-06 19:11:32 -04003036 ssl->s3->hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003037 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07003038
Adam Langleyfcf25832014-12-18 17:42:32 -08003039 default:
3040 return 1;
3041 }
3042}
Adam Langleyed8270a2014-09-02 13:52:56 -07003043
David Benjamin0d56f882015-12-19 17:05:56 -05003044static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04003045 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08003046 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07003047
David Benjamin78f8aab2016-03-10 16:33:58 -05003048 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003049 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05003050 ssl->ctx->tlsext_servername_arg);
3051 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05003052 ret = ssl->initial_ctx->tlsext_servername_callback(
3053 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08003054 }
Adam Langley95c29f32014-06-20 12:00:00 -07003055
Adam Langleyfcf25832014-12-18 17:42:32 -08003056 switch (ret) {
3057 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003058 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003059 return -1;
David Benjamin03973092014-06-24 23:27:17 -04003060
Adam Langleyfcf25832014-12-18 17:42:32 -08003061 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003062 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003063 return 1;
3064
3065 default:
3066 return 1;
3067 }
3068}
3069
David Benjamin0d56f882015-12-19 17:05:56 -05003070int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003071 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05003072 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
3073 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003074 return 0;
3075 }
3076
David Benjamin0d56f882015-12-19 17:05:56 -05003077 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04003078 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08003079 return 0;
3080 }
3081
3082 return 1;
3083}
Adam Langley95c29f32014-06-20 12:00:00 -07003084
David Benjamine3aa1d92015-06-16 15:34:50 -04003085int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05003086 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04003087 size_t ticket_len, const uint8_t *session_id,
3088 size_t session_id_len) {
3089 int ret = 1; /* Most errors are non-fatal. */
3090 SSL_CTX *ssl_ctx = ssl->initial_ctx;
3091 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003092
David Benjamine3aa1d92015-06-16 15:34:50 -04003093 HMAC_CTX hmac_ctx;
3094 HMAC_CTX_init(&hmac_ctx);
3095 EVP_CIPHER_CTX cipher_ctx;
3096 EVP_CIPHER_CTX_init(&cipher_ctx);
3097
David Benjaminef1b0092015-11-21 14:05:44 -05003098 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04003099 *out_session = NULL;
3100
Steven Valdez4aa154e2016-07-29 14:32:55 -04003101 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
3102 goto done;
3103 }
3104
David Benjamine3aa1d92015-06-16 15:34:50 -04003105 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3106 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003107 }
3108
David Benjaminadcc3952015-04-26 13:07:57 -04003109 /* Ensure there is room for the key name and the largest IV
3110 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3111 * the maximum IV length should be well under the minimum size for the
3112 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003113 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3114 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003115 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003116 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003117
David Benjamine3aa1d92015-06-16 15:34:50 -04003118 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003119 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3120 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3121 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003122 if (cb_ret < 0) {
3123 ret = 0;
3124 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003125 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003126 if (cb_ret == 0) {
3127 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003128 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003129 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003130 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003131 }
3132 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003133 /* Check the key name matches. */
3134 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3135 SSL_TICKET_KEY_NAME_LEN) != 0) {
3136 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003137 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003138 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3139 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003140 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003141 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3142 ssl_ctx->tlsext_tick_aes_key, iv)) {
3143 ret = 0;
3144 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003145 }
3146 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003147 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003148
David Benjamine3aa1d92015-06-16 15:34:50 -04003149 /* Check the MAC at the end of the ticket. */
3150 uint8_t mac[EVP_MAX_MD_SIZE];
3151 size_t mac_len = HMAC_size(&hmac_ctx);
3152 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003153 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003154 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003155 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003156 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3157 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003158 int mac_ok =
3159 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3160#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3161 mac_ok = 1;
3162#endif
3163 if (!mac_ok) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003164 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003165 }
3166
David Benjamine3aa1d92015-06-16 15:34:50 -04003167 /* Decrypt the session data. */
3168 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3169 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3170 mac_len;
3171 plaintext = OPENSSL_malloc(ciphertext_len);
3172 if (plaintext == NULL) {
3173 ret = 0;
3174 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003175 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003176 size_t plaintext_len;
3177#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3178 memcpy(plaintext, ciphertext, ciphertext_len);
3179 plaintext_len = ciphertext_len;
3180#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003181 if (ciphertext_len >= INT_MAX) {
3182 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003183 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003184 int len1, len2;
3185 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3186 (int)ciphertext_len) ||
3187 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
3188 ERR_clear_error(); /* Don't leave an error on the queue. */
3189 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003190 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003191 plaintext_len = (size_t)(len1 + len2);
3192#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003193
David Benjamine3aa1d92015-06-16 15:34:50 -04003194 /* Decode the session. */
David Benjaminfbc45d72016-09-22 01:21:24 -04003195 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003196 if (session == NULL) {
3197 ERR_clear_error(); /* Don't leave an error on the queue. */
3198 goto done;
3199 }
3200
3201 /* Copy the client's session ID into the new session, to denote the ticket has
3202 * been accepted. */
3203 memcpy(session->session_id, session_id, session_id_len);
3204 session->session_id_length = session_id_len;
3205
3206 *out_session = session;
3207
3208done:
3209 OPENSSL_free(plaintext);
3210 HMAC_CTX_cleanup(&hmac_ctx);
3211 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3212 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003213}
Adam Langley95c29f32014-06-20 12:00:00 -07003214
Steven Valdez0d62f262015-09-04 12:41:04 -04003215int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003216 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05003217 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003218 return 1;
3219 }
David Benjamincd996942014-07-20 16:23:51 -04003220
David Benjamin0fc37ef2016-08-17 15:29:46 -04003221 SSL_HANDSHAKE *hs = ssl->s3->hs;
3222 OPENSSL_free(hs->peer_sigalgs);
3223 hs->peer_sigalgs = NULL;
3224 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003225
3226 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003227 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003228 return 0;
3229 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003230 num_sigalgs /= 2;
3231
3232 /* supported_signature_algorithms in the certificate request is
3233 * allowed to be empty. */
3234 if (num_sigalgs == 0) {
3235 return 1;
3236 }
3237
Steven Valdez02563852016-06-23 13:33:05 -04003238 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3239 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003240 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3241 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003242 return 0;
3243 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003244 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003245
3246 CBS sigalgs;
3247 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003248 for (size_t i = 0; i < num_sigalgs; i++) {
3249 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003250 return 0;
3251 }
3252 }
Adam Langley95c29f32014-06-20 12:00:00 -07003253
Adam Langleyfcf25832014-12-18 17:42:32 -08003254 return 1;
3255}
David Benjaminec2f27d2014-11-13 19:17:25 -05003256
David Benjaminea9a0d52016-07-08 15:52:59 -07003257int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003258 CERT *cert = ssl->cert;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003259 SSL_HANDSHAKE *hs = ssl->s3->hs;
David Benjaminec2f27d2014-11-13 19:17:25 -05003260
Steven Valdezf0451ca2016-06-29 13:16:27 -04003261 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3262 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3263 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003264 int type = ssl_private_key_type(ssl);
3265 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003266 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003267 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003268 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003269 if (ssl_is_ecdsa_key_type(type)) {
3270 *out = SSL_SIGN_ECDSA_SHA1;
3271 return 1;
3272 }
3273 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3274 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003275 }
3276
David Benjamin3ef76972016-10-17 17:59:54 -04003277 const uint16_t *sigalgs = cert->sigalgs;
3278 size_t num_sigalgs = cert->num_sigalgs;
3279 if (sigalgs == NULL) {
3280 sigalgs = kSignSignatureAlgorithms;
3281 num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
Steven Valdez0d62f262015-09-04 12:41:04 -04003282 }
3283
David Benjamin0fc37ef2016-08-17 15:29:46 -04003284 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3285 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3286 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003287 /* If the client didn't specify any signature_algorithms extension then
3288 * we can assume that it supports SHA1. See
3289 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3290 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3291 SSL_SIGN_ECDSA_SHA1};
3292 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003293 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003294 }
3295
David Benjamin0fc37ef2016-08-17 15:29:46 -04003296 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003297 uint16_t sigalg = sigalgs[i];
3298 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3299 * negotiated. */
3300 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3301 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3302 continue;
3303 }
3304
David Benjamin0fc37ef2016-08-17 15:29:46 -04003305 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003306 if (sigalg == peer_sigalgs[j]) {
3307 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003308 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003309 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003310 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003311 }
Adam Langley95c29f32014-06-20 12:00:00 -07003312
David Benjaminea9a0d52016-07-08 15:52:59 -07003313 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3314 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003315}
Adam Langley95c29f32014-06-20 12:00:00 -07003316
Nick Harper60a85cb2016-09-23 16:25:11 -07003317int tls1_verify_channel_id(SSL *ssl) {
3318 int ret = 0;
3319 uint16_t extension_type;
3320 CBS extension, channel_id;
3321
3322 /* A Channel ID handshake message is structured to contain multiple
3323 * extensions, but the only one that can be present is Channel ID. */
3324 CBS_init(&channel_id, ssl->init_msg, ssl->init_num);
3325 if (!CBS_get_u16(&channel_id, &extension_type) ||
3326 !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
3327 CBS_len(&channel_id) != 0 ||
3328 extension_type != TLSEXT_TYPE_channel_id ||
3329 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
3330 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
3331 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
3332 return 0;
3333 }
3334
3335 EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
3336 if (!p256) {
3337 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
3338 return 0;
3339 }
3340
3341 EC_KEY *key = NULL;
3342 EC_POINT *point = NULL;
3343 BIGNUM x, y;
3344 ECDSA_SIG sig;
3345 BN_init(&x);
3346 BN_init(&y);
3347 sig.r = BN_new();
3348 sig.s = BN_new();
3349 if (sig.r == NULL || sig.s == NULL) {
3350 goto err;
3351 }
3352
3353 const uint8_t *p = CBS_data(&extension);
3354 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
3355 BN_bin2bn(p + 32, 32, &y) == NULL ||
3356 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
3357 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
3358 goto err;
3359 }
3360
3361 point = EC_POINT_new(p256);
3362 if (point == NULL ||
3363 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
3364 goto err;
3365 }
3366
3367 key = EC_KEY_new();
3368 if (key == NULL ||
3369 !EC_KEY_set_group(key, p256) ||
3370 !EC_KEY_set_public_key(key, point)) {
3371 goto err;
3372 }
3373
3374 uint8_t digest[EVP_MAX_MD_SIZE];
3375 size_t digest_len;
3376 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3377 goto err;
3378 }
3379
3380 int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key);
3381#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3382 sig_ok = 1;
3383#endif
3384 if (!sig_ok) {
3385 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
3386 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
3387 ssl->s3->tlsext_channel_id_valid = 0;
3388 goto err;
3389 }
3390
3391 memcpy(ssl->s3->tlsext_channel_id, p, 64);
3392 ret = 1;
3393
3394err:
3395 BN_free(&x);
3396 BN_free(&y);
3397 BN_free(sig.r);
3398 BN_free(sig.s);
3399 EC_KEY_free(key);
3400 EC_POINT_free(point);
3401 EC_GROUP_free(p256);
3402 return ret;
3403}
3404
3405int tls1_write_channel_id(SSL *ssl, CBB *cbb) {
3406 uint8_t digest[EVP_MAX_MD_SIZE];
3407 size_t digest_len;
3408 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
3409 return 0;
3410 }
3411
3412 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
3413 if (ec_key == NULL) {
3414 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3415 return 0;
3416 }
3417
3418 int ret = 0;
3419 BIGNUM *x = BN_new();
3420 BIGNUM *y = BN_new();
3421 ECDSA_SIG *sig = NULL;
3422 if (x == NULL || y == NULL ||
3423 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
3424 EC_KEY_get0_public_key(ec_key),
3425 x, y, NULL)) {
3426 goto err;
3427 }
3428
3429 sig = ECDSA_do_sign(digest, digest_len, ec_key);
3430 if (sig == NULL) {
3431 goto err;
3432 }
3433
3434 CBB child;
3435 if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
3436 !CBB_add_u16_length_prefixed(cbb, &child) ||
3437 !BN_bn2cbb_padded(&child, 32, x) ||
3438 !BN_bn2cbb_padded(&child, 32, y) ||
3439 !BN_bn2cbb_padded(&child, 32, sig->r) ||
3440 !BN_bn2cbb_padded(&child, 32, sig->s) ||
3441 !CBB_flush(cbb)) {
3442 goto err;
3443 }
3444
3445 ret = 1;
3446
3447err:
3448 BN_free(x);
3449 BN_free(y);
3450 ECDSA_SIG_free(sig);
3451 return ret;
3452}
3453
David Benjamind6a4ae92015-08-06 11:10:51 -04003454int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
Nick Harper60a85cb2016-09-23 16:25:11 -07003455 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
3456 uint8_t *msg;
3457 size_t msg_len;
3458 if (!tls13_get_cert_verify_signature_input(ssl, &msg, &msg_len,
3459 ssl_cert_verify_channel_id)) {
3460 return 0;
3461 }
3462 SHA256(msg, msg_len, out);
3463 *out_len = SHA256_DIGEST_LENGTH;
3464 OPENSSL_free(msg);
3465 return 1;
3466 }
3467
Nick Harper95594012016-10-20 14:07:13 -07003468 SHA256_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003469
Nick Harper95594012016-10-20 14:07:13 -07003470 SHA256_Init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003471 static const char kClientIDMagic[] = "TLS Channel ID signature";
Nick Harper95594012016-10-20 14:07:13 -07003472 SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003473
Steven Valdez87eab492016-06-27 16:34:59 -04003474 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003475 static const char kResumptionMagic[] = "Resumption";
Nick Harper95594012016-10-20 14:07:13 -07003476 SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
David Benjamind6a4ae92015-08-06 11:10:51 -04003477 if (ssl->session->original_handshake_hash_len == 0) {
3478 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper95594012016-10-20 14:07:13 -07003479 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003480 }
Nick Harper95594012016-10-20 14:07:13 -07003481 SHA256_Update(&ctx, ssl->session->original_handshake_hash,
3482 ssl->session->original_handshake_hash_len);
David Benjamind6a4ae92015-08-06 11:10:51 -04003483 }
3484
3485 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3486 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3487 sizeof(handshake_hash));
3488 if (handshake_hash_len < 0) {
Nick Harper95594012016-10-20 14:07:13 -07003489 return 0;
David Benjamind6a4ae92015-08-06 11:10:51 -04003490 }
Nick Harper95594012016-10-20 14:07:13 -07003491 SHA256_Update(&ctx, handshake_hash, (size_t)handshake_hash_len);
3492 SHA256_Final(out, &ctx);
3493 *out_len = SHA256_DIGEST_LENGTH;
3494 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003495}
Adam Langley1258b6a2014-06-20 12:00:00 -07003496
3497/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003498 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003499 * data. */
3500int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003501 int digest_len;
3502 /* This function should never be called for a resumed session because the
3503 * handshake hashes that we wish to record are for the original, full
3504 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003505 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003506 return -1;
3507 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003508
Adam Langleyfcf25832014-12-18 17:42:32 -08003509 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003510 tls1_handshake_digest(
3511 ssl, ssl->s3->new_session->original_handshake_hash,
3512 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003513 if (digest_len < 0) {
3514 return -1;
3515 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003516
Steven Valdez87eab492016-06-27 16:34:59 -04003517 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003518
Adam Langleyfcf25832014-12-18 17:42:32 -08003519 return 1;
3520}
Nick Harper60a85cb2016-09-23 16:25:11 -07003521
3522int ssl_do_channel_id_callback(SSL *ssl) {
3523 if (ssl->tlsext_channel_id_private != NULL ||
3524 ssl->ctx->channel_id_cb == NULL) {
3525 return 1;
3526 }
3527
3528 EVP_PKEY *key = NULL;
3529 ssl->ctx->channel_id_cb(ssl, &key);
3530 if (key == NULL) {
3531 /* The caller should try again later. */
3532 return 1;
3533 }
3534
3535 int ret = SSL_set1_tls_channel_id(ssl, key);
3536 EVP_PKEY_free(key);
3537 return ret;
3538}
Adam Langleycfa08c32016-11-17 13:21:27 -08003539
3540int ssl_is_sct_list_valid(const CBS *contents) {
3541 /* Shallow parse the SCT list for sanity. By the RFC
3542 * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
3543 * of the SCTs may be empty. */
3544 CBS copy = *contents;
3545 CBS sct_list;
3546 if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
3547 CBS_len(&copy) != 0 ||
3548 CBS_len(&sct_list) == 0) {
3549 return 0;
3550 }
3551
3552 while (CBS_len(&sct_list) > 0) {
3553 CBS sct;
3554 if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
3555 CBS_len(&sct) == 0) {
3556 return 0;
3557 }
3558 }
3559
3560 return 1;
3561}