blob: d88fdea8582da044e07772c4e6d06451890369ff [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
David Benjamin033e5f42014-11-13 18:47:41 -0500657int tls1_check_ec_cert(SSL *s, X509 *x)
Adam Langley95c29f32014-06-20 12:00:00 -0700658 {
David Benjamin033e5f42014-11-13 18:47:41 -0500659 int ret = 0;
660 EVP_PKEY *pkey = X509_get_pubkey(x);
David Benjamin072334d2014-07-13 16:24:27 -0400661 uint16_t curve_id;
David Benjamin033e5f42014-11-13 18:47:41 -0500662 uint8_t comp_id;
663
664 if (!pkey ||
665 pkey->type != EVP_PKEY_EC ||
666 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
667 !tls1_check_curve_id(s, curve_id) ||
668 !tls1_check_point_format(s, comp_id))
Adam Langley95c29f32014-06-20 12:00:00 -0700669 {
David Benjamin033e5f42014-11-13 18:47:41 -0500670 goto done;
Adam Langley95c29f32014-06-20 12:00:00 -0700671 }
David Benjamin033e5f42014-11-13 18:47:41 -0500672
673 ret = 1;
674
675done:
676 if (pkey)
677 EVP_PKEY_free(pkey);
678 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700679 }
David Benjamin42e9a772014-09-02 23:18:44 -0400680
David Benjamin42e9a772014-09-02 23:18:44 -0400681int tls1_check_ec_tmp_key(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -0700682 {
David Benjamin072334d2014-07-13 16:24:27 -0400683 uint16_t curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700684 EC_KEY *ec = s->cert->ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -0700685 if (s->cert->ecdh_tmp_auto)
686 {
687 /* Need a shared curve */
David Benjamin072334d2014-07-13 16:24:27 -0400688 return tls1_get_shared_curve(s) != NID_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700689 }
690 if (!ec)
691 {
692 if (s->cert->ecdh_tmp_cb)
693 return 1;
694 else
695 return 0;
696 }
David Benjamin42e9a772014-09-02 23:18:44 -0400697 return tls1_curve_params_from_ec_key(&curve_id, NULL, ec) &&
698 tls1_check_curve_id(s, curve_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700699 }
700
Adam Langley95c29f32014-06-20 12:00:00 -0700701
Adam Langley95c29f32014-06-20 12:00:00 -0700702
703/* List of supported signature algorithms and hashes. Should make this
704 * customisable at some point, for now include everything we support.
705 */
706
Adam Langley95c29f32014-06-20 12:00:00 -0700707#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700708
Adam Langley95c29f32014-06-20 12:00:00 -0700709#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700710
711#define tlsext_sigalg(md) \
712 tlsext_sigalg_rsa(md) \
Adam Langley95c29f32014-06-20 12:00:00 -0700713 tlsext_sigalg_ecdsa(md)
714
David Benjamincff64722014-08-19 19:54:46 -0400715static const uint8_t tls12_sigalgs[] = {
Adam Langley95c29f32014-06-20 12:00:00 -0700716 tlsext_sigalg(TLSEXT_hash_sha512)
717 tlsext_sigalg(TLSEXT_hash_sha384)
Adam Langley95c29f32014-06-20 12:00:00 -0700718 tlsext_sigalg(TLSEXT_hash_sha256)
719 tlsext_sigalg(TLSEXT_hash_sha224)
Adam Langley95c29f32014-06-20 12:00:00 -0700720 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700721};
Adam Langley95c29f32014-06-20 12:00:00 -0700722size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
723 {
Adam Langley95c29f32014-06-20 12:00:00 -0700724 /* If server use client authentication sigalgs if not NULL */
725 if (s->server && s->cert->client_sigalgs)
726 {
727 *psigs = s->cert->client_sigalgs;
728 return s->cert->client_sigalgslen;
729 }
730 else if (s->cert->conf_sigalgs)
731 {
732 *psigs = s->cert->conf_sigalgs;
733 return s->cert->conf_sigalgslen;
734 }
735 else
736 {
737 *psigs = tls12_sigalgs;
738 return sizeof(tls12_sigalgs);
739 }
740 }
David Benjamin05da6e12014-07-12 20:42:55 -0400741
742/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of
743 * |cbs|. It checks it is consistent with |s|'s sent supported
744 * signature algorithms and, if so, writes the relevant digest into
745 * |*out_md| and returns 1. Otherwise it returns 0 and writes an alert
746 * into |*out_alert|.
Adam Langley95c29f32014-06-20 12:00:00 -0700747 */
David Benjamin05da6e12014-07-12 20:42:55 -0400748int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert,
749 SSL *s, CBS *cbs, EVP_PKEY *pkey)
Adam Langley95c29f32014-06-20 12:00:00 -0700750 {
751 const unsigned char *sent_sigs;
752 size_t sent_sigslen, i;
753 int sigalg = tls12_get_sigid(pkey);
David Benjamin05da6e12014-07-12 20:42:55 -0400754 uint8_t hash, signature;
Adam Langley95c29f32014-06-20 12:00:00 -0700755 /* Should never happen */
756 if (sigalg == -1)
David Benjamin05da6e12014-07-12 20:42:55 -0400757 {
758 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
759 *out_alert = SSL_AD_INTERNAL_ERROR;
760 return 0;
761 }
762 if (!CBS_get_u8(cbs, &hash) ||
763 !CBS_get_u8(cbs, &signature))
764 {
765 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
766 *out_alert = SSL_AD_DECODE_ERROR;
767 return 0;
768 }
Adam Langley95c29f32014-06-20 12:00:00 -0700769 /* Check key type is consistent with signature */
David Benjamin05da6e12014-07-12 20:42:55 -0400770 if (sigalg != signature)
Adam Langley95c29f32014-06-20 12:00:00 -0700771 {
772 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin05da6e12014-07-12 20:42:55 -0400773 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700774 return 0;
775 }
Adam Langley95c29f32014-06-20 12:00:00 -0700776 if (pkey->type == EVP_PKEY_EC)
777 {
David Benjamin072334d2014-07-13 16:24:27 -0400778 uint16_t curve_id;
779 uint8_t comp_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700780 /* Check compression and curve matches extensions */
David Benjamin072334d2014-07-13 16:24:27 -0400781 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec))
David Benjamin05da6e12014-07-12 20:42:55 -0400782 {
783 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -0700784 return 0;
David Benjamin05da6e12014-07-12 20:42:55 -0400785 }
David Benjamin42e9a772014-09-02 23:18:44 -0400786 if (s->server)
Adam Langley95c29f32014-06-20 12:00:00 -0700787 {
David Benjamin42e9a772014-09-02 23:18:44 -0400788 if (!tls1_check_curve_id(s, curve_id) ||
789 !tls1_check_point_format(s, comp_id))
790 {
791 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
792 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
793 return 0;
794 }
Adam Langley95c29f32014-06-20 12:00:00 -0700795 }
David Benjamin05da6e12014-07-12 20:42:55 -0400796 }
Adam Langley95c29f32014-06-20 12:00:00 -0700797
798 /* Check signature matches a type we sent */
799 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
800 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
801 {
David Benjamin05da6e12014-07-12 20:42:55 -0400802 if (hash == sent_sigs[0] && signature == sent_sigs[1])
Adam Langley95c29f32014-06-20 12:00:00 -0700803 break;
804 }
David Benjamin253b3e72014-11-13 15:53:52 -0500805 /* Allow fallback to SHA-1. */
806 if (i == sent_sigslen && hash != TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700807 {
808 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin05da6e12014-07-12 20:42:55 -0400809 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700810 return 0;
811 }
David Benjamin05da6e12014-07-12 20:42:55 -0400812 *out_md = tls12_get_hash(hash);
813 if (*out_md == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700814 {
815 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
David Benjamin05da6e12014-07-12 20:42:55 -0400816 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700817 return 0;
818 }
Adam Langley95c29f32014-06-20 12:00:00 -0700819 return 1;
820 }
821/* Get a mask of disabled algorithms: an algorithm is disabled
822 * if it isn't supported or doesn't appear in supported signature
823 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
824 * session and not global settings.
825 *
826 */
827void ssl_set_client_disabled(SSL *s)
828 {
829 CERT *c = s->cert;
830 const unsigned char *sigalgs;
831 size_t i, sigalgslen;
David Benjaminef2116d2014-08-19 20:21:56 -0400832 int have_rsa = 0, have_ecdsa = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700833 c->mask_a = 0;
834 c->mask_k = 0;
835 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
836 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
837 c->mask_ssl = SSL_TLSV1_2;
838 else
839 c->mask_ssl = 0;
840 /* Now go through all signature algorithms seeing if we support
841 * any for RSA, DSA, ECDSA. Do this for all versions not just
842 * TLS 1.2.
843 */
844 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
845 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
846 {
847 switch(sigalgs[1])
848 {
Adam Langley95c29f32014-06-20 12:00:00 -0700849 case TLSEXT_signature_rsa:
850 have_rsa = 1;
851 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700852 case TLSEXT_signature_ecdsa:
853 have_ecdsa = 1;
854 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700855 }
856 }
David Benjamin0da0e182014-08-19 16:20:28 -0400857 /* Disable auth if we don't include any appropriate signature
858 * algorithms.
Adam Langley95c29f32014-06-20 12:00:00 -0700859 */
860 if (!have_rsa)
861 {
862 c->mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700863 }
Adam Langley95c29f32014-06-20 12:00:00 -0700864 if (!have_ecdsa)
865 {
866 c->mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700867 }
Adam Langley95c29f32014-06-20 12:00:00 -0700868 /* with PSK there must be client callback set */
869 if (!s->psk_client_callback)
870 {
871 c->mask_a |= SSL_aPSK;
872 c->mask_k |= SSL_kPSK;
873 }
Adam Langley95c29f32014-06-20 12:00:00 -0700874 }
875
Adam Langleyb0c235e2014-06-20 12:00:00 -0700876/* header_len is the length of the ClientHello header written so far, used to
877 * compute padding. It does not include the record header. Pass 0 if no padding
878 * is to be done. */
879unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, size_t header_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700880 {
881 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -0700882 unsigned char *ret = buf;
883 unsigned char *orig = buf;
Adam Langley95c29f32014-06-20 12:00:00 -0700884 /* See if we support any ECC ciphersuites */
885 int using_ecc = 0;
886 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
887 {
David Benjaminfb3ff2c2014-09-30 21:00:38 -0400888 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -0700889 unsigned long alg_k, alg_a;
890 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
891
892 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
893 {
David Benjamin6f260012014-08-15 13:49:12 -0400894 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700895
896 alg_k = c->algorithm_mkey;
897 alg_a = c->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -0400898 if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA))
Adam Langley95c29f32014-06-20 12:00:00 -0700899 {
900 using_ecc = 1;
901 break;
902 }
903 }
904 }
Adam Langley95c29f32014-06-20 12:00:00 -0700905
906 /* don't add extensions for SSLv3 unless doing secure renegotiation */
907 if (s->client_version == SSL3_VERSION
908 && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -0700909 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -0700910
911 ret+=2;
912
913 if (ret>=limit) return NULL; /* this really never occurs, but ... */
914
915 if (s->tlsext_hostname != NULL)
916 {
917 /* Add TLS extension servername to the Client Hello message */
918 unsigned long size_str;
919 long lenmax;
920
921 /* check for enough space.
922 4 for the servername type and entension length
923 2 for servernamelist length
924 1 for the hostname type
925 2 for hostname length
926 + hostname length
927 */
928
929 if ((lenmax = limit - ret - 9) < 0
930 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
931 return NULL;
932
933 /* extension type and length */
934 s2n(TLSEXT_TYPE_server_name,ret);
935 s2n(size_str+5,ret);
936
937 /* length of servername list */
938 s2n(size_str+3,ret);
939
940 /* hostname type, length and hostname */
941 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
942 s2n(size_str,ret);
943 memcpy(ret, s->tlsext_hostname, size_str);
944 ret+=size_str;
945 }
946
947 /* Add RI if renegotiating */
948 if (s->renegotiate)
949 {
950 int el;
951
952 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
953 {
954 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
955 return NULL;
956 }
957
Adam Langleyb0c235e2014-06-20 12:00:00 -0700958 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700959
960 s2n(TLSEXT_TYPE_renegotiate,ret);
961 s2n(el,ret);
962
963 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
964 {
965 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
966 return NULL;
967 }
968
969 ret += el;
970 }
971
Adam Langley75712922014-10-10 16:23:43 -0700972 /* Add extended master secret. */
973 if (s->version != SSL3_VERSION)
974 {
975 if (limit - ret - 4 < 0)
976 return NULL;
977 s2n(TLSEXT_TYPE_extended_master_secret,ret);
978 s2n(0,ret);
979 }
980
Adam Langley95c29f32014-06-20 12:00:00 -0700981 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
982 {
983 int ticklen;
984 if (!s->new_session && s->session && s->session->tlsext_tick)
985 ticklen = s->session->tlsext_ticklen;
986 else if (s->session && s->tlsext_session_ticket &&
987 s->tlsext_session_ticket->data)
988 {
David Benjamin072c9532014-07-26 11:44:25 -0400989 s->session->tlsext_tick = BUF_memdup(
990 s->tlsext_session_ticket->data,
991 s->tlsext_session_ticket->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700992 if (!s->session->tlsext_tick)
993 return NULL;
David Benjamin072c9532014-07-26 11:44:25 -0400994 ticklen = s->tlsext_session_ticket->length;
Adam Langley95c29f32014-06-20 12:00:00 -0700995 s->session->tlsext_ticklen = ticklen;
996 }
997 else
998 ticklen = 0;
999 if (ticklen == 0 && s->tlsext_session_ticket &&
1000 s->tlsext_session_ticket->data == NULL)
1001 goto skip_ext;
1002 /* Check for enough room 2 for extension type, 2 for len
1003 * rest for ticket
1004 */
1005 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1006 s2n(TLSEXT_TYPE_session_ticket,ret);
1007 s2n(ticklen,ret);
1008 if (ticklen)
1009 {
1010 memcpy(ret, s->session->tlsext_tick, ticklen);
1011 ret += ticklen;
1012 }
1013 }
1014 skip_ext:
1015
1016 if (SSL_USE_SIGALGS(s))
1017 {
1018 size_t salglen;
1019 const unsigned char *salg;
1020 salglen = tls12_get_psigalgs(s, &salg);
1021 if ((size_t)(limit - ret) < salglen + 6)
1022 return NULL;
1023 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1024 s2n(salglen + 2, ret);
1025 s2n(salglen, ret);
1026 memcpy(ret, salg, salglen);
1027 ret += salglen;
1028 }
1029
David Benjamin6c7aed02014-08-27 16:42:38 -04001030 if (s->ocsp_stapling_enabled)
Adam Langley95c29f32014-06-20 12:00:00 -07001031 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001032 /* The status_request extension is excessively extensible at
1033 * every layer. On the client, only support requesting OCSP
1034 * responses with an empty responder_id_list and no
1035 * extensions. */
1036 if (limit - ret - 4 - 1 - 2 - 2 < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001037
Adam Langley95c29f32014-06-20 12:00:00 -07001038 s2n(TLSEXT_TYPE_status_request, ret);
David Benjamin6c7aed02014-08-27 16:42:38 -04001039 s2n(1 + 2 + 2, ret);
1040 /* status_type */
Adam Langley95c29f32014-06-20 12:00:00 -07001041 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
David Benjamin6c7aed02014-08-27 16:42:38 -04001042 /* responder_id_list - empty */
1043 s2n(0, ret);
1044 /* request_extensions - empty */
1045 s2n(0, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001046 }
Adam Langley95c29f32014-06-20 12:00:00 -07001047
Adam Langley95c29f32014-06-20 12:00:00 -07001048 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1049 {
1050 /* The client advertises an emtpy extension to indicate its
1051 * support for Next Protocol Negotiation */
1052 if (limit - ret - 4 < 0)
1053 return NULL;
1054 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1055 s2n(0,ret);
1056 }
Adam Langley95c29f32014-06-20 12:00:00 -07001057
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001058 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len)
1059 {
1060 /* The client advertises an empty extension to indicate its support for
1061 * certificate timestamps. */
1062 if (limit - ret - 4 < 0)
1063 return NULL;
1064 s2n(TLSEXT_TYPE_certificate_timestamp,ret);
1065 s2n(0,ret);
1066 }
1067
Adam Langley95c29f32014-06-20 12:00:00 -07001068 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1069 {
1070 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1071 return NULL;
1072 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1073 s2n(2 + s->alpn_client_proto_list_len,ret);
1074 s2n(s->alpn_client_proto_list_len,ret);
1075 memcpy(ret, s->alpn_client_proto_list,
1076 s->alpn_client_proto_list_len);
1077 ret += s->alpn_client_proto_list_len;
1078 }
1079
Adam Langley1258b6a2014-06-20 12:00:00 -07001080 if (s->tlsext_channel_id_enabled)
1081 {
1082 /* The client advertises an emtpy extension to indicate its
1083 * support for Channel ID. */
1084 if (limit - ret - 4 < 0)
1085 return NULL;
1086 if (s->ctx->tlsext_channel_id_enabled_new)
1087 s2n(TLSEXT_TYPE_channel_id_new,ret);
1088 else
1089 s2n(TLSEXT_TYPE_channel_id,ret);
1090 s2n(0,ret);
1091 }
1092
Adam Langley95c29f32014-06-20 12:00:00 -07001093 if(SSL_get_srtp_profiles(s))
1094 {
1095 int el;
1096
1097 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1098
Adam Langleyb0c235e2014-06-20 12:00:00 -07001099 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001100
1101 s2n(TLSEXT_TYPE_use_srtp,ret);
1102 s2n(el,ret);
1103
David Benjamin120a6742014-08-30 14:54:37 -04001104 if(!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001105 {
1106 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1107 return NULL;
1108 }
1109 ret += el;
1110 }
1111
Adam Langleyc3174b72014-06-20 12:00:00 -07001112 if (using_ecc)
1113 {
1114 /* Add TLS extension ECPointFormats to the ClientHello message */
1115 long lenmax;
David Benjamin072334d2014-07-13 16:24:27 -04001116 const uint8_t *formats;
1117 const uint16_t *curves;
1118 size_t formats_len, curves_len, i;
Adam Langleyc3174b72014-06-20 12:00:00 -07001119
David Benjamin072334d2014-07-13 16:24:27 -04001120 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001121
1122 if ((lenmax = limit - ret - 5) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001123 if (formats_len > (size_t)lenmax) return NULL;
1124 if (formats_len > 255)
Adam Langleyc3174b72014-06-20 12:00:00 -07001125 {
1126 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1127 return NULL;
1128 }
1129
1130 s2n(TLSEXT_TYPE_ec_point_formats,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001131 s2n(formats_len + 1,ret);
1132 *(ret++) = (unsigned char)formats_len;
1133 memcpy(ret, formats, formats_len);
1134 ret+=formats_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001135
1136 /* Add TLS extension EllipticCurves to the ClientHello message */
David Benjamin072334d2014-07-13 16:24:27 -04001137 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001138
1139 if ((lenmax = limit - ret - 6) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001140 if ((curves_len * 2) > (size_t)lenmax) return NULL;
1141 if ((curves_len * 2) > 65532)
Adam Langleyc3174b72014-06-20 12:00:00 -07001142 {
1143 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1144 return NULL;
1145 }
1146
1147 s2n(TLSEXT_TYPE_elliptic_curves,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001148 s2n((curves_len * 2) + 2, ret);
Adam Langleyc3174b72014-06-20 12:00:00 -07001149
1150 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1151 * elliptic_curve_list, but the examples use two bytes.
1152 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1153 * resolves this to two bytes.
1154 */
David Benjamin072334d2014-07-13 16:24:27 -04001155 s2n(curves_len * 2, ret);
1156 for (i = 0; i < curves_len; i++)
1157 {
1158 s2n(curves[i], ret);
1159 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001160 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001161
Adam Langley95c29f32014-06-20 12:00:00 -07001162#ifdef TLSEXT_TYPE_padding
1163 /* Add padding to workaround bugs in F5 terminators.
Adam Langleyb0c235e2014-06-20 12:00:00 -07001164 * See https://tools.ietf.org/html/draft-agl-tls-padding-03
Adam Langley95c29f32014-06-20 12:00:00 -07001165 *
1166 * NB: because this code works out the length of all existing
Adam Langleyb0c235e2014-06-20 12:00:00 -07001167 * extensions it MUST always appear last. */
1168 if (header_len > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001169 {
Adam Langleyb0c235e2014-06-20 12:00:00 -07001170 header_len += ret - orig;
1171 if (header_len > 0xff && header_len < 0x200)
1172 {
1173 size_t padding_len = 0x200 - header_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001174 /* Extensions take at least four bytes to encode. Always
1175 * include least one byte of data if including the
1176 * extension. WebSphere Application Server 7.0 is
1177 * intolerant to the last extension being zero-length. */
1178 if (padding_len >= 4 + 1)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001179 padding_len -= 4;
1180 else
Adam Langleyc3174b72014-06-20 12:00:00 -07001181 padding_len = 1;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001182 if (limit - ret - 4 - (long)padding_len < 0)
1183 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001184
Adam Langleyb0c235e2014-06-20 12:00:00 -07001185 s2n(TLSEXT_TYPE_padding, ret);
1186 s2n(padding_len, ret);
1187 memset(ret, 0, padding_len);
1188 ret += padding_len;
1189 }
Adam Langley95c29f32014-06-20 12:00:00 -07001190 }
1191#endif
1192
Adam Langleyb0c235e2014-06-20 12:00:00 -07001193 if ((extdatalen = ret-orig-2)== 0)
1194 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001195
Adam Langleyb0c235e2014-06-20 12:00:00 -07001196 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001197 return ret;
1198 }
1199
Adam Langleyb0c235e2014-06-20 12:00:00 -07001200unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit)
Adam Langley95c29f32014-06-20 12:00:00 -07001201 {
1202 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001203 unsigned char *orig = buf;
1204 unsigned char *ret = buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001205 int next_proto_neg_seen;
Adam Langley95c29f32014-06-20 12:00:00 -07001206 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1207 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -04001208 int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
David Benjamina19fc252014-10-19 00:14:36 -04001209 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001210 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1211 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001212 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001213
1214 ret+=2;
1215 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1216
Adam Langleyed8270a2014-09-02 13:52:56 -07001217 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001218 {
1219 if ((long)(limit - ret - 4) < 0) return NULL;
1220
1221 s2n(TLSEXT_TYPE_server_name,ret);
1222 s2n(0,ret);
1223 }
1224
1225 if(s->s3->send_connection_binding)
1226 {
1227 int el;
1228
1229 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1230 {
1231 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1232 return NULL;
1233 }
1234
Adam Langleyb0c235e2014-06-20 12:00:00 -07001235 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001236
1237 s2n(TLSEXT_TYPE_renegotiate,ret);
1238 s2n(el,ret);
1239
1240 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1241 {
1242 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1243 return NULL;
1244 }
1245
1246 ret += el;
1247 }
1248
Adam Langley75712922014-10-10 16:23:43 -07001249 if (s->s3->tmp.extended_master_secret)
1250 {
1251 if ((long)(limit - ret - 4) < 0) return NULL;
1252
1253 s2n(TLSEXT_TYPE_extended_master_secret,ret);
1254 s2n(0,ret);
1255 }
1256
Adam Langley95c29f32014-06-20 12:00:00 -07001257 if (using_ecc)
1258 {
1259 const unsigned char *plist;
1260 size_t plistlen;
1261 /* Add TLS extension ECPointFormats to the ServerHello message */
1262 long lenmax;
1263
1264 tls1_get_formatlist(s, &plist, &plistlen);
1265
1266 if ((lenmax = limit - ret - 5) < 0) return NULL;
1267 if (plistlen > (size_t)lenmax) return NULL;
1268 if (plistlen > 255)
1269 {
1270 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1271 return NULL;
1272 }
1273
1274 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1275 s2n(plistlen + 1,ret);
1276 *(ret++) = (unsigned char) plistlen;
1277 memcpy(ret, plist, plistlen);
1278 ret+=plistlen;
1279
1280 }
1281 /* Currently the server should not respond with a SupportedCurves extension */
Adam Langley95c29f32014-06-20 12:00:00 -07001282
1283 if (s->tlsext_ticket_expected
1284 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1285 {
1286 if ((long)(limit - ret - 4) < 0) return NULL;
1287 s2n(TLSEXT_TYPE_session_ticket,ret);
1288 s2n(0,ret);
1289 }
1290
David Benjamin6c7aed02014-08-27 16:42:38 -04001291 if (s->s3->tmp.certificate_status_expected)
Adam Langley95c29f32014-06-20 12:00:00 -07001292 {
1293 if ((long)(limit - ret - 4) < 0) return NULL;
1294 s2n(TLSEXT_TYPE_status_request,ret);
1295 s2n(0,ret);
1296 }
1297
Adam Langley95c29f32014-06-20 12:00:00 -07001298 if(s->srtp_profile)
1299 {
1300 int el;
1301
1302 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1303
Adam Langleyb0c235e2014-06-20 12:00:00 -07001304 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001305
1306 s2n(TLSEXT_TYPE_use_srtp,ret);
1307 s2n(el,ret);
1308
David Benjamin120a6742014-08-30 14:54:37 -04001309 if(!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001310 {
1311 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1312 return NULL;
1313 }
1314 ret+=el;
1315 }
1316
Adam Langley95c29f32014-06-20 12:00:00 -07001317 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1318 s->s3->next_proto_neg_seen = 0;
1319 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1320 {
1321 const unsigned char *npa;
1322 unsigned int npalen;
1323 int r;
1324
1325 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1326 if (r == SSL_TLSEXT_ERR_OK)
1327 {
1328 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1329 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1330 s2n(npalen,ret);
1331 memcpy(ret, npa, npalen);
1332 ret += npalen;
1333 s->s3->next_proto_neg_seen = 1;
1334 }
1335 }
Adam Langley95c29f32014-06-20 12:00:00 -07001336
Adam Langley95c29f32014-06-20 12:00:00 -07001337 if (s->s3->alpn_selected)
1338 {
David Benjamin03973092014-06-24 23:27:17 -04001339 const uint8_t *selected = s->s3->alpn_selected;
1340 size_t len = s->s3->alpn_selected_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001341
1342 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1343 return NULL;
1344 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1345 s2n(3 + len,ret);
1346 s2n(1 + len,ret);
1347 *ret++ = len;
1348 memcpy(ret, selected, len);
1349 ret += len;
1350 }
1351
Adam Langley1258b6a2014-06-20 12:00:00 -07001352 /* If the client advertised support for Channel ID, and we have it
1353 * enabled, then we want to echo it back. */
1354 if (s->s3->tlsext_channel_id_valid)
1355 {
1356 if (limit - ret - 4 < 0)
1357 return NULL;
1358 if (s->s3->tlsext_channel_id_new)
1359 s2n(TLSEXT_TYPE_channel_id_new,ret);
1360 else
1361 s2n(TLSEXT_TYPE_channel_id,ret);
1362 s2n(0,ret);
1363 }
1364
Adam Langleyb0c235e2014-06-20 12:00:00 -07001365 if ((extdatalen = ret-orig-2) == 0)
1366 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001367
Adam Langleyb0c235e2014-06-20 12:00:00 -07001368 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001369 return ret;
1370 }
1371
Adam Langley95c29f32014-06-20 12:00:00 -07001372/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1373 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001374 * cbs: the contents of the extension, not including the type and length.
1375 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001376 * return.
1377 *
David Benjamindc72ff72014-06-25 12:36:10 -04001378 * returns: 1 on success. */
1379static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001380 {
Adam Langleyded93582014-07-31 15:23:51 -07001381 CBS protocol_name_list, protocol_name_list_copy;
Adam Langley95c29f32014-06-20 12:00:00 -07001382 const unsigned char *selected;
1383 unsigned char selected_len;
1384 int r;
1385
1386 if (s->ctx->alpn_select_cb == NULL)
David Benjamindc72ff72014-06-25 12:36:10 -04001387 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001388
David Benjamindc72ff72014-06-25 12:36:10 -04001389 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1390 CBS_len(cbs) != 0 ||
1391 CBS_len(&protocol_name_list) < 2)
Adam Langley95c29f32014-06-20 12:00:00 -07001392 goto parse_error;
1393
David Benjamindc72ff72014-06-25 12:36:10 -04001394 /* Validate the protocol list. */
Adam Langleyded93582014-07-31 15:23:51 -07001395 protocol_name_list_copy = protocol_name_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001396 while (CBS_len(&protocol_name_list_copy) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001397 {
David Benjamindc72ff72014-06-25 12:36:10 -04001398 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001399
David Benjamindc72ff72014-06-25 12:36:10 -04001400 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001401 goto parse_error;
Adam Langley95c29f32014-06-20 12:00:00 -07001402 }
1403
David Benjamindc72ff72014-06-25 12:36:10 -04001404 r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1405 CBS_data(&protocol_name_list), CBS_len(&protocol_name_list),
1406 s->ctx->alpn_select_cb_arg);
Adam Langley95c29f32014-06-20 12:00:00 -07001407 if (r == SSL_TLSEXT_ERR_OK) {
1408 if (s->s3->alpn_selected)
1409 OPENSSL_free(s->s3->alpn_selected);
David Benjamin072c9532014-07-26 11:44:25 -04001410 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001411 if (!s->s3->alpn_selected)
1412 {
David Benjamindc72ff72014-06-25 12:36:10 -04001413 *out_alert = SSL_AD_INTERNAL_ERROR;
1414 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001415 }
Adam Langley95c29f32014-06-20 12:00:00 -07001416 s->s3->alpn_selected_len = selected_len;
1417 }
David Benjamindc72ff72014-06-25 12:36:10 -04001418 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001419
1420parse_error:
David Benjamindc72ff72014-06-25 12:36:10 -04001421 *out_alert = SSL_AD_DECODE_ERROR;
1422 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001423 }
1424
David Benjamindc72ff72014-06-25 12:36:10 -04001425static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001426 {
Adam Langley95c29f32014-06-20 12:00:00 -07001427 int renegotiate_seen = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001428 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001429 size_t i;
1430
Adam Langleyed8270a2014-09-02 13:52:56 -07001431 s->should_ack_sni = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001432 s->s3->next_proto_neg_seen = 0;
David Benjamin6c7aed02014-08-27 16:42:38 -04001433 s->s3->tmp.certificate_status_expected = 0;
Adam Langley75712922014-10-10 16:23:43 -07001434 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001435
Adam Langley95c29f32014-06-20 12:00:00 -07001436 if (s->s3->alpn_selected)
1437 {
1438 OPENSSL_free(s->s3->alpn_selected);
1439 s->s3->alpn_selected = NULL;
1440 }
1441
Adam Langley95c29f32014-06-20 12:00:00 -07001442 /* Clear any signature algorithms extension received */
1443 if (s->cert->peer_sigalgs)
1444 {
1445 OPENSSL_free(s->cert->peer_sigalgs);
1446 s->cert->peer_sigalgs = NULL;
1447 }
David Benjamina19fc252014-10-19 00:14:36 -04001448 /* Clear any shared signature algorithms */
Adam Langley95c29f32014-06-20 12:00:00 -07001449 if (s->cert->shared_sigalgs)
1450 {
1451 OPENSSL_free(s->cert->shared_sigalgs);
1452 s->cert->shared_sigalgs = NULL;
1453 }
David Benjamin033e5f42014-11-13 18:47:41 -05001454 /* Clear certificate digests. */
Adam Langley95c29f32014-06-20 12:00:00 -07001455 for (i = 0; i < SSL_PKEY_NUM; i++)
1456 {
1457 s->cert->pkeys[i].digest = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001458 }
David Benjamina19fc252014-10-19 00:14:36 -04001459 /* Clear ECC extensions */
1460 if (s->s3->tmp.peer_ecpointformatlist != 0)
1461 {
1462 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1463 s->s3->tmp.peer_ecpointformatlist = NULL;
1464 s->s3->tmp.peer_ecpointformatlist_length = 0;
1465 }
1466 if (s->s3->tmp.peer_ellipticcurvelist != 0)
1467 {
1468 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1469 s->s3->tmp.peer_ellipticcurvelist = NULL;
1470 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1471 }
Adam Langley95c29f32014-06-20 12:00:00 -07001472
David Benjamindc72ff72014-06-25 12:36:10 -04001473 /* There may be no extensions. */
1474 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001475 {
David Benjamindc72ff72014-06-25 12:36:10 -04001476 goto ri_check;
1477 }
Adam Langley95c29f32014-06-20 12:00:00 -07001478
David Benjamin35a7a442014-07-05 00:23:20 -04001479 /* Decode the extensions block and check it is valid. */
1480 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1481 !tls1_check_duplicate_extensions(&extensions))
David Benjamindc72ff72014-06-25 12:36:10 -04001482 {
1483 *out_alert = SSL_AD_DECODE_ERROR;
1484 return 0;
1485 }
1486
David Benjamindc72ff72014-06-25 12:36:10 -04001487 while (CBS_len(&extensions) != 0)
1488 {
1489 uint16_t type;
1490 CBS extension;
1491
1492 /* Decode the next extension. */
1493 if (!CBS_get_u16(&extensions, &type) ||
1494 !CBS_get_u16_length_prefixed(&extensions, &extension))
1495 {
1496 *out_alert = SSL_AD_DECODE_ERROR;
1497 return 0;
1498 }
1499
Adam Langley95c29f32014-06-20 12:00:00 -07001500 if (s->tlsext_debug_cb)
David Benjamindc72ff72014-06-25 12:36:10 -04001501 {
1502 s->tlsext_debug_cb(s, 0, type, (unsigned char*)CBS_data(&extension),
1503 CBS_len(&extension), s->tlsext_debug_arg);
1504 }
1505
Adam Langley95c29f32014-06-20 12:00:00 -07001506/* The servername extension is treated as follows:
1507
1508 - Only the hostname type is supported with a maximum length of 255.
1509 - The servername is rejected if too long or if it contains zeros,
1510 in which case an fatal alert is generated.
1511 - The servername field is maintained together with the session cache.
1512 - When a session is resumed, the servername call back invoked in order
1513 to allow the application to position itself to the right context.
1514 - The servername is acknowledged if it is new for a session or when
1515 it is identical to a previously used for the same session.
1516 Applications can control the behaviour. They can at any time
1517 set a 'desirable' servername for a new SSL object. This can be the
1518 case for example with HTTPS when a Host: header field is received and
1519 a renegotiation is requested. In this case, a possible servername
1520 presented in the new client hello is only acknowledged if it matches
1521 the value of the Host: field.
1522 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1523 if they provide for changing an explicit servername context for the session,
1524 i.e. when the session has been established with a servername extension.
1525 - On session reconnect, the servername extension may be absent.
1526
1527*/
1528
1529 if (type == TLSEXT_TYPE_server_name)
1530 {
David Benjamindc72ff72014-06-25 12:36:10 -04001531 CBS server_name_list;
Adam Langleyed8270a2014-09-02 13:52:56 -07001532 char have_seen_host_name = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001533
1534 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1535 CBS_len(&server_name_list) < 1 ||
1536 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001537 {
David Benjamindc72ff72014-06-25 12:36:10 -04001538 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001539 return 0;
1540 }
Adam Langley95c29f32014-06-20 12:00:00 -07001541
David Benjamindc72ff72014-06-25 12:36:10 -04001542 /* Decode each ServerName in the extension. */
1543 while (CBS_len(&server_name_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001544 {
David Benjamindc72ff72014-06-25 12:36:10 -04001545 uint8_t name_type;
1546 CBS host_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001547
David Benjamindc72ff72014-06-25 12:36:10 -04001548 /* Decode the NameType. */
1549 if (!CBS_get_u8(&server_name_list, &name_type))
Adam Langley95c29f32014-06-20 12:00:00 -07001550 {
David Benjamindc72ff72014-06-25 12:36:10 -04001551 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001552 return 0;
1553 }
David Benjamindc72ff72014-06-25 12:36:10 -04001554
David Benjamindc72ff72014-06-25 12:36:10 -04001555 /* Only host_name is supported. */
1556 if (name_type != TLSEXT_NAMETYPE_host_name)
1557 continue;
1558
Adam Langleyed8270a2014-09-02 13:52:56 -07001559 if (have_seen_host_name)
1560 {
1561 /* The ServerNameList MUST NOT contain
1562 * more than one name of the same
1563 * name_type. */
1564 *out_alert = SSL_AD_DECODE_ERROR;
1565 return 0;
1566 }
1567
1568 have_seen_host_name = 1;
1569
1570 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1571 CBS_len(&host_name) < 1)
1572 {
1573 *out_alert = SSL_AD_DECODE_ERROR;
1574 return 0;
1575 }
1576
1577 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1578 CBS_contains_zero_byte(&host_name))
1579 {
1580 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1581 return 0;
1582 }
1583
David Benjamindc72ff72014-06-25 12:36:10 -04001584 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -07001585 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001586 assert(s->session->tlsext_hostname == NULL);
David Benjamindc72ff72014-06-25 12:36:10 -04001587 if (s->session->tlsext_hostname)
Adam Langley95c29f32014-06-20 12:00:00 -07001588 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001589 /* This should be impossible. */
David Benjamindc72ff72014-06-25 12:36:10 -04001590 *out_alert = SSL_AD_DECODE_ERROR;
1591 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001592 }
Adam Langley95c29f32014-06-20 12:00:00 -07001593
David Benjamindc72ff72014-06-25 12:36:10 -04001594 /* Copy the hostname as a string. */
David Benjamined439582014-07-14 19:13:02 -04001595 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname))
David Benjamindc72ff72014-06-25 12:36:10 -04001596 {
1597 *out_alert = SSL_AD_INTERNAL_ERROR;
1598 return 0;
1599 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001600
1601 s->should_ack_sni = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001602 }
Adam Langley95c29f32014-06-20 12:00:00 -07001603 }
Adam Langley95c29f32014-06-20 12:00:00 -07001604 }
1605
Adam Langley95c29f32014-06-20 12:00:00 -07001606 else if (type == TLSEXT_TYPE_ec_point_formats)
1607 {
David Benjamindc72ff72014-06-25 12:36:10 -04001608 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001609
David Benjamindc72ff72014-06-25 12:36:10 -04001610 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1611 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001612 {
David Benjamindc72ff72014-06-25 12:36:10 -04001613 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001614 return 0;
1615 }
David Benjamindc72ff72014-06-25 12:36:10 -04001616
David Benjamina19fc252014-10-19 00:14:36 -04001617 if (!CBS_stow(&ec_point_format_list,
1618 &s->s3->tmp.peer_ecpointformatlist,
1619 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001620 {
David Benjamina19fc252014-10-19 00:14:36 -04001621 *out_alert = SSL_AD_INTERNAL_ERROR;
1622 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001623 }
Adam Langley95c29f32014-06-20 12:00:00 -07001624 }
1625 else if (type == TLSEXT_TYPE_elliptic_curves)
1626 {
David Benjamindc72ff72014-06-25 12:36:10 -04001627 CBS elliptic_curve_list;
David Benjamin072334d2014-07-13 16:24:27 -04001628 size_t i, num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001629
David Benjamindc72ff72014-06-25 12:36:10 -04001630 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
David Benjamin072334d2014-07-13 16:24:27 -04001631 CBS_len(&elliptic_curve_list) == 0 ||
1632 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
David Benjamindc72ff72014-06-25 12:36:10 -04001633 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001634 {
David Benjamindc72ff72014-06-25 12:36:10 -04001635 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001636 return 0;
1637 }
David Benjamindc72ff72014-06-25 12:36:10 -04001638
David Benjamina19fc252014-10-19 00:14:36 -04001639 if (s->s3->tmp.peer_ellipticcurvelist)
Adam Langley95c29f32014-06-20 12:00:00 -07001640 {
David Benjamina19fc252014-10-19 00:14:36 -04001641 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1642 s->s3->tmp.peer_ellipticcurvelist_length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001643 }
David Benjamina19fc252014-10-19 00:14:36 -04001644 s->s3->tmp.peer_ellipticcurvelist =
1645 (uint16_t*)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
1646 if (s->s3->tmp.peer_ellipticcurvelist == NULL)
1647 {
1648 *out_alert = SSL_AD_INTERNAL_ERROR;
1649 return 0;
1650 }
1651 num_curves = CBS_len(&elliptic_curve_list) / 2;
1652 for (i = 0; i < num_curves; i++)
1653 {
1654 if (!CBS_get_u16(&elliptic_curve_list,
1655 &s->s3->tmp.peer_ellipticcurvelist[i]))
1656 {
1657 *out_alert = SSL_AD_INTERNAL_ERROR;
1658 return 0;
1659 }
1660 }
1661 if (CBS_len(&elliptic_curve_list) != 0)
1662 {
1663 *out_alert = SSL_AD_INTERNAL_ERROR;
1664 return 0;
1665 }
1666 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001667 }
Adam Langley95c29f32014-06-20 12:00:00 -07001668 else if (type == TLSEXT_TYPE_session_ticket)
1669 {
1670 if (s->tls_session_ticket_ext_cb &&
David Benjamindc72ff72014-06-25 12:36:10 -04001671 !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 -07001672 {
David Benjamindc72ff72014-06-25 12:36:10 -04001673 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001674 return 0;
1675 }
1676 }
1677 else if (type == TLSEXT_TYPE_renegotiate)
1678 {
David Benjamindc72ff72014-06-25 12:36:10 -04001679 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001680 return 0;
1681 renegotiate_seen = 1;
1682 }
1683 else if (type == TLSEXT_TYPE_signature_algorithms)
1684 {
David Benjamindc72ff72014-06-25 12:36:10 -04001685 CBS supported_signature_algorithms;
1686
David Benjamindc72ff72014-06-25 12:36:10 -04001687 if (!CBS_get_u16_length_prefixed(&extension, &supported_signature_algorithms) ||
1688 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001689 {
David Benjamindc72ff72014-06-25 12:36:10 -04001690 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001691 return 0;
1692 }
David Benjamindc72ff72014-06-25 12:36:10 -04001693
1694 /* Ensure the signature algorithms are non-empty. It
1695 * contains a list of SignatureAndHashAlgorithms
1696 * which are two bytes each. */
1697 if (CBS_len(&supported_signature_algorithms) == 0 ||
1698 (CBS_len(&supported_signature_algorithms) % 2) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001699 {
David Benjamindc72ff72014-06-25 12:36:10 -04001700 *out_alert = SSL_AD_DECODE_ERROR;
1701 return 0;
1702 }
1703
David Benjamincd996942014-07-20 16:23:51 -04001704 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
David Benjamindc72ff72014-06-25 12:36:10 -04001705 {
1706 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001707 return 0;
1708 }
1709 /* If sigalgs received and no shared algorithms fatal
1710 * error.
1711 */
1712 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
1713 {
1714 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
David Benjamindc72ff72014-06-25 12:36:10 -04001715 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07001716 return 0;
1717 }
1718 }
1719
Adam Langley95c29f32014-06-20 12:00:00 -07001720 else if (type == TLSEXT_TYPE_next_proto_neg &&
1721 s->s3->tmp.finish_md_len == 0 &&
1722 s->s3->alpn_selected == NULL)
1723 {
David Benjamindc72ff72014-06-25 12:36:10 -04001724 /* The extension must be empty. */
1725 if (CBS_len(&extension) != 0)
1726 {
1727 *out_alert = SSL_AD_DECODE_ERROR;
1728 return 0;
1729 }
1730
Adam Langley95c29f32014-06-20 12:00:00 -07001731 /* We shouldn't accept this extension on a
1732 * renegotiation.
1733 *
1734 * s->new_session will be set on renegotiation, but we
1735 * probably shouldn't rely that it couldn't be set on
1736 * the initial renegotation too in certain cases (when
1737 * there's some other reason to disallow resuming an
1738 * earlier session -- the current code won't be doing
1739 * anything like that, but this might change).
1740
1741 * A valid sign that there's been a previous handshake
1742 * in this connection is if s->s3->tmp.finish_md_len >
1743 * 0. (We are talking about a check that will happen
1744 * in the Hello protocol round, well before a new
1745 * Finished message could have been computed.) */
1746 s->s3->next_proto_neg_seen = 1;
1747 }
Adam Langley95c29f32014-06-20 12:00:00 -07001748
1749 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1750 s->ctx->alpn_select_cb &&
1751 s->s3->tmp.finish_md_len == 0)
1752 {
David Benjamindc72ff72014-06-25 12:36:10 -04001753 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001754 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001755 /* ALPN takes precedence over NPN. */
1756 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001757 }
1758
Adam Langley1258b6a2014-06-20 12:00:00 -07001759 else if (type == TLSEXT_TYPE_channel_id &&
1760 s->tlsext_channel_id_enabled)
David Benjamindc72ff72014-06-25 12:36:10 -04001761 {
1762 /* The extension must be empty. */
1763 if (CBS_len(&extension) != 0)
1764 {
1765 *out_alert = SSL_AD_DECODE_ERROR;
1766 return 0;
1767 }
1768
Adam Langley1258b6a2014-06-20 12:00:00 -07001769 s->s3->tlsext_channel_id_valid = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001770 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001771
1772 else if (type == TLSEXT_TYPE_channel_id_new &&
1773 s->tlsext_channel_id_enabled)
1774 {
David Benjamindc72ff72014-06-25 12:36:10 -04001775 /* The extension must be empty. */
1776 if (CBS_len(&extension) != 0)
1777 {
1778 *out_alert = SSL_AD_DECODE_ERROR;
1779 return 0;
1780 }
1781
Adam Langley1258b6a2014-06-20 12:00:00 -07001782 s->s3->tlsext_channel_id_valid = 1;
1783 s->s3->tlsext_channel_id_new = 1;
1784 }
1785
1786
Adam Langley95c29f32014-06-20 12:00:00 -07001787 /* session ticket processed earlier */
1788 else if (type == TLSEXT_TYPE_use_srtp)
1789 {
David Benjamindc72ff72014-06-25 12:36:10 -04001790 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001791 return 0;
1792 }
Adam Langley75712922014-10-10 16:23:43 -07001793
1794 else if (type == TLSEXT_TYPE_extended_master_secret &&
1795 s->version != SSL3_VERSION)
1796 {
1797 if (CBS_len(&extension) != 0)
1798 {
1799 *out_alert = SSL_AD_DECODE_ERROR;
1800 return 0;
1801 }
1802
1803 s->s3->tmp.extended_master_secret = 1;
1804 }
Adam Langley95c29f32014-06-20 12:00:00 -07001805 }
1806
Adam Langley95c29f32014-06-20 12:00:00 -07001807 ri_check:
1808
1809 /* Need RI if renegotiating */
1810
1811 if (!renegotiate_seen && s->renegotiate &&
1812 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1813 {
David Benjamindc72ff72014-06-25 12:36:10 -04001814 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin172fc2c2014-09-06 13:09:47 -04001815 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07001816 return 0;
1817 }
1818 /* If no signature algorithms extension set default values */
1819 if (!s->cert->peer_sigalgs)
1820 ssl_cert_set_default_md(s->cert);
1821
1822 return 1;
1823 }
1824
David Benjamindc72ff72014-06-25 12:36:10 -04001825int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001826 {
David Benjamindc72ff72014-06-25 12:36:10 -04001827 int alert = -1;
1828 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001829 {
David Benjamindc72ff72014-06-25 12:36:10 -04001830 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07001831 return 0;
1832 }
1833
David Benjamin6c7aed02014-08-27 16:42:38 -04001834 if (ssl_check_clienthello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001835 {
David Benjamin9d28c752014-07-05 00:43:48 -04001836 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07001837 return 0;
1838 }
1839 return 1;
David Benjamin072334d2014-07-13 16:24:27 -04001840 }
Adam Langley95c29f32014-06-20 12:00:00 -07001841
Adam Langley95c29f32014-06-20 12:00:00 -07001842/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1843 * elements of zero length are allowed and the set of elements must exactly fill
1844 * the length of the block. */
David Benjamin03973092014-06-24 23:27:17 -04001845static char ssl_next_proto_validate(const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001846 {
David Benjamin03973092014-06-24 23:27:17 -04001847 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001848
David Benjamin03973092014-06-24 23:27:17 -04001849 while (CBS_len(&copy) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001850 {
David Benjamin03973092014-06-24 23:27:17 -04001851 CBS proto;
1852 if (!CBS_get_u8_length_prefixed(&copy, &proto) ||
1853 CBS_len(&proto) == 0)
1854 {
Adam Langley95c29f32014-06-20 12:00:00 -07001855 return 0;
David Benjamin03973092014-06-24 23:27:17 -04001856 }
Adam Langley95c29f32014-06-20 12:00:00 -07001857 }
David Benjamin03973092014-06-24 23:27:17 -04001858 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001859 }
Adam Langley95c29f32014-06-20 12:00:00 -07001860
David Benjamin03973092014-06-24 23:27:17 -04001861static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001862 {
Adam Langley95c29f32014-06-20 12:00:00 -07001863 int tlsext_servername = 0;
1864 int renegotiate_seen = 0;
David Benjamin03973092014-06-24 23:27:17 -04001865 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001866
David Benjamin6c7aed02014-08-27 16:42:38 -04001867 /* TODO(davidben): Move all of these to some per-handshake state that
1868 * gets systematically reset on a new handshake; perhaps allocate it
1869 * fresh each time so it's not even kept around post-handshake. */
Adam Langley95c29f32014-06-20 12:00:00 -07001870 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001871
David Benjamin6c7aed02014-08-27 16:42:38 -04001872 s->tlsext_ticket_expected = 0;
1873 s->s3->tmp.certificate_status_expected = 0;
Adam Langley75712922014-10-10 16:23:43 -07001874 s->s3->tmp.extended_master_secret = 0;
David Benjamin64442872014-07-21 17:43:45 -04001875
Adam Langley95c29f32014-06-20 12:00:00 -07001876 if (s->s3->alpn_selected)
1877 {
1878 OPENSSL_free(s->s3->alpn_selected);
1879 s->s3->alpn_selected = NULL;
1880 }
1881
David Benjamina19fc252014-10-19 00:14:36 -04001882 /* Clear ECC extensions */
1883 if (s->s3->tmp.peer_ecpointformatlist != 0)
1884 {
1885 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1886 s->s3->tmp.peer_ecpointformatlist = NULL;
1887 s->s3->tmp.peer_ecpointformatlist_length = 0;
1888 }
1889
David Benjamin03973092014-06-24 23:27:17 -04001890 /* There may be no extensions. */
1891 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001892 {
David Benjamin03973092014-06-24 23:27:17 -04001893 goto ri_check;
1894 }
1895
David Benjamin35a7a442014-07-05 00:23:20 -04001896 /* Decode the extensions block and check it is valid. */
1897 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1898 !tls1_check_duplicate_extensions(&extensions))
David Benjamin03973092014-06-24 23:27:17 -04001899 {
1900 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001901 return 0;
1902 }
1903
David Benjamin03973092014-06-24 23:27:17 -04001904 while (CBS_len(&extensions) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001905 {
David Benjamin03973092014-06-24 23:27:17 -04001906 uint16_t type;
1907 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001908
David Benjamin03973092014-06-24 23:27:17 -04001909 /* Decode the next extension. */
1910 if (!CBS_get_u16(&extensions, &type) ||
1911 !CBS_get_u16_length_prefixed(&extensions, &extension))
1912 {
1913 *out_alert = SSL_AD_DECODE_ERROR;
1914 return 0;
1915 }
Adam Langley95c29f32014-06-20 12:00:00 -07001916
1917 if (s->tlsext_debug_cb)
David Benjamin03973092014-06-24 23:27:17 -04001918 {
1919 s->tlsext_debug_cb(s, 1, type, (unsigned char*)CBS_data(&extension),
1920 CBS_len(&extension), s->tlsext_debug_arg);
1921 }
Adam Langley95c29f32014-06-20 12:00:00 -07001922
1923 if (type == TLSEXT_TYPE_server_name)
1924 {
David Benjamin03973092014-06-24 23:27:17 -04001925 /* The extension must be empty. */
1926 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001927 {
David Benjamin03973092014-06-24 23:27:17 -04001928 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001929 return 0;
1930 }
David Benjamin03973092014-06-24 23:27:17 -04001931 /* We must have sent it in ClientHello. */
1932 if (s->tlsext_hostname == NULL)
1933 {
1934 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1935 return 0;
1936 }
1937 tlsext_servername = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001938 }
Adam Langley95c29f32014-06-20 12:00:00 -07001939 else if (type == TLSEXT_TYPE_ec_point_formats)
1940 {
David Benjamin03973092014-06-24 23:27:17 -04001941 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001942
David Benjamin03973092014-06-24 23:27:17 -04001943 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1944 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001945 {
David Benjamin03973092014-06-24 23:27:17 -04001946 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001947 return 0;
1948 }
David Benjamin03973092014-06-24 23:27:17 -04001949
David Benjamina19fc252014-10-19 00:14:36 -04001950 if (!CBS_stow(&ec_point_format_list,
1951 &s->s3->tmp.peer_ecpointformatlist,
1952 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001953 {
David Benjamina19fc252014-10-19 00:14:36 -04001954 *out_alert = SSL_AD_INTERNAL_ERROR;
1955 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001956 }
Adam Langley95c29f32014-06-20 12:00:00 -07001957 }
Adam Langley95c29f32014-06-20 12:00:00 -07001958 else if (type == TLSEXT_TYPE_session_ticket)
1959 {
1960 if (s->tls_session_ticket_ext_cb &&
David Benjamin03973092014-06-24 23:27:17 -04001961 !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension),
1962 s->tls_session_ticket_ext_cb_arg))
Adam Langley95c29f32014-06-20 12:00:00 -07001963 {
David Benjamin03973092014-06-24 23:27:17 -04001964 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001965 return 0;
1966 }
David Benjamin03973092014-06-24 23:27:17 -04001967
1968 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001969 {
David Benjamin03973092014-06-24 23:27:17 -04001970 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07001971 return 0;
1972 }
David Benjamin03973092014-06-24 23:27:17 -04001973
Adam Langley95c29f32014-06-20 12:00:00 -07001974 s->tlsext_ticket_expected = 1;
1975 }
Adam Langley95c29f32014-06-20 12:00:00 -07001976 else if (type == TLSEXT_TYPE_status_request)
1977 {
David Benjamin03973092014-06-24 23:27:17 -04001978 /* The extension MUST be empty and may only sent if
1979 * we've requested a status request message. */
1980 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001981 {
David Benjamin03973092014-06-24 23:27:17 -04001982 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001983 return 0;
1984 }
David Benjamin6c7aed02014-08-27 16:42:38 -04001985 if (!s->ocsp_stapling_enabled)
David Benjamin03973092014-06-24 23:27:17 -04001986 {
1987 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1988 return 0;
1989 }
1990 /* Set a flag to expect a CertificateStatus message */
David Benjamin6c7aed02014-08-27 16:42:38 -04001991 s->s3->tmp.certificate_status_expected = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001992 }
David Benjamin03973092014-06-24 23:27:17 -04001993 else if (type == TLSEXT_TYPE_next_proto_neg && s->s3->tmp.finish_md_len == 0) {
1994 unsigned char *selected;
1995 unsigned char selected_len;
1996
1997 /* We must have requested it. */
1998 if (s->ctx->next_proto_select_cb == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001999 {
David Benjamin03973092014-06-24 23:27:17 -04002000 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2001 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002002 }
Adam Langley95c29f32014-06-20 12:00:00 -07002003
David Benjamin03973092014-06-24 23:27:17 -04002004 /* The data must be valid. */
2005 if (!ssl_next_proto_validate(&extension))
2006 {
2007 *out_alert = SSL_AD_DECODE_ERROR;
2008 return 0;
2009 }
2010
2011 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len,
2012 CBS_data(&extension), CBS_len(&extension),
2013 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
2014 {
2015 *out_alert = SSL_AD_INTERNAL_ERROR;
2016 return 0;
2017 }
2018
2019 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
2020 if (s->next_proto_negotiated == NULL)
2021 {
2022 *out_alert = SSL_AD_INTERNAL_ERROR;
2023 return 0;
2024 }
2025 s->next_proto_negotiated_len = selected_len;
2026 s->s3->next_proto_neg_seen = 1;
2027 }
Adam Langley95c29f32014-06-20 12:00:00 -07002028 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
2029 {
David Benjamin03973092014-06-24 23:27:17 -04002030 CBS protocol_name_list, protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07002031
2032 /* We must have requested it. */
2033 if (s->alpn_client_proto_list == NULL)
2034 {
David Benjamin03973092014-06-24 23:27:17 -04002035 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07002036 return 0;
2037 }
David Benjamin03973092014-06-24 23:27:17 -04002038
2039 /* The extension data consists of a ProtocolNameList
2040 * which must have exactly one ProtocolName. Each of
2041 * these is length-prefixed. */
2042 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
2043 CBS_len(&extension) != 0 ||
2044 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
2045 CBS_len(&protocol_name_list) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002046 {
David Benjamin03973092014-06-24 23:27:17 -04002047 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002048 return 0;
2049 }
David Benjamin03973092014-06-24 23:27:17 -04002050
2051 if (!CBS_stow(&protocol_name,
2052 &s->s3->alpn_selected,
2053 &s->s3->alpn_selected_len))
Adam Langley95c29f32014-06-20 12:00:00 -07002054 {
David Benjamin03973092014-06-24 23:27:17 -04002055 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002056 return 0;
2057 }
Adam Langley95c29f32014-06-20 12:00:00 -07002058 }
2059
Adam Langley1258b6a2014-06-20 12:00:00 -07002060 else if (type == TLSEXT_TYPE_channel_id)
David Benjamin03973092014-06-24 23:27:17 -04002061 {
2062 if (CBS_len(&extension) != 0)
2063 {
2064 *out_alert = SSL_AD_DECODE_ERROR;
2065 return 0;
2066 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002067 s->s3->tlsext_channel_id_valid = 1;
David Benjamin03973092014-06-24 23:27:17 -04002068 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002069 else if (type == TLSEXT_TYPE_channel_id_new)
2070 {
David Benjamin03973092014-06-24 23:27:17 -04002071 if (CBS_len(&extension) != 0)
2072 {
2073 *out_alert = SSL_AD_DECODE_ERROR;
2074 return 0;
2075 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002076 s->s3->tlsext_channel_id_valid = 1;
2077 s->s3->tlsext_channel_id_new = 1;
2078 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002079 else if (type == TLSEXT_TYPE_certificate_timestamp)
2080 {
2081 if (CBS_len(&extension) == 0)
2082 {
2083 *out_alert = SSL_AD_DECODE_ERROR;
2084 return 0;
2085 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002086
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002087 /* Session resumption uses the original session information. */
2088 if (!s->hit)
2089 {
2090 if (!CBS_stow(&extension,
2091 &s->session->tlsext_signed_cert_timestamp_list,
2092 &s->session->tlsext_signed_cert_timestamp_list_length))
2093 {
2094 *out_alert = SSL_AD_INTERNAL_ERROR;
2095 return 0;
2096 }
2097 }
2098 }
Adam Langley95c29f32014-06-20 12:00:00 -07002099 else if (type == TLSEXT_TYPE_renegotiate)
2100 {
David Benjamin03973092014-06-24 23:27:17 -04002101 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002102 return 0;
2103 renegotiate_seen = 1;
2104 }
Adam Langley95c29f32014-06-20 12:00:00 -07002105 else if (type == TLSEXT_TYPE_use_srtp)
2106 {
David Benjamin03973092014-06-24 23:27:17 -04002107 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002108 return 0;
2109 }
Adam Langley75712922014-10-10 16:23:43 -07002110
2111 else if (type == TLSEXT_TYPE_extended_master_secret)
2112 {
2113 if (/* It is invalid for the server to select EMS and
2114 SSLv3. */
2115 s->version == SSL3_VERSION ||
2116 CBS_len(&extension) != 0)
2117 {
2118 *out_alert = SSL_AD_DECODE_ERROR;
2119 return 0;
2120 }
2121
2122 s->s3->tmp.extended_master_secret = 1;
2123 }
Adam Langley95c29f32014-06-20 12:00:00 -07002124 }
2125
2126 if (!s->hit && tlsext_servername == 1)
2127 {
2128 if (s->tlsext_hostname)
2129 {
2130 if (s->session->tlsext_hostname == NULL)
2131 {
2132 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2133 if (!s->session->tlsext_hostname)
2134 {
David Benjamin03973092014-06-24 23:27:17 -04002135 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002136 return 0;
2137 }
2138 }
2139 else
2140 {
David Benjamin03973092014-06-24 23:27:17 -04002141 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002142 return 0;
2143 }
2144 }
2145 }
2146
Adam Langley95c29f32014-06-20 12:00:00 -07002147 ri_check:
2148
2149 /* Determine if we need to see RI. Strictly speaking if we want to
2150 * avoid an attack we should *always* see RI even on initial server
2151 * hello because the client doesn't see any renegotiation during an
2152 * attack. However this would mean we could not connect to any server
2153 * which doesn't support RI so for the immediate future tolerate RI
2154 * absence on initial connect only.
2155 */
2156 if (!renegotiate_seen
2157 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2158 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2159 {
David Benjamin03973092014-06-24 23:27:17 -04002160 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin9d28c752014-07-05 00:43:48 -04002161 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07002162 return 0;
2163 }
2164
2165 return 1;
2166 }
2167
2168
2169int ssl_prepare_clienthello_tlsext(SSL *s)
2170 {
Adam Langley95c29f32014-06-20 12:00:00 -07002171 return 1;
2172 }
2173
2174int ssl_prepare_serverhello_tlsext(SSL *s)
2175 {
2176 return 1;
2177 }
2178
David Benjamin6c7aed02014-08-27 16:42:38 -04002179static int ssl_check_clienthello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002180 {
2181 int ret=SSL_TLSEXT_ERR_NOACK;
2182 int al = SSL_AD_UNRECOGNIZED_NAME;
2183
Adam Langley95c29f32014-06-20 12:00:00 -07002184 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2185 * ssl3_choose_cipher in s3_lib.c.
2186 */
2187 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2188 * ssl3_choose_cipher in s3_lib.c.
2189 */
Adam Langley95c29f32014-06-20 12:00:00 -07002190
2191 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2192 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2193 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2194 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2195
Adam Langley95c29f32014-06-20 12:00:00 -07002196 switch (ret)
2197 {
2198 case SSL_TLSEXT_ERR_ALERT_FATAL:
2199 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2200 return -1;
2201
2202 case SSL_TLSEXT_ERR_ALERT_WARNING:
2203 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002204 return 1;
2205
Adam Langley95c29f32014-06-20 12:00:00 -07002206 case SSL_TLSEXT_ERR_NOACK:
Adam Langleyed8270a2014-09-02 13:52:56 -07002207 s->should_ack_sni = 0;
2208 return 1;
2209
2210 default:
2211 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002212 }
2213 }
2214
David Benjamin6c7aed02014-08-27 16:42:38 -04002215static int ssl_check_serverhello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002216 {
2217 int ret=SSL_TLSEXT_ERR_NOACK;
2218 int al = SSL_AD_UNRECOGNIZED_NAME;
2219
Adam Langley95c29f32014-06-20 12:00:00 -07002220 /* If we are client and using an elliptic curve cryptography cipher
2221 * suite, then if server returns an EC point formats lists extension
2222 * it must contain uncompressed.
2223 */
2224 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2225 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamina9ca90a2014-09-26 18:53:43 -04002226 if (((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) &&
2227 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed))
Adam Langley95c29f32014-06-20 12:00:00 -07002228 {
David Benjamina9ca90a2014-09-26 18:53:43 -04002229 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2230 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002231 }
2232 ret = SSL_TLSEXT_ERR_OK;
Adam Langley95c29f32014-06-20 12:00:00 -07002233
2234 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2235 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2236 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2237 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2238
Adam Langley95c29f32014-06-20 12:00:00 -07002239 switch (ret)
2240 {
2241 case SSL_TLSEXT_ERR_ALERT_FATAL:
Adam Langleyed8270a2014-09-02 13:52:56 -07002242 ssl3_send_alert(s,SSL3_AL_FATAL,al);
Adam Langley95c29f32014-06-20 12:00:00 -07002243 return -1;
2244
2245 case SSL_TLSEXT_ERR_ALERT_WARNING:
2246 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002247 return 1;
2248
2249 default:
2250 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002251 }
2252 }
2253
David Benjamin03973092014-06-24 23:27:17 -04002254int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07002255 {
David Benjamin03973092014-06-24 23:27:17 -04002256 int alert = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002257 if (s->version < SSL3_VERSION)
2258 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002259
2260 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002261 {
David Benjamin03973092014-06-24 23:27:17 -04002262 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07002263 return 0;
2264 }
2265
David Benjamin03973092014-06-24 23:27:17 -04002266 if (ssl_check_serverhello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002267 {
David Benjamin9d28c752014-07-05 00:43:48 -04002268 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, SSL_R_SERVERHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07002269 return 0;
2270 }
David Benjamin03973092014-06-24 23:27:17 -04002271
Adam Langley95c29f32014-06-20 12:00:00 -07002272 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002273 }
Adam Langley95c29f32014-06-20 12:00:00 -07002274
2275/* Since the server cache lookup is done early on in the processing of the
2276 * ClientHello, and other operations depend on the result, we need to handle
2277 * any TLS session ticket extension at the same time.
2278 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002279 * ctx: contains the early callback context, which is the result of a
2280 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002281 * ret: (output) on return, if a ticket was decrypted, then this is set to
2282 * point to the resulting session.
2283 *
2284 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2285 * ciphersuite, in which case we have no use for session tickets and one will
2286 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2287 *
2288 * Returns:
2289 * -1: fatal error, either from parsing or decrypting the ticket.
2290 * 0: no ticket was found (or was ignored, based on settings).
2291 * 1: a zero length extension was found, indicating that the client supports
2292 * session tickets but doesn't currently have one to offer.
2293 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2294 * couldn't be decrypted because of a non-fatal error.
2295 * 3: a ticket was successfully decrypted and *ret was set.
2296 *
2297 * Side effects:
2298 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2299 * a new session ticket to the client because the client indicated support
2300 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2301 * a session ticket or we couldn't use the one it gave us, or if
2302 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2303 * Otherwise, s->tlsext_ticket_expected is set to 0.
2304 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002305int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
2306 SSL_SESSION **ret)
Adam Langley95c29f32014-06-20 12:00:00 -07002307 {
Adam Langley95c29f32014-06-20 12:00:00 -07002308 *ret = NULL;
2309 s->tlsext_ticket_expected = 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002310 const unsigned char *data;
2311 size_t len;
2312 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002313
2314 /* If tickets disabled behave as if no ticket present
2315 * to permit stateful resumption.
2316 */
2317 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2318 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002319 if ((s->version <= SSL3_VERSION) && !ctx->extensions)
Adam Langley95c29f32014-06-20 12:00:00 -07002320 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002321 if (!SSL_early_callback_ctx_extension_get(
2322 ctx, TLSEXT_TYPE_session_ticket, &data, &len))
Adam Langley95c29f32014-06-20 12:00:00 -07002323 {
Adam Langleydc9b1412014-06-20 12:00:00 -07002324 return 0;
2325 }
2326 if (len == 0)
2327 {
2328 /* The client will accept a ticket but doesn't
2329 * currently have one. */
2330 s->tlsext_ticket_expected = 1;
2331 return 1;
2332 }
2333 if (s->tls_session_secret_cb)
2334 {
2335 /* Indicate that the ticket couldn't be
2336 * decrypted rather than generating the session
2337 * from ticket now, trigger abbreviated
2338 * handshake based on external mechanism to
2339 * calculate the master secret later. */
2340 return 2;
2341 }
2342 r = tls_decrypt_ticket(s, data, len, ctx->session_id,
2343 ctx->session_id_len, ret);
2344 switch (r)
2345 {
2346 case 2: /* ticket couldn't be decrypted */
2347 s->tlsext_ticket_expected = 1;
2348 return 2;
2349 case 3: /* ticket was decrypted */
2350 return r;
2351 case 4: /* ticket decrypted but need to renew */
2352 s->tlsext_ticket_expected = 1;
2353 return 3;
2354 default: /* fatal error */
Adam Langley95c29f32014-06-20 12:00:00 -07002355 return -1;
2356 }
Adam Langley95c29f32014-06-20 12:00:00 -07002357 }
2358
2359/* tls_decrypt_ticket attempts to decrypt a session ticket.
2360 *
2361 * etick: points to the body of the session ticket extension.
2362 * eticklen: the length of the session tickets extenion.
2363 * sess_id: points at the session ID.
2364 * sesslen: the length of the session ID.
2365 * psess: (output) on return, if a ticket was decrypted, then this is set to
2366 * point to the resulting session.
2367 *
2368 * Returns:
2369 * -1: fatal error, either from parsing or decrypting the ticket.
2370 * 2: the ticket couldn't be decrypted.
2371 * 3: a ticket was successfully decrypted and *psess was set.
2372 * 4: same as 3, but the ticket needs to be renewed.
2373 */
2374static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
2375 const unsigned char *sess_id, int sesslen,
2376 SSL_SESSION **psess)
2377 {
2378 SSL_SESSION *sess;
2379 unsigned char *sdec;
2380 const unsigned char *p;
2381 int slen, mlen, renew_ticket = 0;
2382 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2383 HMAC_CTX hctx;
2384 EVP_CIPHER_CTX ctx;
2385 SSL_CTX *tctx = s->initial_ctx;
2386 /* Need at least keyname + iv + some encrypted data */
2387 if (eticklen < 48)
2388 return 2;
2389 /* Initialize session ticket encryption and HMAC contexts */
2390 HMAC_CTX_init(&hctx);
2391 EVP_CIPHER_CTX_init(&ctx);
2392 if (tctx->tlsext_ticket_key_cb)
2393 {
2394 unsigned char *nctick = (unsigned char *)etick;
2395 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
2396 &ctx, &hctx, 0);
2397 if (rv < 0)
2398 return -1;
2399 if (rv == 0)
2400 return 2;
2401 if (rv == 2)
2402 renew_ticket = 1;
2403 }
2404 else
2405 {
2406 /* Check key name matches */
2407 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
2408 return 2;
2409 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2410 tlsext_tick_md(), NULL);
2411 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2412 tctx->tlsext_tick_aes_key, etick + 16);
2413 }
2414 /* Attempt to process session ticket, first conduct sanity and
2415 * integrity checks on ticket.
2416 */
2417 mlen = HMAC_size(&hctx);
2418 if (mlen < 0)
2419 {
2420 EVP_CIPHER_CTX_cleanup(&ctx);
2421 return -1;
2422 }
2423 eticklen -= mlen;
2424 /* Check HMAC of encrypted ticket */
2425 HMAC_Update(&hctx, etick, eticklen);
2426 HMAC_Final(&hctx, tick_hmac, NULL);
2427 HMAC_CTX_cleanup(&hctx);
2428 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
Adam Langley38311732014-10-16 19:04:35 -07002429 {
2430 EVP_CIPHER_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002431 return 2;
Adam Langley38311732014-10-16 19:04:35 -07002432 }
Adam Langley95c29f32014-06-20 12:00:00 -07002433 /* Attempt to decrypt session data */
2434 /* Move p after IV to start of encrypted ticket, update length */
2435 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2436 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2437 sdec = OPENSSL_malloc(eticklen);
2438 if (!sdec)
2439 {
2440 EVP_CIPHER_CTX_cleanup(&ctx);
2441 return -1;
2442 }
2443 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2444 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0)
Adam Langley3e148852014-07-24 17:34:02 -07002445 {
2446 EVP_CIPHER_CTX_cleanup(&ctx);
2447 OPENSSL_free(sdec);
Adam Langley95c29f32014-06-20 12:00:00 -07002448 return 2;
Adam Langley3e148852014-07-24 17:34:02 -07002449 }
Adam Langley95c29f32014-06-20 12:00:00 -07002450 slen += mlen;
2451 EVP_CIPHER_CTX_cleanup(&ctx);
2452 p = sdec;
2453
2454 sess = d2i_SSL_SESSION(NULL, &p, slen);
2455 OPENSSL_free(sdec);
2456 if (sess)
2457 {
2458 /* The session ID, if non-empty, is used by some clients to
2459 * detect that the ticket has been accepted. So we copy it to
2460 * the session structure. If it is empty set length to zero
2461 * as required by standard.
2462 */
2463 if (sesslen)
2464 memcpy(sess->session_id, sess_id, sesslen);
2465 sess->session_id_length = sesslen;
2466 *psess = sess;
2467 if (renew_ticket)
2468 return 4;
2469 else
2470 return 3;
2471 }
2472 ERR_clear_error();
2473 /* For session parse failure, indicate that we need to send a new
2474 * ticket. */
2475 return 2;
2476 }
2477
2478/* Tables to translate from NIDs to TLS v1.2 ids */
2479
2480typedef struct
2481 {
2482 int nid;
2483 int id;
2484 } tls12_lookup;
2485
David Benjamincff64722014-08-19 19:54:46 -04002486static const tls12_lookup tls12_md[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002487 {NID_md5, TLSEXT_hash_md5},
2488 {NID_sha1, TLSEXT_hash_sha1},
2489 {NID_sha224, TLSEXT_hash_sha224},
2490 {NID_sha256, TLSEXT_hash_sha256},
2491 {NID_sha384, TLSEXT_hash_sha384},
2492 {NID_sha512, TLSEXT_hash_sha512}
2493};
2494
David Benjamincff64722014-08-19 19:54:46 -04002495static const tls12_lookup tls12_sig[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002496 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
Adam Langley95c29f32014-06-20 12:00:00 -07002497 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
2498};
2499
David Benjamincff64722014-08-19 19:54:46 -04002500static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002501 {
2502 size_t i;
2503 for (i = 0; i < tlen; i++)
2504 {
2505 if (table[i].nid == nid)
2506 return table[i].id;
2507 }
2508 return -1;
2509 }
2510
David Benjamincff64722014-08-19 19:54:46 -04002511static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002512 {
2513 size_t i;
2514 for (i = 0; i < tlen; i++)
2515 {
2516 if ((table[i].id) == id)
2517 return table[i].nid;
2518 }
2519 return NID_undef;
2520 }
2521
2522int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
2523 {
2524 int sig_id, md_id;
2525 if (!md)
2526 return 0;
2527 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2528 sizeof(tls12_md)/sizeof(tls12_lookup));
2529 if (md_id == -1)
2530 return 0;
2531 sig_id = tls12_get_sigid(pk);
2532 if (sig_id == -1)
2533 return 0;
2534 p[0] = (unsigned char)md_id;
2535 p[1] = (unsigned char)sig_id;
2536 return 1;
2537 }
2538
2539int tls12_get_sigid(const EVP_PKEY *pk)
2540 {
2541 return tls12_find_id(pk->type, tls12_sig,
2542 sizeof(tls12_sig)/sizeof(tls12_lookup));
2543 }
2544
2545const EVP_MD *tls12_get_hash(unsigned char hash_alg)
2546 {
2547 switch(hash_alg)
2548 {
Adam Langley95c29f32014-06-20 12:00:00 -07002549 case TLSEXT_hash_md5:
Adam Langley95c29f32014-06-20 12:00:00 -07002550 return EVP_md5();
Adam Langley95c29f32014-06-20 12:00:00 -07002551 case TLSEXT_hash_sha1:
2552 return EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002553 case TLSEXT_hash_sha224:
2554 return EVP_sha224();
2555
2556 case TLSEXT_hash_sha256:
2557 return EVP_sha256();
Adam Langley95c29f32014-06-20 12:00:00 -07002558 case TLSEXT_hash_sha384:
2559 return EVP_sha384();
2560
2561 case TLSEXT_hash_sha512:
2562 return EVP_sha512();
Adam Langley95c29f32014-06-20 12:00:00 -07002563 default:
2564 return NULL;
2565
2566 }
2567 }
2568
2569static int tls12_get_pkey_idx(unsigned char sig_alg)
2570 {
2571 switch(sig_alg)
2572 {
Adam Langley95c29f32014-06-20 12:00:00 -07002573 case TLSEXT_signature_rsa:
2574 return SSL_PKEY_RSA_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002575 case TLSEXT_signature_ecdsa:
2576 return SSL_PKEY_ECC;
Adam Langley95c29f32014-06-20 12:00:00 -07002577 }
2578 return -1;
2579 }
2580
2581/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2582static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
2583 int *psignhash_nid, const unsigned char *data)
2584 {
2585 int sign_nid = 0, hash_nid = 0;
2586 if (!phash_nid && !psign_nid && !psignhash_nid)
2587 return;
2588 if (phash_nid || psignhash_nid)
2589 {
2590 hash_nid = tls12_find_nid(data[0], tls12_md,
2591 sizeof(tls12_md)/sizeof(tls12_lookup));
2592 if (phash_nid)
2593 *phash_nid = hash_nid;
2594 }
2595 if (psign_nid || psignhash_nid)
2596 {
2597 sign_nid = tls12_find_nid(data[1], tls12_sig,
2598 sizeof(tls12_sig)/sizeof(tls12_lookup));
2599 if (psign_nid)
2600 *psign_nid = sign_nid;
2601 }
2602 if (psignhash_nid)
2603 {
2604 if (sign_nid && hash_nid)
2605 OBJ_find_sigid_by_algs(psignhash_nid,
2606 hash_nid, sign_nid);
2607 else
2608 *psignhash_nid = NID_undef;
2609 }
2610 }
2611/* Given preference and allowed sigalgs set shared sigalgs */
2612static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
2613 const unsigned char *pref, size_t preflen,
2614 const unsigned char *allow, size_t allowlen)
2615 {
2616 const unsigned char *ptmp, *atmp;
2617 size_t i, j, nmatch = 0;
2618 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
2619 {
2620 /* Skip disabled hashes or signature algorithms */
2621 if (tls12_get_hash(ptmp[0]) == NULL)
2622 continue;
2623 if (tls12_get_pkey_idx(ptmp[1]) == -1)
2624 continue;
2625 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
2626 {
2627 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
2628 {
2629 nmatch++;
2630 if (shsig)
2631 {
2632 shsig->rhash = ptmp[0];
2633 shsig->rsign = ptmp[1];
2634 tls1_lookup_sigalg(&shsig->hash_nid,
2635 &shsig->sign_nid,
2636 &shsig->signandhash_nid,
2637 ptmp);
2638 shsig++;
2639 }
2640 break;
2641 }
2642 }
2643 }
2644 return nmatch;
2645 }
2646
2647/* Set shared signature algorithms for SSL structures */
2648static int tls1_set_shared_sigalgs(SSL *s)
2649 {
2650 const unsigned char *pref, *allow, *conf;
2651 size_t preflen, allowlen, conflen;
2652 size_t nmatch;
2653 TLS_SIGALGS *salgs = NULL;
2654 CERT *c = s->cert;
Adam Langleydb4f9522014-06-20 12:00:00 -07002655 if (c->shared_sigalgs)
2656 {
2657 OPENSSL_free(c->shared_sigalgs);
2658 c->shared_sigalgs = NULL;
2659 }
Adam Langley95c29f32014-06-20 12:00:00 -07002660 /* If client use client signature algorithms if not NULL */
David Benjamin335d10d2014-08-06 19:56:33 -04002661 if (!s->server && c->client_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002662 {
2663 conf = c->client_sigalgs;
2664 conflen = c->client_sigalgslen;
2665 }
David Benjamin335d10d2014-08-06 19:56:33 -04002666 else if (c->conf_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002667 {
2668 conf = c->conf_sigalgs;
2669 conflen = c->conf_sigalgslen;
2670 }
2671 else
2672 conflen = tls12_get_psigalgs(s, &conf);
David Benjamin335d10d2014-08-06 19:56:33 -04002673 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
Adam Langley95c29f32014-06-20 12:00:00 -07002674 {
2675 pref = conf;
2676 preflen = conflen;
2677 allow = c->peer_sigalgs;
2678 allowlen = c->peer_sigalgslen;
2679 }
2680 else
2681 {
2682 allow = conf;
2683 allowlen = conflen;
2684 pref = c->peer_sigalgs;
2685 preflen = c->peer_sigalgslen;
2686 }
2687 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2688 if (!nmatch)
2689 return 1;
2690 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2691 if (!salgs)
2692 return 0;
2693 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2694 c->shared_sigalgs = salgs;
2695 c->shared_sigalgslen = nmatch;
2696 return 1;
2697 }
2698
2699
2700/* Set preferred digest for each key type */
2701
David Benjamincd996942014-07-20 16:23:51 -04002702int tls1_process_sigalgs(SSL *s, const CBS *sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002703 {
2704 int idx;
2705 size_t i;
2706 const EVP_MD *md;
2707 CERT *c = s->cert;
2708 TLS_SIGALGS *sigptr;
David Benjamincd996942014-07-20 16:23:51 -04002709
Adam Langley95c29f32014-06-20 12:00:00 -07002710 /* Extension ignored for inappropriate versions */
2711 if (!SSL_USE_SIGALGS(s))
2712 return 1;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002713 /* Length must be even */
David Benjamincd996942014-07-20 16:23:51 -04002714 if (CBS_len(sigalgs) % 2 != 0)
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002715 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002716 /* Should never happen */
2717 if (!c)
2718 return 0;
2719
David Benjamincd996942014-07-20 16:23:51 -04002720 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen))
Adam Langley95c29f32014-06-20 12:00:00 -07002721 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002722
2723 tls1_set_shared_sigalgs(s);
2724
Adam Langley95c29f32014-06-20 12:00:00 -07002725 for (i = 0, sigptr = c->shared_sigalgs;
2726 i < c->shared_sigalgslen; i++, sigptr++)
2727 {
2728 idx = tls12_get_pkey_idx(sigptr->rsign);
2729 if (idx > 0 && c->pkeys[idx].digest == NULL)
2730 {
2731 md = tls12_get_hash(sigptr->rhash);
2732 c->pkeys[idx].digest = md;
Adam Langley95c29f32014-06-20 12:00:00 -07002733 if (idx == SSL_PKEY_RSA_SIGN)
2734 {
Adam Langley95c29f32014-06-20 12:00:00 -07002735 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2736 }
2737 }
2738
2739 }
David Benjamin253b3e72014-11-13 15:53:52 -05002740
2741 /* Set any remaining keys to default values. NOTE: if alg is
2742 * not supported it stays as NULL.
Adam Langley95c29f32014-06-20 12:00:00 -07002743 */
David Benjamin253b3e72014-11-13 15:53:52 -05002744 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
Adam Langley95c29f32014-06-20 12:00:00 -07002745 {
David Benjamin253b3e72014-11-13 15:53:52 -05002746 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
2747 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002748 }
David Benjamin253b3e72014-11-13 15:53:52 -05002749 if (!c->pkeys[SSL_PKEY_ECC].digest)
2750 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002751 return 1;
2752 }
2753
2754
2755int SSL_get_sigalgs(SSL *s, int idx,
2756 int *psign, int *phash, int *psignhash,
2757 unsigned char *rsig, unsigned char *rhash)
2758 {
2759 const unsigned char *psig = s->cert->peer_sigalgs;
2760 if (psig == NULL)
2761 return 0;
2762 if (idx >= 0)
2763 {
2764 idx <<= 1;
2765 if (idx >= (int)s->cert->peer_sigalgslen)
2766 return 0;
2767 psig += idx;
2768 if (rhash)
2769 *rhash = psig[0];
2770 if (rsig)
2771 *rsig = psig[1];
2772 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2773 }
2774 return s->cert->peer_sigalgslen / 2;
2775 }
2776
2777int SSL_get_shared_sigalgs(SSL *s, int idx,
2778 int *psign, int *phash, int *psignhash,
2779 unsigned char *rsig, unsigned char *rhash)
2780 {
2781 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
2782 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
2783 return 0;
2784 shsigalgs += idx;
2785 if (phash)
2786 *phash = shsigalgs->hash_nid;
2787 if (psign)
2788 *psign = shsigalgs->sign_nid;
2789 if (psignhash)
2790 *psignhash = shsigalgs->signandhash_nid;
2791 if (rsig)
2792 *rsig = shsigalgs->rsign;
2793 if (rhash)
2794 *rhash = shsigalgs->rhash;
2795 return s->cert->shared_sigalgslen;
2796 }
2797
Adam Langley1258b6a2014-06-20 12:00:00 -07002798/* tls1_channel_id_hash calculates the signed data for a Channel ID on the given
2799 * SSL connection and writes it to |md|. */
2800int
2801tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s)
2802 {
2803 EVP_MD_CTX ctx;
2804 unsigned char temp_digest[EVP_MAX_MD_SIZE];
2805 unsigned temp_digest_len;
2806 int i;
2807 static const char kClientIDMagic[] = "TLS Channel ID signature";
2808
2809 if (s->s3->handshake_buffer)
Adam Langley75712922014-10-10 16:23:43 -07002810 if (!ssl3_digest_cached_records(s, free_handshake_buffer))
Adam Langley1258b6a2014-06-20 12:00:00 -07002811 return 0;
2812
2813 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2814
2815 if (s->hit && s->s3->tlsext_channel_id_new)
2816 {
2817 static const char kResumptionMagic[] = "Resumption";
2818 EVP_DigestUpdate(md, kResumptionMagic,
2819 sizeof(kResumptionMagic));
2820 if (s->session->original_handshake_hash_len == 0)
2821 return 0;
2822 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2823 s->session->original_handshake_hash_len);
2824 }
2825
2826 EVP_MD_CTX_init(&ctx);
2827 for (i = 0; i < SSL_MAX_DIGEST; i++)
2828 {
2829 if (s->s3->handshake_dgst[i] == NULL)
2830 continue;
2831 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
2832 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2833 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2834 }
2835 EVP_MD_CTX_cleanup(&ctx);
2836
2837 return 1;
2838 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002839
2840/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2841 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
2842int tls1_record_handshake_hashes_for_channel_id(SSL *s)
2843 {
2844 int digest_len;
2845 /* This function should never be called for a resumed session because
2846 * the handshake hashes that we wish to record are for the original,
2847 * full handshake. */
2848 if (s->hit)
2849 return -1;
2850 /* It only makes sense to call this function if Channel IDs have been
2851 * negotiated. */
2852 if (!s->s3->tlsext_channel_id_new)
2853 return -1;
2854
2855 digest_len = tls1_handshake_digest(
2856 s, s->session->original_handshake_hash,
2857 sizeof(s->session->original_handshake_hash));
2858 if (digest_len < 0)
2859 return -1;
2860
2861 s->session->original_handshake_hash_len = digest_len;
2862
2863 return 1;
2864 }
2865
Adam Langley95c29f32014-06-20 12:00:00 -07002866int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
2867 {
2868 unsigned char *sigalgs, *sptr;
2869 int rhash, rsign;
2870 size_t i;
2871 if (salglen & 1)
2872 return 0;
2873 sigalgs = OPENSSL_malloc(salglen);
2874 if (sigalgs == NULL)
2875 return 0;
2876 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
2877 {
2878 rhash = tls12_find_id(*psig_nids++, tls12_md,
2879 sizeof(tls12_md)/sizeof(tls12_lookup));
2880 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2881 sizeof(tls12_sig)/sizeof(tls12_lookup));
2882
2883 if (rhash == -1 || rsign == -1)
2884 goto err;
2885 *sptr++ = rhash;
2886 *sptr++ = rsign;
2887 }
2888
2889 if (client)
2890 {
2891 if (c->client_sigalgs)
2892 OPENSSL_free(c->client_sigalgs);
2893 c->client_sigalgs = sigalgs;
2894 c->client_sigalgslen = salglen;
2895 }
2896 else
2897 {
2898 if (c->conf_sigalgs)
2899 OPENSSL_free(c->conf_sigalgs);
2900 c->conf_sigalgs = sigalgs;
2901 c->conf_sigalgslen = salglen;
2902 }
2903
2904 return 1;
2905
2906 err:
2907 OPENSSL_free(sigalgs);
2908 return 0;
2909 }
2910