blob: 4f808656d33ed2ead9df6fc43cb1ff8834162e64 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin03973092014-06-24 23:27:17 -0400116#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400117#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/evp.h>
120#include <openssl/hmac.h>
121#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400122#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/rand.h>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700124#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800127
128
David Benjamin0d56f882015-12-19 17:05:56 -0500129static int ssl_check_clienthello_tlsext(SSL *ssl);
130static int ssl_check_serverhello_tlsext(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700131
Adam Langleyfcf25832014-12-18 17:42:32 -0800132static int compare_uint16_t(const void *p1, const void *p2) {
133 uint16_t u1 = *((const uint16_t *)p1);
134 uint16_t u2 = *((const uint16_t *)p2);
135 if (u1 < u2) {
136 return -1;
137 } else if (u1 > u2) {
138 return 1;
139 } else {
140 return 0;
141 }
142}
David Benjamin35a7a442014-07-05 00:23:20 -0400143
Adam Langleyfcf25832014-12-18 17:42:32 -0800144/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
145 * more than one extension of the same type in a ClientHello or ServerHello.
146 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400147 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800148static int tls1_check_duplicate_extensions(const CBS *cbs) {
149 CBS extensions = *cbs;
150 size_t num_extensions = 0, i = 0;
151 uint16_t *extension_types = NULL;
152 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400153
Adam Langleyfcf25832014-12-18 17:42:32 -0800154 /* First pass: count the extensions. */
155 while (CBS_len(&extensions) > 0) {
156 uint16_t type;
157 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400158
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 if (!CBS_get_u16(&extensions, &type) ||
160 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
161 goto done;
162 }
David Benjamin35a7a442014-07-05 00:23:20 -0400163
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 num_extensions++;
165 }
David Benjamin35a7a442014-07-05 00:23:20 -0400166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 if (num_extensions == 0) {
168 return 1;
169 }
David Benjamin9a373592014-07-25 04:27:53 -0400170
Brian Smith5ba06892016-02-07 09:36:04 -1000171 extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800172 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400173 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800174 goto done;
175 }
David Benjamin35a7a442014-07-05 00:23:20 -0400176
Adam Langleyfcf25832014-12-18 17:42:32 -0800177 /* Second pass: gather the extension types. */
178 extensions = *cbs;
179 for (i = 0; i < num_extensions; i++) {
180 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400181
Adam Langleyfcf25832014-12-18 17:42:32 -0800182 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
183 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
184 /* This should not happen. */
185 goto done;
186 }
187 }
188 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400189
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 /* Sort the extensions and make sure there are no duplicates. */
191 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
192 for (i = 1; i < num_extensions; i++) {
193 if (extension_types[i - 1] == extension_types[i]) {
194 goto done;
195 }
196 }
David Benjamin35a7a442014-07-05 00:23:20 -0400197
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 ret = 1;
199
David Benjamin35a7a442014-07-05 00:23:20 -0400200done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400201 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 return ret;
203}
David Benjamin35a7a442014-07-05 00:23:20 -0400204
Adam Langleyfcf25832014-12-18 17:42:32 -0800205char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
206 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400207
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700209
Adam Langleyfcf25832014-12-18 17:42:32 -0800210 if (/* Skip client version. */
211 !CBS_skip(&client_hello, 2) ||
212 /* Skip client nonce. */
213 !CBS_skip(&client_hello, 32) ||
214 /* Extract session_id. */
215 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
216 return 0;
217 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700218
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 ctx->session_id = CBS_data(&session_id);
220 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700221
Adam Langleyfcf25832014-12-18 17:42:32 -0800222 /* Skip past DTLS cookie */
223 if (SSL_IS_DTLS(ctx->ssl)) {
224 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
227 return 0;
228 }
229 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700230
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 /* Extract cipher_suites. */
232 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
233 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
234 return 0;
235 }
236 ctx->cipher_suites = CBS_data(&cipher_suites);
237 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700238
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 /* Extract compression_methods. */
240 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
241 CBS_len(&compression_methods) < 1) {
242 return 0;
243 }
244 ctx->compression_methods = CBS_data(&compression_methods);
245 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700246
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 /* If the ClientHello ends here then it's valid, but doesn't have any
248 * extensions. (E.g. SSLv3.) */
249 if (CBS_len(&client_hello) == 0) {
250 ctx->extensions = NULL;
251 ctx->extensions_len = 0;
252 return 1;
253 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700254
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 /* Extract extensions and check it is valid. */
256 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
257 !tls1_check_duplicate_extensions(&extensions) ||
258 CBS_len(&client_hello) != 0) {
259 return 0;
260 }
261 ctx->extensions = CBS_data(&extensions);
262 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 return 1;
265}
Adam Langleydc9b1412014-06-20 12:00:00 -0700266
David Benjamind4c2bce2015-10-17 12:28:18 -0400267int SSL_early_callback_ctx_extension_get(
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
269 const uint8_t **out_data, size_t *out_len) {
270 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700271
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700273
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 while (CBS_len(&extensions) != 0) {
275 uint16_t type;
276 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400277
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 /* Decode the next extension. */
279 if (!CBS_get_u16(&extensions, &type) ||
280 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
281 return 0;
282 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 if (type == extension_type) {
285 *out_data = CBS_data(&extension);
286 *out_len = CBS_len(&extension);
287 return 1;
288 }
289 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700290
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 return 0;
292}
Adam Langley95c29f32014-06-20 12:00:00 -0700293
Steven Valdezce902a92016-05-17 11:47:53 -0400294static const uint16_t kDefaultGroups[] = {
295 SSL_GROUP_X25519,
296 SSL_GROUP_SECP256R1,
297 SSL_GROUP_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700298#if defined(BORINGSSL_ANDROID_SYSTEM)
Steven Valdezce902a92016-05-17 11:47:53 -0400299 SSL_GROUP_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700300#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800301};
Adam Langley95c29f32014-06-20 12:00:00 -0700302
Steven Valdezce902a92016-05-17 11:47:53 -0400303/* tls1_get_grouplist sets |*out_group_ids| and |*out_group_ids_len| to the
304 * list of allowed group IDs. If |get_peer_groups| is non-zero, return the
305 * peer's group list. Otherwise, return the preferred list. */
306static void tls1_get_grouplist(SSL *ssl, int get_peer_groups,
307 const uint16_t **out_group_ids,
308 size_t *out_group_ids_len) {
309 if (get_peer_groups) {
310 /* Only clients send a supported group list, so this function is only
311 * called on the server. */
David Benjamin0d56f882015-12-19 17:05:56 -0500312 assert(ssl->server);
Steven Valdezce902a92016-05-17 11:47:53 -0400313 *out_group_ids = ssl->s3->tmp.peer_supported_group_list;
314 *out_group_ids_len = ssl->s3->tmp.peer_supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800315 return;
316 }
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Steven Valdezce902a92016-05-17 11:47:53 -0400318 *out_group_ids = ssl->supported_group_list;
319 *out_group_ids_len = ssl->supported_group_list_len;
320 if (!*out_group_ids) {
321 *out_group_ids = kDefaultGroups;
322 *out_group_ids_len = sizeof(kDefaultGroups) / sizeof(kDefaultGroups[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 }
324}
David Benjamined439582014-07-14 19:13:02 -0400325
Steven Valdezce902a92016-05-17 11:47:53 -0400326int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) {
327 const uint16_t *groups, *peer_groups, *pref, *supp;
328 size_t groups_len, peer_groups_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 /* Can't do anything on client side */
David Benjamin4298d772015-12-19 00:18:25 -0500331 if (ssl->server == 0) {
332 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 }
334
Steven Valdezce902a92016-05-17 11:47:53 -0400335 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
336 tls1_get_grouplist(ssl, 1 /* peer groups */, &peer_groups, &peer_groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800337
Steven Valdezce902a92016-05-17 11:47:53 -0400338 if (peer_groups_len == 0) {
339 /* Clients are not required to send a supported_groups extension. In this
340 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin4298d772015-12-19 00:18:25 -0500341 * section 4, paragraph 3.
342 *
343 * However, in the interests of compatibility, we will skip ECDH if the
344 * client didn't send an extension because we can't be sure that they'll
Steven Valdezce902a92016-05-17 11:47:53 -0400345 * support our favoured group. */
David Benjamin4298d772015-12-19 00:18:25 -0500346 return 0;
David Benjamin55a43642015-04-20 14:45:55 -0400347 }
348
David Benjamin4298d772015-12-19 00:18:25 -0500349 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400350 pref = groups;
351 pref_len = groups_len;
352 supp = peer_groups;
353 supp_len = peer_groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400354 } else {
Steven Valdezce902a92016-05-17 11:47:53 -0400355 pref = peer_groups;
356 pref_len = peer_groups_len;
357 supp = groups;
358 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400359 }
360
361 for (i = 0; i < pref_len; i++) {
362 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400364 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500365 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 }
367 }
368 }
369
David Benjamin4298d772015-12-19 00:18:25 -0500370 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800371}
Adam Langley95c29f32014-06-20 12:00:00 -0700372
Steven Valdezce902a92016-05-17 11:47:53 -0400373int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400375 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 size_t i;
377
Steven Valdezce902a92016-05-17 11:47:53 -0400378 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
379 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 return 0;
381 }
382
383 for (i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400384 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
385 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 return 0;
387 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 }
389
Steven Valdezce902a92016-05-17 11:47:53 -0400390 OPENSSL_free(*out_group_ids);
391 *out_group_ids = group_ids;
392 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800393
394 return 1;
395}
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Steven Valdezce902a92016-05-17 11:47:53 -0400397/* tls1_curve_params_from_ec_key sets |*out_group_id| and |*out_comp_id| to the
398 * TLS group ID and point format, respectively, for |ec|. It returns one on
David Benjamin072334d2014-07-13 16:24:27 -0400399 * success and zero on failure. */
Steven Valdezce902a92016-05-17 11:47:53 -0400400static int tls1_curve_params_from_ec_key(uint16_t *out_group_id,
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 uint8_t *out_comp_id, EC_KEY *ec) {
402 int nid;
403 uint16_t id;
404 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 if (ec == NULL) {
407 return 0;
408 }
Adam Langley95c29f32014-06-20 12:00:00 -0700409
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 grp = EC_KEY_get0_group(ec);
411 if (grp == NULL) {
412 return 0;
413 }
David Benjamin072334d2014-07-13 16:24:27 -0400414
Steven Valdezce902a92016-05-17 11:47:53 -0400415 /* Determine group ID */
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 nid = EC_GROUP_get_curve_name(grp);
Steven Valdezce902a92016-05-17 11:47:53 -0400417 if (!ssl_nid_to_group_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 return 0;
419 }
David Benjamin072334d2014-07-13 16:24:27 -0400420
Steven Valdezce902a92016-05-17 11:47:53 -0400421 /* Set the named group ID. Arbitrary explicit groups are not supported. */
422 *out_group_id = id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800423
424 if (out_comp_id) {
425 if (EC_KEY_get0_public_key(ec) == NULL) {
426 return 0;
427 }
428 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
429 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
430 } else {
431 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
432 }
433 }
434
435 return 1;
436}
David Benjamin072334d2014-07-13 16:24:27 -0400437
Steven Valdezce902a92016-05-17 11:47:53 -0400438/* tls1_check_group_id returns one if |group_id| is consistent with both our
439 * and the peer's group preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400440 * preferences are checked; the peer (the server) does not send preferences. */
Steven Valdezce902a92016-05-17 11:47:53 -0400441int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
442 const uint16_t *groups;
443 size_t groups_len, i, get_peer_groups;
David Benjamin42e9a772014-09-02 23:18:44 -0400444
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 /* Check against our list, then the peer's list. */
Steven Valdezce902a92016-05-17 11:47:53 -0400446 for (get_peer_groups = 0; get_peer_groups <= 1; get_peer_groups++) {
447 if (get_peer_groups && !ssl->server) {
David Benjamin55a43642015-04-20 14:45:55 -0400448 /* Servers do not present a preference list so, if we are a client, only
449 * check our list. */
450 continue;
451 }
452
Steven Valdezce902a92016-05-17 11:47:53 -0400453 tls1_get_grouplist(ssl, get_peer_groups, &groups, &groups_len);
454 if (get_peer_groups && groups_len == 0) {
455 /* Clients are not required to send a supported_groups extension. In this
456 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin55a43642015-04-20 14:45:55 -0400457 * section 4, paragraph 3. */
458 continue;
459 }
Steven Valdezce902a92016-05-17 11:47:53 -0400460 for (i = 0; i < groups_len; i++) {
461 if (groups[i] == group_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 break;
463 }
464 }
Adam Langley95c29f32014-06-20 12:00:00 -0700465
Steven Valdezce902a92016-05-17 11:47:53 -0400466 if (i == groups_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 return 0;
468 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 }
David Benjamin033e5f42014-11-13 18:47:41 -0500470
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 return 1;
472}
David Benjamin033e5f42014-11-13 18:47:41 -0500473
David Benjamin0d56f882015-12-19 17:05:56 -0500474int tls1_check_ec_cert(SSL *ssl, X509 *x) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 int ret = 0;
476 EVP_PKEY *pkey = X509_get_pubkey(x);
Steven Valdezce902a92016-05-17 11:47:53 -0400477 uint16_t group_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 uint8_t comp_id;
479
David Benjamin758d1272015-11-20 17:47:25 -0500480 if (!pkey) {
481 goto done;
482 }
483 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
484 if (ec_key == NULL ||
Steven Valdezce902a92016-05-17 11:47:53 -0400485 !tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) ||
486 !tls1_check_group_id(ssl, group_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400487 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 goto done;
489 }
490
491 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500492
493done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400494 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 return ret;
496}
David Benjamin42e9a772014-09-02 23:18:44 -0400497
Adam Langley95c29f32014-06-20 12:00:00 -0700498/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800499 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Steven Valdez02563852016-06-23 13:33:05 -0400501static const uint16_t kDefaultSignatureAlgorithms[] = {
502 SSL_SIGN_RSA_PKCS1_SHA512,
503 SSL_SIGN_ECDSA_SECP521R1_SHA512,
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Steven Valdez02563852016-06-23 13:33:05 -0400505 SSL_SIGN_RSA_PKCS1_SHA384,
506 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Adam Langley95c29f32014-06-20 12:00:00 -0700507
Steven Valdez02563852016-06-23 13:33:05 -0400508 SSL_SIGN_RSA_PKCS1_SHA256,
509 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Adam Langley95c29f32014-06-20 12:00:00 -0700510
Steven Valdez02563852016-06-23 13:33:05 -0400511 SSL_SIGN_RSA_PKCS1_SHA1,
512 SSL_SIGN_ECDSA_SHA1,
Adam Langley95c29f32014-06-20 12:00:00 -0700513};
David Benjamin05da6e12014-07-12 20:42:55 -0400514
Steven Valdez02563852016-06-23 13:33:05 -0400515size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
516 *psigs = kDefaultSignatureAlgorithms;
517 return sizeof(kDefaultSignatureAlgorithms) /
518 sizeof(kDefaultSignatureAlgorithms[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800519}
Adam Langley95c29f32014-06-20 12:00:00 -0700520
Steven Valdez02563852016-06-23 13:33:05 -0400521static int tls12_get_pkey_type(uint16_t sig_alg);
522static const EVP_MD *tls12_get_hash(uint16_t sig_alg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800523
Steven Valdez02563852016-06-23 13:33:05 -0400524int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert,
525 uint16_t signature_algorithm, EVP_PKEY *pkey) {
526 const uint16_t *sent_sigs;
527 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800528
Adam Langleyfcf25832014-12-18 17:42:32 -0800529 /* Check key type is consistent with signature */
Steven Valdez02563852016-06-23 13:33:05 -0400530 if (pkey->type != tls12_get_pkey_type(signature_algorithm)) {
David Benjamin3570d732015-06-29 00:28:17 -0400531 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800532 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
533 return 0;
534 }
535
Adam Langleyfcf25832014-12-18 17:42:32 -0800536 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500537 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400538 for (i = 0; i < sent_sigslen; i++) {
539 if (signature_algorithm == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 break;
541 }
542 }
543
David Benjamin788be4a2015-10-30 17:50:57 -0400544 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400545 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800546 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
547 return 0;
548 }
549
Steven Valdez02563852016-06-23 13:33:05 -0400550 *out_md = tls12_get_hash(signature_algorithm);
Adam Langleyfcf25832014-12-18 17:42:32 -0800551 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400552 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
554 return 0;
555 }
556
557 return 1;
558}
559
560/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
561 * supported or doesn't appear in supported signature algorithms. Unlike
562 * ssl_cipher_get_disabled this applies to a specific session and not global
563 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500564void ssl_set_client_disabled(SSL *ssl) {
565 CERT *c = ssl->cert;
Steven Valdez02563852016-06-23 13:33:05 -0400566 const uint16_t *sigalgs;
Adam Langleyfcf25832014-12-18 17:42:32 -0800567 size_t i, sigalgslen;
568 int have_rsa = 0, have_ecdsa = 0;
569 c->mask_a = 0;
570 c->mask_k = 0;
571
Adam Langleyfcf25832014-12-18 17:42:32 -0800572 /* Now go through all signature algorithms seeing if we support any for RSA,
573 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500574 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -0400575 for (i = 0; i < sigalgslen; i++) {
576 switch (sigalgs[i]) {
577 case SSL_SIGN_RSA_PKCS1_SHA512:
578 case SSL_SIGN_RSA_PKCS1_SHA384:
579 case SSL_SIGN_RSA_PKCS1_SHA256:
580 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800581 have_rsa = 1;
582 break;
583
Steven Valdez02563852016-06-23 13:33:05 -0400584 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
585 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
586 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
587 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800588 have_ecdsa = 1;
589 break;
590 }
591 }
592
593 /* Disable auth if we don't include any appropriate signature algorithms. */
594 if (!have_rsa) {
595 c->mask_a |= SSL_aRSA;
596 }
597 if (!have_ecdsa) {
598 c->mask_a |= SSL_aECDSA;
599 }
600
601 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500602 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800603 c->mask_a |= SSL_aPSK;
604 c->mask_k |= SSL_kPSK;
605 }
606}
Adam Langley95c29f32014-06-20 12:00:00 -0700607
Adam Langley614c66a2015-06-12 15:26:58 -0700608/* tls_extension represents a TLS extension that is handled internally. The
609 * |init| function is called for each handshake, before any other functions of
610 * the extension. Then the add and parse callbacks are called as needed.
611 *
612 * The parse callbacks receive a |CBS| that contains the contents of the
613 * extension (i.e. not including the type and length bytes). If an extension is
614 * not received then the parse callbacks will be called with a NULL CBS so that
615 * they can do any processing needed to handle the absence of an extension.
616 *
617 * The add callbacks receive a |CBB| to which the extension can be appended but
618 * the function is responsible for appending the type and length bytes too.
619 *
620 * All callbacks return one for success and zero for error. If a parse function
621 * returns zero then a fatal alert with value |*out_alert| will be sent. If
622 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
623struct tls_extension {
624 uint16_t value;
625 void (*init)(SSL *ssl);
626
627 int (*add_clienthello)(SSL *ssl, CBB *out);
628 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
629
630 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
631 int (*add_serverhello)(SSL *ssl, CBB *out);
632};
633
634
635/* Server name indication (SNI).
636 *
637 * https://tools.ietf.org/html/rfc6066#section-3. */
638
639static void ext_sni_init(SSL *ssl) {
640 ssl->s3->tmp.should_ack_sni = 0;
641}
642
643static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
644 if (ssl->tlsext_hostname == NULL) {
645 return 1;
646 }
647
648 CBB contents, server_name_list, name;
649 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
650 !CBB_add_u16_length_prefixed(out, &contents) ||
651 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
652 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
653 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
654 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
655 strlen(ssl->tlsext_hostname)) ||
656 !CBB_flush(out)) {
657 return 0;
658 }
659
660 return 1;
661}
662
David Benjamin0d56f882015-12-19 17:05:56 -0500663static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
664 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700665 if (contents == NULL) {
666 return 1;
667 }
668
669 if (CBS_len(contents) != 0) {
670 return 0;
671 }
672
673 assert(ssl->tlsext_hostname != NULL);
674
675 if (!ssl->hit) {
676 assert(ssl->session->tlsext_hostname == NULL);
677 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
678 if (!ssl->session->tlsext_hostname) {
679 *out_alert = SSL_AD_INTERNAL_ERROR;
680 return 0;
681 }
682 }
683
684 return 1;
685}
686
David Benjamin0d56f882015-12-19 17:05:56 -0500687static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
688 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700689 if (contents == NULL) {
690 return 1;
691 }
692
David Benjamin9b611e22016-03-03 08:48:30 -0500693 CBS server_name_list, host_name;
694 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700695 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500696 !CBS_get_u8(&server_name_list, &name_type) ||
697 /* Although the server_name extension was intended to be extensible to
698 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
699 * different name types will cause an error. Further, RFC 4366 originally
700 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
701 * adding new name types is no longer feasible.
702 *
703 * Act as if the extensibility does not exist to simplify parsing. */
704 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
705 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700706 CBS_len(contents) != 0) {
707 return 0;
708 }
709
David Benjamin9b611e22016-03-03 08:48:30 -0500710 if (name_type != TLSEXT_NAMETYPE_host_name ||
711 CBS_len(&host_name) == 0 ||
712 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
713 CBS_contains_zero_byte(&host_name)) {
714 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
715 return 0;
716 }
Adam Langley614c66a2015-06-12 15:26:58 -0700717
David Benjamin9b611e22016-03-03 08:48:30 -0500718 /* TODO(davidben): SNI should be resolved before resumption. We have the
719 * early callback as a replacement, but we should fix the current callback
720 * and avoid the need for |SSL_CTX_set_session_id_context|. */
721 if (!ssl->hit) {
722 assert(ssl->session->tlsext_hostname == NULL);
723
724 /* Copy the hostname as a string. */
725 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
726 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700727 return 0;
728 }
729
David Benjamin9b611e22016-03-03 08:48:30 -0500730 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700731 }
732
733 return 1;
734}
735
736static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
737 if (ssl->hit ||
738 !ssl->s3->tmp.should_ack_sni ||
739 ssl->session->tlsext_hostname == NULL) {
740 return 1;
741 }
742
743 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
744 !CBB_add_u16(out, 0 /* length */)) {
745 return 0;
746 }
747
748 return 1;
749}
750
751
Adam Langley5021b222015-06-12 18:27:58 -0700752/* Renegotiation indication.
753 *
754 * https://tools.ietf.org/html/rfc5746 */
755
756static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
757 CBB contents, prev_finished;
758 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
759 !CBB_add_u16_length_prefixed(out, &contents) ||
760 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
761 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
762 ssl->s3->previous_client_finished_len) ||
763 !CBB_flush(out)) {
764 return 0;
765 }
766
767 return 1;
768}
769
770static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
771 CBS *contents) {
David Benjamin3e052de2015-11-25 20:10:31 -0500772 /* Servers may not switch between omitting the extension and supporting it.
773 * See RFC 5746, sections 3.5 and 4.2. */
774 if (ssl->s3->initial_handshake_complete &&
775 (contents != NULL) != ssl->s3->send_connection_binding) {
776 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
777 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
778 return 0;
779 }
780
Adam Langley5021b222015-06-12 18:27:58 -0700781 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500782 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700783 * RI even on initial ServerHello because the client doesn't see any
784 * renegotiation during an attack. However this would mean we could not
785 * connect to any server which doesn't support RI.
786 *
David Benjamine9cddb82015-11-23 14:36:40 -0500787 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
788 * practical terms every client sets it so it's just assumed here. */
789 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700790 }
791
792 const size_t expected_len = ssl->s3->previous_client_finished_len +
793 ssl->s3->previous_server_finished_len;
794
795 /* Check for logic errors */
796 assert(!expected_len || ssl->s3->previous_client_finished_len);
797 assert(!expected_len || ssl->s3->previous_server_finished_len);
798
799 /* Parse out the extension contents. */
800 CBS renegotiated_connection;
801 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
802 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400803 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700804 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
805 return 0;
806 }
807
808 /* Check that the extension matches. */
809 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400810 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700811 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
812 return 0;
813 }
814
815 const uint8_t *d = CBS_data(&renegotiated_connection);
816 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
817 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400818 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700819 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
820 return 0;
821 }
822 d += ssl->s3->previous_client_finished_len;
823
824 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
825 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400826 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700827 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
828 return 0;
829 }
830 ssl->s3->send_connection_binding = 1;
831
832 return 1;
833}
834
835static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
836 CBS *contents) {
837 /* Renegotiation isn't supported as a server so this function should never be
838 * called after the initial handshake. */
839 assert(!ssl->s3->initial_handshake_complete);
840
841 CBS fake_contents;
842 static const uint8_t kFakeExtension[] = {0};
843
844 if (contents == NULL) {
845 if (ssl->s3->send_connection_binding) {
846 /* The renegotiation SCSV was received so pretend that we received a
847 * renegotiation extension. */
848 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
849 contents = &fake_contents;
850 /* We require that the renegotiation extension is at index zero of
851 * kExtensions. */
852 ssl->s3->tmp.extensions.received |= (1u << 0);
853 } else {
854 return 1;
855 }
856 }
857
858 CBS renegotiated_connection;
859
860 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
861 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400862 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700863 return 0;
864 }
865
866 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500867 if (!CBS_mem_equal(&renegotiated_connection,
868 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700869 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400870 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700871 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
872 return 0;
873 }
874
875 ssl->s3->send_connection_binding = 1;
876
877 return 1;
878}
879
880static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
881 CBB contents, prev_finished;
882 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
883 !CBB_add_u16_length_prefixed(out, &contents) ||
884 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
885 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
886 ssl->s3->previous_client_finished_len) ||
887 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
888 ssl->s3->previous_server_finished_len) ||
889 !CBB_flush(out)) {
890 return 0;
891 }
892
893 return 1;
894}
895
Adam Langley0a056712015-07-01 15:03:33 -0700896
897/* Extended Master Secret.
898 *
David Benjamin43946d42016-02-01 08:42:19 -0500899 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700900
901static void ext_ems_init(SSL *ssl) {
902 ssl->s3->tmp.extended_master_secret = 0;
903}
904
905static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
906 if (ssl->version == SSL3_VERSION) {
907 return 1;
908 }
909
910 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
911 !CBB_add_u16(out, 0 /* length */)) {
912 return 0;
913 }
914
915 return 1;
916}
917
918static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
919 CBS *contents) {
920 if (contents == NULL) {
921 return 1;
922 }
923
924 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
925 return 0;
926 }
927
928 ssl->s3->tmp.extended_master_secret = 1;
929 return 1;
930}
931
David Benjamin0d56f882015-12-19 17:05:56 -0500932static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
933 CBS *contents) {
Adam Langley0a056712015-07-01 15:03:33 -0700934 if (ssl->version == SSL3_VERSION || contents == NULL) {
935 return 1;
936 }
937
938 if (CBS_len(contents) != 0) {
939 return 0;
940 }
941
942 ssl->s3->tmp.extended_master_secret = 1;
943 return 1;
944}
945
946static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
947 if (!ssl->s3->tmp.extended_master_secret) {
948 return 1;
949 }
950
951 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
952 !CBB_add_u16(out, 0 /* length */)) {
953 return 0;
954 }
955
956 return 1;
957}
958
Adam Langley9b05bc52015-07-01 15:25:33 -0700959
960/* Session tickets.
961 *
962 * https://tools.ietf.org/html/rfc5077 */
963
964static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
965 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
966 return 1;
967 }
968
969 const uint8_t *ticket_data = NULL;
970 int ticket_len = 0;
971
972 /* Renegotiation does not participate in session resumption. However, still
973 * advertise the extension to avoid potentially breaking servers which carry
974 * over the state from the previous handshake, such as OpenSSL servers
975 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
976 if (!ssl->s3->initial_handshake_complete &&
977 ssl->session != NULL &&
978 ssl->session->tlsext_tick != NULL) {
979 ticket_data = ssl->session->tlsext_tick;
980 ticket_len = ssl->session->tlsext_ticklen;
981 }
982
983 CBB ticket;
984 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
985 !CBB_add_u16_length_prefixed(out, &ticket) ||
986 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
987 !CBB_flush(out)) {
988 return 0;
989 }
990
991 return 1;
992}
993
994static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
995 CBS *contents) {
996 ssl->tlsext_ticket_expected = 0;
997
998 if (contents == NULL) {
999 return 1;
1000 }
1001
1002 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1003 * this function should never be called, even if the server tries to send the
1004 * extension. */
1005 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1006
1007 if (CBS_len(contents) != 0) {
1008 return 0;
1009 }
1010
1011 ssl->tlsext_ticket_expected = 1;
1012 return 1;
1013}
1014
David Benjamin0d56f882015-12-19 17:05:56 -05001015static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1016 CBS *contents) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001017 /* This function isn't used because the ticket extension from the client is
David Benjaminc7ce9772015-10-09 19:32:41 -04001018 * handled in ssl_session.c. */
Adam Langley9b05bc52015-07-01 15:25:33 -07001019 return 1;
1020}
1021
1022static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1023 if (!ssl->tlsext_ticket_expected) {
1024 return 1;
1025 }
1026
1027 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1028 * true. */
1029 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1030
1031 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1032 !CBB_add_u16(out, 0 /* length */)) {
1033 return 0;
1034 }
1035
1036 return 1;
1037}
1038
1039
Adam Langley2e857bd2015-07-01 16:09:19 -07001040/* Signature Algorithms.
1041 *
1042 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1043
1044static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1045 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1046 return 1;
1047 }
1048
Steven Valdez02563852016-06-23 13:33:05 -04001049 const uint16_t *sigalgs_data;
Adam Langley2e857bd2015-07-01 16:09:19 -07001050 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1051
1052 CBB contents, sigalgs;
1053 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1054 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez02563852016-06-23 13:33:05 -04001055 !CBB_add_u16_length_prefixed(&contents, &sigalgs)) {
1056 return 0;
1057 }
1058
1059 size_t i;
1060 for (i = 0; i < sigalgs_len; i++) {
1061 if (!CBB_add_u16(&sigalgs, sigalgs_data[i])) {
1062 return 0;
1063 }
1064 }
1065
1066 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001067 return 0;
1068 }
1069
1070 return 1;
1071}
1072
1073static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1074 CBS *contents) {
1075 if (contents != NULL) {
1076 /* Servers MUST NOT send this extension. */
1077 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001078 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001079 return 0;
1080 }
1081
1082 return 1;
1083}
1084
1085static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1086 CBS *contents) {
1087 OPENSSL_free(ssl->cert->peer_sigalgs);
1088 ssl->cert->peer_sigalgs = NULL;
1089 ssl->cert->peer_sigalgslen = 0;
1090
Adam Langley2e857bd2015-07-01 16:09:19 -07001091 if (contents == NULL) {
1092 return 1;
1093 }
1094
1095 CBS supported_signature_algorithms;
1096 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001097 CBS_len(contents) != 0 ||
1098 CBS_len(&supported_signature_algorithms) == 0 ||
1099 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001100 return 0;
1101 }
1102
1103 return 1;
1104}
1105
1106static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1107 /* Servers MUST NOT send this extension. */
1108 return 1;
1109}
1110
1111
Adam Langleybb0bd042015-07-01 16:21:03 -07001112/* OCSP Stapling.
1113 *
1114 * https://tools.ietf.org/html/rfc6066#section-8 */
1115
1116static void ext_ocsp_init(SSL *ssl) {
1117 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001118 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001119}
1120
1121static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1122 if (!ssl->ocsp_stapling_enabled) {
1123 return 1;
1124 }
1125
1126 CBB contents;
1127 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1128 !CBB_add_u16_length_prefixed(out, &contents) ||
1129 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1130 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1131 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1132 !CBB_flush(out)) {
1133 return 0;
1134 }
1135
Adam Langleyce9d85e2016-01-24 15:58:39 -08001136 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001137 return 1;
1138}
1139
1140static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001141 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001142 if (contents == NULL) {
1143 return 1;
1144 }
1145
1146 if (CBS_len(contents) != 0) {
1147 return 0;
1148 }
1149
1150 ssl->s3->tmp.certificate_status_expected = 1;
1151 return 1;
1152}
1153
1154static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1155 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001156 if (contents == NULL) {
1157 return 1;
1158 }
1159
1160 uint8_t status_type;
1161 if (!CBS_get_u8(contents, &status_type)) {
1162 return 0;
1163 }
1164
1165 /* We cannot decide whether OCSP stapling will occur yet because the correct
1166 * SSL_CTX might not have been selected. */
1167 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1168
Adam Langleybb0bd042015-07-01 16:21:03 -07001169 return 1;
1170}
1171
1172static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001173 /* The extension shouldn't be sent when resuming sessions. */
1174 if (ssl->hit ||
1175 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001176 ssl->ctx->ocsp_response_length == 0) {
1177 return 1;
1178 }
1179
1180 ssl->s3->tmp.certificate_status_expected = 1;
1181
1182 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1183 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001184}
1185
1186
Adam Langley97dfcbf2015-07-01 18:35:20 -07001187/* Next protocol negotiation.
1188 *
1189 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1190
1191static void ext_npn_init(SSL *ssl) {
1192 ssl->s3->next_proto_neg_seen = 0;
1193}
1194
1195static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1196 if (ssl->s3->initial_handshake_complete ||
1197 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001198 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001199 SSL_IS_DTLS(ssl)) {
1200 return 1;
1201 }
1202
1203 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1204 !CBB_add_u16(out, 0 /* length */)) {
1205 return 0;
1206 }
1207
1208 return 1;
1209}
1210
1211static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1212 CBS *contents) {
1213 if (contents == NULL) {
1214 return 1;
1215 }
1216
1217 /* If any of these are false then we should never have sent the NPN
1218 * extension in the ClientHello and thus this function should never have been
1219 * called. */
1220 assert(!ssl->s3->initial_handshake_complete);
1221 assert(!SSL_IS_DTLS(ssl));
1222 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001223 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001224
David Benjamin76c2efc2015-08-31 14:24:29 -04001225 if (ssl->s3->alpn_selected != NULL) {
1226 /* NPN and ALPN may not be negotiated in the same connection. */
1227 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1228 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1229 return 0;
1230 }
1231
Adam Langley97dfcbf2015-07-01 18:35:20 -07001232 const uint8_t *const orig_contents = CBS_data(contents);
1233 const size_t orig_len = CBS_len(contents);
1234
1235 while (CBS_len(contents) != 0) {
1236 CBS proto;
1237 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1238 CBS_len(&proto) == 0) {
1239 return 0;
1240 }
1241 }
1242
1243 uint8_t *selected;
1244 uint8_t selected_len;
1245 if (ssl->ctx->next_proto_select_cb(
1246 ssl, &selected, &selected_len, orig_contents, orig_len,
1247 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1248 *out_alert = SSL_AD_INTERNAL_ERROR;
1249 return 0;
1250 }
1251
David Benjamin79978df2015-12-25 15:56:49 -05001252 OPENSSL_free(ssl->s3->next_proto_negotiated);
1253 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1254 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001255 *out_alert = SSL_AD_INTERNAL_ERROR;
1256 return 0;
1257 }
1258
David Benjamin79978df2015-12-25 15:56:49 -05001259 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001260 ssl->s3->next_proto_neg_seen = 1;
1261
1262 return 1;
1263}
1264
1265static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1266 CBS *contents) {
1267 if (contents != NULL && CBS_len(contents) != 0) {
1268 return 0;
1269 }
1270
1271 if (contents == NULL ||
1272 ssl->s3->initial_handshake_complete ||
1273 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1274 * afterwards, parsing the ALPN extension will clear
1275 * |next_proto_neg_seen|. */
1276 ssl->s3->alpn_selected != NULL ||
1277 ssl->ctx->next_protos_advertised_cb == NULL ||
1278 SSL_IS_DTLS(ssl)) {
1279 return 1;
1280 }
1281
1282 ssl->s3->next_proto_neg_seen = 1;
1283 return 1;
1284}
1285
1286static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1287 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1288 * parsed. */
1289 if (!ssl->s3->next_proto_neg_seen) {
1290 return 1;
1291 }
1292
1293 const uint8_t *npa;
1294 unsigned npa_len;
1295
1296 if (ssl->ctx->next_protos_advertised_cb(
1297 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1298 SSL_TLSEXT_ERR_OK) {
1299 ssl->s3->next_proto_neg_seen = 0;
1300 return 1;
1301 }
1302
1303 CBB contents;
1304 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1305 !CBB_add_u16_length_prefixed(out, &contents) ||
1306 !CBB_add_bytes(&contents, npa, npa_len) ||
1307 !CBB_flush(out)) {
1308 return 0;
1309 }
1310
1311 return 1;
1312}
1313
1314
Adam Langleyab8d87d2015-07-10 12:21:39 -07001315/* Signed certificate timestamps.
1316 *
1317 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1318
1319static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1320 if (!ssl->signed_cert_timestamps_enabled) {
1321 return 1;
1322 }
1323
1324 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1325 !CBB_add_u16(out, 0 /* length */)) {
1326 return 0;
1327 }
1328
1329 return 1;
1330}
1331
1332static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1333 CBS *contents) {
1334 if (contents == NULL) {
1335 return 1;
1336 }
1337
1338 /* If this is false then we should never have sent the SCT extension in the
1339 * ClientHello and thus this function should never have been called. */
1340 assert(ssl->signed_cert_timestamps_enabled);
1341
1342 if (CBS_len(contents) == 0) {
1343 *out_alert = SSL_AD_DECODE_ERROR;
1344 return 0;
1345 }
1346
1347 /* Session resumption uses the original session information. */
1348 if (!ssl->hit &&
1349 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1350 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1351 *out_alert = SSL_AD_INTERNAL_ERROR;
1352 return 0;
1353 }
1354
1355 return 1;
1356}
1357
1358static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1359 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001360 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001361}
1362
1363static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001364 /* The extension shouldn't be sent when resuming sessions. */
1365 if (ssl->hit ||
1366 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001367 return 1;
1368 }
1369
1370 CBB contents;
1371 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1372 CBB_add_u16_length_prefixed(out, &contents) &&
1373 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1374 ssl->ctx->signed_cert_timestamp_list_length) &&
1375 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001376}
1377
1378
Adam Langleyf18e4532015-07-10 13:39:53 -07001379/* Application-level Protocol Negotiation.
1380 *
1381 * https://tools.ietf.org/html/rfc7301 */
1382
1383static void ext_alpn_init(SSL *ssl) {
1384 OPENSSL_free(ssl->s3->alpn_selected);
1385 ssl->s3->alpn_selected = NULL;
1386}
1387
1388static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1389 if (ssl->alpn_client_proto_list == NULL ||
1390 ssl->s3->initial_handshake_complete) {
1391 return 1;
1392 }
1393
1394 CBB contents, proto_list;
1395 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1396 !CBB_add_u16_length_prefixed(out, &contents) ||
1397 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1398 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1399 ssl->alpn_client_proto_list_len) ||
1400 !CBB_flush(out)) {
1401 return 0;
1402 }
1403
1404 return 1;
1405}
1406
1407static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1408 CBS *contents) {
1409 if (contents == NULL) {
1410 return 1;
1411 }
1412
1413 assert(!ssl->s3->initial_handshake_complete);
1414 assert(ssl->alpn_client_proto_list != NULL);
1415
David Benjamin76c2efc2015-08-31 14:24:29 -04001416 if (ssl->s3->next_proto_neg_seen) {
1417 /* NPN and ALPN may not be negotiated in the same connection. */
1418 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1419 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1420 return 0;
1421 }
1422
Adam Langleyf18e4532015-07-10 13:39:53 -07001423 /* The extension data consists of a ProtocolNameList which must have
1424 * exactly one ProtocolName. Each of these is length-prefixed. */
1425 CBS protocol_name_list, protocol_name;
1426 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1427 CBS_len(contents) != 0 ||
1428 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1429 /* Empty protocol names are forbidden. */
1430 CBS_len(&protocol_name) == 0 ||
1431 CBS_len(&protocol_name_list) != 0) {
1432 return 0;
1433 }
1434
1435 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1436 &ssl->s3->alpn_selected_len)) {
1437 *out_alert = SSL_AD_INTERNAL_ERROR;
1438 return 0;
1439 }
1440
1441 return 1;
1442}
1443
1444static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1445 CBS *contents) {
1446 if (contents == NULL) {
1447 return 1;
1448 }
1449
1450 if (ssl->ctx->alpn_select_cb == NULL ||
1451 ssl->s3->initial_handshake_complete) {
1452 return 1;
1453 }
1454
1455 /* ALPN takes precedence over NPN. */
1456 ssl->s3->next_proto_neg_seen = 0;
1457
1458 CBS protocol_name_list;
1459 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1460 CBS_len(contents) != 0 ||
1461 CBS_len(&protocol_name_list) < 2) {
1462 return 0;
1463 }
1464
1465 /* Validate the protocol list. */
1466 CBS protocol_name_list_copy = protocol_name_list;
1467 while (CBS_len(&protocol_name_list_copy) > 0) {
1468 CBS protocol_name;
1469
1470 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1471 /* Empty protocol names are forbidden. */
1472 CBS_len(&protocol_name) == 0) {
1473 return 0;
1474 }
1475 }
1476
1477 const uint8_t *selected;
1478 uint8_t selected_len;
1479 if (ssl->ctx->alpn_select_cb(
1480 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1481 CBS_len(&protocol_name_list),
1482 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1483 OPENSSL_free(ssl->s3->alpn_selected);
1484 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1485 if (ssl->s3->alpn_selected == NULL) {
1486 *out_alert = SSL_AD_INTERNAL_ERROR;
1487 return 0;
1488 }
1489 ssl->s3->alpn_selected_len = selected_len;
1490 }
1491
1492 return 1;
1493}
1494
1495static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1496 if (ssl->s3->alpn_selected == NULL) {
1497 return 1;
1498 }
1499
1500 CBB contents, proto_list, proto;
1501 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1502 !CBB_add_u16_length_prefixed(out, &contents) ||
1503 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1504 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001505 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1506 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001507 !CBB_flush(out)) {
1508 return 0;
1509 }
1510
1511 return 1;
1512}
1513
1514
Adam Langley49c7af12015-07-10 14:33:46 -07001515/* Channel ID.
1516 *
1517 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1518
1519static void ext_channel_id_init(SSL *ssl) {
1520 ssl->s3->tlsext_channel_id_valid = 0;
1521}
1522
1523static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1524 if (!ssl->tlsext_channel_id_enabled ||
1525 SSL_IS_DTLS(ssl)) {
1526 return 1;
1527 }
1528
1529 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1530 !CBB_add_u16(out, 0 /* length */)) {
1531 return 0;
1532 }
1533
1534 return 1;
1535}
1536
1537static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1538 CBS *contents) {
1539 if (contents == NULL) {
1540 return 1;
1541 }
1542
1543 assert(!SSL_IS_DTLS(ssl));
1544 assert(ssl->tlsext_channel_id_enabled);
1545
1546 if (CBS_len(contents) != 0) {
1547 return 0;
1548 }
1549
1550 ssl->s3->tlsext_channel_id_valid = 1;
1551 return 1;
1552}
1553
1554static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1555 CBS *contents) {
1556 if (contents == NULL ||
1557 !ssl->tlsext_channel_id_enabled ||
1558 SSL_IS_DTLS(ssl)) {
1559 return 1;
1560 }
1561
1562 if (CBS_len(contents) != 0) {
1563 return 0;
1564 }
1565
1566 ssl->s3->tlsext_channel_id_valid = 1;
1567 return 1;
1568}
1569
1570static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1571 if (!ssl->s3->tlsext_channel_id_valid) {
1572 return 1;
1573 }
1574
1575 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1576 !CBB_add_u16(out, 0 /* length */)) {
1577 return 0;
1578 }
1579
1580 return 1;
1581}
1582
Adam Langley391250d2015-07-15 19:06:07 -07001583
1584/* Secure Real-time Transport Protocol (SRTP) extension.
1585 *
1586 * https://tools.ietf.org/html/rfc5764 */
1587
Adam Langley391250d2015-07-15 19:06:07 -07001588
1589static void ext_srtp_init(SSL *ssl) {
1590 ssl->srtp_profile = NULL;
1591}
1592
1593static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1594 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1595 if (profiles == NULL) {
1596 return 1;
1597 }
1598 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1599 if (num_profiles == 0) {
1600 return 1;
1601 }
1602
1603 CBB contents, profile_ids;
1604 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1605 !CBB_add_u16_length_prefixed(out, &contents) ||
1606 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1607 return 0;
1608 }
1609
1610 size_t i;
1611 for (i = 0; i < num_profiles; i++) {
1612 if (!CBB_add_u16(&profile_ids,
1613 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1614 return 0;
1615 }
1616 }
1617
1618 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1619 !CBB_flush(out)) {
1620 return 0;
1621 }
1622
1623 return 1;
1624}
1625
1626static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1627 CBS *contents) {
1628 if (contents == NULL) {
1629 return 1;
1630 }
1631
1632 /* The extension consists of a u16-prefixed profile ID list containing a
1633 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1634 *
1635 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1636 CBS profile_ids, srtp_mki;
1637 uint16_t profile_id;
1638 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1639 !CBS_get_u16(&profile_ids, &profile_id) ||
1640 CBS_len(&profile_ids) != 0 ||
1641 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1642 CBS_len(contents) != 0) {
1643 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1644 return 0;
1645 }
1646
1647 if (CBS_len(&srtp_mki) != 0) {
1648 /* Must be no MKI, since we never offer one. */
1649 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1650 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1651 return 0;
1652 }
1653
1654 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1655
1656 /* Check to see if the server gave us something we support (and presumably
1657 * offered). */
1658 size_t i;
1659 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1660 const SRTP_PROTECTION_PROFILE *profile =
1661 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1662
1663 if (profile->id == profile_id) {
1664 ssl->srtp_profile = profile;
1665 return 1;
1666 }
1667 }
1668
1669 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1670 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1671 return 0;
1672}
1673
1674static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1675 CBS *contents) {
1676 if (contents == NULL) {
1677 return 1;
1678 }
1679
1680 CBS profile_ids, srtp_mki;
1681 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1682 CBS_len(&profile_ids) < 2 ||
1683 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1684 CBS_len(contents) != 0) {
1685 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1686 return 0;
1687 }
1688 /* Discard the MKI value for now. */
1689
1690 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1691 SSL_get_srtp_profiles(ssl);
1692
1693 /* Pick the server's most preferred profile. */
1694 size_t i;
1695 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1696 const SRTP_PROTECTION_PROFILE *server_profile =
1697 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1698
1699 CBS profile_ids_tmp;
1700 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1701
1702 while (CBS_len(&profile_ids_tmp) > 0) {
1703 uint16_t profile_id;
1704 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1705 return 0;
1706 }
1707
1708 if (server_profile->id == profile_id) {
1709 ssl->srtp_profile = server_profile;
1710 return 1;
1711 }
1712 }
1713 }
1714
1715 return 1;
1716}
1717
1718static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1719 if (ssl->srtp_profile == NULL) {
1720 return 1;
1721 }
1722
1723 CBB contents, profile_ids;
1724 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1725 !CBB_add_u16_length_prefixed(out, &contents) ||
1726 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1727 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1728 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1729 !CBB_flush(out)) {
1730 return 0;
1731 }
1732
1733 return 1;
1734}
1735
Adam Langleybdd5d662015-07-20 16:19:08 -07001736
1737/* EC point formats.
1738 *
1739 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1740
1741static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1742 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1743 return 0;
1744 }
1745
1746 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1747
1748 size_t i;
1749 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1750 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1751
1752 const uint32_t alg_k = cipher->algorithm_mkey;
1753 const uint32_t alg_a = cipher->algorithm_auth;
1754 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1755 return 1;
1756 }
1757 }
1758
1759 return 0;
1760}
1761
Adam Langleybdd5d662015-07-20 16:19:08 -07001762static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001763 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001764 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1765 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001766 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1767 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001768 !CBB_flush(out)) {
1769 return 0;
1770 }
1771
1772 return 1;
1773}
1774
1775static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1776 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1777 return 1;
1778 }
1779
1780 return ext_ec_point_add_extension(ssl, out);
1781}
1782
1783static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1784 CBS *contents) {
1785 if (contents == NULL) {
1786 return 1;
1787 }
1788
1789 CBS ec_point_format_list;
1790 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1791 CBS_len(contents) != 0) {
1792 return 0;
1793 }
1794
David Benjaminfc059942015-07-30 23:01:59 -04001795 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1796 * point format. */
1797 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1798 CBS_len(&ec_point_format_list)) == NULL) {
1799 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001800 return 0;
1801 }
1802
1803 return 1;
1804}
1805
1806static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1807 CBS *contents) {
1808 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1809}
1810
1811static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1812 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1813 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001814 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001815
1816 if (!using_ecc) {
1817 return 1;
1818 }
1819
1820 return ext_ec_point_add_extension(ssl, out);
1821}
1822
Adam Langley273d49c2015-07-20 16:38:52 -07001823
Steven Valdezce902a92016-05-17 11:47:53 -04001824/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07001825 *
Steven Valdezce902a92016-05-17 11:47:53 -04001826 * https://tools.ietf.org/html/rfc4492#section-5.1.2
1827 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07001828
Steven Valdezce902a92016-05-17 11:47:53 -04001829static void ext_supported_groups_init(SSL *ssl) {
1830 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
1831 ssl->s3->tmp.peer_supported_group_list = NULL;
1832 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07001833}
1834
Steven Valdezce902a92016-05-17 11:47:53 -04001835static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07001836 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1837 return 1;
1838 }
1839
Steven Valdezce902a92016-05-17 11:47:53 -04001840 CBB contents, groups_bytes;
1841 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07001842 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001843 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07001844 return 0;
1845 }
1846
Steven Valdezce902a92016-05-17 11:47:53 -04001847 const uint16_t *groups;
1848 size_t groups_len;
1849 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07001850
1851 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04001852 for (i = 0; i < groups_len; i++) {
1853 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07001854 return 0;
1855 }
1856 }
1857
1858 return CBB_flush(out);
1859}
1860
Steven Valdezce902a92016-05-17 11:47:53 -04001861static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1862 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07001863 /* This extension is not expected to be echoed by servers and is ignored. */
1864 return 1;
1865}
1866
Steven Valdezce902a92016-05-17 11:47:53 -04001867static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1868 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07001869 if (contents == NULL) {
1870 return 1;
1871 }
1872
Steven Valdezce902a92016-05-17 11:47:53 -04001873 CBS supported_group_list;
1874 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
1875 CBS_len(&supported_group_list) == 0 ||
1876 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07001877 CBS_len(contents) != 0) {
1878 return 0;
1879 }
1880
Steven Valdezce902a92016-05-17 11:47:53 -04001881 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
1882 CBS_len(&supported_group_list));
1883 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07001884 *out_alert = SSL_AD_INTERNAL_ERROR;
1885 return 0;
1886 }
1887
Steven Valdezce902a92016-05-17 11:47:53 -04001888 const size_t num_groups = CBS_len(&supported_group_list) / 2;
Adam Langley273d49c2015-07-20 16:38:52 -07001889 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04001890 for (i = 0; i < num_groups; i++) {
1891 if (!CBS_get_u16(&supported_group_list,
1892 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07001893 goto err;
1894 }
1895 }
1896
Steven Valdezce902a92016-05-17 11:47:53 -04001897 assert(CBS_len(&supported_group_list) == 0);
1898 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07001899
1900 return 1;
1901
1902err:
Steven Valdezce902a92016-05-17 11:47:53 -04001903 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
1904 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07001905 *out_alert = SSL_AD_INTERNAL_ERROR;
1906 return 0;
1907}
1908
Steven Valdezce902a92016-05-17 11:47:53 -04001909static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07001910 /* Servers don't echo this extension. */
1911 return 1;
1912}
1913
1914
Adam Langley614c66a2015-06-12 15:26:58 -07001915/* kExtensions contains all the supported extensions. */
1916static const struct tls_extension kExtensions[] = {
1917 {
Adam Langley5021b222015-06-12 18:27:58 -07001918 /* The renegotiation extension must always be at index zero because the
1919 * |received| and |sent| bitsets need to be tweaked when the "extension" is
1920 * sent as an SCSV. */
1921 TLSEXT_TYPE_renegotiate,
1922 NULL,
1923 ext_ri_add_clienthello,
1924 ext_ri_parse_serverhello,
1925 ext_ri_parse_clienthello,
1926 ext_ri_add_serverhello,
1927 },
1928 {
Adam Langley614c66a2015-06-12 15:26:58 -07001929 TLSEXT_TYPE_server_name,
1930 ext_sni_init,
1931 ext_sni_add_clienthello,
1932 ext_sni_parse_serverhello,
1933 ext_sni_parse_clienthello,
1934 ext_sni_add_serverhello,
1935 },
Adam Langley0a056712015-07-01 15:03:33 -07001936 {
1937 TLSEXT_TYPE_extended_master_secret,
1938 ext_ems_init,
1939 ext_ems_add_clienthello,
1940 ext_ems_parse_serverhello,
1941 ext_ems_parse_clienthello,
1942 ext_ems_add_serverhello,
1943 },
Adam Langley9b05bc52015-07-01 15:25:33 -07001944 {
1945 TLSEXT_TYPE_session_ticket,
1946 NULL,
1947 ext_ticket_add_clienthello,
1948 ext_ticket_parse_serverhello,
1949 ext_ticket_parse_clienthello,
1950 ext_ticket_add_serverhello,
1951 },
Adam Langley2e857bd2015-07-01 16:09:19 -07001952 {
1953 TLSEXT_TYPE_signature_algorithms,
1954 NULL,
1955 ext_sigalgs_add_clienthello,
1956 ext_sigalgs_parse_serverhello,
1957 ext_sigalgs_parse_clienthello,
1958 ext_sigalgs_add_serverhello,
1959 },
Adam Langleybb0bd042015-07-01 16:21:03 -07001960 {
1961 TLSEXT_TYPE_status_request,
1962 ext_ocsp_init,
1963 ext_ocsp_add_clienthello,
1964 ext_ocsp_parse_serverhello,
1965 ext_ocsp_parse_clienthello,
1966 ext_ocsp_add_serverhello,
1967 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07001968 {
1969 TLSEXT_TYPE_next_proto_neg,
1970 ext_npn_init,
1971 ext_npn_add_clienthello,
1972 ext_npn_parse_serverhello,
1973 ext_npn_parse_clienthello,
1974 ext_npn_add_serverhello,
1975 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07001976 {
1977 TLSEXT_TYPE_certificate_timestamp,
1978 NULL,
1979 ext_sct_add_clienthello,
1980 ext_sct_parse_serverhello,
1981 ext_sct_parse_clienthello,
1982 ext_sct_add_serverhello,
1983 },
Adam Langleyf18e4532015-07-10 13:39:53 -07001984 {
1985 TLSEXT_TYPE_application_layer_protocol_negotiation,
1986 ext_alpn_init,
1987 ext_alpn_add_clienthello,
1988 ext_alpn_parse_serverhello,
1989 ext_alpn_parse_clienthello,
1990 ext_alpn_add_serverhello,
1991 },
Adam Langley49c7af12015-07-10 14:33:46 -07001992 {
1993 TLSEXT_TYPE_channel_id,
1994 ext_channel_id_init,
1995 ext_channel_id_add_clienthello,
1996 ext_channel_id_parse_serverhello,
1997 ext_channel_id_parse_clienthello,
1998 ext_channel_id_add_serverhello,
1999 },
Adam Langley391250d2015-07-15 19:06:07 -07002000 {
2001 TLSEXT_TYPE_srtp,
2002 ext_srtp_init,
2003 ext_srtp_add_clienthello,
2004 ext_srtp_parse_serverhello,
2005 ext_srtp_parse_clienthello,
2006 ext_srtp_add_serverhello,
2007 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002008 {
2009 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002010 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002011 ext_ec_point_add_clienthello,
2012 ext_ec_point_parse_serverhello,
2013 ext_ec_point_parse_clienthello,
2014 ext_ec_point_add_serverhello,
2015 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002016 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2017 * intolerant to the last extension being zero-length. See
2018 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002019 {
Steven Valdezce902a92016-05-17 11:47:53 -04002020 TLSEXT_TYPE_supported_groups,
2021 ext_supported_groups_init,
2022 ext_supported_groups_add_clienthello,
2023 ext_supported_groups_parse_serverhello,
2024 ext_supported_groups_parse_clienthello,
2025 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002026 },
Adam Langley614c66a2015-06-12 15:26:58 -07002027};
2028
2029#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2030
Adam Langley4cfa96b2015-07-01 11:56:55 -07002031OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2032 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002033 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002034OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2035 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2036 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002037 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002038
Adam Langley614c66a2015-06-12 15:26:58 -07002039static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2040 uint16_t value) {
2041 unsigned i;
2042 for (i = 0; i < kNumExtensions; i++) {
2043 if (kExtensions[i].value == value) {
2044 *out_index = i;
2045 return &kExtensions[i];
2046 }
2047 }
2048
2049 return NULL;
2050}
2051
Adam Langley09505632015-07-30 18:10:13 -07002052int SSL_extension_supported(unsigned extension_value) {
2053 uint32_t index;
2054 return extension_value == TLSEXT_TYPE_padding ||
2055 tls_extension_find(&index, extension_value) != NULL;
2056}
2057
David Benjamine8d53502015-10-10 14:13:23 -04002058int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002059 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002060 if (ssl->client_version == SSL3_VERSION &&
2061 !ssl->s3->send_connection_binding) {
2062 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002063 }
Adam Langley95c29f32014-06-20 12:00:00 -07002064
David Benjamine8d53502015-10-10 14:13:23 -04002065 CBB extensions;
2066 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002067 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002068 }
Adam Langley95c29f32014-06-20 12:00:00 -07002069
David Benjamine8d53502015-10-10 14:13:23 -04002070 ssl->s3->tmp.extensions.sent = 0;
2071 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002072
Adam Langley614c66a2015-06-12 15:26:58 -07002073 size_t i;
2074 for (i = 0; i < kNumExtensions; i++) {
2075 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002076 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002077 }
2078 }
Adam Langley95c29f32014-06-20 12:00:00 -07002079
Adam Langley614c66a2015-06-12 15:26:58 -07002080 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002081 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002082 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002083 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2084 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2085 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002086 }
Adam Langley95c29f32014-06-20 12:00:00 -07002087
Adam Langley33ad2b52015-07-20 17:43:53 -07002088 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002089 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002090 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002091 }
Adam Langley75712922014-10-10 16:23:43 -07002092
David Benjamine8d53502015-10-10 14:13:23 -04002093 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002094 goto err;
2095 }
2096
David Benjamine8d53502015-10-10 14:13:23 -04002097 if (!SSL_IS_DTLS(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002098 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002099 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002100 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002101 *
2102 * NB: because this code works out the length of all existing extensions
2103 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002104 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002105 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002106 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002107 * Server 7.0 is intolerant to the last extension being zero-length. See
2108 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002109 if (padding_len >= 4 + 1) {
2110 padding_len -= 4;
2111 } else {
2112 padding_len = 1;
2113 }
Adam Langley95c29f32014-06-20 12:00:00 -07002114
Adam Langley33ad2b52015-07-20 17:43:53 -07002115 uint8_t *padding_bytes;
2116 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2117 !CBB_add_u16(&extensions, padding_len) ||
2118 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2119 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002120 }
Adam Langley75712922014-10-10 16:23:43 -07002121
Adam Langley33ad2b52015-07-20 17:43:53 -07002122 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002123 }
2124 }
Adam Langley75712922014-10-10 16:23:43 -07002125
David Benjamina01deee2015-12-08 18:56:31 -05002126 /* Discard empty extensions blocks. */
2127 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002128 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002129 }
2130
David Benjamine8d53502015-10-10 14:13:23 -04002131 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002132
2133err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002134 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002135 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002136}
Adam Langley95c29f32014-06-20 12:00:00 -07002137
David Benjamin56380462015-10-10 14:59:09 -04002138int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002139 CBB extensions;
2140 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002141 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002142 }
2143
2144 unsigned i;
2145 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002146 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002147 /* Don't send extensions that were not received. */
2148 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002149 }
Adam Langley95c29f32014-06-20 12:00:00 -07002150
David Benjamin56380462015-10-10 14:59:09 -04002151 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002152 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2153 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2154 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002155 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002156 }
Adam Langley95c29f32014-06-20 12:00:00 -07002157
David Benjamin56380462015-10-10 14:59:09 -04002158 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002159 goto err;
2160 }
2161
David Benjamina01deee2015-12-08 18:56:31 -05002162 /* Discard empty extensions blocks. */
2163 if (CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002164 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002165 }
2166
David Benjamin56380462015-10-10 14:59:09 -04002167 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002168
2169err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002170 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002171 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002172}
Adam Langley95c29f32014-06-20 12:00:00 -07002173
David Benjamin0d56f882015-12-19 17:05:56 -05002174static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002175 size_t i;
2176 for (i = 0; i < kNumExtensions; i++) {
2177 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002178 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002179 }
2180 }
2181
David Benjamin0d56f882015-12-19 17:05:56 -05002182 ssl->s3->tmp.extensions.received = 0;
2183 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002184 /* The renegotiation extension must always be at index zero because the
2185 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2186 * sent as an SCSV. */
2187 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002188
Adam Langleyfcf25832014-12-18 17:42:32 -08002189 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002190 if (CBS_len(cbs) != 0) {
2191 /* Decode the extensions block and check it is valid. */
2192 CBS extensions;
2193 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2194 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002195 *out_alert = SSL_AD_DECODE_ERROR;
2196 return 0;
2197 }
Adam Langley95c29f32014-06-20 12:00:00 -07002198
Adam Langley33ad2b52015-07-20 17:43:53 -07002199 while (CBS_len(&extensions) != 0) {
2200 uint16_t type;
2201 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002202
Adam Langley33ad2b52015-07-20 17:43:53 -07002203 /* Decode the next extension. */
2204 if (!CBS_get_u16(&extensions, &type) ||
2205 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2206 *out_alert = SSL_AD_DECODE_ERROR;
2207 return 0;
2208 }
2209
David Benjaminc7ce9772015-10-09 19:32:41 -04002210 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2211 * ambiguous. Ignore all but the renegotiation_info extension. */
David Benjamin0d56f882015-12-19 17:05:56 -05002212 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
David Benjaminc7ce9772015-10-09 19:32:41 -04002213 continue;
2214 }
2215
Adam Langley33ad2b52015-07-20 17:43:53 -07002216 unsigned ext_index;
2217 const struct tls_extension *const ext =
2218 tls_extension_find(&ext_index, type);
2219
2220 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002221 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002222 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2223 return 0;
2224 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002225 continue;
2226 }
2227
David Benjamin0d56f882015-12-19 17:05:56 -05002228 ssl->s3->tmp.extensions.received |= (1u << ext_index);
Adam Langley614c66a2015-06-12 15:26:58 -07002229 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002230 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
Adam Langley614c66a2015-06-12 15:26:58 -07002231 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002232 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2233 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002234 return 0;
2235 }
Adam Langley614c66a2015-06-12 15:26:58 -07002236 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002237 }
Adam Langley75712922014-10-10 16:23:43 -07002238
Adam Langley614c66a2015-06-12 15:26:58 -07002239 for (i = 0; i < kNumExtensions; i++) {
David Benjamin0d56f882015-12-19 17:05:56 -05002240 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002241 /* Extension wasn't observed so call the callback with a NULL
2242 * parameter. */
2243 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002244 if (!kExtensions[i].parse_clienthello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002245 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2246 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002247 *out_alert = alert;
2248 return 0;
2249 }
2250 }
2251 }
2252
Adam Langleyfcf25832014-12-18 17:42:32 -08002253 return 1;
2254}
Adam Langley95c29f32014-06-20 12:00:00 -07002255
David Benjamin0d56f882015-12-19 17:05:56 -05002256int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002257 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002258 if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) {
2259 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002260 return 0;
2261 }
Adam Langley95c29f32014-06-20 12:00:00 -07002262
David Benjamin0d56f882015-12-19 17:05:56 -05002263 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002264 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002265 return 0;
2266 }
Adam Langley95c29f32014-06-20 12:00:00 -07002267
Adam Langleyfcf25832014-12-18 17:42:32 -08002268 return 1;
2269}
Adam Langley95c29f32014-06-20 12:00:00 -07002270
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002271OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2272
David Benjamin0d56f882015-12-19 17:05:56 -05002273static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002274 uint32_t received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002275
Adam Langley33ad2b52015-07-20 17:43:53 -07002276 if (CBS_len(cbs) != 0) {
2277 /* Decode the extensions block and check it is valid. */
2278 CBS extensions;
2279 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2280 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002281 *out_alert = SSL_AD_DECODE_ERROR;
2282 return 0;
2283 }
Adam Langley95c29f32014-06-20 12:00:00 -07002284
Adam Langley614c66a2015-06-12 15:26:58 -07002285
Adam Langley33ad2b52015-07-20 17:43:53 -07002286 while (CBS_len(&extensions) != 0) {
2287 uint16_t type;
2288 CBS extension;
2289
2290 /* Decode the next extension. */
2291 if (!CBS_get_u16(&extensions, &type) ||
2292 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2293 *out_alert = SSL_AD_DECODE_ERROR;
2294 return 0;
2295 }
2296
2297 unsigned ext_index;
2298 const struct tls_extension *const ext =
2299 tls_extension_find(&ext_index, type);
2300
Adam Langley09505632015-07-30 18:10:13 -07002301 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002302 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002303 return 0;
2304 }
2305 continue;
2306 }
2307
David Benjamin0d56f882015-12-19 17:05:56 -05002308 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) {
Adam Langley09505632015-07-30 18:10:13 -07002309 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002310 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002311 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002312 *out_alert = SSL_AD_DECODE_ERROR;
2313 return 0;
2314 }
David Benjamin03973092014-06-24 23:27:17 -04002315
Adam Langley614c66a2015-06-12 15:26:58 -07002316 received |= (1u << ext_index);
2317
2318 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002319 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002320 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2321 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002322 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002323 return 0;
2324 }
Adam Langley614c66a2015-06-12 15:26:58 -07002325 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002326 }
Adam Langley95c29f32014-06-20 12:00:00 -07002327
Adam Langley33ad2b52015-07-20 17:43:53 -07002328 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002329 for (i = 0; i < kNumExtensions; i++) {
2330 if (!(received & (1u << i))) {
2331 /* Extension wasn't observed so call the callback with a NULL
2332 * parameter. */
2333 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002334 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002335 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2336 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002337 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002338 return 0;
2339 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002340 }
2341 }
Adam Langley95c29f32014-06-20 12:00:00 -07002342
Adam Langleyfcf25832014-12-18 17:42:32 -08002343 return 1;
2344}
Adam Langley95c29f32014-06-20 12:00:00 -07002345
David Benjamin0d56f882015-12-19 17:05:56 -05002346static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002347 int ret = SSL_TLSEXT_ERR_NOACK;
2348 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002349
David Benjamin78f8aab2016-03-10 16:33:58 -05002350 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002351 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002352 ssl->ctx->tlsext_servername_arg);
2353 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002354 ret = ssl->initial_ctx->tlsext_servername_callback(
2355 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002356 }
Adam Langley95c29f32014-06-20 12:00:00 -07002357
Adam Langleyfcf25832014-12-18 17:42:32 -08002358 switch (ret) {
2359 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002360 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002361 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002362
Adam Langleyfcf25832014-12-18 17:42:32 -08002363 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002364 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002365 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002366
Adam Langleyfcf25832014-12-18 17:42:32 -08002367 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002368 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002369 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002370
Adam Langleyfcf25832014-12-18 17:42:32 -08002371 default:
2372 return 1;
2373 }
2374}
Adam Langleyed8270a2014-09-02 13:52:56 -07002375
David Benjamin0d56f882015-12-19 17:05:56 -05002376static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002377 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002378 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002379
David Benjamin78f8aab2016-03-10 16:33:58 -05002380 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002381 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002382 ssl->ctx->tlsext_servername_arg);
2383 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002384 ret = ssl->initial_ctx->tlsext_servername_callback(
2385 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002386 }
Adam Langley95c29f32014-06-20 12:00:00 -07002387
Adam Langleyfcf25832014-12-18 17:42:32 -08002388 switch (ret) {
2389 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002390 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002391 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002392
Adam Langleyfcf25832014-12-18 17:42:32 -08002393 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002394 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002395 return 1;
2396
2397 default:
2398 return 1;
2399 }
2400}
2401
David Benjamin0d56f882015-12-19 17:05:56 -05002402int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002403 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002404 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2405 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002406 return 0;
2407 }
2408
David Benjamin0d56f882015-12-19 17:05:56 -05002409 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002410 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002411 return 0;
2412 }
2413
2414 return 1;
2415}
Adam Langley95c29f32014-06-20 12:00:00 -07002416
David Benjamine3aa1d92015-06-16 15:34:50 -04002417int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002418 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002419 size_t ticket_len, const uint8_t *session_id,
2420 size_t session_id_len) {
2421 int ret = 1; /* Most errors are non-fatal. */
2422 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2423 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002424
David Benjamine3aa1d92015-06-16 15:34:50 -04002425 HMAC_CTX hmac_ctx;
2426 HMAC_CTX_init(&hmac_ctx);
2427 EVP_CIPHER_CTX cipher_ctx;
2428 EVP_CIPHER_CTX_init(&cipher_ctx);
2429
David Benjaminef1b0092015-11-21 14:05:44 -05002430 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002431 *out_session = NULL;
2432
2433 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2434 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002435 }
2436
David Benjaminadcc3952015-04-26 13:07:57 -04002437 /* Ensure there is room for the key name and the largest IV
2438 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2439 * the maximum IV length should be well under the minimum size for the
2440 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002441 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2442 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002443 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002444 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002445
David Benjamine3aa1d92015-06-16 15:34:50 -04002446 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002447 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2448 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2449 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002450 if (cb_ret < 0) {
2451 ret = 0;
2452 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002453 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002454 if (cb_ret == 0) {
2455 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002456 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002457 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002458 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002459 }
2460 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002461 /* Check the key name matches. */
2462 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2463 SSL_TICKET_KEY_NAME_LEN) != 0) {
2464 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002465 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002466 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2467 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002468 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002469 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2470 ssl_ctx->tlsext_tick_aes_key, iv)) {
2471 ret = 0;
2472 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002473 }
2474 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002475 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002476
David Benjamine3aa1d92015-06-16 15:34:50 -04002477 /* Check the MAC at the end of the ticket. */
2478 uint8_t mac[EVP_MAX_MD_SIZE];
2479 size_t mac_len = HMAC_size(&hmac_ctx);
2480 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002481 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002482 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002483 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002484 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2485 HMAC_Final(&hmac_ctx, mac, NULL);
2486 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2487 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002488 }
2489
David Benjamine3aa1d92015-06-16 15:34:50 -04002490 /* Decrypt the session data. */
2491 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2492 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2493 mac_len;
2494 plaintext = OPENSSL_malloc(ciphertext_len);
2495 if (plaintext == NULL) {
2496 ret = 0;
2497 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002498 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002499 if (ciphertext_len >= INT_MAX) {
2500 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002501 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002502 int len1, len2;
2503 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2504 (int)ciphertext_len) ||
2505 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2506 ERR_clear_error(); /* Don't leave an error on the queue. */
2507 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002508 }
2509
David Benjamine3aa1d92015-06-16 15:34:50 -04002510 /* Decode the session. */
2511 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2512 if (session == NULL) {
2513 ERR_clear_error(); /* Don't leave an error on the queue. */
2514 goto done;
2515 }
2516
2517 /* Copy the client's session ID into the new session, to denote the ticket has
2518 * been accepted. */
2519 memcpy(session->session_id, session_id, session_id_len);
2520 session->session_id_length = session_id_len;
2521
2522 *out_session = session;
2523
2524done:
2525 OPENSSL_free(plaintext);
2526 HMAC_CTX_cleanup(&hmac_ctx);
2527 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2528 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002529}
Adam Langley95c29f32014-06-20 12:00:00 -07002530
Steven Valdez02563852016-06-23 13:33:05 -04002531/* Tables to translate from NIDs to TLS v1.2 ids
2532 *
2533 * TODO(svaldez): Remove decomposition of SignatureAlgorithm IDs. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002534typedef struct {
Steven Valdez02563852016-06-23 13:33:05 -04002535 int pkey_type;
2536 int md_type;
2537 uint16_t id;
Adam Langleyfcf25832014-12-18 17:42:32 -08002538} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002539
Steven Valdez02563852016-06-23 13:33:05 -04002540static const tls12_lookup kTLS12SignatureAlgorithmIDs[] = {
2541 {EVP_PKEY_RSA, NID_sha512, SSL_SIGN_RSA_PKCS1_SHA512},
2542 {EVP_PKEY_EC, NID_sha512, SSL_SIGN_ECDSA_SECP521R1_SHA512},
2543 {EVP_PKEY_RSA, NID_sha384, SSL_SIGN_RSA_PKCS1_SHA384},
2544 {EVP_PKEY_EC, NID_sha384, SSL_SIGN_ECDSA_SECP384R1_SHA384},
2545 {EVP_PKEY_RSA, NID_sha256, SSL_SIGN_RSA_PKCS1_SHA256},
2546 {EVP_PKEY_EC, NID_sha256, SSL_SIGN_ECDSA_SECP256R1_SHA256},
2547 {EVP_PKEY_RSA, NID_sha1, SSL_SIGN_RSA_PKCS1_SHA1},
2548 {EVP_PKEY_EC, NID_sha1, SSL_SIGN_ECDSA_SHA1},
David Benjamin72f7e212016-01-29 15:28:58 -05002549};
Adam Langley95c29f32014-06-20 12:00:00 -07002550
Steven Valdez02563852016-06-23 13:33:05 -04002551int tls12_add_sigalg(SSL *ssl, CBB *out, const EVP_MD *md) {
2552 int pkey_type = ssl_private_key_type(ssl);
2553 int md_type = EVP_MD_type(md);
Adam Langley95c29f32014-06-20 12:00:00 -07002554
Adam Langleyfcf25832014-12-18 17:42:32 -08002555 size_t i;
Steven Valdez02563852016-06-23 13:33:05 -04002556 for (i = 0; i < sizeof(kTLS12SignatureAlgorithmIDs) / sizeof(tls12_lookup);
2557 i++) {
2558 if (kTLS12SignatureAlgorithmIDs[i].pkey_type == pkey_type &&
2559 kTLS12SignatureAlgorithmIDs[i].md_type == md_type) {
2560 return CBB_add_u16(out, kTLS12SignatureAlgorithmIDs[i].id);
Adam Langleyfcf25832014-12-18 17:42:32 -08002561 }
2562 }
Adam Langley95c29f32014-06-20 12:00:00 -07002563
Steven Valdez02563852016-06-23 13:33:05 -04002564 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002565}
Adam Langley95c29f32014-06-20 12:00:00 -07002566
Steven Valdez02563852016-06-23 13:33:05 -04002567static const EVP_MD *tls12_get_hash(uint16_t sig_alg) {
2568 switch (sig_alg >> 8) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002569 case TLSEXT_hash_sha1:
2570 return EVP_sha1();
2571
Adam Langleyfcf25832014-12-18 17:42:32 -08002572 case TLSEXT_hash_sha256:
2573 return EVP_sha256();
2574
2575 case TLSEXT_hash_sha384:
2576 return EVP_sha384();
2577
2578 case TLSEXT_hash_sha512:
2579 return EVP_sha512();
2580
2581 default:
2582 return NULL;
2583 }
2584}
Adam Langley95c29f32014-06-20 12:00:00 -07002585
David Benjaminec2f27d2014-11-13 19:17:25 -05002586/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2587 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Steven Valdez02563852016-06-23 13:33:05 -04002588static int tls12_get_pkey_type(uint16_t sig_alg) {
2589 switch (sig_alg & 0xff) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002590 case TLSEXT_signature_rsa:
2591 return EVP_PKEY_RSA;
2592
2593 case TLSEXT_signature_ecdsa:
2594 return EVP_PKEY_EC;
2595
2596 default:
2597 return -1;
2598 }
2599}
Adam Langley95c29f32014-06-20 12:00:00 -07002600
Steven Valdez0d62f262015-09-04 12:41:04 -04002601int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002602 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05002603 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002604 return 1;
2605 }
David Benjamincd996942014-07-20 16:23:51 -04002606
Steven Valdez0d62f262015-09-04 12:41:04 -04002607 CERT *const cert = ssl->cert;
2608 OPENSSL_free(cert->peer_sigalgs);
2609 cert->peer_sigalgs = NULL;
2610 cert->peer_sigalgslen = 0;
2611
2612 size_t num_sigalgs = CBS_len(in_sigalgs);
2613
2614 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002615 return 0;
2616 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002617 num_sigalgs /= 2;
2618
2619 /* supported_signature_algorithms in the certificate request is
2620 * allowed to be empty. */
2621 if (num_sigalgs == 0) {
2622 return 1;
2623 }
2624
Steven Valdez02563852016-06-23 13:33:05 -04002625 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
2626 * and we just divided |num_sigalgs| by two. */
2627 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
Steven Valdez0d62f262015-09-04 12:41:04 -04002628 if (cert->peer_sigalgs == NULL) {
2629 return 0;
2630 }
2631 cert->peer_sigalgslen = num_sigalgs;
2632
2633 CBS sigalgs;
2634 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2635
2636 size_t i;
2637 for (i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04002638 if (!CBS_get_u16(&sigalgs, &cert->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04002639 return 0;
2640 }
2641 }
Adam Langley95c29f32014-06-20 12:00:00 -07002642
Adam Langleyfcf25832014-12-18 17:42:32 -08002643 return 1;
2644}
David Benjaminec2f27d2014-11-13 19:17:25 -05002645
David Benjamind1d80782015-07-05 11:54:09 -04002646const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2647 CERT *cert = ssl->cert;
2648 int type = ssl_private_key_type(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -04002649 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002650
Steven Valdez0d62f262015-09-04 12:41:04 -04002651 static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
David Benjamin72f7e212016-01-29 15:28:58 -05002652 NID_sha1};
Steven Valdez0d62f262015-09-04 12:41:04 -04002653
2654 const int *digest_nids = kDefaultDigestList;
2655 size_t num_digest_nids =
2656 sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
2657 if (cert->digest_nids != NULL) {
2658 digest_nids = cert->digest_nids;
2659 num_digest_nids = cert->num_digest_nids;
2660 }
2661
2662 for (i = 0; i < num_digest_nids; i++) {
2663 const int digest_nid = digest_nids[i];
2664 for (j = 0; j < cert->peer_sigalgslen; j++) {
Steven Valdez02563852016-06-23 13:33:05 -04002665 const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j]);
Steven Valdez0d62f262015-09-04 12:41:04 -04002666 if (md == NULL ||
2667 digest_nid != EVP_MD_type(md) ||
Steven Valdez02563852016-06-23 13:33:05 -04002668 tls12_get_pkey_type(cert->peer_sigalgs[j]) != type) {
Steven Valdez0d62f262015-09-04 12:41:04 -04002669 continue;
2670 }
2671
2672 return md;
Adam Langleyfcf25832014-12-18 17:42:32 -08002673 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002674 }
Adam Langley95c29f32014-06-20 12:00:00 -07002675
Adam Langleyfcf25832014-12-18 17:42:32 -08002676 /* If no suitable digest may be found, default to SHA-1. */
2677 return EVP_sha1();
2678}
Adam Langley95c29f32014-06-20 12:00:00 -07002679
David Benjamind6a4ae92015-08-06 11:10:51 -04002680int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2681 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002682 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002683
2684 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002685 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2686 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002687 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002688
David Benjamind6a4ae92015-08-06 11:10:51 -04002689 static const char kClientIDMagic[] = "TLS Channel ID signature";
2690 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2691
2692 if (ssl->hit) {
2693 static const char kResumptionMagic[] = "Resumption";
2694 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2695 if (ssl->session->original_handshake_hash_len == 0) {
2696 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2697 goto err;
2698 }
2699 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2700 ssl->session->original_handshake_hash_len);
2701 }
2702
2703 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2704 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2705 sizeof(handshake_hash));
2706 if (handshake_hash_len < 0) {
2707 goto err;
2708 }
2709 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2710 unsigned len_u;
2711 EVP_DigestFinal_ex(&ctx, out, &len_u);
2712 *out_len = len_u;
2713
2714 ret = 1;
2715
2716err:
2717 EVP_MD_CTX_cleanup(&ctx);
2718 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002719}
Adam Langley1258b6a2014-06-20 12:00:00 -07002720
2721/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin0d56f882015-12-19 17:05:56 -05002722 * hashes in |ssl->session| so that Channel ID resumptions can sign that
2723 * data. */
2724int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002725 int digest_len;
2726 /* This function should never be called for a resumed session because the
2727 * handshake hashes that we wish to record are for the original, full
2728 * handshake. */
David Benjamin0d56f882015-12-19 17:05:56 -05002729 if (ssl->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002730 return -1;
2731 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002732
Adam Langleyfcf25832014-12-18 17:42:32 -08002733 digest_len =
David Benjamin0d56f882015-12-19 17:05:56 -05002734 tls1_handshake_digest(ssl, ssl->session->original_handshake_hash,
2735 sizeof(ssl->session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08002736 if (digest_len < 0) {
2737 return -1;
2738 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002739
David Benjamin0d56f882015-12-19 17:05:56 -05002740 ssl->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002741
Adam Langleyfcf25832014-12-18 17:42:32 -08002742 return 1;
2743}