blob: 496c9c4482d88bfb7500a614b7f9c955b89a4456 [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 size_t i;
386
Steven Valdezce902a92016-05-17 11:47:53 -0400387 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
388 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 return 0;
390 }
391
392 for (i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400393 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
394 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800395 return 0;
396 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 }
398
Steven Valdezce902a92016-05-17 11:47:53 -0400399 OPENSSL_free(*out_group_ids);
400 *out_group_ids = group_ids;
401 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800402
403 return 1;
404}
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Steven Valdezce902a92016-05-17 11:47:53 -0400406/* tls1_curve_params_from_ec_key sets |*out_group_id| and |*out_comp_id| to the
407 * TLS group ID and point format, respectively, for |ec|. It returns one on
David Benjamin072334d2014-07-13 16:24:27 -0400408 * success and zero on failure. */
Steven Valdezce902a92016-05-17 11:47:53 -0400409static int tls1_curve_params_from_ec_key(uint16_t *out_group_id,
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 uint8_t *out_comp_id, EC_KEY *ec) {
411 int nid;
412 uint16_t id;
413 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 if (ec == NULL) {
416 return 0;
417 }
Adam Langley95c29f32014-06-20 12:00:00 -0700418
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 grp = EC_KEY_get0_group(ec);
420 if (grp == NULL) {
421 return 0;
422 }
David Benjamin072334d2014-07-13 16:24:27 -0400423
Steven Valdezce902a92016-05-17 11:47:53 -0400424 /* Determine group ID */
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 nid = EC_GROUP_get_curve_name(grp);
Steven Valdezce902a92016-05-17 11:47:53 -0400426 if (!ssl_nid_to_group_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 return 0;
428 }
David Benjamin072334d2014-07-13 16:24:27 -0400429
Steven Valdezce902a92016-05-17 11:47:53 -0400430 /* Set the named group ID. Arbitrary explicit groups are not supported. */
431 *out_group_id = id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800432
433 if (out_comp_id) {
434 if (EC_KEY_get0_public_key(ec) == NULL) {
435 return 0;
436 }
437 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
438 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
439 } else {
440 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
441 }
442 }
443
444 return 1;
445}
David Benjamin072334d2014-07-13 16:24:27 -0400446
Steven Valdezce902a92016-05-17 11:47:53 -0400447/* tls1_check_group_id returns one if |group_id| is consistent with both our
448 * and the peer's group preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400449 * preferences are checked; the peer (the server) does not send preferences. */
Steven Valdezce902a92016-05-17 11:47:53 -0400450int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
451 const uint16_t *groups;
452 size_t groups_len, i, get_peer_groups;
David Benjamin42e9a772014-09-02 23:18:44 -0400453
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 /* Check against our list, then the peer's list. */
Steven Valdezce902a92016-05-17 11:47:53 -0400455 for (get_peer_groups = 0; get_peer_groups <= 1; get_peer_groups++) {
456 if (get_peer_groups && !ssl->server) {
David Benjamin55a43642015-04-20 14:45:55 -0400457 /* Servers do not present a preference list so, if we are a client, only
458 * check our list. */
459 continue;
460 }
461
Steven Valdezce902a92016-05-17 11:47:53 -0400462 tls1_get_grouplist(ssl, get_peer_groups, &groups, &groups_len);
463 if (get_peer_groups && groups_len == 0) {
464 /* Clients are not required to send a supported_groups extension. In this
465 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin55a43642015-04-20 14:45:55 -0400466 * section 4, paragraph 3. */
467 continue;
468 }
Steven Valdezce902a92016-05-17 11:47:53 -0400469 for (i = 0; i < groups_len; i++) {
470 if (groups[i] == group_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 break;
472 }
473 }
Adam Langley95c29f32014-06-20 12:00:00 -0700474
Steven Valdezce902a92016-05-17 11:47:53 -0400475 if (i == groups_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800476 return 0;
477 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 }
David Benjamin033e5f42014-11-13 18:47:41 -0500479
Adam Langleyfcf25832014-12-18 17:42:32 -0800480 return 1;
481}
David Benjamin033e5f42014-11-13 18:47:41 -0500482
David Benjamin0d56f882015-12-19 17:05:56 -0500483int tls1_check_ec_cert(SSL *ssl, X509 *x) {
David Benjamin75ea5bb2016-07-08 17:43:29 -0700484 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
485 /* In TLS 1.3, the ECDSA curve is negotiated via signature algorithms. */
486 return 1;
487 }
488
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 EVP_PKEY *pkey = X509_get_pubkey(x);
David Benjamin75ea5bb2016-07-08 17:43:29 -0700490 if (pkey == NULL) {
491 return 0;
492 }
493
494 int ret = 0;
Steven Valdezce902a92016-05-17 11:47:53 -0400495 uint16_t group_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 uint8_t comp_id;
David Benjamin758d1272015-11-20 17:47:25 -0500497 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
498 if (ec_key == NULL ||
Steven Valdezce902a92016-05-17 11:47:53 -0400499 !tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) ||
500 !tls1_check_group_id(ssl, group_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400501 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800502 goto done;
503 }
504
505 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500506
507done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400508 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800509 return ret;
510}
David Benjamin42e9a772014-09-02 23:18:44 -0400511
Adam Langley95c29f32014-06-20 12:00:00 -0700512/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800513 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700514
Steven Valdez02563852016-06-23 13:33:05 -0400515static const uint16_t kDefaultSignatureAlgorithms[] = {
516 SSL_SIGN_RSA_PKCS1_SHA512,
517 SSL_SIGN_ECDSA_SECP521R1_SHA512,
Adam Langley95c29f32014-06-20 12:00:00 -0700518
Steven Valdez02563852016-06-23 13:33:05 -0400519 SSL_SIGN_RSA_PKCS1_SHA384,
520 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Adam Langley95c29f32014-06-20 12:00:00 -0700521
Steven Valdez02563852016-06-23 13:33:05 -0400522 SSL_SIGN_RSA_PKCS1_SHA256,
523 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Adam Langley95c29f32014-06-20 12:00:00 -0700524
Steven Valdez02563852016-06-23 13:33:05 -0400525 SSL_SIGN_RSA_PKCS1_SHA1,
526 SSL_SIGN_ECDSA_SHA1,
Adam Langley95c29f32014-06-20 12:00:00 -0700527};
David Benjamin05da6e12014-07-12 20:42:55 -0400528
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400529static const uint16_t kDefaultTLS13SignatureAlgorithms[] = {
530 SSL_SIGN_RSA_PSS_SHA512,
531 SSL_SIGN_RSA_PKCS1_SHA512,
532 SSL_SIGN_ECDSA_SECP521R1_SHA512,
533
534 SSL_SIGN_RSA_PSS_SHA384,
535 SSL_SIGN_RSA_PKCS1_SHA384,
536 SSL_SIGN_ECDSA_SECP384R1_SHA384,
537
538 SSL_SIGN_RSA_PSS_SHA256,
539 SSL_SIGN_RSA_PKCS1_SHA256,
540 SSL_SIGN_ECDSA_SECP256R1_SHA256,
541
542 SSL_SIGN_RSA_PKCS1_SHA1,
543 SSL_SIGN_ECDSA_SHA1,
544};
545
Steven Valdez02563852016-06-23 13:33:05 -0400546size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
David Benjamin0e950152016-08-13 21:08:56 -0400547 uint16_t min_version, max_version;
548 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
549 assert(0); /* This should never happen. */
550
551 /* Return an empty list. */
552 ERR_clear_error();
553 *psigs = NULL;
554 return 0;
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400555 }
556
David Benjamin0e950152016-08-13 21:08:56 -0400557 /* TODO(davidben): Once TLS 1.3 has finalized, probably just advertise the
558 * same algorithm list regardless, as long as no fallback is needed. Note this
559 * may require care due to lingering NSS servers affected by
560 * https://bugzilla.mozilla.org/show_bug.cgi?id=1119983 */
561 if (max_version >= TLS1_3_VERSION) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400562 *psigs = kDefaultTLS13SignatureAlgorithms;
Steven Valdezcb966542016-08-17 16:56:14 -0400563 return OPENSSL_ARRAY_SIZE(kDefaultTLS13SignatureAlgorithms);
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400564 }
565
Steven Valdez02563852016-06-23 13:33:05 -0400566 *psigs = kDefaultSignatureAlgorithms;
Steven Valdezcb966542016-08-17 16:56:14 -0400567 return OPENSSL_ARRAY_SIZE(kDefaultSignatureAlgorithms);
Adam Langleyfcf25832014-12-18 17:42:32 -0800568}
Adam Langley95c29f32014-06-20 12:00:00 -0700569
David Benjamin887c3002016-07-08 16:15:32 -0700570int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
Steven Valdez02563852016-06-23 13:33:05 -0400571 const uint16_t *sent_sigs;
572 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800573
Adam Langleyfcf25832014-12-18 17:42:32 -0800574 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500575 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400576 for (i = 0; i < sent_sigslen; i++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400577 if (sigalg == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800578 break;
579 }
580 }
581
David Benjamin788be4a2015-10-30 17:50:57 -0400582 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400583 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800584 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
585 return 0;
586 }
587
Adam Langleyfcf25832014-12-18 17:42:32 -0800588 return 1;
589}
590
591/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
592 * supported or doesn't appear in supported signature algorithms. Unlike
593 * ssl_cipher_get_disabled this applies to a specific session and not global
594 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500595void ssl_set_client_disabled(SSL *ssl) {
596 CERT *c = ssl->cert;
Steven Valdez02563852016-06-23 13:33:05 -0400597 const uint16_t *sigalgs;
Adam Langleyfcf25832014-12-18 17:42:32 -0800598 size_t i, sigalgslen;
599 int have_rsa = 0, have_ecdsa = 0;
600 c->mask_a = 0;
601 c->mask_k = 0;
602
Adam Langleyfcf25832014-12-18 17:42:32 -0800603 /* Now go through all signature algorithms seeing if we support any for RSA,
604 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500605 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -0400606 for (i = 0; i < sigalgslen; i++) {
607 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400608 case SSL_SIGN_RSA_PSS_SHA512:
609 case SSL_SIGN_RSA_PSS_SHA384:
610 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400611 case SSL_SIGN_RSA_PKCS1_SHA512:
612 case SSL_SIGN_RSA_PKCS1_SHA384:
613 case SSL_SIGN_RSA_PKCS1_SHA256:
614 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800615 have_rsa = 1;
616 break;
617
Steven Valdez02563852016-06-23 13:33:05 -0400618 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
619 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
620 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
621 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800622 have_ecdsa = 1;
623 break;
624 }
625 }
626
627 /* Disable auth if we don't include any appropriate signature algorithms. */
628 if (!have_rsa) {
629 c->mask_a |= SSL_aRSA;
630 }
631 if (!have_ecdsa) {
632 c->mask_a |= SSL_aECDSA;
633 }
634
635 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500636 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800637 c->mask_a |= SSL_aPSK;
638 c->mask_k |= SSL_kPSK;
639 }
640}
Adam Langley95c29f32014-06-20 12:00:00 -0700641
Adam Langley614c66a2015-06-12 15:26:58 -0700642/* tls_extension represents a TLS extension that is handled internally. The
643 * |init| function is called for each handshake, before any other functions of
644 * the extension. Then the add and parse callbacks are called as needed.
645 *
646 * The parse callbacks receive a |CBS| that contains the contents of the
647 * extension (i.e. not including the type and length bytes). If an extension is
648 * not received then the parse callbacks will be called with a NULL CBS so that
649 * they can do any processing needed to handle the absence of an extension.
650 *
651 * The add callbacks receive a |CBB| to which the extension can be appended but
652 * the function is responsible for appending the type and length bytes too.
653 *
654 * All callbacks return one for success and zero for error. If a parse function
655 * returns zero then a fatal alert with value |*out_alert| will be sent. If
656 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
657struct tls_extension {
658 uint16_t value;
659 void (*init)(SSL *ssl);
660
661 int (*add_clienthello)(SSL *ssl, CBB *out);
662 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
663
664 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
665 int (*add_serverhello)(SSL *ssl, CBB *out);
666};
667
Steven Valdez6b8509a2016-07-12 13:38:32 -0400668static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
669 if (contents != NULL) {
670 /* Servers MUST NOT send this extension. */
671 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
672 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
673 return 0;
674 }
675
676 return 1;
677}
678
679static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
680 /* This extension from the client is handled elsewhere. */
681 return 1;
682}
683
684static int dont_add_serverhello(SSL *ssl, CBB *out) {
685 return 1;
686}
Adam Langley614c66a2015-06-12 15:26:58 -0700687
688/* Server name indication (SNI).
689 *
690 * https://tools.ietf.org/html/rfc6066#section-3. */
691
692static void ext_sni_init(SSL *ssl) {
693 ssl->s3->tmp.should_ack_sni = 0;
694}
695
696static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
697 if (ssl->tlsext_hostname == NULL) {
698 return 1;
699 }
700
701 CBB contents, server_name_list, name;
702 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
703 !CBB_add_u16_length_prefixed(out, &contents) ||
704 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
705 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
706 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
707 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
708 strlen(ssl->tlsext_hostname)) ||
709 !CBB_flush(out)) {
710 return 0;
711 }
712
713 return 1;
714}
715
David Benjamin0d56f882015-12-19 17:05:56 -0500716static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
717 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700718 if (contents == NULL) {
719 return 1;
720 }
721
722 if (CBS_len(contents) != 0) {
723 return 0;
724 }
725
726 assert(ssl->tlsext_hostname != NULL);
727
Steven Valdez87eab492016-06-27 16:34:59 -0400728 if (ssl->session == NULL) {
729 assert(ssl->s3->new_session->tlsext_hostname == NULL);
730 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
731 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700732 *out_alert = SSL_AD_INTERNAL_ERROR;
733 return 0;
734 }
735 }
736
737 return 1;
738}
739
David Benjamin0d56f882015-12-19 17:05:56 -0500740static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
741 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700742 if (contents == NULL) {
743 return 1;
744 }
745
David Benjamin9b611e22016-03-03 08:48:30 -0500746 CBS server_name_list, host_name;
747 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700748 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500749 !CBS_get_u8(&server_name_list, &name_type) ||
750 /* Although the server_name extension was intended to be extensible to
751 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
752 * different name types will cause an error. Further, RFC 4366 originally
753 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
754 * adding new name types is no longer feasible.
755 *
756 * Act as if the extensibility does not exist to simplify parsing. */
757 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
758 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700759 CBS_len(contents) != 0) {
760 return 0;
761 }
762
David Benjamin9b611e22016-03-03 08:48:30 -0500763 if (name_type != TLSEXT_NAMETYPE_host_name ||
764 CBS_len(&host_name) == 0 ||
765 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
766 CBS_contains_zero_byte(&host_name)) {
767 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
768 return 0;
769 }
Adam Langley614c66a2015-06-12 15:26:58 -0700770
David Benjamin9b611e22016-03-03 08:48:30 -0500771 /* TODO(davidben): SNI should be resolved before resumption. We have the
772 * early callback as a replacement, but we should fix the current callback
773 * and avoid the need for |SSL_CTX_set_session_id_context|. */
Steven Valdez87eab492016-06-27 16:34:59 -0400774 if (ssl->session == NULL) {
775 assert(ssl->s3->new_session->tlsext_hostname == NULL);
David Benjamin9b611e22016-03-03 08:48:30 -0500776
777 /* Copy the hostname as a string. */
Steven Valdez87eab492016-06-27 16:34:59 -0400778 if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) {
David Benjamin9b611e22016-03-03 08:48:30 -0500779 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700780 return 0;
781 }
782
David Benjamin9b611e22016-03-03 08:48:30 -0500783 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700784 }
785
786 return 1;
787}
788
789static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez87eab492016-06-27 16:34:59 -0400790 if (ssl->session != NULL ||
Adam Langley614c66a2015-06-12 15:26:58 -0700791 !ssl->s3->tmp.should_ack_sni ||
Steven Valdez87eab492016-06-27 16:34:59 -0400792 ssl->s3->new_session->tlsext_hostname == NULL) {
Adam Langley614c66a2015-06-12 15:26:58 -0700793 return 1;
794 }
795
796 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
797 !CBB_add_u16(out, 0 /* length */)) {
798 return 0;
799 }
800
801 return 1;
802}
803
804
Adam Langley5021b222015-06-12 18:27:58 -0700805/* Renegotiation indication.
806 *
807 * https://tools.ietf.org/html/rfc5746 */
808
809static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
810 CBB contents, prev_finished;
811 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
812 !CBB_add_u16_length_prefixed(out, &contents) ||
813 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
814 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
815 ssl->s3->previous_client_finished_len) ||
816 !CBB_flush(out)) {
817 return 0;
818 }
819
820 return 1;
821}
822
823static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
824 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400825 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
826 return 0;
827 }
828
David Benjamin3e052de2015-11-25 20:10:31 -0500829 /* Servers may not switch between omitting the extension and supporting it.
830 * See RFC 5746, sections 3.5 and 4.2. */
831 if (ssl->s3->initial_handshake_complete &&
832 (contents != NULL) != ssl->s3->send_connection_binding) {
833 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
834 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
835 return 0;
836 }
837
Adam Langley5021b222015-06-12 18:27:58 -0700838 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500839 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700840 * RI even on initial ServerHello because the client doesn't see any
841 * renegotiation during an attack. However this would mean we could not
842 * connect to any server which doesn't support RI.
843 *
David Benjamine9cddb82015-11-23 14:36:40 -0500844 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
845 * practical terms every client sets it so it's just assumed here. */
846 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700847 }
848
849 const size_t expected_len = ssl->s3->previous_client_finished_len +
850 ssl->s3->previous_server_finished_len;
851
852 /* Check for logic errors */
853 assert(!expected_len || ssl->s3->previous_client_finished_len);
854 assert(!expected_len || ssl->s3->previous_server_finished_len);
855
856 /* Parse out the extension contents. */
857 CBS renegotiated_connection;
858 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
859 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400860 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700861 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
862 return 0;
863 }
864
865 /* Check that the extension matches. */
866 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400867 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700868 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
869 return 0;
870 }
871
872 const uint8_t *d = CBS_data(&renegotiated_connection);
873 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
874 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400875 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700876 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
877 return 0;
878 }
879 d += ssl->s3->previous_client_finished_len;
880
881 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
882 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400883 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700884 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
885 return 0;
886 }
887 ssl->s3->send_connection_binding = 1;
888
889 return 1;
890}
891
892static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
893 CBS *contents) {
894 /* Renegotiation isn't supported as a server so this function should never be
895 * called after the initial handshake. */
896 assert(!ssl->s3->initial_handshake_complete);
897
Steven Valdez143e8b32016-07-11 13:19:03 -0400898 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
899 return 1;
900 }
901
Adam Langley5021b222015-06-12 18:27:58 -0700902 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400903 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700904 }
905
906 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700907 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
908 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400909 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700910 return 0;
911 }
912
913 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500914 if (!CBS_mem_equal(&renegotiated_connection,
915 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700916 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400917 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700918 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
919 return 0;
920 }
921
922 ssl->s3->send_connection_binding = 1;
923
924 return 1;
925}
926
927static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400928 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
929 return 1;
930 }
931
Adam Langley5021b222015-06-12 18:27:58 -0700932 CBB contents, prev_finished;
933 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
934 !CBB_add_u16_length_prefixed(out, &contents) ||
935 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
936 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
937 ssl->s3->previous_client_finished_len) ||
938 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
939 ssl->s3->previous_server_finished_len) ||
940 !CBB_flush(out)) {
941 return 0;
942 }
943
944 return 1;
945}
946
Adam Langley0a056712015-07-01 15:03:33 -0700947
948/* Extended Master Secret.
949 *
David Benjamin43946d42016-02-01 08:42:19 -0500950 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700951
952static void ext_ems_init(SSL *ssl) {
953 ssl->s3->tmp.extended_master_secret = 0;
954}
955
956static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
957 if (ssl->version == SSL3_VERSION) {
958 return 1;
959 }
960
961 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
962 !CBB_add_u16(out, 0 /* length */)) {
963 return 0;
964 }
965
966 return 1;
967}
968
969static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
970 CBS *contents) {
971 if (contents == NULL) {
972 return 1;
973 }
974
Steven Valdez143e8b32016-07-11 13:19:03 -0400975 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
976 ssl->version == SSL3_VERSION) {
977 return 0;
978 }
979
980 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700981 return 0;
982 }
983
984 ssl->s3->tmp.extended_master_secret = 1;
985 return 1;
986}
987
David Benjamin0d56f882015-12-19 17:05:56 -0500988static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
989 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400990 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
991 ssl->version == SSL3_VERSION) {
992 return 1;
993 }
994
995 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700996 return 1;
997 }
998
999 if (CBS_len(contents) != 0) {
1000 return 0;
1001 }
1002
1003 ssl->s3->tmp.extended_master_secret = 1;
1004 return 1;
1005}
1006
1007static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1008 if (!ssl->s3->tmp.extended_master_secret) {
1009 return 1;
1010 }
1011
1012 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1013 !CBB_add_u16(out, 0 /* length */)) {
1014 return 0;
1015 }
1016
1017 return 1;
1018}
1019
Adam Langley9b05bc52015-07-01 15:25:33 -07001020
1021/* Session tickets.
1022 *
1023 * https://tools.ietf.org/html/rfc5077 */
1024
1025static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1026 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1027 return 1;
1028 }
1029
1030 const uint8_t *ticket_data = NULL;
1031 int ticket_len = 0;
1032
1033 /* Renegotiation does not participate in session resumption. However, still
1034 * advertise the extension to avoid potentially breaking servers which carry
1035 * over the state from the previous handshake, such as OpenSSL servers
1036 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1037 if (!ssl->s3->initial_handshake_complete &&
1038 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001039 ssl->session->tlsext_tick != NULL &&
1040 /* Don't send TLS 1.3 session tickets in the ticket extension. */
1041 ssl->method->version_from_wire(ssl->session->ssl_version) <
1042 TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001043 ticket_data = ssl->session->tlsext_tick;
1044 ticket_len = ssl->session->tlsext_ticklen;
1045 }
1046
1047 CBB ticket;
1048 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1049 !CBB_add_u16_length_prefixed(out, &ticket) ||
1050 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1051 !CBB_flush(out)) {
1052 return 0;
1053 }
1054
1055 return 1;
1056}
1057
1058static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1059 CBS *contents) {
1060 ssl->tlsext_ticket_expected = 0;
1061
1062 if (contents == NULL) {
1063 return 1;
1064 }
1065
Steven Valdez143e8b32016-07-11 13:19:03 -04001066 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1067 return 0;
1068 }
1069
Adam Langley9b05bc52015-07-01 15:25:33 -07001070 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1071 * this function should never be called, even if the server tries to send the
1072 * extension. */
1073 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1074
1075 if (CBS_len(contents) != 0) {
1076 return 0;
1077 }
1078
1079 ssl->tlsext_ticket_expected = 1;
1080 return 1;
1081}
1082
Adam Langley9b05bc52015-07-01 15:25:33 -07001083static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1084 if (!ssl->tlsext_ticket_expected) {
1085 return 1;
1086 }
1087
1088 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1089 * true. */
1090 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1091
1092 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1093 !CBB_add_u16(out, 0 /* length */)) {
1094 return 0;
1095 }
1096
1097 return 1;
1098}
1099
1100
Adam Langley2e857bd2015-07-01 16:09:19 -07001101/* Signature Algorithms.
1102 *
1103 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1104
1105static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjaminb6a0a512016-06-21 10:33:21 -04001106 if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001107 return 1;
1108 }
1109
Steven Valdez02563852016-06-23 13:33:05 -04001110 const uint16_t *sigalgs_data;
Adam Langley2e857bd2015-07-01 16:09:19 -07001111 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1112
1113 CBB contents, sigalgs;
1114 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1115 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez02563852016-06-23 13:33:05 -04001116 !CBB_add_u16_length_prefixed(&contents, &sigalgs)) {
1117 return 0;
1118 }
1119
1120 size_t i;
1121 for (i = 0; i < sigalgs_len; i++) {
1122 if (!CBB_add_u16(&sigalgs, sigalgs_data[i])) {
1123 return 0;
1124 }
1125 }
1126
1127 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001128 return 0;
1129 }
1130
1131 return 1;
1132}
1133
Adam Langley2e857bd2015-07-01 16:09:19 -07001134static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1135 CBS *contents) {
1136 OPENSSL_free(ssl->cert->peer_sigalgs);
1137 ssl->cert->peer_sigalgs = NULL;
1138 ssl->cert->peer_sigalgslen = 0;
1139
Adam Langley2e857bd2015-07-01 16:09:19 -07001140 if (contents == NULL) {
1141 return 1;
1142 }
1143
1144 CBS supported_signature_algorithms;
1145 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001146 CBS_len(contents) != 0 ||
1147 CBS_len(&supported_signature_algorithms) == 0 ||
1148 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001149 return 0;
1150 }
1151
1152 return 1;
1153}
1154
Adam Langley2e857bd2015-07-01 16:09:19 -07001155
Adam Langleybb0bd042015-07-01 16:21:03 -07001156/* OCSP Stapling.
1157 *
1158 * https://tools.ietf.org/html/rfc6066#section-8 */
1159
1160static void ext_ocsp_init(SSL *ssl) {
1161 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001162 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001163}
1164
1165static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1166 if (!ssl->ocsp_stapling_enabled) {
1167 return 1;
1168 }
1169
1170 CBB contents;
1171 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1172 !CBB_add_u16_length_prefixed(out, &contents) ||
1173 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1174 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1175 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1176 !CBB_flush(out)) {
1177 return 0;
1178 }
1179
Adam Langleyce9d85e2016-01-24 15:58:39 -08001180 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001181 return 1;
1182}
1183
1184static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001185 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001186 if (contents == NULL) {
1187 return 1;
1188 }
1189
David Benjamin942f4ed2016-07-16 19:03:49 +03001190 /* OCSP stapling is forbidden on a non-certificate cipher. */
1191 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001192 return 0;
1193 }
1194
David Benjamin942f4ed2016-07-16 19:03:49 +03001195 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1196 if (CBS_len(contents) != 0) {
1197 return 0;
1198 }
1199
1200 ssl->s3->tmp.certificate_status_expected = 1;
1201 return 1;
1202 }
1203
1204 uint8_t status_type;
1205 CBS ocsp_response;
1206 if (!CBS_get_u8(contents, &status_type) ||
1207 status_type != TLSEXT_STATUSTYPE_ocsp ||
1208 !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1209 CBS_len(&ocsp_response) == 0 ||
1210 CBS_len(contents) != 0) {
1211 return 0;
1212 }
1213
Steven Valdez87eab492016-06-27 16:34:59 -04001214 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1215 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001216 *out_alert = SSL_AD_INTERNAL_ERROR;
1217 return 0;
1218 }
1219
Adam Langleybb0bd042015-07-01 16:21:03 -07001220 return 1;
1221}
1222
1223static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1224 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001225 if (contents == NULL) {
1226 return 1;
1227 }
1228
1229 uint8_t status_type;
1230 if (!CBS_get_u8(contents, &status_type)) {
1231 return 0;
1232 }
1233
1234 /* We cannot decide whether OCSP stapling will occur yet because the correct
1235 * SSL_CTX might not have been selected. */
1236 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1237
Adam Langleybb0bd042015-07-01 16:21:03 -07001238 return 1;
1239}
1240
1241static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001242 if (!ssl->s3->tmp.ocsp_stapling_requested ||
1243 ssl->ctx->ocsp_response_length == 0 ||
1244 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001245 return 1;
1246 }
1247
David Benjamin942f4ed2016-07-16 19:03:49 +03001248 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1249 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez87eab492016-06-27 16:34:59 -04001250 if (ssl->session != NULL) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001251 return 1;
1252 }
1253
1254 ssl->s3->tmp.certificate_status_expected = 1;
1255
1256 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1257 CBB_add_u16(out, 0 /* length */);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001258 }
1259
David Benjamin942f4ed2016-07-16 19:03:49 +03001260 CBB body, ocsp_response;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001261 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
David Benjamin942f4ed2016-07-16 19:03:49 +03001262 CBB_add_u16_length_prefixed(out, &body) &&
1263 CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1264 CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1265 CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1266 ssl->ctx->ocsp_response_length) &&
1267 CBB_flush(out);
Adam Langleybb0bd042015-07-01 16:21:03 -07001268}
1269
1270
Adam Langley97dfcbf2015-07-01 18:35:20 -07001271/* Next protocol negotiation.
1272 *
1273 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1274
1275static void ext_npn_init(SSL *ssl) {
1276 ssl->s3->next_proto_neg_seen = 0;
1277}
1278
1279static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1280 if (ssl->s3->initial_handshake_complete ||
1281 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001282 (ssl->options & SSL_OP_DISABLE_NPN) ||
David Benjamince079fd2016-08-02 16:22:34 -04001283 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001284 return 1;
1285 }
1286
1287 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1288 !CBB_add_u16(out, 0 /* length */)) {
1289 return 0;
1290 }
1291
1292 return 1;
1293}
1294
1295static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1296 CBS *contents) {
1297 if (contents == NULL) {
1298 return 1;
1299 }
1300
Steven Valdez143e8b32016-07-11 13:19:03 -04001301 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1302 return 0;
1303 }
1304
Adam Langley97dfcbf2015-07-01 18:35:20 -07001305 /* If any of these are false then we should never have sent the NPN
1306 * extension in the ClientHello and thus this function should never have been
1307 * called. */
1308 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001309 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001310 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001311 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001312
David Benjamin76c2efc2015-08-31 14:24:29 -04001313 if (ssl->s3->alpn_selected != NULL) {
1314 /* NPN and ALPN may not be negotiated in the same connection. */
1315 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1316 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1317 return 0;
1318 }
1319
Adam Langley97dfcbf2015-07-01 18:35:20 -07001320 const uint8_t *const orig_contents = CBS_data(contents);
1321 const size_t orig_len = CBS_len(contents);
1322
1323 while (CBS_len(contents) != 0) {
1324 CBS proto;
1325 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1326 CBS_len(&proto) == 0) {
1327 return 0;
1328 }
1329 }
1330
1331 uint8_t *selected;
1332 uint8_t selected_len;
1333 if (ssl->ctx->next_proto_select_cb(
1334 ssl, &selected, &selected_len, orig_contents, orig_len,
1335 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1336 *out_alert = SSL_AD_INTERNAL_ERROR;
1337 return 0;
1338 }
1339
David Benjamin79978df2015-12-25 15:56:49 -05001340 OPENSSL_free(ssl->s3->next_proto_negotiated);
1341 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1342 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001343 *out_alert = SSL_AD_INTERNAL_ERROR;
1344 return 0;
1345 }
1346
David Benjamin79978df2015-12-25 15:56:49 -05001347 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001348 ssl->s3->next_proto_neg_seen = 1;
1349
1350 return 1;
1351}
1352
1353static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1354 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001355 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1356 return 1;
1357 }
1358
Adam Langley97dfcbf2015-07-01 18:35:20 -07001359 if (contents != NULL && CBS_len(contents) != 0) {
1360 return 0;
1361 }
1362
1363 if (contents == NULL ||
1364 ssl->s3->initial_handshake_complete ||
1365 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1366 * afterwards, parsing the ALPN extension will clear
1367 * |next_proto_neg_seen|. */
1368 ssl->s3->alpn_selected != NULL ||
1369 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001370 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001371 return 1;
1372 }
1373
1374 ssl->s3->next_proto_neg_seen = 1;
1375 return 1;
1376}
1377
1378static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1379 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1380 * parsed. */
1381 if (!ssl->s3->next_proto_neg_seen) {
1382 return 1;
1383 }
1384
1385 const uint8_t *npa;
1386 unsigned npa_len;
1387
1388 if (ssl->ctx->next_protos_advertised_cb(
1389 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1390 SSL_TLSEXT_ERR_OK) {
1391 ssl->s3->next_proto_neg_seen = 0;
1392 return 1;
1393 }
1394
1395 CBB contents;
1396 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1397 !CBB_add_u16_length_prefixed(out, &contents) ||
1398 !CBB_add_bytes(&contents, npa, npa_len) ||
1399 !CBB_flush(out)) {
1400 return 0;
1401 }
1402
1403 return 1;
1404}
1405
1406
Adam Langleyab8d87d2015-07-10 12:21:39 -07001407/* Signed certificate timestamps.
1408 *
1409 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1410
1411static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1412 if (!ssl->signed_cert_timestamps_enabled) {
1413 return 1;
1414 }
1415
1416 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1417 !CBB_add_u16(out, 0 /* length */)) {
1418 return 0;
1419 }
1420
1421 return 1;
1422}
1423
1424static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1425 CBS *contents) {
1426 if (contents == NULL) {
1427 return 1;
1428 }
1429
1430 /* If this is false then we should never have sent the SCT extension in the
1431 * ClientHello and thus this function should never have been called. */
1432 assert(ssl->signed_cert_timestamps_enabled);
1433
1434 if (CBS_len(contents) == 0) {
1435 *out_alert = SSL_AD_DECODE_ERROR;
1436 return 0;
1437 }
1438
1439 /* Session resumption uses the original session information. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001440 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001441 !CBS_stow(
1442 contents,
1443 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1444 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001445 *out_alert = SSL_AD_INTERNAL_ERROR;
1446 return 0;
1447 }
1448
1449 return 1;
1450}
1451
1452static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1453 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001454 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001455}
1456
1457static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001458 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001459 if (ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001460 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001461 return 1;
1462 }
1463
1464 CBB contents;
1465 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1466 CBB_add_u16_length_prefixed(out, &contents) &&
1467 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1468 ssl->ctx->signed_cert_timestamp_list_length) &&
1469 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001470}
1471
1472
Adam Langleyf18e4532015-07-10 13:39:53 -07001473/* Application-level Protocol Negotiation.
1474 *
1475 * https://tools.ietf.org/html/rfc7301 */
1476
1477static void ext_alpn_init(SSL *ssl) {
1478 OPENSSL_free(ssl->s3->alpn_selected);
1479 ssl->s3->alpn_selected = NULL;
1480}
1481
1482static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1483 if (ssl->alpn_client_proto_list == NULL ||
1484 ssl->s3->initial_handshake_complete) {
1485 return 1;
1486 }
1487
1488 CBB contents, proto_list;
1489 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1490 !CBB_add_u16_length_prefixed(out, &contents) ||
1491 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1492 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1493 ssl->alpn_client_proto_list_len) ||
1494 !CBB_flush(out)) {
1495 return 0;
1496 }
1497
1498 return 1;
1499}
1500
1501static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1502 CBS *contents) {
1503 if (contents == NULL) {
1504 return 1;
1505 }
1506
1507 assert(!ssl->s3->initial_handshake_complete);
1508 assert(ssl->alpn_client_proto_list != NULL);
1509
David Benjamin76c2efc2015-08-31 14:24:29 -04001510 if (ssl->s3->next_proto_neg_seen) {
1511 /* NPN and ALPN may not be negotiated in the same connection. */
1512 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1513 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1514 return 0;
1515 }
1516
Adam Langleyf18e4532015-07-10 13:39:53 -07001517 /* The extension data consists of a ProtocolNameList which must have
1518 * exactly one ProtocolName. Each of these is length-prefixed. */
1519 CBS protocol_name_list, protocol_name;
1520 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1521 CBS_len(contents) != 0 ||
1522 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1523 /* Empty protocol names are forbidden. */
1524 CBS_len(&protocol_name) == 0 ||
1525 CBS_len(&protocol_name_list) != 0) {
1526 return 0;
1527 }
1528
David Benjamin3e517572016-08-11 11:52:23 -04001529 /* Check that the protcol name is one of the ones we advertised. */
1530 int protocol_ok = 0;
1531 CBS client_protocol_name_list, client_protocol_name;
1532 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1533 ssl->alpn_client_proto_list_len);
1534 while (CBS_len(&client_protocol_name_list) > 0) {
1535 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1536 &client_protocol_name)) {
1537 *out_alert = SSL_AD_INTERNAL_ERROR;
1538 return 0;
1539 }
1540
1541 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1542 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1543 CBS_len(&protocol_name)) == 0) {
1544 protocol_ok = 1;
1545 break;
1546 }
1547 }
1548
1549 if (!protocol_ok) {
1550 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1551 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1552 return 0;
1553 }
1554
Adam Langleyf18e4532015-07-10 13:39:53 -07001555 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1556 &ssl->s3->alpn_selected_len)) {
1557 *out_alert = SSL_AD_INTERNAL_ERROR;
1558 return 0;
1559 }
1560
1561 return 1;
1562}
1563
1564static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1565 CBS *contents) {
1566 if (contents == NULL) {
1567 return 1;
1568 }
1569
1570 if (ssl->ctx->alpn_select_cb == NULL ||
1571 ssl->s3->initial_handshake_complete) {
1572 return 1;
1573 }
1574
1575 /* ALPN takes precedence over NPN. */
1576 ssl->s3->next_proto_neg_seen = 0;
1577
1578 CBS protocol_name_list;
1579 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1580 CBS_len(contents) != 0 ||
1581 CBS_len(&protocol_name_list) < 2) {
1582 return 0;
1583 }
1584
1585 /* Validate the protocol list. */
1586 CBS protocol_name_list_copy = protocol_name_list;
1587 while (CBS_len(&protocol_name_list_copy) > 0) {
1588 CBS protocol_name;
1589
1590 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1591 /* Empty protocol names are forbidden. */
1592 CBS_len(&protocol_name) == 0) {
1593 return 0;
1594 }
1595 }
1596
1597 const uint8_t *selected;
1598 uint8_t selected_len;
1599 if (ssl->ctx->alpn_select_cb(
1600 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1601 CBS_len(&protocol_name_list),
1602 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1603 OPENSSL_free(ssl->s3->alpn_selected);
1604 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1605 if (ssl->s3->alpn_selected == NULL) {
1606 *out_alert = SSL_AD_INTERNAL_ERROR;
1607 return 0;
1608 }
1609 ssl->s3->alpn_selected_len = selected_len;
1610 }
1611
1612 return 1;
1613}
1614
1615static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1616 if (ssl->s3->alpn_selected == NULL) {
1617 return 1;
1618 }
1619
1620 CBB contents, proto_list, proto;
1621 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1622 !CBB_add_u16_length_prefixed(out, &contents) ||
1623 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1624 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001625 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1626 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001627 !CBB_flush(out)) {
1628 return 0;
1629 }
1630
1631 return 1;
1632}
1633
1634
Adam Langley49c7af12015-07-10 14:33:46 -07001635/* Channel ID.
1636 *
1637 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1638
1639static void ext_channel_id_init(SSL *ssl) {
1640 ssl->s3->tlsext_channel_id_valid = 0;
1641}
1642
1643static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1644 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001645 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001646 return 1;
1647 }
1648
1649 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1650 !CBB_add_u16(out, 0 /* length */)) {
1651 return 0;
1652 }
1653
1654 return 1;
1655}
1656
1657static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1658 CBS *contents) {
1659 if (contents == NULL) {
1660 return 1;
1661 }
1662
Steven Valdez143e8b32016-07-11 13:19:03 -04001663 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1664 return 0;
1665 }
1666
David Benjamince079fd2016-08-02 16:22:34 -04001667 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001668 assert(ssl->tlsext_channel_id_enabled);
1669
1670 if (CBS_len(contents) != 0) {
1671 return 0;
1672 }
1673
1674 ssl->s3->tlsext_channel_id_valid = 1;
1675 return 1;
1676}
1677
1678static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1679 CBS *contents) {
1680 if (contents == NULL ||
1681 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001682 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001683 return 1;
1684 }
1685
1686 if (CBS_len(contents) != 0) {
1687 return 0;
1688 }
1689
1690 ssl->s3->tlsext_channel_id_valid = 1;
1691 return 1;
1692}
1693
1694static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001695 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1696 return 1;
1697 }
1698
Adam Langley49c7af12015-07-10 14:33:46 -07001699 if (!ssl->s3->tlsext_channel_id_valid) {
1700 return 1;
1701 }
1702
1703 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1704 !CBB_add_u16(out, 0 /* length */)) {
1705 return 0;
1706 }
1707
1708 return 1;
1709}
1710
Adam Langley391250d2015-07-15 19:06:07 -07001711
1712/* Secure Real-time Transport Protocol (SRTP) extension.
1713 *
1714 * https://tools.ietf.org/html/rfc5764 */
1715
Adam Langley391250d2015-07-15 19:06:07 -07001716
1717static void ext_srtp_init(SSL *ssl) {
1718 ssl->srtp_profile = NULL;
1719}
1720
1721static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1722 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1723 if (profiles == NULL) {
1724 return 1;
1725 }
1726 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1727 if (num_profiles == 0) {
1728 return 1;
1729 }
1730
1731 CBB contents, profile_ids;
1732 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1733 !CBB_add_u16_length_prefixed(out, &contents) ||
1734 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1735 return 0;
1736 }
1737
1738 size_t i;
1739 for (i = 0; i < num_profiles; i++) {
1740 if (!CBB_add_u16(&profile_ids,
1741 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1742 return 0;
1743 }
1744 }
1745
1746 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1747 !CBB_flush(out)) {
1748 return 0;
1749 }
1750
1751 return 1;
1752}
1753
1754static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1755 CBS *contents) {
1756 if (contents == NULL) {
1757 return 1;
1758 }
1759
1760 /* The extension consists of a u16-prefixed profile ID list containing a
1761 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1762 *
1763 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1764 CBS profile_ids, srtp_mki;
1765 uint16_t profile_id;
1766 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1767 !CBS_get_u16(&profile_ids, &profile_id) ||
1768 CBS_len(&profile_ids) != 0 ||
1769 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1770 CBS_len(contents) != 0) {
1771 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1772 return 0;
1773 }
1774
1775 if (CBS_len(&srtp_mki) != 0) {
1776 /* Must be no MKI, since we never offer one. */
1777 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1778 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1779 return 0;
1780 }
1781
1782 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1783
1784 /* Check to see if the server gave us something we support (and presumably
1785 * offered). */
1786 size_t i;
1787 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1788 const SRTP_PROTECTION_PROFILE *profile =
1789 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1790
1791 if (profile->id == profile_id) {
1792 ssl->srtp_profile = profile;
1793 return 1;
1794 }
1795 }
1796
1797 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1798 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1799 return 0;
1800}
1801
1802static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1803 CBS *contents) {
1804 if (contents == NULL) {
1805 return 1;
1806 }
1807
1808 CBS profile_ids, srtp_mki;
1809 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1810 CBS_len(&profile_ids) < 2 ||
1811 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1812 CBS_len(contents) != 0) {
1813 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1814 return 0;
1815 }
1816 /* Discard the MKI value for now. */
1817
1818 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1819 SSL_get_srtp_profiles(ssl);
1820
1821 /* Pick the server's most preferred profile. */
1822 size_t i;
1823 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1824 const SRTP_PROTECTION_PROFILE *server_profile =
1825 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1826
1827 CBS profile_ids_tmp;
1828 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1829
1830 while (CBS_len(&profile_ids_tmp) > 0) {
1831 uint16_t profile_id;
1832 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1833 return 0;
1834 }
1835
1836 if (server_profile->id == profile_id) {
1837 ssl->srtp_profile = server_profile;
1838 return 1;
1839 }
1840 }
1841 }
1842
1843 return 1;
1844}
1845
1846static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1847 if (ssl->srtp_profile == NULL) {
1848 return 1;
1849 }
1850
1851 CBB contents, profile_ids;
1852 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1853 !CBB_add_u16_length_prefixed(out, &contents) ||
1854 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1855 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1856 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1857 !CBB_flush(out)) {
1858 return 0;
1859 }
1860
1861 return 1;
1862}
1863
Adam Langleybdd5d662015-07-20 16:19:08 -07001864
1865/* EC point formats.
1866 *
1867 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1868
1869static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
David Benjamince079fd2016-08-02 16:22:34 -04001870 if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001871 return 0;
1872 }
1873
1874 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1875
1876 size_t i;
1877 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1878 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1879
1880 const uint32_t alg_k = cipher->algorithm_mkey;
1881 const uint32_t alg_a = cipher->algorithm_auth;
1882 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1883 return 1;
1884 }
1885 }
1886
1887 return 0;
1888}
1889
Adam Langleybdd5d662015-07-20 16:19:08 -07001890static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001891 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001892 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1893 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001894 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1895 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001896 !CBB_flush(out)) {
1897 return 0;
1898 }
1899
1900 return 1;
1901}
1902
1903static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1904 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1905 return 1;
1906 }
1907
1908 return ext_ec_point_add_extension(ssl, out);
1909}
1910
1911static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1912 CBS *contents) {
1913 if (contents == NULL) {
1914 return 1;
1915 }
1916
Steven Valdez143e8b32016-07-11 13:19:03 -04001917 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1918 return 0;
1919 }
1920
Adam Langleybdd5d662015-07-20 16:19:08 -07001921 CBS ec_point_format_list;
1922 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1923 CBS_len(contents) != 0) {
1924 return 0;
1925 }
1926
David Benjaminfc059942015-07-30 23:01:59 -04001927 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1928 * point format. */
1929 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1930 CBS_len(&ec_point_format_list)) == NULL) {
1931 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001932 return 0;
1933 }
1934
1935 return 1;
1936}
1937
1938static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1939 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001940 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1941 return 1;
1942 }
1943
Adam Langleybdd5d662015-07-20 16:19:08 -07001944 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1945}
1946
1947static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001948 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1949 return 1;
1950 }
1951
Adam Langleybdd5d662015-07-20 16:19:08 -07001952 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1953 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001954 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001955
1956 if (!using_ecc) {
1957 return 1;
1958 }
1959
1960 return ext_ec_point_add_extension(ssl, out);
1961}
1962
Adam Langley273d49c2015-07-20 16:38:52 -07001963
Steven Valdez143e8b32016-07-11 13:19:03 -04001964/* Draft Version Extension */
1965
1966static int ext_draft_version_add_clienthello(SSL *ssl, CBB *out) {
1967 uint16_t min_version, max_version;
1968 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
Steven Valdez3a287552016-07-19 05:10:50 -04001969 max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001970 return 1;
1971 }
1972
1973 CBB contents;
1974 if (!CBB_add_u16(out, TLSEXT_TYPE_draft_version) ||
1975 !CBB_add_u16_length_prefixed(out, &contents) ||
1976 !CBB_add_u16(&contents, TLS1_3_DRAFT_VERSION)) {
1977 return 0;
1978 }
1979
1980 return CBB_flush(out);
1981}
1982
1983
Steven Valdez4aa154e2016-07-29 14:32:55 -04001984/* Pre Shared Key
1985 *
1986 * https://tools.ietf.org/html/draft-ietf-tls-tls13-14 */
1987
1988static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
1989 uint16_t min_version, max_version;
1990 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1991 return 0;
1992 }
1993
1994 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1995 ssl->method->version_from_wire(ssl->session->ssl_version) <
1996 TLS1_3_VERSION) {
1997 return 1;
1998 }
1999
2000 CBB contents, identities, identity;
2001 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2002 !CBB_add_u16_length_prefixed(out, &contents) ||
2003 !CBB_add_u16_length_prefixed(&contents, &identities) ||
2004 !CBB_add_u16_length_prefixed(&identities, &identity) ||
2005 !CBB_add_bytes(&identity, ssl->session->tlsext_tick,
2006 ssl->session->tlsext_ticklen)) {
2007 return 0;
2008 }
2009
2010 return CBB_flush(out);
2011}
2012
2013int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2014 CBS *contents) {
2015 uint16_t psk_id;
2016 if (!CBS_get_u16(contents, &psk_id) ||
2017 CBS_len(contents) != 0) {
2018 *out_alert = SSL_AD_DECODE_ERROR;
2019 return 0;
2020 }
2021
2022 if (psk_id != 0) {
2023 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2024 return 0;
2025 }
2026
2027 return 1;
2028}
2029
2030int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
2031 SSL_SESSION **out_session,
2032 uint8_t *out_alert,
2033 CBS *contents) {
2034 CBS identities, identity;
2035 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2036 !CBS_get_u16_length_prefixed(&identities, &identity) ||
2037 CBS_len(contents) != 0) {
2038 *out_alert = SSL_AD_DECODE_ERROR;
2039 return 0;
2040 }
2041
2042 /* TLS 1.3 session tickets are renewed separately as part of the
2043 * NewSessionTicket. */
2044 int renew;
2045 return tls_process_ticket(ssl, out_session, &renew, CBS_data(&identity),
2046 CBS_len(&identity), NULL, 0);
2047}
2048
2049int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
2050 if (!ssl->s3->session_reused) {
2051 return 1;
2052 }
2053
2054 CBB contents;
2055 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2056 !CBB_add_u16_length_prefixed(out, &contents) ||
2057 /* We only consider the first identity for resumption */
2058 !CBB_add_u16(&contents, 0) ||
2059 !CBB_flush(out)) {
2060 return 0;
2061 }
2062
2063 return 1;
2064}
2065
2066
Steven Valdez143e8b32016-07-11 13:19:03 -04002067/* Key Share
2068 *
2069 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
2070
2071static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
2072 uint16_t min_version, max_version;
2073 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2074 return 0;
2075 }
2076
2077 if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
2078 return 1;
2079 }
2080
2081 CBB contents, kse_bytes;
2082 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2083 !CBB_add_u16_length_prefixed(out, &contents) ||
2084 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2085 return 0;
2086 }
2087
2088 const uint16_t *groups;
2089 size_t groups_len;
Steven Valdez5440fe02016-07-18 12:40:30 -04002090 if (ssl->s3->hs->retry_group) {
2091 /* Append the new key share to the old list. */
2092 if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
2093 ssl->s3->hs->key_share_bytes_len)) {
2094 return 0;
2095 }
2096 OPENSSL_free(ssl->s3->hs->key_share_bytes);
2097 ssl->s3->hs->key_share_bytes = NULL;
2098
2099 groups = &ssl->s3->hs->retry_group;
2100 groups_len = 1;
2101 } else {
2102 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
2103 /* Only send the top two preferred key shares. */
2104 if (groups_len > 2) {
2105 groups_len = 2;
2106 }
2107 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002108
2109 ssl->s3->hs->groups = OPENSSL_malloc(groups_len * sizeof(SSL_ECDH_CTX));
2110 if (ssl->s3->hs->groups == NULL) {
2111 return 0;
2112 }
2113 memset(ssl->s3->hs->groups, 0, groups_len * sizeof(SSL_ECDH_CTX));
2114 ssl->s3->hs->groups_len = groups_len;
2115
2116 for (size_t i = 0; i < groups_len; i++) {
2117 if (!CBB_add_u16(&kse_bytes, groups[i])) {
2118 return 0;
2119 }
2120
2121 CBB key_exchange;
2122 if (!CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2123 !SSL_ECDH_CTX_init(&ssl->s3->hs->groups[i], groups[i]) ||
2124 !SSL_ECDH_CTX_offer(&ssl->s3->hs->groups[i], &key_exchange) ||
2125 !CBB_flush(&kse_bytes)) {
2126 return 0;
2127 }
2128 }
2129
Steven Valdez5440fe02016-07-18 12:40:30 -04002130 if (!ssl->s3->hs->retry_group) {
2131 /* Save the contents of the extension to repeat it in the second
2132 * ClientHello. */
2133 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2134 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2135 CBB_len(&kse_bytes));
2136 if (ssl->s3->hs->key_share_bytes == NULL) {
2137 return 0;
2138 }
2139 }
2140
Steven Valdez143e8b32016-07-11 13:19:03 -04002141 return CBB_flush(out);
2142}
2143
Steven Valdez7259f2f2016-08-02 16:55:05 -04002144int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2145 size_t *out_secret_len,
2146 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002147 CBS peer_key;
2148 uint16_t group;
2149 if (!CBS_get_u16(contents, &group) ||
David Benjamina70de142016-08-02 16:52:57 -04002150 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2151 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002152 *out_alert = SSL_AD_DECODE_ERROR;
2153 return 0;
2154 }
2155
2156 SSL_ECDH_CTX *group_ctx = NULL;
2157 for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) {
2158 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->groups[i]) == group) {
2159 group_ctx = &ssl->s3->hs->groups[i];
2160 break;
2161 }
2162 }
2163
2164 if (group_ctx == NULL) {
2165 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2166 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2167 return 0;
2168 }
2169
2170 if (!SSL_ECDH_CTX_finish(group_ctx, out_secret, out_secret_len, out_alert,
2171 CBS_data(&peer_key), CBS_len(&peer_key))) {
2172 *out_alert = SSL_AD_INTERNAL_ERROR;
2173 return 0;
2174 }
2175
Steven Valdez5440fe02016-07-18 12:40:30 -04002176 ssl_handshake_clear_groups(ssl->s3->hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04002177 return 1;
2178}
2179
Steven Valdez7259f2f2016-08-02 16:55:05 -04002180int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2181 uint8_t **out_secret,
2182 size_t *out_secret_len,
2183 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002184 uint16_t group_id;
2185 CBS key_shares;
2186 if (!tls1_get_shared_group(ssl, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002187 !CBS_get_u16_length_prefixed(contents, &key_shares) ||
2188 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002189 return 0;
2190 }
2191
Steven Valdez5440fe02016-07-18 12:40:30 -04002192 *out_found = 0;
Steven Valdez143e8b32016-07-11 13:19:03 -04002193 while (CBS_len(&key_shares) > 0) {
2194 uint16_t id;
2195 CBS peer_key;
2196 if (!CBS_get_u16(&key_shares, &id) ||
2197 !CBS_get_u16_length_prefixed(&key_shares, &peer_key)) {
2198 return 0;
2199 }
2200
Steven Valdez5440fe02016-07-18 12:40:30 -04002201 if (id != group_id || *out_found) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002202 continue;
2203 }
2204
2205 SSL_ECDH_CTX group;
2206 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2207 CBB public_key;
2208 if (!CBB_init(&public_key, 0) ||
2209 !SSL_ECDH_CTX_init(&group, group_id) ||
2210 !SSL_ECDH_CTX_accept(&group, &public_key, out_secret, out_secret_len,
2211 out_alert, CBS_data(&peer_key),
2212 CBS_len(&peer_key)) ||
2213 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2214 &ssl->s3->hs->public_key_len)) {
2215 SSL_ECDH_CTX_cleanup(&group);
2216 CBB_cleanup(&public_key);
2217 return 0;
2218 }
2219 SSL_ECDH_CTX_cleanup(&group);
2220
Steven Valdez5440fe02016-07-18 12:40:30 -04002221 *out_found = 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002222 }
2223
Steven Valdez5440fe02016-07-18 12:40:30 -04002224 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002225}
2226
Steven Valdez7259f2f2016-08-02 16:55:05 -04002227int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002228 if (ssl->s3->tmp.new_cipher->algorithm_mkey != SSL_kECDHE) {
2229 return 1;
2230 }
2231
2232 uint16_t group_id;
2233 CBB kse_bytes, public_key;
2234 if (!tls1_get_shared_group(ssl, &group_id) ||
2235 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2236 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2237 !CBB_add_u16(&kse_bytes, group_id) ||
2238 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2239 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2240 ssl->s3->hs->public_key_len) ||
2241 !CBB_flush(out)) {
2242 return 0;
2243 }
2244
2245 return 1;
2246}
2247
2248
Steven Valdezce902a92016-05-17 11:47:53 -04002249/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002250 *
Steven Valdezce902a92016-05-17 11:47:53 -04002251 * https://tools.ietf.org/html/rfc4492#section-5.1.2
2252 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07002253
Steven Valdezce902a92016-05-17 11:47:53 -04002254static void ext_supported_groups_init(SSL *ssl) {
2255 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2256 ssl->s3->tmp.peer_supported_group_list = NULL;
2257 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07002258}
2259
Steven Valdezce902a92016-05-17 11:47:53 -04002260static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002261 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2262 return 1;
2263 }
2264
Steven Valdezce902a92016-05-17 11:47:53 -04002265 CBB contents, groups_bytes;
2266 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002267 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002268 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002269 return 0;
2270 }
2271
Steven Valdezce902a92016-05-17 11:47:53 -04002272 const uint16_t *groups;
2273 size_t groups_len;
2274 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002275
2276 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002277 for (i = 0; i < groups_len; i++) {
2278 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002279 return 0;
2280 }
2281 }
2282
2283 return CBB_flush(out);
2284}
2285
Steven Valdezce902a92016-05-17 11:47:53 -04002286static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2287 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002288 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2289 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002290 return 1;
2291}
2292
Steven Valdezce902a92016-05-17 11:47:53 -04002293static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2294 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002295 if (contents == NULL) {
2296 return 1;
2297 }
2298
Steven Valdezce902a92016-05-17 11:47:53 -04002299 CBS supported_group_list;
2300 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2301 CBS_len(&supported_group_list) == 0 ||
2302 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002303 CBS_len(contents) != 0) {
2304 return 0;
2305 }
2306
Steven Valdezce902a92016-05-17 11:47:53 -04002307 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
2308 CBS_len(&supported_group_list));
2309 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002310 *out_alert = SSL_AD_INTERNAL_ERROR;
2311 return 0;
2312 }
2313
Steven Valdezce902a92016-05-17 11:47:53 -04002314 const size_t num_groups = CBS_len(&supported_group_list) / 2;
Adam Langley273d49c2015-07-20 16:38:52 -07002315 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002316 for (i = 0; i < num_groups; i++) {
2317 if (!CBS_get_u16(&supported_group_list,
2318 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002319 goto err;
2320 }
2321 }
2322
Steven Valdezce902a92016-05-17 11:47:53 -04002323 assert(CBS_len(&supported_group_list) == 0);
2324 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002325
2326 return 1;
2327
2328err:
Steven Valdezce902a92016-05-17 11:47:53 -04002329 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2330 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002331 *out_alert = SSL_AD_INTERNAL_ERROR;
2332 return 0;
2333}
2334
Steven Valdezce902a92016-05-17 11:47:53 -04002335static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002336 /* Servers don't echo this extension. */
2337 return 1;
2338}
2339
2340
Adam Langley614c66a2015-06-12 15:26:58 -07002341/* kExtensions contains all the supported extensions. */
2342static const struct tls_extension kExtensions[] = {
2343 {
Adam Langley5021b222015-06-12 18:27:58 -07002344 TLSEXT_TYPE_renegotiate,
2345 NULL,
2346 ext_ri_add_clienthello,
2347 ext_ri_parse_serverhello,
2348 ext_ri_parse_clienthello,
2349 ext_ri_add_serverhello,
2350 },
2351 {
Adam Langley614c66a2015-06-12 15:26:58 -07002352 TLSEXT_TYPE_server_name,
2353 ext_sni_init,
2354 ext_sni_add_clienthello,
2355 ext_sni_parse_serverhello,
2356 ext_sni_parse_clienthello,
2357 ext_sni_add_serverhello,
2358 },
Adam Langley0a056712015-07-01 15:03:33 -07002359 {
2360 TLSEXT_TYPE_extended_master_secret,
2361 ext_ems_init,
2362 ext_ems_add_clienthello,
2363 ext_ems_parse_serverhello,
2364 ext_ems_parse_clienthello,
2365 ext_ems_add_serverhello,
2366 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002367 {
2368 TLSEXT_TYPE_session_ticket,
2369 NULL,
2370 ext_ticket_add_clienthello,
2371 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002372 /* Ticket extension client parsing is handled in ssl_session.c */
2373 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002374 ext_ticket_add_serverhello,
2375 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002376 {
2377 TLSEXT_TYPE_signature_algorithms,
2378 NULL,
2379 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002380 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002381 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002382 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002383 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002384 {
2385 TLSEXT_TYPE_status_request,
2386 ext_ocsp_init,
2387 ext_ocsp_add_clienthello,
2388 ext_ocsp_parse_serverhello,
2389 ext_ocsp_parse_clienthello,
2390 ext_ocsp_add_serverhello,
2391 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002392 {
2393 TLSEXT_TYPE_next_proto_neg,
2394 ext_npn_init,
2395 ext_npn_add_clienthello,
2396 ext_npn_parse_serverhello,
2397 ext_npn_parse_clienthello,
2398 ext_npn_add_serverhello,
2399 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002400 {
2401 TLSEXT_TYPE_certificate_timestamp,
2402 NULL,
2403 ext_sct_add_clienthello,
2404 ext_sct_parse_serverhello,
2405 ext_sct_parse_clienthello,
2406 ext_sct_add_serverhello,
2407 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002408 {
2409 TLSEXT_TYPE_application_layer_protocol_negotiation,
2410 ext_alpn_init,
2411 ext_alpn_add_clienthello,
2412 ext_alpn_parse_serverhello,
2413 ext_alpn_parse_clienthello,
2414 ext_alpn_add_serverhello,
2415 },
Adam Langley49c7af12015-07-10 14:33:46 -07002416 {
2417 TLSEXT_TYPE_channel_id,
2418 ext_channel_id_init,
2419 ext_channel_id_add_clienthello,
2420 ext_channel_id_parse_serverhello,
2421 ext_channel_id_parse_clienthello,
2422 ext_channel_id_add_serverhello,
2423 },
Adam Langley391250d2015-07-15 19:06:07 -07002424 {
2425 TLSEXT_TYPE_srtp,
2426 ext_srtp_init,
2427 ext_srtp_add_clienthello,
2428 ext_srtp_parse_serverhello,
2429 ext_srtp_parse_clienthello,
2430 ext_srtp_add_serverhello,
2431 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002432 {
2433 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002434 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002435 ext_ec_point_add_clienthello,
2436 ext_ec_point_parse_serverhello,
2437 ext_ec_point_parse_clienthello,
2438 ext_ec_point_add_serverhello,
2439 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002440 {
2441 TLSEXT_TYPE_draft_version,
2442 NULL,
2443 ext_draft_version_add_clienthello,
2444 forbid_parse_serverhello,
2445 ignore_parse_clienthello,
2446 dont_add_serverhello,
2447 },
2448 {
2449 TLSEXT_TYPE_key_share,
2450 NULL,
2451 ext_key_share_add_clienthello,
2452 forbid_parse_serverhello,
2453 ignore_parse_clienthello,
2454 dont_add_serverhello,
2455 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002456 {
2457 TLSEXT_TYPE_pre_shared_key,
2458 NULL,
2459 ext_pre_shared_key_add_clienthello,
2460 forbid_parse_serverhello,
2461 ignore_parse_clienthello,
2462 dont_add_serverhello,
2463 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002464 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2465 * intolerant to the last extension being zero-length. See
2466 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002467 {
Steven Valdezce902a92016-05-17 11:47:53 -04002468 TLSEXT_TYPE_supported_groups,
2469 ext_supported_groups_init,
2470 ext_supported_groups_add_clienthello,
2471 ext_supported_groups_parse_serverhello,
2472 ext_supported_groups_parse_clienthello,
2473 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002474 },
Adam Langley614c66a2015-06-12 15:26:58 -07002475};
2476
2477#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2478
Adam Langley4cfa96b2015-07-01 11:56:55 -07002479OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2480 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002481 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002482OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2483 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2484 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002485 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002486
Adam Langley614c66a2015-06-12 15:26:58 -07002487static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2488 uint16_t value) {
2489 unsigned i;
2490 for (i = 0; i < kNumExtensions; i++) {
2491 if (kExtensions[i].value == value) {
2492 *out_index = i;
2493 return &kExtensions[i];
2494 }
2495 }
2496
2497 return NULL;
2498}
2499
Adam Langley09505632015-07-30 18:10:13 -07002500int SSL_extension_supported(unsigned extension_value) {
2501 uint32_t index;
2502 return extension_value == TLSEXT_TYPE_padding ||
2503 tls_extension_find(&index, extension_value) != NULL;
2504}
2505
David Benjamine8d53502015-10-10 14:13:23 -04002506int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002507 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002508 if (ssl->client_version == SSL3_VERSION &&
2509 !ssl->s3->send_connection_binding) {
2510 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002511 }
Adam Langley95c29f32014-06-20 12:00:00 -07002512
David Benjamine8d53502015-10-10 14:13:23 -04002513 CBB extensions;
2514 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002515 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002516 }
Adam Langley95c29f32014-06-20 12:00:00 -07002517
David Benjamine8d53502015-10-10 14:13:23 -04002518 ssl->s3->tmp.extensions.sent = 0;
2519 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002520
Adam Langley614c66a2015-06-12 15:26:58 -07002521 size_t i;
2522 for (i = 0; i < kNumExtensions; i++) {
2523 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002524 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002525 }
2526 }
Adam Langley95c29f32014-06-20 12:00:00 -07002527
Adam Langley614c66a2015-06-12 15:26:58 -07002528 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002529 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002530 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002531 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2532 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2533 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002534 }
Adam Langley95c29f32014-06-20 12:00:00 -07002535
Adam Langley33ad2b52015-07-20 17:43:53 -07002536 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002537 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002538 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002539 }
Adam Langley75712922014-10-10 16:23:43 -07002540
David Benjamine8d53502015-10-10 14:13:23 -04002541 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002542 goto err;
2543 }
2544
David Benjamince079fd2016-08-02 16:22:34 -04002545 if (!SSL_is_dtls(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002546 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002547 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002548 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002549 *
2550 * NB: because this code works out the length of all existing extensions
2551 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002552 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002553 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002554 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002555 * Server 7.0 is intolerant to the last extension being zero-length. See
2556 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002557 if (padding_len >= 4 + 1) {
2558 padding_len -= 4;
2559 } else {
2560 padding_len = 1;
2561 }
Adam Langley95c29f32014-06-20 12:00:00 -07002562
Adam Langley33ad2b52015-07-20 17:43:53 -07002563 uint8_t *padding_bytes;
2564 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2565 !CBB_add_u16(&extensions, padding_len) ||
2566 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2567 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002568 }
Adam Langley75712922014-10-10 16:23:43 -07002569
Adam Langley33ad2b52015-07-20 17:43:53 -07002570 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002571 }
2572 }
Adam Langley75712922014-10-10 16:23:43 -07002573
David Benjamina01deee2015-12-08 18:56:31 -05002574 /* Discard empty extensions blocks. */
2575 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002576 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002577 }
2578
David Benjamine8d53502015-10-10 14:13:23 -04002579 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002580
2581err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002582 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002583 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002584}
Adam Langley95c29f32014-06-20 12:00:00 -07002585
David Benjamin56380462015-10-10 14:59:09 -04002586int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002587 CBB extensions;
2588 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002589 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002590 }
2591
2592 unsigned i;
2593 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002594 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002595 /* Don't send extensions that were not received. */
2596 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002597 }
Adam Langley95c29f32014-06-20 12:00:00 -07002598
David Benjamin56380462015-10-10 14:59:09 -04002599 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002600 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2601 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2602 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002603 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002604 }
Adam Langley95c29f32014-06-20 12:00:00 -07002605
David Benjamin56380462015-10-10 14:59:09 -04002606 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002607 goto err;
2608 }
2609
Steven Valdez143e8b32016-07-11 13:19:03 -04002610 /* Discard empty extensions blocks before TLS 1.3. */
2611 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2612 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002613 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002614 }
2615
David Benjamin56380462015-10-10 14:59:09 -04002616 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002617
2618err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002619 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002620 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002621}
Adam Langley95c29f32014-06-20 12:00:00 -07002622
David Benjamine14ff062016-08-09 16:21:24 -04002623static int ssl_scan_clienthello_tlsext(
2624 SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
2625 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002626 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002627 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002628 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002629 }
2630 }
2631
David Benjamin0d56f882015-12-19 17:05:56 -05002632 ssl->s3->tmp.extensions.received = 0;
2633 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002634
David Benjamine14ff062016-08-09 16:21:24 -04002635 CBS extensions;
2636 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2637 while (CBS_len(&extensions) != 0) {
2638 uint16_t type;
2639 CBS extension;
2640
2641 /* Decode the next extension. */
2642 if (!CBS_get_u16(&extensions, &type) ||
2643 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002644 *out_alert = SSL_AD_DECODE_ERROR;
2645 return 0;
2646 }
Adam Langley95c29f32014-06-20 12:00:00 -07002647
David Benjamine14ff062016-08-09 16:21:24 -04002648 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2649 * ambiguous. Ignore all but the renegotiation_info extension. */
2650 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2651 continue;
2652 }
Adam Langley95c29f32014-06-20 12:00:00 -07002653
David Benjamine14ff062016-08-09 16:21:24 -04002654 unsigned ext_index;
2655 const struct tls_extension *const ext =
2656 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002657
David Benjamine14ff062016-08-09 16:21:24 -04002658 if (ext == NULL) {
2659 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002660 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002661 return 0;
2662 }
David Benjamine14ff062016-08-09 16:21:24 -04002663 continue;
2664 }
2665
2666 ssl->s3->tmp.extensions.received |= (1u << ext_index);
2667 uint8_t alert = SSL_AD_DECODE_ERROR;
2668 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2669 *out_alert = alert;
2670 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2671 ERR_add_error_dataf("extension: %u", (unsigned)type);
2672 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002673 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002674 }
Adam Langley75712922014-10-10 16:23:43 -07002675
David Benjamin1deb41b2016-08-09 19:36:38 -04002676 for (size_t i = 0; i < kNumExtensions; i++) {
2677 if (ssl->s3->tmp.extensions.received & (1u << i)) {
2678 continue;
2679 }
2680
2681 CBS *contents = NULL, fake_contents;
2682 static const uint8_t kFakeRenegotiateExtension[] = {0};
2683 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2684 ssl_client_cipher_list_contains_cipher(client_hello,
2685 SSL3_CK_SCSV & 0xffff)) {
2686 /* The renegotiation SCSV was received so pretend that we received a
2687 * renegotiation extension. */
2688 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2689 sizeof(kFakeRenegotiateExtension));
2690 contents = &fake_contents;
2691 ssl->s3->tmp.extensions.received |= (1u << i);
2692 }
2693
2694 /* Extension wasn't observed so call the callback with a NULL
2695 * parameter. */
2696 uint8_t alert = SSL_AD_DECODE_ERROR;
2697 if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
2698 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2699 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2700 *out_alert = alert;
2701 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002702 }
2703 }
2704
Adam Langleyfcf25832014-12-18 17:42:32 -08002705 return 1;
2706}
Adam Langley95c29f32014-06-20 12:00:00 -07002707
David Benjamine14ff062016-08-09 16:21:24 -04002708int ssl_parse_clienthello_tlsext(
2709 SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002710 int alert = -1;
David Benjamine14ff062016-08-09 16:21:24 -04002711 if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002712 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002713 return 0;
2714 }
Adam Langley95c29f32014-06-20 12:00:00 -07002715
David Benjamin0d56f882015-12-19 17:05:56 -05002716 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002717 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002718 return 0;
2719 }
Adam Langley95c29f32014-06-20 12:00:00 -07002720
Adam Langleyfcf25832014-12-18 17:42:32 -08002721 return 1;
2722}
Adam Langley95c29f32014-06-20 12:00:00 -07002723
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002724OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2725
David Benjamin0d56f882015-12-19 17:05:56 -05002726static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002727 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2728 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2729 return 1;
2730 }
Adam Langley614c66a2015-06-12 15:26:58 -07002731
Steven Valdez143e8b32016-07-11 13:19:03 -04002732 /* Decode the extensions block and check it is valid. */
2733 CBS extensions;
2734 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2735 !tls1_check_duplicate_extensions(&extensions)) {
2736 *out_alert = SSL_AD_DECODE_ERROR;
2737 return 0;
2738 }
2739
2740 uint32_t received = 0;
2741 while (CBS_len(&extensions) != 0) {
2742 uint16_t type;
2743 CBS extension;
2744
2745 /* Decode the next extension. */
2746 if (!CBS_get_u16(&extensions, &type) ||
2747 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002748 *out_alert = SSL_AD_DECODE_ERROR;
2749 return 0;
2750 }
Adam Langley95c29f32014-06-20 12:00:00 -07002751
Steven Valdez143e8b32016-07-11 13:19:03 -04002752 unsigned ext_index;
2753 const struct tls_extension *const ext =
2754 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002755
Steven Valdez143e8b32016-07-11 13:19:03 -04002756 if (ext == NULL) {
2757 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002758 return 0;
2759 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002760 continue;
2761 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002762
David Benjamin1deb41b2016-08-09 19:36:38 -04002763 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index)) &&
2764 type != TLSEXT_TYPE_renegotiate) {
2765 /* If the extension was never sent then it is illegal, except for the
2766 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002767 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2768 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002769 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002770 return 0;
2771 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002772
Steven Valdez143e8b32016-07-11 13:19:03 -04002773 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002774
Steven Valdez143e8b32016-07-11 13:19:03 -04002775 uint8_t alert = SSL_AD_DECODE_ERROR;
2776 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2777 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2778 ERR_add_error_dataf("extension: %u", (unsigned)type);
2779 *out_alert = alert;
2780 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002781 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002782 }
Adam Langley95c29f32014-06-20 12:00:00 -07002783
Adam Langley33ad2b52015-07-20 17:43:53 -07002784 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002785 for (i = 0; i < kNumExtensions; i++) {
2786 if (!(received & (1u << i))) {
2787 /* Extension wasn't observed so call the callback with a NULL
2788 * parameter. */
2789 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002790 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002791 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2792 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002793 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002794 return 0;
2795 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002796 }
2797 }
Adam Langley95c29f32014-06-20 12:00:00 -07002798
Adam Langleyfcf25832014-12-18 17:42:32 -08002799 return 1;
2800}
Adam Langley95c29f32014-06-20 12:00:00 -07002801
David Benjamin0d56f882015-12-19 17:05:56 -05002802static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002803 int ret = SSL_TLSEXT_ERR_NOACK;
2804 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002805
David Benjamin78f8aab2016-03-10 16:33:58 -05002806 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002807 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002808 ssl->ctx->tlsext_servername_arg);
2809 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002810 ret = ssl->initial_ctx->tlsext_servername_callback(
2811 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002812 }
Adam Langley95c29f32014-06-20 12:00:00 -07002813
Adam Langleyfcf25832014-12-18 17:42:32 -08002814 switch (ret) {
2815 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002816 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002817 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002818
Adam Langleyfcf25832014-12-18 17:42:32 -08002819 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002820 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002821 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002822
Adam Langleyfcf25832014-12-18 17:42:32 -08002823 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002824 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002825 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002826
Adam Langleyfcf25832014-12-18 17:42:32 -08002827 default:
2828 return 1;
2829 }
2830}
Adam Langleyed8270a2014-09-02 13:52:56 -07002831
David Benjamin0d56f882015-12-19 17:05:56 -05002832static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002833 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002834 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002835
David Benjamin78f8aab2016-03-10 16:33:58 -05002836 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002837 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002838 ssl->ctx->tlsext_servername_arg);
2839 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002840 ret = ssl->initial_ctx->tlsext_servername_callback(
2841 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002842 }
Adam Langley95c29f32014-06-20 12:00:00 -07002843
Adam Langleyfcf25832014-12-18 17:42:32 -08002844 switch (ret) {
2845 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002846 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002847 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002848
Adam Langleyfcf25832014-12-18 17:42:32 -08002849 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002850 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002851 return 1;
2852
2853 default:
2854 return 1;
2855 }
2856}
2857
David Benjamin0d56f882015-12-19 17:05:56 -05002858int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002859 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002860 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2861 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002862 return 0;
2863 }
2864
David Benjamin0d56f882015-12-19 17:05:56 -05002865 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002866 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002867 return 0;
2868 }
2869
2870 return 1;
2871}
Adam Langley95c29f32014-06-20 12:00:00 -07002872
David Benjamine3aa1d92015-06-16 15:34:50 -04002873int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002874 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002875 size_t ticket_len, const uint8_t *session_id,
2876 size_t session_id_len) {
2877 int ret = 1; /* Most errors are non-fatal. */
2878 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2879 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002880
David Benjamine3aa1d92015-06-16 15:34:50 -04002881 HMAC_CTX hmac_ctx;
2882 HMAC_CTX_init(&hmac_ctx);
2883 EVP_CIPHER_CTX cipher_ctx;
2884 EVP_CIPHER_CTX_init(&cipher_ctx);
2885
David Benjaminef1b0092015-11-21 14:05:44 -05002886 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002887 *out_session = NULL;
2888
Steven Valdez4aa154e2016-07-29 14:32:55 -04002889 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
2890 goto done;
2891 }
2892
David Benjamine3aa1d92015-06-16 15:34:50 -04002893 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2894 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002895 }
2896
David Benjaminadcc3952015-04-26 13:07:57 -04002897 /* Ensure there is room for the key name and the largest IV
2898 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2899 * the maximum IV length should be well under the minimum size for the
2900 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002901 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2902 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002903 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002904 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002905
David Benjamine3aa1d92015-06-16 15:34:50 -04002906 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002907 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2908 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2909 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002910 if (cb_ret < 0) {
2911 ret = 0;
2912 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002913 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002914 if (cb_ret == 0) {
2915 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002916 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002917 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002918 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002919 }
2920 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002921 /* Check the key name matches. */
2922 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2923 SSL_TICKET_KEY_NAME_LEN) != 0) {
2924 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002925 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002926 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2927 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002928 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002929 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2930 ssl_ctx->tlsext_tick_aes_key, iv)) {
2931 ret = 0;
2932 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002933 }
2934 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002935 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002936
David Benjamine3aa1d92015-06-16 15:34:50 -04002937 /* Check the MAC at the end of the ticket. */
2938 uint8_t mac[EVP_MAX_MD_SIZE];
2939 size_t mac_len = HMAC_size(&hmac_ctx);
2940 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002941 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002942 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002943 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002944 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2945 HMAC_Final(&hmac_ctx, mac, NULL);
2946 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2947 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002948 }
2949
David Benjamine3aa1d92015-06-16 15:34:50 -04002950 /* Decrypt the session data. */
2951 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2952 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2953 mac_len;
2954 plaintext = OPENSSL_malloc(ciphertext_len);
2955 if (plaintext == NULL) {
2956 ret = 0;
2957 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002958 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002959 if (ciphertext_len >= INT_MAX) {
2960 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002961 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002962 int len1, len2;
2963 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2964 (int)ciphertext_len) ||
2965 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2966 ERR_clear_error(); /* Don't leave an error on the queue. */
2967 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002968 }
2969
David Benjamine3aa1d92015-06-16 15:34:50 -04002970 /* Decode the session. */
2971 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2972 if (session == NULL) {
2973 ERR_clear_error(); /* Don't leave an error on the queue. */
2974 goto done;
2975 }
2976
2977 /* Copy the client's session ID into the new session, to denote the ticket has
2978 * been accepted. */
2979 memcpy(session->session_id, session_id, session_id_len);
2980 session->session_id_length = session_id_len;
2981
Steven Valdez4aa154e2016-07-29 14:32:55 -04002982 if (!ssl_session_is_context_valid(ssl, session) ||
2983 !ssl_session_is_time_valid(ssl, session)) {
2984 SSL_SESSION_free(session);
2985 session = NULL;
2986 }
2987
David Benjamine3aa1d92015-06-16 15:34:50 -04002988 *out_session = session;
2989
2990done:
2991 OPENSSL_free(plaintext);
2992 HMAC_CTX_cleanup(&hmac_ctx);
2993 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2994 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002995}
Adam Langley95c29f32014-06-20 12:00:00 -07002996
Steven Valdez0d62f262015-09-04 12:41:04 -04002997int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002998 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05002999 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003000 return 1;
3001 }
David Benjamincd996942014-07-20 16:23:51 -04003002
Steven Valdez0d62f262015-09-04 12:41:04 -04003003 CERT *const cert = ssl->cert;
3004 OPENSSL_free(cert->peer_sigalgs);
3005 cert->peer_sigalgs = NULL;
3006 cert->peer_sigalgslen = 0;
3007
3008 size_t num_sigalgs = CBS_len(in_sigalgs);
3009
3010 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003011 return 0;
3012 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003013 num_sigalgs /= 2;
3014
3015 /* supported_signature_algorithms in the certificate request is
3016 * allowed to be empty. */
3017 if (num_sigalgs == 0) {
3018 return 1;
3019 }
3020
Steven Valdez02563852016-06-23 13:33:05 -04003021 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3022 * and we just divided |num_sigalgs| by two. */
3023 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
Steven Valdez0d62f262015-09-04 12:41:04 -04003024 if (cert->peer_sigalgs == NULL) {
3025 return 0;
3026 }
3027 cert->peer_sigalgslen = num_sigalgs;
3028
3029 CBS sigalgs;
3030 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
3031
3032 size_t i;
3033 for (i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04003034 if (!CBS_get_u16(&sigalgs, &cert->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003035 return 0;
3036 }
3037 }
Adam Langley95c29f32014-06-20 12:00:00 -07003038
Adam Langleyfcf25832014-12-18 17:42:32 -08003039 return 1;
3040}
David Benjaminec2f27d2014-11-13 19:17:25 -05003041
David Benjaminea9a0d52016-07-08 15:52:59 -07003042int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003043 CERT *cert = ssl->cert;
Steven Valdez0d62f262015-09-04 12:41:04 -04003044 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05003045
Steven Valdezf0451ca2016-06-29 13:16:27 -04003046 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3047 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3048 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003049 int type = ssl_private_key_type(ssl);
3050 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003051 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003052 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003053 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003054 if (ssl_is_ecdsa_key_type(type)) {
3055 *out = SSL_SIGN_ECDSA_SHA1;
3056 return 1;
3057 }
3058 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3059 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003060 }
3061
Steven Valdezeff1e8d2016-07-06 14:24:47 -04003062 const uint16_t *sigalgs;
3063 size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07003064 if (cert->sigalgs != NULL) {
3065 sigalgs = cert->sigalgs;
3066 sigalgs_len = cert->sigalgs_len;
Steven Valdez0d62f262015-09-04 12:41:04 -04003067 }
3068
David Benjaminea9a0d52016-07-08 15:52:59 -07003069 const uint16_t *peer_sigalgs = cert->peer_sigalgs;
3070 size_t peer_sigalgs_len = cert->peer_sigalgslen;
David Benjamin51dd7d62016-07-08 16:07:01 -07003071 if (peer_sigalgs_len == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003072 /* If the client didn't specify any signature_algorithms extension then
3073 * we can assume that it supports SHA1. See
3074 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3075 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3076 SSL_SIGN_ECDSA_SHA1};
3077 peer_sigalgs = kDefaultPeerAlgorithms;
Steven Valdezcb966542016-08-17 16:56:14 -04003078 peer_sigalgs_len = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003079 }
3080
David Benjamind246b812016-07-08 15:07:02 -07003081 for (i = 0; i < sigalgs_len; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003082 uint16_t sigalg = sigalgs[i];
3083 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3084 * negotiated. */
3085 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3086 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3087 continue;
3088 }
3089
David Benjaminea9a0d52016-07-08 15:52:59 -07003090 for (j = 0; j < peer_sigalgs_len; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003091 if (sigalg == peer_sigalgs[j]) {
3092 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003093 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003094 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003095 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003096 }
Adam Langley95c29f32014-06-20 12:00:00 -07003097
David Benjaminea9a0d52016-07-08 15:52:59 -07003098 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3099 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003100}
Adam Langley95c29f32014-06-20 12:00:00 -07003101
David Benjamind6a4ae92015-08-06 11:10:51 -04003102int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
3103 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003104 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003105
3106 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003107 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
3108 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08003109 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003110
David Benjamind6a4ae92015-08-06 11:10:51 -04003111 static const char kClientIDMagic[] = "TLS Channel ID signature";
3112 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
3113
Steven Valdez87eab492016-06-27 16:34:59 -04003114 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003115 static const char kResumptionMagic[] = "Resumption";
3116 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
3117 if (ssl->session->original_handshake_hash_len == 0) {
3118 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3119 goto err;
3120 }
3121 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
3122 ssl->session->original_handshake_hash_len);
3123 }
3124
3125 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3126 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3127 sizeof(handshake_hash));
3128 if (handshake_hash_len < 0) {
3129 goto err;
3130 }
3131 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
3132 unsigned len_u;
3133 EVP_DigestFinal_ex(&ctx, out, &len_u);
3134 *out_len = len_u;
3135
3136 ret = 1;
3137
3138err:
3139 EVP_MD_CTX_cleanup(&ctx);
3140 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003141}
Adam Langley1258b6a2014-06-20 12:00:00 -07003142
3143/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003144 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003145 * data. */
3146int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003147 int digest_len;
3148 /* This function should never be called for a resumed session because the
3149 * handshake hashes that we wish to record are for the original, full
3150 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003151 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003152 return -1;
3153 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003154
Adam Langleyfcf25832014-12-18 17:42:32 -08003155 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003156 tls1_handshake_digest(
3157 ssl, ssl->s3->new_session->original_handshake_hash,
3158 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003159 if (digest_len < 0) {
3160 return -1;
3161 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003162
Steven Valdez87eab492016-06-27 16:34:59 -04003163 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003164
Adam Langleyfcf25832014-12-18 17:42:32 -08003165 return 1;
3166}