blob: 459464987aba9ab7557d4fb866427733bffc22a1 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin03973092014-06-24 23:27:17 -0400116#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400117#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/evp.h>
120#include <openssl/hmac.h>
121#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400122#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/rand.h>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700124#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800127
128
David Benjamin0d56f882015-12-19 17:05:56 -0500129static int ssl_check_clienthello_tlsext(SSL *ssl);
130static int ssl_check_serverhello_tlsext(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700131
Adam Langleyfcf25832014-12-18 17:42:32 -0800132static int compare_uint16_t(const void *p1, const void *p2) {
133 uint16_t u1 = *((const uint16_t *)p1);
134 uint16_t u2 = *((const uint16_t *)p2);
135 if (u1 < u2) {
136 return -1;
137 } else if (u1 > u2) {
138 return 1;
139 } else {
140 return 0;
141 }
142}
David Benjamin35a7a442014-07-05 00:23:20 -0400143
Adam Langleyfcf25832014-12-18 17:42:32 -0800144/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
145 * more than one extension of the same type in a ClientHello or ServerHello.
146 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400147 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800148static int tls1_check_duplicate_extensions(const CBS *cbs) {
149 CBS extensions = *cbs;
150 size_t num_extensions = 0, i = 0;
151 uint16_t *extension_types = NULL;
152 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400153
Adam Langleyfcf25832014-12-18 17:42:32 -0800154 /* First pass: count the extensions. */
155 while (CBS_len(&extensions) > 0) {
156 uint16_t type;
157 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400158
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 if (!CBS_get_u16(&extensions, &type) ||
160 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
161 goto done;
162 }
David Benjamin35a7a442014-07-05 00:23:20 -0400163
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 num_extensions++;
165 }
David Benjamin35a7a442014-07-05 00:23:20 -0400166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 if (num_extensions == 0) {
168 return 1;
169 }
David Benjamin9a373592014-07-25 04:27:53 -0400170
Brian Smith5ba06892016-02-07 09:36:04 -1000171 extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800172 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400173 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800174 goto done;
175 }
David Benjamin35a7a442014-07-05 00:23:20 -0400176
Adam Langleyfcf25832014-12-18 17:42:32 -0800177 /* Second pass: gather the extension types. */
178 extensions = *cbs;
179 for (i = 0; i < num_extensions; i++) {
180 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400181
Adam Langleyfcf25832014-12-18 17:42:32 -0800182 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
183 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
184 /* This should not happen. */
185 goto done;
186 }
187 }
188 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400189
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 /* Sort the extensions and make sure there are no duplicates. */
191 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
192 for (i = 1; i < num_extensions; i++) {
193 if (extension_types[i - 1] == extension_types[i]) {
194 goto done;
195 }
196 }
David Benjamin35a7a442014-07-05 00:23:20 -0400197
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 ret = 1;
199
David Benjamin35a7a442014-07-05 00:23:20 -0400200done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400201 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 return ret;
203}
David Benjamin35a7a442014-07-05 00:23:20 -0400204
Adam Langleyfcf25832014-12-18 17:42:32 -0800205char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
206 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400207
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700209
Adam Langleyfcf25832014-12-18 17:42:32 -0800210 if (/* Skip client version. */
211 !CBS_skip(&client_hello, 2) ||
212 /* Skip client nonce. */
213 !CBS_skip(&client_hello, 32) ||
214 /* Extract session_id. */
215 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
216 return 0;
217 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700218
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 ctx->session_id = CBS_data(&session_id);
220 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700221
Adam Langleyfcf25832014-12-18 17:42:32 -0800222 /* Skip past DTLS cookie */
223 if (SSL_IS_DTLS(ctx->ssl)) {
224 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
227 return 0;
228 }
229 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700230
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 /* Extract cipher_suites. */
232 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
233 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
234 return 0;
235 }
236 ctx->cipher_suites = CBS_data(&cipher_suites);
237 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700238
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 /* Extract compression_methods. */
240 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
241 CBS_len(&compression_methods) < 1) {
242 return 0;
243 }
244 ctx->compression_methods = CBS_data(&compression_methods);
245 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700246
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 /* If the ClientHello ends here then it's valid, but doesn't have any
248 * extensions. (E.g. SSLv3.) */
249 if (CBS_len(&client_hello) == 0) {
250 ctx->extensions = NULL;
251 ctx->extensions_len = 0;
252 return 1;
253 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700254
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 /* Extract extensions and check it is valid. */
256 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
257 !tls1_check_duplicate_extensions(&extensions) ||
258 CBS_len(&client_hello) != 0) {
259 return 0;
260 }
261 ctx->extensions = CBS_data(&extensions);
262 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 return 1;
265}
Adam Langleydc9b1412014-06-20 12:00:00 -0700266
David Benjamind4c2bce2015-10-17 12:28:18 -0400267int SSL_early_callback_ctx_extension_get(
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
269 const uint8_t **out_data, size_t *out_len) {
270 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700271
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700273
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 while (CBS_len(&extensions) != 0) {
275 uint16_t type;
276 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400277
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 /* Decode the next extension. */
279 if (!CBS_get_u16(&extensions, &type) ||
280 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
281 return 0;
282 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 if (type == extension_type) {
285 *out_data = CBS_data(&extension);
286 *out_len = CBS_len(&extension);
287 return 1;
288 }
289 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700290
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 return 0;
292}
Adam Langley95c29f32014-06-20 12:00:00 -0700293
Steven Valdezce902a92016-05-17 11:47:53 -0400294static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400295 SSL_CURVE_X25519,
296 SSL_CURVE_SECP256R1,
297 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700298#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin9e68f192016-06-30 14:55:33 -0400299 SSL_CURVE_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700300#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800301};
Adam Langley95c29f32014-06-20 12:00:00 -0700302
Steven Valdezce902a92016-05-17 11:47:53 -0400303/* tls1_get_grouplist sets |*out_group_ids| and |*out_group_ids_len| to the
304 * list of allowed group IDs. If |get_peer_groups| is non-zero, return the
305 * peer's group list. Otherwise, return the preferred list. */
306static void tls1_get_grouplist(SSL *ssl, int get_peer_groups,
307 const uint16_t **out_group_ids,
308 size_t *out_group_ids_len) {
309 if (get_peer_groups) {
310 /* Only clients send a supported group list, so this function is only
311 * called on the server. */
David Benjamin0d56f882015-12-19 17:05:56 -0500312 assert(ssl->server);
Steven Valdezce902a92016-05-17 11:47:53 -0400313 *out_group_ids = ssl->s3->tmp.peer_supported_group_list;
314 *out_group_ids_len = ssl->s3->tmp.peer_supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800315 return;
316 }
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Steven Valdezce902a92016-05-17 11:47:53 -0400318 *out_group_ids = ssl->supported_group_list;
319 *out_group_ids_len = ssl->supported_group_list_len;
320 if (!*out_group_ids) {
321 *out_group_ids = kDefaultGroups;
322 *out_group_ids_len = sizeof(kDefaultGroups) / sizeof(kDefaultGroups[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 }
324}
David Benjamined439582014-07-14 19:13:02 -0400325
Steven Valdezce902a92016-05-17 11:47:53 -0400326int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) {
327 const uint16_t *groups, *peer_groups, *pref, *supp;
328 size_t groups_len, peer_groups_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 /* Can't do anything on client side */
David Benjamin4298d772015-12-19 00:18:25 -0500331 if (ssl->server == 0) {
332 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 }
334
Steven Valdezce902a92016-05-17 11:47:53 -0400335 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
336 tls1_get_grouplist(ssl, 1 /* peer groups */, &peer_groups, &peer_groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800337
Steven Valdezce902a92016-05-17 11:47:53 -0400338 if (peer_groups_len == 0) {
339 /* Clients are not required to send a supported_groups extension. In this
340 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin4298d772015-12-19 00:18:25 -0500341 * section 4, paragraph 3.
342 *
343 * However, in the interests of compatibility, we will skip ECDH if the
344 * client didn't send an extension because we can't be sure that they'll
Steven Valdezce902a92016-05-17 11:47:53 -0400345 * support our favoured group. */
David Benjamin4298d772015-12-19 00:18:25 -0500346 return 0;
David Benjamin55a43642015-04-20 14:45:55 -0400347 }
348
David Benjamin4298d772015-12-19 00:18:25 -0500349 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400350 pref = groups;
351 pref_len = groups_len;
352 supp = peer_groups;
353 supp_len = peer_groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400354 } else {
Steven Valdezce902a92016-05-17 11:47:53 -0400355 pref = peer_groups;
356 pref_len = peer_groups_len;
357 supp = groups;
358 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400359 }
360
361 for (i = 0; i < pref_len; i++) {
362 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400364 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500365 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 }
367 }
368 }
369
David Benjamin4298d772015-12-19 00:18:25 -0500370 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800371}
Adam Langley95c29f32014-06-20 12:00:00 -0700372
Steven Valdezce902a92016-05-17 11:47:53 -0400373int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400375 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 size_t i;
377
Steven Valdezce902a92016-05-17 11:47:53 -0400378 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
379 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 return 0;
381 }
382
383 for (i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400384 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
385 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 return 0;
387 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 }
389
Steven Valdezce902a92016-05-17 11:47:53 -0400390 OPENSSL_free(*out_group_ids);
391 *out_group_ids = group_ids;
392 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800393
394 return 1;
395}
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Steven Valdezce902a92016-05-17 11:47:53 -0400397/* tls1_curve_params_from_ec_key sets |*out_group_id| and |*out_comp_id| to the
398 * TLS group ID and point format, respectively, for |ec|. It returns one on
David Benjamin072334d2014-07-13 16:24:27 -0400399 * success and zero on failure. */
Steven Valdezce902a92016-05-17 11:47:53 -0400400static int tls1_curve_params_from_ec_key(uint16_t *out_group_id,
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 uint8_t *out_comp_id, EC_KEY *ec) {
402 int nid;
403 uint16_t id;
404 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 if (ec == NULL) {
407 return 0;
408 }
Adam Langley95c29f32014-06-20 12:00:00 -0700409
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 grp = EC_KEY_get0_group(ec);
411 if (grp == NULL) {
412 return 0;
413 }
David Benjamin072334d2014-07-13 16:24:27 -0400414
Steven Valdezce902a92016-05-17 11:47:53 -0400415 /* Determine group ID */
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 nid = EC_GROUP_get_curve_name(grp);
Steven Valdezce902a92016-05-17 11:47:53 -0400417 if (!ssl_nid_to_group_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 return 0;
419 }
David Benjamin072334d2014-07-13 16:24:27 -0400420
Steven Valdezce902a92016-05-17 11:47:53 -0400421 /* Set the named group ID. Arbitrary explicit groups are not supported. */
422 *out_group_id = id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800423
424 if (out_comp_id) {
425 if (EC_KEY_get0_public_key(ec) == NULL) {
426 return 0;
427 }
428 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
429 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
430 } else {
431 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
432 }
433 }
434
435 return 1;
436}
David Benjamin072334d2014-07-13 16:24:27 -0400437
Steven Valdezce902a92016-05-17 11:47:53 -0400438/* tls1_check_group_id returns one if |group_id| is consistent with both our
439 * and the peer's group preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400440 * preferences are checked; the peer (the server) does not send preferences. */
Steven Valdezce902a92016-05-17 11:47:53 -0400441int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
442 const uint16_t *groups;
443 size_t groups_len, i, get_peer_groups;
David Benjamin42e9a772014-09-02 23:18:44 -0400444
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 /* Check against our list, then the peer's list. */
Steven Valdezce902a92016-05-17 11:47:53 -0400446 for (get_peer_groups = 0; get_peer_groups <= 1; get_peer_groups++) {
447 if (get_peer_groups && !ssl->server) {
David Benjamin55a43642015-04-20 14:45:55 -0400448 /* Servers do not present a preference list so, if we are a client, only
449 * check our list. */
450 continue;
451 }
452
Steven Valdezce902a92016-05-17 11:47:53 -0400453 tls1_get_grouplist(ssl, get_peer_groups, &groups, &groups_len);
454 if (get_peer_groups && groups_len == 0) {
455 /* Clients are not required to send a supported_groups extension. In this
456 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin55a43642015-04-20 14:45:55 -0400457 * section 4, paragraph 3. */
458 continue;
459 }
Steven Valdezce902a92016-05-17 11:47:53 -0400460 for (i = 0; i < groups_len; i++) {
461 if (groups[i] == group_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 break;
463 }
464 }
Adam Langley95c29f32014-06-20 12:00:00 -0700465
Steven Valdezce902a92016-05-17 11:47:53 -0400466 if (i == groups_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 return 0;
468 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 }
David Benjamin033e5f42014-11-13 18:47:41 -0500470
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 return 1;
472}
David Benjamin033e5f42014-11-13 18:47:41 -0500473
David Benjamin0d56f882015-12-19 17:05:56 -0500474int tls1_check_ec_cert(SSL *ssl, X509 *x) {
David Benjamin75ea5bb2016-07-08 17:43:29 -0700475 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
476 /* In TLS 1.3, the ECDSA curve is negotiated via signature algorithms. */
477 return 1;
478 }
479
Adam Langleyfcf25832014-12-18 17:42:32 -0800480 EVP_PKEY *pkey = X509_get_pubkey(x);
David Benjamin75ea5bb2016-07-08 17:43:29 -0700481 if (pkey == NULL) {
482 return 0;
483 }
484
485 int ret = 0;
Steven Valdezce902a92016-05-17 11:47:53 -0400486 uint16_t group_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800487 uint8_t comp_id;
David Benjamin758d1272015-11-20 17:47:25 -0500488 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
489 if (ec_key == NULL ||
Steven Valdezce902a92016-05-17 11:47:53 -0400490 !tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) ||
491 !tls1_check_group_id(ssl, group_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400492 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800493 goto done;
494 }
495
496 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500497
498done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400499 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800500 return ret;
501}
David Benjamin42e9a772014-09-02 23:18:44 -0400502
Adam Langley95c29f32014-06-20 12:00:00 -0700503/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800504 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700505
Steven Valdez02563852016-06-23 13:33:05 -0400506static const uint16_t kDefaultSignatureAlgorithms[] = {
507 SSL_SIGN_RSA_PKCS1_SHA512,
508 SSL_SIGN_ECDSA_SECP521R1_SHA512,
Adam Langley95c29f32014-06-20 12:00:00 -0700509
Steven Valdez02563852016-06-23 13:33:05 -0400510 SSL_SIGN_RSA_PKCS1_SHA384,
511 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Adam Langley95c29f32014-06-20 12:00:00 -0700512
Steven Valdez02563852016-06-23 13:33:05 -0400513 SSL_SIGN_RSA_PKCS1_SHA256,
514 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Adam Langley95c29f32014-06-20 12:00:00 -0700515
Steven Valdez02563852016-06-23 13:33:05 -0400516 SSL_SIGN_RSA_PKCS1_SHA1,
517 SSL_SIGN_ECDSA_SHA1,
Adam Langley95c29f32014-06-20 12:00:00 -0700518};
David Benjamin05da6e12014-07-12 20:42:55 -0400519
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400520static const uint16_t kDefaultTLS13SignatureAlgorithms[] = {
521 SSL_SIGN_RSA_PSS_SHA512,
522 SSL_SIGN_RSA_PKCS1_SHA512,
523 SSL_SIGN_ECDSA_SECP521R1_SHA512,
524
525 SSL_SIGN_RSA_PSS_SHA384,
526 SSL_SIGN_RSA_PKCS1_SHA384,
527 SSL_SIGN_ECDSA_SECP384R1_SHA384,
528
529 SSL_SIGN_RSA_PSS_SHA256,
530 SSL_SIGN_RSA_PKCS1_SHA256,
531 SSL_SIGN_ECDSA_SECP256R1_SHA256,
532
533 SSL_SIGN_RSA_PKCS1_SHA1,
534 SSL_SIGN_ECDSA_SHA1,
535};
536
Steven Valdez02563852016-06-23 13:33:05 -0400537size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400538 uint16_t version;
539 if (ssl->s3->have_version) {
540 version = ssl3_protocol_version(ssl);
541 } else {
542 version = ssl->method->version_from_wire(ssl->client_version);
543 }
544
545 if (version >= TLS1_3_VERSION) {
546 *psigs = kDefaultTLS13SignatureAlgorithms;
547 return sizeof(kDefaultTLS13SignatureAlgorithms) /
548 sizeof(kDefaultTLS13SignatureAlgorithms[0]);
549 }
550
Steven Valdez02563852016-06-23 13:33:05 -0400551 *psigs = kDefaultSignatureAlgorithms;
552 return sizeof(kDefaultSignatureAlgorithms) /
553 sizeof(kDefaultSignatureAlgorithms[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800554}
Adam Langley95c29f32014-06-20 12:00:00 -0700555
David Benjamin887c3002016-07-08 16:15:32 -0700556int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
Steven Valdez02563852016-06-23 13:33:05 -0400557 const uint16_t *sent_sigs;
558 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800559
Adam Langleyfcf25832014-12-18 17:42:32 -0800560 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500561 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400562 for (i = 0; i < sent_sigslen; i++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400563 if (sigalg == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800564 break;
565 }
566 }
567
David Benjamin788be4a2015-10-30 17:50:57 -0400568 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400569 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
571 return 0;
572 }
573
Adam Langleyfcf25832014-12-18 17:42:32 -0800574 return 1;
575}
576
577/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
578 * supported or doesn't appear in supported signature algorithms. Unlike
579 * ssl_cipher_get_disabled this applies to a specific session and not global
580 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500581void ssl_set_client_disabled(SSL *ssl) {
582 CERT *c = ssl->cert;
Steven Valdez02563852016-06-23 13:33:05 -0400583 const uint16_t *sigalgs;
Adam Langleyfcf25832014-12-18 17:42:32 -0800584 size_t i, sigalgslen;
585 int have_rsa = 0, have_ecdsa = 0;
586 c->mask_a = 0;
587 c->mask_k = 0;
588
Adam Langleyfcf25832014-12-18 17:42:32 -0800589 /* Now go through all signature algorithms seeing if we support any for RSA,
590 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500591 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -0400592 for (i = 0; i < sigalgslen; i++) {
593 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400594 case SSL_SIGN_RSA_PSS_SHA512:
595 case SSL_SIGN_RSA_PSS_SHA384:
596 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400597 case SSL_SIGN_RSA_PKCS1_SHA512:
598 case SSL_SIGN_RSA_PKCS1_SHA384:
599 case SSL_SIGN_RSA_PKCS1_SHA256:
600 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800601 have_rsa = 1;
602 break;
603
Steven Valdez02563852016-06-23 13:33:05 -0400604 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
605 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
606 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
607 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800608 have_ecdsa = 1;
609 break;
610 }
611 }
612
613 /* Disable auth if we don't include any appropriate signature algorithms. */
614 if (!have_rsa) {
615 c->mask_a |= SSL_aRSA;
616 }
617 if (!have_ecdsa) {
618 c->mask_a |= SSL_aECDSA;
619 }
620
621 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500622 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800623 c->mask_a |= SSL_aPSK;
624 c->mask_k |= SSL_kPSK;
625 }
626}
Adam Langley95c29f32014-06-20 12:00:00 -0700627
Adam Langley614c66a2015-06-12 15:26:58 -0700628/* tls_extension represents a TLS extension that is handled internally. The
629 * |init| function is called for each handshake, before any other functions of
630 * the extension. Then the add and parse callbacks are called as needed.
631 *
632 * The parse callbacks receive a |CBS| that contains the contents of the
633 * extension (i.e. not including the type and length bytes). If an extension is
634 * not received then the parse callbacks will be called with a NULL CBS so that
635 * they can do any processing needed to handle the absence of an extension.
636 *
637 * The add callbacks receive a |CBB| to which the extension can be appended but
638 * the function is responsible for appending the type and length bytes too.
639 *
640 * All callbacks return one for success and zero for error. If a parse function
641 * returns zero then a fatal alert with value |*out_alert| will be sent. If
642 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
643struct tls_extension {
644 uint16_t value;
645 void (*init)(SSL *ssl);
646
647 int (*add_clienthello)(SSL *ssl, CBB *out);
648 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
649
650 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
651 int (*add_serverhello)(SSL *ssl, CBB *out);
652};
653
Steven Valdez6b8509a2016-07-12 13:38:32 -0400654static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
655 if (contents != NULL) {
656 /* Servers MUST NOT send this extension. */
657 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
658 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
659 return 0;
660 }
661
662 return 1;
663}
664
665static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
666 /* This extension from the client is handled elsewhere. */
667 return 1;
668}
669
670static int dont_add_serverhello(SSL *ssl, CBB *out) {
671 return 1;
672}
Adam Langley614c66a2015-06-12 15:26:58 -0700673
674/* Server name indication (SNI).
675 *
676 * https://tools.ietf.org/html/rfc6066#section-3. */
677
678static void ext_sni_init(SSL *ssl) {
679 ssl->s3->tmp.should_ack_sni = 0;
680}
681
682static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
683 if (ssl->tlsext_hostname == NULL) {
684 return 1;
685 }
686
687 CBB contents, server_name_list, name;
688 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
689 !CBB_add_u16_length_prefixed(out, &contents) ||
690 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
691 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
692 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
693 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
694 strlen(ssl->tlsext_hostname)) ||
695 !CBB_flush(out)) {
696 return 0;
697 }
698
699 return 1;
700}
701
David Benjamin0d56f882015-12-19 17:05:56 -0500702static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
703 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700704 if (contents == NULL) {
705 return 1;
706 }
707
708 if (CBS_len(contents) != 0) {
709 return 0;
710 }
711
712 assert(ssl->tlsext_hostname != NULL);
713
714 if (!ssl->hit) {
715 assert(ssl->session->tlsext_hostname == NULL);
716 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
717 if (!ssl->session->tlsext_hostname) {
718 *out_alert = SSL_AD_INTERNAL_ERROR;
719 return 0;
720 }
721 }
722
723 return 1;
724}
725
David Benjamin0d56f882015-12-19 17:05:56 -0500726static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
727 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700728 if (contents == NULL) {
729 return 1;
730 }
731
David Benjamin9b611e22016-03-03 08:48:30 -0500732 CBS server_name_list, host_name;
733 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700734 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500735 !CBS_get_u8(&server_name_list, &name_type) ||
736 /* Although the server_name extension was intended to be extensible to
737 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
738 * different name types will cause an error. Further, RFC 4366 originally
739 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
740 * adding new name types is no longer feasible.
741 *
742 * Act as if the extensibility does not exist to simplify parsing. */
743 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
744 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700745 CBS_len(contents) != 0) {
746 return 0;
747 }
748
David Benjamin9b611e22016-03-03 08:48:30 -0500749 if (name_type != TLSEXT_NAMETYPE_host_name ||
750 CBS_len(&host_name) == 0 ||
751 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
752 CBS_contains_zero_byte(&host_name)) {
753 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
754 return 0;
755 }
Adam Langley614c66a2015-06-12 15:26:58 -0700756
David Benjamin9b611e22016-03-03 08:48:30 -0500757 /* TODO(davidben): SNI should be resolved before resumption. We have the
758 * early callback as a replacement, but we should fix the current callback
759 * and avoid the need for |SSL_CTX_set_session_id_context|. */
760 if (!ssl->hit) {
761 assert(ssl->session->tlsext_hostname == NULL);
762
763 /* Copy the hostname as a string. */
764 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
765 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700766 return 0;
767 }
768
David Benjamin9b611e22016-03-03 08:48:30 -0500769 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700770 }
771
772 return 1;
773}
774
775static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
776 if (ssl->hit ||
777 !ssl->s3->tmp.should_ack_sni ||
778 ssl->session->tlsext_hostname == NULL) {
779 return 1;
780 }
781
782 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
783 !CBB_add_u16(out, 0 /* length */)) {
784 return 0;
785 }
786
787 return 1;
788}
789
790
Adam Langley5021b222015-06-12 18:27:58 -0700791/* Renegotiation indication.
792 *
793 * https://tools.ietf.org/html/rfc5746 */
794
795static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
796 CBB contents, prev_finished;
797 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
798 !CBB_add_u16_length_prefixed(out, &contents) ||
799 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
800 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
801 ssl->s3->previous_client_finished_len) ||
802 !CBB_flush(out)) {
803 return 0;
804 }
805
806 return 1;
807}
808
809static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
810 CBS *contents) {
David Benjamin3e052de2015-11-25 20:10:31 -0500811 /* Servers may not switch between omitting the extension and supporting it.
812 * See RFC 5746, sections 3.5 and 4.2. */
813 if (ssl->s3->initial_handshake_complete &&
814 (contents != NULL) != ssl->s3->send_connection_binding) {
815 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
816 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
817 return 0;
818 }
819
Adam Langley5021b222015-06-12 18:27:58 -0700820 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500821 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700822 * RI even on initial ServerHello because the client doesn't see any
823 * renegotiation during an attack. However this would mean we could not
824 * connect to any server which doesn't support RI.
825 *
David Benjamine9cddb82015-11-23 14:36:40 -0500826 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
827 * practical terms every client sets it so it's just assumed here. */
828 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700829 }
830
831 const size_t expected_len = ssl->s3->previous_client_finished_len +
832 ssl->s3->previous_server_finished_len;
833
834 /* Check for logic errors */
835 assert(!expected_len || ssl->s3->previous_client_finished_len);
836 assert(!expected_len || ssl->s3->previous_server_finished_len);
837
838 /* Parse out the extension contents. */
839 CBS renegotiated_connection;
840 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
841 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400842 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700843 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
844 return 0;
845 }
846
847 /* Check that the extension matches. */
848 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400849 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700850 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
851 return 0;
852 }
853
854 const uint8_t *d = CBS_data(&renegotiated_connection);
855 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
856 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400857 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700858 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
859 return 0;
860 }
861 d += ssl->s3->previous_client_finished_len;
862
863 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
864 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400865 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700866 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
867 return 0;
868 }
869 ssl->s3->send_connection_binding = 1;
870
871 return 1;
872}
873
874static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
875 CBS *contents) {
876 /* Renegotiation isn't supported as a server so this function should never be
877 * called after the initial handshake. */
878 assert(!ssl->s3->initial_handshake_complete);
879
880 CBS fake_contents;
881 static const uint8_t kFakeExtension[] = {0};
882
883 if (contents == NULL) {
884 if (ssl->s3->send_connection_binding) {
885 /* The renegotiation SCSV was received so pretend that we received a
886 * renegotiation extension. */
887 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
888 contents = &fake_contents;
889 /* We require that the renegotiation extension is at index zero of
890 * kExtensions. */
891 ssl->s3->tmp.extensions.received |= (1u << 0);
892 } else {
893 return 1;
894 }
895 }
896
897 CBS renegotiated_connection;
898
899 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
900 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400901 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700902 return 0;
903 }
904
905 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500906 if (!CBS_mem_equal(&renegotiated_connection,
907 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700908 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400909 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700910 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
911 return 0;
912 }
913
914 ssl->s3->send_connection_binding = 1;
915
916 return 1;
917}
918
919static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
920 CBB contents, prev_finished;
921 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
922 !CBB_add_u16_length_prefixed(out, &contents) ||
923 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
924 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
925 ssl->s3->previous_client_finished_len) ||
926 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
927 ssl->s3->previous_server_finished_len) ||
928 !CBB_flush(out)) {
929 return 0;
930 }
931
932 return 1;
933}
934
Adam Langley0a056712015-07-01 15:03:33 -0700935
936/* Extended Master Secret.
937 *
David Benjamin43946d42016-02-01 08:42:19 -0500938 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700939
940static void ext_ems_init(SSL *ssl) {
941 ssl->s3->tmp.extended_master_secret = 0;
942}
943
944static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
945 if (ssl->version == SSL3_VERSION) {
946 return 1;
947 }
948
949 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
950 !CBB_add_u16(out, 0 /* length */)) {
951 return 0;
952 }
953
954 return 1;
955}
956
957static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
958 CBS *contents) {
959 if (contents == NULL) {
960 return 1;
961 }
962
963 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
964 return 0;
965 }
966
967 ssl->s3->tmp.extended_master_secret = 1;
968 return 1;
969}
970
David Benjamin0d56f882015-12-19 17:05:56 -0500971static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
972 CBS *contents) {
Adam Langley0a056712015-07-01 15:03:33 -0700973 if (ssl->version == SSL3_VERSION || contents == NULL) {
974 return 1;
975 }
976
977 if (CBS_len(contents) != 0) {
978 return 0;
979 }
980
981 ssl->s3->tmp.extended_master_secret = 1;
982 return 1;
983}
984
985static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
986 if (!ssl->s3->tmp.extended_master_secret) {
987 return 1;
988 }
989
990 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
991 !CBB_add_u16(out, 0 /* length */)) {
992 return 0;
993 }
994
995 return 1;
996}
997
Adam Langley9b05bc52015-07-01 15:25:33 -0700998
999/* Session tickets.
1000 *
1001 * https://tools.ietf.org/html/rfc5077 */
1002
1003static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1004 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1005 return 1;
1006 }
1007
1008 const uint8_t *ticket_data = NULL;
1009 int ticket_len = 0;
1010
1011 /* Renegotiation does not participate in session resumption. However, still
1012 * advertise the extension to avoid potentially breaking servers which carry
1013 * over the state from the previous handshake, such as OpenSSL servers
1014 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1015 if (!ssl->s3->initial_handshake_complete &&
1016 ssl->session != NULL &&
1017 ssl->session->tlsext_tick != NULL) {
1018 ticket_data = ssl->session->tlsext_tick;
1019 ticket_len = ssl->session->tlsext_ticklen;
1020 }
1021
1022 CBB ticket;
1023 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1024 !CBB_add_u16_length_prefixed(out, &ticket) ||
1025 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1026 !CBB_flush(out)) {
1027 return 0;
1028 }
1029
1030 return 1;
1031}
1032
1033static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1034 CBS *contents) {
1035 ssl->tlsext_ticket_expected = 0;
1036
1037 if (contents == NULL) {
1038 return 1;
1039 }
1040
1041 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1042 * this function should never be called, even if the server tries to send the
1043 * extension. */
1044 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1045
1046 if (CBS_len(contents) != 0) {
1047 return 0;
1048 }
1049
1050 ssl->tlsext_ticket_expected = 1;
1051 return 1;
1052}
1053
Adam Langley9b05bc52015-07-01 15:25:33 -07001054static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1055 if (!ssl->tlsext_ticket_expected) {
1056 return 1;
1057 }
1058
1059 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1060 * true. */
1061 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1062
1063 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1064 !CBB_add_u16(out, 0 /* length */)) {
1065 return 0;
1066 }
1067
1068 return 1;
1069}
1070
1071
Adam Langley2e857bd2015-07-01 16:09:19 -07001072/* Signature Algorithms.
1073 *
1074 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1075
1076static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjaminb6a0a512016-06-21 10:33:21 -04001077 if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001078 return 1;
1079 }
1080
Steven Valdez02563852016-06-23 13:33:05 -04001081 const uint16_t *sigalgs_data;
Adam Langley2e857bd2015-07-01 16:09:19 -07001082 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1083
1084 CBB contents, sigalgs;
1085 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1086 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez02563852016-06-23 13:33:05 -04001087 !CBB_add_u16_length_prefixed(&contents, &sigalgs)) {
1088 return 0;
1089 }
1090
1091 size_t i;
1092 for (i = 0; i < sigalgs_len; i++) {
1093 if (!CBB_add_u16(&sigalgs, sigalgs_data[i])) {
1094 return 0;
1095 }
1096 }
1097
1098 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001099 return 0;
1100 }
1101
1102 return 1;
1103}
1104
Adam Langley2e857bd2015-07-01 16:09:19 -07001105static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1106 CBS *contents) {
1107 OPENSSL_free(ssl->cert->peer_sigalgs);
1108 ssl->cert->peer_sigalgs = NULL;
1109 ssl->cert->peer_sigalgslen = 0;
1110
Adam Langley2e857bd2015-07-01 16:09:19 -07001111 if (contents == NULL) {
1112 return 1;
1113 }
1114
1115 CBS supported_signature_algorithms;
1116 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001117 CBS_len(contents) != 0 ||
1118 CBS_len(&supported_signature_algorithms) == 0 ||
1119 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001120 return 0;
1121 }
1122
1123 return 1;
1124}
1125
Adam Langley2e857bd2015-07-01 16:09:19 -07001126
Adam Langleybb0bd042015-07-01 16:21:03 -07001127/* OCSP Stapling.
1128 *
1129 * https://tools.ietf.org/html/rfc6066#section-8 */
1130
1131static void ext_ocsp_init(SSL *ssl) {
1132 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001133 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001134}
1135
1136static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1137 if (!ssl->ocsp_stapling_enabled) {
1138 return 1;
1139 }
1140
1141 CBB contents;
1142 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1143 !CBB_add_u16_length_prefixed(out, &contents) ||
1144 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1145 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1146 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1147 !CBB_flush(out)) {
1148 return 0;
1149 }
1150
Adam Langleyce9d85e2016-01-24 15:58:39 -08001151 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001152 return 1;
1153}
1154
1155static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001156 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001157 if (contents == NULL) {
1158 return 1;
1159 }
1160
1161 if (CBS_len(contents) != 0) {
1162 return 0;
1163 }
1164
1165 ssl->s3->tmp.certificate_status_expected = 1;
1166 return 1;
1167}
1168
1169static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1170 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001171 if (contents == NULL) {
1172 return 1;
1173 }
1174
1175 uint8_t status_type;
1176 if (!CBS_get_u8(contents, &status_type)) {
1177 return 0;
1178 }
1179
1180 /* We cannot decide whether OCSP stapling will occur yet because the correct
1181 * SSL_CTX might not have been selected. */
1182 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1183
Adam Langleybb0bd042015-07-01 16:21:03 -07001184 return 1;
1185}
1186
1187static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001188 /* The extension shouldn't be sent when resuming sessions. */
1189 if (ssl->hit ||
1190 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001191 ssl->ctx->ocsp_response_length == 0) {
1192 return 1;
1193 }
1194
1195 ssl->s3->tmp.certificate_status_expected = 1;
1196
1197 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1198 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001199}
1200
1201
Adam Langley97dfcbf2015-07-01 18:35:20 -07001202/* Next protocol negotiation.
1203 *
1204 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1205
1206static void ext_npn_init(SSL *ssl) {
1207 ssl->s3->next_proto_neg_seen = 0;
1208}
1209
1210static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1211 if (ssl->s3->initial_handshake_complete ||
1212 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001213 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001214 SSL_IS_DTLS(ssl)) {
1215 return 1;
1216 }
1217
1218 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1219 !CBB_add_u16(out, 0 /* length */)) {
1220 return 0;
1221 }
1222
1223 return 1;
1224}
1225
1226static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1227 CBS *contents) {
1228 if (contents == NULL) {
1229 return 1;
1230 }
1231
1232 /* If any of these are false then we should never have sent the NPN
1233 * extension in the ClientHello and thus this function should never have been
1234 * called. */
1235 assert(!ssl->s3->initial_handshake_complete);
1236 assert(!SSL_IS_DTLS(ssl));
1237 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001238 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001239
David Benjamin76c2efc2015-08-31 14:24:29 -04001240 if (ssl->s3->alpn_selected != NULL) {
1241 /* NPN and ALPN may not be negotiated in the same connection. */
1242 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1243 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1244 return 0;
1245 }
1246
Adam Langley97dfcbf2015-07-01 18:35:20 -07001247 const uint8_t *const orig_contents = CBS_data(contents);
1248 const size_t orig_len = CBS_len(contents);
1249
1250 while (CBS_len(contents) != 0) {
1251 CBS proto;
1252 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1253 CBS_len(&proto) == 0) {
1254 return 0;
1255 }
1256 }
1257
1258 uint8_t *selected;
1259 uint8_t selected_len;
1260 if (ssl->ctx->next_proto_select_cb(
1261 ssl, &selected, &selected_len, orig_contents, orig_len,
1262 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1263 *out_alert = SSL_AD_INTERNAL_ERROR;
1264 return 0;
1265 }
1266
David Benjamin79978df2015-12-25 15:56:49 -05001267 OPENSSL_free(ssl->s3->next_proto_negotiated);
1268 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1269 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001270 *out_alert = SSL_AD_INTERNAL_ERROR;
1271 return 0;
1272 }
1273
David Benjamin79978df2015-12-25 15:56:49 -05001274 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001275 ssl->s3->next_proto_neg_seen = 1;
1276
1277 return 1;
1278}
1279
1280static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1281 CBS *contents) {
1282 if (contents != NULL && CBS_len(contents) != 0) {
1283 return 0;
1284 }
1285
1286 if (contents == NULL ||
1287 ssl->s3->initial_handshake_complete ||
1288 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1289 * afterwards, parsing the ALPN extension will clear
1290 * |next_proto_neg_seen|. */
1291 ssl->s3->alpn_selected != NULL ||
1292 ssl->ctx->next_protos_advertised_cb == NULL ||
1293 SSL_IS_DTLS(ssl)) {
1294 return 1;
1295 }
1296
1297 ssl->s3->next_proto_neg_seen = 1;
1298 return 1;
1299}
1300
1301static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1302 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1303 * parsed. */
1304 if (!ssl->s3->next_proto_neg_seen) {
1305 return 1;
1306 }
1307
1308 const uint8_t *npa;
1309 unsigned npa_len;
1310
1311 if (ssl->ctx->next_protos_advertised_cb(
1312 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1313 SSL_TLSEXT_ERR_OK) {
1314 ssl->s3->next_proto_neg_seen = 0;
1315 return 1;
1316 }
1317
1318 CBB contents;
1319 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1320 !CBB_add_u16_length_prefixed(out, &contents) ||
1321 !CBB_add_bytes(&contents, npa, npa_len) ||
1322 !CBB_flush(out)) {
1323 return 0;
1324 }
1325
1326 return 1;
1327}
1328
1329
Adam Langleyab8d87d2015-07-10 12:21:39 -07001330/* Signed certificate timestamps.
1331 *
1332 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1333
1334static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1335 if (!ssl->signed_cert_timestamps_enabled) {
1336 return 1;
1337 }
1338
1339 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1340 !CBB_add_u16(out, 0 /* length */)) {
1341 return 0;
1342 }
1343
1344 return 1;
1345}
1346
1347static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1348 CBS *contents) {
1349 if (contents == NULL) {
1350 return 1;
1351 }
1352
1353 /* If this is false then we should never have sent the SCT extension in the
1354 * ClientHello and thus this function should never have been called. */
1355 assert(ssl->signed_cert_timestamps_enabled);
1356
1357 if (CBS_len(contents) == 0) {
1358 *out_alert = SSL_AD_DECODE_ERROR;
1359 return 0;
1360 }
1361
1362 /* Session resumption uses the original session information. */
1363 if (!ssl->hit &&
1364 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1365 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1366 *out_alert = SSL_AD_INTERNAL_ERROR;
1367 return 0;
1368 }
1369
1370 return 1;
1371}
1372
1373static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1374 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001375 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001376}
1377
1378static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001379 /* The extension shouldn't be sent when resuming sessions. */
1380 if (ssl->hit ||
1381 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001382 return 1;
1383 }
1384
1385 CBB contents;
1386 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1387 CBB_add_u16_length_prefixed(out, &contents) &&
1388 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1389 ssl->ctx->signed_cert_timestamp_list_length) &&
1390 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001391}
1392
1393
Adam Langleyf18e4532015-07-10 13:39:53 -07001394/* Application-level Protocol Negotiation.
1395 *
1396 * https://tools.ietf.org/html/rfc7301 */
1397
1398static void ext_alpn_init(SSL *ssl) {
1399 OPENSSL_free(ssl->s3->alpn_selected);
1400 ssl->s3->alpn_selected = NULL;
1401}
1402
1403static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1404 if (ssl->alpn_client_proto_list == NULL ||
1405 ssl->s3->initial_handshake_complete) {
1406 return 1;
1407 }
1408
1409 CBB contents, proto_list;
1410 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1411 !CBB_add_u16_length_prefixed(out, &contents) ||
1412 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1413 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1414 ssl->alpn_client_proto_list_len) ||
1415 !CBB_flush(out)) {
1416 return 0;
1417 }
1418
1419 return 1;
1420}
1421
1422static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1423 CBS *contents) {
1424 if (contents == NULL) {
1425 return 1;
1426 }
1427
1428 assert(!ssl->s3->initial_handshake_complete);
1429 assert(ssl->alpn_client_proto_list != NULL);
1430
David Benjamin76c2efc2015-08-31 14:24:29 -04001431 if (ssl->s3->next_proto_neg_seen) {
1432 /* NPN and ALPN may not be negotiated in the same connection. */
1433 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1434 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1435 return 0;
1436 }
1437
Adam Langleyf18e4532015-07-10 13:39:53 -07001438 /* The extension data consists of a ProtocolNameList which must have
1439 * exactly one ProtocolName. Each of these is length-prefixed. */
1440 CBS protocol_name_list, protocol_name;
1441 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1442 CBS_len(contents) != 0 ||
1443 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1444 /* Empty protocol names are forbidden. */
1445 CBS_len(&protocol_name) == 0 ||
1446 CBS_len(&protocol_name_list) != 0) {
1447 return 0;
1448 }
1449
1450 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1451 &ssl->s3->alpn_selected_len)) {
1452 *out_alert = SSL_AD_INTERNAL_ERROR;
1453 return 0;
1454 }
1455
1456 return 1;
1457}
1458
1459static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1460 CBS *contents) {
1461 if (contents == NULL) {
1462 return 1;
1463 }
1464
1465 if (ssl->ctx->alpn_select_cb == NULL ||
1466 ssl->s3->initial_handshake_complete) {
1467 return 1;
1468 }
1469
1470 /* ALPN takes precedence over NPN. */
1471 ssl->s3->next_proto_neg_seen = 0;
1472
1473 CBS protocol_name_list;
1474 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1475 CBS_len(contents) != 0 ||
1476 CBS_len(&protocol_name_list) < 2) {
1477 return 0;
1478 }
1479
1480 /* Validate the protocol list. */
1481 CBS protocol_name_list_copy = protocol_name_list;
1482 while (CBS_len(&protocol_name_list_copy) > 0) {
1483 CBS protocol_name;
1484
1485 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1486 /* Empty protocol names are forbidden. */
1487 CBS_len(&protocol_name) == 0) {
1488 return 0;
1489 }
1490 }
1491
1492 const uint8_t *selected;
1493 uint8_t selected_len;
1494 if (ssl->ctx->alpn_select_cb(
1495 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1496 CBS_len(&protocol_name_list),
1497 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1498 OPENSSL_free(ssl->s3->alpn_selected);
1499 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1500 if (ssl->s3->alpn_selected == NULL) {
1501 *out_alert = SSL_AD_INTERNAL_ERROR;
1502 return 0;
1503 }
1504 ssl->s3->alpn_selected_len = selected_len;
1505 }
1506
1507 return 1;
1508}
1509
1510static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1511 if (ssl->s3->alpn_selected == NULL) {
1512 return 1;
1513 }
1514
1515 CBB contents, proto_list, proto;
1516 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1517 !CBB_add_u16_length_prefixed(out, &contents) ||
1518 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1519 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001520 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1521 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001522 !CBB_flush(out)) {
1523 return 0;
1524 }
1525
1526 return 1;
1527}
1528
1529
Adam Langley49c7af12015-07-10 14:33:46 -07001530/* Channel ID.
1531 *
1532 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1533
1534static void ext_channel_id_init(SSL *ssl) {
1535 ssl->s3->tlsext_channel_id_valid = 0;
1536}
1537
1538static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1539 if (!ssl->tlsext_channel_id_enabled ||
1540 SSL_IS_DTLS(ssl)) {
1541 return 1;
1542 }
1543
1544 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1545 !CBB_add_u16(out, 0 /* length */)) {
1546 return 0;
1547 }
1548
1549 return 1;
1550}
1551
1552static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1553 CBS *contents) {
1554 if (contents == NULL) {
1555 return 1;
1556 }
1557
1558 assert(!SSL_IS_DTLS(ssl));
1559 assert(ssl->tlsext_channel_id_enabled);
1560
1561 if (CBS_len(contents) != 0) {
1562 return 0;
1563 }
1564
1565 ssl->s3->tlsext_channel_id_valid = 1;
1566 return 1;
1567}
1568
1569static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1570 CBS *contents) {
1571 if (contents == NULL ||
1572 !ssl->tlsext_channel_id_enabled ||
1573 SSL_IS_DTLS(ssl)) {
1574 return 1;
1575 }
1576
1577 if (CBS_len(contents) != 0) {
1578 return 0;
1579 }
1580
1581 ssl->s3->tlsext_channel_id_valid = 1;
1582 return 1;
1583}
1584
1585static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1586 if (!ssl->s3->tlsext_channel_id_valid) {
1587 return 1;
1588 }
1589
1590 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1591 !CBB_add_u16(out, 0 /* length */)) {
1592 return 0;
1593 }
1594
1595 return 1;
1596}
1597
Adam Langley391250d2015-07-15 19:06:07 -07001598
1599/* Secure Real-time Transport Protocol (SRTP) extension.
1600 *
1601 * https://tools.ietf.org/html/rfc5764 */
1602
Adam Langley391250d2015-07-15 19:06:07 -07001603
1604static void ext_srtp_init(SSL *ssl) {
1605 ssl->srtp_profile = NULL;
1606}
1607
1608static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1609 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1610 if (profiles == NULL) {
1611 return 1;
1612 }
1613 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1614 if (num_profiles == 0) {
1615 return 1;
1616 }
1617
1618 CBB contents, profile_ids;
1619 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1620 !CBB_add_u16_length_prefixed(out, &contents) ||
1621 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1622 return 0;
1623 }
1624
1625 size_t i;
1626 for (i = 0; i < num_profiles; i++) {
1627 if (!CBB_add_u16(&profile_ids,
1628 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1629 return 0;
1630 }
1631 }
1632
1633 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1634 !CBB_flush(out)) {
1635 return 0;
1636 }
1637
1638 return 1;
1639}
1640
1641static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1642 CBS *contents) {
1643 if (contents == NULL) {
1644 return 1;
1645 }
1646
1647 /* The extension consists of a u16-prefixed profile ID list containing a
1648 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1649 *
1650 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1651 CBS profile_ids, srtp_mki;
1652 uint16_t profile_id;
1653 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1654 !CBS_get_u16(&profile_ids, &profile_id) ||
1655 CBS_len(&profile_ids) != 0 ||
1656 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1657 CBS_len(contents) != 0) {
1658 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1659 return 0;
1660 }
1661
1662 if (CBS_len(&srtp_mki) != 0) {
1663 /* Must be no MKI, since we never offer one. */
1664 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1665 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1666 return 0;
1667 }
1668
1669 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1670
1671 /* Check to see if the server gave us something we support (and presumably
1672 * offered). */
1673 size_t i;
1674 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1675 const SRTP_PROTECTION_PROFILE *profile =
1676 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1677
1678 if (profile->id == profile_id) {
1679 ssl->srtp_profile = profile;
1680 return 1;
1681 }
1682 }
1683
1684 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1685 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1686 return 0;
1687}
1688
1689static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1690 CBS *contents) {
1691 if (contents == NULL) {
1692 return 1;
1693 }
1694
1695 CBS profile_ids, srtp_mki;
1696 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1697 CBS_len(&profile_ids) < 2 ||
1698 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1699 CBS_len(contents) != 0) {
1700 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1701 return 0;
1702 }
1703 /* Discard the MKI value for now. */
1704
1705 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1706 SSL_get_srtp_profiles(ssl);
1707
1708 /* Pick the server's most preferred profile. */
1709 size_t i;
1710 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1711 const SRTP_PROTECTION_PROFILE *server_profile =
1712 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1713
1714 CBS profile_ids_tmp;
1715 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1716
1717 while (CBS_len(&profile_ids_tmp) > 0) {
1718 uint16_t profile_id;
1719 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1720 return 0;
1721 }
1722
1723 if (server_profile->id == profile_id) {
1724 ssl->srtp_profile = server_profile;
1725 return 1;
1726 }
1727 }
1728 }
1729
1730 return 1;
1731}
1732
1733static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1734 if (ssl->srtp_profile == NULL) {
1735 return 1;
1736 }
1737
1738 CBB contents, profile_ids;
1739 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1740 !CBB_add_u16_length_prefixed(out, &contents) ||
1741 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1742 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1743 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1744 !CBB_flush(out)) {
1745 return 0;
1746 }
1747
1748 return 1;
1749}
1750
Adam Langleybdd5d662015-07-20 16:19:08 -07001751
1752/* EC point formats.
1753 *
1754 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1755
1756static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1757 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1758 return 0;
1759 }
1760
1761 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1762
1763 size_t i;
1764 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1765 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1766
1767 const uint32_t alg_k = cipher->algorithm_mkey;
1768 const uint32_t alg_a = cipher->algorithm_auth;
1769 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1770 return 1;
1771 }
1772 }
1773
1774 return 0;
1775}
1776
Adam Langleybdd5d662015-07-20 16:19:08 -07001777static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001778 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001779 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1780 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001781 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1782 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001783 !CBB_flush(out)) {
1784 return 0;
1785 }
1786
1787 return 1;
1788}
1789
1790static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1791 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1792 return 1;
1793 }
1794
1795 return ext_ec_point_add_extension(ssl, out);
1796}
1797
1798static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1799 CBS *contents) {
1800 if (contents == NULL) {
1801 return 1;
1802 }
1803
1804 CBS ec_point_format_list;
1805 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1806 CBS_len(contents) != 0) {
1807 return 0;
1808 }
1809
David Benjaminfc059942015-07-30 23:01:59 -04001810 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1811 * point format. */
1812 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1813 CBS_len(&ec_point_format_list)) == NULL) {
1814 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001815 return 0;
1816 }
1817
1818 return 1;
1819}
1820
1821static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1822 CBS *contents) {
1823 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1824}
1825
1826static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1827 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1828 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001829 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001830
1831 if (!using_ecc) {
1832 return 1;
1833 }
1834
1835 return ext_ec_point_add_extension(ssl, out);
1836}
1837
Adam Langley273d49c2015-07-20 16:38:52 -07001838
Steven Valdezce902a92016-05-17 11:47:53 -04001839/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07001840 *
Steven Valdezce902a92016-05-17 11:47:53 -04001841 * https://tools.ietf.org/html/rfc4492#section-5.1.2
1842 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07001843
Steven Valdezce902a92016-05-17 11:47:53 -04001844static void ext_supported_groups_init(SSL *ssl) {
1845 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
1846 ssl->s3->tmp.peer_supported_group_list = NULL;
1847 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07001848}
1849
Steven Valdezce902a92016-05-17 11:47:53 -04001850static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07001851 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1852 return 1;
1853 }
1854
Steven Valdezce902a92016-05-17 11:47:53 -04001855 CBB contents, groups_bytes;
1856 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07001857 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001858 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07001859 return 0;
1860 }
1861
Steven Valdezce902a92016-05-17 11:47:53 -04001862 const uint16_t *groups;
1863 size_t groups_len;
1864 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07001865
1866 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04001867 for (i = 0; i < groups_len; i++) {
1868 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07001869 return 0;
1870 }
1871 }
1872
1873 return CBB_flush(out);
1874}
1875
Steven Valdezce902a92016-05-17 11:47:53 -04001876static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1877 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07001878 /* This extension is not expected to be echoed by servers and is ignored. */
1879 return 1;
1880}
1881
Steven Valdezce902a92016-05-17 11:47:53 -04001882static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1883 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07001884 if (contents == NULL) {
1885 return 1;
1886 }
1887
Steven Valdezce902a92016-05-17 11:47:53 -04001888 CBS supported_group_list;
1889 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
1890 CBS_len(&supported_group_list) == 0 ||
1891 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07001892 CBS_len(contents) != 0) {
1893 return 0;
1894 }
1895
Steven Valdezce902a92016-05-17 11:47:53 -04001896 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
1897 CBS_len(&supported_group_list));
1898 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07001899 *out_alert = SSL_AD_INTERNAL_ERROR;
1900 return 0;
1901 }
1902
Steven Valdezce902a92016-05-17 11:47:53 -04001903 const size_t num_groups = CBS_len(&supported_group_list) / 2;
Adam Langley273d49c2015-07-20 16:38:52 -07001904 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04001905 for (i = 0; i < num_groups; i++) {
1906 if (!CBS_get_u16(&supported_group_list,
1907 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07001908 goto err;
1909 }
1910 }
1911
Steven Valdezce902a92016-05-17 11:47:53 -04001912 assert(CBS_len(&supported_group_list) == 0);
1913 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07001914
1915 return 1;
1916
1917err:
Steven Valdezce902a92016-05-17 11:47:53 -04001918 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
1919 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07001920 *out_alert = SSL_AD_INTERNAL_ERROR;
1921 return 0;
1922}
1923
Steven Valdezce902a92016-05-17 11:47:53 -04001924static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07001925 /* Servers don't echo this extension. */
1926 return 1;
1927}
1928
1929
Adam Langley614c66a2015-06-12 15:26:58 -07001930/* kExtensions contains all the supported extensions. */
1931static const struct tls_extension kExtensions[] = {
1932 {
Adam Langley5021b222015-06-12 18:27:58 -07001933 /* The renegotiation extension must always be at index zero because the
1934 * |received| and |sent| bitsets need to be tweaked when the "extension" is
1935 * sent as an SCSV. */
1936 TLSEXT_TYPE_renegotiate,
1937 NULL,
1938 ext_ri_add_clienthello,
1939 ext_ri_parse_serverhello,
1940 ext_ri_parse_clienthello,
1941 ext_ri_add_serverhello,
1942 },
1943 {
Adam Langley614c66a2015-06-12 15:26:58 -07001944 TLSEXT_TYPE_server_name,
1945 ext_sni_init,
1946 ext_sni_add_clienthello,
1947 ext_sni_parse_serverhello,
1948 ext_sni_parse_clienthello,
1949 ext_sni_add_serverhello,
1950 },
Adam Langley0a056712015-07-01 15:03:33 -07001951 {
1952 TLSEXT_TYPE_extended_master_secret,
1953 ext_ems_init,
1954 ext_ems_add_clienthello,
1955 ext_ems_parse_serverhello,
1956 ext_ems_parse_clienthello,
1957 ext_ems_add_serverhello,
1958 },
Adam Langley9b05bc52015-07-01 15:25:33 -07001959 {
1960 TLSEXT_TYPE_session_ticket,
1961 NULL,
1962 ext_ticket_add_clienthello,
1963 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04001964 /* Ticket extension client parsing is handled in ssl_session.c */
1965 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07001966 ext_ticket_add_serverhello,
1967 },
Adam Langley2e857bd2015-07-01 16:09:19 -07001968 {
1969 TLSEXT_TYPE_signature_algorithms,
1970 NULL,
1971 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04001972 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07001973 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04001974 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07001975 },
Adam Langleybb0bd042015-07-01 16:21:03 -07001976 {
1977 TLSEXT_TYPE_status_request,
1978 ext_ocsp_init,
1979 ext_ocsp_add_clienthello,
1980 ext_ocsp_parse_serverhello,
1981 ext_ocsp_parse_clienthello,
1982 ext_ocsp_add_serverhello,
1983 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07001984 {
1985 TLSEXT_TYPE_next_proto_neg,
1986 ext_npn_init,
1987 ext_npn_add_clienthello,
1988 ext_npn_parse_serverhello,
1989 ext_npn_parse_clienthello,
1990 ext_npn_add_serverhello,
1991 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07001992 {
1993 TLSEXT_TYPE_certificate_timestamp,
1994 NULL,
1995 ext_sct_add_clienthello,
1996 ext_sct_parse_serverhello,
1997 ext_sct_parse_clienthello,
1998 ext_sct_add_serverhello,
1999 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002000 {
2001 TLSEXT_TYPE_application_layer_protocol_negotiation,
2002 ext_alpn_init,
2003 ext_alpn_add_clienthello,
2004 ext_alpn_parse_serverhello,
2005 ext_alpn_parse_clienthello,
2006 ext_alpn_add_serverhello,
2007 },
Adam Langley49c7af12015-07-10 14:33:46 -07002008 {
2009 TLSEXT_TYPE_channel_id,
2010 ext_channel_id_init,
2011 ext_channel_id_add_clienthello,
2012 ext_channel_id_parse_serverhello,
2013 ext_channel_id_parse_clienthello,
2014 ext_channel_id_add_serverhello,
2015 },
Adam Langley391250d2015-07-15 19:06:07 -07002016 {
2017 TLSEXT_TYPE_srtp,
2018 ext_srtp_init,
2019 ext_srtp_add_clienthello,
2020 ext_srtp_parse_serverhello,
2021 ext_srtp_parse_clienthello,
2022 ext_srtp_add_serverhello,
2023 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002024 {
2025 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002026 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002027 ext_ec_point_add_clienthello,
2028 ext_ec_point_parse_serverhello,
2029 ext_ec_point_parse_clienthello,
2030 ext_ec_point_add_serverhello,
2031 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002032 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2033 * intolerant to the last extension being zero-length. See
2034 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002035 {
Steven Valdezce902a92016-05-17 11:47:53 -04002036 TLSEXT_TYPE_supported_groups,
2037 ext_supported_groups_init,
2038 ext_supported_groups_add_clienthello,
2039 ext_supported_groups_parse_serverhello,
2040 ext_supported_groups_parse_clienthello,
2041 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002042 },
Adam Langley614c66a2015-06-12 15:26:58 -07002043};
2044
2045#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2046
Adam Langley4cfa96b2015-07-01 11:56:55 -07002047OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2048 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002049 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002050OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2051 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2052 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002053 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002054
Adam Langley614c66a2015-06-12 15:26:58 -07002055static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2056 uint16_t value) {
2057 unsigned i;
2058 for (i = 0; i < kNumExtensions; i++) {
2059 if (kExtensions[i].value == value) {
2060 *out_index = i;
2061 return &kExtensions[i];
2062 }
2063 }
2064
2065 return NULL;
2066}
2067
Adam Langley09505632015-07-30 18:10:13 -07002068int SSL_extension_supported(unsigned extension_value) {
2069 uint32_t index;
2070 return extension_value == TLSEXT_TYPE_padding ||
2071 tls_extension_find(&index, extension_value) != NULL;
2072}
2073
David Benjamine8d53502015-10-10 14:13:23 -04002074int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002075 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002076 if (ssl->client_version == SSL3_VERSION &&
2077 !ssl->s3->send_connection_binding) {
2078 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002079 }
Adam Langley95c29f32014-06-20 12:00:00 -07002080
David Benjamine8d53502015-10-10 14:13:23 -04002081 CBB extensions;
2082 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002083 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002084 }
Adam Langley95c29f32014-06-20 12:00:00 -07002085
David Benjamine8d53502015-10-10 14:13:23 -04002086 ssl->s3->tmp.extensions.sent = 0;
2087 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002088
Adam Langley614c66a2015-06-12 15:26:58 -07002089 size_t i;
2090 for (i = 0; i < kNumExtensions; i++) {
2091 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002092 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002093 }
2094 }
Adam Langley95c29f32014-06-20 12:00:00 -07002095
Adam Langley614c66a2015-06-12 15:26:58 -07002096 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002097 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002098 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002099 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2100 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2101 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002102 }
Adam Langley95c29f32014-06-20 12:00:00 -07002103
Adam Langley33ad2b52015-07-20 17:43:53 -07002104 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002105 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002106 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002107 }
Adam Langley75712922014-10-10 16:23:43 -07002108
David Benjamine8d53502015-10-10 14:13:23 -04002109 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002110 goto err;
2111 }
2112
David Benjamine8d53502015-10-10 14:13:23 -04002113 if (!SSL_IS_DTLS(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002114 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002115 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002116 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002117 *
2118 * NB: because this code works out the length of all existing extensions
2119 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002120 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002121 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002122 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002123 * Server 7.0 is intolerant to the last extension being zero-length. See
2124 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002125 if (padding_len >= 4 + 1) {
2126 padding_len -= 4;
2127 } else {
2128 padding_len = 1;
2129 }
Adam Langley95c29f32014-06-20 12:00:00 -07002130
Adam Langley33ad2b52015-07-20 17:43:53 -07002131 uint8_t *padding_bytes;
2132 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2133 !CBB_add_u16(&extensions, padding_len) ||
2134 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2135 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002136 }
Adam Langley75712922014-10-10 16:23:43 -07002137
Adam Langley33ad2b52015-07-20 17:43:53 -07002138 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002139 }
2140 }
Adam Langley75712922014-10-10 16:23:43 -07002141
David Benjamina01deee2015-12-08 18:56:31 -05002142 /* Discard empty extensions blocks. */
2143 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002144 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002145 }
2146
David Benjamine8d53502015-10-10 14:13:23 -04002147 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002148
2149err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002150 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002151 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002152}
Adam Langley95c29f32014-06-20 12:00:00 -07002153
David Benjamin56380462015-10-10 14:59:09 -04002154int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002155 CBB extensions;
2156 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002157 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002158 }
2159
2160 unsigned i;
2161 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002162 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002163 /* Don't send extensions that were not received. */
2164 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002165 }
Adam Langley95c29f32014-06-20 12:00:00 -07002166
David Benjamin56380462015-10-10 14:59:09 -04002167 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002168 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2169 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2170 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002171 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002172 }
Adam Langley95c29f32014-06-20 12:00:00 -07002173
David Benjamin56380462015-10-10 14:59:09 -04002174 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002175 goto err;
2176 }
2177
David Benjamina01deee2015-12-08 18:56:31 -05002178 /* Discard empty extensions blocks. */
2179 if (CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002180 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002181 }
2182
David Benjamin56380462015-10-10 14:59:09 -04002183 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002184
2185err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002186 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002187 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002188}
Adam Langley95c29f32014-06-20 12:00:00 -07002189
David Benjamin0d56f882015-12-19 17:05:56 -05002190static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002191 size_t i;
2192 for (i = 0; i < kNumExtensions; i++) {
2193 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002194 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002195 }
2196 }
2197
David Benjamin0d56f882015-12-19 17:05:56 -05002198 ssl->s3->tmp.extensions.received = 0;
2199 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002200 /* The renegotiation extension must always be at index zero because the
2201 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2202 * sent as an SCSV. */
2203 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002204
Adam Langleyfcf25832014-12-18 17:42:32 -08002205 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002206 if (CBS_len(cbs) != 0) {
2207 /* Decode the extensions block and check it is valid. */
2208 CBS extensions;
2209 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2210 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002211 *out_alert = SSL_AD_DECODE_ERROR;
2212 return 0;
2213 }
Adam Langley95c29f32014-06-20 12:00:00 -07002214
Adam Langley33ad2b52015-07-20 17:43:53 -07002215 while (CBS_len(&extensions) != 0) {
2216 uint16_t type;
2217 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002218
Adam Langley33ad2b52015-07-20 17:43:53 -07002219 /* Decode the next extension. */
2220 if (!CBS_get_u16(&extensions, &type) ||
2221 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2222 *out_alert = SSL_AD_DECODE_ERROR;
2223 return 0;
2224 }
2225
David Benjaminc7ce9772015-10-09 19:32:41 -04002226 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2227 * ambiguous. Ignore all but the renegotiation_info extension. */
David Benjamin0d56f882015-12-19 17:05:56 -05002228 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
David Benjaminc7ce9772015-10-09 19:32:41 -04002229 continue;
2230 }
2231
Adam Langley33ad2b52015-07-20 17:43:53 -07002232 unsigned ext_index;
2233 const struct tls_extension *const ext =
2234 tls_extension_find(&ext_index, type);
2235
2236 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002237 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002238 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2239 return 0;
2240 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002241 continue;
2242 }
2243
David Benjamin0d56f882015-12-19 17:05:56 -05002244 ssl->s3->tmp.extensions.received |= (1u << ext_index);
Adam Langley614c66a2015-06-12 15:26:58 -07002245 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002246 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
Adam Langley614c66a2015-06-12 15:26:58 -07002247 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002248 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2249 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002250 return 0;
2251 }
Adam Langley614c66a2015-06-12 15:26:58 -07002252 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002253 }
Adam Langley75712922014-10-10 16:23:43 -07002254
Adam Langley614c66a2015-06-12 15:26:58 -07002255 for (i = 0; i < kNumExtensions; i++) {
David Benjamin0d56f882015-12-19 17:05:56 -05002256 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002257 /* Extension wasn't observed so call the callback with a NULL
2258 * parameter. */
2259 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002260 if (!kExtensions[i].parse_clienthello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002261 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2262 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002263 *out_alert = alert;
2264 return 0;
2265 }
2266 }
2267 }
2268
Adam Langleyfcf25832014-12-18 17:42:32 -08002269 return 1;
2270}
Adam Langley95c29f32014-06-20 12:00:00 -07002271
David Benjamin0d56f882015-12-19 17:05:56 -05002272int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002273 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002274 if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) {
2275 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002276 return 0;
2277 }
Adam Langley95c29f32014-06-20 12:00:00 -07002278
David Benjamin0d56f882015-12-19 17:05:56 -05002279 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002280 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002281 return 0;
2282 }
Adam Langley95c29f32014-06-20 12:00:00 -07002283
Adam Langleyfcf25832014-12-18 17:42:32 -08002284 return 1;
2285}
Adam Langley95c29f32014-06-20 12:00:00 -07002286
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002287OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2288
David Benjamin0d56f882015-12-19 17:05:56 -05002289static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002290 uint32_t received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002291
Adam Langley33ad2b52015-07-20 17:43:53 -07002292 if (CBS_len(cbs) != 0) {
2293 /* Decode the extensions block and check it is valid. */
2294 CBS extensions;
2295 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2296 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002297 *out_alert = SSL_AD_DECODE_ERROR;
2298 return 0;
2299 }
Adam Langley95c29f32014-06-20 12:00:00 -07002300
Adam Langley614c66a2015-06-12 15:26:58 -07002301
Adam Langley33ad2b52015-07-20 17:43:53 -07002302 while (CBS_len(&extensions) != 0) {
2303 uint16_t type;
2304 CBS extension;
2305
2306 /* Decode the next extension. */
2307 if (!CBS_get_u16(&extensions, &type) ||
2308 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2309 *out_alert = SSL_AD_DECODE_ERROR;
2310 return 0;
2311 }
2312
2313 unsigned ext_index;
2314 const struct tls_extension *const ext =
2315 tls_extension_find(&ext_index, type);
2316
Adam Langley09505632015-07-30 18:10:13 -07002317 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002318 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002319 return 0;
2320 }
2321 continue;
2322 }
2323
David Benjamin0d56f882015-12-19 17:05:56 -05002324 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) {
Adam Langley09505632015-07-30 18:10:13 -07002325 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002326 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002327 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002328 *out_alert = SSL_AD_DECODE_ERROR;
2329 return 0;
2330 }
David Benjamin03973092014-06-24 23:27:17 -04002331
Adam Langley614c66a2015-06-12 15:26:58 -07002332 received |= (1u << ext_index);
2333
2334 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002335 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002336 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2337 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002338 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002339 return 0;
2340 }
Adam Langley614c66a2015-06-12 15:26:58 -07002341 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002342 }
Adam Langley95c29f32014-06-20 12:00:00 -07002343
Adam Langley33ad2b52015-07-20 17:43:53 -07002344 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002345 for (i = 0; i < kNumExtensions; i++) {
2346 if (!(received & (1u << i))) {
2347 /* Extension wasn't observed so call the callback with a NULL
2348 * parameter. */
2349 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002350 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002351 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2352 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002353 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002354 return 0;
2355 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002356 }
2357 }
Adam Langley95c29f32014-06-20 12:00:00 -07002358
Adam Langleyfcf25832014-12-18 17:42:32 -08002359 return 1;
2360}
Adam Langley95c29f32014-06-20 12:00:00 -07002361
David Benjamin0d56f882015-12-19 17:05:56 -05002362static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002363 int ret = SSL_TLSEXT_ERR_NOACK;
2364 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002365
David Benjamin78f8aab2016-03-10 16:33:58 -05002366 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002367 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002368 ssl->ctx->tlsext_servername_arg);
2369 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002370 ret = ssl->initial_ctx->tlsext_servername_callback(
2371 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002372 }
Adam Langley95c29f32014-06-20 12:00:00 -07002373
Adam Langleyfcf25832014-12-18 17:42:32 -08002374 switch (ret) {
2375 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002376 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002377 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002378
Adam Langleyfcf25832014-12-18 17:42:32 -08002379 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002380 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002381 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002382
Adam Langleyfcf25832014-12-18 17:42:32 -08002383 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002384 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002385 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002386
Adam Langleyfcf25832014-12-18 17:42:32 -08002387 default:
2388 return 1;
2389 }
2390}
Adam Langleyed8270a2014-09-02 13:52:56 -07002391
David Benjamin0d56f882015-12-19 17:05:56 -05002392static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002393 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002394 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002395
David Benjamin78f8aab2016-03-10 16:33:58 -05002396 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002397 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002398 ssl->ctx->tlsext_servername_arg);
2399 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002400 ret = ssl->initial_ctx->tlsext_servername_callback(
2401 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002402 }
Adam Langley95c29f32014-06-20 12:00:00 -07002403
Adam Langleyfcf25832014-12-18 17:42:32 -08002404 switch (ret) {
2405 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002406 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002407 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002408
Adam Langleyfcf25832014-12-18 17:42:32 -08002409 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002410 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002411 return 1;
2412
2413 default:
2414 return 1;
2415 }
2416}
2417
David Benjamin0d56f882015-12-19 17:05:56 -05002418int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002419 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002420 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2421 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002422 return 0;
2423 }
2424
David Benjamin0d56f882015-12-19 17:05:56 -05002425 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002426 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002427 return 0;
2428 }
2429
2430 return 1;
2431}
Adam Langley95c29f32014-06-20 12:00:00 -07002432
David Benjamine3aa1d92015-06-16 15:34:50 -04002433int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002434 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002435 size_t ticket_len, const uint8_t *session_id,
2436 size_t session_id_len) {
2437 int ret = 1; /* Most errors are non-fatal. */
2438 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2439 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002440
David Benjamine3aa1d92015-06-16 15:34:50 -04002441 HMAC_CTX hmac_ctx;
2442 HMAC_CTX_init(&hmac_ctx);
2443 EVP_CIPHER_CTX cipher_ctx;
2444 EVP_CIPHER_CTX_init(&cipher_ctx);
2445
David Benjaminef1b0092015-11-21 14:05:44 -05002446 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002447 *out_session = NULL;
2448
2449 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2450 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002451 }
2452
David Benjaminadcc3952015-04-26 13:07:57 -04002453 /* Ensure there is room for the key name and the largest IV
2454 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2455 * the maximum IV length should be well under the minimum size for the
2456 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002457 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2458 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002459 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002460 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002461
David Benjamine3aa1d92015-06-16 15:34:50 -04002462 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002463 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2464 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2465 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002466 if (cb_ret < 0) {
2467 ret = 0;
2468 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002469 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002470 if (cb_ret == 0) {
2471 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002472 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002473 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002474 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002475 }
2476 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002477 /* Check the key name matches. */
2478 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2479 SSL_TICKET_KEY_NAME_LEN) != 0) {
2480 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002481 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002482 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2483 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002484 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002485 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2486 ssl_ctx->tlsext_tick_aes_key, iv)) {
2487 ret = 0;
2488 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002489 }
2490 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002491 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002492
David Benjamine3aa1d92015-06-16 15:34:50 -04002493 /* Check the MAC at the end of the ticket. */
2494 uint8_t mac[EVP_MAX_MD_SIZE];
2495 size_t mac_len = HMAC_size(&hmac_ctx);
2496 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002497 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002498 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002499 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002500 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2501 HMAC_Final(&hmac_ctx, mac, NULL);
2502 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2503 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002504 }
2505
David Benjamine3aa1d92015-06-16 15:34:50 -04002506 /* Decrypt the session data. */
2507 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2508 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2509 mac_len;
2510 plaintext = OPENSSL_malloc(ciphertext_len);
2511 if (plaintext == NULL) {
2512 ret = 0;
2513 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002514 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002515 if (ciphertext_len >= INT_MAX) {
2516 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002517 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002518 int len1, len2;
2519 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2520 (int)ciphertext_len) ||
2521 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2522 ERR_clear_error(); /* Don't leave an error on the queue. */
2523 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002524 }
2525
David Benjamine3aa1d92015-06-16 15:34:50 -04002526 /* Decode the session. */
2527 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2528 if (session == NULL) {
2529 ERR_clear_error(); /* Don't leave an error on the queue. */
2530 goto done;
2531 }
2532
2533 /* Copy the client's session ID into the new session, to denote the ticket has
2534 * been accepted. */
2535 memcpy(session->session_id, session_id, session_id_len);
2536 session->session_id_length = session_id_len;
2537
2538 *out_session = session;
2539
2540done:
2541 OPENSSL_free(plaintext);
2542 HMAC_CTX_cleanup(&hmac_ctx);
2543 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2544 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002545}
Adam Langley95c29f32014-06-20 12:00:00 -07002546
Steven Valdez0d62f262015-09-04 12:41:04 -04002547int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002548 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05002549 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002550 return 1;
2551 }
David Benjamincd996942014-07-20 16:23:51 -04002552
Steven Valdez0d62f262015-09-04 12:41:04 -04002553 CERT *const cert = ssl->cert;
2554 OPENSSL_free(cert->peer_sigalgs);
2555 cert->peer_sigalgs = NULL;
2556 cert->peer_sigalgslen = 0;
2557
2558 size_t num_sigalgs = CBS_len(in_sigalgs);
2559
2560 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002561 return 0;
2562 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002563 num_sigalgs /= 2;
2564
2565 /* supported_signature_algorithms in the certificate request is
2566 * allowed to be empty. */
2567 if (num_sigalgs == 0) {
2568 return 1;
2569 }
2570
Steven Valdez02563852016-06-23 13:33:05 -04002571 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
2572 * and we just divided |num_sigalgs| by two. */
2573 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
Steven Valdez0d62f262015-09-04 12:41:04 -04002574 if (cert->peer_sigalgs == NULL) {
2575 return 0;
2576 }
2577 cert->peer_sigalgslen = num_sigalgs;
2578
2579 CBS sigalgs;
2580 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2581
2582 size_t i;
2583 for (i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04002584 if (!CBS_get_u16(&sigalgs, &cert->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04002585 return 0;
2586 }
2587 }
Adam Langley95c29f32014-06-20 12:00:00 -07002588
Adam Langleyfcf25832014-12-18 17:42:32 -08002589 return 1;
2590}
David Benjaminec2f27d2014-11-13 19:17:25 -05002591
David Benjaminea9a0d52016-07-08 15:52:59 -07002592int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04002593 CERT *cert = ssl->cert;
Steven Valdez0d62f262015-09-04 12:41:04 -04002594 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002595
Steven Valdezf0451ca2016-06-29 13:16:27 -04002596 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
2597 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
2598 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin1fb125c2016-07-08 18:52:12 -07002599 if (ssl_private_key_type(ssl) == EVP_PKEY_RSA) {
David Benjaminea9a0d52016-07-08 15:52:59 -07002600 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
2601 } else {
2602 *out = SSL_SIGN_ECDSA_SHA1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002603 }
David Benjaminea9a0d52016-07-08 15:52:59 -07002604 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002605 }
2606
Steven Valdezeff1e8d2016-07-06 14:24:47 -04002607 const uint16_t *sigalgs;
2608 size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07002609 if (cert->sigalgs != NULL) {
2610 sigalgs = cert->sigalgs;
2611 sigalgs_len = cert->sigalgs_len;
Steven Valdez0d62f262015-09-04 12:41:04 -04002612 }
2613
David Benjaminea9a0d52016-07-08 15:52:59 -07002614 const uint16_t *peer_sigalgs = cert->peer_sigalgs;
2615 size_t peer_sigalgs_len = cert->peer_sigalgslen;
David Benjamin51dd7d62016-07-08 16:07:01 -07002616 if (peer_sigalgs_len == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07002617 /* If the client didn't specify any signature_algorithms extension then
2618 * we can assume that it supports SHA1. See
2619 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
2620 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
2621 SSL_SIGN_ECDSA_SHA1};
2622 peer_sigalgs = kDefaultPeerAlgorithms;
2623 peer_sigalgs_len =
2624 sizeof(kDefaultPeerAlgorithms) / sizeof(kDefaultPeerAlgorithms);
2625 }
2626
David Benjamind246b812016-07-08 15:07:02 -07002627 for (i = 0; i < sigalgs_len; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07002628 uint16_t sigalg = sigalgs[i];
2629 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
2630 * negotiated. */
2631 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
2632 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
2633 continue;
2634 }
2635
David Benjaminea9a0d52016-07-08 15:52:59 -07002636 for (j = 0; j < peer_sigalgs_len; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07002637 if (sigalg == peer_sigalgs[j]) {
2638 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07002639 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002640 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002641 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002642 }
Adam Langley95c29f32014-06-20 12:00:00 -07002643
David Benjaminea9a0d52016-07-08 15:52:59 -07002644 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
2645 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002646}
Adam Langley95c29f32014-06-20 12:00:00 -07002647
David Benjamind6a4ae92015-08-06 11:10:51 -04002648int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2649 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002650 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002651
2652 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002653 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2654 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002655 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002656
David Benjamind6a4ae92015-08-06 11:10:51 -04002657 static const char kClientIDMagic[] = "TLS Channel ID signature";
2658 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2659
2660 if (ssl->hit) {
2661 static const char kResumptionMagic[] = "Resumption";
2662 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2663 if (ssl->session->original_handshake_hash_len == 0) {
2664 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2665 goto err;
2666 }
2667 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2668 ssl->session->original_handshake_hash_len);
2669 }
2670
2671 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2672 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2673 sizeof(handshake_hash));
2674 if (handshake_hash_len < 0) {
2675 goto err;
2676 }
2677 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2678 unsigned len_u;
2679 EVP_DigestFinal_ex(&ctx, out, &len_u);
2680 *out_len = len_u;
2681
2682 ret = 1;
2683
2684err:
2685 EVP_MD_CTX_cleanup(&ctx);
2686 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002687}
Adam Langley1258b6a2014-06-20 12:00:00 -07002688
2689/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin0d56f882015-12-19 17:05:56 -05002690 * hashes in |ssl->session| so that Channel ID resumptions can sign that
2691 * data. */
2692int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002693 int digest_len;
2694 /* This function should never be called for a resumed session because the
2695 * handshake hashes that we wish to record are for the original, full
2696 * handshake. */
David Benjamin0d56f882015-12-19 17:05:56 -05002697 if (ssl->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002698 return -1;
2699 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002700
Adam Langleyfcf25832014-12-18 17:42:32 -08002701 digest_len =
David Benjamin0d56f882015-12-19 17:05:56 -05002702 tls1_handshake_digest(ssl, ssl->session->original_handshake_hash,
2703 sizeof(ssl->session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08002704 if (digest_len < 0) {
2705 return -1;
2706 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002707
David Benjamin0d56f882015-12-19 17:05:56 -05002708 ssl->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002709
Adam Langleyfcf25832014-12-18 17:42:32 -08002710 return 1;
2711}