blob: d312a5255d27e1b5769d2310edaf192418346680 [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 Langleyfcf25832014-12-18 17:42:32 -0800605int tls1_check_ec_tmp_key(SSL *s) {
David Benjaminc0f763b2015-03-27 02:05:39 -0400606 if (s->cert->ecdh_nid != NID_undef) {
David Benjamindd978782015-04-24 15:20:13 -0400607 /* If the curve is preconfigured, ECDH is acceptable iff the peer supports
David Benjaminc0f763b2015-03-27 02:05:39 -0400608 * the curve. */
609 uint16_t curve_id;
610 return tls1_ec_nid2curve_id(&curve_id, s->cert->ecdh_nid) &&
611 tls1_check_curve_id(s, curve_id);
Adam Langleyfcf25832014-12-18 17:42:32 -0800612 }
613
David Benjamindd978782015-04-24 15:20:13 -0400614 if (s->cert->ecdh_tmp_cb != NULL) {
615 /* Assume the callback will provide an acceptable curve. */
616 return 1;
617 }
618
619 /* Otherwise, the curve gets selected automatically. ECDH is acceptable iff
620 * there is a shared curve. */
621 return tls1_get_shared_curve(s) != NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800622}
Adam Langley95c29f32014-06-20 12:00:00 -0700623
624/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800625 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700626
Adam Langley95c29f32014-06-20 12:00:00 -0700627#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700628
Adam Langley95c29f32014-06-20 12:00:00 -0700629#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700630
Adam Langleyfcf25832014-12-18 17:42:32 -0800631#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700632
David Benjamincff64722014-08-19 19:54:46 -0400633static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800634 tlsext_sigalg(TLSEXT_hash_sha512)
635 tlsext_sigalg(TLSEXT_hash_sha384)
636 tlsext_sigalg(TLSEXT_hash_sha256)
637 tlsext_sigalg(TLSEXT_hash_sha224)
638 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700639};
David Benjamin05da6e12014-07-12 20:42:55 -0400640
Adam Langleyfcf25832014-12-18 17:42:32 -0800641size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
David Benjamin3dd90162015-08-09 11:26:40 -0400642 *psigs = tls12_sigalgs;
643 return sizeof(tls12_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800644}
Adam Langley95c29f32014-06-20 12:00:00 -0700645
David Benjamin6e807652015-11-02 12:02:20 -0500646int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert,
647 uint8_t hash, uint8_t signature, EVP_PKEY *pkey) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 const uint8_t *sent_sigs;
649 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400650 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800651
652 /* Should never happen */
653 if (sigalg == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400654 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800655 *out_alert = SSL_AD_INTERNAL_ERROR;
656 return 0;
657 }
658
Adam Langleyfcf25832014-12-18 17:42:32 -0800659 /* Check key type is consistent with signature */
660 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400661 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800662 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
663 return 0;
664 }
665
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500667 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800668 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
669 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
670 break;
671 }
672 }
673
David Benjamin788be4a2015-10-30 17:50:57 -0400674 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400675 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
677 return 0;
678 }
679
680 *out_md = tls12_get_hash(hash);
681 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400682 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800683 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
684 return 0;
685 }
686
687 return 1;
688}
689
690/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
691 * supported or doesn't appear in supported signature algorithms. Unlike
692 * ssl_cipher_get_disabled this applies to a specific session and not global
693 * settings. */
694void ssl_set_client_disabled(SSL *s) {
695 CERT *c = s->cert;
696 const uint8_t *sigalgs;
697 size_t i, sigalgslen;
698 int have_rsa = 0, have_ecdsa = 0;
699 c->mask_a = 0;
700 c->mask_k = 0;
701
Adam Langleyfcf25832014-12-18 17:42:32 -0800702 /* Now go through all signature algorithms seeing if we support any for RSA,
703 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
704 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
705 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
706 switch (sigalgs[1]) {
707 case TLSEXT_signature_rsa:
708 have_rsa = 1;
709 break;
710
711 case TLSEXT_signature_ecdsa:
712 have_ecdsa = 1;
713 break;
714 }
715 }
716
717 /* Disable auth if we don't include any appropriate signature algorithms. */
718 if (!have_rsa) {
719 c->mask_a |= SSL_aRSA;
720 }
721 if (!have_ecdsa) {
722 c->mask_a |= SSL_aECDSA;
723 }
724
725 /* with PSK there must be client callback set */
726 if (!s->psk_client_callback) {
727 c->mask_a |= SSL_aPSK;
728 c->mask_k |= SSL_kPSK;
729 }
730}
Adam Langley95c29f32014-06-20 12:00:00 -0700731
Adam Langley614c66a2015-06-12 15:26:58 -0700732/* tls_extension represents a TLS extension that is handled internally. The
733 * |init| function is called for each handshake, before any other functions of
734 * the extension. Then the add and parse callbacks are called as needed.
735 *
736 * The parse callbacks receive a |CBS| that contains the contents of the
737 * extension (i.e. not including the type and length bytes). If an extension is
738 * not received then the parse callbacks will be called with a NULL CBS so that
739 * they can do any processing needed to handle the absence of an extension.
740 *
741 * The add callbacks receive a |CBB| to which the extension can be appended but
742 * the function is responsible for appending the type and length bytes too.
743 *
744 * All callbacks return one for success and zero for error. If a parse function
745 * returns zero then a fatal alert with value |*out_alert| will be sent. If
746 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
747struct tls_extension {
748 uint16_t value;
749 void (*init)(SSL *ssl);
750
751 int (*add_clienthello)(SSL *ssl, CBB *out);
752 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
753
754 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
755 int (*add_serverhello)(SSL *ssl, CBB *out);
756};
757
758
759/* Server name indication (SNI).
760 *
761 * https://tools.ietf.org/html/rfc6066#section-3. */
762
763static void ext_sni_init(SSL *ssl) {
764 ssl->s3->tmp.should_ack_sni = 0;
765}
766
767static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
768 if (ssl->tlsext_hostname == NULL) {
769 return 1;
770 }
771
772 CBB contents, server_name_list, name;
773 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
774 !CBB_add_u16_length_prefixed(out, &contents) ||
775 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
776 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
777 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
778 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
779 strlen(ssl->tlsext_hostname)) ||
780 !CBB_flush(out)) {
781 return 0;
782 }
783
784 return 1;
785}
786
787static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
788 if (contents == NULL) {
789 return 1;
790 }
791
792 if (CBS_len(contents) != 0) {
793 return 0;
794 }
795
796 assert(ssl->tlsext_hostname != NULL);
797
798 if (!ssl->hit) {
799 assert(ssl->session->tlsext_hostname == NULL);
800 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
801 if (!ssl->session->tlsext_hostname) {
802 *out_alert = SSL_AD_INTERNAL_ERROR;
803 return 0;
804 }
805 }
806
807 return 1;
808}
809
810static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
811 if (contents == NULL) {
812 return 1;
813 }
814
815 /* The servername extension is treated as follows:
816 *
817 * - Only the hostname type is supported with a maximum length of 255.
818 * - The servername is rejected if too long or if it contains zeros, in
819 * which case an fatal alert is generated.
820 * - The servername field is maintained together with the session cache.
821 * - When a session is resumed, the servername callback is invoked in order
822 * to allow the application to position itself to the right context.
823 * - The servername is acknowledged if it is new for a session or when
824 * it is identical to a previously used for the same session.
825 * Applications can control the behaviour. They can at any time
826 * set a 'desirable' servername for a new SSL object. This can be the
827 * case for example with HTTPS when a Host: header field is received and
828 * a renegotiation is requested. In this case, a possible servername
829 * presented in the new client hello is only acknowledged if it matches
830 * the value of the Host: field.
831 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
832 * if they provide for changing an explicit servername context for the
833 * session,
834 * i.e. when the session has been established with a servername extension.
835 */
836
837 CBS server_name_list;
838 char have_seen_host_name = 0;
839
840 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
841 CBS_len(&server_name_list) == 0 ||
842 CBS_len(contents) != 0) {
843 return 0;
844 }
845
846 /* Decode each ServerName in the extension. */
847 while (CBS_len(&server_name_list) > 0) {
848 uint8_t name_type;
849 CBS host_name;
850
851 if (!CBS_get_u8(&server_name_list, &name_type) ||
852 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
853 return 0;
854 }
855
856 /* Only host_name is supported. */
857 if (name_type != TLSEXT_NAMETYPE_host_name) {
858 continue;
859 }
860
861 if (have_seen_host_name) {
862 /* The ServerNameList MUST NOT contain more than one name of the same
863 * name_type. */
864 return 0;
865 }
866
867 have_seen_host_name = 1;
868
869 if (CBS_len(&host_name) == 0 ||
870 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
871 CBS_contains_zero_byte(&host_name)) {
872 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
873 return 0;
874 }
875
876 if (!ssl->hit) {
877 assert(ssl->session->tlsext_hostname == NULL);
878 if (ssl->session->tlsext_hostname) {
879 /* This should be impossible. */
880 return 0;
881 }
882
883 /* Copy the hostname as a string. */
884 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
885 *out_alert = SSL_AD_INTERNAL_ERROR;
886 return 0;
887 }
888
889 ssl->s3->tmp.should_ack_sni = 1;
890 }
891 }
892
893 return 1;
894}
895
896static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
897 if (ssl->hit ||
898 !ssl->s3->tmp.should_ack_sni ||
899 ssl->session->tlsext_hostname == NULL) {
900 return 1;
901 }
902
903 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
904 !CBB_add_u16(out, 0 /* length */)) {
905 return 0;
906 }
907
908 return 1;
909}
910
911
Adam Langley5021b222015-06-12 18:27:58 -0700912/* Renegotiation indication.
913 *
914 * https://tools.ietf.org/html/rfc5746 */
915
916static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
917 CBB contents, prev_finished;
918 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
919 !CBB_add_u16_length_prefixed(out, &contents) ||
920 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
921 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
922 ssl->s3->previous_client_finished_len) ||
923 !CBB_flush(out)) {
924 return 0;
925 }
926
927 return 1;
928}
929
930static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
931 CBS *contents) {
David Benjamin3e052de2015-11-25 20:10:31 -0500932 /* Servers may not switch between omitting the extension and supporting it.
933 * See RFC 5746, sections 3.5 and 4.2. */
934 if (ssl->s3->initial_handshake_complete &&
935 (contents != NULL) != ssl->s3->send_connection_binding) {
936 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
937 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
938 return 0;
939 }
940
Adam Langley5021b222015-06-12 18:27:58 -0700941 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500942 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700943 * RI even on initial ServerHello because the client doesn't see any
944 * renegotiation during an attack. However this would mean we could not
945 * connect to any server which doesn't support RI.
946 *
David Benjamine9cddb82015-11-23 14:36:40 -0500947 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
948 * practical terms every client sets it so it's just assumed here. */
949 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700950 }
951
952 const size_t expected_len = ssl->s3->previous_client_finished_len +
953 ssl->s3->previous_server_finished_len;
954
955 /* Check for logic errors */
956 assert(!expected_len || ssl->s3->previous_client_finished_len);
957 assert(!expected_len || ssl->s3->previous_server_finished_len);
958
959 /* Parse out the extension contents. */
960 CBS renegotiated_connection;
961 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
962 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400963 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700964 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
965 return 0;
966 }
967
968 /* Check that the extension matches. */
969 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400970 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700971 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
972 return 0;
973 }
974
975 const uint8_t *d = CBS_data(&renegotiated_connection);
976 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
977 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400978 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700979 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
980 return 0;
981 }
982 d += ssl->s3->previous_client_finished_len;
983
984 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
985 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400986 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700987 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
988 return 0;
989 }
990 ssl->s3->send_connection_binding = 1;
991
992 return 1;
993}
994
995static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
996 CBS *contents) {
997 /* Renegotiation isn't supported as a server so this function should never be
998 * called after the initial handshake. */
999 assert(!ssl->s3->initial_handshake_complete);
1000
1001 CBS fake_contents;
1002 static const uint8_t kFakeExtension[] = {0};
1003
1004 if (contents == NULL) {
1005 if (ssl->s3->send_connection_binding) {
1006 /* The renegotiation SCSV was received so pretend that we received a
1007 * renegotiation extension. */
1008 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
1009 contents = &fake_contents;
1010 /* We require that the renegotiation extension is at index zero of
1011 * kExtensions. */
1012 ssl->s3->tmp.extensions.received |= (1u << 0);
1013 } else {
1014 return 1;
1015 }
1016 }
1017
1018 CBS renegotiated_connection;
1019
1020 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
1021 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001022 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -07001023 return 0;
1024 }
1025
1026 /* Check that the extension matches */
1027 if (!CBS_mem_equal(&renegotiated_connection, ssl->s3->previous_client_finished,
1028 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001029 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001030 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1031 return 0;
1032 }
1033
1034 ssl->s3->send_connection_binding = 1;
1035
1036 return 1;
1037}
1038
1039static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
1040 CBB contents, prev_finished;
1041 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
1042 !CBB_add_u16_length_prefixed(out, &contents) ||
1043 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
1044 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
1045 ssl->s3->previous_client_finished_len) ||
1046 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
1047 ssl->s3->previous_server_finished_len) ||
1048 !CBB_flush(out)) {
1049 return 0;
1050 }
1051
1052 return 1;
1053}
1054
Adam Langley0a056712015-07-01 15:03:33 -07001055
1056/* Extended Master Secret.
1057 *
1058 * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
1059
1060static void ext_ems_init(SSL *ssl) {
1061 ssl->s3->tmp.extended_master_secret = 0;
1062}
1063
1064static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
1065 if (ssl->version == SSL3_VERSION) {
1066 return 1;
1067 }
1068
1069 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1070 !CBB_add_u16(out, 0 /* length */)) {
1071 return 0;
1072 }
1073
1074 return 1;
1075}
1076
1077static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1078 CBS *contents) {
1079 if (contents == NULL) {
1080 return 1;
1081 }
1082
1083 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
1084 return 0;
1085 }
1086
1087 ssl->s3->tmp.extended_master_secret = 1;
1088 return 1;
1089}
1090
1091static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1092 if (ssl->version == SSL3_VERSION || contents == NULL) {
1093 return 1;
1094 }
1095
1096 if (CBS_len(contents) != 0) {
1097 return 0;
1098 }
1099
1100 ssl->s3->tmp.extended_master_secret = 1;
1101 return 1;
1102}
1103
1104static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1105 if (!ssl->s3->tmp.extended_master_secret) {
1106 return 1;
1107 }
1108
1109 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1110 !CBB_add_u16(out, 0 /* length */)) {
1111 return 0;
1112 }
1113
1114 return 1;
1115}
1116
Adam Langley9b05bc52015-07-01 15:25:33 -07001117
1118/* Session tickets.
1119 *
1120 * https://tools.ietf.org/html/rfc5077 */
1121
1122static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1123 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1124 return 1;
1125 }
1126
1127 const uint8_t *ticket_data = NULL;
1128 int ticket_len = 0;
1129
1130 /* Renegotiation does not participate in session resumption. However, still
1131 * advertise the extension to avoid potentially breaking servers which carry
1132 * over the state from the previous handshake, such as OpenSSL servers
1133 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1134 if (!ssl->s3->initial_handshake_complete &&
1135 ssl->session != NULL &&
1136 ssl->session->tlsext_tick != NULL) {
1137 ticket_data = ssl->session->tlsext_tick;
1138 ticket_len = ssl->session->tlsext_ticklen;
1139 }
1140
1141 CBB ticket;
1142 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1143 !CBB_add_u16_length_prefixed(out, &ticket) ||
1144 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1145 !CBB_flush(out)) {
1146 return 0;
1147 }
1148
1149 return 1;
1150}
1151
1152static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1153 CBS *contents) {
1154 ssl->tlsext_ticket_expected = 0;
1155
1156 if (contents == NULL) {
1157 return 1;
1158 }
1159
1160 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1161 * this function should never be called, even if the server tries to send the
1162 * extension. */
1163 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1164
1165 if (CBS_len(contents) != 0) {
1166 return 0;
1167 }
1168
1169 ssl->tlsext_ticket_expected = 1;
1170 return 1;
1171}
1172
1173static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1174 /* This function isn't used because the ticket extension from the client is
David Benjaminc7ce9772015-10-09 19:32:41 -04001175 * handled in ssl_session.c. */
Adam Langley9b05bc52015-07-01 15:25:33 -07001176 return 1;
1177}
1178
1179static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1180 if (!ssl->tlsext_ticket_expected) {
1181 return 1;
1182 }
1183
1184 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1185 * true. */
1186 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1187
1188 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1189 !CBB_add_u16(out, 0 /* length */)) {
1190 return 0;
1191 }
1192
1193 return 1;
1194}
1195
1196
Adam Langley2e857bd2015-07-01 16:09:19 -07001197/* Signature Algorithms.
1198 *
1199 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1200
1201static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1202 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1203 return 1;
1204 }
1205
1206 const uint8_t *sigalgs_data;
1207 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1208
1209 CBB contents, sigalgs;
1210 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1211 !CBB_add_u16_length_prefixed(out, &contents) ||
1212 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1213 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1214 !CBB_flush(out)) {
1215 return 0;
1216 }
1217
1218 return 1;
1219}
1220
1221static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1222 CBS *contents) {
1223 if (contents != NULL) {
1224 /* Servers MUST NOT send this extension. */
1225 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001226 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001227 return 0;
1228 }
1229
1230 return 1;
1231}
1232
1233static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1234 CBS *contents) {
1235 OPENSSL_free(ssl->cert->peer_sigalgs);
1236 ssl->cert->peer_sigalgs = NULL;
1237 ssl->cert->peer_sigalgslen = 0;
1238
Adam Langley2e857bd2015-07-01 16:09:19 -07001239 if (contents == NULL) {
1240 return 1;
1241 }
1242
1243 CBS supported_signature_algorithms;
1244 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001245 CBS_len(contents) != 0 ||
1246 CBS_len(&supported_signature_algorithms) == 0 ||
1247 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001248 return 0;
1249 }
1250
1251 return 1;
1252}
1253
1254static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1255 /* Servers MUST NOT send this extension. */
1256 return 1;
1257}
1258
1259
Adam Langleybb0bd042015-07-01 16:21:03 -07001260/* OCSP Stapling.
1261 *
1262 * https://tools.ietf.org/html/rfc6066#section-8 */
1263
1264static void ext_ocsp_init(SSL *ssl) {
1265 ssl->s3->tmp.certificate_status_expected = 0;
1266}
1267
1268static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1269 if (!ssl->ocsp_stapling_enabled) {
1270 return 1;
1271 }
1272
1273 CBB contents;
1274 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1275 !CBB_add_u16_length_prefixed(out, &contents) ||
1276 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1277 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1278 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1279 !CBB_flush(out)) {
1280 return 0;
1281 }
1282
1283 return 1;
1284}
1285
1286static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001287 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001288 if (contents == NULL) {
1289 return 1;
1290 }
1291
1292 if (CBS_len(contents) != 0) {
1293 return 0;
1294 }
1295
1296 ssl->s3->tmp.certificate_status_expected = 1;
1297 return 1;
1298}
1299
1300static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1301 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001302 if (contents == NULL) {
1303 return 1;
1304 }
1305
1306 uint8_t status_type;
1307 if (!CBS_get_u8(contents, &status_type)) {
1308 return 0;
1309 }
1310
1311 /* We cannot decide whether OCSP stapling will occur yet because the correct
1312 * SSL_CTX might not have been selected. */
1313 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1314
Adam Langleybb0bd042015-07-01 16:21:03 -07001315 return 1;
1316}
1317
1318static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001319 /* The extension shouldn't be sent when resuming sessions. */
1320 if (ssl->hit ||
1321 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001322 ssl->ctx->ocsp_response_length == 0) {
1323 return 1;
1324 }
1325
1326 ssl->s3->tmp.certificate_status_expected = 1;
1327
1328 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1329 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001330}
1331
1332
Adam Langley97dfcbf2015-07-01 18:35:20 -07001333/* Next protocol negotiation.
1334 *
1335 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1336
1337static void ext_npn_init(SSL *ssl) {
1338 ssl->s3->next_proto_neg_seen = 0;
1339}
1340
1341static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1342 if (ssl->s3->initial_handshake_complete ||
1343 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001344 (ssl->options & SSL_OP_DISABLE_NPN) ||
Adam Langley97dfcbf2015-07-01 18:35:20 -07001345 SSL_IS_DTLS(ssl)) {
1346 return 1;
1347 }
1348
1349 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1350 !CBB_add_u16(out, 0 /* length */)) {
1351 return 0;
1352 }
1353
1354 return 1;
1355}
1356
1357static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1358 CBS *contents) {
1359 if (contents == NULL) {
1360 return 1;
1361 }
1362
1363 /* If any of these are false then we should never have sent the NPN
1364 * extension in the ClientHello and thus this function should never have been
1365 * called. */
1366 assert(!ssl->s3->initial_handshake_complete);
1367 assert(!SSL_IS_DTLS(ssl));
1368 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001369 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001370
David Benjamin76c2efc2015-08-31 14:24:29 -04001371 if (ssl->s3->alpn_selected != NULL) {
1372 /* NPN and ALPN may not be negotiated in the same connection. */
1373 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1374 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1375 return 0;
1376 }
1377
Adam Langley97dfcbf2015-07-01 18:35:20 -07001378 const uint8_t *const orig_contents = CBS_data(contents);
1379 const size_t orig_len = CBS_len(contents);
1380
1381 while (CBS_len(contents) != 0) {
1382 CBS proto;
1383 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1384 CBS_len(&proto) == 0) {
1385 return 0;
1386 }
1387 }
1388
1389 uint8_t *selected;
1390 uint8_t selected_len;
1391 if (ssl->ctx->next_proto_select_cb(
1392 ssl, &selected, &selected_len, orig_contents, orig_len,
1393 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1394 *out_alert = SSL_AD_INTERNAL_ERROR;
1395 return 0;
1396 }
1397
1398 OPENSSL_free(ssl->next_proto_negotiated);
1399 ssl->next_proto_negotiated = BUF_memdup(selected, selected_len);
1400 if (ssl->next_proto_negotiated == NULL) {
1401 *out_alert = SSL_AD_INTERNAL_ERROR;
1402 return 0;
1403 }
1404
1405 ssl->next_proto_negotiated_len = selected_len;
1406 ssl->s3->next_proto_neg_seen = 1;
1407
1408 return 1;
1409}
1410
1411static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1412 CBS *contents) {
1413 if (contents != NULL && CBS_len(contents) != 0) {
1414 return 0;
1415 }
1416
1417 if (contents == NULL ||
1418 ssl->s3->initial_handshake_complete ||
1419 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1420 * afterwards, parsing the ALPN extension will clear
1421 * |next_proto_neg_seen|. */
1422 ssl->s3->alpn_selected != NULL ||
1423 ssl->ctx->next_protos_advertised_cb == NULL ||
1424 SSL_IS_DTLS(ssl)) {
1425 return 1;
1426 }
1427
1428 ssl->s3->next_proto_neg_seen = 1;
1429 return 1;
1430}
1431
1432static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1433 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1434 * parsed. */
1435 if (!ssl->s3->next_proto_neg_seen) {
1436 return 1;
1437 }
1438
1439 const uint8_t *npa;
1440 unsigned npa_len;
1441
1442 if (ssl->ctx->next_protos_advertised_cb(
1443 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1444 SSL_TLSEXT_ERR_OK) {
1445 ssl->s3->next_proto_neg_seen = 0;
1446 return 1;
1447 }
1448
1449 CBB contents;
1450 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1451 !CBB_add_u16_length_prefixed(out, &contents) ||
1452 !CBB_add_bytes(&contents, npa, npa_len) ||
1453 !CBB_flush(out)) {
1454 return 0;
1455 }
1456
1457 return 1;
1458}
1459
1460
Adam Langleyab8d87d2015-07-10 12:21:39 -07001461/* Signed certificate timestamps.
1462 *
1463 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1464
1465static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1466 if (!ssl->signed_cert_timestamps_enabled) {
1467 return 1;
1468 }
1469
1470 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1471 !CBB_add_u16(out, 0 /* length */)) {
1472 return 0;
1473 }
1474
1475 return 1;
1476}
1477
1478static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1479 CBS *contents) {
1480 if (contents == NULL) {
1481 return 1;
1482 }
1483
1484 /* If this is false then we should never have sent the SCT extension in the
1485 * ClientHello and thus this function should never have been called. */
1486 assert(ssl->signed_cert_timestamps_enabled);
1487
1488 if (CBS_len(contents) == 0) {
1489 *out_alert = SSL_AD_DECODE_ERROR;
1490 return 0;
1491 }
1492
1493 /* Session resumption uses the original session information. */
1494 if (!ssl->hit &&
1495 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1496 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1497 *out_alert = SSL_AD_INTERNAL_ERROR;
1498 return 0;
1499 }
1500
1501 return 1;
1502}
1503
1504static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1505 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001506 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001507}
1508
1509static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001510 /* The extension shouldn't be sent when resuming sessions. */
1511 if (ssl->hit ||
1512 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001513 return 1;
1514 }
1515
1516 CBB contents;
1517 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1518 CBB_add_u16_length_prefixed(out, &contents) &&
1519 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1520 ssl->ctx->signed_cert_timestamp_list_length) &&
1521 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001522}
1523
1524
Adam Langleyf18e4532015-07-10 13:39:53 -07001525/* Application-level Protocol Negotiation.
1526 *
1527 * https://tools.ietf.org/html/rfc7301 */
1528
1529static void ext_alpn_init(SSL *ssl) {
1530 OPENSSL_free(ssl->s3->alpn_selected);
1531 ssl->s3->alpn_selected = NULL;
1532}
1533
1534static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1535 if (ssl->alpn_client_proto_list == NULL ||
1536 ssl->s3->initial_handshake_complete) {
1537 return 1;
1538 }
1539
1540 CBB contents, proto_list;
1541 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1542 !CBB_add_u16_length_prefixed(out, &contents) ||
1543 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1544 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1545 ssl->alpn_client_proto_list_len) ||
1546 !CBB_flush(out)) {
1547 return 0;
1548 }
1549
1550 return 1;
1551}
1552
1553static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1554 CBS *contents) {
1555 if (contents == NULL) {
1556 return 1;
1557 }
1558
1559 assert(!ssl->s3->initial_handshake_complete);
1560 assert(ssl->alpn_client_proto_list != NULL);
1561
David Benjamin76c2efc2015-08-31 14:24:29 -04001562 if (ssl->s3->next_proto_neg_seen) {
1563 /* NPN and ALPN may not be negotiated in the same connection. */
1564 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1565 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1566 return 0;
1567 }
1568
Adam Langleyf18e4532015-07-10 13:39:53 -07001569 /* The extension data consists of a ProtocolNameList which must have
1570 * exactly one ProtocolName. Each of these is length-prefixed. */
1571 CBS protocol_name_list, protocol_name;
1572 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1573 CBS_len(contents) != 0 ||
1574 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1575 /* Empty protocol names are forbidden. */
1576 CBS_len(&protocol_name) == 0 ||
1577 CBS_len(&protocol_name_list) != 0) {
1578 return 0;
1579 }
1580
1581 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1582 &ssl->s3->alpn_selected_len)) {
1583 *out_alert = SSL_AD_INTERNAL_ERROR;
1584 return 0;
1585 }
1586
1587 return 1;
1588}
1589
1590static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1591 CBS *contents) {
1592 if (contents == NULL) {
1593 return 1;
1594 }
1595
1596 if (ssl->ctx->alpn_select_cb == NULL ||
1597 ssl->s3->initial_handshake_complete) {
1598 return 1;
1599 }
1600
1601 /* ALPN takes precedence over NPN. */
1602 ssl->s3->next_proto_neg_seen = 0;
1603
1604 CBS protocol_name_list;
1605 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1606 CBS_len(contents) != 0 ||
1607 CBS_len(&protocol_name_list) < 2) {
1608 return 0;
1609 }
1610
1611 /* Validate the protocol list. */
1612 CBS protocol_name_list_copy = protocol_name_list;
1613 while (CBS_len(&protocol_name_list_copy) > 0) {
1614 CBS protocol_name;
1615
1616 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1617 /* Empty protocol names are forbidden. */
1618 CBS_len(&protocol_name) == 0) {
1619 return 0;
1620 }
1621 }
1622
1623 const uint8_t *selected;
1624 uint8_t selected_len;
1625 if (ssl->ctx->alpn_select_cb(
1626 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1627 CBS_len(&protocol_name_list),
1628 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1629 OPENSSL_free(ssl->s3->alpn_selected);
1630 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1631 if (ssl->s3->alpn_selected == NULL) {
1632 *out_alert = SSL_AD_INTERNAL_ERROR;
1633 return 0;
1634 }
1635 ssl->s3->alpn_selected_len = selected_len;
1636 }
1637
1638 return 1;
1639}
1640
1641static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1642 if (ssl->s3->alpn_selected == NULL) {
1643 return 1;
1644 }
1645
1646 CBB contents, proto_list, proto;
1647 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1648 !CBB_add_u16_length_prefixed(out, &contents) ||
1649 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1650 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
1651 !CBB_add_bytes(&proto, ssl->s3->alpn_selected, ssl->s3->alpn_selected_len) ||
1652 !CBB_flush(out)) {
1653 return 0;
1654 }
1655
1656 return 1;
1657}
1658
1659
Adam Langley49c7af12015-07-10 14:33:46 -07001660/* Channel ID.
1661 *
1662 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1663
1664static void ext_channel_id_init(SSL *ssl) {
1665 ssl->s3->tlsext_channel_id_valid = 0;
1666}
1667
1668static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1669 if (!ssl->tlsext_channel_id_enabled ||
1670 SSL_IS_DTLS(ssl)) {
1671 return 1;
1672 }
1673
1674 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1675 !CBB_add_u16(out, 0 /* length */)) {
1676 return 0;
1677 }
1678
1679 return 1;
1680}
1681
1682static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1683 CBS *contents) {
1684 if (contents == NULL) {
1685 return 1;
1686 }
1687
1688 assert(!SSL_IS_DTLS(ssl));
1689 assert(ssl->tlsext_channel_id_enabled);
1690
1691 if (CBS_len(contents) != 0) {
1692 return 0;
1693 }
1694
1695 ssl->s3->tlsext_channel_id_valid = 1;
1696 return 1;
1697}
1698
1699static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1700 CBS *contents) {
1701 if (contents == NULL ||
1702 !ssl->tlsext_channel_id_enabled ||
1703 SSL_IS_DTLS(ssl)) {
1704 return 1;
1705 }
1706
1707 if (CBS_len(contents) != 0) {
1708 return 0;
1709 }
1710
1711 ssl->s3->tlsext_channel_id_valid = 1;
1712 return 1;
1713}
1714
1715static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1716 if (!ssl->s3->tlsext_channel_id_valid) {
1717 return 1;
1718 }
1719
1720 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1721 !CBB_add_u16(out, 0 /* length */)) {
1722 return 0;
1723 }
1724
1725 return 1;
1726}
1727
Adam Langley391250d2015-07-15 19:06:07 -07001728
1729/* Secure Real-time Transport Protocol (SRTP) extension.
1730 *
1731 * https://tools.ietf.org/html/rfc5764 */
1732
Adam Langley391250d2015-07-15 19:06:07 -07001733
1734static void ext_srtp_init(SSL *ssl) {
1735 ssl->srtp_profile = NULL;
1736}
1737
1738static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1739 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1740 if (profiles == NULL) {
1741 return 1;
1742 }
1743 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1744 if (num_profiles == 0) {
1745 return 1;
1746 }
1747
1748 CBB contents, profile_ids;
1749 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1750 !CBB_add_u16_length_prefixed(out, &contents) ||
1751 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1752 return 0;
1753 }
1754
1755 size_t i;
1756 for (i = 0; i < num_profiles; i++) {
1757 if (!CBB_add_u16(&profile_ids,
1758 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1759 return 0;
1760 }
1761 }
1762
1763 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1764 !CBB_flush(out)) {
1765 return 0;
1766 }
1767
1768 return 1;
1769}
1770
1771static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1772 CBS *contents) {
1773 if (contents == NULL) {
1774 return 1;
1775 }
1776
1777 /* The extension consists of a u16-prefixed profile ID list containing a
1778 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1779 *
1780 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1781 CBS profile_ids, srtp_mki;
1782 uint16_t profile_id;
1783 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1784 !CBS_get_u16(&profile_ids, &profile_id) ||
1785 CBS_len(&profile_ids) != 0 ||
1786 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1787 CBS_len(contents) != 0) {
1788 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1789 return 0;
1790 }
1791
1792 if (CBS_len(&srtp_mki) != 0) {
1793 /* Must be no MKI, since we never offer one. */
1794 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1795 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1796 return 0;
1797 }
1798
1799 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1800
1801 /* Check to see if the server gave us something we support (and presumably
1802 * offered). */
1803 size_t i;
1804 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1805 const SRTP_PROTECTION_PROFILE *profile =
1806 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1807
1808 if (profile->id == profile_id) {
1809 ssl->srtp_profile = profile;
1810 return 1;
1811 }
1812 }
1813
1814 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1815 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1816 return 0;
1817}
1818
1819static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1820 CBS *contents) {
1821 if (contents == NULL) {
1822 return 1;
1823 }
1824
1825 CBS profile_ids, srtp_mki;
1826 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1827 CBS_len(&profile_ids) < 2 ||
1828 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1829 CBS_len(contents) != 0) {
1830 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1831 return 0;
1832 }
1833 /* Discard the MKI value for now. */
1834
1835 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1836 SSL_get_srtp_profiles(ssl);
1837
1838 /* Pick the server's most preferred profile. */
1839 size_t i;
1840 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1841 const SRTP_PROTECTION_PROFILE *server_profile =
1842 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1843
1844 CBS profile_ids_tmp;
1845 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1846
1847 while (CBS_len(&profile_ids_tmp) > 0) {
1848 uint16_t profile_id;
1849 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1850 return 0;
1851 }
1852
1853 if (server_profile->id == profile_id) {
1854 ssl->srtp_profile = server_profile;
1855 return 1;
1856 }
1857 }
1858 }
1859
1860 return 1;
1861}
1862
1863static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1864 if (ssl->srtp_profile == NULL) {
1865 return 1;
1866 }
1867
1868 CBB contents, profile_ids;
1869 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1870 !CBB_add_u16_length_prefixed(out, &contents) ||
1871 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1872 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1873 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1874 !CBB_flush(out)) {
1875 return 0;
1876 }
1877
1878 return 1;
1879}
1880
Adam Langleybdd5d662015-07-20 16:19:08 -07001881
1882/* EC point formats.
1883 *
1884 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1885
1886static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1887 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1888 return 0;
1889 }
1890
1891 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1892
1893 size_t i;
1894 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1895 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1896
1897 const uint32_t alg_k = cipher->algorithm_mkey;
1898 const uint32_t alg_a = cipher->algorithm_auth;
1899 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1900 return 1;
1901 }
1902 }
1903
1904 return 0;
1905}
1906
Adam Langleybdd5d662015-07-20 16:19:08 -07001907static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001908 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001909 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1910 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001911 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1912 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001913 !CBB_flush(out)) {
1914 return 0;
1915 }
1916
1917 return 1;
1918}
1919
1920static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1921 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1922 return 1;
1923 }
1924
1925 return ext_ec_point_add_extension(ssl, out);
1926}
1927
1928static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1929 CBS *contents) {
1930 if (contents == NULL) {
1931 return 1;
1932 }
1933
1934 CBS ec_point_format_list;
1935 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1936 CBS_len(contents) != 0) {
1937 return 0;
1938 }
1939
David Benjaminfc059942015-07-30 23:01:59 -04001940 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1941 * point format. */
1942 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1943 CBS_len(&ec_point_format_list)) == NULL) {
1944 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001945 return 0;
1946 }
1947
1948 return 1;
1949}
1950
1951static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1952 CBS *contents) {
1953 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1954}
1955
1956static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1957 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1958 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001959 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001960
1961 if (!using_ecc) {
1962 return 1;
1963 }
1964
1965 return ext_ec_point_add_extension(ssl, out);
1966}
1967
Adam Langley273d49c2015-07-20 16:38:52 -07001968
1969/* EC supported curves.
1970 *
1971 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1972
1973static void ext_ec_curves_init(SSL *ssl) {
1974 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1975 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1976 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
1977}
1978
1979static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
1980 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1981 return 1;
1982 }
1983
1984 CBB contents, curves_bytes;
1985 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
1986 !CBB_add_u16_length_prefixed(out, &contents) ||
1987 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
1988 return 0;
1989 }
1990
1991 const uint16_t *curves;
1992 size_t curves_len;
1993 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
1994
1995 size_t i;
1996 for (i = 0; i < curves_len; i++) {
1997 if (!CBB_add_u16(&curves_bytes, curves[i])) {
1998 return 0;
1999 }
2000 }
2001
2002 return CBB_flush(out);
2003}
2004
2005static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2006 CBS *contents) {
2007 /* This extension is not expected to be echoed by servers and is ignored. */
2008 return 1;
2009}
2010
2011static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2012 CBS *contents) {
2013 if (contents == NULL) {
2014 return 1;
2015 }
2016
2017 CBS elliptic_curve_list;
2018 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
2019 CBS_len(&elliptic_curve_list) == 0 ||
2020 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
2021 CBS_len(contents) != 0) {
2022 return 0;
2023 }
2024
2025 ssl->s3->tmp.peer_ellipticcurvelist =
2026 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
2027
2028 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
2029 *out_alert = SSL_AD_INTERNAL_ERROR;
2030 return 0;
2031 }
2032
2033 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
2034 size_t i;
2035 for (i = 0; i < num_curves; i++) {
2036 if (!CBS_get_u16(&elliptic_curve_list,
2037 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
2038 goto err;
2039 }
2040 }
2041
2042 assert(CBS_len(&elliptic_curve_list) == 0);
2043 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
2044
2045 return 1;
2046
2047err:
2048 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2049 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2050 *out_alert = SSL_AD_INTERNAL_ERROR;
2051 return 0;
2052}
2053
2054static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
2055 /* Servers don't echo this extension. */
2056 return 1;
2057}
2058
2059
Adam Langley614c66a2015-06-12 15:26:58 -07002060/* kExtensions contains all the supported extensions. */
2061static const struct tls_extension kExtensions[] = {
2062 {
Adam Langley5021b222015-06-12 18:27:58 -07002063 /* The renegotiation extension must always be at index zero because the
2064 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2065 * sent as an SCSV. */
2066 TLSEXT_TYPE_renegotiate,
2067 NULL,
2068 ext_ri_add_clienthello,
2069 ext_ri_parse_serverhello,
2070 ext_ri_parse_clienthello,
2071 ext_ri_add_serverhello,
2072 },
2073 {
Adam Langley614c66a2015-06-12 15:26:58 -07002074 TLSEXT_TYPE_server_name,
2075 ext_sni_init,
2076 ext_sni_add_clienthello,
2077 ext_sni_parse_serverhello,
2078 ext_sni_parse_clienthello,
2079 ext_sni_add_serverhello,
2080 },
Adam Langley0a056712015-07-01 15:03:33 -07002081 {
2082 TLSEXT_TYPE_extended_master_secret,
2083 ext_ems_init,
2084 ext_ems_add_clienthello,
2085 ext_ems_parse_serverhello,
2086 ext_ems_parse_clienthello,
2087 ext_ems_add_serverhello,
2088 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002089 {
2090 TLSEXT_TYPE_session_ticket,
2091 NULL,
2092 ext_ticket_add_clienthello,
2093 ext_ticket_parse_serverhello,
2094 ext_ticket_parse_clienthello,
2095 ext_ticket_add_serverhello,
2096 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002097 {
2098 TLSEXT_TYPE_signature_algorithms,
2099 NULL,
2100 ext_sigalgs_add_clienthello,
2101 ext_sigalgs_parse_serverhello,
2102 ext_sigalgs_parse_clienthello,
2103 ext_sigalgs_add_serverhello,
2104 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002105 {
2106 TLSEXT_TYPE_status_request,
2107 ext_ocsp_init,
2108 ext_ocsp_add_clienthello,
2109 ext_ocsp_parse_serverhello,
2110 ext_ocsp_parse_clienthello,
2111 ext_ocsp_add_serverhello,
2112 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002113 {
2114 TLSEXT_TYPE_next_proto_neg,
2115 ext_npn_init,
2116 ext_npn_add_clienthello,
2117 ext_npn_parse_serverhello,
2118 ext_npn_parse_clienthello,
2119 ext_npn_add_serverhello,
2120 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002121 {
2122 TLSEXT_TYPE_certificate_timestamp,
2123 NULL,
2124 ext_sct_add_clienthello,
2125 ext_sct_parse_serverhello,
2126 ext_sct_parse_clienthello,
2127 ext_sct_add_serverhello,
2128 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002129 {
2130 TLSEXT_TYPE_application_layer_protocol_negotiation,
2131 ext_alpn_init,
2132 ext_alpn_add_clienthello,
2133 ext_alpn_parse_serverhello,
2134 ext_alpn_parse_clienthello,
2135 ext_alpn_add_serverhello,
2136 },
Adam Langley49c7af12015-07-10 14:33:46 -07002137 {
2138 TLSEXT_TYPE_channel_id,
2139 ext_channel_id_init,
2140 ext_channel_id_add_clienthello,
2141 ext_channel_id_parse_serverhello,
2142 ext_channel_id_parse_clienthello,
2143 ext_channel_id_add_serverhello,
2144 },
Adam Langley391250d2015-07-15 19:06:07 -07002145 {
2146 TLSEXT_TYPE_srtp,
2147 ext_srtp_init,
2148 ext_srtp_add_clienthello,
2149 ext_srtp_parse_serverhello,
2150 ext_srtp_parse_clienthello,
2151 ext_srtp_add_serverhello,
2152 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002153 {
2154 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002155 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002156 ext_ec_point_add_clienthello,
2157 ext_ec_point_parse_serverhello,
2158 ext_ec_point_parse_clienthello,
2159 ext_ec_point_add_serverhello,
2160 },
Adam Langley273d49c2015-07-20 16:38:52 -07002161 {
2162 TLSEXT_TYPE_elliptic_curves,
2163 ext_ec_curves_init,
2164 ext_ec_curves_add_clienthello,
2165 ext_ec_curves_parse_serverhello,
2166 ext_ec_curves_parse_clienthello,
2167 ext_ec_curves_add_serverhello,
2168 },
Adam Langley614c66a2015-06-12 15:26:58 -07002169};
2170
2171#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2172
Adam Langley4cfa96b2015-07-01 11:56:55 -07002173OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2174 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002175 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002176OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2177 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2178 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002179 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002180
Adam Langley614c66a2015-06-12 15:26:58 -07002181static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2182 uint16_t value) {
2183 unsigned i;
2184 for (i = 0; i < kNumExtensions; i++) {
2185 if (kExtensions[i].value == value) {
2186 *out_index = i;
2187 return &kExtensions[i];
2188 }
2189 }
2190
2191 return NULL;
2192}
2193
Adam Langley09505632015-07-30 18:10:13 -07002194int SSL_extension_supported(unsigned extension_value) {
2195 uint32_t index;
2196 return extension_value == TLSEXT_TYPE_padding ||
2197 tls_extension_find(&index, extension_value) != NULL;
2198}
2199
David Benjamine8d53502015-10-10 14:13:23 -04002200int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002201 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002202 if (ssl->client_version == SSL3_VERSION &&
2203 !ssl->s3->send_connection_binding) {
2204 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002205 }
Adam Langley95c29f32014-06-20 12:00:00 -07002206
David Benjamine8d53502015-10-10 14:13:23 -04002207 size_t orig_len = CBB_len(out);
2208 CBB extensions;
2209 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002210 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002211 }
Adam Langley95c29f32014-06-20 12:00:00 -07002212
David Benjamine8d53502015-10-10 14:13:23 -04002213 ssl->s3->tmp.extensions.sent = 0;
2214 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002215
Adam Langley614c66a2015-06-12 15:26:58 -07002216 size_t i;
2217 for (i = 0; i < kNumExtensions; i++) {
2218 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002219 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002220 }
2221 }
Adam Langley95c29f32014-06-20 12:00:00 -07002222
Adam Langley614c66a2015-06-12 15:26:58 -07002223 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002224 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002225 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002226 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2227 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2228 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002229 }
Adam Langley95c29f32014-06-20 12:00:00 -07002230
Adam Langley33ad2b52015-07-20 17:43:53 -07002231 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002232 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002233 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002234 }
Adam Langley75712922014-10-10 16:23:43 -07002235
David Benjamine8d53502015-10-10 14:13:23 -04002236 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002237 goto err;
2238 }
2239
David Benjamine8d53502015-10-10 14:13:23 -04002240 if (!SSL_IS_DTLS(ssl)) {
2241 header_len += CBB_len(&extensions) - orig_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002242 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002243 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002244 *
2245 * NB: because this code works out the length of all existing extensions
2246 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002247 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002248 /* Extensions take at least four bytes to encode. Always include least
2249 * one byte of data if including the extension. WebSphere Application
2250 * Server 7.0 is intolerant to the last extension being zero-length. */
2251 if (padding_len >= 4 + 1) {
2252 padding_len -= 4;
2253 } else {
2254 padding_len = 1;
2255 }
Adam Langley95c29f32014-06-20 12:00:00 -07002256
Adam Langley33ad2b52015-07-20 17:43:53 -07002257 uint8_t *padding_bytes;
2258 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2259 !CBB_add_u16(&extensions, padding_len) ||
2260 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2261 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002262 }
Adam Langley75712922014-10-10 16:23:43 -07002263
Adam Langley33ad2b52015-07-20 17:43:53 -07002264 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002265 }
2266 }
Adam Langley75712922014-10-10 16:23:43 -07002267
Adam Langley33ad2b52015-07-20 17:43:53 -07002268 /* If only two bytes have been written then the extensions are actually empty
2269 * and those two bytes are the zero length. In that case, we don't bother
2270 * sending the extensions length. */
David Benjamine8d53502015-10-10 14:13:23 -04002271 if (CBB_len(&extensions) - orig_len == 2) {
2272 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002273 }
2274
David Benjamine8d53502015-10-10 14:13:23 -04002275 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002276
2277err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002278 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002279 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002280}
Adam Langley95c29f32014-06-20 12:00:00 -07002281
David Benjamin56380462015-10-10 14:59:09 -04002282int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
2283 const size_t orig_len = CBB_len(out);
2284
2285 CBB extensions;
2286 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002287 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002288 }
2289
2290 unsigned i;
2291 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002292 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002293 /* Don't send extensions that were not received. */
2294 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002295 }
Adam Langley95c29f32014-06-20 12:00:00 -07002296
David Benjamin56380462015-10-10 14:59:09 -04002297 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002298 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2299 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2300 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002301 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002302 }
Adam Langley95c29f32014-06-20 12:00:00 -07002303
David Benjamin56380462015-10-10 14:59:09 -04002304 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002305 goto err;
2306 }
2307
Adam Langley33ad2b52015-07-20 17:43:53 -07002308 /* If only two bytes have been written then the extensions are actually empty
2309 * and those two bytes are the zero length. In that case, we don't bother
2310 * sending the extensions length. */
David Benjamin56380462015-10-10 14:59:09 -04002311 if (CBB_len(&extensions) - orig_len == 2) {
2312 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002313 }
2314
David Benjamin56380462015-10-10 14:59:09 -04002315 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002316
2317err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002318 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002319 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002320}
Adam Langley95c29f32014-06-20 12:00:00 -07002321
Adam Langleyfcf25832014-12-18 17:42:32 -08002322static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002323 size_t i;
2324 for (i = 0; i < kNumExtensions; i++) {
2325 if (kExtensions[i].init != NULL) {
2326 kExtensions[i].init(s);
2327 }
2328 }
2329
2330 s->s3->tmp.extensions.received = 0;
Adam Langley09505632015-07-30 18:10:13 -07002331 s->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002332 /* The renegotiation extension must always be at index zero because the
2333 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2334 * sent as an SCSV. */
2335 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002336
Adam Langleyfcf25832014-12-18 17:42:32 -08002337 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002338 if (CBS_len(cbs) != 0) {
2339 /* Decode the extensions block and check it is valid. */
2340 CBS extensions;
2341 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2342 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002343 *out_alert = SSL_AD_DECODE_ERROR;
2344 return 0;
2345 }
Adam Langley95c29f32014-06-20 12:00:00 -07002346
Adam Langley33ad2b52015-07-20 17:43:53 -07002347 while (CBS_len(&extensions) != 0) {
2348 uint16_t type;
2349 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002350
Adam Langley33ad2b52015-07-20 17:43:53 -07002351 /* Decode the next extension. */
2352 if (!CBS_get_u16(&extensions, &type) ||
2353 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2354 *out_alert = SSL_AD_DECODE_ERROR;
2355 return 0;
2356 }
2357
David Benjaminc7ce9772015-10-09 19:32:41 -04002358 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2359 * ambiguous. Ignore all but the renegotiation_info extension. */
2360 if (s->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2361 continue;
2362 }
2363
Adam Langley33ad2b52015-07-20 17:43:53 -07002364 unsigned ext_index;
2365 const struct tls_extension *const ext =
2366 tls_extension_find(&ext_index, type);
2367
2368 if (ext == NULL) {
Adam Langley09505632015-07-30 18:10:13 -07002369 if (!custom_ext_parse_clienthello(s, out_alert, type, &extension)) {
2370 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2371 return 0;
2372 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002373 continue;
2374 }
2375
Adam Langley614c66a2015-06-12 15:26:58 -07002376 s->s3->tmp.extensions.received |= (1u << ext_index);
2377 uint8_t alert = SSL_AD_DECODE_ERROR;
2378 if (!ext->parse_clienthello(s, &alert, &extension)) {
2379 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002380 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2381 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002382 return 0;
2383 }
Adam Langley614c66a2015-06-12 15:26:58 -07002384 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002385 }
Adam Langley75712922014-10-10 16:23:43 -07002386
Adam Langley614c66a2015-06-12 15:26:58 -07002387 for (i = 0; i < kNumExtensions; i++) {
2388 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2389 /* Extension wasn't observed so call the callback with a NULL
2390 * parameter. */
2391 uint8_t alert = SSL_AD_DECODE_ERROR;
2392 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002393 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2394 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002395 *out_alert = alert;
2396 return 0;
2397 }
2398 }
2399 }
2400
Adam Langleyfcf25832014-12-18 17:42:32 -08002401 return 1;
2402}
Adam Langley95c29f32014-06-20 12:00:00 -07002403
Adam Langleyfcf25832014-12-18 17:42:32 -08002404int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
2405 int alert = -1;
2406 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
2407 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2408 return 0;
2409 }
Adam Langley95c29f32014-06-20 12:00:00 -07002410
Adam Langleyfcf25832014-12-18 17:42:32 -08002411 if (ssl_check_clienthello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002412 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002413 return 0;
2414 }
Adam Langley95c29f32014-06-20 12:00:00 -07002415
Adam Langleyfcf25832014-12-18 17:42:32 -08002416 return 1;
2417}
Adam Langley95c29f32014-06-20 12:00:00 -07002418
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002419OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2420
Adam Langleyfcf25832014-12-18 17:42:32 -08002421static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002422 uint32_t received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002423
Adam Langley33ad2b52015-07-20 17:43:53 -07002424 if (CBS_len(cbs) != 0) {
2425 /* Decode the extensions block and check it is valid. */
2426 CBS extensions;
2427 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2428 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002429 *out_alert = SSL_AD_DECODE_ERROR;
2430 return 0;
2431 }
Adam Langley95c29f32014-06-20 12:00:00 -07002432
Adam Langley614c66a2015-06-12 15:26:58 -07002433
Adam Langley33ad2b52015-07-20 17:43:53 -07002434 while (CBS_len(&extensions) != 0) {
2435 uint16_t type;
2436 CBS extension;
2437
2438 /* Decode the next extension. */
2439 if (!CBS_get_u16(&extensions, &type) ||
2440 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2441 *out_alert = SSL_AD_DECODE_ERROR;
2442 return 0;
2443 }
2444
2445 unsigned ext_index;
2446 const struct tls_extension *const ext =
2447 tls_extension_find(&ext_index, type);
2448
Adam Langley09505632015-07-30 18:10:13 -07002449 if (ext == NULL) {
2450 if (!custom_ext_parse_serverhello(s, out_alert, type, &extension)) {
2451 return 0;
2452 }
2453 continue;
2454 }
2455
2456 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2457 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002458 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002459 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002460 *out_alert = SSL_AD_DECODE_ERROR;
2461 return 0;
2462 }
David Benjamin03973092014-06-24 23:27:17 -04002463
Adam Langley614c66a2015-06-12 15:26:58 -07002464 received |= (1u << ext_index);
2465
2466 uint8_t alert = SSL_AD_DECODE_ERROR;
2467 if (!ext->parse_serverhello(s, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002468 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2469 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002470 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002471 return 0;
2472 }
Adam Langley614c66a2015-06-12 15:26:58 -07002473 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002474 }
Adam Langley95c29f32014-06-20 12:00:00 -07002475
Adam Langley33ad2b52015-07-20 17:43:53 -07002476 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002477 for (i = 0; i < kNumExtensions; i++) {
2478 if (!(received & (1u << i))) {
2479 /* Extension wasn't observed so call the callback with a NULL
2480 * parameter. */
2481 uint8_t alert = SSL_AD_DECODE_ERROR;
2482 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002483 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2484 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002485 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002486 return 0;
2487 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002488 }
2489 }
Adam Langley95c29f32014-06-20 12:00:00 -07002490
Adam Langleyfcf25832014-12-18 17:42:32 -08002491 return 1;
2492}
Adam Langley95c29f32014-06-20 12:00:00 -07002493
Adam Langleyfcf25832014-12-18 17:42:32 -08002494static int ssl_check_clienthello_tlsext(SSL *s) {
2495 int ret = SSL_TLSEXT_ERR_NOACK;
2496 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002497
Adam Langleyfcf25832014-12-18 17:42:32 -08002498 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2499 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002500
Adam Langleyfcf25832014-12-18 17:42:32 -08002501 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2502 ret = s->ctx->tlsext_servername_callback(s, &al,
2503 s->ctx->tlsext_servername_arg);
2504 } else if (s->initial_ctx != NULL &&
2505 s->initial_ctx->tlsext_servername_callback != 0) {
2506 ret = s->initial_ctx->tlsext_servername_callback(
2507 s, &al, s->initial_ctx->tlsext_servername_arg);
2508 }
Adam Langley95c29f32014-06-20 12:00:00 -07002509
Adam Langleyfcf25832014-12-18 17:42:32 -08002510 switch (ret) {
2511 case SSL_TLSEXT_ERR_ALERT_FATAL:
2512 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2513 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002514
Adam Langleyfcf25832014-12-18 17:42:32 -08002515 case SSL_TLSEXT_ERR_ALERT_WARNING:
2516 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2517 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002518
Adam Langleyfcf25832014-12-18 17:42:32 -08002519 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002520 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002521 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002522
Adam Langleyfcf25832014-12-18 17:42:32 -08002523 default:
2524 return 1;
2525 }
2526}
Adam Langleyed8270a2014-09-02 13:52:56 -07002527
Adam Langleyfcf25832014-12-18 17:42:32 -08002528static int ssl_check_serverhello_tlsext(SSL *s) {
David Benjaminfc059942015-07-30 23:01:59 -04002529 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002530 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002531
Adam Langleyfcf25832014-12-18 17:42:32 -08002532 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2533 ret = s->ctx->tlsext_servername_callback(s, &al,
2534 s->ctx->tlsext_servername_arg);
2535 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002536 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002537 ret = s->initial_ctx->tlsext_servername_callback(
2538 s, &al, s->initial_ctx->tlsext_servername_arg);
2539 }
Adam Langley95c29f32014-06-20 12:00:00 -07002540
Adam Langleyfcf25832014-12-18 17:42:32 -08002541 switch (ret) {
2542 case SSL_TLSEXT_ERR_ALERT_FATAL:
2543 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2544 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002545
Adam Langleyfcf25832014-12-18 17:42:32 -08002546 case SSL_TLSEXT_ERR_ALERT_WARNING:
2547 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2548 return 1;
2549
2550 default:
2551 return 1;
2552 }
2553}
2554
2555int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2556 int alert = -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002557 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2558 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2559 return 0;
2560 }
2561
2562 if (ssl_check_serverhello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002563 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002564 return 0;
2565 }
2566
2567 return 1;
2568}
Adam Langley95c29f32014-06-20 12:00:00 -07002569
David Benjamine3aa1d92015-06-16 15:34:50 -04002570int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2571 int *out_send_ticket, const uint8_t *ticket,
2572 size_t ticket_len, const uint8_t *session_id,
2573 size_t session_id_len) {
2574 int ret = 1; /* Most errors are non-fatal. */
2575 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2576 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002577
David Benjamine3aa1d92015-06-16 15:34:50 -04002578 HMAC_CTX hmac_ctx;
2579 HMAC_CTX_init(&hmac_ctx);
2580 EVP_CIPHER_CTX cipher_ctx;
2581 EVP_CIPHER_CTX_init(&cipher_ctx);
2582
2583 *out_send_ticket = 0;
2584 *out_session = NULL;
2585
2586 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2587 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002588 }
2589
David Benjamine3aa1d92015-06-16 15:34:50 -04002590 if (ticket_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002591 /* The client will accept a ticket but doesn't currently have one. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002592 *out_send_ticket = 1;
2593 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002594 }
2595
David Benjaminadcc3952015-04-26 13:07:57 -04002596 /* Ensure there is room for the key name and the largest IV
2597 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2598 * the maximum IV length should be well under the minimum size for the
2599 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002600 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2601 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002602 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002603 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002604
David Benjamine3aa1d92015-06-16 15:34:50 -04002605 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2606 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2607 (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2608 0 /* decrypt */);
2609 if (cb_ret < 0) {
2610 ret = 0;
2611 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002612 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002613 if (cb_ret == 0) {
2614 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002615 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002616 if (cb_ret == 2) {
2617 *out_send_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002618 }
2619 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002620 /* Check the key name matches. */
2621 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2622 SSL_TICKET_KEY_NAME_LEN) != 0) {
2623 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002624 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002625 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2626 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002627 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002628 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2629 ssl_ctx->tlsext_tick_aes_key, iv)) {
2630 ret = 0;
2631 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002632 }
2633 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002634 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002635
David Benjamine3aa1d92015-06-16 15:34:50 -04002636 /* Check the MAC at the end of the ticket. */
2637 uint8_t mac[EVP_MAX_MD_SIZE];
2638 size_t mac_len = HMAC_size(&hmac_ctx);
2639 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002640 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002641 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002642 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002643 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2644 HMAC_Final(&hmac_ctx, mac, NULL);
2645 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2646 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002647 }
2648
David Benjamine3aa1d92015-06-16 15:34:50 -04002649 /* Decrypt the session data. */
2650 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2651 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2652 mac_len;
2653 plaintext = OPENSSL_malloc(ciphertext_len);
2654 if (plaintext == NULL) {
2655 ret = 0;
2656 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002657 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002658 if (ciphertext_len >= INT_MAX) {
2659 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002660 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002661 int len1, len2;
2662 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2663 (int)ciphertext_len) ||
2664 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2665 ERR_clear_error(); /* Don't leave an error on the queue. */
2666 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002667 }
2668
David Benjamine3aa1d92015-06-16 15:34:50 -04002669 /* Decode the session. */
2670 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2671 if (session == NULL) {
2672 ERR_clear_error(); /* Don't leave an error on the queue. */
2673 goto done;
2674 }
2675
2676 /* Copy the client's session ID into the new session, to denote the ticket has
2677 * been accepted. */
2678 memcpy(session->session_id, session_id, session_id_len);
2679 session->session_id_length = session_id_len;
2680
2681 *out_session = session;
2682
2683done:
2684 OPENSSL_free(plaintext);
2685 HMAC_CTX_cleanup(&hmac_ctx);
2686 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2687 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002688}
Adam Langley95c29f32014-06-20 12:00:00 -07002689
2690/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002691typedef struct {
2692 int nid;
2693 int id;
2694} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002695
Adam Langleyfcf25832014-12-18 17:42:32 -08002696static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2697 {NID_sha1, TLSEXT_hash_sha1},
2698 {NID_sha224, TLSEXT_hash_sha224},
2699 {NID_sha256, TLSEXT_hash_sha256},
2700 {NID_sha384, TLSEXT_hash_sha384},
2701 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002702
Adam Langleyfcf25832014-12-18 17:42:32 -08002703static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2704 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002705
Adam Langleyfcf25832014-12-18 17:42:32 -08002706static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2707 size_t i;
2708 for (i = 0; i < tlen; i++) {
2709 if (table[i].nid == nid) {
2710 return table[i].id;
2711 }
2712 }
Adam Langley95c29f32014-06-20 12:00:00 -07002713
Adam Langleyfcf25832014-12-18 17:42:32 -08002714 return -1;
2715}
Adam Langley95c29f32014-06-20 12:00:00 -07002716
David Benjaminb4d65fd2015-05-29 17:11:21 -04002717int tls12_get_sigid(int pkey_type) {
2718 return tls12_find_id(pkey_type, tls12_sig,
2719 sizeof(tls12_sig) / sizeof(tls12_lookup));
2720}
2721
David Benjamind1d80782015-07-05 11:54:09 -04002722int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002723 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002724
Adam Langleyfcf25832014-12-18 17:42:32 -08002725 if (!md) {
2726 return 0;
2727 }
Adam Langley95c29f32014-06-20 12:00:00 -07002728
Adam Langleyfcf25832014-12-18 17:42:32 -08002729 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2730 sizeof(tls12_md) / sizeof(tls12_lookup));
2731 if (md_id == -1) {
2732 return 0;
2733 }
Adam Langley95c29f32014-06-20 12:00:00 -07002734
David Benjamind1d80782015-07-05 11:54:09 -04002735 sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08002736 if (sig_id == -1) {
2737 return 0;
2738 }
Adam Langley95c29f32014-06-20 12:00:00 -07002739
Adam Langleyfcf25832014-12-18 17:42:32 -08002740 p[0] = (uint8_t)md_id;
2741 p[1] = (uint8_t)sig_id;
2742 return 1;
2743}
2744
Adam Langleyfcf25832014-12-18 17:42:32 -08002745const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2746 switch (hash_alg) {
2747 case TLSEXT_hash_md5:
2748 return EVP_md5();
2749
2750 case TLSEXT_hash_sha1:
2751 return EVP_sha1();
2752
2753 case TLSEXT_hash_sha224:
2754 return EVP_sha224();
2755
2756 case TLSEXT_hash_sha256:
2757 return EVP_sha256();
2758
2759 case TLSEXT_hash_sha384:
2760 return EVP_sha384();
2761
2762 case TLSEXT_hash_sha512:
2763 return EVP_sha512();
2764
2765 default:
2766 return NULL;
2767 }
2768}
Adam Langley95c29f32014-06-20 12:00:00 -07002769
David Benjaminec2f27d2014-11-13 19:17:25 -05002770/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2771 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002772static int tls12_get_pkey_type(uint8_t sig_alg) {
2773 switch (sig_alg) {
2774 case TLSEXT_signature_rsa:
2775 return EVP_PKEY_RSA;
2776
2777 case TLSEXT_signature_ecdsa:
2778 return EVP_PKEY_EC;
2779
2780 default:
2781 return -1;
2782 }
2783}
Adam Langley95c29f32014-06-20 12:00:00 -07002784
Steven Valdez0d62f262015-09-04 12:41:04 -04002785OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2,
2786 sizeof_tls_sigalgs_is_not_two);
Adam Langleyfcf25832014-12-18 17:42:32 -08002787
Steven Valdez0d62f262015-09-04 12:41:04 -04002788int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002789 /* Extension ignored for inappropriate versions */
Steven Valdez0d62f262015-09-04 12:41:04 -04002790 if (!SSL_USE_SIGALGS(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002791 return 1;
2792 }
David Benjamincd996942014-07-20 16:23:51 -04002793
Steven Valdez0d62f262015-09-04 12:41:04 -04002794 CERT *const cert = ssl->cert;
2795 OPENSSL_free(cert->peer_sigalgs);
2796 cert->peer_sigalgs = NULL;
2797 cert->peer_sigalgslen = 0;
2798
2799 size_t num_sigalgs = CBS_len(in_sigalgs);
2800
2801 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002802 return 0;
2803 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002804 num_sigalgs /= 2;
2805
2806 /* supported_signature_algorithms in the certificate request is
2807 * allowed to be empty. */
2808 if (num_sigalgs == 0) {
2809 return 1;
2810 }
2811
2812 /* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two
2813 * (statically asserted above) and we just divided |num_sigalgs| by two. */
2814 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS));
2815 if (cert->peer_sigalgs == NULL) {
2816 return 0;
2817 }
2818 cert->peer_sigalgslen = num_sigalgs;
2819
2820 CBS sigalgs;
2821 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2822
2823 size_t i;
2824 for (i = 0; i < num_sigalgs; i++) {
2825 TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i];
2826 if (!CBS_get_u8(&sigalgs, &sigalg->rhash) ||
2827 !CBS_get_u8(&sigalgs, &sigalg->rsign)) {
2828 return 0;
2829 }
2830 }
Adam Langley95c29f32014-06-20 12:00:00 -07002831
Adam Langleyfcf25832014-12-18 17:42:32 -08002832 return 1;
2833}
David Benjaminec2f27d2014-11-13 19:17:25 -05002834
David Benjamind1d80782015-07-05 11:54:09 -04002835const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2836 CERT *cert = ssl->cert;
2837 int type = ssl_private_key_type(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -04002838 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002839
Steven Valdez0d62f262015-09-04 12:41:04 -04002840 static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
2841 NID_sha224, NID_sha1};
2842
2843 const int *digest_nids = kDefaultDigestList;
2844 size_t num_digest_nids =
2845 sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
2846 if (cert->digest_nids != NULL) {
2847 digest_nids = cert->digest_nids;
2848 num_digest_nids = cert->num_digest_nids;
2849 }
2850
2851 for (i = 0; i < num_digest_nids; i++) {
2852 const int digest_nid = digest_nids[i];
2853 for (j = 0; j < cert->peer_sigalgslen; j++) {
2854 const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash);
2855 if (md == NULL ||
2856 digest_nid != EVP_MD_type(md) ||
2857 tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) {
2858 continue;
2859 }
2860
2861 return md;
Adam Langleyfcf25832014-12-18 17:42:32 -08002862 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002863 }
Adam Langley95c29f32014-06-20 12:00:00 -07002864
Adam Langleyfcf25832014-12-18 17:42:32 -08002865 /* If no suitable digest may be found, default to SHA-1. */
2866 return EVP_sha1();
2867}
Adam Langley95c29f32014-06-20 12:00:00 -07002868
David Benjamind6a4ae92015-08-06 11:10:51 -04002869int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2870 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002871 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002872
2873 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002874 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2875 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002876 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002877
David Benjamind6a4ae92015-08-06 11:10:51 -04002878 static const char kClientIDMagic[] = "TLS Channel ID signature";
2879 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2880
2881 if (ssl->hit) {
2882 static const char kResumptionMagic[] = "Resumption";
2883 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2884 if (ssl->session->original_handshake_hash_len == 0) {
2885 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2886 goto err;
2887 }
2888 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2889 ssl->session->original_handshake_hash_len);
2890 }
2891
2892 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2893 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2894 sizeof(handshake_hash));
2895 if (handshake_hash_len < 0) {
2896 goto err;
2897 }
2898 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2899 unsigned len_u;
2900 EVP_DigestFinal_ex(&ctx, out, &len_u);
2901 *out_len = len_u;
2902
2903 ret = 1;
2904
2905err:
2906 EVP_MD_CTX_cleanup(&ctx);
2907 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002908}
Adam Langley1258b6a2014-06-20 12:00:00 -07002909
2910/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2911 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002912int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2913 int digest_len;
2914 /* This function should never be called for a resumed session because the
2915 * handshake hashes that we wish to record are for the original, full
2916 * handshake. */
2917 if (s->hit) {
2918 return -1;
2919 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002920
Adam Langleyfcf25832014-12-18 17:42:32 -08002921 digest_len =
2922 tls1_handshake_digest(s, s->session->original_handshake_hash,
2923 sizeof(s->session->original_handshake_hash));
2924 if (digest_len < 0) {
2925 return -1;
2926 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002927
Adam Langleyfcf25832014-12-18 17:42:32 -08002928 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002929
Adam Langleyfcf25832014-12-18 17:42:32 -08002930 return 1;
2931}