blob: fa506bd51c9fb734e84a300134ff6bc67b82262a [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
109#include <stdio.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400110#include <stdlib.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700111#include <assert.h>
112
David Benjamin03973092014-06-24 23:27:17 -0400113#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700114#include <openssl/evp.h>
115#include <openssl/hmac.h>
116#include <openssl/mem.h>
117#include <openssl/obj.h>
118#include <openssl/rand.h>
119
120#include "ssl_locl.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700121static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
122 const unsigned char *sess_id, int sesslen,
123 SSL_SESSION **psess);
David Benjamin6c7aed02014-08-27 16:42:38 -0400124static int ssl_check_clienthello_tlsext(SSL *s);
125static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700126
127SSL3_ENC_METHOD TLSv1_enc_data={
128 tls1_enc,
129 tls1_mac,
130 tls1_setup_key_block,
131 tls1_generate_master_secret,
132 tls1_change_cipher_state,
133 tls1_final_finish_mac,
134 TLS1_FINISH_MAC_LENGTH,
135 tls1_cert_verify_mac,
136 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
137 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
138 tls1_alert_code,
139 tls1_export_keying_material,
140 0,
141 SSL3_HM_HEADER_LENGTH,
142 ssl3_set_handshake_header,
Adam Langley75712922014-10-10 16:23:43 -0700143 ssl3_handshake_write,
144 ssl3_add_to_finished_hash,
Adam Langley95c29f32014-06-20 12:00:00 -0700145 };
146
147SSL3_ENC_METHOD TLSv1_1_enc_data={
148 tls1_enc,
149 tls1_mac,
150 tls1_setup_key_block,
151 tls1_generate_master_secret,
152 tls1_change_cipher_state,
153 tls1_final_finish_mac,
154 TLS1_FINISH_MAC_LENGTH,
155 tls1_cert_verify_mac,
156 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
157 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
158 tls1_alert_code,
159 tls1_export_keying_material,
160 SSL_ENC_FLAG_EXPLICIT_IV,
161 SSL3_HM_HEADER_LENGTH,
162 ssl3_set_handshake_header,
Adam Langley75712922014-10-10 16:23:43 -0700163 ssl3_handshake_write,
164 ssl3_add_to_finished_hash,
Adam Langley95c29f32014-06-20 12:00:00 -0700165 };
166
167SSL3_ENC_METHOD TLSv1_2_enc_data={
168 tls1_enc,
169 tls1_mac,
170 tls1_setup_key_block,
171 tls1_generate_master_secret,
172 tls1_change_cipher_state,
173 tls1_final_finish_mac,
174 TLS1_FINISH_MAC_LENGTH,
175 tls1_cert_verify_mac,
176 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
177 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
178 tls1_alert_code,
179 tls1_export_keying_material,
180 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
181 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
182 SSL3_HM_HEADER_LENGTH,
183 ssl3_set_handshake_header,
Adam Langley75712922014-10-10 16:23:43 -0700184 ssl3_handshake_write,
185 ssl3_add_to_finished_hash,
Adam Langley95c29f32014-06-20 12:00:00 -0700186 };
187
David Benjamin35a7a442014-07-05 00:23:20 -0400188static int compare_uint16_t(const void *p1, const void *p2)
189 {
190 uint16_t u1 = *((const uint16_t*)p1);
191 uint16_t u2 = *((const uint16_t*)p2);
192 if (u1 < u2)
193 {
194 return -1;
195 }
196 else if (u1 > u2)
197 {
198 return 1;
199 }
200 else
201 {
202 return 0;
203 }
204 }
205
206/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be more
207 * than one extension of the same type in a ClientHello or ServerHello. This
208 * function does an initial scan over the extensions block to filter those
209 * out. */
210static int tls1_check_duplicate_extensions(const CBS *cbs)
211 {
212 CBS extensions = *cbs;
213 size_t num_extensions = 0, i = 0;
214 uint16_t *extension_types = NULL;
215 int ret = 0;
216
217 /* First pass: count the extensions. */
218 while (CBS_len(&extensions) > 0)
219 {
220 uint16_t type;
221 CBS extension;
222
223 if (!CBS_get_u16(&extensions, &type) ||
224 !CBS_get_u16_length_prefixed(&extensions, &extension))
225 {
226 goto done;
227 }
228
229 num_extensions++;
230 }
231
David Benjamin9a373592014-07-25 04:27:53 -0400232 if (num_extensions == 0)
233 {
234 return 1;
235 }
236
David Benjamin35a7a442014-07-05 00:23:20 -0400237 extension_types = (uint16_t*)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
238 if (extension_types == NULL)
239 {
240 OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions, ERR_R_MALLOC_FAILURE);
241 goto done;
242 }
243
244 /* Second pass: gather the extension types. */
245 extensions = *cbs;
246 for (i = 0; i < num_extensions; i++)
247 {
248 CBS extension;
249
250 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
251 !CBS_get_u16_length_prefixed(&extensions, &extension))
252 {
253 /* This should not happen. */
254 goto done;
255 }
256 }
257 assert(CBS_len(&extensions) == 0);
258
259 /* Sort the extensions and make sure there are no duplicates. */
260 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
261 for (i = 1; i < num_extensions; i++)
262 {
263 if (extension_types[i-1] == extension_types[i])
264 {
265 goto done;
266 }
267 }
268
269 ret = 1;
270done:
271 if (extension_types)
272 OPENSSL_free(extension_types);
273 return ret;
274 }
275
Adam Langleydc9b1412014-06-20 12:00:00 -0700276char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx)
277 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400278 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
279
280 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700281
282 /* Skip client version. */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400283 if (!CBS_skip(&client_hello, 2))
Adam Langleydc9b1412014-06-20 12:00:00 -0700284 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700285
286 /* Skip client nonce. */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400287 if (!CBS_skip(&client_hello, 32))
Adam Langleydc9b1412014-06-20 12:00:00 -0700288 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700289
David Benjamin8f2c20e2014-07-09 09:30:38 -0400290 /* Extract session_id. */
291 if (!CBS_get_u8_length_prefixed(&client_hello, &session_id))
Adam Langleydc9b1412014-06-20 12:00:00 -0700292 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400293 ctx->session_id = CBS_data(&session_id);
294 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700295
296 /* Skip past DTLS cookie */
David Benjamin09bd58d2014-08-12 21:22:28 -0400297 if (SSL_IS_DTLS(ctx->ssl))
Adam Langleydc9b1412014-06-20 12:00:00 -0700298 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400299 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700300
David Benjamin8f2c20e2014-07-09 09:30:38 -0400301 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie))
Adam Langleydc9b1412014-06-20 12:00:00 -0700302 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700303 }
304
David Benjamin8f2c20e2014-07-09 09:30:38 -0400305 /* Extract cipher_suites. */
306 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
307 CBS_len(&cipher_suites) < 2 ||
308 (CBS_len(&cipher_suites) & 1) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700309 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400310 ctx->cipher_suites = CBS_data(&cipher_suites);
311 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700312
David Benjamin8f2c20e2014-07-09 09:30:38 -0400313 /* Extract compression_methods. */
314 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
315 CBS_len(&compression_methods) < 1)
Adam Langleydc9b1412014-06-20 12:00:00 -0700316 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400317 ctx->compression_methods = CBS_data(&compression_methods);
318 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700319
320 /* If the ClientHello ends here then it's valid, but doesn't have any
321 * extensions. (E.g. SSLv3.) */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400322 if (CBS_len(&client_hello) == 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700323 {
324 ctx->extensions = NULL;
325 ctx->extensions_len = 0;
326 return 1;
327 }
328
David Benjamin8f2c20e2014-07-09 09:30:38 -0400329 /* Extract extensions and check it is valid. */
330 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
331 !tls1_check_duplicate_extensions(&extensions) ||
332 CBS_len(&client_hello) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700333 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400334 ctx->extensions = CBS_data(&extensions);
335 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700336
Adam Langleydc9b1412014-06-20 12:00:00 -0700337 return 1;
Adam Langleydc9b1412014-06-20 12:00:00 -0700338 }
339
340char
341SSL_early_callback_ctx_extension_get(const struct ssl_early_callback_ctx *ctx,
342 uint16_t extension_type,
343 const unsigned char **out_data,
344 size_t *out_len)
345 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400346 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700347
David Benjamin8f2c20e2014-07-09 09:30:38 -0400348 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
349
350 while (CBS_len(&extensions) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700351 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400352 uint16_t type;
353 CBS extension;
Adam Langleydc9b1412014-06-20 12:00:00 -0700354
David Benjamin8f2c20e2014-07-09 09:30:38 -0400355 /* Decode the next extension. */
356 if (!CBS_get_u16(&extensions, &type) ||
357 !CBS_get_u16_length_prefixed(&extensions, &extension))
Adam Langleydc9b1412014-06-20 12:00:00 -0700358 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700359
David Benjamin8f2c20e2014-07-09 09:30:38 -0400360 if (type == extension_type)
Adam Langleydc9b1412014-06-20 12:00:00 -0700361 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400362 *out_data = CBS_data(&extension);
363 *out_len = CBS_len(&extension);
Adam Langleydc9b1412014-06-20 12:00:00 -0700364 return 1;
365 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700366 }
367
368 return 0;
369 }
370
Adam Langley95c29f32014-06-20 12:00:00 -0700371
David Benjamincff64722014-08-19 19:54:46 -0400372static const int nid_list[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700373 {
374 NID_sect163k1, /* sect163k1 (1) */
375 NID_sect163r1, /* sect163r1 (2) */
376 NID_sect163r2, /* sect163r2 (3) */
377 NID_sect193r1, /* sect193r1 (4) */
378 NID_sect193r2, /* sect193r2 (5) */
379 NID_sect233k1, /* sect233k1 (6) */
380 NID_sect233r1, /* sect233r1 (7) */
381 NID_sect239k1, /* sect239k1 (8) */
382 NID_sect283k1, /* sect283k1 (9) */
383 NID_sect283r1, /* sect283r1 (10) */
384 NID_sect409k1, /* sect409k1 (11) */
385 NID_sect409r1, /* sect409r1 (12) */
386 NID_sect571k1, /* sect571k1 (13) */
387 NID_sect571r1, /* sect571r1 (14) */
388 NID_secp160k1, /* secp160k1 (15) */
389 NID_secp160r1, /* secp160r1 (16) */
390 NID_secp160r2, /* secp160r2 (17) */
391 NID_secp192k1, /* secp192k1 (18) */
392 NID_X9_62_prime192v1, /* secp192r1 (19) */
393 NID_secp224k1, /* secp224k1 (20) */
394 NID_secp224r1, /* secp224r1 (21) */
395 NID_secp256k1, /* secp256k1 (22) */
396 NID_X9_62_prime256v1, /* secp256r1 (23) */
397 NID_secp384r1, /* secp384r1 (24) */
398 NID_secp521r1, /* secp521r1 (25) */
399 NID_brainpoolP256r1, /* brainpoolP256r1 (26) */
400 NID_brainpoolP384r1, /* brainpoolP384r1 (27) */
401 NID_brainpoolP512r1 /* brainpool512r1 (28) */
402 };
403
David Benjamin072334d2014-07-13 16:24:27 -0400404static const uint8_t ecformats_default[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700405 {
406 TLSEXT_ECPOINTFORMAT_uncompressed,
Adam Langley95c29f32014-06-20 12:00:00 -0700407 };
408
David Benjamin072334d2014-07-13 16:24:27 -0400409static const uint16_t eccurves_default[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700410 {
David Benjamin072334d2014-07-13 16:24:27 -0400411 23, /* secp256r1 (23) */
412 24, /* secp384r1 (24) */
413 25, /* secp521r1 (25) */
Adam Langley95c29f32014-06-20 12:00:00 -0700414 };
415
David Benjamin072334d2014-07-13 16:24:27 -0400416int tls1_ec_curve_id2nid(uint16_t curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700417 {
418 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
David Benjamin072334d2014-07-13 16:24:27 -0400419 if (curve_id < 1 || curve_id > sizeof(nid_list)/sizeof(nid_list[0]))
420 return OBJ_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700421 return nid_list[curve_id-1];
422 }
423
David Benjamin072334d2014-07-13 16:24:27 -0400424uint16_t tls1_ec_nid2curve_id(int nid)
Adam Langley95c29f32014-06-20 12:00:00 -0700425 {
David Benjamin072334d2014-07-13 16:24:27 -0400426 size_t i;
427 for (i = 0; i < sizeof(nid_list)/sizeof(nid_list[0]); i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700428 {
David Benjamin072334d2014-07-13 16:24:27 -0400429 /* nid_list[i] stores the NID corresponding to curve ID i+1. */
430 if (nid == nid_list[i])
431 return i + 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700432 }
David Benjamin072334d2014-07-13 16:24:27 -0400433 /* Use 0 for non-existent curve ID. Note: this assumes that curve ID 0
434 * will never be allocated. */
435 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700436 }
David Benjamin072334d2014-07-13 16:24:27 -0400437
David Benjamin42e9a772014-09-02 23:18:44 -0400438/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len|
439 * to the list of allowed curve IDs. If |get_peer_curves| is non-zero,
440 * return the peer's curve list. Otherwise, return the preferred
441 * list. */
442static void tls1_get_curvelist(SSL *s, int get_peer_curves,
David Benjamin072334d2014-07-13 16:24:27 -0400443 const uint16_t **out_curve_ids, size_t *out_curve_ids_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700444 {
David Benjamin42e9a772014-09-02 23:18:44 -0400445 if (get_peer_curves)
Adam Langley95c29f32014-06-20 12:00:00 -0700446 {
David Benjamina19fc252014-10-19 00:14:36 -0400447 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
448 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
Adam Langley95c29f32014-06-20 12:00:00 -0700449 return;
450 }
Adam Langley95c29f32014-06-20 12:00:00 -0700451
David Benjamin335d10d2014-08-06 19:56:33 -0400452 *out_curve_ids = s->tlsext_ellipticcurvelist;
453 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
David Benjamin072334d2014-07-13 16:24:27 -0400454 if (!*out_curve_ids)
Adam Langley95c29f32014-06-20 12:00:00 -0700455 {
David Benjamin072334d2014-07-13 16:24:27 -0400456 *out_curve_ids = eccurves_default;
David Benjamin0eb5a2d2014-07-25 02:40:43 -0400457 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
Adam Langley95c29f32014-06-20 12:00:00 -0700458 }
459 }
David Benjamined439582014-07-14 19:13:02 -0400460
David Benjamined439582014-07-14 19:13:02 -0400461int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700462 {
David Benjamined439582014-07-14 19:13:02 -0400463 uint8_t curve_type;
464 uint16_t curve_id;
David Benjamin072334d2014-07-13 16:24:27 -0400465 const uint16_t *curves;
466 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400467
468 /* Only support named curves. */
469 if (!CBS_get_u8(cbs, &curve_type) ||
470 curve_type != NAMED_CURVE_TYPE ||
471 !CBS_get_u16(cbs, &curve_id))
Adam Langley95c29f32014-06-20 12:00:00 -0700472 return 0;
David Benjamined439582014-07-14 19:13:02 -0400473
David Benjamin072334d2014-07-13 16:24:27 -0400474 tls1_get_curvelist(s, 0, &curves, &curves_len);
475 for (i = 0; i < curves_len; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700476 {
David Benjamin072334d2014-07-13 16:24:27 -0400477 if (curve_id == curves[i])
David Benjamined439582014-07-14 19:13:02 -0400478 {
479 *out_curve_id = curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700480 return 1;
David Benjamined439582014-07-14 19:13:02 -0400481 }
Adam Langley95c29f32014-06-20 12:00:00 -0700482 }
483 return 0;
484 }
485
David Benjamin072334d2014-07-13 16:24:27 -0400486int tls1_get_shared_curve(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -0700487 {
David Benjamin072334d2014-07-13 16:24:27 -0400488 const uint16_t *pref, *supp;
Adam Langley95c29f32014-06-20 12:00:00 -0700489 size_t preflen, supplen, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400490
Adam Langley95c29f32014-06-20 12:00:00 -0700491 /* Can't do anything on client side */
492 if (s->server == 0)
David Benjamin072334d2014-07-13 16:24:27 -0400493 return NID_undef;
494
David Benjamin335d10d2014-08-06 19:56:33 -0400495 /* Return first preference shared curve */
Adam Langley95c29f32014-06-20 12:00:00 -0700496 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
497 &supp, &supplen);
498 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
499 &pref, &preflen);
David Benjamin072334d2014-07-13 16:24:27 -0400500 for (i = 0; i < preflen; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700501 {
David Benjamin072334d2014-07-13 16:24:27 -0400502 for (j = 0; j < supplen; j++)
Adam Langley95c29f32014-06-20 12:00:00 -0700503 {
David Benjamin072334d2014-07-13 16:24:27 -0400504 if (pref[i] == supp[j])
505 return tls1_ec_curve_id2nid(pref[i]);
Adam Langley95c29f32014-06-20 12:00:00 -0700506 }
507 }
David Benjamin072334d2014-07-13 16:24:27 -0400508 return NID_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700509 }
510
David Benjamin072334d2014-07-13 16:24:27 -0400511/* NOTE: tls1_ec_curve_id2nid and tls1_set_curves assume that
512 *
513 * (a) 0 is not a valid curve ID.
514 *
515 * (b) The largest curve ID is 31.
516 *
517 * Those implementations must be revised before adding support for curve IDs
518 * that break these assumptions. */
519OPENSSL_COMPILE_ASSERT(
520 (sizeof(nid_list) / sizeof(nid_list[0])) < 32, small_curve_ids);
521
522int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
523 const int *curves, size_t ncurves)
Adam Langley95c29f32014-06-20 12:00:00 -0700524 {
David Benjamin072334d2014-07-13 16:24:27 -0400525 uint16_t *curve_ids;
Adam Langley95c29f32014-06-20 12:00:00 -0700526 size_t i;
527 /* Bitmap of curves included to detect duplicates: only works
528 * while curve ids < 32
529 */
David Benjamin072334d2014-07-13 16:24:27 -0400530 uint32_t dup_list = 0;
531 curve_ids = (uint16_t*)OPENSSL_malloc(ncurves * sizeof(uint16_t));
532 if (!curve_ids)
Adam Langley95c29f32014-06-20 12:00:00 -0700533 return 0;
David Benjamin072334d2014-07-13 16:24:27 -0400534 for (i = 0; i < ncurves; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700535 {
David Benjamin072334d2014-07-13 16:24:27 -0400536 uint32_t idmask;
537 uint16_t id;
Adam Langley95c29f32014-06-20 12:00:00 -0700538 id = tls1_ec_nid2curve_id(curves[i]);
David Benjamin072334d2014-07-13 16:24:27 -0400539 idmask = ((uint32_t)1) << id;
Adam Langley95c29f32014-06-20 12:00:00 -0700540 if (!id || (dup_list & idmask))
541 {
David Benjamin072334d2014-07-13 16:24:27 -0400542 OPENSSL_free(curve_ids);
Adam Langley95c29f32014-06-20 12:00:00 -0700543 return 0;
544 }
545 dup_list |= idmask;
David Benjamin072334d2014-07-13 16:24:27 -0400546 curve_ids[i] = id;
Adam Langley95c29f32014-06-20 12:00:00 -0700547 }
David Benjamin072334d2014-07-13 16:24:27 -0400548 if (*out_curve_ids)
549 OPENSSL_free(*out_curve_ids);
550 *out_curve_ids = curve_ids;
551 *out_curve_ids_len = ncurves;
Adam Langley95c29f32014-06-20 12:00:00 -0700552 return 1;
553 }
554
David Benjamin072334d2014-07-13 16:24:27 -0400555/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
556 * TLS curve ID and point format, respectively, for |ec|. It returns one on
557 * success and zero on failure. */
558static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id, uint8_t *out_comp_id, EC_KEY *ec)
Adam Langley95c29f32014-06-20 12:00:00 -0700559 {
Adam Langley95c29f32014-06-20 12:00:00 -0700560 int nid;
David Benjamin072334d2014-07-13 16:24:27 -0400561 uint16_t id;
Adam Langley95c29f32014-06-20 12:00:00 -0700562 const EC_GROUP *grp;
563 if (!ec)
564 return 0;
565
Adam Langley95c29f32014-06-20 12:00:00 -0700566 grp = EC_KEY_get0_group(ec);
567 if (!grp)
568 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700569
570 /* Determine curve ID */
David Benjamin072334d2014-07-13 16:24:27 -0400571 nid = EC_GROUP_get_curve_name(grp);
572 id = tls1_ec_nid2curve_id(nid);
573 if (!id)
574 return 0;
575
576 /* Set the named curve ID. Arbitrary explicit curves are not
577 * supported. */
578 *out_curve_id = id;
579
580 if (out_comp_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700581 {
582 if (EC_KEY_get0_public_key(ec) == NULL)
583 return 0;
584 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED)
David Benjamin072334d2014-07-13 16:24:27 -0400585 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
Adam Langley95c29f32014-06-20 12:00:00 -0700586 else
David Benjamin072334d2014-07-13 16:24:27 -0400587 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
Adam Langley95c29f32014-06-20 12:00:00 -0700588 }
589 return 1;
590 }
David Benjamin072334d2014-07-13 16:24:27 -0400591
David Benjamin42e9a772014-09-02 23:18:44 -0400592/* tls1_check_point_format returns one if |comp_id| is consistent with the
593 * peer's point format preferences. */
594static int tls1_check_point_format(SSL *s, uint8_t comp_id)
595 {
David Benjamina19fc252014-10-19 00:14:36 -0400596 uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
597 size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
David Benjamin42e9a772014-09-02 23:18:44 -0400598 size_t i;
599
600 /* If point formats extension present check it, otherwise everything
601 * is supported (see RFC4492). */
602 if (p == NULL)
603 return 1;
604
605 for (i = 0; i < plen; i++)
606 {
607 if (comp_id == p[i])
608 return 1;
609 }
610 return 0;
611 }
612
613/* tls1_check_curve_id returns one if |curve_id| is consistent with both our and
614 * the peer's curve preferences. Note: if called as the client, only our
615 * preferences are checked; the peer (the server) does not send preferences. */
616static int tls1_check_curve_id(SSL *s, uint16_t curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700617 {
David Benjamin072334d2014-07-13 16:24:27 -0400618 const uint16_t *curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400619 size_t curves_len, i, j;
620
621 /* Check against our list, then the peer's list. */
Adam Langley95c29f32014-06-20 12:00:00 -0700622 for (j = 0; j <= 1; j++)
623 {
David Benjamin072334d2014-07-13 16:24:27 -0400624 tls1_get_curvelist(s, j, &curves, &curves_len);
625 for (i = 0; i < curves_len; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700626 {
David Benjamin42e9a772014-09-02 23:18:44 -0400627 if (curves[i] == curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700628 break;
629 }
David Benjamin072334d2014-07-13 16:24:27 -0400630 if (i == curves_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700631 return 0;
David Benjamin42e9a772014-09-02 23:18:44 -0400632 /* Servers do not present a preference list so, if we are a
633 * client, only check our list. */
Adam Langley95c29f32014-06-20 12:00:00 -0700634 if (!s->server)
635 return 1;
636 }
637 return 1;
638 }
639
640static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
641 size_t *pformatslen)
642 {
643 /* If we have a custom point format list use it otherwise
644 * use default */
645 if (s->tlsext_ecpointformatlist)
646 {
647 *pformats = s->tlsext_ecpointformatlist;
648 *pformatslen = s->tlsext_ecpointformatlist_length;
649 }
650 else
651 {
652 *pformats = ecformats_default;
David Benjamin335d10d2014-08-06 19:56:33 -0400653 *pformatslen = sizeof(ecformats_default);
Adam Langley95c29f32014-06-20 12:00:00 -0700654 }
655 }
656
David Benjamin033e5f42014-11-13 18:47:41 -0500657int tls1_check_ec_cert(SSL *s, X509 *x)
Adam Langley95c29f32014-06-20 12:00:00 -0700658 {
David Benjamin033e5f42014-11-13 18:47:41 -0500659 int ret = 0;
660 EVP_PKEY *pkey = X509_get_pubkey(x);
David Benjamin072334d2014-07-13 16:24:27 -0400661 uint16_t curve_id;
David Benjamin033e5f42014-11-13 18:47:41 -0500662 uint8_t comp_id;
663
664 if (!pkey ||
665 pkey->type != EVP_PKEY_EC ||
666 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
667 !tls1_check_curve_id(s, curve_id) ||
668 !tls1_check_point_format(s, comp_id))
Adam Langley95c29f32014-06-20 12:00:00 -0700669 {
David Benjamin033e5f42014-11-13 18:47:41 -0500670 goto done;
Adam Langley95c29f32014-06-20 12:00:00 -0700671 }
David Benjamin033e5f42014-11-13 18:47:41 -0500672
673 ret = 1;
674
675done:
676 if (pkey)
677 EVP_PKEY_free(pkey);
678 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700679 }
David Benjamin42e9a772014-09-02 23:18:44 -0400680
David Benjamin42e9a772014-09-02 23:18:44 -0400681int tls1_check_ec_tmp_key(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -0700682 {
David Benjamin072334d2014-07-13 16:24:27 -0400683 uint16_t curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700684 EC_KEY *ec = s->cert->ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -0700685 if (s->cert->ecdh_tmp_auto)
686 {
687 /* Need a shared curve */
David Benjamin072334d2014-07-13 16:24:27 -0400688 return tls1_get_shared_curve(s) != NID_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700689 }
690 if (!ec)
691 {
692 if (s->cert->ecdh_tmp_cb)
693 return 1;
694 else
695 return 0;
696 }
David Benjamin42e9a772014-09-02 23:18:44 -0400697 return tls1_curve_params_from_ec_key(&curve_id, NULL, ec) &&
698 tls1_check_curve_id(s, curve_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700699 }
700
Adam Langley95c29f32014-06-20 12:00:00 -0700701
Adam Langley95c29f32014-06-20 12:00:00 -0700702
703/* List of supported signature algorithms and hashes. Should make this
704 * customisable at some point, for now include everything we support.
705 */
706
Adam Langley95c29f32014-06-20 12:00:00 -0700707#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700708
Adam Langley95c29f32014-06-20 12:00:00 -0700709#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700710
711#define tlsext_sigalg(md) \
712 tlsext_sigalg_rsa(md) \
Adam Langley95c29f32014-06-20 12:00:00 -0700713 tlsext_sigalg_ecdsa(md)
714
David Benjamincff64722014-08-19 19:54:46 -0400715static const uint8_t tls12_sigalgs[] = {
Adam Langley95c29f32014-06-20 12:00:00 -0700716 tlsext_sigalg(TLSEXT_hash_sha512)
717 tlsext_sigalg(TLSEXT_hash_sha384)
Adam Langley95c29f32014-06-20 12:00:00 -0700718 tlsext_sigalg(TLSEXT_hash_sha256)
719 tlsext_sigalg(TLSEXT_hash_sha224)
Adam Langley95c29f32014-06-20 12:00:00 -0700720 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700721};
Adam Langley95c29f32014-06-20 12:00:00 -0700722size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
723 {
Adam Langley95c29f32014-06-20 12:00:00 -0700724 /* If server use client authentication sigalgs if not NULL */
725 if (s->server && s->cert->client_sigalgs)
726 {
727 *psigs = s->cert->client_sigalgs;
728 return s->cert->client_sigalgslen;
729 }
730 else if (s->cert->conf_sigalgs)
731 {
732 *psigs = s->cert->conf_sigalgs;
733 return s->cert->conf_sigalgslen;
734 }
735 else
736 {
737 *psigs = tls12_sigalgs;
738 return sizeof(tls12_sigalgs);
739 }
740 }
David Benjamin05da6e12014-07-12 20:42:55 -0400741
742/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of
743 * |cbs|. It checks it is consistent with |s|'s sent supported
744 * signature algorithms and, if so, writes the relevant digest into
745 * |*out_md| and returns 1. Otherwise it returns 0 and writes an alert
746 * into |*out_alert|.
Adam Langley95c29f32014-06-20 12:00:00 -0700747 */
David Benjamin05da6e12014-07-12 20:42:55 -0400748int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert,
749 SSL *s, CBS *cbs, EVP_PKEY *pkey)
Adam Langley95c29f32014-06-20 12:00:00 -0700750 {
751 const unsigned char *sent_sigs;
752 size_t sent_sigslen, i;
753 int sigalg = tls12_get_sigid(pkey);
David Benjamin05da6e12014-07-12 20:42:55 -0400754 uint8_t hash, signature;
Adam Langley95c29f32014-06-20 12:00:00 -0700755 /* Should never happen */
756 if (sigalg == -1)
David Benjamin05da6e12014-07-12 20:42:55 -0400757 {
758 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
759 *out_alert = SSL_AD_INTERNAL_ERROR;
760 return 0;
761 }
762 if (!CBS_get_u8(cbs, &hash) ||
763 !CBS_get_u8(cbs, &signature))
764 {
765 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
766 *out_alert = SSL_AD_DECODE_ERROR;
767 return 0;
768 }
Adam Langley95c29f32014-06-20 12:00:00 -0700769 /* Check key type is consistent with signature */
David Benjamin05da6e12014-07-12 20:42:55 -0400770 if (sigalg != signature)
Adam Langley95c29f32014-06-20 12:00:00 -0700771 {
772 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin05da6e12014-07-12 20:42:55 -0400773 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700774 return 0;
775 }
Adam Langley95c29f32014-06-20 12:00:00 -0700776 if (pkey->type == EVP_PKEY_EC)
777 {
David Benjamin072334d2014-07-13 16:24:27 -0400778 uint16_t curve_id;
779 uint8_t comp_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700780 /* Check compression and curve matches extensions */
David Benjamin072334d2014-07-13 16:24:27 -0400781 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec))
David Benjamin05da6e12014-07-12 20:42:55 -0400782 {
783 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -0700784 return 0;
David Benjamin05da6e12014-07-12 20:42:55 -0400785 }
David Benjamin42e9a772014-09-02 23:18:44 -0400786 if (s->server)
Adam Langley95c29f32014-06-20 12:00:00 -0700787 {
David Benjamin42e9a772014-09-02 23:18:44 -0400788 if (!tls1_check_curve_id(s, curve_id) ||
789 !tls1_check_point_format(s, comp_id))
790 {
791 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
792 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
793 return 0;
794 }
Adam Langley95c29f32014-06-20 12:00:00 -0700795 }
David Benjamin05da6e12014-07-12 20:42:55 -0400796 }
Adam Langley95c29f32014-06-20 12:00:00 -0700797
798 /* Check signature matches a type we sent */
799 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
800 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
801 {
David Benjamin05da6e12014-07-12 20:42:55 -0400802 if (hash == sent_sigs[0] && signature == sent_sigs[1])
Adam Langley95c29f32014-06-20 12:00:00 -0700803 break;
804 }
David Benjamin253b3e72014-11-13 15:53:52 -0500805 /* Allow fallback to SHA-1. */
806 if (i == sent_sigslen && hash != TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700807 {
808 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin05da6e12014-07-12 20:42:55 -0400809 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700810 return 0;
811 }
David Benjamin05da6e12014-07-12 20:42:55 -0400812 *out_md = tls12_get_hash(hash);
813 if (*out_md == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700814 {
815 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
David Benjamin05da6e12014-07-12 20:42:55 -0400816 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700817 return 0;
818 }
Adam Langley95c29f32014-06-20 12:00:00 -0700819 return 1;
820 }
821/* Get a mask of disabled algorithms: an algorithm is disabled
822 * if it isn't supported or doesn't appear in supported signature
823 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
824 * session and not global settings.
825 *
826 */
827void ssl_set_client_disabled(SSL *s)
828 {
829 CERT *c = s->cert;
830 const unsigned char *sigalgs;
831 size_t i, sigalgslen;
David Benjaminef2116d2014-08-19 20:21:56 -0400832 int have_rsa = 0, have_ecdsa = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700833 c->mask_a = 0;
834 c->mask_k = 0;
835 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
836 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
837 c->mask_ssl = SSL_TLSV1_2;
838 else
839 c->mask_ssl = 0;
840 /* Now go through all signature algorithms seeing if we support
841 * any for RSA, DSA, ECDSA. Do this for all versions not just
842 * TLS 1.2.
843 */
844 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
845 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
846 {
847 switch(sigalgs[1])
848 {
Adam Langley95c29f32014-06-20 12:00:00 -0700849 case TLSEXT_signature_rsa:
850 have_rsa = 1;
851 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700852 case TLSEXT_signature_ecdsa:
853 have_ecdsa = 1;
854 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700855 }
856 }
David Benjamin0da0e182014-08-19 16:20:28 -0400857 /* Disable auth if we don't include any appropriate signature
858 * algorithms.
Adam Langley95c29f32014-06-20 12:00:00 -0700859 */
860 if (!have_rsa)
861 {
862 c->mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700863 }
Adam Langley95c29f32014-06-20 12:00:00 -0700864 if (!have_ecdsa)
865 {
866 c->mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700867 }
Adam Langley95c29f32014-06-20 12:00:00 -0700868 /* with PSK there must be client callback set */
869 if (!s->psk_client_callback)
870 {
871 c->mask_a |= SSL_aPSK;
872 c->mask_k |= SSL_kPSK;
873 }
Adam Langley95c29f32014-06-20 12:00:00 -0700874 }
875
Adam Langleyb0c235e2014-06-20 12:00:00 -0700876/* header_len is the length of the ClientHello header written so far, used to
877 * compute padding. It does not include the record header. Pass 0 if no padding
878 * is to be done. */
879unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, size_t header_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700880 {
881 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -0700882 unsigned char *ret = buf;
883 unsigned char *orig = buf;
Adam Langley95c29f32014-06-20 12:00:00 -0700884 /* See if we support any ECC ciphersuites */
885 int using_ecc = 0;
886 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
887 {
David Benjaminfb3ff2c2014-09-30 21:00:38 -0400888 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -0700889 unsigned long alg_k, alg_a;
890 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
891
892 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
893 {
David Benjamin6f260012014-08-15 13:49:12 -0400894 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700895
896 alg_k = c->algorithm_mkey;
897 alg_a = c->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -0400898 if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA))
Adam Langley95c29f32014-06-20 12:00:00 -0700899 {
900 using_ecc = 1;
901 break;
902 }
903 }
904 }
Adam Langley95c29f32014-06-20 12:00:00 -0700905
906 /* don't add extensions for SSLv3 unless doing secure renegotiation */
907 if (s->client_version == SSL3_VERSION
908 && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -0700909 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -0700910
911 ret+=2;
912
913 if (ret>=limit) return NULL; /* this really never occurs, but ... */
914
915 if (s->tlsext_hostname != NULL)
916 {
917 /* Add TLS extension servername to the Client Hello message */
918 unsigned long size_str;
919 long lenmax;
920
921 /* check for enough space.
922 4 for the servername type and entension length
923 2 for servernamelist length
924 1 for the hostname type
925 2 for hostname length
926 + hostname length
927 */
928
929 if ((lenmax = limit - ret - 9) < 0
930 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
931 return NULL;
932
933 /* extension type and length */
934 s2n(TLSEXT_TYPE_server_name,ret);
935 s2n(size_str+5,ret);
936
937 /* length of servername list */
938 s2n(size_str+3,ret);
939
940 /* hostname type, length and hostname */
941 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
942 s2n(size_str,ret);
943 memcpy(ret, s->tlsext_hostname, size_str);
944 ret+=size_str;
945 }
946
947 /* Add RI if renegotiating */
948 if (s->renegotiate)
949 {
950 int el;
951
952 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
953 {
954 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
955 return NULL;
956 }
957
Adam Langleyb0c235e2014-06-20 12:00:00 -0700958 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700959
960 s2n(TLSEXT_TYPE_renegotiate,ret);
961 s2n(el,ret);
962
963 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
964 {
965 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
966 return NULL;
967 }
968
969 ret += el;
970 }
971
Adam Langley75712922014-10-10 16:23:43 -0700972 /* Add extended master secret. */
973 if (s->version != SSL3_VERSION)
974 {
975 if (limit - ret - 4 < 0)
976 return NULL;
977 s2n(TLSEXT_TYPE_extended_master_secret,ret);
978 s2n(0,ret);
979 }
980
Adam Langley95c29f32014-06-20 12:00:00 -0700981 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
982 {
David Benjamind1681e62014-11-20 14:54:14 -0500983 int ticklen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700984 if (!s->new_session && s->session && s->session->tlsext_tick)
985 ticklen = s->session->tlsext_ticklen;
David Benjamind1681e62014-11-20 14:54:14 -0500986
Adam Langley95c29f32014-06-20 12:00:00 -0700987 /* Check for enough room 2 for extension type, 2 for len
988 * rest for ticket
989 */
990 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
991 s2n(TLSEXT_TYPE_session_ticket,ret);
992 s2n(ticklen,ret);
993 if (ticklen)
994 {
995 memcpy(ret, s->session->tlsext_tick, ticklen);
996 ret += ticklen;
997 }
998 }
Adam Langley95c29f32014-06-20 12:00:00 -0700999
1000 if (SSL_USE_SIGALGS(s))
1001 {
1002 size_t salglen;
1003 const unsigned char *salg;
1004 salglen = tls12_get_psigalgs(s, &salg);
1005 if ((size_t)(limit - ret) < salglen + 6)
1006 return NULL;
1007 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1008 s2n(salglen + 2, ret);
1009 s2n(salglen, ret);
1010 memcpy(ret, salg, salglen);
1011 ret += salglen;
1012 }
1013
David Benjamin6c7aed02014-08-27 16:42:38 -04001014 if (s->ocsp_stapling_enabled)
Adam Langley95c29f32014-06-20 12:00:00 -07001015 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001016 /* The status_request extension is excessively extensible at
1017 * every layer. On the client, only support requesting OCSP
1018 * responses with an empty responder_id_list and no
1019 * extensions. */
1020 if (limit - ret - 4 - 1 - 2 - 2 < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001021
Adam Langley95c29f32014-06-20 12:00:00 -07001022 s2n(TLSEXT_TYPE_status_request, ret);
David Benjamin6c7aed02014-08-27 16:42:38 -04001023 s2n(1 + 2 + 2, ret);
1024 /* status_type */
Adam Langley95c29f32014-06-20 12:00:00 -07001025 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
David Benjamin6c7aed02014-08-27 16:42:38 -04001026 /* responder_id_list - empty */
1027 s2n(0, ret);
1028 /* request_extensions - empty */
1029 s2n(0, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001030 }
Adam Langley95c29f32014-06-20 12:00:00 -07001031
Adam Langley95c29f32014-06-20 12:00:00 -07001032 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1033 {
1034 /* The client advertises an emtpy extension to indicate its
1035 * support for Next Protocol Negotiation */
1036 if (limit - ret - 4 < 0)
1037 return NULL;
1038 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1039 s2n(0,ret);
1040 }
Adam Langley95c29f32014-06-20 12:00:00 -07001041
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001042 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len)
1043 {
1044 /* The client advertises an empty extension to indicate its support for
1045 * certificate timestamps. */
1046 if (limit - ret - 4 < 0)
1047 return NULL;
1048 s2n(TLSEXT_TYPE_certificate_timestamp,ret);
1049 s2n(0,ret);
1050 }
1051
Adam Langley95c29f32014-06-20 12:00:00 -07001052 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1053 {
1054 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1055 return NULL;
1056 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1057 s2n(2 + s->alpn_client_proto_list_len,ret);
1058 s2n(s->alpn_client_proto_list_len,ret);
1059 memcpy(ret, s->alpn_client_proto_list,
1060 s->alpn_client_proto_list_len);
1061 ret += s->alpn_client_proto_list_len;
1062 }
1063
Adam Langley1258b6a2014-06-20 12:00:00 -07001064 if (s->tlsext_channel_id_enabled)
1065 {
1066 /* The client advertises an emtpy extension to indicate its
1067 * support for Channel ID. */
1068 if (limit - ret - 4 < 0)
1069 return NULL;
1070 if (s->ctx->tlsext_channel_id_enabled_new)
1071 s2n(TLSEXT_TYPE_channel_id_new,ret);
1072 else
1073 s2n(TLSEXT_TYPE_channel_id,ret);
1074 s2n(0,ret);
1075 }
1076
Adam Langley95c29f32014-06-20 12:00:00 -07001077 if(SSL_get_srtp_profiles(s))
1078 {
1079 int el;
1080
1081 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1082
Adam Langleyb0c235e2014-06-20 12:00:00 -07001083 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001084
1085 s2n(TLSEXT_TYPE_use_srtp,ret);
1086 s2n(el,ret);
1087
David Benjamin120a6742014-08-30 14:54:37 -04001088 if(!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001089 {
1090 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1091 return NULL;
1092 }
1093 ret += el;
1094 }
1095
Adam Langleyc3174b72014-06-20 12:00:00 -07001096 if (using_ecc)
1097 {
1098 /* Add TLS extension ECPointFormats to the ClientHello message */
1099 long lenmax;
David Benjamin072334d2014-07-13 16:24:27 -04001100 const uint8_t *formats;
1101 const uint16_t *curves;
1102 size_t formats_len, curves_len, i;
Adam Langleyc3174b72014-06-20 12:00:00 -07001103
David Benjamin072334d2014-07-13 16:24:27 -04001104 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001105
1106 if ((lenmax = limit - ret - 5) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001107 if (formats_len > (size_t)lenmax) return NULL;
1108 if (formats_len > 255)
Adam Langleyc3174b72014-06-20 12:00:00 -07001109 {
1110 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1111 return NULL;
1112 }
1113
1114 s2n(TLSEXT_TYPE_ec_point_formats,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001115 s2n(formats_len + 1,ret);
1116 *(ret++) = (unsigned char)formats_len;
1117 memcpy(ret, formats, formats_len);
1118 ret+=formats_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001119
1120 /* Add TLS extension EllipticCurves to the ClientHello message */
David Benjamin072334d2014-07-13 16:24:27 -04001121 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001122
1123 if ((lenmax = limit - ret - 6) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001124 if ((curves_len * 2) > (size_t)lenmax) return NULL;
1125 if ((curves_len * 2) > 65532)
Adam Langleyc3174b72014-06-20 12:00:00 -07001126 {
1127 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1128 return NULL;
1129 }
1130
1131 s2n(TLSEXT_TYPE_elliptic_curves,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001132 s2n((curves_len * 2) + 2, ret);
Adam Langleyc3174b72014-06-20 12:00:00 -07001133
1134 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1135 * elliptic_curve_list, but the examples use two bytes.
1136 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1137 * resolves this to two bytes.
1138 */
David Benjamin072334d2014-07-13 16:24:27 -04001139 s2n(curves_len * 2, ret);
1140 for (i = 0; i < curves_len; i++)
1141 {
1142 s2n(curves[i], ret);
1143 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001144 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001145
Adam Langley95c29f32014-06-20 12:00:00 -07001146#ifdef TLSEXT_TYPE_padding
1147 /* Add padding to workaround bugs in F5 terminators.
Adam Langleyb0c235e2014-06-20 12:00:00 -07001148 * See https://tools.ietf.org/html/draft-agl-tls-padding-03
Adam Langley95c29f32014-06-20 12:00:00 -07001149 *
1150 * NB: because this code works out the length of all existing
Adam Langleyb0c235e2014-06-20 12:00:00 -07001151 * extensions it MUST always appear last. */
1152 if (header_len > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001153 {
Adam Langleyb0c235e2014-06-20 12:00:00 -07001154 header_len += ret - orig;
1155 if (header_len > 0xff && header_len < 0x200)
1156 {
1157 size_t padding_len = 0x200 - header_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001158 /* Extensions take at least four bytes to encode. Always
1159 * include least one byte of data if including the
1160 * extension. WebSphere Application Server 7.0 is
1161 * intolerant to the last extension being zero-length. */
1162 if (padding_len >= 4 + 1)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001163 padding_len -= 4;
1164 else
Adam Langleyc3174b72014-06-20 12:00:00 -07001165 padding_len = 1;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001166 if (limit - ret - 4 - (long)padding_len < 0)
1167 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001168
Adam Langleyb0c235e2014-06-20 12:00:00 -07001169 s2n(TLSEXT_TYPE_padding, ret);
1170 s2n(padding_len, ret);
1171 memset(ret, 0, padding_len);
1172 ret += padding_len;
1173 }
Adam Langley95c29f32014-06-20 12:00:00 -07001174 }
1175#endif
1176
Adam Langleyb0c235e2014-06-20 12:00:00 -07001177 if ((extdatalen = ret-orig-2)== 0)
1178 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001179
Adam Langleyb0c235e2014-06-20 12:00:00 -07001180 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001181 return ret;
1182 }
1183
Adam Langleyb0c235e2014-06-20 12:00:00 -07001184unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit)
Adam Langley95c29f32014-06-20 12:00:00 -07001185 {
1186 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001187 unsigned char *orig = buf;
1188 unsigned char *ret = buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001189 int next_proto_neg_seen;
Adam Langley95c29f32014-06-20 12:00:00 -07001190 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1191 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -04001192 int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
David Benjamina19fc252014-10-19 00:14:36 -04001193 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001194 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1195 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001196 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001197
1198 ret+=2;
1199 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1200
Adam Langleyed8270a2014-09-02 13:52:56 -07001201 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001202 {
1203 if ((long)(limit - ret - 4) < 0) return NULL;
1204
1205 s2n(TLSEXT_TYPE_server_name,ret);
1206 s2n(0,ret);
1207 }
1208
1209 if(s->s3->send_connection_binding)
1210 {
1211 int el;
1212
1213 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1214 {
1215 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1216 return NULL;
1217 }
1218
Adam Langleyb0c235e2014-06-20 12:00:00 -07001219 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001220
1221 s2n(TLSEXT_TYPE_renegotiate,ret);
1222 s2n(el,ret);
1223
1224 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1225 {
1226 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1227 return NULL;
1228 }
1229
1230 ret += el;
1231 }
1232
Adam Langley75712922014-10-10 16:23:43 -07001233 if (s->s3->tmp.extended_master_secret)
1234 {
1235 if ((long)(limit - ret - 4) < 0) return NULL;
1236
1237 s2n(TLSEXT_TYPE_extended_master_secret,ret);
1238 s2n(0,ret);
1239 }
1240
Adam Langley95c29f32014-06-20 12:00:00 -07001241 if (using_ecc)
1242 {
1243 const unsigned char *plist;
1244 size_t plistlen;
1245 /* Add TLS extension ECPointFormats to the ServerHello message */
1246 long lenmax;
1247
1248 tls1_get_formatlist(s, &plist, &plistlen);
1249
1250 if ((lenmax = limit - ret - 5) < 0) return NULL;
1251 if (plistlen > (size_t)lenmax) return NULL;
1252 if (plistlen > 255)
1253 {
1254 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1255 return NULL;
1256 }
1257
1258 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1259 s2n(plistlen + 1,ret);
1260 *(ret++) = (unsigned char) plistlen;
1261 memcpy(ret, plist, plistlen);
1262 ret+=plistlen;
1263
1264 }
1265 /* Currently the server should not respond with a SupportedCurves extension */
Adam Langley95c29f32014-06-20 12:00:00 -07001266
1267 if (s->tlsext_ticket_expected
1268 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1269 {
1270 if ((long)(limit - ret - 4) < 0) return NULL;
1271 s2n(TLSEXT_TYPE_session_ticket,ret);
1272 s2n(0,ret);
1273 }
1274
David Benjamin6c7aed02014-08-27 16:42:38 -04001275 if (s->s3->tmp.certificate_status_expected)
Adam Langley95c29f32014-06-20 12:00:00 -07001276 {
1277 if ((long)(limit - ret - 4) < 0) return NULL;
1278 s2n(TLSEXT_TYPE_status_request,ret);
1279 s2n(0,ret);
1280 }
1281
Adam Langley95c29f32014-06-20 12:00:00 -07001282 if(s->srtp_profile)
1283 {
1284 int el;
1285
1286 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1287
Adam Langleyb0c235e2014-06-20 12:00:00 -07001288 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001289
1290 s2n(TLSEXT_TYPE_use_srtp,ret);
1291 s2n(el,ret);
1292
David Benjamin120a6742014-08-30 14:54:37 -04001293 if(!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001294 {
1295 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1296 return NULL;
1297 }
1298 ret+=el;
1299 }
1300
Adam Langley95c29f32014-06-20 12:00:00 -07001301 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1302 s->s3->next_proto_neg_seen = 0;
1303 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1304 {
1305 const unsigned char *npa;
1306 unsigned int npalen;
1307 int r;
1308
1309 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1310 if (r == SSL_TLSEXT_ERR_OK)
1311 {
1312 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1313 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1314 s2n(npalen,ret);
1315 memcpy(ret, npa, npalen);
1316 ret += npalen;
1317 s->s3->next_proto_neg_seen = 1;
1318 }
1319 }
Adam Langley95c29f32014-06-20 12:00:00 -07001320
Adam Langley95c29f32014-06-20 12:00:00 -07001321 if (s->s3->alpn_selected)
1322 {
David Benjamin03973092014-06-24 23:27:17 -04001323 const uint8_t *selected = s->s3->alpn_selected;
1324 size_t len = s->s3->alpn_selected_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001325
1326 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1327 return NULL;
1328 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1329 s2n(3 + len,ret);
1330 s2n(1 + len,ret);
1331 *ret++ = len;
1332 memcpy(ret, selected, len);
1333 ret += len;
1334 }
1335
Adam Langley1258b6a2014-06-20 12:00:00 -07001336 /* If the client advertised support for Channel ID, and we have it
1337 * enabled, then we want to echo it back. */
1338 if (s->s3->tlsext_channel_id_valid)
1339 {
1340 if (limit - ret - 4 < 0)
1341 return NULL;
1342 if (s->s3->tlsext_channel_id_new)
1343 s2n(TLSEXT_TYPE_channel_id_new,ret);
1344 else
1345 s2n(TLSEXT_TYPE_channel_id,ret);
1346 s2n(0,ret);
1347 }
1348
Adam Langleyb0c235e2014-06-20 12:00:00 -07001349 if ((extdatalen = ret-orig-2) == 0)
1350 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001351
Adam Langleyb0c235e2014-06-20 12:00:00 -07001352 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001353 return ret;
1354 }
1355
Adam Langley95c29f32014-06-20 12:00:00 -07001356/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1357 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001358 * cbs: the contents of the extension, not including the type and length.
1359 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001360 * return.
1361 *
David Benjamindc72ff72014-06-25 12:36:10 -04001362 * returns: 1 on success. */
1363static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001364 {
Adam Langleyded93582014-07-31 15:23:51 -07001365 CBS protocol_name_list, protocol_name_list_copy;
Adam Langley95c29f32014-06-20 12:00:00 -07001366 const unsigned char *selected;
1367 unsigned char selected_len;
1368 int r;
1369
1370 if (s->ctx->alpn_select_cb == NULL)
David Benjamindc72ff72014-06-25 12:36:10 -04001371 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001372
David Benjamindc72ff72014-06-25 12:36:10 -04001373 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1374 CBS_len(cbs) != 0 ||
1375 CBS_len(&protocol_name_list) < 2)
Adam Langley95c29f32014-06-20 12:00:00 -07001376 goto parse_error;
1377
David Benjamindc72ff72014-06-25 12:36:10 -04001378 /* Validate the protocol list. */
Adam Langleyded93582014-07-31 15:23:51 -07001379 protocol_name_list_copy = protocol_name_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001380 while (CBS_len(&protocol_name_list_copy) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001381 {
David Benjamindc72ff72014-06-25 12:36:10 -04001382 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001383
David Benjamindc72ff72014-06-25 12:36:10 -04001384 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001385 goto parse_error;
Adam Langley95c29f32014-06-20 12:00:00 -07001386 }
1387
David Benjamindc72ff72014-06-25 12:36:10 -04001388 r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1389 CBS_data(&protocol_name_list), CBS_len(&protocol_name_list),
1390 s->ctx->alpn_select_cb_arg);
Adam Langley95c29f32014-06-20 12:00:00 -07001391 if (r == SSL_TLSEXT_ERR_OK) {
1392 if (s->s3->alpn_selected)
1393 OPENSSL_free(s->s3->alpn_selected);
David Benjamin072c9532014-07-26 11:44:25 -04001394 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001395 if (!s->s3->alpn_selected)
1396 {
David Benjamindc72ff72014-06-25 12:36:10 -04001397 *out_alert = SSL_AD_INTERNAL_ERROR;
1398 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001399 }
Adam Langley95c29f32014-06-20 12:00:00 -07001400 s->s3->alpn_selected_len = selected_len;
1401 }
David Benjamindc72ff72014-06-25 12:36:10 -04001402 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001403
1404parse_error:
David Benjamindc72ff72014-06-25 12:36:10 -04001405 *out_alert = SSL_AD_DECODE_ERROR;
1406 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001407 }
1408
David Benjamindc72ff72014-06-25 12:36:10 -04001409static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001410 {
Adam Langley95c29f32014-06-20 12:00:00 -07001411 int renegotiate_seen = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001412 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001413
Adam Langleyed8270a2014-09-02 13:52:56 -07001414 s->should_ack_sni = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001415 s->s3->next_proto_neg_seen = 0;
David Benjamin6c7aed02014-08-27 16:42:38 -04001416 s->s3->tmp.certificate_status_expected = 0;
Adam Langley75712922014-10-10 16:23:43 -07001417 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001418
Adam Langley95c29f32014-06-20 12:00:00 -07001419 if (s->s3->alpn_selected)
1420 {
1421 OPENSSL_free(s->s3->alpn_selected);
1422 s->s3->alpn_selected = NULL;
1423 }
1424
Adam Langley95c29f32014-06-20 12:00:00 -07001425 /* Clear any signature algorithms extension received */
1426 if (s->cert->peer_sigalgs)
1427 {
1428 OPENSSL_free(s->cert->peer_sigalgs);
1429 s->cert->peer_sigalgs = NULL;
1430 }
David Benjamina19fc252014-10-19 00:14:36 -04001431 /* Clear any shared signature algorithms */
Adam Langley95c29f32014-06-20 12:00:00 -07001432 if (s->cert->shared_sigalgs)
1433 {
1434 OPENSSL_free(s->cert->shared_sigalgs);
1435 s->cert->shared_sigalgs = NULL;
1436 }
David Benjamina19fc252014-10-19 00:14:36 -04001437 /* Clear ECC extensions */
1438 if (s->s3->tmp.peer_ecpointformatlist != 0)
1439 {
1440 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1441 s->s3->tmp.peer_ecpointformatlist = NULL;
1442 s->s3->tmp.peer_ecpointformatlist_length = 0;
1443 }
1444 if (s->s3->tmp.peer_ellipticcurvelist != 0)
1445 {
1446 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1447 s->s3->tmp.peer_ellipticcurvelist = NULL;
1448 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1449 }
Adam Langley95c29f32014-06-20 12:00:00 -07001450
David Benjamindc72ff72014-06-25 12:36:10 -04001451 /* There may be no extensions. */
1452 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001453 {
David Benjamindc72ff72014-06-25 12:36:10 -04001454 goto ri_check;
1455 }
Adam Langley95c29f32014-06-20 12:00:00 -07001456
David Benjamin35a7a442014-07-05 00:23:20 -04001457 /* Decode the extensions block and check it is valid. */
1458 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1459 !tls1_check_duplicate_extensions(&extensions))
David Benjamindc72ff72014-06-25 12:36:10 -04001460 {
1461 *out_alert = SSL_AD_DECODE_ERROR;
1462 return 0;
1463 }
1464
David Benjamindc72ff72014-06-25 12:36:10 -04001465 while (CBS_len(&extensions) != 0)
1466 {
1467 uint16_t type;
1468 CBS extension;
1469
1470 /* Decode the next extension. */
1471 if (!CBS_get_u16(&extensions, &type) ||
1472 !CBS_get_u16_length_prefixed(&extensions, &extension))
1473 {
1474 *out_alert = SSL_AD_DECODE_ERROR;
1475 return 0;
1476 }
1477
Adam Langley95c29f32014-06-20 12:00:00 -07001478 if (s->tlsext_debug_cb)
David Benjamindc72ff72014-06-25 12:36:10 -04001479 {
1480 s->tlsext_debug_cb(s, 0, type, (unsigned char*)CBS_data(&extension),
1481 CBS_len(&extension), s->tlsext_debug_arg);
1482 }
1483
Adam Langley95c29f32014-06-20 12:00:00 -07001484/* The servername extension is treated as follows:
1485
1486 - Only the hostname type is supported with a maximum length of 255.
1487 - The servername is rejected if too long or if it contains zeros,
1488 in which case an fatal alert is generated.
1489 - The servername field is maintained together with the session cache.
1490 - When a session is resumed, the servername call back invoked in order
1491 to allow the application to position itself to the right context.
1492 - The servername is acknowledged if it is new for a session or when
1493 it is identical to a previously used for the same session.
1494 Applications can control the behaviour. They can at any time
1495 set a 'desirable' servername for a new SSL object. This can be the
1496 case for example with HTTPS when a Host: header field is received and
1497 a renegotiation is requested. In this case, a possible servername
1498 presented in the new client hello is only acknowledged if it matches
1499 the value of the Host: field.
1500 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1501 if they provide for changing an explicit servername context for the session,
1502 i.e. when the session has been established with a servername extension.
1503 - On session reconnect, the servername extension may be absent.
1504
1505*/
1506
1507 if (type == TLSEXT_TYPE_server_name)
1508 {
David Benjamindc72ff72014-06-25 12:36:10 -04001509 CBS server_name_list;
Adam Langleyed8270a2014-09-02 13:52:56 -07001510 char have_seen_host_name = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001511
1512 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1513 CBS_len(&server_name_list) < 1 ||
1514 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001515 {
David Benjamindc72ff72014-06-25 12:36:10 -04001516 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001517 return 0;
1518 }
Adam Langley95c29f32014-06-20 12:00:00 -07001519
David Benjamindc72ff72014-06-25 12:36:10 -04001520 /* Decode each ServerName in the extension. */
1521 while (CBS_len(&server_name_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001522 {
David Benjamindc72ff72014-06-25 12:36:10 -04001523 uint8_t name_type;
1524 CBS host_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001525
David Benjamindc72ff72014-06-25 12:36:10 -04001526 /* Decode the NameType. */
1527 if (!CBS_get_u8(&server_name_list, &name_type))
Adam Langley95c29f32014-06-20 12:00:00 -07001528 {
David Benjamindc72ff72014-06-25 12:36:10 -04001529 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001530 return 0;
1531 }
David Benjamindc72ff72014-06-25 12:36:10 -04001532
David Benjamindc72ff72014-06-25 12:36:10 -04001533 /* Only host_name is supported. */
1534 if (name_type != TLSEXT_NAMETYPE_host_name)
1535 continue;
1536
Adam Langleyed8270a2014-09-02 13:52:56 -07001537 if (have_seen_host_name)
1538 {
1539 /* The ServerNameList MUST NOT contain
1540 * more than one name of the same
1541 * name_type. */
1542 *out_alert = SSL_AD_DECODE_ERROR;
1543 return 0;
1544 }
1545
1546 have_seen_host_name = 1;
1547
1548 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1549 CBS_len(&host_name) < 1)
1550 {
1551 *out_alert = SSL_AD_DECODE_ERROR;
1552 return 0;
1553 }
1554
1555 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1556 CBS_contains_zero_byte(&host_name))
1557 {
1558 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1559 return 0;
1560 }
1561
David Benjamindc72ff72014-06-25 12:36:10 -04001562 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -07001563 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001564 assert(s->session->tlsext_hostname == NULL);
David Benjamindc72ff72014-06-25 12:36:10 -04001565 if (s->session->tlsext_hostname)
Adam Langley95c29f32014-06-20 12:00:00 -07001566 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001567 /* This should be impossible. */
David Benjamindc72ff72014-06-25 12:36:10 -04001568 *out_alert = SSL_AD_DECODE_ERROR;
1569 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001570 }
Adam Langley95c29f32014-06-20 12:00:00 -07001571
David Benjamindc72ff72014-06-25 12:36:10 -04001572 /* Copy the hostname as a string. */
David Benjamined439582014-07-14 19:13:02 -04001573 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname))
David Benjamindc72ff72014-06-25 12:36:10 -04001574 {
1575 *out_alert = SSL_AD_INTERNAL_ERROR;
1576 return 0;
1577 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001578
1579 s->should_ack_sni = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001580 }
Adam Langley95c29f32014-06-20 12:00:00 -07001581 }
Adam Langley95c29f32014-06-20 12:00:00 -07001582 }
1583
Adam Langley95c29f32014-06-20 12:00:00 -07001584 else if (type == TLSEXT_TYPE_ec_point_formats)
1585 {
David Benjamindc72ff72014-06-25 12:36:10 -04001586 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001587
David Benjamindc72ff72014-06-25 12:36:10 -04001588 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1589 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001590 {
David Benjamindc72ff72014-06-25 12:36:10 -04001591 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001592 return 0;
1593 }
David Benjamindc72ff72014-06-25 12:36:10 -04001594
David Benjamina19fc252014-10-19 00:14:36 -04001595 if (!CBS_stow(&ec_point_format_list,
1596 &s->s3->tmp.peer_ecpointformatlist,
1597 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001598 {
David Benjamina19fc252014-10-19 00:14:36 -04001599 *out_alert = SSL_AD_INTERNAL_ERROR;
1600 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001601 }
Adam Langley95c29f32014-06-20 12:00:00 -07001602 }
1603 else if (type == TLSEXT_TYPE_elliptic_curves)
1604 {
David Benjamindc72ff72014-06-25 12:36:10 -04001605 CBS elliptic_curve_list;
David Benjamin072334d2014-07-13 16:24:27 -04001606 size_t i, num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001607
David Benjamindc72ff72014-06-25 12:36:10 -04001608 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
David Benjamin072334d2014-07-13 16:24:27 -04001609 CBS_len(&elliptic_curve_list) == 0 ||
1610 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
David Benjamindc72ff72014-06-25 12:36:10 -04001611 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001612 {
David Benjamindc72ff72014-06-25 12:36:10 -04001613 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001614 return 0;
1615 }
David Benjamindc72ff72014-06-25 12:36:10 -04001616
David Benjamina19fc252014-10-19 00:14:36 -04001617 if (s->s3->tmp.peer_ellipticcurvelist)
Adam Langley95c29f32014-06-20 12:00:00 -07001618 {
David Benjamina19fc252014-10-19 00:14:36 -04001619 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1620 s->s3->tmp.peer_ellipticcurvelist_length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001621 }
David Benjamina19fc252014-10-19 00:14:36 -04001622 s->s3->tmp.peer_ellipticcurvelist =
1623 (uint16_t*)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
1624 if (s->s3->tmp.peer_ellipticcurvelist == NULL)
1625 {
1626 *out_alert = SSL_AD_INTERNAL_ERROR;
1627 return 0;
1628 }
1629 num_curves = CBS_len(&elliptic_curve_list) / 2;
1630 for (i = 0; i < num_curves; i++)
1631 {
1632 if (!CBS_get_u16(&elliptic_curve_list,
1633 &s->s3->tmp.peer_ellipticcurvelist[i]))
1634 {
1635 *out_alert = SSL_AD_INTERNAL_ERROR;
1636 return 0;
1637 }
1638 }
1639 if (CBS_len(&elliptic_curve_list) != 0)
1640 {
1641 *out_alert = SSL_AD_INTERNAL_ERROR;
1642 return 0;
1643 }
1644 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001645 }
Adam Langley95c29f32014-06-20 12:00:00 -07001646 else if (type == TLSEXT_TYPE_renegotiate)
1647 {
David Benjamindc72ff72014-06-25 12:36:10 -04001648 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001649 return 0;
1650 renegotiate_seen = 1;
1651 }
1652 else if (type == TLSEXT_TYPE_signature_algorithms)
1653 {
David Benjamindc72ff72014-06-25 12:36:10 -04001654 CBS supported_signature_algorithms;
1655
David Benjamindc72ff72014-06-25 12:36:10 -04001656 if (!CBS_get_u16_length_prefixed(&extension, &supported_signature_algorithms) ||
1657 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001658 {
David Benjamindc72ff72014-06-25 12:36:10 -04001659 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001660 return 0;
1661 }
David Benjamindc72ff72014-06-25 12:36:10 -04001662
1663 /* Ensure the signature algorithms are non-empty. It
1664 * contains a list of SignatureAndHashAlgorithms
1665 * which are two bytes each. */
1666 if (CBS_len(&supported_signature_algorithms) == 0 ||
1667 (CBS_len(&supported_signature_algorithms) % 2) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001668 {
David Benjamindc72ff72014-06-25 12:36:10 -04001669 *out_alert = SSL_AD_DECODE_ERROR;
1670 return 0;
1671 }
1672
David Benjamincd996942014-07-20 16:23:51 -04001673 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
David Benjamindc72ff72014-06-25 12:36:10 -04001674 {
1675 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001676 return 0;
1677 }
1678 /* If sigalgs received and no shared algorithms fatal
1679 * error.
1680 */
1681 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
1682 {
1683 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
David Benjamindc72ff72014-06-25 12:36:10 -04001684 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07001685 return 0;
1686 }
1687 }
1688
Adam Langley95c29f32014-06-20 12:00:00 -07001689 else if (type == TLSEXT_TYPE_next_proto_neg &&
1690 s->s3->tmp.finish_md_len == 0 &&
1691 s->s3->alpn_selected == NULL)
1692 {
David Benjamindc72ff72014-06-25 12:36:10 -04001693 /* The extension must be empty. */
1694 if (CBS_len(&extension) != 0)
1695 {
1696 *out_alert = SSL_AD_DECODE_ERROR;
1697 return 0;
1698 }
1699
Adam Langley95c29f32014-06-20 12:00:00 -07001700 /* We shouldn't accept this extension on a
1701 * renegotiation.
1702 *
1703 * s->new_session will be set on renegotiation, but we
1704 * probably shouldn't rely that it couldn't be set on
1705 * the initial renegotation too in certain cases (when
1706 * there's some other reason to disallow resuming an
1707 * earlier session -- the current code won't be doing
1708 * anything like that, but this might change).
1709
1710 * A valid sign that there's been a previous handshake
1711 * in this connection is if s->s3->tmp.finish_md_len >
1712 * 0. (We are talking about a check that will happen
1713 * in the Hello protocol round, well before a new
1714 * Finished message could have been computed.) */
1715 s->s3->next_proto_neg_seen = 1;
1716 }
Adam Langley95c29f32014-06-20 12:00:00 -07001717
1718 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1719 s->ctx->alpn_select_cb &&
1720 s->s3->tmp.finish_md_len == 0)
1721 {
David Benjamindc72ff72014-06-25 12:36:10 -04001722 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001723 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001724 /* ALPN takes precedence over NPN. */
1725 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001726 }
1727
Adam Langley1258b6a2014-06-20 12:00:00 -07001728 else if (type == TLSEXT_TYPE_channel_id &&
1729 s->tlsext_channel_id_enabled)
David Benjamindc72ff72014-06-25 12:36:10 -04001730 {
1731 /* The extension must be empty. */
1732 if (CBS_len(&extension) != 0)
1733 {
1734 *out_alert = SSL_AD_DECODE_ERROR;
1735 return 0;
1736 }
1737
Adam Langley1258b6a2014-06-20 12:00:00 -07001738 s->s3->tlsext_channel_id_valid = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001739 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001740
1741 else if (type == TLSEXT_TYPE_channel_id_new &&
1742 s->tlsext_channel_id_enabled)
1743 {
David Benjamindc72ff72014-06-25 12:36:10 -04001744 /* The extension must be empty. */
1745 if (CBS_len(&extension) != 0)
1746 {
1747 *out_alert = SSL_AD_DECODE_ERROR;
1748 return 0;
1749 }
1750
Adam Langley1258b6a2014-06-20 12:00:00 -07001751 s->s3->tlsext_channel_id_valid = 1;
1752 s->s3->tlsext_channel_id_new = 1;
1753 }
1754
1755
Adam Langley95c29f32014-06-20 12:00:00 -07001756 /* session ticket processed earlier */
1757 else if (type == TLSEXT_TYPE_use_srtp)
1758 {
David Benjamindc72ff72014-06-25 12:36:10 -04001759 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001760 return 0;
1761 }
Adam Langley75712922014-10-10 16:23:43 -07001762
1763 else if (type == TLSEXT_TYPE_extended_master_secret &&
1764 s->version != SSL3_VERSION)
1765 {
1766 if (CBS_len(&extension) != 0)
1767 {
1768 *out_alert = SSL_AD_DECODE_ERROR;
1769 return 0;
1770 }
1771
1772 s->s3->tmp.extended_master_secret = 1;
1773 }
Adam Langley95c29f32014-06-20 12:00:00 -07001774 }
1775
Adam Langley95c29f32014-06-20 12:00:00 -07001776 ri_check:
1777
1778 /* Need RI if renegotiating */
1779
1780 if (!renegotiate_seen && s->renegotiate &&
1781 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1782 {
David Benjamindc72ff72014-06-25 12:36:10 -04001783 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin172fc2c2014-09-06 13:09:47 -04001784 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07001785 return 0;
1786 }
Adam Langley95c29f32014-06-20 12:00:00 -07001787
1788 return 1;
1789 }
1790
David Benjamindc72ff72014-06-25 12:36:10 -04001791int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001792 {
David Benjamindc72ff72014-06-25 12:36:10 -04001793 int alert = -1;
1794 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001795 {
David Benjamindc72ff72014-06-25 12:36:10 -04001796 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07001797 return 0;
1798 }
1799
David Benjamin6c7aed02014-08-27 16:42:38 -04001800 if (ssl_check_clienthello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001801 {
David Benjamin9d28c752014-07-05 00:43:48 -04001802 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07001803 return 0;
1804 }
1805 return 1;
David Benjamin072334d2014-07-13 16:24:27 -04001806 }
Adam Langley95c29f32014-06-20 12:00:00 -07001807
Adam Langley95c29f32014-06-20 12:00:00 -07001808/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1809 * elements of zero length are allowed and the set of elements must exactly fill
1810 * the length of the block. */
David Benjamin03973092014-06-24 23:27:17 -04001811static char ssl_next_proto_validate(const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001812 {
David Benjamin03973092014-06-24 23:27:17 -04001813 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001814
David Benjamin03973092014-06-24 23:27:17 -04001815 while (CBS_len(&copy) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001816 {
David Benjamin03973092014-06-24 23:27:17 -04001817 CBS proto;
1818 if (!CBS_get_u8_length_prefixed(&copy, &proto) ||
1819 CBS_len(&proto) == 0)
1820 {
Adam Langley95c29f32014-06-20 12:00:00 -07001821 return 0;
David Benjamin03973092014-06-24 23:27:17 -04001822 }
Adam Langley95c29f32014-06-20 12:00:00 -07001823 }
David Benjamin03973092014-06-24 23:27:17 -04001824 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001825 }
Adam Langley95c29f32014-06-20 12:00:00 -07001826
David Benjamin03973092014-06-24 23:27:17 -04001827static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001828 {
Adam Langley95c29f32014-06-20 12:00:00 -07001829 int tlsext_servername = 0;
1830 int renegotiate_seen = 0;
David Benjamin03973092014-06-24 23:27:17 -04001831 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001832
David Benjamin6c7aed02014-08-27 16:42:38 -04001833 /* TODO(davidben): Move all of these to some per-handshake state that
1834 * gets systematically reset on a new handshake; perhaps allocate it
1835 * fresh each time so it's not even kept around post-handshake. */
Adam Langley95c29f32014-06-20 12:00:00 -07001836 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001837
David Benjamin6c7aed02014-08-27 16:42:38 -04001838 s->tlsext_ticket_expected = 0;
1839 s->s3->tmp.certificate_status_expected = 0;
Adam Langley75712922014-10-10 16:23:43 -07001840 s->s3->tmp.extended_master_secret = 0;
David Benjamin64442872014-07-21 17:43:45 -04001841
Adam Langley95c29f32014-06-20 12:00:00 -07001842 if (s->s3->alpn_selected)
1843 {
1844 OPENSSL_free(s->s3->alpn_selected);
1845 s->s3->alpn_selected = NULL;
1846 }
1847
David Benjamina19fc252014-10-19 00:14:36 -04001848 /* Clear ECC extensions */
1849 if (s->s3->tmp.peer_ecpointformatlist != 0)
1850 {
1851 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1852 s->s3->tmp.peer_ecpointformatlist = NULL;
1853 s->s3->tmp.peer_ecpointformatlist_length = 0;
1854 }
1855
David Benjamin03973092014-06-24 23:27:17 -04001856 /* There may be no extensions. */
1857 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001858 {
David Benjamin03973092014-06-24 23:27:17 -04001859 goto ri_check;
1860 }
1861
David Benjamin35a7a442014-07-05 00:23:20 -04001862 /* Decode the extensions block and check it is valid. */
1863 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1864 !tls1_check_duplicate_extensions(&extensions))
David Benjamin03973092014-06-24 23:27:17 -04001865 {
1866 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001867 return 0;
1868 }
1869
David Benjamin03973092014-06-24 23:27:17 -04001870 while (CBS_len(&extensions) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001871 {
David Benjamin03973092014-06-24 23:27:17 -04001872 uint16_t type;
1873 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001874
David Benjamin03973092014-06-24 23:27:17 -04001875 /* Decode the next extension. */
1876 if (!CBS_get_u16(&extensions, &type) ||
1877 !CBS_get_u16_length_prefixed(&extensions, &extension))
1878 {
1879 *out_alert = SSL_AD_DECODE_ERROR;
1880 return 0;
1881 }
Adam Langley95c29f32014-06-20 12:00:00 -07001882
1883 if (s->tlsext_debug_cb)
David Benjamin03973092014-06-24 23:27:17 -04001884 {
1885 s->tlsext_debug_cb(s, 1, type, (unsigned char*)CBS_data(&extension),
1886 CBS_len(&extension), s->tlsext_debug_arg);
1887 }
Adam Langley95c29f32014-06-20 12:00:00 -07001888
1889 if (type == TLSEXT_TYPE_server_name)
1890 {
David Benjamin03973092014-06-24 23:27:17 -04001891 /* The extension must be empty. */
1892 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001893 {
David Benjamin03973092014-06-24 23:27:17 -04001894 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001895 return 0;
1896 }
David Benjamin03973092014-06-24 23:27:17 -04001897 /* We must have sent it in ClientHello. */
1898 if (s->tlsext_hostname == NULL)
1899 {
1900 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1901 return 0;
1902 }
1903 tlsext_servername = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001904 }
Adam Langley95c29f32014-06-20 12:00:00 -07001905 else if (type == TLSEXT_TYPE_ec_point_formats)
1906 {
David Benjamin03973092014-06-24 23:27:17 -04001907 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001908
David Benjamin03973092014-06-24 23:27:17 -04001909 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1910 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001911 {
David Benjamin03973092014-06-24 23:27:17 -04001912 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001913 return 0;
1914 }
David Benjamin03973092014-06-24 23:27:17 -04001915
David Benjamina19fc252014-10-19 00:14:36 -04001916 if (!CBS_stow(&ec_point_format_list,
1917 &s->s3->tmp.peer_ecpointformatlist,
1918 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001919 {
David Benjamina19fc252014-10-19 00:14:36 -04001920 *out_alert = SSL_AD_INTERNAL_ERROR;
1921 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001922 }
Adam Langley95c29f32014-06-20 12:00:00 -07001923 }
Adam Langley95c29f32014-06-20 12:00:00 -07001924 else if (type == TLSEXT_TYPE_session_ticket)
1925 {
David Benjamin03973092014-06-24 23:27:17 -04001926 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001927 {
David Benjamin03973092014-06-24 23:27:17 -04001928 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07001929 return 0;
1930 }
David Benjamin03973092014-06-24 23:27:17 -04001931
Adam Langley95c29f32014-06-20 12:00:00 -07001932 s->tlsext_ticket_expected = 1;
1933 }
Adam Langley95c29f32014-06-20 12:00:00 -07001934 else if (type == TLSEXT_TYPE_status_request)
1935 {
David Benjamin03973092014-06-24 23:27:17 -04001936 /* The extension MUST be empty and may only sent if
1937 * we've requested a status request message. */
1938 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001939 {
David Benjamin03973092014-06-24 23:27:17 -04001940 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001941 return 0;
1942 }
David Benjamin6c7aed02014-08-27 16:42:38 -04001943 if (!s->ocsp_stapling_enabled)
David Benjamin03973092014-06-24 23:27:17 -04001944 {
1945 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1946 return 0;
1947 }
1948 /* Set a flag to expect a CertificateStatus message */
David Benjamin6c7aed02014-08-27 16:42:38 -04001949 s->s3->tmp.certificate_status_expected = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001950 }
David Benjamin03973092014-06-24 23:27:17 -04001951 else if (type == TLSEXT_TYPE_next_proto_neg && s->s3->tmp.finish_md_len == 0) {
1952 unsigned char *selected;
1953 unsigned char selected_len;
1954
1955 /* We must have requested it. */
1956 if (s->ctx->next_proto_select_cb == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001957 {
David Benjamin03973092014-06-24 23:27:17 -04001958 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1959 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001960 }
Adam Langley95c29f32014-06-20 12:00:00 -07001961
David Benjamin03973092014-06-24 23:27:17 -04001962 /* The data must be valid. */
1963 if (!ssl_next_proto_validate(&extension))
1964 {
1965 *out_alert = SSL_AD_DECODE_ERROR;
1966 return 0;
1967 }
1968
1969 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len,
1970 CBS_data(&extension), CBS_len(&extension),
1971 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
1972 {
1973 *out_alert = SSL_AD_INTERNAL_ERROR;
1974 return 0;
1975 }
1976
1977 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
1978 if (s->next_proto_negotiated == NULL)
1979 {
1980 *out_alert = SSL_AD_INTERNAL_ERROR;
1981 return 0;
1982 }
1983 s->next_proto_negotiated_len = selected_len;
1984 s->s3->next_proto_neg_seen = 1;
1985 }
Adam Langley95c29f32014-06-20 12:00:00 -07001986 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
1987 {
David Benjamin03973092014-06-24 23:27:17 -04001988 CBS protocol_name_list, protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001989
1990 /* We must have requested it. */
1991 if (s->alpn_client_proto_list == NULL)
1992 {
David Benjamin03973092014-06-24 23:27:17 -04001993 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07001994 return 0;
1995 }
David Benjamin03973092014-06-24 23:27:17 -04001996
1997 /* The extension data consists of a ProtocolNameList
1998 * which must have exactly one ProtocolName. Each of
1999 * these is length-prefixed. */
2000 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
2001 CBS_len(&extension) != 0 ||
2002 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
2003 CBS_len(&protocol_name_list) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002004 {
David Benjamin03973092014-06-24 23:27:17 -04002005 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002006 return 0;
2007 }
David Benjamin03973092014-06-24 23:27:17 -04002008
2009 if (!CBS_stow(&protocol_name,
2010 &s->s3->alpn_selected,
2011 &s->s3->alpn_selected_len))
Adam Langley95c29f32014-06-20 12:00:00 -07002012 {
David Benjamin03973092014-06-24 23:27:17 -04002013 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002014 return 0;
2015 }
Adam Langley95c29f32014-06-20 12:00:00 -07002016 }
2017
Adam Langley1258b6a2014-06-20 12:00:00 -07002018 else if (type == TLSEXT_TYPE_channel_id)
David Benjamin03973092014-06-24 23:27:17 -04002019 {
2020 if (CBS_len(&extension) != 0)
2021 {
2022 *out_alert = SSL_AD_DECODE_ERROR;
2023 return 0;
2024 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002025 s->s3->tlsext_channel_id_valid = 1;
David Benjamin03973092014-06-24 23:27:17 -04002026 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002027 else if (type == TLSEXT_TYPE_channel_id_new)
2028 {
David Benjamin03973092014-06-24 23:27:17 -04002029 if (CBS_len(&extension) != 0)
2030 {
2031 *out_alert = SSL_AD_DECODE_ERROR;
2032 return 0;
2033 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002034 s->s3->tlsext_channel_id_valid = 1;
2035 s->s3->tlsext_channel_id_new = 1;
2036 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002037 else if (type == TLSEXT_TYPE_certificate_timestamp)
2038 {
2039 if (CBS_len(&extension) == 0)
2040 {
2041 *out_alert = SSL_AD_DECODE_ERROR;
2042 return 0;
2043 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002044
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002045 /* Session resumption uses the original session information. */
2046 if (!s->hit)
2047 {
2048 if (!CBS_stow(&extension,
2049 &s->session->tlsext_signed_cert_timestamp_list,
2050 &s->session->tlsext_signed_cert_timestamp_list_length))
2051 {
2052 *out_alert = SSL_AD_INTERNAL_ERROR;
2053 return 0;
2054 }
2055 }
2056 }
Adam Langley95c29f32014-06-20 12:00:00 -07002057 else if (type == TLSEXT_TYPE_renegotiate)
2058 {
David Benjamin03973092014-06-24 23:27:17 -04002059 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002060 return 0;
2061 renegotiate_seen = 1;
2062 }
Adam Langley95c29f32014-06-20 12:00:00 -07002063 else if (type == TLSEXT_TYPE_use_srtp)
2064 {
David Benjamin03973092014-06-24 23:27:17 -04002065 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002066 return 0;
2067 }
Adam Langley75712922014-10-10 16:23:43 -07002068
2069 else if (type == TLSEXT_TYPE_extended_master_secret)
2070 {
2071 if (/* It is invalid for the server to select EMS and
2072 SSLv3. */
2073 s->version == SSL3_VERSION ||
2074 CBS_len(&extension) != 0)
2075 {
2076 *out_alert = SSL_AD_DECODE_ERROR;
2077 return 0;
2078 }
2079
2080 s->s3->tmp.extended_master_secret = 1;
2081 }
Adam Langley95c29f32014-06-20 12:00:00 -07002082 }
2083
2084 if (!s->hit && tlsext_servername == 1)
2085 {
2086 if (s->tlsext_hostname)
2087 {
2088 if (s->session->tlsext_hostname == NULL)
2089 {
2090 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2091 if (!s->session->tlsext_hostname)
2092 {
David Benjamin03973092014-06-24 23:27:17 -04002093 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002094 return 0;
2095 }
2096 }
2097 else
2098 {
David Benjamin03973092014-06-24 23:27:17 -04002099 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002100 return 0;
2101 }
2102 }
2103 }
2104
Adam Langley95c29f32014-06-20 12:00:00 -07002105 ri_check:
2106
2107 /* Determine if we need to see RI. Strictly speaking if we want to
2108 * avoid an attack we should *always* see RI even on initial server
2109 * hello because the client doesn't see any renegotiation during an
2110 * attack. However this would mean we could not connect to any server
2111 * which doesn't support RI so for the immediate future tolerate RI
2112 * absence on initial connect only.
2113 */
2114 if (!renegotiate_seen
2115 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2116 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2117 {
David Benjamin03973092014-06-24 23:27:17 -04002118 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin9d28c752014-07-05 00:43:48 -04002119 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07002120 return 0;
2121 }
2122
2123 return 1;
2124 }
2125
2126
2127int ssl_prepare_clienthello_tlsext(SSL *s)
2128 {
Adam Langley95c29f32014-06-20 12:00:00 -07002129 return 1;
2130 }
2131
2132int ssl_prepare_serverhello_tlsext(SSL *s)
2133 {
2134 return 1;
2135 }
2136
David Benjamin6c7aed02014-08-27 16:42:38 -04002137static int ssl_check_clienthello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002138 {
2139 int ret=SSL_TLSEXT_ERR_NOACK;
2140 int al = SSL_AD_UNRECOGNIZED_NAME;
2141
Adam Langley95c29f32014-06-20 12:00:00 -07002142 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2143 * ssl3_choose_cipher in s3_lib.c.
2144 */
2145 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2146 * ssl3_choose_cipher in s3_lib.c.
2147 */
Adam Langley95c29f32014-06-20 12:00:00 -07002148
2149 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2150 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2151 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2152 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2153
Adam Langley95c29f32014-06-20 12:00:00 -07002154 switch (ret)
2155 {
2156 case SSL_TLSEXT_ERR_ALERT_FATAL:
2157 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2158 return -1;
2159
2160 case SSL_TLSEXT_ERR_ALERT_WARNING:
2161 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002162 return 1;
2163
Adam Langley95c29f32014-06-20 12:00:00 -07002164 case SSL_TLSEXT_ERR_NOACK:
Adam Langleyed8270a2014-09-02 13:52:56 -07002165 s->should_ack_sni = 0;
2166 return 1;
2167
2168 default:
2169 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002170 }
2171 }
2172
David Benjamin6c7aed02014-08-27 16:42:38 -04002173static int ssl_check_serverhello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002174 {
2175 int ret=SSL_TLSEXT_ERR_NOACK;
2176 int al = SSL_AD_UNRECOGNIZED_NAME;
2177
Adam Langley95c29f32014-06-20 12:00:00 -07002178 /* If we are client and using an elliptic curve cryptography cipher
2179 * suite, then if server returns an EC point formats lists extension
2180 * it must contain uncompressed.
2181 */
2182 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2183 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamina9ca90a2014-09-26 18:53:43 -04002184 if (((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) &&
2185 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed))
Adam Langley95c29f32014-06-20 12:00:00 -07002186 {
David Benjamina9ca90a2014-09-26 18:53:43 -04002187 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2188 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002189 }
2190 ret = SSL_TLSEXT_ERR_OK;
Adam Langley95c29f32014-06-20 12:00:00 -07002191
2192 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2193 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2194 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2195 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2196
Adam Langley95c29f32014-06-20 12:00:00 -07002197 switch (ret)
2198 {
2199 case SSL_TLSEXT_ERR_ALERT_FATAL:
Adam Langleyed8270a2014-09-02 13:52:56 -07002200 ssl3_send_alert(s,SSL3_AL_FATAL,al);
Adam Langley95c29f32014-06-20 12:00:00 -07002201 return -1;
2202
2203 case SSL_TLSEXT_ERR_ALERT_WARNING:
2204 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002205 return 1;
2206
2207 default:
2208 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002209 }
2210 }
2211
David Benjamin03973092014-06-24 23:27:17 -04002212int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07002213 {
David Benjamin03973092014-06-24 23:27:17 -04002214 int alert = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002215 if (s->version < SSL3_VERSION)
2216 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002217
2218 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002219 {
David Benjamin03973092014-06-24 23:27:17 -04002220 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07002221 return 0;
2222 }
2223
David Benjamin03973092014-06-24 23:27:17 -04002224 if (ssl_check_serverhello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002225 {
David Benjamin9d28c752014-07-05 00:43:48 -04002226 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, SSL_R_SERVERHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07002227 return 0;
2228 }
David Benjamin03973092014-06-24 23:27:17 -04002229
Adam Langley95c29f32014-06-20 12:00:00 -07002230 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002231 }
Adam Langley95c29f32014-06-20 12:00:00 -07002232
2233/* Since the server cache lookup is done early on in the processing of the
2234 * ClientHello, and other operations depend on the result, we need to handle
2235 * any TLS session ticket extension at the same time.
2236 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002237 * ctx: contains the early callback context, which is the result of a
2238 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002239 * ret: (output) on return, if a ticket was decrypted, then this is set to
2240 * point to the resulting session.
2241 *
Adam Langley95c29f32014-06-20 12:00:00 -07002242 * Returns:
2243 * -1: fatal error, either from parsing or decrypting the ticket.
2244 * 0: no ticket was found (or was ignored, based on settings).
2245 * 1: a zero length extension was found, indicating that the client supports
2246 * session tickets but doesn't currently have one to offer.
David Benjamind1681e62014-11-20 14:54:14 -05002247 * 2: a ticket was offered but couldn't be decrypted because of a non-fatal
2248 * error.
Adam Langley95c29f32014-06-20 12:00:00 -07002249 * 3: a ticket was successfully decrypted and *ret was set.
2250 *
2251 * Side effects:
2252 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2253 * a new session ticket to the client because the client indicated support
David Benjamind1681e62014-11-20 14:54:14 -05002254 * but the client either doesn't have a session ticket or we couldn't use
2255 * the one it gave us, or if s->ctx->tlsext_ticket_key_cb asked to renew
2256 * the client's ticket. Otherwise, s->tlsext_ticket_expected is set to 0.
Adam Langley95c29f32014-06-20 12:00:00 -07002257 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002258int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
2259 SSL_SESSION **ret)
Adam Langley95c29f32014-06-20 12:00:00 -07002260 {
Adam Langley95c29f32014-06-20 12:00:00 -07002261 *ret = NULL;
2262 s->tlsext_ticket_expected = 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002263 const unsigned char *data;
2264 size_t len;
2265 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002266
2267 /* If tickets disabled behave as if no ticket present
2268 * to permit stateful resumption.
2269 */
2270 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2271 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002272 if ((s->version <= SSL3_VERSION) && !ctx->extensions)
Adam Langley95c29f32014-06-20 12:00:00 -07002273 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002274 if (!SSL_early_callback_ctx_extension_get(
2275 ctx, TLSEXT_TYPE_session_ticket, &data, &len))
Adam Langley95c29f32014-06-20 12:00:00 -07002276 {
Adam Langleydc9b1412014-06-20 12:00:00 -07002277 return 0;
2278 }
2279 if (len == 0)
2280 {
2281 /* The client will accept a ticket but doesn't
2282 * currently have one. */
2283 s->tlsext_ticket_expected = 1;
2284 return 1;
2285 }
Adam Langleydc9b1412014-06-20 12:00:00 -07002286 r = tls_decrypt_ticket(s, data, len, ctx->session_id,
2287 ctx->session_id_len, ret);
2288 switch (r)
2289 {
2290 case 2: /* ticket couldn't be decrypted */
2291 s->tlsext_ticket_expected = 1;
2292 return 2;
2293 case 3: /* ticket was decrypted */
2294 return r;
2295 case 4: /* ticket decrypted but need to renew */
2296 s->tlsext_ticket_expected = 1;
2297 return 3;
2298 default: /* fatal error */
Adam Langley95c29f32014-06-20 12:00:00 -07002299 return -1;
2300 }
Adam Langley95c29f32014-06-20 12:00:00 -07002301 }
2302
2303/* tls_decrypt_ticket attempts to decrypt a session ticket.
2304 *
2305 * etick: points to the body of the session ticket extension.
2306 * eticklen: the length of the session tickets extenion.
2307 * sess_id: points at the session ID.
2308 * sesslen: the length of the session ID.
2309 * psess: (output) on return, if a ticket was decrypted, then this is set to
2310 * point to the resulting session.
2311 *
2312 * Returns:
2313 * -1: fatal error, either from parsing or decrypting the ticket.
2314 * 2: the ticket couldn't be decrypted.
2315 * 3: a ticket was successfully decrypted and *psess was set.
2316 * 4: same as 3, but the ticket needs to be renewed.
2317 */
2318static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
2319 const unsigned char *sess_id, int sesslen,
2320 SSL_SESSION **psess)
2321 {
2322 SSL_SESSION *sess;
2323 unsigned char *sdec;
2324 const unsigned char *p;
2325 int slen, mlen, renew_ticket = 0;
2326 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2327 HMAC_CTX hctx;
2328 EVP_CIPHER_CTX ctx;
2329 SSL_CTX *tctx = s->initial_ctx;
2330 /* Need at least keyname + iv + some encrypted data */
2331 if (eticklen < 48)
2332 return 2;
2333 /* Initialize session ticket encryption and HMAC contexts */
2334 HMAC_CTX_init(&hctx);
2335 EVP_CIPHER_CTX_init(&ctx);
2336 if (tctx->tlsext_ticket_key_cb)
2337 {
2338 unsigned char *nctick = (unsigned char *)etick;
2339 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
2340 &ctx, &hctx, 0);
2341 if (rv < 0)
2342 return -1;
2343 if (rv == 0)
2344 return 2;
2345 if (rv == 2)
2346 renew_ticket = 1;
2347 }
2348 else
2349 {
2350 /* Check key name matches */
2351 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
2352 return 2;
David Benjaminae3e4872014-11-18 21:52:26 -05002353 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2354 tlsext_tick_md(), NULL) ||
2355 !EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2356 tctx->tlsext_tick_aes_key, etick + 16))
2357 {
2358 HMAC_CTX_cleanup(&hctx);
2359 EVP_CIPHER_CTX_cleanup(&ctx);
2360 return -1;
2361 }
Adam Langley95c29f32014-06-20 12:00:00 -07002362 }
2363 /* Attempt to process session ticket, first conduct sanity and
2364 * integrity checks on ticket.
2365 */
2366 mlen = HMAC_size(&hctx);
2367 if (mlen < 0)
2368 {
David Benjaminae3e4872014-11-18 21:52:26 -05002369 HMAC_CTX_cleanup(&hctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002370 EVP_CIPHER_CTX_cleanup(&ctx);
2371 return -1;
2372 }
2373 eticklen -= mlen;
2374 /* Check HMAC of encrypted ticket */
2375 HMAC_Update(&hctx, etick, eticklen);
2376 HMAC_Final(&hctx, tick_hmac, NULL);
2377 HMAC_CTX_cleanup(&hctx);
2378 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
Adam Langley38311732014-10-16 19:04:35 -07002379 {
2380 EVP_CIPHER_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002381 return 2;
Adam Langley38311732014-10-16 19:04:35 -07002382 }
Adam Langley95c29f32014-06-20 12:00:00 -07002383 /* Attempt to decrypt session data */
2384 /* Move p after IV to start of encrypted ticket, update length */
2385 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2386 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2387 sdec = OPENSSL_malloc(eticklen);
2388 if (!sdec)
2389 {
2390 EVP_CIPHER_CTX_cleanup(&ctx);
2391 return -1;
2392 }
2393 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2394 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0)
Adam Langley3e148852014-07-24 17:34:02 -07002395 {
2396 EVP_CIPHER_CTX_cleanup(&ctx);
2397 OPENSSL_free(sdec);
Adam Langley95c29f32014-06-20 12:00:00 -07002398 return 2;
Adam Langley3e148852014-07-24 17:34:02 -07002399 }
Adam Langley95c29f32014-06-20 12:00:00 -07002400 slen += mlen;
2401 EVP_CIPHER_CTX_cleanup(&ctx);
2402 p = sdec;
2403
2404 sess = d2i_SSL_SESSION(NULL, &p, slen);
2405 OPENSSL_free(sdec);
2406 if (sess)
2407 {
2408 /* The session ID, if non-empty, is used by some clients to
2409 * detect that the ticket has been accepted. So we copy it to
2410 * the session structure. If it is empty set length to zero
2411 * as required by standard.
2412 */
2413 if (sesslen)
2414 memcpy(sess->session_id, sess_id, sesslen);
2415 sess->session_id_length = sesslen;
2416 *psess = sess;
2417 if (renew_ticket)
2418 return 4;
2419 else
2420 return 3;
2421 }
2422 ERR_clear_error();
2423 /* For session parse failure, indicate that we need to send a new
2424 * ticket. */
2425 return 2;
2426 }
2427
2428/* Tables to translate from NIDs to TLS v1.2 ids */
2429
2430typedef struct
2431 {
2432 int nid;
2433 int id;
2434 } tls12_lookup;
2435
David Benjamincff64722014-08-19 19:54:46 -04002436static const tls12_lookup tls12_md[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002437 {NID_md5, TLSEXT_hash_md5},
2438 {NID_sha1, TLSEXT_hash_sha1},
2439 {NID_sha224, TLSEXT_hash_sha224},
2440 {NID_sha256, TLSEXT_hash_sha256},
2441 {NID_sha384, TLSEXT_hash_sha384},
2442 {NID_sha512, TLSEXT_hash_sha512}
2443};
2444
David Benjamincff64722014-08-19 19:54:46 -04002445static const tls12_lookup tls12_sig[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002446 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
Adam Langley95c29f32014-06-20 12:00:00 -07002447 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
2448};
2449
David Benjamincff64722014-08-19 19:54:46 -04002450static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002451 {
2452 size_t i;
2453 for (i = 0; i < tlen; i++)
2454 {
2455 if (table[i].nid == nid)
2456 return table[i].id;
2457 }
2458 return -1;
2459 }
2460
David Benjamincff64722014-08-19 19:54:46 -04002461static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002462 {
2463 size_t i;
2464 for (i = 0; i < tlen; i++)
2465 {
2466 if ((table[i].id) == id)
2467 return table[i].nid;
2468 }
2469 return NID_undef;
2470 }
2471
2472int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
2473 {
2474 int sig_id, md_id;
2475 if (!md)
2476 return 0;
2477 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2478 sizeof(tls12_md)/sizeof(tls12_lookup));
2479 if (md_id == -1)
2480 return 0;
2481 sig_id = tls12_get_sigid(pk);
2482 if (sig_id == -1)
2483 return 0;
2484 p[0] = (unsigned char)md_id;
2485 p[1] = (unsigned char)sig_id;
2486 return 1;
2487 }
2488
2489int tls12_get_sigid(const EVP_PKEY *pk)
2490 {
2491 return tls12_find_id(pk->type, tls12_sig,
2492 sizeof(tls12_sig)/sizeof(tls12_lookup));
2493 }
2494
2495const EVP_MD *tls12_get_hash(unsigned char hash_alg)
2496 {
2497 switch(hash_alg)
2498 {
Adam Langley95c29f32014-06-20 12:00:00 -07002499 case TLSEXT_hash_md5:
Adam Langley95c29f32014-06-20 12:00:00 -07002500 return EVP_md5();
Adam Langley95c29f32014-06-20 12:00:00 -07002501 case TLSEXT_hash_sha1:
2502 return EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002503 case TLSEXT_hash_sha224:
2504 return EVP_sha224();
2505
2506 case TLSEXT_hash_sha256:
2507 return EVP_sha256();
Adam Langley95c29f32014-06-20 12:00:00 -07002508 case TLSEXT_hash_sha384:
2509 return EVP_sha384();
2510
2511 case TLSEXT_hash_sha512:
2512 return EVP_sha512();
Adam Langley95c29f32014-06-20 12:00:00 -07002513 default:
2514 return NULL;
2515
2516 }
2517 }
2518
David Benjaminec2f27d2014-11-13 19:17:25 -05002519/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2520 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
2521static int tls12_get_pkey_type(uint8_t sig_alg)
Adam Langley95c29f32014-06-20 12:00:00 -07002522 {
2523 switch(sig_alg)
2524 {
Adam Langley95c29f32014-06-20 12:00:00 -07002525 case TLSEXT_signature_rsa:
David Benjaminec2f27d2014-11-13 19:17:25 -05002526 return EVP_PKEY_RSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002527 case TLSEXT_signature_ecdsa:
David Benjaminec2f27d2014-11-13 19:17:25 -05002528 return EVP_PKEY_EC;
Adam Langley95c29f32014-06-20 12:00:00 -07002529 }
2530 return -1;
2531 }
2532
2533/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2534static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
2535 int *psignhash_nid, const unsigned char *data)
2536 {
2537 int sign_nid = 0, hash_nid = 0;
2538 if (!phash_nid && !psign_nid && !psignhash_nid)
2539 return;
2540 if (phash_nid || psignhash_nid)
2541 {
2542 hash_nid = tls12_find_nid(data[0], tls12_md,
2543 sizeof(tls12_md)/sizeof(tls12_lookup));
2544 if (phash_nid)
2545 *phash_nid = hash_nid;
2546 }
2547 if (psign_nid || psignhash_nid)
2548 {
2549 sign_nid = tls12_find_nid(data[1], tls12_sig,
2550 sizeof(tls12_sig)/sizeof(tls12_lookup));
2551 if (psign_nid)
2552 *psign_nid = sign_nid;
2553 }
2554 if (psignhash_nid)
2555 {
2556 if (sign_nid && hash_nid)
2557 OBJ_find_sigid_by_algs(psignhash_nid,
2558 hash_nid, sign_nid);
2559 else
2560 *psignhash_nid = NID_undef;
2561 }
2562 }
2563/* Given preference and allowed sigalgs set shared sigalgs */
2564static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
2565 const unsigned char *pref, size_t preflen,
2566 const unsigned char *allow, size_t allowlen)
2567 {
2568 const unsigned char *ptmp, *atmp;
2569 size_t i, j, nmatch = 0;
2570 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
2571 {
2572 /* Skip disabled hashes or signature algorithms */
2573 if (tls12_get_hash(ptmp[0]) == NULL)
2574 continue;
David Benjaminec2f27d2014-11-13 19:17:25 -05002575 if (tls12_get_pkey_type(ptmp[1]) == -1)
Adam Langley95c29f32014-06-20 12:00:00 -07002576 continue;
2577 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
2578 {
2579 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
2580 {
2581 nmatch++;
2582 if (shsig)
2583 {
2584 shsig->rhash = ptmp[0];
2585 shsig->rsign = ptmp[1];
2586 tls1_lookup_sigalg(&shsig->hash_nid,
2587 &shsig->sign_nid,
2588 &shsig->signandhash_nid,
2589 ptmp);
2590 shsig++;
2591 }
2592 break;
2593 }
2594 }
2595 }
2596 return nmatch;
2597 }
2598
2599/* Set shared signature algorithms for SSL structures */
2600static int tls1_set_shared_sigalgs(SSL *s)
2601 {
2602 const unsigned char *pref, *allow, *conf;
2603 size_t preflen, allowlen, conflen;
2604 size_t nmatch;
2605 TLS_SIGALGS *salgs = NULL;
2606 CERT *c = s->cert;
Adam Langleydb4f9522014-06-20 12:00:00 -07002607 if (c->shared_sigalgs)
2608 {
2609 OPENSSL_free(c->shared_sigalgs);
2610 c->shared_sigalgs = NULL;
2611 }
Adam Langley95c29f32014-06-20 12:00:00 -07002612 /* If client use client signature algorithms if not NULL */
David Benjamin335d10d2014-08-06 19:56:33 -04002613 if (!s->server && c->client_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002614 {
2615 conf = c->client_sigalgs;
2616 conflen = c->client_sigalgslen;
2617 }
David Benjamin335d10d2014-08-06 19:56:33 -04002618 else if (c->conf_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002619 {
2620 conf = c->conf_sigalgs;
2621 conflen = c->conf_sigalgslen;
2622 }
2623 else
2624 conflen = tls12_get_psigalgs(s, &conf);
David Benjamin335d10d2014-08-06 19:56:33 -04002625 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
Adam Langley95c29f32014-06-20 12:00:00 -07002626 {
2627 pref = conf;
2628 preflen = conflen;
2629 allow = c->peer_sigalgs;
2630 allowlen = c->peer_sigalgslen;
2631 }
2632 else
2633 {
2634 allow = conf;
2635 allowlen = conflen;
2636 pref = c->peer_sigalgs;
2637 preflen = c->peer_sigalgslen;
2638 }
2639 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2640 if (!nmatch)
2641 return 1;
2642 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2643 if (!salgs)
2644 return 0;
2645 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2646 c->shared_sigalgs = salgs;
2647 c->shared_sigalgslen = nmatch;
2648 return 1;
2649 }
2650
2651
2652/* Set preferred digest for each key type */
2653
David Benjamincd996942014-07-20 16:23:51 -04002654int tls1_process_sigalgs(SSL *s, const CBS *sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002655 {
Adam Langley95c29f32014-06-20 12:00:00 -07002656 CERT *c = s->cert;
David Benjamincd996942014-07-20 16:23:51 -04002657
Adam Langley95c29f32014-06-20 12:00:00 -07002658 /* Extension ignored for inappropriate versions */
2659 if (!SSL_USE_SIGALGS(s))
2660 return 1;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002661 /* Length must be even */
David Benjamincd996942014-07-20 16:23:51 -04002662 if (CBS_len(sigalgs) % 2 != 0)
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002663 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002664 /* Should never happen */
2665 if (!c)
2666 return 0;
2667
David Benjamincd996942014-07-20 16:23:51 -04002668 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen))
Adam Langley95c29f32014-06-20 12:00:00 -07002669 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002670
2671 tls1_set_shared_sigalgs(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002672 return 1;
2673 }
2674
David Benjaminec2f27d2014-11-13 19:17:25 -05002675const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey)
2676 {
2677 CERT *c = s->cert;
2678 int type = EVP_PKEY_id(pkey);
2679 size_t i;
2680
2681 /* Select the first shared digest supported by our key. */
2682 for (i = 0; i < c->shared_sigalgslen; i++)
2683 {
2684 const EVP_MD *md = tls12_get_hash(c->shared_sigalgs[i].rhash);
2685 if (md == NULL || tls12_get_pkey_type(c->shared_sigalgs[i].rsign) != type)
2686 continue;
2687 if (!EVP_PKEY_supports_digest(pkey, md))
2688 continue;
2689 return md;
2690 }
2691
2692 /* If no suitable digest may be found, default to SHA-1. */
2693 return EVP_sha1();
2694 }
Adam Langley95c29f32014-06-20 12:00:00 -07002695
2696int SSL_get_sigalgs(SSL *s, int idx,
2697 int *psign, int *phash, int *psignhash,
2698 unsigned char *rsig, unsigned char *rhash)
2699 {
2700 const unsigned char *psig = s->cert->peer_sigalgs;
2701 if (psig == NULL)
2702 return 0;
2703 if (idx >= 0)
2704 {
2705 idx <<= 1;
2706 if (idx >= (int)s->cert->peer_sigalgslen)
2707 return 0;
2708 psig += idx;
2709 if (rhash)
2710 *rhash = psig[0];
2711 if (rsig)
2712 *rsig = psig[1];
2713 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2714 }
2715 return s->cert->peer_sigalgslen / 2;
2716 }
2717
2718int SSL_get_shared_sigalgs(SSL *s, int idx,
2719 int *psign, int *phash, int *psignhash,
2720 unsigned char *rsig, unsigned char *rhash)
2721 {
2722 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
2723 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
2724 return 0;
2725 shsigalgs += idx;
2726 if (phash)
2727 *phash = shsigalgs->hash_nid;
2728 if (psign)
2729 *psign = shsigalgs->sign_nid;
2730 if (psignhash)
2731 *psignhash = shsigalgs->signandhash_nid;
2732 if (rsig)
2733 *rsig = shsigalgs->rsign;
2734 if (rhash)
2735 *rhash = shsigalgs->rhash;
2736 return s->cert->shared_sigalgslen;
2737 }
2738
Adam Langley1258b6a2014-06-20 12:00:00 -07002739/* tls1_channel_id_hash calculates the signed data for a Channel ID on the given
2740 * SSL connection and writes it to |md|. */
2741int
2742tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s)
2743 {
2744 EVP_MD_CTX ctx;
2745 unsigned char temp_digest[EVP_MAX_MD_SIZE];
2746 unsigned temp_digest_len;
2747 int i;
2748 static const char kClientIDMagic[] = "TLS Channel ID signature";
2749
2750 if (s->s3->handshake_buffer)
Adam Langley75712922014-10-10 16:23:43 -07002751 if (!ssl3_digest_cached_records(s, free_handshake_buffer))
Adam Langley1258b6a2014-06-20 12:00:00 -07002752 return 0;
2753
2754 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2755
2756 if (s->hit && s->s3->tlsext_channel_id_new)
2757 {
2758 static const char kResumptionMagic[] = "Resumption";
2759 EVP_DigestUpdate(md, kResumptionMagic,
2760 sizeof(kResumptionMagic));
2761 if (s->session->original_handshake_hash_len == 0)
2762 return 0;
2763 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2764 s->session->original_handshake_hash_len);
2765 }
2766
2767 EVP_MD_CTX_init(&ctx);
2768 for (i = 0; i < SSL_MAX_DIGEST; i++)
2769 {
2770 if (s->s3->handshake_dgst[i] == NULL)
2771 continue;
2772 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
2773 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2774 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2775 }
2776 EVP_MD_CTX_cleanup(&ctx);
2777
2778 return 1;
2779 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002780
2781/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2782 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
2783int tls1_record_handshake_hashes_for_channel_id(SSL *s)
2784 {
2785 int digest_len;
2786 /* This function should never be called for a resumed session because
2787 * the handshake hashes that we wish to record are for the original,
2788 * full handshake. */
2789 if (s->hit)
2790 return -1;
2791 /* It only makes sense to call this function if Channel IDs have been
2792 * negotiated. */
2793 if (!s->s3->tlsext_channel_id_new)
2794 return -1;
2795
2796 digest_len = tls1_handshake_digest(
2797 s, s->session->original_handshake_hash,
2798 sizeof(s->session->original_handshake_hash));
2799 if (digest_len < 0)
2800 return -1;
2801
2802 s->session->original_handshake_hash_len = digest_len;
2803
2804 return 1;
2805 }
2806
Adam Langley95c29f32014-06-20 12:00:00 -07002807int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
2808 {
2809 unsigned char *sigalgs, *sptr;
2810 int rhash, rsign;
2811 size_t i;
2812 if (salglen & 1)
2813 return 0;
2814 sigalgs = OPENSSL_malloc(salglen);
2815 if (sigalgs == NULL)
2816 return 0;
2817 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
2818 {
2819 rhash = tls12_find_id(*psig_nids++, tls12_md,
2820 sizeof(tls12_md)/sizeof(tls12_lookup));
2821 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2822 sizeof(tls12_sig)/sizeof(tls12_lookup));
2823
2824 if (rhash == -1 || rsign == -1)
2825 goto err;
2826 *sptr++ = rhash;
2827 *sptr++ = rsign;
2828 }
2829
2830 if (client)
2831 {
2832 if (c->client_sigalgs)
2833 OPENSSL_free(c->client_sigalgs);
2834 c->client_sigalgs = sigalgs;
2835 c->client_sigalgslen = salglen;
2836 }
2837 else
2838 {
2839 if (c->conf_sigalgs)
2840 OPENSSL_free(c->conf_sigalgs);
2841 c->conf_sigalgs = sigalgs;
2842 c->conf_sigalgslen = salglen;
2843 }
2844
2845 return 1;
2846
2847 err:
2848 OPENSSL_free(sigalgs);
2849 return 0;
2850 }
2851