blob: a3d72283615a9b5d8a707d38512d2b1ae1f9bf7d [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 */
David Benjamin6a8d70c2014-11-13 16:02:33 -0500660static int tls1_check_cert_param(SSL *s, X509 *x)
Adam Langley95c29f32014-06-20 12:00:00 -0700661 {
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 }
David Benjamin253b3e72014-11-13 15:53:52 -0500811 /* Allow fallback to SHA-1. */
812 if (i == sent_sigslen && hash != TLSEXT_hash_sha1)
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 }
Adam Langley95c29f32014-06-20 12:00:00 -0700825 return 1;
826 }
827/* Get a mask of disabled algorithms: an algorithm is disabled
828 * if it isn't supported or doesn't appear in supported signature
829 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
830 * session and not global settings.
831 *
832 */
833void ssl_set_client_disabled(SSL *s)
834 {
835 CERT *c = s->cert;
836 const unsigned char *sigalgs;
837 size_t i, sigalgslen;
David Benjaminef2116d2014-08-19 20:21:56 -0400838 int have_rsa = 0, have_ecdsa = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700839 c->mask_a = 0;
840 c->mask_k = 0;
841 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
842 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
843 c->mask_ssl = SSL_TLSV1_2;
844 else
845 c->mask_ssl = 0;
846 /* Now go through all signature algorithms seeing if we support
847 * any for RSA, DSA, ECDSA. Do this for all versions not just
848 * TLS 1.2.
849 */
850 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
851 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
852 {
853 switch(sigalgs[1])
854 {
Adam Langley95c29f32014-06-20 12:00:00 -0700855 case TLSEXT_signature_rsa:
856 have_rsa = 1;
857 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700858 case TLSEXT_signature_ecdsa:
859 have_ecdsa = 1;
860 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700861 }
862 }
David Benjamin0da0e182014-08-19 16:20:28 -0400863 /* Disable auth if we don't include any appropriate signature
864 * algorithms.
Adam Langley95c29f32014-06-20 12:00:00 -0700865 */
866 if (!have_rsa)
867 {
868 c->mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700869 }
Adam Langley95c29f32014-06-20 12:00:00 -0700870 if (!have_ecdsa)
871 {
872 c->mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700873 }
Adam Langley95c29f32014-06-20 12:00:00 -0700874 /* with PSK there must be client callback set */
875 if (!s->psk_client_callback)
876 {
877 c->mask_a |= SSL_aPSK;
878 c->mask_k |= SSL_kPSK;
879 }
Adam Langley95c29f32014-06-20 12:00:00 -0700880 }
881
Adam Langleyb0c235e2014-06-20 12:00:00 -0700882/* header_len is the length of the ClientHello header written so far, used to
883 * compute padding. It does not include the record header. Pass 0 if no padding
884 * is to be done. */
885unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, size_t header_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700886 {
887 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -0700888 unsigned char *ret = buf;
889 unsigned char *orig = buf;
Adam Langley95c29f32014-06-20 12:00:00 -0700890 /* See if we support any ECC ciphersuites */
891 int using_ecc = 0;
892 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
893 {
David Benjaminfb3ff2c2014-09-30 21:00:38 -0400894 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -0700895 unsigned long alg_k, alg_a;
896 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
897
898 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
899 {
David Benjamin6f260012014-08-15 13:49:12 -0400900 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700901
902 alg_k = c->algorithm_mkey;
903 alg_a = c->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -0400904 if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA))
Adam Langley95c29f32014-06-20 12:00:00 -0700905 {
906 using_ecc = 1;
907 break;
908 }
909 }
910 }
Adam Langley95c29f32014-06-20 12:00:00 -0700911
912 /* don't add extensions for SSLv3 unless doing secure renegotiation */
913 if (s->client_version == SSL3_VERSION
914 && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -0700915 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -0700916
917 ret+=2;
918
919 if (ret>=limit) return NULL; /* this really never occurs, but ... */
920
921 if (s->tlsext_hostname != NULL)
922 {
923 /* Add TLS extension servername to the Client Hello message */
924 unsigned long size_str;
925 long lenmax;
926
927 /* check for enough space.
928 4 for the servername type and entension length
929 2 for servernamelist length
930 1 for the hostname type
931 2 for hostname length
932 + hostname length
933 */
934
935 if ((lenmax = limit - ret - 9) < 0
936 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
937 return NULL;
938
939 /* extension type and length */
940 s2n(TLSEXT_TYPE_server_name,ret);
941 s2n(size_str+5,ret);
942
943 /* length of servername list */
944 s2n(size_str+3,ret);
945
946 /* hostname type, length and hostname */
947 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
948 s2n(size_str,ret);
949 memcpy(ret, s->tlsext_hostname, size_str);
950 ret+=size_str;
951 }
952
953 /* Add RI if renegotiating */
954 if (s->renegotiate)
955 {
956 int el;
957
958 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
959 {
960 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
961 return NULL;
962 }
963
Adam Langleyb0c235e2014-06-20 12:00:00 -0700964 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700965
966 s2n(TLSEXT_TYPE_renegotiate,ret);
967 s2n(el,ret);
968
969 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
970 {
971 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
972 return NULL;
973 }
974
975 ret += el;
976 }
977
Adam Langley75712922014-10-10 16:23:43 -0700978 /* Add extended master secret. */
979 if (s->version != SSL3_VERSION)
980 {
981 if (limit - ret - 4 < 0)
982 return NULL;
983 s2n(TLSEXT_TYPE_extended_master_secret,ret);
984 s2n(0,ret);
985 }
986
Adam Langley95c29f32014-06-20 12:00:00 -0700987 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
988 {
989 int ticklen;
990 if (!s->new_session && s->session && s->session->tlsext_tick)
991 ticklen = s->session->tlsext_ticklen;
992 else if (s->session && s->tlsext_session_ticket &&
993 s->tlsext_session_ticket->data)
994 {
David Benjamin072c9532014-07-26 11:44:25 -0400995 s->session->tlsext_tick = BUF_memdup(
996 s->tlsext_session_ticket->data,
997 s->tlsext_session_ticket->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700998 if (!s->session->tlsext_tick)
999 return NULL;
David Benjamin072c9532014-07-26 11:44:25 -04001000 ticklen = s->tlsext_session_ticket->length;
Adam Langley95c29f32014-06-20 12:00:00 -07001001 s->session->tlsext_ticklen = ticklen;
1002 }
1003 else
1004 ticklen = 0;
1005 if (ticklen == 0 && s->tlsext_session_ticket &&
1006 s->tlsext_session_ticket->data == NULL)
1007 goto skip_ext;
1008 /* Check for enough room 2 for extension type, 2 for len
1009 * rest for ticket
1010 */
1011 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1012 s2n(TLSEXT_TYPE_session_ticket,ret);
1013 s2n(ticklen,ret);
1014 if (ticklen)
1015 {
1016 memcpy(ret, s->session->tlsext_tick, ticklen);
1017 ret += ticklen;
1018 }
1019 }
1020 skip_ext:
1021
1022 if (SSL_USE_SIGALGS(s))
1023 {
1024 size_t salglen;
1025 const unsigned char *salg;
1026 salglen = tls12_get_psigalgs(s, &salg);
1027 if ((size_t)(limit - ret) < salglen + 6)
1028 return NULL;
1029 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1030 s2n(salglen + 2, ret);
1031 s2n(salglen, ret);
1032 memcpy(ret, salg, salglen);
1033 ret += salglen;
1034 }
1035
David Benjamin6c7aed02014-08-27 16:42:38 -04001036 if (s->ocsp_stapling_enabled)
Adam Langley95c29f32014-06-20 12:00:00 -07001037 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001038 /* The status_request extension is excessively extensible at
1039 * every layer. On the client, only support requesting OCSP
1040 * responses with an empty responder_id_list and no
1041 * extensions. */
1042 if (limit - ret - 4 - 1 - 2 - 2 < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001043
Adam Langley95c29f32014-06-20 12:00:00 -07001044 s2n(TLSEXT_TYPE_status_request, ret);
David Benjamin6c7aed02014-08-27 16:42:38 -04001045 s2n(1 + 2 + 2, ret);
1046 /* status_type */
Adam Langley95c29f32014-06-20 12:00:00 -07001047 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
David Benjamin6c7aed02014-08-27 16:42:38 -04001048 /* responder_id_list - empty */
1049 s2n(0, ret);
1050 /* request_extensions - empty */
1051 s2n(0, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001052 }
Adam Langley95c29f32014-06-20 12:00:00 -07001053
Adam Langley95c29f32014-06-20 12:00:00 -07001054 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1055 {
1056 /* The client advertises an emtpy extension to indicate its
1057 * support for Next Protocol Negotiation */
1058 if (limit - ret - 4 < 0)
1059 return NULL;
1060 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1061 s2n(0,ret);
1062 }
Adam Langley95c29f32014-06-20 12:00:00 -07001063
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001064 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len)
1065 {
1066 /* The client advertises an empty extension to indicate its support for
1067 * certificate timestamps. */
1068 if (limit - ret - 4 < 0)
1069 return NULL;
1070 s2n(TLSEXT_TYPE_certificate_timestamp,ret);
1071 s2n(0,ret);
1072 }
1073
Adam Langley95c29f32014-06-20 12:00:00 -07001074 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1075 {
1076 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1077 return NULL;
1078 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1079 s2n(2 + s->alpn_client_proto_list_len,ret);
1080 s2n(s->alpn_client_proto_list_len,ret);
1081 memcpy(ret, s->alpn_client_proto_list,
1082 s->alpn_client_proto_list_len);
1083 ret += s->alpn_client_proto_list_len;
1084 }
1085
Adam Langley1258b6a2014-06-20 12:00:00 -07001086 if (s->tlsext_channel_id_enabled)
1087 {
1088 /* The client advertises an emtpy extension to indicate its
1089 * support for Channel ID. */
1090 if (limit - ret - 4 < 0)
1091 return NULL;
1092 if (s->ctx->tlsext_channel_id_enabled_new)
1093 s2n(TLSEXT_TYPE_channel_id_new,ret);
1094 else
1095 s2n(TLSEXT_TYPE_channel_id,ret);
1096 s2n(0,ret);
1097 }
1098
Adam Langley95c29f32014-06-20 12:00:00 -07001099 if(SSL_get_srtp_profiles(s))
1100 {
1101 int el;
1102
1103 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1104
Adam Langleyb0c235e2014-06-20 12:00:00 -07001105 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001106
1107 s2n(TLSEXT_TYPE_use_srtp,ret);
1108 s2n(el,ret);
1109
David Benjamin120a6742014-08-30 14:54:37 -04001110 if(!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001111 {
1112 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1113 return NULL;
1114 }
1115 ret += el;
1116 }
1117
Adam Langleyc3174b72014-06-20 12:00:00 -07001118 if (using_ecc)
1119 {
1120 /* Add TLS extension ECPointFormats to the ClientHello message */
1121 long lenmax;
David Benjamin072334d2014-07-13 16:24:27 -04001122 const uint8_t *formats;
1123 const uint16_t *curves;
1124 size_t formats_len, curves_len, i;
Adam Langleyc3174b72014-06-20 12:00:00 -07001125
David Benjamin072334d2014-07-13 16:24:27 -04001126 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001127
1128 if ((lenmax = limit - ret - 5) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001129 if (formats_len > (size_t)lenmax) return NULL;
1130 if (formats_len > 255)
Adam Langleyc3174b72014-06-20 12:00:00 -07001131 {
1132 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1133 return NULL;
1134 }
1135
1136 s2n(TLSEXT_TYPE_ec_point_formats,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001137 s2n(formats_len + 1,ret);
1138 *(ret++) = (unsigned char)formats_len;
1139 memcpy(ret, formats, formats_len);
1140 ret+=formats_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001141
1142 /* Add TLS extension EllipticCurves to the ClientHello message */
David Benjamin072334d2014-07-13 16:24:27 -04001143 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001144
1145 if ((lenmax = limit - ret - 6) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001146 if ((curves_len * 2) > (size_t)lenmax) return NULL;
1147 if ((curves_len * 2) > 65532)
Adam Langleyc3174b72014-06-20 12:00:00 -07001148 {
1149 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1150 return NULL;
1151 }
1152
1153 s2n(TLSEXT_TYPE_elliptic_curves,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001154 s2n((curves_len * 2) + 2, ret);
Adam Langleyc3174b72014-06-20 12:00:00 -07001155
1156 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1157 * elliptic_curve_list, but the examples use two bytes.
1158 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1159 * resolves this to two bytes.
1160 */
David Benjamin072334d2014-07-13 16:24:27 -04001161 s2n(curves_len * 2, ret);
1162 for (i = 0; i < curves_len; i++)
1163 {
1164 s2n(curves[i], ret);
1165 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001166 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001167
Adam Langley95c29f32014-06-20 12:00:00 -07001168#ifdef TLSEXT_TYPE_padding
1169 /* Add padding to workaround bugs in F5 terminators.
Adam Langleyb0c235e2014-06-20 12:00:00 -07001170 * See https://tools.ietf.org/html/draft-agl-tls-padding-03
Adam Langley95c29f32014-06-20 12:00:00 -07001171 *
1172 * NB: because this code works out the length of all existing
Adam Langleyb0c235e2014-06-20 12:00:00 -07001173 * extensions it MUST always appear last. */
1174 if (header_len > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001175 {
Adam Langleyb0c235e2014-06-20 12:00:00 -07001176 header_len += ret - orig;
1177 if (header_len > 0xff && header_len < 0x200)
1178 {
1179 size_t padding_len = 0x200 - header_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001180 /* Extensions take at least four bytes to encode. Always
1181 * include least one byte of data if including the
1182 * extension. WebSphere Application Server 7.0 is
1183 * intolerant to the last extension being zero-length. */
1184 if (padding_len >= 4 + 1)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001185 padding_len -= 4;
1186 else
Adam Langleyc3174b72014-06-20 12:00:00 -07001187 padding_len = 1;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001188 if (limit - ret - 4 - (long)padding_len < 0)
1189 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001190
Adam Langleyb0c235e2014-06-20 12:00:00 -07001191 s2n(TLSEXT_TYPE_padding, ret);
1192 s2n(padding_len, ret);
1193 memset(ret, 0, padding_len);
1194 ret += padding_len;
1195 }
Adam Langley95c29f32014-06-20 12:00:00 -07001196 }
1197#endif
1198
Adam Langleyb0c235e2014-06-20 12:00:00 -07001199 if ((extdatalen = ret-orig-2)== 0)
1200 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001201
Adam Langleyb0c235e2014-06-20 12:00:00 -07001202 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001203 return ret;
1204 }
1205
Adam Langleyb0c235e2014-06-20 12:00:00 -07001206unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit)
Adam Langley95c29f32014-06-20 12:00:00 -07001207 {
1208 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001209 unsigned char *orig = buf;
1210 unsigned char *ret = buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001211 int next_proto_neg_seen;
Adam Langley95c29f32014-06-20 12:00:00 -07001212 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1213 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -04001214 int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
David Benjamina19fc252014-10-19 00:14:36 -04001215 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001216 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1217 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001218 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001219
1220 ret+=2;
1221 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1222
Adam Langleyed8270a2014-09-02 13:52:56 -07001223 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001224 {
1225 if ((long)(limit - ret - 4) < 0) return NULL;
1226
1227 s2n(TLSEXT_TYPE_server_name,ret);
1228 s2n(0,ret);
1229 }
1230
1231 if(s->s3->send_connection_binding)
1232 {
1233 int el;
1234
1235 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1236 {
1237 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1238 return NULL;
1239 }
1240
Adam Langleyb0c235e2014-06-20 12:00:00 -07001241 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001242
1243 s2n(TLSEXT_TYPE_renegotiate,ret);
1244 s2n(el,ret);
1245
1246 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1247 {
1248 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1249 return NULL;
1250 }
1251
1252 ret += el;
1253 }
1254
Adam Langley75712922014-10-10 16:23:43 -07001255 if (s->s3->tmp.extended_master_secret)
1256 {
1257 if ((long)(limit - ret - 4) < 0) return NULL;
1258
1259 s2n(TLSEXT_TYPE_extended_master_secret,ret);
1260 s2n(0,ret);
1261 }
1262
Adam Langley95c29f32014-06-20 12:00:00 -07001263 if (using_ecc)
1264 {
1265 const unsigned char *plist;
1266 size_t plistlen;
1267 /* Add TLS extension ECPointFormats to the ServerHello message */
1268 long lenmax;
1269
1270 tls1_get_formatlist(s, &plist, &plistlen);
1271
1272 if ((lenmax = limit - ret - 5) < 0) return NULL;
1273 if (plistlen > (size_t)lenmax) return NULL;
1274 if (plistlen > 255)
1275 {
1276 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1277 return NULL;
1278 }
1279
1280 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1281 s2n(plistlen + 1,ret);
1282 *(ret++) = (unsigned char) plistlen;
1283 memcpy(ret, plist, plistlen);
1284 ret+=plistlen;
1285
1286 }
1287 /* Currently the server should not respond with a SupportedCurves extension */
Adam Langley95c29f32014-06-20 12:00:00 -07001288
1289 if (s->tlsext_ticket_expected
1290 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1291 {
1292 if ((long)(limit - ret - 4) < 0) return NULL;
1293 s2n(TLSEXT_TYPE_session_ticket,ret);
1294 s2n(0,ret);
1295 }
1296
David Benjamin6c7aed02014-08-27 16:42:38 -04001297 if (s->s3->tmp.certificate_status_expected)
Adam Langley95c29f32014-06-20 12:00:00 -07001298 {
1299 if ((long)(limit - ret - 4) < 0) return NULL;
1300 s2n(TLSEXT_TYPE_status_request,ret);
1301 s2n(0,ret);
1302 }
1303
Adam Langley95c29f32014-06-20 12:00:00 -07001304 if(s->srtp_profile)
1305 {
1306 int el;
1307
1308 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1309
Adam Langleyb0c235e2014-06-20 12:00:00 -07001310 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001311
1312 s2n(TLSEXT_TYPE_use_srtp,ret);
1313 s2n(el,ret);
1314
David Benjamin120a6742014-08-30 14:54:37 -04001315 if(!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001316 {
1317 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1318 return NULL;
1319 }
1320 ret+=el;
1321 }
1322
Adam Langley95c29f32014-06-20 12:00:00 -07001323 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1324 s->s3->next_proto_neg_seen = 0;
1325 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1326 {
1327 const unsigned char *npa;
1328 unsigned int npalen;
1329 int r;
1330
1331 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1332 if (r == SSL_TLSEXT_ERR_OK)
1333 {
1334 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1335 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1336 s2n(npalen,ret);
1337 memcpy(ret, npa, npalen);
1338 ret += npalen;
1339 s->s3->next_proto_neg_seen = 1;
1340 }
1341 }
Adam Langley95c29f32014-06-20 12:00:00 -07001342
Adam Langley95c29f32014-06-20 12:00:00 -07001343 if (s->s3->alpn_selected)
1344 {
David Benjamin03973092014-06-24 23:27:17 -04001345 const uint8_t *selected = s->s3->alpn_selected;
1346 size_t len = s->s3->alpn_selected_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001347
1348 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1349 return NULL;
1350 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1351 s2n(3 + len,ret);
1352 s2n(1 + len,ret);
1353 *ret++ = len;
1354 memcpy(ret, selected, len);
1355 ret += len;
1356 }
1357
Adam Langley1258b6a2014-06-20 12:00:00 -07001358 /* If the client advertised support for Channel ID, and we have it
1359 * enabled, then we want to echo it back. */
1360 if (s->s3->tlsext_channel_id_valid)
1361 {
1362 if (limit - ret - 4 < 0)
1363 return NULL;
1364 if (s->s3->tlsext_channel_id_new)
1365 s2n(TLSEXT_TYPE_channel_id_new,ret);
1366 else
1367 s2n(TLSEXT_TYPE_channel_id,ret);
1368 s2n(0,ret);
1369 }
1370
Adam Langleyb0c235e2014-06-20 12:00:00 -07001371 if ((extdatalen = ret-orig-2) == 0)
1372 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001373
Adam Langleyb0c235e2014-06-20 12:00:00 -07001374 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001375 return ret;
1376 }
1377
Adam Langley95c29f32014-06-20 12:00:00 -07001378/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1379 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001380 * cbs: the contents of the extension, not including the type and length.
1381 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001382 * return.
1383 *
David Benjamindc72ff72014-06-25 12:36:10 -04001384 * returns: 1 on success. */
1385static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001386 {
Adam Langleyded93582014-07-31 15:23:51 -07001387 CBS protocol_name_list, protocol_name_list_copy;
Adam Langley95c29f32014-06-20 12:00:00 -07001388 const unsigned char *selected;
1389 unsigned char selected_len;
1390 int r;
1391
1392 if (s->ctx->alpn_select_cb == NULL)
David Benjamindc72ff72014-06-25 12:36:10 -04001393 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001394
David Benjamindc72ff72014-06-25 12:36:10 -04001395 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1396 CBS_len(cbs) != 0 ||
1397 CBS_len(&protocol_name_list) < 2)
Adam Langley95c29f32014-06-20 12:00:00 -07001398 goto parse_error;
1399
David Benjamindc72ff72014-06-25 12:36:10 -04001400 /* Validate the protocol list. */
Adam Langleyded93582014-07-31 15:23:51 -07001401 protocol_name_list_copy = protocol_name_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001402 while (CBS_len(&protocol_name_list_copy) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001403 {
David Benjamindc72ff72014-06-25 12:36:10 -04001404 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001405
David Benjamindc72ff72014-06-25 12:36:10 -04001406 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001407 goto parse_error;
Adam Langley95c29f32014-06-20 12:00:00 -07001408 }
1409
David Benjamindc72ff72014-06-25 12:36:10 -04001410 r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1411 CBS_data(&protocol_name_list), CBS_len(&protocol_name_list),
1412 s->ctx->alpn_select_cb_arg);
Adam Langley95c29f32014-06-20 12:00:00 -07001413 if (r == SSL_TLSEXT_ERR_OK) {
1414 if (s->s3->alpn_selected)
1415 OPENSSL_free(s->s3->alpn_selected);
David Benjamin072c9532014-07-26 11:44:25 -04001416 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001417 if (!s->s3->alpn_selected)
1418 {
David Benjamindc72ff72014-06-25 12:36:10 -04001419 *out_alert = SSL_AD_INTERNAL_ERROR;
1420 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001421 }
Adam Langley95c29f32014-06-20 12:00:00 -07001422 s->s3->alpn_selected_len = selected_len;
1423 }
David Benjamindc72ff72014-06-25 12:36:10 -04001424 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001425
1426parse_error:
David Benjamindc72ff72014-06-25 12:36:10 -04001427 *out_alert = SSL_AD_DECODE_ERROR;
1428 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001429 }
1430
David Benjamindc72ff72014-06-25 12:36:10 -04001431static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001432 {
Adam Langley95c29f32014-06-20 12:00:00 -07001433 int renegotiate_seen = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001434 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001435 size_t i;
1436
Adam Langleyed8270a2014-09-02 13:52:56 -07001437 s->should_ack_sni = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001438 s->s3->next_proto_neg_seen = 0;
David Benjamin6c7aed02014-08-27 16:42:38 -04001439 s->s3->tmp.certificate_status_expected = 0;
Adam Langley75712922014-10-10 16:23:43 -07001440 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001441
Adam Langley95c29f32014-06-20 12:00:00 -07001442 if (s->s3->alpn_selected)
1443 {
1444 OPENSSL_free(s->s3->alpn_selected);
1445 s->s3->alpn_selected = NULL;
1446 }
1447
Adam Langley95c29f32014-06-20 12:00:00 -07001448 /* Clear any signature algorithms extension received */
1449 if (s->cert->peer_sigalgs)
1450 {
1451 OPENSSL_free(s->cert->peer_sigalgs);
1452 s->cert->peer_sigalgs = NULL;
1453 }
David Benjamina19fc252014-10-19 00:14:36 -04001454 /* Clear any shared signature algorithms */
Adam Langley95c29f32014-06-20 12:00:00 -07001455 if (s->cert->shared_sigalgs)
1456 {
1457 OPENSSL_free(s->cert->shared_sigalgs);
1458 s->cert->shared_sigalgs = NULL;
1459 }
1460 /* Clear certificate digests and validity flags */
1461 for (i = 0; i < SSL_PKEY_NUM; i++)
1462 {
1463 s->cert->pkeys[i].digest = NULL;
1464 s->cert->pkeys[i].valid_flags = 0;
1465 }
David Benjamina19fc252014-10-19 00:14:36 -04001466 /* Clear ECC extensions */
1467 if (s->s3->tmp.peer_ecpointformatlist != 0)
1468 {
1469 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1470 s->s3->tmp.peer_ecpointformatlist = NULL;
1471 s->s3->tmp.peer_ecpointformatlist_length = 0;
1472 }
1473 if (s->s3->tmp.peer_ellipticcurvelist != 0)
1474 {
1475 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1476 s->s3->tmp.peer_ellipticcurvelist = NULL;
1477 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1478 }
Adam Langley95c29f32014-06-20 12:00:00 -07001479
David Benjamindc72ff72014-06-25 12:36:10 -04001480 /* There may be no extensions. */
1481 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001482 {
David Benjamindc72ff72014-06-25 12:36:10 -04001483 goto ri_check;
1484 }
Adam Langley95c29f32014-06-20 12:00:00 -07001485
David Benjamin35a7a442014-07-05 00:23:20 -04001486 /* Decode the extensions block and check it is valid. */
1487 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1488 !tls1_check_duplicate_extensions(&extensions))
David Benjamindc72ff72014-06-25 12:36:10 -04001489 {
1490 *out_alert = SSL_AD_DECODE_ERROR;
1491 return 0;
1492 }
1493
David Benjamindc72ff72014-06-25 12:36:10 -04001494 while (CBS_len(&extensions) != 0)
1495 {
1496 uint16_t type;
1497 CBS extension;
1498
1499 /* Decode the next extension. */
1500 if (!CBS_get_u16(&extensions, &type) ||
1501 !CBS_get_u16_length_prefixed(&extensions, &extension))
1502 {
1503 *out_alert = SSL_AD_DECODE_ERROR;
1504 return 0;
1505 }
1506
Adam Langley95c29f32014-06-20 12:00:00 -07001507 if (s->tlsext_debug_cb)
David Benjamindc72ff72014-06-25 12:36:10 -04001508 {
1509 s->tlsext_debug_cb(s, 0, type, (unsigned char*)CBS_data(&extension),
1510 CBS_len(&extension), s->tlsext_debug_arg);
1511 }
1512
Adam Langley95c29f32014-06-20 12:00:00 -07001513/* The servername extension is treated as follows:
1514
1515 - Only the hostname type is supported with a maximum length of 255.
1516 - The servername is rejected if too long or if it contains zeros,
1517 in which case an fatal alert is generated.
1518 - The servername field is maintained together with the session cache.
1519 - When a session is resumed, the servername call back invoked in order
1520 to allow the application to position itself to the right context.
1521 - The servername is acknowledged if it is new for a session or when
1522 it is identical to a previously used for the same session.
1523 Applications can control the behaviour. They can at any time
1524 set a 'desirable' servername for a new SSL object. This can be the
1525 case for example with HTTPS when a Host: header field is received and
1526 a renegotiation is requested. In this case, a possible servername
1527 presented in the new client hello is only acknowledged if it matches
1528 the value of the Host: field.
1529 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1530 if they provide for changing an explicit servername context for the session,
1531 i.e. when the session has been established with a servername extension.
1532 - On session reconnect, the servername extension may be absent.
1533
1534*/
1535
1536 if (type == TLSEXT_TYPE_server_name)
1537 {
David Benjamindc72ff72014-06-25 12:36:10 -04001538 CBS server_name_list;
Adam Langleyed8270a2014-09-02 13:52:56 -07001539 char have_seen_host_name = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001540
1541 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1542 CBS_len(&server_name_list) < 1 ||
1543 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001544 {
David Benjamindc72ff72014-06-25 12:36:10 -04001545 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001546 return 0;
1547 }
Adam Langley95c29f32014-06-20 12:00:00 -07001548
David Benjamindc72ff72014-06-25 12:36:10 -04001549 /* Decode each ServerName in the extension. */
1550 while (CBS_len(&server_name_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001551 {
David Benjamindc72ff72014-06-25 12:36:10 -04001552 uint8_t name_type;
1553 CBS host_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001554
David Benjamindc72ff72014-06-25 12:36:10 -04001555 /* Decode the NameType. */
1556 if (!CBS_get_u8(&server_name_list, &name_type))
Adam Langley95c29f32014-06-20 12:00:00 -07001557 {
David Benjamindc72ff72014-06-25 12:36:10 -04001558 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001559 return 0;
1560 }
David Benjamindc72ff72014-06-25 12:36:10 -04001561
David Benjamindc72ff72014-06-25 12:36:10 -04001562 /* Only host_name is supported. */
1563 if (name_type != TLSEXT_NAMETYPE_host_name)
1564 continue;
1565
Adam Langleyed8270a2014-09-02 13:52:56 -07001566 if (have_seen_host_name)
1567 {
1568 /* The ServerNameList MUST NOT contain
1569 * more than one name of the same
1570 * name_type. */
1571 *out_alert = SSL_AD_DECODE_ERROR;
1572 return 0;
1573 }
1574
1575 have_seen_host_name = 1;
1576
1577 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1578 CBS_len(&host_name) < 1)
1579 {
1580 *out_alert = SSL_AD_DECODE_ERROR;
1581 return 0;
1582 }
1583
1584 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1585 CBS_contains_zero_byte(&host_name))
1586 {
1587 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1588 return 0;
1589 }
1590
David Benjamindc72ff72014-06-25 12:36:10 -04001591 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -07001592 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001593 assert(s->session->tlsext_hostname == NULL);
David Benjamindc72ff72014-06-25 12:36:10 -04001594 if (s->session->tlsext_hostname)
Adam Langley95c29f32014-06-20 12:00:00 -07001595 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001596 /* This should be impossible. */
David Benjamindc72ff72014-06-25 12:36:10 -04001597 *out_alert = SSL_AD_DECODE_ERROR;
1598 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001599 }
Adam Langley95c29f32014-06-20 12:00:00 -07001600
David Benjamindc72ff72014-06-25 12:36:10 -04001601 /* Copy the hostname as a string. */
David Benjamined439582014-07-14 19:13:02 -04001602 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname))
David Benjamindc72ff72014-06-25 12:36:10 -04001603 {
1604 *out_alert = SSL_AD_INTERNAL_ERROR;
1605 return 0;
1606 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001607
1608 s->should_ack_sni = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001609 }
Adam Langley95c29f32014-06-20 12:00:00 -07001610 }
Adam Langley95c29f32014-06-20 12:00:00 -07001611 }
1612
Adam Langley95c29f32014-06-20 12:00:00 -07001613 else if (type == TLSEXT_TYPE_ec_point_formats)
1614 {
David Benjamindc72ff72014-06-25 12:36:10 -04001615 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001616
David Benjamindc72ff72014-06-25 12:36:10 -04001617 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1618 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001619 {
David Benjamindc72ff72014-06-25 12:36:10 -04001620 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001621 return 0;
1622 }
David Benjamindc72ff72014-06-25 12:36:10 -04001623
David Benjamina19fc252014-10-19 00:14:36 -04001624 if (!CBS_stow(&ec_point_format_list,
1625 &s->s3->tmp.peer_ecpointformatlist,
1626 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001627 {
David Benjamina19fc252014-10-19 00:14:36 -04001628 *out_alert = SSL_AD_INTERNAL_ERROR;
1629 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001630 }
Adam Langley95c29f32014-06-20 12:00:00 -07001631 }
1632 else if (type == TLSEXT_TYPE_elliptic_curves)
1633 {
David Benjamindc72ff72014-06-25 12:36:10 -04001634 CBS elliptic_curve_list;
David Benjamin072334d2014-07-13 16:24:27 -04001635 size_t i, num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001636
David Benjamindc72ff72014-06-25 12:36:10 -04001637 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
David Benjamin072334d2014-07-13 16:24:27 -04001638 CBS_len(&elliptic_curve_list) == 0 ||
1639 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
David Benjamindc72ff72014-06-25 12:36:10 -04001640 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001641 {
David Benjamindc72ff72014-06-25 12:36:10 -04001642 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001643 return 0;
1644 }
David Benjamindc72ff72014-06-25 12:36:10 -04001645
David Benjamina19fc252014-10-19 00:14:36 -04001646 if (s->s3->tmp.peer_ellipticcurvelist)
Adam Langley95c29f32014-06-20 12:00:00 -07001647 {
David Benjamina19fc252014-10-19 00:14:36 -04001648 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1649 s->s3->tmp.peer_ellipticcurvelist_length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001650 }
David Benjamina19fc252014-10-19 00:14:36 -04001651 s->s3->tmp.peer_ellipticcurvelist =
1652 (uint16_t*)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
1653 if (s->s3->tmp.peer_ellipticcurvelist == NULL)
1654 {
1655 *out_alert = SSL_AD_INTERNAL_ERROR;
1656 return 0;
1657 }
1658 num_curves = CBS_len(&elliptic_curve_list) / 2;
1659 for (i = 0; i < num_curves; i++)
1660 {
1661 if (!CBS_get_u16(&elliptic_curve_list,
1662 &s->s3->tmp.peer_ellipticcurvelist[i]))
1663 {
1664 *out_alert = SSL_AD_INTERNAL_ERROR;
1665 return 0;
1666 }
1667 }
1668 if (CBS_len(&elliptic_curve_list) != 0)
1669 {
1670 *out_alert = SSL_AD_INTERNAL_ERROR;
1671 return 0;
1672 }
1673 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001674 }
Adam Langley95c29f32014-06-20 12:00:00 -07001675 else if (type == TLSEXT_TYPE_session_ticket)
1676 {
1677 if (s->tls_session_ticket_ext_cb &&
David Benjamindc72ff72014-06-25 12:36:10 -04001678 !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 -07001679 {
David Benjamindc72ff72014-06-25 12:36:10 -04001680 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001681 return 0;
1682 }
1683 }
1684 else if (type == TLSEXT_TYPE_renegotiate)
1685 {
David Benjamindc72ff72014-06-25 12:36:10 -04001686 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001687 return 0;
1688 renegotiate_seen = 1;
1689 }
1690 else if (type == TLSEXT_TYPE_signature_algorithms)
1691 {
David Benjamindc72ff72014-06-25 12:36:10 -04001692 CBS supported_signature_algorithms;
1693
David Benjamindc72ff72014-06-25 12:36:10 -04001694 if (!CBS_get_u16_length_prefixed(&extension, &supported_signature_algorithms) ||
1695 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001696 {
David Benjamindc72ff72014-06-25 12:36:10 -04001697 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001698 return 0;
1699 }
David Benjamindc72ff72014-06-25 12:36:10 -04001700
1701 /* Ensure the signature algorithms are non-empty. It
1702 * contains a list of SignatureAndHashAlgorithms
1703 * which are two bytes each. */
1704 if (CBS_len(&supported_signature_algorithms) == 0 ||
1705 (CBS_len(&supported_signature_algorithms) % 2) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001706 {
David Benjamindc72ff72014-06-25 12:36:10 -04001707 *out_alert = SSL_AD_DECODE_ERROR;
1708 return 0;
1709 }
1710
David Benjamincd996942014-07-20 16:23:51 -04001711 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
David Benjamindc72ff72014-06-25 12:36:10 -04001712 {
1713 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001714 return 0;
1715 }
1716 /* If sigalgs received and no shared algorithms fatal
1717 * error.
1718 */
1719 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
1720 {
1721 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
David Benjamindc72ff72014-06-25 12:36:10 -04001722 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07001723 return 0;
1724 }
1725 }
1726
Adam Langley95c29f32014-06-20 12:00:00 -07001727 else if (type == TLSEXT_TYPE_next_proto_neg &&
1728 s->s3->tmp.finish_md_len == 0 &&
1729 s->s3->alpn_selected == NULL)
1730 {
David Benjamindc72ff72014-06-25 12:36:10 -04001731 /* The extension must be empty. */
1732 if (CBS_len(&extension) != 0)
1733 {
1734 *out_alert = SSL_AD_DECODE_ERROR;
1735 return 0;
1736 }
1737
Adam Langley95c29f32014-06-20 12:00:00 -07001738 /* We shouldn't accept this extension on a
1739 * renegotiation.
1740 *
1741 * s->new_session will be set on renegotiation, but we
1742 * probably shouldn't rely that it couldn't be set on
1743 * the initial renegotation too in certain cases (when
1744 * there's some other reason to disallow resuming an
1745 * earlier session -- the current code won't be doing
1746 * anything like that, but this might change).
1747
1748 * A valid sign that there's been a previous handshake
1749 * in this connection is if s->s3->tmp.finish_md_len >
1750 * 0. (We are talking about a check that will happen
1751 * in the Hello protocol round, well before a new
1752 * Finished message could have been computed.) */
1753 s->s3->next_proto_neg_seen = 1;
1754 }
Adam Langley95c29f32014-06-20 12:00:00 -07001755
1756 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1757 s->ctx->alpn_select_cb &&
1758 s->s3->tmp.finish_md_len == 0)
1759 {
David Benjamindc72ff72014-06-25 12:36:10 -04001760 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001761 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001762 /* ALPN takes precedence over NPN. */
1763 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001764 }
1765
Adam Langley1258b6a2014-06-20 12:00:00 -07001766 else if (type == TLSEXT_TYPE_channel_id &&
1767 s->tlsext_channel_id_enabled)
David Benjamindc72ff72014-06-25 12:36:10 -04001768 {
1769 /* The extension must be empty. */
1770 if (CBS_len(&extension) != 0)
1771 {
1772 *out_alert = SSL_AD_DECODE_ERROR;
1773 return 0;
1774 }
1775
Adam Langley1258b6a2014-06-20 12:00:00 -07001776 s->s3->tlsext_channel_id_valid = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001777 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001778
1779 else if (type == TLSEXT_TYPE_channel_id_new &&
1780 s->tlsext_channel_id_enabled)
1781 {
David Benjamindc72ff72014-06-25 12:36:10 -04001782 /* The extension must be empty. */
1783 if (CBS_len(&extension) != 0)
1784 {
1785 *out_alert = SSL_AD_DECODE_ERROR;
1786 return 0;
1787 }
1788
Adam Langley1258b6a2014-06-20 12:00:00 -07001789 s->s3->tlsext_channel_id_valid = 1;
1790 s->s3->tlsext_channel_id_new = 1;
1791 }
1792
1793
Adam Langley95c29f32014-06-20 12:00:00 -07001794 /* session ticket processed earlier */
1795 else if (type == TLSEXT_TYPE_use_srtp)
1796 {
David Benjamindc72ff72014-06-25 12:36:10 -04001797 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001798 return 0;
1799 }
Adam Langley75712922014-10-10 16:23:43 -07001800
1801 else if (type == TLSEXT_TYPE_extended_master_secret &&
1802 s->version != SSL3_VERSION)
1803 {
1804 if (CBS_len(&extension) != 0)
1805 {
1806 *out_alert = SSL_AD_DECODE_ERROR;
1807 return 0;
1808 }
1809
1810 s->s3->tmp.extended_master_secret = 1;
1811 }
Adam Langley95c29f32014-06-20 12:00:00 -07001812 }
1813
Adam Langley95c29f32014-06-20 12:00:00 -07001814 ri_check:
1815
1816 /* Need RI if renegotiating */
1817
1818 if (!renegotiate_seen && s->renegotiate &&
1819 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1820 {
David Benjamindc72ff72014-06-25 12:36:10 -04001821 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin172fc2c2014-09-06 13:09:47 -04001822 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07001823 return 0;
1824 }
1825 /* If no signature algorithms extension set default values */
1826 if (!s->cert->peer_sigalgs)
1827 ssl_cert_set_default_md(s->cert);
1828
1829 return 1;
1830 }
1831
David Benjamindc72ff72014-06-25 12:36:10 -04001832int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001833 {
David Benjamindc72ff72014-06-25 12:36:10 -04001834 int alert = -1;
1835 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001836 {
David Benjamindc72ff72014-06-25 12:36:10 -04001837 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07001838 return 0;
1839 }
1840
David Benjamin6c7aed02014-08-27 16:42:38 -04001841 if (ssl_check_clienthello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001842 {
David Benjamin9d28c752014-07-05 00:43:48 -04001843 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07001844 return 0;
1845 }
1846 return 1;
David Benjamin072334d2014-07-13 16:24:27 -04001847 }
Adam Langley95c29f32014-06-20 12:00:00 -07001848
Adam Langley95c29f32014-06-20 12:00:00 -07001849/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1850 * elements of zero length are allowed and the set of elements must exactly fill
1851 * the length of the block. */
David Benjamin03973092014-06-24 23:27:17 -04001852static char ssl_next_proto_validate(const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001853 {
David Benjamin03973092014-06-24 23:27:17 -04001854 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001855
David Benjamin03973092014-06-24 23:27:17 -04001856 while (CBS_len(&copy) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001857 {
David Benjamin03973092014-06-24 23:27:17 -04001858 CBS proto;
1859 if (!CBS_get_u8_length_prefixed(&copy, &proto) ||
1860 CBS_len(&proto) == 0)
1861 {
Adam Langley95c29f32014-06-20 12:00:00 -07001862 return 0;
David Benjamin03973092014-06-24 23:27:17 -04001863 }
Adam Langley95c29f32014-06-20 12:00:00 -07001864 }
David Benjamin03973092014-06-24 23:27:17 -04001865 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001866 }
Adam Langley95c29f32014-06-20 12:00:00 -07001867
David Benjamin03973092014-06-24 23:27:17 -04001868static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001869 {
Adam Langley95c29f32014-06-20 12:00:00 -07001870 int tlsext_servername = 0;
1871 int renegotiate_seen = 0;
David Benjamin03973092014-06-24 23:27:17 -04001872 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001873
David Benjamin6c7aed02014-08-27 16:42:38 -04001874 /* TODO(davidben): Move all of these to some per-handshake state that
1875 * gets systematically reset on a new handshake; perhaps allocate it
1876 * fresh each time so it's not even kept around post-handshake. */
Adam Langley95c29f32014-06-20 12:00:00 -07001877 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001878
David Benjamin6c7aed02014-08-27 16:42:38 -04001879 s->tlsext_ticket_expected = 0;
1880 s->s3->tmp.certificate_status_expected = 0;
Adam Langley75712922014-10-10 16:23:43 -07001881 s->s3->tmp.extended_master_secret = 0;
David Benjamin64442872014-07-21 17:43:45 -04001882
Adam Langley95c29f32014-06-20 12:00:00 -07001883 if (s->s3->alpn_selected)
1884 {
1885 OPENSSL_free(s->s3->alpn_selected);
1886 s->s3->alpn_selected = NULL;
1887 }
1888
David Benjamina19fc252014-10-19 00:14:36 -04001889 /* Clear ECC extensions */
1890 if (s->s3->tmp.peer_ecpointformatlist != 0)
1891 {
1892 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1893 s->s3->tmp.peer_ecpointformatlist = NULL;
1894 s->s3->tmp.peer_ecpointformatlist_length = 0;
1895 }
1896
David Benjamin03973092014-06-24 23:27:17 -04001897 /* There may be no extensions. */
1898 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001899 {
David Benjamin03973092014-06-24 23:27:17 -04001900 goto ri_check;
1901 }
1902
David Benjamin35a7a442014-07-05 00:23:20 -04001903 /* Decode the extensions block and check it is valid. */
1904 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1905 !tls1_check_duplicate_extensions(&extensions))
David Benjamin03973092014-06-24 23:27:17 -04001906 {
1907 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001908 return 0;
1909 }
1910
David Benjamin03973092014-06-24 23:27:17 -04001911 while (CBS_len(&extensions) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001912 {
David Benjamin03973092014-06-24 23:27:17 -04001913 uint16_t type;
1914 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001915
David Benjamin03973092014-06-24 23:27:17 -04001916 /* Decode the next extension. */
1917 if (!CBS_get_u16(&extensions, &type) ||
1918 !CBS_get_u16_length_prefixed(&extensions, &extension))
1919 {
1920 *out_alert = SSL_AD_DECODE_ERROR;
1921 return 0;
1922 }
Adam Langley95c29f32014-06-20 12:00:00 -07001923
1924 if (s->tlsext_debug_cb)
David Benjamin03973092014-06-24 23:27:17 -04001925 {
1926 s->tlsext_debug_cb(s, 1, type, (unsigned char*)CBS_data(&extension),
1927 CBS_len(&extension), s->tlsext_debug_arg);
1928 }
Adam Langley95c29f32014-06-20 12:00:00 -07001929
1930 if (type == TLSEXT_TYPE_server_name)
1931 {
David Benjamin03973092014-06-24 23:27:17 -04001932 /* The extension must be empty. */
1933 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001934 {
David Benjamin03973092014-06-24 23:27:17 -04001935 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001936 return 0;
1937 }
David Benjamin03973092014-06-24 23:27:17 -04001938 /* We must have sent it in ClientHello. */
1939 if (s->tlsext_hostname == NULL)
1940 {
1941 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1942 return 0;
1943 }
1944 tlsext_servername = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001945 }
Adam Langley95c29f32014-06-20 12:00:00 -07001946 else if (type == TLSEXT_TYPE_ec_point_formats)
1947 {
David Benjamin03973092014-06-24 23:27:17 -04001948 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001949
David Benjamin03973092014-06-24 23:27:17 -04001950 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1951 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001952 {
David Benjamin03973092014-06-24 23:27:17 -04001953 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001954 return 0;
1955 }
David Benjamin03973092014-06-24 23:27:17 -04001956
David Benjamina19fc252014-10-19 00:14:36 -04001957 if (!CBS_stow(&ec_point_format_list,
1958 &s->s3->tmp.peer_ecpointformatlist,
1959 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001960 {
David Benjamina19fc252014-10-19 00:14:36 -04001961 *out_alert = SSL_AD_INTERNAL_ERROR;
1962 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001963 }
Adam Langley95c29f32014-06-20 12:00:00 -07001964 }
Adam Langley95c29f32014-06-20 12:00:00 -07001965 else if (type == TLSEXT_TYPE_session_ticket)
1966 {
1967 if (s->tls_session_ticket_ext_cb &&
David Benjamin03973092014-06-24 23:27:17 -04001968 !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension),
1969 s->tls_session_ticket_ext_cb_arg))
Adam Langley95c29f32014-06-20 12:00:00 -07001970 {
David Benjamin03973092014-06-24 23:27:17 -04001971 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001972 return 0;
1973 }
David Benjamin03973092014-06-24 23:27:17 -04001974
1975 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001976 {
David Benjamin03973092014-06-24 23:27:17 -04001977 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07001978 return 0;
1979 }
David Benjamin03973092014-06-24 23:27:17 -04001980
Adam Langley95c29f32014-06-20 12:00:00 -07001981 s->tlsext_ticket_expected = 1;
1982 }
Adam Langley95c29f32014-06-20 12:00:00 -07001983 else if (type == TLSEXT_TYPE_status_request)
1984 {
David Benjamin03973092014-06-24 23:27:17 -04001985 /* The extension MUST be empty and may only sent if
1986 * we've requested a status request message. */
1987 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001988 {
David Benjamin03973092014-06-24 23:27:17 -04001989 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001990 return 0;
1991 }
David Benjamin6c7aed02014-08-27 16:42:38 -04001992 if (!s->ocsp_stapling_enabled)
David Benjamin03973092014-06-24 23:27:17 -04001993 {
1994 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1995 return 0;
1996 }
1997 /* Set a flag to expect a CertificateStatus message */
David Benjamin6c7aed02014-08-27 16:42:38 -04001998 s->s3->tmp.certificate_status_expected = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001999 }
David Benjamin03973092014-06-24 23:27:17 -04002000 else if (type == TLSEXT_TYPE_next_proto_neg && s->s3->tmp.finish_md_len == 0) {
2001 unsigned char *selected;
2002 unsigned char selected_len;
2003
2004 /* We must have requested it. */
2005 if (s->ctx->next_proto_select_cb == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002006 {
David Benjamin03973092014-06-24 23:27:17 -04002007 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2008 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002009 }
Adam Langley95c29f32014-06-20 12:00:00 -07002010
David Benjamin03973092014-06-24 23:27:17 -04002011 /* The data must be valid. */
2012 if (!ssl_next_proto_validate(&extension))
2013 {
2014 *out_alert = SSL_AD_DECODE_ERROR;
2015 return 0;
2016 }
2017
2018 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len,
2019 CBS_data(&extension), CBS_len(&extension),
2020 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
2021 {
2022 *out_alert = SSL_AD_INTERNAL_ERROR;
2023 return 0;
2024 }
2025
2026 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
2027 if (s->next_proto_negotiated == NULL)
2028 {
2029 *out_alert = SSL_AD_INTERNAL_ERROR;
2030 return 0;
2031 }
2032 s->next_proto_negotiated_len = selected_len;
2033 s->s3->next_proto_neg_seen = 1;
2034 }
Adam Langley95c29f32014-06-20 12:00:00 -07002035 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
2036 {
David Benjamin03973092014-06-24 23:27:17 -04002037 CBS protocol_name_list, protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07002038
2039 /* We must have requested it. */
2040 if (s->alpn_client_proto_list == NULL)
2041 {
David Benjamin03973092014-06-24 23:27:17 -04002042 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07002043 return 0;
2044 }
David Benjamin03973092014-06-24 23:27:17 -04002045
2046 /* The extension data consists of a ProtocolNameList
2047 * which must have exactly one ProtocolName. Each of
2048 * these is length-prefixed. */
2049 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
2050 CBS_len(&extension) != 0 ||
2051 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
2052 CBS_len(&protocol_name_list) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002053 {
David Benjamin03973092014-06-24 23:27:17 -04002054 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002055 return 0;
2056 }
David Benjamin03973092014-06-24 23:27:17 -04002057
2058 if (!CBS_stow(&protocol_name,
2059 &s->s3->alpn_selected,
2060 &s->s3->alpn_selected_len))
Adam Langley95c29f32014-06-20 12:00:00 -07002061 {
David Benjamin03973092014-06-24 23:27:17 -04002062 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002063 return 0;
2064 }
Adam Langley95c29f32014-06-20 12:00:00 -07002065 }
2066
Adam Langley1258b6a2014-06-20 12:00:00 -07002067 else if (type == TLSEXT_TYPE_channel_id)
David Benjamin03973092014-06-24 23:27:17 -04002068 {
2069 if (CBS_len(&extension) != 0)
2070 {
2071 *out_alert = SSL_AD_DECODE_ERROR;
2072 return 0;
2073 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002074 s->s3->tlsext_channel_id_valid = 1;
David Benjamin03973092014-06-24 23:27:17 -04002075 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002076 else if (type == TLSEXT_TYPE_channel_id_new)
2077 {
David Benjamin03973092014-06-24 23:27:17 -04002078 if (CBS_len(&extension) != 0)
2079 {
2080 *out_alert = SSL_AD_DECODE_ERROR;
2081 return 0;
2082 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002083 s->s3->tlsext_channel_id_valid = 1;
2084 s->s3->tlsext_channel_id_new = 1;
2085 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002086 else if (type == TLSEXT_TYPE_certificate_timestamp)
2087 {
2088 if (CBS_len(&extension) == 0)
2089 {
2090 *out_alert = SSL_AD_DECODE_ERROR;
2091 return 0;
2092 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002093
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002094 /* Session resumption uses the original session information. */
2095 if (!s->hit)
2096 {
2097 if (!CBS_stow(&extension,
2098 &s->session->tlsext_signed_cert_timestamp_list,
2099 &s->session->tlsext_signed_cert_timestamp_list_length))
2100 {
2101 *out_alert = SSL_AD_INTERNAL_ERROR;
2102 return 0;
2103 }
2104 }
2105 }
Adam Langley95c29f32014-06-20 12:00:00 -07002106 else if (type == TLSEXT_TYPE_renegotiate)
2107 {
David Benjamin03973092014-06-24 23:27:17 -04002108 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002109 return 0;
2110 renegotiate_seen = 1;
2111 }
Adam Langley95c29f32014-06-20 12:00:00 -07002112 else if (type == TLSEXT_TYPE_use_srtp)
2113 {
David Benjamin03973092014-06-24 23:27:17 -04002114 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002115 return 0;
2116 }
Adam Langley75712922014-10-10 16:23:43 -07002117
2118 else if (type == TLSEXT_TYPE_extended_master_secret)
2119 {
2120 if (/* It is invalid for the server to select EMS and
2121 SSLv3. */
2122 s->version == SSL3_VERSION ||
2123 CBS_len(&extension) != 0)
2124 {
2125 *out_alert = SSL_AD_DECODE_ERROR;
2126 return 0;
2127 }
2128
2129 s->s3->tmp.extended_master_secret = 1;
2130 }
Adam Langley95c29f32014-06-20 12:00:00 -07002131 }
2132
2133 if (!s->hit && tlsext_servername == 1)
2134 {
2135 if (s->tlsext_hostname)
2136 {
2137 if (s->session->tlsext_hostname == NULL)
2138 {
2139 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2140 if (!s->session->tlsext_hostname)
2141 {
David Benjamin03973092014-06-24 23:27:17 -04002142 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002143 return 0;
2144 }
2145 }
2146 else
2147 {
David Benjamin03973092014-06-24 23:27:17 -04002148 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002149 return 0;
2150 }
2151 }
2152 }
2153
Adam Langley95c29f32014-06-20 12:00:00 -07002154 ri_check:
2155
2156 /* Determine if we need to see RI. Strictly speaking if we want to
2157 * avoid an attack we should *always* see RI even on initial server
2158 * hello because the client doesn't see any renegotiation during an
2159 * attack. However this would mean we could not connect to any server
2160 * which doesn't support RI so for the immediate future tolerate RI
2161 * absence on initial connect only.
2162 */
2163 if (!renegotiate_seen
2164 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2165 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2166 {
David Benjamin03973092014-06-24 23:27:17 -04002167 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin9d28c752014-07-05 00:43:48 -04002168 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07002169 return 0;
2170 }
2171
2172 return 1;
2173 }
2174
2175
2176int ssl_prepare_clienthello_tlsext(SSL *s)
2177 {
Adam Langley95c29f32014-06-20 12:00:00 -07002178 return 1;
2179 }
2180
2181int ssl_prepare_serverhello_tlsext(SSL *s)
2182 {
2183 return 1;
2184 }
2185
David Benjamin6c7aed02014-08-27 16:42:38 -04002186static int ssl_check_clienthello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002187 {
2188 int ret=SSL_TLSEXT_ERR_NOACK;
2189 int al = SSL_AD_UNRECOGNIZED_NAME;
2190
Adam Langley95c29f32014-06-20 12:00:00 -07002191 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2192 * ssl3_choose_cipher in s3_lib.c.
2193 */
2194 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2195 * ssl3_choose_cipher in s3_lib.c.
2196 */
Adam Langley95c29f32014-06-20 12:00:00 -07002197
2198 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2199 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2200 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2201 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2202
Adam Langley95c29f32014-06-20 12:00:00 -07002203 switch (ret)
2204 {
2205 case SSL_TLSEXT_ERR_ALERT_FATAL:
2206 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2207 return -1;
2208
2209 case SSL_TLSEXT_ERR_ALERT_WARNING:
2210 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002211 return 1;
2212
Adam Langley95c29f32014-06-20 12:00:00 -07002213 case SSL_TLSEXT_ERR_NOACK:
Adam Langleyed8270a2014-09-02 13:52:56 -07002214 s->should_ack_sni = 0;
2215 return 1;
2216
2217 default:
2218 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002219 }
2220 }
2221
David Benjamin6c7aed02014-08-27 16:42:38 -04002222static int ssl_check_serverhello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002223 {
2224 int ret=SSL_TLSEXT_ERR_NOACK;
2225 int al = SSL_AD_UNRECOGNIZED_NAME;
2226
Adam Langley95c29f32014-06-20 12:00:00 -07002227 /* If we are client and using an elliptic curve cryptography cipher
2228 * suite, then if server returns an EC point formats lists extension
2229 * it must contain uncompressed.
2230 */
2231 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2232 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamina9ca90a2014-09-26 18:53:43 -04002233 if (((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) &&
2234 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed))
Adam Langley95c29f32014-06-20 12:00:00 -07002235 {
David Benjamina9ca90a2014-09-26 18:53:43 -04002236 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2237 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002238 }
2239 ret = SSL_TLSEXT_ERR_OK;
Adam Langley95c29f32014-06-20 12:00:00 -07002240
2241 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2242 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2243 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2244 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2245
Adam Langley95c29f32014-06-20 12:00:00 -07002246 switch (ret)
2247 {
2248 case SSL_TLSEXT_ERR_ALERT_FATAL:
Adam Langleyed8270a2014-09-02 13:52:56 -07002249 ssl3_send_alert(s,SSL3_AL_FATAL,al);
Adam Langley95c29f32014-06-20 12:00:00 -07002250 return -1;
2251
2252 case SSL_TLSEXT_ERR_ALERT_WARNING:
2253 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002254 return 1;
2255
2256 default:
2257 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002258 }
2259 }
2260
David Benjamin03973092014-06-24 23:27:17 -04002261int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07002262 {
David Benjamin03973092014-06-24 23:27:17 -04002263 int alert = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002264 if (s->version < SSL3_VERSION)
2265 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002266
2267 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002268 {
David Benjamin03973092014-06-24 23:27:17 -04002269 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07002270 return 0;
2271 }
2272
David Benjamin03973092014-06-24 23:27:17 -04002273 if (ssl_check_serverhello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002274 {
David Benjamin9d28c752014-07-05 00:43:48 -04002275 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, SSL_R_SERVERHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07002276 return 0;
2277 }
David Benjamin03973092014-06-24 23:27:17 -04002278
Adam Langley95c29f32014-06-20 12:00:00 -07002279 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002280 }
Adam Langley95c29f32014-06-20 12:00:00 -07002281
2282/* Since the server cache lookup is done early on in the processing of the
2283 * ClientHello, and other operations depend on the result, we need to handle
2284 * any TLS session ticket extension at the same time.
2285 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002286 * ctx: contains the early callback context, which is the result of a
2287 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002288 * ret: (output) on return, if a ticket was decrypted, then this is set to
2289 * point to the resulting session.
2290 *
2291 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2292 * ciphersuite, in which case we have no use for session tickets and one will
2293 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2294 *
2295 * Returns:
2296 * -1: fatal error, either from parsing or decrypting the ticket.
2297 * 0: no ticket was found (or was ignored, based on settings).
2298 * 1: a zero length extension was found, indicating that the client supports
2299 * session tickets but doesn't currently have one to offer.
2300 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2301 * couldn't be decrypted because of a non-fatal error.
2302 * 3: a ticket was successfully decrypted and *ret was set.
2303 *
2304 * Side effects:
2305 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2306 * a new session ticket to the client because the client indicated support
2307 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2308 * a session ticket or we couldn't use the one it gave us, or if
2309 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2310 * Otherwise, s->tlsext_ticket_expected is set to 0.
2311 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002312int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
2313 SSL_SESSION **ret)
Adam Langley95c29f32014-06-20 12:00:00 -07002314 {
Adam Langley95c29f32014-06-20 12:00:00 -07002315 *ret = NULL;
2316 s->tlsext_ticket_expected = 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002317 const unsigned char *data;
2318 size_t len;
2319 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002320
2321 /* If tickets disabled behave as if no ticket present
2322 * to permit stateful resumption.
2323 */
2324 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2325 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002326 if ((s->version <= SSL3_VERSION) && !ctx->extensions)
Adam Langley95c29f32014-06-20 12:00:00 -07002327 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002328 if (!SSL_early_callback_ctx_extension_get(
2329 ctx, TLSEXT_TYPE_session_ticket, &data, &len))
Adam Langley95c29f32014-06-20 12:00:00 -07002330 {
Adam Langleydc9b1412014-06-20 12:00:00 -07002331 return 0;
2332 }
2333 if (len == 0)
2334 {
2335 /* The client will accept a ticket but doesn't
2336 * currently have one. */
2337 s->tlsext_ticket_expected = 1;
2338 return 1;
2339 }
2340 if (s->tls_session_secret_cb)
2341 {
2342 /* Indicate that the ticket couldn't be
2343 * decrypted rather than generating the session
2344 * from ticket now, trigger abbreviated
2345 * handshake based on external mechanism to
2346 * calculate the master secret later. */
2347 return 2;
2348 }
2349 r = tls_decrypt_ticket(s, data, len, ctx->session_id,
2350 ctx->session_id_len, ret);
2351 switch (r)
2352 {
2353 case 2: /* ticket couldn't be decrypted */
2354 s->tlsext_ticket_expected = 1;
2355 return 2;
2356 case 3: /* ticket was decrypted */
2357 return r;
2358 case 4: /* ticket decrypted but need to renew */
2359 s->tlsext_ticket_expected = 1;
2360 return 3;
2361 default: /* fatal error */
Adam Langley95c29f32014-06-20 12:00:00 -07002362 return -1;
2363 }
Adam Langley95c29f32014-06-20 12:00:00 -07002364 }
2365
2366/* tls_decrypt_ticket attempts to decrypt a session ticket.
2367 *
2368 * etick: points to the body of the session ticket extension.
2369 * eticklen: the length of the session tickets extenion.
2370 * sess_id: points at the session ID.
2371 * sesslen: the length of the session ID.
2372 * psess: (output) on return, if a ticket was decrypted, then this is set to
2373 * point to the resulting session.
2374 *
2375 * Returns:
2376 * -1: fatal error, either from parsing or decrypting the ticket.
2377 * 2: the ticket couldn't be decrypted.
2378 * 3: a ticket was successfully decrypted and *psess was set.
2379 * 4: same as 3, but the ticket needs to be renewed.
2380 */
2381static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
2382 const unsigned char *sess_id, int sesslen,
2383 SSL_SESSION **psess)
2384 {
2385 SSL_SESSION *sess;
2386 unsigned char *sdec;
2387 const unsigned char *p;
2388 int slen, mlen, renew_ticket = 0;
2389 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2390 HMAC_CTX hctx;
2391 EVP_CIPHER_CTX ctx;
2392 SSL_CTX *tctx = s->initial_ctx;
2393 /* Need at least keyname + iv + some encrypted data */
2394 if (eticklen < 48)
2395 return 2;
2396 /* Initialize session ticket encryption and HMAC contexts */
2397 HMAC_CTX_init(&hctx);
2398 EVP_CIPHER_CTX_init(&ctx);
2399 if (tctx->tlsext_ticket_key_cb)
2400 {
2401 unsigned char *nctick = (unsigned char *)etick;
2402 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
2403 &ctx, &hctx, 0);
2404 if (rv < 0)
2405 return -1;
2406 if (rv == 0)
2407 return 2;
2408 if (rv == 2)
2409 renew_ticket = 1;
2410 }
2411 else
2412 {
2413 /* Check key name matches */
2414 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
2415 return 2;
2416 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2417 tlsext_tick_md(), NULL);
2418 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2419 tctx->tlsext_tick_aes_key, etick + 16);
2420 }
2421 /* Attempt to process session ticket, first conduct sanity and
2422 * integrity checks on ticket.
2423 */
2424 mlen = HMAC_size(&hctx);
2425 if (mlen < 0)
2426 {
2427 EVP_CIPHER_CTX_cleanup(&ctx);
2428 return -1;
2429 }
2430 eticklen -= mlen;
2431 /* Check HMAC of encrypted ticket */
2432 HMAC_Update(&hctx, etick, eticklen);
2433 HMAC_Final(&hctx, tick_hmac, NULL);
2434 HMAC_CTX_cleanup(&hctx);
2435 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
Adam Langley38311732014-10-16 19:04:35 -07002436 {
2437 EVP_CIPHER_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002438 return 2;
Adam Langley38311732014-10-16 19:04:35 -07002439 }
Adam Langley95c29f32014-06-20 12:00:00 -07002440 /* Attempt to decrypt session data */
2441 /* Move p after IV to start of encrypted ticket, update length */
2442 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2443 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2444 sdec = OPENSSL_malloc(eticklen);
2445 if (!sdec)
2446 {
2447 EVP_CIPHER_CTX_cleanup(&ctx);
2448 return -1;
2449 }
2450 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2451 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0)
Adam Langley3e148852014-07-24 17:34:02 -07002452 {
2453 EVP_CIPHER_CTX_cleanup(&ctx);
2454 OPENSSL_free(sdec);
Adam Langley95c29f32014-06-20 12:00:00 -07002455 return 2;
Adam Langley3e148852014-07-24 17:34:02 -07002456 }
Adam Langley95c29f32014-06-20 12:00:00 -07002457 slen += mlen;
2458 EVP_CIPHER_CTX_cleanup(&ctx);
2459 p = sdec;
2460
2461 sess = d2i_SSL_SESSION(NULL, &p, slen);
2462 OPENSSL_free(sdec);
2463 if (sess)
2464 {
2465 /* The session ID, if non-empty, is used by some clients to
2466 * detect that the ticket has been accepted. So we copy it to
2467 * the session structure. If it is empty set length to zero
2468 * as required by standard.
2469 */
2470 if (sesslen)
2471 memcpy(sess->session_id, sess_id, sesslen);
2472 sess->session_id_length = sesslen;
2473 *psess = sess;
2474 if (renew_ticket)
2475 return 4;
2476 else
2477 return 3;
2478 }
2479 ERR_clear_error();
2480 /* For session parse failure, indicate that we need to send a new
2481 * ticket. */
2482 return 2;
2483 }
2484
2485/* Tables to translate from NIDs to TLS v1.2 ids */
2486
2487typedef struct
2488 {
2489 int nid;
2490 int id;
2491 } tls12_lookup;
2492
David Benjamincff64722014-08-19 19:54:46 -04002493static const tls12_lookup tls12_md[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002494 {NID_md5, TLSEXT_hash_md5},
2495 {NID_sha1, TLSEXT_hash_sha1},
2496 {NID_sha224, TLSEXT_hash_sha224},
2497 {NID_sha256, TLSEXT_hash_sha256},
2498 {NID_sha384, TLSEXT_hash_sha384},
2499 {NID_sha512, TLSEXT_hash_sha512}
2500};
2501
David Benjamincff64722014-08-19 19:54:46 -04002502static const tls12_lookup tls12_sig[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002503 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
Adam Langley95c29f32014-06-20 12:00:00 -07002504 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
2505};
2506
David Benjamincff64722014-08-19 19:54:46 -04002507static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002508 {
2509 size_t i;
2510 for (i = 0; i < tlen; i++)
2511 {
2512 if (table[i].nid == nid)
2513 return table[i].id;
2514 }
2515 return -1;
2516 }
2517
David Benjamincff64722014-08-19 19:54:46 -04002518static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002519 {
2520 size_t i;
2521 for (i = 0; i < tlen; i++)
2522 {
2523 if ((table[i].id) == id)
2524 return table[i].nid;
2525 }
2526 return NID_undef;
2527 }
2528
2529int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
2530 {
2531 int sig_id, md_id;
2532 if (!md)
2533 return 0;
2534 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2535 sizeof(tls12_md)/sizeof(tls12_lookup));
2536 if (md_id == -1)
2537 return 0;
2538 sig_id = tls12_get_sigid(pk);
2539 if (sig_id == -1)
2540 return 0;
2541 p[0] = (unsigned char)md_id;
2542 p[1] = (unsigned char)sig_id;
2543 return 1;
2544 }
2545
2546int tls12_get_sigid(const EVP_PKEY *pk)
2547 {
2548 return tls12_find_id(pk->type, tls12_sig,
2549 sizeof(tls12_sig)/sizeof(tls12_lookup));
2550 }
2551
2552const EVP_MD *tls12_get_hash(unsigned char hash_alg)
2553 {
2554 switch(hash_alg)
2555 {
Adam Langley95c29f32014-06-20 12:00:00 -07002556 case TLSEXT_hash_md5:
Adam Langley95c29f32014-06-20 12:00:00 -07002557 return EVP_md5();
Adam Langley95c29f32014-06-20 12:00:00 -07002558 case TLSEXT_hash_sha1:
2559 return EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002560 case TLSEXT_hash_sha224:
2561 return EVP_sha224();
2562
2563 case TLSEXT_hash_sha256:
2564 return EVP_sha256();
Adam Langley95c29f32014-06-20 12:00:00 -07002565 case TLSEXT_hash_sha384:
2566 return EVP_sha384();
2567
2568 case TLSEXT_hash_sha512:
2569 return EVP_sha512();
Adam Langley95c29f32014-06-20 12:00:00 -07002570 default:
2571 return NULL;
2572
2573 }
2574 }
2575
2576static int tls12_get_pkey_idx(unsigned char sig_alg)
2577 {
2578 switch(sig_alg)
2579 {
Adam Langley95c29f32014-06-20 12:00:00 -07002580 case TLSEXT_signature_rsa:
2581 return SSL_PKEY_RSA_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002582 case TLSEXT_signature_ecdsa:
2583 return SSL_PKEY_ECC;
Adam Langley95c29f32014-06-20 12:00:00 -07002584 }
2585 return -1;
2586 }
2587
2588/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2589static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
2590 int *psignhash_nid, const unsigned char *data)
2591 {
2592 int sign_nid = 0, hash_nid = 0;
2593 if (!phash_nid && !psign_nid && !psignhash_nid)
2594 return;
2595 if (phash_nid || psignhash_nid)
2596 {
2597 hash_nid = tls12_find_nid(data[0], tls12_md,
2598 sizeof(tls12_md)/sizeof(tls12_lookup));
2599 if (phash_nid)
2600 *phash_nid = hash_nid;
2601 }
2602 if (psign_nid || psignhash_nid)
2603 {
2604 sign_nid = tls12_find_nid(data[1], tls12_sig,
2605 sizeof(tls12_sig)/sizeof(tls12_lookup));
2606 if (psign_nid)
2607 *psign_nid = sign_nid;
2608 }
2609 if (psignhash_nid)
2610 {
2611 if (sign_nid && hash_nid)
2612 OBJ_find_sigid_by_algs(psignhash_nid,
2613 hash_nid, sign_nid);
2614 else
2615 *psignhash_nid = NID_undef;
2616 }
2617 }
2618/* Given preference and allowed sigalgs set shared sigalgs */
2619static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
2620 const unsigned char *pref, size_t preflen,
2621 const unsigned char *allow, size_t allowlen)
2622 {
2623 const unsigned char *ptmp, *atmp;
2624 size_t i, j, nmatch = 0;
2625 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
2626 {
2627 /* Skip disabled hashes or signature algorithms */
2628 if (tls12_get_hash(ptmp[0]) == NULL)
2629 continue;
2630 if (tls12_get_pkey_idx(ptmp[1]) == -1)
2631 continue;
2632 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
2633 {
2634 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
2635 {
2636 nmatch++;
2637 if (shsig)
2638 {
2639 shsig->rhash = ptmp[0];
2640 shsig->rsign = ptmp[1];
2641 tls1_lookup_sigalg(&shsig->hash_nid,
2642 &shsig->sign_nid,
2643 &shsig->signandhash_nid,
2644 ptmp);
2645 shsig++;
2646 }
2647 break;
2648 }
2649 }
2650 }
2651 return nmatch;
2652 }
2653
2654/* Set shared signature algorithms for SSL structures */
2655static int tls1_set_shared_sigalgs(SSL *s)
2656 {
2657 const unsigned char *pref, *allow, *conf;
2658 size_t preflen, allowlen, conflen;
2659 size_t nmatch;
2660 TLS_SIGALGS *salgs = NULL;
2661 CERT *c = s->cert;
Adam Langleydb4f9522014-06-20 12:00:00 -07002662 if (c->shared_sigalgs)
2663 {
2664 OPENSSL_free(c->shared_sigalgs);
2665 c->shared_sigalgs = NULL;
2666 }
Adam Langley95c29f32014-06-20 12:00:00 -07002667 /* If client use client signature algorithms if not NULL */
David Benjamin335d10d2014-08-06 19:56:33 -04002668 if (!s->server && c->client_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002669 {
2670 conf = c->client_sigalgs;
2671 conflen = c->client_sigalgslen;
2672 }
David Benjamin335d10d2014-08-06 19:56:33 -04002673 else if (c->conf_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002674 {
2675 conf = c->conf_sigalgs;
2676 conflen = c->conf_sigalgslen;
2677 }
2678 else
2679 conflen = tls12_get_psigalgs(s, &conf);
David Benjamin335d10d2014-08-06 19:56:33 -04002680 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
Adam Langley95c29f32014-06-20 12:00:00 -07002681 {
2682 pref = conf;
2683 preflen = conflen;
2684 allow = c->peer_sigalgs;
2685 allowlen = c->peer_sigalgslen;
2686 }
2687 else
2688 {
2689 allow = conf;
2690 allowlen = conflen;
2691 pref = c->peer_sigalgs;
2692 preflen = c->peer_sigalgslen;
2693 }
2694 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2695 if (!nmatch)
2696 return 1;
2697 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2698 if (!salgs)
2699 return 0;
2700 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2701 c->shared_sigalgs = salgs;
2702 c->shared_sigalgslen = nmatch;
2703 return 1;
2704 }
2705
2706
2707/* Set preferred digest for each key type */
2708
David Benjamincd996942014-07-20 16:23:51 -04002709int tls1_process_sigalgs(SSL *s, const CBS *sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002710 {
2711 int idx;
2712 size_t i;
2713 const EVP_MD *md;
2714 CERT *c = s->cert;
2715 TLS_SIGALGS *sigptr;
David Benjamincd996942014-07-20 16:23:51 -04002716
Adam Langley95c29f32014-06-20 12:00:00 -07002717 /* Extension ignored for inappropriate versions */
2718 if (!SSL_USE_SIGALGS(s))
2719 return 1;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002720 /* Length must be even */
David Benjamincd996942014-07-20 16:23:51 -04002721 if (CBS_len(sigalgs) % 2 != 0)
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002722 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002723 /* Should never happen */
2724 if (!c)
2725 return 0;
2726
David Benjamincd996942014-07-20 16:23:51 -04002727 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen))
Adam Langley95c29f32014-06-20 12:00:00 -07002728 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002729
2730 tls1_set_shared_sigalgs(s);
2731
Adam Langley95c29f32014-06-20 12:00:00 -07002732 for (i = 0, sigptr = c->shared_sigalgs;
2733 i < c->shared_sigalgslen; i++, sigptr++)
2734 {
2735 idx = tls12_get_pkey_idx(sigptr->rsign);
2736 if (idx > 0 && c->pkeys[idx].digest == NULL)
2737 {
2738 md = tls12_get_hash(sigptr->rhash);
2739 c->pkeys[idx].digest = md;
Adam Langley95c29f32014-06-20 12:00:00 -07002740 if (idx == SSL_PKEY_RSA_SIGN)
2741 {
Adam Langley95c29f32014-06-20 12:00:00 -07002742 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2743 }
2744 }
2745
2746 }
David Benjamin253b3e72014-11-13 15:53:52 -05002747
2748 /* Set any remaining keys to default values. NOTE: if alg is
2749 * not supported it stays as NULL.
Adam Langley95c29f32014-06-20 12:00:00 -07002750 */
David Benjamin253b3e72014-11-13 15:53:52 -05002751 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
Adam Langley95c29f32014-06-20 12:00:00 -07002752 {
David Benjamin253b3e72014-11-13 15:53:52 -05002753 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
2754 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002755 }
David Benjamin253b3e72014-11-13 15:53:52 -05002756 if (!c->pkeys[SSL_PKEY_ECC].digest)
2757 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002758 return 1;
2759 }
2760
2761
2762int SSL_get_sigalgs(SSL *s, int idx,
2763 int *psign, int *phash, int *psignhash,
2764 unsigned char *rsig, unsigned char *rhash)
2765 {
2766 const unsigned char *psig = s->cert->peer_sigalgs;
2767 if (psig == NULL)
2768 return 0;
2769 if (idx >= 0)
2770 {
2771 idx <<= 1;
2772 if (idx >= (int)s->cert->peer_sigalgslen)
2773 return 0;
2774 psig += idx;
2775 if (rhash)
2776 *rhash = psig[0];
2777 if (rsig)
2778 *rsig = psig[1];
2779 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2780 }
2781 return s->cert->peer_sigalgslen / 2;
2782 }
2783
2784int SSL_get_shared_sigalgs(SSL *s, int idx,
2785 int *psign, int *phash, int *psignhash,
2786 unsigned char *rsig, unsigned char *rhash)
2787 {
2788 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
2789 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
2790 return 0;
2791 shsigalgs += idx;
2792 if (phash)
2793 *phash = shsigalgs->hash_nid;
2794 if (psign)
2795 *psign = shsigalgs->sign_nid;
2796 if (psignhash)
2797 *psignhash = shsigalgs->signandhash_nid;
2798 if (rsig)
2799 *rsig = shsigalgs->rsign;
2800 if (rhash)
2801 *rhash = shsigalgs->rhash;
2802 return s->cert->shared_sigalgslen;
2803 }
2804
Adam Langley1258b6a2014-06-20 12:00:00 -07002805/* tls1_channel_id_hash calculates the signed data for a Channel ID on the given
2806 * SSL connection and writes it to |md|. */
2807int
2808tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s)
2809 {
2810 EVP_MD_CTX ctx;
2811 unsigned char temp_digest[EVP_MAX_MD_SIZE];
2812 unsigned temp_digest_len;
2813 int i;
2814 static const char kClientIDMagic[] = "TLS Channel ID signature";
2815
2816 if (s->s3->handshake_buffer)
Adam Langley75712922014-10-10 16:23:43 -07002817 if (!ssl3_digest_cached_records(s, free_handshake_buffer))
Adam Langley1258b6a2014-06-20 12:00:00 -07002818 return 0;
2819
2820 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2821
2822 if (s->hit && s->s3->tlsext_channel_id_new)
2823 {
2824 static const char kResumptionMagic[] = "Resumption";
2825 EVP_DigestUpdate(md, kResumptionMagic,
2826 sizeof(kResumptionMagic));
2827 if (s->session->original_handshake_hash_len == 0)
2828 return 0;
2829 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2830 s->session->original_handshake_hash_len);
2831 }
2832
2833 EVP_MD_CTX_init(&ctx);
2834 for (i = 0; i < SSL_MAX_DIGEST; i++)
2835 {
2836 if (s->s3->handshake_dgst[i] == NULL)
2837 continue;
2838 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
2839 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2840 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2841 }
2842 EVP_MD_CTX_cleanup(&ctx);
2843
2844 return 1;
2845 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002846
2847/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2848 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
2849int tls1_record_handshake_hashes_for_channel_id(SSL *s)
2850 {
2851 int digest_len;
2852 /* This function should never be called for a resumed session because
2853 * the handshake hashes that we wish to record are for the original,
2854 * full handshake. */
2855 if (s->hit)
2856 return -1;
2857 /* It only makes sense to call this function if Channel IDs have been
2858 * negotiated. */
2859 if (!s->s3->tlsext_channel_id_new)
2860 return -1;
2861
2862 digest_len = tls1_handshake_digest(
2863 s, s->session->original_handshake_hash,
2864 sizeof(s->session->original_handshake_hash));
2865 if (digest_len < 0)
2866 return -1;
2867
2868 s->session->original_handshake_hash_len = digest_len;
2869
2870 return 1;
2871 }
2872
Adam Langley95c29f32014-06-20 12:00:00 -07002873int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
2874 {
2875 unsigned char *sigalgs, *sptr;
2876 int rhash, rsign;
2877 size_t i;
2878 if (salglen & 1)
2879 return 0;
2880 sigalgs = OPENSSL_malloc(salglen);
2881 if (sigalgs == NULL)
2882 return 0;
2883 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
2884 {
2885 rhash = tls12_find_id(*psig_nids++, tls12_md,
2886 sizeof(tls12_md)/sizeof(tls12_lookup));
2887 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2888 sizeof(tls12_sig)/sizeof(tls12_lookup));
2889
2890 if (rhash == -1 || rsign == -1)
2891 goto err;
2892 *sptr++ = rhash;
2893 *sptr++ = rsign;
2894 }
2895
2896 if (client)
2897 {
2898 if (c->client_sigalgs)
2899 OPENSSL_free(c->client_sigalgs);
2900 c->client_sigalgs = sigalgs;
2901 c->client_sigalgslen = salglen;
2902 }
2903 else
2904 {
2905 if (c->conf_sigalgs)
2906 OPENSSL_free(c->conf_sigalgs);
2907 c->conf_sigalgs = sigalgs;
2908 c->conf_sigalgslen = salglen;
2909 }
2910
2911 return 1;
2912
2913 err:
2914 OPENSSL_free(sigalgs);
2915 return 0;
2916 }
2917
David Benjaminb398d162014-11-13 15:29:09 -05002918/* Check certificate chain is consistent with TLS extensions and is usable by
2919 * server. This allows the server to check chains before attempting to use them.
Adam Langley95c29f32014-06-20 12:00:00 -07002920 */
2921
David Benjamin6a8d70c2014-11-13 16:02:33 -05002922void tls1_check_chain(SSL *s, size_t idx)
Adam Langley95c29f32014-06-20 12:00:00 -07002923 {
David Benjamin6a8d70c2014-11-13 16:02:33 -05002924 CERT_PKEY *cpk = &s->cert->pkeys[idx];
David Benjaminb398d162014-11-13 15:29:09 -05002925
David Benjamin6a8d70c2014-11-13 16:02:33 -05002926 /* Clear the flags. */
2927 cpk->valid_flags = 0;
2928
2929 /* If no cert or key, forget it. */
2930 if (!cpk->x509 || !cpk->privatekey)
2931 return;
Adam Langley95c29f32014-06-20 12:00:00 -07002932
Adam Langley95c29f32014-06-20 12:00:00 -07002933 /* Check cert parameters are consistent */
David Benjamin6a8d70c2014-11-13 16:02:33 -05002934 if (!tls1_check_cert_param(s, cpk->x509))
2935 return;
Adam Langley95c29f32014-06-20 12:00:00 -07002936
David Benjamin6a8d70c2014-11-13 16:02:33 -05002937 cpk->valid_flags = CERT_PKEY_VALID;
2938 if (!SSL_USE_SIGALGS(s) || cpk->digest)
2939 cpk->valid_flags |= CERT_PKEY_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002940 }
2941
2942/* Set validity of certificates in an SSL structure */
2943void tls1_set_cert_validity(SSL *s)
2944 {
David Benjaminb398d162014-11-13 15:29:09 -05002945 tls1_check_chain(s, SSL_PKEY_RSA_ENC);
2946 tls1_check_chain(s, SSL_PKEY_RSA_SIGN);
2947 tls1_check_chain(s, SSL_PKEY_ECC);
Adam Langley95c29f32014-06-20 12:00:00 -07002948 }
2949