blob: b1e3b13c9562788b3ecd2de6fb57ab4dd4fa0325 [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"
Adam Langleyfcf25832014-12-18 17:42:32 -0800127
128
David Benjamin0d56f882015-12-19 17:05:56 -0500129static int ssl_check_clienthello_tlsext(SSL *ssl);
130static int ssl_check_serverhello_tlsext(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700131
Adam Langleyfcf25832014-12-18 17:42:32 -0800132static int compare_uint16_t(const void *p1, const void *p2) {
133 uint16_t u1 = *((const uint16_t *)p1);
134 uint16_t u2 = *((const uint16_t *)p2);
135 if (u1 < u2) {
136 return -1;
137 } else if (u1 > u2) {
138 return 1;
139 } else {
140 return 0;
141 }
142}
David Benjamin35a7a442014-07-05 00:23:20 -0400143
Adam Langleyfcf25832014-12-18 17:42:32 -0800144/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
145 * more than one extension of the same type in a ClientHello or ServerHello.
146 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400147 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800148static int tls1_check_duplicate_extensions(const CBS *cbs) {
149 CBS extensions = *cbs;
150 size_t num_extensions = 0, i = 0;
151 uint16_t *extension_types = NULL;
152 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400153
Adam Langleyfcf25832014-12-18 17:42:32 -0800154 /* First pass: count the extensions. */
155 while (CBS_len(&extensions) > 0) {
156 uint16_t type;
157 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400158
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 if (!CBS_get_u16(&extensions, &type) ||
160 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
161 goto done;
162 }
David Benjamin35a7a442014-07-05 00:23:20 -0400163
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 num_extensions++;
165 }
David Benjamin35a7a442014-07-05 00:23:20 -0400166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 if (num_extensions == 0) {
168 return 1;
169 }
David Benjamin9a373592014-07-25 04:27:53 -0400170
Brian Smith5ba06892016-02-07 09:36:04 -1000171 extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800172 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400173 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800174 goto done;
175 }
David Benjamin35a7a442014-07-05 00:23:20 -0400176
Adam Langleyfcf25832014-12-18 17:42:32 -0800177 /* Second pass: gather the extension types. */
178 extensions = *cbs;
179 for (i = 0; i < num_extensions; i++) {
180 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400181
Adam Langleyfcf25832014-12-18 17:42:32 -0800182 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
183 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
184 /* This should not happen. */
185 goto done;
186 }
187 }
188 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400189
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 /* Sort the extensions and make sure there are no duplicates. */
191 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
192 for (i = 1; i < num_extensions; i++) {
193 if (extension_types[i - 1] == extension_types[i]) {
194 goto done;
195 }
196 }
David Benjamin35a7a442014-07-05 00:23:20 -0400197
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 ret = 1;
199
David Benjamin35a7a442014-07-05 00:23:20 -0400200done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400201 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 return ret;
203}
David Benjamin35a7a442014-07-05 00:23:20 -0400204
David Benjamind7573dc2016-07-20 19:05:22 +0200205int ssl_early_callback_init(SSL *ssl, struct ssl_early_callback_ctx *ctx,
206 const uint8_t *in, size_t in_len) {
207 memset(ctx, 0, sizeof(*ctx));
208 ctx->ssl = ssl;
209 ctx->client_hello = in;
210 ctx->client_hello_len = in_len;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400211
David Benjamind7573dc2016-07-20 19:05:22 +0200212 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800213 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700214
Adam Langleyfcf25832014-12-18 17:42:32 -0800215 if (/* Skip client version. */
216 !CBS_skip(&client_hello, 2) ||
217 /* Skip client nonce. */
218 !CBS_skip(&client_hello, 32) ||
219 /* Extract session_id. */
220 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
221 return 0;
222 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700223
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 */
228 if (SSL_IS_DTLS(ctx->ssl)) {
229 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700230
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
232 return 0;
233 }
234 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700235
Adam Langleyfcf25832014-12-18 17:42:32 -0800236 /* Extract cipher_suites. */
237 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
238 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
239 return 0;
240 }
241 ctx->cipher_suites = CBS_data(&cipher_suites);
242 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700243
Adam Langleyfcf25832014-12-18 17:42:32 -0800244 /* Extract compression_methods. */
245 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
246 CBS_len(&compression_methods) < 1) {
247 return 0;
248 }
249 ctx->compression_methods = CBS_data(&compression_methods);
250 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700251
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 /* If the ClientHello ends here then it's valid, but doesn't have any
253 * extensions. (E.g. SSLv3.) */
254 if (CBS_len(&client_hello) == 0) {
255 ctx->extensions = NULL;
256 ctx->extensions_len = 0;
257 return 1;
258 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700259
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 /* Extract extensions and check it is valid. */
261 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
262 !tls1_check_duplicate_extensions(&extensions) ||
263 CBS_len(&client_hello) != 0) {
264 return 0;
265 }
266 ctx->extensions = CBS_data(&extensions);
267 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700268
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 return 1;
270}
Adam Langleydc9b1412014-06-20 12:00:00 -0700271
David Benjamind4c2bce2015-10-17 12:28:18 -0400272int SSL_early_callback_ctx_extension_get(
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
274 const uint8_t **out_data, size_t *out_len) {
275 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700276
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700278
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 while (CBS_len(&extensions) != 0) {
280 uint16_t type;
281 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400282
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 /* Decode the next extension. */
284 if (!CBS_get_u16(&extensions, &type) ||
285 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
286 return 0;
287 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700288
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 if (type == extension_type) {
290 *out_data = CBS_data(&extension);
291 *out_len = CBS_len(&extension);
292 return 1;
293 }
294 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700295
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 return 0;
297}
Adam Langley95c29f32014-06-20 12:00:00 -0700298
Steven Valdezce902a92016-05-17 11:47:53 -0400299static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400300 SSL_CURVE_X25519,
301 SSL_CURVE_SECP256R1,
302 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700303#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin9e68f192016-06-30 14:55:33 -0400304 SSL_CURVE_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700305#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800306};
Adam Langley95c29f32014-06-20 12:00:00 -0700307
Steven Valdez5440fe02016-07-18 12:40:30 -0400308void tls1_get_grouplist(SSL *ssl, int get_peer_groups,
309 const uint16_t **out_group_ids,
310 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400311 if (get_peer_groups) {
312 /* Only clients send a supported group list, so this function is only
313 * called on the server. */
David Benjamin0d56f882015-12-19 17:05:56 -0500314 assert(ssl->server);
Steven Valdezce902a92016-05-17 11:47:53 -0400315 *out_group_ids = ssl->s3->tmp.peer_supported_group_list;
316 *out_group_ids_len = ssl->s3->tmp.peer_supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800317 return;
318 }
Adam Langley95c29f32014-06-20 12:00:00 -0700319
Steven Valdezce902a92016-05-17 11:47:53 -0400320 *out_group_ids = ssl->supported_group_list;
321 *out_group_ids_len = ssl->supported_group_list_len;
322 if (!*out_group_ids) {
323 *out_group_ids = kDefaultGroups;
324 *out_group_ids_len = sizeof(kDefaultGroups) / sizeof(kDefaultGroups[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 }
326}
David Benjamined439582014-07-14 19:13:02 -0400327
Steven Valdezce902a92016-05-17 11:47:53 -0400328int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) {
329 const uint16_t *groups, *peer_groups, *pref, *supp;
330 size_t groups_len, peer_groups_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400331
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 /* Can't do anything on client side */
David Benjamin4298d772015-12-19 00:18:25 -0500333 if (ssl->server == 0) {
334 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 }
336
Steven Valdezce902a92016-05-17 11:47:53 -0400337 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
338 tls1_get_grouplist(ssl, 1 /* peer groups */, &peer_groups, &peer_groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800339
Steven Valdezce902a92016-05-17 11:47:53 -0400340 if (peer_groups_len == 0) {
341 /* Clients are not required to send a supported_groups extension. In this
342 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin4298d772015-12-19 00:18:25 -0500343 * section 4, paragraph 3.
344 *
345 * However, in the interests of compatibility, we will skip ECDH if the
346 * client didn't send an extension because we can't be sure that they'll
Steven Valdezce902a92016-05-17 11:47:53 -0400347 * support our favoured group. */
David Benjamin4298d772015-12-19 00:18:25 -0500348 return 0;
David Benjamin55a43642015-04-20 14:45:55 -0400349 }
350
David Benjamin4298d772015-12-19 00:18:25 -0500351 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400352 pref = groups;
353 pref_len = groups_len;
354 supp = peer_groups;
355 supp_len = peer_groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400356 } else {
Steven Valdezce902a92016-05-17 11:47:53 -0400357 pref = peer_groups;
358 pref_len = peer_groups_len;
359 supp = groups;
360 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400361 }
362
363 for (i = 0; i < pref_len; i++) {
364 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400366 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500367 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 }
369 }
370 }
371
David Benjamin4298d772015-12-19 00:18:25 -0500372 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800373}
Adam Langley95c29f32014-06-20 12:00:00 -0700374
Steven Valdezce902a92016-05-17 11:47:53 -0400375int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400377 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 size_t i;
379
Steven Valdezce902a92016-05-17 11:47:53 -0400380 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
381 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800382 return 0;
383 }
384
385 for (i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400386 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
387 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 return 0;
389 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 }
391
Steven Valdezce902a92016-05-17 11:47:53 -0400392 OPENSSL_free(*out_group_ids);
393 *out_group_ids = group_ids;
394 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800395
396 return 1;
397}
Adam Langley95c29f32014-06-20 12:00:00 -0700398
Steven Valdezce902a92016-05-17 11:47:53 -0400399/* tls1_curve_params_from_ec_key sets |*out_group_id| and |*out_comp_id| to the
400 * TLS group ID and point format, respectively, for |ec|. It returns one on
David Benjamin072334d2014-07-13 16:24:27 -0400401 * success and zero on failure. */
Steven Valdezce902a92016-05-17 11:47:53 -0400402static int tls1_curve_params_from_ec_key(uint16_t *out_group_id,
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 uint8_t *out_comp_id, EC_KEY *ec) {
404 int nid;
405 uint16_t id;
406 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 if (ec == NULL) {
409 return 0;
410 }
Adam Langley95c29f32014-06-20 12:00:00 -0700411
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 grp = EC_KEY_get0_group(ec);
413 if (grp == NULL) {
414 return 0;
415 }
David Benjamin072334d2014-07-13 16:24:27 -0400416
Steven Valdezce902a92016-05-17 11:47:53 -0400417 /* Determine group ID */
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 nid = EC_GROUP_get_curve_name(grp);
Steven Valdezce902a92016-05-17 11:47:53 -0400419 if (!ssl_nid_to_group_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 return 0;
421 }
David Benjamin072334d2014-07-13 16:24:27 -0400422
Steven Valdezce902a92016-05-17 11:47:53 -0400423 /* Set the named group ID. Arbitrary explicit groups are not supported. */
424 *out_group_id = id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800425
426 if (out_comp_id) {
427 if (EC_KEY_get0_public_key(ec) == NULL) {
428 return 0;
429 }
430 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
431 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
432 } else {
433 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
434 }
435 }
436
437 return 1;
438}
David Benjamin072334d2014-07-13 16:24:27 -0400439
Steven Valdezce902a92016-05-17 11:47:53 -0400440/* tls1_check_group_id returns one if |group_id| is consistent with both our
441 * and the peer's group preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400442 * preferences are checked; the peer (the server) does not send preferences. */
Steven Valdezce902a92016-05-17 11:47:53 -0400443int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
444 const uint16_t *groups;
445 size_t groups_len, i, get_peer_groups;
David Benjamin42e9a772014-09-02 23:18:44 -0400446
Adam Langleyfcf25832014-12-18 17:42:32 -0800447 /* Check against our list, then the peer's list. */
Steven Valdezce902a92016-05-17 11:47:53 -0400448 for (get_peer_groups = 0; get_peer_groups <= 1; get_peer_groups++) {
449 if (get_peer_groups && !ssl->server) {
David Benjamin55a43642015-04-20 14:45:55 -0400450 /* Servers do not present a preference list so, if we are a client, only
451 * check our list. */
452 continue;
453 }
454
Steven Valdezce902a92016-05-17 11:47:53 -0400455 tls1_get_grouplist(ssl, get_peer_groups, &groups, &groups_len);
456 if (get_peer_groups && groups_len == 0) {
457 /* Clients are not required to send a supported_groups extension. In this
458 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin55a43642015-04-20 14:45:55 -0400459 * section 4, paragraph 3. */
460 continue;
461 }
Steven Valdezce902a92016-05-17 11:47:53 -0400462 for (i = 0; i < groups_len; i++) {
463 if (groups[i] == group_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 break;
465 }
466 }
Adam Langley95c29f32014-06-20 12:00:00 -0700467
Steven Valdezce902a92016-05-17 11:47:53 -0400468 if (i == groups_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 return 0;
470 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 }
David Benjamin033e5f42014-11-13 18:47:41 -0500472
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 return 1;
474}
David Benjamin033e5f42014-11-13 18:47:41 -0500475
David Benjamin0d56f882015-12-19 17:05:56 -0500476int tls1_check_ec_cert(SSL *ssl, X509 *x) {
David Benjamin75ea5bb2016-07-08 17:43:29 -0700477 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
478 /* In TLS 1.3, the ECDSA curve is negotiated via signature algorithms. */
479 return 1;
480 }
481
Adam Langleyfcf25832014-12-18 17:42:32 -0800482 EVP_PKEY *pkey = X509_get_pubkey(x);
David Benjamin75ea5bb2016-07-08 17:43:29 -0700483 if (pkey == NULL) {
484 return 0;
485 }
486
487 int ret = 0;
Steven Valdezce902a92016-05-17 11:47:53 -0400488 uint16_t group_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 uint8_t comp_id;
David Benjamin758d1272015-11-20 17:47:25 -0500490 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
491 if (ec_key == NULL ||
Steven Valdezce902a92016-05-17 11:47:53 -0400492 !tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) ||
493 !tls1_check_group_id(ssl, group_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400494 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 goto done;
496 }
497
498 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500499
500done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400501 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800502 return ret;
503}
David Benjamin42e9a772014-09-02 23:18:44 -0400504
Adam Langley95c29f32014-06-20 12:00:00 -0700505/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800506 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700507
Steven Valdez02563852016-06-23 13:33:05 -0400508static const uint16_t kDefaultSignatureAlgorithms[] = {
509 SSL_SIGN_RSA_PKCS1_SHA512,
510 SSL_SIGN_ECDSA_SECP521R1_SHA512,
Adam Langley95c29f32014-06-20 12:00:00 -0700511
Steven Valdez02563852016-06-23 13:33:05 -0400512 SSL_SIGN_RSA_PKCS1_SHA384,
513 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Adam Langley95c29f32014-06-20 12:00:00 -0700514
Steven Valdez02563852016-06-23 13:33:05 -0400515 SSL_SIGN_RSA_PKCS1_SHA256,
516 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Adam Langley95c29f32014-06-20 12:00:00 -0700517
Steven Valdez02563852016-06-23 13:33:05 -0400518 SSL_SIGN_RSA_PKCS1_SHA1,
519 SSL_SIGN_ECDSA_SHA1,
Adam Langley95c29f32014-06-20 12:00:00 -0700520};
David Benjamin05da6e12014-07-12 20:42:55 -0400521
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400522static const uint16_t kDefaultTLS13SignatureAlgorithms[] = {
523 SSL_SIGN_RSA_PSS_SHA512,
524 SSL_SIGN_RSA_PKCS1_SHA512,
525 SSL_SIGN_ECDSA_SECP521R1_SHA512,
526
527 SSL_SIGN_RSA_PSS_SHA384,
528 SSL_SIGN_RSA_PKCS1_SHA384,
529 SSL_SIGN_ECDSA_SECP384R1_SHA384,
530
531 SSL_SIGN_RSA_PSS_SHA256,
532 SSL_SIGN_RSA_PKCS1_SHA256,
533 SSL_SIGN_ECDSA_SECP256R1_SHA256,
534
535 SSL_SIGN_RSA_PKCS1_SHA1,
536 SSL_SIGN_ECDSA_SHA1,
537};
538
Steven Valdez02563852016-06-23 13:33:05 -0400539size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400540 uint16_t version;
541 if (ssl->s3->have_version) {
542 version = ssl3_protocol_version(ssl);
543 } else {
544 version = ssl->method->version_from_wire(ssl->client_version);
545 }
546
547 if (version >= TLS1_3_VERSION) {
548 *psigs = kDefaultTLS13SignatureAlgorithms;
549 return sizeof(kDefaultTLS13SignatureAlgorithms) /
550 sizeof(kDefaultTLS13SignatureAlgorithms[0]);
551 }
552
Steven Valdez02563852016-06-23 13:33:05 -0400553 *psigs = kDefaultSignatureAlgorithms;
554 return sizeof(kDefaultSignatureAlgorithms) /
555 sizeof(kDefaultSignatureAlgorithms[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800556}
Adam Langley95c29f32014-06-20 12:00:00 -0700557
David Benjamin887c3002016-07-08 16:15:32 -0700558int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
Steven Valdez02563852016-06-23 13:33:05 -0400559 const uint16_t *sent_sigs;
560 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800561
Adam Langleyfcf25832014-12-18 17:42:32 -0800562 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500563 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400564 for (i = 0; i < sent_sigslen; i++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400565 if (sigalg == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800566 break;
567 }
568 }
569
David Benjamin788be4a2015-10-30 17:50:57 -0400570 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400571 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800572 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
573 return 0;
574 }
575
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 return 1;
577}
578
579/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
580 * supported or doesn't appear in supported signature algorithms. Unlike
581 * ssl_cipher_get_disabled this applies to a specific session and not global
582 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500583void ssl_set_client_disabled(SSL *ssl) {
584 CERT *c = ssl->cert;
Steven Valdez02563852016-06-23 13:33:05 -0400585 const uint16_t *sigalgs;
Adam Langleyfcf25832014-12-18 17:42:32 -0800586 size_t i, sigalgslen;
587 int have_rsa = 0, have_ecdsa = 0;
588 c->mask_a = 0;
589 c->mask_k = 0;
590
Adam Langleyfcf25832014-12-18 17:42:32 -0800591 /* Now go through all signature algorithms seeing if we support any for RSA,
592 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500593 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -0400594 for (i = 0; i < sigalgslen; i++) {
595 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400596 case SSL_SIGN_RSA_PSS_SHA512:
597 case SSL_SIGN_RSA_PSS_SHA384:
598 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400599 case SSL_SIGN_RSA_PKCS1_SHA512:
600 case SSL_SIGN_RSA_PKCS1_SHA384:
601 case SSL_SIGN_RSA_PKCS1_SHA256:
602 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800603 have_rsa = 1;
604 break;
605
Steven Valdez02563852016-06-23 13:33:05 -0400606 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
607 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
608 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
609 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800610 have_ecdsa = 1;
611 break;
612 }
613 }
614
615 /* Disable auth if we don't include any appropriate signature algorithms. */
616 if (!have_rsa) {
617 c->mask_a |= SSL_aRSA;
618 }
619 if (!have_ecdsa) {
620 c->mask_a |= SSL_aECDSA;
621 }
622
623 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500624 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800625 c->mask_a |= SSL_aPSK;
626 c->mask_k |= SSL_kPSK;
627 }
628}
Adam Langley95c29f32014-06-20 12:00:00 -0700629
Adam Langley614c66a2015-06-12 15:26:58 -0700630/* tls_extension represents a TLS extension that is handled internally. The
631 * |init| function is called for each handshake, before any other functions of
632 * the extension. Then the add and parse callbacks are called as needed.
633 *
634 * The parse callbacks receive a |CBS| that contains the contents of the
635 * extension (i.e. not including the type and length bytes). If an extension is
636 * not received then the parse callbacks will be called with a NULL CBS so that
637 * they can do any processing needed to handle the absence of an extension.
638 *
639 * The add callbacks receive a |CBB| to which the extension can be appended but
640 * the function is responsible for appending the type and length bytes too.
641 *
642 * All callbacks return one for success and zero for error. If a parse function
643 * returns zero then a fatal alert with value |*out_alert| will be sent. If
644 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
645struct tls_extension {
646 uint16_t value;
647 void (*init)(SSL *ssl);
648
649 int (*add_clienthello)(SSL *ssl, CBB *out);
650 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
651
652 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
653 int (*add_serverhello)(SSL *ssl, CBB *out);
654};
655
Steven Valdez6b8509a2016-07-12 13:38:32 -0400656static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
657 if (contents != NULL) {
658 /* Servers MUST NOT send this extension. */
659 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
660 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
661 return 0;
662 }
663
664 return 1;
665}
666
667static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
668 /* This extension from the client is handled elsewhere. */
669 return 1;
670}
671
672static int dont_add_serverhello(SSL *ssl, CBB *out) {
673 return 1;
674}
Adam Langley614c66a2015-06-12 15:26:58 -0700675
676/* Server name indication (SNI).
677 *
678 * https://tools.ietf.org/html/rfc6066#section-3. */
679
680static void ext_sni_init(SSL *ssl) {
681 ssl->s3->tmp.should_ack_sni = 0;
682}
683
684static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
685 if (ssl->tlsext_hostname == NULL) {
686 return 1;
687 }
688
689 CBB contents, server_name_list, name;
690 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
691 !CBB_add_u16_length_prefixed(out, &contents) ||
692 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
693 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
694 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
695 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
696 strlen(ssl->tlsext_hostname)) ||
697 !CBB_flush(out)) {
698 return 0;
699 }
700
701 return 1;
702}
703
David Benjamin0d56f882015-12-19 17:05:56 -0500704static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
705 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700706 if (contents == NULL) {
707 return 1;
708 }
709
710 if (CBS_len(contents) != 0) {
711 return 0;
712 }
713
714 assert(ssl->tlsext_hostname != NULL);
715
716 if (!ssl->hit) {
717 assert(ssl->session->tlsext_hostname == NULL);
718 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
719 if (!ssl->session->tlsext_hostname) {
720 *out_alert = SSL_AD_INTERNAL_ERROR;
721 return 0;
722 }
723 }
724
725 return 1;
726}
727
David Benjamin0d56f882015-12-19 17:05:56 -0500728static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
729 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700730 if (contents == NULL) {
731 return 1;
732 }
733
David Benjamin9b611e22016-03-03 08:48:30 -0500734 CBS server_name_list, host_name;
735 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700736 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500737 !CBS_get_u8(&server_name_list, &name_type) ||
738 /* Although the server_name extension was intended to be extensible to
739 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
740 * different name types will cause an error. Further, RFC 4366 originally
741 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
742 * adding new name types is no longer feasible.
743 *
744 * Act as if the extensibility does not exist to simplify parsing. */
745 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
746 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700747 CBS_len(contents) != 0) {
748 return 0;
749 }
750
David Benjamin9b611e22016-03-03 08:48:30 -0500751 if (name_type != TLSEXT_NAMETYPE_host_name ||
752 CBS_len(&host_name) == 0 ||
753 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
754 CBS_contains_zero_byte(&host_name)) {
755 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
756 return 0;
757 }
Adam Langley614c66a2015-06-12 15:26:58 -0700758
David Benjamin9b611e22016-03-03 08:48:30 -0500759 /* TODO(davidben): SNI should be resolved before resumption. We have the
760 * early callback as a replacement, but we should fix the current callback
761 * and avoid the need for |SSL_CTX_set_session_id_context|. */
762 if (!ssl->hit) {
763 assert(ssl->session->tlsext_hostname == NULL);
764
765 /* Copy the hostname as a string. */
766 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
767 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700768 return 0;
769 }
770
David Benjamin9b611e22016-03-03 08:48:30 -0500771 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700772 }
773
774 return 1;
775}
776
777static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
778 if (ssl->hit ||
779 !ssl->s3->tmp.should_ack_sni ||
780 ssl->session->tlsext_hostname == NULL) {
781 return 1;
782 }
783
784 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
785 !CBB_add_u16(out, 0 /* length */)) {
786 return 0;
787 }
788
789 return 1;
790}
791
792
Adam Langley5021b222015-06-12 18:27:58 -0700793/* Renegotiation indication.
794 *
795 * https://tools.ietf.org/html/rfc5746 */
796
797static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
798 CBB contents, prev_finished;
799 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
800 !CBB_add_u16_length_prefixed(out, &contents) ||
801 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
802 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
803 ssl->s3->previous_client_finished_len) ||
804 !CBB_flush(out)) {
805 return 0;
806 }
807
808 return 1;
809}
810
811static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
812 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400813 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
814 return 0;
815 }
816
David Benjamin3e052de2015-11-25 20:10:31 -0500817 /* Servers may not switch between omitting the extension and supporting it.
818 * See RFC 5746, sections 3.5 and 4.2. */
819 if (ssl->s3->initial_handshake_complete &&
820 (contents != NULL) != ssl->s3->send_connection_binding) {
821 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
822 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
823 return 0;
824 }
825
Adam Langley5021b222015-06-12 18:27:58 -0700826 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500827 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700828 * RI even on initial ServerHello because the client doesn't see any
829 * renegotiation during an attack. However this would mean we could not
830 * connect to any server which doesn't support RI.
831 *
David Benjamine9cddb82015-11-23 14:36:40 -0500832 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
833 * practical terms every client sets it so it's just assumed here. */
834 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700835 }
836
837 const size_t expected_len = ssl->s3->previous_client_finished_len +
838 ssl->s3->previous_server_finished_len;
839
840 /* Check for logic errors */
841 assert(!expected_len || ssl->s3->previous_client_finished_len);
842 assert(!expected_len || ssl->s3->previous_server_finished_len);
843
844 /* Parse out the extension contents. */
845 CBS renegotiated_connection;
846 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
847 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400848 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700849 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
850 return 0;
851 }
852
853 /* Check that the extension matches. */
854 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400855 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700856 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
857 return 0;
858 }
859
860 const uint8_t *d = CBS_data(&renegotiated_connection);
861 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
862 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400863 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700864 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
865 return 0;
866 }
867 d += ssl->s3->previous_client_finished_len;
868
869 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
870 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400871 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700872 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
873 return 0;
874 }
875 ssl->s3->send_connection_binding = 1;
876
877 return 1;
878}
879
880static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
881 CBS *contents) {
882 /* Renegotiation isn't supported as a server so this function should never be
883 * called after the initial handshake. */
884 assert(!ssl->s3->initial_handshake_complete);
885
Steven Valdez143e8b32016-07-11 13:19:03 -0400886 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
887 return 1;
888 }
889
Adam Langley5021b222015-06-12 18:27:58 -0700890 CBS fake_contents;
891 static const uint8_t kFakeExtension[] = {0};
892
893 if (contents == NULL) {
894 if (ssl->s3->send_connection_binding) {
895 /* The renegotiation SCSV was received so pretend that we received a
896 * renegotiation extension. */
897 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
898 contents = &fake_contents;
899 /* We require that the renegotiation extension is at index zero of
900 * kExtensions. */
901 ssl->s3->tmp.extensions.received |= (1u << 0);
902 } else {
903 return 1;
904 }
905 }
906
907 CBS renegotiated_connection;
908
909 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
910 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400911 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700912 return 0;
913 }
914
915 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500916 if (!CBS_mem_equal(&renegotiated_connection,
917 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700918 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400919 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700920 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
921 return 0;
922 }
923
924 ssl->s3->send_connection_binding = 1;
925
926 return 1;
927}
928
929static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400930 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
931 return 1;
932 }
933
Adam Langley5021b222015-06-12 18:27:58 -0700934 CBB contents, prev_finished;
935 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
936 !CBB_add_u16_length_prefixed(out, &contents) ||
937 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
938 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
939 ssl->s3->previous_client_finished_len) ||
940 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
941 ssl->s3->previous_server_finished_len) ||
942 !CBB_flush(out)) {
943 return 0;
944 }
945
946 return 1;
947}
948
Adam Langley0a056712015-07-01 15:03:33 -0700949
950/* Extended Master Secret.
951 *
David Benjamin43946d42016-02-01 08:42:19 -0500952 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700953
954static void ext_ems_init(SSL *ssl) {
955 ssl->s3->tmp.extended_master_secret = 0;
956}
957
958static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
959 if (ssl->version == SSL3_VERSION) {
960 return 1;
961 }
962
963 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
964 !CBB_add_u16(out, 0 /* length */)) {
965 return 0;
966 }
967
968 return 1;
969}
970
971static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
972 CBS *contents) {
973 if (contents == NULL) {
974 return 1;
975 }
976
Steven Valdez143e8b32016-07-11 13:19:03 -0400977 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
978 ssl->version == SSL3_VERSION) {
979 return 0;
980 }
981
982 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700983 return 0;
984 }
985
986 ssl->s3->tmp.extended_master_secret = 1;
987 return 1;
988}
989
David Benjamin0d56f882015-12-19 17:05:56 -0500990static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
991 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400992 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
993 ssl->version == SSL3_VERSION) {
994 return 1;
995 }
996
997 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700998 return 1;
999 }
1000
1001 if (CBS_len(contents) != 0) {
1002 return 0;
1003 }
1004
1005 ssl->s3->tmp.extended_master_secret = 1;
1006 return 1;
1007}
1008
1009static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1010 if (!ssl->s3->tmp.extended_master_secret) {
1011 return 1;
1012 }
1013
1014 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1015 !CBB_add_u16(out, 0 /* length */)) {
1016 return 0;
1017 }
1018
1019 return 1;
1020}
1021
Adam Langley9b05bc52015-07-01 15:25:33 -07001022
1023/* Session tickets.
1024 *
1025 * https://tools.ietf.org/html/rfc5077 */
1026
1027static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1028 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1029 return 1;
1030 }
1031
1032 const uint8_t *ticket_data = NULL;
1033 int ticket_len = 0;
1034
1035 /* Renegotiation does not participate in session resumption. However, still
1036 * advertise the extension to avoid potentially breaking servers which carry
1037 * over the state from the previous handshake, such as OpenSSL servers
1038 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1039 if (!ssl->s3->initial_handshake_complete &&
1040 ssl->session != NULL &&
1041 ssl->session->tlsext_tick != NULL) {
1042 ticket_data = ssl->session->tlsext_tick;
1043 ticket_len = ssl->session->tlsext_ticklen;
1044 }
1045
1046 CBB ticket;
1047 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1048 !CBB_add_u16_length_prefixed(out, &ticket) ||
1049 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1050 !CBB_flush(out)) {
1051 return 0;
1052 }
1053
1054 return 1;
1055}
1056
1057static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1058 CBS *contents) {
1059 ssl->tlsext_ticket_expected = 0;
1060
1061 if (contents == NULL) {
1062 return 1;
1063 }
1064
Steven Valdez143e8b32016-07-11 13:19:03 -04001065 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1066 return 0;
1067 }
1068
Adam Langley9b05bc52015-07-01 15:25:33 -07001069 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1070 * this function should never be called, even if the server tries to send the
1071 * extension. */
1072 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1073
1074 if (CBS_len(contents) != 0) {
1075 return 0;
1076 }
1077
1078 ssl->tlsext_ticket_expected = 1;
1079 return 1;
1080}
1081
Adam Langley9b05bc52015-07-01 15:25:33 -07001082static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1083 if (!ssl->tlsext_ticket_expected) {
1084 return 1;
1085 }
1086
1087 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1088 * true. */
1089 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1090
1091 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1092 !CBB_add_u16(out, 0 /* length */)) {
1093 return 0;
1094 }
1095
1096 return 1;
1097}
1098
1099
Adam Langley2e857bd2015-07-01 16:09:19 -07001100/* Signature Algorithms.
1101 *
1102 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1103
1104static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjaminb6a0a512016-06-21 10:33:21 -04001105 if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001106 return 1;
1107 }
1108
Steven Valdez02563852016-06-23 13:33:05 -04001109 const uint16_t *sigalgs_data;
Adam Langley2e857bd2015-07-01 16:09:19 -07001110 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1111
1112 CBB contents, sigalgs;
1113 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1114 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez02563852016-06-23 13:33:05 -04001115 !CBB_add_u16_length_prefixed(&contents, &sigalgs)) {
1116 return 0;
1117 }
1118
1119 size_t i;
1120 for (i = 0; i < sigalgs_len; i++) {
1121 if (!CBB_add_u16(&sigalgs, sigalgs_data[i])) {
1122 return 0;
1123 }
1124 }
1125
1126 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001127 return 0;
1128 }
1129
1130 return 1;
1131}
1132
Adam Langley2e857bd2015-07-01 16:09:19 -07001133static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1134 CBS *contents) {
1135 OPENSSL_free(ssl->cert->peer_sigalgs);
1136 ssl->cert->peer_sigalgs = NULL;
1137 ssl->cert->peer_sigalgslen = 0;
1138
Adam Langley2e857bd2015-07-01 16:09:19 -07001139 if (contents == NULL) {
1140 return 1;
1141 }
1142
1143 CBS supported_signature_algorithms;
1144 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001145 CBS_len(contents) != 0 ||
1146 CBS_len(&supported_signature_algorithms) == 0 ||
1147 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001148 return 0;
1149 }
1150
1151 return 1;
1152}
1153
Adam Langley2e857bd2015-07-01 16:09:19 -07001154
Adam Langleybb0bd042015-07-01 16:21:03 -07001155/* OCSP Stapling.
1156 *
1157 * https://tools.ietf.org/html/rfc6066#section-8 */
1158
1159static void ext_ocsp_init(SSL *ssl) {
1160 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001161 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001162}
1163
1164static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1165 if (!ssl->ocsp_stapling_enabled) {
1166 return 1;
1167 }
1168
1169 CBB contents;
1170 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1171 !CBB_add_u16_length_prefixed(out, &contents) ||
1172 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1173 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1174 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1175 !CBB_flush(out)) {
1176 return 0;
1177 }
1178
Adam Langleyce9d85e2016-01-24 15:58:39 -08001179 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001180 return 1;
1181}
1182
1183static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001184 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001185 if (contents == NULL) {
1186 return 1;
1187 }
1188
David Benjamin942f4ed2016-07-16 19:03:49 +03001189 /* OCSP stapling is forbidden on a non-certificate cipher. */
1190 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001191 return 0;
1192 }
1193
David Benjamin942f4ed2016-07-16 19:03:49 +03001194 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1195 if (CBS_len(contents) != 0) {
1196 return 0;
1197 }
1198
1199 ssl->s3->tmp.certificate_status_expected = 1;
1200 return 1;
1201 }
1202
1203 uint8_t status_type;
1204 CBS ocsp_response;
1205 if (!CBS_get_u8(contents, &status_type) ||
1206 status_type != TLSEXT_STATUSTYPE_ocsp ||
1207 !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1208 CBS_len(&ocsp_response) == 0 ||
1209 CBS_len(contents) != 0) {
1210 return 0;
1211 }
1212
1213 if (!CBS_stow(&ocsp_response, &ssl->session->ocsp_response,
1214 &ssl->session->ocsp_response_length)) {
1215 *out_alert = SSL_AD_INTERNAL_ERROR;
1216 return 0;
1217 }
1218
Adam Langleybb0bd042015-07-01 16:21:03 -07001219 return 1;
1220}
1221
1222static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1223 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001224 if (contents == NULL) {
1225 return 1;
1226 }
1227
1228 uint8_t status_type;
1229 if (!CBS_get_u8(contents, &status_type)) {
1230 return 0;
1231 }
1232
1233 /* We cannot decide whether OCSP stapling will occur yet because the correct
1234 * SSL_CTX might not have been selected. */
1235 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1236
Adam Langleybb0bd042015-07-01 16:21:03 -07001237 return 1;
1238}
1239
1240static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001241 if (!ssl->s3->tmp.ocsp_stapling_requested ||
1242 ssl->ctx->ocsp_response_length == 0 ||
1243 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001244 return 1;
1245 }
1246
David Benjamin942f4ed2016-07-16 19:03:49 +03001247 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1248 /* The extension shouldn't be sent when resuming sessions. */
1249 if (ssl->hit) {
1250 return 1;
1251 }
1252
1253 ssl->s3->tmp.certificate_status_expected = 1;
1254
1255 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1256 CBB_add_u16(out, 0 /* length */);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001257 }
1258
David Benjamin942f4ed2016-07-16 19:03:49 +03001259 CBB body, ocsp_response;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001260 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
David Benjamin942f4ed2016-07-16 19:03:49 +03001261 CBB_add_u16_length_prefixed(out, &body) &&
1262 CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1263 CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1264 CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1265 ssl->ctx->ocsp_response_length) &&
1266 CBB_flush(out);
Adam Langleybb0bd042015-07-01 16:21:03 -07001267}
1268
1269
Adam Langley97dfcbf2015-07-01 18:35:20 -07001270/* Next protocol negotiation.
1271 *
1272 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1273
1274static void ext_npn_init(SSL *ssl) {
1275 ssl->s3->next_proto_neg_seen = 0;
1276}
1277
1278static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1279 if (ssl->s3->initial_handshake_complete ||
1280 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001281 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001282 SSL_IS_DTLS(ssl)) {
1283 return 1;
1284 }
1285
1286 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1287 !CBB_add_u16(out, 0 /* length */)) {
1288 return 0;
1289 }
1290
1291 return 1;
1292}
1293
1294static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1295 CBS *contents) {
1296 if (contents == NULL) {
1297 return 1;
1298 }
1299
Steven Valdez143e8b32016-07-11 13:19:03 -04001300 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1301 return 0;
1302 }
1303
Adam Langley97dfcbf2015-07-01 18:35:20 -07001304 /* If any of these are false then we should never have sent the NPN
1305 * extension in the ClientHello and thus this function should never have been
1306 * called. */
1307 assert(!ssl->s3->initial_handshake_complete);
1308 assert(!SSL_IS_DTLS(ssl));
1309 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001310 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001311
David Benjamin76c2efc2015-08-31 14:24:29 -04001312 if (ssl->s3->alpn_selected != NULL) {
1313 /* NPN and ALPN may not be negotiated in the same connection. */
1314 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1315 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1316 return 0;
1317 }
1318
Adam Langley97dfcbf2015-07-01 18:35:20 -07001319 const uint8_t *const orig_contents = CBS_data(contents);
1320 const size_t orig_len = CBS_len(contents);
1321
1322 while (CBS_len(contents) != 0) {
1323 CBS proto;
1324 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1325 CBS_len(&proto) == 0) {
1326 return 0;
1327 }
1328 }
1329
1330 uint8_t *selected;
1331 uint8_t selected_len;
1332 if (ssl->ctx->next_proto_select_cb(
1333 ssl, &selected, &selected_len, orig_contents, orig_len,
1334 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1335 *out_alert = SSL_AD_INTERNAL_ERROR;
1336 return 0;
1337 }
1338
David Benjamin79978df2015-12-25 15:56:49 -05001339 OPENSSL_free(ssl->s3->next_proto_negotiated);
1340 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1341 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001342 *out_alert = SSL_AD_INTERNAL_ERROR;
1343 return 0;
1344 }
1345
David Benjamin79978df2015-12-25 15:56:49 -05001346 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001347 ssl->s3->next_proto_neg_seen = 1;
1348
1349 return 1;
1350}
1351
1352static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1353 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001354 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1355 return 1;
1356 }
1357
Adam Langley97dfcbf2015-07-01 18:35:20 -07001358 if (contents != NULL && CBS_len(contents) != 0) {
1359 return 0;
1360 }
1361
1362 if (contents == NULL ||
1363 ssl->s3->initial_handshake_complete ||
1364 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1365 * afterwards, parsing the ALPN extension will clear
1366 * |next_proto_neg_seen|. */
1367 ssl->s3->alpn_selected != NULL ||
1368 ssl->ctx->next_protos_advertised_cb == NULL ||
1369 SSL_IS_DTLS(ssl)) {
1370 return 1;
1371 }
1372
1373 ssl->s3->next_proto_neg_seen = 1;
1374 return 1;
1375}
1376
1377static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1378 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1379 * parsed. */
1380 if (!ssl->s3->next_proto_neg_seen) {
1381 return 1;
1382 }
1383
1384 const uint8_t *npa;
1385 unsigned npa_len;
1386
1387 if (ssl->ctx->next_protos_advertised_cb(
1388 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1389 SSL_TLSEXT_ERR_OK) {
1390 ssl->s3->next_proto_neg_seen = 0;
1391 return 1;
1392 }
1393
1394 CBB contents;
1395 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1396 !CBB_add_u16_length_prefixed(out, &contents) ||
1397 !CBB_add_bytes(&contents, npa, npa_len) ||
1398 !CBB_flush(out)) {
1399 return 0;
1400 }
1401
1402 return 1;
1403}
1404
1405
Adam Langleyab8d87d2015-07-10 12:21:39 -07001406/* Signed certificate timestamps.
1407 *
1408 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1409
1410static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1411 if (!ssl->signed_cert_timestamps_enabled) {
1412 return 1;
1413 }
1414
1415 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1416 !CBB_add_u16(out, 0 /* length */)) {
1417 return 0;
1418 }
1419
1420 return 1;
1421}
1422
1423static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1424 CBS *contents) {
1425 if (contents == NULL) {
1426 return 1;
1427 }
1428
1429 /* If this is false then we should never have sent the SCT extension in the
1430 * ClientHello and thus this function should never have been called. */
1431 assert(ssl->signed_cert_timestamps_enabled);
1432
1433 if (CBS_len(contents) == 0) {
1434 *out_alert = SSL_AD_DECODE_ERROR;
1435 return 0;
1436 }
1437
1438 /* Session resumption uses the original session information. */
1439 if (!ssl->hit &&
1440 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1441 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1442 *out_alert = SSL_AD_INTERNAL_ERROR;
1443 return 0;
1444 }
1445
1446 return 1;
1447}
1448
1449static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1450 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001451 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001452}
1453
1454static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001455 /* The extension shouldn't be sent when resuming sessions. */
1456 if (ssl->hit ||
1457 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001458 return 1;
1459 }
1460
1461 CBB contents;
1462 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1463 CBB_add_u16_length_prefixed(out, &contents) &&
1464 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1465 ssl->ctx->signed_cert_timestamp_list_length) &&
1466 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001467}
1468
1469
Adam Langleyf18e4532015-07-10 13:39:53 -07001470/* Application-level Protocol Negotiation.
1471 *
1472 * https://tools.ietf.org/html/rfc7301 */
1473
1474static void ext_alpn_init(SSL *ssl) {
1475 OPENSSL_free(ssl->s3->alpn_selected);
1476 ssl->s3->alpn_selected = NULL;
1477}
1478
1479static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1480 if (ssl->alpn_client_proto_list == NULL ||
1481 ssl->s3->initial_handshake_complete) {
1482 return 1;
1483 }
1484
1485 CBB contents, proto_list;
1486 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1487 !CBB_add_u16_length_prefixed(out, &contents) ||
1488 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1489 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1490 ssl->alpn_client_proto_list_len) ||
1491 !CBB_flush(out)) {
1492 return 0;
1493 }
1494
1495 return 1;
1496}
1497
1498static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1499 CBS *contents) {
1500 if (contents == NULL) {
1501 return 1;
1502 }
1503
1504 assert(!ssl->s3->initial_handshake_complete);
1505 assert(ssl->alpn_client_proto_list != NULL);
1506
David Benjamin76c2efc2015-08-31 14:24:29 -04001507 if (ssl->s3->next_proto_neg_seen) {
1508 /* NPN and ALPN may not be negotiated in the same connection. */
1509 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1510 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1511 return 0;
1512 }
1513
Adam Langleyf18e4532015-07-10 13:39:53 -07001514 /* The extension data consists of a ProtocolNameList which must have
1515 * exactly one ProtocolName. Each of these is length-prefixed. */
1516 CBS protocol_name_list, protocol_name;
1517 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1518 CBS_len(contents) != 0 ||
1519 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1520 /* Empty protocol names are forbidden. */
1521 CBS_len(&protocol_name) == 0 ||
1522 CBS_len(&protocol_name_list) != 0) {
1523 return 0;
1524 }
1525
1526 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1527 &ssl->s3->alpn_selected_len)) {
1528 *out_alert = SSL_AD_INTERNAL_ERROR;
1529 return 0;
1530 }
1531
1532 return 1;
1533}
1534
1535static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1536 CBS *contents) {
1537 if (contents == NULL) {
1538 return 1;
1539 }
1540
1541 if (ssl->ctx->alpn_select_cb == NULL ||
1542 ssl->s3->initial_handshake_complete) {
1543 return 1;
1544 }
1545
1546 /* ALPN takes precedence over NPN. */
1547 ssl->s3->next_proto_neg_seen = 0;
1548
1549 CBS protocol_name_list;
1550 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1551 CBS_len(contents) != 0 ||
1552 CBS_len(&protocol_name_list) < 2) {
1553 return 0;
1554 }
1555
1556 /* Validate the protocol list. */
1557 CBS protocol_name_list_copy = protocol_name_list;
1558 while (CBS_len(&protocol_name_list_copy) > 0) {
1559 CBS protocol_name;
1560
1561 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1562 /* Empty protocol names are forbidden. */
1563 CBS_len(&protocol_name) == 0) {
1564 return 0;
1565 }
1566 }
1567
1568 const uint8_t *selected;
1569 uint8_t selected_len;
1570 if (ssl->ctx->alpn_select_cb(
1571 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1572 CBS_len(&protocol_name_list),
1573 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1574 OPENSSL_free(ssl->s3->alpn_selected);
1575 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1576 if (ssl->s3->alpn_selected == NULL) {
1577 *out_alert = SSL_AD_INTERNAL_ERROR;
1578 return 0;
1579 }
1580 ssl->s3->alpn_selected_len = selected_len;
1581 }
1582
1583 return 1;
1584}
1585
1586static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1587 if (ssl->s3->alpn_selected == NULL) {
1588 return 1;
1589 }
1590
1591 CBB contents, proto_list, proto;
1592 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1593 !CBB_add_u16_length_prefixed(out, &contents) ||
1594 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1595 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001596 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1597 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001598 !CBB_flush(out)) {
1599 return 0;
1600 }
1601
1602 return 1;
1603}
1604
1605
Adam Langley49c7af12015-07-10 14:33:46 -07001606/* Channel ID.
1607 *
1608 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1609
1610static void ext_channel_id_init(SSL *ssl) {
1611 ssl->s3->tlsext_channel_id_valid = 0;
1612}
1613
1614static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1615 if (!ssl->tlsext_channel_id_enabled ||
1616 SSL_IS_DTLS(ssl)) {
1617 return 1;
1618 }
1619
1620 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1621 !CBB_add_u16(out, 0 /* length */)) {
1622 return 0;
1623 }
1624
1625 return 1;
1626}
1627
1628static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1629 CBS *contents) {
1630 if (contents == NULL) {
1631 return 1;
1632 }
1633
Steven Valdez143e8b32016-07-11 13:19:03 -04001634 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1635 return 0;
1636 }
1637
Adam Langley49c7af12015-07-10 14:33:46 -07001638 assert(!SSL_IS_DTLS(ssl));
1639 assert(ssl->tlsext_channel_id_enabled);
1640
1641 if (CBS_len(contents) != 0) {
1642 return 0;
1643 }
1644
1645 ssl->s3->tlsext_channel_id_valid = 1;
1646 return 1;
1647}
1648
1649static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1650 CBS *contents) {
1651 if (contents == NULL ||
1652 !ssl->tlsext_channel_id_enabled ||
1653 SSL_IS_DTLS(ssl)) {
1654 return 1;
1655 }
1656
1657 if (CBS_len(contents) != 0) {
1658 return 0;
1659 }
1660
1661 ssl->s3->tlsext_channel_id_valid = 1;
1662 return 1;
1663}
1664
1665static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001666 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1667 return 1;
1668 }
1669
Adam Langley49c7af12015-07-10 14:33:46 -07001670 if (!ssl->s3->tlsext_channel_id_valid) {
1671 return 1;
1672 }
1673
1674 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1675 !CBB_add_u16(out, 0 /* length */)) {
1676 return 0;
1677 }
1678
1679 return 1;
1680}
1681
Adam Langley391250d2015-07-15 19:06:07 -07001682
1683/* Secure Real-time Transport Protocol (SRTP) extension.
1684 *
1685 * https://tools.ietf.org/html/rfc5764 */
1686
Adam Langley391250d2015-07-15 19:06:07 -07001687
1688static void ext_srtp_init(SSL *ssl) {
1689 ssl->srtp_profile = NULL;
1690}
1691
1692static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1693 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1694 if (profiles == NULL) {
1695 return 1;
1696 }
1697 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1698 if (num_profiles == 0) {
1699 return 1;
1700 }
1701
1702 CBB contents, profile_ids;
1703 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1704 !CBB_add_u16_length_prefixed(out, &contents) ||
1705 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1706 return 0;
1707 }
1708
1709 size_t i;
1710 for (i = 0; i < num_profiles; i++) {
1711 if (!CBB_add_u16(&profile_ids,
1712 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1713 return 0;
1714 }
1715 }
1716
1717 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1718 !CBB_flush(out)) {
1719 return 0;
1720 }
1721
1722 return 1;
1723}
1724
1725static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1726 CBS *contents) {
1727 if (contents == NULL) {
1728 return 1;
1729 }
1730
1731 /* The extension consists of a u16-prefixed profile ID list containing a
1732 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1733 *
1734 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1735 CBS profile_ids, srtp_mki;
1736 uint16_t profile_id;
1737 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1738 !CBS_get_u16(&profile_ids, &profile_id) ||
1739 CBS_len(&profile_ids) != 0 ||
1740 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1741 CBS_len(contents) != 0) {
1742 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1743 return 0;
1744 }
1745
1746 if (CBS_len(&srtp_mki) != 0) {
1747 /* Must be no MKI, since we never offer one. */
1748 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1749 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1750 return 0;
1751 }
1752
1753 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1754
1755 /* Check to see if the server gave us something we support (and presumably
1756 * offered). */
1757 size_t i;
1758 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1759 const SRTP_PROTECTION_PROFILE *profile =
1760 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1761
1762 if (profile->id == profile_id) {
1763 ssl->srtp_profile = profile;
1764 return 1;
1765 }
1766 }
1767
1768 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1769 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1770 return 0;
1771}
1772
1773static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1774 CBS *contents) {
1775 if (contents == NULL) {
1776 return 1;
1777 }
1778
1779 CBS profile_ids, srtp_mki;
1780 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1781 CBS_len(&profile_ids) < 2 ||
1782 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1783 CBS_len(contents) != 0) {
1784 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1785 return 0;
1786 }
1787 /* Discard the MKI value for now. */
1788
1789 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1790 SSL_get_srtp_profiles(ssl);
1791
1792 /* Pick the server's most preferred profile. */
1793 size_t i;
1794 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1795 const SRTP_PROTECTION_PROFILE *server_profile =
1796 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1797
1798 CBS profile_ids_tmp;
1799 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1800
1801 while (CBS_len(&profile_ids_tmp) > 0) {
1802 uint16_t profile_id;
1803 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1804 return 0;
1805 }
1806
1807 if (server_profile->id == profile_id) {
1808 ssl->srtp_profile = server_profile;
1809 return 1;
1810 }
1811 }
1812 }
1813
1814 return 1;
1815}
1816
1817static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1818 if (ssl->srtp_profile == NULL) {
1819 return 1;
1820 }
1821
1822 CBB contents, profile_ids;
1823 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1824 !CBB_add_u16_length_prefixed(out, &contents) ||
1825 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1826 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1827 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1828 !CBB_flush(out)) {
1829 return 0;
1830 }
1831
1832 return 1;
1833}
1834
Adam Langleybdd5d662015-07-20 16:19:08 -07001835
1836/* EC point formats.
1837 *
1838 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1839
1840static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1841 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1842 return 0;
1843 }
1844
1845 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1846
1847 size_t i;
1848 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1849 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1850
1851 const uint32_t alg_k = cipher->algorithm_mkey;
1852 const uint32_t alg_a = cipher->algorithm_auth;
1853 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1854 return 1;
1855 }
1856 }
1857
1858 return 0;
1859}
1860
Adam Langleybdd5d662015-07-20 16:19:08 -07001861static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001862 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001863 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1864 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001865 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1866 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001867 !CBB_flush(out)) {
1868 return 0;
1869 }
1870
1871 return 1;
1872}
1873
1874static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1875 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1876 return 1;
1877 }
1878
1879 return ext_ec_point_add_extension(ssl, out);
1880}
1881
1882static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1883 CBS *contents) {
1884 if (contents == NULL) {
1885 return 1;
1886 }
1887
Steven Valdez143e8b32016-07-11 13:19:03 -04001888 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1889 return 0;
1890 }
1891
Adam Langleybdd5d662015-07-20 16:19:08 -07001892 CBS ec_point_format_list;
1893 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1894 CBS_len(contents) != 0) {
1895 return 0;
1896 }
1897
David Benjaminfc059942015-07-30 23:01:59 -04001898 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1899 * point format. */
1900 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1901 CBS_len(&ec_point_format_list)) == NULL) {
1902 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001903 return 0;
1904 }
1905
1906 return 1;
1907}
1908
1909static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1910 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001911 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1912 return 1;
1913 }
1914
Adam Langleybdd5d662015-07-20 16:19:08 -07001915 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1916}
1917
1918static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001919 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1920 return 1;
1921 }
1922
Adam Langleybdd5d662015-07-20 16:19:08 -07001923 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1924 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001925 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001926
1927 if (!using_ecc) {
1928 return 1;
1929 }
1930
1931 return ext_ec_point_add_extension(ssl, out);
1932}
1933
Adam Langley273d49c2015-07-20 16:38:52 -07001934
Steven Valdez143e8b32016-07-11 13:19:03 -04001935/* Draft Version Extension */
1936
1937static int ext_draft_version_add_clienthello(SSL *ssl, CBB *out) {
1938 uint16_t min_version, max_version;
1939 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
Steven Valdez3a287552016-07-19 05:10:50 -04001940 max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001941 return 1;
1942 }
1943
1944 CBB contents;
1945 if (!CBB_add_u16(out, TLSEXT_TYPE_draft_version) ||
1946 !CBB_add_u16_length_prefixed(out, &contents) ||
1947 !CBB_add_u16(&contents, TLS1_3_DRAFT_VERSION)) {
1948 return 0;
1949 }
1950
1951 return CBB_flush(out);
1952}
1953
1954
1955/* Key Share
1956 *
1957 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
1958
1959static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
1960 uint16_t min_version, max_version;
1961 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1962 return 0;
1963 }
1964
1965 if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
1966 return 1;
1967 }
1968
1969 CBB contents, kse_bytes;
1970 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
1971 !CBB_add_u16_length_prefixed(out, &contents) ||
1972 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
1973 return 0;
1974 }
1975
1976 const uint16_t *groups;
1977 size_t groups_len;
Steven Valdez5440fe02016-07-18 12:40:30 -04001978 if (ssl->s3->hs->retry_group) {
1979 /* Append the new key share to the old list. */
1980 if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
1981 ssl->s3->hs->key_share_bytes_len)) {
1982 return 0;
1983 }
1984 OPENSSL_free(ssl->s3->hs->key_share_bytes);
1985 ssl->s3->hs->key_share_bytes = NULL;
1986
1987 groups = &ssl->s3->hs->retry_group;
1988 groups_len = 1;
1989 } else {
1990 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
1991 /* Only send the top two preferred key shares. */
1992 if (groups_len > 2) {
1993 groups_len = 2;
1994 }
1995 }
Steven Valdez143e8b32016-07-11 13:19:03 -04001996
1997 ssl->s3->hs->groups = OPENSSL_malloc(groups_len * sizeof(SSL_ECDH_CTX));
1998 if (ssl->s3->hs->groups == NULL) {
1999 return 0;
2000 }
2001 memset(ssl->s3->hs->groups, 0, groups_len * sizeof(SSL_ECDH_CTX));
2002 ssl->s3->hs->groups_len = groups_len;
2003
2004 for (size_t i = 0; i < groups_len; i++) {
2005 if (!CBB_add_u16(&kse_bytes, groups[i])) {
2006 return 0;
2007 }
2008
2009 CBB key_exchange;
2010 if (!CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2011 !SSL_ECDH_CTX_init(&ssl->s3->hs->groups[i], groups[i]) ||
2012 !SSL_ECDH_CTX_offer(&ssl->s3->hs->groups[i], &key_exchange) ||
2013 !CBB_flush(&kse_bytes)) {
2014 return 0;
2015 }
2016 }
2017
Steven Valdez5440fe02016-07-18 12:40:30 -04002018 if (!ssl->s3->hs->retry_group) {
2019 /* Save the contents of the extension to repeat it in the second
2020 * ClientHello. */
2021 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2022 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2023 CBB_len(&kse_bytes));
2024 if (ssl->s3->hs->key_share_bytes == NULL) {
2025 return 0;
2026 }
2027 }
2028
Steven Valdez143e8b32016-07-11 13:19:03 -04002029 return CBB_flush(out);
2030}
2031
2032int ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2033 size_t *out_secret_len, uint8_t *out_alert,
2034 CBS *contents) {
2035 CBS peer_key;
2036 uint16_t group;
2037 if (!CBS_get_u16(contents, &group) ||
2038 !CBS_get_u16_length_prefixed(contents, &peer_key)) {
2039 *out_alert = SSL_AD_DECODE_ERROR;
2040 return 0;
2041 }
2042
2043 SSL_ECDH_CTX *group_ctx = NULL;
2044 for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) {
2045 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->groups[i]) == group) {
2046 group_ctx = &ssl->s3->hs->groups[i];
2047 break;
2048 }
2049 }
2050
2051 if (group_ctx == NULL) {
2052 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2053 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2054 return 0;
2055 }
2056
2057 if (!SSL_ECDH_CTX_finish(group_ctx, out_secret, out_secret_len, out_alert,
2058 CBS_data(&peer_key), CBS_len(&peer_key))) {
2059 *out_alert = SSL_AD_INTERNAL_ERROR;
2060 return 0;
2061 }
2062
Steven Valdez5440fe02016-07-18 12:40:30 -04002063 ssl_handshake_clear_groups(ssl->s3->hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04002064 return 1;
2065}
2066
Steven Valdez5440fe02016-07-18 12:40:30 -04002067int ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2068 uint8_t **out_secret,
Steven Valdez143e8b32016-07-11 13:19:03 -04002069 size_t *out_secret_len, uint8_t *out_alert,
2070 CBS *contents) {
2071 uint16_t group_id;
2072 CBS key_shares;
2073 if (!tls1_get_shared_group(ssl, &group_id) ||
2074 !CBS_get_u16_length_prefixed(contents, &key_shares)) {
2075 return 0;
2076 }
2077
Steven Valdez5440fe02016-07-18 12:40:30 -04002078 *out_found = 0;
Steven Valdez143e8b32016-07-11 13:19:03 -04002079 while (CBS_len(&key_shares) > 0) {
2080 uint16_t id;
2081 CBS peer_key;
2082 if (!CBS_get_u16(&key_shares, &id) ||
2083 !CBS_get_u16_length_prefixed(&key_shares, &peer_key)) {
2084 return 0;
2085 }
2086
Steven Valdez5440fe02016-07-18 12:40:30 -04002087 if (id != group_id || *out_found) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002088 continue;
2089 }
2090
2091 SSL_ECDH_CTX group;
2092 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2093 CBB public_key;
2094 if (!CBB_init(&public_key, 0) ||
2095 !SSL_ECDH_CTX_init(&group, group_id) ||
2096 !SSL_ECDH_CTX_accept(&group, &public_key, out_secret, out_secret_len,
2097 out_alert, CBS_data(&peer_key),
2098 CBS_len(&peer_key)) ||
2099 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2100 &ssl->s3->hs->public_key_len)) {
2101 SSL_ECDH_CTX_cleanup(&group);
2102 CBB_cleanup(&public_key);
2103 return 0;
2104 }
2105 SSL_ECDH_CTX_cleanup(&group);
2106
Steven Valdez5440fe02016-07-18 12:40:30 -04002107 *out_found = 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002108 }
2109
Steven Valdez5440fe02016-07-18 12:40:30 -04002110 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002111}
2112
2113int ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
2114 if (ssl->s3->tmp.new_cipher->algorithm_mkey != SSL_kECDHE) {
2115 return 1;
2116 }
2117
2118 uint16_t group_id;
2119 CBB kse_bytes, public_key;
2120 if (!tls1_get_shared_group(ssl, &group_id) ||
2121 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2122 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2123 !CBB_add_u16(&kse_bytes, group_id) ||
2124 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2125 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2126 ssl->s3->hs->public_key_len) ||
2127 !CBB_flush(out)) {
2128 return 0;
2129 }
2130
2131 return 1;
2132}
2133
2134
Steven Valdezce902a92016-05-17 11:47:53 -04002135/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002136 *
Steven Valdezce902a92016-05-17 11:47:53 -04002137 * https://tools.ietf.org/html/rfc4492#section-5.1.2
2138 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07002139
Steven Valdezce902a92016-05-17 11:47:53 -04002140static void ext_supported_groups_init(SSL *ssl) {
2141 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2142 ssl->s3->tmp.peer_supported_group_list = NULL;
2143 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07002144}
2145
Steven Valdezce902a92016-05-17 11:47:53 -04002146static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002147 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2148 return 1;
2149 }
2150
Steven Valdezce902a92016-05-17 11:47:53 -04002151 CBB contents, groups_bytes;
2152 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002153 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002154 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002155 return 0;
2156 }
2157
Steven Valdezce902a92016-05-17 11:47:53 -04002158 const uint16_t *groups;
2159 size_t groups_len;
2160 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002161
2162 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002163 for (i = 0; i < groups_len; i++) {
2164 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002165 return 0;
2166 }
2167 }
2168
2169 return CBB_flush(out);
2170}
2171
Steven Valdezce902a92016-05-17 11:47:53 -04002172static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2173 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002174 /* This extension is not expected to be echoed by servers and is ignored. */
2175 return 1;
2176}
2177
Steven Valdezce902a92016-05-17 11:47:53 -04002178static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2179 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002180 if (contents == NULL) {
2181 return 1;
2182 }
2183
Steven Valdezce902a92016-05-17 11:47:53 -04002184 CBS supported_group_list;
2185 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2186 CBS_len(&supported_group_list) == 0 ||
2187 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002188 CBS_len(contents) != 0) {
2189 return 0;
2190 }
2191
Steven Valdezce902a92016-05-17 11:47:53 -04002192 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
2193 CBS_len(&supported_group_list));
2194 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002195 *out_alert = SSL_AD_INTERNAL_ERROR;
2196 return 0;
2197 }
2198
Steven Valdezce902a92016-05-17 11:47:53 -04002199 const size_t num_groups = CBS_len(&supported_group_list) / 2;
Adam Langley273d49c2015-07-20 16:38:52 -07002200 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002201 for (i = 0; i < num_groups; i++) {
2202 if (!CBS_get_u16(&supported_group_list,
2203 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002204 goto err;
2205 }
2206 }
2207
Steven Valdezce902a92016-05-17 11:47:53 -04002208 assert(CBS_len(&supported_group_list) == 0);
2209 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002210
2211 return 1;
2212
2213err:
Steven Valdezce902a92016-05-17 11:47:53 -04002214 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2215 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002216 *out_alert = SSL_AD_INTERNAL_ERROR;
2217 return 0;
2218}
2219
Steven Valdezce902a92016-05-17 11:47:53 -04002220static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002221 /* Servers don't echo this extension. */
2222 return 1;
2223}
2224
2225
Adam Langley614c66a2015-06-12 15:26:58 -07002226/* kExtensions contains all the supported extensions. */
2227static const struct tls_extension kExtensions[] = {
2228 {
Adam Langley5021b222015-06-12 18:27:58 -07002229 /* The renegotiation extension must always be at index zero because the
2230 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2231 * sent as an SCSV. */
2232 TLSEXT_TYPE_renegotiate,
2233 NULL,
2234 ext_ri_add_clienthello,
2235 ext_ri_parse_serverhello,
2236 ext_ri_parse_clienthello,
2237 ext_ri_add_serverhello,
2238 },
2239 {
Adam Langley614c66a2015-06-12 15:26:58 -07002240 TLSEXT_TYPE_server_name,
2241 ext_sni_init,
2242 ext_sni_add_clienthello,
2243 ext_sni_parse_serverhello,
2244 ext_sni_parse_clienthello,
2245 ext_sni_add_serverhello,
2246 },
Adam Langley0a056712015-07-01 15:03:33 -07002247 {
2248 TLSEXT_TYPE_extended_master_secret,
2249 ext_ems_init,
2250 ext_ems_add_clienthello,
2251 ext_ems_parse_serverhello,
2252 ext_ems_parse_clienthello,
2253 ext_ems_add_serverhello,
2254 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002255 {
2256 TLSEXT_TYPE_session_ticket,
2257 NULL,
2258 ext_ticket_add_clienthello,
2259 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002260 /* Ticket extension client parsing is handled in ssl_session.c */
2261 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002262 ext_ticket_add_serverhello,
2263 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002264 {
2265 TLSEXT_TYPE_signature_algorithms,
2266 NULL,
2267 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002268 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002269 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002270 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002271 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002272 {
2273 TLSEXT_TYPE_status_request,
2274 ext_ocsp_init,
2275 ext_ocsp_add_clienthello,
2276 ext_ocsp_parse_serverhello,
2277 ext_ocsp_parse_clienthello,
2278 ext_ocsp_add_serverhello,
2279 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002280 {
2281 TLSEXT_TYPE_next_proto_neg,
2282 ext_npn_init,
2283 ext_npn_add_clienthello,
2284 ext_npn_parse_serverhello,
2285 ext_npn_parse_clienthello,
2286 ext_npn_add_serverhello,
2287 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002288 {
2289 TLSEXT_TYPE_certificate_timestamp,
2290 NULL,
2291 ext_sct_add_clienthello,
2292 ext_sct_parse_serverhello,
2293 ext_sct_parse_clienthello,
2294 ext_sct_add_serverhello,
2295 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002296 {
2297 TLSEXT_TYPE_application_layer_protocol_negotiation,
2298 ext_alpn_init,
2299 ext_alpn_add_clienthello,
2300 ext_alpn_parse_serverhello,
2301 ext_alpn_parse_clienthello,
2302 ext_alpn_add_serverhello,
2303 },
Adam Langley49c7af12015-07-10 14:33:46 -07002304 {
2305 TLSEXT_TYPE_channel_id,
2306 ext_channel_id_init,
2307 ext_channel_id_add_clienthello,
2308 ext_channel_id_parse_serverhello,
2309 ext_channel_id_parse_clienthello,
2310 ext_channel_id_add_serverhello,
2311 },
Adam Langley391250d2015-07-15 19:06:07 -07002312 {
2313 TLSEXT_TYPE_srtp,
2314 ext_srtp_init,
2315 ext_srtp_add_clienthello,
2316 ext_srtp_parse_serverhello,
2317 ext_srtp_parse_clienthello,
2318 ext_srtp_add_serverhello,
2319 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002320 {
2321 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002322 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002323 ext_ec_point_add_clienthello,
2324 ext_ec_point_parse_serverhello,
2325 ext_ec_point_parse_clienthello,
2326 ext_ec_point_add_serverhello,
2327 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002328 {
2329 TLSEXT_TYPE_draft_version,
2330 NULL,
2331 ext_draft_version_add_clienthello,
2332 forbid_parse_serverhello,
2333 ignore_parse_clienthello,
2334 dont_add_serverhello,
2335 },
2336 {
2337 TLSEXT_TYPE_key_share,
2338 NULL,
2339 ext_key_share_add_clienthello,
2340 forbid_parse_serverhello,
2341 ignore_parse_clienthello,
2342 dont_add_serverhello,
2343 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002344 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2345 * intolerant to the last extension being zero-length. See
2346 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002347 {
Steven Valdezce902a92016-05-17 11:47:53 -04002348 TLSEXT_TYPE_supported_groups,
2349 ext_supported_groups_init,
2350 ext_supported_groups_add_clienthello,
2351 ext_supported_groups_parse_serverhello,
2352 ext_supported_groups_parse_clienthello,
2353 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002354 },
Adam Langley614c66a2015-06-12 15:26:58 -07002355};
2356
2357#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2358
Adam Langley4cfa96b2015-07-01 11:56:55 -07002359OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2360 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002361 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002362OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2363 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2364 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002365 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002366
Adam Langley614c66a2015-06-12 15:26:58 -07002367static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2368 uint16_t value) {
2369 unsigned i;
2370 for (i = 0; i < kNumExtensions; i++) {
2371 if (kExtensions[i].value == value) {
2372 *out_index = i;
2373 return &kExtensions[i];
2374 }
2375 }
2376
2377 return NULL;
2378}
2379
Adam Langley09505632015-07-30 18:10:13 -07002380int SSL_extension_supported(unsigned extension_value) {
2381 uint32_t index;
2382 return extension_value == TLSEXT_TYPE_padding ||
2383 tls_extension_find(&index, extension_value) != NULL;
2384}
2385
David Benjamine8d53502015-10-10 14:13:23 -04002386int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002387 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002388 if (ssl->client_version == SSL3_VERSION &&
2389 !ssl->s3->send_connection_binding) {
2390 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002391 }
Adam Langley95c29f32014-06-20 12:00:00 -07002392
David Benjamine8d53502015-10-10 14:13:23 -04002393 CBB extensions;
2394 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002395 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002396 }
Adam Langley95c29f32014-06-20 12:00:00 -07002397
David Benjamine8d53502015-10-10 14:13:23 -04002398 ssl->s3->tmp.extensions.sent = 0;
2399 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002400
Adam Langley614c66a2015-06-12 15:26:58 -07002401 size_t i;
2402 for (i = 0; i < kNumExtensions; i++) {
2403 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002404 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002405 }
2406 }
Adam Langley95c29f32014-06-20 12:00:00 -07002407
Adam Langley614c66a2015-06-12 15:26:58 -07002408 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002409 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002410 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002411 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2412 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2413 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002414 }
Adam Langley95c29f32014-06-20 12:00:00 -07002415
Adam Langley33ad2b52015-07-20 17:43:53 -07002416 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002417 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002418 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002419 }
Adam Langley75712922014-10-10 16:23:43 -07002420
David Benjamine8d53502015-10-10 14:13:23 -04002421 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002422 goto err;
2423 }
2424
David Benjamine8d53502015-10-10 14:13:23 -04002425 if (!SSL_IS_DTLS(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002426 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002427 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002428 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002429 *
2430 * NB: because this code works out the length of all existing extensions
2431 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002432 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002433 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002434 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002435 * Server 7.0 is intolerant to the last extension being zero-length. See
2436 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002437 if (padding_len >= 4 + 1) {
2438 padding_len -= 4;
2439 } else {
2440 padding_len = 1;
2441 }
Adam Langley95c29f32014-06-20 12:00:00 -07002442
Adam Langley33ad2b52015-07-20 17:43:53 -07002443 uint8_t *padding_bytes;
2444 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2445 !CBB_add_u16(&extensions, padding_len) ||
2446 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2447 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002448 }
Adam Langley75712922014-10-10 16:23:43 -07002449
Adam Langley33ad2b52015-07-20 17:43:53 -07002450 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002451 }
2452 }
Adam Langley75712922014-10-10 16:23:43 -07002453
David Benjamina01deee2015-12-08 18:56:31 -05002454 /* Discard empty extensions blocks. */
2455 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002456 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002457 }
2458
David Benjamine8d53502015-10-10 14:13:23 -04002459 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002460
2461err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002462 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002463 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002464}
Adam Langley95c29f32014-06-20 12:00:00 -07002465
David Benjamin56380462015-10-10 14:59:09 -04002466int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002467 CBB extensions;
2468 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002469 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002470 }
2471
2472 unsigned i;
2473 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002474 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002475 /* Don't send extensions that were not received. */
2476 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002477 }
Adam Langley95c29f32014-06-20 12:00:00 -07002478
David Benjamin56380462015-10-10 14:59:09 -04002479 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002480 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2481 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2482 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002483 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002484 }
Adam Langley95c29f32014-06-20 12:00:00 -07002485
David Benjamin56380462015-10-10 14:59:09 -04002486 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002487 goto err;
2488 }
2489
Steven Valdez143e8b32016-07-11 13:19:03 -04002490 /* Discard empty extensions blocks before TLS 1.3. */
2491 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2492 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002493 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002494 }
2495
David Benjamin56380462015-10-10 14:59:09 -04002496 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002497
2498err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002499 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002500 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002501}
Adam Langley95c29f32014-06-20 12:00:00 -07002502
David Benjamin0d56f882015-12-19 17:05:56 -05002503static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002504 size_t i;
2505 for (i = 0; i < kNumExtensions; i++) {
2506 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002507 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002508 }
2509 }
2510
David Benjamin0d56f882015-12-19 17:05:56 -05002511 ssl->s3->tmp.extensions.received = 0;
2512 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002513 /* The renegotiation extension must always be at index zero because the
2514 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2515 * sent as an SCSV. */
2516 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002517
Adam Langleyfcf25832014-12-18 17:42:32 -08002518 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002519 if (CBS_len(cbs) != 0) {
2520 /* Decode the extensions block and check it is valid. */
2521 CBS extensions;
2522 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2523 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002524 *out_alert = SSL_AD_DECODE_ERROR;
2525 return 0;
2526 }
Adam Langley95c29f32014-06-20 12:00:00 -07002527
Adam Langley33ad2b52015-07-20 17:43:53 -07002528 while (CBS_len(&extensions) != 0) {
2529 uint16_t type;
2530 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002531
Adam Langley33ad2b52015-07-20 17:43:53 -07002532 /* Decode the next extension. */
2533 if (!CBS_get_u16(&extensions, &type) ||
2534 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2535 *out_alert = SSL_AD_DECODE_ERROR;
2536 return 0;
2537 }
2538
David Benjaminc7ce9772015-10-09 19:32:41 -04002539 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2540 * ambiguous. Ignore all but the renegotiation_info extension. */
David Benjamin0d56f882015-12-19 17:05:56 -05002541 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
David Benjaminc7ce9772015-10-09 19:32:41 -04002542 continue;
2543 }
2544
Adam Langley33ad2b52015-07-20 17:43:53 -07002545 unsigned ext_index;
2546 const struct tls_extension *const ext =
2547 tls_extension_find(&ext_index, type);
2548
2549 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002550 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002551 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2552 return 0;
2553 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002554 continue;
2555 }
2556
David Benjamin0d56f882015-12-19 17:05:56 -05002557 ssl->s3->tmp.extensions.received |= (1u << ext_index);
Adam Langley614c66a2015-06-12 15:26:58 -07002558 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002559 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
Adam Langley614c66a2015-06-12 15:26:58 -07002560 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002561 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2562 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002563 return 0;
2564 }
Adam Langley614c66a2015-06-12 15:26:58 -07002565 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002566 }
Adam Langley75712922014-10-10 16:23:43 -07002567
Adam Langley614c66a2015-06-12 15:26:58 -07002568 for (i = 0; i < kNumExtensions; i++) {
David Benjamin0d56f882015-12-19 17:05:56 -05002569 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002570 /* Extension wasn't observed so call the callback with a NULL
2571 * parameter. */
2572 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002573 if (!kExtensions[i].parse_clienthello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002574 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2575 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002576 *out_alert = alert;
2577 return 0;
2578 }
2579 }
2580 }
2581
Adam Langleyfcf25832014-12-18 17:42:32 -08002582 return 1;
2583}
Adam Langley95c29f32014-06-20 12:00:00 -07002584
David Benjamin0d56f882015-12-19 17:05:56 -05002585int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002586 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002587 if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) {
2588 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002589 return 0;
2590 }
Adam Langley95c29f32014-06-20 12:00:00 -07002591
David Benjamin0d56f882015-12-19 17:05:56 -05002592 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002593 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002594 return 0;
2595 }
Adam Langley95c29f32014-06-20 12:00:00 -07002596
Adam Langleyfcf25832014-12-18 17:42:32 -08002597 return 1;
2598}
Adam Langley95c29f32014-06-20 12:00:00 -07002599
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002600OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2601
David Benjamin0d56f882015-12-19 17:05:56 -05002602static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002603 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2604 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2605 return 1;
2606 }
Adam Langley614c66a2015-06-12 15:26:58 -07002607
Steven Valdez143e8b32016-07-11 13:19:03 -04002608 /* Decode the extensions block and check it is valid. */
2609 CBS extensions;
2610 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2611 !tls1_check_duplicate_extensions(&extensions)) {
2612 *out_alert = SSL_AD_DECODE_ERROR;
2613 return 0;
2614 }
2615
2616 uint32_t received = 0;
2617 while (CBS_len(&extensions) != 0) {
2618 uint16_t type;
2619 CBS extension;
2620
2621 /* Decode the next extension. */
2622 if (!CBS_get_u16(&extensions, &type) ||
2623 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002624 *out_alert = SSL_AD_DECODE_ERROR;
2625 return 0;
2626 }
Adam Langley95c29f32014-06-20 12:00:00 -07002627
Steven Valdez143e8b32016-07-11 13:19:03 -04002628 unsigned ext_index;
2629 const struct tls_extension *const ext =
2630 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002631
Steven Valdez143e8b32016-07-11 13:19:03 -04002632 if (ext == NULL) {
2633 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002634 return 0;
2635 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002636 continue;
2637 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002638
Steven Valdez143e8b32016-07-11 13:19:03 -04002639 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) {
2640 /* If the extension was never sent then it is illegal. */
2641 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2642 ERR_add_error_dataf("extension :%u", (unsigned)type);
2643 *out_alert = SSL_AD_DECODE_ERROR;
2644 return 0;
2645 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002646
Steven Valdez143e8b32016-07-11 13:19:03 -04002647 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002648
Steven Valdez143e8b32016-07-11 13:19:03 -04002649 uint8_t alert = SSL_AD_DECODE_ERROR;
2650 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2651 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2652 ERR_add_error_dataf("extension: %u", (unsigned)type);
2653 *out_alert = alert;
2654 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002655 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002656 }
Adam Langley95c29f32014-06-20 12:00:00 -07002657
Adam Langley33ad2b52015-07-20 17:43:53 -07002658 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002659 for (i = 0; i < kNumExtensions; i++) {
2660 if (!(received & (1u << i))) {
2661 /* Extension wasn't observed so call the callback with a NULL
2662 * parameter. */
2663 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002664 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002665 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2666 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002667 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002668 return 0;
2669 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002670 }
2671 }
Adam Langley95c29f32014-06-20 12:00:00 -07002672
Adam Langleyfcf25832014-12-18 17:42:32 -08002673 return 1;
2674}
Adam Langley95c29f32014-06-20 12:00:00 -07002675
David Benjamin0d56f882015-12-19 17:05:56 -05002676static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002677 int ret = SSL_TLSEXT_ERR_NOACK;
2678 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002679
David Benjamin78f8aab2016-03-10 16:33:58 -05002680 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002681 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002682 ssl->ctx->tlsext_servername_arg);
2683 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002684 ret = ssl->initial_ctx->tlsext_servername_callback(
2685 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002686 }
Adam Langley95c29f32014-06-20 12:00:00 -07002687
Adam Langleyfcf25832014-12-18 17:42:32 -08002688 switch (ret) {
2689 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002690 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002691 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002692
Adam Langleyfcf25832014-12-18 17:42:32 -08002693 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002694 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002695 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002696
Adam Langleyfcf25832014-12-18 17:42:32 -08002697 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002698 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002699 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002700
Adam Langleyfcf25832014-12-18 17:42:32 -08002701 default:
2702 return 1;
2703 }
2704}
Adam Langleyed8270a2014-09-02 13:52:56 -07002705
David Benjamin0d56f882015-12-19 17:05:56 -05002706static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002707 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002708 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002709
David Benjamin78f8aab2016-03-10 16:33:58 -05002710 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002711 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002712 ssl->ctx->tlsext_servername_arg);
2713 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002714 ret = ssl->initial_ctx->tlsext_servername_callback(
2715 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002716 }
Adam Langley95c29f32014-06-20 12:00:00 -07002717
Adam Langleyfcf25832014-12-18 17:42:32 -08002718 switch (ret) {
2719 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002720 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002721 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002722
Adam Langleyfcf25832014-12-18 17:42:32 -08002723 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002724 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002725 return 1;
2726
2727 default:
2728 return 1;
2729 }
2730}
2731
David Benjamin0d56f882015-12-19 17:05:56 -05002732int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002733 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002734 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2735 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002736 return 0;
2737 }
2738
David Benjamin0d56f882015-12-19 17:05:56 -05002739 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002740 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002741 return 0;
2742 }
2743
2744 return 1;
2745}
Adam Langley95c29f32014-06-20 12:00:00 -07002746
David Benjamine3aa1d92015-06-16 15:34:50 -04002747int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002748 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002749 size_t ticket_len, const uint8_t *session_id,
2750 size_t session_id_len) {
2751 int ret = 1; /* Most errors are non-fatal. */
2752 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2753 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002754
David Benjamine3aa1d92015-06-16 15:34:50 -04002755 HMAC_CTX hmac_ctx;
2756 HMAC_CTX_init(&hmac_ctx);
2757 EVP_CIPHER_CTX cipher_ctx;
2758 EVP_CIPHER_CTX_init(&cipher_ctx);
2759
David Benjaminef1b0092015-11-21 14:05:44 -05002760 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002761 *out_session = NULL;
2762
2763 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2764 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002765 }
2766
David Benjaminadcc3952015-04-26 13:07:57 -04002767 /* Ensure there is room for the key name and the largest IV
2768 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2769 * the maximum IV length should be well under the minimum size for the
2770 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002771 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2772 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002773 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002774 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002775
David Benjamine3aa1d92015-06-16 15:34:50 -04002776 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002777 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2778 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2779 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002780 if (cb_ret < 0) {
2781 ret = 0;
2782 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002783 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002784 if (cb_ret == 0) {
2785 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002786 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002787 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002788 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002789 }
2790 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002791 /* Check the key name matches. */
2792 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2793 SSL_TICKET_KEY_NAME_LEN) != 0) {
2794 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002795 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002796 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2797 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002798 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002799 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2800 ssl_ctx->tlsext_tick_aes_key, iv)) {
2801 ret = 0;
2802 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002803 }
2804 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002805 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002806
David Benjamine3aa1d92015-06-16 15:34:50 -04002807 /* Check the MAC at the end of the ticket. */
2808 uint8_t mac[EVP_MAX_MD_SIZE];
2809 size_t mac_len = HMAC_size(&hmac_ctx);
2810 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002811 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002812 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002813 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002814 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2815 HMAC_Final(&hmac_ctx, mac, NULL);
2816 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2817 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002818 }
2819
David Benjamine3aa1d92015-06-16 15:34:50 -04002820 /* Decrypt the session data. */
2821 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2822 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2823 mac_len;
2824 plaintext = OPENSSL_malloc(ciphertext_len);
2825 if (plaintext == NULL) {
2826 ret = 0;
2827 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002828 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002829 if (ciphertext_len >= INT_MAX) {
2830 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002831 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002832 int len1, len2;
2833 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2834 (int)ciphertext_len) ||
2835 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2836 ERR_clear_error(); /* Don't leave an error on the queue. */
2837 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002838 }
2839
David Benjamine3aa1d92015-06-16 15:34:50 -04002840 /* Decode the session. */
2841 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2842 if (session == NULL) {
2843 ERR_clear_error(); /* Don't leave an error on the queue. */
2844 goto done;
2845 }
2846
2847 /* Copy the client's session ID into the new session, to denote the ticket has
2848 * been accepted. */
2849 memcpy(session->session_id, session_id, session_id_len);
2850 session->session_id_length = session_id_len;
2851
2852 *out_session = session;
2853
2854done:
2855 OPENSSL_free(plaintext);
2856 HMAC_CTX_cleanup(&hmac_ctx);
2857 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2858 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002859}
Adam Langley95c29f32014-06-20 12:00:00 -07002860
Steven Valdez0d62f262015-09-04 12:41:04 -04002861int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002862 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05002863 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002864 return 1;
2865 }
David Benjamincd996942014-07-20 16:23:51 -04002866
Steven Valdez0d62f262015-09-04 12:41:04 -04002867 CERT *const cert = ssl->cert;
2868 OPENSSL_free(cert->peer_sigalgs);
2869 cert->peer_sigalgs = NULL;
2870 cert->peer_sigalgslen = 0;
2871
2872 size_t num_sigalgs = CBS_len(in_sigalgs);
2873
2874 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002875 return 0;
2876 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002877 num_sigalgs /= 2;
2878
2879 /* supported_signature_algorithms in the certificate request is
2880 * allowed to be empty. */
2881 if (num_sigalgs == 0) {
2882 return 1;
2883 }
2884
Steven Valdez02563852016-06-23 13:33:05 -04002885 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
2886 * and we just divided |num_sigalgs| by two. */
2887 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
Steven Valdez0d62f262015-09-04 12:41:04 -04002888 if (cert->peer_sigalgs == NULL) {
2889 return 0;
2890 }
2891 cert->peer_sigalgslen = num_sigalgs;
2892
2893 CBS sigalgs;
2894 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2895
2896 size_t i;
2897 for (i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04002898 if (!CBS_get_u16(&sigalgs, &cert->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04002899 return 0;
2900 }
2901 }
Adam Langley95c29f32014-06-20 12:00:00 -07002902
Adam Langleyfcf25832014-12-18 17:42:32 -08002903 return 1;
2904}
David Benjaminec2f27d2014-11-13 19:17:25 -05002905
David Benjaminea9a0d52016-07-08 15:52:59 -07002906int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04002907 CERT *cert = ssl->cert;
Steven Valdez0d62f262015-09-04 12:41:04 -04002908 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002909
Steven Valdezf0451ca2016-06-29 13:16:27 -04002910 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
2911 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
2912 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04002913 int type = ssl_private_key_type(ssl);
2914 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07002915 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04002916 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002917 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04002918 if (ssl_is_ecdsa_key_type(type)) {
2919 *out = SSL_SIGN_ECDSA_SHA1;
2920 return 1;
2921 }
2922 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
2923 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002924 }
2925
Steven Valdezeff1e8d2016-07-06 14:24:47 -04002926 const uint16_t *sigalgs;
2927 size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07002928 if (cert->sigalgs != NULL) {
2929 sigalgs = cert->sigalgs;
2930 sigalgs_len = cert->sigalgs_len;
Steven Valdez0d62f262015-09-04 12:41:04 -04002931 }
2932
David Benjaminea9a0d52016-07-08 15:52:59 -07002933 const uint16_t *peer_sigalgs = cert->peer_sigalgs;
2934 size_t peer_sigalgs_len = cert->peer_sigalgslen;
David Benjamin51dd7d62016-07-08 16:07:01 -07002935 if (peer_sigalgs_len == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07002936 /* If the client didn't specify any signature_algorithms extension then
2937 * we can assume that it supports SHA1. See
2938 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
2939 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
2940 SSL_SIGN_ECDSA_SHA1};
2941 peer_sigalgs = kDefaultPeerAlgorithms;
2942 peer_sigalgs_len =
2943 sizeof(kDefaultPeerAlgorithms) / sizeof(kDefaultPeerAlgorithms);
2944 }
2945
David Benjamind246b812016-07-08 15:07:02 -07002946 for (i = 0; i < sigalgs_len; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07002947 uint16_t sigalg = sigalgs[i];
2948 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
2949 * negotiated. */
2950 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
2951 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
2952 continue;
2953 }
2954
David Benjaminea9a0d52016-07-08 15:52:59 -07002955 for (j = 0; j < peer_sigalgs_len; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07002956 if (sigalg == peer_sigalgs[j]) {
2957 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07002958 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002959 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002960 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002961 }
Adam Langley95c29f32014-06-20 12:00:00 -07002962
David Benjaminea9a0d52016-07-08 15:52:59 -07002963 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
2964 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002965}
Adam Langley95c29f32014-06-20 12:00:00 -07002966
David Benjamind6a4ae92015-08-06 11:10:51 -04002967int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2968 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002969 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002970
2971 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002972 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2973 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002974 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002975
David Benjamind6a4ae92015-08-06 11:10:51 -04002976 static const char kClientIDMagic[] = "TLS Channel ID signature";
2977 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2978
2979 if (ssl->hit) {
2980 static const char kResumptionMagic[] = "Resumption";
2981 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2982 if (ssl->session->original_handshake_hash_len == 0) {
2983 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2984 goto err;
2985 }
2986 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2987 ssl->session->original_handshake_hash_len);
2988 }
2989
2990 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2991 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2992 sizeof(handshake_hash));
2993 if (handshake_hash_len < 0) {
2994 goto err;
2995 }
2996 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2997 unsigned len_u;
2998 EVP_DigestFinal_ex(&ctx, out, &len_u);
2999 *out_len = len_u;
3000
3001 ret = 1;
3002
3003err:
3004 EVP_MD_CTX_cleanup(&ctx);
3005 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003006}
Adam Langley1258b6a2014-06-20 12:00:00 -07003007
3008/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin0d56f882015-12-19 17:05:56 -05003009 * hashes in |ssl->session| so that Channel ID resumptions can sign that
3010 * data. */
3011int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003012 int digest_len;
3013 /* This function should never be called for a resumed session because the
3014 * handshake hashes that we wish to record are for the original, full
3015 * handshake. */
David Benjamin0d56f882015-12-19 17:05:56 -05003016 if (ssl->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003017 return -1;
3018 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003019
Adam Langleyfcf25832014-12-18 17:42:32 -08003020 digest_len =
David Benjamin0d56f882015-12-19 17:05:56 -05003021 tls1_handshake_digest(ssl, ssl->session->original_handshake_hash,
3022 sizeof(ssl->session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003023 if (digest_len < 0) {
3024 return -1;
3025 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003026
David Benjamin0d56f882015-12-19 17:05:56 -05003027 ssl->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003028
Adam Langleyfcf25832014-12-18 17:42:32 -08003029 return 1;
3030}