blob: ffd80d8137335cadca28885eaeb1e1cede67a5f0 [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
Adam Langleyed8270a2014-09-02 13:52:56 -07001430 s->should_ack_sni = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001431 s->s3->next_proto_neg_seen = 0;
David Benjamin6c7aed02014-08-27 16:42:38 -04001432 s->s3->tmp.certificate_status_expected = 0;
Adam Langley75712922014-10-10 16:23:43 -07001433 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001434
Adam Langley95c29f32014-06-20 12:00:00 -07001435 if (s->s3->alpn_selected)
1436 {
1437 OPENSSL_free(s->s3->alpn_selected);
1438 s->s3->alpn_selected = NULL;
1439 }
1440
Adam Langley95c29f32014-06-20 12:00:00 -07001441 /* Clear any signature algorithms extension received */
1442 if (s->cert->peer_sigalgs)
1443 {
1444 OPENSSL_free(s->cert->peer_sigalgs);
1445 s->cert->peer_sigalgs = NULL;
1446 }
David Benjamina19fc252014-10-19 00:14:36 -04001447 /* Clear any shared signature algorithms */
Adam Langley95c29f32014-06-20 12:00:00 -07001448 if (s->cert->shared_sigalgs)
1449 {
1450 OPENSSL_free(s->cert->shared_sigalgs);
1451 s->cert->shared_sigalgs = NULL;
1452 }
David Benjamina19fc252014-10-19 00:14:36 -04001453 /* Clear ECC extensions */
1454 if (s->s3->tmp.peer_ecpointformatlist != 0)
1455 {
1456 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1457 s->s3->tmp.peer_ecpointformatlist = NULL;
1458 s->s3->tmp.peer_ecpointformatlist_length = 0;
1459 }
1460 if (s->s3->tmp.peer_ellipticcurvelist != 0)
1461 {
1462 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1463 s->s3->tmp.peer_ellipticcurvelist = NULL;
1464 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1465 }
Adam Langley95c29f32014-06-20 12:00:00 -07001466
David Benjamindc72ff72014-06-25 12:36:10 -04001467 /* There may be no extensions. */
1468 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001469 {
David Benjamindc72ff72014-06-25 12:36:10 -04001470 goto ri_check;
1471 }
Adam Langley95c29f32014-06-20 12:00:00 -07001472
David Benjamin35a7a442014-07-05 00:23:20 -04001473 /* Decode the extensions block and check it is valid. */
1474 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1475 !tls1_check_duplicate_extensions(&extensions))
David Benjamindc72ff72014-06-25 12:36:10 -04001476 {
1477 *out_alert = SSL_AD_DECODE_ERROR;
1478 return 0;
1479 }
1480
David Benjamindc72ff72014-06-25 12:36:10 -04001481 while (CBS_len(&extensions) != 0)
1482 {
1483 uint16_t type;
1484 CBS extension;
1485
1486 /* Decode the next extension. */
1487 if (!CBS_get_u16(&extensions, &type) ||
1488 !CBS_get_u16_length_prefixed(&extensions, &extension))
1489 {
1490 *out_alert = SSL_AD_DECODE_ERROR;
1491 return 0;
1492 }
1493
Adam Langley95c29f32014-06-20 12:00:00 -07001494 if (s->tlsext_debug_cb)
David Benjamindc72ff72014-06-25 12:36:10 -04001495 {
1496 s->tlsext_debug_cb(s, 0, type, (unsigned char*)CBS_data(&extension),
1497 CBS_len(&extension), s->tlsext_debug_arg);
1498 }
1499
Adam Langley95c29f32014-06-20 12:00:00 -07001500/* The servername extension is treated as follows:
1501
1502 - Only the hostname type is supported with a maximum length of 255.
1503 - The servername is rejected if too long or if it contains zeros,
1504 in which case an fatal alert is generated.
1505 - The servername field is maintained together with the session cache.
1506 - When a session is resumed, the servername call back invoked in order
1507 to allow the application to position itself to the right context.
1508 - The servername is acknowledged if it is new for a session or when
1509 it is identical to a previously used for the same session.
1510 Applications can control the behaviour. They can at any time
1511 set a 'desirable' servername for a new SSL object. This can be the
1512 case for example with HTTPS when a Host: header field is received and
1513 a renegotiation is requested. In this case, a possible servername
1514 presented in the new client hello is only acknowledged if it matches
1515 the value of the Host: field.
1516 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1517 if they provide for changing an explicit servername context for the session,
1518 i.e. when the session has been established with a servername extension.
1519 - On session reconnect, the servername extension may be absent.
1520
1521*/
1522
1523 if (type == TLSEXT_TYPE_server_name)
1524 {
David Benjamindc72ff72014-06-25 12:36:10 -04001525 CBS server_name_list;
Adam Langleyed8270a2014-09-02 13:52:56 -07001526 char have_seen_host_name = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001527
1528 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1529 CBS_len(&server_name_list) < 1 ||
1530 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001531 {
David Benjamindc72ff72014-06-25 12:36:10 -04001532 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001533 return 0;
1534 }
Adam Langley95c29f32014-06-20 12:00:00 -07001535
David Benjamindc72ff72014-06-25 12:36:10 -04001536 /* Decode each ServerName in the extension. */
1537 while (CBS_len(&server_name_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001538 {
David Benjamindc72ff72014-06-25 12:36:10 -04001539 uint8_t name_type;
1540 CBS host_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001541
David Benjamindc72ff72014-06-25 12:36:10 -04001542 /* Decode the NameType. */
1543 if (!CBS_get_u8(&server_name_list, &name_type))
Adam Langley95c29f32014-06-20 12:00:00 -07001544 {
David Benjamindc72ff72014-06-25 12:36:10 -04001545 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001546 return 0;
1547 }
David Benjamindc72ff72014-06-25 12:36:10 -04001548
David Benjamindc72ff72014-06-25 12:36:10 -04001549 /* Only host_name is supported. */
1550 if (name_type != TLSEXT_NAMETYPE_host_name)
1551 continue;
1552
Adam Langleyed8270a2014-09-02 13:52:56 -07001553 if (have_seen_host_name)
1554 {
1555 /* The ServerNameList MUST NOT contain
1556 * more than one name of the same
1557 * name_type. */
1558 *out_alert = SSL_AD_DECODE_ERROR;
1559 return 0;
1560 }
1561
1562 have_seen_host_name = 1;
1563
1564 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1565 CBS_len(&host_name) < 1)
1566 {
1567 *out_alert = SSL_AD_DECODE_ERROR;
1568 return 0;
1569 }
1570
1571 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1572 CBS_contains_zero_byte(&host_name))
1573 {
1574 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1575 return 0;
1576 }
1577
David Benjamindc72ff72014-06-25 12:36:10 -04001578 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -07001579 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001580 assert(s->session->tlsext_hostname == NULL);
David Benjamindc72ff72014-06-25 12:36:10 -04001581 if (s->session->tlsext_hostname)
Adam Langley95c29f32014-06-20 12:00:00 -07001582 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001583 /* This should be impossible. */
David Benjamindc72ff72014-06-25 12:36:10 -04001584 *out_alert = SSL_AD_DECODE_ERROR;
1585 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001586 }
Adam Langley95c29f32014-06-20 12:00:00 -07001587
David Benjamindc72ff72014-06-25 12:36:10 -04001588 /* Copy the hostname as a string. */
David Benjamined439582014-07-14 19:13:02 -04001589 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname))
David Benjamindc72ff72014-06-25 12:36:10 -04001590 {
1591 *out_alert = SSL_AD_INTERNAL_ERROR;
1592 return 0;
1593 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001594
1595 s->should_ack_sni = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001596 }
Adam Langley95c29f32014-06-20 12:00:00 -07001597 }
Adam Langley95c29f32014-06-20 12:00:00 -07001598 }
1599
Adam Langley95c29f32014-06-20 12:00:00 -07001600 else if (type == TLSEXT_TYPE_ec_point_formats)
1601 {
David Benjamindc72ff72014-06-25 12:36:10 -04001602 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001603
David Benjamindc72ff72014-06-25 12:36:10 -04001604 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1605 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001606 {
David Benjamindc72ff72014-06-25 12:36:10 -04001607 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001608 return 0;
1609 }
David Benjamindc72ff72014-06-25 12:36:10 -04001610
David Benjamina19fc252014-10-19 00:14:36 -04001611 if (!CBS_stow(&ec_point_format_list,
1612 &s->s3->tmp.peer_ecpointformatlist,
1613 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001614 {
David Benjamina19fc252014-10-19 00:14:36 -04001615 *out_alert = SSL_AD_INTERNAL_ERROR;
1616 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001617 }
Adam Langley95c29f32014-06-20 12:00:00 -07001618 }
1619 else if (type == TLSEXT_TYPE_elliptic_curves)
1620 {
David Benjamindc72ff72014-06-25 12:36:10 -04001621 CBS elliptic_curve_list;
David Benjamin072334d2014-07-13 16:24:27 -04001622 size_t i, num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001623
David Benjamindc72ff72014-06-25 12:36:10 -04001624 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
David Benjamin072334d2014-07-13 16:24:27 -04001625 CBS_len(&elliptic_curve_list) == 0 ||
1626 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
David Benjamindc72ff72014-06-25 12:36:10 -04001627 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001628 {
David Benjamindc72ff72014-06-25 12:36:10 -04001629 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001630 return 0;
1631 }
David Benjamindc72ff72014-06-25 12:36:10 -04001632
David Benjamina19fc252014-10-19 00:14:36 -04001633 if (s->s3->tmp.peer_ellipticcurvelist)
Adam Langley95c29f32014-06-20 12:00:00 -07001634 {
David Benjamina19fc252014-10-19 00:14:36 -04001635 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1636 s->s3->tmp.peer_ellipticcurvelist_length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001637 }
David Benjamina19fc252014-10-19 00:14:36 -04001638 s->s3->tmp.peer_ellipticcurvelist =
1639 (uint16_t*)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
1640 if (s->s3->tmp.peer_ellipticcurvelist == NULL)
1641 {
1642 *out_alert = SSL_AD_INTERNAL_ERROR;
1643 return 0;
1644 }
1645 num_curves = CBS_len(&elliptic_curve_list) / 2;
1646 for (i = 0; i < num_curves; i++)
1647 {
1648 if (!CBS_get_u16(&elliptic_curve_list,
1649 &s->s3->tmp.peer_ellipticcurvelist[i]))
1650 {
1651 *out_alert = SSL_AD_INTERNAL_ERROR;
1652 return 0;
1653 }
1654 }
1655 if (CBS_len(&elliptic_curve_list) != 0)
1656 {
1657 *out_alert = SSL_AD_INTERNAL_ERROR;
1658 return 0;
1659 }
1660 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001661 }
Adam Langley95c29f32014-06-20 12:00:00 -07001662 else if (type == TLSEXT_TYPE_session_ticket)
1663 {
1664 if (s->tls_session_ticket_ext_cb &&
David Benjamindc72ff72014-06-25 12:36:10 -04001665 !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 -07001666 {
David Benjamindc72ff72014-06-25 12:36:10 -04001667 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001668 return 0;
1669 }
1670 }
1671 else if (type == TLSEXT_TYPE_renegotiate)
1672 {
David Benjamindc72ff72014-06-25 12:36:10 -04001673 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001674 return 0;
1675 renegotiate_seen = 1;
1676 }
1677 else if (type == TLSEXT_TYPE_signature_algorithms)
1678 {
David Benjamindc72ff72014-06-25 12:36:10 -04001679 CBS supported_signature_algorithms;
1680
David Benjamindc72ff72014-06-25 12:36:10 -04001681 if (!CBS_get_u16_length_prefixed(&extension, &supported_signature_algorithms) ||
1682 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001683 {
David Benjamindc72ff72014-06-25 12:36:10 -04001684 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001685 return 0;
1686 }
David Benjamindc72ff72014-06-25 12:36:10 -04001687
1688 /* Ensure the signature algorithms are non-empty. It
1689 * contains a list of SignatureAndHashAlgorithms
1690 * which are two bytes each. */
1691 if (CBS_len(&supported_signature_algorithms) == 0 ||
1692 (CBS_len(&supported_signature_algorithms) % 2) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001693 {
David Benjamindc72ff72014-06-25 12:36:10 -04001694 *out_alert = SSL_AD_DECODE_ERROR;
1695 return 0;
1696 }
1697
David Benjamincd996942014-07-20 16:23:51 -04001698 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
David Benjamindc72ff72014-06-25 12:36:10 -04001699 {
1700 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001701 return 0;
1702 }
1703 /* If sigalgs received and no shared algorithms fatal
1704 * error.
1705 */
1706 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
1707 {
1708 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
David Benjamindc72ff72014-06-25 12:36:10 -04001709 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07001710 return 0;
1711 }
1712 }
1713
Adam Langley95c29f32014-06-20 12:00:00 -07001714 else if (type == TLSEXT_TYPE_next_proto_neg &&
1715 s->s3->tmp.finish_md_len == 0 &&
1716 s->s3->alpn_selected == NULL)
1717 {
David Benjamindc72ff72014-06-25 12:36:10 -04001718 /* The extension must be empty. */
1719 if (CBS_len(&extension) != 0)
1720 {
1721 *out_alert = SSL_AD_DECODE_ERROR;
1722 return 0;
1723 }
1724
Adam Langley95c29f32014-06-20 12:00:00 -07001725 /* We shouldn't accept this extension on a
1726 * renegotiation.
1727 *
1728 * s->new_session will be set on renegotiation, but we
1729 * probably shouldn't rely that it couldn't be set on
1730 * the initial renegotation too in certain cases (when
1731 * there's some other reason to disallow resuming an
1732 * earlier session -- the current code won't be doing
1733 * anything like that, but this might change).
1734
1735 * A valid sign that there's been a previous handshake
1736 * in this connection is if s->s3->tmp.finish_md_len >
1737 * 0. (We are talking about a check that will happen
1738 * in the Hello protocol round, well before a new
1739 * Finished message could have been computed.) */
1740 s->s3->next_proto_neg_seen = 1;
1741 }
Adam Langley95c29f32014-06-20 12:00:00 -07001742
1743 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1744 s->ctx->alpn_select_cb &&
1745 s->s3->tmp.finish_md_len == 0)
1746 {
David Benjamindc72ff72014-06-25 12:36:10 -04001747 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001748 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001749 /* ALPN takes precedence over NPN. */
1750 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001751 }
1752
Adam Langley1258b6a2014-06-20 12:00:00 -07001753 else if (type == TLSEXT_TYPE_channel_id &&
1754 s->tlsext_channel_id_enabled)
David Benjamindc72ff72014-06-25 12:36:10 -04001755 {
1756 /* The extension must be empty. */
1757 if (CBS_len(&extension) != 0)
1758 {
1759 *out_alert = SSL_AD_DECODE_ERROR;
1760 return 0;
1761 }
1762
Adam Langley1258b6a2014-06-20 12:00:00 -07001763 s->s3->tlsext_channel_id_valid = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001764 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001765
1766 else if (type == TLSEXT_TYPE_channel_id_new &&
1767 s->tlsext_channel_id_enabled)
1768 {
David Benjamindc72ff72014-06-25 12:36:10 -04001769 /* The extension must be empty. */
1770 if (CBS_len(&extension) != 0)
1771 {
1772 *out_alert = SSL_AD_DECODE_ERROR;
1773 return 0;
1774 }
1775
Adam Langley1258b6a2014-06-20 12:00:00 -07001776 s->s3->tlsext_channel_id_valid = 1;
1777 s->s3->tlsext_channel_id_new = 1;
1778 }
1779
1780
Adam Langley95c29f32014-06-20 12:00:00 -07001781 /* session ticket processed earlier */
1782 else if (type == TLSEXT_TYPE_use_srtp)
1783 {
David Benjamindc72ff72014-06-25 12:36:10 -04001784 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001785 return 0;
1786 }
Adam Langley75712922014-10-10 16:23:43 -07001787
1788 else if (type == TLSEXT_TYPE_extended_master_secret &&
1789 s->version != SSL3_VERSION)
1790 {
1791 if (CBS_len(&extension) != 0)
1792 {
1793 *out_alert = SSL_AD_DECODE_ERROR;
1794 return 0;
1795 }
1796
1797 s->s3->tmp.extended_master_secret = 1;
1798 }
Adam Langley95c29f32014-06-20 12:00:00 -07001799 }
1800
Adam Langley95c29f32014-06-20 12:00:00 -07001801 ri_check:
1802
1803 /* Need RI if renegotiating */
1804
1805 if (!renegotiate_seen && s->renegotiate &&
1806 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1807 {
David Benjamindc72ff72014-06-25 12:36:10 -04001808 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin172fc2c2014-09-06 13:09:47 -04001809 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07001810 return 0;
1811 }
Adam Langley95c29f32014-06-20 12:00:00 -07001812
1813 return 1;
1814 }
1815
David Benjamindc72ff72014-06-25 12:36:10 -04001816int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001817 {
David Benjamindc72ff72014-06-25 12:36:10 -04001818 int alert = -1;
1819 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001820 {
David Benjamindc72ff72014-06-25 12:36:10 -04001821 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07001822 return 0;
1823 }
1824
David Benjamin6c7aed02014-08-27 16:42:38 -04001825 if (ssl_check_clienthello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001826 {
David Benjamin9d28c752014-07-05 00:43:48 -04001827 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07001828 return 0;
1829 }
1830 return 1;
David Benjamin072334d2014-07-13 16:24:27 -04001831 }
Adam Langley95c29f32014-06-20 12:00:00 -07001832
Adam Langley95c29f32014-06-20 12:00:00 -07001833/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1834 * elements of zero length are allowed and the set of elements must exactly fill
1835 * the length of the block. */
David Benjamin03973092014-06-24 23:27:17 -04001836static char ssl_next_proto_validate(const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001837 {
David Benjamin03973092014-06-24 23:27:17 -04001838 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001839
David Benjamin03973092014-06-24 23:27:17 -04001840 while (CBS_len(&copy) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001841 {
David Benjamin03973092014-06-24 23:27:17 -04001842 CBS proto;
1843 if (!CBS_get_u8_length_prefixed(&copy, &proto) ||
1844 CBS_len(&proto) == 0)
1845 {
Adam Langley95c29f32014-06-20 12:00:00 -07001846 return 0;
David Benjamin03973092014-06-24 23:27:17 -04001847 }
Adam Langley95c29f32014-06-20 12:00:00 -07001848 }
David Benjamin03973092014-06-24 23:27:17 -04001849 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001850 }
Adam Langley95c29f32014-06-20 12:00:00 -07001851
David Benjamin03973092014-06-24 23:27:17 -04001852static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001853 {
Adam Langley95c29f32014-06-20 12:00:00 -07001854 int tlsext_servername = 0;
1855 int renegotiate_seen = 0;
David Benjamin03973092014-06-24 23:27:17 -04001856 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001857
David Benjamin6c7aed02014-08-27 16:42:38 -04001858 /* TODO(davidben): Move all of these to some per-handshake state that
1859 * gets systematically reset on a new handshake; perhaps allocate it
1860 * fresh each time so it's not even kept around post-handshake. */
Adam Langley95c29f32014-06-20 12:00:00 -07001861 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001862
David Benjamin6c7aed02014-08-27 16:42:38 -04001863 s->tlsext_ticket_expected = 0;
1864 s->s3->tmp.certificate_status_expected = 0;
Adam Langley75712922014-10-10 16:23:43 -07001865 s->s3->tmp.extended_master_secret = 0;
David Benjamin64442872014-07-21 17:43:45 -04001866
Adam Langley95c29f32014-06-20 12:00:00 -07001867 if (s->s3->alpn_selected)
1868 {
1869 OPENSSL_free(s->s3->alpn_selected);
1870 s->s3->alpn_selected = NULL;
1871 }
1872
David Benjamina19fc252014-10-19 00:14:36 -04001873 /* Clear ECC extensions */
1874 if (s->s3->tmp.peer_ecpointformatlist != 0)
1875 {
1876 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1877 s->s3->tmp.peer_ecpointformatlist = NULL;
1878 s->s3->tmp.peer_ecpointformatlist_length = 0;
1879 }
1880
David Benjamin03973092014-06-24 23:27:17 -04001881 /* There may be no extensions. */
1882 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001883 {
David Benjamin03973092014-06-24 23:27:17 -04001884 goto ri_check;
1885 }
1886
David Benjamin35a7a442014-07-05 00:23:20 -04001887 /* Decode the extensions block and check it is valid. */
1888 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1889 !tls1_check_duplicate_extensions(&extensions))
David Benjamin03973092014-06-24 23:27:17 -04001890 {
1891 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001892 return 0;
1893 }
1894
David Benjamin03973092014-06-24 23:27:17 -04001895 while (CBS_len(&extensions) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001896 {
David Benjamin03973092014-06-24 23:27:17 -04001897 uint16_t type;
1898 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001899
David Benjamin03973092014-06-24 23:27:17 -04001900 /* Decode the next extension. */
1901 if (!CBS_get_u16(&extensions, &type) ||
1902 !CBS_get_u16_length_prefixed(&extensions, &extension))
1903 {
1904 *out_alert = SSL_AD_DECODE_ERROR;
1905 return 0;
1906 }
Adam Langley95c29f32014-06-20 12:00:00 -07001907
1908 if (s->tlsext_debug_cb)
David Benjamin03973092014-06-24 23:27:17 -04001909 {
1910 s->tlsext_debug_cb(s, 1, type, (unsigned char*)CBS_data(&extension),
1911 CBS_len(&extension), s->tlsext_debug_arg);
1912 }
Adam Langley95c29f32014-06-20 12:00:00 -07001913
1914 if (type == TLSEXT_TYPE_server_name)
1915 {
David Benjamin03973092014-06-24 23:27:17 -04001916 /* The extension must be empty. */
1917 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001918 {
David Benjamin03973092014-06-24 23:27:17 -04001919 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001920 return 0;
1921 }
David Benjamin03973092014-06-24 23:27:17 -04001922 /* We must have sent it in ClientHello. */
1923 if (s->tlsext_hostname == NULL)
1924 {
1925 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1926 return 0;
1927 }
1928 tlsext_servername = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001929 }
Adam Langley95c29f32014-06-20 12:00:00 -07001930 else if (type == TLSEXT_TYPE_ec_point_formats)
1931 {
David Benjamin03973092014-06-24 23:27:17 -04001932 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001933
David Benjamin03973092014-06-24 23:27:17 -04001934 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1935 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001936 {
David Benjamin03973092014-06-24 23:27:17 -04001937 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001938 return 0;
1939 }
David Benjamin03973092014-06-24 23:27:17 -04001940
David Benjamina19fc252014-10-19 00:14:36 -04001941 if (!CBS_stow(&ec_point_format_list,
1942 &s->s3->tmp.peer_ecpointformatlist,
1943 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001944 {
David Benjamina19fc252014-10-19 00:14:36 -04001945 *out_alert = SSL_AD_INTERNAL_ERROR;
1946 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001947 }
Adam Langley95c29f32014-06-20 12:00:00 -07001948 }
Adam Langley95c29f32014-06-20 12:00:00 -07001949 else if (type == TLSEXT_TYPE_session_ticket)
1950 {
1951 if (s->tls_session_ticket_ext_cb &&
David Benjamin03973092014-06-24 23:27:17 -04001952 !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension),
1953 s->tls_session_ticket_ext_cb_arg))
Adam Langley95c29f32014-06-20 12:00:00 -07001954 {
David Benjamin03973092014-06-24 23:27:17 -04001955 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001956 return 0;
1957 }
David Benjamin03973092014-06-24 23:27:17 -04001958
1959 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001960 {
David Benjamin03973092014-06-24 23:27:17 -04001961 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07001962 return 0;
1963 }
David Benjamin03973092014-06-24 23:27:17 -04001964
Adam Langley95c29f32014-06-20 12:00:00 -07001965 s->tlsext_ticket_expected = 1;
1966 }
Adam Langley95c29f32014-06-20 12:00:00 -07001967 else if (type == TLSEXT_TYPE_status_request)
1968 {
David Benjamin03973092014-06-24 23:27:17 -04001969 /* The extension MUST be empty and may only sent if
1970 * we've requested a status request message. */
1971 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001972 {
David Benjamin03973092014-06-24 23:27:17 -04001973 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001974 return 0;
1975 }
David Benjamin6c7aed02014-08-27 16:42:38 -04001976 if (!s->ocsp_stapling_enabled)
David Benjamin03973092014-06-24 23:27:17 -04001977 {
1978 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1979 return 0;
1980 }
1981 /* Set a flag to expect a CertificateStatus message */
David Benjamin6c7aed02014-08-27 16:42:38 -04001982 s->s3->tmp.certificate_status_expected = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001983 }
David Benjamin03973092014-06-24 23:27:17 -04001984 else if (type == TLSEXT_TYPE_next_proto_neg && s->s3->tmp.finish_md_len == 0) {
1985 unsigned char *selected;
1986 unsigned char selected_len;
1987
1988 /* We must have requested it. */
1989 if (s->ctx->next_proto_select_cb == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001990 {
David Benjamin03973092014-06-24 23:27:17 -04001991 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1992 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001993 }
Adam Langley95c29f32014-06-20 12:00:00 -07001994
David Benjamin03973092014-06-24 23:27:17 -04001995 /* The data must be valid. */
1996 if (!ssl_next_proto_validate(&extension))
1997 {
1998 *out_alert = SSL_AD_DECODE_ERROR;
1999 return 0;
2000 }
2001
2002 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len,
2003 CBS_data(&extension), CBS_len(&extension),
2004 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
2005 {
2006 *out_alert = SSL_AD_INTERNAL_ERROR;
2007 return 0;
2008 }
2009
2010 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
2011 if (s->next_proto_negotiated == NULL)
2012 {
2013 *out_alert = SSL_AD_INTERNAL_ERROR;
2014 return 0;
2015 }
2016 s->next_proto_negotiated_len = selected_len;
2017 s->s3->next_proto_neg_seen = 1;
2018 }
Adam Langley95c29f32014-06-20 12:00:00 -07002019 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
2020 {
David Benjamin03973092014-06-24 23:27:17 -04002021 CBS protocol_name_list, protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07002022
2023 /* We must have requested it. */
2024 if (s->alpn_client_proto_list == NULL)
2025 {
David Benjamin03973092014-06-24 23:27:17 -04002026 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07002027 return 0;
2028 }
David Benjamin03973092014-06-24 23:27:17 -04002029
2030 /* The extension data consists of a ProtocolNameList
2031 * which must have exactly one ProtocolName. Each of
2032 * these is length-prefixed. */
2033 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
2034 CBS_len(&extension) != 0 ||
2035 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
2036 CBS_len(&protocol_name_list) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002037 {
David Benjamin03973092014-06-24 23:27:17 -04002038 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002039 return 0;
2040 }
David Benjamin03973092014-06-24 23:27:17 -04002041
2042 if (!CBS_stow(&protocol_name,
2043 &s->s3->alpn_selected,
2044 &s->s3->alpn_selected_len))
Adam Langley95c29f32014-06-20 12:00:00 -07002045 {
David Benjamin03973092014-06-24 23:27:17 -04002046 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002047 return 0;
2048 }
Adam Langley95c29f32014-06-20 12:00:00 -07002049 }
2050
Adam Langley1258b6a2014-06-20 12:00:00 -07002051 else if (type == TLSEXT_TYPE_channel_id)
David Benjamin03973092014-06-24 23:27:17 -04002052 {
2053 if (CBS_len(&extension) != 0)
2054 {
2055 *out_alert = SSL_AD_DECODE_ERROR;
2056 return 0;
2057 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002058 s->s3->tlsext_channel_id_valid = 1;
David Benjamin03973092014-06-24 23:27:17 -04002059 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002060 else if (type == TLSEXT_TYPE_channel_id_new)
2061 {
David Benjamin03973092014-06-24 23:27:17 -04002062 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;
2068 s->s3->tlsext_channel_id_new = 1;
2069 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002070 else if (type == TLSEXT_TYPE_certificate_timestamp)
2071 {
2072 if (CBS_len(&extension) == 0)
2073 {
2074 *out_alert = SSL_AD_DECODE_ERROR;
2075 return 0;
2076 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002077
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002078 /* Session resumption uses the original session information. */
2079 if (!s->hit)
2080 {
2081 if (!CBS_stow(&extension,
2082 &s->session->tlsext_signed_cert_timestamp_list,
2083 &s->session->tlsext_signed_cert_timestamp_list_length))
2084 {
2085 *out_alert = SSL_AD_INTERNAL_ERROR;
2086 return 0;
2087 }
2088 }
2089 }
Adam Langley95c29f32014-06-20 12:00:00 -07002090 else if (type == TLSEXT_TYPE_renegotiate)
2091 {
David Benjamin03973092014-06-24 23:27:17 -04002092 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002093 return 0;
2094 renegotiate_seen = 1;
2095 }
Adam Langley95c29f32014-06-20 12:00:00 -07002096 else if (type == TLSEXT_TYPE_use_srtp)
2097 {
David Benjamin03973092014-06-24 23:27:17 -04002098 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002099 return 0;
2100 }
Adam Langley75712922014-10-10 16:23:43 -07002101
2102 else if (type == TLSEXT_TYPE_extended_master_secret)
2103 {
2104 if (/* It is invalid for the server to select EMS and
2105 SSLv3. */
2106 s->version == SSL3_VERSION ||
2107 CBS_len(&extension) != 0)
2108 {
2109 *out_alert = SSL_AD_DECODE_ERROR;
2110 return 0;
2111 }
2112
2113 s->s3->tmp.extended_master_secret = 1;
2114 }
Adam Langley95c29f32014-06-20 12:00:00 -07002115 }
2116
2117 if (!s->hit && tlsext_servername == 1)
2118 {
2119 if (s->tlsext_hostname)
2120 {
2121 if (s->session->tlsext_hostname == NULL)
2122 {
2123 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2124 if (!s->session->tlsext_hostname)
2125 {
David Benjamin03973092014-06-24 23:27:17 -04002126 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002127 return 0;
2128 }
2129 }
2130 else
2131 {
David Benjamin03973092014-06-24 23:27:17 -04002132 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002133 return 0;
2134 }
2135 }
2136 }
2137
Adam Langley95c29f32014-06-20 12:00:00 -07002138 ri_check:
2139
2140 /* Determine if we need to see RI. Strictly speaking if we want to
2141 * avoid an attack we should *always* see RI even on initial server
2142 * hello because the client doesn't see any renegotiation during an
2143 * attack. However this would mean we could not connect to any server
2144 * which doesn't support RI so for the immediate future tolerate RI
2145 * absence on initial connect only.
2146 */
2147 if (!renegotiate_seen
2148 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2149 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2150 {
David Benjamin03973092014-06-24 23:27:17 -04002151 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin9d28c752014-07-05 00:43:48 -04002152 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07002153 return 0;
2154 }
2155
2156 return 1;
2157 }
2158
2159
2160int ssl_prepare_clienthello_tlsext(SSL *s)
2161 {
Adam Langley95c29f32014-06-20 12:00:00 -07002162 return 1;
2163 }
2164
2165int ssl_prepare_serverhello_tlsext(SSL *s)
2166 {
2167 return 1;
2168 }
2169
David Benjamin6c7aed02014-08-27 16:42:38 -04002170static int ssl_check_clienthello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002171 {
2172 int ret=SSL_TLSEXT_ERR_NOACK;
2173 int al = SSL_AD_UNRECOGNIZED_NAME;
2174
Adam Langley95c29f32014-06-20 12:00:00 -07002175 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2176 * ssl3_choose_cipher in s3_lib.c.
2177 */
2178 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2179 * ssl3_choose_cipher in s3_lib.c.
2180 */
Adam Langley95c29f32014-06-20 12:00:00 -07002181
2182 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2183 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2184 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2185 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2186
Adam Langley95c29f32014-06-20 12:00:00 -07002187 switch (ret)
2188 {
2189 case SSL_TLSEXT_ERR_ALERT_FATAL:
2190 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2191 return -1;
2192
2193 case SSL_TLSEXT_ERR_ALERT_WARNING:
2194 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002195 return 1;
2196
Adam Langley95c29f32014-06-20 12:00:00 -07002197 case SSL_TLSEXT_ERR_NOACK:
Adam Langleyed8270a2014-09-02 13:52:56 -07002198 s->should_ack_sni = 0;
2199 return 1;
2200
2201 default:
2202 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002203 }
2204 }
2205
David Benjamin6c7aed02014-08-27 16:42:38 -04002206static int ssl_check_serverhello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002207 {
2208 int ret=SSL_TLSEXT_ERR_NOACK;
2209 int al = SSL_AD_UNRECOGNIZED_NAME;
2210
Adam Langley95c29f32014-06-20 12:00:00 -07002211 /* If we are client and using an elliptic curve cryptography cipher
2212 * suite, then if server returns an EC point formats lists extension
2213 * it must contain uncompressed.
2214 */
2215 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2216 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamina9ca90a2014-09-26 18:53:43 -04002217 if (((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) &&
2218 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed))
Adam Langley95c29f32014-06-20 12:00:00 -07002219 {
David Benjamina9ca90a2014-09-26 18:53:43 -04002220 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2221 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002222 }
2223 ret = SSL_TLSEXT_ERR_OK;
Adam Langley95c29f32014-06-20 12:00:00 -07002224
2225 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2226 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2227 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2228 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2229
Adam Langley95c29f32014-06-20 12:00:00 -07002230 switch (ret)
2231 {
2232 case SSL_TLSEXT_ERR_ALERT_FATAL:
Adam Langleyed8270a2014-09-02 13:52:56 -07002233 ssl3_send_alert(s,SSL3_AL_FATAL,al);
Adam Langley95c29f32014-06-20 12:00:00 -07002234 return -1;
2235
2236 case SSL_TLSEXT_ERR_ALERT_WARNING:
2237 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002238 return 1;
2239
2240 default:
2241 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002242 }
2243 }
2244
David Benjamin03973092014-06-24 23:27:17 -04002245int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07002246 {
David Benjamin03973092014-06-24 23:27:17 -04002247 int alert = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002248 if (s->version < SSL3_VERSION)
2249 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002250
2251 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002252 {
David Benjamin03973092014-06-24 23:27:17 -04002253 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07002254 return 0;
2255 }
2256
David Benjamin03973092014-06-24 23:27:17 -04002257 if (ssl_check_serverhello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002258 {
David Benjamin9d28c752014-07-05 00:43:48 -04002259 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, SSL_R_SERVERHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07002260 return 0;
2261 }
David Benjamin03973092014-06-24 23:27:17 -04002262
Adam Langley95c29f32014-06-20 12:00:00 -07002263 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002264 }
Adam Langley95c29f32014-06-20 12:00:00 -07002265
2266/* Since the server cache lookup is done early on in the processing of the
2267 * ClientHello, and other operations depend on the result, we need to handle
2268 * any TLS session ticket extension at the same time.
2269 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002270 * ctx: contains the early callback context, which is the result of a
2271 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002272 * ret: (output) on return, if a ticket was decrypted, then this is set to
2273 * point to the resulting session.
2274 *
2275 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2276 * ciphersuite, in which case we have no use for session tickets and one will
2277 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2278 *
2279 * Returns:
2280 * -1: fatal error, either from parsing or decrypting the ticket.
2281 * 0: no ticket was found (or was ignored, based on settings).
2282 * 1: a zero length extension was found, indicating that the client supports
2283 * session tickets but doesn't currently have one to offer.
2284 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2285 * couldn't be decrypted because of a non-fatal error.
2286 * 3: a ticket was successfully decrypted and *ret was set.
2287 *
2288 * Side effects:
2289 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2290 * a new session ticket to the client because the client indicated support
2291 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2292 * a session ticket or we couldn't use the one it gave us, or if
2293 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2294 * Otherwise, s->tlsext_ticket_expected is set to 0.
2295 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002296int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
2297 SSL_SESSION **ret)
Adam Langley95c29f32014-06-20 12:00:00 -07002298 {
Adam Langley95c29f32014-06-20 12:00:00 -07002299 *ret = NULL;
2300 s->tlsext_ticket_expected = 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002301 const unsigned char *data;
2302 size_t len;
2303 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002304
2305 /* If tickets disabled behave as if no ticket present
2306 * to permit stateful resumption.
2307 */
2308 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2309 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002310 if ((s->version <= SSL3_VERSION) && !ctx->extensions)
Adam Langley95c29f32014-06-20 12:00:00 -07002311 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002312 if (!SSL_early_callback_ctx_extension_get(
2313 ctx, TLSEXT_TYPE_session_ticket, &data, &len))
Adam Langley95c29f32014-06-20 12:00:00 -07002314 {
Adam Langleydc9b1412014-06-20 12:00:00 -07002315 return 0;
2316 }
2317 if (len == 0)
2318 {
2319 /* The client will accept a ticket but doesn't
2320 * currently have one. */
2321 s->tlsext_ticket_expected = 1;
2322 return 1;
2323 }
2324 if (s->tls_session_secret_cb)
2325 {
2326 /* Indicate that the ticket couldn't be
2327 * decrypted rather than generating the session
2328 * from ticket now, trigger abbreviated
2329 * handshake based on external mechanism to
2330 * calculate the master secret later. */
2331 return 2;
2332 }
2333 r = tls_decrypt_ticket(s, data, len, ctx->session_id,
2334 ctx->session_id_len, ret);
2335 switch (r)
2336 {
2337 case 2: /* ticket couldn't be decrypted */
2338 s->tlsext_ticket_expected = 1;
2339 return 2;
2340 case 3: /* ticket was decrypted */
2341 return r;
2342 case 4: /* ticket decrypted but need to renew */
2343 s->tlsext_ticket_expected = 1;
2344 return 3;
2345 default: /* fatal error */
Adam Langley95c29f32014-06-20 12:00:00 -07002346 return -1;
2347 }
Adam Langley95c29f32014-06-20 12:00:00 -07002348 }
2349
2350/* tls_decrypt_ticket attempts to decrypt a session ticket.
2351 *
2352 * etick: points to the body of the session ticket extension.
2353 * eticklen: the length of the session tickets extenion.
2354 * sess_id: points at the session ID.
2355 * sesslen: the length of the session ID.
2356 * psess: (output) on return, if a ticket was decrypted, then this is set to
2357 * point to the resulting session.
2358 *
2359 * Returns:
2360 * -1: fatal error, either from parsing or decrypting the ticket.
2361 * 2: the ticket couldn't be decrypted.
2362 * 3: a ticket was successfully decrypted and *psess was set.
2363 * 4: same as 3, but the ticket needs to be renewed.
2364 */
2365static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
2366 const unsigned char *sess_id, int sesslen,
2367 SSL_SESSION **psess)
2368 {
2369 SSL_SESSION *sess;
2370 unsigned char *sdec;
2371 const unsigned char *p;
2372 int slen, mlen, renew_ticket = 0;
2373 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2374 HMAC_CTX hctx;
2375 EVP_CIPHER_CTX ctx;
2376 SSL_CTX *tctx = s->initial_ctx;
2377 /* Need at least keyname + iv + some encrypted data */
2378 if (eticklen < 48)
2379 return 2;
2380 /* Initialize session ticket encryption and HMAC contexts */
2381 HMAC_CTX_init(&hctx);
2382 EVP_CIPHER_CTX_init(&ctx);
2383 if (tctx->tlsext_ticket_key_cb)
2384 {
2385 unsigned char *nctick = (unsigned char *)etick;
2386 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
2387 &ctx, &hctx, 0);
2388 if (rv < 0)
2389 return -1;
2390 if (rv == 0)
2391 return 2;
2392 if (rv == 2)
2393 renew_ticket = 1;
2394 }
2395 else
2396 {
2397 /* Check key name matches */
2398 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
2399 return 2;
2400 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2401 tlsext_tick_md(), NULL);
2402 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2403 tctx->tlsext_tick_aes_key, etick + 16);
2404 }
2405 /* Attempt to process session ticket, first conduct sanity and
2406 * integrity checks on ticket.
2407 */
2408 mlen = HMAC_size(&hctx);
2409 if (mlen < 0)
2410 {
2411 EVP_CIPHER_CTX_cleanup(&ctx);
2412 return -1;
2413 }
2414 eticklen -= mlen;
2415 /* Check HMAC of encrypted ticket */
2416 HMAC_Update(&hctx, etick, eticklen);
2417 HMAC_Final(&hctx, tick_hmac, NULL);
2418 HMAC_CTX_cleanup(&hctx);
2419 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
Adam Langley38311732014-10-16 19:04:35 -07002420 {
2421 EVP_CIPHER_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002422 return 2;
Adam Langley38311732014-10-16 19:04:35 -07002423 }
Adam Langley95c29f32014-06-20 12:00:00 -07002424 /* Attempt to decrypt session data */
2425 /* Move p after IV to start of encrypted ticket, update length */
2426 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2427 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2428 sdec = OPENSSL_malloc(eticklen);
2429 if (!sdec)
2430 {
2431 EVP_CIPHER_CTX_cleanup(&ctx);
2432 return -1;
2433 }
2434 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2435 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0)
Adam Langley3e148852014-07-24 17:34:02 -07002436 {
2437 EVP_CIPHER_CTX_cleanup(&ctx);
2438 OPENSSL_free(sdec);
Adam Langley95c29f32014-06-20 12:00:00 -07002439 return 2;
Adam Langley3e148852014-07-24 17:34:02 -07002440 }
Adam Langley95c29f32014-06-20 12:00:00 -07002441 slen += mlen;
2442 EVP_CIPHER_CTX_cleanup(&ctx);
2443 p = sdec;
2444
2445 sess = d2i_SSL_SESSION(NULL, &p, slen);
2446 OPENSSL_free(sdec);
2447 if (sess)
2448 {
2449 /* The session ID, if non-empty, is used by some clients to
2450 * detect that the ticket has been accepted. So we copy it to
2451 * the session structure. If it is empty set length to zero
2452 * as required by standard.
2453 */
2454 if (sesslen)
2455 memcpy(sess->session_id, sess_id, sesslen);
2456 sess->session_id_length = sesslen;
2457 *psess = sess;
2458 if (renew_ticket)
2459 return 4;
2460 else
2461 return 3;
2462 }
2463 ERR_clear_error();
2464 /* For session parse failure, indicate that we need to send a new
2465 * ticket. */
2466 return 2;
2467 }
2468
2469/* Tables to translate from NIDs to TLS v1.2 ids */
2470
2471typedef struct
2472 {
2473 int nid;
2474 int id;
2475 } tls12_lookup;
2476
David Benjamincff64722014-08-19 19:54:46 -04002477static const tls12_lookup tls12_md[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002478 {NID_md5, TLSEXT_hash_md5},
2479 {NID_sha1, TLSEXT_hash_sha1},
2480 {NID_sha224, TLSEXT_hash_sha224},
2481 {NID_sha256, TLSEXT_hash_sha256},
2482 {NID_sha384, TLSEXT_hash_sha384},
2483 {NID_sha512, TLSEXT_hash_sha512}
2484};
2485
David Benjamincff64722014-08-19 19:54:46 -04002486static const tls12_lookup tls12_sig[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002487 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
Adam Langley95c29f32014-06-20 12:00:00 -07002488 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
2489};
2490
David Benjamincff64722014-08-19 19:54:46 -04002491static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002492 {
2493 size_t i;
2494 for (i = 0; i < tlen; i++)
2495 {
2496 if (table[i].nid == nid)
2497 return table[i].id;
2498 }
2499 return -1;
2500 }
2501
David Benjamincff64722014-08-19 19:54:46 -04002502static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002503 {
2504 size_t i;
2505 for (i = 0; i < tlen; i++)
2506 {
2507 if ((table[i].id) == id)
2508 return table[i].nid;
2509 }
2510 return NID_undef;
2511 }
2512
2513int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
2514 {
2515 int sig_id, md_id;
2516 if (!md)
2517 return 0;
2518 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2519 sizeof(tls12_md)/sizeof(tls12_lookup));
2520 if (md_id == -1)
2521 return 0;
2522 sig_id = tls12_get_sigid(pk);
2523 if (sig_id == -1)
2524 return 0;
2525 p[0] = (unsigned char)md_id;
2526 p[1] = (unsigned char)sig_id;
2527 return 1;
2528 }
2529
2530int tls12_get_sigid(const EVP_PKEY *pk)
2531 {
2532 return tls12_find_id(pk->type, tls12_sig,
2533 sizeof(tls12_sig)/sizeof(tls12_lookup));
2534 }
2535
2536const EVP_MD *tls12_get_hash(unsigned char hash_alg)
2537 {
2538 switch(hash_alg)
2539 {
Adam Langley95c29f32014-06-20 12:00:00 -07002540 case TLSEXT_hash_md5:
Adam Langley95c29f32014-06-20 12:00:00 -07002541 return EVP_md5();
Adam Langley95c29f32014-06-20 12:00:00 -07002542 case TLSEXT_hash_sha1:
2543 return EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002544 case TLSEXT_hash_sha224:
2545 return EVP_sha224();
2546
2547 case TLSEXT_hash_sha256:
2548 return EVP_sha256();
Adam Langley95c29f32014-06-20 12:00:00 -07002549 case TLSEXT_hash_sha384:
2550 return EVP_sha384();
2551
2552 case TLSEXT_hash_sha512:
2553 return EVP_sha512();
Adam Langley95c29f32014-06-20 12:00:00 -07002554 default:
2555 return NULL;
2556
2557 }
2558 }
2559
David Benjaminec2f27d2014-11-13 19:17:25 -05002560/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2561 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
2562static int tls12_get_pkey_type(uint8_t sig_alg)
Adam Langley95c29f32014-06-20 12:00:00 -07002563 {
2564 switch(sig_alg)
2565 {
Adam Langley95c29f32014-06-20 12:00:00 -07002566 case TLSEXT_signature_rsa:
David Benjaminec2f27d2014-11-13 19:17:25 -05002567 return EVP_PKEY_RSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002568 case TLSEXT_signature_ecdsa:
David Benjaminec2f27d2014-11-13 19:17:25 -05002569 return EVP_PKEY_EC;
Adam Langley95c29f32014-06-20 12:00:00 -07002570 }
2571 return -1;
2572 }
2573
2574/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2575static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
2576 int *psignhash_nid, const unsigned char *data)
2577 {
2578 int sign_nid = 0, hash_nid = 0;
2579 if (!phash_nid && !psign_nid && !psignhash_nid)
2580 return;
2581 if (phash_nid || psignhash_nid)
2582 {
2583 hash_nid = tls12_find_nid(data[0], tls12_md,
2584 sizeof(tls12_md)/sizeof(tls12_lookup));
2585 if (phash_nid)
2586 *phash_nid = hash_nid;
2587 }
2588 if (psign_nid || psignhash_nid)
2589 {
2590 sign_nid = tls12_find_nid(data[1], tls12_sig,
2591 sizeof(tls12_sig)/sizeof(tls12_lookup));
2592 if (psign_nid)
2593 *psign_nid = sign_nid;
2594 }
2595 if (psignhash_nid)
2596 {
2597 if (sign_nid && hash_nid)
2598 OBJ_find_sigid_by_algs(psignhash_nid,
2599 hash_nid, sign_nid);
2600 else
2601 *psignhash_nid = NID_undef;
2602 }
2603 }
2604/* Given preference and allowed sigalgs set shared sigalgs */
2605static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
2606 const unsigned char *pref, size_t preflen,
2607 const unsigned char *allow, size_t allowlen)
2608 {
2609 const unsigned char *ptmp, *atmp;
2610 size_t i, j, nmatch = 0;
2611 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
2612 {
2613 /* Skip disabled hashes or signature algorithms */
2614 if (tls12_get_hash(ptmp[0]) == NULL)
2615 continue;
David Benjaminec2f27d2014-11-13 19:17:25 -05002616 if (tls12_get_pkey_type(ptmp[1]) == -1)
Adam Langley95c29f32014-06-20 12:00:00 -07002617 continue;
2618 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
2619 {
2620 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
2621 {
2622 nmatch++;
2623 if (shsig)
2624 {
2625 shsig->rhash = ptmp[0];
2626 shsig->rsign = ptmp[1];
2627 tls1_lookup_sigalg(&shsig->hash_nid,
2628 &shsig->sign_nid,
2629 &shsig->signandhash_nid,
2630 ptmp);
2631 shsig++;
2632 }
2633 break;
2634 }
2635 }
2636 }
2637 return nmatch;
2638 }
2639
2640/* Set shared signature algorithms for SSL structures */
2641static int tls1_set_shared_sigalgs(SSL *s)
2642 {
2643 const unsigned char *pref, *allow, *conf;
2644 size_t preflen, allowlen, conflen;
2645 size_t nmatch;
2646 TLS_SIGALGS *salgs = NULL;
2647 CERT *c = s->cert;
Adam Langleydb4f9522014-06-20 12:00:00 -07002648 if (c->shared_sigalgs)
2649 {
2650 OPENSSL_free(c->shared_sigalgs);
2651 c->shared_sigalgs = NULL;
2652 }
Adam Langley95c29f32014-06-20 12:00:00 -07002653 /* If client use client signature algorithms if not NULL */
David Benjamin335d10d2014-08-06 19:56:33 -04002654 if (!s->server && c->client_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002655 {
2656 conf = c->client_sigalgs;
2657 conflen = c->client_sigalgslen;
2658 }
David Benjamin335d10d2014-08-06 19:56:33 -04002659 else if (c->conf_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002660 {
2661 conf = c->conf_sigalgs;
2662 conflen = c->conf_sigalgslen;
2663 }
2664 else
2665 conflen = tls12_get_psigalgs(s, &conf);
David Benjamin335d10d2014-08-06 19:56:33 -04002666 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
Adam Langley95c29f32014-06-20 12:00:00 -07002667 {
2668 pref = conf;
2669 preflen = conflen;
2670 allow = c->peer_sigalgs;
2671 allowlen = c->peer_sigalgslen;
2672 }
2673 else
2674 {
2675 allow = conf;
2676 allowlen = conflen;
2677 pref = c->peer_sigalgs;
2678 preflen = c->peer_sigalgslen;
2679 }
2680 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2681 if (!nmatch)
2682 return 1;
2683 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2684 if (!salgs)
2685 return 0;
2686 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2687 c->shared_sigalgs = salgs;
2688 c->shared_sigalgslen = nmatch;
2689 return 1;
2690 }
2691
2692
2693/* Set preferred digest for each key type */
2694
David Benjamincd996942014-07-20 16:23:51 -04002695int tls1_process_sigalgs(SSL *s, const CBS *sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002696 {
Adam Langley95c29f32014-06-20 12:00:00 -07002697 CERT *c = s->cert;
David Benjamincd996942014-07-20 16:23:51 -04002698
Adam Langley95c29f32014-06-20 12:00:00 -07002699 /* Extension ignored for inappropriate versions */
2700 if (!SSL_USE_SIGALGS(s))
2701 return 1;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002702 /* Length must be even */
David Benjamincd996942014-07-20 16:23:51 -04002703 if (CBS_len(sigalgs) % 2 != 0)
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002704 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002705 /* Should never happen */
2706 if (!c)
2707 return 0;
2708
David Benjamincd996942014-07-20 16:23:51 -04002709 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen))
Adam Langley95c29f32014-06-20 12:00:00 -07002710 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002711
2712 tls1_set_shared_sigalgs(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002713 return 1;
2714 }
2715
David Benjaminec2f27d2014-11-13 19:17:25 -05002716const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey)
2717 {
2718 CERT *c = s->cert;
2719 int type = EVP_PKEY_id(pkey);
2720 size_t i;
2721
2722 /* Select the first shared digest supported by our key. */
2723 for (i = 0; i < c->shared_sigalgslen; i++)
2724 {
2725 const EVP_MD *md = tls12_get_hash(c->shared_sigalgs[i].rhash);
2726 if (md == NULL || tls12_get_pkey_type(c->shared_sigalgs[i].rsign) != type)
2727 continue;
2728 if (!EVP_PKEY_supports_digest(pkey, md))
2729 continue;
2730 return md;
2731 }
2732
2733 /* If no suitable digest may be found, default to SHA-1. */
2734 return EVP_sha1();
2735 }
Adam Langley95c29f32014-06-20 12:00:00 -07002736
2737int SSL_get_sigalgs(SSL *s, int idx,
2738 int *psign, int *phash, int *psignhash,
2739 unsigned char *rsig, unsigned char *rhash)
2740 {
2741 const unsigned char *psig = s->cert->peer_sigalgs;
2742 if (psig == NULL)
2743 return 0;
2744 if (idx >= 0)
2745 {
2746 idx <<= 1;
2747 if (idx >= (int)s->cert->peer_sigalgslen)
2748 return 0;
2749 psig += idx;
2750 if (rhash)
2751 *rhash = psig[0];
2752 if (rsig)
2753 *rsig = psig[1];
2754 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2755 }
2756 return s->cert->peer_sigalgslen / 2;
2757 }
2758
2759int SSL_get_shared_sigalgs(SSL *s, int idx,
2760 int *psign, int *phash, int *psignhash,
2761 unsigned char *rsig, unsigned char *rhash)
2762 {
2763 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
2764 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
2765 return 0;
2766 shsigalgs += idx;
2767 if (phash)
2768 *phash = shsigalgs->hash_nid;
2769 if (psign)
2770 *psign = shsigalgs->sign_nid;
2771 if (psignhash)
2772 *psignhash = shsigalgs->signandhash_nid;
2773 if (rsig)
2774 *rsig = shsigalgs->rsign;
2775 if (rhash)
2776 *rhash = shsigalgs->rhash;
2777 return s->cert->shared_sigalgslen;
2778 }
2779
Adam Langley1258b6a2014-06-20 12:00:00 -07002780/* tls1_channel_id_hash calculates the signed data for a Channel ID on the given
2781 * SSL connection and writes it to |md|. */
2782int
2783tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s)
2784 {
2785 EVP_MD_CTX ctx;
2786 unsigned char temp_digest[EVP_MAX_MD_SIZE];
2787 unsigned temp_digest_len;
2788 int i;
2789 static const char kClientIDMagic[] = "TLS Channel ID signature";
2790
2791 if (s->s3->handshake_buffer)
Adam Langley75712922014-10-10 16:23:43 -07002792 if (!ssl3_digest_cached_records(s, free_handshake_buffer))
Adam Langley1258b6a2014-06-20 12:00:00 -07002793 return 0;
2794
2795 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2796
2797 if (s->hit && s->s3->tlsext_channel_id_new)
2798 {
2799 static const char kResumptionMagic[] = "Resumption";
2800 EVP_DigestUpdate(md, kResumptionMagic,
2801 sizeof(kResumptionMagic));
2802 if (s->session->original_handshake_hash_len == 0)
2803 return 0;
2804 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2805 s->session->original_handshake_hash_len);
2806 }
2807
2808 EVP_MD_CTX_init(&ctx);
2809 for (i = 0; i < SSL_MAX_DIGEST; i++)
2810 {
2811 if (s->s3->handshake_dgst[i] == NULL)
2812 continue;
2813 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
2814 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2815 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2816 }
2817 EVP_MD_CTX_cleanup(&ctx);
2818
2819 return 1;
2820 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002821
2822/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2823 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
2824int tls1_record_handshake_hashes_for_channel_id(SSL *s)
2825 {
2826 int digest_len;
2827 /* This function should never be called for a resumed session because
2828 * the handshake hashes that we wish to record are for the original,
2829 * full handshake. */
2830 if (s->hit)
2831 return -1;
2832 /* It only makes sense to call this function if Channel IDs have been
2833 * negotiated. */
2834 if (!s->s3->tlsext_channel_id_new)
2835 return -1;
2836
2837 digest_len = tls1_handshake_digest(
2838 s, s->session->original_handshake_hash,
2839 sizeof(s->session->original_handshake_hash));
2840 if (digest_len < 0)
2841 return -1;
2842
2843 s->session->original_handshake_hash_len = digest_len;
2844
2845 return 1;
2846 }
2847
Adam Langley95c29f32014-06-20 12:00:00 -07002848int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
2849 {
2850 unsigned char *sigalgs, *sptr;
2851 int rhash, rsign;
2852 size_t i;
2853 if (salglen & 1)
2854 return 0;
2855 sigalgs = OPENSSL_malloc(salglen);
2856 if (sigalgs == NULL)
2857 return 0;
2858 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
2859 {
2860 rhash = tls12_find_id(*psig_nids++, tls12_md,
2861 sizeof(tls12_md)/sizeof(tls12_lookup));
2862 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2863 sizeof(tls12_sig)/sizeof(tls12_lookup));
2864
2865 if (rhash == -1 || rsign == -1)
2866 goto err;
2867 *sptr++ = rhash;
2868 *sptr++ = rsign;
2869 }
2870
2871 if (client)
2872 {
2873 if (c->client_sigalgs)
2874 OPENSSL_free(c->client_sigalgs);
2875 c->client_sigalgs = sigalgs;
2876 c->client_sigalgslen = salglen;
2877 }
2878 else
2879 {
2880 if (c->conf_sigalgs)
2881 OPENSSL_free(c->conf_sigalgs);
2882 c->conf_sigalgs = sigalgs;
2883 c->conf_sigalgslen = salglen;
2884 }
2885
2886 return 1;
2887
2888 err:
2889 OPENSSL_free(sigalgs);
2890 return 0;
2891 }
2892