blob: 4b13cfecae82e6e68614602a10688f146f8831b7 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
109#include <stdio.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400110#include <stdlib.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700111#include <assert.h>
112
David Benjamin03973092014-06-24 23:27:17 -0400113#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700114#include <openssl/evp.h>
115#include <openssl/hmac.h>
116#include <openssl/mem.h>
117#include <openssl/obj.h>
118#include <openssl/rand.h>
119
120#include "ssl_locl.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700121static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
122 const unsigned char *sess_id, int sesslen,
123 SSL_SESSION **psess);
David Benjamin6c7aed02014-08-27 16:42:38 -0400124static int ssl_check_clienthello_tlsext(SSL *s);
125static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700126
127SSL3_ENC_METHOD TLSv1_enc_data={
128 tls1_enc,
129 tls1_mac,
130 tls1_setup_key_block,
131 tls1_generate_master_secret,
132 tls1_change_cipher_state,
133 tls1_final_finish_mac,
134 TLS1_FINISH_MAC_LENGTH,
135 tls1_cert_verify_mac,
136 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
137 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
138 tls1_alert_code,
139 tls1_export_keying_material,
140 0,
141 SSL3_HM_HEADER_LENGTH,
142 ssl3_set_handshake_header,
143 ssl3_handshake_write
144 };
145
146SSL3_ENC_METHOD TLSv1_1_enc_data={
147 tls1_enc,
148 tls1_mac,
149 tls1_setup_key_block,
150 tls1_generate_master_secret,
151 tls1_change_cipher_state,
152 tls1_final_finish_mac,
153 TLS1_FINISH_MAC_LENGTH,
154 tls1_cert_verify_mac,
155 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
156 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
157 tls1_alert_code,
158 tls1_export_keying_material,
159 SSL_ENC_FLAG_EXPLICIT_IV,
160 SSL3_HM_HEADER_LENGTH,
161 ssl3_set_handshake_header,
162 ssl3_handshake_write
163 };
164
165SSL3_ENC_METHOD TLSv1_2_enc_data={
166 tls1_enc,
167 tls1_mac,
168 tls1_setup_key_block,
169 tls1_generate_master_secret,
170 tls1_change_cipher_state,
171 tls1_final_finish_mac,
172 TLS1_FINISH_MAC_LENGTH,
173 tls1_cert_verify_mac,
174 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
175 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
176 tls1_alert_code,
177 tls1_export_keying_material,
178 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
179 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
180 SSL3_HM_HEADER_LENGTH,
181 ssl3_set_handshake_header,
182 ssl3_handshake_write
183 };
184
David Benjamin35a7a442014-07-05 00:23:20 -0400185static int compare_uint16_t(const void *p1, const void *p2)
186 {
187 uint16_t u1 = *((const uint16_t*)p1);
188 uint16_t u2 = *((const uint16_t*)p2);
189 if (u1 < u2)
190 {
191 return -1;
192 }
193 else if (u1 > u2)
194 {
195 return 1;
196 }
197 else
198 {
199 return 0;
200 }
201 }
202
203/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be more
204 * than one extension of the same type in a ClientHello or ServerHello. This
205 * function does an initial scan over the extensions block to filter those
206 * out. */
207static int tls1_check_duplicate_extensions(const CBS *cbs)
208 {
209 CBS extensions = *cbs;
210 size_t num_extensions = 0, i = 0;
211 uint16_t *extension_types = NULL;
212 int ret = 0;
213
214 /* First pass: count the extensions. */
215 while (CBS_len(&extensions) > 0)
216 {
217 uint16_t type;
218 CBS extension;
219
220 if (!CBS_get_u16(&extensions, &type) ||
221 !CBS_get_u16_length_prefixed(&extensions, &extension))
222 {
223 goto done;
224 }
225
226 num_extensions++;
227 }
228
David Benjamin9a373592014-07-25 04:27:53 -0400229 if (num_extensions == 0)
230 {
231 return 1;
232 }
233
David Benjamin35a7a442014-07-05 00:23:20 -0400234 extension_types = (uint16_t*)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
235 if (extension_types == NULL)
236 {
237 OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions, ERR_R_MALLOC_FAILURE);
238 goto done;
239 }
240
241 /* Second pass: gather the extension types. */
242 extensions = *cbs;
243 for (i = 0; i < num_extensions; i++)
244 {
245 CBS extension;
246
247 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
248 !CBS_get_u16_length_prefixed(&extensions, &extension))
249 {
250 /* This should not happen. */
251 goto done;
252 }
253 }
254 assert(CBS_len(&extensions) == 0);
255
256 /* Sort the extensions and make sure there are no duplicates. */
257 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
258 for (i = 1; i < num_extensions; i++)
259 {
260 if (extension_types[i-1] == extension_types[i])
261 {
262 goto done;
263 }
264 }
265
266 ret = 1;
267done:
268 if (extension_types)
269 OPENSSL_free(extension_types);
270 return ret;
271 }
272
Adam Langleydc9b1412014-06-20 12:00:00 -0700273char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx)
274 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400275 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
276
277 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700278
279 /* Skip client version. */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400280 if (!CBS_skip(&client_hello, 2))
Adam Langleydc9b1412014-06-20 12:00:00 -0700281 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700282
283 /* Skip client nonce. */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400284 if (!CBS_skip(&client_hello, 32))
Adam Langleydc9b1412014-06-20 12:00:00 -0700285 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700286
David Benjamin8f2c20e2014-07-09 09:30:38 -0400287 /* Extract session_id. */
288 if (!CBS_get_u8_length_prefixed(&client_hello, &session_id))
Adam Langleydc9b1412014-06-20 12:00:00 -0700289 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400290 ctx->session_id = CBS_data(&session_id);
291 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700292
293 /* Skip past DTLS cookie */
David Benjamin09bd58d2014-08-12 21:22:28 -0400294 if (SSL_IS_DTLS(ctx->ssl))
Adam Langleydc9b1412014-06-20 12:00:00 -0700295 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400296 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700297
David Benjamin8f2c20e2014-07-09 09:30:38 -0400298 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie))
Adam Langleydc9b1412014-06-20 12:00:00 -0700299 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700300 }
301
David Benjamin8f2c20e2014-07-09 09:30:38 -0400302 /* Extract cipher_suites. */
303 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
304 CBS_len(&cipher_suites) < 2 ||
305 (CBS_len(&cipher_suites) & 1) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700306 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400307 ctx->cipher_suites = CBS_data(&cipher_suites);
308 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700309
David Benjamin8f2c20e2014-07-09 09:30:38 -0400310 /* Extract compression_methods. */
311 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
312 CBS_len(&compression_methods) < 1)
Adam Langleydc9b1412014-06-20 12:00:00 -0700313 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400314 ctx->compression_methods = CBS_data(&compression_methods);
315 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700316
317 /* If the ClientHello ends here then it's valid, but doesn't have any
318 * extensions. (E.g. SSLv3.) */
David Benjamin8f2c20e2014-07-09 09:30:38 -0400319 if (CBS_len(&client_hello) == 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700320 {
321 ctx->extensions = NULL;
322 ctx->extensions_len = 0;
323 return 1;
324 }
325
David Benjamin8f2c20e2014-07-09 09:30:38 -0400326 /* Extract extensions and check it is valid. */
327 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
328 !tls1_check_duplicate_extensions(&extensions) ||
329 CBS_len(&client_hello) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700330 return 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400331 ctx->extensions = CBS_data(&extensions);
332 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700333
Adam Langleydc9b1412014-06-20 12:00:00 -0700334 return 1;
Adam Langleydc9b1412014-06-20 12:00:00 -0700335 }
336
337char
338SSL_early_callback_ctx_extension_get(const struct ssl_early_callback_ctx *ctx,
339 uint16_t extension_type,
340 const unsigned char **out_data,
341 size_t *out_len)
342 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400343 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700344
David Benjamin8f2c20e2014-07-09 09:30:38 -0400345 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
346
347 while (CBS_len(&extensions) != 0)
Adam Langleydc9b1412014-06-20 12:00:00 -0700348 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400349 uint16_t type;
350 CBS extension;
Adam Langleydc9b1412014-06-20 12:00:00 -0700351
David Benjamin8f2c20e2014-07-09 09:30:38 -0400352 /* Decode the next extension. */
353 if (!CBS_get_u16(&extensions, &type) ||
354 !CBS_get_u16_length_prefixed(&extensions, &extension))
Adam Langleydc9b1412014-06-20 12:00:00 -0700355 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -0700356
David Benjamin8f2c20e2014-07-09 09:30:38 -0400357 if (type == extension_type)
Adam Langleydc9b1412014-06-20 12:00:00 -0700358 {
David Benjamin8f2c20e2014-07-09 09:30:38 -0400359 *out_data = CBS_data(&extension);
360 *out_len = CBS_len(&extension);
Adam Langleydc9b1412014-06-20 12:00:00 -0700361 return 1;
362 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700363 }
364
365 return 0;
366 }
367
Adam Langley95c29f32014-06-20 12:00:00 -0700368
David Benjamincff64722014-08-19 19:54:46 -0400369static const int nid_list[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700370 {
371 NID_sect163k1, /* sect163k1 (1) */
372 NID_sect163r1, /* sect163r1 (2) */
373 NID_sect163r2, /* sect163r2 (3) */
374 NID_sect193r1, /* sect193r1 (4) */
375 NID_sect193r2, /* sect193r2 (5) */
376 NID_sect233k1, /* sect233k1 (6) */
377 NID_sect233r1, /* sect233r1 (7) */
378 NID_sect239k1, /* sect239k1 (8) */
379 NID_sect283k1, /* sect283k1 (9) */
380 NID_sect283r1, /* sect283r1 (10) */
381 NID_sect409k1, /* sect409k1 (11) */
382 NID_sect409r1, /* sect409r1 (12) */
383 NID_sect571k1, /* sect571k1 (13) */
384 NID_sect571r1, /* sect571r1 (14) */
385 NID_secp160k1, /* secp160k1 (15) */
386 NID_secp160r1, /* secp160r1 (16) */
387 NID_secp160r2, /* secp160r2 (17) */
388 NID_secp192k1, /* secp192k1 (18) */
389 NID_X9_62_prime192v1, /* secp192r1 (19) */
390 NID_secp224k1, /* secp224k1 (20) */
391 NID_secp224r1, /* secp224r1 (21) */
392 NID_secp256k1, /* secp256k1 (22) */
393 NID_X9_62_prime256v1, /* secp256r1 (23) */
394 NID_secp384r1, /* secp384r1 (24) */
395 NID_secp521r1, /* secp521r1 (25) */
396 NID_brainpoolP256r1, /* brainpoolP256r1 (26) */
397 NID_brainpoolP384r1, /* brainpoolP384r1 (27) */
398 NID_brainpoolP512r1 /* brainpool512r1 (28) */
399 };
400
David Benjamin072334d2014-07-13 16:24:27 -0400401static const uint8_t ecformats_default[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700402 {
403 TLSEXT_ECPOINTFORMAT_uncompressed,
Adam Langley95c29f32014-06-20 12:00:00 -0700404 };
405
David Benjamin072334d2014-07-13 16:24:27 -0400406static const uint16_t eccurves_default[] =
Adam Langley95c29f32014-06-20 12:00:00 -0700407 {
David Benjamin072334d2014-07-13 16:24:27 -0400408 23, /* secp256r1 (23) */
409 24, /* secp384r1 (24) */
410 25, /* secp521r1 (25) */
Adam Langley95c29f32014-06-20 12:00:00 -0700411 };
412
David Benjamin072334d2014-07-13 16:24:27 -0400413int tls1_ec_curve_id2nid(uint16_t curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700414 {
415 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
David Benjamin072334d2014-07-13 16:24:27 -0400416 if (curve_id < 1 || curve_id > sizeof(nid_list)/sizeof(nid_list[0]))
417 return OBJ_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700418 return nid_list[curve_id-1];
419 }
420
David Benjamin072334d2014-07-13 16:24:27 -0400421uint16_t tls1_ec_nid2curve_id(int nid)
Adam Langley95c29f32014-06-20 12:00:00 -0700422 {
David Benjamin072334d2014-07-13 16:24:27 -0400423 size_t i;
424 for (i = 0; i < sizeof(nid_list)/sizeof(nid_list[0]); i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700425 {
David Benjamin072334d2014-07-13 16:24:27 -0400426 /* nid_list[i] stores the NID corresponding to curve ID i+1. */
427 if (nid == nid_list[i])
428 return i + 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700429 }
David Benjamin072334d2014-07-13 16:24:27 -0400430 /* Use 0 for non-existent curve ID. Note: this assumes that curve ID 0
431 * will never be allocated. */
432 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700433 }
David Benjamin072334d2014-07-13 16:24:27 -0400434
David Benjamin42e9a772014-09-02 23:18:44 -0400435/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len|
436 * to the list of allowed curve IDs. If |get_peer_curves| is non-zero,
437 * return the peer's curve list. Otherwise, return the preferred
438 * list. */
439static void tls1_get_curvelist(SSL *s, int get_peer_curves,
David Benjamin072334d2014-07-13 16:24:27 -0400440 const uint16_t **out_curve_ids, size_t *out_curve_ids_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700441 {
David Benjamin42e9a772014-09-02 23:18:44 -0400442 if (get_peer_curves)
Adam Langley95c29f32014-06-20 12:00:00 -0700443 {
David Benjamina19fc252014-10-19 00:14:36 -0400444 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
445 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
Adam Langley95c29f32014-06-20 12:00:00 -0700446 return;
447 }
Adam Langley95c29f32014-06-20 12:00:00 -0700448
David Benjamin335d10d2014-08-06 19:56:33 -0400449 *out_curve_ids = s->tlsext_ellipticcurvelist;
450 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
David Benjamin072334d2014-07-13 16:24:27 -0400451 if (!*out_curve_ids)
Adam Langley95c29f32014-06-20 12:00:00 -0700452 {
David Benjamin072334d2014-07-13 16:24:27 -0400453 *out_curve_ids = eccurves_default;
David Benjamin0eb5a2d2014-07-25 02:40:43 -0400454 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
Adam Langley95c29f32014-06-20 12:00:00 -0700455 }
456 }
David Benjamined439582014-07-14 19:13:02 -0400457
David Benjamined439582014-07-14 19:13:02 -0400458int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700459 {
David Benjamined439582014-07-14 19:13:02 -0400460 uint8_t curve_type;
461 uint16_t curve_id;
David Benjamin072334d2014-07-13 16:24:27 -0400462 const uint16_t *curves;
463 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400464
465 /* Only support named curves. */
466 if (!CBS_get_u8(cbs, &curve_type) ||
467 curve_type != NAMED_CURVE_TYPE ||
468 !CBS_get_u16(cbs, &curve_id))
Adam Langley95c29f32014-06-20 12:00:00 -0700469 return 0;
David Benjamined439582014-07-14 19:13:02 -0400470
David Benjamin072334d2014-07-13 16:24:27 -0400471 tls1_get_curvelist(s, 0, &curves, &curves_len);
472 for (i = 0; i < curves_len; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700473 {
David Benjamin072334d2014-07-13 16:24:27 -0400474 if (curve_id == curves[i])
David Benjamined439582014-07-14 19:13:02 -0400475 {
476 *out_curve_id = curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700477 return 1;
David Benjamined439582014-07-14 19:13:02 -0400478 }
Adam Langley95c29f32014-06-20 12:00:00 -0700479 }
480 return 0;
481 }
482
David Benjamin072334d2014-07-13 16:24:27 -0400483int tls1_get_shared_curve(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -0700484 {
David Benjamin072334d2014-07-13 16:24:27 -0400485 const uint16_t *pref, *supp;
Adam Langley95c29f32014-06-20 12:00:00 -0700486 size_t preflen, supplen, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400487
Adam Langley95c29f32014-06-20 12:00:00 -0700488 /* Can't do anything on client side */
489 if (s->server == 0)
David Benjamin072334d2014-07-13 16:24:27 -0400490 return NID_undef;
491
David Benjamin335d10d2014-08-06 19:56:33 -0400492 /* Return first preference shared curve */
Adam Langley95c29f32014-06-20 12:00:00 -0700493 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
494 &supp, &supplen);
495 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
496 &pref, &preflen);
David Benjamin072334d2014-07-13 16:24:27 -0400497 for (i = 0; i < preflen; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700498 {
David Benjamin072334d2014-07-13 16:24:27 -0400499 for (j = 0; j < supplen; j++)
Adam Langley95c29f32014-06-20 12:00:00 -0700500 {
David Benjamin072334d2014-07-13 16:24:27 -0400501 if (pref[i] == supp[j])
502 return tls1_ec_curve_id2nid(pref[i]);
Adam Langley95c29f32014-06-20 12:00:00 -0700503 }
504 }
David Benjamin072334d2014-07-13 16:24:27 -0400505 return NID_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700506 }
507
David Benjamin072334d2014-07-13 16:24:27 -0400508/* NOTE: tls1_ec_curve_id2nid and tls1_set_curves assume that
509 *
510 * (a) 0 is not a valid curve ID.
511 *
512 * (b) The largest curve ID is 31.
513 *
514 * Those implementations must be revised before adding support for curve IDs
515 * that break these assumptions. */
516OPENSSL_COMPILE_ASSERT(
517 (sizeof(nid_list) / sizeof(nid_list[0])) < 32, small_curve_ids);
518
519int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
520 const int *curves, size_t ncurves)
Adam Langley95c29f32014-06-20 12:00:00 -0700521 {
David Benjamin072334d2014-07-13 16:24:27 -0400522 uint16_t *curve_ids;
Adam Langley95c29f32014-06-20 12:00:00 -0700523 size_t i;
524 /* Bitmap of curves included to detect duplicates: only works
525 * while curve ids < 32
526 */
David Benjamin072334d2014-07-13 16:24:27 -0400527 uint32_t dup_list = 0;
528 curve_ids = (uint16_t*)OPENSSL_malloc(ncurves * sizeof(uint16_t));
529 if (!curve_ids)
Adam Langley95c29f32014-06-20 12:00:00 -0700530 return 0;
David Benjamin072334d2014-07-13 16:24:27 -0400531 for (i = 0; i < ncurves; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700532 {
David Benjamin072334d2014-07-13 16:24:27 -0400533 uint32_t idmask;
534 uint16_t id;
Adam Langley95c29f32014-06-20 12:00:00 -0700535 id = tls1_ec_nid2curve_id(curves[i]);
David Benjamin072334d2014-07-13 16:24:27 -0400536 idmask = ((uint32_t)1) << id;
Adam Langley95c29f32014-06-20 12:00:00 -0700537 if (!id || (dup_list & idmask))
538 {
David Benjamin072334d2014-07-13 16:24:27 -0400539 OPENSSL_free(curve_ids);
Adam Langley95c29f32014-06-20 12:00:00 -0700540 return 0;
541 }
542 dup_list |= idmask;
David Benjamin072334d2014-07-13 16:24:27 -0400543 curve_ids[i] = id;
Adam Langley95c29f32014-06-20 12:00:00 -0700544 }
David Benjamin072334d2014-07-13 16:24:27 -0400545 if (*out_curve_ids)
546 OPENSSL_free(*out_curve_ids);
547 *out_curve_ids = curve_ids;
548 *out_curve_ids_len = ncurves;
Adam Langley95c29f32014-06-20 12:00:00 -0700549 return 1;
550 }
551
David Benjamin072334d2014-07-13 16:24:27 -0400552/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
553 * TLS curve ID and point format, respectively, for |ec|. It returns one on
554 * success and zero on failure. */
555static 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 -0700556 {
Adam Langley95c29f32014-06-20 12:00:00 -0700557 int nid;
David Benjamin072334d2014-07-13 16:24:27 -0400558 uint16_t id;
Adam Langley95c29f32014-06-20 12:00:00 -0700559 const EC_GROUP *grp;
560 if (!ec)
561 return 0;
562
Adam Langley95c29f32014-06-20 12:00:00 -0700563 grp = EC_KEY_get0_group(ec);
564 if (!grp)
565 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700566
567 /* Determine curve ID */
David Benjamin072334d2014-07-13 16:24:27 -0400568 nid = EC_GROUP_get_curve_name(grp);
569 id = tls1_ec_nid2curve_id(nid);
570 if (!id)
571 return 0;
572
573 /* Set the named curve ID. Arbitrary explicit curves are not
574 * supported. */
575 *out_curve_id = id;
576
577 if (out_comp_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700578 {
579 if (EC_KEY_get0_public_key(ec) == NULL)
580 return 0;
581 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED)
David Benjamin072334d2014-07-13 16:24:27 -0400582 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
Adam Langley95c29f32014-06-20 12:00:00 -0700583 else
David Benjamin072334d2014-07-13 16:24:27 -0400584 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
Adam Langley95c29f32014-06-20 12:00:00 -0700585 }
586 return 1;
587 }
David Benjamin072334d2014-07-13 16:24:27 -0400588
David Benjamin42e9a772014-09-02 23:18:44 -0400589/* tls1_check_point_format returns one if |comp_id| is consistent with the
590 * peer's point format preferences. */
591static int tls1_check_point_format(SSL *s, uint8_t comp_id)
592 {
David Benjamina19fc252014-10-19 00:14:36 -0400593 uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
594 size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
David Benjamin42e9a772014-09-02 23:18:44 -0400595 size_t i;
596
597 /* If point formats extension present check it, otherwise everything
598 * is supported (see RFC4492). */
599 if (p == NULL)
600 return 1;
601
602 for (i = 0; i < plen; i++)
603 {
604 if (comp_id == p[i])
605 return 1;
606 }
607 return 0;
608 }
609
610/* tls1_check_curve_id returns one if |curve_id| is consistent with both our and
611 * the peer's curve preferences. Note: if called as the client, only our
612 * preferences are checked; the peer (the server) does not send preferences. */
613static int tls1_check_curve_id(SSL *s, uint16_t curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700614 {
David Benjamin072334d2014-07-13 16:24:27 -0400615 const uint16_t *curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400616 size_t curves_len, i, j;
617
618 /* Check against our list, then the peer's list. */
Adam Langley95c29f32014-06-20 12:00:00 -0700619 for (j = 0; j <= 1; j++)
620 {
David Benjamin072334d2014-07-13 16:24:27 -0400621 tls1_get_curvelist(s, j, &curves, &curves_len);
622 for (i = 0; i < curves_len; i++)
Adam Langley95c29f32014-06-20 12:00:00 -0700623 {
David Benjamin42e9a772014-09-02 23:18:44 -0400624 if (curves[i] == curve_id)
Adam Langley95c29f32014-06-20 12:00:00 -0700625 break;
626 }
David Benjamin072334d2014-07-13 16:24:27 -0400627 if (i == curves_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700628 return 0;
David Benjamin42e9a772014-09-02 23:18:44 -0400629 /* Servers do not present a preference list so, if we are a
630 * client, only check our list. */
Adam Langley95c29f32014-06-20 12:00:00 -0700631 if (!s->server)
632 return 1;
633 }
634 return 1;
635 }
636
637static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
638 size_t *pformatslen)
639 {
640 /* If we have a custom point format list use it otherwise
641 * use default */
642 if (s->tlsext_ecpointformatlist)
643 {
644 *pformats = s->tlsext_ecpointformatlist;
645 *pformatslen = s->tlsext_ecpointformatlist_length;
646 }
647 else
648 {
649 *pformats = ecformats_default;
David Benjamin335d10d2014-08-06 19:56:33 -0400650 *pformatslen = sizeof(ecformats_default);
Adam Langley95c29f32014-06-20 12:00:00 -0700651 }
652 }
653
654/* Check cert parameters compatible with extensions: currently just checks
655 * EC certificates have compatible curves and compression.
656 */
657static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
658 {
David Benjamin072334d2014-07-13 16:24:27 -0400659 uint8_t comp_id;
660 uint16_t curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700661 EVP_PKEY *pkey;
662 int rv;
663 pkey = X509_get_pubkey(x);
664 if (!pkey)
665 return 0;
666 /* If not EC nothing to do */
667 if (pkey->type != EVP_PKEY_EC)
668 {
669 EVP_PKEY_free(pkey);
670 return 1;
671 }
David Benjamin072334d2014-07-13 16:24:27 -0400672 rv = tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec);
Adam Langley95c29f32014-06-20 12:00:00 -0700673 EVP_PKEY_free(pkey);
674 if (!rv)
675 return 0;
676 /* Can't check curve_id for client certs as we don't have a
David Benjamin42e9a772014-09-02 23:18:44 -0400677 * supported curves extension. */
678 if (s->server && !tls1_check_curve_id(s, curve_id))
679 return 0;
680 return tls1_check_point_format(s, comp_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700681 }
David Benjamin42e9a772014-09-02 23:18:44 -0400682
Adam Langley95c29f32014-06-20 12:00:00 -0700683/* Check EC temporary key is compatible with client extensions */
David Benjamin42e9a772014-09-02 23:18:44 -0400684int tls1_check_ec_tmp_key(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -0700685 {
David Benjamin072334d2014-07-13 16:24:27 -0400686 uint16_t curve_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700687 EC_KEY *ec = s->cert->ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -0700688 if (s->cert->ecdh_tmp_auto)
689 {
690 /* Need a shared curve */
David Benjamin072334d2014-07-13 16:24:27 -0400691 return tls1_get_shared_curve(s) != NID_undef;
Adam Langley95c29f32014-06-20 12:00:00 -0700692 }
693 if (!ec)
694 {
695 if (s->cert->ecdh_tmp_cb)
696 return 1;
697 else
698 return 0;
699 }
David Benjamin42e9a772014-09-02 23:18:44 -0400700 return tls1_curve_params_from_ec_key(&curve_id, NULL, ec) &&
701 tls1_check_curve_id(s, curve_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700702 }
703
Adam Langley95c29f32014-06-20 12:00:00 -0700704
Adam Langley95c29f32014-06-20 12:00:00 -0700705
706/* List of supported signature algorithms and hashes. Should make this
707 * customisable at some point, for now include everything we support.
708 */
709
Adam Langley95c29f32014-06-20 12:00:00 -0700710#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700711
Adam Langley95c29f32014-06-20 12:00:00 -0700712#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700713
714#define tlsext_sigalg(md) \
715 tlsext_sigalg_rsa(md) \
Adam Langley95c29f32014-06-20 12:00:00 -0700716 tlsext_sigalg_ecdsa(md)
717
David Benjamincff64722014-08-19 19:54:46 -0400718static const uint8_t tls12_sigalgs[] = {
Adam Langley95c29f32014-06-20 12:00:00 -0700719 tlsext_sigalg(TLSEXT_hash_sha512)
720 tlsext_sigalg(TLSEXT_hash_sha384)
Adam Langley95c29f32014-06-20 12:00:00 -0700721 tlsext_sigalg(TLSEXT_hash_sha256)
722 tlsext_sigalg(TLSEXT_hash_sha224)
Adam Langley95c29f32014-06-20 12:00:00 -0700723 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700724};
Adam Langley95c29f32014-06-20 12:00:00 -0700725size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
726 {
Adam Langley95c29f32014-06-20 12:00:00 -0700727 /* If server use client authentication sigalgs if not NULL */
728 if (s->server && s->cert->client_sigalgs)
729 {
730 *psigs = s->cert->client_sigalgs;
731 return s->cert->client_sigalgslen;
732 }
733 else if (s->cert->conf_sigalgs)
734 {
735 *psigs = s->cert->conf_sigalgs;
736 return s->cert->conf_sigalgslen;
737 }
738 else
739 {
740 *psigs = tls12_sigalgs;
741 return sizeof(tls12_sigalgs);
742 }
743 }
David Benjamin05da6e12014-07-12 20:42:55 -0400744
745/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of
746 * |cbs|. It checks it is consistent with |s|'s sent supported
747 * signature algorithms and, if so, writes the relevant digest into
748 * |*out_md| and returns 1. Otherwise it returns 0 and writes an alert
749 * into |*out_alert|.
Adam Langley95c29f32014-06-20 12:00:00 -0700750 */
David Benjamin05da6e12014-07-12 20:42:55 -0400751int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert,
752 SSL *s, CBS *cbs, EVP_PKEY *pkey)
Adam Langley95c29f32014-06-20 12:00:00 -0700753 {
754 const unsigned char *sent_sigs;
755 size_t sent_sigslen, i;
756 int sigalg = tls12_get_sigid(pkey);
David Benjamin05da6e12014-07-12 20:42:55 -0400757 uint8_t hash, signature;
Adam Langley95c29f32014-06-20 12:00:00 -0700758 /* Should never happen */
759 if (sigalg == -1)
David Benjamin05da6e12014-07-12 20:42:55 -0400760 {
761 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
762 *out_alert = SSL_AD_INTERNAL_ERROR;
763 return 0;
764 }
765 if (!CBS_get_u8(cbs, &hash) ||
766 !CBS_get_u8(cbs, &signature))
767 {
768 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
769 *out_alert = SSL_AD_DECODE_ERROR;
770 return 0;
771 }
Adam Langley95c29f32014-06-20 12:00:00 -0700772 /* Check key type is consistent with signature */
David Benjamin05da6e12014-07-12 20:42:55 -0400773 if (sigalg != signature)
Adam Langley95c29f32014-06-20 12:00:00 -0700774 {
775 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin05da6e12014-07-12 20:42:55 -0400776 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700777 return 0;
778 }
Adam Langley95c29f32014-06-20 12:00:00 -0700779 if (pkey->type == EVP_PKEY_EC)
780 {
David Benjamin072334d2014-07-13 16:24:27 -0400781 uint16_t curve_id;
782 uint8_t comp_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700783 /* Check compression and curve matches extensions */
David Benjamin072334d2014-07-13 16:24:27 -0400784 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec))
David Benjamin05da6e12014-07-12 20:42:55 -0400785 {
786 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -0700787 return 0;
David Benjamin05da6e12014-07-12 20:42:55 -0400788 }
David Benjamin42e9a772014-09-02 23:18:44 -0400789 if (s->server)
Adam Langley95c29f32014-06-20 12:00:00 -0700790 {
David Benjamin42e9a772014-09-02 23:18:44 -0400791 if (!tls1_check_curve_id(s, curve_id) ||
792 !tls1_check_point_format(s, comp_id))
793 {
794 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
795 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
796 return 0;
797 }
Adam Langley95c29f32014-06-20 12:00:00 -0700798 }
David Benjamin05da6e12014-07-12 20:42:55 -0400799 }
Adam Langley95c29f32014-06-20 12:00:00 -0700800
801 /* Check signature matches a type we sent */
802 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
803 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
804 {
David Benjamin05da6e12014-07-12 20:42:55 -0400805 if (hash == sent_sigs[0] && signature == sent_sigs[1])
Adam Langley95c29f32014-06-20 12:00:00 -0700806 break;
807 }
808 /* Allow fallback to SHA1 if not strict mode */
David Benjamin05da6e12014-07-12 20:42:55 -0400809 if (i == sent_sigslen && (hash != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
Adam Langley95c29f32014-06-20 12:00:00 -0700810 {
811 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
David Benjamin05da6e12014-07-12 20:42:55 -0400812 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700813 return 0;
814 }
David Benjamin05da6e12014-07-12 20:42:55 -0400815 *out_md = tls12_get_hash(hash);
816 if (*out_md == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -0700817 {
818 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
David Benjamin05da6e12014-07-12 20:42:55 -0400819 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -0700820 return 0;
821 }
822 /* Store the digest used so applications can retrieve it if they
823 * wish.
824 */
825 if (s->session && s->session->sess_cert)
David Benjamin05da6e12014-07-12 20:42:55 -0400826 s->session->sess_cert->peer_key->digest = *out_md;
Adam Langley95c29f32014-06-20 12:00:00 -0700827 return 1;
828 }
829/* Get a mask of disabled algorithms: an algorithm is disabled
830 * if it isn't supported or doesn't appear in supported signature
831 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
832 * session and not global settings.
833 *
834 */
835void ssl_set_client_disabled(SSL *s)
836 {
837 CERT *c = s->cert;
838 const unsigned char *sigalgs;
839 size_t i, sigalgslen;
David Benjaminef2116d2014-08-19 20:21:56 -0400840 int have_rsa = 0, have_ecdsa = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700841 c->mask_a = 0;
842 c->mask_k = 0;
843 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
844 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
845 c->mask_ssl = SSL_TLSV1_2;
846 else
847 c->mask_ssl = 0;
848 /* Now go through all signature algorithms seeing if we support
849 * any for RSA, DSA, ECDSA. Do this for all versions not just
850 * TLS 1.2.
851 */
852 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
853 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
854 {
855 switch(sigalgs[1])
856 {
Adam Langley95c29f32014-06-20 12:00:00 -0700857 case TLSEXT_signature_rsa:
858 have_rsa = 1;
859 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700860 case TLSEXT_signature_ecdsa:
861 have_ecdsa = 1;
862 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700863 }
864 }
David Benjamin0da0e182014-08-19 16:20:28 -0400865 /* Disable auth if we don't include any appropriate signature
866 * algorithms.
Adam Langley95c29f32014-06-20 12:00:00 -0700867 */
868 if (!have_rsa)
869 {
870 c->mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700871 }
Adam Langley95c29f32014-06-20 12:00:00 -0700872 if (!have_ecdsa)
873 {
874 c->mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -0700875 }
Adam Langley95c29f32014-06-20 12:00:00 -0700876 /* with PSK there must be client callback set */
877 if (!s->psk_client_callback)
878 {
879 c->mask_a |= SSL_aPSK;
880 c->mask_k |= SSL_kPSK;
881 }
Adam Langley95c29f32014-06-20 12:00:00 -0700882 c->valid = 1;
883 }
884
Adam Langleyb0c235e2014-06-20 12:00:00 -0700885/* header_len is the length of the ClientHello header written so far, used to
886 * compute padding. It does not include the record header. Pass 0 if no padding
887 * is to be done. */
888unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, size_t header_len)
Adam Langley95c29f32014-06-20 12:00:00 -0700889 {
890 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -0700891 unsigned char *ret = buf;
892 unsigned char *orig = buf;
Adam Langley95c29f32014-06-20 12:00:00 -0700893 /* See if we support any ECC ciphersuites */
894 int using_ecc = 0;
895 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
896 {
David Benjaminfb3ff2c2014-09-30 21:00:38 -0400897 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -0700898 unsigned long alg_k, alg_a;
899 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
900
901 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
902 {
David Benjamin6f260012014-08-15 13:49:12 -0400903 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700904
905 alg_k = c->algorithm_mkey;
906 alg_a = c->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -0400907 if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA))
Adam Langley95c29f32014-06-20 12:00:00 -0700908 {
909 using_ecc = 1;
910 break;
911 }
912 }
913 }
Adam Langley95c29f32014-06-20 12:00:00 -0700914
915 /* don't add extensions for SSLv3 unless doing secure renegotiation */
916 if (s->client_version == SSL3_VERSION
917 && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -0700918 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -0700919
920 ret+=2;
921
922 if (ret>=limit) return NULL; /* this really never occurs, but ... */
923
924 if (s->tlsext_hostname != NULL)
925 {
926 /* Add TLS extension servername to the Client Hello message */
927 unsigned long size_str;
928 long lenmax;
929
930 /* check for enough space.
931 4 for the servername type and entension length
932 2 for servernamelist length
933 1 for the hostname type
934 2 for hostname length
935 + hostname length
936 */
937
938 if ((lenmax = limit - ret - 9) < 0
939 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
940 return NULL;
941
942 /* extension type and length */
943 s2n(TLSEXT_TYPE_server_name,ret);
944 s2n(size_str+5,ret);
945
946 /* length of servername list */
947 s2n(size_str+3,ret);
948
949 /* hostname type, length and hostname */
950 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
951 s2n(size_str,ret);
952 memcpy(ret, s->tlsext_hostname, size_str);
953 ret+=size_str;
954 }
955
956 /* Add RI if renegotiating */
957 if (s->renegotiate)
958 {
959 int el;
960
961 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
962 {
963 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
964 return NULL;
965 }
966
Adam Langleyb0c235e2014-06-20 12:00:00 -0700967 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700968
969 s2n(TLSEXT_TYPE_renegotiate,ret);
970 s2n(el,ret);
971
972 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
973 {
974 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
975 return NULL;
976 }
977
978 ret += el;
979 }
980
Adam Langley95c29f32014-06-20 12:00:00 -0700981 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
982 {
983 int ticklen;
984 if (!s->new_session && s->session && s->session->tlsext_tick)
985 ticklen = s->session->tlsext_ticklen;
986 else if (s->session && s->tlsext_session_ticket &&
987 s->tlsext_session_ticket->data)
988 {
David Benjamin072c9532014-07-26 11:44:25 -0400989 s->session->tlsext_tick = BUF_memdup(
990 s->tlsext_session_ticket->data,
991 s->tlsext_session_ticket->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700992 if (!s->session->tlsext_tick)
993 return NULL;
David Benjamin072c9532014-07-26 11:44:25 -0400994 ticklen = s->tlsext_session_ticket->length;
Adam Langley95c29f32014-06-20 12:00:00 -0700995 s->session->tlsext_ticklen = ticklen;
996 }
997 else
998 ticklen = 0;
999 if (ticklen == 0 && s->tlsext_session_ticket &&
1000 s->tlsext_session_ticket->data == NULL)
1001 goto skip_ext;
1002 /* Check for enough room 2 for extension type, 2 for len
1003 * rest for ticket
1004 */
1005 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1006 s2n(TLSEXT_TYPE_session_ticket,ret);
1007 s2n(ticklen,ret);
1008 if (ticklen)
1009 {
1010 memcpy(ret, s->session->tlsext_tick, ticklen);
1011 ret += ticklen;
1012 }
1013 }
1014 skip_ext:
1015
1016 if (SSL_USE_SIGALGS(s))
1017 {
1018 size_t salglen;
1019 const unsigned char *salg;
1020 salglen = tls12_get_psigalgs(s, &salg);
1021 if ((size_t)(limit - ret) < salglen + 6)
1022 return NULL;
1023 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1024 s2n(salglen + 2, ret);
1025 s2n(salglen, ret);
1026 memcpy(ret, salg, salglen);
1027 ret += salglen;
1028 }
1029
David Benjamin6c7aed02014-08-27 16:42:38 -04001030 if (s->ocsp_stapling_enabled)
Adam Langley95c29f32014-06-20 12:00:00 -07001031 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001032 /* The status_request extension is excessively extensible at
1033 * every layer. On the client, only support requesting OCSP
1034 * responses with an empty responder_id_list and no
1035 * extensions. */
1036 if (limit - ret - 4 - 1 - 2 - 2 < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001037
Adam Langley95c29f32014-06-20 12:00:00 -07001038 s2n(TLSEXT_TYPE_status_request, ret);
David Benjamin6c7aed02014-08-27 16:42:38 -04001039 s2n(1 + 2 + 2, ret);
1040 /* status_type */
Adam Langley95c29f32014-06-20 12:00:00 -07001041 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
David Benjamin6c7aed02014-08-27 16:42:38 -04001042 /* responder_id_list - empty */
1043 s2n(0, ret);
1044 /* request_extensions - empty */
1045 s2n(0, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001046 }
Adam Langley95c29f32014-06-20 12:00:00 -07001047
Adam Langley95c29f32014-06-20 12:00:00 -07001048 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1049 {
1050 /* The client advertises an emtpy extension to indicate its
1051 * support for Next Protocol Negotiation */
1052 if (limit - ret - 4 < 0)
1053 return NULL;
1054 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1055 s2n(0,ret);
1056 }
Adam Langley95c29f32014-06-20 12:00:00 -07001057
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001058 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len)
1059 {
1060 /* The client advertises an empty extension to indicate its support for
1061 * certificate timestamps. */
1062 if (limit - ret - 4 < 0)
1063 return NULL;
1064 s2n(TLSEXT_TYPE_certificate_timestamp,ret);
1065 s2n(0,ret);
1066 }
1067
Adam Langley95c29f32014-06-20 12:00:00 -07001068 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1069 {
1070 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1071 return NULL;
1072 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1073 s2n(2 + s->alpn_client_proto_list_len,ret);
1074 s2n(s->alpn_client_proto_list_len,ret);
1075 memcpy(ret, s->alpn_client_proto_list,
1076 s->alpn_client_proto_list_len);
1077 ret += s->alpn_client_proto_list_len;
1078 }
1079
Adam Langley1258b6a2014-06-20 12:00:00 -07001080 if (s->tlsext_channel_id_enabled)
1081 {
1082 /* The client advertises an emtpy extension to indicate its
1083 * support for Channel ID. */
1084 if (limit - ret - 4 < 0)
1085 return NULL;
1086 if (s->ctx->tlsext_channel_id_enabled_new)
1087 s2n(TLSEXT_TYPE_channel_id_new,ret);
1088 else
1089 s2n(TLSEXT_TYPE_channel_id,ret);
1090 s2n(0,ret);
1091 }
1092
Adam Langley95c29f32014-06-20 12:00:00 -07001093 if(SSL_get_srtp_profiles(s))
1094 {
1095 int el;
1096
1097 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1098
Adam Langleyb0c235e2014-06-20 12:00:00 -07001099 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001100
1101 s2n(TLSEXT_TYPE_use_srtp,ret);
1102 s2n(el,ret);
1103
David Benjamin120a6742014-08-30 14:54:37 -04001104 if(!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001105 {
1106 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1107 return NULL;
1108 }
1109 ret += el;
1110 }
1111
Adam Langleyc3174b72014-06-20 12:00:00 -07001112 if (using_ecc)
1113 {
1114 /* Add TLS extension ECPointFormats to the ClientHello message */
1115 long lenmax;
David Benjamin072334d2014-07-13 16:24:27 -04001116 const uint8_t *formats;
1117 const uint16_t *curves;
1118 size_t formats_len, curves_len, i;
Adam Langleyc3174b72014-06-20 12:00:00 -07001119
David Benjamin072334d2014-07-13 16:24:27 -04001120 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001121
1122 if ((lenmax = limit - ret - 5) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001123 if (formats_len > (size_t)lenmax) return NULL;
1124 if (formats_len > 255)
Adam Langleyc3174b72014-06-20 12:00:00 -07001125 {
1126 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1127 return NULL;
1128 }
1129
1130 s2n(TLSEXT_TYPE_ec_point_formats,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001131 s2n(formats_len + 1,ret);
1132 *(ret++) = (unsigned char)formats_len;
1133 memcpy(ret, formats, formats_len);
1134 ret+=formats_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001135
1136 /* Add TLS extension EllipticCurves to the ClientHello message */
David Benjamin072334d2014-07-13 16:24:27 -04001137 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langleyc3174b72014-06-20 12:00:00 -07001138
1139 if ((lenmax = limit - ret - 6) < 0) return NULL;
David Benjamin072334d2014-07-13 16:24:27 -04001140 if ((curves_len * 2) > (size_t)lenmax) return NULL;
1141 if ((curves_len * 2) > 65532)
Adam Langleyc3174b72014-06-20 12:00:00 -07001142 {
1143 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1144 return NULL;
1145 }
1146
1147 s2n(TLSEXT_TYPE_elliptic_curves,ret);
David Benjamin072334d2014-07-13 16:24:27 -04001148 s2n((curves_len * 2) + 2, ret);
Adam Langleyc3174b72014-06-20 12:00:00 -07001149
1150 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1151 * elliptic_curve_list, but the examples use two bytes.
1152 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1153 * resolves this to two bytes.
1154 */
David Benjamin072334d2014-07-13 16:24:27 -04001155 s2n(curves_len * 2, ret);
1156 for (i = 0; i < curves_len; i++)
1157 {
1158 s2n(curves[i], ret);
1159 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001160 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001161
Adam Langley95c29f32014-06-20 12:00:00 -07001162#ifdef TLSEXT_TYPE_padding
1163 /* Add padding to workaround bugs in F5 terminators.
Adam Langleyb0c235e2014-06-20 12:00:00 -07001164 * See https://tools.ietf.org/html/draft-agl-tls-padding-03
Adam Langley95c29f32014-06-20 12:00:00 -07001165 *
1166 * NB: because this code works out the length of all existing
Adam Langleyb0c235e2014-06-20 12:00:00 -07001167 * extensions it MUST always appear last. */
1168 if (header_len > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001169 {
Adam Langleyb0c235e2014-06-20 12:00:00 -07001170 header_len += ret - orig;
1171 if (header_len > 0xff && header_len < 0x200)
1172 {
1173 size_t padding_len = 0x200 - header_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001174 /* Extensions take at least four bytes to encode. Always
1175 * include least one byte of data if including the
1176 * extension. WebSphere Application Server 7.0 is
1177 * intolerant to the last extension being zero-length. */
1178 if (padding_len >= 4 + 1)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001179 padding_len -= 4;
1180 else
Adam Langleyc3174b72014-06-20 12:00:00 -07001181 padding_len = 1;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001182 if (limit - ret - 4 - (long)padding_len < 0)
1183 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001184
Adam Langleyb0c235e2014-06-20 12:00:00 -07001185 s2n(TLSEXT_TYPE_padding, ret);
1186 s2n(padding_len, ret);
1187 memset(ret, 0, padding_len);
1188 ret += padding_len;
1189 }
Adam Langley95c29f32014-06-20 12:00:00 -07001190 }
1191#endif
1192
Adam Langleyb0c235e2014-06-20 12:00:00 -07001193 if ((extdatalen = ret-orig-2)== 0)
1194 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001195
Adam Langleyb0c235e2014-06-20 12:00:00 -07001196 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001197 return ret;
1198 }
1199
Adam Langleyb0c235e2014-06-20 12:00:00 -07001200unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit)
Adam Langley95c29f32014-06-20 12:00:00 -07001201 {
1202 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001203 unsigned char *orig = buf;
1204 unsigned char *ret = buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001205 int next_proto_neg_seen;
Adam Langley95c29f32014-06-20 12:00:00 -07001206 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1207 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin0da0e182014-08-19 16:20:28 -04001208 int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA);
David Benjamina19fc252014-10-19 00:14:36 -04001209 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001210 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1211 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001212 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001213
1214 ret+=2;
1215 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1216
Adam Langleyed8270a2014-09-02 13:52:56 -07001217 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001218 {
1219 if ((long)(limit - ret - 4) < 0) return NULL;
1220
1221 s2n(TLSEXT_TYPE_server_name,ret);
1222 s2n(0,ret);
1223 }
1224
1225 if(s->s3->send_connection_binding)
1226 {
1227 int el;
1228
1229 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1230 {
1231 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1232 return NULL;
1233 }
1234
Adam Langleyb0c235e2014-06-20 12:00:00 -07001235 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001236
1237 s2n(TLSEXT_TYPE_renegotiate,ret);
1238 s2n(el,ret);
1239
1240 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1241 {
1242 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1243 return NULL;
1244 }
1245
1246 ret += el;
1247 }
1248
Adam Langley95c29f32014-06-20 12:00:00 -07001249 if (using_ecc)
1250 {
1251 const unsigned char *plist;
1252 size_t plistlen;
1253 /* Add TLS extension ECPointFormats to the ServerHello message */
1254 long lenmax;
1255
1256 tls1_get_formatlist(s, &plist, &plistlen);
1257
1258 if ((lenmax = limit - ret - 5) < 0) return NULL;
1259 if (plistlen > (size_t)lenmax) return NULL;
1260 if (plistlen > 255)
1261 {
1262 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1263 return NULL;
1264 }
1265
1266 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1267 s2n(plistlen + 1,ret);
1268 *(ret++) = (unsigned char) plistlen;
1269 memcpy(ret, plist, plistlen);
1270 ret+=plistlen;
1271
1272 }
1273 /* Currently the server should not respond with a SupportedCurves extension */
Adam Langley95c29f32014-06-20 12:00:00 -07001274
1275 if (s->tlsext_ticket_expected
1276 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1277 {
1278 if ((long)(limit - ret - 4) < 0) return NULL;
1279 s2n(TLSEXT_TYPE_session_ticket,ret);
1280 s2n(0,ret);
1281 }
1282
David Benjamin6c7aed02014-08-27 16:42:38 -04001283 if (s->s3->tmp.certificate_status_expected)
Adam Langley95c29f32014-06-20 12:00:00 -07001284 {
1285 if ((long)(limit - ret - 4) < 0) return NULL;
1286 s2n(TLSEXT_TYPE_status_request,ret);
1287 s2n(0,ret);
1288 }
1289
Adam Langley95c29f32014-06-20 12:00:00 -07001290 if(s->srtp_profile)
1291 {
1292 int el;
1293
1294 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1295
Adam Langleyb0c235e2014-06-20 12:00:00 -07001296 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001297
1298 s2n(TLSEXT_TYPE_use_srtp,ret);
1299 s2n(el,ret);
1300
David Benjamin120a6742014-08-30 14:54:37 -04001301 if(!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
Adam Langley95c29f32014-06-20 12:00:00 -07001302 {
1303 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1304 return NULL;
1305 }
1306 ret+=el;
1307 }
1308
Adam Langley95c29f32014-06-20 12:00:00 -07001309 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1310 s->s3->next_proto_neg_seen = 0;
1311 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1312 {
1313 const unsigned char *npa;
1314 unsigned int npalen;
1315 int r;
1316
1317 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1318 if (r == SSL_TLSEXT_ERR_OK)
1319 {
1320 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1321 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1322 s2n(npalen,ret);
1323 memcpy(ret, npa, npalen);
1324 ret += npalen;
1325 s->s3->next_proto_neg_seen = 1;
1326 }
1327 }
Adam Langley95c29f32014-06-20 12:00:00 -07001328
Adam Langley95c29f32014-06-20 12:00:00 -07001329 if (s->s3->alpn_selected)
1330 {
David Benjamin03973092014-06-24 23:27:17 -04001331 const uint8_t *selected = s->s3->alpn_selected;
1332 size_t len = s->s3->alpn_selected_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001333
1334 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1335 return NULL;
1336 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1337 s2n(3 + len,ret);
1338 s2n(1 + len,ret);
1339 *ret++ = len;
1340 memcpy(ret, selected, len);
1341 ret += len;
1342 }
1343
Adam Langley1258b6a2014-06-20 12:00:00 -07001344 /* If the client advertised support for Channel ID, and we have it
1345 * enabled, then we want to echo it back. */
1346 if (s->s3->tlsext_channel_id_valid)
1347 {
1348 if (limit - ret - 4 < 0)
1349 return NULL;
1350 if (s->s3->tlsext_channel_id_new)
1351 s2n(TLSEXT_TYPE_channel_id_new,ret);
1352 else
1353 s2n(TLSEXT_TYPE_channel_id,ret);
1354 s2n(0,ret);
1355 }
1356
Adam Langleyb0c235e2014-06-20 12:00:00 -07001357 if ((extdatalen = ret-orig-2) == 0)
1358 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001359
Adam Langleyb0c235e2014-06-20 12:00:00 -07001360 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001361 return ret;
1362 }
1363
Adam Langley95c29f32014-06-20 12:00:00 -07001364/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1365 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001366 * cbs: the contents of the extension, not including the type and length.
1367 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001368 * return.
1369 *
David Benjamindc72ff72014-06-25 12:36:10 -04001370 * returns: 1 on success. */
1371static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001372 {
Adam Langleyded93582014-07-31 15:23:51 -07001373 CBS protocol_name_list, protocol_name_list_copy;
Adam Langley95c29f32014-06-20 12:00:00 -07001374 const unsigned char *selected;
1375 unsigned char selected_len;
1376 int r;
1377
1378 if (s->ctx->alpn_select_cb == NULL)
David Benjamindc72ff72014-06-25 12:36:10 -04001379 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001380
David Benjamindc72ff72014-06-25 12:36:10 -04001381 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1382 CBS_len(cbs) != 0 ||
1383 CBS_len(&protocol_name_list) < 2)
Adam Langley95c29f32014-06-20 12:00:00 -07001384 goto parse_error;
1385
David Benjamindc72ff72014-06-25 12:36:10 -04001386 /* Validate the protocol list. */
Adam Langleyded93582014-07-31 15:23:51 -07001387 protocol_name_list_copy = protocol_name_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001388 while (CBS_len(&protocol_name_list_copy) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001389 {
David Benjamindc72ff72014-06-25 12:36:10 -04001390 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001391
David Benjamindc72ff72014-06-25 12:36:10 -04001392 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name))
Adam Langley95c29f32014-06-20 12:00:00 -07001393 goto parse_error;
Adam Langley95c29f32014-06-20 12:00:00 -07001394 }
1395
David Benjamindc72ff72014-06-25 12:36:10 -04001396 r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1397 CBS_data(&protocol_name_list), CBS_len(&protocol_name_list),
1398 s->ctx->alpn_select_cb_arg);
Adam Langley95c29f32014-06-20 12:00:00 -07001399 if (r == SSL_TLSEXT_ERR_OK) {
1400 if (s->s3->alpn_selected)
1401 OPENSSL_free(s->s3->alpn_selected);
David Benjamin072c9532014-07-26 11:44:25 -04001402 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001403 if (!s->s3->alpn_selected)
1404 {
David Benjamindc72ff72014-06-25 12:36:10 -04001405 *out_alert = SSL_AD_INTERNAL_ERROR;
1406 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001407 }
Adam Langley95c29f32014-06-20 12:00:00 -07001408 s->s3->alpn_selected_len = selected_len;
1409 }
David Benjamindc72ff72014-06-25 12:36:10 -04001410 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001411
1412parse_error:
David Benjamindc72ff72014-06-25 12:36:10 -04001413 *out_alert = SSL_AD_DECODE_ERROR;
1414 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001415 }
1416
David Benjamindc72ff72014-06-25 12:36:10 -04001417static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001418 {
Adam Langley95c29f32014-06-20 12:00:00 -07001419 int renegotiate_seen = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001420 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001421 size_t i;
1422
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 Langley95c29f32014-06-20 12:00:00 -07001426
Adam Langley95c29f32014-06-20 12:00:00 -07001427 if (s->s3->alpn_selected)
1428 {
1429 OPENSSL_free(s->s3->alpn_selected);
1430 s->s3->alpn_selected = NULL;
1431 }
1432
Adam Langley95c29f32014-06-20 12:00:00 -07001433 /* Clear any signature algorithms extension received */
1434 if (s->cert->peer_sigalgs)
1435 {
1436 OPENSSL_free(s->cert->peer_sigalgs);
1437 s->cert->peer_sigalgs = NULL;
1438 }
David Benjamina19fc252014-10-19 00:14:36 -04001439 /* Clear any shared signature algorithms */
Adam Langley95c29f32014-06-20 12:00:00 -07001440 if (s->cert->shared_sigalgs)
1441 {
1442 OPENSSL_free(s->cert->shared_sigalgs);
1443 s->cert->shared_sigalgs = NULL;
1444 }
1445 /* Clear certificate digests and validity flags */
1446 for (i = 0; i < SSL_PKEY_NUM; i++)
1447 {
1448 s->cert->pkeys[i].digest = NULL;
1449 s->cert->pkeys[i].valid_flags = 0;
1450 }
David Benjamina19fc252014-10-19 00:14:36 -04001451 /* Clear ECC extensions */
1452 if (s->s3->tmp.peer_ecpointformatlist != 0)
1453 {
1454 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1455 s->s3->tmp.peer_ecpointformatlist = NULL;
1456 s->s3->tmp.peer_ecpointformatlist_length = 0;
1457 }
1458 if (s->s3->tmp.peer_ellipticcurvelist != 0)
1459 {
1460 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1461 s->s3->tmp.peer_ellipticcurvelist = NULL;
1462 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1463 }
Adam Langley95c29f32014-06-20 12:00:00 -07001464
David Benjamindc72ff72014-06-25 12:36:10 -04001465 /* There may be no extensions. */
1466 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001467 {
David Benjamindc72ff72014-06-25 12:36:10 -04001468 goto ri_check;
1469 }
Adam Langley95c29f32014-06-20 12:00:00 -07001470
David Benjamin35a7a442014-07-05 00:23:20 -04001471 /* Decode the extensions block and check it is valid. */
1472 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1473 !tls1_check_duplicate_extensions(&extensions))
David Benjamindc72ff72014-06-25 12:36:10 -04001474 {
1475 *out_alert = SSL_AD_DECODE_ERROR;
1476 return 0;
1477 }
1478
David Benjamindc72ff72014-06-25 12:36:10 -04001479 while (CBS_len(&extensions) != 0)
1480 {
1481 uint16_t type;
1482 CBS extension;
1483
1484 /* Decode the next extension. */
1485 if (!CBS_get_u16(&extensions, &type) ||
1486 !CBS_get_u16_length_prefixed(&extensions, &extension))
1487 {
1488 *out_alert = SSL_AD_DECODE_ERROR;
1489 return 0;
1490 }
1491
Adam Langley95c29f32014-06-20 12:00:00 -07001492 if (s->tlsext_debug_cb)
David Benjamindc72ff72014-06-25 12:36:10 -04001493 {
1494 s->tlsext_debug_cb(s, 0, type, (unsigned char*)CBS_data(&extension),
1495 CBS_len(&extension), s->tlsext_debug_arg);
1496 }
1497
Adam Langley95c29f32014-06-20 12:00:00 -07001498/* The servername extension is treated as follows:
1499
1500 - Only the hostname type is supported with a maximum length of 255.
1501 - The servername is rejected if too long or if it contains zeros,
1502 in which case an fatal alert is generated.
1503 - The servername field is maintained together with the session cache.
1504 - When a session is resumed, the servername call back invoked in order
1505 to allow the application to position itself to the right context.
1506 - The servername is acknowledged if it is new for a session or when
1507 it is identical to a previously used for the same session.
1508 Applications can control the behaviour. They can at any time
1509 set a 'desirable' servername for a new SSL object. This can be the
1510 case for example with HTTPS when a Host: header field is received and
1511 a renegotiation is requested. In this case, a possible servername
1512 presented in the new client hello is only acknowledged if it matches
1513 the value of the Host: field.
1514 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1515 if they provide for changing an explicit servername context for the session,
1516 i.e. when the session has been established with a servername extension.
1517 - On session reconnect, the servername extension may be absent.
1518
1519*/
1520
1521 if (type == TLSEXT_TYPE_server_name)
1522 {
David Benjamindc72ff72014-06-25 12:36:10 -04001523 CBS server_name_list;
Adam Langleyed8270a2014-09-02 13:52:56 -07001524 char have_seen_host_name = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001525
1526 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1527 CBS_len(&server_name_list) < 1 ||
1528 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001529 {
David Benjamindc72ff72014-06-25 12:36:10 -04001530 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001531 return 0;
1532 }
Adam Langley95c29f32014-06-20 12:00:00 -07001533
David Benjamindc72ff72014-06-25 12:36:10 -04001534 /* Decode each ServerName in the extension. */
1535 while (CBS_len(&server_name_list) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001536 {
David Benjamindc72ff72014-06-25 12:36:10 -04001537 uint8_t name_type;
1538 CBS host_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001539
David Benjamindc72ff72014-06-25 12:36:10 -04001540 /* Decode the NameType. */
1541 if (!CBS_get_u8(&server_name_list, &name_type))
Adam Langley95c29f32014-06-20 12:00:00 -07001542 {
David Benjamindc72ff72014-06-25 12:36:10 -04001543 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001544 return 0;
1545 }
David Benjamindc72ff72014-06-25 12:36:10 -04001546
David Benjamindc72ff72014-06-25 12:36:10 -04001547 /* Only host_name is supported. */
1548 if (name_type != TLSEXT_NAMETYPE_host_name)
1549 continue;
1550
Adam Langleyed8270a2014-09-02 13:52:56 -07001551 if (have_seen_host_name)
1552 {
1553 /* The ServerNameList MUST NOT contain
1554 * more than one name of the same
1555 * name_type. */
1556 *out_alert = SSL_AD_DECODE_ERROR;
1557 return 0;
1558 }
1559
1560 have_seen_host_name = 1;
1561
1562 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1563 CBS_len(&host_name) < 1)
1564 {
1565 *out_alert = SSL_AD_DECODE_ERROR;
1566 return 0;
1567 }
1568
1569 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1570 CBS_contains_zero_byte(&host_name))
1571 {
1572 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1573 return 0;
1574 }
1575
David Benjamindc72ff72014-06-25 12:36:10 -04001576 if (!s->hit)
Adam Langley95c29f32014-06-20 12:00:00 -07001577 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001578 assert(s->session->tlsext_hostname == NULL);
David Benjamindc72ff72014-06-25 12:36:10 -04001579 if (s->session->tlsext_hostname)
Adam Langley95c29f32014-06-20 12:00:00 -07001580 {
Adam Langleyed8270a2014-09-02 13:52:56 -07001581 /* This should be impossible. */
David Benjamindc72ff72014-06-25 12:36:10 -04001582 *out_alert = SSL_AD_DECODE_ERROR;
1583 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001584 }
Adam Langley95c29f32014-06-20 12:00:00 -07001585
David Benjamindc72ff72014-06-25 12:36:10 -04001586 /* Copy the hostname as a string. */
David Benjamined439582014-07-14 19:13:02 -04001587 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname))
David Benjamindc72ff72014-06-25 12:36:10 -04001588 {
1589 *out_alert = SSL_AD_INTERNAL_ERROR;
1590 return 0;
1591 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001592
1593 s->should_ack_sni = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001594 }
Adam Langley95c29f32014-06-20 12:00:00 -07001595 }
Adam Langley95c29f32014-06-20 12:00:00 -07001596 }
1597
Adam Langley95c29f32014-06-20 12:00:00 -07001598 else if (type == TLSEXT_TYPE_ec_point_formats)
1599 {
David Benjamindc72ff72014-06-25 12:36:10 -04001600 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001601
David Benjamindc72ff72014-06-25 12:36:10 -04001602 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1603 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001604 {
David Benjamindc72ff72014-06-25 12:36:10 -04001605 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001606 return 0;
1607 }
David Benjamindc72ff72014-06-25 12:36:10 -04001608
David Benjamina19fc252014-10-19 00:14:36 -04001609 if (!CBS_stow(&ec_point_format_list,
1610 &s->s3->tmp.peer_ecpointformatlist,
1611 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001612 {
David Benjamina19fc252014-10-19 00:14:36 -04001613 *out_alert = SSL_AD_INTERNAL_ERROR;
1614 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001615 }
Adam Langley95c29f32014-06-20 12:00:00 -07001616 }
1617 else if (type == TLSEXT_TYPE_elliptic_curves)
1618 {
David Benjamindc72ff72014-06-25 12:36:10 -04001619 CBS elliptic_curve_list;
David Benjamin072334d2014-07-13 16:24:27 -04001620 size_t i, num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001621
David Benjamindc72ff72014-06-25 12:36:10 -04001622 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
David Benjamin072334d2014-07-13 16:24:27 -04001623 CBS_len(&elliptic_curve_list) == 0 ||
1624 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
David Benjamindc72ff72014-06-25 12:36:10 -04001625 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001626 {
David Benjamindc72ff72014-06-25 12:36:10 -04001627 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001628 return 0;
1629 }
David Benjamindc72ff72014-06-25 12:36:10 -04001630
David Benjamina19fc252014-10-19 00:14:36 -04001631 if (s->s3->tmp.peer_ellipticcurvelist)
Adam Langley95c29f32014-06-20 12:00:00 -07001632 {
David Benjamina19fc252014-10-19 00:14:36 -04001633 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1634 s->s3->tmp.peer_ellipticcurvelist_length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001635 }
David Benjamina19fc252014-10-19 00:14:36 -04001636 s->s3->tmp.peer_ellipticcurvelist =
1637 (uint16_t*)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
1638 if (s->s3->tmp.peer_ellipticcurvelist == NULL)
1639 {
1640 *out_alert = SSL_AD_INTERNAL_ERROR;
1641 return 0;
1642 }
1643 num_curves = CBS_len(&elliptic_curve_list) / 2;
1644 for (i = 0; i < num_curves; i++)
1645 {
1646 if (!CBS_get_u16(&elliptic_curve_list,
1647 &s->s3->tmp.peer_ellipticcurvelist[i]))
1648 {
1649 *out_alert = SSL_AD_INTERNAL_ERROR;
1650 return 0;
1651 }
1652 }
1653 if (CBS_len(&elliptic_curve_list) != 0)
1654 {
1655 *out_alert = SSL_AD_INTERNAL_ERROR;
1656 return 0;
1657 }
1658 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
Adam Langley95c29f32014-06-20 12:00:00 -07001659 }
Adam Langley95c29f32014-06-20 12:00:00 -07001660 else if (type == TLSEXT_TYPE_session_ticket)
1661 {
1662 if (s->tls_session_ticket_ext_cb &&
David Benjamindc72ff72014-06-25 12:36:10 -04001663 !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension), s->tls_session_ticket_ext_cb_arg))
Adam Langley95c29f32014-06-20 12:00:00 -07001664 {
David Benjamindc72ff72014-06-25 12:36:10 -04001665 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001666 return 0;
1667 }
1668 }
1669 else if (type == TLSEXT_TYPE_renegotiate)
1670 {
David Benjamindc72ff72014-06-25 12:36:10 -04001671 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001672 return 0;
1673 renegotiate_seen = 1;
1674 }
1675 else if (type == TLSEXT_TYPE_signature_algorithms)
1676 {
David Benjamindc72ff72014-06-25 12:36:10 -04001677 CBS supported_signature_algorithms;
1678
David Benjamindc72ff72014-06-25 12:36:10 -04001679 if (!CBS_get_u16_length_prefixed(&extension, &supported_signature_algorithms) ||
1680 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001681 {
David Benjamindc72ff72014-06-25 12:36:10 -04001682 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001683 return 0;
1684 }
David Benjamindc72ff72014-06-25 12:36:10 -04001685
1686 /* Ensure the signature algorithms are non-empty. It
1687 * contains a list of SignatureAndHashAlgorithms
1688 * which are two bytes each. */
1689 if (CBS_len(&supported_signature_algorithms) == 0 ||
1690 (CBS_len(&supported_signature_algorithms) % 2) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001691 {
David Benjamindc72ff72014-06-25 12:36:10 -04001692 *out_alert = SSL_AD_DECODE_ERROR;
1693 return 0;
1694 }
1695
David Benjamincd996942014-07-20 16:23:51 -04001696 if (!tls1_process_sigalgs(s, &supported_signature_algorithms))
David Benjamindc72ff72014-06-25 12:36:10 -04001697 {
1698 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001699 return 0;
1700 }
1701 /* If sigalgs received and no shared algorithms fatal
1702 * error.
1703 */
1704 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
1705 {
1706 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
David Benjamindc72ff72014-06-25 12:36:10 -04001707 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07001708 return 0;
1709 }
1710 }
1711
Adam Langley95c29f32014-06-20 12:00:00 -07001712 else if (type == TLSEXT_TYPE_next_proto_neg &&
1713 s->s3->tmp.finish_md_len == 0 &&
1714 s->s3->alpn_selected == NULL)
1715 {
David Benjamindc72ff72014-06-25 12:36:10 -04001716 /* The extension must be empty. */
1717 if (CBS_len(&extension) != 0)
1718 {
1719 *out_alert = SSL_AD_DECODE_ERROR;
1720 return 0;
1721 }
1722
Adam Langley95c29f32014-06-20 12:00:00 -07001723 /* We shouldn't accept this extension on a
1724 * renegotiation.
1725 *
1726 * s->new_session will be set on renegotiation, but we
1727 * probably shouldn't rely that it couldn't be set on
1728 * the initial renegotation too in certain cases (when
1729 * there's some other reason to disallow resuming an
1730 * earlier session -- the current code won't be doing
1731 * anything like that, but this might change).
1732
1733 * A valid sign that there's been a previous handshake
1734 * in this connection is if s->s3->tmp.finish_md_len >
1735 * 0. (We are talking about a check that will happen
1736 * in the Hello protocol round, well before a new
1737 * Finished message could have been computed.) */
1738 s->s3->next_proto_neg_seen = 1;
1739 }
Adam Langley95c29f32014-06-20 12:00:00 -07001740
1741 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1742 s->ctx->alpn_select_cb &&
1743 s->s3->tmp.finish_md_len == 0)
1744 {
David Benjamindc72ff72014-06-25 12:36:10 -04001745 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001746 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001747 /* ALPN takes precedence over NPN. */
1748 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001749 }
1750
Adam Langley1258b6a2014-06-20 12:00:00 -07001751 else if (type == TLSEXT_TYPE_channel_id &&
1752 s->tlsext_channel_id_enabled)
David Benjamindc72ff72014-06-25 12:36:10 -04001753 {
1754 /* The extension must be empty. */
1755 if (CBS_len(&extension) != 0)
1756 {
1757 *out_alert = SSL_AD_DECODE_ERROR;
1758 return 0;
1759 }
1760
Adam Langley1258b6a2014-06-20 12:00:00 -07001761 s->s3->tlsext_channel_id_valid = 1;
David Benjamindc72ff72014-06-25 12:36:10 -04001762 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001763
1764 else if (type == TLSEXT_TYPE_channel_id_new &&
1765 s->tlsext_channel_id_enabled)
1766 {
David Benjamindc72ff72014-06-25 12:36:10 -04001767 /* The extension must be empty. */
1768 if (CBS_len(&extension) != 0)
1769 {
1770 *out_alert = SSL_AD_DECODE_ERROR;
1771 return 0;
1772 }
1773
Adam Langley1258b6a2014-06-20 12:00:00 -07001774 s->s3->tlsext_channel_id_valid = 1;
1775 s->s3->tlsext_channel_id_new = 1;
1776 }
1777
1778
Adam Langley95c29f32014-06-20 12:00:00 -07001779 /* session ticket processed earlier */
1780 else if (type == TLSEXT_TYPE_use_srtp)
1781 {
David Benjamindc72ff72014-06-25 12:36:10 -04001782 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07001783 return 0;
1784 }
Adam Langley95c29f32014-06-20 12:00:00 -07001785 }
1786
Adam Langley95c29f32014-06-20 12:00:00 -07001787 ri_check:
1788
1789 /* Need RI if renegotiating */
1790
1791 if (!renegotiate_seen && s->renegotiate &&
1792 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1793 {
David Benjamindc72ff72014-06-25 12:36:10 -04001794 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin172fc2c2014-09-06 13:09:47 -04001795 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07001796 return 0;
1797 }
1798 /* If no signature algorithms extension set default values */
1799 if (!s->cert->peer_sigalgs)
1800 ssl_cert_set_default_md(s->cert);
1801
1802 return 1;
1803 }
1804
David Benjamindc72ff72014-06-25 12:36:10 -04001805int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001806 {
David Benjamindc72ff72014-06-25 12:36:10 -04001807 int alert = -1;
1808 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001809 {
David Benjamindc72ff72014-06-25 12:36:10 -04001810 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07001811 return 0;
1812 }
1813
David Benjamin6c7aed02014-08-27 16:42:38 -04001814 if (ssl_check_clienthello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001815 {
David Benjamin9d28c752014-07-05 00:43:48 -04001816 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07001817 return 0;
1818 }
1819 return 1;
David Benjamin072334d2014-07-13 16:24:27 -04001820 }
Adam Langley95c29f32014-06-20 12:00:00 -07001821
Adam Langley95c29f32014-06-20 12:00:00 -07001822/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1823 * elements of zero length are allowed and the set of elements must exactly fill
1824 * the length of the block. */
David Benjamin03973092014-06-24 23:27:17 -04001825static char ssl_next_proto_validate(const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001826 {
David Benjamin03973092014-06-24 23:27:17 -04001827 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001828
David Benjamin03973092014-06-24 23:27:17 -04001829 while (CBS_len(&copy) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001830 {
David Benjamin03973092014-06-24 23:27:17 -04001831 CBS proto;
1832 if (!CBS_get_u8_length_prefixed(&copy, &proto) ||
1833 CBS_len(&proto) == 0)
1834 {
Adam Langley95c29f32014-06-20 12:00:00 -07001835 return 0;
David Benjamin03973092014-06-24 23:27:17 -04001836 }
Adam Langley95c29f32014-06-20 12:00:00 -07001837 }
David Benjamin03973092014-06-24 23:27:17 -04001838 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001839 }
Adam Langley95c29f32014-06-20 12:00:00 -07001840
David Benjamin03973092014-06-24 23:27:17 -04001841static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07001842 {
Adam Langley95c29f32014-06-20 12:00:00 -07001843 int tlsext_servername = 0;
1844 int renegotiate_seen = 0;
David Benjamin03973092014-06-24 23:27:17 -04001845 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001846
David Benjamin6c7aed02014-08-27 16:42:38 -04001847 /* TODO(davidben): Move all of these to some per-handshake state that
1848 * gets systematically reset on a new handshake; perhaps allocate it
1849 * fresh each time so it's not even kept around post-handshake. */
Adam Langley95c29f32014-06-20 12:00:00 -07001850 s->s3->next_proto_neg_seen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001851
David Benjamin6c7aed02014-08-27 16:42:38 -04001852 s->tlsext_ticket_expected = 0;
1853 s->s3->tmp.certificate_status_expected = 0;
David Benjamin64442872014-07-21 17:43:45 -04001854
Adam Langley95c29f32014-06-20 12:00:00 -07001855 if (s->s3->alpn_selected)
1856 {
1857 OPENSSL_free(s->s3->alpn_selected);
1858 s->s3->alpn_selected = NULL;
1859 }
1860
David Benjamina19fc252014-10-19 00:14:36 -04001861 /* Clear ECC extensions */
1862 if (s->s3->tmp.peer_ecpointformatlist != 0)
1863 {
1864 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1865 s->s3->tmp.peer_ecpointformatlist = NULL;
1866 s->s3->tmp.peer_ecpointformatlist_length = 0;
1867 }
1868
David Benjamin03973092014-06-24 23:27:17 -04001869 /* There may be no extensions. */
1870 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001871 {
David Benjamin03973092014-06-24 23:27:17 -04001872 goto ri_check;
1873 }
1874
David Benjamin35a7a442014-07-05 00:23:20 -04001875 /* Decode the extensions block and check it is valid. */
1876 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1877 !tls1_check_duplicate_extensions(&extensions))
David Benjamin03973092014-06-24 23:27:17 -04001878 {
1879 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001880 return 0;
1881 }
1882
David Benjamin03973092014-06-24 23:27:17 -04001883 while (CBS_len(&extensions) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001884 {
David Benjamin03973092014-06-24 23:27:17 -04001885 uint16_t type;
1886 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001887
David Benjamin03973092014-06-24 23:27:17 -04001888 /* Decode the next extension. */
1889 if (!CBS_get_u16(&extensions, &type) ||
1890 !CBS_get_u16_length_prefixed(&extensions, &extension))
1891 {
1892 *out_alert = SSL_AD_DECODE_ERROR;
1893 return 0;
1894 }
Adam Langley95c29f32014-06-20 12:00:00 -07001895
1896 if (s->tlsext_debug_cb)
David Benjamin03973092014-06-24 23:27:17 -04001897 {
1898 s->tlsext_debug_cb(s, 1, type, (unsigned char*)CBS_data(&extension),
1899 CBS_len(&extension), s->tlsext_debug_arg);
1900 }
Adam Langley95c29f32014-06-20 12:00:00 -07001901
1902 if (type == TLSEXT_TYPE_server_name)
1903 {
David Benjamin03973092014-06-24 23:27:17 -04001904 /* The extension must be empty. */
1905 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001906 {
David Benjamin03973092014-06-24 23:27:17 -04001907 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001908 return 0;
1909 }
David Benjamin03973092014-06-24 23:27:17 -04001910 /* We must have sent it in ClientHello. */
1911 if (s->tlsext_hostname == NULL)
1912 {
1913 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1914 return 0;
1915 }
1916 tlsext_servername = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001917 }
Adam Langley95c29f32014-06-20 12:00:00 -07001918 else if (type == TLSEXT_TYPE_ec_point_formats)
1919 {
David Benjamin03973092014-06-24 23:27:17 -04001920 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07001921
David Benjamin03973092014-06-24 23:27:17 -04001922 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1923 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001924 {
David Benjamin03973092014-06-24 23:27:17 -04001925 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001926 return 0;
1927 }
David Benjamin03973092014-06-24 23:27:17 -04001928
David Benjamina19fc252014-10-19 00:14:36 -04001929 if (!CBS_stow(&ec_point_format_list,
1930 &s->s3->tmp.peer_ecpointformatlist,
1931 &s->s3->tmp.peer_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07001932 {
David Benjamina19fc252014-10-19 00:14:36 -04001933 *out_alert = SSL_AD_INTERNAL_ERROR;
1934 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001935 }
Adam Langley95c29f32014-06-20 12:00:00 -07001936 }
Adam Langley95c29f32014-06-20 12:00:00 -07001937 else if (type == TLSEXT_TYPE_session_ticket)
1938 {
1939 if (s->tls_session_ticket_ext_cb &&
David Benjamin03973092014-06-24 23:27:17 -04001940 !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension),
1941 s->tls_session_ticket_ext_cb_arg))
Adam Langley95c29f32014-06-20 12:00:00 -07001942 {
David Benjamin03973092014-06-24 23:27:17 -04001943 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001944 return 0;
1945 }
David Benjamin03973092014-06-24 23:27:17 -04001946
1947 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001948 {
David Benjamin03973092014-06-24 23:27:17 -04001949 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07001950 return 0;
1951 }
David Benjamin03973092014-06-24 23:27:17 -04001952
Adam Langley95c29f32014-06-20 12:00:00 -07001953 s->tlsext_ticket_expected = 1;
1954 }
Adam Langley95c29f32014-06-20 12:00:00 -07001955 else if (type == TLSEXT_TYPE_status_request)
1956 {
David Benjamin03973092014-06-24 23:27:17 -04001957 /* The extension MUST be empty and may only sent if
1958 * we've requested a status request message. */
1959 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001960 {
David Benjamin03973092014-06-24 23:27:17 -04001961 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07001962 return 0;
1963 }
David Benjamin6c7aed02014-08-27 16:42:38 -04001964 if (!s->ocsp_stapling_enabled)
David Benjamin03973092014-06-24 23:27:17 -04001965 {
1966 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1967 return 0;
1968 }
1969 /* Set a flag to expect a CertificateStatus message */
David Benjamin6c7aed02014-08-27 16:42:38 -04001970 s->s3->tmp.certificate_status_expected = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001971 }
David Benjamin03973092014-06-24 23:27:17 -04001972 else if (type == TLSEXT_TYPE_next_proto_neg && s->s3->tmp.finish_md_len == 0) {
1973 unsigned char *selected;
1974 unsigned char selected_len;
1975
1976 /* We must have requested it. */
1977 if (s->ctx->next_proto_select_cb == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001978 {
David Benjamin03973092014-06-24 23:27:17 -04001979 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1980 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001981 }
Adam Langley95c29f32014-06-20 12:00:00 -07001982
David Benjamin03973092014-06-24 23:27:17 -04001983 /* The data must be valid. */
1984 if (!ssl_next_proto_validate(&extension))
1985 {
1986 *out_alert = SSL_AD_DECODE_ERROR;
1987 return 0;
1988 }
1989
1990 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len,
1991 CBS_data(&extension), CBS_len(&extension),
1992 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
1993 {
1994 *out_alert = SSL_AD_INTERNAL_ERROR;
1995 return 0;
1996 }
1997
1998 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
1999 if (s->next_proto_negotiated == NULL)
2000 {
2001 *out_alert = SSL_AD_INTERNAL_ERROR;
2002 return 0;
2003 }
2004 s->next_proto_negotiated_len = selected_len;
2005 s->s3->next_proto_neg_seen = 1;
2006 }
Adam Langley95c29f32014-06-20 12:00:00 -07002007 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
2008 {
David Benjamin03973092014-06-24 23:27:17 -04002009 CBS protocol_name_list, protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07002010
2011 /* We must have requested it. */
2012 if (s->alpn_client_proto_list == NULL)
2013 {
David Benjamin03973092014-06-24 23:27:17 -04002014 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07002015 return 0;
2016 }
David Benjamin03973092014-06-24 23:27:17 -04002017
2018 /* The extension data consists of a ProtocolNameList
2019 * which must have exactly one ProtocolName. Each of
2020 * these is length-prefixed. */
2021 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
2022 CBS_len(&extension) != 0 ||
2023 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
2024 CBS_len(&protocol_name_list) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002025 {
David Benjamin03973092014-06-24 23:27:17 -04002026 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002027 return 0;
2028 }
David Benjamin03973092014-06-24 23:27:17 -04002029
2030 if (!CBS_stow(&protocol_name,
2031 &s->s3->alpn_selected,
2032 &s->s3->alpn_selected_len))
Adam Langley95c29f32014-06-20 12:00:00 -07002033 {
David Benjamin03973092014-06-24 23:27:17 -04002034 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002035 return 0;
2036 }
Adam Langley95c29f32014-06-20 12:00:00 -07002037 }
2038
Adam Langley1258b6a2014-06-20 12:00:00 -07002039 else if (type == TLSEXT_TYPE_channel_id)
David Benjamin03973092014-06-24 23:27:17 -04002040 {
2041 if (CBS_len(&extension) != 0)
2042 {
2043 *out_alert = SSL_AD_DECODE_ERROR;
2044 return 0;
2045 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002046 s->s3->tlsext_channel_id_valid = 1;
David Benjamin03973092014-06-24 23:27:17 -04002047 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002048 else if (type == TLSEXT_TYPE_channel_id_new)
2049 {
David Benjamin03973092014-06-24 23:27:17 -04002050 if (CBS_len(&extension) != 0)
2051 {
2052 *out_alert = SSL_AD_DECODE_ERROR;
2053 return 0;
2054 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002055 s->s3->tlsext_channel_id_valid = 1;
2056 s->s3->tlsext_channel_id_new = 1;
2057 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002058 else if (type == TLSEXT_TYPE_certificate_timestamp)
2059 {
2060 if (CBS_len(&extension) == 0)
2061 {
2062 *out_alert = SSL_AD_DECODE_ERROR;
2063 return 0;
2064 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002065
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02002066 /* Session resumption uses the original session information. */
2067 if (!s->hit)
2068 {
2069 if (!CBS_stow(&extension,
2070 &s->session->tlsext_signed_cert_timestamp_list,
2071 &s->session->tlsext_signed_cert_timestamp_list_length))
2072 {
2073 *out_alert = SSL_AD_INTERNAL_ERROR;
2074 return 0;
2075 }
2076 }
2077 }
Adam Langley95c29f32014-06-20 12:00:00 -07002078 else if (type == TLSEXT_TYPE_renegotiate)
2079 {
David Benjamin03973092014-06-24 23:27:17 -04002080 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002081 return 0;
2082 renegotiate_seen = 1;
2083 }
Adam Langley95c29f32014-06-20 12:00:00 -07002084 else if (type == TLSEXT_TYPE_use_srtp)
2085 {
David Benjamin03973092014-06-24 23:27:17 -04002086 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07002087 return 0;
2088 }
Adam Langley95c29f32014-06-20 12:00:00 -07002089 }
2090
2091 if (!s->hit && tlsext_servername == 1)
2092 {
2093 if (s->tlsext_hostname)
2094 {
2095 if (s->session->tlsext_hostname == NULL)
2096 {
2097 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2098 if (!s->session->tlsext_hostname)
2099 {
David Benjamin03973092014-06-24 23:27:17 -04002100 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002101 return 0;
2102 }
2103 }
2104 else
2105 {
David Benjamin03973092014-06-24 23:27:17 -04002106 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002107 return 0;
2108 }
2109 }
2110 }
2111
Adam Langley95c29f32014-06-20 12:00:00 -07002112 ri_check:
2113
2114 /* Determine if we need to see RI. Strictly speaking if we want to
2115 * avoid an attack we should *always* see RI even on initial server
2116 * hello because the client doesn't see any renegotiation during an
2117 * attack. However this would mean we could not connect to any server
2118 * which doesn't support RI so for the immediate future tolerate RI
2119 * absence on initial connect only.
2120 */
2121 if (!renegotiate_seen
2122 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2123 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2124 {
David Benjamin03973092014-06-24 23:27:17 -04002125 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin9d28c752014-07-05 00:43:48 -04002126 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
Adam Langley95c29f32014-06-20 12:00:00 -07002127 return 0;
2128 }
2129
2130 return 1;
2131 }
2132
2133
2134int ssl_prepare_clienthello_tlsext(SSL *s)
2135 {
Adam Langley95c29f32014-06-20 12:00:00 -07002136 return 1;
2137 }
2138
2139int ssl_prepare_serverhello_tlsext(SSL *s)
2140 {
2141 return 1;
2142 }
2143
David Benjamin6c7aed02014-08-27 16:42:38 -04002144static int ssl_check_clienthello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002145 {
2146 int ret=SSL_TLSEXT_ERR_NOACK;
2147 int al = SSL_AD_UNRECOGNIZED_NAME;
2148
Adam Langley95c29f32014-06-20 12:00:00 -07002149 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2150 * ssl3_choose_cipher in s3_lib.c.
2151 */
2152 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2153 * ssl3_choose_cipher in s3_lib.c.
2154 */
Adam Langley95c29f32014-06-20 12:00:00 -07002155
2156 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2157 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2158 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2159 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2160
Adam Langley95c29f32014-06-20 12:00:00 -07002161 switch (ret)
2162 {
2163 case SSL_TLSEXT_ERR_ALERT_FATAL:
2164 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2165 return -1;
2166
2167 case SSL_TLSEXT_ERR_ALERT_WARNING:
2168 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002169 return 1;
2170
Adam Langley95c29f32014-06-20 12:00:00 -07002171 case SSL_TLSEXT_ERR_NOACK:
Adam Langleyed8270a2014-09-02 13:52:56 -07002172 s->should_ack_sni = 0;
2173 return 1;
2174
2175 default:
2176 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002177 }
2178 }
2179
David Benjamin6c7aed02014-08-27 16:42:38 -04002180static int ssl_check_serverhello_tlsext(SSL *s)
Adam Langley95c29f32014-06-20 12:00:00 -07002181 {
2182 int ret=SSL_TLSEXT_ERR_NOACK;
2183 int al = SSL_AD_UNRECOGNIZED_NAME;
2184
Adam Langley95c29f32014-06-20 12:00:00 -07002185 /* If we are client and using an elliptic curve cryptography cipher
2186 * suite, then if server returns an EC point formats lists extension
2187 * it must contain uncompressed.
2188 */
2189 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2190 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamina9ca90a2014-09-26 18:53:43 -04002191 if (((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) &&
2192 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed))
Adam Langley95c29f32014-06-20 12:00:00 -07002193 {
David Benjamina9ca90a2014-09-26 18:53:43 -04002194 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2195 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002196 }
2197 ret = SSL_TLSEXT_ERR_OK;
Adam Langley95c29f32014-06-20 12:00:00 -07002198
2199 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2200 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2201 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2202 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2203
Adam Langley95c29f32014-06-20 12:00:00 -07002204 switch (ret)
2205 {
2206 case SSL_TLSEXT_ERR_ALERT_FATAL:
Adam Langleyed8270a2014-09-02 13:52:56 -07002207 ssl3_send_alert(s,SSL3_AL_FATAL,al);
Adam Langley95c29f32014-06-20 12:00:00 -07002208 return -1;
2209
2210 case SSL_TLSEXT_ERR_ALERT_WARNING:
2211 ssl3_send_alert(s,SSL3_AL_WARNING,al);
Adam Langleyed8270a2014-09-02 13:52:56 -07002212 return 1;
2213
2214 default:
2215 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002216 }
2217 }
2218
David Benjamin03973092014-06-24 23:27:17 -04002219int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07002220 {
David Benjamin03973092014-06-24 23:27:17 -04002221 int alert = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002222 if (s->version < SSL3_VERSION)
2223 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002224
2225 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002226 {
David Benjamin03973092014-06-24 23:27:17 -04002227 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07002228 return 0;
2229 }
2230
David Benjamin03973092014-06-24 23:27:17 -04002231 if (ssl_check_serverhello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002232 {
David Benjamin9d28c752014-07-05 00:43:48 -04002233 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, SSL_R_SERVERHELLO_TLSEXT);
Adam Langley95c29f32014-06-20 12:00:00 -07002234 return 0;
2235 }
David Benjamin03973092014-06-24 23:27:17 -04002236
Adam Langley95c29f32014-06-20 12:00:00 -07002237 return 1;
David Benjamin03973092014-06-24 23:27:17 -04002238 }
Adam Langley95c29f32014-06-20 12:00:00 -07002239
2240/* Since the server cache lookup is done early on in the processing of the
2241 * ClientHello, and other operations depend on the result, we need to handle
2242 * any TLS session ticket extension at the same time.
2243 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002244 * ctx: contains the early callback context, which is the result of a
2245 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002246 * ret: (output) on return, if a ticket was decrypted, then this is set to
2247 * point to the resulting session.
2248 *
2249 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2250 * ciphersuite, in which case we have no use for session tickets and one will
2251 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2252 *
2253 * Returns:
2254 * -1: fatal error, either from parsing or decrypting the ticket.
2255 * 0: no ticket was found (or was ignored, based on settings).
2256 * 1: a zero length extension was found, indicating that the client supports
2257 * session tickets but doesn't currently have one to offer.
2258 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2259 * couldn't be decrypted because of a non-fatal error.
2260 * 3: a ticket was successfully decrypted and *ret was set.
2261 *
2262 * Side effects:
2263 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2264 * a new session ticket to the client because the client indicated support
2265 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2266 * a session ticket or we couldn't use the one it gave us, or if
2267 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2268 * Otherwise, s->tlsext_ticket_expected is set to 0.
2269 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002270int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
2271 SSL_SESSION **ret)
Adam Langley95c29f32014-06-20 12:00:00 -07002272 {
Adam Langley95c29f32014-06-20 12:00:00 -07002273 *ret = NULL;
2274 s->tlsext_ticket_expected = 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002275 const unsigned char *data;
2276 size_t len;
2277 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002278
2279 /* If tickets disabled behave as if no ticket present
2280 * to permit stateful resumption.
2281 */
2282 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2283 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002284 if ((s->version <= SSL3_VERSION) && !ctx->extensions)
Adam Langley95c29f32014-06-20 12:00:00 -07002285 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07002286 if (!SSL_early_callback_ctx_extension_get(
2287 ctx, TLSEXT_TYPE_session_ticket, &data, &len))
Adam Langley95c29f32014-06-20 12:00:00 -07002288 {
Adam Langleydc9b1412014-06-20 12:00:00 -07002289 return 0;
2290 }
2291 if (len == 0)
2292 {
2293 /* The client will accept a ticket but doesn't
2294 * currently have one. */
2295 s->tlsext_ticket_expected = 1;
2296 return 1;
2297 }
2298 if (s->tls_session_secret_cb)
2299 {
2300 /* Indicate that the ticket couldn't be
2301 * decrypted rather than generating the session
2302 * from ticket now, trigger abbreviated
2303 * handshake based on external mechanism to
2304 * calculate the master secret later. */
2305 return 2;
2306 }
2307 r = tls_decrypt_ticket(s, data, len, ctx->session_id,
2308 ctx->session_id_len, ret);
2309 switch (r)
2310 {
2311 case 2: /* ticket couldn't be decrypted */
2312 s->tlsext_ticket_expected = 1;
2313 return 2;
2314 case 3: /* ticket was decrypted */
2315 return r;
2316 case 4: /* ticket decrypted but need to renew */
2317 s->tlsext_ticket_expected = 1;
2318 return 3;
2319 default: /* fatal error */
Adam Langley95c29f32014-06-20 12:00:00 -07002320 return -1;
2321 }
Adam Langley95c29f32014-06-20 12:00:00 -07002322 }
2323
2324/* tls_decrypt_ticket attempts to decrypt a session ticket.
2325 *
2326 * etick: points to the body of the session ticket extension.
2327 * eticklen: the length of the session tickets extenion.
2328 * sess_id: points at the session ID.
2329 * sesslen: the length of the session ID.
2330 * psess: (output) on return, if a ticket was decrypted, then this is set to
2331 * point to the resulting session.
2332 *
2333 * Returns:
2334 * -1: fatal error, either from parsing or decrypting the ticket.
2335 * 2: the ticket couldn't be decrypted.
2336 * 3: a ticket was successfully decrypted and *psess was set.
2337 * 4: same as 3, but the ticket needs to be renewed.
2338 */
2339static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
2340 const unsigned char *sess_id, int sesslen,
2341 SSL_SESSION **psess)
2342 {
2343 SSL_SESSION *sess;
2344 unsigned char *sdec;
2345 const unsigned char *p;
2346 int slen, mlen, renew_ticket = 0;
2347 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2348 HMAC_CTX hctx;
2349 EVP_CIPHER_CTX ctx;
2350 SSL_CTX *tctx = s->initial_ctx;
2351 /* Need at least keyname + iv + some encrypted data */
2352 if (eticklen < 48)
2353 return 2;
2354 /* Initialize session ticket encryption and HMAC contexts */
2355 HMAC_CTX_init(&hctx);
2356 EVP_CIPHER_CTX_init(&ctx);
2357 if (tctx->tlsext_ticket_key_cb)
2358 {
2359 unsigned char *nctick = (unsigned char *)etick;
2360 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
2361 &ctx, &hctx, 0);
2362 if (rv < 0)
2363 return -1;
2364 if (rv == 0)
2365 return 2;
2366 if (rv == 2)
2367 renew_ticket = 1;
2368 }
2369 else
2370 {
2371 /* Check key name matches */
2372 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
2373 return 2;
2374 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2375 tlsext_tick_md(), NULL);
2376 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2377 tctx->tlsext_tick_aes_key, etick + 16);
2378 }
2379 /* Attempt to process session ticket, first conduct sanity and
2380 * integrity checks on ticket.
2381 */
2382 mlen = HMAC_size(&hctx);
2383 if (mlen < 0)
2384 {
2385 EVP_CIPHER_CTX_cleanup(&ctx);
2386 return -1;
2387 }
2388 eticklen -= mlen;
2389 /* Check HMAC of encrypted ticket */
2390 HMAC_Update(&hctx, etick, eticklen);
2391 HMAC_Final(&hctx, tick_hmac, NULL);
2392 HMAC_CTX_cleanup(&hctx);
2393 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
Adam Langley38311732014-10-16 19:04:35 -07002394 {
2395 EVP_CIPHER_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07002396 return 2;
Adam Langley38311732014-10-16 19:04:35 -07002397 }
Adam Langley95c29f32014-06-20 12:00:00 -07002398 /* Attempt to decrypt session data */
2399 /* Move p after IV to start of encrypted ticket, update length */
2400 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2401 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2402 sdec = OPENSSL_malloc(eticklen);
2403 if (!sdec)
2404 {
2405 EVP_CIPHER_CTX_cleanup(&ctx);
2406 return -1;
2407 }
2408 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2409 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0)
Adam Langley3e148852014-07-24 17:34:02 -07002410 {
2411 EVP_CIPHER_CTX_cleanup(&ctx);
2412 OPENSSL_free(sdec);
Adam Langley95c29f32014-06-20 12:00:00 -07002413 return 2;
Adam Langley3e148852014-07-24 17:34:02 -07002414 }
Adam Langley95c29f32014-06-20 12:00:00 -07002415 slen += mlen;
2416 EVP_CIPHER_CTX_cleanup(&ctx);
2417 p = sdec;
2418
2419 sess = d2i_SSL_SESSION(NULL, &p, slen);
2420 OPENSSL_free(sdec);
2421 if (sess)
2422 {
2423 /* The session ID, if non-empty, is used by some clients to
2424 * detect that the ticket has been accepted. So we copy it to
2425 * the session structure. If it is empty set length to zero
2426 * as required by standard.
2427 */
2428 if (sesslen)
2429 memcpy(sess->session_id, sess_id, sesslen);
2430 sess->session_id_length = sesslen;
2431 *psess = sess;
2432 if (renew_ticket)
2433 return 4;
2434 else
2435 return 3;
2436 }
2437 ERR_clear_error();
2438 /* For session parse failure, indicate that we need to send a new
2439 * ticket. */
2440 return 2;
2441 }
2442
2443/* Tables to translate from NIDs to TLS v1.2 ids */
2444
2445typedef struct
2446 {
2447 int nid;
2448 int id;
2449 } tls12_lookup;
2450
David Benjamincff64722014-08-19 19:54:46 -04002451static const tls12_lookup tls12_md[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002452 {NID_md5, TLSEXT_hash_md5},
2453 {NID_sha1, TLSEXT_hash_sha1},
2454 {NID_sha224, TLSEXT_hash_sha224},
2455 {NID_sha256, TLSEXT_hash_sha256},
2456 {NID_sha384, TLSEXT_hash_sha384},
2457 {NID_sha512, TLSEXT_hash_sha512}
2458};
2459
David Benjamincff64722014-08-19 19:54:46 -04002460static const tls12_lookup tls12_sig[] = {
Adam Langley95c29f32014-06-20 12:00:00 -07002461 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
Adam Langley95c29f32014-06-20 12:00:00 -07002462 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
2463};
2464
David Benjamincff64722014-08-19 19:54:46 -04002465static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002466 {
2467 size_t i;
2468 for (i = 0; i < tlen; i++)
2469 {
2470 if (table[i].nid == nid)
2471 return table[i].id;
2472 }
2473 return -1;
2474 }
2475
David Benjamincff64722014-08-19 19:54:46 -04002476static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
Adam Langley95c29f32014-06-20 12:00:00 -07002477 {
2478 size_t i;
2479 for (i = 0; i < tlen; i++)
2480 {
2481 if ((table[i].id) == id)
2482 return table[i].nid;
2483 }
2484 return NID_undef;
2485 }
2486
2487int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
2488 {
2489 int sig_id, md_id;
2490 if (!md)
2491 return 0;
2492 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2493 sizeof(tls12_md)/sizeof(tls12_lookup));
2494 if (md_id == -1)
2495 return 0;
2496 sig_id = tls12_get_sigid(pk);
2497 if (sig_id == -1)
2498 return 0;
2499 p[0] = (unsigned char)md_id;
2500 p[1] = (unsigned char)sig_id;
2501 return 1;
2502 }
2503
2504int tls12_get_sigid(const EVP_PKEY *pk)
2505 {
2506 return tls12_find_id(pk->type, tls12_sig,
2507 sizeof(tls12_sig)/sizeof(tls12_lookup));
2508 }
2509
2510const EVP_MD *tls12_get_hash(unsigned char hash_alg)
2511 {
2512 switch(hash_alg)
2513 {
Adam Langley95c29f32014-06-20 12:00:00 -07002514 case TLSEXT_hash_md5:
Adam Langley95c29f32014-06-20 12:00:00 -07002515 return EVP_md5();
Adam Langley95c29f32014-06-20 12:00:00 -07002516 case TLSEXT_hash_sha1:
2517 return EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002518 case TLSEXT_hash_sha224:
2519 return EVP_sha224();
2520
2521 case TLSEXT_hash_sha256:
2522 return EVP_sha256();
Adam Langley95c29f32014-06-20 12:00:00 -07002523 case TLSEXT_hash_sha384:
2524 return EVP_sha384();
2525
2526 case TLSEXT_hash_sha512:
2527 return EVP_sha512();
Adam Langley95c29f32014-06-20 12:00:00 -07002528 default:
2529 return NULL;
2530
2531 }
2532 }
2533
2534static int tls12_get_pkey_idx(unsigned char sig_alg)
2535 {
2536 switch(sig_alg)
2537 {
Adam Langley95c29f32014-06-20 12:00:00 -07002538 case TLSEXT_signature_rsa:
2539 return SSL_PKEY_RSA_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002540 case TLSEXT_signature_ecdsa:
2541 return SSL_PKEY_ECC;
Adam Langley95c29f32014-06-20 12:00:00 -07002542 }
2543 return -1;
2544 }
2545
2546/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2547static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
2548 int *psignhash_nid, const unsigned char *data)
2549 {
2550 int sign_nid = 0, hash_nid = 0;
2551 if (!phash_nid && !psign_nid && !psignhash_nid)
2552 return;
2553 if (phash_nid || psignhash_nid)
2554 {
2555 hash_nid = tls12_find_nid(data[0], tls12_md,
2556 sizeof(tls12_md)/sizeof(tls12_lookup));
2557 if (phash_nid)
2558 *phash_nid = hash_nid;
2559 }
2560 if (psign_nid || psignhash_nid)
2561 {
2562 sign_nid = tls12_find_nid(data[1], tls12_sig,
2563 sizeof(tls12_sig)/sizeof(tls12_lookup));
2564 if (psign_nid)
2565 *psign_nid = sign_nid;
2566 }
2567 if (psignhash_nid)
2568 {
2569 if (sign_nid && hash_nid)
2570 OBJ_find_sigid_by_algs(psignhash_nid,
2571 hash_nid, sign_nid);
2572 else
2573 *psignhash_nid = NID_undef;
2574 }
2575 }
2576/* Given preference and allowed sigalgs set shared sigalgs */
2577static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
2578 const unsigned char *pref, size_t preflen,
2579 const unsigned char *allow, size_t allowlen)
2580 {
2581 const unsigned char *ptmp, *atmp;
2582 size_t i, j, nmatch = 0;
2583 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
2584 {
2585 /* Skip disabled hashes or signature algorithms */
2586 if (tls12_get_hash(ptmp[0]) == NULL)
2587 continue;
2588 if (tls12_get_pkey_idx(ptmp[1]) == -1)
2589 continue;
2590 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
2591 {
2592 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
2593 {
2594 nmatch++;
2595 if (shsig)
2596 {
2597 shsig->rhash = ptmp[0];
2598 shsig->rsign = ptmp[1];
2599 tls1_lookup_sigalg(&shsig->hash_nid,
2600 &shsig->sign_nid,
2601 &shsig->signandhash_nid,
2602 ptmp);
2603 shsig++;
2604 }
2605 break;
2606 }
2607 }
2608 }
2609 return nmatch;
2610 }
2611
2612/* Set shared signature algorithms for SSL structures */
2613static int tls1_set_shared_sigalgs(SSL *s)
2614 {
2615 const unsigned char *pref, *allow, *conf;
2616 size_t preflen, allowlen, conflen;
2617 size_t nmatch;
2618 TLS_SIGALGS *salgs = NULL;
2619 CERT *c = s->cert;
Adam Langleydb4f9522014-06-20 12:00:00 -07002620 if (c->shared_sigalgs)
2621 {
2622 OPENSSL_free(c->shared_sigalgs);
2623 c->shared_sigalgs = NULL;
2624 }
Adam Langley95c29f32014-06-20 12:00:00 -07002625 /* If client use client signature algorithms if not NULL */
David Benjamin335d10d2014-08-06 19:56:33 -04002626 if (!s->server && c->client_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002627 {
2628 conf = c->client_sigalgs;
2629 conflen = c->client_sigalgslen;
2630 }
David Benjamin335d10d2014-08-06 19:56:33 -04002631 else if (c->conf_sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002632 {
2633 conf = c->conf_sigalgs;
2634 conflen = c->conf_sigalgslen;
2635 }
2636 else
2637 conflen = tls12_get_psigalgs(s, &conf);
David Benjamin335d10d2014-08-06 19:56:33 -04002638 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
Adam Langley95c29f32014-06-20 12:00:00 -07002639 {
2640 pref = conf;
2641 preflen = conflen;
2642 allow = c->peer_sigalgs;
2643 allowlen = c->peer_sigalgslen;
2644 }
2645 else
2646 {
2647 allow = conf;
2648 allowlen = conflen;
2649 pref = c->peer_sigalgs;
2650 preflen = c->peer_sigalgslen;
2651 }
2652 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2653 if (!nmatch)
2654 return 1;
2655 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2656 if (!salgs)
2657 return 0;
2658 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2659 c->shared_sigalgs = salgs;
2660 c->shared_sigalgslen = nmatch;
2661 return 1;
2662 }
2663
2664
2665/* Set preferred digest for each key type */
2666
David Benjamincd996942014-07-20 16:23:51 -04002667int tls1_process_sigalgs(SSL *s, const CBS *sigalgs)
Adam Langley95c29f32014-06-20 12:00:00 -07002668 {
2669 int idx;
2670 size_t i;
2671 const EVP_MD *md;
2672 CERT *c = s->cert;
2673 TLS_SIGALGS *sigptr;
David Benjamincd996942014-07-20 16:23:51 -04002674
Adam Langley95c29f32014-06-20 12:00:00 -07002675 /* Extension ignored for inappropriate versions */
2676 if (!SSL_USE_SIGALGS(s))
2677 return 1;
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002678 /* Length must be even */
David Benjamincd996942014-07-20 16:23:51 -04002679 if (CBS_len(sigalgs) % 2 != 0)
Alex Chernyakhovsky31955f92014-07-05 01:12:34 -04002680 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002681 /* Should never happen */
2682 if (!c)
2683 return 0;
2684
David Benjamincd996942014-07-20 16:23:51 -04002685 if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen))
Adam Langley95c29f32014-06-20 12:00:00 -07002686 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002687
2688 tls1_set_shared_sigalgs(s);
2689
Adam Langley95c29f32014-06-20 12:00:00 -07002690 for (i = 0, sigptr = c->shared_sigalgs;
2691 i < c->shared_sigalgslen; i++, sigptr++)
2692 {
2693 idx = tls12_get_pkey_idx(sigptr->rsign);
2694 if (idx > 0 && c->pkeys[idx].digest == NULL)
2695 {
2696 md = tls12_get_hash(sigptr->rhash);
2697 c->pkeys[idx].digest = md;
2698 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2699 if (idx == SSL_PKEY_RSA_SIGN)
2700 {
2701 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
2702 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2703 }
2704 }
2705
2706 }
2707 /* In strict mode leave unset digests as NULL to indicate we can't
2708 * use the certificate for signing.
2709 */
2710 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
2711 {
2712 /* Set any remaining keys to default values. NOTE: if alg is
2713 * not supported it stays as NULL.
2714 */
Adam Langley95c29f32014-06-20 12:00:00 -07002715 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
2716 {
2717 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
2718 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
2719 }
Adam Langley95c29f32014-06-20 12:00:00 -07002720 if (!c->pkeys[SSL_PKEY_ECC].digest)
2721 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -07002722 }
2723 return 1;
2724 }
2725
2726
2727int SSL_get_sigalgs(SSL *s, int idx,
2728 int *psign, int *phash, int *psignhash,
2729 unsigned char *rsig, unsigned char *rhash)
2730 {
2731 const unsigned char *psig = s->cert->peer_sigalgs;
2732 if (psig == NULL)
2733 return 0;
2734 if (idx >= 0)
2735 {
2736 idx <<= 1;
2737 if (idx >= (int)s->cert->peer_sigalgslen)
2738 return 0;
2739 psig += idx;
2740 if (rhash)
2741 *rhash = psig[0];
2742 if (rsig)
2743 *rsig = psig[1];
2744 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2745 }
2746 return s->cert->peer_sigalgslen / 2;
2747 }
2748
2749int SSL_get_shared_sigalgs(SSL *s, int idx,
2750 int *psign, int *phash, int *psignhash,
2751 unsigned char *rsig, unsigned char *rhash)
2752 {
2753 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
2754 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
2755 return 0;
2756 shsigalgs += idx;
2757 if (phash)
2758 *phash = shsigalgs->hash_nid;
2759 if (psign)
2760 *psign = shsigalgs->sign_nid;
2761 if (psignhash)
2762 *psignhash = shsigalgs->signandhash_nid;
2763 if (rsig)
2764 *rsig = shsigalgs->rsign;
2765 if (rhash)
2766 *rhash = shsigalgs->rhash;
2767 return s->cert->shared_sigalgslen;
2768 }
2769
Adam Langley1258b6a2014-06-20 12:00:00 -07002770/* tls1_channel_id_hash calculates the signed data for a Channel ID on the given
2771 * SSL connection and writes it to |md|. */
2772int
2773tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s)
2774 {
2775 EVP_MD_CTX ctx;
2776 unsigned char temp_digest[EVP_MAX_MD_SIZE];
2777 unsigned temp_digest_len;
2778 int i;
2779 static const char kClientIDMagic[] = "TLS Channel ID signature";
2780
2781 if (s->s3->handshake_buffer)
2782 if (!ssl3_digest_cached_records(s))
2783 return 0;
2784
2785 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2786
2787 if (s->hit && s->s3->tlsext_channel_id_new)
2788 {
2789 static const char kResumptionMagic[] = "Resumption";
2790 EVP_DigestUpdate(md, kResumptionMagic,
2791 sizeof(kResumptionMagic));
2792 if (s->session->original_handshake_hash_len == 0)
2793 return 0;
2794 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2795 s->session->original_handshake_hash_len);
2796 }
2797
2798 EVP_MD_CTX_init(&ctx);
2799 for (i = 0; i < SSL_MAX_DIGEST; i++)
2800 {
2801 if (s->s3->handshake_dgst[i] == NULL)
2802 continue;
2803 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
2804 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2805 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2806 }
2807 EVP_MD_CTX_cleanup(&ctx);
2808
2809 return 1;
2810 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002811
2812/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2813 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
2814int tls1_record_handshake_hashes_for_channel_id(SSL *s)
2815 {
2816 int digest_len;
2817 /* This function should never be called for a resumed session because
2818 * the handshake hashes that we wish to record are for the original,
2819 * full handshake. */
2820 if (s->hit)
2821 return -1;
2822 /* It only makes sense to call this function if Channel IDs have been
2823 * negotiated. */
2824 if (!s->s3->tlsext_channel_id_new)
2825 return -1;
2826
2827 digest_len = tls1_handshake_digest(
2828 s, s->session->original_handshake_hash,
2829 sizeof(s->session->original_handshake_hash));
2830 if (digest_len < 0)
2831 return -1;
2832
2833 s->session->original_handshake_hash_len = digest_len;
2834
2835 return 1;
2836 }
2837
Adam Langley95c29f32014-06-20 12:00:00 -07002838int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
2839 {
2840 unsigned char *sigalgs, *sptr;
2841 int rhash, rsign;
2842 size_t i;
2843 if (salglen & 1)
2844 return 0;
2845 sigalgs = OPENSSL_malloc(salglen);
2846 if (sigalgs == NULL)
2847 return 0;
2848 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
2849 {
2850 rhash = tls12_find_id(*psig_nids++, tls12_md,
2851 sizeof(tls12_md)/sizeof(tls12_lookup));
2852 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2853 sizeof(tls12_sig)/sizeof(tls12_lookup));
2854
2855 if (rhash == -1 || rsign == -1)
2856 goto err;
2857 *sptr++ = rhash;
2858 *sptr++ = rsign;
2859 }
2860
2861 if (client)
2862 {
2863 if (c->client_sigalgs)
2864 OPENSSL_free(c->client_sigalgs);
2865 c->client_sigalgs = sigalgs;
2866 c->client_sigalgslen = salglen;
2867 }
2868 else
2869 {
2870 if (c->conf_sigalgs)
2871 OPENSSL_free(c->conf_sigalgs);
2872 c->conf_sigalgs = sigalgs;
2873 c->conf_sigalgslen = salglen;
2874 }
2875
2876 return 1;
2877
2878 err:
2879 OPENSSL_free(sigalgs);
2880 return 0;
2881 }
2882
2883static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
2884 {
2885 int sig_nid;
2886 size_t i;
2887 if (default_nid == -1)
2888 return 1;
2889 sig_nid = X509_get_signature_nid(x);
2890 if (default_nid)
2891 return sig_nid == default_nid ? 1 : 0;
2892 for (i = 0; i < c->shared_sigalgslen; i++)
2893 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
2894 return 1;
2895 return 0;
2896 }
2897/* Check to see if a certificate issuer name matches list of CA names */
2898static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
2899 {
2900 X509_NAME *nm;
David Benjaminfb3ff2c2014-09-30 21:00:38 -04002901 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002902 nm = X509_get_issuer_name(x);
2903 for (i = 0; i < sk_X509_NAME_num(names); i++)
2904 {
2905 if(!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
2906 return 1;
2907 }
2908 return 0;
2909 }
2910
2911/* Check certificate chain is consistent with TLS extensions and is
2912 * usable by server. This servers two purposes: it allows users to
2913 * check chains before passing them to the server and it allows the
2914 * server to check chains before attempting to use them.
2915 */
2916
2917/* Flags which need to be set for a certificate when stict mode not set */
2918
2919#define CERT_PKEY_VALID_FLAGS \
2920 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
2921/* Strict mode flags */
2922#define CERT_PKEY_STRICT_FLAGS \
2923 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
2924 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
2925
2926int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
2927 int idx)
2928 {
David Benjaminfb3ff2c2014-09-30 21:00:38 -04002929 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002930 int rv = 0;
2931 int check_flags = 0, strict_mode;
2932 CERT_PKEY *cpk = NULL;
2933 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002934 /* idx == -1 means checking server chains */
2935 if (idx != -1)
2936 {
2937 /* idx == -2 means checking client certificate chains */
2938 if (idx == -2)
2939 {
2940 cpk = c->key;
2941 idx = cpk - c->pkeys;
2942 }
2943 else
2944 cpk = c->pkeys + idx;
2945 x = cpk->x509;
2946 pk = cpk->privatekey;
2947 chain = cpk->chain;
2948 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
2949 /* If no cert or key, forget it */
2950 if (!x || !pk)
2951 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -07002952 }
2953 else
2954 {
2955 if (!x || !pk)
2956 goto end;
2957 idx = ssl_cert_type(x, pk);
2958 if (idx == -1)
2959 goto end;
2960 cpk = c->pkeys + idx;
2961 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
2962 check_flags = CERT_PKEY_STRICT_FLAGS;
2963 else
2964 check_flags = CERT_PKEY_VALID_FLAGS;
2965 strict_mode = 1;
2966 }
2967
Adam Langley95c29f32014-06-20 12:00:00 -07002968 /* Check all signature algorithms are consistent with
2969 * signature algorithms extension if TLS 1.2 or later
2970 * and strict mode.
2971 */
2972 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode)
2973 {
2974 int default_nid;
2975 unsigned char rsign = 0;
2976 if (c->peer_sigalgs)
2977 default_nid = 0;
2978 /* If no sigalgs extension use defaults from RFC5246 */
2979 else
2980 {
2981 switch(idx)
2982 {
2983 case SSL_PKEY_RSA_ENC:
2984 case SSL_PKEY_RSA_SIGN:
Adam Langley95c29f32014-06-20 12:00:00 -07002985 rsign = TLSEXT_signature_rsa;
2986 default_nid = NID_sha1WithRSAEncryption;
2987 break;
2988
Adam Langley95c29f32014-06-20 12:00:00 -07002989 case SSL_PKEY_ECC:
2990 rsign = TLSEXT_signature_ecdsa;
2991 default_nid = NID_ecdsa_with_SHA1;
2992 break;
2993
2994 default:
2995 default_nid = -1;
2996 break;
2997 }
2998 }
2999 /* If peer sent no signature algorithms extension and we
3000 * have set preferred signature algorithms check we support
3001 * sha1.
3002 */
3003 if (default_nid > 0 && c->conf_sigalgs)
3004 {
3005 size_t j;
3006 const unsigned char *p = c->conf_sigalgs;
3007 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2)
3008 {
3009 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3010 break;
3011 }
3012 if (j == c->conf_sigalgslen)
3013 {
3014 if (check_flags)
3015 goto skip_sigs;
3016 else
3017 goto end;
3018 }
3019 }
3020 /* Check signature algorithm of each cert in chain */
3021 if (!tls1_check_sig_alg(c, x, default_nid))
3022 {
3023 if (!check_flags) goto end;
3024 }
3025 else
3026 rv |= CERT_PKEY_EE_SIGNATURE;
3027 rv |= CERT_PKEY_CA_SIGNATURE;
3028 for (i = 0; i < sk_X509_num(chain); i++)
3029 {
3030 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i),
3031 default_nid))
3032 {
3033 if (check_flags)
3034 {
3035 rv &= ~CERT_PKEY_CA_SIGNATURE;
3036 break;
3037 }
3038 else
3039 goto end;
3040 }
3041 }
3042 }
3043 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3044 else if(check_flags)
3045 rv |= CERT_PKEY_EE_SIGNATURE|CERT_PKEY_CA_SIGNATURE;
3046 skip_sigs:
3047 /* Check cert parameters are consistent */
3048 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3049 rv |= CERT_PKEY_EE_PARAM;
3050 else if (!check_flags)
3051 goto end;
3052 if (!s->server)
3053 rv |= CERT_PKEY_CA_PARAM;
3054 /* In strict mode check rest of chain too */
3055 else if (strict_mode)
3056 {
3057 rv |= CERT_PKEY_CA_PARAM;
3058 for (i = 0; i < sk_X509_num(chain); i++)
3059 {
3060 X509 *ca = sk_X509_value(chain, i);
3061 if (!tls1_check_cert_param(s, ca, 0))
3062 {
3063 if (check_flags)
3064 {
3065 rv &= ~CERT_PKEY_CA_PARAM;
3066 break;
3067 }
3068 else
3069 goto end;
3070 }
3071 }
3072 }
3073 if (!s->server && strict_mode)
3074 {
3075 STACK_OF(X509_NAME) *ca_dn;
David Benjamin676d1e72014-07-08 14:34:10 -04003076 uint8_t check_type = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07003077 switch (pk->type)
3078 {
3079 case EVP_PKEY_RSA:
3080 check_type = TLS_CT_RSA_SIGN;
3081 break;
Adam Langley95c29f32014-06-20 12:00:00 -07003082 case EVP_PKEY_EC:
3083 check_type = TLS_CT_ECDSA_SIGN;
3084 break;
Adam Langley95c29f32014-06-20 12:00:00 -07003085 }
3086 if (check_type)
3087 {
David Benjamin676d1e72014-07-08 14:34:10 -04003088 if (s->s3->tmp.certificate_types &&
3089 memchr(s->s3->tmp.certificate_types, check_type, s->s3->tmp.num_certificate_types))
Adam Langley95c29f32014-06-20 12:00:00 -07003090 {
Adam Langley95c29f32014-06-20 12:00:00 -07003091 rv |= CERT_PKEY_CERT_TYPE;
Adam Langley95c29f32014-06-20 12:00:00 -07003092 }
3093 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
3094 goto end;
3095 }
3096 else
3097 rv |= CERT_PKEY_CERT_TYPE;
3098
3099
3100 ca_dn = s->s3->tmp.ca_names;
3101
3102 if (!sk_X509_NAME_num(ca_dn))
3103 rv |= CERT_PKEY_ISSUER_NAME;
3104
3105 if (!(rv & CERT_PKEY_ISSUER_NAME))
3106 {
3107 if (ssl_check_ca_name(ca_dn, x))
3108 rv |= CERT_PKEY_ISSUER_NAME;
3109 }
3110 if (!(rv & CERT_PKEY_ISSUER_NAME))
3111 {
3112 for (i = 0; i < sk_X509_num(chain); i++)
3113 {
3114 X509 *xtmp = sk_X509_value(chain, i);
3115 if (ssl_check_ca_name(ca_dn, xtmp))
3116 {
3117 rv |= CERT_PKEY_ISSUER_NAME;
3118 break;
3119 }
3120 }
3121 }
3122 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
3123 goto end;
3124 }
3125 else
3126 rv |= CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE;
3127
3128 if (!check_flags || (rv & check_flags) == check_flags)
3129 rv |= CERT_PKEY_VALID;
3130
3131 end:
3132
3133 if (TLS1_get_version(s) >= TLS1_2_VERSION)
3134 {
3135 if (cpk->valid_flags & CERT_PKEY_EXPLICIT_SIGN)
3136 rv |= CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_SIGN;
3137 else if (cpk->digest)
3138 rv |= CERT_PKEY_SIGN;
3139 }
3140 else
3141 rv |= CERT_PKEY_SIGN|CERT_PKEY_EXPLICIT_SIGN;
3142
3143 /* When checking a CERT_PKEY structure all flags are irrelevant
3144 * if the chain is invalid.
3145 */
3146 if (!check_flags)
3147 {
3148 if (rv & CERT_PKEY_VALID)
3149 cpk->valid_flags = rv;
3150 else
3151 {
3152 /* Preserve explicit sign flag, clear rest */
3153 cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
3154 return 0;
3155 }
3156 }
3157 return rv;
3158 }
3159
3160/* Set validity of certificates in an SSL structure */
3161void tls1_set_cert_validity(SSL *s)
3162 {
3163 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
3164 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
Adam Langley95c29f32014-06-20 12:00:00 -07003165 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
3166 }
3167/* User level utiity function to check a chain is suitable */
3168int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
3169 {
3170 return tls1_check_chain(s, x, pk, chain, -1);
3171 }
3172