blob: 8facdd380bee601af574918b5d8ac2f60f92fbc5 [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>
122#include <openssl/obj.h>
123#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
Adam Langleyfcf25832014-12-18 17:42:32 -0800294static const uint16_t eccurves_default[] = {
David Benjaminfc6e5a72016-01-19 15:36:43 -0500295 SSL_CURVE_X25519,
David Benjamin4298d772015-12-19 00:18:25 -0500296 SSL_CURVE_SECP256R1,
297 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700298#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin4298d772015-12-19 00:18:25 -0500299 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
Adam Langleyfcf25832014-12-18 17:42:32 -0800303/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
304 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
305 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin0d56f882015-12-19 17:05:56 -0500306static void tls1_get_curvelist(SSL *ssl, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 const uint16_t **out_curve_ids,
308 size_t *out_curve_ids_len) {
309 if (get_peer_curves) {
David Benjamin55a43642015-04-20 14:45:55 -0400310 /* Only clients send a curve list, so this function is only called
311 * on the server. */
David Benjamin0d56f882015-12-19 17:05:56 -0500312 assert(ssl->server);
313 *out_curve_ids = ssl->s3->tmp.peer_ellipticcurvelist;
314 *out_curve_ids_len = ssl->s3->tmp.peer_ellipticcurvelist_length;
Adam Langleyfcf25832014-12-18 17:42:32 -0800315 return;
316 }
Adam Langley95c29f32014-06-20 12:00:00 -0700317
David Benjamin0d56f882015-12-19 17:05:56 -0500318 *out_curve_ids = ssl->tlsext_ellipticcurvelist;
319 *out_curve_ids_len = ssl->tlsext_ellipticcurvelist_length;
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 if (!*out_curve_ids) {
321 *out_curve_ids = eccurves_default;
322 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
323 }
324}
David Benjamined439582014-07-14 19:13:02 -0400325
David Benjamin4298d772015-12-19 00:18:25 -0500326int tls1_get_shared_curve(SSL *ssl, uint16_t *out_curve_id) {
David Benjamin55a43642015-04-20 14:45:55 -0400327 const uint16_t *curves, *peer_curves, *pref, *supp;
328 size_t curves_len, peer_curves_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
David Benjamin4298d772015-12-19 00:18:25 -0500335 tls1_get_curvelist(ssl, 0 /* local curves */, &curves, &curves_len);
336 tls1_get_curvelist(ssl, 1 /* peer curves */, &peer_curves, &peer_curves_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800337
David Benjamin55a43642015-04-20 14:45:55 -0400338 if (peer_curves_len == 0) {
339 /* Clients are not required to send a supported_curves extension. In this
340 * case, the server is free to pick any curve 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
345 * support our favoured curve. */
346 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) {
David Benjamin55a43642015-04-20 14:45:55 -0400350 pref = curves;
351 pref_len = curves_len;
352 supp = peer_curves;
353 supp_len = peer_curves_len;
354 } else {
355 pref = peer_curves;
356 pref_len = peer_curves_len;
357 supp = curves;
358 supp_len = curves_len;
359 }
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]) {
David Benjamin4298d772015-12-19 00:18:25 -0500364 *out_curve_id = pref[i];
365 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
David Benjamin072334d2014-07-13 16:24:27 -0400373int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 const int *curves, size_t ncurves) {
375 uint16_t *curve_ids;
376 size_t i;
377
Brian Smith5ba06892016-02-07 09:36:04 -1000378 curve_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 if (curve_ids == NULL) {
380 return 0;
381 }
382
383 for (i = 0; i < ncurves; i++) {
David Benjamin4298d772015-12-19 00:18:25 -0500384 if (!ssl_nid_to_curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800385 OPENSSL_free(curve_ids);
386 return 0;
387 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 }
389
David Benjamin2755a3e2015-04-22 16:17:58 -0400390 OPENSSL_free(*out_curve_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 *out_curve_ids = curve_ids;
392 *out_curve_ids_len = ncurves;
393
394 return 1;
395}
Adam Langley95c29f32014-06-20 12:00:00 -0700396
David Benjamin072334d2014-07-13 16:24:27 -0400397/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
398 * TLS curve ID and point format, respectively, for |ec|. It returns one on
399 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800400static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
401 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
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 /* Determine curve ID */
416 nid = EC_GROUP_get_curve_name(grp);
David Benjamin4298d772015-12-19 00:18:25 -0500417 if (!ssl_nid_to_curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 return 0;
419 }
David Benjamin072334d2014-07-13 16:24:27 -0400420
Adam Langleyfcf25832014-12-18 17:42:32 -0800421 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
422 *out_curve_id = id;
423
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
Adam Langleyfcf25832014-12-18 17:42:32 -0800438/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
439 * and the peer's curve 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. */
David Benjamin4298d772015-12-19 00:18:25 -0500441int tls1_check_curve_id(SSL *ssl, uint16_t curve_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800442 const uint16_t *curves;
David Benjamin55a43642015-04-20 14:45:55 -0400443 size_t curves_len, i, get_peer_curves;
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. */
David Benjamin55a43642015-04-20 14:45:55 -0400446 for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
David Benjamin4298d772015-12-19 00:18:25 -0500447 if (get_peer_curves && !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
David Benjamin4298d772015-12-19 00:18:25 -0500453 tls1_get_curvelist(ssl, get_peer_curves, &curves, &curves_len);
David Benjamin55a43642015-04-20 14:45:55 -0400454 if (get_peer_curves && curves_len == 0) {
455 /* Clients are not required to send a supported_curves extension. In this
456 * case, the server is free to pick any curve it likes. See RFC 4492,
457 * section 4, paragraph 3. */
458 continue;
459 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 for (i = 0; i < curves_len; i++) {
461 if (curves[i] == curve_id) {
462 break;
463 }
464 }
Adam Langley95c29f32014-06-20 12:00:00 -0700465
Adam Langleyfcf25832014-12-18 17:42:32 -0800466 if (i == curves_len) {
467 return 0;
468 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 }
David Benjamin033e5f42014-11-13 18:47:41 -0500470
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 return 1;
472}
David Benjamin033e5f42014-11-13 18:47:41 -0500473
David Benjamin0d56f882015-12-19 17:05:56 -0500474int tls1_check_ec_cert(SSL *ssl, X509 *x) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 int ret = 0;
476 EVP_PKEY *pkey = X509_get_pubkey(x);
477 uint16_t curve_id;
478 uint8_t comp_id;
479
David Benjamin758d1272015-11-20 17:47:25 -0500480 if (!pkey) {
481 goto done;
482 }
483 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
484 if (ec_key == NULL ||
485 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, ec_key) ||
David Benjamin0d56f882015-12-19 17:05:56 -0500486 !tls1_check_curve_id(ssl, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400487 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 goto done;
489 }
490
491 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500492
493done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400494 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 return ret;
496}
David Benjamin42e9a772014-09-02 23:18:44 -0400497
Adam Langley95c29f32014-06-20 12:00:00 -0700498/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800499 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Adam Langley95c29f32014-06-20 12:00:00 -0700501#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700502
Adam Langley95c29f32014-06-20 12:00:00 -0700503#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langleyfcf25832014-12-18 17:42:32 -0800505#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700506
David Benjamincff64722014-08-19 19:54:46 -0400507static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800508 tlsext_sigalg(TLSEXT_hash_sha512)
509 tlsext_sigalg(TLSEXT_hash_sha384)
510 tlsext_sigalg(TLSEXT_hash_sha256)
Adam Langleyfcf25832014-12-18 17:42:32 -0800511 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700512};
David Benjamin05da6e12014-07-12 20:42:55 -0400513
David Benjamin0d56f882015-12-19 17:05:56 -0500514size_t tls12_get_psigalgs(SSL *ssl, const uint8_t **psigs) {
David Benjamin3dd90162015-08-09 11:26:40 -0400515 *psigs = tls12_sigalgs;
516 return sizeof(tls12_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800517}
Adam Langley95c29f32014-06-20 12:00:00 -0700518
David Benjamin6e807652015-11-02 12:02:20 -0500519int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert,
520 uint8_t hash, uint8_t signature, EVP_PKEY *pkey) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800521 const uint8_t *sent_sigs;
522 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400523 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800524
525 /* Should never happen */
526 if (sigalg == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400527 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 *out_alert = SSL_AD_INTERNAL_ERROR;
529 return 0;
530 }
531
Adam Langleyfcf25832014-12-18 17:42:32 -0800532 /* Check key type is consistent with signature */
533 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400534 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
536 return 0;
537 }
538
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500540 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800541 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
542 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
543 break;
544 }
545 }
546
David Benjamin788be4a2015-10-30 17:50:57 -0400547 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400548 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
550 return 0;
551 }
552
553 *out_md = tls12_get_hash(hash);
554 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400555 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800556 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
557 return 0;
558 }
559
560 return 1;
561}
562
563/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
564 * supported or doesn't appear in supported signature algorithms. Unlike
565 * ssl_cipher_get_disabled this applies to a specific session and not global
566 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500567void ssl_set_client_disabled(SSL *ssl) {
568 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800569 const uint8_t *sigalgs;
570 size_t i, sigalgslen;
571 int have_rsa = 0, have_ecdsa = 0;
572 c->mask_a = 0;
573 c->mask_k = 0;
574
Adam Langleyfcf25832014-12-18 17:42:32 -0800575 /* Now go through all signature algorithms seeing if we support any for RSA,
576 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500577 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800578 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
579 switch (sigalgs[1]) {
580 case TLSEXT_signature_rsa:
581 have_rsa = 1;
582 break;
583
584 case TLSEXT_signature_ecdsa:
585 have_ecdsa = 1;
586 break;
587 }
588 }
589
590 /* Disable auth if we don't include any appropriate signature algorithms. */
591 if (!have_rsa) {
592 c->mask_a |= SSL_aRSA;
593 }
594 if (!have_ecdsa) {
595 c->mask_a |= SSL_aECDSA;
596 }
597
598 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500599 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800600 c->mask_a |= SSL_aPSK;
601 c->mask_k |= SSL_kPSK;
602 }
603}
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langley614c66a2015-06-12 15:26:58 -0700605/* tls_extension represents a TLS extension that is handled internally. The
606 * |init| function is called for each handshake, before any other functions of
607 * the extension. Then the add and parse callbacks are called as needed.
608 *
609 * The parse callbacks receive a |CBS| that contains the contents of the
610 * extension (i.e. not including the type and length bytes). If an extension is
611 * not received then the parse callbacks will be called with a NULL CBS so that
612 * they can do any processing needed to handle the absence of an extension.
613 *
614 * The add callbacks receive a |CBB| to which the extension can be appended but
615 * the function is responsible for appending the type and length bytes too.
616 *
617 * All callbacks return one for success and zero for error. If a parse function
618 * returns zero then a fatal alert with value |*out_alert| will be sent. If
619 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
620struct tls_extension {
621 uint16_t value;
622 void (*init)(SSL *ssl);
623
624 int (*add_clienthello)(SSL *ssl, CBB *out);
625 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
626
627 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
628 int (*add_serverhello)(SSL *ssl, CBB *out);
629};
630
631
632/* Server name indication (SNI).
633 *
634 * https://tools.ietf.org/html/rfc6066#section-3. */
635
636static void ext_sni_init(SSL *ssl) {
637 ssl->s3->tmp.should_ack_sni = 0;
638}
639
640static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
641 if (ssl->tlsext_hostname == NULL) {
642 return 1;
643 }
644
645 CBB contents, server_name_list, name;
646 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
647 !CBB_add_u16_length_prefixed(out, &contents) ||
648 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
649 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
650 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
651 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
652 strlen(ssl->tlsext_hostname)) ||
653 !CBB_flush(out)) {
654 return 0;
655 }
656
657 return 1;
658}
659
David Benjamin0d56f882015-12-19 17:05:56 -0500660static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
661 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700662 if (contents == NULL) {
663 return 1;
664 }
665
666 if (CBS_len(contents) != 0) {
667 return 0;
668 }
669
670 assert(ssl->tlsext_hostname != NULL);
671
672 if (!ssl->hit) {
673 assert(ssl->session->tlsext_hostname == NULL);
674 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
675 if (!ssl->session->tlsext_hostname) {
676 *out_alert = SSL_AD_INTERNAL_ERROR;
677 return 0;
678 }
679 }
680
681 return 1;
682}
683
David Benjamin0d56f882015-12-19 17:05:56 -0500684static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
685 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700686 if (contents == NULL) {
687 return 1;
688 }
689
690 /* The servername extension is treated as follows:
691 *
692 * - Only the hostname type is supported with a maximum length of 255.
693 * - The servername is rejected if too long or if it contains zeros, in
694 * which case an fatal alert is generated.
695 * - The servername field is maintained together with the session cache.
696 * - When a session is resumed, the servername callback is invoked in order
697 * to allow the application to position itself to the right context.
698 * - The servername is acknowledged if it is new for a session or when
699 * it is identical to a previously used for the same session.
700 * Applications can control the behaviour. They can at any time
701 * set a 'desirable' servername for a new SSL object. This can be the
702 * case for example with HTTPS when a Host: header field is received and
703 * a renegotiation is requested. In this case, a possible servername
704 * presented in the new client hello is only acknowledged if it matches
705 * the value of the Host: field.
706 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
707 * if they provide for changing an explicit servername context for the
708 * session,
709 * i.e. when the session has been established with a servername extension.
710 */
711
712 CBS server_name_list;
713 char have_seen_host_name = 0;
714
715 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
716 CBS_len(&server_name_list) == 0 ||
717 CBS_len(contents) != 0) {
718 return 0;
719 }
720
721 /* Decode each ServerName in the extension. */
722 while (CBS_len(&server_name_list) > 0) {
723 uint8_t name_type;
724 CBS host_name;
725
726 if (!CBS_get_u8(&server_name_list, &name_type) ||
727 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
728 return 0;
729 }
730
731 /* Only host_name is supported. */
732 if (name_type != TLSEXT_NAMETYPE_host_name) {
733 continue;
734 }
735
736 if (have_seen_host_name) {
737 /* The ServerNameList MUST NOT contain more than one name of the same
738 * name_type. */
739 return 0;
740 }
741
742 have_seen_host_name = 1;
743
744 if (CBS_len(&host_name) == 0 ||
745 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
746 CBS_contains_zero_byte(&host_name)) {
747 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
748 return 0;
749 }
750
751 if (!ssl->hit) {
752 assert(ssl->session->tlsext_hostname == NULL);
753 if (ssl->session->tlsext_hostname) {
754 /* This should be impossible. */
755 return 0;
756 }
757
758 /* Copy the hostname as a string. */
759 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
760 *out_alert = SSL_AD_INTERNAL_ERROR;
761 return 0;
762 }
763
764 ssl->s3->tmp.should_ack_sni = 1;
765 }
766 }
767
768 return 1;
769}
770
771static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
772 if (ssl->hit ||
773 !ssl->s3->tmp.should_ack_sni ||
774 ssl->session->tlsext_hostname == NULL) {
775 return 1;
776 }
777
778 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
779 !CBB_add_u16(out, 0 /* length */)) {
780 return 0;
781 }
782
783 return 1;
784}
785
786
Adam Langley5021b222015-06-12 18:27:58 -0700787/* Renegotiation indication.
788 *
789 * https://tools.ietf.org/html/rfc5746 */
790
791static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
792 CBB contents, prev_finished;
793 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
794 !CBB_add_u16_length_prefixed(out, &contents) ||
795 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
796 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
797 ssl->s3->previous_client_finished_len) ||
798 !CBB_flush(out)) {
799 return 0;
800 }
801
802 return 1;
803}
804
805static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
806 CBS *contents) {
David Benjamin3e052de2015-11-25 20:10:31 -0500807 /* Servers may not switch between omitting the extension and supporting it.
808 * See RFC 5746, sections 3.5 and 4.2. */
809 if (ssl->s3->initial_handshake_complete &&
810 (contents != NULL) != ssl->s3->send_connection_binding) {
811 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
812 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
813 return 0;
814 }
815
Adam Langley5021b222015-06-12 18:27:58 -0700816 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500817 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700818 * RI even on initial ServerHello because the client doesn't see any
819 * renegotiation during an attack. However this would mean we could not
820 * connect to any server which doesn't support RI.
821 *
David Benjamine9cddb82015-11-23 14:36:40 -0500822 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
823 * practical terms every client sets it so it's just assumed here. */
824 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700825 }
826
827 const size_t expected_len = ssl->s3->previous_client_finished_len +
828 ssl->s3->previous_server_finished_len;
829
830 /* Check for logic errors */
831 assert(!expected_len || ssl->s3->previous_client_finished_len);
832 assert(!expected_len || ssl->s3->previous_server_finished_len);
833
834 /* Parse out the extension contents. */
835 CBS renegotiated_connection;
836 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
837 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400838 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700839 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
840 return 0;
841 }
842
843 /* Check that the extension matches. */
844 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400845 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700846 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
847 return 0;
848 }
849
850 const uint8_t *d = CBS_data(&renegotiated_connection);
851 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
852 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400853 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700854 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
855 return 0;
856 }
857 d += ssl->s3->previous_client_finished_len;
858
859 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
860 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400861 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700862 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
863 return 0;
864 }
865 ssl->s3->send_connection_binding = 1;
866
867 return 1;
868}
869
870static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
871 CBS *contents) {
872 /* Renegotiation isn't supported as a server so this function should never be
873 * called after the initial handshake. */
874 assert(!ssl->s3->initial_handshake_complete);
875
876 CBS fake_contents;
877 static const uint8_t kFakeExtension[] = {0};
878
879 if (contents == NULL) {
880 if (ssl->s3->send_connection_binding) {
881 /* The renegotiation SCSV was received so pretend that we received a
882 * renegotiation extension. */
883 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
884 contents = &fake_contents;
885 /* We require that the renegotiation extension is at index zero of
886 * kExtensions. */
887 ssl->s3->tmp.extensions.received |= (1u << 0);
888 } else {
889 return 1;
890 }
891 }
892
893 CBS renegotiated_connection;
894
895 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
896 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400897 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700898 return 0;
899 }
900
901 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500902 if (!CBS_mem_equal(&renegotiated_connection,
903 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700904 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400905 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700906 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
907 return 0;
908 }
909
910 ssl->s3->send_connection_binding = 1;
911
912 return 1;
913}
914
915static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
916 CBB contents, prev_finished;
917 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
918 !CBB_add_u16_length_prefixed(out, &contents) ||
919 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
920 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
921 ssl->s3->previous_client_finished_len) ||
922 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
923 ssl->s3->previous_server_finished_len) ||
924 !CBB_flush(out)) {
925 return 0;
926 }
927
928 return 1;
929}
930
Adam Langley0a056712015-07-01 15:03:33 -0700931
932/* Extended Master Secret.
933 *
David Benjamin43946d42016-02-01 08:42:19 -0500934 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700935
936static void ext_ems_init(SSL *ssl) {
937 ssl->s3->tmp.extended_master_secret = 0;
938}
939
940static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
941 if (ssl->version == SSL3_VERSION) {
942 return 1;
943 }
944
945 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
946 !CBB_add_u16(out, 0 /* length */)) {
947 return 0;
948 }
949
950 return 1;
951}
952
953static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
954 CBS *contents) {
955 if (contents == NULL) {
956 return 1;
957 }
958
959 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
960 return 0;
961 }
962
963 ssl->s3->tmp.extended_master_secret = 1;
964 return 1;
965}
966
David Benjamin0d56f882015-12-19 17:05:56 -0500967static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
968 CBS *contents) {
Adam Langley0a056712015-07-01 15:03:33 -0700969 if (ssl->version == SSL3_VERSION || contents == NULL) {
970 return 1;
971 }
972
973 if (CBS_len(contents) != 0) {
974 return 0;
975 }
976
977 ssl->s3->tmp.extended_master_secret = 1;
978 return 1;
979}
980
981static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
982 if (!ssl->s3->tmp.extended_master_secret) {
983 return 1;
984 }
985
986 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
987 !CBB_add_u16(out, 0 /* length */)) {
988 return 0;
989 }
990
991 return 1;
992}
993
Adam Langley9b05bc52015-07-01 15:25:33 -0700994
995/* Session tickets.
996 *
997 * https://tools.ietf.org/html/rfc5077 */
998
999static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1000 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1001 return 1;
1002 }
1003
1004 const uint8_t *ticket_data = NULL;
1005 int ticket_len = 0;
1006
1007 /* Renegotiation does not participate in session resumption. However, still
1008 * advertise the extension to avoid potentially breaking servers which carry
1009 * over the state from the previous handshake, such as OpenSSL servers
1010 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1011 if (!ssl->s3->initial_handshake_complete &&
1012 ssl->session != NULL &&
1013 ssl->session->tlsext_tick != NULL) {
1014 ticket_data = ssl->session->tlsext_tick;
1015 ticket_len = ssl->session->tlsext_ticklen;
1016 }
1017
1018 CBB ticket;
1019 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1020 !CBB_add_u16_length_prefixed(out, &ticket) ||
1021 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1022 !CBB_flush(out)) {
1023 return 0;
1024 }
1025
1026 return 1;
1027}
1028
1029static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1030 CBS *contents) {
1031 ssl->tlsext_ticket_expected = 0;
1032
1033 if (contents == NULL) {
1034 return 1;
1035 }
1036
1037 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1038 * this function should never be called, even if the server tries to send the
1039 * extension. */
1040 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1041
1042 if (CBS_len(contents) != 0) {
1043 return 0;
1044 }
1045
1046 ssl->tlsext_ticket_expected = 1;
1047 return 1;
1048}
1049
David Benjamin0d56f882015-12-19 17:05:56 -05001050static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1051 CBS *contents) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001052 /* This function isn't used because the ticket extension from the client is
David Benjaminc7ce9772015-10-09 19:32:41 -04001053 * handled in ssl_session.c. */
Adam Langley9b05bc52015-07-01 15:25:33 -07001054 return 1;
1055}
1056
1057static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1058 if (!ssl->tlsext_ticket_expected) {
1059 return 1;
1060 }
1061
1062 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1063 * true. */
1064 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1065
1066 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1067 !CBB_add_u16(out, 0 /* length */)) {
1068 return 0;
1069 }
1070
1071 return 1;
1072}
1073
1074
Adam Langley2e857bd2015-07-01 16:09:19 -07001075/* Signature Algorithms.
1076 *
1077 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1078
1079static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1080 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1081 return 1;
1082 }
1083
1084 const uint8_t *sigalgs_data;
1085 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1086
1087 CBB contents, sigalgs;
1088 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1089 !CBB_add_u16_length_prefixed(out, &contents) ||
1090 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1091 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1092 !CBB_flush(out)) {
1093 return 0;
1094 }
1095
1096 return 1;
1097}
1098
1099static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1100 CBS *contents) {
1101 if (contents != NULL) {
1102 /* Servers MUST NOT send this extension. */
1103 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001104 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001105 return 0;
1106 }
1107
1108 return 1;
1109}
1110
1111static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1112 CBS *contents) {
1113 OPENSSL_free(ssl->cert->peer_sigalgs);
1114 ssl->cert->peer_sigalgs = NULL;
1115 ssl->cert->peer_sigalgslen = 0;
1116
Adam Langley2e857bd2015-07-01 16:09:19 -07001117 if (contents == NULL) {
1118 return 1;
1119 }
1120
1121 CBS supported_signature_algorithms;
1122 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001123 CBS_len(contents) != 0 ||
1124 CBS_len(&supported_signature_algorithms) == 0 ||
1125 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001126 return 0;
1127 }
1128
1129 return 1;
1130}
1131
1132static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1133 /* Servers MUST NOT send this extension. */
1134 return 1;
1135}
1136
1137
Adam Langleybb0bd042015-07-01 16:21:03 -07001138/* OCSP Stapling.
1139 *
1140 * https://tools.ietf.org/html/rfc6066#section-8 */
1141
1142static void ext_ocsp_init(SSL *ssl) {
1143 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001144 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001145}
1146
1147static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1148 if (!ssl->ocsp_stapling_enabled) {
1149 return 1;
1150 }
1151
1152 CBB contents;
1153 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1154 !CBB_add_u16_length_prefixed(out, &contents) ||
1155 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1156 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1157 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1158 !CBB_flush(out)) {
1159 return 0;
1160 }
1161
Adam Langleyce9d85e2016-01-24 15:58:39 -08001162 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001163 return 1;
1164}
1165
1166static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001167 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001168 if (contents == NULL) {
1169 return 1;
1170 }
1171
1172 if (CBS_len(contents) != 0) {
1173 return 0;
1174 }
1175
1176 ssl->s3->tmp.certificate_status_expected = 1;
1177 return 1;
1178}
1179
1180static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1181 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001182 if (contents == NULL) {
1183 return 1;
1184 }
1185
1186 uint8_t status_type;
1187 if (!CBS_get_u8(contents, &status_type)) {
1188 return 0;
1189 }
1190
1191 /* We cannot decide whether OCSP stapling will occur yet because the correct
1192 * SSL_CTX might not have been selected. */
1193 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1194
Adam Langleybb0bd042015-07-01 16:21:03 -07001195 return 1;
1196}
1197
1198static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001199 /* The extension shouldn't be sent when resuming sessions. */
1200 if (ssl->hit ||
1201 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001202 ssl->ctx->ocsp_response_length == 0) {
1203 return 1;
1204 }
1205
1206 ssl->s3->tmp.certificate_status_expected = 1;
1207
1208 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1209 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001210}
1211
1212
Adam Langley97dfcbf2015-07-01 18:35:20 -07001213/* Next protocol negotiation.
1214 *
1215 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1216
1217static void ext_npn_init(SSL *ssl) {
1218 ssl->s3->next_proto_neg_seen = 0;
1219}
1220
1221static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1222 if (ssl->s3->initial_handshake_complete ||
1223 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001224 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001225 SSL_IS_DTLS(ssl)) {
1226 return 1;
1227 }
1228
1229 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1230 !CBB_add_u16(out, 0 /* length */)) {
1231 return 0;
1232 }
1233
1234 return 1;
1235}
1236
1237static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1238 CBS *contents) {
1239 if (contents == NULL) {
1240 return 1;
1241 }
1242
1243 /* If any of these are false then we should never have sent the NPN
1244 * extension in the ClientHello and thus this function should never have been
1245 * called. */
1246 assert(!ssl->s3->initial_handshake_complete);
1247 assert(!SSL_IS_DTLS(ssl));
1248 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001249 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001250
David Benjamin76c2efc2015-08-31 14:24:29 -04001251 if (ssl->s3->alpn_selected != NULL) {
1252 /* NPN and ALPN may not be negotiated in the same connection. */
1253 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1254 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1255 return 0;
1256 }
1257
Adam Langley97dfcbf2015-07-01 18:35:20 -07001258 const uint8_t *const orig_contents = CBS_data(contents);
1259 const size_t orig_len = CBS_len(contents);
1260
1261 while (CBS_len(contents) != 0) {
1262 CBS proto;
1263 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1264 CBS_len(&proto) == 0) {
1265 return 0;
1266 }
1267 }
1268
1269 uint8_t *selected;
1270 uint8_t selected_len;
1271 if (ssl->ctx->next_proto_select_cb(
1272 ssl, &selected, &selected_len, orig_contents, orig_len,
1273 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1274 *out_alert = SSL_AD_INTERNAL_ERROR;
1275 return 0;
1276 }
1277
David Benjamin79978df2015-12-25 15:56:49 -05001278 OPENSSL_free(ssl->s3->next_proto_negotiated);
1279 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1280 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001281 *out_alert = SSL_AD_INTERNAL_ERROR;
1282 return 0;
1283 }
1284
David Benjamin79978df2015-12-25 15:56:49 -05001285 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001286 ssl->s3->next_proto_neg_seen = 1;
1287
1288 return 1;
1289}
1290
1291static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1292 CBS *contents) {
1293 if (contents != NULL && CBS_len(contents) != 0) {
1294 return 0;
1295 }
1296
1297 if (contents == NULL ||
1298 ssl->s3->initial_handshake_complete ||
1299 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1300 * afterwards, parsing the ALPN extension will clear
1301 * |next_proto_neg_seen|. */
1302 ssl->s3->alpn_selected != NULL ||
1303 ssl->ctx->next_protos_advertised_cb == NULL ||
1304 SSL_IS_DTLS(ssl)) {
1305 return 1;
1306 }
1307
1308 ssl->s3->next_proto_neg_seen = 1;
1309 return 1;
1310}
1311
1312static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1313 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1314 * parsed. */
1315 if (!ssl->s3->next_proto_neg_seen) {
1316 return 1;
1317 }
1318
1319 const uint8_t *npa;
1320 unsigned npa_len;
1321
1322 if (ssl->ctx->next_protos_advertised_cb(
1323 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1324 SSL_TLSEXT_ERR_OK) {
1325 ssl->s3->next_proto_neg_seen = 0;
1326 return 1;
1327 }
1328
1329 CBB contents;
1330 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1331 !CBB_add_u16_length_prefixed(out, &contents) ||
1332 !CBB_add_bytes(&contents, npa, npa_len) ||
1333 !CBB_flush(out)) {
1334 return 0;
1335 }
1336
1337 return 1;
1338}
1339
1340
Adam Langleyab8d87d2015-07-10 12:21:39 -07001341/* Signed certificate timestamps.
1342 *
1343 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1344
1345static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1346 if (!ssl->signed_cert_timestamps_enabled) {
1347 return 1;
1348 }
1349
1350 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1351 !CBB_add_u16(out, 0 /* length */)) {
1352 return 0;
1353 }
1354
1355 return 1;
1356}
1357
1358static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1359 CBS *contents) {
1360 if (contents == NULL) {
1361 return 1;
1362 }
1363
1364 /* If this is false then we should never have sent the SCT extension in the
1365 * ClientHello and thus this function should never have been called. */
1366 assert(ssl->signed_cert_timestamps_enabled);
1367
1368 if (CBS_len(contents) == 0) {
1369 *out_alert = SSL_AD_DECODE_ERROR;
1370 return 0;
1371 }
1372
1373 /* Session resumption uses the original session information. */
1374 if (!ssl->hit &&
1375 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1376 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1377 *out_alert = SSL_AD_INTERNAL_ERROR;
1378 return 0;
1379 }
1380
1381 return 1;
1382}
1383
1384static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1385 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001386 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001387}
1388
1389static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001390 /* The extension shouldn't be sent when resuming sessions. */
1391 if (ssl->hit ||
1392 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001393 return 1;
1394 }
1395
1396 CBB contents;
1397 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1398 CBB_add_u16_length_prefixed(out, &contents) &&
1399 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1400 ssl->ctx->signed_cert_timestamp_list_length) &&
1401 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001402}
1403
1404
Adam Langleyf18e4532015-07-10 13:39:53 -07001405/* Application-level Protocol Negotiation.
1406 *
1407 * https://tools.ietf.org/html/rfc7301 */
1408
1409static void ext_alpn_init(SSL *ssl) {
1410 OPENSSL_free(ssl->s3->alpn_selected);
1411 ssl->s3->alpn_selected = NULL;
1412}
1413
1414static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1415 if (ssl->alpn_client_proto_list == NULL ||
1416 ssl->s3->initial_handshake_complete) {
1417 return 1;
1418 }
1419
1420 CBB contents, proto_list;
1421 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1422 !CBB_add_u16_length_prefixed(out, &contents) ||
1423 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1424 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1425 ssl->alpn_client_proto_list_len) ||
1426 !CBB_flush(out)) {
1427 return 0;
1428 }
1429
1430 return 1;
1431}
1432
1433static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1434 CBS *contents) {
1435 if (contents == NULL) {
1436 return 1;
1437 }
1438
1439 assert(!ssl->s3->initial_handshake_complete);
1440 assert(ssl->alpn_client_proto_list != NULL);
1441
David Benjamin76c2efc2015-08-31 14:24:29 -04001442 if (ssl->s3->next_proto_neg_seen) {
1443 /* NPN and ALPN may not be negotiated in the same connection. */
1444 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1445 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1446 return 0;
1447 }
1448
Adam Langleyf18e4532015-07-10 13:39:53 -07001449 /* The extension data consists of a ProtocolNameList which must have
1450 * exactly one ProtocolName. Each of these is length-prefixed. */
1451 CBS protocol_name_list, protocol_name;
1452 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1453 CBS_len(contents) != 0 ||
1454 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1455 /* Empty protocol names are forbidden. */
1456 CBS_len(&protocol_name) == 0 ||
1457 CBS_len(&protocol_name_list) != 0) {
1458 return 0;
1459 }
1460
1461 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1462 &ssl->s3->alpn_selected_len)) {
1463 *out_alert = SSL_AD_INTERNAL_ERROR;
1464 return 0;
1465 }
1466
1467 return 1;
1468}
1469
1470static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1471 CBS *contents) {
1472 if (contents == NULL) {
1473 return 1;
1474 }
1475
1476 if (ssl->ctx->alpn_select_cb == NULL ||
1477 ssl->s3->initial_handshake_complete) {
1478 return 1;
1479 }
1480
1481 /* ALPN takes precedence over NPN. */
1482 ssl->s3->next_proto_neg_seen = 0;
1483
1484 CBS protocol_name_list;
1485 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1486 CBS_len(contents) != 0 ||
1487 CBS_len(&protocol_name_list) < 2) {
1488 return 0;
1489 }
1490
1491 /* Validate the protocol list. */
1492 CBS protocol_name_list_copy = protocol_name_list;
1493 while (CBS_len(&protocol_name_list_copy) > 0) {
1494 CBS protocol_name;
1495
1496 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1497 /* Empty protocol names are forbidden. */
1498 CBS_len(&protocol_name) == 0) {
1499 return 0;
1500 }
1501 }
1502
1503 const uint8_t *selected;
1504 uint8_t selected_len;
1505 if (ssl->ctx->alpn_select_cb(
1506 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1507 CBS_len(&protocol_name_list),
1508 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1509 OPENSSL_free(ssl->s3->alpn_selected);
1510 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1511 if (ssl->s3->alpn_selected == NULL) {
1512 *out_alert = SSL_AD_INTERNAL_ERROR;
1513 return 0;
1514 }
1515 ssl->s3->alpn_selected_len = selected_len;
1516 }
1517
1518 return 1;
1519}
1520
1521static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1522 if (ssl->s3->alpn_selected == NULL) {
1523 return 1;
1524 }
1525
1526 CBB contents, proto_list, proto;
1527 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1528 !CBB_add_u16_length_prefixed(out, &contents) ||
1529 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1530 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001531 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1532 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001533 !CBB_flush(out)) {
1534 return 0;
1535 }
1536
1537 return 1;
1538}
1539
1540
Adam Langley49c7af12015-07-10 14:33:46 -07001541/* Channel ID.
1542 *
1543 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1544
1545static void ext_channel_id_init(SSL *ssl) {
1546 ssl->s3->tlsext_channel_id_valid = 0;
1547}
1548
1549static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1550 if (!ssl->tlsext_channel_id_enabled ||
1551 SSL_IS_DTLS(ssl)) {
1552 return 1;
1553 }
1554
1555 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1556 !CBB_add_u16(out, 0 /* length */)) {
1557 return 0;
1558 }
1559
1560 return 1;
1561}
1562
1563static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1564 CBS *contents) {
1565 if (contents == NULL) {
1566 return 1;
1567 }
1568
1569 assert(!SSL_IS_DTLS(ssl));
1570 assert(ssl->tlsext_channel_id_enabled);
1571
1572 if (CBS_len(contents) != 0) {
1573 return 0;
1574 }
1575
1576 ssl->s3->tlsext_channel_id_valid = 1;
1577 return 1;
1578}
1579
1580static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1581 CBS *contents) {
1582 if (contents == NULL ||
1583 !ssl->tlsext_channel_id_enabled ||
1584 SSL_IS_DTLS(ssl)) {
1585 return 1;
1586 }
1587
1588 if (CBS_len(contents) != 0) {
1589 return 0;
1590 }
1591
1592 ssl->s3->tlsext_channel_id_valid = 1;
1593 return 1;
1594}
1595
1596static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1597 if (!ssl->s3->tlsext_channel_id_valid) {
1598 return 1;
1599 }
1600
1601 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1602 !CBB_add_u16(out, 0 /* length */)) {
1603 return 0;
1604 }
1605
1606 return 1;
1607}
1608
Adam Langley391250d2015-07-15 19:06:07 -07001609
1610/* Secure Real-time Transport Protocol (SRTP) extension.
1611 *
1612 * https://tools.ietf.org/html/rfc5764 */
1613
Adam Langley391250d2015-07-15 19:06:07 -07001614
1615static void ext_srtp_init(SSL *ssl) {
1616 ssl->srtp_profile = NULL;
1617}
1618
1619static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1620 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1621 if (profiles == NULL) {
1622 return 1;
1623 }
1624 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1625 if (num_profiles == 0) {
1626 return 1;
1627 }
1628
1629 CBB contents, profile_ids;
1630 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1631 !CBB_add_u16_length_prefixed(out, &contents) ||
1632 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1633 return 0;
1634 }
1635
1636 size_t i;
1637 for (i = 0; i < num_profiles; i++) {
1638 if (!CBB_add_u16(&profile_ids,
1639 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1640 return 0;
1641 }
1642 }
1643
1644 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1645 !CBB_flush(out)) {
1646 return 0;
1647 }
1648
1649 return 1;
1650}
1651
1652static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1653 CBS *contents) {
1654 if (contents == NULL) {
1655 return 1;
1656 }
1657
1658 /* The extension consists of a u16-prefixed profile ID list containing a
1659 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1660 *
1661 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1662 CBS profile_ids, srtp_mki;
1663 uint16_t profile_id;
1664 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1665 !CBS_get_u16(&profile_ids, &profile_id) ||
1666 CBS_len(&profile_ids) != 0 ||
1667 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1668 CBS_len(contents) != 0) {
1669 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1670 return 0;
1671 }
1672
1673 if (CBS_len(&srtp_mki) != 0) {
1674 /* Must be no MKI, since we never offer one. */
1675 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1676 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1677 return 0;
1678 }
1679
1680 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1681
1682 /* Check to see if the server gave us something we support (and presumably
1683 * offered). */
1684 size_t i;
1685 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1686 const SRTP_PROTECTION_PROFILE *profile =
1687 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1688
1689 if (profile->id == profile_id) {
1690 ssl->srtp_profile = profile;
1691 return 1;
1692 }
1693 }
1694
1695 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1696 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1697 return 0;
1698}
1699
1700static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1701 CBS *contents) {
1702 if (contents == NULL) {
1703 return 1;
1704 }
1705
1706 CBS profile_ids, srtp_mki;
1707 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1708 CBS_len(&profile_ids) < 2 ||
1709 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1710 CBS_len(contents) != 0) {
1711 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1712 return 0;
1713 }
1714 /* Discard the MKI value for now. */
1715
1716 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1717 SSL_get_srtp_profiles(ssl);
1718
1719 /* Pick the server's most preferred profile. */
1720 size_t i;
1721 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1722 const SRTP_PROTECTION_PROFILE *server_profile =
1723 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1724
1725 CBS profile_ids_tmp;
1726 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1727
1728 while (CBS_len(&profile_ids_tmp) > 0) {
1729 uint16_t profile_id;
1730 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1731 return 0;
1732 }
1733
1734 if (server_profile->id == profile_id) {
1735 ssl->srtp_profile = server_profile;
1736 return 1;
1737 }
1738 }
1739 }
1740
1741 return 1;
1742}
1743
1744static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1745 if (ssl->srtp_profile == NULL) {
1746 return 1;
1747 }
1748
1749 CBB contents, profile_ids;
1750 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1751 !CBB_add_u16_length_prefixed(out, &contents) ||
1752 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1753 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1754 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1755 !CBB_flush(out)) {
1756 return 0;
1757 }
1758
1759 return 1;
1760}
1761
Adam Langleybdd5d662015-07-20 16:19:08 -07001762
1763/* EC point formats.
1764 *
1765 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1766
1767static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1768 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1769 return 0;
1770 }
1771
1772 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1773
1774 size_t i;
1775 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1776 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1777
1778 const uint32_t alg_k = cipher->algorithm_mkey;
1779 const uint32_t alg_a = cipher->algorithm_auth;
1780 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1781 return 1;
1782 }
1783 }
1784
1785 return 0;
1786}
1787
Adam Langleybdd5d662015-07-20 16:19:08 -07001788static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001789 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001790 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1791 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001792 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1793 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001794 !CBB_flush(out)) {
1795 return 0;
1796 }
1797
1798 return 1;
1799}
1800
1801static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1802 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1803 return 1;
1804 }
1805
1806 return ext_ec_point_add_extension(ssl, out);
1807}
1808
1809static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1810 CBS *contents) {
1811 if (contents == NULL) {
1812 return 1;
1813 }
1814
1815 CBS ec_point_format_list;
1816 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1817 CBS_len(contents) != 0) {
1818 return 0;
1819 }
1820
David Benjaminfc059942015-07-30 23:01:59 -04001821 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1822 * point format. */
1823 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1824 CBS_len(&ec_point_format_list)) == NULL) {
1825 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001826 return 0;
1827 }
1828
1829 return 1;
1830}
1831
1832static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1833 CBS *contents) {
1834 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1835}
1836
1837static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1838 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1839 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001840 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001841
1842 if (!using_ecc) {
1843 return 1;
1844 }
1845
1846 return ext_ec_point_add_extension(ssl, out);
1847}
1848
Adam Langley273d49c2015-07-20 16:38:52 -07001849
1850/* EC supported curves.
1851 *
1852 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1853
1854static void ext_ec_curves_init(SSL *ssl) {
1855 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1856 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1857 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
1858}
1859
1860static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
1861 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1862 return 1;
1863 }
1864
1865 CBB contents, curves_bytes;
1866 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
1867 !CBB_add_u16_length_prefixed(out, &contents) ||
1868 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
1869 return 0;
1870 }
1871
1872 const uint16_t *curves;
1873 size_t curves_len;
1874 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
1875
1876 size_t i;
1877 for (i = 0; i < curves_len; i++) {
1878 if (!CBB_add_u16(&curves_bytes, curves[i])) {
1879 return 0;
1880 }
1881 }
1882
1883 return CBB_flush(out);
1884}
1885
1886static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1887 CBS *contents) {
1888 /* This extension is not expected to be echoed by servers and is ignored. */
1889 return 1;
1890}
1891
1892static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1893 CBS *contents) {
1894 if (contents == NULL) {
1895 return 1;
1896 }
1897
1898 CBS elliptic_curve_list;
1899 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
1900 CBS_len(&elliptic_curve_list) == 0 ||
1901 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
1902 CBS_len(contents) != 0) {
1903 return 0;
1904 }
1905
Brian Smith5ba06892016-02-07 09:36:04 -10001906 ssl->s3->tmp.peer_ellipticcurvelist = OPENSSL_malloc(CBS_len(&elliptic_curve_list));
Adam Langley273d49c2015-07-20 16:38:52 -07001907 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
1908 *out_alert = SSL_AD_INTERNAL_ERROR;
1909 return 0;
1910 }
1911
1912 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
1913 size_t i;
1914 for (i = 0; i < num_curves; i++) {
1915 if (!CBS_get_u16(&elliptic_curve_list,
1916 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
1917 goto err;
1918 }
1919 }
1920
1921 assert(CBS_len(&elliptic_curve_list) == 0);
1922 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
1923
1924 return 1;
1925
1926err:
1927 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1928 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1929 *out_alert = SSL_AD_INTERNAL_ERROR;
1930 return 0;
1931}
1932
1933static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
1934 /* Servers don't echo this extension. */
1935 return 1;
1936}
1937
1938
Adam Langley614c66a2015-06-12 15:26:58 -07001939/* kExtensions contains all the supported extensions. */
1940static const struct tls_extension kExtensions[] = {
1941 {
Adam Langley5021b222015-06-12 18:27:58 -07001942 /* The renegotiation extension must always be at index zero because the
1943 * |received| and |sent| bitsets need to be tweaked when the "extension" is
1944 * sent as an SCSV. */
1945 TLSEXT_TYPE_renegotiate,
1946 NULL,
1947 ext_ri_add_clienthello,
1948 ext_ri_parse_serverhello,
1949 ext_ri_parse_clienthello,
1950 ext_ri_add_serverhello,
1951 },
1952 {
Adam Langley614c66a2015-06-12 15:26:58 -07001953 TLSEXT_TYPE_server_name,
1954 ext_sni_init,
1955 ext_sni_add_clienthello,
1956 ext_sni_parse_serverhello,
1957 ext_sni_parse_clienthello,
1958 ext_sni_add_serverhello,
1959 },
Adam Langley0a056712015-07-01 15:03:33 -07001960 {
1961 TLSEXT_TYPE_extended_master_secret,
1962 ext_ems_init,
1963 ext_ems_add_clienthello,
1964 ext_ems_parse_serverhello,
1965 ext_ems_parse_clienthello,
1966 ext_ems_add_serverhello,
1967 },
Adam Langley9b05bc52015-07-01 15:25:33 -07001968 {
1969 TLSEXT_TYPE_session_ticket,
1970 NULL,
1971 ext_ticket_add_clienthello,
1972 ext_ticket_parse_serverhello,
1973 ext_ticket_parse_clienthello,
1974 ext_ticket_add_serverhello,
1975 },
Adam Langley2e857bd2015-07-01 16:09:19 -07001976 {
1977 TLSEXT_TYPE_signature_algorithms,
1978 NULL,
1979 ext_sigalgs_add_clienthello,
1980 ext_sigalgs_parse_serverhello,
1981 ext_sigalgs_parse_clienthello,
1982 ext_sigalgs_add_serverhello,
1983 },
Adam Langleybb0bd042015-07-01 16:21:03 -07001984 {
1985 TLSEXT_TYPE_status_request,
1986 ext_ocsp_init,
1987 ext_ocsp_add_clienthello,
1988 ext_ocsp_parse_serverhello,
1989 ext_ocsp_parse_clienthello,
1990 ext_ocsp_add_serverhello,
1991 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07001992 {
1993 TLSEXT_TYPE_next_proto_neg,
1994 ext_npn_init,
1995 ext_npn_add_clienthello,
1996 ext_npn_parse_serverhello,
1997 ext_npn_parse_clienthello,
1998 ext_npn_add_serverhello,
1999 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002000 {
2001 TLSEXT_TYPE_certificate_timestamp,
2002 NULL,
2003 ext_sct_add_clienthello,
2004 ext_sct_parse_serverhello,
2005 ext_sct_parse_clienthello,
2006 ext_sct_add_serverhello,
2007 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002008 {
2009 TLSEXT_TYPE_application_layer_protocol_negotiation,
2010 ext_alpn_init,
2011 ext_alpn_add_clienthello,
2012 ext_alpn_parse_serverhello,
2013 ext_alpn_parse_clienthello,
2014 ext_alpn_add_serverhello,
2015 },
Adam Langley49c7af12015-07-10 14:33:46 -07002016 {
2017 TLSEXT_TYPE_channel_id,
2018 ext_channel_id_init,
2019 ext_channel_id_add_clienthello,
2020 ext_channel_id_parse_serverhello,
2021 ext_channel_id_parse_clienthello,
2022 ext_channel_id_add_serverhello,
2023 },
Adam Langley391250d2015-07-15 19:06:07 -07002024 {
2025 TLSEXT_TYPE_srtp,
2026 ext_srtp_init,
2027 ext_srtp_add_clienthello,
2028 ext_srtp_parse_serverhello,
2029 ext_srtp_parse_clienthello,
2030 ext_srtp_add_serverhello,
2031 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002032 {
2033 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002034 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002035 ext_ec_point_add_clienthello,
2036 ext_ec_point_parse_serverhello,
2037 ext_ec_point_parse_clienthello,
2038 ext_ec_point_add_serverhello,
2039 },
Adam Langley273d49c2015-07-20 16:38:52 -07002040 {
2041 TLSEXT_TYPE_elliptic_curves,
2042 ext_ec_curves_init,
2043 ext_ec_curves_add_clienthello,
2044 ext_ec_curves_parse_serverhello,
2045 ext_ec_curves_parse_clienthello,
2046 ext_ec_curves_add_serverhello,
2047 },
Adam Langley614c66a2015-06-12 15:26:58 -07002048};
2049
2050#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2051
Adam Langley4cfa96b2015-07-01 11:56:55 -07002052OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2053 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002054 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002055OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2056 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2057 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002058 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002059
Adam Langley614c66a2015-06-12 15:26:58 -07002060static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2061 uint16_t value) {
2062 unsigned i;
2063 for (i = 0; i < kNumExtensions; i++) {
2064 if (kExtensions[i].value == value) {
2065 *out_index = i;
2066 return &kExtensions[i];
2067 }
2068 }
2069
2070 return NULL;
2071}
2072
Adam Langley09505632015-07-30 18:10:13 -07002073int SSL_extension_supported(unsigned extension_value) {
2074 uint32_t index;
2075 return extension_value == TLSEXT_TYPE_padding ||
2076 tls_extension_find(&index, extension_value) != NULL;
2077}
2078
David Benjamine8d53502015-10-10 14:13:23 -04002079int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002080 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002081 if (ssl->client_version == SSL3_VERSION &&
2082 !ssl->s3->send_connection_binding) {
2083 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002084 }
Adam Langley95c29f32014-06-20 12:00:00 -07002085
David Benjamine8d53502015-10-10 14:13:23 -04002086 CBB extensions;
2087 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002088 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002089 }
Adam Langley95c29f32014-06-20 12:00:00 -07002090
David Benjamine8d53502015-10-10 14:13:23 -04002091 ssl->s3->tmp.extensions.sent = 0;
2092 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002093
Adam Langley614c66a2015-06-12 15:26:58 -07002094 size_t i;
2095 for (i = 0; i < kNumExtensions; i++) {
2096 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002097 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002098 }
2099 }
Adam Langley95c29f32014-06-20 12:00:00 -07002100
Adam Langley614c66a2015-06-12 15:26:58 -07002101 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002102 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002103 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002104 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2105 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2106 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002107 }
Adam Langley95c29f32014-06-20 12:00:00 -07002108
Adam Langley33ad2b52015-07-20 17:43:53 -07002109 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002110 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002111 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002112 }
Adam Langley75712922014-10-10 16:23:43 -07002113
David Benjamine8d53502015-10-10 14:13:23 -04002114 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002115 goto err;
2116 }
2117
David Benjamine8d53502015-10-10 14:13:23 -04002118 if (!SSL_IS_DTLS(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002119 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002120 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002121 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002122 *
2123 * NB: because this code works out the length of all existing extensions
2124 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002125 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002126 /* Extensions take at least four bytes to encode. Always include least
2127 * one byte of data if including the extension. WebSphere Application
2128 * Server 7.0 is intolerant to the last extension being zero-length. */
2129 if (padding_len >= 4 + 1) {
2130 padding_len -= 4;
2131 } else {
2132 padding_len = 1;
2133 }
Adam Langley95c29f32014-06-20 12:00:00 -07002134
Adam Langley33ad2b52015-07-20 17:43:53 -07002135 uint8_t *padding_bytes;
2136 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2137 !CBB_add_u16(&extensions, padding_len) ||
2138 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2139 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002140 }
Adam Langley75712922014-10-10 16:23:43 -07002141
Adam Langley33ad2b52015-07-20 17:43:53 -07002142 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002143 }
2144 }
Adam Langley75712922014-10-10 16:23:43 -07002145
David Benjamina01deee2015-12-08 18:56:31 -05002146 /* Discard empty extensions blocks. */
2147 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002148 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002149 }
2150
David Benjamine8d53502015-10-10 14:13:23 -04002151 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002152
2153err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002154 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002155 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002156}
Adam Langley95c29f32014-06-20 12:00:00 -07002157
David Benjamin56380462015-10-10 14:59:09 -04002158int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002159 CBB extensions;
2160 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002161 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002162 }
2163
2164 unsigned i;
2165 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002166 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002167 /* Don't send extensions that were not received. */
2168 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002169 }
Adam Langley95c29f32014-06-20 12:00:00 -07002170
David Benjamin56380462015-10-10 14:59:09 -04002171 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002172 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2173 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2174 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002175 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002176 }
Adam Langley95c29f32014-06-20 12:00:00 -07002177
David Benjamin56380462015-10-10 14:59:09 -04002178 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002179 goto err;
2180 }
2181
David Benjamina01deee2015-12-08 18:56:31 -05002182 /* Discard empty extensions blocks. */
2183 if (CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002184 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002185 }
2186
David Benjamin56380462015-10-10 14:59:09 -04002187 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002188
2189err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002190 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002191 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002192}
Adam Langley95c29f32014-06-20 12:00:00 -07002193
David Benjamin0d56f882015-12-19 17:05:56 -05002194static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002195 size_t i;
2196 for (i = 0; i < kNumExtensions; i++) {
2197 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002198 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002199 }
2200 }
2201
David Benjamin0d56f882015-12-19 17:05:56 -05002202 ssl->s3->tmp.extensions.received = 0;
2203 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002204 /* The renegotiation extension must always be at index zero because the
2205 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2206 * sent as an SCSV. */
2207 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002208
Adam Langleyfcf25832014-12-18 17:42:32 -08002209 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002210 if (CBS_len(cbs) != 0) {
2211 /* Decode the extensions block and check it is valid. */
2212 CBS extensions;
2213 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2214 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002215 *out_alert = SSL_AD_DECODE_ERROR;
2216 return 0;
2217 }
Adam Langley95c29f32014-06-20 12:00:00 -07002218
Adam Langley33ad2b52015-07-20 17:43:53 -07002219 while (CBS_len(&extensions) != 0) {
2220 uint16_t type;
2221 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002222
Adam Langley33ad2b52015-07-20 17:43:53 -07002223 /* Decode the next extension. */
2224 if (!CBS_get_u16(&extensions, &type) ||
2225 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2226 *out_alert = SSL_AD_DECODE_ERROR;
2227 return 0;
2228 }
2229
David Benjaminc7ce9772015-10-09 19:32:41 -04002230 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2231 * ambiguous. Ignore all but the renegotiation_info extension. */
David Benjamin0d56f882015-12-19 17:05:56 -05002232 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
David Benjaminc7ce9772015-10-09 19:32:41 -04002233 continue;
2234 }
2235
Adam Langley33ad2b52015-07-20 17:43:53 -07002236 unsigned ext_index;
2237 const struct tls_extension *const ext =
2238 tls_extension_find(&ext_index, type);
2239
2240 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002241 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002242 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2243 return 0;
2244 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002245 continue;
2246 }
2247
David Benjamin0d56f882015-12-19 17:05:56 -05002248 ssl->s3->tmp.extensions.received |= (1u << ext_index);
Adam Langley614c66a2015-06-12 15:26:58 -07002249 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002250 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
Adam Langley614c66a2015-06-12 15:26:58 -07002251 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002252 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2253 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002254 return 0;
2255 }
Adam Langley614c66a2015-06-12 15:26:58 -07002256 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002257 }
Adam Langley75712922014-10-10 16:23:43 -07002258
Adam Langley614c66a2015-06-12 15:26:58 -07002259 for (i = 0; i < kNumExtensions; i++) {
David Benjamin0d56f882015-12-19 17:05:56 -05002260 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002261 /* Extension wasn't observed so call the callback with a NULL
2262 * parameter. */
2263 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002264 if (!kExtensions[i].parse_clienthello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002265 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2266 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002267 *out_alert = alert;
2268 return 0;
2269 }
2270 }
2271 }
2272
Adam Langleyfcf25832014-12-18 17:42:32 -08002273 return 1;
2274}
Adam Langley95c29f32014-06-20 12:00:00 -07002275
David Benjamin0d56f882015-12-19 17:05:56 -05002276int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002277 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002278 if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) {
2279 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002280 return 0;
2281 }
Adam Langley95c29f32014-06-20 12:00:00 -07002282
David Benjamin0d56f882015-12-19 17:05:56 -05002283 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002284 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002285 return 0;
2286 }
Adam Langley95c29f32014-06-20 12:00:00 -07002287
Adam Langleyfcf25832014-12-18 17:42:32 -08002288 return 1;
2289}
Adam Langley95c29f32014-06-20 12:00:00 -07002290
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002291OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2292
David Benjamin0d56f882015-12-19 17:05:56 -05002293static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002294 uint32_t received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002295
Adam Langley33ad2b52015-07-20 17:43:53 -07002296 if (CBS_len(cbs) != 0) {
2297 /* Decode the extensions block and check it is valid. */
2298 CBS extensions;
2299 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2300 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002301 *out_alert = SSL_AD_DECODE_ERROR;
2302 return 0;
2303 }
Adam Langley95c29f32014-06-20 12:00:00 -07002304
Adam Langley614c66a2015-06-12 15:26:58 -07002305
Adam Langley33ad2b52015-07-20 17:43:53 -07002306 while (CBS_len(&extensions) != 0) {
2307 uint16_t type;
2308 CBS extension;
2309
2310 /* Decode the next extension. */
2311 if (!CBS_get_u16(&extensions, &type) ||
2312 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2313 *out_alert = SSL_AD_DECODE_ERROR;
2314 return 0;
2315 }
2316
2317 unsigned ext_index;
2318 const struct tls_extension *const ext =
2319 tls_extension_find(&ext_index, type);
2320
Adam Langley09505632015-07-30 18:10:13 -07002321 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002322 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002323 return 0;
2324 }
2325 continue;
2326 }
2327
David Benjamin0d56f882015-12-19 17:05:56 -05002328 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) {
Adam Langley09505632015-07-30 18:10:13 -07002329 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002330 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002331 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002332 *out_alert = SSL_AD_DECODE_ERROR;
2333 return 0;
2334 }
David Benjamin03973092014-06-24 23:27:17 -04002335
Adam Langley614c66a2015-06-12 15:26:58 -07002336 received |= (1u << ext_index);
2337
2338 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002339 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002340 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2341 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002342 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002343 return 0;
2344 }
Adam Langley614c66a2015-06-12 15:26:58 -07002345 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002346 }
Adam Langley95c29f32014-06-20 12:00:00 -07002347
Adam Langley33ad2b52015-07-20 17:43:53 -07002348 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002349 for (i = 0; i < kNumExtensions; i++) {
2350 if (!(received & (1u << i))) {
2351 /* Extension wasn't observed so call the callback with a NULL
2352 * parameter. */
2353 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002354 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002355 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2356 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002357 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002358 return 0;
2359 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002360 }
2361 }
Adam Langley95c29f32014-06-20 12:00:00 -07002362
Adam Langleyfcf25832014-12-18 17:42:32 -08002363 return 1;
2364}
Adam Langley95c29f32014-06-20 12:00:00 -07002365
David Benjamin0d56f882015-12-19 17:05:56 -05002366static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002367 int ret = SSL_TLSEXT_ERR_NOACK;
2368 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002369
David Benjamin78f8aab2016-03-10 16:33:58 -05002370 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002371 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002372 ssl->ctx->tlsext_servername_arg);
2373 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002374 ret = ssl->initial_ctx->tlsext_servername_callback(
2375 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002376 }
Adam Langley95c29f32014-06-20 12:00:00 -07002377
Adam Langleyfcf25832014-12-18 17:42:32 -08002378 switch (ret) {
2379 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002380 ssl3_send_alert(ssl, SSL3_AL_FATAL, 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_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002384 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
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 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002388 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002389 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002390
Adam Langleyfcf25832014-12-18 17:42:32 -08002391 default:
2392 return 1;
2393 }
2394}
Adam Langleyed8270a2014-09-02 13:52:56 -07002395
David Benjamin0d56f882015-12-19 17:05:56 -05002396static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002397 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002398 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002399
David Benjamin78f8aab2016-03-10 16:33:58 -05002400 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002401 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002402 ssl->ctx->tlsext_servername_arg);
2403 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002404 ret = ssl->initial_ctx->tlsext_servername_callback(
2405 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002406 }
Adam Langley95c29f32014-06-20 12:00:00 -07002407
Adam Langleyfcf25832014-12-18 17:42:32 -08002408 switch (ret) {
2409 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002410 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002411 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002412
Adam Langleyfcf25832014-12-18 17:42:32 -08002413 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002414 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002415 return 1;
2416
2417 default:
2418 return 1;
2419 }
2420}
2421
David Benjamin0d56f882015-12-19 17:05:56 -05002422int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002423 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002424 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2425 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002426 return 0;
2427 }
2428
David Benjamin0d56f882015-12-19 17:05:56 -05002429 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002430 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002431 return 0;
2432 }
2433
2434 return 1;
2435}
Adam Langley95c29f32014-06-20 12:00:00 -07002436
David Benjamine3aa1d92015-06-16 15:34:50 -04002437int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002438 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002439 size_t ticket_len, const uint8_t *session_id,
2440 size_t session_id_len) {
2441 int ret = 1; /* Most errors are non-fatal. */
2442 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2443 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002444
David Benjamine3aa1d92015-06-16 15:34:50 -04002445 HMAC_CTX hmac_ctx;
2446 HMAC_CTX_init(&hmac_ctx);
2447 EVP_CIPHER_CTX cipher_ctx;
2448 EVP_CIPHER_CTX_init(&cipher_ctx);
2449
David Benjaminef1b0092015-11-21 14:05:44 -05002450 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002451 *out_session = NULL;
2452
2453 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2454 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002455 }
2456
David Benjaminadcc3952015-04-26 13:07:57 -04002457 /* Ensure there is room for the key name and the largest IV
2458 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2459 * the maximum IV length should be well under the minimum size for the
2460 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002461 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2462 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002463 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002464 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002465
David Benjamine3aa1d92015-06-16 15:34:50 -04002466 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002467 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2468 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2469 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002470 if (cb_ret < 0) {
2471 ret = 0;
2472 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002473 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002474 if (cb_ret == 0) {
2475 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002476 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002477 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002478 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002479 }
2480 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002481 /* Check the key name matches. */
2482 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2483 SSL_TICKET_KEY_NAME_LEN) != 0) {
2484 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002485 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002486 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2487 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002488 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002489 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2490 ssl_ctx->tlsext_tick_aes_key, iv)) {
2491 ret = 0;
2492 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002493 }
2494 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002495 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002496
David Benjamine3aa1d92015-06-16 15:34:50 -04002497 /* Check the MAC at the end of the ticket. */
2498 uint8_t mac[EVP_MAX_MD_SIZE];
2499 size_t mac_len = HMAC_size(&hmac_ctx);
2500 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002501 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002502 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002503 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002504 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2505 HMAC_Final(&hmac_ctx, mac, NULL);
2506 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2507 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002508 }
2509
David Benjamine3aa1d92015-06-16 15:34:50 -04002510 /* Decrypt the session data. */
2511 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2512 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2513 mac_len;
2514 plaintext = OPENSSL_malloc(ciphertext_len);
2515 if (plaintext == NULL) {
2516 ret = 0;
2517 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002518 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002519 if (ciphertext_len >= INT_MAX) {
2520 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002521 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002522 int len1, len2;
2523 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2524 (int)ciphertext_len) ||
2525 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2526 ERR_clear_error(); /* Don't leave an error on the queue. */
2527 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002528 }
2529
David Benjamine3aa1d92015-06-16 15:34:50 -04002530 /* Decode the session. */
2531 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2532 if (session == NULL) {
2533 ERR_clear_error(); /* Don't leave an error on the queue. */
2534 goto done;
2535 }
2536
2537 /* Copy the client's session ID into the new session, to denote the ticket has
2538 * been accepted. */
2539 memcpy(session->session_id, session_id, session_id_len);
2540 session->session_id_length = session_id_len;
2541
2542 *out_session = session;
2543
2544done:
2545 OPENSSL_free(plaintext);
2546 HMAC_CTX_cleanup(&hmac_ctx);
2547 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2548 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002549}
Adam Langley95c29f32014-06-20 12:00:00 -07002550
2551/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002552typedef struct {
2553 int nid;
2554 int id;
2555} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002556
David Benjamin72f7e212016-01-29 15:28:58 -05002557static const tls12_lookup tls12_md[] = {
2558 {NID_sha1, TLSEXT_hash_sha1},
2559 {NID_sha256, TLSEXT_hash_sha256},
2560 {NID_sha384, TLSEXT_hash_sha384},
2561 {NID_sha512, TLSEXT_hash_sha512},
2562};
Adam Langley95c29f32014-06-20 12:00:00 -07002563
Adam Langleyfcf25832014-12-18 17:42:32 -08002564static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2565 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002566
Adam Langleyfcf25832014-12-18 17:42:32 -08002567static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2568 size_t i;
2569 for (i = 0; i < tlen; i++) {
2570 if (table[i].nid == nid) {
2571 return table[i].id;
2572 }
2573 }
Adam Langley95c29f32014-06-20 12:00:00 -07002574
Adam Langleyfcf25832014-12-18 17:42:32 -08002575 return -1;
2576}
Adam Langley95c29f32014-06-20 12:00:00 -07002577
David Benjaminb4d65fd2015-05-29 17:11:21 -04002578int tls12_get_sigid(int pkey_type) {
2579 return tls12_find_id(pkey_type, tls12_sig,
2580 sizeof(tls12_sig) / sizeof(tls12_lookup));
2581}
2582
David Benjaminfc825122015-12-18 01:57:43 -05002583int tls12_add_sigandhash(SSL *ssl, CBB *out, const EVP_MD *md) {
2584 int md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2585 sizeof(tls12_md) / sizeof(tls12_lookup));
2586 int sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langley95c29f32014-06-20 12:00:00 -07002587
David Benjaminfc825122015-12-18 01:57:43 -05002588 return md_id != -1 &&
2589 sig_id != -1 &&
2590 CBB_add_u8(out, (uint8_t)md_id) &&
2591 CBB_add_u8(out, (uint8_t)sig_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08002592}
2593
Adam Langleyfcf25832014-12-18 17:42:32 -08002594const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2595 switch (hash_alg) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002596 case TLSEXT_hash_sha1:
2597 return EVP_sha1();
2598
Adam Langleyfcf25832014-12-18 17:42:32 -08002599 case TLSEXT_hash_sha256:
2600 return EVP_sha256();
2601
2602 case TLSEXT_hash_sha384:
2603 return EVP_sha384();
2604
2605 case TLSEXT_hash_sha512:
2606 return EVP_sha512();
2607
2608 default:
2609 return NULL;
2610 }
2611}
Adam Langley95c29f32014-06-20 12:00:00 -07002612
David Benjaminec2f27d2014-11-13 19:17:25 -05002613/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2614 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002615static int tls12_get_pkey_type(uint8_t sig_alg) {
2616 switch (sig_alg) {
2617 case TLSEXT_signature_rsa:
2618 return EVP_PKEY_RSA;
2619
2620 case TLSEXT_signature_ecdsa:
2621 return EVP_PKEY_EC;
2622
2623 default:
2624 return -1;
2625 }
2626}
Adam Langley95c29f32014-06-20 12:00:00 -07002627
Steven Valdez0d62f262015-09-04 12:41:04 -04002628OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2,
2629 sizeof_tls_sigalgs_is_not_two);
Adam Langleyfcf25832014-12-18 17:42:32 -08002630
Steven Valdez0d62f262015-09-04 12:41:04 -04002631int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002632 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05002633 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002634 return 1;
2635 }
David Benjamincd996942014-07-20 16:23:51 -04002636
Steven Valdez0d62f262015-09-04 12:41:04 -04002637 CERT *const cert = ssl->cert;
2638 OPENSSL_free(cert->peer_sigalgs);
2639 cert->peer_sigalgs = NULL;
2640 cert->peer_sigalgslen = 0;
2641
2642 size_t num_sigalgs = CBS_len(in_sigalgs);
2643
2644 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002645 return 0;
2646 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002647 num_sigalgs /= 2;
2648
2649 /* supported_signature_algorithms in the certificate request is
2650 * allowed to be empty. */
2651 if (num_sigalgs == 0) {
2652 return 1;
2653 }
2654
2655 /* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two
2656 * (statically asserted above) and we just divided |num_sigalgs| by two. */
2657 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS));
2658 if (cert->peer_sigalgs == NULL) {
2659 return 0;
2660 }
2661 cert->peer_sigalgslen = num_sigalgs;
2662
2663 CBS sigalgs;
2664 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2665
2666 size_t i;
2667 for (i = 0; i < num_sigalgs; i++) {
2668 TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i];
2669 if (!CBS_get_u8(&sigalgs, &sigalg->rhash) ||
2670 !CBS_get_u8(&sigalgs, &sigalg->rsign)) {
2671 return 0;
2672 }
2673 }
Adam Langley95c29f32014-06-20 12:00:00 -07002674
Adam Langleyfcf25832014-12-18 17:42:32 -08002675 return 1;
2676}
David Benjaminec2f27d2014-11-13 19:17:25 -05002677
David Benjamind1d80782015-07-05 11:54:09 -04002678const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2679 CERT *cert = ssl->cert;
2680 int type = ssl_private_key_type(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -04002681 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002682
Steven Valdez0d62f262015-09-04 12:41:04 -04002683 static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
David Benjamin72f7e212016-01-29 15:28:58 -05002684 NID_sha1};
Steven Valdez0d62f262015-09-04 12:41:04 -04002685
2686 const int *digest_nids = kDefaultDigestList;
2687 size_t num_digest_nids =
2688 sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
2689 if (cert->digest_nids != NULL) {
2690 digest_nids = cert->digest_nids;
2691 num_digest_nids = cert->num_digest_nids;
2692 }
2693
2694 for (i = 0; i < num_digest_nids; i++) {
2695 const int digest_nid = digest_nids[i];
2696 for (j = 0; j < cert->peer_sigalgslen; j++) {
2697 const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash);
2698 if (md == NULL ||
2699 digest_nid != EVP_MD_type(md) ||
2700 tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) {
2701 continue;
2702 }
2703
2704 return md;
Adam Langleyfcf25832014-12-18 17:42:32 -08002705 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002706 }
Adam Langley95c29f32014-06-20 12:00:00 -07002707
Adam Langleyfcf25832014-12-18 17:42:32 -08002708 /* If no suitable digest may be found, default to SHA-1. */
2709 return EVP_sha1();
2710}
Adam Langley95c29f32014-06-20 12:00:00 -07002711
David Benjamind6a4ae92015-08-06 11:10:51 -04002712int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2713 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002714 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002715
2716 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002717 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2718 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002719 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002720
David Benjamind6a4ae92015-08-06 11:10:51 -04002721 static const char kClientIDMagic[] = "TLS Channel ID signature";
2722 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2723
2724 if (ssl->hit) {
2725 static const char kResumptionMagic[] = "Resumption";
2726 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2727 if (ssl->session->original_handshake_hash_len == 0) {
2728 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2729 goto err;
2730 }
2731 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2732 ssl->session->original_handshake_hash_len);
2733 }
2734
2735 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2736 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2737 sizeof(handshake_hash));
2738 if (handshake_hash_len < 0) {
2739 goto err;
2740 }
2741 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2742 unsigned len_u;
2743 EVP_DigestFinal_ex(&ctx, out, &len_u);
2744 *out_len = len_u;
2745
2746 ret = 1;
2747
2748err:
2749 EVP_MD_CTX_cleanup(&ctx);
2750 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002751}
Adam Langley1258b6a2014-06-20 12:00:00 -07002752
2753/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin0d56f882015-12-19 17:05:56 -05002754 * hashes in |ssl->session| so that Channel ID resumptions can sign that
2755 * data. */
2756int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002757 int digest_len;
2758 /* This function should never be called for a resumed session because the
2759 * handshake hashes that we wish to record are for the original, full
2760 * handshake. */
David Benjamin0d56f882015-12-19 17:05:56 -05002761 if (ssl->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002762 return -1;
2763 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002764
Adam Langleyfcf25832014-12-18 17:42:32 -08002765 digest_len =
David Benjamin0d56f882015-12-19 17:05:56 -05002766 tls1_handshake_digest(ssl, ssl->session->original_handshake_hash,
2767 sizeof(ssl->session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08002768 if (digest_len < 0) {
2769 return -1;
2770 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002771
David Benjamin0d56f882015-12-19 17:05:56 -05002772 ssl->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002773
Adam Langleyfcf25832014-12-18 17:42:32 -08002774 return 1;
2775}