blob: 9b212a53b294a34d4e047ed610f45e8b98ae5c37 [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,
131 tls1_mac,
David Benjamin41ac9792014-12-23 10:41:06 -0500132 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800133 tls1_setup_key_block,
134 tls1_generate_master_secret,
135 tls1_change_cipher_state,
136 tls1_final_finish_mac,
137 TLS1_FINISH_MAC_LENGTH,
138 tls1_cert_verify_mac,
139 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
140 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
141 tls1_alert_code,
142 tls1_export_keying_material,
143 0,
144 SSL3_HM_HEADER_LENGTH,
145 ssl3_set_handshake_header,
146 ssl3_handshake_write,
147};
Adam Langley95c29f32014-06-20 12:00:00 -0700148
David Benjamin338fcaf2014-12-11 01:20:52 -0500149const SSL3_ENC_METHOD TLSv1_1_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800150 tls1_enc,
151 tls1_mac,
David Benjamin41ac9792014-12-23 10:41:06 -0500152 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800153 tls1_setup_key_block,
154 tls1_generate_master_secret,
155 tls1_change_cipher_state,
156 tls1_final_finish_mac,
157 TLS1_FINISH_MAC_LENGTH,
158 tls1_cert_verify_mac,
159 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
160 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
161 tls1_alert_code,
162 tls1_export_keying_material,
163 SSL_ENC_FLAG_EXPLICIT_IV,
164 SSL3_HM_HEADER_LENGTH,
165 ssl3_set_handshake_header,
166 ssl3_handshake_write,
167};
Adam Langley95c29f32014-06-20 12:00:00 -0700168
David Benjamin338fcaf2014-12-11 01:20:52 -0500169const SSL3_ENC_METHOD TLSv1_2_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800170 tls1_enc,
171 tls1_mac,
David Benjamin41ac9792014-12-23 10:41:06 -0500172 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 tls1_setup_key_block,
174 tls1_generate_master_secret,
175 tls1_change_cipher_state,
176 tls1_final_finish_mac,
177 TLS1_FINISH_MAC_LENGTH,
178 tls1_cert_verify_mac,
179 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
180 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
181 tls1_alert_code,
182 tls1_export_keying_material,
183 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
184 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
185 SSL3_HM_HEADER_LENGTH,
186 ssl3_set_handshake_header,
187 ssl3_handshake_write,
188};
Adam Langley95c29f32014-06-20 12:00:00 -0700189
Adam Langleyfcf25832014-12-18 17:42:32 -0800190static int compare_uint16_t(const void *p1, const void *p2) {
191 uint16_t u1 = *((const uint16_t *)p1);
192 uint16_t u2 = *((const uint16_t *)p2);
193 if (u1 < u2) {
194 return -1;
195 } else if (u1 > u2) {
196 return 1;
197 } else {
198 return 0;
199 }
200}
David Benjamin35a7a442014-07-05 00:23:20 -0400201
Adam Langleyfcf25832014-12-18 17:42:32 -0800202/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
203 * more than one extension of the same type in a ClientHello or ServerHello.
204 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400205 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800206static int tls1_check_duplicate_extensions(const CBS *cbs) {
207 CBS extensions = *cbs;
208 size_t num_extensions = 0, i = 0;
209 uint16_t *extension_types = NULL;
210 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400211
Adam Langleyfcf25832014-12-18 17:42:32 -0800212 /* First pass: count the extensions. */
213 while (CBS_len(&extensions) > 0) {
214 uint16_t type;
215 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400216
Adam Langleyfcf25832014-12-18 17:42:32 -0800217 if (!CBS_get_u16(&extensions, &type) ||
218 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
219 goto done;
220 }
David Benjamin35a7a442014-07-05 00:23:20 -0400221
Adam Langleyfcf25832014-12-18 17:42:32 -0800222 num_extensions++;
223 }
David Benjamin35a7a442014-07-05 00:23:20 -0400224
Adam Langleyfcf25832014-12-18 17:42:32 -0800225 if (num_extensions == 0) {
226 return 1;
227 }
David Benjamin9a373592014-07-25 04:27:53 -0400228
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 extension_types =
230 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
231 if (extension_types == NULL) {
232 OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions,
233 ERR_R_MALLOC_FAILURE);
234 goto done;
235 }
David Benjamin35a7a442014-07-05 00:23:20 -0400236
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 /* Second pass: gather the extension types. */
238 extensions = *cbs;
239 for (i = 0; i < num_extensions; i++) {
240 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400241
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
243 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
244 /* This should not happen. */
245 goto done;
246 }
247 }
248 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400249
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 /* Sort the extensions and make sure there are no duplicates. */
251 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
252 for (i = 1; i < num_extensions; i++) {
253 if (extension_types[i - 1] == extension_types[i]) {
254 goto done;
255 }
256 }
David Benjamin35a7a442014-07-05 00:23:20 -0400257
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 ret = 1;
259
David Benjamin35a7a442014-07-05 00:23:20 -0400260done:
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 if (extension_types)
262 OPENSSL_free(extension_types);
263 return ret;
264}
David Benjamin35a7a442014-07-05 00:23:20 -0400265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
267 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400268
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700270
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 if (/* Skip client version. */
272 !CBS_skip(&client_hello, 2) ||
273 /* Skip client nonce. */
274 !CBS_skip(&client_hello, 32) ||
275 /* Extract session_id. */
276 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
277 return 0;
278 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700279
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 ctx->session_id = CBS_data(&session_id);
281 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700282
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 /* Skip past DTLS cookie */
284 if (SSL_IS_DTLS(ctx->ssl)) {
285 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700286
Adam Langleyfcf25832014-12-18 17:42:32 -0800287 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
288 return 0;
289 }
290 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700291
Adam Langleyfcf25832014-12-18 17:42:32 -0800292 /* Extract cipher_suites. */
293 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
294 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
295 return 0;
296 }
297 ctx->cipher_suites = CBS_data(&cipher_suites);
298 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700299
Adam Langleyfcf25832014-12-18 17:42:32 -0800300 /* Extract compression_methods. */
301 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
302 CBS_len(&compression_methods) < 1) {
303 return 0;
304 }
305 ctx->compression_methods = CBS_data(&compression_methods);
306 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700307
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 /* If the ClientHello ends here then it's valid, but doesn't have any
309 * extensions. (E.g. SSLv3.) */
310 if (CBS_len(&client_hello) == 0) {
311 ctx->extensions = NULL;
312 ctx->extensions_len = 0;
313 return 1;
314 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700315
Adam Langleyfcf25832014-12-18 17:42:32 -0800316 /* Extract extensions and check it is valid. */
317 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
318 !tls1_check_duplicate_extensions(&extensions) ||
319 CBS_len(&client_hello) != 0) {
320 return 0;
321 }
322 ctx->extensions = CBS_data(&extensions);
323 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700324
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 return 1;
326}
Adam Langleydc9b1412014-06-20 12:00:00 -0700327
Adam Langleyfcf25832014-12-18 17:42:32 -0800328char SSL_early_callback_ctx_extension_get(
329 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
330 const uint8_t **out_data, size_t *out_len) {
331 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700332
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700334
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 while (CBS_len(&extensions) != 0) {
336 uint16_t type;
337 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400338
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 /* Decode the next extension. */
340 if (!CBS_get_u16(&extensions, &type) ||
341 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
342 return 0;
343 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700344
Adam Langleyfcf25832014-12-18 17:42:32 -0800345 if (type == extension_type) {
346 *out_data = CBS_data(&extension);
347 *out_len = CBS_len(&extension);
348 return 1;
349 }
350 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700351
Adam Langleyfcf25832014-12-18 17:42:32 -0800352 return 0;
353}
Adam Langley95c29f32014-06-20 12:00:00 -0700354
David Benjamine518f652014-10-13 16:12:45 -0400355/* ECC curves from RFC4492 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800356static const int nid_list[] = {
357 NID_sect163k1, /* sect163k1 (1) */
358 NID_sect163r1, /* sect163r1 (2) */
359 NID_sect163r2, /* sect163r2 (3) */
360 NID_sect193r1, /* sect193r1 (4) */
361 NID_sect193r2, /* sect193r2 (5) */
362 NID_sect233k1, /* sect233k1 (6) */
363 NID_sect233r1, /* sect233r1 (7) */
364 NID_sect239k1, /* sect239k1 (8) */
365 NID_sect283k1, /* sect283k1 (9) */
366 NID_sect283r1, /* sect283r1 (10) */
367 NID_sect409k1, /* sect409k1 (11) */
368 NID_sect409r1, /* sect409r1 (12) */
369 NID_sect571k1, /* sect571k1 (13) */
370 NID_sect571r1, /* sect571r1 (14) */
371 NID_secp160k1, /* secp160k1 (15) */
372 NID_secp160r1, /* secp160r1 (16) */
373 NID_secp160r2, /* secp160r2 (17) */
374 NID_secp192k1, /* secp192k1 (18) */
375 NID_X9_62_prime192v1, /* secp192r1 (19) */
376 NID_secp224k1, /* secp224k1 (20) */
377 NID_secp224r1, /* secp224r1 (21) */
378 NID_secp256k1, /* secp256k1 (22) */
379 NID_X9_62_prime256v1, /* secp256r1 (23) */
380 NID_secp384r1, /* secp384r1 (24) */
381 NID_secp521r1, /* secp521r1 (25) */
382 NID_brainpoolP256r1, /* brainpoolP256r1 (26) */
383 NID_brainpoolP384r1, /* brainpoolP384r1 (27) */
384 NID_brainpoolP512r1 /* brainpool512r1 (28) */
385};
Adam Langley95c29f32014-06-20 12:00:00 -0700386
Adam Langleyfcf25832014-12-18 17:42:32 -0800387static const uint8_t ecformats_default[] = {
388 TLSEXT_ECPOINTFORMAT_uncompressed,
389};
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langleyfcf25832014-12-18 17:42:32 -0800391static const uint16_t eccurves_default[] = {
392 23, /* secp256r1 (23) */
393 24, /* secp384r1 (24) */
394 25, /* secp521r1 (25) */
395};
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langleyfcf25832014-12-18 17:42:32 -0800397int tls1_ec_curve_id2nid(uint16_t curve_id) {
398 if (curve_id < 1 || curve_id > sizeof(nid_list) / sizeof(nid_list[0])) {
399 return OBJ_undef;
400 }
401 return nid_list[curve_id - 1];
402}
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langleyfcf25832014-12-18 17:42:32 -0800404uint16_t tls1_ec_nid2curve_id(int nid) {
405 size_t i;
406 for (i = 0; i < sizeof(nid_list) / sizeof(nid_list[0]); i++) {
407 /* nid_list[i] stores the NID corresponding to curve ID i+1. */
408 if (nid == nid_list[i]) {
409 return i + 1;
410 }
411 }
David Benjamin072334d2014-07-13 16:24:27 -0400412
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 /* Use 0 for non-existent curve ID. Note: this assumes that curve ID 0 will
414 * never be allocated. */
415 return 0;
416}
417
418/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
419 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
420 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400421static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 const uint16_t **out_curve_ids,
423 size_t *out_curve_ids_len) {
424 if (get_peer_curves) {
425 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
426 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
427 return;
428 }
Adam Langley95c29f32014-06-20 12:00:00 -0700429
Adam Langleyfcf25832014-12-18 17:42:32 -0800430 *out_curve_ids = s->tlsext_ellipticcurvelist;
431 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
432 if (!*out_curve_ids) {
433 *out_curve_ids = eccurves_default;
434 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
435 }
436}
David Benjamined439582014-07-14 19:13:02 -0400437
Adam Langleyfcf25832014-12-18 17:42:32 -0800438int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
439 uint8_t curve_type;
440 uint16_t curve_id;
441 const uint16_t *curves;
442 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400443
Adam Langleyfcf25832014-12-18 17:42:32 -0800444 /* Only support named curves. */
445 if (!CBS_get_u8(cbs, &curve_type) ||
446 curve_type != NAMED_CURVE_TYPE ||
447 !CBS_get_u16(cbs, &curve_id)) {
448 return 0;
449 }
David Benjamined439582014-07-14 19:13:02 -0400450
Adam Langleyfcf25832014-12-18 17:42:32 -0800451 tls1_get_curvelist(s, 0, &curves, &curves_len);
452 for (i = 0; i < curves_len; i++) {
453 if (curve_id == curves[i]) {
454 *out_curve_id = curve_id;
455 return 1;
456 }
457 }
Adam Langley95c29f32014-06-20 12:00:00 -0700458
Adam Langleyfcf25832014-12-18 17:42:32 -0800459 return 0;
460}
David Benjamin072334d2014-07-13 16:24:27 -0400461
Adam Langleyfcf25832014-12-18 17:42:32 -0800462int tls1_get_shared_curve(SSL *s) {
463 const uint16_t *pref, *supp;
464 size_t preflen, supplen, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400465
Adam Langleyfcf25832014-12-18 17:42:32 -0800466 /* Can't do anything on client side */
467 if (s->server == 0) {
468 return NID_undef;
469 }
470
471 /* Return first preference shared curve */
472 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &supp,
473 &supplen);
474 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref,
475 &preflen);
476
477 for (i = 0; i < preflen; i++) {
478 for (j = 0; j < supplen; j++) {
479 if (pref[i] == supp[j]) {
480 return tls1_ec_curve_id2nid(pref[i]);
481 }
482 }
483 }
484
485 return NID_undef;
486}
Adam Langley95c29f32014-06-20 12:00:00 -0700487
David Benjamin072334d2014-07-13 16:24:27 -0400488/* NOTE: tls1_ec_curve_id2nid and tls1_set_curves assume that
489 *
490 * (a) 0 is not a valid curve ID.
491 *
492 * (b) The largest curve ID is 31.
493 *
494 * Those implementations must be revised before adding support for curve IDs
495 * that break these assumptions. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800496OPENSSL_COMPILE_ASSERT((sizeof(nid_list) / sizeof(nid_list[0])) < 32,
497 small_curve_ids);
David Benjamin072334d2014-07-13 16:24:27 -0400498
499int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800500 const int *curves, size_t ncurves) {
501 uint16_t *curve_ids;
502 size_t i;
503
504 /* Bitmap of curves included to detect duplicates: only works
505 * while curve ids < 32. */
506 uint32_t dup_list = 0;
507 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
508 if (curve_ids == NULL) {
509 return 0;
510 }
511
512 for (i = 0; i < ncurves; i++) {
513 uint32_t idmask;
514 uint16_t id;
515 id = tls1_ec_nid2curve_id(curves[i]);
516 idmask = ((uint32_t)1) << id;
517 if (!id || (dup_list & idmask)) {
518 OPENSSL_free(curve_ids);
519 return 0;
520 }
521 dup_list |= idmask;
522 curve_ids[i] = id;
523 }
524
525 if (*out_curve_ids) {
526 OPENSSL_free(*out_curve_ids);
527 }
528 *out_curve_ids = curve_ids;
529 *out_curve_ids_len = ncurves;
530
531 return 1;
532}
Adam Langley95c29f32014-06-20 12:00:00 -0700533
David Benjamin072334d2014-07-13 16:24:27 -0400534/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
535 * TLS curve ID and point format, respectively, for |ec|. It returns one on
536 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800537static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
538 uint8_t *out_comp_id, EC_KEY *ec) {
539 int nid;
540 uint16_t id;
541 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700542
Adam Langleyfcf25832014-12-18 17:42:32 -0800543 if (ec == NULL) {
544 return 0;
545 }
Adam Langley95c29f32014-06-20 12:00:00 -0700546
Adam Langleyfcf25832014-12-18 17:42:32 -0800547 grp = EC_KEY_get0_group(ec);
548 if (grp == NULL) {
549 return 0;
550 }
David Benjamin072334d2014-07-13 16:24:27 -0400551
Adam Langleyfcf25832014-12-18 17:42:32 -0800552 /* Determine curve ID */
553 nid = EC_GROUP_get_curve_name(grp);
554 id = tls1_ec_nid2curve_id(nid);
555 if (!id) {
556 return 0;
557 }
David Benjamin072334d2014-07-13 16:24:27 -0400558
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
560 *out_curve_id = id;
561
562 if (out_comp_id) {
563 if (EC_KEY_get0_public_key(ec) == NULL) {
564 return 0;
565 }
566 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
567 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
568 } else {
569 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
570 }
571 }
572
573 return 1;
574}
David Benjamin072334d2014-07-13 16:24:27 -0400575
David Benjamin42e9a772014-09-02 23:18:44 -0400576/* tls1_check_point_format returns one if |comp_id| is consistent with the
577 * peer's point format preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800578static int tls1_check_point_format(SSL *s, uint8_t comp_id) {
579 uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
580 size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
581 size_t i;
David Benjamin42e9a772014-09-02 23:18:44 -0400582
Adam Langleyfcf25832014-12-18 17:42:32 -0800583 /* If point formats extension present check it, otherwise everything is
584 * supported (see RFC4492). */
585 if (p == NULL) {
586 return 1;
587 }
David Benjamin42e9a772014-09-02 23:18:44 -0400588
Adam Langleyfcf25832014-12-18 17:42:32 -0800589 for (i = 0; i < plen; i++) {
590 if (comp_id == p[i]) {
591 return 1;
592 }
593 }
David Benjamin42e9a772014-09-02 23:18:44 -0400594
Adam Langleyfcf25832014-12-18 17:42:32 -0800595 return 0;
596}
597
598/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
599 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400600 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800601static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
602 const uint16_t *curves;
603 size_t curves_len, i, j;
David Benjamin42e9a772014-09-02 23:18:44 -0400604
Adam Langleyfcf25832014-12-18 17:42:32 -0800605 /* Check against our list, then the peer's list. */
606 for (j = 0; j <= 1; j++) {
607 tls1_get_curvelist(s, j, &curves, &curves_len);
608 for (i = 0; i < curves_len; i++) {
609 if (curves[i] == curve_id) {
610 break;
611 }
612 }
Adam Langley95c29f32014-06-20 12:00:00 -0700613
Adam Langleyfcf25832014-12-18 17:42:32 -0800614 if (i == curves_len) {
615 return 0;
616 }
Adam Langley95c29f32014-06-20 12:00:00 -0700617
Adam Langleyfcf25832014-12-18 17:42:32 -0800618 /* Servers do not present a preference list so, if we are a client, only
619 * check our list. */
620 if (!s->server) {
621 return 1;
622 }
623 }
David Benjamin033e5f42014-11-13 18:47:41 -0500624
Adam Langleyfcf25832014-12-18 17:42:32 -0800625 return 1;
626}
David Benjamin033e5f42014-11-13 18:47:41 -0500627
Adam Langleyfcf25832014-12-18 17:42:32 -0800628static void tls1_get_formatlist(SSL *s, const uint8_t **pformats,
629 size_t *pformatslen) {
630 /* If we have a custom point format list use it otherwise use default */
631 if (s->tlsext_ecpointformatlist) {
632 *pformats = s->tlsext_ecpointformatlist;
633 *pformatslen = s->tlsext_ecpointformatlist_length;
634 } else {
635 *pformats = ecformats_default;
636 *pformatslen = sizeof(ecformats_default);
637 }
638}
639
640int tls1_check_ec_cert(SSL *s, X509 *x) {
641 int ret = 0;
642 EVP_PKEY *pkey = X509_get_pubkey(x);
643 uint16_t curve_id;
644 uint8_t comp_id;
645
646 if (!pkey ||
647 pkey->type != EVP_PKEY_EC ||
648 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
649 !tls1_check_curve_id(s, curve_id) ||
650 !tls1_check_point_format(s, comp_id)) {
651 goto done;
652 }
653
654 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500655
656done:
Adam Langleyfcf25832014-12-18 17:42:32 -0800657 if (pkey) {
658 EVP_PKEY_free(pkey);
659 }
660 return ret;
661}
David Benjamin42e9a772014-09-02 23:18:44 -0400662
Adam Langleyfcf25832014-12-18 17:42:32 -0800663int tls1_check_ec_tmp_key(SSL *s) {
664 uint16_t curve_id;
665 EC_KEY *ec = s->cert->ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -0700666
Adam Langleyfcf25832014-12-18 17:42:32 -0800667 if (s->cert->ecdh_tmp_auto) {
668 /* Need a shared curve */
669 return tls1_get_shared_curve(s) != NID_undef;
670 }
Adam Langley95c29f32014-06-20 12:00:00 -0700671
Adam Langleyfcf25832014-12-18 17:42:32 -0800672 if (!ec) {
673 if (s->cert->ecdh_tmp_cb) {
674 return 1;
675 }
676 return 0;
677 }
678
679 return tls1_curve_params_from_ec_key(&curve_id, NULL, ec) &&
680 tls1_check_curve_id(s, curve_id);
681}
Adam Langley95c29f32014-06-20 12:00:00 -0700682
683/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800684 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700685
Adam Langley95c29f32014-06-20 12:00:00 -0700686#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700687
Adam Langley95c29f32014-06-20 12:00:00 -0700688#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700689
Adam Langleyfcf25832014-12-18 17:42:32 -0800690#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700691
David Benjamincff64722014-08-19 19:54:46 -0400692static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800693 tlsext_sigalg(TLSEXT_hash_sha512)
694 tlsext_sigalg(TLSEXT_hash_sha384)
695 tlsext_sigalg(TLSEXT_hash_sha256)
696 tlsext_sigalg(TLSEXT_hash_sha224)
697 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700698};
David Benjamin05da6e12014-07-12 20:42:55 -0400699
Adam Langleyfcf25832014-12-18 17:42:32 -0800700size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
701 /* If server use client authentication sigalgs if not NULL */
702 if (s->server && s->cert->client_sigalgs) {
703 *psigs = s->cert->client_sigalgs;
704 return s->cert->client_sigalgslen;
705 } else if (s->cert->conf_sigalgs) {
706 *psigs = s->cert->conf_sigalgs;
707 return s->cert->conf_sigalgslen;
708 } else {
709 *psigs = tls12_sigalgs;
710 return sizeof(tls12_sigalgs);
711 }
712}
Adam Langley95c29f32014-06-20 12:00:00 -0700713
Adam Langleyfcf25832014-12-18 17:42:32 -0800714/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
715 * checks it is consistent with |s|'s sent supported signature algorithms and,
716 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
717 * returns 0 and writes an alert into |*out_alert|. */
718int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
719 CBS *cbs, EVP_PKEY *pkey) {
720 const uint8_t *sent_sigs;
721 size_t sent_sigslen, i;
722 int sigalg = tls12_get_sigid(pkey);
723 uint8_t hash, signature;
724
725 /* Should never happen */
726 if (sigalg == -1) {
727 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
728 *out_alert = SSL_AD_INTERNAL_ERROR;
729 return 0;
730 }
731
732 if (!CBS_get_u8(cbs, &hash) ||
733 !CBS_get_u8(cbs, &signature)) {
734 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
735 *out_alert = SSL_AD_DECODE_ERROR;
736 return 0;
737 }
738
739 /* Check key type is consistent with signature */
740 if (sigalg != signature) {
741 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
742 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
743 return 0;
744 }
745
746 if (pkey->type == EVP_PKEY_EC) {
747 uint16_t curve_id;
748 uint8_t comp_id;
749 /* Check compression and curve matches extensions */
750 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
751 *out_alert = SSL_AD_INTERNAL_ERROR;
752 return 0;
753 }
754
755 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
756 !tls1_check_point_format(s, comp_id))) {
757 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
758 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
759 return 0;
760 }
761 }
762
763 /* Check signature matches a type we sent */
764 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
765 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
766 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
767 break;
768 }
769 }
770
771 /* Allow fallback to SHA-1. */
772 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
773 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
774 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
775 return 0;
776 }
777
778 *out_md = tls12_get_hash(hash);
779 if (*out_md == NULL) {
780 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
781 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
782 return 0;
783 }
784
785 return 1;
786}
787
788/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
789 * supported or doesn't appear in supported signature algorithms. Unlike
790 * ssl_cipher_get_disabled this applies to a specific session and not global
791 * settings. */
792void ssl_set_client_disabled(SSL *s) {
793 CERT *c = s->cert;
794 const uint8_t *sigalgs;
795 size_t i, sigalgslen;
796 int have_rsa = 0, have_ecdsa = 0;
797 c->mask_a = 0;
798 c->mask_k = 0;
799
800 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
801 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
802 c->mask_ssl = SSL_TLSV1_2;
803 } else {
804 c->mask_ssl = 0;
805 }
806
807 /* Now go through all signature algorithms seeing if we support any for RSA,
808 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
809 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
810 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
811 switch (sigalgs[1]) {
812 case TLSEXT_signature_rsa:
813 have_rsa = 1;
814 break;
815
816 case TLSEXT_signature_ecdsa:
817 have_ecdsa = 1;
818 break;
819 }
820 }
821
822 /* Disable auth if we don't include any appropriate signature algorithms. */
823 if (!have_rsa) {
824 c->mask_a |= SSL_aRSA;
825 }
826 if (!have_ecdsa) {
827 c->mask_a |= SSL_aECDSA;
828 }
829
830 /* with PSK there must be client callback set */
831 if (!s->psk_client_callback) {
832 c->mask_a |= SSL_aPSK;
833 c->mask_k |= SSL_kPSK;
834 }
835}
Adam Langley95c29f32014-06-20 12:00:00 -0700836
Adam Langleyb0c235e2014-06-20 12:00:00 -0700837/* header_len is the length of the ClientHello header written so far, used to
838 * compute padding. It does not include the record header. Pass 0 if no padding
839 * is to be done. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800840uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit,
841 size_t header_len) {
842 int extdatalen = 0;
843 uint8_t *ret = buf;
844 uint8_t *orig = buf;
845 /* See if we support any ECC ciphersuites */
846 int using_ecc = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700847
Adam Langleyfcf25832014-12-18 17:42:32 -0800848 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
849 size_t i;
850 unsigned long alg_k, alg_a;
851 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
854 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700855
Adam Langleyfcf25832014-12-18 17:42:32 -0800856 alg_k = c->algorithm_mkey;
857 alg_a = c->algorithm_auth;
858 if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) {
859 using_ecc = 1;
860 break;
861 }
862 }
863 }
Adam Langley95c29f32014-06-20 12:00:00 -0700864
Adam Langleyfcf25832014-12-18 17:42:32 -0800865 /* don't add extensions for SSLv3 unless doing secure renegotiation */
866 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
867 return orig;
868 }
Adam Langley95c29f32014-06-20 12:00:00 -0700869
Adam Langleyfcf25832014-12-18 17:42:32 -0800870 ret += 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700871
Adam Langleyfcf25832014-12-18 17:42:32 -0800872 if (ret >= limit) {
873 return NULL; /* should never occur. */
874 }
Adam Langley95c29f32014-06-20 12:00:00 -0700875
Adam Langleyfcf25832014-12-18 17:42:32 -0800876 if (s->tlsext_hostname != NULL) {
877 /* Add TLS extension servername to the Client Hello message */
878 unsigned long size_str;
879 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -0700880
Adam Langleyfcf25832014-12-18 17:42:32 -0800881 /* check for enough space.
882 4 for the servername type and entension length
883 2 for servernamelist length
884 1 for the hostname type
885 2 for hostname length
886 + hostname length */
Adam Langley95c29f32014-06-20 12:00:00 -0700887
Adam Langleyfcf25832014-12-18 17:42:32 -0800888 lenmax = limit - ret - 9;
889 size_str = strlen(s->tlsext_hostname);
890 if (lenmax < 0 || size_str > (unsigned long)lenmax) {
891 return NULL;
892 }
Adam Langley95c29f32014-06-20 12:00:00 -0700893
Adam Langleyfcf25832014-12-18 17:42:32 -0800894 /* extension type and length */
895 s2n(TLSEXT_TYPE_server_name, ret);
896 s2n(size_str + 5, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700897
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 /* length of servername list */
899 s2n(size_str + 3, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700900
Adam Langleyfcf25832014-12-18 17:42:32 -0800901 /* hostname type, length and hostname */
902 *(ret++) = (uint8_t)TLSEXT_NAMETYPE_host_name;
903 s2n(size_str, ret);
904 memcpy(ret, s->tlsext_hostname, size_str);
905 ret += size_str;
906 }
Adam Langley75712922014-10-10 16:23:43 -0700907
Adam Langleyfcf25832014-12-18 17:42:32 -0800908 /* Add RI if renegotiating */
909 if (s->renegotiate) {
910 int el;
David Benjamind1681e62014-11-20 14:54:14 -0500911
Adam Langleyfcf25832014-12-18 17:42:32 -0800912 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
913 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
914 return NULL;
915 }
Adam Langley95c29f32014-06-20 12:00:00 -0700916
Adam Langleyfcf25832014-12-18 17:42:32 -0800917 if ((limit - ret - 4 - el) < 0) {
918 return NULL;
919 }
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921 s2n(TLSEXT_TYPE_renegotiate, ret);
922 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700923
Adam Langleyfcf25832014-12-18 17:42:32 -0800924 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
925 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
926 return NULL;
927 }
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langleyfcf25832014-12-18 17:42:32 -0800929 ret += el;
930 }
Adam Langley95c29f32014-06-20 12:00:00 -0700931
Adam Langleyfcf25832014-12-18 17:42:32 -0800932 /* Add extended master secret. */
933 if (s->version != SSL3_VERSION) {
934 if (limit - ret - 4 < 0) {
935 return NULL;
936 }
937 s2n(TLSEXT_TYPE_extended_master_secret, ret);
938 s2n(0, ret);
939 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200940
Adam Langleyfcf25832014-12-18 17:42:32 -0800941 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
942 int ticklen = 0;
943 if (!s->new_session && s->session && s->session->tlsext_tick) {
944 ticklen = s->session->tlsext_ticklen;
945 }
Adam Langley95c29f32014-06-20 12:00:00 -0700946
Adam Langleyfcf25832014-12-18 17:42:32 -0800947 /* Check for enough room 2 for extension type, 2 for len rest for
948 * ticket. */
949 if ((long)(limit - ret - 4 - ticklen) < 0) {
950 return NULL;
951 }
952 s2n(TLSEXT_TYPE_session_ticket, ret);
953 s2n(ticklen, ret);
954 if (ticklen) {
955 memcpy(ret, s->session->tlsext_tick, ticklen);
956 ret += ticklen;
957 }
958 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700959
Adam Langleyfcf25832014-12-18 17:42:32 -0800960 if (SSL_USE_SIGALGS(s)) {
961 size_t salglen;
962 const uint8_t *salg;
963 salglen = tls12_get_psigalgs(s, &salg);
964 if ((size_t)(limit - ret) < salglen + 6) {
965 return NULL;
966 }
967 s2n(TLSEXT_TYPE_signature_algorithms, ret);
968 s2n(salglen + 2, ret);
969 s2n(salglen, ret);
970 memcpy(ret, salg, salglen);
971 ret += salglen;
972 }
Adam Langley95c29f32014-06-20 12:00:00 -0700973
Adam Langleyfcf25832014-12-18 17:42:32 -0800974 if (s->ocsp_stapling_enabled) {
975 /* The status_request extension is excessively extensible at every layer.
976 * On the client, only support requesting OCSP responses with an empty
977 * responder_id_list and no extensions. */
978 if (limit - ret - 4 - 1 - 2 - 2 < 0) {
979 return NULL;
980 }
Adam Langley95c29f32014-06-20 12:00:00 -0700981
Adam Langleyfcf25832014-12-18 17:42:32 -0800982 s2n(TLSEXT_TYPE_status_request, ret);
983 s2n(1 + 2 + 2, ret);
984 /* status_type */
985 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
986 /* responder_id_list - empty */
987 s2n(0, ret);
988 /* request_extensions - empty */
989 s2n(0, ret);
990 }
Adam Langley95c29f32014-06-20 12:00:00 -0700991
Adam Langleyfcf25832014-12-18 17:42:32 -0800992 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
993 /* The client advertises an emtpy extension to indicate its support for
994 * Next Protocol Negotiation */
995 if (limit - ret - 4 < 0) {
996 return NULL;
997 }
998 s2n(TLSEXT_TYPE_next_proto_neg, ret);
999 s2n(0, ret);
1000 }
Adam Langley95c29f32014-06-20 12:00:00 -07001001
Adam Langleyfcf25832014-12-18 17:42:32 -08001002 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len) {
1003 /* The client advertises an empty extension to indicate its support for
1004 * certificate timestamps. */
1005 if (limit - ret - 4 < 0) {
1006 return NULL;
1007 }
1008 s2n(TLSEXT_TYPE_certificate_timestamp, ret);
1009 s2n(0, ret);
1010 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001011
Adam Langleyfcf25832014-12-18 17:42:32 -08001012 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
1013 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) {
1014 return NULL;
1015 }
1016 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1017 s2n(2 + s->alpn_client_proto_list_len, ret);
1018 s2n(s->alpn_client_proto_list_len, ret);
1019 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
1020 ret += s->alpn_client_proto_list_len;
1021 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001022
Adam Langleyfcf25832014-12-18 17:42:32 -08001023 if (s->tlsext_channel_id_enabled) {
1024 /* The client advertises an emtpy extension to indicate its support for
1025 * Channel ID. */
1026 if (limit - ret - 4 < 0) {
1027 return NULL;
1028 }
1029 if (s->ctx->tlsext_channel_id_enabled_new) {
1030 s2n(TLSEXT_TYPE_channel_id_new, ret);
1031 } else {
1032 s2n(TLSEXT_TYPE_channel_id, ret);
1033 }
1034 s2n(0, ret);
1035 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001036
Adam Langleyfcf25832014-12-18 17:42:32 -08001037 if (SSL_get_srtp_profiles(s)) {
1038 int el;
Adam Langleyc3174b72014-06-20 12:00:00 -07001039
Adam Langleyfcf25832014-12-18 17:42:32 -08001040 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
Adam Langleyc3174b72014-06-20 12:00:00 -07001041
Adam Langleyfcf25832014-12-18 17:42:32 -08001042 if ((limit - ret - 4 - el) < 0) {
1043 return NULL;
1044 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001045
Adam Langleyfcf25832014-12-18 17:42:32 -08001046 s2n(TLSEXT_TYPE_use_srtp, ret);
1047 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001048
Adam Langleyfcf25832014-12-18 17:42:32 -08001049 if (!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1050 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1051 return NULL;
1052 }
1053 ret += el;
1054 }
Adam Langley95c29f32014-06-20 12:00:00 -07001055
Adam Langleyfcf25832014-12-18 17:42:32 -08001056 if (using_ecc) {
1057 /* Add TLS extension ECPointFormats to the ClientHello message */
1058 long lenmax;
1059 const uint8_t *formats;
1060 const uint16_t *curves;
1061 size_t formats_len, curves_len, i;
Adam Langley95c29f32014-06-20 12:00:00 -07001062
Adam Langleyfcf25832014-12-18 17:42:32 -08001063 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001064
Adam Langleyfcf25832014-12-18 17:42:32 -08001065 lenmax = limit - ret - 5;
1066 if (lenmax < 0) {
1067 return NULL;
1068 }
1069 if (formats_len > (size_t)lenmax) {
1070 return NULL;
1071 }
1072 if (formats_len > 255) {
1073 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1074 return NULL;
1075 }
Adam Langley95c29f32014-06-20 12:00:00 -07001076
Adam Langleyfcf25832014-12-18 17:42:32 -08001077 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1078 s2n(formats_len + 1, ret);
1079 *(ret++) = (uint8_t)formats_len;
1080 memcpy(ret, formats, formats_len);
1081 ret += formats_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001082
Adam Langleyfcf25832014-12-18 17:42:32 -08001083 /* Add TLS extension EllipticCurves to the ClientHello message */
1084 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001085
Adam Langleyfcf25832014-12-18 17:42:32 -08001086 lenmax = limit - ret - 6;
1087 if (lenmax < 0) {
1088 return NULL;
1089 }
1090 if (curves_len * 2 > (size_t)lenmax) {
1091 return NULL;
1092 }
1093 if (curves_len * 2 > 65532) {
1094 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1095 return NULL;
1096 }
Adam Langley95c29f32014-06-20 12:00:00 -07001097
Adam Langleyfcf25832014-12-18 17:42:32 -08001098 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1099 s2n((curves_len * 2) + 2, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001100
Adam Langleyfcf25832014-12-18 17:42:32 -08001101 s2n(curves_len * 2, ret);
1102 for (i = 0; i < curves_len; i++) {
1103 s2n(curves[i], ret);
1104 }
1105 }
Adam Langley95c29f32014-06-20 12:00:00 -07001106
Adam Langleyfcf25832014-12-18 17:42:32 -08001107 if (header_len > 0) {
1108 size_t clienthello_minsize = 0;
1109 header_len += ret - orig;
1110 if (header_len > 0xff && header_len < 0x200) {
1111 /* Add padding to workaround bugs in F5 terminators. See
1112 * https://tools.ietf.org/html/draft-agl-tls-padding-03
1113 *
1114 * NB: because this code works out the length of all existing extensions
1115 * it MUST always appear last. */
1116 clienthello_minsize = 0x200;
1117 }
1118 if (s->fastradio_padding) {
1119 /* Pad the ClientHello record to 1024 bytes to fast forward the radio
1120 * into DCH (high data rate) state in 3G networks. Note that when
1121 * fastradio_padding is enabled, even if the header_len is less than 255
1122 * bytes, the padding will be applied regardless. This is slightly
1123 * different from the TLS padding extension suggested in
1124 * https://tools.ietf.org/html/draft-agl-tls-padding-03 */
1125 clienthello_minsize = 0x400;
1126 }
1127 if (header_len < clienthello_minsize) {
1128 size_t padding_len = clienthello_minsize - header_len;
1129 /* Extensions take at least four bytes to encode. Always include least
1130 * one byte of data if including the extension. WebSphere Application
1131 * Server 7.0 is intolerant to the last extension being zero-length. */
1132 if (padding_len >= 4 + 1) {
1133 padding_len -= 4;
1134 } else {
1135 padding_len = 1;
1136 }
Adam Langley95c29f32014-06-20 12:00:00 -07001137
Adam Langleyfcf25832014-12-18 17:42:32 -08001138 if (limit - ret - 4 - (long)padding_len < 0) {
1139 return NULL;
1140 }
Adam Langley75712922014-10-10 16:23:43 -07001141
Adam Langleyfcf25832014-12-18 17:42:32 -08001142 s2n(TLSEXT_TYPE_padding, ret);
1143 s2n(padding_len, ret);
1144 memset(ret, 0, padding_len);
1145 ret += padding_len;
1146 }
1147 }
Adam Langley75712922014-10-10 16:23:43 -07001148
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 extdatalen = ret - orig - 2;
1150 if (extdatalen == 0) {
1151 return orig;
1152 }
Adam Langley95c29f32014-06-20 12:00:00 -07001153
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 s2n(extdatalen, orig);
1155 return ret;
1156}
Adam Langley95c29f32014-06-20 12:00:00 -07001157
Adam Langleyfcf25832014-12-18 17:42:32 -08001158uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit) {
1159 int extdatalen = 0;
1160 uint8_t *orig = buf;
1161 uint8_t *ret = buf;
1162 int next_proto_neg_seen;
1163 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1164 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1165 int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
1166 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001167
Adam Langleyfcf25832014-12-18 17:42:32 -08001168 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1169 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
1170 return orig;
1171 }
Adam Langley95c29f32014-06-20 12:00:00 -07001172
Adam Langleyfcf25832014-12-18 17:42:32 -08001173 ret += 2;
1174 if (ret >= limit) {
1175 return NULL; /* should never happen. */
1176 }
Adam Langley95c29f32014-06-20 12:00:00 -07001177
Adam Langleyfcf25832014-12-18 17:42:32 -08001178 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL) {
1179 if ((long)(limit - ret - 4) < 0) {
1180 return NULL;
1181 }
Adam Langley95c29f32014-06-20 12:00:00 -07001182
Adam Langleyfcf25832014-12-18 17:42:32 -08001183 s2n(TLSEXT_TYPE_server_name, ret);
1184 s2n(0, ret);
1185 }
Adam Langley95c29f32014-06-20 12:00:00 -07001186
Adam Langleyfcf25832014-12-18 17:42:32 -08001187 if (s->s3->send_connection_binding) {
1188 int el;
Adam Langley95c29f32014-06-20 12:00:00 -07001189
Adam Langleyfcf25832014-12-18 17:42:32 -08001190 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1191 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1192 return NULL;
1193 }
Adam Langley95c29f32014-06-20 12:00:00 -07001194
Adam Langleyfcf25832014-12-18 17:42:32 -08001195 if ((limit - ret - 4 - el) < 0) {
1196 return NULL;
1197 }
Adam Langley95c29f32014-06-20 12:00:00 -07001198
Adam Langleyfcf25832014-12-18 17:42:32 -08001199 s2n(TLSEXT_TYPE_renegotiate, ret);
1200 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001201
Adam Langleyfcf25832014-12-18 17:42:32 -08001202 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1203 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1204 return NULL;
1205 }
Adam Langley95c29f32014-06-20 12:00:00 -07001206
Adam Langleyfcf25832014-12-18 17:42:32 -08001207 ret += el;
1208 }
Adam Langley95c29f32014-06-20 12:00:00 -07001209
Adam Langleyfcf25832014-12-18 17:42:32 -08001210 if (s->s3->tmp.extended_master_secret) {
1211 if ((long)(limit - ret - 4) < 0) {
1212 return NULL;
1213 }
Adam Langley95c29f32014-06-20 12:00:00 -07001214
Adam Langleyfcf25832014-12-18 17:42:32 -08001215 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1216 s2n(0, ret);
1217 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001218
Adam Langleyfcf25832014-12-18 17:42:32 -08001219 if (using_ecc) {
1220 const uint8_t *plist;
1221 size_t plistlen;
1222 /* Add TLS extension ECPointFormats to the ServerHello message */
1223 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -07001224
Adam Langleyfcf25832014-12-18 17:42:32 -08001225 tls1_get_formatlist(s, &plist, &plistlen);
1226
1227 lenmax = limit - ret - 5;
1228 if (lenmax < 0) {
1229 return NULL;
1230 }
1231 if (plistlen > (size_t)lenmax) {
1232 return NULL;
1233 }
1234 if (plistlen > 255) {
1235 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1236 return NULL;
1237 }
1238
1239 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1240 s2n(plistlen + 1, ret);
1241 *(ret++) = (uint8_t)plistlen;
1242 memcpy(ret, plist, plistlen);
1243 ret += plistlen;
1244 }
1245 /* Currently the server should not respond with a SupportedCurves extension */
1246
1247 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1248 if ((long)(limit - ret - 4) < 0) {
1249 return NULL;
1250 }
1251 s2n(TLSEXT_TYPE_session_ticket, ret);
1252 s2n(0, ret);
1253 }
1254
1255 if (s->s3->tmp.certificate_status_expected) {
1256 if ((long)(limit - ret - 4) < 0) {
1257 return NULL;
1258 }
1259 s2n(TLSEXT_TYPE_status_request, ret);
1260 s2n(0, ret);
1261 }
1262
1263 if (s->srtp_profile) {
1264 int el;
1265
1266 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1267
1268 if ((limit - ret - 4 - el) < 0) {
1269 return NULL;
1270 }
1271
1272 s2n(TLSEXT_TYPE_use_srtp, ret);
1273 s2n(el, ret);
1274
1275 if (!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1276 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1277 return NULL;
1278 }
1279 ret += el;
1280 }
1281
1282 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1283 s->s3->next_proto_neg_seen = 0;
1284 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1285 const uint8_t *npa;
1286 unsigned int npalen;
1287 int r;
1288
1289 r = s->ctx->next_protos_advertised_cb(
1290 s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1291 if (r == SSL_TLSEXT_ERR_OK) {
1292 if ((long)(limit - ret - 4 - npalen) < 0) {
1293 return NULL;
1294 }
1295 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1296 s2n(npalen, ret);
1297 memcpy(ret, npa, npalen);
1298 ret += npalen;
1299 s->s3->next_proto_neg_seen = 1;
1300 }
1301 }
1302
1303 if (s->s3->alpn_selected) {
1304 const uint8_t *selected = s->s3->alpn_selected;
1305 size_t len = s->s3->alpn_selected_len;
1306
1307 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) {
1308 return NULL;
1309 }
1310 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1311 s2n(3 + len, ret);
1312 s2n(1 + len, ret);
1313 *ret++ = len;
1314 memcpy(ret, selected, len);
1315 ret += len;
1316 }
1317
1318 /* If the client advertised support for Channel ID, and we have it
1319 * enabled, then we want to echo it back. */
1320 if (s->s3->tlsext_channel_id_valid) {
1321 if (limit - ret - 4 < 0) {
1322 return NULL;
1323 }
1324 if (s->s3->tlsext_channel_id_new) {
1325 s2n(TLSEXT_TYPE_channel_id_new, ret);
1326 } else {
1327 s2n(TLSEXT_TYPE_channel_id, ret);
1328 }
1329 s2n(0, ret);
1330 }
1331
1332 extdatalen = ret - orig - 2;
1333 if (extdatalen == 0) {
1334 return orig;
1335 }
1336
1337 s2n(extdatalen, orig);
1338 return ret;
1339}
Adam Langley95c29f32014-06-20 12:00:00 -07001340
Adam Langley95c29f32014-06-20 12:00:00 -07001341/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1342 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001343 * cbs: the contents of the extension, not including the type and length.
1344 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001345 * return.
1346 *
David Benjamindc72ff72014-06-25 12:36:10 -04001347 * returns: 1 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001348static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert) {
1349 CBS protocol_name_list, protocol_name_list_copy;
1350 const uint8_t *selected;
1351 uint8_t selected_len;
1352 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07001353
Adam Langleyfcf25832014-12-18 17:42:32 -08001354 if (s->ctx->alpn_select_cb == NULL) {
1355 return 1;
1356 }
Adam Langley95c29f32014-06-20 12:00:00 -07001357
Adam Langleyfcf25832014-12-18 17:42:32 -08001358 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1359 CBS_len(cbs) != 0 || CBS_len(&protocol_name_list) < 2) {
1360 goto parse_error;
1361 }
Adam Langley95c29f32014-06-20 12:00:00 -07001362
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 /* Validate the protocol list. */
1364 protocol_name_list_copy = protocol_name_list;
1365 while (CBS_len(&protocol_name_list_copy) > 0) {
1366 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001367
Adam Langleyfcf25832014-12-18 17:42:32 -08001368 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name)) {
1369 goto parse_error;
1370 }
1371 }
Adam Langley95c29f32014-06-20 12:00:00 -07001372
Adam Langleyfcf25832014-12-18 17:42:32 -08001373 r = s->ctx->alpn_select_cb(
1374 s, &selected, &selected_len, CBS_data(&protocol_name_list),
1375 CBS_len(&protocol_name_list), s->ctx->alpn_select_cb_arg);
1376 if (r == SSL_TLSEXT_ERR_OK) {
1377 if (s->s3->alpn_selected) {
1378 OPENSSL_free(s->s3->alpn_selected);
1379 }
1380 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
1381 if (!s->s3->alpn_selected) {
1382 *out_alert = SSL_AD_INTERNAL_ERROR;
1383 return 0;
1384 }
1385 s->s3->alpn_selected_len = selected_len;
1386 }
1387
1388 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001389
1390parse_error:
Adam Langleyfcf25832014-12-18 17:42:32 -08001391 *out_alert = SSL_AD_DECODE_ERROR;
1392 return 0;
1393}
Adam Langley95c29f32014-06-20 12:00:00 -07001394
Adam Langleyfcf25832014-12-18 17:42:32 -08001395static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1396 int renegotiate_seen = 0;
1397 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001398
Adam Langleyfcf25832014-12-18 17:42:32 -08001399 s->should_ack_sni = 0;
1400 s->s3->next_proto_neg_seen = 0;
1401 s->s3->tmp.certificate_status_expected = 0;
1402 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001403
Adam Langleyfcf25832014-12-18 17:42:32 -08001404 if (s->s3->alpn_selected) {
1405 OPENSSL_free(s->s3->alpn_selected);
1406 s->s3->alpn_selected = NULL;
1407 }
Adam Langley95c29f32014-06-20 12:00:00 -07001408
Adam Langleyfcf25832014-12-18 17:42:32 -08001409 /* Clear any signature algorithms extension received */
1410 if (s->cert->peer_sigalgs) {
1411 OPENSSL_free(s->cert->peer_sigalgs);
1412 s->cert->peer_sigalgs = NULL;
1413 }
Adam Langley95c29f32014-06-20 12:00:00 -07001414
Adam Langleyfcf25832014-12-18 17:42:32 -08001415 /* Clear any shared signature algorithms */
1416 if (s->cert->shared_sigalgs) {
1417 OPENSSL_free(s->cert->shared_sigalgs);
1418 s->cert->shared_sigalgs = NULL;
1419 }
Adam Langley95c29f32014-06-20 12:00:00 -07001420
Adam Langleyfcf25832014-12-18 17:42:32 -08001421 /* Clear ECC extensions */
1422 if (s->s3->tmp.peer_ecpointformatlist != 0) {
1423 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1424 s->s3->tmp.peer_ecpointformatlist = NULL;
1425 s->s3->tmp.peer_ecpointformatlist_length = 0;
1426 }
David Benjamindc72ff72014-06-25 12:36:10 -04001427
Adam Langleyfcf25832014-12-18 17:42:32 -08001428 if (s->s3->tmp.peer_ellipticcurvelist != 0) {
1429 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1430 s->s3->tmp.peer_ellipticcurvelist = NULL;
1431 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1432 }
David Benjamindc72ff72014-06-25 12:36:10 -04001433
Adam Langleyfcf25832014-12-18 17:42:32 -08001434 /* There may be no extensions. */
1435 if (CBS_len(cbs) == 0) {
1436 goto ri_check;
1437 }
David Benjamindc72ff72014-06-25 12:36:10 -04001438
Adam Langleyfcf25832014-12-18 17:42:32 -08001439 /* Decode the extensions block and check it is valid. */
1440 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1441 !tls1_check_duplicate_extensions(&extensions)) {
1442 *out_alert = SSL_AD_DECODE_ERROR;
1443 return 0;
1444 }
David Benjamindc72ff72014-06-25 12:36:10 -04001445
Adam Langleyfcf25832014-12-18 17:42:32 -08001446 while (CBS_len(&extensions) != 0) {
1447 uint16_t type;
1448 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001449
Adam Langleyfcf25832014-12-18 17:42:32 -08001450 /* Decode the next extension. */
1451 if (!CBS_get_u16(&extensions, &type) ||
1452 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1453 *out_alert = SSL_AD_DECODE_ERROR;
1454 return 0;
1455 }
Adam Langley95c29f32014-06-20 12:00:00 -07001456
Adam Langleyfcf25832014-12-18 17:42:32 -08001457 if (s->tlsext_debug_cb) {
1458 s->tlsext_debug_cb(s, 0, type, (uint8_t *)CBS_data(&extension),
1459 CBS_len(&extension), s->tlsext_debug_arg);
1460 }
Adam Langley95c29f32014-06-20 12:00:00 -07001461
Adam Langleyfcf25832014-12-18 17:42:32 -08001462 /* The servername extension is treated as follows:
David Benjamindc72ff72014-06-25 12:36:10 -04001463
Adam Langleyfcf25832014-12-18 17:42:32 -08001464 - Only the hostname type is supported with a maximum length of 255.
1465 - The servername is rejected if too long or if it contains zeros, in
1466 which case an fatal alert is generated.
1467 - The servername field is maintained together with the session cache.
1468 - When a session is resumed, the servername call back invoked in order
1469 to allow the application to position itself to the right context.
1470 - The servername is acknowledged if it is new for a session or when
1471 it is identical to a previously used for the same session.
1472 Applications can control the behaviour. They can at any time
1473 set a 'desirable' servername for a new SSL object. This can be the
1474 case for example with HTTPS when a Host: header field is received and
1475 a renegotiation is requested. In this case, a possible servername
1476 presented in the new client hello is only acknowledged if it matches
1477 the value of the Host: field.
1478 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1479 if they provide for changing an explicit servername context for the
1480 session,
1481 i.e. when the session has been established with a servername extension.
1482 - On session reconnect, the servername extension may be absent. */
Adam Langley95c29f32014-06-20 12:00:00 -07001483
Adam Langleyfcf25832014-12-18 17:42:32 -08001484 if (type == TLSEXT_TYPE_server_name) {
1485 CBS server_name_list;
1486 char have_seen_host_name = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001487
Adam Langleyfcf25832014-12-18 17:42:32 -08001488 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1489 CBS_len(&server_name_list) < 1 || CBS_len(&extension) != 0) {
1490 *out_alert = SSL_AD_DECODE_ERROR;
1491 return 0;
1492 }
David Benjamindc72ff72014-06-25 12:36:10 -04001493
Adam Langleyfcf25832014-12-18 17:42:32 -08001494 /* Decode each ServerName in the extension. */
1495 while (CBS_len(&server_name_list) > 0) {
1496 uint8_t name_type;
1497 CBS host_name;
David Benjamindc72ff72014-06-25 12:36:10 -04001498
Adam Langleyfcf25832014-12-18 17:42:32 -08001499 /* Decode the NameType. */
1500 if (!CBS_get_u8(&server_name_list, &name_type)) {
1501 *out_alert = SSL_AD_DECODE_ERROR;
1502 return 0;
1503 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001504
Adam Langleyfcf25832014-12-18 17:42:32 -08001505 /* Only host_name is supported. */
1506 if (name_type != TLSEXT_NAMETYPE_host_name) {
1507 continue;
1508 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001509
Adam Langleyfcf25832014-12-18 17:42:32 -08001510 if (have_seen_host_name) {
1511 /* The ServerNameList MUST NOT contain more than one name of the same
1512 * name_type. */
1513 *out_alert = SSL_AD_DECODE_ERROR;
1514 return 0;
1515 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001516
Adam Langleyfcf25832014-12-18 17:42:32 -08001517 have_seen_host_name = 1;
Adam Langleyed8270a2014-09-02 13:52:56 -07001518
Adam Langleyfcf25832014-12-18 17:42:32 -08001519 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1520 CBS_len(&host_name) < 1) {
1521 *out_alert = SSL_AD_DECODE_ERROR;
1522 return 0;
1523 }
Adam Langley95c29f32014-06-20 12:00:00 -07001524
Adam Langleyfcf25832014-12-18 17:42:32 -08001525 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1526 CBS_contains_zero_byte(&host_name)) {
1527 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1528 return 0;
1529 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001530
Adam Langleyfcf25832014-12-18 17:42:32 -08001531 if (!s->hit) {
1532 assert(s->session->tlsext_hostname == NULL);
1533 if (s->session->tlsext_hostname) {
1534 /* This should be impossible. */
1535 *out_alert = SSL_AD_DECODE_ERROR;
1536 return 0;
1537 }
Adam Langley95c29f32014-06-20 12:00:00 -07001538
Adam Langleyfcf25832014-12-18 17:42:32 -08001539 /* Copy the hostname as a string. */
1540 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname)) {
1541 *out_alert = SSL_AD_INTERNAL_ERROR;
1542 return 0;
1543 }
Adam Langley95c29f32014-06-20 12:00:00 -07001544
Adam Langleyfcf25832014-12-18 17:42:32 -08001545 s->should_ack_sni = 1;
1546 }
1547 }
1548 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1549 CBS ec_point_format_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001550
Adam Langleyfcf25832014-12-18 17:42:32 -08001551 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1552 CBS_len(&extension) != 0) {
1553 *out_alert = SSL_AD_DECODE_ERROR;
1554 return 0;
1555 }
Adam Langley95c29f32014-06-20 12:00:00 -07001556
Adam Langleyfcf25832014-12-18 17:42:32 -08001557 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1558 &s->s3->tmp.peer_ecpointformatlist_length)) {
1559 *out_alert = SSL_AD_INTERNAL_ERROR;
1560 return 0;
1561 }
1562 } else if (type == TLSEXT_TYPE_elliptic_curves) {
1563 CBS elliptic_curve_list;
1564 size_t i, num_curves;
David Benjamindc72ff72014-06-25 12:36:10 -04001565
Adam Langleyfcf25832014-12-18 17:42:32 -08001566 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
1567 CBS_len(&elliptic_curve_list) == 0 ||
1568 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
1569 CBS_len(&extension) != 0) {
1570 *out_alert = SSL_AD_DECODE_ERROR;
1571 return 0;
1572 }
David Benjamindc72ff72014-06-25 12:36:10 -04001573
Adam Langleyfcf25832014-12-18 17:42:32 -08001574 if (s->s3->tmp.peer_ellipticcurvelist) {
1575 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1576 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1577 }
David Benjamindc72ff72014-06-25 12:36:10 -04001578
Adam Langleyfcf25832014-12-18 17:42:32 -08001579 s->s3->tmp.peer_ellipticcurvelist =
1580 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
David Benjamindc72ff72014-06-25 12:36:10 -04001581
Adam Langleyfcf25832014-12-18 17:42:32 -08001582 if (s->s3->tmp.peer_ellipticcurvelist == NULL) {
1583 *out_alert = SSL_AD_INTERNAL_ERROR;
1584 return 0;
1585 }
Adam Langley95c29f32014-06-20 12:00:00 -07001586
Adam Langleyfcf25832014-12-18 17:42:32 -08001587 num_curves = CBS_len(&elliptic_curve_list) / 2;
1588 for (i = 0; i < num_curves; i++) {
1589 if (!CBS_get_u16(&elliptic_curve_list,
1590 &s->s3->tmp.peer_ellipticcurvelist[i])) {
1591 *out_alert = SSL_AD_INTERNAL_ERROR;
1592 return 0;
1593 }
1594 }
David Benjamindc72ff72014-06-25 12:36:10 -04001595
Adam Langleyfcf25832014-12-18 17:42:32 -08001596 if (CBS_len(&elliptic_curve_list) != 0) {
1597 *out_alert = SSL_AD_INTERNAL_ERROR;
1598 return 0;
1599 }
Adam Langley95c29f32014-06-20 12:00:00 -07001600
Adam Langleyfcf25832014-12-18 17:42:32 -08001601 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
1602 } else if (type == TLSEXT_TYPE_renegotiate) {
1603 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert)) {
1604 return 0;
1605 }
1606 renegotiate_seen = 1;
1607 } else if (type == TLSEXT_TYPE_signature_algorithms) {
1608 CBS supported_signature_algorithms;
Adam Langley95c29f32014-06-20 12:00:00 -07001609
Adam Langleyfcf25832014-12-18 17:42:32 -08001610 if (!CBS_get_u16_length_prefixed(&extension,
1611 &supported_signature_algorithms) ||
1612 CBS_len(&extension) != 0) {
1613 *out_alert = SSL_AD_DECODE_ERROR;
1614 return 0;
1615 }
Adam Langley95c29f32014-06-20 12:00:00 -07001616
Adam Langleyfcf25832014-12-18 17:42:32 -08001617 /* Ensure the signature algorithms are non-empty. It contains a list of
1618 * SignatureAndHashAlgorithms which are two bytes each. */
1619 if (CBS_len(&supported_signature_algorithms) == 0 ||
1620 (CBS_len(&supported_signature_algorithms) % 2) != 0) {
1621 *out_alert = SSL_AD_DECODE_ERROR;
1622 return 0;
1623 }
David Benjamindc72ff72014-06-25 12:36:10 -04001624
Adam Langleyfcf25832014-12-18 17:42:32 -08001625 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1626 *out_alert = SSL_AD_DECODE_ERROR;
1627 return 0;
1628 }
1629 /* If sigalgs received and no shared algorithms fatal error. */
1630 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs) {
1631 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext,
1632 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
1633 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1634 return 0;
1635 }
1636 } else if (type == TLSEXT_TYPE_next_proto_neg &&
1637 s->s3->tmp.finish_md_len == 0 && s->s3->alpn_selected == NULL) {
1638 /* The extension must be empty. */
1639 if (CBS_len(&extension) != 0) {
1640 *out_alert = SSL_AD_DECODE_ERROR;
1641 return 0;
1642 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001643
Adam Langleyfcf25832014-12-18 17:42:32 -08001644 /* We shouldn't accept this extension on a renegotiation.
1645 *
1646 * s->new_session will be set on renegotiation, but we probably shouldn't
1647 * rely that it couldn't be set on the initial renegotation too in
1648 * certain cases (when there's some other reason to disallow resuming an
1649 * earlier session -- the current code won't be doing anything like that,
1650 * but this might change).
David Benjamindc72ff72014-06-25 12:36:10 -04001651
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 * A valid sign that there's been a previous handshake in this connection
1653 * is if s->s3->tmp.finish_md_len > 0. (We are talking about a check
1654 * that will happen in the Hello protocol round, well before a new
1655 * Finished message could have been computed.) */
1656 s->s3->next_proto_neg_seen = 1;
1657 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1658 s->ctx->alpn_select_cb && s->s3->tmp.finish_md_len == 0) {
1659 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert)) {
1660 return 0;
1661 }
1662 /* ALPN takes precedence over NPN. */
1663 s->s3->next_proto_neg_seen = 0;
1664 } else if (type == TLSEXT_TYPE_channel_id && s->tlsext_channel_id_enabled) {
1665 /* The extension must be empty. */
1666 if (CBS_len(&extension) != 0) {
1667 *out_alert = SSL_AD_DECODE_ERROR;
1668 return 0;
1669 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001670
Adam Langleyfcf25832014-12-18 17:42:32 -08001671 s->s3->tlsext_channel_id_valid = 1;
1672 } else if (type == TLSEXT_TYPE_channel_id_new &&
1673 s->tlsext_channel_id_enabled) {
1674 /* The extension must be empty. */
1675 if (CBS_len(&extension) != 0) {
1676 *out_alert = SSL_AD_DECODE_ERROR;
1677 return 0;
1678 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001679
Adam Langleyfcf25832014-12-18 17:42:32 -08001680 s->s3->tlsext_channel_id_valid = 1;
1681 s->s3->tlsext_channel_id_new = 1;
1682 } else if (type == TLSEXT_TYPE_use_srtp) {
1683 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert)) {
1684 return 0;
1685 }
1686 } else if (type == TLSEXT_TYPE_extended_master_secret &&
1687 s->version != SSL3_VERSION) {
1688 if (CBS_len(&extension) != 0) {
1689 *out_alert = SSL_AD_DECODE_ERROR;
1690 return 0;
1691 }
Adam Langley75712922014-10-10 16:23:43 -07001692
Adam Langleyfcf25832014-12-18 17:42:32 -08001693 s->s3->tmp.extended_master_secret = 1;
1694 }
1695 }
Adam Langley75712922014-10-10 16:23:43 -07001696
Adam Langleyfcf25832014-12-18 17:42:32 -08001697ri_check:
1698 /* Need RI if renegotiating */
Adam Langley95c29f32014-06-20 12:00:00 -07001699
Adam Langleyfcf25832014-12-18 17:42:32 -08001700 if (!renegotiate_seen && s->renegotiate &&
1701 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1702 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1703 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
1704 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1705 return 0;
1706 }
Adam Langley95c29f32014-06-20 12:00:00 -07001707
Adam Langleyfcf25832014-12-18 17:42:32 -08001708 return 1;
1709}
Adam Langley95c29f32014-06-20 12:00:00 -07001710
Adam Langleyfcf25832014-12-18 17:42:32 -08001711int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
1712 int alert = -1;
1713 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
1714 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
1715 return 0;
1716 }
Adam Langley95c29f32014-06-20 12:00:00 -07001717
Adam Langleyfcf25832014-12-18 17:42:32 -08001718 if (ssl_check_clienthello_tlsext(s) <= 0) {
1719 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext,
1720 SSL_R_CLIENTHELLO_TLSEXT);
1721 return 0;
1722 }
Adam Langley95c29f32014-06-20 12:00:00 -07001723
Adam Langleyfcf25832014-12-18 17:42:32 -08001724 return 1;
1725}
Adam Langley95c29f32014-06-20 12:00:00 -07001726
Adam Langley95c29f32014-06-20 12:00:00 -07001727/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
Adam Langleyfcf25832014-12-18 17:42:32 -08001728 * elements of zero length are allowed and the set of elements must exactly
1729 * fill the length of the block. */
1730static char ssl_next_proto_validate(const CBS *cbs) {
1731 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001732
Adam Langleyfcf25832014-12-18 17:42:32 -08001733 while (CBS_len(&copy) != 0) {
1734 CBS proto;
1735 if (!CBS_get_u8_length_prefixed(&copy, &proto) || CBS_len(&proto) == 0) {
1736 return 0;
1737 }
1738 }
Adam Langley95c29f32014-06-20 12:00:00 -07001739
Adam Langleyfcf25832014-12-18 17:42:32 -08001740 return 1;
1741}
Adam Langley95c29f32014-06-20 12:00:00 -07001742
Adam Langleyfcf25832014-12-18 17:42:32 -08001743static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1744 int tlsext_servername = 0;
1745 int renegotiate_seen = 0;
1746 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001747
Adam Langleyfcf25832014-12-18 17:42:32 -08001748 /* TODO(davidben): Move all of these to some per-handshake state that gets
1749 * systematically reset on a new handshake; perhaps allocate it fresh each
1750 * time so it's not even kept around post-handshake. */
1751 s->s3->next_proto_neg_seen = 0;
David Benjamin64442872014-07-21 17:43:45 -04001752
Adam Langleyfcf25832014-12-18 17:42:32 -08001753 s->tlsext_ticket_expected = 0;
1754 s->s3->tmp.certificate_status_expected = 0;
1755 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001756
Adam Langleyfcf25832014-12-18 17:42:32 -08001757 if (s->s3->alpn_selected) {
1758 OPENSSL_free(s->s3->alpn_selected);
1759 s->s3->alpn_selected = NULL;
1760 }
David Benjamina19fc252014-10-19 00:14:36 -04001761
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 /* Clear ECC extensions */
1763 if (s->s3->tmp.peer_ecpointformatlist != 0) {
1764 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1765 s->s3->tmp.peer_ecpointformatlist = NULL;
1766 s->s3->tmp.peer_ecpointformatlist_length = 0;
1767 }
David Benjamin03973092014-06-24 23:27:17 -04001768
Adam Langleyfcf25832014-12-18 17:42:32 -08001769 /* There may be no extensions. */
1770 if (CBS_len(cbs) == 0) {
1771 goto ri_check;
1772 }
Adam Langley95c29f32014-06-20 12:00:00 -07001773
Adam Langleyfcf25832014-12-18 17:42:32 -08001774 /* Decode the extensions block and check it is valid. */
1775 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1776 !tls1_check_duplicate_extensions(&extensions)) {
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 while (CBS_len(&extensions) != 0) {
1782 uint16_t type;
1783 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001784
Adam Langleyfcf25832014-12-18 17:42:32 -08001785 /* Decode the next extension. */
1786 if (!CBS_get_u16(&extensions, &type) ||
1787 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1788 *out_alert = SSL_AD_DECODE_ERROR;
1789 return 0;
1790 }
Adam Langley95c29f32014-06-20 12:00:00 -07001791
Adam Langleyfcf25832014-12-18 17:42:32 -08001792 if (s->tlsext_debug_cb) {
1793 s->tlsext_debug_cb(s, 1, type, (uint8_t *)CBS_data(&extension),
1794 CBS_len(&extension), s->tlsext_debug_arg);
1795 }
Adam Langley95c29f32014-06-20 12:00:00 -07001796
Adam Langleyfcf25832014-12-18 17:42:32 -08001797 if (type == TLSEXT_TYPE_server_name) {
1798 /* The extension must be empty. */
1799 if (CBS_len(&extension) != 0) {
1800 *out_alert = SSL_AD_DECODE_ERROR;
1801 return 0;
1802 }
David Benjamin03973092014-06-24 23:27:17 -04001803
Adam Langleyfcf25832014-12-18 17:42:32 -08001804 /* We must have sent it in ClientHello. */
1805 if (s->tlsext_hostname == NULL) {
1806 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1807 return 0;
1808 }
David Benjamin03973092014-06-24 23:27:17 -04001809
Adam Langleyfcf25832014-12-18 17:42:32 -08001810 tlsext_servername = 1;
1811 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1812 CBS ec_point_format_list;
David Benjamin03973092014-06-24 23:27:17 -04001813
Adam Langleyfcf25832014-12-18 17:42:32 -08001814 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1815 CBS_len(&extension) != 0) {
1816 *out_alert = SSL_AD_DECODE_ERROR;
1817 return 0;
1818 }
Adam Langley95c29f32014-06-20 12:00:00 -07001819
Adam Langleyfcf25832014-12-18 17:42:32 -08001820 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1821 &s->s3->tmp.peer_ecpointformatlist_length)) {
1822 *out_alert = SSL_AD_INTERNAL_ERROR;
1823 return 0;
1824 }
1825 } else if (type == TLSEXT_TYPE_session_ticket) {
1826 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0) {
1827 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1828 return 0;
1829 }
David Benjamin03973092014-06-24 23:27:17 -04001830
Adam Langleyfcf25832014-12-18 17:42:32 -08001831 s->tlsext_ticket_expected = 1;
1832 } else if (type == TLSEXT_TYPE_status_request) {
1833 /* The extension MUST be empty and may only sent if we've requested a
1834 * status request message. */
1835 if (CBS_len(&extension) != 0) {
1836 *out_alert = SSL_AD_DECODE_ERROR;
1837 return 0;
1838 }
David Benjamin03973092014-06-24 23:27:17 -04001839
Adam Langleyfcf25832014-12-18 17:42:32 -08001840 if (!s->ocsp_stapling_enabled) {
1841 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1842 return 0;
1843 }
Adam Langley95c29f32014-06-20 12:00:00 -07001844
Adam Langleyfcf25832014-12-18 17:42:32 -08001845 /* Set a flag to expect a CertificateStatus message */
1846 s->s3->tmp.certificate_status_expected = 1;
1847 } else if (type == TLSEXT_TYPE_next_proto_neg &&
1848 s->s3->tmp.finish_md_len == 0) {
1849 uint8_t *selected;
1850 uint8_t selected_len;
David Benjamin03973092014-06-24 23:27:17 -04001851
Adam Langleyfcf25832014-12-18 17:42:32 -08001852 /* We must have requested it. */
1853 if (s->ctx->next_proto_select_cb == NULL) {
1854 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1855 return 0;
1856 }
David Benjamin03973092014-06-24 23:27:17 -04001857
Adam Langleyfcf25832014-12-18 17:42:32 -08001858 /* The data must be valid. */
1859 if (!ssl_next_proto_validate(&extension)) {
1860 *out_alert = SSL_AD_DECODE_ERROR;
1861 return 0;
1862 }
Adam Langley95c29f32014-06-20 12:00:00 -07001863
Adam Langleyfcf25832014-12-18 17:42:32 -08001864 if (s->ctx->next_proto_select_cb(
1865 s, &selected, &selected_len, CBS_data(&extension),
1866 CBS_len(&extension),
1867 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1868 *out_alert = SSL_AD_INTERNAL_ERROR;
1869 return 0;
1870 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001871
Adam Langleyfcf25832014-12-18 17:42:32 -08001872 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
1873 if (s->next_proto_negotiated == NULL) {
1874 *out_alert = SSL_AD_INTERNAL_ERROR;
1875 return 0;
1876 }
Adam Langley75712922014-10-10 16:23:43 -07001877
Adam Langleyfcf25832014-12-18 17:42:32 -08001878 s->next_proto_negotiated_len = selected_len;
1879 s->s3->next_proto_neg_seen = 1;
1880 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
1881 CBS protocol_name_list, protocol_name;
Adam Langley75712922014-10-10 16:23:43 -07001882
Adam Langleyfcf25832014-12-18 17:42:32 -08001883 /* We must have requested it. */
1884 if (s->alpn_client_proto_list == NULL) {
1885 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1886 return 0;
1887 }
Adam Langley95c29f32014-06-20 12:00:00 -07001888
Adam Langleyfcf25832014-12-18 17:42:32 -08001889 /* The extension data consists of a ProtocolNameList which must have
1890 * exactly one ProtocolName. Each of these is length-prefixed. */
1891 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
1892 CBS_len(&extension) != 0 ||
1893 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1894 CBS_len(&protocol_name_list) != 0) {
1895 *out_alert = SSL_AD_DECODE_ERROR;
1896 return 0;
1897 }
Adam Langley95c29f32014-06-20 12:00:00 -07001898
Adam Langleyfcf25832014-12-18 17:42:32 -08001899 if (!CBS_stow(&protocol_name, &s->s3->alpn_selected,
1900 &s->s3->alpn_selected_len)) {
1901 *out_alert = SSL_AD_INTERNAL_ERROR;
1902 return 0;
1903 }
1904 } else if (type == TLSEXT_TYPE_channel_id) {
1905 if (CBS_len(&extension) != 0) {
1906 *out_alert = SSL_AD_DECODE_ERROR;
1907 return 0;
1908 }
Adam Langley95c29f32014-06-20 12:00:00 -07001909
Adam Langleyfcf25832014-12-18 17:42:32 -08001910 s->s3->tlsext_channel_id_valid = 1;
1911 } else if (type == TLSEXT_TYPE_channel_id_new) {
1912 if (CBS_len(&extension) != 0) {
1913 *out_alert = SSL_AD_DECODE_ERROR;
1914 return 0;
1915 }
Adam Langley95c29f32014-06-20 12:00:00 -07001916
Adam Langleyfcf25832014-12-18 17:42:32 -08001917 s->s3->tlsext_channel_id_valid = 1;
1918 s->s3->tlsext_channel_id_new = 1;
1919 } else if (type == TLSEXT_TYPE_certificate_timestamp) {
1920 if (CBS_len(&extension) == 0) {
1921 *out_alert = SSL_AD_DECODE_ERROR;
1922 return 0;
1923 }
Adam Langley95c29f32014-06-20 12:00:00 -07001924
Adam Langleyfcf25832014-12-18 17:42:32 -08001925 /* Session resumption uses the original session information. */
1926 if (!s->hit &&
1927 !CBS_stow(&extension, &s->session->tlsext_signed_cert_timestamp_list,
1928 &s->session->tlsext_signed_cert_timestamp_list_length)) {
1929 *out_alert = SSL_AD_INTERNAL_ERROR;
1930 return 0;
1931 }
1932 } else if (type == TLSEXT_TYPE_renegotiate) {
1933 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert)) {
1934 return 0;
1935 }
Adam Langley95c29f32014-06-20 12:00:00 -07001936
Adam Langleyfcf25832014-12-18 17:42:32 -08001937 renegotiate_seen = 1;
1938 } else if (type == TLSEXT_TYPE_use_srtp) {
1939 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert)) {
1940 return 0;
1941 }
1942 } else if (type == TLSEXT_TYPE_extended_master_secret) {
1943 if (/* It is invalid for the server to select EMS and
1944 SSLv3. */
1945 s->version == SSL3_VERSION || CBS_len(&extension) != 0) {
1946 *out_alert = SSL_AD_DECODE_ERROR;
1947 return 0;
1948 }
Adam Langley95c29f32014-06-20 12:00:00 -07001949
Adam Langleyfcf25832014-12-18 17:42:32 -08001950 s->s3->tmp.extended_master_secret = 1;
1951 }
1952 }
Adam Langley95c29f32014-06-20 12:00:00 -07001953
Adam Langleyfcf25832014-12-18 17:42:32 -08001954 if (!s->hit && tlsext_servername == 1 && s->tlsext_hostname) {
1955 if (s->session->tlsext_hostname == NULL) {
1956 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
1957 if (!s->session->tlsext_hostname) {
1958 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1959 return 0;
1960 }
1961 } else {
1962 *out_alert = SSL_AD_DECODE_ERROR;
1963 return 0;
1964 }
1965 }
Adam Langley95c29f32014-06-20 12:00:00 -07001966
Adam Langleyfcf25832014-12-18 17:42:32 -08001967ri_check:
1968 /* Determine if we need to see RI. Strictly speaking if we want to avoid an
1969 * attack we should *always* see RI even on initial server hello because the
1970 * client doesn't see any renegotiation during an attack. However this would
1971 * mean we could not connect to any server which doesn't support RI so for
1972 * the immediate future tolerate RI absence on initial connect only. */
1973 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) &&
1974 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1975 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1976 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext,
1977 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1978 return 0;
1979 }
Adam Langley95c29f32014-06-20 12:00:00 -07001980
Adam Langleyfcf25832014-12-18 17:42:32 -08001981 return 1;
1982}
Adam Langley95c29f32014-06-20 12:00:00 -07001983
Adam Langleyfcf25832014-12-18 17:42:32 -08001984int ssl_prepare_clienthello_tlsext(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07001985
Adam Langleyfcf25832014-12-18 17:42:32 -08001986int ssl_prepare_serverhello_tlsext(SSL *s) { return 1; }
Adam Langleyed8270a2014-09-02 13:52:56 -07001987
Adam Langleyfcf25832014-12-18 17:42:32 -08001988static int ssl_check_clienthello_tlsext(SSL *s) {
1989 int ret = SSL_TLSEXT_ERR_NOACK;
1990 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07001991
Adam Langleyfcf25832014-12-18 17:42:32 -08001992 /* The handling of the ECPointFormats extension is done elsewhere, namely in
1993 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07001994
Adam Langleyfcf25832014-12-18 17:42:32 -08001995 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
1996 ret = s->ctx->tlsext_servername_callback(s, &al,
1997 s->ctx->tlsext_servername_arg);
1998 } else if (s->initial_ctx != NULL &&
1999 s->initial_ctx->tlsext_servername_callback != 0) {
2000 ret = s->initial_ctx->tlsext_servername_callback(
2001 s, &al, s->initial_ctx->tlsext_servername_arg);
2002 }
Adam Langley95c29f32014-06-20 12:00:00 -07002003
Adam Langleyfcf25832014-12-18 17:42:32 -08002004 switch (ret) {
2005 case SSL_TLSEXT_ERR_ALERT_FATAL:
2006 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2007 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002008
Adam Langleyfcf25832014-12-18 17:42:32 -08002009 case SSL_TLSEXT_ERR_ALERT_WARNING:
2010 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2011 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002012
Adam Langleyfcf25832014-12-18 17:42:32 -08002013 case SSL_TLSEXT_ERR_NOACK:
2014 s->should_ack_sni = 0;
2015 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002016
Adam Langleyfcf25832014-12-18 17:42:32 -08002017 default:
2018 return 1;
2019 }
2020}
Adam Langleyed8270a2014-09-02 13:52:56 -07002021
Adam Langleyfcf25832014-12-18 17:42:32 -08002022static int ssl_check_serverhello_tlsext(SSL *s) {
2023 int ret = SSL_TLSEXT_ERR_NOACK;
2024 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002025
Adam Langleyfcf25832014-12-18 17:42:32 -08002026 /* If we are client and using an elliptic curve cryptography cipher suite,
2027 * then if server returns an EC point formats lists extension it must contain
2028 * uncompressed. */
2029 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2030 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2031 if (((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) &&
2032 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed)) {
2033 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext,
2034 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2035 return -1;
2036 }
2037 ret = SSL_TLSEXT_ERR_OK;
David Benjamin03973092014-06-24 23:27:17 -04002038
Adam Langleyfcf25832014-12-18 17:42:32 -08002039 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2040 ret = s->ctx->tlsext_servername_callback(s, &al,
2041 s->ctx->tlsext_servername_arg);
2042 } else if (s->initial_ctx != NULL &&
2043 s->initial_ctx->tlsext_servername_callback != 0) {
2044 ret = s->initial_ctx->tlsext_servername_callback(
2045 s, &al, s->initial_ctx->tlsext_servername_arg);
2046 }
Adam Langley95c29f32014-06-20 12:00:00 -07002047
Adam Langleyfcf25832014-12-18 17:42:32 -08002048 switch (ret) {
2049 case SSL_TLSEXT_ERR_ALERT_FATAL:
2050 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2051 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002052
Adam Langleyfcf25832014-12-18 17:42:32 -08002053 case SSL_TLSEXT_ERR_ALERT_WARNING:
2054 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2055 return 1;
2056
2057 default:
2058 return 1;
2059 }
2060}
2061
2062int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2063 int alert = -1;
2064 if (s->version < SSL3_VERSION) {
2065 return 1;
2066 }
2067
2068 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2069 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2070 return 0;
2071 }
2072
2073 if (ssl_check_serverhello_tlsext(s) <= 0) {
2074 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext,
2075 SSL_R_SERVERHELLO_TLSEXT);
2076 return 0;
2077 }
2078
2079 return 1;
2080}
Adam Langley95c29f32014-06-20 12:00:00 -07002081
2082/* Since the server cache lookup is done early on in the processing of the
2083 * ClientHello, and other operations depend on the result, we need to handle
2084 * any TLS session ticket extension at the same time.
2085 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002086 * ctx: contains the early callback context, which is the result of a
2087 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002088 * ret: (output) on return, if a ticket was decrypted, then this is set to
2089 * point to the resulting session.
2090 *
Adam Langley95c29f32014-06-20 12:00:00 -07002091 * Returns:
2092 * -1: fatal error, either from parsing or decrypting the ticket.
2093 * 0: no ticket was found (or was ignored, based on settings).
2094 * 1: a zero length extension was found, indicating that the client supports
2095 * session tickets but doesn't currently have one to offer.
David Benjamind1681e62014-11-20 14:54:14 -05002096 * 2: a ticket was offered but couldn't be decrypted because of a non-fatal
2097 * error.
Adam Langley95c29f32014-06-20 12:00:00 -07002098 * 3: a ticket was successfully decrypted and *ret was set.
2099 *
2100 * Side effects:
2101 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2102 * a new session ticket to the client because the client indicated support
David Benjamind1681e62014-11-20 14:54:14 -05002103 * but the client either doesn't have a session ticket or we couldn't use
2104 * the one it gave us, or if s->ctx->tlsext_ticket_key_cb asked to renew
2105 * the client's ticket. Otherwise, s->tlsext_ticket_expected is set to 0.
Adam Langley95c29f32014-06-20 12:00:00 -07002106 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002107int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002108 SSL_SESSION **ret) {
2109 *ret = NULL;
2110 s->tlsext_ticket_expected = 0;
2111 const uint8_t *data;
2112 size_t len;
2113 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002114
Adam Langleyfcf25832014-12-18 17:42:32 -08002115 /* If tickets disabled behave as if no ticket present to permit stateful
2116 * resumption. */
2117 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) ||
2118 (s->version <= SSL3_VERSION && !ctx->extensions) ||
2119 !SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_session_ticket,
2120 &data, &len)) {
2121 return 0;
2122 }
2123
2124 if (len == 0) {
2125 /* The client will accept a ticket but doesn't currently have one. */
2126 s->tlsext_ticket_expected = 1;
2127 return 1;
2128 }
2129
2130 r = tls_decrypt_ticket(s, data, len, ctx->session_id, ctx->session_id_len,
2131 ret);
2132 switch (r) {
2133 case 2: /* ticket couldn't be decrypted */
2134 s->tlsext_ticket_expected = 1;
2135 return 2;
2136
2137 case 3: /* ticket was decrypted */
2138 return r;
2139
2140 case 4: /* ticket decrypted but need to renew */
2141 s->tlsext_ticket_expected = 1;
2142 return 3;
2143
2144 default: /* fatal error */
2145 return -1;
2146 }
2147}
Adam Langley95c29f32014-06-20 12:00:00 -07002148
2149/* tls_decrypt_ticket attempts to decrypt a session ticket.
2150 *
2151 * etick: points to the body of the session ticket extension.
2152 * eticklen: the length of the session tickets extenion.
2153 * sess_id: points at the session ID.
2154 * sesslen: the length of the session ID.
2155 * psess: (output) on return, if a ticket was decrypted, then this is set to
2156 * point to the resulting session.
2157 *
2158 * Returns:
2159 * -1: fatal error, either from parsing or decrypting the ticket.
2160 * 2: the ticket couldn't be decrypted.
2161 * 3: a ticket was successfully decrypted and *psess was set.
Adam Langleyfcf25832014-12-18 17:42:32 -08002162 * 4: same as 3, but the ticket needs to be renewed. */
2163static int tls_decrypt_ticket(SSL *s, const uint8_t *etick, int eticklen,
2164 const uint8_t *sess_id, int sesslen,
2165 SSL_SESSION **psess) {
2166 SSL_SESSION *sess;
2167 uint8_t *sdec;
2168 const uint8_t *p;
2169 int slen, mlen, renew_ticket = 0;
2170 uint8_t tick_hmac[EVP_MAX_MD_SIZE];
2171 HMAC_CTX hctx;
2172 EVP_CIPHER_CTX ctx;
2173 SSL_CTX *tctx = s->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002174
Adam Langleyfcf25832014-12-18 17:42:32 -08002175 /* Need at least keyname + iv + some encrypted data */
2176 if (eticklen < 48) {
2177 return 2;
2178 }
2179
2180 /* Initialize session ticket encryption and HMAC contexts */
2181 HMAC_CTX_init(&hctx);
2182 EVP_CIPHER_CTX_init(&ctx);
2183 if (tctx->tlsext_ticket_key_cb) {
2184 uint8_t *nctick = (uint8_t *)etick;
2185 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, &ctx, &hctx, 0);
2186 if (rv < 0) {
2187 return -1;
2188 }
2189 if (rv == 0) {
2190 return 2;
2191 }
2192 if (rv == 2) {
2193 renew_ticket = 1;
2194 }
2195 } else {
2196 /* Check key name matches */
2197 if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) {
2198 return 2;
2199 }
2200 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2201 NULL) ||
2202 !EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2203 tctx->tlsext_tick_aes_key, etick + 16)) {
2204 HMAC_CTX_cleanup(&hctx);
2205 EVP_CIPHER_CTX_cleanup(&ctx);
2206 return -1;
2207 }
2208 }
2209
2210 /* Attempt to process session ticket, first conduct sanity and integrity
2211 * checks on ticket. */
2212 mlen = HMAC_size(&hctx);
2213 if (mlen < 0) {
2214 HMAC_CTX_cleanup(&hctx);
2215 EVP_CIPHER_CTX_cleanup(&ctx);
2216 return -1;
2217 }
2218 eticklen -= mlen;
2219 /* Check HMAC of encrypted ticket */
2220 HMAC_Update(&hctx, etick, eticklen);
2221 HMAC_Final(&hctx, tick_hmac, NULL);
2222 HMAC_CTX_cleanup(&hctx);
2223 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
2224 EVP_CIPHER_CTX_cleanup(&ctx);
2225 return 2;
2226 }
2227
2228 /* Attempt to decrypt session data */
2229 /* Move p after IV to start of encrypted ticket, update length */
2230 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2231 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2232 sdec = OPENSSL_malloc(eticklen);
2233 if (!sdec) {
2234 EVP_CIPHER_CTX_cleanup(&ctx);
2235 return -1;
2236 }
2237 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2238 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0) {
2239 EVP_CIPHER_CTX_cleanup(&ctx);
2240 OPENSSL_free(sdec);
2241 return 2;
2242 }
2243 slen += mlen;
2244 EVP_CIPHER_CTX_cleanup(&ctx);
2245 p = sdec;
2246
2247 sess = d2i_SSL_SESSION(NULL, &p, slen);
2248 OPENSSL_free(sdec);
2249 if (sess) {
2250 /* The session ID, if non-empty, is used by some clients to detect that the
2251 * ticket has been accepted. So we copy it to the session structure. If it
2252 * is empty set length to zero as required by standard. */
2253 if (sesslen) {
2254 memcpy(sess->session_id, sess_id, sesslen);
2255 }
2256 sess->session_id_length = sesslen;
2257 *psess = sess;
2258 if (renew_ticket) {
2259 return 4;
2260 }
2261 return 3;
2262 }
2263
2264 ERR_clear_error();
2265 /* For session parse failure, indicate that we need to send a new ticket. */
2266 return 2;
2267}
Adam Langley95c29f32014-06-20 12:00:00 -07002268
2269/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002270typedef struct {
2271 int nid;
2272 int id;
2273} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002274
Adam Langleyfcf25832014-12-18 17:42:32 -08002275static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2276 {NID_sha1, TLSEXT_hash_sha1},
2277 {NID_sha224, TLSEXT_hash_sha224},
2278 {NID_sha256, TLSEXT_hash_sha256},
2279 {NID_sha384, TLSEXT_hash_sha384},
2280 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002281
Adam Langleyfcf25832014-12-18 17:42:32 -08002282static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2283 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002284
Adam Langleyfcf25832014-12-18 17:42:32 -08002285static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2286 size_t i;
2287 for (i = 0; i < tlen; i++) {
2288 if (table[i].nid == nid) {
2289 return table[i].id;
2290 }
2291 }
Adam Langley95c29f32014-06-20 12:00:00 -07002292
Adam Langleyfcf25832014-12-18 17:42:32 -08002293 return -1;
2294}
Adam Langley95c29f32014-06-20 12:00:00 -07002295
Adam Langleyfcf25832014-12-18 17:42:32 -08002296static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) {
2297 size_t i;
2298 for (i = 0; i < tlen; i++) {
2299 if (table[i].id == id) {
2300 return table[i].nid;
2301 }
2302 }
Adam Langley95c29f32014-06-20 12:00:00 -07002303
Adam Langleyfcf25832014-12-18 17:42:32 -08002304 return NID_undef;
2305}
Adam Langley95c29f32014-06-20 12:00:00 -07002306
Adam Langleyfcf25832014-12-18 17:42:32 -08002307int tls12_get_sigandhash(uint8_t *p, const EVP_PKEY *pk, const EVP_MD *md) {
2308 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002309
Adam Langleyfcf25832014-12-18 17:42:32 -08002310 if (!md) {
2311 return 0;
2312 }
Adam Langley95c29f32014-06-20 12:00:00 -07002313
Adam Langleyfcf25832014-12-18 17:42:32 -08002314 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2315 sizeof(tls12_md) / sizeof(tls12_lookup));
2316 if (md_id == -1) {
2317 return 0;
2318 }
Adam Langley95c29f32014-06-20 12:00:00 -07002319
Adam Langleyfcf25832014-12-18 17:42:32 -08002320 sig_id = tls12_get_sigid(pk);
2321 if (sig_id == -1) {
2322 return 0;
2323 }
Adam Langley95c29f32014-06-20 12:00:00 -07002324
Adam Langleyfcf25832014-12-18 17:42:32 -08002325 p[0] = (uint8_t)md_id;
2326 p[1] = (uint8_t)sig_id;
2327 return 1;
2328}
2329
2330int tls12_get_sigid(const EVP_PKEY *pk) {
2331 return tls12_find_id(pk->type, tls12_sig,
2332 sizeof(tls12_sig) / sizeof(tls12_lookup));
2333}
2334
2335const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2336 switch (hash_alg) {
2337 case TLSEXT_hash_md5:
2338 return EVP_md5();
2339
2340 case TLSEXT_hash_sha1:
2341 return EVP_sha1();
2342
2343 case TLSEXT_hash_sha224:
2344 return EVP_sha224();
2345
2346 case TLSEXT_hash_sha256:
2347 return EVP_sha256();
2348
2349 case TLSEXT_hash_sha384:
2350 return EVP_sha384();
2351
2352 case TLSEXT_hash_sha512:
2353 return EVP_sha512();
2354
2355 default:
2356 return NULL;
2357 }
2358}
Adam Langley95c29f32014-06-20 12:00:00 -07002359
David Benjaminec2f27d2014-11-13 19:17:25 -05002360/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2361 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002362static int tls12_get_pkey_type(uint8_t sig_alg) {
2363 switch (sig_alg) {
2364 case TLSEXT_signature_rsa:
2365 return EVP_PKEY_RSA;
2366
2367 case TLSEXT_signature_ecdsa:
2368 return EVP_PKEY_EC;
2369
2370 default:
2371 return -1;
2372 }
2373}
Adam Langley95c29f32014-06-20 12:00:00 -07002374
2375/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2376static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
Adam Langleyfcf25832014-12-18 17:42:32 -08002377 int *psignhash_nid, const uint8_t *data) {
2378 int sign_nid = 0, hash_nid = 0;
2379 if (!phash_nid && !psign_nid && !psignhash_nid) {
2380 return;
2381 }
2382
2383 if (phash_nid || psignhash_nid) {
2384 hash_nid = tls12_find_nid(data[0], tls12_md,
2385 sizeof(tls12_md) / sizeof(tls12_lookup));
2386 if (phash_nid) {
2387 *phash_nid = hash_nid;
2388 }
2389 }
2390
2391 if (psign_nid || psignhash_nid) {
2392 sign_nid = tls12_find_nid(data[1], tls12_sig,
2393 sizeof(tls12_sig) / sizeof(tls12_lookup));
2394 if (psign_nid) {
2395 *psign_nid = sign_nid;
2396 }
2397 }
2398
2399 if (psignhash_nid) {
2400 if (sign_nid && hash_nid) {
2401 OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid);
2402 } else {
2403 *psignhash_nid = NID_undef;
2404 }
2405 }
2406}
2407
Adam Langley95c29f32014-06-20 12:00:00 -07002408/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002409static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2410 size_t preflen, const uint8_t *allow,
2411 size_t allowlen) {
2412 const uint8_t *ptmp, *atmp;
2413 size_t i, j, nmatch = 0;
2414
2415 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2416 /* Skip disabled hashes or signature algorithms */
2417 if (tls12_get_hash(ptmp[0]) == NULL ||
2418 tls12_get_pkey_type(ptmp[1]) == -1) {
2419 continue;
2420 }
2421
2422 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2423 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2424 nmatch++;
2425 if (shsig) {
2426 shsig->rhash = ptmp[0];
2427 shsig->rsign = ptmp[1];
2428 tls1_lookup_sigalg(&shsig->hash_nid, &shsig->sign_nid,
2429 &shsig->signandhash_nid, ptmp);
2430 shsig++;
2431 }
2432
2433 break;
2434 }
2435 }
2436 }
2437
2438 return nmatch;
2439}
Adam Langley95c29f32014-06-20 12:00:00 -07002440
2441/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002442static int tls1_set_shared_sigalgs(SSL *s) {
2443 const uint8_t *pref, *allow, *conf;
2444 size_t preflen, allowlen, conflen;
2445 size_t nmatch;
2446 TLS_SIGALGS *salgs = NULL;
2447 CERT *c = s->cert;
2448
2449 if (c->shared_sigalgs) {
2450 OPENSSL_free(c->shared_sigalgs);
2451 c->shared_sigalgs = NULL;
2452 }
2453
2454 /* If client use client signature algorithms if not NULL */
2455 if (!s->server && c->client_sigalgs) {
2456 conf = c->client_sigalgs;
2457 conflen = c->client_sigalgslen;
2458 } else if (c->conf_sigalgs) {
2459 conf = c->conf_sigalgs;
2460 conflen = c->conf_sigalgslen;
2461 } else {
2462 conflen = tls12_get_psigalgs(s, &conf);
2463 }
2464
2465 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2466 pref = conf;
2467 preflen = conflen;
2468 allow = c->peer_sigalgs;
2469 allowlen = c->peer_sigalgslen;
2470 } else {
2471 allow = conf;
2472 allowlen = conflen;
2473 pref = c->peer_sigalgs;
2474 preflen = c->peer_sigalgslen;
2475 }
2476
2477 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2478 if (!nmatch) {
2479 return 1;
2480 }
2481
2482 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2483 if (!salgs) {
2484 return 0;
2485 }
2486
2487 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2488 c->shared_sigalgs = salgs;
2489 c->shared_sigalgslen = nmatch;
2490 return 1;
2491}
Adam Langley95c29f32014-06-20 12:00:00 -07002492
2493/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002494int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2495 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002496
Adam Langleyfcf25832014-12-18 17:42:32 -08002497 /* Extension ignored for inappropriate versions */
2498 if (!SSL_USE_SIGALGS(s)) {
2499 return 1;
2500 }
David Benjamincd996942014-07-20 16:23:51 -04002501
Adam Langleyfcf25832014-12-18 17:42:32 -08002502 /* Length must be even */
2503 if (CBS_len(sigalgs) % 2 != 0) {
2504 return 0;
2505 }
Adam Langley95c29f32014-06-20 12:00:00 -07002506
Adam Langleyfcf25832014-12-18 17:42:32 -08002507 /* Should never happen */
2508 if (!c) {
2509 return 0;
2510 }
Adam Langley95c29f32014-06-20 12:00:00 -07002511
Adam Langleyfcf25832014-12-18 17:42:32 -08002512 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen)) {
2513 return 0;
2514 }
Adam Langley95c29f32014-06-20 12:00:00 -07002515
Adam Langleyfcf25832014-12-18 17:42:32 -08002516 tls1_set_shared_sigalgs(s);
2517 return 1;
2518}
David Benjaminec2f27d2014-11-13 19:17:25 -05002519
Adam Langleyfcf25832014-12-18 17:42:32 -08002520const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey) {
2521 CERT *c = s->cert;
2522 int type = EVP_PKEY_id(pkey);
2523 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002524
Adam Langleyfcf25832014-12-18 17:42:32 -08002525 /* Select the first shared digest supported by our key. */
2526 for (i = 0; i < c->shared_sigalgslen; i++) {
2527 const EVP_MD *md = tls12_get_hash(c->shared_sigalgs[i].rhash);
2528 if (md == NULL ||
2529 tls12_get_pkey_type(c->shared_sigalgs[i].rsign) != type ||
2530 !EVP_PKEY_supports_digest(pkey, md)) {
2531 continue;
2532 }
2533 return md;
2534 }
Adam Langley95c29f32014-06-20 12:00:00 -07002535
Adam Langleyfcf25832014-12-18 17:42:32 -08002536 /* If no suitable digest may be found, default to SHA-1. */
2537 return EVP_sha1();
2538}
Adam Langley95c29f32014-06-20 12:00:00 -07002539
Adam Langleyfcf25832014-12-18 17:42:32 -08002540int SSL_get_sigalgs(SSL *s, int idx, int *psign, int *phash, int *psignhash,
2541 uint8_t *rsig, uint8_t *rhash) {
2542 const uint8_t *psig = s->cert->peer_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002543
Adam Langleyfcf25832014-12-18 17:42:32 -08002544 if (psig == NULL) {
2545 return 0;
2546 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002547
Adam Langleyfcf25832014-12-18 17:42:32 -08002548 if (idx >= 0) {
2549 idx <<= 1;
2550 if (idx >= (int)s->cert->peer_sigalgslen) {
2551 return 0;
2552 }
2553 psig += idx;
2554 if (rhash) {
2555 *rhash = psig[0];
2556 }
2557 if (rsig) {
2558 *rsig = psig[1];
2559 }
2560 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2561 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002562
Adam Langleyfcf25832014-12-18 17:42:32 -08002563 return s->cert->peer_sigalgslen / 2;
2564}
Adam Langley1258b6a2014-06-20 12:00:00 -07002565
Adam Langleyfcf25832014-12-18 17:42:32 -08002566int SSL_get_shared_sigalgs(SSL *s, int idx, int *psign, int *phash,
2567 int *psignhash, uint8_t *rsig, uint8_t *rhash) {
2568 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002569
Adam Langleyfcf25832014-12-18 17:42:32 -08002570 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen) {
2571 return 0;
2572 }
2573
2574 shsigalgs += idx;
2575 if (phash) {
2576 *phash = shsigalgs->hash_nid;
2577 }
2578 if (psign) {
2579 *psign = shsigalgs->sign_nid;
2580 }
2581 if (psignhash) {
2582 *psignhash = shsigalgs->signandhash_nid;
2583 }
2584 if (rsig) {
2585 *rsig = shsigalgs->rsign;
2586 }
2587 if (rhash) {
2588 *rhash = shsigalgs->rhash;
2589 }
2590
2591 return s->cert->shared_sigalgslen;
2592}
2593
2594/* tls1_channel_id_hash calculates the signed data for a Channel ID on the
2595 * given SSL connection and writes it to |md|. */
2596int tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) {
2597 EVP_MD_CTX ctx;
2598 uint8_t temp_digest[EVP_MAX_MD_SIZE];
2599 unsigned temp_digest_len;
2600 int i;
2601 static const char kClientIDMagic[] = "TLS Channel ID signature";
2602
2603 if (s->s3->handshake_buffer &&
2604 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2605 return 0;
2606 }
2607
2608 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2609
2610 if (s->hit && s->s3->tlsext_channel_id_new) {
2611 static const char kResumptionMagic[] = "Resumption";
2612 EVP_DigestUpdate(md, kResumptionMagic, sizeof(kResumptionMagic));
2613 if (s->session->original_handshake_hash_len == 0) {
2614 return 0;
2615 }
2616 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2617 s->session->original_handshake_hash_len);
2618 }
2619
2620 EVP_MD_CTX_init(&ctx);
2621 for (i = 0; i < SSL_MAX_DIGEST; i++) {
2622 if (s->s3->handshake_dgst[i] == NULL) {
2623 continue;
2624 }
2625 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
2626 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2627 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2628 }
2629 EVP_MD_CTX_cleanup(&ctx);
2630
2631 return 1;
2632}
Adam Langley1258b6a2014-06-20 12:00:00 -07002633
2634/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2635 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002636int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2637 int digest_len;
2638 /* This function should never be called for a resumed session because the
2639 * handshake hashes that we wish to record are for the original, full
2640 * handshake. */
2641 if (s->hit) {
2642 return -1;
2643 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002644
Adam Langleyfcf25832014-12-18 17:42:32 -08002645 /* It only makes sense to call this function if Channel IDs have been
2646 * negotiated. */
2647 if (!s->s3->tlsext_channel_id_new) {
2648 return -1;
2649 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002650
Adam Langleyfcf25832014-12-18 17:42:32 -08002651 digest_len =
2652 tls1_handshake_digest(s, s->session->original_handshake_hash,
2653 sizeof(s->session->original_handshake_hash));
2654 if (digest_len < 0) {
2655 return -1;
2656 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002657
Adam Langleyfcf25832014-12-18 17:42:32 -08002658 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002659
Adam Langleyfcf25832014-12-18 17:42:32 -08002660 return 1;
2661}
Adam Langley95c29f32014-06-20 12:00:00 -07002662
Adam Langleyfcf25832014-12-18 17:42:32 -08002663int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
2664 int client) {
2665 uint8_t *sigalgs, *sptr;
2666 int rhash, rsign;
2667 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002668
Adam Langleyfcf25832014-12-18 17:42:32 -08002669 if (salglen & 1) {
2670 return 0;
2671 }
Adam Langley95c29f32014-06-20 12:00:00 -07002672
Adam Langleyfcf25832014-12-18 17:42:32 -08002673 sigalgs = OPENSSL_malloc(salglen);
2674 if (sigalgs == NULL) {
2675 return 0;
2676 }
Adam Langley95c29f32014-06-20 12:00:00 -07002677
Adam Langleyfcf25832014-12-18 17:42:32 -08002678 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
2679 rhash = tls12_find_id(*psig_nids++, tls12_md,
2680 sizeof(tls12_md) / sizeof(tls12_lookup));
2681 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2682 sizeof(tls12_sig) / sizeof(tls12_lookup));
Adam Langley95c29f32014-06-20 12:00:00 -07002683
Adam Langleyfcf25832014-12-18 17:42:32 -08002684 if (rhash == -1 || rsign == -1) {
2685 goto err;
2686 }
2687 *sptr++ = rhash;
2688 *sptr++ = rsign;
2689 }
2690
2691 if (client) {
2692 if (c->client_sigalgs) {
2693 OPENSSL_free(c->client_sigalgs);
2694 }
2695 c->client_sigalgs = sigalgs;
2696 c->client_sigalgslen = salglen;
2697 } else {
2698 if (c->conf_sigalgs) {
2699 OPENSSL_free(c->conf_sigalgs);
2700 }
2701 c->conf_sigalgs = sigalgs;
2702 c->conf_sigalgslen = salglen;
2703 }
2704
2705 return 1;
2706
2707err:
2708 OPENSSL_free(sigalgs);
2709 return 0;
2710}