blob: 02c713ba7509f16ff847edb6becf73c6a0de2361 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin03973092014-06-24 23:27:17 -0400116#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400117#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/evp.h>
120#include <openssl/hmac.h>
121#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400122#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/rand.h>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700124#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400127#include "../crypto/internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800128
129
David Benjamin0d56f882015-12-19 17:05:56 -0500130static int ssl_check_clienthello_tlsext(SSL *ssl);
131static int ssl_check_serverhello_tlsext(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700132
Adam Langleyfcf25832014-12-18 17:42:32 -0800133static int compare_uint16_t(const void *p1, const void *p2) {
134 uint16_t u1 = *((const uint16_t *)p1);
135 uint16_t u2 = *((const uint16_t *)p2);
136 if (u1 < u2) {
137 return -1;
138 } else if (u1 > u2) {
139 return 1;
140 } else {
141 return 0;
142 }
143}
David Benjamin35a7a442014-07-05 00:23:20 -0400144
Adam Langleyfcf25832014-12-18 17:42:32 -0800145/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
146 * more than one extension of the same type in a ClientHello or ServerHello.
147 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400148 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800149static int tls1_check_duplicate_extensions(const CBS *cbs) {
150 CBS extensions = *cbs;
151 size_t num_extensions = 0, i = 0;
152 uint16_t *extension_types = NULL;
153 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400154
Adam Langleyfcf25832014-12-18 17:42:32 -0800155 /* First pass: count the extensions. */
156 while (CBS_len(&extensions) > 0) {
157 uint16_t type;
158 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400159
Adam Langleyfcf25832014-12-18 17:42:32 -0800160 if (!CBS_get_u16(&extensions, &type) ||
161 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
162 goto done;
163 }
David Benjamin35a7a442014-07-05 00:23:20 -0400164
Adam Langleyfcf25832014-12-18 17:42:32 -0800165 num_extensions++;
166 }
David Benjamin35a7a442014-07-05 00:23:20 -0400167
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 if (num_extensions == 0) {
169 return 1;
170 }
David Benjamin9a373592014-07-25 04:27:53 -0400171
Brian Smith5ba06892016-02-07 09:36:04 -1000172 extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400174 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800175 goto done;
176 }
David Benjamin35a7a442014-07-05 00:23:20 -0400177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 /* Second pass: gather the extension types. */
179 extensions = *cbs;
180 for (i = 0; i < num_extensions; i++) {
181 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400182
Adam Langleyfcf25832014-12-18 17:42:32 -0800183 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
184 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
185 /* This should not happen. */
186 goto done;
187 }
188 }
189 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400190
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 /* Sort the extensions and make sure there are no duplicates. */
192 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
193 for (i = 1; i < num_extensions; i++) {
194 if (extension_types[i - 1] == extension_types[i]) {
195 goto done;
196 }
197 }
David Benjamin35a7a442014-07-05 00:23:20 -0400198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 ret = 1;
200
David Benjamin35a7a442014-07-05 00:23:20 -0400201done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400202 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 return ret;
204}
David Benjamin35a7a442014-07-05 00:23:20 -0400205
David Benjamind7573dc2016-07-20 19:05:22 +0200206int ssl_early_callback_init(SSL *ssl, struct ssl_early_callback_ctx *ctx,
207 const uint8_t *in, size_t in_len) {
208 memset(ctx, 0, sizeof(*ctx));
209 ctx->ssl = ssl;
210 ctx->client_hello = in;
211 ctx->client_hello_len = in_len;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400212
David Benjamine14ff062016-08-09 16:21:24 -0400213 CBS client_hello, random, session_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
David Benjamine14ff062016-08-09 16:21:24 -0400215 if (!CBS_get_u16(&client_hello, &ctx->version) ||
216 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
217 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
218 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 return 0;
220 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700221
David Benjamine14ff062016-08-09 16:21:24 -0400222 ctx->random = CBS_data(&random);
223 ctx->random_len = CBS_len(&random);
Adam Langleyfcf25832014-12-18 17:42:32 -0800224 ctx->session_id = CBS_data(&session_id);
225 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700226
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 /* Skip past DTLS cookie */
David Benjamince079fd2016-08-02 16:22:34 -0400228 if (SSL_is_dtls(ctx->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400230 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
231 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 return 0;
233 }
234 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700235
David Benjamine14ff062016-08-09 16:21:24 -0400236 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400238 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
239 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 CBS_len(&compression_methods) < 1) {
241 return 0;
242 }
David Benjamine14ff062016-08-09 16:21:24 -0400243
244 ctx->cipher_suites = CBS_data(&cipher_suites);
245 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 ctx->compression_methods = CBS_data(&compression_methods);
247 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 /* If the ClientHello ends here then it's valid, but doesn't have any
250 * extensions. (E.g. SSLv3.) */
251 if (CBS_len(&client_hello) == 0) {
252 ctx->extensions = NULL;
253 ctx->extensions_len = 0;
254 return 1;
255 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 /* Extract extensions and check it is valid. */
David Benjamine14ff062016-08-09 16:21:24 -0400258 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
260 !tls1_check_duplicate_extensions(&extensions) ||
261 CBS_len(&client_hello) != 0) {
262 return 0;
263 }
David Benjamine14ff062016-08-09 16:21:24 -0400264
Adam Langleyfcf25832014-12-18 17:42:32 -0800265 ctx->extensions = CBS_data(&extensions);
266 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700267
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 return 1;
269}
Adam Langleydc9b1412014-06-20 12:00:00 -0700270
David Benjamincec73442016-08-02 17:41:33 -0400271int ssl_early_callback_get_extension(const struct ssl_early_callback_ctx *ctx,
272 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 while (CBS_len(&extensions) != 0) {
David Benjamincec73442016-08-02 17:41:33 -0400276 /* Decode the next extension. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 uint16_t type;
278 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 if (!CBS_get_u16(&extensions, &type) ||
280 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
281 return 0;
282 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400285 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800286 return 1;
287 }
288 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700289
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 return 0;
291}
Adam Langley95c29f32014-06-20 12:00:00 -0700292
David Benjamincec73442016-08-02 17:41:33 -0400293int SSL_early_callback_ctx_extension_get(
294 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
295 const uint8_t **out_data, size_t *out_len) {
296 CBS cbs;
297 if (!ssl_early_callback_get_extension(ctx, &cbs, extension_type)) {
298 return 0;
299 }
300
301 *out_data = CBS_data(&cbs);
302 *out_len = CBS_len(&cbs);
303 return 1;
304}
305
Steven Valdezce902a92016-05-17 11:47:53 -0400306static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400307 SSL_CURVE_X25519,
308 SSL_CURVE_SECP256R1,
309 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700310#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin9e68f192016-06-30 14:55:33 -0400311 SSL_CURVE_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700312#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800313};
Adam Langley95c29f32014-06-20 12:00:00 -0700314
David Benjaminf04976b2016-10-07 00:37:55 -0400315void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
Steven Valdez5440fe02016-07-18 12:40:30 -0400316 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400317 *out_group_ids = ssl->supported_group_list;
318 *out_group_ids_len = ssl->supported_group_list_len;
319 if (!*out_group_ids) {
320 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400321 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 }
323}
David Benjamined439582014-07-14 19:13:02 -0400324
Steven Valdezce902a92016-05-17 11:47:53 -0400325int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) {
David Benjaminf04976b2016-10-07 00:37:55 -0400326 assert(ssl->server);
David Benjamin072334d2014-07-13 16:24:27 -0400327
David Benjaminf04976b2016-10-07 00:37:55 -0400328 const uint16_t *groups, *pref, *supp;
329 size_t groups_len, pref_len, supp_len;
330 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800331
David Benjaminf04976b2016-10-07 00:37:55 -0400332 /* Clients are not required to send a supported_groups extension. In this
333 * case, the server is free to pick any group it likes. See RFC 4492,
334 * section 4, paragraph 3.
335 *
336 * However, in the interests of compatibility, we will skip ECDH if the
337 * client didn't send an extension because we can't be sure that they'll
338 * support our favoured group. Thus we do not special-case an emtpy
339 * |peer_supported_group_list|. */
David Benjamin55a43642015-04-20 14:45:55 -0400340
David Benjamin4298d772015-12-19 00:18:25 -0500341 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400342 pref = groups;
343 pref_len = groups_len;
David Benjamin43612b62016-10-07 00:41:50 -0400344 supp = ssl->s3->hs->peer_supported_group_list;
345 supp_len = ssl->s3->hs->peer_supported_group_list_len;
David Benjamin55a43642015-04-20 14:45:55 -0400346 } else {
David Benjamin43612b62016-10-07 00:41:50 -0400347 pref = ssl->s3->hs->peer_supported_group_list;
348 pref_len = ssl->s3->hs->peer_supported_group_list_len;
Steven Valdezce902a92016-05-17 11:47:53 -0400349 supp = groups;
350 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400351 }
352
David Benjaminf04976b2016-10-07 00:37:55 -0400353 for (size_t i = 0; i < pref_len; i++) {
354 for (size_t j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800355 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400356 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500357 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800358 }
359 }
360 }
361
David Benjamin4298d772015-12-19 00:18:25 -0500362 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800363}
Adam Langley95c29f32014-06-20 12:00:00 -0700364
Steven Valdezce902a92016-05-17 11:47:53 -0400365int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400367 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800368
Steven Valdezce902a92016-05-17 11:47:53 -0400369 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
370 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 return 0;
372 }
373
David Benjamin54091232016-09-05 12:47:25 -0400374 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400375 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
376 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 return 0;
378 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 }
380
Steven Valdezce902a92016-05-17 11:47:53 -0400381 OPENSSL_free(*out_group_ids);
382 *out_group_ids = group_ids;
383 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800384
385 return 1;
386}
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100388int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
389 const char *curves) {
390 uint16_t *group_ids = NULL;
391 size_t ncurves = 0;
392
393 const char *col;
394 const char *ptr = curves;
395
396 do {
397 col = strchr(ptr, ':');
398
399 uint16_t group_id;
400 if (!ssl_name_to_group_id(&group_id, ptr,
401 col ? (size_t)(col - ptr) : strlen(ptr))) {
402 goto err;
403 }
404
405 uint16_t *new_group_ids = OPENSSL_realloc(group_ids,
406 (ncurves + 1) * sizeof(uint16_t));
407 if (new_group_ids == NULL) {
408 goto err;
409 }
410 group_ids = new_group_ids;
411
412 group_ids[ncurves] = group_id;
413 ncurves++;
414
415 if (col) {
416 ptr = col + 1;
417 }
418 } while (col);
419
420 OPENSSL_free(*out_group_ids);
421 *out_group_ids = group_ids;
422 *out_group_ids_len = ncurves;
423
424 return 1;
425
426err:
427 OPENSSL_free(group_ids);
428 return 0;
429}
430
Steven Valdezce902a92016-05-17 11:47:53 -0400431int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
432 const uint16_t *groups;
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400433 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -0400434 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400435 for (size_t i = 0; i < groups_len; i++) {
436 if (groups[i] == group_id) {
437 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800438 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800439 }
David Benjamin033e5f42014-11-13 18:47:41 -0500440
David Benjamin9d0b4bc2016-10-07 00:34:08 -0400441 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800442}
David Benjamin033e5f42014-11-13 18:47:41 -0500443
Adam Langley95c29f32014-06-20 12:00:00 -0700444/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700446
Steven Valdez02563852016-06-23 13:33:05 -0400447static const uint16_t kDefaultSignatureAlgorithms[] = {
David Benjamin57e929f2016-08-30 00:30:38 -0400448 /* For now, do not ship RSA-PSS signature algorithms on Android's system
449 * BoringSSL. Once TLS 1.3 is finalized and the change in Chrome has stuck,
450 * restore them. */
451#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400452 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400453#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400454 SSL_SIGN_RSA_PKCS1_SHA512,
455 SSL_SIGN_ECDSA_SECP521R1_SHA512,
456
David Benjamin57e929f2016-08-30 00:30:38 -0400457#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400458 SSL_SIGN_RSA_PSS_SHA384,
David Benjamin57e929f2016-08-30 00:30:38 -0400459#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400460 SSL_SIGN_RSA_PKCS1_SHA384,
461 SSL_SIGN_ECDSA_SECP384R1_SHA384,
462
David Benjamin57e929f2016-08-30 00:30:38 -0400463#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400464 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400465#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400466 SSL_SIGN_RSA_PKCS1_SHA256,
467 SSL_SIGN_ECDSA_SECP256R1_SHA256,
468
469 SSL_SIGN_RSA_PKCS1_SHA1,
470 SSL_SIGN_ECDSA_SHA1,
471};
472
Steven Valdez02563852016-06-23 13:33:05 -0400473size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
474 *psigs = kDefaultSignatureAlgorithms;
Steven Valdezcb966542016-08-17 16:56:14 -0400475 return OPENSSL_ARRAY_SIZE(kDefaultSignatureAlgorithms);
Adam Langleyfcf25832014-12-18 17:42:32 -0800476}
Adam Langley95c29f32014-06-20 12:00:00 -0700477
David Benjamin887c3002016-07-08 16:15:32 -0700478int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
Steven Valdez02563852016-06-23 13:33:05 -0400479 const uint16_t *sent_sigs;
480 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800481
Adam Langleyfcf25832014-12-18 17:42:32 -0800482 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500483 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400484 for (i = 0; i < sent_sigslen; i++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400485 if (sigalg == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800486 break;
487 }
488 }
489
David Benjamin788be4a2015-10-30 17:50:57 -0400490 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400491 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800492 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
493 return 0;
494 }
495
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 return 1;
497}
498
499/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
500 * supported or doesn't appear in supported signature algorithms. Unlike
501 * ssl_cipher_get_disabled this applies to a specific session and not global
502 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500503void ssl_set_client_disabled(SSL *ssl) {
504 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800505 int have_rsa = 0, have_ecdsa = 0;
506 c->mask_a = 0;
507 c->mask_k = 0;
508
Adam Langleyfcf25832014-12-18 17:42:32 -0800509 /* Now go through all signature algorithms seeing if we support any for RSA,
510 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin0fc37ef2016-08-17 15:29:46 -0400511 const uint16_t *sigalgs;
512 size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
513 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -0400514 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400515 case SSL_SIGN_RSA_PSS_SHA512:
516 case SSL_SIGN_RSA_PSS_SHA384:
517 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400518 case SSL_SIGN_RSA_PKCS1_SHA512:
519 case SSL_SIGN_RSA_PKCS1_SHA384:
520 case SSL_SIGN_RSA_PKCS1_SHA256:
521 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 have_rsa = 1;
523 break;
524
Steven Valdez02563852016-06-23 13:33:05 -0400525 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
526 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
527 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
528 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800529 have_ecdsa = 1;
530 break;
531 }
532 }
533
534 /* Disable auth if we don't include any appropriate signature algorithms. */
535 if (!have_rsa) {
536 c->mask_a |= SSL_aRSA;
537 }
538 if (!have_ecdsa) {
539 c->mask_a |= SSL_aECDSA;
540 }
541
542 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500543 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800544 c->mask_a |= SSL_aPSK;
545 c->mask_k |= SSL_kPSK;
546 }
547}
Adam Langley95c29f32014-06-20 12:00:00 -0700548
Adam Langley614c66a2015-06-12 15:26:58 -0700549/* tls_extension represents a TLS extension that is handled internally. The
550 * |init| function is called for each handshake, before any other functions of
551 * the extension. Then the add and parse callbacks are called as needed.
552 *
553 * The parse callbacks receive a |CBS| that contains the contents of the
554 * extension (i.e. not including the type and length bytes). If an extension is
555 * not received then the parse callbacks will be called with a NULL CBS so that
556 * they can do any processing needed to handle the absence of an extension.
557 *
558 * The add callbacks receive a |CBB| to which the extension can be appended but
559 * the function is responsible for appending the type and length bytes too.
560 *
561 * All callbacks return one for success and zero for error. If a parse function
562 * returns zero then a fatal alert with value |*out_alert| will be sent. If
563 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
564struct tls_extension {
565 uint16_t value;
566 void (*init)(SSL *ssl);
567
568 int (*add_clienthello)(SSL *ssl, CBB *out);
569 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
570
571 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
572 int (*add_serverhello)(SSL *ssl, CBB *out);
573};
574
Steven Valdez6b8509a2016-07-12 13:38:32 -0400575static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
576 if (contents != NULL) {
577 /* Servers MUST NOT send this extension. */
578 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
579 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
580 return 0;
581 }
582
583 return 1;
584}
585
586static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
587 /* This extension from the client is handled elsewhere. */
588 return 1;
589}
590
591static int dont_add_serverhello(SSL *ssl, CBB *out) {
592 return 1;
593}
Adam Langley614c66a2015-06-12 15:26:58 -0700594
595/* Server name indication (SNI).
596 *
597 * https://tools.ietf.org/html/rfc6066#section-3. */
598
Adam Langley614c66a2015-06-12 15:26:58 -0700599static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
600 if (ssl->tlsext_hostname == NULL) {
601 return 1;
602 }
603
604 CBB contents, server_name_list, name;
605 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
606 !CBB_add_u16_length_prefixed(out, &contents) ||
607 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
608 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
609 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
610 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
611 strlen(ssl->tlsext_hostname)) ||
612 !CBB_flush(out)) {
613 return 0;
614 }
615
616 return 1;
617}
618
David Benjamin0d56f882015-12-19 17:05:56 -0500619static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
620 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700621 if (contents == NULL) {
622 return 1;
623 }
624
625 if (CBS_len(contents) != 0) {
626 return 0;
627 }
628
629 assert(ssl->tlsext_hostname != NULL);
630
Steven Valdez87eab492016-06-27 16:34:59 -0400631 if (ssl->session == NULL) {
632 assert(ssl->s3->new_session->tlsext_hostname == NULL);
633 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
634 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700635 *out_alert = SSL_AD_INTERNAL_ERROR;
636 return 0;
637 }
638 }
639
640 return 1;
641}
642
David Benjamin0d56f882015-12-19 17:05:56 -0500643static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
644 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700645 if (contents == NULL) {
646 return 1;
647 }
648
David Benjamin9b611e22016-03-03 08:48:30 -0500649 CBS server_name_list, host_name;
650 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700651 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500652 !CBS_get_u8(&server_name_list, &name_type) ||
653 /* Although the server_name extension was intended to be extensible to
654 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
655 * different name types will cause an error. Further, RFC 4366 originally
656 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
657 * adding new name types is no longer feasible.
658 *
659 * Act as if the extensibility does not exist to simplify parsing. */
660 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
661 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700662 CBS_len(contents) != 0) {
663 return 0;
664 }
665
David Benjamin9b611e22016-03-03 08:48:30 -0500666 if (name_type != TLSEXT_NAMETYPE_host_name ||
667 CBS_len(&host_name) == 0 ||
668 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
669 CBS_contains_zero_byte(&host_name)) {
670 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
671 return 0;
672 }
Adam Langley614c66a2015-06-12 15:26:58 -0700673
David Benjamin9b611e22016-03-03 08:48:30 -0500674 /* TODO(davidben): SNI should be resolved before resumption. We have the
675 * early callback as a replacement, but we should fix the current callback
676 * and avoid the need for |SSL_CTX_set_session_id_context|. */
Steven Valdez87eab492016-06-27 16:34:59 -0400677 if (ssl->session == NULL) {
678 assert(ssl->s3->new_session->tlsext_hostname == NULL);
David Benjamin9b611e22016-03-03 08:48:30 -0500679
680 /* Copy the hostname as a string. */
Steven Valdez87eab492016-06-27 16:34:59 -0400681 if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) {
David Benjamin9b611e22016-03-03 08:48:30 -0500682 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700683 return 0;
684 }
685
David Benjamina0486782016-10-06 19:11:32 -0400686 ssl->s3->hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700687 }
688
689 return 1;
690}
691
692static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez87eab492016-06-27 16:34:59 -0400693 if (ssl->session != NULL ||
David Benjamina0486782016-10-06 19:11:32 -0400694 !ssl->s3->hs->should_ack_sni ||
Steven Valdez87eab492016-06-27 16:34:59 -0400695 ssl->s3->new_session->tlsext_hostname == NULL) {
Adam Langley614c66a2015-06-12 15:26:58 -0700696 return 1;
697 }
698
699 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
700 !CBB_add_u16(out, 0 /* length */)) {
701 return 0;
702 }
703
704 return 1;
705}
706
707
Adam Langley5021b222015-06-12 18:27:58 -0700708/* Renegotiation indication.
709 *
710 * https://tools.ietf.org/html/rfc5746 */
711
712static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400713 uint16_t min_version, max_version;
714 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
715 return 0;
716 }
717
718 /* Renegotiation indication is not necessary in TLS 1.3. */
719 if (min_version >= TLS1_3_VERSION) {
720 return 1;
721 }
722
David Benjamin52bf6902016-10-08 12:05:03 -0400723 assert(ssl->s3->initial_handshake_complete ==
724 (ssl->s3->previous_client_finished_len != 0));
725
Adam Langley5021b222015-06-12 18:27:58 -0700726 CBB contents, prev_finished;
727 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
728 !CBB_add_u16_length_prefixed(out, &contents) ||
729 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
730 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
731 ssl->s3->previous_client_finished_len) ||
732 !CBB_flush(out)) {
733 return 0;
734 }
735
736 return 1;
737}
738
739static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
740 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400741 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
742 return 0;
743 }
744
David Benjamin3e052de2015-11-25 20:10:31 -0500745 /* Servers may not switch between omitting the extension and supporting it.
746 * See RFC 5746, sections 3.5 and 4.2. */
747 if (ssl->s3->initial_handshake_complete &&
748 (contents != NULL) != ssl->s3->send_connection_binding) {
749 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
750 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
751 return 0;
752 }
753
Adam Langley5021b222015-06-12 18:27:58 -0700754 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500755 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700756 * RI even on initial ServerHello because the client doesn't see any
757 * renegotiation during an attack. However this would mean we could not
758 * connect to any server which doesn't support RI.
759 *
David Benjamine9cddb82015-11-23 14:36:40 -0500760 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
761 * practical terms every client sets it so it's just assumed here. */
762 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700763 }
764
765 const size_t expected_len = ssl->s3->previous_client_finished_len +
766 ssl->s3->previous_server_finished_len;
767
768 /* Check for logic errors */
769 assert(!expected_len || ssl->s3->previous_client_finished_len);
770 assert(!expected_len || ssl->s3->previous_server_finished_len);
David Benjamin52bf6902016-10-08 12:05:03 -0400771 assert(ssl->s3->initial_handshake_complete ==
772 (ssl->s3->previous_client_finished_len != 0));
773 assert(ssl->s3->initial_handshake_complete ==
774 (ssl->s3->previous_server_finished_len != 0));
Adam Langley5021b222015-06-12 18:27:58 -0700775
776 /* Parse out the extension contents. */
777 CBS renegotiated_connection;
778 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
779 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400780 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700781 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
782 return 0;
783 }
784
785 /* Check that the extension matches. */
786 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400787 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700788 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
789 return 0;
790 }
791
792 const uint8_t *d = CBS_data(&renegotiated_connection);
793 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
794 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400795 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700796 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
797 return 0;
798 }
799 d += ssl->s3->previous_client_finished_len;
800
801 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
802 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400803 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700804 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
805 return 0;
806 }
807 ssl->s3->send_connection_binding = 1;
808
809 return 1;
810}
811
812static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
813 CBS *contents) {
814 /* Renegotiation isn't supported as a server so this function should never be
815 * called after the initial handshake. */
816 assert(!ssl->s3->initial_handshake_complete);
817
Steven Valdez143e8b32016-07-11 13:19:03 -0400818 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
819 return 1;
820 }
821
Adam Langley5021b222015-06-12 18:27:58 -0700822 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400823 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700824 }
825
826 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700827 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
828 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400829 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700830 return 0;
831 }
832
David Benjamin52bf6902016-10-08 12:05:03 -0400833 /* Check that the extension matches. We do not support renegotiation as a
834 * server, so this must be empty. */
835 if (CBS_len(&renegotiated_connection) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400836 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700837 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
838 return 0;
839 }
840
841 ssl->s3->send_connection_binding = 1;
842
843 return 1;
844}
845
846static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin52bf6902016-10-08 12:05:03 -0400847 /* Renegotiation isn't supported as a server so this function should never be
848 * called after the initial handshake. */
849 assert(!ssl->s3->initial_handshake_complete);
850
Steven Valdez143e8b32016-07-11 13:19:03 -0400851 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
852 return 1;
853 }
854
Adam Langley5021b222015-06-12 18:27:58 -0700855 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
David Benjamin52bf6902016-10-08 12:05:03 -0400856 !CBB_add_u16(out, 1 /* length */) ||
857 !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
Adam Langley5021b222015-06-12 18:27:58 -0700858 return 0;
859 }
860
861 return 1;
862}
863
Adam Langley0a056712015-07-01 15:03:33 -0700864
865/* Extended Master Secret.
866 *
David Benjamin43946d42016-02-01 08:42:19 -0500867 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700868
Adam Langley0a056712015-07-01 15:03:33 -0700869static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400870 uint16_t min_version, max_version;
871 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
872 return 0;
873 }
874
875 /* Extended master secret is not necessary in TLS 1.3. */
876 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700877 return 1;
878 }
879
880 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
881 !CBB_add_u16(out, 0 /* length */)) {
882 return 0;
883 }
884
885 return 1;
886}
887
888static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
889 CBS *contents) {
David Benjamin163c9562016-08-29 23:14:17 -0400890 /* Whether EMS is negotiated may not change on renegotation. */
891 if (ssl->s3->initial_handshake_complete) {
892 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
893 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
894 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
895 return 0;
896 }
897
898 return 1;
899 }
900
Adam Langley0a056712015-07-01 15:03:33 -0700901 if (contents == NULL) {
902 return 1;
903 }
904
Steven Valdez143e8b32016-07-11 13:19:03 -0400905 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
906 ssl->version == SSL3_VERSION) {
907 return 0;
908 }
909
910 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700911 return 0;
912 }
913
914 ssl->s3->tmp.extended_master_secret = 1;
915 return 1;
916}
917
David Benjamin0d56f882015-12-19 17:05:56 -0500918static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
919 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400920 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
921 ssl->version == SSL3_VERSION) {
922 return 1;
923 }
924
925 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700926 return 1;
927 }
928
929 if (CBS_len(contents) != 0) {
930 return 0;
931 }
932
933 ssl->s3->tmp.extended_master_secret = 1;
934 return 1;
935}
936
937static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
938 if (!ssl->s3->tmp.extended_master_secret) {
939 return 1;
940 }
941
942 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
943 !CBB_add_u16(out, 0 /* length */)) {
944 return 0;
945 }
946
947 return 1;
948}
949
Adam Langley9b05bc52015-07-01 15:25:33 -0700950
951/* Session tickets.
952 *
953 * https://tools.ietf.org/html/rfc5077 */
954
955static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400956 uint16_t min_version, max_version;
957 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
958 return 0;
959 }
960
961 /* TLS 1.3 uses a different ticket extension. */
962 if (min_version >= TLS1_3_VERSION ||
963 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700964 return 1;
965 }
966
967 const uint8_t *ticket_data = NULL;
968 int ticket_len = 0;
969
970 /* Renegotiation does not participate in session resumption. However, still
971 * advertise the extension to avoid potentially breaking servers which carry
972 * over the state from the previous handshake, such as OpenSSL servers
973 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -0400974 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -0700975 if (!ssl->s3->initial_handshake_complete &&
976 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400977 ssl->session->tlsext_tick != NULL &&
978 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -0400979 ssl->method->version_from_wire(&session_version,
980 ssl->session->ssl_version) &&
981 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -0700982 ticket_data = ssl->session->tlsext_tick;
983 ticket_len = ssl->session->tlsext_ticklen;
984 }
985
986 CBB ticket;
987 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
988 !CBB_add_u16_length_prefixed(out, &ticket) ||
989 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
990 !CBB_flush(out)) {
991 return 0;
992 }
993
994 return 1;
995}
996
997static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
998 CBS *contents) {
999 ssl->tlsext_ticket_expected = 0;
1000
1001 if (contents == NULL) {
1002 return 1;
1003 }
1004
Steven Valdez143e8b32016-07-11 13:19:03 -04001005 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1006 return 0;
1007 }
1008
Adam Langley9b05bc52015-07-01 15:25:33 -07001009 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1010 * this function should never be called, even if the server tries to send the
1011 * extension. */
1012 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1013
1014 if (CBS_len(contents) != 0) {
1015 return 0;
1016 }
1017
1018 ssl->tlsext_ticket_expected = 1;
1019 return 1;
1020}
1021
Adam Langley9b05bc52015-07-01 15:25:33 -07001022static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1023 if (!ssl->tlsext_ticket_expected) {
1024 return 1;
1025 }
1026
1027 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1028 * true. */
1029 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1030
1031 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1032 !CBB_add_u16(out, 0 /* length */)) {
1033 return 0;
1034 }
1035
1036 return 1;
1037}
1038
1039
Adam Langley2e857bd2015-07-01 16:09:19 -07001040/* Signature Algorithms.
1041 *
1042 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1043
1044static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin2dc02042016-09-19 19:57:37 -04001045 uint16_t min_version, max_version;
1046 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1047 return 0;
1048 }
1049
1050 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001051 return 1;
1052 }
1053
David Benjamin0fc37ef2016-08-17 15:29:46 -04001054 const uint16_t *sigalgs;
1055 const size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001056
David Benjamin0fc37ef2016-08-17 15:29:46 -04001057 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001058 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1059 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001060 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001061 return 0;
1062 }
1063
David Benjamin0fc37ef2016-08-17 15:29:46 -04001064 for (size_t i = 0; i < num_sigalgs; i++) {
1065 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001066 return 0;
1067 }
1068 }
1069
1070 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001071 return 0;
1072 }
1073
1074 return 1;
1075}
1076
Adam Langley2e857bd2015-07-01 16:09:19 -07001077static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1078 CBS *contents) {
David Benjamin0fc37ef2016-08-17 15:29:46 -04001079 OPENSSL_free(ssl->s3->hs->peer_sigalgs);
1080 ssl->s3->hs->peer_sigalgs = NULL;
1081 ssl->s3->hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001082
Adam Langley2e857bd2015-07-01 16:09:19 -07001083 if (contents == NULL) {
1084 return 1;
1085 }
1086
1087 CBS supported_signature_algorithms;
1088 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001089 CBS_len(contents) != 0 ||
1090 CBS_len(&supported_signature_algorithms) == 0 ||
1091 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001092 return 0;
1093 }
1094
1095 return 1;
1096}
1097
Adam Langley2e857bd2015-07-01 16:09:19 -07001098
Adam Langleybb0bd042015-07-01 16:21:03 -07001099/* OCSP Stapling.
1100 *
1101 * https://tools.ietf.org/html/rfc6066#section-8 */
1102
1103static void ext_ocsp_init(SSL *ssl) {
Adam Langleyce9d85e2016-01-24 15:58:39 -08001104 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001105}
1106
1107static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1108 if (!ssl->ocsp_stapling_enabled) {
1109 return 1;
1110 }
1111
1112 CBB contents;
1113 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1114 !CBB_add_u16_length_prefixed(out, &contents) ||
1115 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1116 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1117 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1118 !CBB_flush(out)) {
1119 return 0;
1120 }
1121
Adam Langleyce9d85e2016-01-24 15:58:39 -08001122 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001123 return 1;
1124}
1125
1126static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001127 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001128 if (contents == NULL) {
1129 return 1;
1130 }
1131
David Benjamin942f4ed2016-07-16 19:03:49 +03001132 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001133 /* OCSP stapling is forbidden on non-certificate ciphers. */
1134 if (CBS_len(contents) != 0 ||
1135 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001136 return 0;
1137 }
1138
David Benjamindaa88502016-10-04 16:32:16 -04001139 /* Note this does not check for resumption in TLS 1.2. Sending
1140 * status_request here does not make sense, but OpenSSL does so and the
1141 * specification does not say anything. Tolerate it but ignore it. */
1142
David Benjamina0486782016-10-06 19:11:32 -04001143 ssl->s3->hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001144 return 1;
1145 }
1146
Steven Valdez803c77a2016-09-06 14:13:43 -04001147 /* In TLS 1.3, OCSP stapling is forbidden on resumption. */
1148 if (ssl->s3->session_reused) {
1149 return 0;
1150 }
1151
David Benjamin942f4ed2016-07-16 19:03:49 +03001152 uint8_t status_type;
1153 CBS ocsp_response;
1154 if (!CBS_get_u8(contents, &status_type) ||
1155 status_type != TLSEXT_STATUSTYPE_ocsp ||
1156 !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1157 CBS_len(&ocsp_response) == 0 ||
1158 CBS_len(contents) != 0) {
1159 return 0;
1160 }
1161
Steven Valdez87eab492016-06-27 16:34:59 -04001162 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1163 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001164 *out_alert = SSL_AD_INTERNAL_ERROR;
1165 return 0;
1166 }
1167
Adam Langleybb0bd042015-07-01 16:21:03 -07001168 return 1;
1169}
1170
1171static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1172 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001173 if (contents == NULL) {
1174 return 1;
1175 }
1176
1177 uint8_t status_type;
1178 if (!CBS_get_u8(contents, &status_type)) {
1179 return 0;
1180 }
1181
1182 /* We cannot decide whether OCSP stapling will occur yet because the correct
1183 * SSL_CTX might not have been selected. */
David Benjamina0486782016-10-06 19:11:32 -04001184 ssl->s3->hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001185
Adam Langleybb0bd042015-07-01 16:21:03 -07001186 return 1;
1187}
1188
1189static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
David Benjamina0486782016-10-06 19:11:32 -04001190 if (!ssl->s3->hs->ocsp_stapling_requested ||
David Benjamin942f4ed2016-07-16 19:03:49 +03001191 ssl->ctx->ocsp_response_length == 0 ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001192 ssl->s3->session_reused ||
1193 (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
1194 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001195 return 1;
1196 }
1197
David Benjamin942f4ed2016-07-16 19:03:49 +03001198 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1199 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez87eab492016-06-27 16:34:59 -04001200 if (ssl->session != NULL) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001201 return 1;
1202 }
1203
David Benjamina0486782016-10-06 19:11:32 -04001204 ssl->s3->hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001205
1206 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1207 CBB_add_u16(out, 0 /* length */);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001208 }
1209
David Benjamin942f4ed2016-07-16 19:03:49 +03001210 CBB body, ocsp_response;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001211 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
David Benjamin942f4ed2016-07-16 19:03:49 +03001212 CBB_add_u16_length_prefixed(out, &body) &&
1213 CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1214 CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1215 CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1216 ssl->ctx->ocsp_response_length) &&
1217 CBB_flush(out);
Adam Langleybb0bd042015-07-01 16:21:03 -07001218}
1219
1220
Adam Langley97dfcbf2015-07-01 18:35:20 -07001221/* Next protocol negotiation.
1222 *
1223 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1224
Adam Langley97dfcbf2015-07-01 18:35:20 -07001225static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1226 if (ssl->s3->initial_handshake_complete ||
1227 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001228 (ssl->options & SSL_OP_DISABLE_NPN) ||
David Benjamince079fd2016-08-02 16:22:34 -04001229 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001230 return 1;
1231 }
1232
1233 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1234 !CBB_add_u16(out, 0 /* length */)) {
1235 return 0;
1236 }
1237
1238 return 1;
1239}
1240
1241static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1242 CBS *contents) {
1243 if (contents == NULL) {
1244 return 1;
1245 }
1246
Steven Valdez143e8b32016-07-11 13:19:03 -04001247 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1248 return 0;
1249 }
1250
Adam Langley97dfcbf2015-07-01 18:35:20 -07001251 /* If any of these are false then we should never have sent the NPN
1252 * extension in the ClientHello and thus this function should never have been
1253 * called. */
1254 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001255 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001256 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001257 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001258
David Benjamin76c2efc2015-08-31 14:24:29 -04001259 if (ssl->s3->alpn_selected != NULL) {
1260 /* NPN and ALPN may not be negotiated in the same connection. */
1261 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1262 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1263 return 0;
1264 }
1265
Adam Langley97dfcbf2015-07-01 18:35:20 -07001266 const uint8_t *const orig_contents = CBS_data(contents);
1267 const size_t orig_len = CBS_len(contents);
1268
1269 while (CBS_len(contents) != 0) {
1270 CBS proto;
1271 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1272 CBS_len(&proto) == 0) {
1273 return 0;
1274 }
1275 }
1276
1277 uint8_t *selected;
1278 uint8_t selected_len;
1279 if (ssl->ctx->next_proto_select_cb(
1280 ssl, &selected, &selected_len, orig_contents, orig_len,
1281 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1282 *out_alert = SSL_AD_INTERNAL_ERROR;
1283 return 0;
1284 }
1285
David Benjamin79978df2015-12-25 15:56:49 -05001286 OPENSSL_free(ssl->s3->next_proto_negotiated);
1287 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1288 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001289 *out_alert = SSL_AD_INTERNAL_ERROR;
1290 return 0;
1291 }
1292
David Benjamin79978df2015-12-25 15:56:49 -05001293 ssl->s3->next_proto_negotiated_len = selected_len;
David Benjaminb74b0812016-10-06 19:43:48 -04001294 ssl->s3->hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001295
1296 return 1;
1297}
1298
1299static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1300 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001301 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1302 return 1;
1303 }
1304
Adam Langley97dfcbf2015-07-01 18:35:20 -07001305 if (contents != NULL && CBS_len(contents) != 0) {
1306 return 0;
1307 }
1308
1309 if (contents == NULL ||
1310 ssl->s3->initial_handshake_complete ||
1311 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1312 * afterwards, parsing the ALPN extension will clear
1313 * |next_proto_neg_seen|. */
1314 ssl->s3->alpn_selected != NULL ||
1315 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001316 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001317 return 1;
1318 }
1319
David Benjaminb74b0812016-10-06 19:43:48 -04001320 ssl->s3->hs->next_proto_neg_seen = 1;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001321 return 1;
1322}
1323
1324static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1325 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1326 * parsed. */
David Benjaminb74b0812016-10-06 19:43:48 -04001327 if (!ssl->s3->hs->next_proto_neg_seen) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001328 return 1;
1329 }
1330
1331 const uint8_t *npa;
1332 unsigned npa_len;
1333
1334 if (ssl->ctx->next_protos_advertised_cb(
1335 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1336 SSL_TLSEXT_ERR_OK) {
David Benjaminb74b0812016-10-06 19:43:48 -04001337 ssl->s3->hs->next_proto_neg_seen = 0;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001338 return 1;
1339 }
1340
1341 CBB contents;
1342 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1343 !CBB_add_u16_length_prefixed(out, &contents) ||
1344 !CBB_add_bytes(&contents, npa, npa_len) ||
1345 !CBB_flush(out)) {
1346 return 0;
1347 }
1348
1349 return 1;
1350}
1351
1352
Adam Langleyab8d87d2015-07-10 12:21:39 -07001353/* Signed certificate timestamps.
1354 *
1355 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1356
1357static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1358 if (!ssl->signed_cert_timestamps_enabled) {
1359 return 1;
1360 }
1361
1362 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1363 !CBB_add_u16(out, 0 /* length */)) {
1364 return 0;
1365 }
1366
1367 return 1;
1368}
1369
1370static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1371 CBS *contents) {
1372 if (contents == NULL) {
1373 return 1;
1374 }
1375
1376 /* If this is false then we should never have sent the SCT extension in the
1377 * ClientHello and thus this function should never have been called. */
1378 assert(ssl->signed_cert_timestamps_enabled);
1379
1380 if (CBS_len(contents) == 0) {
1381 *out_alert = SSL_AD_DECODE_ERROR;
1382 return 0;
1383 }
1384
David Benjamindaa88502016-10-04 16:32:16 -04001385 /* Session resumption uses the original session information. The extension
1386 * should not be sent on resumption, but RFC 6962 did not make it a
1387 * requirement, so tolerate this.
1388 *
1389 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001390 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001391 !CBS_stow(
1392 contents,
1393 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1394 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001395 *out_alert = SSL_AD_INTERNAL_ERROR;
1396 return 0;
1397 }
1398
1399 return 1;
1400}
1401
1402static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1403 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001404 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001405}
1406
1407static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001408 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001409 if (ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001410 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001411 return 1;
1412 }
1413
1414 CBB contents;
1415 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1416 CBB_add_u16_length_prefixed(out, &contents) &&
1417 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1418 ssl->ctx->signed_cert_timestamp_list_length) &&
1419 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001420}
1421
1422
Adam Langleyf18e4532015-07-10 13:39:53 -07001423/* Application-level Protocol Negotiation.
1424 *
1425 * https://tools.ietf.org/html/rfc7301 */
1426
1427static void ext_alpn_init(SSL *ssl) {
1428 OPENSSL_free(ssl->s3->alpn_selected);
1429 ssl->s3->alpn_selected = NULL;
1430}
1431
1432static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1433 if (ssl->alpn_client_proto_list == NULL ||
1434 ssl->s3->initial_handshake_complete) {
1435 return 1;
1436 }
1437
1438 CBB contents, proto_list;
1439 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1440 !CBB_add_u16_length_prefixed(out, &contents) ||
1441 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1442 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1443 ssl->alpn_client_proto_list_len) ||
1444 !CBB_flush(out)) {
1445 return 0;
1446 }
1447
1448 return 1;
1449}
1450
1451static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1452 CBS *contents) {
1453 if (contents == NULL) {
1454 return 1;
1455 }
1456
1457 assert(!ssl->s3->initial_handshake_complete);
1458 assert(ssl->alpn_client_proto_list != NULL);
1459
David Benjaminb74b0812016-10-06 19:43:48 -04001460 if (ssl->s3->hs->next_proto_neg_seen) {
David Benjamin76c2efc2015-08-31 14:24:29 -04001461 /* NPN and ALPN may not be negotiated in the same connection. */
1462 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1463 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1464 return 0;
1465 }
1466
Adam Langleyf18e4532015-07-10 13:39:53 -07001467 /* The extension data consists of a ProtocolNameList which must have
1468 * exactly one ProtocolName. Each of these is length-prefixed. */
1469 CBS protocol_name_list, protocol_name;
1470 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1471 CBS_len(contents) != 0 ||
1472 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1473 /* Empty protocol names are forbidden. */
1474 CBS_len(&protocol_name) == 0 ||
1475 CBS_len(&protocol_name_list) != 0) {
1476 return 0;
1477 }
1478
David Benjamin3e517572016-08-11 11:52:23 -04001479 /* Check that the protcol name is one of the ones we advertised. */
1480 int protocol_ok = 0;
1481 CBS client_protocol_name_list, client_protocol_name;
1482 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1483 ssl->alpn_client_proto_list_len);
1484 while (CBS_len(&client_protocol_name_list) > 0) {
1485 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1486 &client_protocol_name)) {
1487 *out_alert = SSL_AD_INTERNAL_ERROR;
1488 return 0;
1489 }
1490
1491 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1492 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1493 CBS_len(&protocol_name)) == 0) {
1494 protocol_ok = 1;
1495 break;
1496 }
1497 }
1498
1499 if (!protocol_ok) {
1500 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1501 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1502 return 0;
1503 }
1504
Adam Langleyf18e4532015-07-10 13:39:53 -07001505 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1506 &ssl->s3->alpn_selected_len)) {
1507 *out_alert = SSL_AD_INTERNAL_ERROR;
1508 return 0;
1509 }
1510
1511 return 1;
1512}
1513
1514static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1515 CBS *contents) {
1516 if (contents == NULL) {
1517 return 1;
1518 }
1519
1520 if (ssl->ctx->alpn_select_cb == NULL ||
1521 ssl->s3->initial_handshake_complete) {
1522 return 1;
1523 }
1524
1525 /* ALPN takes precedence over NPN. */
David Benjaminb74b0812016-10-06 19:43:48 -04001526 ssl->s3->hs->next_proto_neg_seen = 0;
Adam Langleyf18e4532015-07-10 13:39:53 -07001527
1528 CBS protocol_name_list;
1529 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1530 CBS_len(contents) != 0 ||
1531 CBS_len(&protocol_name_list) < 2) {
1532 return 0;
1533 }
1534
1535 /* Validate the protocol list. */
1536 CBS protocol_name_list_copy = protocol_name_list;
1537 while (CBS_len(&protocol_name_list_copy) > 0) {
1538 CBS protocol_name;
1539
1540 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1541 /* Empty protocol names are forbidden. */
1542 CBS_len(&protocol_name) == 0) {
1543 return 0;
1544 }
1545 }
1546
1547 const uint8_t *selected;
1548 uint8_t selected_len;
1549 if (ssl->ctx->alpn_select_cb(
1550 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1551 CBS_len(&protocol_name_list),
1552 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1553 OPENSSL_free(ssl->s3->alpn_selected);
1554 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1555 if (ssl->s3->alpn_selected == NULL) {
1556 *out_alert = SSL_AD_INTERNAL_ERROR;
1557 return 0;
1558 }
1559 ssl->s3->alpn_selected_len = selected_len;
1560 }
1561
1562 return 1;
1563}
1564
1565static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1566 if (ssl->s3->alpn_selected == NULL) {
1567 return 1;
1568 }
1569
1570 CBB contents, proto_list, proto;
1571 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1572 !CBB_add_u16_length_prefixed(out, &contents) ||
1573 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1574 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001575 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1576 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001577 !CBB_flush(out)) {
1578 return 0;
1579 }
1580
1581 return 1;
1582}
1583
1584
Adam Langley49c7af12015-07-10 14:33:46 -07001585/* Channel ID.
1586 *
1587 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1588
1589static void ext_channel_id_init(SSL *ssl) {
1590 ssl->s3->tlsext_channel_id_valid = 0;
1591}
1592
1593static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1594 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001595 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001596 return 1;
1597 }
1598
1599 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1600 !CBB_add_u16(out, 0 /* length */)) {
1601 return 0;
1602 }
1603
1604 return 1;
1605}
1606
1607static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1608 CBS *contents) {
1609 if (contents == NULL) {
1610 return 1;
1611 }
1612
Steven Valdez143e8b32016-07-11 13:19:03 -04001613 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1614 return 0;
1615 }
1616
David Benjamince079fd2016-08-02 16:22:34 -04001617 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001618 assert(ssl->tlsext_channel_id_enabled);
1619
1620 if (CBS_len(contents) != 0) {
1621 return 0;
1622 }
1623
1624 ssl->s3->tlsext_channel_id_valid = 1;
1625 return 1;
1626}
1627
1628static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1629 CBS *contents) {
1630 if (contents == NULL ||
1631 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001632 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001633 return 1;
1634 }
1635
1636 if (CBS_len(contents) != 0) {
1637 return 0;
1638 }
1639
1640 ssl->s3->tlsext_channel_id_valid = 1;
1641 return 1;
1642}
1643
1644static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001645 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1646 return 1;
1647 }
1648
Adam Langley49c7af12015-07-10 14:33:46 -07001649 if (!ssl->s3->tlsext_channel_id_valid) {
1650 return 1;
1651 }
1652
1653 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1654 !CBB_add_u16(out, 0 /* length */)) {
1655 return 0;
1656 }
1657
1658 return 1;
1659}
1660
Adam Langley391250d2015-07-15 19:06:07 -07001661
1662/* Secure Real-time Transport Protocol (SRTP) extension.
1663 *
1664 * https://tools.ietf.org/html/rfc5764 */
1665
Adam Langley391250d2015-07-15 19:06:07 -07001666
1667static void ext_srtp_init(SSL *ssl) {
1668 ssl->srtp_profile = NULL;
1669}
1670
1671static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1672 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1673 if (profiles == NULL) {
1674 return 1;
1675 }
1676 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1677 if (num_profiles == 0) {
1678 return 1;
1679 }
1680
1681 CBB contents, profile_ids;
1682 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1683 !CBB_add_u16_length_prefixed(out, &contents) ||
1684 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1685 return 0;
1686 }
1687
David Benjamin54091232016-09-05 12:47:25 -04001688 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001689 if (!CBB_add_u16(&profile_ids,
1690 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1691 return 0;
1692 }
1693 }
1694
1695 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1696 !CBB_flush(out)) {
1697 return 0;
1698 }
1699
1700 return 1;
1701}
1702
1703static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1704 CBS *contents) {
1705 if (contents == NULL) {
1706 return 1;
1707 }
1708
1709 /* The extension consists of a u16-prefixed profile ID list containing a
1710 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1711 *
1712 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1713 CBS profile_ids, srtp_mki;
1714 uint16_t profile_id;
1715 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1716 !CBS_get_u16(&profile_ids, &profile_id) ||
1717 CBS_len(&profile_ids) != 0 ||
1718 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1719 CBS_len(contents) != 0) {
1720 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1721 return 0;
1722 }
1723
1724 if (CBS_len(&srtp_mki) != 0) {
1725 /* Must be no MKI, since we never offer one. */
1726 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1727 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1728 return 0;
1729 }
1730
1731 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1732
1733 /* Check to see if the server gave us something we support (and presumably
1734 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001735 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001736 const SRTP_PROTECTION_PROFILE *profile =
1737 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1738
1739 if (profile->id == profile_id) {
1740 ssl->srtp_profile = profile;
1741 return 1;
1742 }
1743 }
1744
1745 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1746 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1747 return 0;
1748}
1749
1750static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1751 CBS *contents) {
1752 if (contents == NULL) {
1753 return 1;
1754 }
1755
1756 CBS profile_ids, srtp_mki;
1757 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1758 CBS_len(&profile_ids) < 2 ||
1759 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1760 CBS_len(contents) != 0) {
1761 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1762 return 0;
1763 }
1764 /* Discard the MKI value for now. */
1765
1766 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1767 SSL_get_srtp_profiles(ssl);
1768
1769 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001770 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001771 const SRTP_PROTECTION_PROFILE *server_profile =
1772 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1773
1774 CBS profile_ids_tmp;
1775 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1776
1777 while (CBS_len(&profile_ids_tmp) > 0) {
1778 uint16_t profile_id;
1779 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1780 return 0;
1781 }
1782
1783 if (server_profile->id == profile_id) {
1784 ssl->srtp_profile = server_profile;
1785 return 1;
1786 }
1787 }
1788 }
1789
1790 return 1;
1791}
1792
1793static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1794 if (ssl->srtp_profile == NULL) {
1795 return 1;
1796 }
1797
1798 CBB contents, profile_ids;
1799 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1800 !CBB_add_u16_length_prefixed(out, &contents) ||
1801 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1802 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1803 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1804 !CBB_flush(out)) {
1805 return 0;
1806 }
1807
1808 return 1;
1809}
1810
Adam Langleybdd5d662015-07-20 16:19:08 -07001811
1812/* EC point formats.
1813 *
1814 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1815
1816static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
David Benjamince079fd2016-08-02 16:22:34 -04001817 if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001818 return 0;
1819 }
1820
1821 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1822
David Benjamin54091232016-09-05 12:47:25 -04001823 for (size_t i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001824 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1825
1826 const uint32_t alg_k = cipher->algorithm_mkey;
1827 const uint32_t alg_a = cipher->algorithm_auth;
1828 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1829 return 1;
1830 }
1831 }
1832
1833 return 0;
1834}
1835
Adam Langleybdd5d662015-07-20 16:19:08 -07001836static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001837 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001838 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1839 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001840 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1841 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001842 !CBB_flush(out)) {
1843 return 0;
1844 }
1845
1846 return 1;
1847}
1848
1849static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1850 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1851 return 1;
1852 }
1853
1854 return ext_ec_point_add_extension(ssl, out);
1855}
1856
1857static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1858 CBS *contents) {
1859 if (contents == NULL) {
1860 return 1;
1861 }
1862
Steven Valdez143e8b32016-07-11 13:19:03 -04001863 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1864 return 0;
1865 }
1866
Adam Langleybdd5d662015-07-20 16:19:08 -07001867 CBS ec_point_format_list;
1868 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1869 CBS_len(contents) != 0) {
1870 return 0;
1871 }
1872
David Benjaminfc059942015-07-30 23:01:59 -04001873 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1874 * point format. */
1875 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1876 CBS_len(&ec_point_format_list)) == NULL) {
1877 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001878 return 0;
1879 }
1880
1881 return 1;
1882}
1883
1884static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1885 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001886 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1887 return 1;
1888 }
1889
Adam Langleybdd5d662015-07-20 16:19:08 -07001890 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1891}
1892
1893static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001894 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1895 return 1;
1896 }
1897
Adam Langleybdd5d662015-07-20 16:19:08 -07001898 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1899 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001900 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001901
1902 if (!using_ecc) {
1903 return 1;
1904 }
1905
1906 return ext_ec_point_add_extension(ssl, out);
1907}
1908
Steven Valdez4aa154e2016-07-29 14:32:55 -04001909/* Pre Shared Key
1910 *
Steven Valdez5b986082016-09-01 12:29:49 -04001911 * https://tools.ietf.org/html/draft-ietf-tls-tls13-15 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001912
1913static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
1914 uint16_t min_version, max_version;
1915 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1916 return 0;
1917 }
1918
David Benjamin2dc02042016-09-19 19:57:37 -04001919 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001920 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04001921 !ssl->method->version_from_wire(&session_version,
1922 ssl->session->ssl_version) ||
1923 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001924 return 1;
1925 }
1926
Steven Valdez5b986082016-09-01 12:29:49 -04001927 CBB contents, identity, ke_modes, auth_modes, ticket;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001928 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1929 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04001930 !CBB_add_u16_length_prefixed(&contents, &identity) ||
1931 !CBB_add_u8_length_prefixed(&identity, &ke_modes) ||
1932 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE) ||
1933 !CBB_add_u8_length_prefixed(&identity, &auth_modes) ||
1934 !CBB_add_u8(&auth_modes, SSL_PSK_AUTH) ||
1935 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
1936 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdez4aa154e2016-07-29 14:32:55 -04001937 ssl->session->tlsext_ticklen)) {
1938 return 0;
1939 }
1940
1941 return CBB_flush(out);
1942}
1943
1944int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1945 CBS *contents) {
1946 uint16_t psk_id;
1947 if (!CBS_get_u16(contents, &psk_id) ||
1948 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001949 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001950 *out_alert = SSL_AD_DECODE_ERROR;
1951 return 0;
1952 }
1953
1954 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04001955 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001956 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
1957 return 0;
1958 }
1959
1960 return 1;
1961}
1962
1963int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
1964 SSL_SESSION **out_session,
1965 uint8_t *out_alert,
1966 CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04001967 /* We only process the first PSK identity since we don't support pure PSK. */
1968 CBS identity, ke_modes, auth_modes, ticket;
1969 if (!CBS_get_u16_length_prefixed(contents, &identity) ||
1970 !CBS_get_u8_length_prefixed(&identity, &ke_modes) ||
1971 !CBS_get_u8_length_prefixed(&identity, &auth_modes) ||
1972 !CBS_get_u16_length_prefixed(&identity, &ticket) ||
1973 CBS_len(&identity) != 0) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001974 *out_alert = SSL_AD_DECODE_ERROR;
1975 return 0;
1976 }
1977
Steven Valdez5b986082016-09-01 12:29:49 -04001978 /* We only support tickets with PSK_DHE_KE and PSK_AUTH. */
1979 if (memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, CBS_len(&ke_modes)) == NULL ||
1980 memchr(CBS_data(&auth_modes), SSL_PSK_AUTH, CBS_len(&auth_modes)) ==
1981 NULL) {
1982 *out_session = NULL;
1983 return 1;
1984 }
1985
Steven Valdez4aa154e2016-07-29 14:32:55 -04001986 /* TLS 1.3 session tickets are renewed separately as part of the
1987 * NewSessionTicket. */
1988 int renew;
Steven Valdez5b986082016-09-01 12:29:49 -04001989 return tls_process_ticket(ssl, out_session, &renew, CBS_data(&ticket),
1990 CBS_len(&ticket), NULL, 0);
Steven Valdez4aa154e2016-07-29 14:32:55 -04001991}
1992
1993int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
1994 if (!ssl->s3->session_reused) {
1995 return 1;
1996 }
1997
1998 CBB contents;
1999 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2000 !CBB_add_u16_length_prefixed(out, &contents) ||
2001 /* We only consider the first identity for resumption */
2002 !CBB_add_u16(&contents, 0) ||
2003 !CBB_flush(out)) {
2004 return 0;
2005 }
2006
2007 return 1;
2008}
2009
2010
Steven Valdez143e8b32016-07-11 13:19:03 -04002011/* Key Share
2012 *
2013 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
2014
2015static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
2016 uint16_t min_version, max_version;
2017 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2018 return 0;
2019 }
2020
2021 if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
2022 return 1;
2023 }
2024
2025 CBB contents, kse_bytes;
2026 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2027 !CBB_add_u16_length_prefixed(out, &contents) ||
2028 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2029 return 0;
2030 }
2031
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002032 uint16_t group_id;
Steven Valdez5440fe02016-07-18 12:40:30 -04002033 if (ssl->s3->hs->retry_group) {
2034 /* Append the new key share to the old list. */
2035 if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
2036 ssl->s3->hs->key_share_bytes_len)) {
2037 return 0;
2038 }
2039 OPENSSL_free(ssl->s3->hs->key_share_bytes);
2040 ssl->s3->hs->key_share_bytes = NULL;
David Benjamin4fe3c902016-08-16 02:17:03 -04002041 ssl->s3->hs->key_share_bytes_len = 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002042
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002043 group_id = ssl->s3->hs->retry_group;
Steven Valdez5440fe02016-07-18 12:40:30 -04002044 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002045 /* Add a fake group. See draft-davidben-tls-grease-01. */
2046 if (ssl->ctx->grease_enabled &&
2047 (!CBB_add_u16(&kse_bytes,
2048 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2049 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2050 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2051 return 0;
2052 }
2053
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002054 /* Predict the most preferred group. */
2055 const uint16_t *groups;
2056 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002057 tls1_get_grouplist(ssl, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002058 if (groups_len == 0) {
2059 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2060 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002061 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002062
2063 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002064 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002065
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002066 CBB key_exchange;
2067 if (!CBB_add_u16(&kse_bytes, group_id) ||
2068 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2069 !SSL_ECDH_CTX_init(&ssl->s3->hs->ecdh_ctx, group_id) ||
2070 !SSL_ECDH_CTX_offer(&ssl->s3->hs->ecdh_ctx, &key_exchange) ||
2071 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002072 return 0;
2073 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002074
Steven Valdez5440fe02016-07-18 12:40:30 -04002075 if (!ssl->s3->hs->retry_group) {
2076 /* Save the contents of the extension to repeat it in the second
2077 * ClientHello. */
2078 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2079 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2080 CBB_len(&kse_bytes));
2081 if (ssl->s3->hs->key_share_bytes == NULL) {
2082 return 0;
2083 }
2084 }
2085
Steven Valdez143e8b32016-07-11 13:19:03 -04002086 return CBB_flush(out);
2087}
2088
Steven Valdez7259f2f2016-08-02 16:55:05 -04002089int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2090 size_t *out_secret_len,
2091 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002092 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002093 uint16_t group_id;
2094 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002095 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2096 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002097 *out_alert = SSL_AD_DECODE_ERROR;
2098 return 0;
2099 }
2100
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002101 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002102 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2103 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2104 return 0;
2105 }
2106
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002107 if (!SSL_ECDH_CTX_finish(&ssl->s3->hs->ecdh_ctx, out_secret, out_secret_len,
2108 out_alert, CBS_data(&peer_key),
2109 CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002110 *out_alert = SSL_AD_INTERNAL_ERROR;
2111 return 0;
2112 }
2113
David Benjamin5c4e8572016-08-19 17:44:53 -04002114 ssl->s3->new_session->key_exchange_info = group_id;
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002115 SSL_ECDH_CTX_cleanup(&ssl->s3->hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002116 return 1;
2117}
2118
Steven Valdez7259f2f2016-08-02 16:55:05 -04002119int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2120 uint8_t **out_secret,
2121 size_t *out_secret_len,
2122 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002123 uint16_t group_id;
2124 CBS key_shares;
Steven Valdez803c77a2016-09-06 14:13:43 -04002125 if (!tls1_get_shared_group(ssl, &group_id)) {
2126 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2127 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2128 return 0;
2129 }
2130
2131 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002132 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002133 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002134 return 0;
2135 }
2136
David Benjamin7e1f9842016-09-20 19:24:40 -04002137 /* Find the corresponding key share. */
2138 int found = 0;
2139 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002140 while (CBS_len(&key_shares) > 0) {
2141 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002142 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002143 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002144 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2145 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002146 return 0;
2147 }
2148
David Benjamin7e1f9842016-09-20 19:24:40 -04002149 if (id == group_id) {
2150 if (found) {
2151 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2152 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2153 return 0;
2154 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002155
David Benjamin7e1f9842016-09-20 19:24:40 -04002156 found = 1;
2157 peer_key = peer_key_tmp;
2158 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002159 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002160 }
2161
David Benjamin7e1f9842016-09-20 19:24:40 -04002162 if (!found) {
2163 *out_found = 0;
2164 *out_secret = NULL;
2165 *out_secret_len = 0;
2166 return 1;
2167 }
2168
2169 /* Compute the DH secret. */
2170 uint8_t *secret = NULL;
2171 size_t secret_len;
2172 SSL_ECDH_CTX group;
2173 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2174 CBB public_key;
2175 if (!CBB_init(&public_key, 32) ||
2176 !SSL_ECDH_CTX_init(&group, group_id) ||
2177 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len,
2178 out_alert, CBS_data(&peer_key),
2179 CBS_len(&peer_key)) ||
2180 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2181 &ssl->s3->hs->public_key_len)) {
2182 OPENSSL_free(secret);
2183 SSL_ECDH_CTX_cleanup(&group);
2184 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002185 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002186 return 0;
2187 }
2188
2189 SSL_ECDH_CTX_cleanup(&group);
2190
2191 *out_secret = secret;
2192 *out_secret_len = secret_len;
2193 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002194 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002195}
2196
Steven Valdez7259f2f2016-08-02 16:55:05 -04002197int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002198 uint16_t group_id;
2199 CBB kse_bytes, public_key;
2200 if (!tls1_get_shared_group(ssl, &group_id) ||
2201 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2202 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2203 !CBB_add_u16(&kse_bytes, group_id) ||
2204 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2205 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2206 ssl->s3->hs->public_key_len) ||
2207 !CBB_flush(out)) {
2208 return 0;
2209 }
2210
David Benjamin4fe3c902016-08-16 02:17:03 -04002211 OPENSSL_free(ssl->s3->hs->public_key);
2212 ssl->s3->hs->public_key = NULL;
2213 ssl->s3->hs->public_key_len = 0;
2214
David Benjamin5c4e8572016-08-19 17:44:53 -04002215 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002216 return 1;
2217}
2218
2219
Steven Valdezfdd10992016-09-15 16:27:05 -04002220/* Supported Versions
2221 *
2222 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2223
2224static int ext_supported_versions_add_clienthello(SSL *ssl, CBB *out) {
2225 uint16_t min_version, max_version;
2226 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2227 return 0;
2228 }
2229
2230 if (max_version <= TLS1_2_VERSION) {
2231 return 1;
2232 }
2233
2234 CBB contents, versions;
2235 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2236 !CBB_add_u16_length_prefixed(out, &contents) ||
2237 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2238 return 0;
2239 }
2240
David Benjamind9791bf2016-09-27 16:39:52 -04002241 /* Add a fake version. See draft-davidben-tls-grease-01. */
2242 if (ssl->ctx->grease_enabled &&
2243 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2244 return 0;
2245 }
2246
Steven Valdezfdd10992016-09-15 16:27:05 -04002247 for (uint16_t version = max_version; version >= min_version; version--) {
2248 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2249 return 0;
2250 }
2251 }
2252
2253 if (!CBB_flush(out)) {
2254 return 0;
2255 }
2256
2257 return 1;
2258}
2259
2260
Steven Valdezce902a92016-05-17 11:47:53 -04002261/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002262 *
Steven Valdezce902a92016-05-17 11:47:53 -04002263 * https://tools.ietf.org/html/rfc4492#section-5.1.2
2264 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07002265
Steven Valdezce902a92016-05-17 11:47:53 -04002266static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002267 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2268 return 1;
2269 }
2270
Steven Valdezce902a92016-05-17 11:47:53 -04002271 CBB contents, groups_bytes;
2272 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002273 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002274 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002275 return 0;
2276 }
2277
David Benjamin65ac9972016-09-02 21:35:25 -04002278 /* Add a fake group. See draft-davidben-tls-grease-01. */
2279 if (ssl->ctx->grease_enabled &&
2280 !CBB_add_u16(&groups_bytes,
2281 ssl_get_grease_value(ssl, ssl_grease_group))) {
2282 return 0;
2283 }
2284
Steven Valdezce902a92016-05-17 11:47:53 -04002285 const uint16_t *groups;
2286 size_t groups_len;
David Benjaminf04976b2016-10-07 00:37:55 -04002287 tls1_get_grouplist(ssl, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002288
David Benjamin54091232016-09-05 12:47:25 -04002289 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002290 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002291 return 0;
2292 }
2293 }
2294
2295 return CBB_flush(out);
2296}
2297
Steven Valdezce902a92016-05-17 11:47:53 -04002298static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2299 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002300 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2301 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002302 return 1;
2303}
2304
Steven Valdezce902a92016-05-17 11:47:53 -04002305static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2306 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002307 if (contents == NULL) {
2308 return 1;
2309 }
2310
Steven Valdezce902a92016-05-17 11:47:53 -04002311 CBS supported_group_list;
2312 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2313 CBS_len(&supported_group_list) == 0 ||
2314 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002315 CBS_len(contents) != 0) {
2316 return 0;
2317 }
2318
David Benjamin43612b62016-10-07 00:41:50 -04002319 ssl->s3->hs->peer_supported_group_list = OPENSSL_malloc(
Steven Valdezce902a92016-05-17 11:47:53 -04002320 CBS_len(&supported_group_list));
David Benjamin43612b62016-10-07 00:41:50 -04002321 if (ssl->s3->hs->peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002322 *out_alert = SSL_AD_INTERNAL_ERROR;
2323 return 0;
2324 }
2325
Steven Valdezce902a92016-05-17 11:47:53 -04002326 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002327 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002328 if (!CBS_get_u16(&supported_group_list,
David Benjamin43612b62016-10-07 00:41:50 -04002329 &ssl->s3->hs->peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002330 goto err;
2331 }
2332 }
2333
Steven Valdezce902a92016-05-17 11:47:53 -04002334 assert(CBS_len(&supported_group_list) == 0);
David Benjamin43612b62016-10-07 00:41:50 -04002335 ssl->s3->hs->peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002336
2337 return 1;
2338
2339err:
David Benjamin43612b62016-10-07 00:41:50 -04002340 OPENSSL_free(ssl->s3->hs->peer_supported_group_list);
2341 ssl->s3->hs->peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002342 *out_alert = SSL_AD_INTERNAL_ERROR;
2343 return 0;
2344}
2345
Steven Valdezce902a92016-05-17 11:47:53 -04002346static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002347 /* Servers don't echo this extension. */
2348 return 1;
2349}
2350
2351
Adam Langley614c66a2015-06-12 15:26:58 -07002352/* kExtensions contains all the supported extensions. */
2353static const struct tls_extension kExtensions[] = {
2354 {
Adam Langley5021b222015-06-12 18:27:58 -07002355 TLSEXT_TYPE_renegotiate,
2356 NULL,
2357 ext_ri_add_clienthello,
2358 ext_ri_parse_serverhello,
2359 ext_ri_parse_clienthello,
2360 ext_ri_add_serverhello,
2361 },
2362 {
Adam Langley614c66a2015-06-12 15:26:58 -07002363 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002364 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002365 ext_sni_add_clienthello,
2366 ext_sni_parse_serverhello,
2367 ext_sni_parse_clienthello,
2368 ext_sni_add_serverhello,
2369 },
Adam Langley0a056712015-07-01 15:03:33 -07002370 {
2371 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002372 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002373 ext_ems_add_clienthello,
2374 ext_ems_parse_serverhello,
2375 ext_ems_parse_clienthello,
2376 ext_ems_add_serverhello,
2377 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002378 {
2379 TLSEXT_TYPE_session_ticket,
2380 NULL,
2381 ext_ticket_add_clienthello,
2382 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002383 /* Ticket extension client parsing is handled in ssl_session.c */
2384 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002385 ext_ticket_add_serverhello,
2386 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002387 {
2388 TLSEXT_TYPE_signature_algorithms,
2389 NULL,
2390 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002391 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002392 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002393 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002394 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002395 {
2396 TLSEXT_TYPE_status_request,
2397 ext_ocsp_init,
2398 ext_ocsp_add_clienthello,
2399 ext_ocsp_parse_serverhello,
2400 ext_ocsp_parse_clienthello,
2401 ext_ocsp_add_serverhello,
2402 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002403 {
2404 TLSEXT_TYPE_next_proto_neg,
David Benjaminb74b0812016-10-06 19:43:48 -04002405 NULL,
Adam Langley97dfcbf2015-07-01 18:35:20 -07002406 ext_npn_add_clienthello,
2407 ext_npn_parse_serverhello,
2408 ext_npn_parse_clienthello,
2409 ext_npn_add_serverhello,
2410 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002411 {
2412 TLSEXT_TYPE_certificate_timestamp,
2413 NULL,
2414 ext_sct_add_clienthello,
2415 ext_sct_parse_serverhello,
2416 ext_sct_parse_clienthello,
2417 ext_sct_add_serverhello,
2418 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002419 {
2420 TLSEXT_TYPE_application_layer_protocol_negotiation,
2421 ext_alpn_init,
2422 ext_alpn_add_clienthello,
2423 ext_alpn_parse_serverhello,
2424 ext_alpn_parse_clienthello,
2425 ext_alpn_add_serverhello,
2426 },
Adam Langley49c7af12015-07-10 14:33:46 -07002427 {
2428 TLSEXT_TYPE_channel_id,
2429 ext_channel_id_init,
2430 ext_channel_id_add_clienthello,
2431 ext_channel_id_parse_serverhello,
2432 ext_channel_id_parse_clienthello,
2433 ext_channel_id_add_serverhello,
2434 },
Adam Langley391250d2015-07-15 19:06:07 -07002435 {
2436 TLSEXT_TYPE_srtp,
2437 ext_srtp_init,
2438 ext_srtp_add_clienthello,
2439 ext_srtp_parse_serverhello,
2440 ext_srtp_parse_clienthello,
2441 ext_srtp_add_serverhello,
2442 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002443 {
2444 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002445 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002446 ext_ec_point_add_clienthello,
2447 ext_ec_point_parse_serverhello,
2448 ext_ec_point_parse_clienthello,
2449 ext_ec_point_add_serverhello,
2450 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002451 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002452 TLSEXT_TYPE_key_share,
2453 NULL,
2454 ext_key_share_add_clienthello,
2455 forbid_parse_serverhello,
2456 ignore_parse_clienthello,
2457 dont_add_serverhello,
2458 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002459 {
2460 TLSEXT_TYPE_pre_shared_key,
2461 NULL,
2462 ext_pre_shared_key_add_clienthello,
2463 forbid_parse_serverhello,
2464 ignore_parse_clienthello,
2465 dont_add_serverhello,
2466 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002467 {
2468 TLSEXT_TYPE_supported_versions,
2469 NULL,
2470 ext_supported_versions_add_clienthello,
2471 forbid_parse_serverhello,
2472 ignore_parse_clienthello,
2473 dont_add_serverhello,
2474 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002475 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2476 * intolerant to the last extension being zero-length. See
2477 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002478 {
Steven Valdezce902a92016-05-17 11:47:53 -04002479 TLSEXT_TYPE_supported_groups,
David Benjamin43612b62016-10-07 00:41:50 -04002480 NULL,
Steven Valdezce902a92016-05-17 11:47:53 -04002481 ext_supported_groups_add_clienthello,
2482 ext_supported_groups_parse_serverhello,
2483 ext_supported_groups_parse_clienthello,
2484 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002485 },
Adam Langley614c66a2015-06-12 15:26:58 -07002486};
2487
2488#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2489
Adam Langley4cfa96b2015-07-01 11:56:55 -07002490OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002491 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002492 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002493OPENSSL_COMPILE_ASSERT(
2494 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2495 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002496
Adam Langley614c66a2015-06-12 15:26:58 -07002497static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2498 uint16_t value) {
2499 unsigned i;
2500 for (i = 0; i < kNumExtensions; i++) {
2501 if (kExtensions[i].value == value) {
2502 *out_index = i;
2503 return &kExtensions[i];
2504 }
2505 }
2506
2507 return NULL;
2508}
2509
Adam Langley09505632015-07-30 18:10:13 -07002510int SSL_extension_supported(unsigned extension_value) {
2511 uint32_t index;
2512 return extension_value == TLSEXT_TYPE_padding ||
2513 tls_extension_find(&index, extension_value) != NULL;
2514}
2515
David Benjamine8d53502015-10-10 14:13:23 -04002516int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002517 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002518 if (ssl->client_version == SSL3_VERSION &&
2519 !ssl->s3->send_connection_binding) {
2520 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002521 }
Adam Langley95c29f32014-06-20 12:00:00 -07002522
David Benjamine8d53502015-10-10 14:13:23 -04002523 CBB extensions;
2524 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002525 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002526 }
Adam Langley95c29f32014-06-20 12:00:00 -07002527
David Benjaminf5d2cd02016-10-06 19:39:20 -04002528 ssl->s3->hs->extensions.sent = 0;
2529 ssl->s3->hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002530
David Benjamin54091232016-09-05 12:47:25 -04002531 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002532 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002533 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002534 }
2535 }
Adam Langley95c29f32014-06-20 12:00:00 -07002536
David Benjamin65ac9972016-09-02 21:35:25 -04002537 uint16_t grease_ext1 = 0;
2538 if (ssl->ctx->grease_enabled) {
2539 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2540 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2541 if (!CBB_add_u16(&extensions, grease_ext1) ||
2542 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2543 goto err;
2544 }
2545 }
2546
David Benjamin54091232016-09-05 12:47:25 -04002547 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002548 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002549 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002550 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2551 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2552 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002553 }
Adam Langley95c29f32014-06-20 12:00:00 -07002554
Adam Langley33ad2b52015-07-20 17:43:53 -07002555 if (CBB_len(&extensions) != len_before) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002556 ssl->s3->hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002557 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002558 }
Adam Langley75712922014-10-10 16:23:43 -07002559
David Benjamine8d53502015-10-10 14:13:23 -04002560 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002561 goto err;
2562 }
2563
David Benjamin65ac9972016-09-02 21:35:25 -04002564 if (ssl->ctx->grease_enabled) {
2565 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2566 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2567
2568 /* The two fake extensions must not have the same value. GREASE values are
2569 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2570 * one. */
2571 if (grease_ext1 == grease_ext2) {
2572 grease_ext2 ^= 0x1010;
2573 }
2574
2575 if (!CBB_add_u16(&extensions, grease_ext2) ||
2576 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2577 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2578 goto err;
2579 }
2580 }
2581
David Benjamince079fd2016-08-02 16:22:34 -04002582 if (!SSL_is_dtls(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002583 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002584 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002585 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002586 *
2587 * NB: because this code works out the length of all existing extensions
2588 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002589 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002590 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002591 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002592 * Server 7.0 is intolerant to the last extension being zero-length. See
2593 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002594 if (padding_len >= 4 + 1) {
2595 padding_len -= 4;
2596 } else {
2597 padding_len = 1;
2598 }
Adam Langley95c29f32014-06-20 12:00:00 -07002599
Adam Langley33ad2b52015-07-20 17:43:53 -07002600 uint8_t *padding_bytes;
2601 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2602 !CBB_add_u16(&extensions, padding_len) ||
2603 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2604 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002605 }
Adam Langley75712922014-10-10 16:23:43 -07002606
Adam Langley33ad2b52015-07-20 17:43:53 -07002607 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002608 }
2609 }
Adam Langley75712922014-10-10 16:23:43 -07002610
David Benjamina01deee2015-12-08 18:56:31 -05002611 /* Discard empty extensions blocks. */
2612 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002613 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002614 }
2615
David Benjamine8d53502015-10-10 14:13:23 -04002616 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002617
2618err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002619 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002620 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002621}
Adam Langley95c29f32014-06-20 12:00:00 -07002622
David Benjamin56380462015-10-10 14:59:09 -04002623int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002624 CBB extensions;
2625 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002626 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002627 }
2628
2629 unsigned i;
2630 for (i = 0; i < kNumExtensions; i++) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002631 if (!(ssl->s3->hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002632 /* Don't send extensions that were not received. */
2633 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002634 }
Adam Langley95c29f32014-06-20 12:00:00 -07002635
David Benjamin56380462015-10-10 14:59:09 -04002636 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002637 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2638 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2639 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002640 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002641 }
Adam Langley95c29f32014-06-20 12:00:00 -07002642
David Benjamin56380462015-10-10 14:59:09 -04002643 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002644 goto err;
2645 }
2646
Steven Valdez143e8b32016-07-11 13:19:03 -04002647 /* Discard empty extensions blocks before TLS 1.3. */
2648 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2649 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002650 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002651 }
2652
David Benjamin56380462015-10-10 14:59:09 -04002653 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002654
2655err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002656 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002657 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002658}
Adam Langley95c29f32014-06-20 12:00:00 -07002659
David Benjamine14ff062016-08-09 16:21:24 -04002660static int ssl_scan_clienthello_tlsext(
2661 SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
2662 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002663 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002664 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002665 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002666 }
2667 }
2668
David Benjaminf5d2cd02016-10-06 19:39:20 -04002669 ssl->s3->hs->extensions.received = 0;
2670 ssl->s3->hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002671
David Benjamine14ff062016-08-09 16:21:24 -04002672 CBS extensions;
2673 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2674 while (CBS_len(&extensions) != 0) {
2675 uint16_t type;
2676 CBS extension;
2677
2678 /* Decode the next extension. */
2679 if (!CBS_get_u16(&extensions, &type) ||
2680 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002681 *out_alert = SSL_AD_DECODE_ERROR;
2682 return 0;
2683 }
Adam Langley95c29f32014-06-20 12:00:00 -07002684
David Benjamine14ff062016-08-09 16:21:24 -04002685 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2686 * ambiguous. Ignore all but the renegotiation_info extension. */
2687 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2688 continue;
2689 }
Adam Langley95c29f32014-06-20 12:00:00 -07002690
David Benjamine14ff062016-08-09 16:21:24 -04002691 unsigned ext_index;
2692 const struct tls_extension *const ext =
2693 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002694
David Benjamine14ff062016-08-09 16:21:24 -04002695 if (ext == NULL) {
2696 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002697 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002698 return 0;
2699 }
David Benjamine14ff062016-08-09 16:21:24 -04002700 continue;
2701 }
2702
David Benjaminf5d2cd02016-10-06 19:39:20 -04002703 ssl->s3->hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002704 uint8_t alert = SSL_AD_DECODE_ERROR;
2705 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2706 *out_alert = alert;
2707 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2708 ERR_add_error_dataf("extension: %u", (unsigned)type);
2709 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002710 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002711 }
Adam Langley75712922014-10-10 16:23:43 -07002712
David Benjamin1deb41b2016-08-09 19:36:38 -04002713 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002714 if (ssl->s3->hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002715 continue;
2716 }
2717
2718 CBS *contents = NULL, fake_contents;
2719 static const uint8_t kFakeRenegotiateExtension[] = {0};
2720 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2721 ssl_client_cipher_list_contains_cipher(client_hello,
2722 SSL3_CK_SCSV & 0xffff)) {
2723 /* The renegotiation SCSV was received so pretend that we received a
2724 * renegotiation extension. */
2725 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2726 sizeof(kFakeRenegotiateExtension));
2727 contents = &fake_contents;
David Benjaminf5d2cd02016-10-06 19:39:20 -04002728 ssl->s3->hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002729 }
2730
2731 /* Extension wasn't observed so call the callback with a NULL
2732 * parameter. */
2733 uint8_t alert = SSL_AD_DECODE_ERROR;
2734 if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
2735 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2736 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2737 *out_alert = alert;
2738 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002739 }
2740 }
2741
Adam Langleyfcf25832014-12-18 17:42:32 -08002742 return 1;
2743}
Adam Langley95c29f32014-06-20 12:00:00 -07002744
David Benjamine14ff062016-08-09 16:21:24 -04002745int ssl_parse_clienthello_tlsext(
2746 SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002747 int alert = -1;
David Benjamine14ff062016-08-09 16:21:24 -04002748 if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002749 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002750 return 0;
2751 }
Adam Langley95c29f32014-06-20 12:00:00 -07002752
David Benjamin0d56f882015-12-19 17:05:56 -05002753 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002754 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002755 return 0;
2756 }
Adam Langley95c29f32014-06-20 12:00:00 -07002757
Adam Langleyfcf25832014-12-18 17:42:32 -08002758 return 1;
2759}
Adam Langley95c29f32014-06-20 12:00:00 -07002760
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002761OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2762
David Benjamin0d56f882015-12-19 17:05:56 -05002763static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002764 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2765 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2766 return 1;
2767 }
Adam Langley614c66a2015-06-12 15:26:58 -07002768
Steven Valdez143e8b32016-07-11 13:19:03 -04002769 /* Decode the extensions block and check it is valid. */
2770 CBS extensions;
2771 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2772 !tls1_check_duplicate_extensions(&extensions)) {
2773 *out_alert = SSL_AD_DECODE_ERROR;
2774 return 0;
2775 }
2776
2777 uint32_t received = 0;
2778 while (CBS_len(&extensions) != 0) {
2779 uint16_t type;
2780 CBS extension;
2781
2782 /* Decode the next extension. */
2783 if (!CBS_get_u16(&extensions, &type) ||
2784 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002785 *out_alert = SSL_AD_DECODE_ERROR;
2786 return 0;
2787 }
Adam Langley95c29f32014-06-20 12:00:00 -07002788
Steven Valdez143e8b32016-07-11 13:19:03 -04002789 unsigned ext_index;
2790 const struct tls_extension *const ext =
2791 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002792
Steven Valdez143e8b32016-07-11 13:19:03 -04002793 if (ext == NULL) {
2794 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002795 return 0;
2796 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002797 continue;
2798 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002799
David Benjaminf5d2cd02016-10-06 19:39:20 -04002800 if (!(ssl->s3->hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002801 type != TLSEXT_TYPE_renegotiate) {
2802 /* If the extension was never sent then it is illegal, except for the
2803 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002804 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2805 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002806 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002807 return 0;
2808 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002809
Steven Valdez143e8b32016-07-11 13:19:03 -04002810 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002811
Steven Valdez143e8b32016-07-11 13:19:03 -04002812 uint8_t alert = SSL_AD_DECODE_ERROR;
2813 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2814 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2815 ERR_add_error_dataf("extension: %u", (unsigned)type);
2816 *out_alert = alert;
2817 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002818 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002819 }
Adam Langley95c29f32014-06-20 12:00:00 -07002820
David Benjamin54091232016-09-05 12:47:25 -04002821 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002822 if (!(received & (1u << i))) {
2823 /* Extension wasn't observed so call the callback with a NULL
2824 * parameter. */
2825 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002826 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002827 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2828 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002829 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002830 return 0;
2831 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002832 }
2833 }
Adam Langley95c29f32014-06-20 12:00:00 -07002834
Adam Langleyfcf25832014-12-18 17:42:32 -08002835 return 1;
2836}
Adam Langley95c29f32014-06-20 12:00:00 -07002837
David Benjamin0d56f882015-12-19 17:05:56 -05002838static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002839 int ret = SSL_TLSEXT_ERR_NOACK;
2840 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002841
David Benjamin78f8aab2016-03-10 16:33:58 -05002842 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002843 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002844 ssl->ctx->tlsext_servername_arg);
2845 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002846 ret = ssl->initial_ctx->tlsext_servername_callback(
2847 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002848 }
Adam Langley95c29f32014-06-20 12:00:00 -07002849
Adam Langleyfcf25832014-12-18 17:42:32 -08002850 switch (ret) {
2851 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002852 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002853 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002854
Adam Langleyfcf25832014-12-18 17:42:32 -08002855 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002856 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002857 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002858
Adam Langleyfcf25832014-12-18 17:42:32 -08002859 case SSL_TLSEXT_ERR_NOACK:
David Benjamina0486782016-10-06 19:11:32 -04002860 ssl->s3->hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002861 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002862
Adam Langleyfcf25832014-12-18 17:42:32 -08002863 default:
2864 return 1;
2865 }
2866}
Adam Langleyed8270a2014-09-02 13:52:56 -07002867
David Benjamin0d56f882015-12-19 17:05:56 -05002868static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002869 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002870 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002871
David Benjamin78f8aab2016-03-10 16:33:58 -05002872 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002873 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002874 ssl->ctx->tlsext_servername_arg);
2875 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002876 ret = ssl->initial_ctx->tlsext_servername_callback(
2877 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002878 }
Adam Langley95c29f32014-06-20 12:00:00 -07002879
Adam Langleyfcf25832014-12-18 17:42:32 -08002880 switch (ret) {
2881 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002882 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002883 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002884
Adam Langleyfcf25832014-12-18 17:42:32 -08002885 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002886 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002887 return 1;
2888
2889 default:
2890 return 1;
2891 }
2892}
2893
David Benjamin0d56f882015-12-19 17:05:56 -05002894int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002895 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002896 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2897 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002898 return 0;
2899 }
2900
David Benjamin0d56f882015-12-19 17:05:56 -05002901 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002902 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002903 return 0;
2904 }
2905
2906 return 1;
2907}
Adam Langley95c29f32014-06-20 12:00:00 -07002908
David Benjamine3aa1d92015-06-16 15:34:50 -04002909int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002910 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002911 size_t ticket_len, const uint8_t *session_id,
2912 size_t session_id_len) {
2913 int ret = 1; /* Most errors are non-fatal. */
2914 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2915 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002916
David Benjamine3aa1d92015-06-16 15:34:50 -04002917 HMAC_CTX hmac_ctx;
2918 HMAC_CTX_init(&hmac_ctx);
2919 EVP_CIPHER_CTX cipher_ctx;
2920 EVP_CIPHER_CTX_init(&cipher_ctx);
2921
David Benjaminef1b0092015-11-21 14:05:44 -05002922 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002923 *out_session = NULL;
2924
Steven Valdez4aa154e2016-07-29 14:32:55 -04002925 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
2926 goto done;
2927 }
2928
David Benjamine3aa1d92015-06-16 15:34:50 -04002929 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2930 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002931 }
2932
David Benjaminadcc3952015-04-26 13:07:57 -04002933 /* Ensure there is room for the key name and the largest IV
2934 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2935 * the maximum IV length should be well under the minimum size for the
2936 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002937 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2938 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002939 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002940 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002941
David Benjamine3aa1d92015-06-16 15:34:50 -04002942 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002943 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2944 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2945 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002946 if (cb_ret < 0) {
2947 ret = 0;
2948 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002949 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002950 if (cb_ret == 0) {
2951 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002952 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002953 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002954 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002955 }
2956 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002957 /* Check the key name matches. */
2958 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2959 SSL_TICKET_KEY_NAME_LEN) != 0) {
2960 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002961 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002962 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2963 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002964 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002965 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2966 ssl_ctx->tlsext_tick_aes_key, iv)) {
2967 ret = 0;
2968 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002969 }
2970 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002971 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002972
David Benjamine3aa1d92015-06-16 15:34:50 -04002973 /* Check the MAC at the end of the ticket. */
2974 uint8_t mac[EVP_MAX_MD_SIZE];
2975 size_t mac_len = HMAC_size(&hmac_ctx);
2976 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002977 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002978 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002979 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002980 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2981 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04002982 int mac_ok =
2983 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
2984#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
2985 mac_ok = 1;
2986#endif
2987 if (!mac_ok) {
David Benjamine3aa1d92015-06-16 15:34:50 -04002988 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002989 }
2990
David Benjamine3aa1d92015-06-16 15:34:50 -04002991 /* Decrypt the session data. */
2992 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2993 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2994 mac_len;
2995 plaintext = OPENSSL_malloc(ciphertext_len);
2996 if (plaintext == NULL) {
2997 ret = 0;
2998 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002999 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003000 size_t plaintext_len;
3001#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3002 memcpy(plaintext, ciphertext, ciphertext_len);
3003 plaintext_len = ciphertext_len;
3004#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003005 if (ciphertext_len >= INT_MAX) {
3006 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003007 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003008 int len1, len2;
3009 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3010 (int)ciphertext_len) ||
3011 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
3012 ERR_clear_error(); /* Don't leave an error on the queue. */
3013 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003014 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003015 plaintext_len = (size_t)(len1 + len2);
3016#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003017
David Benjamine3aa1d92015-06-16 15:34:50 -04003018 /* Decode the session. */
David Benjaminfbc45d72016-09-22 01:21:24 -04003019 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003020 if (session == NULL) {
3021 ERR_clear_error(); /* Don't leave an error on the queue. */
3022 goto done;
3023 }
3024
3025 /* Copy the client's session ID into the new session, to denote the ticket has
3026 * been accepted. */
3027 memcpy(session->session_id, session_id, session_id_len);
3028 session->session_id_length = session_id_len;
3029
Steven Valdez4aa154e2016-07-29 14:32:55 -04003030 if (!ssl_session_is_context_valid(ssl, session) ||
3031 !ssl_session_is_time_valid(ssl, session)) {
3032 SSL_SESSION_free(session);
3033 session = NULL;
3034 }
3035
David Benjamine3aa1d92015-06-16 15:34:50 -04003036 *out_session = session;
3037
3038done:
3039 OPENSSL_free(plaintext);
3040 HMAC_CTX_cleanup(&hmac_ctx);
3041 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3042 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003043}
Adam Langley95c29f32014-06-20 12:00:00 -07003044
Steven Valdez0d62f262015-09-04 12:41:04 -04003045int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003046 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05003047 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003048 return 1;
3049 }
David Benjamincd996942014-07-20 16:23:51 -04003050
David Benjamin0fc37ef2016-08-17 15:29:46 -04003051 SSL_HANDSHAKE *hs = ssl->s3->hs;
3052 OPENSSL_free(hs->peer_sigalgs);
3053 hs->peer_sigalgs = NULL;
3054 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003055
3056 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003057 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003058 return 0;
3059 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003060 num_sigalgs /= 2;
3061
3062 /* supported_signature_algorithms in the certificate request is
3063 * allowed to be empty. */
3064 if (num_sigalgs == 0) {
3065 return 1;
3066 }
3067
Steven Valdez02563852016-06-23 13:33:05 -04003068 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3069 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003070 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3071 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003072 return 0;
3073 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003074 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003075
3076 CBS sigalgs;
3077 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003078 for (size_t i = 0; i < num_sigalgs; i++) {
3079 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003080 return 0;
3081 }
3082 }
Adam Langley95c29f32014-06-20 12:00:00 -07003083
Adam Langleyfcf25832014-12-18 17:42:32 -08003084 return 1;
3085}
David Benjaminec2f27d2014-11-13 19:17:25 -05003086
David Benjaminea9a0d52016-07-08 15:52:59 -07003087int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003088 CERT *cert = ssl->cert;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003089 SSL_HANDSHAKE *hs = ssl->s3->hs;
David Benjaminec2f27d2014-11-13 19:17:25 -05003090
Steven Valdezf0451ca2016-06-29 13:16:27 -04003091 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3092 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3093 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003094 int type = ssl_private_key_type(ssl);
3095 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003096 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003097 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003098 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003099 if (ssl_is_ecdsa_key_type(type)) {
3100 *out = SSL_SIGN_ECDSA_SHA1;
3101 return 1;
3102 }
3103 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3104 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003105 }
3106
Steven Valdezeff1e8d2016-07-06 14:24:47 -04003107 const uint16_t *sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003108 size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07003109 if (cert->sigalgs != NULL) {
3110 sigalgs = cert->sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003111 num_sigalgs = cert->num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003112 }
3113
David Benjamin0fc37ef2016-08-17 15:29:46 -04003114 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3115 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3116 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003117 /* If the client didn't specify any signature_algorithms extension then
3118 * we can assume that it supports SHA1. See
3119 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3120 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3121 SSL_SIGN_ECDSA_SHA1};
3122 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003123 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003124 }
3125
David Benjamin0fc37ef2016-08-17 15:29:46 -04003126 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003127 uint16_t sigalg = sigalgs[i];
3128 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3129 * negotiated. */
3130 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3131 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3132 continue;
3133 }
3134
David Benjamin0fc37ef2016-08-17 15:29:46 -04003135 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003136 if (sigalg == peer_sigalgs[j]) {
3137 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003138 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003139 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003140 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003141 }
Adam Langley95c29f32014-06-20 12:00:00 -07003142
David Benjaminea9a0d52016-07-08 15:52:59 -07003143 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3144 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003145}
Adam Langley95c29f32014-06-20 12:00:00 -07003146
David Benjamind6a4ae92015-08-06 11:10:51 -04003147int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
3148 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003149 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003150
3151 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003152 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
3153 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08003154 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003155
David Benjamind6a4ae92015-08-06 11:10:51 -04003156 static const char kClientIDMagic[] = "TLS Channel ID signature";
3157 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
3158
Steven Valdez87eab492016-06-27 16:34:59 -04003159 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003160 static const char kResumptionMagic[] = "Resumption";
3161 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
3162 if (ssl->session->original_handshake_hash_len == 0) {
3163 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3164 goto err;
3165 }
3166 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
3167 ssl->session->original_handshake_hash_len);
3168 }
3169
3170 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3171 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3172 sizeof(handshake_hash));
3173 if (handshake_hash_len < 0) {
3174 goto err;
3175 }
3176 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
3177 unsigned len_u;
3178 EVP_DigestFinal_ex(&ctx, out, &len_u);
3179 *out_len = len_u;
3180
3181 ret = 1;
3182
3183err:
3184 EVP_MD_CTX_cleanup(&ctx);
3185 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003186}
Adam Langley1258b6a2014-06-20 12:00:00 -07003187
3188/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003189 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003190 * data. */
3191int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003192 int digest_len;
3193 /* This function should never be called for a resumed session because the
3194 * handshake hashes that we wish to record are for the original, full
3195 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003196 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003197 return -1;
3198 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003199
Adam Langleyfcf25832014-12-18 17:42:32 -08003200 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003201 tls1_handshake_digest(
3202 ssl, ssl->s3->new_session->original_handshake_hash,
3203 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003204 if (digest_len < 0) {
3205 return -1;
3206 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003207
Steven Valdez87eab492016-06-27 16:34:59 -04003208 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003209
Adam Langleyfcf25832014-12-18 17:42:32 -08003210 return 1;
3211}