blob: c0ef97ef7ffecc6ecc50cc9cd07627dadd35dd4a [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 Benjamin6c7aed02014-08-27 16:42:38 -0400130static int ssl_check_clienthello_tlsext(SSL *s);
131static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700132
David Benjamin338fcaf2014-12-11 01:20:52 -0500133const SSL3_ENC_METHOD TLSv1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500134 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800135 tls1_setup_key_block,
136 tls1_generate_master_secret,
137 tls1_change_cipher_state,
138 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800139 tls1_cert_verify_mac,
140 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
141 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
142 tls1_alert_code,
143 tls1_export_keying_material,
144 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800145};
Adam Langley95c29f32014-06-20 12:00:00 -0700146
David Benjamin338fcaf2014-12-11 01:20:52 -0500147const SSL3_ENC_METHOD TLSv1_1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500148 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800149 tls1_setup_key_block,
150 tls1_generate_master_secret,
151 tls1_change_cipher_state,
152 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800153 tls1_cert_verify_mac,
154 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
155 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
156 tls1_alert_code,
157 tls1_export_keying_material,
158 SSL_ENC_FLAG_EXPLICIT_IV,
Adam Langleyfcf25832014-12-18 17:42:32 -0800159};
Adam Langley95c29f32014-06-20 12:00:00 -0700160
David Benjamin338fcaf2014-12-11 01:20:52 -0500161const SSL3_ENC_METHOD TLSv1_2_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500162 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800163 tls1_setup_key_block,
164 tls1_generate_master_secret,
165 tls1_change_cipher_state,
166 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 tls1_cert_verify_mac,
168 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
169 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
170 tls1_alert_code,
171 tls1_export_keying_material,
David Benjaminc7817d82015-11-05 18:28:33 -0500172 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF,
Adam Langleyfcf25832014-12-18 17:42:32 -0800173};
Adam Langley95c29f32014-06-20 12:00:00 -0700174
Adam Langleyfcf25832014-12-18 17:42:32 -0800175static int compare_uint16_t(const void *p1, const void *p2) {
176 uint16_t u1 = *((const uint16_t *)p1);
177 uint16_t u2 = *((const uint16_t *)p2);
178 if (u1 < u2) {
179 return -1;
180 } else if (u1 > u2) {
181 return 1;
182 } else {
183 return 0;
184 }
185}
David Benjamin35a7a442014-07-05 00:23:20 -0400186
Adam Langleyfcf25832014-12-18 17:42:32 -0800187/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
188 * more than one extension of the same type in a ClientHello or ServerHello.
189 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400190 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800191static int tls1_check_duplicate_extensions(const CBS *cbs) {
192 CBS extensions = *cbs;
193 size_t num_extensions = 0, i = 0;
194 uint16_t *extension_types = NULL;
195 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400196
Adam Langleyfcf25832014-12-18 17:42:32 -0800197 /* First pass: count the extensions. */
198 while (CBS_len(&extensions) > 0) {
199 uint16_t type;
200 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400201
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 if (!CBS_get_u16(&extensions, &type) ||
203 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
204 goto done;
205 }
David Benjamin35a7a442014-07-05 00:23:20 -0400206
Adam Langleyfcf25832014-12-18 17:42:32 -0800207 num_extensions++;
208 }
David Benjamin35a7a442014-07-05 00:23:20 -0400209
Adam Langleyfcf25832014-12-18 17:42:32 -0800210 if (num_extensions == 0) {
211 return 1;
212 }
David Benjamin9a373592014-07-25 04:27:53 -0400213
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 extension_types =
215 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
216 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400217 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 goto done;
219 }
David Benjamin35a7a442014-07-05 00:23:20 -0400220
Adam Langleyfcf25832014-12-18 17:42:32 -0800221 /* Second pass: gather the extension types. */
222 extensions = *cbs;
223 for (i = 0; i < num_extensions; i++) {
224 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
227 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
228 /* This should not happen. */
229 goto done;
230 }
231 }
232 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400233
Adam Langleyfcf25832014-12-18 17:42:32 -0800234 /* Sort the extensions and make sure there are no duplicates. */
235 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
236 for (i = 1; i < num_extensions; i++) {
237 if (extension_types[i - 1] == extension_types[i]) {
238 goto done;
239 }
240 }
David Benjamin35a7a442014-07-05 00:23:20 -0400241
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 ret = 1;
243
David Benjamin35a7a442014-07-05 00:23:20 -0400244done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400245 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 return ret;
247}
David Benjamin35a7a442014-07-05 00:23:20 -0400248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
250 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400251
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700253
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 if (/* Skip client version. */
255 !CBS_skip(&client_hello, 2) ||
256 /* Skip client nonce. */
257 !CBS_skip(&client_hello, 32) ||
258 /* Extract session_id. */
259 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
260 return 0;
261 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700262
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 ctx->session_id = CBS_data(&session_id);
264 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 /* Skip past DTLS cookie */
267 if (SSL_IS_DTLS(ctx->ssl)) {
268 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700269
Adam Langleyfcf25832014-12-18 17:42:32 -0800270 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
271 return 0;
272 }
273 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700274
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 /* Extract cipher_suites. */
276 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
277 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
278 return 0;
279 }
280 ctx->cipher_suites = CBS_data(&cipher_suites);
281 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700282
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 /* Extract compression_methods. */
284 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
285 CBS_len(&compression_methods) < 1) {
286 return 0;
287 }
288 ctx->compression_methods = CBS_data(&compression_methods);
289 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700290
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 /* If the ClientHello ends here then it's valid, but doesn't have any
292 * extensions. (E.g. SSLv3.) */
293 if (CBS_len(&client_hello) == 0) {
294 ctx->extensions = NULL;
295 ctx->extensions_len = 0;
296 return 1;
297 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700298
Adam Langleyfcf25832014-12-18 17:42:32 -0800299 /* Extract extensions and check it is valid. */
300 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
301 !tls1_check_duplicate_extensions(&extensions) ||
302 CBS_len(&client_hello) != 0) {
303 return 0;
304 }
305 ctx->extensions = CBS_data(&extensions);
306 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700307
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 return 1;
309}
Adam Langleydc9b1412014-06-20 12:00:00 -0700310
David Benjamind4c2bce2015-10-17 12:28:18 -0400311int SSL_early_callback_ctx_extension_get(
Adam Langleyfcf25832014-12-18 17:42:32 -0800312 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
313 const uint8_t **out_data, size_t *out_len) {
314 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700315
Adam Langleyfcf25832014-12-18 17:42:32 -0800316 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700317
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 while (CBS_len(&extensions) != 0) {
319 uint16_t type;
320 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400321
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 /* Decode the next extension. */
323 if (!CBS_get_u16(&extensions, &type) ||
324 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
325 return 0;
326 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700327
Adam Langleyfcf25832014-12-18 17:42:32 -0800328 if (type == extension_type) {
329 *out_data = CBS_data(&extension);
330 *out_len = CBS_len(&extension);
331 return 1;
332 }
333 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700334
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 return 0;
336}
Adam Langley95c29f32014-06-20 12:00:00 -0700337
David Benjamin52e5bac2014-12-27 02:27:35 -0500338struct tls_curve {
339 uint16_t curve_id;
340 int nid;
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200341 const char curve_name[8];
David Benjamin52e5bac2014-12-27 02:27:35 -0500342};
343
David Benjamin70bd80a2014-12-27 03:06:46 -0500344/* ECC curves from RFC4492. */
David Benjamin52e5bac2014-12-27 02:27:35 -0500345static const struct tls_curve tls_curves[] = {
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200346 {21, NID_secp224r1, "P-224"},
347 {23, NID_X9_62_prime256v1, "P-256"},
348 {24, NID_secp384r1, "P-384"},
349 {25, NID_secp521r1, "P-521"},
Adam Langleyfcf25832014-12-18 17:42:32 -0800350};
Adam Langley95c29f32014-06-20 12:00:00 -0700351
Adam Langleyfcf25832014-12-18 17:42:32 -0800352static const uint16_t eccurves_default[] = {
David Benjamin55a43642015-04-20 14:45:55 -0400353 23, /* X9_62_prime256v1 */
David Benjamin52e5bac2014-12-27 02:27:35 -0500354 24, /* secp384r1 */
Adam Langleyd98dc132015-09-23 16:41:33 -0700355#if defined(BORINGSSL_ANDROID_SYSTEM)
356 25, /* secp521r1 */
357#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800358};
Adam Langley95c29f32014-06-20 12:00:00 -0700359
Adam Langleyfcf25832014-12-18 17:42:32 -0800360int tls1_ec_curve_id2nid(uint16_t curve_id) {
David Benjamin52e5bac2014-12-27 02:27:35 -0500361 size_t i;
362 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
363 if (curve_id == tls_curves[i].curve_id) {
364 return tls_curves[i].nid;
365 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 }
David Benjamin52e5bac2014-12-27 02:27:35 -0500367 return NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800368}
Adam Langley95c29f32014-06-20 12:00:00 -0700369
David Benjamin70bd80a2014-12-27 03:06:46 -0500370int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 size_t i;
David Benjamin52e5bac2014-12-27 02:27:35 -0500372 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
373 if (nid == tls_curves[i].nid) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500374 *out_curve_id = tls_curves[i].curve_id;
375 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 }
377 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 return 0;
379}
380
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200381const char* tls1_ec_curve_id2name(uint16_t curve_id) {
382 size_t i;
383 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
384 if (curve_id == tls_curves[i].curve_id) {
385 return tls_curves[i].curve_name;
386 }
387 }
388 return NULL;
389}
390
Adam Langleyfcf25832014-12-18 17:42:32 -0800391/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
392 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
393 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400394static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800395 const uint16_t **out_curve_ids,
396 size_t *out_curve_ids_len) {
397 if (get_peer_curves) {
David Benjamin55a43642015-04-20 14:45:55 -0400398 /* Only clients send a curve list, so this function is only called
399 * on the server. */
400 assert(s->server);
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
402 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
403 return;
404 }
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 *out_curve_ids = s->tlsext_ellipticcurvelist;
407 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
408 if (!*out_curve_ids) {
409 *out_curve_ids = eccurves_default;
410 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
411 }
412}
David Benjamined439582014-07-14 19:13:02 -0400413
Adam Langleyfcf25832014-12-18 17:42:32 -0800414int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
415 uint8_t curve_type;
416 uint16_t curve_id;
417 const uint16_t *curves;
418 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400419
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 /* Only support named curves. */
421 if (!CBS_get_u8(cbs, &curve_type) ||
422 curve_type != NAMED_CURVE_TYPE ||
423 !CBS_get_u16(cbs, &curve_id)) {
424 return 0;
425 }
David Benjamined439582014-07-14 19:13:02 -0400426
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 tls1_get_curvelist(s, 0, &curves, &curves_len);
428 for (i = 0; i < curves_len; i++) {
429 if (curve_id == curves[i]) {
430 *out_curve_id = curve_id;
431 return 1;
432 }
433 }
Adam Langley95c29f32014-06-20 12:00:00 -0700434
Adam Langleyfcf25832014-12-18 17:42:32 -0800435 return 0;
436}
David Benjamin072334d2014-07-13 16:24:27 -0400437
Adam Langleyfcf25832014-12-18 17:42:32 -0800438int tls1_get_shared_curve(SSL *s) {
David Benjamin55a43642015-04-20 14:45:55 -0400439 const uint16_t *curves, *peer_curves, *pref, *supp;
440 size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400441
Adam Langleyfcf25832014-12-18 17:42:32 -0800442 /* Can't do anything on client side */
443 if (s->server == 0) {
444 return NID_undef;
445 }
446
David Benjamin55a43642015-04-20 14:45:55 -0400447 tls1_get_curvelist(s, 0 /* local curves */, &curves, &curves_len);
448 tls1_get_curvelist(s, 1 /* peer curves */, &peer_curves, &peer_curves_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800449
David Benjamin55a43642015-04-20 14:45:55 -0400450 if (peer_curves_len == 0) {
451 /* Clients are not required to send a supported_curves extension. In this
452 * case, the server is free to pick any curve it likes. See RFC 4492,
453 * section 4, paragraph 3. */
454 return (curves_len == 0) ? NID_undef : tls1_ec_curve_id2nid(curves[0]);
455 }
456
457 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
458 pref = curves;
459 pref_len = curves_len;
460 supp = peer_curves;
461 supp_len = peer_curves_len;
462 } else {
463 pref = peer_curves;
464 pref_len = peer_curves_len;
465 supp = curves;
466 supp_len = curves_len;
467 }
468
469 for (i = 0; i < pref_len; i++) {
470 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 if (pref[i] == supp[j]) {
472 return tls1_ec_curve_id2nid(pref[i]);
473 }
474 }
475 }
476
477 return NID_undef;
478}
Adam Langley95c29f32014-06-20 12:00:00 -0700479
David Benjamin072334d2014-07-13 16:24:27 -0400480int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 const int *curves, size_t ncurves) {
482 uint16_t *curve_ids;
483 size_t i;
484
Adam Langleyfcf25832014-12-18 17:42:32 -0800485 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
486 if (curve_ids == NULL) {
487 return 0;
488 }
489
490 for (i = 0; i < ncurves; i++) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500491 if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800492 OPENSSL_free(curve_ids);
493 return 0;
494 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 }
496
David Benjamin2755a3e2015-04-22 16:17:58 -0400497 OPENSSL_free(*out_curve_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800498 *out_curve_ids = curve_ids;
499 *out_curve_ids_len = ncurves;
500
501 return 1;
502}
Adam Langley95c29f32014-06-20 12:00:00 -0700503
David Benjamin072334d2014-07-13 16:24:27 -0400504/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
505 * TLS curve ID and point format, respectively, for |ec|. It returns one on
506 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800507static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
508 uint8_t *out_comp_id, EC_KEY *ec) {
509 int nid;
510 uint16_t id;
511 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700512
Adam Langleyfcf25832014-12-18 17:42:32 -0800513 if (ec == NULL) {
514 return 0;
515 }
Adam Langley95c29f32014-06-20 12:00:00 -0700516
Adam Langleyfcf25832014-12-18 17:42:32 -0800517 grp = EC_KEY_get0_group(ec);
518 if (grp == NULL) {
519 return 0;
520 }
David Benjamin072334d2014-07-13 16:24:27 -0400521
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 /* Determine curve ID */
523 nid = EC_GROUP_get_curve_name(grp);
David Benjamin70bd80a2014-12-27 03:06:46 -0500524 if (!tls1_ec_nid2curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 return 0;
526 }
David Benjamin072334d2014-07-13 16:24:27 -0400527
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
529 *out_curve_id = id;
530
531 if (out_comp_id) {
532 if (EC_KEY_get0_public_key(ec) == NULL) {
533 return 0;
534 }
535 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
536 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
537 } else {
538 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
539 }
540 }
541
542 return 1;
543}
David Benjamin072334d2014-07-13 16:24:27 -0400544
Adam Langleyfcf25832014-12-18 17:42:32 -0800545/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
546 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400547 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800548static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
549 const uint16_t *curves;
David Benjamin55a43642015-04-20 14:45:55 -0400550 size_t curves_len, i, get_peer_curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400551
Adam Langleyfcf25832014-12-18 17:42:32 -0800552 /* Check against our list, then the peer's list. */
David Benjamin55a43642015-04-20 14:45:55 -0400553 for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
554 if (get_peer_curves && !s->server) {
555 /* Servers do not present a preference list so, if we are a client, only
556 * check our list. */
557 continue;
558 }
559
560 tls1_get_curvelist(s, get_peer_curves, &curves, &curves_len);
561 if (get_peer_curves && curves_len == 0) {
562 /* Clients are not required to send a supported_curves extension. In this
563 * case, the server is free to pick any curve it likes. See RFC 4492,
564 * section 4, paragraph 3. */
565 continue;
566 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800567 for (i = 0; i < curves_len; i++) {
568 if (curves[i] == curve_id) {
569 break;
570 }
571 }
Adam Langley95c29f32014-06-20 12:00:00 -0700572
Adam Langleyfcf25832014-12-18 17:42:32 -0800573 if (i == curves_len) {
574 return 0;
575 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 }
David Benjamin033e5f42014-11-13 18:47:41 -0500577
Adam Langleyfcf25832014-12-18 17:42:32 -0800578 return 1;
579}
David Benjamin033e5f42014-11-13 18:47:41 -0500580
Adam Langleyfcf25832014-12-18 17:42:32 -0800581int tls1_check_ec_cert(SSL *s, X509 *x) {
582 int ret = 0;
583 EVP_PKEY *pkey = X509_get_pubkey(x);
584 uint16_t curve_id;
585 uint8_t comp_id;
586
David Benjamin758d1272015-11-20 17:47:25 -0500587 if (!pkey) {
588 goto done;
589 }
590 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
591 if (ec_key == NULL ||
592 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, ec_key) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 !tls1_check_curve_id(s, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400594 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800595 goto done;
596 }
597
598 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500599
600done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400601 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800602 return ret;
603}
David Benjamin42e9a772014-09-02 23:18:44 -0400604
Adam Langley95c29f32014-06-20 12:00:00 -0700605/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800606 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700607
Adam Langley95c29f32014-06-20 12:00:00 -0700608#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700609
Adam Langley95c29f32014-06-20 12:00:00 -0700610#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700611
Adam Langleyfcf25832014-12-18 17:42:32 -0800612#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700613
David Benjamincff64722014-08-19 19:54:46 -0400614static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800615 tlsext_sigalg(TLSEXT_hash_sha512)
616 tlsext_sigalg(TLSEXT_hash_sha384)
617 tlsext_sigalg(TLSEXT_hash_sha256)
618 tlsext_sigalg(TLSEXT_hash_sha224)
619 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700620};
David Benjamin05da6e12014-07-12 20:42:55 -0400621
Adam Langleyfcf25832014-12-18 17:42:32 -0800622size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
David Benjamin3dd90162015-08-09 11:26:40 -0400623 *psigs = tls12_sigalgs;
624 return sizeof(tls12_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800625}
Adam Langley95c29f32014-06-20 12:00:00 -0700626
David Benjamin6e807652015-11-02 12:02:20 -0500627int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert,
628 uint8_t hash, uint8_t signature, EVP_PKEY *pkey) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800629 const uint8_t *sent_sigs;
630 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400631 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800632
633 /* Should never happen */
634 if (sigalg == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400635 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 *out_alert = SSL_AD_INTERNAL_ERROR;
637 return 0;
638 }
639
Adam Langleyfcf25832014-12-18 17:42:32 -0800640 /* Check key type is consistent with signature */
641 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400642 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800643 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
644 return 0;
645 }
646
Adam Langleyfcf25832014-12-18 17:42:32 -0800647 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500648 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800649 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
650 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
651 break;
652 }
653 }
654
David Benjamin788be4a2015-10-30 17:50:57 -0400655 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400656 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800657 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
658 return 0;
659 }
660
661 *out_md = tls12_get_hash(hash);
662 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400663 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800664 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
665 return 0;
666 }
667
668 return 1;
669}
670
671/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
672 * supported or doesn't appear in supported signature algorithms. Unlike
673 * ssl_cipher_get_disabled this applies to a specific session and not global
674 * settings. */
675void ssl_set_client_disabled(SSL *s) {
676 CERT *c = s->cert;
677 const uint8_t *sigalgs;
678 size_t i, sigalgslen;
679 int have_rsa = 0, have_ecdsa = 0;
680 c->mask_a = 0;
681 c->mask_k = 0;
682
Adam Langleyfcf25832014-12-18 17:42:32 -0800683 /* Now go through all signature algorithms seeing if we support any for RSA,
684 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
685 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
686 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
687 switch (sigalgs[1]) {
688 case TLSEXT_signature_rsa:
689 have_rsa = 1;
690 break;
691
692 case TLSEXT_signature_ecdsa:
693 have_ecdsa = 1;
694 break;
695 }
696 }
697
698 /* Disable auth if we don't include any appropriate signature algorithms. */
699 if (!have_rsa) {
700 c->mask_a |= SSL_aRSA;
701 }
702 if (!have_ecdsa) {
703 c->mask_a |= SSL_aECDSA;
704 }
705
706 /* with PSK there must be client callback set */
707 if (!s->psk_client_callback) {
708 c->mask_a |= SSL_aPSK;
709 c->mask_k |= SSL_kPSK;
710 }
711}
Adam Langley95c29f32014-06-20 12:00:00 -0700712
Adam Langley614c66a2015-06-12 15:26:58 -0700713/* tls_extension represents a TLS extension that is handled internally. The
714 * |init| function is called for each handshake, before any other functions of
715 * the extension. Then the add and parse callbacks are called as needed.
716 *
717 * The parse callbacks receive a |CBS| that contains the contents of the
718 * extension (i.e. not including the type and length bytes). If an extension is
719 * not received then the parse callbacks will be called with a NULL CBS so that
720 * they can do any processing needed to handle the absence of an extension.
721 *
722 * The add callbacks receive a |CBB| to which the extension can be appended but
723 * the function is responsible for appending the type and length bytes too.
724 *
725 * All callbacks return one for success and zero for error. If a parse function
726 * returns zero then a fatal alert with value |*out_alert| will be sent. If
727 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
728struct tls_extension {
729 uint16_t value;
730 void (*init)(SSL *ssl);
731
732 int (*add_clienthello)(SSL *ssl, CBB *out);
733 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
734
735 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
736 int (*add_serverhello)(SSL *ssl, CBB *out);
737};
738
739
740/* Server name indication (SNI).
741 *
742 * https://tools.ietf.org/html/rfc6066#section-3. */
743
744static void ext_sni_init(SSL *ssl) {
745 ssl->s3->tmp.should_ack_sni = 0;
746}
747
748static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
749 if (ssl->tlsext_hostname == NULL) {
750 return 1;
751 }
752
753 CBB contents, server_name_list, name;
754 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
755 !CBB_add_u16_length_prefixed(out, &contents) ||
756 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
757 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
758 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
759 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
760 strlen(ssl->tlsext_hostname)) ||
761 !CBB_flush(out)) {
762 return 0;
763 }
764
765 return 1;
766}
767
768static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
769 if (contents == NULL) {
770 return 1;
771 }
772
773 if (CBS_len(contents) != 0) {
774 return 0;
775 }
776
777 assert(ssl->tlsext_hostname != NULL);
778
779 if (!ssl->hit) {
780 assert(ssl->session->tlsext_hostname == NULL);
781 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
782 if (!ssl->session->tlsext_hostname) {
783 *out_alert = SSL_AD_INTERNAL_ERROR;
784 return 0;
785 }
786 }
787
788 return 1;
789}
790
791static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
792 if (contents == NULL) {
793 return 1;
794 }
795
796 /* The servername extension is treated as follows:
797 *
798 * - Only the hostname type is supported with a maximum length of 255.
799 * - The servername is rejected if too long or if it contains zeros, in
800 * which case an fatal alert is generated.
801 * - The servername field is maintained together with the session cache.
802 * - When a session is resumed, the servername callback is invoked in order
803 * to allow the application to position itself to the right context.
804 * - The servername is acknowledged if it is new for a session or when
805 * it is identical to a previously used for the same session.
806 * Applications can control the behaviour. They can at any time
807 * set a 'desirable' servername for a new SSL object. This can be the
808 * case for example with HTTPS when a Host: header field is received and
809 * a renegotiation is requested. In this case, a possible servername
810 * presented in the new client hello is only acknowledged if it matches
811 * the value of the Host: field.
812 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
813 * if they provide for changing an explicit servername context for the
814 * session,
815 * i.e. when the session has been established with a servername extension.
816 */
817
818 CBS server_name_list;
819 char have_seen_host_name = 0;
820
821 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
822 CBS_len(&server_name_list) == 0 ||
823 CBS_len(contents) != 0) {
824 return 0;
825 }
826
827 /* Decode each ServerName in the extension. */
828 while (CBS_len(&server_name_list) > 0) {
829 uint8_t name_type;
830 CBS host_name;
831
832 if (!CBS_get_u8(&server_name_list, &name_type) ||
833 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
834 return 0;
835 }
836
837 /* Only host_name is supported. */
838 if (name_type != TLSEXT_NAMETYPE_host_name) {
839 continue;
840 }
841
842 if (have_seen_host_name) {
843 /* The ServerNameList MUST NOT contain more than one name of the same
844 * name_type. */
845 return 0;
846 }
847
848 have_seen_host_name = 1;
849
850 if (CBS_len(&host_name) == 0 ||
851 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
852 CBS_contains_zero_byte(&host_name)) {
853 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
854 return 0;
855 }
856
857 if (!ssl->hit) {
858 assert(ssl->session->tlsext_hostname == NULL);
859 if (ssl->session->tlsext_hostname) {
860 /* This should be impossible. */
861 return 0;
862 }
863
864 /* Copy the hostname as a string. */
865 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
866 *out_alert = SSL_AD_INTERNAL_ERROR;
867 return 0;
868 }
869
870 ssl->s3->tmp.should_ack_sni = 1;
871 }
872 }
873
874 return 1;
875}
876
877static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
878 if (ssl->hit ||
879 !ssl->s3->tmp.should_ack_sni ||
880 ssl->session->tlsext_hostname == NULL) {
881 return 1;
882 }
883
884 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
885 !CBB_add_u16(out, 0 /* length */)) {
886 return 0;
887 }
888
889 return 1;
890}
891
892
Adam Langley5021b222015-06-12 18:27:58 -0700893/* Renegotiation indication.
894 *
895 * https://tools.ietf.org/html/rfc5746 */
896
897static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
898 CBB contents, prev_finished;
899 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
900 !CBB_add_u16_length_prefixed(out, &contents) ||
901 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
902 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
903 ssl->s3->previous_client_finished_len) ||
904 !CBB_flush(out)) {
905 return 0;
906 }
907
908 return 1;
909}
910
911static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
912 CBS *contents) {
David Benjamin3e052de2015-11-25 20:10:31 -0500913 /* Servers may not switch between omitting the extension and supporting it.
914 * See RFC 5746, sections 3.5 and 4.2. */
915 if (ssl->s3->initial_handshake_complete &&
916 (contents != NULL) != ssl->s3->send_connection_binding) {
917 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
918 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
919 return 0;
920 }
921
Adam Langley5021b222015-06-12 18:27:58 -0700922 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500923 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700924 * RI even on initial ServerHello because the client doesn't see any
925 * renegotiation during an attack. However this would mean we could not
926 * connect to any server which doesn't support RI.
927 *
David Benjamine9cddb82015-11-23 14:36:40 -0500928 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
929 * practical terms every client sets it so it's just assumed here. */
930 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700931 }
932
933 const size_t expected_len = ssl->s3->previous_client_finished_len +
934 ssl->s3->previous_server_finished_len;
935
936 /* Check for logic errors */
937 assert(!expected_len || ssl->s3->previous_client_finished_len);
938 assert(!expected_len || ssl->s3->previous_server_finished_len);
939
940 /* Parse out the extension contents. */
941 CBS renegotiated_connection;
942 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
943 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400944 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700945 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
946 return 0;
947 }
948
949 /* Check that the extension matches. */
950 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400951 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700952 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
953 return 0;
954 }
955
956 const uint8_t *d = CBS_data(&renegotiated_connection);
957 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
958 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400959 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700960 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
961 return 0;
962 }
963 d += ssl->s3->previous_client_finished_len;
964
965 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
966 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400967 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700968 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
969 return 0;
970 }
971 ssl->s3->send_connection_binding = 1;
972
973 return 1;
974}
975
976static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
977 CBS *contents) {
978 /* Renegotiation isn't supported as a server so this function should never be
979 * called after the initial handshake. */
980 assert(!ssl->s3->initial_handshake_complete);
981
982 CBS fake_contents;
983 static const uint8_t kFakeExtension[] = {0};
984
985 if (contents == NULL) {
986 if (ssl->s3->send_connection_binding) {
987 /* The renegotiation SCSV was received so pretend that we received a
988 * renegotiation extension. */
989 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
990 contents = &fake_contents;
991 /* We require that the renegotiation extension is at index zero of
992 * kExtensions. */
993 ssl->s3->tmp.extensions.received |= (1u << 0);
994 } else {
995 return 1;
996 }
997 }
998
999 CBS renegotiated_connection;
1000
1001 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
1002 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001003 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -07001004 return 0;
1005 }
1006
1007 /* Check that the extension matches */
1008 if (!CBS_mem_equal(&renegotiated_connection, ssl->s3->previous_client_finished,
1009 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001010 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001011 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1012 return 0;
1013 }
1014
1015 ssl->s3->send_connection_binding = 1;
1016
1017 return 1;
1018}
1019
1020static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
1021 CBB contents, prev_finished;
1022 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
1023 !CBB_add_u16_length_prefixed(out, &contents) ||
1024 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
1025 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
1026 ssl->s3->previous_client_finished_len) ||
1027 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
1028 ssl->s3->previous_server_finished_len) ||
1029 !CBB_flush(out)) {
1030 return 0;
1031 }
1032
1033 return 1;
1034}
1035
Adam Langley0a056712015-07-01 15:03:33 -07001036
1037/* Extended Master Secret.
1038 *
1039 * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
1040
1041static void ext_ems_init(SSL *ssl) {
1042 ssl->s3->tmp.extended_master_secret = 0;
1043}
1044
1045static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
1046 if (ssl->version == SSL3_VERSION) {
1047 return 1;
1048 }
1049
1050 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1051 !CBB_add_u16(out, 0 /* length */)) {
1052 return 0;
1053 }
1054
1055 return 1;
1056}
1057
1058static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1059 CBS *contents) {
1060 if (contents == NULL) {
1061 return 1;
1062 }
1063
1064 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
1065 return 0;
1066 }
1067
1068 ssl->s3->tmp.extended_master_secret = 1;
1069 return 1;
1070}
1071
1072static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1073 if (ssl->version == SSL3_VERSION || contents == NULL) {
1074 return 1;
1075 }
1076
1077 if (CBS_len(contents) != 0) {
1078 return 0;
1079 }
1080
1081 ssl->s3->tmp.extended_master_secret = 1;
1082 return 1;
1083}
1084
1085static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1086 if (!ssl->s3->tmp.extended_master_secret) {
1087 return 1;
1088 }
1089
1090 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1091 !CBB_add_u16(out, 0 /* length */)) {
1092 return 0;
1093 }
1094
1095 return 1;
1096}
1097
Adam Langley9b05bc52015-07-01 15:25:33 -07001098
1099/* Session tickets.
1100 *
1101 * https://tools.ietf.org/html/rfc5077 */
1102
1103static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1104 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1105 return 1;
1106 }
1107
1108 const uint8_t *ticket_data = NULL;
1109 int ticket_len = 0;
1110
1111 /* Renegotiation does not participate in session resumption. However, still
1112 * advertise the extension to avoid potentially breaking servers which carry
1113 * over the state from the previous handshake, such as OpenSSL servers
1114 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1115 if (!ssl->s3->initial_handshake_complete &&
1116 ssl->session != NULL &&
1117 ssl->session->tlsext_tick != NULL) {
1118 ticket_data = ssl->session->tlsext_tick;
1119 ticket_len = ssl->session->tlsext_ticklen;
1120 }
1121
1122 CBB ticket;
1123 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1124 !CBB_add_u16_length_prefixed(out, &ticket) ||
1125 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1126 !CBB_flush(out)) {
1127 return 0;
1128 }
1129
1130 return 1;
1131}
1132
1133static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1134 CBS *contents) {
1135 ssl->tlsext_ticket_expected = 0;
1136
1137 if (contents == NULL) {
1138 return 1;
1139 }
1140
1141 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1142 * this function should never be called, even if the server tries to send the
1143 * extension. */
1144 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1145
1146 if (CBS_len(contents) != 0) {
1147 return 0;
1148 }
1149
1150 ssl->tlsext_ticket_expected = 1;
1151 return 1;
1152}
1153
1154static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1155 /* This function isn't used because the ticket extension from the client is
David Benjaminc7ce9772015-10-09 19:32:41 -04001156 * handled in ssl_session.c. */
Adam Langley9b05bc52015-07-01 15:25:33 -07001157 return 1;
1158}
1159
1160static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1161 if (!ssl->tlsext_ticket_expected) {
1162 return 1;
1163 }
1164
1165 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1166 * true. */
1167 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1168
1169 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1170 !CBB_add_u16(out, 0 /* length */)) {
1171 return 0;
1172 }
1173
1174 return 1;
1175}
1176
1177
Adam Langley2e857bd2015-07-01 16:09:19 -07001178/* Signature Algorithms.
1179 *
1180 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1181
1182static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1183 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1184 return 1;
1185 }
1186
1187 const uint8_t *sigalgs_data;
1188 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1189
1190 CBB contents, sigalgs;
1191 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1192 !CBB_add_u16_length_prefixed(out, &contents) ||
1193 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1194 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1195 !CBB_flush(out)) {
1196 return 0;
1197 }
1198
1199 return 1;
1200}
1201
1202static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1203 CBS *contents) {
1204 if (contents != NULL) {
1205 /* Servers MUST NOT send this extension. */
1206 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001207 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001208 return 0;
1209 }
1210
1211 return 1;
1212}
1213
1214static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1215 CBS *contents) {
1216 OPENSSL_free(ssl->cert->peer_sigalgs);
1217 ssl->cert->peer_sigalgs = NULL;
1218 ssl->cert->peer_sigalgslen = 0;
1219
Adam Langley2e857bd2015-07-01 16:09:19 -07001220 if (contents == NULL) {
1221 return 1;
1222 }
1223
1224 CBS supported_signature_algorithms;
1225 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001226 CBS_len(contents) != 0 ||
1227 CBS_len(&supported_signature_algorithms) == 0 ||
1228 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001229 return 0;
1230 }
1231
1232 return 1;
1233}
1234
1235static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1236 /* Servers MUST NOT send this extension. */
1237 return 1;
1238}
1239
1240
Adam Langleybb0bd042015-07-01 16:21:03 -07001241/* OCSP Stapling.
1242 *
1243 * https://tools.ietf.org/html/rfc6066#section-8 */
1244
1245static void ext_ocsp_init(SSL *ssl) {
1246 ssl->s3->tmp.certificate_status_expected = 0;
1247}
1248
1249static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1250 if (!ssl->ocsp_stapling_enabled) {
1251 return 1;
1252 }
1253
1254 CBB contents;
1255 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1256 !CBB_add_u16_length_prefixed(out, &contents) ||
1257 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1258 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1259 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1260 !CBB_flush(out)) {
1261 return 0;
1262 }
1263
1264 return 1;
1265}
1266
1267static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001268 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001269 if (contents == NULL) {
1270 return 1;
1271 }
1272
1273 if (CBS_len(contents) != 0) {
1274 return 0;
1275 }
1276
1277 ssl->s3->tmp.certificate_status_expected = 1;
1278 return 1;
1279}
1280
1281static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1282 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001283 if (contents == NULL) {
1284 return 1;
1285 }
1286
1287 uint8_t status_type;
1288 if (!CBS_get_u8(contents, &status_type)) {
1289 return 0;
1290 }
1291
1292 /* We cannot decide whether OCSP stapling will occur yet because the correct
1293 * SSL_CTX might not have been selected. */
1294 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1295
Adam Langleybb0bd042015-07-01 16:21:03 -07001296 return 1;
1297}
1298
1299static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001300 /* The extension shouldn't be sent when resuming sessions. */
1301 if (ssl->hit ||
1302 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001303 ssl->ctx->ocsp_response_length == 0) {
1304 return 1;
1305 }
1306
1307 ssl->s3->tmp.certificate_status_expected = 1;
1308
1309 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1310 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001311}
1312
1313
Adam Langley97dfcbf2015-07-01 18:35:20 -07001314/* Next protocol negotiation.
1315 *
1316 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1317
1318static void ext_npn_init(SSL *ssl) {
1319 ssl->s3->next_proto_neg_seen = 0;
1320}
1321
1322static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1323 if (ssl->s3->initial_handshake_complete ||
1324 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001325 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001326 SSL_IS_DTLS(ssl)) {
1327 return 1;
1328 }
1329
1330 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1331 !CBB_add_u16(out, 0 /* length */)) {
1332 return 0;
1333 }
1334
1335 return 1;
1336}
1337
1338static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1339 CBS *contents) {
1340 if (contents == NULL) {
1341 return 1;
1342 }
1343
1344 /* If any of these are false then we should never have sent the NPN
1345 * extension in the ClientHello and thus this function should never have been
1346 * called. */
1347 assert(!ssl->s3->initial_handshake_complete);
1348 assert(!SSL_IS_DTLS(ssl));
1349 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001350 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001351
David Benjamin76c2efc2015-08-31 14:24:29 -04001352 if (ssl->s3->alpn_selected != NULL) {
1353 /* NPN and ALPN may not be negotiated in the same connection. */
1354 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1355 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1356 return 0;
1357 }
1358
Adam Langley97dfcbf2015-07-01 18:35:20 -07001359 const uint8_t *const orig_contents = CBS_data(contents);
1360 const size_t orig_len = CBS_len(contents);
1361
1362 while (CBS_len(contents) != 0) {
1363 CBS proto;
1364 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1365 CBS_len(&proto) == 0) {
1366 return 0;
1367 }
1368 }
1369
1370 uint8_t *selected;
1371 uint8_t selected_len;
1372 if (ssl->ctx->next_proto_select_cb(
1373 ssl, &selected, &selected_len, orig_contents, orig_len,
1374 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1375 *out_alert = SSL_AD_INTERNAL_ERROR;
1376 return 0;
1377 }
1378
1379 OPENSSL_free(ssl->next_proto_negotiated);
1380 ssl->next_proto_negotiated = BUF_memdup(selected, selected_len);
1381 if (ssl->next_proto_negotiated == NULL) {
1382 *out_alert = SSL_AD_INTERNAL_ERROR;
1383 return 0;
1384 }
1385
1386 ssl->next_proto_negotiated_len = selected_len;
1387 ssl->s3->next_proto_neg_seen = 1;
1388
1389 return 1;
1390}
1391
1392static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1393 CBS *contents) {
1394 if (contents != NULL && CBS_len(contents) != 0) {
1395 return 0;
1396 }
1397
1398 if (contents == NULL ||
1399 ssl->s3->initial_handshake_complete ||
1400 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1401 * afterwards, parsing the ALPN extension will clear
1402 * |next_proto_neg_seen|. */
1403 ssl->s3->alpn_selected != NULL ||
1404 ssl->ctx->next_protos_advertised_cb == NULL ||
1405 SSL_IS_DTLS(ssl)) {
1406 return 1;
1407 }
1408
1409 ssl->s3->next_proto_neg_seen = 1;
1410 return 1;
1411}
1412
1413static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1414 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1415 * parsed. */
1416 if (!ssl->s3->next_proto_neg_seen) {
1417 return 1;
1418 }
1419
1420 const uint8_t *npa;
1421 unsigned npa_len;
1422
1423 if (ssl->ctx->next_protos_advertised_cb(
1424 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1425 SSL_TLSEXT_ERR_OK) {
1426 ssl->s3->next_proto_neg_seen = 0;
1427 return 1;
1428 }
1429
1430 CBB contents;
1431 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1432 !CBB_add_u16_length_prefixed(out, &contents) ||
1433 !CBB_add_bytes(&contents, npa, npa_len) ||
1434 !CBB_flush(out)) {
1435 return 0;
1436 }
1437
1438 return 1;
1439}
1440
1441
Adam Langleyab8d87d2015-07-10 12:21:39 -07001442/* Signed certificate timestamps.
1443 *
1444 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1445
1446static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1447 if (!ssl->signed_cert_timestamps_enabled) {
1448 return 1;
1449 }
1450
1451 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1452 !CBB_add_u16(out, 0 /* length */)) {
1453 return 0;
1454 }
1455
1456 return 1;
1457}
1458
1459static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1460 CBS *contents) {
1461 if (contents == NULL) {
1462 return 1;
1463 }
1464
1465 /* If this is false then we should never have sent the SCT extension in the
1466 * ClientHello and thus this function should never have been called. */
1467 assert(ssl->signed_cert_timestamps_enabled);
1468
1469 if (CBS_len(contents) == 0) {
1470 *out_alert = SSL_AD_DECODE_ERROR;
1471 return 0;
1472 }
1473
1474 /* Session resumption uses the original session information. */
1475 if (!ssl->hit &&
1476 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1477 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1478 *out_alert = SSL_AD_INTERNAL_ERROR;
1479 return 0;
1480 }
1481
1482 return 1;
1483}
1484
1485static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1486 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001487 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001488}
1489
1490static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001491 /* The extension shouldn't be sent when resuming sessions. */
1492 if (ssl->hit ||
1493 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001494 return 1;
1495 }
1496
1497 CBB contents;
1498 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1499 CBB_add_u16_length_prefixed(out, &contents) &&
1500 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1501 ssl->ctx->signed_cert_timestamp_list_length) &&
1502 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001503}
1504
1505
Adam Langleyf18e4532015-07-10 13:39:53 -07001506/* Application-level Protocol Negotiation.
1507 *
1508 * https://tools.ietf.org/html/rfc7301 */
1509
1510static void ext_alpn_init(SSL *ssl) {
1511 OPENSSL_free(ssl->s3->alpn_selected);
1512 ssl->s3->alpn_selected = NULL;
1513}
1514
1515static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1516 if (ssl->alpn_client_proto_list == NULL ||
1517 ssl->s3->initial_handshake_complete) {
1518 return 1;
1519 }
1520
1521 CBB contents, proto_list;
1522 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1523 !CBB_add_u16_length_prefixed(out, &contents) ||
1524 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1525 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1526 ssl->alpn_client_proto_list_len) ||
1527 !CBB_flush(out)) {
1528 return 0;
1529 }
1530
1531 return 1;
1532}
1533
1534static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1535 CBS *contents) {
1536 if (contents == NULL) {
1537 return 1;
1538 }
1539
1540 assert(!ssl->s3->initial_handshake_complete);
1541 assert(ssl->alpn_client_proto_list != NULL);
1542
David Benjamin76c2efc2015-08-31 14:24:29 -04001543 if (ssl->s3->next_proto_neg_seen) {
1544 /* NPN and ALPN may not be negotiated in the same connection. */
1545 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1546 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1547 return 0;
1548 }
1549
Adam Langleyf18e4532015-07-10 13:39:53 -07001550 /* The extension data consists of a ProtocolNameList which must have
1551 * exactly one ProtocolName. Each of these is length-prefixed. */
1552 CBS protocol_name_list, protocol_name;
1553 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1554 CBS_len(contents) != 0 ||
1555 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1556 /* Empty protocol names are forbidden. */
1557 CBS_len(&protocol_name) == 0 ||
1558 CBS_len(&protocol_name_list) != 0) {
1559 return 0;
1560 }
1561
1562 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1563 &ssl->s3->alpn_selected_len)) {
1564 *out_alert = SSL_AD_INTERNAL_ERROR;
1565 return 0;
1566 }
1567
1568 return 1;
1569}
1570
1571static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1572 CBS *contents) {
1573 if (contents == NULL) {
1574 return 1;
1575 }
1576
1577 if (ssl->ctx->alpn_select_cb == NULL ||
1578 ssl->s3->initial_handshake_complete) {
1579 return 1;
1580 }
1581
1582 /* ALPN takes precedence over NPN. */
1583 ssl->s3->next_proto_neg_seen = 0;
1584
1585 CBS protocol_name_list;
1586 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1587 CBS_len(contents) != 0 ||
1588 CBS_len(&protocol_name_list) < 2) {
1589 return 0;
1590 }
1591
1592 /* Validate the protocol list. */
1593 CBS protocol_name_list_copy = protocol_name_list;
1594 while (CBS_len(&protocol_name_list_copy) > 0) {
1595 CBS protocol_name;
1596
1597 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1598 /* Empty protocol names are forbidden. */
1599 CBS_len(&protocol_name) == 0) {
1600 return 0;
1601 }
1602 }
1603
1604 const uint8_t *selected;
1605 uint8_t selected_len;
1606 if (ssl->ctx->alpn_select_cb(
1607 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1608 CBS_len(&protocol_name_list),
1609 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1610 OPENSSL_free(ssl->s3->alpn_selected);
1611 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1612 if (ssl->s3->alpn_selected == NULL) {
1613 *out_alert = SSL_AD_INTERNAL_ERROR;
1614 return 0;
1615 }
1616 ssl->s3->alpn_selected_len = selected_len;
1617 }
1618
1619 return 1;
1620}
1621
1622static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1623 if (ssl->s3->alpn_selected == NULL) {
1624 return 1;
1625 }
1626
1627 CBB contents, proto_list, proto;
1628 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1629 !CBB_add_u16_length_prefixed(out, &contents) ||
1630 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1631 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
1632 !CBB_add_bytes(&proto, ssl->s3->alpn_selected, ssl->s3->alpn_selected_len) ||
1633 !CBB_flush(out)) {
1634 return 0;
1635 }
1636
1637 return 1;
1638}
1639
1640
Adam Langley49c7af12015-07-10 14:33:46 -07001641/* Channel ID.
1642 *
1643 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1644
1645static void ext_channel_id_init(SSL *ssl) {
1646 ssl->s3->tlsext_channel_id_valid = 0;
1647}
1648
1649static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1650 if (!ssl->tlsext_channel_id_enabled ||
1651 SSL_IS_DTLS(ssl)) {
1652 return 1;
1653 }
1654
1655 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1656 !CBB_add_u16(out, 0 /* length */)) {
1657 return 0;
1658 }
1659
1660 return 1;
1661}
1662
1663static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1664 CBS *contents) {
1665 if (contents == NULL) {
1666 return 1;
1667 }
1668
1669 assert(!SSL_IS_DTLS(ssl));
1670 assert(ssl->tlsext_channel_id_enabled);
1671
1672 if (CBS_len(contents) != 0) {
1673 return 0;
1674 }
1675
1676 ssl->s3->tlsext_channel_id_valid = 1;
1677 return 1;
1678}
1679
1680static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1681 CBS *contents) {
1682 if (contents == NULL ||
1683 !ssl->tlsext_channel_id_enabled ||
1684 SSL_IS_DTLS(ssl)) {
1685 return 1;
1686 }
1687
1688 if (CBS_len(contents) != 0) {
1689 return 0;
1690 }
1691
1692 ssl->s3->tlsext_channel_id_valid = 1;
1693 return 1;
1694}
1695
1696static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1697 if (!ssl->s3->tlsext_channel_id_valid) {
1698 return 1;
1699 }
1700
1701 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1702 !CBB_add_u16(out, 0 /* length */)) {
1703 return 0;
1704 }
1705
1706 return 1;
1707}
1708
Adam Langley391250d2015-07-15 19:06:07 -07001709
1710/* Secure Real-time Transport Protocol (SRTP) extension.
1711 *
1712 * https://tools.ietf.org/html/rfc5764 */
1713
Adam Langley391250d2015-07-15 19:06:07 -07001714
1715static void ext_srtp_init(SSL *ssl) {
1716 ssl->srtp_profile = NULL;
1717}
1718
1719static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1720 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1721 if (profiles == NULL) {
1722 return 1;
1723 }
1724 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1725 if (num_profiles == 0) {
1726 return 1;
1727 }
1728
1729 CBB contents, profile_ids;
1730 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1731 !CBB_add_u16_length_prefixed(out, &contents) ||
1732 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1733 return 0;
1734 }
1735
1736 size_t i;
1737 for (i = 0; i < num_profiles; i++) {
1738 if (!CBB_add_u16(&profile_ids,
1739 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1740 return 0;
1741 }
1742 }
1743
1744 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1745 !CBB_flush(out)) {
1746 return 0;
1747 }
1748
1749 return 1;
1750}
1751
1752static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1753 CBS *contents) {
1754 if (contents == NULL) {
1755 return 1;
1756 }
1757
1758 /* The extension consists of a u16-prefixed profile ID list containing a
1759 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1760 *
1761 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1762 CBS profile_ids, srtp_mki;
1763 uint16_t profile_id;
1764 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1765 !CBS_get_u16(&profile_ids, &profile_id) ||
1766 CBS_len(&profile_ids) != 0 ||
1767 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1768 CBS_len(contents) != 0) {
1769 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1770 return 0;
1771 }
1772
1773 if (CBS_len(&srtp_mki) != 0) {
1774 /* Must be no MKI, since we never offer one. */
1775 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1776 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1777 return 0;
1778 }
1779
1780 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1781
1782 /* Check to see if the server gave us something we support (and presumably
1783 * offered). */
1784 size_t i;
1785 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1786 const SRTP_PROTECTION_PROFILE *profile =
1787 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1788
1789 if (profile->id == profile_id) {
1790 ssl->srtp_profile = profile;
1791 return 1;
1792 }
1793 }
1794
1795 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1796 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1797 return 0;
1798}
1799
1800static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1801 CBS *contents) {
1802 if (contents == NULL) {
1803 return 1;
1804 }
1805
1806 CBS profile_ids, srtp_mki;
1807 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1808 CBS_len(&profile_ids) < 2 ||
1809 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1810 CBS_len(contents) != 0) {
1811 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1812 return 0;
1813 }
1814 /* Discard the MKI value for now. */
1815
1816 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1817 SSL_get_srtp_profiles(ssl);
1818
1819 /* Pick the server's most preferred profile. */
1820 size_t i;
1821 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1822 const SRTP_PROTECTION_PROFILE *server_profile =
1823 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1824
1825 CBS profile_ids_tmp;
1826 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1827
1828 while (CBS_len(&profile_ids_tmp) > 0) {
1829 uint16_t profile_id;
1830 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1831 return 0;
1832 }
1833
1834 if (server_profile->id == profile_id) {
1835 ssl->srtp_profile = server_profile;
1836 return 1;
1837 }
1838 }
1839 }
1840
1841 return 1;
1842}
1843
1844static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1845 if (ssl->srtp_profile == NULL) {
1846 return 1;
1847 }
1848
1849 CBB contents, profile_ids;
1850 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1851 !CBB_add_u16_length_prefixed(out, &contents) ||
1852 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1853 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1854 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1855 !CBB_flush(out)) {
1856 return 0;
1857 }
1858
1859 return 1;
1860}
1861
Adam Langleybdd5d662015-07-20 16:19:08 -07001862
1863/* EC point formats.
1864 *
1865 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1866
1867static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1868 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1869 return 0;
1870 }
1871
1872 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1873
1874 size_t i;
1875 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1876 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1877
1878 const uint32_t alg_k = cipher->algorithm_mkey;
1879 const uint32_t alg_a = cipher->algorithm_auth;
1880 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1881 return 1;
1882 }
1883 }
1884
1885 return 0;
1886}
1887
Adam Langleybdd5d662015-07-20 16:19:08 -07001888static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001889 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001890 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1891 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001892 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1893 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001894 !CBB_flush(out)) {
1895 return 0;
1896 }
1897
1898 return 1;
1899}
1900
1901static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1902 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1903 return 1;
1904 }
1905
1906 return ext_ec_point_add_extension(ssl, out);
1907}
1908
1909static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1910 CBS *contents) {
1911 if (contents == NULL) {
1912 return 1;
1913 }
1914
1915 CBS ec_point_format_list;
1916 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1917 CBS_len(contents) != 0) {
1918 return 0;
1919 }
1920
David Benjaminfc059942015-07-30 23:01:59 -04001921 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1922 * point format. */
1923 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1924 CBS_len(&ec_point_format_list)) == NULL) {
1925 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001926 return 0;
1927 }
1928
1929 return 1;
1930}
1931
1932static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1933 CBS *contents) {
1934 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1935}
1936
1937static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1938 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1939 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001940 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001941
1942 if (!using_ecc) {
1943 return 1;
1944 }
1945
1946 return ext_ec_point_add_extension(ssl, out);
1947}
1948
Adam Langley273d49c2015-07-20 16:38:52 -07001949
1950/* EC supported curves.
1951 *
1952 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1953
1954static void ext_ec_curves_init(SSL *ssl) {
1955 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1956 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1957 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
1958}
1959
1960static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
1961 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1962 return 1;
1963 }
1964
1965 CBB contents, curves_bytes;
1966 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
1967 !CBB_add_u16_length_prefixed(out, &contents) ||
1968 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
1969 return 0;
1970 }
1971
1972 const uint16_t *curves;
1973 size_t curves_len;
1974 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
1975
1976 size_t i;
1977 for (i = 0; i < curves_len; i++) {
1978 if (!CBB_add_u16(&curves_bytes, curves[i])) {
1979 return 0;
1980 }
1981 }
1982
1983 return CBB_flush(out);
1984}
1985
1986static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1987 CBS *contents) {
1988 /* This extension is not expected to be echoed by servers and is ignored. */
1989 return 1;
1990}
1991
1992static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1993 CBS *contents) {
1994 if (contents == NULL) {
1995 return 1;
1996 }
1997
1998 CBS elliptic_curve_list;
1999 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
2000 CBS_len(&elliptic_curve_list) == 0 ||
2001 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
2002 CBS_len(contents) != 0) {
2003 return 0;
2004 }
2005
2006 ssl->s3->tmp.peer_ellipticcurvelist =
2007 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
2008
2009 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
2010 *out_alert = SSL_AD_INTERNAL_ERROR;
2011 return 0;
2012 }
2013
2014 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
2015 size_t i;
2016 for (i = 0; i < num_curves; i++) {
2017 if (!CBS_get_u16(&elliptic_curve_list,
2018 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
2019 goto err;
2020 }
2021 }
2022
2023 assert(CBS_len(&elliptic_curve_list) == 0);
2024 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
2025
2026 return 1;
2027
2028err:
2029 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2030 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2031 *out_alert = SSL_AD_INTERNAL_ERROR;
2032 return 0;
2033}
2034
2035static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
2036 /* Servers don't echo this extension. */
2037 return 1;
2038}
2039
2040
Adam Langley614c66a2015-06-12 15:26:58 -07002041/* kExtensions contains all the supported extensions. */
2042static const struct tls_extension kExtensions[] = {
2043 {
Adam Langley5021b222015-06-12 18:27:58 -07002044 /* The renegotiation extension must always be at index zero because the
2045 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2046 * sent as an SCSV. */
2047 TLSEXT_TYPE_renegotiate,
2048 NULL,
2049 ext_ri_add_clienthello,
2050 ext_ri_parse_serverhello,
2051 ext_ri_parse_clienthello,
2052 ext_ri_add_serverhello,
2053 },
2054 {
Adam Langley614c66a2015-06-12 15:26:58 -07002055 TLSEXT_TYPE_server_name,
2056 ext_sni_init,
2057 ext_sni_add_clienthello,
2058 ext_sni_parse_serverhello,
2059 ext_sni_parse_clienthello,
2060 ext_sni_add_serverhello,
2061 },
Adam Langley0a056712015-07-01 15:03:33 -07002062 {
2063 TLSEXT_TYPE_extended_master_secret,
2064 ext_ems_init,
2065 ext_ems_add_clienthello,
2066 ext_ems_parse_serverhello,
2067 ext_ems_parse_clienthello,
2068 ext_ems_add_serverhello,
2069 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002070 {
2071 TLSEXT_TYPE_session_ticket,
2072 NULL,
2073 ext_ticket_add_clienthello,
2074 ext_ticket_parse_serverhello,
2075 ext_ticket_parse_clienthello,
2076 ext_ticket_add_serverhello,
2077 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002078 {
2079 TLSEXT_TYPE_signature_algorithms,
2080 NULL,
2081 ext_sigalgs_add_clienthello,
2082 ext_sigalgs_parse_serverhello,
2083 ext_sigalgs_parse_clienthello,
2084 ext_sigalgs_add_serverhello,
2085 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002086 {
2087 TLSEXT_TYPE_status_request,
2088 ext_ocsp_init,
2089 ext_ocsp_add_clienthello,
2090 ext_ocsp_parse_serverhello,
2091 ext_ocsp_parse_clienthello,
2092 ext_ocsp_add_serverhello,
2093 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002094 {
2095 TLSEXT_TYPE_next_proto_neg,
2096 ext_npn_init,
2097 ext_npn_add_clienthello,
2098 ext_npn_parse_serverhello,
2099 ext_npn_parse_clienthello,
2100 ext_npn_add_serverhello,
2101 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002102 {
2103 TLSEXT_TYPE_certificate_timestamp,
2104 NULL,
2105 ext_sct_add_clienthello,
2106 ext_sct_parse_serverhello,
2107 ext_sct_parse_clienthello,
2108 ext_sct_add_serverhello,
2109 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002110 {
2111 TLSEXT_TYPE_application_layer_protocol_negotiation,
2112 ext_alpn_init,
2113 ext_alpn_add_clienthello,
2114 ext_alpn_parse_serverhello,
2115 ext_alpn_parse_clienthello,
2116 ext_alpn_add_serverhello,
2117 },
Adam Langley49c7af12015-07-10 14:33:46 -07002118 {
2119 TLSEXT_TYPE_channel_id,
2120 ext_channel_id_init,
2121 ext_channel_id_add_clienthello,
2122 ext_channel_id_parse_serverhello,
2123 ext_channel_id_parse_clienthello,
2124 ext_channel_id_add_serverhello,
2125 },
Adam Langley391250d2015-07-15 19:06:07 -07002126 {
2127 TLSEXT_TYPE_srtp,
2128 ext_srtp_init,
2129 ext_srtp_add_clienthello,
2130 ext_srtp_parse_serverhello,
2131 ext_srtp_parse_clienthello,
2132 ext_srtp_add_serverhello,
2133 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002134 {
2135 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002136 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002137 ext_ec_point_add_clienthello,
2138 ext_ec_point_parse_serverhello,
2139 ext_ec_point_parse_clienthello,
2140 ext_ec_point_add_serverhello,
2141 },
Adam Langley273d49c2015-07-20 16:38:52 -07002142 {
2143 TLSEXT_TYPE_elliptic_curves,
2144 ext_ec_curves_init,
2145 ext_ec_curves_add_clienthello,
2146 ext_ec_curves_parse_serverhello,
2147 ext_ec_curves_parse_clienthello,
2148 ext_ec_curves_add_serverhello,
2149 },
Adam Langley614c66a2015-06-12 15:26:58 -07002150};
2151
2152#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2153
Adam Langley4cfa96b2015-07-01 11:56:55 -07002154OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2155 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002156 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002157OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2158 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2159 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002160 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002161
Adam Langley614c66a2015-06-12 15:26:58 -07002162static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2163 uint16_t value) {
2164 unsigned i;
2165 for (i = 0; i < kNumExtensions; i++) {
2166 if (kExtensions[i].value == value) {
2167 *out_index = i;
2168 return &kExtensions[i];
2169 }
2170 }
2171
2172 return NULL;
2173}
2174
Adam Langley09505632015-07-30 18:10:13 -07002175int SSL_extension_supported(unsigned extension_value) {
2176 uint32_t index;
2177 return extension_value == TLSEXT_TYPE_padding ||
2178 tls_extension_find(&index, extension_value) != NULL;
2179}
2180
David Benjamine8d53502015-10-10 14:13:23 -04002181int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002182 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002183 if (ssl->client_version == SSL3_VERSION &&
2184 !ssl->s3->send_connection_binding) {
2185 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002186 }
Adam Langley95c29f32014-06-20 12:00:00 -07002187
David Benjamine8d53502015-10-10 14:13:23 -04002188 CBB extensions;
2189 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002190 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002191 }
Adam Langley95c29f32014-06-20 12:00:00 -07002192
David Benjamine8d53502015-10-10 14:13:23 -04002193 ssl->s3->tmp.extensions.sent = 0;
2194 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002195
Adam Langley614c66a2015-06-12 15:26:58 -07002196 size_t i;
2197 for (i = 0; i < kNumExtensions; i++) {
2198 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002199 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002200 }
2201 }
Adam Langley95c29f32014-06-20 12:00:00 -07002202
Adam Langley614c66a2015-06-12 15:26:58 -07002203 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002204 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002205 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002206 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2207 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2208 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002209 }
Adam Langley95c29f32014-06-20 12:00:00 -07002210
Adam Langley33ad2b52015-07-20 17:43:53 -07002211 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002212 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002213 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002214 }
Adam Langley75712922014-10-10 16:23:43 -07002215
David Benjamine8d53502015-10-10 14:13:23 -04002216 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002217 goto err;
2218 }
2219
David Benjamine8d53502015-10-10 14:13:23 -04002220 if (!SSL_IS_DTLS(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002221 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002222 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002223 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002224 *
2225 * NB: because this code works out the length of all existing extensions
2226 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002227 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002228 /* Extensions take at least four bytes to encode. Always include least
2229 * one byte of data if including the extension. WebSphere Application
2230 * Server 7.0 is intolerant to the last extension being zero-length. */
2231 if (padding_len >= 4 + 1) {
2232 padding_len -= 4;
2233 } else {
2234 padding_len = 1;
2235 }
Adam Langley95c29f32014-06-20 12:00:00 -07002236
Adam Langley33ad2b52015-07-20 17:43:53 -07002237 uint8_t *padding_bytes;
2238 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2239 !CBB_add_u16(&extensions, padding_len) ||
2240 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2241 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002242 }
Adam Langley75712922014-10-10 16:23:43 -07002243
Adam Langley33ad2b52015-07-20 17:43:53 -07002244 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002245 }
2246 }
Adam Langley75712922014-10-10 16:23:43 -07002247
David Benjamina01deee2015-12-08 18:56:31 -05002248 /* Discard empty extensions blocks. */
2249 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002250 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002251 }
2252
David Benjamine8d53502015-10-10 14:13:23 -04002253 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002254
2255err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002256 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002257 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002258}
Adam Langley95c29f32014-06-20 12:00:00 -07002259
David Benjamin56380462015-10-10 14:59:09 -04002260int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002261 CBB extensions;
2262 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002263 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002264 }
2265
2266 unsigned i;
2267 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002268 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002269 /* Don't send extensions that were not received. */
2270 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002271 }
Adam Langley95c29f32014-06-20 12:00:00 -07002272
David Benjamin56380462015-10-10 14:59:09 -04002273 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002274 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2275 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2276 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002277 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002278 }
Adam Langley95c29f32014-06-20 12:00:00 -07002279
David Benjamin56380462015-10-10 14:59:09 -04002280 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002281 goto err;
2282 }
2283
David Benjamina01deee2015-12-08 18:56:31 -05002284 /* Discard empty extensions blocks. */
2285 if (CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002286 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002287 }
2288
David Benjamin56380462015-10-10 14:59:09 -04002289 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002290
2291err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002292 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002293 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002294}
Adam Langley95c29f32014-06-20 12:00:00 -07002295
Adam Langleyfcf25832014-12-18 17:42:32 -08002296static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002297 size_t i;
2298 for (i = 0; i < kNumExtensions; i++) {
2299 if (kExtensions[i].init != NULL) {
2300 kExtensions[i].init(s);
2301 }
2302 }
2303
2304 s->s3->tmp.extensions.received = 0;
Adam Langley09505632015-07-30 18:10:13 -07002305 s->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002306 /* The renegotiation extension must always be at index zero because the
2307 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2308 * sent as an SCSV. */
2309 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002310
Adam Langleyfcf25832014-12-18 17:42:32 -08002311 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002312 if (CBS_len(cbs) != 0) {
2313 /* Decode the extensions block and check it is valid. */
2314 CBS extensions;
2315 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2316 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002317 *out_alert = SSL_AD_DECODE_ERROR;
2318 return 0;
2319 }
Adam Langley95c29f32014-06-20 12:00:00 -07002320
Adam Langley33ad2b52015-07-20 17:43:53 -07002321 while (CBS_len(&extensions) != 0) {
2322 uint16_t type;
2323 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002324
Adam Langley33ad2b52015-07-20 17:43:53 -07002325 /* Decode the next extension. */
2326 if (!CBS_get_u16(&extensions, &type) ||
2327 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2328 *out_alert = SSL_AD_DECODE_ERROR;
2329 return 0;
2330 }
2331
David Benjaminc7ce9772015-10-09 19:32:41 -04002332 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2333 * ambiguous. Ignore all but the renegotiation_info extension. */
2334 if (s->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2335 continue;
2336 }
2337
Adam Langley33ad2b52015-07-20 17:43:53 -07002338 unsigned ext_index;
2339 const struct tls_extension *const ext =
2340 tls_extension_find(&ext_index, type);
2341
2342 if (ext == NULL) {
Adam Langley09505632015-07-30 18:10:13 -07002343 if (!custom_ext_parse_clienthello(s, out_alert, type, &extension)) {
2344 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2345 return 0;
2346 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002347 continue;
2348 }
2349
Adam Langley614c66a2015-06-12 15:26:58 -07002350 s->s3->tmp.extensions.received |= (1u << ext_index);
2351 uint8_t alert = SSL_AD_DECODE_ERROR;
2352 if (!ext->parse_clienthello(s, &alert, &extension)) {
2353 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002354 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2355 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002356 return 0;
2357 }
Adam Langley614c66a2015-06-12 15:26:58 -07002358 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002359 }
Adam Langley75712922014-10-10 16:23:43 -07002360
Adam Langley614c66a2015-06-12 15:26:58 -07002361 for (i = 0; i < kNumExtensions; i++) {
2362 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2363 /* Extension wasn't observed so call the callback with a NULL
2364 * parameter. */
2365 uint8_t alert = SSL_AD_DECODE_ERROR;
2366 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002367 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2368 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002369 *out_alert = alert;
2370 return 0;
2371 }
2372 }
2373 }
2374
Adam Langleyfcf25832014-12-18 17:42:32 -08002375 return 1;
2376}
Adam Langley95c29f32014-06-20 12:00:00 -07002377
Adam Langleyfcf25832014-12-18 17:42:32 -08002378int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
2379 int alert = -1;
2380 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
2381 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2382 return 0;
2383 }
Adam Langley95c29f32014-06-20 12:00:00 -07002384
Adam Langleyfcf25832014-12-18 17:42:32 -08002385 if (ssl_check_clienthello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002386 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002387 return 0;
2388 }
Adam Langley95c29f32014-06-20 12:00:00 -07002389
Adam Langleyfcf25832014-12-18 17:42:32 -08002390 return 1;
2391}
Adam Langley95c29f32014-06-20 12:00:00 -07002392
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002393OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2394
Adam Langleyfcf25832014-12-18 17:42:32 -08002395static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002396 uint32_t received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002397
Adam Langley33ad2b52015-07-20 17:43:53 -07002398 if (CBS_len(cbs) != 0) {
2399 /* Decode the extensions block and check it is valid. */
2400 CBS extensions;
2401 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2402 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002403 *out_alert = SSL_AD_DECODE_ERROR;
2404 return 0;
2405 }
Adam Langley95c29f32014-06-20 12:00:00 -07002406
Adam Langley614c66a2015-06-12 15:26:58 -07002407
Adam Langley33ad2b52015-07-20 17:43:53 -07002408 while (CBS_len(&extensions) != 0) {
2409 uint16_t type;
2410 CBS extension;
2411
2412 /* Decode the next extension. */
2413 if (!CBS_get_u16(&extensions, &type) ||
2414 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2415 *out_alert = SSL_AD_DECODE_ERROR;
2416 return 0;
2417 }
2418
2419 unsigned ext_index;
2420 const struct tls_extension *const ext =
2421 tls_extension_find(&ext_index, type);
2422
Adam Langley09505632015-07-30 18:10:13 -07002423 if (ext == NULL) {
2424 if (!custom_ext_parse_serverhello(s, out_alert, type, &extension)) {
2425 return 0;
2426 }
2427 continue;
2428 }
2429
2430 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2431 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002432 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002433 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002434 *out_alert = SSL_AD_DECODE_ERROR;
2435 return 0;
2436 }
David Benjamin03973092014-06-24 23:27:17 -04002437
Adam Langley614c66a2015-06-12 15:26:58 -07002438 received |= (1u << ext_index);
2439
2440 uint8_t alert = SSL_AD_DECODE_ERROR;
2441 if (!ext->parse_serverhello(s, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002442 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2443 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002444 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002445 return 0;
2446 }
Adam Langley614c66a2015-06-12 15:26:58 -07002447 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002448 }
Adam Langley95c29f32014-06-20 12:00:00 -07002449
Adam Langley33ad2b52015-07-20 17:43:53 -07002450 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002451 for (i = 0; i < kNumExtensions; i++) {
2452 if (!(received & (1u << i))) {
2453 /* Extension wasn't observed so call the callback with a NULL
2454 * parameter. */
2455 uint8_t alert = SSL_AD_DECODE_ERROR;
2456 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002457 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2458 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002459 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002460 return 0;
2461 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002462 }
2463 }
Adam Langley95c29f32014-06-20 12:00:00 -07002464
Adam Langleyfcf25832014-12-18 17:42:32 -08002465 return 1;
2466}
Adam Langley95c29f32014-06-20 12:00:00 -07002467
Adam Langleyfcf25832014-12-18 17:42:32 -08002468static int ssl_check_clienthello_tlsext(SSL *s) {
2469 int ret = SSL_TLSEXT_ERR_NOACK;
2470 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002471
Adam Langleyfcf25832014-12-18 17:42:32 -08002472 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2473 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002474
Adam Langleyfcf25832014-12-18 17:42:32 -08002475 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2476 ret = s->ctx->tlsext_servername_callback(s, &al,
2477 s->ctx->tlsext_servername_arg);
2478 } else if (s->initial_ctx != NULL &&
2479 s->initial_ctx->tlsext_servername_callback != 0) {
2480 ret = s->initial_ctx->tlsext_servername_callback(
2481 s, &al, s->initial_ctx->tlsext_servername_arg);
2482 }
Adam Langley95c29f32014-06-20 12:00:00 -07002483
Adam Langleyfcf25832014-12-18 17:42:32 -08002484 switch (ret) {
2485 case SSL_TLSEXT_ERR_ALERT_FATAL:
2486 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2487 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002488
Adam Langleyfcf25832014-12-18 17:42:32 -08002489 case SSL_TLSEXT_ERR_ALERT_WARNING:
2490 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2491 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002492
Adam Langleyfcf25832014-12-18 17:42:32 -08002493 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002494 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002495 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002496
Adam Langleyfcf25832014-12-18 17:42:32 -08002497 default:
2498 return 1;
2499 }
2500}
Adam Langleyed8270a2014-09-02 13:52:56 -07002501
Adam Langleyfcf25832014-12-18 17:42:32 -08002502static int ssl_check_serverhello_tlsext(SSL *s) {
David Benjaminfc059942015-07-30 23:01:59 -04002503 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002504 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002505
Adam Langleyfcf25832014-12-18 17:42:32 -08002506 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2507 ret = s->ctx->tlsext_servername_callback(s, &al,
2508 s->ctx->tlsext_servername_arg);
2509 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002510 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002511 ret = s->initial_ctx->tlsext_servername_callback(
2512 s, &al, s->initial_ctx->tlsext_servername_arg);
2513 }
Adam Langley95c29f32014-06-20 12:00:00 -07002514
Adam Langleyfcf25832014-12-18 17:42:32 -08002515 switch (ret) {
2516 case SSL_TLSEXT_ERR_ALERT_FATAL:
2517 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2518 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002519
Adam Langleyfcf25832014-12-18 17:42:32 -08002520 case SSL_TLSEXT_ERR_ALERT_WARNING:
2521 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2522 return 1;
2523
2524 default:
2525 return 1;
2526 }
2527}
2528
2529int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2530 int alert = -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002531 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2532 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2533 return 0;
2534 }
2535
2536 if (ssl_check_serverhello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002537 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002538 return 0;
2539 }
2540
2541 return 1;
2542}
Adam Langley95c29f32014-06-20 12:00:00 -07002543
David Benjamine3aa1d92015-06-16 15:34:50 -04002544int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2545 int *out_send_ticket, const uint8_t *ticket,
2546 size_t ticket_len, const uint8_t *session_id,
2547 size_t session_id_len) {
2548 int ret = 1; /* Most errors are non-fatal. */
2549 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2550 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002551
David Benjamine3aa1d92015-06-16 15:34:50 -04002552 HMAC_CTX hmac_ctx;
2553 HMAC_CTX_init(&hmac_ctx);
2554 EVP_CIPHER_CTX cipher_ctx;
2555 EVP_CIPHER_CTX_init(&cipher_ctx);
2556
2557 *out_send_ticket = 0;
2558 *out_session = NULL;
2559
2560 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2561 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002562 }
2563
David Benjamine3aa1d92015-06-16 15:34:50 -04002564 if (ticket_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002565 /* The client will accept a ticket but doesn't currently have one. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002566 *out_send_ticket = 1;
2567 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002568 }
2569
David Benjaminadcc3952015-04-26 13:07:57 -04002570 /* Ensure there is room for the key name and the largest IV
2571 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2572 * the maximum IV length should be well under the minimum size for the
2573 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002574 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2575 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002576 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002577 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002578
David Benjamine3aa1d92015-06-16 15:34:50 -04002579 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2580 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2581 (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2582 0 /* decrypt */);
2583 if (cb_ret < 0) {
2584 ret = 0;
2585 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002586 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002587 if (cb_ret == 0) {
2588 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002589 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002590 if (cb_ret == 2) {
2591 *out_send_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002592 }
2593 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002594 /* Check the key name matches. */
2595 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2596 SSL_TICKET_KEY_NAME_LEN) != 0) {
2597 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002598 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002599 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2600 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002601 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002602 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2603 ssl_ctx->tlsext_tick_aes_key, iv)) {
2604 ret = 0;
2605 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002606 }
2607 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002608 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002609
David Benjamine3aa1d92015-06-16 15:34:50 -04002610 /* Check the MAC at the end of the ticket. */
2611 uint8_t mac[EVP_MAX_MD_SIZE];
2612 size_t mac_len = HMAC_size(&hmac_ctx);
2613 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002614 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002615 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002616 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002617 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2618 HMAC_Final(&hmac_ctx, mac, NULL);
2619 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2620 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002621 }
2622
David Benjamine3aa1d92015-06-16 15:34:50 -04002623 /* Decrypt the session data. */
2624 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2625 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2626 mac_len;
2627 plaintext = OPENSSL_malloc(ciphertext_len);
2628 if (plaintext == NULL) {
2629 ret = 0;
2630 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002631 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002632 if (ciphertext_len >= INT_MAX) {
2633 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002634 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002635 int len1, len2;
2636 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2637 (int)ciphertext_len) ||
2638 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2639 ERR_clear_error(); /* Don't leave an error on the queue. */
2640 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002641 }
2642
David Benjamine3aa1d92015-06-16 15:34:50 -04002643 /* Decode the session. */
2644 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2645 if (session == NULL) {
2646 ERR_clear_error(); /* Don't leave an error on the queue. */
2647 goto done;
2648 }
2649
2650 /* Copy the client's session ID into the new session, to denote the ticket has
2651 * been accepted. */
2652 memcpy(session->session_id, session_id, session_id_len);
2653 session->session_id_length = session_id_len;
2654
2655 *out_session = session;
2656
2657done:
2658 OPENSSL_free(plaintext);
2659 HMAC_CTX_cleanup(&hmac_ctx);
2660 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2661 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002662}
Adam Langley95c29f32014-06-20 12:00:00 -07002663
2664/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002665typedef struct {
2666 int nid;
2667 int id;
2668} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002669
Adam Langleyfcf25832014-12-18 17:42:32 -08002670static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2671 {NID_sha1, TLSEXT_hash_sha1},
2672 {NID_sha224, TLSEXT_hash_sha224},
2673 {NID_sha256, TLSEXT_hash_sha256},
2674 {NID_sha384, TLSEXT_hash_sha384},
2675 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002676
Adam Langleyfcf25832014-12-18 17:42:32 -08002677static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2678 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002679
Adam Langleyfcf25832014-12-18 17:42:32 -08002680static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2681 size_t i;
2682 for (i = 0; i < tlen; i++) {
2683 if (table[i].nid == nid) {
2684 return table[i].id;
2685 }
2686 }
Adam Langley95c29f32014-06-20 12:00:00 -07002687
Adam Langleyfcf25832014-12-18 17:42:32 -08002688 return -1;
2689}
Adam Langley95c29f32014-06-20 12:00:00 -07002690
David Benjaminb4d65fd2015-05-29 17:11:21 -04002691int tls12_get_sigid(int pkey_type) {
2692 return tls12_find_id(pkey_type, tls12_sig,
2693 sizeof(tls12_sig) / sizeof(tls12_lookup));
2694}
2695
David Benjamind1d80782015-07-05 11:54:09 -04002696int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002697 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002698
Adam Langleyfcf25832014-12-18 17:42:32 -08002699 if (!md) {
2700 return 0;
2701 }
Adam Langley95c29f32014-06-20 12:00:00 -07002702
Adam Langleyfcf25832014-12-18 17:42:32 -08002703 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2704 sizeof(tls12_md) / sizeof(tls12_lookup));
2705 if (md_id == -1) {
2706 return 0;
2707 }
Adam Langley95c29f32014-06-20 12:00:00 -07002708
David Benjamind1d80782015-07-05 11:54:09 -04002709 sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08002710 if (sig_id == -1) {
2711 return 0;
2712 }
Adam Langley95c29f32014-06-20 12:00:00 -07002713
Adam Langleyfcf25832014-12-18 17:42:32 -08002714 p[0] = (uint8_t)md_id;
2715 p[1] = (uint8_t)sig_id;
2716 return 1;
2717}
2718
Adam Langleyfcf25832014-12-18 17:42:32 -08002719const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2720 switch (hash_alg) {
2721 case TLSEXT_hash_md5:
2722 return EVP_md5();
2723
2724 case TLSEXT_hash_sha1:
2725 return EVP_sha1();
2726
2727 case TLSEXT_hash_sha224:
2728 return EVP_sha224();
2729
2730 case TLSEXT_hash_sha256:
2731 return EVP_sha256();
2732
2733 case TLSEXT_hash_sha384:
2734 return EVP_sha384();
2735
2736 case TLSEXT_hash_sha512:
2737 return EVP_sha512();
2738
2739 default:
2740 return NULL;
2741 }
2742}
Adam Langley95c29f32014-06-20 12:00:00 -07002743
David Benjaminec2f27d2014-11-13 19:17:25 -05002744/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2745 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002746static int tls12_get_pkey_type(uint8_t sig_alg) {
2747 switch (sig_alg) {
2748 case TLSEXT_signature_rsa:
2749 return EVP_PKEY_RSA;
2750
2751 case TLSEXT_signature_ecdsa:
2752 return EVP_PKEY_EC;
2753
2754 default:
2755 return -1;
2756 }
2757}
Adam Langley95c29f32014-06-20 12:00:00 -07002758
Steven Valdez0d62f262015-09-04 12:41:04 -04002759OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2,
2760 sizeof_tls_sigalgs_is_not_two);
Adam Langleyfcf25832014-12-18 17:42:32 -08002761
Steven Valdez0d62f262015-09-04 12:41:04 -04002762int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002763 /* Extension ignored for inappropriate versions */
Steven Valdez0d62f262015-09-04 12:41:04 -04002764 if (!SSL_USE_SIGALGS(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002765 return 1;
2766 }
David Benjamincd996942014-07-20 16:23:51 -04002767
Steven Valdez0d62f262015-09-04 12:41:04 -04002768 CERT *const cert = ssl->cert;
2769 OPENSSL_free(cert->peer_sigalgs);
2770 cert->peer_sigalgs = NULL;
2771 cert->peer_sigalgslen = 0;
2772
2773 size_t num_sigalgs = CBS_len(in_sigalgs);
2774
2775 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002776 return 0;
2777 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002778 num_sigalgs /= 2;
2779
2780 /* supported_signature_algorithms in the certificate request is
2781 * allowed to be empty. */
2782 if (num_sigalgs == 0) {
2783 return 1;
2784 }
2785
2786 /* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two
2787 * (statically asserted above) and we just divided |num_sigalgs| by two. */
2788 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS));
2789 if (cert->peer_sigalgs == NULL) {
2790 return 0;
2791 }
2792 cert->peer_sigalgslen = num_sigalgs;
2793
2794 CBS sigalgs;
2795 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2796
2797 size_t i;
2798 for (i = 0; i < num_sigalgs; i++) {
2799 TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i];
2800 if (!CBS_get_u8(&sigalgs, &sigalg->rhash) ||
2801 !CBS_get_u8(&sigalgs, &sigalg->rsign)) {
2802 return 0;
2803 }
2804 }
Adam Langley95c29f32014-06-20 12:00:00 -07002805
Adam Langleyfcf25832014-12-18 17:42:32 -08002806 return 1;
2807}
David Benjaminec2f27d2014-11-13 19:17:25 -05002808
David Benjamind1d80782015-07-05 11:54:09 -04002809const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2810 CERT *cert = ssl->cert;
2811 int type = ssl_private_key_type(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -04002812 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002813
Steven Valdez0d62f262015-09-04 12:41:04 -04002814 static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
2815 NID_sha224, NID_sha1};
2816
2817 const int *digest_nids = kDefaultDigestList;
2818 size_t num_digest_nids =
2819 sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
2820 if (cert->digest_nids != NULL) {
2821 digest_nids = cert->digest_nids;
2822 num_digest_nids = cert->num_digest_nids;
2823 }
2824
2825 for (i = 0; i < num_digest_nids; i++) {
2826 const int digest_nid = digest_nids[i];
2827 for (j = 0; j < cert->peer_sigalgslen; j++) {
2828 const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash);
2829 if (md == NULL ||
2830 digest_nid != EVP_MD_type(md) ||
2831 tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) {
2832 continue;
2833 }
2834
2835 return md;
Adam Langleyfcf25832014-12-18 17:42:32 -08002836 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002837 }
Adam Langley95c29f32014-06-20 12:00:00 -07002838
Adam Langleyfcf25832014-12-18 17:42:32 -08002839 /* If no suitable digest may be found, default to SHA-1. */
2840 return EVP_sha1();
2841}
Adam Langley95c29f32014-06-20 12:00:00 -07002842
David Benjamind6a4ae92015-08-06 11:10:51 -04002843int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2844 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002845 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002846
2847 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002848 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2849 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002850 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002851
David Benjamind6a4ae92015-08-06 11:10:51 -04002852 static const char kClientIDMagic[] = "TLS Channel ID signature";
2853 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2854
2855 if (ssl->hit) {
2856 static const char kResumptionMagic[] = "Resumption";
2857 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2858 if (ssl->session->original_handshake_hash_len == 0) {
2859 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2860 goto err;
2861 }
2862 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2863 ssl->session->original_handshake_hash_len);
2864 }
2865
2866 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2867 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2868 sizeof(handshake_hash));
2869 if (handshake_hash_len < 0) {
2870 goto err;
2871 }
2872 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2873 unsigned len_u;
2874 EVP_DigestFinal_ex(&ctx, out, &len_u);
2875 *out_len = len_u;
2876
2877 ret = 1;
2878
2879err:
2880 EVP_MD_CTX_cleanup(&ctx);
2881 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002882}
Adam Langley1258b6a2014-06-20 12:00:00 -07002883
2884/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2885 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002886int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2887 int digest_len;
2888 /* This function should never be called for a resumed session because the
2889 * handshake hashes that we wish to record are for the original, full
2890 * handshake. */
2891 if (s->hit) {
2892 return -1;
2893 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002894
Adam Langleyfcf25832014-12-18 17:42:32 -08002895 digest_len =
2896 tls1_handshake_digest(s, s->session->original_handshake_hash,
2897 sizeof(s->session->original_handshake_hash));
2898 if (digest_len < 0) {
2899 return -1;
2900 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002901
Adam Langleyfcf25832014-12-18 17:42:32 -08002902 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002903
Adam Langleyfcf25832014-12-18 17:42:32 -08002904 return 1;
2905}