blob: 2b9402d0be85c83bc357facf6e8cbd82c4d9a6fe [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin03973092014-06-24 23:27:17 -0400116#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400117#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/evp.h>
120#include <openssl/hmac.h>
121#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400122#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/rand.h>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700124#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800127
128
David Benjamin0d56f882015-12-19 17:05:56 -0500129static int ssl_check_clienthello_tlsext(SSL *ssl);
130static int ssl_check_serverhello_tlsext(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700131
Adam Langleyfcf25832014-12-18 17:42:32 -0800132static int compare_uint16_t(const void *p1, const void *p2) {
133 uint16_t u1 = *((const uint16_t *)p1);
134 uint16_t u2 = *((const uint16_t *)p2);
135 if (u1 < u2) {
136 return -1;
137 } else if (u1 > u2) {
138 return 1;
139 } else {
140 return 0;
141 }
142}
David Benjamin35a7a442014-07-05 00:23:20 -0400143
Adam Langleyfcf25832014-12-18 17:42:32 -0800144/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
145 * more than one extension of the same type in a ClientHello or ServerHello.
146 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400147 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800148static int tls1_check_duplicate_extensions(const CBS *cbs) {
149 CBS extensions = *cbs;
150 size_t num_extensions = 0, i = 0;
151 uint16_t *extension_types = NULL;
152 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400153
Adam Langleyfcf25832014-12-18 17:42:32 -0800154 /* First pass: count the extensions. */
155 while (CBS_len(&extensions) > 0) {
156 uint16_t type;
157 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400158
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 if (!CBS_get_u16(&extensions, &type) ||
160 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
161 goto done;
162 }
David Benjamin35a7a442014-07-05 00:23:20 -0400163
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 num_extensions++;
165 }
David Benjamin35a7a442014-07-05 00:23:20 -0400166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 if (num_extensions == 0) {
168 return 1;
169 }
David Benjamin9a373592014-07-25 04:27:53 -0400170
Brian Smith5ba06892016-02-07 09:36:04 -1000171 extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800172 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400173 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800174 goto done;
175 }
David Benjamin35a7a442014-07-05 00:23:20 -0400176
Adam Langleyfcf25832014-12-18 17:42:32 -0800177 /* Second pass: gather the extension types. */
178 extensions = *cbs;
179 for (i = 0; i < num_extensions; i++) {
180 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400181
Adam Langleyfcf25832014-12-18 17:42:32 -0800182 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
183 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
184 /* This should not happen. */
185 goto done;
186 }
187 }
188 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400189
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 /* Sort the extensions and make sure there are no duplicates. */
191 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
192 for (i = 1; i < num_extensions; i++) {
193 if (extension_types[i - 1] == extension_types[i]) {
194 goto done;
195 }
196 }
David Benjamin35a7a442014-07-05 00:23:20 -0400197
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 ret = 1;
199
David Benjamin35a7a442014-07-05 00:23:20 -0400200done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400201 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 return ret;
203}
David Benjamin35a7a442014-07-05 00:23:20 -0400204
Adam Langleyfcf25832014-12-18 17:42:32 -0800205char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
206 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400207
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700209
Adam Langleyfcf25832014-12-18 17:42:32 -0800210 if (/* Skip client version. */
211 !CBS_skip(&client_hello, 2) ||
212 /* Skip client nonce. */
213 !CBS_skip(&client_hello, 32) ||
214 /* Extract session_id. */
215 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
216 return 0;
217 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700218
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 ctx->session_id = CBS_data(&session_id);
220 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700221
Adam Langleyfcf25832014-12-18 17:42:32 -0800222 /* Skip past DTLS cookie */
223 if (SSL_IS_DTLS(ctx->ssl)) {
224 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
227 return 0;
228 }
229 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700230
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 /* Extract cipher_suites. */
232 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
233 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
234 return 0;
235 }
236 ctx->cipher_suites = CBS_data(&cipher_suites);
237 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700238
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 /* Extract compression_methods. */
240 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
241 CBS_len(&compression_methods) < 1) {
242 return 0;
243 }
244 ctx->compression_methods = CBS_data(&compression_methods);
245 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700246
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 /* If the ClientHello ends here then it's valid, but doesn't have any
248 * extensions. (E.g. SSLv3.) */
249 if (CBS_len(&client_hello) == 0) {
250 ctx->extensions = NULL;
251 ctx->extensions_len = 0;
252 return 1;
253 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700254
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 /* Extract extensions and check it is valid. */
256 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
257 !tls1_check_duplicate_extensions(&extensions) ||
258 CBS_len(&client_hello) != 0) {
259 return 0;
260 }
261 ctx->extensions = CBS_data(&extensions);
262 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 return 1;
265}
Adam Langleydc9b1412014-06-20 12:00:00 -0700266
David Benjamind4c2bce2015-10-17 12:28:18 -0400267int SSL_early_callback_ctx_extension_get(
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
269 const uint8_t **out_data, size_t *out_len) {
270 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700271
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700273
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 while (CBS_len(&extensions) != 0) {
275 uint16_t type;
276 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400277
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 /* Decode the next extension. */
279 if (!CBS_get_u16(&extensions, &type) ||
280 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
281 return 0;
282 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 if (type == extension_type) {
285 *out_data = CBS_data(&extension);
286 *out_len = CBS_len(&extension);
287 return 1;
288 }
289 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700290
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 return 0;
292}
Adam Langley95c29f32014-06-20 12:00:00 -0700293
Steven Valdezce902a92016-05-17 11:47:53 -0400294static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400295 SSL_CURVE_X25519,
296 SSL_CURVE_SECP256R1,
297 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700298#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin9e68f192016-06-30 14:55:33 -0400299 SSL_CURVE_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700300#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800301};
Adam Langley95c29f32014-06-20 12:00:00 -0700302
Steven Valdezce902a92016-05-17 11:47:53 -0400303/* tls1_get_grouplist sets |*out_group_ids| and |*out_group_ids_len| to the
304 * list of allowed group IDs. If |get_peer_groups| is non-zero, return the
305 * peer's group list. Otherwise, return the preferred list. */
306static void tls1_get_grouplist(SSL *ssl, int get_peer_groups,
307 const uint16_t **out_group_ids,
308 size_t *out_group_ids_len) {
309 if (get_peer_groups) {
310 /* Only clients send a supported group list, so this function is only
311 * called on the server. */
David Benjamin0d56f882015-12-19 17:05:56 -0500312 assert(ssl->server);
Steven Valdezce902a92016-05-17 11:47:53 -0400313 *out_group_ids = ssl->s3->tmp.peer_supported_group_list;
314 *out_group_ids_len = ssl->s3->tmp.peer_supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800315 return;
316 }
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Steven Valdezce902a92016-05-17 11:47:53 -0400318 *out_group_ids = ssl->supported_group_list;
319 *out_group_ids_len = ssl->supported_group_list_len;
320 if (!*out_group_ids) {
321 *out_group_ids = kDefaultGroups;
322 *out_group_ids_len = sizeof(kDefaultGroups) / sizeof(kDefaultGroups[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 }
324}
David Benjamined439582014-07-14 19:13:02 -0400325
Steven Valdezce902a92016-05-17 11:47:53 -0400326int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) {
327 const uint16_t *groups, *peer_groups, *pref, *supp;
328 size_t groups_len, peer_groups_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 /* Can't do anything on client side */
David Benjamin4298d772015-12-19 00:18:25 -0500331 if (ssl->server == 0) {
332 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 }
334
Steven Valdezce902a92016-05-17 11:47:53 -0400335 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
336 tls1_get_grouplist(ssl, 1 /* peer groups */, &peer_groups, &peer_groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800337
Steven Valdezce902a92016-05-17 11:47:53 -0400338 if (peer_groups_len == 0) {
339 /* Clients are not required to send a supported_groups extension. In this
340 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin4298d772015-12-19 00:18:25 -0500341 * section 4, paragraph 3.
342 *
343 * However, in the interests of compatibility, we will skip ECDH if the
344 * client didn't send an extension because we can't be sure that they'll
Steven Valdezce902a92016-05-17 11:47:53 -0400345 * support our favoured group. */
David Benjamin4298d772015-12-19 00:18:25 -0500346 return 0;
David Benjamin55a43642015-04-20 14:45:55 -0400347 }
348
David Benjamin4298d772015-12-19 00:18:25 -0500349 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400350 pref = groups;
351 pref_len = groups_len;
352 supp = peer_groups;
353 supp_len = peer_groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400354 } else {
Steven Valdezce902a92016-05-17 11:47:53 -0400355 pref = peer_groups;
356 pref_len = peer_groups_len;
357 supp = groups;
358 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400359 }
360
361 for (i = 0; i < pref_len; i++) {
362 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400364 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500365 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 }
367 }
368 }
369
David Benjamin4298d772015-12-19 00:18:25 -0500370 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800371}
Adam Langley95c29f32014-06-20 12:00:00 -0700372
Steven Valdezce902a92016-05-17 11:47:53 -0400373int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400375 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 size_t i;
377
Steven Valdezce902a92016-05-17 11:47:53 -0400378 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
379 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 return 0;
381 }
382
383 for (i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400384 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
385 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 return 0;
387 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 }
389
Steven Valdezce902a92016-05-17 11:47:53 -0400390 OPENSSL_free(*out_group_ids);
391 *out_group_ids = group_ids;
392 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800393
394 return 1;
395}
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Steven Valdezce902a92016-05-17 11:47:53 -0400397/* tls1_curve_params_from_ec_key sets |*out_group_id| and |*out_comp_id| to the
398 * TLS group ID and point format, respectively, for |ec|. It returns one on
David Benjamin072334d2014-07-13 16:24:27 -0400399 * success and zero on failure. */
Steven Valdezce902a92016-05-17 11:47:53 -0400400static int tls1_curve_params_from_ec_key(uint16_t *out_group_id,
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 uint8_t *out_comp_id, EC_KEY *ec) {
402 int nid;
403 uint16_t id;
404 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 if (ec == NULL) {
407 return 0;
408 }
Adam Langley95c29f32014-06-20 12:00:00 -0700409
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 grp = EC_KEY_get0_group(ec);
411 if (grp == NULL) {
412 return 0;
413 }
David Benjamin072334d2014-07-13 16:24:27 -0400414
Steven Valdezce902a92016-05-17 11:47:53 -0400415 /* Determine group ID */
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 nid = EC_GROUP_get_curve_name(grp);
Steven Valdezce902a92016-05-17 11:47:53 -0400417 if (!ssl_nid_to_group_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 return 0;
419 }
David Benjamin072334d2014-07-13 16:24:27 -0400420
Steven Valdezce902a92016-05-17 11:47:53 -0400421 /* Set the named group ID. Arbitrary explicit groups are not supported. */
422 *out_group_id = id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800423
424 if (out_comp_id) {
425 if (EC_KEY_get0_public_key(ec) == NULL) {
426 return 0;
427 }
428 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
429 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
430 } else {
431 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
432 }
433 }
434
435 return 1;
436}
David Benjamin072334d2014-07-13 16:24:27 -0400437
Steven Valdezce902a92016-05-17 11:47:53 -0400438/* tls1_check_group_id returns one if |group_id| is consistent with both our
439 * and the peer's group preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400440 * preferences are checked; the peer (the server) does not send preferences. */
Steven Valdezce902a92016-05-17 11:47:53 -0400441int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
442 const uint16_t *groups;
443 size_t groups_len, i, get_peer_groups;
David Benjamin42e9a772014-09-02 23:18:44 -0400444
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 /* Check against our list, then the peer's list. */
Steven Valdezce902a92016-05-17 11:47:53 -0400446 for (get_peer_groups = 0; get_peer_groups <= 1; get_peer_groups++) {
447 if (get_peer_groups && !ssl->server) {
David Benjamin55a43642015-04-20 14:45:55 -0400448 /* Servers do not present a preference list so, if we are a client, only
449 * check our list. */
450 continue;
451 }
452
Steven Valdezce902a92016-05-17 11:47:53 -0400453 tls1_get_grouplist(ssl, get_peer_groups, &groups, &groups_len);
454 if (get_peer_groups && groups_len == 0) {
455 /* Clients are not required to send a supported_groups extension. In this
456 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin55a43642015-04-20 14:45:55 -0400457 * section 4, paragraph 3. */
458 continue;
459 }
Steven Valdezce902a92016-05-17 11:47:53 -0400460 for (i = 0; i < groups_len; i++) {
461 if (groups[i] == group_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 break;
463 }
464 }
Adam Langley95c29f32014-06-20 12:00:00 -0700465
Steven Valdezce902a92016-05-17 11:47:53 -0400466 if (i == groups_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 return 0;
468 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 }
David Benjamin033e5f42014-11-13 18:47:41 -0500470
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 return 1;
472}
David Benjamin033e5f42014-11-13 18:47:41 -0500473
David Benjamin0d56f882015-12-19 17:05:56 -0500474int tls1_check_ec_cert(SSL *ssl, X509 *x) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 int ret = 0;
476 EVP_PKEY *pkey = X509_get_pubkey(x);
Steven Valdezce902a92016-05-17 11:47:53 -0400477 uint16_t group_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 uint8_t comp_id;
479
David Benjamin758d1272015-11-20 17:47:25 -0500480 if (!pkey) {
481 goto done;
482 }
483 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
484 if (ec_key == NULL ||
Steven Valdezce902a92016-05-17 11:47:53 -0400485 !tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) ||
486 !tls1_check_group_id(ssl, group_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400487 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 goto done;
489 }
490
491 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500492
493done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400494 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 return ret;
496}
David Benjamin42e9a772014-09-02 23:18:44 -0400497
Adam Langley95c29f32014-06-20 12:00:00 -0700498/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800499 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Steven Valdez02563852016-06-23 13:33:05 -0400501static const uint16_t kDefaultSignatureAlgorithms[] = {
502 SSL_SIGN_RSA_PKCS1_SHA512,
503 SSL_SIGN_ECDSA_SECP521R1_SHA512,
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Steven Valdez02563852016-06-23 13:33:05 -0400505 SSL_SIGN_RSA_PKCS1_SHA384,
506 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Adam Langley95c29f32014-06-20 12:00:00 -0700507
Steven Valdez02563852016-06-23 13:33:05 -0400508 SSL_SIGN_RSA_PKCS1_SHA256,
509 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Adam Langley95c29f32014-06-20 12:00:00 -0700510
Steven Valdez02563852016-06-23 13:33:05 -0400511 SSL_SIGN_RSA_PKCS1_SHA1,
512 SSL_SIGN_ECDSA_SHA1,
Adam Langley95c29f32014-06-20 12:00:00 -0700513};
David Benjamin05da6e12014-07-12 20:42:55 -0400514
Steven Valdez02563852016-06-23 13:33:05 -0400515size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
516 *psigs = kDefaultSignatureAlgorithms;
517 return sizeof(kDefaultSignatureAlgorithms) /
518 sizeof(kDefaultSignatureAlgorithms[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800519}
Adam Langley95c29f32014-06-20 12:00:00 -0700520
Steven Valdezf0451ca2016-06-29 13:16:27 -0400521static int tls12_get_pkey_type(uint16_t sigalg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800522
Steven Valdezf0451ca2016-06-29 13:16:27 -0400523int tls12_check_peer_sigalg(SSL *ssl, int *out_alert,
524 uint16_t sigalg, EVP_PKEY *pkey) {
Steven Valdez02563852016-06-23 13:33:05 -0400525 const uint16_t *sent_sigs;
526 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800527
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 /* Check key type is consistent with signature */
Steven Valdezf0451ca2016-06-29 13:16:27 -0400529 if (pkey->type != tls12_get_pkey_type(sigalg)) {
David Benjamin3570d732015-06-29 00:28:17 -0400530 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800531 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
532 return 0;
533 }
534
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500536 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400537 for (i = 0; i < sent_sigslen; i++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400538 if (sigalg == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 break;
540 }
541 }
542
David Benjamin788be4a2015-10-30 17:50:57 -0400543 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400544 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
546 return 0;
547 }
548
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 return 1;
550}
551
552/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
553 * supported or doesn't appear in supported signature algorithms. Unlike
554 * ssl_cipher_get_disabled this applies to a specific session and not global
555 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500556void ssl_set_client_disabled(SSL *ssl) {
557 CERT *c = ssl->cert;
Steven Valdez02563852016-06-23 13:33:05 -0400558 const uint16_t *sigalgs;
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 size_t i, sigalgslen;
560 int have_rsa = 0, have_ecdsa = 0;
561 c->mask_a = 0;
562 c->mask_k = 0;
563
Adam Langleyfcf25832014-12-18 17:42:32 -0800564 /* Now go through all signature algorithms seeing if we support any for RSA,
565 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500566 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -0400567 for (i = 0; i < sigalgslen; i++) {
568 switch (sigalgs[i]) {
569 case SSL_SIGN_RSA_PKCS1_SHA512:
570 case SSL_SIGN_RSA_PKCS1_SHA384:
571 case SSL_SIGN_RSA_PKCS1_SHA256:
572 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800573 have_rsa = 1;
574 break;
575
Steven Valdez02563852016-06-23 13:33:05 -0400576 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
577 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
578 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
579 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800580 have_ecdsa = 1;
581 break;
582 }
583 }
584
585 /* Disable auth if we don't include any appropriate signature algorithms. */
586 if (!have_rsa) {
587 c->mask_a |= SSL_aRSA;
588 }
589 if (!have_ecdsa) {
590 c->mask_a |= SSL_aECDSA;
591 }
592
593 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500594 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800595 c->mask_a |= SSL_aPSK;
596 c->mask_k |= SSL_kPSK;
597 }
598}
Adam Langley95c29f32014-06-20 12:00:00 -0700599
Adam Langley614c66a2015-06-12 15:26:58 -0700600/* tls_extension represents a TLS extension that is handled internally. The
601 * |init| function is called for each handshake, before any other functions of
602 * the extension. Then the add and parse callbacks are called as needed.
603 *
604 * The parse callbacks receive a |CBS| that contains the contents of the
605 * extension (i.e. not including the type and length bytes). If an extension is
606 * not received then the parse callbacks will be called with a NULL CBS so that
607 * they can do any processing needed to handle the absence of an extension.
608 *
609 * The add callbacks receive a |CBB| to which the extension can be appended but
610 * the function is responsible for appending the type and length bytes too.
611 *
612 * All callbacks return one for success and zero for error. If a parse function
613 * returns zero then a fatal alert with value |*out_alert| will be sent. If
614 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
615struct tls_extension {
616 uint16_t value;
617 void (*init)(SSL *ssl);
618
619 int (*add_clienthello)(SSL *ssl, CBB *out);
620 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
621
622 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
623 int (*add_serverhello)(SSL *ssl, CBB *out);
624};
625
Steven Valdez6b8509a2016-07-12 13:38:32 -0400626static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
627 if (contents != NULL) {
628 /* Servers MUST NOT send this extension. */
629 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
630 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
631 return 0;
632 }
633
634 return 1;
635}
636
637static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
638 /* This extension from the client is handled elsewhere. */
639 return 1;
640}
641
642static int dont_add_serverhello(SSL *ssl, CBB *out) {
643 return 1;
644}
Adam Langley614c66a2015-06-12 15:26:58 -0700645
646/* Server name indication (SNI).
647 *
648 * https://tools.ietf.org/html/rfc6066#section-3. */
649
650static void ext_sni_init(SSL *ssl) {
651 ssl->s3->tmp.should_ack_sni = 0;
652}
653
654static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
655 if (ssl->tlsext_hostname == NULL) {
656 return 1;
657 }
658
659 CBB contents, server_name_list, name;
660 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
661 !CBB_add_u16_length_prefixed(out, &contents) ||
662 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
663 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
664 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
665 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
666 strlen(ssl->tlsext_hostname)) ||
667 !CBB_flush(out)) {
668 return 0;
669 }
670
671 return 1;
672}
673
David Benjamin0d56f882015-12-19 17:05:56 -0500674static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
675 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700676 if (contents == NULL) {
677 return 1;
678 }
679
680 if (CBS_len(contents) != 0) {
681 return 0;
682 }
683
684 assert(ssl->tlsext_hostname != NULL);
685
686 if (!ssl->hit) {
687 assert(ssl->session->tlsext_hostname == NULL);
688 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
689 if (!ssl->session->tlsext_hostname) {
690 *out_alert = SSL_AD_INTERNAL_ERROR;
691 return 0;
692 }
693 }
694
695 return 1;
696}
697
David Benjamin0d56f882015-12-19 17:05:56 -0500698static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
699 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700700 if (contents == NULL) {
701 return 1;
702 }
703
David Benjamin9b611e22016-03-03 08:48:30 -0500704 CBS server_name_list, host_name;
705 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700706 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500707 !CBS_get_u8(&server_name_list, &name_type) ||
708 /* Although the server_name extension was intended to be extensible to
709 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
710 * different name types will cause an error. Further, RFC 4366 originally
711 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
712 * adding new name types is no longer feasible.
713 *
714 * Act as if the extensibility does not exist to simplify parsing. */
715 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
716 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700717 CBS_len(contents) != 0) {
718 return 0;
719 }
720
David Benjamin9b611e22016-03-03 08:48:30 -0500721 if (name_type != TLSEXT_NAMETYPE_host_name ||
722 CBS_len(&host_name) == 0 ||
723 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
724 CBS_contains_zero_byte(&host_name)) {
725 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
726 return 0;
727 }
Adam Langley614c66a2015-06-12 15:26:58 -0700728
David Benjamin9b611e22016-03-03 08:48:30 -0500729 /* TODO(davidben): SNI should be resolved before resumption. We have the
730 * early callback as a replacement, but we should fix the current callback
731 * and avoid the need for |SSL_CTX_set_session_id_context|. */
732 if (!ssl->hit) {
733 assert(ssl->session->tlsext_hostname == NULL);
734
735 /* Copy the hostname as a string. */
736 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
737 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700738 return 0;
739 }
740
David Benjamin9b611e22016-03-03 08:48:30 -0500741 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700742 }
743
744 return 1;
745}
746
747static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
748 if (ssl->hit ||
749 !ssl->s3->tmp.should_ack_sni ||
750 ssl->session->tlsext_hostname == NULL) {
751 return 1;
752 }
753
754 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
755 !CBB_add_u16(out, 0 /* length */)) {
756 return 0;
757 }
758
759 return 1;
760}
761
762
Adam Langley5021b222015-06-12 18:27:58 -0700763/* Renegotiation indication.
764 *
765 * https://tools.ietf.org/html/rfc5746 */
766
767static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
768 CBB contents, prev_finished;
769 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
770 !CBB_add_u16_length_prefixed(out, &contents) ||
771 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
772 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
773 ssl->s3->previous_client_finished_len) ||
774 !CBB_flush(out)) {
775 return 0;
776 }
777
778 return 1;
779}
780
781static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
782 CBS *contents) {
David Benjamin3e052de2015-11-25 20:10:31 -0500783 /* Servers may not switch between omitting the extension and supporting it.
784 * See RFC 5746, sections 3.5 and 4.2. */
785 if (ssl->s3->initial_handshake_complete &&
786 (contents != NULL) != ssl->s3->send_connection_binding) {
787 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
788 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
789 return 0;
790 }
791
Adam Langley5021b222015-06-12 18:27:58 -0700792 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500793 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700794 * RI even on initial ServerHello because the client doesn't see any
795 * renegotiation during an attack. However this would mean we could not
796 * connect to any server which doesn't support RI.
797 *
David Benjamine9cddb82015-11-23 14:36:40 -0500798 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
799 * practical terms every client sets it so it's just assumed here. */
800 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700801 }
802
803 const size_t expected_len = ssl->s3->previous_client_finished_len +
804 ssl->s3->previous_server_finished_len;
805
806 /* Check for logic errors */
807 assert(!expected_len || ssl->s3->previous_client_finished_len);
808 assert(!expected_len || ssl->s3->previous_server_finished_len);
809
810 /* Parse out the extension contents. */
811 CBS renegotiated_connection;
812 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
813 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400814 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700815 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
816 return 0;
817 }
818
819 /* Check that the extension matches. */
820 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400821 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700822 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
823 return 0;
824 }
825
826 const uint8_t *d = CBS_data(&renegotiated_connection);
827 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
828 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400829 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700830 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
831 return 0;
832 }
833 d += ssl->s3->previous_client_finished_len;
834
835 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
836 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400837 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700838 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
839 return 0;
840 }
841 ssl->s3->send_connection_binding = 1;
842
843 return 1;
844}
845
846static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
847 CBS *contents) {
848 /* Renegotiation isn't supported as a server so this function should never be
849 * called after the initial handshake. */
850 assert(!ssl->s3->initial_handshake_complete);
851
852 CBS fake_contents;
853 static const uint8_t kFakeExtension[] = {0};
854
855 if (contents == NULL) {
856 if (ssl->s3->send_connection_binding) {
857 /* The renegotiation SCSV was received so pretend that we received a
858 * renegotiation extension. */
859 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
860 contents = &fake_contents;
861 /* We require that the renegotiation extension is at index zero of
862 * kExtensions. */
863 ssl->s3->tmp.extensions.received |= (1u << 0);
864 } else {
865 return 1;
866 }
867 }
868
869 CBS renegotiated_connection;
870
871 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
872 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400873 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700874 return 0;
875 }
876
877 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500878 if (!CBS_mem_equal(&renegotiated_connection,
879 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700880 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400881 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700882 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
883 return 0;
884 }
885
886 ssl->s3->send_connection_binding = 1;
887
888 return 1;
889}
890
891static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
892 CBB contents, prev_finished;
893 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
894 !CBB_add_u16_length_prefixed(out, &contents) ||
895 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
896 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
897 ssl->s3->previous_client_finished_len) ||
898 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
899 ssl->s3->previous_server_finished_len) ||
900 !CBB_flush(out)) {
901 return 0;
902 }
903
904 return 1;
905}
906
Adam Langley0a056712015-07-01 15:03:33 -0700907
908/* Extended Master Secret.
909 *
David Benjamin43946d42016-02-01 08:42:19 -0500910 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700911
912static void ext_ems_init(SSL *ssl) {
913 ssl->s3->tmp.extended_master_secret = 0;
914}
915
916static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
917 if (ssl->version == SSL3_VERSION) {
918 return 1;
919 }
920
921 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
922 !CBB_add_u16(out, 0 /* length */)) {
923 return 0;
924 }
925
926 return 1;
927}
928
929static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
930 CBS *contents) {
931 if (contents == NULL) {
932 return 1;
933 }
934
935 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
936 return 0;
937 }
938
939 ssl->s3->tmp.extended_master_secret = 1;
940 return 1;
941}
942
David Benjamin0d56f882015-12-19 17:05:56 -0500943static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
944 CBS *contents) {
Adam Langley0a056712015-07-01 15:03:33 -0700945 if (ssl->version == SSL3_VERSION || contents == NULL) {
946 return 1;
947 }
948
949 if (CBS_len(contents) != 0) {
950 return 0;
951 }
952
953 ssl->s3->tmp.extended_master_secret = 1;
954 return 1;
955}
956
957static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
958 if (!ssl->s3->tmp.extended_master_secret) {
959 return 1;
960 }
961
962 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
963 !CBB_add_u16(out, 0 /* length */)) {
964 return 0;
965 }
966
967 return 1;
968}
969
Adam Langley9b05bc52015-07-01 15:25:33 -0700970
971/* Session tickets.
972 *
973 * https://tools.ietf.org/html/rfc5077 */
974
975static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
976 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
977 return 1;
978 }
979
980 const uint8_t *ticket_data = NULL;
981 int ticket_len = 0;
982
983 /* Renegotiation does not participate in session resumption. However, still
984 * advertise the extension to avoid potentially breaking servers which carry
985 * over the state from the previous handshake, such as OpenSSL servers
986 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
987 if (!ssl->s3->initial_handshake_complete &&
988 ssl->session != NULL &&
989 ssl->session->tlsext_tick != NULL) {
990 ticket_data = ssl->session->tlsext_tick;
991 ticket_len = ssl->session->tlsext_ticklen;
992 }
993
994 CBB ticket;
995 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
996 !CBB_add_u16_length_prefixed(out, &ticket) ||
997 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
998 !CBB_flush(out)) {
999 return 0;
1000 }
1001
1002 return 1;
1003}
1004
1005static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1006 CBS *contents) {
1007 ssl->tlsext_ticket_expected = 0;
1008
1009 if (contents == NULL) {
1010 return 1;
1011 }
1012
1013 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1014 * this function should never be called, even if the server tries to send the
1015 * extension. */
1016 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1017
1018 if (CBS_len(contents) != 0) {
1019 return 0;
1020 }
1021
1022 ssl->tlsext_ticket_expected = 1;
1023 return 1;
1024}
1025
Adam Langley9b05bc52015-07-01 15:25:33 -07001026static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1027 if (!ssl->tlsext_ticket_expected) {
1028 return 1;
1029 }
1030
1031 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1032 * true. */
1033 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1034
1035 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1036 !CBB_add_u16(out, 0 /* length */)) {
1037 return 0;
1038 }
1039
1040 return 1;
1041}
1042
1043
Adam Langley2e857bd2015-07-01 16:09:19 -07001044/* Signature Algorithms.
1045 *
1046 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1047
1048static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjaminb6a0a512016-06-21 10:33:21 -04001049 if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001050 return 1;
1051 }
1052
Steven Valdez02563852016-06-23 13:33:05 -04001053 const uint16_t *sigalgs_data;
Adam Langley2e857bd2015-07-01 16:09:19 -07001054 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1055
1056 CBB contents, sigalgs;
1057 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1058 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez02563852016-06-23 13:33:05 -04001059 !CBB_add_u16_length_prefixed(&contents, &sigalgs)) {
1060 return 0;
1061 }
1062
1063 size_t i;
1064 for (i = 0; i < sigalgs_len; i++) {
1065 if (!CBB_add_u16(&sigalgs, sigalgs_data[i])) {
1066 return 0;
1067 }
1068 }
1069
1070 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001071 return 0;
1072 }
1073
1074 return 1;
1075}
1076
Adam Langley2e857bd2015-07-01 16:09:19 -07001077static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1078 CBS *contents) {
1079 OPENSSL_free(ssl->cert->peer_sigalgs);
1080 ssl->cert->peer_sigalgs = NULL;
1081 ssl->cert->peer_sigalgslen = 0;
1082
Adam Langley2e857bd2015-07-01 16:09:19 -07001083 if (contents == NULL) {
1084 return 1;
1085 }
1086
1087 CBS supported_signature_algorithms;
1088 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001089 CBS_len(contents) != 0 ||
1090 CBS_len(&supported_signature_algorithms) == 0 ||
1091 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001092 return 0;
1093 }
1094
1095 return 1;
1096}
1097
Adam Langley2e857bd2015-07-01 16:09:19 -07001098
Adam Langleybb0bd042015-07-01 16:21:03 -07001099/* OCSP Stapling.
1100 *
1101 * https://tools.ietf.org/html/rfc6066#section-8 */
1102
1103static void ext_ocsp_init(SSL *ssl) {
1104 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001105 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001106}
1107
1108static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1109 if (!ssl->ocsp_stapling_enabled) {
1110 return 1;
1111 }
1112
1113 CBB contents;
1114 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1115 !CBB_add_u16_length_prefixed(out, &contents) ||
1116 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1117 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1118 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1119 !CBB_flush(out)) {
1120 return 0;
1121 }
1122
Adam Langleyce9d85e2016-01-24 15:58:39 -08001123 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001124 return 1;
1125}
1126
1127static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001128 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001129 if (contents == NULL) {
1130 return 1;
1131 }
1132
1133 if (CBS_len(contents) != 0) {
1134 return 0;
1135 }
1136
1137 ssl->s3->tmp.certificate_status_expected = 1;
1138 return 1;
1139}
1140
1141static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1142 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001143 if (contents == NULL) {
1144 return 1;
1145 }
1146
1147 uint8_t status_type;
1148 if (!CBS_get_u8(contents, &status_type)) {
1149 return 0;
1150 }
1151
1152 /* We cannot decide whether OCSP stapling will occur yet because the correct
1153 * SSL_CTX might not have been selected. */
1154 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1155
Adam Langleybb0bd042015-07-01 16:21:03 -07001156 return 1;
1157}
1158
1159static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001160 /* The extension shouldn't be sent when resuming sessions. */
1161 if (ssl->hit ||
1162 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001163 ssl->ctx->ocsp_response_length == 0) {
1164 return 1;
1165 }
1166
1167 ssl->s3->tmp.certificate_status_expected = 1;
1168
1169 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1170 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001171}
1172
1173
Adam Langley97dfcbf2015-07-01 18:35:20 -07001174/* Next protocol negotiation.
1175 *
1176 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1177
1178static void ext_npn_init(SSL *ssl) {
1179 ssl->s3->next_proto_neg_seen = 0;
1180}
1181
1182static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1183 if (ssl->s3->initial_handshake_complete ||
1184 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001185 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001186 SSL_IS_DTLS(ssl)) {
1187 return 1;
1188 }
1189
1190 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1191 !CBB_add_u16(out, 0 /* length */)) {
1192 return 0;
1193 }
1194
1195 return 1;
1196}
1197
1198static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1199 CBS *contents) {
1200 if (contents == NULL) {
1201 return 1;
1202 }
1203
1204 /* If any of these are false then we should never have sent the NPN
1205 * extension in the ClientHello and thus this function should never have been
1206 * called. */
1207 assert(!ssl->s3->initial_handshake_complete);
1208 assert(!SSL_IS_DTLS(ssl));
1209 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001210 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001211
David Benjamin76c2efc2015-08-31 14:24:29 -04001212 if (ssl->s3->alpn_selected != NULL) {
1213 /* NPN and ALPN may not be negotiated in the same connection. */
1214 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1215 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1216 return 0;
1217 }
1218
Adam Langley97dfcbf2015-07-01 18:35:20 -07001219 const uint8_t *const orig_contents = CBS_data(contents);
1220 const size_t orig_len = CBS_len(contents);
1221
1222 while (CBS_len(contents) != 0) {
1223 CBS proto;
1224 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1225 CBS_len(&proto) == 0) {
1226 return 0;
1227 }
1228 }
1229
1230 uint8_t *selected;
1231 uint8_t selected_len;
1232 if (ssl->ctx->next_proto_select_cb(
1233 ssl, &selected, &selected_len, orig_contents, orig_len,
1234 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1235 *out_alert = SSL_AD_INTERNAL_ERROR;
1236 return 0;
1237 }
1238
David Benjamin79978df2015-12-25 15:56:49 -05001239 OPENSSL_free(ssl->s3->next_proto_negotiated);
1240 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1241 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001242 *out_alert = SSL_AD_INTERNAL_ERROR;
1243 return 0;
1244 }
1245
David Benjamin79978df2015-12-25 15:56:49 -05001246 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001247 ssl->s3->next_proto_neg_seen = 1;
1248
1249 return 1;
1250}
1251
1252static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1253 CBS *contents) {
1254 if (contents != NULL && CBS_len(contents) != 0) {
1255 return 0;
1256 }
1257
1258 if (contents == NULL ||
1259 ssl->s3->initial_handshake_complete ||
1260 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1261 * afterwards, parsing the ALPN extension will clear
1262 * |next_proto_neg_seen|. */
1263 ssl->s3->alpn_selected != NULL ||
1264 ssl->ctx->next_protos_advertised_cb == NULL ||
1265 SSL_IS_DTLS(ssl)) {
1266 return 1;
1267 }
1268
1269 ssl->s3->next_proto_neg_seen = 1;
1270 return 1;
1271}
1272
1273static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1274 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1275 * parsed. */
1276 if (!ssl->s3->next_proto_neg_seen) {
1277 return 1;
1278 }
1279
1280 const uint8_t *npa;
1281 unsigned npa_len;
1282
1283 if (ssl->ctx->next_protos_advertised_cb(
1284 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1285 SSL_TLSEXT_ERR_OK) {
1286 ssl->s3->next_proto_neg_seen = 0;
1287 return 1;
1288 }
1289
1290 CBB contents;
1291 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1292 !CBB_add_u16_length_prefixed(out, &contents) ||
1293 !CBB_add_bytes(&contents, npa, npa_len) ||
1294 !CBB_flush(out)) {
1295 return 0;
1296 }
1297
1298 return 1;
1299}
1300
1301
Adam Langleyab8d87d2015-07-10 12:21:39 -07001302/* Signed certificate timestamps.
1303 *
1304 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1305
1306static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1307 if (!ssl->signed_cert_timestamps_enabled) {
1308 return 1;
1309 }
1310
1311 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1312 !CBB_add_u16(out, 0 /* length */)) {
1313 return 0;
1314 }
1315
1316 return 1;
1317}
1318
1319static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1320 CBS *contents) {
1321 if (contents == NULL) {
1322 return 1;
1323 }
1324
1325 /* If this is false then we should never have sent the SCT extension in the
1326 * ClientHello and thus this function should never have been called. */
1327 assert(ssl->signed_cert_timestamps_enabled);
1328
1329 if (CBS_len(contents) == 0) {
1330 *out_alert = SSL_AD_DECODE_ERROR;
1331 return 0;
1332 }
1333
1334 /* Session resumption uses the original session information. */
1335 if (!ssl->hit &&
1336 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1337 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1338 *out_alert = SSL_AD_INTERNAL_ERROR;
1339 return 0;
1340 }
1341
1342 return 1;
1343}
1344
1345static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1346 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001347 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001348}
1349
1350static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001351 /* The extension shouldn't be sent when resuming sessions. */
1352 if (ssl->hit ||
1353 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001354 return 1;
1355 }
1356
1357 CBB contents;
1358 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1359 CBB_add_u16_length_prefixed(out, &contents) &&
1360 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1361 ssl->ctx->signed_cert_timestamp_list_length) &&
1362 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001363}
1364
1365
Adam Langleyf18e4532015-07-10 13:39:53 -07001366/* Application-level Protocol Negotiation.
1367 *
1368 * https://tools.ietf.org/html/rfc7301 */
1369
1370static void ext_alpn_init(SSL *ssl) {
1371 OPENSSL_free(ssl->s3->alpn_selected);
1372 ssl->s3->alpn_selected = NULL;
1373}
1374
1375static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1376 if (ssl->alpn_client_proto_list == NULL ||
1377 ssl->s3->initial_handshake_complete) {
1378 return 1;
1379 }
1380
1381 CBB contents, proto_list;
1382 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1383 !CBB_add_u16_length_prefixed(out, &contents) ||
1384 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1385 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1386 ssl->alpn_client_proto_list_len) ||
1387 !CBB_flush(out)) {
1388 return 0;
1389 }
1390
1391 return 1;
1392}
1393
1394static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1395 CBS *contents) {
1396 if (contents == NULL) {
1397 return 1;
1398 }
1399
1400 assert(!ssl->s3->initial_handshake_complete);
1401 assert(ssl->alpn_client_proto_list != NULL);
1402
David Benjamin76c2efc2015-08-31 14:24:29 -04001403 if (ssl->s3->next_proto_neg_seen) {
1404 /* NPN and ALPN may not be negotiated in the same connection. */
1405 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1406 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1407 return 0;
1408 }
1409
Adam Langleyf18e4532015-07-10 13:39:53 -07001410 /* The extension data consists of a ProtocolNameList which must have
1411 * exactly one ProtocolName. Each of these is length-prefixed. */
1412 CBS protocol_name_list, protocol_name;
1413 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1414 CBS_len(contents) != 0 ||
1415 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1416 /* Empty protocol names are forbidden. */
1417 CBS_len(&protocol_name) == 0 ||
1418 CBS_len(&protocol_name_list) != 0) {
1419 return 0;
1420 }
1421
1422 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1423 &ssl->s3->alpn_selected_len)) {
1424 *out_alert = SSL_AD_INTERNAL_ERROR;
1425 return 0;
1426 }
1427
1428 return 1;
1429}
1430
1431static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1432 CBS *contents) {
1433 if (contents == NULL) {
1434 return 1;
1435 }
1436
1437 if (ssl->ctx->alpn_select_cb == NULL ||
1438 ssl->s3->initial_handshake_complete) {
1439 return 1;
1440 }
1441
1442 /* ALPN takes precedence over NPN. */
1443 ssl->s3->next_proto_neg_seen = 0;
1444
1445 CBS protocol_name_list;
1446 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1447 CBS_len(contents) != 0 ||
1448 CBS_len(&protocol_name_list) < 2) {
1449 return 0;
1450 }
1451
1452 /* Validate the protocol list. */
1453 CBS protocol_name_list_copy = protocol_name_list;
1454 while (CBS_len(&protocol_name_list_copy) > 0) {
1455 CBS protocol_name;
1456
1457 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1458 /* Empty protocol names are forbidden. */
1459 CBS_len(&protocol_name) == 0) {
1460 return 0;
1461 }
1462 }
1463
1464 const uint8_t *selected;
1465 uint8_t selected_len;
1466 if (ssl->ctx->alpn_select_cb(
1467 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1468 CBS_len(&protocol_name_list),
1469 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1470 OPENSSL_free(ssl->s3->alpn_selected);
1471 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1472 if (ssl->s3->alpn_selected == NULL) {
1473 *out_alert = SSL_AD_INTERNAL_ERROR;
1474 return 0;
1475 }
1476 ssl->s3->alpn_selected_len = selected_len;
1477 }
1478
1479 return 1;
1480}
1481
1482static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1483 if (ssl->s3->alpn_selected == NULL) {
1484 return 1;
1485 }
1486
1487 CBB contents, proto_list, proto;
1488 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1489 !CBB_add_u16_length_prefixed(out, &contents) ||
1490 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1491 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001492 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1493 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001494 !CBB_flush(out)) {
1495 return 0;
1496 }
1497
1498 return 1;
1499}
1500
1501
Adam Langley49c7af12015-07-10 14:33:46 -07001502/* Channel ID.
1503 *
1504 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1505
1506static void ext_channel_id_init(SSL *ssl) {
1507 ssl->s3->tlsext_channel_id_valid = 0;
1508}
1509
1510static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1511 if (!ssl->tlsext_channel_id_enabled ||
1512 SSL_IS_DTLS(ssl)) {
1513 return 1;
1514 }
1515
1516 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1517 !CBB_add_u16(out, 0 /* length */)) {
1518 return 0;
1519 }
1520
1521 return 1;
1522}
1523
1524static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1525 CBS *contents) {
1526 if (contents == NULL) {
1527 return 1;
1528 }
1529
1530 assert(!SSL_IS_DTLS(ssl));
1531 assert(ssl->tlsext_channel_id_enabled);
1532
1533 if (CBS_len(contents) != 0) {
1534 return 0;
1535 }
1536
1537 ssl->s3->tlsext_channel_id_valid = 1;
1538 return 1;
1539}
1540
1541static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1542 CBS *contents) {
1543 if (contents == NULL ||
1544 !ssl->tlsext_channel_id_enabled ||
1545 SSL_IS_DTLS(ssl)) {
1546 return 1;
1547 }
1548
1549 if (CBS_len(contents) != 0) {
1550 return 0;
1551 }
1552
1553 ssl->s3->tlsext_channel_id_valid = 1;
1554 return 1;
1555}
1556
1557static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1558 if (!ssl->s3->tlsext_channel_id_valid) {
1559 return 1;
1560 }
1561
1562 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1563 !CBB_add_u16(out, 0 /* length */)) {
1564 return 0;
1565 }
1566
1567 return 1;
1568}
1569
Adam Langley391250d2015-07-15 19:06:07 -07001570
1571/* Secure Real-time Transport Protocol (SRTP) extension.
1572 *
1573 * https://tools.ietf.org/html/rfc5764 */
1574
Adam Langley391250d2015-07-15 19:06:07 -07001575
1576static void ext_srtp_init(SSL *ssl) {
1577 ssl->srtp_profile = NULL;
1578}
1579
1580static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1581 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1582 if (profiles == NULL) {
1583 return 1;
1584 }
1585 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1586 if (num_profiles == 0) {
1587 return 1;
1588 }
1589
1590 CBB contents, profile_ids;
1591 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1592 !CBB_add_u16_length_prefixed(out, &contents) ||
1593 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1594 return 0;
1595 }
1596
1597 size_t i;
1598 for (i = 0; i < num_profiles; i++) {
1599 if (!CBB_add_u16(&profile_ids,
1600 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1601 return 0;
1602 }
1603 }
1604
1605 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1606 !CBB_flush(out)) {
1607 return 0;
1608 }
1609
1610 return 1;
1611}
1612
1613static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1614 CBS *contents) {
1615 if (contents == NULL) {
1616 return 1;
1617 }
1618
1619 /* The extension consists of a u16-prefixed profile ID list containing a
1620 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1621 *
1622 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1623 CBS profile_ids, srtp_mki;
1624 uint16_t profile_id;
1625 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1626 !CBS_get_u16(&profile_ids, &profile_id) ||
1627 CBS_len(&profile_ids) != 0 ||
1628 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1629 CBS_len(contents) != 0) {
1630 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1631 return 0;
1632 }
1633
1634 if (CBS_len(&srtp_mki) != 0) {
1635 /* Must be no MKI, since we never offer one. */
1636 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1637 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1638 return 0;
1639 }
1640
1641 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1642
1643 /* Check to see if the server gave us something we support (and presumably
1644 * offered). */
1645 size_t i;
1646 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1647 const SRTP_PROTECTION_PROFILE *profile =
1648 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1649
1650 if (profile->id == profile_id) {
1651 ssl->srtp_profile = profile;
1652 return 1;
1653 }
1654 }
1655
1656 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1657 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1658 return 0;
1659}
1660
1661static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1662 CBS *contents) {
1663 if (contents == NULL) {
1664 return 1;
1665 }
1666
1667 CBS profile_ids, srtp_mki;
1668 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1669 CBS_len(&profile_ids) < 2 ||
1670 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1671 CBS_len(contents) != 0) {
1672 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1673 return 0;
1674 }
1675 /* Discard the MKI value for now. */
1676
1677 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1678 SSL_get_srtp_profiles(ssl);
1679
1680 /* Pick the server's most preferred profile. */
1681 size_t i;
1682 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1683 const SRTP_PROTECTION_PROFILE *server_profile =
1684 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1685
1686 CBS profile_ids_tmp;
1687 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1688
1689 while (CBS_len(&profile_ids_tmp) > 0) {
1690 uint16_t profile_id;
1691 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1692 return 0;
1693 }
1694
1695 if (server_profile->id == profile_id) {
1696 ssl->srtp_profile = server_profile;
1697 return 1;
1698 }
1699 }
1700 }
1701
1702 return 1;
1703}
1704
1705static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1706 if (ssl->srtp_profile == NULL) {
1707 return 1;
1708 }
1709
1710 CBB contents, profile_ids;
1711 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1712 !CBB_add_u16_length_prefixed(out, &contents) ||
1713 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1714 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1715 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1716 !CBB_flush(out)) {
1717 return 0;
1718 }
1719
1720 return 1;
1721}
1722
Adam Langleybdd5d662015-07-20 16:19:08 -07001723
1724/* EC point formats.
1725 *
1726 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1727
1728static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1729 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1730 return 0;
1731 }
1732
1733 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1734
1735 size_t i;
1736 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1737 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1738
1739 const uint32_t alg_k = cipher->algorithm_mkey;
1740 const uint32_t alg_a = cipher->algorithm_auth;
1741 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1742 return 1;
1743 }
1744 }
1745
1746 return 0;
1747}
1748
Adam Langleybdd5d662015-07-20 16:19:08 -07001749static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001750 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001751 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1752 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001753 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1754 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001755 !CBB_flush(out)) {
1756 return 0;
1757 }
1758
1759 return 1;
1760}
1761
1762static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1763 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1764 return 1;
1765 }
1766
1767 return ext_ec_point_add_extension(ssl, out);
1768}
1769
1770static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1771 CBS *contents) {
1772 if (contents == NULL) {
1773 return 1;
1774 }
1775
1776 CBS ec_point_format_list;
1777 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1778 CBS_len(contents) != 0) {
1779 return 0;
1780 }
1781
David Benjaminfc059942015-07-30 23:01:59 -04001782 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1783 * point format. */
1784 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1785 CBS_len(&ec_point_format_list)) == NULL) {
1786 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001787 return 0;
1788 }
1789
1790 return 1;
1791}
1792
1793static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1794 CBS *contents) {
1795 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1796}
1797
1798static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1799 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1800 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001801 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001802
1803 if (!using_ecc) {
1804 return 1;
1805 }
1806
1807 return ext_ec_point_add_extension(ssl, out);
1808}
1809
Adam Langley273d49c2015-07-20 16:38:52 -07001810
Steven Valdezce902a92016-05-17 11:47:53 -04001811/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07001812 *
Steven Valdezce902a92016-05-17 11:47:53 -04001813 * https://tools.ietf.org/html/rfc4492#section-5.1.2
1814 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07001815
Steven Valdezce902a92016-05-17 11:47:53 -04001816static void ext_supported_groups_init(SSL *ssl) {
1817 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
1818 ssl->s3->tmp.peer_supported_group_list = NULL;
1819 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07001820}
1821
Steven Valdezce902a92016-05-17 11:47:53 -04001822static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07001823 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1824 return 1;
1825 }
1826
Steven Valdezce902a92016-05-17 11:47:53 -04001827 CBB contents, groups_bytes;
1828 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07001829 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001830 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07001831 return 0;
1832 }
1833
Steven Valdezce902a92016-05-17 11:47:53 -04001834 const uint16_t *groups;
1835 size_t groups_len;
1836 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07001837
1838 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04001839 for (i = 0; i < groups_len; i++) {
1840 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07001841 return 0;
1842 }
1843 }
1844
1845 return CBB_flush(out);
1846}
1847
Steven Valdezce902a92016-05-17 11:47:53 -04001848static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1849 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07001850 /* This extension is not expected to be echoed by servers and is ignored. */
1851 return 1;
1852}
1853
Steven Valdezce902a92016-05-17 11:47:53 -04001854static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1855 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07001856 if (contents == NULL) {
1857 return 1;
1858 }
1859
Steven Valdezce902a92016-05-17 11:47:53 -04001860 CBS supported_group_list;
1861 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
1862 CBS_len(&supported_group_list) == 0 ||
1863 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07001864 CBS_len(contents) != 0) {
1865 return 0;
1866 }
1867
Steven Valdezce902a92016-05-17 11:47:53 -04001868 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
1869 CBS_len(&supported_group_list));
1870 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07001871 *out_alert = SSL_AD_INTERNAL_ERROR;
1872 return 0;
1873 }
1874
Steven Valdezce902a92016-05-17 11:47:53 -04001875 const size_t num_groups = CBS_len(&supported_group_list) / 2;
Adam Langley273d49c2015-07-20 16:38:52 -07001876 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04001877 for (i = 0; i < num_groups; i++) {
1878 if (!CBS_get_u16(&supported_group_list,
1879 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07001880 goto err;
1881 }
1882 }
1883
Steven Valdezce902a92016-05-17 11:47:53 -04001884 assert(CBS_len(&supported_group_list) == 0);
1885 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07001886
1887 return 1;
1888
1889err:
Steven Valdezce902a92016-05-17 11:47:53 -04001890 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
1891 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07001892 *out_alert = SSL_AD_INTERNAL_ERROR;
1893 return 0;
1894}
1895
Steven Valdezce902a92016-05-17 11:47:53 -04001896static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07001897 /* Servers don't echo this extension. */
1898 return 1;
1899}
1900
1901
Adam Langley614c66a2015-06-12 15:26:58 -07001902/* kExtensions contains all the supported extensions. */
1903static const struct tls_extension kExtensions[] = {
1904 {
Adam Langley5021b222015-06-12 18:27:58 -07001905 /* The renegotiation extension must always be at index zero because the
1906 * |received| and |sent| bitsets need to be tweaked when the "extension" is
1907 * sent as an SCSV. */
1908 TLSEXT_TYPE_renegotiate,
1909 NULL,
1910 ext_ri_add_clienthello,
1911 ext_ri_parse_serverhello,
1912 ext_ri_parse_clienthello,
1913 ext_ri_add_serverhello,
1914 },
1915 {
Adam Langley614c66a2015-06-12 15:26:58 -07001916 TLSEXT_TYPE_server_name,
1917 ext_sni_init,
1918 ext_sni_add_clienthello,
1919 ext_sni_parse_serverhello,
1920 ext_sni_parse_clienthello,
1921 ext_sni_add_serverhello,
1922 },
Adam Langley0a056712015-07-01 15:03:33 -07001923 {
1924 TLSEXT_TYPE_extended_master_secret,
1925 ext_ems_init,
1926 ext_ems_add_clienthello,
1927 ext_ems_parse_serverhello,
1928 ext_ems_parse_clienthello,
1929 ext_ems_add_serverhello,
1930 },
Adam Langley9b05bc52015-07-01 15:25:33 -07001931 {
1932 TLSEXT_TYPE_session_ticket,
1933 NULL,
1934 ext_ticket_add_clienthello,
1935 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04001936 /* Ticket extension client parsing is handled in ssl_session.c */
1937 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07001938 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,
Steven Valdez6b8509a2016-07-12 13:38:32 -04001944 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07001945 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04001946 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07001947 },
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
David Benjaminec2f27d2014-11-13 19:17:25 -05002519/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
Steven Valdezf0451ca2016-06-29 13:16:27 -04002520 * algorithm |sigalg|. It returns -1 if the type is unknown. */
2521static int tls12_get_pkey_type(uint16_t sigalg) {
David Benjamind246b812016-07-08 15:07:02 -07002522 switch (sigalg) {
2523 case SSL_SIGN_RSA_PKCS1_SHA1:
2524 case SSL_SIGN_RSA_PKCS1_SHA256:
2525 case SSL_SIGN_RSA_PKCS1_SHA384:
2526 case SSL_SIGN_RSA_PKCS1_SHA512:
Adam Langleyfcf25832014-12-18 17:42:32 -08002527 return EVP_PKEY_RSA;
2528
David Benjamind246b812016-07-08 15:07:02 -07002529 case SSL_SIGN_ECDSA_SHA1:
2530 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
2531 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
2532 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
Adam Langleyfcf25832014-12-18 17:42:32 -08002533 return EVP_PKEY_EC;
2534
2535 default:
2536 return -1;
2537 }
2538}
Adam Langley95c29f32014-06-20 12:00:00 -07002539
Steven Valdez0d62f262015-09-04 12:41:04 -04002540int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002541 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05002542 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002543 return 1;
2544 }
David Benjamincd996942014-07-20 16:23:51 -04002545
Steven Valdez0d62f262015-09-04 12:41:04 -04002546 CERT *const cert = ssl->cert;
2547 OPENSSL_free(cert->peer_sigalgs);
2548 cert->peer_sigalgs = NULL;
2549 cert->peer_sigalgslen = 0;
2550
2551 size_t num_sigalgs = CBS_len(in_sigalgs);
2552
2553 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002554 return 0;
2555 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002556 num_sigalgs /= 2;
2557
2558 /* supported_signature_algorithms in the certificate request is
2559 * allowed to be empty. */
2560 if (num_sigalgs == 0) {
2561 return 1;
2562 }
2563
Steven Valdez02563852016-06-23 13:33:05 -04002564 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
2565 * and we just divided |num_sigalgs| by two. */
2566 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
Steven Valdez0d62f262015-09-04 12:41:04 -04002567 if (cert->peer_sigalgs == NULL) {
2568 return 0;
2569 }
2570 cert->peer_sigalgslen = num_sigalgs;
2571
2572 CBS sigalgs;
2573 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2574
2575 size_t i;
2576 for (i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04002577 if (!CBS_get_u16(&sigalgs, &cert->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04002578 return 0;
2579 }
2580 }
Adam Langley95c29f32014-06-20 12:00:00 -07002581
Adam Langleyfcf25832014-12-18 17:42:32 -08002582 return 1;
2583}
David Benjaminec2f27d2014-11-13 19:17:25 -05002584
David Benjaminea9a0d52016-07-08 15:52:59 -07002585int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04002586 CERT *cert = ssl->cert;
2587 int type = ssl_private_key_type(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -04002588 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002589
Steven Valdezf0451ca2016-06-29 13:16:27 -04002590 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
2591 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
2592 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
2593 if (type == EVP_PKEY_RSA) {
David Benjaminea9a0d52016-07-08 15:52:59 -07002594 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
2595 } else {
2596 *out = SSL_SIGN_ECDSA_SHA1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002597 }
David Benjaminea9a0d52016-07-08 15:52:59 -07002598 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002599 }
2600
David Benjamind246b812016-07-08 15:07:02 -07002601 const uint16_t *sigalgs = kDefaultSignatureAlgorithms;
2602 size_t sigalgs_len = sizeof(kDefaultSignatureAlgorithms) /
2603 sizeof(kDefaultSignatureAlgorithms[0]);
2604 if (cert->sigalgs != NULL) {
2605 sigalgs = cert->sigalgs;
2606 sigalgs_len = cert->sigalgs_len;
Steven Valdez0d62f262015-09-04 12:41:04 -04002607 }
2608
David Benjaminea9a0d52016-07-08 15:52:59 -07002609 const uint16_t *peer_sigalgs = cert->peer_sigalgs;
2610 size_t peer_sigalgs_len = cert->peer_sigalgslen;
2611 if (peer_sigalgs_len == 0) {
2612 /* If the client didn't specify any signature_algorithms extension then
2613 * we can assume that it supports SHA1. See
2614 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
2615 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
2616 SSL_SIGN_ECDSA_SHA1};
2617 peer_sigalgs = kDefaultPeerAlgorithms;
2618 peer_sigalgs_len =
2619 sizeof(kDefaultPeerAlgorithms) / sizeof(kDefaultPeerAlgorithms);
2620 }
2621
David Benjamind246b812016-07-08 15:07:02 -07002622 for (i = 0; i < sigalgs_len; i++) {
David Benjaminea9a0d52016-07-08 15:52:59 -07002623 for (j = 0; j < peer_sigalgs_len; j++) {
2624 uint16_t signature_algorithm = peer_sigalgs[j];
Steven Valdezf0451ca2016-06-29 13:16:27 -04002625 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
2626 * negotiated. */
David Benjamind246b812016-07-08 15:07:02 -07002627 if (signature_algorithm != SSL_SIGN_RSA_PKCS1_MD5_SHA1 &&
2628 signature_algorithm == sigalgs[i] &&
Steven Valdezf0451ca2016-06-29 13:16:27 -04002629 tls12_get_pkey_type(signature_algorithm) == type) {
David Benjaminea9a0d52016-07-08 15:52:59 -07002630 *out = signature_algorithm;
2631 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002632 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002633 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002634 }
Adam Langley95c29f32014-06-20 12:00:00 -07002635
David Benjaminea9a0d52016-07-08 15:52:59 -07002636 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
2637 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002638}
Adam Langley95c29f32014-06-20 12:00:00 -07002639
David Benjamind6a4ae92015-08-06 11:10:51 -04002640int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2641 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002642 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002643
2644 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002645 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2646 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002647 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002648
David Benjamind6a4ae92015-08-06 11:10:51 -04002649 static const char kClientIDMagic[] = "TLS Channel ID signature";
2650 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2651
2652 if (ssl->hit) {
2653 static const char kResumptionMagic[] = "Resumption";
2654 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2655 if (ssl->session->original_handshake_hash_len == 0) {
2656 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2657 goto err;
2658 }
2659 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2660 ssl->session->original_handshake_hash_len);
2661 }
2662
2663 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2664 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2665 sizeof(handshake_hash));
2666 if (handshake_hash_len < 0) {
2667 goto err;
2668 }
2669 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2670 unsigned len_u;
2671 EVP_DigestFinal_ex(&ctx, out, &len_u);
2672 *out_len = len_u;
2673
2674 ret = 1;
2675
2676err:
2677 EVP_MD_CTX_cleanup(&ctx);
2678 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002679}
Adam Langley1258b6a2014-06-20 12:00:00 -07002680
2681/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin0d56f882015-12-19 17:05:56 -05002682 * hashes in |ssl->session| so that Channel ID resumptions can sign that
2683 * data. */
2684int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002685 int digest_len;
2686 /* This function should never be called for a resumed session because the
2687 * handshake hashes that we wish to record are for the original, full
2688 * handshake. */
David Benjamin0d56f882015-12-19 17:05:56 -05002689 if (ssl->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002690 return -1;
2691 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002692
Adam Langleyfcf25832014-12-18 17:42:32 -08002693 digest_len =
David Benjamin0d56f882015-12-19 17:05:56 -05002694 tls1_handshake_digest(ssl, ssl->session->original_handshake_hash,
2695 sizeof(ssl->session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08002696 if (digest_len < 0) {
2697 return -1;
2698 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002699
David Benjamin0d56f882015-12-19 17:05:56 -05002700 ssl->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002701
Adam Langleyfcf25832014-12-18 17:42:32 -08002702 return 1;
2703}