blob: 4456dfcac3811fc35706663a6791156f2440729b [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
109#include <stdio.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400110#include <stdlib.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700111#include <assert.h>
112
David Benjamin03973092014-06-24 23:27:17 -0400113#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700114#include <openssl/evp.h>
115#include <openssl/hmac.h>
116#include <openssl/mem.h>
117#include <openssl/obj.h>
118#include <openssl/rand.h>
119
120#include "ssl_locl.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700121static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
122 const unsigned char *sess_id, int sesslen,
123 SSL_SESSION **psess);
David Benjamin6c7aed02014-08-27 16:42:38 -0400124static int ssl_check_clienthello_tlsext(SSL *s);
125static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700126
127SSL3_ENC_METHOD TLSv1_enc_data={
128 tls1_enc,
129 tls1_mac,
130 tls1_setup_key_block,
131 tls1_generate_master_secret,
132 tls1_change_cipher_state,
133 tls1_final_finish_mac,
134 TLS1_FINISH_MAC_LENGTH,
135 tls1_cert_verify_mac,
136 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
137 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
138 tls1_alert_code,
139 tls1_export_keying_material,
140 0,
141 SSL3_HM_HEADER_LENGTH,
142 ssl3_set_handshake_header,
Adam Langley75712922014-10-10 16:23:43 -0700143 ssl3_handshake_write,
144 ssl3_add_to_finished_hash,
Adam Langley95c29f32014-06-20 12:00:00 -0700145 };
146
147SSL3_ENC_METHOD TLSv1_1_enc_data={
148 tls1_enc,
149 tls1_mac,
150 tls1_setup_key_block,
151 tls1_generate_master_secret,
152 tls1_change_cipher_state,
153 tls1_final_finish_mac,
154 TLS1_FINISH_MAC_LENGTH,
155 tls1_cert_verify_mac,
156 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
157 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
158 tls1_alert_code,
159 tls1_export_keying_material,
160 SSL_ENC_FLAG_EXPLICIT_IV,
161 SSL3_HM_HEADER_LENGTH,
162 ssl3_set_handshake_header,
Adam Langley75712922014-10-10 16:23:43 -0700163 ssl3_handshake_write,
164 ssl3_add_to_finished_hash,
Adam Langley95c29f32014-06-20 12:00:00 -0700165 };
166
167SSL3_ENC_METHOD TLSv1_2_enc_data={
168 tls1_enc,
169 tls1_mac,
170 tls1_setup_key_block,
171 tls1_generate_master_secret,
172 tls1_change_cipher_state,
173 tls1_final_finish_mac,
174 TLS1_FINISH_MAC_LENGTH,
175 tls1_cert_verify_mac,
176 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
177 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
178 tls1_alert_code,
179 tls1_export_keying_material,
180 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
181 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
182 SSL3_HM_HEADER_LENGTH,
183 ssl3_set_handshake_header,
Adam Langley75712922014-10-10 16:23:43 -0700184 ssl3_handshake_write,
185 ssl3_add_to_finished_hash,
Adam Langley95c29f32014-06-20 12:00:00 -0700186 };
187
David Benjamin35a7a442014-07-05 00:23:20 -0400188static int compare_uint16_t(const void *p1, const void *p2)
189 {
190 uint16_t u1 = *((const uint16_t*)p1);
191 uint16_t u2 = *((const uint16_t*)p2);
192 if (u1 < u2)
193 {
194 return -1;
195 }
196 else if (u1 > u2)
197 {
198 return 1;
199 }
200 else
201 {
202 return 0;
203 }
204 }
205
206/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be more
207 * than one extension of the same type in a ClientHello or ServerHello. This
208 * function does an initial scan over the extensions block to filter those
209 * out. */
210static int tls1_check_duplicate_extensions(const CBS *cbs)
211 {
212 CBS extensions = *cbs;
213 size_t num_extensions = 0, i = 0;
214 uint16_t *extension_types = NULL;
215 int ret = 0;
216
217 /* First pass: count the extensions. */
218 while (CBS_len(&extensions) > 0)
219 {
220 uint16_t type;
221 CBS extension;
222
223 if (!CBS_get_u16(&extensions, &type) ||
224 !CBS_get_u16_length_prefixed(&extensions, &extension))
225 {
226 goto done;
227 }
228
229 num_extensions++;
230 }
231
David Benjamin9a373592014-07-25 04:27:53 -0400232 if (num_extensions == 0)
233 {
234 return 1;
235 }
236
David Benjamin35a7a442014-07-05 00:23:20 -0400237 extension_types = (uint16_t*)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
238 if (extension_types == NULL)
239 {
240 OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions, ERR_R_MALLOC_FAILURE);
241 goto done;
242 }
243
244 /* Second pass: gather the extension types. */
245 extensions = *cbs;
246 for (i = 0; i < num_extensions; i++)
247 {
248 CBS extension;
249
250 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
251 !CBS_get_u16_length_prefixed(&extensions, &extension))
252 {
253 /* This should not happen. */
254 goto done;
255 }
256 }
257 assert(CBS_len(&extensions) == 0);
258
259 /* Sort the extensions and make sure there are no duplicates. */
260 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
261 for (i = 1; i < num_extensions; i++)
262 {
263 if (extension_types[i-1] == extension_types[i])
264 {
265 goto done;
266 }
267 }
268
269 ret = 1;
270done:
271 if (extension_types)
272 OPENSSL_free(extension_types);
273 return ret;
274 }
275
Adam Langleydc9b1412014-06-20 12:00:00 -0700276char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx)
277 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400278 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
279
280 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700281
282 /* Skip client version. */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400283 if (!CBS_skip(&client_hello, 2))
Adam Langleydc9b1412014-06-20 12:00:00 -0700284 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700285
286 /* Skip client nonce. */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400287 if (!CBS_skip(&client_hello, 32))
Adam Langleydc9b1412014-06-20 12:00:00 -0700288 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700289
David Benjamin8f2c20e2014-07-09 09:30:38 -0400290 /* Extract session_id. */
291 if (!CBS_get_u8_length_prefixed(&client_hello, &session_id))
Adam Langleydc9b1412014-06-20 12:00:00 -0700292 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400293 ctx->session_id = CBS_data(&session_id);
294 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700295
296 /* Skip past DTLS cookie */
David Benjamin09bd58d2014-08-12 21:22:28 -0400297 if (SSL_IS_DTLS(ctx->ssl))
Adam Langleydc9b1412014-06-20 12:00:00 -0700298 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400299 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700300
David Benjamin8f2c20e2014-07-09 09:30:38 -0400301 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie))
Adam Langleydc9b1412014-06-20 12:00:00 -0700302 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700303 }
304
David Benjamin8f2c20e2014-07-09 09:30:38 -0400305 /* Extract cipher_suites. */
306 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
307 CBS_len(&cipher_suites) < 2 ||
308 (CBS_len(&cipher_suites) & 1) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700309 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400310 ctx->cipher_suites = CBS_data(&cipher_suites);
311 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700312
David Benjamin8f2c20e2014-07-09 09:30:38 -0400313 /* Extract compression_methods. */
314 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
315 CBS_len(&compression_methods) < 1)
Adam Langleydc9b1412014-06-20 12:00:00 -0700316 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400317 ctx->compression_methods = CBS_data(&compression_methods);
318 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700319
320 /* If the ClientHello ends here then it's valid, but doesn't have any
321 * extensions. (E.g. SSLv3.) */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400322 if (CBS_len(&client_hello) == 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700323 {
324 ctx->extensions = NULL;
325 ctx->extensions_len = 0;
326 return 1;
327 }
328
David Benjamin8f2c20e2014-07-09 09:30:38 -0400329 /* Extract extensions and check it is valid. */
330 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
331 !tls1_check_duplicate_extensions(&extensions) ||
332 CBS_len(&client_hello) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700333 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400334 ctx->extensions = CBS_data(&extensions);
335 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700336
Adam Langleydc9b1412014-06-20 12:00:00 -0700337 return 1;
Adam Langleydc9b1412014-06-20 12:00:00 -0700338 }
339
340char
341SSL_early_callback_ctx_extension_get(const struct ssl_early_callback_ctx *ctx,
342 uint16_t extension_type,
343 const unsigned char **out_data,
344 size_t *out_len)
345 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400346 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700347
David Benjamin8f2c20e2014-07-09 09:30:38 -0400348 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
349
350 while (CBS_len(&extensions) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700351 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400352 uint16_t type;
353 CBS extension;
Adam Langleydc9b1412014-06-20 12:00:00 -0700354
David Benjamin8f2c20e2014-07-09 09:30:38 -0400355 /* Decode the next extension. */
356 if (!CBS_get_u16(&extensions, &type) ||
357 !CBS_get_u16_length_prefixed(&extensions, &extension))
Adam Langleydc9b1412014-06-20 12:00:00 -0700358 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700359
David Benjamin8f2c20e2014-07-09 09:30:38 -0400360 if (type == extension_type)
Adam Langleydc9b1412014-06-20 12:00:00 -0700361 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400362 *out_data = CBS_data(&extension);
363 *out_len = CBS_len(&extension);
Adam Langleydc9b1412014-06-20 12:00:00 -0700364 return 1;
365 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700366 }
367
368 return 0;
369 }
370
Adam Langley95c29f32014-06-20 12:00:00 -0700371
David Benjamincff64722014-08-19 19:54:46 -0400372static const int nid_list[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700373 {
374 NID_sect163k1, /* sect163k1 (1) */
375 NID_sect163r1, /* sect163r1 (2) */
376 NID_sect163r2, /* sect163r2 (3) */
377 NID_sect193r1, /* sect193r1 (4) */
378 NID_sect193r2, /* sect193r2 (5) */
379 NID_sect233k1, /* sect233k1 (6) */
380 NID_sect233r1, /* sect233r1 (7) */
381 NID_sect239k1, /* sect239k1 (8) */
382 NID_sect283k1, /* sect283k1 (9) */
383 NID_sect283r1, /* sect283r1 (10) */
384 NID_sect409k1, /* sect409k1 (11) */
385 NID_sect409r1, /* sect409r1 (12) */
386 NID_sect571k1, /* sect571k1 (13) */
387 NID_sect571r1, /* sect571r1 (14) */
388 NID_secp160k1, /* secp160k1 (15) */
389 NID_secp160r1, /* secp160r1 (16) */
390 NID_secp160r2, /* secp160r2 (17) */
391 NID_secp192k1, /* secp192k1 (18) */
392 NID_X9_62_prime192v1, /* secp192r1 (19) */
393 NID_secp224k1, /* secp224k1 (20) */
394 NID_secp224r1, /* secp224r1 (21) */
395 NID_secp256k1, /* secp256k1 (22) */
396 NID_X9_62_prime256v1, /* secp256r1 (23) */
397 NID_secp384r1, /* secp384r1 (24) */
398 NID_secp521r1, /* secp521r1 (25) */
399 NID_brainpoolP256r1, /* brainpoolP256r1 (26) */
400 NID_brainpoolP384r1, /* brainpoolP384r1 (27) */
401 NID_brainpoolP512r1 /* brainpool512r1 (28) */
402 };
403
David Benjamin072334d2014-07-13 16:24:27 -0400404static const uint8_t ecformats_default[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700405 {
406 TLSEXT_ECPOINTFORMAT_uncompressed,
Adam Langley95c29f32014-06-20 12:00:00 -0700407 };
408
David Benjamin072334d2014-07-13 16:24:27 -0400409static const uint16_t eccurves_default[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700410 {
David Benjamin072334d2014-07-13 16:24:27 -0400411 23, /* secp256r1 (23) */
412 24, /* secp384r1 (24) */
413 25, /* secp521r1 (25) */
Adam Langley95c29f32014-06-20 12:00:00 -0700414 };
415
David Benjamin072334d2014-07-13 16:24:27 -0400416int tls1_ec_curve_id2nid(uint16_t curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700417 {
418 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
David Benjamin072334d2014-07-13 16:24:27 -0400419 if (curve_id < 1 || curve_id > sizeof(nid_list)/sizeof(nid_list[0]))
420 return OBJ_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700421 return nid_list[curve_id-1];
422 }
423
David Benjamin072334d2014-07-13 16:24:27 -0400424uint16_t tls1_ec_nid2curve_id(int nid)
Adam Langley95c29f32014-06-20 12:00:00 -0700425 {
David Benjamin072334d2014-07-13 16:24:27 -0400426 size_t i;
427 for (i = 0; i < sizeof(nid_list)/sizeof(nid_list[0]); i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700428 {
David Benjamin072334d2014-07-13 16:24:27 -0400429 /* nid_list[i] stores the NID corresponding to curve ID i+1. */
430 if (nid == nid_list[i])
431 return i + 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700432 }
David Benjamin072334d2014-07-13 16:24:27 -0400433 /* Use 0 for non-existent curve ID. Note: this assumes that curve ID 0
434 * will never be allocated. */
435 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700436 }
David Benjamin072334d2014-07-13 16:24:27 -0400437
David Benjamin42e9a772014-09-02 23:18:44 -0400438/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len|
439 * to the list of allowed curve IDs. If |get_peer_curves| is non-zero,
440 * return the peer's curve list. Otherwise, return the preferred
441 * list. */
442static void tls1_get_curvelist(SSL *s, int get_peer_curves,
David Benjamin072334d2014-07-13 16:24:27 -0400443 const uint16_t **out_curve_ids, size_t *out_curve_ids_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700444 {
David Benjamin42e9a772014-09-02 23:18:44 -0400445 if (get_peer_curves)
Adam Langley95c29f32014-06-20 12:00:00 -0700446 {
David Benjamina19fc252014-10-19 00:14:36 -0400447 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
448 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
Adam Langley95c29f32014-06-20 12:00:00 -0700449 return;
450 }
Adam Langley95c29f32014-06-20 12:00:00 -0700451
David Benjamin335d10d2014-08-06 19:56:33 -0400452 *out_curve_ids = s->tlsext_ellipticcurvelist;
453 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
David Benjamin072334d2014-07-13 16:24:27 -0400454 if (!*out_curve_ids)
Adam Langley95c29f32014-06-20 12:00:00 -0700455 {
David Benjamin072334d2014-07-13 16:24:27 -0400456 *out_curve_ids = eccurves_default;
David Benjamin0eb5a2d2014-07-25 02:40:43 -0400457 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
Adam Langley95c29f32014-06-20 12:00:00 -0700458 }
459 }
David Benjamined439582014-07-14 19:13:02 -0400460
David Benjamined439582014-07-14 19:13:02 -0400461int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700462 {
David Benjamined439582014-07-14 19:13:02 -0400463 uint8_t curve_type;
464 uint16_t curve_id;
David Benjamin072334d2014-07-13 16:24:27 -0400465 const uint16_t *curves;
466 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400467
468 /* Only support named curves. */
469 if (!CBS_get_u8(cbs, &curve_type) ||
470 curve_type != NAMED_CURVE_TYPE ||
471 !CBS_get_u16(cbs, &curve_id))
Adam Langley95c29f32014-06-20 12:00:00 -0700472 return 0;
David Benjamined439582014-07-14 19:13:02 -0400473
David Benjamin072334d2014-07-13 16:24:27 -0400474 tls1_get_curvelist(s, 0, &curves, &curves_len);
475 for (i = 0; i < curves_len; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700476 {
David Benjamin072334d2014-07-13 16:24:27 -0400477 if (curve_id == curves[i])
David Benjamined439582014-07-14 19:13:02 -0400478 {
479 *out_curve_id = curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700480 return 1;
David Benjamined439582014-07-14 19:13:02 -0400481 }
Adam Langley95c29f32014-06-20 12:00:00 -0700482 }
483 return 0;
484 }
485
David Benjamin072334d2014-07-13 16:24:27 -0400486int tls1_get_shared_curve(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -0700487 {
David Benjamin072334d2014-07-13 16:24:27 -0400488 const uint16_t *pref, *supp;
Adam Langley95c29f32014-06-20 12:00:00 -0700489 size_t preflen, supplen, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400490
Adam Langley95c29f32014-06-20 12:00:00 -0700491 /* Can't do anything on client side */
492 if (s->server == 0)
David Benjamin072334d2014-07-13 16:24:27 -0400493 return NID_undef;
494
David Benjamin335d10d2014-08-06 19:56:33 -0400495 /* Return first preference shared curve */
Adam Langley95c29f32014-06-20 12:00:00 -0700496 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
497 &supp, &supplen);
498 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
499 &pref, &preflen);
David Benjamin072334d2014-07-13 16:24:27 -0400500 for (i = 0; i < preflen; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700501 {
David Benjamin072334d2014-07-13 16:24:27 -0400502 for (j = 0; j < supplen; j++)
Adam Langley95c29f32014-06-20 12:00:00 -0700503 {
David Benjamin072334d2014-07-13 16:24:27 -0400504 if (pref[i] == supp[j])
505 return tls1_ec_curve_id2nid(pref[i]);
Adam Langley95c29f32014-06-20 12:00:00 -0700506 }
507 }
David Benjamin072334d2014-07-13 16:24:27 -0400508 return NID_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700509 }
510
David Benjamin072334d2014-07-13 16:24:27 -0400511/* NOTE: tls1_ec_curve_id2nid and tls1_set_curves assume that
512 *
513 * (a) 0 is not a valid curve ID.
514 *
515 * (b) The largest curve ID is 31.
516 *
517 * Those implementations must be revised before adding support for curve IDs
518 * that break these assumptions. */
519OPENSSL_COMPILE_ASSERT(
520 (sizeof(nid_list) / sizeof(nid_list[0])) < 32, small_curve_ids);
521
522int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
523 const int *curves, size_t ncurves)
Adam Langley95c29f32014-06-20 12:00:00 -0700524 {
David Benjamin072334d2014-07-13 16:24:27 -0400525 uint16_t *curve_ids;
Adam Langley95c29f32014-06-20 12:00:00 -0700526 size_t i;
527 /* Bitmap of curves included to detect duplicates: only works
528 * while curve ids < 32
529 */
David Benjamin072334d2014-07-13 16:24:27 -0400530 uint32_t dup_list = 0;
531 curve_ids = (uint16_t*)OPENSSL_malloc(ncurves * sizeof(uint16_t));
532 if (!curve_ids)
Adam Langley95c29f32014-06-20 12:00:00 -0700533 return 0;
David Benjamin072334d2014-07-13 16:24:27 -0400534 for (i = 0; i < ncurves; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700535 {
David Benjamin072334d2014-07-13 16:24:27 -0400536 uint32_t idmask;
537 uint16_t id;
Adam Langley95c29f32014-06-20 12:00:00 -0700538 id = tls1_ec_nid2curve_id(curves[i]);
David Benjamin072334d2014-07-13 16:24:27 -0400539 idmask = ((uint32_t)1) << id;
Adam Langley95c29f32014-06-20 12:00:00 -0700540 if (!id || (dup_list & idmask))
541 {
David Benjamin072334d2014-07-13 16:24:27 -0400542 OPENSSL_free(curve_ids);
Adam Langley95c29f32014-06-20 12:00:00 -0700543 return 0;
544 }
545 dup_list |= idmask;
David Benjamin072334d2014-07-13 16:24:27 -0400546 curve_ids[i] = id;
Adam Langley95c29f32014-06-20 12:00:00 -0700547 }
David Benjamin072334d2014-07-13 16:24:27 -0400548 if (*out_curve_ids)
549 OPENSSL_free(*out_curve_ids);
550 *out_curve_ids = curve_ids;
551 *out_curve_ids_len = ncurves;
Adam Langley95c29f32014-06-20 12:00:00 -0700552 return 1;
553 }
554
David Benjamin072334d2014-07-13 16:24:27 -0400555/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
556 * TLS curve ID and point format, respectively, for |ec|. It returns one on
557 * success and zero on failure. */
558static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id, uint8_t *out_comp_id, EC_KEY *ec)
Adam Langley95c29f32014-06-20 12:00:00 -0700559 {
Adam Langley95c29f32014-06-20 12:00:00 -0700560 int nid;
David Benjamin072334d2014-07-13 16:24:27 -0400561 uint16_t id;
Adam Langley95c29f32014-06-20 12:00:00 -0700562 const EC_GROUP *grp;
563 if (!ec)
564 return 0;
565
Adam Langley95c29f32014-06-20 12:00:00 -0700566 grp = EC_KEY_get0_group(ec);
567 if (!grp)
568 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700569
570 /* Determine curve ID */
David Benjamin072334d2014-07-13 16:24:27 -0400571 nid = EC_GROUP_get_curve_name(grp);
572 id = tls1_ec_nid2curve_id(nid);
573 if (!id)
574 return 0;
575
576 /* Set the named curve ID. Arbitrary explicit curves are not
577 * supported. */
578 *out_curve_id = id;
579
580 if (out_comp_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700581 {
582 if (EC_KEY_get0_public_key(ec) == NULL)
583 return 0;
584 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED)
David Benjamin072334d2014-07-13 16:24:27 -0400585 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
Adam Langley95c29f32014-06-20 12:00:00 -0700586 else
David Benjamin072334d2014-07-13 16:24:27 -0400587 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
Adam Langley95c29f32014-06-20 12:00:00 -0700588 }
589 return 1;
590 }
David Benjamin072334d2014-07-13 16:24:27 -0400591
David Benjamin42e9a772014-09-02 23:18:44 -0400592/* tls1_check_point_format returns one if |comp_id| is consistent with the
593 * peer's point format preferences. */
594static int tls1_check_point_format(SSL *s, uint8_t comp_id)
595 {
David Benjamina19fc252014-10-19 00:14:36 -0400596 uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
597 size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
David Benjamin42e9a772014-09-02 23:18:44 -0400598 size_t i;
599
600 /* If point formats extension present check it, otherwise everything
601 * is supported (see RFC4492). */
602 if (p == NULL)
603 return 1;
604
605 for (i = 0; i < plen; i++)
606 {
607 if (comp_id == p[i])
608 return 1;
609 }
610 return 0;
611 }
612
613/* tls1_check_curve_id returns one if |curve_id| is consistent with both our and
614 * the peer's curve preferences. Note: if called as the client, only our
615 * preferences are checked; the peer (the server) does not send preferences. */
616static int tls1_check_curve_id(SSL *s, uint16_t curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700617 {
David Benjamin072334d2014-07-13 16:24:27 -0400618 const uint16_t *curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400619 size_t curves_len, i, j;
620
621 /* Check against our list, then the peer's list. */
Adam Langley95c29f32014-06-20 12:00:00 -0700622 for (j = 0; j <= 1; j++)
623 {
David Benjamin072334d2014-07-13 16:24:27 -0400624 tls1_get_curvelist(s, j, &curves, &curves_len);
625 for (i = 0; i < curves_len; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700626 {
David Benjamin42e9a772014-09-02 23:18:44 -0400627 if (curves[i] == curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700628 break;
629 }
David Benjamin072334d2014-07-13 16:24:27 -0400630 if (i == curves_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700631 return 0;
David Benjamin42e9a772014-09-02 23:18:44 -0400632 /* Servers do not present a preference list so, if we are a
633 * client, only check our list. */
Adam Langley95c29f32014-06-20 12:00:00 -0700634 if (!s->server)
635 return 1;
636 }
637 return 1;
638 }
639
640static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
641 size_t *pformatslen)
642 {
643 /* If we have a custom point format list use it otherwise
644 * use default */
645 if (s->tlsext_ecpointformatlist)
646 {
647 *pformats = s->tlsext_ecpointformatlist;
648 *pformatslen = s->tlsext_ecpointformatlist_length;
649 }
650 else
651 {
652 *pformats = ecformats_default;
David Benjamin335d10d2014-08-06 19:56:33 -0400653 *pformatslen = sizeof(ecformats_default);
Adam Langley95c29f32014-06-20 12:00:00 -0700654 }
655 }
656
657/* Check cert parameters compatible with extensions: currently just checks
658 * EC certificates have compatible curves and compression.
659 */
660static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
661 {
David Benjamin072334d2014-07-13 16:24:27 -0400662 uint8_t comp_id;
663 uint16_t curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700664 EVP_PKEY *pkey;
665 int rv;
666 pkey = X509_get_pubkey(x);
667 if (!pkey)
668 return 0;
669 /* If not EC nothing to do */
670 if (pkey->type != EVP_PKEY_EC)
671 {
672 EVP_PKEY_free(pkey);
673 return 1;
674 }
David Benjamin072334d2014-07-13 16:24:27 -0400675 rv = tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec);
Adam Langley95c29f32014-06-20 12:00:00 -0700676 EVP_PKEY_free(pkey);
677 if (!rv)
678 return 0;
679 /* Can't check curve_id for client certs as we don't have a
David Benjamin42e9a772014-09-02 23:18:44 -0400680 * supported curves extension. */
681 if (s->server && !tls1_check_curve_id(s, curve_id))
682 return 0;
683 return tls1_check_point_format(s, comp_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700684 }
David Benjamin42e9a772014-09-02 23:18:44 -0400685
Adam Langley95c29f32014-06-20 12:00:00 -0700686/* Check EC temporary key is compatible with client extensions */
David Benjamin42e9a772014-09-02 23:18:44 -0400687int tls1_check_ec_tmp_key(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -0700688 {
David Benjamin072334d2014-07-13 16:24:27 -0400689 uint16_t curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700690 EC_KEY *ec = s->cert->ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -0700691 if (s->cert->ecdh_tmp_auto)
692 {
693 /* Need a shared curve */
David Benjamin072334d2014-07-13 16:24:27 -0400694 return tls1_get_shared_curve(s) != NID_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700695 }
696 if (!ec)
697 {
698 if (s->cert->ecdh_tmp_cb)
699 return 1;
700 else
701 return 0;
702 }
David Benjamin42e9a772014-09-02 23:18:44 -0400703 return tls1_curve_params_from_ec_key(&curve_id, NULL, ec) &&
704 tls1_check_curve_id(s, curve_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700705 }
706
Adam Langley95c29f32014-06-20 12:00:00 -0700707
Adam Langley95c29f32014-06-20 12:00:00 -0700708
709/* List of supported signature algorithms and hashes. Should make this
710 * customisable at some point, for now include everything we support.
711 */
712
Adam Langley95c29f32014-06-20 12:00:00 -0700713#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700714
Adam Langley95c29f32014-06-20 12:00:00 -0700715#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700716
717#define tlsext_sigalg(md) \
718 tlsext_sigalg_rsa(md) \
Adam Langley95c29f32014-06-20 12:00:00 -0700719 tlsext_sigalg_ecdsa(md)
720
David Benjamincff64722014-08-19 19:54:46 -0400721static const uint8_t tls12_sigalgs[] = {
Adam Langley95c29f32014-06-20 12:00:00 -0700722 tlsext_sigalg(TLSEXT_hash_sha512)
723 tlsext_sigalg(TLSEXT_hash_sha384)
Adam Langley95c29f32014-06-20 12:00:00 -0700724 tlsext_sigalg(TLSEXT_hash_sha256)
725 tlsext_sigalg(TLSEXT_hash_sha224)
Adam Langley95c29f32014-06-20 12:00:00 -0700726 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700727};
Adam Langley95c29f32014-06-20 12:00:00 -0700728size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
729 {
Adam Langley95c29f32014-06-20 12:00:00 -0700730 /* If server use client authentication sigalgs if not NULL */
731 if (s->server && s->cert->client_sigalgs)
732 {
733 *psigs = s->cert->client_sigalgs;
734 return s->cert->client_sigalgslen;
735 }
736 else if (s->cert->conf_sigalgs)
737 {
738 *psigs = s->cert->conf_sigalgs;
739 return s->cert->conf_sigalgslen;
740 }
741 else
742 {
743 *psigs = tls12_sigalgs;
744 return sizeof(tls12_sigalgs);
745 }
746 }
David Benjamin05da6e12014-07-12 20:42:55 -0400747
748/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of
749 * |cbs|. It checks it is consistent with |s|'s sent supported
750 * signature algorithms and, if so, writes the relevant digest into
751 * |*out_md| and returns 1. Otherwise it returns 0 and writes an alert
752 * into |*out_alert|.
Adam Langley95c29f32014-06-20 12:00:00 -0700753 */
David Benjamin05da6e12014-07-12 20:42:55 -0400754int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert,
755 SSL *s, CBS *cbs, EVP_PKEY *pkey)
Adam Langley95c29f32014-06-20 12:00:00 -0700756 {
757 const unsigned char *sent_sigs;
758 size_t sent_sigslen, i;
759 int sigalg = tls12_get_sigid(pkey);
David Benjamin05da6e12014-07-12 20:42:55 -0400760 uint8_t hash, signature;
Adam Langley95c29f32014-06-20 12:00:00 -0700761 /* Should never happen */
762 if (sigalg == -1)
David Benjamin05da6e12014-07-12 20:42:55 -0400763 {
764 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
765 *out_alert = SSL_AD_INTERNAL_ERROR;
766 return 0;
767 }
768 if (!CBS_get_u8(cbs, &hash) ||
769 !CBS_get_u8(cbs, &signature))
770 {
771 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
772 *out_alert = SSL_AD_DECODE_ERROR;
773 return 0;
774 }
Adam Langley95c29f32014-06-20 12:00:00 -0700775 /* Check key type is consistent with signature */
David Benjamin05da6e12014-07-12 20:42:55 -0400776 if (sigalg != signature)
Adam Langley95c29f32014-06-20 12:00:00 -0700777 {
778 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin05da6e12014-07-12 20:42:55 -0400779 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700780 return 0;
781 }
Adam Langley95c29f32014-06-20 12:00:00 -0700782 if (pkey->type == EVP_PKEY_EC)
783 {
David Benjamin072334d2014-07-13 16:24:27 -0400784 uint16_t curve_id;
785 uint8_t comp_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700786 /* Check compression and curve matches extensions */
David Benjamin072334d2014-07-13 16:24:27 -0400787 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec))
David Benjamin05da6e12014-07-12 20:42:55 -0400788 {
789 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -0700790 return 0;
David Benjamin05da6e12014-07-12 20:42:55 -0400791 }
David Benjamin42e9a772014-09-02 23:18:44 -0400792 if (s->server)
Adam Langley95c29f32014-06-20 12:00:00 -0700793 {
David Benjamin42e9a772014-09-02 23:18:44 -0400794 if (!tls1_check_curve_id(s, curve_id) ||
795 !tls1_check_point_format(s, comp_id))
796 {
797 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
798 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
799 return 0;
800 }
Adam Langley95c29f32014-06-20 12:00:00 -0700801 }
David Benjamin05da6e12014-07-12 20:42:55 -0400802 }
Adam Langley95c29f32014-06-20 12:00:00 -0700803
804 /* Check signature matches a type we sent */
805 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
806 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
807 {
David Benjamin05da6e12014-07-12 20:42:55 -0400808 if (hash == sent_sigs[0] && signature == sent_sigs[1])
Adam Langley95c29f32014-06-20 12:00:00 -0700809 break;
810 }
David Benjamin253b3e72014-11-13 15:53:52 -0500811 /* Allow fallback to SHA-1. */
812 if (i == sent_sigslen && hash != TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700813 {
814 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin05da6e12014-07-12 20:42:55 -0400815 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700816 return 0;
817 }
David Benjamin05da6e12014-07-12 20:42:55 -0400818 *out_md = tls12_get_hash(hash);
819 if (*out_md == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700820 {
821 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
David Benjamin05da6e12014-07-12 20:42:55 -0400822 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700823 return 0;
824 }
Adam Langley95c29f32014-06-20 12:00:00 -0700825 return 1;
826 }
827/* Get a mask of disabled algorithms: an algorithm is disabled
828 * if it isn't supported or doesn't appear in supported signature
829 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
830 * session and not global settings.
831 *
832 */
833void ssl_set_client_disabled(SSL *s)
834 {
835 CERT *c = s->cert;
836 const unsigned char *sigalgs;
837 size_t i, sigalgslen;
David Benjaminef2116d2014-08-19 20:21:56 -0400838 int have_rsa = 0, have_ecdsa = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700839 c->mask_a = 0;
840 c->mask_k = 0;
841 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
842 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
843 c->mask_ssl = SSL_TLSV1_2;
844 else
845 c->mask_ssl = 0;
846 /* Now go through all signature algorithms seeing if we support
847 * any for RSA, DSA, ECDSA. Do this for all versions not just
848 * TLS 1.2.
849 */
850 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
851 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
852 {
853 switch(sigalgs[1])
854 {
Adam Langley95c29f32014-06-20 12:00:00 -0700855 case TLSEXT_signature_rsa:
856 have_rsa = 1;
857 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700858 case TLSEXT_signature_ecdsa:
859 have_ecdsa = 1;
860 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700861 }
862 }
David Benjamin0da0e182014-08-19 16:20:28 -0400863 /* Disable auth if we don't include any appropriate signature
864 * algorithms.
Adam Langley95c29f32014-06-20 12:00:00 -0700865 */
866 if (!have_rsa)
867 {
868 c->mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700869 }
Adam Langley95c29f32014-06-20 12:00:00 -0700870 if (!have_ecdsa)
871 {
872 c->mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700873 }
Adam Langley95c29f32014-06-20 12:00:00 -0700874 /* with PSK there must be client callback set */
875 if (!s->psk_client_callback)
876 {
877 c->mask_a |= SSL_aPSK;
878 c->mask_k |= SSL_kPSK;
879 }
Adam Langley95c29f32014-06-20 12:00:00 -0700880 c->valid = 1;
881 }
882
Adam Langleyb0c235e2014-06-20 12:00:00 -0700883/* header_len is the length of the ClientHello header written so far, used to
884 * compute padding. It does not include the record header. Pass 0 if no padding
885 * is to be done. */
886unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, size_t header_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700887 {
888 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -0700889 unsigned char *ret = buf;
890 unsigned char *orig = buf;
Adam Langley95c29f32014-06-20 12:00:00 -0700891 /* See if we support any ECC ciphersuites */
892 int using_ecc = 0;
893 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
894 {
David Benjaminfb3ff2c2014-09-30 21:00:38 -0400895 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -0700896 unsigned long alg_k, alg_a;
897 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
898
899 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
900 {
David Benjamin6f260012014-08-15 13:49:12 -0400901 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700902
903 alg_k = c->algorithm_mkey;
904 alg_a = c->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -0400905 if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA))
Adam Langley95c29f32014-06-20 12:00:00 -0700906 {
907 using_ecc = 1;
908 break;
909 }
910 }
911 }
Adam Langley95c29f32014-06-20 12:00:00 -0700912
913 /* don't add extensions for SSLv3 unless doing secure renegotiation */
914 if (s->client_version == SSL3_VERSION
915 && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -0700916 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -0700917
918 ret+=2;
919
920 if (ret>=limit) return NULL; /* this really never occurs, but ... */
921
922 if (s->tlsext_hostname != NULL)
923 {
924 /* Add TLS extension servername to the Client Hello message */
925 unsigned long size_str;
926 long lenmax;
927
928 /* check for enough space.
929 4 for the servername type and entension length
930 2 for servernamelist length
931 1 for the hostname type
932 2 for hostname length
933 + hostname length
934 */
935
936 if ((lenmax = limit - ret - 9) < 0
937 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
938 return NULL;
939
940 /* extension type and length */
941 s2n(TLSEXT_TYPE_server_name,ret);
942 s2n(size_str+5,ret);
943
944 /* length of servername list */
945 s2n(size_str+3,ret);
946
947 /* hostname type, length and hostname */
948 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
949 s2n(size_str,ret);
950 memcpy(ret, s->tlsext_hostname, size_str);
951 ret+=size_str;
952 }
953
954 /* Add RI if renegotiating */
955 if (s->renegotiate)
956 {
957 int el;
958
959 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
960 {
961 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
962 return NULL;
963 }
964
Adam Langleyb0c235e2014-06-20 12:00:00 -0700965 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700966
967 s2n(TLSEXT_TYPE_renegotiate,ret);
968 s2n(el,ret);
969
970 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
971 {
972 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
973 return NULL;
974 }
975
976 ret += el;
977 }
978
Adam Langley75712922014-10-10 16:23:43 -0700979 /* Add extended master secret. */
980 if (s->version != SSL3_VERSION)
981 {
982 if (limit - ret - 4 < 0)
983 return NULL;
984 s2n(TLSEXT_TYPE_extended_master_secret,ret);
985 s2n(0,ret);
986 }
987
Adam Langley95c29f32014-06-20 12:00:00 -0700988 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
989 {
990 int ticklen;
991 if (!s->new_session && s->session && s->session->tlsext_tick)
992 ticklen = s->session->tlsext_ticklen;
993 else if (s->session && s->tlsext_session_ticket &&
994 s->tlsext_session_ticket->data)
995 {
David Benjamin072c9532014-07-26 11:44:25 -0400996 s->session->tlsext_tick = BUF_memdup(
997 s->tlsext_session_ticket->data,
998 s->tlsext_session_ticket->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700999 if (!s->session->tlsext_tick)
1000 return NULL;
David Benjamin072c9532014-07-26 11:44:25 -04001001 ticklen = s->tlsext_session_ticket->length;
Adam Langley95c29f32014-06-20 12:00:00 -07001002 s->session->tlsext_ticklen = ticklen;
1003 }
1004 else
1005 ticklen = 0;
1006 if (ticklen == 0 && s->tlsext_session_ticket &&
1007 s->tlsext_session_ticket->data == NULL)
1008 goto skip_ext;
1009 /* Check for enough room 2 for extension type, 2 for len
1010 * rest for ticket
1011 */
1012 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1013 s2n(TLSEXT_TYPE_session_ticket,ret);
1014 s2n(ticklen,ret);
1015 if (ticklen)
1016 {
1017 memcpy(ret, s->session->tlsext_tick, ticklen);
1018 ret += ticklen;
1019 }
1020 }
1021 skip_ext:
1022
1023 if (SSL_USE_SIGALGS(s))
1024 {
1025 size_t salglen;
1026 const unsigned char *salg;
1027 salglen = tls12_get_psigalgs(s, &salg);
1028 if ((size_t)(limit - ret) < salglen + 6)
1029 return NULL;
1030 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1031 s2n(salglen + 2, ret);
1032 s2n(salglen, ret);
1033 memcpy(ret, salg, salglen);
1034 ret += salglen;
1035 }
1036
David Benjamin6c7aed02014-08-27 16:42:38 -04001037 if (s->ocsp_stapling_enabled)
Adam Langley95c29f32014-06-20 12:00:00 -07001038 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001039 /* The status_request extension is excessively extensible at
1040 * every layer. On the client, only support requesting OCSP
1041 * responses with an empty responder_id_list and no
1042 * extensions. */
1043 if (limit - ret - 4 - 1 - 2 - 2 < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001044
Adam Langley95c29f32014-06-20 12:00:00 -07001045 s2n(TLSEXT_TYPE_status_request, ret);
David Benjamin6c7aed02014-08-27 16:42:38 -04001046 s2n(1 + 2 + 2, ret);
1047 /* status_type */
Adam Langley95c29f32014-06-20 12:00:00 -07001048 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
David Benjamin6c7aed02014-08-27 16:42:38 -04001049 /* responder_id_list - empty */
1050 s2n(0, ret);
1051 /* request_extensions - empty */
1052 s2n(0, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001053 }
Adam Langley95c29f32014-06-20 12:00:00 -07001054
Adam Langley95c29f32014-06-20 12:00:00 -07001055 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1056 {
1057 /* The client advertises an emtpy extension to indicate its
1058 * support for Next Protocol Negotiation */
1059 if (limit - ret - 4 < 0)
1060 return NULL;
1061 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1062 s2n(0,ret);
1063 }
Adam Langley95c29f32014-06-20 12:00:00 -07001064
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001065 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len)
1066 {
1067 /* The client advertises an empty extension to indicate its support for
1068 * certificate timestamps. */
1069 if (limit - ret - 4 < 0)
1070 return NULL;
1071 s2n(TLSEXT_TYPE_certificate_timestamp,ret);
1072 s2n(0,ret);
1073 }
1074
Adam Langley95c29f32014-06-20 12:00:00 -07001075 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1076 {
1077 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1078 return NULL;
1079 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1080 s2n(2 + s->alpn_client_proto_list_len,ret);
1081 s2n(s->alpn_client_proto_list_len,ret);
1082 memcpy(ret, s->alpn_client_proto_list,
1083 s->alpn_client_proto_list_len);
1084 ret += s->alpn_client_proto_list_len;
1085 }
1086
Adam Langley1258b6a2014-06-20 12:00:00 -07001087 if (s->tlsext_channel_id_enabled)
1088 {
1089 /* The client advertises an emtpy extension to indicate its
1090 * support for Channel ID. */
1091 if (limit - ret - 4 < 0)
1092 return NULL;
1093 if (s->ctx->tlsext_channel_id_enabled_new)
1094 s2n(TLSEXT_TYPE_channel_id_new,ret);
1095 else
1096 s2n(TLSEXT_TYPE_channel_id,ret);
1097 s2n(0,ret);
1098 }
1099
Adam Langley95c29f32014-06-20 12:00:00 -07001100 if(SSL_get_srtp_profiles(s))
1101 {
1102 int el;
1103
1104 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1105
Adam Langleyb0c235e2014-06-20 12:00:00 -07001106 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001107
1108 s2n(TLSEXT_TYPE_use_srtp,ret);
1109 s2n(el,ret);
1110
David Benjamin120a6742014-08-30 14:54:37 -04001111 if(!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001112 {
1113 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1114 return NULL;
1115 }
1116 ret += el;
1117 }
1118
Adam Langleyc3174b72014-06-20 12:00:00 -07001119 if (using_ecc)
1120 {
1121 /* Add TLS extension ECPointFormats to the ClientHello message */
1122 long lenmax;
David Benjamin072334d2014-07-13 16:24:27 -04001123 const uint8_t *formats;
1124 const uint16_t *curves;
1125 size_t formats_len, curves_len, i;
Adam Langleyc3174b72014-06-20 12:00:00 -07001126
David Benjamin072334d2014-07-13 16:24:27 -04001127 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001128
1129 if ((lenmax = limit - ret - 5) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001130 if (formats_len > (size_t)lenmax) return NULL;
1131 if (formats_len > 255)
Adam Langleyc3174b72014-06-20 12:00:00 -07001132 {
1133 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1134 return NULL;
1135 }
1136
1137 s2n(TLSEXT_TYPE_ec_point_formats,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001138 s2n(formats_len + 1,ret);
1139 *(ret++) = (unsigned char)formats_len;
1140 memcpy(ret, formats, formats_len);
1141 ret+=formats_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001142
1143 /* Add TLS extension EllipticCurves to the ClientHello message */
David Benjamin072334d2014-07-13 16:24:27 -04001144 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001145
1146 if ((lenmax = limit - ret - 6) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001147 if ((curves_len * 2) > (size_t)lenmax) return NULL;
1148 if ((curves_len * 2) > 65532)
Adam Langleyc3174b72014-06-20 12:00:00 -07001149 {
1150 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1151 return NULL;
1152 }
1153
1154 s2n(TLSEXT_TYPE_elliptic_curves,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001155 s2n((curves_len * 2) + 2, ret);
Adam Langleyc3174b72014-06-20 12:00:00 -07001156
1157 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1158 * elliptic_curve_list, but the examples use two bytes.
1159 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1160 * resolves this to two bytes.
1161 */
David Benjamin072334d2014-07-13 16:24:27 -04001162 s2n(curves_len * 2, ret);
1163 for (i = 0; i < curves_len; i++)
1164 {
1165 s2n(curves[i], ret);
1166 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001167 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001168
Adam Langley95c29f32014-06-20 12:00:00 -07001169#ifdef TLSEXT_TYPE_padding
1170 /* Add padding to workaround bugs in F5 terminators.
Adam Langleyb0c235e2014-06-20 12:00:00 -07001171 * See https://tools.ietf.org/html/draft-agl-tls-padding-03
Adam Langley95c29f32014-06-20 12:00:00 -07001172 *
1173 * NB: because this code works out the length of all existing
Adam Langleyb0c235e2014-06-20 12:00:00 -07001174 * extensions it MUST always appear last. */
1175 if (header_len > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001176 {
Adam Langleyb0c235e2014-06-20 12:00:00 -07001177 header_len += ret - orig;
1178 if (header_len > 0xff && header_len < 0x200)
1179 {
1180 size_t padding_len = 0x200 - header_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001181 /* Extensions take at least four bytes to encode. Always
1182 * include least one byte of data if including the
1183 * extension. WebSphere Application Server 7.0 is
1184 * intolerant to the last extension being zero-length. */
1185 if (padding_len >= 4 + 1)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001186 padding_len -= 4;
1187 else
Adam Langleyc3174b72014-06-20 12:00:00 -07001188 padding_len = 1;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001189 if (limit - ret - 4 - (long)padding_len < 0)
1190 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001191
Adam Langleyb0c235e2014-06-20 12:00:00 -07001192 s2n(TLSEXT_TYPE_padding, ret);
1193 s2n(padding_len, ret);
1194 memset(ret, 0, padding_len);
1195 ret += padding_len;
1196 }
Adam Langley95c29f32014-06-20 12:00:00 -07001197 }
1198#endif
1199
Adam Langleyb0c235e2014-06-20 12:00:00 -07001200 if ((extdatalen = ret-orig-2)== 0)
1201 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001202
Adam Langleyb0c235e2014-06-20 12:00:00 -07001203 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001204 return ret;
1205 }
1206
Adam Langleyb0c235e2014-06-20 12:00:00 -07001207unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit)
Adam Langley95c29f32014-06-20 12:00:00 -07001208 {
1209 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001210 unsigned char *orig = buf;
1211 unsigned char *ret = buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001212 int next_proto_neg_seen;
Adam Langley95c29f32014-06-20 12:00:00 -07001213 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1214 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -04001215 int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
David Benjamina19fc252014-10-19 00:14:36 -04001216 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001217 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1218 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001219 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001220
1221 ret+=2;
1222 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1223
Adam Langleyed8270a2014-09-02 13:52:56 -07001224 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001225 {
1226 if ((long)(limit - ret - 4) < 0) return NULL;
1227
1228 s2n(TLSEXT_TYPE_server_name,ret);
1229 s2n(0,ret);
1230 }
1231
1232 if(s->s3->send_connection_binding)
1233 {
1234 int el;
1235
1236 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1237 {
1238 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1239 return NULL;
1240 }
1241
Adam Langleyb0c235e2014-06-20 12:00:00 -07001242 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001243
1244 s2n(TLSEXT_TYPE_renegotiate,ret);
1245 s2n(el,ret);
1246
1247 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1248 {
1249 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1250 return NULL;
1251 }
1252
1253 ret += el;
1254 }
1255
Adam Langley75712922014-10-10 16:23:43 -07001256 if (s->s3->tmp.extended_master_secret)
1257 {
1258 if ((long)(limit - ret - 4) < 0) return NULL;
1259
1260 s2n(TLSEXT_TYPE_extended_master_secret,ret);
1261 s2n(0,ret);
1262 }
1263
Adam Langley95c29f32014-06-20 12:00:00 -07001264 if (using_ecc)
1265 {
1266 const unsigned char *plist;
1267 size_t plistlen;
1268 /* Add TLS extension ECPointFormats to the ServerHello message */
1269 long lenmax;
1270
1271 tls1_get_formatlist(s, &plist, &plistlen);
1272
1273 if ((lenmax = limit - ret - 5) < 0) return NULL;
1274 if (plistlen > (size_t)lenmax) return NULL;
1275 if (plistlen > 255)
1276 {
1277 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1278 return NULL;
1279 }
1280
1281 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1282 s2n(plistlen + 1,ret);
1283 *(ret++) = (unsigned char) plistlen;
1284 memcpy(ret, plist, plistlen);
1285 ret+=plistlen;
1286
1287 }
1288 /* Currently the server should not respond with a SupportedCurves extension */
Adam Langley95c29f32014-06-20 12:00:00 -07001289
1290 if (s->tlsext_ticket_expected
1291 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1292 {
1293 if ((long)(limit - ret - 4) < 0) return NULL;
1294 s2n(TLSEXT_TYPE_session_ticket,ret);
1295 s2n(0,ret);
1296 }
1297
David Benjamin6c7aed02014-08-27 16:42:38 -04001298 if (s->s3->tmp.certificate_status_expected)
Adam Langley95c29f32014-06-20 12:00:00 -07001299 {
1300 if ((long)(limit - ret - 4) < 0) return NULL;
1301 s2n(TLSEXT_TYPE_status_request,ret);
1302 s2n(0,ret);
1303 }
1304
Adam Langley95c29f32014-06-20 12:00:00 -07001305 if(s->srtp_profile)
1306 {
1307 int el;
1308
1309 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1310
Adam Langleyb0c235e2014-06-20 12:00:00 -07001311 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001312
1313 s2n(TLSEXT_TYPE_use_srtp,ret);
1314 s2n(el,ret);
1315
David Benjamin120a6742014-08-30 14:54:37 -04001316 if(!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001317 {
1318 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1319 return NULL;
1320 }
1321 ret+=el;
1322 }
1323
Adam Langley95c29f32014-06-20 12:00:00 -07001324 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1325 s->s3->next_proto_neg_seen = 0;
1326 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1327 {
1328 const unsigned char *npa;
1329 unsigned int npalen;
1330 int r;
1331
1332 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1333 if (r == SSL_TLSEXT_ERR_OK)
1334 {
1335 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1336 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1337 s2n(npalen,ret);
1338 memcpy(ret, npa, npalen);
1339 ret += npalen;
1340 s->s3->next_proto_neg_seen = 1;
1341 }
1342 }
Adam Langley95c29f32014-06-20 12:00:00 -07001343
Adam Langley95c29f32014-06-20 12:00:00 -07001344 if (s->s3->alpn_selected)
1345 {
David Benjamin03973092014-06-24 23:27:17 -04001346 const uint8_t *selected = s->s3->alpn_selected;
1347 size_t len = s->s3->alpn_selected_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001348
1349 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1350 return NULL;
1351 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1352 s2n(3 + len,ret);
1353 s2n(1 + len,ret);
1354 *ret++ = len;
1355 memcpy(ret, selected, len);
1356 ret += len;
1357 }
1358
Adam Langley1258b6a2014-06-20 12:00:00 -07001359 /* If the client advertised support for Channel ID, and we have it
1360 * enabled, then we want to echo it back. */
1361 if (s->s3->tlsext_channel_id_valid)
1362 {
1363 if (limit - ret - 4 < 0)
1364 return NULL;
1365 if (s->s3->tlsext_channel_id_new)
1366 s2n(TLSEXT_TYPE_channel_id_new,ret);
1367 else
1368 s2n(TLSEXT_TYPE_channel_id,ret);
1369 s2n(0,ret);
1370 }
1371
Adam Langleyb0c235e2014-06-20 12:00:00 -07001372 if ((extdatalen = ret-orig-2) == 0)
1373 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001374
Adam Langleyb0c235e2014-06-20 12:00:00 -07001375 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001376 return ret;
1377 }
1378
Adam Langley95c29f32014-06-20 12:00:00 -07001379/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1380 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001381 * cbs: the contents of the extension, not including the type and length.
1382 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001383 * return.
1384 *
David Benjamindc72ff72014-06-25 12:36:10 -04001385 * returns: 1 on success. */
1386static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001387 {
Adam Langleyded93582014-07-31 15:23:51 -07001388 CBS protocol_name_list, protocol_name_list_copy;
Adam Langley95c29f32014-06-20 12:00:00 -07001389 const unsigned char *selected;
1390 unsigned char selected_len;
1391 int r;
1392
1393 if (s->ctx->alpn_select_cb == NULL)
David Benjamindc72ff72014-06-25 12:36:10 -04001394 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001395
David Benjamindc72ff72014-06-25 12:36:10 -04001396 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1397 CBS_len(cbs) != 0 ||
1398 CBS_len(&protocol_name_list) < 2)
Adam Langley95c29f32014-06-20 12:00:00 -07001399 goto parse_error;
1400
David Benjamindc72ff72014-06-25 12:36:10 -04001401 /* Validate the protocol list. */
Adam Langleyded93582014-07-31 15:23:51 -07001402 protocol_name_list_copy = protocol_name_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001403 while (CBS_len(&protocol_name_list_copy) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001404 {
David Benjamindc72ff72014-06-25 12:36:10 -04001405 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001406
David Benjamindc72ff72014-06-25 12:36:10 -04001407 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001408 goto parse_error;
Adam Langley95c29f32014-06-20 12:00:00 -07001409 }
1410
David Benjamindc72ff72014-06-25 12:36:10 -04001411 r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1412 CBS_data(&protocol_name_list), CBS_len(&protocol_name_list),
1413 s->ctx->alpn_select_cb_arg);
Adam Langley95c29f32014-06-20 12:00:00 -07001414 if (r == SSL_TLSEXT_ERR_OK) {
1415 if (s->s3->alpn_selected)
1416 OPENSSL_free(s->s3->alpn_selected);
David Benjamin072c9532014-07-26 11:44:25 -04001417 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001418 if (!s->s3->alpn_selected)
1419 {
David Benjamindc72ff72014-06-25 12:36:10 -04001420 *out_alert = SSL_AD_INTERNAL_ERROR;
1421 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001422 }
Adam Langley95c29f32014-06-20 12:00:00 -07001423 s->s3->alpn_selected_len = selected_len;
1424 }
David Benjamindc72ff72014-06-25 12:36:10 -04001425 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001426
1427parse_error:
David Benjamindc72ff72014-06-25 12:36:10 -04001428 *out_alert = SSL_AD_DECODE_ERROR;
1429 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001430 }
1431
David Benjamindc72ff72014-06-25 12:36:10 -04001432static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001433 {
Adam Langley95c29f32014-06-20 12:00:00 -07001434 int renegotiate_seen = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001435 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001436 size_t i;
1437
Adam Langleyed8270a2014-09-02 13:52:56 -07001438 s->should_ack_sni = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001439 s->s3->next_proto_neg_seen = 0;
David Benjamin6c7aed02014-08-27 16:42:38 -04001440 s->s3->tmp.certificate_status_expected = 0;
Adam Langley75712922014-10-10 16:23:43 -07001441 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001442
Adam Langley95c29f32014-06-20 12:00:00 -07001443 if (s->s3->alpn_selected)
1444 {
1445 OPENSSL_free(s->s3->alpn_selected);
1446 s->s3->alpn_selected = NULL;
1447 }
1448
Adam Langley95c29f32014-06-20 12:00:00 -07001449 /* Clear any signature algorithms extension received */
1450 if (s->cert->peer_sigalgs)
1451 {
1452 OPENSSL_free(s->cert->peer_sigalgs);
1453 s->cert->peer_sigalgs = NULL;
1454 }
David Benjamina19fc252014-10-19 00:14:36 -04001455 /* Clear any shared signature algorithms */
Adam Langley95c29f32014-06-20 12:00:00 -07001456 if (s->cert->shared_sigalgs)
1457 {
1458 OPENSSL_free(s->cert->shared_sigalgs);
1459 s->cert->shared_sigalgs = NULL;
1460 }
1461 /* Clear certificate digests and validity flags */
1462 for (i = 0; i < SSL_PKEY_NUM; i++)
1463 {
1464 s->cert->pkeys[i].digest = NULL;
1465 s->cert->pkeys[i].valid_flags = 0;
1466 }
David Benjamina19fc252014-10-19 00:14:36 -04001467 /* Clear ECC extensions */
1468 if (s->s3->tmp.peer_ecpointformatlist != 0)
1469 {
1470 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1471 s->s3->tmp.peer_ecpointformatlist = NULL;
1472 s->s3->tmp.peer_ecpointformatlist_length = 0;
1473 }
1474 if (s->s3->tmp.peer_ellipticcurvelist != 0)
1475 {
1476 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1477 s->s3->tmp.peer_ellipticcurvelist = NULL;
1478 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1479 }
Adam Langley95c29f32014-06-20 12:00:00 -07001480
David Benjamindc72ff72014-06-25 12:36:10 -04001481 /* There may be no extensions. */
1482 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001483 {
David Benjamindc72ff72014-06-25 12:36:10 -04001484 goto ri_check;
1485 }
Adam Langley95c29f32014-06-20 12:00:00 -07001486
David Benjamin35a7a442014-07-05 00:23:20 -04001487 /* Decode the extensions block and check it is valid. */
1488 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1489 !tls1_check_duplicate_extensions(&extensions))
David Benjamindc72ff72014-06-25 12:36:10 -04001490 {
1491 *out_alert = SSL_AD_DECODE_ERROR;
1492 return 0;
1493 }
1494
David Benjamindc72ff72014-06-25 12:36:10 -04001495 while (CBS_len(&extensions) != 0)
1496 {
1497 uint16_t type;
1498 CBS extension;
1499
1500 /* Decode the next extension. */
1501 if (!CBS_get_u16(&extensions, &type) ||
1502 !CBS_get_u16_length_prefixed(&extensions, &extension))
1503 {
1504 *out_alert = SSL_AD_DECODE_ERROR;
1505 return 0;
1506 }
1507
Adam Langley95c29f32014-06-20 12:00:00 -07001508 if (s->tlsext_debug_cb)
David Benjamindc72ff72014-06-25 12:36:10 -04001509 {
1510 s->tlsext_debug_cb(s, 0, type, (unsigned char*)CBS_data(&extension),
1511 CBS_len(&extension), s->tlsext_debug_arg);
1512 }
1513
Adam Langley95c29f32014-06-20 12:00:00 -07001514/* The servername extension is treated as follows:
1515
1516 - Only the hostname type is supported with a maximum length of 255.
1517 - The servername is rejected if too long or if it contains zeros,
1518 in which case an fatal alert is generated.
1519 - The servername field is maintained together with the session cache.
1520 - When a session is resumed, the servername call back invoked in order
1521 to allow the application to position itself to the right context.
1522 - The servername is acknowledged if it is new for a session or when
1523 it is identical to a previously used for the same session.
1524 Applications can control the behaviour. They can at any time
1525 set a 'desirable' servername for a new SSL object. This can be the
1526 case for example with HTTPS when a Host: header field is received and
1527 a renegotiation is requested. In this case, a possible servername
1528 presented in the new client hello is only acknowledged if it matches
1529 the value of the Host: field.
1530 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1531 if they provide for changing an explicit servername context for the session,
1532 i.e. when the session has been established with a servername extension.
1533 - On session reconnect, the servername extension may be absent.
1534
1535*/
1536
1537 if (type == TLSEXT_TYPE_server_name)
1538 {
David Benjamindc72ff72014-06-25 12:36:10 -04001539 CBS server_name_list;
Adam Langleyed8270a2014-09-02 13:52:56 -07001540 char have_seen_host_name = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001541
1542 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1543 CBS_len(&server_name_list) < 1 ||
1544 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001545 {
David Benjamindc72ff72014-06-25 12:36:10 -04001546 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001547 return 0;
1548 }
Adam Langley95c29f32014-06-20 12:00:00 -07001549
David Benjamindc72ff72014-06-25 12:36:10 -04001550 /* Decode each ServerName in the extension. */
1551 while (CBS_len(&server_name_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001552 {
David Benjamindc72ff72014-06-25 12:36:10 -04001553 uint8_t name_type;
1554 CBS host_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001555
David Benjamindc72ff72014-06-25 12:36:10 -04001556 /* Decode the NameType. */
1557 if (!CBS_get_u8(&server_name_list, &name_type))
Adam Langley95c29f32014-06-20 12:00:00 -07001558 {
David Benjamindc72ff72014-06-25 12:36:10 -04001559 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001560 return 0;
1561 }
David Benjamindc72ff72014-06-25 12:36:10 -04001562
David Benjamindc72ff72014-06-25 12:36:10 -04001563 /* Only host_name is supported. */
1564 if (name_type != TLSEXT_NAMETYPE_host_name)
1565 continue;
1566
Adam Langleyed8270a2014-09-02 13:52:56 -07001567 if (have_seen_host_name)
1568 {
1569 /* The ServerNameList MUST NOT contain
1570 * more than one name of the same
1571 * name_type. */
1572 *out_alert = SSL_AD_DECODE_ERROR;
1573 return 0;
1574 }
1575
1576 have_seen_host_name = 1;
1577
1578 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1579 CBS_len(&host_name) < 1)
1580 {
1581 *out_alert = SSL_AD_DECODE_ERROR;
1582 return 0;
1583 }
1584
1585 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1586 CBS_contains_zero_byte(&host_name))
1587 {
1588 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1589 return 0;
1590 }
1591
David Benjamindc72ff72014-06-25 12:36:10 -04001592 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -07001593 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001594 assert(s->session->tlsext_hostname == NULL);
David Benjamindc72ff72014-06-25 12:36:10 -04001595 if (s->session->tlsext_hostname)
Adam Langley95c29f32014-06-20 12:00:00 -07001596 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001597 /* This should be impossible. */
David Benjamindc72ff72014-06-25 12:36:10 -04001598 *out_alert = SSL_AD_DECODE_ERROR;
1599 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001600 }
Adam Langley95c29f32014-06-20 12:00:00 -07001601
David Benjamindc72ff72014-06-25 12:36:10 -04001602 /* Copy the hostname as a string. */
David Benjamined439582014-07-14 19:13:02 -04001603 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname))
David Benjamindc72ff72014-06-25 12:36:10 -04001604 {
1605 *out_alert = SSL_AD_INTERNAL_ERROR;
1606 return 0;
1607 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001608
1609 s->should_ack_sni = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001610 }
Adam Langley95c29f32014-06-20 12:00:00 -07001611 }
Adam Langley95c29f32014-06-20 12:00:00 -07001612 }
1613
Adam Langley95c29f32014-06-20 12:00:00 -07001614 else if (type == TLSEXT_TYPE_ec_point_formats)
1615 {
David Benjamindc72ff72014-06-25 12:36:10 -04001616 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001617
David Benjamindc72ff72014-06-25 12:36:10 -04001618 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1619 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001620 {
David Benjamindc72ff72014-06-25 12:36:10 -04001621 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001622 return 0;
1623 }
David Benjamindc72ff72014-06-25 12:36:10 -04001624
David Benjamina19fc252014-10-19 00:14:36 -04001625 if (!CBS_stow(&ec_point_format_list,
1626 &s->s3->tmp.peer_ecpointformatlist,
1627 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001628 {
David Benjamina19fc252014-10-19 00:14:36 -04001629 *out_alert = SSL_AD_INTERNAL_ERROR;
1630 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001631 }
Adam Langley95c29f32014-06-20 12:00:00 -07001632 }
1633 else if (type == TLSEXT_TYPE_elliptic_curves)
1634 {
David Benjamindc72ff72014-06-25 12:36:10 -04001635 CBS elliptic_curve_list;
David Benjamin072334d2014-07-13 16:24:27 -04001636 size_t i, num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001637
David Benjamindc72ff72014-06-25 12:36:10 -04001638 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
David Benjamin072334d2014-07-13 16:24:27 -04001639 CBS_len(&elliptic_curve_list) == 0 ||
1640 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
David Benjamindc72ff72014-06-25 12:36:10 -04001641 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001642 {
David Benjamindc72ff72014-06-25 12:36:10 -04001643 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001644 return 0;
1645 }
David Benjamindc72ff72014-06-25 12:36:10 -04001646
David Benjamina19fc252014-10-19 00:14:36 -04001647 if (s->s3->tmp.peer_ellipticcurvelist)
Adam Langley95c29f32014-06-20 12:00:00 -07001648 {
David Benjamina19fc252014-10-19 00:14:36 -04001649 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1650 s->s3->tmp.peer_ellipticcurvelist_length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001651 }
David Benjamina19fc252014-10-19 00:14:36 -04001652 s->s3->tmp.peer_ellipticcurvelist =
1653 (uint16_t*)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
1654 if (s->s3->tmp.peer_ellipticcurvelist == NULL)
1655 {
1656 *out_alert = SSL_AD_INTERNAL_ERROR;
1657 return 0;
1658 }
1659 num_curves = CBS_len(&elliptic_curve_list) / 2;
1660 for (i = 0; i < num_curves; i++)
1661 {
1662 if (!CBS_get_u16(&elliptic_curve_list,
1663 &s->s3->tmp.peer_ellipticcurvelist[i]))
1664 {
1665 *out_alert = SSL_AD_INTERNAL_ERROR;
1666 return 0;
1667 }
1668 }
1669 if (CBS_len(&elliptic_curve_list) != 0)
1670 {
1671 *out_alert = SSL_AD_INTERNAL_ERROR;
1672 return 0;
1673 }
1674 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001675 }
Adam Langley95c29f32014-06-20 12:00:00 -07001676 else if (type == TLSEXT_TYPE_session_ticket)
1677 {
1678 if (s->tls_session_ticket_ext_cb &&
David Benjamindc72ff72014-06-25 12:36:10 -04001679 !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 -07001680 {
David Benjamindc72ff72014-06-25 12:36:10 -04001681 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001682 return 0;
1683 }
1684 }
1685 else if (type == TLSEXT_TYPE_renegotiate)
1686 {
David Benjamindc72ff72014-06-25 12:36:10 -04001687 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001688 return 0;
1689 renegotiate_seen = 1;
1690 }
1691 else if (type == TLSEXT_TYPE_signature_algorithms)
1692 {
David Benjamindc72ff72014-06-25 12:36:10 -04001693 CBS supported_signature_algorithms;
1694
David Benjamindc72ff72014-06-25 12:36:10 -04001695 if (!CBS_get_u16_length_prefixed(&extension, &supported_signature_algorithms) ||
1696 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001697 {
David Benjamindc72ff72014-06-25 12:36:10 -04001698 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001699 return 0;
1700 }
David Benjamindc72ff72014-06-25 12:36:10 -04001701
1702 /* Ensure the signature algorithms are non-empty. It
1703 * contains a list of SignatureAndHashAlgorithms
1704 * which are two bytes each. */
1705 if (CBS_len(&supported_signature_algorithms) == 0 ||
1706 (CBS_len(&supported_signature_algorithms) % 2) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001707 {
David Benjamindc72ff72014-06-25 12:36:10 -04001708 *out_alert = SSL_AD_DECODE_ERROR;
1709 return 0;
1710 }
1711
David Benjamincd996942014-07-20 16:23:51 -04001712 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
David Benjamindc72ff72014-06-25 12:36:10 -04001713 {
1714 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001715 return 0;
1716 }
1717 /* If sigalgs received and no shared algorithms fatal
1718 * error.
1719 */
1720 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
1721 {
1722 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
David Benjamindc72ff72014-06-25 12:36:10 -04001723 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07001724 return 0;
1725 }
1726 }
1727
Adam Langley95c29f32014-06-20 12:00:00 -07001728 else if (type == TLSEXT_TYPE_next_proto_neg &&
1729 s->s3->tmp.finish_md_len == 0 &&
1730 s->s3->alpn_selected == NULL)
1731 {
David Benjamindc72ff72014-06-25 12:36:10 -04001732 /* The extension must be empty. */
1733 if (CBS_len(&extension) != 0)
1734 {
1735 *out_alert = SSL_AD_DECODE_ERROR;
1736 return 0;
1737 }
1738
Adam Langley95c29f32014-06-20 12:00:00 -07001739 /* We shouldn't accept this extension on a
1740 * renegotiation.
1741 *
1742 * s->new_session will be set on renegotiation, but we
1743 * probably shouldn't rely that it couldn't be set on
1744 * the initial renegotation too in certain cases (when
1745 * there's some other reason to disallow resuming an
1746 * earlier session -- the current code won't be doing
1747 * anything like that, but this might change).
1748
1749 * A valid sign that there's been a previous handshake
1750 * in this connection is if s->s3->tmp.finish_md_len >
1751 * 0. (We are talking about a check that will happen
1752 * in the Hello protocol round, well before a new
1753 * Finished message could have been computed.) */
1754 s->s3->next_proto_neg_seen = 1;
1755 }
Adam Langley95c29f32014-06-20 12:00:00 -07001756
1757 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1758 s->ctx->alpn_select_cb &&
1759 s->s3->tmp.finish_md_len == 0)
1760 {
David Benjamindc72ff72014-06-25 12:36:10 -04001761 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001762 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001763 /* ALPN takes precedence over NPN. */
1764 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001765 }
1766
Adam Langley1258b6a2014-06-20 12:00:00 -07001767 else if (type == TLSEXT_TYPE_channel_id &&
1768 s->tlsext_channel_id_enabled)
David Benjamindc72ff72014-06-25 12:36:10 -04001769 {
1770 /* The extension must be empty. */
1771 if (CBS_len(&extension) != 0)
1772 {
1773 *out_alert = SSL_AD_DECODE_ERROR;
1774 return 0;
1775 }
1776
Adam Langley1258b6a2014-06-20 12:00:00 -07001777 s->s3->tlsext_channel_id_valid = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001778 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001779
1780 else if (type == TLSEXT_TYPE_channel_id_new &&
1781 s->tlsext_channel_id_enabled)
1782 {
David Benjamindc72ff72014-06-25 12:36:10 -04001783 /* The extension must be empty. */
1784 if (CBS_len(&extension) != 0)
1785 {
1786 *out_alert = SSL_AD_DECODE_ERROR;
1787 return 0;
1788 }
1789
Adam Langley1258b6a2014-06-20 12:00:00 -07001790 s->s3->tlsext_channel_id_valid = 1;
1791 s->s3->tlsext_channel_id_new = 1;
1792 }
1793
1794
Adam Langley95c29f32014-06-20 12:00:00 -07001795 /* session ticket processed earlier */
1796 else if (type == TLSEXT_TYPE_use_srtp)
1797 {
David Benjamindc72ff72014-06-25 12:36:10 -04001798 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001799 return 0;
1800 }
Adam Langley75712922014-10-10 16:23:43 -07001801
1802 else if (type == TLSEXT_TYPE_extended_master_secret &&
1803 s->version != SSL3_VERSION)
1804 {
1805 if (CBS_len(&extension) != 0)
1806 {
1807 *out_alert = SSL_AD_DECODE_ERROR;
1808 return 0;
1809 }
1810
1811 s->s3->tmp.extended_master_secret = 1;
1812 }
Adam Langley95c29f32014-06-20 12:00:00 -07001813 }
1814
Adam Langley95c29f32014-06-20 12:00:00 -07001815 ri_check:
1816
1817 /* Need RI if renegotiating */
1818
1819 if (!renegotiate_seen && s->renegotiate &&
1820 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1821 {
David Benjamindc72ff72014-06-25 12:36:10 -04001822 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin172fc2c2014-09-06 13:09:47 -04001823 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07001824 return 0;
1825 }
1826 /* If no signature algorithms extension set default values */
1827 if (!s->cert->peer_sigalgs)
1828 ssl_cert_set_default_md(s->cert);
1829
1830 return 1;
1831 }
1832
David Benjamindc72ff72014-06-25 12:36:10 -04001833int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001834 {
David Benjamindc72ff72014-06-25 12:36:10 -04001835 int alert = -1;
1836 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001837 {
David Benjamindc72ff72014-06-25 12:36:10 -04001838 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07001839 return 0;
1840 }
1841
David Benjamin6c7aed02014-08-27 16:42:38 -04001842 if (ssl_check_clienthello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001843 {
David Benjamin9d28c752014-07-05 00:43:48 -04001844 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07001845 return 0;
1846 }
1847 return 1;
David Benjamin072334d2014-07-13 16:24:27 -04001848 }
Adam Langley95c29f32014-06-20 12:00:00 -07001849
Adam Langley95c29f32014-06-20 12:00:00 -07001850/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1851 * elements of zero length are allowed and the set of elements must exactly fill
1852 * the length of the block. */
David Benjamin03973092014-06-24 23:27:17 -04001853static char ssl_next_proto_validate(const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001854 {
David Benjamin03973092014-06-24 23:27:17 -04001855 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001856
David Benjamin03973092014-06-24 23:27:17 -04001857 while (CBS_len(&copy) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001858 {
David Benjamin03973092014-06-24 23:27:17 -04001859 CBS proto;
1860 if (!CBS_get_u8_length_prefixed(&copy, &proto) ||
1861 CBS_len(&proto) == 0)
1862 {
Adam Langley95c29f32014-06-20 12:00:00 -07001863 return 0;
David Benjamin03973092014-06-24 23:27:17 -04001864 }
Adam Langley95c29f32014-06-20 12:00:00 -07001865 }
David Benjamin03973092014-06-24 23:27:17 -04001866 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001867 }
Adam Langley95c29f32014-06-20 12:00:00 -07001868
David Benjamin03973092014-06-24 23:27:17 -04001869static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001870 {
Adam Langley95c29f32014-06-20 12:00:00 -07001871 int tlsext_servername = 0;
1872 int renegotiate_seen = 0;
David Benjamin03973092014-06-24 23:27:17 -04001873 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001874
David Benjamin6c7aed02014-08-27 16:42:38 -04001875 /* TODO(davidben): Move all of these to some per-handshake state that
1876 * gets systematically reset on a new handshake; perhaps allocate it
1877 * fresh each time so it's not even kept around post-handshake. */
Adam Langley95c29f32014-06-20 12:00:00 -07001878 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001879
David Benjamin6c7aed02014-08-27 16:42:38 -04001880 s->tlsext_ticket_expected = 0;
1881 s->s3->tmp.certificate_status_expected = 0;
Adam Langley75712922014-10-10 16:23:43 -07001882 s->s3->tmp.extended_master_secret = 0;
David Benjamin64442872014-07-21 17:43:45 -04001883
Adam Langley95c29f32014-06-20 12:00:00 -07001884 if (s->s3->alpn_selected)
1885 {
1886 OPENSSL_free(s->s3->alpn_selected);
1887 s->s3->alpn_selected = NULL;
1888 }
1889
David Benjamina19fc252014-10-19 00:14:36 -04001890 /* Clear ECC extensions */
1891 if (s->s3->tmp.peer_ecpointformatlist != 0)
1892 {
1893 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1894 s->s3->tmp.peer_ecpointformatlist = NULL;
1895 s->s3->tmp.peer_ecpointformatlist_length = 0;
1896 }
1897
David Benjamin03973092014-06-24 23:27:17 -04001898 /* There may be no extensions. */
1899 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001900 {
David Benjamin03973092014-06-24 23:27:17 -04001901 goto ri_check;
1902 }
1903
David Benjamin35a7a442014-07-05 00:23:20 -04001904 /* Decode the extensions block and check it is valid. */
1905 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1906 !tls1_check_duplicate_extensions(&extensions))
David Benjamin03973092014-06-24 23:27:17 -04001907 {
1908 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001909 return 0;
1910 }
1911
David Benjamin03973092014-06-24 23:27:17 -04001912 while (CBS_len(&extensions) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001913 {
David Benjamin03973092014-06-24 23:27:17 -04001914 uint16_t type;
1915 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001916
David Benjamin03973092014-06-24 23:27:17 -04001917 /* Decode the next extension. */
1918 if (!CBS_get_u16(&extensions, &type) ||
1919 !CBS_get_u16_length_prefixed(&extensions, &extension))
1920 {
1921 *out_alert = SSL_AD_DECODE_ERROR;
1922 return 0;
1923 }
Adam Langley95c29f32014-06-20 12:00:00 -07001924
1925 if (s->tlsext_debug_cb)
David Benjamin03973092014-06-24 23:27:17 -04001926 {
1927 s->tlsext_debug_cb(s, 1, type, (unsigned char*)CBS_data(&extension),
1928 CBS_len(&extension), s->tlsext_debug_arg);
1929 }
Adam Langley95c29f32014-06-20 12:00:00 -07001930
1931 if (type == TLSEXT_TYPE_server_name)
1932 {
David Benjamin03973092014-06-24 23:27:17 -04001933 /* The extension must be empty. */
1934 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001935 {
David Benjamin03973092014-06-24 23:27:17 -04001936 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001937 return 0;
1938 }
David Benjamin03973092014-06-24 23:27:17 -04001939 /* We must have sent it in ClientHello. */
1940 if (s->tlsext_hostname == NULL)
1941 {
1942 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1943 return 0;
1944 }
1945 tlsext_servername = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001946 }
Adam Langley95c29f32014-06-20 12:00:00 -07001947 else if (type == TLSEXT_TYPE_ec_point_formats)
1948 {
David Benjamin03973092014-06-24 23:27:17 -04001949 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001950
David Benjamin03973092014-06-24 23:27:17 -04001951 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1952 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001953 {
David Benjamin03973092014-06-24 23:27:17 -04001954 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001955 return 0;
1956 }
David Benjamin03973092014-06-24 23:27:17 -04001957
David Benjamina19fc252014-10-19 00:14:36 -04001958 if (!CBS_stow(&ec_point_format_list,
1959 &s->s3->tmp.peer_ecpointformatlist,
1960 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001961 {
David Benjamina19fc252014-10-19 00:14:36 -04001962 *out_alert = SSL_AD_INTERNAL_ERROR;
1963 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001964 }
Adam Langley95c29f32014-06-20 12:00:00 -07001965 }
Adam Langley95c29f32014-06-20 12:00:00 -07001966 else if (type == TLSEXT_TYPE_session_ticket)
1967 {
1968 if (s->tls_session_ticket_ext_cb &&
David Benjamin03973092014-06-24 23:27:17 -04001969 !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension),
1970 s->tls_session_ticket_ext_cb_arg))
Adam Langley95c29f32014-06-20 12:00:00 -07001971 {
David Benjamin03973092014-06-24 23:27:17 -04001972 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001973 return 0;
1974 }
David Benjamin03973092014-06-24 23:27:17 -04001975
1976 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001977 {
David Benjamin03973092014-06-24 23:27:17 -04001978 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07001979 return 0;
1980 }
David Benjamin03973092014-06-24 23:27:17 -04001981
Adam Langley95c29f32014-06-20 12:00:00 -07001982 s->tlsext_ticket_expected = 1;
1983 }
Adam Langley95c29f32014-06-20 12:00:00 -07001984 else if (type == TLSEXT_TYPE_status_request)
1985 {
David Benjamin03973092014-06-24 23:27:17 -04001986 /* The extension MUST be empty and may only sent if
1987 * we've requested a status request message. */
1988 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001989 {
David Benjamin03973092014-06-24 23:27:17 -04001990 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001991 return 0;
1992 }
David Benjamin6c7aed02014-08-27 16:42:38 -04001993 if (!s->ocsp_stapling_enabled)
David Benjamin03973092014-06-24 23:27:17 -04001994 {
1995 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1996 return 0;
1997 }
1998 /* Set a flag to expect a CertificateStatus message */
David Benjamin6c7aed02014-08-27 16:42:38 -04001999 s->s3->tmp.certificate_status_expected = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002000 }
David Benjamin03973092014-06-24 23:27:17 -04002001 else if (type == TLSEXT_TYPE_next_proto_neg && s->s3->tmp.finish_md_len == 0) {
2002 unsigned char *selected;
2003 unsigned char selected_len;
2004
2005 /* We must have requested it. */
2006 if (s->ctx->next_proto_select_cb == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002007 {
David Benjamin03973092014-06-24 23:27:17 -04002008 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2009 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002010 }
Adam Langley95c29f32014-06-20 12:00:00 -07002011
David Benjamin03973092014-06-24 23:27:17 -04002012 /* The data must be valid. */
2013 if (!ssl_next_proto_validate(&extension))
2014 {
2015 *out_alert = SSL_AD_DECODE_ERROR;
2016 return 0;
2017 }
2018
2019 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len,
2020 CBS_data(&extension), CBS_len(&extension),
2021 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
2022 {
2023 *out_alert = SSL_AD_INTERNAL_ERROR;
2024 return 0;
2025 }
2026
2027 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
2028 if (s->next_proto_negotiated == NULL)
2029 {
2030 *out_alert = SSL_AD_INTERNAL_ERROR;
2031 return 0;
2032 }
2033 s->next_proto_negotiated_len = selected_len;
2034 s->s3->next_proto_neg_seen = 1;
2035 }
Adam Langley95c29f32014-06-20 12:00:00 -07002036 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
2037 {
David Benjamin03973092014-06-24 23:27:17 -04002038 CBS protocol_name_list, protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07002039
2040 /* We must have requested it. */
2041 if (s->alpn_client_proto_list == NULL)
2042 {
David Benjamin03973092014-06-24 23:27:17 -04002043 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07002044 return 0;
2045 }
David Benjamin03973092014-06-24 23:27:17 -04002046
2047 /* The extension data consists of a ProtocolNameList
2048 * which must have exactly one ProtocolName. Each of
2049 * these is length-prefixed. */
2050 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
2051 CBS_len(&extension) != 0 ||
2052 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
2053 CBS_len(&protocol_name_list) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002054 {
David Benjamin03973092014-06-24 23:27:17 -04002055 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002056 return 0;
2057 }
David Benjamin03973092014-06-24 23:27:17 -04002058
2059 if (!CBS_stow(&protocol_name,
2060 &s->s3->alpn_selected,
2061 &s->s3->alpn_selected_len))
Adam Langley95c29f32014-06-20 12:00:00 -07002062 {
David Benjamin03973092014-06-24 23:27:17 -04002063 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002064 return 0;
2065 }
Adam Langley95c29f32014-06-20 12:00:00 -07002066 }
2067
Adam Langley1258b6a2014-06-20 12:00:00 -07002068 else if (type == TLSEXT_TYPE_channel_id)
David Benjamin03973092014-06-24 23:27:17 -04002069 {
2070 if (CBS_len(&extension) != 0)
2071 {
2072 *out_alert = SSL_AD_DECODE_ERROR;
2073 return 0;
2074 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002075 s->s3->tlsext_channel_id_valid = 1;
David Benjamin03973092014-06-24 23:27:17 -04002076 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002077 else if (type == TLSEXT_TYPE_channel_id_new)
2078 {
David Benjamin03973092014-06-24 23:27:17 -04002079 if (CBS_len(&extension) != 0)
2080 {
2081 *out_alert = SSL_AD_DECODE_ERROR;
2082 return 0;
2083 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002084 s->s3->tlsext_channel_id_valid = 1;
2085 s->s3->tlsext_channel_id_new = 1;
2086 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002087 else if (type == TLSEXT_TYPE_certificate_timestamp)
2088 {
2089 if (CBS_len(&extension) == 0)
2090 {
2091 *out_alert = SSL_AD_DECODE_ERROR;
2092 return 0;
2093 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002094
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002095 /* Session resumption uses the original session information. */
2096 if (!s->hit)
2097 {
2098 if (!CBS_stow(&extension,
2099 &s->session->tlsext_signed_cert_timestamp_list,
2100 &s->session->tlsext_signed_cert_timestamp_list_length))
2101 {
2102 *out_alert = SSL_AD_INTERNAL_ERROR;
2103 return 0;
2104 }
2105 }
2106 }
Adam Langley95c29f32014-06-20 12:00:00 -07002107 else if (type == TLSEXT_TYPE_renegotiate)
2108 {
David Benjamin03973092014-06-24 23:27:17 -04002109 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002110 return 0;
2111 renegotiate_seen = 1;
2112 }
Adam Langley95c29f32014-06-20 12:00:00 -07002113 else if (type == TLSEXT_TYPE_use_srtp)
2114 {
David Benjamin03973092014-06-24 23:27:17 -04002115 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002116 return 0;
2117 }
Adam Langley75712922014-10-10 16:23:43 -07002118
2119 else if (type == TLSEXT_TYPE_extended_master_secret)
2120 {
2121 if (/* It is invalid for the server to select EMS and
2122 SSLv3. */
2123 s->version == SSL3_VERSION ||
2124 CBS_len(&extension) != 0)
2125 {
2126 *out_alert = SSL_AD_DECODE_ERROR;
2127 return 0;
2128 }
2129
2130 s->s3->tmp.extended_master_secret = 1;
2131 }
Adam Langley95c29f32014-06-20 12:00:00 -07002132 }
2133
2134 if (!s->hit && tlsext_servername == 1)
2135 {
2136 if (s->tlsext_hostname)
2137 {
2138 if (s->session->tlsext_hostname == NULL)
2139 {
2140 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2141 if (!s->session->tlsext_hostname)
2142 {
David Benjamin03973092014-06-24 23:27:17 -04002143 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002144 return 0;
2145 }
2146 }
2147 else
2148 {
David Benjamin03973092014-06-24 23:27:17 -04002149 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002150 return 0;
2151 }
2152 }
2153 }
2154
Adam Langley95c29f32014-06-20 12:00:00 -07002155 ri_check:
2156
2157 /* Determine if we need to see RI. Strictly speaking if we want to
2158 * avoid an attack we should *always* see RI even on initial server
2159 * hello because the client doesn't see any renegotiation during an
2160 * attack. However this would mean we could not connect to any server
2161 * which doesn't support RI so for the immediate future tolerate RI
2162 * absence on initial connect only.
2163 */
2164 if (!renegotiate_seen
2165 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2166 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2167 {
David Benjamin03973092014-06-24 23:27:17 -04002168 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin9d28c752014-07-05 00:43:48 -04002169 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07002170 return 0;
2171 }
2172
2173 return 1;
2174 }
2175
2176
2177int ssl_prepare_clienthello_tlsext(SSL *s)
2178 {
Adam Langley95c29f32014-06-20 12:00:00 -07002179 return 1;
2180 }
2181
2182int ssl_prepare_serverhello_tlsext(SSL *s)
2183 {
2184 return 1;
2185 }
2186
David Benjamin6c7aed02014-08-27 16:42:38 -04002187static int ssl_check_clienthello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002188 {
2189 int ret=SSL_TLSEXT_ERR_NOACK;
2190 int al = SSL_AD_UNRECOGNIZED_NAME;
2191
Adam Langley95c29f32014-06-20 12:00:00 -07002192 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2193 * ssl3_choose_cipher in s3_lib.c.
2194 */
2195 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2196 * ssl3_choose_cipher in s3_lib.c.
2197 */
Adam Langley95c29f32014-06-20 12:00:00 -07002198
2199 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2200 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2201 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2202 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2203
Adam Langley95c29f32014-06-20 12:00:00 -07002204 switch (ret)
2205 {
2206 case SSL_TLSEXT_ERR_ALERT_FATAL:
2207 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2208 return -1;
2209
2210 case SSL_TLSEXT_ERR_ALERT_WARNING:
2211 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002212 return 1;
2213
Adam Langley95c29f32014-06-20 12:00:00 -07002214 case SSL_TLSEXT_ERR_NOACK:
Adam Langleyed8270a2014-09-02 13:52:56 -07002215 s->should_ack_sni = 0;
2216 return 1;
2217
2218 default:
2219 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002220 }
2221 }
2222
David Benjamin6c7aed02014-08-27 16:42:38 -04002223static int ssl_check_serverhello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002224 {
2225 int ret=SSL_TLSEXT_ERR_NOACK;
2226 int al = SSL_AD_UNRECOGNIZED_NAME;
2227
Adam Langley95c29f32014-06-20 12:00:00 -07002228 /* If we are client and using an elliptic curve cryptography cipher
2229 * suite, then if server returns an EC point formats lists extension
2230 * it must contain uncompressed.
2231 */
2232 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2233 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamina9ca90a2014-09-26 18:53:43 -04002234 if (((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) &&
2235 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed))
Adam Langley95c29f32014-06-20 12:00:00 -07002236 {
David Benjamina9ca90a2014-09-26 18:53:43 -04002237 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2238 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002239 }
2240 ret = SSL_TLSEXT_ERR_OK;
Adam Langley95c29f32014-06-20 12:00:00 -07002241
2242 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2243 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2244 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2245 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2246
Adam Langley95c29f32014-06-20 12:00:00 -07002247 switch (ret)
2248 {
2249 case SSL_TLSEXT_ERR_ALERT_FATAL:
Adam Langleyed8270a2014-09-02 13:52:56 -07002250 ssl3_send_alert(s,SSL3_AL_FATAL,al);
Adam Langley95c29f32014-06-20 12:00:00 -07002251 return -1;
2252
2253 case SSL_TLSEXT_ERR_ALERT_WARNING:
2254 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002255 return 1;
2256
2257 default:
2258 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002259 }
2260 }
2261
David Benjamin03973092014-06-24 23:27:17 -04002262int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07002263 {
David Benjamin03973092014-06-24 23:27:17 -04002264 int alert = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002265 if (s->version < SSL3_VERSION)
2266 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002267
2268 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002269 {
David Benjamin03973092014-06-24 23:27:17 -04002270 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07002271 return 0;
2272 }
2273
David Benjamin03973092014-06-24 23:27:17 -04002274 if (ssl_check_serverhello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002275 {
David Benjamin9d28c752014-07-05 00:43:48 -04002276 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, SSL_R_SERVERHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07002277 return 0;
2278 }
David Benjamin03973092014-06-24 23:27:17 -04002279
Adam Langley95c29f32014-06-20 12:00:00 -07002280 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002281 }
Adam Langley95c29f32014-06-20 12:00:00 -07002282
2283/* Since the server cache lookup is done early on in the processing of the
2284 * ClientHello, and other operations depend on the result, we need to handle
2285 * any TLS session ticket extension at the same time.
2286 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002287 * ctx: contains the early callback context, which is the result of a
2288 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002289 * ret: (output) on return, if a ticket was decrypted, then this is set to
2290 * point to the resulting session.
2291 *
2292 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2293 * ciphersuite, in which case we have no use for session tickets and one will
2294 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2295 *
2296 * Returns:
2297 * -1: fatal error, either from parsing or decrypting the ticket.
2298 * 0: no ticket was found (or was ignored, based on settings).
2299 * 1: a zero length extension was found, indicating that the client supports
2300 * session tickets but doesn't currently have one to offer.
2301 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2302 * couldn't be decrypted because of a non-fatal error.
2303 * 3: a ticket was successfully decrypted and *ret was set.
2304 *
2305 * Side effects:
2306 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2307 * a new session ticket to the client because the client indicated support
2308 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2309 * a session ticket or we couldn't use the one it gave us, or if
2310 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2311 * Otherwise, s->tlsext_ticket_expected is set to 0.
2312 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002313int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
2314 SSL_SESSION **ret)
Adam Langley95c29f32014-06-20 12:00:00 -07002315 {
Adam Langley95c29f32014-06-20 12:00:00 -07002316 *ret = NULL;
2317 s->tlsext_ticket_expected = 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002318 const unsigned char *data;
2319 size_t len;
2320 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002321
2322 /* If tickets disabled behave as if no ticket present
2323 * to permit stateful resumption.
2324 */
2325 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2326 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002327 if ((s->version <= SSL3_VERSION) && !ctx->extensions)
Adam Langley95c29f32014-06-20 12:00:00 -07002328 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002329 if (!SSL_early_callback_ctx_extension_get(
2330 ctx, TLSEXT_TYPE_session_ticket, &data, &len))
Adam Langley95c29f32014-06-20 12:00:00 -07002331 {
Adam Langleydc9b1412014-06-20 12:00:00 -07002332 return 0;
2333 }
2334 if (len == 0)
2335 {
2336 /* The client will accept a ticket but doesn't
2337 * currently have one. */
2338 s->tlsext_ticket_expected = 1;
2339 return 1;
2340 }
2341 if (s->tls_session_secret_cb)
2342 {
2343 /* Indicate that the ticket couldn't be
2344 * decrypted rather than generating the session
2345 * from ticket now, trigger abbreviated
2346 * handshake based on external mechanism to
2347 * calculate the master secret later. */
2348 return 2;
2349 }
2350 r = tls_decrypt_ticket(s, data, len, ctx->session_id,
2351 ctx->session_id_len, ret);
2352 switch (r)
2353 {
2354 case 2: /* ticket couldn't be decrypted */
2355 s->tlsext_ticket_expected = 1;
2356 return 2;
2357 case 3: /* ticket was decrypted */
2358 return r;
2359 case 4: /* ticket decrypted but need to renew */
2360 s->tlsext_ticket_expected = 1;
2361 return 3;
2362 default: /* fatal error */
Adam Langley95c29f32014-06-20 12:00:00 -07002363 return -1;
2364 }
Adam Langley95c29f32014-06-20 12:00:00 -07002365 }
2366
2367/* tls_decrypt_ticket attempts to decrypt a session ticket.
2368 *
2369 * etick: points to the body of the session ticket extension.
2370 * eticklen: the length of the session tickets extenion.
2371 * sess_id: points at the session ID.
2372 * sesslen: the length of the session ID.
2373 * psess: (output) on return, if a ticket was decrypted, then this is set to
2374 * point to the resulting session.
2375 *
2376 * Returns:
2377 * -1: fatal error, either from parsing or decrypting the ticket.
2378 * 2: the ticket couldn't be decrypted.
2379 * 3: a ticket was successfully decrypted and *psess was set.
2380 * 4: same as 3, but the ticket needs to be renewed.
2381 */
2382static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
2383 const unsigned char *sess_id, int sesslen,
2384 SSL_SESSION **psess)
2385 {
2386 SSL_SESSION *sess;
2387 unsigned char *sdec;
2388 const unsigned char *p;
2389 int slen, mlen, renew_ticket = 0;
2390 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2391 HMAC_CTX hctx;
2392 EVP_CIPHER_CTX ctx;
2393 SSL_CTX *tctx = s->initial_ctx;
2394 /* Need at least keyname + iv + some encrypted data */
2395 if (eticklen < 48)
2396 return 2;
2397 /* Initialize session ticket encryption and HMAC contexts */
2398 HMAC_CTX_init(&hctx);
2399 EVP_CIPHER_CTX_init(&ctx);
2400 if (tctx->tlsext_ticket_key_cb)
2401 {
2402 unsigned char *nctick = (unsigned char *)etick;
2403 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
2404 &ctx, &hctx, 0);
2405 if (rv < 0)
2406 return -1;
2407 if (rv == 0)
2408 return 2;
2409 if (rv == 2)
2410 renew_ticket = 1;
2411 }
2412 else
2413 {
2414 /* Check key name matches */
2415 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
2416 return 2;
2417 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2418 tlsext_tick_md(), NULL);
2419 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2420 tctx->tlsext_tick_aes_key, etick + 16);
2421 }
2422 /* Attempt to process session ticket, first conduct sanity and
2423 * integrity checks on ticket.
2424 */
2425 mlen = HMAC_size(&hctx);
2426 if (mlen < 0)
2427 {
2428 EVP_CIPHER_CTX_cleanup(&ctx);
2429 return -1;
2430 }
2431 eticklen -= mlen;
2432 /* Check HMAC of encrypted ticket */
2433 HMAC_Update(&hctx, etick, eticklen);
2434 HMAC_Final(&hctx, tick_hmac, NULL);
2435 HMAC_CTX_cleanup(&hctx);
2436 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
Adam Langley38311732014-10-16 19:04:35 -07002437 {
2438 EVP_CIPHER_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002439 return 2;
Adam Langley38311732014-10-16 19:04:35 -07002440 }
Adam Langley95c29f32014-06-20 12:00:00 -07002441 /* Attempt to decrypt session data */
2442 /* Move p after IV to start of encrypted ticket, update length */
2443 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2444 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2445 sdec = OPENSSL_malloc(eticklen);
2446 if (!sdec)
2447 {
2448 EVP_CIPHER_CTX_cleanup(&ctx);
2449 return -1;
2450 }
2451 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2452 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0)
Adam Langley3e148852014-07-24 17:34:02 -07002453 {
2454 EVP_CIPHER_CTX_cleanup(&ctx);
2455 OPENSSL_free(sdec);
Adam Langley95c29f32014-06-20 12:00:00 -07002456 return 2;
Adam Langley3e148852014-07-24 17:34:02 -07002457 }
Adam Langley95c29f32014-06-20 12:00:00 -07002458 slen += mlen;
2459 EVP_CIPHER_CTX_cleanup(&ctx);
2460 p = sdec;
2461
2462 sess = d2i_SSL_SESSION(NULL, &p, slen);
2463 OPENSSL_free(sdec);
2464 if (sess)
2465 {
2466 /* The session ID, if non-empty, is used by some clients to
2467 * detect that the ticket has been accepted. So we copy it to
2468 * the session structure. If it is empty set length to zero
2469 * as required by standard.
2470 */
2471 if (sesslen)
2472 memcpy(sess->session_id, sess_id, sesslen);
2473 sess->session_id_length = sesslen;
2474 *psess = sess;
2475 if (renew_ticket)
2476 return 4;
2477 else
2478 return 3;
2479 }
2480 ERR_clear_error();
2481 /* For session parse failure, indicate that we need to send a new
2482 * ticket. */
2483 return 2;
2484 }
2485
2486/* Tables to translate from NIDs to TLS v1.2 ids */
2487
2488typedef struct
2489 {
2490 int nid;
2491 int id;
2492 } tls12_lookup;
2493
David Benjamincff64722014-08-19 19:54:46 -04002494static const tls12_lookup tls12_md[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002495 {NID_md5, TLSEXT_hash_md5},
2496 {NID_sha1, TLSEXT_hash_sha1},
2497 {NID_sha224, TLSEXT_hash_sha224},
2498 {NID_sha256, TLSEXT_hash_sha256},
2499 {NID_sha384, TLSEXT_hash_sha384},
2500 {NID_sha512, TLSEXT_hash_sha512}
2501};
2502
David Benjamincff64722014-08-19 19:54:46 -04002503static const tls12_lookup tls12_sig[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002504 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
Adam Langley95c29f32014-06-20 12:00:00 -07002505 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
2506};
2507
David Benjamincff64722014-08-19 19:54:46 -04002508static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002509 {
2510 size_t i;
2511 for (i = 0; i < tlen; i++)
2512 {
2513 if (table[i].nid == nid)
2514 return table[i].id;
2515 }
2516 return -1;
2517 }
2518
David Benjamincff64722014-08-19 19:54:46 -04002519static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002520 {
2521 size_t i;
2522 for (i = 0; i < tlen; i++)
2523 {
2524 if ((table[i].id) == id)
2525 return table[i].nid;
2526 }
2527 return NID_undef;
2528 }
2529
2530int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
2531 {
2532 int sig_id, md_id;
2533 if (!md)
2534 return 0;
2535 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2536 sizeof(tls12_md)/sizeof(tls12_lookup));
2537 if (md_id == -1)
2538 return 0;
2539 sig_id = tls12_get_sigid(pk);
2540 if (sig_id == -1)
2541 return 0;
2542 p[0] = (unsigned char)md_id;
2543 p[1] = (unsigned char)sig_id;
2544 return 1;
2545 }
2546
2547int tls12_get_sigid(const EVP_PKEY *pk)
2548 {
2549 return tls12_find_id(pk->type, tls12_sig,
2550 sizeof(tls12_sig)/sizeof(tls12_lookup));
2551 }
2552
2553const EVP_MD *tls12_get_hash(unsigned char hash_alg)
2554 {
2555 switch(hash_alg)
2556 {
Adam Langley95c29f32014-06-20 12:00:00 -07002557 case TLSEXT_hash_md5:
Adam Langley95c29f32014-06-20 12:00:00 -07002558 return EVP_md5();
Adam Langley95c29f32014-06-20 12:00:00 -07002559 case TLSEXT_hash_sha1:
2560 return EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002561 case TLSEXT_hash_sha224:
2562 return EVP_sha224();
2563
2564 case TLSEXT_hash_sha256:
2565 return EVP_sha256();
Adam Langley95c29f32014-06-20 12:00:00 -07002566 case TLSEXT_hash_sha384:
2567 return EVP_sha384();
2568
2569 case TLSEXT_hash_sha512:
2570 return EVP_sha512();
Adam Langley95c29f32014-06-20 12:00:00 -07002571 default:
2572 return NULL;
2573
2574 }
2575 }
2576
2577static int tls12_get_pkey_idx(unsigned char sig_alg)
2578 {
2579 switch(sig_alg)
2580 {
Adam Langley95c29f32014-06-20 12:00:00 -07002581 case TLSEXT_signature_rsa:
2582 return SSL_PKEY_RSA_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002583 case TLSEXT_signature_ecdsa:
2584 return SSL_PKEY_ECC;
Adam Langley95c29f32014-06-20 12:00:00 -07002585 }
2586 return -1;
2587 }
2588
2589/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2590static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
2591 int *psignhash_nid, const unsigned char *data)
2592 {
2593 int sign_nid = 0, hash_nid = 0;
2594 if (!phash_nid && !psign_nid && !psignhash_nid)
2595 return;
2596 if (phash_nid || psignhash_nid)
2597 {
2598 hash_nid = tls12_find_nid(data[0], tls12_md,
2599 sizeof(tls12_md)/sizeof(tls12_lookup));
2600 if (phash_nid)
2601 *phash_nid = hash_nid;
2602 }
2603 if (psign_nid || psignhash_nid)
2604 {
2605 sign_nid = tls12_find_nid(data[1], tls12_sig,
2606 sizeof(tls12_sig)/sizeof(tls12_lookup));
2607 if (psign_nid)
2608 *psign_nid = sign_nid;
2609 }
2610 if (psignhash_nid)
2611 {
2612 if (sign_nid && hash_nid)
2613 OBJ_find_sigid_by_algs(psignhash_nid,
2614 hash_nid, sign_nid);
2615 else
2616 *psignhash_nid = NID_undef;
2617 }
2618 }
2619/* Given preference and allowed sigalgs set shared sigalgs */
2620static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
2621 const unsigned char *pref, size_t preflen,
2622 const unsigned char *allow, size_t allowlen)
2623 {
2624 const unsigned char *ptmp, *atmp;
2625 size_t i, j, nmatch = 0;
2626 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
2627 {
2628 /* Skip disabled hashes or signature algorithms */
2629 if (tls12_get_hash(ptmp[0]) == NULL)
2630 continue;
2631 if (tls12_get_pkey_idx(ptmp[1]) == -1)
2632 continue;
2633 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
2634 {
2635 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
2636 {
2637 nmatch++;
2638 if (shsig)
2639 {
2640 shsig->rhash = ptmp[0];
2641 shsig->rsign = ptmp[1];
2642 tls1_lookup_sigalg(&shsig->hash_nid,
2643 &shsig->sign_nid,
2644 &shsig->signandhash_nid,
2645 ptmp);
2646 shsig++;
2647 }
2648 break;
2649 }
2650 }
2651 }
2652 return nmatch;
2653 }
2654
2655/* Set shared signature algorithms for SSL structures */
2656static int tls1_set_shared_sigalgs(SSL *s)
2657 {
2658 const unsigned char *pref, *allow, *conf;
2659 size_t preflen, allowlen, conflen;
2660 size_t nmatch;
2661 TLS_SIGALGS *salgs = NULL;
2662 CERT *c = s->cert;
Adam Langleydb4f9522014-06-20 12:00:00 -07002663 if (c->shared_sigalgs)
2664 {
2665 OPENSSL_free(c->shared_sigalgs);
2666 c->shared_sigalgs = NULL;
2667 }
Adam Langley95c29f32014-06-20 12:00:00 -07002668 /* If client use client signature algorithms if not NULL */
David Benjamin335d10d2014-08-06 19:56:33 -04002669 if (!s->server && c->client_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002670 {
2671 conf = c->client_sigalgs;
2672 conflen = c->client_sigalgslen;
2673 }
David Benjamin335d10d2014-08-06 19:56:33 -04002674 else if (c->conf_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002675 {
2676 conf = c->conf_sigalgs;
2677 conflen = c->conf_sigalgslen;
2678 }
2679 else
2680 conflen = tls12_get_psigalgs(s, &conf);
David Benjamin335d10d2014-08-06 19:56:33 -04002681 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
Adam Langley95c29f32014-06-20 12:00:00 -07002682 {
2683 pref = conf;
2684 preflen = conflen;
2685 allow = c->peer_sigalgs;
2686 allowlen = c->peer_sigalgslen;
2687 }
2688 else
2689 {
2690 allow = conf;
2691 allowlen = conflen;
2692 pref = c->peer_sigalgs;
2693 preflen = c->peer_sigalgslen;
2694 }
2695 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2696 if (!nmatch)
2697 return 1;
2698 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2699 if (!salgs)
2700 return 0;
2701 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2702 c->shared_sigalgs = salgs;
2703 c->shared_sigalgslen = nmatch;
2704 return 1;
2705 }
2706
2707
2708/* Set preferred digest for each key type */
2709
David Benjamincd996942014-07-20 16:23:51 -04002710int tls1_process_sigalgs(SSL *s, const CBS *sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002711 {
2712 int idx;
2713 size_t i;
2714 const EVP_MD *md;
2715 CERT *c = s->cert;
2716 TLS_SIGALGS *sigptr;
David Benjamincd996942014-07-20 16:23:51 -04002717
Adam Langley95c29f32014-06-20 12:00:00 -07002718 /* Extension ignored for inappropriate versions */
2719 if (!SSL_USE_SIGALGS(s))
2720 return 1;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002721 /* Length must be even */
David Benjamincd996942014-07-20 16:23:51 -04002722 if (CBS_len(sigalgs) % 2 != 0)
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002723 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002724 /* Should never happen */
2725 if (!c)
2726 return 0;
2727
David Benjamincd996942014-07-20 16:23:51 -04002728 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen))
Adam Langley95c29f32014-06-20 12:00:00 -07002729 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002730
2731 tls1_set_shared_sigalgs(s);
2732
Adam Langley95c29f32014-06-20 12:00:00 -07002733 for (i = 0, sigptr = c->shared_sigalgs;
2734 i < c->shared_sigalgslen; i++, sigptr++)
2735 {
2736 idx = tls12_get_pkey_idx(sigptr->rsign);
2737 if (idx > 0 && c->pkeys[idx].digest == NULL)
2738 {
2739 md = tls12_get_hash(sigptr->rhash);
2740 c->pkeys[idx].digest = md;
Adam Langley95c29f32014-06-20 12:00:00 -07002741 if (idx == SSL_PKEY_RSA_SIGN)
2742 {
Adam Langley95c29f32014-06-20 12:00:00 -07002743 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2744 }
2745 }
2746
2747 }
David Benjamin253b3e72014-11-13 15:53:52 -05002748
2749 /* Set any remaining keys to default values. NOTE: if alg is
2750 * not supported it stays as NULL.
Adam Langley95c29f32014-06-20 12:00:00 -07002751 */
David Benjamin253b3e72014-11-13 15:53:52 -05002752 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
Adam Langley95c29f32014-06-20 12:00:00 -07002753 {
David Benjamin253b3e72014-11-13 15:53:52 -05002754 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
2755 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002756 }
David Benjamin253b3e72014-11-13 15:53:52 -05002757 if (!c->pkeys[SSL_PKEY_ECC].digest)
2758 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002759 return 1;
2760 }
2761
2762
2763int SSL_get_sigalgs(SSL *s, int idx,
2764 int *psign, int *phash, int *psignhash,
2765 unsigned char *rsig, unsigned char *rhash)
2766 {
2767 const unsigned char *psig = s->cert->peer_sigalgs;
2768 if (psig == NULL)
2769 return 0;
2770 if (idx >= 0)
2771 {
2772 idx <<= 1;
2773 if (idx >= (int)s->cert->peer_sigalgslen)
2774 return 0;
2775 psig += idx;
2776 if (rhash)
2777 *rhash = psig[0];
2778 if (rsig)
2779 *rsig = psig[1];
2780 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2781 }
2782 return s->cert->peer_sigalgslen / 2;
2783 }
2784
2785int SSL_get_shared_sigalgs(SSL *s, int idx,
2786 int *psign, int *phash, int *psignhash,
2787 unsigned char *rsig, unsigned char *rhash)
2788 {
2789 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
2790 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
2791 return 0;
2792 shsigalgs += idx;
2793 if (phash)
2794 *phash = shsigalgs->hash_nid;
2795 if (psign)
2796 *psign = shsigalgs->sign_nid;
2797 if (psignhash)
2798 *psignhash = shsigalgs->signandhash_nid;
2799 if (rsig)
2800 *rsig = shsigalgs->rsign;
2801 if (rhash)
2802 *rhash = shsigalgs->rhash;
2803 return s->cert->shared_sigalgslen;
2804 }
2805
Adam Langley1258b6a2014-06-20 12:00:00 -07002806/* tls1_channel_id_hash calculates the signed data for a Channel ID on the given
2807 * SSL connection and writes it to |md|. */
2808int
2809tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s)
2810 {
2811 EVP_MD_CTX ctx;
2812 unsigned char temp_digest[EVP_MAX_MD_SIZE];
2813 unsigned temp_digest_len;
2814 int i;
2815 static const char kClientIDMagic[] = "TLS Channel ID signature";
2816
2817 if (s->s3->handshake_buffer)
Adam Langley75712922014-10-10 16:23:43 -07002818 if (!ssl3_digest_cached_records(s, free_handshake_buffer))
Adam Langley1258b6a2014-06-20 12:00:00 -07002819 return 0;
2820
2821 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2822
2823 if (s->hit && s->s3->tlsext_channel_id_new)
2824 {
2825 static const char kResumptionMagic[] = "Resumption";
2826 EVP_DigestUpdate(md, kResumptionMagic,
2827 sizeof(kResumptionMagic));
2828 if (s->session->original_handshake_hash_len == 0)
2829 return 0;
2830 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2831 s->session->original_handshake_hash_len);
2832 }
2833
2834 EVP_MD_CTX_init(&ctx);
2835 for (i = 0; i < SSL_MAX_DIGEST; i++)
2836 {
2837 if (s->s3->handshake_dgst[i] == NULL)
2838 continue;
2839 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
2840 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2841 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2842 }
2843 EVP_MD_CTX_cleanup(&ctx);
2844
2845 return 1;
2846 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002847
2848/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2849 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
2850int tls1_record_handshake_hashes_for_channel_id(SSL *s)
2851 {
2852 int digest_len;
2853 /* This function should never be called for a resumed session because
2854 * the handshake hashes that we wish to record are for the original,
2855 * full handshake. */
2856 if (s->hit)
2857 return -1;
2858 /* It only makes sense to call this function if Channel IDs have been
2859 * negotiated. */
2860 if (!s->s3->tlsext_channel_id_new)
2861 return -1;
2862
2863 digest_len = tls1_handshake_digest(
2864 s, s->session->original_handshake_hash,
2865 sizeof(s->session->original_handshake_hash));
2866 if (digest_len < 0)
2867 return -1;
2868
2869 s->session->original_handshake_hash_len = digest_len;
2870
2871 return 1;
2872 }
2873
Adam Langley95c29f32014-06-20 12:00:00 -07002874int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
2875 {
2876 unsigned char *sigalgs, *sptr;
2877 int rhash, rsign;
2878 size_t i;
2879 if (salglen & 1)
2880 return 0;
2881 sigalgs = OPENSSL_malloc(salglen);
2882 if (sigalgs == NULL)
2883 return 0;
2884 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
2885 {
2886 rhash = tls12_find_id(*psig_nids++, tls12_md,
2887 sizeof(tls12_md)/sizeof(tls12_lookup));
2888 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2889 sizeof(tls12_sig)/sizeof(tls12_lookup));
2890
2891 if (rhash == -1 || rsign == -1)
2892 goto err;
2893 *sptr++ = rhash;
2894 *sptr++ = rsign;
2895 }
2896
2897 if (client)
2898 {
2899 if (c->client_sigalgs)
2900 OPENSSL_free(c->client_sigalgs);
2901 c->client_sigalgs = sigalgs;
2902 c->client_sigalgslen = salglen;
2903 }
2904 else
2905 {
2906 if (c->conf_sigalgs)
2907 OPENSSL_free(c->conf_sigalgs);
2908 c->conf_sigalgs = sigalgs;
2909 c->conf_sigalgslen = salglen;
2910 }
2911
2912 return 1;
2913
2914 err:
2915 OPENSSL_free(sigalgs);
2916 return 0;
2917 }
2918
David Benjaminb398d162014-11-13 15:29:09 -05002919/* Check certificate chain is consistent with TLS extensions and is usable by
2920 * server. This allows the server to check chains before attempting to use them.
Adam Langley95c29f32014-06-20 12:00:00 -07002921 */
2922
David Benjaminb398d162014-11-13 15:29:09 -05002923int tls1_check_chain(SSL *s, int idx)
Adam Langley95c29f32014-06-20 12:00:00 -07002924 {
Adam Langley95c29f32014-06-20 12:00:00 -07002925 int rv = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002926 CERT_PKEY *cpk = NULL;
2927 CERT *c = s->cert;
David Benjaminb398d162014-11-13 15:29:09 -05002928 X509 *x;
2929 EVP_PKEY *pk;
David Benjaminb398d162014-11-13 15:29:09 -05002930
2931 cpk = c->pkeys + idx;
2932 x = cpk->x509;
2933 pk = cpk->privatekey;
David Benjaminb398d162014-11-13 15:29:09 -05002934 /* If no cert or key, forget it */
2935 if (!x || !pk)
2936 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -07002937
Adam Langley95c29f32014-06-20 12:00:00 -07002938 /* Check cert parameters are consistent */
David Benjamin1ad86812014-11-13 15:40:31 -05002939 if (tls1_check_cert_param(s, x, 2))
Adam Langley95c29f32014-06-20 12:00:00 -07002940 rv |= CERT_PKEY_EE_PARAM;
David Benjamin1ad86812014-11-13 15:40:31 -05002941 else
Adam Langley95c29f32014-06-20 12:00:00 -07002942 goto end;
2943 if (!s->server)
2944 rv |= CERT_PKEY_CA_PARAM;
David Benjamin253b3e72014-11-13 15:53:52 -05002945 rv |= CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE|CERT_PKEY_VALID;
Adam Langley95c29f32014-06-20 12:00:00 -07002946
2947 end:
2948
2949 if (TLS1_get_version(s) >= TLS1_2_VERSION)
2950 {
David Benjamin675227e2014-11-13 15:19:23 -05002951 if (cpk->digest)
Adam Langley95c29f32014-06-20 12:00:00 -07002952 rv |= CERT_PKEY_SIGN;
2953 }
2954 else
David Benjamin675227e2014-11-13 15:19:23 -05002955 rv |= CERT_PKEY_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002956
2957 /* When checking a CERT_PKEY structure all flags are irrelevant
2958 * if the chain is invalid.
2959 */
David Benjamin1ad86812014-11-13 15:40:31 -05002960 if (rv & CERT_PKEY_VALID)
2961 cpk->valid_flags = rv;
2962 else
Adam Langley95c29f32014-06-20 12:00:00 -07002963 {
David Benjamin1ad86812014-11-13 15:40:31 -05002964 /* Clear flags. */
2965 cpk->valid_flags = 0;
2966 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002967 }
2968 return rv;
2969 }
2970
2971/* Set validity of certificates in an SSL structure */
2972void tls1_set_cert_validity(SSL *s)
2973 {
David Benjaminb398d162014-11-13 15:29:09 -05002974 tls1_check_chain(s, SSL_PKEY_RSA_ENC);
2975 tls1_check_chain(s, SSL_PKEY_RSA_SIGN);
2976 tls1_check_chain(s, SSL_PKEY_ECC);
Adam Langley95c29f32014-06-20 12:00:00 -07002977 }
2978