blob: 967dad7556096012894bb3c777b379850ca437bb [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,
136 TLS1_FINISH_MAC_LENGTH,
137 tls1_cert_verify_mac,
138 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
139 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
140 tls1_alert_code,
141 tls1_export_keying_material,
142 0,
143 SSL3_HM_HEADER_LENGTH,
144 ssl3_set_handshake_header,
145 ssl3_handshake_write,
146};
Adam Langley95c29f32014-06-20 12:00:00 -0700147
David Benjamin338fcaf2014-12-11 01:20:52 -0500148const SSL3_ENC_METHOD TLSv1_1_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800149 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -0500150 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800151 tls1_setup_key_block,
152 tls1_generate_master_secret,
153 tls1_change_cipher_state,
154 tls1_final_finish_mac,
155 TLS1_FINISH_MAC_LENGTH,
156 tls1_cert_verify_mac,
157 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
158 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
159 tls1_alert_code,
160 tls1_export_keying_material,
161 SSL_ENC_FLAG_EXPLICIT_IV,
162 SSL3_HM_HEADER_LENGTH,
163 ssl3_set_handshake_header,
164 ssl3_handshake_write,
165};
Adam Langley95c29f32014-06-20 12:00:00 -0700166
David Benjamin338fcaf2014-12-11 01:20:52 -0500167const SSL3_ENC_METHOD TLSv1_2_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -0500169 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800170 tls1_setup_key_block,
171 tls1_generate_master_secret,
172 tls1_change_cipher_state,
173 tls1_final_finish_mac,
174 TLS1_FINISH_MAC_LENGTH,
175 tls1_cert_verify_mac,
176 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
177 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
178 tls1_alert_code,
179 tls1_export_keying_material,
180 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
181 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
182 SSL3_HM_HEADER_LENGTH,
183 ssl3_set_handshake_header,
184 ssl3_handshake_write,
185};
Adam Langley95c29f32014-06-20 12:00:00 -0700186
Adam Langleyfcf25832014-12-18 17:42:32 -0800187static int compare_uint16_t(const void *p1, const void *p2) {
188 uint16_t u1 = *((const uint16_t *)p1);
189 uint16_t u2 = *((const uint16_t *)p2);
190 if (u1 < u2) {
191 return -1;
192 } else if (u1 > u2) {
193 return 1;
194 } else {
195 return 0;
196 }
197}
David Benjamin35a7a442014-07-05 00:23:20 -0400198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
200 * more than one extension of the same type in a ClientHello or ServerHello.
201 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400202 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800203static int tls1_check_duplicate_extensions(const CBS *cbs) {
204 CBS extensions = *cbs;
205 size_t num_extensions = 0, i = 0;
206 uint16_t *extension_types = NULL;
207 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400208
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 /* First pass: count the extensions. */
210 while (CBS_len(&extensions) > 0) {
211 uint16_t type;
212 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400213
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 if (!CBS_get_u16(&extensions, &type) ||
215 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
216 goto done;
217 }
David Benjamin35a7a442014-07-05 00:23:20 -0400218
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 num_extensions++;
220 }
David Benjamin35a7a442014-07-05 00:23:20 -0400221
Adam Langleyfcf25832014-12-18 17:42:32 -0800222 if (num_extensions == 0) {
223 return 1;
224 }
David Benjamin9a373592014-07-25 04:27:53 -0400225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 extension_types =
227 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
228 if (extension_types == NULL) {
229 OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions,
230 ERR_R_MALLOC_FAILURE);
231 goto done;
232 }
David Benjamin35a7a442014-07-05 00:23:20 -0400233
Adam Langleyfcf25832014-12-18 17:42:32 -0800234 /* Second pass: gather the extension types. */
235 extensions = *cbs;
236 for (i = 0; i < num_extensions; i++) {
237 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400238
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
240 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
241 /* This should not happen. */
242 goto done;
243 }
244 }
245 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400246
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 /* Sort the extensions and make sure there are no duplicates. */
248 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
249 for (i = 1; i < num_extensions; i++) {
250 if (extension_types[i - 1] == extension_types[i]) {
251 goto done;
252 }
253 }
David Benjamin35a7a442014-07-05 00:23:20 -0400254
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 ret = 1;
256
David Benjamin35a7a442014-07-05 00:23:20 -0400257done:
David Benjamin6eb000d2015-02-11 01:17:41 -0500258 if (extension_types) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 OPENSSL_free(extension_types);
David Benjamin6eb000d2015-02-11 01:17:41 -0500260 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 return ret;
262}
David Benjamin35a7a442014-07-05 00:23:20 -0400263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
265 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400266
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700268
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 if (/* Skip client version. */
270 !CBS_skip(&client_hello, 2) ||
271 /* Skip client nonce. */
272 !CBS_skip(&client_hello, 32) ||
273 /* Extract session_id. */
274 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
275 return 0;
276 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700277
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 ctx->session_id = CBS_data(&session_id);
279 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700280
Adam Langleyfcf25832014-12-18 17:42:32 -0800281 /* Skip past DTLS cookie */
282 if (SSL_IS_DTLS(ctx->ssl)) {
283 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700284
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
286 return 0;
287 }
288 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700289
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 /* Extract cipher_suites. */
291 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
292 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
293 return 0;
294 }
295 ctx->cipher_suites = CBS_data(&cipher_suites);
296 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700297
Adam Langleyfcf25832014-12-18 17:42:32 -0800298 /* Extract compression_methods. */
299 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
300 CBS_len(&compression_methods) < 1) {
301 return 0;
302 }
303 ctx->compression_methods = CBS_data(&compression_methods);
304 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700305
Adam Langleyfcf25832014-12-18 17:42:32 -0800306 /* If the ClientHello ends here then it's valid, but doesn't have any
307 * extensions. (E.g. SSLv3.) */
308 if (CBS_len(&client_hello) == 0) {
309 ctx->extensions = NULL;
310 ctx->extensions_len = 0;
311 return 1;
312 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700313
Adam Langleyfcf25832014-12-18 17:42:32 -0800314 /* Extract extensions and check it is valid. */
315 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
316 !tls1_check_duplicate_extensions(&extensions) ||
317 CBS_len(&client_hello) != 0) {
318 return 0;
319 }
320 ctx->extensions = CBS_data(&extensions);
321 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700322
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 return 1;
324}
Adam Langleydc9b1412014-06-20 12:00:00 -0700325
Adam Langleyfcf25832014-12-18 17:42:32 -0800326char SSL_early_callback_ctx_extension_get(
327 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
328 const uint8_t **out_data, size_t *out_len) {
329 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700330
Adam Langleyfcf25832014-12-18 17:42:32 -0800331 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700332
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 while (CBS_len(&extensions) != 0) {
334 uint16_t type;
335 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400336
Adam Langleyfcf25832014-12-18 17:42:32 -0800337 /* Decode the next extension. */
338 if (!CBS_get_u16(&extensions, &type) ||
339 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
340 return 0;
341 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700342
Adam Langleyfcf25832014-12-18 17:42:32 -0800343 if (type == extension_type) {
344 *out_data = CBS_data(&extension);
345 *out_len = CBS_len(&extension);
346 return 1;
347 }
348 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700349
Adam Langleyfcf25832014-12-18 17:42:32 -0800350 return 0;
351}
Adam Langley95c29f32014-06-20 12:00:00 -0700352
David Benjamin52e5bac2014-12-27 02:27:35 -0500353struct tls_curve {
354 uint16_t curve_id;
355 int nid;
356};
357
David Benjamin70bd80a2014-12-27 03:06:46 -0500358/* ECC curves from RFC4492. */
David Benjamin52e5bac2014-12-27 02:27:35 -0500359static const struct tls_curve tls_curves[] = {
360 {21, NID_secp224r1},
361 {23, NID_X9_62_prime256v1},
362 {24, NID_secp384r1},
363 {25, NID_secp521r1},
Adam Langleyfcf25832014-12-18 17:42:32 -0800364};
Adam Langley95c29f32014-06-20 12:00:00 -0700365
Adam Langleyfcf25832014-12-18 17:42:32 -0800366static const uint8_t ecformats_default[] = {
367 TLSEXT_ECPOINTFORMAT_uncompressed,
368};
Adam Langley95c29f32014-06-20 12:00:00 -0700369
Adam Langleyfcf25832014-12-18 17:42:32 -0800370static const uint16_t eccurves_default[] = {
David Benjamin52e5bac2014-12-27 02:27:35 -0500371 23, /* X9_64_prime256v1 */
372 24, /* secp384r1 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800373};
Adam Langley95c29f32014-06-20 12:00:00 -0700374
Adam Langleyfcf25832014-12-18 17:42:32 -0800375int tls1_ec_curve_id2nid(uint16_t curve_id) {
David Benjamin52e5bac2014-12-27 02:27:35 -0500376 size_t i;
377 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
378 if (curve_id == tls_curves[i].curve_id) {
379 return tls_curves[i].nid;
380 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 }
David Benjamin52e5bac2014-12-27 02:27:35 -0500382 return NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800383}
Adam Langley95c29f32014-06-20 12:00:00 -0700384
David Benjamin70bd80a2014-12-27 03:06:46 -0500385int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 size_t i;
David Benjamin52e5bac2014-12-27 02:27:35 -0500387 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
388 if (nid == tls_curves[i].nid) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500389 *out_curve_id = tls_curves[i].curve_id;
390 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 }
392 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800393 return 0;
394}
395
396/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
397 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
398 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400399static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800400 const uint16_t **out_curve_ids,
401 size_t *out_curve_ids_len) {
402 if (get_peer_curves) {
403 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
404 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
405 return;
406 }
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 *out_curve_ids = s->tlsext_ellipticcurvelist;
409 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
410 if (!*out_curve_ids) {
411 *out_curve_ids = eccurves_default;
412 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
413 }
414}
David Benjamined439582014-07-14 19:13:02 -0400415
Adam Langleyfcf25832014-12-18 17:42:32 -0800416int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
417 uint8_t curve_type;
418 uint16_t curve_id;
419 const uint16_t *curves;
420 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400421
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 /* Only support named curves. */
423 if (!CBS_get_u8(cbs, &curve_type) ||
424 curve_type != NAMED_CURVE_TYPE ||
425 !CBS_get_u16(cbs, &curve_id)) {
426 return 0;
427 }
David Benjamined439582014-07-14 19:13:02 -0400428
Adam Langleyfcf25832014-12-18 17:42:32 -0800429 tls1_get_curvelist(s, 0, &curves, &curves_len);
430 for (i = 0; i < curves_len; i++) {
431 if (curve_id == curves[i]) {
432 *out_curve_id = curve_id;
433 return 1;
434 }
435 }
Adam Langley95c29f32014-06-20 12:00:00 -0700436
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 return 0;
438}
David Benjamin072334d2014-07-13 16:24:27 -0400439
Adam Langleyfcf25832014-12-18 17:42:32 -0800440int tls1_get_shared_curve(SSL *s) {
441 const uint16_t *pref, *supp;
442 size_t preflen, supplen, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400443
Adam Langleyfcf25832014-12-18 17:42:32 -0800444 /* Can't do anything on client side */
445 if (s->server == 0) {
446 return NID_undef;
447 }
448
449 /* Return first preference shared curve */
450 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &supp,
451 &supplen);
452 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref,
453 &preflen);
454
455 for (i = 0; i < preflen; i++) {
456 for (j = 0; j < supplen; j++) {
457 if (pref[i] == supp[j]) {
458 return tls1_ec_curve_id2nid(pref[i]);
459 }
460 }
461 }
462
463 return NID_undef;
464}
Adam Langley95c29f32014-06-20 12:00:00 -0700465
David Benjamin072334d2014-07-13 16:24:27 -0400466int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 const int *curves, size_t ncurves) {
468 uint16_t *curve_ids;
469 size_t i;
470
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
472 if (curve_ids == NULL) {
473 return 0;
474 }
475
476 for (i = 0; i < ncurves; i++) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500477 if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 OPENSSL_free(curve_ids);
479 return 0;
480 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 }
482
483 if (*out_curve_ids) {
484 OPENSSL_free(*out_curve_ids);
485 }
486 *out_curve_ids = curve_ids;
487 *out_curve_ids_len = ncurves;
488
489 return 1;
490}
Adam Langley95c29f32014-06-20 12:00:00 -0700491
David Benjamin072334d2014-07-13 16:24:27 -0400492/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
493 * TLS curve ID and point format, respectively, for |ec|. It returns one on
494 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800495static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
496 uint8_t *out_comp_id, EC_KEY *ec) {
497 int nid;
498 uint16_t id;
499 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 if (ec == NULL) {
502 return 0;
503 }
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langleyfcf25832014-12-18 17:42:32 -0800505 grp = EC_KEY_get0_group(ec);
506 if (grp == NULL) {
507 return 0;
508 }
David Benjamin072334d2014-07-13 16:24:27 -0400509
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 /* Determine curve ID */
511 nid = EC_GROUP_get_curve_name(grp);
David Benjamin70bd80a2014-12-27 03:06:46 -0500512 if (!tls1_ec_nid2curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800513 return 0;
514 }
David Benjamin072334d2014-07-13 16:24:27 -0400515
Adam Langleyfcf25832014-12-18 17:42:32 -0800516 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
517 *out_curve_id = id;
518
519 if (out_comp_id) {
520 if (EC_KEY_get0_public_key(ec) == NULL) {
521 return 0;
522 }
523 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
524 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
525 } else {
526 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
527 }
528 }
529
530 return 1;
531}
David Benjamin072334d2014-07-13 16:24:27 -0400532
David Benjamin42e9a772014-09-02 23:18:44 -0400533/* tls1_check_point_format returns one if |comp_id| is consistent with the
534 * peer's point format preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800535static int tls1_check_point_format(SSL *s, uint8_t comp_id) {
536 uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
537 size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
538 size_t i;
David Benjamin42e9a772014-09-02 23:18:44 -0400539
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 /* If point formats extension present check it, otherwise everything is
541 * supported (see RFC4492). */
542 if (p == NULL) {
543 return 1;
544 }
David Benjamin42e9a772014-09-02 23:18:44 -0400545
Adam Langleyfcf25832014-12-18 17:42:32 -0800546 for (i = 0; i < plen; i++) {
547 if (comp_id == p[i]) {
548 return 1;
549 }
550 }
David Benjamin42e9a772014-09-02 23:18:44 -0400551
Adam Langleyfcf25832014-12-18 17:42:32 -0800552 return 0;
553}
554
555/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
556 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400557 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800558static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
559 const uint16_t *curves;
560 size_t curves_len, i, j;
David Benjamin42e9a772014-09-02 23:18:44 -0400561
Adam Langleyfcf25832014-12-18 17:42:32 -0800562 /* Check against our list, then the peer's list. */
563 for (j = 0; j <= 1; j++) {
564 tls1_get_curvelist(s, j, &curves, &curves_len);
565 for (i = 0; i < curves_len; i++) {
566 if (curves[i] == curve_id) {
567 break;
568 }
569 }
Adam Langley95c29f32014-06-20 12:00:00 -0700570
Adam Langleyfcf25832014-12-18 17:42:32 -0800571 if (i == curves_len) {
572 return 0;
573 }
Adam Langley95c29f32014-06-20 12:00:00 -0700574
Adam Langleyfcf25832014-12-18 17:42:32 -0800575 /* Servers do not present a preference list so, if we are a client, only
576 * check our list. */
577 if (!s->server) {
578 return 1;
579 }
580 }
David Benjamin033e5f42014-11-13 18:47:41 -0500581
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 return 1;
583}
David Benjamin033e5f42014-11-13 18:47:41 -0500584
Adam Langleyfcf25832014-12-18 17:42:32 -0800585static void tls1_get_formatlist(SSL *s, const uint8_t **pformats,
586 size_t *pformatslen) {
587 /* If we have a custom point format list use it otherwise use default */
588 if (s->tlsext_ecpointformatlist) {
589 *pformats = s->tlsext_ecpointformatlist;
590 *pformatslen = s->tlsext_ecpointformatlist_length;
591 } else {
592 *pformats = ecformats_default;
593 *pformatslen = sizeof(ecformats_default);
594 }
595}
596
597int tls1_check_ec_cert(SSL *s, X509 *x) {
598 int ret = 0;
599 EVP_PKEY *pkey = X509_get_pubkey(x);
600 uint16_t curve_id;
601 uint8_t comp_id;
602
603 if (!pkey ||
604 pkey->type != EVP_PKEY_EC ||
605 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
606 !tls1_check_curve_id(s, curve_id) ||
607 !tls1_check_point_format(s, comp_id)) {
608 goto done;
609 }
610
611 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500612
613done:
Adam Langleyfcf25832014-12-18 17:42:32 -0800614 if (pkey) {
615 EVP_PKEY_free(pkey);
616 }
617 return ret;
618}
David Benjamin42e9a772014-09-02 23:18:44 -0400619
Adam Langleyfcf25832014-12-18 17:42:32 -0800620int tls1_check_ec_tmp_key(SSL *s) {
621 uint16_t curve_id;
622 EC_KEY *ec = s->cert->ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -0700623
Adam Langleyfcf25832014-12-18 17:42:32 -0800624 if (s->cert->ecdh_tmp_auto) {
625 /* Need a shared curve */
626 return tls1_get_shared_curve(s) != NID_undef;
627 }
Adam Langley95c29f32014-06-20 12:00:00 -0700628
Adam Langleyfcf25832014-12-18 17:42:32 -0800629 if (!ec) {
630 if (s->cert->ecdh_tmp_cb) {
631 return 1;
632 }
633 return 0;
634 }
635
636 return tls1_curve_params_from_ec_key(&curve_id, NULL, ec) &&
637 tls1_check_curve_id(s, curve_id);
638}
Adam Langley95c29f32014-06-20 12:00:00 -0700639
640/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800641 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700642
Adam Langley95c29f32014-06-20 12:00:00 -0700643#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700644
Adam Langley95c29f32014-06-20 12:00:00 -0700645#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700646
Adam Langleyfcf25832014-12-18 17:42:32 -0800647#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700648
David Benjamincff64722014-08-19 19:54:46 -0400649static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800650 tlsext_sigalg(TLSEXT_hash_sha512)
651 tlsext_sigalg(TLSEXT_hash_sha384)
652 tlsext_sigalg(TLSEXT_hash_sha256)
653 tlsext_sigalg(TLSEXT_hash_sha224)
654 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700655};
David Benjamin05da6e12014-07-12 20:42:55 -0400656
Adam Langleyfcf25832014-12-18 17:42:32 -0800657size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
658 /* If server use client authentication sigalgs if not NULL */
659 if (s->server && s->cert->client_sigalgs) {
660 *psigs = s->cert->client_sigalgs;
661 return s->cert->client_sigalgslen;
662 } else if (s->cert->conf_sigalgs) {
663 *psigs = s->cert->conf_sigalgs;
664 return s->cert->conf_sigalgslen;
665 } else {
666 *psigs = tls12_sigalgs;
667 return sizeof(tls12_sigalgs);
668 }
669}
Adam Langley95c29f32014-06-20 12:00:00 -0700670
Adam Langleyfcf25832014-12-18 17:42:32 -0800671/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
672 * checks it is consistent with |s|'s sent supported signature algorithms and,
673 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
674 * returns 0 and writes an alert into |*out_alert|. */
675int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
676 CBS *cbs, EVP_PKEY *pkey) {
677 const uint8_t *sent_sigs;
678 size_t sent_sigslen, i;
679 int sigalg = tls12_get_sigid(pkey);
680 uint8_t hash, signature;
681
682 /* Should never happen */
683 if (sigalg == -1) {
684 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
685 *out_alert = SSL_AD_INTERNAL_ERROR;
686 return 0;
687 }
688
689 if (!CBS_get_u8(cbs, &hash) ||
690 !CBS_get_u8(cbs, &signature)) {
691 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
692 *out_alert = SSL_AD_DECODE_ERROR;
693 return 0;
694 }
695
696 /* Check key type is consistent with signature */
697 if (sigalg != signature) {
698 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
699 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
700 return 0;
701 }
702
703 if (pkey->type == EVP_PKEY_EC) {
704 uint16_t curve_id;
705 uint8_t comp_id;
706 /* Check compression and curve matches extensions */
707 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
708 *out_alert = SSL_AD_INTERNAL_ERROR;
709 return 0;
710 }
711
712 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
713 !tls1_check_point_format(s, comp_id))) {
714 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
715 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
716 return 0;
717 }
718 }
719
720 /* Check signature matches a type we sent */
721 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
722 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
723 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
724 break;
725 }
726 }
727
728 /* Allow fallback to SHA-1. */
729 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
730 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
731 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
732 return 0;
733 }
734
735 *out_md = tls12_get_hash(hash);
736 if (*out_md == NULL) {
737 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
738 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
739 return 0;
740 }
741
742 return 1;
743}
744
745/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
746 * supported or doesn't appear in supported signature algorithms. Unlike
747 * ssl_cipher_get_disabled this applies to a specific session and not global
748 * settings. */
749void ssl_set_client_disabled(SSL *s) {
750 CERT *c = s->cert;
751 const uint8_t *sigalgs;
752 size_t i, sigalgslen;
753 int have_rsa = 0, have_ecdsa = 0;
754 c->mask_a = 0;
755 c->mask_k = 0;
756
757 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
758 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
759 c->mask_ssl = SSL_TLSV1_2;
760 } else {
761 c->mask_ssl = 0;
762 }
763
764 /* Now go through all signature algorithms seeing if we support any for RSA,
765 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
766 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
767 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
768 switch (sigalgs[1]) {
769 case TLSEXT_signature_rsa:
770 have_rsa = 1;
771 break;
772
773 case TLSEXT_signature_ecdsa:
774 have_ecdsa = 1;
775 break;
776 }
777 }
778
779 /* Disable auth if we don't include any appropriate signature algorithms. */
780 if (!have_rsa) {
781 c->mask_a |= SSL_aRSA;
782 }
783 if (!have_ecdsa) {
784 c->mask_a |= SSL_aECDSA;
785 }
786
787 /* with PSK there must be client callback set */
788 if (!s->psk_client_callback) {
789 c->mask_a |= SSL_aPSK;
790 c->mask_k |= SSL_kPSK;
791 }
792}
Adam Langley95c29f32014-06-20 12:00:00 -0700793
Adam Langleyb0c235e2014-06-20 12:00:00 -0700794/* header_len is the length of the ClientHello header written so far, used to
795 * compute padding. It does not include the record header. Pass 0 if no padding
796 * is to be done. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800797uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit,
798 size_t header_len) {
799 int extdatalen = 0;
800 uint8_t *ret = buf;
801 uint8_t *orig = buf;
802 /* See if we support any ECC ciphersuites */
803 int using_ecc = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700804
Adam Langleyfcf25832014-12-18 17:42:32 -0800805 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
806 size_t i;
807 unsigned long alg_k, alg_a;
808 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700809
Adam Langleyfcf25832014-12-18 17:42:32 -0800810 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
811 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700812
Adam Langleyfcf25832014-12-18 17:42:32 -0800813 alg_k = c->algorithm_mkey;
814 alg_a = c->algorithm_auth;
815 if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) {
816 using_ecc = 1;
817 break;
818 }
819 }
820 }
Adam Langley95c29f32014-06-20 12:00:00 -0700821
Adam Langleyfcf25832014-12-18 17:42:32 -0800822 /* don't add extensions for SSLv3 unless doing secure renegotiation */
823 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
824 return orig;
825 }
Adam Langley95c29f32014-06-20 12:00:00 -0700826
Adam Langleyfcf25832014-12-18 17:42:32 -0800827 ret += 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700828
Adam Langleyfcf25832014-12-18 17:42:32 -0800829 if (ret >= limit) {
830 return NULL; /* should never occur. */
831 }
Adam Langley95c29f32014-06-20 12:00:00 -0700832
Adam Langleyfcf25832014-12-18 17:42:32 -0800833 if (s->tlsext_hostname != NULL) {
834 /* Add TLS extension servername to the Client Hello message */
835 unsigned long size_str;
836 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -0700837
Adam Langleyfcf25832014-12-18 17:42:32 -0800838 /* check for enough space.
839 4 for the servername type and entension length
840 2 for servernamelist length
841 1 for the hostname type
842 2 for hostname length
843 + hostname length */
Adam Langley95c29f32014-06-20 12:00:00 -0700844
Adam Langleyfcf25832014-12-18 17:42:32 -0800845 lenmax = limit - ret - 9;
846 size_str = strlen(s->tlsext_hostname);
847 if (lenmax < 0 || size_str > (unsigned long)lenmax) {
848 return NULL;
849 }
Adam Langley95c29f32014-06-20 12:00:00 -0700850
Adam Langleyfcf25832014-12-18 17:42:32 -0800851 /* extension type and length */
852 s2n(TLSEXT_TYPE_server_name, ret);
853 s2n(size_str + 5, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700854
Adam Langleyfcf25832014-12-18 17:42:32 -0800855 /* length of servername list */
856 s2n(size_str + 3, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700857
Adam Langleyfcf25832014-12-18 17:42:32 -0800858 /* hostname type, length and hostname */
859 *(ret++) = (uint8_t)TLSEXT_NAMETYPE_host_name;
860 s2n(size_str, ret);
861 memcpy(ret, s->tlsext_hostname, size_str);
862 ret += size_str;
863 }
Adam Langley75712922014-10-10 16:23:43 -0700864
Adam Langleyfcf25832014-12-18 17:42:32 -0800865 /* Add RI if renegotiating */
866 if (s->renegotiate) {
867 int el;
David Benjamind1681e62014-11-20 14:54:14 -0500868
Adam Langleyfcf25832014-12-18 17:42:32 -0800869 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
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 if ((limit - ret - 4 - el) < 0) {
875 return NULL;
876 }
Adam Langley95c29f32014-06-20 12:00:00 -0700877
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 s2n(TLSEXT_TYPE_renegotiate, ret);
879 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700880
Adam Langleyfcf25832014-12-18 17:42:32 -0800881 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
882 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
883 return NULL;
884 }
Adam Langley95c29f32014-06-20 12:00:00 -0700885
Adam Langleyfcf25832014-12-18 17:42:32 -0800886 ret += el;
887 }
Adam Langley95c29f32014-06-20 12:00:00 -0700888
Adam Langleyfcf25832014-12-18 17:42:32 -0800889 /* Add extended master secret. */
890 if (s->version != SSL3_VERSION) {
891 if (limit - ret - 4 < 0) {
892 return NULL;
893 }
894 s2n(TLSEXT_TYPE_extended_master_secret, ret);
895 s2n(0, ret);
896 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200897
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
899 int ticklen = 0;
900 if (!s->new_session && s->session && s->session->tlsext_tick) {
901 ticklen = s->session->tlsext_ticklen;
902 }
Adam Langley95c29f32014-06-20 12:00:00 -0700903
Adam Langleyfcf25832014-12-18 17:42:32 -0800904 /* Check for enough room 2 for extension type, 2 for len rest for
905 * ticket. */
906 if ((long)(limit - ret - 4 - ticklen) < 0) {
907 return NULL;
908 }
909 s2n(TLSEXT_TYPE_session_ticket, ret);
910 s2n(ticklen, ret);
911 if (ticklen) {
912 memcpy(ret, s->session->tlsext_tick, ticklen);
913 ret += ticklen;
914 }
915 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700916
David Benjamin6ae7f072015-01-26 10:22:13 -0500917 if (ssl3_version_from_wire(s, s->client_version) >= TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800918 size_t salglen;
919 const uint8_t *salg;
920 salglen = tls12_get_psigalgs(s, &salg);
921 if ((size_t)(limit - ret) < salglen + 6) {
922 return NULL;
923 }
924 s2n(TLSEXT_TYPE_signature_algorithms, ret);
925 s2n(salglen + 2, ret);
926 s2n(salglen, ret);
927 memcpy(ret, salg, salglen);
928 ret += salglen;
929 }
Adam Langley95c29f32014-06-20 12:00:00 -0700930
Adam Langleyfcf25832014-12-18 17:42:32 -0800931 if (s->ocsp_stapling_enabled) {
932 /* The status_request extension is excessively extensible at every layer.
933 * On the client, only support requesting OCSP responses with an empty
934 * responder_id_list and no extensions. */
935 if (limit - ret - 4 - 1 - 2 - 2 < 0) {
936 return NULL;
937 }
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langleyfcf25832014-12-18 17:42:32 -0800939 s2n(TLSEXT_TYPE_status_request, ret);
940 s2n(1 + 2 + 2, ret);
941 /* status_type */
942 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
943 /* responder_id_list - empty */
944 s2n(0, ret);
945 /* request_extensions - empty */
946 s2n(0, ret);
947 }
Adam Langley95c29f32014-06-20 12:00:00 -0700948
David Benjamin78e69782014-12-19 04:52:17 -0500949 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len &&
950 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800951 /* The client advertises an emtpy extension to indicate its support for
952 * Next Protocol Negotiation */
953 if (limit - ret - 4 < 0) {
954 return NULL;
955 }
956 s2n(TLSEXT_TYPE_next_proto_neg, ret);
957 s2n(0, ret);
958 }
Adam Langley95c29f32014-06-20 12:00:00 -0700959
Adam Langleyfcf25832014-12-18 17:42:32 -0800960 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len) {
961 /* The client advertises an empty extension to indicate its support for
962 * certificate timestamps. */
963 if (limit - ret - 4 < 0) {
964 return NULL;
965 }
966 s2n(TLSEXT_TYPE_certificate_timestamp, ret);
967 s2n(0, ret);
968 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700969
Adam Langleyfcf25832014-12-18 17:42:32 -0800970 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
971 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) {
972 return NULL;
973 }
974 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
975 s2n(2 + s->alpn_client_proto_list_len, ret);
976 s2n(s->alpn_client_proto_list_len, ret);
977 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
978 ret += s->alpn_client_proto_list_len;
979 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700980
David Benjamin78e69782014-12-19 04:52:17 -0500981 if (s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800982 /* The client advertises an emtpy extension to indicate its support for
983 * Channel ID. */
984 if (limit - ret - 4 < 0) {
985 return NULL;
986 }
987 if (s->ctx->tlsext_channel_id_enabled_new) {
988 s2n(TLSEXT_TYPE_channel_id_new, ret);
989 } else {
990 s2n(TLSEXT_TYPE_channel_id, ret);
991 }
992 s2n(0, ret);
993 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700994
Adam Langleyfcf25832014-12-18 17:42:32 -0800995 if (SSL_get_srtp_profiles(s)) {
996 int el;
Adam Langleyc3174b72014-06-20 12:00:00 -0700997
Adam Langleyfcf25832014-12-18 17:42:32 -0800998 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
Adam Langleyc3174b72014-06-20 12:00:00 -0700999
Adam Langleyfcf25832014-12-18 17:42:32 -08001000 if ((limit - ret - 4 - el) < 0) {
1001 return NULL;
1002 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001003
Adam Langleyfcf25832014-12-18 17:42:32 -08001004 s2n(TLSEXT_TYPE_use_srtp, ret);
1005 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001006
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 if (!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1008 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1009 return NULL;
1010 }
1011 ret += el;
1012 }
Adam Langley95c29f32014-06-20 12:00:00 -07001013
Adam Langleyfcf25832014-12-18 17:42:32 -08001014 if (using_ecc) {
1015 /* Add TLS extension ECPointFormats to the ClientHello message */
1016 long lenmax;
1017 const uint8_t *formats;
1018 const uint16_t *curves;
1019 size_t formats_len, curves_len, i;
Adam Langley95c29f32014-06-20 12:00:00 -07001020
Adam Langleyfcf25832014-12-18 17:42:32 -08001021 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001022
Adam Langleyfcf25832014-12-18 17:42:32 -08001023 lenmax = limit - ret - 5;
1024 if (lenmax < 0) {
1025 return NULL;
1026 }
1027 if (formats_len > (size_t)lenmax) {
1028 return NULL;
1029 }
1030 if (formats_len > 255) {
1031 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1032 return NULL;
1033 }
Adam Langley95c29f32014-06-20 12:00:00 -07001034
Adam Langleyfcf25832014-12-18 17:42:32 -08001035 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1036 s2n(formats_len + 1, ret);
1037 *(ret++) = (uint8_t)formats_len;
1038 memcpy(ret, formats, formats_len);
1039 ret += formats_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 /* Add TLS extension EllipticCurves to the ClientHello message */
1042 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001043
Adam Langleyfcf25832014-12-18 17:42:32 -08001044 lenmax = limit - ret - 6;
1045 if (lenmax < 0) {
1046 return NULL;
1047 }
1048 if (curves_len * 2 > (size_t)lenmax) {
1049 return NULL;
1050 }
1051 if (curves_len * 2 > 65532) {
1052 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1053 return NULL;
1054 }
Adam Langley95c29f32014-06-20 12:00:00 -07001055
Adam Langleyfcf25832014-12-18 17:42:32 -08001056 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1057 s2n((curves_len * 2) + 2, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001058
Adam Langleyfcf25832014-12-18 17:42:32 -08001059 s2n(curves_len * 2, ret);
1060 for (i = 0; i < curves_len; i++) {
1061 s2n(curves[i], ret);
1062 }
1063 }
Adam Langley95c29f32014-06-20 12:00:00 -07001064
Adam Langleyfcf25832014-12-18 17:42:32 -08001065 if (header_len > 0) {
1066 size_t clienthello_minsize = 0;
1067 header_len += ret - orig;
1068 if (header_len > 0xff && header_len < 0x200) {
1069 /* Add padding to workaround bugs in F5 terminators. See
1070 * https://tools.ietf.org/html/draft-agl-tls-padding-03
1071 *
1072 * NB: because this code works out the length of all existing extensions
1073 * it MUST always appear last. */
1074 clienthello_minsize = 0x200;
1075 }
1076 if (s->fastradio_padding) {
1077 /* Pad the ClientHello record to 1024 bytes to fast forward the radio
1078 * into DCH (high data rate) state in 3G networks. Note that when
1079 * fastradio_padding is enabled, even if the header_len is less than 255
1080 * bytes, the padding will be applied regardless. This is slightly
1081 * different from the TLS padding extension suggested in
1082 * https://tools.ietf.org/html/draft-agl-tls-padding-03 */
1083 clienthello_minsize = 0x400;
1084 }
1085 if (header_len < clienthello_minsize) {
1086 size_t padding_len = clienthello_minsize - header_len;
1087 /* Extensions take at least four bytes to encode. Always include least
1088 * one byte of data if including the extension. WebSphere Application
1089 * Server 7.0 is intolerant to the last extension being zero-length. */
1090 if (padding_len >= 4 + 1) {
1091 padding_len -= 4;
1092 } else {
1093 padding_len = 1;
1094 }
Adam Langley95c29f32014-06-20 12:00:00 -07001095
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 if (limit - ret - 4 - (long)padding_len < 0) {
1097 return NULL;
1098 }
Adam Langley75712922014-10-10 16:23:43 -07001099
Adam Langleyfcf25832014-12-18 17:42:32 -08001100 s2n(TLSEXT_TYPE_padding, ret);
1101 s2n(padding_len, ret);
1102 memset(ret, 0, padding_len);
1103 ret += padding_len;
1104 }
1105 }
Adam Langley75712922014-10-10 16:23:43 -07001106
Adam Langleyfcf25832014-12-18 17:42:32 -08001107 extdatalen = ret - orig - 2;
1108 if (extdatalen == 0) {
1109 return orig;
1110 }
Adam Langley95c29f32014-06-20 12:00:00 -07001111
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 s2n(extdatalen, orig);
1113 return ret;
1114}
Adam Langley95c29f32014-06-20 12:00:00 -07001115
Adam Langleyfcf25832014-12-18 17:42:32 -08001116uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit) {
1117 int extdatalen = 0;
1118 uint8_t *orig = buf;
1119 uint8_t *ret = buf;
1120 int next_proto_neg_seen;
1121 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1122 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1123 int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
1124 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001125
Adam Langleyfcf25832014-12-18 17:42:32 -08001126 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1127 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
1128 return orig;
1129 }
Adam Langley95c29f32014-06-20 12:00:00 -07001130
Adam Langleyfcf25832014-12-18 17:42:32 -08001131 ret += 2;
1132 if (ret >= limit) {
1133 return NULL; /* should never happen. */
1134 }
Adam Langley95c29f32014-06-20 12:00:00 -07001135
Adam Langleyfcf25832014-12-18 17:42:32 -08001136 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL) {
1137 if ((long)(limit - ret - 4) < 0) {
1138 return NULL;
1139 }
Adam Langley95c29f32014-06-20 12:00:00 -07001140
Adam Langleyfcf25832014-12-18 17:42:32 -08001141 s2n(TLSEXT_TYPE_server_name, ret);
1142 s2n(0, ret);
1143 }
Adam Langley95c29f32014-06-20 12:00:00 -07001144
Adam Langleyfcf25832014-12-18 17:42:32 -08001145 if (s->s3->send_connection_binding) {
1146 int el;
Adam Langley95c29f32014-06-20 12:00:00 -07001147
Adam Langleyfcf25832014-12-18 17:42:32 -08001148 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
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 if ((limit - ret - 4 - el) < 0) {
1154 return NULL;
1155 }
Adam Langley95c29f32014-06-20 12:00:00 -07001156
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 s2n(TLSEXT_TYPE_renegotiate, ret);
1158 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001159
Adam Langleyfcf25832014-12-18 17:42:32 -08001160 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1161 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1162 return NULL;
1163 }
Adam Langley95c29f32014-06-20 12:00:00 -07001164
Adam Langleyfcf25832014-12-18 17:42:32 -08001165 ret += el;
1166 }
Adam Langley95c29f32014-06-20 12:00:00 -07001167
Adam Langleyfcf25832014-12-18 17:42:32 -08001168 if (s->s3->tmp.extended_master_secret) {
1169 if ((long)(limit - ret - 4) < 0) {
1170 return NULL;
1171 }
Adam Langley95c29f32014-06-20 12:00:00 -07001172
Adam Langleyfcf25832014-12-18 17:42:32 -08001173 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1174 s2n(0, ret);
1175 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001176
Adam Langleyfcf25832014-12-18 17:42:32 -08001177 if (using_ecc) {
1178 const uint8_t *plist;
1179 size_t plistlen;
1180 /* Add TLS extension ECPointFormats to the ServerHello message */
1181 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -07001182
Adam Langleyfcf25832014-12-18 17:42:32 -08001183 tls1_get_formatlist(s, &plist, &plistlen);
1184
1185 lenmax = limit - ret - 5;
1186 if (lenmax < 0) {
1187 return NULL;
1188 }
1189 if (plistlen > (size_t)lenmax) {
1190 return NULL;
1191 }
1192 if (plistlen > 255) {
1193 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1194 return NULL;
1195 }
1196
1197 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1198 s2n(plistlen + 1, ret);
1199 *(ret++) = (uint8_t)plistlen;
1200 memcpy(ret, plist, plistlen);
1201 ret += plistlen;
1202 }
1203 /* Currently the server should not respond with a SupportedCurves extension */
1204
1205 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1206 if ((long)(limit - ret - 4) < 0) {
1207 return NULL;
1208 }
1209 s2n(TLSEXT_TYPE_session_ticket, ret);
1210 s2n(0, ret);
1211 }
1212
1213 if (s->s3->tmp.certificate_status_expected) {
1214 if ((long)(limit - ret - 4) < 0) {
1215 return NULL;
1216 }
1217 s2n(TLSEXT_TYPE_status_request, ret);
1218 s2n(0, ret);
1219 }
1220
1221 if (s->srtp_profile) {
1222 int el;
1223
1224 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1225
1226 if ((limit - ret - 4 - el) < 0) {
1227 return NULL;
1228 }
1229
1230 s2n(TLSEXT_TYPE_use_srtp, ret);
1231 s2n(el, ret);
1232
1233 if (!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1234 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1235 return NULL;
1236 }
1237 ret += el;
1238 }
1239
1240 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1241 s->s3->next_proto_neg_seen = 0;
1242 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1243 const uint8_t *npa;
1244 unsigned int npalen;
1245 int r;
1246
1247 r = s->ctx->next_protos_advertised_cb(
1248 s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1249 if (r == SSL_TLSEXT_ERR_OK) {
1250 if ((long)(limit - ret - 4 - npalen) < 0) {
1251 return NULL;
1252 }
1253 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1254 s2n(npalen, ret);
1255 memcpy(ret, npa, npalen);
1256 ret += npalen;
1257 s->s3->next_proto_neg_seen = 1;
1258 }
1259 }
1260
1261 if (s->s3->alpn_selected) {
1262 const uint8_t *selected = s->s3->alpn_selected;
1263 size_t len = s->s3->alpn_selected_len;
1264
1265 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) {
1266 return NULL;
1267 }
1268 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1269 s2n(3 + len, ret);
1270 s2n(1 + len, ret);
1271 *ret++ = len;
1272 memcpy(ret, selected, len);
1273 ret += len;
1274 }
1275
1276 /* If the client advertised support for Channel ID, and we have it
1277 * enabled, then we want to echo it back. */
1278 if (s->s3->tlsext_channel_id_valid) {
1279 if (limit - ret - 4 < 0) {
1280 return NULL;
1281 }
1282 if (s->s3->tlsext_channel_id_new) {
1283 s2n(TLSEXT_TYPE_channel_id_new, ret);
1284 } else {
1285 s2n(TLSEXT_TYPE_channel_id, ret);
1286 }
1287 s2n(0, ret);
1288 }
1289
1290 extdatalen = ret - orig - 2;
1291 if (extdatalen == 0) {
1292 return orig;
1293 }
1294
1295 s2n(extdatalen, orig);
1296 return ret;
1297}
Adam Langley95c29f32014-06-20 12:00:00 -07001298
Adam Langley95c29f32014-06-20 12:00:00 -07001299/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1300 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001301 * cbs: the contents of the extension, not including the type and length.
1302 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001303 * return.
1304 *
David Benjamindc72ff72014-06-25 12:36:10 -04001305 * returns: 1 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001306static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert) {
1307 CBS protocol_name_list, protocol_name_list_copy;
1308 const uint8_t *selected;
1309 uint8_t selected_len;
1310 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07001311
Adam Langleyfcf25832014-12-18 17:42:32 -08001312 if (s->ctx->alpn_select_cb == NULL) {
1313 return 1;
1314 }
Adam Langley95c29f32014-06-20 12:00:00 -07001315
Adam Langleyfcf25832014-12-18 17:42:32 -08001316 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1317 CBS_len(cbs) != 0 || CBS_len(&protocol_name_list) < 2) {
1318 goto parse_error;
1319 }
Adam Langley95c29f32014-06-20 12:00:00 -07001320
Adam Langleyfcf25832014-12-18 17:42:32 -08001321 /* Validate the protocol list. */
1322 protocol_name_list_copy = protocol_name_list;
1323 while (CBS_len(&protocol_name_list_copy) > 0) {
1324 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001325
Adam Langleyfcf25832014-12-18 17:42:32 -08001326 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name)) {
1327 goto parse_error;
1328 }
1329 }
Adam Langley95c29f32014-06-20 12:00:00 -07001330
Adam Langleyfcf25832014-12-18 17:42:32 -08001331 r = s->ctx->alpn_select_cb(
1332 s, &selected, &selected_len, CBS_data(&protocol_name_list),
1333 CBS_len(&protocol_name_list), s->ctx->alpn_select_cb_arg);
1334 if (r == SSL_TLSEXT_ERR_OK) {
1335 if (s->s3->alpn_selected) {
1336 OPENSSL_free(s->s3->alpn_selected);
1337 }
1338 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
1339 if (!s->s3->alpn_selected) {
1340 *out_alert = SSL_AD_INTERNAL_ERROR;
1341 return 0;
1342 }
1343 s->s3->alpn_selected_len = selected_len;
1344 }
1345
1346 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001347
1348parse_error:
Adam Langleyfcf25832014-12-18 17:42:32 -08001349 *out_alert = SSL_AD_DECODE_ERROR;
1350 return 0;
1351}
Adam Langley95c29f32014-06-20 12:00:00 -07001352
Adam Langleyfcf25832014-12-18 17:42:32 -08001353static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1354 int renegotiate_seen = 0;
1355 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001356
Adam Langleyfcf25832014-12-18 17:42:32 -08001357 s->should_ack_sni = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05001358 s->srtp_profile = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001359 s->s3->next_proto_neg_seen = 0;
1360 s->s3->tmp.certificate_status_expected = 0;
1361 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001362
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 if (s->s3->alpn_selected) {
1364 OPENSSL_free(s->s3->alpn_selected);
1365 s->s3->alpn_selected = NULL;
1366 }
Adam Langley95c29f32014-06-20 12:00:00 -07001367
Adam Langleyfcf25832014-12-18 17:42:32 -08001368 /* Clear any signature algorithms extension received */
1369 if (s->cert->peer_sigalgs) {
1370 OPENSSL_free(s->cert->peer_sigalgs);
1371 s->cert->peer_sigalgs = NULL;
1372 }
Adam Langley95c29f32014-06-20 12:00:00 -07001373
Adam Langleyfcf25832014-12-18 17:42:32 -08001374 /* Clear any shared signature algorithms */
1375 if (s->cert->shared_sigalgs) {
1376 OPENSSL_free(s->cert->shared_sigalgs);
1377 s->cert->shared_sigalgs = NULL;
1378 }
Adam Langley95c29f32014-06-20 12:00:00 -07001379
Adam Langleyfcf25832014-12-18 17:42:32 -08001380 /* Clear ECC extensions */
1381 if (s->s3->tmp.peer_ecpointformatlist != 0) {
1382 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1383 s->s3->tmp.peer_ecpointformatlist = NULL;
1384 s->s3->tmp.peer_ecpointformatlist_length = 0;
1385 }
David Benjamindc72ff72014-06-25 12:36:10 -04001386
Adam Langleyfcf25832014-12-18 17:42:32 -08001387 if (s->s3->tmp.peer_ellipticcurvelist != 0) {
1388 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1389 s->s3->tmp.peer_ellipticcurvelist = NULL;
1390 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1391 }
David Benjamindc72ff72014-06-25 12:36:10 -04001392
Adam Langleyfcf25832014-12-18 17:42:32 -08001393 /* There may be no extensions. */
1394 if (CBS_len(cbs) == 0) {
1395 goto ri_check;
1396 }
David Benjamindc72ff72014-06-25 12:36:10 -04001397
Adam Langleyfcf25832014-12-18 17:42:32 -08001398 /* Decode the extensions block and check it is valid. */
1399 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1400 !tls1_check_duplicate_extensions(&extensions)) {
1401 *out_alert = SSL_AD_DECODE_ERROR;
1402 return 0;
1403 }
David Benjamindc72ff72014-06-25 12:36:10 -04001404
Adam Langleyfcf25832014-12-18 17:42:32 -08001405 while (CBS_len(&extensions) != 0) {
1406 uint16_t type;
1407 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001408
Adam Langleyfcf25832014-12-18 17:42:32 -08001409 /* Decode the next extension. */
1410 if (!CBS_get_u16(&extensions, &type) ||
1411 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1412 *out_alert = SSL_AD_DECODE_ERROR;
1413 return 0;
1414 }
Adam Langley95c29f32014-06-20 12:00:00 -07001415
Adam Langleyfcf25832014-12-18 17:42:32 -08001416 if (s->tlsext_debug_cb) {
1417 s->tlsext_debug_cb(s, 0, type, (uint8_t *)CBS_data(&extension),
1418 CBS_len(&extension), s->tlsext_debug_arg);
1419 }
Adam Langley95c29f32014-06-20 12:00:00 -07001420
Adam Langleyfcf25832014-12-18 17:42:32 -08001421 /* The servername extension is treated as follows:
David Benjamindc72ff72014-06-25 12:36:10 -04001422
Adam Langleyfcf25832014-12-18 17:42:32 -08001423 - Only the hostname type is supported with a maximum length of 255.
1424 - The servername is rejected if too long or if it contains zeros, in
1425 which case an fatal alert is generated.
1426 - The servername field is maintained together with the session cache.
1427 - When a session is resumed, the servername call back invoked in order
1428 to allow the application to position itself to the right context.
1429 - The servername is acknowledged if it is new for a session or when
1430 it is identical to a previously used for the same session.
1431 Applications can control the behaviour. They can at any time
1432 set a 'desirable' servername for a new SSL object. This can be the
1433 case for example with HTTPS when a Host: header field is received and
1434 a renegotiation is requested. In this case, a possible servername
1435 presented in the new client hello is only acknowledged if it matches
1436 the value of the Host: field.
1437 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1438 if they provide for changing an explicit servername context for the
1439 session,
1440 i.e. when the session has been established with a servername extension.
1441 - On session reconnect, the servername extension may be absent. */
Adam Langley95c29f32014-06-20 12:00:00 -07001442
Adam Langleyfcf25832014-12-18 17:42:32 -08001443 if (type == TLSEXT_TYPE_server_name) {
1444 CBS server_name_list;
1445 char have_seen_host_name = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001446
Adam Langleyfcf25832014-12-18 17:42:32 -08001447 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1448 CBS_len(&server_name_list) < 1 || CBS_len(&extension) != 0) {
1449 *out_alert = SSL_AD_DECODE_ERROR;
1450 return 0;
1451 }
David Benjamindc72ff72014-06-25 12:36:10 -04001452
Adam Langleyfcf25832014-12-18 17:42:32 -08001453 /* Decode each ServerName in the extension. */
1454 while (CBS_len(&server_name_list) > 0) {
1455 uint8_t name_type;
1456 CBS host_name;
David Benjamindc72ff72014-06-25 12:36:10 -04001457
Adam Langleyfcf25832014-12-18 17:42:32 -08001458 /* Decode the NameType. */
1459 if (!CBS_get_u8(&server_name_list, &name_type)) {
1460 *out_alert = SSL_AD_DECODE_ERROR;
1461 return 0;
1462 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001463
Adam Langleyfcf25832014-12-18 17:42:32 -08001464 /* Only host_name is supported. */
1465 if (name_type != TLSEXT_NAMETYPE_host_name) {
1466 continue;
1467 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001468
Adam Langleyfcf25832014-12-18 17:42:32 -08001469 if (have_seen_host_name) {
1470 /* The ServerNameList MUST NOT contain more than one name of the same
1471 * name_type. */
1472 *out_alert = SSL_AD_DECODE_ERROR;
1473 return 0;
1474 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001475
Adam Langleyfcf25832014-12-18 17:42:32 -08001476 have_seen_host_name = 1;
Adam Langleyed8270a2014-09-02 13:52:56 -07001477
Adam Langleyfcf25832014-12-18 17:42:32 -08001478 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1479 CBS_len(&host_name) < 1) {
1480 *out_alert = SSL_AD_DECODE_ERROR;
1481 return 0;
1482 }
Adam Langley95c29f32014-06-20 12:00:00 -07001483
Adam Langleyfcf25832014-12-18 17:42:32 -08001484 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1485 CBS_contains_zero_byte(&host_name)) {
1486 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1487 return 0;
1488 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001489
Adam Langleyfcf25832014-12-18 17:42:32 -08001490 if (!s->hit) {
1491 assert(s->session->tlsext_hostname == NULL);
1492 if (s->session->tlsext_hostname) {
1493 /* This should be impossible. */
1494 *out_alert = SSL_AD_DECODE_ERROR;
1495 return 0;
1496 }
Adam Langley95c29f32014-06-20 12:00:00 -07001497
Adam Langleyfcf25832014-12-18 17:42:32 -08001498 /* Copy the hostname as a string. */
1499 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname)) {
1500 *out_alert = SSL_AD_INTERNAL_ERROR;
1501 return 0;
1502 }
Adam Langley95c29f32014-06-20 12:00:00 -07001503
Adam Langleyfcf25832014-12-18 17:42:32 -08001504 s->should_ack_sni = 1;
1505 }
1506 }
1507 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1508 CBS ec_point_format_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001509
Adam Langleyfcf25832014-12-18 17:42:32 -08001510 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1511 CBS_len(&extension) != 0) {
1512 *out_alert = SSL_AD_DECODE_ERROR;
1513 return 0;
1514 }
Adam Langley95c29f32014-06-20 12:00:00 -07001515
Adam Langleyfcf25832014-12-18 17:42:32 -08001516 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1517 &s->s3->tmp.peer_ecpointformatlist_length)) {
1518 *out_alert = SSL_AD_INTERNAL_ERROR;
1519 return 0;
1520 }
1521 } else if (type == TLSEXT_TYPE_elliptic_curves) {
1522 CBS elliptic_curve_list;
1523 size_t i, num_curves;
David Benjamindc72ff72014-06-25 12:36:10 -04001524
Adam Langleyfcf25832014-12-18 17:42:32 -08001525 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
1526 CBS_len(&elliptic_curve_list) == 0 ||
1527 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
1528 CBS_len(&extension) != 0) {
1529 *out_alert = SSL_AD_DECODE_ERROR;
1530 return 0;
1531 }
David Benjamindc72ff72014-06-25 12:36:10 -04001532
Adam Langleyfcf25832014-12-18 17:42:32 -08001533 if (s->s3->tmp.peer_ellipticcurvelist) {
1534 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1535 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1536 }
David Benjamindc72ff72014-06-25 12:36:10 -04001537
Adam Langleyfcf25832014-12-18 17:42:32 -08001538 s->s3->tmp.peer_ellipticcurvelist =
1539 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
David Benjamindc72ff72014-06-25 12:36:10 -04001540
Adam Langleyfcf25832014-12-18 17:42:32 -08001541 if (s->s3->tmp.peer_ellipticcurvelist == NULL) {
1542 *out_alert = SSL_AD_INTERNAL_ERROR;
1543 return 0;
1544 }
Adam Langley95c29f32014-06-20 12:00:00 -07001545
Adam Langleyfcf25832014-12-18 17:42:32 -08001546 num_curves = CBS_len(&elliptic_curve_list) / 2;
1547 for (i = 0; i < num_curves; i++) {
1548 if (!CBS_get_u16(&elliptic_curve_list,
1549 &s->s3->tmp.peer_ellipticcurvelist[i])) {
1550 *out_alert = SSL_AD_INTERNAL_ERROR;
1551 return 0;
1552 }
1553 }
David Benjamindc72ff72014-06-25 12:36:10 -04001554
Adam Langleyfcf25832014-12-18 17:42:32 -08001555 if (CBS_len(&elliptic_curve_list) != 0) {
1556 *out_alert = SSL_AD_INTERNAL_ERROR;
1557 return 0;
1558 }
Adam Langley95c29f32014-06-20 12:00:00 -07001559
Adam Langleyfcf25832014-12-18 17:42:32 -08001560 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
1561 } else if (type == TLSEXT_TYPE_renegotiate) {
1562 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert)) {
1563 return 0;
1564 }
1565 renegotiate_seen = 1;
1566 } else if (type == TLSEXT_TYPE_signature_algorithms) {
1567 CBS supported_signature_algorithms;
Adam Langley95c29f32014-06-20 12:00:00 -07001568
Adam Langleyfcf25832014-12-18 17:42:32 -08001569 if (!CBS_get_u16_length_prefixed(&extension,
1570 &supported_signature_algorithms) ||
1571 CBS_len(&extension) != 0) {
1572 *out_alert = SSL_AD_DECODE_ERROR;
1573 return 0;
1574 }
Adam Langley95c29f32014-06-20 12:00:00 -07001575
Adam Langleyfcf25832014-12-18 17:42:32 -08001576 /* Ensure the signature algorithms are non-empty. It contains a list of
1577 * SignatureAndHashAlgorithms which are two bytes each. */
1578 if (CBS_len(&supported_signature_algorithms) == 0 ||
1579 (CBS_len(&supported_signature_algorithms) % 2) != 0) {
1580 *out_alert = SSL_AD_DECODE_ERROR;
1581 return 0;
1582 }
David Benjamindc72ff72014-06-25 12:36:10 -04001583
Adam Langleyfcf25832014-12-18 17:42:32 -08001584 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1585 *out_alert = SSL_AD_DECODE_ERROR;
1586 return 0;
1587 }
1588 /* If sigalgs received and no shared algorithms fatal error. */
1589 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs) {
1590 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext,
1591 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
1592 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1593 return 0;
1594 }
1595 } else if (type == TLSEXT_TYPE_next_proto_neg &&
David Benjamin78e69782014-12-19 04:52:17 -05001596 s->s3->tmp.finish_md_len == 0 && s->s3->alpn_selected == NULL &&
1597 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001598 /* The extension must be empty. */
1599 if (CBS_len(&extension) != 0) {
1600 *out_alert = SSL_AD_DECODE_ERROR;
1601 return 0;
1602 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001603
Adam Langleyfcf25832014-12-18 17:42:32 -08001604 /* We shouldn't accept this extension on a renegotiation.
1605 *
1606 * s->new_session will be set on renegotiation, but we probably shouldn't
1607 * rely that it couldn't be set on the initial renegotation too in
1608 * certain cases (when there's some other reason to disallow resuming an
1609 * earlier session -- the current code won't be doing anything like that,
1610 * but this might change).
David Benjamindc72ff72014-06-25 12:36:10 -04001611
Adam Langleyfcf25832014-12-18 17:42:32 -08001612 * A valid sign that there's been a previous handshake in this connection
1613 * is if s->s3->tmp.finish_md_len > 0. (We are talking about a check
1614 * that will happen in the Hello protocol round, well before a new
1615 * Finished message could have been computed.) */
1616 s->s3->next_proto_neg_seen = 1;
1617 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1618 s->ctx->alpn_select_cb && s->s3->tmp.finish_md_len == 0) {
1619 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert)) {
1620 return 0;
1621 }
1622 /* ALPN takes precedence over NPN. */
1623 s->s3->next_proto_neg_seen = 0;
David Benjamin78e69782014-12-19 04:52:17 -05001624 } else if (type == TLSEXT_TYPE_channel_id && s->tlsext_channel_id_enabled &&
1625 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001626 /* The extension must be empty. */
1627 if (CBS_len(&extension) != 0) {
1628 *out_alert = SSL_AD_DECODE_ERROR;
1629 return 0;
1630 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001631
Adam Langleyfcf25832014-12-18 17:42:32 -08001632 s->s3->tlsext_channel_id_valid = 1;
1633 } else if (type == TLSEXT_TYPE_channel_id_new &&
David Benjamin78e69782014-12-19 04:52:17 -05001634 s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001635 /* The extension must be empty. */
1636 if (CBS_len(&extension) != 0) {
1637 *out_alert = SSL_AD_DECODE_ERROR;
1638 return 0;
1639 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001640
Adam Langleyfcf25832014-12-18 17:42:32 -08001641 s->s3->tlsext_channel_id_valid = 1;
1642 s->s3->tlsext_channel_id_new = 1;
1643 } else if (type == TLSEXT_TYPE_use_srtp) {
1644 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert)) {
1645 return 0;
1646 }
1647 } else if (type == TLSEXT_TYPE_extended_master_secret &&
1648 s->version != SSL3_VERSION) {
1649 if (CBS_len(&extension) != 0) {
1650 *out_alert = SSL_AD_DECODE_ERROR;
1651 return 0;
1652 }
Adam Langley75712922014-10-10 16:23:43 -07001653
Adam Langleyfcf25832014-12-18 17:42:32 -08001654 s->s3->tmp.extended_master_secret = 1;
1655 }
1656 }
Adam Langley75712922014-10-10 16:23:43 -07001657
Adam Langleyfcf25832014-12-18 17:42:32 -08001658ri_check:
1659 /* Need RI if renegotiating */
Adam Langley95c29f32014-06-20 12:00:00 -07001660
Adam Langleyfcf25832014-12-18 17:42:32 -08001661 if (!renegotiate_seen && s->renegotiate &&
1662 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1663 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1664 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
1665 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1666 return 0;
1667 }
Adam Langley95c29f32014-06-20 12:00:00 -07001668
Adam Langleyfcf25832014-12-18 17:42:32 -08001669 return 1;
1670}
Adam Langley95c29f32014-06-20 12:00:00 -07001671
Adam Langleyfcf25832014-12-18 17:42:32 -08001672int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
1673 int alert = -1;
1674 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
1675 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
1676 return 0;
1677 }
Adam Langley95c29f32014-06-20 12:00:00 -07001678
Adam Langleyfcf25832014-12-18 17:42:32 -08001679 if (ssl_check_clienthello_tlsext(s) <= 0) {
1680 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext,
1681 SSL_R_CLIENTHELLO_TLSEXT);
1682 return 0;
1683 }
Adam Langley95c29f32014-06-20 12:00:00 -07001684
Adam Langleyfcf25832014-12-18 17:42:32 -08001685 return 1;
1686}
Adam Langley95c29f32014-06-20 12:00:00 -07001687
Adam Langley95c29f32014-06-20 12:00:00 -07001688/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
Adam Langleyfcf25832014-12-18 17:42:32 -08001689 * elements of zero length are allowed and the set of elements must exactly
1690 * fill the length of the block. */
1691static char ssl_next_proto_validate(const CBS *cbs) {
1692 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001693
Adam Langleyfcf25832014-12-18 17:42:32 -08001694 while (CBS_len(&copy) != 0) {
1695 CBS proto;
1696 if (!CBS_get_u8_length_prefixed(&copy, &proto) || CBS_len(&proto) == 0) {
1697 return 0;
1698 }
1699 }
Adam Langley95c29f32014-06-20 12:00:00 -07001700
Adam Langleyfcf25832014-12-18 17:42:32 -08001701 return 1;
1702}
Adam Langley95c29f32014-06-20 12:00:00 -07001703
Adam Langleyfcf25832014-12-18 17:42:32 -08001704static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1705 int tlsext_servername = 0;
1706 int renegotiate_seen = 0;
1707 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001708
Adam Langleyfcf25832014-12-18 17:42:32 -08001709 /* TODO(davidben): Move all of these to some per-handshake state that gets
1710 * systematically reset on a new handshake; perhaps allocate it fresh each
1711 * time so it's not even kept around post-handshake. */
1712 s->s3->next_proto_neg_seen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001713 s->tlsext_ticket_expected = 0;
1714 s->s3->tmp.certificate_status_expected = 0;
1715 s->s3->tmp.extended_master_secret = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05001716 s->srtp_profile = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001717
Adam Langleyfcf25832014-12-18 17:42:32 -08001718 if (s->s3->alpn_selected) {
1719 OPENSSL_free(s->s3->alpn_selected);
1720 s->s3->alpn_selected = NULL;
1721 }
David Benjamina19fc252014-10-19 00:14:36 -04001722
Adam Langleyfcf25832014-12-18 17:42:32 -08001723 /* Clear ECC extensions */
1724 if (s->s3->tmp.peer_ecpointformatlist != 0) {
1725 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1726 s->s3->tmp.peer_ecpointformatlist = NULL;
1727 s->s3->tmp.peer_ecpointformatlist_length = 0;
1728 }
David Benjamin03973092014-06-24 23:27:17 -04001729
Adam Langleyfcf25832014-12-18 17:42:32 -08001730 /* There may be no extensions. */
1731 if (CBS_len(cbs) == 0) {
1732 goto ri_check;
1733 }
Adam Langley95c29f32014-06-20 12:00:00 -07001734
Adam Langleyfcf25832014-12-18 17:42:32 -08001735 /* Decode the extensions block and check it is valid. */
1736 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1737 !tls1_check_duplicate_extensions(&extensions)) {
1738 *out_alert = SSL_AD_DECODE_ERROR;
1739 return 0;
1740 }
Adam Langley95c29f32014-06-20 12:00:00 -07001741
Adam Langleyfcf25832014-12-18 17:42:32 -08001742 while (CBS_len(&extensions) != 0) {
1743 uint16_t type;
1744 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001745
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 /* Decode the next extension. */
1747 if (!CBS_get_u16(&extensions, &type) ||
1748 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1749 *out_alert = SSL_AD_DECODE_ERROR;
1750 return 0;
1751 }
Adam Langley95c29f32014-06-20 12:00:00 -07001752
Adam Langleyfcf25832014-12-18 17:42:32 -08001753 if (s->tlsext_debug_cb) {
1754 s->tlsext_debug_cb(s, 1, type, (uint8_t *)CBS_data(&extension),
1755 CBS_len(&extension), s->tlsext_debug_arg);
1756 }
Adam Langley95c29f32014-06-20 12:00:00 -07001757
Adam Langleyfcf25832014-12-18 17:42:32 -08001758 if (type == TLSEXT_TYPE_server_name) {
1759 /* The extension must be empty. */
1760 if (CBS_len(&extension) != 0) {
1761 *out_alert = SSL_AD_DECODE_ERROR;
1762 return 0;
1763 }
David Benjamin03973092014-06-24 23:27:17 -04001764
Adam Langleyfcf25832014-12-18 17:42:32 -08001765 /* We must have sent it in ClientHello. */
1766 if (s->tlsext_hostname == NULL) {
1767 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1768 return 0;
1769 }
David Benjamin03973092014-06-24 23:27:17 -04001770
Adam Langleyfcf25832014-12-18 17:42:32 -08001771 tlsext_servername = 1;
1772 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1773 CBS ec_point_format_list;
David Benjamin03973092014-06-24 23:27:17 -04001774
Adam Langleyfcf25832014-12-18 17:42:32 -08001775 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1776 CBS_len(&extension) != 0) {
1777 *out_alert = SSL_AD_DECODE_ERROR;
1778 return 0;
1779 }
Adam Langley95c29f32014-06-20 12:00:00 -07001780
Adam Langleyfcf25832014-12-18 17:42:32 -08001781 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1782 &s->s3->tmp.peer_ecpointformatlist_length)) {
1783 *out_alert = SSL_AD_INTERNAL_ERROR;
1784 return 0;
1785 }
1786 } else if (type == TLSEXT_TYPE_session_ticket) {
1787 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0) {
1788 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1789 return 0;
1790 }
David Benjamin03973092014-06-24 23:27:17 -04001791
Adam Langleyfcf25832014-12-18 17:42:32 -08001792 s->tlsext_ticket_expected = 1;
1793 } else if (type == TLSEXT_TYPE_status_request) {
1794 /* The extension MUST be empty and may only sent if we've requested a
1795 * status request message. */
1796 if (CBS_len(&extension) != 0) {
1797 *out_alert = SSL_AD_DECODE_ERROR;
1798 return 0;
1799 }
David Benjamin03973092014-06-24 23:27:17 -04001800
Adam Langleyfcf25832014-12-18 17:42:32 -08001801 if (!s->ocsp_stapling_enabled) {
1802 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1803 return 0;
1804 }
Adam Langley95c29f32014-06-20 12:00:00 -07001805
Adam Langleyfcf25832014-12-18 17:42:32 -08001806 /* Set a flag to expect a CertificateStatus message */
1807 s->s3->tmp.certificate_status_expected = 1;
1808 } else if (type == TLSEXT_TYPE_next_proto_neg &&
David Benjamin78e69782014-12-19 04:52:17 -05001809 s->s3->tmp.finish_md_len == 0 &&
1810 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001811 uint8_t *selected;
1812 uint8_t selected_len;
David Benjamin03973092014-06-24 23:27:17 -04001813
Adam Langleyfcf25832014-12-18 17:42:32 -08001814 /* We must have requested it. */
1815 if (s->ctx->next_proto_select_cb == NULL) {
1816 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1817 return 0;
1818 }
David Benjamin03973092014-06-24 23:27:17 -04001819
Adam Langleyfcf25832014-12-18 17:42:32 -08001820 /* The data must be valid. */
1821 if (!ssl_next_proto_validate(&extension)) {
1822 *out_alert = SSL_AD_DECODE_ERROR;
1823 return 0;
1824 }
Adam Langley95c29f32014-06-20 12:00:00 -07001825
Adam Langleyfcf25832014-12-18 17:42:32 -08001826 if (s->ctx->next_proto_select_cb(
1827 s, &selected, &selected_len, CBS_data(&extension),
1828 CBS_len(&extension),
1829 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1830 *out_alert = SSL_AD_INTERNAL_ERROR;
1831 return 0;
1832 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001833
Adam Langleyfcf25832014-12-18 17:42:32 -08001834 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
1835 if (s->next_proto_negotiated == NULL) {
1836 *out_alert = SSL_AD_INTERNAL_ERROR;
1837 return 0;
1838 }
Adam Langley75712922014-10-10 16:23:43 -07001839
Adam Langleyfcf25832014-12-18 17:42:32 -08001840 s->next_proto_negotiated_len = selected_len;
1841 s->s3->next_proto_neg_seen = 1;
1842 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
1843 CBS protocol_name_list, protocol_name;
Adam Langley75712922014-10-10 16:23:43 -07001844
Adam Langleyfcf25832014-12-18 17:42:32 -08001845 /* We must have requested it. */
1846 if (s->alpn_client_proto_list == NULL) {
1847 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1848 return 0;
1849 }
Adam Langley95c29f32014-06-20 12:00:00 -07001850
Adam Langleyfcf25832014-12-18 17:42:32 -08001851 /* The extension data consists of a ProtocolNameList which must have
1852 * exactly one ProtocolName. Each of these is length-prefixed. */
1853 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
1854 CBS_len(&extension) != 0 ||
1855 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1856 CBS_len(&protocol_name_list) != 0) {
1857 *out_alert = SSL_AD_DECODE_ERROR;
1858 return 0;
1859 }
Adam Langley95c29f32014-06-20 12:00:00 -07001860
Adam Langleyfcf25832014-12-18 17:42:32 -08001861 if (!CBS_stow(&protocol_name, &s->s3->alpn_selected,
1862 &s->s3->alpn_selected_len)) {
1863 *out_alert = SSL_AD_INTERNAL_ERROR;
1864 return 0;
1865 }
David Benjamin78e69782014-12-19 04:52:17 -05001866 } else if (type == TLSEXT_TYPE_channel_id && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001867 if (CBS_len(&extension) != 0) {
1868 *out_alert = SSL_AD_DECODE_ERROR;
1869 return 0;
1870 }
Adam Langley95c29f32014-06-20 12:00:00 -07001871
Adam Langleyfcf25832014-12-18 17:42:32 -08001872 s->s3->tlsext_channel_id_valid = 1;
David Benjamin78e69782014-12-19 04:52:17 -05001873 } else if (type == TLSEXT_TYPE_channel_id_new && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001874 if (CBS_len(&extension) != 0) {
1875 *out_alert = SSL_AD_DECODE_ERROR;
1876 return 0;
1877 }
Adam Langley95c29f32014-06-20 12:00:00 -07001878
Adam Langleyfcf25832014-12-18 17:42:32 -08001879 s->s3->tlsext_channel_id_valid = 1;
1880 s->s3->tlsext_channel_id_new = 1;
1881 } else if (type == TLSEXT_TYPE_certificate_timestamp) {
1882 if (CBS_len(&extension) == 0) {
1883 *out_alert = SSL_AD_DECODE_ERROR;
1884 return 0;
1885 }
Adam Langley95c29f32014-06-20 12:00:00 -07001886
Adam Langleyfcf25832014-12-18 17:42:32 -08001887 /* Session resumption uses the original session information. */
1888 if (!s->hit &&
1889 !CBS_stow(&extension, &s->session->tlsext_signed_cert_timestamp_list,
1890 &s->session->tlsext_signed_cert_timestamp_list_length)) {
1891 *out_alert = SSL_AD_INTERNAL_ERROR;
1892 return 0;
1893 }
1894 } else if (type == TLSEXT_TYPE_renegotiate) {
1895 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert)) {
1896 return 0;
1897 }
Adam Langley95c29f32014-06-20 12:00:00 -07001898
Adam Langleyfcf25832014-12-18 17:42:32 -08001899 renegotiate_seen = 1;
1900 } else if (type == TLSEXT_TYPE_use_srtp) {
1901 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert)) {
1902 return 0;
1903 }
1904 } else if (type == TLSEXT_TYPE_extended_master_secret) {
1905 if (/* It is invalid for the server to select EMS and
1906 SSLv3. */
1907 s->version == SSL3_VERSION || CBS_len(&extension) != 0) {
1908 *out_alert = SSL_AD_DECODE_ERROR;
1909 return 0;
1910 }
Adam Langley95c29f32014-06-20 12:00:00 -07001911
Adam Langleyfcf25832014-12-18 17:42:32 -08001912 s->s3->tmp.extended_master_secret = 1;
1913 }
1914 }
Adam Langley95c29f32014-06-20 12:00:00 -07001915
Adam Langleyfcf25832014-12-18 17:42:32 -08001916 if (!s->hit && tlsext_servername == 1 && s->tlsext_hostname) {
1917 if (s->session->tlsext_hostname == NULL) {
1918 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
1919 if (!s->session->tlsext_hostname) {
1920 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1921 return 0;
1922 }
1923 } else {
1924 *out_alert = SSL_AD_DECODE_ERROR;
1925 return 0;
1926 }
1927 }
Adam Langley95c29f32014-06-20 12:00:00 -07001928
Adam Langleyfcf25832014-12-18 17:42:32 -08001929ri_check:
1930 /* Determine if we need to see RI. Strictly speaking if we want to avoid an
1931 * attack we should *always* see RI even on initial server hello because the
1932 * client doesn't see any renegotiation during an attack. However this would
1933 * mean we could not connect to any server which doesn't support RI so for
1934 * the immediate future tolerate RI absence on initial connect only. */
1935 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) &&
1936 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1937 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1938 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext,
1939 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1940 return 0;
1941 }
Adam Langley95c29f32014-06-20 12:00:00 -07001942
Adam Langleyfcf25832014-12-18 17:42:32 -08001943 return 1;
1944}
Adam Langley95c29f32014-06-20 12:00:00 -07001945
Adam Langleyfcf25832014-12-18 17:42:32 -08001946int ssl_prepare_clienthello_tlsext(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07001947
Adam Langleyfcf25832014-12-18 17:42:32 -08001948int ssl_prepare_serverhello_tlsext(SSL *s) { return 1; }
Adam Langleyed8270a2014-09-02 13:52:56 -07001949
Adam Langleyfcf25832014-12-18 17:42:32 -08001950static int ssl_check_clienthello_tlsext(SSL *s) {
1951 int ret = SSL_TLSEXT_ERR_NOACK;
1952 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07001953
Adam Langleyfcf25832014-12-18 17:42:32 -08001954 /* The handling of the ECPointFormats extension is done elsewhere, namely in
1955 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07001956
Adam Langleyfcf25832014-12-18 17:42:32 -08001957 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
1958 ret = s->ctx->tlsext_servername_callback(s, &al,
1959 s->ctx->tlsext_servername_arg);
1960 } else if (s->initial_ctx != NULL &&
1961 s->initial_ctx->tlsext_servername_callback != 0) {
1962 ret = s->initial_ctx->tlsext_servername_callback(
1963 s, &al, s->initial_ctx->tlsext_servername_arg);
1964 }
Adam Langley95c29f32014-06-20 12:00:00 -07001965
Adam Langleyfcf25832014-12-18 17:42:32 -08001966 switch (ret) {
1967 case SSL_TLSEXT_ERR_ALERT_FATAL:
1968 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1969 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001970
Adam Langleyfcf25832014-12-18 17:42:32 -08001971 case SSL_TLSEXT_ERR_ALERT_WARNING:
1972 ssl3_send_alert(s, SSL3_AL_WARNING, al);
1973 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001974
Adam Langleyfcf25832014-12-18 17:42:32 -08001975 case SSL_TLSEXT_ERR_NOACK:
1976 s->should_ack_sni = 0;
1977 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001978
Adam Langleyfcf25832014-12-18 17:42:32 -08001979 default:
1980 return 1;
1981 }
1982}
Adam Langleyed8270a2014-09-02 13:52:56 -07001983
Adam Langleyfcf25832014-12-18 17:42:32 -08001984static int ssl_check_serverhello_tlsext(SSL *s) {
1985 int ret = SSL_TLSEXT_ERR_NOACK;
1986 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07001987
Adam Langleyfcf25832014-12-18 17:42:32 -08001988 /* If we are client and using an elliptic curve cryptography cipher suite,
1989 * then if server returns an EC point formats lists extension it must contain
1990 * uncompressed. */
1991 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1992 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1993 if (((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) &&
1994 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed)) {
1995 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext,
1996 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
1997 return -1;
1998 }
1999 ret = SSL_TLSEXT_ERR_OK;
David Benjamin03973092014-06-24 23:27:17 -04002000
Adam Langleyfcf25832014-12-18 17:42:32 -08002001 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2002 ret = s->ctx->tlsext_servername_callback(s, &al,
2003 s->ctx->tlsext_servername_arg);
2004 } else if (s->initial_ctx != NULL &&
2005 s->initial_ctx->tlsext_servername_callback != 0) {
2006 ret = s->initial_ctx->tlsext_servername_callback(
2007 s, &al, s->initial_ctx->tlsext_servername_arg);
2008 }
Adam Langley95c29f32014-06-20 12:00:00 -07002009
Adam Langleyfcf25832014-12-18 17:42:32 -08002010 switch (ret) {
2011 case SSL_TLSEXT_ERR_ALERT_FATAL:
2012 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2013 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002014
Adam Langleyfcf25832014-12-18 17:42:32 -08002015 case SSL_TLSEXT_ERR_ALERT_WARNING:
2016 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2017 return 1;
2018
2019 default:
2020 return 1;
2021 }
2022}
2023
2024int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2025 int alert = -1;
2026 if (s->version < SSL3_VERSION) {
2027 return 1;
2028 }
2029
2030 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2031 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2032 return 0;
2033 }
2034
2035 if (ssl_check_serverhello_tlsext(s) <= 0) {
2036 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext,
2037 SSL_R_SERVERHELLO_TLSEXT);
2038 return 0;
2039 }
2040
2041 return 1;
2042}
Adam Langley95c29f32014-06-20 12:00:00 -07002043
2044/* Since the server cache lookup is done early on in the processing of the
2045 * ClientHello, and other operations depend on the result, we need to handle
2046 * any TLS session ticket extension at the same time.
2047 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002048 * ctx: contains the early callback context, which is the result of a
2049 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002050 * ret: (output) on return, if a ticket was decrypted, then this is set to
2051 * point to the resulting session.
2052 *
Adam Langley95c29f32014-06-20 12:00:00 -07002053 * Returns:
2054 * -1: fatal error, either from parsing or decrypting the ticket.
2055 * 0: no ticket was found (or was ignored, based on settings).
2056 * 1: a zero length extension was found, indicating that the client supports
2057 * session tickets but doesn't currently have one to offer.
David Benjamind1681e62014-11-20 14:54:14 -05002058 * 2: a ticket was offered but couldn't be decrypted because of a non-fatal
2059 * error.
Adam Langley95c29f32014-06-20 12:00:00 -07002060 * 3: a ticket was successfully decrypted and *ret was set.
2061 *
2062 * Side effects:
2063 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2064 * a new session ticket to the client because the client indicated support
David Benjamind1681e62014-11-20 14:54:14 -05002065 * but the client either doesn't have a session ticket or we couldn't use
2066 * the one it gave us, or if s->ctx->tlsext_ticket_key_cb asked to renew
2067 * the client's ticket. Otherwise, s->tlsext_ticket_expected is set to 0.
Adam Langley95c29f32014-06-20 12:00:00 -07002068 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002069int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002070 SSL_SESSION **ret) {
2071 *ret = NULL;
2072 s->tlsext_ticket_expected = 0;
2073 const uint8_t *data;
2074 size_t len;
2075 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002076
Adam Langleyfcf25832014-12-18 17:42:32 -08002077 /* If tickets disabled behave as if no ticket present to permit stateful
2078 * resumption. */
2079 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) ||
2080 (s->version <= SSL3_VERSION && !ctx->extensions) ||
2081 !SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_session_ticket,
2082 &data, &len)) {
2083 return 0;
2084 }
2085
2086 if (len == 0) {
2087 /* The client will accept a ticket but doesn't currently have one. */
2088 s->tlsext_ticket_expected = 1;
2089 return 1;
2090 }
2091
2092 r = tls_decrypt_ticket(s, data, len, ctx->session_id, ctx->session_id_len,
2093 ret);
2094 switch (r) {
2095 case 2: /* ticket couldn't be decrypted */
2096 s->tlsext_ticket_expected = 1;
2097 return 2;
2098
2099 case 3: /* ticket was decrypted */
2100 return r;
2101
2102 case 4: /* ticket decrypted but need to renew */
2103 s->tlsext_ticket_expected = 1;
2104 return 3;
2105
2106 default: /* fatal error */
2107 return -1;
2108 }
2109}
Adam Langley95c29f32014-06-20 12:00:00 -07002110
2111/* tls_decrypt_ticket attempts to decrypt a session ticket.
2112 *
2113 * etick: points to the body of the session ticket extension.
2114 * eticklen: the length of the session tickets extenion.
2115 * sess_id: points at the session ID.
2116 * sesslen: the length of the session ID.
2117 * psess: (output) on return, if a ticket was decrypted, then this is set to
2118 * point to the resulting session.
2119 *
2120 * Returns:
2121 * -1: fatal error, either from parsing or decrypting the ticket.
2122 * 2: the ticket couldn't be decrypted.
2123 * 3: a ticket was successfully decrypted and *psess was set.
Adam Langleyfcf25832014-12-18 17:42:32 -08002124 * 4: same as 3, but the ticket needs to be renewed. */
2125static int tls_decrypt_ticket(SSL *s, const uint8_t *etick, int eticklen,
2126 const uint8_t *sess_id, int sesslen,
2127 SSL_SESSION **psess) {
2128 SSL_SESSION *sess;
2129 uint8_t *sdec;
2130 const uint8_t *p;
2131 int slen, mlen, renew_ticket = 0;
2132 uint8_t tick_hmac[EVP_MAX_MD_SIZE];
2133 HMAC_CTX hctx;
2134 EVP_CIPHER_CTX ctx;
2135 SSL_CTX *tctx = s->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002136
Adam Langleyfcf25832014-12-18 17:42:32 -08002137 /* Need at least keyname + iv + some encrypted data */
2138 if (eticklen < 48) {
2139 return 2;
2140 }
2141
2142 /* Initialize session ticket encryption and HMAC contexts */
2143 HMAC_CTX_init(&hctx);
2144 EVP_CIPHER_CTX_init(&ctx);
2145 if (tctx->tlsext_ticket_key_cb) {
2146 uint8_t *nctick = (uint8_t *)etick;
2147 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, &ctx, &hctx, 0);
2148 if (rv < 0) {
2149 return -1;
2150 }
2151 if (rv == 0) {
2152 return 2;
2153 }
2154 if (rv == 2) {
2155 renew_ticket = 1;
2156 }
2157 } else {
2158 /* Check key name matches */
2159 if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) {
2160 return 2;
2161 }
2162 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2163 NULL) ||
2164 !EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2165 tctx->tlsext_tick_aes_key, etick + 16)) {
2166 HMAC_CTX_cleanup(&hctx);
2167 EVP_CIPHER_CTX_cleanup(&ctx);
2168 return -1;
2169 }
2170 }
2171
2172 /* Attempt to process session ticket, first conduct sanity and integrity
2173 * checks on ticket. */
2174 mlen = HMAC_size(&hctx);
2175 if (mlen < 0) {
2176 HMAC_CTX_cleanup(&hctx);
2177 EVP_CIPHER_CTX_cleanup(&ctx);
2178 return -1;
2179 }
2180 eticklen -= mlen;
2181 /* Check HMAC of encrypted ticket */
2182 HMAC_Update(&hctx, etick, eticklen);
2183 HMAC_Final(&hctx, tick_hmac, NULL);
2184 HMAC_CTX_cleanup(&hctx);
2185 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
2186 EVP_CIPHER_CTX_cleanup(&ctx);
2187 return 2;
2188 }
2189
2190 /* Attempt to decrypt session data */
2191 /* Move p after IV to start of encrypted ticket, update length */
2192 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2193 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2194 sdec = OPENSSL_malloc(eticklen);
2195 if (!sdec) {
2196 EVP_CIPHER_CTX_cleanup(&ctx);
2197 return -1;
2198 }
2199 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2200 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0) {
2201 EVP_CIPHER_CTX_cleanup(&ctx);
2202 OPENSSL_free(sdec);
2203 return 2;
2204 }
2205 slen += mlen;
2206 EVP_CIPHER_CTX_cleanup(&ctx);
2207 p = sdec;
2208
2209 sess = d2i_SSL_SESSION(NULL, &p, slen);
2210 OPENSSL_free(sdec);
2211 if (sess) {
2212 /* The session ID, if non-empty, is used by some clients to detect that the
2213 * ticket has been accepted. So we copy it to the session structure. If it
2214 * is empty set length to zero as required by standard. */
2215 if (sesslen) {
2216 memcpy(sess->session_id, sess_id, sesslen);
2217 }
2218 sess->session_id_length = sesslen;
2219 *psess = sess;
2220 if (renew_ticket) {
2221 return 4;
2222 }
2223 return 3;
2224 }
2225
2226 ERR_clear_error();
2227 /* For session parse failure, indicate that we need to send a new ticket. */
2228 return 2;
2229}
Adam Langley95c29f32014-06-20 12:00:00 -07002230
2231/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002232typedef struct {
2233 int nid;
2234 int id;
2235} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002236
Adam Langleyfcf25832014-12-18 17:42:32 -08002237static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2238 {NID_sha1, TLSEXT_hash_sha1},
2239 {NID_sha224, TLSEXT_hash_sha224},
2240 {NID_sha256, TLSEXT_hash_sha256},
2241 {NID_sha384, TLSEXT_hash_sha384},
2242 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002243
Adam Langleyfcf25832014-12-18 17:42:32 -08002244static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2245 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002246
Adam Langleyfcf25832014-12-18 17:42:32 -08002247static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2248 size_t i;
2249 for (i = 0; i < tlen; i++) {
2250 if (table[i].nid == nid) {
2251 return table[i].id;
2252 }
2253 }
Adam Langley95c29f32014-06-20 12:00:00 -07002254
Adam Langleyfcf25832014-12-18 17:42:32 -08002255 return -1;
2256}
Adam Langley95c29f32014-06-20 12:00:00 -07002257
Adam Langleyfcf25832014-12-18 17:42:32 -08002258static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) {
2259 size_t i;
2260 for (i = 0; i < tlen; i++) {
2261 if (table[i].id == id) {
2262 return table[i].nid;
2263 }
2264 }
Adam Langley95c29f32014-06-20 12:00:00 -07002265
Adam Langleyfcf25832014-12-18 17:42:32 -08002266 return NID_undef;
2267}
Adam Langley95c29f32014-06-20 12:00:00 -07002268
Adam Langleyfcf25832014-12-18 17:42:32 -08002269int tls12_get_sigandhash(uint8_t *p, const EVP_PKEY *pk, const EVP_MD *md) {
2270 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002271
Adam Langleyfcf25832014-12-18 17:42:32 -08002272 if (!md) {
2273 return 0;
2274 }
Adam Langley95c29f32014-06-20 12:00:00 -07002275
Adam Langleyfcf25832014-12-18 17:42:32 -08002276 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2277 sizeof(tls12_md) / sizeof(tls12_lookup));
2278 if (md_id == -1) {
2279 return 0;
2280 }
Adam Langley95c29f32014-06-20 12:00:00 -07002281
Adam Langleyfcf25832014-12-18 17:42:32 -08002282 sig_id = tls12_get_sigid(pk);
2283 if (sig_id == -1) {
2284 return 0;
2285 }
Adam Langley95c29f32014-06-20 12:00:00 -07002286
Adam Langleyfcf25832014-12-18 17:42:32 -08002287 p[0] = (uint8_t)md_id;
2288 p[1] = (uint8_t)sig_id;
2289 return 1;
2290}
2291
2292int tls12_get_sigid(const EVP_PKEY *pk) {
2293 return tls12_find_id(pk->type, tls12_sig,
2294 sizeof(tls12_sig) / sizeof(tls12_lookup));
2295}
2296
2297const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2298 switch (hash_alg) {
2299 case TLSEXT_hash_md5:
2300 return EVP_md5();
2301
2302 case TLSEXT_hash_sha1:
2303 return EVP_sha1();
2304
2305 case TLSEXT_hash_sha224:
2306 return EVP_sha224();
2307
2308 case TLSEXT_hash_sha256:
2309 return EVP_sha256();
2310
2311 case TLSEXT_hash_sha384:
2312 return EVP_sha384();
2313
2314 case TLSEXT_hash_sha512:
2315 return EVP_sha512();
2316
2317 default:
2318 return NULL;
2319 }
2320}
Adam Langley95c29f32014-06-20 12:00:00 -07002321
David Benjaminec2f27d2014-11-13 19:17:25 -05002322/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2323 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002324static int tls12_get_pkey_type(uint8_t sig_alg) {
2325 switch (sig_alg) {
2326 case TLSEXT_signature_rsa:
2327 return EVP_PKEY_RSA;
2328
2329 case TLSEXT_signature_ecdsa:
2330 return EVP_PKEY_EC;
2331
2332 default:
2333 return -1;
2334 }
2335}
Adam Langley95c29f32014-06-20 12:00:00 -07002336
2337/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2338static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
Adam Langleyfcf25832014-12-18 17:42:32 -08002339 int *psignhash_nid, const uint8_t *data) {
2340 int sign_nid = 0, hash_nid = 0;
2341 if (!phash_nid && !psign_nid && !psignhash_nid) {
2342 return;
2343 }
2344
2345 if (phash_nid || psignhash_nid) {
2346 hash_nid = tls12_find_nid(data[0], tls12_md,
2347 sizeof(tls12_md) / sizeof(tls12_lookup));
2348 if (phash_nid) {
2349 *phash_nid = hash_nid;
2350 }
2351 }
2352
2353 if (psign_nid || psignhash_nid) {
2354 sign_nid = tls12_find_nid(data[1], tls12_sig,
2355 sizeof(tls12_sig) / sizeof(tls12_lookup));
2356 if (psign_nid) {
2357 *psign_nid = sign_nid;
2358 }
2359 }
2360
2361 if (psignhash_nid) {
2362 if (sign_nid && hash_nid) {
2363 OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid);
2364 } else {
2365 *psignhash_nid = NID_undef;
2366 }
2367 }
2368}
2369
Adam Langley95c29f32014-06-20 12:00:00 -07002370/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002371static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2372 size_t preflen, const uint8_t *allow,
2373 size_t allowlen) {
2374 const uint8_t *ptmp, *atmp;
2375 size_t i, j, nmatch = 0;
2376
2377 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2378 /* Skip disabled hashes or signature algorithms */
2379 if (tls12_get_hash(ptmp[0]) == NULL ||
2380 tls12_get_pkey_type(ptmp[1]) == -1) {
2381 continue;
2382 }
2383
2384 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2385 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2386 nmatch++;
2387 if (shsig) {
2388 shsig->rhash = ptmp[0];
2389 shsig->rsign = ptmp[1];
2390 tls1_lookup_sigalg(&shsig->hash_nid, &shsig->sign_nid,
2391 &shsig->signandhash_nid, ptmp);
2392 shsig++;
2393 }
2394
2395 break;
2396 }
2397 }
2398 }
2399
2400 return nmatch;
2401}
Adam Langley95c29f32014-06-20 12:00:00 -07002402
2403/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002404static int tls1_set_shared_sigalgs(SSL *s) {
2405 const uint8_t *pref, *allow, *conf;
2406 size_t preflen, allowlen, conflen;
2407 size_t nmatch;
2408 TLS_SIGALGS *salgs = NULL;
2409 CERT *c = s->cert;
2410
2411 if (c->shared_sigalgs) {
2412 OPENSSL_free(c->shared_sigalgs);
2413 c->shared_sigalgs = NULL;
2414 }
2415
2416 /* If client use client signature algorithms if not NULL */
2417 if (!s->server && c->client_sigalgs) {
2418 conf = c->client_sigalgs;
2419 conflen = c->client_sigalgslen;
2420 } else if (c->conf_sigalgs) {
2421 conf = c->conf_sigalgs;
2422 conflen = c->conf_sigalgslen;
2423 } else {
2424 conflen = tls12_get_psigalgs(s, &conf);
2425 }
2426
2427 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2428 pref = conf;
2429 preflen = conflen;
2430 allow = c->peer_sigalgs;
2431 allowlen = c->peer_sigalgslen;
2432 } else {
2433 allow = conf;
2434 allowlen = conflen;
2435 pref = c->peer_sigalgs;
2436 preflen = c->peer_sigalgslen;
2437 }
2438
2439 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2440 if (!nmatch) {
2441 return 1;
2442 }
2443
2444 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2445 if (!salgs) {
2446 return 0;
2447 }
2448
2449 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2450 c->shared_sigalgs = salgs;
2451 c->shared_sigalgslen = nmatch;
2452 return 1;
2453}
Adam Langley95c29f32014-06-20 12:00:00 -07002454
2455/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002456int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2457 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002458
Adam Langleyfcf25832014-12-18 17:42:32 -08002459 /* Extension ignored for inappropriate versions */
2460 if (!SSL_USE_SIGALGS(s)) {
2461 return 1;
2462 }
David Benjamincd996942014-07-20 16:23:51 -04002463
Adam Langleyfcf25832014-12-18 17:42:32 -08002464 /* Length must be even */
2465 if (CBS_len(sigalgs) % 2 != 0) {
2466 return 0;
2467 }
Adam Langley95c29f32014-06-20 12:00:00 -07002468
Adam Langleyfcf25832014-12-18 17:42:32 -08002469 /* Should never happen */
2470 if (!c) {
2471 return 0;
2472 }
Adam Langley95c29f32014-06-20 12:00:00 -07002473
Adam Langleyfcf25832014-12-18 17:42:32 -08002474 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen)) {
2475 return 0;
2476 }
Adam Langley95c29f32014-06-20 12:00:00 -07002477
Adam Langleyfcf25832014-12-18 17:42:32 -08002478 tls1_set_shared_sigalgs(s);
2479 return 1;
2480}
David Benjaminec2f27d2014-11-13 19:17:25 -05002481
Adam Langleyfcf25832014-12-18 17:42:32 -08002482const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey) {
2483 CERT *c = s->cert;
2484 int type = EVP_PKEY_id(pkey);
2485 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002486
Adam Langleyfcf25832014-12-18 17:42:32 -08002487 /* Select the first shared digest supported by our key. */
2488 for (i = 0; i < c->shared_sigalgslen; i++) {
2489 const EVP_MD *md = tls12_get_hash(c->shared_sigalgs[i].rhash);
2490 if (md == NULL ||
2491 tls12_get_pkey_type(c->shared_sigalgs[i].rsign) != type ||
2492 !EVP_PKEY_supports_digest(pkey, md)) {
2493 continue;
2494 }
2495 return md;
2496 }
Adam Langley95c29f32014-06-20 12:00:00 -07002497
Adam Langleyfcf25832014-12-18 17:42:32 -08002498 /* If no suitable digest may be found, default to SHA-1. */
2499 return EVP_sha1();
2500}
Adam Langley95c29f32014-06-20 12:00:00 -07002501
Adam Langleyfcf25832014-12-18 17:42:32 -08002502int SSL_get_sigalgs(SSL *s, int idx, int *psign, int *phash, int *psignhash,
2503 uint8_t *rsig, uint8_t *rhash) {
2504 const uint8_t *psig = s->cert->peer_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002505
Adam Langleyfcf25832014-12-18 17:42:32 -08002506 if (psig == NULL) {
2507 return 0;
2508 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002509
Adam Langleyfcf25832014-12-18 17:42:32 -08002510 if (idx >= 0) {
2511 idx <<= 1;
2512 if (idx >= (int)s->cert->peer_sigalgslen) {
2513 return 0;
2514 }
2515 psig += idx;
2516 if (rhash) {
2517 *rhash = psig[0];
2518 }
2519 if (rsig) {
2520 *rsig = psig[1];
2521 }
2522 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2523 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002524
Adam Langleyfcf25832014-12-18 17:42:32 -08002525 return s->cert->peer_sigalgslen / 2;
2526}
Adam Langley1258b6a2014-06-20 12:00:00 -07002527
Adam Langleyfcf25832014-12-18 17:42:32 -08002528int SSL_get_shared_sigalgs(SSL *s, int idx, int *psign, int *phash,
2529 int *psignhash, uint8_t *rsig, uint8_t *rhash) {
2530 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002531
Adam Langleyfcf25832014-12-18 17:42:32 -08002532 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen) {
2533 return 0;
2534 }
2535
2536 shsigalgs += idx;
2537 if (phash) {
2538 *phash = shsigalgs->hash_nid;
2539 }
2540 if (psign) {
2541 *psign = shsigalgs->sign_nid;
2542 }
2543 if (psignhash) {
2544 *psignhash = shsigalgs->signandhash_nid;
2545 }
2546 if (rsig) {
2547 *rsig = shsigalgs->rsign;
2548 }
2549 if (rhash) {
2550 *rhash = shsigalgs->rhash;
2551 }
2552
2553 return s->cert->shared_sigalgslen;
2554}
2555
2556/* tls1_channel_id_hash calculates the signed data for a Channel ID on the
2557 * given SSL connection and writes it to |md|. */
2558int tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) {
2559 EVP_MD_CTX ctx;
2560 uint8_t temp_digest[EVP_MAX_MD_SIZE];
2561 unsigned temp_digest_len;
2562 int i;
2563 static const char kClientIDMagic[] = "TLS Channel ID signature";
2564
2565 if (s->s3->handshake_buffer &&
2566 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2567 return 0;
2568 }
2569
2570 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2571
2572 if (s->hit && s->s3->tlsext_channel_id_new) {
2573 static const char kResumptionMagic[] = "Resumption";
2574 EVP_DigestUpdate(md, kResumptionMagic, sizeof(kResumptionMagic));
2575 if (s->session->original_handshake_hash_len == 0) {
2576 return 0;
2577 }
2578 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2579 s->session->original_handshake_hash_len);
2580 }
2581
2582 EVP_MD_CTX_init(&ctx);
2583 for (i = 0; i < SSL_MAX_DIGEST; i++) {
2584 if (s->s3->handshake_dgst[i] == NULL) {
2585 continue;
2586 }
David Benjamin9d0847a2015-02-16 03:57:55 -05002587 if (!EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i])) {
2588 EVP_MD_CTX_cleanup(&ctx);
2589 return 0;
2590 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002591 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2592 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2593 }
2594 EVP_MD_CTX_cleanup(&ctx);
2595
2596 return 1;
2597}
Adam Langley1258b6a2014-06-20 12:00:00 -07002598
2599/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2600 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002601int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2602 int digest_len;
2603 /* This function should never be called for a resumed session because the
2604 * handshake hashes that we wish to record are for the original, full
2605 * handshake. */
2606 if (s->hit) {
2607 return -1;
2608 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002609
Adam Langleyfcf25832014-12-18 17:42:32 -08002610 /* It only makes sense to call this function if Channel IDs have been
2611 * negotiated. */
2612 if (!s->s3->tlsext_channel_id_new) {
2613 return -1;
2614 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002615
Adam Langleyfcf25832014-12-18 17:42:32 -08002616 digest_len =
2617 tls1_handshake_digest(s, s->session->original_handshake_hash,
2618 sizeof(s->session->original_handshake_hash));
2619 if (digest_len < 0) {
2620 return -1;
2621 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002622
Adam Langleyfcf25832014-12-18 17:42:32 -08002623 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002624
Adam Langleyfcf25832014-12-18 17:42:32 -08002625 return 1;
2626}
Adam Langley95c29f32014-06-20 12:00:00 -07002627
Adam Langleyfcf25832014-12-18 17:42:32 -08002628int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
2629 int client) {
2630 uint8_t *sigalgs, *sptr;
2631 int rhash, rsign;
2632 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002633
Adam Langleyfcf25832014-12-18 17:42:32 -08002634 if (salglen & 1) {
2635 return 0;
2636 }
Adam Langley95c29f32014-06-20 12:00:00 -07002637
Adam Langleyfcf25832014-12-18 17:42:32 -08002638 sigalgs = OPENSSL_malloc(salglen);
2639 if (sigalgs == NULL) {
2640 return 0;
2641 }
Adam Langley95c29f32014-06-20 12:00:00 -07002642
Adam Langleyfcf25832014-12-18 17:42:32 -08002643 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
2644 rhash = tls12_find_id(*psig_nids++, tls12_md,
2645 sizeof(tls12_md) / sizeof(tls12_lookup));
2646 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2647 sizeof(tls12_sig) / sizeof(tls12_lookup));
Adam Langley95c29f32014-06-20 12:00:00 -07002648
Adam Langleyfcf25832014-12-18 17:42:32 -08002649 if (rhash == -1 || rsign == -1) {
2650 goto err;
2651 }
2652 *sptr++ = rhash;
2653 *sptr++ = rsign;
2654 }
2655
2656 if (client) {
2657 if (c->client_sigalgs) {
2658 OPENSSL_free(c->client_sigalgs);
2659 }
2660 c->client_sigalgs = sigalgs;
2661 c->client_sigalgslen = salglen;
2662 } else {
2663 if (c->conf_sigalgs) {
2664 OPENSSL_free(c->conf_sigalgs);
2665 }
2666 c->conf_sigalgs = sigalgs;
2667 c->conf_sigalgslen = salglen;
2668 }
2669
2670 return 1;
2671
2672err:
2673 OPENSSL_free(sigalgs);
2674 return 0;
2675}