blob: dbf43135777a1cb63d8c14806b5d53c7bb8af3b1 [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) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400810 uint16_t min_version, max_version;
811 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
812 return 0;
813 }
814
815 /* Renegotiation indication is not necessary in TLS 1.3. */
816 if (min_version >= TLS1_3_VERSION) {
817 return 1;
818 }
819
Adam Langley5021b222015-06-12 18:27:58 -0700820 CBB contents, prev_finished;
821 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
822 !CBB_add_u16_length_prefixed(out, &contents) ||
823 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
824 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
825 ssl->s3->previous_client_finished_len) ||
826 !CBB_flush(out)) {
827 return 0;
828 }
829
830 return 1;
831}
832
833static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
834 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400835 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
836 return 0;
837 }
838
David Benjamin3e052de2015-11-25 20:10:31 -0500839 /* Servers may not switch between omitting the extension and supporting it.
840 * See RFC 5746, sections 3.5 and 4.2. */
841 if (ssl->s3->initial_handshake_complete &&
842 (contents != NULL) != ssl->s3->send_connection_binding) {
843 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
844 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
845 return 0;
846 }
847
Adam Langley5021b222015-06-12 18:27:58 -0700848 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500849 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700850 * RI even on initial ServerHello because the client doesn't see any
851 * renegotiation during an attack. However this would mean we could not
852 * connect to any server which doesn't support RI.
853 *
David Benjamine9cddb82015-11-23 14:36:40 -0500854 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
855 * practical terms every client sets it so it's just assumed here. */
856 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700857 }
858
859 const size_t expected_len = ssl->s3->previous_client_finished_len +
860 ssl->s3->previous_server_finished_len;
861
862 /* Check for logic errors */
863 assert(!expected_len || ssl->s3->previous_client_finished_len);
864 assert(!expected_len || ssl->s3->previous_server_finished_len);
865
866 /* Parse out the extension contents. */
867 CBS renegotiated_connection;
868 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
869 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400870 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700871 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
872 return 0;
873 }
874
875 /* Check that the extension matches. */
876 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400877 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700878 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
879 return 0;
880 }
881
882 const uint8_t *d = CBS_data(&renegotiated_connection);
883 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
884 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400885 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700886 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
887 return 0;
888 }
889 d += ssl->s3->previous_client_finished_len;
890
891 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
892 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400893 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700894 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
895 return 0;
896 }
897 ssl->s3->send_connection_binding = 1;
898
899 return 1;
900}
901
902static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
903 CBS *contents) {
904 /* Renegotiation isn't supported as a server so this function should never be
905 * called after the initial handshake. */
906 assert(!ssl->s3->initial_handshake_complete);
907
Steven Valdez143e8b32016-07-11 13:19:03 -0400908 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
909 return 1;
910 }
911
Adam Langley5021b222015-06-12 18:27:58 -0700912 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400913 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700914 }
915
916 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700917 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
918 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400919 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700920 return 0;
921 }
922
923 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500924 if (!CBS_mem_equal(&renegotiated_connection,
925 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700926 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400927 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700928 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
929 return 0;
930 }
931
932 ssl->s3->send_connection_binding = 1;
933
934 return 1;
935}
936
937static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400938 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
939 return 1;
940 }
941
Adam Langley5021b222015-06-12 18:27:58 -0700942 CBB contents, prev_finished;
943 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
944 !CBB_add_u16_length_prefixed(out, &contents) ||
945 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
946 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
947 ssl->s3->previous_client_finished_len) ||
948 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
949 ssl->s3->previous_server_finished_len) ||
950 !CBB_flush(out)) {
951 return 0;
952 }
953
954 return 1;
955}
956
Adam Langley0a056712015-07-01 15:03:33 -0700957
958/* Extended Master Secret.
959 *
David Benjamin43946d42016-02-01 08:42:19 -0500960 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700961
962static void ext_ems_init(SSL *ssl) {
963 ssl->s3->tmp.extended_master_secret = 0;
964}
965
966static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400967 uint16_t min_version, max_version;
968 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
969 return 0;
970 }
971
972 /* Extended master secret is not necessary in TLS 1.3. */
973 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700974 return 1;
975 }
976
977 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
978 !CBB_add_u16(out, 0 /* length */)) {
979 return 0;
980 }
981
982 return 1;
983}
984
985static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
986 CBS *contents) {
987 if (contents == NULL) {
988 return 1;
989 }
990
Steven Valdez143e8b32016-07-11 13:19:03 -0400991 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
992 ssl->version == SSL3_VERSION) {
993 return 0;
994 }
995
996 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700997 return 0;
998 }
999
1000 ssl->s3->tmp.extended_master_secret = 1;
1001 return 1;
1002}
1003
David Benjamin0d56f882015-12-19 17:05:56 -05001004static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1005 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001006 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1007 ssl->version == SSL3_VERSION) {
1008 return 1;
1009 }
1010
1011 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -07001012 return 1;
1013 }
1014
1015 if (CBS_len(contents) != 0) {
1016 return 0;
1017 }
1018
1019 ssl->s3->tmp.extended_master_secret = 1;
1020 return 1;
1021}
1022
1023static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1024 if (!ssl->s3->tmp.extended_master_secret) {
1025 return 1;
1026 }
1027
1028 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1029 !CBB_add_u16(out, 0 /* length */)) {
1030 return 0;
1031 }
1032
1033 return 1;
1034}
1035
Adam Langley9b05bc52015-07-01 15:25:33 -07001036
1037/* Session tickets.
1038 *
1039 * https://tools.ietf.org/html/rfc5077 */
1040
1041static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -04001042 uint16_t min_version, max_version;
1043 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1044 return 0;
1045 }
1046
1047 /* TLS 1.3 uses a different ticket extension. */
1048 if (min_version >= TLS1_3_VERSION ||
1049 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001050 return 1;
1051 }
1052
1053 const uint8_t *ticket_data = NULL;
1054 int ticket_len = 0;
1055
1056 /* Renegotiation does not participate in session resumption. However, still
1057 * advertise the extension to avoid potentially breaking servers which carry
1058 * over the state from the previous handshake, such as OpenSSL servers
1059 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1060 if (!ssl->s3->initial_handshake_complete &&
1061 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001062 ssl->session->tlsext_tick != NULL &&
1063 /* Don't send TLS 1.3 session tickets in the ticket extension. */
1064 ssl->method->version_from_wire(ssl->session->ssl_version) <
1065 TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001066 ticket_data = ssl->session->tlsext_tick;
1067 ticket_len = ssl->session->tlsext_ticklen;
1068 }
1069
1070 CBB ticket;
1071 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1072 !CBB_add_u16_length_prefixed(out, &ticket) ||
1073 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1074 !CBB_flush(out)) {
1075 return 0;
1076 }
1077
1078 return 1;
1079}
1080
1081static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1082 CBS *contents) {
1083 ssl->tlsext_ticket_expected = 0;
1084
1085 if (contents == NULL) {
1086 return 1;
1087 }
1088
Steven Valdez143e8b32016-07-11 13:19:03 -04001089 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1090 return 0;
1091 }
1092
Adam Langley9b05bc52015-07-01 15:25:33 -07001093 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1094 * this function should never be called, even if the server tries to send the
1095 * extension. */
1096 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1097
1098 if (CBS_len(contents) != 0) {
1099 return 0;
1100 }
1101
1102 ssl->tlsext_ticket_expected = 1;
1103 return 1;
1104}
1105
Adam Langley9b05bc52015-07-01 15:25:33 -07001106static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1107 if (!ssl->tlsext_ticket_expected) {
1108 return 1;
1109 }
1110
1111 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1112 * true. */
1113 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1114
1115 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1116 !CBB_add_u16(out, 0 /* length */)) {
1117 return 0;
1118 }
1119
1120 return 1;
1121}
1122
1123
Adam Langley2e857bd2015-07-01 16:09:19 -07001124/* Signature Algorithms.
1125 *
1126 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1127
1128static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjaminb6a0a512016-06-21 10:33:21 -04001129 if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001130 return 1;
1131 }
1132
Steven Valdez02563852016-06-23 13:33:05 -04001133 const uint16_t *sigalgs_data;
Adam Langley2e857bd2015-07-01 16:09:19 -07001134 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1135
1136 CBB contents, sigalgs;
1137 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1138 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez02563852016-06-23 13:33:05 -04001139 !CBB_add_u16_length_prefixed(&contents, &sigalgs)) {
1140 return 0;
1141 }
1142
1143 size_t i;
1144 for (i = 0; i < sigalgs_len; i++) {
1145 if (!CBB_add_u16(&sigalgs, sigalgs_data[i])) {
1146 return 0;
1147 }
1148 }
1149
1150 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001151 return 0;
1152 }
1153
1154 return 1;
1155}
1156
Adam Langley2e857bd2015-07-01 16:09:19 -07001157static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1158 CBS *contents) {
1159 OPENSSL_free(ssl->cert->peer_sigalgs);
1160 ssl->cert->peer_sigalgs = NULL;
1161 ssl->cert->peer_sigalgslen = 0;
1162
Adam Langley2e857bd2015-07-01 16:09:19 -07001163 if (contents == NULL) {
1164 return 1;
1165 }
1166
1167 CBS supported_signature_algorithms;
1168 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001169 CBS_len(contents) != 0 ||
1170 CBS_len(&supported_signature_algorithms) == 0 ||
1171 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001172 return 0;
1173 }
1174
1175 return 1;
1176}
1177
Adam Langley2e857bd2015-07-01 16:09:19 -07001178
Adam Langleybb0bd042015-07-01 16:21:03 -07001179/* OCSP Stapling.
1180 *
1181 * https://tools.ietf.org/html/rfc6066#section-8 */
1182
1183static void ext_ocsp_init(SSL *ssl) {
1184 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001185 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001186}
1187
1188static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1189 if (!ssl->ocsp_stapling_enabled) {
1190 return 1;
1191 }
1192
1193 CBB contents;
1194 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1195 !CBB_add_u16_length_prefixed(out, &contents) ||
1196 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1197 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1198 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1199 !CBB_flush(out)) {
1200 return 0;
1201 }
1202
Adam Langleyce9d85e2016-01-24 15:58:39 -08001203 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001204 return 1;
1205}
1206
1207static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001208 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001209 if (contents == NULL) {
1210 return 1;
1211 }
1212
David Benjamin942f4ed2016-07-16 19:03:49 +03001213 /* OCSP stapling is forbidden on a non-certificate cipher. */
1214 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001215 return 0;
1216 }
1217
David Benjamin942f4ed2016-07-16 19:03:49 +03001218 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1219 if (CBS_len(contents) != 0) {
1220 return 0;
1221 }
1222
1223 ssl->s3->tmp.certificate_status_expected = 1;
1224 return 1;
1225 }
1226
1227 uint8_t status_type;
1228 CBS ocsp_response;
1229 if (!CBS_get_u8(contents, &status_type) ||
1230 status_type != TLSEXT_STATUSTYPE_ocsp ||
1231 !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1232 CBS_len(&ocsp_response) == 0 ||
1233 CBS_len(contents) != 0) {
1234 return 0;
1235 }
1236
Steven Valdez87eab492016-06-27 16:34:59 -04001237 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1238 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001239 *out_alert = SSL_AD_INTERNAL_ERROR;
1240 return 0;
1241 }
1242
Adam Langleybb0bd042015-07-01 16:21:03 -07001243 return 1;
1244}
1245
1246static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1247 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001248 if (contents == NULL) {
1249 return 1;
1250 }
1251
1252 uint8_t status_type;
1253 if (!CBS_get_u8(contents, &status_type)) {
1254 return 0;
1255 }
1256
1257 /* We cannot decide whether OCSP stapling will occur yet because the correct
1258 * SSL_CTX might not have been selected. */
1259 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1260
Adam Langleybb0bd042015-07-01 16:21:03 -07001261 return 1;
1262}
1263
1264static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001265 if (!ssl->s3->tmp.ocsp_stapling_requested ||
1266 ssl->ctx->ocsp_response_length == 0 ||
1267 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001268 return 1;
1269 }
1270
David Benjamin942f4ed2016-07-16 19:03:49 +03001271 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1272 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez87eab492016-06-27 16:34:59 -04001273 if (ssl->session != NULL) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001274 return 1;
1275 }
1276
1277 ssl->s3->tmp.certificate_status_expected = 1;
1278
1279 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1280 CBB_add_u16(out, 0 /* length */);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001281 }
1282
David Benjamin942f4ed2016-07-16 19:03:49 +03001283 CBB body, ocsp_response;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001284 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
David Benjamin942f4ed2016-07-16 19:03:49 +03001285 CBB_add_u16_length_prefixed(out, &body) &&
1286 CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1287 CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1288 CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1289 ssl->ctx->ocsp_response_length) &&
1290 CBB_flush(out);
Adam Langleybb0bd042015-07-01 16:21:03 -07001291}
1292
1293
Adam Langley97dfcbf2015-07-01 18:35:20 -07001294/* Next protocol negotiation.
1295 *
1296 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1297
1298static void ext_npn_init(SSL *ssl) {
1299 ssl->s3->next_proto_neg_seen = 0;
1300}
1301
1302static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1303 if (ssl->s3->initial_handshake_complete ||
1304 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001305 (ssl->options & SSL_OP_DISABLE_NPN) ||
David Benjamince079fd2016-08-02 16:22:34 -04001306 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001307 return 1;
1308 }
1309
1310 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1311 !CBB_add_u16(out, 0 /* length */)) {
1312 return 0;
1313 }
1314
1315 return 1;
1316}
1317
1318static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1319 CBS *contents) {
1320 if (contents == NULL) {
1321 return 1;
1322 }
1323
Steven Valdez143e8b32016-07-11 13:19:03 -04001324 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1325 return 0;
1326 }
1327
Adam Langley97dfcbf2015-07-01 18:35:20 -07001328 /* If any of these are false then we should never have sent the NPN
1329 * extension in the ClientHello and thus this function should never have been
1330 * called. */
1331 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001332 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001333 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001334 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001335
David Benjamin76c2efc2015-08-31 14:24:29 -04001336 if (ssl->s3->alpn_selected != NULL) {
1337 /* NPN and ALPN may not be negotiated in the same connection. */
1338 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1339 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1340 return 0;
1341 }
1342
Adam Langley97dfcbf2015-07-01 18:35:20 -07001343 const uint8_t *const orig_contents = CBS_data(contents);
1344 const size_t orig_len = CBS_len(contents);
1345
1346 while (CBS_len(contents) != 0) {
1347 CBS proto;
1348 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1349 CBS_len(&proto) == 0) {
1350 return 0;
1351 }
1352 }
1353
1354 uint8_t *selected;
1355 uint8_t selected_len;
1356 if (ssl->ctx->next_proto_select_cb(
1357 ssl, &selected, &selected_len, orig_contents, orig_len,
1358 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1359 *out_alert = SSL_AD_INTERNAL_ERROR;
1360 return 0;
1361 }
1362
David Benjamin79978df2015-12-25 15:56:49 -05001363 OPENSSL_free(ssl->s3->next_proto_negotiated);
1364 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1365 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001366 *out_alert = SSL_AD_INTERNAL_ERROR;
1367 return 0;
1368 }
1369
David Benjamin79978df2015-12-25 15:56:49 -05001370 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001371 ssl->s3->next_proto_neg_seen = 1;
1372
1373 return 1;
1374}
1375
1376static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1377 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001378 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1379 return 1;
1380 }
1381
Adam Langley97dfcbf2015-07-01 18:35:20 -07001382 if (contents != NULL && CBS_len(contents) != 0) {
1383 return 0;
1384 }
1385
1386 if (contents == NULL ||
1387 ssl->s3->initial_handshake_complete ||
1388 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1389 * afterwards, parsing the ALPN extension will clear
1390 * |next_proto_neg_seen|. */
1391 ssl->s3->alpn_selected != NULL ||
1392 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001393 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001394 return 1;
1395 }
1396
1397 ssl->s3->next_proto_neg_seen = 1;
1398 return 1;
1399}
1400
1401static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1402 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1403 * parsed. */
1404 if (!ssl->s3->next_proto_neg_seen) {
1405 return 1;
1406 }
1407
1408 const uint8_t *npa;
1409 unsigned npa_len;
1410
1411 if (ssl->ctx->next_protos_advertised_cb(
1412 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1413 SSL_TLSEXT_ERR_OK) {
1414 ssl->s3->next_proto_neg_seen = 0;
1415 return 1;
1416 }
1417
1418 CBB contents;
1419 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1420 !CBB_add_u16_length_prefixed(out, &contents) ||
1421 !CBB_add_bytes(&contents, npa, npa_len) ||
1422 !CBB_flush(out)) {
1423 return 0;
1424 }
1425
1426 return 1;
1427}
1428
1429
Adam Langleyab8d87d2015-07-10 12:21:39 -07001430/* Signed certificate timestamps.
1431 *
1432 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1433
1434static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1435 if (!ssl->signed_cert_timestamps_enabled) {
1436 return 1;
1437 }
1438
1439 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1440 !CBB_add_u16(out, 0 /* length */)) {
1441 return 0;
1442 }
1443
1444 return 1;
1445}
1446
1447static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1448 CBS *contents) {
1449 if (contents == NULL) {
1450 return 1;
1451 }
1452
1453 /* If this is false then we should never have sent the SCT extension in the
1454 * ClientHello and thus this function should never have been called. */
1455 assert(ssl->signed_cert_timestamps_enabled);
1456
1457 if (CBS_len(contents) == 0) {
1458 *out_alert = SSL_AD_DECODE_ERROR;
1459 return 0;
1460 }
1461
1462 /* Session resumption uses the original session information. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001463 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001464 !CBS_stow(
1465 contents,
1466 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1467 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001468 *out_alert = SSL_AD_INTERNAL_ERROR;
1469 return 0;
1470 }
1471
1472 return 1;
1473}
1474
1475static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1476 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001477 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001478}
1479
1480static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001481 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001482 if (ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001483 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001484 return 1;
1485 }
1486
1487 CBB contents;
1488 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1489 CBB_add_u16_length_prefixed(out, &contents) &&
1490 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1491 ssl->ctx->signed_cert_timestamp_list_length) &&
1492 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001493}
1494
1495
Adam Langleyf18e4532015-07-10 13:39:53 -07001496/* Application-level Protocol Negotiation.
1497 *
1498 * https://tools.ietf.org/html/rfc7301 */
1499
1500static void ext_alpn_init(SSL *ssl) {
1501 OPENSSL_free(ssl->s3->alpn_selected);
1502 ssl->s3->alpn_selected = NULL;
1503}
1504
1505static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1506 if (ssl->alpn_client_proto_list == NULL ||
1507 ssl->s3->initial_handshake_complete) {
1508 return 1;
1509 }
1510
1511 CBB contents, proto_list;
1512 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1513 !CBB_add_u16_length_prefixed(out, &contents) ||
1514 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1515 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1516 ssl->alpn_client_proto_list_len) ||
1517 !CBB_flush(out)) {
1518 return 0;
1519 }
1520
1521 return 1;
1522}
1523
1524static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1525 CBS *contents) {
1526 if (contents == NULL) {
1527 return 1;
1528 }
1529
1530 assert(!ssl->s3->initial_handshake_complete);
1531 assert(ssl->alpn_client_proto_list != NULL);
1532
David Benjamin76c2efc2015-08-31 14:24:29 -04001533 if (ssl->s3->next_proto_neg_seen) {
1534 /* NPN and ALPN may not be negotiated in the same connection. */
1535 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1536 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1537 return 0;
1538 }
1539
Adam Langleyf18e4532015-07-10 13:39:53 -07001540 /* The extension data consists of a ProtocolNameList which must have
1541 * exactly one ProtocolName. Each of these is length-prefixed. */
1542 CBS protocol_name_list, protocol_name;
1543 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1544 CBS_len(contents) != 0 ||
1545 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1546 /* Empty protocol names are forbidden. */
1547 CBS_len(&protocol_name) == 0 ||
1548 CBS_len(&protocol_name_list) != 0) {
1549 return 0;
1550 }
1551
David Benjamin3e517572016-08-11 11:52:23 -04001552 /* Check that the protcol name is one of the ones we advertised. */
1553 int protocol_ok = 0;
1554 CBS client_protocol_name_list, client_protocol_name;
1555 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1556 ssl->alpn_client_proto_list_len);
1557 while (CBS_len(&client_protocol_name_list) > 0) {
1558 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1559 &client_protocol_name)) {
1560 *out_alert = SSL_AD_INTERNAL_ERROR;
1561 return 0;
1562 }
1563
1564 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1565 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1566 CBS_len(&protocol_name)) == 0) {
1567 protocol_ok = 1;
1568 break;
1569 }
1570 }
1571
1572 if (!protocol_ok) {
1573 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1574 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1575 return 0;
1576 }
1577
Adam Langleyf18e4532015-07-10 13:39:53 -07001578 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1579 &ssl->s3->alpn_selected_len)) {
1580 *out_alert = SSL_AD_INTERNAL_ERROR;
1581 return 0;
1582 }
1583
1584 return 1;
1585}
1586
1587static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1588 CBS *contents) {
1589 if (contents == NULL) {
1590 return 1;
1591 }
1592
1593 if (ssl->ctx->alpn_select_cb == NULL ||
1594 ssl->s3->initial_handshake_complete) {
1595 return 1;
1596 }
1597
1598 /* ALPN takes precedence over NPN. */
1599 ssl->s3->next_proto_neg_seen = 0;
1600
1601 CBS protocol_name_list;
1602 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1603 CBS_len(contents) != 0 ||
1604 CBS_len(&protocol_name_list) < 2) {
1605 return 0;
1606 }
1607
1608 /* Validate the protocol list. */
1609 CBS protocol_name_list_copy = protocol_name_list;
1610 while (CBS_len(&protocol_name_list_copy) > 0) {
1611 CBS protocol_name;
1612
1613 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1614 /* Empty protocol names are forbidden. */
1615 CBS_len(&protocol_name) == 0) {
1616 return 0;
1617 }
1618 }
1619
1620 const uint8_t *selected;
1621 uint8_t selected_len;
1622 if (ssl->ctx->alpn_select_cb(
1623 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1624 CBS_len(&protocol_name_list),
1625 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1626 OPENSSL_free(ssl->s3->alpn_selected);
1627 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1628 if (ssl->s3->alpn_selected == NULL) {
1629 *out_alert = SSL_AD_INTERNAL_ERROR;
1630 return 0;
1631 }
1632 ssl->s3->alpn_selected_len = selected_len;
1633 }
1634
1635 return 1;
1636}
1637
1638static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1639 if (ssl->s3->alpn_selected == NULL) {
1640 return 1;
1641 }
1642
1643 CBB contents, proto_list, proto;
1644 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1645 !CBB_add_u16_length_prefixed(out, &contents) ||
1646 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1647 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001648 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1649 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001650 !CBB_flush(out)) {
1651 return 0;
1652 }
1653
1654 return 1;
1655}
1656
1657
Adam Langley49c7af12015-07-10 14:33:46 -07001658/* Channel ID.
1659 *
1660 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1661
1662static void ext_channel_id_init(SSL *ssl) {
1663 ssl->s3->tlsext_channel_id_valid = 0;
1664}
1665
1666static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1667 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001668 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001669 return 1;
1670 }
1671
1672 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1673 !CBB_add_u16(out, 0 /* length */)) {
1674 return 0;
1675 }
1676
1677 return 1;
1678}
1679
1680static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1681 CBS *contents) {
1682 if (contents == NULL) {
1683 return 1;
1684 }
1685
Steven Valdez143e8b32016-07-11 13:19:03 -04001686 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1687 return 0;
1688 }
1689
David Benjamince079fd2016-08-02 16:22:34 -04001690 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001691 assert(ssl->tlsext_channel_id_enabled);
1692
1693 if (CBS_len(contents) != 0) {
1694 return 0;
1695 }
1696
1697 ssl->s3->tlsext_channel_id_valid = 1;
1698 return 1;
1699}
1700
1701static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1702 CBS *contents) {
1703 if (contents == NULL ||
1704 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001705 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001706 return 1;
1707 }
1708
1709 if (CBS_len(contents) != 0) {
1710 return 0;
1711 }
1712
1713 ssl->s3->tlsext_channel_id_valid = 1;
1714 return 1;
1715}
1716
1717static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001718 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1719 return 1;
1720 }
1721
Adam Langley49c7af12015-07-10 14:33:46 -07001722 if (!ssl->s3->tlsext_channel_id_valid) {
1723 return 1;
1724 }
1725
1726 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1727 !CBB_add_u16(out, 0 /* length */)) {
1728 return 0;
1729 }
1730
1731 return 1;
1732}
1733
Adam Langley391250d2015-07-15 19:06:07 -07001734
1735/* Secure Real-time Transport Protocol (SRTP) extension.
1736 *
1737 * https://tools.ietf.org/html/rfc5764 */
1738
Adam Langley391250d2015-07-15 19:06:07 -07001739
1740static void ext_srtp_init(SSL *ssl) {
1741 ssl->srtp_profile = NULL;
1742}
1743
1744static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1745 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1746 if (profiles == NULL) {
1747 return 1;
1748 }
1749 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1750 if (num_profiles == 0) {
1751 return 1;
1752 }
1753
1754 CBB contents, profile_ids;
1755 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1756 !CBB_add_u16_length_prefixed(out, &contents) ||
1757 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1758 return 0;
1759 }
1760
1761 size_t i;
1762 for (i = 0; i < num_profiles; i++) {
1763 if (!CBB_add_u16(&profile_ids,
1764 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1765 return 0;
1766 }
1767 }
1768
1769 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1770 !CBB_flush(out)) {
1771 return 0;
1772 }
1773
1774 return 1;
1775}
1776
1777static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1778 CBS *contents) {
1779 if (contents == NULL) {
1780 return 1;
1781 }
1782
1783 /* The extension consists of a u16-prefixed profile ID list containing a
1784 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1785 *
1786 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1787 CBS profile_ids, srtp_mki;
1788 uint16_t profile_id;
1789 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1790 !CBS_get_u16(&profile_ids, &profile_id) ||
1791 CBS_len(&profile_ids) != 0 ||
1792 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1793 CBS_len(contents) != 0) {
1794 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1795 return 0;
1796 }
1797
1798 if (CBS_len(&srtp_mki) != 0) {
1799 /* Must be no MKI, since we never offer one. */
1800 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1801 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1802 return 0;
1803 }
1804
1805 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1806
1807 /* Check to see if the server gave us something we support (and presumably
1808 * offered). */
1809 size_t i;
1810 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1811 const SRTP_PROTECTION_PROFILE *profile =
1812 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1813
1814 if (profile->id == profile_id) {
1815 ssl->srtp_profile = profile;
1816 return 1;
1817 }
1818 }
1819
1820 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1821 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1822 return 0;
1823}
1824
1825static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1826 CBS *contents) {
1827 if (contents == NULL) {
1828 return 1;
1829 }
1830
1831 CBS profile_ids, srtp_mki;
1832 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1833 CBS_len(&profile_ids) < 2 ||
1834 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1835 CBS_len(contents) != 0) {
1836 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1837 return 0;
1838 }
1839 /* Discard the MKI value for now. */
1840
1841 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1842 SSL_get_srtp_profiles(ssl);
1843
1844 /* Pick the server's most preferred profile. */
1845 size_t i;
1846 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1847 const SRTP_PROTECTION_PROFILE *server_profile =
1848 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1849
1850 CBS profile_ids_tmp;
1851 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1852
1853 while (CBS_len(&profile_ids_tmp) > 0) {
1854 uint16_t profile_id;
1855 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1856 return 0;
1857 }
1858
1859 if (server_profile->id == profile_id) {
1860 ssl->srtp_profile = server_profile;
1861 return 1;
1862 }
1863 }
1864 }
1865
1866 return 1;
1867}
1868
1869static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1870 if (ssl->srtp_profile == NULL) {
1871 return 1;
1872 }
1873
1874 CBB contents, profile_ids;
1875 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1876 !CBB_add_u16_length_prefixed(out, &contents) ||
1877 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1878 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1879 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1880 !CBB_flush(out)) {
1881 return 0;
1882 }
1883
1884 return 1;
1885}
1886
Adam Langleybdd5d662015-07-20 16:19:08 -07001887
1888/* EC point formats.
1889 *
1890 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1891
1892static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
David Benjamince079fd2016-08-02 16:22:34 -04001893 if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001894 return 0;
1895 }
1896
1897 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1898
1899 size_t i;
1900 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1901 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1902
1903 const uint32_t alg_k = cipher->algorithm_mkey;
1904 const uint32_t alg_a = cipher->algorithm_auth;
1905 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1906 return 1;
1907 }
1908 }
1909
1910 return 0;
1911}
1912
Adam Langleybdd5d662015-07-20 16:19:08 -07001913static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001914 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001915 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1916 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001917 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1918 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001919 !CBB_flush(out)) {
1920 return 0;
1921 }
1922
1923 return 1;
1924}
1925
1926static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1927 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1928 return 1;
1929 }
1930
1931 return ext_ec_point_add_extension(ssl, out);
1932}
1933
1934static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1935 CBS *contents) {
1936 if (contents == NULL) {
1937 return 1;
1938 }
1939
Steven Valdez143e8b32016-07-11 13:19:03 -04001940 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1941 return 0;
1942 }
1943
Adam Langleybdd5d662015-07-20 16:19:08 -07001944 CBS ec_point_format_list;
1945 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1946 CBS_len(contents) != 0) {
1947 return 0;
1948 }
1949
David Benjaminfc059942015-07-30 23:01:59 -04001950 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1951 * point format. */
1952 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1953 CBS_len(&ec_point_format_list)) == NULL) {
1954 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001955 return 0;
1956 }
1957
1958 return 1;
1959}
1960
1961static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1962 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001963 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1964 return 1;
1965 }
1966
Adam Langleybdd5d662015-07-20 16:19:08 -07001967 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1968}
1969
1970static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001971 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1972 return 1;
1973 }
1974
Adam Langleybdd5d662015-07-20 16:19:08 -07001975 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1976 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001977 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001978
1979 if (!using_ecc) {
1980 return 1;
1981 }
1982
1983 return ext_ec_point_add_extension(ssl, out);
1984}
1985
Adam Langley273d49c2015-07-20 16:38:52 -07001986
Steven Valdez143e8b32016-07-11 13:19:03 -04001987/* Draft Version Extension */
1988
1989static int ext_draft_version_add_clienthello(SSL *ssl, CBB *out) {
1990 uint16_t min_version, max_version;
1991 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
Steven Valdez3a287552016-07-19 05:10:50 -04001992 max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001993 return 1;
1994 }
1995
1996 CBB contents;
1997 if (!CBB_add_u16(out, TLSEXT_TYPE_draft_version) ||
1998 !CBB_add_u16_length_prefixed(out, &contents) ||
1999 !CBB_add_u16(&contents, TLS1_3_DRAFT_VERSION)) {
2000 return 0;
2001 }
2002
2003 return CBB_flush(out);
2004}
2005
2006
Steven Valdez4aa154e2016-07-29 14:32:55 -04002007/* Pre Shared Key
2008 *
2009 * https://tools.ietf.org/html/draft-ietf-tls-tls13-14 */
2010
2011static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
2012 uint16_t min_version, max_version;
2013 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2014 return 0;
2015 }
2016
2017 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
2018 ssl->method->version_from_wire(ssl->session->ssl_version) <
2019 TLS1_3_VERSION) {
2020 return 1;
2021 }
2022
2023 CBB contents, identities, identity;
2024 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2025 !CBB_add_u16_length_prefixed(out, &contents) ||
2026 !CBB_add_u16_length_prefixed(&contents, &identities) ||
2027 !CBB_add_u16_length_prefixed(&identities, &identity) ||
2028 !CBB_add_bytes(&identity, ssl->session->tlsext_tick,
2029 ssl->session->tlsext_ticklen)) {
2030 return 0;
2031 }
2032
2033 return CBB_flush(out);
2034}
2035
2036int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2037 CBS *contents) {
2038 uint16_t psk_id;
2039 if (!CBS_get_u16(contents, &psk_id) ||
2040 CBS_len(contents) != 0) {
2041 *out_alert = SSL_AD_DECODE_ERROR;
2042 return 0;
2043 }
2044
2045 if (psk_id != 0) {
2046 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2047 return 0;
2048 }
2049
2050 return 1;
2051}
2052
2053int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
2054 SSL_SESSION **out_session,
2055 uint8_t *out_alert,
2056 CBS *contents) {
2057 CBS identities, identity;
2058 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2059 !CBS_get_u16_length_prefixed(&identities, &identity) ||
2060 CBS_len(contents) != 0) {
2061 *out_alert = SSL_AD_DECODE_ERROR;
2062 return 0;
2063 }
2064
2065 /* TLS 1.3 session tickets are renewed separately as part of the
2066 * NewSessionTicket. */
2067 int renew;
2068 return tls_process_ticket(ssl, out_session, &renew, CBS_data(&identity),
2069 CBS_len(&identity), NULL, 0);
2070}
2071
2072int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
2073 if (!ssl->s3->session_reused) {
2074 return 1;
2075 }
2076
2077 CBB contents;
2078 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2079 !CBB_add_u16_length_prefixed(out, &contents) ||
2080 /* We only consider the first identity for resumption */
2081 !CBB_add_u16(&contents, 0) ||
2082 !CBB_flush(out)) {
2083 return 0;
2084 }
2085
2086 return 1;
2087}
2088
2089
Steven Valdez143e8b32016-07-11 13:19:03 -04002090/* Key Share
2091 *
2092 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
2093
2094static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
2095 uint16_t min_version, max_version;
2096 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2097 return 0;
2098 }
2099
2100 if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
2101 return 1;
2102 }
2103
2104 CBB contents, kse_bytes;
2105 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2106 !CBB_add_u16_length_prefixed(out, &contents) ||
2107 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2108 return 0;
2109 }
2110
2111 const uint16_t *groups;
2112 size_t groups_len;
Steven Valdez5440fe02016-07-18 12:40:30 -04002113 if (ssl->s3->hs->retry_group) {
2114 /* Append the new key share to the old list. */
2115 if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
2116 ssl->s3->hs->key_share_bytes_len)) {
2117 return 0;
2118 }
2119 OPENSSL_free(ssl->s3->hs->key_share_bytes);
2120 ssl->s3->hs->key_share_bytes = NULL;
2121
2122 groups = &ssl->s3->hs->retry_group;
2123 groups_len = 1;
2124 } else {
2125 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
2126 /* Only send the top two preferred key shares. */
2127 if (groups_len > 2) {
2128 groups_len = 2;
2129 }
2130 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002131
2132 ssl->s3->hs->groups = OPENSSL_malloc(groups_len * sizeof(SSL_ECDH_CTX));
2133 if (ssl->s3->hs->groups == NULL) {
2134 return 0;
2135 }
2136 memset(ssl->s3->hs->groups, 0, groups_len * sizeof(SSL_ECDH_CTX));
2137 ssl->s3->hs->groups_len = groups_len;
2138
2139 for (size_t i = 0; i < groups_len; i++) {
2140 if (!CBB_add_u16(&kse_bytes, groups[i])) {
2141 return 0;
2142 }
2143
2144 CBB key_exchange;
2145 if (!CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2146 !SSL_ECDH_CTX_init(&ssl->s3->hs->groups[i], groups[i]) ||
2147 !SSL_ECDH_CTX_offer(&ssl->s3->hs->groups[i], &key_exchange) ||
2148 !CBB_flush(&kse_bytes)) {
2149 return 0;
2150 }
2151 }
2152
Steven Valdez5440fe02016-07-18 12:40:30 -04002153 if (!ssl->s3->hs->retry_group) {
2154 /* Save the contents of the extension to repeat it in the second
2155 * ClientHello. */
2156 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2157 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2158 CBB_len(&kse_bytes));
2159 if (ssl->s3->hs->key_share_bytes == NULL) {
2160 return 0;
2161 }
2162 }
2163
Steven Valdez143e8b32016-07-11 13:19:03 -04002164 return CBB_flush(out);
2165}
2166
Steven Valdez7259f2f2016-08-02 16:55:05 -04002167int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2168 size_t *out_secret_len,
2169 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002170 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002171 uint16_t group_id;
2172 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002173 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2174 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002175 *out_alert = SSL_AD_DECODE_ERROR;
2176 return 0;
2177 }
2178
2179 SSL_ECDH_CTX *group_ctx = NULL;
2180 for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) {
David Benjamin5c4e8572016-08-19 17:44:53 -04002181 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->groups[i]) == group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002182 group_ctx = &ssl->s3->hs->groups[i];
2183 break;
2184 }
2185 }
2186
2187 if (group_ctx == NULL) {
2188 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2189 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2190 return 0;
2191 }
2192
2193 if (!SSL_ECDH_CTX_finish(group_ctx, out_secret, out_secret_len, out_alert,
2194 CBS_data(&peer_key), CBS_len(&peer_key))) {
2195 *out_alert = SSL_AD_INTERNAL_ERROR;
2196 return 0;
2197 }
2198
David Benjamin5c4e8572016-08-19 17:44:53 -04002199 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez5440fe02016-07-18 12:40:30 -04002200 ssl_handshake_clear_groups(ssl->s3->hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04002201 return 1;
2202}
2203
Steven Valdez7259f2f2016-08-02 16:55:05 -04002204int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2205 uint8_t **out_secret,
2206 size_t *out_secret_len,
2207 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002208 uint16_t group_id;
2209 CBS key_shares;
2210 if (!tls1_get_shared_group(ssl, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002211 !CBS_get_u16_length_prefixed(contents, &key_shares) ||
2212 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002213 return 0;
2214 }
2215
Steven Valdez5440fe02016-07-18 12:40:30 -04002216 *out_found = 0;
Steven Valdez143e8b32016-07-11 13:19:03 -04002217 while (CBS_len(&key_shares) > 0) {
2218 uint16_t id;
2219 CBS peer_key;
2220 if (!CBS_get_u16(&key_shares, &id) ||
2221 !CBS_get_u16_length_prefixed(&key_shares, &peer_key)) {
2222 return 0;
2223 }
2224
Steven Valdez5440fe02016-07-18 12:40:30 -04002225 if (id != group_id || *out_found) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002226 continue;
2227 }
2228
2229 SSL_ECDH_CTX group;
2230 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2231 CBB public_key;
2232 if (!CBB_init(&public_key, 0) ||
2233 !SSL_ECDH_CTX_init(&group, group_id) ||
2234 !SSL_ECDH_CTX_accept(&group, &public_key, out_secret, out_secret_len,
2235 out_alert, CBS_data(&peer_key),
2236 CBS_len(&peer_key)) ||
2237 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2238 &ssl->s3->hs->public_key_len)) {
2239 SSL_ECDH_CTX_cleanup(&group);
2240 CBB_cleanup(&public_key);
2241 return 0;
2242 }
2243 SSL_ECDH_CTX_cleanup(&group);
2244
Steven Valdez5440fe02016-07-18 12:40:30 -04002245 *out_found = 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002246 }
2247
Steven Valdez5440fe02016-07-18 12:40:30 -04002248 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002249}
2250
Steven Valdez7259f2f2016-08-02 16:55:05 -04002251int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002252 if (ssl->s3->tmp.new_cipher->algorithm_mkey != SSL_kECDHE) {
2253 return 1;
2254 }
2255
2256 uint16_t group_id;
2257 CBB kse_bytes, public_key;
2258 if (!tls1_get_shared_group(ssl, &group_id) ||
2259 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2260 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2261 !CBB_add_u16(&kse_bytes, group_id) ||
2262 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2263 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2264 ssl->s3->hs->public_key_len) ||
2265 !CBB_flush(out)) {
2266 return 0;
2267 }
2268
David Benjamin5c4e8572016-08-19 17:44:53 -04002269 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002270 return 1;
2271}
2272
2273
Steven Valdezce902a92016-05-17 11:47:53 -04002274/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002275 *
Steven Valdezce902a92016-05-17 11:47:53 -04002276 * https://tools.ietf.org/html/rfc4492#section-5.1.2
2277 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07002278
Steven Valdezce902a92016-05-17 11:47:53 -04002279static void ext_supported_groups_init(SSL *ssl) {
2280 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2281 ssl->s3->tmp.peer_supported_group_list = NULL;
2282 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07002283}
2284
Steven Valdezce902a92016-05-17 11:47:53 -04002285static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002286 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2287 return 1;
2288 }
2289
Steven Valdezce902a92016-05-17 11:47:53 -04002290 CBB contents, groups_bytes;
2291 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002292 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002293 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002294 return 0;
2295 }
2296
Steven Valdezce902a92016-05-17 11:47:53 -04002297 const uint16_t *groups;
2298 size_t groups_len;
2299 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002300
2301 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002302 for (i = 0; i < groups_len; i++) {
2303 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002304 return 0;
2305 }
2306 }
2307
2308 return CBB_flush(out);
2309}
2310
Steven Valdezce902a92016-05-17 11:47:53 -04002311static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2312 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002313 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2314 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002315 return 1;
2316}
2317
Steven Valdezce902a92016-05-17 11:47:53 -04002318static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2319 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002320 if (contents == NULL) {
2321 return 1;
2322 }
2323
Steven Valdezce902a92016-05-17 11:47:53 -04002324 CBS supported_group_list;
2325 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2326 CBS_len(&supported_group_list) == 0 ||
2327 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002328 CBS_len(contents) != 0) {
2329 return 0;
2330 }
2331
Steven Valdezce902a92016-05-17 11:47:53 -04002332 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
2333 CBS_len(&supported_group_list));
2334 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002335 *out_alert = SSL_AD_INTERNAL_ERROR;
2336 return 0;
2337 }
2338
Steven Valdezce902a92016-05-17 11:47:53 -04002339 const size_t num_groups = CBS_len(&supported_group_list) / 2;
Adam Langley273d49c2015-07-20 16:38:52 -07002340 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002341 for (i = 0; i < num_groups; i++) {
2342 if (!CBS_get_u16(&supported_group_list,
2343 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002344 goto err;
2345 }
2346 }
2347
Steven Valdezce902a92016-05-17 11:47:53 -04002348 assert(CBS_len(&supported_group_list) == 0);
2349 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002350
2351 return 1;
2352
2353err:
Steven Valdezce902a92016-05-17 11:47:53 -04002354 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2355 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002356 *out_alert = SSL_AD_INTERNAL_ERROR;
2357 return 0;
2358}
2359
Steven Valdezce902a92016-05-17 11:47:53 -04002360static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002361 /* Servers don't echo this extension. */
2362 return 1;
2363}
2364
2365
Adam Langley614c66a2015-06-12 15:26:58 -07002366/* kExtensions contains all the supported extensions. */
2367static const struct tls_extension kExtensions[] = {
2368 {
Adam Langley5021b222015-06-12 18:27:58 -07002369 TLSEXT_TYPE_renegotiate,
2370 NULL,
2371 ext_ri_add_clienthello,
2372 ext_ri_parse_serverhello,
2373 ext_ri_parse_clienthello,
2374 ext_ri_add_serverhello,
2375 },
2376 {
Adam Langley614c66a2015-06-12 15:26:58 -07002377 TLSEXT_TYPE_server_name,
2378 ext_sni_init,
2379 ext_sni_add_clienthello,
2380 ext_sni_parse_serverhello,
2381 ext_sni_parse_clienthello,
2382 ext_sni_add_serverhello,
2383 },
Adam Langley0a056712015-07-01 15:03:33 -07002384 {
2385 TLSEXT_TYPE_extended_master_secret,
2386 ext_ems_init,
2387 ext_ems_add_clienthello,
2388 ext_ems_parse_serverhello,
2389 ext_ems_parse_clienthello,
2390 ext_ems_add_serverhello,
2391 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002392 {
2393 TLSEXT_TYPE_session_ticket,
2394 NULL,
2395 ext_ticket_add_clienthello,
2396 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002397 /* Ticket extension client parsing is handled in ssl_session.c */
2398 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002399 ext_ticket_add_serverhello,
2400 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002401 {
2402 TLSEXT_TYPE_signature_algorithms,
2403 NULL,
2404 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002405 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002406 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002407 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002408 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002409 {
2410 TLSEXT_TYPE_status_request,
2411 ext_ocsp_init,
2412 ext_ocsp_add_clienthello,
2413 ext_ocsp_parse_serverhello,
2414 ext_ocsp_parse_clienthello,
2415 ext_ocsp_add_serverhello,
2416 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002417 {
2418 TLSEXT_TYPE_next_proto_neg,
2419 ext_npn_init,
2420 ext_npn_add_clienthello,
2421 ext_npn_parse_serverhello,
2422 ext_npn_parse_clienthello,
2423 ext_npn_add_serverhello,
2424 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002425 {
2426 TLSEXT_TYPE_certificate_timestamp,
2427 NULL,
2428 ext_sct_add_clienthello,
2429 ext_sct_parse_serverhello,
2430 ext_sct_parse_clienthello,
2431 ext_sct_add_serverhello,
2432 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002433 {
2434 TLSEXT_TYPE_application_layer_protocol_negotiation,
2435 ext_alpn_init,
2436 ext_alpn_add_clienthello,
2437 ext_alpn_parse_serverhello,
2438 ext_alpn_parse_clienthello,
2439 ext_alpn_add_serverhello,
2440 },
Adam Langley49c7af12015-07-10 14:33:46 -07002441 {
2442 TLSEXT_TYPE_channel_id,
2443 ext_channel_id_init,
2444 ext_channel_id_add_clienthello,
2445 ext_channel_id_parse_serverhello,
2446 ext_channel_id_parse_clienthello,
2447 ext_channel_id_add_serverhello,
2448 },
Adam Langley391250d2015-07-15 19:06:07 -07002449 {
2450 TLSEXT_TYPE_srtp,
2451 ext_srtp_init,
2452 ext_srtp_add_clienthello,
2453 ext_srtp_parse_serverhello,
2454 ext_srtp_parse_clienthello,
2455 ext_srtp_add_serverhello,
2456 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002457 {
2458 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002459 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002460 ext_ec_point_add_clienthello,
2461 ext_ec_point_parse_serverhello,
2462 ext_ec_point_parse_clienthello,
2463 ext_ec_point_add_serverhello,
2464 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002465 {
2466 TLSEXT_TYPE_draft_version,
2467 NULL,
2468 ext_draft_version_add_clienthello,
2469 forbid_parse_serverhello,
2470 ignore_parse_clienthello,
2471 dont_add_serverhello,
2472 },
2473 {
2474 TLSEXT_TYPE_key_share,
2475 NULL,
2476 ext_key_share_add_clienthello,
2477 forbid_parse_serverhello,
2478 ignore_parse_clienthello,
2479 dont_add_serverhello,
2480 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002481 {
2482 TLSEXT_TYPE_pre_shared_key,
2483 NULL,
2484 ext_pre_shared_key_add_clienthello,
2485 forbid_parse_serverhello,
2486 ignore_parse_clienthello,
2487 dont_add_serverhello,
2488 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002489 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2490 * intolerant to the last extension being zero-length. See
2491 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002492 {
Steven Valdezce902a92016-05-17 11:47:53 -04002493 TLSEXT_TYPE_supported_groups,
2494 ext_supported_groups_init,
2495 ext_supported_groups_add_clienthello,
2496 ext_supported_groups_parse_serverhello,
2497 ext_supported_groups_parse_clienthello,
2498 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002499 },
Adam Langley614c66a2015-06-12 15:26:58 -07002500};
2501
2502#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2503
Adam Langley4cfa96b2015-07-01 11:56:55 -07002504OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2505 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002506 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002507OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2508 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2509 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002510 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002511
Adam Langley614c66a2015-06-12 15:26:58 -07002512static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2513 uint16_t value) {
2514 unsigned i;
2515 for (i = 0; i < kNumExtensions; i++) {
2516 if (kExtensions[i].value == value) {
2517 *out_index = i;
2518 return &kExtensions[i];
2519 }
2520 }
2521
2522 return NULL;
2523}
2524
Adam Langley09505632015-07-30 18:10:13 -07002525int SSL_extension_supported(unsigned extension_value) {
2526 uint32_t index;
2527 return extension_value == TLSEXT_TYPE_padding ||
2528 tls_extension_find(&index, extension_value) != NULL;
2529}
2530
David Benjamine8d53502015-10-10 14:13:23 -04002531int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002532 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002533 if (ssl->client_version == SSL3_VERSION &&
2534 !ssl->s3->send_connection_binding) {
2535 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002536 }
Adam Langley95c29f32014-06-20 12:00:00 -07002537
David Benjamine8d53502015-10-10 14:13:23 -04002538 CBB extensions;
2539 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002540 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002541 }
Adam Langley95c29f32014-06-20 12:00:00 -07002542
David Benjamine8d53502015-10-10 14:13:23 -04002543 ssl->s3->tmp.extensions.sent = 0;
2544 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002545
Adam Langley614c66a2015-06-12 15:26:58 -07002546 size_t i;
2547 for (i = 0; i < kNumExtensions; i++) {
2548 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002549 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002550 }
2551 }
Adam Langley95c29f32014-06-20 12:00:00 -07002552
Adam Langley614c66a2015-06-12 15:26:58 -07002553 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002554 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002555 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002556 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2557 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2558 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002559 }
Adam Langley95c29f32014-06-20 12:00:00 -07002560
Adam Langley33ad2b52015-07-20 17:43:53 -07002561 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002562 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002563 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002564 }
Adam Langley75712922014-10-10 16:23:43 -07002565
David Benjamine8d53502015-10-10 14:13:23 -04002566 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002567 goto err;
2568 }
2569
David Benjamince079fd2016-08-02 16:22:34 -04002570 if (!SSL_is_dtls(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002571 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002572 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002573 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002574 *
2575 * NB: because this code works out the length of all existing extensions
2576 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002577 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002578 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002579 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002580 * Server 7.0 is intolerant to the last extension being zero-length. See
2581 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002582 if (padding_len >= 4 + 1) {
2583 padding_len -= 4;
2584 } else {
2585 padding_len = 1;
2586 }
Adam Langley95c29f32014-06-20 12:00:00 -07002587
Adam Langley33ad2b52015-07-20 17:43:53 -07002588 uint8_t *padding_bytes;
2589 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2590 !CBB_add_u16(&extensions, padding_len) ||
2591 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2592 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002593 }
Adam Langley75712922014-10-10 16:23:43 -07002594
Adam Langley33ad2b52015-07-20 17:43:53 -07002595 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002596 }
2597 }
Adam Langley75712922014-10-10 16:23:43 -07002598
David Benjamina01deee2015-12-08 18:56:31 -05002599 /* Discard empty extensions blocks. */
2600 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002601 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002602 }
2603
David Benjamine8d53502015-10-10 14:13:23 -04002604 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002605
2606err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002607 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002608 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002609}
Adam Langley95c29f32014-06-20 12:00:00 -07002610
David Benjamin56380462015-10-10 14:59:09 -04002611int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002612 CBB extensions;
2613 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002614 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002615 }
2616
2617 unsigned i;
2618 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002619 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002620 /* Don't send extensions that were not received. */
2621 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002622 }
Adam Langley95c29f32014-06-20 12:00:00 -07002623
David Benjamin56380462015-10-10 14:59:09 -04002624 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002625 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2626 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2627 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002628 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002629 }
Adam Langley95c29f32014-06-20 12:00:00 -07002630
David Benjamin56380462015-10-10 14:59:09 -04002631 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002632 goto err;
2633 }
2634
Steven Valdez143e8b32016-07-11 13:19:03 -04002635 /* Discard empty extensions blocks before TLS 1.3. */
2636 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2637 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002638 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002639 }
2640
David Benjamin56380462015-10-10 14:59:09 -04002641 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002642
2643err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002644 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002645 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002646}
Adam Langley95c29f32014-06-20 12:00:00 -07002647
David Benjamine14ff062016-08-09 16:21:24 -04002648static int ssl_scan_clienthello_tlsext(
2649 SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
2650 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002651 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002652 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002653 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002654 }
2655 }
2656
David Benjamin0d56f882015-12-19 17:05:56 -05002657 ssl->s3->tmp.extensions.received = 0;
2658 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002659
David Benjamine14ff062016-08-09 16:21:24 -04002660 CBS extensions;
2661 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2662 while (CBS_len(&extensions) != 0) {
2663 uint16_t type;
2664 CBS extension;
2665
2666 /* Decode the next extension. */
2667 if (!CBS_get_u16(&extensions, &type) ||
2668 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002669 *out_alert = SSL_AD_DECODE_ERROR;
2670 return 0;
2671 }
Adam Langley95c29f32014-06-20 12:00:00 -07002672
David Benjamine14ff062016-08-09 16:21:24 -04002673 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2674 * ambiguous. Ignore all but the renegotiation_info extension. */
2675 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2676 continue;
2677 }
Adam Langley95c29f32014-06-20 12:00:00 -07002678
David Benjamine14ff062016-08-09 16:21:24 -04002679 unsigned ext_index;
2680 const struct tls_extension *const ext =
2681 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002682
David Benjamine14ff062016-08-09 16:21:24 -04002683 if (ext == NULL) {
2684 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002685 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002686 return 0;
2687 }
David Benjamine14ff062016-08-09 16:21:24 -04002688 continue;
2689 }
2690
2691 ssl->s3->tmp.extensions.received |= (1u << ext_index);
2692 uint8_t alert = SSL_AD_DECODE_ERROR;
2693 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2694 *out_alert = alert;
2695 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2696 ERR_add_error_dataf("extension: %u", (unsigned)type);
2697 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002698 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002699 }
Adam Langley75712922014-10-10 16:23:43 -07002700
David Benjamin1deb41b2016-08-09 19:36:38 -04002701 for (size_t i = 0; i < kNumExtensions; i++) {
2702 if (ssl->s3->tmp.extensions.received & (1u << i)) {
2703 continue;
2704 }
2705
2706 CBS *contents = NULL, fake_contents;
2707 static const uint8_t kFakeRenegotiateExtension[] = {0};
2708 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2709 ssl_client_cipher_list_contains_cipher(client_hello,
2710 SSL3_CK_SCSV & 0xffff)) {
2711 /* The renegotiation SCSV was received so pretend that we received a
2712 * renegotiation extension. */
2713 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2714 sizeof(kFakeRenegotiateExtension));
2715 contents = &fake_contents;
2716 ssl->s3->tmp.extensions.received |= (1u << i);
2717 }
2718
2719 /* Extension wasn't observed so call the callback with a NULL
2720 * parameter. */
2721 uint8_t alert = SSL_AD_DECODE_ERROR;
2722 if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
2723 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2724 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2725 *out_alert = alert;
2726 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002727 }
2728 }
2729
Adam Langleyfcf25832014-12-18 17:42:32 -08002730 return 1;
2731}
Adam Langley95c29f32014-06-20 12:00:00 -07002732
David Benjamine14ff062016-08-09 16:21:24 -04002733int ssl_parse_clienthello_tlsext(
2734 SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002735 int alert = -1;
David Benjamine14ff062016-08-09 16:21:24 -04002736 if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002737 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002738 return 0;
2739 }
Adam Langley95c29f32014-06-20 12:00:00 -07002740
David Benjamin0d56f882015-12-19 17:05:56 -05002741 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002742 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002743 return 0;
2744 }
Adam Langley95c29f32014-06-20 12:00:00 -07002745
Adam Langleyfcf25832014-12-18 17:42:32 -08002746 return 1;
2747}
Adam Langley95c29f32014-06-20 12:00:00 -07002748
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002749OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2750
David Benjamin0d56f882015-12-19 17:05:56 -05002751static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002752 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2753 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2754 return 1;
2755 }
Adam Langley614c66a2015-06-12 15:26:58 -07002756
Steven Valdez143e8b32016-07-11 13:19:03 -04002757 /* Decode the extensions block and check it is valid. */
2758 CBS extensions;
2759 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2760 !tls1_check_duplicate_extensions(&extensions)) {
2761 *out_alert = SSL_AD_DECODE_ERROR;
2762 return 0;
2763 }
2764
2765 uint32_t received = 0;
2766 while (CBS_len(&extensions) != 0) {
2767 uint16_t type;
2768 CBS extension;
2769
2770 /* Decode the next extension. */
2771 if (!CBS_get_u16(&extensions, &type) ||
2772 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002773 *out_alert = SSL_AD_DECODE_ERROR;
2774 return 0;
2775 }
Adam Langley95c29f32014-06-20 12:00:00 -07002776
Steven Valdez143e8b32016-07-11 13:19:03 -04002777 unsigned ext_index;
2778 const struct tls_extension *const ext =
2779 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002780
Steven Valdez143e8b32016-07-11 13:19:03 -04002781 if (ext == NULL) {
2782 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002783 return 0;
2784 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002785 continue;
2786 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002787
David Benjamin1deb41b2016-08-09 19:36:38 -04002788 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index)) &&
2789 type != TLSEXT_TYPE_renegotiate) {
2790 /* If the extension was never sent then it is illegal, except for the
2791 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002792 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2793 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002794 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002795 return 0;
2796 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002797
Steven Valdez143e8b32016-07-11 13:19:03 -04002798 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002799
Steven Valdez143e8b32016-07-11 13:19:03 -04002800 uint8_t alert = SSL_AD_DECODE_ERROR;
2801 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2802 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2803 ERR_add_error_dataf("extension: %u", (unsigned)type);
2804 *out_alert = alert;
2805 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002806 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002807 }
Adam Langley95c29f32014-06-20 12:00:00 -07002808
Adam Langley33ad2b52015-07-20 17:43:53 -07002809 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002810 for (i = 0; i < kNumExtensions; i++) {
2811 if (!(received & (1u << i))) {
2812 /* Extension wasn't observed so call the callback with a NULL
2813 * parameter. */
2814 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002815 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002816 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2817 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002818 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002819 return 0;
2820 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002821 }
2822 }
Adam Langley95c29f32014-06-20 12:00:00 -07002823
Adam Langleyfcf25832014-12-18 17:42:32 -08002824 return 1;
2825}
Adam Langley95c29f32014-06-20 12:00:00 -07002826
David Benjamin0d56f882015-12-19 17:05:56 -05002827static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002828 int ret = SSL_TLSEXT_ERR_NOACK;
2829 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002830
David Benjamin78f8aab2016-03-10 16:33:58 -05002831 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002832 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002833 ssl->ctx->tlsext_servername_arg);
2834 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002835 ret = ssl->initial_ctx->tlsext_servername_callback(
2836 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002837 }
Adam Langley95c29f32014-06-20 12:00:00 -07002838
Adam Langleyfcf25832014-12-18 17:42:32 -08002839 switch (ret) {
2840 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002841 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002842 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002843
Adam Langleyfcf25832014-12-18 17:42:32 -08002844 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002845 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002846 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002847
Adam Langleyfcf25832014-12-18 17:42:32 -08002848 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002849 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002850 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002851
Adam Langleyfcf25832014-12-18 17:42:32 -08002852 default:
2853 return 1;
2854 }
2855}
Adam Langleyed8270a2014-09-02 13:52:56 -07002856
David Benjamin0d56f882015-12-19 17:05:56 -05002857static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002858 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002859 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002860
David Benjamin78f8aab2016-03-10 16:33:58 -05002861 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002862 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002863 ssl->ctx->tlsext_servername_arg);
2864 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002865 ret = ssl->initial_ctx->tlsext_servername_callback(
2866 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002867 }
Adam Langley95c29f32014-06-20 12:00:00 -07002868
Adam Langleyfcf25832014-12-18 17:42:32 -08002869 switch (ret) {
2870 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002871 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002872 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002873
Adam Langleyfcf25832014-12-18 17:42:32 -08002874 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002875 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002876 return 1;
2877
2878 default:
2879 return 1;
2880 }
2881}
2882
David Benjamin0d56f882015-12-19 17:05:56 -05002883int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002884 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002885 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2886 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002887 return 0;
2888 }
2889
David Benjamin0d56f882015-12-19 17:05:56 -05002890 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002891 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002892 return 0;
2893 }
2894
2895 return 1;
2896}
Adam Langley95c29f32014-06-20 12:00:00 -07002897
David Benjamine3aa1d92015-06-16 15:34:50 -04002898int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002899 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002900 size_t ticket_len, const uint8_t *session_id,
2901 size_t session_id_len) {
2902 int ret = 1; /* Most errors are non-fatal. */
2903 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2904 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002905
David Benjamine3aa1d92015-06-16 15:34:50 -04002906 HMAC_CTX hmac_ctx;
2907 HMAC_CTX_init(&hmac_ctx);
2908 EVP_CIPHER_CTX cipher_ctx;
2909 EVP_CIPHER_CTX_init(&cipher_ctx);
2910
David Benjaminef1b0092015-11-21 14:05:44 -05002911 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002912 *out_session = NULL;
2913
Steven Valdez4aa154e2016-07-29 14:32:55 -04002914 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
2915 goto done;
2916 }
2917
David Benjamine3aa1d92015-06-16 15:34:50 -04002918 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2919 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002920 }
2921
David Benjaminadcc3952015-04-26 13:07:57 -04002922 /* Ensure there is room for the key name and the largest IV
2923 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2924 * the maximum IV length should be well under the minimum size for the
2925 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002926 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2927 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002928 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002929 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002930
David Benjamine3aa1d92015-06-16 15:34:50 -04002931 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002932 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2933 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2934 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002935 if (cb_ret < 0) {
2936 ret = 0;
2937 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002938 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002939 if (cb_ret == 0) {
2940 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002941 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002942 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002943 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002944 }
2945 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002946 /* Check the key name matches. */
2947 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2948 SSL_TICKET_KEY_NAME_LEN) != 0) {
2949 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002950 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002951 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2952 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002953 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002954 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2955 ssl_ctx->tlsext_tick_aes_key, iv)) {
2956 ret = 0;
2957 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002958 }
2959 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002960 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002961
David Benjamine3aa1d92015-06-16 15:34:50 -04002962 /* Check the MAC at the end of the ticket. */
2963 uint8_t mac[EVP_MAX_MD_SIZE];
2964 size_t mac_len = HMAC_size(&hmac_ctx);
2965 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002966 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002967 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002968 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002969 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2970 HMAC_Final(&hmac_ctx, mac, NULL);
2971 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2972 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002973 }
2974
David Benjamine3aa1d92015-06-16 15:34:50 -04002975 /* Decrypt the session data. */
2976 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2977 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2978 mac_len;
2979 plaintext = OPENSSL_malloc(ciphertext_len);
2980 if (plaintext == NULL) {
2981 ret = 0;
2982 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002983 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002984 if (ciphertext_len >= INT_MAX) {
2985 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002986 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002987 int len1, len2;
2988 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2989 (int)ciphertext_len) ||
2990 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2991 ERR_clear_error(); /* Don't leave an error on the queue. */
2992 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002993 }
2994
David Benjamine3aa1d92015-06-16 15:34:50 -04002995 /* Decode the session. */
2996 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2997 if (session == NULL) {
2998 ERR_clear_error(); /* Don't leave an error on the queue. */
2999 goto done;
3000 }
3001
3002 /* Copy the client's session ID into the new session, to denote the ticket has
3003 * been accepted. */
3004 memcpy(session->session_id, session_id, session_id_len);
3005 session->session_id_length = session_id_len;
3006
Steven Valdez4aa154e2016-07-29 14:32:55 -04003007 if (!ssl_session_is_context_valid(ssl, session) ||
3008 !ssl_session_is_time_valid(ssl, session)) {
3009 SSL_SESSION_free(session);
3010 session = NULL;
3011 }
3012
David Benjamine3aa1d92015-06-16 15:34:50 -04003013 *out_session = session;
3014
3015done:
3016 OPENSSL_free(plaintext);
3017 HMAC_CTX_cleanup(&hmac_ctx);
3018 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3019 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003020}
Adam Langley95c29f32014-06-20 12:00:00 -07003021
Steven Valdez0d62f262015-09-04 12:41:04 -04003022int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003023 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05003024 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003025 return 1;
3026 }
David Benjamincd996942014-07-20 16:23:51 -04003027
Steven Valdez0d62f262015-09-04 12:41:04 -04003028 CERT *const cert = ssl->cert;
3029 OPENSSL_free(cert->peer_sigalgs);
3030 cert->peer_sigalgs = NULL;
3031 cert->peer_sigalgslen = 0;
3032
3033 size_t num_sigalgs = CBS_len(in_sigalgs);
3034
3035 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003036 return 0;
3037 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003038 num_sigalgs /= 2;
3039
3040 /* supported_signature_algorithms in the certificate request is
3041 * allowed to be empty. */
3042 if (num_sigalgs == 0) {
3043 return 1;
3044 }
3045
Steven Valdez02563852016-06-23 13:33:05 -04003046 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3047 * and we just divided |num_sigalgs| by two. */
3048 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
Steven Valdez0d62f262015-09-04 12:41:04 -04003049 if (cert->peer_sigalgs == NULL) {
3050 return 0;
3051 }
3052 cert->peer_sigalgslen = num_sigalgs;
3053
3054 CBS sigalgs;
3055 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
3056
3057 size_t i;
3058 for (i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04003059 if (!CBS_get_u16(&sigalgs, &cert->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003060 return 0;
3061 }
3062 }
Adam Langley95c29f32014-06-20 12:00:00 -07003063
Adam Langleyfcf25832014-12-18 17:42:32 -08003064 return 1;
3065}
David Benjaminec2f27d2014-11-13 19:17:25 -05003066
David Benjaminea9a0d52016-07-08 15:52:59 -07003067int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003068 CERT *cert = ssl->cert;
Steven Valdez0d62f262015-09-04 12:41:04 -04003069 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05003070
Steven Valdezf0451ca2016-06-29 13:16:27 -04003071 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3072 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3073 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003074 int type = ssl_private_key_type(ssl);
3075 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003076 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003077 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003078 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003079 if (ssl_is_ecdsa_key_type(type)) {
3080 *out = SSL_SIGN_ECDSA_SHA1;
3081 return 1;
3082 }
3083 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3084 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003085 }
3086
Steven Valdezeff1e8d2016-07-06 14:24:47 -04003087 const uint16_t *sigalgs;
3088 size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07003089 if (cert->sigalgs != NULL) {
3090 sigalgs = cert->sigalgs;
3091 sigalgs_len = cert->sigalgs_len;
Steven Valdez0d62f262015-09-04 12:41:04 -04003092 }
3093
David Benjaminea9a0d52016-07-08 15:52:59 -07003094 const uint16_t *peer_sigalgs = cert->peer_sigalgs;
3095 size_t peer_sigalgs_len = cert->peer_sigalgslen;
David Benjamin51dd7d62016-07-08 16:07:01 -07003096 if (peer_sigalgs_len == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003097 /* If the client didn't specify any signature_algorithms extension then
3098 * we can assume that it supports SHA1. See
3099 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3100 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3101 SSL_SIGN_ECDSA_SHA1};
3102 peer_sigalgs = kDefaultPeerAlgorithms;
Steven Valdezcb966542016-08-17 16:56:14 -04003103 peer_sigalgs_len = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003104 }
3105
David Benjamind246b812016-07-08 15:07:02 -07003106 for (i = 0; i < sigalgs_len; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003107 uint16_t sigalg = sigalgs[i];
3108 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3109 * negotiated. */
3110 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3111 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3112 continue;
3113 }
3114
David Benjaminea9a0d52016-07-08 15:52:59 -07003115 for (j = 0; j < peer_sigalgs_len; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003116 if (sigalg == peer_sigalgs[j]) {
3117 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003118 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003119 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003120 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003121 }
Adam Langley95c29f32014-06-20 12:00:00 -07003122
David Benjaminea9a0d52016-07-08 15:52:59 -07003123 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3124 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003125}
Adam Langley95c29f32014-06-20 12:00:00 -07003126
David Benjamind6a4ae92015-08-06 11:10:51 -04003127int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
3128 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003129 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003130
3131 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003132 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
3133 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08003134 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003135
David Benjamind6a4ae92015-08-06 11:10:51 -04003136 static const char kClientIDMagic[] = "TLS Channel ID signature";
3137 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
3138
Steven Valdez87eab492016-06-27 16:34:59 -04003139 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003140 static const char kResumptionMagic[] = "Resumption";
3141 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
3142 if (ssl->session->original_handshake_hash_len == 0) {
3143 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3144 goto err;
3145 }
3146 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
3147 ssl->session->original_handshake_hash_len);
3148 }
3149
3150 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3151 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3152 sizeof(handshake_hash));
3153 if (handshake_hash_len < 0) {
3154 goto err;
3155 }
3156 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
3157 unsigned len_u;
3158 EVP_DigestFinal_ex(&ctx, out, &len_u);
3159 *out_len = len_u;
3160
3161 ret = 1;
3162
3163err:
3164 EVP_MD_CTX_cleanup(&ctx);
3165 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003166}
Adam Langley1258b6a2014-06-20 12:00:00 -07003167
3168/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003169 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003170 * data. */
3171int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003172 int digest_len;
3173 /* This function should never be called for a resumed session because the
3174 * handshake hashes that we wish to record are for the original, full
3175 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003176 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003177 return -1;
3178 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003179
Adam Langleyfcf25832014-12-18 17:42:32 -08003180 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003181 tls1_handshake_digest(
3182 ssl, ssl->s3->new_session->original_handshake_hash,
3183 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003184 if (digest_len < 0) {
3185 return -1;
3186 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003187
Steven Valdez87eab492016-06-27 16:34:59 -04003188 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003189
Adam Langleyfcf25832014-12-18 17:42:32 -08003190 return 1;
3191}