blob: 7549240d6fa673bc1f6a264c08fbc1324f5464b5 [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
Adam Langleyfcf25832014-12-18 17:42:32 -0800205char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
206 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400207
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700209
Adam Langleyfcf25832014-12-18 17:42:32 -0800210 if (/* Skip client version. */
211 !CBS_skip(&client_hello, 2) ||
212 /* Skip client nonce. */
213 !CBS_skip(&client_hello, 32) ||
214 /* Extract session_id. */
215 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
216 return 0;
217 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700218
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 ctx->session_id = CBS_data(&session_id);
220 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700221
Adam Langleyfcf25832014-12-18 17:42:32 -0800222 /* Skip past DTLS cookie */
223 if (SSL_IS_DTLS(ctx->ssl)) {
224 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
227 return 0;
228 }
229 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700230
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 /* Extract cipher_suites. */
232 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
233 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
234 return 0;
235 }
236 ctx->cipher_suites = CBS_data(&cipher_suites);
237 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700238
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 /* Extract compression_methods. */
240 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
241 CBS_len(&compression_methods) < 1) {
242 return 0;
243 }
244 ctx->compression_methods = CBS_data(&compression_methods);
245 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700246
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 /* If the ClientHello ends here then it's valid, but doesn't have any
248 * extensions. (E.g. SSLv3.) */
249 if (CBS_len(&client_hello) == 0) {
250 ctx->extensions = NULL;
251 ctx->extensions_len = 0;
252 return 1;
253 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700254
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 /* Extract extensions and check it is valid. */
256 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
257 !tls1_check_duplicate_extensions(&extensions) ||
258 CBS_len(&client_hello) != 0) {
259 return 0;
260 }
261 ctx->extensions = CBS_data(&extensions);
262 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 return 1;
265}
Adam Langleydc9b1412014-06-20 12:00:00 -0700266
David Benjamind4c2bce2015-10-17 12:28:18 -0400267int SSL_early_callback_ctx_extension_get(
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
269 const uint8_t **out_data, size_t *out_len) {
270 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700271
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700273
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 while (CBS_len(&extensions) != 0) {
275 uint16_t type;
276 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400277
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 /* Decode the next extension. */
279 if (!CBS_get_u16(&extensions, &type) ||
280 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
281 return 0;
282 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 if (type == extension_type) {
285 *out_data = CBS_data(&extension);
286 *out_len = CBS_len(&extension);
287 return 1;
288 }
289 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700290
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 return 0;
292}
Adam Langley95c29f32014-06-20 12:00:00 -0700293
Steven Valdezce902a92016-05-17 11:47:53 -0400294static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400295 SSL_CURVE_X25519,
296 SSL_CURVE_SECP256R1,
297 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700298#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin9e68f192016-06-30 14:55:33 -0400299 SSL_CURVE_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700300#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800301};
Adam Langley95c29f32014-06-20 12:00:00 -0700302
Steven Valdez5440fe02016-07-18 12:40:30 -0400303void tls1_get_grouplist(SSL *ssl, int get_peer_groups,
304 const uint16_t **out_group_ids,
305 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400306 if (get_peer_groups) {
307 /* Only clients send a supported group list, so this function is only
308 * called on the server. */
David Benjamin0d56f882015-12-19 17:05:56 -0500309 assert(ssl->server);
Steven Valdezce902a92016-05-17 11:47:53 -0400310 *out_group_ids = ssl->s3->tmp.peer_supported_group_list;
311 *out_group_ids_len = ssl->s3->tmp.peer_supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800312 return;
313 }
Adam Langley95c29f32014-06-20 12:00:00 -0700314
Steven Valdezce902a92016-05-17 11:47:53 -0400315 *out_group_ids = ssl->supported_group_list;
316 *out_group_ids_len = ssl->supported_group_list_len;
317 if (!*out_group_ids) {
318 *out_group_ids = kDefaultGroups;
319 *out_group_ids_len = sizeof(kDefaultGroups) / sizeof(kDefaultGroups[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 }
321}
David Benjamined439582014-07-14 19:13:02 -0400322
Steven Valdezce902a92016-05-17 11:47:53 -0400323int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) {
324 const uint16_t *groups, *peer_groups, *pref, *supp;
325 size_t groups_len, peer_groups_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400326
Adam Langleyfcf25832014-12-18 17:42:32 -0800327 /* Can't do anything on client side */
David Benjamin4298d772015-12-19 00:18:25 -0500328 if (ssl->server == 0) {
329 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 }
331
Steven Valdezce902a92016-05-17 11:47:53 -0400332 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
333 tls1_get_grouplist(ssl, 1 /* peer groups */, &peer_groups, &peer_groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800334
Steven Valdezce902a92016-05-17 11:47:53 -0400335 if (peer_groups_len == 0) {
336 /* Clients are not required to send a supported_groups extension. In this
337 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin4298d772015-12-19 00:18:25 -0500338 * section 4, paragraph 3.
339 *
340 * However, in the interests of compatibility, we will skip ECDH if the
341 * client didn't send an extension because we can't be sure that they'll
Steven Valdezce902a92016-05-17 11:47:53 -0400342 * support our favoured group. */
David Benjamin4298d772015-12-19 00:18:25 -0500343 return 0;
David Benjamin55a43642015-04-20 14:45:55 -0400344 }
345
David Benjamin4298d772015-12-19 00:18:25 -0500346 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400347 pref = groups;
348 pref_len = groups_len;
349 supp = peer_groups;
350 supp_len = peer_groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400351 } else {
Steven Valdezce902a92016-05-17 11:47:53 -0400352 pref = peer_groups;
353 pref_len = peer_groups_len;
354 supp = groups;
355 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400356 }
357
358 for (i = 0; i < pref_len; i++) {
359 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800360 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400361 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500362 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 }
364 }
365 }
366
David Benjamin4298d772015-12-19 00:18:25 -0500367 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800368}
Adam Langley95c29f32014-06-20 12:00:00 -0700369
Steven Valdezce902a92016-05-17 11:47:53 -0400370int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400372 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 size_t i;
374
Steven Valdezce902a92016-05-17 11:47:53 -0400375 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
376 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 return 0;
378 }
379
380 for (i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400381 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
382 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 return 0;
384 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800385 }
386
Steven Valdezce902a92016-05-17 11:47:53 -0400387 OPENSSL_free(*out_group_ids);
388 *out_group_ids = group_ids;
389 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800390
391 return 1;
392}
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Steven Valdezce902a92016-05-17 11:47:53 -0400394/* tls1_curve_params_from_ec_key sets |*out_group_id| and |*out_comp_id| to the
395 * TLS group ID and point format, respectively, for |ec|. It returns one on
David Benjamin072334d2014-07-13 16:24:27 -0400396 * success and zero on failure. */
Steven Valdezce902a92016-05-17 11:47:53 -0400397static int tls1_curve_params_from_ec_key(uint16_t *out_group_id,
Adam Langleyfcf25832014-12-18 17:42:32 -0800398 uint8_t *out_comp_id, EC_KEY *ec) {
399 int nid;
400 uint16_t id;
401 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 if (ec == NULL) {
404 return 0;
405 }
Adam Langley95c29f32014-06-20 12:00:00 -0700406
Adam Langleyfcf25832014-12-18 17:42:32 -0800407 grp = EC_KEY_get0_group(ec);
408 if (grp == NULL) {
409 return 0;
410 }
David Benjamin072334d2014-07-13 16:24:27 -0400411
Steven Valdezce902a92016-05-17 11:47:53 -0400412 /* Determine group ID */
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 nid = EC_GROUP_get_curve_name(grp);
Steven Valdezce902a92016-05-17 11:47:53 -0400414 if (!ssl_nid_to_group_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 return 0;
416 }
David Benjamin072334d2014-07-13 16:24:27 -0400417
Steven Valdezce902a92016-05-17 11:47:53 -0400418 /* Set the named group ID. Arbitrary explicit groups are not supported. */
419 *out_group_id = id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800420
421 if (out_comp_id) {
422 if (EC_KEY_get0_public_key(ec) == NULL) {
423 return 0;
424 }
425 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
426 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
427 } else {
428 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
429 }
430 }
431
432 return 1;
433}
David Benjamin072334d2014-07-13 16:24:27 -0400434
Steven Valdezce902a92016-05-17 11:47:53 -0400435/* tls1_check_group_id returns one if |group_id| is consistent with both our
436 * and the peer's group preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400437 * preferences are checked; the peer (the server) does not send preferences. */
Steven Valdezce902a92016-05-17 11:47:53 -0400438int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
439 const uint16_t *groups;
440 size_t groups_len, i, get_peer_groups;
David Benjamin42e9a772014-09-02 23:18:44 -0400441
Adam Langleyfcf25832014-12-18 17:42:32 -0800442 /* Check against our list, then the peer's list. */
Steven Valdezce902a92016-05-17 11:47:53 -0400443 for (get_peer_groups = 0; get_peer_groups <= 1; get_peer_groups++) {
444 if (get_peer_groups && !ssl->server) {
David Benjamin55a43642015-04-20 14:45:55 -0400445 /* Servers do not present a preference list so, if we are a client, only
446 * check our list. */
447 continue;
448 }
449
Steven Valdezce902a92016-05-17 11:47:53 -0400450 tls1_get_grouplist(ssl, get_peer_groups, &groups, &groups_len);
451 if (get_peer_groups && groups_len == 0) {
452 /* Clients are not required to send a supported_groups extension. In this
453 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin55a43642015-04-20 14:45:55 -0400454 * section 4, paragraph 3. */
455 continue;
456 }
Steven Valdezce902a92016-05-17 11:47:53 -0400457 for (i = 0; i < groups_len; i++) {
458 if (groups[i] == group_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800459 break;
460 }
461 }
Adam Langley95c29f32014-06-20 12:00:00 -0700462
Steven Valdezce902a92016-05-17 11:47:53 -0400463 if (i == groups_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 return 0;
465 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800466 }
David Benjamin033e5f42014-11-13 18:47:41 -0500467
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 return 1;
469}
David Benjamin033e5f42014-11-13 18:47:41 -0500470
David Benjamin0d56f882015-12-19 17:05:56 -0500471int tls1_check_ec_cert(SSL *ssl, X509 *x) {
David Benjamin75ea5bb2016-07-08 17:43:29 -0700472 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
473 /* In TLS 1.3, the ECDSA curve is negotiated via signature algorithms. */
474 return 1;
475 }
476
Adam Langleyfcf25832014-12-18 17:42:32 -0800477 EVP_PKEY *pkey = X509_get_pubkey(x);
David Benjamin75ea5bb2016-07-08 17:43:29 -0700478 if (pkey == NULL) {
479 return 0;
480 }
481
482 int ret = 0;
Steven Valdezce902a92016-05-17 11:47:53 -0400483 uint16_t group_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800484 uint8_t comp_id;
David Benjamin758d1272015-11-20 17:47:25 -0500485 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
486 if (ec_key == NULL ||
Steven Valdezce902a92016-05-17 11:47:53 -0400487 !tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) ||
488 !tls1_check_group_id(ssl, group_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400489 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800490 goto done;
491 }
492
493 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500494
495done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400496 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800497 return ret;
498}
David Benjamin42e9a772014-09-02 23:18:44 -0400499
Adam Langley95c29f32014-06-20 12:00:00 -0700500/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700502
Steven Valdez02563852016-06-23 13:33:05 -0400503static const uint16_t kDefaultSignatureAlgorithms[] = {
504 SSL_SIGN_RSA_PKCS1_SHA512,
505 SSL_SIGN_ECDSA_SECP521R1_SHA512,
Adam Langley95c29f32014-06-20 12:00:00 -0700506
Steven Valdez02563852016-06-23 13:33:05 -0400507 SSL_SIGN_RSA_PKCS1_SHA384,
508 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Adam Langley95c29f32014-06-20 12:00:00 -0700509
Steven Valdez02563852016-06-23 13:33:05 -0400510 SSL_SIGN_RSA_PKCS1_SHA256,
511 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Adam Langley95c29f32014-06-20 12:00:00 -0700512
Steven Valdez02563852016-06-23 13:33:05 -0400513 SSL_SIGN_RSA_PKCS1_SHA1,
514 SSL_SIGN_ECDSA_SHA1,
Adam Langley95c29f32014-06-20 12:00:00 -0700515};
David Benjamin05da6e12014-07-12 20:42:55 -0400516
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400517static const uint16_t kDefaultTLS13SignatureAlgorithms[] = {
518 SSL_SIGN_RSA_PSS_SHA512,
519 SSL_SIGN_RSA_PKCS1_SHA512,
520 SSL_SIGN_ECDSA_SECP521R1_SHA512,
521
522 SSL_SIGN_RSA_PSS_SHA384,
523 SSL_SIGN_RSA_PKCS1_SHA384,
524 SSL_SIGN_ECDSA_SECP384R1_SHA384,
525
526 SSL_SIGN_RSA_PSS_SHA256,
527 SSL_SIGN_RSA_PKCS1_SHA256,
528 SSL_SIGN_ECDSA_SECP256R1_SHA256,
529
530 SSL_SIGN_RSA_PKCS1_SHA1,
531 SSL_SIGN_ECDSA_SHA1,
532};
533
Steven Valdez02563852016-06-23 13:33:05 -0400534size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400535 uint16_t version;
536 if (ssl->s3->have_version) {
537 version = ssl3_protocol_version(ssl);
538 } else {
539 version = ssl->method->version_from_wire(ssl->client_version);
540 }
541
542 if (version >= TLS1_3_VERSION) {
543 *psigs = kDefaultTLS13SignatureAlgorithms;
544 return sizeof(kDefaultTLS13SignatureAlgorithms) /
545 sizeof(kDefaultTLS13SignatureAlgorithms[0]);
546 }
547
Steven Valdez02563852016-06-23 13:33:05 -0400548 *psigs = kDefaultSignatureAlgorithms;
549 return sizeof(kDefaultSignatureAlgorithms) /
550 sizeof(kDefaultSignatureAlgorithms[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800551}
Adam Langley95c29f32014-06-20 12:00:00 -0700552
David Benjamin887c3002016-07-08 16:15:32 -0700553int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
Steven Valdez02563852016-06-23 13:33:05 -0400554 const uint16_t *sent_sigs;
555 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800556
Adam Langleyfcf25832014-12-18 17:42:32 -0800557 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500558 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400559 for (i = 0; i < sent_sigslen; i++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400560 if (sigalg == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 break;
562 }
563 }
564
David Benjamin788be4a2015-10-30 17:50:57 -0400565 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400566 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800567 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
568 return 0;
569 }
570
Adam Langleyfcf25832014-12-18 17:42:32 -0800571 return 1;
572}
573
574/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
575 * supported or doesn't appear in supported signature algorithms. Unlike
576 * ssl_cipher_get_disabled this applies to a specific session and not global
577 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500578void ssl_set_client_disabled(SSL *ssl) {
579 CERT *c = ssl->cert;
Steven Valdez02563852016-06-23 13:33:05 -0400580 const uint16_t *sigalgs;
Adam Langleyfcf25832014-12-18 17:42:32 -0800581 size_t i, sigalgslen;
582 int have_rsa = 0, have_ecdsa = 0;
583 c->mask_a = 0;
584 c->mask_k = 0;
585
Adam Langleyfcf25832014-12-18 17:42:32 -0800586 /* Now go through all signature algorithms seeing if we support any for RSA,
587 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500588 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -0400589 for (i = 0; i < sigalgslen; i++) {
590 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400591 case SSL_SIGN_RSA_PSS_SHA512:
592 case SSL_SIGN_RSA_PSS_SHA384:
593 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400594 case SSL_SIGN_RSA_PKCS1_SHA512:
595 case SSL_SIGN_RSA_PKCS1_SHA384:
596 case SSL_SIGN_RSA_PKCS1_SHA256:
597 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800598 have_rsa = 1;
599 break;
600
Steven Valdez02563852016-06-23 13:33:05 -0400601 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
602 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
603 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
604 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800605 have_ecdsa = 1;
606 break;
607 }
608 }
609
610 /* Disable auth if we don't include any appropriate signature algorithms. */
611 if (!have_rsa) {
612 c->mask_a |= SSL_aRSA;
613 }
614 if (!have_ecdsa) {
615 c->mask_a |= SSL_aECDSA;
616 }
617
618 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500619 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800620 c->mask_a |= SSL_aPSK;
621 c->mask_k |= SSL_kPSK;
622 }
623}
Adam Langley95c29f32014-06-20 12:00:00 -0700624
Adam Langley614c66a2015-06-12 15:26:58 -0700625/* tls_extension represents a TLS extension that is handled internally. The
626 * |init| function is called for each handshake, before any other functions of
627 * the extension. Then the add and parse callbacks are called as needed.
628 *
629 * The parse callbacks receive a |CBS| that contains the contents of the
630 * extension (i.e. not including the type and length bytes). If an extension is
631 * not received then the parse callbacks will be called with a NULL CBS so that
632 * they can do any processing needed to handle the absence of an extension.
633 *
634 * The add callbacks receive a |CBB| to which the extension can be appended but
635 * the function is responsible for appending the type and length bytes too.
636 *
637 * All callbacks return one for success and zero for error. If a parse function
638 * returns zero then a fatal alert with value |*out_alert| will be sent. If
639 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
640struct tls_extension {
641 uint16_t value;
642 void (*init)(SSL *ssl);
643
644 int (*add_clienthello)(SSL *ssl, CBB *out);
645 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
646
647 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
648 int (*add_serverhello)(SSL *ssl, CBB *out);
649};
650
Steven Valdez6b8509a2016-07-12 13:38:32 -0400651static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
652 if (contents != NULL) {
653 /* Servers MUST NOT send this extension. */
654 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
655 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
656 return 0;
657 }
658
659 return 1;
660}
661
662static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
663 /* This extension from the client is handled elsewhere. */
664 return 1;
665}
666
667static int dont_add_serverhello(SSL *ssl, CBB *out) {
668 return 1;
669}
Adam Langley614c66a2015-06-12 15:26:58 -0700670
671/* Server name indication (SNI).
672 *
673 * https://tools.ietf.org/html/rfc6066#section-3. */
674
675static void ext_sni_init(SSL *ssl) {
676 ssl->s3->tmp.should_ack_sni = 0;
677}
678
679static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
680 if (ssl->tlsext_hostname == NULL) {
681 return 1;
682 }
683
684 CBB contents, server_name_list, name;
685 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
686 !CBB_add_u16_length_prefixed(out, &contents) ||
687 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
688 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
689 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
690 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
691 strlen(ssl->tlsext_hostname)) ||
692 !CBB_flush(out)) {
693 return 0;
694 }
695
696 return 1;
697}
698
David Benjamin0d56f882015-12-19 17:05:56 -0500699static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
700 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700701 if (contents == NULL) {
702 return 1;
703 }
704
705 if (CBS_len(contents) != 0) {
706 return 0;
707 }
708
709 assert(ssl->tlsext_hostname != NULL);
710
711 if (!ssl->hit) {
712 assert(ssl->session->tlsext_hostname == NULL);
713 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
714 if (!ssl->session->tlsext_hostname) {
715 *out_alert = SSL_AD_INTERNAL_ERROR;
716 return 0;
717 }
718 }
719
720 return 1;
721}
722
David Benjamin0d56f882015-12-19 17:05:56 -0500723static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
724 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700725 if (contents == NULL) {
726 return 1;
727 }
728
David Benjamin9b611e22016-03-03 08:48:30 -0500729 CBS server_name_list, host_name;
730 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700731 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500732 !CBS_get_u8(&server_name_list, &name_type) ||
733 /* Although the server_name extension was intended to be extensible to
734 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
735 * different name types will cause an error. Further, RFC 4366 originally
736 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
737 * adding new name types is no longer feasible.
738 *
739 * Act as if the extensibility does not exist to simplify parsing. */
740 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
741 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700742 CBS_len(contents) != 0) {
743 return 0;
744 }
745
David Benjamin9b611e22016-03-03 08:48:30 -0500746 if (name_type != TLSEXT_NAMETYPE_host_name ||
747 CBS_len(&host_name) == 0 ||
748 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
749 CBS_contains_zero_byte(&host_name)) {
750 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
751 return 0;
752 }
Adam Langley614c66a2015-06-12 15:26:58 -0700753
David Benjamin9b611e22016-03-03 08:48:30 -0500754 /* TODO(davidben): SNI should be resolved before resumption. We have the
755 * early callback as a replacement, but we should fix the current callback
756 * and avoid the need for |SSL_CTX_set_session_id_context|. */
757 if (!ssl->hit) {
758 assert(ssl->session->tlsext_hostname == NULL);
759
760 /* Copy the hostname as a string. */
761 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
762 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700763 return 0;
764 }
765
David Benjamin9b611e22016-03-03 08:48:30 -0500766 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700767 }
768
769 return 1;
770}
771
772static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
773 if (ssl->hit ||
774 !ssl->s3->tmp.should_ack_sni ||
775 ssl->session->tlsext_hostname == NULL) {
776 return 1;
777 }
778
779 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
780 !CBB_add_u16(out, 0 /* length */)) {
781 return 0;
782 }
783
784 return 1;
785}
786
787
Adam Langley5021b222015-06-12 18:27:58 -0700788/* Renegotiation indication.
789 *
790 * https://tools.ietf.org/html/rfc5746 */
791
792static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
793 CBB contents, prev_finished;
794 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
795 !CBB_add_u16_length_prefixed(out, &contents) ||
796 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
797 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
798 ssl->s3->previous_client_finished_len) ||
799 !CBB_flush(out)) {
800 return 0;
801 }
802
803 return 1;
804}
805
806static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
807 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400808 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
809 return 0;
810 }
811
David Benjamin3e052de2015-11-25 20:10:31 -0500812 /* Servers may not switch between omitting the extension and supporting it.
813 * See RFC 5746, sections 3.5 and 4.2. */
814 if (ssl->s3->initial_handshake_complete &&
815 (contents != NULL) != ssl->s3->send_connection_binding) {
816 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
817 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
818 return 0;
819 }
820
Adam Langley5021b222015-06-12 18:27:58 -0700821 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500822 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700823 * RI even on initial ServerHello because the client doesn't see any
824 * renegotiation during an attack. However this would mean we could not
825 * connect to any server which doesn't support RI.
826 *
David Benjamine9cddb82015-11-23 14:36:40 -0500827 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
828 * practical terms every client sets it so it's just assumed here. */
829 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700830 }
831
832 const size_t expected_len = ssl->s3->previous_client_finished_len +
833 ssl->s3->previous_server_finished_len;
834
835 /* Check for logic errors */
836 assert(!expected_len || ssl->s3->previous_client_finished_len);
837 assert(!expected_len || ssl->s3->previous_server_finished_len);
838
839 /* Parse out the extension contents. */
840 CBS renegotiated_connection;
841 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
842 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400843 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700844 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
845 return 0;
846 }
847
848 /* Check that the extension matches. */
849 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400850 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700851 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
852 return 0;
853 }
854
855 const uint8_t *d = CBS_data(&renegotiated_connection);
856 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
857 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400858 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700859 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
860 return 0;
861 }
862 d += ssl->s3->previous_client_finished_len;
863
864 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
865 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400866 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700867 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
868 return 0;
869 }
870 ssl->s3->send_connection_binding = 1;
871
872 return 1;
873}
874
875static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
876 CBS *contents) {
877 /* Renegotiation isn't supported as a server so this function should never be
878 * called after the initial handshake. */
879 assert(!ssl->s3->initial_handshake_complete);
880
Steven Valdez143e8b32016-07-11 13:19:03 -0400881 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
882 return 1;
883 }
884
Adam Langley5021b222015-06-12 18:27:58 -0700885 CBS fake_contents;
886 static const uint8_t kFakeExtension[] = {0};
887
888 if (contents == NULL) {
889 if (ssl->s3->send_connection_binding) {
890 /* The renegotiation SCSV was received so pretend that we received a
891 * renegotiation extension. */
892 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
893 contents = &fake_contents;
894 /* We require that the renegotiation extension is at index zero of
895 * kExtensions. */
896 ssl->s3->tmp.extensions.received |= (1u << 0);
897 } else {
898 return 1;
899 }
900 }
901
902 CBS renegotiated_connection;
903
904 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
905 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400906 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700907 return 0;
908 }
909
910 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500911 if (!CBS_mem_equal(&renegotiated_connection,
912 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700913 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400914 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700915 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
916 return 0;
917 }
918
919 ssl->s3->send_connection_binding = 1;
920
921 return 1;
922}
923
924static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400925 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
926 return 1;
927 }
928
Adam Langley5021b222015-06-12 18:27:58 -0700929 CBB contents, prev_finished;
930 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
931 !CBB_add_u16_length_prefixed(out, &contents) ||
932 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
933 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
934 ssl->s3->previous_client_finished_len) ||
935 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
936 ssl->s3->previous_server_finished_len) ||
937 !CBB_flush(out)) {
938 return 0;
939 }
940
941 return 1;
942}
943
Adam Langley0a056712015-07-01 15:03:33 -0700944
945/* Extended Master Secret.
946 *
David Benjamin43946d42016-02-01 08:42:19 -0500947 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700948
949static void ext_ems_init(SSL *ssl) {
950 ssl->s3->tmp.extended_master_secret = 0;
951}
952
953static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
954 if (ssl->version == SSL3_VERSION) {
955 return 1;
956 }
957
958 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
959 !CBB_add_u16(out, 0 /* length */)) {
960 return 0;
961 }
962
963 return 1;
964}
965
966static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
967 CBS *contents) {
968 if (contents == NULL) {
969 return 1;
970 }
971
Steven Valdez143e8b32016-07-11 13:19:03 -0400972 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
973 ssl->version == SSL3_VERSION) {
974 return 0;
975 }
976
977 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700978 return 0;
979 }
980
981 ssl->s3->tmp.extended_master_secret = 1;
982 return 1;
983}
984
David Benjamin0d56f882015-12-19 17:05:56 -0500985static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
986 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400987 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
988 ssl->version == SSL3_VERSION) {
989 return 1;
990 }
991
992 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700993 return 1;
994 }
995
996 if (CBS_len(contents) != 0) {
997 return 0;
998 }
999
1000 ssl->s3->tmp.extended_master_secret = 1;
1001 return 1;
1002}
1003
1004static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1005 if (!ssl->s3->tmp.extended_master_secret) {
1006 return 1;
1007 }
1008
1009 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1010 !CBB_add_u16(out, 0 /* length */)) {
1011 return 0;
1012 }
1013
1014 return 1;
1015}
1016
Adam Langley9b05bc52015-07-01 15:25:33 -07001017
1018/* Session tickets.
1019 *
1020 * https://tools.ietf.org/html/rfc5077 */
1021
1022static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1023 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1024 return 1;
1025 }
1026
1027 const uint8_t *ticket_data = NULL;
1028 int ticket_len = 0;
1029
1030 /* Renegotiation does not participate in session resumption. However, still
1031 * advertise the extension to avoid potentially breaking servers which carry
1032 * over the state from the previous handshake, such as OpenSSL servers
1033 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1034 if (!ssl->s3->initial_handshake_complete &&
1035 ssl->session != NULL &&
1036 ssl->session->tlsext_tick != NULL) {
1037 ticket_data = ssl->session->tlsext_tick;
1038 ticket_len = ssl->session->tlsext_ticklen;
1039 }
1040
1041 CBB ticket;
1042 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1043 !CBB_add_u16_length_prefixed(out, &ticket) ||
1044 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1045 !CBB_flush(out)) {
1046 return 0;
1047 }
1048
1049 return 1;
1050}
1051
1052static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1053 CBS *contents) {
1054 ssl->tlsext_ticket_expected = 0;
1055
1056 if (contents == NULL) {
1057 return 1;
1058 }
1059
Steven Valdez143e8b32016-07-11 13:19:03 -04001060 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1061 return 0;
1062 }
1063
Adam Langley9b05bc52015-07-01 15:25:33 -07001064 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1065 * this function should never be called, even if the server tries to send the
1066 * extension. */
1067 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1068
1069 if (CBS_len(contents) != 0) {
1070 return 0;
1071 }
1072
1073 ssl->tlsext_ticket_expected = 1;
1074 return 1;
1075}
1076
Adam Langley9b05bc52015-07-01 15:25:33 -07001077static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1078 if (!ssl->tlsext_ticket_expected) {
1079 return 1;
1080 }
1081
1082 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1083 * true. */
1084 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1085
1086 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1087 !CBB_add_u16(out, 0 /* length */)) {
1088 return 0;
1089 }
1090
1091 return 1;
1092}
1093
1094
Adam Langley2e857bd2015-07-01 16:09:19 -07001095/* Signature Algorithms.
1096 *
1097 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1098
1099static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjaminb6a0a512016-06-21 10:33:21 -04001100 if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001101 return 1;
1102 }
1103
Steven Valdez02563852016-06-23 13:33:05 -04001104 const uint16_t *sigalgs_data;
Adam Langley2e857bd2015-07-01 16:09:19 -07001105 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1106
1107 CBB contents, sigalgs;
1108 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1109 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez02563852016-06-23 13:33:05 -04001110 !CBB_add_u16_length_prefixed(&contents, &sigalgs)) {
1111 return 0;
1112 }
1113
1114 size_t i;
1115 for (i = 0; i < sigalgs_len; i++) {
1116 if (!CBB_add_u16(&sigalgs, sigalgs_data[i])) {
1117 return 0;
1118 }
1119 }
1120
1121 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001122 return 0;
1123 }
1124
1125 return 1;
1126}
1127
Adam Langley2e857bd2015-07-01 16:09:19 -07001128static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1129 CBS *contents) {
1130 OPENSSL_free(ssl->cert->peer_sigalgs);
1131 ssl->cert->peer_sigalgs = NULL;
1132 ssl->cert->peer_sigalgslen = 0;
1133
Adam Langley2e857bd2015-07-01 16:09:19 -07001134 if (contents == NULL) {
1135 return 1;
1136 }
1137
1138 CBS supported_signature_algorithms;
1139 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001140 CBS_len(contents) != 0 ||
1141 CBS_len(&supported_signature_algorithms) == 0 ||
1142 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001143 return 0;
1144 }
1145
1146 return 1;
1147}
1148
Adam Langley2e857bd2015-07-01 16:09:19 -07001149
Adam Langleybb0bd042015-07-01 16:21:03 -07001150/* OCSP Stapling.
1151 *
1152 * https://tools.ietf.org/html/rfc6066#section-8 */
1153
1154static void ext_ocsp_init(SSL *ssl) {
1155 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001156 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001157}
1158
1159static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1160 if (!ssl->ocsp_stapling_enabled) {
1161 return 1;
1162 }
1163
1164 CBB contents;
1165 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1166 !CBB_add_u16_length_prefixed(out, &contents) ||
1167 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1168 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1169 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1170 !CBB_flush(out)) {
1171 return 0;
1172 }
1173
Adam Langleyce9d85e2016-01-24 15:58:39 -08001174 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001175 return 1;
1176}
1177
1178static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001179 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001180 if (contents == NULL) {
1181 return 1;
1182 }
1183
David Benjamin942f4ed2016-07-16 19:03:49 +03001184 /* OCSP stapling is forbidden on a non-certificate cipher. */
1185 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001186 return 0;
1187 }
1188
David Benjamin942f4ed2016-07-16 19:03:49 +03001189 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1190 if (CBS_len(contents) != 0) {
1191 return 0;
1192 }
1193
1194 ssl->s3->tmp.certificate_status_expected = 1;
1195 return 1;
1196 }
1197
1198 uint8_t status_type;
1199 CBS ocsp_response;
1200 if (!CBS_get_u8(contents, &status_type) ||
1201 status_type != TLSEXT_STATUSTYPE_ocsp ||
1202 !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1203 CBS_len(&ocsp_response) == 0 ||
1204 CBS_len(contents) != 0) {
1205 return 0;
1206 }
1207
1208 if (!CBS_stow(&ocsp_response, &ssl->session->ocsp_response,
1209 &ssl->session->ocsp_response_length)) {
1210 *out_alert = SSL_AD_INTERNAL_ERROR;
1211 return 0;
1212 }
1213
Adam Langleybb0bd042015-07-01 16:21:03 -07001214 return 1;
1215}
1216
1217static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1218 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001219 if (contents == NULL) {
1220 return 1;
1221 }
1222
1223 uint8_t status_type;
1224 if (!CBS_get_u8(contents, &status_type)) {
1225 return 0;
1226 }
1227
1228 /* We cannot decide whether OCSP stapling will occur yet because the correct
1229 * SSL_CTX might not have been selected. */
1230 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1231
Adam Langleybb0bd042015-07-01 16:21:03 -07001232 return 1;
1233}
1234
1235static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001236 if (!ssl->s3->tmp.ocsp_stapling_requested ||
1237 ssl->ctx->ocsp_response_length == 0 ||
1238 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001239 return 1;
1240 }
1241
David Benjamin942f4ed2016-07-16 19:03:49 +03001242 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1243 /* The extension shouldn't be sent when resuming sessions. */
1244 if (ssl->hit) {
1245 return 1;
1246 }
1247
1248 ssl->s3->tmp.certificate_status_expected = 1;
1249
1250 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1251 CBB_add_u16(out, 0 /* length */);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001252 }
1253
David Benjamin942f4ed2016-07-16 19:03:49 +03001254 CBB body, ocsp_response;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001255 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
David Benjamin942f4ed2016-07-16 19:03:49 +03001256 CBB_add_u16_length_prefixed(out, &body) &&
1257 CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1258 CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1259 CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1260 ssl->ctx->ocsp_response_length) &&
1261 CBB_flush(out);
Adam Langleybb0bd042015-07-01 16:21:03 -07001262}
1263
1264
Adam Langley97dfcbf2015-07-01 18:35:20 -07001265/* Next protocol negotiation.
1266 *
1267 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1268
1269static void ext_npn_init(SSL *ssl) {
1270 ssl->s3->next_proto_neg_seen = 0;
1271}
1272
1273static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1274 if (ssl->s3->initial_handshake_complete ||
1275 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001276 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001277 SSL_IS_DTLS(ssl)) {
1278 return 1;
1279 }
1280
1281 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1282 !CBB_add_u16(out, 0 /* length */)) {
1283 return 0;
1284 }
1285
1286 return 1;
1287}
1288
1289static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1290 CBS *contents) {
1291 if (contents == NULL) {
1292 return 1;
1293 }
1294
Steven Valdez143e8b32016-07-11 13:19:03 -04001295 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1296 return 0;
1297 }
1298
Adam Langley97dfcbf2015-07-01 18:35:20 -07001299 /* If any of these are false then we should never have sent the NPN
1300 * extension in the ClientHello and thus this function should never have been
1301 * called. */
1302 assert(!ssl->s3->initial_handshake_complete);
1303 assert(!SSL_IS_DTLS(ssl));
1304 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001305 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001306
David Benjamin76c2efc2015-08-31 14:24:29 -04001307 if (ssl->s3->alpn_selected != NULL) {
1308 /* NPN and ALPN may not be negotiated in the same connection. */
1309 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1310 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1311 return 0;
1312 }
1313
Adam Langley97dfcbf2015-07-01 18:35:20 -07001314 const uint8_t *const orig_contents = CBS_data(contents);
1315 const size_t orig_len = CBS_len(contents);
1316
1317 while (CBS_len(contents) != 0) {
1318 CBS proto;
1319 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1320 CBS_len(&proto) == 0) {
1321 return 0;
1322 }
1323 }
1324
1325 uint8_t *selected;
1326 uint8_t selected_len;
1327 if (ssl->ctx->next_proto_select_cb(
1328 ssl, &selected, &selected_len, orig_contents, orig_len,
1329 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1330 *out_alert = SSL_AD_INTERNAL_ERROR;
1331 return 0;
1332 }
1333
David Benjamin79978df2015-12-25 15:56:49 -05001334 OPENSSL_free(ssl->s3->next_proto_negotiated);
1335 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1336 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001337 *out_alert = SSL_AD_INTERNAL_ERROR;
1338 return 0;
1339 }
1340
David Benjamin79978df2015-12-25 15:56:49 -05001341 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001342 ssl->s3->next_proto_neg_seen = 1;
1343
1344 return 1;
1345}
1346
1347static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1348 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001349 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1350 return 1;
1351 }
1352
Adam Langley97dfcbf2015-07-01 18:35:20 -07001353 if (contents != NULL && CBS_len(contents) != 0) {
1354 return 0;
1355 }
1356
1357 if (contents == NULL ||
1358 ssl->s3->initial_handshake_complete ||
1359 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1360 * afterwards, parsing the ALPN extension will clear
1361 * |next_proto_neg_seen|. */
1362 ssl->s3->alpn_selected != NULL ||
1363 ssl->ctx->next_protos_advertised_cb == NULL ||
1364 SSL_IS_DTLS(ssl)) {
1365 return 1;
1366 }
1367
1368 ssl->s3->next_proto_neg_seen = 1;
1369 return 1;
1370}
1371
1372static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1373 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1374 * parsed. */
1375 if (!ssl->s3->next_proto_neg_seen) {
1376 return 1;
1377 }
1378
1379 const uint8_t *npa;
1380 unsigned npa_len;
1381
1382 if (ssl->ctx->next_protos_advertised_cb(
1383 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1384 SSL_TLSEXT_ERR_OK) {
1385 ssl->s3->next_proto_neg_seen = 0;
1386 return 1;
1387 }
1388
1389 CBB contents;
1390 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1391 !CBB_add_u16_length_prefixed(out, &contents) ||
1392 !CBB_add_bytes(&contents, npa, npa_len) ||
1393 !CBB_flush(out)) {
1394 return 0;
1395 }
1396
1397 return 1;
1398}
1399
1400
Adam Langleyab8d87d2015-07-10 12:21:39 -07001401/* Signed certificate timestamps.
1402 *
1403 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1404
1405static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1406 if (!ssl->signed_cert_timestamps_enabled) {
1407 return 1;
1408 }
1409
1410 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1411 !CBB_add_u16(out, 0 /* length */)) {
1412 return 0;
1413 }
1414
1415 return 1;
1416}
1417
1418static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1419 CBS *contents) {
1420 if (contents == NULL) {
1421 return 1;
1422 }
1423
1424 /* If this is false then we should never have sent the SCT extension in the
1425 * ClientHello and thus this function should never have been called. */
1426 assert(ssl->signed_cert_timestamps_enabled);
1427
1428 if (CBS_len(contents) == 0) {
1429 *out_alert = SSL_AD_DECODE_ERROR;
1430 return 0;
1431 }
1432
1433 /* Session resumption uses the original session information. */
1434 if (!ssl->hit &&
1435 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1436 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1437 *out_alert = SSL_AD_INTERNAL_ERROR;
1438 return 0;
1439 }
1440
1441 return 1;
1442}
1443
1444static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1445 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001446 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001447}
1448
1449static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001450 /* The extension shouldn't be sent when resuming sessions. */
1451 if (ssl->hit ||
1452 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001453 return 1;
1454 }
1455
1456 CBB contents;
1457 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1458 CBB_add_u16_length_prefixed(out, &contents) &&
1459 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1460 ssl->ctx->signed_cert_timestamp_list_length) &&
1461 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001462}
1463
1464
Adam Langleyf18e4532015-07-10 13:39:53 -07001465/* Application-level Protocol Negotiation.
1466 *
1467 * https://tools.ietf.org/html/rfc7301 */
1468
1469static void ext_alpn_init(SSL *ssl) {
1470 OPENSSL_free(ssl->s3->alpn_selected);
1471 ssl->s3->alpn_selected = NULL;
1472}
1473
1474static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1475 if (ssl->alpn_client_proto_list == NULL ||
1476 ssl->s3->initial_handshake_complete) {
1477 return 1;
1478 }
1479
1480 CBB contents, proto_list;
1481 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1482 !CBB_add_u16_length_prefixed(out, &contents) ||
1483 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1484 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1485 ssl->alpn_client_proto_list_len) ||
1486 !CBB_flush(out)) {
1487 return 0;
1488 }
1489
1490 return 1;
1491}
1492
1493static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1494 CBS *contents) {
1495 if (contents == NULL) {
1496 return 1;
1497 }
1498
1499 assert(!ssl->s3->initial_handshake_complete);
1500 assert(ssl->alpn_client_proto_list != NULL);
1501
David Benjamin76c2efc2015-08-31 14:24:29 -04001502 if (ssl->s3->next_proto_neg_seen) {
1503 /* NPN and ALPN may not be negotiated in the same connection. */
1504 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1505 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1506 return 0;
1507 }
1508
Adam Langleyf18e4532015-07-10 13:39:53 -07001509 /* The extension data consists of a ProtocolNameList which must have
1510 * exactly one ProtocolName. Each of these is length-prefixed. */
1511 CBS protocol_name_list, protocol_name;
1512 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1513 CBS_len(contents) != 0 ||
1514 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1515 /* Empty protocol names are forbidden. */
1516 CBS_len(&protocol_name) == 0 ||
1517 CBS_len(&protocol_name_list) != 0) {
1518 return 0;
1519 }
1520
1521 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1522 &ssl->s3->alpn_selected_len)) {
1523 *out_alert = SSL_AD_INTERNAL_ERROR;
1524 return 0;
1525 }
1526
1527 return 1;
1528}
1529
1530static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1531 CBS *contents) {
1532 if (contents == NULL) {
1533 return 1;
1534 }
1535
1536 if (ssl->ctx->alpn_select_cb == NULL ||
1537 ssl->s3->initial_handshake_complete) {
1538 return 1;
1539 }
1540
1541 /* ALPN takes precedence over NPN. */
1542 ssl->s3->next_proto_neg_seen = 0;
1543
1544 CBS protocol_name_list;
1545 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1546 CBS_len(contents) != 0 ||
1547 CBS_len(&protocol_name_list) < 2) {
1548 return 0;
1549 }
1550
1551 /* Validate the protocol list. */
1552 CBS protocol_name_list_copy = protocol_name_list;
1553 while (CBS_len(&protocol_name_list_copy) > 0) {
1554 CBS protocol_name;
1555
1556 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1557 /* Empty protocol names are forbidden. */
1558 CBS_len(&protocol_name) == 0) {
1559 return 0;
1560 }
1561 }
1562
1563 const uint8_t *selected;
1564 uint8_t selected_len;
1565 if (ssl->ctx->alpn_select_cb(
1566 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1567 CBS_len(&protocol_name_list),
1568 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1569 OPENSSL_free(ssl->s3->alpn_selected);
1570 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1571 if (ssl->s3->alpn_selected == NULL) {
1572 *out_alert = SSL_AD_INTERNAL_ERROR;
1573 return 0;
1574 }
1575 ssl->s3->alpn_selected_len = selected_len;
1576 }
1577
1578 return 1;
1579}
1580
1581static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1582 if (ssl->s3->alpn_selected == NULL) {
1583 return 1;
1584 }
1585
1586 CBB contents, proto_list, proto;
1587 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1588 !CBB_add_u16_length_prefixed(out, &contents) ||
1589 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1590 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001591 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1592 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001593 !CBB_flush(out)) {
1594 return 0;
1595 }
1596
1597 return 1;
1598}
1599
1600
Adam Langley49c7af12015-07-10 14:33:46 -07001601/* Channel ID.
1602 *
1603 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1604
1605static void ext_channel_id_init(SSL *ssl) {
1606 ssl->s3->tlsext_channel_id_valid = 0;
1607}
1608
1609static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1610 if (!ssl->tlsext_channel_id_enabled ||
1611 SSL_IS_DTLS(ssl)) {
1612 return 1;
1613 }
1614
1615 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1616 !CBB_add_u16(out, 0 /* length */)) {
1617 return 0;
1618 }
1619
1620 return 1;
1621}
1622
1623static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1624 CBS *contents) {
1625 if (contents == NULL) {
1626 return 1;
1627 }
1628
Steven Valdez143e8b32016-07-11 13:19:03 -04001629 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1630 return 0;
1631 }
1632
Adam Langley49c7af12015-07-10 14:33:46 -07001633 assert(!SSL_IS_DTLS(ssl));
1634 assert(ssl->tlsext_channel_id_enabled);
1635
1636 if (CBS_len(contents) != 0) {
1637 return 0;
1638 }
1639
1640 ssl->s3->tlsext_channel_id_valid = 1;
1641 return 1;
1642}
1643
1644static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1645 CBS *contents) {
1646 if (contents == NULL ||
1647 !ssl->tlsext_channel_id_enabled ||
1648 SSL_IS_DTLS(ssl)) {
1649 return 1;
1650 }
1651
1652 if (CBS_len(contents) != 0) {
1653 return 0;
1654 }
1655
1656 ssl->s3->tlsext_channel_id_valid = 1;
1657 return 1;
1658}
1659
1660static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001661 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1662 return 1;
1663 }
1664
Adam Langley49c7af12015-07-10 14:33:46 -07001665 if (!ssl->s3->tlsext_channel_id_valid) {
1666 return 1;
1667 }
1668
1669 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1670 !CBB_add_u16(out, 0 /* length */)) {
1671 return 0;
1672 }
1673
1674 return 1;
1675}
1676
Adam Langley391250d2015-07-15 19:06:07 -07001677
1678/* Secure Real-time Transport Protocol (SRTP) extension.
1679 *
1680 * https://tools.ietf.org/html/rfc5764 */
1681
Adam Langley391250d2015-07-15 19:06:07 -07001682
1683static void ext_srtp_init(SSL *ssl) {
1684 ssl->srtp_profile = NULL;
1685}
1686
1687static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1688 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1689 if (profiles == NULL) {
1690 return 1;
1691 }
1692 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1693 if (num_profiles == 0) {
1694 return 1;
1695 }
1696
1697 CBB contents, profile_ids;
1698 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1699 !CBB_add_u16_length_prefixed(out, &contents) ||
1700 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1701 return 0;
1702 }
1703
1704 size_t i;
1705 for (i = 0; i < num_profiles; i++) {
1706 if (!CBB_add_u16(&profile_ids,
1707 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1708 return 0;
1709 }
1710 }
1711
1712 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1713 !CBB_flush(out)) {
1714 return 0;
1715 }
1716
1717 return 1;
1718}
1719
1720static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1721 CBS *contents) {
1722 if (contents == NULL) {
1723 return 1;
1724 }
1725
1726 /* The extension consists of a u16-prefixed profile ID list containing a
1727 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1728 *
1729 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1730 CBS profile_ids, srtp_mki;
1731 uint16_t profile_id;
1732 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1733 !CBS_get_u16(&profile_ids, &profile_id) ||
1734 CBS_len(&profile_ids) != 0 ||
1735 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1736 CBS_len(contents) != 0) {
1737 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1738 return 0;
1739 }
1740
1741 if (CBS_len(&srtp_mki) != 0) {
1742 /* Must be no MKI, since we never offer one. */
1743 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1744 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1745 return 0;
1746 }
1747
1748 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1749
1750 /* Check to see if the server gave us something we support (and presumably
1751 * offered). */
1752 size_t i;
1753 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1754 const SRTP_PROTECTION_PROFILE *profile =
1755 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1756
1757 if (profile->id == profile_id) {
1758 ssl->srtp_profile = profile;
1759 return 1;
1760 }
1761 }
1762
1763 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1764 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1765 return 0;
1766}
1767
1768static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1769 CBS *contents) {
1770 if (contents == NULL) {
1771 return 1;
1772 }
1773
1774 CBS profile_ids, srtp_mki;
1775 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1776 CBS_len(&profile_ids) < 2 ||
1777 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1778 CBS_len(contents) != 0) {
1779 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1780 return 0;
1781 }
1782 /* Discard the MKI value for now. */
1783
1784 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1785 SSL_get_srtp_profiles(ssl);
1786
1787 /* Pick the server's most preferred profile. */
1788 size_t i;
1789 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1790 const SRTP_PROTECTION_PROFILE *server_profile =
1791 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1792
1793 CBS profile_ids_tmp;
1794 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1795
1796 while (CBS_len(&profile_ids_tmp) > 0) {
1797 uint16_t profile_id;
1798 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1799 return 0;
1800 }
1801
1802 if (server_profile->id == profile_id) {
1803 ssl->srtp_profile = server_profile;
1804 return 1;
1805 }
1806 }
1807 }
1808
1809 return 1;
1810}
1811
1812static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1813 if (ssl->srtp_profile == NULL) {
1814 return 1;
1815 }
1816
1817 CBB contents, profile_ids;
1818 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1819 !CBB_add_u16_length_prefixed(out, &contents) ||
1820 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1821 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1822 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1823 !CBB_flush(out)) {
1824 return 0;
1825 }
1826
1827 return 1;
1828}
1829
Adam Langleybdd5d662015-07-20 16:19:08 -07001830
1831/* EC point formats.
1832 *
1833 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1834
1835static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1836 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1837 return 0;
1838 }
1839
1840 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1841
1842 size_t i;
1843 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1844 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1845
1846 const uint32_t alg_k = cipher->algorithm_mkey;
1847 const uint32_t alg_a = cipher->algorithm_auth;
1848 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1849 return 1;
1850 }
1851 }
1852
1853 return 0;
1854}
1855
Adam Langleybdd5d662015-07-20 16:19:08 -07001856static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001857 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001858 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1859 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001860 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1861 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001862 !CBB_flush(out)) {
1863 return 0;
1864 }
1865
1866 return 1;
1867}
1868
1869static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1870 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1871 return 1;
1872 }
1873
1874 return ext_ec_point_add_extension(ssl, out);
1875}
1876
1877static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1878 CBS *contents) {
1879 if (contents == NULL) {
1880 return 1;
1881 }
1882
Steven Valdez143e8b32016-07-11 13:19:03 -04001883 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1884 return 0;
1885 }
1886
Adam Langleybdd5d662015-07-20 16:19:08 -07001887 CBS ec_point_format_list;
1888 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1889 CBS_len(contents) != 0) {
1890 return 0;
1891 }
1892
David Benjaminfc059942015-07-30 23:01:59 -04001893 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1894 * point format. */
1895 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1896 CBS_len(&ec_point_format_list)) == NULL) {
1897 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001898 return 0;
1899 }
1900
1901 return 1;
1902}
1903
1904static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1905 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001906 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1907 return 1;
1908 }
1909
Adam Langleybdd5d662015-07-20 16:19:08 -07001910 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1911}
1912
1913static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001914 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1915 return 1;
1916 }
1917
Adam Langleybdd5d662015-07-20 16:19:08 -07001918 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1919 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001920 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001921
1922 if (!using_ecc) {
1923 return 1;
1924 }
1925
1926 return ext_ec_point_add_extension(ssl, out);
1927}
1928
Adam Langley273d49c2015-07-20 16:38:52 -07001929
Steven Valdez143e8b32016-07-11 13:19:03 -04001930/* Draft Version Extension */
1931
1932static int ext_draft_version_add_clienthello(SSL *ssl, CBB *out) {
1933 uint16_t min_version, max_version;
1934 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
Steven Valdez3a287552016-07-19 05:10:50 -04001935 max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001936 return 1;
1937 }
1938
1939 CBB contents;
1940 if (!CBB_add_u16(out, TLSEXT_TYPE_draft_version) ||
1941 !CBB_add_u16_length_prefixed(out, &contents) ||
1942 !CBB_add_u16(&contents, TLS1_3_DRAFT_VERSION)) {
1943 return 0;
1944 }
1945
1946 return CBB_flush(out);
1947}
1948
1949
1950/* Key Share
1951 *
1952 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
1953
1954static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
1955 uint16_t min_version, max_version;
1956 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1957 return 0;
1958 }
1959
1960 if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
1961 return 1;
1962 }
1963
1964 CBB contents, kse_bytes;
1965 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
1966 !CBB_add_u16_length_prefixed(out, &contents) ||
1967 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
1968 return 0;
1969 }
1970
1971 const uint16_t *groups;
1972 size_t groups_len;
Steven Valdez5440fe02016-07-18 12:40:30 -04001973 if (ssl->s3->hs->retry_group) {
1974 /* Append the new key share to the old list. */
1975 if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
1976 ssl->s3->hs->key_share_bytes_len)) {
1977 return 0;
1978 }
1979 OPENSSL_free(ssl->s3->hs->key_share_bytes);
1980 ssl->s3->hs->key_share_bytes = NULL;
1981
1982 groups = &ssl->s3->hs->retry_group;
1983 groups_len = 1;
1984 } else {
1985 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
1986 /* Only send the top two preferred key shares. */
1987 if (groups_len > 2) {
1988 groups_len = 2;
1989 }
1990 }
Steven Valdez143e8b32016-07-11 13:19:03 -04001991
1992 ssl->s3->hs->groups = OPENSSL_malloc(groups_len * sizeof(SSL_ECDH_CTX));
1993 if (ssl->s3->hs->groups == NULL) {
1994 return 0;
1995 }
1996 memset(ssl->s3->hs->groups, 0, groups_len * sizeof(SSL_ECDH_CTX));
1997 ssl->s3->hs->groups_len = groups_len;
1998
1999 for (size_t i = 0; i < groups_len; i++) {
2000 if (!CBB_add_u16(&kse_bytes, groups[i])) {
2001 return 0;
2002 }
2003
2004 CBB key_exchange;
2005 if (!CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2006 !SSL_ECDH_CTX_init(&ssl->s3->hs->groups[i], groups[i]) ||
2007 !SSL_ECDH_CTX_offer(&ssl->s3->hs->groups[i], &key_exchange) ||
2008 !CBB_flush(&kse_bytes)) {
2009 return 0;
2010 }
2011 }
2012
Steven Valdez5440fe02016-07-18 12:40:30 -04002013 if (!ssl->s3->hs->retry_group) {
2014 /* Save the contents of the extension to repeat it in the second
2015 * ClientHello. */
2016 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2017 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2018 CBB_len(&kse_bytes));
2019 if (ssl->s3->hs->key_share_bytes == NULL) {
2020 return 0;
2021 }
2022 }
2023
Steven Valdez143e8b32016-07-11 13:19:03 -04002024 return CBB_flush(out);
2025}
2026
2027int ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2028 size_t *out_secret_len, uint8_t *out_alert,
2029 CBS *contents) {
2030 CBS peer_key;
2031 uint16_t group;
2032 if (!CBS_get_u16(contents, &group) ||
2033 !CBS_get_u16_length_prefixed(contents, &peer_key)) {
2034 *out_alert = SSL_AD_DECODE_ERROR;
2035 return 0;
2036 }
2037
2038 SSL_ECDH_CTX *group_ctx = NULL;
2039 for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) {
2040 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->groups[i]) == group) {
2041 group_ctx = &ssl->s3->hs->groups[i];
2042 break;
2043 }
2044 }
2045
2046 if (group_ctx == NULL) {
2047 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2048 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2049 return 0;
2050 }
2051
2052 if (!SSL_ECDH_CTX_finish(group_ctx, out_secret, out_secret_len, out_alert,
2053 CBS_data(&peer_key), CBS_len(&peer_key))) {
2054 *out_alert = SSL_AD_INTERNAL_ERROR;
2055 return 0;
2056 }
2057
Steven Valdez5440fe02016-07-18 12:40:30 -04002058 ssl_handshake_clear_groups(ssl->s3->hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04002059 return 1;
2060}
2061
Steven Valdez5440fe02016-07-18 12:40:30 -04002062int ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2063 uint8_t **out_secret,
Steven Valdez143e8b32016-07-11 13:19:03 -04002064 size_t *out_secret_len, uint8_t *out_alert,
2065 CBS *contents) {
2066 uint16_t group_id;
2067 CBS key_shares;
2068 if (!tls1_get_shared_group(ssl, &group_id) ||
2069 !CBS_get_u16_length_prefixed(contents, &key_shares)) {
2070 return 0;
2071 }
2072
Steven Valdez5440fe02016-07-18 12:40:30 -04002073 *out_found = 0;
Steven Valdez143e8b32016-07-11 13:19:03 -04002074 while (CBS_len(&key_shares) > 0) {
2075 uint16_t id;
2076 CBS peer_key;
2077 if (!CBS_get_u16(&key_shares, &id) ||
2078 !CBS_get_u16_length_prefixed(&key_shares, &peer_key)) {
2079 return 0;
2080 }
2081
Steven Valdez5440fe02016-07-18 12:40:30 -04002082 if (id != group_id || *out_found) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002083 continue;
2084 }
2085
2086 SSL_ECDH_CTX group;
2087 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2088 CBB public_key;
2089 if (!CBB_init(&public_key, 0) ||
2090 !SSL_ECDH_CTX_init(&group, group_id) ||
2091 !SSL_ECDH_CTX_accept(&group, &public_key, out_secret, out_secret_len,
2092 out_alert, CBS_data(&peer_key),
2093 CBS_len(&peer_key)) ||
2094 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2095 &ssl->s3->hs->public_key_len)) {
2096 SSL_ECDH_CTX_cleanup(&group);
2097 CBB_cleanup(&public_key);
2098 return 0;
2099 }
2100 SSL_ECDH_CTX_cleanup(&group);
2101
Steven Valdez5440fe02016-07-18 12:40:30 -04002102 *out_found = 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002103 }
2104
Steven Valdez5440fe02016-07-18 12:40:30 -04002105 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002106}
2107
2108int ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
2109 if (ssl->s3->tmp.new_cipher->algorithm_mkey != SSL_kECDHE) {
2110 return 1;
2111 }
2112
2113 uint16_t group_id;
2114 CBB kse_bytes, public_key;
2115 if (!tls1_get_shared_group(ssl, &group_id) ||
2116 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2117 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2118 !CBB_add_u16(&kse_bytes, group_id) ||
2119 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2120 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2121 ssl->s3->hs->public_key_len) ||
2122 !CBB_flush(out)) {
2123 return 0;
2124 }
2125
2126 return 1;
2127}
2128
2129
Steven Valdezce902a92016-05-17 11:47:53 -04002130/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002131 *
Steven Valdezce902a92016-05-17 11:47:53 -04002132 * https://tools.ietf.org/html/rfc4492#section-5.1.2
2133 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07002134
Steven Valdezce902a92016-05-17 11:47:53 -04002135static void ext_supported_groups_init(SSL *ssl) {
2136 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2137 ssl->s3->tmp.peer_supported_group_list = NULL;
2138 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07002139}
2140
Steven Valdezce902a92016-05-17 11:47:53 -04002141static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002142 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2143 return 1;
2144 }
2145
Steven Valdezce902a92016-05-17 11:47:53 -04002146 CBB contents, groups_bytes;
2147 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002148 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002149 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002150 return 0;
2151 }
2152
Steven Valdezce902a92016-05-17 11:47:53 -04002153 const uint16_t *groups;
2154 size_t groups_len;
2155 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002156
2157 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002158 for (i = 0; i < groups_len; i++) {
2159 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002160 return 0;
2161 }
2162 }
2163
2164 return CBB_flush(out);
2165}
2166
Steven Valdezce902a92016-05-17 11:47:53 -04002167static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2168 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002169 /* This extension is not expected to be echoed by servers and is ignored. */
2170 return 1;
2171}
2172
Steven Valdezce902a92016-05-17 11:47:53 -04002173static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2174 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002175 if (contents == NULL) {
2176 return 1;
2177 }
2178
Steven Valdezce902a92016-05-17 11:47:53 -04002179 CBS supported_group_list;
2180 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2181 CBS_len(&supported_group_list) == 0 ||
2182 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002183 CBS_len(contents) != 0) {
2184 return 0;
2185 }
2186
Steven Valdezce902a92016-05-17 11:47:53 -04002187 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
2188 CBS_len(&supported_group_list));
2189 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002190 *out_alert = SSL_AD_INTERNAL_ERROR;
2191 return 0;
2192 }
2193
Steven Valdezce902a92016-05-17 11:47:53 -04002194 const size_t num_groups = CBS_len(&supported_group_list) / 2;
Adam Langley273d49c2015-07-20 16:38:52 -07002195 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002196 for (i = 0; i < num_groups; i++) {
2197 if (!CBS_get_u16(&supported_group_list,
2198 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002199 goto err;
2200 }
2201 }
2202
Steven Valdezce902a92016-05-17 11:47:53 -04002203 assert(CBS_len(&supported_group_list) == 0);
2204 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002205
2206 return 1;
2207
2208err:
Steven Valdezce902a92016-05-17 11:47:53 -04002209 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2210 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002211 *out_alert = SSL_AD_INTERNAL_ERROR;
2212 return 0;
2213}
2214
Steven Valdezce902a92016-05-17 11:47:53 -04002215static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002216 /* Servers don't echo this extension. */
2217 return 1;
2218}
2219
2220
Adam Langley614c66a2015-06-12 15:26:58 -07002221/* kExtensions contains all the supported extensions. */
2222static const struct tls_extension kExtensions[] = {
2223 {
Adam Langley5021b222015-06-12 18:27:58 -07002224 /* The renegotiation extension must always be at index zero because the
2225 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2226 * sent as an SCSV. */
2227 TLSEXT_TYPE_renegotiate,
2228 NULL,
2229 ext_ri_add_clienthello,
2230 ext_ri_parse_serverhello,
2231 ext_ri_parse_clienthello,
2232 ext_ri_add_serverhello,
2233 },
2234 {
Adam Langley614c66a2015-06-12 15:26:58 -07002235 TLSEXT_TYPE_server_name,
2236 ext_sni_init,
2237 ext_sni_add_clienthello,
2238 ext_sni_parse_serverhello,
2239 ext_sni_parse_clienthello,
2240 ext_sni_add_serverhello,
2241 },
Adam Langley0a056712015-07-01 15:03:33 -07002242 {
2243 TLSEXT_TYPE_extended_master_secret,
2244 ext_ems_init,
2245 ext_ems_add_clienthello,
2246 ext_ems_parse_serverhello,
2247 ext_ems_parse_clienthello,
2248 ext_ems_add_serverhello,
2249 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002250 {
2251 TLSEXT_TYPE_session_ticket,
2252 NULL,
2253 ext_ticket_add_clienthello,
2254 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002255 /* Ticket extension client parsing is handled in ssl_session.c */
2256 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002257 ext_ticket_add_serverhello,
2258 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002259 {
2260 TLSEXT_TYPE_signature_algorithms,
2261 NULL,
2262 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002263 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002264 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002265 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002266 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002267 {
2268 TLSEXT_TYPE_status_request,
2269 ext_ocsp_init,
2270 ext_ocsp_add_clienthello,
2271 ext_ocsp_parse_serverhello,
2272 ext_ocsp_parse_clienthello,
2273 ext_ocsp_add_serverhello,
2274 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002275 {
2276 TLSEXT_TYPE_next_proto_neg,
2277 ext_npn_init,
2278 ext_npn_add_clienthello,
2279 ext_npn_parse_serverhello,
2280 ext_npn_parse_clienthello,
2281 ext_npn_add_serverhello,
2282 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002283 {
2284 TLSEXT_TYPE_certificate_timestamp,
2285 NULL,
2286 ext_sct_add_clienthello,
2287 ext_sct_parse_serverhello,
2288 ext_sct_parse_clienthello,
2289 ext_sct_add_serverhello,
2290 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002291 {
2292 TLSEXT_TYPE_application_layer_protocol_negotiation,
2293 ext_alpn_init,
2294 ext_alpn_add_clienthello,
2295 ext_alpn_parse_serverhello,
2296 ext_alpn_parse_clienthello,
2297 ext_alpn_add_serverhello,
2298 },
Adam Langley49c7af12015-07-10 14:33:46 -07002299 {
2300 TLSEXT_TYPE_channel_id,
2301 ext_channel_id_init,
2302 ext_channel_id_add_clienthello,
2303 ext_channel_id_parse_serverhello,
2304 ext_channel_id_parse_clienthello,
2305 ext_channel_id_add_serverhello,
2306 },
Adam Langley391250d2015-07-15 19:06:07 -07002307 {
2308 TLSEXT_TYPE_srtp,
2309 ext_srtp_init,
2310 ext_srtp_add_clienthello,
2311 ext_srtp_parse_serverhello,
2312 ext_srtp_parse_clienthello,
2313 ext_srtp_add_serverhello,
2314 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002315 {
2316 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002317 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002318 ext_ec_point_add_clienthello,
2319 ext_ec_point_parse_serverhello,
2320 ext_ec_point_parse_clienthello,
2321 ext_ec_point_add_serverhello,
2322 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002323 {
2324 TLSEXT_TYPE_draft_version,
2325 NULL,
2326 ext_draft_version_add_clienthello,
2327 forbid_parse_serverhello,
2328 ignore_parse_clienthello,
2329 dont_add_serverhello,
2330 },
2331 {
2332 TLSEXT_TYPE_key_share,
2333 NULL,
2334 ext_key_share_add_clienthello,
2335 forbid_parse_serverhello,
2336 ignore_parse_clienthello,
2337 dont_add_serverhello,
2338 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002339 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2340 * intolerant to the last extension being zero-length. See
2341 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002342 {
Steven Valdezce902a92016-05-17 11:47:53 -04002343 TLSEXT_TYPE_supported_groups,
2344 ext_supported_groups_init,
2345 ext_supported_groups_add_clienthello,
2346 ext_supported_groups_parse_serverhello,
2347 ext_supported_groups_parse_clienthello,
2348 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002349 },
Adam Langley614c66a2015-06-12 15:26:58 -07002350};
2351
2352#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2353
Adam Langley4cfa96b2015-07-01 11:56:55 -07002354OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2355 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002356 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002357OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2358 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2359 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002360 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002361
Adam Langley614c66a2015-06-12 15:26:58 -07002362static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2363 uint16_t value) {
2364 unsigned i;
2365 for (i = 0; i < kNumExtensions; i++) {
2366 if (kExtensions[i].value == value) {
2367 *out_index = i;
2368 return &kExtensions[i];
2369 }
2370 }
2371
2372 return NULL;
2373}
2374
Adam Langley09505632015-07-30 18:10:13 -07002375int SSL_extension_supported(unsigned extension_value) {
2376 uint32_t index;
2377 return extension_value == TLSEXT_TYPE_padding ||
2378 tls_extension_find(&index, extension_value) != NULL;
2379}
2380
David Benjamine8d53502015-10-10 14:13:23 -04002381int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002382 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002383 if (ssl->client_version == SSL3_VERSION &&
2384 !ssl->s3->send_connection_binding) {
2385 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002386 }
Adam Langley95c29f32014-06-20 12:00:00 -07002387
David Benjamine8d53502015-10-10 14:13:23 -04002388 CBB extensions;
2389 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002390 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002391 }
Adam Langley95c29f32014-06-20 12:00:00 -07002392
David Benjamine8d53502015-10-10 14:13:23 -04002393 ssl->s3->tmp.extensions.sent = 0;
2394 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002395
Adam Langley614c66a2015-06-12 15:26:58 -07002396 size_t i;
2397 for (i = 0; i < kNumExtensions; i++) {
2398 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002399 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002400 }
2401 }
Adam Langley95c29f32014-06-20 12:00:00 -07002402
Adam Langley614c66a2015-06-12 15:26:58 -07002403 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002404 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002405 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002406 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2407 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2408 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002409 }
Adam Langley95c29f32014-06-20 12:00:00 -07002410
Adam Langley33ad2b52015-07-20 17:43:53 -07002411 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002412 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002413 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002414 }
Adam Langley75712922014-10-10 16:23:43 -07002415
David Benjamine8d53502015-10-10 14:13:23 -04002416 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002417 goto err;
2418 }
2419
David Benjamine8d53502015-10-10 14:13:23 -04002420 if (!SSL_IS_DTLS(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002421 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002422 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002423 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002424 *
2425 * NB: because this code works out the length of all existing extensions
2426 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002427 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002428 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002429 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002430 * Server 7.0 is intolerant to the last extension being zero-length. See
2431 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002432 if (padding_len >= 4 + 1) {
2433 padding_len -= 4;
2434 } else {
2435 padding_len = 1;
2436 }
Adam Langley95c29f32014-06-20 12:00:00 -07002437
Adam Langley33ad2b52015-07-20 17:43:53 -07002438 uint8_t *padding_bytes;
2439 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2440 !CBB_add_u16(&extensions, padding_len) ||
2441 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2442 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002443 }
Adam Langley75712922014-10-10 16:23:43 -07002444
Adam Langley33ad2b52015-07-20 17:43:53 -07002445 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002446 }
2447 }
Adam Langley75712922014-10-10 16:23:43 -07002448
David Benjamina01deee2015-12-08 18:56:31 -05002449 /* Discard empty extensions blocks. */
2450 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002451 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002452 }
2453
David Benjamine8d53502015-10-10 14:13:23 -04002454 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002455
2456err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002457 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002458 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002459}
Adam Langley95c29f32014-06-20 12:00:00 -07002460
David Benjamin56380462015-10-10 14:59:09 -04002461int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002462 CBB extensions;
2463 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002464 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002465 }
2466
2467 unsigned i;
2468 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002469 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002470 /* Don't send extensions that were not received. */
2471 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002472 }
Adam Langley95c29f32014-06-20 12:00:00 -07002473
David Benjamin56380462015-10-10 14:59:09 -04002474 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002475 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2476 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2477 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002478 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002479 }
Adam Langley95c29f32014-06-20 12:00:00 -07002480
David Benjamin56380462015-10-10 14:59:09 -04002481 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002482 goto err;
2483 }
2484
Steven Valdez143e8b32016-07-11 13:19:03 -04002485 /* Discard empty extensions blocks before TLS 1.3. */
2486 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2487 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002488 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002489 }
2490
David Benjamin56380462015-10-10 14:59:09 -04002491 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002492
2493err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002494 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002495 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002496}
Adam Langley95c29f32014-06-20 12:00:00 -07002497
David Benjamin0d56f882015-12-19 17:05:56 -05002498static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002499 size_t i;
2500 for (i = 0; i < kNumExtensions; i++) {
2501 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002502 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002503 }
2504 }
2505
David Benjamin0d56f882015-12-19 17:05:56 -05002506 ssl->s3->tmp.extensions.received = 0;
2507 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002508 /* The renegotiation extension must always be at index zero because the
2509 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2510 * sent as an SCSV. */
2511 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002512
Adam Langleyfcf25832014-12-18 17:42:32 -08002513 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002514 if (CBS_len(cbs) != 0) {
2515 /* Decode the extensions block and check it is valid. */
2516 CBS extensions;
2517 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2518 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002519 *out_alert = SSL_AD_DECODE_ERROR;
2520 return 0;
2521 }
Adam Langley95c29f32014-06-20 12:00:00 -07002522
Adam Langley33ad2b52015-07-20 17:43:53 -07002523 while (CBS_len(&extensions) != 0) {
2524 uint16_t type;
2525 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002526
Adam Langley33ad2b52015-07-20 17:43:53 -07002527 /* Decode the next extension. */
2528 if (!CBS_get_u16(&extensions, &type) ||
2529 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2530 *out_alert = SSL_AD_DECODE_ERROR;
2531 return 0;
2532 }
2533
David Benjaminc7ce9772015-10-09 19:32:41 -04002534 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2535 * ambiguous. Ignore all but the renegotiation_info extension. */
David Benjamin0d56f882015-12-19 17:05:56 -05002536 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
David Benjaminc7ce9772015-10-09 19:32:41 -04002537 continue;
2538 }
2539
Adam Langley33ad2b52015-07-20 17:43:53 -07002540 unsigned ext_index;
2541 const struct tls_extension *const ext =
2542 tls_extension_find(&ext_index, type);
2543
2544 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002545 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002546 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2547 return 0;
2548 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002549 continue;
2550 }
2551
David Benjamin0d56f882015-12-19 17:05:56 -05002552 ssl->s3->tmp.extensions.received |= (1u << ext_index);
Adam Langley614c66a2015-06-12 15:26:58 -07002553 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002554 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
Adam Langley614c66a2015-06-12 15:26:58 -07002555 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002556 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2557 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002558 return 0;
2559 }
Adam Langley614c66a2015-06-12 15:26:58 -07002560 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002561 }
Adam Langley75712922014-10-10 16:23:43 -07002562
Adam Langley614c66a2015-06-12 15:26:58 -07002563 for (i = 0; i < kNumExtensions; i++) {
David Benjamin0d56f882015-12-19 17:05:56 -05002564 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002565 /* Extension wasn't observed so call the callback with a NULL
2566 * parameter. */
2567 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002568 if (!kExtensions[i].parse_clienthello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002569 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2570 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002571 *out_alert = alert;
2572 return 0;
2573 }
2574 }
2575 }
2576
Adam Langleyfcf25832014-12-18 17:42:32 -08002577 return 1;
2578}
Adam Langley95c29f32014-06-20 12:00:00 -07002579
David Benjamin0d56f882015-12-19 17:05:56 -05002580int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002581 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002582 if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) {
2583 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002584 return 0;
2585 }
Adam Langley95c29f32014-06-20 12:00:00 -07002586
David Benjamin0d56f882015-12-19 17:05:56 -05002587 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002588 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002589 return 0;
2590 }
Adam Langley95c29f32014-06-20 12:00:00 -07002591
Adam Langleyfcf25832014-12-18 17:42:32 -08002592 return 1;
2593}
Adam Langley95c29f32014-06-20 12:00:00 -07002594
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002595OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2596
David Benjamin0d56f882015-12-19 17:05:56 -05002597static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002598 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2599 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2600 return 1;
2601 }
Adam Langley614c66a2015-06-12 15:26:58 -07002602
Steven Valdez143e8b32016-07-11 13:19:03 -04002603 /* Decode the extensions block and check it is valid. */
2604 CBS extensions;
2605 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2606 !tls1_check_duplicate_extensions(&extensions)) {
2607 *out_alert = SSL_AD_DECODE_ERROR;
2608 return 0;
2609 }
2610
2611 uint32_t received = 0;
2612 while (CBS_len(&extensions) != 0) {
2613 uint16_t type;
2614 CBS extension;
2615
2616 /* Decode the next extension. */
2617 if (!CBS_get_u16(&extensions, &type) ||
2618 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002619 *out_alert = SSL_AD_DECODE_ERROR;
2620 return 0;
2621 }
Adam Langley95c29f32014-06-20 12:00:00 -07002622
Steven Valdez143e8b32016-07-11 13:19:03 -04002623 unsigned ext_index;
2624 const struct tls_extension *const ext =
2625 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002626
Steven Valdez143e8b32016-07-11 13:19:03 -04002627 if (ext == NULL) {
2628 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002629 return 0;
2630 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002631 continue;
2632 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002633
Steven Valdez143e8b32016-07-11 13:19:03 -04002634 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) {
2635 /* If the extension was never sent then it is illegal. */
2636 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2637 ERR_add_error_dataf("extension :%u", (unsigned)type);
2638 *out_alert = SSL_AD_DECODE_ERROR;
2639 return 0;
2640 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002641
Steven Valdez143e8b32016-07-11 13:19:03 -04002642 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002643
Steven Valdez143e8b32016-07-11 13:19:03 -04002644 uint8_t alert = SSL_AD_DECODE_ERROR;
2645 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2646 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2647 ERR_add_error_dataf("extension: %u", (unsigned)type);
2648 *out_alert = alert;
2649 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002650 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002651 }
Adam Langley95c29f32014-06-20 12:00:00 -07002652
Adam Langley33ad2b52015-07-20 17:43:53 -07002653 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002654 for (i = 0; i < kNumExtensions; i++) {
2655 if (!(received & (1u << i))) {
2656 /* Extension wasn't observed so call the callback with a NULL
2657 * parameter. */
2658 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002659 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002660 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2661 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002662 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002663 return 0;
2664 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002665 }
2666 }
Adam Langley95c29f32014-06-20 12:00:00 -07002667
Adam Langleyfcf25832014-12-18 17:42:32 -08002668 return 1;
2669}
Adam Langley95c29f32014-06-20 12:00:00 -07002670
David Benjamin0d56f882015-12-19 17:05:56 -05002671static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002672 int ret = SSL_TLSEXT_ERR_NOACK;
2673 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002674
David Benjamin78f8aab2016-03-10 16:33:58 -05002675 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002676 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002677 ssl->ctx->tlsext_servername_arg);
2678 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002679 ret = ssl->initial_ctx->tlsext_servername_callback(
2680 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002681 }
Adam Langley95c29f32014-06-20 12:00:00 -07002682
Adam Langleyfcf25832014-12-18 17:42:32 -08002683 switch (ret) {
2684 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002685 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002686 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002687
Adam Langleyfcf25832014-12-18 17:42:32 -08002688 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002689 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002690 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002691
Adam Langleyfcf25832014-12-18 17:42:32 -08002692 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002693 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002694 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002695
Adam Langleyfcf25832014-12-18 17:42:32 -08002696 default:
2697 return 1;
2698 }
2699}
Adam Langleyed8270a2014-09-02 13:52:56 -07002700
David Benjamin0d56f882015-12-19 17:05:56 -05002701static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002702 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002703 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002704
David Benjamin78f8aab2016-03-10 16:33:58 -05002705 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002706 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002707 ssl->ctx->tlsext_servername_arg);
2708 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002709 ret = ssl->initial_ctx->tlsext_servername_callback(
2710 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002711 }
Adam Langley95c29f32014-06-20 12:00:00 -07002712
Adam Langleyfcf25832014-12-18 17:42:32 -08002713 switch (ret) {
2714 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002715 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002716 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002717
Adam Langleyfcf25832014-12-18 17:42:32 -08002718 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002719 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002720 return 1;
2721
2722 default:
2723 return 1;
2724 }
2725}
2726
David Benjamin0d56f882015-12-19 17:05:56 -05002727int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002728 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002729 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2730 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002731 return 0;
2732 }
2733
David Benjamin0d56f882015-12-19 17:05:56 -05002734 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002735 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002736 return 0;
2737 }
2738
2739 return 1;
2740}
Adam Langley95c29f32014-06-20 12:00:00 -07002741
David Benjamine3aa1d92015-06-16 15:34:50 -04002742int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002743 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002744 size_t ticket_len, const uint8_t *session_id,
2745 size_t session_id_len) {
2746 int ret = 1; /* Most errors are non-fatal. */
2747 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2748 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002749
David Benjamine3aa1d92015-06-16 15:34:50 -04002750 HMAC_CTX hmac_ctx;
2751 HMAC_CTX_init(&hmac_ctx);
2752 EVP_CIPHER_CTX cipher_ctx;
2753 EVP_CIPHER_CTX_init(&cipher_ctx);
2754
David Benjaminef1b0092015-11-21 14:05:44 -05002755 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002756 *out_session = NULL;
2757
2758 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2759 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002760 }
2761
David Benjaminadcc3952015-04-26 13:07:57 -04002762 /* Ensure there is room for the key name and the largest IV
2763 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2764 * the maximum IV length should be well under the minimum size for the
2765 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002766 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2767 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002768 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002769 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002770
David Benjamine3aa1d92015-06-16 15:34:50 -04002771 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002772 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2773 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2774 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002775 if (cb_ret < 0) {
2776 ret = 0;
2777 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002778 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002779 if (cb_ret == 0) {
2780 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002781 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002782 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002783 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002784 }
2785 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002786 /* Check the key name matches. */
2787 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2788 SSL_TICKET_KEY_NAME_LEN) != 0) {
2789 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002790 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002791 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2792 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002793 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002794 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2795 ssl_ctx->tlsext_tick_aes_key, iv)) {
2796 ret = 0;
2797 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002798 }
2799 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002800 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002801
David Benjamine3aa1d92015-06-16 15:34:50 -04002802 /* Check the MAC at the end of the ticket. */
2803 uint8_t mac[EVP_MAX_MD_SIZE];
2804 size_t mac_len = HMAC_size(&hmac_ctx);
2805 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002806 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002807 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002808 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002809 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2810 HMAC_Final(&hmac_ctx, mac, NULL);
2811 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2812 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002813 }
2814
David Benjamine3aa1d92015-06-16 15:34:50 -04002815 /* Decrypt the session data. */
2816 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2817 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2818 mac_len;
2819 plaintext = OPENSSL_malloc(ciphertext_len);
2820 if (plaintext == NULL) {
2821 ret = 0;
2822 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002823 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002824 if (ciphertext_len >= INT_MAX) {
2825 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002826 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002827 int len1, len2;
2828 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2829 (int)ciphertext_len) ||
2830 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2831 ERR_clear_error(); /* Don't leave an error on the queue. */
2832 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002833 }
2834
David Benjamine3aa1d92015-06-16 15:34:50 -04002835 /* Decode the session. */
2836 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2837 if (session == NULL) {
2838 ERR_clear_error(); /* Don't leave an error on the queue. */
2839 goto done;
2840 }
2841
2842 /* Copy the client's session ID into the new session, to denote the ticket has
2843 * been accepted. */
2844 memcpy(session->session_id, session_id, session_id_len);
2845 session->session_id_length = session_id_len;
2846
2847 *out_session = session;
2848
2849done:
2850 OPENSSL_free(plaintext);
2851 HMAC_CTX_cleanup(&hmac_ctx);
2852 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2853 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002854}
Adam Langley95c29f32014-06-20 12:00:00 -07002855
Steven Valdez0d62f262015-09-04 12:41:04 -04002856int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002857 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05002858 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002859 return 1;
2860 }
David Benjamincd996942014-07-20 16:23:51 -04002861
Steven Valdez0d62f262015-09-04 12:41:04 -04002862 CERT *const cert = ssl->cert;
2863 OPENSSL_free(cert->peer_sigalgs);
2864 cert->peer_sigalgs = NULL;
2865 cert->peer_sigalgslen = 0;
2866
2867 size_t num_sigalgs = CBS_len(in_sigalgs);
2868
2869 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002870 return 0;
2871 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002872 num_sigalgs /= 2;
2873
2874 /* supported_signature_algorithms in the certificate request is
2875 * allowed to be empty. */
2876 if (num_sigalgs == 0) {
2877 return 1;
2878 }
2879
Steven Valdez02563852016-06-23 13:33:05 -04002880 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
2881 * and we just divided |num_sigalgs| by two. */
2882 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
Steven Valdez0d62f262015-09-04 12:41:04 -04002883 if (cert->peer_sigalgs == NULL) {
2884 return 0;
2885 }
2886 cert->peer_sigalgslen = num_sigalgs;
2887
2888 CBS sigalgs;
2889 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2890
2891 size_t i;
2892 for (i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04002893 if (!CBS_get_u16(&sigalgs, &cert->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04002894 return 0;
2895 }
2896 }
Adam Langley95c29f32014-06-20 12:00:00 -07002897
Adam Langleyfcf25832014-12-18 17:42:32 -08002898 return 1;
2899}
David Benjaminec2f27d2014-11-13 19:17:25 -05002900
David Benjaminea9a0d52016-07-08 15:52:59 -07002901int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04002902 CERT *cert = ssl->cert;
Steven Valdez0d62f262015-09-04 12:41:04 -04002903 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002904
Steven Valdezf0451ca2016-06-29 13:16:27 -04002905 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
2906 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
2907 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04002908 int type = ssl_private_key_type(ssl);
2909 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07002910 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04002911 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002912 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04002913 if (ssl_is_ecdsa_key_type(type)) {
2914 *out = SSL_SIGN_ECDSA_SHA1;
2915 return 1;
2916 }
2917 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
2918 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002919 }
2920
Steven Valdezeff1e8d2016-07-06 14:24:47 -04002921 const uint16_t *sigalgs;
2922 size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07002923 if (cert->sigalgs != NULL) {
2924 sigalgs = cert->sigalgs;
2925 sigalgs_len = cert->sigalgs_len;
Steven Valdez0d62f262015-09-04 12:41:04 -04002926 }
2927
David Benjaminea9a0d52016-07-08 15:52:59 -07002928 const uint16_t *peer_sigalgs = cert->peer_sigalgs;
2929 size_t peer_sigalgs_len = cert->peer_sigalgslen;
David Benjamin51dd7d62016-07-08 16:07:01 -07002930 if (peer_sigalgs_len == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07002931 /* If the client didn't specify any signature_algorithms extension then
2932 * we can assume that it supports SHA1. See
2933 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
2934 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
2935 SSL_SIGN_ECDSA_SHA1};
2936 peer_sigalgs = kDefaultPeerAlgorithms;
2937 peer_sigalgs_len =
2938 sizeof(kDefaultPeerAlgorithms) / sizeof(kDefaultPeerAlgorithms);
2939 }
2940
David Benjamind246b812016-07-08 15:07:02 -07002941 for (i = 0; i < sigalgs_len; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07002942 uint16_t sigalg = sigalgs[i];
2943 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
2944 * negotiated. */
2945 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
2946 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
2947 continue;
2948 }
2949
David Benjaminea9a0d52016-07-08 15:52:59 -07002950 for (j = 0; j < peer_sigalgs_len; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07002951 if (sigalg == peer_sigalgs[j]) {
2952 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07002953 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002954 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002955 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002956 }
Adam Langley95c29f32014-06-20 12:00:00 -07002957
David Benjaminea9a0d52016-07-08 15:52:59 -07002958 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
2959 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002960}
Adam Langley95c29f32014-06-20 12:00:00 -07002961
David Benjamind6a4ae92015-08-06 11:10:51 -04002962int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2963 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002964 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002965
2966 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002967 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2968 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002969 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002970
David Benjamind6a4ae92015-08-06 11:10:51 -04002971 static const char kClientIDMagic[] = "TLS Channel ID signature";
2972 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2973
2974 if (ssl->hit) {
2975 static const char kResumptionMagic[] = "Resumption";
2976 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2977 if (ssl->session->original_handshake_hash_len == 0) {
2978 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2979 goto err;
2980 }
2981 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2982 ssl->session->original_handshake_hash_len);
2983 }
2984
2985 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2986 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2987 sizeof(handshake_hash));
2988 if (handshake_hash_len < 0) {
2989 goto err;
2990 }
2991 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2992 unsigned len_u;
2993 EVP_DigestFinal_ex(&ctx, out, &len_u);
2994 *out_len = len_u;
2995
2996 ret = 1;
2997
2998err:
2999 EVP_MD_CTX_cleanup(&ctx);
3000 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003001}
Adam Langley1258b6a2014-06-20 12:00:00 -07003002
3003/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin0d56f882015-12-19 17:05:56 -05003004 * hashes in |ssl->session| so that Channel ID resumptions can sign that
3005 * data. */
3006int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003007 int digest_len;
3008 /* This function should never be called for a resumed session because the
3009 * handshake hashes that we wish to record are for the original, full
3010 * handshake. */
David Benjamin0d56f882015-12-19 17:05:56 -05003011 if (ssl->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003012 return -1;
3013 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003014
Adam Langleyfcf25832014-12-18 17:42:32 -08003015 digest_len =
David Benjamin0d56f882015-12-19 17:05:56 -05003016 tls1_handshake_digest(ssl, ssl->session->original_handshake_hash,
3017 sizeof(ssl->session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003018 if (digest_len < 0) {
3019 return -1;
3020 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003021
David Benjamin0d56f882015-12-19 17:05:56 -05003022 ssl->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003023
Adam Langleyfcf25832014-12-18 17:42:32 -08003024 return 1;
3025}