blob: decf5fc532e4495780f2a3e2251e22d928cd56ea [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)
Adam Langleyfcf25832014-12-18 17:42:32 -0800513 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700514};
David Benjamin05da6e12014-07-12 20:42:55 -0400515
David Benjamin0d56f882015-12-19 17:05:56 -0500516size_t tls12_get_psigalgs(SSL *ssl, const uint8_t **psigs) {
David Benjamin3dd90162015-08-09 11:26:40 -0400517 *psigs = tls12_sigalgs;
518 return sizeof(tls12_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800519}
Adam Langley95c29f32014-06-20 12:00:00 -0700520
David Benjamin6e807652015-11-02 12:02:20 -0500521int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert,
522 uint8_t hash, uint8_t signature, EVP_PKEY *pkey) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800523 const uint8_t *sent_sigs;
524 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400525 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800526
527 /* Should never happen */
528 if (sigalg == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400529 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800530 *out_alert = SSL_AD_INTERNAL_ERROR;
531 return 0;
532 }
533
Adam Langleyfcf25832014-12-18 17:42:32 -0800534 /* Check key type is consistent with signature */
535 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400536 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800537 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
538 return 0;
539 }
540
Adam Langleyfcf25832014-12-18 17:42:32 -0800541 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500542 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800543 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
544 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
545 break;
546 }
547 }
548
David Benjamin788be4a2015-10-30 17:50:57 -0400549 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400550 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800551 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
552 return 0;
553 }
554
555 *out_md = tls12_get_hash(hash);
556 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400557 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800558 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
559 return 0;
560 }
561
562 return 1;
563}
564
565/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
566 * supported or doesn't appear in supported signature algorithms. Unlike
567 * ssl_cipher_get_disabled this applies to a specific session and not global
568 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500569void ssl_set_client_disabled(SSL *ssl) {
570 CERT *c = ssl->cert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800571 const uint8_t *sigalgs;
572 size_t i, sigalgslen;
573 int have_rsa = 0, have_ecdsa = 0;
574 c->mask_a = 0;
575 c->mask_k = 0;
576
Adam Langleyfcf25832014-12-18 17:42:32 -0800577 /* Now go through all signature algorithms seeing if we support any for RSA,
578 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500579 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800580 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
581 switch (sigalgs[1]) {
582 case TLSEXT_signature_rsa:
583 have_rsa = 1;
584 break;
585
586 case TLSEXT_signature_ecdsa:
587 have_ecdsa = 1;
588 break;
589 }
590 }
591
592 /* Disable auth if we don't include any appropriate signature algorithms. */
593 if (!have_rsa) {
594 c->mask_a |= SSL_aRSA;
595 }
596 if (!have_ecdsa) {
597 c->mask_a |= SSL_aECDSA;
598 }
599
600 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500601 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800602 c->mask_a |= SSL_aPSK;
603 c->mask_k |= SSL_kPSK;
604 }
605}
Adam Langley95c29f32014-06-20 12:00:00 -0700606
Adam Langley614c66a2015-06-12 15:26:58 -0700607/* tls_extension represents a TLS extension that is handled internally. The
608 * |init| function is called for each handshake, before any other functions of
609 * the extension. Then the add and parse callbacks are called as needed.
610 *
611 * The parse callbacks receive a |CBS| that contains the contents of the
612 * extension (i.e. not including the type and length bytes). If an extension is
613 * not received then the parse callbacks will be called with a NULL CBS so that
614 * they can do any processing needed to handle the absence of an extension.
615 *
616 * The add callbacks receive a |CBB| to which the extension can be appended but
617 * the function is responsible for appending the type and length bytes too.
618 *
619 * All callbacks return one for success and zero for error. If a parse function
620 * returns zero then a fatal alert with value |*out_alert| will be sent. If
621 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
622struct tls_extension {
623 uint16_t value;
624 void (*init)(SSL *ssl);
625
626 int (*add_clienthello)(SSL *ssl, CBB *out);
627 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
628
629 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
630 int (*add_serverhello)(SSL *ssl, CBB *out);
631};
632
633
634/* Server name indication (SNI).
635 *
636 * https://tools.ietf.org/html/rfc6066#section-3. */
637
638static void ext_sni_init(SSL *ssl) {
639 ssl->s3->tmp.should_ack_sni = 0;
640}
641
642static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
643 if (ssl->tlsext_hostname == NULL) {
644 return 1;
645 }
646
647 CBB contents, server_name_list, name;
648 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
649 !CBB_add_u16_length_prefixed(out, &contents) ||
650 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
651 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
652 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
653 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
654 strlen(ssl->tlsext_hostname)) ||
655 !CBB_flush(out)) {
656 return 0;
657 }
658
659 return 1;
660}
661
David Benjamin0d56f882015-12-19 17:05:56 -0500662static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
663 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700664 if (contents == NULL) {
665 return 1;
666 }
667
668 if (CBS_len(contents) != 0) {
669 return 0;
670 }
671
672 assert(ssl->tlsext_hostname != NULL);
673
674 if (!ssl->hit) {
675 assert(ssl->session->tlsext_hostname == NULL);
676 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
677 if (!ssl->session->tlsext_hostname) {
678 *out_alert = SSL_AD_INTERNAL_ERROR;
679 return 0;
680 }
681 }
682
683 return 1;
684}
685
David Benjamin0d56f882015-12-19 17:05:56 -0500686static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
687 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700688 if (contents == NULL) {
689 return 1;
690 }
691
692 /* The servername extension is treated as follows:
693 *
694 * - Only the hostname type is supported with a maximum length of 255.
695 * - The servername is rejected if too long or if it contains zeros, in
696 * which case an fatal alert is generated.
697 * - The servername field is maintained together with the session cache.
698 * - When a session is resumed, the servername callback is invoked in order
699 * to allow the application to position itself to the right context.
700 * - The servername is acknowledged if it is new for a session or when
701 * it is identical to a previously used for the same session.
702 * Applications can control the behaviour. They can at any time
703 * set a 'desirable' servername for a new SSL object. This can be the
704 * case for example with HTTPS when a Host: header field is received and
705 * a renegotiation is requested. In this case, a possible servername
706 * presented in the new client hello is only acknowledged if it matches
707 * the value of the Host: field.
708 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
709 * if they provide for changing an explicit servername context for the
710 * session,
711 * i.e. when the session has been established with a servername extension.
712 */
713
714 CBS server_name_list;
715 char have_seen_host_name = 0;
716
717 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
718 CBS_len(&server_name_list) == 0 ||
719 CBS_len(contents) != 0) {
720 return 0;
721 }
722
723 /* Decode each ServerName in the extension. */
724 while (CBS_len(&server_name_list) > 0) {
725 uint8_t name_type;
726 CBS host_name;
727
728 if (!CBS_get_u8(&server_name_list, &name_type) ||
729 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
730 return 0;
731 }
732
733 /* Only host_name is supported. */
734 if (name_type != TLSEXT_NAMETYPE_host_name) {
735 continue;
736 }
737
738 if (have_seen_host_name) {
739 /* The ServerNameList MUST NOT contain more than one name of the same
740 * name_type. */
741 return 0;
742 }
743
744 have_seen_host_name = 1;
745
746 if (CBS_len(&host_name) == 0 ||
747 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
748 CBS_contains_zero_byte(&host_name)) {
749 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
750 return 0;
751 }
752
753 if (!ssl->hit) {
754 assert(ssl->session->tlsext_hostname == NULL);
755 if (ssl->session->tlsext_hostname) {
756 /* This should be impossible. */
757 return 0;
758 }
759
760 /* Copy the hostname as a string. */
761 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
762 *out_alert = SSL_AD_INTERNAL_ERROR;
763 return 0;
764 }
765
766 ssl->s3->tmp.should_ack_sni = 1;
767 }
768 }
769
770 return 1;
771}
772
773static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
774 if (ssl->hit ||
775 !ssl->s3->tmp.should_ack_sni ||
776 ssl->session->tlsext_hostname == NULL) {
777 return 1;
778 }
779
780 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
781 !CBB_add_u16(out, 0 /* length */)) {
782 return 0;
783 }
784
785 return 1;
786}
787
788
Adam Langley5021b222015-06-12 18:27:58 -0700789/* Renegotiation indication.
790 *
791 * https://tools.ietf.org/html/rfc5746 */
792
793static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
794 CBB contents, prev_finished;
795 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
796 !CBB_add_u16_length_prefixed(out, &contents) ||
797 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
798 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
799 ssl->s3->previous_client_finished_len) ||
800 !CBB_flush(out)) {
801 return 0;
802 }
803
804 return 1;
805}
806
807static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
808 CBS *contents) {
David Benjamin3e052de2015-11-25 20:10:31 -0500809 /* Servers may not switch between omitting the extension and supporting it.
810 * See RFC 5746, sections 3.5 and 4.2. */
811 if (ssl->s3->initial_handshake_complete &&
812 (contents != NULL) != ssl->s3->send_connection_binding) {
813 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
814 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
815 return 0;
816 }
817
Adam Langley5021b222015-06-12 18:27:58 -0700818 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500819 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700820 * RI even on initial ServerHello because the client doesn't see any
821 * renegotiation during an attack. However this would mean we could not
822 * connect to any server which doesn't support RI.
823 *
David Benjamine9cddb82015-11-23 14:36:40 -0500824 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
825 * practical terms every client sets it so it's just assumed here. */
826 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700827 }
828
829 const size_t expected_len = ssl->s3->previous_client_finished_len +
830 ssl->s3->previous_server_finished_len;
831
832 /* Check for logic errors */
833 assert(!expected_len || ssl->s3->previous_client_finished_len);
834 assert(!expected_len || ssl->s3->previous_server_finished_len);
835
836 /* Parse out the extension contents. */
837 CBS renegotiated_connection;
838 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
839 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400840 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700841 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
842 return 0;
843 }
844
845 /* Check that the extension matches. */
846 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400847 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700848 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
849 return 0;
850 }
851
852 const uint8_t *d = CBS_data(&renegotiated_connection);
853 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
854 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400855 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700856 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
857 return 0;
858 }
859 d += ssl->s3->previous_client_finished_len;
860
861 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
862 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400863 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700864 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
865 return 0;
866 }
867 ssl->s3->send_connection_binding = 1;
868
869 return 1;
870}
871
872static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
873 CBS *contents) {
874 /* Renegotiation isn't supported as a server so this function should never be
875 * called after the initial handshake. */
876 assert(!ssl->s3->initial_handshake_complete);
877
878 CBS fake_contents;
879 static const uint8_t kFakeExtension[] = {0};
880
881 if (contents == NULL) {
882 if (ssl->s3->send_connection_binding) {
883 /* The renegotiation SCSV was received so pretend that we received a
884 * renegotiation extension. */
885 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
886 contents = &fake_contents;
887 /* We require that the renegotiation extension is at index zero of
888 * kExtensions. */
889 ssl->s3->tmp.extensions.received |= (1u << 0);
890 } else {
891 return 1;
892 }
893 }
894
895 CBS renegotiated_connection;
896
897 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
898 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400899 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700900 return 0;
901 }
902
903 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500904 if (!CBS_mem_equal(&renegotiated_connection,
905 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700906 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400907 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700908 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
909 return 0;
910 }
911
912 ssl->s3->send_connection_binding = 1;
913
914 return 1;
915}
916
917static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
918 CBB contents, prev_finished;
919 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
920 !CBB_add_u16_length_prefixed(out, &contents) ||
921 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
922 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
923 ssl->s3->previous_client_finished_len) ||
924 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
925 ssl->s3->previous_server_finished_len) ||
926 !CBB_flush(out)) {
927 return 0;
928 }
929
930 return 1;
931}
932
Adam Langley0a056712015-07-01 15:03:33 -0700933
934/* Extended Master Secret.
935 *
936 * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
937
938static void ext_ems_init(SSL *ssl) {
939 ssl->s3->tmp.extended_master_secret = 0;
940}
941
942static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
943 if (ssl->version == SSL3_VERSION) {
944 return 1;
945 }
946
947 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
948 !CBB_add_u16(out, 0 /* length */)) {
949 return 0;
950 }
951
952 return 1;
953}
954
955static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
956 CBS *contents) {
957 if (contents == NULL) {
958 return 1;
959 }
960
961 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
962 return 0;
963 }
964
965 ssl->s3->tmp.extended_master_secret = 1;
966 return 1;
967}
968
David Benjamin0d56f882015-12-19 17:05:56 -0500969static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
970 CBS *contents) {
Adam Langley0a056712015-07-01 15:03:33 -0700971 if (ssl->version == SSL3_VERSION || contents == NULL) {
972 return 1;
973 }
974
975 if (CBS_len(contents) != 0) {
976 return 0;
977 }
978
979 ssl->s3->tmp.extended_master_secret = 1;
980 return 1;
981}
982
983static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
984 if (!ssl->s3->tmp.extended_master_secret) {
985 return 1;
986 }
987
988 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
989 !CBB_add_u16(out, 0 /* length */)) {
990 return 0;
991 }
992
993 return 1;
994}
995
Adam Langley9b05bc52015-07-01 15:25:33 -0700996
997/* Session tickets.
998 *
999 * https://tools.ietf.org/html/rfc5077 */
1000
1001static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1002 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1003 return 1;
1004 }
1005
1006 const uint8_t *ticket_data = NULL;
1007 int ticket_len = 0;
1008
1009 /* Renegotiation does not participate in session resumption. However, still
1010 * advertise the extension to avoid potentially breaking servers which carry
1011 * over the state from the previous handshake, such as OpenSSL servers
1012 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1013 if (!ssl->s3->initial_handshake_complete &&
1014 ssl->session != NULL &&
1015 ssl->session->tlsext_tick != NULL) {
1016 ticket_data = ssl->session->tlsext_tick;
1017 ticket_len = ssl->session->tlsext_ticklen;
1018 }
1019
1020 CBB ticket;
1021 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1022 !CBB_add_u16_length_prefixed(out, &ticket) ||
1023 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1024 !CBB_flush(out)) {
1025 return 0;
1026 }
1027
1028 return 1;
1029}
1030
1031static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1032 CBS *contents) {
1033 ssl->tlsext_ticket_expected = 0;
1034
1035 if (contents == NULL) {
1036 return 1;
1037 }
1038
1039 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1040 * this function should never be called, even if the server tries to send the
1041 * extension. */
1042 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1043
1044 if (CBS_len(contents) != 0) {
1045 return 0;
1046 }
1047
1048 ssl->tlsext_ticket_expected = 1;
1049 return 1;
1050}
1051
David Benjamin0d56f882015-12-19 17:05:56 -05001052static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1053 CBS *contents) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001054 /* This function isn't used because the ticket extension from the client is
David Benjaminc7ce9772015-10-09 19:32:41 -04001055 * handled in ssl_session.c. */
Adam Langley9b05bc52015-07-01 15:25:33 -07001056 return 1;
1057}
1058
1059static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1060 if (!ssl->tlsext_ticket_expected) {
1061 return 1;
1062 }
1063
1064 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1065 * true. */
1066 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1067
1068 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1069 !CBB_add_u16(out, 0 /* length */)) {
1070 return 0;
1071 }
1072
1073 return 1;
1074}
1075
1076
Adam Langley2e857bd2015-07-01 16:09:19 -07001077/* Signature Algorithms.
1078 *
1079 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1080
1081static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1082 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1083 return 1;
1084 }
1085
1086 const uint8_t *sigalgs_data;
1087 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1088
1089 CBB contents, sigalgs;
1090 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1091 !CBB_add_u16_length_prefixed(out, &contents) ||
1092 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1093 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1094 !CBB_flush(out)) {
1095 return 0;
1096 }
1097
1098 return 1;
1099}
1100
1101static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1102 CBS *contents) {
1103 if (contents != NULL) {
1104 /* Servers MUST NOT send this extension. */
1105 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001106 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001107 return 0;
1108 }
1109
1110 return 1;
1111}
1112
1113static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1114 CBS *contents) {
1115 OPENSSL_free(ssl->cert->peer_sigalgs);
1116 ssl->cert->peer_sigalgs = NULL;
1117 ssl->cert->peer_sigalgslen = 0;
1118
Adam Langley2e857bd2015-07-01 16:09:19 -07001119 if (contents == NULL) {
1120 return 1;
1121 }
1122
1123 CBS supported_signature_algorithms;
1124 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001125 CBS_len(contents) != 0 ||
1126 CBS_len(&supported_signature_algorithms) == 0 ||
1127 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001128 return 0;
1129 }
1130
1131 return 1;
1132}
1133
1134static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1135 /* Servers MUST NOT send this extension. */
1136 return 1;
1137}
1138
1139
Adam Langleybb0bd042015-07-01 16:21:03 -07001140/* OCSP Stapling.
1141 *
1142 * https://tools.ietf.org/html/rfc6066#section-8 */
1143
1144static void ext_ocsp_init(SSL *ssl) {
1145 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001146 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001147}
1148
1149static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1150 if (!ssl->ocsp_stapling_enabled) {
1151 return 1;
1152 }
1153
1154 CBB contents;
1155 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1156 !CBB_add_u16_length_prefixed(out, &contents) ||
1157 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1158 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1159 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1160 !CBB_flush(out)) {
1161 return 0;
1162 }
1163
Adam Langleyce9d85e2016-01-24 15:58:39 -08001164 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001165 return 1;
1166}
1167
1168static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001169 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001170 if (contents == NULL) {
1171 return 1;
1172 }
1173
1174 if (CBS_len(contents) != 0) {
1175 return 0;
1176 }
1177
1178 ssl->s3->tmp.certificate_status_expected = 1;
1179 return 1;
1180}
1181
1182static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1183 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001184 if (contents == NULL) {
1185 return 1;
1186 }
1187
1188 uint8_t status_type;
1189 if (!CBS_get_u8(contents, &status_type)) {
1190 return 0;
1191 }
1192
1193 /* We cannot decide whether OCSP stapling will occur yet because the correct
1194 * SSL_CTX might not have been selected. */
1195 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1196
Adam Langleybb0bd042015-07-01 16:21:03 -07001197 return 1;
1198}
1199
1200static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001201 /* The extension shouldn't be sent when resuming sessions. */
1202 if (ssl->hit ||
1203 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001204 ssl->ctx->ocsp_response_length == 0) {
1205 return 1;
1206 }
1207
1208 ssl->s3->tmp.certificate_status_expected = 1;
1209
1210 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1211 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001212}
1213
1214
Adam Langley97dfcbf2015-07-01 18:35:20 -07001215/* Next protocol negotiation.
1216 *
1217 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1218
1219static void ext_npn_init(SSL *ssl) {
1220 ssl->s3->next_proto_neg_seen = 0;
1221}
1222
1223static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1224 if (ssl->s3->initial_handshake_complete ||
1225 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001226 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001227 SSL_IS_DTLS(ssl)) {
1228 return 1;
1229 }
1230
1231 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1232 !CBB_add_u16(out, 0 /* length */)) {
1233 return 0;
1234 }
1235
1236 return 1;
1237}
1238
1239static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1240 CBS *contents) {
1241 if (contents == NULL) {
1242 return 1;
1243 }
1244
1245 /* If any of these are false then we should never have sent the NPN
1246 * extension in the ClientHello and thus this function should never have been
1247 * called. */
1248 assert(!ssl->s3->initial_handshake_complete);
1249 assert(!SSL_IS_DTLS(ssl));
1250 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001251 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001252
David Benjamin76c2efc2015-08-31 14:24:29 -04001253 if (ssl->s3->alpn_selected != NULL) {
1254 /* NPN and ALPN may not be negotiated in the same connection. */
1255 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1256 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1257 return 0;
1258 }
1259
Adam Langley97dfcbf2015-07-01 18:35:20 -07001260 const uint8_t *const orig_contents = CBS_data(contents);
1261 const size_t orig_len = CBS_len(contents);
1262
1263 while (CBS_len(contents) != 0) {
1264 CBS proto;
1265 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1266 CBS_len(&proto) == 0) {
1267 return 0;
1268 }
1269 }
1270
1271 uint8_t *selected;
1272 uint8_t selected_len;
1273 if (ssl->ctx->next_proto_select_cb(
1274 ssl, &selected, &selected_len, orig_contents, orig_len,
1275 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1276 *out_alert = SSL_AD_INTERNAL_ERROR;
1277 return 0;
1278 }
1279
David Benjamin79978df2015-12-25 15:56:49 -05001280 OPENSSL_free(ssl->s3->next_proto_negotiated);
1281 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1282 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001283 *out_alert = SSL_AD_INTERNAL_ERROR;
1284 return 0;
1285 }
1286
David Benjamin79978df2015-12-25 15:56:49 -05001287 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001288 ssl->s3->next_proto_neg_seen = 1;
1289
1290 return 1;
1291}
1292
1293static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1294 CBS *contents) {
1295 if (contents != NULL && CBS_len(contents) != 0) {
1296 return 0;
1297 }
1298
1299 if (contents == NULL ||
1300 ssl->s3->initial_handshake_complete ||
1301 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1302 * afterwards, parsing the ALPN extension will clear
1303 * |next_proto_neg_seen|. */
1304 ssl->s3->alpn_selected != NULL ||
1305 ssl->ctx->next_protos_advertised_cb == NULL ||
1306 SSL_IS_DTLS(ssl)) {
1307 return 1;
1308 }
1309
1310 ssl->s3->next_proto_neg_seen = 1;
1311 return 1;
1312}
1313
1314static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1315 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1316 * parsed. */
1317 if (!ssl->s3->next_proto_neg_seen) {
1318 return 1;
1319 }
1320
1321 const uint8_t *npa;
1322 unsigned npa_len;
1323
1324 if (ssl->ctx->next_protos_advertised_cb(
1325 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1326 SSL_TLSEXT_ERR_OK) {
1327 ssl->s3->next_proto_neg_seen = 0;
1328 return 1;
1329 }
1330
1331 CBB contents;
1332 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1333 !CBB_add_u16_length_prefixed(out, &contents) ||
1334 !CBB_add_bytes(&contents, npa, npa_len) ||
1335 !CBB_flush(out)) {
1336 return 0;
1337 }
1338
1339 return 1;
1340}
1341
1342
Adam Langleyab8d87d2015-07-10 12:21:39 -07001343/* Signed certificate timestamps.
1344 *
1345 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1346
1347static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1348 if (!ssl->signed_cert_timestamps_enabled) {
1349 return 1;
1350 }
1351
1352 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1353 !CBB_add_u16(out, 0 /* length */)) {
1354 return 0;
1355 }
1356
1357 return 1;
1358}
1359
1360static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1361 CBS *contents) {
1362 if (contents == NULL) {
1363 return 1;
1364 }
1365
1366 /* If this is false then we should never have sent the SCT extension in the
1367 * ClientHello and thus this function should never have been called. */
1368 assert(ssl->signed_cert_timestamps_enabled);
1369
1370 if (CBS_len(contents) == 0) {
1371 *out_alert = SSL_AD_DECODE_ERROR;
1372 return 0;
1373 }
1374
1375 /* Session resumption uses the original session information. */
1376 if (!ssl->hit &&
1377 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1378 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1379 *out_alert = SSL_AD_INTERNAL_ERROR;
1380 return 0;
1381 }
1382
1383 return 1;
1384}
1385
1386static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1387 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001388 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001389}
1390
1391static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001392 /* The extension shouldn't be sent when resuming sessions. */
1393 if (ssl->hit ||
1394 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001395 return 1;
1396 }
1397
1398 CBB contents;
1399 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1400 CBB_add_u16_length_prefixed(out, &contents) &&
1401 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1402 ssl->ctx->signed_cert_timestamp_list_length) &&
1403 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001404}
1405
1406
Adam Langleyf18e4532015-07-10 13:39:53 -07001407/* Application-level Protocol Negotiation.
1408 *
1409 * https://tools.ietf.org/html/rfc7301 */
1410
1411static void ext_alpn_init(SSL *ssl) {
1412 OPENSSL_free(ssl->s3->alpn_selected);
1413 ssl->s3->alpn_selected = NULL;
1414}
1415
1416static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1417 if (ssl->alpn_client_proto_list == NULL ||
1418 ssl->s3->initial_handshake_complete) {
1419 return 1;
1420 }
1421
1422 CBB contents, proto_list;
1423 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1424 !CBB_add_u16_length_prefixed(out, &contents) ||
1425 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1426 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1427 ssl->alpn_client_proto_list_len) ||
1428 !CBB_flush(out)) {
1429 return 0;
1430 }
1431
1432 return 1;
1433}
1434
1435static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1436 CBS *contents) {
1437 if (contents == NULL) {
1438 return 1;
1439 }
1440
1441 assert(!ssl->s3->initial_handshake_complete);
1442 assert(ssl->alpn_client_proto_list != NULL);
1443
David Benjamin76c2efc2015-08-31 14:24:29 -04001444 if (ssl->s3->next_proto_neg_seen) {
1445 /* NPN and ALPN may not be negotiated in the same connection. */
1446 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1447 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1448 return 0;
1449 }
1450
Adam Langleyf18e4532015-07-10 13:39:53 -07001451 /* The extension data consists of a ProtocolNameList which must have
1452 * exactly one ProtocolName. Each of these is length-prefixed. */
1453 CBS protocol_name_list, protocol_name;
1454 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1455 CBS_len(contents) != 0 ||
1456 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1457 /* Empty protocol names are forbidden. */
1458 CBS_len(&protocol_name) == 0 ||
1459 CBS_len(&protocol_name_list) != 0) {
1460 return 0;
1461 }
1462
1463 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1464 &ssl->s3->alpn_selected_len)) {
1465 *out_alert = SSL_AD_INTERNAL_ERROR;
1466 return 0;
1467 }
1468
1469 return 1;
1470}
1471
1472static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1473 CBS *contents) {
1474 if (contents == NULL) {
1475 return 1;
1476 }
1477
1478 if (ssl->ctx->alpn_select_cb == NULL ||
1479 ssl->s3->initial_handshake_complete) {
1480 return 1;
1481 }
1482
1483 /* ALPN takes precedence over NPN. */
1484 ssl->s3->next_proto_neg_seen = 0;
1485
1486 CBS protocol_name_list;
1487 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1488 CBS_len(contents) != 0 ||
1489 CBS_len(&protocol_name_list) < 2) {
1490 return 0;
1491 }
1492
1493 /* Validate the protocol list. */
1494 CBS protocol_name_list_copy = protocol_name_list;
1495 while (CBS_len(&protocol_name_list_copy) > 0) {
1496 CBS protocol_name;
1497
1498 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1499 /* Empty protocol names are forbidden. */
1500 CBS_len(&protocol_name) == 0) {
1501 return 0;
1502 }
1503 }
1504
1505 const uint8_t *selected;
1506 uint8_t selected_len;
1507 if (ssl->ctx->alpn_select_cb(
1508 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1509 CBS_len(&protocol_name_list),
1510 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1511 OPENSSL_free(ssl->s3->alpn_selected);
1512 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1513 if (ssl->s3->alpn_selected == NULL) {
1514 *out_alert = SSL_AD_INTERNAL_ERROR;
1515 return 0;
1516 }
1517 ssl->s3->alpn_selected_len = selected_len;
1518 }
1519
1520 return 1;
1521}
1522
1523static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1524 if (ssl->s3->alpn_selected == NULL) {
1525 return 1;
1526 }
1527
1528 CBB contents, proto_list, proto;
1529 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1530 !CBB_add_u16_length_prefixed(out, &contents) ||
1531 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1532 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001533 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1534 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001535 !CBB_flush(out)) {
1536 return 0;
1537 }
1538
1539 return 1;
1540}
1541
1542
Adam Langley49c7af12015-07-10 14:33:46 -07001543/* Channel ID.
1544 *
1545 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1546
1547static void ext_channel_id_init(SSL *ssl) {
1548 ssl->s3->tlsext_channel_id_valid = 0;
1549}
1550
1551static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1552 if (!ssl->tlsext_channel_id_enabled ||
1553 SSL_IS_DTLS(ssl)) {
1554 return 1;
1555 }
1556
1557 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1558 !CBB_add_u16(out, 0 /* length */)) {
1559 return 0;
1560 }
1561
1562 return 1;
1563}
1564
1565static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1566 CBS *contents) {
1567 if (contents == NULL) {
1568 return 1;
1569 }
1570
1571 assert(!SSL_IS_DTLS(ssl));
1572 assert(ssl->tlsext_channel_id_enabled);
1573
1574 if (CBS_len(contents) != 0) {
1575 return 0;
1576 }
1577
1578 ssl->s3->tlsext_channel_id_valid = 1;
1579 return 1;
1580}
1581
1582static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1583 CBS *contents) {
1584 if (contents == NULL ||
1585 !ssl->tlsext_channel_id_enabled ||
1586 SSL_IS_DTLS(ssl)) {
1587 return 1;
1588 }
1589
1590 if (CBS_len(contents) != 0) {
1591 return 0;
1592 }
1593
1594 ssl->s3->tlsext_channel_id_valid = 1;
1595 return 1;
1596}
1597
1598static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1599 if (!ssl->s3->tlsext_channel_id_valid) {
1600 return 1;
1601 }
1602
1603 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1604 !CBB_add_u16(out, 0 /* length */)) {
1605 return 0;
1606 }
1607
1608 return 1;
1609}
1610
Adam Langley391250d2015-07-15 19:06:07 -07001611
1612/* Secure Real-time Transport Protocol (SRTP) extension.
1613 *
1614 * https://tools.ietf.org/html/rfc5764 */
1615
Adam Langley391250d2015-07-15 19:06:07 -07001616
1617static void ext_srtp_init(SSL *ssl) {
1618 ssl->srtp_profile = NULL;
1619}
1620
1621static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1622 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1623 if (profiles == NULL) {
1624 return 1;
1625 }
1626 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1627 if (num_profiles == 0) {
1628 return 1;
1629 }
1630
1631 CBB contents, profile_ids;
1632 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1633 !CBB_add_u16_length_prefixed(out, &contents) ||
1634 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1635 return 0;
1636 }
1637
1638 size_t i;
1639 for (i = 0; i < num_profiles; i++) {
1640 if (!CBB_add_u16(&profile_ids,
1641 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1642 return 0;
1643 }
1644 }
1645
1646 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1647 !CBB_flush(out)) {
1648 return 0;
1649 }
1650
1651 return 1;
1652}
1653
1654static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1655 CBS *contents) {
1656 if (contents == NULL) {
1657 return 1;
1658 }
1659
1660 /* The extension consists of a u16-prefixed profile ID list containing a
1661 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1662 *
1663 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1664 CBS profile_ids, srtp_mki;
1665 uint16_t profile_id;
1666 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1667 !CBS_get_u16(&profile_ids, &profile_id) ||
1668 CBS_len(&profile_ids) != 0 ||
1669 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1670 CBS_len(contents) != 0) {
1671 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1672 return 0;
1673 }
1674
1675 if (CBS_len(&srtp_mki) != 0) {
1676 /* Must be no MKI, since we never offer one. */
1677 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1678 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1679 return 0;
1680 }
1681
1682 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1683
1684 /* Check to see if the server gave us something we support (and presumably
1685 * offered). */
1686 size_t i;
1687 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1688 const SRTP_PROTECTION_PROFILE *profile =
1689 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1690
1691 if (profile->id == profile_id) {
1692 ssl->srtp_profile = profile;
1693 return 1;
1694 }
1695 }
1696
1697 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1698 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1699 return 0;
1700}
1701
1702static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1703 CBS *contents) {
1704 if (contents == NULL) {
1705 return 1;
1706 }
1707
1708 CBS profile_ids, srtp_mki;
1709 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1710 CBS_len(&profile_ids) < 2 ||
1711 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1712 CBS_len(contents) != 0) {
1713 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1714 return 0;
1715 }
1716 /* Discard the MKI value for now. */
1717
1718 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1719 SSL_get_srtp_profiles(ssl);
1720
1721 /* Pick the server's most preferred profile. */
1722 size_t i;
1723 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1724 const SRTP_PROTECTION_PROFILE *server_profile =
1725 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1726
1727 CBS profile_ids_tmp;
1728 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1729
1730 while (CBS_len(&profile_ids_tmp) > 0) {
1731 uint16_t profile_id;
1732 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1733 return 0;
1734 }
1735
1736 if (server_profile->id == profile_id) {
1737 ssl->srtp_profile = server_profile;
1738 return 1;
1739 }
1740 }
1741 }
1742
1743 return 1;
1744}
1745
1746static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1747 if (ssl->srtp_profile == NULL) {
1748 return 1;
1749 }
1750
1751 CBB contents, profile_ids;
1752 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1753 !CBB_add_u16_length_prefixed(out, &contents) ||
1754 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1755 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1756 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1757 !CBB_flush(out)) {
1758 return 0;
1759 }
1760
1761 return 1;
1762}
1763
Adam Langleybdd5d662015-07-20 16:19:08 -07001764
1765/* EC point formats.
1766 *
1767 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1768
1769static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1770 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1771 return 0;
1772 }
1773
1774 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1775
1776 size_t i;
1777 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1778 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1779
1780 const uint32_t alg_k = cipher->algorithm_mkey;
1781 const uint32_t alg_a = cipher->algorithm_auth;
1782 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1783 return 1;
1784 }
1785 }
1786
1787 return 0;
1788}
1789
Adam Langleybdd5d662015-07-20 16:19:08 -07001790static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001791 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001792 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1793 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001794 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1795 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001796 !CBB_flush(out)) {
1797 return 0;
1798 }
1799
1800 return 1;
1801}
1802
1803static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1804 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1805 return 1;
1806 }
1807
1808 return ext_ec_point_add_extension(ssl, out);
1809}
1810
1811static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1812 CBS *contents) {
1813 if (contents == NULL) {
1814 return 1;
1815 }
1816
1817 CBS ec_point_format_list;
1818 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1819 CBS_len(contents) != 0) {
1820 return 0;
1821 }
1822
David Benjaminfc059942015-07-30 23:01:59 -04001823 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1824 * point format. */
1825 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1826 CBS_len(&ec_point_format_list)) == NULL) {
1827 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001828 return 0;
1829 }
1830
1831 return 1;
1832}
1833
1834static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1835 CBS *contents) {
1836 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1837}
1838
1839static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1840 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1841 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001842 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001843
1844 if (!using_ecc) {
1845 return 1;
1846 }
1847
1848 return ext_ec_point_add_extension(ssl, out);
1849}
1850
Adam Langley273d49c2015-07-20 16:38:52 -07001851
1852/* EC supported curves.
1853 *
1854 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1855
1856static void ext_ec_curves_init(SSL *ssl) {
1857 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1858 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1859 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
1860}
1861
1862static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
1863 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1864 return 1;
1865 }
1866
1867 CBB contents, curves_bytes;
1868 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
1869 !CBB_add_u16_length_prefixed(out, &contents) ||
1870 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
1871 return 0;
1872 }
1873
1874 const uint16_t *curves;
1875 size_t curves_len;
1876 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
1877
1878 size_t i;
1879 for (i = 0; i < curves_len; i++) {
1880 if (!CBB_add_u16(&curves_bytes, curves[i])) {
1881 return 0;
1882 }
1883 }
1884
1885 return CBB_flush(out);
1886}
1887
1888static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1889 CBS *contents) {
1890 /* This extension is not expected to be echoed by servers and is ignored. */
1891 return 1;
1892}
1893
1894static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1895 CBS *contents) {
1896 if (contents == NULL) {
1897 return 1;
1898 }
1899
1900 CBS elliptic_curve_list;
1901 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
1902 CBS_len(&elliptic_curve_list) == 0 ||
1903 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
1904 CBS_len(contents) != 0) {
1905 return 0;
1906 }
1907
1908 ssl->s3->tmp.peer_ellipticcurvelist =
1909 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
1910
1911 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
1912 *out_alert = SSL_AD_INTERNAL_ERROR;
1913 return 0;
1914 }
1915
1916 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
1917 size_t i;
1918 for (i = 0; i < num_curves; i++) {
1919 if (!CBS_get_u16(&elliptic_curve_list,
1920 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
1921 goto err;
1922 }
1923 }
1924
1925 assert(CBS_len(&elliptic_curve_list) == 0);
1926 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
1927
1928 return 1;
1929
1930err:
1931 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1932 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1933 *out_alert = SSL_AD_INTERNAL_ERROR;
1934 return 0;
1935}
1936
1937static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
1938 /* Servers don't echo this extension. */
1939 return 1;
1940}
1941
1942
Adam Langley614c66a2015-06-12 15:26:58 -07001943/* kExtensions contains all the supported extensions. */
1944static const struct tls_extension kExtensions[] = {
1945 {
Adam Langley5021b222015-06-12 18:27:58 -07001946 /* The renegotiation extension must always be at index zero because the
1947 * |received| and |sent| bitsets need to be tweaked when the "extension" is
1948 * sent as an SCSV. */
1949 TLSEXT_TYPE_renegotiate,
1950 NULL,
1951 ext_ri_add_clienthello,
1952 ext_ri_parse_serverhello,
1953 ext_ri_parse_clienthello,
1954 ext_ri_add_serverhello,
1955 },
1956 {
Adam Langley614c66a2015-06-12 15:26:58 -07001957 TLSEXT_TYPE_server_name,
1958 ext_sni_init,
1959 ext_sni_add_clienthello,
1960 ext_sni_parse_serverhello,
1961 ext_sni_parse_clienthello,
1962 ext_sni_add_serverhello,
1963 },
Adam Langley0a056712015-07-01 15:03:33 -07001964 {
1965 TLSEXT_TYPE_extended_master_secret,
1966 ext_ems_init,
1967 ext_ems_add_clienthello,
1968 ext_ems_parse_serverhello,
1969 ext_ems_parse_clienthello,
1970 ext_ems_add_serverhello,
1971 },
Adam Langley9b05bc52015-07-01 15:25:33 -07001972 {
1973 TLSEXT_TYPE_session_ticket,
1974 NULL,
1975 ext_ticket_add_clienthello,
1976 ext_ticket_parse_serverhello,
1977 ext_ticket_parse_clienthello,
1978 ext_ticket_add_serverhello,
1979 },
Adam Langley2e857bd2015-07-01 16:09:19 -07001980 {
1981 TLSEXT_TYPE_signature_algorithms,
1982 NULL,
1983 ext_sigalgs_add_clienthello,
1984 ext_sigalgs_parse_serverhello,
1985 ext_sigalgs_parse_clienthello,
1986 ext_sigalgs_add_serverhello,
1987 },
Adam Langleybb0bd042015-07-01 16:21:03 -07001988 {
1989 TLSEXT_TYPE_status_request,
1990 ext_ocsp_init,
1991 ext_ocsp_add_clienthello,
1992 ext_ocsp_parse_serverhello,
1993 ext_ocsp_parse_clienthello,
1994 ext_ocsp_add_serverhello,
1995 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07001996 {
1997 TLSEXT_TYPE_next_proto_neg,
1998 ext_npn_init,
1999 ext_npn_add_clienthello,
2000 ext_npn_parse_serverhello,
2001 ext_npn_parse_clienthello,
2002 ext_npn_add_serverhello,
2003 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002004 {
2005 TLSEXT_TYPE_certificate_timestamp,
2006 NULL,
2007 ext_sct_add_clienthello,
2008 ext_sct_parse_serverhello,
2009 ext_sct_parse_clienthello,
2010 ext_sct_add_serverhello,
2011 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002012 {
2013 TLSEXT_TYPE_application_layer_protocol_negotiation,
2014 ext_alpn_init,
2015 ext_alpn_add_clienthello,
2016 ext_alpn_parse_serverhello,
2017 ext_alpn_parse_clienthello,
2018 ext_alpn_add_serverhello,
2019 },
Adam Langley49c7af12015-07-10 14:33:46 -07002020 {
2021 TLSEXT_TYPE_channel_id,
2022 ext_channel_id_init,
2023 ext_channel_id_add_clienthello,
2024 ext_channel_id_parse_serverhello,
2025 ext_channel_id_parse_clienthello,
2026 ext_channel_id_add_serverhello,
2027 },
Adam Langley391250d2015-07-15 19:06:07 -07002028 {
2029 TLSEXT_TYPE_srtp,
2030 ext_srtp_init,
2031 ext_srtp_add_clienthello,
2032 ext_srtp_parse_serverhello,
2033 ext_srtp_parse_clienthello,
2034 ext_srtp_add_serverhello,
2035 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002036 {
2037 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002038 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002039 ext_ec_point_add_clienthello,
2040 ext_ec_point_parse_serverhello,
2041 ext_ec_point_parse_clienthello,
2042 ext_ec_point_add_serverhello,
2043 },
Adam Langley273d49c2015-07-20 16:38:52 -07002044 {
2045 TLSEXT_TYPE_elliptic_curves,
2046 ext_ec_curves_init,
2047 ext_ec_curves_add_clienthello,
2048 ext_ec_curves_parse_serverhello,
2049 ext_ec_curves_parse_clienthello,
2050 ext_ec_curves_add_serverhello,
2051 },
Adam Langley614c66a2015-06-12 15:26:58 -07002052};
2053
2054#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2055
Adam Langley4cfa96b2015-07-01 11:56:55 -07002056OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2057 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002058 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002059OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2060 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2061 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002062 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002063
Adam Langley614c66a2015-06-12 15:26:58 -07002064static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2065 uint16_t value) {
2066 unsigned i;
2067 for (i = 0; i < kNumExtensions; i++) {
2068 if (kExtensions[i].value == value) {
2069 *out_index = i;
2070 return &kExtensions[i];
2071 }
2072 }
2073
2074 return NULL;
2075}
2076
Adam Langley09505632015-07-30 18:10:13 -07002077int SSL_extension_supported(unsigned extension_value) {
2078 uint32_t index;
2079 return extension_value == TLSEXT_TYPE_padding ||
2080 tls_extension_find(&index, extension_value) != NULL;
2081}
2082
David Benjamine8d53502015-10-10 14:13:23 -04002083int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002084 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002085 if (ssl->client_version == SSL3_VERSION &&
2086 !ssl->s3->send_connection_binding) {
2087 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002088 }
Adam Langley95c29f32014-06-20 12:00:00 -07002089
David Benjamine8d53502015-10-10 14:13:23 -04002090 CBB extensions;
2091 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002092 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002093 }
Adam Langley95c29f32014-06-20 12:00:00 -07002094
David Benjamine8d53502015-10-10 14:13:23 -04002095 ssl->s3->tmp.extensions.sent = 0;
2096 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002097
Adam Langley614c66a2015-06-12 15:26:58 -07002098 size_t i;
2099 for (i = 0; i < kNumExtensions; i++) {
2100 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002101 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002102 }
2103 }
Adam Langley95c29f32014-06-20 12:00:00 -07002104
Adam Langley614c66a2015-06-12 15:26:58 -07002105 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002106 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002107 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002108 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2109 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2110 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002111 }
Adam Langley95c29f32014-06-20 12:00:00 -07002112
Adam Langley33ad2b52015-07-20 17:43:53 -07002113 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002114 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002115 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002116 }
Adam Langley75712922014-10-10 16:23:43 -07002117
David Benjamine8d53502015-10-10 14:13:23 -04002118 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002119 goto err;
2120 }
2121
David Benjamine8d53502015-10-10 14:13:23 -04002122 if (!SSL_IS_DTLS(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002123 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002124 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002125 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002126 *
2127 * NB: because this code works out the length of all existing extensions
2128 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002129 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002130 /* Extensions take at least four bytes to encode. Always include least
2131 * one byte of data if including the extension. WebSphere Application
2132 * Server 7.0 is intolerant to the last extension being zero-length. */
2133 if (padding_len >= 4 + 1) {
2134 padding_len -= 4;
2135 } else {
2136 padding_len = 1;
2137 }
Adam Langley95c29f32014-06-20 12:00:00 -07002138
Adam Langley33ad2b52015-07-20 17:43:53 -07002139 uint8_t *padding_bytes;
2140 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2141 !CBB_add_u16(&extensions, padding_len) ||
2142 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2143 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002144 }
Adam Langley75712922014-10-10 16:23:43 -07002145
Adam Langley33ad2b52015-07-20 17:43:53 -07002146 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002147 }
2148 }
Adam Langley75712922014-10-10 16:23:43 -07002149
David Benjamina01deee2015-12-08 18:56:31 -05002150 /* Discard empty extensions blocks. */
2151 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002152 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002153 }
2154
David Benjamine8d53502015-10-10 14:13:23 -04002155 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002156
2157err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002158 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002159 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002160}
Adam Langley95c29f32014-06-20 12:00:00 -07002161
David Benjamin56380462015-10-10 14:59:09 -04002162int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002163 CBB extensions;
2164 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002165 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002166 }
2167
2168 unsigned i;
2169 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002170 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002171 /* Don't send extensions that were not received. */
2172 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002173 }
Adam Langley95c29f32014-06-20 12:00:00 -07002174
David Benjamin56380462015-10-10 14:59:09 -04002175 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002176 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2177 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2178 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002179 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002180 }
Adam Langley95c29f32014-06-20 12:00:00 -07002181
David Benjamin56380462015-10-10 14:59:09 -04002182 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002183 goto err;
2184 }
2185
David Benjamina01deee2015-12-08 18:56:31 -05002186 /* Discard empty extensions blocks. */
2187 if (CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002188 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002189 }
2190
David Benjamin56380462015-10-10 14:59:09 -04002191 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002192
2193err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002194 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002195 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002196}
Adam Langley95c29f32014-06-20 12:00:00 -07002197
David Benjamin0d56f882015-12-19 17:05:56 -05002198static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002199 size_t i;
2200 for (i = 0; i < kNumExtensions; i++) {
2201 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002202 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002203 }
2204 }
2205
David Benjamin0d56f882015-12-19 17:05:56 -05002206 ssl->s3->tmp.extensions.received = 0;
2207 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002208 /* The renegotiation extension must always be at index zero because the
2209 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2210 * sent as an SCSV. */
2211 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002212
Adam Langleyfcf25832014-12-18 17:42:32 -08002213 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002214 if (CBS_len(cbs) != 0) {
2215 /* Decode the extensions block and check it is valid. */
2216 CBS extensions;
2217 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2218 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002219 *out_alert = SSL_AD_DECODE_ERROR;
2220 return 0;
2221 }
Adam Langley95c29f32014-06-20 12:00:00 -07002222
Adam Langley33ad2b52015-07-20 17:43:53 -07002223 while (CBS_len(&extensions) != 0) {
2224 uint16_t type;
2225 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002226
Adam Langley33ad2b52015-07-20 17:43:53 -07002227 /* Decode the next extension. */
2228 if (!CBS_get_u16(&extensions, &type) ||
2229 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2230 *out_alert = SSL_AD_DECODE_ERROR;
2231 return 0;
2232 }
2233
David Benjaminc7ce9772015-10-09 19:32:41 -04002234 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2235 * ambiguous. Ignore all but the renegotiation_info extension. */
David Benjamin0d56f882015-12-19 17:05:56 -05002236 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
David Benjaminc7ce9772015-10-09 19:32:41 -04002237 continue;
2238 }
2239
Adam Langley33ad2b52015-07-20 17:43:53 -07002240 unsigned ext_index;
2241 const struct tls_extension *const ext =
2242 tls_extension_find(&ext_index, type);
2243
2244 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002245 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002246 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2247 return 0;
2248 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002249 continue;
2250 }
2251
David Benjamin0d56f882015-12-19 17:05:56 -05002252 ssl->s3->tmp.extensions.received |= (1u << ext_index);
Adam Langley614c66a2015-06-12 15:26:58 -07002253 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002254 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
Adam Langley614c66a2015-06-12 15:26:58 -07002255 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002256 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2257 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002258 return 0;
2259 }
Adam Langley614c66a2015-06-12 15:26:58 -07002260 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002261 }
Adam Langley75712922014-10-10 16:23:43 -07002262
Adam Langley614c66a2015-06-12 15:26:58 -07002263 for (i = 0; i < kNumExtensions; i++) {
David Benjamin0d56f882015-12-19 17:05:56 -05002264 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002265 /* Extension wasn't observed so call the callback with a NULL
2266 * parameter. */
2267 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002268 if (!kExtensions[i].parse_clienthello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002269 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2270 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002271 *out_alert = alert;
2272 return 0;
2273 }
2274 }
2275 }
2276
Adam Langleyfcf25832014-12-18 17:42:32 -08002277 return 1;
2278}
Adam Langley95c29f32014-06-20 12:00:00 -07002279
David Benjamin0d56f882015-12-19 17:05:56 -05002280int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002281 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002282 if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) {
2283 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002284 return 0;
2285 }
Adam Langley95c29f32014-06-20 12:00:00 -07002286
David Benjamin0d56f882015-12-19 17:05:56 -05002287 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002288 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002289 return 0;
2290 }
Adam Langley95c29f32014-06-20 12:00:00 -07002291
Adam Langleyfcf25832014-12-18 17:42:32 -08002292 return 1;
2293}
Adam Langley95c29f32014-06-20 12:00:00 -07002294
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002295OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2296
David Benjamin0d56f882015-12-19 17:05:56 -05002297static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002298 uint32_t received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002299
Adam Langley33ad2b52015-07-20 17:43:53 -07002300 if (CBS_len(cbs) != 0) {
2301 /* Decode the extensions block and check it is valid. */
2302 CBS extensions;
2303 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2304 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002305 *out_alert = SSL_AD_DECODE_ERROR;
2306 return 0;
2307 }
Adam Langley95c29f32014-06-20 12:00:00 -07002308
Adam Langley614c66a2015-06-12 15:26:58 -07002309
Adam Langley33ad2b52015-07-20 17:43:53 -07002310 while (CBS_len(&extensions) != 0) {
2311 uint16_t type;
2312 CBS extension;
2313
2314 /* Decode the next extension. */
2315 if (!CBS_get_u16(&extensions, &type) ||
2316 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2317 *out_alert = SSL_AD_DECODE_ERROR;
2318 return 0;
2319 }
2320
2321 unsigned ext_index;
2322 const struct tls_extension *const ext =
2323 tls_extension_find(&ext_index, type);
2324
Adam Langley09505632015-07-30 18:10:13 -07002325 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002326 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002327 return 0;
2328 }
2329 continue;
2330 }
2331
David Benjamin0d56f882015-12-19 17:05:56 -05002332 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) {
Adam Langley09505632015-07-30 18:10:13 -07002333 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002334 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002335 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002336 *out_alert = SSL_AD_DECODE_ERROR;
2337 return 0;
2338 }
David Benjamin03973092014-06-24 23:27:17 -04002339
Adam Langley614c66a2015-06-12 15:26:58 -07002340 received |= (1u << ext_index);
2341
2342 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002343 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002344 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2345 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002346 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002347 return 0;
2348 }
Adam Langley614c66a2015-06-12 15:26:58 -07002349 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002350 }
Adam Langley95c29f32014-06-20 12:00:00 -07002351
Adam Langley33ad2b52015-07-20 17:43:53 -07002352 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002353 for (i = 0; i < kNumExtensions; i++) {
2354 if (!(received & (1u << i))) {
2355 /* Extension wasn't observed so call the callback with a NULL
2356 * parameter. */
2357 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002358 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002359 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2360 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002361 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002362 return 0;
2363 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002364 }
2365 }
Adam Langley95c29f32014-06-20 12:00:00 -07002366
Adam Langleyfcf25832014-12-18 17:42:32 -08002367 return 1;
2368}
Adam Langley95c29f32014-06-20 12:00:00 -07002369
David Benjamin0d56f882015-12-19 17:05:56 -05002370static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002371 int ret = SSL_TLSEXT_ERR_NOACK;
2372 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002373
Adam Langleyfcf25832014-12-18 17:42:32 -08002374 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2375 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002376
David Benjamin0d56f882015-12-19 17:05:56 -05002377 if (ssl->ctx != NULL && ssl->ctx->tlsext_servername_callback != 0) {
2378 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
2379 ssl->ctx->tlsext_servername_arg);
2380 } else if (ssl->initial_ctx != NULL &&
2381 ssl->initial_ctx->tlsext_servername_callback != 0) {
2382 ret = ssl->initial_ctx->tlsext_servername_callback(
2383 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002384 }
Adam Langley95c29f32014-06-20 12:00:00 -07002385
Adam Langleyfcf25832014-12-18 17:42:32 -08002386 switch (ret) {
2387 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002388 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002389 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002390
Adam Langleyfcf25832014-12-18 17:42:32 -08002391 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002392 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002393 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002394
Adam Langleyfcf25832014-12-18 17:42:32 -08002395 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002396 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002397 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002398
Adam Langleyfcf25832014-12-18 17:42:32 -08002399 default:
2400 return 1;
2401 }
2402}
Adam Langleyed8270a2014-09-02 13:52:56 -07002403
David Benjamin0d56f882015-12-19 17:05:56 -05002404static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002405 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002406 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002407
David Benjamin0d56f882015-12-19 17:05:56 -05002408 if (ssl->ctx != NULL && ssl->ctx->tlsext_servername_callback != 0) {
2409 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
2410 ssl->ctx->tlsext_servername_arg);
2411 } else if (ssl->initial_ctx != NULL &&
2412 ssl->initial_ctx->tlsext_servername_callback != 0) {
2413 ret = ssl->initial_ctx->tlsext_servername_callback(
2414 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002415 }
Adam Langley95c29f32014-06-20 12:00:00 -07002416
Adam Langleyfcf25832014-12-18 17:42:32 -08002417 switch (ret) {
2418 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002419 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002420 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002421
Adam Langleyfcf25832014-12-18 17:42:32 -08002422 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002423 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002424 return 1;
2425
2426 default:
2427 return 1;
2428 }
2429}
2430
David Benjamin0d56f882015-12-19 17:05:56 -05002431int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002432 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002433 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2434 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002435 return 0;
2436 }
2437
David Benjamin0d56f882015-12-19 17:05:56 -05002438 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002439 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002440 return 0;
2441 }
2442
2443 return 1;
2444}
Adam Langley95c29f32014-06-20 12:00:00 -07002445
David Benjamine3aa1d92015-06-16 15:34:50 -04002446int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002447 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002448 size_t ticket_len, const uint8_t *session_id,
2449 size_t session_id_len) {
2450 int ret = 1; /* Most errors are non-fatal. */
2451 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2452 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002453
David Benjamine3aa1d92015-06-16 15:34:50 -04002454 HMAC_CTX hmac_ctx;
2455 HMAC_CTX_init(&hmac_ctx);
2456 EVP_CIPHER_CTX cipher_ctx;
2457 EVP_CIPHER_CTX_init(&cipher_ctx);
2458
David Benjaminef1b0092015-11-21 14:05:44 -05002459 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002460 *out_session = NULL;
2461
2462 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2463 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002464 }
2465
David Benjaminadcc3952015-04-26 13:07:57 -04002466 /* Ensure there is room for the key name and the largest IV
2467 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2468 * the maximum IV length should be well under the minimum size for the
2469 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002470 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2471 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002472 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002473 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002474
David Benjamine3aa1d92015-06-16 15:34:50 -04002475 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002476 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2477 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2478 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002479 if (cb_ret < 0) {
2480 ret = 0;
2481 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002482 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002483 if (cb_ret == 0) {
2484 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002485 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002486 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002487 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002488 }
2489 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002490 /* Check the key name matches. */
2491 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2492 SSL_TICKET_KEY_NAME_LEN) != 0) {
2493 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002494 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002495 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2496 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002497 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002498 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2499 ssl_ctx->tlsext_tick_aes_key, iv)) {
2500 ret = 0;
2501 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002502 }
2503 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002504 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002505
David Benjamine3aa1d92015-06-16 15:34:50 -04002506 /* Check the MAC at the end of the ticket. */
2507 uint8_t mac[EVP_MAX_MD_SIZE];
2508 size_t mac_len = HMAC_size(&hmac_ctx);
2509 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002510 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002511 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002512 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002513 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2514 HMAC_Final(&hmac_ctx, mac, NULL);
2515 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2516 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002517 }
2518
David Benjamine3aa1d92015-06-16 15:34:50 -04002519 /* Decrypt the session data. */
2520 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2521 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2522 mac_len;
2523 plaintext = OPENSSL_malloc(ciphertext_len);
2524 if (plaintext == NULL) {
2525 ret = 0;
2526 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002527 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002528 if (ciphertext_len >= INT_MAX) {
2529 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002530 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002531 int len1, len2;
2532 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2533 (int)ciphertext_len) ||
2534 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2535 ERR_clear_error(); /* Don't leave an error on the queue. */
2536 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002537 }
2538
David Benjamine3aa1d92015-06-16 15:34:50 -04002539 /* Decode the session. */
2540 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2541 if (session == NULL) {
2542 ERR_clear_error(); /* Don't leave an error on the queue. */
2543 goto done;
2544 }
2545
2546 /* Copy the client's session ID into the new session, to denote the ticket has
2547 * been accepted. */
2548 memcpy(session->session_id, session_id, session_id_len);
2549 session->session_id_length = session_id_len;
2550
2551 *out_session = session;
2552
2553done:
2554 OPENSSL_free(plaintext);
2555 HMAC_CTX_cleanup(&hmac_ctx);
2556 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2557 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002558}
Adam Langley95c29f32014-06-20 12:00:00 -07002559
2560/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002561typedef struct {
2562 int nid;
2563 int id;
2564} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002565
David Benjamin72f7e212016-01-29 15:28:58 -05002566static const tls12_lookup tls12_md[] = {
2567 {NID_sha1, TLSEXT_hash_sha1},
2568 {NID_sha256, TLSEXT_hash_sha256},
2569 {NID_sha384, TLSEXT_hash_sha384},
2570 {NID_sha512, TLSEXT_hash_sha512},
2571};
Adam Langley95c29f32014-06-20 12:00:00 -07002572
Adam Langleyfcf25832014-12-18 17:42:32 -08002573static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2574 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002575
Adam Langleyfcf25832014-12-18 17:42:32 -08002576static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2577 size_t i;
2578 for (i = 0; i < tlen; i++) {
2579 if (table[i].nid == nid) {
2580 return table[i].id;
2581 }
2582 }
Adam Langley95c29f32014-06-20 12:00:00 -07002583
Adam Langleyfcf25832014-12-18 17:42:32 -08002584 return -1;
2585}
Adam Langley95c29f32014-06-20 12:00:00 -07002586
David Benjaminb4d65fd2015-05-29 17:11:21 -04002587int tls12_get_sigid(int pkey_type) {
2588 return tls12_find_id(pkey_type, tls12_sig,
2589 sizeof(tls12_sig) / sizeof(tls12_lookup));
2590}
2591
David Benjaminfc825122015-12-18 01:57:43 -05002592int tls12_add_sigandhash(SSL *ssl, CBB *out, const EVP_MD *md) {
2593 int md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2594 sizeof(tls12_md) / sizeof(tls12_lookup));
2595 int sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langley95c29f32014-06-20 12:00:00 -07002596
David Benjaminfc825122015-12-18 01:57:43 -05002597 return md_id != -1 &&
2598 sig_id != -1 &&
2599 CBB_add_u8(out, (uint8_t)md_id) &&
2600 CBB_add_u8(out, (uint8_t)sig_id);
Adam Langleyfcf25832014-12-18 17:42:32 -08002601}
2602
Adam Langleyfcf25832014-12-18 17:42:32 -08002603const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2604 switch (hash_alg) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002605 case TLSEXT_hash_sha1:
2606 return EVP_sha1();
2607
Adam Langleyfcf25832014-12-18 17:42:32 -08002608 case TLSEXT_hash_sha256:
2609 return EVP_sha256();
2610
2611 case TLSEXT_hash_sha384:
2612 return EVP_sha384();
2613
2614 case TLSEXT_hash_sha512:
2615 return EVP_sha512();
2616
2617 default:
2618 return NULL;
2619 }
2620}
Adam Langley95c29f32014-06-20 12:00:00 -07002621
David Benjaminec2f27d2014-11-13 19:17:25 -05002622/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2623 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002624static int tls12_get_pkey_type(uint8_t sig_alg) {
2625 switch (sig_alg) {
2626 case TLSEXT_signature_rsa:
2627 return EVP_PKEY_RSA;
2628
2629 case TLSEXT_signature_ecdsa:
2630 return EVP_PKEY_EC;
2631
2632 default:
2633 return -1;
2634 }
2635}
Adam Langley95c29f32014-06-20 12:00:00 -07002636
Steven Valdez0d62f262015-09-04 12:41:04 -04002637OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2,
2638 sizeof_tls_sigalgs_is_not_two);
Adam Langleyfcf25832014-12-18 17:42:32 -08002639
Steven Valdez0d62f262015-09-04 12:41:04 -04002640int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002641 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05002642 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002643 return 1;
2644 }
David Benjamincd996942014-07-20 16:23:51 -04002645
Steven Valdez0d62f262015-09-04 12:41:04 -04002646 CERT *const cert = ssl->cert;
2647 OPENSSL_free(cert->peer_sigalgs);
2648 cert->peer_sigalgs = NULL;
2649 cert->peer_sigalgslen = 0;
2650
2651 size_t num_sigalgs = CBS_len(in_sigalgs);
2652
2653 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002654 return 0;
2655 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002656 num_sigalgs /= 2;
2657
2658 /* supported_signature_algorithms in the certificate request is
2659 * allowed to be empty. */
2660 if (num_sigalgs == 0) {
2661 return 1;
2662 }
2663
2664 /* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two
2665 * (statically asserted above) and we just divided |num_sigalgs| by two. */
2666 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS));
2667 if (cert->peer_sigalgs == NULL) {
2668 return 0;
2669 }
2670 cert->peer_sigalgslen = num_sigalgs;
2671
2672 CBS sigalgs;
2673 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2674
2675 size_t i;
2676 for (i = 0; i < num_sigalgs; i++) {
2677 TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i];
2678 if (!CBS_get_u8(&sigalgs, &sigalg->rhash) ||
2679 !CBS_get_u8(&sigalgs, &sigalg->rsign)) {
2680 return 0;
2681 }
2682 }
Adam Langley95c29f32014-06-20 12:00:00 -07002683
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 return 1;
2685}
David Benjaminec2f27d2014-11-13 19:17:25 -05002686
David Benjamind1d80782015-07-05 11:54:09 -04002687const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2688 CERT *cert = ssl->cert;
2689 int type = ssl_private_key_type(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -04002690 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002691
Steven Valdez0d62f262015-09-04 12:41:04 -04002692 static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
David Benjamin72f7e212016-01-29 15:28:58 -05002693 NID_sha1};
Steven Valdez0d62f262015-09-04 12:41:04 -04002694
2695 const int *digest_nids = kDefaultDigestList;
2696 size_t num_digest_nids =
2697 sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
2698 if (cert->digest_nids != NULL) {
2699 digest_nids = cert->digest_nids;
2700 num_digest_nids = cert->num_digest_nids;
2701 }
2702
2703 for (i = 0; i < num_digest_nids; i++) {
2704 const int digest_nid = digest_nids[i];
2705 for (j = 0; j < cert->peer_sigalgslen; j++) {
2706 const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash);
2707 if (md == NULL ||
2708 digest_nid != EVP_MD_type(md) ||
2709 tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) {
2710 continue;
2711 }
2712
2713 return md;
Adam Langleyfcf25832014-12-18 17:42:32 -08002714 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002715 }
Adam Langley95c29f32014-06-20 12:00:00 -07002716
Adam Langleyfcf25832014-12-18 17:42:32 -08002717 /* If no suitable digest may be found, default to SHA-1. */
2718 return EVP_sha1();
2719}
Adam Langley95c29f32014-06-20 12:00:00 -07002720
David Benjamind6a4ae92015-08-06 11:10:51 -04002721int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2722 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002723 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002724
2725 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002726 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2727 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002728 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002729
David Benjamind6a4ae92015-08-06 11:10:51 -04002730 static const char kClientIDMagic[] = "TLS Channel ID signature";
2731 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2732
2733 if (ssl->hit) {
2734 static const char kResumptionMagic[] = "Resumption";
2735 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2736 if (ssl->session->original_handshake_hash_len == 0) {
2737 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2738 goto err;
2739 }
2740 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2741 ssl->session->original_handshake_hash_len);
2742 }
2743
2744 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2745 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2746 sizeof(handshake_hash));
2747 if (handshake_hash_len < 0) {
2748 goto err;
2749 }
2750 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2751 unsigned len_u;
2752 EVP_DigestFinal_ex(&ctx, out, &len_u);
2753 *out_len = len_u;
2754
2755 ret = 1;
2756
2757err:
2758 EVP_MD_CTX_cleanup(&ctx);
2759 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002760}
Adam Langley1258b6a2014-06-20 12:00:00 -07002761
2762/* tls1_record_handshake_hashes_for_channel_id records the current handshake
David Benjamin0d56f882015-12-19 17:05:56 -05002763 * hashes in |ssl->session| so that Channel ID resumptions can sign that
2764 * data. */
2765int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002766 int digest_len;
2767 /* This function should never be called for a resumed session because the
2768 * handshake hashes that we wish to record are for the original, full
2769 * handshake. */
David Benjamin0d56f882015-12-19 17:05:56 -05002770 if (ssl->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002771 return -1;
2772 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002773
Adam Langleyfcf25832014-12-18 17:42:32 -08002774 digest_len =
David Benjamin0d56f882015-12-19 17:05:56 -05002775 tls1_handshake_digest(ssl, ssl->session->original_handshake_hash,
2776 sizeof(ssl->session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08002777 if (digest_len < 0) {
2778 return -1;
2779 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002780
David Benjamin0d56f882015-12-19 17:05:56 -05002781 ssl->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002782
Adam Langleyfcf25832014-12-18 17:42:32 -08002783 return 1;
2784}