blob: 16cac15efa7fa771530a30c74711c8a091ff63e0 [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
Adam Langley95c29f32014-06-20 12:00:00 -0700501#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700502
Adam Langley95c29f32014-06-20 12:00:00 -0700503#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langleyfcf25832014-12-18 17:42:32 -0800505#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700506
David Benjamincff64722014-08-19 19:54:46 -0400507static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800508 tlsext_sigalg(TLSEXT_hash_sha512)
509 tlsext_sigalg(TLSEXT_hash_sha384)
510 tlsext_sigalg(TLSEXT_hash_sha256)
Adam Langleyfcf25832014-12-18 17:42:32 -0800511 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700512};
David Benjamin05da6e12014-07-12 20:42:55 -0400513
David Benjamin0d56f882015-12-19 17:05:56 -0500514size_t tls12_get_psigalgs(SSL *ssl, const uint8_t **psigs) {
David Benjamin3dd90162015-08-09 11:26:40 -0400515 *psigs = tls12_sigalgs;
516 return sizeof(tls12_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800517}
Adam Langley95c29f32014-06-20 12:00:00 -0700518
David Benjamin6e807652015-11-02 12:02:20 -0500519int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert,
520 uint8_t hash, uint8_t signature, EVP_PKEY *pkey) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800521 const uint8_t *sent_sigs;
522 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400523 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800524
525 /* Should never happen */
526 if (sigalg == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400527 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 *out_alert = SSL_AD_INTERNAL_ERROR;
529 return 0;
530 }
531
Adam Langleyfcf25832014-12-18 17:42:32 -0800532 /* Check key type is consistent with signature */
533 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400534 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
536 return 0;
537 }
538
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500540 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800541 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
542 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
543 break;
544 }
545 }
546
David Benjamin788be4a2015-10-30 17:50:57 -0400547 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400548 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
550 return 0;
551 }
552
553 *out_md = tls12_get_hash(hash);
554 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400555 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800556 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
557 return 0;
558 }
559
560 return 1;
561}
562
563/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
564 * supported or doesn't appear in supported signature algorithms. Unlike
565 * ssl_cipher_get_disabled this applies to a specific session and not global
566 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500567void ssl_set_client_disabled(SSL *ssl) {
568 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800569 const uint8_t *sigalgs;
570 size_t i, sigalgslen;
571 int have_rsa = 0, have_ecdsa = 0;
572 c->mask_a = 0;
573 c->mask_k = 0;
574
Adam Langleyfcf25832014-12-18 17:42:32 -0800575 /* Now go through all signature algorithms seeing if we support any for RSA,
576 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500577 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800578 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
579 switch (sigalgs[1]) {
580 case TLSEXT_signature_rsa:
581 have_rsa = 1;
582 break;
583
584 case TLSEXT_signature_ecdsa:
585 have_ecdsa = 1;
586 break;
587 }
588 }
589
590 /* Disable auth if we don't include any appropriate signature algorithms. */
591 if (!have_rsa) {
592 c->mask_a |= SSL_aRSA;
593 }
594 if (!have_ecdsa) {
595 c->mask_a |= SSL_aECDSA;
596 }
597
598 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500599 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800600 c->mask_a |= SSL_aPSK;
601 c->mask_k |= SSL_kPSK;
602 }
603}
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langley614c66a2015-06-12 15:26:58 -0700605/* tls_extension represents a TLS extension that is handled internally. The
606 * |init| function is called for each handshake, before any other functions of
607 * the extension. Then the add and parse callbacks are called as needed.
608 *
609 * The parse callbacks receive a |CBS| that contains the contents of the
610 * extension (i.e. not including the type and length bytes). If an extension is
611 * not received then the parse callbacks will be called with a NULL CBS so that
612 * they can do any processing needed to handle the absence of an extension.
613 *
614 * The add callbacks receive a |CBB| to which the extension can be appended but
615 * the function is responsible for appending the type and length bytes too.
616 *
617 * All callbacks return one for success and zero for error. If a parse function
618 * returns zero then a fatal alert with value |*out_alert| will be sent. If
619 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
620struct tls_extension {
621 uint16_t value;
622 void (*init)(SSL *ssl);
623
624 int (*add_clienthello)(SSL *ssl, CBB *out);
625 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
626
627 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
628 int (*add_serverhello)(SSL *ssl, CBB *out);
629};
630
631
632/* Server name indication (SNI).
633 *
634 * https://tools.ietf.org/html/rfc6066#section-3. */
635
636static void ext_sni_init(SSL *ssl) {
637 ssl->s3->tmp.should_ack_sni = 0;
638}
639
640static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
641 if (ssl->tlsext_hostname == NULL) {
642 return 1;
643 }
644
645 CBB contents, server_name_list, name;
646 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
647 !CBB_add_u16_length_prefixed(out, &contents) ||
648 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
649 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
650 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
651 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
652 strlen(ssl->tlsext_hostname)) ||
653 !CBB_flush(out)) {
654 return 0;
655 }
656
657 return 1;
658}
659
David Benjamin0d56f882015-12-19 17:05:56 -0500660static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
661 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700662 if (contents == NULL) {
663 return 1;
664 }
665
666 if (CBS_len(contents) != 0) {
667 return 0;
668 }
669
670 assert(ssl->tlsext_hostname != NULL);
671
672 if (!ssl->hit) {
673 assert(ssl->session->tlsext_hostname == NULL);
674 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
675 if (!ssl->session->tlsext_hostname) {
676 *out_alert = SSL_AD_INTERNAL_ERROR;
677 return 0;
678 }
679 }
680
681 return 1;
682}
683
David Benjamin0d56f882015-12-19 17:05:56 -0500684static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
685 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700686 if (contents == NULL) {
687 return 1;
688 }
689
David Benjamin9b611e22016-03-03 08:48:30 -0500690 CBS server_name_list, host_name;
691 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700692 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500693 !CBS_get_u8(&server_name_list, &name_type) ||
694 /* Although the server_name extension was intended to be extensible to
695 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
696 * different name types will cause an error. Further, RFC 4366 originally
697 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
698 * adding new name types is no longer feasible.
699 *
700 * Act as if the extensibility does not exist to simplify parsing. */
701 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
702 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700703 CBS_len(contents) != 0) {
704 return 0;
705 }
706
David Benjamin9b611e22016-03-03 08:48:30 -0500707 if (name_type != TLSEXT_NAMETYPE_host_name ||
708 CBS_len(&host_name) == 0 ||
709 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
710 CBS_contains_zero_byte(&host_name)) {
711 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
712 return 0;
713 }
Adam Langley614c66a2015-06-12 15:26:58 -0700714
David Benjamin9b611e22016-03-03 08:48:30 -0500715 /* TODO(davidben): SNI should be resolved before resumption. We have the
716 * early callback as a replacement, but we should fix the current callback
717 * and avoid the need for |SSL_CTX_set_session_id_context|. */
718 if (!ssl->hit) {
719 assert(ssl->session->tlsext_hostname == NULL);
720
721 /* Copy the hostname as a string. */
722 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
723 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700724 return 0;
725 }
726
David Benjamin9b611e22016-03-03 08:48:30 -0500727 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700728 }
729
730 return 1;
731}
732
733static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
734 if (ssl->hit ||
735 !ssl->s3->tmp.should_ack_sni ||
736 ssl->session->tlsext_hostname == NULL) {
737 return 1;
738 }
739
740 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
741 !CBB_add_u16(out, 0 /* length */)) {
742 return 0;
743 }
744
745 return 1;
746}
747
748
Adam Langley5021b222015-06-12 18:27:58 -0700749/* Renegotiation indication.
750 *
751 * https://tools.ietf.org/html/rfc5746 */
752
753static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
754 CBB contents, prev_finished;
755 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
756 !CBB_add_u16_length_prefixed(out, &contents) ||
757 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
758 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
759 ssl->s3->previous_client_finished_len) ||
760 !CBB_flush(out)) {
761 return 0;
762 }
763
764 return 1;
765}
766
767static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
768 CBS *contents) {
David Benjamin3e052de2015-11-25 20:10:31 -0500769 /* Servers may not switch between omitting the extension and supporting it.
770 * See RFC 5746, sections 3.5 and 4.2. */
771 if (ssl->s3->initial_handshake_complete &&
772 (contents != NULL) != ssl->s3->send_connection_binding) {
773 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
774 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
775 return 0;
776 }
777
Adam Langley5021b222015-06-12 18:27:58 -0700778 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500779 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700780 * RI even on initial ServerHello because the client doesn't see any
781 * renegotiation during an attack. However this would mean we could not
782 * connect to any server which doesn't support RI.
783 *
David Benjamine9cddb82015-11-23 14:36:40 -0500784 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
785 * practical terms every client sets it so it's just assumed here. */
786 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700787 }
788
789 const size_t expected_len = ssl->s3->previous_client_finished_len +
790 ssl->s3->previous_server_finished_len;
791
792 /* Check for logic errors */
793 assert(!expected_len || ssl->s3->previous_client_finished_len);
794 assert(!expected_len || ssl->s3->previous_server_finished_len);
795
796 /* Parse out the extension contents. */
797 CBS renegotiated_connection;
798 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
799 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400800 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700801 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
802 return 0;
803 }
804
805 /* Check that the extension matches. */
806 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400807 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700808 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
809 return 0;
810 }
811
812 const uint8_t *d = CBS_data(&renegotiated_connection);
813 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
814 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400815 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700816 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
817 return 0;
818 }
819 d += ssl->s3->previous_client_finished_len;
820
821 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
822 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400823 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700824 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
825 return 0;
826 }
827 ssl->s3->send_connection_binding = 1;
828
829 return 1;
830}
831
832static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
833 CBS *contents) {
834 /* Renegotiation isn't supported as a server so this function should never be
835 * called after the initial handshake. */
836 assert(!ssl->s3->initial_handshake_complete);
837
838 CBS fake_contents;
839 static const uint8_t kFakeExtension[] = {0};
840
841 if (contents == NULL) {
842 if (ssl->s3->send_connection_binding) {
843 /* The renegotiation SCSV was received so pretend that we received a
844 * renegotiation extension. */
845 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
846 contents = &fake_contents;
847 /* We require that the renegotiation extension is at index zero of
848 * kExtensions. */
849 ssl->s3->tmp.extensions.received |= (1u << 0);
850 } else {
851 return 1;
852 }
853 }
854
855 CBS renegotiated_connection;
856
857 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
858 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400859 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700860 return 0;
861 }
862
863 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500864 if (!CBS_mem_equal(&renegotiated_connection,
865 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700866 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400867 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700868 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
869 return 0;
870 }
871
872 ssl->s3->send_connection_binding = 1;
873
874 return 1;
875}
876
877static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
878 CBB contents, prev_finished;
879 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
880 !CBB_add_u16_length_prefixed(out, &contents) ||
881 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
882 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
883 ssl->s3->previous_client_finished_len) ||
884 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
885 ssl->s3->previous_server_finished_len) ||
886 !CBB_flush(out)) {
887 return 0;
888 }
889
890 return 1;
891}
892
Adam Langley0a056712015-07-01 15:03:33 -0700893
894/* Extended Master Secret.
895 *
David Benjamin43946d42016-02-01 08:42:19 -0500896 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700897
898static void ext_ems_init(SSL *ssl) {
899 ssl->s3->tmp.extended_master_secret = 0;
900}
901
902static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
903 if (ssl->version == SSL3_VERSION) {
904 return 1;
905 }
906
907 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
908 !CBB_add_u16(out, 0 /* length */)) {
909 return 0;
910 }
911
912 return 1;
913}
914
915static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
916 CBS *contents) {
917 if (contents == NULL) {
918 return 1;
919 }
920
921 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
922 return 0;
923 }
924
925 ssl->s3->tmp.extended_master_secret = 1;
926 return 1;
927}
928
David Benjamin0d56f882015-12-19 17:05:56 -0500929static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
930 CBS *contents) {
Adam Langley0a056712015-07-01 15:03:33 -0700931 if (ssl->version == SSL3_VERSION || contents == NULL) {
932 return 1;
933 }
934
935 if (CBS_len(contents) != 0) {
936 return 0;
937 }
938
939 ssl->s3->tmp.extended_master_secret = 1;
940 return 1;
941}
942
943static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
944 if (!ssl->s3->tmp.extended_master_secret) {
945 return 1;
946 }
947
948 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
949 !CBB_add_u16(out, 0 /* length */)) {
950 return 0;
951 }
952
953 return 1;
954}
955
Adam Langley9b05bc52015-07-01 15:25:33 -0700956
957/* Session tickets.
958 *
959 * https://tools.ietf.org/html/rfc5077 */
960
961static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
962 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
963 return 1;
964 }
965
966 const uint8_t *ticket_data = NULL;
967 int ticket_len = 0;
968
969 /* Renegotiation does not participate in session resumption. However, still
970 * advertise the extension to avoid potentially breaking servers which carry
971 * over the state from the previous handshake, such as OpenSSL servers
972 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
973 if (!ssl->s3->initial_handshake_complete &&
974 ssl->session != NULL &&
975 ssl->session->tlsext_tick != NULL) {
976 ticket_data = ssl->session->tlsext_tick;
977 ticket_len = ssl->session->tlsext_ticklen;
978 }
979
980 CBB ticket;
981 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
982 !CBB_add_u16_length_prefixed(out, &ticket) ||
983 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
984 !CBB_flush(out)) {
985 return 0;
986 }
987
988 return 1;
989}
990
991static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
992 CBS *contents) {
993 ssl->tlsext_ticket_expected = 0;
994
995 if (contents == NULL) {
996 return 1;
997 }
998
999 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1000 * this function should never be called, even if the server tries to send the
1001 * extension. */
1002 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1003
1004 if (CBS_len(contents) != 0) {
1005 return 0;
1006 }
1007
1008 ssl->tlsext_ticket_expected = 1;
1009 return 1;
1010}
1011
David Benjamin0d56f882015-12-19 17:05:56 -05001012static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1013 CBS *contents) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001014 /* This function isn't used because the ticket extension from the client is
David Benjaminc7ce9772015-10-09 19:32:41 -04001015 * handled in ssl_session.c. */
Adam Langley9b05bc52015-07-01 15:25:33 -07001016 return 1;
1017}
1018
1019static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1020 if (!ssl->tlsext_ticket_expected) {
1021 return 1;
1022 }
1023
1024 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1025 * true. */
1026 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1027
1028 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1029 !CBB_add_u16(out, 0 /* length */)) {
1030 return 0;
1031 }
1032
1033 return 1;
1034}
1035
1036
Adam Langley2e857bd2015-07-01 16:09:19 -07001037/* Signature Algorithms.
1038 *
1039 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1040
1041static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1042 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1043 return 1;
1044 }
1045
1046 const uint8_t *sigalgs_data;
1047 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1048
1049 CBB contents, sigalgs;
1050 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1051 !CBB_add_u16_length_prefixed(out, &contents) ||
1052 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1053 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1054 !CBB_flush(out)) {
1055 return 0;
1056 }
1057
1058 return 1;
1059}
1060
1061static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1062 CBS *contents) {
1063 if (contents != NULL) {
1064 /* Servers MUST NOT send this extension. */
1065 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001066 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001067 return 0;
1068 }
1069
1070 return 1;
1071}
1072
1073static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1074 CBS *contents) {
1075 OPENSSL_free(ssl->cert->peer_sigalgs);
1076 ssl->cert->peer_sigalgs = NULL;
1077 ssl->cert->peer_sigalgslen = 0;
1078
Adam Langley2e857bd2015-07-01 16:09:19 -07001079 if (contents == NULL) {
1080 return 1;
1081 }
1082
1083 CBS supported_signature_algorithms;
1084 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001085 CBS_len(contents) != 0 ||
1086 CBS_len(&supported_signature_algorithms) == 0 ||
1087 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001088 return 0;
1089 }
1090
1091 return 1;
1092}
1093
1094static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1095 /* Servers MUST NOT send this extension. */
1096 return 1;
1097}
1098
1099
Adam Langleybb0bd042015-07-01 16:21:03 -07001100/* OCSP Stapling.
1101 *
1102 * https://tools.ietf.org/html/rfc6066#section-8 */
1103
1104static void ext_ocsp_init(SSL *ssl) {
1105 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001106 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001107}
1108
1109static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1110 if (!ssl->ocsp_stapling_enabled) {
1111 return 1;
1112 }
1113
1114 CBB contents;
1115 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1116 !CBB_add_u16_length_prefixed(out, &contents) ||
1117 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1118 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1119 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1120 !CBB_flush(out)) {
1121 return 0;
1122 }
1123
Adam Langleyce9d85e2016-01-24 15:58:39 -08001124 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001125 return 1;
1126}
1127
1128static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001129 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001130 if (contents == NULL) {
1131 return 1;
1132 }
1133
1134 if (CBS_len(contents) != 0) {
1135 return 0;
1136 }
1137
1138 ssl->s3->tmp.certificate_status_expected = 1;
1139 return 1;
1140}
1141
1142static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1143 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001144 if (contents == NULL) {
1145 return 1;
1146 }
1147
1148 uint8_t status_type;
1149 if (!CBS_get_u8(contents, &status_type)) {
1150 return 0;
1151 }
1152
1153 /* We cannot decide whether OCSP stapling will occur yet because the correct
1154 * SSL_CTX might not have been selected. */
1155 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1156
Adam Langleybb0bd042015-07-01 16:21:03 -07001157 return 1;
1158}
1159
1160static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001161 /* The extension shouldn't be sent when resuming sessions. */
1162 if (ssl->hit ||
1163 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001164 ssl->ctx->ocsp_response_length == 0) {
1165 return 1;
1166 }
1167
1168 ssl->s3->tmp.certificate_status_expected = 1;
1169
1170 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1171 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001172}
1173
1174
Adam Langley97dfcbf2015-07-01 18:35:20 -07001175/* Next protocol negotiation.
1176 *
1177 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1178
1179static void ext_npn_init(SSL *ssl) {
1180 ssl->s3->next_proto_neg_seen = 0;
1181}
1182
1183static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1184 if (ssl->s3->initial_handshake_complete ||
1185 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001186 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001187 SSL_IS_DTLS(ssl)) {
1188 return 1;
1189 }
1190
1191 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1192 !CBB_add_u16(out, 0 /* length */)) {
1193 return 0;
1194 }
1195
1196 return 1;
1197}
1198
1199static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1200 CBS *contents) {
1201 if (contents == NULL) {
1202 return 1;
1203 }
1204
1205 /* If any of these are false then we should never have sent the NPN
1206 * extension in the ClientHello and thus this function should never have been
1207 * called. */
1208 assert(!ssl->s3->initial_handshake_complete);
1209 assert(!SSL_IS_DTLS(ssl));
1210 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001211 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001212
David Benjamin76c2efc2015-08-31 14:24:29 -04001213 if (ssl->s3->alpn_selected != NULL) {
1214 /* NPN and ALPN may not be negotiated in the same connection. */
1215 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1216 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1217 return 0;
1218 }
1219
Adam Langley97dfcbf2015-07-01 18:35:20 -07001220 const uint8_t *const orig_contents = CBS_data(contents);
1221 const size_t orig_len = CBS_len(contents);
1222
1223 while (CBS_len(contents) != 0) {
1224 CBS proto;
1225 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1226 CBS_len(&proto) == 0) {
1227 return 0;
1228 }
1229 }
1230
1231 uint8_t *selected;
1232 uint8_t selected_len;
1233 if (ssl->ctx->next_proto_select_cb(
1234 ssl, &selected, &selected_len, orig_contents, orig_len,
1235 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1236 *out_alert = SSL_AD_INTERNAL_ERROR;
1237 return 0;
1238 }
1239
David Benjamin79978df2015-12-25 15:56:49 -05001240 OPENSSL_free(ssl->s3->next_proto_negotiated);
1241 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1242 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001243 *out_alert = SSL_AD_INTERNAL_ERROR;
1244 return 0;
1245 }
1246
David Benjamin79978df2015-12-25 15:56:49 -05001247 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001248 ssl->s3->next_proto_neg_seen = 1;
1249
1250 return 1;
1251}
1252
1253static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1254 CBS *contents) {
1255 if (contents != NULL && CBS_len(contents) != 0) {
1256 return 0;
1257 }
1258
1259 if (contents == NULL ||
1260 ssl->s3->initial_handshake_complete ||
1261 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1262 * afterwards, parsing the ALPN extension will clear
1263 * |next_proto_neg_seen|. */
1264 ssl->s3->alpn_selected != NULL ||
1265 ssl->ctx->next_protos_advertised_cb == NULL ||
1266 SSL_IS_DTLS(ssl)) {
1267 return 1;
1268 }
1269
1270 ssl->s3->next_proto_neg_seen = 1;
1271 return 1;
1272}
1273
1274static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1275 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1276 * parsed. */
1277 if (!ssl->s3->next_proto_neg_seen) {
1278 return 1;
1279 }
1280
1281 const uint8_t *npa;
1282 unsigned npa_len;
1283
1284 if (ssl->ctx->next_protos_advertised_cb(
1285 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1286 SSL_TLSEXT_ERR_OK) {
1287 ssl->s3->next_proto_neg_seen = 0;
1288 return 1;
1289 }
1290
1291 CBB contents;
1292 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1293 !CBB_add_u16_length_prefixed(out, &contents) ||
1294 !CBB_add_bytes(&contents, npa, npa_len) ||
1295 !CBB_flush(out)) {
1296 return 0;
1297 }
1298
1299 return 1;
1300}
1301
1302
Adam Langleyab8d87d2015-07-10 12:21:39 -07001303/* Signed certificate timestamps.
1304 *
1305 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1306
1307static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1308 if (!ssl->signed_cert_timestamps_enabled) {
1309 return 1;
1310 }
1311
1312 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1313 !CBB_add_u16(out, 0 /* length */)) {
1314 return 0;
1315 }
1316
1317 return 1;
1318}
1319
1320static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1321 CBS *contents) {
1322 if (contents == NULL) {
1323 return 1;
1324 }
1325
1326 /* If this is false then we should never have sent the SCT extension in the
1327 * ClientHello and thus this function should never have been called. */
1328 assert(ssl->signed_cert_timestamps_enabled);
1329
1330 if (CBS_len(contents) == 0) {
1331 *out_alert = SSL_AD_DECODE_ERROR;
1332 return 0;
1333 }
1334
1335 /* Session resumption uses the original session information. */
1336 if (!ssl->hit &&
1337 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1338 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1339 *out_alert = SSL_AD_INTERNAL_ERROR;
1340 return 0;
1341 }
1342
1343 return 1;
1344}
1345
1346static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1347 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001348 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001349}
1350
1351static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001352 /* The extension shouldn't be sent when resuming sessions. */
1353 if (ssl->hit ||
1354 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001355 return 1;
1356 }
1357
1358 CBB contents;
1359 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1360 CBB_add_u16_length_prefixed(out, &contents) &&
1361 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1362 ssl->ctx->signed_cert_timestamp_list_length) &&
1363 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001364}
1365
1366
Adam Langleyf18e4532015-07-10 13:39:53 -07001367/* Application-level Protocol Negotiation.
1368 *
1369 * https://tools.ietf.org/html/rfc7301 */
1370
1371static void ext_alpn_init(SSL *ssl) {
1372 OPENSSL_free(ssl->s3->alpn_selected);
1373 ssl->s3->alpn_selected = NULL;
1374}
1375
1376static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1377 if (ssl->alpn_client_proto_list == NULL ||
1378 ssl->s3->initial_handshake_complete) {
1379 return 1;
1380 }
1381
1382 CBB contents, proto_list;
1383 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1384 !CBB_add_u16_length_prefixed(out, &contents) ||
1385 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1386 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1387 ssl->alpn_client_proto_list_len) ||
1388 !CBB_flush(out)) {
1389 return 0;
1390 }
1391
1392 return 1;
1393}
1394
1395static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1396 CBS *contents) {
1397 if (contents == NULL) {
1398 return 1;
1399 }
1400
1401 assert(!ssl->s3->initial_handshake_complete);
1402 assert(ssl->alpn_client_proto_list != NULL);
1403
David Benjamin76c2efc2015-08-31 14:24:29 -04001404 if (ssl->s3->next_proto_neg_seen) {
1405 /* NPN and ALPN may not be negotiated in the same connection. */
1406 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1407 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1408 return 0;
1409 }
1410
Adam Langleyf18e4532015-07-10 13:39:53 -07001411 /* The extension data consists of a ProtocolNameList which must have
1412 * exactly one ProtocolName. Each of these is length-prefixed. */
1413 CBS protocol_name_list, protocol_name;
1414 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1415 CBS_len(contents) != 0 ||
1416 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1417 /* Empty protocol names are forbidden. */
1418 CBS_len(&protocol_name) == 0 ||
1419 CBS_len(&protocol_name_list) != 0) {
1420 return 0;
1421 }
1422
1423 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1424 &ssl->s3->alpn_selected_len)) {
1425 *out_alert = SSL_AD_INTERNAL_ERROR;
1426 return 0;
1427 }
1428
1429 return 1;
1430}
1431
1432static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1433 CBS *contents) {
1434 if (contents == NULL) {
1435 return 1;
1436 }
1437
1438 if (ssl->ctx->alpn_select_cb == NULL ||
1439 ssl->s3->initial_handshake_complete) {
1440 return 1;
1441 }
1442
1443 /* ALPN takes precedence over NPN. */
1444 ssl->s3->next_proto_neg_seen = 0;
1445
1446 CBS protocol_name_list;
1447 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1448 CBS_len(contents) != 0 ||
1449 CBS_len(&protocol_name_list) < 2) {
1450 return 0;
1451 }
1452
1453 /* Validate the protocol list. */
1454 CBS protocol_name_list_copy = protocol_name_list;
1455 while (CBS_len(&protocol_name_list_copy) > 0) {
1456 CBS protocol_name;
1457
1458 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1459 /* Empty protocol names are forbidden. */
1460 CBS_len(&protocol_name) == 0) {
1461 return 0;
1462 }
1463 }
1464
1465 const uint8_t *selected;
1466 uint8_t selected_len;
1467 if (ssl->ctx->alpn_select_cb(
1468 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1469 CBS_len(&protocol_name_list),
1470 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1471 OPENSSL_free(ssl->s3->alpn_selected);
1472 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1473 if (ssl->s3->alpn_selected == NULL) {
1474 *out_alert = SSL_AD_INTERNAL_ERROR;
1475 return 0;
1476 }
1477 ssl->s3->alpn_selected_len = selected_len;
1478 }
1479
1480 return 1;
1481}
1482
1483static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1484 if (ssl->s3->alpn_selected == NULL) {
1485 return 1;
1486 }
1487
1488 CBB contents, proto_list, proto;
1489 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1490 !CBB_add_u16_length_prefixed(out, &contents) ||
1491 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1492 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001493 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1494 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001495 !CBB_flush(out)) {
1496 return 0;
1497 }
1498
1499 return 1;
1500}
1501
1502
Adam Langley49c7af12015-07-10 14:33:46 -07001503/* Channel ID.
1504 *
1505 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1506
1507static void ext_channel_id_init(SSL *ssl) {
1508 ssl->s3->tlsext_channel_id_valid = 0;
1509}
1510
1511static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1512 if (!ssl->tlsext_channel_id_enabled ||
1513 SSL_IS_DTLS(ssl)) {
1514 return 1;
1515 }
1516
1517 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1518 !CBB_add_u16(out, 0 /* length */)) {
1519 return 0;
1520 }
1521
1522 return 1;
1523}
1524
1525static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1526 CBS *contents) {
1527 if (contents == NULL) {
1528 return 1;
1529 }
1530
1531 assert(!SSL_IS_DTLS(ssl));
1532 assert(ssl->tlsext_channel_id_enabled);
1533
1534 if (CBS_len(contents) != 0) {
1535 return 0;
1536 }
1537
1538 ssl->s3->tlsext_channel_id_valid = 1;
1539 return 1;
1540}
1541
1542static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1543 CBS *contents) {
1544 if (contents == NULL ||
1545 !ssl->tlsext_channel_id_enabled ||
1546 SSL_IS_DTLS(ssl)) {
1547 return 1;
1548 }
1549
1550 if (CBS_len(contents) != 0) {
1551 return 0;
1552 }
1553
1554 ssl->s3->tlsext_channel_id_valid = 1;
1555 return 1;
1556}
1557
1558static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1559 if (!ssl->s3->tlsext_channel_id_valid) {
1560 return 1;
1561 }
1562
1563 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1564 !CBB_add_u16(out, 0 /* length */)) {
1565 return 0;
1566 }
1567
1568 return 1;
1569}
1570
Adam Langley391250d2015-07-15 19:06:07 -07001571
1572/* Secure Real-time Transport Protocol (SRTP) extension.
1573 *
1574 * https://tools.ietf.org/html/rfc5764 */
1575
Adam Langley391250d2015-07-15 19:06:07 -07001576
1577static void ext_srtp_init(SSL *ssl) {
1578 ssl->srtp_profile = NULL;
1579}
1580
1581static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1582 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1583 if (profiles == NULL) {
1584 return 1;
1585 }
1586 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1587 if (num_profiles == 0) {
1588 return 1;
1589 }
1590
1591 CBB contents, profile_ids;
1592 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1593 !CBB_add_u16_length_prefixed(out, &contents) ||
1594 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1595 return 0;
1596 }
1597
1598 size_t i;
1599 for (i = 0; i < num_profiles; i++) {
1600 if (!CBB_add_u16(&profile_ids,
1601 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1602 return 0;
1603 }
1604 }
1605
1606 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1607 !CBB_flush(out)) {
1608 return 0;
1609 }
1610
1611 return 1;
1612}
1613
1614static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1615 CBS *contents) {
1616 if (contents == NULL) {
1617 return 1;
1618 }
1619
1620 /* The extension consists of a u16-prefixed profile ID list containing a
1621 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1622 *
1623 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1624 CBS profile_ids, srtp_mki;
1625 uint16_t profile_id;
1626 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1627 !CBS_get_u16(&profile_ids, &profile_id) ||
1628 CBS_len(&profile_ids) != 0 ||
1629 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1630 CBS_len(contents) != 0) {
1631 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1632 return 0;
1633 }
1634
1635 if (CBS_len(&srtp_mki) != 0) {
1636 /* Must be no MKI, since we never offer one. */
1637 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1638 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1639 return 0;
1640 }
1641
1642 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1643
1644 /* Check to see if the server gave us something we support (and presumably
1645 * offered). */
1646 size_t i;
1647 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1648 const SRTP_PROTECTION_PROFILE *profile =
1649 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1650
1651 if (profile->id == profile_id) {
1652 ssl->srtp_profile = profile;
1653 return 1;
1654 }
1655 }
1656
1657 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1658 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1659 return 0;
1660}
1661
1662static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1663 CBS *contents) {
1664 if (contents == NULL) {
1665 return 1;
1666 }
1667
1668 CBS profile_ids, srtp_mki;
1669 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1670 CBS_len(&profile_ids) < 2 ||
1671 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1672 CBS_len(contents) != 0) {
1673 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1674 return 0;
1675 }
1676 /* Discard the MKI value for now. */
1677
1678 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1679 SSL_get_srtp_profiles(ssl);
1680
1681 /* Pick the server's most preferred profile. */
1682 size_t i;
1683 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1684 const SRTP_PROTECTION_PROFILE *server_profile =
1685 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1686
1687 CBS profile_ids_tmp;
1688 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1689
1690 while (CBS_len(&profile_ids_tmp) > 0) {
1691 uint16_t profile_id;
1692 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1693 return 0;
1694 }
1695
1696 if (server_profile->id == profile_id) {
1697 ssl->srtp_profile = server_profile;
1698 return 1;
1699 }
1700 }
1701 }
1702
1703 return 1;
1704}
1705
1706static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1707 if (ssl->srtp_profile == NULL) {
1708 return 1;
1709 }
1710
1711 CBB contents, profile_ids;
1712 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1713 !CBB_add_u16_length_prefixed(out, &contents) ||
1714 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1715 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1716 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1717 !CBB_flush(out)) {
1718 return 0;
1719 }
1720
1721 return 1;
1722}
1723
Adam Langleybdd5d662015-07-20 16:19:08 -07001724
1725/* EC point formats.
1726 *
1727 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1728
1729static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1730 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1731 return 0;
1732 }
1733
1734 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1735
1736 size_t i;
1737 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1738 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1739
1740 const uint32_t alg_k = cipher->algorithm_mkey;
1741 const uint32_t alg_a = cipher->algorithm_auth;
1742 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1743 return 1;
1744 }
1745 }
1746
1747 return 0;
1748}
1749
Adam Langleybdd5d662015-07-20 16:19:08 -07001750static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001751 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001752 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1753 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001754 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1755 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001756 !CBB_flush(out)) {
1757 return 0;
1758 }
1759
1760 return 1;
1761}
1762
1763static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1764 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1765 return 1;
1766 }
1767
1768 return ext_ec_point_add_extension(ssl, out);
1769}
1770
1771static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1772 CBS *contents) {
1773 if (contents == NULL) {
1774 return 1;
1775 }
1776
1777 CBS ec_point_format_list;
1778 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1779 CBS_len(contents) != 0) {
1780 return 0;
1781 }
1782
David Benjaminfc059942015-07-30 23:01:59 -04001783 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1784 * point format. */
1785 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1786 CBS_len(&ec_point_format_list)) == NULL) {
1787 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001788 return 0;
1789 }
1790
1791 return 1;
1792}
1793
1794static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1795 CBS *contents) {
1796 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1797}
1798
1799static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1800 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1801 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001802 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001803
1804 if (!using_ecc) {
1805 return 1;
1806 }
1807
1808 return ext_ec_point_add_extension(ssl, out);
1809}
1810
Adam Langley273d49c2015-07-20 16:38:52 -07001811
Steven Valdezce902a92016-05-17 11:47:53 -04001812/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07001813 *
Steven Valdezce902a92016-05-17 11:47:53 -04001814 * https://tools.ietf.org/html/rfc4492#section-5.1.2
1815 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07001816
Steven Valdezce902a92016-05-17 11:47:53 -04001817static void ext_supported_groups_init(SSL *ssl) {
1818 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
1819 ssl->s3->tmp.peer_supported_group_list = NULL;
1820 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07001821}
1822
Steven Valdezce902a92016-05-17 11:47:53 -04001823static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07001824 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1825 return 1;
1826 }
1827
Steven Valdezce902a92016-05-17 11:47:53 -04001828 CBB contents, groups_bytes;
1829 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07001830 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001831 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07001832 return 0;
1833 }
1834
Steven Valdezce902a92016-05-17 11:47:53 -04001835 const uint16_t *groups;
1836 size_t groups_len;
1837 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07001838
1839 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04001840 for (i = 0; i < groups_len; i++) {
1841 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07001842 return 0;
1843 }
1844 }
1845
1846 return CBB_flush(out);
1847}
1848
Steven Valdezce902a92016-05-17 11:47:53 -04001849static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1850 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07001851 /* This extension is not expected to be echoed by servers and is ignored. */
1852 return 1;
1853}
1854
Steven Valdezce902a92016-05-17 11:47:53 -04001855static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1856 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07001857 if (contents == NULL) {
1858 return 1;
1859 }
1860
Steven Valdezce902a92016-05-17 11:47:53 -04001861 CBS supported_group_list;
1862 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
1863 CBS_len(&supported_group_list) == 0 ||
1864 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07001865 CBS_len(contents) != 0) {
1866 return 0;
1867 }
1868
Steven Valdezce902a92016-05-17 11:47:53 -04001869 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
1870 CBS_len(&supported_group_list));
1871 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07001872 *out_alert = SSL_AD_INTERNAL_ERROR;
1873 return 0;
1874 }
1875
Steven Valdezce902a92016-05-17 11:47:53 -04001876 const size_t num_groups = CBS_len(&supported_group_list) / 2;
Adam Langley273d49c2015-07-20 16:38:52 -07001877 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04001878 for (i = 0; i < num_groups; i++) {
1879 if (!CBS_get_u16(&supported_group_list,
1880 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07001881 goto err;
1882 }
1883 }
1884
Steven Valdezce902a92016-05-17 11:47:53 -04001885 assert(CBS_len(&supported_group_list) == 0);
1886 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07001887
1888 return 1;
1889
1890err:
Steven Valdezce902a92016-05-17 11:47:53 -04001891 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
1892 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07001893 *out_alert = SSL_AD_INTERNAL_ERROR;
1894 return 0;
1895}
1896
Steven Valdezce902a92016-05-17 11:47:53 -04001897static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07001898 /* Servers don't echo this extension. */
1899 return 1;
1900}
1901
1902
Adam Langley614c66a2015-06-12 15:26:58 -07001903/* kExtensions contains all the supported extensions. */
1904static const struct tls_extension kExtensions[] = {
1905 {
Adam Langley5021b222015-06-12 18:27:58 -07001906 /* The renegotiation extension must always be at index zero because the
1907 * |received| and |sent| bitsets need to be tweaked when the "extension" is
1908 * sent as an SCSV. */
1909 TLSEXT_TYPE_renegotiate,
1910 NULL,
1911 ext_ri_add_clienthello,
1912 ext_ri_parse_serverhello,
1913 ext_ri_parse_clienthello,
1914 ext_ri_add_serverhello,
1915 },
1916 {
Adam Langley614c66a2015-06-12 15:26:58 -07001917 TLSEXT_TYPE_server_name,
1918 ext_sni_init,
1919 ext_sni_add_clienthello,
1920 ext_sni_parse_serverhello,
1921 ext_sni_parse_clienthello,
1922 ext_sni_add_serverhello,
1923 },
Adam Langley0a056712015-07-01 15:03:33 -07001924 {
1925 TLSEXT_TYPE_extended_master_secret,
1926 ext_ems_init,
1927 ext_ems_add_clienthello,
1928 ext_ems_parse_serverhello,
1929 ext_ems_parse_clienthello,
1930 ext_ems_add_serverhello,
1931 },
Adam Langley9b05bc52015-07-01 15:25:33 -07001932 {
1933 TLSEXT_TYPE_session_ticket,
1934 NULL,
1935 ext_ticket_add_clienthello,
1936 ext_ticket_parse_serverhello,
1937 ext_ticket_parse_clienthello,
1938 ext_ticket_add_serverhello,
1939 },
Adam Langley2e857bd2015-07-01 16:09:19 -07001940 {
1941 TLSEXT_TYPE_signature_algorithms,
1942 NULL,
1943 ext_sigalgs_add_clienthello,
1944 ext_sigalgs_parse_serverhello,
1945 ext_sigalgs_parse_clienthello,
1946 ext_sigalgs_add_serverhello,
1947 },
Adam Langleybb0bd042015-07-01 16:21:03 -07001948 {
1949 TLSEXT_TYPE_status_request,
1950 ext_ocsp_init,
1951 ext_ocsp_add_clienthello,
1952 ext_ocsp_parse_serverhello,
1953 ext_ocsp_parse_clienthello,
1954 ext_ocsp_add_serverhello,
1955 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07001956 {
1957 TLSEXT_TYPE_next_proto_neg,
1958 ext_npn_init,
1959 ext_npn_add_clienthello,
1960 ext_npn_parse_serverhello,
1961 ext_npn_parse_clienthello,
1962 ext_npn_add_serverhello,
1963 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07001964 {
1965 TLSEXT_TYPE_certificate_timestamp,
1966 NULL,
1967 ext_sct_add_clienthello,
1968 ext_sct_parse_serverhello,
1969 ext_sct_parse_clienthello,
1970 ext_sct_add_serverhello,
1971 },
Adam Langleyf18e4532015-07-10 13:39:53 -07001972 {
1973 TLSEXT_TYPE_application_layer_protocol_negotiation,
1974 ext_alpn_init,
1975 ext_alpn_add_clienthello,
1976 ext_alpn_parse_serverhello,
1977 ext_alpn_parse_clienthello,
1978 ext_alpn_add_serverhello,
1979 },
Adam Langley49c7af12015-07-10 14:33:46 -07001980 {
1981 TLSEXT_TYPE_channel_id,
1982 ext_channel_id_init,
1983 ext_channel_id_add_clienthello,
1984 ext_channel_id_parse_serverhello,
1985 ext_channel_id_parse_clienthello,
1986 ext_channel_id_add_serverhello,
1987 },
Adam Langley391250d2015-07-15 19:06:07 -07001988 {
1989 TLSEXT_TYPE_srtp,
1990 ext_srtp_init,
1991 ext_srtp_add_clienthello,
1992 ext_srtp_parse_serverhello,
1993 ext_srtp_parse_clienthello,
1994 ext_srtp_add_serverhello,
1995 },
Adam Langleybdd5d662015-07-20 16:19:08 -07001996 {
1997 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04001998 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07001999 ext_ec_point_add_clienthello,
2000 ext_ec_point_parse_serverhello,
2001 ext_ec_point_parse_clienthello,
2002 ext_ec_point_add_serverhello,
2003 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002004 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2005 * intolerant to the last extension being zero-length. See
2006 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002007 {
Steven Valdezce902a92016-05-17 11:47:53 -04002008 TLSEXT_TYPE_supported_groups,
2009 ext_supported_groups_init,
2010 ext_supported_groups_add_clienthello,
2011 ext_supported_groups_parse_serverhello,
2012 ext_supported_groups_parse_clienthello,
2013 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002014 },
Adam Langley614c66a2015-06-12 15:26:58 -07002015};
2016
2017#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2018
Adam Langley4cfa96b2015-07-01 11:56:55 -07002019OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2020 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002021 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002022OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2023 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2024 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002025 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002026
Adam Langley614c66a2015-06-12 15:26:58 -07002027static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2028 uint16_t value) {
2029 unsigned i;
2030 for (i = 0; i < kNumExtensions; i++) {
2031 if (kExtensions[i].value == value) {
2032 *out_index = i;
2033 return &kExtensions[i];
2034 }
2035 }
2036
2037 return NULL;
2038}
2039
Adam Langley09505632015-07-30 18:10:13 -07002040int SSL_extension_supported(unsigned extension_value) {
2041 uint32_t index;
2042 return extension_value == TLSEXT_TYPE_padding ||
2043 tls_extension_find(&index, extension_value) != NULL;
2044}
2045
David Benjamine8d53502015-10-10 14:13:23 -04002046int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002047 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002048 if (ssl->client_version == SSL3_VERSION &&
2049 !ssl->s3->send_connection_binding) {
2050 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002051 }
Adam Langley95c29f32014-06-20 12:00:00 -07002052
David Benjamine8d53502015-10-10 14:13:23 -04002053 CBB extensions;
2054 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002055 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002056 }
Adam Langley95c29f32014-06-20 12:00:00 -07002057
David Benjamine8d53502015-10-10 14:13:23 -04002058 ssl->s3->tmp.extensions.sent = 0;
2059 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002060
Adam Langley614c66a2015-06-12 15:26:58 -07002061 size_t i;
2062 for (i = 0; i < kNumExtensions; i++) {
2063 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002064 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002065 }
2066 }
Adam Langley95c29f32014-06-20 12:00:00 -07002067
Adam Langley614c66a2015-06-12 15:26:58 -07002068 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002069 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002070 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002071 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2072 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2073 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002074 }
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langley33ad2b52015-07-20 17:43:53 -07002076 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002077 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002078 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002079 }
Adam Langley75712922014-10-10 16:23:43 -07002080
David Benjamine8d53502015-10-10 14:13:23 -04002081 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002082 goto err;
2083 }
2084
David Benjamine8d53502015-10-10 14:13:23 -04002085 if (!SSL_IS_DTLS(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002086 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002087 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002088 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002089 *
2090 * NB: because this code works out the length of all existing extensions
2091 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002092 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002093 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002094 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002095 * Server 7.0 is intolerant to the last extension being zero-length. See
2096 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002097 if (padding_len >= 4 + 1) {
2098 padding_len -= 4;
2099 } else {
2100 padding_len = 1;
2101 }
Adam Langley95c29f32014-06-20 12:00:00 -07002102
Adam Langley33ad2b52015-07-20 17:43:53 -07002103 uint8_t *padding_bytes;
2104 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2105 !CBB_add_u16(&extensions, padding_len) ||
2106 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2107 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002108 }
Adam Langley75712922014-10-10 16:23:43 -07002109
Adam Langley33ad2b52015-07-20 17:43:53 -07002110 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002111 }
2112 }
Adam Langley75712922014-10-10 16:23:43 -07002113
David Benjamina01deee2015-12-08 18:56:31 -05002114 /* Discard empty extensions blocks. */
2115 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002116 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002117 }
2118
David Benjamine8d53502015-10-10 14:13:23 -04002119 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002120
2121err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002122 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002123 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002124}
Adam Langley95c29f32014-06-20 12:00:00 -07002125
David Benjamin56380462015-10-10 14:59:09 -04002126int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002127 CBB extensions;
2128 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002129 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002130 }
2131
2132 unsigned i;
2133 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002134 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002135 /* Don't send extensions that were not received. */
2136 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002137 }
Adam Langley95c29f32014-06-20 12:00:00 -07002138
David Benjamin56380462015-10-10 14:59:09 -04002139 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002140 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2141 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2142 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002143 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002144 }
Adam Langley95c29f32014-06-20 12:00:00 -07002145
David Benjamin56380462015-10-10 14:59:09 -04002146 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002147 goto err;
2148 }
2149
David Benjamina01deee2015-12-08 18:56:31 -05002150 /* Discard empty extensions blocks. */
2151 if (CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002152 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002153 }
2154
David Benjamin56380462015-10-10 14:59:09 -04002155 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002156
2157err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002158 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002159 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002160}
Adam Langley95c29f32014-06-20 12:00:00 -07002161
David Benjamin0d56f882015-12-19 17:05:56 -05002162static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002163 size_t i;
2164 for (i = 0; i < kNumExtensions; i++) {
2165 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002166 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002167 }
2168 }
2169
David Benjamin0d56f882015-12-19 17:05:56 -05002170 ssl->s3->tmp.extensions.received = 0;
2171 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002172 /* The renegotiation extension must always be at index zero because the
2173 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2174 * sent as an SCSV. */
2175 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002176
Adam Langleyfcf25832014-12-18 17:42:32 -08002177 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002178 if (CBS_len(cbs) != 0) {
2179 /* Decode the extensions block and check it is valid. */
2180 CBS extensions;
2181 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2182 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002183 *out_alert = SSL_AD_DECODE_ERROR;
2184 return 0;
2185 }
Adam Langley95c29f32014-06-20 12:00:00 -07002186
Adam Langley33ad2b52015-07-20 17:43:53 -07002187 while (CBS_len(&extensions) != 0) {
2188 uint16_t type;
2189 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002190
Adam Langley33ad2b52015-07-20 17:43:53 -07002191 /* Decode the next extension. */
2192 if (!CBS_get_u16(&extensions, &type) ||
2193 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2194 *out_alert = SSL_AD_DECODE_ERROR;
2195 return 0;
2196 }
2197
David Benjaminc7ce9772015-10-09 19:32:41 -04002198 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2199 * ambiguous. Ignore all but the renegotiation_info extension. */
David Benjamin0d56f882015-12-19 17:05:56 -05002200 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
David Benjaminc7ce9772015-10-09 19:32:41 -04002201 continue;
2202 }
2203
Adam Langley33ad2b52015-07-20 17:43:53 -07002204 unsigned ext_index;
2205 const struct tls_extension *const ext =
2206 tls_extension_find(&ext_index, type);
2207
2208 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002209 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002210 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2211 return 0;
2212 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002213 continue;
2214 }
2215
David Benjamin0d56f882015-12-19 17:05:56 -05002216 ssl->s3->tmp.extensions.received |= (1u << ext_index);
Adam Langley614c66a2015-06-12 15:26:58 -07002217 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002218 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
Adam Langley614c66a2015-06-12 15:26:58 -07002219 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002220 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2221 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002222 return 0;
2223 }
Adam Langley614c66a2015-06-12 15:26:58 -07002224 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002225 }
Adam Langley75712922014-10-10 16:23:43 -07002226
Adam Langley614c66a2015-06-12 15:26:58 -07002227 for (i = 0; i < kNumExtensions; i++) {
David Benjamin0d56f882015-12-19 17:05:56 -05002228 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002229 /* Extension wasn't observed so call the callback with a NULL
2230 * parameter. */
2231 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002232 if (!kExtensions[i].parse_clienthello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002233 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2234 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002235 *out_alert = alert;
2236 return 0;
2237 }
2238 }
2239 }
2240
Adam Langleyfcf25832014-12-18 17:42:32 -08002241 return 1;
2242}
Adam Langley95c29f32014-06-20 12:00:00 -07002243
David Benjamin0d56f882015-12-19 17:05:56 -05002244int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002245 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002246 if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) {
2247 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002248 return 0;
2249 }
Adam Langley95c29f32014-06-20 12:00:00 -07002250
David Benjamin0d56f882015-12-19 17:05:56 -05002251 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002252 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002253 return 0;
2254 }
Adam Langley95c29f32014-06-20 12:00:00 -07002255
Adam Langleyfcf25832014-12-18 17:42:32 -08002256 return 1;
2257}
Adam Langley95c29f32014-06-20 12:00:00 -07002258
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002259OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2260
David Benjamin0d56f882015-12-19 17:05:56 -05002261static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002262 uint32_t received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002263
Adam Langley33ad2b52015-07-20 17:43:53 -07002264 if (CBS_len(cbs) != 0) {
2265 /* Decode the extensions block and check it is valid. */
2266 CBS extensions;
2267 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2268 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002269 *out_alert = SSL_AD_DECODE_ERROR;
2270 return 0;
2271 }
Adam Langley95c29f32014-06-20 12:00:00 -07002272
Adam Langley614c66a2015-06-12 15:26:58 -07002273
Adam Langley33ad2b52015-07-20 17:43:53 -07002274 while (CBS_len(&extensions) != 0) {
2275 uint16_t type;
2276 CBS extension;
2277
2278 /* Decode the next extension. */
2279 if (!CBS_get_u16(&extensions, &type) ||
2280 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2281 *out_alert = SSL_AD_DECODE_ERROR;
2282 return 0;
2283 }
2284
2285 unsigned ext_index;
2286 const struct tls_extension *const ext =
2287 tls_extension_find(&ext_index, type);
2288
Adam Langley09505632015-07-30 18:10:13 -07002289 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002290 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002291 return 0;
2292 }
2293 continue;
2294 }
2295
David Benjamin0d56f882015-12-19 17:05:56 -05002296 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) {
Adam Langley09505632015-07-30 18:10:13 -07002297 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002298 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002299 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002300 *out_alert = SSL_AD_DECODE_ERROR;
2301 return 0;
2302 }
David Benjamin03973092014-06-24 23:27:17 -04002303
Adam Langley614c66a2015-06-12 15:26:58 -07002304 received |= (1u << ext_index);
2305
2306 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002307 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002308 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2309 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002310 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002311 return 0;
2312 }
Adam Langley614c66a2015-06-12 15:26:58 -07002313 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002314 }
Adam Langley95c29f32014-06-20 12:00:00 -07002315
Adam Langley33ad2b52015-07-20 17:43:53 -07002316 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002317 for (i = 0; i < kNumExtensions; i++) {
2318 if (!(received & (1u << i))) {
2319 /* Extension wasn't observed so call the callback with a NULL
2320 * parameter. */
2321 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002322 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002323 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2324 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002325 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002326 return 0;
2327 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002328 }
2329 }
Adam Langley95c29f32014-06-20 12:00:00 -07002330
Adam Langleyfcf25832014-12-18 17:42:32 -08002331 return 1;
2332}
Adam Langley95c29f32014-06-20 12:00:00 -07002333
David Benjamin0d56f882015-12-19 17:05:56 -05002334static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002335 int ret = SSL_TLSEXT_ERR_NOACK;
2336 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002337
David Benjamin78f8aab2016-03-10 16:33:58 -05002338 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002339 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002340 ssl->ctx->tlsext_servername_arg);
2341 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002342 ret = ssl->initial_ctx->tlsext_servername_callback(
2343 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002344 }
Adam Langley95c29f32014-06-20 12:00:00 -07002345
Adam Langleyfcf25832014-12-18 17:42:32 -08002346 switch (ret) {
2347 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002348 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002349 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002350
Adam Langleyfcf25832014-12-18 17:42:32 -08002351 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002352 ssl3_send_alert(ssl, SSL3_AL_WARNING, 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_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002356 ssl->s3->tmp.should_ack_sni = 0;
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 default:
2360 return 1;
2361 }
2362}
Adam Langleyed8270a2014-09-02 13:52:56 -07002363
David Benjamin0d56f882015-12-19 17:05:56 -05002364static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002365 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002366 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002367
David Benjamin78f8aab2016-03-10 16:33:58 -05002368 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002369 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002370 ssl->ctx->tlsext_servername_arg);
2371 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002372 ret = ssl->initial_ctx->tlsext_servername_callback(
2373 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002374 }
Adam Langley95c29f32014-06-20 12:00:00 -07002375
Adam Langleyfcf25832014-12-18 17:42:32 -08002376 switch (ret) {
2377 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002378 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002379 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002380
Adam Langleyfcf25832014-12-18 17:42:32 -08002381 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002382 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002383 return 1;
2384
2385 default:
2386 return 1;
2387 }
2388}
2389
David Benjamin0d56f882015-12-19 17:05:56 -05002390int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002391 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002392 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2393 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002394 return 0;
2395 }
2396
David Benjamin0d56f882015-12-19 17:05:56 -05002397 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002398 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002399 return 0;
2400 }
2401
2402 return 1;
2403}
Adam Langley95c29f32014-06-20 12:00:00 -07002404
David Benjamine3aa1d92015-06-16 15:34:50 -04002405int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002406 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002407 size_t ticket_len, const uint8_t *session_id,
2408 size_t session_id_len) {
2409 int ret = 1; /* Most errors are non-fatal. */
2410 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2411 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002412
David Benjamine3aa1d92015-06-16 15:34:50 -04002413 HMAC_CTX hmac_ctx;
2414 HMAC_CTX_init(&hmac_ctx);
2415 EVP_CIPHER_CTX cipher_ctx;
2416 EVP_CIPHER_CTX_init(&cipher_ctx);
2417
David Benjaminef1b0092015-11-21 14:05:44 -05002418 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002419 *out_session = NULL;
2420
2421 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2422 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002423 }
2424
David Benjaminadcc3952015-04-26 13:07:57 -04002425 /* Ensure there is room for the key name and the largest IV
2426 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2427 * the maximum IV length should be well under the minimum size for the
2428 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002429 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2430 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002431 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002432 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002433
David Benjamine3aa1d92015-06-16 15:34:50 -04002434 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002435 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2436 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2437 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002438 if (cb_ret < 0) {
2439 ret = 0;
2440 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002441 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002442 if (cb_ret == 0) {
2443 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002444 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002445 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002446 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002447 }
2448 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002449 /* Check the key name matches. */
2450 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2451 SSL_TICKET_KEY_NAME_LEN) != 0) {
2452 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002453 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002454 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2455 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002456 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002457 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2458 ssl_ctx->tlsext_tick_aes_key, iv)) {
2459 ret = 0;
2460 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002461 }
2462 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002463 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002464
David Benjamine3aa1d92015-06-16 15:34:50 -04002465 /* Check the MAC at the end of the ticket. */
2466 uint8_t mac[EVP_MAX_MD_SIZE];
2467 size_t mac_len = HMAC_size(&hmac_ctx);
2468 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002469 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002470 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002471 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002472 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2473 HMAC_Final(&hmac_ctx, mac, NULL);
2474 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2475 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002476 }
2477
David Benjamine3aa1d92015-06-16 15:34:50 -04002478 /* Decrypt the session data. */
2479 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2480 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2481 mac_len;
2482 plaintext = OPENSSL_malloc(ciphertext_len);
2483 if (plaintext == NULL) {
2484 ret = 0;
2485 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002486 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002487 if (ciphertext_len >= INT_MAX) {
2488 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002489 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002490 int len1, len2;
2491 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2492 (int)ciphertext_len) ||
2493 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2494 ERR_clear_error(); /* Don't leave an error on the queue. */
2495 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002496 }
2497
David Benjamine3aa1d92015-06-16 15:34:50 -04002498 /* Decode the session. */
2499 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2500 if (session == NULL) {
2501 ERR_clear_error(); /* Don't leave an error on the queue. */
2502 goto done;
2503 }
2504
2505 /* Copy the client's session ID into the new session, to denote the ticket has
2506 * been accepted. */
2507 memcpy(session->session_id, session_id, session_id_len);
2508 session->session_id_length = session_id_len;
2509
2510 *out_session = session;
2511
2512done:
2513 OPENSSL_free(plaintext);
2514 HMAC_CTX_cleanup(&hmac_ctx);
2515 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2516 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002517}
Adam Langley95c29f32014-06-20 12:00:00 -07002518
2519/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002520typedef struct {
2521 int nid;
2522 int id;
2523} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002524
David Benjamin72f7e212016-01-29 15:28:58 -05002525static const tls12_lookup tls12_md[] = {
2526 {NID_sha1, TLSEXT_hash_sha1},
2527 {NID_sha256, TLSEXT_hash_sha256},
2528 {NID_sha384, TLSEXT_hash_sha384},
2529 {NID_sha512, TLSEXT_hash_sha512},
2530};
Adam Langley95c29f32014-06-20 12:00:00 -07002531
Adam Langleyfcf25832014-12-18 17:42:32 -08002532static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2533 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002534
Adam Langleyfcf25832014-12-18 17:42:32 -08002535static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2536 size_t i;
2537 for (i = 0; i < tlen; i++) {
2538 if (table[i].nid == nid) {
2539 return table[i].id;
2540 }
2541 }
Adam Langley95c29f32014-06-20 12:00:00 -07002542
Adam Langleyfcf25832014-12-18 17:42:32 -08002543 return -1;
2544}
Adam Langley95c29f32014-06-20 12:00:00 -07002545
David Benjaminb4d65fd2015-05-29 17:11:21 -04002546int tls12_get_sigid(int pkey_type) {
2547 return tls12_find_id(pkey_type, tls12_sig,
2548 sizeof(tls12_sig) / sizeof(tls12_lookup));
2549}
2550
David Benjaminfc825122015-12-18 01:57:43 -05002551int tls12_add_sigandhash(SSL *ssl, CBB *out, const EVP_MD *md) {
2552 int md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2553 sizeof(tls12_md) / sizeof(tls12_lookup));
2554 int sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langley95c29f32014-06-20 12:00:00 -07002555
David Benjaminfc825122015-12-18 01:57:43 -05002556 return md_id != -1 &&
2557 sig_id != -1 &&
2558 CBB_add_u8(out, (uint8_t)md_id) &&
2559 CBB_add_u8(out, (uint8_t)sig_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08002560}
2561
Adam Langleyfcf25832014-12-18 17:42:32 -08002562const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2563 switch (hash_alg) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002564 case TLSEXT_hash_sha1:
2565 return EVP_sha1();
2566
Adam Langleyfcf25832014-12-18 17:42:32 -08002567 case TLSEXT_hash_sha256:
2568 return EVP_sha256();
2569
2570 case TLSEXT_hash_sha384:
2571 return EVP_sha384();
2572
2573 case TLSEXT_hash_sha512:
2574 return EVP_sha512();
2575
2576 default:
2577 return NULL;
2578 }
2579}
Adam Langley95c29f32014-06-20 12:00:00 -07002580
David Benjaminec2f27d2014-11-13 19:17:25 -05002581/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2582 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002583static int tls12_get_pkey_type(uint8_t sig_alg) {
2584 switch (sig_alg) {
2585 case TLSEXT_signature_rsa:
2586 return EVP_PKEY_RSA;
2587
2588 case TLSEXT_signature_ecdsa:
2589 return EVP_PKEY_EC;
2590
2591 default:
2592 return -1;
2593 }
2594}
Adam Langley95c29f32014-06-20 12:00:00 -07002595
Steven Valdez0d62f262015-09-04 12:41:04 -04002596OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2,
2597 sizeof_tls_sigalgs_is_not_two);
Adam Langleyfcf25832014-12-18 17:42:32 -08002598
Steven Valdez0d62f262015-09-04 12:41:04 -04002599int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002600 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05002601 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002602 return 1;
2603 }
David Benjamincd996942014-07-20 16:23:51 -04002604
Steven Valdez0d62f262015-09-04 12:41:04 -04002605 CERT *const cert = ssl->cert;
2606 OPENSSL_free(cert->peer_sigalgs);
2607 cert->peer_sigalgs = NULL;
2608 cert->peer_sigalgslen = 0;
2609
2610 size_t num_sigalgs = CBS_len(in_sigalgs);
2611
2612 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002613 return 0;
2614 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002615 num_sigalgs /= 2;
2616
2617 /* supported_signature_algorithms in the certificate request is
2618 * allowed to be empty. */
2619 if (num_sigalgs == 0) {
2620 return 1;
2621 }
2622
2623 /* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two
2624 * (statically asserted above) and we just divided |num_sigalgs| by two. */
2625 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS));
2626 if (cert->peer_sigalgs == NULL) {
2627 return 0;
2628 }
2629 cert->peer_sigalgslen = num_sigalgs;
2630
2631 CBS sigalgs;
2632 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2633
2634 size_t i;
2635 for (i = 0; i < num_sigalgs; i++) {
2636 TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i];
2637 if (!CBS_get_u8(&sigalgs, &sigalg->rhash) ||
2638 !CBS_get_u8(&sigalgs, &sigalg->rsign)) {
2639 return 0;
2640 }
2641 }
Adam Langley95c29f32014-06-20 12:00:00 -07002642
Adam Langleyfcf25832014-12-18 17:42:32 -08002643 return 1;
2644}
David Benjaminec2f27d2014-11-13 19:17:25 -05002645
David Benjamind1d80782015-07-05 11:54:09 -04002646const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2647 CERT *cert = ssl->cert;
2648 int type = ssl_private_key_type(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -04002649 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002650
Steven Valdez0d62f262015-09-04 12:41:04 -04002651 static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
David Benjamin72f7e212016-01-29 15:28:58 -05002652 NID_sha1};
Steven Valdez0d62f262015-09-04 12:41:04 -04002653
2654 const int *digest_nids = kDefaultDigestList;
2655 size_t num_digest_nids =
2656 sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
2657 if (cert->digest_nids != NULL) {
2658 digest_nids = cert->digest_nids;
2659 num_digest_nids = cert->num_digest_nids;
2660 }
2661
2662 for (i = 0; i < num_digest_nids; i++) {
2663 const int digest_nid = digest_nids[i];
2664 for (j = 0; j < cert->peer_sigalgslen; j++) {
2665 const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash);
2666 if (md == NULL ||
2667 digest_nid != EVP_MD_type(md) ||
2668 tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) {
2669 continue;
2670 }
2671
2672 return md;
Adam Langleyfcf25832014-12-18 17:42:32 -08002673 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002674 }
Adam Langley95c29f32014-06-20 12:00:00 -07002675
Adam Langleyfcf25832014-12-18 17:42:32 -08002676 /* If no suitable digest may be found, default to SHA-1. */
2677 return EVP_sha1();
2678}
Adam Langley95c29f32014-06-20 12:00:00 -07002679
David Benjamind6a4ae92015-08-06 11:10:51 -04002680int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2681 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002682 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002683
2684 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002685 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2686 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002687 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002688
David Benjamind6a4ae92015-08-06 11:10:51 -04002689 static const char kClientIDMagic[] = "TLS Channel ID signature";
2690 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2691
2692 if (ssl->hit) {
2693 static const char kResumptionMagic[] = "Resumption";
2694 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2695 if (ssl->session->original_handshake_hash_len == 0) {
2696 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2697 goto err;
2698 }
2699 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2700 ssl->session->original_handshake_hash_len);
2701 }
2702
2703 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2704 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2705 sizeof(handshake_hash));
2706 if (handshake_hash_len < 0) {
2707 goto err;
2708 }
2709 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2710 unsigned len_u;
2711 EVP_DigestFinal_ex(&ctx, out, &len_u);
2712 *out_len = len_u;
2713
2714 ret = 1;
2715
2716err:
2717 EVP_MD_CTX_cleanup(&ctx);
2718 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002719}
Adam Langley1258b6a2014-06-20 12:00:00 -07002720
2721/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin0d56f882015-12-19 17:05:56 -05002722 * hashes in |ssl->session| so that Channel ID resumptions can sign that
2723 * data. */
2724int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002725 int digest_len;
2726 /* This function should never be called for a resumed session because the
2727 * handshake hashes that we wish to record are for the original, full
2728 * handshake. */
David Benjamin0d56f882015-12-19 17:05:56 -05002729 if (ssl->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002730 return -1;
2731 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002732
Adam Langleyfcf25832014-12-18 17:42:32 -08002733 digest_len =
David Benjamin0d56f882015-12-19 17:05:56 -05002734 tls1_handshake_digest(ssl, ssl->session->original_handshake_hash,
2735 sizeof(ssl->session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08002736 if (digest_len < 0) {
2737 return -1;
2738 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002739
David Benjamin0d56f882015-12-19 17:05:56 -05002740 ssl->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002741
Adam Langleyfcf25832014-12-18 17:42:32 -08002742 return 1;
2743}