blob: 5871be2365536982040d406b01fd66bc6dc4f6ed [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin03973092014-06-24 23:27:17 -0400116#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400117#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/evp.h>
120#include <openssl/hmac.h>
121#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400122#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/rand.h>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700124#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400127#include "../crypto/internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800128
129
David Benjamin0d56f882015-12-19 17:05:56 -0500130static int ssl_check_clienthello_tlsext(SSL *ssl);
131static int ssl_check_serverhello_tlsext(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700132
Adam Langleyfcf25832014-12-18 17:42:32 -0800133static int compare_uint16_t(const void *p1, const void *p2) {
134 uint16_t u1 = *((const uint16_t *)p1);
135 uint16_t u2 = *((const uint16_t *)p2);
136 if (u1 < u2) {
137 return -1;
138 } else if (u1 > u2) {
139 return 1;
140 } else {
141 return 0;
142 }
143}
David Benjamin35a7a442014-07-05 00:23:20 -0400144
Adam Langleyfcf25832014-12-18 17:42:32 -0800145/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
146 * more than one extension of the same type in a ClientHello or ServerHello.
147 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400148 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800149static int tls1_check_duplicate_extensions(const CBS *cbs) {
150 CBS extensions = *cbs;
151 size_t num_extensions = 0, i = 0;
152 uint16_t *extension_types = NULL;
153 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400154
Adam Langleyfcf25832014-12-18 17:42:32 -0800155 /* First pass: count the extensions. */
156 while (CBS_len(&extensions) > 0) {
157 uint16_t type;
158 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400159
Adam Langleyfcf25832014-12-18 17:42:32 -0800160 if (!CBS_get_u16(&extensions, &type) ||
161 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
162 goto done;
163 }
David Benjamin35a7a442014-07-05 00:23:20 -0400164
Adam Langleyfcf25832014-12-18 17:42:32 -0800165 num_extensions++;
166 }
David Benjamin35a7a442014-07-05 00:23:20 -0400167
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 if (num_extensions == 0) {
169 return 1;
170 }
David Benjamin9a373592014-07-25 04:27:53 -0400171
Brian Smith5ba06892016-02-07 09:36:04 -1000172 extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400174 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800175 goto done;
176 }
David Benjamin35a7a442014-07-05 00:23:20 -0400177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 /* Second pass: gather the extension types. */
179 extensions = *cbs;
180 for (i = 0; i < num_extensions; i++) {
181 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400182
Adam Langleyfcf25832014-12-18 17:42:32 -0800183 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
184 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
185 /* This should not happen. */
186 goto done;
187 }
188 }
189 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400190
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 /* Sort the extensions and make sure there are no duplicates. */
192 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
193 for (i = 1; i < num_extensions; i++) {
194 if (extension_types[i - 1] == extension_types[i]) {
195 goto done;
196 }
197 }
David Benjamin35a7a442014-07-05 00:23:20 -0400198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 ret = 1;
200
David Benjamin35a7a442014-07-05 00:23:20 -0400201done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400202 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 return ret;
204}
David Benjamin35a7a442014-07-05 00:23:20 -0400205
David Benjamind7573dc2016-07-20 19:05:22 +0200206int ssl_early_callback_init(SSL *ssl, struct ssl_early_callback_ctx *ctx,
207 const uint8_t *in, size_t in_len) {
208 memset(ctx, 0, sizeof(*ctx));
209 ctx->ssl = ssl;
210 ctx->client_hello = in;
211 ctx->client_hello_len = in_len;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400212
David Benjamine14ff062016-08-09 16:21:24 -0400213 CBS client_hello, random, session_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
David Benjamine14ff062016-08-09 16:21:24 -0400215 if (!CBS_get_u16(&client_hello, &ctx->version) ||
216 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
217 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
218 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 return 0;
220 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700221
David Benjamine14ff062016-08-09 16:21:24 -0400222 ctx->random = CBS_data(&random);
223 ctx->random_len = CBS_len(&random);
Adam Langleyfcf25832014-12-18 17:42:32 -0800224 ctx->session_id = CBS_data(&session_id);
225 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700226
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 /* Skip past DTLS cookie */
David Benjamince079fd2016-08-02 16:22:34 -0400228 if (SSL_is_dtls(ctx->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400230 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
231 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 return 0;
233 }
234 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700235
David Benjamine14ff062016-08-09 16:21:24 -0400236 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400238 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
239 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 CBS_len(&compression_methods) < 1) {
241 return 0;
242 }
David Benjamine14ff062016-08-09 16:21:24 -0400243
244 ctx->cipher_suites = CBS_data(&cipher_suites);
245 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 ctx->compression_methods = CBS_data(&compression_methods);
247 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 /* If the ClientHello ends here then it's valid, but doesn't have any
250 * extensions. (E.g. SSLv3.) */
251 if (CBS_len(&client_hello) == 0) {
252 ctx->extensions = NULL;
253 ctx->extensions_len = 0;
254 return 1;
255 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 /* Extract extensions and check it is valid. */
David Benjamine14ff062016-08-09 16:21:24 -0400258 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
260 !tls1_check_duplicate_extensions(&extensions) ||
261 CBS_len(&client_hello) != 0) {
262 return 0;
263 }
David Benjamine14ff062016-08-09 16:21:24 -0400264
Adam Langleyfcf25832014-12-18 17:42:32 -0800265 ctx->extensions = CBS_data(&extensions);
266 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700267
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 return 1;
269}
Adam Langleydc9b1412014-06-20 12:00:00 -0700270
David Benjamincec73442016-08-02 17:41:33 -0400271int ssl_early_callback_get_extension(const struct ssl_early_callback_ctx *ctx,
272 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 while (CBS_len(&extensions) != 0) {
David Benjamincec73442016-08-02 17:41:33 -0400276 /* Decode the next extension. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 uint16_t type;
278 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 if (!CBS_get_u16(&extensions, &type) ||
280 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
281 return 0;
282 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400285 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800286 return 1;
287 }
288 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700289
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 return 0;
291}
Adam Langley95c29f32014-06-20 12:00:00 -0700292
David Benjamincec73442016-08-02 17:41:33 -0400293int SSL_early_callback_ctx_extension_get(
294 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
295 const uint8_t **out_data, size_t *out_len) {
296 CBS cbs;
297 if (!ssl_early_callback_get_extension(ctx, &cbs, extension_type)) {
298 return 0;
299 }
300
301 *out_data = CBS_data(&cbs);
302 *out_len = CBS_len(&cbs);
303 return 1;
304}
305
Steven Valdezce902a92016-05-17 11:47:53 -0400306static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400307 SSL_CURVE_X25519,
308 SSL_CURVE_SECP256R1,
309 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700310#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin9e68f192016-06-30 14:55:33 -0400311 SSL_CURVE_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700312#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800313};
Adam Langley95c29f32014-06-20 12:00:00 -0700314
Steven Valdez5440fe02016-07-18 12:40:30 -0400315void tls1_get_grouplist(SSL *ssl, int get_peer_groups,
316 const uint16_t **out_group_ids,
317 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400318 if (get_peer_groups) {
319 /* Only clients send a supported group list, so this function is only
320 * called on the server. */
David Benjamin0d56f882015-12-19 17:05:56 -0500321 assert(ssl->server);
Steven Valdezce902a92016-05-17 11:47:53 -0400322 *out_group_ids = ssl->s3->tmp.peer_supported_group_list;
323 *out_group_ids_len = ssl->s3->tmp.peer_supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800324 return;
325 }
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Steven Valdezce902a92016-05-17 11:47:53 -0400327 *out_group_ids = ssl->supported_group_list;
328 *out_group_ids_len = ssl->supported_group_list_len;
329 if (!*out_group_ids) {
330 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400331 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 }
333}
David Benjamined439582014-07-14 19:13:02 -0400334
Steven Valdezce902a92016-05-17 11:47:53 -0400335int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) {
336 const uint16_t *groups, *peer_groups, *pref, *supp;
337 size_t groups_len, peer_groups_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400338
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 /* Can't do anything on client side */
David Benjamin4298d772015-12-19 00:18:25 -0500340 if (ssl->server == 0) {
341 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800342 }
343
Steven Valdezce902a92016-05-17 11:47:53 -0400344 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
345 tls1_get_grouplist(ssl, 1 /* peer groups */, &peer_groups, &peer_groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800346
Steven Valdezce902a92016-05-17 11:47:53 -0400347 if (peer_groups_len == 0) {
348 /* Clients are not required to send a supported_groups extension. In this
349 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin4298d772015-12-19 00:18:25 -0500350 * section 4, paragraph 3.
351 *
352 * However, in the interests of compatibility, we will skip ECDH if the
353 * client didn't send an extension because we can't be sure that they'll
Steven Valdezce902a92016-05-17 11:47:53 -0400354 * support our favoured group. */
David Benjamin4298d772015-12-19 00:18:25 -0500355 return 0;
David Benjamin55a43642015-04-20 14:45:55 -0400356 }
357
David Benjamin4298d772015-12-19 00:18:25 -0500358 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400359 pref = groups;
360 pref_len = groups_len;
361 supp = peer_groups;
362 supp_len = peer_groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400363 } else {
Steven Valdezce902a92016-05-17 11:47:53 -0400364 pref = peer_groups;
365 pref_len = peer_groups_len;
366 supp = groups;
367 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400368 }
369
370 for (i = 0; i < pref_len; i++) {
371 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800372 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400373 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500374 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 }
376 }
377 }
378
David Benjamin4298d772015-12-19 00:18:25 -0500379 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800380}
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Steven Valdezce902a92016-05-17 11:47:53 -0400382int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400384 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800385
Steven Valdezce902a92016-05-17 11:47:53 -0400386 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
387 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 return 0;
389 }
390
David Benjamin54091232016-09-05 12:47:25 -0400391 for (size_t i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400392 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
393 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 return 0;
395 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 }
397
Steven Valdezce902a92016-05-17 11:47:53 -0400398 OPENSSL_free(*out_group_ids);
399 *out_group_ids = group_ids;
400 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800401
402 return 1;
403}
Adam Langley95c29f32014-06-20 12:00:00 -0700404
Steven Valdezce902a92016-05-17 11:47:53 -0400405/* tls1_curve_params_from_ec_key sets |*out_group_id| and |*out_comp_id| to the
406 * TLS group ID and point format, respectively, for |ec|. It returns one on
David Benjamin072334d2014-07-13 16:24:27 -0400407 * success and zero on failure. */
Steven Valdezce902a92016-05-17 11:47:53 -0400408static int tls1_curve_params_from_ec_key(uint16_t *out_group_id,
Adam Langleyfcf25832014-12-18 17:42:32 -0800409 uint8_t *out_comp_id, EC_KEY *ec) {
410 int nid;
411 uint16_t id;
412 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700413
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 if (ec == NULL) {
415 return 0;
416 }
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 grp = EC_KEY_get0_group(ec);
419 if (grp == NULL) {
420 return 0;
421 }
David Benjamin072334d2014-07-13 16:24:27 -0400422
Steven Valdezce902a92016-05-17 11:47:53 -0400423 /* Determine group ID */
Adam Langleyfcf25832014-12-18 17:42:32 -0800424 nid = EC_GROUP_get_curve_name(grp);
Steven Valdezce902a92016-05-17 11:47:53 -0400425 if (!ssl_nid_to_group_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800426 return 0;
427 }
David Benjamin072334d2014-07-13 16:24:27 -0400428
Steven Valdezce902a92016-05-17 11:47:53 -0400429 /* Set the named group ID. Arbitrary explicit groups are not supported. */
430 *out_group_id = id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800431
432 if (out_comp_id) {
433 if (EC_KEY_get0_public_key(ec) == NULL) {
434 return 0;
435 }
436 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
437 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
438 } else {
439 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
440 }
441 }
442
443 return 1;
444}
David Benjamin072334d2014-07-13 16:24:27 -0400445
Steven Valdezce902a92016-05-17 11:47:53 -0400446/* tls1_check_group_id returns one if |group_id| is consistent with both our
447 * and the peer's group preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400448 * preferences are checked; the peer (the server) does not send preferences. */
Steven Valdezce902a92016-05-17 11:47:53 -0400449int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
450 const uint16_t *groups;
451 size_t groups_len, i, get_peer_groups;
David Benjamin42e9a772014-09-02 23:18:44 -0400452
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 /* Check against our list, then the peer's list. */
Steven Valdezce902a92016-05-17 11:47:53 -0400454 for (get_peer_groups = 0; get_peer_groups <= 1; get_peer_groups++) {
455 if (get_peer_groups && !ssl->server) {
David Benjamin55a43642015-04-20 14:45:55 -0400456 /* Servers do not present a preference list so, if we are a client, only
457 * check our list. */
458 continue;
459 }
460
Steven Valdezce902a92016-05-17 11:47:53 -0400461 tls1_get_grouplist(ssl, get_peer_groups, &groups, &groups_len);
462 if (get_peer_groups && groups_len == 0) {
463 /* Clients are not required to send a supported_groups extension. In this
464 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin55a43642015-04-20 14:45:55 -0400465 * section 4, paragraph 3. */
466 continue;
467 }
Steven Valdezce902a92016-05-17 11:47:53 -0400468 for (i = 0; i < groups_len; i++) {
469 if (groups[i] == group_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800470 break;
471 }
472 }
Adam Langley95c29f32014-06-20 12:00:00 -0700473
Steven Valdezce902a92016-05-17 11:47:53 -0400474 if (i == groups_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 return 0;
476 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800477 }
David Benjamin033e5f42014-11-13 18:47:41 -0500478
Adam Langleyfcf25832014-12-18 17:42:32 -0800479 return 1;
480}
David Benjamin033e5f42014-11-13 18:47:41 -0500481
David Benjamin0d56f882015-12-19 17:05:56 -0500482int tls1_check_ec_cert(SSL *ssl, X509 *x) {
David Benjamin75ea5bb2016-07-08 17:43:29 -0700483 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
484 /* In TLS 1.3, the ECDSA curve is negotiated via signature algorithms. */
485 return 1;
486 }
487
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 EVP_PKEY *pkey = X509_get_pubkey(x);
David Benjamin75ea5bb2016-07-08 17:43:29 -0700489 if (pkey == NULL) {
490 return 0;
491 }
492
493 int ret = 0;
Steven Valdezce902a92016-05-17 11:47:53 -0400494 uint16_t group_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 uint8_t comp_id;
David Benjamin758d1272015-11-20 17:47:25 -0500496 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
497 if (ec_key == NULL ||
Steven Valdezce902a92016-05-17 11:47:53 -0400498 !tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) ||
499 !tls1_check_group_id(ssl, group_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400500 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 goto done;
502 }
503
504 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500505
506done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400507 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800508 return ret;
509}
David Benjamin42e9a772014-09-02 23:18:44 -0400510
Adam Langley95c29f32014-06-20 12:00:00 -0700511/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800512 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700513
Steven Valdez02563852016-06-23 13:33:05 -0400514static const uint16_t kDefaultSignatureAlgorithms[] = {
David Benjamin57e929f2016-08-30 00:30:38 -0400515 /* For now, do not ship RSA-PSS signature algorithms on Android's system
516 * BoringSSL. Once TLS 1.3 is finalized and the change in Chrome has stuck,
517 * restore them. */
518#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400519 SSL_SIGN_RSA_PSS_SHA512,
David Benjamin57e929f2016-08-30 00:30:38 -0400520#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400521 SSL_SIGN_RSA_PKCS1_SHA512,
522 SSL_SIGN_ECDSA_SECP521R1_SHA512,
523
David Benjamin57e929f2016-08-30 00:30:38 -0400524#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400525 SSL_SIGN_RSA_PSS_SHA384,
David Benjamin57e929f2016-08-30 00:30:38 -0400526#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400527 SSL_SIGN_RSA_PKCS1_SHA384,
528 SSL_SIGN_ECDSA_SECP384R1_SHA384,
529
David Benjamin57e929f2016-08-30 00:30:38 -0400530#if !defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400531 SSL_SIGN_RSA_PSS_SHA256,
David Benjamin57e929f2016-08-30 00:30:38 -0400532#endif
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400533 SSL_SIGN_RSA_PKCS1_SHA256,
534 SSL_SIGN_ECDSA_SECP256R1_SHA256,
535
536 SSL_SIGN_RSA_PKCS1_SHA1,
537 SSL_SIGN_ECDSA_SHA1,
538};
539
Steven Valdez02563852016-06-23 13:33:05 -0400540size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
541 *psigs = kDefaultSignatureAlgorithms;
Steven Valdezcb966542016-08-17 16:56:14 -0400542 return OPENSSL_ARRAY_SIZE(kDefaultSignatureAlgorithms);
Adam Langleyfcf25832014-12-18 17:42:32 -0800543}
Adam Langley95c29f32014-06-20 12:00:00 -0700544
David Benjamin887c3002016-07-08 16:15:32 -0700545int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
Steven Valdez02563852016-06-23 13:33:05 -0400546 const uint16_t *sent_sigs;
547 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800548
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500550 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400551 for (i = 0; i < sent_sigslen; i++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400552 if (sigalg == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 break;
554 }
555 }
556
David Benjamin788be4a2015-10-30 17:50:57 -0400557 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400558 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
560 return 0;
561 }
562
Adam Langleyfcf25832014-12-18 17:42:32 -0800563 return 1;
564}
565
566/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
567 * supported or doesn't appear in supported signature algorithms. Unlike
568 * ssl_cipher_get_disabled this applies to a specific session and not global
569 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500570void ssl_set_client_disabled(SSL *ssl) {
571 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800572 int have_rsa = 0, have_ecdsa = 0;
573 c->mask_a = 0;
574 c->mask_k = 0;
575
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 /* Now go through all signature algorithms seeing if we support any for RSA,
577 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin0fc37ef2016-08-17 15:29:46 -0400578 const uint16_t *sigalgs;
579 size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
580 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -0400581 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400582 case SSL_SIGN_RSA_PSS_SHA512:
583 case SSL_SIGN_RSA_PSS_SHA384:
584 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400585 case SSL_SIGN_RSA_PKCS1_SHA512:
586 case SSL_SIGN_RSA_PKCS1_SHA384:
587 case SSL_SIGN_RSA_PKCS1_SHA256:
588 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800589 have_rsa = 1;
590 break;
591
Steven Valdez02563852016-06-23 13:33:05 -0400592 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
593 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
594 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
595 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 have_ecdsa = 1;
597 break;
598 }
599 }
600
601 /* Disable auth if we don't include any appropriate signature algorithms. */
602 if (!have_rsa) {
603 c->mask_a |= SSL_aRSA;
604 }
605 if (!have_ecdsa) {
606 c->mask_a |= SSL_aECDSA;
607 }
608
609 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500610 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 c->mask_a |= SSL_aPSK;
612 c->mask_k |= SSL_kPSK;
613 }
614}
Adam Langley95c29f32014-06-20 12:00:00 -0700615
Adam Langley614c66a2015-06-12 15:26:58 -0700616/* tls_extension represents a TLS extension that is handled internally. The
617 * |init| function is called for each handshake, before any other functions of
618 * the extension. Then the add and parse callbacks are called as needed.
619 *
620 * The parse callbacks receive a |CBS| that contains the contents of the
621 * extension (i.e. not including the type and length bytes). If an extension is
622 * not received then the parse callbacks will be called with a NULL CBS so that
623 * they can do any processing needed to handle the absence of an extension.
624 *
625 * The add callbacks receive a |CBB| to which the extension can be appended but
626 * the function is responsible for appending the type and length bytes too.
627 *
628 * All callbacks return one for success and zero for error. If a parse function
629 * returns zero then a fatal alert with value |*out_alert| will be sent. If
630 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
631struct tls_extension {
632 uint16_t value;
633 void (*init)(SSL *ssl);
634
635 int (*add_clienthello)(SSL *ssl, CBB *out);
636 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
637
638 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
639 int (*add_serverhello)(SSL *ssl, CBB *out);
640};
641
Steven Valdez6b8509a2016-07-12 13:38:32 -0400642static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
643 if (contents != NULL) {
644 /* Servers MUST NOT send this extension. */
645 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
646 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
647 return 0;
648 }
649
650 return 1;
651}
652
653static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
654 /* This extension from the client is handled elsewhere. */
655 return 1;
656}
657
658static int dont_add_serverhello(SSL *ssl, CBB *out) {
659 return 1;
660}
Adam Langley614c66a2015-06-12 15:26:58 -0700661
662/* Server name indication (SNI).
663 *
664 * https://tools.ietf.org/html/rfc6066#section-3. */
665
666static void ext_sni_init(SSL *ssl) {
667 ssl->s3->tmp.should_ack_sni = 0;
668}
669
670static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
671 if (ssl->tlsext_hostname == NULL) {
672 return 1;
673 }
674
675 CBB contents, server_name_list, name;
676 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
677 !CBB_add_u16_length_prefixed(out, &contents) ||
678 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
679 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
680 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
681 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
682 strlen(ssl->tlsext_hostname)) ||
683 !CBB_flush(out)) {
684 return 0;
685 }
686
687 return 1;
688}
689
David Benjamin0d56f882015-12-19 17:05:56 -0500690static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
691 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700692 if (contents == NULL) {
693 return 1;
694 }
695
696 if (CBS_len(contents) != 0) {
697 return 0;
698 }
699
700 assert(ssl->tlsext_hostname != NULL);
701
Steven Valdez87eab492016-06-27 16:34:59 -0400702 if (ssl->session == NULL) {
703 assert(ssl->s3->new_session->tlsext_hostname == NULL);
704 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
705 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700706 *out_alert = SSL_AD_INTERNAL_ERROR;
707 return 0;
708 }
709 }
710
711 return 1;
712}
713
David Benjamin0d56f882015-12-19 17:05:56 -0500714static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
715 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700716 if (contents == NULL) {
717 return 1;
718 }
719
David Benjamin9b611e22016-03-03 08:48:30 -0500720 CBS server_name_list, host_name;
721 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700722 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500723 !CBS_get_u8(&server_name_list, &name_type) ||
724 /* Although the server_name extension was intended to be extensible to
725 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
726 * different name types will cause an error. Further, RFC 4366 originally
727 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
728 * adding new name types is no longer feasible.
729 *
730 * Act as if the extensibility does not exist to simplify parsing. */
731 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
732 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700733 CBS_len(contents) != 0) {
734 return 0;
735 }
736
David Benjamin9b611e22016-03-03 08:48:30 -0500737 if (name_type != TLSEXT_NAMETYPE_host_name ||
738 CBS_len(&host_name) == 0 ||
739 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
740 CBS_contains_zero_byte(&host_name)) {
741 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
742 return 0;
743 }
Adam Langley614c66a2015-06-12 15:26:58 -0700744
David Benjamin9b611e22016-03-03 08:48:30 -0500745 /* TODO(davidben): SNI should be resolved before resumption. We have the
746 * early callback as a replacement, but we should fix the current callback
747 * and avoid the need for |SSL_CTX_set_session_id_context|. */
Steven Valdez87eab492016-06-27 16:34:59 -0400748 if (ssl->session == NULL) {
749 assert(ssl->s3->new_session->tlsext_hostname == NULL);
David Benjamin9b611e22016-03-03 08:48:30 -0500750
751 /* Copy the hostname as a string. */
Steven Valdez87eab492016-06-27 16:34:59 -0400752 if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) {
David Benjamin9b611e22016-03-03 08:48:30 -0500753 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700754 return 0;
755 }
756
David Benjamin9b611e22016-03-03 08:48:30 -0500757 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700758 }
759
760 return 1;
761}
762
763static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez87eab492016-06-27 16:34:59 -0400764 if (ssl->session != NULL ||
Adam Langley614c66a2015-06-12 15:26:58 -0700765 !ssl->s3->tmp.should_ack_sni ||
Steven Valdez87eab492016-06-27 16:34:59 -0400766 ssl->s3->new_session->tlsext_hostname == NULL) {
Adam Langley614c66a2015-06-12 15:26:58 -0700767 return 1;
768 }
769
770 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
771 !CBB_add_u16(out, 0 /* length */)) {
772 return 0;
773 }
774
775 return 1;
776}
777
778
Adam Langley5021b222015-06-12 18:27:58 -0700779/* Renegotiation indication.
780 *
781 * https://tools.ietf.org/html/rfc5746 */
782
783static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400784 uint16_t min_version, max_version;
785 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
786 return 0;
787 }
788
789 /* Renegotiation indication is not necessary in TLS 1.3. */
790 if (min_version >= TLS1_3_VERSION) {
791 return 1;
792 }
793
Adam Langley5021b222015-06-12 18:27:58 -0700794 CBB contents, prev_finished;
795 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
796 !CBB_add_u16_length_prefixed(out, &contents) ||
797 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
798 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
799 ssl->s3->previous_client_finished_len) ||
800 !CBB_flush(out)) {
801 return 0;
802 }
803
804 return 1;
805}
806
807static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
808 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400809 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
810 return 0;
811 }
812
David Benjamin3e052de2015-11-25 20:10:31 -0500813 /* Servers may not switch between omitting the extension and supporting it.
814 * See RFC 5746, sections 3.5 and 4.2. */
815 if (ssl->s3->initial_handshake_complete &&
816 (contents != NULL) != ssl->s3->send_connection_binding) {
817 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
818 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
819 return 0;
820 }
821
Adam Langley5021b222015-06-12 18:27:58 -0700822 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500823 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700824 * RI even on initial ServerHello because the client doesn't see any
825 * renegotiation during an attack. However this would mean we could not
826 * connect to any server which doesn't support RI.
827 *
David Benjamine9cddb82015-11-23 14:36:40 -0500828 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
829 * practical terms every client sets it so it's just assumed here. */
830 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700831 }
832
833 const size_t expected_len = ssl->s3->previous_client_finished_len +
834 ssl->s3->previous_server_finished_len;
835
836 /* Check for logic errors */
837 assert(!expected_len || ssl->s3->previous_client_finished_len);
838 assert(!expected_len || ssl->s3->previous_server_finished_len);
839
840 /* Parse out the extension contents. */
841 CBS renegotiated_connection;
842 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
843 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400844 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700845 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
846 return 0;
847 }
848
849 /* Check that the extension matches. */
850 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400851 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700852 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
853 return 0;
854 }
855
856 const uint8_t *d = CBS_data(&renegotiated_connection);
857 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
858 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400859 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700860 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
861 return 0;
862 }
863 d += ssl->s3->previous_client_finished_len;
864
865 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
866 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400867 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700868 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
869 return 0;
870 }
871 ssl->s3->send_connection_binding = 1;
872
873 return 1;
874}
875
876static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
877 CBS *contents) {
878 /* Renegotiation isn't supported as a server so this function should never be
879 * called after the initial handshake. */
880 assert(!ssl->s3->initial_handshake_complete);
881
Steven Valdez143e8b32016-07-11 13:19:03 -0400882 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
883 return 1;
884 }
885
Adam Langley5021b222015-06-12 18:27:58 -0700886 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400887 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700888 }
889
890 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700891 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
892 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400893 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700894 return 0;
895 }
896
897 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500898 if (!CBS_mem_equal(&renegotiated_connection,
899 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700900 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400901 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700902 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
903 return 0;
904 }
905
906 ssl->s3->send_connection_binding = 1;
907
908 return 1;
909}
910
911static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400912 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
913 return 1;
914 }
915
Adam Langley5021b222015-06-12 18:27:58 -0700916 CBB contents, prev_finished;
917 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
918 !CBB_add_u16_length_prefixed(out, &contents) ||
919 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
920 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
921 ssl->s3->previous_client_finished_len) ||
922 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
923 ssl->s3->previous_server_finished_len) ||
924 !CBB_flush(out)) {
925 return 0;
926 }
927
928 return 1;
929}
930
Adam Langley0a056712015-07-01 15:03:33 -0700931
932/* Extended Master Secret.
933 *
David Benjamin43946d42016-02-01 08:42:19 -0500934 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700935
Adam Langley0a056712015-07-01 15:03:33 -0700936static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400937 uint16_t min_version, max_version;
938 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
939 return 0;
940 }
941
942 /* Extended master secret is not necessary in TLS 1.3. */
943 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700944 return 1;
945 }
946
947 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
948 !CBB_add_u16(out, 0 /* length */)) {
949 return 0;
950 }
951
952 return 1;
953}
954
955static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
956 CBS *contents) {
David Benjamin163c9562016-08-29 23:14:17 -0400957 /* Whether EMS is negotiated may not change on renegotation. */
958 if (ssl->s3->initial_handshake_complete) {
959 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
960 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
961 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
962 return 0;
963 }
964
965 return 1;
966 }
967
Adam Langley0a056712015-07-01 15:03:33 -0700968 if (contents == NULL) {
969 return 1;
970 }
971
Steven Valdez143e8b32016-07-11 13:19:03 -0400972 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
973 ssl->version == SSL3_VERSION) {
974 return 0;
975 }
976
977 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700978 return 0;
979 }
980
981 ssl->s3->tmp.extended_master_secret = 1;
982 return 1;
983}
984
David Benjamin0d56f882015-12-19 17:05:56 -0500985static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
986 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400987 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
988 ssl->version == SSL3_VERSION) {
989 return 1;
990 }
991
992 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700993 return 1;
994 }
995
996 if (CBS_len(contents) != 0) {
997 return 0;
998 }
999
1000 ssl->s3->tmp.extended_master_secret = 1;
1001 return 1;
1002}
1003
1004static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1005 if (!ssl->s3->tmp.extended_master_secret) {
1006 return 1;
1007 }
1008
1009 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1010 !CBB_add_u16(out, 0 /* length */)) {
1011 return 0;
1012 }
1013
1014 return 1;
1015}
1016
Adam Langley9b05bc52015-07-01 15:25:33 -07001017
1018/* Session tickets.
1019 *
1020 * https://tools.ietf.org/html/rfc5077 */
1021
1022static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -04001023 uint16_t min_version, max_version;
1024 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1025 return 0;
1026 }
1027
1028 /* TLS 1.3 uses a different ticket extension. */
1029 if (min_version >= TLS1_3_VERSION ||
1030 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001031 return 1;
1032 }
1033
1034 const uint8_t *ticket_data = NULL;
1035 int ticket_len = 0;
1036
1037 /* Renegotiation does not participate in session resumption. However, still
1038 * advertise the extension to avoid potentially breaking servers which carry
1039 * over the state from the previous handshake, such as OpenSSL servers
1040 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1041 if (!ssl->s3->initial_handshake_complete &&
1042 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001043 ssl->session->tlsext_tick != NULL &&
1044 /* Don't send TLS 1.3 session tickets in the ticket extension. */
1045 ssl->method->version_from_wire(ssl->session->ssl_version) <
1046 TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001047 ticket_data = ssl->session->tlsext_tick;
1048 ticket_len = ssl->session->tlsext_ticklen;
1049 }
1050
1051 CBB ticket;
1052 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1053 !CBB_add_u16_length_prefixed(out, &ticket) ||
1054 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1055 !CBB_flush(out)) {
1056 return 0;
1057 }
1058
1059 return 1;
1060}
1061
1062static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1063 CBS *contents) {
1064 ssl->tlsext_ticket_expected = 0;
1065
1066 if (contents == NULL) {
1067 return 1;
1068 }
1069
Steven Valdez143e8b32016-07-11 13:19:03 -04001070 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1071 return 0;
1072 }
1073
Adam Langley9b05bc52015-07-01 15:25:33 -07001074 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1075 * this function should never be called, even if the server tries to send the
1076 * extension. */
1077 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1078
1079 if (CBS_len(contents) != 0) {
1080 return 0;
1081 }
1082
1083 ssl->tlsext_ticket_expected = 1;
1084 return 1;
1085}
1086
Adam Langley9b05bc52015-07-01 15:25:33 -07001087static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1088 if (!ssl->tlsext_ticket_expected) {
1089 return 1;
1090 }
1091
1092 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1093 * true. */
1094 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1095
1096 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1097 !CBB_add_u16(out, 0 /* length */)) {
1098 return 0;
1099 }
1100
1101 return 1;
1102}
1103
1104
Adam Langley2e857bd2015-07-01 16:09:19 -07001105/* Signature Algorithms.
1106 *
1107 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1108
1109static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjaminb6a0a512016-06-21 10:33:21 -04001110 if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001111 return 1;
1112 }
1113
David Benjamin0fc37ef2016-08-17 15:29:46 -04001114 const uint16_t *sigalgs;
1115 const size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001116
David Benjamin0fc37ef2016-08-17 15:29:46 -04001117 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001118 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1119 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001120 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001121 return 0;
1122 }
1123
David Benjamin0fc37ef2016-08-17 15:29:46 -04001124 for (size_t i = 0; i < num_sigalgs; i++) {
1125 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001126 return 0;
1127 }
1128 }
1129
1130 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001131 return 0;
1132 }
1133
1134 return 1;
1135}
1136
Adam Langley2e857bd2015-07-01 16:09:19 -07001137static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1138 CBS *contents) {
David Benjamin0fc37ef2016-08-17 15:29:46 -04001139 OPENSSL_free(ssl->s3->hs->peer_sigalgs);
1140 ssl->s3->hs->peer_sigalgs = NULL;
1141 ssl->s3->hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001142
Adam Langley2e857bd2015-07-01 16:09:19 -07001143 if (contents == NULL) {
1144 return 1;
1145 }
1146
1147 CBS supported_signature_algorithms;
1148 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001149 CBS_len(contents) != 0 ||
1150 CBS_len(&supported_signature_algorithms) == 0 ||
1151 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001152 return 0;
1153 }
1154
1155 return 1;
1156}
1157
Adam Langley2e857bd2015-07-01 16:09:19 -07001158
Adam Langleybb0bd042015-07-01 16:21:03 -07001159/* OCSP Stapling.
1160 *
1161 * https://tools.ietf.org/html/rfc6066#section-8 */
1162
1163static void ext_ocsp_init(SSL *ssl) {
1164 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001165 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001166}
1167
1168static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1169 if (!ssl->ocsp_stapling_enabled) {
1170 return 1;
1171 }
1172
1173 CBB contents;
1174 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1175 !CBB_add_u16_length_prefixed(out, &contents) ||
1176 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1177 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1178 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1179 !CBB_flush(out)) {
1180 return 0;
1181 }
1182
Adam Langleyce9d85e2016-01-24 15:58:39 -08001183 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001184 return 1;
1185}
1186
1187static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001188 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001189 if (contents == NULL) {
1190 return 1;
1191 }
1192
David Benjamin942f4ed2016-07-16 19:03:49 +03001193 /* OCSP stapling is forbidden on a non-certificate cipher. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001194 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001195 return 0;
1196 }
1197
David Benjamin942f4ed2016-07-16 19:03:49 +03001198 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1199 if (CBS_len(contents) != 0) {
1200 return 0;
1201 }
1202
1203 ssl->s3->tmp.certificate_status_expected = 1;
1204 return 1;
1205 }
1206
1207 uint8_t status_type;
1208 CBS ocsp_response;
1209 if (!CBS_get_u8(contents, &status_type) ||
1210 status_type != TLSEXT_STATUSTYPE_ocsp ||
1211 !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1212 CBS_len(&ocsp_response) == 0 ||
1213 CBS_len(contents) != 0) {
1214 return 0;
1215 }
1216
Steven Valdez87eab492016-06-27 16:34:59 -04001217 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1218 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001219 *out_alert = SSL_AD_INTERNAL_ERROR;
1220 return 0;
1221 }
1222
Adam Langleybb0bd042015-07-01 16:21:03 -07001223 return 1;
1224}
1225
1226static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1227 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001228 if (contents == NULL) {
1229 return 1;
1230 }
1231
1232 uint8_t status_type;
1233 if (!CBS_get_u8(contents, &status_type)) {
1234 return 0;
1235 }
1236
1237 /* We cannot decide whether OCSP stapling will occur yet because the correct
1238 * SSL_CTX might not have been selected. */
1239 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1240
Adam Langleybb0bd042015-07-01 16:21:03 -07001241 return 1;
1242}
1243
1244static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001245 if (!ssl->s3->tmp.ocsp_stapling_requested ||
1246 ssl->ctx->ocsp_response_length == 0 ||
David Benjamin3d458dc2016-09-12 22:40:27 +00001247 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001248 return 1;
1249 }
1250
David Benjamin942f4ed2016-07-16 19:03:49 +03001251 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1252 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez87eab492016-06-27 16:34:59 -04001253 if (ssl->session != NULL) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001254 return 1;
1255 }
1256
1257 ssl->s3->tmp.certificate_status_expected = 1;
1258
1259 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1260 CBB_add_u16(out, 0 /* length */);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001261 }
1262
David Benjamin942f4ed2016-07-16 19:03:49 +03001263 CBB body, ocsp_response;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001264 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
David Benjamin942f4ed2016-07-16 19:03:49 +03001265 CBB_add_u16_length_prefixed(out, &body) &&
1266 CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1267 CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1268 CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1269 ssl->ctx->ocsp_response_length) &&
1270 CBB_flush(out);
Adam Langleybb0bd042015-07-01 16:21:03 -07001271}
1272
1273
Adam Langley97dfcbf2015-07-01 18:35:20 -07001274/* Next protocol negotiation.
1275 *
1276 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1277
1278static void ext_npn_init(SSL *ssl) {
1279 ssl->s3->next_proto_neg_seen = 0;
1280}
1281
1282static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1283 if (ssl->s3->initial_handshake_complete ||
1284 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001285 (ssl->options & SSL_OP_DISABLE_NPN) ||
David Benjamince079fd2016-08-02 16:22:34 -04001286 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001287 return 1;
1288 }
1289
1290 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1291 !CBB_add_u16(out, 0 /* length */)) {
1292 return 0;
1293 }
1294
1295 return 1;
1296}
1297
1298static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1299 CBS *contents) {
1300 if (contents == NULL) {
1301 return 1;
1302 }
1303
Steven Valdez143e8b32016-07-11 13:19:03 -04001304 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1305 return 0;
1306 }
1307
Adam Langley97dfcbf2015-07-01 18:35:20 -07001308 /* If any of these are false then we should never have sent the NPN
1309 * extension in the ClientHello and thus this function should never have been
1310 * called. */
1311 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001312 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001313 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001314 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001315
David Benjamin76c2efc2015-08-31 14:24:29 -04001316 if (ssl->s3->alpn_selected != NULL) {
1317 /* NPN and ALPN may not be negotiated in the same connection. */
1318 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1319 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1320 return 0;
1321 }
1322
Adam Langley97dfcbf2015-07-01 18:35:20 -07001323 const uint8_t *const orig_contents = CBS_data(contents);
1324 const size_t orig_len = CBS_len(contents);
1325
1326 while (CBS_len(contents) != 0) {
1327 CBS proto;
1328 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1329 CBS_len(&proto) == 0) {
1330 return 0;
1331 }
1332 }
1333
1334 uint8_t *selected;
1335 uint8_t selected_len;
1336 if (ssl->ctx->next_proto_select_cb(
1337 ssl, &selected, &selected_len, orig_contents, orig_len,
1338 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1339 *out_alert = SSL_AD_INTERNAL_ERROR;
1340 return 0;
1341 }
1342
David Benjamin79978df2015-12-25 15:56:49 -05001343 OPENSSL_free(ssl->s3->next_proto_negotiated);
1344 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1345 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001346 *out_alert = SSL_AD_INTERNAL_ERROR;
1347 return 0;
1348 }
1349
David Benjamin79978df2015-12-25 15:56:49 -05001350 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001351 ssl->s3->next_proto_neg_seen = 1;
1352
1353 return 1;
1354}
1355
1356static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1357 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001358 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1359 return 1;
1360 }
1361
Adam Langley97dfcbf2015-07-01 18:35:20 -07001362 if (contents != NULL && CBS_len(contents) != 0) {
1363 return 0;
1364 }
1365
1366 if (contents == NULL ||
1367 ssl->s3->initial_handshake_complete ||
1368 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1369 * afterwards, parsing the ALPN extension will clear
1370 * |next_proto_neg_seen|. */
1371 ssl->s3->alpn_selected != NULL ||
1372 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001373 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001374 return 1;
1375 }
1376
1377 ssl->s3->next_proto_neg_seen = 1;
1378 return 1;
1379}
1380
1381static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1382 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1383 * parsed. */
1384 if (!ssl->s3->next_proto_neg_seen) {
1385 return 1;
1386 }
1387
1388 const uint8_t *npa;
1389 unsigned npa_len;
1390
1391 if (ssl->ctx->next_protos_advertised_cb(
1392 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1393 SSL_TLSEXT_ERR_OK) {
1394 ssl->s3->next_proto_neg_seen = 0;
1395 return 1;
1396 }
1397
1398 CBB contents;
1399 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1400 !CBB_add_u16_length_prefixed(out, &contents) ||
1401 !CBB_add_bytes(&contents, npa, npa_len) ||
1402 !CBB_flush(out)) {
1403 return 0;
1404 }
1405
1406 return 1;
1407}
1408
1409
Adam Langleyab8d87d2015-07-10 12:21:39 -07001410/* Signed certificate timestamps.
1411 *
1412 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1413
1414static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1415 if (!ssl->signed_cert_timestamps_enabled) {
1416 return 1;
1417 }
1418
1419 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1420 !CBB_add_u16(out, 0 /* length */)) {
1421 return 0;
1422 }
1423
1424 return 1;
1425}
1426
1427static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1428 CBS *contents) {
1429 if (contents == NULL) {
1430 return 1;
1431 }
1432
1433 /* If this is false then we should never have sent the SCT extension in the
1434 * ClientHello and thus this function should never have been called. */
1435 assert(ssl->signed_cert_timestamps_enabled);
1436
1437 if (CBS_len(contents) == 0) {
1438 *out_alert = SSL_AD_DECODE_ERROR;
1439 return 0;
1440 }
1441
1442 /* Session resumption uses the original session information. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001443 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001444 !CBS_stow(
1445 contents,
1446 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1447 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001448 *out_alert = SSL_AD_INTERNAL_ERROR;
1449 return 0;
1450 }
1451
1452 return 1;
1453}
1454
1455static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1456 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001457 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001458}
1459
1460static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001461 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001462 if (ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001463 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001464 return 1;
1465 }
1466
1467 CBB contents;
1468 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1469 CBB_add_u16_length_prefixed(out, &contents) &&
1470 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1471 ssl->ctx->signed_cert_timestamp_list_length) &&
1472 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001473}
1474
1475
Adam Langleyf18e4532015-07-10 13:39:53 -07001476/* Application-level Protocol Negotiation.
1477 *
1478 * https://tools.ietf.org/html/rfc7301 */
1479
1480static void ext_alpn_init(SSL *ssl) {
1481 OPENSSL_free(ssl->s3->alpn_selected);
1482 ssl->s3->alpn_selected = NULL;
1483}
1484
1485static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1486 if (ssl->alpn_client_proto_list == NULL ||
1487 ssl->s3->initial_handshake_complete) {
1488 return 1;
1489 }
1490
1491 CBB contents, proto_list;
1492 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1493 !CBB_add_u16_length_prefixed(out, &contents) ||
1494 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1495 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1496 ssl->alpn_client_proto_list_len) ||
1497 !CBB_flush(out)) {
1498 return 0;
1499 }
1500
1501 return 1;
1502}
1503
1504static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1505 CBS *contents) {
1506 if (contents == NULL) {
1507 return 1;
1508 }
1509
1510 assert(!ssl->s3->initial_handshake_complete);
1511 assert(ssl->alpn_client_proto_list != NULL);
1512
David Benjamin76c2efc2015-08-31 14:24:29 -04001513 if (ssl->s3->next_proto_neg_seen) {
1514 /* NPN and ALPN may not be negotiated in the same connection. */
1515 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1516 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1517 return 0;
1518 }
1519
Adam Langleyf18e4532015-07-10 13:39:53 -07001520 /* The extension data consists of a ProtocolNameList which must have
1521 * exactly one ProtocolName. Each of these is length-prefixed. */
1522 CBS protocol_name_list, protocol_name;
1523 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1524 CBS_len(contents) != 0 ||
1525 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1526 /* Empty protocol names are forbidden. */
1527 CBS_len(&protocol_name) == 0 ||
1528 CBS_len(&protocol_name_list) != 0) {
1529 return 0;
1530 }
1531
David Benjamin3e517572016-08-11 11:52:23 -04001532 /* Check that the protcol name is one of the ones we advertised. */
1533 int protocol_ok = 0;
1534 CBS client_protocol_name_list, client_protocol_name;
1535 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1536 ssl->alpn_client_proto_list_len);
1537 while (CBS_len(&client_protocol_name_list) > 0) {
1538 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1539 &client_protocol_name)) {
1540 *out_alert = SSL_AD_INTERNAL_ERROR;
1541 return 0;
1542 }
1543
1544 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1545 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1546 CBS_len(&protocol_name)) == 0) {
1547 protocol_ok = 1;
1548 break;
1549 }
1550 }
1551
1552 if (!protocol_ok) {
1553 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1554 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1555 return 0;
1556 }
1557
Adam Langleyf18e4532015-07-10 13:39:53 -07001558 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1559 &ssl->s3->alpn_selected_len)) {
1560 *out_alert = SSL_AD_INTERNAL_ERROR;
1561 return 0;
1562 }
1563
1564 return 1;
1565}
1566
1567static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1568 CBS *contents) {
1569 if (contents == NULL) {
1570 return 1;
1571 }
1572
1573 if (ssl->ctx->alpn_select_cb == NULL ||
1574 ssl->s3->initial_handshake_complete) {
1575 return 1;
1576 }
1577
1578 /* ALPN takes precedence over NPN. */
1579 ssl->s3->next_proto_neg_seen = 0;
1580
1581 CBS protocol_name_list;
1582 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1583 CBS_len(contents) != 0 ||
1584 CBS_len(&protocol_name_list) < 2) {
1585 return 0;
1586 }
1587
1588 /* Validate the protocol list. */
1589 CBS protocol_name_list_copy = protocol_name_list;
1590 while (CBS_len(&protocol_name_list_copy) > 0) {
1591 CBS protocol_name;
1592
1593 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1594 /* Empty protocol names are forbidden. */
1595 CBS_len(&protocol_name) == 0) {
1596 return 0;
1597 }
1598 }
1599
1600 const uint8_t *selected;
1601 uint8_t selected_len;
1602 if (ssl->ctx->alpn_select_cb(
1603 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1604 CBS_len(&protocol_name_list),
1605 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1606 OPENSSL_free(ssl->s3->alpn_selected);
1607 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1608 if (ssl->s3->alpn_selected == NULL) {
1609 *out_alert = SSL_AD_INTERNAL_ERROR;
1610 return 0;
1611 }
1612 ssl->s3->alpn_selected_len = selected_len;
1613 }
1614
1615 return 1;
1616}
1617
1618static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1619 if (ssl->s3->alpn_selected == NULL) {
1620 return 1;
1621 }
1622
1623 CBB contents, proto_list, proto;
1624 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1625 !CBB_add_u16_length_prefixed(out, &contents) ||
1626 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1627 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001628 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1629 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001630 !CBB_flush(out)) {
1631 return 0;
1632 }
1633
1634 return 1;
1635}
1636
1637
Adam Langley49c7af12015-07-10 14:33:46 -07001638/* Channel ID.
1639 *
1640 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1641
1642static void ext_channel_id_init(SSL *ssl) {
1643 ssl->s3->tlsext_channel_id_valid = 0;
1644}
1645
1646static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1647 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001648 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001649 return 1;
1650 }
1651
1652 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1653 !CBB_add_u16(out, 0 /* length */)) {
1654 return 0;
1655 }
1656
1657 return 1;
1658}
1659
1660static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1661 CBS *contents) {
1662 if (contents == NULL) {
1663 return 1;
1664 }
1665
Steven Valdez143e8b32016-07-11 13:19:03 -04001666 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1667 return 0;
1668 }
1669
David Benjamince079fd2016-08-02 16:22:34 -04001670 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001671 assert(ssl->tlsext_channel_id_enabled);
1672
1673 if (CBS_len(contents) != 0) {
1674 return 0;
1675 }
1676
1677 ssl->s3->tlsext_channel_id_valid = 1;
1678 return 1;
1679}
1680
1681static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1682 CBS *contents) {
1683 if (contents == NULL ||
1684 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001685 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001686 return 1;
1687 }
1688
1689 if (CBS_len(contents) != 0) {
1690 return 0;
1691 }
1692
1693 ssl->s3->tlsext_channel_id_valid = 1;
1694 return 1;
1695}
1696
1697static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001698 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1699 return 1;
1700 }
1701
Adam Langley49c7af12015-07-10 14:33:46 -07001702 if (!ssl->s3->tlsext_channel_id_valid) {
1703 return 1;
1704 }
1705
1706 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1707 !CBB_add_u16(out, 0 /* length */)) {
1708 return 0;
1709 }
1710
1711 return 1;
1712}
1713
Adam Langley391250d2015-07-15 19:06:07 -07001714
1715/* Secure Real-time Transport Protocol (SRTP) extension.
1716 *
1717 * https://tools.ietf.org/html/rfc5764 */
1718
Adam Langley391250d2015-07-15 19:06:07 -07001719
1720static void ext_srtp_init(SSL *ssl) {
1721 ssl->srtp_profile = NULL;
1722}
1723
1724static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1725 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1726 if (profiles == NULL) {
1727 return 1;
1728 }
1729 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1730 if (num_profiles == 0) {
1731 return 1;
1732 }
1733
1734 CBB contents, profile_ids;
1735 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1736 !CBB_add_u16_length_prefixed(out, &contents) ||
1737 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1738 return 0;
1739 }
1740
David Benjamin54091232016-09-05 12:47:25 -04001741 for (size_t i = 0; i < num_profiles; i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001742 if (!CBB_add_u16(&profile_ids,
1743 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1744 return 0;
1745 }
1746 }
1747
1748 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1749 !CBB_flush(out)) {
1750 return 0;
1751 }
1752
1753 return 1;
1754}
1755
1756static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1757 CBS *contents) {
1758 if (contents == NULL) {
1759 return 1;
1760 }
1761
1762 /* The extension consists of a u16-prefixed profile ID list containing a
1763 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1764 *
1765 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1766 CBS profile_ids, srtp_mki;
1767 uint16_t profile_id;
1768 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1769 !CBS_get_u16(&profile_ids, &profile_id) ||
1770 CBS_len(&profile_ids) != 0 ||
1771 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1772 CBS_len(contents) != 0) {
1773 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1774 return 0;
1775 }
1776
1777 if (CBS_len(&srtp_mki) != 0) {
1778 /* Must be no MKI, since we never offer one. */
1779 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1780 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1781 return 0;
1782 }
1783
1784 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1785
1786 /* Check to see if the server gave us something we support (and presumably
1787 * offered). */
David Benjamin54091232016-09-05 12:47:25 -04001788 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001789 const SRTP_PROTECTION_PROFILE *profile =
1790 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1791
1792 if (profile->id == profile_id) {
1793 ssl->srtp_profile = profile;
1794 return 1;
1795 }
1796 }
1797
1798 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1799 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1800 return 0;
1801}
1802
1803static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1804 CBS *contents) {
1805 if (contents == NULL) {
1806 return 1;
1807 }
1808
1809 CBS profile_ids, srtp_mki;
1810 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1811 CBS_len(&profile_ids) < 2 ||
1812 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1813 CBS_len(contents) != 0) {
1814 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1815 return 0;
1816 }
1817 /* Discard the MKI value for now. */
1818
1819 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1820 SSL_get_srtp_profiles(ssl);
1821
1822 /* Pick the server's most preferred profile. */
David Benjamin54091232016-09-05 12:47:25 -04001823 for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
Adam Langley391250d2015-07-15 19:06:07 -07001824 const SRTP_PROTECTION_PROFILE *server_profile =
1825 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1826
1827 CBS profile_ids_tmp;
1828 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1829
1830 while (CBS_len(&profile_ids_tmp) > 0) {
1831 uint16_t profile_id;
1832 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1833 return 0;
1834 }
1835
1836 if (server_profile->id == profile_id) {
1837 ssl->srtp_profile = server_profile;
1838 return 1;
1839 }
1840 }
1841 }
1842
1843 return 1;
1844}
1845
1846static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1847 if (ssl->srtp_profile == NULL) {
1848 return 1;
1849 }
1850
1851 CBB contents, profile_ids;
1852 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1853 !CBB_add_u16_length_prefixed(out, &contents) ||
1854 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1855 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1856 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1857 !CBB_flush(out)) {
1858 return 0;
1859 }
1860
1861 return 1;
1862}
1863
Adam Langleybdd5d662015-07-20 16:19:08 -07001864
1865/* EC point formats.
1866 *
1867 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1868
1869static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
David Benjamince079fd2016-08-02 16:22:34 -04001870 if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001871 return 0;
1872 }
1873
1874 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1875
David Benjamin54091232016-09-05 12:47:25 -04001876 for (size_t i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001877 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1878
1879 const uint32_t alg_k = cipher->algorithm_mkey;
1880 const uint32_t alg_a = cipher->algorithm_auth;
1881 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1882 return 1;
1883 }
1884 }
1885
1886 return 0;
1887}
1888
Adam Langleybdd5d662015-07-20 16:19:08 -07001889static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001890 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001891 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1892 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001893 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1894 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001895 !CBB_flush(out)) {
1896 return 0;
1897 }
1898
1899 return 1;
1900}
1901
1902static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1903 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1904 return 1;
1905 }
1906
1907 return ext_ec_point_add_extension(ssl, out);
1908}
1909
1910static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1911 CBS *contents) {
1912 if (contents == NULL) {
1913 return 1;
1914 }
1915
Steven Valdez143e8b32016-07-11 13:19:03 -04001916 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1917 return 0;
1918 }
1919
Adam Langleybdd5d662015-07-20 16:19:08 -07001920 CBS ec_point_format_list;
1921 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1922 CBS_len(contents) != 0) {
1923 return 0;
1924 }
1925
David Benjaminfc059942015-07-30 23:01:59 -04001926 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1927 * point format. */
1928 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1929 CBS_len(&ec_point_format_list)) == NULL) {
1930 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001931 return 0;
1932 }
1933
1934 return 1;
1935}
1936
1937static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1938 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001939 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1940 return 1;
1941 }
1942
Adam Langleybdd5d662015-07-20 16:19:08 -07001943 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1944}
1945
1946static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001947 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1948 return 1;
1949 }
1950
Adam Langleybdd5d662015-07-20 16:19:08 -07001951 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1952 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001953 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001954
1955 if (!using_ecc) {
1956 return 1;
1957 }
1958
1959 return ext_ec_point_add_extension(ssl, out);
1960}
1961
Adam Langley273d49c2015-07-20 16:38:52 -07001962
Steven Valdez143e8b32016-07-11 13:19:03 -04001963/* Draft Version Extension */
1964
1965static int ext_draft_version_add_clienthello(SSL *ssl, CBB *out) {
1966 uint16_t min_version, max_version;
1967 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
Steven Valdez3a287552016-07-19 05:10:50 -04001968 max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001969 return 1;
1970 }
1971
1972 CBB contents;
1973 if (!CBB_add_u16(out, TLSEXT_TYPE_draft_version) ||
1974 !CBB_add_u16_length_prefixed(out, &contents) ||
1975 !CBB_add_u16(&contents, TLS1_3_DRAFT_VERSION)) {
1976 return 0;
1977 }
1978
1979 return CBB_flush(out);
1980}
1981
1982
Steven Valdez4aa154e2016-07-29 14:32:55 -04001983/* Pre Shared Key
1984 *
1985 * https://tools.ietf.org/html/draft-ietf-tls-tls13-14 */
1986
1987static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
1988 uint16_t min_version, max_version;
1989 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1990 return 0;
1991 }
1992
1993 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1994 ssl->method->version_from_wire(ssl->session->ssl_version) <
1995 TLS1_3_VERSION) {
1996 return 1;
1997 }
1998
1999 CBB contents, identities, identity;
2000 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2001 !CBB_add_u16_length_prefixed(out, &contents) ||
2002 !CBB_add_u16_length_prefixed(&contents, &identities) ||
2003 !CBB_add_u16_length_prefixed(&identities, &identity) ||
2004 !CBB_add_bytes(&identity, ssl->session->tlsext_tick,
2005 ssl->session->tlsext_ticklen)) {
2006 return 0;
2007 }
2008
2009 return CBB_flush(out);
2010}
2011
2012int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2013 CBS *contents) {
2014 uint16_t psk_id;
2015 if (!CBS_get_u16(contents, &psk_id) ||
2016 CBS_len(contents) != 0) {
2017 *out_alert = SSL_AD_DECODE_ERROR;
2018 return 0;
2019 }
2020
2021 if (psk_id != 0) {
2022 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2023 return 0;
2024 }
2025
2026 return 1;
2027}
2028
2029int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
2030 SSL_SESSION **out_session,
2031 uint8_t *out_alert,
2032 CBS *contents) {
2033 CBS identities, identity;
2034 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2035 !CBS_get_u16_length_prefixed(&identities, &identity) ||
2036 CBS_len(contents) != 0) {
2037 *out_alert = SSL_AD_DECODE_ERROR;
2038 return 0;
2039 }
2040
2041 /* TLS 1.3 session tickets are renewed separately as part of the
2042 * NewSessionTicket. */
2043 int renew;
2044 return tls_process_ticket(ssl, out_session, &renew, CBS_data(&identity),
2045 CBS_len(&identity), NULL, 0);
2046}
2047
2048int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
2049 if (!ssl->s3->session_reused) {
2050 return 1;
2051 }
2052
2053 CBB contents;
2054 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2055 !CBB_add_u16_length_prefixed(out, &contents) ||
2056 /* We only consider the first identity for resumption */
2057 !CBB_add_u16(&contents, 0) ||
2058 !CBB_flush(out)) {
2059 return 0;
2060 }
2061
2062 return 1;
2063}
2064
2065
Steven Valdez143e8b32016-07-11 13:19:03 -04002066/* Key Share
2067 *
2068 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
2069
2070static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
2071 uint16_t min_version, max_version;
2072 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2073 return 0;
2074 }
2075
2076 if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
2077 return 1;
2078 }
2079
2080 CBB contents, kse_bytes;
2081 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2082 !CBB_add_u16_length_prefixed(out, &contents) ||
2083 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2084 return 0;
2085 }
2086
2087 const uint16_t *groups;
2088 size_t groups_len;
Steven Valdez5440fe02016-07-18 12:40:30 -04002089 if (ssl->s3->hs->retry_group) {
2090 /* Append the new key share to the old list. */
2091 if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
2092 ssl->s3->hs->key_share_bytes_len)) {
2093 return 0;
2094 }
2095 OPENSSL_free(ssl->s3->hs->key_share_bytes);
2096 ssl->s3->hs->key_share_bytes = NULL;
David Benjamin4fe3c902016-08-16 02:17:03 -04002097 ssl->s3->hs->key_share_bytes_len = 0;
Steven Valdez5440fe02016-07-18 12:40:30 -04002098
2099 groups = &ssl->s3->hs->retry_group;
2100 groups_len = 1;
2101 } else {
2102 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
2103 /* Only send the top two preferred key shares. */
2104 if (groups_len > 2) {
2105 groups_len = 2;
2106 }
2107 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002108
2109 ssl->s3->hs->groups = OPENSSL_malloc(groups_len * sizeof(SSL_ECDH_CTX));
2110 if (ssl->s3->hs->groups == NULL) {
2111 return 0;
2112 }
2113 memset(ssl->s3->hs->groups, 0, groups_len * sizeof(SSL_ECDH_CTX));
2114 ssl->s3->hs->groups_len = groups_len;
2115
2116 for (size_t i = 0; i < groups_len; i++) {
2117 if (!CBB_add_u16(&kse_bytes, groups[i])) {
2118 return 0;
2119 }
2120
2121 CBB key_exchange;
2122 if (!CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2123 !SSL_ECDH_CTX_init(&ssl->s3->hs->groups[i], groups[i]) ||
2124 !SSL_ECDH_CTX_offer(&ssl->s3->hs->groups[i], &key_exchange) ||
2125 !CBB_flush(&kse_bytes)) {
2126 return 0;
2127 }
2128 }
2129
Steven Valdez5440fe02016-07-18 12:40:30 -04002130 if (!ssl->s3->hs->retry_group) {
2131 /* Save the contents of the extension to repeat it in the second
2132 * ClientHello. */
2133 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2134 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2135 CBB_len(&kse_bytes));
2136 if (ssl->s3->hs->key_share_bytes == NULL) {
2137 return 0;
2138 }
2139 }
2140
Steven Valdez143e8b32016-07-11 13:19:03 -04002141 return CBB_flush(out);
2142}
2143
Steven Valdez7259f2f2016-08-02 16:55:05 -04002144int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2145 size_t *out_secret_len,
2146 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002147 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002148 uint16_t group_id;
2149 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002150 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2151 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002152 *out_alert = SSL_AD_DECODE_ERROR;
2153 return 0;
2154 }
2155
2156 SSL_ECDH_CTX *group_ctx = NULL;
2157 for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) {
David Benjamin5c4e8572016-08-19 17:44:53 -04002158 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->groups[i]) == group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002159 group_ctx = &ssl->s3->hs->groups[i];
2160 break;
2161 }
2162 }
2163
2164 if (group_ctx == NULL) {
2165 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2166 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2167 return 0;
2168 }
2169
2170 if (!SSL_ECDH_CTX_finish(group_ctx, out_secret, out_secret_len, out_alert,
2171 CBS_data(&peer_key), CBS_len(&peer_key))) {
2172 *out_alert = SSL_AD_INTERNAL_ERROR;
2173 return 0;
2174 }
2175
David Benjamin5c4e8572016-08-19 17:44:53 -04002176 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez5440fe02016-07-18 12:40:30 -04002177 ssl_handshake_clear_groups(ssl->s3->hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04002178 return 1;
2179}
2180
Steven Valdez7259f2f2016-08-02 16:55:05 -04002181int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2182 uint8_t **out_secret,
2183 size_t *out_secret_len,
2184 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002185 uint16_t group_id;
2186 CBS key_shares;
2187 if (!tls1_get_shared_group(ssl, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002188 !CBS_get_u16_length_prefixed(contents, &key_shares) ||
2189 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002190 return 0;
2191 }
2192
Steven Valdez5440fe02016-07-18 12:40:30 -04002193 *out_found = 0;
Steven Valdez143e8b32016-07-11 13:19:03 -04002194 while (CBS_len(&key_shares) > 0) {
2195 uint16_t id;
2196 CBS peer_key;
2197 if (!CBS_get_u16(&key_shares, &id) ||
2198 !CBS_get_u16_length_prefixed(&key_shares, &peer_key)) {
2199 return 0;
2200 }
2201
Steven Valdez5440fe02016-07-18 12:40:30 -04002202 if (id != group_id || *out_found) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002203 continue;
2204 }
2205
2206 SSL_ECDH_CTX group;
2207 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2208 CBB public_key;
2209 if (!CBB_init(&public_key, 0) ||
2210 !SSL_ECDH_CTX_init(&group, group_id) ||
2211 !SSL_ECDH_CTX_accept(&group, &public_key, out_secret, out_secret_len,
2212 out_alert, CBS_data(&peer_key),
2213 CBS_len(&peer_key)) ||
2214 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2215 &ssl->s3->hs->public_key_len)) {
2216 SSL_ECDH_CTX_cleanup(&group);
2217 CBB_cleanup(&public_key);
2218 return 0;
2219 }
2220 SSL_ECDH_CTX_cleanup(&group);
2221
Steven Valdez5440fe02016-07-18 12:40:30 -04002222 *out_found = 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002223 }
2224
Steven Valdez5440fe02016-07-18 12:40:30 -04002225 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002226}
2227
Steven Valdez7259f2f2016-08-02 16:55:05 -04002228int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002229 if (ssl->s3->tmp.new_cipher->algorithm_mkey != SSL_kECDHE) {
2230 return 1;
2231 }
2232
2233 uint16_t group_id;
2234 CBB kse_bytes, public_key;
2235 if (!tls1_get_shared_group(ssl, &group_id) ||
2236 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2237 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2238 !CBB_add_u16(&kse_bytes, group_id) ||
2239 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2240 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2241 ssl->s3->hs->public_key_len) ||
2242 !CBB_flush(out)) {
2243 return 0;
2244 }
2245
David Benjamin4fe3c902016-08-16 02:17:03 -04002246 OPENSSL_free(ssl->s3->hs->public_key);
2247 ssl->s3->hs->public_key = NULL;
2248 ssl->s3->hs->public_key_len = 0;
2249
David Benjamin5c4e8572016-08-19 17:44:53 -04002250 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002251 return 1;
2252}
2253
2254
Steven Valdezce902a92016-05-17 11:47:53 -04002255/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002256 *
Steven Valdezce902a92016-05-17 11:47:53 -04002257 * https://tools.ietf.org/html/rfc4492#section-5.1.2
2258 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07002259
Steven Valdezce902a92016-05-17 11:47:53 -04002260static void ext_supported_groups_init(SSL *ssl) {
2261 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2262 ssl->s3->tmp.peer_supported_group_list = NULL;
2263 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07002264}
2265
Steven Valdezce902a92016-05-17 11:47:53 -04002266static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002267 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2268 return 1;
2269 }
2270
Steven Valdezce902a92016-05-17 11:47:53 -04002271 CBB contents, groups_bytes;
2272 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002273 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002274 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002275 return 0;
2276 }
2277
Steven Valdezce902a92016-05-17 11:47:53 -04002278 const uint16_t *groups;
2279 size_t groups_len;
2280 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002281
David Benjamin54091232016-09-05 12:47:25 -04002282 for (size_t i = 0; i < groups_len; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002283 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002284 return 0;
2285 }
2286 }
2287
2288 return CBB_flush(out);
2289}
2290
Steven Valdezce902a92016-05-17 11:47:53 -04002291static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2292 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002293 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2294 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002295 return 1;
2296}
2297
Steven Valdezce902a92016-05-17 11:47:53 -04002298static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2299 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002300 if (contents == NULL) {
2301 return 1;
2302 }
2303
Steven Valdezce902a92016-05-17 11:47:53 -04002304 CBS supported_group_list;
2305 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2306 CBS_len(&supported_group_list) == 0 ||
2307 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002308 CBS_len(contents) != 0) {
2309 return 0;
2310 }
2311
Steven Valdezce902a92016-05-17 11:47:53 -04002312 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
2313 CBS_len(&supported_group_list));
2314 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002315 *out_alert = SSL_AD_INTERNAL_ERROR;
2316 return 0;
2317 }
2318
Steven Valdezce902a92016-05-17 11:47:53 -04002319 const size_t num_groups = CBS_len(&supported_group_list) / 2;
David Benjamin54091232016-09-05 12:47:25 -04002320 for (size_t i = 0; i < num_groups; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -04002321 if (!CBS_get_u16(&supported_group_list,
2322 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002323 goto err;
2324 }
2325 }
2326
Steven Valdezce902a92016-05-17 11:47:53 -04002327 assert(CBS_len(&supported_group_list) == 0);
2328 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002329
2330 return 1;
2331
2332err:
Steven Valdezce902a92016-05-17 11:47:53 -04002333 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2334 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002335 *out_alert = SSL_AD_INTERNAL_ERROR;
2336 return 0;
2337}
2338
Steven Valdezce902a92016-05-17 11:47:53 -04002339static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002340 /* Servers don't echo this extension. */
2341 return 1;
2342}
2343
2344
Adam Langley614c66a2015-06-12 15:26:58 -07002345/* kExtensions contains all the supported extensions. */
2346static const struct tls_extension kExtensions[] = {
2347 {
Adam Langley5021b222015-06-12 18:27:58 -07002348 TLSEXT_TYPE_renegotiate,
2349 NULL,
2350 ext_ri_add_clienthello,
2351 ext_ri_parse_serverhello,
2352 ext_ri_parse_clienthello,
2353 ext_ri_add_serverhello,
2354 },
2355 {
Adam Langley614c66a2015-06-12 15:26:58 -07002356 TLSEXT_TYPE_server_name,
2357 ext_sni_init,
2358 ext_sni_add_clienthello,
2359 ext_sni_parse_serverhello,
2360 ext_sni_parse_clienthello,
2361 ext_sni_add_serverhello,
2362 },
Adam Langley0a056712015-07-01 15:03:33 -07002363 {
2364 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002365 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002366 ext_ems_add_clienthello,
2367 ext_ems_parse_serverhello,
2368 ext_ems_parse_clienthello,
2369 ext_ems_add_serverhello,
2370 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002371 {
2372 TLSEXT_TYPE_session_ticket,
2373 NULL,
2374 ext_ticket_add_clienthello,
2375 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002376 /* Ticket extension client parsing is handled in ssl_session.c */
2377 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002378 ext_ticket_add_serverhello,
2379 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002380 {
2381 TLSEXT_TYPE_signature_algorithms,
2382 NULL,
2383 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002384 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002385 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002386 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002387 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002388 {
2389 TLSEXT_TYPE_status_request,
2390 ext_ocsp_init,
2391 ext_ocsp_add_clienthello,
2392 ext_ocsp_parse_serverhello,
2393 ext_ocsp_parse_clienthello,
2394 ext_ocsp_add_serverhello,
2395 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002396 {
2397 TLSEXT_TYPE_next_proto_neg,
2398 ext_npn_init,
2399 ext_npn_add_clienthello,
2400 ext_npn_parse_serverhello,
2401 ext_npn_parse_clienthello,
2402 ext_npn_add_serverhello,
2403 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002404 {
2405 TLSEXT_TYPE_certificate_timestamp,
2406 NULL,
2407 ext_sct_add_clienthello,
2408 ext_sct_parse_serverhello,
2409 ext_sct_parse_clienthello,
2410 ext_sct_add_serverhello,
2411 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002412 {
2413 TLSEXT_TYPE_application_layer_protocol_negotiation,
2414 ext_alpn_init,
2415 ext_alpn_add_clienthello,
2416 ext_alpn_parse_serverhello,
2417 ext_alpn_parse_clienthello,
2418 ext_alpn_add_serverhello,
2419 },
Adam Langley49c7af12015-07-10 14:33:46 -07002420 {
2421 TLSEXT_TYPE_channel_id,
2422 ext_channel_id_init,
2423 ext_channel_id_add_clienthello,
2424 ext_channel_id_parse_serverhello,
2425 ext_channel_id_parse_clienthello,
2426 ext_channel_id_add_serverhello,
2427 },
Adam Langley391250d2015-07-15 19:06:07 -07002428 {
2429 TLSEXT_TYPE_srtp,
2430 ext_srtp_init,
2431 ext_srtp_add_clienthello,
2432 ext_srtp_parse_serverhello,
2433 ext_srtp_parse_clienthello,
2434 ext_srtp_add_serverhello,
2435 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002436 {
2437 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002438 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002439 ext_ec_point_add_clienthello,
2440 ext_ec_point_parse_serverhello,
2441 ext_ec_point_parse_clienthello,
2442 ext_ec_point_add_serverhello,
2443 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002444 {
2445 TLSEXT_TYPE_draft_version,
2446 NULL,
2447 ext_draft_version_add_clienthello,
2448 forbid_parse_serverhello,
2449 ignore_parse_clienthello,
2450 dont_add_serverhello,
2451 },
2452 {
2453 TLSEXT_TYPE_key_share,
2454 NULL,
2455 ext_key_share_add_clienthello,
2456 forbid_parse_serverhello,
2457 ignore_parse_clienthello,
2458 dont_add_serverhello,
2459 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002460 {
2461 TLSEXT_TYPE_pre_shared_key,
2462 NULL,
2463 ext_pre_shared_key_add_clienthello,
2464 forbid_parse_serverhello,
2465 ignore_parse_clienthello,
2466 dont_add_serverhello,
2467 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002468 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2469 * intolerant to the last extension being zero-length. See
2470 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002471 {
Steven Valdezce902a92016-05-17 11:47:53 -04002472 TLSEXT_TYPE_supported_groups,
2473 ext_supported_groups_init,
2474 ext_supported_groups_add_clienthello,
2475 ext_supported_groups_parse_serverhello,
2476 ext_supported_groups_parse_clienthello,
2477 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002478 },
Adam Langley614c66a2015-06-12 15:26:58 -07002479};
2480
2481#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2482
Adam Langley4cfa96b2015-07-01 11:56:55 -07002483OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2484 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002485 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002486OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2487 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2488 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002489 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002490
Adam Langley614c66a2015-06-12 15:26:58 -07002491static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2492 uint16_t value) {
2493 unsigned i;
2494 for (i = 0; i < kNumExtensions; i++) {
2495 if (kExtensions[i].value == value) {
2496 *out_index = i;
2497 return &kExtensions[i];
2498 }
2499 }
2500
2501 return NULL;
2502}
2503
Adam Langley09505632015-07-30 18:10:13 -07002504int SSL_extension_supported(unsigned extension_value) {
2505 uint32_t index;
2506 return extension_value == TLSEXT_TYPE_padding ||
2507 tls_extension_find(&index, extension_value) != NULL;
2508}
2509
David Benjamine8d53502015-10-10 14:13:23 -04002510int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002511 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002512 if (ssl->client_version == SSL3_VERSION &&
2513 !ssl->s3->send_connection_binding) {
2514 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002515 }
Adam Langley95c29f32014-06-20 12:00:00 -07002516
David Benjamine8d53502015-10-10 14:13:23 -04002517 CBB extensions;
2518 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002519 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002520 }
Adam Langley95c29f32014-06-20 12:00:00 -07002521
David Benjamine8d53502015-10-10 14:13:23 -04002522 ssl->s3->tmp.extensions.sent = 0;
2523 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002524
David Benjamin54091232016-09-05 12:47:25 -04002525 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002526 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002527 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002528 }
2529 }
Adam Langley95c29f32014-06-20 12:00:00 -07002530
David Benjamin54091232016-09-05 12:47:25 -04002531 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002532 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002533 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002534 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2535 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2536 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002537 }
Adam Langley95c29f32014-06-20 12:00:00 -07002538
Adam Langley33ad2b52015-07-20 17:43:53 -07002539 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002540 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002541 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002542 }
Adam Langley75712922014-10-10 16:23:43 -07002543
David Benjamine8d53502015-10-10 14:13:23 -04002544 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002545 goto err;
2546 }
2547
David Benjamince079fd2016-08-02 16:22:34 -04002548 if (!SSL_is_dtls(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002549 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002550 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002551 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002552 *
2553 * NB: because this code works out the length of all existing extensions
2554 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002555 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002556 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002557 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002558 * Server 7.0 is intolerant to the last extension being zero-length. See
2559 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002560 if (padding_len >= 4 + 1) {
2561 padding_len -= 4;
2562 } else {
2563 padding_len = 1;
2564 }
Adam Langley95c29f32014-06-20 12:00:00 -07002565
Adam Langley33ad2b52015-07-20 17:43:53 -07002566 uint8_t *padding_bytes;
2567 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2568 !CBB_add_u16(&extensions, padding_len) ||
2569 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2570 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002571 }
Adam Langley75712922014-10-10 16:23:43 -07002572
Adam Langley33ad2b52015-07-20 17:43:53 -07002573 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002574 }
2575 }
Adam Langley75712922014-10-10 16:23:43 -07002576
David Benjamina01deee2015-12-08 18:56:31 -05002577 /* Discard empty extensions blocks. */
2578 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002579 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002580 }
2581
David Benjamine8d53502015-10-10 14:13:23 -04002582 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002583
2584err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002585 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002586 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002587}
Adam Langley95c29f32014-06-20 12:00:00 -07002588
David Benjamin56380462015-10-10 14:59:09 -04002589int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002590 CBB extensions;
2591 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002592 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002593 }
2594
2595 unsigned i;
2596 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002597 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002598 /* Don't send extensions that were not received. */
2599 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002600 }
Adam Langley95c29f32014-06-20 12:00:00 -07002601
David Benjamin56380462015-10-10 14:59:09 -04002602 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002603 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2604 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2605 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002606 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002607 }
Adam Langley95c29f32014-06-20 12:00:00 -07002608
David Benjamin56380462015-10-10 14:59:09 -04002609 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002610 goto err;
2611 }
2612
Steven Valdez143e8b32016-07-11 13:19:03 -04002613 /* Discard empty extensions blocks before TLS 1.3. */
2614 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2615 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002616 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002617 }
2618
David Benjamin56380462015-10-10 14:59:09 -04002619 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002620
2621err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002622 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002623 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002624}
Adam Langley95c29f32014-06-20 12:00:00 -07002625
David Benjamine14ff062016-08-09 16:21:24 -04002626static int ssl_scan_clienthello_tlsext(
2627 SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
2628 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002629 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002630 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002631 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002632 }
2633 }
2634
David Benjamin0d56f882015-12-19 17:05:56 -05002635 ssl->s3->tmp.extensions.received = 0;
2636 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002637
David Benjamine14ff062016-08-09 16:21:24 -04002638 CBS extensions;
2639 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2640 while (CBS_len(&extensions) != 0) {
2641 uint16_t type;
2642 CBS extension;
2643
2644 /* Decode the next extension. */
2645 if (!CBS_get_u16(&extensions, &type) ||
2646 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002647 *out_alert = SSL_AD_DECODE_ERROR;
2648 return 0;
2649 }
Adam Langley95c29f32014-06-20 12:00:00 -07002650
David Benjamine14ff062016-08-09 16:21:24 -04002651 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2652 * ambiguous. Ignore all but the renegotiation_info extension. */
2653 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2654 continue;
2655 }
Adam Langley95c29f32014-06-20 12:00:00 -07002656
David Benjamine14ff062016-08-09 16:21:24 -04002657 unsigned ext_index;
2658 const struct tls_extension *const ext =
2659 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002660
David Benjamine14ff062016-08-09 16:21:24 -04002661 if (ext == NULL) {
2662 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002663 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002664 return 0;
2665 }
David Benjamine14ff062016-08-09 16:21:24 -04002666 continue;
2667 }
2668
2669 ssl->s3->tmp.extensions.received |= (1u << ext_index);
2670 uint8_t alert = SSL_AD_DECODE_ERROR;
2671 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2672 *out_alert = alert;
2673 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2674 ERR_add_error_dataf("extension: %u", (unsigned)type);
2675 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002676 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002677 }
Adam Langley75712922014-10-10 16:23:43 -07002678
David Benjamin1deb41b2016-08-09 19:36:38 -04002679 for (size_t i = 0; i < kNumExtensions; i++) {
2680 if (ssl->s3->tmp.extensions.received & (1u << i)) {
2681 continue;
2682 }
2683
2684 CBS *contents = NULL, fake_contents;
2685 static const uint8_t kFakeRenegotiateExtension[] = {0};
2686 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2687 ssl_client_cipher_list_contains_cipher(client_hello,
2688 SSL3_CK_SCSV & 0xffff)) {
2689 /* The renegotiation SCSV was received so pretend that we received a
2690 * renegotiation extension. */
2691 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2692 sizeof(kFakeRenegotiateExtension));
2693 contents = &fake_contents;
2694 ssl->s3->tmp.extensions.received |= (1u << i);
2695 }
2696
2697 /* Extension wasn't observed so call the callback with a NULL
2698 * parameter. */
2699 uint8_t alert = SSL_AD_DECODE_ERROR;
2700 if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
2701 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2702 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2703 *out_alert = alert;
2704 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002705 }
2706 }
2707
Adam Langleyfcf25832014-12-18 17:42:32 -08002708 return 1;
2709}
Adam Langley95c29f32014-06-20 12:00:00 -07002710
David Benjamine14ff062016-08-09 16:21:24 -04002711int ssl_parse_clienthello_tlsext(
2712 SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002713 int alert = -1;
David Benjamine14ff062016-08-09 16:21:24 -04002714 if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002715 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002716 return 0;
2717 }
Adam Langley95c29f32014-06-20 12:00:00 -07002718
David Benjamin0d56f882015-12-19 17:05:56 -05002719 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002720 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002721 return 0;
2722 }
Adam Langley95c29f32014-06-20 12:00:00 -07002723
Adam Langleyfcf25832014-12-18 17:42:32 -08002724 return 1;
2725}
Adam Langley95c29f32014-06-20 12:00:00 -07002726
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002727OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2728
David Benjamin0d56f882015-12-19 17:05:56 -05002729static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002730 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2731 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2732 return 1;
2733 }
Adam Langley614c66a2015-06-12 15:26:58 -07002734
Steven Valdez143e8b32016-07-11 13:19:03 -04002735 /* Decode the extensions block and check it is valid. */
2736 CBS extensions;
2737 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2738 !tls1_check_duplicate_extensions(&extensions)) {
2739 *out_alert = SSL_AD_DECODE_ERROR;
2740 return 0;
2741 }
2742
2743 uint32_t received = 0;
2744 while (CBS_len(&extensions) != 0) {
2745 uint16_t type;
2746 CBS extension;
2747
2748 /* Decode the next extension. */
2749 if (!CBS_get_u16(&extensions, &type) ||
2750 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002751 *out_alert = SSL_AD_DECODE_ERROR;
2752 return 0;
2753 }
Adam Langley95c29f32014-06-20 12:00:00 -07002754
Steven Valdez143e8b32016-07-11 13:19:03 -04002755 unsigned ext_index;
2756 const struct tls_extension *const ext =
2757 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002758
Steven Valdez143e8b32016-07-11 13:19:03 -04002759 if (ext == NULL) {
2760 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002761 return 0;
2762 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002763 continue;
2764 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002765
David Benjamin1deb41b2016-08-09 19:36:38 -04002766 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index)) &&
2767 type != TLSEXT_TYPE_renegotiate) {
2768 /* If the extension was never sent then it is illegal, except for the
2769 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002770 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2771 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002772 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002773 return 0;
2774 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002775
Steven Valdez143e8b32016-07-11 13:19:03 -04002776 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002777
Steven Valdez143e8b32016-07-11 13:19:03 -04002778 uint8_t alert = SSL_AD_DECODE_ERROR;
2779 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2780 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2781 ERR_add_error_dataf("extension: %u", (unsigned)type);
2782 *out_alert = alert;
2783 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002784 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002785 }
Adam Langley95c29f32014-06-20 12:00:00 -07002786
David Benjamin54091232016-09-05 12:47:25 -04002787 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002788 if (!(received & (1u << i))) {
2789 /* Extension wasn't observed so call the callback with a NULL
2790 * parameter. */
2791 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002792 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002793 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2794 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002795 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002796 return 0;
2797 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002798 }
2799 }
Adam Langley95c29f32014-06-20 12:00:00 -07002800
Adam Langleyfcf25832014-12-18 17:42:32 -08002801 return 1;
2802}
Adam Langley95c29f32014-06-20 12:00:00 -07002803
David Benjamin0d56f882015-12-19 17:05:56 -05002804static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002805 int ret = SSL_TLSEXT_ERR_NOACK;
2806 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002807
David Benjamin78f8aab2016-03-10 16:33:58 -05002808 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002809 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002810 ssl->ctx->tlsext_servername_arg);
2811 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002812 ret = ssl->initial_ctx->tlsext_servername_callback(
2813 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002814 }
Adam Langley95c29f32014-06-20 12:00:00 -07002815
Adam Langleyfcf25832014-12-18 17:42:32 -08002816 switch (ret) {
2817 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002818 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002819 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002820
Adam Langleyfcf25832014-12-18 17:42:32 -08002821 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002822 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002823 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002824
Adam Langleyfcf25832014-12-18 17:42:32 -08002825 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002826 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002827 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002828
Adam Langleyfcf25832014-12-18 17:42:32 -08002829 default:
2830 return 1;
2831 }
2832}
Adam Langleyed8270a2014-09-02 13:52:56 -07002833
David Benjamin0d56f882015-12-19 17:05:56 -05002834static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002835 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002836 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002837
David Benjamin78f8aab2016-03-10 16:33:58 -05002838 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002839 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002840 ssl->ctx->tlsext_servername_arg);
2841 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002842 ret = ssl->initial_ctx->tlsext_servername_callback(
2843 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002844 }
Adam Langley95c29f32014-06-20 12:00:00 -07002845
Adam Langleyfcf25832014-12-18 17:42:32 -08002846 switch (ret) {
2847 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002848 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002849 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002850
Adam Langleyfcf25832014-12-18 17:42:32 -08002851 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002852 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002853 return 1;
2854
2855 default:
2856 return 1;
2857 }
2858}
2859
David Benjamin0d56f882015-12-19 17:05:56 -05002860int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002861 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002862 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2863 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002864 return 0;
2865 }
2866
David Benjamin0d56f882015-12-19 17:05:56 -05002867 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002868 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002869 return 0;
2870 }
2871
2872 return 1;
2873}
Adam Langley95c29f32014-06-20 12:00:00 -07002874
David Benjamine3aa1d92015-06-16 15:34:50 -04002875int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002876 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002877 size_t ticket_len, const uint8_t *session_id,
2878 size_t session_id_len) {
2879 int ret = 1; /* Most errors are non-fatal. */
2880 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2881 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002882
David Benjamine3aa1d92015-06-16 15:34:50 -04002883 HMAC_CTX hmac_ctx;
2884 HMAC_CTX_init(&hmac_ctx);
2885 EVP_CIPHER_CTX cipher_ctx;
2886 EVP_CIPHER_CTX_init(&cipher_ctx);
2887
David Benjaminef1b0092015-11-21 14:05:44 -05002888 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002889 *out_session = NULL;
2890
Steven Valdez4aa154e2016-07-29 14:32:55 -04002891 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
2892 goto done;
2893 }
2894
David Benjamine3aa1d92015-06-16 15:34:50 -04002895 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2896 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002897 }
2898
David Benjaminadcc3952015-04-26 13:07:57 -04002899 /* Ensure there is room for the key name and the largest IV
2900 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2901 * the maximum IV length should be well under the minimum size for the
2902 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002903 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2904 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002905 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002906 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002907
David Benjamine3aa1d92015-06-16 15:34:50 -04002908 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002909 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2910 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2911 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002912 if (cb_ret < 0) {
2913 ret = 0;
2914 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002915 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002916 if (cb_ret == 0) {
2917 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002918 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002919 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002920 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002921 }
2922 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002923 /* Check the key name matches. */
2924 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2925 SSL_TICKET_KEY_NAME_LEN) != 0) {
2926 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002927 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002928 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2929 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002930 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002931 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2932 ssl_ctx->tlsext_tick_aes_key, iv)) {
2933 ret = 0;
2934 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002935 }
2936 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002937 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002938
David Benjamine3aa1d92015-06-16 15:34:50 -04002939 /* Check the MAC at the end of the ticket. */
2940 uint8_t mac[EVP_MAX_MD_SIZE];
2941 size_t mac_len = HMAC_size(&hmac_ctx);
2942 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002943 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002944 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002945 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002946 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2947 HMAC_Final(&hmac_ctx, mac, NULL);
2948 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2949 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002950 }
2951
David Benjamine3aa1d92015-06-16 15:34:50 -04002952 /* Decrypt the session data. */
2953 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2954 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2955 mac_len;
2956 plaintext = OPENSSL_malloc(ciphertext_len);
2957 if (plaintext == NULL) {
2958 ret = 0;
2959 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002960 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002961 if (ciphertext_len >= INT_MAX) {
2962 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002963 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002964 int len1, len2;
2965 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2966 (int)ciphertext_len) ||
2967 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2968 ERR_clear_error(); /* Don't leave an error on the queue. */
2969 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002970 }
2971
David Benjamine3aa1d92015-06-16 15:34:50 -04002972 /* Decode the session. */
2973 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2974 if (session == NULL) {
2975 ERR_clear_error(); /* Don't leave an error on the queue. */
2976 goto done;
2977 }
2978
2979 /* Copy the client's session ID into the new session, to denote the ticket has
2980 * been accepted. */
2981 memcpy(session->session_id, session_id, session_id_len);
2982 session->session_id_length = session_id_len;
2983
Steven Valdez4aa154e2016-07-29 14:32:55 -04002984 if (!ssl_session_is_context_valid(ssl, session) ||
2985 !ssl_session_is_time_valid(ssl, session)) {
2986 SSL_SESSION_free(session);
2987 session = NULL;
2988 }
2989
David Benjamine3aa1d92015-06-16 15:34:50 -04002990 *out_session = session;
2991
2992done:
2993 OPENSSL_free(plaintext);
2994 HMAC_CTX_cleanup(&hmac_ctx);
2995 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2996 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002997}
Adam Langley95c29f32014-06-20 12:00:00 -07002998
Steven Valdez0d62f262015-09-04 12:41:04 -04002999int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003000 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05003001 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003002 return 1;
3003 }
David Benjamincd996942014-07-20 16:23:51 -04003004
David Benjamin0fc37ef2016-08-17 15:29:46 -04003005 SSL_HANDSHAKE *hs = ssl->s3->hs;
3006 OPENSSL_free(hs->peer_sigalgs);
3007 hs->peer_sigalgs = NULL;
3008 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003009
3010 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003011 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003012 return 0;
3013 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003014 num_sigalgs /= 2;
3015
3016 /* supported_signature_algorithms in the certificate request is
3017 * allowed to be empty. */
3018 if (num_sigalgs == 0) {
3019 return 1;
3020 }
3021
Steven Valdez02563852016-06-23 13:33:05 -04003022 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3023 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003024 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3025 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003026 return 0;
3027 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003028 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003029
3030 CBS sigalgs;
3031 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003032 for (size_t i = 0; i < num_sigalgs; i++) {
3033 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003034 return 0;
3035 }
3036 }
Adam Langley95c29f32014-06-20 12:00:00 -07003037
Adam Langleyfcf25832014-12-18 17:42:32 -08003038 return 1;
3039}
David Benjaminec2f27d2014-11-13 19:17:25 -05003040
David Benjaminea9a0d52016-07-08 15:52:59 -07003041int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003042 CERT *cert = ssl->cert;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003043 SSL_HANDSHAKE *hs = ssl->s3->hs;
David Benjaminec2f27d2014-11-13 19:17:25 -05003044
Steven Valdezf0451ca2016-06-29 13:16:27 -04003045 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3046 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3047 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003048 int type = ssl_private_key_type(ssl);
3049 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003050 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003051 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003052 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003053 if (ssl_is_ecdsa_key_type(type)) {
3054 *out = SSL_SIGN_ECDSA_SHA1;
3055 return 1;
3056 }
3057 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3058 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003059 }
3060
Steven Valdezeff1e8d2016-07-06 14:24:47 -04003061 const uint16_t *sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003062 size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07003063 if (cert->sigalgs != NULL) {
3064 sigalgs = cert->sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003065 num_sigalgs = cert->num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003066 }
3067
David Benjamin0fc37ef2016-08-17 15:29:46 -04003068 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3069 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3070 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003071 /* If the client didn't specify any signature_algorithms extension then
3072 * we can assume that it supports SHA1. See
3073 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3074 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3075 SSL_SIGN_ECDSA_SHA1};
3076 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003077 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003078 }
3079
David Benjamin0fc37ef2016-08-17 15:29:46 -04003080 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003081 uint16_t sigalg = sigalgs[i];
3082 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3083 * negotiated. */
3084 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3085 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3086 continue;
3087 }
3088
David Benjamin0fc37ef2016-08-17 15:29:46 -04003089 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003090 if (sigalg == peer_sigalgs[j]) {
3091 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003092 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003093 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003094 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003095 }
Adam Langley95c29f32014-06-20 12:00:00 -07003096
David Benjaminea9a0d52016-07-08 15:52:59 -07003097 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3098 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003099}
Adam Langley95c29f32014-06-20 12:00:00 -07003100
David Benjamind6a4ae92015-08-06 11:10:51 -04003101int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
3102 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003103 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003104
3105 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003106 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
3107 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08003108 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003109
David Benjamind6a4ae92015-08-06 11:10:51 -04003110 static const char kClientIDMagic[] = "TLS Channel ID signature";
3111 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
3112
Steven Valdez87eab492016-06-27 16:34:59 -04003113 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003114 static const char kResumptionMagic[] = "Resumption";
3115 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
3116 if (ssl->session->original_handshake_hash_len == 0) {
3117 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3118 goto err;
3119 }
3120 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
3121 ssl->session->original_handshake_hash_len);
3122 }
3123
3124 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3125 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3126 sizeof(handshake_hash));
3127 if (handshake_hash_len < 0) {
3128 goto err;
3129 }
3130 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
3131 unsigned len_u;
3132 EVP_DigestFinal_ex(&ctx, out, &len_u);
3133 *out_len = len_u;
3134
3135 ret = 1;
3136
3137err:
3138 EVP_MD_CTX_cleanup(&ctx);
3139 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003140}
Adam Langley1258b6a2014-06-20 12:00:00 -07003141
3142/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003143 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003144 * data. */
3145int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003146 int digest_len;
3147 /* This function should never be called for a resumed session because the
3148 * handshake hashes that we wish to record are for the original, full
3149 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003150 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003151 return -1;
3152 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003153
Adam Langleyfcf25832014-12-18 17:42:32 -08003154 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003155 tls1_handshake_digest(
3156 ssl, ssl->s3->new_session->original_handshake_hash,
3157 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003158 if (digest_len < 0) {
3159 return -1;
3160 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003161
Steven Valdez87eab492016-06-27 16:34:59 -04003162 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003163
Adam Langleyfcf25832014-12-18 17:42:32 -08003164 return 1;
3165}