blob: c8a4feafeba4907780f301d3b891f288210a2bff [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
David Benjamin338fcaf2014-12-11 01:20:52 -0500127const SSL3_ENC_METHOD TLSv1_enc_data = {
Adam Langley95c29f32014-06-20 12:00:00 -0700128 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
David Benjamin338fcaf2014-12-11 01:20:52 -0500147const SSL3_ENC_METHOD TLSv1_1_enc_data = {
Adam Langley95c29f32014-06-20 12:00:00 -0700148 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
David Benjamin338fcaf2014-12-11 01:20:52 -0500167const SSL3_ENC_METHOD TLSv1_2_enc_data = {
Adam Langley95c29f32014-06-20 12:00:00 -0700168 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 Benjamine518f652014-10-13 16:12:45 -0400372/* ECC curves from RFC4492 */
David Benjamincff64722014-08-19 19:54:46 -0400373static const int nid_list[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700374 {
375 NID_sect163k1, /* sect163k1 (1) */
376 NID_sect163r1, /* sect163r1 (2) */
377 NID_sect163r2, /* sect163r2 (3) */
378 NID_sect193r1, /* sect193r1 (4) */
379 NID_sect193r2, /* sect193r2 (5) */
380 NID_sect233k1, /* sect233k1 (6) */
381 NID_sect233r1, /* sect233r1 (7) */
382 NID_sect239k1, /* sect239k1 (8) */
383 NID_sect283k1, /* sect283k1 (9) */
384 NID_sect283r1, /* sect283r1 (10) */
385 NID_sect409k1, /* sect409k1 (11) */
386 NID_sect409r1, /* sect409r1 (12) */
387 NID_sect571k1, /* sect571k1 (13) */
388 NID_sect571r1, /* sect571r1 (14) */
389 NID_secp160k1, /* secp160k1 (15) */
390 NID_secp160r1, /* secp160r1 (16) */
391 NID_secp160r2, /* secp160r2 (17) */
392 NID_secp192k1, /* secp192k1 (18) */
393 NID_X9_62_prime192v1, /* secp192r1 (19) */
394 NID_secp224k1, /* secp224k1 (20) */
395 NID_secp224r1, /* secp224r1 (21) */
396 NID_secp256k1, /* secp256k1 (22) */
397 NID_X9_62_prime256v1, /* secp256r1 (23) */
398 NID_secp384r1, /* secp384r1 (24) */
399 NID_secp521r1, /* secp521r1 (25) */
400 NID_brainpoolP256r1, /* brainpoolP256r1 (26) */
401 NID_brainpoolP384r1, /* brainpoolP384r1 (27) */
402 NID_brainpoolP512r1 /* brainpool512r1 (28) */
403 };
404
David Benjamin072334d2014-07-13 16:24:27 -0400405static const uint8_t ecformats_default[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700406 {
407 TLSEXT_ECPOINTFORMAT_uncompressed,
Adam Langley95c29f32014-06-20 12:00:00 -0700408 };
409
David Benjamin072334d2014-07-13 16:24:27 -0400410static const uint16_t eccurves_default[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700411 {
David Benjamin072334d2014-07-13 16:24:27 -0400412 23, /* secp256r1 (23) */
413 24, /* secp384r1 (24) */
414 25, /* secp521r1 (25) */
Adam Langley95c29f32014-06-20 12:00:00 -0700415 };
416
David Benjamin072334d2014-07-13 16:24:27 -0400417int tls1_ec_curve_id2nid(uint16_t curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700418 {
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
David Benjamin072334d2014-07-13 16:24:27 -04001134 s2n(curves_len * 2, ret);
1135 for (i = 0; i < curves_len; i++)
1136 {
1137 s2n(curves[i], ret);
1138 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001139 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001140
Adam Langleyb0c235e2014-06-20 12:00:00 -07001141 if (header_len > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001142 {
Feng Lu41aa3252014-11-21 22:47:56 -08001143 size_t clienthello_minsize = 0;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001144 header_len += ret - orig;
1145 if (header_len > 0xff && header_len < 0x200)
1146 {
Feng Lu41aa3252014-11-21 22:47:56 -08001147 /* Add padding to workaround bugs in F5 terminators.
1148 * See https://tools.ietf.org/html/draft-agl-tls-padding-03
1149 *
1150 * NB: because this code works out the length of all existing
1151 * extensions it MUST always appear last. */
1152 clienthello_minsize = 0x200;
1153 }
1154 if (s->fastradio_padding)
1155 {
1156 /* Pad the ClientHello record to 1024 bytes to fast forward
1157 * the radio into DCH (high data rate) state in 3G networks.
1158 * Note that when fastradio_padding is enabled, even if the
1159 * header_len is less than 255 bytes, the padding will be
1160 * applied regardless. This is slightly different from the TLS
1161 * padding extension suggested in
1162 * https://tools.ietf.org/html/draft-agl-tls-padding-03 */
1163 clienthello_minsize = 0x400;
1164 }
1165 if (header_len < clienthello_minsize)
1166 {
1167 size_t padding_len = clienthello_minsize - header_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001168 /* Extensions take at least four bytes to encode. Always
1169 * include least one byte of data if including the
1170 * extension. WebSphere Application Server 7.0 is
1171 * intolerant to the last extension being zero-length. */
1172 if (padding_len >= 4 + 1)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001173 padding_len -= 4;
1174 else
Adam Langleyc3174b72014-06-20 12:00:00 -07001175 padding_len = 1;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001176 if (limit - ret - 4 - (long)padding_len < 0)
1177 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001178
Adam Langleyb0c235e2014-06-20 12:00:00 -07001179 s2n(TLSEXT_TYPE_padding, ret);
1180 s2n(padding_len, ret);
1181 memset(ret, 0, padding_len);
1182 ret += padding_len;
1183 }
Adam Langley95c29f32014-06-20 12:00:00 -07001184 }
Adam Langley95c29f32014-06-20 12:00:00 -07001185
Adam Langleyb0c235e2014-06-20 12:00:00 -07001186 if ((extdatalen = ret-orig-2)== 0)
1187 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001188
Adam Langleyb0c235e2014-06-20 12:00:00 -07001189 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001190 return ret;
1191 }
1192
Adam Langleyb0c235e2014-06-20 12:00:00 -07001193unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit)
Adam Langley95c29f32014-06-20 12:00:00 -07001194 {
1195 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001196 unsigned char *orig = buf;
1197 unsigned char *ret = buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001198 int next_proto_neg_seen;
Adam Langley95c29f32014-06-20 12:00:00 -07001199 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1200 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -04001201 int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
David Benjamina19fc252014-10-19 00:14:36 -04001202 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001203 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1204 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001205 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001206
1207 ret+=2;
1208 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1209
Adam Langleyed8270a2014-09-02 13:52:56 -07001210 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001211 {
1212 if ((long)(limit - ret - 4) < 0) return NULL;
1213
1214 s2n(TLSEXT_TYPE_server_name,ret);
1215 s2n(0,ret);
1216 }
1217
1218 if(s->s3->send_connection_binding)
1219 {
1220 int el;
1221
1222 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1223 {
1224 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1225 return NULL;
1226 }
1227
Adam Langleyb0c235e2014-06-20 12:00:00 -07001228 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001229
1230 s2n(TLSEXT_TYPE_renegotiate,ret);
1231 s2n(el,ret);
1232
1233 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1234 {
1235 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1236 return NULL;
1237 }
1238
1239 ret += el;
1240 }
1241
Adam Langley75712922014-10-10 16:23:43 -07001242 if (s->s3->tmp.extended_master_secret)
1243 {
1244 if ((long)(limit - ret - 4) < 0) return NULL;
1245
1246 s2n(TLSEXT_TYPE_extended_master_secret,ret);
1247 s2n(0,ret);
1248 }
1249
Adam Langley95c29f32014-06-20 12:00:00 -07001250 if (using_ecc)
1251 {
1252 const unsigned char *plist;
1253 size_t plistlen;
1254 /* Add TLS extension ECPointFormats to the ServerHello message */
1255 long lenmax;
1256
1257 tls1_get_formatlist(s, &plist, &plistlen);
1258
1259 if ((lenmax = limit - ret - 5) < 0) return NULL;
1260 if (plistlen > (size_t)lenmax) return NULL;
1261 if (plistlen > 255)
1262 {
1263 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1264 return NULL;
1265 }
1266
1267 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1268 s2n(plistlen + 1,ret);
1269 *(ret++) = (unsigned char) plistlen;
1270 memcpy(ret, plist, plistlen);
1271 ret+=plistlen;
1272
1273 }
1274 /* Currently the server should not respond with a SupportedCurves extension */
Adam Langley95c29f32014-06-20 12:00:00 -07001275
1276 if (s->tlsext_ticket_expected
1277 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1278 {
1279 if ((long)(limit - ret - 4) < 0) return NULL;
1280 s2n(TLSEXT_TYPE_session_ticket,ret);
1281 s2n(0,ret);
1282 }
1283
David Benjamin6c7aed02014-08-27 16:42:38 -04001284 if (s->s3->tmp.certificate_status_expected)
Adam Langley95c29f32014-06-20 12:00:00 -07001285 {
1286 if ((long)(limit - ret - 4) < 0) return NULL;
1287 s2n(TLSEXT_TYPE_status_request,ret);
1288 s2n(0,ret);
1289 }
1290
Adam Langley95c29f32014-06-20 12:00:00 -07001291 if(s->srtp_profile)
1292 {
1293 int el;
1294
1295 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1296
Adam Langleyb0c235e2014-06-20 12:00:00 -07001297 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001298
1299 s2n(TLSEXT_TYPE_use_srtp,ret);
1300 s2n(el,ret);
1301
David Benjamin120a6742014-08-30 14:54:37 -04001302 if(!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001303 {
1304 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1305 return NULL;
1306 }
1307 ret+=el;
1308 }
1309
Adam Langley95c29f32014-06-20 12:00:00 -07001310 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1311 s->s3->next_proto_neg_seen = 0;
1312 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1313 {
1314 const unsigned char *npa;
1315 unsigned int npalen;
1316 int r;
1317
1318 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1319 if (r == SSL_TLSEXT_ERR_OK)
1320 {
1321 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1322 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1323 s2n(npalen,ret);
1324 memcpy(ret, npa, npalen);
1325 ret += npalen;
1326 s->s3->next_proto_neg_seen = 1;
1327 }
1328 }
Adam Langley95c29f32014-06-20 12:00:00 -07001329
Adam Langley95c29f32014-06-20 12:00:00 -07001330 if (s->s3->alpn_selected)
1331 {
David Benjamin03973092014-06-24 23:27:17 -04001332 const uint8_t *selected = s->s3->alpn_selected;
1333 size_t len = s->s3->alpn_selected_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001334
1335 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1336 return NULL;
1337 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1338 s2n(3 + len,ret);
1339 s2n(1 + len,ret);
1340 *ret++ = len;
1341 memcpy(ret, selected, len);
1342 ret += len;
1343 }
1344
Adam Langley1258b6a2014-06-20 12:00:00 -07001345 /* If the client advertised support for Channel ID, and we have it
1346 * enabled, then we want to echo it back. */
1347 if (s->s3->tlsext_channel_id_valid)
1348 {
1349 if (limit - ret - 4 < 0)
1350 return NULL;
1351 if (s->s3->tlsext_channel_id_new)
1352 s2n(TLSEXT_TYPE_channel_id_new,ret);
1353 else
1354 s2n(TLSEXT_TYPE_channel_id,ret);
1355 s2n(0,ret);
1356 }
1357
Adam Langleyb0c235e2014-06-20 12:00:00 -07001358 if ((extdatalen = ret-orig-2) == 0)
1359 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001360
Adam Langleyb0c235e2014-06-20 12:00:00 -07001361 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001362 return ret;
1363 }
1364
Adam Langley95c29f32014-06-20 12:00:00 -07001365/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1366 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001367 * cbs: the contents of the extension, not including the type and length.
1368 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001369 * return.
1370 *
David Benjamindc72ff72014-06-25 12:36:10 -04001371 * returns: 1 on success. */
1372static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001373 {
Adam Langleyded93582014-07-31 15:23:51 -07001374 CBS protocol_name_list, protocol_name_list_copy;
Adam Langley95c29f32014-06-20 12:00:00 -07001375 const unsigned char *selected;
1376 unsigned char selected_len;
1377 int r;
1378
1379 if (s->ctx->alpn_select_cb == NULL)
David Benjamindc72ff72014-06-25 12:36:10 -04001380 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001381
David Benjamindc72ff72014-06-25 12:36:10 -04001382 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1383 CBS_len(cbs) != 0 ||
1384 CBS_len(&protocol_name_list) < 2)
Adam Langley95c29f32014-06-20 12:00:00 -07001385 goto parse_error;
1386
David Benjamindc72ff72014-06-25 12:36:10 -04001387 /* Validate the protocol list. */
Adam Langleyded93582014-07-31 15:23:51 -07001388 protocol_name_list_copy = protocol_name_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001389 while (CBS_len(&protocol_name_list_copy) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001390 {
David Benjamindc72ff72014-06-25 12:36:10 -04001391 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001392
David Benjamindc72ff72014-06-25 12:36:10 -04001393 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001394 goto parse_error;
Adam Langley95c29f32014-06-20 12:00:00 -07001395 }
1396
David Benjamindc72ff72014-06-25 12:36:10 -04001397 r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1398 CBS_data(&protocol_name_list), CBS_len(&protocol_name_list),
1399 s->ctx->alpn_select_cb_arg);
Adam Langley95c29f32014-06-20 12:00:00 -07001400 if (r == SSL_TLSEXT_ERR_OK) {
1401 if (s->s3->alpn_selected)
1402 OPENSSL_free(s->s3->alpn_selected);
David Benjamin072c9532014-07-26 11:44:25 -04001403 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001404 if (!s->s3->alpn_selected)
1405 {
David Benjamindc72ff72014-06-25 12:36:10 -04001406 *out_alert = SSL_AD_INTERNAL_ERROR;
1407 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001408 }
Adam Langley95c29f32014-06-20 12:00:00 -07001409 s->s3->alpn_selected_len = selected_len;
1410 }
David Benjamindc72ff72014-06-25 12:36:10 -04001411 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001412
1413parse_error:
David Benjamindc72ff72014-06-25 12:36:10 -04001414 *out_alert = SSL_AD_DECODE_ERROR;
1415 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001416 }
1417
David Benjamindc72ff72014-06-25 12:36:10 -04001418static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001419 {
Adam Langley95c29f32014-06-20 12:00:00 -07001420 int renegotiate_seen = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001421 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001422
Adam Langleyed8270a2014-09-02 13:52:56 -07001423 s->should_ack_sni = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001424 s->s3->next_proto_neg_seen = 0;
David Benjamin6c7aed02014-08-27 16:42:38 -04001425 s->s3->tmp.certificate_status_expected = 0;
Adam Langley75712922014-10-10 16:23:43 -07001426 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001427
Adam Langley95c29f32014-06-20 12:00:00 -07001428 if (s->s3->alpn_selected)
1429 {
1430 OPENSSL_free(s->s3->alpn_selected);
1431 s->s3->alpn_selected = NULL;
1432 }
1433
Adam Langley95c29f32014-06-20 12:00:00 -07001434 /* Clear any signature algorithms extension received */
1435 if (s->cert->peer_sigalgs)
1436 {
1437 OPENSSL_free(s->cert->peer_sigalgs);
1438 s->cert->peer_sigalgs = NULL;
1439 }
David Benjamina19fc252014-10-19 00:14:36 -04001440 /* Clear any shared signature algorithms */
Adam Langley95c29f32014-06-20 12:00:00 -07001441 if (s->cert->shared_sigalgs)
1442 {
1443 OPENSSL_free(s->cert->shared_sigalgs);
1444 s->cert->shared_sigalgs = NULL;
1445 }
David Benjamina19fc252014-10-19 00:14:36 -04001446 /* Clear ECC extensions */
1447 if (s->s3->tmp.peer_ecpointformatlist != 0)
1448 {
1449 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1450 s->s3->tmp.peer_ecpointformatlist = NULL;
1451 s->s3->tmp.peer_ecpointformatlist_length = 0;
1452 }
1453 if (s->s3->tmp.peer_ellipticcurvelist != 0)
1454 {
1455 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1456 s->s3->tmp.peer_ellipticcurvelist = NULL;
1457 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1458 }
Adam Langley95c29f32014-06-20 12:00:00 -07001459
David Benjamindc72ff72014-06-25 12:36:10 -04001460 /* There may be no extensions. */
1461 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001462 {
David Benjamindc72ff72014-06-25 12:36:10 -04001463 goto ri_check;
1464 }
Adam Langley95c29f32014-06-20 12:00:00 -07001465
David Benjamin35a7a442014-07-05 00:23:20 -04001466 /* Decode the extensions block and check it is valid. */
1467 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1468 !tls1_check_duplicate_extensions(&extensions))
David Benjamindc72ff72014-06-25 12:36:10 -04001469 {
1470 *out_alert = SSL_AD_DECODE_ERROR;
1471 return 0;
1472 }
1473
David Benjamindc72ff72014-06-25 12:36:10 -04001474 while (CBS_len(&extensions) != 0)
1475 {
1476 uint16_t type;
1477 CBS extension;
1478
1479 /* Decode the next extension. */
1480 if (!CBS_get_u16(&extensions, &type) ||
1481 !CBS_get_u16_length_prefixed(&extensions, &extension))
1482 {
1483 *out_alert = SSL_AD_DECODE_ERROR;
1484 return 0;
1485 }
1486
Adam Langley95c29f32014-06-20 12:00:00 -07001487 if (s->tlsext_debug_cb)
David Benjamindc72ff72014-06-25 12:36:10 -04001488 {
1489 s->tlsext_debug_cb(s, 0, type, (unsigned char*)CBS_data(&extension),
1490 CBS_len(&extension), s->tlsext_debug_arg);
1491 }
1492
Adam Langley95c29f32014-06-20 12:00:00 -07001493/* The servername extension is treated as follows:
1494
1495 - Only the hostname type is supported with a maximum length of 255.
1496 - The servername is rejected if too long or if it contains zeros,
1497 in which case an fatal alert is generated.
1498 - The servername field is maintained together with the session cache.
1499 - When a session is resumed, the servername call back invoked in order
1500 to allow the application to position itself to the right context.
1501 - The servername is acknowledged if it is new for a session or when
1502 it is identical to a previously used for the same session.
1503 Applications can control the behaviour. They can at any time
1504 set a 'desirable' servername for a new SSL object. This can be the
1505 case for example with HTTPS when a Host: header field is received and
1506 a renegotiation is requested. In this case, a possible servername
1507 presented in the new client hello is only acknowledged if it matches
1508 the value of the Host: field.
1509 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1510 if they provide for changing an explicit servername context for the session,
1511 i.e. when the session has been established with a servername extension.
1512 - On session reconnect, the servername extension may be absent.
1513
1514*/
1515
1516 if (type == TLSEXT_TYPE_server_name)
1517 {
David Benjamindc72ff72014-06-25 12:36:10 -04001518 CBS server_name_list;
Adam Langleyed8270a2014-09-02 13:52:56 -07001519 char have_seen_host_name = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001520
1521 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1522 CBS_len(&server_name_list) < 1 ||
1523 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001524 {
David Benjamindc72ff72014-06-25 12:36:10 -04001525 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001526 return 0;
1527 }
Adam Langley95c29f32014-06-20 12:00:00 -07001528
David Benjamindc72ff72014-06-25 12:36:10 -04001529 /* Decode each ServerName in the extension. */
1530 while (CBS_len(&server_name_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001531 {
David Benjamindc72ff72014-06-25 12:36:10 -04001532 uint8_t name_type;
1533 CBS host_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001534
David Benjamindc72ff72014-06-25 12:36:10 -04001535 /* Decode the NameType. */
1536 if (!CBS_get_u8(&server_name_list, &name_type))
Adam Langley95c29f32014-06-20 12:00:00 -07001537 {
David Benjamindc72ff72014-06-25 12:36:10 -04001538 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001539 return 0;
1540 }
David Benjamindc72ff72014-06-25 12:36:10 -04001541
David Benjamindc72ff72014-06-25 12:36:10 -04001542 /* Only host_name is supported. */
1543 if (name_type != TLSEXT_NAMETYPE_host_name)
1544 continue;
1545
Adam Langleyed8270a2014-09-02 13:52:56 -07001546 if (have_seen_host_name)
1547 {
1548 /* The ServerNameList MUST NOT contain
1549 * more than one name of the same
1550 * name_type. */
1551 *out_alert = SSL_AD_DECODE_ERROR;
1552 return 0;
1553 }
1554
1555 have_seen_host_name = 1;
1556
1557 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1558 CBS_len(&host_name) < 1)
1559 {
1560 *out_alert = SSL_AD_DECODE_ERROR;
1561 return 0;
1562 }
1563
1564 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1565 CBS_contains_zero_byte(&host_name))
1566 {
1567 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1568 return 0;
1569 }
1570
David Benjamindc72ff72014-06-25 12:36:10 -04001571 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -07001572 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001573 assert(s->session->tlsext_hostname == NULL);
David Benjamindc72ff72014-06-25 12:36:10 -04001574 if (s->session->tlsext_hostname)
Adam Langley95c29f32014-06-20 12:00:00 -07001575 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001576 /* This should be impossible. */
David Benjamindc72ff72014-06-25 12:36:10 -04001577 *out_alert = SSL_AD_DECODE_ERROR;
1578 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001579 }
Adam Langley95c29f32014-06-20 12:00:00 -07001580
David Benjamindc72ff72014-06-25 12:36:10 -04001581 /* Copy the hostname as a string. */
David Benjamined439582014-07-14 19:13:02 -04001582 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname))
David Benjamindc72ff72014-06-25 12:36:10 -04001583 {
1584 *out_alert = SSL_AD_INTERNAL_ERROR;
1585 return 0;
1586 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001587
1588 s->should_ack_sni = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001589 }
Adam Langley95c29f32014-06-20 12:00:00 -07001590 }
Adam Langley95c29f32014-06-20 12:00:00 -07001591 }
1592
Adam Langley95c29f32014-06-20 12:00:00 -07001593 else if (type == TLSEXT_TYPE_ec_point_formats)
1594 {
David Benjamindc72ff72014-06-25 12:36:10 -04001595 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001596
David Benjamindc72ff72014-06-25 12:36:10 -04001597 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1598 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001599 {
David Benjamindc72ff72014-06-25 12:36:10 -04001600 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001601 return 0;
1602 }
David Benjamindc72ff72014-06-25 12:36:10 -04001603
David Benjamina19fc252014-10-19 00:14:36 -04001604 if (!CBS_stow(&ec_point_format_list,
1605 &s->s3->tmp.peer_ecpointformatlist,
1606 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001607 {
David Benjamina19fc252014-10-19 00:14:36 -04001608 *out_alert = SSL_AD_INTERNAL_ERROR;
1609 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001610 }
Adam Langley95c29f32014-06-20 12:00:00 -07001611 }
1612 else if (type == TLSEXT_TYPE_elliptic_curves)
1613 {
David Benjamindc72ff72014-06-25 12:36:10 -04001614 CBS elliptic_curve_list;
David Benjamin072334d2014-07-13 16:24:27 -04001615 size_t i, num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001616
David Benjamindc72ff72014-06-25 12:36:10 -04001617 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
David Benjamin072334d2014-07-13 16:24:27 -04001618 CBS_len(&elliptic_curve_list) == 0 ||
1619 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
David Benjamindc72ff72014-06-25 12:36:10 -04001620 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001621 {
David Benjamindc72ff72014-06-25 12:36:10 -04001622 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001623 return 0;
1624 }
David Benjamindc72ff72014-06-25 12:36:10 -04001625
David Benjamina19fc252014-10-19 00:14:36 -04001626 if (s->s3->tmp.peer_ellipticcurvelist)
Adam Langley95c29f32014-06-20 12:00:00 -07001627 {
David Benjamina19fc252014-10-19 00:14:36 -04001628 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1629 s->s3->tmp.peer_ellipticcurvelist_length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001630 }
David Benjamina19fc252014-10-19 00:14:36 -04001631 s->s3->tmp.peer_ellipticcurvelist =
1632 (uint16_t*)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
1633 if (s->s3->tmp.peer_ellipticcurvelist == NULL)
1634 {
1635 *out_alert = SSL_AD_INTERNAL_ERROR;
1636 return 0;
1637 }
1638 num_curves = CBS_len(&elliptic_curve_list) / 2;
1639 for (i = 0; i < num_curves; i++)
1640 {
1641 if (!CBS_get_u16(&elliptic_curve_list,
1642 &s->s3->tmp.peer_ellipticcurvelist[i]))
1643 {
1644 *out_alert = SSL_AD_INTERNAL_ERROR;
1645 return 0;
1646 }
1647 }
1648 if (CBS_len(&elliptic_curve_list) != 0)
1649 {
1650 *out_alert = SSL_AD_INTERNAL_ERROR;
1651 return 0;
1652 }
1653 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001654 }
Adam Langley95c29f32014-06-20 12:00:00 -07001655 else if (type == TLSEXT_TYPE_renegotiate)
1656 {
David Benjamindc72ff72014-06-25 12:36:10 -04001657 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001658 return 0;
1659 renegotiate_seen = 1;
1660 }
1661 else if (type == TLSEXT_TYPE_signature_algorithms)
1662 {
David Benjamindc72ff72014-06-25 12:36:10 -04001663 CBS supported_signature_algorithms;
1664
David Benjamindc72ff72014-06-25 12:36:10 -04001665 if (!CBS_get_u16_length_prefixed(&extension, &supported_signature_algorithms) ||
1666 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001667 {
David Benjamindc72ff72014-06-25 12:36:10 -04001668 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001669 return 0;
1670 }
David Benjamindc72ff72014-06-25 12:36:10 -04001671
1672 /* Ensure the signature algorithms are non-empty. It
1673 * contains a list of SignatureAndHashAlgorithms
1674 * which are two bytes each. */
1675 if (CBS_len(&supported_signature_algorithms) == 0 ||
1676 (CBS_len(&supported_signature_algorithms) % 2) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001677 {
David Benjamindc72ff72014-06-25 12:36:10 -04001678 *out_alert = SSL_AD_DECODE_ERROR;
1679 return 0;
1680 }
1681
David Benjamincd996942014-07-20 16:23:51 -04001682 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
David Benjamindc72ff72014-06-25 12:36:10 -04001683 {
1684 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001685 return 0;
1686 }
1687 /* If sigalgs received and no shared algorithms fatal
1688 * error.
1689 */
1690 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
1691 {
1692 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
David Benjamindc72ff72014-06-25 12:36:10 -04001693 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07001694 return 0;
1695 }
1696 }
1697
Adam Langley95c29f32014-06-20 12:00:00 -07001698 else if (type == TLSEXT_TYPE_next_proto_neg &&
1699 s->s3->tmp.finish_md_len == 0 &&
1700 s->s3->alpn_selected == NULL)
1701 {
David Benjamindc72ff72014-06-25 12:36:10 -04001702 /* The extension must be empty. */
1703 if (CBS_len(&extension) != 0)
1704 {
1705 *out_alert = SSL_AD_DECODE_ERROR;
1706 return 0;
1707 }
1708
Adam Langley95c29f32014-06-20 12:00:00 -07001709 /* We shouldn't accept this extension on a
1710 * renegotiation.
1711 *
1712 * s->new_session will be set on renegotiation, but we
1713 * probably shouldn't rely that it couldn't be set on
1714 * the initial renegotation too in certain cases (when
1715 * there's some other reason to disallow resuming an
1716 * earlier session -- the current code won't be doing
1717 * anything like that, but this might change).
1718
1719 * A valid sign that there's been a previous handshake
1720 * in this connection is if s->s3->tmp.finish_md_len >
1721 * 0. (We are talking about a check that will happen
1722 * in the Hello protocol round, well before a new
1723 * Finished message could have been computed.) */
1724 s->s3->next_proto_neg_seen = 1;
1725 }
Adam Langley95c29f32014-06-20 12:00:00 -07001726
1727 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1728 s->ctx->alpn_select_cb &&
1729 s->s3->tmp.finish_md_len == 0)
1730 {
David Benjamindc72ff72014-06-25 12:36:10 -04001731 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001732 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001733 /* ALPN takes precedence over NPN. */
1734 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001735 }
1736
Adam Langley1258b6a2014-06-20 12:00:00 -07001737 else if (type == TLSEXT_TYPE_channel_id &&
1738 s->tlsext_channel_id_enabled)
David Benjamindc72ff72014-06-25 12:36:10 -04001739 {
1740 /* The extension must be empty. */
1741 if (CBS_len(&extension) != 0)
1742 {
1743 *out_alert = SSL_AD_DECODE_ERROR;
1744 return 0;
1745 }
1746
Adam Langley1258b6a2014-06-20 12:00:00 -07001747 s->s3->tlsext_channel_id_valid = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001748 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001749
1750 else if (type == TLSEXT_TYPE_channel_id_new &&
1751 s->tlsext_channel_id_enabled)
1752 {
David Benjamindc72ff72014-06-25 12:36:10 -04001753 /* The extension must be empty. */
1754 if (CBS_len(&extension) != 0)
1755 {
1756 *out_alert = SSL_AD_DECODE_ERROR;
1757 return 0;
1758 }
1759
Adam Langley1258b6a2014-06-20 12:00:00 -07001760 s->s3->tlsext_channel_id_valid = 1;
1761 s->s3->tlsext_channel_id_new = 1;
1762 }
1763
1764
Adam Langley95c29f32014-06-20 12:00:00 -07001765 /* session ticket processed earlier */
1766 else if (type == TLSEXT_TYPE_use_srtp)
1767 {
David Benjamindc72ff72014-06-25 12:36:10 -04001768 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001769 return 0;
1770 }
Adam Langley75712922014-10-10 16:23:43 -07001771
1772 else if (type == TLSEXT_TYPE_extended_master_secret &&
1773 s->version != SSL3_VERSION)
1774 {
1775 if (CBS_len(&extension) != 0)
1776 {
1777 *out_alert = SSL_AD_DECODE_ERROR;
1778 return 0;
1779 }
1780
1781 s->s3->tmp.extended_master_secret = 1;
1782 }
Adam Langley95c29f32014-06-20 12:00:00 -07001783 }
1784
Adam Langley95c29f32014-06-20 12:00:00 -07001785 ri_check:
1786
1787 /* Need RI if renegotiating */
1788
1789 if (!renegotiate_seen && s->renegotiate &&
1790 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1791 {
David Benjamindc72ff72014-06-25 12:36:10 -04001792 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin172fc2c2014-09-06 13:09:47 -04001793 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07001794 return 0;
1795 }
Adam Langley95c29f32014-06-20 12:00:00 -07001796
1797 return 1;
1798 }
1799
David Benjamindc72ff72014-06-25 12:36:10 -04001800int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001801 {
David Benjamindc72ff72014-06-25 12:36:10 -04001802 int alert = -1;
1803 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001804 {
David Benjamindc72ff72014-06-25 12:36:10 -04001805 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07001806 return 0;
1807 }
1808
David Benjamin6c7aed02014-08-27 16:42:38 -04001809 if (ssl_check_clienthello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001810 {
David Benjamin9d28c752014-07-05 00:43:48 -04001811 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07001812 return 0;
1813 }
1814 return 1;
David Benjamin072334d2014-07-13 16:24:27 -04001815 }
Adam Langley95c29f32014-06-20 12:00:00 -07001816
Adam Langley95c29f32014-06-20 12:00:00 -07001817/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1818 * elements of zero length are allowed and the set of elements must exactly fill
1819 * the length of the block. */
David Benjamin03973092014-06-24 23:27:17 -04001820static char ssl_next_proto_validate(const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001821 {
David Benjamin03973092014-06-24 23:27:17 -04001822 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001823
David Benjamin03973092014-06-24 23:27:17 -04001824 while (CBS_len(&copy) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001825 {
David Benjamin03973092014-06-24 23:27:17 -04001826 CBS proto;
1827 if (!CBS_get_u8_length_prefixed(&copy, &proto) ||
1828 CBS_len(&proto) == 0)
1829 {
Adam Langley95c29f32014-06-20 12:00:00 -07001830 return 0;
David Benjamin03973092014-06-24 23:27:17 -04001831 }
Adam Langley95c29f32014-06-20 12:00:00 -07001832 }
David Benjamin03973092014-06-24 23:27:17 -04001833 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001834 }
Adam Langley95c29f32014-06-20 12:00:00 -07001835
David Benjamin03973092014-06-24 23:27:17 -04001836static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001837 {
Adam Langley95c29f32014-06-20 12:00:00 -07001838 int tlsext_servername = 0;
1839 int renegotiate_seen = 0;
David Benjamin03973092014-06-24 23:27:17 -04001840 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001841
David Benjamin6c7aed02014-08-27 16:42:38 -04001842 /* TODO(davidben): Move all of these to some per-handshake state that
1843 * gets systematically reset on a new handshake; perhaps allocate it
1844 * fresh each time so it's not even kept around post-handshake. */
Adam Langley95c29f32014-06-20 12:00:00 -07001845 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001846
David Benjamin6c7aed02014-08-27 16:42:38 -04001847 s->tlsext_ticket_expected = 0;
1848 s->s3->tmp.certificate_status_expected = 0;
Adam Langley75712922014-10-10 16:23:43 -07001849 s->s3->tmp.extended_master_secret = 0;
David Benjamin64442872014-07-21 17:43:45 -04001850
Adam Langley95c29f32014-06-20 12:00:00 -07001851 if (s->s3->alpn_selected)
1852 {
1853 OPENSSL_free(s->s3->alpn_selected);
1854 s->s3->alpn_selected = NULL;
1855 }
1856
David Benjamina19fc252014-10-19 00:14:36 -04001857 /* Clear ECC extensions */
1858 if (s->s3->tmp.peer_ecpointformatlist != 0)
1859 {
1860 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1861 s->s3->tmp.peer_ecpointformatlist = NULL;
1862 s->s3->tmp.peer_ecpointformatlist_length = 0;
1863 }
1864
David Benjamin03973092014-06-24 23:27:17 -04001865 /* There may be no extensions. */
1866 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001867 {
David Benjamin03973092014-06-24 23:27:17 -04001868 goto ri_check;
1869 }
1870
David Benjamin35a7a442014-07-05 00:23:20 -04001871 /* Decode the extensions block and check it is valid. */
1872 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1873 !tls1_check_duplicate_extensions(&extensions))
David Benjamin03973092014-06-24 23:27:17 -04001874 {
1875 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001876 return 0;
1877 }
1878
David Benjamin03973092014-06-24 23:27:17 -04001879 while (CBS_len(&extensions) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001880 {
David Benjamin03973092014-06-24 23:27:17 -04001881 uint16_t type;
1882 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001883
David Benjamin03973092014-06-24 23:27:17 -04001884 /* Decode the next extension. */
1885 if (!CBS_get_u16(&extensions, &type) ||
1886 !CBS_get_u16_length_prefixed(&extensions, &extension))
1887 {
1888 *out_alert = SSL_AD_DECODE_ERROR;
1889 return 0;
1890 }
Adam Langley95c29f32014-06-20 12:00:00 -07001891
1892 if (s->tlsext_debug_cb)
David Benjamin03973092014-06-24 23:27:17 -04001893 {
1894 s->tlsext_debug_cb(s, 1, type, (unsigned char*)CBS_data(&extension),
1895 CBS_len(&extension), s->tlsext_debug_arg);
1896 }
Adam Langley95c29f32014-06-20 12:00:00 -07001897
1898 if (type == TLSEXT_TYPE_server_name)
1899 {
David Benjamin03973092014-06-24 23:27:17 -04001900 /* The extension must be empty. */
1901 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001902 {
David Benjamin03973092014-06-24 23:27:17 -04001903 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001904 return 0;
1905 }
David Benjamin03973092014-06-24 23:27:17 -04001906 /* We must have sent it in ClientHello. */
1907 if (s->tlsext_hostname == NULL)
1908 {
1909 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1910 return 0;
1911 }
1912 tlsext_servername = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001913 }
Adam Langley95c29f32014-06-20 12:00:00 -07001914 else if (type == TLSEXT_TYPE_ec_point_formats)
1915 {
David Benjamin03973092014-06-24 23:27:17 -04001916 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001917
David Benjamin03973092014-06-24 23:27:17 -04001918 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1919 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001920 {
David Benjamin03973092014-06-24 23:27:17 -04001921 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001922 return 0;
1923 }
David Benjamin03973092014-06-24 23:27:17 -04001924
David Benjamina19fc252014-10-19 00:14:36 -04001925 if (!CBS_stow(&ec_point_format_list,
1926 &s->s3->tmp.peer_ecpointformatlist,
1927 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001928 {
David Benjamina19fc252014-10-19 00:14:36 -04001929 *out_alert = SSL_AD_INTERNAL_ERROR;
1930 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001931 }
Adam Langley95c29f32014-06-20 12:00:00 -07001932 }
Adam Langley95c29f32014-06-20 12:00:00 -07001933 else if (type == TLSEXT_TYPE_session_ticket)
1934 {
David Benjamin03973092014-06-24 23:27:17 -04001935 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001936 {
David Benjamin03973092014-06-24 23:27:17 -04001937 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07001938 return 0;
1939 }
David Benjamin03973092014-06-24 23:27:17 -04001940
Adam Langley95c29f32014-06-20 12:00:00 -07001941 s->tlsext_ticket_expected = 1;
1942 }
Adam Langley95c29f32014-06-20 12:00:00 -07001943 else if (type == TLSEXT_TYPE_status_request)
1944 {
David Benjamin03973092014-06-24 23:27:17 -04001945 /* The extension MUST be empty and may only sent if
1946 * we've requested a status request message. */
1947 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001948 {
David Benjamin03973092014-06-24 23:27:17 -04001949 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001950 return 0;
1951 }
David Benjamin6c7aed02014-08-27 16:42:38 -04001952 if (!s->ocsp_stapling_enabled)
David Benjamin03973092014-06-24 23:27:17 -04001953 {
1954 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1955 return 0;
1956 }
1957 /* Set a flag to expect a CertificateStatus message */
David Benjamin6c7aed02014-08-27 16:42:38 -04001958 s->s3->tmp.certificate_status_expected = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001959 }
David Benjamin03973092014-06-24 23:27:17 -04001960 else if (type == TLSEXT_TYPE_next_proto_neg && s->s3->tmp.finish_md_len == 0) {
1961 unsigned char *selected;
1962 unsigned char selected_len;
1963
1964 /* We must have requested it. */
1965 if (s->ctx->next_proto_select_cb == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001966 {
David Benjamin03973092014-06-24 23:27:17 -04001967 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1968 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001969 }
Adam Langley95c29f32014-06-20 12:00:00 -07001970
David Benjamin03973092014-06-24 23:27:17 -04001971 /* The data must be valid. */
1972 if (!ssl_next_proto_validate(&extension))
1973 {
1974 *out_alert = SSL_AD_DECODE_ERROR;
1975 return 0;
1976 }
1977
1978 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len,
1979 CBS_data(&extension), CBS_len(&extension),
1980 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
1981 {
1982 *out_alert = SSL_AD_INTERNAL_ERROR;
1983 return 0;
1984 }
1985
1986 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
1987 if (s->next_proto_negotiated == NULL)
1988 {
1989 *out_alert = SSL_AD_INTERNAL_ERROR;
1990 return 0;
1991 }
1992 s->next_proto_negotiated_len = selected_len;
1993 s->s3->next_proto_neg_seen = 1;
1994 }
Adam Langley95c29f32014-06-20 12:00:00 -07001995 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
1996 {
David Benjamin03973092014-06-24 23:27:17 -04001997 CBS protocol_name_list, protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001998
1999 /* We must have requested it. */
2000 if (s->alpn_client_proto_list == NULL)
2001 {
David Benjamin03973092014-06-24 23:27:17 -04002002 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07002003 return 0;
2004 }
David Benjamin03973092014-06-24 23:27:17 -04002005
2006 /* The extension data consists of a ProtocolNameList
2007 * which must have exactly one ProtocolName. Each of
2008 * these is length-prefixed. */
2009 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
2010 CBS_len(&extension) != 0 ||
2011 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
2012 CBS_len(&protocol_name_list) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002013 {
David Benjamin03973092014-06-24 23:27:17 -04002014 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002015 return 0;
2016 }
David Benjamin03973092014-06-24 23:27:17 -04002017
2018 if (!CBS_stow(&protocol_name,
2019 &s->s3->alpn_selected,
2020 &s->s3->alpn_selected_len))
Adam Langley95c29f32014-06-20 12:00:00 -07002021 {
David Benjamin03973092014-06-24 23:27:17 -04002022 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002023 return 0;
2024 }
Adam Langley95c29f32014-06-20 12:00:00 -07002025 }
2026
Adam Langley1258b6a2014-06-20 12:00:00 -07002027 else if (type == TLSEXT_TYPE_channel_id)
David Benjamin03973092014-06-24 23:27:17 -04002028 {
2029 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;
David Benjamin03973092014-06-24 23:27:17 -04002035 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002036 else if (type == TLSEXT_TYPE_channel_id_new)
2037 {
David Benjamin03973092014-06-24 23:27:17 -04002038 if (CBS_len(&extension) != 0)
2039 {
2040 *out_alert = SSL_AD_DECODE_ERROR;
2041 return 0;
2042 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002043 s->s3->tlsext_channel_id_valid = 1;
2044 s->s3->tlsext_channel_id_new = 1;
2045 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002046 else if (type == TLSEXT_TYPE_certificate_timestamp)
2047 {
2048 if (CBS_len(&extension) == 0)
2049 {
2050 *out_alert = SSL_AD_DECODE_ERROR;
2051 return 0;
2052 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002053
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002054 /* Session resumption uses the original session information. */
2055 if (!s->hit)
2056 {
2057 if (!CBS_stow(&extension,
2058 &s->session->tlsext_signed_cert_timestamp_list,
2059 &s->session->tlsext_signed_cert_timestamp_list_length))
2060 {
2061 *out_alert = SSL_AD_INTERNAL_ERROR;
2062 return 0;
2063 }
2064 }
2065 }
Adam Langley95c29f32014-06-20 12:00:00 -07002066 else if (type == TLSEXT_TYPE_renegotiate)
2067 {
David Benjamin03973092014-06-24 23:27:17 -04002068 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002069 return 0;
2070 renegotiate_seen = 1;
2071 }
Adam Langley95c29f32014-06-20 12:00:00 -07002072 else if (type == TLSEXT_TYPE_use_srtp)
2073 {
David Benjamin03973092014-06-24 23:27:17 -04002074 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002075 return 0;
2076 }
Adam Langley75712922014-10-10 16:23:43 -07002077
2078 else if (type == TLSEXT_TYPE_extended_master_secret)
2079 {
2080 if (/* It is invalid for the server to select EMS and
2081 SSLv3. */
2082 s->version == SSL3_VERSION ||
2083 CBS_len(&extension) != 0)
2084 {
2085 *out_alert = SSL_AD_DECODE_ERROR;
2086 return 0;
2087 }
2088
2089 s->s3->tmp.extended_master_secret = 1;
2090 }
Adam Langley95c29f32014-06-20 12:00:00 -07002091 }
2092
2093 if (!s->hit && tlsext_servername == 1)
2094 {
2095 if (s->tlsext_hostname)
2096 {
2097 if (s->session->tlsext_hostname == NULL)
2098 {
2099 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2100 if (!s->session->tlsext_hostname)
2101 {
David Benjamin03973092014-06-24 23:27:17 -04002102 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002103 return 0;
2104 }
2105 }
2106 else
2107 {
David Benjamin03973092014-06-24 23:27:17 -04002108 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002109 return 0;
2110 }
2111 }
2112 }
2113
Adam Langley95c29f32014-06-20 12:00:00 -07002114 ri_check:
2115
2116 /* Determine if we need to see RI. Strictly speaking if we want to
2117 * avoid an attack we should *always* see RI even on initial server
2118 * hello because the client doesn't see any renegotiation during an
2119 * attack. However this would mean we could not connect to any server
2120 * which doesn't support RI so for the immediate future tolerate RI
2121 * absence on initial connect only.
2122 */
2123 if (!renegotiate_seen
2124 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2125 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2126 {
David Benjamin03973092014-06-24 23:27:17 -04002127 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin9d28c752014-07-05 00:43:48 -04002128 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07002129 return 0;
2130 }
2131
2132 return 1;
2133 }
2134
2135
2136int ssl_prepare_clienthello_tlsext(SSL *s)
2137 {
Adam Langley95c29f32014-06-20 12:00:00 -07002138 return 1;
2139 }
2140
2141int ssl_prepare_serverhello_tlsext(SSL *s)
2142 {
2143 return 1;
2144 }
2145
David Benjamin6c7aed02014-08-27 16:42:38 -04002146static int ssl_check_clienthello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002147 {
2148 int ret=SSL_TLSEXT_ERR_NOACK;
2149 int al = SSL_AD_UNRECOGNIZED_NAME;
2150
Adam Langley95c29f32014-06-20 12:00:00 -07002151 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2152 * ssl3_choose_cipher in s3_lib.c.
2153 */
2154 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2155 * ssl3_choose_cipher in s3_lib.c.
2156 */
Adam Langley95c29f32014-06-20 12:00:00 -07002157
2158 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2159 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2160 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2161 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2162
Adam Langley95c29f32014-06-20 12:00:00 -07002163 switch (ret)
2164 {
2165 case SSL_TLSEXT_ERR_ALERT_FATAL:
2166 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2167 return -1;
2168
2169 case SSL_TLSEXT_ERR_ALERT_WARNING:
2170 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002171 return 1;
2172
Adam Langley95c29f32014-06-20 12:00:00 -07002173 case SSL_TLSEXT_ERR_NOACK:
Adam Langleyed8270a2014-09-02 13:52:56 -07002174 s->should_ack_sni = 0;
2175 return 1;
2176
2177 default:
2178 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002179 }
2180 }
2181
David Benjamin6c7aed02014-08-27 16:42:38 -04002182static int ssl_check_serverhello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002183 {
2184 int ret=SSL_TLSEXT_ERR_NOACK;
2185 int al = SSL_AD_UNRECOGNIZED_NAME;
2186
Adam Langley95c29f32014-06-20 12:00:00 -07002187 /* If we are client and using an elliptic curve cryptography cipher
2188 * suite, then if server returns an EC point formats lists extension
2189 * it must contain uncompressed.
2190 */
2191 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2192 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamina9ca90a2014-09-26 18:53:43 -04002193 if (((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) &&
2194 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed))
Adam Langley95c29f32014-06-20 12:00:00 -07002195 {
David Benjamina9ca90a2014-09-26 18:53:43 -04002196 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2197 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002198 }
2199 ret = SSL_TLSEXT_ERR_OK;
Adam Langley95c29f32014-06-20 12:00:00 -07002200
2201 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2202 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2203 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2204 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2205
Adam Langley95c29f32014-06-20 12:00:00 -07002206 switch (ret)
2207 {
2208 case SSL_TLSEXT_ERR_ALERT_FATAL:
Adam Langleyed8270a2014-09-02 13:52:56 -07002209 ssl3_send_alert(s,SSL3_AL_FATAL,al);
Adam Langley95c29f32014-06-20 12:00:00 -07002210 return -1;
2211
2212 case SSL_TLSEXT_ERR_ALERT_WARNING:
2213 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002214 return 1;
2215
2216 default:
2217 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002218 }
2219 }
2220
David Benjamin03973092014-06-24 23:27:17 -04002221int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07002222 {
David Benjamin03973092014-06-24 23:27:17 -04002223 int alert = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002224 if (s->version < SSL3_VERSION)
2225 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002226
2227 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002228 {
David Benjamin03973092014-06-24 23:27:17 -04002229 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07002230 return 0;
2231 }
2232
David Benjamin03973092014-06-24 23:27:17 -04002233 if (ssl_check_serverhello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002234 {
David Benjamin9d28c752014-07-05 00:43:48 -04002235 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, SSL_R_SERVERHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07002236 return 0;
2237 }
David Benjamin03973092014-06-24 23:27:17 -04002238
Adam Langley95c29f32014-06-20 12:00:00 -07002239 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002240 }
Adam Langley95c29f32014-06-20 12:00:00 -07002241
2242/* Since the server cache lookup is done early on in the processing of the
2243 * ClientHello, and other operations depend on the result, we need to handle
2244 * any TLS session ticket extension at the same time.
2245 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002246 * ctx: contains the early callback context, which is the result of a
2247 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002248 * ret: (output) on return, if a ticket was decrypted, then this is set to
2249 * point to the resulting session.
2250 *
Adam Langley95c29f32014-06-20 12:00:00 -07002251 * Returns:
2252 * -1: fatal error, either from parsing or decrypting the ticket.
2253 * 0: no ticket was found (or was ignored, based on settings).
2254 * 1: a zero length extension was found, indicating that the client supports
2255 * session tickets but doesn't currently have one to offer.
David Benjamind1681e62014-11-20 14:54:14 -05002256 * 2: a ticket was offered but couldn't be decrypted because of a non-fatal
2257 * error.
Adam Langley95c29f32014-06-20 12:00:00 -07002258 * 3: a ticket was successfully decrypted and *ret was set.
2259 *
2260 * Side effects:
2261 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2262 * a new session ticket to the client because the client indicated support
David Benjamind1681e62014-11-20 14:54:14 -05002263 * but the client either doesn't have a session ticket or we couldn't use
2264 * the one it gave us, or if s->ctx->tlsext_ticket_key_cb asked to renew
2265 * the client's ticket. Otherwise, s->tlsext_ticket_expected is set to 0.
Adam Langley95c29f32014-06-20 12:00:00 -07002266 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002267int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
2268 SSL_SESSION **ret)
Adam Langley95c29f32014-06-20 12:00:00 -07002269 {
Adam Langley95c29f32014-06-20 12:00:00 -07002270 *ret = NULL;
2271 s->tlsext_ticket_expected = 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002272 const unsigned char *data;
2273 size_t len;
2274 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002275
2276 /* If tickets disabled behave as if no ticket present
2277 * to permit stateful resumption.
2278 */
2279 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2280 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002281 if ((s->version <= SSL3_VERSION) && !ctx->extensions)
Adam Langley95c29f32014-06-20 12:00:00 -07002282 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002283 if (!SSL_early_callback_ctx_extension_get(
2284 ctx, TLSEXT_TYPE_session_ticket, &data, &len))
Adam Langley95c29f32014-06-20 12:00:00 -07002285 {
Adam Langleydc9b1412014-06-20 12:00:00 -07002286 return 0;
2287 }
2288 if (len == 0)
2289 {
2290 /* The client will accept a ticket but doesn't
2291 * currently have one. */
2292 s->tlsext_ticket_expected = 1;
2293 return 1;
2294 }
Adam Langleydc9b1412014-06-20 12:00:00 -07002295 r = tls_decrypt_ticket(s, data, len, ctx->session_id,
2296 ctx->session_id_len, ret);
2297 switch (r)
2298 {
2299 case 2: /* ticket couldn't be decrypted */
2300 s->tlsext_ticket_expected = 1;
2301 return 2;
2302 case 3: /* ticket was decrypted */
2303 return r;
2304 case 4: /* ticket decrypted but need to renew */
2305 s->tlsext_ticket_expected = 1;
2306 return 3;
2307 default: /* fatal error */
Adam Langley95c29f32014-06-20 12:00:00 -07002308 return -1;
2309 }
Adam Langley95c29f32014-06-20 12:00:00 -07002310 }
2311
2312/* tls_decrypt_ticket attempts to decrypt a session ticket.
2313 *
2314 * etick: points to the body of the session ticket extension.
2315 * eticklen: the length of the session tickets extenion.
2316 * sess_id: points at the session ID.
2317 * sesslen: the length of the session ID.
2318 * psess: (output) on return, if a ticket was decrypted, then this is set to
2319 * point to the resulting session.
2320 *
2321 * Returns:
2322 * -1: fatal error, either from parsing or decrypting the ticket.
2323 * 2: the ticket couldn't be decrypted.
2324 * 3: a ticket was successfully decrypted and *psess was set.
2325 * 4: same as 3, but the ticket needs to be renewed.
2326 */
2327static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
2328 const unsigned char *sess_id, int sesslen,
2329 SSL_SESSION **psess)
2330 {
2331 SSL_SESSION *sess;
2332 unsigned char *sdec;
2333 const unsigned char *p;
2334 int slen, mlen, renew_ticket = 0;
2335 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2336 HMAC_CTX hctx;
2337 EVP_CIPHER_CTX ctx;
2338 SSL_CTX *tctx = s->initial_ctx;
2339 /* Need at least keyname + iv + some encrypted data */
2340 if (eticklen < 48)
2341 return 2;
2342 /* Initialize session ticket encryption and HMAC contexts */
2343 HMAC_CTX_init(&hctx);
2344 EVP_CIPHER_CTX_init(&ctx);
2345 if (tctx->tlsext_ticket_key_cb)
2346 {
2347 unsigned char *nctick = (unsigned char *)etick;
2348 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
2349 &ctx, &hctx, 0);
2350 if (rv < 0)
2351 return -1;
2352 if (rv == 0)
2353 return 2;
2354 if (rv == 2)
2355 renew_ticket = 1;
2356 }
2357 else
2358 {
2359 /* Check key name matches */
2360 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
2361 return 2;
David Benjaminae3e4872014-11-18 21:52:26 -05002362 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2363 tlsext_tick_md(), NULL) ||
2364 !EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2365 tctx->tlsext_tick_aes_key, etick + 16))
2366 {
2367 HMAC_CTX_cleanup(&hctx);
2368 EVP_CIPHER_CTX_cleanup(&ctx);
2369 return -1;
2370 }
Adam Langley95c29f32014-06-20 12:00:00 -07002371 }
2372 /* Attempt to process session ticket, first conduct sanity and
2373 * integrity checks on ticket.
2374 */
2375 mlen = HMAC_size(&hctx);
2376 if (mlen < 0)
2377 {
David Benjaminae3e4872014-11-18 21:52:26 -05002378 HMAC_CTX_cleanup(&hctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002379 EVP_CIPHER_CTX_cleanup(&ctx);
2380 return -1;
2381 }
2382 eticklen -= mlen;
2383 /* Check HMAC of encrypted ticket */
2384 HMAC_Update(&hctx, etick, eticklen);
2385 HMAC_Final(&hctx, tick_hmac, NULL);
2386 HMAC_CTX_cleanup(&hctx);
2387 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
Adam Langley38311732014-10-16 19:04:35 -07002388 {
2389 EVP_CIPHER_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002390 return 2;
Adam Langley38311732014-10-16 19:04:35 -07002391 }
Adam Langley95c29f32014-06-20 12:00:00 -07002392 /* Attempt to decrypt session data */
2393 /* Move p after IV to start of encrypted ticket, update length */
2394 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2395 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2396 sdec = OPENSSL_malloc(eticklen);
2397 if (!sdec)
2398 {
2399 EVP_CIPHER_CTX_cleanup(&ctx);
2400 return -1;
2401 }
2402 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2403 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0)
Adam Langley3e148852014-07-24 17:34:02 -07002404 {
2405 EVP_CIPHER_CTX_cleanup(&ctx);
2406 OPENSSL_free(sdec);
Adam Langley95c29f32014-06-20 12:00:00 -07002407 return 2;
Adam Langley3e148852014-07-24 17:34:02 -07002408 }
Adam Langley95c29f32014-06-20 12:00:00 -07002409 slen += mlen;
2410 EVP_CIPHER_CTX_cleanup(&ctx);
2411 p = sdec;
2412
2413 sess = d2i_SSL_SESSION(NULL, &p, slen);
2414 OPENSSL_free(sdec);
2415 if (sess)
2416 {
2417 /* The session ID, if non-empty, is used by some clients to
2418 * detect that the ticket has been accepted. So we copy it to
2419 * the session structure. If it is empty set length to zero
2420 * as required by standard.
2421 */
2422 if (sesslen)
2423 memcpy(sess->session_id, sess_id, sesslen);
2424 sess->session_id_length = sesslen;
2425 *psess = sess;
2426 if (renew_ticket)
2427 return 4;
2428 else
2429 return 3;
2430 }
2431 ERR_clear_error();
2432 /* For session parse failure, indicate that we need to send a new
2433 * ticket. */
2434 return 2;
2435 }
2436
2437/* Tables to translate from NIDs to TLS v1.2 ids */
2438
2439typedef struct
2440 {
2441 int nid;
2442 int id;
2443 } tls12_lookup;
2444
David Benjamincff64722014-08-19 19:54:46 -04002445static const tls12_lookup tls12_md[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002446 {NID_md5, TLSEXT_hash_md5},
2447 {NID_sha1, TLSEXT_hash_sha1},
2448 {NID_sha224, TLSEXT_hash_sha224},
2449 {NID_sha256, TLSEXT_hash_sha256},
2450 {NID_sha384, TLSEXT_hash_sha384},
2451 {NID_sha512, TLSEXT_hash_sha512}
2452};
2453
David Benjamincff64722014-08-19 19:54:46 -04002454static const tls12_lookup tls12_sig[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002455 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
Adam Langley95c29f32014-06-20 12:00:00 -07002456 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
2457};
2458
David Benjamincff64722014-08-19 19:54:46 -04002459static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002460 {
2461 size_t i;
2462 for (i = 0; i < tlen; i++)
2463 {
2464 if (table[i].nid == nid)
2465 return table[i].id;
2466 }
2467 return -1;
2468 }
2469
David Benjamincff64722014-08-19 19:54:46 -04002470static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002471 {
2472 size_t i;
2473 for (i = 0; i < tlen; i++)
2474 {
2475 if ((table[i].id) == id)
2476 return table[i].nid;
2477 }
2478 return NID_undef;
2479 }
2480
2481int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
2482 {
2483 int sig_id, md_id;
2484 if (!md)
2485 return 0;
2486 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2487 sizeof(tls12_md)/sizeof(tls12_lookup));
2488 if (md_id == -1)
2489 return 0;
2490 sig_id = tls12_get_sigid(pk);
2491 if (sig_id == -1)
2492 return 0;
2493 p[0] = (unsigned char)md_id;
2494 p[1] = (unsigned char)sig_id;
2495 return 1;
2496 }
2497
2498int tls12_get_sigid(const EVP_PKEY *pk)
2499 {
2500 return tls12_find_id(pk->type, tls12_sig,
2501 sizeof(tls12_sig)/sizeof(tls12_lookup));
2502 }
2503
2504const EVP_MD *tls12_get_hash(unsigned char hash_alg)
2505 {
2506 switch(hash_alg)
2507 {
Adam Langley95c29f32014-06-20 12:00:00 -07002508 case TLSEXT_hash_md5:
Adam Langley95c29f32014-06-20 12:00:00 -07002509 return EVP_md5();
Adam Langley95c29f32014-06-20 12:00:00 -07002510 case TLSEXT_hash_sha1:
2511 return EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002512 case TLSEXT_hash_sha224:
2513 return EVP_sha224();
2514
2515 case TLSEXT_hash_sha256:
2516 return EVP_sha256();
Adam Langley95c29f32014-06-20 12:00:00 -07002517 case TLSEXT_hash_sha384:
2518 return EVP_sha384();
2519
2520 case TLSEXT_hash_sha512:
2521 return EVP_sha512();
Adam Langley95c29f32014-06-20 12:00:00 -07002522 default:
2523 return NULL;
2524
2525 }
2526 }
2527
David Benjaminec2f27d2014-11-13 19:17:25 -05002528/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2529 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
2530static int tls12_get_pkey_type(uint8_t sig_alg)
Adam Langley95c29f32014-06-20 12:00:00 -07002531 {
2532 switch(sig_alg)
2533 {
Adam Langley95c29f32014-06-20 12:00:00 -07002534 case TLSEXT_signature_rsa:
David Benjaminec2f27d2014-11-13 19:17:25 -05002535 return EVP_PKEY_RSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002536 case TLSEXT_signature_ecdsa:
David Benjaminec2f27d2014-11-13 19:17:25 -05002537 return EVP_PKEY_EC;
Adam Langley95c29f32014-06-20 12:00:00 -07002538 }
2539 return -1;
2540 }
2541
2542/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2543static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
2544 int *psignhash_nid, const unsigned char *data)
2545 {
2546 int sign_nid = 0, hash_nid = 0;
2547 if (!phash_nid && !psign_nid && !psignhash_nid)
2548 return;
2549 if (phash_nid || psignhash_nid)
2550 {
2551 hash_nid = tls12_find_nid(data[0], tls12_md,
2552 sizeof(tls12_md)/sizeof(tls12_lookup));
2553 if (phash_nid)
2554 *phash_nid = hash_nid;
2555 }
2556 if (psign_nid || psignhash_nid)
2557 {
2558 sign_nid = tls12_find_nid(data[1], tls12_sig,
2559 sizeof(tls12_sig)/sizeof(tls12_lookup));
2560 if (psign_nid)
2561 *psign_nid = sign_nid;
2562 }
2563 if (psignhash_nid)
2564 {
2565 if (sign_nid && hash_nid)
2566 OBJ_find_sigid_by_algs(psignhash_nid,
2567 hash_nid, sign_nid);
2568 else
2569 *psignhash_nid = NID_undef;
2570 }
2571 }
2572/* Given preference and allowed sigalgs set shared sigalgs */
2573static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
2574 const unsigned char *pref, size_t preflen,
2575 const unsigned char *allow, size_t allowlen)
2576 {
2577 const unsigned char *ptmp, *atmp;
2578 size_t i, j, nmatch = 0;
2579 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
2580 {
2581 /* Skip disabled hashes or signature algorithms */
2582 if (tls12_get_hash(ptmp[0]) == NULL)
2583 continue;
David Benjaminec2f27d2014-11-13 19:17:25 -05002584 if (tls12_get_pkey_type(ptmp[1]) == -1)
Adam Langley95c29f32014-06-20 12:00:00 -07002585 continue;
2586 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
2587 {
2588 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
2589 {
2590 nmatch++;
2591 if (shsig)
2592 {
2593 shsig->rhash = ptmp[0];
2594 shsig->rsign = ptmp[1];
2595 tls1_lookup_sigalg(&shsig->hash_nid,
2596 &shsig->sign_nid,
2597 &shsig->signandhash_nid,
2598 ptmp);
2599 shsig++;
2600 }
2601 break;
2602 }
2603 }
2604 }
2605 return nmatch;
2606 }
2607
2608/* Set shared signature algorithms for SSL structures */
2609static int tls1_set_shared_sigalgs(SSL *s)
2610 {
2611 const unsigned char *pref, *allow, *conf;
2612 size_t preflen, allowlen, conflen;
2613 size_t nmatch;
2614 TLS_SIGALGS *salgs = NULL;
2615 CERT *c = s->cert;
Adam Langleydb4f9522014-06-20 12:00:00 -07002616 if (c->shared_sigalgs)
2617 {
2618 OPENSSL_free(c->shared_sigalgs);
2619 c->shared_sigalgs = NULL;
2620 }
Adam Langley95c29f32014-06-20 12:00:00 -07002621 /* If client use client signature algorithms if not NULL */
David Benjamin335d10d2014-08-06 19:56:33 -04002622 if (!s->server && c->client_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002623 {
2624 conf = c->client_sigalgs;
2625 conflen = c->client_sigalgslen;
2626 }
David Benjamin335d10d2014-08-06 19:56:33 -04002627 else if (c->conf_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002628 {
2629 conf = c->conf_sigalgs;
2630 conflen = c->conf_sigalgslen;
2631 }
2632 else
2633 conflen = tls12_get_psigalgs(s, &conf);
David Benjamin335d10d2014-08-06 19:56:33 -04002634 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
Adam Langley95c29f32014-06-20 12:00:00 -07002635 {
2636 pref = conf;
2637 preflen = conflen;
2638 allow = c->peer_sigalgs;
2639 allowlen = c->peer_sigalgslen;
2640 }
2641 else
2642 {
2643 allow = conf;
2644 allowlen = conflen;
2645 pref = c->peer_sigalgs;
2646 preflen = c->peer_sigalgslen;
2647 }
2648 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2649 if (!nmatch)
2650 return 1;
2651 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2652 if (!salgs)
2653 return 0;
2654 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2655 c->shared_sigalgs = salgs;
2656 c->shared_sigalgslen = nmatch;
2657 return 1;
2658 }
2659
2660
2661/* Set preferred digest for each key type */
2662
David Benjamincd996942014-07-20 16:23:51 -04002663int tls1_process_sigalgs(SSL *s, const CBS *sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002664 {
Adam Langley95c29f32014-06-20 12:00:00 -07002665 CERT *c = s->cert;
David Benjamincd996942014-07-20 16:23:51 -04002666
Adam Langley95c29f32014-06-20 12:00:00 -07002667 /* Extension ignored for inappropriate versions */
2668 if (!SSL_USE_SIGALGS(s))
2669 return 1;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002670 /* Length must be even */
David Benjamincd996942014-07-20 16:23:51 -04002671 if (CBS_len(sigalgs) % 2 != 0)
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002672 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002673 /* Should never happen */
2674 if (!c)
2675 return 0;
2676
David Benjamincd996942014-07-20 16:23:51 -04002677 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen))
Adam Langley95c29f32014-06-20 12:00:00 -07002678 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002679
2680 tls1_set_shared_sigalgs(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002681 return 1;
2682 }
2683
David Benjaminec2f27d2014-11-13 19:17:25 -05002684const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey)
2685 {
2686 CERT *c = s->cert;
2687 int type = EVP_PKEY_id(pkey);
2688 size_t i;
2689
2690 /* Select the first shared digest supported by our key. */
2691 for (i = 0; i < c->shared_sigalgslen; i++)
2692 {
2693 const EVP_MD *md = tls12_get_hash(c->shared_sigalgs[i].rhash);
2694 if (md == NULL || tls12_get_pkey_type(c->shared_sigalgs[i].rsign) != type)
2695 continue;
2696 if (!EVP_PKEY_supports_digest(pkey, md))
2697 continue;
2698 return md;
2699 }
2700
2701 /* If no suitable digest may be found, default to SHA-1. */
2702 return EVP_sha1();
2703 }
Adam Langley95c29f32014-06-20 12:00:00 -07002704
2705int SSL_get_sigalgs(SSL *s, int idx,
2706 int *psign, int *phash, int *psignhash,
2707 unsigned char *rsig, unsigned char *rhash)
2708 {
2709 const unsigned char *psig = s->cert->peer_sigalgs;
2710 if (psig == NULL)
2711 return 0;
2712 if (idx >= 0)
2713 {
2714 idx <<= 1;
2715 if (idx >= (int)s->cert->peer_sigalgslen)
2716 return 0;
2717 psig += idx;
2718 if (rhash)
2719 *rhash = psig[0];
2720 if (rsig)
2721 *rsig = psig[1];
2722 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2723 }
2724 return s->cert->peer_sigalgslen / 2;
2725 }
2726
2727int SSL_get_shared_sigalgs(SSL *s, int idx,
2728 int *psign, int *phash, int *psignhash,
2729 unsigned char *rsig, unsigned char *rhash)
2730 {
2731 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
2732 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
2733 return 0;
2734 shsigalgs += idx;
2735 if (phash)
2736 *phash = shsigalgs->hash_nid;
2737 if (psign)
2738 *psign = shsigalgs->sign_nid;
2739 if (psignhash)
2740 *psignhash = shsigalgs->signandhash_nid;
2741 if (rsig)
2742 *rsig = shsigalgs->rsign;
2743 if (rhash)
2744 *rhash = shsigalgs->rhash;
2745 return s->cert->shared_sigalgslen;
2746 }
2747
Adam Langley1258b6a2014-06-20 12:00:00 -07002748/* tls1_channel_id_hash calculates the signed data for a Channel ID on the given
2749 * SSL connection and writes it to |md|. */
2750int
2751tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s)
2752 {
2753 EVP_MD_CTX ctx;
2754 unsigned char temp_digest[EVP_MAX_MD_SIZE];
2755 unsigned temp_digest_len;
2756 int i;
2757 static const char kClientIDMagic[] = "TLS Channel ID signature";
2758
2759 if (s->s3->handshake_buffer)
Adam Langley75712922014-10-10 16:23:43 -07002760 if (!ssl3_digest_cached_records(s, free_handshake_buffer))
Adam Langley1258b6a2014-06-20 12:00:00 -07002761 return 0;
2762
2763 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2764
2765 if (s->hit && s->s3->tlsext_channel_id_new)
2766 {
2767 static const char kResumptionMagic[] = "Resumption";
2768 EVP_DigestUpdate(md, kResumptionMagic,
2769 sizeof(kResumptionMagic));
2770 if (s->session->original_handshake_hash_len == 0)
2771 return 0;
2772 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2773 s->session->original_handshake_hash_len);
2774 }
2775
2776 EVP_MD_CTX_init(&ctx);
2777 for (i = 0; i < SSL_MAX_DIGEST; i++)
2778 {
2779 if (s->s3->handshake_dgst[i] == NULL)
2780 continue;
2781 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
2782 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2783 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2784 }
2785 EVP_MD_CTX_cleanup(&ctx);
2786
2787 return 1;
2788 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002789
2790/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2791 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
2792int tls1_record_handshake_hashes_for_channel_id(SSL *s)
2793 {
2794 int digest_len;
2795 /* This function should never be called for a resumed session because
2796 * the handshake hashes that we wish to record are for the original,
2797 * full handshake. */
2798 if (s->hit)
2799 return -1;
2800 /* It only makes sense to call this function if Channel IDs have been
2801 * negotiated. */
2802 if (!s->s3->tlsext_channel_id_new)
2803 return -1;
2804
2805 digest_len = tls1_handshake_digest(
2806 s, s->session->original_handshake_hash,
2807 sizeof(s->session->original_handshake_hash));
2808 if (digest_len < 0)
2809 return -1;
2810
2811 s->session->original_handshake_hash_len = digest_len;
2812
2813 return 1;
2814 }
2815
Adam Langley95c29f32014-06-20 12:00:00 -07002816int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
2817 {
2818 unsigned char *sigalgs, *sptr;
2819 int rhash, rsign;
2820 size_t i;
2821 if (salglen & 1)
2822 return 0;
2823 sigalgs = OPENSSL_malloc(salglen);
2824 if (sigalgs == NULL)
2825 return 0;
2826 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
2827 {
2828 rhash = tls12_find_id(*psig_nids++, tls12_md,
2829 sizeof(tls12_md)/sizeof(tls12_lookup));
2830 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2831 sizeof(tls12_sig)/sizeof(tls12_lookup));
2832
2833 if (rhash == -1 || rsign == -1)
2834 goto err;
2835 *sptr++ = rhash;
2836 *sptr++ = rsign;
2837 }
2838
2839 if (client)
2840 {
2841 if (c->client_sigalgs)
2842 OPENSSL_free(c->client_sigalgs);
2843 c->client_sigalgs = sigalgs;
2844 c->client_sigalgslen = salglen;
2845 }
2846 else
2847 {
2848 if (c->conf_sigalgs)
2849 OPENSSL_free(c->conf_sigalgs);
2850 c->conf_sigalgs = sigalgs;
2851 c->conf_sigalgslen = salglen;
2852 }
2853
2854 return 1;
2855
2856 err:
2857 OPENSSL_free(sigalgs);
2858 return 0;
2859 }
2860