blob: 7a336f17ef4566b7fa4484347b70683ad8951c95 [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>
Adam Langley95c29f32014-06-20 12:00:00 -0700113#include <stdio.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400114#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400115#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700116
David Benjamin03973092014-06-24 23:27:17 -0400117#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400118#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400119#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700120#include <openssl/evp.h>
121#include <openssl/hmac.h>
122#include <openssl/mem.h>
123#include <openssl/obj.h>
124#include <openssl/rand.h>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700125#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700126
David Benjamin2ee94aa2015-04-07 22:38:30 -0400127#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800128
129
David Benjamin0d56f882015-12-19 17:05:56 -0500130static int ssl_check_clienthello_tlsext(SSL *ssl);
131static int ssl_check_serverhello_tlsext(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700132
Adam Langleyfcf25832014-12-18 17:42:32 -0800133static int compare_uint16_t(const void *p1, const void *p2) {
134 uint16_t u1 = *((const uint16_t *)p1);
135 uint16_t u2 = *((const uint16_t *)p2);
136 if (u1 < u2) {
137 return -1;
138 } else if (u1 > u2) {
139 return 1;
140 } else {
141 return 0;
142 }
143}
David Benjamin35a7a442014-07-05 00:23:20 -0400144
Adam Langleyfcf25832014-12-18 17:42:32 -0800145/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
146 * more than one extension of the same type in a ClientHello or ServerHello.
147 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400148 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800149static int tls1_check_duplicate_extensions(const CBS *cbs) {
150 CBS extensions = *cbs;
151 size_t num_extensions = 0, i = 0;
152 uint16_t *extension_types = NULL;
153 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400154
Adam Langleyfcf25832014-12-18 17:42:32 -0800155 /* First pass: count the extensions. */
156 while (CBS_len(&extensions) > 0) {
157 uint16_t type;
158 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400159
Adam Langleyfcf25832014-12-18 17:42:32 -0800160 if (!CBS_get_u16(&extensions, &type) ||
161 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
162 goto done;
163 }
David Benjamin35a7a442014-07-05 00:23:20 -0400164
Adam Langleyfcf25832014-12-18 17:42:32 -0800165 num_extensions++;
166 }
David Benjamin35a7a442014-07-05 00:23:20 -0400167
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 if (num_extensions == 0) {
169 return 1;
170 }
David Benjamin9a373592014-07-25 04:27:53 -0400171
Adam Langleyfcf25832014-12-18 17:42:32 -0800172 extension_types =
173 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
174 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400175 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800176 goto done;
177 }
David Benjamin35a7a442014-07-05 00:23:20 -0400178
Adam Langleyfcf25832014-12-18 17:42:32 -0800179 /* Second pass: gather the extension types. */
180 extensions = *cbs;
181 for (i = 0; i < num_extensions; i++) {
182 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400183
Adam Langleyfcf25832014-12-18 17:42:32 -0800184 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
185 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
186 /* This should not happen. */
187 goto done;
188 }
189 }
190 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400191
Adam Langleyfcf25832014-12-18 17:42:32 -0800192 /* Sort the extensions and make sure there are no duplicates. */
193 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
194 for (i = 1; i < num_extensions; i++) {
195 if (extension_types[i - 1] == extension_types[i]) {
196 goto done;
197 }
198 }
David Benjamin35a7a442014-07-05 00:23:20 -0400199
Adam Langleyfcf25832014-12-18 17:42:32 -0800200 ret = 1;
201
David Benjamin35a7a442014-07-05 00:23:20 -0400202done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400203 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800204 return ret;
205}
David Benjamin35a7a442014-07-05 00:23:20 -0400206
Adam Langleyfcf25832014-12-18 17:42:32 -0800207char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
208 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400209
Adam Langleyfcf25832014-12-18 17:42:32 -0800210 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700211
Adam Langleyfcf25832014-12-18 17:42:32 -0800212 if (/* Skip client version. */
213 !CBS_skip(&client_hello, 2) ||
214 /* Skip client nonce. */
215 !CBS_skip(&client_hello, 32) ||
216 /* Extract session_id. */
217 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
218 return 0;
219 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700220
Adam Langleyfcf25832014-12-18 17:42:32 -0800221 ctx->session_id = CBS_data(&session_id);
222 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700223
Adam Langleyfcf25832014-12-18 17:42:32 -0800224 /* Skip past DTLS cookie */
225 if (SSL_IS_DTLS(ctx->ssl)) {
226 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700227
Adam Langleyfcf25832014-12-18 17:42:32 -0800228 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
229 return 0;
230 }
231 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700232
Adam Langleyfcf25832014-12-18 17:42:32 -0800233 /* Extract cipher_suites. */
234 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
235 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
236 return 0;
237 }
238 ctx->cipher_suites = CBS_data(&cipher_suites);
239 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700240
Adam Langleyfcf25832014-12-18 17:42:32 -0800241 /* Extract compression_methods. */
242 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
243 CBS_len(&compression_methods) < 1) {
244 return 0;
245 }
246 ctx->compression_methods = CBS_data(&compression_methods);
247 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 /* If the ClientHello ends here then it's valid, but doesn't have any
250 * extensions. (E.g. SSLv3.) */
251 if (CBS_len(&client_hello) == 0) {
252 ctx->extensions = NULL;
253 ctx->extensions_len = 0;
254 return 1;
255 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 /* Extract extensions and check it is valid. */
258 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
259 !tls1_check_duplicate_extensions(&extensions) ||
260 CBS_len(&client_hello) != 0) {
261 return 0;
262 }
263 ctx->extensions = CBS_data(&extensions);
264 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 return 1;
267}
Adam Langleydc9b1412014-06-20 12:00:00 -0700268
David Benjamind4c2bce2015-10-17 12:28:18 -0400269int SSL_early_callback_ctx_extension_get(
Adam Langleyfcf25832014-12-18 17:42:32 -0800270 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
271 const uint8_t **out_data, size_t *out_len) {
272 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700273
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700275
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 while (CBS_len(&extensions) != 0) {
277 uint16_t type;
278 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400279
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 /* Decode the next extension. */
281 if (!CBS_get_u16(&extensions, &type) ||
282 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
283 return 0;
284 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700285
Adam Langleyfcf25832014-12-18 17:42:32 -0800286 if (type == extension_type) {
287 *out_data = CBS_data(&extension);
288 *out_len = CBS_len(&extension);
289 return 1;
290 }
291 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700292
Adam Langleyfcf25832014-12-18 17:42:32 -0800293 return 0;
294}
Adam Langley95c29f32014-06-20 12:00:00 -0700295
Adam Langleyfcf25832014-12-18 17:42:32 -0800296static const uint16_t eccurves_default[] = {
David Benjaminfc6e5a72016-01-19 15:36:43 -0500297 SSL_CURVE_X25519,
David Benjamin4298d772015-12-19 00:18:25 -0500298 SSL_CURVE_SECP256R1,
299 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700300#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin4298d772015-12-19 00:18:25 -0500301 SSL_CURVE_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700302#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800303};
Adam Langley95c29f32014-06-20 12:00:00 -0700304
Adam Langleyfcf25832014-12-18 17:42:32 -0800305/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
306 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
307 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin0d56f882015-12-19 17:05:56 -0500308static void tls1_get_curvelist(SSL *ssl, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800309 const uint16_t **out_curve_ids,
310 size_t *out_curve_ids_len) {
311 if (get_peer_curves) {
David Benjamin55a43642015-04-20 14:45:55 -0400312 /* Only clients send a curve list, so this function is only called
313 * on the server. */
David Benjamin0d56f882015-12-19 17:05:56 -0500314 assert(ssl->server);
315 *out_curve_ids = ssl->s3->tmp.peer_ellipticcurvelist;
316 *out_curve_ids_len = ssl->s3->tmp.peer_ellipticcurvelist_length;
Adam Langleyfcf25832014-12-18 17:42:32 -0800317 return;
318 }
Adam Langley95c29f32014-06-20 12:00:00 -0700319
David Benjamin0d56f882015-12-19 17:05:56 -0500320 *out_curve_ids = ssl->tlsext_ellipticcurvelist;
321 *out_curve_ids_len = ssl->tlsext_ellipticcurvelist_length;
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 if (!*out_curve_ids) {
323 *out_curve_ids = eccurves_default;
324 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
325 }
326}
David Benjamined439582014-07-14 19:13:02 -0400327
David Benjamin4298d772015-12-19 00:18:25 -0500328int tls1_get_shared_curve(SSL *ssl, uint16_t *out_curve_id) {
David Benjamin55a43642015-04-20 14:45:55 -0400329 const uint16_t *curves, *peer_curves, *pref, *supp;
330 size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400331
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 /* Can't do anything on client side */
David Benjamin4298d772015-12-19 00:18:25 -0500333 if (ssl->server == 0) {
334 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 }
336
David Benjamin4298d772015-12-19 00:18:25 -0500337 tls1_get_curvelist(ssl, 0 /* local curves */, &curves, &curves_len);
338 tls1_get_curvelist(ssl, 1 /* peer curves */, &peer_curves, &peer_curves_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800339
David Benjamin55a43642015-04-20 14:45:55 -0400340 if (peer_curves_len == 0) {
341 /* Clients are not required to send a supported_curves extension. In this
342 * case, the server is free to pick any curve it likes. See RFC 4492,
David Benjamin4298d772015-12-19 00:18:25 -0500343 * section 4, paragraph 3.
344 *
345 * However, in the interests of compatibility, we will skip ECDH if the
346 * client didn't send an extension because we can't be sure that they'll
347 * support our favoured curve. */
348 return 0;
David Benjamin55a43642015-04-20 14:45:55 -0400349 }
350
David Benjamin4298d772015-12-19 00:18:25 -0500351 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
David Benjamin55a43642015-04-20 14:45:55 -0400352 pref = curves;
353 pref_len = curves_len;
354 supp = peer_curves;
355 supp_len = peer_curves_len;
356 } else {
357 pref = peer_curves;
358 pref_len = peer_curves_len;
359 supp = curves;
360 supp_len = curves_len;
361 }
362
363 for (i = 0; i < pref_len; i++) {
364 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 if (pref[i] == supp[j]) {
David Benjamin4298d772015-12-19 00:18:25 -0500366 *out_curve_id = pref[i];
367 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 }
369 }
370 }
371
David Benjamin4298d772015-12-19 00:18:25 -0500372 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800373}
Adam Langley95c29f32014-06-20 12:00:00 -0700374
David Benjamin072334d2014-07-13 16:24:27 -0400375int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 const int *curves, size_t ncurves) {
377 uint16_t *curve_ids;
378 size_t i;
379
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
381 if (curve_ids == NULL) {
382 return 0;
383 }
384
385 for (i = 0; i < ncurves; i++) {
David Benjamin4298d772015-12-19 00:18:25 -0500386 if (!ssl_nid_to_curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800387 OPENSSL_free(curve_ids);
388 return 0;
389 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 }
391
David Benjamin2755a3e2015-04-22 16:17:58 -0400392 OPENSSL_free(*out_curve_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800393 *out_curve_ids = curve_ids;
394 *out_curve_ids_len = ncurves;
395
396 return 1;
397}
Adam Langley95c29f32014-06-20 12:00:00 -0700398
David Benjamin072334d2014-07-13 16:24:27 -0400399/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
400 * TLS curve ID and point format, respectively, for |ec|. It returns one on
401 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800402static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
403 uint8_t *out_comp_id, EC_KEY *ec) {
404 int nid;
405 uint16_t id;
406 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 if (ec == NULL) {
409 return 0;
410 }
Adam Langley95c29f32014-06-20 12:00:00 -0700411
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 grp = EC_KEY_get0_group(ec);
413 if (grp == NULL) {
414 return 0;
415 }
David Benjamin072334d2014-07-13 16:24:27 -0400416
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 /* Determine curve ID */
418 nid = EC_GROUP_get_curve_name(grp);
David Benjamin4298d772015-12-19 00:18:25 -0500419 if (!ssl_nid_to_curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 return 0;
421 }
David Benjamin072334d2014-07-13 16:24:27 -0400422
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
424 *out_curve_id = id;
425
426 if (out_comp_id) {
427 if (EC_KEY_get0_public_key(ec) == NULL) {
428 return 0;
429 }
430 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
431 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
432 } else {
433 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
434 }
435 }
436
437 return 1;
438}
David Benjamin072334d2014-07-13 16:24:27 -0400439
Adam Langleyfcf25832014-12-18 17:42:32 -0800440/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
441 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400442 * preferences are checked; the peer (the server) does not send preferences. */
David Benjamin4298d772015-12-19 00:18:25 -0500443int tls1_check_curve_id(SSL *ssl, uint16_t curve_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800444 const uint16_t *curves;
David Benjamin55a43642015-04-20 14:45:55 -0400445 size_t curves_len, i, get_peer_curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400446
Adam Langleyfcf25832014-12-18 17:42:32 -0800447 /* Check against our list, then the peer's list. */
David Benjamin55a43642015-04-20 14:45:55 -0400448 for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
David Benjamin4298d772015-12-19 00:18:25 -0500449 if (get_peer_curves && !ssl->server) {
David Benjamin55a43642015-04-20 14:45:55 -0400450 /* Servers do not present a preference list so, if we are a client, only
451 * check our list. */
452 continue;
453 }
454
David Benjamin4298d772015-12-19 00:18:25 -0500455 tls1_get_curvelist(ssl, get_peer_curves, &curves, &curves_len);
David Benjamin55a43642015-04-20 14:45:55 -0400456 if (get_peer_curves && curves_len == 0) {
457 /* Clients are not required to send a supported_curves extension. In this
458 * case, the server is free to pick any curve it likes. See RFC 4492,
459 * section 4, paragraph 3. */
460 continue;
461 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 for (i = 0; i < curves_len; i++) {
463 if (curves[i] == curve_id) {
464 break;
465 }
466 }
Adam Langley95c29f32014-06-20 12:00:00 -0700467
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 if (i == curves_len) {
469 return 0;
470 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 }
David Benjamin033e5f42014-11-13 18:47:41 -0500472
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 return 1;
474}
David Benjamin033e5f42014-11-13 18:47:41 -0500475
David Benjamin0d56f882015-12-19 17:05:56 -0500476int tls1_check_ec_cert(SSL *ssl, X509 *x) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800477 int ret = 0;
478 EVP_PKEY *pkey = X509_get_pubkey(x);
479 uint16_t curve_id;
480 uint8_t comp_id;
481
David Benjamin758d1272015-11-20 17:47:25 -0500482 if (!pkey) {
483 goto done;
484 }
485 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
486 if (ec_key == NULL ||
487 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, ec_key) ||
David Benjamin0d56f882015-12-19 17:05:56 -0500488 !tls1_check_curve_id(ssl, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400489 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800490 goto done;
491 }
492
493 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500494
495done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400496 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800497 return ret;
498}
David Benjamin42e9a772014-09-02 23:18:44 -0400499
Adam Langley95c29f32014-06-20 12:00:00 -0700500/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700502
Adam Langley95c29f32014-06-20 12:00:00 -0700503#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langley95c29f32014-06-20 12:00:00 -0700505#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700506
Adam Langleyfcf25832014-12-18 17:42:32 -0800507#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700508
David Benjamincff64722014-08-19 19:54:46 -0400509static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 tlsext_sigalg(TLSEXT_hash_sha512)
511 tlsext_sigalg(TLSEXT_hash_sha384)
512 tlsext_sigalg(TLSEXT_hash_sha256)
513 tlsext_sigalg(TLSEXT_hash_sha224)
514 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700515};
David Benjamin05da6e12014-07-12 20:42:55 -0400516
David Benjamin0d56f882015-12-19 17:05:56 -0500517size_t tls12_get_psigalgs(SSL *ssl, const uint8_t **psigs) {
David Benjamin3dd90162015-08-09 11:26:40 -0400518 *psigs = tls12_sigalgs;
519 return sizeof(tls12_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800520}
Adam Langley95c29f32014-06-20 12:00:00 -0700521
David Benjamin6e807652015-11-02 12:02:20 -0500522int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert,
523 uint8_t hash, uint8_t signature, EVP_PKEY *pkey) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800524 const uint8_t *sent_sigs;
525 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400526 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800527
528 /* Should never happen */
529 if (sigalg == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400530 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800531 *out_alert = SSL_AD_INTERNAL_ERROR;
532 return 0;
533 }
534
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 /* Check key type is consistent with signature */
536 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400537 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800538 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
539 return 0;
540 }
541
Adam Langleyfcf25832014-12-18 17:42:32 -0800542 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500543 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800544 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
545 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
546 break;
547 }
548 }
549
David Benjamin788be4a2015-10-30 17:50:57 -0400550 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400551 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800552 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
553 return 0;
554 }
555
556 *out_md = tls12_get_hash(hash);
557 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400558 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
560 return 0;
561 }
562
563 return 1;
564}
565
566/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
567 * supported or doesn't appear in supported signature algorithms. Unlike
568 * ssl_cipher_get_disabled this applies to a specific session and not global
569 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500570void ssl_set_client_disabled(SSL *ssl) {
571 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800572 const uint8_t *sigalgs;
573 size_t i, sigalgslen;
574 int have_rsa = 0, have_ecdsa = 0;
575 c->mask_a = 0;
576 c->mask_k = 0;
577
Adam Langleyfcf25832014-12-18 17:42:32 -0800578 /* Now go through all signature algorithms seeing if we support any for RSA,
579 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500580 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800581 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
582 switch (sigalgs[1]) {
583 case TLSEXT_signature_rsa:
584 have_rsa = 1;
585 break;
586
587 case TLSEXT_signature_ecdsa:
588 have_ecdsa = 1;
589 break;
590 }
591 }
592
593 /* Disable auth if we don't include any appropriate signature algorithms. */
594 if (!have_rsa) {
595 c->mask_a |= SSL_aRSA;
596 }
597 if (!have_ecdsa) {
598 c->mask_a |= SSL_aECDSA;
599 }
600
601 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500602 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800603 c->mask_a |= SSL_aPSK;
604 c->mask_k |= SSL_kPSK;
605 }
606}
Adam Langley95c29f32014-06-20 12:00:00 -0700607
Adam Langley614c66a2015-06-12 15:26:58 -0700608/* tls_extension represents a TLS extension that is handled internally. The
609 * |init| function is called for each handshake, before any other functions of
610 * the extension. Then the add and parse callbacks are called as needed.
611 *
612 * The parse callbacks receive a |CBS| that contains the contents of the
613 * extension (i.e. not including the type and length bytes). If an extension is
614 * not received then the parse callbacks will be called with a NULL CBS so that
615 * they can do any processing needed to handle the absence of an extension.
616 *
617 * The add callbacks receive a |CBB| to which the extension can be appended but
618 * the function is responsible for appending the type and length bytes too.
619 *
620 * All callbacks return one for success and zero for error. If a parse function
621 * returns zero then a fatal alert with value |*out_alert| will be sent. If
622 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
623struct tls_extension {
624 uint16_t value;
625 void (*init)(SSL *ssl);
626
627 int (*add_clienthello)(SSL *ssl, CBB *out);
628 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
629
630 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
631 int (*add_serverhello)(SSL *ssl, CBB *out);
632};
633
634
635/* Server name indication (SNI).
636 *
637 * https://tools.ietf.org/html/rfc6066#section-3. */
638
639static void ext_sni_init(SSL *ssl) {
640 ssl->s3->tmp.should_ack_sni = 0;
641}
642
643static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
644 if (ssl->tlsext_hostname == NULL) {
645 return 1;
646 }
647
648 CBB contents, server_name_list, name;
649 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
650 !CBB_add_u16_length_prefixed(out, &contents) ||
651 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
652 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
653 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
654 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
655 strlen(ssl->tlsext_hostname)) ||
656 !CBB_flush(out)) {
657 return 0;
658 }
659
660 return 1;
661}
662
David Benjamin0d56f882015-12-19 17:05:56 -0500663static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
664 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700665 if (contents == NULL) {
666 return 1;
667 }
668
669 if (CBS_len(contents) != 0) {
670 return 0;
671 }
672
673 assert(ssl->tlsext_hostname != NULL);
674
675 if (!ssl->hit) {
676 assert(ssl->session->tlsext_hostname == NULL);
677 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
678 if (!ssl->session->tlsext_hostname) {
679 *out_alert = SSL_AD_INTERNAL_ERROR;
680 return 0;
681 }
682 }
683
684 return 1;
685}
686
David Benjamin0d56f882015-12-19 17:05:56 -0500687static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
688 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700689 if (contents == NULL) {
690 return 1;
691 }
692
693 /* The servername extension is treated as follows:
694 *
695 * - Only the hostname type is supported with a maximum length of 255.
696 * - The servername is rejected if too long or if it contains zeros, in
697 * which case an fatal alert is generated.
698 * - The servername field is maintained together with the session cache.
699 * - When a session is resumed, the servername callback is invoked in order
700 * to allow the application to position itself to the right context.
701 * - The servername is acknowledged if it is new for a session or when
702 * it is identical to a previously used for the same session.
703 * Applications can control the behaviour. They can at any time
704 * set a 'desirable' servername for a new SSL object. This can be the
705 * case for example with HTTPS when a Host: header field is received and
706 * a renegotiation is requested. In this case, a possible servername
707 * presented in the new client hello is only acknowledged if it matches
708 * the value of the Host: field.
709 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
710 * if they provide for changing an explicit servername context for the
711 * session,
712 * i.e. when the session has been established with a servername extension.
713 */
714
715 CBS server_name_list;
716 char have_seen_host_name = 0;
717
718 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
719 CBS_len(&server_name_list) == 0 ||
720 CBS_len(contents) != 0) {
721 return 0;
722 }
723
724 /* Decode each ServerName in the extension. */
725 while (CBS_len(&server_name_list) > 0) {
726 uint8_t name_type;
727 CBS host_name;
728
729 if (!CBS_get_u8(&server_name_list, &name_type) ||
730 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
731 return 0;
732 }
733
734 /* Only host_name is supported. */
735 if (name_type != TLSEXT_NAMETYPE_host_name) {
736 continue;
737 }
738
739 if (have_seen_host_name) {
740 /* The ServerNameList MUST NOT contain more than one name of the same
741 * name_type. */
742 return 0;
743 }
744
745 have_seen_host_name = 1;
746
747 if (CBS_len(&host_name) == 0 ||
748 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
749 CBS_contains_zero_byte(&host_name)) {
750 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
751 return 0;
752 }
753
754 if (!ssl->hit) {
755 assert(ssl->session->tlsext_hostname == NULL);
756 if (ssl->session->tlsext_hostname) {
757 /* This should be impossible. */
758 return 0;
759 }
760
761 /* Copy the hostname as a string. */
762 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
763 *out_alert = SSL_AD_INTERNAL_ERROR;
764 return 0;
765 }
766
767 ssl->s3->tmp.should_ack_sni = 1;
768 }
769 }
770
771 return 1;
772}
773
774static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
775 if (ssl->hit ||
776 !ssl->s3->tmp.should_ack_sni ||
777 ssl->session->tlsext_hostname == NULL) {
778 return 1;
779 }
780
781 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
782 !CBB_add_u16(out, 0 /* length */)) {
783 return 0;
784 }
785
786 return 1;
787}
788
789
Adam Langley5021b222015-06-12 18:27:58 -0700790/* Renegotiation indication.
791 *
792 * https://tools.ietf.org/html/rfc5746 */
793
794static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
795 CBB contents, prev_finished;
796 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
797 !CBB_add_u16_length_prefixed(out, &contents) ||
798 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
799 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
800 ssl->s3->previous_client_finished_len) ||
801 !CBB_flush(out)) {
802 return 0;
803 }
804
805 return 1;
806}
807
808static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
809 CBS *contents) {
David Benjamin3e052de2015-11-25 20:10:31 -0500810 /* Servers may not switch between omitting the extension and supporting it.
811 * See RFC 5746, sections 3.5 and 4.2. */
812 if (ssl->s3->initial_handshake_complete &&
813 (contents != NULL) != ssl->s3->send_connection_binding) {
814 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
815 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
816 return 0;
817 }
818
Adam Langley5021b222015-06-12 18:27:58 -0700819 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500820 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700821 * RI even on initial ServerHello because the client doesn't see any
822 * renegotiation during an attack. However this would mean we could not
823 * connect to any server which doesn't support RI.
824 *
David Benjamine9cddb82015-11-23 14:36:40 -0500825 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
826 * practical terms every client sets it so it's just assumed here. */
827 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700828 }
829
830 const size_t expected_len = ssl->s3->previous_client_finished_len +
831 ssl->s3->previous_server_finished_len;
832
833 /* Check for logic errors */
834 assert(!expected_len || ssl->s3->previous_client_finished_len);
835 assert(!expected_len || ssl->s3->previous_server_finished_len);
836
837 /* Parse out the extension contents. */
838 CBS renegotiated_connection;
839 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
840 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400841 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700842 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
843 return 0;
844 }
845
846 /* Check that the extension matches. */
847 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400848 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700849 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
850 return 0;
851 }
852
853 const uint8_t *d = CBS_data(&renegotiated_connection);
854 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
855 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400856 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700857 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
858 return 0;
859 }
860 d += ssl->s3->previous_client_finished_len;
861
862 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
863 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400864 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700865 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
866 return 0;
867 }
868 ssl->s3->send_connection_binding = 1;
869
870 return 1;
871}
872
873static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
874 CBS *contents) {
875 /* Renegotiation isn't supported as a server so this function should never be
876 * called after the initial handshake. */
877 assert(!ssl->s3->initial_handshake_complete);
878
879 CBS fake_contents;
880 static const uint8_t kFakeExtension[] = {0};
881
882 if (contents == NULL) {
883 if (ssl->s3->send_connection_binding) {
884 /* The renegotiation SCSV was received so pretend that we received a
885 * renegotiation extension. */
886 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
887 contents = &fake_contents;
888 /* We require that the renegotiation extension is at index zero of
889 * kExtensions. */
890 ssl->s3->tmp.extensions.received |= (1u << 0);
891 } else {
892 return 1;
893 }
894 }
895
896 CBS renegotiated_connection;
897
898 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
899 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400900 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700901 return 0;
902 }
903
904 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500905 if (!CBS_mem_equal(&renegotiated_connection,
906 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700907 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400908 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700909 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
910 return 0;
911 }
912
913 ssl->s3->send_connection_binding = 1;
914
915 return 1;
916}
917
918static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
919 CBB contents, prev_finished;
920 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
921 !CBB_add_u16_length_prefixed(out, &contents) ||
922 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
923 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
924 ssl->s3->previous_client_finished_len) ||
925 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
926 ssl->s3->previous_server_finished_len) ||
927 !CBB_flush(out)) {
928 return 0;
929 }
930
931 return 1;
932}
933
Adam Langley0a056712015-07-01 15:03:33 -0700934
935/* Extended Master Secret.
936 *
937 * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
938
939static void ext_ems_init(SSL *ssl) {
940 ssl->s3->tmp.extended_master_secret = 0;
941}
942
943static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
944 if (ssl->version == SSL3_VERSION) {
945 return 1;
946 }
947
948 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
949 !CBB_add_u16(out, 0 /* length */)) {
950 return 0;
951 }
952
953 return 1;
954}
955
956static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
957 CBS *contents) {
958 if (contents == NULL) {
959 return 1;
960 }
961
962 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
963 return 0;
964 }
965
966 ssl->s3->tmp.extended_master_secret = 1;
967 return 1;
968}
969
David Benjamin0d56f882015-12-19 17:05:56 -0500970static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
971 CBS *contents) {
Adam Langley0a056712015-07-01 15:03:33 -0700972 if (ssl->version == SSL3_VERSION || contents == NULL) {
973 return 1;
974 }
975
976 if (CBS_len(contents) != 0) {
977 return 0;
978 }
979
980 ssl->s3->tmp.extended_master_secret = 1;
981 return 1;
982}
983
984static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
985 if (!ssl->s3->tmp.extended_master_secret) {
986 return 1;
987 }
988
989 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
990 !CBB_add_u16(out, 0 /* length */)) {
991 return 0;
992 }
993
994 return 1;
995}
996
Adam Langley9b05bc52015-07-01 15:25:33 -0700997
998/* Session tickets.
999 *
1000 * https://tools.ietf.org/html/rfc5077 */
1001
1002static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1003 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1004 return 1;
1005 }
1006
1007 const uint8_t *ticket_data = NULL;
1008 int ticket_len = 0;
1009
1010 /* Renegotiation does not participate in session resumption. However, still
1011 * advertise the extension to avoid potentially breaking servers which carry
1012 * over the state from the previous handshake, such as OpenSSL servers
1013 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1014 if (!ssl->s3->initial_handshake_complete &&
1015 ssl->session != NULL &&
1016 ssl->session->tlsext_tick != NULL) {
1017 ticket_data = ssl->session->tlsext_tick;
1018 ticket_len = ssl->session->tlsext_ticklen;
1019 }
1020
1021 CBB ticket;
1022 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1023 !CBB_add_u16_length_prefixed(out, &ticket) ||
1024 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1025 !CBB_flush(out)) {
1026 return 0;
1027 }
1028
1029 return 1;
1030}
1031
1032static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1033 CBS *contents) {
1034 ssl->tlsext_ticket_expected = 0;
1035
1036 if (contents == NULL) {
1037 return 1;
1038 }
1039
1040 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1041 * this function should never be called, even if the server tries to send the
1042 * extension. */
1043 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1044
1045 if (CBS_len(contents) != 0) {
1046 return 0;
1047 }
1048
1049 ssl->tlsext_ticket_expected = 1;
1050 return 1;
1051}
1052
David Benjamin0d56f882015-12-19 17:05:56 -05001053static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1054 CBS *contents) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001055 /* This function isn't used because the ticket extension from the client is
David Benjaminc7ce9772015-10-09 19:32:41 -04001056 * handled in ssl_session.c. */
Adam Langley9b05bc52015-07-01 15:25:33 -07001057 return 1;
1058}
1059
1060static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1061 if (!ssl->tlsext_ticket_expected) {
1062 return 1;
1063 }
1064
1065 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1066 * true. */
1067 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1068
1069 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1070 !CBB_add_u16(out, 0 /* length */)) {
1071 return 0;
1072 }
1073
1074 return 1;
1075}
1076
1077
Adam Langley2e857bd2015-07-01 16:09:19 -07001078/* Signature Algorithms.
1079 *
1080 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1081
1082static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1083 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1084 return 1;
1085 }
1086
1087 const uint8_t *sigalgs_data;
1088 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1089
1090 CBB contents, sigalgs;
1091 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1092 !CBB_add_u16_length_prefixed(out, &contents) ||
1093 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1094 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1095 !CBB_flush(out)) {
1096 return 0;
1097 }
1098
1099 return 1;
1100}
1101
1102static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1103 CBS *contents) {
1104 if (contents != NULL) {
1105 /* Servers MUST NOT send this extension. */
1106 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001107 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001108 return 0;
1109 }
1110
1111 return 1;
1112}
1113
1114static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1115 CBS *contents) {
1116 OPENSSL_free(ssl->cert->peer_sigalgs);
1117 ssl->cert->peer_sigalgs = NULL;
1118 ssl->cert->peer_sigalgslen = 0;
1119
Adam Langley2e857bd2015-07-01 16:09:19 -07001120 if (contents == NULL) {
1121 return 1;
1122 }
1123
1124 CBS supported_signature_algorithms;
1125 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001126 CBS_len(contents) != 0 ||
1127 CBS_len(&supported_signature_algorithms) == 0 ||
1128 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001129 return 0;
1130 }
1131
1132 return 1;
1133}
1134
1135static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1136 /* Servers MUST NOT send this extension. */
1137 return 1;
1138}
1139
1140
Adam Langleybb0bd042015-07-01 16:21:03 -07001141/* OCSP Stapling.
1142 *
1143 * https://tools.ietf.org/html/rfc6066#section-8 */
1144
1145static void ext_ocsp_init(SSL *ssl) {
1146 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001147 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001148}
1149
1150static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1151 if (!ssl->ocsp_stapling_enabled) {
1152 return 1;
1153 }
1154
1155 CBB contents;
1156 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1157 !CBB_add_u16_length_prefixed(out, &contents) ||
1158 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1159 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1160 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1161 !CBB_flush(out)) {
1162 return 0;
1163 }
1164
Adam Langleyce9d85e2016-01-24 15:58:39 -08001165 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001166 return 1;
1167}
1168
1169static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001170 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001171 if (contents == NULL) {
1172 return 1;
1173 }
1174
1175 if (CBS_len(contents) != 0) {
1176 return 0;
1177 }
1178
1179 ssl->s3->tmp.certificate_status_expected = 1;
1180 return 1;
1181}
1182
1183static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1184 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001185 if (contents == NULL) {
1186 return 1;
1187 }
1188
1189 uint8_t status_type;
1190 if (!CBS_get_u8(contents, &status_type)) {
1191 return 0;
1192 }
1193
1194 /* We cannot decide whether OCSP stapling will occur yet because the correct
1195 * SSL_CTX might not have been selected. */
1196 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1197
Adam Langleybb0bd042015-07-01 16:21:03 -07001198 return 1;
1199}
1200
1201static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001202 /* The extension shouldn't be sent when resuming sessions. */
1203 if (ssl->hit ||
1204 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001205 ssl->ctx->ocsp_response_length == 0) {
1206 return 1;
1207 }
1208
1209 ssl->s3->tmp.certificate_status_expected = 1;
1210
1211 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1212 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001213}
1214
1215
Adam Langley97dfcbf2015-07-01 18:35:20 -07001216/* Next protocol negotiation.
1217 *
1218 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1219
1220static void ext_npn_init(SSL *ssl) {
1221 ssl->s3->next_proto_neg_seen = 0;
1222}
1223
1224static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1225 if (ssl->s3->initial_handshake_complete ||
1226 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001227 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001228 SSL_IS_DTLS(ssl)) {
1229 return 1;
1230 }
1231
1232 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1233 !CBB_add_u16(out, 0 /* length */)) {
1234 return 0;
1235 }
1236
1237 return 1;
1238}
1239
1240static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1241 CBS *contents) {
1242 if (contents == NULL) {
1243 return 1;
1244 }
1245
1246 /* If any of these are false then we should never have sent the NPN
1247 * extension in the ClientHello and thus this function should never have been
1248 * called. */
1249 assert(!ssl->s3->initial_handshake_complete);
1250 assert(!SSL_IS_DTLS(ssl));
1251 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001252 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001253
David Benjamin76c2efc2015-08-31 14:24:29 -04001254 if (ssl->s3->alpn_selected != NULL) {
1255 /* NPN and ALPN may not be negotiated in the same connection. */
1256 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1257 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1258 return 0;
1259 }
1260
Adam Langley97dfcbf2015-07-01 18:35:20 -07001261 const uint8_t *const orig_contents = CBS_data(contents);
1262 const size_t orig_len = CBS_len(contents);
1263
1264 while (CBS_len(contents) != 0) {
1265 CBS proto;
1266 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1267 CBS_len(&proto) == 0) {
1268 return 0;
1269 }
1270 }
1271
1272 uint8_t *selected;
1273 uint8_t selected_len;
1274 if (ssl->ctx->next_proto_select_cb(
1275 ssl, &selected, &selected_len, orig_contents, orig_len,
1276 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1277 *out_alert = SSL_AD_INTERNAL_ERROR;
1278 return 0;
1279 }
1280
David Benjamin79978df2015-12-25 15:56:49 -05001281 OPENSSL_free(ssl->s3->next_proto_negotiated);
1282 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1283 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001284 *out_alert = SSL_AD_INTERNAL_ERROR;
1285 return 0;
1286 }
1287
David Benjamin79978df2015-12-25 15:56:49 -05001288 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001289 ssl->s3->next_proto_neg_seen = 1;
1290
1291 return 1;
1292}
1293
1294static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1295 CBS *contents) {
1296 if (contents != NULL && CBS_len(contents) != 0) {
1297 return 0;
1298 }
1299
1300 if (contents == NULL ||
1301 ssl->s3->initial_handshake_complete ||
1302 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1303 * afterwards, parsing the ALPN extension will clear
1304 * |next_proto_neg_seen|. */
1305 ssl->s3->alpn_selected != NULL ||
1306 ssl->ctx->next_protos_advertised_cb == NULL ||
1307 SSL_IS_DTLS(ssl)) {
1308 return 1;
1309 }
1310
1311 ssl->s3->next_proto_neg_seen = 1;
1312 return 1;
1313}
1314
1315static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1316 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1317 * parsed. */
1318 if (!ssl->s3->next_proto_neg_seen) {
1319 return 1;
1320 }
1321
1322 const uint8_t *npa;
1323 unsigned npa_len;
1324
1325 if (ssl->ctx->next_protos_advertised_cb(
1326 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1327 SSL_TLSEXT_ERR_OK) {
1328 ssl->s3->next_proto_neg_seen = 0;
1329 return 1;
1330 }
1331
1332 CBB contents;
1333 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1334 !CBB_add_u16_length_prefixed(out, &contents) ||
1335 !CBB_add_bytes(&contents, npa, npa_len) ||
1336 !CBB_flush(out)) {
1337 return 0;
1338 }
1339
1340 return 1;
1341}
1342
1343
Adam Langleyab8d87d2015-07-10 12:21:39 -07001344/* Signed certificate timestamps.
1345 *
1346 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1347
1348static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1349 if (!ssl->signed_cert_timestamps_enabled) {
1350 return 1;
1351 }
1352
1353 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1354 !CBB_add_u16(out, 0 /* length */)) {
1355 return 0;
1356 }
1357
1358 return 1;
1359}
1360
1361static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1362 CBS *contents) {
1363 if (contents == NULL) {
1364 return 1;
1365 }
1366
1367 /* If this is false then we should never have sent the SCT extension in the
1368 * ClientHello and thus this function should never have been called. */
1369 assert(ssl->signed_cert_timestamps_enabled);
1370
1371 if (CBS_len(contents) == 0) {
1372 *out_alert = SSL_AD_DECODE_ERROR;
1373 return 0;
1374 }
1375
1376 /* Session resumption uses the original session information. */
1377 if (!ssl->hit &&
1378 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1379 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1380 *out_alert = SSL_AD_INTERNAL_ERROR;
1381 return 0;
1382 }
1383
1384 return 1;
1385}
1386
1387static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1388 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001389 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001390}
1391
1392static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001393 /* The extension shouldn't be sent when resuming sessions. */
1394 if (ssl->hit ||
1395 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001396 return 1;
1397 }
1398
1399 CBB contents;
1400 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1401 CBB_add_u16_length_prefixed(out, &contents) &&
1402 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1403 ssl->ctx->signed_cert_timestamp_list_length) &&
1404 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001405}
1406
1407
Adam Langleyf18e4532015-07-10 13:39:53 -07001408/* Application-level Protocol Negotiation.
1409 *
1410 * https://tools.ietf.org/html/rfc7301 */
1411
1412static void ext_alpn_init(SSL *ssl) {
1413 OPENSSL_free(ssl->s3->alpn_selected);
1414 ssl->s3->alpn_selected = NULL;
1415}
1416
1417static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1418 if (ssl->alpn_client_proto_list == NULL ||
1419 ssl->s3->initial_handshake_complete) {
1420 return 1;
1421 }
1422
1423 CBB contents, proto_list;
1424 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1425 !CBB_add_u16_length_prefixed(out, &contents) ||
1426 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1427 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1428 ssl->alpn_client_proto_list_len) ||
1429 !CBB_flush(out)) {
1430 return 0;
1431 }
1432
1433 return 1;
1434}
1435
1436static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1437 CBS *contents) {
1438 if (contents == NULL) {
1439 return 1;
1440 }
1441
1442 assert(!ssl->s3->initial_handshake_complete);
1443 assert(ssl->alpn_client_proto_list != NULL);
1444
David Benjamin76c2efc2015-08-31 14:24:29 -04001445 if (ssl->s3->next_proto_neg_seen) {
1446 /* NPN and ALPN may not be negotiated in the same connection. */
1447 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1448 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1449 return 0;
1450 }
1451
Adam Langleyf18e4532015-07-10 13:39:53 -07001452 /* The extension data consists of a ProtocolNameList which must have
1453 * exactly one ProtocolName. Each of these is length-prefixed. */
1454 CBS protocol_name_list, protocol_name;
1455 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1456 CBS_len(contents) != 0 ||
1457 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1458 /* Empty protocol names are forbidden. */
1459 CBS_len(&protocol_name) == 0 ||
1460 CBS_len(&protocol_name_list) != 0) {
1461 return 0;
1462 }
1463
1464 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1465 &ssl->s3->alpn_selected_len)) {
1466 *out_alert = SSL_AD_INTERNAL_ERROR;
1467 return 0;
1468 }
1469
1470 return 1;
1471}
1472
1473static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1474 CBS *contents) {
1475 if (contents == NULL) {
1476 return 1;
1477 }
1478
1479 if (ssl->ctx->alpn_select_cb == NULL ||
1480 ssl->s3->initial_handshake_complete) {
1481 return 1;
1482 }
1483
1484 /* ALPN takes precedence over NPN. */
1485 ssl->s3->next_proto_neg_seen = 0;
1486
1487 CBS protocol_name_list;
1488 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1489 CBS_len(contents) != 0 ||
1490 CBS_len(&protocol_name_list) < 2) {
1491 return 0;
1492 }
1493
1494 /* Validate the protocol list. */
1495 CBS protocol_name_list_copy = protocol_name_list;
1496 while (CBS_len(&protocol_name_list_copy) > 0) {
1497 CBS protocol_name;
1498
1499 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1500 /* Empty protocol names are forbidden. */
1501 CBS_len(&protocol_name) == 0) {
1502 return 0;
1503 }
1504 }
1505
1506 const uint8_t *selected;
1507 uint8_t selected_len;
1508 if (ssl->ctx->alpn_select_cb(
1509 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1510 CBS_len(&protocol_name_list),
1511 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1512 OPENSSL_free(ssl->s3->alpn_selected);
1513 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1514 if (ssl->s3->alpn_selected == NULL) {
1515 *out_alert = SSL_AD_INTERNAL_ERROR;
1516 return 0;
1517 }
1518 ssl->s3->alpn_selected_len = selected_len;
1519 }
1520
1521 return 1;
1522}
1523
1524static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1525 if (ssl->s3->alpn_selected == NULL) {
1526 return 1;
1527 }
1528
1529 CBB contents, proto_list, proto;
1530 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1531 !CBB_add_u16_length_prefixed(out, &contents) ||
1532 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1533 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001534 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1535 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001536 !CBB_flush(out)) {
1537 return 0;
1538 }
1539
1540 return 1;
1541}
1542
1543
Adam Langley49c7af12015-07-10 14:33:46 -07001544/* Channel ID.
1545 *
1546 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1547
1548static void ext_channel_id_init(SSL *ssl) {
1549 ssl->s3->tlsext_channel_id_valid = 0;
1550}
1551
1552static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1553 if (!ssl->tlsext_channel_id_enabled ||
1554 SSL_IS_DTLS(ssl)) {
1555 return 1;
1556 }
1557
1558 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1559 !CBB_add_u16(out, 0 /* length */)) {
1560 return 0;
1561 }
1562
1563 return 1;
1564}
1565
1566static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1567 CBS *contents) {
1568 if (contents == NULL) {
1569 return 1;
1570 }
1571
1572 assert(!SSL_IS_DTLS(ssl));
1573 assert(ssl->tlsext_channel_id_enabled);
1574
1575 if (CBS_len(contents) != 0) {
1576 return 0;
1577 }
1578
1579 ssl->s3->tlsext_channel_id_valid = 1;
1580 return 1;
1581}
1582
1583static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1584 CBS *contents) {
1585 if (contents == NULL ||
1586 !ssl->tlsext_channel_id_enabled ||
1587 SSL_IS_DTLS(ssl)) {
1588 return 1;
1589 }
1590
1591 if (CBS_len(contents) != 0) {
1592 return 0;
1593 }
1594
1595 ssl->s3->tlsext_channel_id_valid = 1;
1596 return 1;
1597}
1598
1599static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1600 if (!ssl->s3->tlsext_channel_id_valid) {
1601 return 1;
1602 }
1603
1604 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1605 !CBB_add_u16(out, 0 /* length */)) {
1606 return 0;
1607 }
1608
1609 return 1;
1610}
1611
Adam Langley391250d2015-07-15 19:06:07 -07001612
1613/* Secure Real-time Transport Protocol (SRTP) extension.
1614 *
1615 * https://tools.ietf.org/html/rfc5764 */
1616
Adam Langley391250d2015-07-15 19:06:07 -07001617
1618static void ext_srtp_init(SSL *ssl) {
1619 ssl->srtp_profile = NULL;
1620}
1621
1622static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1623 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1624 if (profiles == NULL) {
1625 return 1;
1626 }
1627 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1628 if (num_profiles == 0) {
1629 return 1;
1630 }
1631
1632 CBB contents, profile_ids;
1633 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1634 !CBB_add_u16_length_prefixed(out, &contents) ||
1635 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1636 return 0;
1637 }
1638
1639 size_t i;
1640 for (i = 0; i < num_profiles; i++) {
1641 if (!CBB_add_u16(&profile_ids,
1642 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1643 return 0;
1644 }
1645 }
1646
1647 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1648 !CBB_flush(out)) {
1649 return 0;
1650 }
1651
1652 return 1;
1653}
1654
1655static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1656 CBS *contents) {
1657 if (contents == NULL) {
1658 return 1;
1659 }
1660
1661 /* The extension consists of a u16-prefixed profile ID list containing a
1662 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1663 *
1664 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1665 CBS profile_ids, srtp_mki;
1666 uint16_t profile_id;
1667 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1668 !CBS_get_u16(&profile_ids, &profile_id) ||
1669 CBS_len(&profile_ids) != 0 ||
1670 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1671 CBS_len(contents) != 0) {
1672 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1673 return 0;
1674 }
1675
1676 if (CBS_len(&srtp_mki) != 0) {
1677 /* Must be no MKI, since we never offer one. */
1678 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1679 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1680 return 0;
1681 }
1682
1683 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1684
1685 /* Check to see if the server gave us something we support (and presumably
1686 * offered). */
1687 size_t i;
1688 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1689 const SRTP_PROTECTION_PROFILE *profile =
1690 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1691
1692 if (profile->id == profile_id) {
1693 ssl->srtp_profile = profile;
1694 return 1;
1695 }
1696 }
1697
1698 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1699 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1700 return 0;
1701}
1702
1703static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1704 CBS *contents) {
1705 if (contents == NULL) {
1706 return 1;
1707 }
1708
1709 CBS profile_ids, srtp_mki;
1710 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1711 CBS_len(&profile_ids) < 2 ||
1712 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1713 CBS_len(contents) != 0) {
1714 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1715 return 0;
1716 }
1717 /* Discard the MKI value for now. */
1718
1719 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1720 SSL_get_srtp_profiles(ssl);
1721
1722 /* Pick the server's most preferred profile. */
1723 size_t i;
1724 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1725 const SRTP_PROTECTION_PROFILE *server_profile =
1726 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1727
1728 CBS profile_ids_tmp;
1729 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1730
1731 while (CBS_len(&profile_ids_tmp) > 0) {
1732 uint16_t profile_id;
1733 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1734 return 0;
1735 }
1736
1737 if (server_profile->id == profile_id) {
1738 ssl->srtp_profile = server_profile;
1739 return 1;
1740 }
1741 }
1742 }
1743
1744 return 1;
1745}
1746
1747static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1748 if (ssl->srtp_profile == NULL) {
1749 return 1;
1750 }
1751
1752 CBB contents, profile_ids;
1753 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1754 !CBB_add_u16_length_prefixed(out, &contents) ||
1755 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1756 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1757 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1758 !CBB_flush(out)) {
1759 return 0;
1760 }
1761
1762 return 1;
1763}
1764
Adam Langleybdd5d662015-07-20 16:19:08 -07001765
1766/* EC point formats.
1767 *
1768 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1769
1770static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1771 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1772 return 0;
1773 }
1774
1775 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1776
1777 size_t i;
1778 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1779 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1780
1781 const uint32_t alg_k = cipher->algorithm_mkey;
1782 const uint32_t alg_a = cipher->algorithm_auth;
1783 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1784 return 1;
1785 }
1786 }
1787
1788 return 0;
1789}
1790
Adam Langleybdd5d662015-07-20 16:19:08 -07001791static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001792 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001793 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1794 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001795 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1796 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001797 !CBB_flush(out)) {
1798 return 0;
1799 }
1800
1801 return 1;
1802}
1803
1804static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1805 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1806 return 1;
1807 }
1808
1809 return ext_ec_point_add_extension(ssl, out);
1810}
1811
1812static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1813 CBS *contents) {
1814 if (contents == NULL) {
1815 return 1;
1816 }
1817
1818 CBS ec_point_format_list;
1819 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1820 CBS_len(contents) != 0) {
1821 return 0;
1822 }
1823
David Benjaminfc059942015-07-30 23:01:59 -04001824 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1825 * point format. */
1826 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1827 CBS_len(&ec_point_format_list)) == NULL) {
1828 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001829 return 0;
1830 }
1831
1832 return 1;
1833}
1834
1835static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1836 CBS *contents) {
1837 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1838}
1839
1840static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1841 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1842 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001843 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001844
1845 if (!using_ecc) {
1846 return 1;
1847 }
1848
1849 return ext_ec_point_add_extension(ssl, out);
1850}
1851
Adam Langley273d49c2015-07-20 16:38:52 -07001852
1853/* EC supported curves.
1854 *
1855 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1856
1857static void ext_ec_curves_init(SSL *ssl) {
1858 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1859 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1860 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
1861}
1862
1863static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
1864 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1865 return 1;
1866 }
1867
1868 CBB contents, curves_bytes;
1869 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
1870 !CBB_add_u16_length_prefixed(out, &contents) ||
1871 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
1872 return 0;
1873 }
1874
1875 const uint16_t *curves;
1876 size_t curves_len;
1877 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
1878
1879 size_t i;
1880 for (i = 0; i < curves_len; i++) {
1881 if (!CBB_add_u16(&curves_bytes, curves[i])) {
1882 return 0;
1883 }
1884 }
1885
1886 return CBB_flush(out);
1887}
1888
1889static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1890 CBS *contents) {
1891 /* This extension is not expected to be echoed by servers and is ignored. */
1892 return 1;
1893}
1894
1895static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1896 CBS *contents) {
1897 if (contents == NULL) {
1898 return 1;
1899 }
1900
1901 CBS elliptic_curve_list;
1902 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
1903 CBS_len(&elliptic_curve_list) == 0 ||
1904 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
1905 CBS_len(contents) != 0) {
1906 return 0;
1907 }
1908
1909 ssl->s3->tmp.peer_ellipticcurvelist =
1910 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
1911
1912 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
1913 *out_alert = SSL_AD_INTERNAL_ERROR;
1914 return 0;
1915 }
1916
1917 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
1918 size_t i;
1919 for (i = 0; i < num_curves; i++) {
1920 if (!CBS_get_u16(&elliptic_curve_list,
1921 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
1922 goto err;
1923 }
1924 }
1925
1926 assert(CBS_len(&elliptic_curve_list) == 0);
1927 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
1928
1929 return 1;
1930
1931err:
1932 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1933 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1934 *out_alert = SSL_AD_INTERNAL_ERROR;
1935 return 0;
1936}
1937
1938static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
1939 /* Servers don't echo this extension. */
1940 return 1;
1941}
1942
1943
Adam Langley614c66a2015-06-12 15:26:58 -07001944/* kExtensions contains all the supported extensions. */
1945static const struct tls_extension kExtensions[] = {
1946 {
Adam Langley5021b222015-06-12 18:27:58 -07001947 /* The renegotiation extension must always be at index zero because the
1948 * |received| and |sent| bitsets need to be tweaked when the "extension" is
1949 * sent as an SCSV. */
1950 TLSEXT_TYPE_renegotiate,
1951 NULL,
1952 ext_ri_add_clienthello,
1953 ext_ri_parse_serverhello,
1954 ext_ri_parse_clienthello,
1955 ext_ri_add_serverhello,
1956 },
1957 {
Adam Langley614c66a2015-06-12 15:26:58 -07001958 TLSEXT_TYPE_server_name,
1959 ext_sni_init,
1960 ext_sni_add_clienthello,
1961 ext_sni_parse_serverhello,
1962 ext_sni_parse_clienthello,
1963 ext_sni_add_serverhello,
1964 },
Adam Langley0a056712015-07-01 15:03:33 -07001965 {
1966 TLSEXT_TYPE_extended_master_secret,
1967 ext_ems_init,
1968 ext_ems_add_clienthello,
1969 ext_ems_parse_serverhello,
1970 ext_ems_parse_clienthello,
1971 ext_ems_add_serverhello,
1972 },
Adam Langley9b05bc52015-07-01 15:25:33 -07001973 {
1974 TLSEXT_TYPE_session_ticket,
1975 NULL,
1976 ext_ticket_add_clienthello,
1977 ext_ticket_parse_serverhello,
1978 ext_ticket_parse_clienthello,
1979 ext_ticket_add_serverhello,
1980 },
Adam Langley2e857bd2015-07-01 16:09:19 -07001981 {
1982 TLSEXT_TYPE_signature_algorithms,
1983 NULL,
1984 ext_sigalgs_add_clienthello,
1985 ext_sigalgs_parse_serverhello,
1986 ext_sigalgs_parse_clienthello,
1987 ext_sigalgs_add_serverhello,
1988 },
Adam Langleybb0bd042015-07-01 16:21:03 -07001989 {
1990 TLSEXT_TYPE_status_request,
1991 ext_ocsp_init,
1992 ext_ocsp_add_clienthello,
1993 ext_ocsp_parse_serverhello,
1994 ext_ocsp_parse_clienthello,
1995 ext_ocsp_add_serverhello,
1996 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07001997 {
1998 TLSEXT_TYPE_next_proto_neg,
1999 ext_npn_init,
2000 ext_npn_add_clienthello,
2001 ext_npn_parse_serverhello,
2002 ext_npn_parse_clienthello,
2003 ext_npn_add_serverhello,
2004 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002005 {
2006 TLSEXT_TYPE_certificate_timestamp,
2007 NULL,
2008 ext_sct_add_clienthello,
2009 ext_sct_parse_serverhello,
2010 ext_sct_parse_clienthello,
2011 ext_sct_add_serverhello,
2012 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002013 {
2014 TLSEXT_TYPE_application_layer_protocol_negotiation,
2015 ext_alpn_init,
2016 ext_alpn_add_clienthello,
2017 ext_alpn_parse_serverhello,
2018 ext_alpn_parse_clienthello,
2019 ext_alpn_add_serverhello,
2020 },
Adam Langley49c7af12015-07-10 14:33:46 -07002021 {
2022 TLSEXT_TYPE_channel_id,
2023 ext_channel_id_init,
2024 ext_channel_id_add_clienthello,
2025 ext_channel_id_parse_serverhello,
2026 ext_channel_id_parse_clienthello,
2027 ext_channel_id_add_serverhello,
2028 },
Adam Langley391250d2015-07-15 19:06:07 -07002029 {
2030 TLSEXT_TYPE_srtp,
2031 ext_srtp_init,
2032 ext_srtp_add_clienthello,
2033 ext_srtp_parse_serverhello,
2034 ext_srtp_parse_clienthello,
2035 ext_srtp_add_serverhello,
2036 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002037 {
2038 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002039 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002040 ext_ec_point_add_clienthello,
2041 ext_ec_point_parse_serverhello,
2042 ext_ec_point_parse_clienthello,
2043 ext_ec_point_add_serverhello,
2044 },
Adam Langley273d49c2015-07-20 16:38:52 -07002045 {
2046 TLSEXT_TYPE_elliptic_curves,
2047 ext_ec_curves_init,
2048 ext_ec_curves_add_clienthello,
2049 ext_ec_curves_parse_serverhello,
2050 ext_ec_curves_parse_clienthello,
2051 ext_ec_curves_add_serverhello,
2052 },
Adam Langley614c66a2015-06-12 15:26:58 -07002053};
2054
2055#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2056
Adam Langley4cfa96b2015-07-01 11:56:55 -07002057OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2058 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002059 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002060OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2061 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2062 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002063 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002064
Adam Langley614c66a2015-06-12 15:26:58 -07002065static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2066 uint16_t value) {
2067 unsigned i;
2068 for (i = 0; i < kNumExtensions; i++) {
2069 if (kExtensions[i].value == value) {
2070 *out_index = i;
2071 return &kExtensions[i];
2072 }
2073 }
2074
2075 return NULL;
2076}
2077
Adam Langley09505632015-07-30 18:10:13 -07002078int SSL_extension_supported(unsigned extension_value) {
2079 uint32_t index;
2080 return extension_value == TLSEXT_TYPE_padding ||
2081 tls_extension_find(&index, extension_value) != NULL;
2082}
2083
David Benjamine8d53502015-10-10 14:13:23 -04002084int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002085 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002086 if (ssl->client_version == SSL3_VERSION &&
2087 !ssl->s3->send_connection_binding) {
2088 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002089 }
Adam Langley95c29f32014-06-20 12:00:00 -07002090
David Benjamine8d53502015-10-10 14:13:23 -04002091 CBB extensions;
2092 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002093 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002094 }
Adam Langley95c29f32014-06-20 12:00:00 -07002095
David Benjamine8d53502015-10-10 14:13:23 -04002096 ssl->s3->tmp.extensions.sent = 0;
2097 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002098
Adam Langley614c66a2015-06-12 15:26:58 -07002099 size_t i;
2100 for (i = 0; i < kNumExtensions; i++) {
2101 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002102 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002103 }
2104 }
Adam Langley95c29f32014-06-20 12:00:00 -07002105
Adam Langley614c66a2015-06-12 15:26:58 -07002106 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002107 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002108 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002109 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2110 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2111 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002112 }
Adam Langley95c29f32014-06-20 12:00:00 -07002113
Adam Langley33ad2b52015-07-20 17:43:53 -07002114 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002115 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002116 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002117 }
Adam Langley75712922014-10-10 16:23:43 -07002118
David Benjamine8d53502015-10-10 14:13:23 -04002119 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002120 goto err;
2121 }
2122
David Benjamine8d53502015-10-10 14:13:23 -04002123 if (!SSL_IS_DTLS(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002124 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002125 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002126 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002127 *
2128 * NB: because this code works out the length of all existing extensions
2129 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002130 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002131 /* Extensions take at least four bytes to encode. Always include least
2132 * one byte of data if including the extension. WebSphere Application
2133 * Server 7.0 is intolerant to the last extension being zero-length. */
2134 if (padding_len >= 4 + 1) {
2135 padding_len -= 4;
2136 } else {
2137 padding_len = 1;
2138 }
Adam Langley95c29f32014-06-20 12:00:00 -07002139
Adam Langley33ad2b52015-07-20 17:43:53 -07002140 uint8_t *padding_bytes;
2141 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2142 !CBB_add_u16(&extensions, padding_len) ||
2143 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2144 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002145 }
Adam Langley75712922014-10-10 16:23:43 -07002146
Adam Langley33ad2b52015-07-20 17:43:53 -07002147 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002148 }
2149 }
Adam Langley75712922014-10-10 16:23:43 -07002150
David Benjamina01deee2015-12-08 18:56:31 -05002151 /* Discard empty extensions blocks. */
2152 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002153 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002154 }
2155
David Benjamine8d53502015-10-10 14:13:23 -04002156 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002157
2158err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002159 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002160 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002161}
Adam Langley95c29f32014-06-20 12:00:00 -07002162
David Benjamin56380462015-10-10 14:59:09 -04002163int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002164 CBB extensions;
2165 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002166 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002167 }
2168
2169 unsigned i;
2170 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002171 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002172 /* Don't send extensions that were not received. */
2173 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002174 }
Adam Langley95c29f32014-06-20 12:00:00 -07002175
David Benjamin56380462015-10-10 14:59:09 -04002176 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002177 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2178 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2179 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002180 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002181 }
Adam Langley95c29f32014-06-20 12:00:00 -07002182
David Benjamin56380462015-10-10 14:59:09 -04002183 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002184 goto err;
2185 }
2186
David Benjamina01deee2015-12-08 18:56:31 -05002187 /* Discard empty extensions blocks. */
2188 if (CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002189 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002190 }
2191
David Benjamin56380462015-10-10 14:59:09 -04002192 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002193
2194err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002195 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002196 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002197}
Adam Langley95c29f32014-06-20 12:00:00 -07002198
David Benjamin0d56f882015-12-19 17:05:56 -05002199static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002200 size_t i;
2201 for (i = 0; i < kNumExtensions; i++) {
2202 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002203 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002204 }
2205 }
2206
David Benjamin0d56f882015-12-19 17:05:56 -05002207 ssl->s3->tmp.extensions.received = 0;
2208 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002209 /* The renegotiation extension must always be at index zero because the
2210 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2211 * sent as an SCSV. */
2212 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002213
Adam Langleyfcf25832014-12-18 17:42:32 -08002214 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002215 if (CBS_len(cbs) != 0) {
2216 /* Decode the extensions block and check it is valid. */
2217 CBS extensions;
2218 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2219 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002220 *out_alert = SSL_AD_DECODE_ERROR;
2221 return 0;
2222 }
Adam Langley95c29f32014-06-20 12:00:00 -07002223
Adam Langley33ad2b52015-07-20 17:43:53 -07002224 while (CBS_len(&extensions) != 0) {
2225 uint16_t type;
2226 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002227
Adam Langley33ad2b52015-07-20 17:43:53 -07002228 /* Decode the next extension. */
2229 if (!CBS_get_u16(&extensions, &type) ||
2230 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2231 *out_alert = SSL_AD_DECODE_ERROR;
2232 return 0;
2233 }
2234
David Benjaminc7ce9772015-10-09 19:32:41 -04002235 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2236 * ambiguous. Ignore all but the renegotiation_info extension. */
David Benjamin0d56f882015-12-19 17:05:56 -05002237 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
David Benjaminc7ce9772015-10-09 19:32:41 -04002238 continue;
2239 }
2240
Adam Langley33ad2b52015-07-20 17:43:53 -07002241 unsigned ext_index;
2242 const struct tls_extension *const ext =
2243 tls_extension_find(&ext_index, type);
2244
2245 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002246 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002247 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2248 return 0;
2249 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002250 continue;
2251 }
2252
David Benjamin0d56f882015-12-19 17:05:56 -05002253 ssl->s3->tmp.extensions.received |= (1u << ext_index);
Adam Langley614c66a2015-06-12 15:26:58 -07002254 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002255 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
Adam Langley614c66a2015-06-12 15:26:58 -07002256 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002257 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2258 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002259 return 0;
2260 }
Adam Langley614c66a2015-06-12 15:26:58 -07002261 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002262 }
Adam Langley75712922014-10-10 16:23:43 -07002263
Adam Langley614c66a2015-06-12 15:26:58 -07002264 for (i = 0; i < kNumExtensions; i++) {
David Benjamin0d56f882015-12-19 17:05:56 -05002265 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002266 /* Extension wasn't observed so call the callback with a NULL
2267 * parameter. */
2268 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002269 if (!kExtensions[i].parse_clienthello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002270 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2271 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002272 *out_alert = alert;
2273 return 0;
2274 }
2275 }
2276 }
2277
Adam Langleyfcf25832014-12-18 17:42:32 -08002278 return 1;
2279}
Adam Langley95c29f32014-06-20 12:00:00 -07002280
David Benjamin0d56f882015-12-19 17:05:56 -05002281int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002282 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002283 if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) {
2284 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002285 return 0;
2286 }
Adam Langley95c29f32014-06-20 12:00:00 -07002287
David Benjamin0d56f882015-12-19 17:05:56 -05002288 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002289 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002290 return 0;
2291 }
Adam Langley95c29f32014-06-20 12:00:00 -07002292
Adam Langleyfcf25832014-12-18 17:42:32 -08002293 return 1;
2294}
Adam Langley95c29f32014-06-20 12:00:00 -07002295
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002296OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2297
David Benjamin0d56f882015-12-19 17:05:56 -05002298static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002299 uint32_t received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002300
Adam Langley33ad2b52015-07-20 17:43:53 -07002301 if (CBS_len(cbs) != 0) {
2302 /* Decode the extensions block and check it is valid. */
2303 CBS extensions;
2304 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2305 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002306 *out_alert = SSL_AD_DECODE_ERROR;
2307 return 0;
2308 }
Adam Langley95c29f32014-06-20 12:00:00 -07002309
Adam Langley614c66a2015-06-12 15:26:58 -07002310
Adam Langley33ad2b52015-07-20 17:43:53 -07002311 while (CBS_len(&extensions) != 0) {
2312 uint16_t type;
2313 CBS extension;
2314
2315 /* Decode the next extension. */
2316 if (!CBS_get_u16(&extensions, &type) ||
2317 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2318 *out_alert = SSL_AD_DECODE_ERROR;
2319 return 0;
2320 }
2321
2322 unsigned ext_index;
2323 const struct tls_extension *const ext =
2324 tls_extension_find(&ext_index, type);
2325
Adam Langley09505632015-07-30 18:10:13 -07002326 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002327 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002328 return 0;
2329 }
2330 continue;
2331 }
2332
David Benjamin0d56f882015-12-19 17:05:56 -05002333 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) {
Adam Langley09505632015-07-30 18:10:13 -07002334 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002335 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002336 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002337 *out_alert = SSL_AD_DECODE_ERROR;
2338 return 0;
2339 }
David Benjamin03973092014-06-24 23:27:17 -04002340
Adam Langley614c66a2015-06-12 15:26:58 -07002341 received |= (1u << ext_index);
2342
2343 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002344 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002345 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2346 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002347 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002348 return 0;
2349 }
Adam Langley614c66a2015-06-12 15:26:58 -07002350 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002351 }
Adam Langley95c29f32014-06-20 12:00:00 -07002352
Adam Langley33ad2b52015-07-20 17:43:53 -07002353 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002354 for (i = 0; i < kNumExtensions; i++) {
2355 if (!(received & (1u << i))) {
2356 /* Extension wasn't observed so call the callback with a NULL
2357 * parameter. */
2358 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002359 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002360 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2361 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002362 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002363 return 0;
2364 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002365 }
2366 }
Adam Langley95c29f32014-06-20 12:00:00 -07002367
Adam Langleyfcf25832014-12-18 17:42:32 -08002368 return 1;
2369}
Adam Langley95c29f32014-06-20 12:00:00 -07002370
David Benjamin0d56f882015-12-19 17:05:56 -05002371static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002372 int ret = SSL_TLSEXT_ERR_NOACK;
2373 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002374
Adam Langleyfcf25832014-12-18 17:42:32 -08002375 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2376 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002377
David Benjamin0d56f882015-12-19 17:05:56 -05002378 if (ssl->ctx != NULL && ssl->ctx->tlsext_servername_callback != 0) {
2379 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
2380 ssl->ctx->tlsext_servername_arg);
2381 } else if (ssl->initial_ctx != NULL &&
2382 ssl->initial_ctx->tlsext_servername_callback != 0) {
2383 ret = ssl->initial_ctx->tlsext_servername_callback(
2384 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002385 }
Adam Langley95c29f32014-06-20 12:00:00 -07002386
Adam Langleyfcf25832014-12-18 17:42:32 -08002387 switch (ret) {
2388 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002389 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002390 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002391
Adam Langleyfcf25832014-12-18 17:42:32 -08002392 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002393 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002394 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002395
Adam Langleyfcf25832014-12-18 17:42:32 -08002396 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002397 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002398 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002399
Adam Langleyfcf25832014-12-18 17:42:32 -08002400 default:
2401 return 1;
2402 }
2403}
Adam Langleyed8270a2014-09-02 13:52:56 -07002404
David Benjamin0d56f882015-12-19 17:05:56 -05002405static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002406 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002407 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002408
David Benjamin0d56f882015-12-19 17:05:56 -05002409 if (ssl->ctx != NULL && ssl->ctx->tlsext_servername_callback != 0) {
2410 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
2411 ssl->ctx->tlsext_servername_arg);
2412 } else if (ssl->initial_ctx != NULL &&
2413 ssl->initial_ctx->tlsext_servername_callback != 0) {
2414 ret = ssl->initial_ctx->tlsext_servername_callback(
2415 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002416 }
Adam Langley95c29f32014-06-20 12:00:00 -07002417
Adam Langleyfcf25832014-12-18 17:42:32 -08002418 switch (ret) {
2419 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002420 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002421 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002422
Adam Langleyfcf25832014-12-18 17:42:32 -08002423 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002424 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002425 return 1;
2426
2427 default:
2428 return 1;
2429 }
2430}
2431
David Benjamin0d56f882015-12-19 17:05:56 -05002432int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002433 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002434 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2435 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002436 return 0;
2437 }
2438
David Benjamin0d56f882015-12-19 17:05:56 -05002439 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002440 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002441 return 0;
2442 }
2443
2444 return 1;
2445}
Adam Langley95c29f32014-06-20 12:00:00 -07002446
David Benjamine3aa1d92015-06-16 15:34:50 -04002447int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002448 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002449 size_t ticket_len, const uint8_t *session_id,
2450 size_t session_id_len) {
2451 int ret = 1; /* Most errors are non-fatal. */
2452 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2453 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002454
David Benjamine3aa1d92015-06-16 15:34:50 -04002455 HMAC_CTX hmac_ctx;
2456 HMAC_CTX_init(&hmac_ctx);
2457 EVP_CIPHER_CTX cipher_ctx;
2458 EVP_CIPHER_CTX_init(&cipher_ctx);
2459
David Benjaminef1b0092015-11-21 14:05:44 -05002460 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002461 *out_session = NULL;
2462
2463 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2464 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002465 }
2466
David Benjaminadcc3952015-04-26 13:07:57 -04002467 /* Ensure there is room for the key name and the largest IV
2468 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2469 * the maximum IV length should be well under the minimum size for the
2470 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002471 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2472 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002473 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002474 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002475
David Benjamine3aa1d92015-06-16 15:34:50 -04002476 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002477 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2478 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2479 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002480 if (cb_ret < 0) {
2481 ret = 0;
2482 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002483 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002484 if (cb_ret == 0) {
2485 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002486 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002487 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002488 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002489 }
2490 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002491 /* Check the key name matches. */
2492 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2493 SSL_TICKET_KEY_NAME_LEN) != 0) {
2494 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002495 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002496 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2497 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002498 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002499 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2500 ssl_ctx->tlsext_tick_aes_key, iv)) {
2501 ret = 0;
2502 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002503 }
2504 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002505 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002506
David Benjamine3aa1d92015-06-16 15:34:50 -04002507 /* Check the MAC at the end of the ticket. */
2508 uint8_t mac[EVP_MAX_MD_SIZE];
2509 size_t mac_len = HMAC_size(&hmac_ctx);
2510 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002511 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002512 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002513 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002514 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2515 HMAC_Final(&hmac_ctx, mac, NULL);
2516 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2517 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002518 }
2519
David Benjamine3aa1d92015-06-16 15:34:50 -04002520 /* Decrypt the session data. */
2521 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2522 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2523 mac_len;
2524 plaintext = OPENSSL_malloc(ciphertext_len);
2525 if (plaintext == NULL) {
2526 ret = 0;
2527 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002528 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002529 if (ciphertext_len >= INT_MAX) {
2530 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002531 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002532 int len1, len2;
2533 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2534 (int)ciphertext_len) ||
2535 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2536 ERR_clear_error(); /* Don't leave an error on the queue. */
2537 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002538 }
2539
David Benjamine3aa1d92015-06-16 15:34:50 -04002540 /* Decode the session. */
2541 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2542 if (session == NULL) {
2543 ERR_clear_error(); /* Don't leave an error on the queue. */
2544 goto done;
2545 }
2546
2547 /* Copy the client's session ID into the new session, to denote the ticket has
2548 * been accepted. */
2549 memcpy(session->session_id, session_id, session_id_len);
2550 session->session_id_length = session_id_len;
2551
2552 *out_session = session;
2553
2554done:
2555 OPENSSL_free(plaintext);
2556 HMAC_CTX_cleanup(&hmac_ctx);
2557 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2558 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002559}
Adam Langley95c29f32014-06-20 12:00:00 -07002560
2561/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002562typedef struct {
2563 int nid;
2564 int id;
2565} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002566
Adam Langleyfcf25832014-12-18 17:42:32 -08002567static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2568 {NID_sha1, TLSEXT_hash_sha1},
2569 {NID_sha224, TLSEXT_hash_sha224},
2570 {NID_sha256, TLSEXT_hash_sha256},
2571 {NID_sha384, TLSEXT_hash_sha384},
2572 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002573
Adam Langleyfcf25832014-12-18 17:42:32 -08002574static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2575 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002576
Adam Langleyfcf25832014-12-18 17:42:32 -08002577static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2578 size_t i;
2579 for (i = 0; i < tlen; i++) {
2580 if (table[i].nid == nid) {
2581 return table[i].id;
2582 }
2583 }
Adam Langley95c29f32014-06-20 12:00:00 -07002584
Adam Langleyfcf25832014-12-18 17:42:32 -08002585 return -1;
2586}
Adam Langley95c29f32014-06-20 12:00:00 -07002587
David Benjaminb4d65fd2015-05-29 17:11:21 -04002588int tls12_get_sigid(int pkey_type) {
2589 return tls12_find_id(pkey_type, tls12_sig,
2590 sizeof(tls12_sig) / sizeof(tls12_lookup));
2591}
2592
David Benjaminfc825122015-12-18 01:57:43 -05002593int tls12_add_sigandhash(SSL *ssl, CBB *out, const EVP_MD *md) {
2594 int md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2595 sizeof(tls12_md) / sizeof(tls12_lookup));
2596 int sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langley95c29f32014-06-20 12:00:00 -07002597
David Benjaminfc825122015-12-18 01:57:43 -05002598 return md_id != -1 &&
2599 sig_id != -1 &&
2600 CBB_add_u8(out, (uint8_t)md_id) &&
2601 CBB_add_u8(out, (uint8_t)sig_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08002602}
2603
Adam Langleyfcf25832014-12-18 17:42:32 -08002604const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2605 switch (hash_alg) {
2606 case TLSEXT_hash_md5:
2607 return EVP_md5();
2608
2609 case TLSEXT_hash_sha1:
2610 return EVP_sha1();
2611
2612 case TLSEXT_hash_sha224:
2613 return EVP_sha224();
2614
2615 case TLSEXT_hash_sha256:
2616 return EVP_sha256();
2617
2618 case TLSEXT_hash_sha384:
2619 return EVP_sha384();
2620
2621 case TLSEXT_hash_sha512:
2622 return EVP_sha512();
2623
2624 default:
2625 return NULL;
2626 }
2627}
Adam Langley95c29f32014-06-20 12:00:00 -07002628
David Benjaminec2f27d2014-11-13 19:17:25 -05002629/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2630 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002631static int tls12_get_pkey_type(uint8_t sig_alg) {
2632 switch (sig_alg) {
2633 case TLSEXT_signature_rsa:
2634 return EVP_PKEY_RSA;
2635
2636 case TLSEXT_signature_ecdsa:
2637 return EVP_PKEY_EC;
2638
2639 default:
2640 return -1;
2641 }
2642}
Adam Langley95c29f32014-06-20 12:00:00 -07002643
Steven Valdez0d62f262015-09-04 12:41:04 -04002644OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2,
2645 sizeof_tls_sigalgs_is_not_two);
Adam Langleyfcf25832014-12-18 17:42:32 -08002646
Steven Valdez0d62f262015-09-04 12:41:04 -04002647int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002648 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05002649 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002650 return 1;
2651 }
David Benjamincd996942014-07-20 16:23:51 -04002652
Steven Valdez0d62f262015-09-04 12:41:04 -04002653 CERT *const cert = ssl->cert;
2654 OPENSSL_free(cert->peer_sigalgs);
2655 cert->peer_sigalgs = NULL;
2656 cert->peer_sigalgslen = 0;
2657
2658 size_t num_sigalgs = CBS_len(in_sigalgs);
2659
2660 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002661 return 0;
2662 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002663 num_sigalgs /= 2;
2664
2665 /* supported_signature_algorithms in the certificate request is
2666 * allowed to be empty. */
2667 if (num_sigalgs == 0) {
2668 return 1;
2669 }
2670
2671 /* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two
2672 * (statically asserted above) and we just divided |num_sigalgs| by two. */
2673 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS));
2674 if (cert->peer_sigalgs == NULL) {
2675 return 0;
2676 }
2677 cert->peer_sigalgslen = num_sigalgs;
2678
2679 CBS sigalgs;
2680 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2681
2682 size_t i;
2683 for (i = 0; i < num_sigalgs; i++) {
2684 TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i];
2685 if (!CBS_get_u8(&sigalgs, &sigalg->rhash) ||
2686 !CBS_get_u8(&sigalgs, &sigalg->rsign)) {
2687 return 0;
2688 }
2689 }
Adam Langley95c29f32014-06-20 12:00:00 -07002690
Adam Langleyfcf25832014-12-18 17:42:32 -08002691 return 1;
2692}
David Benjaminec2f27d2014-11-13 19:17:25 -05002693
David Benjamind1d80782015-07-05 11:54:09 -04002694const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2695 CERT *cert = ssl->cert;
2696 int type = ssl_private_key_type(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -04002697 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002698
Steven Valdez0d62f262015-09-04 12:41:04 -04002699 static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
2700 NID_sha224, NID_sha1};
2701
2702 const int *digest_nids = kDefaultDigestList;
2703 size_t num_digest_nids =
2704 sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
2705 if (cert->digest_nids != NULL) {
2706 digest_nids = cert->digest_nids;
2707 num_digest_nids = cert->num_digest_nids;
2708 }
2709
2710 for (i = 0; i < num_digest_nids; i++) {
2711 const int digest_nid = digest_nids[i];
2712 for (j = 0; j < cert->peer_sigalgslen; j++) {
2713 const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash);
2714 if (md == NULL ||
2715 digest_nid != EVP_MD_type(md) ||
2716 tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) {
2717 continue;
2718 }
2719
2720 return md;
Adam Langleyfcf25832014-12-18 17:42:32 -08002721 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002722 }
Adam Langley95c29f32014-06-20 12:00:00 -07002723
Adam Langleyfcf25832014-12-18 17:42:32 -08002724 /* If no suitable digest may be found, default to SHA-1. */
2725 return EVP_sha1();
2726}
Adam Langley95c29f32014-06-20 12:00:00 -07002727
David Benjamind6a4ae92015-08-06 11:10:51 -04002728int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2729 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002730 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002731
2732 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002733 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2734 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002735 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002736
David Benjamind6a4ae92015-08-06 11:10:51 -04002737 static const char kClientIDMagic[] = "TLS Channel ID signature";
2738 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2739
2740 if (ssl->hit) {
2741 static const char kResumptionMagic[] = "Resumption";
2742 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2743 if (ssl->session->original_handshake_hash_len == 0) {
2744 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2745 goto err;
2746 }
2747 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2748 ssl->session->original_handshake_hash_len);
2749 }
2750
2751 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2752 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2753 sizeof(handshake_hash));
2754 if (handshake_hash_len < 0) {
2755 goto err;
2756 }
2757 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2758 unsigned len_u;
2759 EVP_DigestFinal_ex(&ctx, out, &len_u);
2760 *out_len = len_u;
2761
2762 ret = 1;
2763
2764err:
2765 EVP_MD_CTX_cleanup(&ctx);
2766 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002767}
Adam Langley1258b6a2014-06-20 12:00:00 -07002768
2769/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin0d56f882015-12-19 17:05:56 -05002770 * hashes in |ssl->session| so that Channel ID resumptions can sign that
2771 * data. */
2772int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002773 int digest_len;
2774 /* This function should never be called for a resumed session because the
2775 * handshake hashes that we wish to record are for the original, full
2776 * handshake. */
David Benjamin0d56f882015-12-19 17:05:56 -05002777 if (ssl->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002778 return -1;
2779 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002780
Adam Langleyfcf25832014-12-18 17:42:32 -08002781 digest_len =
David Benjamin0d56f882015-12-19 17:05:56 -05002782 tls1_handshake_digest(ssl, ssl->session->original_handshake_hash,
2783 sizeof(ssl->session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08002784 if (digest_len < 0) {
2785 return -1;
2786 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002787
David Benjamin0d56f882015-12-19 17:05:56 -05002788 ssl->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002789
Adam Langleyfcf25832014-12-18 17:42:32 -08002790 return 1;
2791}