blob: 0b0840082e1ab542709411b9c10d951101c36b3b [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;
Adam Langleyfcf25832014-12-18 17:42:32 -0800597 int have_rsa = 0, have_ecdsa = 0;
598 c->mask_a = 0;
599 c->mask_k = 0;
600
Adam Langleyfcf25832014-12-18 17:42:32 -0800601 /* Now go through all signature algorithms seeing if we support any for RSA,
602 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin0fc37ef2016-08-17 15:29:46 -0400603 const uint16_t *sigalgs;
604 size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
605 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -0400606 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400607 case SSL_SIGN_RSA_PSS_SHA512:
608 case SSL_SIGN_RSA_PSS_SHA384:
609 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400610 case SSL_SIGN_RSA_PKCS1_SHA512:
611 case SSL_SIGN_RSA_PKCS1_SHA384:
612 case SSL_SIGN_RSA_PKCS1_SHA256:
613 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800614 have_rsa = 1;
615 break;
616
Steven Valdez02563852016-06-23 13:33:05 -0400617 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
618 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
619 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
620 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800621 have_ecdsa = 1;
622 break;
623 }
624 }
625
626 /* Disable auth if we don't include any appropriate signature algorithms. */
627 if (!have_rsa) {
628 c->mask_a |= SSL_aRSA;
629 }
630 if (!have_ecdsa) {
631 c->mask_a |= SSL_aECDSA;
632 }
633
634 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500635 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 c->mask_a |= SSL_aPSK;
637 c->mask_k |= SSL_kPSK;
638 }
639}
Adam Langley95c29f32014-06-20 12:00:00 -0700640
Adam Langley614c66a2015-06-12 15:26:58 -0700641/* tls_extension represents a TLS extension that is handled internally. The
642 * |init| function is called for each handshake, before any other functions of
643 * the extension. Then the add and parse callbacks are called as needed.
644 *
645 * The parse callbacks receive a |CBS| that contains the contents of the
646 * extension (i.e. not including the type and length bytes). If an extension is
647 * not received then the parse callbacks will be called with a NULL CBS so that
648 * they can do any processing needed to handle the absence of an extension.
649 *
650 * The add callbacks receive a |CBB| to which the extension can be appended but
651 * the function is responsible for appending the type and length bytes too.
652 *
653 * All callbacks return one for success and zero for error. If a parse function
654 * returns zero then a fatal alert with value |*out_alert| will be sent. If
655 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
656struct tls_extension {
657 uint16_t value;
658 void (*init)(SSL *ssl);
659
660 int (*add_clienthello)(SSL *ssl, CBB *out);
661 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
662
663 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
664 int (*add_serverhello)(SSL *ssl, CBB *out);
665};
666
Steven Valdez6b8509a2016-07-12 13:38:32 -0400667static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
668 if (contents != NULL) {
669 /* Servers MUST NOT send this extension. */
670 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
671 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
672 return 0;
673 }
674
675 return 1;
676}
677
678static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
679 /* This extension from the client is handled elsewhere. */
680 return 1;
681}
682
683static int dont_add_serverhello(SSL *ssl, CBB *out) {
684 return 1;
685}
Adam Langley614c66a2015-06-12 15:26:58 -0700686
687/* Server name indication (SNI).
688 *
689 * https://tools.ietf.org/html/rfc6066#section-3. */
690
691static void ext_sni_init(SSL *ssl) {
692 ssl->s3->tmp.should_ack_sni = 0;
693}
694
695static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
696 if (ssl->tlsext_hostname == NULL) {
697 return 1;
698 }
699
700 CBB contents, server_name_list, name;
701 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
702 !CBB_add_u16_length_prefixed(out, &contents) ||
703 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
704 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
705 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
706 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
707 strlen(ssl->tlsext_hostname)) ||
708 !CBB_flush(out)) {
709 return 0;
710 }
711
712 return 1;
713}
714
David Benjamin0d56f882015-12-19 17:05:56 -0500715static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
716 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700717 if (contents == NULL) {
718 return 1;
719 }
720
721 if (CBS_len(contents) != 0) {
722 return 0;
723 }
724
725 assert(ssl->tlsext_hostname != NULL);
726
Steven Valdez87eab492016-06-27 16:34:59 -0400727 if (ssl->session == NULL) {
728 assert(ssl->s3->new_session->tlsext_hostname == NULL);
729 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
730 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700731 *out_alert = SSL_AD_INTERNAL_ERROR;
732 return 0;
733 }
734 }
735
736 return 1;
737}
738
David Benjamin0d56f882015-12-19 17:05:56 -0500739static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
740 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700741 if (contents == NULL) {
742 return 1;
743 }
744
David Benjamin9b611e22016-03-03 08:48:30 -0500745 CBS server_name_list, host_name;
746 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700747 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500748 !CBS_get_u8(&server_name_list, &name_type) ||
749 /* Although the server_name extension was intended to be extensible to
750 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
751 * different name types will cause an error. Further, RFC 4366 originally
752 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
753 * adding new name types is no longer feasible.
754 *
755 * Act as if the extensibility does not exist to simplify parsing. */
756 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
757 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700758 CBS_len(contents) != 0) {
759 return 0;
760 }
761
David Benjamin9b611e22016-03-03 08:48:30 -0500762 if (name_type != TLSEXT_NAMETYPE_host_name ||
763 CBS_len(&host_name) == 0 ||
764 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
765 CBS_contains_zero_byte(&host_name)) {
766 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
767 return 0;
768 }
Adam Langley614c66a2015-06-12 15:26:58 -0700769
David Benjamin9b611e22016-03-03 08:48:30 -0500770 /* TODO(davidben): SNI should be resolved before resumption. We have the
771 * early callback as a replacement, but we should fix the current callback
772 * and avoid the need for |SSL_CTX_set_session_id_context|. */
Steven Valdez87eab492016-06-27 16:34:59 -0400773 if (ssl->session == NULL) {
774 assert(ssl->s3->new_session->tlsext_hostname == NULL);
David Benjamin9b611e22016-03-03 08:48:30 -0500775
776 /* Copy the hostname as a string. */
Steven Valdez87eab492016-06-27 16:34:59 -0400777 if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) {
David Benjamin9b611e22016-03-03 08:48:30 -0500778 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700779 return 0;
780 }
781
David Benjamin9b611e22016-03-03 08:48:30 -0500782 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700783 }
784
785 return 1;
786}
787
788static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez87eab492016-06-27 16:34:59 -0400789 if (ssl->session != NULL ||
Adam Langley614c66a2015-06-12 15:26:58 -0700790 !ssl->s3->tmp.should_ack_sni ||
Steven Valdez87eab492016-06-27 16:34:59 -0400791 ssl->s3->new_session->tlsext_hostname == NULL) {
Adam Langley614c66a2015-06-12 15:26:58 -0700792 return 1;
793 }
794
795 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
796 !CBB_add_u16(out, 0 /* length */)) {
797 return 0;
798 }
799
800 return 1;
801}
802
803
Adam Langley5021b222015-06-12 18:27:58 -0700804/* Renegotiation indication.
805 *
806 * https://tools.ietf.org/html/rfc5746 */
807
808static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400809 uint16_t min_version, max_version;
810 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
811 return 0;
812 }
813
814 /* Renegotiation indication is not necessary in TLS 1.3. */
815 if (min_version >= TLS1_3_VERSION) {
816 return 1;
817 }
818
Adam Langley5021b222015-06-12 18:27:58 -0700819 CBB contents, prev_finished;
820 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
821 !CBB_add_u16_length_prefixed(out, &contents) ||
822 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
823 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
824 ssl->s3->previous_client_finished_len) ||
825 !CBB_flush(out)) {
826 return 0;
827 }
828
829 return 1;
830}
831
832static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
833 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400834 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
835 return 0;
836 }
837
David Benjamin3e052de2015-11-25 20:10:31 -0500838 /* Servers may not switch between omitting the extension and supporting it.
839 * See RFC 5746, sections 3.5 and 4.2. */
840 if (ssl->s3->initial_handshake_complete &&
841 (contents != NULL) != ssl->s3->send_connection_binding) {
842 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
843 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
844 return 0;
845 }
846
Adam Langley5021b222015-06-12 18:27:58 -0700847 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500848 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700849 * RI even on initial ServerHello because the client doesn't see any
850 * renegotiation during an attack. However this would mean we could not
851 * connect to any server which doesn't support RI.
852 *
David Benjamine9cddb82015-11-23 14:36:40 -0500853 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
854 * practical terms every client sets it so it's just assumed here. */
855 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700856 }
857
858 const size_t expected_len = ssl->s3->previous_client_finished_len +
859 ssl->s3->previous_server_finished_len;
860
861 /* Check for logic errors */
862 assert(!expected_len || ssl->s3->previous_client_finished_len);
863 assert(!expected_len || ssl->s3->previous_server_finished_len);
864
865 /* Parse out the extension contents. */
866 CBS renegotiated_connection;
867 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
868 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400869 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700870 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
871 return 0;
872 }
873
874 /* Check that the extension matches. */
875 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400876 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700877 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
878 return 0;
879 }
880
881 const uint8_t *d = CBS_data(&renegotiated_connection);
882 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
883 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400884 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700885 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
886 return 0;
887 }
888 d += ssl->s3->previous_client_finished_len;
889
890 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
891 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400892 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700893 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
894 return 0;
895 }
896 ssl->s3->send_connection_binding = 1;
897
898 return 1;
899}
900
901static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
902 CBS *contents) {
903 /* Renegotiation isn't supported as a server so this function should never be
904 * called after the initial handshake. */
905 assert(!ssl->s3->initial_handshake_complete);
906
Steven Valdez143e8b32016-07-11 13:19:03 -0400907 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
908 return 1;
909 }
910
Adam Langley5021b222015-06-12 18:27:58 -0700911 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400912 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700913 }
914
915 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700916 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
917 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400918 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700919 return 0;
920 }
921
922 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500923 if (!CBS_mem_equal(&renegotiated_connection,
924 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700925 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400926 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700927 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
928 return 0;
929 }
930
931 ssl->s3->send_connection_binding = 1;
932
933 return 1;
934}
935
936static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400937 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
938 return 1;
939 }
940
Adam Langley5021b222015-06-12 18:27:58 -0700941 CBB contents, prev_finished;
942 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
943 !CBB_add_u16_length_prefixed(out, &contents) ||
944 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
945 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
946 ssl->s3->previous_client_finished_len) ||
947 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
948 ssl->s3->previous_server_finished_len) ||
949 !CBB_flush(out)) {
950 return 0;
951 }
952
953 return 1;
954}
955
Adam Langley0a056712015-07-01 15:03:33 -0700956
957/* Extended Master Secret.
958 *
David Benjamin43946d42016-02-01 08:42:19 -0500959 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700960
961static void ext_ems_init(SSL *ssl) {
962 ssl->s3->tmp.extended_master_secret = 0;
963}
964
965static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400966 uint16_t min_version, max_version;
967 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
968 return 0;
969 }
970
971 /* Extended master secret is not necessary in TLS 1.3. */
972 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700973 return 1;
974 }
975
976 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
977 !CBB_add_u16(out, 0 /* length */)) {
978 return 0;
979 }
980
981 return 1;
982}
983
984static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
985 CBS *contents) {
986 if (contents == NULL) {
987 return 1;
988 }
989
Steven Valdez143e8b32016-07-11 13:19:03 -0400990 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
991 ssl->version == SSL3_VERSION) {
992 return 0;
993 }
994
995 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700996 return 0;
997 }
998
999 ssl->s3->tmp.extended_master_secret = 1;
1000 return 1;
1001}
1002
David Benjamin0d56f882015-12-19 17:05:56 -05001003static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1004 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001005 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1006 ssl->version == SSL3_VERSION) {
1007 return 1;
1008 }
1009
1010 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -07001011 return 1;
1012 }
1013
1014 if (CBS_len(contents) != 0) {
1015 return 0;
1016 }
1017
1018 ssl->s3->tmp.extended_master_secret = 1;
1019 return 1;
1020}
1021
1022static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1023 if (!ssl->s3->tmp.extended_master_secret) {
1024 return 1;
1025 }
1026
1027 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1028 !CBB_add_u16(out, 0 /* length */)) {
1029 return 0;
1030 }
1031
1032 return 1;
1033}
1034
Adam Langley9b05bc52015-07-01 15:25:33 -07001035
1036/* Session tickets.
1037 *
1038 * https://tools.ietf.org/html/rfc5077 */
1039
1040static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -04001041 uint16_t min_version, max_version;
1042 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1043 return 0;
1044 }
1045
1046 /* TLS 1.3 uses a different ticket extension. */
1047 if (min_version >= TLS1_3_VERSION ||
1048 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001049 return 1;
1050 }
1051
1052 const uint8_t *ticket_data = NULL;
1053 int ticket_len = 0;
1054
1055 /* Renegotiation does not participate in session resumption. However, still
1056 * advertise the extension to avoid potentially breaking servers which carry
1057 * over the state from the previous handshake, such as OpenSSL servers
1058 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1059 if (!ssl->s3->initial_handshake_complete &&
1060 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001061 ssl->session->tlsext_tick != NULL &&
1062 /* Don't send TLS 1.3 session tickets in the ticket extension. */
1063 ssl->method->version_from_wire(ssl->session->ssl_version) <
1064 TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001065 ticket_data = ssl->session->tlsext_tick;
1066 ticket_len = ssl->session->tlsext_ticklen;
1067 }
1068
1069 CBB ticket;
1070 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1071 !CBB_add_u16_length_prefixed(out, &ticket) ||
1072 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1073 !CBB_flush(out)) {
1074 return 0;
1075 }
1076
1077 return 1;
1078}
1079
1080static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1081 CBS *contents) {
1082 ssl->tlsext_ticket_expected = 0;
1083
1084 if (contents == NULL) {
1085 return 1;
1086 }
1087
Steven Valdez143e8b32016-07-11 13:19:03 -04001088 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1089 return 0;
1090 }
1091
Adam Langley9b05bc52015-07-01 15:25:33 -07001092 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1093 * this function should never be called, even if the server tries to send the
1094 * extension. */
1095 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1096
1097 if (CBS_len(contents) != 0) {
1098 return 0;
1099 }
1100
1101 ssl->tlsext_ticket_expected = 1;
1102 return 1;
1103}
1104
Adam Langley9b05bc52015-07-01 15:25:33 -07001105static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1106 if (!ssl->tlsext_ticket_expected) {
1107 return 1;
1108 }
1109
1110 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1111 * true. */
1112 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1113
1114 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1115 !CBB_add_u16(out, 0 /* length */)) {
1116 return 0;
1117 }
1118
1119 return 1;
1120}
1121
1122
Adam Langley2e857bd2015-07-01 16:09:19 -07001123/* Signature Algorithms.
1124 *
1125 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1126
1127static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjaminb6a0a512016-06-21 10:33:21 -04001128 if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001129 return 1;
1130 }
1131
David Benjamin0fc37ef2016-08-17 15:29:46 -04001132 const uint16_t *sigalgs;
1133 const size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001134
David Benjamin0fc37ef2016-08-17 15:29:46 -04001135 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001136 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1137 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001138 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001139 return 0;
1140 }
1141
David Benjamin0fc37ef2016-08-17 15:29:46 -04001142 for (size_t i = 0; i < num_sigalgs; i++) {
1143 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001144 return 0;
1145 }
1146 }
1147
1148 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001149 return 0;
1150 }
1151
1152 return 1;
1153}
1154
Adam Langley2e857bd2015-07-01 16:09:19 -07001155static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1156 CBS *contents) {
David Benjamin0fc37ef2016-08-17 15:29:46 -04001157 OPENSSL_free(ssl->s3->hs->peer_sigalgs);
1158 ssl->s3->hs->peer_sigalgs = NULL;
1159 ssl->s3->hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001160
Adam Langley2e857bd2015-07-01 16:09:19 -07001161 if (contents == NULL) {
1162 return 1;
1163 }
1164
1165 CBS supported_signature_algorithms;
1166 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001167 CBS_len(contents) != 0 ||
1168 CBS_len(&supported_signature_algorithms) == 0 ||
1169 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001170 return 0;
1171 }
1172
1173 return 1;
1174}
1175
Adam Langley2e857bd2015-07-01 16:09:19 -07001176
Adam Langleybb0bd042015-07-01 16:21:03 -07001177/* OCSP Stapling.
1178 *
1179 * https://tools.ietf.org/html/rfc6066#section-8 */
1180
1181static void ext_ocsp_init(SSL *ssl) {
1182 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001183 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001184}
1185
1186static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1187 if (!ssl->ocsp_stapling_enabled) {
1188 return 1;
1189 }
1190
1191 CBB contents;
1192 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1193 !CBB_add_u16_length_prefixed(out, &contents) ||
1194 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1195 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1196 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1197 !CBB_flush(out)) {
1198 return 0;
1199 }
1200
Adam Langleyce9d85e2016-01-24 15:58:39 -08001201 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001202 return 1;
1203}
1204
1205static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001206 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001207 if (contents == NULL) {
1208 return 1;
1209 }
1210
David Benjamin942f4ed2016-07-16 19:03:49 +03001211 /* OCSP stapling is forbidden on a non-certificate cipher. */
1212 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001213 return 0;
1214 }
1215
David Benjamin942f4ed2016-07-16 19:03:49 +03001216 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1217 if (CBS_len(contents) != 0) {
1218 return 0;
1219 }
1220
1221 ssl->s3->tmp.certificate_status_expected = 1;
1222 return 1;
1223 }
1224
1225 uint8_t status_type;
1226 CBS ocsp_response;
1227 if (!CBS_get_u8(contents, &status_type) ||
1228 status_type != TLSEXT_STATUSTYPE_ocsp ||
1229 !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1230 CBS_len(&ocsp_response) == 0 ||
1231 CBS_len(contents) != 0) {
1232 return 0;
1233 }
1234
Steven Valdez87eab492016-06-27 16:34:59 -04001235 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1236 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001237 *out_alert = SSL_AD_INTERNAL_ERROR;
1238 return 0;
1239 }
1240
Adam Langleybb0bd042015-07-01 16:21:03 -07001241 return 1;
1242}
1243
1244static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1245 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001246 if (contents == NULL) {
1247 return 1;
1248 }
1249
1250 uint8_t status_type;
1251 if (!CBS_get_u8(contents, &status_type)) {
1252 return 0;
1253 }
1254
1255 /* We cannot decide whether OCSP stapling will occur yet because the correct
1256 * SSL_CTX might not have been selected. */
1257 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1258
Adam Langleybb0bd042015-07-01 16:21:03 -07001259 return 1;
1260}
1261
1262static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001263 if (!ssl->s3->tmp.ocsp_stapling_requested ||
1264 ssl->ctx->ocsp_response_length == 0 ||
1265 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001266 return 1;
1267 }
1268
David Benjamin942f4ed2016-07-16 19:03:49 +03001269 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1270 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez87eab492016-06-27 16:34:59 -04001271 if (ssl->session != NULL) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001272 return 1;
1273 }
1274
1275 ssl->s3->tmp.certificate_status_expected = 1;
1276
1277 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1278 CBB_add_u16(out, 0 /* length */);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001279 }
1280
David Benjamin942f4ed2016-07-16 19:03:49 +03001281 CBB body, ocsp_response;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001282 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
David Benjamin942f4ed2016-07-16 19:03:49 +03001283 CBB_add_u16_length_prefixed(out, &body) &&
1284 CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1285 CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1286 CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1287 ssl->ctx->ocsp_response_length) &&
1288 CBB_flush(out);
Adam Langleybb0bd042015-07-01 16:21:03 -07001289}
1290
1291
Adam Langley97dfcbf2015-07-01 18:35:20 -07001292/* Next protocol negotiation.
1293 *
1294 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1295
1296static void ext_npn_init(SSL *ssl) {
1297 ssl->s3->next_proto_neg_seen = 0;
1298}
1299
1300static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1301 if (ssl->s3->initial_handshake_complete ||
1302 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001303 (ssl->options & SSL_OP_DISABLE_NPN) ||
David Benjamince079fd2016-08-02 16:22:34 -04001304 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001305 return 1;
1306 }
1307
1308 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1309 !CBB_add_u16(out, 0 /* length */)) {
1310 return 0;
1311 }
1312
1313 return 1;
1314}
1315
1316static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1317 CBS *contents) {
1318 if (contents == NULL) {
1319 return 1;
1320 }
1321
Steven Valdez143e8b32016-07-11 13:19:03 -04001322 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1323 return 0;
1324 }
1325
Adam Langley97dfcbf2015-07-01 18:35:20 -07001326 /* If any of these are false then we should never have sent the NPN
1327 * extension in the ClientHello and thus this function should never have been
1328 * called. */
1329 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001330 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001331 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001332 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001333
David Benjamin76c2efc2015-08-31 14:24:29 -04001334 if (ssl->s3->alpn_selected != NULL) {
1335 /* NPN and ALPN may not be negotiated in the same connection. */
1336 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1337 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1338 return 0;
1339 }
1340
Adam Langley97dfcbf2015-07-01 18:35:20 -07001341 const uint8_t *const orig_contents = CBS_data(contents);
1342 const size_t orig_len = CBS_len(contents);
1343
1344 while (CBS_len(contents) != 0) {
1345 CBS proto;
1346 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1347 CBS_len(&proto) == 0) {
1348 return 0;
1349 }
1350 }
1351
1352 uint8_t *selected;
1353 uint8_t selected_len;
1354 if (ssl->ctx->next_proto_select_cb(
1355 ssl, &selected, &selected_len, orig_contents, orig_len,
1356 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1357 *out_alert = SSL_AD_INTERNAL_ERROR;
1358 return 0;
1359 }
1360
David Benjamin79978df2015-12-25 15:56:49 -05001361 OPENSSL_free(ssl->s3->next_proto_negotiated);
1362 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1363 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001364 *out_alert = SSL_AD_INTERNAL_ERROR;
1365 return 0;
1366 }
1367
David Benjamin79978df2015-12-25 15:56:49 -05001368 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001369 ssl->s3->next_proto_neg_seen = 1;
1370
1371 return 1;
1372}
1373
1374static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1375 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001376 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1377 return 1;
1378 }
1379
Adam Langley97dfcbf2015-07-01 18:35:20 -07001380 if (contents != NULL && CBS_len(contents) != 0) {
1381 return 0;
1382 }
1383
1384 if (contents == NULL ||
1385 ssl->s3->initial_handshake_complete ||
1386 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1387 * afterwards, parsing the ALPN extension will clear
1388 * |next_proto_neg_seen|. */
1389 ssl->s3->alpn_selected != NULL ||
1390 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001391 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001392 return 1;
1393 }
1394
1395 ssl->s3->next_proto_neg_seen = 1;
1396 return 1;
1397}
1398
1399static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1400 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1401 * parsed. */
1402 if (!ssl->s3->next_proto_neg_seen) {
1403 return 1;
1404 }
1405
1406 const uint8_t *npa;
1407 unsigned npa_len;
1408
1409 if (ssl->ctx->next_protos_advertised_cb(
1410 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1411 SSL_TLSEXT_ERR_OK) {
1412 ssl->s3->next_proto_neg_seen = 0;
1413 return 1;
1414 }
1415
1416 CBB contents;
1417 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1418 !CBB_add_u16_length_prefixed(out, &contents) ||
1419 !CBB_add_bytes(&contents, npa, npa_len) ||
1420 !CBB_flush(out)) {
1421 return 0;
1422 }
1423
1424 return 1;
1425}
1426
1427
Adam Langleyab8d87d2015-07-10 12:21:39 -07001428/* Signed certificate timestamps.
1429 *
1430 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1431
1432static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1433 if (!ssl->signed_cert_timestamps_enabled) {
1434 return 1;
1435 }
1436
1437 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1438 !CBB_add_u16(out, 0 /* length */)) {
1439 return 0;
1440 }
1441
1442 return 1;
1443}
1444
1445static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1446 CBS *contents) {
1447 if (contents == NULL) {
1448 return 1;
1449 }
1450
1451 /* If this is false then we should never have sent the SCT extension in the
1452 * ClientHello and thus this function should never have been called. */
1453 assert(ssl->signed_cert_timestamps_enabled);
1454
1455 if (CBS_len(contents) == 0) {
1456 *out_alert = SSL_AD_DECODE_ERROR;
1457 return 0;
1458 }
1459
1460 /* Session resumption uses the original session information. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001461 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001462 !CBS_stow(
1463 contents,
1464 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1465 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001466 *out_alert = SSL_AD_INTERNAL_ERROR;
1467 return 0;
1468 }
1469
1470 return 1;
1471}
1472
1473static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1474 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001475 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001476}
1477
1478static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001479 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001480 if (ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001481 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001482 return 1;
1483 }
1484
1485 CBB contents;
1486 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1487 CBB_add_u16_length_prefixed(out, &contents) &&
1488 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1489 ssl->ctx->signed_cert_timestamp_list_length) &&
1490 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001491}
1492
1493
Adam Langleyf18e4532015-07-10 13:39:53 -07001494/* Application-level Protocol Negotiation.
1495 *
1496 * https://tools.ietf.org/html/rfc7301 */
1497
1498static void ext_alpn_init(SSL *ssl) {
1499 OPENSSL_free(ssl->s3->alpn_selected);
1500 ssl->s3->alpn_selected = NULL;
1501}
1502
1503static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1504 if (ssl->alpn_client_proto_list == NULL ||
1505 ssl->s3->initial_handshake_complete) {
1506 return 1;
1507 }
1508
1509 CBB contents, proto_list;
1510 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1511 !CBB_add_u16_length_prefixed(out, &contents) ||
1512 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1513 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1514 ssl->alpn_client_proto_list_len) ||
1515 !CBB_flush(out)) {
1516 return 0;
1517 }
1518
1519 return 1;
1520}
1521
1522static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1523 CBS *contents) {
1524 if (contents == NULL) {
1525 return 1;
1526 }
1527
1528 assert(!ssl->s3->initial_handshake_complete);
1529 assert(ssl->alpn_client_proto_list != NULL);
1530
David Benjamin76c2efc2015-08-31 14:24:29 -04001531 if (ssl->s3->next_proto_neg_seen) {
1532 /* NPN and ALPN may not be negotiated in the same connection. */
1533 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1534 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1535 return 0;
1536 }
1537
Adam Langleyf18e4532015-07-10 13:39:53 -07001538 /* The extension data consists of a ProtocolNameList which must have
1539 * exactly one ProtocolName. Each of these is length-prefixed. */
1540 CBS protocol_name_list, protocol_name;
1541 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1542 CBS_len(contents) != 0 ||
1543 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1544 /* Empty protocol names are forbidden. */
1545 CBS_len(&protocol_name) == 0 ||
1546 CBS_len(&protocol_name_list) != 0) {
1547 return 0;
1548 }
1549
David Benjamin3e51757d2016-08-11 11:52:23 -04001550 /* Check that the protcol name is one of the ones we advertised. */
1551 int protocol_ok = 0;
1552 CBS client_protocol_name_list, client_protocol_name;
1553 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1554 ssl->alpn_client_proto_list_len);
1555 while (CBS_len(&client_protocol_name_list) > 0) {
1556 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1557 &client_protocol_name)) {
1558 *out_alert = SSL_AD_INTERNAL_ERROR;
1559 return 0;
1560 }
1561
1562 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1563 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1564 CBS_len(&protocol_name)) == 0) {
1565 protocol_ok = 1;
1566 break;
1567 }
1568 }
1569
1570 if (!protocol_ok) {
1571 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1572 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1573 return 0;
1574 }
1575
Adam Langleyf18e4532015-07-10 13:39:53 -07001576 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1577 &ssl->s3->alpn_selected_len)) {
1578 *out_alert = SSL_AD_INTERNAL_ERROR;
1579 return 0;
1580 }
1581
1582 return 1;
1583}
1584
1585static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1586 CBS *contents) {
1587 if (contents == NULL) {
1588 return 1;
1589 }
1590
1591 if (ssl->ctx->alpn_select_cb == NULL ||
1592 ssl->s3->initial_handshake_complete) {
1593 return 1;
1594 }
1595
1596 /* ALPN takes precedence over NPN. */
1597 ssl->s3->next_proto_neg_seen = 0;
1598
1599 CBS protocol_name_list;
1600 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1601 CBS_len(contents) != 0 ||
1602 CBS_len(&protocol_name_list) < 2) {
1603 return 0;
1604 }
1605
1606 /* Validate the protocol list. */
1607 CBS protocol_name_list_copy = protocol_name_list;
1608 while (CBS_len(&protocol_name_list_copy) > 0) {
1609 CBS protocol_name;
1610
1611 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1612 /* Empty protocol names are forbidden. */
1613 CBS_len(&protocol_name) == 0) {
1614 return 0;
1615 }
1616 }
1617
1618 const uint8_t *selected;
1619 uint8_t selected_len;
1620 if (ssl->ctx->alpn_select_cb(
1621 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1622 CBS_len(&protocol_name_list),
1623 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1624 OPENSSL_free(ssl->s3->alpn_selected);
1625 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1626 if (ssl->s3->alpn_selected == NULL) {
1627 *out_alert = SSL_AD_INTERNAL_ERROR;
1628 return 0;
1629 }
1630 ssl->s3->alpn_selected_len = selected_len;
1631 }
1632
1633 return 1;
1634}
1635
1636static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1637 if (ssl->s3->alpn_selected == NULL) {
1638 return 1;
1639 }
1640
1641 CBB contents, proto_list, proto;
1642 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1643 !CBB_add_u16_length_prefixed(out, &contents) ||
1644 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1645 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001646 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1647 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001648 !CBB_flush(out)) {
1649 return 0;
1650 }
1651
1652 return 1;
1653}
1654
1655
Adam Langley49c7af12015-07-10 14:33:46 -07001656/* Channel ID.
1657 *
1658 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1659
1660static void ext_channel_id_init(SSL *ssl) {
1661 ssl->s3->tlsext_channel_id_valid = 0;
1662}
1663
1664static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1665 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001666 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001667 return 1;
1668 }
1669
1670 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1671 !CBB_add_u16(out, 0 /* length */)) {
1672 return 0;
1673 }
1674
1675 return 1;
1676}
1677
1678static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1679 CBS *contents) {
1680 if (contents == NULL) {
1681 return 1;
1682 }
1683
Steven Valdez143e8b32016-07-11 13:19:03 -04001684 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1685 return 0;
1686 }
1687
David Benjamince079fd2016-08-02 16:22:34 -04001688 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001689 assert(ssl->tlsext_channel_id_enabled);
1690
1691 if (CBS_len(contents) != 0) {
1692 return 0;
1693 }
1694
1695 ssl->s3->tlsext_channel_id_valid = 1;
1696 return 1;
1697}
1698
1699static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1700 CBS *contents) {
1701 if (contents == NULL ||
1702 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001703 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001704 return 1;
1705 }
1706
1707 if (CBS_len(contents) != 0) {
1708 return 0;
1709 }
1710
1711 ssl->s3->tlsext_channel_id_valid = 1;
1712 return 1;
1713}
1714
1715static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001716 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1717 return 1;
1718 }
1719
Adam Langley49c7af12015-07-10 14:33:46 -07001720 if (!ssl->s3->tlsext_channel_id_valid) {
1721 return 1;
1722 }
1723
1724 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1725 !CBB_add_u16(out, 0 /* length */)) {
1726 return 0;
1727 }
1728
1729 return 1;
1730}
1731
Adam Langley391250d2015-07-15 19:06:07 -07001732
1733/* Secure Real-time Transport Protocol (SRTP) extension.
1734 *
1735 * https://tools.ietf.org/html/rfc5764 */
1736
Adam Langley391250d2015-07-15 19:06:07 -07001737
1738static void ext_srtp_init(SSL *ssl) {
1739 ssl->srtp_profile = NULL;
1740}
1741
1742static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1743 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1744 if (profiles == NULL) {
1745 return 1;
1746 }
1747 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1748 if (num_profiles == 0) {
1749 return 1;
1750 }
1751
1752 CBB contents, profile_ids;
1753 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1754 !CBB_add_u16_length_prefixed(out, &contents) ||
1755 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1756 return 0;
1757 }
1758
1759 size_t i;
1760 for (i = 0; i < num_profiles; i++) {
1761 if (!CBB_add_u16(&profile_ids,
1762 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1763 return 0;
1764 }
1765 }
1766
1767 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1768 !CBB_flush(out)) {
1769 return 0;
1770 }
1771
1772 return 1;
1773}
1774
1775static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1776 CBS *contents) {
1777 if (contents == NULL) {
1778 return 1;
1779 }
1780
1781 /* The extension consists of a u16-prefixed profile ID list containing a
1782 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1783 *
1784 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1785 CBS profile_ids, srtp_mki;
1786 uint16_t profile_id;
1787 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1788 !CBS_get_u16(&profile_ids, &profile_id) ||
1789 CBS_len(&profile_ids) != 0 ||
1790 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1791 CBS_len(contents) != 0) {
1792 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1793 return 0;
1794 }
1795
1796 if (CBS_len(&srtp_mki) != 0) {
1797 /* Must be no MKI, since we never offer one. */
1798 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1799 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1800 return 0;
1801 }
1802
1803 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1804
1805 /* Check to see if the server gave us something we support (and presumably
1806 * offered). */
1807 size_t i;
1808 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1809 const SRTP_PROTECTION_PROFILE *profile =
1810 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1811
1812 if (profile->id == profile_id) {
1813 ssl->srtp_profile = profile;
1814 return 1;
1815 }
1816 }
1817
1818 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1819 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1820 return 0;
1821}
1822
1823static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1824 CBS *contents) {
1825 if (contents == NULL) {
1826 return 1;
1827 }
1828
1829 CBS profile_ids, srtp_mki;
1830 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1831 CBS_len(&profile_ids) < 2 ||
1832 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1833 CBS_len(contents) != 0) {
1834 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1835 return 0;
1836 }
1837 /* Discard the MKI value for now. */
1838
1839 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1840 SSL_get_srtp_profiles(ssl);
1841
1842 /* Pick the server's most preferred profile. */
1843 size_t i;
1844 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1845 const SRTP_PROTECTION_PROFILE *server_profile =
1846 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1847
1848 CBS profile_ids_tmp;
1849 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1850
1851 while (CBS_len(&profile_ids_tmp) > 0) {
1852 uint16_t profile_id;
1853 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1854 return 0;
1855 }
1856
1857 if (server_profile->id == profile_id) {
1858 ssl->srtp_profile = server_profile;
1859 return 1;
1860 }
1861 }
1862 }
1863
1864 return 1;
1865}
1866
1867static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1868 if (ssl->srtp_profile == NULL) {
1869 return 1;
1870 }
1871
1872 CBB contents, profile_ids;
1873 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1874 !CBB_add_u16_length_prefixed(out, &contents) ||
1875 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1876 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1877 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1878 !CBB_flush(out)) {
1879 return 0;
1880 }
1881
1882 return 1;
1883}
1884
Adam Langleybdd5d662015-07-20 16:19:08 -07001885
1886/* EC point formats.
1887 *
1888 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1889
1890static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
David Benjamince079fd2016-08-02 16:22:34 -04001891 if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001892 return 0;
1893 }
1894
1895 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1896
1897 size_t i;
1898 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1899 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1900
1901 const uint32_t alg_k = cipher->algorithm_mkey;
1902 const uint32_t alg_a = cipher->algorithm_auth;
1903 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1904 return 1;
1905 }
1906 }
1907
1908 return 0;
1909}
1910
Adam Langleybdd5d662015-07-20 16:19:08 -07001911static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001912 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001913 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1914 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001915 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1916 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001917 !CBB_flush(out)) {
1918 return 0;
1919 }
1920
1921 return 1;
1922}
1923
1924static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1925 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1926 return 1;
1927 }
1928
1929 return ext_ec_point_add_extension(ssl, out);
1930}
1931
1932static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1933 CBS *contents) {
1934 if (contents == NULL) {
1935 return 1;
1936 }
1937
Steven Valdez143e8b32016-07-11 13:19:03 -04001938 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1939 return 0;
1940 }
1941
Adam Langleybdd5d662015-07-20 16:19:08 -07001942 CBS ec_point_format_list;
1943 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1944 CBS_len(contents) != 0) {
1945 return 0;
1946 }
1947
David Benjaminfc059942015-07-30 23:01:59 -04001948 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1949 * point format. */
1950 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1951 CBS_len(&ec_point_format_list)) == NULL) {
1952 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001953 return 0;
1954 }
1955
1956 return 1;
1957}
1958
1959static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1960 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001961 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1962 return 1;
1963 }
1964
Adam Langleybdd5d662015-07-20 16:19:08 -07001965 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1966}
1967
1968static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001969 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1970 return 1;
1971 }
1972
Adam Langleybdd5d662015-07-20 16:19:08 -07001973 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1974 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001975 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001976
1977 if (!using_ecc) {
1978 return 1;
1979 }
1980
1981 return ext_ec_point_add_extension(ssl, out);
1982}
1983
Adam Langley273d49c2015-07-20 16:38:52 -07001984
Steven Valdez143e8b32016-07-11 13:19:03 -04001985/* Draft Version Extension */
1986
1987static int ext_draft_version_add_clienthello(SSL *ssl, CBB *out) {
1988 uint16_t min_version, max_version;
1989 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
Steven Valdez3a287552016-07-19 05:10:50 -04001990 max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001991 return 1;
1992 }
1993
1994 CBB contents;
1995 if (!CBB_add_u16(out, TLSEXT_TYPE_draft_version) ||
1996 !CBB_add_u16_length_prefixed(out, &contents) ||
1997 !CBB_add_u16(&contents, TLS1_3_DRAFT_VERSION)) {
1998 return 0;
1999 }
2000
2001 return CBB_flush(out);
2002}
2003
2004
Steven Valdez4aa154e2016-07-29 14:32:55 -04002005/* Pre Shared Key
2006 *
2007 * https://tools.ietf.org/html/draft-ietf-tls-tls13-14 */
2008
2009static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
2010 uint16_t min_version, max_version;
2011 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2012 return 0;
2013 }
2014
2015 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
2016 ssl->method->version_from_wire(ssl->session->ssl_version) <
2017 TLS1_3_VERSION) {
2018 return 1;
2019 }
2020
2021 CBB contents, identities, identity;
2022 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2023 !CBB_add_u16_length_prefixed(out, &contents) ||
2024 !CBB_add_u16_length_prefixed(&contents, &identities) ||
2025 !CBB_add_u16_length_prefixed(&identities, &identity) ||
2026 !CBB_add_bytes(&identity, ssl->session->tlsext_tick,
2027 ssl->session->tlsext_ticklen)) {
2028 return 0;
2029 }
2030
2031 return CBB_flush(out);
2032}
2033
2034int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2035 CBS *contents) {
2036 uint16_t psk_id;
2037 if (!CBS_get_u16(contents, &psk_id) ||
2038 CBS_len(contents) != 0) {
2039 *out_alert = SSL_AD_DECODE_ERROR;
2040 return 0;
2041 }
2042
2043 if (psk_id != 0) {
2044 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2045 return 0;
2046 }
2047
2048 return 1;
2049}
2050
2051int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
2052 SSL_SESSION **out_session,
2053 uint8_t *out_alert,
2054 CBS *contents) {
2055 CBS identities, identity;
2056 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2057 !CBS_get_u16_length_prefixed(&identities, &identity) ||
2058 CBS_len(contents) != 0) {
2059 *out_alert = SSL_AD_DECODE_ERROR;
2060 return 0;
2061 }
2062
2063 /* TLS 1.3 session tickets are renewed separately as part of the
2064 * NewSessionTicket. */
2065 int renew;
2066 return tls_process_ticket(ssl, out_session, &renew, CBS_data(&identity),
2067 CBS_len(&identity), NULL, 0);
2068}
2069
2070int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
2071 if (!ssl->s3->session_reused) {
2072 return 1;
2073 }
2074
2075 CBB contents;
2076 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2077 !CBB_add_u16_length_prefixed(out, &contents) ||
2078 /* We only consider the first identity for resumption */
2079 !CBB_add_u16(&contents, 0) ||
2080 !CBB_flush(out)) {
2081 return 0;
2082 }
2083
2084 return 1;
2085}
2086
2087
Steven Valdez143e8b32016-07-11 13:19:03 -04002088/* Key Share
2089 *
2090 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
2091
2092static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
2093 uint16_t min_version, max_version;
2094 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2095 return 0;
2096 }
2097
2098 if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
2099 return 1;
2100 }
2101
2102 CBB contents, kse_bytes;
2103 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2104 !CBB_add_u16_length_prefixed(out, &contents) ||
2105 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2106 return 0;
2107 }
2108
2109 const uint16_t *groups;
2110 size_t groups_len;
Steven Valdez5440fe02016-07-18 12:40:30 -04002111 if (ssl->s3->hs->retry_group) {
2112 /* Append the new key share to the old list. */
2113 if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
2114 ssl->s3->hs->key_share_bytes_len)) {
2115 return 0;
2116 }
2117 OPENSSL_free(ssl->s3->hs->key_share_bytes);
2118 ssl->s3->hs->key_share_bytes = NULL;
2119
2120 groups = &ssl->s3->hs->retry_group;
2121 groups_len = 1;
2122 } else {
2123 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
2124 /* Only send the top two preferred key shares. */
2125 if (groups_len > 2) {
2126 groups_len = 2;
2127 }
2128 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002129
2130 ssl->s3->hs->groups = OPENSSL_malloc(groups_len * sizeof(SSL_ECDH_CTX));
2131 if (ssl->s3->hs->groups == NULL) {
2132 return 0;
2133 }
2134 memset(ssl->s3->hs->groups, 0, groups_len * sizeof(SSL_ECDH_CTX));
2135 ssl->s3->hs->groups_len = groups_len;
2136
2137 for (size_t i = 0; i < groups_len; i++) {
2138 if (!CBB_add_u16(&kse_bytes, groups[i])) {
2139 return 0;
2140 }
2141
2142 CBB key_exchange;
2143 if (!CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2144 !SSL_ECDH_CTX_init(&ssl->s3->hs->groups[i], groups[i]) ||
2145 !SSL_ECDH_CTX_offer(&ssl->s3->hs->groups[i], &key_exchange) ||
2146 !CBB_flush(&kse_bytes)) {
2147 return 0;
2148 }
2149 }
2150
Steven Valdez5440fe02016-07-18 12:40:30 -04002151 if (!ssl->s3->hs->retry_group) {
2152 /* Save the contents of the extension to repeat it in the second
2153 * ClientHello. */
2154 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2155 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2156 CBB_len(&kse_bytes));
2157 if (ssl->s3->hs->key_share_bytes == NULL) {
2158 return 0;
2159 }
2160 }
2161
Steven Valdez143e8b32016-07-11 13:19:03 -04002162 return CBB_flush(out);
2163}
2164
Steven Valdez7259f2f2016-08-02 16:55:05 -04002165int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2166 size_t *out_secret_len,
2167 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002168 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002169 uint16_t group_id;
2170 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002171 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2172 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002173 *out_alert = SSL_AD_DECODE_ERROR;
2174 return 0;
2175 }
2176
2177 SSL_ECDH_CTX *group_ctx = NULL;
2178 for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) {
David Benjamin5c4e8572016-08-19 17:44:53 -04002179 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->groups[i]) == group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002180 group_ctx = &ssl->s3->hs->groups[i];
2181 break;
2182 }
2183 }
2184
2185 if (group_ctx == NULL) {
2186 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2187 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2188 return 0;
2189 }
2190
2191 if (!SSL_ECDH_CTX_finish(group_ctx, out_secret, out_secret_len, out_alert,
2192 CBS_data(&peer_key), CBS_len(&peer_key))) {
2193 *out_alert = SSL_AD_INTERNAL_ERROR;
2194 return 0;
2195 }
2196
David Benjamin5c4e8572016-08-19 17:44:53 -04002197 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez5440fe02016-07-18 12:40:30 -04002198 ssl_handshake_clear_groups(ssl->s3->hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04002199 return 1;
2200}
2201
Steven Valdez7259f2f2016-08-02 16:55:05 -04002202int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2203 uint8_t **out_secret,
2204 size_t *out_secret_len,
2205 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002206 uint16_t group_id;
2207 CBS key_shares;
2208 if (!tls1_get_shared_group(ssl, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002209 !CBS_get_u16_length_prefixed(contents, &key_shares) ||
2210 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002211 return 0;
2212 }
2213
Steven Valdez5440fe02016-07-18 12:40:30 -04002214 *out_found = 0;
Steven Valdez143e8b32016-07-11 13:19:03 -04002215 while (CBS_len(&key_shares) > 0) {
2216 uint16_t id;
2217 CBS peer_key;
2218 if (!CBS_get_u16(&key_shares, &id) ||
2219 !CBS_get_u16_length_prefixed(&key_shares, &peer_key)) {
2220 return 0;
2221 }
2222
Steven Valdez5440fe02016-07-18 12:40:30 -04002223 if (id != group_id || *out_found) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002224 continue;
2225 }
2226
2227 SSL_ECDH_CTX group;
2228 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2229 CBB public_key;
2230 if (!CBB_init(&public_key, 0) ||
2231 !SSL_ECDH_CTX_init(&group, group_id) ||
2232 !SSL_ECDH_CTX_accept(&group, &public_key, out_secret, out_secret_len,
2233 out_alert, CBS_data(&peer_key),
2234 CBS_len(&peer_key)) ||
2235 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2236 &ssl->s3->hs->public_key_len)) {
2237 SSL_ECDH_CTX_cleanup(&group);
2238 CBB_cleanup(&public_key);
2239 return 0;
2240 }
2241 SSL_ECDH_CTX_cleanup(&group);
2242
Steven Valdez5440fe02016-07-18 12:40:30 -04002243 *out_found = 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002244 }
2245
Steven Valdez5440fe02016-07-18 12:40:30 -04002246 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002247}
2248
Steven Valdez7259f2f2016-08-02 16:55:05 -04002249int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002250 if (ssl->s3->tmp.new_cipher->algorithm_mkey != SSL_kECDHE) {
2251 return 1;
2252 }
2253
2254 uint16_t group_id;
2255 CBB kse_bytes, public_key;
2256 if (!tls1_get_shared_group(ssl, &group_id) ||
2257 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2258 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2259 !CBB_add_u16(&kse_bytes, group_id) ||
2260 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2261 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2262 ssl->s3->hs->public_key_len) ||
2263 !CBB_flush(out)) {
2264 return 0;
2265 }
2266
David Benjamin5c4e8572016-08-19 17:44:53 -04002267 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002268 return 1;
2269}
2270
2271
Steven Valdezce902a92016-05-17 11:47:53 -04002272/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002273 *
Steven Valdezce902a92016-05-17 11:47:53 -04002274 * https://tools.ietf.org/html/rfc4492#section-5.1.2
2275 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07002276
Steven Valdezce902a92016-05-17 11:47:53 -04002277static void ext_supported_groups_init(SSL *ssl) {
2278 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2279 ssl->s3->tmp.peer_supported_group_list = NULL;
2280 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07002281}
2282
Steven Valdezce902a92016-05-17 11:47:53 -04002283static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002284 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2285 return 1;
2286 }
2287
Steven Valdezce902a92016-05-17 11:47:53 -04002288 CBB contents, groups_bytes;
2289 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002290 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002291 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002292 return 0;
2293 }
2294
Steven Valdezce902a92016-05-17 11:47:53 -04002295 const uint16_t *groups;
2296 size_t groups_len;
2297 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002298
2299 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002300 for (i = 0; i < groups_len; i++) {
2301 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002302 return 0;
2303 }
2304 }
2305
2306 return CBB_flush(out);
2307}
2308
Steven Valdezce902a92016-05-17 11:47:53 -04002309static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2310 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002311 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2312 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002313 return 1;
2314}
2315
Steven Valdezce902a92016-05-17 11:47:53 -04002316static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2317 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002318 if (contents == NULL) {
2319 return 1;
2320 }
2321
Steven Valdezce902a92016-05-17 11:47:53 -04002322 CBS supported_group_list;
2323 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2324 CBS_len(&supported_group_list) == 0 ||
2325 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002326 CBS_len(contents) != 0) {
2327 return 0;
2328 }
2329
Steven Valdezce902a92016-05-17 11:47:53 -04002330 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
2331 CBS_len(&supported_group_list));
2332 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002333 *out_alert = SSL_AD_INTERNAL_ERROR;
2334 return 0;
2335 }
2336
Steven Valdezce902a92016-05-17 11:47:53 -04002337 const size_t num_groups = CBS_len(&supported_group_list) / 2;
Adam Langley273d49c2015-07-20 16:38:52 -07002338 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002339 for (i = 0; i < num_groups; i++) {
2340 if (!CBS_get_u16(&supported_group_list,
2341 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002342 goto err;
2343 }
2344 }
2345
Steven Valdezce902a92016-05-17 11:47:53 -04002346 assert(CBS_len(&supported_group_list) == 0);
2347 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002348
2349 return 1;
2350
2351err:
Steven Valdezce902a92016-05-17 11:47:53 -04002352 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2353 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002354 *out_alert = SSL_AD_INTERNAL_ERROR;
2355 return 0;
2356}
2357
Steven Valdezce902a92016-05-17 11:47:53 -04002358static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002359 /* Servers don't echo this extension. */
2360 return 1;
2361}
2362
2363
Adam Langley614c66a2015-06-12 15:26:58 -07002364/* kExtensions contains all the supported extensions. */
2365static const struct tls_extension kExtensions[] = {
2366 {
Adam Langley5021b222015-06-12 18:27:58 -07002367 TLSEXT_TYPE_renegotiate,
2368 NULL,
2369 ext_ri_add_clienthello,
2370 ext_ri_parse_serverhello,
2371 ext_ri_parse_clienthello,
2372 ext_ri_add_serverhello,
2373 },
2374 {
Adam Langley614c66a2015-06-12 15:26:58 -07002375 TLSEXT_TYPE_server_name,
2376 ext_sni_init,
2377 ext_sni_add_clienthello,
2378 ext_sni_parse_serverhello,
2379 ext_sni_parse_clienthello,
2380 ext_sni_add_serverhello,
2381 },
Adam Langley0a056712015-07-01 15:03:33 -07002382 {
2383 TLSEXT_TYPE_extended_master_secret,
2384 ext_ems_init,
2385 ext_ems_add_clienthello,
2386 ext_ems_parse_serverhello,
2387 ext_ems_parse_clienthello,
2388 ext_ems_add_serverhello,
2389 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002390 {
2391 TLSEXT_TYPE_session_ticket,
2392 NULL,
2393 ext_ticket_add_clienthello,
2394 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002395 /* Ticket extension client parsing is handled in ssl_session.c */
2396 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002397 ext_ticket_add_serverhello,
2398 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002399 {
2400 TLSEXT_TYPE_signature_algorithms,
2401 NULL,
2402 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002403 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002404 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002405 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002406 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002407 {
2408 TLSEXT_TYPE_status_request,
2409 ext_ocsp_init,
2410 ext_ocsp_add_clienthello,
2411 ext_ocsp_parse_serverhello,
2412 ext_ocsp_parse_clienthello,
2413 ext_ocsp_add_serverhello,
2414 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002415 {
2416 TLSEXT_TYPE_next_proto_neg,
2417 ext_npn_init,
2418 ext_npn_add_clienthello,
2419 ext_npn_parse_serverhello,
2420 ext_npn_parse_clienthello,
2421 ext_npn_add_serverhello,
2422 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002423 {
2424 TLSEXT_TYPE_certificate_timestamp,
2425 NULL,
2426 ext_sct_add_clienthello,
2427 ext_sct_parse_serverhello,
2428 ext_sct_parse_clienthello,
2429 ext_sct_add_serverhello,
2430 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002431 {
2432 TLSEXT_TYPE_application_layer_protocol_negotiation,
2433 ext_alpn_init,
2434 ext_alpn_add_clienthello,
2435 ext_alpn_parse_serverhello,
2436 ext_alpn_parse_clienthello,
2437 ext_alpn_add_serverhello,
2438 },
Adam Langley49c7af12015-07-10 14:33:46 -07002439 {
2440 TLSEXT_TYPE_channel_id,
2441 ext_channel_id_init,
2442 ext_channel_id_add_clienthello,
2443 ext_channel_id_parse_serverhello,
2444 ext_channel_id_parse_clienthello,
2445 ext_channel_id_add_serverhello,
2446 },
Adam Langley391250d2015-07-15 19:06:07 -07002447 {
2448 TLSEXT_TYPE_srtp,
2449 ext_srtp_init,
2450 ext_srtp_add_clienthello,
2451 ext_srtp_parse_serverhello,
2452 ext_srtp_parse_clienthello,
2453 ext_srtp_add_serverhello,
2454 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002455 {
2456 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002457 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002458 ext_ec_point_add_clienthello,
2459 ext_ec_point_parse_serverhello,
2460 ext_ec_point_parse_clienthello,
2461 ext_ec_point_add_serverhello,
2462 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002463 {
2464 TLSEXT_TYPE_draft_version,
2465 NULL,
2466 ext_draft_version_add_clienthello,
2467 forbid_parse_serverhello,
2468 ignore_parse_clienthello,
2469 dont_add_serverhello,
2470 },
2471 {
2472 TLSEXT_TYPE_key_share,
2473 NULL,
2474 ext_key_share_add_clienthello,
2475 forbid_parse_serverhello,
2476 ignore_parse_clienthello,
2477 dont_add_serverhello,
2478 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002479 {
2480 TLSEXT_TYPE_pre_shared_key,
2481 NULL,
2482 ext_pre_shared_key_add_clienthello,
2483 forbid_parse_serverhello,
2484 ignore_parse_clienthello,
2485 dont_add_serverhello,
2486 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002487 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2488 * intolerant to the last extension being zero-length. See
2489 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002490 {
Steven Valdezce902a92016-05-17 11:47:53 -04002491 TLSEXT_TYPE_supported_groups,
2492 ext_supported_groups_init,
2493 ext_supported_groups_add_clienthello,
2494 ext_supported_groups_parse_serverhello,
2495 ext_supported_groups_parse_clienthello,
2496 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002497 },
Adam Langley614c66a2015-06-12 15:26:58 -07002498};
2499
2500#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2501
Adam Langley4cfa96b2015-07-01 11:56:55 -07002502OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2503 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002504 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002505OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2506 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2507 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002508 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002509
Adam Langley614c66a2015-06-12 15:26:58 -07002510static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2511 uint16_t value) {
2512 unsigned i;
2513 for (i = 0; i < kNumExtensions; i++) {
2514 if (kExtensions[i].value == value) {
2515 *out_index = i;
2516 return &kExtensions[i];
2517 }
2518 }
2519
2520 return NULL;
2521}
2522
Adam Langley09505632015-07-30 18:10:13 -07002523int SSL_extension_supported(unsigned extension_value) {
2524 uint32_t index;
2525 return extension_value == TLSEXT_TYPE_padding ||
2526 tls_extension_find(&index, extension_value) != NULL;
2527}
2528
David Benjamine8d53502015-10-10 14:13:23 -04002529int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002530 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002531 if (ssl->client_version == SSL3_VERSION &&
2532 !ssl->s3->send_connection_binding) {
2533 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002534 }
Adam Langley95c29f32014-06-20 12:00:00 -07002535
David Benjamine8d53502015-10-10 14:13:23 -04002536 CBB extensions;
2537 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002538 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002539 }
Adam Langley95c29f32014-06-20 12:00:00 -07002540
David Benjamine8d53502015-10-10 14:13:23 -04002541 ssl->s3->tmp.extensions.sent = 0;
2542 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002543
Adam Langley614c66a2015-06-12 15:26:58 -07002544 size_t i;
2545 for (i = 0; i < kNumExtensions; i++) {
2546 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002547 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002548 }
2549 }
Adam Langley95c29f32014-06-20 12:00:00 -07002550
Adam Langley614c66a2015-06-12 15:26:58 -07002551 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002552 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002553 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002554 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2555 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2556 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002557 }
Adam Langley95c29f32014-06-20 12:00:00 -07002558
Adam Langley33ad2b52015-07-20 17:43:53 -07002559 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002560 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002561 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002562 }
Adam Langley75712922014-10-10 16:23:43 -07002563
David Benjamine8d53502015-10-10 14:13:23 -04002564 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002565 goto err;
2566 }
2567
David Benjamince079fd2016-08-02 16:22:34 -04002568 if (!SSL_is_dtls(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002569 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002570 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002571 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002572 *
2573 * NB: because this code works out the length of all existing extensions
2574 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002575 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002576 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002577 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002578 * Server 7.0 is intolerant to the last extension being zero-length. See
2579 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002580 if (padding_len >= 4 + 1) {
2581 padding_len -= 4;
2582 } else {
2583 padding_len = 1;
2584 }
Adam Langley95c29f32014-06-20 12:00:00 -07002585
Adam Langley33ad2b52015-07-20 17:43:53 -07002586 uint8_t *padding_bytes;
2587 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2588 !CBB_add_u16(&extensions, padding_len) ||
2589 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2590 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002591 }
Adam Langley75712922014-10-10 16:23:43 -07002592
Adam Langley33ad2b52015-07-20 17:43:53 -07002593 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002594 }
2595 }
Adam Langley75712922014-10-10 16:23:43 -07002596
David Benjamina01deee2015-12-08 18:56:31 -05002597 /* Discard empty extensions blocks. */
2598 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002599 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002600 }
2601
David Benjamine8d53502015-10-10 14:13:23 -04002602 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002603
2604err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002605 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002606 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002607}
Adam Langley95c29f32014-06-20 12:00:00 -07002608
David Benjamin56380462015-10-10 14:59:09 -04002609int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002610 CBB extensions;
2611 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002612 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002613 }
2614
2615 unsigned i;
2616 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002617 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002618 /* Don't send extensions that were not received. */
2619 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002620 }
Adam Langley95c29f32014-06-20 12:00:00 -07002621
David Benjamin56380462015-10-10 14:59:09 -04002622 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002623 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2624 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2625 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002626 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002627 }
Adam Langley95c29f32014-06-20 12:00:00 -07002628
David Benjamin56380462015-10-10 14:59:09 -04002629 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002630 goto err;
2631 }
2632
Steven Valdez143e8b32016-07-11 13:19:03 -04002633 /* Discard empty extensions blocks before TLS 1.3. */
2634 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2635 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002636 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002637 }
2638
David Benjamin56380462015-10-10 14:59:09 -04002639 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002640
2641err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002642 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002643 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002644}
Adam Langley95c29f32014-06-20 12:00:00 -07002645
David Benjamine14ff062016-08-09 16:21:24 -04002646static int ssl_scan_clienthello_tlsext(
2647 SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
2648 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002649 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002650 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002651 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002652 }
2653 }
2654
David Benjamin0d56f882015-12-19 17:05:56 -05002655 ssl->s3->tmp.extensions.received = 0;
2656 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002657
David Benjamine14ff062016-08-09 16:21:24 -04002658 CBS extensions;
2659 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2660 while (CBS_len(&extensions) != 0) {
2661 uint16_t type;
2662 CBS extension;
2663
2664 /* Decode the next extension. */
2665 if (!CBS_get_u16(&extensions, &type) ||
2666 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002667 *out_alert = SSL_AD_DECODE_ERROR;
2668 return 0;
2669 }
Adam Langley95c29f32014-06-20 12:00:00 -07002670
David Benjamine14ff062016-08-09 16:21:24 -04002671 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2672 * ambiguous. Ignore all but the renegotiation_info extension. */
2673 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2674 continue;
2675 }
Adam Langley95c29f32014-06-20 12:00:00 -07002676
David Benjamine14ff062016-08-09 16:21:24 -04002677 unsigned ext_index;
2678 const struct tls_extension *const ext =
2679 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002680
David Benjamine14ff062016-08-09 16:21:24 -04002681 if (ext == NULL) {
2682 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002683 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 return 0;
2685 }
David Benjamine14ff062016-08-09 16:21:24 -04002686 continue;
2687 }
2688
2689 ssl->s3->tmp.extensions.received |= (1u << ext_index);
2690 uint8_t alert = SSL_AD_DECODE_ERROR;
2691 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2692 *out_alert = alert;
2693 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2694 ERR_add_error_dataf("extension: %u", (unsigned)type);
2695 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002696 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002697 }
Adam Langley75712922014-10-10 16:23:43 -07002698
David Benjamin1deb41b2016-08-09 19:36:38 -04002699 for (size_t i = 0; i < kNumExtensions; i++) {
2700 if (ssl->s3->tmp.extensions.received & (1u << i)) {
2701 continue;
2702 }
2703
2704 CBS *contents = NULL, fake_contents;
2705 static const uint8_t kFakeRenegotiateExtension[] = {0};
2706 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2707 ssl_client_cipher_list_contains_cipher(client_hello,
2708 SSL3_CK_SCSV & 0xffff)) {
2709 /* The renegotiation SCSV was received so pretend that we received a
2710 * renegotiation extension. */
2711 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2712 sizeof(kFakeRenegotiateExtension));
2713 contents = &fake_contents;
2714 ssl->s3->tmp.extensions.received |= (1u << i);
2715 }
2716
2717 /* Extension wasn't observed so call the callback with a NULL
2718 * parameter. */
2719 uint8_t alert = SSL_AD_DECODE_ERROR;
2720 if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
2721 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2722 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2723 *out_alert = alert;
2724 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002725 }
2726 }
2727
Adam Langleyfcf25832014-12-18 17:42:32 -08002728 return 1;
2729}
Adam Langley95c29f32014-06-20 12:00:00 -07002730
David Benjamine14ff062016-08-09 16:21:24 -04002731int ssl_parse_clienthello_tlsext(
2732 SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002733 int alert = -1;
David Benjamine14ff062016-08-09 16:21:24 -04002734 if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002735 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002736 return 0;
2737 }
Adam Langley95c29f32014-06-20 12:00:00 -07002738
David Benjamin0d56f882015-12-19 17:05:56 -05002739 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002740 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002741 return 0;
2742 }
Adam Langley95c29f32014-06-20 12:00:00 -07002743
Adam Langleyfcf25832014-12-18 17:42:32 -08002744 return 1;
2745}
Adam Langley95c29f32014-06-20 12:00:00 -07002746
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002747OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2748
David Benjamin0d56f882015-12-19 17:05:56 -05002749static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002750 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2751 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2752 return 1;
2753 }
Adam Langley614c66a2015-06-12 15:26:58 -07002754
Steven Valdez143e8b32016-07-11 13:19:03 -04002755 /* Decode the extensions block and check it is valid. */
2756 CBS extensions;
2757 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2758 !tls1_check_duplicate_extensions(&extensions)) {
2759 *out_alert = SSL_AD_DECODE_ERROR;
2760 return 0;
2761 }
2762
2763 uint32_t received = 0;
2764 while (CBS_len(&extensions) != 0) {
2765 uint16_t type;
2766 CBS extension;
2767
2768 /* Decode the next extension. */
2769 if (!CBS_get_u16(&extensions, &type) ||
2770 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002771 *out_alert = SSL_AD_DECODE_ERROR;
2772 return 0;
2773 }
Adam Langley95c29f32014-06-20 12:00:00 -07002774
Steven Valdez143e8b32016-07-11 13:19:03 -04002775 unsigned ext_index;
2776 const struct tls_extension *const ext =
2777 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002778
Steven Valdez143e8b32016-07-11 13:19:03 -04002779 if (ext == NULL) {
2780 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002781 return 0;
2782 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002783 continue;
2784 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002785
David Benjamin1deb41b2016-08-09 19:36:38 -04002786 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index)) &&
2787 type != TLSEXT_TYPE_renegotiate) {
2788 /* If the extension was never sent then it is illegal, except for the
2789 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002790 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2791 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002792 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002793 return 0;
2794 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002795
Steven Valdez143e8b32016-07-11 13:19:03 -04002796 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002797
Steven Valdez143e8b32016-07-11 13:19:03 -04002798 uint8_t alert = SSL_AD_DECODE_ERROR;
2799 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2800 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2801 ERR_add_error_dataf("extension: %u", (unsigned)type);
2802 *out_alert = alert;
2803 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002804 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002805 }
Adam Langley95c29f32014-06-20 12:00:00 -07002806
Adam Langley33ad2b52015-07-20 17:43:53 -07002807 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002808 for (i = 0; i < kNumExtensions; i++) {
2809 if (!(received & (1u << i))) {
2810 /* Extension wasn't observed so call the callback with a NULL
2811 * parameter. */
2812 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002813 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002814 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2815 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002816 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002817 return 0;
2818 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002819 }
2820 }
Adam Langley95c29f32014-06-20 12:00:00 -07002821
Adam Langleyfcf25832014-12-18 17:42:32 -08002822 return 1;
2823}
Adam Langley95c29f32014-06-20 12:00:00 -07002824
David Benjamin0d56f882015-12-19 17:05:56 -05002825static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002826 int ret = SSL_TLSEXT_ERR_NOACK;
2827 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002828
David Benjamin78f8aab2016-03-10 16:33:58 -05002829 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002830 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002831 ssl->ctx->tlsext_servername_arg);
2832 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002833 ret = ssl->initial_ctx->tlsext_servername_callback(
2834 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002835 }
Adam Langley95c29f32014-06-20 12:00:00 -07002836
Adam Langleyfcf25832014-12-18 17:42:32 -08002837 switch (ret) {
2838 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002839 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002840 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002841
Adam Langleyfcf25832014-12-18 17:42:32 -08002842 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002843 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002844 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002845
Adam Langleyfcf25832014-12-18 17:42:32 -08002846 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002847 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002848 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002849
Adam Langleyfcf25832014-12-18 17:42:32 -08002850 default:
2851 return 1;
2852 }
2853}
Adam Langleyed8270a2014-09-02 13:52:56 -07002854
David Benjamin0d56f882015-12-19 17:05:56 -05002855static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002856 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002857 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002858
David Benjamin78f8aab2016-03-10 16:33:58 -05002859 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002860 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002861 ssl->ctx->tlsext_servername_arg);
2862 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002863 ret = ssl->initial_ctx->tlsext_servername_callback(
2864 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002865 }
Adam Langley95c29f32014-06-20 12:00:00 -07002866
Adam Langleyfcf25832014-12-18 17:42:32 -08002867 switch (ret) {
2868 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002869 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002870 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002871
Adam Langleyfcf25832014-12-18 17:42:32 -08002872 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002873 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002874 return 1;
2875
2876 default:
2877 return 1;
2878 }
2879}
2880
David Benjamin0d56f882015-12-19 17:05:56 -05002881int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002882 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002883 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2884 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002885 return 0;
2886 }
2887
David Benjamin0d56f882015-12-19 17:05:56 -05002888 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002889 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002890 return 0;
2891 }
2892
2893 return 1;
2894}
Adam Langley95c29f32014-06-20 12:00:00 -07002895
David Benjamine3aa1d92015-06-16 15:34:50 -04002896int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002897 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002898 size_t ticket_len, const uint8_t *session_id,
2899 size_t session_id_len) {
2900 int ret = 1; /* Most errors are non-fatal. */
2901 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2902 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002903
David Benjamine3aa1d92015-06-16 15:34:50 -04002904 HMAC_CTX hmac_ctx;
2905 HMAC_CTX_init(&hmac_ctx);
2906 EVP_CIPHER_CTX cipher_ctx;
2907 EVP_CIPHER_CTX_init(&cipher_ctx);
2908
David Benjaminef1b0092015-11-21 14:05:44 -05002909 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002910 *out_session = NULL;
2911
Steven Valdez4aa154e2016-07-29 14:32:55 -04002912 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
2913 goto done;
2914 }
2915
David Benjamine3aa1d92015-06-16 15:34:50 -04002916 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2917 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002918 }
2919
David Benjaminadcc3952015-04-26 13:07:57 -04002920 /* Ensure there is room for the key name and the largest IV
2921 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2922 * the maximum IV length should be well under the minimum size for the
2923 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002924 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2925 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002926 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002927 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002928
David Benjamine3aa1d92015-06-16 15:34:50 -04002929 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002930 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2931 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2932 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002933 if (cb_ret < 0) {
2934 ret = 0;
2935 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002936 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002937 if (cb_ret == 0) {
2938 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002939 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002940 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002941 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002942 }
2943 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002944 /* Check the key name matches. */
2945 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2946 SSL_TICKET_KEY_NAME_LEN) != 0) {
2947 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002948 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002949 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2950 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002951 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002952 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2953 ssl_ctx->tlsext_tick_aes_key, iv)) {
2954 ret = 0;
2955 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002956 }
2957 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002958 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002959
David Benjamine3aa1d92015-06-16 15:34:50 -04002960 /* Check the MAC at the end of the ticket. */
2961 uint8_t mac[EVP_MAX_MD_SIZE];
2962 size_t mac_len = HMAC_size(&hmac_ctx);
2963 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002964 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002965 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002966 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002967 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2968 HMAC_Final(&hmac_ctx, mac, NULL);
2969 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2970 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002971 }
2972
David Benjamine3aa1d92015-06-16 15:34:50 -04002973 /* Decrypt the session data. */
2974 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2975 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2976 mac_len;
2977 plaintext = OPENSSL_malloc(ciphertext_len);
2978 if (plaintext == NULL) {
2979 ret = 0;
2980 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002981 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002982 if (ciphertext_len >= INT_MAX) {
2983 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002984 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002985 int len1, len2;
2986 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2987 (int)ciphertext_len) ||
2988 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2989 ERR_clear_error(); /* Don't leave an error on the queue. */
2990 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002991 }
2992
David Benjamine3aa1d92015-06-16 15:34:50 -04002993 /* Decode the session. */
2994 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2995 if (session == NULL) {
2996 ERR_clear_error(); /* Don't leave an error on the queue. */
2997 goto done;
2998 }
2999
3000 /* Copy the client's session ID into the new session, to denote the ticket has
3001 * been accepted. */
3002 memcpy(session->session_id, session_id, session_id_len);
3003 session->session_id_length = session_id_len;
3004
Steven Valdez4aa154e2016-07-29 14:32:55 -04003005 if (!ssl_session_is_context_valid(ssl, session) ||
3006 !ssl_session_is_time_valid(ssl, session)) {
3007 SSL_SESSION_free(session);
3008 session = NULL;
3009 }
3010
David Benjamine3aa1d92015-06-16 15:34:50 -04003011 *out_session = session;
3012
3013done:
3014 OPENSSL_free(plaintext);
3015 HMAC_CTX_cleanup(&hmac_ctx);
3016 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3017 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003018}
Adam Langley95c29f32014-06-20 12:00:00 -07003019
Steven Valdez0d62f262015-09-04 12:41:04 -04003020int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003021 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05003022 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003023 return 1;
3024 }
David Benjamincd996942014-07-20 16:23:51 -04003025
David Benjamin0fc37ef2016-08-17 15:29:46 -04003026 SSL_HANDSHAKE *hs = ssl->s3->hs;
3027 OPENSSL_free(hs->peer_sigalgs);
3028 hs->peer_sigalgs = NULL;
3029 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003030
3031 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003032 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003033 return 0;
3034 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003035 num_sigalgs /= 2;
3036
3037 /* supported_signature_algorithms in the certificate request is
3038 * allowed to be empty. */
3039 if (num_sigalgs == 0) {
3040 return 1;
3041 }
3042
Steven Valdez02563852016-06-23 13:33:05 -04003043 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3044 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003045 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3046 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003047 return 0;
3048 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003049 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003050
3051 CBS sigalgs;
3052 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003053 for (size_t i = 0; i < num_sigalgs; i++) {
3054 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003055 return 0;
3056 }
3057 }
Adam Langley95c29f32014-06-20 12:00:00 -07003058
Adam Langleyfcf25832014-12-18 17:42:32 -08003059 return 1;
3060}
David Benjaminec2f27d2014-11-13 19:17:25 -05003061
David Benjaminea9a0d52016-07-08 15:52:59 -07003062int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003063 CERT *cert = ssl->cert;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003064 SSL_HANDSHAKE *hs = ssl->s3->hs;
David Benjaminec2f27d2014-11-13 19:17:25 -05003065
Steven Valdezf0451ca2016-06-29 13:16:27 -04003066 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3067 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3068 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003069 int type = ssl_private_key_type(ssl);
3070 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003071 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003072 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003073 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003074 if (ssl_is_ecdsa_key_type(type)) {
3075 *out = SSL_SIGN_ECDSA_SHA1;
3076 return 1;
3077 }
3078 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3079 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003080 }
3081
Steven Valdezeff1e8d2016-07-06 14:24:47 -04003082 const uint16_t *sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003083 size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07003084 if (cert->sigalgs != NULL) {
3085 sigalgs = cert->sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003086 num_sigalgs = cert->num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003087 }
3088
David Benjamin0fc37ef2016-08-17 15:29:46 -04003089 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3090 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3091 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003092 /* If the client didn't specify any signature_algorithms extension then
3093 * we can assume that it supports SHA1. See
3094 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3095 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3096 SSL_SIGN_ECDSA_SHA1};
3097 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003098 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003099 }
3100
David Benjamin0fc37ef2016-08-17 15:29:46 -04003101 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003102 uint16_t sigalg = sigalgs[i];
3103 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3104 * negotiated. */
3105 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3106 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3107 continue;
3108 }
3109
David Benjamin0fc37ef2016-08-17 15:29:46 -04003110 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003111 if (sigalg == peer_sigalgs[j]) {
3112 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003113 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003114 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003115 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003116 }
Adam Langley95c29f32014-06-20 12:00:00 -07003117
David Benjaminea9a0d52016-07-08 15:52:59 -07003118 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3119 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003120}
Adam Langley95c29f32014-06-20 12:00:00 -07003121
David Benjamind6a4ae92015-08-06 11:10:51 -04003122int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
3123 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003124 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003125
3126 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003127 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
3128 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08003129 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003130
David Benjamind6a4ae92015-08-06 11:10:51 -04003131 static const char kClientIDMagic[] = "TLS Channel ID signature";
3132 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
3133
Steven Valdez87eab492016-06-27 16:34:59 -04003134 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003135 static const char kResumptionMagic[] = "Resumption";
3136 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
3137 if (ssl->session->original_handshake_hash_len == 0) {
3138 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3139 goto err;
3140 }
3141 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
3142 ssl->session->original_handshake_hash_len);
3143 }
3144
3145 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3146 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3147 sizeof(handshake_hash));
3148 if (handshake_hash_len < 0) {
3149 goto err;
3150 }
3151 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
3152 unsigned len_u;
3153 EVP_DigestFinal_ex(&ctx, out, &len_u);
3154 *out_len = len_u;
3155
3156 ret = 1;
3157
3158err:
3159 EVP_MD_CTX_cleanup(&ctx);
3160 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003161}
Adam Langley1258b6a2014-06-20 12:00:00 -07003162
3163/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003164 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003165 * data. */
3166int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003167 int digest_len;
3168 /* This function should never be called for a resumed session because the
3169 * handshake hashes that we wish to record are for the original, full
3170 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003171 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003172 return -1;
3173 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003174
Adam Langleyfcf25832014-12-18 17:42:32 -08003175 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003176 tls1_handshake_digest(
3177 ssl, ssl->s3->new_session->original_handshake_hash,
3178 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003179 if (digest_len < 0) {
3180 return -1;
3181 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003182
Steven Valdez87eab492016-06-27 16:34:59 -04003183 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003184
Adam Langleyfcf25832014-12-18 17:42:32 -08003185 return 1;
3186}