blob: e733b489de409f5866b7d4722f74414c8e6b38b4 [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[] = {
295 SSL_GROUP_X25519,
296 SSL_GROUP_SECP256R1,
297 SSL_GROUP_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700298#if defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezce902a92016-05-17 11:47:53 -0400299 SSL_GROUP_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) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 int ret = 0;
476 EVP_PKEY *pkey = X509_get_pubkey(x);
Steven Valdezce902a92016-05-17 11:47:53 -0400477 uint16_t group_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 uint8_t comp_id;
479
David Benjamin758d1272015-11-20 17:47:25 -0500480 if (!pkey) {
481 goto done;
482 }
483 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
484 if (ec_key == NULL ||
Steven Valdezce902a92016-05-17 11:47:53 -0400485 !tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) ||
486 !tls1_check_group_id(ssl, group_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400487 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 goto done;
489 }
490
491 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500492
493done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400494 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 return ret;
496}
David Benjamin42e9a772014-09-02 23:18:44 -0400497
Adam Langley95c29f32014-06-20 12:00:00 -0700498/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800499 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Steven Valdez02563852016-06-23 13:33:05 -0400501static const uint16_t kDefaultSignatureAlgorithms[] = {
502 SSL_SIGN_RSA_PKCS1_SHA512,
503 SSL_SIGN_ECDSA_SECP521R1_SHA512,
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Steven Valdez02563852016-06-23 13:33:05 -0400505 SSL_SIGN_RSA_PKCS1_SHA384,
506 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Adam Langley95c29f32014-06-20 12:00:00 -0700507
Steven Valdez02563852016-06-23 13:33:05 -0400508 SSL_SIGN_RSA_PKCS1_SHA256,
509 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Adam Langley95c29f32014-06-20 12:00:00 -0700510
Steven Valdez02563852016-06-23 13:33:05 -0400511 SSL_SIGN_RSA_PKCS1_SHA1,
512 SSL_SIGN_ECDSA_SHA1,
Adam Langley95c29f32014-06-20 12:00:00 -0700513};
David Benjamin05da6e12014-07-12 20:42:55 -0400514
Steven Valdez02563852016-06-23 13:33:05 -0400515size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
516 *psigs = kDefaultSignatureAlgorithms;
517 return sizeof(kDefaultSignatureAlgorithms) /
518 sizeof(kDefaultSignatureAlgorithms[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800519}
Adam Langley95c29f32014-06-20 12:00:00 -0700520
Steven Valdezf0451ca2016-06-29 13:16:27 -0400521static int tls12_get_pkey_type(uint16_t sigalg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800522
Steven Valdezf0451ca2016-06-29 13:16:27 -0400523int tls12_check_peer_sigalg(SSL *ssl, int *out_alert,
524 uint16_t sigalg, EVP_PKEY *pkey) {
Steven Valdez02563852016-06-23 13:33:05 -0400525 const uint16_t *sent_sigs;
526 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800527
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 /* Check key type is consistent with signature */
Steven Valdezf0451ca2016-06-29 13:16:27 -0400529 if (pkey->type != tls12_get_pkey_type(sigalg)) {
David Benjamin3570d732015-06-29 00:28:17 -0400530 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800531 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
532 return 0;
533 }
534
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500536 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400537 for (i = 0; i < sent_sigslen; i++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400538 if (sigalg == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 break;
540 }
541 }
542
David Benjamin788be4a2015-10-30 17:50:57 -0400543 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400544 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
546 return 0;
547 }
548
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 return 1;
550}
551
552/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
553 * supported or doesn't appear in supported signature algorithms. Unlike
554 * ssl_cipher_get_disabled this applies to a specific session and not global
555 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500556void ssl_set_client_disabled(SSL *ssl) {
557 CERT *c = ssl->cert;
Steven Valdez02563852016-06-23 13:33:05 -0400558 const uint16_t *sigalgs;
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 size_t i, sigalgslen;
560 int have_rsa = 0, have_ecdsa = 0;
561 c->mask_a = 0;
562 c->mask_k = 0;
563
Adam Langleyfcf25832014-12-18 17:42:32 -0800564 /* Now go through all signature algorithms seeing if we support any for RSA,
565 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500566 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -0400567 for (i = 0; i < sigalgslen; i++) {
568 switch (sigalgs[i]) {
569 case SSL_SIGN_RSA_PKCS1_SHA512:
570 case SSL_SIGN_RSA_PKCS1_SHA384:
571 case SSL_SIGN_RSA_PKCS1_SHA256:
572 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800573 have_rsa = 1;
574 break;
575
Steven Valdez02563852016-06-23 13:33:05 -0400576 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
577 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
578 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
579 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800580 have_ecdsa = 1;
581 break;
582 }
583 }
584
585 /* Disable auth if we don't include any appropriate signature algorithms. */
586 if (!have_rsa) {
587 c->mask_a |= SSL_aRSA;
588 }
589 if (!have_ecdsa) {
590 c->mask_a |= SSL_aECDSA;
591 }
592
593 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500594 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800595 c->mask_a |= SSL_aPSK;
596 c->mask_k |= SSL_kPSK;
597 }
598}
Adam Langley95c29f32014-06-20 12:00:00 -0700599
Adam Langley614c66a2015-06-12 15:26:58 -0700600/* tls_extension represents a TLS extension that is handled internally. The
601 * |init| function is called for each handshake, before any other functions of
602 * the extension. Then the add and parse callbacks are called as needed.
603 *
604 * The parse callbacks receive a |CBS| that contains the contents of the
605 * extension (i.e. not including the type and length bytes). If an extension is
606 * not received then the parse callbacks will be called with a NULL CBS so that
607 * they can do any processing needed to handle the absence of an extension.
608 *
609 * The add callbacks receive a |CBB| to which the extension can be appended but
610 * the function is responsible for appending the type and length bytes too.
611 *
612 * All callbacks return one for success and zero for error. If a parse function
613 * returns zero then a fatal alert with value |*out_alert| will be sent. If
614 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
615struct tls_extension {
616 uint16_t value;
617 void (*init)(SSL *ssl);
618
619 int (*add_clienthello)(SSL *ssl, CBB *out);
620 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
621
622 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
623 int (*add_serverhello)(SSL *ssl, CBB *out);
624};
625
626
627/* Server name indication (SNI).
628 *
629 * https://tools.ietf.org/html/rfc6066#section-3. */
630
631static void ext_sni_init(SSL *ssl) {
632 ssl->s3->tmp.should_ack_sni = 0;
633}
634
635static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
636 if (ssl->tlsext_hostname == NULL) {
637 return 1;
638 }
639
640 CBB contents, server_name_list, name;
641 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
642 !CBB_add_u16_length_prefixed(out, &contents) ||
643 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
644 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
645 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
646 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
647 strlen(ssl->tlsext_hostname)) ||
648 !CBB_flush(out)) {
649 return 0;
650 }
651
652 return 1;
653}
654
David Benjamin0d56f882015-12-19 17:05:56 -0500655static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
656 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700657 if (contents == NULL) {
658 return 1;
659 }
660
661 if (CBS_len(contents) != 0) {
662 return 0;
663 }
664
665 assert(ssl->tlsext_hostname != NULL);
666
667 if (!ssl->hit) {
668 assert(ssl->session->tlsext_hostname == NULL);
669 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
670 if (!ssl->session->tlsext_hostname) {
671 *out_alert = SSL_AD_INTERNAL_ERROR;
672 return 0;
673 }
674 }
675
676 return 1;
677}
678
David Benjamin0d56f882015-12-19 17:05:56 -0500679static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
680 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700681 if (contents == NULL) {
682 return 1;
683 }
684
David Benjamin9b611e22016-03-03 08:48:30 -0500685 CBS server_name_list, host_name;
686 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700687 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500688 !CBS_get_u8(&server_name_list, &name_type) ||
689 /* Although the server_name extension was intended to be extensible to
690 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
691 * different name types will cause an error. Further, RFC 4366 originally
692 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
693 * adding new name types is no longer feasible.
694 *
695 * Act as if the extensibility does not exist to simplify parsing. */
696 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
697 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700698 CBS_len(contents) != 0) {
699 return 0;
700 }
701
David Benjamin9b611e22016-03-03 08:48:30 -0500702 if (name_type != TLSEXT_NAMETYPE_host_name ||
703 CBS_len(&host_name) == 0 ||
704 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
705 CBS_contains_zero_byte(&host_name)) {
706 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
707 return 0;
708 }
Adam Langley614c66a2015-06-12 15:26:58 -0700709
David Benjamin9b611e22016-03-03 08:48:30 -0500710 /* TODO(davidben): SNI should be resolved before resumption. We have the
711 * early callback as a replacement, but we should fix the current callback
712 * and avoid the need for |SSL_CTX_set_session_id_context|. */
713 if (!ssl->hit) {
714 assert(ssl->session->tlsext_hostname == NULL);
715
716 /* Copy the hostname as a string. */
717 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
718 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700719 return 0;
720 }
721
David Benjamin9b611e22016-03-03 08:48:30 -0500722 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700723 }
724
725 return 1;
726}
727
728static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
729 if (ssl->hit ||
730 !ssl->s3->tmp.should_ack_sni ||
731 ssl->session->tlsext_hostname == NULL) {
732 return 1;
733 }
734
735 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
736 !CBB_add_u16(out, 0 /* length */)) {
737 return 0;
738 }
739
740 return 1;
741}
742
743
Adam Langley5021b222015-06-12 18:27:58 -0700744/* Renegotiation indication.
745 *
746 * https://tools.ietf.org/html/rfc5746 */
747
748static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
749 CBB contents, prev_finished;
750 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
751 !CBB_add_u16_length_prefixed(out, &contents) ||
752 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
753 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
754 ssl->s3->previous_client_finished_len) ||
755 !CBB_flush(out)) {
756 return 0;
757 }
758
759 return 1;
760}
761
762static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
763 CBS *contents) {
David Benjamin3e052de2015-11-25 20:10:31 -0500764 /* Servers may not switch between omitting the extension and supporting it.
765 * See RFC 5746, sections 3.5 and 4.2. */
766 if (ssl->s3->initial_handshake_complete &&
767 (contents != NULL) != ssl->s3->send_connection_binding) {
768 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
769 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
770 return 0;
771 }
772
Adam Langley5021b222015-06-12 18:27:58 -0700773 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500774 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700775 * RI even on initial ServerHello because the client doesn't see any
776 * renegotiation during an attack. However this would mean we could not
777 * connect to any server which doesn't support RI.
778 *
David Benjamine9cddb82015-11-23 14:36:40 -0500779 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
780 * practical terms every client sets it so it's just assumed here. */
781 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700782 }
783
784 const size_t expected_len = ssl->s3->previous_client_finished_len +
785 ssl->s3->previous_server_finished_len;
786
787 /* Check for logic errors */
788 assert(!expected_len || ssl->s3->previous_client_finished_len);
789 assert(!expected_len || ssl->s3->previous_server_finished_len);
790
791 /* Parse out the extension contents. */
792 CBS renegotiated_connection;
793 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
794 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400795 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700796 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
797 return 0;
798 }
799
800 /* Check that the extension matches. */
801 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400802 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700803 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
804 return 0;
805 }
806
807 const uint8_t *d = CBS_data(&renegotiated_connection);
808 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
809 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400810 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700811 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
812 return 0;
813 }
814 d += ssl->s3->previous_client_finished_len;
815
816 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
817 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400818 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700819 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
820 return 0;
821 }
822 ssl->s3->send_connection_binding = 1;
823
824 return 1;
825}
826
827static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
828 CBS *contents) {
829 /* Renegotiation isn't supported as a server so this function should never be
830 * called after the initial handshake. */
831 assert(!ssl->s3->initial_handshake_complete);
832
833 CBS fake_contents;
834 static const uint8_t kFakeExtension[] = {0};
835
836 if (contents == NULL) {
837 if (ssl->s3->send_connection_binding) {
838 /* The renegotiation SCSV was received so pretend that we received a
839 * renegotiation extension. */
840 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
841 contents = &fake_contents;
842 /* We require that the renegotiation extension is at index zero of
843 * kExtensions. */
844 ssl->s3->tmp.extensions.received |= (1u << 0);
845 } else {
846 return 1;
847 }
848 }
849
850 CBS renegotiated_connection;
851
852 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
853 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400854 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700855 return 0;
856 }
857
858 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500859 if (!CBS_mem_equal(&renegotiated_connection,
860 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700861 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400862 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700863 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
864 return 0;
865 }
866
867 ssl->s3->send_connection_binding = 1;
868
869 return 1;
870}
871
872static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
873 CBB contents, prev_finished;
874 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
875 !CBB_add_u16_length_prefixed(out, &contents) ||
876 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
877 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
878 ssl->s3->previous_client_finished_len) ||
879 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
880 ssl->s3->previous_server_finished_len) ||
881 !CBB_flush(out)) {
882 return 0;
883 }
884
885 return 1;
886}
887
Adam Langley0a056712015-07-01 15:03:33 -0700888
889/* Extended Master Secret.
890 *
David Benjamin43946d42016-02-01 08:42:19 -0500891 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700892
893static void ext_ems_init(SSL *ssl) {
894 ssl->s3->tmp.extended_master_secret = 0;
895}
896
897static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
898 if (ssl->version == SSL3_VERSION) {
899 return 1;
900 }
901
902 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
903 !CBB_add_u16(out, 0 /* length */)) {
904 return 0;
905 }
906
907 return 1;
908}
909
910static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
911 CBS *contents) {
912 if (contents == NULL) {
913 return 1;
914 }
915
916 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
917 return 0;
918 }
919
920 ssl->s3->tmp.extended_master_secret = 1;
921 return 1;
922}
923
David Benjamin0d56f882015-12-19 17:05:56 -0500924static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
925 CBS *contents) {
Adam Langley0a056712015-07-01 15:03:33 -0700926 if (ssl->version == SSL3_VERSION || contents == NULL) {
927 return 1;
928 }
929
930 if (CBS_len(contents) != 0) {
931 return 0;
932 }
933
934 ssl->s3->tmp.extended_master_secret = 1;
935 return 1;
936}
937
938static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
939 if (!ssl->s3->tmp.extended_master_secret) {
940 return 1;
941 }
942
943 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
944 !CBB_add_u16(out, 0 /* length */)) {
945 return 0;
946 }
947
948 return 1;
949}
950
Adam Langley9b05bc52015-07-01 15:25:33 -0700951
952/* Session tickets.
953 *
954 * https://tools.ietf.org/html/rfc5077 */
955
956static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
957 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
958 return 1;
959 }
960
961 const uint8_t *ticket_data = NULL;
962 int ticket_len = 0;
963
964 /* Renegotiation does not participate in session resumption. However, still
965 * advertise the extension to avoid potentially breaking servers which carry
966 * over the state from the previous handshake, such as OpenSSL servers
967 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
968 if (!ssl->s3->initial_handshake_complete &&
969 ssl->session != NULL &&
970 ssl->session->tlsext_tick != NULL) {
971 ticket_data = ssl->session->tlsext_tick;
972 ticket_len = ssl->session->tlsext_ticklen;
973 }
974
975 CBB ticket;
976 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
977 !CBB_add_u16_length_prefixed(out, &ticket) ||
978 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
979 !CBB_flush(out)) {
980 return 0;
981 }
982
983 return 1;
984}
985
986static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
987 CBS *contents) {
988 ssl->tlsext_ticket_expected = 0;
989
990 if (contents == NULL) {
991 return 1;
992 }
993
994 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
995 * this function should never be called, even if the server tries to send the
996 * extension. */
997 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
998
999 if (CBS_len(contents) != 0) {
1000 return 0;
1001 }
1002
1003 ssl->tlsext_ticket_expected = 1;
1004 return 1;
1005}
1006
David Benjamin0d56f882015-12-19 17:05:56 -05001007static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1008 CBS *contents) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001009 /* This function isn't used because the ticket extension from the client is
David Benjaminc7ce9772015-10-09 19:32:41 -04001010 * handled in ssl_session.c. */
Adam Langley9b05bc52015-07-01 15:25:33 -07001011 return 1;
1012}
1013
1014static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1015 if (!ssl->tlsext_ticket_expected) {
1016 return 1;
1017 }
1018
1019 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1020 * true. */
1021 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1022
1023 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1024 !CBB_add_u16(out, 0 /* length */)) {
1025 return 0;
1026 }
1027
1028 return 1;
1029}
1030
1031
Adam Langley2e857bd2015-07-01 16:09:19 -07001032/* Signature Algorithms.
1033 *
1034 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1035
1036static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjaminb6a0a512016-06-21 10:33:21 -04001037 if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001038 return 1;
1039 }
1040
Steven Valdez02563852016-06-23 13:33:05 -04001041 const uint16_t *sigalgs_data;
Adam Langley2e857bd2015-07-01 16:09:19 -07001042 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1043
1044 CBB contents, sigalgs;
1045 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1046 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez02563852016-06-23 13:33:05 -04001047 !CBB_add_u16_length_prefixed(&contents, &sigalgs)) {
1048 return 0;
1049 }
1050
1051 size_t i;
1052 for (i = 0; i < sigalgs_len; i++) {
1053 if (!CBB_add_u16(&sigalgs, sigalgs_data[i])) {
1054 return 0;
1055 }
1056 }
1057
1058 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001059 return 0;
1060 }
1061
1062 return 1;
1063}
1064
1065static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1066 CBS *contents) {
1067 if (contents != NULL) {
1068 /* Servers MUST NOT send this extension. */
1069 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001070 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001071 return 0;
1072 }
1073
1074 return 1;
1075}
1076
1077static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1078 CBS *contents) {
1079 OPENSSL_free(ssl->cert->peer_sigalgs);
1080 ssl->cert->peer_sigalgs = NULL;
1081 ssl->cert->peer_sigalgslen = 0;
1082
Adam Langley2e857bd2015-07-01 16:09:19 -07001083 if (contents == NULL) {
1084 return 1;
1085 }
1086
1087 CBS supported_signature_algorithms;
1088 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001089 CBS_len(contents) != 0 ||
1090 CBS_len(&supported_signature_algorithms) == 0 ||
1091 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001092 return 0;
1093 }
1094
1095 return 1;
1096}
1097
1098static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1099 /* Servers MUST NOT send this extension. */
1100 return 1;
1101}
1102
1103
Adam Langleybb0bd042015-07-01 16:21:03 -07001104/* OCSP Stapling.
1105 *
1106 * https://tools.ietf.org/html/rfc6066#section-8 */
1107
1108static void ext_ocsp_init(SSL *ssl) {
1109 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001110 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001111}
1112
1113static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1114 if (!ssl->ocsp_stapling_enabled) {
1115 return 1;
1116 }
1117
1118 CBB contents;
1119 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1120 !CBB_add_u16_length_prefixed(out, &contents) ||
1121 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1122 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1123 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1124 !CBB_flush(out)) {
1125 return 0;
1126 }
1127
Adam Langleyce9d85e2016-01-24 15:58:39 -08001128 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001129 return 1;
1130}
1131
1132static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001133 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001134 if (contents == NULL) {
1135 return 1;
1136 }
1137
1138 if (CBS_len(contents) != 0) {
1139 return 0;
1140 }
1141
1142 ssl->s3->tmp.certificate_status_expected = 1;
1143 return 1;
1144}
1145
1146static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1147 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001148 if (contents == NULL) {
1149 return 1;
1150 }
1151
1152 uint8_t status_type;
1153 if (!CBS_get_u8(contents, &status_type)) {
1154 return 0;
1155 }
1156
1157 /* We cannot decide whether OCSP stapling will occur yet because the correct
1158 * SSL_CTX might not have been selected. */
1159 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1160
Adam Langleybb0bd042015-07-01 16:21:03 -07001161 return 1;
1162}
1163
1164static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001165 /* The extension shouldn't be sent when resuming sessions. */
1166 if (ssl->hit ||
1167 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001168 ssl->ctx->ocsp_response_length == 0) {
1169 return 1;
1170 }
1171
1172 ssl->s3->tmp.certificate_status_expected = 1;
1173
1174 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1175 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001176}
1177
1178
Adam Langley97dfcbf2015-07-01 18:35:20 -07001179/* Next protocol negotiation.
1180 *
1181 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1182
1183static void ext_npn_init(SSL *ssl) {
1184 ssl->s3->next_proto_neg_seen = 0;
1185}
1186
1187static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1188 if (ssl->s3->initial_handshake_complete ||
1189 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001190 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001191 SSL_IS_DTLS(ssl)) {
1192 return 1;
1193 }
1194
1195 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1196 !CBB_add_u16(out, 0 /* length */)) {
1197 return 0;
1198 }
1199
1200 return 1;
1201}
1202
1203static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1204 CBS *contents) {
1205 if (contents == NULL) {
1206 return 1;
1207 }
1208
1209 /* If any of these are false then we should never have sent the NPN
1210 * extension in the ClientHello and thus this function should never have been
1211 * called. */
1212 assert(!ssl->s3->initial_handshake_complete);
1213 assert(!SSL_IS_DTLS(ssl));
1214 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001215 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001216
David Benjamin76c2efc2015-08-31 14:24:29 -04001217 if (ssl->s3->alpn_selected != NULL) {
1218 /* NPN and ALPN may not be negotiated in the same connection. */
1219 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1220 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1221 return 0;
1222 }
1223
Adam Langley97dfcbf2015-07-01 18:35:20 -07001224 const uint8_t *const orig_contents = CBS_data(contents);
1225 const size_t orig_len = CBS_len(contents);
1226
1227 while (CBS_len(contents) != 0) {
1228 CBS proto;
1229 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1230 CBS_len(&proto) == 0) {
1231 return 0;
1232 }
1233 }
1234
1235 uint8_t *selected;
1236 uint8_t selected_len;
1237 if (ssl->ctx->next_proto_select_cb(
1238 ssl, &selected, &selected_len, orig_contents, orig_len,
1239 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1240 *out_alert = SSL_AD_INTERNAL_ERROR;
1241 return 0;
1242 }
1243
David Benjamin79978df2015-12-25 15:56:49 -05001244 OPENSSL_free(ssl->s3->next_proto_negotiated);
1245 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1246 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001247 *out_alert = SSL_AD_INTERNAL_ERROR;
1248 return 0;
1249 }
1250
David Benjamin79978df2015-12-25 15:56:49 -05001251 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001252 ssl->s3->next_proto_neg_seen = 1;
1253
1254 return 1;
1255}
1256
1257static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1258 CBS *contents) {
1259 if (contents != NULL && CBS_len(contents) != 0) {
1260 return 0;
1261 }
1262
1263 if (contents == NULL ||
1264 ssl->s3->initial_handshake_complete ||
1265 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1266 * afterwards, parsing the ALPN extension will clear
1267 * |next_proto_neg_seen|. */
1268 ssl->s3->alpn_selected != NULL ||
1269 ssl->ctx->next_protos_advertised_cb == NULL ||
1270 SSL_IS_DTLS(ssl)) {
1271 return 1;
1272 }
1273
1274 ssl->s3->next_proto_neg_seen = 1;
1275 return 1;
1276}
1277
1278static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1279 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1280 * parsed. */
1281 if (!ssl->s3->next_proto_neg_seen) {
1282 return 1;
1283 }
1284
1285 const uint8_t *npa;
1286 unsigned npa_len;
1287
1288 if (ssl->ctx->next_protos_advertised_cb(
1289 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1290 SSL_TLSEXT_ERR_OK) {
1291 ssl->s3->next_proto_neg_seen = 0;
1292 return 1;
1293 }
1294
1295 CBB contents;
1296 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1297 !CBB_add_u16_length_prefixed(out, &contents) ||
1298 !CBB_add_bytes(&contents, npa, npa_len) ||
1299 !CBB_flush(out)) {
1300 return 0;
1301 }
1302
1303 return 1;
1304}
1305
1306
Adam Langleyab8d87d2015-07-10 12:21:39 -07001307/* Signed certificate timestamps.
1308 *
1309 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1310
1311static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1312 if (!ssl->signed_cert_timestamps_enabled) {
1313 return 1;
1314 }
1315
1316 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1317 !CBB_add_u16(out, 0 /* length */)) {
1318 return 0;
1319 }
1320
1321 return 1;
1322}
1323
1324static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1325 CBS *contents) {
1326 if (contents == NULL) {
1327 return 1;
1328 }
1329
1330 /* If this is false then we should never have sent the SCT extension in the
1331 * ClientHello and thus this function should never have been called. */
1332 assert(ssl->signed_cert_timestamps_enabled);
1333
1334 if (CBS_len(contents) == 0) {
1335 *out_alert = SSL_AD_DECODE_ERROR;
1336 return 0;
1337 }
1338
1339 /* Session resumption uses the original session information. */
1340 if (!ssl->hit &&
1341 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1342 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1343 *out_alert = SSL_AD_INTERNAL_ERROR;
1344 return 0;
1345 }
1346
1347 return 1;
1348}
1349
1350static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1351 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001352 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001353}
1354
1355static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001356 /* The extension shouldn't be sent when resuming sessions. */
1357 if (ssl->hit ||
1358 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001359 return 1;
1360 }
1361
1362 CBB contents;
1363 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1364 CBB_add_u16_length_prefixed(out, &contents) &&
1365 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1366 ssl->ctx->signed_cert_timestamp_list_length) &&
1367 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001368}
1369
1370
Adam Langleyf18e4532015-07-10 13:39:53 -07001371/* Application-level Protocol Negotiation.
1372 *
1373 * https://tools.ietf.org/html/rfc7301 */
1374
1375static void ext_alpn_init(SSL *ssl) {
1376 OPENSSL_free(ssl->s3->alpn_selected);
1377 ssl->s3->alpn_selected = NULL;
1378}
1379
1380static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1381 if (ssl->alpn_client_proto_list == NULL ||
1382 ssl->s3->initial_handshake_complete) {
1383 return 1;
1384 }
1385
1386 CBB contents, proto_list;
1387 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1388 !CBB_add_u16_length_prefixed(out, &contents) ||
1389 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1390 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1391 ssl->alpn_client_proto_list_len) ||
1392 !CBB_flush(out)) {
1393 return 0;
1394 }
1395
1396 return 1;
1397}
1398
1399static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1400 CBS *contents) {
1401 if (contents == NULL) {
1402 return 1;
1403 }
1404
1405 assert(!ssl->s3->initial_handshake_complete);
1406 assert(ssl->alpn_client_proto_list != NULL);
1407
David Benjamin76c2efc2015-08-31 14:24:29 -04001408 if (ssl->s3->next_proto_neg_seen) {
1409 /* NPN and ALPN may not be negotiated in the same connection. */
1410 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1411 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1412 return 0;
1413 }
1414
Adam Langleyf18e4532015-07-10 13:39:53 -07001415 /* The extension data consists of a ProtocolNameList which must have
1416 * exactly one ProtocolName. Each of these is length-prefixed. */
1417 CBS protocol_name_list, protocol_name;
1418 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1419 CBS_len(contents) != 0 ||
1420 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1421 /* Empty protocol names are forbidden. */
1422 CBS_len(&protocol_name) == 0 ||
1423 CBS_len(&protocol_name_list) != 0) {
1424 return 0;
1425 }
1426
1427 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1428 &ssl->s3->alpn_selected_len)) {
1429 *out_alert = SSL_AD_INTERNAL_ERROR;
1430 return 0;
1431 }
1432
1433 return 1;
1434}
1435
1436static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1437 CBS *contents) {
1438 if (contents == NULL) {
1439 return 1;
1440 }
1441
1442 if (ssl->ctx->alpn_select_cb == NULL ||
1443 ssl->s3->initial_handshake_complete) {
1444 return 1;
1445 }
1446
1447 /* ALPN takes precedence over NPN. */
1448 ssl->s3->next_proto_neg_seen = 0;
1449
1450 CBS protocol_name_list;
1451 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1452 CBS_len(contents) != 0 ||
1453 CBS_len(&protocol_name_list) < 2) {
1454 return 0;
1455 }
1456
1457 /* Validate the protocol list. */
1458 CBS protocol_name_list_copy = protocol_name_list;
1459 while (CBS_len(&protocol_name_list_copy) > 0) {
1460 CBS protocol_name;
1461
1462 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1463 /* Empty protocol names are forbidden. */
1464 CBS_len(&protocol_name) == 0) {
1465 return 0;
1466 }
1467 }
1468
1469 const uint8_t *selected;
1470 uint8_t selected_len;
1471 if (ssl->ctx->alpn_select_cb(
1472 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1473 CBS_len(&protocol_name_list),
1474 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1475 OPENSSL_free(ssl->s3->alpn_selected);
1476 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1477 if (ssl->s3->alpn_selected == NULL) {
1478 *out_alert = SSL_AD_INTERNAL_ERROR;
1479 return 0;
1480 }
1481 ssl->s3->alpn_selected_len = selected_len;
1482 }
1483
1484 return 1;
1485}
1486
1487static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1488 if (ssl->s3->alpn_selected == NULL) {
1489 return 1;
1490 }
1491
1492 CBB contents, proto_list, proto;
1493 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1494 !CBB_add_u16_length_prefixed(out, &contents) ||
1495 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1496 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001497 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1498 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001499 !CBB_flush(out)) {
1500 return 0;
1501 }
1502
1503 return 1;
1504}
1505
1506
Adam Langley49c7af12015-07-10 14:33:46 -07001507/* Channel ID.
1508 *
1509 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1510
1511static void ext_channel_id_init(SSL *ssl) {
1512 ssl->s3->tlsext_channel_id_valid = 0;
1513}
1514
1515static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1516 if (!ssl->tlsext_channel_id_enabled ||
1517 SSL_IS_DTLS(ssl)) {
1518 return 1;
1519 }
1520
1521 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1522 !CBB_add_u16(out, 0 /* length */)) {
1523 return 0;
1524 }
1525
1526 return 1;
1527}
1528
1529static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1530 CBS *contents) {
1531 if (contents == NULL) {
1532 return 1;
1533 }
1534
1535 assert(!SSL_IS_DTLS(ssl));
1536 assert(ssl->tlsext_channel_id_enabled);
1537
1538 if (CBS_len(contents) != 0) {
1539 return 0;
1540 }
1541
1542 ssl->s3->tlsext_channel_id_valid = 1;
1543 return 1;
1544}
1545
1546static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1547 CBS *contents) {
1548 if (contents == NULL ||
1549 !ssl->tlsext_channel_id_enabled ||
1550 SSL_IS_DTLS(ssl)) {
1551 return 1;
1552 }
1553
1554 if (CBS_len(contents) != 0) {
1555 return 0;
1556 }
1557
1558 ssl->s3->tlsext_channel_id_valid = 1;
1559 return 1;
1560}
1561
1562static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1563 if (!ssl->s3->tlsext_channel_id_valid) {
1564 return 1;
1565 }
1566
1567 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1568 !CBB_add_u16(out, 0 /* length */)) {
1569 return 0;
1570 }
1571
1572 return 1;
1573}
1574
Adam Langley391250d2015-07-15 19:06:07 -07001575
1576/* Secure Real-time Transport Protocol (SRTP) extension.
1577 *
1578 * https://tools.ietf.org/html/rfc5764 */
1579
Adam Langley391250d2015-07-15 19:06:07 -07001580
1581static void ext_srtp_init(SSL *ssl) {
1582 ssl->srtp_profile = NULL;
1583}
1584
1585static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1586 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1587 if (profiles == NULL) {
1588 return 1;
1589 }
1590 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1591 if (num_profiles == 0) {
1592 return 1;
1593 }
1594
1595 CBB contents, profile_ids;
1596 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1597 !CBB_add_u16_length_prefixed(out, &contents) ||
1598 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1599 return 0;
1600 }
1601
1602 size_t i;
1603 for (i = 0; i < num_profiles; i++) {
1604 if (!CBB_add_u16(&profile_ids,
1605 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1606 return 0;
1607 }
1608 }
1609
1610 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1611 !CBB_flush(out)) {
1612 return 0;
1613 }
1614
1615 return 1;
1616}
1617
1618static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1619 CBS *contents) {
1620 if (contents == NULL) {
1621 return 1;
1622 }
1623
1624 /* The extension consists of a u16-prefixed profile ID list containing a
1625 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1626 *
1627 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1628 CBS profile_ids, srtp_mki;
1629 uint16_t profile_id;
1630 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1631 !CBS_get_u16(&profile_ids, &profile_id) ||
1632 CBS_len(&profile_ids) != 0 ||
1633 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1634 CBS_len(contents) != 0) {
1635 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1636 return 0;
1637 }
1638
1639 if (CBS_len(&srtp_mki) != 0) {
1640 /* Must be no MKI, since we never offer one. */
1641 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1642 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1643 return 0;
1644 }
1645
1646 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1647
1648 /* Check to see if the server gave us something we support (and presumably
1649 * offered). */
1650 size_t i;
1651 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1652 const SRTP_PROTECTION_PROFILE *profile =
1653 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1654
1655 if (profile->id == profile_id) {
1656 ssl->srtp_profile = profile;
1657 return 1;
1658 }
1659 }
1660
1661 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1662 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1663 return 0;
1664}
1665
1666static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1667 CBS *contents) {
1668 if (contents == NULL) {
1669 return 1;
1670 }
1671
1672 CBS profile_ids, srtp_mki;
1673 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1674 CBS_len(&profile_ids) < 2 ||
1675 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1676 CBS_len(contents) != 0) {
1677 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1678 return 0;
1679 }
1680 /* Discard the MKI value for now. */
1681
1682 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1683 SSL_get_srtp_profiles(ssl);
1684
1685 /* Pick the server's most preferred profile. */
1686 size_t i;
1687 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1688 const SRTP_PROTECTION_PROFILE *server_profile =
1689 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1690
1691 CBS profile_ids_tmp;
1692 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1693
1694 while (CBS_len(&profile_ids_tmp) > 0) {
1695 uint16_t profile_id;
1696 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1697 return 0;
1698 }
1699
1700 if (server_profile->id == profile_id) {
1701 ssl->srtp_profile = server_profile;
1702 return 1;
1703 }
1704 }
1705 }
1706
1707 return 1;
1708}
1709
1710static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1711 if (ssl->srtp_profile == NULL) {
1712 return 1;
1713 }
1714
1715 CBB contents, profile_ids;
1716 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1717 !CBB_add_u16_length_prefixed(out, &contents) ||
1718 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1719 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1720 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1721 !CBB_flush(out)) {
1722 return 0;
1723 }
1724
1725 return 1;
1726}
1727
Adam Langleybdd5d662015-07-20 16:19:08 -07001728
1729/* EC point formats.
1730 *
1731 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1732
1733static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1734 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1735 return 0;
1736 }
1737
1738 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1739
1740 size_t i;
1741 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1742 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1743
1744 const uint32_t alg_k = cipher->algorithm_mkey;
1745 const uint32_t alg_a = cipher->algorithm_auth;
1746 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1747 return 1;
1748 }
1749 }
1750
1751 return 0;
1752}
1753
Adam Langleybdd5d662015-07-20 16:19:08 -07001754static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001755 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001756 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1757 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001758 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1759 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001760 !CBB_flush(out)) {
1761 return 0;
1762 }
1763
1764 return 1;
1765}
1766
1767static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1768 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1769 return 1;
1770 }
1771
1772 return ext_ec_point_add_extension(ssl, out);
1773}
1774
1775static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1776 CBS *contents) {
1777 if (contents == NULL) {
1778 return 1;
1779 }
1780
1781 CBS ec_point_format_list;
1782 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1783 CBS_len(contents) != 0) {
1784 return 0;
1785 }
1786
David Benjaminfc059942015-07-30 23:01:59 -04001787 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1788 * point format. */
1789 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1790 CBS_len(&ec_point_format_list)) == NULL) {
1791 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001792 return 0;
1793 }
1794
1795 return 1;
1796}
1797
1798static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1799 CBS *contents) {
1800 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1801}
1802
1803static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1804 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1805 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001806 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001807
1808 if (!using_ecc) {
1809 return 1;
1810 }
1811
1812 return ext_ec_point_add_extension(ssl, out);
1813}
1814
Adam Langley273d49c2015-07-20 16:38:52 -07001815
Steven Valdezce902a92016-05-17 11:47:53 -04001816/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07001817 *
Steven Valdezce902a92016-05-17 11:47:53 -04001818 * https://tools.ietf.org/html/rfc4492#section-5.1.2
1819 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07001820
Steven Valdezce902a92016-05-17 11:47:53 -04001821static void ext_supported_groups_init(SSL *ssl) {
1822 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
1823 ssl->s3->tmp.peer_supported_group_list = NULL;
1824 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07001825}
1826
Steven Valdezce902a92016-05-17 11:47:53 -04001827static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07001828 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1829 return 1;
1830 }
1831
Steven Valdezce902a92016-05-17 11:47:53 -04001832 CBB contents, groups_bytes;
1833 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07001834 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001835 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07001836 return 0;
1837 }
1838
Steven Valdezce902a92016-05-17 11:47:53 -04001839 const uint16_t *groups;
1840 size_t groups_len;
1841 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07001842
1843 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04001844 for (i = 0; i < groups_len; i++) {
1845 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07001846 return 0;
1847 }
1848 }
1849
1850 return CBB_flush(out);
1851}
1852
Steven Valdezce902a92016-05-17 11:47:53 -04001853static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1854 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07001855 /* This extension is not expected to be echoed by servers and is ignored. */
1856 return 1;
1857}
1858
Steven Valdezce902a92016-05-17 11:47:53 -04001859static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1860 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07001861 if (contents == NULL) {
1862 return 1;
1863 }
1864
Steven Valdezce902a92016-05-17 11:47:53 -04001865 CBS supported_group_list;
1866 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
1867 CBS_len(&supported_group_list) == 0 ||
1868 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07001869 CBS_len(contents) != 0) {
1870 return 0;
1871 }
1872
Steven Valdezce902a92016-05-17 11:47:53 -04001873 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
1874 CBS_len(&supported_group_list));
1875 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07001876 *out_alert = SSL_AD_INTERNAL_ERROR;
1877 return 0;
1878 }
1879
Steven Valdezce902a92016-05-17 11:47:53 -04001880 const size_t num_groups = CBS_len(&supported_group_list) / 2;
Adam Langley273d49c2015-07-20 16:38:52 -07001881 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04001882 for (i = 0; i < num_groups; i++) {
1883 if (!CBS_get_u16(&supported_group_list,
1884 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07001885 goto err;
1886 }
1887 }
1888
Steven Valdezce902a92016-05-17 11:47:53 -04001889 assert(CBS_len(&supported_group_list) == 0);
1890 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07001891
1892 return 1;
1893
1894err:
Steven Valdezce902a92016-05-17 11:47:53 -04001895 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
1896 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07001897 *out_alert = SSL_AD_INTERNAL_ERROR;
1898 return 0;
1899}
1900
Steven Valdezce902a92016-05-17 11:47:53 -04001901static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07001902 /* Servers don't echo this extension. */
1903 return 1;
1904}
1905
1906
Adam Langley614c66a2015-06-12 15:26:58 -07001907/* kExtensions contains all the supported extensions. */
1908static const struct tls_extension kExtensions[] = {
1909 {
Adam Langley5021b222015-06-12 18:27:58 -07001910 /* The renegotiation extension must always be at index zero because the
1911 * |received| and |sent| bitsets need to be tweaked when the "extension" is
1912 * sent as an SCSV. */
1913 TLSEXT_TYPE_renegotiate,
1914 NULL,
1915 ext_ri_add_clienthello,
1916 ext_ri_parse_serverhello,
1917 ext_ri_parse_clienthello,
1918 ext_ri_add_serverhello,
1919 },
1920 {
Adam Langley614c66a2015-06-12 15:26:58 -07001921 TLSEXT_TYPE_server_name,
1922 ext_sni_init,
1923 ext_sni_add_clienthello,
1924 ext_sni_parse_serverhello,
1925 ext_sni_parse_clienthello,
1926 ext_sni_add_serverhello,
1927 },
Adam Langley0a056712015-07-01 15:03:33 -07001928 {
1929 TLSEXT_TYPE_extended_master_secret,
1930 ext_ems_init,
1931 ext_ems_add_clienthello,
1932 ext_ems_parse_serverhello,
1933 ext_ems_parse_clienthello,
1934 ext_ems_add_serverhello,
1935 },
Adam Langley9b05bc52015-07-01 15:25:33 -07001936 {
1937 TLSEXT_TYPE_session_ticket,
1938 NULL,
1939 ext_ticket_add_clienthello,
1940 ext_ticket_parse_serverhello,
1941 ext_ticket_parse_clienthello,
1942 ext_ticket_add_serverhello,
1943 },
Adam Langley2e857bd2015-07-01 16:09:19 -07001944 {
1945 TLSEXT_TYPE_signature_algorithms,
1946 NULL,
1947 ext_sigalgs_add_clienthello,
1948 ext_sigalgs_parse_serverhello,
1949 ext_sigalgs_parse_clienthello,
1950 ext_sigalgs_add_serverhello,
1951 },
Adam Langleybb0bd042015-07-01 16:21:03 -07001952 {
1953 TLSEXT_TYPE_status_request,
1954 ext_ocsp_init,
1955 ext_ocsp_add_clienthello,
1956 ext_ocsp_parse_serverhello,
1957 ext_ocsp_parse_clienthello,
1958 ext_ocsp_add_serverhello,
1959 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07001960 {
1961 TLSEXT_TYPE_next_proto_neg,
1962 ext_npn_init,
1963 ext_npn_add_clienthello,
1964 ext_npn_parse_serverhello,
1965 ext_npn_parse_clienthello,
1966 ext_npn_add_serverhello,
1967 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07001968 {
1969 TLSEXT_TYPE_certificate_timestamp,
1970 NULL,
1971 ext_sct_add_clienthello,
1972 ext_sct_parse_serverhello,
1973 ext_sct_parse_clienthello,
1974 ext_sct_add_serverhello,
1975 },
Adam Langleyf18e4532015-07-10 13:39:53 -07001976 {
1977 TLSEXT_TYPE_application_layer_protocol_negotiation,
1978 ext_alpn_init,
1979 ext_alpn_add_clienthello,
1980 ext_alpn_parse_serverhello,
1981 ext_alpn_parse_clienthello,
1982 ext_alpn_add_serverhello,
1983 },
Adam Langley49c7af12015-07-10 14:33:46 -07001984 {
1985 TLSEXT_TYPE_channel_id,
1986 ext_channel_id_init,
1987 ext_channel_id_add_clienthello,
1988 ext_channel_id_parse_serverhello,
1989 ext_channel_id_parse_clienthello,
1990 ext_channel_id_add_serverhello,
1991 },
Adam Langley391250d2015-07-15 19:06:07 -07001992 {
1993 TLSEXT_TYPE_srtp,
1994 ext_srtp_init,
1995 ext_srtp_add_clienthello,
1996 ext_srtp_parse_serverhello,
1997 ext_srtp_parse_clienthello,
1998 ext_srtp_add_serverhello,
1999 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002000 {
2001 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002002 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002003 ext_ec_point_add_clienthello,
2004 ext_ec_point_parse_serverhello,
2005 ext_ec_point_parse_clienthello,
2006 ext_ec_point_add_serverhello,
2007 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002008 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2009 * intolerant to the last extension being zero-length. See
2010 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002011 {
Steven Valdezce902a92016-05-17 11:47:53 -04002012 TLSEXT_TYPE_supported_groups,
2013 ext_supported_groups_init,
2014 ext_supported_groups_add_clienthello,
2015 ext_supported_groups_parse_serverhello,
2016 ext_supported_groups_parse_clienthello,
2017 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002018 },
Adam Langley614c66a2015-06-12 15:26:58 -07002019};
2020
2021#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2022
Adam Langley4cfa96b2015-07-01 11:56:55 -07002023OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2024 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002025 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002026OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2027 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2028 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002029 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002030
Adam Langley614c66a2015-06-12 15:26:58 -07002031static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2032 uint16_t value) {
2033 unsigned i;
2034 for (i = 0; i < kNumExtensions; i++) {
2035 if (kExtensions[i].value == value) {
2036 *out_index = i;
2037 return &kExtensions[i];
2038 }
2039 }
2040
2041 return NULL;
2042}
2043
Adam Langley09505632015-07-30 18:10:13 -07002044int SSL_extension_supported(unsigned extension_value) {
2045 uint32_t index;
2046 return extension_value == TLSEXT_TYPE_padding ||
2047 tls_extension_find(&index, extension_value) != NULL;
2048}
2049
David Benjamine8d53502015-10-10 14:13:23 -04002050int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002051 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002052 if (ssl->client_version == SSL3_VERSION &&
2053 !ssl->s3->send_connection_binding) {
2054 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002055 }
Adam Langley95c29f32014-06-20 12:00:00 -07002056
David Benjamine8d53502015-10-10 14:13:23 -04002057 CBB extensions;
2058 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002059 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002060 }
Adam Langley95c29f32014-06-20 12:00:00 -07002061
David Benjamine8d53502015-10-10 14:13:23 -04002062 ssl->s3->tmp.extensions.sent = 0;
2063 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002064
Adam Langley614c66a2015-06-12 15:26:58 -07002065 size_t i;
2066 for (i = 0; i < kNumExtensions; i++) {
2067 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002068 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002069 }
2070 }
Adam Langley95c29f32014-06-20 12:00:00 -07002071
Adam Langley614c66a2015-06-12 15:26:58 -07002072 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002073 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002074 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002075 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2076 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2077 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002078 }
Adam Langley95c29f32014-06-20 12:00:00 -07002079
Adam Langley33ad2b52015-07-20 17:43:53 -07002080 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002081 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002082 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002083 }
Adam Langley75712922014-10-10 16:23:43 -07002084
David Benjamine8d53502015-10-10 14:13:23 -04002085 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002086 goto err;
2087 }
2088
David Benjamine8d53502015-10-10 14:13:23 -04002089 if (!SSL_IS_DTLS(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002090 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002091 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002092 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002093 *
2094 * NB: because this code works out the length of all existing extensions
2095 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002096 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002097 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002098 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002099 * Server 7.0 is intolerant to the last extension being zero-length. See
2100 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002101 if (padding_len >= 4 + 1) {
2102 padding_len -= 4;
2103 } else {
2104 padding_len = 1;
2105 }
Adam Langley95c29f32014-06-20 12:00:00 -07002106
Adam Langley33ad2b52015-07-20 17:43:53 -07002107 uint8_t *padding_bytes;
2108 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2109 !CBB_add_u16(&extensions, padding_len) ||
2110 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2111 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002112 }
Adam Langley75712922014-10-10 16:23:43 -07002113
Adam Langley33ad2b52015-07-20 17:43:53 -07002114 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002115 }
2116 }
Adam Langley75712922014-10-10 16:23:43 -07002117
David Benjamina01deee2015-12-08 18:56:31 -05002118 /* Discard empty extensions blocks. */
2119 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002120 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002121 }
2122
David Benjamine8d53502015-10-10 14:13:23 -04002123 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002124
2125err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002126 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002127 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002128}
Adam Langley95c29f32014-06-20 12:00:00 -07002129
David Benjamin56380462015-10-10 14:59:09 -04002130int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002131 CBB extensions;
2132 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002133 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002134 }
2135
2136 unsigned i;
2137 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002138 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002139 /* Don't send extensions that were not received. */
2140 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002141 }
Adam Langley95c29f32014-06-20 12:00:00 -07002142
David Benjamin56380462015-10-10 14:59:09 -04002143 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002144 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2145 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2146 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002147 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002148 }
Adam Langley95c29f32014-06-20 12:00:00 -07002149
David Benjamin56380462015-10-10 14:59:09 -04002150 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002151 goto err;
2152 }
2153
David Benjamina01deee2015-12-08 18:56:31 -05002154 /* Discard empty extensions blocks. */
2155 if (CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002156 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002157 }
2158
David Benjamin56380462015-10-10 14:59:09 -04002159 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002160
2161err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002162 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002163 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002164}
Adam Langley95c29f32014-06-20 12:00:00 -07002165
David Benjamin0d56f882015-12-19 17:05:56 -05002166static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002167 size_t i;
2168 for (i = 0; i < kNumExtensions; i++) {
2169 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002170 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002171 }
2172 }
2173
David Benjamin0d56f882015-12-19 17:05:56 -05002174 ssl->s3->tmp.extensions.received = 0;
2175 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002176 /* The renegotiation extension must always be at index zero because the
2177 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2178 * sent as an SCSV. */
2179 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002180
Adam Langleyfcf25832014-12-18 17:42:32 -08002181 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002182 if (CBS_len(cbs) != 0) {
2183 /* Decode the extensions block and check it is valid. */
2184 CBS extensions;
2185 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2186 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002187 *out_alert = SSL_AD_DECODE_ERROR;
2188 return 0;
2189 }
Adam Langley95c29f32014-06-20 12:00:00 -07002190
Adam Langley33ad2b52015-07-20 17:43:53 -07002191 while (CBS_len(&extensions) != 0) {
2192 uint16_t type;
2193 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002194
Adam Langley33ad2b52015-07-20 17:43:53 -07002195 /* Decode the next extension. */
2196 if (!CBS_get_u16(&extensions, &type) ||
2197 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2198 *out_alert = SSL_AD_DECODE_ERROR;
2199 return 0;
2200 }
2201
David Benjaminc7ce9772015-10-09 19:32:41 -04002202 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2203 * ambiguous. Ignore all but the renegotiation_info extension. */
David Benjamin0d56f882015-12-19 17:05:56 -05002204 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
David Benjaminc7ce9772015-10-09 19:32:41 -04002205 continue;
2206 }
2207
Adam Langley33ad2b52015-07-20 17:43:53 -07002208 unsigned ext_index;
2209 const struct tls_extension *const ext =
2210 tls_extension_find(&ext_index, type);
2211
2212 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002213 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002214 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2215 return 0;
2216 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002217 continue;
2218 }
2219
David Benjamin0d56f882015-12-19 17:05:56 -05002220 ssl->s3->tmp.extensions.received |= (1u << ext_index);
Adam Langley614c66a2015-06-12 15:26:58 -07002221 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002222 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
Adam Langley614c66a2015-06-12 15:26:58 -07002223 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002224 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2225 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002226 return 0;
2227 }
Adam Langley614c66a2015-06-12 15:26:58 -07002228 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002229 }
Adam Langley75712922014-10-10 16:23:43 -07002230
Adam Langley614c66a2015-06-12 15:26:58 -07002231 for (i = 0; i < kNumExtensions; i++) {
David Benjamin0d56f882015-12-19 17:05:56 -05002232 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002233 /* Extension wasn't observed so call the callback with a NULL
2234 * parameter. */
2235 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002236 if (!kExtensions[i].parse_clienthello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002237 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2238 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002239 *out_alert = alert;
2240 return 0;
2241 }
2242 }
2243 }
2244
Adam Langleyfcf25832014-12-18 17:42:32 -08002245 return 1;
2246}
Adam Langley95c29f32014-06-20 12:00:00 -07002247
David Benjamin0d56f882015-12-19 17:05:56 -05002248int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002249 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002250 if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) {
2251 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002252 return 0;
2253 }
Adam Langley95c29f32014-06-20 12:00:00 -07002254
David Benjamin0d56f882015-12-19 17:05:56 -05002255 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002256 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002257 return 0;
2258 }
Adam Langley95c29f32014-06-20 12:00:00 -07002259
Adam Langleyfcf25832014-12-18 17:42:32 -08002260 return 1;
2261}
Adam Langley95c29f32014-06-20 12:00:00 -07002262
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002263OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2264
David Benjamin0d56f882015-12-19 17:05:56 -05002265static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002266 uint32_t received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002267
Adam Langley33ad2b52015-07-20 17:43:53 -07002268 if (CBS_len(cbs) != 0) {
2269 /* Decode the extensions block and check it is valid. */
2270 CBS extensions;
2271 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2272 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002273 *out_alert = SSL_AD_DECODE_ERROR;
2274 return 0;
2275 }
Adam Langley95c29f32014-06-20 12:00:00 -07002276
Adam Langley614c66a2015-06-12 15:26:58 -07002277
Adam Langley33ad2b52015-07-20 17:43:53 -07002278 while (CBS_len(&extensions) != 0) {
2279 uint16_t type;
2280 CBS extension;
2281
2282 /* Decode the next extension. */
2283 if (!CBS_get_u16(&extensions, &type) ||
2284 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2285 *out_alert = SSL_AD_DECODE_ERROR;
2286 return 0;
2287 }
2288
2289 unsigned ext_index;
2290 const struct tls_extension *const ext =
2291 tls_extension_find(&ext_index, type);
2292
Adam Langley09505632015-07-30 18:10:13 -07002293 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002294 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002295 return 0;
2296 }
2297 continue;
2298 }
2299
David Benjamin0d56f882015-12-19 17:05:56 -05002300 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) {
Adam Langley09505632015-07-30 18:10:13 -07002301 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002302 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002303 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002304 *out_alert = SSL_AD_DECODE_ERROR;
2305 return 0;
2306 }
David Benjamin03973092014-06-24 23:27:17 -04002307
Adam Langley614c66a2015-06-12 15:26:58 -07002308 received |= (1u << ext_index);
2309
2310 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002311 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002312 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2313 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002314 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002315 return 0;
2316 }
Adam Langley614c66a2015-06-12 15:26:58 -07002317 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002318 }
Adam Langley95c29f32014-06-20 12:00:00 -07002319
Adam Langley33ad2b52015-07-20 17:43:53 -07002320 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002321 for (i = 0; i < kNumExtensions; i++) {
2322 if (!(received & (1u << i))) {
2323 /* Extension wasn't observed so call the callback with a NULL
2324 * parameter. */
2325 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002326 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002327 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2328 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002329 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002330 return 0;
2331 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002332 }
2333 }
Adam Langley95c29f32014-06-20 12:00:00 -07002334
Adam Langleyfcf25832014-12-18 17:42:32 -08002335 return 1;
2336}
Adam Langley95c29f32014-06-20 12:00:00 -07002337
David Benjamin0d56f882015-12-19 17:05:56 -05002338static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002339 int ret = SSL_TLSEXT_ERR_NOACK;
2340 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002341
David Benjamin78f8aab2016-03-10 16:33:58 -05002342 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002343 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002344 ssl->ctx->tlsext_servername_arg);
2345 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002346 ret = ssl->initial_ctx->tlsext_servername_callback(
2347 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002348 }
Adam Langley95c29f32014-06-20 12:00:00 -07002349
Adam Langleyfcf25832014-12-18 17:42:32 -08002350 switch (ret) {
2351 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002352 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002353 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002354
Adam Langleyfcf25832014-12-18 17:42:32 -08002355 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002356 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002357 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002358
Adam Langleyfcf25832014-12-18 17:42:32 -08002359 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002360 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002361 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002362
Adam Langleyfcf25832014-12-18 17:42:32 -08002363 default:
2364 return 1;
2365 }
2366}
Adam Langleyed8270a2014-09-02 13:52:56 -07002367
David Benjamin0d56f882015-12-19 17:05:56 -05002368static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002369 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002370 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002371
David Benjamin78f8aab2016-03-10 16:33:58 -05002372 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002373 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002374 ssl->ctx->tlsext_servername_arg);
2375 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002376 ret = ssl->initial_ctx->tlsext_servername_callback(
2377 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002378 }
Adam Langley95c29f32014-06-20 12:00:00 -07002379
Adam Langleyfcf25832014-12-18 17:42:32 -08002380 switch (ret) {
2381 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002382 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002383 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002384
Adam Langleyfcf25832014-12-18 17:42:32 -08002385 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002386 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002387 return 1;
2388
2389 default:
2390 return 1;
2391 }
2392}
2393
David Benjamin0d56f882015-12-19 17:05:56 -05002394int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002395 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002396 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2397 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002398 return 0;
2399 }
2400
David Benjamin0d56f882015-12-19 17:05:56 -05002401 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002402 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002403 return 0;
2404 }
2405
2406 return 1;
2407}
Adam Langley95c29f32014-06-20 12:00:00 -07002408
David Benjamine3aa1d92015-06-16 15:34:50 -04002409int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002410 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002411 size_t ticket_len, const uint8_t *session_id,
2412 size_t session_id_len) {
2413 int ret = 1; /* Most errors are non-fatal. */
2414 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2415 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002416
David Benjamine3aa1d92015-06-16 15:34:50 -04002417 HMAC_CTX hmac_ctx;
2418 HMAC_CTX_init(&hmac_ctx);
2419 EVP_CIPHER_CTX cipher_ctx;
2420 EVP_CIPHER_CTX_init(&cipher_ctx);
2421
David Benjaminef1b0092015-11-21 14:05:44 -05002422 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002423 *out_session = NULL;
2424
2425 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2426 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002427 }
2428
David Benjaminadcc3952015-04-26 13:07:57 -04002429 /* Ensure there is room for the key name and the largest IV
2430 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2431 * the maximum IV length should be well under the minimum size for the
2432 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002433 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2434 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002435 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002436 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002437
David Benjamine3aa1d92015-06-16 15:34:50 -04002438 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002439 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2440 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2441 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002442 if (cb_ret < 0) {
2443 ret = 0;
2444 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002445 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002446 if (cb_ret == 0) {
2447 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002448 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002449 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002450 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002451 }
2452 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002453 /* Check the key name matches. */
2454 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2455 SSL_TICKET_KEY_NAME_LEN) != 0) {
2456 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002457 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002458 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2459 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002460 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002461 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2462 ssl_ctx->tlsext_tick_aes_key, iv)) {
2463 ret = 0;
2464 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002465 }
2466 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002467 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002468
David Benjamine3aa1d92015-06-16 15:34:50 -04002469 /* Check the MAC at the end of the ticket. */
2470 uint8_t mac[EVP_MAX_MD_SIZE];
2471 size_t mac_len = HMAC_size(&hmac_ctx);
2472 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002473 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002474 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002475 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002476 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2477 HMAC_Final(&hmac_ctx, mac, NULL);
2478 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2479 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002480 }
2481
David Benjamine3aa1d92015-06-16 15:34:50 -04002482 /* Decrypt the session data. */
2483 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2484 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2485 mac_len;
2486 plaintext = OPENSSL_malloc(ciphertext_len);
2487 if (plaintext == NULL) {
2488 ret = 0;
2489 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002490 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002491 if (ciphertext_len >= INT_MAX) {
2492 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002493 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002494 int len1, len2;
2495 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2496 (int)ciphertext_len) ||
2497 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2498 ERR_clear_error(); /* Don't leave an error on the queue. */
2499 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002500 }
2501
David Benjamine3aa1d92015-06-16 15:34:50 -04002502 /* Decode the session. */
2503 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2504 if (session == NULL) {
2505 ERR_clear_error(); /* Don't leave an error on the queue. */
2506 goto done;
2507 }
2508
2509 /* Copy the client's session ID into the new session, to denote the ticket has
2510 * been accepted. */
2511 memcpy(session->session_id, session_id, session_id_len);
2512 session->session_id_length = session_id_len;
2513
2514 *out_session = session;
2515
2516done:
2517 OPENSSL_free(plaintext);
2518 HMAC_CTX_cleanup(&hmac_ctx);
2519 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2520 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002521}
Adam Langley95c29f32014-06-20 12:00:00 -07002522
Steven Valdezf0451ca2016-06-29 13:16:27 -04002523const EVP_MD *tls12_get_hash(uint16_t sigalg) {
2524 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1) {
2525 return EVP_md5_sha1();
Adam Langleyfcf25832014-12-18 17:42:32 -08002526 }
Adam Langley95c29f32014-06-20 12:00:00 -07002527
Steven Valdezf0451ca2016-06-29 13:16:27 -04002528 switch (sigalg >> 8) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002529 case TLSEXT_hash_sha1:
2530 return EVP_sha1();
2531
Adam Langleyfcf25832014-12-18 17:42:32 -08002532 case TLSEXT_hash_sha256:
2533 return EVP_sha256();
2534
2535 case TLSEXT_hash_sha384:
2536 return EVP_sha384();
2537
2538 case TLSEXT_hash_sha512:
2539 return EVP_sha512();
2540
2541 default:
2542 return NULL;
2543 }
2544}
Adam Langley95c29f32014-06-20 12:00:00 -07002545
David Benjaminec2f27d2014-11-13 19:17:25 -05002546/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
Steven Valdezf0451ca2016-06-29 13:16:27 -04002547 * algorithm |sigalg|. It returns -1 if the type is unknown. */
2548static int tls12_get_pkey_type(uint16_t sigalg) {
2549 switch (sigalg & 0xff) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002550 case TLSEXT_signature_rsa:
2551 return EVP_PKEY_RSA;
2552
2553 case TLSEXT_signature_ecdsa:
2554 return EVP_PKEY_EC;
2555
2556 default:
2557 return -1;
2558 }
2559}
Adam Langley95c29f32014-06-20 12:00:00 -07002560
Steven Valdez0d62f262015-09-04 12:41:04 -04002561int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002562 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05002563 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002564 return 1;
2565 }
David Benjamincd996942014-07-20 16:23:51 -04002566
Steven Valdez0d62f262015-09-04 12:41:04 -04002567 CERT *const cert = ssl->cert;
2568 OPENSSL_free(cert->peer_sigalgs);
2569 cert->peer_sigalgs = NULL;
2570 cert->peer_sigalgslen = 0;
2571
2572 size_t num_sigalgs = CBS_len(in_sigalgs);
2573
2574 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002575 return 0;
2576 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002577 num_sigalgs /= 2;
2578
2579 /* supported_signature_algorithms in the certificate request is
2580 * allowed to be empty. */
2581 if (num_sigalgs == 0) {
2582 return 1;
2583 }
2584
Steven Valdez02563852016-06-23 13:33:05 -04002585 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
2586 * and we just divided |num_sigalgs| by two. */
2587 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
Steven Valdez0d62f262015-09-04 12:41:04 -04002588 if (cert->peer_sigalgs == NULL) {
2589 return 0;
2590 }
2591 cert->peer_sigalgslen = num_sigalgs;
2592
2593 CBS sigalgs;
2594 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2595
2596 size_t i;
2597 for (i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04002598 if (!CBS_get_u16(&sigalgs, &cert->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04002599 return 0;
2600 }
2601 }
Adam Langley95c29f32014-06-20 12:00:00 -07002602
Adam Langleyfcf25832014-12-18 17:42:32 -08002603 return 1;
2604}
David Benjaminec2f27d2014-11-13 19:17:25 -05002605
Steven Valdezf0451ca2016-06-29 13:16:27 -04002606uint16_t tls1_choose_signature_algorithm(SSL *ssl) {
David Benjamind1d80782015-07-05 11:54:09 -04002607 CERT *cert = ssl->cert;
2608 int type = ssl_private_key_type(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -04002609 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002610
Steven Valdezf0451ca2016-06-29 13:16:27 -04002611 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
2612 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
2613 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
2614 if (type == EVP_PKEY_RSA) {
2615 return SSL_SIGN_RSA_PKCS1_MD5_SHA1;
2616 }
2617 return SSL_SIGN_ECDSA_SHA1;
2618 }
2619
Steven Valdez0d62f262015-09-04 12:41:04 -04002620 static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
David Benjamin72f7e212016-01-29 15:28:58 -05002621 NID_sha1};
Steven Valdez0d62f262015-09-04 12:41:04 -04002622
2623 const int *digest_nids = kDefaultDigestList;
2624 size_t num_digest_nids =
2625 sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
2626 if (cert->digest_nids != NULL) {
2627 digest_nids = cert->digest_nids;
2628 num_digest_nids = cert->num_digest_nids;
2629 }
2630
2631 for (i = 0; i < num_digest_nids; i++) {
2632 const int digest_nid = digest_nids[i];
2633 for (j = 0; j < cert->peer_sigalgslen; j++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04002634 uint16_t signature_algorithm = cert->peer_sigalgs[j];
2635 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
2636 * negotiated. */
2637 if (signature_algorithm == SSL_SIGN_RSA_PKCS1_MD5_SHA1) {
Steven Valdez0d62f262015-09-04 12:41:04 -04002638 continue;
2639 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04002640 const EVP_MD *md = tls12_get_hash(signature_algorithm);
2641 if (md != NULL && EVP_MD_type(md) == digest_nid &&
2642 tls12_get_pkey_type(signature_algorithm) == type) {
2643 return signature_algorithm;
2644 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002645 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002646 }
Adam Langley95c29f32014-06-20 12:00:00 -07002647
Adam Langleyfcf25832014-12-18 17:42:32 -08002648 /* If no suitable digest may be found, default to SHA-1. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04002649 if (type == EVP_PKEY_RSA) {
2650 return SSL_SIGN_RSA_PKCS1_SHA1;
2651 }
2652 return SSL_SIGN_ECDSA_SHA1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002653}
Adam Langley95c29f32014-06-20 12:00:00 -07002654
David Benjamind6a4ae92015-08-06 11:10:51 -04002655int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2656 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002657 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002658
2659 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002660 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2661 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002662 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002663
David Benjamind6a4ae92015-08-06 11:10:51 -04002664 static const char kClientIDMagic[] = "TLS Channel ID signature";
2665 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2666
2667 if (ssl->hit) {
2668 static const char kResumptionMagic[] = "Resumption";
2669 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2670 if (ssl->session->original_handshake_hash_len == 0) {
2671 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2672 goto err;
2673 }
2674 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2675 ssl->session->original_handshake_hash_len);
2676 }
2677
2678 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2679 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2680 sizeof(handshake_hash));
2681 if (handshake_hash_len < 0) {
2682 goto err;
2683 }
2684 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2685 unsigned len_u;
2686 EVP_DigestFinal_ex(&ctx, out, &len_u);
2687 *out_len = len_u;
2688
2689 ret = 1;
2690
2691err:
2692 EVP_MD_CTX_cleanup(&ctx);
2693 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002694}
Adam Langley1258b6a2014-06-20 12:00:00 -07002695
2696/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin0d56f882015-12-19 17:05:56 -05002697 * hashes in |ssl->session| so that Channel ID resumptions can sign that
2698 * data. */
2699int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002700 int digest_len;
2701 /* This function should never be called for a resumed session because the
2702 * handshake hashes that we wish to record are for the original, full
2703 * handshake. */
David Benjamin0d56f882015-12-19 17:05:56 -05002704 if (ssl->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002705 return -1;
2706 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002707
Adam Langleyfcf25832014-12-18 17:42:32 -08002708 digest_len =
David Benjamin0d56f882015-12-19 17:05:56 -05002709 tls1_handshake_digest(ssl, ssl->session->original_handshake_hash,
2710 sizeof(ssl->session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08002711 if (digest_len < 0) {
2712 return -1;
2713 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002714
David Benjamin0d56f882015-12-19 17:05:56 -05002715 ssl->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002716
Adam Langleyfcf25832014-12-18 17:42:32 -08002717 return 1;
2718}