blob: caa0cc40c99f1a4f7ae4b07cb4d9cdd60ec79a7e [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>
125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800127
128
David Benjamin6c7aed02014-08-27 16:42:38 -0400129static int ssl_check_clienthello_tlsext(SSL *s);
130static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700131
David Benjamin338fcaf2014-12-11 01:20:52 -0500132const SSL3_ENC_METHOD TLSv1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500133 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800134 tls1_setup_key_block,
135 tls1_generate_master_secret,
136 tls1_change_cipher_state,
137 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800138 tls1_cert_verify_mac,
139 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
140 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
141 tls1_alert_code,
142 tls1_export_keying_material,
143 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800144};
Adam Langley95c29f32014-06-20 12:00:00 -0700145
David Benjamin338fcaf2014-12-11 01:20:52 -0500146const SSL3_ENC_METHOD TLSv1_1_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500147 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800148 tls1_setup_key_block,
149 tls1_generate_master_secret,
150 tls1_change_cipher_state,
151 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800152 tls1_cert_verify_mac,
153 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
154 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
155 tls1_alert_code,
156 tls1_export_keying_material,
157 SSL_ENC_FLAG_EXPLICIT_IV,
Adam Langleyfcf25832014-12-18 17:42:32 -0800158};
Adam Langley95c29f32014-06-20 12:00:00 -0700159
David Benjamin338fcaf2014-12-11 01:20:52 -0500160const SSL3_ENC_METHOD TLSv1_2_enc_data = {
David Benjamin41ac9792014-12-23 10:41:06 -0500161 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800162 tls1_setup_key_block,
163 tls1_generate_master_secret,
164 tls1_change_cipher_state,
165 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800166 tls1_cert_verify_mac,
167 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
168 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
169 tls1_alert_code,
170 tls1_export_keying_material,
171 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
172 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
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
Adam Langleyfcf25832014-12-18 17:42:32 -0800311char SSL_early_callback_ctx_extension_get(
312 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 Langleyfcf25832014-12-18 17:42:32 -0800355};
Adam Langley95c29f32014-06-20 12:00:00 -0700356
Adam Langleyfcf25832014-12-18 17:42:32 -0800357int tls1_ec_curve_id2nid(uint16_t curve_id) {
David Benjamin52e5bac2014-12-27 02:27:35 -0500358 size_t i;
359 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
360 if (curve_id == tls_curves[i].curve_id) {
361 return tls_curves[i].nid;
362 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 }
David Benjamin52e5bac2014-12-27 02:27:35 -0500364 return NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800365}
Adam Langley95c29f32014-06-20 12:00:00 -0700366
David Benjamin70bd80a2014-12-27 03:06:46 -0500367int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 size_t i;
David Benjamin52e5bac2014-12-27 02:27:35 -0500369 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
370 if (nid == tls_curves[i].nid) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500371 *out_curve_id = tls_curves[i].curve_id;
372 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 }
374 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 return 0;
376}
377
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200378const char* tls1_ec_curve_id2name(uint16_t curve_id) {
379 size_t i;
380 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
381 if (curve_id == tls_curves[i].curve_id) {
382 return tls_curves[i].curve_name;
383 }
384 }
385 return NULL;
386}
387
Adam Langleyfcf25832014-12-18 17:42:32 -0800388/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
389 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
390 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400391static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800392 const uint16_t **out_curve_ids,
393 size_t *out_curve_ids_len) {
394 if (get_peer_curves) {
David Benjamin55a43642015-04-20 14:45:55 -0400395 /* Only clients send a curve list, so this function is only called
396 * on the server. */
397 assert(s->server);
Adam Langleyfcf25832014-12-18 17:42:32 -0800398 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
399 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
400 return;
401 }
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 *out_curve_ids = s->tlsext_ellipticcurvelist;
404 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
405 if (!*out_curve_ids) {
406 *out_curve_ids = eccurves_default;
407 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
408 }
409}
David Benjamined439582014-07-14 19:13:02 -0400410
Adam Langleyfcf25832014-12-18 17:42:32 -0800411int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
412 uint8_t curve_type;
413 uint16_t curve_id;
414 const uint16_t *curves;
415 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400416
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 /* Only support named curves. */
418 if (!CBS_get_u8(cbs, &curve_type) ||
419 curve_type != NAMED_CURVE_TYPE ||
420 !CBS_get_u16(cbs, &curve_id)) {
421 return 0;
422 }
David Benjamined439582014-07-14 19:13:02 -0400423
Adam Langleyfcf25832014-12-18 17:42:32 -0800424 tls1_get_curvelist(s, 0, &curves, &curves_len);
425 for (i = 0; i < curves_len; i++) {
426 if (curve_id == curves[i]) {
427 *out_curve_id = curve_id;
428 return 1;
429 }
430 }
Adam Langley95c29f32014-06-20 12:00:00 -0700431
Adam Langleyfcf25832014-12-18 17:42:32 -0800432 return 0;
433}
David Benjamin072334d2014-07-13 16:24:27 -0400434
Adam Langleyfcf25832014-12-18 17:42:32 -0800435int tls1_get_shared_curve(SSL *s) {
David Benjamin55a43642015-04-20 14:45:55 -0400436 const uint16_t *curves, *peer_curves, *pref, *supp;
437 size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400438
Adam Langleyfcf25832014-12-18 17:42:32 -0800439 /* Can't do anything on client side */
440 if (s->server == 0) {
441 return NID_undef;
442 }
443
David Benjamin55a43642015-04-20 14:45:55 -0400444 tls1_get_curvelist(s, 0 /* local curves */, &curves, &curves_len);
445 tls1_get_curvelist(s, 1 /* peer curves */, &peer_curves, &peer_curves_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800446
David Benjamin55a43642015-04-20 14:45:55 -0400447 if (peer_curves_len == 0) {
448 /* Clients are not required to send a supported_curves extension. In this
449 * case, the server is free to pick any curve it likes. See RFC 4492,
450 * section 4, paragraph 3. */
451 return (curves_len == 0) ? NID_undef : tls1_ec_curve_id2nid(curves[0]);
452 }
453
454 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
455 pref = curves;
456 pref_len = curves_len;
457 supp = peer_curves;
458 supp_len = peer_curves_len;
459 } else {
460 pref = peer_curves;
461 pref_len = peer_curves_len;
462 supp = curves;
463 supp_len = curves_len;
464 }
465
466 for (i = 0; i < pref_len; i++) {
467 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 if (pref[i] == supp[j]) {
469 return tls1_ec_curve_id2nid(pref[i]);
470 }
471 }
472 }
473
474 return NID_undef;
475}
Adam Langley95c29f32014-06-20 12:00:00 -0700476
David Benjamin072334d2014-07-13 16:24:27 -0400477int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 const int *curves, size_t ncurves) {
479 uint16_t *curve_ids;
480 size_t i;
481
Adam Langleyfcf25832014-12-18 17:42:32 -0800482 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
483 if (curve_ids == NULL) {
484 return 0;
485 }
486
487 for (i = 0; i < ncurves; i++) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500488 if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 OPENSSL_free(curve_ids);
490 return 0;
491 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800492 }
493
David Benjamin2755a3e2015-04-22 16:17:58 -0400494 OPENSSL_free(*out_curve_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 *out_curve_ids = curve_ids;
496 *out_curve_ids_len = ncurves;
497
498 return 1;
499}
Adam Langley95c29f32014-06-20 12:00:00 -0700500
David Benjamin072334d2014-07-13 16:24:27 -0400501/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
502 * TLS curve ID and point format, respectively, for |ec|. It returns one on
503 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800504static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
505 uint8_t *out_comp_id, EC_KEY *ec) {
506 int nid;
507 uint16_t id;
508 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700509
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 if (ec == NULL) {
511 return 0;
512 }
Adam Langley95c29f32014-06-20 12:00:00 -0700513
Adam Langleyfcf25832014-12-18 17:42:32 -0800514 grp = EC_KEY_get0_group(ec);
515 if (grp == NULL) {
516 return 0;
517 }
David Benjamin072334d2014-07-13 16:24:27 -0400518
Adam Langleyfcf25832014-12-18 17:42:32 -0800519 /* Determine curve ID */
520 nid = EC_GROUP_get_curve_name(grp);
David Benjamin70bd80a2014-12-27 03:06:46 -0500521 if (!tls1_ec_nid2curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 return 0;
523 }
David Benjamin072334d2014-07-13 16:24:27 -0400524
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
526 *out_curve_id = id;
527
528 if (out_comp_id) {
529 if (EC_KEY_get0_public_key(ec) == NULL) {
530 return 0;
531 }
532 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
533 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
534 } else {
535 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
536 }
537 }
538
539 return 1;
540}
David Benjamin072334d2014-07-13 16:24:27 -0400541
Adam Langleyfcf25832014-12-18 17:42:32 -0800542/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
543 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400544 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800545static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
546 const uint16_t *curves;
David Benjamin55a43642015-04-20 14:45:55 -0400547 size_t curves_len, i, get_peer_curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400548
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 /* Check against our list, then the peer's list. */
David Benjamin55a43642015-04-20 14:45:55 -0400550 for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
551 if (get_peer_curves && !s->server) {
552 /* Servers do not present a preference list so, if we are a client, only
553 * check our list. */
554 continue;
555 }
556
557 tls1_get_curvelist(s, get_peer_curves, &curves, &curves_len);
558 if (get_peer_curves && curves_len == 0) {
559 /* Clients are not required to send a supported_curves extension. In this
560 * case, the server is free to pick any curve it likes. See RFC 4492,
561 * section 4, paragraph 3. */
562 continue;
563 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800564 for (i = 0; i < curves_len; i++) {
565 if (curves[i] == curve_id) {
566 break;
567 }
568 }
Adam Langley95c29f32014-06-20 12:00:00 -0700569
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 if (i == curves_len) {
571 return 0;
572 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800573 }
David Benjamin033e5f42014-11-13 18:47:41 -0500574
Adam Langleyfcf25832014-12-18 17:42:32 -0800575 return 1;
576}
David Benjamin033e5f42014-11-13 18:47:41 -0500577
Adam Langleyfcf25832014-12-18 17:42:32 -0800578int tls1_check_ec_cert(SSL *s, X509 *x) {
579 int ret = 0;
580 EVP_PKEY *pkey = X509_get_pubkey(x);
581 uint16_t curve_id;
582 uint8_t comp_id;
583
584 if (!pkey ||
585 pkey->type != EVP_PKEY_EC ||
586 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
587 !tls1_check_curve_id(s, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400588 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800589 goto done;
590 }
591
592 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500593
594done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400595 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 return ret;
597}
David Benjamin42e9a772014-09-02 23:18:44 -0400598
Adam Langleyfcf25832014-12-18 17:42:32 -0800599int tls1_check_ec_tmp_key(SSL *s) {
David Benjaminc0f763b2015-03-27 02:05:39 -0400600 if (s->cert->ecdh_nid != NID_undef) {
David Benjamindd978782015-04-24 15:20:13 -0400601 /* If the curve is preconfigured, ECDH is acceptable iff the peer supports
David Benjaminc0f763b2015-03-27 02:05:39 -0400602 * the curve. */
603 uint16_t curve_id;
604 return tls1_ec_nid2curve_id(&curve_id, s->cert->ecdh_nid) &&
605 tls1_check_curve_id(s, curve_id);
Adam Langleyfcf25832014-12-18 17:42:32 -0800606 }
607
David Benjamindd978782015-04-24 15:20:13 -0400608 if (s->cert->ecdh_tmp_cb != NULL) {
609 /* Assume the callback will provide an acceptable curve. */
610 return 1;
611 }
612
613 /* Otherwise, the curve gets selected automatically. ECDH is acceptable iff
614 * there is a shared curve. */
615 return tls1_get_shared_curve(s) != NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800616}
Adam Langley95c29f32014-06-20 12:00:00 -0700617
618/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800619 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700620
Adam Langley95c29f32014-06-20 12:00:00 -0700621#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700622
Adam Langley95c29f32014-06-20 12:00:00 -0700623#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700624
Adam Langleyfcf25832014-12-18 17:42:32 -0800625#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700626
David Benjamincff64722014-08-19 19:54:46 -0400627static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800628 tlsext_sigalg(TLSEXT_hash_sha512)
629 tlsext_sigalg(TLSEXT_hash_sha384)
630 tlsext_sigalg(TLSEXT_hash_sha256)
631 tlsext_sigalg(TLSEXT_hash_sha224)
632 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700633};
David Benjamin05da6e12014-07-12 20:42:55 -0400634
Adam Langleyfcf25832014-12-18 17:42:32 -0800635size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
David Benjamin3dd90162015-08-09 11:26:40 -0400636 *psigs = tls12_sigalgs;
637 return sizeof(tls12_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800638}
Adam Langley95c29f32014-06-20 12:00:00 -0700639
Adam Langleyfcf25832014-12-18 17:42:32 -0800640/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
641 * checks it is consistent with |s|'s sent supported signature algorithms and,
642 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
643 * returns 0 and writes an alert into |*out_alert|. */
644int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
645 CBS *cbs, EVP_PKEY *pkey) {
646 const uint8_t *sent_sigs;
647 size_t sent_sigslen, i;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400648 int sigalg = tls12_get_sigid(pkey->type);
Adam Langleyfcf25832014-12-18 17:42:32 -0800649 uint8_t hash, signature;
650
651 /* Should never happen */
652 if (sigalg == -1) {
David Benjamin3570d732015-06-29 00:28:17 -0400653 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800654 *out_alert = SSL_AD_INTERNAL_ERROR;
655 return 0;
656 }
657
658 if (!CBS_get_u8(cbs, &hash) ||
659 !CBS_get_u8(cbs, &signature)) {
David Benjamin3570d732015-06-29 00:28:17 -0400660 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800661 *out_alert = SSL_AD_DECODE_ERROR;
662 return 0;
663 }
664
665 /* Check key type is consistent with signature */
666 if (sigalg != signature) {
David Benjamin3570d732015-06-29 00:28:17 -0400667 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800668 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
669 return 0;
670 }
671
672 if (pkey->type == EVP_PKEY_EC) {
673 uint16_t curve_id;
674 uint8_t comp_id;
675 /* Check compression and curve matches extensions */
676 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
677 *out_alert = SSL_AD_INTERNAL_ERROR;
678 return 0;
679 }
680
681 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400682 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed)) {
David Benjamin3570d732015-06-29 00:28:17 -0400683 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800684 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
685 return 0;
686 }
687 }
688
689 /* Check signature matches a type we sent */
690 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
691 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
692 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
693 break;
694 }
695 }
696
697 /* Allow fallback to SHA-1. */
698 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
David Benjamin3570d732015-06-29 00:28:17 -0400699 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800700 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
701 return 0;
702 }
703
704 *out_md = tls12_get_hash(hash);
705 if (*out_md == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400706 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800707 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
708 return 0;
709 }
710
711 return 1;
712}
713
714/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
715 * supported or doesn't appear in supported signature algorithms. Unlike
716 * ssl_cipher_get_disabled this applies to a specific session and not global
717 * settings. */
718void ssl_set_client_disabled(SSL *s) {
719 CERT *c = s->cert;
720 const uint8_t *sigalgs;
721 size_t i, sigalgslen;
722 int have_rsa = 0, have_ecdsa = 0;
723 c->mask_a = 0;
724 c->mask_k = 0;
725
726 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
727 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
728 c->mask_ssl = SSL_TLSV1_2;
729 } else {
730 c->mask_ssl = 0;
731 }
732
733 /* Now go through all signature algorithms seeing if we support any for RSA,
734 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
735 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
736 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
737 switch (sigalgs[1]) {
738 case TLSEXT_signature_rsa:
739 have_rsa = 1;
740 break;
741
742 case TLSEXT_signature_ecdsa:
743 have_ecdsa = 1;
744 break;
745 }
746 }
747
748 /* Disable auth if we don't include any appropriate signature algorithms. */
749 if (!have_rsa) {
750 c->mask_a |= SSL_aRSA;
751 }
752 if (!have_ecdsa) {
753 c->mask_a |= SSL_aECDSA;
754 }
755
756 /* with PSK there must be client callback set */
757 if (!s->psk_client_callback) {
758 c->mask_a |= SSL_aPSK;
759 c->mask_k |= SSL_kPSK;
760 }
761}
Adam Langley95c29f32014-06-20 12:00:00 -0700762
Adam Langley614c66a2015-06-12 15:26:58 -0700763/* tls_extension represents a TLS extension that is handled internally. The
764 * |init| function is called for each handshake, before any other functions of
765 * the extension. Then the add and parse callbacks are called as needed.
766 *
767 * The parse callbacks receive a |CBS| that contains the contents of the
768 * extension (i.e. not including the type and length bytes). If an extension is
769 * not received then the parse callbacks will be called with a NULL CBS so that
770 * they can do any processing needed to handle the absence of an extension.
771 *
772 * The add callbacks receive a |CBB| to which the extension can be appended but
773 * the function is responsible for appending the type and length bytes too.
774 *
775 * All callbacks return one for success and zero for error. If a parse function
776 * returns zero then a fatal alert with value |*out_alert| will be sent. If
777 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
778struct tls_extension {
779 uint16_t value;
780 void (*init)(SSL *ssl);
781
782 int (*add_clienthello)(SSL *ssl, CBB *out);
783 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
784
785 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
786 int (*add_serverhello)(SSL *ssl, CBB *out);
787};
788
789
790/* Server name indication (SNI).
791 *
792 * https://tools.ietf.org/html/rfc6066#section-3. */
793
794static void ext_sni_init(SSL *ssl) {
795 ssl->s3->tmp.should_ack_sni = 0;
796}
797
798static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
799 if (ssl->tlsext_hostname == NULL) {
800 return 1;
801 }
802
803 CBB contents, server_name_list, name;
804 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
805 !CBB_add_u16_length_prefixed(out, &contents) ||
806 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
807 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
808 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
809 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
810 strlen(ssl->tlsext_hostname)) ||
811 !CBB_flush(out)) {
812 return 0;
813 }
814
815 return 1;
816}
817
818static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
819 if (contents == NULL) {
820 return 1;
821 }
822
823 if (CBS_len(contents) != 0) {
824 return 0;
825 }
826
827 assert(ssl->tlsext_hostname != NULL);
828
829 if (!ssl->hit) {
830 assert(ssl->session->tlsext_hostname == NULL);
831 ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
832 if (!ssl->session->tlsext_hostname) {
833 *out_alert = SSL_AD_INTERNAL_ERROR;
834 return 0;
835 }
836 }
837
838 return 1;
839}
840
841static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
842 if (contents == NULL) {
843 return 1;
844 }
845
846 /* The servername extension is treated as follows:
847 *
848 * - Only the hostname type is supported with a maximum length of 255.
849 * - The servername is rejected if too long or if it contains zeros, in
850 * which case an fatal alert is generated.
851 * - The servername field is maintained together with the session cache.
852 * - When a session is resumed, the servername callback is invoked in order
853 * to allow the application to position itself to the right context.
854 * - The servername is acknowledged if it is new for a session or when
855 * it is identical to a previously used for the same session.
856 * Applications can control the behaviour. They can at any time
857 * set a 'desirable' servername for a new SSL object. This can be the
858 * case for example with HTTPS when a Host: header field is received and
859 * a renegotiation is requested. In this case, a possible servername
860 * presented in the new client hello is only acknowledged if it matches
861 * the value of the Host: field.
862 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
863 * if they provide for changing an explicit servername context for the
864 * session,
865 * i.e. when the session has been established with a servername extension.
866 */
867
868 CBS server_name_list;
869 char have_seen_host_name = 0;
870
871 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
872 CBS_len(&server_name_list) == 0 ||
873 CBS_len(contents) != 0) {
874 return 0;
875 }
876
877 /* Decode each ServerName in the extension. */
878 while (CBS_len(&server_name_list) > 0) {
879 uint8_t name_type;
880 CBS host_name;
881
882 if (!CBS_get_u8(&server_name_list, &name_type) ||
883 !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
884 return 0;
885 }
886
887 /* Only host_name is supported. */
888 if (name_type != TLSEXT_NAMETYPE_host_name) {
889 continue;
890 }
891
892 if (have_seen_host_name) {
893 /* The ServerNameList MUST NOT contain more than one name of the same
894 * name_type. */
895 return 0;
896 }
897
898 have_seen_host_name = 1;
899
900 if (CBS_len(&host_name) == 0 ||
901 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
902 CBS_contains_zero_byte(&host_name)) {
903 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
904 return 0;
905 }
906
907 if (!ssl->hit) {
908 assert(ssl->session->tlsext_hostname == NULL);
909 if (ssl->session->tlsext_hostname) {
910 /* This should be impossible. */
911 return 0;
912 }
913
914 /* Copy the hostname as a string. */
915 if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
916 *out_alert = SSL_AD_INTERNAL_ERROR;
917 return 0;
918 }
919
920 ssl->s3->tmp.should_ack_sni = 1;
921 }
922 }
923
924 return 1;
925}
926
927static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
928 if (ssl->hit ||
929 !ssl->s3->tmp.should_ack_sni ||
930 ssl->session->tlsext_hostname == NULL) {
931 return 1;
932 }
933
934 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
935 !CBB_add_u16(out, 0 /* length */)) {
936 return 0;
937 }
938
939 return 1;
940}
941
942
Adam Langley5021b222015-06-12 18:27:58 -0700943/* Renegotiation indication.
944 *
945 * https://tools.ietf.org/html/rfc5746 */
946
947static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
948 CBB contents, prev_finished;
949 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
950 !CBB_add_u16_length_prefixed(out, &contents) ||
951 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
952 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
953 ssl->s3->previous_client_finished_len) ||
954 !CBB_flush(out)) {
955 return 0;
956 }
957
958 return 1;
959}
960
961static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
962 CBS *contents) {
963 if (contents == NULL) {
964 /* No renegotiation extension received.
965 *
966 * Strictly speaking if we want to avoid an attack we should *always* see
967 * RI even on initial ServerHello because the client doesn't see any
968 * renegotiation during an attack. However this would mean we could not
969 * connect to any server which doesn't support RI.
970 *
971 * A lack of the extension is allowed if SSL_OP_LEGACY_SERVER_CONNECT is
972 * defined. */
973 if (ssl->options & SSL_OP_LEGACY_SERVER_CONNECT) {
974 return 1;
975 }
976
977 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -0400978 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley5021b222015-06-12 18:27:58 -0700979 return 0;
980 }
981
982 const size_t expected_len = ssl->s3->previous_client_finished_len +
983 ssl->s3->previous_server_finished_len;
984
985 /* Check for logic errors */
986 assert(!expected_len || ssl->s3->previous_client_finished_len);
987 assert(!expected_len || ssl->s3->previous_server_finished_len);
988
989 /* Parse out the extension contents. */
990 CBS renegotiated_connection;
991 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
992 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400993 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700994 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
995 return 0;
996 }
997
998 /* Check that the extension matches. */
999 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -04001000 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001001 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1002 return 0;
1003 }
1004
1005 const uint8_t *d = CBS_data(&renegotiated_connection);
1006 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
1007 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001008 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001009 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1010 return 0;
1011 }
1012 d += ssl->s3->previous_client_finished_len;
1013
1014 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
1015 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001016 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001017 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1018 return 0;
1019 }
1020 ssl->s3->send_connection_binding = 1;
1021
1022 return 1;
1023}
1024
1025static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1026 CBS *contents) {
1027 /* Renegotiation isn't supported as a server so this function should never be
1028 * called after the initial handshake. */
1029 assert(!ssl->s3->initial_handshake_complete);
1030
1031 CBS fake_contents;
1032 static const uint8_t kFakeExtension[] = {0};
1033
1034 if (contents == NULL) {
1035 if (ssl->s3->send_connection_binding) {
1036 /* The renegotiation SCSV was received so pretend that we received a
1037 * renegotiation extension. */
1038 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
1039 contents = &fake_contents;
1040 /* We require that the renegotiation extension is at index zero of
1041 * kExtensions. */
1042 ssl->s3->tmp.extensions.received |= (1u << 0);
1043 } else {
1044 return 1;
1045 }
1046 }
1047
1048 CBS renegotiated_connection;
1049
1050 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
1051 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001052 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -07001053 return 0;
1054 }
1055
1056 /* Check that the extension matches */
1057 if (!CBS_mem_equal(&renegotiated_connection, ssl->s3->previous_client_finished,
1058 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001059 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -07001060 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1061 return 0;
1062 }
1063
1064 ssl->s3->send_connection_binding = 1;
1065
1066 return 1;
1067}
1068
1069static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
1070 CBB contents, prev_finished;
1071 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
1072 !CBB_add_u16_length_prefixed(out, &contents) ||
1073 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
1074 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
1075 ssl->s3->previous_client_finished_len) ||
1076 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
1077 ssl->s3->previous_server_finished_len) ||
1078 !CBB_flush(out)) {
1079 return 0;
1080 }
1081
1082 return 1;
1083}
1084
Adam Langley0a056712015-07-01 15:03:33 -07001085
1086/* Extended Master Secret.
1087 *
1088 * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
1089
1090static void ext_ems_init(SSL *ssl) {
1091 ssl->s3->tmp.extended_master_secret = 0;
1092}
1093
1094static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
1095 if (ssl->version == SSL3_VERSION) {
1096 return 1;
1097 }
1098
1099 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1100 !CBB_add_u16(out, 0 /* length */)) {
1101 return 0;
1102 }
1103
1104 return 1;
1105}
1106
1107static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1108 CBS *contents) {
1109 if (contents == NULL) {
1110 return 1;
1111 }
1112
1113 if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
1114 return 0;
1115 }
1116
1117 ssl->s3->tmp.extended_master_secret = 1;
1118 return 1;
1119}
1120
1121static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1122 if (ssl->version == SSL3_VERSION || contents == NULL) {
1123 return 1;
1124 }
1125
1126 if (CBS_len(contents) != 0) {
1127 return 0;
1128 }
1129
1130 ssl->s3->tmp.extended_master_secret = 1;
1131 return 1;
1132}
1133
1134static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1135 if (!ssl->s3->tmp.extended_master_secret) {
1136 return 1;
1137 }
1138
1139 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1140 !CBB_add_u16(out, 0 /* length */)) {
1141 return 0;
1142 }
1143
1144 return 1;
1145}
1146
Adam Langley9b05bc52015-07-01 15:25:33 -07001147
1148/* Session tickets.
1149 *
1150 * https://tools.ietf.org/html/rfc5077 */
1151
1152static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1153 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1154 return 1;
1155 }
1156
1157 const uint8_t *ticket_data = NULL;
1158 int ticket_len = 0;
1159
1160 /* Renegotiation does not participate in session resumption. However, still
1161 * advertise the extension to avoid potentially breaking servers which carry
1162 * over the state from the previous handshake, such as OpenSSL servers
1163 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1164 if (!ssl->s3->initial_handshake_complete &&
1165 ssl->session != NULL &&
1166 ssl->session->tlsext_tick != NULL) {
1167 ticket_data = ssl->session->tlsext_tick;
1168 ticket_len = ssl->session->tlsext_ticklen;
1169 }
1170
1171 CBB ticket;
1172 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1173 !CBB_add_u16_length_prefixed(out, &ticket) ||
1174 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1175 !CBB_flush(out)) {
1176 return 0;
1177 }
1178
1179 return 1;
1180}
1181
1182static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1183 CBS *contents) {
1184 ssl->tlsext_ticket_expected = 0;
1185
1186 if (contents == NULL) {
1187 return 1;
1188 }
1189
1190 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1191 * this function should never be called, even if the server tries to send the
1192 * extension. */
1193 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1194
1195 if (CBS_len(contents) != 0) {
1196 return 0;
1197 }
1198
1199 ssl->tlsext_ticket_expected = 1;
1200 return 1;
1201}
1202
1203static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1204 /* This function isn't used because the ticket extension from the client is
1205 * handled in ssl_sess.c. */
1206 return 1;
1207}
1208
1209static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1210 if (!ssl->tlsext_ticket_expected) {
1211 return 1;
1212 }
1213
1214 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1215 * true. */
1216 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1217
1218 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1219 !CBB_add_u16(out, 0 /* length */)) {
1220 return 0;
1221 }
1222
1223 return 1;
1224}
1225
1226
Adam Langley2e857bd2015-07-01 16:09:19 -07001227/* Signature Algorithms.
1228 *
1229 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1230
1231static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1232 if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1233 return 1;
1234 }
1235
1236 const uint8_t *sigalgs_data;
1237 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1238
1239 CBB contents, sigalgs;
1240 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1241 !CBB_add_u16_length_prefixed(out, &contents) ||
1242 !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1243 !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1244 !CBB_flush(out)) {
1245 return 0;
1246 }
1247
1248 return 1;
1249}
1250
1251static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1252 CBS *contents) {
1253 if (contents != NULL) {
1254 /* Servers MUST NOT send this extension. */
1255 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
David Benjamin3570d732015-06-29 00:28:17 -04001256 OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
Adam Langley2e857bd2015-07-01 16:09:19 -07001257 return 0;
1258 }
1259
1260 return 1;
1261}
1262
1263static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1264 CBS *contents) {
1265 OPENSSL_free(ssl->cert->peer_sigalgs);
1266 ssl->cert->peer_sigalgs = NULL;
1267 ssl->cert->peer_sigalgslen = 0;
1268
Adam Langley2e857bd2015-07-01 16:09:19 -07001269 if (contents == NULL) {
1270 return 1;
1271 }
1272
1273 CBS supported_signature_algorithms;
1274 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001275 CBS_len(contents) != 0 ||
1276 CBS_len(&supported_signature_algorithms) == 0 ||
1277 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001278 return 0;
1279 }
1280
1281 return 1;
1282}
1283
1284static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1285 /* Servers MUST NOT send this extension. */
1286 return 1;
1287}
1288
1289
Adam Langleybb0bd042015-07-01 16:21:03 -07001290/* OCSP Stapling.
1291 *
1292 * https://tools.ietf.org/html/rfc6066#section-8 */
1293
1294static void ext_ocsp_init(SSL *ssl) {
1295 ssl->s3->tmp.certificate_status_expected = 0;
1296}
1297
1298static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1299 if (!ssl->ocsp_stapling_enabled) {
1300 return 1;
1301 }
1302
1303 CBB contents;
1304 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1305 !CBB_add_u16_length_prefixed(out, &contents) ||
1306 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1307 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1308 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1309 !CBB_flush(out)) {
1310 return 0;
1311 }
1312
1313 return 1;
1314}
1315
1316static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001317 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001318 if (contents == NULL) {
1319 return 1;
1320 }
1321
1322 if (CBS_len(contents) != 0) {
1323 return 0;
1324 }
1325
1326 ssl->s3->tmp.certificate_status_expected = 1;
1327 return 1;
1328}
1329
1330static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1331 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001332 if (contents == NULL) {
1333 return 1;
1334 }
1335
1336 uint8_t status_type;
1337 if (!CBS_get_u8(contents, &status_type)) {
1338 return 0;
1339 }
1340
1341 /* We cannot decide whether OCSP stapling will occur yet because the correct
1342 * SSL_CTX might not have been selected. */
1343 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1344
Adam Langleybb0bd042015-07-01 16:21:03 -07001345 return 1;
1346}
1347
1348static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001349 /* The extension shouldn't be sent when resuming sessions. */
1350 if (ssl->hit ||
1351 !ssl->s3->tmp.ocsp_stapling_requested ||
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001352 ssl->ctx->ocsp_response_length == 0) {
1353 return 1;
1354 }
1355
1356 ssl->s3->tmp.certificate_status_expected = 1;
1357
1358 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1359 CBB_add_u16(out, 0 /* length */);
Adam Langleybb0bd042015-07-01 16:21:03 -07001360}
1361
1362
Adam Langley97dfcbf2015-07-01 18:35:20 -07001363/* Next protocol negotiation.
1364 *
1365 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1366
1367static void ext_npn_init(SSL *ssl) {
1368 ssl->s3->next_proto_neg_seen = 0;
1369}
1370
1371static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1372 if (ssl->s3->initial_handshake_complete ||
1373 ssl->ctx->next_proto_select_cb == NULL ||
1374 SSL_IS_DTLS(ssl)) {
1375 return 1;
1376 }
1377
1378 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1379 !CBB_add_u16(out, 0 /* length */)) {
1380 return 0;
1381 }
1382
1383 return 1;
1384}
1385
1386static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1387 CBS *contents) {
1388 if (contents == NULL) {
1389 return 1;
1390 }
1391
1392 /* If any of these are false then we should never have sent the NPN
1393 * extension in the ClientHello and thus this function should never have been
1394 * called. */
1395 assert(!ssl->s3->initial_handshake_complete);
1396 assert(!SSL_IS_DTLS(ssl));
1397 assert(ssl->ctx->next_proto_select_cb != NULL);
1398
David Benjamin76c2efc2015-08-31 14:24:29 -04001399 if (ssl->s3->alpn_selected != NULL) {
1400 /* NPN and ALPN may not be negotiated in the same connection. */
1401 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1402 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1403 return 0;
1404 }
1405
Adam Langley97dfcbf2015-07-01 18:35:20 -07001406 const uint8_t *const orig_contents = CBS_data(contents);
1407 const size_t orig_len = CBS_len(contents);
1408
1409 while (CBS_len(contents) != 0) {
1410 CBS proto;
1411 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1412 CBS_len(&proto) == 0) {
1413 return 0;
1414 }
1415 }
1416
1417 uint8_t *selected;
1418 uint8_t selected_len;
1419 if (ssl->ctx->next_proto_select_cb(
1420 ssl, &selected, &selected_len, orig_contents, orig_len,
1421 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1422 *out_alert = SSL_AD_INTERNAL_ERROR;
1423 return 0;
1424 }
1425
1426 OPENSSL_free(ssl->next_proto_negotiated);
1427 ssl->next_proto_negotiated = BUF_memdup(selected, selected_len);
1428 if (ssl->next_proto_negotiated == NULL) {
1429 *out_alert = SSL_AD_INTERNAL_ERROR;
1430 return 0;
1431 }
1432
1433 ssl->next_proto_negotiated_len = selected_len;
1434 ssl->s3->next_proto_neg_seen = 1;
1435
1436 return 1;
1437}
1438
1439static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1440 CBS *contents) {
1441 if (contents != NULL && CBS_len(contents) != 0) {
1442 return 0;
1443 }
1444
1445 if (contents == NULL ||
1446 ssl->s3->initial_handshake_complete ||
1447 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1448 * afterwards, parsing the ALPN extension will clear
1449 * |next_proto_neg_seen|. */
1450 ssl->s3->alpn_selected != NULL ||
1451 ssl->ctx->next_protos_advertised_cb == NULL ||
1452 SSL_IS_DTLS(ssl)) {
1453 return 1;
1454 }
1455
1456 ssl->s3->next_proto_neg_seen = 1;
1457 return 1;
1458}
1459
1460static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1461 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1462 * parsed. */
1463 if (!ssl->s3->next_proto_neg_seen) {
1464 return 1;
1465 }
1466
1467 const uint8_t *npa;
1468 unsigned npa_len;
1469
1470 if (ssl->ctx->next_protos_advertised_cb(
1471 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1472 SSL_TLSEXT_ERR_OK) {
1473 ssl->s3->next_proto_neg_seen = 0;
1474 return 1;
1475 }
1476
1477 CBB contents;
1478 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1479 !CBB_add_u16_length_prefixed(out, &contents) ||
1480 !CBB_add_bytes(&contents, npa, npa_len) ||
1481 !CBB_flush(out)) {
1482 return 0;
1483 }
1484
1485 return 1;
1486}
1487
1488
Adam Langleyab8d87d2015-07-10 12:21:39 -07001489/* Signed certificate timestamps.
1490 *
1491 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1492
1493static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1494 if (!ssl->signed_cert_timestamps_enabled) {
1495 return 1;
1496 }
1497
1498 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1499 !CBB_add_u16(out, 0 /* length */)) {
1500 return 0;
1501 }
1502
1503 return 1;
1504}
1505
1506static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1507 CBS *contents) {
1508 if (contents == NULL) {
1509 return 1;
1510 }
1511
1512 /* If this is false then we should never have sent the SCT extension in the
1513 * ClientHello and thus this function should never have been called. */
1514 assert(ssl->signed_cert_timestamps_enabled);
1515
1516 if (CBS_len(contents) == 0) {
1517 *out_alert = SSL_AD_DECODE_ERROR;
1518 return 0;
1519 }
1520
1521 /* Session resumption uses the original session information. */
1522 if (!ssl->hit &&
1523 !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1524 &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1525 *out_alert = SSL_AD_INTERNAL_ERROR;
1526 return 0;
1527 }
1528
1529 return 1;
1530}
1531
1532static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1533 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001534 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001535}
1536
1537static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001538 /* The extension shouldn't be sent when resuming sessions. */
1539 if (ssl->hit ||
1540 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001541 return 1;
1542 }
1543
1544 CBB contents;
1545 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1546 CBB_add_u16_length_prefixed(out, &contents) &&
1547 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1548 ssl->ctx->signed_cert_timestamp_list_length) &&
1549 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001550}
1551
1552
Adam Langleyf18e4532015-07-10 13:39:53 -07001553/* Application-level Protocol Negotiation.
1554 *
1555 * https://tools.ietf.org/html/rfc7301 */
1556
1557static void ext_alpn_init(SSL *ssl) {
1558 OPENSSL_free(ssl->s3->alpn_selected);
1559 ssl->s3->alpn_selected = NULL;
1560}
1561
1562static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1563 if (ssl->alpn_client_proto_list == NULL ||
1564 ssl->s3->initial_handshake_complete) {
1565 return 1;
1566 }
1567
1568 CBB contents, proto_list;
1569 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1570 !CBB_add_u16_length_prefixed(out, &contents) ||
1571 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1572 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1573 ssl->alpn_client_proto_list_len) ||
1574 !CBB_flush(out)) {
1575 return 0;
1576 }
1577
1578 return 1;
1579}
1580
1581static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1582 CBS *contents) {
1583 if (contents == NULL) {
1584 return 1;
1585 }
1586
1587 assert(!ssl->s3->initial_handshake_complete);
1588 assert(ssl->alpn_client_proto_list != NULL);
1589
David Benjamin76c2efc2015-08-31 14:24:29 -04001590 if (ssl->s3->next_proto_neg_seen) {
1591 /* NPN and ALPN may not be negotiated in the same connection. */
1592 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1593 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1594 return 0;
1595 }
1596
Adam Langleyf18e4532015-07-10 13:39:53 -07001597 /* The extension data consists of a ProtocolNameList which must have
1598 * exactly one ProtocolName. Each of these is length-prefixed. */
1599 CBS protocol_name_list, protocol_name;
1600 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1601 CBS_len(contents) != 0 ||
1602 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1603 /* Empty protocol names are forbidden. */
1604 CBS_len(&protocol_name) == 0 ||
1605 CBS_len(&protocol_name_list) != 0) {
1606 return 0;
1607 }
1608
1609 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1610 &ssl->s3->alpn_selected_len)) {
1611 *out_alert = SSL_AD_INTERNAL_ERROR;
1612 return 0;
1613 }
1614
1615 return 1;
1616}
1617
1618static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1619 CBS *contents) {
1620 if (contents == NULL) {
1621 return 1;
1622 }
1623
1624 if (ssl->ctx->alpn_select_cb == NULL ||
1625 ssl->s3->initial_handshake_complete) {
1626 return 1;
1627 }
1628
1629 /* ALPN takes precedence over NPN. */
1630 ssl->s3->next_proto_neg_seen = 0;
1631
1632 CBS protocol_name_list;
1633 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1634 CBS_len(contents) != 0 ||
1635 CBS_len(&protocol_name_list) < 2) {
1636 return 0;
1637 }
1638
1639 /* Validate the protocol list. */
1640 CBS protocol_name_list_copy = protocol_name_list;
1641 while (CBS_len(&protocol_name_list_copy) > 0) {
1642 CBS protocol_name;
1643
1644 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1645 /* Empty protocol names are forbidden. */
1646 CBS_len(&protocol_name) == 0) {
1647 return 0;
1648 }
1649 }
1650
1651 const uint8_t *selected;
1652 uint8_t selected_len;
1653 if (ssl->ctx->alpn_select_cb(
1654 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1655 CBS_len(&protocol_name_list),
1656 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1657 OPENSSL_free(ssl->s3->alpn_selected);
1658 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1659 if (ssl->s3->alpn_selected == NULL) {
1660 *out_alert = SSL_AD_INTERNAL_ERROR;
1661 return 0;
1662 }
1663 ssl->s3->alpn_selected_len = selected_len;
1664 }
1665
1666 return 1;
1667}
1668
1669static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1670 if (ssl->s3->alpn_selected == NULL) {
1671 return 1;
1672 }
1673
1674 CBB contents, proto_list, proto;
1675 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1676 !CBB_add_u16_length_prefixed(out, &contents) ||
1677 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1678 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
1679 !CBB_add_bytes(&proto, ssl->s3->alpn_selected, ssl->s3->alpn_selected_len) ||
1680 !CBB_flush(out)) {
1681 return 0;
1682 }
1683
1684 return 1;
1685}
1686
1687
Adam Langley49c7af12015-07-10 14:33:46 -07001688/* Channel ID.
1689 *
1690 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1691
1692static void ext_channel_id_init(SSL *ssl) {
1693 ssl->s3->tlsext_channel_id_valid = 0;
1694}
1695
1696static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1697 if (!ssl->tlsext_channel_id_enabled ||
1698 SSL_IS_DTLS(ssl)) {
1699 return 1;
1700 }
1701
1702 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1703 !CBB_add_u16(out, 0 /* length */)) {
1704 return 0;
1705 }
1706
1707 return 1;
1708}
1709
1710static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1711 CBS *contents) {
1712 if (contents == NULL) {
1713 return 1;
1714 }
1715
1716 assert(!SSL_IS_DTLS(ssl));
1717 assert(ssl->tlsext_channel_id_enabled);
1718
1719 if (CBS_len(contents) != 0) {
1720 return 0;
1721 }
1722
1723 ssl->s3->tlsext_channel_id_valid = 1;
1724 return 1;
1725}
1726
1727static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1728 CBS *contents) {
1729 if (contents == NULL ||
1730 !ssl->tlsext_channel_id_enabled ||
1731 SSL_IS_DTLS(ssl)) {
1732 return 1;
1733 }
1734
1735 if (CBS_len(contents) != 0) {
1736 return 0;
1737 }
1738
1739 ssl->s3->tlsext_channel_id_valid = 1;
1740 return 1;
1741}
1742
1743static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1744 if (!ssl->s3->tlsext_channel_id_valid) {
1745 return 1;
1746 }
1747
1748 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1749 !CBB_add_u16(out, 0 /* length */)) {
1750 return 0;
1751 }
1752
1753 return 1;
1754}
1755
Adam Langley391250d2015-07-15 19:06:07 -07001756
1757/* Secure Real-time Transport Protocol (SRTP) extension.
1758 *
1759 * https://tools.ietf.org/html/rfc5764 */
1760
Adam Langley391250d2015-07-15 19:06:07 -07001761
1762static void ext_srtp_init(SSL *ssl) {
1763 ssl->srtp_profile = NULL;
1764}
1765
1766static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1767 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1768 if (profiles == NULL) {
1769 return 1;
1770 }
1771 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1772 if (num_profiles == 0) {
1773 return 1;
1774 }
1775
1776 CBB contents, profile_ids;
1777 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1778 !CBB_add_u16_length_prefixed(out, &contents) ||
1779 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1780 return 0;
1781 }
1782
1783 size_t i;
1784 for (i = 0; i < num_profiles; i++) {
1785 if (!CBB_add_u16(&profile_ids,
1786 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1787 return 0;
1788 }
1789 }
1790
1791 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1792 !CBB_flush(out)) {
1793 return 0;
1794 }
1795
1796 return 1;
1797}
1798
1799static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1800 CBS *contents) {
1801 if (contents == NULL) {
1802 return 1;
1803 }
1804
1805 /* The extension consists of a u16-prefixed profile ID list containing a
1806 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1807 *
1808 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1809 CBS profile_ids, srtp_mki;
1810 uint16_t profile_id;
1811 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1812 !CBS_get_u16(&profile_ids, &profile_id) ||
1813 CBS_len(&profile_ids) != 0 ||
1814 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1815 CBS_len(contents) != 0) {
1816 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1817 return 0;
1818 }
1819
1820 if (CBS_len(&srtp_mki) != 0) {
1821 /* Must be no MKI, since we never offer one. */
1822 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1823 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1824 return 0;
1825 }
1826
1827 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1828
1829 /* Check to see if the server gave us something we support (and presumably
1830 * offered). */
1831 size_t i;
1832 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1833 const SRTP_PROTECTION_PROFILE *profile =
1834 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1835
1836 if (profile->id == profile_id) {
1837 ssl->srtp_profile = profile;
1838 return 1;
1839 }
1840 }
1841
1842 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1843 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1844 return 0;
1845}
1846
1847static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1848 CBS *contents) {
1849 if (contents == NULL) {
1850 return 1;
1851 }
1852
1853 CBS profile_ids, srtp_mki;
1854 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1855 CBS_len(&profile_ids) < 2 ||
1856 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1857 CBS_len(contents) != 0) {
1858 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1859 return 0;
1860 }
1861 /* Discard the MKI value for now. */
1862
1863 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1864 SSL_get_srtp_profiles(ssl);
1865
1866 /* Pick the server's most preferred profile. */
1867 size_t i;
1868 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1869 const SRTP_PROTECTION_PROFILE *server_profile =
1870 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1871
1872 CBS profile_ids_tmp;
1873 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1874
1875 while (CBS_len(&profile_ids_tmp) > 0) {
1876 uint16_t profile_id;
1877 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1878 return 0;
1879 }
1880
1881 if (server_profile->id == profile_id) {
1882 ssl->srtp_profile = server_profile;
1883 return 1;
1884 }
1885 }
1886 }
1887
1888 return 1;
1889}
1890
1891static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1892 if (ssl->srtp_profile == NULL) {
1893 return 1;
1894 }
1895
1896 CBB contents, profile_ids;
1897 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1898 !CBB_add_u16_length_prefixed(out, &contents) ||
1899 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1900 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1901 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1902 !CBB_flush(out)) {
1903 return 0;
1904 }
1905
1906 return 1;
1907}
1908
Adam Langleybdd5d662015-07-20 16:19:08 -07001909
1910/* EC point formats.
1911 *
1912 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1913
1914static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1915 if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1916 return 0;
1917 }
1918
1919 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1920
1921 size_t i;
1922 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1923 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1924
1925 const uint32_t alg_k = cipher->algorithm_mkey;
1926 const uint32_t alg_a = cipher->algorithm_auth;
1927 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1928 return 1;
1929 }
1930 }
1931
1932 return 0;
1933}
1934
Adam Langleybdd5d662015-07-20 16:19:08 -07001935static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001936 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001937 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1938 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001939 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1940 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001941 !CBB_flush(out)) {
1942 return 0;
1943 }
1944
1945 return 1;
1946}
1947
1948static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1949 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1950 return 1;
1951 }
1952
1953 return ext_ec_point_add_extension(ssl, out);
1954}
1955
1956static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1957 CBS *contents) {
1958 if (contents == NULL) {
1959 return 1;
1960 }
1961
1962 CBS ec_point_format_list;
1963 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1964 CBS_len(contents) != 0) {
1965 return 0;
1966 }
1967
David Benjaminfc059942015-07-30 23:01:59 -04001968 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1969 * point format. */
1970 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1971 CBS_len(&ec_point_format_list)) == NULL) {
1972 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001973 return 0;
1974 }
1975
1976 return 1;
1977}
1978
1979static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1980 CBS *contents) {
1981 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1982}
1983
1984static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1985 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1986 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001987 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001988
1989 if (!using_ecc) {
1990 return 1;
1991 }
1992
1993 return ext_ec_point_add_extension(ssl, out);
1994}
1995
Adam Langley273d49c2015-07-20 16:38:52 -07001996
1997/* EC supported curves.
1998 *
1999 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
2000
2001static void ext_ec_curves_init(SSL *ssl) {
2002 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2003 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2004 ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
2005}
2006
2007static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
2008 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2009 return 1;
2010 }
2011
2012 CBB contents, curves_bytes;
2013 if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
2014 !CBB_add_u16_length_prefixed(out, &contents) ||
2015 !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
2016 return 0;
2017 }
2018
2019 const uint16_t *curves;
2020 size_t curves_len;
2021 tls1_get_curvelist(ssl, 0, &curves, &curves_len);
2022
2023 size_t i;
2024 for (i = 0; i < curves_len; i++) {
2025 if (!CBB_add_u16(&curves_bytes, curves[i])) {
2026 return 0;
2027 }
2028 }
2029
2030 return CBB_flush(out);
2031}
2032
2033static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2034 CBS *contents) {
2035 /* This extension is not expected to be echoed by servers and is ignored. */
2036 return 1;
2037}
2038
2039static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2040 CBS *contents) {
2041 if (contents == NULL) {
2042 return 1;
2043 }
2044
2045 CBS elliptic_curve_list;
2046 if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
2047 CBS_len(&elliptic_curve_list) == 0 ||
2048 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
2049 CBS_len(contents) != 0) {
2050 return 0;
2051 }
2052
2053 ssl->s3->tmp.peer_ellipticcurvelist =
2054 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
2055
2056 if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
2057 *out_alert = SSL_AD_INTERNAL_ERROR;
2058 return 0;
2059 }
2060
2061 const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
2062 size_t i;
2063 for (i = 0; i < num_curves; i++) {
2064 if (!CBS_get_u16(&elliptic_curve_list,
2065 &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
2066 goto err;
2067 }
2068 }
2069
2070 assert(CBS_len(&elliptic_curve_list) == 0);
2071 ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
2072
2073 return 1;
2074
2075err:
2076 OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2077 ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2078 *out_alert = SSL_AD_INTERNAL_ERROR;
2079 return 0;
2080}
2081
2082static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
2083 /* Servers don't echo this extension. */
2084 return 1;
2085}
2086
2087
Adam Langley614c66a2015-06-12 15:26:58 -07002088/* kExtensions contains all the supported extensions. */
2089static const struct tls_extension kExtensions[] = {
2090 {
Adam Langley5021b222015-06-12 18:27:58 -07002091 /* The renegotiation extension must always be at index zero because the
2092 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2093 * sent as an SCSV. */
2094 TLSEXT_TYPE_renegotiate,
2095 NULL,
2096 ext_ri_add_clienthello,
2097 ext_ri_parse_serverhello,
2098 ext_ri_parse_clienthello,
2099 ext_ri_add_serverhello,
2100 },
2101 {
Adam Langley614c66a2015-06-12 15:26:58 -07002102 TLSEXT_TYPE_server_name,
2103 ext_sni_init,
2104 ext_sni_add_clienthello,
2105 ext_sni_parse_serverhello,
2106 ext_sni_parse_clienthello,
2107 ext_sni_add_serverhello,
2108 },
Adam Langley0a056712015-07-01 15:03:33 -07002109 {
2110 TLSEXT_TYPE_extended_master_secret,
2111 ext_ems_init,
2112 ext_ems_add_clienthello,
2113 ext_ems_parse_serverhello,
2114 ext_ems_parse_clienthello,
2115 ext_ems_add_serverhello,
2116 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002117 {
2118 TLSEXT_TYPE_session_ticket,
2119 NULL,
2120 ext_ticket_add_clienthello,
2121 ext_ticket_parse_serverhello,
2122 ext_ticket_parse_clienthello,
2123 ext_ticket_add_serverhello,
2124 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002125 {
2126 TLSEXT_TYPE_signature_algorithms,
2127 NULL,
2128 ext_sigalgs_add_clienthello,
2129 ext_sigalgs_parse_serverhello,
2130 ext_sigalgs_parse_clienthello,
2131 ext_sigalgs_add_serverhello,
2132 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002133 {
2134 TLSEXT_TYPE_status_request,
2135 ext_ocsp_init,
2136 ext_ocsp_add_clienthello,
2137 ext_ocsp_parse_serverhello,
2138 ext_ocsp_parse_clienthello,
2139 ext_ocsp_add_serverhello,
2140 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002141 {
2142 TLSEXT_TYPE_next_proto_neg,
2143 ext_npn_init,
2144 ext_npn_add_clienthello,
2145 ext_npn_parse_serverhello,
2146 ext_npn_parse_clienthello,
2147 ext_npn_add_serverhello,
2148 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002149 {
2150 TLSEXT_TYPE_certificate_timestamp,
2151 NULL,
2152 ext_sct_add_clienthello,
2153 ext_sct_parse_serverhello,
2154 ext_sct_parse_clienthello,
2155 ext_sct_add_serverhello,
2156 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002157 {
2158 TLSEXT_TYPE_application_layer_protocol_negotiation,
2159 ext_alpn_init,
2160 ext_alpn_add_clienthello,
2161 ext_alpn_parse_serverhello,
2162 ext_alpn_parse_clienthello,
2163 ext_alpn_add_serverhello,
2164 },
Adam Langley49c7af12015-07-10 14:33:46 -07002165 {
2166 TLSEXT_TYPE_channel_id,
2167 ext_channel_id_init,
2168 ext_channel_id_add_clienthello,
2169 ext_channel_id_parse_serverhello,
2170 ext_channel_id_parse_clienthello,
2171 ext_channel_id_add_serverhello,
2172 },
Adam Langley391250d2015-07-15 19:06:07 -07002173 {
2174 TLSEXT_TYPE_srtp,
2175 ext_srtp_init,
2176 ext_srtp_add_clienthello,
2177 ext_srtp_parse_serverhello,
2178 ext_srtp_parse_clienthello,
2179 ext_srtp_add_serverhello,
2180 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002181 {
2182 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002183 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002184 ext_ec_point_add_clienthello,
2185 ext_ec_point_parse_serverhello,
2186 ext_ec_point_parse_clienthello,
2187 ext_ec_point_add_serverhello,
2188 },
Adam Langley273d49c2015-07-20 16:38:52 -07002189 {
2190 TLSEXT_TYPE_elliptic_curves,
2191 ext_ec_curves_init,
2192 ext_ec_curves_add_clienthello,
2193 ext_ec_curves_parse_serverhello,
2194 ext_ec_curves_parse_clienthello,
2195 ext_ec_curves_add_serverhello,
2196 },
Adam Langley614c66a2015-06-12 15:26:58 -07002197};
2198
2199#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2200
Adam Langley4cfa96b2015-07-01 11:56:55 -07002201OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2202 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002203 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002204OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2205 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2206 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002207 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002208
Adam Langley614c66a2015-06-12 15:26:58 -07002209static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2210 uint16_t value) {
2211 unsigned i;
2212 for (i = 0; i < kNumExtensions; i++) {
2213 if (kExtensions[i].value == value) {
2214 *out_index = i;
2215 return &kExtensions[i];
2216 }
2217 }
2218
2219 return NULL;
2220}
2221
Adam Langley09505632015-07-30 18:10:13 -07002222int SSL_extension_supported(unsigned extension_value) {
2223 uint32_t index;
2224 return extension_value == TLSEXT_TYPE_padding ||
2225 tls_extension_find(&index, extension_value) != NULL;
2226}
2227
Adam Langleyb0c235e2014-06-20 12:00:00 -07002228/* header_len is the length of the ClientHello header written so far, used to
2229 * compute padding. It does not include the record header. Pass 0 if no padding
2230 * is to be done. */
Adam Langley614c66a2015-06-12 15:26:58 -07002231uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *const buf,
2232 uint8_t *const limit, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002233 /* don't add extensions for SSLv3 unless doing secure renegotiation */
2234 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002235 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002236 }
Adam Langley95c29f32014-06-20 12:00:00 -07002237
Adam Langley33ad2b52015-07-20 17:43:53 -07002238 CBB cbb, extensions;
2239 CBB_zero(&cbb);
2240 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2241 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2242 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002243 }
Adam Langley95c29f32014-06-20 12:00:00 -07002244
Adam Langley614c66a2015-06-12 15:26:58 -07002245 s->s3->tmp.extensions.sent = 0;
Adam Langley09505632015-07-30 18:10:13 -07002246 s->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002247
Adam Langley614c66a2015-06-12 15:26:58 -07002248 size_t i;
2249 for (i = 0; i < kNumExtensions; i++) {
2250 if (kExtensions[i].init != NULL) {
2251 kExtensions[i].init(s);
2252 }
2253 }
Adam Langley95c29f32014-06-20 12:00:00 -07002254
Adam Langley614c66a2015-06-12 15:26:58 -07002255 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002256 const size_t len_before = CBB_len(&extensions);
2257 if (!kExtensions[i].add_clienthello(s, &extensions)) {
2258 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2259 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2260 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002261 }
Adam Langley95c29f32014-06-20 12:00:00 -07002262
Adam Langley33ad2b52015-07-20 17:43:53 -07002263 if (CBB_len(&extensions) != len_before) {
Adam Langley614c66a2015-06-12 15:26:58 -07002264 s->s3->tmp.extensions.sent |= (1u << i);
2265 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002266 }
Adam Langley75712922014-10-10 16:23:43 -07002267
Adam Langley09505632015-07-30 18:10:13 -07002268 if (!custom_ext_add_clienthello(s, &extensions)) {
2269 goto err;
2270 }
2271
Adam Langleyfcf25832014-12-18 17:42:32 -08002272 if (header_len > 0) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002273 header_len += CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002274 if (header_len > 0xff && header_len < 0x200) {
2275 /* Add padding to workaround bugs in F5 terminators. See
2276 * https://tools.ietf.org/html/draft-agl-tls-padding-03
2277 *
2278 * NB: because this code works out the length of all existing extensions
2279 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002280 size_t padding_len = 0x200 - header_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002281 /* Extensions take at least four bytes to encode. Always include least
2282 * one byte of data if including the extension. WebSphere Application
2283 * Server 7.0 is intolerant to the last extension being zero-length. */
2284 if (padding_len >= 4 + 1) {
2285 padding_len -= 4;
2286 } else {
2287 padding_len = 1;
2288 }
Adam Langley95c29f32014-06-20 12:00:00 -07002289
Adam Langley33ad2b52015-07-20 17:43:53 -07002290 uint8_t *padding_bytes;
2291 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2292 !CBB_add_u16(&extensions, padding_len) ||
2293 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2294 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002295 }
Adam Langley75712922014-10-10 16:23:43 -07002296
Adam Langley33ad2b52015-07-20 17:43:53 -07002297 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002298 }
2299 }
Adam Langley75712922014-10-10 16:23:43 -07002300
Adam Langley33ad2b52015-07-20 17:43:53 -07002301 if (!CBB_flush(&cbb)) {
2302 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002303 }
Adam Langley95c29f32014-06-20 12:00:00 -07002304
Adam Langley33ad2b52015-07-20 17:43:53 -07002305 uint8_t *ret = buf;
2306 const size_t cbb_len = CBB_len(&cbb);
2307 /* If only two bytes have been written then the extensions are actually empty
2308 * and those two bytes are the zero length. In that case, we don't bother
2309 * sending the extensions length. */
2310 if (cbb_len > 2) {
2311 ret += cbb_len;
2312 }
2313
2314 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002315 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002316
2317err:
2318 CBB_cleanup(&cbb);
2319 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2320 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002321}
Adam Langley95c29f32014-06-20 12:00:00 -07002322
Adam Langley614c66a2015-06-12 15:26:58 -07002323uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *const buf,
2324 uint8_t *const limit) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002325 /* don't add extensions for SSLv3, unless doing secure renegotiation */
2326 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002327 return buf;
Adam Langleyfcf25832014-12-18 17:42:32 -08002328 }
Adam Langley95c29f32014-06-20 12:00:00 -07002329
Adam Langley33ad2b52015-07-20 17:43:53 -07002330 CBB cbb, extensions;
2331 CBB_zero(&cbb);
2332 if (!CBB_init_fixed(&cbb, buf, limit - buf) ||
2333 !CBB_add_u16_length_prefixed(&cbb, &extensions)) {
2334 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002335 }
2336
2337 unsigned i;
2338 for (i = 0; i < kNumExtensions; i++) {
2339 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2340 /* Don't send extensions that were not received. */
2341 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002342 }
Adam Langley95c29f32014-06-20 12:00:00 -07002343
Adam Langley33ad2b52015-07-20 17:43:53 -07002344 if (!kExtensions[i].add_serverhello(s, &extensions)) {
2345 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2346 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2347 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002348 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002349 }
Adam Langley95c29f32014-06-20 12:00:00 -07002350
Adam Langley09505632015-07-30 18:10:13 -07002351 if (!custom_ext_add_serverhello(s, &extensions)) {
2352 goto err;
2353 }
2354
Adam Langley33ad2b52015-07-20 17:43:53 -07002355 if (!CBB_flush(&cbb)) {
2356 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002357 }
2358
Adam Langley33ad2b52015-07-20 17:43:53 -07002359 uint8_t *ret = buf;
2360 const size_t cbb_len = CBB_len(&cbb);
2361 /* If only two bytes have been written then the extensions are actually empty
2362 * and those two bytes are the zero length. In that case, we don't bother
2363 * sending the extensions length. */
2364 if (cbb_len > 2) {
2365 ret += cbb_len;
2366 }
2367
2368 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08002369 return ret;
Adam Langley33ad2b52015-07-20 17:43:53 -07002370
2371err:
2372 CBB_cleanup(&cbb);
2373 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2374 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08002375}
Adam Langley95c29f32014-06-20 12:00:00 -07002376
Adam Langleyfcf25832014-12-18 17:42:32 -08002377static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002378 size_t i;
2379 for (i = 0; i < kNumExtensions; i++) {
2380 if (kExtensions[i].init != NULL) {
2381 kExtensions[i].init(s);
2382 }
2383 }
2384
2385 s->s3->tmp.extensions.received = 0;
Adam Langley09505632015-07-30 18:10:13 -07002386 s->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002387 /* The renegotiation extension must always be at index zero because the
2388 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2389 * sent as an SCSV. */
2390 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002391
Adam Langleyfcf25832014-12-18 17:42:32 -08002392 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002393 if (CBS_len(cbs) != 0) {
2394 /* Decode the extensions block and check it is valid. */
2395 CBS extensions;
2396 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2397 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002398 *out_alert = SSL_AD_DECODE_ERROR;
2399 return 0;
2400 }
Adam Langley95c29f32014-06-20 12:00:00 -07002401
Adam Langley33ad2b52015-07-20 17:43:53 -07002402 while (CBS_len(&extensions) != 0) {
2403 uint16_t type;
2404 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002405
Adam Langley33ad2b52015-07-20 17:43:53 -07002406 /* Decode the next extension. */
2407 if (!CBS_get_u16(&extensions, &type) ||
2408 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2409 *out_alert = SSL_AD_DECODE_ERROR;
2410 return 0;
2411 }
2412
2413 unsigned ext_index;
2414 const struct tls_extension *const ext =
2415 tls_extension_find(&ext_index, type);
2416
2417 if (ext == NULL) {
Adam Langley09505632015-07-30 18:10:13 -07002418 if (!custom_ext_parse_clienthello(s, out_alert, type, &extension)) {
2419 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2420 return 0;
2421 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002422 continue;
2423 }
2424
Adam Langley614c66a2015-06-12 15:26:58 -07002425 s->s3->tmp.extensions.received |= (1u << ext_index);
2426 uint8_t alert = SSL_AD_DECODE_ERROR;
2427 if (!ext->parse_clienthello(s, &alert, &extension)) {
2428 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002429 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2430 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002431 return 0;
2432 }
Adam Langley614c66a2015-06-12 15:26:58 -07002433 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002434 }
Adam Langley75712922014-10-10 16:23:43 -07002435
Adam Langley614c66a2015-06-12 15:26:58 -07002436 for (i = 0; i < kNumExtensions; i++) {
2437 if (!(s->s3->tmp.extensions.received & (1u << i))) {
2438 /* Extension wasn't observed so call the callback with a NULL
2439 * parameter. */
2440 uint8_t alert = SSL_AD_DECODE_ERROR;
2441 if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002442 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2443 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002444 *out_alert = alert;
2445 return 0;
2446 }
2447 }
2448 }
2449
Adam Langleyfcf25832014-12-18 17:42:32 -08002450 return 1;
2451}
Adam Langley95c29f32014-06-20 12:00:00 -07002452
Adam Langleyfcf25832014-12-18 17:42:32 -08002453int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
2454 int alert = -1;
2455 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
2456 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2457 return 0;
2458 }
Adam Langley95c29f32014-06-20 12:00:00 -07002459
Adam Langleyfcf25832014-12-18 17:42:32 -08002460 if (ssl_check_clienthello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002461 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002462 return 0;
2463 }
Adam Langley95c29f32014-06-20 12:00:00 -07002464
Adam Langleyfcf25832014-12-18 17:42:32 -08002465 return 1;
2466}
Adam Langley95c29f32014-06-20 12:00:00 -07002467
Adam Langleyfcf25832014-12-18 17:42:32 -08002468static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002469 uint32_t received = 0;
Adam Langley4cfa96b2015-07-01 11:56:55 -07002470 assert(kNumExtensions <= sizeof(received) * 8);
Adam Langley614c66a2015-06-12 15:26:58 -07002471
Adam Langley33ad2b52015-07-20 17:43:53 -07002472 if (CBS_len(cbs) != 0) {
2473 /* Decode the extensions block and check it is valid. */
2474 CBS extensions;
2475 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2476 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002477 *out_alert = SSL_AD_DECODE_ERROR;
2478 return 0;
2479 }
Adam Langley95c29f32014-06-20 12:00:00 -07002480
Adam Langley614c66a2015-06-12 15:26:58 -07002481
Adam Langley33ad2b52015-07-20 17:43:53 -07002482 while (CBS_len(&extensions) != 0) {
2483 uint16_t type;
2484 CBS extension;
2485
2486 /* Decode the next extension. */
2487 if (!CBS_get_u16(&extensions, &type) ||
2488 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2489 *out_alert = SSL_AD_DECODE_ERROR;
2490 return 0;
2491 }
2492
2493 unsigned ext_index;
2494 const struct tls_extension *const ext =
2495 tls_extension_find(&ext_index, type);
2496
Adam Langley09505632015-07-30 18:10:13 -07002497 if (ext == NULL) {
2498 if (!custom_ext_parse_serverhello(s, out_alert, type, &extension)) {
2499 return 0;
2500 }
2501 continue;
2502 }
2503
2504 if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2505 /* If the extension was never sent then it is illegal. */
David Benjamin3570d732015-06-29 00:28:17 -04002506 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langley33ad2b52015-07-20 17:43:53 -07002507 ERR_add_error_dataf("extension :%u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002508 *out_alert = SSL_AD_DECODE_ERROR;
2509 return 0;
2510 }
David Benjamin03973092014-06-24 23:27:17 -04002511
Adam Langley614c66a2015-06-12 15:26:58 -07002512 received |= (1u << ext_index);
2513
2514 uint8_t alert = SSL_AD_DECODE_ERROR;
2515 if (!ext->parse_serverhello(s, &alert, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002516 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2517 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langley614c66a2015-06-12 15:26:58 -07002518 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002519 return 0;
2520 }
Adam Langley614c66a2015-06-12 15:26:58 -07002521 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002522 }
Adam Langley95c29f32014-06-20 12:00:00 -07002523
Adam Langley33ad2b52015-07-20 17:43:53 -07002524 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002525 for (i = 0; i < kNumExtensions; i++) {
2526 if (!(received & (1u << i))) {
2527 /* Extension wasn't observed so call the callback with a NULL
2528 * parameter. */
2529 uint8_t alert = SSL_AD_DECODE_ERROR;
2530 if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002531 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2532 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002533 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002534 return 0;
2535 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002536 }
2537 }
Adam Langley95c29f32014-06-20 12:00:00 -07002538
Adam Langleyfcf25832014-12-18 17:42:32 -08002539 return 1;
2540}
Adam Langley95c29f32014-06-20 12:00:00 -07002541
Adam Langleyfcf25832014-12-18 17:42:32 -08002542static int ssl_check_clienthello_tlsext(SSL *s) {
2543 int ret = SSL_TLSEXT_ERR_NOACK;
2544 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002545
Adam Langleyfcf25832014-12-18 17:42:32 -08002546 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2547 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07002548
Adam Langleyfcf25832014-12-18 17:42:32 -08002549 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2550 ret = s->ctx->tlsext_servername_callback(s, &al,
2551 s->ctx->tlsext_servername_arg);
2552 } else if (s->initial_ctx != NULL &&
2553 s->initial_ctx->tlsext_servername_callback != 0) {
2554 ret = s->initial_ctx->tlsext_servername_callback(
2555 s, &al, s->initial_ctx->tlsext_servername_arg);
2556 }
Adam Langley95c29f32014-06-20 12:00:00 -07002557
Adam Langleyfcf25832014-12-18 17:42:32 -08002558 switch (ret) {
2559 case SSL_TLSEXT_ERR_ALERT_FATAL:
2560 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2561 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002562
Adam Langleyfcf25832014-12-18 17:42:32 -08002563 case SSL_TLSEXT_ERR_ALERT_WARNING:
2564 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2565 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002566
Adam Langleyfcf25832014-12-18 17:42:32 -08002567 case SSL_TLSEXT_ERR_NOACK:
Adam Langley614c66a2015-06-12 15:26:58 -07002568 s->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002569 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002570
Adam Langleyfcf25832014-12-18 17:42:32 -08002571 default:
2572 return 1;
2573 }
2574}
Adam Langleyed8270a2014-09-02 13:52:56 -07002575
Adam Langleyfcf25832014-12-18 17:42:32 -08002576static int ssl_check_serverhello_tlsext(SSL *s) {
David Benjaminfc059942015-07-30 23:01:59 -04002577 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002578 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002579
Adam Langleyfcf25832014-12-18 17:42:32 -08002580 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2581 ret = s->ctx->tlsext_servername_callback(s, &al,
2582 s->ctx->tlsext_servername_arg);
2583 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002584 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002585 ret = s->initial_ctx->tlsext_servername_callback(
2586 s, &al, s->initial_ctx->tlsext_servername_arg);
2587 }
Adam Langley95c29f32014-06-20 12:00:00 -07002588
Adam Langleyfcf25832014-12-18 17:42:32 -08002589 switch (ret) {
2590 case SSL_TLSEXT_ERR_ALERT_FATAL:
2591 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2592 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002593
Adam Langleyfcf25832014-12-18 17:42:32 -08002594 case SSL_TLSEXT_ERR_ALERT_WARNING:
2595 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2596 return 1;
2597
2598 default:
2599 return 1;
2600 }
2601}
2602
2603int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2604 int alert = -1;
2605 if (s->version < SSL3_VERSION) {
2606 return 1;
2607 }
2608
2609 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2610 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2611 return 0;
2612 }
2613
2614 if (ssl_check_serverhello_tlsext(s) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002615 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002616 return 0;
2617 }
2618
2619 return 1;
2620}
Adam Langley95c29f32014-06-20 12:00:00 -07002621
David Benjamine3aa1d92015-06-16 15:34:50 -04002622int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2623 int *out_send_ticket, const uint8_t *ticket,
2624 size_t ticket_len, const uint8_t *session_id,
2625 size_t session_id_len) {
2626 int ret = 1; /* Most errors are non-fatal. */
2627 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2628 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002629
David Benjamine3aa1d92015-06-16 15:34:50 -04002630 HMAC_CTX hmac_ctx;
2631 HMAC_CTX_init(&hmac_ctx);
2632 EVP_CIPHER_CTX cipher_ctx;
2633 EVP_CIPHER_CTX_init(&cipher_ctx);
2634
2635 *out_send_ticket = 0;
2636 *out_session = NULL;
2637
2638 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2639 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002640 }
2641
David Benjamine3aa1d92015-06-16 15:34:50 -04002642 if (ticket_len == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002643 /* The client will accept a ticket but doesn't currently have one. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002644 *out_send_ticket = 1;
2645 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002646 }
2647
David Benjaminadcc3952015-04-26 13:07:57 -04002648 /* Ensure there is room for the key name and the largest IV
2649 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2650 * the maximum IV length should be well under the minimum size for the
2651 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002652 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2653 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002654 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002655 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002656
David Benjamine3aa1d92015-06-16 15:34:50 -04002657 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2658 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2659 (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2660 0 /* decrypt */);
2661 if (cb_ret < 0) {
2662 ret = 0;
2663 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002664 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002665 if (cb_ret == 0) {
2666 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002667 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002668 if (cb_ret == 2) {
2669 *out_send_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002670 }
2671 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002672 /* Check the key name matches. */
2673 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2674 SSL_TICKET_KEY_NAME_LEN) != 0) {
2675 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002676 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002677 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2678 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002679 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002680 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2681 ssl_ctx->tlsext_tick_aes_key, iv)) {
2682 ret = 0;
2683 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 }
2685 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002686 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002687
David Benjamine3aa1d92015-06-16 15:34:50 -04002688 /* Check the MAC at the end of the ticket. */
2689 uint8_t mac[EVP_MAX_MD_SIZE];
2690 size_t mac_len = HMAC_size(&hmac_ctx);
2691 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002692 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002693 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002694 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002695 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2696 HMAC_Final(&hmac_ctx, mac, NULL);
2697 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2698 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002699 }
2700
David Benjamine3aa1d92015-06-16 15:34:50 -04002701 /* Decrypt the session data. */
2702 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2703 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2704 mac_len;
2705 plaintext = OPENSSL_malloc(ciphertext_len);
2706 if (plaintext == NULL) {
2707 ret = 0;
2708 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002709 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002710 if (ciphertext_len >= INT_MAX) {
2711 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002712 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002713 int len1, len2;
2714 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2715 (int)ciphertext_len) ||
2716 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2717 ERR_clear_error(); /* Don't leave an error on the queue. */
2718 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002719 }
2720
David Benjamine3aa1d92015-06-16 15:34:50 -04002721 /* Decode the session. */
2722 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2723 if (session == NULL) {
2724 ERR_clear_error(); /* Don't leave an error on the queue. */
2725 goto done;
2726 }
2727
2728 /* Copy the client's session ID into the new session, to denote the ticket has
2729 * been accepted. */
2730 memcpy(session->session_id, session_id, session_id_len);
2731 session->session_id_length = session_id_len;
2732
2733 *out_session = session;
2734
2735done:
2736 OPENSSL_free(plaintext);
2737 HMAC_CTX_cleanup(&hmac_ctx);
2738 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2739 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002740}
Adam Langley95c29f32014-06-20 12:00:00 -07002741
2742/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002743typedef struct {
2744 int nid;
2745 int id;
2746} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002747
Adam Langleyfcf25832014-12-18 17:42:32 -08002748static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2749 {NID_sha1, TLSEXT_hash_sha1},
2750 {NID_sha224, TLSEXT_hash_sha224},
2751 {NID_sha256, TLSEXT_hash_sha256},
2752 {NID_sha384, TLSEXT_hash_sha384},
2753 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002754
Adam Langleyfcf25832014-12-18 17:42:32 -08002755static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2756 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002757
Adam Langleyfcf25832014-12-18 17:42:32 -08002758static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2759 size_t i;
2760 for (i = 0; i < tlen; i++) {
2761 if (table[i].nid == nid) {
2762 return table[i].id;
2763 }
2764 }
Adam Langley95c29f32014-06-20 12:00:00 -07002765
Adam Langleyfcf25832014-12-18 17:42:32 -08002766 return -1;
2767}
Adam Langley95c29f32014-06-20 12:00:00 -07002768
David Benjaminb4d65fd2015-05-29 17:11:21 -04002769int tls12_get_sigid(int pkey_type) {
2770 return tls12_find_id(pkey_type, tls12_sig,
2771 sizeof(tls12_sig) / sizeof(tls12_lookup));
2772}
2773
David Benjamind1d80782015-07-05 11:54:09 -04002774int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002775 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002776
Adam Langleyfcf25832014-12-18 17:42:32 -08002777 if (!md) {
2778 return 0;
2779 }
Adam Langley95c29f32014-06-20 12:00:00 -07002780
Adam Langleyfcf25832014-12-18 17:42:32 -08002781 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2782 sizeof(tls12_md) / sizeof(tls12_lookup));
2783 if (md_id == -1) {
2784 return 0;
2785 }
Adam Langley95c29f32014-06-20 12:00:00 -07002786
David Benjamind1d80782015-07-05 11:54:09 -04002787 sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08002788 if (sig_id == -1) {
2789 return 0;
2790 }
Adam Langley95c29f32014-06-20 12:00:00 -07002791
Adam Langleyfcf25832014-12-18 17:42:32 -08002792 p[0] = (uint8_t)md_id;
2793 p[1] = (uint8_t)sig_id;
2794 return 1;
2795}
2796
Adam Langleyfcf25832014-12-18 17:42:32 -08002797const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2798 switch (hash_alg) {
2799 case TLSEXT_hash_md5:
2800 return EVP_md5();
2801
2802 case TLSEXT_hash_sha1:
2803 return EVP_sha1();
2804
2805 case TLSEXT_hash_sha224:
2806 return EVP_sha224();
2807
2808 case TLSEXT_hash_sha256:
2809 return EVP_sha256();
2810
2811 case TLSEXT_hash_sha384:
2812 return EVP_sha384();
2813
2814 case TLSEXT_hash_sha512:
2815 return EVP_sha512();
2816
2817 default:
2818 return NULL;
2819 }
2820}
Adam Langley95c29f32014-06-20 12:00:00 -07002821
David Benjaminec2f27d2014-11-13 19:17:25 -05002822/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2823 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002824static int tls12_get_pkey_type(uint8_t sig_alg) {
2825 switch (sig_alg) {
2826 case TLSEXT_signature_rsa:
2827 return EVP_PKEY_RSA;
2828
2829 case TLSEXT_signature_ecdsa:
2830 return EVP_PKEY_EC;
2831
2832 default:
2833 return -1;
2834 }
2835}
Adam Langley95c29f32014-06-20 12:00:00 -07002836
Steven Valdez0d62f262015-09-04 12:41:04 -04002837OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2,
2838 sizeof_tls_sigalgs_is_not_two);
Adam Langleyfcf25832014-12-18 17:42:32 -08002839
Steven Valdez0d62f262015-09-04 12:41:04 -04002840int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002841 /* Extension ignored for inappropriate versions */
Steven Valdez0d62f262015-09-04 12:41:04 -04002842 if (!SSL_USE_SIGALGS(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002843 return 1;
2844 }
David Benjamincd996942014-07-20 16:23:51 -04002845
Steven Valdez0d62f262015-09-04 12:41:04 -04002846 CERT *const cert = ssl->cert;
2847 OPENSSL_free(cert->peer_sigalgs);
2848 cert->peer_sigalgs = NULL;
2849 cert->peer_sigalgslen = 0;
2850
2851 size_t num_sigalgs = CBS_len(in_sigalgs);
2852
2853 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002854 return 0;
2855 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002856 num_sigalgs /= 2;
2857
2858 /* supported_signature_algorithms in the certificate request is
2859 * allowed to be empty. */
2860 if (num_sigalgs == 0) {
2861 return 1;
2862 }
2863
2864 /* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two
2865 * (statically asserted above) and we just divided |num_sigalgs| by two. */
2866 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS));
2867 if (cert->peer_sigalgs == NULL) {
2868 return 0;
2869 }
2870 cert->peer_sigalgslen = num_sigalgs;
2871
2872 CBS sigalgs;
2873 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2874
2875 size_t i;
2876 for (i = 0; i < num_sigalgs; i++) {
2877 TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i];
2878 if (!CBS_get_u8(&sigalgs, &sigalg->rhash) ||
2879 !CBS_get_u8(&sigalgs, &sigalg->rsign)) {
2880 return 0;
2881 }
2882 }
Adam Langley95c29f32014-06-20 12:00:00 -07002883
Adam Langleyfcf25832014-12-18 17:42:32 -08002884 return 1;
2885}
David Benjaminec2f27d2014-11-13 19:17:25 -05002886
David Benjamind1d80782015-07-05 11:54:09 -04002887const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2888 CERT *cert = ssl->cert;
2889 int type = ssl_private_key_type(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -04002890 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002891
Steven Valdez0d62f262015-09-04 12:41:04 -04002892 static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
2893 NID_sha224, NID_sha1};
2894
2895 const int *digest_nids = kDefaultDigestList;
2896 size_t num_digest_nids =
2897 sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
2898 if (cert->digest_nids != NULL) {
2899 digest_nids = cert->digest_nids;
2900 num_digest_nids = cert->num_digest_nids;
2901 }
2902
2903 for (i = 0; i < num_digest_nids; i++) {
2904 const int digest_nid = digest_nids[i];
2905 for (j = 0; j < cert->peer_sigalgslen; j++) {
2906 const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash);
2907 if (md == NULL ||
2908 digest_nid != EVP_MD_type(md) ||
2909 tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) {
2910 continue;
2911 }
2912
2913 return md;
Adam Langleyfcf25832014-12-18 17:42:32 -08002914 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002915 }
Adam Langley95c29f32014-06-20 12:00:00 -07002916
Adam Langleyfcf25832014-12-18 17:42:32 -08002917 /* If no suitable digest may be found, default to SHA-1. */
2918 return EVP_sha1();
2919}
Adam Langley95c29f32014-06-20 12:00:00 -07002920
David Benjamind6a4ae92015-08-06 11:10:51 -04002921int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2922 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002923 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002924
2925 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002926 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2927 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002928 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002929
David Benjamind6a4ae92015-08-06 11:10:51 -04002930 static const char kClientIDMagic[] = "TLS Channel ID signature";
2931 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2932
2933 if (ssl->hit) {
2934 static const char kResumptionMagic[] = "Resumption";
2935 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2936 if (ssl->session->original_handshake_hash_len == 0) {
2937 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2938 goto err;
2939 }
2940 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2941 ssl->session->original_handshake_hash_len);
2942 }
2943
2944 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2945 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2946 sizeof(handshake_hash));
2947 if (handshake_hash_len < 0) {
2948 goto err;
2949 }
2950 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2951 unsigned len_u;
2952 EVP_DigestFinal_ex(&ctx, out, &len_u);
2953 *out_len = len_u;
2954
2955 ret = 1;
2956
2957err:
2958 EVP_MD_CTX_cleanup(&ctx);
2959 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002960}
Adam Langley1258b6a2014-06-20 12:00:00 -07002961
2962/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2963 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002964int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2965 int digest_len;
2966 /* This function should never be called for a resumed session because the
2967 * handshake hashes that we wish to record are for the original, full
2968 * handshake. */
2969 if (s->hit) {
2970 return -1;
2971 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002972
Adam Langleyfcf25832014-12-18 17:42:32 -08002973 digest_len =
2974 tls1_handshake_digest(s, s->session->original_handshake_hash,
2975 sizeof(s->session->original_handshake_hash));
2976 if (digest_len < 0) {
2977 return -1;
2978 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002979
Adam Langleyfcf25832014-12-18 17:42:32 -08002980 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002981
Adam Langleyfcf25832014-12-18 17:42:32 -08002982 return 1;
2983}