blob: e550796861958057051e9654e6945d7fef962ceb [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin03973092014-06-24 23:27:17 -0400116#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400117#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/evp.h>
120#include <openssl/hmac.h>
121#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400122#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/rand.h>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700124#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -0400127#include "../crypto/internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800128
129
David Benjamin0d56f882015-12-19 17:05:56 -0500130static int ssl_check_clienthello_tlsext(SSL *ssl);
131static int ssl_check_serverhello_tlsext(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700132
Adam Langleyfcf25832014-12-18 17:42:32 -0800133static int compare_uint16_t(const void *p1, const void *p2) {
134 uint16_t u1 = *((const uint16_t *)p1);
135 uint16_t u2 = *((const uint16_t *)p2);
136 if (u1 < u2) {
137 return -1;
138 } else if (u1 > u2) {
139 return 1;
140 } else {
141 return 0;
142 }
143}
David Benjamin35a7a442014-07-05 00:23:20 -0400144
Adam Langleyfcf25832014-12-18 17:42:32 -0800145/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
146 * more than one extension of the same type in a ClientHello or ServerHello.
147 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400148 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800149static int tls1_check_duplicate_extensions(const CBS *cbs) {
150 CBS extensions = *cbs;
151 size_t num_extensions = 0, i = 0;
152 uint16_t *extension_types = NULL;
153 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400154
Adam Langleyfcf25832014-12-18 17:42:32 -0800155 /* First pass: count the extensions. */
156 while (CBS_len(&extensions) > 0) {
157 uint16_t type;
158 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400159
Adam Langleyfcf25832014-12-18 17:42:32 -0800160 if (!CBS_get_u16(&extensions, &type) ||
161 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
162 goto done;
163 }
David Benjamin35a7a442014-07-05 00:23:20 -0400164
Adam Langleyfcf25832014-12-18 17:42:32 -0800165 num_extensions++;
166 }
David Benjamin35a7a442014-07-05 00:23:20 -0400167
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 if (num_extensions == 0) {
169 return 1;
170 }
David Benjamin9a373592014-07-25 04:27:53 -0400171
Brian Smith5ba06892016-02-07 09:36:04 -1000172 extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400174 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800175 goto done;
176 }
David Benjamin35a7a442014-07-05 00:23:20 -0400177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 /* Second pass: gather the extension types. */
179 extensions = *cbs;
180 for (i = 0; i < num_extensions; i++) {
181 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400182
Adam Langleyfcf25832014-12-18 17:42:32 -0800183 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
184 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
185 /* This should not happen. */
186 goto done;
187 }
188 }
189 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400190
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 /* Sort the extensions and make sure there are no duplicates. */
192 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
193 for (i = 1; i < num_extensions; i++) {
194 if (extension_types[i - 1] == extension_types[i]) {
195 goto done;
196 }
197 }
David Benjamin35a7a442014-07-05 00:23:20 -0400198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 ret = 1;
200
David Benjamin35a7a442014-07-05 00:23:20 -0400201done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400202 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 return ret;
204}
David Benjamin35a7a442014-07-05 00:23:20 -0400205
David Benjamind7573dc2016-07-20 19:05:22 +0200206int ssl_early_callback_init(SSL *ssl, struct ssl_early_callback_ctx *ctx,
207 const uint8_t *in, size_t in_len) {
208 memset(ctx, 0, sizeof(*ctx));
209 ctx->ssl = ssl;
210 ctx->client_hello = in;
211 ctx->client_hello_len = in_len;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400212
David Benjamine14ff062016-08-09 16:21:24 -0400213 CBS client_hello, random, session_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
David Benjamine14ff062016-08-09 16:21:24 -0400215 if (!CBS_get_u16(&client_hello, &ctx->version) ||
216 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
217 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
218 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 return 0;
220 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700221
David Benjamine14ff062016-08-09 16:21:24 -0400222 ctx->random = CBS_data(&random);
223 ctx->random_len = CBS_len(&random);
Adam Langleyfcf25832014-12-18 17:42:32 -0800224 ctx->session_id = CBS_data(&session_id);
225 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700226
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 /* Skip past DTLS cookie */
David Benjamince079fd2016-08-02 16:22:34 -0400228 if (SSL_is_dtls(ctx->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400230 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
231 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 return 0;
233 }
234 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700235
David Benjamine14ff062016-08-09 16:21:24 -0400236 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400238 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
239 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 CBS_len(&compression_methods) < 1) {
241 return 0;
242 }
David Benjamine14ff062016-08-09 16:21:24 -0400243
244 ctx->cipher_suites = CBS_data(&cipher_suites);
245 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 ctx->compression_methods = CBS_data(&compression_methods);
247 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 /* If the ClientHello ends here then it's valid, but doesn't have any
250 * extensions. (E.g. SSLv3.) */
251 if (CBS_len(&client_hello) == 0) {
252 ctx->extensions = NULL;
253 ctx->extensions_len = 0;
254 return 1;
255 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 /* Extract extensions and check it is valid. */
David Benjamine14ff062016-08-09 16:21:24 -0400258 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
260 !tls1_check_duplicate_extensions(&extensions) ||
261 CBS_len(&client_hello) != 0) {
262 return 0;
263 }
David Benjamine14ff062016-08-09 16:21:24 -0400264
Adam Langleyfcf25832014-12-18 17:42:32 -0800265 ctx->extensions = CBS_data(&extensions);
266 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700267
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 return 1;
269}
Adam Langleydc9b1412014-06-20 12:00:00 -0700270
David Benjamincec73442016-08-02 17:41:33 -0400271int ssl_early_callback_get_extension(const struct ssl_early_callback_ctx *ctx,
272 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 while (CBS_len(&extensions) != 0) {
David Benjamincec73442016-08-02 17:41:33 -0400276 /* Decode the next extension. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 uint16_t type;
278 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 if (!CBS_get_u16(&extensions, &type) ||
280 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
281 return 0;
282 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400285 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800286 return 1;
287 }
288 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700289
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 return 0;
291}
Adam Langley95c29f32014-06-20 12:00:00 -0700292
David Benjamincec73442016-08-02 17:41:33 -0400293int SSL_early_callback_ctx_extension_get(
294 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
295 const uint8_t **out_data, size_t *out_len) {
296 CBS cbs;
297 if (!ssl_early_callback_get_extension(ctx, &cbs, extension_type)) {
298 return 0;
299 }
300
301 *out_data = CBS_data(&cbs);
302 *out_len = CBS_len(&cbs);
303 return 1;
304}
305
Steven Valdezce902a92016-05-17 11:47:53 -0400306static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400307 SSL_CURVE_X25519,
308 SSL_CURVE_SECP256R1,
309 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700310#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin9e68f192016-06-30 14:55:33 -0400311 SSL_CURVE_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700312#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800313};
Adam Langley95c29f32014-06-20 12:00:00 -0700314
Steven Valdez5440fe02016-07-18 12:40:30 -0400315void tls1_get_grouplist(SSL *ssl, int get_peer_groups,
316 const uint16_t **out_group_ids,
317 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400318 if (get_peer_groups) {
319 /* Only clients send a supported group list, so this function is only
320 * called on the server. */
David Benjamin0d56f882015-12-19 17:05:56 -0500321 assert(ssl->server);
Steven Valdezce902a92016-05-17 11:47:53 -0400322 *out_group_ids = ssl->s3->tmp.peer_supported_group_list;
323 *out_group_ids_len = ssl->s3->tmp.peer_supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800324 return;
325 }
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Steven Valdezce902a92016-05-17 11:47:53 -0400327 *out_group_ids = ssl->supported_group_list;
328 *out_group_ids_len = ssl->supported_group_list_len;
329 if (!*out_group_ids) {
330 *out_group_ids = kDefaultGroups;
Steven Valdezcb966542016-08-17 16:56:14 -0400331 *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 }
333}
David Benjamined439582014-07-14 19:13:02 -0400334
Steven Valdezce902a92016-05-17 11:47:53 -0400335int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) {
336 const uint16_t *groups, *peer_groups, *pref, *supp;
337 size_t groups_len, peer_groups_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400338
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 /* Can't do anything on client side */
David Benjamin4298d772015-12-19 00:18:25 -0500340 if (ssl->server == 0) {
341 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800342 }
343
Steven Valdezce902a92016-05-17 11:47:53 -0400344 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
345 tls1_get_grouplist(ssl, 1 /* peer groups */, &peer_groups, &peer_groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800346
Steven Valdezce902a92016-05-17 11:47:53 -0400347 if (peer_groups_len == 0) {
348 /* Clients are not required to send a supported_groups extension. In this
349 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin4298d772015-12-19 00:18:25 -0500350 * section 4, paragraph 3.
351 *
352 * However, in the interests of compatibility, we will skip ECDH if the
353 * client didn't send an extension because we can't be sure that they'll
Steven Valdezce902a92016-05-17 11:47:53 -0400354 * support our favoured group. */
David Benjamin4298d772015-12-19 00:18:25 -0500355 return 0;
David Benjamin55a43642015-04-20 14:45:55 -0400356 }
357
David Benjamin4298d772015-12-19 00:18:25 -0500358 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400359 pref = groups;
360 pref_len = groups_len;
361 supp = peer_groups;
362 supp_len = peer_groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400363 } else {
Steven Valdezce902a92016-05-17 11:47:53 -0400364 pref = peer_groups;
365 pref_len = peer_groups_len;
366 supp = groups;
367 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400368 }
369
370 for (i = 0; i < pref_len; i++) {
371 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800372 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400373 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500374 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 }
376 }
377 }
378
David Benjamin4298d772015-12-19 00:18:25 -0500379 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800380}
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Steven Valdezce902a92016-05-17 11:47:53 -0400382int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400384 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800385 size_t i;
386
Steven Valdezce902a92016-05-17 11:47:53 -0400387 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
388 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 return 0;
390 }
391
392 for (i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400393 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
394 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800395 return 0;
396 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 }
398
Steven Valdezce902a92016-05-17 11:47:53 -0400399 OPENSSL_free(*out_group_ids);
400 *out_group_ids = group_ids;
401 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800402
403 return 1;
404}
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Steven Valdezce902a92016-05-17 11:47:53 -0400406/* tls1_curve_params_from_ec_key sets |*out_group_id| and |*out_comp_id| to the
407 * TLS group ID and point format, respectively, for |ec|. It returns one on
David Benjamin072334d2014-07-13 16:24:27 -0400408 * success and zero on failure. */
Steven Valdezce902a92016-05-17 11:47:53 -0400409static int tls1_curve_params_from_ec_key(uint16_t *out_group_id,
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 uint8_t *out_comp_id, EC_KEY *ec) {
411 int nid;
412 uint16_t id;
413 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 if (ec == NULL) {
416 return 0;
417 }
Adam Langley95c29f32014-06-20 12:00:00 -0700418
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 grp = EC_KEY_get0_group(ec);
420 if (grp == NULL) {
421 return 0;
422 }
David Benjamin072334d2014-07-13 16:24:27 -0400423
Steven Valdezce902a92016-05-17 11:47:53 -0400424 /* Determine group ID */
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 nid = EC_GROUP_get_curve_name(grp);
Steven Valdezce902a92016-05-17 11:47:53 -0400426 if (!ssl_nid_to_group_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 return 0;
428 }
David Benjamin072334d2014-07-13 16:24:27 -0400429
Steven Valdezce902a92016-05-17 11:47:53 -0400430 /* Set the named group ID. Arbitrary explicit groups are not supported. */
431 *out_group_id = id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800432
433 if (out_comp_id) {
434 if (EC_KEY_get0_public_key(ec) == NULL) {
435 return 0;
436 }
437 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
438 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
439 } else {
440 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
441 }
442 }
443
444 return 1;
445}
David Benjamin072334d2014-07-13 16:24:27 -0400446
Steven Valdezce902a92016-05-17 11:47:53 -0400447/* tls1_check_group_id returns one if |group_id| is consistent with both our
448 * and the peer's group preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400449 * preferences are checked; the peer (the server) does not send preferences. */
Steven Valdezce902a92016-05-17 11:47:53 -0400450int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
451 const uint16_t *groups;
452 size_t groups_len, i, get_peer_groups;
David Benjamin42e9a772014-09-02 23:18:44 -0400453
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 /* Check against our list, then the peer's list. */
Steven Valdezce902a92016-05-17 11:47:53 -0400455 for (get_peer_groups = 0; get_peer_groups <= 1; get_peer_groups++) {
456 if (get_peer_groups && !ssl->server) {
David Benjamin55a43642015-04-20 14:45:55 -0400457 /* Servers do not present a preference list so, if we are a client, only
458 * check our list. */
459 continue;
460 }
461
Steven Valdezce902a92016-05-17 11:47:53 -0400462 tls1_get_grouplist(ssl, get_peer_groups, &groups, &groups_len);
463 if (get_peer_groups && groups_len == 0) {
464 /* Clients are not required to send a supported_groups extension. In this
465 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin55a43642015-04-20 14:45:55 -0400466 * section 4, paragraph 3. */
467 continue;
468 }
Steven Valdezce902a92016-05-17 11:47:53 -0400469 for (i = 0; i < groups_len; i++) {
470 if (groups[i] == group_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 break;
472 }
473 }
Adam Langley95c29f32014-06-20 12:00:00 -0700474
Steven Valdezce902a92016-05-17 11:47:53 -0400475 if (i == groups_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800476 return 0;
477 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 }
David Benjamin033e5f42014-11-13 18:47:41 -0500479
Adam Langleyfcf25832014-12-18 17:42:32 -0800480 return 1;
481}
David Benjamin033e5f42014-11-13 18:47:41 -0500482
David Benjamin0d56f882015-12-19 17:05:56 -0500483int tls1_check_ec_cert(SSL *ssl, X509 *x) {
David Benjamin75ea5bb2016-07-08 17:43:29 -0700484 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
485 /* In TLS 1.3, the ECDSA curve is negotiated via signature algorithms. */
486 return 1;
487 }
488
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 EVP_PKEY *pkey = X509_get_pubkey(x);
David Benjamin75ea5bb2016-07-08 17:43:29 -0700490 if (pkey == NULL) {
491 return 0;
492 }
493
494 int ret = 0;
Steven Valdezce902a92016-05-17 11:47:53 -0400495 uint16_t group_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 uint8_t comp_id;
David Benjamin758d1272015-11-20 17:47:25 -0500497 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
498 if (ec_key == NULL ||
Steven Valdezce902a92016-05-17 11:47:53 -0400499 !tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) ||
500 !tls1_check_group_id(ssl, group_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400501 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800502 goto done;
503 }
504
505 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500506
507done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400508 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800509 return ret;
510}
David Benjamin42e9a772014-09-02 23:18:44 -0400511
Adam Langley95c29f32014-06-20 12:00:00 -0700512/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800513 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700514
Steven Valdez02563852016-06-23 13:33:05 -0400515static const uint16_t kDefaultSignatureAlgorithms[] = {
516 SSL_SIGN_RSA_PKCS1_SHA512,
517 SSL_SIGN_ECDSA_SECP521R1_SHA512,
Adam Langley95c29f32014-06-20 12:00:00 -0700518
Steven Valdez02563852016-06-23 13:33:05 -0400519 SSL_SIGN_RSA_PKCS1_SHA384,
520 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Adam Langley95c29f32014-06-20 12:00:00 -0700521
Steven Valdez02563852016-06-23 13:33:05 -0400522 SSL_SIGN_RSA_PKCS1_SHA256,
523 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Adam Langley95c29f32014-06-20 12:00:00 -0700524
Steven Valdez02563852016-06-23 13:33:05 -0400525 SSL_SIGN_RSA_PKCS1_SHA1,
526 SSL_SIGN_ECDSA_SHA1,
Adam Langley95c29f32014-06-20 12:00:00 -0700527};
David Benjamin05da6e12014-07-12 20:42:55 -0400528
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400529static const uint16_t kDefaultTLS13SignatureAlgorithms[] = {
530 SSL_SIGN_RSA_PSS_SHA512,
531 SSL_SIGN_RSA_PKCS1_SHA512,
532 SSL_SIGN_ECDSA_SECP521R1_SHA512,
533
534 SSL_SIGN_RSA_PSS_SHA384,
535 SSL_SIGN_RSA_PKCS1_SHA384,
536 SSL_SIGN_ECDSA_SECP384R1_SHA384,
537
538 SSL_SIGN_RSA_PSS_SHA256,
539 SSL_SIGN_RSA_PKCS1_SHA256,
540 SSL_SIGN_ECDSA_SECP256R1_SHA256,
541
542 SSL_SIGN_RSA_PKCS1_SHA1,
543 SSL_SIGN_ECDSA_SHA1,
544};
545
Steven Valdez02563852016-06-23 13:33:05 -0400546size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
David Benjamin0e950152016-08-13 21:08:56 -0400547 uint16_t min_version, max_version;
548 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
549 assert(0); /* This should never happen. */
550
551 /* Return an empty list. */
552 ERR_clear_error();
553 *psigs = NULL;
554 return 0;
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400555 }
556
David Benjamin0e950152016-08-13 21:08:56 -0400557 /* TODO(davidben): Once TLS 1.3 has finalized, probably just advertise the
558 * same algorithm list regardless, as long as no fallback is needed. Note this
559 * may require care due to lingering NSS servers affected by
560 * https://bugzilla.mozilla.org/show_bug.cgi?id=1119983 */
561 if (max_version >= TLS1_3_VERSION) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400562 *psigs = kDefaultTLS13SignatureAlgorithms;
Steven Valdezcb966542016-08-17 16:56:14 -0400563 return OPENSSL_ARRAY_SIZE(kDefaultTLS13SignatureAlgorithms);
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400564 }
565
Steven Valdez02563852016-06-23 13:33:05 -0400566 *psigs = kDefaultSignatureAlgorithms;
Steven Valdezcb966542016-08-17 16:56:14 -0400567 return OPENSSL_ARRAY_SIZE(kDefaultSignatureAlgorithms);
Adam Langleyfcf25832014-12-18 17:42:32 -0800568}
Adam Langley95c29f32014-06-20 12:00:00 -0700569
David Benjamin887c3002016-07-08 16:15:32 -0700570int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
Steven Valdez02563852016-06-23 13:33:05 -0400571 const uint16_t *sent_sigs;
572 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800573
Adam Langleyfcf25832014-12-18 17:42:32 -0800574 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500575 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400576 for (i = 0; i < sent_sigslen; i++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400577 if (sigalg == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800578 break;
579 }
580 }
581
David Benjamin788be4a2015-10-30 17:50:57 -0400582 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400583 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800584 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
585 return 0;
586 }
587
Adam Langleyfcf25832014-12-18 17:42:32 -0800588 return 1;
589}
590
591/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
592 * supported or doesn't appear in supported signature algorithms. Unlike
593 * ssl_cipher_get_disabled this applies to a specific session and not global
594 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500595void ssl_set_client_disabled(SSL *ssl) {
596 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800597 int have_rsa = 0, have_ecdsa = 0;
598 c->mask_a = 0;
599 c->mask_k = 0;
600
Adam Langleyfcf25832014-12-18 17:42:32 -0800601 /* Now go through all signature algorithms seeing if we support any for RSA,
602 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin0fc37ef2016-08-17 15:29:46 -0400603 const uint16_t *sigalgs;
604 size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
605 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -0400606 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400607 case SSL_SIGN_RSA_PSS_SHA512:
608 case SSL_SIGN_RSA_PSS_SHA384:
609 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400610 case SSL_SIGN_RSA_PKCS1_SHA512:
611 case SSL_SIGN_RSA_PKCS1_SHA384:
612 case SSL_SIGN_RSA_PKCS1_SHA256:
613 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800614 have_rsa = 1;
615 break;
616
Steven Valdez02563852016-06-23 13:33:05 -0400617 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
618 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
619 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
620 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800621 have_ecdsa = 1;
622 break;
623 }
624 }
625
626 /* Disable auth if we don't include any appropriate signature algorithms. */
627 if (!have_rsa) {
628 c->mask_a |= SSL_aRSA;
629 }
630 if (!have_ecdsa) {
631 c->mask_a |= SSL_aECDSA;
632 }
633
634 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500635 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 c->mask_a |= SSL_aPSK;
637 c->mask_k |= SSL_kPSK;
638 }
639}
Adam Langley95c29f32014-06-20 12:00:00 -0700640
Adam Langley614c66a2015-06-12 15:26:58 -0700641/* tls_extension represents a TLS extension that is handled internally. The
642 * |init| function is called for each handshake, before any other functions of
643 * the extension. Then the add and parse callbacks are called as needed.
644 *
645 * The parse callbacks receive a |CBS| that contains the contents of the
646 * extension (i.e. not including the type and length bytes). If an extension is
647 * not received then the parse callbacks will be called with a NULL CBS so that
648 * they can do any processing needed to handle the absence of an extension.
649 *
650 * The add callbacks receive a |CBB| to which the extension can be appended but
651 * the function is responsible for appending the type and length bytes too.
652 *
653 * All callbacks return one for success and zero for error. If a parse function
654 * returns zero then a fatal alert with value |*out_alert| will be sent. If
655 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
656struct tls_extension {
657 uint16_t value;
658 void (*init)(SSL *ssl);
659
660 int (*add_clienthello)(SSL *ssl, CBB *out);
661 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
662
663 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
664 int (*add_serverhello)(SSL *ssl, CBB *out);
665};
666
Steven Valdez6b8509a2016-07-12 13:38:32 -0400667static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
668 if (contents != NULL) {
669 /* Servers MUST NOT send this extension. */
670 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
671 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
672 return 0;
673 }
674
675 return 1;
676}
677
678static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
679 /* This extension from the client is handled elsewhere. */
680 return 1;
681}
682
683static int dont_add_serverhello(SSL *ssl, CBB *out) {
684 return 1;
685}
Adam Langley614c66a2015-06-12 15:26:58 -0700686
687/* Server name indication (SNI).
688 *
689 * https://tools.ietf.org/html/rfc6066#section-3. */
690
691static void ext_sni_init(SSL *ssl) {
692 ssl->s3->tmp.should_ack_sni = 0;
693}
694
695static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
696 if (ssl->tlsext_hostname == NULL) {
697 return 1;
698 }
699
700 CBB contents, server_name_list, name;
701 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
702 !CBB_add_u16_length_prefixed(out, &contents) ||
703 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
704 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
705 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
706 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
707 strlen(ssl->tlsext_hostname)) ||
708 !CBB_flush(out)) {
709 return 0;
710 }
711
712 return 1;
713}
714
David Benjamin0d56f882015-12-19 17:05:56 -0500715static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
716 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700717 if (contents == NULL) {
718 return 1;
719 }
720
721 if (CBS_len(contents) != 0) {
722 return 0;
723 }
724
725 assert(ssl->tlsext_hostname != NULL);
726
Steven Valdez87eab492016-06-27 16:34:59 -0400727 if (ssl->session == NULL) {
728 assert(ssl->s3->new_session->tlsext_hostname == NULL);
729 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
730 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700731 *out_alert = SSL_AD_INTERNAL_ERROR;
732 return 0;
733 }
734 }
735
736 return 1;
737}
738
David Benjamin0d56f882015-12-19 17:05:56 -0500739static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
740 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700741 if (contents == NULL) {
742 return 1;
743 }
744
David Benjamin9b611e22016-03-03 08:48:30 -0500745 CBS server_name_list, host_name;
746 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700747 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500748 !CBS_get_u8(&server_name_list, &name_type) ||
749 /* Although the server_name extension was intended to be extensible to
750 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
751 * different name types will cause an error. Further, RFC 4366 originally
752 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
753 * adding new name types is no longer feasible.
754 *
755 * Act as if the extensibility does not exist to simplify parsing. */
756 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
757 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700758 CBS_len(contents) != 0) {
759 return 0;
760 }
761
David Benjamin9b611e22016-03-03 08:48:30 -0500762 if (name_type != TLSEXT_NAMETYPE_host_name ||
763 CBS_len(&host_name) == 0 ||
764 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
765 CBS_contains_zero_byte(&host_name)) {
766 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
767 return 0;
768 }
Adam Langley614c66a2015-06-12 15:26:58 -0700769
David Benjamin9b611e22016-03-03 08:48:30 -0500770 /* TODO(davidben): SNI should be resolved before resumption. We have the
771 * early callback as a replacement, but we should fix the current callback
772 * and avoid the need for |SSL_CTX_set_session_id_context|. */
Steven Valdez87eab492016-06-27 16:34:59 -0400773 if (ssl->session == NULL) {
774 assert(ssl->s3->new_session->tlsext_hostname == NULL);
David Benjamin9b611e22016-03-03 08:48:30 -0500775
776 /* Copy the hostname as a string. */
Steven Valdez87eab492016-06-27 16:34:59 -0400777 if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) {
David Benjamin9b611e22016-03-03 08:48:30 -0500778 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700779 return 0;
780 }
781
David Benjamin9b611e22016-03-03 08:48:30 -0500782 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700783 }
784
785 return 1;
786}
787
788static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez87eab492016-06-27 16:34:59 -0400789 if (ssl->session != NULL ||
Adam Langley614c66a2015-06-12 15:26:58 -0700790 !ssl->s3->tmp.should_ack_sni ||
Steven Valdez87eab492016-06-27 16:34:59 -0400791 ssl->s3->new_session->tlsext_hostname == NULL) {
Adam Langley614c66a2015-06-12 15:26:58 -0700792 return 1;
793 }
794
795 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
796 !CBB_add_u16(out, 0 /* length */)) {
797 return 0;
798 }
799
800 return 1;
801}
802
803
Adam Langley5021b222015-06-12 18:27:58 -0700804/* Renegotiation indication.
805 *
806 * https://tools.ietf.org/html/rfc5746 */
807
808static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400809 uint16_t min_version, max_version;
810 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
811 return 0;
812 }
813
814 /* Renegotiation indication is not necessary in TLS 1.3. */
815 if (min_version >= TLS1_3_VERSION) {
816 return 1;
817 }
818
Adam Langley5021b222015-06-12 18:27:58 -0700819 CBB contents, prev_finished;
820 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
821 !CBB_add_u16_length_prefixed(out, &contents) ||
822 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
823 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
824 ssl->s3->previous_client_finished_len) ||
825 !CBB_flush(out)) {
826 return 0;
827 }
828
829 return 1;
830}
831
832static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
833 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400834 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
835 return 0;
836 }
837
David Benjamin3e052de2015-11-25 20:10:31 -0500838 /* Servers may not switch between omitting the extension and supporting it.
839 * See RFC 5746, sections 3.5 and 4.2. */
840 if (ssl->s3->initial_handshake_complete &&
841 (contents != NULL) != ssl->s3->send_connection_binding) {
842 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
843 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
844 return 0;
845 }
846
Adam Langley5021b222015-06-12 18:27:58 -0700847 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500848 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700849 * RI even on initial ServerHello because the client doesn't see any
850 * renegotiation during an attack. However this would mean we could not
851 * connect to any server which doesn't support RI.
852 *
David Benjamine9cddb82015-11-23 14:36:40 -0500853 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
854 * practical terms every client sets it so it's just assumed here. */
855 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700856 }
857
858 const size_t expected_len = ssl->s3->previous_client_finished_len +
859 ssl->s3->previous_server_finished_len;
860
861 /* Check for logic errors */
862 assert(!expected_len || ssl->s3->previous_client_finished_len);
863 assert(!expected_len || ssl->s3->previous_server_finished_len);
864
865 /* Parse out the extension contents. */
866 CBS renegotiated_connection;
867 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
868 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400869 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700870 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
871 return 0;
872 }
873
874 /* Check that the extension matches. */
875 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400876 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700877 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
878 return 0;
879 }
880
881 const uint8_t *d = CBS_data(&renegotiated_connection);
882 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
883 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400884 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700885 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
886 return 0;
887 }
888 d += ssl->s3->previous_client_finished_len;
889
890 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
891 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400892 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700893 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
894 return 0;
895 }
896 ssl->s3->send_connection_binding = 1;
897
898 return 1;
899}
900
901static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
902 CBS *contents) {
903 /* Renegotiation isn't supported as a server so this function should never be
904 * called after the initial handshake. */
905 assert(!ssl->s3->initial_handshake_complete);
906
Steven Valdez143e8b32016-07-11 13:19:03 -0400907 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
908 return 1;
909 }
910
Adam Langley5021b222015-06-12 18:27:58 -0700911 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400912 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700913 }
914
915 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700916 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
917 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400918 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700919 return 0;
920 }
921
922 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500923 if (!CBS_mem_equal(&renegotiated_connection,
924 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700925 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400926 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700927 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
928 return 0;
929 }
930
931 ssl->s3->send_connection_binding = 1;
932
933 return 1;
934}
935
936static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400937 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
938 return 1;
939 }
940
Adam Langley5021b222015-06-12 18:27:58 -0700941 CBB contents, prev_finished;
942 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
943 !CBB_add_u16_length_prefixed(out, &contents) ||
944 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
945 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
946 ssl->s3->previous_client_finished_len) ||
947 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
948 ssl->s3->previous_server_finished_len) ||
949 !CBB_flush(out)) {
950 return 0;
951 }
952
953 return 1;
954}
955
Adam Langley0a056712015-07-01 15:03:33 -0700956
957/* Extended Master Secret.
958 *
David Benjamin43946d42016-02-01 08:42:19 -0500959 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700960
Adam Langley0a056712015-07-01 15:03:33 -0700961static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -0400962 uint16_t min_version, max_version;
963 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
964 return 0;
965 }
966
967 /* Extended master secret is not necessary in TLS 1.3. */
968 if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
Adam Langley0a056712015-07-01 15:03:33 -0700969 return 1;
970 }
971
972 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
973 !CBB_add_u16(out, 0 /* length */)) {
974 return 0;
975 }
976
977 return 1;
978}
979
980static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
981 CBS *contents) {
David Benjamin163c9562016-08-29 23:14:17 -0400982 /* Whether EMS is negotiated may not change on renegotation. */
983 if (ssl->s3->initial_handshake_complete) {
984 if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
985 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
986 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
987 return 0;
988 }
989
990 return 1;
991 }
992
Adam Langley0a056712015-07-01 15:03:33 -0700993 if (contents == NULL) {
994 return 1;
995 }
996
Steven Valdez143e8b32016-07-11 13:19:03 -0400997 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
998 ssl->version == SSL3_VERSION) {
999 return 0;
1000 }
1001
1002 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -07001003 return 0;
1004 }
1005
1006 ssl->s3->tmp.extended_master_secret = 1;
1007 return 1;
1008}
1009
David Benjamin0d56f882015-12-19 17:05:56 -05001010static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1011 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001012 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1013 ssl->version == SSL3_VERSION) {
1014 return 1;
1015 }
1016
1017 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -07001018 return 1;
1019 }
1020
1021 if (CBS_len(contents) != 0) {
1022 return 0;
1023 }
1024
1025 ssl->s3->tmp.extended_master_secret = 1;
1026 return 1;
1027}
1028
1029static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1030 if (!ssl->s3->tmp.extended_master_secret) {
1031 return 1;
1032 }
1033
1034 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1035 !CBB_add_u16(out, 0 /* length */)) {
1036 return 0;
1037 }
1038
1039 return 1;
1040}
1041
Adam Langley9b05bc52015-07-01 15:25:33 -07001042
1043/* Session tickets.
1044 *
1045 * https://tools.ietf.org/html/rfc5077 */
1046
1047static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
David Benjamin7c7d8312016-08-20 13:39:03 -04001048 uint16_t min_version, max_version;
1049 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1050 return 0;
1051 }
1052
1053 /* TLS 1.3 uses a different ticket extension. */
1054 if (min_version >= TLS1_3_VERSION ||
1055 SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001056 return 1;
1057 }
1058
1059 const uint8_t *ticket_data = NULL;
1060 int ticket_len = 0;
1061
1062 /* Renegotiation does not participate in session resumption. However, still
1063 * advertise the extension to avoid potentially breaking servers which carry
1064 * over the state from the previous handshake, such as OpenSSL servers
1065 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1066 if (!ssl->s3->initial_handshake_complete &&
1067 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001068 ssl->session->tlsext_tick != NULL &&
1069 /* Don't send TLS 1.3 session tickets in the ticket extension. */
1070 ssl->method->version_from_wire(ssl->session->ssl_version) <
1071 TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001072 ticket_data = ssl->session->tlsext_tick;
1073 ticket_len = ssl->session->tlsext_ticklen;
1074 }
1075
1076 CBB ticket;
1077 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1078 !CBB_add_u16_length_prefixed(out, &ticket) ||
1079 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1080 !CBB_flush(out)) {
1081 return 0;
1082 }
1083
1084 return 1;
1085}
1086
1087static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1088 CBS *contents) {
1089 ssl->tlsext_ticket_expected = 0;
1090
1091 if (contents == NULL) {
1092 return 1;
1093 }
1094
Steven Valdez143e8b32016-07-11 13:19:03 -04001095 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1096 return 0;
1097 }
1098
Adam Langley9b05bc52015-07-01 15:25:33 -07001099 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1100 * this function should never be called, even if the server tries to send the
1101 * extension. */
1102 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1103
1104 if (CBS_len(contents) != 0) {
1105 return 0;
1106 }
1107
1108 ssl->tlsext_ticket_expected = 1;
1109 return 1;
1110}
1111
Adam Langley9b05bc52015-07-01 15:25:33 -07001112static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1113 if (!ssl->tlsext_ticket_expected) {
1114 return 1;
1115 }
1116
1117 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1118 * true. */
1119 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1120
1121 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1122 !CBB_add_u16(out, 0 /* length */)) {
1123 return 0;
1124 }
1125
1126 return 1;
1127}
1128
1129
Adam Langley2e857bd2015-07-01 16:09:19 -07001130/* Signature Algorithms.
1131 *
1132 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1133
1134static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjaminb6a0a512016-06-21 10:33:21 -04001135 if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001136 return 1;
1137 }
1138
David Benjamin0fc37ef2016-08-17 15:29:46 -04001139 const uint16_t *sigalgs;
1140 const size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
Adam Langley2e857bd2015-07-01 16:09:19 -07001141
David Benjamin0fc37ef2016-08-17 15:29:46 -04001142 CBB contents, sigalgs_cbb;
Adam Langley2e857bd2015-07-01 16:09:19 -07001143 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1144 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjamin0fc37ef2016-08-17 15:29:46 -04001145 !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
Steven Valdez02563852016-06-23 13:33:05 -04001146 return 0;
1147 }
1148
David Benjamin0fc37ef2016-08-17 15:29:46 -04001149 for (size_t i = 0; i < num_sigalgs; i++) {
1150 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
Steven Valdez02563852016-06-23 13:33:05 -04001151 return 0;
1152 }
1153 }
1154
1155 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001156 return 0;
1157 }
1158
1159 return 1;
1160}
1161
Adam Langley2e857bd2015-07-01 16:09:19 -07001162static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1163 CBS *contents) {
David Benjamin0fc37ef2016-08-17 15:29:46 -04001164 OPENSSL_free(ssl->s3->hs->peer_sigalgs);
1165 ssl->s3->hs->peer_sigalgs = NULL;
1166 ssl->s3->hs->num_peer_sigalgs = 0;
Adam Langley2e857bd2015-07-01 16:09:19 -07001167
Adam Langley2e857bd2015-07-01 16:09:19 -07001168 if (contents == NULL) {
1169 return 1;
1170 }
1171
1172 CBS supported_signature_algorithms;
1173 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001174 CBS_len(contents) != 0 ||
1175 CBS_len(&supported_signature_algorithms) == 0 ||
1176 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001177 return 0;
1178 }
1179
1180 return 1;
1181}
1182
Adam Langley2e857bd2015-07-01 16:09:19 -07001183
Adam Langleybb0bd042015-07-01 16:21:03 -07001184/* OCSP Stapling.
1185 *
1186 * https://tools.ietf.org/html/rfc6066#section-8 */
1187
1188static void ext_ocsp_init(SSL *ssl) {
1189 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001190 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001191}
1192
1193static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1194 if (!ssl->ocsp_stapling_enabled) {
1195 return 1;
1196 }
1197
1198 CBB contents;
1199 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1200 !CBB_add_u16_length_prefixed(out, &contents) ||
1201 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1202 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1203 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1204 !CBB_flush(out)) {
1205 return 0;
1206 }
1207
Adam Langleyce9d85e2016-01-24 15:58:39 -08001208 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001209 return 1;
1210}
1211
1212static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001213 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001214 if (contents == NULL) {
1215 return 1;
1216 }
1217
David Benjamin942f4ed2016-07-16 19:03:49 +03001218 /* OCSP stapling is forbidden on a non-certificate cipher. */
1219 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001220 return 0;
1221 }
1222
David Benjamin942f4ed2016-07-16 19:03:49 +03001223 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1224 if (CBS_len(contents) != 0) {
1225 return 0;
1226 }
1227
1228 ssl->s3->tmp.certificate_status_expected = 1;
1229 return 1;
1230 }
1231
1232 uint8_t status_type;
1233 CBS ocsp_response;
1234 if (!CBS_get_u8(contents, &status_type) ||
1235 status_type != TLSEXT_STATUSTYPE_ocsp ||
1236 !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1237 CBS_len(&ocsp_response) == 0 ||
1238 CBS_len(contents) != 0) {
1239 return 0;
1240 }
1241
Steven Valdez87eab492016-06-27 16:34:59 -04001242 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1243 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001244 *out_alert = SSL_AD_INTERNAL_ERROR;
1245 return 0;
1246 }
1247
Adam Langleybb0bd042015-07-01 16:21:03 -07001248 return 1;
1249}
1250
1251static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1252 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001253 if (contents == NULL) {
1254 return 1;
1255 }
1256
1257 uint8_t status_type;
1258 if (!CBS_get_u8(contents, &status_type)) {
1259 return 0;
1260 }
1261
1262 /* We cannot decide whether OCSP stapling will occur yet because the correct
1263 * SSL_CTX might not have been selected. */
1264 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1265
Adam Langleybb0bd042015-07-01 16:21:03 -07001266 return 1;
1267}
1268
1269static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001270 if (!ssl->s3->tmp.ocsp_stapling_requested ||
1271 ssl->ctx->ocsp_response_length == 0 ||
1272 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001273 return 1;
1274 }
1275
David Benjamin942f4ed2016-07-16 19:03:49 +03001276 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1277 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez87eab492016-06-27 16:34:59 -04001278 if (ssl->session != NULL) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001279 return 1;
1280 }
1281
1282 ssl->s3->tmp.certificate_status_expected = 1;
1283
1284 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1285 CBB_add_u16(out, 0 /* length */);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001286 }
1287
David Benjamin942f4ed2016-07-16 19:03:49 +03001288 CBB body, ocsp_response;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001289 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
David Benjamin942f4ed2016-07-16 19:03:49 +03001290 CBB_add_u16_length_prefixed(out, &body) &&
1291 CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1292 CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1293 CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1294 ssl->ctx->ocsp_response_length) &&
1295 CBB_flush(out);
Adam Langleybb0bd042015-07-01 16:21:03 -07001296}
1297
1298
Adam Langley97dfcbf2015-07-01 18:35:20 -07001299/* Next protocol negotiation.
1300 *
1301 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1302
1303static void ext_npn_init(SSL *ssl) {
1304 ssl->s3->next_proto_neg_seen = 0;
1305}
1306
1307static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1308 if (ssl->s3->initial_handshake_complete ||
1309 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001310 (ssl->options & SSL_OP_DISABLE_NPN) ||
David Benjamince079fd2016-08-02 16:22:34 -04001311 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001312 return 1;
1313 }
1314
1315 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1316 !CBB_add_u16(out, 0 /* length */)) {
1317 return 0;
1318 }
1319
1320 return 1;
1321}
1322
1323static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1324 CBS *contents) {
1325 if (contents == NULL) {
1326 return 1;
1327 }
1328
Steven Valdez143e8b32016-07-11 13:19:03 -04001329 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1330 return 0;
1331 }
1332
Adam Langley97dfcbf2015-07-01 18:35:20 -07001333 /* If any of these are false then we should never have sent the NPN
1334 * extension in the ClientHello and thus this function should never have been
1335 * called. */
1336 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001337 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001338 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001339 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001340
David Benjamin76c2efc2015-08-31 14:24:29 -04001341 if (ssl->s3->alpn_selected != NULL) {
1342 /* NPN and ALPN may not be negotiated in the same connection. */
1343 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1344 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1345 return 0;
1346 }
1347
Adam Langley97dfcbf2015-07-01 18:35:20 -07001348 const uint8_t *const orig_contents = CBS_data(contents);
1349 const size_t orig_len = CBS_len(contents);
1350
1351 while (CBS_len(contents) != 0) {
1352 CBS proto;
1353 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1354 CBS_len(&proto) == 0) {
1355 return 0;
1356 }
1357 }
1358
1359 uint8_t *selected;
1360 uint8_t selected_len;
1361 if (ssl->ctx->next_proto_select_cb(
1362 ssl, &selected, &selected_len, orig_contents, orig_len,
1363 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1364 *out_alert = SSL_AD_INTERNAL_ERROR;
1365 return 0;
1366 }
1367
David Benjamin79978df2015-12-25 15:56:49 -05001368 OPENSSL_free(ssl->s3->next_proto_negotiated);
1369 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1370 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001371 *out_alert = SSL_AD_INTERNAL_ERROR;
1372 return 0;
1373 }
1374
David Benjamin79978df2015-12-25 15:56:49 -05001375 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001376 ssl->s3->next_proto_neg_seen = 1;
1377
1378 return 1;
1379}
1380
1381static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1382 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001383 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1384 return 1;
1385 }
1386
Adam Langley97dfcbf2015-07-01 18:35:20 -07001387 if (contents != NULL && CBS_len(contents) != 0) {
1388 return 0;
1389 }
1390
1391 if (contents == NULL ||
1392 ssl->s3->initial_handshake_complete ||
1393 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1394 * afterwards, parsing the ALPN extension will clear
1395 * |next_proto_neg_seen|. */
1396 ssl->s3->alpn_selected != NULL ||
1397 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001398 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001399 return 1;
1400 }
1401
1402 ssl->s3->next_proto_neg_seen = 1;
1403 return 1;
1404}
1405
1406static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1407 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1408 * parsed. */
1409 if (!ssl->s3->next_proto_neg_seen) {
1410 return 1;
1411 }
1412
1413 const uint8_t *npa;
1414 unsigned npa_len;
1415
1416 if (ssl->ctx->next_protos_advertised_cb(
1417 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1418 SSL_TLSEXT_ERR_OK) {
1419 ssl->s3->next_proto_neg_seen = 0;
1420 return 1;
1421 }
1422
1423 CBB contents;
1424 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1425 !CBB_add_u16_length_prefixed(out, &contents) ||
1426 !CBB_add_bytes(&contents, npa, npa_len) ||
1427 !CBB_flush(out)) {
1428 return 0;
1429 }
1430
1431 return 1;
1432}
1433
1434
Adam Langleyab8d87d2015-07-10 12:21:39 -07001435/* Signed certificate timestamps.
1436 *
1437 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1438
1439static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1440 if (!ssl->signed_cert_timestamps_enabled) {
1441 return 1;
1442 }
1443
1444 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1445 !CBB_add_u16(out, 0 /* length */)) {
1446 return 0;
1447 }
1448
1449 return 1;
1450}
1451
1452static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1453 CBS *contents) {
1454 if (contents == NULL) {
1455 return 1;
1456 }
1457
1458 /* If this is false then we should never have sent the SCT extension in the
1459 * ClientHello and thus this function should never have been called. */
1460 assert(ssl->signed_cert_timestamps_enabled);
1461
1462 if (CBS_len(contents) == 0) {
1463 *out_alert = SSL_AD_DECODE_ERROR;
1464 return 0;
1465 }
1466
1467 /* Session resumption uses the original session information. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001468 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001469 !CBS_stow(
1470 contents,
1471 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1472 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001473 *out_alert = SSL_AD_INTERNAL_ERROR;
1474 return 0;
1475 }
1476
1477 return 1;
1478}
1479
1480static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1481 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001482 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001483}
1484
1485static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001486 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001487 if (ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001488 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001489 return 1;
1490 }
1491
1492 CBB contents;
1493 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1494 CBB_add_u16_length_prefixed(out, &contents) &&
1495 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1496 ssl->ctx->signed_cert_timestamp_list_length) &&
1497 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001498}
1499
1500
Adam Langleyf18e4532015-07-10 13:39:53 -07001501/* Application-level Protocol Negotiation.
1502 *
1503 * https://tools.ietf.org/html/rfc7301 */
1504
1505static void ext_alpn_init(SSL *ssl) {
1506 OPENSSL_free(ssl->s3->alpn_selected);
1507 ssl->s3->alpn_selected = NULL;
1508}
1509
1510static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1511 if (ssl->alpn_client_proto_list == NULL ||
1512 ssl->s3->initial_handshake_complete) {
1513 return 1;
1514 }
1515
1516 CBB contents, proto_list;
1517 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1518 !CBB_add_u16_length_prefixed(out, &contents) ||
1519 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1520 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1521 ssl->alpn_client_proto_list_len) ||
1522 !CBB_flush(out)) {
1523 return 0;
1524 }
1525
1526 return 1;
1527}
1528
1529static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1530 CBS *contents) {
1531 if (contents == NULL) {
1532 return 1;
1533 }
1534
1535 assert(!ssl->s3->initial_handshake_complete);
1536 assert(ssl->alpn_client_proto_list != NULL);
1537
David Benjamin76c2efc2015-08-31 14:24:29 -04001538 if (ssl->s3->next_proto_neg_seen) {
1539 /* NPN and ALPN may not be negotiated in the same connection. */
1540 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1541 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1542 return 0;
1543 }
1544
Adam Langleyf18e4532015-07-10 13:39:53 -07001545 /* The extension data consists of a ProtocolNameList which must have
1546 * exactly one ProtocolName. Each of these is length-prefixed. */
1547 CBS protocol_name_list, protocol_name;
1548 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1549 CBS_len(contents) != 0 ||
1550 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1551 /* Empty protocol names are forbidden. */
1552 CBS_len(&protocol_name) == 0 ||
1553 CBS_len(&protocol_name_list) != 0) {
1554 return 0;
1555 }
1556
David Benjamin3e517572016-08-11 11:52:23 -04001557 /* Check that the protcol name is one of the ones we advertised. */
1558 int protocol_ok = 0;
1559 CBS client_protocol_name_list, client_protocol_name;
1560 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1561 ssl->alpn_client_proto_list_len);
1562 while (CBS_len(&client_protocol_name_list) > 0) {
1563 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1564 &client_protocol_name)) {
1565 *out_alert = SSL_AD_INTERNAL_ERROR;
1566 return 0;
1567 }
1568
1569 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1570 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1571 CBS_len(&protocol_name)) == 0) {
1572 protocol_ok = 1;
1573 break;
1574 }
1575 }
1576
1577 if (!protocol_ok) {
1578 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1579 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1580 return 0;
1581 }
1582
Adam Langleyf18e4532015-07-10 13:39:53 -07001583 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1584 &ssl->s3->alpn_selected_len)) {
1585 *out_alert = SSL_AD_INTERNAL_ERROR;
1586 return 0;
1587 }
1588
1589 return 1;
1590}
1591
1592static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1593 CBS *contents) {
1594 if (contents == NULL) {
1595 return 1;
1596 }
1597
1598 if (ssl->ctx->alpn_select_cb == NULL ||
1599 ssl->s3->initial_handshake_complete) {
1600 return 1;
1601 }
1602
1603 /* ALPN takes precedence over NPN. */
1604 ssl->s3->next_proto_neg_seen = 0;
1605
1606 CBS protocol_name_list;
1607 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1608 CBS_len(contents) != 0 ||
1609 CBS_len(&protocol_name_list) < 2) {
1610 return 0;
1611 }
1612
1613 /* Validate the protocol list. */
1614 CBS protocol_name_list_copy = protocol_name_list;
1615 while (CBS_len(&protocol_name_list_copy) > 0) {
1616 CBS protocol_name;
1617
1618 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1619 /* Empty protocol names are forbidden. */
1620 CBS_len(&protocol_name) == 0) {
1621 return 0;
1622 }
1623 }
1624
1625 const uint8_t *selected;
1626 uint8_t selected_len;
1627 if (ssl->ctx->alpn_select_cb(
1628 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1629 CBS_len(&protocol_name_list),
1630 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1631 OPENSSL_free(ssl->s3->alpn_selected);
1632 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1633 if (ssl->s3->alpn_selected == NULL) {
1634 *out_alert = SSL_AD_INTERNAL_ERROR;
1635 return 0;
1636 }
1637 ssl->s3->alpn_selected_len = selected_len;
1638 }
1639
1640 return 1;
1641}
1642
1643static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1644 if (ssl->s3->alpn_selected == NULL) {
1645 return 1;
1646 }
1647
1648 CBB contents, proto_list, proto;
1649 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1650 !CBB_add_u16_length_prefixed(out, &contents) ||
1651 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1652 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001653 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1654 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001655 !CBB_flush(out)) {
1656 return 0;
1657 }
1658
1659 return 1;
1660}
1661
1662
Adam Langley49c7af12015-07-10 14:33:46 -07001663/* Channel ID.
1664 *
1665 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1666
1667static void ext_channel_id_init(SSL *ssl) {
1668 ssl->s3->tlsext_channel_id_valid = 0;
1669}
1670
1671static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1672 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001673 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001674 return 1;
1675 }
1676
1677 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1678 !CBB_add_u16(out, 0 /* length */)) {
1679 return 0;
1680 }
1681
1682 return 1;
1683}
1684
1685static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1686 CBS *contents) {
1687 if (contents == NULL) {
1688 return 1;
1689 }
1690
Steven Valdez143e8b32016-07-11 13:19:03 -04001691 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1692 return 0;
1693 }
1694
David Benjamince079fd2016-08-02 16:22:34 -04001695 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001696 assert(ssl->tlsext_channel_id_enabled);
1697
1698 if (CBS_len(contents) != 0) {
1699 return 0;
1700 }
1701
1702 ssl->s3->tlsext_channel_id_valid = 1;
1703 return 1;
1704}
1705
1706static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1707 CBS *contents) {
1708 if (contents == NULL ||
1709 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001710 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001711 return 1;
1712 }
1713
1714 if (CBS_len(contents) != 0) {
1715 return 0;
1716 }
1717
1718 ssl->s3->tlsext_channel_id_valid = 1;
1719 return 1;
1720}
1721
1722static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001723 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1724 return 1;
1725 }
1726
Adam Langley49c7af12015-07-10 14:33:46 -07001727 if (!ssl->s3->tlsext_channel_id_valid) {
1728 return 1;
1729 }
1730
1731 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1732 !CBB_add_u16(out, 0 /* length */)) {
1733 return 0;
1734 }
1735
1736 return 1;
1737}
1738
Adam Langley391250d2015-07-15 19:06:07 -07001739
1740/* Secure Real-time Transport Protocol (SRTP) extension.
1741 *
1742 * https://tools.ietf.org/html/rfc5764 */
1743
Adam Langley391250d2015-07-15 19:06:07 -07001744
1745static void ext_srtp_init(SSL *ssl) {
1746 ssl->srtp_profile = NULL;
1747}
1748
1749static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1750 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1751 if (profiles == NULL) {
1752 return 1;
1753 }
1754 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1755 if (num_profiles == 0) {
1756 return 1;
1757 }
1758
1759 CBB contents, profile_ids;
1760 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1761 !CBB_add_u16_length_prefixed(out, &contents) ||
1762 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1763 return 0;
1764 }
1765
1766 size_t i;
1767 for (i = 0; i < num_profiles; i++) {
1768 if (!CBB_add_u16(&profile_ids,
1769 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1770 return 0;
1771 }
1772 }
1773
1774 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1775 !CBB_flush(out)) {
1776 return 0;
1777 }
1778
1779 return 1;
1780}
1781
1782static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1783 CBS *contents) {
1784 if (contents == NULL) {
1785 return 1;
1786 }
1787
1788 /* The extension consists of a u16-prefixed profile ID list containing a
1789 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1790 *
1791 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1792 CBS profile_ids, srtp_mki;
1793 uint16_t profile_id;
1794 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1795 !CBS_get_u16(&profile_ids, &profile_id) ||
1796 CBS_len(&profile_ids) != 0 ||
1797 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1798 CBS_len(contents) != 0) {
1799 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1800 return 0;
1801 }
1802
1803 if (CBS_len(&srtp_mki) != 0) {
1804 /* Must be no MKI, since we never offer one. */
1805 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1806 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1807 return 0;
1808 }
1809
1810 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1811
1812 /* Check to see if the server gave us something we support (and presumably
1813 * offered). */
1814 size_t i;
1815 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1816 const SRTP_PROTECTION_PROFILE *profile =
1817 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1818
1819 if (profile->id == profile_id) {
1820 ssl->srtp_profile = profile;
1821 return 1;
1822 }
1823 }
1824
1825 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1826 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1827 return 0;
1828}
1829
1830static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1831 CBS *contents) {
1832 if (contents == NULL) {
1833 return 1;
1834 }
1835
1836 CBS profile_ids, srtp_mki;
1837 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1838 CBS_len(&profile_ids) < 2 ||
1839 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1840 CBS_len(contents) != 0) {
1841 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1842 return 0;
1843 }
1844 /* Discard the MKI value for now. */
1845
1846 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1847 SSL_get_srtp_profiles(ssl);
1848
1849 /* Pick the server's most preferred profile. */
1850 size_t i;
1851 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1852 const SRTP_PROTECTION_PROFILE *server_profile =
1853 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1854
1855 CBS profile_ids_tmp;
1856 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1857
1858 while (CBS_len(&profile_ids_tmp) > 0) {
1859 uint16_t profile_id;
1860 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1861 return 0;
1862 }
1863
1864 if (server_profile->id == profile_id) {
1865 ssl->srtp_profile = server_profile;
1866 return 1;
1867 }
1868 }
1869 }
1870
1871 return 1;
1872}
1873
1874static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1875 if (ssl->srtp_profile == NULL) {
1876 return 1;
1877 }
1878
1879 CBB contents, profile_ids;
1880 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1881 !CBB_add_u16_length_prefixed(out, &contents) ||
1882 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1883 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1884 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1885 !CBB_flush(out)) {
1886 return 0;
1887 }
1888
1889 return 1;
1890}
1891
Adam Langleybdd5d662015-07-20 16:19:08 -07001892
1893/* EC point formats.
1894 *
1895 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1896
1897static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
David Benjamince079fd2016-08-02 16:22:34 -04001898 if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001899 return 0;
1900 }
1901
1902 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1903
1904 size_t i;
1905 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1906 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1907
1908 const uint32_t alg_k = cipher->algorithm_mkey;
1909 const uint32_t alg_a = cipher->algorithm_auth;
1910 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1911 return 1;
1912 }
1913 }
1914
1915 return 0;
1916}
1917
Adam Langleybdd5d662015-07-20 16:19:08 -07001918static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001919 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001920 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1921 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001922 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1923 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001924 !CBB_flush(out)) {
1925 return 0;
1926 }
1927
1928 return 1;
1929}
1930
1931static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1932 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1933 return 1;
1934 }
1935
1936 return ext_ec_point_add_extension(ssl, out);
1937}
1938
1939static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1940 CBS *contents) {
1941 if (contents == NULL) {
1942 return 1;
1943 }
1944
Steven Valdez143e8b32016-07-11 13:19:03 -04001945 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1946 return 0;
1947 }
1948
Adam Langleybdd5d662015-07-20 16:19:08 -07001949 CBS ec_point_format_list;
1950 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1951 CBS_len(contents) != 0) {
1952 return 0;
1953 }
1954
David Benjaminfc059942015-07-30 23:01:59 -04001955 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1956 * point format. */
1957 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1958 CBS_len(&ec_point_format_list)) == NULL) {
1959 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001960 return 0;
1961 }
1962
1963 return 1;
1964}
1965
1966static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1967 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001968 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1969 return 1;
1970 }
1971
Adam Langleybdd5d662015-07-20 16:19:08 -07001972 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1973}
1974
1975static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001976 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1977 return 1;
1978 }
1979
Adam Langleybdd5d662015-07-20 16:19:08 -07001980 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1981 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001982 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001983
1984 if (!using_ecc) {
1985 return 1;
1986 }
1987
1988 return ext_ec_point_add_extension(ssl, out);
1989}
1990
Adam Langley273d49c2015-07-20 16:38:52 -07001991
Steven Valdez143e8b32016-07-11 13:19:03 -04001992/* Draft Version Extension */
1993
1994static int ext_draft_version_add_clienthello(SSL *ssl, CBB *out) {
1995 uint16_t min_version, max_version;
1996 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
Steven Valdez3a287552016-07-19 05:10:50 -04001997 max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001998 return 1;
1999 }
2000
2001 CBB contents;
2002 if (!CBB_add_u16(out, TLSEXT_TYPE_draft_version) ||
2003 !CBB_add_u16_length_prefixed(out, &contents) ||
2004 !CBB_add_u16(&contents, TLS1_3_DRAFT_VERSION)) {
2005 return 0;
2006 }
2007
2008 return CBB_flush(out);
2009}
2010
2011
Steven Valdez4aa154e2016-07-29 14:32:55 -04002012/* Pre Shared Key
2013 *
2014 * https://tools.ietf.org/html/draft-ietf-tls-tls13-14 */
2015
2016static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
2017 uint16_t min_version, max_version;
2018 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2019 return 0;
2020 }
2021
2022 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
2023 ssl->method->version_from_wire(ssl->session->ssl_version) <
2024 TLS1_3_VERSION) {
2025 return 1;
2026 }
2027
2028 CBB contents, identities, identity;
2029 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2030 !CBB_add_u16_length_prefixed(out, &contents) ||
2031 !CBB_add_u16_length_prefixed(&contents, &identities) ||
2032 !CBB_add_u16_length_prefixed(&identities, &identity) ||
2033 !CBB_add_bytes(&identity, ssl->session->tlsext_tick,
2034 ssl->session->tlsext_ticklen)) {
2035 return 0;
2036 }
2037
2038 return CBB_flush(out);
2039}
2040
2041int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2042 CBS *contents) {
2043 uint16_t psk_id;
2044 if (!CBS_get_u16(contents, &psk_id) ||
2045 CBS_len(contents) != 0) {
2046 *out_alert = SSL_AD_DECODE_ERROR;
2047 return 0;
2048 }
2049
2050 if (psk_id != 0) {
2051 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2052 return 0;
2053 }
2054
2055 return 1;
2056}
2057
2058int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
2059 SSL_SESSION **out_session,
2060 uint8_t *out_alert,
2061 CBS *contents) {
2062 CBS identities, identity;
2063 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2064 !CBS_get_u16_length_prefixed(&identities, &identity) ||
2065 CBS_len(contents) != 0) {
2066 *out_alert = SSL_AD_DECODE_ERROR;
2067 return 0;
2068 }
2069
2070 /* TLS 1.3 session tickets are renewed separately as part of the
2071 * NewSessionTicket. */
2072 int renew;
2073 return tls_process_ticket(ssl, out_session, &renew, CBS_data(&identity),
2074 CBS_len(&identity), NULL, 0);
2075}
2076
2077int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
2078 if (!ssl->s3->session_reused) {
2079 return 1;
2080 }
2081
2082 CBB contents;
2083 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2084 !CBB_add_u16_length_prefixed(out, &contents) ||
2085 /* We only consider the first identity for resumption */
2086 !CBB_add_u16(&contents, 0) ||
2087 !CBB_flush(out)) {
2088 return 0;
2089 }
2090
2091 return 1;
2092}
2093
2094
Steven Valdez143e8b32016-07-11 13:19:03 -04002095/* Key Share
2096 *
2097 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
2098
2099static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
2100 uint16_t min_version, max_version;
2101 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2102 return 0;
2103 }
2104
2105 if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
2106 return 1;
2107 }
2108
2109 CBB contents, kse_bytes;
2110 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2111 !CBB_add_u16_length_prefixed(out, &contents) ||
2112 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2113 return 0;
2114 }
2115
2116 const uint16_t *groups;
2117 size_t groups_len;
Steven Valdez5440fe02016-07-18 12:40:30 -04002118 if (ssl->s3->hs->retry_group) {
2119 /* Append the new key share to the old list. */
2120 if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
2121 ssl->s3->hs->key_share_bytes_len)) {
2122 return 0;
2123 }
2124 OPENSSL_free(ssl->s3->hs->key_share_bytes);
2125 ssl->s3->hs->key_share_bytes = NULL;
2126
2127 groups = &ssl->s3->hs->retry_group;
2128 groups_len = 1;
2129 } else {
2130 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
2131 /* Only send the top two preferred key shares. */
2132 if (groups_len > 2) {
2133 groups_len = 2;
2134 }
2135 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002136
2137 ssl->s3->hs->groups = OPENSSL_malloc(groups_len * sizeof(SSL_ECDH_CTX));
2138 if (ssl->s3->hs->groups == NULL) {
2139 return 0;
2140 }
2141 memset(ssl->s3->hs->groups, 0, groups_len * sizeof(SSL_ECDH_CTX));
2142 ssl->s3->hs->groups_len = groups_len;
2143
2144 for (size_t i = 0; i < groups_len; i++) {
2145 if (!CBB_add_u16(&kse_bytes, groups[i])) {
2146 return 0;
2147 }
2148
2149 CBB key_exchange;
2150 if (!CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2151 !SSL_ECDH_CTX_init(&ssl->s3->hs->groups[i], groups[i]) ||
2152 !SSL_ECDH_CTX_offer(&ssl->s3->hs->groups[i], &key_exchange) ||
2153 !CBB_flush(&kse_bytes)) {
2154 return 0;
2155 }
2156 }
2157
Steven Valdez5440fe02016-07-18 12:40:30 -04002158 if (!ssl->s3->hs->retry_group) {
2159 /* Save the contents of the extension to repeat it in the second
2160 * ClientHello. */
2161 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2162 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2163 CBB_len(&kse_bytes));
2164 if (ssl->s3->hs->key_share_bytes == NULL) {
2165 return 0;
2166 }
2167 }
2168
Steven Valdez143e8b32016-07-11 13:19:03 -04002169 return CBB_flush(out);
2170}
2171
Steven Valdez7259f2f2016-08-02 16:55:05 -04002172int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2173 size_t *out_secret_len,
2174 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002175 CBS peer_key;
David Benjamin5c4e8572016-08-19 17:44:53 -04002176 uint16_t group_id;
2177 if (!CBS_get_u16(contents, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002178 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2179 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002180 *out_alert = SSL_AD_DECODE_ERROR;
2181 return 0;
2182 }
2183
2184 SSL_ECDH_CTX *group_ctx = NULL;
2185 for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) {
David Benjamin5c4e8572016-08-19 17:44:53 -04002186 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->groups[i]) == group_id) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002187 group_ctx = &ssl->s3->hs->groups[i];
2188 break;
2189 }
2190 }
2191
2192 if (group_ctx == NULL) {
2193 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2194 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2195 return 0;
2196 }
2197
2198 if (!SSL_ECDH_CTX_finish(group_ctx, out_secret, out_secret_len, out_alert,
2199 CBS_data(&peer_key), CBS_len(&peer_key))) {
2200 *out_alert = SSL_AD_INTERNAL_ERROR;
2201 return 0;
2202 }
2203
David Benjamin5c4e8572016-08-19 17:44:53 -04002204 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez5440fe02016-07-18 12:40:30 -04002205 ssl_handshake_clear_groups(ssl->s3->hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04002206 return 1;
2207}
2208
Steven Valdez7259f2f2016-08-02 16:55:05 -04002209int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2210 uint8_t **out_secret,
2211 size_t *out_secret_len,
2212 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002213 uint16_t group_id;
2214 CBS key_shares;
2215 if (!tls1_get_shared_group(ssl, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002216 !CBS_get_u16_length_prefixed(contents, &key_shares) ||
2217 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002218 return 0;
2219 }
2220
Steven Valdez5440fe02016-07-18 12:40:30 -04002221 *out_found = 0;
Steven Valdez143e8b32016-07-11 13:19:03 -04002222 while (CBS_len(&key_shares) > 0) {
2223 uint16_t id;
2224 CBS peer_key;
2225 if (!CBS_get_u16(&key_shares, &id) ||
2226 !CBS_get_u16_length_prefixed(&key_shares, &peer_key)) {
2227 return 0;
2228 }
2229
Steven Valdez5440fe02016-07-18 12:40:30 -04002230 if (id != group_id || *out_found) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002231 continue;
2232 }
2233
2234 SSL_ECDH_CTX group;
2235 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2236 CBB public_key;
2237 if (!CBB_init(&public_key, 0) ||
2238 !SSL_ECDH_CTX_init(&group, group_id) ||
2239 !SSL_ECDH_CTX_accept(&group, &public_key, out_secret, out_secret_len,
2240 out_alert, CBS_data(&peer_key),
2241 CBS_len(&peer_key)) ||
2242 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2243 &ssl->s3->hs->public_key_len)) {
2244 SSL_ECDH_CTX_cleanup(&group);
2245 CBB_cleanup(&public_key);
2246 return 0;
2247 }
2248 SSL_ECDH_CTX_cleanup(&group);
2249
Steven Valdez5440fe02016-07-18 12:40:30 -04002250 *out_found = 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002251 }
2252
Steven Valdez5440fe02016-07-18 12:40:30 -04002253 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002254}
2255
Steven Valdez7259f2f2016-08-02 16:55:05 -04002256int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002257 if (ssl->s3->tmp.new_cipher->algorithm_mkey != SSL_kECDHE) {
2258 return 1;
2259 }
2260
2261 uint16_t group_id;
2262 CBB kse_bytes, public_key;
2263 if (!tls1_get_shared_group(ssl, &group_id) ||
2264 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2265 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2266 !CBB_add_u16(&kse_bytes, group_id) ||
2267 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2268 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2269 ssl->s3->hs->public_key_len) ||
2270 !CBB_flush(out)) {
2271 return 0;
2272 }
2273
David Benjamin5c4e8572016-08-19 17:44:53 -04002274 ssl->s3->new_session->key_exchange_info = group_id;
Steven Valdez143e8b32016-07-11 13:19:03 -04002275 return 1;
2276}
2277
2278
Steven Valdezce902a92016-05-17 11:47:53 -04002279/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002280 *
Steven Valdezce902a92016-05-17 11:47:53 -04002281 * https://tools.ietf.org/html/rfc4492#section-5.1.2
2282 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07002283
Steven Valdezce902a92016-05-17 11:47:53 -04002284static void ext_supported_groups_init(SSL *ssl) {
2285 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2286 ssl->s3->tmp.peer_supported_group_list = NULL;
2287 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07002288}
2289
Steven Valdezce902a92016-05-17 11:47:53 -04002290static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002291 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2292 return 1;
2293 }
2294
Steven Valdezce902a92016-05-17 11:47:53 -04002295 CBB contents, groups_bytes;
2296 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002297 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002298 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002299 return 0;
2300 }
2301
Steven Valdezce902a92016-05-17 11:47:53 -04002302 const uint16_t *groups;
2303 size_t groups_len;
2304 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002305
2306 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002307 for (i = 0; i < groups_len; i++) {
2308 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002309 return 0;
2310 }
2311 }
2312
2313 return CBB_flush(out);
2314}
2315
Steven Valdezce902a92016-05-17 11:47:53 -04002316static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2317 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002318 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2319 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002320 return 1;
2321}
2322
Steven Valdezce902a92016-05-17 11:47:53 -04002323static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2324 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002325 if (contents == NULL) {
2326 return 1;
2327 }
2328
Steven Valdezce902a92016-05-17 11:47:53 -04002329 CBS supported_group_list;
2330 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2331 CBS_len(&supported_group_list) == 0 ||
2332 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002333 CBS_len(contents) != 0) {
2334 return 0;
2335 }
2336
Steven Valdezce902a92016-05-17 11:47:53 -04002337 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
2338 CBS_len(&supported_group_list));
2339 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002340 *out_alert = SSL_AD_INTERNAL_ERROR;
2341 return 0;
2342 }
2343
Steven Valdezce902a92016-05-17 11:47:53 -04002344 const size_t num_groups = CBS_len(&supported_group_list) / 2;
Adam Langley273d49c2015-07-20 16:38:52 -07002345 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002346 for (i = 0; i < num_groups; i++) {
2347 if (!CBS_get_u16(&supported_group_list,
2348 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002349 goto err;
2350 }
2351 }
2352
Steven Valdezce902a92016-05-17 11:47:53 -04002353 assert(CBS_len(&supported_group_list) == 0);
2354 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002355
2356 return 1;
2357
2358err:
Steven Valdezce902a92016-05-17 11:47:53 -04002359 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2360 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002361 *out_alert = SSL_AD_INTERNAL_ERROR;
2362 return 0;
2363}
2364
Steven Valdezce902a92016-05-17 11:47:53 -04002365static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002366 /* Servers don't echo this extension. */
2367 return 1;
2368}
2369
2370
Adam Langley614c66a2015-06-12 15:26:58 -07002371/* kExtensions contains all the supported extensions. */
2372static const struct tls_extension kExtensions[] = {
2373 {
Adam Langley5021b222015-06-12 18:27:58 -07002374 TLSEXT_TYPE_renegotiate,
2375 NULL,
2376 ext_ri_add_clienthello,
2377 ext_ri_parse_serverhello,
2378 ext_ri_parse_clienthello,
2379 ext_ri_add_serverhello,
2380 },
2381 {
Adam Langley614c66a2015-06-12 15:26:58 -07002382 TLSEXT_TYPE_server_name,
2383 ext_sni_init,
2384 ext_sni_add_clienthello,
2385 ext_sni_parse_serverhello,
2386 ext_sni_parse_clienthello,
2387 ext_sni_add_serverhello,
2388 },
Adam Langley0a056712015-07-01 15:03:33 -07002389 {
2390 TLSEXT_TYPE_extended_master_secret,
David Benjamin163c9562016-08-29 23:14:17 -04002391 NULL,
Adam Langley0a056712015-07-01 15:03:33 -07002392 ext_ems_add_clienthello,
2393 ext_ems_parse_serverhello,
2394 ext_ems_parse_clienthello,
2395 ext_ems_add_serverhello,
2396 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002397 {
2398 TLSEXT_TYPE_session_ticket,
2399 NULL,
2400 ext_ticket_add_clienthello,
2401 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002402 /* Ticket extension client parsing is handled in ssl_session.c */
2403 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002404 ext_ticket_add_serverhello,
2405 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002406 {
2407 TLSEXT_TYPE_signature_algorithms,
2408 NULL,
2409 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002410 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002411 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002412 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002413 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002414 {
2415 TLSEXT_TYPE_status_request,
2416 ext_ocsp_init,
2417 ext_ocsp_add_clienthello,
2418 ext_ocsp_parse_serverhello,
2419 ext_ocsp_parse_clienthello,
2420 ext_ocsp_add_serverhello,
2421 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002422 {
2423 TLSEXT_TYPE_next_proto_neg,
2424 ext_npn_init,
2425 ext_npn_add_clienthello,
2426 ext_npn_parse_serverhello,
2427 ext_npn_parse_clienthello,
2428 ext_npn_add_serverhello,
2429 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002430 {
2431 TLSEXT_TYPE_certificate_timestamp,
2432 NULL,
2433 ext_sct_add_clienthello,
2434 ext_sct_parse_serverhello,
2435 ext_sct_parse_clienthello,
2436 ext_sct_add_serverhello,
2437 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002438 {
2439 TLSEXT_TYPE_application_layer_protocol_negotiation,
2440 ext_alpn_init,
2441 ext_alpn_add_clienthello,
2442 ext_alpn_parse_serverhello,
2443 ext_alpn_parse_clienthello,
2444 ext_alpn_add_serverhello,
2445 },
Adam Langley49c7af12015-07-10 14:33:46 -07002446 {
2447 TLSEXT_TYPE_channel_id,
2448 ext_channel_id_init,
2449 ext_channel_id_add_clienthello,
2450 ext_channel_id_parse_serverhello,
2451 ext_channel_id_parse_clienthello,
2452 ext_channel_id_add_serverhello,
2453 },
Adam Langley391250d2015-07-15 19:06:07 -07002454 {
2455 TLSEXT_TYPE_srtp,
2456 ext_srtp_init,
2457 ext_srtp_add_clienthello,
2458 ext_srtp_parse_serverhello,
2459 ext_srtp_parse_clienthello,
2460 ext_srtp_add_serverhello,
2461 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002462 {
2463 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002464 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002465 ext_ec_point_add_clienthello,
2466 ext_ec_point_parse_serverhello,
2467 ext_ec_point_parse_clienthello,
2468 ext_ec_point_add_serverhello,
2469 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002470 {
2471 TLSEXT_TYPE_draft_version,
2472 NULL,
2473 ext_draft_version_add_clienthello,
2474 forbid_parse_serverhello,
2475 ignore_parse_clienthello,
2476 dont_add_serverhello,
2477 },
2478 {
2479 TLSEXT_TYPE_key_share,
2480 NULL,
2481 ext_key_share_add_clienthello,
2482 forbid_parse_serverhello,
2483 ignore_parse_clienthello,
2484 dont_add_serverhello,
2485 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002486 {
2487 TLSEXT_TYPE_pre_shared_key,
2488 NULL,
2489 ext_pre_shared_key_add_clienthello,
2490 forbid_parse_serverhello,
2491 ignore_parse_clienthello,
2492 dont_add_serverhello,
2493 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002494 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2495 * intolerant to the last extension being zero-length. See
2496 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002497 {
Steven Valdezce902a92016-05-17 11:47:53 -04002498 TLSEXT_TYPE_supported_groups,
2499 ext_supported_groups_init,
2500 ext_supported_groups_add_clienthello,
2501 ext_supported_groups_parse_serverhello,
2502 ext_supported_groups_parse_clienthello,
2503 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002504 },
Adam Langley614c66a2015-06-12 15:26:58 -07002505};
2506
2507#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2508
Adam Langley4cfa96b2015-07-01 11:56:55 -07002509OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2510 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002511 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002512OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2513 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2514 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002515 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002516
Adam Langley614c66a2015-06-12 15:26:58 -07002517static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2518 uint16_t value) {
2519 unsigned i;
2520 for (i = 0; i < kNumExtensions; i++) {
2521 if (kExtensions[i].value == value) {
2522 *out_index = i;
2523 return &kExtensions[i];
2524 }
2525 }
2526
2527 return NULL;
2528}
2529
Adam Langley09505632015-07-30 18:10:13 -07002530int SSL_extension_supported(unsigned extension_value) {
2531 uint32_t index;
2532 return extension_value == TLSEXT_TYPE_padding ||
2533 tls_extension_find(&index, extension_value) != NULL;
2534}
2535
David Benjamine8d53502015-10-10 14:13:23 -04002536int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002537 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002538 if (ssl->client_version == SSL3_VERSION &&
2539 !ssl->s3->send_connection_binding) {
2540 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002541 }
Adam Langley95c29f32014-06-20 12:00:00 -07002542
David Benjamine8d53502015-10-10 14:13:23 -04002543 CBB extensions;
2544 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002545 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002546 }
Adam Langley95c29f32014-06-20 12:00:00 -07002547
David Benjamine8d53502015-10-10 14:13:23 -04002548 ssl->s3->tmp.extensions.sent = 0;
2549 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002550
Adam Langley614c66a2015-06-12 15:26:58 -07002551 size_t i;
2552 for (i = 0; i < kNumExtensions; i++) {
2553 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002554 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002555 }
2556 }
Adam Langley95c29f32014-06-20 12:00:00 -07002557
Adam Langley614c66a2015-06-12 15:26:58 -07002558 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002559 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002560 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002561 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2562 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2563 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002564 }
Adam Langley95c29f32014-06-20 12:00:00 -07002565
Adam Langley33ad2b52015-07-20 17:43:53 -07002566 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002567 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002568 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002569 }
Adam Langley75712922014-10-10 16:23:43 -07002570
David Benjamine8d53502015-10-10 14:13:23 -04002571 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002572 goto err;
2573 }
2574
David Benjamince079fd2016-08-02 16:22:34 -04002575 if (!SSL_is_dtls(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002576 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002577 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002578 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002579 *
2580 * NB: because this code works out the length of all existing extensions
2581 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002582 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002583 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002584 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002585 * Server 7.0 is intolerant to the last extension being zero-length. See
2586 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002587 if (padding_len >= 4 + 1) {
2588 padding_len -= 4;
2589 } else {
2590 padding_len = 1;
2591 }
Adam Langley95c29f32014-06-20 12:00:00 -07002592
Adam Langley33ad2b52015-07-20 17:43:53 -07002593 uint8_t *padding_bytes;
2594 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2595 !CBB_add_u16(&extensions, padding_len) ||
2596 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2597 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002598 }
Adam Langley75712922014-10-10 16:23:43 -07002599
Adam Langley33ad2b52015-07-20 17:43:53 -07002600 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002601 }
2602 }
Adam Langley75712922014-10-10 16:23:43 -07002603
David Benjamina01deee2015-12-08 18:56:31 -05002604 /* Discard empty extensions blocks. */
2605 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002606 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002607 }
2608
David Benjamine8d53502015-10-10 14:13:23 -04002609 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002610
2611err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002612 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002613 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002614}
Adam Langley95c29f32014-06-20 12:00:00 -07002615
David Benjamin56380462015-10-10 14:59:09 -04002616int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002617 CBB extensions;
2618 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002619 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002620 }
2621
2622 unsigned i;
2623 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002624 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002625 /* Don't send extensions that were not received. */
2626 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002627 }
Adam Langley95c29f32014-06-20 12:00:00 -07002628
David Benjamin56380462015-10-10 14:59:09 -04002629 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002630 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2631 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2632 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002633 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002634 }
Adam Langley95c29f32014-06-20 12:00:00 -07002635
David Benjamin56380462015-10-10 14:59:09 -04002636 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002637 goto err;
2638 }
2639
Steven Valdez143e8b32016-07-11 13:19:03 -04002640 /* Discard empty extensions blocks before TLS 1.3. */
2641 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2642 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002643 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002644 }
2645
David Benjamin56380462015-10-10 14:59:09 -04002646 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002647
2648err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002649 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002650 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002651}
Adam Langley95c29f32014-06-20 12:00:00 -07002652
David Benjamine14ff062016-08-09 16:21:24 -04002653static int ssl_scan_clienthello_tlsext(
2654 SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
2655 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002656 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002657 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002658 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002659 }
2660 }
2661
David Benjamin0d56f882015-12-19 17:05:56 -05002662 ssl->s3->tmp.extensions.received = 0;
2663 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002664
David Benjamine14ff062016-08-09 16:21:24 -04002665 CBS extensions;
2666 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2667 while (CBS_len(&extensions) != 0) {
2668 uint16_t type;
2669 CBS extension;
2670
2671 /* Decode the next extension. */
2672 if (!CBS_get_u16(&extensions, &type) ||
2673 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002674 *out_alert = SSL_AD_DECODE_ERROR;
2675 return 0;
2676 }
Adam Langley95c29f32014-06-20 12:00:00 -07002677
David Benjamine14ff062016-08-09 16:21:24 -04002678 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2679 * ambiguous. Ignore all but the renegotiation_info extension. */
2680 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2681 continue;
2682 }
Adam Langley95c29f32014-06-20 12:00:00 -07002683
David Benjamine14ff062016-08-09 16:21:24 -04002684 unsigned ext_index;
2685 const struct tls_extension *const ext =
2686 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002687
David Benjamine14ff062016-08-09 16:21:24 -04002688 if (ext == NULL) {
2689 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002690 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002691 return 0;
2692 }
David Benjamine14ff062016-08-09 16:21:24 -04002693 continue;
2694 }
2695
2696 ssl->s3->tmp.extensions.received |= (1u << ext_index);
2697 uint8_t alert = SSL_AD_DECODE_ERROR;
2698 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2699 *out_alert = alert;
2700 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2701 ERR_add_error_dataf("extension: %u", (unsigned)type);
2702 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002703 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002704 }
Adam Langley75712922014-10-10 16:23:43 -07002705
David Benjamin1deb41b2016-08-09 19:36:38 -04002706 for (size_t i = 0; i < kNumExtensions; i++) {
2707 if (ssl->s3->tmp.extensions.received & (1u << i)) {
2708 continue;
2709 }
2710
2711 CBS *contents = NULL, fake_contents;
2712 static const uint8_t kFakeRenegotiateExtension[] = {0};
2713 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2714 ssl_client_cipher_list_contains_cipher(client_hello,
2715 SSL3_CK_SCSV & 0xffff)) {
2716 /* The renegotiation SCSV was received so pretend that we received a
2717 * renegotiation extension. */
2718 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2719 sizeof(kFakeRenegotiateExtension));
2720 contents = &fake_contents;
2721 ssl->s3->tmp.extensions.received |= (1u << i);
2722 }
2723
2724 /* Extension wasn't observed so call the callback with a NULL
2725 * parameter. */
2726 uint8_t alert = SSL_AD_DECODE_ERROR;
2727 if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
2728 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2729 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2730 *out_alert = alert;
2731 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002732 }
2733 }
2734
Adam Langleyfcf25832014-12-18 17:42:32 -08002735 return 1;
2736}
Adam Langley95c29f32014-06-20 12:00:00 -07002737
David Benjamine14ff062016-08-09 16:21:24 -04002738int ssl_parse_clienthello_tlsext(
2739 SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002740 int alert = -1;
David Benjamine14ff062016-08-09 16:21:24 -04002741 if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002742 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002743 return 0;
2744 }
Adam Langley95c29f32014-06-20 12:00:00 -07002745
David Benjamin0d56f882015-12-19 17:05:56 -05002746 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002747 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002748 return 0;
2749 }
Adam Langley95c29f32014-06-20 12:00:00 -07002750
Adam Langleyfcf25832014-12-18 17:42:32 -08002751 return 1;
2752}
Adam Langley95c29f32014-06-20 12:00:00 -07002753
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002754OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2755
David Benjamin0d56f882015-12-19 17:05:56 -05002756static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002757 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2758 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2759 return 1;
2760 }
Adam Langley614c66a2015-06-12 15:26:58 -07002761
Steven Valdez143e8b32016-07-11 13:19:03 -04002762 /* Decode the extensions block and check it is valid. */
2763 CBS extensions;
2764 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2765 !tls1_check_duplicate_extensions(&extensions)) {
2766 *out_alert = SSL_AD_DECODE_ERROR;
2767 return 0;
2768 }
2769
2770 uint32_t received = 0;
2771 while (CBS_len(&extensions) != 0) {
2772 uint16_t type;
2773 CBS extension;
2774
2775 /* Decode the next extension. */
2776 if (!CBS_get_u16(&extensions, &type) ||
2777 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002778 *out_alert = SSL_AD_DECODE_ERROR;
2779 return 0;
2780 }
Adam Langley95c29f32014-06-20 12:00:00 -07002781
Steven Valdez143e8b32016-07-11 13:19:03 -04002782 unsigned ext_index;
2783 const struct tls_extension *const ext =
2784 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002785
Steven Valdez143e8b32016-07-11 13:19:03 -04002786 if (ext == NULL) {
2787 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002788 return 0;
2789 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002790 continue;
2791 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002792
David Benjamin1deb41b2016-08-09 19:36:38 -04002793 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index)) &&
2794 type != TLSEXT_TYPE_renegotiate) {
2795 /* If the extension was never sent then it is illegal, except for the
2796 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002797 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2798 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002799 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002800 return 0;
2801 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002802
Steven Valdez143e8b32016-07-11 13:19:03 -04002803 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002804
Steven Valdez143e8b32016-07-11 13:19:03 -04002805 uint8_t alert = SSL_AD_DECODE_ERROR;
2806 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2807 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2808 ERR_add_error_dataf("extension: %u", (unsigned)type);
2809 *out_alert = alert;
2810 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002811 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002812 }
Adam Langley95c29f32014-06-20 12:00:00 -07002813
Adam Langley33ad2b52015-07-20 17:43:53 -07002814 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002815 for (i = 0; i < kNumExtensions; i++) {
2816 if (!(received & (1u << i))) {
2817 /* Extension wasn't observed so call the callback with a NULL
2818 * parameter. */
2819 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002820 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002821 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2822 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002823 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002824 return 0;
2825 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002826 }
2827 }
Adam Langley95c29f32014-06-20 12:00:00 -07002828
Adam Langleyfcf25832014-12-18 17:42:32 -08002829 return 1;
2830}
Adam Langley95c29f32014-06-20 12:00:00 -07002831
David Benjamin0d56f882015-12-19 17:05:56 -05002832static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002833 int ret = SSL_TLSEXT_ERR_NOACK;
2834 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002835
David Benjamin78f8aab2016-03-10 16:33:58 -05002836 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002837 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002838 ssl->ctx->tlsext_servername_arg);
2839 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002840 ret = ssl->initial_ctx->tlsext_servername_callback(
2841 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002842 }
Adam Langley95c29f32014-06-20 12:00:00 -07002843
Adam Langleyfcf25832014-12-18 17:42:32 -08002844 switch (ret) {
2845 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002846 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002847 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002848
Adam Langleyfcf25832014-12-18 17:42:32 -08002849 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002850 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002851 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002852
Adam Langleyfcf25832014-12-18 17:42:32 -08002853 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002854 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002855 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002856
Adam Langleyfcf25832014-12-18 17:42:32 -08002857 default:
2858 return 1;
2859 }
2860}
Adam Langleyed8270a2014-09-02 13:52:56 -07002861
David Benjamin0d56f882015-12-19 17:05:56 -05002862static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002863 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002864 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002865
David Benjamin78f8aab2016-03-10 16:33:58 -05002866 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002867 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002868 ssl->ctx->tlsext_servername_arg);
2869 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002870 ret = ssl->initial_ctx->tlsext_servername_callback(
2871 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002872 }
Adam Langley95c29f32014-06-20 12:00:00 -07002873
Adam Langleyfcf25832014-12-18 17:42:32 -08002874 switch (ret) {
2875 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002876 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002877 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002878
Adam Langleyfcf25832014-12-18 17:42:32 -08002879 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002880 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002881 return 1;
2882
2883 default:
2884 return 1;
2885 }
2886}
2887
David Benjamin0d56f882015-12-19 17:05:56 -05002888int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002889 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002890 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2891 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002892 return 0;
2893 }
2894
David Benjamin0d56f882015-12-19 17:05:56 -05002895 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002896 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002897 return 0;
2898 }
2899
2900 return 1;
2901}
Adam Langley95c29f32014-06-20 12:00:00 -07002902
David Benjamine3aa1d92015-06-16 15:34:50 -04002903int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002904 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002905 size_t ticket_len, const uint8_t *session_id,
2906 size_t session_id_len) {
2907 int ret = 1; /* Most errors are non-fatal. */
2908 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2909 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002910
David Benjamine3aa1d92015-06-16 15:34:50 -04002911 HMAC_CTX hmac_ctx;
2912 HMAC_CTX_init(&hmac_ctx);
2913 EVP_CIPHER_CTX cipher_ctx;
2914 EVP_CIPHER_CTX_init(&cipher_ctx);
2915
David Benjaminef1b0092015-11-21 14:05:44 -05002916 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002917 *out_session = NULL;
2918
Steven Valdez4aa154e2016-07-29 14:32:55 -04002919 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
2920 goto done;
2921 }
2922
David Benjamine3aa1d92015-06-16 15:34:50 -04002923 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2924 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002925 }
2926
David Benjaminadcc3952015-04-26 13:07:57 -04002927 /* Ensure there is room for the key name and the largest IV
2928 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2929 * the maximum IV length should be well under the minimum size for the
2930 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002931 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2932 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002933 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002934 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002935
David Benjamine3aa1d92015-06-16 15:34:50 -04002936 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002937 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2938 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2939 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002940 if (cb_ret < 0) {
2941 ret = 0;
2942 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002943 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002944 if (cb_ret == 0) {
2945 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002946 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002947 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002948 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002949 }
2950 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002951 /* Check the key name matches. */
2952 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2953 SSL_TICKET_KEY_NAME_LEN) != 0) {
2954 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002955 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002956 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2957 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002958 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002959 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2960 ssl_ctx->tlsext_tick_aes_key, iv)) {
2961 ret = 0;
2962 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002963 }
2964 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002965 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002966
David Benjamine3aa1d92015-06-16 15:34:50 -04002967 /* Check the MAC at the end of the ticket. */
2968 uint8_t mac[EVP_MAX_MD_SIZE];
2969 size_t mac_len = HMAC_size(&hmac_ctx);
2970 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002971 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002972 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002973 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002974 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2975 HMAC_Final(&hmac_ctx, mac, NULL);
2976 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2977 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002978 }
2979
David Benjamine3aa1d92015-06-16 15:34:50 -04002980 /* Decrypt the session data. */
2981 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2982 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2983 mac_len;
2984 plaintext = OPENSSL_malloc(ciphertext_len);
2985 if (plaintext == NULL) {
2986 ret = 0;
2987 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002988 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002989 if (ciphertext_len >= INT_MAX) {
2990 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002991 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002992 int len1, len2;
2993 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2994 (int)ciphertext_len) ||
2995 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2996 ERR_clear_error(); /* Don't leave an error on the queue. */
2997 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002998 }
2999
David Benjamine3aa1d92015-06-16 15:34:50 -04003000 /* Decode the session. */
3001 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
3002 if (session == NULL) {
3003 ERR_clear_error(); /* Don't leave an error on the queue. */
3004 goto done;
3005 }
3006
3007 /* Copy the client's session ID into the new session, to denote the ticket has
3008 * been accepted. */
3009 memcpy(session->session_id, session_id, session_id_len);
3010 session->session_id_length = session_id_len;
3011
Steven Valdez4aa154e2016-07-29 14:32:55 -04003012 if (!ssl_session_is_context_valid(ssl, session) ||
3013 !ssl_session_is_time_valid(ssl, session)) {
3014 SSL_SESSION_free(session);
3015 session = NULL;
3016 }
3017
David Benjamine3aa1d92015-06-16 15:34:50 -04003018 *out_session = session;
3019
3020done:
3021 OPENSSL_free(plaintext);
3022 HMAC_CTX_cleanup(&hmac_ctx);
3023 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
3024 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003025}
Adam Langley95c29f32014-06-20 12:00:00 -07003026
Steven Valdez0d62f262015-09-04 12:41:04 -04003027int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003028 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05003029 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003030 return 1;
3031 }
David Benjamincd996942014-07-20 16:23:51 -04003032
David Benjamin0fc37ef2016-08-17 15:29:46 -04003033 SSL_HANDSHAKE *hs = ssl->s3->hs;
3034 OPENSSL_free(hs->peer_sigalgs);
3035 hs->peer_sigalgs = NULL;
3036 hs->num_peer_sigalgs = 0;
Steven Valdez0d62f262015-09-04 12:41:04 -04003037
3038 size_t num_sigalgs = CBS_len(in_sigalgs);
Steven Valdez0d62f262015-09-04 12:41:04 -04003039 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003040 return 0;
3041 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003042 num_sigalgs /= 2;
3043
3044 /* supported_signature_algorithms in the certificate request is
3045 * allowed to be empty. */
3046 if (num_sigalgs == 0) {
3047 return 1;
3048 }
3049
Steven Valdez02563852016-06-23 13:33:05 -04003050 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3051 * and we just divided |num_sigalgs| by two. */
David Benjamin0fc37ef2016-08-17 15:29:46 -04003052 hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3053 if (hs->peer_sigalgs == NULL) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003054 return 0;
3055 }
David Benjamin0fc37ef2016-08-17 15:29:46 -04003056 hs->num_peer_sigalgs = num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003057
3058 CBS sigalgs;
3059 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
David Benjamin0fc37ef2016-08-17 15:29:46 -04003060 for (size_t i = 0; i < num_sigalgs; i++) {
3061 if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003062 return 0;
3063 }
3064 }
Adam Langley95c29f32014-06-20 12:00:00 -07003065
Adam Langleyfcf25832014-12-18 17:42:32 -08003066 return 1;
3067}
David Benjaminec2f27d2014-11-13 19:17:25 -05003068
David Benjaminea9a0d52016-07-08 15:52:59 -07003069int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003070 CERT *cert = ssl->cert;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003071 SSL_HANDSHAKE *hs = ssl->s3->hs;
David Benjaminec2f27d2014-11-13 19:17:25 -05003072
Steven Valdezf0451ca2016-06-29 13:16:27 -04003073 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3074 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3075 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003076 int type = ssl_private_key_type(ssl);
3077 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003078 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003079 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003080 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003081 if (ssl_is_ecdsa_key_type(type)) {
3082 *out = SSL_SIGN_ECDSA_SHA1;
3083 return 1;
3084 }
3085 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3086 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003087 }
3088
Steven Valdezeff1e8d2016-07-06 14:24:47 -04003089 const uint16_t *sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003090 size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07003091 if (cert->sigalgs != NULL) {
3092 sigalgs = cert->sigalgs;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003093 num_sigalgs = cert->num_sigalgs;
Steven Valdez0d62f262015-09-04 12:41:04 -04003094 }
3095
David Benjamin0fc37ef2016-08-17 15:29:46 -04003096 const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3097 size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3098 if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003099 /* If the client didn't specify any signature_algorithms extension then
3100 * we can assume that it supports SHA1. See
3101 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3102 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3103 SSL_SIGN_ECDSA_SHA1};
3104 peer_sigalgs = kDefaultPeerAlgorithms;
David Benjamin0fc37ef2016-08-17 15:29:46 -04003105 num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
David Benjaminea9a0d52016-07-08 15:52:59 -07003106 }
3107
David Benjamin0fc37ef2016-08-17 15:29:46 -04003108 for (size_t i = 0; i < num_sigalgs; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003109 uint16_t sigalg = sigalgs[i];
3110 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3111 * negotiated. */
3112 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3113 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3114 continue;
3115 }
3116
David Benjamin0fc37ef2016-08-17 15:29:46 -04003117 for (size_t j = 0; j < num_peer_sigalgs; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003118 if (sigalg == peer_sigalgs[j]) {
3119 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003120 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003121 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003122 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003123 }
Adam Langley95c29f32014-06-20 12:00:00 -07003124
David Benjaminea9a0d52016-07-08 15:52:59 -07003125 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3126 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003127}
Adam Langley95c29f32014-06-20 12:00:00 -07003128
David Benjamind6a4ae92015-08-06 11:10:51 -04003129int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
3130 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003131 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003132
3133 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003134 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
3135 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08003136 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003137
David Benjamind6a4ae92015-08-06 11:10:51 -04003138 static const char kClientIDMagic[] = "TLS Channel ID signature";
3139 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
3140
Steven Valdez87eab492016-06-27 16:34:59 -04003141 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003142 static const char kResumptionMagic[] = "Resumption";
3143 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
3144 if (ssl->session->original_handshake_hash_len == 0) {
3145 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3146 goto err;
3147 }
3148 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
3149 ssl->session->original_handshake_hash_len);
3150 }
3151
3152 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3153 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3154 sizeof(handshake_hash));
3155 if (handshake_hash_len < 0) {
3156 goto err;
3157 }
3158 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
3159 unsigned len_u;
3160 EVP_DigestFinal_ex(&ctx, out, &len_u);
3161 *out_len = len_u;
3162
3163 ret = 1;
3164
3165err:
3166 EVP_MD_CTX_cleanup(&ctx);
3167 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003168}
Adam Langley1258b6a2014-06-20 12:00:00 -07003169
3170/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003171 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003172 * data. */
3173int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003174 int digest_len;
3175 /* This function should never be called for a resumed session because the
3176 * handshake hashes that we wish to record are for the original, full
3177 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003178 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003179 return -1;
3180 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003181
Adam Langleyfcf25832014-12-18 17:42:32 -08003182 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003183 tls1_handshake_digest(
3184 ssl, ssl->s3->new_session->original_handshake_hash,
3185 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003186 if (digest_len < 0) {
3187 return -1;
3188 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003189
Steven Valdez87eab492016-06-27 16:34:59 -04003190 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003191
Adam Langleyfcf25832014-12-18 17:42:32 -08003192 return 1;
3193}