blob: efb3347e11a28efc9865af2d102481727d3b3dcb [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
Steven Valdez5440fe02016-07-18 12:40:30 -0400315void tls1_get_grouplist(SSL *ssl, int get_peer_groups,
316 const uint16_t **out_group_ids,
317 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400318 if (get_peer_groups) {
319 /* Only clients send a supported group list, so this function is only
320 * called on the server. */
David Benjamin0d56f882015-12-19 17:05:56 -0500321 assert(ssl->server);
Steven Valdezce902a92016-05-17 11:47:53 -0400322 *out_group_ids = ssl->s3->tmp.peer_supported_group_list;
323 *out_group_ids_len = ssl->s3->tmp.peer_supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800324 return;
325 }
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Steven Valdezce902a92016-05-17 11:47:53 -0400327 *out_group_ids = ssl->supported_group_list;
328 *out_group_ids_len = ssl->supported_group_list_len;
329 if (!*out_group_ids) {
330 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400331 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 }
333}
David Benjamined439582014-07-14 19:13:02 -0400334
Steven Valdezce902a92016-05-17 11:47:53 -0400335int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) {
336 const uint16_t *groups, *peer_groups, *pref, *supp;
337 size_t groups_len, peer_groups_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400338
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 /* Can't do anything on client side */
David Benjamin4298d772015-12-19 00:18:25 -0500340 if (ssl->server == 0) {
341 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800342 }
343
Steven Valdezce902a92016-05-17 11:47:53 -0400344 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
345 tls1_get_grouplist(ssl, 1 /* peer groups */, &peer_groups, &peer_groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800346
Steven Valdezce902a92016-05-17 11:47:53 -0400347 if (peer_groups_len == 0) {
348 /* Clients are not required to send a supported_groups extension. In this
349 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin4298d772015-12-19 00:18:25 -0500350 * section 4, paragraph 3.
351 *
352 * However, in the interests of compatibility, we will skip ECDH if the
353 * client didn't send an extension because we can't be sure that they'll
Steven Valdezce902a92016-05-17 11:47:53 -0400354 * support our favoured group. */
David Benjamin4298d772015-12-19 00:18:25 -0500355 return 0;
David Benjamin55a43642015-04-20 14:45:55 -0400356 }
357
David Benjamin4298d772015-12-19 00:18:25 -0500358 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400359 pref = groups;
360 pref_len = groups_len;
361 supp = peer_groups;
362 supp_len = peer_groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400363 } else {
Steven Valdezce902a92016-05-17 11:47:53 -0400364 pref = peer_groups;
365 pref_len = peer_groups_len;
366 supp = groups;
367 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400368 }
369
370 for (i = 0; i < pref_len; i++) {
371 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800372 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400373 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500374 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 }
376 }
377 }
378
David Benjamin4298d772015-12-19 00:18:25 -0500379 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800380}
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Steven Valdezce902a92016-05-17 11:47:53 -0400382int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400384 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800385
Steven Valdezce902a92016-05-17 11:47:53 -0400386 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
387 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 return 0;
389 }
390
David Benjamin54091232016-09-05 12:47:25 -0400391 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400392 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
393 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 return 0;
395 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 }
397
Steven Valdezce902a92016-05-17 11:47:53 -0400398 OPENSSL_free(*out_group_ids);
399 *out_group_ids = group_ids;
400 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800401
402 return 1;
403}
Adam Langley95c29f32014-06-20 12:00:00 -0700404
Alessandro Ghedini5fd18072016-09-28 21:04:25 +0100405int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
406 const char *curves) {
407 uint16_t *group_ids = NULL;
408 size_t ncurves = 0;
409
410 const char *col;
411 const char *ptr = curves;
412
413 do {
414 col = strchr(ptr, ':');
415
416 uint16_t group_id;
417 if (!ssl_name_to_group_id(&group_id, ptr,
418 col ? (size_t)(col - ptr) : strlen(ptr))) {
419 goto err;
420 }
421
422 uint16_t *new_group_ids = OPENSSL_realloc(group_ids,
423 (ncurves + 1) * sizeof(uint16_t));
424 if (new_group_ids == NULL) {
425 goto err;
426 }
427 group_ids = new_group_ids;
428
429 group_ids[ncurves] = group_id;
430 ncurves++;
431
432 if (col) {
433 ptr = col + 1;
434 }
435 } while (col);
436
437 OPENSSL_free(*out_group_ids);
438 *out_group_ids = group_ids;
439 *out_group_ids_len = ncurves;
440
441 return 1;
442
443err:
444 OPENSSL_free(group_ids);
445 return 0;
446}
447
Steven Valdezce902a92016-05-17 11:47:53 -0400448/* tls1_curve_params_from_ec_key sets |*out_group_id| and |*out_comp_id| to the
449 * TLS group ID and point format, respectively, for |ec|. It returns one on
David Benjamin072334d2014-07-13 16:24:27 -0400450 * success and zero on failure. */
Steven Valdezce902a92016-05-17 11:47:53 -0400451static int tls1_curve_params_from_ec_key(uint16_t *out_group_id,
Adam Langleyfcf25832014-12-18 17:42:32 -0800452 uint8_t *out_comp_id, EC_KEY *ec) {
453 int nid;
454 uint16_t id;
455 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700456
Adam Langleyfcf25832014-12-18 17:42:32 -0800457 if (ec == NULL) {
458 return 0;
459 }
Adam Langley95c29f32014-06-20 12:00:00 -0700460
Adam Langleyfcf25832014-12-18 17:42:32 -0800461 grp = EC_KEY_get0_group(ec);
462 if (grp == NULL) {
463 return 0;
464 }
David Benjamin072334d2014-07-13 16:24:27 -0400465
Steven Valdezce902a92016-05-17 11:47:53 -0400466 /* Determine group ID */
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 nid = EC_GROUP_get_curve_name(grp);
Steven Valdezce902a92016-05-17 11:47:53 -0400468 if (!ssl_nid_to_group_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 return 0;
470 }
David Benjamin072334d2014-07-13 16:24:27 -0400471
Steven Valdezce902a92016-05-17 11:47:53 -0400472 /* Set the named group ID. Arbitrary explicit groups are not supported. */
473 *out_group_id = id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800474
475 if (out_comp_id) {
476 if (EC_KEY_get0_public_key(ec) == NULL) {
477 return 0;
478 }
479 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
480 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
481 } else {
482 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
483 }
484 }
485
486 return 1;
487}
David Benjamin072334d2014-07-13 16:24:27 -0400488
Steven Valdezce902a92016-05-17 11:47:53 -0400489/* tls1_check_group_id returns one if |group_id| is consistent with both our
490 * and the peer's group preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400491 * preferences are checked; the peer (the server) does not send preferences. */
Steven Valdezce902a92016-05-17 11:47:53 -0400492int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
493 const uint16_t *groups;
494 size_t groups_len, i, get_peer_groups;
David Benjamin42e9a772014-09-02 23:18:44 -0400495
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 /* Check against our list, then the peer's list. */
Steven Valdezce902a92016-05-17 11:47:53 -0400497 for (get_peer_groups = 0; get_peer_groups <= 1; get_peer_groups++) {
498 if (get_peer_groups && !ssl->server) {
David Benjamin55a43642015-04-20 14:45:55 -0400499 /* Servers do not present a preference list so, if we are a client, only
500 * check our list. */
501 continue;
502 }
503
Steven Valdezce902a92016-05-17 11:47:53 -0400504 tls1_get_grouplist(ssl, get_peer_groups, &groups, &groups_len);
505 if (get_peer_groups && groups_len == 0) {
506 /* Clients are not required to send a supported_groups extension. In this
507 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin55a43642015-04-20 14:45:55 -0400508 * section 4, paragraph 3. */
509 continue;
510 }
Steven Valdezce902a92016-05-17 11:47:53 -0400511 for (i = 0; i < groups_len; i++) {
512 if (groups[i] == group_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800513 break;
514 }
515 }
Adam Langley95c29f32014-06-20 12:00:00 -0700516
Steven Valdezce902a92016-05-17 11:47:53 -0400517 if (i == groups_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 return 0;
519 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800520 }
David Benjamin033e5f42014-11-13 18:47:41 -0500521
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 return 1;
523}
David Benjamin033e5f42014-11-13 18:47:41 -0500524
David Benjamin0d56f882015-12-19 17:05:56 -0500525int tls1_check_ec_cert(SSL *ssl, X509 *x) {
David Benjamin75ea5bb2016-07-08 17:43:29 -0700526 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
527 /* In TLS 1.3, the ECDSA curve is negotiated via signature algorithms. */
528 return 1;
529 }
530
Adam Langleyfcf25832014-12-18 17:42:32 -0800531 EVP_PKEY *pkey = X509_get_pubkey(x);
David Benjamin75ea5bb2016-07-08 17:43:29 -0700532 if (pkey == NULL) {
533 return 0;
534 }
535
536 int ret = 0;
Steven Valdezce902a92016-05-17 11:47:53 -0400537 uint16_t group_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800538 uint8_t comp_id;
David Benjamin758d1272015-11-20 17:47:25 -0500539 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
540 if (ec_key == NULL ||
Steven Valdezce902a92016-05-17 11:47:53 -0400541 !tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) ||
542 !tls1_check_group_id(ssl, group_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400543 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800544 goto done;
545 }
546
547 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500548
549done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400550 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800551 return ret;
552}
David Benjamin42e9a772014-09-02 23:18:44 -0400553
Adam Langley95c29f32014-06-20 12:00:00 -0700554/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800555 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700556
Steven Valdez02563852016-06-23 13:33:05 -0400557static const uint16_t kDefaultSignatureAlgorithms[] = {
David Benjamin57e929f2016-08-30 00:30:38 -0400558 /* For now, do not ship RSA-PSS signature algorithms on Android's system
559 * BoringSSL. Once TLS 1.3 is finalized and the change in Chrome has stuck,
560 * restore them. */
561#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400562 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400563#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400564 SSL_SIGN_RSA_PKCS1_SHA512,
565 SSL_SIGN_ECDSA_SECP521R1_SHA512,
566
David Benjamin57e929f2016-08-30 00:30:38 -0400567#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400568 SSL_SIGN_RSA_PSS_SHA384,
David Benjamin57e929f2016-08-30 00:30:38 -0400569#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400570 SSL_SIGN_RSA_PKCS1_SHA384,
571 SSL_SIGN_ECDSA_SECP384R1_SHA384,
572
David Benjamin57e929f2016-08-30 00:30:38 -0400573#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400574 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400575#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400576 SSL_SIGN_RSA_PKCS1_SHA256,
577 SSL_SIGN_ECDSA_SECP256R1_SHA256,
578
579 SSL_SIGN_RSA_PKCS1_SHA1,
580 SSL_SIGN_ECDSA_SHA1,
581};
582
Steven Valdez02563852016-06-23 13:33:05 -0400583size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
584 *psigs = kDefaultSignatureAlgorithms;
Steven Valdezcb966542016-08-17 16:56:14 -0400585 return OPENSSL_ARRAY_SIZE(kDefaultSignatureAlgorithms);
Adam Langleyfcf25832014-12-18 17:42:32 -0800586}
Adam Langley95c29f32014-06-20 12:00:00 -0700587
David Benjamin887c3002016-07-08 16:15:32 -0700588int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
Steven Valdez02563852016-06-23 13:33:05 -0400589 const uint16_t *sent_sigs;
590 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800591
Adam Langleyfcf25832014-12-18 17:42:32 -0800592 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500593 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400594 for (i = 0; i < sent_sigslen; i++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400595 if (sigalg == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 break;
597 }
598 }
599
David Benjamin788be4a2015-10-30 17:50:57 -0400600 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400601 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800602 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
603 return 0;
604 }
605
Adam Langleyfcf25832014-12-18 17:42:32 -0800606 return 1;
607}
608
609/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
610 * supported or doesn't appear in supported signature algorithms. Unlike
611 * ssl_cipher_get_disabled this applies to a specific session and not global
612 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500613void ssl_set_client_disabled(SSL *ssl) {
614 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800615 int have_rsa = 0, have_ecdsa = 0;
616 c->mask_a = 0;
617 c->mask_k = 0;
618
Adam Langleyfcf25832014-12-18 17:42:32 -0800619 /* Now go through all signature algorithms seeing if we support any for RSA,
620 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin0fc37ef2016-08-17 15:29:46 -0400621 const uint16_t *sigalgs;
622 size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
623 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -0400624 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400625 case SSL_SIGN_RSA_PSS_SHA512:
626 case SSL_SIGN_RSA_PSS_SHA384:
627 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400628 case SSL_SIGN_RSA_PKCS1_SHA512:
629 case SSL_SIGN_RSA_PKCS1_SHA384:
630 case SSL_SIGN_RSA_PKCS1_SHA256:
631 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800632 have_rsa = 1;
633 break;
634
Steven Valdez02563852016-06-23 13:33:05 -0400635 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
636 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
637 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
638 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800639 have_ecdsa = 1;
640 break;
641 }
642 }
643
644 /* Disable auth if we don't include any appropriate signature algorithms. */
645 if (!have_rsa) {
646 c->mask_a |= SSL_aRSA;
647 }
648 if (!have_ecdsa) {
649 c->mask_a |= SSL_aECDSA;
650 }
651
652 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500653 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800654 c->mask_a |= SSL_aPSK;
655 c->mask_k |= SSL_kPSK;
656 }
657}
Adam Langley95c29f32014-06-20 12:00:00 -0700658
Adam Langley614c66a2015-06-12 15:26:58 -0700659/* tls_extension represents a TLS extension that is handled internally. The
660 * |init| function is called for each handshake, before any other functions of
661 * the extension. Then the add and parse callbacks are called as needed.
662 *
663 * The parse callbacks receive a |CBS| that contains the contents of the
664 * extension (i.e. not including the type and length bytes). If an extension is
665 * not received then the parse callbacks will be called with a NULL CBS so that
666 * they can do any processing needed to handle the absence of an extension.
667 *
668 * The add callbacks receive a |CBB| to which the extension can be appended but
669 * the function is responsible for appending the type and length bytes too.
670 *
671 * All callbacks return one for success and zero for error. If a parse function
672 * returns zero then a fatal alert with value |*out_alert| will be sent. If
673 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
674struct tls_extension {
675 uint16_t value;
676 void (*init)(SSL *ssl);
677
678 int (*add_clienthello)(SSL *ssl, CBB *out);
679 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
680
681 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
682 int (*add_serverhello)(SSL *ssl, CBB *out);
683};
684
Steven Valdez6b8509a2016-07-12 13:38:32 -0400685static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
686 if (contents != NULL) {
687 /* Servers MUST NOT send this extension. */
688 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
689 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
690 return 0;
691 }
692
693 return 1;
694}
695
696static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
697 /* This extension from the client is handled elsewhere. */
698 return 1;
699}
700
701static int dont_add_serverhello(SSL *ssl, CBB *out) {
702 return 1;
703}
Adam Langley614c66a2015-06-12 15:26:58 -0700704
705/* Server name indication (SNI).
706 *
707 * https://tools.ietf.org/html/rfc6066#section-3. */
708
Adam Langley614c66a2015-06-12 15:26:58 -0700709static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
710 if (ssl->tlsext_hostname == NULL) {
711 return 1;
712 }
713
714 CBB contents, server_name_list, name;
715 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
716 !CBB_add_u16_length_prefixed(out, &contents) ||
717 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
718 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
719 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
720 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
721 strlen(ssl->tlsext_hostname)) ||
722 !CBB_flush(out)) {
723 return 0;
724 }
725
726 return 1;
727}
728
David Benjamin0d56f882015-12-19 17:05:56 -0500729static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
730 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700731 if (contents == NULL) {
732 return 1;
733 }
734
735 if (CBS_len(contents) != 0) {
736 return 0;
737 }
738
739 assert(ssl->tlsext_hostname != NULL);
740
Steven Valdez87eab492016-06-27 16:34:59 -0400741 if (ssl->session == NULL) {
742 assert(ssl->s3->new_session->tlsext_hostname == NULL);
743 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
744 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700745 *out_alert = SSL_AD_INTERNAL_ERROR;
746 return 0;
747 }
748 }
749
750 return 1;
751}
752
David Benjamin0d56f882015-12-19 17:05:56 -0500753static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
754 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700755 if (contents == NULL) {
756 return 1;
757 }
758
David Benjamin9b611e22016-03-03 08:48:30 -0500759 CBS server_name_list, host_name;
760 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700761 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500762 !CBS_get_u8(&server_name_list, &name_type) ||
763 /* Although the server_name extension was intended to be extensible to
764 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
765 * different name types will cause an error. Further, RFC 4366 originally
766 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
767 * adding new name types is no longer feasible.
768 *
769 * Act as if the extensibility does not exist to simplify parsing. */
770 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
771 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700772 CBS_len(contents) != 0) {
773 return 0;
774 }
775
David Benjamin9b611e22016-03-03 08:48:30 -0500776 if (name_type != TLSEXT_NAMETYPE_host_name ||
777 CBS_len(&host_name) == 0 ||
778 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
779 CBS_contains_zero_byte(&host_name)) {
780 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
781 return 0;
782 }
Adam Langley614c66a2015-06-12 15:26:58 -0700783
David Benjamin9b611e22016-03-03 08:48:30 -0500784 /* TODO(davidben): SNI should be resolved before resumption. We have the
785 * early callback as a replacement, but we should fix the current callback
786 * and avoid the need for |SSL_CTX_set_session_id_context|. */
Steven Valdez87eab492016-06-27 16:34:59 -0400787 if (ssl->session == NULL) {
788 assert(ssl->s3->new_session->tlsext_hostname == NULL);
David Benjamin9b611e22016-03-03 08:48:30 -0500789
790 /* Copy the hostname as a string. */
Steven Valdez87eab492016-06-27 16:34:59 -0400791 if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) {
David Benjamin9b611e22016-03-03 08:48:30 -0500792 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700793 return 0;
794 }
795
David Benjamina0486782016-10-06 19:11:32 -0400796 ssl->s3->hs->should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700797 }
798
799 return 1;
800}
801
802static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez87eab492016-06-27 16:34:59 -0400803 if (ssl->session != NULL ||
David Benjamina0486782016-10-06 19:11:32 -0400804 !ssl->s3->hs->should_ack_sni ||
Steven Valdez87eab492016-06-27 16:34:59 -0400805 ssl->s3->new_session->tlsext_hostname == NULL) {
Adam Langley614c66a2015-06-12 15:26:58 -0700806 return 1;
807 }
808
809 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
810 !CBB_add_u16(out, 0 /* length */)) {
811 return 0;
812 }
813
814 return 1;
815}
816
817
Adam Langley5021b222015-06-12 18:27:58 -0700818/* Renegotiation indication.
819 *
820 * https://tools.ietf.org/html/rfc5746 */
821
822static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400823 uint16_t min_version, max_version;
824 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
825 return 0;
826 }
827
828 /* Renegotiation indication is not necessary in TLS 1.3. */
829 if (min_version >= TLS1_3_VERSION) {
830 return 1;
831 }
832
Adam Langley5021b222015-06-12 18:27:58 -0700833 CBB contents, prev_finished;
834 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
835 !CBB_add_u16_length_prefixed(out, &contents) ||
836 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
837 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
838 ssl->s3->previous_client_finished_len) ||
839 !CBB_flush(out)) {
840 return 0;
841 }
842
843 return 1;
844}
845
846static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
847 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400848 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
849 return 0;
850 }
851
David Benjamin3e052de2015-11-25 20:10:31 -0500852 /* Servers may not switch between omitting the extension and supporting it.
853 * See RFC 5746, sections 3.5 and 4.2. */
854 if (ssl->s3->initial_handshake_complete &&
855 (contents != NULL) != ssl->s3->send_connection_binding) {
856 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
857 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
858 return 0;
859 }
860
Adam Langley5021b222015-06-12 18:27:58 -0700861 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500862 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700863 * RI even on initial ServerHello because the client doesn't see any
864 * renegotiation during an attack. However this would mean we could not
865 * connect to any server which doesn't support RI.
866 *
David Benjamine9cddb82015-11-23 14:36:40 -0500867 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
868 * practical terms every client sets it so it's just assumed here. */
869 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700870 }
871
872 const size_t expected_len = ssl->s3->previous_client_finished_len +
873 ssl->s3->previous_server_finished_len;
874
875 /* Check for logic errors */
876 assert(!expected_len || ssl->s3->previous_client_finished_len);
877 assert(!expected_len || ssl->s3->previous_server_finished_len);
878
879 /* Parse out the extension contents. */
880 CBS renegotiated_connection;
881 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
882 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400883 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700884 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
885 return 0;
886 }
887
888 /* Check that the extension matches. */
889 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400890 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700891 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
892 return 0;
893 }
894
895 const uint8_t *d = CBS_data(&renegotiated_connection);
896 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
897 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400898 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700899 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
900 return 0;
901 }
902 d += ssl->s3->previous_client_finished_len;
903
904 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
905 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400906 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700907 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
908 return 0;
909 }
910 ssl->s3->send_connection_binding = 1;
911
912 return 1;
913}
914
915static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
916 CBS *contents) {
917 /* Renegotiation isn't supported as a server so this function should never be
918 * called after the initial handshake. */
919 assert(!ssl->s3->initial_handshake_complete);
920
Steven Valdez143e8b32016-07-11 13:19:03 -0400921 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
922 return 1;
923 }
924
Adam Langley5021b222015-06-12 18:27:58 -0700925 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400926 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700927 }
928
929 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700930 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
931 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400932 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700933 return 0;
934 }
935
936 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500937 if (!CBS_mem_equal(&renegotiated_connection,
938 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700939 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400940 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700941 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
942 return 0;
943 }
944
945 ssl->s3->send_connection_binding = 1;
946
947 return 1;
948}
949
950static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400951 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
952 return 1;
953 }
954
Adam Langley5021b222015-06-12 18:27:58 -0700955 CBB contents, prev_finished;
956 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
957 !CBB_add_u16_length_prefixed(out, &contents) ||
958 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
959 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
960 ssl->s3->previous_client_finished_len) ||
961 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
962 ssl->s3->previous_server_finished_len) ||
963 !CBB_flush(out)) {
964 return 0;
965 }
966
967 return 1;
968}
969
Adam Langley0a056712015-07-01 15:03:33 -0700970
971/* Extended Master Secret.
972 *
David Benjamin43946d42016-02-01 08:42:19 -0500973 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700974
Adam Langley0a056712015-07-01 15:03:33 -0700975static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400976 uint16_t min_version, max_version;
977 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
978 return 0;
979 }
980
981 /* Extended master secret is not necessary in TLS 1.3. */
982 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700983 return 1;
984 }
985
986 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
987 !CBB_add_u16(out, 0 /* length */)) {
988 return 0;
989 }
990
991 return 1;
992}
993
994static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
995 CBS *contents) {
David Benjamin163c9562016-08-29 23:14:17 -0400996 /* Whether EMS is negotiated may not change on renegotation. */
997 if (ssl->s3->initial_handshake_complete) {
998 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
999 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
1000 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1001 return 0;
1002 }
1003
1004 return 1;
1005 }
1006
Adam Langley0a056712015-07-01 15:03:33 -07001007 if (contents == NULL) {
1008 return 1;
1009 }
1010
Steven Valdez143e8b32016-07-11 13:19:03 -04001011 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1012 ssl->version == SSL3_VERSION) {
1013 return 0;
1014 }
1015
1016 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -07001017 return 0;
1018 }
1019
1020 ssl->s3->tmp.extended_master_secret = 1;
1021 return 1;
1022}
1023
David Benjamin0d56f882015-12-19 17:05:56 -05001024static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1025 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001026 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1027 ssl->version == SSL3_VERSION) {
1028 return 1;
1029 }
1030
1031 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -07001032 return 1;
1033 }
1034
1035 if (CBS_len(contents) != 0) {
1036 return 0;
1037 }
1038
1039 ssl->s3->tmp.extended_master_secret = 1;
1040 return 1;
1041}
1042
1043static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1044 if (!ssl->s3->tmp.extended_master_secret) {
1045 return 1;
1046 }
1047
1048 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1049 !CBB_add_u16(out, 0 /* length */)) {
1050 return 0;
1051 }
1052
1053 return 1;
1054}
1055
Adam Langley9b05bc52015-07-01 15:25:33 -07001056
1057/* Session tickets.
1058 *
1059 * https://tools.ietf.org/html/rfc5077 */
1060
1061static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -04001062 uint16_t min_version, max_version;
1063 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1064 return 0;
1065 }
1066
1067 /* TLS 1.3 uses a different ticket extension. */
1068 if (min_version >= TLS1_3_VERSION ||
1069 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001070 return 1;
1071 }
1072
1073 const uint8_t *ticket_data = NULL;
1074 int ticket_len = 0;
1075
1076 /* Renegotiation does not participate in session resumption. However, still
1077 * advertise the extension to avoid potentially breaking servers which carry
1078 * over the state from the previous handshake, such as OpenSSL servers
1079 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -04001080 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -07001081 if (!ssl->s3->initial_handshake_complete &&
1082 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001083 ssl->session->tlsext_tick != NULL &&
1084 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -04001085 ssl->method->version_from_wire(&session_version,
1086 ssl->session->ssl_version) &&
1087 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001088 ticket_data = ssl->session->tlsext_tick;
1089 ticket_len = ssl->session->tlsext_ticklen;
1090 }
1091
1092 CBB ticket;
1093 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1094 !CBB_add_u16_length_prefixed(out, &ticket) ||
1095 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1096 !CBB_flush(out)) {
1097 return 0;
1098 }
1099
1100 return 1;
1101}
1102
1103static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1104 CBS *contents) {
1105 ssl->tlsext_ticket_expected = 0;
1106
1107 if (contents == NULL) {
1108 return 1;
1109 }
1110
Steven Valdez143e8b32016-07-11 13:19:03 -04001111 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1112 return 0;
1113 }
1114
Adam Langley9b05bc52015-07-01 15:25:33 -07001115 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1116 * this function should never be called, even if the server tries to send the
1117 * extension. */
1118 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1119
1120 if (CBS_len(contents) != 0) {
1121 return 0;
1122 }
1123
1124 ssl->tlsext_ticket_expected = 1;
1125 return 1;
1126}
1127
Adam Langley9b05bc52015-07-01 15:25:33 -07001128static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1129 if (!ssl->tlsext_ticket_expected) {
1130 return 1;
1131 }
1132
1133 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1134 * true. */
1135 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1136
1137 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1138 !CBB_add_u16(out, 0 /* length */)) {
1139 return 0;
1140 }
1141
1142 return 1;
1143}
1144
1145
Adam Langley2e857bd2015-07-01 16:09:19 -07001146/* Signature Algorithms.
1147 *
1148 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1149
1150static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin2dc02042016-09-19 19:57:37 -04001151 uint16_t min_version, max_version;
1152 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1153 return 0;
1154 }
1155
1156 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001157 return 1;
1158 }
1159
David Benjamin0fc37ef2016-08-17 15:29:46 -04001160 const uint16_t *sigalgs;
1161 const size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001162
David Benjamin0fc37ef2016-08-17 15:29:46 -04001163 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001164 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1165 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001166 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001167 return 0;
1168 }
1169
David Benjamin0fc37ef2016-08-17 15:29:46 -04001170 for (size_t i = 0; i < num_sigalgs; i++) {
1171 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001172 return 0;
1173 }
1174 }
1175
1176 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001177 return 0;
1178 }
1179
1180 return 1;
1181}
1182
Adam Langley2e857bd2015-07-01 16:09:19 -07001183static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1184 CBS *contents) {
David Benjamin0fc37ef2016-08-17 15:29:46 -04001185 OPENSSL_free(ssl->s3->hs->peer_sigalgs);
1186 ssl->s3->hs->peer_sigalgs = NULL;
1187 ssl->s3->hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001188
Adam Langley2e857bd2015-07-01 16:09:19 -07001189 if (contents == NULL) {
1190 return 1;
1191 }
1192
1193 CBS supported_signature_algorithms;
1194 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001195 CBS_len(contents) != 0 ||
1196 CBS_len(&supported_signature_algorithms) == 0 ||
1197 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001198 return 0;
1199 }
1200
1201 return 1;
1202}
1203
Adam Langley2e857bd2015-07-01 16:09:19 -07001204
Adam Langleybb0bd042015-07-01 16:21:03 -07001205/* OCSP Stapling.
1206 *
1207 * https://tools.ietf.org/html/rfc6066#section-8 */
1208
1209static void ext_ocsp_init(SSL *ssl) {
Adam Langleyce9d85e2016-01-24 15:58:39 -08001210 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001211}
1212
1213static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1214 if (!ssl->ocsp_stapling_enabled) {
1215 return 1;
1216 }
1217
1218 CBB contents;
1219 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1220 !CBB_add_u16_length_prefixed(out, &contents) ||
1221 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1222 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1223 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1224 !CBB_flush(out)) {
1225 return 0;
1226 }
1227
Adam Langleyce9d85e2016-01-24 15:58:39 -08001228 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001229 return 1;
1230}
1231
1232static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001233 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001234 if (contents == NULL) {
1235 return 1;
1236 }
1237
David Benjamin942f4ed2016-07-16 19:03:49 +03001238 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001239 /* OCSP stapling is forbidden on non-certificate ciphers. */
1240 if (CBS_len(contents) != 0 ||
1241 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001242 return 0;
1243 }
1244
David Benjamindaa88502016-10-04 16:32:16 -04001245 /* Note this does not check for resumption in TLS 1.2. Sending
1246 * status_request here does not make sense, but OpenSSL does so and the
1247 * specification does not say anything. Tolerate it but ignore it. */
1248
David Benjamina0486782016-10-06 19:11:32 -04001249 ssl->s3->hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001250 return 1;
1251 }
1252
Steven Valdez803c77a2016-09-06 14:13:43 -04001253 /* In TLS 1.3, OCSP stapling is forbidden on resumption. */
1254 if (ssl->s3->session_reused) {
1255 return 0;
1256 }
1257
David Benjamin942f4ed2016-07-16 19:03:49 +03001258 uint8_t status_type;
1259 CBS ocsp_response;
1260 if (!CBS_get_u8(contents, &status_type) ||
1261 status_type != TLSEXT_STATUSTYPE_ocsp ||
1262 !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1263 CBS_len(&ocsp_response) == 0 ||
1264 CBS_len(contents) != 0) {
1265 return 0;
1266 }
1267
Steven Valdez87eab492016-06-27 16:34:59 -04001268 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1269 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001270 *out_alert = SSL_AD_INTERNAL_ERROR;
1271 return 0;
1272 }
1273
Adam Langleybb0bd042015-07-01 16:21:03 -07001274 return 1;
1275}
1276
1277static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1278 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001279 if (contents == NULL) {
1280 return 1;
1281 }
1282
1283 uint8_t status_type;
1284 if (!CBS_get_u8(contents, &status_type)) {
1285 return 0;
1286 }
1287
1288 /* We cannot decide whether OCSP stapling will occur yet because the correct
1289 * SSL_CTX might not have been selected. */
David Benjamina0486782016-10-06 19:11:32 -04001290 ssl->s3->hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001291
Adam Langleybb0bd042015-07-01 16:21:03 -07001292 return 1;
1293}
1294
1295static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
David Benjamina0486782016-10-06 19:11:32 -04001296 if (!ssl->s3->hs->ocsp_stapling_requested ||
David Benjamin942f4ed2016-07-16 19:03:49 +03001297 ssl->ctx->ocsp_response_length == 0 ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001298 ssl->s3->session_reused ||
1299 (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
1300 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001301 return 1;
1302 }
1303
David Benjamin942f4ed2016-07-16 19:03:49 +03001304 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1305 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez87eab492016-06-27 16:34:59 -04001306 if (ssl->session != NULL) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001307 return 1;
1308 }
1309
David Benjamina0486782016-10-06 19:11:32 -04001310 ssl->s3->hs->certificate_status_expected = 1;
David Benjamin942f4ed2016-07-16 19:03:49 +03001311
1312 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1313 CBB_add_u16(out, 0 /* length */);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001314 }
1315
David Benjamin942f4ed2016-07-16 19:03:49 +03001316 CBB body, ocsp_response;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001317 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
David Benjamin942f4ed2016-07-16 19:03:49 +03001318 CBB_add_u16_length_prefixed(out, &body) &&
1319 CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1320 CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1321 CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1322 ssl->ctx->ocsp_response_length) &&
1323 CBB_flush(out);
Adam Langleybb0bd042015-07-01 16:21:03 -07001324}
1325
1326
Adam Langley97dfcbf2015-07-01 18:35:20 -07001327/* Next protocol negotiation.
1328 *
1329 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1330
1331static void ext_npn_init(SSL *ssl) {
1332 ssl->s3->next_proto_neg_seen = 0;
1333}
1334
1335static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1336 if (ssl->s3->initial_handshake_complete ||
1337 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001338 (ssl->options & SSL_OP_DISABLE_NPN) ||
David Benjamince079fd2016-08-02 16:22:34 -04001339 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001340 return 1;
1341 }
1342
1343 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1344 !CBB_add_u16(out, 0 /* length */)) {
1345 return 0;
1346 }
1347
1348 return 1;
1349}
1350
1351static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1352 CBS *contents) {
1353 if (contents == NULL) {
1354 return 1;
1355 }
1356
Steven Valdez143e8b32016-07-11 13:19:03 -04001357 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1358 return 0;
1359 }
1360
Adam Langley97dfcbf2015-07-01 18:35:20 -07001361 /* If any of these are false then we should never have sent the NPN
1362 * extension in the ClientHello and thus this function should never have been
1363 * called. */
1364 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001365 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001366 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001367 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001368
David Benjamin76c2efc2015-08-31 14:24:29 -04001369 if (ssl->s3->alpn_selected != NULL) {
1370 /* NPN and ALPN may not be negotiated in the same connection. */
1371 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1372 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1373 return 0;
1374 }
1375
Adam Langley97dfcbf2015-07-01 18:35:20 -07001376 const uint8_t *const orig_contents = CBS_data(contents);
1377 const size_t orig_len = CBS_len(contents);
1378
1379 while (CBS_len(contents) != 0) {
1380 CBS proto;
1381 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1382 CBS_len(&proto) == 0) {
1383 return 0;
1384 }
1385 }
1386
1387 uint8_t *selected;
1388 uint8_t selected_len;
1389 if (ssl->ctx->next_proto_select_cb(
1390 ssl, &selected, &selected_len, orig_contents, orig_len,
1391 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1392 *out_alert = SSL_AD_INTERNAL_ERROR;
1393 return 0;
1394 }
1395
David Benjamin79978df2015-12-25 15:56:49 -05001396 OPENSSL_free(ssl->s3->next_proto_negotiated);
1397 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1398 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001399 *out_alert = SSL_AD_INTERNAL_ERROR;
1400 return 0;
1401 }
1402
David Benjamin79978df2015-12-25 15:56:49 -05001403 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001404 ssl->s3->next_proto_neg_seen = 1;
1405
1406 return 1;
1407}
1408
1409static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1410 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001411 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1412 return 1;
1413 }
1414
Adam Langley97dfcbf2015-07-01 18:35:20 -07001415 if (contents != NULL && CBS_len(contents) != 0) {
1416 return 0;
1417 }
1418
1419 if (contents == NULL ||
1420 ssl->s3->initial_handshake_complete ||
1421 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1422 * afterwards, parsing the ALPN extension will clear
1423 * |next_proto_neg_seen|. */
1424 ssl->s3->alpn_selected != NULL ||
1425 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001426 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001427 return 1;
1428 }
1429
1430 ssl->s3->next_proto_neg_seen = 1;
1431 return 1;
1432}
1433
1434static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1435 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1436 * parsed. */
1437 if (!ssl->s3->next_proto_neg_seen) {
1438 return 1;
1439 }
1440
1441 const uint8_t *npa;
1442 unsigned npa_len;
1443
1444 if (ssl->ctx->next_protos_advertised_cb(
1445 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1446 SSL_TLSEXT_ERR_OK) {
1447 ssl->s3->next_proto_neg_seen = 0;
1448 return 1;
1449 }
1450
1451 CBB contents;
1452 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1453 !CBB_add_u16_length_prefixed(out, &contents) ||
1454 !CBB_add_bytes(&contents, npa, npa_len) ||
1455 !CBB_flush(out)) {
1456 return 0;
1457 }
1458
1459 return 1;
1460}
1461
1462
Adam Langleyab8d87d2015-07-10 12:21:39 -07001463/* Signed certificate timestamps.
1464 *
1465 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1466
1467static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1468 if (!ssl->signed_cert_timestamps_enabled) {
1469 return 1;
1470 }
1471
1472 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1473 !CBB_add_u16(out, 0 /* length */)) {
1474 return 0;
1475 }
1476
1477 return 1;
1478}
1479
1480static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1481 CBS *contents) {
1482 if (contents == NULL) {
1483 return 1;
1484 }
1485
1486 /* If this is false then we should never have sent the SCT extension in the
1487 * ClientHello and thus this function should never have been called. */
1488 assert(ssl->signed_cert_timestamps_enabled);
1489
1490 if (CBS_len(contents) == 0) {
1491 *out_alert = SSL_AD_DECODE_ERROR;
1492 return 0;
1493 }
1494
David Benjamindaa88502016-10-04 16:32:16 -04001495 /* Session resumption uses the original session information. The extension
1496 * should not be sent on resumption, but RFC 6962 did not make it a
1497 * requirement, so tolerate this.
1498 *
1499 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001500 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001501 !CBS_stow(
1502 contents,
1503 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1504 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001505 *out_alert = SSL_AD_INTERNAL_ERROR;
1506 return 0;
1507 }
1508
1509 return 1;
1510}
1511
1512static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1513 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001514 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001515}
1516
1517static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001518 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001519 if (ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001520 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001521 return 1;
1522 }
1523
1524 CBB contents;
1525 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1526 CBB_add_u16_length_prefixed(out, &contents) &&
1527 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1528 ssl->ctx->signed_cert_timestamp_list_length) &&
1529 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001530}
1531
1532
Adam Langleyf18e4532015-07-10 13:39:53 -07001533/* Application-level Protocol Negotiation.
1534 *
1535 * https://tools.ietf.org/html/rfc7301 */
1536
1537static void ext_alpn_init(SSL *ssl) {
1538 OPENSSL_free(ssl->s3->alpn_selected);
1539 ssl->s3->alpn_selected = NULL;
1540}
1541
1542static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1543 if (ssl->alpn_client_proto_list == NULL ||
1544 ssl->s3->initial_handshake_complete) {
1545 return 1;
1546 }
1547
1548 CBB contents, proto_list;
1549 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1550 !CBB_add_u16_length_prefixed(out, &contents) ||
1551 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1552 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1553 ssl->alpn_client_proto_list_len) ||
1554 !CBB_flush(out)) {
1555 return 0;
1556 }
1557
1558 return 1;
1559}
1560
1561static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1562 CBS *contents) {
1563 if (contents == NULL) {
1564 return 1;
1565 }
1566
1567 assert(!ssl->s3->initial_handshake_complete);
1568 assert(ssl->alpn_client_proto_list != NULL);
1569
David Benjamin76c2efc2015-08-31 14:24:29 -04001570 if (ssl->s3->next_proto_neg_seen) {
1571 /* NPN and ALPN may not be negotiated in the same connection. */
1572 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1573 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1574 return 0;
1575 }
1576
Adam Langleyf18e4532015-07-10 13:39:53 -07001577 /* The extension data consists of a ProtocolNameList which must have
1578 * exactly one ProtocolName. Each of these is length-prefixed. */
1579 CBS protocol_name_list, protocol_name;
1580 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1581 CBS_len(contents) != 0 ||
1582 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1583 /* Empty protocol names are forbidden. */
1584 CBS_len(&protocol_name) == 0 ||
1585 CBS_len(&protocol_name_list) != 0) {
1586 return 0;
1587 }
1588
David Benjamin3e517572016-08-11 11:52:23 -04001589 /* Check that the protcol name is one of the ones we advertised. */
1590 int protocol_ok = 0;
1591 CBS client_protocol_name_list, client_protocol_name;
1592 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1593 ssl->alpn_client_proto_list_len);
1594 while (CBS_len(&client_protocol_name_list) > 0) {
1595 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1596 &client_protocol_name)) {
1597 *out_alert = SSL_AD_INTERNAL_ERROR;
1598 return 0;
1599 }
1600
1601 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1602 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1603 CBS_len(&protocol_name)) == 0) {
1604 protocol_ok = 1;
1605 break;
1606 }
1607 }
1608
1609 if (!protocol_ok) {
1610 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1611 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1612 return 0;
1613 }
1614
Adam Langleyf18e4532015-07-10 13:39:53 -07001615 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1616 &ssl->s3->alpn_selected_len)) {
1617 *out_alert = SSL_AD_INTERNAL_ERROR;
1618 return 0;
1619 }
1620
1621 return 1;
1622}
1623
1624static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1625 CBS *contents) {
1626 if (contents == NULL) {
1627 return 1;
1628 }
1629
1630 if (ssl->ctx->alpn_select_cb == NULL ||
1631 ssl->s3->initial_handshake_complete) {
1632 return 1;
1633 }
1634
1635 /* ALPN takes precedence over NPN. */
1636 ssl->s3->next_proto_neg_seen = 0;
1637
1638 CBS protocol_name_list;
1639 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1640 CBS_len(contents) != 0 ||
1641 CBS_len(&protocol_name_list) < 2) {
1642 return 0;
1643 }
1644
1645 /* Validate the protocol list. */
1646 CBS protocol_name_list_copy = protocol_name_list;
1647 while (CBS_len(&protocol_name_list_copy) > 0) {
1648 CBS protocol_name;
1649
1650 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1651 /* Empty protocol names are forbidden. */
1652 CBS_len(&protocol_name) == 0) {
1653 return 0;
1654 }
1655 }
1656
1657 const uint8_t *selected;
1658 uint8_t selected_len;
1659 if (ssl->ctx->alpn_select_cb(
1660 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1661 CBS_len(&protocol_name_list),
1662 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1663 OPENSSL_free(ssl->s3->alpn_selected);
1664 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1665 if (ssl->s3->alpn_selected == NULL) {
1666 *out_alert = SSL_AD_INTERNAL_ERROR;
1667 return 0;
1668 }
1669 ssl->s3->alpn_selected_len = selected_len;
1670 }
1671
1672 return 1;
1673}
1674
1675static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1676 if (ssl->s3->alpn_selected == NULL) {
1677 return 1;
1678 }
1679
1680 CBB contents, proto_list, proto;
1681 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1682 !CBB_add_u16_length_prefixed(out, &contents) ||
1683 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1684 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001685 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1686 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001687 !CBB_flush(out)) {
1688 return 0;
1689 }
1690
1691 return 1;
1692}
1693
1694
Adam Langley49c7af12015-07-10 14:33:46 -07001695/* Channel ID.
1696 *
1697 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1698
1699static void ext_channel_id_init(SSL *ssl) {
1700 ssl->s3->tlsext_channel_id_valid = 0;
1701}
1702
1703static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1704 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001705 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001706 return 1;
1707 }
1708
1709 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1710 !CBB_add_u16(out, 0 /* length */)) {
1711 return 0;
1712 }
1713
1714 return 1;
1715}
1716
1717static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1718 CBS *contents) {
1719 if (contents == NULL) {
1720 return 1;
1721 }
1722
Steven Valdez143e8b32016-07-11 13:19:03 -04001723 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1724 return 0;
1725 }
1726
David Benjamince079fd2016-08-02 16:22:34 -04001727 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001728 assert(ssl->tlsext_channel_id_enabled);
1729
1730 if (CBS_len(contents) != 0) {
1731 return 0;
1732 }
1733
1734 ssl->s3->tlsext_channel_id_valid = 1;
1735 return 1;
1736}
1737
1738static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1739 CBS *contents) {
1740 if (contents == NULL ||
1741 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001742 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001743 return 1;
1744 }
1745
1746 if (CBS_len(contents) != 0) {
1747 return 0;
1748 }
1749
1750 ssl->s3->tlsext_channel_id_valid = 1;
1751 return 1;
1752}
1753
1754static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001755 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1756 return 1;
1757 }
1758
Adam Langley49c7af12015-07-10 14:33:46 -07001759 if (!ssl->s3->tlsext_channel_id_valid) {
1760 return 1;
1761 }
1762
1763 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1764 !CBB_add_u16(out, 0 /* length */)) {
1765 return 0;
1766 }
1767
1768 return 1;
1769}
1770
Adam Langley391250d2015-07-15 19:06:07 -07001771
1772/* Secure Real-time Transport Protocol (SRTP) extension.
1773 *
1774 * https://tools.ietf.org/html/rfc5764 */
1775
Adam Langley391250d2015-07-15 19:06:07 -07001776
1777static void ext_srtp_init(SSL *ssl) {
1778 ssl->srtp_profile = NULL;
1779}
1780
1781static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1782 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1783 if (profiles == NULL) {
1784 return 1;
1785 }
1786 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1787 if (num_profiles == 0) {
1788 return 1;
1789 }
1790
1791 CBB contents, profile_ids;
1792 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1793 !CBB_add_u16_length_prefixed(out, &contents) ||
1794 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1795 return 0;
1796 }
1797
David Benjamin54091232016-09-05 12:47:25 -04001798 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001799 if (!CBB_add_u16(&profile_ids,
1800 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1801 return 0;
1802 }
1803 }
1804
1805 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1806 !CBB_flush(out)) {
1807 return 0;
1808 }
1809
1810 return 1;
1811}
1812
1813static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1814 CBS *contents) {
1815 if (contents == NULL) {
1816 return 1;
1817 }
1818
1819 /* The extension consists of a u16-prefixed profile ID list containing a
1820 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1821 *
1822 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1823 CBS profile_ids, srtp_mki;
1824 uint16_t profile_id;
1825 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1826 !CBS_get_u16(&profile_ids, &profile_id) ||
1827 CBS_len(&profile_ids) != 0 ||
1828 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1829 CBS_len(contents) != 0) {
1830 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1831 return 0;
1832 }
1833
1834 if (CBS_len(&srtp_mki) != 0) {
1835 /* Must be no MKI, since we never offer one. */
1836 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1837 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1838 return 0;
1839 }
1840
1841 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1842
1843 /* Check to see if the server gave us something we support (and presumably
1844 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001845 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001846 const SRTP_PROTECTION_PROFILE *profile =
1847 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1848
1849 if (profile->id == profile_id) {
1850 ssl->srtp_profile = profile;
1851 return 1;
1852 }
1853 }
1854
1855 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1856 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1857 return 0;
1858}
1859
1860static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1861 CBS *contents) {
1862 if (contents == NULL) {
1863 return 1;
1864 }
1865
1866 CBS profile_ids, srtp_mki;
1867 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1868 CBS_len(&profile_ids) < 2 ||
1869 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1870 CBS_len(contents) != 0) {
1871 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1872 return 0;
1873 }
1874 /* Discard the MKI value for now. */
1875
1876 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1877 SSL_get_srtp_profiles(ssl);
1878
1879 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001880 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001881 const SRTP_PROTECTION_PROFILE *server_profile =
1882 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1883
1884 CBS profile_ids_tmp;
1885 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1886
1887 while (CBS_len(&profile_ids_tmp) > 0) {
1888 uint16_t profile_id;
1889 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1890 return 0;
1891 }
1892
1893 if (server_profile->id == profile_id) {
1894 ssl->srtp_profile = server_profile;
1895 return 1;
1896 }
1897 }
1898 }
1899
1900 return 1;
1901}
1902
1903static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1904 if (ssl->srtp_profile == NULL) {
1905 return 1;
1906 }
1907
1908 CBB contents, profile_ids;
1909 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1910 !CBB_add_u16_length_prefixed(out, &contents) ||
1911 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1912 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1913 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1914 !CBB_flush(out)) {
1915 return 0;
1916 }
1917
1918 return 1;
1919}
1920
Adam Langleybdd5d662015-07-20 16:19:08 -07001921
1922/* EC point formats.
1923 *
1924 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1925
1926static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
David Benjamince079fd2016-08-02 16:22:34 -04001927 if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001928 return 0;
1929 }
1930
1931 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1932
David Benjamin54091232016-09-05 12:47:25 -04001933 for (size_t i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001934 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1935
1936 const uint32_t alg_k = cipher->algorithm_mkey;
1937 const uint32_t alg_a = cipher->algorithm_auth;
1938 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1939 return 1;
1940 }
1941 }
1942
1943 return 0;
1944}
1945
Adam Langleybdd5d662015-07-20 16:19:08 -07001946static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001947 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001948 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1949 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001950 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1951 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001952 !CBB_flush(out)) {
1953 return 0;
1954 }
1955
1956 return 1;
1957}
1958
1959static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1960 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1961 return 1;
1962 }
1963
1964 return ext_ec_point_add_extension(ssl, out);
1965}
1966
1967static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1968 CBS *contents) {
1969 if (contents == NULL) {
1970 return 1;
1971 }
1972
Steven Valdez143e8b32016-07-11 13:19:03 -04001973 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1974 return 0;
1975 }
1976
Adam Langleybdd5d662015-07-20 16:19:08 -07001977 CBS ec_point_format_list;
1978 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1979 CBS_len(contents) != 0) {
1980 return 0;
1981 }
1982
David Benjaminfc059942015-07-30 23:01:59 -04001983 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1984 * point format. */
1985 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1986 CBS_len(&ec_point_format_list)) == NULL) {
1987 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001988 return 0;
1989 }
1990
1991 return 1;
1992}
1993
1994static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1995 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001996 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1997 return 1;
1998 }
1999
Adam Langleybdd5d662015-07-20 16:19:08 -07002000 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
2001}
2002
2003static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002004 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
2005 return 1;
2006 }
2007
Adam Langleybdd5d662015-07-20 16:19:08 -07002008 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
2009 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04002010 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07002011
2012 if (!using_ecc) {
2013 return 1;
2014 }
2015
2016 return ext_ec_point_add_extension(ssl, out);
2017}
2018
Steven Valdez4aa154e2016-07-29 14:32:55 -04002019/* Pre Shared Key
2020 *
Steven Valdez5b986082016-09-01 12:29:49 -04002021 * https://tools.ietf.org/html/draft-ietf-tls-tls13-15 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04002022
2023static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
2024 uint16_t min_version, max_version;
2025 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2026 return 0;
2027 }
2028
David Benjamin2dc02042016-09-19 19:57:37 -04002029 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002030 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04002031 !ssl->method->version_from_wire(&session_version,
2032 ssl->session->ssl_version) ||
2033 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002034 return 1;
2035 }
2036
Steven Valdez5b986082016-09-01 12:29:49 -04002037 CBB contents, identity, ke_modes, auth_modes, ticket;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002038 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2039 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04002040 !CBB_add_u16_length_prefixed(&contents, &identity) ||
2041 !CBB_add_u8_length_prefixed(&identity, &ke_modes) ||
2042 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE) ||
2043 !CBB_add_u8_length_prefixed(&identity, &auth_modes) ||
2044 !CBB_add_u8(&auth_modes, SSL_PSK_AUTH) ||
2045 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
2046 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002047 ssl->session->tlsext_ticklen)) {
2048 return 0;
2049 }
2050
2051 return CBB_flush(out);
2052}
2053
2054int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2055 CBS *contents) {
2056 uint16_t psk_id;
2057 if (!CBS_get_u16(contents, &psk_id) ||
2058 CBS_len(contents) != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04002059 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002060 *out_alert = SSL_AD_DECODE_ERROR;
2061 return 0;
2062 }
2063
2064 if (psk_id != 0) {
David Benjamin7f78df42016-10-05 22:33:19 -04002065 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002066 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2067 return 0;
2068 }
2069
2070 return 1;
2071}
2072
2073int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
2074 SSL_SESSION **out_session,
2075 uint8_t *out_alert,
2076 CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04002077 /* We only process the first PSK identity since we don't support pure PSK. */
2078 CBS identity, ke_modes, auth_modes, ticket;
2079 if (!CBS_get_u16_length_prefixed(contents, &identity) ||
2080 !CBS_get_u8_length_prefixed(&identity, &ke_modes) ||
2081 !CBS_get_u8_length_prefixed(&identity, &auth_modes) ||
2082 !CBS_get_u16_length_prefixed(&identity, &ticket) ||
2083 CBS_len(&identity) != 0) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002084 *out_alert = SSL_AD_DECODE_ERROR;
2085 return 0;
2086 }
2087
Steven Valdez5b986082016-09-01 12:29:49 -04002088 /* We only support tickets with PSK_DHE_KE and PSK_AUTH. */
2089 if (memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, CBS_len(&ke_modes)) == NULL ||
2090 memchr(CBS_data(&auth_modes), SSL_PSK_AUTH, CBS_len(&auth_modes)) ==
2091 NULL) {
2092 *out_session = NULL;
2093 return 1;
2094 }
2095
Steven Valdez4aa154e2016-07-29 14:32:55 -04002096 /* TLS 1.3 session tickets are renewed separately as part of the
2097 * NewSessionTicket. */
2098 int renew;
Steven Valdez5b986082016-09-01 12:29:49 -04002099 return tls_process_ticket(ssl, out_session, &renew, CBS_data(&ticket),
2100 CBS_len(&ticket), NULL, 0);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002101}
2102
2103int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
2104 if (!ssl->s3->session_reused) {
2105 return 1;
2106 }
2107
2108 CBB contents;
2109 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2110 !CBB_add_u16_length_prefixed(out, &contents) ||
2111 /* We only consider the first identity for resumption */
2112 !CBB_add_u16(&contents, 0) ||
2113 !CBB_flush(out)) {
2114 return 0;
2115 }
2116
2117 return 1;
2118}
2119
2120
Steven Valdez143e8b32016-07-11 13:19:03 -04002121/* Key Share
2122 *
2123 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
2124
2125static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
2126 uint16_t min_version, max_version;
2127 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2128 return 0;
2129 }
2130
2131 if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
2132 return 1;
2133 }
2134
2135 CBB contents, kse_bytes;
2136 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2137 !CBB_add_u16_length_prefixed(out, &contents) ||
2138 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2139 return 0;
2140 }
2141
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002142 uint16_t group_id;
Steven Valdez5440fe02016-07-18 12:40:30 -04002143 if (ssl->s3->hs->retry_group) {
2144 /* Append the new key share to the old list. */
2145 if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
2146 ssl->s3->hs->key_share_bytes_len)) {
2147 return 0;
2148 }
2149 OPENSSL_free(ssl->s3->hs->key_share_bytes);
2150 ssl->s3->hs->key_share_bytes = NULL;
David Benjamin4fe3c902016-08-16 02:17:03 -04002151 ssl->s3->hs->key_share_bytes_len = 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002152
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002153 group_id = ssl->s3->hs->retry_group;
Steven Valdez5440fe02016-07-18 12:40:30 -04002154 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002155 /* Add a fake group. See draft-davidben-tls-grease-01. */
2156 if (ssl->ctx->grease_enabled &&
2157 (!CBB_add_u16(&kse_bytes,
2158 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2159 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2160 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2161 return 0;
2162 }
2163
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002164 /* Predict the most preferred group. */
2165 const uint16_t *groups;
2166 size_t groups_len;
Steven Valdez5440fe02016-07-18 12:40:30 -04002167 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002168 if (groups_len == 0) {
2169 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2170 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002171 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002172
2173 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002174 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002175
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002176 CBB key_exchange;
2177 if (!CBB_add_u16(&kse_bytes, group_id) ||
2178 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2179 !SSL_ECDH_CTX_init(&ssl->s3->hs->ecdh_ctx, group_id) ||
2180 !SSL_ECDH_CTX_offer(&ssl->s3->hs->ecdh_ctx, &key_exchange) ||
2181 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002182 return 0;
2183 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002184
Steven Valdez5440fe02016-07-18 12:40:30 -04002185 if (!ssl->s3->hs->retry_group) {
2186 /* Save the contents of the extension to repeat it in the second
2187 * ClientHello. */
2188 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2189 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2190 CBB_len(&kse_bytes));
2191 if (ssl->s3->hs->key_share_bytes == NULL) {
2192 return 0;
2193 }
2194 }
2195
Steven Valdez143e8b32016-07-11 13:19:03 -04002196 return CBB_flush(out);
2197}
2198
Steven Valdez7259f2f2016-08-02 16:55:05 -04002199int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2200 size_t *out_secret_len,
2201 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002202 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002203 uint16_t group_id;
2204 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002205 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2206 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002207 *out_alert = SSL_AD_DECODE_ERROR;
2208 return 0;
2209 }
2210
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002211 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002212 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2213 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2214 return 0;
2215 }
2216
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002217 if (!SSL_ECDH_CTX_finish(&ssl->s3->hs->ecdh_ctx, out_secret, out_secret_len,
2218 out_alert, CBS_data(&peer_key),
2219 CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002220 *out_alert = SSL_AD_INTERNAL_ERROR;
2221 return 0;
2222 }
2223
David Benjamin5c4e8572016-08-19 17:44:53 -04002224 ssl->s3->new_session->key_exchange_info = group_id;
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002225 SSL_ECDH_CTX_cleanup(&ssl->s3->hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002226 return 1;
2227}
2228
Steven Valdez7259f2f2016-08-02 16:55:05 -04002229int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2230 uint8_t **out_secret,
2231 size_t *out_secret_len,
2232 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002233 uint16_t group_id;
2234 CBS key_shares;
Steven Valdez803c77a2016-09-06 14:13:43 -04002235 if (!tls1_get_shared_group(ssl, &group_id)) {
2236 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2237 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2238 return 0;
2239 }
2240
2241 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002242 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002243 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002244 return 0;
2245 }
2246
David Benjamin7e1f9842016-09-20 19:24:40 -04002247 /* Find the corresponding key share. */
2248 int found = 0;
2249 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002250 while (CBS_len(&key_shares) > 0) {
2251 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002252 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002253 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002254 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2255 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002256 return 0;
2257 }
2258
David Benjamin7e1f9842016-09-20 19:24:40 -04002259 if (id == group_id) {
2260 if (found) {
2261 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2262 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2263 return 0;
2264 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002265
David Benjamin7e1f9842016-09-20 19:24:40 -04002266 found = 1;
2267 peer_key = peer_key_tmp;
2268 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002269 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002270 }
2271
David Benjamin7e1f9842016-09-20 19:24:40 -04002272 if (!found) {
2273 *out_found = 0;
2274 *out_secret = NULL;
2275 *out_secret_len = 0;
2276 return 1;
2277 }
2278
2279 /* Compute the DH secret. */
2280 uint8_t *secret = NULL;
2281 size_t secret_len;
2282 SSL_ECDH_CTX group;
2283 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2284 CBB public_key;
2285 if (!CBB_init(&public_key, 32) ||
2286 !SSL_ECDH_CTX_init(&group, group_id) ||
2287 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len,
2288 out_alert, CBS_data(&peer_key),
2289 CBS_len(&peer_key)) ||
2290 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2291 &ssl->s3->hs->public_key_len)) {
2292 OPENSSL_free(secret);
2293 SSL_ECDH_CTX_cleanup(&group);
2294 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002295 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002296 return 0;
2297 }
2298
2299 SSL_ECDH_CTX_cleanup(&group);
2300
2301 *out_secret = secret;
2302 *out_secret_len = secret_len;
2303 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002304 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002305}
2306
Steven Valdez7259f2f2016-08-02 16:55:05 -04002307int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002308 uint16_t group_id;
2309 CBB kse_bytes, public_key;
2310 if (!tls1_get_shared_group(ssl, &group_id) ||
2311 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2312 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2313 !CBB_add_u16(&kse_bytes, group_id) ||
2314 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2315 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2316 ssl->s3->hs->public_key_len) ||
2317 !CBB_flush(out)) {
2318 return 0;
2319 }
2320
David Benjamin4fe3c902016-08-16 02:17:03 -04002321 OPENSSL_free(ssl->s3->hs->public_key);
2322 ssl->s3->hs->public_key = NULL;
2323 ssl->s3->hs->public_key_len = 0;
2324
David Benjamin5c4e8572016-08-19 17:44:53 -04002325 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002326 return 1;
2327}
2328
2329
Steven Valdezfdd10992016-09-15 16:27:05 -04002330/* Supported Versions
2331 *
2332 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2333
2334static int ext_supported_versions_add_clienthello(SSL *ssl, CBB *out) {
2335 uint16_t min_version, max_version;
2336 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2337 return 0;
2338 }
2339
2340 if (max_version <= TLS1_2_VERSION) {
2341 return 1;
2342 }
2343
2344 CBB contents, versions;
2345 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2346 !CBB_add_u16_length_prefixed(out, &contents) ||
2347 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2348 return 0;
2349 }
2350
David Benjamind9791bf2016-09-27 16:39:52 -04002351 /* Add a fake version. See draft-davidben-tls-grease-01. */
2352 if (ssl->ctx->grease_enabled &&
2353 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2354 return 0;
2355 }
2356
Steven Valdezfdd10992016-09-15 16:27:05 -04002357 for (uint16_t version = max_version; version >= min_version; version--) {
2358 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2359 return 0;
2360 }
2361 }
2362
2363 if (!CBB_flush(out)) {
2364 return 0;
2365 }
2366
2367 return 1;
2368}
2369
2370
Steven Valdezce902a92016-05-17 11:47:53 -04002371/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002372 *
Steven Valdezce902a92016-05-17 11:47:53 -04002373 * https://tools.ietf.org/html/rfc4492#section-5.1.2
2374 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07002375
Steven Valdezce902a92016-05-17 11:47:53 -04002376static void ext_supported_groups_init(SSL *ssl) {
2377 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2378 ssl->s3->tmp.peer_supported_group_list = NULL;
2379 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07002380}
2381
Steven Valdezce902a92016-05-17 11:47:53 -04002382static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002383 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2384 return 1;
2385 }
2386
Steven Valdezce902a92016-05-17 11:47:53 -04002387 CBB contents, groups_bytes;
2388 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002389 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002390 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002391 return 0;
2392 }
2393
David Benjamin65ac9972016-09-02 21:35:25 -04002394 /* Add a fake group. See draft-davidben-tls-grease-01. */
2395 if (ssl->ctx->grease_enabled &&
2396 !CBB_add_u16(&groups_bytes,
2397 ssl_get_grease_value(ssl, ssl_grease_group))) {
2398 return 0;
2399 }
2400
Steven Valdezce902a92016-05-17 11:47:53 -04002401 const uint16_t *groups;
2402 size_t groups_len;
2403 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002404
David Benjamin54091232016-09-05 12:47:25 -04002405 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002406 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002407 return 0;
2408 }
2409 }
2410
2411 return CBB_flush(out);
2412}
2413
Steven Valdezce902a92016-05-17 11:47:53 -04002414static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2415 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002416 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2417 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002418 return 1;
2419}
2420
Steven Valdezce902a92016-05-17 11:47:53 -04002421static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2422 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002423 if (contents == NULL) {
2424 return 1;
2425 }
2426
Steven Valdezce902a92016-05-17 11:47:53 -04002427 CBS supported_group_list;
2428 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2429 CBS_len(&supported_group_list) == 0 ||
2430 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002431 CBS_len(contents) != 0) {
2432 return 0;
2433 }
2434
Steven Valdezce902a92016-05-17 11:47:53 -04002435 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
2436 CBS_len(&supported_group_list));
2437 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002438 *out_alert = SSL_AD_INTERNAL_ERROR;
2439 return 0;
2440 }
2441
Steven Valdezce902a92016-05-17 11:47:53 -04002442 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002443 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002444 if (!CBS_get_u16(&supported_group_list,
2445 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002446 goto err;
2447 }
2448 }
2449
Steven Valdezce902a92016-05-17 11:47:53 -04002450 assert(CBS_len(&supported_group_list) == 0);
2451 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002452
2453 return 1;
2454
2455err:
Steven Valdezce902a92016-05-17 11:47:53 -04002456 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2457 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002458 *out_alert = SSL_AD_INTERNAL_ERROR;
2459 return 0;
2460}
2461
Steven Valdezce902a92016-05-17 11:47:53 -04002462static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002463 /* Servers don't echo this extension. */
2464 return 1;
2465}
2466
2467
Adam Langley614c66a2015-06-12 15:26:58 -07002468/* kExtensions contains all the supported extensions. */
2469static const struct tls_extension kExtensions[] = {
2470 {
Adam Langley5021b222015-06-12 18:27:58 -07002471 TLSEXT_TYPE_renegotiate,
2472 NULL,
2473 ext_ri_add_clienthello,
2474 ext_ri_parse_serverhello,
2475 ext_ri_parse_clienthello,
2476 ext_ri_add_serverhello,
2477 },
2478 {
Adam Langley614c66a2015-06-12 15:26:58 -07002479 TLSEXT_TYPE_server_name,
David Benjamina0486782016-10-06 19:11:32 -04002480 NULL,
Adam Langley614c66a2015-06-12 15:26:58 -07002481 ext_sni_add_clienthello,
2482 ext_sni_parse_serverhello,
2483 ext_sni_parse_clienthello,
2484 ext_sni_add_serverhello,
2485 },
Adam Langley0a056712015-07-01 15:03:33 -07002486 {
2487 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002488 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002489 ext_ems_add_clienthello,
2490 ext_ems_parse_serverhello,
2491 ext_ems_parse_clienthello,
2492 ext_ems_add_serverhello,
2493 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002494 {
2495 TLSEXT_TYPE_session_ticket,
2496 NULL,
2497 ext_ticket_add_clienthello,
2498 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002499 /* Ticket extension client parsing is handled in ssl_session.c */
2500 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002501 ext_ticket_add_serverhello,
2502 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002503 {
2504 TLSEXT_TYPE_signature_algorithms,
2505 NULL,
2506 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002507 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002508 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002509 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002510 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002511 {
2512 TLSEXT_TYPE_status_request,
2513 ext_ocsp_init,
2514 ext_ocsp_add_clienthello,
2515 ext_ocsp_parse_serverhello,
2516 ext_ocsp_parse_clienthello,
2517 ext_ocsp_add_serverhello,
2518 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002519 {
2520 TLSEXT_TYPE_next_proto_neg,
2521 ext_npn_init,
2522 ext_npn_add_clienthello,
2523 ext_npn_parse_serverhello,
2524 ext_npn_parse_clienthello,
2525 ext_npn_add_serverhello,
2526 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002527 {
2528 TLSEXT_TYPE_certificate_timestamp,
2529 NULL,
2530 ext_sct_add_clienthello,
2531 ext_sct_parse_serverhello,
2532 ext_sct_parse_clienthello,
2533 ext_sct_add_serverhello,
2534 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002535 {
2536 TLSEXT_TYPE_application_layer_protocol_negotiation,
2537 ext_alpn_init,
2538 ext_alpn_add_clienthello,
2539 ext_alpn_parse_serverhello,
2540 ext_alpn_parse_clienthello,
2541 ext_alpn_add_serverhello,
2542 },
Adam Langley49c7af12015-07-10 14:33:46 -07002543 {
2544 TLSEXT_TYPE_channel_id,
2545 ext_channel_id_init,
2546 ext_channel_id_add_clienthello,
2547 ext_channel_id_parse_serverhello,
2548 ext_channel_id_parse_clienthello,
2549 ext_channel_id_add_serverhello,
2550 },
Adam Langley391250d2015-07-15 19:06:07 -07002551 {
2552 TLSEXT_TYPE_srtp,
2553 ext_srtp_init,
2554 ext_srtp_add_clienthello,
2555 ext_srtp_parse_serverhello,
2556 ext_srtp_parse_clienthello,
2557 ext_srtp_add_serverhello,
2558 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002559 {
2560 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002561 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002562 ext_ec_point_add_clienthello,
2563 ext_ec_point_parse_serverhello,
2564 ext_ec_point_parse_clienthello,
2565 ext_ec_point_add_serverhello,
2566 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002567 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002568 TLSEXT_TYPE_key_share,
2569 NULL,
2570 ext_key_share_add_clienthello,
2571 forbid_parse_serverhello,
2572 ignore_parse_clienthello,
2573 dont_add_serverhello,
2574 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002575 {
2576 TLSEXT_TYPE_pre_shared_key,
2577 NULL,
2578 ext_pre_shared_key_add_clienthello,
2579 forbid_parse_serverhello,
2580 ignore_parse_clienthello,
2581 dont_add_serverhello,
2582 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002583 {
2584 TLSEXT_TYPE_supported_versions,
2585 NULL,
2586 ext_supported_versions_add_clienthello,
2587 forbid_parse_serverhello,
2588 ignore_parse_clienthello,
2589 dont_add_serverhello,
2590 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002591 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2592 * intolerant to the last extension being zero-length. See
2593 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002594 {
Steven Valdezce902a92016-05-17 11:47:53 -04002595 TLSEXT_TYPE_supported_groups,
2596 ext_supported_groups_init,
2597 ext_supported_groups_add_clienthello,
2598 ext_supported_groups_parse_serverhello,
2599 ext_supported_groups_parse_clienthello,
2600 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002601 },
Adam Langley614c66a2015-06-12 15:26:58 -07002602};
2603
2604#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2605
Adam Langley4cfa96b2015-07-01 11:56:55 -07002606OPENSSL_COMPILE_ASSERT(kNumExtensions <=
David Benjaminf5d2cd02016-10-06 19:39:20 -04002607 sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002608 too_many_extensions_for_sent_bitset);
David Benjaminf5d2cd02016-10-06 19:39:20 -04002609OPENSSL_COMPILE_ASSERT(
2610 kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
2611 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002612
Adam Langley614c66a2015-06-12 15:26:58 -07002613static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2614 uint16_t value) {
2615 unsigned i;
2616 for (i = 0; i < kNumExtensions; i++) {
2617 if (kExtensions[i].value == value) {
2618 *out_index = i;
2619 return &kExtensions[i];
2620 }
2621 }
2622
2623 return NULL;
2624}
2625
Adam Langley09505632015-07-30 18:10:13 -07002626int SSL_extension_supported(unsigned extension_value) {
2627 uint32_t index;
2628 return extension_value == TLSEXT_TYPE_padding ||
2629 tls_extension_find(&index, extension_value) != NULL;
2630}
2631
David Benjamine8d53502015-10-10 14:13:23 -04002632int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002633 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002634 if (ssl->client_version == SSL3_VERSION &&
2635 !ssl->s3->send_connection_binding) {
2636 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002637 }
Adam Langley95c29f32014-06-20 12:00:00 -07002638
David Benjamine8d53502015-10-10 14:13:23 -04002639 CBB extensions;
2640 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002641 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002642 }
Adam Langley95c29f32014-06-20 12:00:00 -07002643
David Benjaminf5d2cd02016-10-06 19:39:20 -04002644 ssl->s3->hs->extensions.sent = 0;
2645 ssl->s3->hs->custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002646
David Benjamin54091232016-09-05 12:47:25 -04002647 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002648 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002649 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002650 }
2651 }
Adam Langley95c29f32014-06-20 12:00:00 -07002652
David Benjamin65ac9972016-09-02 21:35:25 -04002653 uint16_t grease_ext1 = 0;
2654 if (ssl->ctx->grease_enabled) {
2655 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2656 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2657 if (!CBB_add_u16(&extensions, grease_ext1) ||
2658 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2659 goto err;
2660 }
2661 }
2662
David Benjamin54091232016-09-05 12:47:25 -04002663 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002664 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002665 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002666 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2667 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2668 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002669 }
Adam Langley95c29f32014-06-20 12:00:00 -07002670
Adam Langley33ad2b52015-07-20 17:43:53 -07002671 if (CBB_len(&extensions) != len_before) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002672 ssl->s3->hs->extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002673 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002674 }
Adam Langley75712922014-10-10 16:23:43 -07002675
David Benjamine8d53502015-10-10 14:13:23 -04002676 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002677 goto err;
2678 }
2679
David Benjamin65ac9972016-09-02 21:35:25 -04002680 if (ssl->ctx->grease_enabled) {
2681 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2682 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2683
2684 /* The two fake extensions must not have the same value. GREASE values are
2685 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2686 * one. */
2687 if (grease_ext1 == grease_ext2) {
2688 grease_ext2 ^= 0x1010;
2689 }
2690
2691 if (!CBB_add_u16(&extensions, grease_ext2) ||
2692 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2693 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2694 goto err;
2695 }
2696 }
2697
David Benjamince079fd2016-08-02 16:22:34 -04002698 if (!SSL_is_dtls(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002699 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002700 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002701 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002702 *
2703 * NB: because this code works out the length of all existing extensions
2704 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002705 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002706 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002707 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002708 * Server 7.0 is intolerant to the last extension being zero-length. See
2709 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002710 if (padding_len >= 4 + 1) {
2711 padding_len -= 4;
2712 } else {
2713 padding_len = 1;
2714 }
Adam Langley95c29f32014-06-20 12:00:00 -07002715
Adam Langley33ad2b52015-07-20 17:43:53 -07002716 uint8_t *padding_bytes;
2717 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2718 !CBB_add_u16(&extensions, padding_len) ||
2719 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2720 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002721 }
Adam Langley75712922014-10-10 16:23:43 -07002722
Adam Langley33ad2b52015-07-20 17:43:53 -07002723 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002724 }
2725 }
Adam Langley75712922014-10-10 16:23:43 -07002726
David Benjamina01deee2015-12-08 18:56:31 -05002727 /* Discard empty extensions blocks. */
2728 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002729 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002730 }
2731
David Benjamine8d53502015-10-10 14:13:23 -04002732 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002733
2734err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002735 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002736 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002737}
Adam Langley95c29f32014-06-20 12:00:00 -07002738
David Benjamin56380462015-10-10 14:59:09 -04002739int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002740 CBB extensions;
2741 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002742 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002743 }
2744
2745 unsigned i;
2746 for (i = 0; i < kNumExtensions; i++) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002747 if (!(ssl->s3->hs->extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002748 /* Don't send extensions that were not received. */
2749 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002750 }
Adam Langley95c29f32014-06-20 12:00:00 -07002751
David Benjamin56380462015-10-10 14:59:09 -04002752 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002753 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2754 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2755 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002756 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002757 }
Adam Langley95c29f32014-06-20 12:00:00 -07002758
David Benjamin56380462015-10-10 14:59:09 -04002759 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002760 goto err;
2761 }
2762
Steven Valdez143e8b32016-07-11 13:19:03 -04002763 /* Discard empty extensions blocks before TLS 1.3. */
2764 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2765 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002766 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002767 }
2768
David Benjamin56380462015-10-10 14:59:09 -04002769 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002770
2771err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002772 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002773 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002774}
Adam Langley95c29f32014-06-20 12:00:00 -07002775
David Benjamine14ff062016-08-09 16:21:24 -04002776static int ssl_scan_clienthello_tlsext(
2777 SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
2778 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002779 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002780 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002781 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002782 }
2783 }
2784
David Benjaminf5d2cd02016-10-06 19:39:20 -04002785 ssl->s3->hs->extensions.received = 0;
2786 ssl->s3->hs->custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002787
David Benjamine14ff062016-08-09 16:21:24 -04002788 CBS extensions;
2789 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2790 while (CBS_len(&extensions) != 0) {
2791 uint16_t type;
2792 CBS extension;
2793
2794 /* Decode the next extension. */
2795 if (!CBS_get_u16(&extensions, &type) ||
2796 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002797 *out_alert = SSL_AD_DECODE_ERROR;
2798 return 0;
2799 }
Adam Langley95c29f32014-06-20 12:00:00 -07002800
David Benjamine14ff062016-08-09 16:21:24 -04002801 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2802 * ambiguous. Ignore all but the renegotiation_info extension. */
2803 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2804 continue;
2805 }
Adam Langley95c29f32014-06-20 12:00:00 -07002806
David Benjamine14ff062016-08-09 16:21:24 -04002807 unsigned ext_index;
2808 const struct tls_extension *const ext =
2809 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002810
David Benjamine14ff062016-08-09 16:21:24 -04002811 if (ext == NULL) {
2812 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002813 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002814 return 0;
2815 }
David Benjamine14ff062016-08-09 16:21:24 -04002816 continue;
2817 }
2818
David Benjaminf5d2cd02016-10-06 19:39:20 -04002819 ssl->s3->hs->extensions.received |= (1u << ext_index);
David Benjamine14ff062016-08-09 16:21:24 -04002820 uint8_t alert = SSL_AD_DECODE_ERROR;
2821 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2822 *out_alert = alert;
2823 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2824 ERR_add_error_dataf("extension: %u", (unsigned)type);
2825 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002826 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002827 }
Adam Langley75712922014-10-10 16:23:43 -07002828
David Benjamin1deb41b2016-08-09 19:36:38 -04002829 for (size_t i = 0; i < kNumExtensions; i++) {
David Benjaminf5d2cd02016-10-06 19:39:20 -04002830 if (ssl->s3->hs->extensions.received & (1u << i)) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002831 continue;
2832 }
2833
2834 CBS *contents = NULL, fake_contents;
2835 static const uint8_t kFakeRenegotiateExtension[] = {0};
2836 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2837 ssl_client_cipher_list_contains_cipher(client_hello,
2838 SSL3_CK_SCSV & 0xffff)) {
2839 /* The renegotiation SCSV was received so pretend that we received a
2840 * renegotiation extension. */
2841 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2842 sizeof(kFakeRenegotiateExtension));
2843 contents = &fake_contents;
David Benjaminf5d2cd02016-10-06 19:39:20 -04002844 ssl->s3->hs->extensions.received |= (1u << i);
David Benjamin1deb41b2016-08-09 19:36:38 -04002845 }
2846
2847 /* Extension wasn't observed so call the callback with a NULL
2848 * parameter. */
2849 uint8_t alert = SSL_AD_DECODE_ERROR;
2850 if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
2851 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2852 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2853 *out_alert = alert;
2854 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002855 }
2856 }
2857
Adam Langleyfcf25832014-12-18 17:42:32 -08002858 return 1;
2859}
Adam Langley95c29f32014-06-20 12:00:00 -07002860
David Benjamine14ff062016-08-09 16:21:24 -04002861int ssl_parse_clienthello_tlsext(
2862 SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002863 int alert = -1;
David Benjamine14ff062016-08-09 16:21:24 -04002864 if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002865 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002866 return 0;
2867 }
Adam Langley95c29f32014-06-20 12:00:00 -07002868
David Benjamin0d56f882015-12-19 17:05:56 -05002869 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002870 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002871 return 0;
2872 }
Adam Langley95c29f32014-06-20 12:00:00 -07002873
Adam Langleyfcf25832014-12-18 17:42:32 -08002874 return 1;
2875}
Adam Langley95c29f32014-06-20 12:00:00 -07002876
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002877OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2878
David Benjamin0d56f882015-12-19 17:05:56 -05002879static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002880 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2881 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2882 return 1;
2883 }
Adam Langley614c66a2015-06-12 15:26:58 -07002884
Steven Valdez143e8b32016-07-11 13:19:03 -04002885 /* Decode the extensions block and check it is valid. */
2886 CBS extensions;
2887 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2888 !tls1_check_duplicate_extensions(&extensions)) {
2889 *out_alert = SSL_AD_DECODE_ERROR;
2890 return 0;
2891 }
2892
2893 uint32_t received = 0;
2894 while (CBS_len(&extensions) != 0) {
2895 uint16_t type;
2896 CBS extension;
2897
2898 /* Decode the next extension. */
2899 if (!CBS_get_u16(&extensions, &type) ||
2900 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002901 *out_alert = SSL_AD_DECODE_ERROR;
2902 return 0;
2903 }
Adam Langley95c29f32014-06-20 12:00:00 -07002904
Steven Valdez143e8b32016-07-11 13:19:03 -04002905 unsigned ext_index;
2906 const struct tls_extension *const ext =
2907 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002908
Steven Valdez143e8b32016-07-11 13:19:03 -04002909 if (ext == NULL) {
2910 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002911 return 0;
2912 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002913 continue;
2914 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002915
David Benjaminf5d2cd02016-10-06 19:39:20 -04002916 if (!(ssl->s3->hs->extensions.sent & (1u << ext_index)) &&
David Benjamin1deb41b2016-08-09 19:36:38 -04002917 type != TLSEXT_TYPE_renegotiate) {
2918 /* If the extension was never sent then it is illegal, except for the
2919 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002920 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2921 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002922 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002923 return 0;
2924 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002925
Steven Valdez143e8b32016-07-11 13:19:03 -04002926 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002927
Steven Valdez143e8b32016-07-11 13:19:03 -04002928 uint8_t alert = SSL_AD_DECODE_ERROR;
2929 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2930 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2931 ERR_add_error_dataf("extension: %u", (unsigned)type);
2932 *out_alert = alert;
2933 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002934 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002935 }
Adam Langley95c29f32014-06-20 12:00:00 -07002936
David Benjamin54091232016-09-05 12:47:25 -04002937 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002938 if (!(received & (1u << i))) {
2939 /* Extension wasn't observed so call the callback with a NULL
2940 * parameter. */
2941 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002942 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002943 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2944 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002945 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002946 return 0;
2947 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002948 }
2949 }
Adam Langley95c29f32014-06-20 12:00:00 -07002950
Adam Langleyfcf25832014-12-18 17:42:32 -08002951 return 1;
2952}
Adam Langley95c29f32014-06-20 12:00:00 -07002953
David Benjamin0d56f882015-12-19 17:05:56 -05002954static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002955 int ret = SSL_TLSEXT_ERR_NOACK;
2956 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002957
David Benjamin78f8aab2016-03-10 16:33:58 -05002958 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002959 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002960 ssl->ctx->tlsext_servername_arg);
2961 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002962 ret = ssl->initial_ctx->tlsext_servername_callback(
2963 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002964 }
Adam Langley95c29f32014-06-20 12:00:00 -07002965
Adam Langleyfcf25832014-12-18 17:42:32 -08002966 switch (ret) {
2967 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002968 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002969 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002970
Adam Langleyfcf25832014-12-18 17:42:32 -08002971 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002972 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002973 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002974
Adam Langleyfcf25832014-12-18 17:42:32 -08002975 case SSL_TLSEXT_ERR_NOACK:
David Benjamina0486782016-10-06 19:11:32 -04002976 ssl->s3->hs->should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002977 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002978
Adam Langleyfcf25832014-12-18 17:42:32 -08002979 default:
2980 return 1;
2981 }
2982}
Adam Langleyed8270a2014-09-02 13:52:56 -07002983
David Benjamin0d56f882015-12-19 17:05:56 -05002984static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002985 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002986 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002987
David Benjamin78f8aab2016-03-10 16:33:58 -05002988 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002989 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002990 ssl->ctx->tlsext_servername_arg);
2991 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002992 ret = ssl->initial_ctx->tlsext_servername_callback(
2993 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002994 }
Adam Langley95c29f32014-06-20 12:00:00 -07002995
Adam Langleyfcf25832014-12-18 17:42:32 -08002996 switch (ret) {
2997 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002998 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002999 return -1;
David Benjamin03973092014-06-24 23:27:17 -04003000
Adam Langleyfcf25832014-12-18 17:42:32 -08003001 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003002 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003003 return 1;
3004
3005 default:
3006 return 1;
3007 }
3008}
3009
David Benjamin0d56f882015-12-19 17:05:56 -05003010int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003011 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05003012 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
3013 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003014 return 0;
3015 }
3016
David Benjamin0d56f882015-12-19 17:05:56 -05003017 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04003018 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08003019 return 0;
3020 }
3021
3022 return 1;
3023}
Adam Langley95c29f32014-06-20 12:00:00 -07003024
David Benjamine3aa1d92015-06-16 15:34:50 -04003025int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05003026 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04003027 size_t ticket_len, const uint8_t *session_id,
3028 size_t session_id_len) {
3029 int ret = 1; /* Most errors are non-fatal. */
3030 SSL_CTX *ssl_ctx = ssl->initial_ctx;
3031 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003032
David Benjamine3aa1d92015-06-16 15:34:50 -04003033 HMAC_CTX hmac_ctx;
3034 HMAC_CTX_init(&hmac_ctx);
3035 EVP_CIPHER_CTX cipher_ctx;
3036 EVP_CIPHER_CTX_init(&cipher_ctx);
3037
David Benjaminef1b0092015-11-21 14:05:44 -05003038 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04003039 *out_session = NULL;
3040
Steven Valdez4aa154e2016-07-29 14:32:55 -04003041 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
3042 goto done;
3043 }
3044
David Benjamine3aa1d92015-06-16 15:34:50 -04003045 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3046 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003047 }
3048
David Benjaminadcc3952015-04-26 13:07:57 -04003049 /* Ensure there is room for the key name and the largest IV
3050 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3051 * the maximum IV length should be well under the minimum size for the
3052 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003053 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3054 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003055 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003056 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003057
David Benjamine3aa1d92015-06-16 15:34:50 -04003058 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003059 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3060 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3061 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003062 if (cb_ret < 0) {
3063 ret = 0;
3064 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003065 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003066 if (cb_ret == 0) {
3067 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003068 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003069 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003070 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003071 }
3072 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003073 /* Check the key name matches. */
3074 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3075 SSL_TICKET_KEY_NAME_LEN) != 0) {
3076 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003077 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003078 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3079 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003080 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003081 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3082 ssl_ctx->tlsext_tick_aes_key, iv)) {
3083 ret = 0;
3084 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003085 }
3086 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003087 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003088
David Benjamine3aa1d92015-06-16 15:34:50 -04003089 /* Check the MAC at the end of the ticket. */
3090 uint8_t mac[EVP_MAX_MD_SIZE];
3091 size_t mac_len = HMAC_size(&hmac_ctx);
3092 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003093 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003094 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003095 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003096 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3097 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003098 int mac_ok =
3099 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3100#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3101 mac_ok = 1;
3102#endif
3103 if (!mac_ok) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003104 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003105 }
3106
David Benjamine3aa1d92015-06-16 15:34:50 -04003107 /* Decrypt the session data. */
3108 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3109 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3110 mac_len;
3111 plaintext = OPENSSL_malloc(ciphertext_len);
3112 if (plaintext == NULL) {
3113 ret = 0;
3114 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003115 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003116 size_t plaintext_len;
3117#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3118 memcpy(plaintext, ciphertext, ciphertext_len);
3119 plaintext_len = ciphertext_len;
3120#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003121 if (ciphertext_len >= INT_MAX) {
3122 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003123 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003124 int len1, len2;
3125 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3126 (int)ciphertext_len) ||
3127 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
3128 ERR_clear_error(); /* Don't leave an error on the queue. */
3129 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003130 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003131 plaintext_len = (size_t)(len1 + len2);
3132#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003133
David Benjamine3aa1d92015-06-16 15:34:50 -04003134 /* Decode the session. */
David Benjaminfbc45d72016-09-22 01:21:24 -04003135 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003136 if (session == NULL) {
3137 ERR_clear_error(); /* Don't leave an error on the queue. */
3138 goto done;
3139 }
3140
3141 /* Copy the client's session ID into the new session, to denote the ticket has
3142 * been accepted. */
3143 memcpy(session->session_id, session_id, session_id_len);
3144 session->session_id_length = session_id_len;
3145
Steven Valdez4aa154e2016-07-29 14:32:55 -04003146 if (!ssl_session_is_context_valid(ssl, session) ||
3147 !ssl_session_is_time_valid(ssl, session)) {
3148 SSL_SESSION_free(session);
3149 session = NULL;
3150 }
3151
David Benjamine3aa1d92015-06-16 15:34:50 -04003152 *out_session = session;
3153
3154done:
3155 OPENSSL_free(plaintext);
3156 HMAC_CTX_cleanup(&hmac_ctx);
3157 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3158 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003159}
Adam Langley95c29f32014-06-20 12:00:00 -07003160
Steven Valdez0d62f262015-09-04 12:41:04 -04003161int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003162 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05003163 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003164 return 1;
3165 }
David Benjamincd996942014-07-20 16:23:51 -04003166
David Benjamin0fc37ef2016-08-17 15:29:46 -04003167 SSL_HANDSHAKE *hs = ssl->s3->hs;
3168 OPENSSL_free(hs->peer_sigalgs);
3169 hs->peer_sigalgs = NULL;
3170 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003171
3172 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003173 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003174 return 0;
3175 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003176 num_sigalgs /= 2;
3177
3178 /* supported_signature_algorithms in the certificate request is
3179 * allowed to be empty. */
3180 if (num_sigalgs == 0) {
3181 return 1;
3182 }
3183
Steven Valdez02563852016-06-23 13:33:05 -04003184 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3185 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003186 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3187 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003188 return 0;
3189 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003190 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003191
3192 CBS sigalgs;
3193 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003194 for (size_t i = 0; i < num_sigalgs; i++) {
3195 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003196 return 0;
3197 }
3198 }
Adam Langley95c29f32014-06-20 12:00:00 -07003199
Adam Langleyfcf25832014-12-18 17:42:32 -08003200 return 1;
3201}
David Benjaminec2f27d2014-11-13 19:17:25 -05003202
David Benjaminea9a0d52016-07-08 15:52:59 -07003203int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003204 CERT *cert = ssl->cert;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003205 SSL_HANDSHAKE *hs = ssl->s3->hs;
David Benjaminec2f27d2014-11-13 19:17:25 -05003206
Steven Valdezf0451ca2016-06-29 13:16:27 -04003207 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3208 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3209 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003210 int type = ssl_private_key_type(ssl);
3211 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003212 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003213 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003214 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003215 if (ssl_is_ecdsa_key_type(type)) {
3216 *out = SSL_SIGN_ECDSA_SHA1;
3217 return 1;
3218 }
3219 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3220 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003221 }
3222
Steven Valdezeff1e8d2016-07-06 14:24:47 -04003223 const uint16_t *sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003224 size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07003225 if (cert->sigalgs != NULL) {
3226 sigalgs = cert->sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003227 num_sigalgs = cert->num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003228 }
3229
David Benjamin0fc37ef2016-08-17 15:29:46 -04003230 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3231 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3232 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003233 /* If the client didn't specify any signature_algorithms extension then
3234 * we can assume that it supports SHA1. See
3235 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3236 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3237 SSL_SIGN_ECDSA_SHA1};
3238 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003239 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003240 }
3241
David Benjamin0fc37ef2016-08-17 15:29:46 -04003242 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003243 uint16_t sigalg = sigalgs[i];
3244 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3245 * negotiated. */
3246 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3247 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3248 continue;
3249 }
3250
David Benjamin0fc37ef2016-08-17 15:29:46 -04003251 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003252 if (sigalg == peer_sigalgs[j]) {
3253 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003254 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003255 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003256 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003257 }
Adam Langley95c29f32014-06-20 12:00:00 -07003258
David Benjaminea9a0d52016-07-08 15:52:59 -07003259 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3260 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003261}
Adam Langley95c29f32014-06-20 12:00:00 -07003262
David Benjamind6a4ae92015-08-06 11:10:51 -04003263int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
3264 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003265 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003266
3267 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003268 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
3269 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08003270 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003271
David Benjamind6a4ae92015-08-06 11:10:51 -04003272 static const char kClientIDMagic[] = "TLS Channel ID signature";
3273 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
3274
Steven Valdez87eab492016-06-27 16:34:59 -04003275 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003276 static const char kResumptionMagic[] = "Resumption";
3277 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
3278 if (ssl->session->original_handshake_hash_len == 0) {
3279 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3280 goto err;
3281 }
3282 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
3283 ssl->session->original_handshake_hash_len);
3284 }
3285
3286 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3287 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3288 sizeof(handshake_hash));
3289 if (handshake_hash_len < 0) {
3290 goto err;
3291 }
3292 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
3293 unsigned len_u;
3294 EVP_DigestFinal_ex(&ctx, out, &len_u);
3295 *out_len = len_u;
3296
3297 ret = 1;
3298
3299err:
3300 EVP_MD_CTX_cleanup(&ctx);
3301 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003302}
Adam Langley1258b6a2014-06-20 12:00:00 -07003303
3304/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003305 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003306 * data. */
3307int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003308 int digest_len;
3309 /* This function should never be called for a resumed session because the
3310 * handshake hashes that we wish to record are for the original, full
3311 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003312 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003313 return -1;
3314 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003315
Adam Langleyfcf25832014-12-18 17:42:32 -08003316 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003317 tls1_handshake_digest(
3318 ssl, ssl->s3->new_session->original_handshake_hash,
3319 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003320 if (digest_len < 0) {
3321 return -1;
3322 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003323
Steven Valdez87eab492016-06-27 16:34:59 -04003324 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003325
Adam Langleyfcf25832014-12-18 17:42:32 -08003326 return 1;
3327}