blob: b9d359ecda2218f3b4a8c753730064be932614f2 [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
709static void ext_sni_init(SSL *ssl) {
710 ssl->s3->tmp.should_ack_sni = 0;
711}
712
713static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
714 if (ssl->tlsext_hostname == NULL) {
715 return 1;
716 }
717
718 CBB contents, server_name_list, name;
719 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
720 !CBB_add_u16_length_prefixed(out, &contents) ||
721 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
722 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
723 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
724 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
725 strlen(ssl->tlsext_hostname)) ||
726 !CBB_flush(out)) {
727 return 0;
728 }
729
730 return 1;
731}
732
David Benjamin0d56f882015-12-19 17:05:56 -0500733static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
734 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700735 if (contents == NULL) {
736 return 1;
737 }
738
739 if (CBS_len(contents) != 0) {
740 return 0;
741 }
742
743 assert(ssl->tlsext_hostname != NULL);
744
Steven Valdez87eab492016-06-27 16:34:59 -0400745 if (ssl->session == NULL) {
746 assert(ssl->s3->new_session->tlsext_hostname == NULL);
747 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
748 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700749 *out_alert = SSL_AD_INTERNAL_ERROR;
750 return 0;
751 }
752 }
753
754 return 1;
755}
756
David Benjamin0d56f882015-12-19 17:05:56 -0500757static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
758 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700759 if (contents == NULL) {
760 return 1;
761 }
762
David Benjamin9b611e22016-03-03 08:48:30 -0500763 CBS server_name_list, host_name;
764 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700765 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500766 !CBS_get_u8(&server_name_list, &name_type) ||
767 /* Although the server_name extension was intended to be extensible to
768 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
769 * different name types will cause an error. Further, RFC 4366 originally
770 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
771 * adding new name types is no longer feasible.
772 *
773 * Act as if the extensibility does not exist to simplify parsing. */
774 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
775 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700776 CBS_len(contents) != 0) {
777 return 0;
778 }
779
David Benjamin9b611e22016-03-03 08:48:30 -0500780 if (name_type != TLSEXT_NAMETYPE_host_name ||
781 CBS_len(&host_name) == 0 ||
782 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
783 CBS_contains_zero_byte(&host_name)) {
784 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
785 return 0;
786 }
Adam Langley614c66a2015-06-12 15:26:58 -0700787
David Benjamin9b611e22016-03-03 08:48:30 -0500788 /* TODO(davidben): SNI should be resolved before resumption. We have the
789 * early callback as a replacement, but we should fix the current callback
790 * and avoid the need for |SSL_CTX_set_session_id_context|. */
Steven Valdez87eab492016-06-27 16:34:59 -0400791 if (ssl->session == NULL) {
792 assert(ssl->s3->new_session->tlsext_hostname == NULL);
David Benjamin9b611e22016-03-03 08:48:30 -0500793
794 /* Copy the hostname as a string. */
Steven Valdez87eab492016-06-27 16:34:59 -0400795 if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) {
David Benjamin9b611e22016-03-03 08:48:30 -0500796 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700797 return 0;
798 }
799
David Benjamin9b611e22016-03-03 08:48:30 -0500800 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700801 }
802
803 return 1;
804}
805
806static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez87eab492016-06-27 16:34:59 -0400807 if (ssl->session != NULL ||
Adam Langley614c66a2015-06-12 15:26:58 -0700808 !ssl->s3->tmp.should_ack_sni ||
Steven Valdez87eab492016-06-27 16:34:59 -0400809 ssl->s3->new_session->tlsext_hostname == NULL) {
Adam Langley614c66a2015-06-12 15:26:58 -0700810 return 1;
811 }
812
813 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
814 !CBB_add_u16(out, 0 /* length */)) {
815 return 0;
816 }
817
818 return 1;
819}
820
821
Adam Langley5021b222015-06-12 18:27:58 -0700822/* Renegotiation indication.
823 *
824 * https://tools.ietf.org/html/rfc5746 */
825
826static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400827 uint16_t min_version, max_version;
828 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
829 return 0;
830 }
831
832 /* Renegotiation indication is not necessary in TLS 1.3. */
833 if (min_version >= TLS1_3_VERSION) {
834 return 1;
835 }
836
Adam Langley5021b222015-06-12 18:27:58 -0700837 CBB contents, prev_finished;
838 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
839 !CBB_add_u16_length_prefixed(out, &contents) ||
840 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
841 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
842 ssl->s3->previous_client_finished_len) ||
843 !CBB_flush(out)) {
844 return 0;
845 }
846
847 return 1;
848}
849
850static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
851 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400852 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
853 return 0;
854 }
855
David Benjamin3e052de2015-11-25 20:10:31 -0500856 /* Servers may not switch between omitting the extension and supporting it.
857 * See RFC 5746, sections 3.5 and 4.2. */
858 if (ssl->s3->initial_handshake_complete &&
859 (contents != NULL) != ssl->s3->send_connection_binding) {
860 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
861 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
862 return 0;
863 }
864
Adam Langley5021b222015-06-12 18:27:58 -0700865 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500866 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700867 * RI even on initial ServerHello because the client doesn't see any
868 * renegotiation during an attack. However this would mean we could not
869 * connect to any server which doesn't support RI.
870 *
David Benjamine9cddb82015-11-23 14:36:40 -0500871 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
872 * practical terms every client sets it so it's just assumed here. */
873 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700874 }
875
876 const size_t expected_len = ssl->s3->previous_client_finished_len +
877 ssl->s3->previous_server_finished_len;
878
879 /* Check for logic errors */
880 assert(!expected_len || ssl->s3->previous_client_finished_len);
881 assert(!expected_len || ssl->s3->previous_server_finished_len);
882
883 /* Parse out the extension contents. */
884 CBS renegotiated_connection;
885 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
886 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400887 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700888 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
889 return 0;
890 }
891
892 /* Check that the extension matches. */
893 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400894 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700895 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
896 return 0;
897 }
898
899 const uint8_t *d = CBS_data(&renegotiated_connection);
900 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
901 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400902 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700903 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
904 return 0;
905 }
906 d += ssl->s3->previous_client_finished_len;
907
908 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
909 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400910 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700911 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
912 return 0;
913 }
914 ssl->s3->send_connection_binding = 1;
915
916 return 1;
917}
918
919static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
920 CBS *contents) {
921 /* Renegotiation isn't supported as a server so this function should never be
922 * called after the initial handshake. */
923 assert(!ssl->s3->initial_handshake_complete);
924
Steven Valdez143e8b32016-07-11 13:19:03 -0400925 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
926 return 1;
927 }
928
Adam Langley5021b222015-06-12 18:27:58 -0700929 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400930 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700931 }
932
933 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700934 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
935 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400936 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700937 return 0;
938 }
939
940 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500941 if (!CBS_mem_equal(&renegotiated_connection,
942 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700943 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400944 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700945 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
946 return 0;
947 }
948
949 ssl->s3->send_connection_binding = 1;
950
951 return 1;
952}
953
954static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400955 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
956 return 1;
957 }
958
Adam Langley5021b222015-06-12 18:27:58 -0700959 CBB contents, prev_finished;
960 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
961 !CBB_add_u16_length_prefixed(out, &contents) ||
962 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
963 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
964 ssl->s3->previous_client_finished_len) ||
965 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
966 ssl->s3->previous_server_finished_len) ||
967 !CBB_flush(out)) {
968 return 0;
969 }
970
971 return 1;
972}
973
Adam Langley0a056712015-07-01 15:03:33 -0700974
975/* Extended Master Secret.
976 *
David Benjamin43946d42016-02-01 08:42:19 -0500977 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700978
Adam Langley0a056712015-07-01 15:03:33 -0700979static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400980 uint16_t min_version, max_version;
981 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
982 return 0;
983 }
984
985 /* Extended master secret is not necessary in TLS 1.3. */
986 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700987 return 1;
988 }
989
990 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
991 !CBB_add_u16(out, 0 /* length */)) {
992 return 0;
993 }
994
995 return 1;
996}
997
998static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
999 CBS *contents) {
David Benjamin163c9562016-08-29 23:14:17 -04001000 /* Whether EMS is negotiated may not change on renegotation. */
1001 if (ssl->s3->initial_handshake_complete) {
1002 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
1003 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
1004 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1005 return 0;
1006 }
1007
1008 return 1;
1009 }
1010
Adam Langley0a056712015-07-01 15:03:33 -07001011 if (contents == NULL) {
1012 return 1;
1013 }
1014
Steven Valdez143e8b32016-07-11 13:19:03 -04001015 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1016 ssl->version == SSL3_VERSION) {
1017 return 0;
1018 }
1019
1020 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -07001021 return 0;
1022 }
1023
1024 ssl->s3->tmp.extended_master_secret = 1;
1025 return 1;
1026}
1027
David Benjamin0d56f882015-12-19 17:05:56 -05001028static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1029 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001030 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1031 ssl->version == SSL3_VERSION) {
1032 return 1;
1033 }
1034
1035 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -07001036 return 1;
1037 }
1038
1039 if (CBS_len(contents) != 0) {
1040 return 0;
1041 }
1042
1043 ssl->s3->tmp.extended_master_secret = 1;
1044 return 1;
1045}
1046
1047static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1048 if (!ssl->s3->tmp.extended_master_secret) {
1049 return 1;
1050 }
1051
1052 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1053 !CBB_add_u16(out, 0 /* length */)) {
1054 return 0;
1055 }
1056
1057 return 1;
1058}
1059
Adam Langley9b05bc52015-07-01 15:25:33 -07001060
1061/* Session tickets.
1062 *
1063 * https://tools.ietf.org/html/rfc5077 */
1064
1065static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -04001066 uint16_t min_version, max_version;
1067 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1068 return 0;
1069 }
1070
1071 /* TLS 1.3 uses a different ticket extension. */
1072 if (min_version >= TLS1_3_VERSION ||
1073 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001074 return 1;
1075 }
1076
1077 const uint8_t *ticket_data = NULL;
1078 int ticket_len = 0;
1079
1080 /* Renegotiation does not participate in session resumption. However, still
1081 * advertise the extension to avoid potentially breaking servers which carry
1082 * over the state from the previous handshake, such as OpenSSL servers
1083 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
David Benjamin2dc02042016-09-19 19:57:37 -04001084 uint16_t session_version;
Adam Langley9b05bc52015-07-01 15:25:33 -07001085 if (!ssl->s3->initial_handshake_complete &&
1086 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001087 ssl->session->tlsext_tick != NULL &&
1088 /* Don't send TLS 1.3 session tickets in the ticket extension. */
David Benjamin2dc02042016-09-19 19:57:37 -04001089 ssl->method->version_from_wire(&session_version,
1090 ssl->session->ssl_version) &&
1091 session_version < TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001092 ticket_data = ssl->session->tlsext_tick;
1093 ticket_len = ssl->session->tlsext_ticklen;
1094 }
1095
1096 CBB ticket;
1097 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1098 !CBB_add_u16_length_prefixed(out, &ticket) ||
1099 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1100 !CBB_flush(out)) {
1101 return 0;
1102 }
1103
1104 return 1;
1105}
1106
1107static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1108 CBS *contents) {
1109 ssl->tlsext_ticket_expected = 0;
1110
1111 if (contents == NULL) {
1112 return 1;
1113 }
1114
Steven Valdez143e8b32016-07-11 13:19:03 -04001115 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1116 return 0;
1117 }
1118
Adam Langley9b05bc52015-07-01 15:25:33 -07001119 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1120 * this function should never be called, even if the server tries to send the
1121 * extension. */
1122 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1123
1124 if (CBS_len(contents) != 0) {
1125 return 0;
1126 }
1127
1128 ssl->tlsext_ticket_expected = 1;
1129 return 1;
1130}
1131
Adam Langley9b05bc52015-07-01 15:25:33 -07001132static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1133 if (!ssl->tlsext_ticket_expected) {
1134 return 1;
1135 }
1136
1137 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1138 * true. */
1139 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1140
1141 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1142 !CBB_add_u16(out, 0 /* length */)) {
1143 return 0;
1144 }
1145
1146 return 1;
1147}
1148
1149
Adam Langley2e857bd2015-07-01 16:09:19 -07001150/* Signature Algorithms.
1151 *
1152 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1153
1154static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin2dc02042016-09-19 19:57:37 -04001155 uint16_t min_version, max_version;
1156 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1157 return 0;
1158 }
1159
1160 if (max_version < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001161 return 1;
1162 }
1163
David Benjamin0fc37ef2016-08-17 15:29:46 -04001164 const uint16_t *sigalgs;
1165 const size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001166
David Benjamin0fc37ef2016-08-17 15:29:46 -04001167 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001168 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1169 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001170 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001171 return 0;
1172 }
1173
David Benjamin0fc37ef2016-08-17 15:29:46 -04001174 for (size_t i = 0; i < num_sigalgs; i++) {
1175 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001176 return 0;
1177 }
1178 }
1179
1180 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001181 return 0;
1182 }
1183
1184 return 1;
1185}
1186
Adam Langley2e857bd2015-07-01 16:09:19 -07001187static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1188 CBS *contents) {
David Benjamin0fc37ef2016-08-17 15:29:46 -04001189 OPENSSL_free(ssl->s3->hs->peer_sigalgs);
1190 ssl->s3->hs->peer_sigalgs = NULL;
1191 ssl->s3->hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001192
Adam Langley2e857bd2015-07-01 16:09:19 -07001193 if (contents == NULL) {
1194 return 1;
1195 }
1196
1197 CBS supported_signature_algorithms;
1198 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001199 CBS_len(contents) != 0 ||
1200 CBS_len(&supported_signature_algorithms) == 0 ||
1201 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001202 return 0;
1203 }
1204
1205 return 1;
1206}
1207
Adam Langley2e857bd2015-07-01 16:09:19 -07001208
Adam Langleybb0bd042015-07-01 16:21:03 -07001209/* OCSP Stapling.
1210 *
1211 * https://tools.ietf.org/html/rfc6066#section-8 */
1212
1213static void ext_ocsp_init(SSL *ssl) {
1214 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001215 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001216}
1217
1218static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1219 if (!ssl->ocsp_stapling_enabled) {
1220 return 1;
1221 }
1222
1223 CBB contents;
1224 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1225 !CBB_add_u16_length_prefixed(out, &contents) ||
1226 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1227 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1228 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1229 !CBB_flush(out)) {
1230 return 0;
1231 }
1232
Adam Langleyce9d85e2016-01-24 15:58:39 -08001233 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001234 return 1;
1235}
1236
1237static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001238 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001239 if (contents == NULL) {
1240 return 1;
1241 }
1242
David Benjamin942f4ed2016-07-16 19:03:49 +03001243 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001244 /* OCSP stapling is forbidden on non-certificate ciphers. */
1245 if (CBS_len(contents) != 0 ||
1246 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001247 return 0;
1248 }
1249
David Benjamindaa88502016-10-04 16:32:16 -04001250 /* Note this does not check for resumption in TLS 1.2. Sending
1251 * status_request here does not make sense, but OpenSSL does so and the
1252 * specification does not say anything. Tolerate it but ignore it. */
1253
David Benjamin942f4ed2016-07-16 19:03:49 +03001254 ssl->s3->tmp.certificate_status_expected = 1;
1255 return 1;
1256 }
1257
Steven Valdez803c77a2016-09-06 14:13:43 -04001258 /* In TLS 1.3, OCSP stapling is forbidden on resumption. */
1259 if (ssl->s3->session_reused) {
1260 return 0;
1261 }
1262
David Benjamin942f4ed2016-07-16 19:03:49 +03001263 uint8_t status_type;
1264 CBS ocsp_response;
1265 if (!CBS_get_u8(contents, &status_type) ||
1266 status_type != TLSEXT_STATUSTYPE_ocsp ||
1267 !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1268 CBS_len(&ocsp_response) == 0 ||
1269 CBS_len(contents) != 0) {
1270 return 0;
1271 }
1272
Steven Valdez87eab492016-06-27 16:34:59 -04001273 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1274 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001275 *out_alert = SSL_AD_INTERNAL_ERROR;
1276 return 0;
1277 }
1278
Adam Langleybb0bd042015-07-01 16:21:03 -07001279 return 1;
1280}
1281
1282static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1283 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001284 if (contents == NULL) {
1285 return 1;
1286 }
1287
1288 uint8_t status_type;
1289 if (!CBS_get_u8(contents, &status_type)) {
1290 return 0;
1291 }
1292
1293 /* We cannot decide whether OCSP stapling will occur yet because the correct
1294 * SSL_CTX might not have been selected. */
1295 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1296
Adam Langleybb0bd042015-07-01 16:21:03 -07001297 return 1;
1298}
1299
1300static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001301 if (!ssl->s3->tmp.ocsp_stapling_requested ||
1302 ssl->ctx->ocsp_response_length == 0 ||
Steven Valdez803c77a2016-09-06 14:13:43 -04001303 ssl->s3->session_reused ||
1304 (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
1305 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001306 return 1;
1307 }
1308
David Benjamin942f4ed2016-07-16 19:03:49 +03001309 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1310 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez87eab492016-06-27 16:34:59 -04001311 if (ssl->session != NULL) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001312 return 1;
1313 }
1314
1315 ssl->s3->tmp.certificate_status_expected = 1;
1316
1317 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1318 CBB_add_u16(out, 0 /* length */);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001319 }
1320
David Benjamin942f4ed2016-07-16 19:03:49 +03001321 CBB body, ocsp_response;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001322 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
David Benjamin942f4ed2016-07-16 19:03:49 +03001323 CBB_add_u16_length_prefixed(out, &body) &&
1324 CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1325 CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1326 CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1327 ssl->ctx->ocsp_response_length) &&
1328 CBB_flush(out);
Adam Langleybb0bd042015-07-01 16:21:03 -07001329}
1330
1331
Adam Langley97dfcbf2015-07-01 18:35:20 -07001332/* Next protocol negotiation.
1333 *
1334 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1335
1336static void ext_npn_init(SSL *ssl) {
1337 ssl->s3->next_proto_neg_seen = 0;
1338}
1339
1340static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1341 if (ssl->s3->initial_handshake_complete ||
1342 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001343 (ssl->options & SSL_OP_DISABLE_NPN) ||
David Benjamince079fd2016-08-02 16:22:34 -04001344 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001345 return 1;
1346 }
1347
1348 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1349 !CBB_add_u16(out, 0 /* length */)) {
1350 return 0;
1351 }
1352
1353 return 1;
1354}
1355
1356static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1357 CBS *contents) {
1358 if (contents == NULL) {
1359 return 1;
1360 }
1361
Steven Valdez143e8b32016-07-11 13:19:03 -04001362 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1363 return 0;
1364 }
1365
Adam Langley97dfcbf2015-07-01 18:35:20 -07001366 /* If any of these are false then we should never have sent the NPN
1367 * extension in the ClientHello and thus this function should never have been
1368 * called. */
1369 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001370 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001371 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001372 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001373
David Benjamin76c2efc2015-08-31 14:24:29 -04001374 if (ssl->s3->alpn_selected != NULL) {
1375 /* NPN and ALPN may not be negotiated in the same connection. */
1376 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1377 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1378 return 0;
1379 }
1380
Adam Langley97dfcbf2015-07-01 18:35:20 -07001381 const uint8_t *const orig_contents = CBS_data(contents);
1382 const size_t orig_len = CBS_len(contents);
1383
1384 while (CBS_len(contents) != 0) {
1385 CBS proto;
1386 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1387 CBS_len(&proto) == 0) {
1388 return 0;
1389 }
1390 }
1391
1392 uint8_t *selected;
1393 uint8_t selected_len;
1394 if (ssl->ctx->next_proto_select_cb(
1395 ssl, &selected, &selected_len, orig_contents, orig_len,
1396 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1397 *out_alert = SSL_AD_INTERNAL_ERROR;
1398 return 0;
1399 }
1400
David Benjamin79978df2015-12-25 15:56:49 -05001401 OPENSSL_free(ssl->s3->next_proto_negotiated);
1402 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1403 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001404 *out_alert = SSL_AD_INTERNAL_ERROR;
1405 return 0;
1406 }
1407
David Benjamin79978df2015-12-25 15:56:49 -05001408 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001409 ssl->s3->next_proto_neg_seen = 1;
1410
1411 return 1;
1412}
1413
1414static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1415 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001416 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1417 return 1;
1418 }
1419
Adam Langley97dfcbf2015-07-01 18:35:20 -07001420 if (contents != NULL && CBS_len(contents) != 0) {
1421 return 0;
1422 }
1423
1424 if (contents == NULL ||
1425 ssl->s3->initial_handshake_complete ||
1426 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1427 * afterwards, parsing the ALPN extension will clear
1428 * |next_proto_neg_seen|. */
1429 ssl->s3->alpn_selected != NULL ||
1430 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001431 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001432 return 1;
1433 }
1434
1435 ssl->s3->next_proto_neg_seen = 1;
1436 return 1;
1437}
1438
1439static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1440 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1441 * parsed. */
1442 if (!ssl->s3->next_proto_neg_seen) {
1443 return 1;
1444 }
1445
1446 const uint8_t *npa;
1447 unsigned npa_len;
1448
1449 if (ssl->ctx->next_protos_advertised_cb(
1450 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1451 SSL_TLSEXT_ERR_OK) {
1452 ssl->s3->next_proto_neg_seen = 0;
1453 return 1;
1454 }
1455
1456 CBB contents;
1457 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1458 !CBB_add_u16_length_prefixed(out, &contents) ||
1459 !CBB_add_bytes(&contents, npa, npa_len) ||
1460 !CBB_flush(out)) {
1461 return 0;
1462 }
1463
1464 return 1;
1465}
1466
1467
Adam Langleyab8d87d2015-07-10 12:21:39 -07001468/* Signed certificate timestamps.
1469 *
1470 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1471
1472static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1473 if (!ssl->signed_cert_timestamps_enabled) {
1474 return 1;
1475 }
1476
1477 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1478 !CBB_add_u16(out, 0 /* length */)) {
1479 return 0;
1480 }
1481
1482 return 1;
1483}
1484
1485static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1486 CBS *contents) {
1487 if (contents == NULL) {
1488 return 1;
1489 }
1490
1491 /* If this is false then we should never have sent the SCT extension in the
1492 * ClientHello and thus this function should never have been called. */
1493 assert(ssl->signed_cert_timestamps_enabled);
1494
1495 if (CBS_len(contents) == 0) {
1496 *out_alert = SSL_AD_DECODE_ERROR;
1497 return 0;
1498 }
1499
David Benjamindaa88502016-10-04 16:32:16 -04001500 /* Session resumption uses the original session information. The extension
1501 * should not be sent on resumption, but RFC 6962 did not make it a
1502 * requirement, so tolerate this.
1503 *
1504 * TODO(davidben): Enforce this anyway. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001505 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001506 !CBS_stow(
1507 contents,
1508 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1509 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001510 *out_alert = SSL_AD_INTERNAL_ERROR;
1511 return 0;
1512 }
1513
1514 return 1;
1515}
1516
1517static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1518 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001519 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001520}
1521
1522static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001523 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001524 if (ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001525 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001526 return 1;
1527 }
1528
1529 CBB contents;
1530 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1531 CBB_add_u16_length_prefixed(out, &contents) &&
1532 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1533 ssl->ctx->signed_cert_timestamp_list_length) &&
1534 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001535}
1536
1537
Adam Langleyf18e4532015-07-10 13:39:53 -07001538/* Application-level Protocol Negotiation.
1539 *
1540 * https://tools.ietf.org/html/rfc7301 */
1541
1542static void ext_alpn_init(SSL *ssl) {
1543 OPENSSL_free(ssl->s3->alpn_selected);
1544 ssl->s3->alpn_selected = NULL;
1545}
1546
1547static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1548 if (ssl->alpn_client_proto_list == NULL ||
1549 ssl->s3->initial_handshake_complete) {
1550 return 1;
1551 }
1552
1553 CBB contents, proto_list;
1554 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1555 !CBB_add_u16_length_prefixed(out, &contents) ||
1556 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1557 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1558 ssl->alpn_client_proto_list_len) ||
1559 !CBB_flush(out)) {
1560 return 0;
1561 }
1562
1563 return 1;
1564}
1565
1566static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1567 CBS *contents) {
1568 if (contents == NULL) {
1569 return 1;
1570 }
1571
1572 assert(!ssl->s3->initial_handshake_complete);
1573 assert(ssl->alpn_client_proto_list != NULL);
1574
David Benjamin76c2efc2015-08-31 14:24:29 -04001575 if (ssl->s3->next_proto_neg_seen) {
1576 /* NPN and ALPN may not be negotiated in the same connection. */
1577 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1578 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1579 return 0;
1580 }
1581
Adam Langleyf18e4532015-07-10 13:39:53 -07001582 /* The extension data consists of a ProtocolNameList which must have
1583 * exactly one ProtocolName. Each of these is length-prefixed. */
1584 CBS protocol_name_list, protocol_name;
1585 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1586 CBS_len(contents) != 0 ||
1587 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1588 /* Empty protocol names are forbidden. */
1589 CBS_len(&protocol_name) == 0 ||
1590 CBS_len(&protocol_name_list) != 0) {
1591 return 0;
1592 }
1593
David Benjamin3e517572016-08-11 11:52:23 -04001594 /* Check that the protcol name is one of the ones we advertised. */
1595 int protocol_ok = 0;
1596 CBS client_protocol_name_list, client_protocol_name;
1597 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1598 ssl->alpn_client_proto_list_len);
1599 while (CBS_len(&client_protocol_name_list) > 0) {
1600 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1601 &client_protocol_name)) {
1602 *out_alert = SSL_AD_INTERNAL_ERROR;
1603 return 0;
1604 }
1605
1606 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1607 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1608 CBS_len(&protocol_name)) == 0) {
1609 protocol_ok = 1;
1610 break;
1611 }
1612 }
1613
1614 if (!protocol_ok) {
1615 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1616 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1617 return 0;
1618 }
1619
Adam Langleyf18e4532015-07-10 13:39:53 -07001620 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1621 &ssl->s3->alpn_selected_len)) {
1622 *out_alert = SSL_AD_INTERNAL_ERROR;
1623 return 0;
1624 }
1625
1626 return 1;
1627}
1628
1629static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1630 CBS *contents) {
1631 if (contents == NULL) {
1632 return 1;
1633 }
1634
1635 if (ssl->ctx->alpn_select_cb == NULL ||
1636 ssl->s3->initial_handshake_complete) {
1637 return 1;
1638 }
1639
1640 /* ALPN takes precedence over NPN. */
1641 ssl->s3->next_proto_neg_seen = 0;
1642
1643 CBS protocol_name_list;
1644 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1645 CBS_len(contents) != 0 ||
1646 CBS_len(&protocol_name_list) < 2) {
1647 return 0;
1648 }
1649
1650 /* Validate the protocol list. */
1651 CBS protocol_name_list_copy = protocol_name_list;
1652 while (CBS_len(&protocol_name_list_copy) > 0) {
1653 CBS protocol_name;
1654
1655 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1656 /* Empty protocol names are forbidden. */
1657 CBS_len(&protocol_name) == 0) {
1658 return 0;
1659 }
1660 }
1661
1662 const uint8_t *selected;
1663 uint8_t selected_len;
1664 if (ssl->ctx->alpn_select_cb(
1665 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1666 CBS_len(&protocol_name_list),
1667 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1668 OPENSSL_free(ssl->s3->alpn_selected);
1669 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1670 if (ssl->s3->alpn_selected == NULL) {
1671 *out_alert = SSL_AD_INTERNAL_ERROR;
1672 return 0;
1673 }
1674 ssl->s3->alpn_selected_len = selected_len;
1675 }
1676
1677 return 1;
1678}
1679
1680static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1681 if (ssl->s3->alpn_selected == NULL) {
1682 return 1;
1683 }
1684
1685 CBB contents, proto_list, proto;
1686 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1687 !CBB_add_u16_length_prefixed(out, &contents) ||
1688 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1689 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001690 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1691 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001692 !CBB_flush(out)) {
1693 return 0;
1694 }
1695
1696 return 1;
1697}
1698
1699
Adam Langley49c7af12015-07-10 14:33:46 -07001700/* Channel ID.
1701 *
1702 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1703
1704static void ext_channel_id_init(SSL *ssl) {
1705 ssl->s3->tlsext_channel_id_valid = 0;
1706}
1707
1708static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1709 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001710 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001711 return 1;
1712 }
1713
1714 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1715 !CBB_add_u16(out, 0 /* length */)) {
1716 return 0;
1717 }
1718
1719 return 1;
1720}
1721
1722static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1723 CBS *contents) {
1724 if (contents == NULL) {
1725 return 1;
1726 }
1727
Steven Valdez143e8b32016-07-11 13:19:03 -04001728 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1729 return 0;
1730 }
1731
David Benjamince079fd2016-08-02 16:22:34 -04001732 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001733 assert(ssl->tlsext_channel_id_enabled);
1734
1735 if (CBS_len(contents) != 0) {
1736 return 0;
1737 }
1738
1739 ssl->s3->tlsext_channel_id_valid = 1;
1740 return 1;
1741}
1742
1743static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1744 CBS *contents) {
1745 if (contents == NULL ||
1746 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001747 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001748 return 1;
1749 }
1750
1751 if (CBS_len(contents) != 0) {
1752 return 0;
1753 }
1754
1755 ssl->s3->tlsext_channel_id_valid = 1;
1756 return 1;
1757}
1758
1759static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001760 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1761 return 1;
1762 }
1763
Adam Langley49c7af12015-07-10 14:33:46 -07001764 if (!ssl->s3->tlsext_channel_id_valid) {
1765 return 1;
1766 }
1767
1768 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1769 !CBB_add_u16(out, 0 /* length */)) {
1770 return 0;
1771 }
1772
1773 return 1;
1774}
1775
Adam Langley391250d2015-07-15 19:06:07 -07001776
1777/* Secure Real-time Transport Protocol (SRTP) extension.
1778 *
1779 * https://tools.ietf.org/html/rfc5764 */
1780
Adam Langley391250d2015-07-15 19:06:07 -07001781
1782static void ext_srtp_init(SSL *ssl) {
1783 ssl->srtp_profile = NULL;
1784}
1785
1786static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1787 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1788 if (profiles == NULL) {
1789 return 1;
1790 }
1791 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1792 if (num_profiles == 0) {
1793 return 1;
1794 }
1795
1796 CBB contents, profile_ids;
1797 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1798 !CBB_add_u16_length_prefixed(out, &contents) ||
1799 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1800 return 0;
1801 }
1802
David Benjamin54091232016-09-05 12:47:25 -04001803 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001804 if (!CBB_add_u16(&profile_ids,
1805 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1806 return 0;
1807 }
1808 }
1809
1810 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1811 !CBB_flush(out)) {
1812 return 0;
1813 }
1814
1815 return 1;
1816}
1817
1818static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1819 CBS *contents) {
1820 if (contents == NULL) {
1821 return 1;
1822 }
1823
1824 /* The extension consists of a u16-prefixed profile ID list containing a
1825 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1826 *
1827 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1828 CBS profile_ids, srtp_mki;
1829 uint16_t profile_id;
1830 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1831 !CBS_get_u16(&profile_ids, &profile_id) ||
1832 CBS_len(&profile_ids) != 0 ||
1833 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1834 CBS_len(contents) != 0) {
1835 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1836 return 0;
1837 }
1838
1839 if (CBS_len(&srtp_mki) != 0) {
1840 /* Must be no MKI, since we never offer one. */
1841 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1842 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1843 return 0;
1844 }
1845
1846 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1847
1848 /* Check to see if the server gave us something we support (and presumably
1849 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001850 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001851 const SRTP_PROTECTION_PROFILE *profile =
1852 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1853
1854 if (profile->id == profile_id) {
1855 ssl->srtp_profile = profile;
1856 return 1;
1857 }
1858 }
1859
1860 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1861 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1862 return 0;
1863}
1864
1865static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1866 CBS *contents) {
1867 if (contents == NULL) {
1868 return 1;
1869 }
1870
1871 CBS profile_ids, srtp_mki;
1872 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1873 CBS_len(&profile_ids) < 2 ||
1874 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1875 CBS_len(contents) != 0) {
1876 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1877 return 0;
1878 }
1879 /* Discard the MKI value for now. */
1880
1881 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1882 SSL_get_srtp_profiles(ssl);
1883
1884 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001885 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001886 const SRTP_PROTECTION_PROFILE *server_profile =
1887 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1888
1889 CBS profile_ids_tmp;
1890 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1891
1892 while (CBS_len(&profile_ids_tmp) > 0) {
1893 uint16_t profile_id;
1894 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1895 return 0;
1896 }
1897
1898 if (server_profile->id == profile_id) {
1899 ssl->srtp_profile = server_profile;
1900 return 1;
1901 }
1902 }
1903 }
1904
1905 return 1;
1906}
1907
1908static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1909 if (ssl->srtp_profile == NULL) {
1910 return 1;
1911 }
1912
1913 CBB contents, profile_ids;
1914 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1915 !CBB_add_u16_length_prefixed(out, &contents) ||
1916 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1917 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1918 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1919 !CBB_flush(out)) {
1920 return 0;
1921 }
1922
1923 return 1;
1924}
1925
Adam Langleybdd5d662015-07-20 16:19:08 -07001926
1927/* EC point formats.
1928 *
1929 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1930
1931static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
David Benjamince079fd2016-08-02 16:22:34 -04001932 if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001933 return 0;
1934 }
1935
1936 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1937
David Benjamin54091232016-09-05 12:47:25 -04001938 for (size_t i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001939 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1940
1941 const uint32_t alg_k = cipher->algorithm_mkey;
1942 const uint32_t alg_a = cipher->algorithm_auth;
1943 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1944 return 1;
1945 }
1946 }
1947
1948 return 0;
1949}
1950
Adam Langleybdd5d662015-07-20 16:19:08 -07001951static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001952 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001953 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1954 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001955 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1956 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001957 !CBB_flush(out)) {
1958 return 0;
1959 }
1960
1961 return 1;
1962}
1963
1964static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1965 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1966 return 1;
1967 }
1968
1969 return ext_ec_point_add_extension(ssl, out);
1970}
1971
1972static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1973 CBS *contents) {
1974 if (contents == NULL) {
1975 return 1;
1976 }
1977
Steven Valdez143e8b32016-07-11 13:19:03 -04001978 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1979 return 0;
1980 }
1981
Adam Langleybdd5d662015-07-20 16:19:08 -07001982 CBS ec_point_format_list;
1983 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1984 CBS_len(contents) != 0) {
1985 return 0;
1986 }
1987
David Benjaminfc059942015-07-30 23:01:59 -04001988 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1989 * point format. */
1990 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1991 CBS_len(&ec_point_format_list)) == NULL) {
1992 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001993 return 0;
1994 }
1995
1996 return 1;
1997}
1998
1999static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2000 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002001 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
2002 return 1;
2003 }
2004
Adam Langleybdd5d662015-07-20 16:19:08 -07002005 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
2006}
2007
2008static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002009 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
2010 return 1;
2011 }
2012
Adam Langleybdd5d662015-07-20 16:19:08 -07002013 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
2014 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04002015 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07002016
2017 if (!using_ecc) {
2018 return 1;
2019 }
2020
2021 return ext_ec_point_add_extension(ssl, out);
2022}
2023
Steven Valdez4aa154e2016-07-29 14:32:55 -04002024/* Pre Shared Key
2025 *
Steven Valdez5b986082016-09-01 12:29:49 -04002026 * https://tools.ietf.org/html/draft-ietf-tls-tls13-15 */
Steven Valdez4aa154e2016-07-29 14:32:55 -04002027
2028static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
2029 uint16_t min_version, max_version;
2030 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2031 return 0;
2032 }
2033
David Benjamin2dc02042016-09-19 19:57:37 -04002034 uint16_t session_version;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002035 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
David Benjamin2dc02042016-09-19 19:57:37 -04002036 !ssl->method->version_from_wire(&session_version,
2037 ssl->session->ssl_version) ||
2038 session_version < TLS1_3_VERSION) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002039 return 1;
2040 }
2041
Steven Valdez5b986082016-09-01 12:29:49 -04002042 CBB contents, identity, ke_modes, auth_modes, ticket;
Steven Valdez4aa154e2016-07-29 14:32:55 -04002043 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2044 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez5b986082016-09-01 12:29:49 -04002045 !CBB_add_u16_length_prefixed(&contents, &identity) ||
2046 !CBB_add_u8_length_prefixed(&identity, &ke_modes) ||
2047 !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE) ||
2048 !CBB_add_u8_length_prefixed(&identity, &auth_modes) ||
2049 !CBB_add_u8(&auth_modes, SSL_PSK_AUTH) ||
2050 !CBB_add_u16_length_prefixed(&identity, &ticket) ||
2051 !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
Steven Valdez4aa154e2016-07-29 14:32:55 -04002052 ssl->session->tlsext_ticklen)) {
2053 return 0;
2054 }
2055
2056 return CBB_flush(out);
2057}
2058
2059int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2060 CBS *contents) {
2061 uint16_t psk_id;
2062 if (!CBS_get_u16(contents, &psk_id) ||
2063 CBS_len(contents) != 0) {
2064 *out_alert = SSL_AD_DECODE_ERROR;
2065 return 0;
2066 }
2067
2068 if (psk_id != 0) {
2069 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2070 return 0;
2071 }
2072
2073 return 1;
2074}
2075
2076int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
2077 SSL_SESSION **out_session,
2078 uint8_t *out_alert,
2079 CBS *contents) {
Steven Valdez5b986082016-09-01 12:29:49 -04002080 /* We only process the first PSK identity since we don't support pure PSK. */
2081 CBS identity, ke_modes, auth_modes, ticket;
2082 if (!CBS_get_u16_length_prefixed(contents, &identity) ||
2083 !CBS_get_u8_length_prefixed(&identity, &ke_modes) ||
2084 !CBS_get_u8_length_prefixed(&identity, &auth_modes) ||
2085 !CBS_get_u16_length_prefixed(&identity, &ticket) ||
2086 CBS_len(&identity) != 0) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002087 *out_alert = SSL_AD_DECODE_ERROR;
2088 return 0;
2089 }
2090
Steven Valdez5b986082016-09-01 12:29:49 -04002091 /* We only support tickets with PSK_DHE_KE and PSK_AUTH. */
2092 if (memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, CBS_len(&ke_modes)) == NULL ||
2093 memchr(CBS_data(&auth_modes), SSL_PSK_AUTH, CBS_len(&auth_modes)) ==
2094 NULL) {
2095 *out_session = NULL;
2096 return 1;
2097 }
2098
Steven Valdez4aa154e2016-07-29 14:32:55 -04002099 /* TLS 1.3 session tickets are renewed separately as part of the
2100 * NewSessionTicket. */
2101 int renew;
Steven Valdez5b986082016-09-01 12:29:49 -04002102 return tls_process_ticket(ssl, out_session, &renew, CBS_data(&ticket),
2103 CBS_len(&ticket), NULL, 0);
Steven Valdez4aa154e2016-07-29 14:32:55 -04002104}
2105
2106int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
2107 if (!ssl->s3->session_reused) {
2108 return 1;
2109 }
2110
2111 CBB contents;
2112 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2113 !CBB_add_u16_length_prefixed(out, &contents) ||
2114 /* We only consider the first identity for resumption */
2115 !CBB_add_u16(&contents, 0) ||
2116 !CBB_flush(out)) {
2117 return 0;
2118 }
2119
2120 return 1;
2121}
2122
2123
Steven Valdez143e8b32016-07-11 13:19:03 -04002124/* Key Share
2125 *
2126 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
2127
2128static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
2129 uint16_t min_version, max_version;
2130 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2131 return 0;
2132 }
2133
2134 if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
2135 return 1;
2136 }
2137
2138 CBB contents, kse_bytes;
2139 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2140 !CBB_add_u16_length_prefixed(out, &contents) ||
2141 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2142 return 0;
2143 }
2144
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002145 uint16_t group_id;
Steven Valdez5440fe02016-07-18 12:40:30 -04002146 if (ssl->s3->hs->retry_group) {
2147 /* Append the new key share to the old list. */
2148 if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
2149 ssl->s3->hs->key_share_bytes_len)) {
2150 return 0;
2151 }
2152 OPENSSL_free(ssl->s3->hs->key_share_bytes);
2153 ssl->s3->hs->key_share_bytes = NULL;
David Benjamin4fe3c902016-08-16 02:17:03 -04002154 ssl->s3->hs->key_share_bytes_len = 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002155
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002156 group_id = ssl->s3->hs->retry_group;
Steven Valdez5440fe02016-07-18 12:40:30 -04002157 } else {
David Benjamin65ac9972016-09-02 21:35:25 -04002158 /* Add a fake group. See draft-davidben-tls-grease-01. */
2159 if (ssl->ctx->grease_enabled &&
2160 (!CBB_add_u16(&kse_bytes,
2161 ssl_get_grease_value(ssl, ssl_grease_group)) ||
2162 !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2163 !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2164 return 0;
2165 }
2166
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002167 /* Predict the most preferred group. */
2168 const uint16_t *groups;
2169 size_t groups_len;
Steven Valdez5440fe02016-07-18 12:40:30 -04002170 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002171 if (groups_len == 0) {
2172 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2173 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002174 }
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002175
2176 group_id = groups[0];
Steven Valdez5440fe02016-07-18 12:40:30 -04002177 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002178
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002179 CBB key_exchange;
2180 if (!CBB_add_u16(&kse_bytes, group_id) ||
2181 !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2182 !SSL_ECDH_CTX_init(&ssl->s3->hs->ecdh_ctx, group_id) ||
2183 !SSL_ECDH_CTX_offer(&ssl->s3->hs->ecdh_ctx, &key_exchange) ||
2184 !CBB_flush(&kse_bytes)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002185 return 0;
2186 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002187
Steven Valdez5440fe02016-07-18 12:40:30 -04002188 if (!ssl->s3->hs->retry_group) {
2189 /* Save the contents of the extension to repeat it in the second
2190 * ClientHello. */
2191 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2192 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2193 CBB_len(&kse_bytes));
2194 if (ssl->s3->hs->key_share_bytes == NULL) {
2195 return 0;
2196 }
2197 }
2198
Steven Valdez143e8b32016-07-11 13:19:03 -04002199 return CBB_flush(out);
2200}
2201
Steven Valdez7259f2f2016-08-02 16:55:05 -04002202int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2203 size_t *out_secret_len,
2204 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002205 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002206 uint16_t group_id;
2207 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002208 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2209 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002210 *out_alert = SSL_AD_DECODE_ERROR;
2211 return 0;
2212 }
2213
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002214 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->ecdh_ctx) != group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002215 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2216 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2217 return 0;
2218 }
2219
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002220 if (!SSL_ECDH_CTX_finish(&ssl->s3->hs->ecdh_ctx, out_secret, out_secret_len,
2221 out_alert, CBS_data(&peer_key),
2222 CBS_len(&peer_key))) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002223 *out_alert = SSL_AD_INTERNAL_ERROR;
2224 return 0;
2225 }
2226
David Benjamin5c4e8572016-08-19 17:44:53 -04002227 ssl->s3->new_session->key_exchange_info = group_id;
David Benjaminc8b6b4f2016-09-08 23:47:48 -04002228 SSL_ECDH_CTX_cleanup(&ssl->s3->hs->ecdh_ctx);
Steven Valdez143e8b32016-07-11 13:19:03 -04002229 return 1;
2230}
2231
Steven Valdez7259f2f2016-08-02 16:55:05 -04002232int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2233 uint8_t **out_secret,
2234 size_t *out_secret_len,
2235 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002236 uint16_t group_id;
2237 CBS key_shares;
Steven Valdez803c77a2016-09-06 14:13:43 -04002238 if (!tls1_get_shared_group(ssl, &group_id)) {
2239 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2240 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2241 return 0;
2242 }
2243
2244 if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
David Benjamina70de142016-08-02 16:52:57 -04002245 CBS_len(contents) != 0) {
David Benjamin7e1f9842016-09-20 19:24:40 -04002246 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002247 return 0;
2248 }
2249
David Benjamin7e1f9842016-09-20 19:24:40 -04002250 /* Find the corresponding key share. */
2251 int found = 0;
2252 CBS peer_key;
Steven Valdez143e8b32016-07-11 13:19:03 -04002253 while (CBS_len(&key_shares) > 0) {
2254 uint16_t id;
David Benjamin7e1f9842016-09-20 19:24:40 -04002255 CBS peer_key_tmp;
Steven Valdez143e8b32016-07-11 13:19:03 -04002256 if (!CBS_get_u16(&key_shares, &id) ||
David Benjamin7e1f9842016-09-20 19:24:40 -04002257 !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2258 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez143e8b32016-07-11 13:19:03 -04002259 return 0;
2260 }
2261
David Benjamin7e1f9842016-09-20 19:24:40 -04002262 if (id == group_id) {
2263 if (found) {
2264 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2265 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2266 return 0;
2267 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002268
David Benjamin7e1f9842016-09-20 19:24:40 -04002269 found = 1;
2270 peer_key = peer_key_tmp;
2271 /* Continue parsing the structure to keep peers honest. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002272 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002273 }
2274
David Benjamin7e1f9842016-09-20 19:24:40 -04002275 if (!found) {
2276 *out_found = 0;
2277 *out_secret = NULL;
2278 *out_secret_len = 0;
2279 return 1;
2280 }
2281
2282 /* Compute the DH secret. */
2283 uint8_t *secret = NULL;
2284 size_t secret_len;
2285 SSL_ECDH_CTX group;
2286 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2287 CBB public_key;
2288 if (!CBB_init(&public_key, 32) ||
2289 !SSL_ECDH_CTX_init(&group, group_id) ||
2290 !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len,
2291 out_alert, CBS_data(&peer_key),
2292 CBS_len(&peer_key)) ||
2293 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2294 &ssl->s3->hs->public_key_len)) {
2295 OPENSSL_free(secret);
2296 SSL_ECDH_CTX_cleanup(&group);
2297 CBB_cleanup(&public_key);
Steven Valdez803c77a2016-09-06 14:13:43 -04002298 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin7e1f9842016-09-20 19:24:40 -04002299 return 0;
2300 }
2301
2302 SSL_ECDH_CTX_cleanup(&group);
2303
2304 *out_secret = secret;
2305 *out_secret_len = secret_len;
2306 *out_found = 1;
Steven Valdez5440fe02016-07-18 12:40:30 -04002307 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002308}
2309
Steven Valdez7259f2f2016-08-02 16:55:05 -04002310int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002311 uint16_t group_id;
2312 CBB kse_bytes, public_key;
2313 if (!tls1_get_shared_group(ssl, &group_id) ||
2314 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2315 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2316 !CBB_add_u16(&kse_bytes, group_id) ||
2317 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2318 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2319 ssl->s3->hs->public_key_len) ||
2320 !CBB_flush(out)) {
2321 return 0;
2322 }
2323
David Benjamin4fe3c902016-08-16 02:17:03 -04002324 OPENSSL_free(ssl->s3->hs->public_key);
2325 ssl->s3->hs->public_key = NULL;
2326 ssl->s3->hs->public_key_len = 0;
2327
David Benjamin5c4e8572016-08-19 17:44:53 -04002328 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002329 return 1;
2330}
2331
2332
Steven Valdezfdd10992016-09-15 16:27:05 -04002333/* Supported Versions
2334 *
2335 * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2336
2337static int ext_supported_versions_add_clienthello(SSL *ssl, CBB *out) {
2338 uint16_t min_version, max_version;
2339 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2340 return 0;
2341 }
2342
2343 if (max_version <= TLS1_2_VERSION) {
2344 return 1;
2345 }
2346
2347 CBB contents, versions;
2348 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2349 !CBB_add_u16_length_prefixed(out, &contents) ||
2350 !CBB_add_u8_length_prefixed(&contents, &versions)) {
2351 return 0;
2352 }
2353
David Benjamind9791bf2016-09-27 16:39:52 -04002354 /* Add a fake version. See draft-davidben-tls-grease-01. */
2355 if (ssl->ctx->grease_enabled &&
2356 !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2357 return 0;
2358 }
2359
Steven Valdezfdd10992016-09-15 16:27:05 -04002360 for (uint16_t version = max_version; version >= min_version; version--) {
2361 if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2362 return 0;
2363 }
2364 }
2365
2366 if (!CBB_flush(out)) {
2367 return 0;
2368 }
2369
2370 return 1;
2371}
2372
2373
Steven Valdezce902a92016-05-17 11:47:53 -04002374/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002375 *
Steven Valdezce902a92016-05-17 11:47:53 -04002376 * https://tools.ietf.org/html/rfc4492#section-5.1.2
2377 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07002378
Steven Valdezce902a92016-05-17 11:47:53 -04002379static void ext_supported_groups_init(SSL *ssl) {
2380 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2381 ssl->s3->tmp.peer_supported_group_list = NULL;
2382 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07002383}
2384
Steven Valdezce902a92016-05-17 11:47:53 -04002385static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002386 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2387 return 1;
2388 }
2389
Steven Valdezce902a92016-05-17 11:47:53 -04002390 CBB contents, groups_bytes;
2391 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002392 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002393 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002394 return 0;
2395 }
2396
David Benjamin65ac9972016-09-02 21:35:25 -04002397 /* Add a fake group. See draft-davidben-tls-grease-01. */
2398 if (ssl->ctx->grease_enabled &&
2399 !CBB_add_u16(&groups_bytes,
2400 ssl_get_grease_value(ssl, ssl_grease_group))) {
2401 return 0;
2402 }
2403
Steven Valdezce902a92016-05-17 11:47:53 -04002404 const uint16_t *groups;
2405 size_t groups_len;
2406 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002407
David Benjamin54091232016-09-05 12:47:25 -04002408 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002409 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002410 return 0;
2411 }
2412 }
2413
2414 return CBB_flush(out);
2415}
2416
Steven Valdezce902a92016-05-17 11:47:53 -04002417static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2418 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002419 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2420 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002421 return 1;
2422}
2423
Steven Valdezce902a92016-05-17 11:47:53 -04002424static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2425 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002426 if (contents == NULL) {
2427 return 1;
2428 }
2429
Steven Valdezce902a92016-05-17 11:47:53 -04002430 CBS supported_group_list;
2431 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2432 CBS_len(&supported_group_list) == 0 ||
2433 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002434 CBS_len(contents) != 0) {
2435 return 0;
2436 }
2437
Steven Valdezce902a92016-05-17 11:47:53 -04002438 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
2439 CBS_len(&supported_group_list));
2440 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002441 *out_alert = SSL_AD_INTERNAL_ERROR;
2442 return 0;
2443 }
2444
Steven Valdezce902a92016-05-17 11:47:53 -04002445 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002446 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002447 if (!CBS_get_u16(&supported_group_list,
2448 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002449 goto err;
2450 }
2451 }
2452
Steven Valdezce902a92016-05-17 11:47:53 -04002453 assert(CBS_len(&supported_group_list) == 0);
2454 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002455
2456 return 1;
2457
2458err:
Steven Valdezce902a92016-05-17 11:47:53 -04002459 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2460 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002461 *out_alert = SSL_AD_INTERNAL_ERROR;
2462 return 0;
2463}
2464
Steven Valdezce902a92016-05-17 11:47:53 -04002465static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002466 /* Servers don't echo this extension. */
2467 return 1;
2468}
2469
2470
Adam Langley614c66a2015-06-12 15:26:58 -07002471/* kExtensions contains all the supported extensions. */
2472static const struct tls_extension kExtensions[] = {
2473 {
Adam Langley5021b222015-06-12 18:27:58 -07002474 TLSEXT_TYPE_renegotiate,
2475 NULL,
2476 ext_ri_add_clienthello,
2477 ext_ri_parse_serverhello,
2478 ext_ri_parse_clienthello,
2479 ext_ri_add_serverhello,
2480 },
2481 {
Adam Langley614c66a2015-06-12 15:26:58 -07002482 TLSEXT_TYPE_server_name,
2483 ext_sni_init,
2484 ext_sni_add_clienthello,
2485 ext_sni_parse_serverhello,
2486 ext_sni_parse_clienthello,
2487 ext_sni_add_serverhello,
2488 },
Adam Langley0a056712015-07-01 15:03:33 -07002489 {
2490 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002491 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002492 ext_ems_add_clienthello,
2493 ext_ems_parse_serverhello,
2494 ext_ems_parse_clienthello,
2495 ext_ems_add_serverhello,
2496 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002497 {
2498 TLSEXT_TYPE_session_ticket,
2499 NULL,
2500 ext_ticket_add_clienthello,
2501 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002502 /* Ticket extension client parsing is handled in ssl_session.c */
2503 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002504 ext_ticket_add_serverhello,
2505 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002506 {
2507 TLSEXT_TYPE_signature_algorithms,
2508 NULL,
2509 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002510 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002511 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002512 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002513 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002514 {
2515 TLSEXT_TYPE_status_request,
2516 ext_ocsp_init,
2517 ext_ocsp_add_clienthello,
2518 ext_ocsp_parse_serverhello,
2519 ext_ocsp_parse_clienthello,
2520 ext_ocsp_add_serverhello,
2521 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002522 {
2523 TLSEXT_TYPE_next_proto_neg,
2524 ext_npn_init,
2525 ext_npn_add_clienthello,
2526 ext_npn_parse_serverhello,
2527 ext_npn_parse_clienthello,
2528 ext_npn_add_serverhello,
2529 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002530 {
2531 TLSEXT_TYPE_certificate_timestamp,
2532 NULL,
2533 ext_sct_add_clienthello,
2534 ext_sct_parse_serverhello,
2535 ext_sct_parse_clienthello,
2536 ext_sct_add_serverhello,
2537 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002538 {
2539 TLSEXT_TYPE_application_layer_protocol_negotiation,
2540 ext_alpn_init,
2541 ext_alpn_add_clienthello,
2542 ext_alpn_parse_serverhello,
2543 ext_alpn_parse_clienthello,
2544 ext_alpn_add_serverhello,
2545 },
Adam Langley49c7af12015-07-10 14:33:46 -07002546 {
2547 TLSEXT_TYPE_channel_id,
2548 ext_channel_id_init,
2549 ext_channel_id_add_clienthello,
2550 ext_channel_id_parse_serverhello,
2551 ext_channel_id_parse_clienthello,
2552 ext_channel_id_add_serverhello,
2553 },
Adam Langley391250d2015-07-15 19:06:07 -07002554 {
2555 TLSEXT_TYPE_srtp,
2556 ext_srtp_init,
2557 ext_srtp_add_clienthello,
2558 ext_srtp_parse_serverhello,
2559 ext_srtp_parse_clienthello,
2560 ext_srtp_add_serverhello,
2561 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002562 {
2563 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002564 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002565 ext_ec_point_add_clienthello,
2566 ext_ec_point_parse_serverhello,
2567 ext_ec_point_parse_clienthello,
2568 ext_ec_point_add_serverhello,
2569 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002570 {
Steven Valdez143e8b32016-07-11 13:19:03 -04002571 TLSEXT_TYPE_key_share,
2572 NULL,
2573 ext_key_share_add_clienthello,
2574 forbid_parse_serverhello,
2575 ignore_parse_clienthello,
2576 dont_add_serverhello,
2577 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002578 {
2579 TLSEXT_TYPE_pre_shared_key,
2580 NULL,
2581 ext_pre_shared_key_add_clienthello,
2582 forbid_parse_serverhello,
2583 ignore_parse_clienthello,
2584 dont_add_serverhello,
2585 },
Steven Valdezfdd10992016-09-15 16:27:05 -04002586 {
2587 TLSEXT_TYPE_supported_versions,
2588 NULL,
2589 ext_supported_versions_add_clienthello,
2590 forbid_parse_serverhello,
2591 ignore_parse_clienthello,
2592 dont_add_serverhello,
2593 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002594 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2595 * intolerant to the last extension being zero-length. See
2596 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002597 {
Steven Valdezce902a92016-05-17 11:47:53 -04002598 TLSEXT_TYPE_supported_groups,
2599 ext_supported_groups_init,
2600 ext_supported_groups_add_clienthello,
2601 ext_supported_groups_parse_serverhello,
2602 ext_supported_groups_parse_clienthello,
2603 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002604 },
Adam Langley614c66a2015-06-12 15:26:58 -07002605};
2606
2607#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2608
Adam Langley4cfa96b2015-07-01 11:56:55 -07002609OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2610 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002611 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002612OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2613 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2614 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002615 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002616
Adam Langley614c66a2015-06-12 15:26:58 -07002617static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2618 uint16_t value) {
2619 unsigned i;
2620 for (i = 0; i < kNumExtensions; i++) {
2621 if (kExtensions[i].value == value) {
2622 *out_index = i;
2623 return &kExtensions[i];
2624 }
2625 }
2626
2627 return NULL;
2628}
2629
Adam Langley09505632015-07-30 18:10:13 -07002630int SSL_extension_supported(unsigned extension_value) {
2631 uint32_t index;
2632 return extension_value == TLSEXT_TYPE_padding ||
2633 tls_extension_find(&index, extension_value) != NULL;
2634}
2635
David Benjamine8d53502015-10-10 14:13:23 -04002636int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002637 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002638 if (ssl->client_version == SSL3_VERSION &&
2639 !ssl->s3->send_connection_binding) {
2640 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002641 }
Adam Langley95c29f32014-06-20 12:00:00 -07002642
David Benjamine8d53502015-10-10 14:13:23 -04002643 CBB extensions;
2644 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002645 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002646 }
Adam Langley95c29f32014-06-20 12:00:00 -07002647
David Benjamine8d53502015-10-10 14:13:23 -04002648 ssl->s3->tmp.extensions.sent = 0;
2649 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002650
David Benjamin54091232016-09-05 12:47:25 -04002651 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002652 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002653 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002654 }
2655 }
Adam Langley95c29f32014-06-20 12:00:00 -07002656
David Benjamin65ac9972016-09-02 21:35:25 -04002657 uint16_t grease_ext1 = 0;
2658 if (ssl->ctx->grease_enabled) {
2659 /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2660 grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2661 if (!CBB_add_u16(&extensions, grease_ext1) ||
2662 !CBB_add_u16(&extensions, 0 /* zero length */)) {
2663 goto err;
2664 }
2665 }
2666
David Benjamin54091232016-09-05 12:47:25 -04002667 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002668 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002669 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002670 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2671 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2672 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002673 }
Adam Langley95c29f32014-06-20 12:00:00 -07002674
Adam Langley33ad2b52015-07-20 17:43:53 -07002675 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002676 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002677 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002678 }
Adam Langley75712922014-10-10 16:23:43 -07002679
David Benjamine8d53502015-10-10 14:13:23 -04002680 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002681 goto err;
2682 }
2683
David Benjamin65ac9972016-09-02 21:35:25 -04002684 if (ssl->ctx->grease_enabled) {
2685 /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2686 uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2687
2688 /* The two fake extensions must not have the same value. GREASE values are
2689 * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2690 * one. */
2691 if (grease_ext1 == grease_ext2) {
2692 grease_ext2 ^= 0x1010;
2693 }
2694
2695 if (!CBB_add_u16(&extensions, grease_ext2) ||
2696 !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2697 !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2698 goto err;
2699 }
2700 }
2701
David Benjamince079fd2016-08-02 16:22:34 -04002702 if (!SSL_is_dtls(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002703 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002704 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002705 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002706 *
2707 * NB: because this code works out the length of all existing extensions
2708 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002709 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002710 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002711 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002712 * Server 7.0 is intolerant to the last extension being zero-length. See
2713 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002714 if (padding_len >= 4 + 1) {
2715 padding_len -= 4;
2716 } else {
2717 padding_len = 1;
2718 }
Adam Langley95c29f32014-06-20 12:00:00 -07002719
Adam Langley33ad2b52015-07-20 17:43:53 -07002720 uint8_t *padding_bytes;
2721 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2722 !CBB_add_u16(&extensions, padding_len) ||
2723 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2724 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002725 }
Adam Langley75712922014-10-10 16:23:43 -07002726
Adam Langley33ad2b52015-07-20 17:43:53 -07002727 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002728 }
2729 }
Adam Langley75712922014-10-10 16:23:43 -07002730
David Benjamina01deee2015-12-08 18:56:31 -05002731 /* Discard empty extensions blocks. */
2732 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002733 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002734 }
2735
David Benjamine8d53502015-10-10 14:13:23 -04002736 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002737
2738err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002739 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002740 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002741}
Adam Langley95c29f32014-06-20 12:00:00 -07002742
David Benjamin56380462015-10-10 14:59:09 -04002743int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002744 CBB extensions;
2745 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002746 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002747 }
2748
2749 unsigned i;
2750 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002751 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002752 /* Don't send extensions that were not received. */
2753 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002754 }
Adam Langley95c29f32014-06-20 12:00:00 -07002755
David Benjamin56380462015-10-10 14:59:09 -04002756 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002757 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2758 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2759 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002760 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002761 }
Adam Langley95c29f32014-06-20 12:00:00 -07002762
David Benjamin56380462015-10-10 14:59:09 -04002763 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002764 goto err;
2765 }
2766
Steven Valdez143e8b32016-07-11 13:19:03 -04002767 /* Discard empty extensions blocks before TLS 1.3. */
2768 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2769 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002770 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002771 }
2772
David Benjamin56380462015-10-10 14:59:09 -04002773 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002774
2775err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002776 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002777 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002778}
Adam Langley95c29f32014-06-20 12:00:00 -07002779
David Benjamine14ff062016-08-09 16:21:24 -04002780static int ssl_scan_clienthello_tlsext(
2781 SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
2782 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002783 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002784 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002785 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002786 }
2787 }
2788
David Benjamin0d56f882015-12-19 17:05:56 -05002789 ssl->s3->tmp.extensions.received = 0;
2790 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002791
David Benjamine14ff062016-08-09 16:21:24 -04002792 CBS extensions;
2793 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2794 while (CBS_len(&extensions) != 0) {
2795 uint16_t type;
2796 CBS extension;
2797
2798 /* Decode the next extension. */
2799 if (!CBS_get_u16(&extensions, &type) ||
2800 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002801 *out_alert = SSL_AD_DECODE_ERROR;
2802 return 0;
2803 }
Adam Langley95c29f32014-06-20 12:00:00 -07002804
David Benjamine14ff062016-08-09 16:21:24 -04002805 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2806 * ambiguous. Ignore all but the renegotiation_info extension. */
2807 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2808 continue;
2809 }
Adam Langley95c29f32014-06-20 12:00:00 -07002810
David Benjamine14ff062016-08-09 16:21:24 -04002811 unsigned ext_index;
2812 const struct tls_extension *const ext =
2813 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002814
David Benjamine14ff062016-08-09 16:21:24 -04002815 if (ext == NULL) {
2816 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002817 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002818 return 0;
2819 }
David Benjamine14ff062016-08-09 16:21:24 -04002820 continue;
2821 }
2822
2823 ssl->s3->tmp.extensions.received |= (1u << ext_index);
2824 uint8_t alert = SSL_AD_DECODE_ERROR;
2825 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2826 *out_alert = alert;
2827 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2828 ERR_add_error_dataf("extension: %u", (unsigned)type);
2829 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002830 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002831 }
Adam Langley75712922014-10-10 16:23:43 -07002832
David Benjamin1deb41b2016-08-09 19:36:38 -04002833 for (size_t i = 0; i < kNumExtensions; i++) {
2834 if (ssl->s3->tmp.extensions.received & (1u << i)) {
2835 continue;
2836 }
2837
2838 CBS *contents = NULL, fake_contents;
2839 static const uint8_t kFakeRenegotiateExtension[] = {0};
2840 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2841 ssl_client_cipher_list_contains_cipher(client_hello,
2842 SSL3_CK_SCSV & 0xffff)) {
2843 /* The renegotiation SCSV was received so pretend that we received a
2844 * renegotiation extension. */
2845 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2846 sizeof(kFakeRenegotiateExtension));
2847 contents = &fake_contents;
2848 ssl->s3->tmp.extensions.received |= (1u << i);
2849 }
2850
2851 /* Extension wasn't observed so call the callback with a NULL
2852 * parameter. */
2853 uint8_t alert = SSL_AD_DECODE_ERROR;
2854 if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
2855 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2856 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2857 *out_alert = alert;
2858 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002859 }
2860 }
2861
Adam Langleyfcf25832014-12-18 17:42:32 -08002862 return 1;
2863}
Adam Langley95c29f32014-06-20 12:00:00 -07002864
David Benjamine14ff062016-08-09 16:21:24 -04002865int ssl_parse_clienthello_tlsext(
2866 SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002867 int alert = -1;
David Benjamine14ff062016-08-09 16:21:24 -04002868 if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002869 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002870 return 0;
2871 }
Adam Langley95c29f32014-06-20 12:00:00 -07002872
David Benjamin0d56f882015-12-19 17:05:56 -05002873 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002874 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002875 return 0;
2876 }
Adam Langley95c29f32014-06-20 12:00:00 -07002877
Adam Langleyfcf25832014-12-18 17:42:32 -08002878 return 1;
2879}
Adam Langley95c29f32014-06-20 12:00:00 -07002880
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002881OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2882
David Benjamin0d56f882015-12-19 17:05:56 -05002883static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002884 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2885 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2886 return 1;
2887 }
Adam Langley614c66a2015-06-12 15:26:58 -07002888
Steven Valdez143e8b32016-07-11 13:19:03 -04002889 /* Decode the extensions block and check it is valid. */
2890 CBS extensions;
2891 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2892 !tls1_check_duplicate_extensions(&extensions)) {
2893 *out_alert = SSL_AD_DECODE_ERROR;
2894 return 0;
2895 }
2896
2897 uint32_t received = 0;
2898 while (CBS_len(&extensions) != 0) {
2899 uint16_t type;
2900 CBS extension;
2901
2902 /* Decode the next extension. */
2903 if (!CBS_get_u16(&extensions, &type) ||
2904 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002905 *out_alert = SSL_AD_DECODE_ERROR;
2906 return 0;
2907 }
Adam Langley95c29f32014-06-20 12:00:00 -07002908
Steven Valdez143e8b32016-07-11 13:19:03 -04002909 unsigned ext_index;
2910 const struct tls_extension *const ext =
2911 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002912
Steven Valdez143e8b32016-07-11 13:19:03 -04002913 if (ext == NULL) {
2914 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002915 return 0;
2916 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002917 continue;
2918 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002919
David Benjamin1deb41b2016-08-09 19:36:38 -04002920 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index)) &&
2921 type != TLSEXT_TYPE_renegotiate) {
2922 /* If the extension was never sent then it is illegal, except for the
2923 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002924 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2925 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002926 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002927 return 0;
2928 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002929
Steven Valdez143e8b32016-07-11 13:19:03 -04002930 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002931
Steven Valdez143e8b32016-07-11 13:19:03 -04002932 uint8_t alert = SSL_AD_DECODE_ERROR;
2933 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2934 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2935 ERR_add_error_dataf("extension: %u", (unsigned)type);
2936 *out_alert = alert;
2937 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002938 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002939 }
Adam Langley95c29f32014-06-20 12:00:00 -07002940
David Benjamin54091232016-09-05 12:47:25 -04002941 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002942 if (!(received & (1u << i))) {
2943 /* Extension wasn't observed so call the callback with a NULL
2944 * parameter. */
2945 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002946 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002947 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2948 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002949 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002950 return 0;
2951 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002952 }
2953 }
Adam Langley95c29f32014-06-20 12:00:00 -07002954
Adam Langleyfcf25832014-12-18 17:42:32 -08002955 return 1;
2956}
Adam Langley95c29f32014-06-20 12:00:00 -07002957
David Benjamin0d56f882015-12-19 17:05:56 -05002958static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002959 int ret = SSL_TLSEXT_ERR_NOACK;
2960 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002961
David Benjamin78f8aab2016-03-10 16:33:58 -05002962 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002963 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002964 ssl->ctx->tlsext_servername_arg);
2965 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002966 ret = ssl->initial_ctx->tlsext_servername_callback(
2967 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002968 }
Adam Langley95c29f32014-06-20 12:00:00 -07002969
Adam Langleyfcf25832014-12-18 17:42:32 -08002970 switch (ret) {
2971 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002972 ssl3_send_alert(ssl, SSL3_AL_FATAL, 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_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002976 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
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 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002980 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002981 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002982
Adam Langleyfcf25832014-12-18 17:42:32 -08002983 default:
2984 return 1;
2985 }
2986}
Adam Langleyed8270a2014-09-02 13:52:56 -07002987
David Benjamin0d56f882015-12-19 17:05:56 -05002988static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002989 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002990 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002991
David Benjamin78f8aab2016-03-10 16:33:58 -05002992 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002993 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002994 ssl->ctx->tlsext_servername_arg);
2995 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002996 ret = ssl->initial_ctx->tlsext_servername_callback(
2997 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002998 }
Adam Langley95c29f32014-06-20 12:00:00 -07002999
Adam Langleyfcf25832014-12-18 17:42:32 -08003000 switch (ret) {
3001 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05003002 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003003 return -1;
David Benjamin03973092014-06-24 23:27:17 -04003004
Adam Langleyfcf25832014-12-18 17:42:32 -08003005 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05003006 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08003007 return 1;
3008
3009 default:
3010 return 1;
3011 }
3012}
3013
David Benjamin0d56f882015-12-19 17:05:56 -05003014int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003015 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05003016 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
3017 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08003018 return 0;
3019 }
3020
David Benjamin0d56f882015-12-19 17:05:56 -05003021 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04003022 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08003023 return 0;
3024 }
3025
3026 return 1;
3027}
Adam Langley95c29f32014-06-20 12:00:00 -07003028
David Benjamine3aa1d92015-06-16 15:34:50 -04003029int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05003030 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04003031 size_t ticket_len, const uint8_t *session_id,
3032 size_t session_id_len) {
3033 int ret = 1; /* Most errors are non-fatal. */
3034 SSL_CTX *ssl_ctx = ssl->initial_ctx;
3035 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07003036
David Benjamine3aa1d92015-06-16 15:34:50 -04003037 HMAC_CTX hmac_ctx;
3038 HMAC_CTX_init(&hmac_ctx);
3039 EVP_CIPHER_CTX cipher_ctx;
3040 EVP_CIPHER_CTX_init(&cipher_ctx);
3041
David Benjaminef1b0092015-11-21 14:05:44 -05003042 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04003043 *out_session = NULL;
3044
Steven Valdez4aa154e2016-07-29 14:32:55 -04003045 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
3046 goto done;
3047 }
3048
David Benjamine3aa1d92015-06-16 15:34:50 -04003049 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
3050 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003051 }
3052
David Benjaminadcc3952015-04-26 13:07:57 -04003053 /* Ensure there is room for the key name and the largest IV
3054 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
3055 * the maximum IV length should be well under the minimum size for the
3056 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003057 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
3058 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003059 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003060 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08003061
David Benjamine3aa1d92015-06-16 15:34:50 -04003062 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05003063 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
3064 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
3065 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04003066 if (cb_ret < 0) {
3067 ret = 0;
3068 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003069 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003070 if (cb_ret == 0) {
3071 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003072 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003073 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05003074 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08003075 }
3076 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04003077 /* Check the key name matches. */
3078 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
3079 SSL_TICKET_KEY_NAME_LEN) != 0) {
3080 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003081 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003082 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
3083 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08003084 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04003085 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
3086 ssl_ctx->tlsext_tick_aes_key, iv)) {
3087 ret = 0;
3088 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003089 }
3090 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003091 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08003092
David Benjamine3aa1d92015-06-16 15:34:50 -04003093 /* Check the MAC at the end of the ticket. */
3094 uint8_t mac[EVP_MAX_MD_SIZE];
3095 size_t mac_len = HMAC_size(&hmac_ctx);
3096 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04003097 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04003098 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003099 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003100 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
3101 HMAC_Final(&hmac_ctx, mac, NULL);
David Benjaminfbc45d72016-09-22 01:21:24 -04003102 int mac_ok =
3103 CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3104#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3105 mac_ok = 1;
3106#endif
3107 if (!mac_ok) {
David Benjamine3aa1d92015-06-16 15:34:50 -04003108 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003109 }
3110
David Benjamine3aa1d92015-06-16 15:34:50 -04003111 /* Decrypt the session data. */
3112 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
3113 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
3114 mac_len;
3115 plaintext = OPENSSL_malloc(ciphertext_len);
3116 if (plaintext == NULL) {
3117 ret = 0;
3118 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003119 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003120 size_t plaintext_len;
3121#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3122 memcpy(plaintext, ciphertext, ciphertext_len);
3123 plaintext_len = ciphertext_len;
3124#else
David Benjamine3aa1d92015-06-16 15:34:50 -04003125 if (ciphertext_len >= INT_MAX) {
3126 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003127 }
David Benjamine3aa1d92015-06-16 15:34:50 -04003128 int len1, len2;
3129 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
3130 (int)ciphertext_len) ||
3131 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
3132 ERR_clear_error(); /* Don't leave an error on the queue. */
3133 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08003134 }
David Benjaminfbc45d72016-09-22 01:21:24 -04003135 plaintext_len = (size_t)(len1 + len2);
3136#endif
Adam Langleyfcf25832014-12-18 17:42:32 -08003137
David Benjamine3aa1d92015-06-16 15:34:50 -04003138 /* Decode the session. */
David Benjaminfbc45d72016-09-22 01:21:24 -04003139 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len);
David Benjamine3aa1d92015-06-16 15:34:50 -04003140 if (session == NULL) {
3141 ERR_clear_error(); /* Don't leave an error on the queue. */
3142 goto done;
3143 }
3144
3145 /* Copy the client's session ID into the new session, to denote the ticket has
3146 * been accepted. */
3147 memcpy(session->session_id, session_id, session_id_len);
3148 session->session_id_length = session_id_len;
3149
Steven Valdez4aa154e2016-07-29 14:32:55 -04003150 if (!ssl_session_is_context_valid(ssl, session) ||
3151 !ssl_session_is_time_valid(ssl, session)) {
3152 SSL_SESSION_free(session);
3153 session = NULL;
3154 }
3155
David Benjamine3aa1d92015-06-16 15:34:50 -04003156 *out_session = session;
3157
3158done:
3159 OPENSSL_free(plaintext);
3160 HMAC_CTX_cleanup(&hmac_ctx);
3161 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3162 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003163}
Adam Langley95c29f32014-06-20 12:00:00 -07003164
Steven Valdez0d62f262015-09-04 12:41:04 -04003165int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003166 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05003167 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003168 return 1;
3169 }
David Benjamincd996942014-07-20 16:23:51 -04003170
David Benjamin0fc37ef2016-08-17 15:29:46 -04003171 SSL_HANDSHAKE *hs = ssl->s3->hs;
3172 OPENSSL_free(hs->peer_sigalgs);
3173 hs->peer_sigalgs = NULL;
3174 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003175
3176 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003177 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003178 return 0;
3179 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003180 num_sigalgs /= 2;
3181
3182 /* supported_signature_algorithms in the certificate request is
3183 * allowed to be empty. */
3184 if (num_sigalgs == 0) {
3185 return 1;
3186 }
3187
Steven Valdez02563852016-06-23 13:33:05 -04003188 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3189 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003190 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3191 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003192 return 0;
3193 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003194 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003195
3196 CBS sigalgs;
3197 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003198 for (size_t i = 0; i < num_sigalgs; i++) {
3199 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003200 return 0;
3201 }
3202 }
Adam Langley95c29f32014-06-20 12:00:00 -07003203
Adam Langleyfcf25832014-12-18 17:42:32 -08003204 return 1;
3205}
David Benjaminec2f27d2014-11-13 19:17:25 -05003206
David Benjaminea9a0d52016-07-08 15:52:59 -07003207int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003208 CERT *cert = ssl->cert;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003209 SSL_HANDSHAKE *hs = ssl->s3->hs;
David Benjaminec2f27d2014-11-13 19:17:25 -05003210
Steven Valdezf0451ca2016-06-29 13:16:27 -04003211 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3212 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3213 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003214 int type = ssl_private_key_type(ssl);
3215 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003216 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003217 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003218 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003219 if (ssl_is_ecdsa_key_type(type)) {
3220 *out = SSL_SIGN_ECDSA_SHA1;
3221 return 1;
3222 }
3223 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3224 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003225 }
3226
Steven Valdezeff1e8d2016-07-06 14:24:47 -04003227 const uint16_t *sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003228 size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07003229 if (cert->sigalgs != NULL) {
3230 sigalgs = cert->sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003231 num_sigalgs = cert->num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003232 }
3233
David Benjamin0fc37ef2016-08-17 15:29:46 -04003234 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3235 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3236 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003237 /* If the client didn't specify any signature_algorithms extension then
3238 * we can assume that it supports SHA1. See
3239 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3240 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3241 SSL_SIGN_ECDSA_SHA1};
3242 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003243 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003244 }
3245
David Benjamin0fc37ef2016-08-17 15:29:46 -04003246 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003247 uint16_t sigalg = sigalgs[i];
3248 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3249 * negotiated. */
3250 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3251 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3252 continue;
3253 }
3254
David Benjamin0fc37ef2016-08-17 15:29:46 -04003255 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003256 if (sigalg == peer_sigalgs[j]) {
3257 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003258 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003259 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003260 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003261 }
Adam Langley95c29f32014-06-20 12:00:00 -07003262
David Benjaminea9a0d52016-07-08 15:52:59 -07003263 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3264 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003265}
Adam Langley95c29f32014-06-20 12:00:00 -07003266
David Benjamind6a4ae92015-08-06 11:10:51 -04003267int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
3268 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003269 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003270
3271 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003272 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
3273 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08003274 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003275
David Benjamind6a4ae92015-08-06 11:10:51 -04003276 static const char kClientIDMagic[] = "TLS Channel ID signature";
3277 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
3278
Steven Valdez87eab492016-06-27 16:34:59 -04003279 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003280 static const char kResumptionMagic[] = "Resumption";
3281 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
3282 if (ssl->session->original_handshake_hash_len == 0) {
3283 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3284 goto err;
3285 }
3286 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
3287 ssl->session->original_handshake_hash_len);
3288 }
3289
3290 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3291 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3292 sizeof(handshake_hash));
3293 if (handshake_hash_len < 0) {
3294 goto err;
3295 }
3296 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
3297 unsigned len_u;
3298 EVP_DigestFinal_ex(&ctx, out, &len_u);
3299 *out_len = len_u;
3300
3301 ret = 1;
3302
3303err:
3304 EVP_MD_CTX_cleanup(&ctx);
3305 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003306}
Adam Langley1258b6a2014-06-20 12:00:00 -07003307
3308/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003309 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003310 * data. */
3311int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003312 int digest_len;
3313 /* This function should never be called for a resumed session because the
3314 * handshake hashes that we wish to record are for the original, full
3315 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003316 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003317 return -1;
3318 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003319
Adam Langleyfcf25832014-12-18 17:42:32 -08003320 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003321 tls1_handshake_digest(
3322 ssl, ssl->s3->new_session->original_handshake_hash,
3323 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003324 if (digest_len < 0) {
3325 return -1;
3326 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003327
Steven Valdez87eab492016-06-27 16:34:59 -04003328 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003329
Adam Langleyfcf25832014-12-18 17:42:32 -08003330 return 1;
3331}