blob: 01d98755749b325211a8b633cc7c38d54d90c2fc [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
109#include <stdio.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400110#include <stdlib.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700111#include <assert.h>
112
David Benjamin03973092014-06-24 23:27:17 -0400113#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700114#include <openssl/evp.h>
115#include <openssl/hmac.h>
116#include <openssl/mem.h>
117#include <openssl/obj.h>
118#include <openssl/rand.h>
119
120#include "ssl_locl.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800121
122
123static int tls_decrypt_ticket(SSL *s, const uint8_t *tick, int ticklen,
124 const uint8_t *sess_id, int sesslen,
125 SSL_SESSION **psess);
David Benjamin6c7aed02014-08-27 16:42:38 -0400126static int ssl_check_clienthello_tlsext(SSL *s);
127static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700128
David Benjamin338fcaf2014-12-11 01:20:52 -0500129const SSL3_ENC_METHOD TLSv1_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800130 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -0500131 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800132 tls1_setup_key_block,
133 tls1_generate_master_secret,
134 tls1_change_cipher_state,
135 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800136 tls1_cert_verify_mac,
137 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
138 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
139 tls1_alert_code,
140 tls1_export_keying_material,
141 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800142};
Adam Langley95c29f32014-06-20 12:00:00 -0700143
David Benjamin338fcaf2014-12-11 01:20:52 -0500144const SSL3_ENC_METHOD TLSv1_1_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800145 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -0500146 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800147 tls1_setup_key_block,
148 tls1_generate_master_secret,
149 tls1_change_cipher_state,
150 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800151 tls1_cert_verify_mac,
152 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
153 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
154 tls1_alert_code,
155 tls1_export_keying_material,
156 SSL_ENC_FLAG_EXPLICIT_IV,
Adam Langleyfcf25832014-12-18 17:42:32 -0800157};
Adam Langley95c29f32014-06-20 12:00:00 -0700158
David Benjamin338fcaf2014-12-11 01:20:52 -0500159const SSL3_ENC_METHOD TLSv1_2_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800160 tls1_enc,
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) {
217 OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions,
218 ERR_R_MALLOC_FAILURE);
219 goto done;
220 }
David Benjamin35a7a442014-07-05 00:23:20 -0400221
Adam Langleyfcf25832014-12-18 17:42:32 -0800222 /* Second pass: gather the extension types. */
223 extensions = *cbs;
224 for (i = 0; i < num_extensions; i++) {
225 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400226
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
228 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
229 /* This should not happen. */
230 goto done;
231 }
232 }
233 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400234
Adam Langleyfcf25832014-12-18 17:42:32 -0800235 /* Sort the extensions and make sure there are no duplicates. */
236 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
237 for (i = 1; i < num_extensions; i++) {
238 if (extension_types[i - 1] == extension_types[i]) {
239 goto done;
240 }
241 }
David Benjamin35a7a442014-07-05 00:23:20 -0400242
Adam Langleyfcf25832014-12-18 17:42:32 -0800243 ret = 1;
244
David Benjamin35a7a442014-07-05 00:23:20 -0400245done:
David Benjamin6eb000d2015-02-11 01:17:41 -0500246 if (extension_types) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 OPENSSL_free(extension_types);
David Benjamin6eb000d2015-02-11 01:17:41 -0500248 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 return ret;
250}
David Benjamin35a7a442014-07-05 00:23:20 -0400251
Adam Langleyfcf25832014-12-18 17:42:32 -0800252char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
253 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400254
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 if (/* Skip client version. */
258 !CBS_skip(&client_hello, 2) ||
259 /* Skip client nonce. */
260 !CBS_skip(&client_hello, 32) ||
261 /* Extract session_id. */
262 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
263 return 0;
264 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 ctx->session_id = CBS_data(&session_id);
267 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700268
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 /* Skip past DTLS cookie */
270 if (SSL_IS_DTLS(ctx->ssl)) {
271 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700272
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
274 return 0;
275 }
276 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700277
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 /* Extract cipher_suites. */
279 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
280 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
281 return 0;
282 }
283 ctx->cipher_suites = CBS_data(&cipher_suites);
284 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700285
Adam Langleyfcf25832014-12-18 17:42:32 -0800286 /* Extract compression_methods. */
287 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
288 CBS_len(&compression_methods) < 1) {
289 return 0;
290 }
291 ctx->compression_methods = CBS_data(&compression_methods);
292 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700293
Adam Langleyfcf25832014-12-18 17:42:32 -0800294 /* If the ClientHello ends here then it's valid, but doesn't have any
295 * extensions. (E.g. SSLv3.) */
296 if (CBS_len(&client_hello) == 0) {
297 ctx->extensions = NULL;
298 ctx->extensions_len = 0;
299 return 1;
300 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700301
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 /* Extract extensions and check it is valid. */
303 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
304 !tls1_check_duplicate_extensions(&extensions) ||
305 CBS_len(&client_hello) != 0) {
306 return 0;
307 }
308 ctx->extensions = CBS_data(&extensions);
309 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700310
Adam Langleyfcf25832014-12-18 17:42:32 -0800311 return 1;
312}
Adam Langleydc9b1412014-06-20 12:00:00 -0700313
Adam Langleyfcf25832014-12-18 17:42:32 -0800314char SSL_early_callback_ctx_extension_get(
315 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
316 const uint8_t **out_data, size_t *out_len) {
317 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700318
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700320
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 while (CBS_len(&extensions) != 0) {
322 uint16_t type;
323 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400324
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 /* Decode the next extension. */
326 if (!CBS_get_u16(&extensions, &type) ||
327 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
328 return 0;
329 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700330
Adam Langleyfcf25832014-12-18 17:42:32 -0800331 if (type == extension_type) {
332 *out_data = CBS_data(&extension);
333 *out_len = CBS_len(&extension);
334 return 1;
335 }
336 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700337
Adam Langleyfcf25832014-12-18 17:42:32 -0800338 return 0;
339}
Adam Langley95c29f32014-06-20 12:00:00 -0700340
David Benjamin52e5bac2014-12-27 02:27:35 -0500341struct tls_curve {
342 uint16_t curve_id;
343 int nid;
344};
345
David Benjamin70bd80a2014-12-27 03:06:46 -0500346/* ECC curves from RFC4492. */
David Benjamin52e5bac2014-12-27 02:27:35 -0500347static const struct tls_curve tls_curves[] = {
348 {21, NID_secp224r1},
349 {23, NID_X9_62_prime256v1},
350 {24, NID_secp384r1},
351 {25, NID_secp521r1},
Adam Langleyfcf25832014-12-18 17:42:32 -0800352};
Adam Langley95c29f32014-06-20 12:00:00 -0700353
Adam Langleyfcf25832014-12-18 17:42:32 -0800354static const uint8_t ecformats_default[] = {
355 TLSEXT_ECPOINTFORMAT_uncompressed,
356};
Adam Langley95c29f32014-06-20 12:00:00 -0700357
Adam Langleyfcf25832014-12-18 17:42:32 -0800358static const uint16_t eccurves_default[] = {
David Benjamin52e5bac2014-12-27 02:27:35 -0500359 23, /* X9_64_prime256v1 */
360 24, /* secp384r1 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800361};
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Adam Langleyfcf25832014-12-18 17:42:32 -0800363int tls1_ec_curve_id2nid(uint16_t curve_id) {
David Benjamin52e5bac2014-12-27 02:27:35 -0500364 size_t i;
365 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
366 if (curve_id == tls_curves[i].curve_id) {
367 return tls_curves[i].nid;
368 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 }
David Benjamin52e5bac2014-12-27 02:27:35 -0500370 return NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800371}
Adam Langley95c29f32014-06-20 12:00:00 -0700372
David Benjamin70bd80a2014-12-27 03:06:46 -0500373int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 size_t i;
David Benjamin52e5bac2014-12-27 02:27:35 -0500375 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
376 if (nid == tls_curves[i].nid) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500377 *out_curve_id = tls_curves[i].curve_id;
378 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 }
380 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 return 0;
382}
383
384/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
385 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
386 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400387static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 const uint16_t **out_curve_ids,
389 size_t *out_curve_ids_len) {
390 if (get_peer_curves) {
391 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
392 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
393 return;
394 }
Adam Langley95c29f32014-06-20 12:00:00 -0700395
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 *out_curve_ids = s->tlsext_ellipticcurvelist;
397 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
398 if (!*out_curve_ids) {
399 *out_curve_ids = eccurves_default;
400 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
401 }
402}
David Benjamined439582014-07-14 19:13:02 -0400403
Adam Langleyfcf25832014-12-18 17:42:32 -0800404int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
405 uint8_t curve_type;
406 uint16_t curve_id;
407 const uint16_t *curves;
408 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400409
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 /* Only support named curves. */
411 if (!CBS_get_u8(cbs, &curve_type) ||
412 curve_type != NAMED_CURVE_TYPE ||
413 !CBS_get_u16(cbs, &curve_id)) {
414 return 0;
415 }
David Benjamined439582014-07-14 19:13:02 -0400416
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 tls1_get_curvelist(s, 0, &curves, &curves_len);
418 for (i = 0; i < curves_len; i++) {
419 if (curve_id == curves[i]) {
420 *out_curve_id = curve_id;
421 return 1;
422 }
423 }
Adam Langley95c29f32014-06-20 12:00:00 -0700424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 return 0;
426}
David Benjamin072334d2014-07-13 16:24:27 -0400427
Adam Langleyfcf25832014-12-18 17:42:32 -0800428int tls1_get_shared_curve(SSL *s) {
429 const uint16_t *pref, *supp;
430 size_t preflen, supplen, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400431
Adam Langleyfcf25832014-12-18 17:42:32 -0800432 /* Can't do anything on client side */
433 if (s->server == 0) {
434 return NID_undef;
435 }
436
437 /* Return first preference shared curve */
438 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &supp,
439 &supplen);
440 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref,
441 &preflen);
442
443 for (i = 0; i < preflen; i++) {
444 for (j = 0; j < supplen; j++) {
445 if (pref[i] == supp[j]) {
446 return tls1_ec_curve_id2nid(pref[i]);
447 }
448 }
449 }
450
451 return NID_undef;
452}
Adam Langley95c29f32014-06-20 12:00:00 -0700453
David Benjamin072334d2014-07-13 16:24:27 -0400454int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800455 const int *curves, size_t ncurves) {
456 uint16_t *curve_ids;
457 size_t i;
458
Adam Langleyfcf25832014-12-18 17:42:32 -0800459 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
460 if (curve_ids == NULL) {
461 return 0;
462 }
463
464 for (i = 0; i < ncurves; i++) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500465 if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800466 OPENSSL_free(curve_ids);
467 return 0;
468 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 }
470
471 if (*out_curve_ids) {
472 OPENSSL_free(*out_curve_ids);
473 }
474 *out_curve_ids = curve_ids;
475 *out_curve_ids_len = ncurves;
476
477 return 1;
478}
Adam Langley95c29f32014-06-20 12:00:00 -0700479
David Benjamin072334d2014-07-13 16:24:27 -0400480/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
481 * TLS curve ID and point format, respectively, for |ec|. It returns one on
482 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800483static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
484 uint8_t *out_comp_id, EC_KEY *ec) {
485 int nid;
486 uint16_t id;
487 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700488
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 if (ec == NULL) {
490 return 0;
491 }
Adam Langley95c29f32014-06-20 12:00:00 -0700492
Adam Langleyfcf25832014-12-18 17:42:32 -0800493 grp = EC_KEY_get0_group(ec);
494 if (grp == NULL) {
495 return 0;
496 }
David Benjamin072334d2014-07-13 16:24:27 -0400497
Adam Langleyfcf25832014-12-18 17:42:32 -0800498 /* Determine curve ID */
499 nid = EC_GROUP_get_curve_name(grp);
David Benjamin70bd80a2014-12-27 03:06:46 -0500500 if (!tls1_ec_nid2curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 return 0;
502 }
David Benjamin072334d2014-07-13 16:24:27 -0400503
Adam Langleyfcf25832014-12-18 17:42:32 -0800504 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
505 *out_curve_id = id;
506
507 if (out_comp_id) {
508 if (EC_KEY_get0_public_key(ec) == NULL) {
509 return 0;
510 }
511 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
512 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
513 } else {
514 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
515 }
516 }
517
518 return 1;
519}
David Benjamin072334d2014-07-13 16:24:27 -0400520
David Benjamin42e9a772014-09-02 23:18:44 -0400521/* tls1_check_point_format returns one if |comp_id| is consistent with the
522 * peer's point format preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800523static int tls1_check_point_format(SSL *s, uint8_t comp_id) {
524 uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
525 size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
526 size_t i;
David Benjamin42e9a772014-09-02 23:18:44 -0400527
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 /* If point formats extension present check it, otherwise everything is
529 * supported (see RFC4492). */
530 if (p == NULL) {
531 return 1;
532 }
David Benjamin42e9a772014-09-02 23:18:44 -0400533
Adam Langleyfcf25832014-12-18 17:42:32 -0800534 for (i = 0; i < plen; i++) {
535 if (comp_id == p[i]) {
536 return 1;
537 }
538 }
David Benjamin42e9a772014-09-02 23:18:44 -0400539
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 return 0;
541}
542
543/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
544 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400545 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800546static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
547 const uint16_t *curves;
548 size_t curves_len, i, j;
David Benjamin42e9a772014-09-02 23:18:44 -0400549
Adam Langleyfcf25832014-12-18 17:42:32 -0800550 /* Check against our list, then the peer's list. */
551 for (j = 0; j <= 1; j++) {
552 tls1_get_curvelist(s, j, &curves, &curves_len);
553 for (i = 0; i < curves_len; i++) {
554 if (curves[i] == curve_id) {
555 break;
556 }
557 }
Adam Langley95c29f32014-06-20 12:00:00 -0700558
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 if (i == curves_len) {
560 return 0;
561 }
Adam Langley95c29f32014-06-20 12:00:00 -0700562
Adam Langleyfcf25832014-12-18 17:42:32 -0800563 /* Servers do not present a preference list so, if we are a client, only
564 * check our list. */
565 if (!s->server) {
566 return 1;
567 }
568 }
David Benjamin033e5f42014-11-13 18:47:41 -0500569
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 return 1;
571}
David Benjamin033e5f42014-11-13 18:47:41 -0500572
Adam Langleyfcf25832014-12-18 17:42:32 -0800573static void tls1_get_formatlist(SSL *s, const uint8_t **pformats,
574 size_t *pformatslen) {
575 /* If we have a custom point format list use it otherwise use default */
576 if (s->tlsext_ecpointformatlist) {
577 *pformats = s->tlsext_ecpointformatlist;
578 *pformatslen = s->tlsext_ecpointformatlist_length;
579 } else {
580 *pformats = ecformats_default;
581 *pformatslen = sizeof(ecformats_default);
582 }
583}
584
585int tls1_check_ec_cert(SSL *s, X509 *x) {
586 int ret = 0;
587 EVP_PKEY *pkey = X509_get_pubkey(x);
588 uint16_t curve_id;
589 uint8_t comp_id;
590
591 if (!pkey ||
592 pkey->type != EVP_PKEY_EC ||
593 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
594 !tls1_check_curve_id(s, curve_id) ||
595 !tls1_check_point_format(s, comp_id)) {
596 goto done;
597 }
598
599 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500600
601done:
Adam Langleyfcf25832014-12-18 17:42:32 -0800602 if (pkey) {
603 EVP_PKEY_free(pkey);
604 }
605 return ret;
606}
David Benjamin42e9a772014-09-02 23:18:44 -0400607
Adam Langleyfcf25832014-12-18 17:42:32 -0800608int tls1_check_ec_tmp_key(SSL *s) {
609 uint16_t curve_id;
610 EC_KEY *ec = s->cert->ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -0700611
Adam Langleyfcf25832014-12-18 17:42:32 -0800612 if (s->cert->ecdh_tmp_auto) {
613 /* Need a shared curve */
614 return tls1_get_shared_curve(s) != NID_undef;
615 }
Adam Langley95c29f32014-06-20 12:00:00 -0700616
Adam Langleyfcf25832014-12-18 17:42:32 -0800617 if (!ec) {
618 if (s->cert->ecdh_tmp_cb) {
619 return 1;
620 }
621 return 0;
622 }
623
624 return tls1_curve_params_from_ec_key(&curve_id, NULL, ec) &&
625 tls1_check_curve_id(s, curve_id);
626}
Adam Langley95c29f32014-06-20 12:00:00 -0700627
628/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800629 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700630
Adam Langley95c29f32014-06-20 12:00:00 -0700631#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700632
Adam Langley95c29f32014-06-20 12:00:00 -0700633#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700634
Adam Langleyfcf25832014-12-18 17:42:32 -0800635#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700636
David Benjamincff64722014-08-19 19:54:46 -0400637static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800638 tlsext_sigalg(TLSEXT_hash_sha512)
639 tlsext_sigalg(TLSEXT_hash_sha384)
640 tlsext_sigalg(TLSEXT_hash_sha256)
641 tlsext_sigalg(TLSEXT_hash_sha224)
642 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700643};
David Benjamin05da6e12014-07-12 20:42:55 -0400644
Adam Langleyfcf25832014-12-18 17:42:32 -0800645size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
646 /* If server use client authentication sigalgs if not NULL */
647 if (s->server && s->cert->client_sigalgs) {
648 *psigs = s->cert->client_sigalgs;
649 return s->cert->client_sigalgslen;
650 } else if (s->cert->conf_sigalgs) {
651 *psigs = s->cert->conf_sigalgs;
652 return s->cert->conf_sigalgslen;
653 } else {
654 *psigs = tls12_sigalgs;
655 return sizeof(tls12_sigalgs);
656 }
657}
Adam Langley95c29f32014-06-20 12:00:00 -0700658
Adam Langleyfcf25832014-12-18 17:42:32 -0800659/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
660 * checks it is consistent with |s|'s sent supported signature algorithms and,
661 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
662 * returns 0 and writes an alert into |*out_alert|. */
663int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
664 CBS *cbs, EVP_PKEY *pkey) {
665 const uint8_t *sent_sigs;
666 size_t sent_sigslen, i;
667 int sigalg = tls12_get_sigid(pkey);
668 uint8_t hash, signature;
669
670 /* Should never happen */
671 if (sigalg == -1) {
672 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
673 *out_alert = SSL_AD_INTERNAL_ERROR;
674 return 0;
675 }
676
677 if (!CBS_get_u8(cbs, &hash) ||
678 !CBS_get_u8(cbs, &signature)) {
679 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
680 *out_alert = SSL_AD_DECODE_ERROR;
681 return 0;
682 }
683
684 /* Check key type is consistent with signature */
685 if (sigalg != signature) {
686 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
687 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
688 return 0;
689 }
690
691 if (pkey->type == EVP_PKEY_EC) {
692 uint16_t curve_id;
693 uint8_t comp_id;
694 /* Check compression and curve matches extensions */
695 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
696 *out_alert = SSL_AD_INTERNAL_ERROR;
697 return 0;
698 }
699
700 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
701 !tls1_check_point_format(s, comp_id))) {
702 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
703 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
704 return 0;
705 }
706 }
707
708 /* Check signature matches a type we sent */
709 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
710 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
711 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
712 break;
713 }
714 }
715
716 /* Allow fallback to SHA-1. */
717 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
718 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
719 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
720 return 0;
721 }
722
723 *out_md = tls12_get_hash(hash);
724 if (*out_md == NULL) {
725 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
726 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
727 return 0;
728 }
729
730 return 1;
731}
732
733/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
734 * supported or doesn't appear in supported signature algorithms. Unlike
735 * ssl_cipher_get_disabled this applies to a specific session and not global
736 * settings. */
737void ssl_set_client_disabled(SSL *s) {
738 CERT *c = s->cert;
739 const uint8_t *sigalgs;
740 size_t i, sigalgslen;
741 int have_rsa = 0, have_ecdsa = 0;
742 c->mask_a = 0;
743 c->mask_k = 0;
744
745 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
746 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
747 c->mask_ssl = SSL_TLSV1_2;
748 } else {
749 c->mask_ssl = 0;
750 }
751
752 /* Now go through all signature algorithms seeing if we support any for RSA,
753 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
754 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
755 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
756 switch (sigalgs[1]) {
757 case TLSEXT_signature_rsa:
758 have_rsa = 1;
759 break;
760
761 case TLSEXT_signature_ecdsa:
762 have_ecdsa = 1;
763 break;
764 }
765 }
766
767 /* Disable auth if we don't include any appropriate signature algorithms. */
768 if (!have_rsa) {
769 c->mask_a |= SSL_aRSA;
770 }
771 if (!have_ecdsa) {
772 c->mask_a |= SSL_aECDSA;
773 }
774
775 /* with PSK there must be client callback set */
776 if (!s->psk_client_callback) {
777 c->mask_a |= SSL_aPSK;
778 c->mask_k |= SSL_kPSK;
779 }
780}
Adam Langley95c29f32014-06-20 12:00:00 -0700781
Adam Langleyb0c235e2014-06-20 12:00:00 -0700782/* header_len is the length of the ClientHello header written so far, used to
783 * compute padding. It does not include the record header. Pass 0 if no padding
784 * is to be done. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800785uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit,
786 size_t header_len) {
787 int extdatalen = 0;
788 uint8_t *ret = buf;
789 uint8_t *orig = buf;
790 /* See if we support any ECC ciphersuites */
791 int using_ecc = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700792
Adam Langleyfcf25832014-12-18 17:42:32 -0800793 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
794 size_t i;
795 unsigned long alg_k, alg_a;
796 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700797
Adam Langleyfcf25832014-12-18 17:42:32 -0800798 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
799 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700800
Adam Langleyfcf25832014-12-18 17:42:32 -0800801 alg_k = c->algorithm_mkey;
802 alg_a = c->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -0400803 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800804 using_ecc = 1;
805 break;
806 }
807 }
808 }
Adam Langley95c29f32014-06-20 12:00:00 -0700809
Adam Langleyfcf25832014-12-18 17:42:32 -0800810 /* don't add extensions for SSLv3 unless doing secure renegotiation */
811 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
812 return orig;
813 }
Adam Langley95c29f32014-06-20 12:00:00 -0700814
Adam Langleyfcf25832014-12-18 17:42:32 -0800815 ret += 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700816
Adam Langleyfcf25832014-12-18 17:42:32 -0800817 if (ret >= limit) {
818 return NULL; /* should never occur. */
819 }
Adam Langley95c29f32014-06-20 12:00:00 -0700820
Adam Langleyfcf25832014-12-18 17:42:32 -0800821 if (s->tlsext_hostname != NULL) {
822 /* Add TLS extension servername to the Client Hello message */
823 unsigned long size_str;
824 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -0700825
Adam Langleyfcf25832014-12-18 17:42:32 -0800826 /* check for enough space.
827 4 for the servername type and entension length
828 2 for servernamelist length
829 1 for the hostname type
830 2 for hostname length
831 + hostname length */
Adam Langley95c29f32014-06-20 12:00:00 -0700832
Adam Langleyfcf25832014-12-18 17:42:32 -0800833 lenmax = limit - ret - 9;
834 size_str = strlen(s->tlsext_hostname);
835 if (lenmax < 0 || size_str > (unsigned long)lenmax) {
836 return NULL;
837 }
Adam Langley95c29f32014-06-20 12:00:00 -0700838
Adam Langleyfcf25832014-12-18 17:42:32 -0800839 /* extension type and length */
840 s2n(TLSEXT_TYPE_server_name, ret);
841 s2n(size_str + 5, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700842
Adam Langleyfcf25832014-12-18 17:42:32 -0800843 /* length of servername list */
844 s2n(size_str + 3, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700845
Adam Langleyfcf25832014-12-18 17:42:32 -0800846 /* hostname type, length and hostname */
847 *(ret++) = (uint8_t)TLSEXT_NAMETYPE_host_name;
848 s2n(size_str, ret);
849 memcpy(ret, s->tlsext_hostname, size_str);
850 ret += size_str;
851 }
Adam Langley75712922014-10-10 16:23:43 -0700852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 /* Add RI if renegotiating */
854 if (s->renegotiate) {
855 int el;
David Benjamind1681e62014-11-20 14:54:14 -0500856
Adam Langleyfcf25832014-12-18 17:42:32 -0800857 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
858 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
859 return NULL;
860 }
Adam Langley95c29f32014-06-20 12:00:00 -0700861
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 if ((limit - ret - 4 - el) < 0) {
863 return NULL;
864 }
Adam Langley95c29f32014-06-20 12:00:00 -0700865
Adam Langleyfcf25832014-12-18 17:42:32 -0800866 s2n(TLSEXT_TYPE_renegotiate, ret);
867 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700868
Adam Langleyfcf25832014-12-18 17:42:32 -0800869 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
870 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
871 return NULL;
872 }
Adam Langley95c29f32014-06-20 12:00:00 -0700873
Adam Langleyfcf25832014-12-18 17:42:32 -0800874 ret += el;
875 }
Adam Langley95c29f32014-06-20 12:00:00 -0700876
Adam Langleyfcf25832014-12-18 17:42:32 -0800877 /* Add extended master secret. */
878 if (s->version != SSL3_VERSION) {
879 if (limit - ret - 4 < 0) {
880 return NULL;
881 }
882 s2n(TLSEXT_TYPE_extended_master_secret, ret);
883 s2n(0, ret);
884 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200885
Adam Langleyfcf25832014-12-18 17:42:32 -0800886 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
887 int ticklen = 0;
888 if (!s->new_session && s->session && s->session->tlsext_tick) {
889 ticklen = s->session->tlsext_ticklen;
890 }
Adam Langley95c29f32014-06-20 12:00:00 -0700891
Adam Langleyfcf25832014-12-18 17:42:32 -0800892 /* Check for enough room 2 for extension type, 2 for len rest for
893 * ticket. */
894 if ((long)(limit - ret - 4 - ticklen) < 0) {
895 return NULL;
896 }
897 s2n(TLSEXT_TYPE_session_ticket, ret);
898 s2n(ticklen, ret);
899 if (ticklen) {
900 memcpy(ret, s->session->tlsext_tick, ticklen);
901 ret += ticklen;
902 }
903 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700904
David Benjamin6ae7f072015-01-26 10:22:13 -0500905 if (ssl3_version_from_wire(s, s->client_version) >= TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800906 size_t salglen;
907 const uint8_t *salg;
908 salglen = tls12_get_psigalgs(s, &salg);
909 if ((size_t)(limit - ret) < salglen + 6) {
910 return NULL;
911 }
912 s2n(TLSEXT_TYPE_signature_algorithms, ret);
913 s2n(salglen + 2, ret);
914 s2n(salglen, ret);
915 memcpy(ret, salg, salglen);
916 ret += salglen;
917 }
Adam Langley95c29f32014-06-20 12:00:00 -0700918
Adam Langleyfcf25832014-12-18 17:42:32 -0800919 if (s->ocsp_stapling_enabled) {
920 /* The status_request extension is excessively extensible at every layer.
921 * On the client, only support requesting OCSP responses with an empty
922 * responder_id_list and no extensions. */
923 if (limit - ret - 4 - 1 - 2 - 2 < 0) {
924 return NULL;
925 }
Adam Langley95c29f32014-06-20 12:00:00 -0700926
Adam Langleyfcf25832014-12-18 17:42:32 -0800927 s2n(TLSEXT_TYPE_status_request, ret);
928 s2n(1 + 2 + 2, ret);
929 /* status_type */
930 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
931 /* responder_id_list - empty */
932 s2n(0, ret);
933 /* request_extensions - empty */
934 s2n(0, ret);
935 }
Adam Langley95c29f32014-06-20 12:00:00 -0700936
David Benjamin78e69782014-12-19 04:52:17 -0500937 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len &&
938 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800939 /* The client advertises an emtpy extension to indicate its support for
940 * Next Protocol Negotiation */
941 if (limit - ret - 4 < 0) {
942 return NULL;
943 }
944 s2n(TLSEXT_TYPE_next_proto_neg, ret);
945 s2n(0, ret);
946 }
Adam Langley95c29f32014-06-20 12:00:00 -0700947
Adam Langleyfcf25832014-12-18 17:42:32 -0800948 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len) {
949 /* The client advertises an empty extension to indicate its support for
950 * certificate timestamps. */
951 if (limit - ret - 4 < 0) {
952 return NULL;
953 }
954 s2n(TLSEXT_TYPE_certificate_timestamp, ret);
955 s2n(0, ret);
956 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700957
Adam Langleyfcf25832014-12-18 17:42:32 -0800958 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
959 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) {
960 return NULL;
961 }
962 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
963 s2n(2 + s->alpn_client_proto_list_len, ret);
964 s2n(s->alpn_client_proto_list_len, ret);
965 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
966 ret += s->alpn_client_proto_list_len;
967 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700968
David Benjamin78e69782014-12-19 04:52:17 -0500969 if (s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800970 /* The client advertises an emtpy extension to indicate its support for
971 * Channel ID. */
972 if (limit - ret - 4 < 0) {
973 return NULL;
974 }
975 if (s->ctx->tlsext_channel_id_enabled_new) {
976 s2n(TLSEXT_TYPE_channel_id_new, ret);
977 } else {
978 s2n(TLSEXT_TYPE_channel_id, ret);
979 }
980 s2n(0, ret);
981 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700982
Adam Langleyfcf25832014-12-18 17:42:32 -0800983 if (SSL_get_srtp_profiles(s)) {
984 int el;
Adam Langleyc3174b72014-06-20 12:00:00 -0700985
Adam Langleyfcf25832014-12-18 17:42:32 -0800986 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
Adam Langleyc3174b72014-06-20 12:00:00 -0700987
Adam Langleyfcf25832014-12-18 17:42:32 -0800988 if ((limit - ret - 4 - el) < 0) {
989 return NULL;
990 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700991
Adam Langleyfcf25832014-12-18 17:42:32 -0800992 s2n(TLSEXT_TYPE_use_srtp, ret);
993 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700994
Adam Langleyfcf25832014-12-18 17:42:32 -0800995 if (!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
996 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
997 return NULL;
998 }
999 ret += el;
1000 }
Adam Langley95c29f32014-06-20 12:00:00 -07001001
Adam Langleyfcf25832014-12-18 17:42:32 -08001002 if (using_ecc) {
1003 /* Add TLS extension ECPointFormats to the ClientHello message */
1004 long lenmax;
1005 const uint8_t *formats;
1006 const uint16_t *curves;
1007 size_t formats_len, curves_len, i;
Adam Langley95c29f32014-06-20 12:00:00 -07001008
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001010
Adam Langleyfcf25832014-12-18 17:42:32 -08001011 lenmax = limit - ret - 5;
1012 if (lenmax < 0) {
1013 return NULL;
1014 }
1015 if (formats_len > (size_t)lenmax) {
1016 return NULL;
1017 }
1018 if (formats_len > 255) {
1019 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1020 return NULL;
1021 }
Adam Langley95c29f32014-06-20 12:00:00 -07001022
Adam Langleyfcf25832014-12-18 17:42:32 -08001023 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1024 s2n(formats_len + 1, ret);
1025 *(ret++) = (uint8_t)formats_len;
1026 memcpy(ret, formats, formats_len);
1027 ret += formats_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001028
Adam Langleyfcf25832014-12-18 17:42:32 -08001029 /* Add TLS extension EllipticCurves to the ClientHello message */
1030 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001031
Adam Langleyfcf25832014-12-18 17:42:32 -08001032 lenmax = limit - ret - 6;
1033 if (lenmax < 0) {
1034 return NULL;
1035 }
1036 if (curves_len * 2 > (size_t)lenmax) {
1037 return NULL;
1038 }
1039 if (curves_len * 2 > 65532) {
1040 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1041 return NULL;
1042 }
Adam Langley95c29f32014-06-20 12:00:00 -07001043
Adam Langleyfcf25832014-12-18 17:42:32 -08001044 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1045 s2n((curves_len * 2) + 2, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001046
Adam Langleyfcf25832014-12-18 17:42:32 -08001047 s2n(curves_len * 2, ret);
1048 for (i = 0; i < curves_len; i++) {
1049 s2n(curves[i], ret);
1050 }
1051 }
Adam Langley95c29f32014-06-20 12:00:00 -07001052
Adam Langleyfcf25832014-12-18 17:42:32 -08001053 if (header_len > 0) {
1054 size_t clienthello_minsize = 0;
1055 header_len += ret - orig;
1056 if (header_len > 0xff && header_len < 0x200) {
1057 /* Add padding to workaround bugs in F5 terminators. See
1058 * https://tools.ietf.org/html/draft-agl-tls-padding-03
1059 *
1060 * NB: because this code works out the length of all existing extensions
1061 * it MUST always appear last. */
1062 clienthello_minsize = 0x200;
1063 }
1064 if (s->fastradio_padding) {
1065 /* Pad the ClientHello record to 1024 bytes to fast forward the radio
1066 * into DCH (high data rate) state in 3G networks. Note that when
1067 * fastradio_padding is enabled, even if the header_len is less than 255
1068 * bytes, the padding will be applied regardless. This is slightly
1069 * different from the TLS padding extension suggested in
1070 * https://tools.ietf.org/html/draft-agl-tls-padding-03 */
1071 clienthello_minsize = 0x400;
1072 }
1073 if (header_len < clienthello_minsize) {
1074 size_t padding_len = clienthello_minsize - header_len;
1075 /* Extensions take at least four bytes to encode. Always include least
1076 * one byte of data if including the extension. WebSphere Application
1077 * Server 7.0 is intolerant to the last extension being zero-length. */
1078 if (padding_len >= 4 + 1) {
1079 padding_len -= 4;
1080 } else {
1081 padding_len = 1;
1082 }
Adam Langley95c29f32014-06-20 12:00:00 -07001083
Adam Langleyfcf25832014-12-18 17:42:32 -08001084 if (limit - ret - 4 - (long)padding_len < 0) {
1085 return NULL;
1086 }
Adam Langley75712922014-10-10 16:23:43 -07001087
Adam Langleyfcf25832014-12-18 17:42:32 -08001088 s2n(TLSEXT_TYPE_padding, ret);
1089 s2n(padding_len, ret);
1090 memset(ret, 0, padding_len);
1091 ret += padding_len;
1092 }
1093 }
Adam Langley75712922014-10-10 16:23:43 -07001094
Adam Langleyfcf25832014-12-18 17:42:32 -08001095 extdatalen = ret - orig - 2;
1096 if (extdatalen == 0) {
1097 return orig;
1098 }
Adam Langley95c29f32014-06-20 12:00:00 -07001099
Adam Langleyfcf25832014-12-18 17:42:32 -08001100 s2n(extdatalen, orig);
1101 return ret;
1102}
Adam Langley95c29f32014-06-20 12:00:00 -07001103
Adam Langleyfcf25832014-12-18 17:42:32 -08001104uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit) {
1105 int extdatalen = 0;
1106 uint8_t *orig = buf;
1107 uint8_t *ret = buf;
1108 int next_proto_neg_seen;
1109 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1110 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -04001111 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001113
Adam Langleyfcf25832014-12-18 17:42:32 -08001114 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1115 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
1116 return orig;
1117 }
Adam Langley95c29f32014-06-20 12:00:00 -07001118
Adam Langleyfcf25832014-12-18 17:42:32 -08001119 ret += 2;
1120 if (ret >= limit) {
1121 return NULL; /* should never happen. */
1122 }
Adam Langley95c29f32014-06-20 12:00:00 -07001123
Adam Langleyfcf25832014-12-18 17:42:32 -08001124 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL) {
1125 if ((long)(limit - ret - 4) < 0) {
1126 return NULL;
1127 }
Adam Langley95c29f32014-06-20 12:00:00 -07001128
Adam Langleyfcf25832014-12-18 17:42:32 -08001129 s2n(TLSEXT_TYPE_server_name, ret);
1130 s2n(0, ret);
1131 }
Adam Langley95c29f32014-06-20 12:00:00 -07001132
Adam Langleyfcf25832014-12-18 17:42:32 -08001133 if (s->s3->send_connection_binding) {
1134 int el;
Adam Langley95c29f32014-06-20 12:00:00 -07001135
Adam Langleyfcf25832014-12-18 17:42:32 -08001136 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1137 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1138 return NULL;
1139 }
Adam Langley95c29f32014-06-20 12:00:00 -07001140
Adam Langleyfcf25832014-12-18 17:42:32 -08001141 if ((limit - ret - 4 - el) < 0) {
1142 return NULL;
1143 }
Adam Langley95c29f32014-06-20 12:00:00 -07001144
Adam Langleyfcf25832014-12-18 17:42:32 -08001145 s2n(TLSEXT_TYPE_renegotiate, ret);
1146 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001147
Adam Langleyfcf25832014-12-18 17:42:32 -08001148 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1149 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1150 return NULL;
1151 }
Adam Langley95c29f32014-06-20 12:00:00 -07001152
Adam Langleyfcf25832014-12-18 17:42:32 -08001153 ret += el;
1154 }
Adam Langley95c29f32014-06-20 12:00:00 -07001155
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 if (s->s3->tmp.extended_master_secret) {
1157 if ((long)(limit - ret - 4) < 0) {
1158 return NULL;
1159 }
Adam Langley95c29f32014-06-20 12:00:00 -07001160
Adam Langleyfcf25832014-12-18 17:42:32 -08001161 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1162 s2n(0, ret);
1163 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001164
Adam Langleyfcf25832014-12-18 17:42:32 -08001165 if (using_ecc) {
1166 const uint8_t *plist;
1167 size_t plistlen;
1168 /* Add TLS extension ECPointFormats to the ServerHello message */
1169 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -07001170
Adam Langleyfcf25832014-12-18 17:42:32 -08001171 tls1_get_formatlist(s, &plist, &plistlen);
1172
1173 lenmax = limit - ret - 5;
1174 if (lenmax < 0) {
1175 return NULL;
1176 }
1177 if (plistlen > (size_t)lenmax) {
1178 return NULL;
1179 }
1180 if (plistlen > 255) {
1181 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1182 return NULL;
1183 }
1184
1185 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1186 s2n(plistlen + 1, ret);
1187 *(ret++) = (uint8_t)plistlen;
1188 memcpy(ret, plist, plistlen);
1189 ret += plistlen;
1190 }
1191 /* Currently the server should not respond with a SupportedCurves extension */
1192
1193 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1194 if ((long)(limit - ret - 4) < 0) {
1195 return NULL;
1196 }
1197 s2n(TLSEXT_TYPE_session_ticket, ret);
1198 s2n(0, ret);
1199 }
1200
1201 if (s->s3->tmp.certificate_status_expected) {
1202 if ((long)(limit - ret - 4) < 0) {
1203 return NULL;
1204 }
1205 s2n(TLSEXT_TYPE_status_request, ret);
1206 s2n(0, ret);
1207 }
1208
1209 if (s->srtp_profile) {
1210 int el;
1211
1212 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1213
1214 if ((limit - ret - 4 - el) < 0) {
1215 return NULL;
1216 }
1217
1218 s2n(TLSEXT_TYPE_use_srtp, ret);
1219 s2n(el, ret);
1220
1221 if (!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1222 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1223 return NULL;
1224 }
1225 ret += el;
1226 }
1227
1228 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1229 s->s3->next_proto_neg_seen = 0;
1230 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1231 const uint8_t *npa;
1232 unsigned int npalen;
1233 int r;
1234
1235 r = s->ctx->next_protos_advertised_cb(
1236 s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1237 if (r == SSL_TLSEXT_ERR_OK) {
1238 if ((long)(limit - ret - 4 - npalen) < 0) {
1239 return NULL;
1240 }
1241 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1242 s2n(npalen, ret);
1243 memcpy(ret, npa, npalen);
1244 ret += npalen;
1245 s->s3->next_proto_neg_seen = 1;
1246 }
1247 }
1248
1249 if (s->s3->alpn_selected) {
1250 const uint8_t *selected = s->s3->alpn_selected;
1251 size_t len = s->s3->alpn_selected_len;
1252
1253 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) {
1254 return NULL;
1255 }
1256 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1257 s2n(3 + len, ret);
1258 s2n(1 + len, ret);
1259 *ret++ = len;
1260 memcpy(ret, selected, len);
1261 ret += len;
1262 }
1263
1264 /* If the client advertised support for Channel ID, and we have it
1265 * enabled, then we want to echo it back. */
1266 if (s->s3->tlsext_channel_id_valid) {
1267 if (limit - ret - 4 < 0) {
1268 return NULL;
1269 }
1270 if (s->s3->tlsext_channel_id_new) {
1271 s2n(TLSEXT_TYPE_channel_id_new, ret);
1272 } else {
1273 s2n(TLSEXT_TYPE_channel_id, ret);
1274 }
1275 s2n(0, ret);
1276 }
1277
1278 extdatalen = ret - orig - 2;
1279 if (extdatalen == 0) {
1280 return orig;
1281 }
1282
1283 s2n(extdatalen, orig);
1284 return ret;
1285}
Adam Langley95c29f32014-06-20 12:00:00 -07001286
Adam Langley95c29f32014-06-20 12:00:00 -07001287/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1288 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001289 * cbs: the contents of the extension, not including the type and length.
1290 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001291 * return.
1292 *
David Benjamindc72ff72014-06-25 12:36:10 -04001293 * returns: 1 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001294static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert) {
1295 CBS protocol_name_list, protocol_name_list_copy;
1296 const uint8_t *selected;
1297 uint8_t selected_len;
1298 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07001299
Adam Langleyfcf25832014-12-18 17:42:32 -08001300 if (s->ctx->alpn_select_cb == NULL) {
1301 return 1;
1302 }
Adam Langley95c29f32014-06-20 12:00:00 -07001303
Adam Langleyfcf25832014-12-18 17:42:32 -08001304 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1305 CBS_len(cbs) != 0 || CBS_len(&protocol_name_list) < 2) {
1306 goto parse_error;
1307 }
Adam Langley95c29f32014-06-20 12:00:00 -07001308
Adam Langleyfcf25832014-12-18 17:42:32 -08001309 /* Validate the protocol list. */
1310 protocol_name_list_copy = protocol_name_list;
1311 while (CBS_len(&protocol_name_list_copy) > 0) {
1312 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001313
Adam Langleyfcf25832014-12-18 17:42:32 -08001314 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name)) {
1315 goto parse_error;
1316 }
1317 }
Adam Langley95c29f32014-06-20 12:00:00 -07001318
Adam Langleyfcf25832014-12-18 17:42:32 -08001319 r = s->ctx->alpn_select_cb(
1320 s, &selected, &selected_len, CBS_data(&protocol_name_list),
1321 CBS_len(&protocol_name_list), s->ctx->alpn_select_cb_arg);
1322 if (r == SSL_TLSEXT_ERR_OK) {
1323 if (s->s3->alpn_selected) {
1324 OPENSSL_free(s->s3->alpn_selected);
1325 }
1326 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
1327 if (!s->s3->alpn_selected) {
1328 *out_alert = SSL_AD_INTERNAL_ERROR;
1329 return 0;
1330 }
1331 s->s3->alpn_selected_len = selected_len;
1332 }
1333
1334 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001335
1336parse_error:
Adam Langleyfcf25832014-12-18 17:42:32 -08001337 *out_alert = SSL_AD_DECODE_ERROR;
1338 return 0;
1339}
Adam Langley95c29f32014-06-20 12:00:00 -07001340
Adam Langleyfcf25832014-12-18 17:42:32 -08001341static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1342 int renegotiate_seen = 0;
1343 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001344
Adam Langleyfcf25832014-12-18 17:42:32 -08001345 s->should_ack_sni = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05001346 s->srtp_profile = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001347 s->s3->next_proto_neg_seen = 0;
1348 s->s3->tmp.certificate_status_expected = 0;
1349 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001350
Adam Langleyfcf25832014-12-18 17:42:32 -08001351 if (s->s3->alpn_selected) {
1352 OPENSSL_free(s->s3->alpn_selected);
1353 s->s3->alpn_selected = NULL;
1354 }
Adam Langley95c29f32014-06-20 12:00:00 -07001355
Adam Langleyfcf25832014-12-18 17:42:32 -08001356 /* Clear any signature algorithms extension received */
1357 if (s->cert->peer_sigalgs) {
1358 OPENSSL_free(s->cert->peer_sigalgs);
1359 s->cert->peer_sigalgs = NULL;
David Benjamin61c0d4e2015-03-19 14:24:37 -04001360 s->cert->peer_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001361 }
Adam Langley95c29f32014-06-20 12:00:00 -07001362
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 /* Clear any shared signature algorithms */
1364 if (s->cert->shared_sigalgs) {
1365 OPENSSL_free(s->cert->shared_sigalgs);
1366 s->cert->shared_sigalgs = NULL;
David Benjamin61c0d4e2015-03-19 14:24:37 -04001367 s->cert->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001368 }
Adam Langley95c29f32014-06-20 12:00:00 -07001369
Adam Langleyfcf25832014-12-18 17:42:32 -08001370 /* Clear ECC extensions */
1371 if (s->s3->tmp.peer_ecpointformatlist != 0) {
1372 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1373 s->s3->tmp.peer_ecpointformatlist = NULL;
1374 s->s3->tmp.peer_ecpointformatlist_length = 0;
1375 }
David Benjamindc72ff72014-06-25 12:36:10 -04001376
Adam Langleyfcf25832014-12-18 17:42:32 -08001377 if (s->s3->tmp.peer_ellipticcurvelist != 0) {
1378 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1379 s->s3->tmp.peer_ellipticcurvelist = NULL;
1380 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1381 }
David Benjamindc72ff72014-06-25 12:36:10 -04001382
Adam Langleyfcf25832014-12-18 17:42:32 -08001383 /* There may be no extensions. */
1384 if (CBS_len(cbs) == 0) {
1385 goto ri_check;
1386 }
David Benjamindc72ff72014-06-25 12:36:10 -04001387
Adam Langleyfcf25832014-12-18 17:42:32 -08001388 /* Decode the extensions block and check it is valid. */
1389 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1390 !tls1_check_duplicate_extensions(&extensions)) {
1391 *out_alert = SSL_AD_DECODE_ERROR;
1392 return 0;
1393 }
David Benjamindc72ff72014-06-25 12:36:10 -04001394
Adam Langleyfcf25832014-12-18 17:42:32 -08001395 while (CBS_len(&extensions) != 0) {
1396 uint16_t type;
1397 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001398
Adam Langleyfcf25832014-12-18 17:42:32 -08001399 /* Decode the next extension. */
1400 if (!CBS_get_u16(&extensions, &type) ||
1401 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1402 *out_alert = SSL_AD_DECODE_ERROR;
1403 return 0;
1404 }
Adam Langley95c29f32014-06-20 12:00:00 -07001405
Adam Langleyfcf25832014-12-18 17:42:32 -08001406 if (s->tlsext_debug_cb) {
1407 s->tlsext_debug_cb(s, 0, type, (uint8_t *)CBS_data(&extension),
1408 CBS_len(&extension), s->tlsext_debug_arg);
1409 }
Adam Langley95c29f32014-06-20 12:00:00 -07001410
Adam Langleyfcf25832014-12-18 17:42:32 -08001411 /* The servername extension is treated as follows:
David Benjamindc72ff72014-06-25 12:36:10 -04001412
Adam Langleyfcf25832014-12-18 17:42:32 -08001413 - Only the hostname type is supported with a maximum length of 255.
1414 - The servername is rejected if too long or if it contains zeros, in
1415 which case an fatal alert is generated.
1416 - The servername field is maintained together with the session cache.
1417 - When a session is resumed, the servername call back invoked in order
1418 to allow the application to position itself to the right context.
1419 - The servername is acknowledged if it is new for a session or when
1420 it is identical to a previously used for the same session.
1421 Applications can control the behaviour. They can at any time
1422 set a 'desirable' servername for a new SSL object. This can be the
1423 case for example with HTTPS when a Host: header field is received and
1424 a renegotiation is requested. In this case, a possible servername
1425 presented in the new client hello is only acknowledged if it matches
1426 the value of the Host: field.
1427 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1428 if they provide for changing an explicit servername context for the
1429 session,
1430 i.e. when the session has been established with a servername extension.
1431 - On session reconnect, the servername extension may be absent. */
Adam Langley95c29f32014-06-20 12:00:00 -07001432
Adam Langleyfcf25832014-12-18 17:42:32 -08001433 if (type == TLSEXT_TYPE_server_name) {
1434 CBS server_name_list;
1435 char have_seen_host_name = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001436
Adam Langleyfcf25832014-12-18 17:42:32 -08001437 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1438 CBS_len(&server_name_list) < 1 || CBS_len(&extension) != 0) {
1439 *out_alert = SSL_AD_DECODE_ERROR;
1440 return 0;
1441 }
David Benjamindc72ff72014-06-25 12:36:10 -04001442
Adam Langleyfcf25832014-12-18 17:42:32 -08001443 /* Decode each ServerName in the extension. */
1444 while (CBS_len(&server_name_list) > 0) {
1445 uint8_t name_type;
1446 CBS host_name;
David Benjamindc72ff72014-06-25 12:36:10 -04001447
Adam Langleyfcf25832014-12-18 17:42:32 -08001448 /* Decode the NameType. */
1449 if (!CBS_get_u8(&server_name_list, &name_type)) {
1450 *out_alert = SSL_AD_DECODE_ERROR;
1451 return 0;
1452 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001453
Adam Langleyfcf25832014-12-18 17:42:32 -08001454 /* Only host_name is supported. */
1455 if (name_type != TLSEXT_NAMETYPE_host_name) {
1456 continue;
1457 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001458
Adam Langleyfcf25832014-12-18 17:42:32 -08001459 if (have_seen_host_name) {
1460 /* The ServerNameList MUST NOT contain more than one name of the same
1461 * name_type. */
1462 *out_alert = SSL_AD_DECODE_ERROR;
1463 return 0;
1464 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001465
Adam Langleyfcf25832014-12-18 17:42:32 -08001466 have_seen_host_name = 1;
Adam Langleyed8270a2014-09-02 13:52:56 -07001467
Adam Langleyfcf25832014-12-18 17:42:32 -08001468 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1469 CBS_len(&host_name) < 1) {
1470 *out_alert = SSL_AD_DECODE_ERROR;
1471 return 0;
1472 }
Adam Langley95c29f32014-06-20 12:00:00 -07001473
Adam Langleyfcf25832014-12-18 17:42:32 -08001474 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1475 CBS_contains_zero_byte(&host_name)) {
1476 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1477 return 0;
1478 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001479
Adam Langleyfcf25832014-12-18 17:42:32 -08001480 if (!s->hit) {
1481 assert(s->session->tlsext_hostname == NULL);
1482 if (s->session->tlsext_hostname) {
1483 /* This should be impossible. */
1484 *out_alert = SSL_AD_DECODE_ERROR;
1485 return 0;
1486 }
Adam Langley95c29f32014-06-20 12:00:00 -07001487
Adam Langleyfcf25832014-12-18 17:42:32 -08001488 /* Copy the hostname as a string. */
1489 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname)) {
1490 *out_alert = SSL_AD_INTERNAL_ERROR;
1491 return 0;
1492 }
Adam Langley95c29f32014-06-20 12:00:00 -07001493
Adam Langleyfcf25832014-12-18 17:42:32 -08001494 s->should_ack_sni = 1;
1495 }
1496 }
1497 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1498 CBS ec_point_format_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001499
Adam Langleyfcf25832014-12-18 17:42:32 -08001500 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1501 CBS_len(&extension) != 0) {
1502 *out_alert = SSL_AD_DECODE_ERROR;
1503 return 0;
1504 }
Adam Langley95c29f32014-06-20 12:00:00 -07001505
Adam Langleyfcf25832014-12-18 17:42:32 -08001506 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1507 &s->s3->tmp.peer_ecpointformatlist_length)) {
1508 *out_alert = SSL_AD_INTERNAL_ERROR;
1509 return 0;
1510 }
1511 } else if (type == TLSEXT_TYPE_elliptic_curves) {
1512 CBS elliptic_curve_list;
1513 size_t i, num_curves;
David Benjamindc72ff72014-06-25 12:36:10 -04001514
Adam Langleyfcf25832014-12-18 17:42:32 -08001515 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
1516 CBS_len(&elliptic_curve_list) == 0 ||
1517 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
1518 CBS_len(&extension) != 0) {
1519 *out_alert = SSL_AD_DECODE_ERROR;
1520 return 0;
1521 }
David Benjamindc72ff72014-06-25 12:36:10 -04001522
Adam Langleyfcf25832014-12-18 17:42:32 -08001523 if (s->s3->tmp.peer_ellipticcurvelist) {
1524 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1525 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1526 }
David Benjamindc72ff72014-06-25 12:36:10 -04001527
Adam Langleyfcf25832014-12-18 17:42:32 -08001528 s->s3->tmp.peer_ellipticcurvelist =
1529 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
David Benjamindc72ff72014-06-25 12:36:10 -04001530
Adam Langleyfcf25832014-12-18 17:42:32 -08001531 if (s->s3->tmp.peer_ellipticcurvelist == NULL) {
1532 *out_alert = SSL_AD_INTERNAL_ERROR;
1533 return 0;
1534 }
Adam Langley95c29f32014-06-20 12:00:00 -07001535
Adam Langleyfcf25832014-12-18 17:42:32 -08001536 num_curves = CBS_len(&elliptic_curve_list) / 2;
1537 for (i = 0; i < num_curves; i++) {
1538 if (!CBS_get_u16(&elliptic_curve_list,
1539 &s->s3->tmp.peer_ellipticcurvelist[i])) {
1540 *out_alert = SSL_AD_INTERNAL_ERROR;
1541 return 0;
1542 }
1543 }
David Benjamindc72ff72014-06-25 12:36:10 -04001544
Adam Langleyfcf25832014-12-18 17:42:32 -08001545 if (CBS_len(&elliptic_curve_list) != 0) {
1546 *out_alert = SSL_AD_INTERNAL_ERROR;
1547 return 0;
1548 }
Adam Langley95c29f32014-06-20 12:00:00 -07001549
Adam Langleyfcf25832014-12-18 17:42:32 -08001550 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
1551 } else if (type == TLSEXT_TYPE_renegotiate) {
1552 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert)) {
1553 return 0;
1554 }
1555 renegotiate_seen = 1;
1556 } else if (type == TLSEXT_TYPE_signature_algorithms) {
1557 CBS supported_signature_algorithms;
Adam Langley95c29f32014-06-20 12:00:00 -07001558
Adam Langleyfcf25832014-12-18 17:42:32 -08001559 if (!CBS_get_u16_length_prefixed(&extension,
1560 &supported_signature_algorithms) ||
1561 CBS_len(&extension) != 0) {
1562 *out_alert = SSL_AD_DECODE_ERROR;
1563 return 0;
1564 }
Adam Langley95c29f32014-06-20 12:00:00 -07001565
Adam Langleyfcf25832014-12-18 17:42:32 -08001566 /* Ensure the signature algorithms are non-empty. It contains a list of
1567 * SignatureAndHashAlgorithms which are two bytes each. */
1568 if (CBS_len(&supported_signature_algorithms) == 0 ||
1569 (CBS_len(&supported_signature_algorithms) % 2) != 0) {
1570 *out_alert = SSL_AD_DECODE_ERROR;
1571 return 0;
1572 }
David Benjamindc72ff72014-06-25 12:36:10 -04001573
Adam Langleyfcf25832014-12-18 17:42:32 -08001574 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1575 *out_alert = SSL_AD_DECODE_ERROR;
1576 return 0;
1577 }
1578 /* If sigalgs received and no shared algorithms fatal error. */
1579 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001580 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
Adam Langleyfcf25832014-12-18 17:42:32 -08001581 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
1582 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1583 return 0;
1584 }
1585 } else if (type == TLSEXT_TYPE_next_proto_neg &&
David Benjamin78e69782014-12-19 04:52:17 -05001586 s->s3->tmp.finish_md_len == 0 && s->s3->alpn_selected == NULL &&
1587 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001588 /* The extension must be empty. */
1589 if (CBS_len(&extension) != 0) {
1590 *out_alert = SSL_AD_DECODE_ERROR;
1591 return 0;
1592 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001593
Adam Langleyfcf25832014-12-18 17:42:32 -08001594 /* We shouldn't accept this extension on a renegotiation.
1595 *
1596 * s->new_session will be set on renegotiation, but we probably shouldn't
1597 * rely that it couldn't be set on the initial renegotation too in
1598 * certain cases (when there's some other reason to disallow resuming an
1599 * earlier session -- the current code won't be doing anything like that,
1600 * but this might change).
David Benjamindc72ff72014-06-25 12:36:10 -04001601
Adam Langleyfcf25832014-12-18 17:42:32 -08001602 * A valid sign that there's been a previous handshake in this connection
1603 * is if s->s3->tmp.finish_md_len > 0. (We are talking about a check
1604 * that will happen in the Hello protocol round, well before a new
1605 * Finished message could have been computed.) */
1606 s->s3->next_proto_neg_seen = 1;
1607 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1608 s->ctx->alpn_select_cb && s->s3->tmp.finish_md_len == 0) {
1609 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert)) {
1610 return 0;
1611 }
1612 /* ALPN takes precedence over NPN. */
1613 s->s3->next_proto_neg_seen = 0;
David Benjamin78e69782014-12-19 04:52:17 -05001614 } else if (type == TLSEXT_TYPE_channel_id && s->tlsext_channel_id_enabled &&
1615 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001616 /* The extension must be empty. */
1617 if (CBS_len(&extension) != 0) {
1618 *out_alert = SSL_AD_DECODE_ERROR;
1619 return 0;
1620 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001621
Adam Langleyfcf25832014-12-18 17:42:32 -08001622 s->s3->tlsext_channel_id_valid = 1;
1623 } else if (type == TLSEXT_TYPE_channel_id_new &&
David Benjamin78e69782014-12-19 04:52:17 -05001624 s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001625 /* The extension must be empty. */
1626 if (CBS_len(&extension) != 0) {
1627 *out_alert = SSL_AD_DECODE_ERROR;
1628 return 0;
1629 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001630
Adam Langleyfcf25832014-12-18 17:42:32 -08001631 s->s3->tlsext_channel_id_valid = 1;
1632 s->s3->tlsext_channel_id_new = 1;
1633 } else if (type == TLSEXT_TYPE_use_srtp) {
1634 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert)) {
1635 return 0;
1636 }
1637 } else if (type == TLSEXT_TYPE_extended_master_secret &&
1638 s->version != SSL3_VERSION) {
1639 if (CBS_len(&extension) != 0) {
1640 *out_alert = SSL_AD_DECODE_ERROR;
1641 return 0;
1642 }
Adam Langley75712922014-10-10 16:23:43 -07001643
Adam Langleyfcf25832014-12-18 17:42:32 -08001644 s->s3->tmp.extended_master_secret = 1;
1645 }
1646 }
Adam Langley75712922014-10-10 16:23:43 -07001647
Adam Langleyfcf25832014-12-18 17:42:32 -08001648ri_check:
1649 /* Need RI if renegotiating */
Adam Langley95c29f32014-06-20 12:00:00 -07001650
Adam Langleyfcf25832014-12-18 17:42:32 -08001651 if (!renegotiate_seen && s->renegotiate &&
1652 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1653 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1654 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
1655 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1656 return 0;
1657 }
Adam Langley95c29f32014-06-20 12:00:00 -07001658
Adam Langleyfcf25832014-12-18 17:42:32 -08001659 return 1;
1660}
Adam Langley95c29f32014-06-20 12:00:00 -07001661
Adam Langleyfcf25832014-12-18 17:42:32 -08001662int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
1663 int alert = -1;
1664 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
1665 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
1666 return 0;
1667 }
Adam Langley95c29f32014-06-20 12:00:00 -07001668
Adam Langleyfcf25832014-12-18 17:42:32 -08001669 if (ssl_check_clienthello_tlsext(s) <= 0) {
1670 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext,
1671 SSL_R_CLIENTHELLO_TLSEXT);
1672 return 0;
1673 }
Adam Langley95c29f32014-06-20 12:00:00 -07001674
Adam Langleyfcf25832014-12-18 17:42:32 -08001675 return 1;
1676}
Adam Langley95c29f32014-06-20 12:00:00 -07001677
Adam Langley95c29f32014-06-20 12:00:00 -07001678/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
Adam Langleyfcf25832014-12-18 17:42:32 -08001679 * elements of zero length are allowed and the set of elements must exactly
1680 * fill the length of the block. */
1681static char ssl_next_proto_validate(const CBS *cbs) {
1682 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001683
Adam Langleyfcf25832014-12-18 17:42:32 -08001684 while (CBS_len(&copy) != 0) {
1685 CBS proto;
1686 if (!CBS_get_u8_length_prefixed(&copy, &proto) || CBS_len(&proto) == 0) {
1687 return 0;
1688 }
1689 }
Adam Langley95c29f32014-06-20 12:00:00 -07001690
Adam Langleyfcf25832014-12-18 17:42:32 -08001691 return 1;
1692}
Adam Langley95c29f32014-06-20 12:00:00 -07001693
Adam Langleyfcf25832014-12-18 17:42:32 -08001694static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1695 int tlsext_servername = 0;
1696 int renegotiate_seen = 0;
1697 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001698
Adam Langleyfcf25832014-12-18 17:42:32 -08001699 /* TODO(davidben): Move all of these to some per-handshake state that gets
1700 * systematically reset on a new handshake; perhaps allocate it fresh each
1701 * time so it's not even kept around post-handshake. */
1702 s->s3->next_proto_neg_seen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001703 s->tlsext_ticket_expected = 0;
1704 s->s3->tmp.certificate_status_expected = 0;
1705 s->s3->tmp.extended_master_secret = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05001706 s->srtp_profile = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001707
Adam Langleyfcf25832014-12-18 17:42:32 -08001708 if (s->s3->alpn_selected) {
1709 OPENSSL_free(s->s3->alpn_selected);
1710 s->s3->alpn_selected = NULL;
1711 }
David Benjamina19fc252014-10-19 00:14:36 -04001712
Adam Langleyfcf25832014-12-18 17:42:32 -08001713 /* Clear ECC extensions */
1714 if (s->s3->tmp.peer_ecpointformatlist != 0) {
1715 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1716 s->s3->tmp.peer_ecpointformatlist = NULL;
1717 s->s3->tmp.peer_ecpointformatlist_length = 0;
1718 }
David Benjamin03973092014-06-24 23:27:17 -04001719
Adam Langleyfcf25832014-12-18 17:42:32 -08001720 /* There may be no extensions. */
1721 if (CBS_len(cbs) == 0) {
1722 goto ri_check;
1723 }
Adam Langley95c29f32014-06-20 12:00:00 -07001724
Adam Langleyfcf25832014-12-18 17:42:32 -08001725 /* Decode the extensions block and check it is valid. */
1726 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1727 !tls1_check_duplicate_extensions(&extensions)) {
1728 *out_alert = SSL_AD_DECODE_ERROR;
1729 return 0;
1730 }
Adam Langley95c29f32014-06-20 12:00:00 -07001731
Adam Langleyfcf25832014-12-18 17:42:32 -08001732 while (CBS_len(&extensions) != 0) {
1733 uint16_t type;
1734 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001735
Adam Langleyfcf25832014-12-18 17:42:32 -08001736 /* Decode the next extension. */
1737 if (!CBS_get_u16(&extensions, &type) ||
1738 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1739 *out_alert = SSL_AD_DECODE_ERROR;
1740 return 0;
1741 }
Adam Langley95c29f32014-06-20 12:00:00 -07001742
Adam Langleyfcf25832014-12-18 17:42:32 -08001743 if (s->tlsext_debug_cb) {
1744 s->tlsext_debug_cb(s, 1, type, (uint8_t *)CBS_data(&extension),
1745 CBS_len(&extension), s->tlsext_debug_arg);
1746 }
Adam Langley95c29f32014-06-20 12:00:00 -07001747
Adam Langleyfcf25832014-12-18 17:42:32 -08001748 if (type == TLSEXT_TYPE_server_name) {
1749 /* The extension must be empty. */
1750 if (CBS_len(&extension) != 0) {
1751 *out_alert = SSL_AD_DECODE_ERROR;
1752 return 0;
1753 }
David Benjamin03973092014-06-24 23:27:17 -04001754
Adam Langleyfcf25832014-12-18 17:42:32 -08001755 /* We must have sent it in ClientHello. */
1756 if (s->tlsext_hostname == NULL) {
1757 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1758 return 0;
1759 }
David Benjamin03973092014-06-24 23:27:17 -04001760
Adam Langleyfcf25832014-12-18 17:42:32 -08001761 tlsext_servername = 1;
1762 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1763 CBS ec_point_format_list;
David Benjamin03973092014-06-24 23:27:17 -04001764
Adam Langleyfcf25832014-12-18 17:42:32 -08001765 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1766 CBS_len(&extension) != 0) {
1767 *out_alert = SSL_AD_DECODE_ERROR;
1768 return 0;
1769 }
Adam Langley95c29f32014-06-20 12:00:00 -07001770
Adam Langleyfcf25832014-12-18 17:42:32 -08001771 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1772 &s->s3->tmp.peer_ecpointformatlist_length)) {
1773 *out_alert = SSL_AD_INTERNAL_ERROR;
1774 return 0;
1775 }
1776 } else if (type == TLSEXT_TYPE_session_ticket) {
1777 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0) {
1778 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1779 return 0;
1780 }
David Benjamin03973092014-06-24 23:27:17 -04001781
Adam Langleyfcf25832014-12-18 17:42:32 -08001782 s->tlsext_ticket_expected = 1;
1783 } else if (type == TLSEXT_TYPE_status_request) {
1784 /* The extension MUST be empty and may only sent if we've requested a
1785 * status request message. */
1786 if (CBS_len(&extension) != 0) {
1787 *out_alert = SSL_AD_DECODE_ERROR;
1788 return 0;
1789 }
David Benjamin03973092014-06-24 23:27:17 -04001790
Adam Langleyfcf25832014-12-18 17:42:32 -08001791 if (!s->ocsp_stapling_enabled) {
1792 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1793 return 0;
1794 }
Adam Langley95c29f32014-06-20 12:00:00 -07001795
Adam Langleyfcf25832014-12-18 17:42:32 -08001796 /* Set a flag to expect a CertificateStatus message */
1797 s->s3->tmp.certificate_status_expected = 1;
1798 } else if (type == TLSEXT_TYPE_next_proto_neg &&
David Benjamin78e69782014-12-19 04:52:17 -05001799 s->s3->tmp.finish_md_len == 0 &&
1800 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001801 uint8_t *selected;
1802 uint8_t selected_len;
David Benjamin03973092014-06-24 23:27:17 -04001803
Adam Langleyfcf25832014-12-18 17:42:32 -08001804 /* We must have requested it. */
1805 if (s->ctx->next_proto_select_cb == NULL) {
1806 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1807 return 0;
1808 }
David Benjamin03973092014-06-24 23:27:17 -04001809
Adam Langleyfcf25832014-12-18 17:42:32 -08001810 /* The data must be valid. */
1811 if (!ssl_next_proto_validate(&extension)) {
1812 *out_alert = SSL_AD_DECODE_ERROR;
1813 return 0;
1814 }
Adam Langley95c29f32014-06-20 12:00:00 -07001815
Adam Langleyfcf25832014-12-18 17:42:32 -08001816 if (s->ctx->next_proto_select_cb(
1817 s, &selected, &selected_len, CBS_data(&extension),
1818 CBS_len(&extension),
1819 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1820 *out_alert = SSL_AD_INTERNAL_ERROR;
1821 return 0;
1822 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001823
Adam Langleyfcf25832014-12-18 17:42:32 -08001824 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
1825 if (s->next_proto_negotiated == NULL) {
1826 *out_alert = SSL_AD_INTERNAL_ERROR;
1827 return 0;
1828 }
Adam Langley75712922014-10-10 16:23:43 -07001829
Adam Langleyfcf25832014-12-18 17:42:32 -08001830 s->next_proto_negotiated_len = selected_len;
1831 s->s3->next_proto_neg_seen = 1;
1832 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
1833 CBS protocol_name_list, protocol_name;
Adam Langley75712922014-10-10 16:23:43 -07001834
Adam Langleyfcf25832014-12-18 17:42:32 -08001835 /* We must have requested it. */
1836 if (s->alpn_client_proto_list == NULL) {
1837 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1838 return 0;
1839 }
Adam Langley95c29f32014-06-20 12:00:00 -07001840
Adam Langleyfcf25832014-12-18 17:42:32 -08001841 /* The extension data consists of a ProtocolNameList which must have
1842 * exactly one ProtocolName. Each of these is length-prefixed. */
1843 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
1844 CBS_len(&extension) != 0 ||
1845 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1846 CBS_len(&protocol_name_list) != 0) {
1847 *out_alert = SSL_AD_DECODE_ERROR;
1848 return 0;
1849 }
Adam Langley95c29f32014-06-20 12:00:00 -07001850
Adam Langleyfcf25832014-12-18 17:42:32 -08001851 if (!CBS_stow(&protocol_name, &s->s3->alpn_selected,
1852 &s->s3->alpn_selected_len)) {
1853 *out_alert = SSL_AD_INTERNAL_ERROR;
1854 return 0;
1855 }
David Benjamin78e69782014-12-19 04:52:17 -05001856 } else if (type == TLSEXT_TYPE_channel_id && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001857 if (CBS_len(&extension) != 0) {
1858 *out_alert = SSL_AD_DECODE_ERROR;
1859 return 0;
1860 }
Adam Langley95c29f32014-06-20 12:00:00 -07001861
Adam Langleyfcf25832014-12-18 17:42:32 -08001862 s->s3->tlsext_channel_id_valid = 1;
David Benjamin78e69782014-12-19 04:52:17 -05001863 } else if (type == TLSEXT_TYPE_channel_id_new && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001864 if (CBS_len(&extension) != 0) {
1865 *out_alert = SSL_AD_DECODE_ERROR;
1866 return 0;
1867 }
Adam Langley95c29f32014-06-20 12:00:00 -07001868
Adam Langleyfcf25832014-12-18 17:42:32 -08001869 s->s3->tlsext_channel_id_valid = 1;
1870 s->s3->tlsext_channel_id_new = 1;
1871 } else if (type == TLSEXT_TYPE_certificate_timestamp) {
1872 if (CBS_len(&extension) == 0) {
1873 *out_alert = SSL_AD_DECODE_ERROR;
1874 return 0;
1875 }
Adam Langley95c29f32014-06-20 12:00:00 -07001876
Adam Langleyfcf25832014-12-18 17:42:32 -08001877 /* Session resumption uses the original session information. */
1878 if (!s->hit &&
1879 !CBS_stow(&extension, &s->session->tlsext_signed_cert_timestamp_list,
1880 &s->session->tlsext_signed_cert_timestamp_list_length)) {
1881 *out_alert = SSL_AD_INTERNAL_ERROR;
1882 return 0;
1883 }
1884 } else if (type == TLSEXT_TYPE_renegotiate) {
1885 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert)) {
1886 return 0;
1887 }
Adam Langley95c29f32014-06-20 12:00:00 -07001888
Adam Langleyfcf25832014-12-18 17:42:32 -08001889 renegotiate_seen = 1;
1890 } else if (type == TLSEXT_TYPE_use_srtp) {
1891 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert)) {
1892 return 0;
1893 }
1894 } else if (type == TLSEXT_TYPE_extended_master_secret) {
1895 if (/* It is invalid for the server to select EMS and
1896 SSLv3. */
1897 s->version == SSL3_VERSION || CBS_len(&extension) != 0) {
1898 *out_alert = SSL_AD_DECODE_ERROR;
1899 return 0;
1900 }
Adam Langley95c29f32014-06-20 12:00:00 -07001901
Adam Langleyfcf25832014-12-18 17:42:32 -08001902 s->s3->tmp.extended_master_secret = 1;
1903 }
1904 }
Adam Langley95c29f32014-06-20 12:00:00 -07001905
Adam Langleyfcf25832014-12-18 17:42:32 -08001906 if (!s->hit && tlsext_servername == 1 && s->tlsext_hostname) {
1907 if (s->session->tlsext_hostname == NULL) {
1908 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
1909 if (!s->session->tlsext_hostname) {
1910 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1911 return 0;
1912 }
1913 } else {
1914 *out_alert = SSL_AD_DECODE_ERROR;
1915 return 0;
1916 }
1917 }
Adam Langley95c29f32014-06-20 12:00:00 -07001918
Adam Langleyfcf25832014-12-18 17:42:32 -08001919ri_check:
1920 /* Determine if we need to see RI. Strictly speaking if we want to avoid an
1921 * attack we should *always* see RI even on initial server hello because the
1922 * client doesn't see any renegotiation during an attack. However this would
1923 * mean we could not connect to any server which doesn't support RI so for
1924 * the immediate future tolerate RI absence on initial connect only. */
1925 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) &&
1926 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1927 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1928 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext,
1929 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1930 return 0;
1931 }
Adam Langley95c29f32014-06-20 12:00:00 -07001932
Adam Langleyfcf25832014-12-18 17:42:32 -08001933 return 1;
1934}
Adam Langley95c29f32014-06-20 12:00:00 -07001935
Adam Langleyfcf25832014-12-18 17:42:32 -08001936int ssl_prepare_clienthello_tlsext(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07001937
Adam Langleyfcf25832014-12-18 17:42:32 -08001938int ssl_prepare_serverhello_tlsext(SSL *s) { return 1; }
Adam Langleyed8270a2014-09-02 13:52:56 -07001939
Adam Langleyfcf25832014-12-18 17:42:32 -08001940static int ssl_check_clienthello_tlsext(SSL *s) {
1941 int ret = SSL_TLSEXT_ERR_NOACK;
1942 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07001943
Adam Langleyfcf25832014-12-18 17:42:32 -08001944 /* The handling of the ECPointFormats extension is done elsewhere, namely in
1945 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07001946
Adam Langleyfcf25832014-12-18 17:42:32 -08001947 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
1948 ret = s->ctx->tlsext_servername_callback(s, &al,
1949 s->ctx->tlsext_servername_arg);
1950 } else if (s->initial_ctx != NULL &&
1951 s->initial_ctx->tlsext_servername_callback != 0) {
1952 ret = s->initial_ctx->tlsext_servername_callback(
1953 s, &al, s->initial_ctx->tlsext_servername_arg);
1954 }
Adam Langley95c29f32014-06-20 12:00:00 -07001955
Adam Langleyfcf25832014-12-18 17:42:32 -08001956 switch (ret) {
1957 case SSL_TLSEXT_ERR_ALERT_FATAL:
1958 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1959 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001960
Adam Langleyfcf25832014-12-18 17:42:32 -08001961 case SSL_TLSEXT_ERR_ALERT_WARNING:
1962 ssl3_send_alert(s, SSL3_AL_WARNING, al);
1963 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001964
Adam Langleyfcf25832014-12-18 17:42:32 -08001965 case SSL_TLSEXT_ERR_NOACK:
1966 s->should_ack_sni = 0;
1967 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001968
Adam Langleyfcf25832014-12-18 17:42:32 -08001969 default:
1970 return 1;
1971 }
1972}
Adam Langleyed8270a2014-09-02 13:52:56 -07001973
Adam Langleyfcf25832014-12-18 17:42:32 -08001974static int ssl_check_serverhello_tlsext(SSL *s) {
1975 int ret = SSL_TLSEXT_ERR_NOACK;
1976 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07001977
Adam Langleyfcf25832014-12-18 17:42:32 -08001978 /* If we are client and using an elliptic curve cryptography cipher suite,
1979 * then if server returns an EC point formats lists extension it must contain
1980 * uncompressed. */
1981 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1982 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -04001983 if (((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) &&
Adam Langleyfcf25832014-12-18 17:42:32 -08001984 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed)) {
1985 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext,
1986 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
1987 return -1;
1988 }
1989 ret = SSL_TLSEXT_ERR_OK;
David Benjamin03973092014-06-24 23:27:17 -04001990
Adam Langleyfcf25832014-12-18 17:42:32 -08001991 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
1992 ret = s->ctx->tlsext_servername_callback(s, &al,
1993 s->ctx->tlsext_servername_arg);
1994 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04001995 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001996 ret = s->initial_ctx->tlsext_servername_callback(
1997 s, &al, s->initial_ctx->tlsext_servername_arg);
1998 }
Adam Langley95c29f32014-06-20 12:00:00 -07001999
Adam Langleyfcf25832014-12-18 17:42:32 -08002000 switch (ret) {
2001 case SSL_TLSEXT_ERR_ALERT_FATAL:
2002 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2003 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002004
Adam Langleyfcf25832014-12-18 17:42:32 -08002005 case SSL_TLSEXT_ERR_ALERT_WARNING:
2006 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2007 return 1;
2008
2009 default:
2010 return 1;
2011 }
2012}
2013
2014int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2015 int alert = -1;
2016 if (s->version < SSL3_VERSION) {
2017 return 1;
2018 }
2019
2020 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2021 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2022 return 0;
2023 }
2024
2025 if (ssl_check_serverhello_tlsext(s) <= 0) {
2026 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext,
2027 SSL_R_SERVERHELLO_TLSEXT);
2028 return 0;
2029 }
2030
2031 return 1;
2032}
Adam Langley95c29f32014-06-20 12:00:00 -07002033
2034/* Since the server cache lookup is done early on in the processing of the
2035 * ClientHello, and other operations depend on the result, we need to handle
2036 * any TLS session ticket extension at the same time.
2037 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002038 * ctx: contains the early callback context, which is the result of a
2039 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002040 * ret: (output) on return, if a ticket was decrypted, then this is set to
2041 * point to the resulting session.
2042 *
Adam Langley95c29f32014-06-20 12:00:00 -07002043 * Returns:
2044 * -1: fatal error, either from parsing or decrypting the ticket.
2045 * 0: no ticket was found (or was ignored, based on settings).
2046 * 1: a zero length extension was found, indicating that the client supports
2047 * session tickets but doesn't currently have one to offer.
David Benjamind1681e62014-11-20 14:54:14 -05002048 * 2: a ticket was offered but couldn't be decrypted because of a non-fatal
2049 * error.
Adam Langley95c29f32014-06-20 12:00:00 -07002050 * 3: a ticket was successfully decrypted and *ret was set.
2051 *
2052 * Side effects:
2053 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2054 * a new session ticket to the client because the client indicated support
David Benjamind1681e62014-11-20 14:54:14 -05002055 * but the client either doesn't have a session ticket or we couldn't use
2056 * the one it gave us, or if s->ctx->tlsext_ticket_key_cb asked to renew
2057 * the client's ticket. Otherwise, s->tlsext_ticket_expected is set to 0.
Adam Langley95c29f32014-06-20 12:00:00 -07002058 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002059int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002060 SSL_SESSION **ret) {
2061 *ret = NULL;
2062 s->tlsext_ticket_expected = 0;
2063 const uint8_t *data;
2064 size_t len;
2065 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002066
Adam Langleyfcf25832014-12-18 17:42:32 -08002067 /* If tickets disabled behave as if no ticket present to permit stateful
2068 * resumption. */
2069 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) ||
2070 (s->version <= SSL3_VERSION && !ctx->extensions) ||
2071 !SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_session_ticket,
2072 &data, &len)) {
2073 return 0;
2074 }
2075
2076 if (len == 0) {
2077 /* The client will accept a ticket but doesn't currently have one. */
2078 s->tlsext_ticket_expected = 1;
2079 return 1;
2080 }
2081
2082 r = tls_decrypt_ticket(s, data, len, ctx->session_id, ctx->session_id_len,
2083 ret);
2084 switch (r) {
2085 case 2: /* ticket couldn't be decrypted */
2086 s->tlsext_ticket_expected = 1;
2087 return 2;
2088
2089 case 3: /* ticket was decrypted */
2090 return r;
2091
2092 case 4: /* ticket decrypted but need to renew */
2093 s->tlsext_ticket_expected = 1;
2094 return 3;
2095
2096 default: /* fatal error */
2097 return -1;
2098 }
2099}
Adam Langley95c29f32014-06-20 12:00:00 -07002100
2101/* tls_decrypt_ticket attempts to decrypt a session ticket.
2102 *
2103 * etick: points to the body of the session ticket extension.
2104 * eticklen: the length of the session tickets extenion.
2105 * sess_id: points at the session ID.
2106 * sesslen: the length of the session ID.
2107 * psess: (output) on return, if a ticket was decrypted, then this is set to
2108 * point to the resulting session.
2109 *
2110 * Returns:
2111 * -1: fatal error, either from parsing or decrypting the ticket.
2112 * 2: the ticket couldn't be decrypted.
2113 * 3: a ticket was successfully decrypted and *psess was set.
Adam Langleyfcf25832014-12-18 17:42:32 -08002114 * 4: same as 3, but the ticket needs to be renewed. */
2115static int tls_decrypt_ticket(SSL *s, const uint8_t *etick, int eticklen,
2116 const uint8_t *sess_id, int sesslen,
2117 SSL_SESSION **psess) {
2118 SSL_SESSION *sess;
2119 uint8_t *sdec;
2120 const uint8_t *p;
2121 int slen, mlen, renew_ticket = 0;
2122 uint8_t tick_hmac[EVP_MAX_MD_SIZE];
2123 HMAC_CTX hctx;
2124 EVP_CIPHER_CTX ctx;
2125 SSL_CTX *tctx = s->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002126
Adam Langleyfcf25832014-12-18 17:42:32 -08002127 /* Need at least keyname + iv + some encrypted data */
2128 if (eticklen < 48) {
2129 return 2;
2130 }
2131
2132 /* Initialize session ticket encryption and HMAC contexts */
2133 HMAC_CTX_init(&hctx);
2134 EVP_CIPHER_CTX_init(&ctx);
2135 if (tctx->tlsext_ticket_key_cb) {
2136 uint8_t *nctick = (uint8_t *)etick;
2137 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, &ctx, &hctx, 0);
2138 if (rv < 0) {
2139 return -1;
2140 }
2141 if (rv == 0) {
2142 return 2;
2143 }
2144 if (rv == 2) {
2145 renew_ticket = 1;
2146 }
2147 } else {
2148 /* Check key name matches */
2149 if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) {
2150 return 2;
2151 }
2152 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2153 NULL) ||
2154 !EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2155 tctx->tlsext_tick_aes_key, etick + 16)) {
2156 HMAC_CTX_cleanup(&hctx);
2157 EVP_CIPHER_CTX_cleanup(&ctx);
2158 return -1;
2159 }
2160 }
2161
2162 /* Attempt to process session ticket, first conduct sanity and integrity
2163 * checks on ticket. */
2164 mlen = HMAC_size(&hctx);
2165 if (mlen < 0) {
2166 HMAC_CTX_cleanup(&hctx);
2167 EVP_CIPHER_CTX_cleanup(&ctx);
2168 return -1;
2169 }
2170 eticklen -= mlen;
2171 /* Check HMAC of encrypted ticket */
2172 HMAC_Update(&hctx, etick, eticklen);
2173 HMAC_Final(&hctx, tick_hmac, NULL);
2174 HMAC_CTX_cleanup(&hctx);
2175 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
2176 EVP_CIPHER_CTX_cleanup(&ctx);
2177 return 2;
2178 }
2179
2180 /* Attempt to decrypt session data */
2181 /* Move p after IV to start of encrypted ticket, update length */
2182 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2183 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2184 sdec = OPENSSL_malloc(eticklen);
2185 if (!sdec) {
2186 EVP_CIPHER_CTX_cleanup(&ctx);
2187 return -1;
2188 }
2189 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2190 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0) {
2191 EVP_CIPHER_CTX_cleanup(&ctx);
2192 OPENSSL_free(sdec);
2193 return 2;
2194 }
2195 slen += mlen;
2196 EVP_CIPHER_CTX_cleanup(&ctx);
2197 p = sdec;
2198
2199 sess = d2i_SSL_SESSION(NULL, &p, slen);
2200 OPENSSL_free(sdec);
2201 if (sess) {
2202 /* The session ID, if non-empty, is used by some clients to detect that the
2203 * ticket has been accepted. So we copy it to the session structure. If it
2204 * is empty set length to zero as required by standard. */
2205 if (sesslen) {
2206 memcpy(sess->session_id, sess_id, sesslen);
2207 }
2208 sess->session_id_length = sesslen;
2209 *psess = sess;
2210 if (renew_ticket) {
2211 return 4;
2212 }
2213 return 3;
2214 }
2215
2216 ERR_clear_error();
2217 /* For session parse failure, indicate that we need to send a new ticket. */
2218 return 2;
2219}
Adam Langley95c29f32014-06-20 12:00:00 -07002220
2221/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002222typedef struct {
2223 int nid;
2224 int id;
2225} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002226
Adam Langleyfcf25832014-12-18 17:42:32 -08002227static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2228 {NID_sha1, TLSEXT_hash_sha1},
2229 {NID_sha224, TLSEXT_hash_sha224},
2230 {NID_sha256, TLSEXT_hash_sha256},
2231 {NID_sha384, TLSEXT_hash_sha384},
2232 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002233
Adam Langleyfcf25832014-12-18 17:42:32 -08002234static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2235 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002236
Adam Langleyfcf25832014-12-18 17:42:32 -08002237static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2238 size_t i;
2239 for (i = 0; i < tlen; i++) {
2240 if (table[i].nid == nid) {
2241 return table[i].id;
2242 }
2243 }
Adam Langley95c29f32014-06-20 12:00:00 -07002244
Adam Langleyfcf25832014-12-18 17:42:32 -08002245 return -1;
2246}
Adam Langley95c29f32014-06-20 12:00:00 -07002247
Adam Langleyfcf25832014-12-18 17:42:32 -08002248static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) {
2249 size_t i;
2250 for (i = 0; i < tlen; i++) {
2251 if (table[i].id == id) {
2252 return table[i].nid;
2253 }
2254 }
Adam Langley95c29f32014-06-20 12:00:00 -07002255
Adam Langleyfcf25832014-12-18 17:42:32 -08002256 return NID_undef;
2257}
Adam Langley95c29f32014-06-20 12:00:00 -07002258
Adam Langleyfcf25832014-12-18 17:42:32 -08002259int tls12_get_sigandhash(uint8_t *p, const EVP_PKEY *pk, const EVP_MD *md) {
2260 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002261
Adam Langleyfcf25832014-12-18 17:42:32 -08002262 if (!md) {
2263 return 0;
2264 }
Adam Langley95c29f32014-06-20 12:00:00 -07002265
Adam Langleyfcf25832014-12-18 17:42:32 -08002266 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2267 sizeof(tls12_md) / sizeof(tls12_lookup));
2268 if (md_id == -1) {
2269 return 0;
2270 }
Adam Langley95c29f32014-06-20 12:00:00 -07002271
Adam Langleyfcf25832014-12-18 17:42:32 -08002272 sig_id = tls12_get_sigid(pk);
2273 if (sig_id == -1) {
2274 return 0;
2275 }
Adam Langley95c29f32014-06-20 12:00:00 -07002276
Adam Langleyfcf25832014-12-18 17:42:32 -08002277 p[0] = (uint8_t)md_id;
2278 p[1] = (uint8_t)sig_id;
2279 return 1;
2280}
2281
2282int tls12_get_sigid(const EVP_PKEY *pk) {
2283 return tls12_find_id(pk->type, tls12_sig,
2284 sizeof(tls12_sig) / sizeof(tls12_lookup));
2285}
2286
2287const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2288 switch (hash_alg) {
2289 case TLSEXT_hash_md5:
2290 return EVP_md5();
2291
2292 case TLSEXT_hash_sha1:
2293 return EVP_sha1();
2294
2295 case TLSEXT_hash_sha224:
2296 return EVP_sha224();
2297
2298 case TLSEXT_hash_sha256:
2299 return EVP_sha256();
2300
2301 case TLSEXT_hash_sha384:
2302 return EVP_sha384();
2303
2304 case TLSEXT_hash_sha512:
2305 return EVP_sha512();
2306
2307 default:
2308 return NULL;
2309 }
2310}
Adam Langley95c29f32014-06-20 12:00:00 -07002311
David Benjaminec2f27d2014-11-13 19:17:25 -05002312/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2313 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002314static int tls12_get_pkey_type(uint8_t sig_alg) {
2315 switch (sig_alg) {
2316 case TLSEXT_signature_rsa:
2317 return EVP_PKEY_RSA;
2318
2319 case TLSEXT_signature_ecdsa:
2320 return EVP_PKEY_EC;
2321
2322 default:
2323 return -1;
2324 }
2325}
Adam Langley95c29f32014-06-20 12:00:00 -07002326
2327/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2328static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
Adam Langleyfcf25832014-12-18 17:42:32 -08002329 int *psignhash_nid, const uint8_t *data) {
2330 int sign_nid = 0, hash_nid = 0;
2331 if (!phash_nid && !psign_nid && !psignhash_nid) {
2332 return;
2333 }
2334
2335 if (phash_nid || psignhash_nid) {
2336 hash_nid = tls12_find_nid(data[0], tls12_md,
2337 sizeof(tls12_md) / sizeof(tls12_lookup));
2338 if (phash_nid) {
2339 *phash_nid = hash_nid;
2340 }
2341 }
2342
2343 if (psign_nid || psignhash_nid) {
2344 sign_nid = tls12_find_nid(data[1], tls12_sig,
2345 sizeof(tls12_sig) / sizeof(tls12_lookup));
2346 if (psign_nid) {
2347 *psign_nid = sign_nid;
2348 }
2349 }
2350
2351 if (psignhash_nid) {
2352 if (sign_nid && hash_nid) {
2353 OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid);
2354 } else {
2355 *psignhash_nid = NID_undef;
2356 }
2357 }
2358}
2359
Adam Langley95c29f32014-06-20 12:00:00 -07002360/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002361static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2362 size_t preflen, const uint8_t *allow,
2363 size_t allowlen) {
2364 const uint8_t *ptmp, *atmp;
2365 size_t i, j, nmatch = 0;
2366
2367 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2368 /* Skip disabled hashes or signature algorithms */
2369 if (tls12_get_hash(ptmp[0]) == NULL ||
2370 tls12_get_pkey_type(ptmp[1]) == -1) {
2371 continue;
2372 }
2373
2374 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2375 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2376 nmatch++;
2377 if (shsig) {
2378 shsig->rhash = ptmp[0];
2379 shsig->rsign = ptmp[1];
2380 tls1_lookup_sigalg(&shsig->hash_nid, &shsig->sign_nid,
2381 &shsig->signandhash_nid, ptmp);
2382 shsig++;
2383 }
2384
2385 break;
2386 }
2387 }
2388 }
2389
2390 return nmatch;
2391}
Adam Langley95c29f32014-06-20 12:00:00 -07002392
2393/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002394static int tls1_set_shared_sigalgs(SSL *s) {
2395 const uint8_t *pref, *allow, *conf;
2396 size_t preflen, allowlen, conflen;
2397 size_t nmatch;
2398 TLS_SIGALGS *salgs = NULL;
2399 CERT *c = s->cert;
2400
2401 if (c->shared_sigalgs) {
2402 OPENSSL_free(c->shared_sigalgs);
2403 c->shared_sigalgs = NULL;
David Benjamin61c0d4e2015-03-19 14:24:37 -04002404 c->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002405 }
2406
2407 /* If client use client signature algorithms if not NULL */
2408 if (!s->server && c->client_sigalgs) {
2409 conf = c->client_sigalgs;
2410 conflen = c->client_sigalgslen;
2411 } else if (c->conf_sigalgs) {
2412 conf = c->conf_sigalgs;
2413 conflen = c->conf_sigalgslen;
2414 } else {
2415 conflen = tls12_get_psigalgs(s, &conf);
2416 }
2417
2418 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2419 pref = conf;
2420 preflen = conflen;
2421 allow = c->peer_sigalgs;
2422 allowlen = c->peer_sigalgslen;
2423 } else {
2424 allow = conf;
2425 allowlen = conflen;
2426 pref = c->peer_sigalgs;
2427 preflen = c->peer_sigalgslen;
2428 }
2429
2430 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2431 if (!nmatch) {
2432 return 1;
2433 }
2434
2435 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2436 if (!salgs) {
2437 return 0;
2438 }
2439
2440 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2441 c->shared_sigalgs = salgs;
2442 c->shared_sigalgslen = nmatch;
2443 return 1;
2444}
Adam Langley95c29f32014-06-20 12:00:00 -07002445
2446/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002447int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2448 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002449
Adam Langleyfcf25832014-12-18 17:42:32 -08002450 /* Extension ignored for inappropriate versions */
2451 if (!SSL_USE_SIGALGS(s)) {
2452 return 1;
2453 }
David Benjamincd996942014-07-20 16:23:51 -04002454
David Benjamin61c0d4e2015-03-19 14:24:37 -04002455 if (CBS_len(sigalgs) % 2 != 0 ||
2456 !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) ||
2457 !tls1_set_shared_sigalgs(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002458 return 0;
2459 }
Adam Langley95c29f32014-06-20 12:00:00 -07002460
Adam Langleyfcf25832014-12-18 17:42:32 -08002461 return 1;
2462}
David Benjaminec2f27d2014-11-13 19:17:25 -05002463
Adam Langleyfcf25832014-12-18 17:42:32 -08002464const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey) {
2465 CERT *c = s->cert;
2466 int type = EVP_PKEY_id(pkey);
2467 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002468
Adam Langleyfcf25832014-12-18 17:42:32 -08002469 /* Select the first shared digest supported by our key. */
2470 for (i = 0; i < c->shared_sigalgslen; i++) {
2471 const EVP_MD *md = tls12_get_hash(c->shared_sigalgs[i].rhash);
2472 if (md == NULL ||
2473 tls12_get_pkey_type(c->shared_sigalgs[i].rsign) != type ||
2474 !EVP_PKEY_supports_digest(pkey, md)) {
2475 continue;
2476 }
2477 return md;
2478 }
Adam Langley95c29f32014-06-20 12:00:00 -07002479
Adam Langleyfcf25832014-12-18 17:42:32 -08002480 /* If no suitable digest may be found, default to SHA-1. */
2481 return EVP_sha1();
2482}
Adam Langley95c29f32014-06-20 12:00:00 -07002483
Adam Langleyfcf25832014-12-18 17:42:32 -08002484int SSL_get_sigalgs(SSL *s, int idx, int *psign, int *phash, int *psignhash,
2485 uint8_t *rsig, uint8_t *rhash) {
2486 const uint8_t *psig = s->cert->peer_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002487
Adam Langleyfcf25832014-12-18 17:42:32 -08002488 if (psig == NULL) {
2489 return 0;
2490 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002491
Adam Langleyfcf25832014-12-18 17:42:32 -08002492 if (idx >= 0) {
2493 idx <<= 1;
2494 if (idx >= (int)s->cert->peer_sigalgslen) {
2495 return 0;
2496 }
2497 psig += idx;
2498 if (rhash) {
2499 *rhash = psig[0];
2500 }
2501 if (rsig) {
2502 *rsig = psig[1];
2503 }
2504 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2505 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002506
Adam Langleyfcf25832014-12-18 17:42:32 -08002507 return s->cert->peer_sigalgslen / 2;
2508}
Adam Langley1258b6a2014-06-20 12:00:00 -07002509
Adam Langleyfcf25832014-12-18 17:42:32 -08002510int SSL_get_shared_sigalgs(SSL *s, int idx, int *psign, int *phash,
2511 int *psignhash, uint8_t *rsig, uint8_t *rhash) {
2512 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002513
Adam Langleyfcf25832014-12-18 17:42:32 -08002514 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen) {
2515 return 0;
2516 }
2517
2518 shsigalgs += idx;
2519 if (phash) {
2520 *phash = shsigalgs->hash_nid;
2521 }
2522 if (psign) {
2523 *psign = shsigalgs->sign_nid;
2524 }
2525 if (psignhash) {
2526 *psignhash = shsigalgs->signandhash_nid;
2527 }
2528 if (rsig) {
2529 *rsig = shsigalgs->rsign;
2530 }
2531 if (rhash) {
2532 *rhash = shsigalgs->rhash;
2533 }
2534
2535 return s->cert->shared_sigalgslen;
2536}
2537
2538/* tls1_channel_id_hash calculates the signed data for a Channel ID on the
2539 * given SSL connection and writes it to |md|. */
2540int tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) {
2541 EVP_MD_CTX ctx;
2542 uint8_t temp_digest[EVP_MAX_MD_SIZE];
2543 unsigned temp_digest_len;
2544 int i;
2545 static const char kClientIDMagic[] = "TLS Channel ID signature";
2546
2547 if (s->s3->handshake_buffer &&
2548 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2549 return 0;
2550 }
2551
2552 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2553
2554 if (s->hit && s->s3->tlsext_channel_id_new) {
2555 static const char kResumptionMagic[] = "Resumption";
2556 EVP_DigestUpdate(md, kResumptionMagic, sizeof(kResumptionMagic));
2557 if (s->session->original_handshake_hash_len == 0) {
2558 return 0;
2559 }
2560 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2561 s->session->original_handshake_hash_len);
2562 }
2563
2564 EVP_MD_CTX_init(&ctx);
2565 for (i = 0; i < SSL_MAX_DIGEST; i++) {
2566 if (s->s3->handshake_dgst[i] == NULL) {
2567 continue;
2568 }
David Benjamin9d0847a2015-02-16 03:57:55 -05002569 if (!EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i])) {
2570 EVP_MD_CTX_cleanup(&ctx);
2571 return 0;
2572 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002573 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2574 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2575 }
2576 EVP_MD_CTX_cleanup(&ctx);
2577
2578 return 1;
2579}
Adam Langley1258b6a2014-06-20 12:00:00 -07002580
2581/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2582 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002583int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2584 int digest_len;
2585 /* This function should never be called for a resumed session because the
2586 * handshake hashes that we wish to record are for the original, full
2587 * handshake. */
2588 if (s->hit) {
2589 return -1;
2590 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002591
Adam Langleyfcf25832014-12-18 17:42:32 -08002592 /* It only makes sense to call this function if Channel IDs have been
2593 * negotiated. */
2594 if (!s->s3->tlsext_channel_id_new) {
2595 return -1;
2596 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002597
Adam Langleyfcf25832014-12-18 17:42:32 -08002598 digest_len =
2599 tls1_handshake_digest(s, s->session->original_handshake_hash,
2600 sizeof(s->session->original_handshake_hash));
2601 if (digest_len < 0) {
2602 return -1;
2603 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002604
Adam Langleyfcf25832014-12-18 17:42:32 -08002605 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002606
Adam Langleyfcf25832014-12-18 17:42:32 -08002607 return 1;
2608}
Adam Langley95c29f32014-06-20 12:00:00 -07002609
Adam Langleyfcf25832014-12-18 17:42:32 -08002610int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
2611 int client) {
2612 uint8_t *sigalgs, *sptr;
2613 int rhash, rsign;
2614 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002615
Adam Langleyfcf25832014-12-18 17:42:32 -08002616 if (salglen & 1) {
2617 return 0;
2618 }
Adam Langley95c29f32014-06-20 12:00:00 -07002619
Adam Langleyfcf25832014-12-18 17:42:32 -08002620 sigalgs = OPENSSL_malloc(salglen);
2621 if (sigalgs == NULL) {
2622 return 0;
2623 }
Adam Langley95c29f32014-06-20 12:00:00 -07002624
Adam Langleyfcf25832014-12-18 17:42:32 -08002625 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
2626 rhash = tls12_find_id(*psig_nids++, tls12_md,
2627 sizeof(tls12_md) / sizeof(tls12_lookup));
2628 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2629 sizeof(tls12_sig) / sizeof(tls12_lookup));
Adam Langley95c29f32014-06-20 12:00:00 -07002630
Adam Langleyfcf25832014-12-18 17:42:32 -08002631 if (rhash == -1 || rsign == -1) {
2632 goto err;
2633 }
2634 *sptr++ = rhash;
2635 *sptr++ = rsign;
2636 }
2637
2638 if (client) {
2639 if (c->client_sigalgs) {
2640 OPENSSL_free(c->client_sigalgs);
2641 }
2642 c->client_sigalgs = sigalgs;
2643 c->client_sigalgslen = salglen;
2644 } else {
2645 if (c->conf_sigalgs) {
2646 OPENSSL_free(c->conf_sigalgs);
2647 }
2648 c->conf_sigalgs = sigalgs;
2649 c->conf_sigalgslen = salglen;
2650 }
2651
2652 return 1;
2653
2654err:
2655 OPENSSL_free(sigalgs);
2656 return 0;
2657}