blob: 9143ba85c33ebc1cb6bdef14d6f4c75815a8a291 [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 Valdezce902a92016-05-17 11:47:53 -0400303/* tls1_get_grouplist sets |*out_group_ids| and |*out_group_ids_len| to the
304 * list of allowed group IDs. If |get_peer_groups| is non-zero, return the
305 * peer's group list. Otherwise, return the preferred list. */
306static void tls1_get_grouplist(SSL *ssl, int get_peer_groups,
307 const uint16_t **out_group_ids,
308 size_t *out_group_ids_len) {
309 if (get_peer_groups) {
310 /* Only clients send a supported group list, so this function is only
311 * called on the server. */
David Benjamin0d56f882015-12-19 17:05:56 -0500312 assert(ssl->server);
Steven Valdezce902a92016-05-17 11:47:53 -0400313 *out_group_ids = ssl->s3->tmp.peer_supported_group_list;
314 *out_group_ids_len = ssl->s3->tmp.peer_supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800315 return;
316 }
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Steven Valdezce902a92016-05-17 11:47:53 -0400318 *out_group_ids = ssl->supported_group_list;
319 *out_group_ids_len = ssl->supported_group_list_len;
320 if (!*out_group_ids) {
321 *out_group_ids = kDefaultGroups;
322 *out_group_ids_len = sizeof(kDefaultGroups) / sizeof(kDefaultGroups[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 }
324}
David Benjamined439582014-07-14 19:13:02 -0400325
Steven Valdezce902a92016-05-17 11:47:53 -0400326int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) {
327 const uint16_t *groups, *peer_groups, *pref, *supp;
328 size_t groups_len, peer_groups_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 /* Can't do anything on client side */
David Benjamin4298d772015-12-19 00:18:25 -0500331 if (ssl->server == 0) {
332 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 }
334
Steven Valdezce902a92016-05-17 11:47:53 -0400335 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
336 tls1_get_grouplist(ssl, 1 /* peer groups */, &peer_groups, &peer_groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800337
Steven Valdezce902a92016-05-17 11:47:53 -0400338 if (peer_groups_len == 0) {
339 /* Clients are not required to send a supported_groups extension. In this
340 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin4298d772015-12-19 00:18:25 -0500341 * section 4, paragraph 3.
342 *
343 * However, in the interests of compatibility, we will skip ECDH if the
344 * client didn't send an extension because we can't be sure that they'll
Steven Valdezce902a92016-05-17 11:47:53 -0400345 * support our favoured group. */
David Benjamin4298d772015-12-19 00:18:25 -0500346 return 0;
David Benjamin55a43642015-04-20 14:45:55 -0400347 }
348
David Benjamin4298d772015-12-19 00:18:25 -0500349 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400350 pref = groups;
351 pref_len = groups_len;
352 supp = peer_groups;
353 supp_len = peer_groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400354 } else {
Steven Valdezce902a92016-05-17 11:47:53 -0400355 pref = peer_groups;
356 pref_len = peer_groups_len;
357 supp = groups;
358 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400359 }
360
361 for (i = 0; i < pref_len; i++) {
362 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400364 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500365 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 }
367 }
368 }
369
David Benjamin4298d772015-12-19 00:18:25 -0500370 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800371}
Adam Langley95c29f32014-06-20 12:00:00 -0700372
Steven Valdezce902a92016-05-17 11:47:53 -0400373int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400375 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 size_t i;
377
Steven Valdezce902a92016-05-17 11:47:53 -0400378 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
379 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 return 0;
381 }
382
383 for (i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400384 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
385 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 return 0;
387 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 }
389
Steven Valdezce902a92016-05-17 11:47:53 -0400390 OPENSSL_free(*out_group_ids);
391 *out_group_ids = group_ids;
392 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800393
394 return 1;
395}
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Steven Valdezce902a92016-05-17 11:47:53 -0400397/* tls1_curve_params_from_ec_key sets |*out_group_id| and |*out_comp_id| to the
398 * TLS group ID and point format, respectively, for |ec|. It returns one on
David Benjamin072334d2014-07-13 16:24:27 -0400399 * success and zero on failure. */
Steven Valdezce902a92016-05-17 11:47:53 -0400400static int tls1_curve_params_from_ec_key(uint16_t *out_group_id,
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 uint8_t *out_comp_id, EC_KEY *ec) {
402 int nid;
403 uint16_t id;
404 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 if (ec == NULL) {
407 return 0;
408 }
Adam Langley95c29f32014-06-20 12:00:00 -0700409
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 grp = EC_KEY_get0_group(ec);
411 if (grp == NULL) {
412 return 0;
413 }
David Benjamin072334d2014-07-13 16:24:27 -0400414
Steven Valdezce902a92016-05-17 11:47:53 -0400415 /* Determine group ID */
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 nid = EC_GROUP_get_curve_name(grp);
Steven Valdezce902a92016-05-17 11:47:53 -0400417 if (!ssl_nid_to_group_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 return 0;
419 }
David Benjamin072334d2014-07-13 16:24:27 -0400420
Steven Valdezce902a92016-05-17 11:47:53 -0400421 /* Set the named group ID. Arbitrary explicit groups are not supported. */
422 *out_group_id = id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800423
424 if (out_comp_id) {
425 if (EC_KEY_get0_public_key(ec) == NULL) {
426 return 0;
427 }
428 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
429 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
430 } else {
431 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
432 }
433 }
434
435 return 1;
436}
David Benjamin072334d2014-07-13 16:24:27 -0400437
Steven Valdezce902a92016-05-17 11:47:53 -0400438/* tls1_check_group_id returns one if |group_id| is consistent with both our
439 * and the peer's group preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400440 * preferences are checked; the peer (the server) does not send preferences. */
Steven Valdezce902a92016-05-17 11:47:53 -0400441int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
442 const uint16_t *groups;
443 size_t groups_len, i, get_peer_groups;
David Benjamin42e9a772014-09-02 23:18:44 -0400444
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 /* Check against our list, then the peer's list. */
Steven Valdezce902a92016-05-17 11:47:53 -0400446 for (get_peer_groups = 0; get_peer_groups <= 1; get_peer_groups++) {
447 if (get_peer_groups && !ssl->server) {
David Benjamin55a43642015-04-20 14:45:55 -0400448 /* Servers do not present a preference list so, if we are a client, only
449 * check our list. */
450 continue;
451 }
452
Steven Valdezce902a92016-05-17 11:47:53 -0400453 tls1_get_grouplist(ssl, get_peer_groups, &groups, &groups_len);
454 if (get_peer_groups && groups_len == 0) {
455 /* Clients are not required to send a supported_groups extension. In this
456 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin55a43642015-04-20 14:45:55 -0400457 * section 4, paragraph 3. */
458 continue;
459 }
Steven Valdezce902a92016-05-17 11:47:53 -0400460 for (i = 0; i < groups_len; i++) {
461 if (groups[i] == group_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 break;
463 }
464 }
Adam Langley95c29f32014-06-20 12:00:00 -0700465
Steven Valdezce902a92016-05-17 11:47:53 -0400466 if (i == groups_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 return 0;
468 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 }
David Benjamin033e5f42014-11-13 18:47:41 -0500470
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 return 1;
472}
David Benjamin033e5f42014-11-13 18:47:41 -0500473
David Benjamin0d56f882015-12-19 17:05:56 -0500474int tls1_check_ec_cert(SSL *ssl, X509 *x) {
David Benjamin75ea5bb2016-07-08 17:43:29 -0700475 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
476 /* In TLS 1.3, the ECDSA curve is negotiated via signature algorithms. */
477 return 1;
478 }
479
Adam Langleyfcf25832014-12-18 17:42:32 -0800480 EVP_PKEY *pkey = X509_get_pubkey(x);
David Benjamin75ea5bb2016-07-08 17:43:29 -0700481 if (pkey == NULL) {
482 return 0;
483 }
484
485 int ret = 0;
Steven Valdezce902a92016-05-17 11:47:53 -0400486 uint16_t group_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800487 uint8_t comp_id;
David Benjamin758d1272015-11-20 17:47:25 -0500488 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
489 if (ec_key == NULL ||
Steven Valdezce902a92016-05-17 11:47:53 -0400490 !tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) ||
491 !tls1_check_group_id(ssl, group_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400492 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800493 goto done;
494 }
495
496 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500497
498done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400499 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800500 return ret;
501}
David Benjamin42e9a772014-09-02 23:18:44 -0400502
Adam Langley95c29f32014-06-20 12:00:00 -0700503/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800504 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700505
Steven Valdez02563852016-06-23 13:33:05 -0400506static const uint16_t kDefaultSignatureAlgorithms[] = {
507 SSL_SIGN_RSA_PKCS1_SHA512,
508 SSL_SIGN_ECDSA_SECP521R1_SHA512,
Adam Langley95c29f32014-06-20 12:00:00 -0700509
Steven Valdez02563852016-06-23 13:33:05 -0400510 SSL_SIGN_RSA_PKCS1_SHA384,
511 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Adam Langley95c29f32014-06-20 12:00:00 -0700512
Steven Valdez02563852016-06-23 13:33:05 -0400513 SSL_SIGN_RSA_PKCS1_SHA256,
514 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Adam Langley95c29f32014-06-20 12:00:00 -0700515
Steven Valdez02563852016-06-23 13:33:05 -0400516 SSL_SIGN_RSA_PKCS1_SHA1,
517 SSL_SIGN_ECDSA_SHA1,
Adam Langley95c29f32014-06-20 12:00:00 -0700518};
David Benjamin05da6e12014-07-12 20:42:55 -0400519
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400520static const uint16_t kDefaultTLS13SignatureAlgorithms[] = {
521 SSL_SIGN_RSA_PSS_SHA512,
522 SSL_SIGN_RSA_PKCS1_SHA512,
523 SSL_SIGN_ECDSA_SECP521R1_SHA512,
524
525 SSL_SIGN_RSA_PSS_SHA384,
526 SSL_SIGN_RSA_PKCS1_SHA384,
527 SSL_SIGN_ECDSA_SECP384R1_SHA384,
528
529 SSL_SIGN_RSA_PSS_SHA256,
530 SSL_SIGN_RSA_PKCS1_SHA256,
531 SSL_SIGN_ECDSA_SECP256R1_SHA256,
532
533 SSL_SIGN_RSA_PKCS1_SHA1,
534 SSL_SIGN_ECDSA_SHA1,
535};
536
Steven Valdez02563852016-06-23 13:33:05 -0400537size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400538 uint16_t version;
539 if (ssl->s3->have_version) {
540 version = ssl3_protocol_version(ssl);
541 } else {
542 version = ssl->method->version_from_wire(ssl->client_version);
543 }
544
545 if (version >= TLS1_3_VERSION) {
546 *psigs = kDefaultTLS13SignatureAlgorithms;
547 return sizeof(kDefaultTLS13SignatureAlgorithms) /
548 sizeof(kDefaultTLS13SignatureAlgorithms[0]);
549 }
550
Steven Valdez02563852016-06-23 13:33:05 -0400551 *psigs = kDefaultSignatureAlgorithms;
552 return sizeof(kDefaultSignatureAlgorithms) /
553 sizeof(kDefaultSignatureAlgorithms[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800554}
Adam Langley95c29f32014-06-20 12:00:00 -0700555
David Benjamin887c3002016-07-08 16:15:32 -0700556int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
Steven Valdez02563852016-06-23 13:33:05 -0400557 const uint16_t *sent_sigs;
558 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800559
Adam Langleyfcf25832014-12-18 17:42:32 -0800560 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500561 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400562 for (i = 0; i < sent_sigslen; i++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400563 if (sigalg == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800564 break;
565 }
566 }
567
David Benjamin788be4a2015-10-30 17:50:57 -0400568 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400569 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
571 return 0;
572 }
573
Adam Langleyfcf25832014-12-18 17:42:32 -0800574 return 1;
575}
576
577/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
578 * supported or doesn't appear in supported signature algorithms. Unlike
579 * ssl_cipher_get_disabled this applies to a specific session and not global
580 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500581void ssl_set_client_disabled(SSL *ssl) {
582 CERT *c = ssl->cert;
Steven Valdez02563852016-06-23 13:33:05 -0400583 const uint16_t *sigalgs;
Adam Langleyfcf25832014-12-18 17:42:32 -0800584 size_t i, sigalgslen;
585 int have_rsa = 0, have_ecdsa = 0;
586 c->mask_a = 0;
587 c->mask_k = 0;
588
Adam Langleyfcf25832014-12-18 17:42:32 -0800589 /* Now go through all signature algorithms seeing if we support any for RSA,
590 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500591 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -0400592 for (i = 0; i < sigalgslen; i++) {
593 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400594 case SSL_SIGN_RSA_PSS_SHA512:
595 case SSL_SIGN_RSA_PSS_SHA384:
596 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400597 case SSL_SIGN_RSA_PKCS1_SHA512:
598 case SSL_SIGN_RSA_PKCS1_SHA384:
599 case SSL_SIGN_RSA_PKCS1_SHA256:
600 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800601 have_rsa = 1;
602 break;
603
Steven Valdez02563852016-06-23 13:33:05 -0400604 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
605 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
606 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
607 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800608 have_ecdsa = 1;
609 break;
610 }
611 }
612
613 /* Disable auth if we don't include any appropriate signature algorithms. */
614 if (!have_rsa) {
615 c->mask_a |= SSL_aRSA;
616 }
617 if (!have_ecdsa) {
618 c->mask_a |= SSL_aECDSA;
619 }
620
621 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500622 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800623 c->mask_a |= SSL_aPSK;
624 c->mask_k |= SSL_kPSK;
625 }
626}
Adam Langley95c29f32014-06-20 12:00:00 -0700627
Adam Langley614c66a2015-06-12 15:26:58 -0700628/* tls_extension represents a TLS extension that is handled internally. The
629 * |init| function is called for each handshake, before any other functions of
630 * the extension. Then the add and parse callbacks are called as needed.
631 *
632 * The parse callbacks receive a |CBS| that contains the contents of the
633 * extension (i.e. not including the type and length bytes). If an extension is
634 * not received then the parse callbacks will be called with a NULL CBS so that
635 * they can do any processing needed to handle the absence of an extension.
636 *
637 * The add callbacks receive a |CBB| to which the extension can be appended but
638 * the function is responsible for appending the type and length bytes too.
639 *
640 * All callbacks return one for success and zero for error. If a parse function
641 * returns zero then a fatal alert with value |*out_alert| will be sent. If
642 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
643struct tls_extension {
644 uint16_t value;
645 void (*init)(SSL *ssl);
646
647 int (*add_clienthello)(SSL *ssl, CBB *out);
648 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
649
650 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
651 int (*add_serverhello)(SSL *ssl, CBB *out);
652};
653
Steven Valdez6b8509a2016-07-12 13:38:32 -0400654static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
655 if (contents != NULL) {
656 /* Servers MUST NOT send this extension. */
657 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
658 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
659 return 0;
660 }
661
662 return 1;
663}
664
665static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
666 /* This extension from the client is handled elsewhere. */
667 return 1;
668}
669
670static int dont_add_serverhello(SSL *ssl, CBB *out) {
671 return 1;
672}
Adam Langley614c66a2015-06-12 15:26:58 -0700673
674/* Server name indication (SNI).
675 *
676 * https://tools.ietf.org/html/rfc6066#section-3. */
677
678static void ext_sni_init(SSL *ssl) {
679 ssl->s3->tmp.should_ack_sni = 0;
680}
681
682static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
683 if (ssl->tlsext_hostname == NULL) {
684 return 1;
685 }
686
687 CBB contents, server_name_list, name;
688 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
689 !CBB_add_u16_length_prefixed(out, &contents) ||
690 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
691 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
692 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
693 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
694 strlen(ssl->tlsext_hostname)) ||
695 !CBB_flush(out)) {
696 return 0;
697 }
698
699 return 1;
700}
701
David Benjamin0d56f882015-12-19 17:05:56 -0500702static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
703 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700704 if (contents == NULL) {
705 return 1;
706 }
707
708 if (CBS_len(contents) != 0) {
709 return 0;
710 }
711
712 assert(ssl->tlsext_hostname != NULL);
713
714 if (!ssl->hit) {
715 assert(ssl->session->tlsext_hostname == NULL);
716 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
717 if (!ssl->session->tlsext_hostname) {
718 *out_alert = SSL_AD_INTERNAL_ERROR;
719 return 0;
720 }
721 }
722
723 return 1;
724}
725
David Benjamin0d56f882015-12-19 17:05:56 -0500726static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
727 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700728 if (contents == NULL) {
729 return 1;
730 }
731
David Benjamin9b611e22016-03-03 08:48:30 -0500732 CBS server_name_list, host_name;
733 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700734 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500735 !CBS_get_u8(&server_name_list, &name_type) ||
736 /* Although the server_name extension was intended to be extensible to
737 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
738 * different name types will cause an error. Further, RFC 4366 originally
739 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
740 * adding new name types is no longer feasible.
741 *
742 * Act as if the extensibility does not exist to simplify parsing. */
743 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
744 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700745 CBS_len(contents) != 0) {
746 return 0;
747 }
748
David Benjamin9b611e22016-03-03 08:48:30 -0500749 if (name_type != TLSEXT_NAMETYPE_host_name ||
750 CBS_len(&host_name) == 0 ||
751 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
752 CBS_contains_zero_byte(&host_name)) {
753 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
754 return 0;
755 }
Adam Langley614c66a2015-06-12 15:26:58 -0700756
David Benjamin9b611e22016-03-03 08:48:30 -0500757 /* TODO(davidben): SNI should be resolved before resumption. We have the
758 * early callback as a replacement, but we should fix the current callback
759 * and avoid the need for |SSL_CTX_set_session_id_context|. */
760 if (!ssl->hit) {
761 assert(ssl->session->tlsext_hostname == NULL);
762
763 /* Copy the hostname as a string. */
764 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
765 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700766 return 0;
767 }
768
David Benjamin9b611e22016-03-03 08:48:30 -0500769 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700770 }
771
772 return 1;
773}
774
775static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
776 if (ssl->hit ||
777 !ssl->s3->tmp.should_ack_sni ||
778 ssl->session->tlsext_hostname == NULL) {
779 return 1;
780 }
781
782 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
783 !CBB_add_u16(out, 0 /* length */)) {
784 return 0;
785 }
786
787 return 1;
788}
789
790
Adam Langley5021b222015-06-12 18:27:58 -0700791/* Renegotiation indication.
792 *
793 * https://tools.ietf.org/html/rfc5746 */
794
795static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
796 CBB contents, prev_finished;
797 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
798 !CBB_add_u16_length_prefixed(out, &contents) ||
799 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
800 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
801 ssl->s3->previous_client_finished_len) ||
802 !CBB_flush(out)) {
803 return 0;
804 }
805
806 return 1;
807}
808
809static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
810 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400811 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
812 return 0;
813 }
814
David Benjamin3e052de2015-11-25 20:10:31 -0500815 /* Servers may not switch between omitting the extension and supporting it.
816 * See RFC 5746, sections 3.5 and 4.2. */
817 if (ssl->s3->initial_handshake_complete &&
818 (contents != NULL) != ssl->s3->send_connection_binding) {
819 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
820 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
821 return 0;
822 }
823
Adam Langley5021b222015-06-12 18:27:58 -0700824 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500825 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700826 * RI even on initial ServerHello because the client doesn't see any
827 * renegotiation during an attack. However this would mean we could not
828 * connect to any server which doesn't support RI.
829 *
David Benjamine9cddb82015-11-23 14:36:40 -0500830 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
831 * practical terms every client sets it so it's just assumed here. */
832 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700833 }
834
835 const size_t expected_len = ssl->s3->previous_client_finished_len +
836 ssl->s3->previous_server_finished_len;
837
838 /* Check for logic errors */
839 assert(!expected_len || ssl->s3->previous_client_finished_len);
840 assert(!expected_len || ssl->s3->previous_server_finished_len);
841
842 /* Parse out the extension contents. */
843 CBS renegotiated_connection;
844 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
845 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400846 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700847 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
848 return 0;
849 }
850
851 /* Check that the extension matches. */
852 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400853 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700854 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
855 return 0;
856 }
857
858 const uint8_t *d = CBS_data(&renegotiated_connection);
859 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
860 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400861 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700862 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
863 return 0;
864 }
865 d += ssl->s3->previous_client_finished_len;
866
867 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
868 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400869 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700870 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
871 return 0;
872 }
873 ssl->s3->send_connection_binding = 1;
874
875 return 1;
876}
877
878static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
879 CBS *contents) {
880 /* Renegotiation isn't supported as a server so this function should never be
881 * called after the initial handshake. */
882 assert(!ssl->s3->initial_handshake_complete);
883
Steven Valdez143e8b32016-07-11 13:19:03 -0400884 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
885 return 1;
886 }
887
Adam Langley5021b222015-06-12 18:27:58 -0700888 CBS fake_contents;
889 static const uint8_t kFakeExtension[] = {0};
890
891 if (contents == NULL) {
892 if (ssl->s3->send_connection_binding) {
893 /* The renegotiation SCSV was received so pretend that we received a
894 * renegotiation extension. */
895 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
896 contents = &fake_contents;
897 /* We require that the renegotiation extension is at index zero of
898 * kExtensions. */
899 ssl->s3->tmp.extensions.received |= (1u << 0);
900 } else {
901 return 1;
902 }
903 }
904
905 CBS renegotiated_connection;
906
907 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
908 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400909 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700910 return 0;
911 }
912
913 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500914 if (!CBS_mem_equal(&renegotiated_connection,
915 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700916 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400917 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700918 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
919 return 0;
920 }
921
922 ssl->s3->send_connection_binding = 1;
923
924 return 1;
925}
926
927static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400928 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
929 return 1;
930 }
931
Adam Langley5021b222015-06-12 18:27:58 -0700932 CBB contents, prev_finished;
933 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
934 !CBB_add_u16_length_prefixed(out, &contents) ||
935 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
936 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
937 ssl->s3->previous_client_finished_len) ||
938 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
939 ssl->s3->previous_server_finished_len) ||
940 !CBB_flush(out)) {
941 return 0;
942 }
943
944 return 1;
945}
946
Adam Langley0a056712015-07-01 15:03:33 -0700947
948/* Extended Master Secret.
949 *
David Benjamin43946d42016-02-01 08:42:19 -0500950 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700951
952static void ext_ems_init(SSL *ssl) {
953 ssl->s3->tmp.extended_master_secret = 0;
954}
955
956static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
957 if (ssl->version == SSL3_VERSION) {
958 return 1;
959 }
960
961 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
962 !CBB_add_u16(out, 0 /* length */)) {
963 return 0;
964 }
965
966 return 1;
967}
968
969static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
970 CBS *contents) {
971 if (contents == NULL) {
972 return 1;
973 }
974
Steven Valdez143e8b32016-07-11 13:19:03 -0400975 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
976 ssl->version == SSL3_VERSION) {
977 return 0;
978 }
979
980 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700981 return 0;
982 }
983
984 ssl->s3->tmp.extended_master_secret = 1;
985 return 1;
986}
987
David Benjamin0d56f882015-12-19 17:05:56 -0500988static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
989 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400990 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
991 ssl->version == SSL3_VERSION) {
992 return 1;
993 }
994
995 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700996 return 1;
997 }
998
999 if (CBS_len(contents) != 0) {
1000 return 0;
1001 }
1002
1003 ssl->s3->tmp.extended_master_secret = 1;
1004 return 1;
1005}
1006
1007static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1008 if (!ssl->s3->tmp.extended_master_secret) {
1009 return 1;
1010 }
1011
1012 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1013 !CBB_add_u16(out, 0 /* length */)) {
1014 return 0;
1015 }
1016
1017 return 1;
1018}
1019
Adam Langley9b05bc52015-07-01 15:25:33 -07001020
1021/* Session tickets.
1022 *
1023 * https://tools.ietf.org/html/rfc5077 */
1024
1025static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1026 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1027 return 1;
1028 }
1029
1030 const uint8_t *ticket_data = NULL;
1031 int ticket_len = 0;
1032
1033 /* Renegotiation does not participate in session resumption. However, still
1034 * advertise the extension to avoid potentially breaking servers which carry
1035 * over the state from the previous handshake, such as OpenSSL servers
1036 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1037 if (!ssl->s3->initial_handshake_complete &&
1038 ssl->session != NULL &&
1039 ssl->session->tlsext_tick != NULL) {
1040 ticket_data = ssl->session->tlsext_tick;
1041 ticket_len = ssl->session->tlsext_ticklen;
1042 }
1043
1044 CBB ticket;
1045 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1046 !CBB_add_u16_length_prefixed(out, &ticket) ||
1047 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1048 !CBB_flush(out)) {
1049 return 0;
1050 }
1051
1052 return 1;
1053}
1054
1055static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1056 CBS *contents) {
1057 ssl->tlsext_ticket_expected = 0;
1058
1059 if (contents == NULL) {
1060 return 1;
1061 }
1062
Steven Valdez143e8b32016-07-11 13:19:03 -04001063 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1064 return 0;
1065 }
1066
Adam Langley9b05bc52015-07-01 15:25:33 -07001067 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1068 * this function should never be called, even if the server tries to send the
1069 * extension. */
1070 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1071
1072 if (CBS_len(contents) != 0) {
1073 return 0;
1074 }
1075
1076 ssl->tlsext_ticket_expected = 1;
1077 return 1;
1078}
1079
Adam Langley9b05bc52015-07-01 15:25:33 -07001080static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1081 if (!ssl->tlsext_ticket_expected) {
1082 return 1;
1083 }
1084
1085 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1086 * true. */
1087 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1088
1089 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1090 !CBB_add_u16(out, 0 /* length */)) {
1091 return 0;
1092 }
1093
1094 return 1;
1095}
1096
1097
Adam Langley2e857bd2015-07-01 16:09:19 -07001098/* Signature Algorithms.
1099 *
1100 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1101
1102static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjaminb6a0a512016-06-21 10:33:21 -04001103 if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001104 return 1;
1105 }
1106
Steven Valdez02563852016-06-23 13:33:05 -04001107 const uint16_t *sigalgs_data;
Adam Langley2e857bd2015-07-01 16:09:19 -07001108 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1109
1110 CBB contents, sigalgs;
1111 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1112 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez02563852016-06-23 13:33:05 -04001113 !CBB_add_u16_length_prefixed(&contents, &sigalgs)) {
1114 return 0;
1115 }
1116
1117 size_t i;
1118 for (i = 0; i < sigalgs_len; i++) {
1119 if (!CBB_add_u16(&sigalgs, sigalgs_data[i])) {
1120 return 0;
1121 }
1122 }
1123
1124 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001125 return 0;
1126 }
1127
1128 return 1;
1129}
1130
Adam Langley2e857bd2015-07-01 16:09:19 -07001131static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1132 CBS *contents) {
1133 OPENSSL_free(ssl->cert->peer_sigalgs);
1134 ssl->cert->peer_sigalgs = NULL;
1135 ssl->cert->peer_sigalgslen = 0;
1136
Adam Langley2e857bd2015-07-01 16:09:19 -07001137 if (contents == NULL) {
1138 return 1;
1139 }
1140
1141 CBS supported_signature_algorithms;
1142 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001143 CBS_len(contents) != 0 ||
1144 CBS_len(&supported_signature_algorithms) == 0 ||
1145 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001146 return 0;
1147 }
1148
1149 return 1;
1150}
1151
Adam Langley2e857bd2015-07-01 16:09:19 -07001152
Adam Langleybb0bd042015-07-01 16:21:03 -07001153/* OCSP Stapling.
1154 *
1155 * https://tools.ietf.org/html/rfc6066#section-8 */
1156
1157static void ext_ocsp_init(SSL *ssl) {
1158 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001159 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001160}
1161
1162static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1163 if (!ssl->ocsp_stapling_enabled) {
1164 return 1;
1165 }
1166
1167 CBB contents;
1168 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1169 !CBB_add_u16_length_prefixed(out, &contents) ||
1170 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1171 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1172 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1173 !CBB_flush(out)) {
1174 return 0;
1175 }
1176
Adam Langleyce9d85e2016-01-24 15:58:39 -08001177 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001178 return 1;
1179}
1180
1181static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001182 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001183 if (contents == NULL) {
1184 return 1;
1185 }
1186
1187 if (CBS_len(contents) != 0) {
1188 return 0;
1189 }
1190
1191 ssl->s3->tmp.certificate_status_expected = 1;
1192 return 1;
1193}
1194
1195static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1196 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001197 if (contents == NULL) {
1198 return 1;
1199 }
1200
1201 uint8_t status_type;
1202 if (!CBS_get_u8(contents, &status_type)) {
1203 return 0;
1204 }
1205
1206 /* We cannot decide whether OCSP stapling will occur yet because the correct
1207 * SSL_CTX might not have been selected. */
1208 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1209
Adam Langleybb0bd042015-07-01 16:21:03 -07001210 return 1;
1211}
1212
1213static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001214 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1215 return 1;
1216 }
1217
Paul Lietar62be8ac2015-09-16 10:03:30 +01001218 /* The extension shouldn't be sent when resuming sessions. */
1219 if (ssl->hit ||
1220 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001221 ssl->ctx->ocsp_response_length == 0) {
1222 return 1;
1223 }
1224
1225 ssl->s3->tmp.certificate_status_expected = 1;
1226
1227 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1228 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001229}
1230
1231
Adam Langley97dfcbf2015-07-01 18:35:20 -07001232/* Next protocol negotiation.
1233 *
1234 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1235
1236static void ext_npn_init(SSL *ssl) {
1237 ssl->s3->next_proto_neg_seen = 0;
1238}
1239
1240static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1241 if (ssl->s3->initial_handshake_complete ||
1242 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001243 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001244 SSL_IS_DTLS(ssl)) {
1245 return 1;
1246 }
1247
1248 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1249 !CBB_add_u16(out, 0 /* length */)) {
1250 return 0;
1251 }
1252
1253 return 1;
1254}
1255
1256static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1257 CBS *contents) {
1258 if (contents == NULL) {
1259 return 1;
1260 }
1261
Steven Valdez143e8b32016-07-11 13:19:03 -04001262 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1263 return 0;
1264 }
1265
Adam Langley97dfcbf2015-07-01 18:35:20 -07001266 /* If any of these are false then we should never have sent the NPN
1267 * extension in the ClientHello and thus this function should never have been
1268 * called. */
1269 assert(!ssl->s3->initial_handshake_complete);
1270 assert(!SSL_IS_DTLS(ssl));
1271 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001272 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001273
David Benjamin76c2efc2015-08-31 14:24:29 -04001274 if (ssl->s3->alpn_selected != NULL) {
1275 /* NPN and ALPN may not be negotiated in the same connection. */
1276 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1277 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1278 return 0;
1279 }
1280
Adam Langley97dfcbf2015-07-01 18:35:20 -07001281 const uint8_t *const orig_contents = CBS_data(contents);
1282 const size_t orig_len = CBS_len(contents);
1283
1284 while (CBS_len(contents) != 0) {
1285 CBS proto;
1286 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1287 CBS_len(&proto) == 0) {
1288 return 0;
1289 }
1290 }
1291
1292 uint8_t *selected;
1293 uint8_t selected_len;
1294 if (ssl->ctx->next_proto_select_cb(
1295 ssl, &selected, &selected_len, orig_contents, orig_len,
1296 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1297 *out_alert = SSL_AD_INTERNAL_ERROR;
1298 return 0;
1299 }
1300
David Benjamin79978df2015-12-25 15:56:49 -05001301 OPENSSL_free(ssl->s3->next_proto_negotiated);
1302 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1303 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001304 *out_alert = SSL_AD_INTERNAL_ERROR;
1305 return 0;
1306 }
1307
David Benjamin79978df2015-12-25 15:56:49 -05001308 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001309 ssl->s3->next_proto_neg_seen = 1;
1310
1311 return 1;
1312}
1313
1314static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1315 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001316 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1317 return 1;
1318 }
1319
Adam Langley97dfcbf2015-07-01 18:35:20 -07001320 if (contents != NULL && CBS_len(contents) != 0) {
1321 return 0;
1322 }
1323
1324 if (contents == NULL ||
1325 ssl->s3->initial_handshake_complete ||
1326 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1327 * afterwards, parsing the ALPN extension will clear
1328 * |next_proto_neg_seen|. */
1329 ssl->s3->alpn_selected != NULL ||
1330 ssl->ctx->next_protos_advertised_cb == NULL ||
1331 SSL_IS_DTLS(ssl)) {
1332 return 1;
1333 }
1334
1335 ssl->s3->next_proto_neg_seen = 1;
1336 return 1;
1337}
1338
1339static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1340 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1341 * parsed. */
1342 if (!ssl->s3->next_proto_neg_seen) {
1343 return 1;
1344 }
1345
1346 const uint8_t *npa;
1347 unsigned npa_len;
1348
1349 if (ssl->ctx->next_protos_advertised_cb(
1350 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1351 SSL_TLSEXT_ERR_OK) {
1352 ssl->s3->next_proto_neg_seen = 0;
1353 return 1;
1354 }
1355
1356 CBB contents;
1357 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1358 !CBB_add_u16_length_prefixed(out, &contents) ||
1359 !CBB_add_bytes(&contents, npa, npa_len) ||
1360 !CBB_flush(out)) {
1361 return 0;
1362 }
1363
1364 return 1;
1365}
1366
1367
Adam Langleyab8d87d2015-07-10 12:21:39 -07001368/* Signed certificate timestamps.
1369 *
1370 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1371
1372static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1373 if (!ssl->signed_cert_timestamps_enabled) {
1374 return 1;
1375 }
1376
1377 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1378 !CBB_add_u16(out, 0 /* length */)) {
1379 return 0;
1380 }
1381
1382 return 1;
1383}
1384
1385static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1386 CBS *contents) {
1387 if (contents == NULL) {
1388 return 1;
1389 }
1390
1391 /* If this is false then we should never have sent the SCT extension in the
1392 * ClientHello and thus this function should never have been called. */
1393 assert(ssl->signed_cert_timestamps_enabled);
1394
1395 if (CBS_len(contents) == 0) {
1396 *out_alert = SSL_AD_DECODE_ERROR;
1397 return 0;
1398 }
1399
1400 /* Session resumption uses the original session information. */
1401 if (!ssl->hit &&
1402 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1403 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1404 *out_alert = SSL_AD_INTERNAL_ERROR;
1405 return 0;
1406 }
1407
1408 return 1;
1409}
1410
1411static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1412 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001413 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001414}
1415
1416static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001417 /* The extension shouldn't be sent when resuming sessions. */
1418 if (ssl->hit ||
1419 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001420 return 1;
1421 }
1422
1423 CBB contents;
1424 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1425 CBB_add_u16_length_prefixed(out, &contents) &&
1426 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1427 ssl->ctx->signed_cert_timestamp_list_length) &&
1428 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001429}
1430
1431
Adam Langleyf18e4532015-07-10 13:39:53 -07001432/* Application-level Protocol Negotiation.
1433 *
1434 * https://tools.ietf.org/html/rfc7301 */
1435
1436static void ext_alpn_init(SSL *ssl) {
1437 OPENSSL_free(ssl->s3->alpn_selected);
1438 ssl->s3->alpn_selected = NULL;
1439}
1440
1441static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1442 if (ssl->alpn_client_proto_list == NULL ||
1443 ssl->s3->initial_handshake_complete) {
1444 return 1;
1445 }
1446
1447 CBB contents, proto_list;
1448 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1449 !CBB_add_u16_length_prefixed(out, &contents) ||
1450 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1451 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1452 ssl->alpn_client_proto_list_len) ||
1453 !CBB_flush(out)) {
1454 return 0;
1455 }
1456
1457 return 1;
1458}
1459
1460static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1461 CBS *contents) {
1462 if (contents == NULL) {
1463 return 1;
1464 }
1465
1466 assert(!ssl->s3->initial_handshake_complete);
1467 assert(ssl->alpn_client_proto_list != NULL);
1468
David Benjamin76c2efc2015-08-31 14:24:29 -04001469 if (ssl->s3->next_proto_neg_seen) {
1470 /* NPN and ALPN may not be negotiated in the same connection. */
1471 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1472 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1473 return 0;
1474 }
1475
Adam Langleyf18e4532015-07-10 13:39:53 -07001476 /* The extension data consists of a ProtocolNameList which must have
1477 * exactly one ProtocolName. Each of these is length-prefixed. */
1478 CBS protocol_name_list, protocol_name;
1479 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1480 CBS_len(contents) != 0 ||
1481 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1482 /* Empty protocol names are forbidden. */
1483 CBS_len(&protocol_name) == 0 ||
1484 CBS_len(&protocol_name_list) != 0) {
1485 return 0;
1486 }
1487
1488 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1489 &ssl->s3->alpn_selected_len)) {
1490 *out_alert = SSL_AD_INTERNAL_ERROR;
1491 return 0;
1492 }
1493
1494 return 1;
1495}
1496
1497static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1498 CBS *contents) {
1499 if (contents == NULL) {
1500 return 1;
1501 }
1502
1503 if (ssl->ctx->alpn_select_cb == NULL ||
1504 ssl->s3->initial_handshake_complete) {
1505 return 1;
1506 }
1507
1508 /* ALPN takes precedence over NPN. */
1509 ssl->s3->next_proto_neg_seen = 0;
1510
1511 CBS protocol_name_list;
1512 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1513 CBS_len(contents) != 0 ||
1514 CBS_len(&protocol_name_list) < 2) {
1515 return 0;
1516 }
1517
1518 /* Validate the protocol list. */
1519 CBS protocol_name_list_copy = protocol_name_list;
1520 while (CBS_len(&protocol_name_list_copy) > 0) {
1521 CBS protocol_name;
1522
1523 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1524 /* Empty protocol names are forbidden. */
1525 CBS_len(&protocol_name) == 0) {
1526 return 0;
1527 }
1528 }
1529
1530 const uint8_t *selected;
1531 uint8_t selected_len;
1532 if (ssl->ctx->alpn_select_cb(
1533 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1534 CBS_len(&protocol_name_list),
1535 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1536 OPENSSL_free(ssl->s3->alpn_selected);
1537 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1538 if (ssl->s3->alpn_selected == NULL) {
1539 *out_alert = SSL_AD_INTERNAL_ERROR;
1540 return 0;
1541 }
1542 ssl->s3->alpn_selected_len = selected_len;
1543 }
1544
1545 return 1;
1546}
1547
1548static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1549 if (ssl->s3->alpn_selected == NULL) {
1550 return 1;
1551 }
1552
1553 CBB contents, proto_list, proto;
1554 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1555 !CBB_add_u16_length_prefixed(out, &contents) ||
1556 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1557 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001558 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1559 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001560 !CBB_flush(out)) {
1561 return 0;
1562 }
1563
1564 return 1;
1565}
1566
1567
Adam Langley49c7af12015-07-10 14:33:46 -07001568/* Channel ID.
1569 *
1570 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1571
1572static void ext_channel_id_init(SSL *ssl) {
1573 ssl->s3->tlsext_channel_id_valid = 0;
1574}
1575
1576static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1577 if (!ssl->tlsext_channel_id_enabled ||
1578 SSL_IS_DTLS(ssl)) {
1579 return 1;
1580 }
1581
1582 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1583 !CBB_add_u16(out, 0 /* length */)) {
1584 return 0;
1585 }
1586
1587 return 1;
1588}
1589
1590static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1591 CBS *contents) {
1592 if (contents == NULL) {
1593 return 1;
1594 }
1595
Steven Valdez143e8b32016-07-11 13:19:03 -04001596 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1597 return 0;
1598 }
1599
Adam Langley49c7af12015-07-10 14:33:46 -07001600 assert(!SSL_IS_DTLS(ssl));
1601 assert(ssl->tlsext_channel_id_enabled);
1602
1603 if (CBS_len(contents) != 0) {
1604 return 0;
1605 }
1606
1607 ssl->s3->tlsext_channel_id_valid = 1;
1608 return 1;
1609}
1610
1611static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1612 CBS *contents) {
1613 if (contents == NULL ||
1614 !ssl->tlsext_channel_id_enabled ||
1615 SSL_IS_DTLS(ssl)) {
1616 return 1;
1617 }
1618
1619 if (CBS_len(contents) != 0) {
1620 return 0;
1621 }
1622
1623 ssl->s3->tlsext_channel_id_valid = 1;
1624 return 1;
1625}
1626
1627static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001628 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1629 return 1;
1630 }
1631
Adam Langley49c7af12015-07-10 14:33:46 -07001632 if (!ssl->s3->tlsext_channel_id_valid) {
1633 return 1;
1634 }
1635
1636 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1637 !CBB_add_u16(out, 0 /* length */)) {
1638 return 0;
1639 }
1640
1641 return 1;
1642}
1643
Adam Langley391250d2015-07-15 19:06:07 -07001644
1645/* Secure Real-time Transport Protocol (SRTP) extension.
1646 *
1647 * https://tools.ietf.org/html/rfc5764 */
1648
Adam Langley391250d2015-07-15 19:06:07 -07001649
1650static void ext_srtp_init(SSL *ssl) {
1651 ssl->srtp_profile = NULL;
1652}
1653
1654static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1655 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1656 if (profiles == NULL) {
1657 return 1;
1658 }
1659 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1660 if (num_profiles == 0) {
1661 return 1;
1662 }
1663
1664 CBB contents, profile_ids;
1665 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1666 !CBB_add_u16_length_prefixed(out, &contents) ||
1667 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1668 return 0;
1669 }
1670
1671 size_t i;
1672 for (i = 0; i < num_profiles; i++) {
1673 if (!CBB_add_u16(&profile_ids,
1674 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1675 return 0;
1676 }
1677 }
1678
1679 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1680 !CBB_flush(out)) {
1681 return 0;
1682 }
1683
1684 return 1;
1685}
1686
1687static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1688 CBS *contents) {
1689 if (contents == NULL) {
1690 return 1;
1691 }
1692
1693 /* The extension consists of a u16-prefixed profile ID list containing a
1694 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1695 *
1696 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1697 CBS profile_ids, srtp_mki;
1698 uint16_t profile_id;
1699 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1700 !CBS_get_u16(&profile_ids, &profile_id) ||
1701 CBS_len(&profile_ids) != 0 ||
1702 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1703 CBS_len(contents) != 0) {
1704 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1705 return 0;
1706 }
1707
1708 if (CBS_len(&srtp_mki) != 0) {
1709 /* Must be no MKI, since we never offer one. */
1710 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1711 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1712 return 0;
1713 }
1714
1715 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1716
1717 /* Check to see if the server gave us something we support (and presumably
1718 * offered). */
1719 size_t i;
1720 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1721 const SRTP_PROTECTION_PROFILE *profile =
1722 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1723
1724 if (profile->id == profile_id) {
1725 ssl->srtp_profile = profile;
1726 return 1;
1727 }
1728 }
1729
1730 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1731 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1732 return 0;
1733}
1734
1735static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1736 CBS *contents) {
1737 if (contents == NULL) {
1738 return 1;
1739 }
1740
1741 CBS profile_ids, srtp_mki;
1742 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1743 CBS_len(&profile_ids) < 2 ||
1744 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1745 CBS_len(contents) != 0) {
1746 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1747 return 0;
1748 }
1749 /* Discard the MKI value for now. */
1750
1751 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1752 SSL_get_srtp_profiles(ssl);
1753
1754 /* Pick the server's most preferred profile. */
1755 size_t i;
1756 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1757 const SRTP_PROTECTION_PROFILE *server_profile =
1758 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1759
1760 CBS profile_ids_tmp;
1761 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1762
1763 while (CBS_len(&profile_ids_tmp) > 0) {
1764 uint16_t profile_id;
1765 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1766 return 0;
1767 }
1768
1769 if (server_profile->id == profile_id) {
1770 ssl->srtp_profile = server_profile;
1771 return 1;
1772 }
1773 }
1774 }
1775
1776 return 1;
1777}
1778
1779static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1780 if (ssl->srtp_profile == NULL) {
1781 return 1;
1782 }
1783
1784 CBB contents, profile_ids;
1785 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1786 !CBB_add_u16_length_prefixed(out, &contents) ||
1787 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1788 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1789 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1790 !CBB_flush(out)) {
1791 return 0;
1792 }
1793
1794 return 1;
1795}
1796
Adam Langleybdd5d662015-07-20 16:19:08 -07001797
1798/* EC point formats.
1799 *
1800 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1801
1802static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1803 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1804 return 0;
1805 }
1806
1807 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1808
1809 size_t i;
1810 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1811 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1812
1813 const uint32_t alg_k = cipher->algorithm_mkey;
1814 const uint32_t alg_a = cipher->algorithm_auth;
1815 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1816 return 1;
1817 }
1818 }
1819
1820 return 0;
1821}
1822
Adam Langleybdd5d662015-07-20 16:19:08 -07001823static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001824 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001825 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1826 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001827 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1828 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001829 !CBB_flush(out)) {
1830 return 0;
1831 }
1832
1833 return 1;
1834}
1835
1836static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1837 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1838 return 1;
1839 }
1840
1841 return ext_ec_point_add_extension(ssl, out);
1842}
1843
1844static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1845 CBS *contents) {
1846 if (contents == NULL) {
1847 return 1;
1848 }
1849
Steven Valdez143e8b32016-07-11 13:19:03 -04001850 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1851 return 0;
1852 }
1853
Adam Langleybdd5d662015-07-20 16:19:08 -07001854 CBS ec_point_format_list;
1855 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1856 CBS_len(contents) != 0) {
1857 return 0;
1858 }
1859
David Benjaminfc059942015-07-30 23:01:59 -04001860 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1861 * point format. */
1862 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1863 CBS_len(&ec_point_format_list)) == NULL) {
1864 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001865 return 0;
1866 }
1867
1868 return 1;
1869}
1870
1871static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1872 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001873 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1874 return 1;
1875 }
1876
Adam Langleybdd5d662015-07-20 16:19:08 -07001877 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1878}
1879
1880static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001881 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1882 return 1;
1883 }
1884
Adam Langleybdd5d662015-07-20 16:19:08 -07001885 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1886 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001887 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001888
1889 if (!using_ecc) {
1890 return 1;
1891 }
1892
1893 return ext_ec_point_add_extension(ssl, out);
1894}
1895
Adam Langley273d49c2015-07-20 16:38:52 -07001896
Steven Valdez143e8b32016-07-11 13:19:03 -04001897/* Draft Version Extension */
1898
1899static int ext_draft_version_add_clienthello(SSL *ssl, CBB *out) {
1900 uint16_t min_version, max_version;
1901 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
1902 max_version >= TLS1_3_VERSION) {
1903 return 1;
1904 }
1905
1906 CBB contents;
1907 if (!CBB_add_u16(out, TLSEXT_TYPE_draft_version) ||
1908 !CBB_add_u16_length_prefixed(out, &contents) ||
1909 !CBB_add_u16(&contents, TLS1_3_DRAFT_VERSION)) {
1910 return 0;
1911 }
1912
1913 return CBB_flush(out);
1914}
1915
1916
1917/* Key Share
1918 *
1919 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
1920
1921static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
1922 uint16_t min_version, max_version;
1923 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1924 return 0;
1925 }
1926
1927 if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
1928 return 1;
1929 }
1930
1931 CBB contents, kse_bytes;
1932 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
1933 !CBB_add_u16_length_prefixed(out, &contents) ||
1934 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
1935 return 0;
1936 }
1937
1938 const uint16_t *groups;
1939 size_t groups_len;
1940 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
1941
1942 ssl->s3->hs->groups = OPENSSL_malloc(groups_len * sizeof(SSL_ECDH_CTX));
1943 if (ssl->s3->hs->groups == NULL) {
1944 return 0;
1945 }
1946 memset(ssl->s3->hs->groups, 0, groups_len * sizeof(SSL_ECDH_CTX));
1947 ssl->s3->hs->groups_len = groups_len;
1948
1949 for (size_t i = 0; i < groups_len; i++) {
1950 if (!CBB_add_u16(&kse_bytes, groups[i])) {
1951 return 0;
1952 }
1953
1954 CBB key_exchange;
1955 if (!CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
1956 !SSL_ECDH_CTX_init(&ssl->s3->hs->groups[i], groups[i]) ||
1957 !SSL_ECDH_CTX_offer(&ssl->s3->hs->groups[i], &key_exchange) ||
1958 !CBB_flush(&kse_bytes)) {
1959 return 0;
1960 }
1961 }
1962
1963 return CBB_flush(out);
1964}
1965
1966int ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
1967 size_t *out_secret_len, uint8_t *out_alert,
1968 CBS *contents) {
1969 CBS peer_key;
1970 uint16_t group;
1971 if (!CBS_get_u16(contents, &group) ||
1972 !CBS_get_u16_length_prefixed(contents, &peer_key)) {
1973 *out_alert = SSL_AD_DECODE_ERROR;
1974 return 0;
1975 }
1976
1977 SSL_ECDH_CTX *group_ctx = NULL;
1978 for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) {
1979 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->groups[i]) == group) {
1980 group_ctx = &ssl->s3->hs->groups[i];
1981 break;
1982 }
1983 }
1984
1985 if (group_ctx == NULL) {
1986 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1987 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
1988 return 0;
1989 }
1990
1991 if (!SSL_ECDH_CTX_finish(group_ctx, out_secret, out_secret_len, out_alert,
1992 CBS_data(&peer_key), CBS_len(&peer_key))) {
1993 *out_alert = SSL_AD_INTERNAL_ERROR;
1994 return 0;
1995 }
1996
1997 for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) {
1998 SSL_ECDH_CTX_cleanup(&ssl->s3->hs->groups[i]);
1999 }
2000 OPENSSL_free(ssl->s3->hs->groups);
2001 ssl->s3->hs->groups = NULL;
2002
2003 return 1;
2004}
2005
2006int ext_key_share_parse_clienthello(SSL *ssl, uint8_t **out_secret,
2007 size_t *out_secret_len, uint8_t *out_alert,
2008 CBS *contents) {
2009 uint16_t group_id;
2010 CBS key_shares;
2011 if (!tls1_get_shared_group(ssl, &group_id) ||
2012 !CBS_get_u16_length_prefixed(contents, &key_shares)) {
2013 return 0;
2014 }
2015
2016 int found = 0;
2017 while (CBS_len(&key_shares) > 0) {
2018 uint16_t id;
2019 CBS peer_key;
2020 if (!CBS_get_u16(&key_shares, &id) ||
2021 !CBS_get_u16_length_prefixed(&key_shares, &peer_key)) {
2022 return 0;
2023 }
2024
2025 if (id != group_id || found) {
2026 continue;
2027 }
2028
2029 SSL_ECDH_CTX group;
2030 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2031 CBB public_key;
2032 if (!CBB_init(&public_key, 0) ||
2033 !SSL_ECDH_CTX_init(&group, group_id) ||
2034 !SSL_ECDH_CTX_accept(&group, &public_key, out_secret, out_secret_len,
2035 out_alert, CBS_data(&peer_key),
2036 CBS_len(&peer_key)) ||
2037 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2038 &ssl->s3->hs->public_key_len)) {
2039 SSL_ECDH_CTX_cleanup(&group);
2040 CBB_cleanup(&public_key);
2041 return 0;
2042 }
2043 SSL_ECDH_CTX_cleanup(&group);
2044
2045 found = 1;
2046 }
2047
2048 return found;
2049}
2050
2051int ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
2052 if (ssl->s3->tmp.new_cipher->algorithm_mkey != SSL_kECDHE) {
2053 return 1;
2054 }
2055
2056 uint16_t group_id;
2057 CBB kse_bytes, public_key;
2058 if (!tls1_get_shared_group(ssl, &group_id) ||
2059 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2060 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2061 !CBB_add_u16(&kse_bytes, group_id) ||
2062 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2063 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2064 ssl->s3->hs->public_key_len) ||
2065 !CBB_flush(out)) {
2066 return 0;
2067 }
2068
2069 return 1;
2070}
2071
2072
Steven Valdezce902a92016-05-17 11:47:53 -04002073/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002074 *
Steven Valdezce902a92016-05-17 11:47:53 -04002075 * https://tools.ietf.org/html/rfc4492#section-5.1.2
2076 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07002077
Steven Valdezce902a92016-05-17 11:47:53 -04002078static void ext_supported_groups_init(SSL *ssl) {
2079 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2080 ssl->s3->tmp.peer_supported_group_list = NULL;
2081 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07002082}
2083
Steven Valdezce902a92016-05-17 11:47:53 -04002084static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002085 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2086 return 1;
2087 }
2088
Steven Valdezce902a92016-05-17 11:47:53 -04002089 CBB contents, groups_bytes;
2090 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002091 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002092 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002093 return 0;
2094 }
2095
Steven Valdezce902a92016-05-17 11:47:53 -04002096 const uint16_t *groups;
2097 size_t groups_len;
2098 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002099
2100 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002101 for (i = 0; i < groups_len; i++) {
2102 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002103 return 0;
2104 }
2105 }
2106
2107 return CBB_flush(out);
2108}
2109
Steven Valdezce902a92016-05-17 11:47:53 -04002110static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2111 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002112 /* This extension is not expected to be echoed by servers and is ignored. */
2113 return 1;
2114}
2115
Steven Valdezce902a92016-05-17 11:47:53 -04002116static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2117 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002118 if (contents == NULL) {
2119 return 1;
2120 }
2121
Steven Valdezce902a92016-05-17 11:47:53 -04002122 CBS supported_group_list;
2123 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2124 CBS_len(&supported_group_list) == 0 ||
2125 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002126 CBS_len(contents) != 0) {
2127 return 0;
2128 }
2129
Steven Valdezce902a92016-05-17 11:47:53 -04002130 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
2131 CBS_len(&supported_group_list));
2132 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002133 *out_alert = SSL_AD_INTERNAL_ERROR;
2134 return 0;
2135 }
2136
Steven Valdezce902a92016-05-17 11:47:53 -04002137 const size_t num_groups = CBS_len(&supported_group_list) / 2;
Adam Langley273d49c2015-07-20 16:38:52 -07002138 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002139 for (i = 0; i < num_groups; i++) {
2140 if (!CBS_get_u16(&supported_group_list,
2141 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002142 goto err;
2143 }
2144 }
2145
Steven Valdezce902a92016-05-17 11:47:53 -04002146 assert(CBS_len(&supported_group_list) == 0);
2147 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002148
2149 return 1;
2150
2151err:
Steven Valdezce902a92016-05-17 11:47:53 -04002152 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2153 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002154 *out_alert = SSL_AD_INTERNAL_ERROR;
2155 return 0;
2156}
2157
Steven Valdezce902a92016-05-17 11:47:53 -04002158static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002159 /* Servers don't echo this extension. */
2160 return 1;
2161}
2162
2163
Adam Langley614c66a2015-06-12 15:26:58 -07002164/* kExtensions contains all the supported extensions. */
2165static const struct tls_extension kExtensions[] = {
2166 {
Adam Langley5021b222015-06-12 18:27:58 -07002167 /* The renegotiation extension must always be at index zero because the
2168 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2169 * sent as an SCSV. */
2170 TLSEXT_TYPE_renegotiate,
2171 NULL,
2172 ext_ri_add_clienthello,
2173 ext_ri_parse_serverhello,
2174 ext_ri_parse_clienthello,
2175 ext_ri_add_serverhello,
2176 },
2177 {
Adam Langley614c66a2015-06-12 15:26:58 -07002178 TLSEXT_TYPE_server_name,
2179 ext_sni_init,
2180 ext_sni_add_clienthello,
2181 ext_sni_parse_serverhello,
2182 ext_sni_parse_clienthello,
2183 ext_sni_add_serverhello,
2184 },
Adam Langley0a056712015-07-01 15:03:33 -07002185 {
2186 TLSEXT_TYPE_extended_master_secret,
2187 ext_ems_init,
2188 ext_ems_add_clienthello,
2189 ext_ems_parse_serverhello,
2190 ext_ems_parse_clienthello,
2191 ext_ems_add_serverhello,
2192 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002193 {
2194 TLSEXT_TYPE_session_ticket,
2195 NULL,
2196 ext_ticket_add_clienthello,
2197 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002198 /* Ticket extension client parsing is handled in ssl_session.c */
2199 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002200 ext_ticket_add_serverhello,
2201 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002202 {
2203 TLSEXT_TYPE_signature_algorithms,
2204 NULL,
2205 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002206 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002207 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002208 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002209 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002210 {
2211 TLSEXT_TYPE_status_request,
2212 ext_ocsp_init,
2213 ext_ocsp_add_clienthello,
2214 ext_ocsp_parse_serverhello,
2215 ext_ocsp_parse_clienthello,
2216 ext_ocsp_add_serverhello,
2217 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002218 {
2219 TLSEXT_TYPE_next_proto_neg,
2220 ext_npn_init,
2221 ext_npn_add_clienthello,
2222 ext_npn_parse_serverhello,
2223 ext_npn_parse_clienthello,
2224 ext_npn_add_serverhello,
2225 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002226 {
2227 TLSEXT_TYPE_certificate_timestamp,
2228 NULL,
2229 ext_sct_add_clienthello,
2230 ext_sct_parse_serverhello,
2231 ext_sct_parse_clienthello,
2232 ext_sct_add_serverhello,
2233 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002234 {
2235 TLSEXT_TYPE_application_layer_protocol_negotiation,
2236 ext_alpn_init,
2237 ext_alpn_add_clienthello,
2238 ext_alpn_parse_serverhello,
2239 ext_alpn_parse_clienthello,
2240 ext_alpn_add_serverhello,
2241 },
Adam Langley49c7af12015-07-10 14:33:46 -07002242 {
2243 TLSEXT_TYPE_channel_id,
2244 ext_channel_id_init,
2245 ext_channel_id_add_clienthello,
2246 ext_channel_id_parse_serverhello,
2247 ext_channel_id_parse_clienthello,
2248 ext_channel_id_add_serverhello,
2249 },
Adam Langley391250d2015-07-15 19:06:07 -07002250 {
2251 TLSEXT_TYPE_srtp,
2252 ext_srtp_init,
2253 ext_srtp_add_clienthello,
2254 ext_srtp_parse_serverhello,
2255 ext_srtp_parse_clienthello,
2256 ext_srtp_add_serverhello,
2257 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002258 {
2259 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002260 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002261 ext_ec_point_add_clienthello,
2262 ext_ec_point_parse_serverhello,
2263 ext_ec_point_parse_clienthello,
2264 ext_ec_point_add_serverhello,
2265 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002266 {
2267 TLSEXT_TYPE_draft_version,
2268 NULL,
2269 ext_draft_version_add_clienthello,
2270 forbid_parse_serverhello,
2271 ignore_parse_clienthello,
2272 dont_add_serverhello,
2273 },
2274 {
2275 TLSEXT_TYPE_key_share,
2276 NULL,
2277 ext_key_share_add_clienthello,
2278 forbid_parse_serverhello,
2279 ignore_parse_clienthello,
2280 dont_add_serverhello,
2281 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002282 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2283 * intolerant to the last extension being zero-length. See
2284 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002285 {
Steven Valdezce902a92016-05-17 11:47:53 -04002286 TLSEXT_TYPE_supported_groups,
2287 ext_supported_groups_init,
2288 ext_supported_groups_add_clienthello,
2289 ext_supported_groups_parse_serverhello,
2290 ext_supported_groups_parse_clienthello,
2291 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002292 },
Adam Langley614c66a2015-06-12 15:26:58 -07002293};
2294
2295#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2296
Adam Langley4cfa96b2015-07-01 11:56:55 -07002297OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2298 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002299 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002300OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2301 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2302 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002303 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002304
Adam Langley614c66a2015-06-12 15:26:58 -07002305static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2306 uint16_t value) {
2307 unsigned i;
2308 for (i = 0; i < kNumExtensions; i++) {
2309 if (kExtensions[i].value == value) {
2310 *out_index = i;
2311 return &kExtensions[i];
2312 }
2313 }
2314
2315 return NULL;
2316}
2317
Adam Langley09505632015-07-30 18:10:13 -07002318int SSL_extension_supported(unsigned extension_value) {
2319 uint32_t index;
2320 return extension_value == TLSEXT_TYPE_padding ||
2321 tls_extension_find(&index, extension_value) != NULL;
2322}
2323
David Benjamine8d53502015-10-10 14:13:23 -04002324int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002325 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002326 if (ssl->client_version == SSL3_VERSION &&
2327 !ssl->s3->send_connection_binding) {
2328 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002329 }
Adam Langley95c29f32014-06-20 12:00:00 -07002330
David Benjamine8d53502015-10-10 14:13:23 -04002331 CBB extensions;
2332 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002333 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002334 }
Adam Langley95c29f32014-06-20 12:00:00 -07002335
David Benjamine8d53502015-10-10 14:13:23 -04002336 ssl->s3->tmp.extensions.sent = 0;
2337 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002338
Adam Langley614c66a2015-06-12 15:26:58 -07002339 size_t i;
2340 for (i = 0; i < kNumExtensions; i++) {
2341 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002342 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002343 }
2344 }
Adam Langley95c29f32014-06-20 12:00:00 -07002345
Adam Langley614c66a2015-06-12 15:26:58 -07002346 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002347 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002348 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002349 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2350 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2351 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002352 }
Adam Langley95c29f32014-06-20 12:00:00 -07002353
Adam Langley33ad2b52015-07-20 17:43:53 -07002354 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002355 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002356 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002357 }
Adam Langley75712922014-10-10 16:23:43 -07002358
David Benjamine8d53502015-10-10 14:13:23 -04002359 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002360 goto err;
2361 }
2362
David Benjamine8d53502015-10-10 14:13:23 -04002363 if (!SSL_IS_DTLS(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002364 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002365 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002366 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002367 *
2368 * NB: because this code works out the length of all existing extensions
2369 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002370 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002371 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002372 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002373 * Server 7.0 is intolerant to the last extension being zero-length. See
2374 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002375 if (padding_len >= 4 + 1) {
2376 padding_len -= 4;
2377 } else {
2378 padding_len = 1;
2379 }
Adam Langley95c29f32014-06-20 12:00:00 -07002380
Adam Langley33ad2b52015-07-20 17:43:53 -07002381 uint8_t *padding_bytes;
2382 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2383 !CBB_add_u16(&extensions, padding_len) ||
2384 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2385 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002386 }
Adam Langley75712922014-10-10 16:23:43 -07002387
Adam Langley33ad2b52015-07-20 17:43:53 -07002388 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002389 }
2390 }
Adam Langley75712922014-10-10 16:23:43 -07002391
David Benjamina01deee2015-12-08 18:56:31 -05002392 /* Discard empty extensions blocks. */
2393 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002394 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002395 }
2396
David Benjamine8d53502015-10-10 14:13:23 -04002397 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002398
2399err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002400 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002401 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002402}
Adam Langley95c29f32014-06-20 12:00:00 -07002403
David Benjamin56380462015-10-10 14:59:09 -04002404int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002405 CBB extensions;
2406 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002407 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002408 }
2409
2410 unsigned i;
2411 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002412 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002413 /* Don't send extensions that were not received. */
2414 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002415 }
Adam Langley95c29f32014-06-20 12:00:00 -07002416
David Benjamin56380462015-10-10 14:59:09 -04002417 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002418 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2419 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2420 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002421 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002422 }
Adam Langley95c29f32014-06-20 12:00:00 -07002423
David Benjamin56380462015-10-10 14:59:09 -04002424 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002425 goto err;
2426 }
2427
Steven Valdez143e8b32016-07-11 13:19:03 -04002428 /* Discard empty extensions blocks before TLS 1.3. */
2429 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2430 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002431 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002432 }
2433
David Benjamin56380462015-10-10 14:59:09 -04002434 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002435
2436err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002437 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002438 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002439}
Adam Langley95c29f32014-06-20 12:00:00 -07002440
David Benjamin0d56f882015-12-19 17:05:56 -05002441static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002442 size_t i;
2443 for (i = 0; i < kNumExtensions; i++) {
2444 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002445 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002446 }
2447 }
2448
David Benjamin0d56f882015-12-19 17:05:56 -05002449 ssl->s3->tmp.extensions.received = 0;
2450 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002451 /* The renegotiation extension must always be at index zero because the
2452 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2453 * sent as an SCSV. */
2454 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002455
Adam Langleyfcf25832014-12-18 17:42:32 -08002456 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002457 if (CBS_len(cbs) != 0) {
2458 /* Decode the extensions block and check it is valid. */
2459 CBS extensions;
2460 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2461 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002462 *out_alert = SSL_AD_DECODE_ERROR;
2463 return 0;
2464 }
Adam Langley95c29f32014-06-20 12:00:00 -07002465
Adam Langley33ad2b52015-07-20 17:43:53 -07002466 while (CBS_len(&extensions) != 0) {
2467 uint16_t type;
2468 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002469
Adam Langley33ad2b52015-07-20 17:43:53 -07002470 /* Decode the next extension. */
2471 if (!CBS_get_u16(&extensions, &type) ||
2472 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2473 *out_alert = SSL_AD_DECODE_ERROR;
2474 return 0;
2475 }
2476
David Benjaminc7ce9772015-10-09 19:32:41 -04002477 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2478 * ambiguous. Ignore all but the renegotiation_info extension. */
David Benjamin0d56f882015-12-19 17:05:56 -05002479 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
David Benjaminc7ce9772015-10-09 19:32:41 -04002480 continue;
2481 }
2482
Adam Langley33ad2b52015-07-20 17:43:53 -07002483 unsigned ext_index;
2484 const struct tls_extension *const ext =
2485 tls_extension_find(&ext_index, type);
2486
2487 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002488 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002489 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2490 return 0;
2491 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002492 continue;
2493 }
2494
David Benjamin0d56f882015-12-19 17:05:56 -05002495 ssl->s3->tmp.extensions.received |= (1u << ext_index);
Adam Langley614c66a2015-06-12 15:26:58 -07002496 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002497 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
Adam Langley614c66a2015-06-12 15:26:58 -07002498 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002499 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2500 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002501 return 0;
2502 }
Adam Langley614c66a2015-06-12 15:26:58 -07002503 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002504 }
Adam Langley75712922014-10-10 16:23:43 -07002505
Adam Langley614c66a2015-06-12 15:26:58 -07002506 for (i = 0; i < kNumExtensions; i++) {
David Benjamin0d56f882015-12-19 17:05:56 -05002507 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002508 /* Extension wasn't observed so call the callback with a NULL
2509 * parameter. */
2510 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002511 if (!kExtensions[i].parse_clienthello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002512 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2513 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002514 *out_alert = alert;
2515 return 0;
2516 }
2517 }
2518 }
2519
Adam Langleyfcf25832014-12-18 17:42:32 -08002520 return 1;
2521}
Adam Langley95c29f32014-06-20 12:00:00 -07002522
David Benjamin0d56f882015-12-19 17:05:56 -05002523int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002524 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002525 if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) {
2526 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002527 return 0;
2528 }
Adam Langley95c29f32014-06-20 12:00:00 -07002529
David Benjamin0d56f882015-12-19 17:05:56 -05002530 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002531 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002532 return 0;
2533 }
Adam Langley95c29f32014-06-20 12:00:00 -07002534
Adam Langleyfcf25832014-12-18 17:42:32 -08002535 return 1;
2536}
Adam Langley95c29f32014-06-20 12:00:00 -07002537
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002538OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2539
David Benjamin0d56f882015-12-19 17:05:56 -05002540static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002541 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2542 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2543 return 1;
2544 }
Adam Langley614c66a2015-06-12 15:26:58 -07002545
Steven Valdez143e8b32016-07-11 13:19:03 -04002546 /* Decode the extensions block and check it is valid. */
2547 CBS extensions;
2548 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2549 !tls1_check_duplicate_extensions(&extensions)) {
2550 *out_alert = SSL_AD_DECODE_ERROR;
2551 return 0;
2552 }
2553
2554 uint32_t received = 0;
2555 while (CBS_len(&extensions) != 0) {
2556 uint16_t type;
2557 CBS extension;
2558
2559 /* Decode the next extension. */
2560 if (!CBS_get_u16(&extensions, &type) ||
2561 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002562 *out_alert = SSL_AD_DECODE_ERROR;
2563 return 0;
2564 }
Adam Langley95c29f32014-06-20 12:00:00 -07002565
Steven Valdez143e8b32016-07-11 13:19:03 -04002566 unsigned ext_index;
2567 const struct tls_extension *const ext =
2568 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002569
Steven Valdez143e8b32016-07-11 13:19:03 -04002570 if (ext == NULL) {
2571 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002572 return 0;
2573 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002574 continue;
2575 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002576
Steven Valdez143e8b32016-07-11 13:19:03 -04002577 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) {
2578 /* If the extension was never sent then it is illegal. */
2579 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2580 ERR_add_error_dataf("extension :%u", (unsigned)type);
2581 *out_alert = SSL_AD_DECODE_ERROR;
2582 return 0;
2583 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002584
Steven Valdez143e8b32016-07-11 13:19:03 -04002585 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002586
Steven Valdez143e8b32016-07-11 13:19:03 -04002587 uint8_t alert = SSL_AD_DECODE_ERROR;
2588 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2589 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2590 ERR_add_error_dataf("extension: %u", (unsigned)type);
2591 *out_alert = alert;
2592 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002593 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002594 }
Adam Langley95c29f32014-06-20 12:00:00 -07002595
Adam Langley33ad2b52015-07-20 17:43:53 -07002596 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002597 for (i = 0; i < kNumExtensions; i++) {
2598 if (!(received & (1u << i))) {
2599 /* Extension wasn't observed so call the callback with a NULL
2600 * parameter. */
2601 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002602 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002603 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2604 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002605 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002606 return 0;
2607 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002608 }
2609 }
Adam Langley95c29f32014-06-20 12:00:00 -07002610
Adam Langleyfcf25832014-12-18 17:42:32 -08002611 return 1;
2612}
Adam Langley95c29f32014-06-20 12:00:00 -07002613
David Benjamin0d56f882015-12-19 17:05:56 -05002614static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002615 int ret = SSL_TLSEXT_ERR_NOACK;
2616 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002617
David Benjamin78f8aab2016-03-10 16:33:58 -05002618 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002619 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002620 ssl->ctx->tlsext_servername_arg);
2621 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002622 ret = ssl->initial_ctx->tlsext_servername_callback(
2623 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002624 }
Adam Langley95c29f32014-06-20 12:00:00 -07002625
Adam Langleyfcf25832014-12-18 17:42:32 -08002626 switch (ret) {
2627 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002628 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002629 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002630
Adam Langleyfcf25832014-12-18 17:42:32 -08002631 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002632 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002633 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002634
Adam Langleyfcf25832014-12-18 17:42:32 -08002635 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002636 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002637 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002638
Adam Langleyfcf25832014-12-18 17:42:32 -08002639 default:
2640 return 1;
2641 }
2642}
Adam Langleyed8270a2014-09-02 13:52:56 -07002643
David Benjamin0d56f882015-12-19 17:05:56 -05002644static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002645 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002646 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002647
David Benjamin78f8aab2016-03-10 16:33:58 -05002648 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002649 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002650 ssl->ctx->tlsext_servername_arg);
2651 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002652 ret = ssl->initial_ctx->tlsext_servername_callback(
2653 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002654 }
Adam Langley95c29f32014-06-20 12:00:00 -07002655
Adam Langleyfcf25832014-12-18 17:42:32 -08002656 switch (ret) {
2657 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002658 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002659 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002660
Adam Langleyfcf25832014-12-18 17:42:32 -08002661 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002662 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002663 return 1;
2664
2665 default:
2666 return 1;
2667 }
2668}
2669
David Benjamin0d56f882015-12-19 17:05:56 -05002670int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002671 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002672 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2673 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002674 return 0;
2675 }
2676
David Benjamin0d56f882015-12-19 17:05:56 -05002677 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002678 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002679 return 0;
2680 }
2681
2682 return 1;
2683}
Adam Langley95c29f32014-06-20 12:00:00 -07002684
David Benjamine3aa1d92015-06-16 15:34:50 -04002685int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002686 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002687 size_t ticket_len, const uint8_t *session_id,
2688 size_t session_id_len) {
2689 int ret = 1; /* Most errors are non-fatal. */
2690 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2691 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002692
David Benjamine3aa1d92015-06-16 15:34:50 -04002693 HMAC_CTX hmac_ctx;
2694 HMAC_CTX_init(&hmac_ctx);
2695 EVP_CIPHER_CTX cipher_ctx;
2696 EVP_CIPHER_CTX_init(&cipher_ctx);
2697
David Benjaminef1b0092015-11-21 14:05:44 -05002698 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002699 *out_session = NULL;
2700
2701 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2702 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002703 }
2704
David Benjaminadcc3952015-04-26 13:07:57 -04002705 /* Ensure there is room for the key name and the largest IV
2706 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2707 * the maximum IV length should be well under the minimum size for the
2708 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002709 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2710 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002711 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002712 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002713
David Benjamine3aa1d92015-06-16 15:34:50 -04002714 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002715 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2716 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2717 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002718 if (cb_ret < 0) {
2719 ret = 0;
2720 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002721 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002722 if (cb_ret == 0) {
2723 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002724 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002725 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002726 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002727 }
2728 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002729 /* Check the key name matches. */
2730 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2731 SSL_TICKET_KEY_NAME_LEN) != 0) {
2732 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002733 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002734 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2735 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002736 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002737 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2738 ssl_ctx->tlsext_tick_aes_key, iv)) {
2739 ret = 0;
2740 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002741 }
2742 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002743 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002744
David Benjamine3aa1d92015-06-16 15:34:50 -04002745 /* Check the MAC at the end of the ticket. */
2746 uint8_t mac[EVP_MAX_MD_SIZE];
2747 size_t mac_len = HMAC_size(&hmac_ctx);
2748 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002749 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002750 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002751 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002752 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2753 HMAC_Final(&hmac_ctx, mac, NULL);
2754 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2755 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002756 }
2757
David Benjamine3aa1d92015-06-16 15:34:50 -04002758 /* Decrypt the session data. */
2759 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2760 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2761 mac_len;
2762 plaintext = OPENSSL_malloc(ciphertext_len);
2763 if (plaintext == NULL) {
2764 ret = 0;
2765 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002766 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002767 if (ciphertext_len >= INT_MAX) {
2768 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002769 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002770 int len1, len2;
2771 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2772 (int)ciphertext_len) ||
2773 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2774 ERR_clear_error(); /* Don't leave an error on the queue. */
2775 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002776 }
2777
David Benjamine3aa1d92015-06-16 15:34:50 -04002778 /* Decode the session. */
2779 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2780 if (session == NULL) {
2781 ERR_clear_error(); /* Don't leave an error on the queue. */
2782 goto done;
2783 }
2784
2785 /* Copy the client's session ID into the new session, to denote the ticket has
2786 * been accepted. */
2787 memcpy(session->session_id, session_id, session_id_len);
2788 session->session_id_length = session_id_len;
2789
2790 *out_session = session;
2791
2792done:
2793 OPENSSL_free(plaintext);
2794 HMAC_CTX_cleanup(&hmac_ctx);
2795 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2796 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002797}
Adam Langley95c29f32014-06-20 12:00:00 -07002798
Steven Valdez0d62f262015-09-04 12:41:04 -04002799int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002800 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05002801 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002802 return 1;
2803 }
David Benjamincd996942014-07-20 16:23:51 -04002804
Steven Valdez0d62f262015-09-04 12:41:04 -04002805 CERT *const cert = ssl->cert;
2806 OPENSSL_free(cert->peer_sigalgs);
2807 cert->peer_sigalgs = NULL;
2808 cert->peer_sigalgslen = 0;
2809
2810 size_t num_sigalgs = CBS_len(in_sigalgs);
2811
2812 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002813 return 0;
2814 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002815 num_sigalgs /= 2;
2816
2817 /* supported_signature_algorithms in the certificate request is
2818 * allowed to be empty. */
2819 if (num_sigalgs == 0) {
2820 return 1;
2821 }
2822
Steven Valdez02563852016-06-23 13:33:05 -04002823 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
2824 * and we just divided |num_sigalgs| by two. */
2825 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
Steven Valdez0d62f262015-09-04 12:41:04 -04002826 if (cert->peer_sigalgs == NULL) {
2827 return 0;
2828 }
2829 cert->peer_sigalgslen = num_sigalgs;
2830
2831 CBS sigalgs;
2832 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2833
2834 size_t i;
2835 for (i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04002836 if (!CBS_get_u16(&sigalgs, &cert->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04002837 return 0;
2838 }
2839 }
Adam Langley95c29f32014-06-20 12:00:00 -07002840
Adam Langleyfcf25832014-12-18 17:42:32 -08002841 return 1;
2842}
David Benjaminec2f27d2014-11-13 19:17:25 -05002843
David Benjaminea9a0d52016-07-08 15:52:59 -07002844int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04002845 CERT *cert = ssl->cert;
Steven Valdez0d62f262015-09-04 12:41:04 -04002846 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002847
Steven Valdezf0451ca2016-06-29 13:16:27 -04002848 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
2849 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
2850 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04002851 int type = ssl_private_key_type(ssl);
2852 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07002853 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04002854 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002855 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04002856 if (ssl_is_ecdsa_key_type(type)) {
2857 *out = SSL_SIGN_ECDSA_SHA1;
2858 return 1;
2859 }
2860 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
2861 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002862 }
2863
Steven Valdezeff1e8d2016-07-06 14:24:47 -04002864 const uint16_t *sigalgs;
2865 size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07002866 if (cert->sigalgs != NULL) {
2867 sigalgs = cert->sigalgs;
2868 sigalgs_len = cert->sigalgs_len;
Steven Valdez0d62f262015-09-04 12:41:04 -04002869 }
2870
David Benjaminea9a0d52016-07-08 15:52:59 -07002871 const uint16_t *peer_sigalgs = cert->peer_sigalgs;
2872 size_t peer_sigalgs_len = cert->peer_sigalgslen;
David Benjamin51dd7d62016-07-08 16:07:01 -07002873 if (peer_sigalgs_len == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07002874 /* If the client didn't specify any signature_algorithms extension then
2875 * we can assume that it supports SHA1. See
2876 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
2877 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
2878 SSL_SIGN_ECDSA_SHA1};
2879 peer_sigalgs = kDefaultPeerAlgorithms;
2880 peer_sigalgs_len =
2881 sizeof(kDefaultPeerAlgorithms) / sizeof(kDefaultPeerAlgorithms);
2882 }
2883
David Benjamind246b812016-07-08 15:07:02 -07002884 for (i = 0; i < sigalgs_len; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07002885 uint16_t sigalg = sigalgs[i];
2886 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
2887 * negotiated. */
2888 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
2889 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
2890 continue;
2891 }
2892
David Benjaminea9a0d52016-07-08 15:52:59 -07002893 for (j = 0; j < peer_sigalgs_len; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07002894 if (sigalg == peer_sigalgs[j]) {
2895 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07002896 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002897 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002898 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002899 }
Adam Langley95c29f32014-06-20 12:00:00 -07002900
David Benjaminea9a0d52016-07-08 15:52:59 -07002901 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
2902 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002903}
Adam Langley95c29f32014-06-20 12:00:00 -07002904
David Benjamind6a4ae92015-08-06 11:10:51 -04002905int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2906 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002907 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002908
2909 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002910 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2911 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002912 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002913
David Benjamind6a4ae92015-08-06 11:10:51 -04002914 static const char kClientIDMagic[] = "TLS Channel ID signature";
2915 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2916
2917 if (ssl->hit) {
2918 static const char kResumptionMagic[] = "Resumption";
2919 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2920 if (ssl->session->original_handshake_hash_len == 0) {
2921 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2922 goto err;
2923 }
2924 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2925 ssl->session->original_handshake_hash_len);
2926 }
2927
2928 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2929 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2930 sizeof(handshake_hash));
2931 if (handshake_hash_len < 0) {
2932 goto err;
2933 }
2934 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2935 unsigned len_u;
2936 EVP_DigestFinal_ex(&ctx, out, &len_u);
2937 *out_len = len_u;
2938
2939 ret = 1;
2940
2941err:
2942 EVP_MD_CTX_cleanup(&ctx);
2943 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002944}
Adam Langley1258b6a2014-06-20 12:00:00 -07002945
2946/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin0d56f882015-12-19 17:05:56 -05002947 * hashes in |ssl->session| so that Channel ID resumptions can sign that
2948 * data. */
2949int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002950 int digest_len;
2951 /* This function should never be called for a resumed session because the
2952 * handshake hashes that we wish to record are for the original, full
2953 * handshake. */
David Benjamin0d56f882015-12-19 17:05:56 -05002954 if (ssl->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002955 return -1;
2956 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002957
Adam Langleyfcf25832014-12-18 17:42:32 -08002958 digest_len =
David Benjamin0d56f882015-12-19 17:05:56 -05002959 tls1_handshake_digest(ssl, ssl->session->original_handshake_hash,
2960 sizeof(ssl->session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08002961 if (digest_len < 0) {
2962 return -1;
2963 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002964
David Benjamin0d56f882015-12-19 17:05:56 -05002965 ssl->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002966
Adam Langleyfcf25832014-12-18 17:42:32 -08002967 return 1;
2968}