blob: 0972515f8b76c2958d79d24b4c0ced8d64e3b0b9 [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 Langley95c29f32014-06-20 12:00:00 -0700121static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
122 const unsigned char *sess_id, int sesslen,
123 SSL_SESSION **psess);
David Benjamin6c7aed02014-08-27 16:42:38 -0400124static int ssl_check_clienthello_tlsext(SSL *s);
125static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700126
127SSL3_ENC_METHOD TLSv1_enc_data={
128 tls1_enc,
129 tls1_mac,
130 tls1_setup_key_block,
131 tls1_generate_master_secret,
132 tls1_change_cipher_state,
133 tls1_final_finish_mac,
134 TLS1_FINISH_MAC_LENGTH,
135 tls1_cert_verify_mac,
136 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
137 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
138 tls1_alert_code,
139 tls1_export_keying_material,
140 0,
141 SSL3_HM_HEADER_LENGTH,
142 ssl3_set_handshake_header,
Adam Langley75712922014-10-10 16:23:43 -0700143 ssl3_handshake_write,
144 ssl3_add_to_finished_hash,
Adam Langley95c29f32014-06-20 12:00:00 -0700145 };
146
147SSL3_ENC_METHOD TLSv1_1_enc_data={
148 tls1_enc,
149 tls1_mac,
150 tls1_setup_key_block,
151 tls1_generate_master_secret,
152 tls1_change_cipher_state,
153 tls1_final_finish_mac,
154 TLS1_FINISH_MAC_LENGTH,
155 tls1_cert_verify_mac,
156 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
157 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
158 tls1_alert_code,
159 tls1_export_keying_material,
160 SSL_ENC_FLAG_EXPLICIT_IV,
161 SSL3_HM_HEADER_LENGTH,
162 ssl3_set_handshake_header,
Adam Langley75712922014-10-10 16:23:43 -0700163 ssl3_handshake_write,
164 ssl3_add_to_finished_hash,
Adam Langley95c29f32014-06-20 12:00:00 -0700165 };
166
167SSL3_ENC_METHOD TLSv1_2_enc_data={
168 tls1_enc,
169 tls1_mac,
170 tls1_setup_key_block,
171 tls1_generate_master_secret,
172 tls1_change_cipher_state,
173 tls1_final_finish_mac,
174 TLS1_FINISH_MAC_LENGTH,
175 tls1_cert_verify_mac,
176 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
177 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
178 tls1_alert_code,
179 tls1_export_keying_material,
180 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
181 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
182 SSL3_HM_HEADER_LENGTH,
183 ssl3_set_handshake_header,
Adam Langley75712922014-10-10 16:23:43 -0700184 ssl3_handshake_write,
185 ssl3_add_to_finished_hash,
Adam Langley95c29f32014-06-20 12:00:00 -0700186 };
187
David Benjamin35a7a442014-07-05 00:23:20 -0400188static int compare_uint16_t(const void *p1, const void *p2)
189 {
190 uint16_t u1 = *((const uint16_t*)p1);
191 uint16_t u2 = *((const uint16_t*)p2);
192 if (u1 < u2)
193 {
194 return -1;
195 }
196 else if (u1 > u2)
197 {
198 return 1;
199 }
200 else
201 {
202 return 0;
203 }
204 }
205
206/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be more
207 * than one extension of the same type in a ClientHello or ServerHello. This
208 * function does an initial scan over the extensions block to filter those
209 * out. */
210static int tls1_check_duplicate_extensions(const CBS *cbs)
211 {
212 CBS extensions = *cbs;
213 size_t num_extensions = 0, i = 0;
214 uint16_t *extension_types = NULL;
215 int ret = 0;
216
217 /* First pass: count the extensions. */
218 while (CBS_len(&extensions) > 0)
219 {
220 uint16_t type;
221 CBS extension;
222
223 if (!CBS_get_u16(&extensions, &type) ||
224 !CBS_get_u16_length_prefixed(&extensions, &extension))
225 {
226 goto done;
227 }
228
229 num_extensions++;
230 }
231
David Benjamin9a373592014-07-25 04:27:53 -0400232 if (num_extensions == 0)
233 {
234 return 1;
235 }
236
David Benjamin35a7a442014-07-05 00:23:20 -0400237 extension_types = (uint16_t*)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
238 if (extension_types == NULL)
239 {
240 OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions, ERR_R_MALLOC_FAILURE);
241 goto done;
242 }
243
244 /* Second pass: gather the extension types. */
245 extensions = *cbs;
246 for (i = 0; i < num_extensions; i++)
247 {
248 CBS extension;
249
250 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
251 !CBS_get_u16_length_prefixed(&extensions, &extension))
252 {
253 /* This should not happen. */
254 goto done;
255 }
256 }
257 assert(CBS_len(&extensions) == 0);
258
259 /* Sort the extensions and make sure there are no duplicates. */
260 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
261 for (i = 1; i < num_extensions; i++)
262 {
263 if (extension_types[i-1] == extension_types[i])
264 {
265 goto done;
266 }
267 }
268
269 ret = 1;
270done:
271 if (extension_types)
272 OPENSSL_free(extension_types);
273 return ret;
274 }
275
Adam Langleydc9b1412014-06-20 12:00:00 -0700276char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx)
277 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400278 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
279
280 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700281
282 /* Skip client version. */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400283 if (!CBS_skip(&client_hello, 2))
Adam Langleydc9b1412014-06-20 12:00:00 -0700284 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700285
286 /* Skip client nonce. */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400287 if (!CBS_skip(&client_hello, 32))
Adam Langleydc9b1412014-06-20 12:00:00 -0700288 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700289
David Benjamin8f2c20e2014-07-09 09:30:38 -0400290 /* Extract session_id. */
291 if (!CBS_get_u8_length_prefixed(&client_hello, &session_id))
Adam Langleydc9b1412014-06-20 12:00:00 -0700292 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400293 ctx->session_id = CBS_data(&session_id);
294 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700295
296 /* Skip past DTLS cookie */
David Benjamin09bd58d2014-08-12 21:22:28 -0400297 if (SSL_IS_DTLS(ctx->ssl))
Adam Langleydc9b1412014-06-20 12:00:00 -0700298 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400299 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700300
David Benjamin8f2c20e2014-07-09 09:30:38 -0400301 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie))
Adam Langleydc9b1412014-06-20 12:00:00 -0700302 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700303 }
304
David Benjamin8f2c20e2014-07-09 09:30:38 -0400305 /* Extract cipher_suites. */
306 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
307 CBS_len(&cipher_suites) < 2 ||
308 (CBS_len(&cipher_suites) & 1) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700309 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400310 ctx->cipher_suites = CBS_data(&cipher_suites);
311 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700312
David Benjamin8f2c20e2014-07-09 09:30:38 -0400313 /* Extract compression_methods. */
314 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
315 CBS_len(&compression_methods) < 1)
Adam Langleydc9b1412014-06-20 12:00:00 -0700316 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400317 ctx->compression_methods = CBS_data(&compression_methods);
318 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700319
320 /* If the ClientHello ends here then it's valid, but doesn't have any
321 * extensions. (E.g. SSLv3.) */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400322 if (CBS_len(&client_hello) == 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700323 {
324 ctx->extensions = NULL;
325 ctx->extensions_len = 0;
326 return 1;
327 }
328
David Benjamin8f2c20e2014-07-09 09:30:38 -0400329 /* Extract extensions and check it is valid. */
330 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
331 !tls1_check_duplicate_extensions(&extensions) ||
332 CBS_len(&client_hello) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700333 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400334 ctx->extensions = CBS_data(&extensions);
335 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700336
Adam Langleydc9b1412014-06-20 12:00:00 -0700337 return 1;
Adam Langleydc9b1412014-06-20 12:00:00 -0700338 }
339
340char
341SSL_early_callback_ctx_extension_get(const struct ssl_early_callback_ctx *ctx,
342 uint16_t extension_type,
343 const unsigned char **out_data,
344 size_t *out_len)
345 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400346 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700347
David Benjamin8f2c20e2014-07-09 09:30:38 -0400348 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
349
350 while (CBS_len(&extensions) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700351 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400352 uint16_t type;
353 CBS extension;
Adam Langleydc9b1412014-06-20 12:00:00 -0700354
David Benjamin8f2c20e2014-07-09 09:30:38 -0400355 /* Decode the next extension. */
356 if (!CBS_get_u16(&extensions, &type) ||
357 !CBS_get_u16_length_prefixed(&extensions, &extension))
Adam Langleydc9b1412014-06-20 12:00:00 -0700358 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700359
David Benjamin8f2c20e2014-07-09 09:30:38 -0400360 if (type == extension_type)
Adam Langleydc9b1412014-06-20 12:00:00 -0700361 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400362 *out_data = CBS_data(&extension);
363 *out_len = CBS_len(&extension);
Adam Langleydc9b1412014-06-20 12:00:00 -0700364 return 1;
365 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700366 }
367
368 return 0;
369 }
370
Adam Langley95c29f32014-06-20 12:00:00 -0700371
David Benjamincff64722014-08-19 19:54:46 -0400372static const int nid_list[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700373 {
374 NID_sect163k1, /* sect163k1 (1) */
375 NID_sect163r1, /* sect163r1 (2) */
376 NID_sect163r2, /* sect163r2 (3) */
377 NID_sect193r1, /* sect193r1 (4) */
378 NID_sect193r2, /* sect193r2 (5) */
379 NID_sect233k1, /* sect233k1 (6) */
380 NID_sect233r1, /* sect233r1 (7) */
381 NID_sect239k1, /* sect239k1 (8) */
382 NID_sect283k1, /* sect283k1 (9) */
383 NID_sect283r1, /* sect283r1 (10) */
384 NID_sect409k1, /* sect409k1 (11) */
385 NID_sect409r1, /* sect409r1 (12) */
386 NID_sect571k1, /* sect571k1 (13) */
387 NID_sect571r1, /* sect571r1 (14) */
388 NID_secp160k1, /* secp160k1 (15) */
389 NID_secp160r1, /* secp160r1 (16) */
390 NID_secp160r2, /* secp160r2 (17) */
391 NID_secp192k1, /* secp192k1 (18) */
392 NID_X9_62_prime192v1, /* secp192r1 (19) */
393 NID_secp224k1, /* secp224k1 (20) */
394 NID_secp224r1, /* secp224r1 (21) */
395 NID_secp256k1, /* secp256k1 (22) */
396 NID_X9_62_prime256v1, /* secp256r1 (23) */
397 NID_secp384r1, /* secp384r1 (24) */
398 NID_secp521r1, /* secp521r1 (25) */
399 NID_brainpoolP256r1, /* brainpoolP256r1 (26) */
400 NID_brainpoolP384r1, /* brainpoolP384r1 (27) */
401 NID_brainpoolP512r1 /* brainpool512r1 (28) */
402 };
403
David Benjamin072334d2014-07-13 16:24:27 -0400404static const uint8_t ecformats_default[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700405 {
406 TLSEXT_ECPOINTFORMAT_uncompressed,
Adam Langley95c29f32014-06-20 12:00:00 -0700407 };
408
David Benjamin072334d2014-07-13 16:24:27 -0400409static const uint16_t eccurves_default[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700410 {
David Benjamin072334d2014-07-13 16:24:27 -0400411 23, /* secp256r1 (23) */
412 24, /* secp384r1 (24) */
413 25, /* secp521r1 (25) */
Adam Langley95c29f32014-06-20 12:00:00 -0700414 };
415
David Benjamin072334d2014-07-13 16:24:27 -0400416int tls1_ec_curve_id2nid(uint16_t curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700417 {
418 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
David Benjamin072334d2014-07-13 16:24:27 -0400419 if (curve_id < 1 || curve_id > sizeof(nid_list)/sizeof(nid_list[0]))
420 return OBJ_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700421 return nid_list[curve_id-1];
422 }
423
David Benjamin072334d2014-07-13 16:24:27 -0400424uint16_t tls1_ec_nid2curve_id(int nid)
Adam Langley95c29f32014-06-20 12:00:00 -0700425 {
David Benjamin072334d2014-07-13 16:24:27 -0400426 size_t i;
427 for (i = 0; i < sizeof(nid_list)/sizeof(nid_list[0]); i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700428 {
David Benjamin072334d2014-07-13 16:24:27 -0400429 /* nid_list[i] stores the NID corresponding to curve ID i+1. */
430 if (nid == nid_list[i])
431 return i + 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700432 }
David Benjamin072334d2014-07-13 16:24:27 -0400433 /* Use 0 for non-existent curve ID. Note: this assumes that curve ID 0
434 * will never be allocated. */
435 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700436 }
David Benjamin072334d2014-07-13 16:24:27 -0400437
David Benjamin42e9a772014-09-02 23:18:44 -0400438/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len|
439 * to the list of allowed curve IDs. If |get_peer_curves| is non-zero,
440 * return the peer's curve list. Otherwise, return the preferred
441 * list. */
442static void tls1_get_curvelist(SSL *s, int get_peer_curves,
David Benjamin072334d2014-07-13 16:24:27 -0400443 const uint16_t **out_curve_ids, size_t *out_curve_ids_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700444 {
David Benjamin42e9a772014-09-02 23:18:44 -0400445 if (get_peer_curves)
Adam Langley95c29f32014-06-20 12:00:00 -0700446 {
David Benjamina19fc252014-10-19 00:14:36 -0400447 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
448 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
Adam Langley95c29f32014-06-20 12:00:00 -0700449 return;
450 }
Adam Langley95c29f32014-06-20 12:00:00 -0700451
David Benjamin335d10d2014-08-06 19:56:33 -0400452 *out_curve_ids = s->tlsext_ellipticcurvelist;
453 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
David Benjamin072334d2014-07-13 16:24:27 -0400454 if (!*out_curve_ids)
Adam Langley95c29f32014-06-20 12:00:00 -0700455 {
David Benjamin072334d2014-07-13 16:24:27 -0400456 *out_curve_ids = eccurves_default;
David Benjamin0eb5a2d2014-07-25 02:40:43 -0400457 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
Adam Langley95c29f32014-06-20 12:00:00 -0700458 }
459 }
David Benjamined439582014-07-14 19:13:02 -0400460
David Benjamined439582014-07-14 19:13:02 -0400461int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700462 {
David Benjamined439582014-07-14 19:13:02 -0400463 uint8_t curve_type;
464 uint16_t curve_id;
David Benjamin072334d2014-07-13 16:24:27 -0400465 const uint16_t *curves;
466 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400467
468 /* Only support named curves. */
469 if (!CBS_get_u8(cbs, &curve_type) ||
470 curve_type != NAMED_CURVE_TYPE ||
471 !CBS_get_u16(cbs, &curve_id))
Adam Langley95c29f32014-06-20 12:00:00 -0700472 return 0;
David Benjamined439582014-07-14 19:13:02 -0400473
David Benjamin072334d2014-07-13 16:24:27 -0400474 tls1_get_curvelist(s, 0, &curves, &curves_len);
475 for (i = 0; i < curves_len; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700476 {
David Benjamin072334d2014-07-13 16:24:27 -0400477 if (curve_id == curves[i])
David Benjamined439582014-07-14 19:13:02 -0400478 {
479 *out_curve_id = curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700480 return 1;
David Benjamined439582014-07-14 19:13:02 -0400481 }
Adam Langley95c29f32014-06-20 12:00:00 -0700482 }
483 return 0;
484 }
485
David Benjamin072334d2014-07-13 16:24:27 -0400486int tls1_get_shared_curve(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -0700487 {
David Benjamin072334d2014-07-13 16:24:27 -0400488 const uint16_t *pref, *supp;
Adam Langley95c29f32014-06-20 12:00:00 -0700489 size_t preflen, supplen, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400490
Adam Langley95c29f32014-06-20 12:00:00 -0700491 /* Can't do anything on client side */
492 if (s->server == 0)
David Benjamin072334d2014-07-13 16:24:27 -0400493 return NID_undef;
494
David Benjamin335d10d2014-08-06 19:56:33 -0400495 /* Return first preference shared curve */
Adam Langley95c29f32014-06-20 12:00:00 -0700496 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
497 &supp, &supplen);
498 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
499 &pref, &preflen);
David Benjamin072334d2014-07-13 16:24:27 -0400500 for (i = 0; i < preflen; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700501 {
David Benjamin072334d2014-07-13 16:24:27 -0400502 for (j = 0; j < supplen; j++)
Adam Langley95c29f32014-06-20 12:00:00 -0700503 {
David Benjamin072334d2014-07-13 16:24:27 -0400504 if (pref[i] == supp[j])
505 return tls1_ec_curve_id2nid(pref[i]);
Adam Langley95c29f32014-06-20 12:00:00 -0700506 }
507 }
David Benjamin072334d2014-07-13 16:24:27 -0400508 return NID_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700509 }
510
David Benjamin072334d2014-07-13 16:24:27 -0400511/* NOTE: tls1_ec_curve_id2nid and tls1_set_curves assume that
512 *
513 * (a) 0 is not a valid curve ID.
514 *
515 * (b) The largest curve ID is 31.
516 *
517 * Those implementations must be revised before adding support for curve IDs
518 * that break these assumptions. */
519OPENSSL_COMPILE_ASSERT(
520 (sizeof(nid_list) / sizeof(nid_list[0])) < 32, small_curve_ids);
521
522int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
523 const int *curves, size_t ncurves)
Adam Langley95c29f32014-06-20 12:00:00 -0700524 {
David Benjamin072334d2014-07-13 16:24:27 -0400525 uint16_t *curve_ids;
Adam Langley95c29f32014-06-20 12:00:00 -0700526 size_t i;
527 /* Bitmap of curves included to detect duplicates: only works
528 * while curve ids < 32
529 */
David Benjamin072334d2014-07-13 16:24:27 -0400530 uint32_t dup_list = 0;
531 curve_ids = (uint16_t*)OPENSSL_malloc(ncurves * sizeof(uint16_t));
532 if (!curve_ids)
Adam Langley95c29f32014-06-20 12:00:00 -0700533 return 0;
David Benjamin072334d2014-07-13 16:24:27 -0400534 for (i = 0; i < ncurves; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700535 {
David Benjamin072334d2014-07-13 16:24:27 -0400536 uint32_t idmask;
537 uint16_t id;
Adam Langley95c29f32014-06-20 12:00:00 -0700538 id = tls1_ec_nid2curve_id(curves[i]);
David Benjamin072334d2014-07-13 16:24:27 -0400539 idmask = ((uint32_t)1) << id;
Adam Langley95c29f32014-06-20 12:00:00 -0700540 if (!id || (dup_list & idmask))
541 {
David Benjamin072334d2014-07-13 16:24:27 -0400542 OPENSSL_free(curve_ids);
Adam Langley95c29f32014-06-20 12:00:00 -0700543 return 0;
544 }
545 dup_list |= idmask;
David Benjamin072334d2014-07-13 16:24:27 -0400546 curve_ids[i] = id;
Adam Langley95c29f32014-06-20 12:00:00 -0700547 }
David Benjamin072334d2014-07-13 16:24:27 -0400548 if (*out_curve_ids)
549 OPENSSL_free(*out_curve_ids);
550 *out_curve_ids = curve_ids;
551 *out_curve_ids_len = ncurves;
Adam Langley95c29f32014-06-20 12:00:00 -0700552 return 1;
553 }
554
David Benjamin072334d2014-07-13 16:24:27 -0400555/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
556 * TLS curve ID and point format, respectively, for |ec|. It returns one on
557 * success and zero on failure. */
558static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id, uint8_t *out_comp_id, EC_KEY *ec)
Adam Langley95c29f32014-06-20 12:00:00 -0700559 {
Adam Langley95c29f32014-06-20 12:00:00 -0700560 int nid;
David Benjamin072334d2014-07-13 16:24:27 -0400561 uint16_t id;
Adam Langley95c29f32014-06-20 12:00:00 -0700562 const EC_GROUP *grp;
563 if (!ec)
564 return 0;
565
Adam Langley95c29f32014-06-20 12:00:00 -0700566 grp = EC_KEY_get0_group(ec);
567 if (!grp)
568 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700569
570 /* Determine curve ID */
David Benjamin072334d2014-07-13 16:24:27 -0400571 nid = EC_GROUP_get_curve_name(grp);
572 id = tls1_ec_nid2curve_id(nid);
573 if (!id)
574 return 0;
575
576 /* Set the named curve ID. Arbitrary explicit curves are not
577 * supported. */
578 *out_curve_id = id;
579
580 if (out_comp_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700581 {
582 if (EC_KEY_get0_public_key(ec) == NULL)
583 return 0;
584 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED)
David Benjamin072334d2014-07-13 16:24:27 -0400585 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
Adam Langley95c29f32014-06-20 12:00:00 -0700586 else
David Benjamin072334d2014-07-13 16:24:27 -0400587 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
Adam Langley95c29f32014-06-20 12:00:00 -0700588 }
589 return 1;
590 }
David Benjamin072334d2014-07-13 16:24:27 -0400591
David Benjamin42e9a772014-09-02 23:18:44 -0400592/* tls1_check_point_format returns one if |comp_id| is consistent with the
593 * peer's point format preferences. */
594static int tls1_check_point_format(SSL *s, uint8_t comp_id)
595 {
David Benjamina19fc252014-10-19 00:14:36 -0400596 uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
597 size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
David Benjamin42e9a772014-09-02 23:18:44 -0400598 size_t i;
599
600 /* If point formats extension present check it, otherwise everything
601 * is supported (see RFC4492). */
602 if (p == NULL)
603 return 1;
604
605 for (i = 0; i < plen; i++)
606 {
607 if (comp_id == p[i])
608 return 1;
609 }
610 return 0;
611 }
612
613/* tls1_check_curve_id returns one if |curve_id| is consistent with both our and
614 * the peer's curve preferences. Note: if called as the client, only our
615 * preferences are checked; the peer (the server) does not send preferences. */
616static int tls1_check_curve_id(SSL *s, uint16_t curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700617 {
David Benjamin072334d2014-07-13 16:24:27 -0400618 const uint16_t *curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400619 size_t curves_len, i, j;
620
621 /* Check against our list, then the peer's list. */
Adam Langley95c29f32014-06-20 12:00:00 -0700622 for (j = 0; j <= 1; j++)
623 {
David Benjamin072334d2014-07-13 16:24:27 -0400624 tls1_get_curvelist(s, j, &curves, &curves_len);
625 for (i = 0; i < curves_len; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700626 {
David Benjamin42e9a772014-09-02 23:18:44 -0400627 if (curves[i] == curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700628 break;
629 }
David Benjamin072334d2014-07-13 16:24:27 -0400630 if (i == curves_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700631 return 0;
David Benjamin42e9a772014-09-02 23:18:44 -0400632 /* Servers do not present a preference list so, if we are a
633 * client, only check our list. */
Adam Langley95c29f32014-06-20 12:00:00 -0700634 if (!s->server)
635 return 1;
636 }
637 return 1;
638 }
639
640static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
641 size_t *pformatslen)
642 {
643 /* If we have a custom point format list use it otherwise
644 * use default */
645 if (s->tlsext_ecpointformatlist)
646 {
647 *pformats = s->tlsext_ecpointformatlist;
648 *pformatslen = s->tlsext_ecpointformatlist_length;
649 }
650 else
651 {
652 *pformats = ecformats_default;
David Benjamin335d10d2014-08-06 19:56:33 -0400653 *pformatslen = sizeof(ecformats_default);
Adam Langley95c29f32014-06-20 12:00:00 -0700654 }
655 }
656
657/* Check cert parameters compatible with extensions: currently just checks
658 * EC certificates have compatible curves and compression.
659 */
660static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
661 {
David Benjamin072334d2014-07-13 16:24:27 -0400662 uint8_t comp_id;
663 uint16_t curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700664 EVP_PKEY *pkey;
665 int rv;
666 pkey = X509_get_pubkey(x);
667 if (!pkey)
668 return 0;
669 /* If not EC nothing to do */
670 if (pkey->type != EVP_PKEY_EC)
671 {
672 EVP_PKEY_free(pkey);
673 return 1;
674 }
David Benjamin072334d2014-07-13 16:24:27 -0400675 rv = tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec);
Adam Langley95c29f32014-06-20 12:00:00 -0700676 EVP_PKEY_free(pkey);
677 if (!rv)
678 return 0;
679 /* Can't check curve_id for client certs as we don't have a
David Benjamin42e9a772014-09-02 23:18:44 -0400680 * supported curves extension. */
681 if (s->server && !tls1_check_curve_id(s, curve_id))
682 return 0;
683 return tls1_check_point_format(s, comp_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700684 }
David Benjamin42e9a772014-09-02 23:18:44 -0400685
Adam Langley95c29f32014-06-20 12:00:00 -0700686/* Check EC temporary key is compatible with client extensions */
David Benjamin42e9a772014-09-02 23:18:44 -0400687int tls1_check_ec_tmp_key(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -0700688 {
David Benjamin072334d2014-07-13 16:24:27 -0400689 uint16_t curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700690 EC_KEY *ec = s->cert->ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -0700691 if (s->cert->ecdh_tmp_auto)
692 {
693 /* Need a shared curve */
David Benjamin072334d2014-07-13 16:24:27 -0400694 return tls1_get_shared_curve(s) != NID_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700695 }
696 if (!ec)
697 {
698 if (s->cert->ecdh_tmp_cb)
699 return 1;
700 else
701 return 0;
702 }
David Benjamin42e9a772014-09-02 23:18:44 -0400703 return tls1_curve_params_from_ec_key(&curve_id, NULL, ec) &&
704 tls1_check_curve_id(s, curve_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700705 }
706
Adam Langley95c29f32014-06-20 12:00:00 -0700707
Adam Langley95c29f32014-06-20 12:00:00 -0700708
709/* List of supported signature algorithms and hashes. Should make this
710 * customisable at some point, for now include everything we support.
711 */
712
Adam Langley95c29f32014-06-20 12:00:00 -0700713#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700714
Adam Langley95c29f32014-06-20 12:00:00 -0700715#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700716
717#define tlsext_sigalg(md) \
718 tlsext_sigalg_rsa(md) \
Adam Langley95c29f32014-06-20 12:00:00 -0700719 tlsext_sigalg_ecdsa(md)
720
David Benjamincff64722014-08-19 19:54:46 -0400721static const uint8_t tls12_sigalgs[] = {
Adam Langley95c29f32014-06-20 12:00:00 -0700722 tlsext_sigalg(TLSEXT_hash_sha512)
723 tlsext_sigalg(TLSEXT_hash_sha384)
Adam Langley95c29f32014-06-20 12:00:00 -0700724 tlsext_sigalg(TLSEXT_hash_sha256)
725 tlsext_sigalg(TLSEXT_hash_sha224)
Adam Langley95c29f32014-06-20 12:00:00 -0700726 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700727};
Adam Langley95c29f32014-06-20 12:00:00 -0700728size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
729 {
Adam Langley95c29f32014-06-20 12:00:00 -0700730 /* If server use client authentication sigalgs if not NULL */
731 if (s->server && s->cert->client_sigalgs)
732 {
733 *psigs = s->cert->client_sigalgs;
734 return s->cert->client_sigalgslen;
735 }
736 else if (s->cert->conf_sigalgs)
737 {
738 *psigs = s->cert->conf_sigalgs;
739 return s->cert->conf_sigalgslen;
740 }
741 else
742 {
743 *psigs = tls12_sigalgs;
744 return sizeof(tls12_sigalgs);
745 }
746 }
David Benjamin05da6e12014-07-12 20:42:55 -0400747
748/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of
749 * |cbs|. It checks it is consistent with |s|'s sent supported
750 * signature algorithms and, if so, writes the relevant digest into
751 * |*out_md| and returns 1. Otherwise it returns 0 and writes an alert
752 * into |*out_alert|.
Adam Langley95c29f32014-06-20 12:00:00 -0700753 */
David Benjamin05da6e12014-07-12 20:42:55 -0400754int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert,
755 SSL *s, CBS *cbs, EVP_PKEY *pkey)
Adam Langley95c29f32014-06-20 12:00:00 -0700756 {
757 const unsigned char *sent_sigs;
758 size_t sent_sigslen, i;
759 int sigalg = tls12_get_sigid(pkey);
David Benjamin05da6e12014-07-12 20:42:55 -0400760 uint8_t hash, signature;
Adam Langley95c29f32014-06-20 12:00:00 -0700761 /* Should never happen */
762 if (sigalg == -1)
David Benjamin05da6e12014-07-12 20:42:55 -0400763 {
764 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
765 *out_alert = SSL_AD_INTERNAL_ERROR;
766 return 0;
767 }
768 if (!CBS_get_u8(cbs, &hash) ||
769 !CBS_get_u8(cbs, &signature))
770 {
771 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
772 *out_alert = SSL_AD_DECODE_ERROR;
773 return 0;
774 }
Adam Langley95c29f32014-06-20 12:00:00 -0700775 /* Check key type is consistent with signature */
David Benjamin05da6e12014-07-12 20:42:55 -0400776 if (sigalg != signature)
Adam Langley95c29f32014-06-20 12:00:00 -0700777 {
778 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin05da6e12014-07-12 20:42:55 -0400779 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700780 return 0;
781 }
Adam Langley95c29f32014-06-20 12:00:00 -0700782 if (pkey->type == EVP_PKEY_EC)
783 {
David Benjamin072334d2014-07-13 16:24:27 -0400784 uint16_t curve_id;
785 uint8_t comp_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700786 /* Check compression and curve matches extensions */
David Benjamin072334d2014-07-13 16:24:27 -0400787 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec))
David Benjamin05da6e12014-07-12 20:42:55 -0400788 {
789 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -0700790 return 0;
David Benjamin05da6e12014-07-12 20:42:55 -0400791 }
David Benjamin42e9a772014-09-02 23:18:44 -0400792 if (s->server)
Adam Langley95c29f32014-06-20 12:00:00 -0700793 {
David Benjamin42e9a772014-09-02 23:18:44 -0400794 if (!tls1_check_curve_id(s, curve_id) ||
795 !tls1_check_point_format(s, comp_id))
796 {
797 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
798 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
799 return 0;
800 }
Adam Langley95c29f32014-06-20 12:00:00 -0700801 }
David Benjamin05da6e12014-07-12 20:42:55 -0400802 }
Adam Langley95c29f32014-06-20 12:00:00 -0700803
804 /* Check signature matches a type we sent */
805 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
806 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
807 {
David Benjamin05da6e12014-07-12 20:42:55 -0400808 if (hash == sent_sigs[0] && signature == sent_sigs[1])
Adam Langley95c29f32014-06-20 12:00:00 -0700809 break;
810 }
811 /* Allow fallback to SHA1 if not strict mode */
David Benjamin05da6e12014-07-12 20:42:55 -0400812 if (i == sent_sigslen && (hash != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
Adam Langley95c29f32014-06-20 12:00:00 -0700813 {
814 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin05da6e12014-07-12 20:42:55 -0400815 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700816 return 0;
817 }
David Benjamin05da6e12014-07-12 20:42:55 -0400818 *out_md = tls12_get_hash(hash);
819 if (*out_md == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700820 {
821 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
David Benjamin05da6e12014-07-12 20:42:55 -0400822 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700823 return 0;
824 }
825 /* Store the digest used so applications can retrieve it if they
826 * wish.
827 */
828 if (s->session && s->session->sess_cert)
David Benjamin05da6e12014-07-12 20:42:55 -0400829 s->session->sess_cert->peer_key->digest = *out_md;
Adam Langley95c29f32014-06-20 12:00:00 -0700830 return 1;
831 }
832/* Get a mask of disabled algorithms: an algorithm is disabled
833 * if it isn't supported or doesn't appear in supported signature
834 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
835 * session and not global settings.
836 *
837 */
838void ssl_set_client_disabled(SSL *s)
839 {
840 CERT *c = s->cert;
841 const unsigned char *sigalgs;
842 size_t i, sigalgslen;
David Benjaminef2116d2014-08-19 20:21:56 -0400843 int have_rsa = 0, have_ecdsa = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700844 c->mask_a = 0;
845 c->mask_k = 0;
846 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
847 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
848 c->mask_ssl = SSL_TLSV1_2;
849 else
850 c->mask_ssl = 0;
851 /* Now go through all signature algorithms seeing if we support
852 * any for RSA, DSA, ECDSA. Do this for all versions not just
853 * TLS 1.2.
854 */
855 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
856 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
857 {
858 switch(sigalgs[1])
859 {
Adam Langley95c29f32014-06-20 12:00:00 -0700860 case TLSEXT_signature_rsa:
861 have_rsa = 1;
862 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700863 case TLSEXT_signature_ecdsa:
864 have_ecdsa = 1;
865 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700866 }
867 }
David Benjamin0da0e182014-08-19 16:20:28 -0400868 /* Disable auth if we don't include any appropriate signature
869 * algorithms.
Adam Langley95c29f32014-06-20 12:00:00 -0700870 */
871 if (!have_rsa)
872 {
873 c->mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700874 }
Adam Langley95c29f32014-06-20 12:00:00 -0700875 if (!have_ecdsa)
876 {
877 c->mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700878 }
Adam Langley95c29f32014-06-20 12:00:00 -0700879 /* with PSK there must be client callback set */
880 if (!s->psk_client_callback)
881 {
882 c->mask_a |= SSL_aPSK;
883 c->mask_k |= SSL_kPSK;
884 }
Adam Langley95c29f32014-06-20 12:00:00 -0700885 c->valid = 1;
886 }
887
Adam Langleyb0c235e2014-06-20 12:00:00 -0700888/* header_len is the length of the ClientHello header written so far, used to
889 * compute padding. It does not include the record header. Pass 0 if no padding
890 * is to be done. */
891unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, size_t header_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700892 {
893 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -0700894 unsigned char *ret = buf;
895 unsigned char *orig = buf;
Adam Langley95c29f32014-06-20 12:00:00 -0700896 /* See if we support any ECC ciphersuites */
897 int using_ecc = 0;
898 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
899 {
David Benjaminfb3ff2c2014-09-30 21:00:38 -0400900 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -0700901 unsigned long alg_k, alg_a;
902 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
903
904 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
905 {
David Benjamin6f260012014-08-15 13:49:12 -0400906 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700907
908 alg_k = c->algorithm_mkey;
909 alg_a = c->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -0400910 if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA))
Adam Langley95c29f32014-06-20 12:00:00 -0700911 {
912 using_ecc = 1;
913 break;
914 }
915 }
916 }
Adam Langley95c29f32014-06-20 12:00:00 -0700917
918 /* don't add extensions for SSLv3 unless doing secure renegotiation */
919 if (s->client_version == SSL3_VERSION
920 && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -0700921 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -0700922
923 ret+=2;
924
925 if (ret>=limit) return NULL; /* this really never occurs, but ... */
926
927 if (s->tlsext_hostname != NULL)
928 {
929 /* Add TLS extension servername to the Client Hello message */
930 unsigned long size_str;
931 long lenmax;
932
933 /* check for enough space.
934 4 for the servername type and entension length
935 2 for servernamelist length
936 1 for the hostname type
937 2 for hostname length
938 + hostname length
939 */
940
941 if ((lenmax = limit - ret - 9) < 0
942 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
943 return NULL;
944
945 /* extension type and length */
946 s2n(TLSEXT_TYPE_server_name,ret);
947 s2n(size_str+5,ret);
948
949 /* length of servername list */
950 s2n(size_str+3,ret);
951
952 /* hostname type, length and hostname */
953 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
954 s2n(size_str,ret);
955 memcpy(ret, s->tlsext_hostname, size_str);
956 ret+=size_str;
957 }
958
959 /* Add RI if renegotiating */
960 if (s->renegotiate)
961 {
962 int el;
963
964 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
965 {
966 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
967 return NULL;
968 }
969
Adam Langleyb0c235e2014-06-20 12:00:00 -0700970 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700971
972 s2n(TLSEXT_TYPE_renegotiate,ret);
973 s2n(el,ret);
974
975 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
976 {
977 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
978 return NULL;
979 }
980
981 ret += el;
982 }
983
Adam Langley75712922014-10-10 16:23:43 -0700984 /* Add extended master secret. */
985 if (s->version != SSL3_VERSION)
986 {
987 if (limit - ret - 4 < 0)
988 return NULL;
989 s2n(TLSEXT_TYPE_extended_master_secret,ret);
990 s2n(0,ret);
991 }
992
Adam Langley95c29f32014-06-20 12:00:00 -0700993 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
994 {
995 int ticklen;
996 if (!s->new_session && s->session && s->session->tlsext_tick)
997 ticklen = s->session->tlsext_ticklen;
998 else if (s->session && s->tlsext_session_ticket &&
999 s->tlsext_session_ticket->data)
1000 {
David Benjamin072c9532014-07-26 11:44:25 -04001001 s->session->tlsext_tick = BUF_memdup(
1002 s->tlsext_session_ticket->data,
1003 s->tlsext_session_ticket->length);
Adam Langley95c29f32014-06-20 12:00:00 -07001004 if (!s->session->tlsext_tick)
1005 return NULL;
David Benjamin072c9532014-07-26 11:44:25 -04001006 ticklen = s->tlsext_session_ticket->length;
Adam Langley95c29f32014-06-20 12:00:00 -07001007 s->session->tlsext_ticklen = ticklen;
1008 }
1009 else
1010 ticklen = 0;
1011 if (ticklen == 0 && s->tlsext_session_ticket &&
1012 s->tlsext_session_ticket->data == NULL)
1013 goto skip_ext;
1014 /* Check for enough room 2 for extension type, 2 for len
1015 * rest for ticket
1016 */
1017 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1018 s2n(TLSEXT_TYPE_session_ticket,ret);
1019 s2n(ticklen,ret);
1020 if (ticklen)
1021 {
1022 memcpy(ret, s->session->tlsext_tick, ticklen);
1023 ret += ticklen;
1024 }
1025 }
1026 skip_ext:
1027
1028 if (SSL_USE_SIGALGS(s))
1029 {
1030 size_t salglen;
1031 const unsigned char *salg;
1032 salglen = tls12_get_psigalgs(s, &salg);
1033 if ((size_t)(limit - ret) < salglen + 6)
1034 return NULL;
1035 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1036 s2n(salglen + 2, ret);
1037 s2n(salglen, ret);
1038 memcpy(ret, salg, salglen);
1039 ret += salglen;
1040 }
1041
David Benjamin6c7aed02014-08-27 16:42:38 -04001042 if (s->ocsp_stapling_enabled)
Adam Langley95c29f32014-06-20 12:00:00 -07001043 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001044 /* The status_request extension is excessively extensible at
1045 * every layer. On the client, only support requesting OCSP
1046 * responses with an empty responder_id_list and no
1047 * extensions. */
1048 if (limit - ret - 4 - 1 - 2 - 2 < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001049
Adam Langley95c29f32014-06-20 12:00:00 -07001050 s2n(TLSEXT_TYPE_status_request, ret);
David Benjamin6c7aed02014-08-27 16:42:38 -04001051 s2n(1 + 2 + 2, ret);
1052 /* status_type */
Adam Langley95c29f32014-06-20 12:00:00 -07001053 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
David Benjamin6c7aed02014-08-27 16:42:38 -04001054 /* responder_id_list - empty */
1055 s2n(0, ret);
1056 /* request_extensions - empty */
1057 s2n(0, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001058 }
Adam Langley95c29f32014-06-20 12:00:00 -07001059
Adam Langley95c29f32014-06-20 12:00:00 -07001060 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1061 {
1062 /* The client advertises an emtpy extension to indicate its
1063 * support for Next Protocol Negotiation */
1064 if (limit - ret - 4 < 0)
1065 return NULL;
1066 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1067 s2n(0,ret);
1068 }
Adam Langley95c29f32014-06-20 12:00:00 -07001069
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001070 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len)
1071 {
1072 /* The client advertises an empty extension to indicate its support for
1073 * certificate timestamps. */
1074 if (limit - ret - 4 < 0)
1075 return NULL;
1076 s2n(TLSEXT_TYPE_certificate_timestamp,ret);
1077 s2n(0,ret);
1078 }
1079
Adam Langley95c29f32014-06-20 12:00:00 -07001080 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1081 {
1082 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1083 return NULL;
1084 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1085 s2n(2 + s->alpn_client_proto_list_len,ret);
1086 s2n(s->alpn_client_proto_list_len,ret);
1087 memcpy(ret, s->alpn_client_proto_list,
1088 s->alpn_client_proto_list_len);
1089 ret += s->alpn_client_proto_list_len;
1090 }
1091
Adam Langley1258b6a2014-06-20 12:00:00 -07001092 if (s->tlsext_channel_id_enabled)
1093 {
1094 /* The client advertises an emtpy extension to indicate its
1095 * support for Channel ID. */
1096 if (limit - ret - 4 < 0)
1097 return NULL;
1098 if (s->ctx->tlsext_channel_id_enabled_new)
1099 s2n(TLSEXT_TYPE_channel_id_new,ret);
1100 else
1101 s2n(TLSEXT_TYPE_channel_id,ret);
1102 s2n(0,ret);
1103 }
1104
Adam Langley95c29f32014-06-20 12:00:00 -07001105 if(SSL_get_srtp_profiles(s))
1106 {
1107 int el;
1108
1109 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1110
Adam Langleyb0c235e2014-06-20 12:00:00 -07001111 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001112
1113 s2n(TLSEXT_TYPE_use_srtp,ret);
1114 s2n(el,ret);
1115
David Benjamin120a6742014-08-30 14:54:37 -04001116 if(!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001117 {
1118 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1119 return NULL;
1120 }
1121 ret += el;
1122 }
1123
Adam Langleyc3174b72014-06-20 12:00:00 -07001124 if (using_ecc)
1125 {
1126 /* Add TLS extension ECPointFormats to the ClientHello message */
1127 long lenmax;
David Benjamin072334d2014-07-13 16:24:27 -04001128 const uint8_t *formats;
1129 const uint16_t *curves;
1130 size_t formats_len, curves_len, i;
Adam Langleyc3174b72014-06-20 12:00:00 -07001131
David Benjamin072334d2014-07-13 16:24:27 -04001132 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001133
1134 if ((lenmax = limit - ret - 5) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001135 if (formats_len > (size_t)lenmax) return NULL;
1136 if (formats_len > 255)
Adam Langleyc3174b72014-06-20 12:00:00 -07001137 {
1138 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1139 return NULL;
1140 }
1141
1142 s2n(TLSEXT_TYPE_ec_point_formats,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001143 s2n(formats_len + 1,ret);
1144 *(ret++) = (unsigned char)formats_len;
1145 memcpy(ret, formats, formats_len);
1146 ret+=formats_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001147
1148 /* Add TLS extension EllipticCurves to the ClientHello message */
David Benjamin072334d2014-07-13 16:24:27 -04001149 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001150
1151 if ((lenmax = limit - ret - 6) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001152 if ((curves_len * 2) > (size_t)lenmax) return NULL;
1153 if ((curves_len * 2) > 65532)
Adam Langleyc3174b72014-06-20 12:00:00 -07001154 {
1155 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1156 return NULL;
1157 }
1158
1159 s2n(TLSEXT_TYPE_elliptic_curves,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001160 s2n((curves_len * 2) + 2, ret);
Adam Langleyc3174b72014-06-20 12:00:00 -07001161
1162 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1163 * elliptic_curve_list, but the examples use two bytes.
1164 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1165 * resolves this to two bytes.
1166 */
David Benjamin072334d2014-07-13 16:24:27 -04001167 s2n(curves_len * 2, ret);
1168 for (i = 0; i < curves_len; i++)
1169 {
1170 s2n(curves[i], ret);
1171 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001172 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001173
Adam Langley95c29f32014-06-20 12:00:00 -07001174#ifdef TLSEXT_TYPE_padding
1175 /* Add padding to workaround bugs in F5 terminators.
Adam Langleyb0c235e2014-06-20 12:00:00 -07001176 * See https://tools.ietf.org/html/draft-agl-tls-padding-03
Adam Langley95c29f32014-06-20 12:00:00 -07001177 *
1178 * NB: because this code works out the length of all existing
Adam Langleyb0c235e2014-06-20 12:00:00 -07001179 * extensions it MUST always appear last. */
1180 if (header_len > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001181 {
Adam Langleyb0c235e2014-06-20 12:00:00 -07001182 header_len += ret - orig;
1183 if (header_len > 0xff && header_len < 0x200)
1184 {
1185 size_t padding_len = 0x200 - header_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001186 /* Extensions take at least four bytes to encode. Always
1187 * include least one byte of data if including the
1188 * extension. WebSphere Application Server 7.0 is
1189 * intolerant to the last extension being zero-length. */
1190 if (padding_len >= 4 + 1)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001191 padding_len -= 4;
1192 else
Adam Langleyc3174b72014-06-20 12:00:00 -07001193 padding_len = 1;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001194 if (limit - ret - 4 - (long)padding_len < 0)
1195 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001196
Adam Langleyb0c235e2014-06-20 12:00:00 -07001197 s2n(TLSEXT_TYPE_padding, ret);
1198 s2n(padding_len, ret);
1199 memset(ret, 0, padding_len);
1200 ret += padding_len;
1201 }
Adam Langley95c29f32014-06-20 12:00:00 -07001202 }
1203#endif
1204
Adam Langleyb0c235e2014-06-20 12:00:00 -07001205 if ((extdatalen = ret-orig-2)== 0)
1206 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001207
Adam Langleyb0c235e2014-06-20 12:00:00 -07001208 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001209 return ret;
1210 }
1211
Adam Langleyb0c235e2014-06-20 12:00:00 -07001212unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit)
Adam Langley95c29f32014-06-20 12:00:00 -07001213 {
1214 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001215 unsigned char *orig = buf;
1216 unsigned char *ret = buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001217 int next_proto_neg_seen;
Adam Langley95c29f32014-06-20 12:00:00 -07001218 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1219 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -04001220 int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
David Benjamina19fc252014-10-19 00:14:36 -04001221 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001222 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1223 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001224 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001225
1226 ret+=2;
1227 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1228
Adam Langleyed8270a2014-09-02 13:52:56 -07001229 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001230 {
1231 if ((long)(limit - ret - 4) < 0) return NULL;
1232
1233 s2n(TLSEXT_TYPE_server_name,ret);
1234 s2n(0,ret);
1235 }
1236
1237 if(s->s3->send_connection_binding)
1238 {
1239 int el;
1240
1241 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1242 {
1243 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1244 return NULL;
1245 }
1246
Adam Langleyb0c235e2014-06-20 12:00:00 -07001247 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001248
1249 s2n(TLSEXT_TYPE_renegotiate,ret);
1250 s2n(el,ret);
1251
1252 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1253 {
1254 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1255 return NULL;
1256 }
1257
1258 ret += el;
1259 }
1260
Adam Langley75712922014-10-10 16:23:43 -07001261 if (s->s3->tmp.extended_master_secret)
1262 {
1263 if ((long)(limit - ret - 4) < 0) return NULL;
1264
1265 s2n(TLSEXT_TYPE_extended_master_secret,ret);
1266 s2n(0,ret);
1267 }
1268
Adam Langley95c29f32014-06-20 12:00:00 -07001269 if (using_ecc)
1270 {
1271 const unsigned char *plist;
1272 size_t plistlen;
1273 /* Add TLS extension ECPointFormats to the ServerHello message */
1274 long lenmax;
1275
1276 tls1_get_formatlist(s, &plist, &plistlen);
1277
1278 if ((lenmax = limit - ret - 5) < 0) return NULL;
1279 if (plistlen > (size_t)lenmax) return NULL;
1280 if (plistlen > 255)
1281 {
1282 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1283 return NULL;
1284 }
1285
1286 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1287 s2n(plistlen + 1,ret);
1288 *(ret++) = (unsigned char) plistlen;
1289 memcpy(ret, plist, plistlen);
1290 ret+=plistlen;
1291
1292 }
1293 /* Currently the server should not respond with a SupportedCurves extension */
Adam Langley95c29f32014-06-20 12:00:00 -07001294
1295 if (s->tlsext_ticket_expected
1296 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1297 {
1298 if ((long)(limit - ret - 4) < 0) return NULL;
1299 s2n(TLSEXT_TYPE_session_ticket,ret);
1300 s2n(0,ret);
1301 }
1302
David Benjamin6c7aed02014-08-27 16:42:38 -04001303 if (s->s3->tmp.certificate_status_expected)
Adam Langley95c29f32014-06-20 12:00:00 -07001304 {
1305 if ((long)(limit - ret - 4) < 0) return NULL;
1306 s2n(TLSEXT_TYPE_status_request,ret);
1307 s2n(0,ret);
1308 }
1309
Adam Langley95c29f32014-06-20 12:00:00 -07001310 if(s->srtp_profile)
1311 {
1312 int el;
1313
1314 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1315
Adam Langleyb0c235e2014-06-20 12:00:00 -07001316 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001317
1318 s2n(TLSEXT_TYPE_use_srtp,ret);
1319 s2n(el,ret);
1320
David Benjamin120a6742014-08-30 14:54:37 -04001321 if(!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001322 {
1323 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1324 return NULL;
1325 }
1326 ret+=el;
1327 }
1328
Adam Langley95c29f32014-06-20 12:00:00 -07001329 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1330 s->s3->next_proto_neg_seen = 0;
1331 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1332 {
1333 const unsigned char *npa;
1334 unsigned int npalen;
1335 int r;
1336
1337 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1338 if (r == SSL_TLSEXT_ERR_OK)
1339 {
1340 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1341 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1342 s2n(npalen,ret);
1343 memcpy(ret, npa, npalen);
1344 ret += npalen;
1345 s->s3->next_proto_neg_seen = 1;
1346 }
1347 }
Adam Langley95c29f32014-06-20 12:00:00 -07001348
Adam Langley95c29f32014-06-20 12:00:00 -07001349 if (s->s3->alpn_selected)
1350 {
David Benjamin03973092014-06-24 23:27:17 -04001351 const uint8_t *selected = s->s3->alpn_selected;
1352 size_t len = s->s3->alpn_selected_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001353
1354 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1355 return NULL;
1356 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1357 s2n(3 + len,ret);
1358 s2n(1 + len,ret);
1359 *ret++ = len;
1360 memcpy(ret, selected, len);
1361 ret += len;
1362 }
1363
Adam Langley1258b6a2014-06-20 12:00:00 -07001364 /* If the client advertised support for Channel ID, and we have it
1365 * enabled, then we want to echo it back. */
1366 if (s->s3->tlsext_channel_id_valid)
1367 {
1368 if (limit - ret - 4 < 0)
1369 return NULL;
1370 if (s->s3->tlsext_channel_id_new)
1371 s2n(TLSEXT_TYPE_channel_id_new,ret);
1372 else
1373 s2n(TLSEXT_TYPE_channel_id,ret);
1374 s2n(0,ret);
1375 }
1376
Adam Langleyb0c235e2014-06-20 12:00:00 -07001377 if ((extdatalen = ret-orig-2) == 0)
1378 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001379
Adam Langleyb0c235e2014-06-20 12:00:00 -07001380 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001381 return ret;
1382 }
1383
Adam Langley95c29f32014-06-20 12:00:00 -07001384/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1385 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001386 * cbs: the contents of the extension, not including the type and length.
1387 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001388 * return.
1389 *
David Benjamindc72ff72014-06-25 12:36:10 -04001390 * returns: 1 on success. */
1391static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001392 {
Adam Langleyded93582014-07-31 15:23:51 -07001393 CBS protocol_name_list, protocol_name_list_copy;
Adam Langley95c29f32014-06-20 12:00:00 -07001394 const unsigned char *selected;
1395 unsigned char selected_len;
1396 int r;
1397
1398 if (s->ctx->alpn_select_cb == NULL)
David Benjamindc72ff72014-06-25 12:36:10 -04001399 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001400
David Benjamindc72ff72014-06-25 12:36:10 -04001401 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1402 CBS_len(cbs) != 0 ||
1403 CBS_len(&protocol_name_list) < 2)
Adam Langley95c29f32014-06-20 12:00:00 -07001404 goto parse_error;
1405
David Benjamindc72ff72014-06-25 12:36:10 -04001406 /* Validate the protocol list. */
Adam Langleyded93582014-07-31 15:23:51 -07001407 protocol_name_list_copy = protocol_name_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001408 while (CBS_len(&protocol_name_list_copy) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001409 {
David Benjamindc72ff72014-06-25 12:36:10 -04001410 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001411
David Benjamindc72ff72014-06-25 12:36:10 -04001412 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001413 goto parse_error;
Adam Langley95c29f32014-06-20 12:00:00 -07001414 }
1415
David Benjamindc72ff72014-06-25 12:36:10 -04001416 r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1417 CBS_data(&protocol_name_list), CBS_len(&protocol_name_list),
1418 s->ctx->alpn_select_cb_arg);
Adam Langley95c29f32014-06-20 12:00:00 -07001419 if (r == SSL_TLSEXT_ERR_OK) {
1420 if (s->s3->alpn_selected)
1421 OPENSSL_free(s->s3->alpn_selected);
David Benjamin072c9532014-07-26 11:44:25 -04001422 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001423 if (!s->s3->alpn_selected)
1424 {
David Benjamindc72ff72014-06-25 12:36:10 -04001425 *out_alert = SSL_AD_INTERNAL_ERROR;
1426 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001427 }
Adam Langley95c29f32014-06-20 12:00:00 -07001428 s->s3->alpn_selected_len = selected_len;
1429 }
David Benjamindc72ff72014-06-25 12:36:10 -04001430 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001431
1432parse_error:
David Benjamindc72ff72014-06-25 12:36:10 -04001433 *out_alert = SSL_AD_DECODE_ERROR;
1434 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001435 }
1436
David Benjamindc72ff72014-06-25 12:36:10 -04001437static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001438 {
Adam Langley95c29f32014-06-20 12:00:00 -07001439 int renegotiate_seen = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001440 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001441 size_t i;
1442
Adam Langleyed8270a2014-09-02 13:52:56 -07001443 s->should_ack_sni = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001444 s->s3->next_proto_neg_seen = 0;
David Benjamin6c7aed02014-08-27 16:42:38 -04001445 s->s3->tmp.certificate_status_expected = 0;
Adam Langley75712922014-10-10 16:23:43 -07001446 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001447
Adam Langley95c29f32014-06-20 12:00:00 -07001448 if (s->s3->alpn_selected)
1449 {
1450 OPENSSL_free(s->s3->alpn_selected);
1451 s->s3->alpn_selected = NULL;
1452 }
1453
Adam Langley95c29f32014-06-20 12:00:00 -07001454 /* Clear any signature algorithms extension received */
1455 if (s->cert->peer_sigalgs)
1456 {
1457 OPENSSL_free(s->cert->peer_sigalgs);
1458 s->cert->peer_sigalgs = NULL;
1459 }
David Benjamina19fc252014-10-19 00:14:36 -04001460 /* Clear any shared signature algorithms */
Adam Langley95c29f32014-06-20 12:00:00 -07001461 if (s->cert->shared_sigalgs)
1462 {
1463 OPENSSL_free(s->cert->shared_sigalgs);
1464 s->cert->shared_sigalgs = NULL;
1465 }
1466 /* Clear certificate digests and validity flags */
1467 for (i = 0; i < SSL_PKEY_NUM; i++)
1468 {
1469 s->cert->pkeys[i].digest = NULL;
1470 s->cert->pkeys[i].valid_flags = 0;
1471 }
David Benjamina19fc252014-10-19 00:14:36 -04001472 /* Clear ECC extensions */
1473 if (s->s3->tmp.peer_ecpointformatlist != 0)
1474 {
1475 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1476 s->s3->tmp.peer_ecpointformatlist = NULL;
1477 s->s3->tmp.peer_ecpointformatlist_length = 0;
1478 }
1479 if (s->s3->tmp.peer_ellipticcurvelist != 0)
1480 {
1481 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1482 s->s3->tmp.peer_ellipticcurvelist = NULL;
1483 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1484 }
Adam Langley95c29f32014-06-20 12:00:00 -07001485
David Benjamindc72ff72014-06-25 12:36:10 -04001486 /* There may be no extensions. */
1487 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001488 {
David Benjamindc72ff72014-06-25 12:36:10 -04001489 goto ri_check;
1490 }
Adam Langley95c29f32014-06-20 12:00:00 -07001491
David Benjamin35a7a442014-07-05 00:23:20 -04001492 /* Decode the extensions block and check it is valid. */
1493 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1494 !tls1_check_duplicate_extensions(&extensions))
David Benjamindc72ff72014-06-25 12:36:10 -04001495 {
1496 *out_alert = SSL_AD_DECODE_ERROR;
1497 return 0;
1498 }
1499
David Benjamindc72ff72014-06-25 12:36:10 -04001500 while (CBS_len(&extensions) != 0)
1501 {
1502 uint16_t type;
1503 CBS extension;
1504
1505 /* Decode the next extension. */
1506 if (!CBS_get_u16(&extensions, &type) ||
1507 !CBS_get_u16_length_prefixed(&extensions, &extension))
1508 {
1509 *out_alert = SSL_AD_DECODE_ERROR;
1510 return 0;
1511 }
1512
Adam Langley95c29f32014-06-20 12:00:00 -07001513 if (s->tlsext_debug_cb)
David Benjamindc72ff72014-06-25 12:36:10 -04001514 {
1515 s->tlsext_debug_cb(s, 0, type, (unsigned char*)CBS_data(&extension),
1516 CBS_len(&extension), s->tlsext_debug_arg);
1517 }
1518
Adam Langley95c29f32014-06-20 12:00:00 -07001519/* The servername extension is treated as follows:
1520
1521 - Only the hostname type is supported with a maximum length of 255.
1522 - The servername is rejected if too long or if it contains zeros,
1523 in which case an fatal alert is generated.
1524 - The servername field is maintained together with the session cache.
1525 - When a session is resumed, the servername call back invoked in order
1526 to allow the application to position itself to the right context.
1527 - The servername is acknowledged if it is new for a session or when
1528 it is identical to a previously used for the same session.
1529 Applications can control the behaviour. They can at any time
1530 set a 'desirable' servername for a new SSL object. This can be the
1531 case for example with HTTPS when a Host: header field is received and
1532 a renegotiation is requested. In this case, a possible servername
1533 presented in the new client hello is only acknowledged if it matches
1534 the value of the Host: field.
1535 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1536 if they provide for changing an explicit servername context for the session,
1537 i.e. when the session has been established with a servername extension.
1538 - On session reconnect, the servername extension may be absent.
1539
1540*/
1541
1542 if (type == TLSEXT_TYPE_server_name)
1543 {
David Benjamindc72ff72014-06-25 12:36:10 -04001544 CBS server_name_list;
Adam Langleyed8270a2014-09-02 13:52:56 -07001545 char have_seen_host_name = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001546
1547 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1548 CBS_len(&server_name_list) < 1 ||
1549 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001550 {
David Benjamindc72ff72014-06-25 12:36:10 -04001551 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001552 return 0;
1553 }
Adam Langley95c29f32014-06-20 12:00:00 -07001554
David Benjamindc72ff72014-06-25 12:36:10 -04001555 /* Decode each ServerName in the extension. */
1556 while (CBS_len(&server_name_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001557 {
David Benjamindc72ff72014-06-25 12:36:10 -04001558 uint8_t name_type;
1559 CBS host_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001560
David Benjamindc72ff72014-06-25 12:36:10 -04001561 /* Decode the NameType. */
1562 if (!CBS_get_u8(&server_name_list, &name_type))
Adam Langley95c29f32014-06-20 12:00:00 -07001563 {
David Benjamindc72ff72014-06-25 12:36:10 -04001564 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001565 return 0;
1566 }
David Benjamindc72ff72014-06-25 12:36:10 -04001567
David Benjamindc72ff72014-06-25 12:36:10 -04001568 /* Only host_name is supported. */
1569 if (name_type != TLSEXT_NAMETYPE_host_name)
1570 continue;
1571
Adam Langleyed8270a2014-09-02 13:52:56 -07001572 if (have_seen_host_name)
1573 {
1574 /* The ServerNameList MUST NOT contain
1575 * more than one name of the same
1576 * name_type. */
1577 *out_alert = SSL_AD_DECODE_ERROR;
1578 return 0;
1579 }
1580
1581 have_seen_host_name = 1;
1582
1583 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1584 CBS_len(&host_name) < 1)
1585 {
1586 *out_alert = SSL_AD_DECODE_ERROR;
1587 return 0;
1588 }
1589
1590 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1591 CBS_contains_zero_byte(&host_name))
1592 {
1593 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1594 return 0;
1595 }
1596
David Benjamindc72ff72014-06-25 12:36:10 -04001597 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -07001598 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001599 assert(s->session->tlsext_hostname == NULL);
David Benjamindc72ff72014-06-25 12:36:10 -04001600 if (s->session->tlsext_hostname)
Adam Langley95c29f32014-06-20 12:00:00 -07001601 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001602 /* This should be impossible. */
David Benjamindc72ff72014-06-25 12:36:10 -04001603 *out_alert = SSL_AD_DECODE_ERROR;
1604 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001605 }
Adam Langley95c29f32014-06-20 12:00:00 -07001606
David Benjamindc72ff72014-06-25 12:36:10 -04001607 /* Copy the hostname as a string. */
David Benjamined439582014-07-14 19:13:02 -04001608 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname))
David Benjamindc72ff72014-06-25 12:36:10 -04001609 {
1610 *out_alert = SSL_AD_INTERNAL_ERROR;
1611 return 0;
1612 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001613
1614 s->should_ack_sni = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001615 }
Adam Langley95c29f32014-06-20 12:00:00 -07001616 }
Adam Langley95c29f32014-06-20 12:00:00 -07001617 }
1618
Adam Langley95c29f32014-06-20 12:00:00 -07001619 else if (type == TLSEXT_TYPE_ec_point_formats)
1620 {
David Benjamindc72ff72014-06-25 12:36:10 -04001621 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001622
David Benjamindc72ff72014-06-25 12:36:10 -04001623 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1624 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001625 {
David Benjamindc72ff72014-06-25 12:36:10 -04001626 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001627 return 0;
1628 }
David Benjamindc72ff72014-06-25 12:36:10 -04001629
David Benjamina19fc252014-10-19 00:14:36 -04001630 if (!CBS_stow(&ec_point_format_list,
1631 &s->s3->tmp.peer_ecpointformatlist,
1632 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001633 {
David Benjamina19fc252014-10-19 00:14:36 -04001634 *out_alert = SSL_AD_INTERNAL_ERROR;
1635 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001636 }
Adam Langley95c29f32014-06-20 12:00:00 -07001637 }
1638 else if (type == TLSEXT_TYPE_elliptic_curves)
1639 {
David Benjamindc72ff72014-06-25 12:36:10 -04001640 CBS elliptic_curve_list;
David Benjamin072334d2014-07-13 16:24:27 -04001641 size_t i, num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001642
David Benjamindc72ff72014-06-25 12:36:10 -04001643 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
David Benjamin072334d2014-07-13 16:24:27 -04001644 CBS_len(&elliptic_curve_list) == 0 ||
1645 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
David Benjamindc72ff72014-06-25 12:36:10 -04001646 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001647 {
David Benjamindc72ff72014-06-25 12:36:10 -04001648 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001649 return 0;
1650 }
David Benjamindc72ff72014-06-25 12:36:10 -04001651
David Benjamina19fc252014-10-19 00:14:36 -04001652 if (s->s3->tmp.peer_ellipticcurvelist)
Adam Langley95c29f32014-06-20 12:00:00 -07001653 {
David Benjamina19fc252014-10-19 00:14:36 -04001654 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1655 s->s3->tmp.peer_ellipticcurvelist_length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001656 }
David Benjamina19fc252014-10-19 00:14:36 -04001657 s->s3->tmp.peer_ellipticcurvelist =
1658 (uint16_t*)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
1659 if (s->s3->tmp.peer_ellipticcurvelist == NULL)
1660 {
1661 *out_alert = SSL_AD_INTERNAL_ERROR;
1662 return 0;
1663 }
1664 num_curves = CBS_len(&elliptic_curve_list) / 2;
1665 for (i = 0; i < num_curves; i++)
1666 {
1667 if (!CBS_get_u16(&elliptic_curve_list,
1668 &s->s3->tmp.peer_ellipticcurvelist[i]))
1669 {
1670 *out_alert = SSL_AD_INTERNAL_ERROR;
1671 return 0;
1672 }
1673 }
1674 if (CBS_len(&elliptic_curve_list) != 0)
1675 {
1676 *out_alert = SSL_AD_INTERNAL_ERROR;
1677 return 0;
1678 }
1679 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001680 }
Adam Langley95c29f32014-06-20 12:00:00 -07001681 else if (type == TLSEXT_TYPE_session_ticket)
1682 {
1683 if (s->tls_session_ticket_ext_cb &&
David Benjamindc72ff72014-06-25 12:36:10 -04001684 !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension), s->tls_session_ticket_ext_cb_arg))
Adam Langley95c29f32014-06-20 12:00:00 -07001685 {
David Benjamindc72ff72014-06-25 12:36:10 -04001686 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001687 return 0;
1688 }
1689 }
1690 else if (type == TLSEXT_TYPE_renegotiate)
1691 {
David Benjamindc72ff72014-06-25 12:36:10 -04001692 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001693 return 0;
1694 renegotiate_seen = 1;
1695 }
1696 else if (type == TLSEXT_TYPE_signature_algorithms)
1697 {
David Benjamindc72ff72014-06-25 12:36:10 -04001698 CBS supported_signature_algorithms;
1699
David Benjamindc72ff72014-06-25 12:36:10 -04001700 if (!CBS_get_u16_length_prefixed(&extension, &supported_signature_algorithms) ||
1701 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001702 {
David Benjamindc72ff72014-06-25 12:36:10 -04001703 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001704 return 0;
1705 }
David Benjamindc72ff72014-06-25 12:36:10 -04001706
1707 /* Ensure the signature algorithms are non-empty. It
1708 * contains a list of SignatureAndHashAlgorithms
1709 * which are two bytes each. */
1710 if (CBS_len(&supported_signature_algorithms) == 0 ||
1711 (CBS_len(&supported_signature_algorithms) % 2) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001712 {
David Benjamindc72ff72014-06-25 12:36:10 -04001713 *out_alert = SSL_AD_DECODE_ERROR;
1714 return 0;
1715 }
1716
David Benjamincd996942014-07-20 16:23:51 -04001717 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
David Benjamindc72ff72014-06-25 12:36:10 -04001718 {
1719 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001720 return 0;
1721 }
1722 /* If sigalgs received and no shared algorithms fatal
1723 * error.
1724 */
1725 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
1726 {
1727 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
David Benjamindc72ff72014-06-25 12:36:10 -04001728 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07001729 return 0;
1730 }
1731 }
1732
Adam Langley95c29f32014-06-20 12:00:00 -07001733 else if (type == TLSEXT_TYPE_next_proto_neg &&
1734 s->s3->tmp.finish_md_len == 0 &&
1735 s->s3->alpn_selected == NULL)
1736 {
David Benjamindc72ff72014-06-25 12:36:10 -04001737 /* The extension must be empty. */
1738 if (CBS_len(&extension) != 0)
1739 {
1740 *out_alert = SSL_AD_DECODE_ERROR;
1741 return 0;
1742 }
1743
Adam Langley95c29f32014-06-20 12:00:00 -07001744 /* We shouldn't accept this extension on a
1745 * renegotiation.
1746 *
1747 * s->new_session will be set on renegotiation, but we
1748 * probably shouldn't rely that it couldn't be set on
1749 * the initial renegotation too in certain cases (when
1750 * there's some other reason to disallow resuming an
1751 * earlier session -- the current code won't be doing
1752 * anything like that, but this might change).
1753
1754 * A valid sign that there's been a previous handshake
1755 * in this connection is if s->s3->tmp.finish_md_len >
1756 * 0. (We are talking about a check that will happen
1757 * in the Hello protocol round, well before a new
1758 * Finished message could have been computed.) */
1759 s->s3->next_proto_neg_seen = 1;
1760 }
Adam Langley95c29f32014-06-20 12:00:00 -07001761
1762 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1763 s->ctx->alpn_select_cb &&
1764 s->s3->tmp.finish_md_len == 0)
1765 {
David Benjamindc72ff72014-06-25 12:36:10 -04001766 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001767 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001768 /* ALPN takes precedence over NPN. */
1769 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001770 }
1771
Adam Langley1258b6a2014-06-20 12:00:00 -07001772 else if (type == TLSEXT_TYPE_channel_id &&
1773 s->tlsext_channel_id_enabled)
David Benjamindc72ff72014-06-25 12:36:10 -04001774 {
1775 /* The extension must be empty. */
1776 if (CBS_len(&extension) != 0)
1777 {
1778 *out_alert = SSL_AD_DECODE_ERROR;
1779 return 0;
1780 }
1781
Adam Langley1258b6a2014-06-20 12:00:00 -07001782 s->s3->tlsext_channel_id_valid = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001783 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001784
1785 else if (type == TLSEXT_TYPE_channel_id_new &&
1786 s->tlsext_channel_id_enabled)
1787 {
David Benjamindc72ff72014-06-25 12:36:10 -04001788 /* The extension must be empty. */
1789 if (CBS_len(&extension) != 0)
1790 {
1791 *out_alert = SSL_AD_DECODE_ERROR;
1792 return 0;
1793 }
1794
Adam Langley1258b6a2014-06-20 12:00:00 -07001795 s->s3->tlsext_channel_id_valid = 1;
1796 s->s3->tlsext_channel_id_new = 1;
1797 }
1798
1799
Adam Langley95c29f32014-06-20 12:00:00 -07001800 /* session ticket processed earlier */
1801 else if (type == TLSEXT_TYPE_use_srtp)
1802 {
David Benjamindc72ff72014-06-25 12:36:10 -04001803 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001804 return 0;
1805 }
Adam Langley75712922014-10-10 16:23:43 -07001806
1807 else if (type == TLSEXT_TYPE_extended_master_secret &&
1808 s->version != SSL3_VERSION)
1809 {
1810 if (CBS_len(&extension) != 0)
1811 {
1812 *out_alert = SSL_AD_DECODE_ERROR;
1813 return 0;
1814 }
1815
1816 s->s3->tmp.extended_master_secret = 1;
1817 }
Adam Langley95c29f32014-06-20 12:00:00 -07001818 }
1819
Adam Langley95c29f32014-06-20 12:00:00 -07001820 ri_check:
1821
1822 /* Need RI if renegotiating */
1823
1824 if (!renegotiate_seen && s->renegotiate &&
1825 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1826 {
David Benjamindc72ff72014-06-25 12:36:10 -04001827 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin172fc2c2014-09-06 13:09:47 -04001828 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07001829 return 0;
1830 }
1831 /* If no signature algorithms extension set default values */
1832 if (!s->cert->peer_sigalgs)
1833 ssl_cert_set_default_md(s->cert);
1834
1835 return 1;
1836 }
1837
David Benjamindc72ff72014-06-25 12:36:10 -04001838int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001839 {
David Benjamindc72ff72014-06-25 12:36:10 -04001840 int alert = -1;
1841 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001842 {
David Benjamindc72ff72014-06-25 12:36:10 -04001843 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07001844 return 0;
1845 }
1846
David Benjamin6c7aed02014-08-27 16:42:38 -04001847 if (ssl_check_clienthello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001848 {
David Benjamin9d28c752014-07-05 00:43:48 -04001849 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07001850 return 0;
1851 }
1852 return 1;
David Benjamin072334d2014-07-13 16:24:27 -04001853 }
Adam Langley95c29f32014-06-20 12:00:00 -07001854
Adam Langley95c29f32014-06-20 12:00:00 -07001855/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1856 * elements of zero length are allowed and the set of elements must exactly fill
1857 * the length of the block. */
David Benjamin03973092014-06-24 23:27:17 -04001858static char ssl_next_proto_validate(const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001859 {
David Benjamin03973092014-06-24 23:27:17 -04001860 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001861
David Benjamin03973092014-06-24 23:27:17 -04001862 while (CBS_len(&copy) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001863 {
David Benjamin03973092014-06-24 23:27:17 -04001864 CBS proto;
1865 if (!CBS_get_u8_length_prefixed(&copy, &proto) ||
1866 CBS_len(&proto) == 0)
1867 {
Adam Langley95c29f32014-06-20 12:00:00 -07001868 return 0;
David Benjamin03973092014-06-24 23:27:17 -04001869 }
Adam Langley95c29f32014-06-20 12:00:00 -07001870 }
David Benjamin03973092014-06-24 23:27:17 -04001871 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001872 }
Adam Langley95c29f32014-06-20 12:00:00 -07001873
David Benjamin03973092014-06-24 23:27:17 -04001874static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001875 {
Adam Langley95c29f32014-06-20 12:00:00 -07001876 int tlsext_servername = 0;
1877 int renegotiate_seen = 0;
David Benjamin03973092014-06-24 23:27:17 -04001878 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001879
David Benjamin6c7aed02014-08-27 16:42:38 -04001880 /* TODO(davidben): Move all of these to some per-handshake state that
1881 * gets systematically reset on a new handshake; perhaps allocate it
1882 * fresh each time so it's not even kept around post-handshake. */
Adam Langley95c29f32014-06-20 12:00:00 -07001883 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001884
David Benjamin6c7aed02014-08-27 16:42:38 -04001885 s->tlsext_ticket_expected = 0;
1886 s->s3->tmp.certificate_status_expected = 0;
Adam Langley75712922014-10-10 16:23:43 -07001887 s->s3->tmp.extended_master_secret = 0;
David Benjamin64442872014-07-21 17:43:45 -04001888
Adam Langley95c29f32014-06-20 12:00:00 -07001889 if (s->s3->alpn_selected)
1890 {
1891 OPENSSL_free(s->s3->alpn_selected);
1892 s->s3->alpn_selected = NULL;
1893 }
1894
David Benjamina19fc252014-10-19 00:14:36 -04001895 /* Clear ECC extensions */
1896 if (s->s3->tmp.peer_ecpointformatlist != 0)
1897 {
1898 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1899 s->s3->tmp.peer_ecpointformatlist = NULL;
1900 s->s3->tmp.peer_ecpointformatlist_length = 0;
1901 }
1902
David Benjamin03973092014-06-24 23:27:17 -04001903 /* There may be no extensions. */
1904 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001905 {
David Benjamin03973092014-06-24 23:27:17 -04001906 goto ri_check;
1907 }
1908
David Benjamin35a7a442014-07-05 00:23:20 -04001909 /* Decode the extensions block and check it is valid. */
1910 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1911 !tls1_check_duplicate_extensions(&extensions))
David Benjamin03973092014-06-24 23:27:17 -04001912 {
1913 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001914 return 0;
1915 }
1916
David Benjamin03973092014-06-24 23:27:17 -04001917 while (CBS_len(&extensions) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001918 {
David Benjamin03973092014-06-24 23:27:17 -04001919 uint16_t type;
1920 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001921
David Benjamin03973092014-06-24 23:27:17 -04001922 /* Decode the next extension. */
1923 if (!CBS_get_u16(&extensions, &type) ||
1924 !CBS_get_u16_length_prefixed(&extensions, &extension))
1925 {
1926 *out_alert = SSL_AD_DECODE_ERROR;
1927 return 0;
1928 }
Adam Langley95c29f32014-06-20 12:00:00 -07001929
1930 if (s->tlsext_debug_cb)
David Benjamin03973092014-06-24 23:27:17 -04001931 {
1932 s->tlsext_debug_cb(s, 1, type, (unsigned char*)CBS_data(&extension),
1933 CBS_len(&extension), s->tlsext_debug_arg);
1934 }
Adam Langley95c29f32014-06-20 12:00:00 -07001935
1936 if (type == TLSEXT_TYPE_server_name)
1937 {
David Benjamin03973092014-06-24 23:27:17 -04001938 /* The extension must be empty. */
1939 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001940 {
David Benjamin03973092014-06-24 23:27:17 -04001941 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001942 return 0;
1943 }
David Benjamin03973092014-06-24 23:27:17 -04001944 /* We must have sent it in ClientHello. */
1945 if (s->tlsext_hostname == NULL)
1946 {
1947 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1948 return 0;
1949 }
1950 tlsext_servername = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001951 }
Adam Langley95c29f32014-06-20 12:00:00 -07001952 else if (type == TLSEXT_TYPE_ec_point_formats)
1953 {
David Benjamin03973092014-06-24 23:27:17 -04001954 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001955
David Benjamin03973092014-06-24 23:27:17 -04001956 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1957 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001958 {
David Benjamin03973092014-06-24 23:27:17 -04001959 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001960 return 0;
1961 }
David Benjamin03973092014-06-24 23:27:17 -04001962
David Benjamina19fc252014-10-19 00:14:36 -04001963 if (!CBS_stow(&ec_point_format_list,
1964 &s->s3->tmp.peer_ecpointformatlist,
1965 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001966 {
David Benjamina19fc252014-10-19 00:14:36 -04001967 *out_alert = SSL_AD_INTERNAL_ERROR;
1968 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001969 }
Adam Langley95c29f32014-06-20 12:00:00 -07001970 }
Adam Langley95c29f32014-06-20 12:00:00 -07001971 else if (type == TLSEXT_TYPE_session_ticket)
1972 {
1973 if (s->tls_session_ticket_ext_cb &&
David Benjamin03973092014-06-24 23:27:17 -04001974 !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension),
1975 s->tls_session_ticket_ext_cb_arg))
Adam Langley95c29f32014-06-20 12:00:00 -07001976 {
David Benjamin03973092014-06-24 23:27:17 -04001977 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001978 return 0;
1979 }
David Benjamin03973092014-06-24 23:27:17 -04001980
1981 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001982 {
David Benjamin03973092014-06-24 23:27:17 -04001983 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07001984 return 0;
1985 }
David Benjamin03973092014-06-24 23:27:17 -04001986
Adam Langley95c29f32014-06-20 12:00:00 -07001987 s->tlsext_ticket_expected = 1;
1988 }
Adam Langley95c29f32014-06-20 12:00:00 -07001989 else if (type == TLSEXT_TYPE_status_request)
1990 {
David Benjamin03973092014-06-24 23:27:17 -04001991 /* The extension MUST be empty and may only sent if
1992 * we've requested a status request message. */
1993 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001994 {
David Benjamin03973092014-06-24 23:27:17 -04001995 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001996 return 0;
1997 }
David Benjamin6c7aed02014-08-27 16:42:38 -04001998 if (!s->ocsp_stapling_enabled)
David Benjamin03973092014-06-24 23:27:17 -04001999 {
2000 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2001 return 0;
2002 }
2003 /* Set a flag to expect a CertificateStatus message */
David Benjamin6c7aed02014-08-27 16:42:38 -04002004 s->s3->tmp.certificate_status_expected = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002005 }
David Benjamin03973092014-06-24 23:27:17 -04002006 else if (type == TLSEXT_TYPE_next_proto_neg && s->s3->tmp.finish_md_len == 0) {
2007 unsigned char *selected;
2008 unsigned char selected_len;
2009
2010 /* We must have requested it. */
2011 if (s->ctx->next_proto_select_cb == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002012 {
David Benjamin03973092014-06-24 23:27:17 -04002013 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2014 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002015 }
Adam Langley95c29f32014-06-20 12:00:00 -07002016
David Benjamin03973092014-06-24 23:27:17 -04002017 /* The data must be valid. */
2018 if (!ssl_next_proto_validate(&extension))
2019 {
2020 *out_alert = SSL_AD_DECODE_ERROR;
2021 return 0;
2022 }
2023
2024 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len,
2025 CBS_data(&extension), CBS_len(&extension),
2026 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
2027 {
2028 *out_alert = SSL_AD_INTERNAL_ERROR;
2029 return 0;
2030 }
2031
2032 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
2033 if (s->next_proto_negotiated == NULL)
2034 {
2035 *out_alert = SSL_AD_INTERNAL_ERROR;
2036 return 0;
2037 }
2038 s->next_proto_negotiated_len = selected_len;
2039 s->s3->next_proto_neg_seen = 1;
2040 }
Adam Langley95c29f32014-06-20 12:00:00 -07002041 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
2042 {
David Benjamin03973092014-06-24 23:27:17 -04002043 CBS protocol_name_list, protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07002044
2045 /* We must have requested it. */
2046 if (s->alpn_client_proto_list == NULL)
2047 {
David Benjamin03973092014-06-24 23:27:17 -04002048 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07002049 return 0;
2050 }
David Benjamin03973092014-06-24 23:27:17 -04002051
2052 /* The extension data consists of a ProtocolNameList
2053 * which must have exactly one ProtocolName. Each of
2054 * these is length-prefixed. */
2055 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
2056 CBS_len(&extension) != 0 ||
2057 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
2058 CBS_len(&protocol_name_list) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002059 {
David Benjamin03973092014-06-24 23:27:17 -04002060 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002061 return 0;
2062 }
David Benjamin03973092014-06-24 23:27:17 -04002063
2064 if (!CBS_stow(&protocol_name,
2065 &s->s3->alpn_selected,
2066 &s->s3->alpn_selected_len))
Adam Langley95c29f32014-06-20 12:00:00 -07002067 {
David Benjamin03973092014-06-24 23:27:17 -04002068 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002069 return 0;
2070 }
Adam Langley95c29f32014-06-20 12:00:00 -07002071 }
2072
Adam Langley1258b6a2014-06-20 12:00:00 -07002073 else if (type == TLSEXT_TYPE_channel_id)
David Benjamin03973092014-06-24 23:27:17 -04002074 {
2075 if (CBS_len(&extension) != 0)
2076 {
2077 *out_alert = SSL_AD_DECODE_ERROR;
2078 return 0;
2079 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002080 s->s3->tlsext_channel_id_valid = 1;
David Benjamin03973092014-06-24 23:27:17 -04002081 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002082 else if (type == TLSEXT_TYPE_channel_id_new)
2083 {
David Benjamin03973092014-06-24 23:27:17 -04002084 if (CBS_len(&extension) != 0)
2085 {
2086 *out_alert = SSL_AD_DECODE_ERROR;
2087 return 0;
2088 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002089 s->s3->tlsext_channel_id_valid = 1;
2090 s->s3->tlsext_channel_id_new = 1;
2091 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002092 else if (type == TLSEXT_TYPE_certificate_timestamp)
2093 {
2094 if (CBS_len(&extension) == 0)
2095 {
2096 *out_alert = SSL_AD_DECODE_ERROR;
2097 return 0;
2098 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002099
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002100 /* Session resumption uses the original session information. */
2101 if (!s->hit)
2102 {
2103 if (!CBS_stow(&extension,
2104 &s->session->tlsext_signed_cert_timestamp_list,
2105 &s->session->tlsext_signed_cert_timestamp_list_length))
2106 {
2107 *out_alert = SSL_AD_INTERNAL_ERROR;
2108 return 0;
2109 }
2110 }
2111 }
Adam Langley95c29f32014-06-20 12:00:00 -07002112 else if (type == TLSEXT_TYPE_renegotiate)
2113 {
David Benjamin03973092014-06-24 23:27:17 -04002114 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002115 return 0;
2116 renegotiate_seen = 1;
2117 }
Adam Langley95c29f32014-06-20 12:00:00 -07002118 else if (type == TLSEXT_TYPE_use_srtp)
2119 {
David Benjamin03973092014-06-24 23:27:17 -04002120 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002121 return 0;
2122 }
Adam Langley75712922014-10-10 16:23:43 -07002123
2124 else if (type == TLSEXT_TYPE_extended_master_secret)
2125 {
2126 if (/* It is invalid for the server to select EMS and
2127 SSLv3. */
2128 s->version == SSL3_VERSION ||
2129 CBS_len(&extension) != 0)
2130 {
2131 *out_alert = SSL_AD_DECODE_ERROR;
2132 return 0;
2133 }
2134
2135 s->s3->tmp.extended_master_secret = 1;
2136 }
Adam Langley95c29f32014-06-20 12:00:00 -07002137 }
2138
2139 if (!s->hit && tlsext_servername == 1)
2140 {
2141 if (s->tlsext_hostname)
2142 {
2143 if (s->session->tlsext_hostname == NULL)
2144 {
2145 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2146 if (!s->session->tlsext_hostname)
2147 {
David Benjamin03973092014-06-24 23:27:17 -04002148 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002149 return 0;
2150 }
2151 }
2152 else
2153 {
David Benjamin03973092014-06-24 23:27:17 -04002154 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002155 return 0;
2156 }
2157 }
2158 }
2159
Adam Langley95c29f32014-06-20 12:00:00 -07002160 ri_check:
2161
2162 /* Determine if we need to see RI. Strictly speaking if we want to
2163 * avoid an attack we should *always* see RI even on initial server
2164 * hello because the client doesn't see any renegotiation during an
2165 * attack. However this would mean we could not connect to any server
2166 * which doesn't support RI so for the immediate future tolerate RI
2167 * absence on initial connect only.
2168 */
2169 if (!renegotiate_seen
2170 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2171 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2172 {
David Benjamin03973092014-06-24 23:27:17 -04002173 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin9d28c752014-07-05 00:43:48 -04002174 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07002175 return 0;
2176 }
2177
2178 return 1;
2179 }
2180
2181
2182int ssl_prepare_clienthello_tlsext(SSL *s)
2183 {
Adam Langley95c29f32014-06-20 12:00:00 -07002184 return 1;
2185 }
2186
2187int ssl_prepare_serverhello_tlsext(SSL *s)
2188 {
2189 return 1;
2190 }
2191
David Benjamin6c7aed02014-08-27 16:42:38 -04002192static int ssl_check_clienthello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002193 {
2194 int ret=SSL_TLSEXT_ERR_NOACK;
2195 int al = SSL_AD_UNRECOGNIZED_NAME;
2196
Adam Langley95c29f32014-06-20 12:00:00 -07002197 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2198 * ssl3_choose_cipher in s3_lib.c.
2199 */
2200 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2201 * ssl3_choose_cipher in s3_lib.c.
2202 */
Adam Langley95c29f32014-06-20 12:00:00 -07002203
2204 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2205 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2206 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2207 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2208
Adam Langley95c29f32014-06-20 12:00:00 -07002209 switch (ret)
2210 {
2211 case SSL_TLSEXT_ERR_ALERT_FATAL:
2212 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2213 return -1;
2214
2215 case SSL_TLSEXT_ERR_ALERT_WARNING:
2216 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002217 return 1;
2218
Adam Langley95c29f32014-06-20 12:00:00 -07002219 case SSL_TLSEXT_ERR_NOACK:
Adam Langleyed8270a2014-09-02 13:52:56 -07002220 s->should_ack_sni = 0;
2221 return 1;
2222
2223 default:
2224 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002225 }
2226 }
2227
David Benjamin6c7aed02014-08-27 16:42:38 -04002228static int ssl_check_serverhello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002229 {
2230 int ret=SSL_TLSEXT_ERR_NOACK;
2231 int al = SSL_AD_UNRECOGNIZED_NAME;
2232
Adam Langley95c29f32014-06-20 12:00:00 -07002233 /* If we are client and using an elliptic curve cryptography cipher
2234 * suite, then if server returns an EC point formats lists extension
2235 * it must contain uncompressed.
2236 */
2237 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2238 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamina9ca90a2014-09-26 18:53:43 -04002239 if (((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) &&
2240 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed))
Adam Langley95c29f32014-06-20 12:00:00 -07002241 {
David Benjamina9ca90a2014-09-26 18:53:43 -04002242 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2243 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002244 }
2245 ret = SSL_TLSEXT_ERR_OK;
Adam Langley95c29f32014-06-20 12:00:00 -07002246
2247 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2248 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2249 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2250 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2251
Adam Langley95c29f32014-06-20 12:00:00 -07002252 switch (ret)
2253 {
2254 case SSL_TLSEXT_ERR_ALERT_FATAL:
Adam Langleyed8270a2014-09-02 13:52:56 -07002255 ssl3_send_alert(s,SSL3_AL_FATAL,al);
Adam Langley95c29f32014-06-20 12:00:00 -07002256 return -1;
2257
2258 case SSL_TLSEXT_ERR_ALERT_WARNING:
2259 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002260 return 1;
2261
2262 default:
2263 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002264 }
2265 }
2266
David Benjamin03973092014-06-24 23:27:17 -04002267int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07002268 {
David Benjamin03973092014-06-24 23:27:17 -04002269 int alert = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002270 if (s->version < SSL3_VERSION)
2271 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002272
2273 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002274 {
David Benjamin03973092014-06-24 23:27:17 -04002275 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07002276 return 0;
2277 }
2278
David Benjamin03973092014-06-24 23:27:17 -04002279 if (ssl_check_serverhello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002280 {
David Benjamin9d28c752014-07-05 00:43:48 -04002281 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, SSL_R_SERVERHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07002282 return 0;
2283 }
David Benjamin03973092014-06-24 23:27:17 -04002284
Adam Langley95c29f32014-06-20 12:00:00 -07002285 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002286 }
Adam Langley95c29f32014-06-20 12:00:00 -07002287
2288/* Since the server cache lookup is done early on in the processing of the
2289 * ClientHello, and other operations depend on the result, we need to handle
2290 * any TLS session ticket extension at the same time.
2291 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002292 * ctx: contains the early callback context, which is the result of a
2293 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002294 * ret: (output) on return, if a ticket was decrypted, then this is set to
2295 * point to the resulting session.
2296 *
2297 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2298 * ciphersuite, in which case we have no use for session tickets and one will
2299 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2300 *
2301 * Returns:
2302 * -1: fatal error, either from parsing or decrypting the ticket.
2303 * 0: no ticket was found (or was ignored, based on settings).
2304 * 1: a zero length extension was found, indicating that the client supports
2305 * session tickets but doesn't currently have one to offer.
2306 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2307 * couldn't be decrypted because of a non-fatal error.
2308 * 3: a ticket was successfully decrypted and *ret was set.
2309 *
2310 * Side effects:
2311 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2312 * a new session ticket to the client because the client indicated support
2313 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2314 * a session ticket or we couldn't use the one it gave us, or if
2315 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2316 * Otherwise, s->tlsext_ticket_expected is set to 0.
2317 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002318int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
2319 SSL_SESSION **ret)
Adam Langley95c29f32014-06-20 12:00:00 -07002320 {
Adam Langley95c29f32014-06-20 12:00:00 -07002321 *ret = NULL;
2322 s->tlsext_ticket_expected = 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002323 const unsigned char *data;
2324 size_t len;
2325 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002326
2327 /* If tickets disabled behave as if no ticket present
2328 * to permit stateful resumption.
2329 */
2330 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2331 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002332 if ((s->version <= SSL3_VERSION) && !ctx->extensions)
Adam Langley95c29f32014-06-20 12:00:00 -07002333 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002334 if (!SSL_early_callback_ctx_extension_get(
2335 ctx, TLSEXT_TYPE_session_ticket, &data, &len))
Adam Langley95c29f32014-06-20 12:00:00 -07002336 {
Adam Langleydc9b1412014-06-20 12:00:00 -07002337 return 0;
2338 }
2339 if (len == 0)
2340 {
2341 /* The client will accept a ticket but doesn't
2342 * currently have one. */
2343 s->tlsext_ticket_expected = 1;
2344 return 1;
2345 }
2346 if (s->tls_session_secret_cb)
2347 {
2348 /* Indicate that the ticket couldn't be
2349 * decrypted rather than generating the session
2350 * from ticket now, trigger abbreviated
2351 * handshake based on external mechanism to
2352 * calculate the master secret later. */
2353 return 2;
2354 }
2355 r = tls_decrypt_ticket(s, data, len, ctx->session_id,
2356 ctx->session_id_len, ret);
2357 switch (r)
2358 {
2359 case 2: /* ticket couldn't be decrypted */
2360 s->tlsext_ticket_expected = 1;
2361 return 2;
2362 case 3: /* ticket was decrypted */
2363 return r;
2364 case 4: /* ticket decrypted but need to renew */
2365 s->tlsext_ticket_expected = 1;
2366 return 3;
2367 default: /* fatal error */
Adam Langley95c29f32014-06-20 12:00:00 -07002368 return -1;
2369 }
Adam Langley95c29f32014-06-20 12:00:00 -07002370 }
2371
2372/* tls_decrypt_ticket attempts to decrypt a session ticket.
2373 *
2374 * etick: points to the body of the session ticket extension.
2375 * eticklen: the length of the session tickets extenion.
2376 * sess_id: points at the session ID.
2377 * sesslen: the length of the session ID.
2378 * psess: (output) on return, if a ticket was decrypted, then this is set to
2379 * point to the resulting session.
2380 *
2381 * Returns:
2382 * -1: fatal error, either from parsing or decrypting the ticket.
2383 * 2: the ticket couldn't be decrypted.
2384 * 3: a ticket was successfully decrypted and *psess was set.
2385 * 4: same as 3, but the ticket needs to be renewed.
2386 */
2387static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
2388 const unsigned char *sess_id, int sesslen,
2389 SSL_SESSION **psess)
2390 {
2391 SSL_SESSION *sess;
2392 unsigned char *sdec;
2393 const unsigned char *p;
2394 int slen, mlen, renew_ticket = 0;
2395 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2396 HMAC_CTX hctx;
2397 EVP_CIPHER_CTX ctx;
2398 SSL_CTX *tctx = s->initial_ctx;
2399 /* Need at least keyname + iv + some encrypted data */
2400 if (eticklen < 48)
2401 return 2;
2402 /* Initialize session ticket encryption and HMAC contexts */
2403 HMAC_CTX_init(&hctx);
2404 EVP_CIPHER_CTX_init(&ctx);
2405 if (tctx->tlsext_ticket_key_cb)
2406 {
2407 unsigned char *nctick = (unsigned char *)etick;
2408 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
2409 &ctx, &hctx, 0);
2410 if (rv < 0)
2411 return -1;
2412 if (rv == 0)
2413 return 2;
2414 if (rv == 2)
2415 renew_ticket = 1;
2416 }
2417 else
2418 {
2419 /* Check key name matches */
2420 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
2421 return 2;
2422 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2423 tlsext_tick_md(), NULL);
2424 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2425 tctx->tlsext_tick_aes_key, etick + 16);
2426 }
2427 /* Attempt to process session ticket, first conduct sanity and
2428 * integrity checks on ticket.
2429 */
2430 mlen = HMAC_size(&hctx);
2431 if (mlen < 0)
2432 {
2433 EVP_CIPHER_CTX_cleanup(&ctx);
2434 return -1;
2435 }
2436 eticklen -= mlen;
2437 /* Check HMAC of encrypted ticket */
2438 HMAC_Update(&hctx, etick, eticklen);
2439 HMAC_Final(&hctx, tick_hmac, NULL);
2440 HMAC_CTX_cleanup(&hctx);
2441 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
Adam Langley38311732014-10-16 19:04:35 -07002442 {
2443 EVP_CIPHER_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002444 return 2;
Adam Langley38311732014-10-16 19:04:35 -07002445 }
Adam Langley95c29f32014-06-20 12:00:00 -07002446 /* Attempt to decrypt session data */
2447 /* Move p after IV to start of encrypted ticket, update length */
2448 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2449 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2450 sdec = OPENSSL_malloc(eticklen);
2451 if (!sdec)
2452 {
2453 EVP_CIPHER_CTX_cleanup(&ctx);
2454 return -1;
2455 }
2456 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2457 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0)
Adam Langley3e148852014-07-24 17:34:02 -07002458 {
2459 EVP_CIPHER_CTX_cleanup(&ctx);
2460 OPENSSL_free(sdec);
Adam Langley95c29f32014-06-20 12:00:00 -07002461 return 2;
Adam Langley3e148852014-07-24 17:34:02 -07002462 }
Adam Langley95c29f32014-06-20 12:00:00 -07002463 slen += mlen;
2464 EVP_CIPHER_CTX_cleanup(&ctx);
2465 p = sdec;
2466
2467 sess = d2i_SSL_SESSION(NULL, &p, slen);
2468 OPENSSL_free(sdec);
2469 if (sess)
2470 {
2471 /* The session ID, if non-empty, is used by some clients to
2472 * detect that the ticket has been accepted. So we copy it to
2473 * the session structure. If it is empty set length to zero
2474 * as required by standard.
2475 */
2476 if (sesslen)
2477 memcpy(sess->session_id, sess_id, sesslen);
2478 sess->session_id_length = sesslen;
2479 *psess = sess;
2480 if (renew_ticket)
2481 return 4;
2482 else
2483 return 3;
2484 }
2485 ERR_clear_error();
2486 /* For session parse failure, indicate that we need to send a new
2487 * ticket. */
2488 return 2;
2489 }
2490
2491/* Tables to translate from NIDs to TLS v1.2 ids */
2492
2493typedef struct
2494 {
2495 int nid;
2496 int id;
2497 } tls12_lookup;
2498
David Benjamincff64722014-08-19 19:54:46 -04002499static const tls12_lookup tls12_md[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002500 {NID_md5, TLSEXT_hash_md5},
2501 {NID_sha1, TLSEXT_hash_sha1},
2502 {NID_sha224, TLSEXT_hash_sha224},
2503 {NID_sha256, TLSEXT_hash_sha256},
2504 {NID_sha384, TLSEXT_hash_sha384},
2505 {NID_sha512, TLSEXT_hash_sha512}
2506};
2507
David Benjamincff64722014-08-19 19:54:46 -04002508static const tls12_lookup tls12_sig[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002509 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
Adam Langley95c29f32014-06-20 12:00:00 -07002510 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
2511};
2512
David Benjamincff64722014-08-19 19:54:46 -04002513static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002514 {
2515 size_t i;
2516 for (i = 0; i < tlen; i++)
2517 {
2518 if (table[i].nid == nid)
2519 return table[i].id;
2520 }
2521 return -1;
2522 }
2523
David Benjamincff64722014-08-19 19:54:46 -04002524static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002525 {
2526 size_t i;
2527 for (i = 0; i < tlen; i++)
2528 {
2529 if ((table[i].id) == id)
2530 return table[i].nid;
2531 }
2532 return NID_undef;
2533 }
2534
2535int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
2536 {
2537 int sig_id, md_id;
2538 if (!md)
2539 return 0;
2540 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2541 sizeof(tls12_md)/sizeof(tls12_lookup));
2542 if (md_id == -1)
2543 return 0;
2544 sig_id = tls12_get_sigid(pk);
2545 if (sig_id == -1)
2546 return 0;
2547 p[0] = (unsigned char)md_id;
2548 p[1] = (unsigned char)sig_id;
2549 return 1;
2550 }
2551
2552int tls12_get_sigid(const EVP_PKEY *pk)
2553 {
2554 return tls12_find_id(pk->type, tls12_sig,
2555 sizeof(tls12_sig)/sizeof(tls12_lookup));
2556 }
2557
2558const EVP_MD *tls12_get_hash(unsigned char hash_alg)
2559 {
2560 switch(hash_alg)
2561 {
Adam Langley95c29f32014-06-20 12:00:00 -07002562 case TLSEXT_hash_md5:
Adam Langley95c29f32014-06-20 12:00:00 -07002563 return EVP_md5();
Adam Langley95c29f32014-06-20 12:00:00 -07002564 case TLSEXT_hash_sha1:
2565 return EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002566 case TLSEXT_hash_sha224:
2567 return EVP_sha224();
2568
2569 case TLSEXT_hash_sha256:
2570 return EVP_sha256();
Adam Langley95c29f32014-06-20 12:00:00 -07002571 case TLSEXT_hash_sha384:
2572 return EVP_sha384();
2573
2574 case TLSEXT_hash_sha512:
2575 return EVP_sha512();
Adam Langley95c29f32014-06-20 12:00:00 -07002576 default:
2577 return NULL;
2578
2579 }
2580 }
2581
2582static int tls12_get_pkey_idx(unsigned char sig_alg)
2583 {
2584 switch(sig_alg)
2585 {
Adam Langley95c29f32014-06-20 12:00:00 -07002586 case TLSEXT_signature_rsa:
2587 return SSL_PKEY_RSA_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002588 case TLSEXT_signature_ecdsa:
2589 return SSL_PKEY_ECC;
Adam Langley95c29f32014-06-20 12:00:00 -07002590 }
2591 return -1;
2592 }
2593
2594/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2595static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
2596 int *psignhash_nid, const unsigned char *data)
2597 {
2598 int sign_nid = 0, hash_nid = 0;
2599 if (!phash_nid && !psign_nid && !psignhash_nid)
2600 return;
2601 if (phash_nid || psignhash_nid)
2602 {
2603 hash_nid = tls12_find_nid(data[0], tls12_md,
2604 sizeof(tls12_md)/sizeof(tls12_lookup));
2605 if (phash_nid)
2606 *phash_nid = hash_nid;
2607 }
2608 if (psign_nid || psignhash_nid)
2609 {
2610 sign_nid = tls12_find_nid(data[1], tls12_sig,
2611 sizeof(tls12_sig)/sizeof(tls12_lookup));
2612 if (psign_nid)
2613 *psign_nid = sign_nid;
2614 }
2615 if (psignhash_nid)
2616 {
2617 if (sign_nid && hash_nid)
2618 OBJ_find_sigid_by_algs(psignhash_nid,
2619 hash_nid, sign_nid);
2620 else
2621 *psignhash_nid = NID_undef;
2622 }
2623 }
2624/* Given preference and allowed sigalgs set shared sigalgs */
2625static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
2626 const unsigned char *pref, size_t preflen,
2627 const unsigned char *allow, size_t allowlen)
2628 {
2629 const unsigned char *ptmp, *atmp;
2630 size_t i, j, nmatch = 0;
2631 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
2632 {
2633 /* Skip disabled hashes or signature algorithms */
2634 if (tls12_get_hash(ptmp[0]) == NULL)
2635 continue;
2636 if (tls12_get_pkey_idx(ptmp[1]) == -1)
2637 continue;
2638 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
2639 {
2640 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
2641 {
2642 nmatch++;
2643 if (shsig)
2644 {
2645 shsig->rhash = ptmp[0];
2646 shsig->rsign = ptmp[1];
2647 tls1_lookup_sigalg(&shsig->hash_nid,
2648 &shsig->sign_nid,
2649 &shsig->signandhash_nid,
2650 ptmp);
2651 shsig++;
2652 }
2653 break;
2654 }
2655 }
2656 }
2657 return nmatch;
2658 }
2659
2660/* Set shared signature algorithms for SSL structures */
2661static int tls1_set_shared_sigalgs(SSL *s)
2662 {
2663 const unsigned char *pref, *allow, *conf;
2664 size_t preflen, allowlen, conflen;
2665 size_t nmatch;
2666 TLS_SIGALGS *salgs = NULL;
2667 CERT *c = s->cert;
Adam Langleydb4f9522014-06-20 12:00:00 -07002668 if (c->shared_sigalgs)
2669 {
2670 OPENSSL_free(c->shared_sigalgs);
2671 c->shared_sigalgs = NULL;
2672 }
Adam Langley95c29f32014-06-20 12:00:00 -07002673 /* If client use client signature algorithms if not NULL */
David Benjamin335d10d2014-08-06 19:56:33 -04002674 if (!s->server && c->client_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002675 {
2676 conf = c->client_sigalgs;
2677 conflen = c->client_sigalgslen;
2678 }
David Benjamin335d10d2014-08-06 19:56:33 -04002679 else if (c->conf_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002680 {
2681 conf = c->conf_sigalgs;
2682 conflen = c->conf_sigalgslen;
2683 }
2684 else
2685 conflen = tls12_get_psigalgs(s, &conf);
David Benjamin335d10d2014-08-06 19:56:33 -04002686 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
Adam Langley95c29f32014-06-20 12:00:00 -07002687 {
2688 pref = conf;
2689 preflen = conflen;
2690 allow = c->peer_sigalgs;
2691 allowlen = c->peer_sigalgslen;
2692 }
2693 else
2694 {
2695 allow = conf;
2696 allowlen = conflen;
2697 pref = c->peer_sigalgs;
2698 preflen = c->peer_sigalgslen;
2699 }
2700 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2701 if (!nmatch)
2702 return 1;
2703 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2704 if (!salgs)
2705 return 0;
2706 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2707 c->shared_sigalgs = salgs;
2708 c->shared_sigalgslen = nmatch;
2709 return 1;
2710 }
2711
2712
2713/* Set preferred digest for each key type */
2714
David Benjamincd996942014-07-20 16:23:51 -04002715int tls1_process_sigalgs(SSL *s, const CBS *sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002716 {
2717 int idx;
2718 size_t i;
2719 const EVP_MD *md;
2720 CERT *c = s->cert;
2721 TLS_SIGALGS *sigptr;
David Benjamincd996942014-07-20 16:23:51 -04002722
Adam Langley95c29f32014-06-20 12:00:00 -07002723 /* Extension ignored for inappropriate versions */
2724 if (!SSL_USE_SIGALGS(s))
2725 return 1;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002726 /* Length must be even */
David Benjamincd996942014-07-20 16:23:51 -04002727 if (CBS_len(sigalgs) % 2 != 0)
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002728 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002729 /* Should never happen */
2730 if (!c)
2731 return 0;
2732
David Benjamincd996942014-07-20 16:23:51 -04002733 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen))
Adam Langley95c29f32014-06-20 12:00:00 -07002734 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002735
2736 tls1_set_shared_sigalgs(s);
2737
Adam Langley95c29f32014-06-20 12:00:00 -07002738 for (i = 0, sigptr = c->shared_sigalgs;
2739 i < c->shared_sigalgslen; i++, sigptr++)
2740 {
2741 idx = tls12_get_pkey_idx(sigptr->rsign);
2742 if (idx > 0 && c->pkeys[idx].digest == NULL)
2743 {
2744 md = tls12_get_hash(sigptr->rhash);
2745 c->pkeys[idx].digest = md;
2746 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2747 if (idx == SSL_PKEY_RSA_SIGN)
2748 {
2749 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2750 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2751 }
2752 }
2753
2754 }
2755 /* In strict mode leave unset digests as NULL to indicate we can't
2756 * use the certificate for signing.
2757 */
2758 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
2759 {
2760 /* Set any remaining keys to default values. NOTE: if alg is
2761 * not supported it stays as NULL.
2762 */
Adam Langley95c29f32014-06-20 12:00:00 -07002763 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
2764 {
2765 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
2766 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
2767 }
Adam Langley95c29f32014-06-20 12:00:00 -07002768 if (!c->pkeys[SSL_PKEY_ECC].digest)
2769 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002770 }
2771 return 1;
2772 }
2773
2774
2775int SSL_get_sigalgs(SSL *s, int idx,
2776 int *psign, int *phash, int *psignhash,
2777 unsigned char *rsig, unsigned char *rhash)
2778 {
2779 const unsigned char *psig = s->cert->peer_sigalgs;
2780 if (psig == NULL)
2781 return 0;
2782 if (idx >= 0)
2783 {
2784 idx <<= 1;
2785 if (idx >= (int)s->cert->peer_sigalgslen)
2786 return 0;
2787 psig += idx;
2788 if (rhash)
2789 *rhash = psig[0];
2790 if (rsig)
2791 *rsig = psig[1];
2792 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2793 }
2794 return s->cert->peer_sigalgslen / 2;
2795 }
2796
2797int SSL_get_shared_sigalgs(SSL *s, int idx,
2798 int *psign, int *phash, int *psignhash,
2799 unsigned char *rsig, unsigned char *rhash)
2800 {
2801 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
2802 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
2803 return 0;
2804 shsigalgs += idx;
2805 if (phash)
2806 *phash = shsigalgs->hash_nid;
2807 if (psign)
2808 *psign = shsigalgs->sign_nid;
2809 if (psignhash)
2810 *psignhash = shsigalgs->signandhash_nid;
2811 if (rsig)
2812 *rsig = shsigalgs->rsign;
2813 if (rhash)
2814 *rhash = shsigalgs->rhash;
2815 return s->cert->shared_sigalgslen;
2816 }
2817
Adam Langley1258b6a2014-06-20 12:00:00 -07002818/* tls1_channel_id_hash calculates the signed data for a Channel ID on the given
2819 * SSL connection and writes it to |md|. */
2820int
2821tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s)
2822 {
2823 EVP_MD_CTX ctx;
2824 unsigned char temp_digest[EVP_MAX_MD_SIZE];
2825 unsigned temp_digest_len;
2826 int i;
2827 static const char kClientIDMagic[] = "TLS Channel ID signature";
2828
2829 if (s->s3->handshake_buffer)
Adam Langley75712922014-10-10 16:23:43 -07002830 if (!ssl3_digest_cached_records(s, free_handshake_buffer))
Adam Langley1258b6a2014-06-20 12:00:00 -07002831 return 0;
2832
2833 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2834
2835 if (s->hit && s->s3->tlsext_channel_id_new)
2836 {
2837 static const char kResumptionMagic[] = "Resumption";
2838 EVP_DigestUpdate(md, kResumptionMagic,
2839 sizeof(kResumptionMagic));
2840 if (s->session->original_handshake_hash_len == 0)
2841 return 0;
2842 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2843 s->session->original_handshake_hash_len);
2844 }
2845
2846 EVP_MD_CTX_init(&ctx);
2847 for (i = 0; i < SSL_MAX_DIGEST; i++)
2848 {
2849 if (s->s3->handshake_dgst[i] == NULL)
2850 continue;
2851 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
2852 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2853 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2854 }
2855 EVP_MD_CTX_cleanup(&ctx);
2856
2857 return 1;
2858 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002859
2860/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2861 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
2862int tls1_record_handshake_hashes_for_channel_id(SSL *s)
2863 {
2864 int digest_len;
2865 /* This function should never be called for a resumed session because
2866 * the handshake hashes that we wish to record are for the original,
2867 * full handshake. */
2868 if (s->hit)
2869 return -1;
2870 /* It only makes sense to call this function if Channel IDs have been
2871 * negotiated. */
2872 if (!s->s3->tlsext_channel_id_new)
2873 return -1;
2874
2875 digest_len = tls1_handshake_digest(
2876 s, s->session->original_handshake_hash,
2877 sizeof(s->session->original_handshake_hash));
2878 if (digest_len < 0)
2879 return -1;
2880
2881 s->session->original_handshake_hash_len = digest_len;
2882
2883 return 1;
2884 }
2885
Adam Langley95c29f32014-06-20 12:00:00 -07002886int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
2887 {
2888 unsigned char *sigalgs, *sptr;
2889 int rhash, rsign;
2890 size_t i;
2891 if (salglen & 1)
2892 return 0;
2893 sigalgs = OPENSSL_malloc(salglen);
2894 if (sigalgs == NULL)
2895 return 0;
2896 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
2897 {
2898 rhash = tls12_find_id(*psig_nids++, tls12_md,
2899 sizeof(tls12_md)/sizeof(tls12_lookup));
2900 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2901 sizeof(tls12_sig)/sizeof(tls12_lookup));
2902
2903 if (rhash == -1 || rsign == -1)
2904 goto err;
2905 *sptr++ = rhash;
2906 *sptr++ = rsign;
2907 }
2908
2909 if (client)
2910 {
2911 if (c->client_sigalgs)
2912 OPENSSL_free(c->client_sigalgs);
2913 c->client_sigalgs = sigalgs;
2914 c->client_sigalgslen = salglen;
2915 }
2916 else
2917 {
2918 if (c->conf_sigalgs)
2919 OPENSSL_free(c->conf_sigalgs);
2920 c->conf_sigalgs = sigalgs;
2921 c->conf_sigalgslen = salglen;
2922 }
2923
2924 return 1;
2925
2926 err:
2927 OPENSSL_free(sigalgs);
2928 return 0;
2929 }
2930
2931static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
2932 {
2933 int sig_nid;
2934 size_t i;
2935 if (default_nid == -1)
2936 return 1;
2937 sig_nid = X509_get_signature_nid(x);
2938 if (default_nid)
2939 return sig_nid == default_nid ? 1 : 0;
2940 for (i = 0; i < c->shared_sigalgslen; i++)
2941 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
2942 return 1;
2943 return 0;
2944 }
2945/* Check to see if a certificate issuer name matches list of CA names */
2946static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
2947 {
2948 X509_NAME *nm;
David Benjaminfb3ff2c2014-09-30 21:00:38 -04002949 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002950 nm = X509_get_issuer_name(x);
2951 for (i = 0; i < sk_X509_NAME_num(names); i++)
2952 {
2953 if(!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
2954 return 1;
2955 }
2956 return 0;
2957 }
2958
2959/* Check certificate chain is consistent with TLS extensions and is
2960 * usable by server. This servers two purposes: it allows users to
2961 * check chains before passing them to the server and it allows the
2962 * server to check chains before attempting to use them.
2963 */
2964
2965/* Flags which need to be set for a certificate when stict mode not set */
2966
2967#define CERT_PKEY_VALID_FLAGS \
2968 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
2969/* Strict mode flags */
2970#define CERT_PKEY_STRICT_FLAGS \
2971 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
2972 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
2973
2974int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
2975 int idx)
2976 {
David Benjaminfb3ff2c2014-09-30 21:00:38 -04002977 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002978 int rv = 0;
2979 int check_flags = 0, strict_mode;
2980 CERT_PKEY *cpk = NULL;
2981 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002982 /* idx == -1 means checking server chains */
2983 if (idx != -1)
2984 {
2985 /* idx == -2 means checking client certificate chains */
2986 if (idx == -2)
2987 {
2988 cpk = c->key;
2989 idx = cpk - c->pkeys;
2990 }
2991 else
2992 cpk = c->pkeys + idx;
2993 x = cpk->x509;
2994 pk = cpk->privatekey;
2995 chain = cpk->chain;
2996 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
2997 /* If no cert or key, forget it */
2998 if (!x || !pk)
2999 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -07003000 }
3001 else
3002 {
3003 if (!x || !pk)
3004 goto end;
3005 idx = ssl_cert_type(x, pk);
3006 if (idx == -1)
3007 goto end;
3008 cpk = c->pkeys + idx;
3009 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
3010 check_flags = CERT_PKEY_STRICT_FLAGS;
3011 else
3012 check_flags = CERT_PKEY_VALID_FLAGS;
3013 strict_mode = 1;
3014 }
3015
Adam Langley95c29f32014-06-20 12:00:00 -07003016 /* Check all signature algorithms are consistent with
3017 * signature algorithms extension if TLS 1.2 or later
3018 * and strict mode.
3019 */
3020 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode)
3021 {
3022 int default_nid;
3023 unsigned char rsign = 0;
3024 if (c->peer_sigalgs)
3025 default_nid = 0;
3026 /* If no sigalgs extension use defaults from RFC5246 */
3027 else
3028 {
3029 switch(idx)
3030 {
3031 case SSL_PKEY_RSA_ENC:
3032 case SSL_PKEY_RSA_SIGN:
Adam Langley95c29f32014-06-20 12:00:00 -07003033 rsign = TLSEXT_signature_rsa;
3034 default_nid = NID_sha1WithRSAEncryption;
3035 break;
3036
Adam Langley95c29f32014-06-20 12:00:00 -07003037 case SSL_PKEY_ECC:
3038 rsign = TLSEXT_signature_ecdsa;
3039 default_nid = NID_ecdsa_with_SHA1;
3040 break;
3041
3042 default:
3043 default_nid = -1;
3044 break;
3045 }
3046 }
3047 /* If peer sent no signature algorithms extension and we
3048 * have set preferred signature algorithms check we support
3049 * sha1.
3050 */
3051 if (default_nid > 0 && c->conf_sigalgs)
3052 {
3053 size_t j;
3054 const unsigned char *p = c->conf_sigalgs;
3055 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2)
3056 {
3057 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3058 break;
3059 }
3060 if (j == c->conf_sigalgslen)
3061 {
3062 if (check_flags)
3063 goto skip_sigs;
3064 else
3065 goto end;
3066 }
3067 }
3068 /* Check signature algorithm of each cert in chain */
3069 if (!tls1_check_sig_alg(c, x, default_nid))
3070 {
3071 if (!check_flags) goto end;
3072 }
3073 else
3074 rv |= CERT_PKEY_EE_SIGNATURE;
3075 rv |= CERT_PKEY_CA_SIGNATURE;
3076 for (i = 0; i < sk_X509_num(chain); i++)
3077 {
3078 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i),
3079 default_nid))
3080 {
3081 if (check_flags)
3082 {
3083 rv &= ~CERT_PKEY_CA_SIGNATURE;
3084 break;
3085 }
3086 else
3087 goto end;
3088 }
3089 }
3090 }
3091 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3092 else if(check_flags)
3093 rv |= CERT_PKEY_EE_SIGNATURE|CERT_PKEY_CA_SIGNATURE;
3094 skip_sigs:
3095 /* Check cert parameters are consistent */
3096 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3097 rv |= CERT_PKEY_EE_PARAM;
3098 else if (!check_flags)
3099 goto end;
3100 if (!s->server)
3101 rv |= CERT_PKEY_CA_PARAM;
3102 /* In strict mode check rest of chain too */
3103 else if (strict_mode)
3104 {
3105 rv |= CERT_PKEY_CA_PARAM;
3106 for (i = 0; i < sk_X509_num(chain); i++)
3107 {
3108 X509 *ca = sk_X509_value(chain, i);
3109 if (!tls1_check_cert_param(s, ca, 0))
3110 {
3111 if (check_flags)
3112 {
3113 rv &= ~CERT_PKEY_CA_PARAM;
3114 break;
3115 }
3116 else
3117 goto end;
3118 }
3119 }
3120 }
3121 if (!s->server && strict_mode)
3122 {
3123 STACK_OF(X509_NAME) *ca_dn;
David Benjamin676d1e72014-07-08 14:34:10 -04003124 uint8_t check_type = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07003125 switch (pk->type)
3126 {
3127 case EVP_PKEY_RSA:
3128 check_type = TLS_CT_RSA_SIGN;
3129 break;
Adam Langley95c29f32014-06-20 12:00:00 -07003130 case EVP_PKEY_EC:
3131 check_type = TLS_CT_ECDSA_SIGN;
3132 break;
Adam Langley95c29f32014-06-20 12:00:00 -07003133 }
3134 if (check_type)
3135 {
David Benjamin676d1e72014-07-08 14:34:10 -04003136 if (s->s3->tmp.certificate_types &&
3137 memchr(s->s3->tmp.certificate_types, check_type, s->s3->tmp.num_certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07003138 {
Adam Langley95c29f32014-06-20 12:00:00 -07003139 rv |= CERT_PKEY_CERT_TYPE;
Adam Langley95c29f32014-06-20 12:00:00 -07003140 }
3141 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
3142 goto end;
3143 }
3144 else
3145 rv |= CERT_PKEY_CERT_TYPE;
3146
3147
3148 ca_dn = s->s3->tmp.ca_names;
3149
3150 if (!sk_X509_NAME_num(ca_dn))
3151 rv |= CERT_PKEY_ISSUER_NAME;
3152
3153 if (!(rv & CERT_PKEY_ISSUER_NAME))
3154 {
3155 if (ssl_check_ca_name(ca_dn, x))
3156 rv |= CERT_PKEY_ISSUER_NAME;
3157 }
3158 if (!(rv & CERT_PKEY_ISSUER_NAME))
3159 {
3160 for (i = 0; i < sk_X509_num(chain); i++)
3161 {
3162 X509 *xtmp = sk_X509_value(chain, i);
3163 if (ssl_check_ca_name(ca_dn, xtmp))
3164 {
3165 rv |= CERT_PKEY_ISSUER_NAME;
3166 break;
3167 }
3168 }
3169 }
3170 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
3171 goto end;
3172 }
3173 else
3174 rv |= CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE;
3175
3176 if (!check_flags || (rv & check_flags) == check_flags)
3177 rv |= CERT_PKEY_VALID;
3178
3179 end:
3180
3181 if (TLS1_get_version(s) >= TLS1_2_VERSION)
3182 {
3183 if (cpk->valid_flags & CERT_PKEY_EXPLICIT_SIGN)
3184 rv |= CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_SIGN;
3185 else if (cpk->digest)
3186 rv |= CERT_PKEY_SIGN;
3187 }
3188 else
3189 rv |= CERT_PKEY_SIGN|CERT_PKEY_EXPLICIT_SIGN;
3190
3191 /* When checking a CERT_PKEY structure all flags are irrelevant
3192 * if the chain is invalid.
3193 */
3194 if (!check_flags)
3195 {
3196 if (rv & CERT_PKEY_VALID)
3197 cpk->valid_flags = rv;
3198 else
3199 {
3200 /* Preserve explicit sign flag, clear rest */
3201 cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
3202 return 0;
3203 }
3204 }
3205 return rv;
3206 }
3207
3208/* Set validity of certificates in an SSL structure */
3209void tls1_set_cert_validity(SSL *s)
3210 {
3211 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
3212 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
Adam Langley95c29f32014-06-20 12:00:00 -07003213 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
3214 }
3215/* User level utiity function to check a chain is suitable */
3216int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
3217 {
3218 return tls1_check_chain(s, x, pk, chain, -1);
3219 }
3220